hns_enet.c 64 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532
  1. /*
  2. * Copyright (c) 2014-2015 Hisilicon Limited.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. */
  9. #include <linux/clk.h>
  10. #include <linux/cpumask.h>
  11. #include <linux/etherdevice.h>
  12. #include <linux/if_vlan.h>
  13. #include <linux/interrupt.h>
  14. #include <linux/io.h>
  15. #include <linux/ip.h>
  16. #include <linux/ipv6.h>
  17. #include <linux/module.h>
  18. #include <linux/phy.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/skbuff.h>
  21. #include "hnae.h"
  22. #include "hns_enet.h"
  23. #include "hns_dsaf_mac.h"
  24. #define NIC_MAX_Q_PER_VF 16
  25. #define HNS_NIC_TX_TIMEOUT (5 * HZ)
  26. #define SERVICE_TIMER_HZ (1 * HZ)
  27. #define NIC_TX_CLEAN_MAX_NUM 256
  28. #define NIC_RX_CLEAN_MAX_NUM 64
  29. #define RCB_IRQ_NOT_INITED 0
  30. #define RCB_IRQ_INITED 1
  31. #define HNS_BUFFER_SIZE_2048 2048
  32. #define BD_MAX_SEND_SIZE 8191
  33. #define SKB_TMP_LEN(SKB) \
  34. (((SKB)->transport_header - (SKB)->mac_header) + tcp_hdrlen(SKB))
  35. static void fill_v2_desc(struct hnae_ring *ring, void *priv,
  36. int size, dma_addr_t dma, int frag_end,
  37. int buf_num, enum hns_desc_type type, int mtu)
  38. {
  39. struct hnae_desc *desc = &ring->desc[ring->next_to_use];
  40. struct hnae_desc_cb *desc_cb = &ring->desc_cb[ring->next_to_use];
  41. struct iphdr *iphdr;
  42. struct ipv6hdr *ipv6hdr;
  43. struct sk_buff *skb;
  44. __be16 protocol;
  45. u8 bn_pid = 0;
  46. u8 rrcfv = 0;
  47. u8 ip_offset = 0;
  48. u8 tvsvsn = 0;
  49. u16 mss = 0;
  50. u8 l4_len = 0;
  51. u16 paylen = 0;
  52. desc_cb->priv = priv;
  53. desc_cb->length = size;
  54. desc_cb->dma = dma;
  55. desc_cb->type = type;
  56. desc->addr = cpu_to_le64(dma);
  57. desc->tx.send_size = cpu_to_le16((u16)size);
  58. /* config bd buffer end */
  59. hnae_set_bit(rrcfv, HNSV2_TXD_VLD_B, 1);
  60. hnae_set_field(bn_pid, HNSV2_TXD_BUFNUM_M, 0, buf_num - 1);
  61. /* fill port_id in the tx bd for sending management pkts */
  62. hnae_set_field(bn_pid, HNSV2_TXD_PORTID_M,
  63. HNSV2_TXD_PORTID_S, ring->q->handle->dport_id);
  64. if (type == DESC_TYPE_SKB) {
  65. skb = (struct sk_buff *)priv;
  66. if (skb->ip_summed == CHECKSUM_PARTIAL) {
  67. skb_reset_mac_len(skb);
  68. protocol = skb->protocol;
  69. ip_offset = ETH_HLEN;
  70. if (protocol == htons(ETH_P_8021Q)) {
  71. ip_offset += VLAN_HLEN;
  72. protocol = vlan_get_protocol(skb);
  73. skb->protocol = protocol;
  74. }
  75. if (skb->protocol == htons(ETH_P_IP)) {
  76. iphdr = ip_hdr(skb);
  77. hnae_set_bit(rrcfv, HNSV2_TXD_L3CS_B, 1);
  78. hnae_set_bit(rrcfv, HNSV2_TXD_L4CS_B, 1);
  79. /* check for tcp/udp header */
  80. if (iphdr->protocol == IPPROTO_TCP &&
  81. skb_is_gso(skb)) {
  82. hnae_set_bit(tvsvsn,
  83. HNSV2_TXD_TSE_B, 1);
  84. l4_len = tcp_hdrlen(skb);
  85. mss = skb_shinfo(skb)->gso_size;
  86. paylen = skb->len - SKB_TMP_LEN(skb);
  87. }
  88. } else if (skb->protocol == htons(ETH_P_IPV6)) {
  89. hnae_set_bit(tvsvsn, HNSV2_TXD_IPV6_B, 1);
  90. ipv6hdr = ipv6_hdr(skb);
  91. hnae_set_bit(rrcfv, HNSV2_TXD_L4CS_B, 1);
  92. /* check for tcp/udp header */
  93. if (ipv6hdr->nexthdr == IPPROTO_TCP &&
  94. skb_is_gso(skb) && skb_is_gso_v6(skb)) {
  95. hnae_set_bit(tvsvsn,
  96. HNSV2_TXD_TSE_B, 1);
  97. l4_len = tcp_hdrlen(skb);
  98. mss = skb_shinfo(skb)->gso_size;
  99. paylen = skb->len - SKB_TMP_LEN(skb);
  100. }
  101. }
  102. desc->tx.ip_offset = ip_offset;
  103. desc->tx.tse_vlan_snap_v6_sctp_nth = tvsvsn;
  104. desc->tx.mss = cpu_to_le16(mss);
  105. desc->tx.l4_len = l4_len;
  106. desc->tx.paylen = cpu_to_le16(paylen);
  107. }
  108. }
  109. hnae_set_bit(rrcfv, HNSV2_TXD_FE_B, frag_end);
  110. desc->tx.bn_pid = bn_pid;
  111. desc->tx.ra_ri_cs_fe_vld = rrcfv;
  112. ring_ptr_move_fw(ring, next_to_use);
  113. }
  114. static const struct acpi_device_id hns_enet_acpi_match[] = {
  115. { "HISI00C1", 0 },
  116. { "HISI00C2", 0 },
  117. { },
  118. };
  119. MODULE_DEVICE_TABLE(acpi, hns_enet_acpi_match);
  120. static void fill_desc(struct hnae_ring *ring, void *priv,
  121. int size, dma_addr_t dma, int frag_end,
  122. int buf_num, enum hns_desc_type type, int mtu)
  123. {
  124. struct hnae_desc *desc = &ring->desc[ring->next_to_use];
  125. struct hnae_desc_cb *desc_cb = &ring->desc_cb[ring->next_to_use];
  126. struct sk_buff *skb;
  127. __be16 protocol;
  128. u32 ip_offset;
  129. u32 asid_bufnum_pid = 0;
  130. u32 flag_ipoffset = 0;
  131. desc_cb->priv = priv;
  132. desc_cb->length = size;
  133. desc_cb->dma = dma;
  134. desc_cb->type = type;
  135. desc->addr = cpu_to_le64(dma);
  136. desc->tx.send_size = cpu_to_le16((u16)size);
  137. /*config bd buffer end */
  138. flag_ipoffset |= 1 << HNS_TXD_VLD_B;
  139. asid_bufnum_pid |= buf_num << HNS_TXD_BUFNUM_S;
  140. if (type == DESC_TYPE_SKB) {
  141. skb = (struct sk_buff *)priv;
  142. if (skb->ip_summed == CHECKSUM_PARTIAL) {
  143. protocol = skb->protocol;
  144. ip_offset = ETH_HLEN;
  145. /*if it is a SW VLAN check the next protocol*/
  146. if (protocol == htons(ETH_P_8021Q)) {
  147. ip_offset += VLAN_HLEN;
  148. protocol = vlan_get_protocol(skb);
  149. skb->protocol = protocol;
  150. }
  151. if (skb->protocol == htons(ETH_P_IP)) {
  152. flag_ipoffset |= 1 << HNS_TXD_L3CS_B;
  153. /* check for tcp/udp header */
  154. flag_ipoffset |= 1 << HNS_TXD_L4CS_B;
  155. } else if (skb->protocol == htons(ETH_P_IPV6)) {
  156. /* ipv6 has not l3 cs, check for L4 header */
  157. flag_ipoffset |= 1 << HNS_TXD_L4CS_B;
  158. }
  159. flag_ipoffset |= ip_offset << HNS_TXD_IPOFFSET_S;
  160. }
  161. }
  162. flag_ipoffset |= frag_end << HNS_TXD_FE_B;
  163. desc->tx.asid_bufnum_pid = cpu_to_le16(asid_bufnum_pid);
  164. desc->tx.flag_ipoffset = cpu_to_le32(flag_ipoffset);
  165. ring_ptr_move_fw(ring, next_to_use);
  166. }
  167. static void unfill_desc(struct hnae_ring *ring)
  168. {
  169. ring_ptr_move_bw(ring, next_to_use);
  170. }
  171. static int hns_nic_maybe_stop_tx(
  172. struct sk_buff **out_skb, int *bnum, struct hnae_ring *ring)
  173. {
  174. struct sk_buff *skb = *out_skb;
  175. struct sk_buff *new_skb = NULL;
  176. int buf_num;
  177. /* no. of segments (plus a header) */
  178. buf_num = skb_shinfo(skb)->nr_frags + 1;
  179. if (unlikely(buf_num > ring->max_desc_num_per_pkt)) {
  180. if (ring_space(ring) < 1)
  181. return -EBUSY;
  182. new_skb = skb_copy(skb, GFP_ATOMIC);
  183. if (!new_skb)
  184. return -ENOMEM;
  185. dev_kfree_skb_any(skb);
  186. *out_skb = new_skb;
  187. buf_num = 1;
  188. } else if (buf_num > ring_space(ring)) {
  189. return -EBUSY;
  190. }
  191. *bnum = buf_num;
  192. return 0;
  193. }
  194. static int hns_nic_maybe_stop_tso(
  195. struct sk_buff **out_skb, int *bnum, struct hnae_ring *ring)
  196. {
  197. int i;
  198. int size;
  199. int buf_num;
  200. int frag_num;
  201. struct sk_buff *skb = *out_skb;
  202. struct sk_buff *new_skb = NULL;
  203. struct skb_frag_struct *frag;
  204. size = skb_headlen(skb);
  205. buf_num = (size + BD_MAX_SEND_SIZE - 1) / BD_MAX_SEND_SIZE;
  206. frag_num = skb_shinfo(skb)->nr_frags;
  207. for (i = 0; i < frag_num; i++) {
  208. frag = &skb_shinfo(skb)->frags[i];
  209. size = skb_frag_size(frag);
  210. buf_num += (size + BD_MAX_SEND_SIZE - 1) / BD_MAX_SEND_SIZE;
  211. }
  212. if (unlikely(buf_num > ring->max_desc_num_per_pkt)) {
  213. buf_num = (skb->len + BD_MAX_SEND_SIZE - 1) / BD_MAX_SEND_SIZE;
  214. if (ring_space(ring) < buf_num)
  215. return -EBUSY;
  216. /* manual split the send packet */
  217. new_skb = skb_copy(skb, GFP_ATOMIC);
  218. if (!new_skb)
  219. return -ENOMEM;
  220. dev_kfree_skb_any(skb);
  221. *out_skb = new_skb;
  222. } else if (ring_space(ring) < buf_num) {
  223. return -EBUSY;
  224. }
  225. *bnum = buf_num;
  226. return 0;
  227. }
  228. static void fill_tso_desc(struct hnae_ring *ring, void *priv,
  229. int size, dma_addr_t dma, int frag_end,
  230. int buf_num, enum hns_desc_type type, int mtu)
  231. {
  232. int frag_buf_num;
  233. int sizeoflast;
  234. int k;
  235. frag_buf_num = (size + BD_MAX_SEND_SIZE - 1) / BD_MAX_SEND_SIZE;
  236. sizeoflast = size % BD_MAX_SEND_SIZE;
  237. sizeoflast = sizeoflast ? sizeoflast : BD_MAX_SEND_SIZE;
  238. /* when the frag size is bigger than hardware, split this frag */
  239. for (k = 0; k < frag_buf_num; k++)
  240. fill_v2_desc(ring, priv,
  241. (k == frag_buf_num - 1) ?
  242. sizeoflast : BD_MAX_SEND_SIZE,
  243. dma + BD_MAX_SEND_SIZE * k,
  244. frag_end && (k == frag_buf_num - 1) ? 1 : 0,
  245. buf_num,
  246. (type == DESC_TYPE_SKB && !k) ?
  247. DESC_TYPE_SKB : DESC_TYPE_PAGE,
  248. mtu);
  249. }
  250. netdev_tx_t hns_nic_net_xmit_hw(struct net_device *ndev,
  251. struct sk_buff *skb,
  252. struct hns_nic_ring_data *ring_data)
  253. {
  254. struct hns_nic_priv *priv = netdev_priv(ndev);
  255. struct hnae_ring *ring = ring_data->ring;
  256. struct device *dev = ring_to_dev(ring);
  257. struct netdev_queue *dev_queue;
  258. struct skb_frag_struct *frag;
  259. int buf_num;
  260. int seg_num;
  261. dma_addr_t dma;
  262. int size, next_to_use;
  263. int i;
  264. switch (priv->ops.maybe_stop_tx(&skb, &buf_num, ring)) {
  265. case -EBUSY:
  266. ring->stats.tx_busy++;
  267. goto out_net_tx_busy;
  268. case -ENOMEM:
  269. ring->stats.sw_err_cnt++;
  270. netdev_err(ndev, "no memory to xmit!\n");
  271. goto out_err_tx_ok;
  272. default:
  273. break;
  274. }
  275. /* no. of segments (plus a header) */
  276. seg_num = skb_shinfo(skb)->nr_frags + 1;
  277. next_to_use = ring->next_to_use;
  278. /* fill the first part */
  279. size = skb_headlen(skb);
  280. dma = dma_map_single(dev, skb->data, size, DMA_TO_DEVICE);
  281. if (dma_mapping_error(dev, dma)) {
  282. netdev_err(ndev, "TX head DMA map failed\n");
  283. ring->stats.sw_err_cnt++;
  284. goto out_err_tx_ok;
  285. }
  286. priv->ops.fill_desc(ring, skb, size, dma, seg_num == 1 ? 1 : 0,
  287. buf_num, DESC_TYPE_SKB, ndev->mtu);
  288. /* fill the fragments */
  289. for (i = 1; i < seg_num; i++) {
  290. frag = &skb_shinfo(skb)->frags[i - 1];
  291. size = skb_frag_size(frag);
  292. dma = skb_frag_dma_map(dev, frag, 0, size, DMA_TO_DEVICE);
  293. if (dma_mapping_error(dev, dma)) {
  294. netdev_err(ndev, "TX frag(%d) DMA map failed\n", i);
  295. ring->stats.sw_err_cnt++;
  296. goto out_map_frag_fail;
  297. }
  298. priv->ops.fill_desc(ring, skb_frag_page(frag), size, dma,
  299. seg_num - 1 == i ? 1 : 0, buf_num,
  300. DESC_TYPE_PAGE, ndev->mtu);
  301. }
  302. /*complete translate all packets*/
  303. dev_queue = netdev_get_tx_queue(ndev, skb->queue_mapping);
  304. netdev_tx_sent_queue(dev_queue, skb->len);
  305. netif_trans_update(ndev);
  306. ndev->stats.tx_bytes += skb->len;
  307. ndev->stats.tx_packets++;
  308. wmb(); /* commit all data before submit */
  309. assert(skb->queue_mapping < priv->ae_handle->q_num);
  310. hnae_queue_xmit(priv->ae_handle->qs[skb->queue_mapping], buf_num);
  311. ring->stats.tx_pkts++;
  312. ring->stats.tx_bytes += skb->len;
  313. return NETDEV_TX_OK;
  314. out_map_frag_fail:
  315. while (ring->next_to_use != next_to_use) {
  316. unfill_desc(ring);
  317. if (ring->next_to_use != next_to_use)
  318. dma_unmap_page(dev,
  319. ring->desc_cb[ring->next_to_use].dma,
  320. ring->desc_cb[ring->next_to_use].length,
  321. DMA_TO_DEVICE);
  322. else
  323. dma_unmap_single(dev,
  324. ring->desc_cb[next_to_use].dma,
  325. ring->desc_cb[next_to_use].length,
  326. DMA_TO_DEVICE);
  327. }
  328. out_err_tx_ok:
  329. dev_kfree_skb_any(skb);
  330. return NETDEV_TX_OK;
  331. out_net_tx_busy:
  332. netif_stop_subqueue(ndev, skb->queue_mapping);
  333. /* Herbert's original patch had:
  334. * smp_mb__after_netif_stop_queue();
  335. * but since that doesn't exist yet, just open code it.
  336. */
  337. smp_mb();
  338. return NETDEV_TX_BUSY;
  339. }
  340. /**
  341. * hns_nic_get_headlen - determine size of header for RSC/LRO/GRO/FCOE
  342. * @data: pointer to the start of the headers
  343. * @max: total length of section to find headers in
  344. *
  345. * This function is meant to determine the length of headers that will
  346. * be recognized by hardware for LRO, GRO, and RSC offloads. The main
  347. * motivation of doing this is to only perform one pull for IPv4 TCP
  348. * packets so that we can do basic things like calculating the gso_size
  349. * based on the average data per packet.
  350. **/
  351. static unsigned int hns_nic_get_headlen(unsigned char *data, u32 flag,
  352. unsigned int max_size)
  353. {
  354. unsigned char *network;
  355. u8 hlen;
  356. /* this should never happen, but better safe than sorry */
  357. if (max_size < ETH_HLEN)
  358. return max_size;
  359. /* initialize network frame pointer */
  360. network = data;
  361. /* set first protocol and move network header forward */
  362. network += ETH_HLEN;
  363. /* handle any vlan tag if present */
  364. if (hnae_get_field(flag, HNS_RXD_VLAN_M, HNS_RXD_VLAN_S)
  365. == HNS_RX_FLAG_VLAN_PRESENT) {
  366. if ((typeof(max_size))(network - data) > (max_size - VLAN_HLEN))
  367. return max_size;
  368. network += VLAN_HLEN;
  369. }
  370. /* handle L3 protocols */
  371. if (hnae_get_field(flag, HNS_RXD_L3ID_M, HNS_RXD_L3ID_S)
  372. == HNS_RX_FLAG_L3ID_IPV4) {
  373. if ((typeof(max_size))(network - data) >
  374. (max_size - sizeof(struct iphdr)))
  375. return max_size;
  376. /* access ihl as a u8 to avoid unaligned access on ia64 */
  377. hlen = (network[0] & 0x0F) << 2;
  378. /* verify hlen meets minimum size requirements */
  379. if (hlen < sizeof(struct iphdr))
  380. return network - data;
  381. /* record next protocol if header is present */
  382. } else if (hnae_get_field(flag, HNS_RXD_L3ID_M, HNS_RXD_L3ID_S)
  383. == HNS_RX_FLAG_L3ID_IPV6) {
  384. if ((typeof(max_size))(network - data) >
  385. (max_size - sizeof(struct ipv6hdr)))
  386. return max_size;
  387. /* record next protocol */
  388. hlen = sizeof(struct ipv6hdr);
  389. } else {
  390. return network - data;
  391. }
  392. /* relocate pointer to start of L4 header */
  393. network += hlen;
  394. /* finally sort out TCP/UDP */
  395. if (hnae_get_field(flag, HNS_RXD_L4ID_M, HNS_RXD_L4ID_S)
  396. == HNS_RX_FLAG_L4ID_TCP) {
  397. if ((typeof(max_size))(network - data) >
  398. (max_size - sizeof(struct tcphdr)))
  399. return max_size;
  400. /* access doff as a u8 to avoid unaligned access on ia64 */
  401. hlen = (network[12] & 0xF0) >> 2;
  402. /* verify hlen meets minimum size requirements */
  403. if (hlen < sizeof(struct tcphdr))
  404. return network - data;
  405. network += hlen;
  406. } else if (hnae_get_field(flag, HNS_RXD_L4ID_M, HNS_RXD_L4ID_S)
  407. == HNS_RX_FLAG_L4ID_UDP) {
  408. if ((typeof(max_size))(network - data) >
  409. (max_size - sizeof(struct udphdr)))
  410. return max_size;
  411. network += sizeof(struct udphdr);
  412. }
  413. /* If everything has gone correctly network should be the
  414. * data section of the packet and will be the end of the header.
  415. * If not then it probably represents the end of the last recognized
  416. * header.
  417. */
  418. if ((typeof(max_size))(network - data) < max_size)
  419. return network - data;
  420. else
  421. return max_size;
  422. }
  423. static void hns_nic_reuse_page(struct sk_buff *skb, int i,
  424. struct hnae_ring *ring, int pull_len,
  425. struct hnae_desc_cb *desc_cb)
  426. {
  427. struct hnae_desc *desc;
  428. int truesize, size;
  429. int last_offset;
  430. bool twobufs;
  431. twobufs = ((PAGE_SIZE < 8192) &&
  432. hnae_buf_size(ring) == HNS_BUFFER_SIZE_2048);
  433. desc = &ring->desc[ring->next_to_clean];
  434. size = le16_to_cpu(desc->rx.size);
  435. if (twobufs) {
  436. truesize = hnae_buf_size(ring);
  437. } else {
  438. truesize = ALIGN(size, L1_CACHE_BYTES);
  439. last_offset = hnae_page_size(ring) - hnae_buf_size(ring);
  440. }
  441. skb_add_rx_frag(skb, i, desc_cb->priv, desc_cb->page_offset + pull_len,
  442. size - pull_len, truesize - pull_len);
  443. /* avoid re-using remote pages,flag default unreuse */
  444. if (unlikely(page_to_nid(desc_cb->priv) != numa_node_id()))
  445. return;
  446. if (twobufs) {
  447. /* if we are only owner of page we can reuse it */
  448. if (likely(page_count(desc_cb->priv) == 1)) {
  449. /* flip page offset to other buffer */
  450. desc_cb->page_offset ^= truesize;
  451. desc_cb->reuse_flag = 1;
  452. /* bump ref count on page before it is given*/
  453. get_page(desc_cb->priv);
  454. }
  455. return;
  456. }
  457. /* move offset up to the next cache line */
  458. desc_cb->page_offset += truesize;
  459. if (desc_cb->page_offset <= last_offset) {
  460. desc_cb->reuse_flag = 1;
  461. /* bump ref count on page before it is given*/
  462. get_page(desc_cb->priv);
  463. }
  464. }
  465. static void get_v2rx_desc_bnum(u32 bnum_flag, int *out_bnum)
  466. {
  467. *out_bnum = hnae_get_field(bnum_flag,
  468. HNS_RXD_BUFNUM_M, HNS_RXD_BUFNUM_S) + 1;
  469. }
  470. static void get_rx_desc_bnum(u32 bnum_flag, int *out_bnum)
  471. {
  472. *out_bnum = hnae_get_field(bnum_flag,
  473. HNS_RXD_BUFNUM_M, HNS_RXD_BUFNUM_S);
  474. }
  475. static void hns_nic_rx_checksum(struct hns_nic_ring_data *ring_data,
  476. struct sk_buff *skb, u32 flag)
  477. {
  478. struct net_device *netdev = ring_data->napi.dev;
  479. u32 l3id;
  480. u32 l4id;
  481. /* check if RX checksum offload is enabled */
  482. if (unlikely(!(netdev->features & NETIF_F_RXCSUM)))
  483. return;
  484. /* In hardware, we only support checksum for the following protocols:
  485. * 1) IPv4,
  486. * 2) TCP(over IPv4 or IPv6),
  487. * 3) UDP(over IPv4 or IPv6),
  488. * 4) SCTP(over IPv4 or IPv6)
  489. * but we support many L3(IPv4, IPv6, MPLS, PPPoE etc) and L4(TCP,
  490. * UDP, GRE, SCTP, IGMP, ICMP etc.) protocols.
  491. *
  492. * Hardware limitation:
  493. * Our present hardware RX Descriptor lacks L3/L4 checksum "Status &
  494. * Error" bit (which usually can be used to indicate whether checksum
  495. * was calculated by the hardware and if there was any error encountered
  496. * during checksum calculation).
  497. *
  498. * Software workaround:
  499. * We do get info within the RX descriptor about the kind of L3/L4
  500. * protocol coming in the packet and the error status. These errors
  501. * might not just be checksum errors but could be related to version,
  502. * length of IPv4, UDP, TCP etc.
  503. * Because there is no-way of knowing if it is a L3/L4 error due to bad
  504. * checksum or any other L3/L4 error, we will not (cannot) convey
  505. * checksum status for such cases to upper stack and will not maintain
  506. * the RX L3/L4 checksum counters as well.
  507. */
  508. l3id = hnae_get_field(flag, HNS_RXD_L3ID_M, HNS_RXD_L3ID_S);
  509. l4id = hnae_get_field(flag, HNS_RXD_L4ID_M, HNS_RXD_L4ID_S);
  510. /* check L3 protocol for which checksum is supported */
  511. if ((l3id != HNS_RX_FLAG_L3ID_IPV4) && (l3id != HNS_RX_FLAG_L3ID_IPV6))
  512. return;
  513. /* check for any(not just checksum)flagged L3 protocol errors */
  514. if (unlikely(hnae_get_bit(flag, HNS_RXD_L3E_B)))
  515. return;
  516. /* we do not support checksum of fragmented packets */
  517. if (unlikely(hnae_get_bit(flag, HNS_RXD_FRAG_B)))
  518. return;
  519. /* check L4 protocol for which checksum is supported */
  520. if ((l4id != HNS_RX_FLAG_L4ID_TCP) &&
  521. (l4id != HNS_RX_FLAG_L4ID_UDP) &&
  522. (l4id != HNS_RX_FLAG_L4ID_SCTP))
  523. return;
  524. /* check for any(not just checksum)flagged L4 protocol errors */
  525. if (unlikely(hnae_get_bit(flag, HNS_RXD_L4E_B)))
  526. return;
  527. /* now, this has to be a packet with valid RX checksum */
  528. skb->ip_summed = CHECKSUM_UNNECESSARY;
  529. }
  530. static int hns_nic_poll_rx_skb(struct hns_nic_ring_data *ring_data,
  531. struct sk_buff **out_skb, int *out_bnum)
  532. {
  533. struct hnae_ring *ring = ring_data->ring;
  534. struct net_device *ndev = ring_data->napi.dev;
  535. struct hns_nic_priv *priv = netdev_priv(ndev);
  536. struct sk_buff *skb;
  537. struct hnae_desc *desc;
  538. struct hnae_desc_cb *desc_cb;
  539. unsigned char *va;
  540. int bnum, length, i;
  541. int pull_len;
  542. u32 bnum_flag;
  543. desc = &ring->desc[ring->next_to_clean];
  544. desc_cb = &ring->desc_cb[ring->next_to_clean];
  545. prefetch(desc);
  546. va = (unsigned char *)desc_cb->buf + desc_cb->page_offset;
  547. /* prefetch first cache line of first page */
  548. prefetch(va);
  549. #if L1_CACHE_BYTES < 128
  550. prefetch(va + L1_CACHE_BYTES);
  551. #endif
  552. skb = *out_skb = napi_alloc_skb(&ring_data->napi,
  553. HNS_RX_HEAD_SIZE);
  554. if (unlikely(!skb)) {
  555. netdev_err(ndev, "alloc rx skb fail\n");
  556. ring->stats.sw_err_cnt++;
  557. return -ENOMEM;
  558. }
  559. prefetchw(skb->data);
  560. length = le16_to_cpu(desc->rx.pkt_len);
  561. bnum_flag = le32_to_cpu(desc->rx.ipoff_bnum_pid_flag);
  562. priv->ops.get_rxd_bnum(bnum_flag, &bnum);
  563. *out_bnum = bnum;
  564. if (length <= HNS_RX_HEAD_SIZE) {
  565. memcpy(__skb_put(skb, length), va, ALIGN(length, sizeof(long)));
  566. /* we can reuse buffer as-is, just make sure it is local */
  567. if (likely(page_to_nid(desc_cb->priv) == numa_node_id()))
  568. desc_cb->reuse_flag = 1;
  569. else /* this page cannot be reused so discard it */
  570. put_page(desc_cb->priv);
  571. ring_ptr_move_fw(ring, next_to_clean);
  572. if (unlikely(bnum != 1)) { /* check err*/
  573. *out_bnum = 1;
  574. goto out_bnum_err;
  575. }
  576. } else {
  577. ring->stats.seg_pkt_cnt++;
  578. pull_len = hns_nic_get_headlen(va, bnum_flag, HNS_RX_HEAD_SIZE);
  579. memcpy(__skb_put(skb, pull_len), va,
  580. ALIGN(pull_len, sizeof(long)));
  581. hns_nic_reuse_page(skb, 0, ring, pull_len, desc_cb);
  582. ring_ptr_move_fw(ring, next_to_clean);
  583. if (unlikely(bnum >= (int)MAX_SKB_FRAGS)) { /* check err*/
  584. *out_bnum = 1;
  585. goto out_bnum_err;
  586. }
  587. for (i = 1; i < bnum; i++) {
  588. desc = &ring->desc[ring->next_to_clean];
  589. desc_cb = &ring->desc_cb[ring->next_to_clean];
  590. hns_nic_reuse_page(skb, i, ring, 0, desc_cb);
  591. ring_ptr_move_fw(ring, next_to_clean);
  592. }
  593. }
  594. /* check except process, free skb and jump the desc */
  595. if (unlikely((!bnum) || (bnum > ring->max_desc_num_per_pkt))) {
  596. out_bnum_err:
  597. *out_bnum = *out_bnum ? *out_bnum : 1; /* ntc moved,cannot 0*/
  598. netdev_err(ndev, "invalid bnum(%d,%d,%d,%d),%016llx,%016llx\n",
  599. bnum, ring->max_desc_num_per_pkt,
  600. length, (int)MAX_SKB_FRAGS,
  601. ((u64 *)desc)[0], ((u64 *)desc)[1]);
  602. ring->stats.err_bd_num++;
  603. dev_kfree_skb_any(skb);
  604. return -EDOM;
  605. }
  606. bnum_flag = le32_to_cpu(desc->rx.ipoff_bnum_pid_flag);
  607. if (unlikely(!hnae_get_bit(bnum_flag, HNS_RXD_VLD_B))) {
  608. netdev_err(ndev, "no valid bd,%016llx,%016llx\n",
  609. ((u64 *)desc)[0], ((u64 *)desc)[1]);
  610. ring->stats.non_vld_descs++;
  611. dev_kfree_skb_any(skb);
  612. return -EINVAL;
  613. }
  614. if (unlikely((!desc->rx.pkt_len) ||
  615. hnae_get_bit(bnum_flag, HNS_RXD_DROP_B))) {
  616. ring->stats.err_pkt_len++;
  617. dev_kfree_skb_any(skb);
  618. return -EFAULT;
  619. }
  620. if (unlikely(hnae_get_bit(bnum_flag, HNS_RXD_L2E_B))) {
  621. ring->stats.l2_err++;
  622. dev_kfree_skb_any(skb);
  623. return -EFAULT;
  624. }
  625. ring->stats.rx_pkts++;
  626. ring->stats.rx_bytes += skb->len;
  627. /* indicate to upper stack if our hardware has already calculated
  628. * the RX checksum
  629. */
  630. hns_nic_rx_checksum(ring_data, skb, bnum_flag);
  631. return 0;
  632. }
  633. static void
  634. hns_nic_alloc_rx_buffers(struct hns_nic_ring_data *ring_data, int cleand_count)
  635. {
  636. int i, ret;
  637. struct hnae_desc_cb res_cbs;
  638. struct hnae_desc_cb *desc_cb;
  639. struct hnae_ring *ring = ring_data->ring;
  640. struct net_device *ndev = ring_data->napi.dev;
  641. for (i = 0; i < cleand_count; i++) {
  642. desc_cb = &ring->desc_cb[ring->next_to_use];
  643. if (desc_cb->reuse_flag) {
  644. ring->stats.reuse_pg_cnt++;
  645. hnae_reuse_buffer(ring, ring->next_to_use);
  646. } else {
  647. ret = hnae_reserve_buffer_map(ring, &res_cbs);
  648. if (ret) {
  649. ring->stats.sw_err_cnt++;
  650. netdev_err(ndev, "hnae reserve buffer map failed.\n");
  651. break;
  652. }
  653. hnae_replace_buffer(ring, ring->next_to_use, &res_cbs);
  654. }
  655. ring_ptr_move_fw(ring, next_to_use);
  656. }
  657. wmb(); /* make all data has been write before submit */
  658. writel_relaxed(i, ring->io_base + RCB_REG_HEAD);
  659. }
  660. /* return error number for error or number of desc left to take
  661. */
  662. static void hns_nic_rx_up_pro(struct hns_nic_ring_data *ring_data,
  663. struct sk_buff *skb)
  664. {
  665. struct net_device *ndev = ring_data->napi.dev;
  666. skb->protocol = eth_type_trans(skb, ndev);
  667. (void)napi_gro_receive(&ring_data->napi, skb);
  668. }
  669. static int hns_desc_unused(struct hnae_ring *ring)
  670. {
  671. int ntc = ring->next_to_clean;
  672. int ntu = ring->next_to_use;
  673. return ((ntc >= ntu) ? 0 : ring->desc_num) + ntc - ntu;
  674. }
  675. #define HNS_LOWEST_LATENCY_RATE 27 /* 27 MB/s */
  676. #define HNS_LOW_LATENCY_RATE 80 /* 80 MB/s */
  677. #define HNS_COAL_BDNUM 3
  678. static u32 hns_coal_rx_bdnum(struct hnae_ring *ring)
  679. {
  680. bool coal_enable = ring->q->handle->coal_adapt_en;
  681. if (coal_enable &&
  682. ring->coal_last_rx_bytes > HNS_LOWEST_LATENCY_RATE)
  683. return HNS_COAL_BDNUM;
  684. else
  685. return 0;
  686. }
  687. static void hns_update_rx_rate(struct hnae_ring *ring)
  688. {
  689. bool coal_enable = ring->q->handle->coal_adapt_en;
  690. u32 time_passed_ms;
  691. u64 total_bytes;
  692. if (!coal_enable ||
  693. time_before(jiffies, ring->coal_last_jiffies + (HZ >> 4)))
  694. return;
  695. /* ring->stats.rx_bytes overflowed */
  696. if (ring->coal_last_rx_bytes > ring->stats.rx_bytes) {
  697. ring->coal_last_rx_bytes = ring->stats.rx_bytes;
  698. ring->coal_last_jiffies = jiffies;
  699. return;
  700. }
  701. total_bytes = ring->stats.rx_bytes - ring->coal_last_rx_bytes;
  702. time_passed_ms = jiffies_to_msecs(jiffies - ring->coal_last_jiffies);
  703. do_div(total_bytes, time_passed_ms);
  704. ring->coal_rx_rate = total_bytes >> 10;
  705. ring->coal_last_rx_bytes = ring->stats.rx_bytes;
  706. ring->coal_last_jiffies = jiffies;
  707. }
  708. /**
  709. * smooth_alg - smoothing algrithm for adjusting coalesce parameter
  710. **/
  711. static u32 smooth_alg(u32 new_param, u32 old_param)
  712. {
  713. u32 gap = (new_param > old_param) ? new_param - old_param
  714. : old_param - new_param;
  715. if (gap > 8)
  716. gap >>= 3;
  717. if (new_param > old_param)
  718. return old_param + gap;
  719. else
  720. return old_param - gap;
  721. }
  722. /**
  723. * hns_nic_adp_coalesce - self adapte coalesce according to rx rate
  724. * @ring_data: pointer to hns_nic_ring_data
  725. **/
  726. static void hns_nic_adpt_coalesce(struct hns_nic_ring_data *ring_data)
  727. {
  728. struct hnae_ring *ring = ring_data->ring;
  729. struct hnae_handle *handle = ring->q->handle;
  730. u32 new_coal_param, old_coal_param = ring->coal_param;
  731. if (ring->coal_rx_rate < HNS_LOWEST_LATENCY_RATE)
  732. new_coal_param = HNAE_LOWEST_LATENCY_COAL_PARAM;
  733. else if (ring->coal_rx_rate < HNS_LOW_LATENCY_RATE)
  734. new_coal_param = HNAE_LOW_LATENCY_COAL_PARAM;
  735. else
  736. new_coal_param = HNAE_BULK_LATENCY_COAL_PARAM;
  737. if (new_coal_param == old_coal_param &&
  738. new_coal_param == handle->coal_param)
  739. return;
  740. new_coal_param = smooth_alg(new_coal_param, old_coal_param);
  741. ring->coal_param = new_coal_param;
  742. /**
  743. * Because all ring in one port has one coalesce param, when one ring
  744. * calculate its own coalesce param, it cannot write to hardware at
  745. * once. There are three conditions as follows:
  746. * 1. current ring's coalesce param is larger than the hardware.
  747. * 2. or ring which adapt last time can change again.
  748. * 3. timeout.
  749. */
  750. if (new_coal_param == handle->coal_param) {
  751. handle->coal_last_jiffies = jiffies;
  752. handle->coal_ring_idx = ring_data->queue_index;
  753. } else if (new_coal_param > handle->coal_param ||
  754. handle->coal_ring_idx == ring_data->queue_index ||
  755. time_after(jiffies, handle->coal_last_jiffies + (HZ >> 4))) {
  756. handle->dev->ops->set_coalesce_usecs(handle,
  757. new_coal_param);
  758. handle->dev->ops->set_coalesce_frames(handle,
  759. 1, new_coal_param);
  760. handle->coal_param = new_coal_param;
  761. handle->coal_ring_idx = ring_data->queue_index;
  762. handle->coal_last_jiffies = jiffies;
  763. }
  764. }
  765. static int hns_nic_rx_poll_one(struct hns_nic_ring_data *ring_data,
  766. int budget, void *v)
  767. {
  768. struct hnae_ring *ring = ring_data->ring;
  769. struct sk_buff *skb;
  770. int num, bnum;
  771. #define RCB_NOF_ALLOC_RX_BUFF_ONCE 16
  772. int recv_pkts, recv_bds, clean_count, err;
  773. int unused_count = hns_desc_unused(ring);
  774. num = readl_relaxed(ring->io_base + RCB_REG_FBDNUM);
  775. rmb(); /* make sure num taken effect before the other data is touched */
  776. recv_pkts = 0, recv_bds = 0, clean_count = 0;
  777. num -= unused_count;
  778. while (recv_pkts < budget && recv_bds < num) {
  779. /* reuse or realloc buffers */
  780. if (clean_count + unused_count >= RCB_NOF_ALLOC_RX_BUFF_ONCE) {
  781. hns_nic_alloc_rx_buffers(ring_data,
  782. clean_count + unused_count);
  783. clean_count = 0;
  784. unused_count = hns_desc_unused(ring);
  785. }
  786. /* poll one pkt */
  787. err = hns_nic_poll_rx_skb(ring_data, &skb, &bnum);
  788. if (unlikely(!skb)) /* this fault cannot be repaired */
  789. goto out;
  790. recv_bds += bnum;
  791. clean_count += bnum;
  792. if (unlikely(err)) { /* do jump the err */
  793. recv_pkts++;
  794. continue;
  795. }
  796. /* do update ip stack process*/
  797. ((void (*)(struct hns_nic_ring_data *, struct sk_buff *))v)(
  798. ring_data, skb);
  799. recv_pkts++;
  800. }
  801. out:
  802. /* make all data has been write before submit */
  803. if (clean_count + unused_count > 0)
  804. hns_nic_alloc_rx_buffers(ring_data,
  805. clean_count + unused_count);
  806. return recv_pkts;
  807. }
  808. static bool hns_nic_rx_fini_pro(struct hns_nic_ring_data *ring_data)
  809. {
  810. struct hnae_ring *ring = ring_data->ring;
  811. int num = 0;
  812. bool rx_stopped;
  813. hns_update_rx_rate(ring);
  814. /* for hardware bug fixed */
  815. ring_data->ring->q->handle->dev->ops->toggle_ring_irq(ring, 0);
  816. num = readl_relaxed(ring->io_base + RCB_REG_FBDNUM);
  817. if (num <= hns_coal_rx_bdnum(ring)) {
  818. if (ring->q->handle->coal_adapt_en)
  819. hns_nic_adpt_coalesce(ring_data);
  820. rx_stopped = true;
  821. } else {
  822. ring_data->ring->q->handle->dev->ops->toggle_ring_irq(
  823. ring_data->ring, 1);
  824. rx_stopped = false;
  825. }
  826. return rx_stopped;
  827. }
  828. static bool hns_nic_rx_fini_pro_v2(struct hns_nic_ring_data *ring_data)
  829. {
  830. struct hnae_ring *ring = ring_data->ring;
  831. int num;
  832. hns_update_rx_rate(ring);
  833. num = readl_relaxed(ring->io_base + RCB_REG_FBDNUM);
  834. if (num <= hns_coal_rx_bdnum(ring)) {
  835. if (ring->q->handle->coal_adapt_en)
  836. hns_nic_adpt_coalesce(ring_data);
  837. return true;
  838. }
  839. return false;
  840. }
  841. static inline void hns_nic_reclaim_one_desc(struct hnae_ring *ring,
  842. int *bytes, int *pkts)
  843. {
  844. struct hnae_desc_cb *desc_cb = &ring->desc_cb[ring->next_to_clean];
  845. (*pkts) += (desc_cb->type == DESC_TYPE_SKB);
  846. (*bytes) += desc_cb->length;
  847. /* desc_cb will be cleaned, after hnae_free_buffer_detach*/
  848. hnae_free_buffer_detach(ring, ring->next_to_clean);
  849. ring_ptr_move_fw(ring, next_to_clean);
  850. }
  851. static int is_valid_clean_head(struct hnae_ring *ring, int h)
  852. {
  853. int u = ring->next_to_use;
  854. int c = ring->next_to_clean;
  855. if (unlikely(h > ring->desc_num))
  856. return 0;
  857. assert(u > 0 && u < ring->desc_num);
  858. assert(c > 0 && c < ring->desc_num);
  859. assert(u != c && h != c); /* must be checked before call this func */
  860. return u > c ? (h > c && h <= u) : (h > c || h <= u);
  861. }
  862. /* netif_tx_lock will turn down the performance, set only when necessary */
  863. #ifdef CONFIG_NET_POLL_CONTROLLER
  864. #define NETIF_TX_LOCK(ring) spin_lock(&(ring)->lock)
  865. #define NETIF_TX_UNLOCK(ring) spin_unlock(&(ring)->lock)
  866. #else
  867. #define NETIF_TX_LOCK(ring)
  868. #define NETIF_TX_UNLOCK(ring)
  869. #endif
  870. /* reclaim all desc in one budget
  871. * return error or number of desc left
  872. */
  873. static int hns_nic_tx_poll_one(struct hns_nic_ring_data *ring_data,
  874. int budget, void *v)
  875. {
  876. struct hnae_ring *ring = ring_data->ring;
  877. struct net_device *ndev = ring_data->napi.dev;
  878. struct netdev_queue *dev_queue;
  879. struct hns_nic_priv *priv = netdev_priv(ndev);
  880. int head;
  881. int bytes, pkts;
  882. NETIF_TX_LOCK(ring);
  883. head = readl_relaxed(ring->io_base + RCB_REG_HEAD);
  884. rmb(); /* make sure head is ready before touch any data */
  885. if (is_ring_empty(ring) || head == ring->next_to_clean) {
  886. NETIF_TX_UNLOCK(ring);
  887. return 0; /* no data to poll */
  888. }
  889. if (!is_valid_clean_head(ring, head)) {
  890. netdev_err(ndev, "wrong head (%d, %d-%d)\n", head,
  891. ring->next_to_use, ring->next_to_clean);
  892. ring->stats.io_err_cnt++;
  893. NETIF_TX_UNLOCK(ring);
  894. return -EIO;
  895. }
  896. bytes = 0;
  897. pkts = 0;
  898. while (head != ring->next_to_clean) {
  899. hns_nic_reclaim_one_desc(ring, &bytes, &pkts);
  900. /* issue prefetch for next Tx descriptor */
  901. prefetch(&ring->desc_cb[ring->next_to_clean]);
  902. }
  903. NETIF_TX_UNLOCK(ring);
  904. dev_queue = netdev_get_tx_queue(ndev, ring_data->queue_index);
  905. netdev_tx_completed_queue(dev_queue, pkts, bytes);
  906. if (unlikely(priv->link && !netif_carrier_ok(ndev)))
  907. netif_carrier_on(ndev);
  908. if (unlikely(pkts && netif_carrier_ok(ndev) &&
  909. (ring_space(ring) >= ring->max_desc_num_per_pkt * 2))) {
  910. /* Make sure that anybody stopping the queue after this
  911. * sees the new next_to_clean.
  912. */
  913. smp_mb();
  914. if (netif_tx_queue_stopped(dev_queue) &&
  915. !test_bit(NIC_STATE_DOWN, &priv->state)) {
  916. netif_tx_wake_queue(dev_queue);
  917. ring->stats.restart_queue++;
  918. }
  919. }
  920. return 0;
  921. }
  922. static bool hns_nic_tx_fini_pro(struct hns_nic_ring_data *ring_data)
  923. {
  924. struct hnae_ring *ring = ring_data->ring;
  925. int head;
  926. ring_data->ring->q->handle->dev->ops->toggle_ring_irq(ring, 0);
  927. head = readl_relaxed(ring->io_base + RCB_REG_HEAD);
  928. if (head != ring->next_to_clean) {
  929. ring_data->ring->q->handle->dev->ops->toggle_ring_irq(
  930. ring_data->ring, 1);
  931. return false;
  932. } else {
  933. return true;
  934. }
  935. }
  936. static bool hns_nic_tx_fini_pro_v2(struct hns_nic_ring_data *ring_data)
  937. {
  938. struct hnae_ring *ring = ring_data->ring;
  939. int head = readl_relaxed(ring->io_base + RCB_REG_HEAD);
  940. if (head == ring->next_to_clean)
  941. return true;
  942. else
  943. return false;
  944. }
  945. static void hns_nic_tx_clr_all_bufs(struct hns_nic_ring_data *ring_data)
  946. {
  947. struct hnae_ring *ring = ring_data->ring;
  948. struct net_device *ndev = ring_data->napi.dev;
  949. struct netdev_queue *dev_queue;
  950. int head;
  951. int bytes, pkts;
  952. NETIF_TX_LOCK(ring);
  953. head = ring->next_to_use; /* ntu :soft setted ring position*/
  954. bytes = 0;
  955. pkts = 0;
  956. while (head != ring->next_to_clean)
  957. hns_nic_reclaim_one_desc(ring, &bytes, &pkts);
  958. NETIF_TX_UNLOCK(ring);
  959. dev_queue = netdev_get_tx_queue(ndev, ring_data->queue_index);
  960. netdev_tx_reset_queue(dev_queue);
  961. }
  962. static int hns_nic_common_poll(struct napi_struct *napi, int budget)
  963. {
  964. int clean_complete = 0;
  965. struct hns_nic_ring_data *ring_data =
  966. container_of(napi, struct hns_nic_ring_data, napi);
  967. struct hnae_ring *ring = ring_data->ring;
  968. try_again:
  969. clean_complete += ring_data->poll_one(
  970. ring_data, budget - clean_complete,
  971. ring_data->ex_process);
  972. if (clean_complete < budget) {
  973. if (ring_data->fini_process(ring_data)) {
  974. napi_complete(napi);
  975. ring->q->handle->dev->ops->toggle_ring_irq(ring, 0);
  976. } else {
  977. goto try_again;
  978. }
  979. }
  980. return clean_complete;
  981. }
  982. static irqreturn_t hns_irq_handle(int irq, void *dev)
  983. {
  984. struct hns_nic_ring_data *ring_data = (struct hns_nic_ring_data *)dev;
  985. ring_data->ring->q->handle->dev->ops->toggle_ring_irq(
  986. ring_data->ring, 1);
  987. napi_schedule(&ring_data->napi);
  988. return IRQ_HANDLED;
  989. }
  990. /**
  991. *hns_nic_adjust_link - adjust net work mode by the phy stat or new param
  992. *@ndev: net device
  993. */
  994. static void hns_nic_adjust_link(struct net_device *ndev)
  995. {
  996. struct hns_nic_priv *priv = netdev_priv(ndev);
  997. struct hnae_handle *h = priv->ae_handle;
  998. int state = 1;
  999. if (ndev->phydev) {
  1000. h->dev->ops->adjust_link(h, ndev->phydev->speed,
  1001. ndev->phydev->duplex);
  1002. state = ndev->phydev->link;
  1003. }
  1004. state = state && h->dev->ops->get_status(h);
  1005. if (state != priv->link) {
  1006. if (state) {
  1007. netif_carrier_on(ndev);
  1008. netif_tx_wake_all_queues(ndev);
  1009. netdev_info(ndev, "link up\n");
  1010. } else {
  1011. netif_carrier_off(ndev);
  1012. netdev_info(ndev, "link down\n");
  1013. }
  1014. priv->link = state;
  1015. }
  1016. }
  1017. /**
  1018. *hns_nic_init_phy - init phy
  1019. *@ndev: net device
  1020. *@h: ae handle
  1021. * Return 0 on success, negative on failure
  1022. */
  1023. int hns_nic_init_phy(struct net_device *ndev, struct hnae_handle *h)
  1024. {
  1025. struct phy_device *phy_dev = h->phy_dev;
  1026. int ret;
  1027. if (!h->phy_dev)
  1028. return 0;
  1029. if (h->phy_if != PHY_INTERFACE_MODE_XGMII) {
  1030. phy_dev->dev_flags = 0;
  1031. ret = phy_connect_direct(ndev, phy_dev, hns_nic_adjust_link,
  1032. h->phy_if);
  1033. } else {
  1034. ret = phy_attach_direct(ndev, phy_dev, 0, h->phy_if);
  1035. }
  1036. if (unlikely(ret))
  1037. return -ENODEV;
  1038. phy_dev->supported &= h->if_support;
  1039. phy_dev->advertising = phy_dev->supported;
  1040. if (h->phy_if == PHY_INTERFACE_MODE_XGMII)
  1041. phy_dev->autoneg = false;
  1042. return 0;
  1043. }
  1044. static int hns_nic_ring_open(struct net_device *netdev, int idx)
  1045. {
  1046. struct hns_nic_priv *priv = netdev_priv(netdev);
  1047. struct hnae_handle *h = priv->ae_handle;
  1048. napi_enable(&priv->ring_data[idx].napi);
  1049. enable_irq(priv->ring_data[idx].ring->irq);
  1050. h->dev->ops->toggle_ring_irq(priv->ring_data[idx].ring, 0);
  1051. return 0;
  1052. }
  1053. static int hns_nic_net_set_mac_address(struct net_device *ndev, void *p)
  1054. {
  1055. struct hns_nic_priv *priv = netdev_priv(ndev);
  1056. struct hnae_handle *h = priv->ae_handle;
  1057. struct sockaddr *mac_addr = p;
  1058. int ret;
  1059. if (!mac_addr || !is_valid_ether_addr((const u8 *)mac_addr->sa_data))
  1060. return -EADDRNOTAVAIL;
  1061. ret = h->dev->ops->set_mac_addr(h, mac_addr->sa_data);
  1062. if (ret) {
  1063. netdev_err(ndev, "set_mac_address fail, ret=%d!\n", ret);
  1064. return ret;
  1065. }
  1066. memcpy(ndev->dev_addr, mac_addr->sa_data, ndev->addr_len);
  1067. return 0;
  1068. }
  1069. void hns_nic_update_stats(struct net_device *netdev)
  1070. {
  1071. struct hns_nic_priv *priv = netdev_priv(netdev);
  1072. struct hnae_handle *h = priv->ae_handle;
  1073. h->dev->ops->update_stats(h, &netdev->stats);
  1074. }
  1075. /* set mac addr if it is configed. or leave it to the AE driver */
  1076. static void hns_init_mac_addr(struct net_device *ndev)
  1077. {
  1078. struct hns_nic_priv *priv = netdev_priv(ndev);
  1079. if (!device_get_mac_address(priv->dev, ndev->dev_addr, ETH_ALEN)) {
  1080. eth_hw_addr_random(ndev);
  1081. dev_warn(priv->dev, "No valid mac, use random mac %pM",
  1082. ndev->dev_addr);
  1083. }
  1084. }
  1085. static void hns_nic_ring_close(struct net_device *netdev, int idx)
  1086. {
  1087. struct hns_nic_priv *priv = netdev_priv(netdev);
  1088. struct hnae_handle *h = priv->ae_handle;
  1089. h->dev->ops->toggle_ring_irq(priv->ring_data[idx].ring, 1);
  1090. disable_irq(priv->ring_data[idx].ring->irq);
  1091. napi_disable(&priv->ring_data[idx].napi);
  1092. }
  1093. static int hns_nic_init_affinity_mask(int q_num, int ring_idx,
  1094. struct hnae_ring *ring, cpumask_t *mask)
  1095. {
  1096. int cpu;
  1097. /* Diffrent irq banlance between 16core and 32core.
  1098. * The cpu mask set by ring index according to the ring flag
  1099. * which indicate the ring is tx or rx.
  1100. */
  1101. if (q_num == num_possible_cpus()) {
  1102. if (is_tx_ring(ring))
  1103. cpu = ring_idx;
  1104. else
  1105. cpu = ring_idx - q_num;
  1106. } else {
  1107. if (is_tx_ring(ring))
  1108. cpu = ring_idx * 2;
  1109. else
  1110. cpu = (ring_idx - q_num) * 2 + 1;
  1111. }
  1112. cpumask_clear(mask);
  1113. cpumask_set_cpu(cpu, mask);
  1114. return cpu;
  1115. }
  1116. static int hns_nic_init_irq(struct hns_nic_priv *priv)
  1117. {
  1118. struct hnae_handle *h = priv->ae_handle;
  1119. struct hns_nic_ring_data *rd;
  1120. int i;
  1121. int ret;
  1122. int cpu;
  1123. for (i = 0; i < h->q_num * 2; i++) {
  1124. rd = &priv->ring_data[i];
  1125. if (rd->ring->irq_init_flag == RCB_IRQ_INITED)
  1126. break;
  1127. snprintf(rd->ring->ring_name, RCB_RING_NAME_LEN,
  1128. "%s-%s%d", priv->netdev->name,
  1129. (is_tx_ring(rd->ring) ? "tx" : "rx"), rd->queue_index);
  1130. rd->ring->ring_name[RCB_RING_NAME_LEN - 1] = '\0';
  1131. ret = request_irq(rd->ring->irq,
  1132. hns_irq_handle, 0, rd->ring->ring_name, rd);
  1133. if (ret) {
  1134. netdev_err(priv->netdev, "request irq(%d) fail\n",
  1135. rd->ring->irq);
  1136. return ret;
  1137. }
  1138. disable_irq(rd->ring->irq);
  1139. cpu = hns_nic_init_affinity_mask(h->q_num, i,
  1140. rd->ring, &rd->mask);
  1141. if (cpu_online(cpu))
  1142. irq_set_affinity_hint(rd->ring->irq,
  1143. &rd->mask);
  1144. rd->ring->irq_init_flag = RCB_IRQ_INITED;
  1145. }
  1146. return 0;
  1147. }
  1148. static int hns_nic_net_up(struct net_device *ndev)
  1149. {
  1150. struct hns_nic_priv *priv = netdev_priv(ndev);
  1151. struct hnae_handle *h = priv->ae_handle;
  1152. int i, j;
  1153. int ret;
  1154. ret = hns_nic_init_irq(priv);
  1155. if (ret != 0) {
  1156. netdev_err(ndev, "hns init irq failed! ret=%d\n", ret);
  1157. return ret;
  1158. }
  1159. for (i = 0; i < h->q_num * 2; i++) {
  1160. ret = hns_nic_ring_open(ndev, i);
  1161. if (ret)
  1162. goto out_has_some_queues;
  1163. }
  1164. ret = h->dev->ops->set_mac_addr(h, ndev->dev_addr);
  1165. if (ret)
  1166. goto out_set_mac_addr_err;
  1167. ret = h->dev->ops->start ? h->dev->ops->start(h) : 0;
  1168. if (ret)
  1169. goto out_start_err;
  1170. if (ndev->phydev)
  1171. phy_start(ndev->phydev);
  1172. clear_bit(NIC_STATE_DOWN, &priv->state);
  1173. (void)mod_timer(&priv->service_timer, jiffies + SERVICE_TIMER_HZ);
  1174. return 0;
  1175. out_start_err:
  1176. netif_stop_queue(ndev);
  1177. out_set_mac_addr_err:
  1178. out_has_some_queues:
  1179. for (j = i - 1; j >= 0; j--)
  1180. hns_nic_ring_close(ndev, j);
  1181. set_bit(NIC_STATE_DOWN, &priv->state);
  1182. return ret;
  1183. }
  1184. static void hns_nic_net_down(struct net_device *ndev)
  1185. {
  1186. int i;
  1187. struct hnae_ae_ops *ops;
  1188. struct hns_nic_priv *priv = netdev_priv(ndev);
  1189. if (test_and_set_bit(NIC_STATE_DOWN, &priv->state))
  1190. return;
  1191. (void)del_timer_sync(&priv->service_timer);
  1192. netif_tx_stop_all_queues(ndev);
  1193. netif_carrier_off(ndev);
  1194. netif_tx_disable(ndev);
  1195. priv->link = 0;
  1196. if (ndev->phydev)
  1197. phy_stop(ndev->phydev);
  1198. ops = priv->ae_handle->dev->ops;
  1199. if (ops->stop)
  1200. ops->stop(priv->ae_handle);
  1201. netif_tx_stop_all_queues(ndev);
  1202. for (i = priv->ae_handle->q_num - 1; i >= 0; i--) {
  1203. hns_nic_ring_close(ndev, i);
  1204. hns_nic_ring_close(ndev, i + priv->ae_handle->q_num);
  1205. /* clean tx buffers*/
  1206. hns_nic_tx_clr_all_bufs(priv->ring_data + i);
  1207. }
  1208. }
  1209. void hns_nic_net_reset(struct net_device *ndev)
  1210. {
  1211. struct hns_nic_priv *priv = netdev_priv(ndev);
  1212. struct hnae_handle *handle = priv->ae_handle;
  1213. while (test_and_set_bit(NIC_STATE_RESETTING, &priv->state))
  1214. usleep_range(1000, 2000);
  1215. (void)hnae_reinit_handle(handle);
  1216. clear_bit(NIC_STATE_RESETTING, &priv->state);
  1217. }
  1218. void hns_nic_net_reinit(struct net_device *netdev)
  1219. {
  1220. struct hns_nic_priv *priv = netdev_priv(netdev);
  1221. enum hnae_port_type type = priv->ae_handle->port_type;
  1222. netif_trans_update(priv->netdev);
  1223. while (test_and_set_bit(NIC_STATE_REINITING, &priv->state))
  1224. usleep_range(1000, 2000);
  1225. hns_nic_net_down(netdev);
  1226. /* Only do hns_nic_net_reset in debug mode
  1227. * because of hardware limitation.
  1228. */
  1229. if (type == HNAE_PORT_DEBUG)
  1230. hns_nic_net_reset(netdev);
  1231. (void)hns_nic_net_up(netdev);
  1232. clear_bit(NIC_STATE_REINITING, &priv->state);
  1233. }
  1234. static int hns_nic_net_open(struct net_device *ndev)
  1235. {
  1236. struct hns_nic_priv *priv = netdev_priv(ndev);
  1237. struct hnae_handle *h = priv->ae_handle;
  1238. int ret;
  1239. if (test_bit(NIC_STATE_TESTING, &priv->state))
  1240. return -EBUSY;
  1241. priv->link = 0;
  1242. netif_carrier_off(ndev);
  1243. ret = netif_set_real_num_tx_queues(ndev, h->q_num);
  1244. if (ret < 0) {
  1245. netdev_err(ndev, "netif_set_real_num_tx_queues fail, ret=%d!\n",
  1246. ret);
  1247. return ret;
  1248. }
  1249. ret = netif_set_real_num_rx_queues(ndev, h->q_num);
  1250. if (ret < 0) {
  1251. netdev_err(ndev,
  1252. "netif_set_real_num_rx_queues fail, ret=%d!\n", ret);
  1253. return ret;
  1254. }
  1255. ret = hns_nic_net_up(ndev);
  1256. if (ret) {
  1257. netdev_err(ndev,
  1258. "hns net up fail, ret=%d!\n", ret);
  1259. return ret;
  1260. }
  1261. return 0;
  1262. }
  1263. static int hns_nic_net_stop(struct net_device *ndev)
  1264. {
  1265. hns_nic_net_down(ndev);
  1266. return 0;
  1267. }
  1268. static void hns_tx_timeout_reset(struct hns_nic_priv *priv);
  1269. static void hns_nic_net_timeout(struct net_device *ndev)
  1270. {
  1271. struct hns_nic_priv *priv = netdev_priv(ndev);
  1272. hns_tx_timeout_reset(priv);
  1273. }
  1274. static int hns_nic_do_ioctl(struct net_device *netdev, struct ifreq *ifr,
  1275. int cmd)
  1276. {
  1277. struct phy_device *phy_dev = netdev->phydev;
  1278. if (!netif_running(netdev))
  1279. return -EINVAL;
  1280. if (!phy_dev)
  1281. return -ENOTSUPP;
  1282. return phy_mii_ioctl(phy_dev, ifr, cmd);
  1283. }
  1284. /* use only for netconsole to poll with the device without interrupt */
  1285. #ifdef CONFIG_NET_POLL_CONTROLLER
  1286. void hns_nic_poll_controller(struct net_device *ndev)
  1287. {
  1288. struct hns_nic_priv *priv = netdev_priv(ndev);
  1289. unsigned long flags;
  1290. int i;
  1291. local_irq_save(flags);
  1292. for (i = 0; i < priv->ae_handle->q_num * 2; i++)
  1293. napi_schedule(&priv->ring_data[i].napi);
  1294. local_irq_restore(flags);
  1295. }
  1296. #endif
  1297. static netdev_tx_t hns_nic_net_xmit(struct sk_buff *skb,
  1298. struct net_device *ndev)
  1299. {
  1300. struct hns_nic_priv *priv = netdev_priv(ndev);
  1301. assert(skb->queue_mapping < ndev->ae_handle->q_num);
  1302. return hns_nic_net_xmit_hw(ndev, skb,
  1303. &tx_ring_data(priv, skb->queue_mapping));
  1304. }
  1305. static void hns_nic_drop_rx_fetch(struct hns_nic_ring_data *ring_data,
  1306. struct sk_buff *skb)
  1307. {
  1308. dev_kfree_skb_any(skb);
  1309. }
  1310. #define HNS_LB_TX_RING 0
  1311. static struct sk_buff *hns_assemble_skb(struct net_device *ndev)
  1312. {
  1313. struct sk_buff *skb;
  1314. struct ethhdr *ethhdr;
  1315. int frame_len;
  1316. /* allocate test skb */
  1317. skb = alloc_skb(64, GFP_KERNEL);
  1318. if (!skb)
  1319. return NULL;
  1320. skb_put(skb, 64);
  1321. skb->dev = ndev;
  1322. memset(skb->data, 0xFF, skb->len);
  1323. /* must be tcp/ip package */
  1324. ethhdr = (struct ethhdr *)skb->data;
  1325. ethhdr->h_proto = htons(ETH_P_IP);
  1326. frame_len = skb->len & (~1ul);
  1327. memset(&skb->data[frame_len / 2], 0xAA,
  1328. frame_len / 2 - 1);
  1329. skb->queue_mapping = HNS_LB_TX_RING;
  1330. return skb;
  1331. }
  1332. static int hns_enable_serdes_lb(struct net_device *ndev)
  1333. {
  1334. struct hns_nic_priv *priv = netdev_priv(ndev);
  1335. struct hnae_handle *h = priv->ae_handle;
  1336. struct hnae_ae_ops *ops = h->dev->ops;
  1337. int speed, duplex;
  1338. int ret;
  1339. ret = ops->set_loopback(h, MAC_INTERNALLOOP_SERDES, 1);
  1340. if (ret)
  1341. return ret;
  1342. ret = ops->start ? ops->start(h) : 0;
  1343. if (ret)
  1344. return ret;
  1345. /* link adjust duplex*/
  1346. if (h->phy_if != PHY_INTERFACE_MODE_XGMII)
  1347. speed = 1000;
  1348. else
  1349. speed = 10000;
  1350. duplex = 1;
  1351. ops->adjust_link(h, speed, duplex);
  1352. /* wait h/w ready */
  1353. mdelay(300);
  1354. return 0;
  1355. }
  1356. static void hns_disable_serdes_lb(struct net_device *ndev)
  1357. {
  1358. struct hns_nic_priv *priv = netdev_priv(ndev);
  1359. struct hnae_handle *h = priv->ae_handle;
  1360. struct hnae_ae_ops *ops = h->dev->ops;
  1361. ops->stop(h);
  1362. ops->set_loopback(h, MAC_INTERNALLOOP_SERDES, 0);
  1363. }
  1364. /**
  1365. *hns_nic_clear_all_rx_fetch - clear the chip fetched descriptions. The
  1366. *function as follows:
  1367. * 1. if one rx ring has found the page_offset is not equal 0 between head
  1368. * and tail, it means that the chip fetched the wrong descs for the ring
  1369. * which buffer size is 4096.
  1370. * 2. we set the chip serdes loopback and set rss indirection to the ring.
  1371. * 3. construct 64-bytes ip broadcast packages, wait the associated rx ring
  1372. * recieving all packages and it will fetch new descriptions.
  1373. * 4. recover to the original state.
  1374. *
  1375. *@ndev: net device
  1376. */
  1377. static int hns_nic_clear_all_rx_fetch(struct net_device *ndev)
  1378. {
  1379. struct hns_nic_priv *priv = netdev_priv(ndev);
  1380. struct hnae_handle *h = priv->ae_handle;
  1381. struct hnae_ae_ops *ops = h->dev->ops;
  1382. struct hns_nic_ring_data *rd;
  1383. struct hnae_ring *ring;
  1384. struct sk_buff *skb;
  1385. u32 *org_indir;
  1386. u32 *cur_indir;
  1387. int indir_size;
  1388. int head, tail;
  1389. int fetch_num;
  1390. int i, j;
  1391. bool found;
  1392. int retry_times;
  1393. int ret = 0;
  1394. /* alloc indir memory */
  1395. indir_size = ops->get_rss_indir_size(h) * sizeof(*org_indir);
  1396. org_indir = kzalloc(indir_size, GFP_KERNEL);
  1397. if (!org_indir)
  1398. return -ENOMEM;
  1399. /* store the orginal indirection */
  1400. ops->get_rss(h, org_indir, NULL, NULL);
  1401. cur_indir = kzalloc(indir_size, GFP_KERNEL);
  1402. if (!cur_indir) {
  1403. ret = -ENOMEM;
  1404. goto cur_indir_alloc_err;
  1405. }
  1406. /* set loopback */
  1407. if (hns_enable_serdes_lb(ndev)) {
  1408. ret = -EINVAL;
  1409. goto enable_serdes_lb_err;
  1410. }
  1411. /* foreach every rx ring to clear fetch desc */
  1412. for (i = 0; i < h->q_num; i++) {
  1413. ring = &h->qs[i]->rx_ring;
  1414. head = readl_relaxed(ring->io_base + RCB_REG_HEAD);
  1415. tail = readl_relaxed(ring->io_base + RCB_REG_TAIL);
  1416. found = false;
  1417. fetch_num = ring_dist(ring, head, tail);
  1418. while (head != tail) {
  1419. if (ring->desc_cb[head].page_offset != 0) {
  1420. found = true;
  1421. break;
  1422. }
  1423. head++;
  1424. if (head == ring->desc_num)
  1425. head = 0;
  1426. }
  1427. if (found) {
  1428. for (j = 0; j < indir_size / sizeof(*org_indir); j++)
  1429. cur_indir[j] = i;
  1430. ops->set_rss(h, cur_indir, NULL, 0);
  1431. for (j = 0; j < fetch_num; j++) {
  1432. /* alloc one skb and init */
  1433. skb = hns_assemble_skb(ndev);
  1434. if (!skb)
  1435. goto out;
  1436. rd = &tx_ring_data(priv, skb->queue_mapping);
  1437. hns_nic_net_xmit_hw(ndev, skb, rd);
  1438. retry_times = 0;
  1439. while (retry_times++ < 10) {
  1440. mdelay(10);
  1441. /* clean rx */
  1442. rd = &rx_ring_data(priv, i);
  1443. if (rd->poll_one(rd, fetch_num,
  1444. hns_nic_drop_rx_fetch))
  1445. break;
  1446. }
  1447. retry_times = 0;
  1448. while (retry_times++ < 10) {
  1449. mdelay(10);
  1450. /* clean tx ring 0 send package */
  1451. rd = &tx_ring_data(priv,
  1452. HNS_LB_TX_RING);
  1453. if (rd->poll_one(rd, fetch_num, NULL))
  1454. break;
  1455. }
  1456. }
  1457. }
  1458. }
  1459. out:
  1460. /* restore everything */
  1461. ops->set_rss(h, org_indir, NULL, 0);
  1462. hns_disable_serdes_lb(ndev);
  1463. enable_serdes_lb_err:
  1464. kfree(cur_indir);
  1465. cur_indir_alloc_err:
  1466. kfree(org_indir);
  1467. return ret;
  1468. }
  1469. static int hns_nic_change_mtu(struct net_device *ndev, int new_mtu)
  1470. {
  1471. struct hns_nic_priv *priv = netdev_priv(ndev);
  1472. struct hnae_handle *h = priv->ae_handle;
  1473. bool if_running = netif_running(ndev);
  1474. int ret;
  1475. /* MTU < 68 is an error and causes problems on some kernels */
  1476. if (new_mtu < 68)
  1477. return -EINVAL;
  1478. /* MTU no change */
  1479. if (new_mtu == ndev->mtu)
  1480. return 0;
  1481. if (!h->dev->ops->set_mtu)
  1482. return -ENOTSUPP;
  1483. if (if_running) {
  1484. (void)hns_nic_net_stop(ndev);
  1485. msleep(100);
  1486. }
  1487. if (priv->enet_ver != AE_VERSION_1 &&
  1488. ndev->mtu <= BD_SIZE_2048_MAX_MTU &&
  1489. new_mtu > BD_SIZE_2048_MAX_MTU) {
  1490. /* update desc */
  1491. hnae_reinit_all_ring_desc(h);
  1492. /* clear the package which the chip has fetched */
  1493. ret = hns_nic_clear_all_rx_fetch(ndev);
  1494. /* the page offset must be consist with desc */
  1495. hnae_reinit_all_ring_page_off(h);
  1496. if (ret) {
  1497. netdev_err(ndev, "clear the fetched desc fail\n");
  1498. goto out;
  1499. }
  1500. }
  1501. ret = h->dev->ops->set_mtu(h, new_mtu);
  1502. if (ret) {
  1503. netdev_err(ndev, "set mtu fail, return value %d\n",
  1504. ret);
  1505. goto out;
  1506. }
  1507. /* finally, set new mtu to netdevice */
  1508. ndev->mtu = new_mtu;
  1509. out:
  1510. if (if_running) {
  1511. if (hns_nic_net_open(ndev)) {
  1512. netdev_err(ndev, "hns net open fail\n");
  1513. ret = -EINVAL;
  1514. }
  1515. }
  1516. return ret;
  1517. }
  1518. static int hns_nic_set_features(struct net_device *netdev,
  1519. netdev_features_t features)
  1520. {
  1521. struct hns_nic_priv *priv = netdev_priv(netdev);
  1522. switch (priv->enet_ver) {
  1523. case AE_VERSION_1:
  1524. if (features & (NETIF_F_TSO | NETIF_F_TSO6))
  1525. netdev_info(netdev, "enet v1 do not support tso!\n");
  1526. break;
  1527. default:
  1528. if (features & (NETIF_F_TSO | NETIF_F_TSO6)) {
  1529. priv->ops.fill_desc = fill_tso_desc;
  1530. priv->ops.maybe_stop_tx = hns_nic_maybe_stop_tso;
  1531. /* The chip only support 7*4096 */
  1532. netif_set_gso_max_size(netdev, 7 * 4096);
  1533. } else {
  1534. priv->ops.fill_desc = fill_v2_desc;
  1535. priv->ops.maybe_stop_tx = hns_nic_maybe_stop_tx;
  1536. }
  1537. break;
  1538. }
  1539. netdev->features = features;
  1540. return 0;
  1541. }
  1542. static netdev_features_t hns_nic_fix_features(
  1543. struct net_device *netdev, netdev_features_t features)
  1544. {
  1545. struct hns_nic_priv *priv = netdev_priv(netdev);
  1546. switch (priv->enet_ver) {
  1547. case AE_VERSION_1:
  1548. features &= ~(NETIF_F_TSO | NETIF_F_TSO6 |
  1549. NETIF_F_HW_VLAN_CTAG_FILTER);
  1550. break;
  1551. default:
  1552. break;
  1553. }
  1554. return features;
  1555. }
  1556. static int hns_nic_uc_sync(struct net_device *netdev, const unsigned char *addr)
  1557. {
  1558. struct hns_nic_priv *priv = netdev_priv(netdev);
  1559. struct hnae_handle *h = priv->ae_handle;
  1560. if (h->dev->ops->add_uc_addr)
  1561. return h->dev->ops->add_uc_addr(h, addr);
  1562. return 0;
  1563. }
  1564. static int hns_nic_uc_unsync(struct net_device *netdev,
  1565. const unsigned char *addr)
  1566. {
  1567. struct hns_nic_priv *priv = netdev_priv(netdev);
  1568. struct hnae_handle *h = priv->ae_handle;
  1569. if (h->dev->ops->rm_uc_addr)
  1570. return h->dev->ops->rm_uc_addr(h, addr);
  1571. return 0;
  1572. }
  1573. /**
  1574. * nic_set_multicast_list - set mutl mac address
  1575. * @netdev: net device
  1576. * @p: mac address
  1577. *
  1578. * return void
  1579. */
  1580. void hns_set_multicast_list(struct net_device *ndev)
  1581. {
  1582. struct hns_nic_priv *priv = netdev_priv(ndev);
  1583. struct hnae_handle *h = priv->ae_handle;
  1584. struct netdev_hw_addr *ha = NULL;
  1585. if (!h) {
  1586. netdev_err(ndev, "hnae handle is null\n");
  1587. return;
  1588. }
  1589. if (h->dev->ops->clr_mc_addr)
  1590. if (h->dev->ops->clr_mc_addr(h))
  1591. netdev_err(ndev, "clear multicast address fail\n");
  1592. if (h->dev->ops->set_mc_addr) {
  1593. netdev_for_each_mc_addr(ha, ndev)
  1594. if (h->dev->ops->set_mc_addr(h, ha->addr))
  1595. netdev_err(ndev, "set multicast fail\n");
  1596. }
  1597. }
  1598. void hns_nic_set_rx_mode(struct net_device *ndev)
  1599. {
  1600. struct hns_nic_priv *priv = netdev_priv(ndev);
  1601. struct hnae_handle *h = priv->ae_handle;
  1602. if (h->dev->ops->set_promisc_mode) {
  1603. if (ndev->flags & IFF_PROMISC)
  1604. h->dev->ops->set_promisc_mode(h, 1);
  1605. else
  1606. h->dev->ops->set_promisc_mode(h, 0);
  1607. }
  1608. hns_set_multicast_list(ndev);
  1609. if (__dev_uc_sync(ndev, hns_nic_uc_sync, hns_nic_uc_unsync))
  1610. netdev_err(ndev, "sync uc address fail\n");
  1611. }
  1612. static void hns_nic_get_stats64(struct net_device *ndev,
  1613. struct rtnl_link_stats64 *stats)
  1614. {
  1615. int idx = 0;
  1616. u64 tx_bytes = 0;
  1617. u64 rx_bytes = 0;
  1618. u64 tx_pkts = 0;
  1619. u64 rx_pkts = 0;
  1620. struct hns_nic_priv *priv = netdev_priv(ndev);
  1621. struct hnae_handle *h = priv->ae_handle;
  1622. for (idx = 0; idx < h->q_num; idx++) {
  1623. tx_bytes += h->qs[idx]->tx_ring.stats.tx_bytes;
  1624. tx_pkts += h->qs[idx]->tx_ring.stats.tx_pkts;
  1625. rx_bytes += h->qs[idx]->rx_ring.stats.rx_bytes;
  1626. rx_pkts += h->qs[idx]->rx_ring.stats.rx_pkts;
  1627. }
  1628. stats->tx_bytes = tx_bytes;
  1629. stats->tx_packets = tx_pkts;
  1630. stats->rx_bytes = rx_bytes;
  1631. stats->rx_packets = rx_pkts;
  1632. stats->rx_errors = ndev->stats.rx_errors;
  1633. stats->multicast = ndev->stats.multicast;
  1634. stats->rx_length_errors = ndev->stats.rx_length_errors;
  1635. stats->rx_crc_errors = ndev->stats.rx_crc_errors;
  1636. stats->rx_missed_errors = ndev->stats.rx_missed_errors;
  1637. stats->tx_errors = ndev->stats.tx_errors;
  1638. stats->rx_dropped = ndev->stats.rx_dropped;
  1639. stats->tx_dropped = ndev->stats.tx_dropped;
  1640. stats->collisions = ndev->stats.collisions;
  1641. stats->rx_over_errors = ndev->stats.rx_over_errors;
  1642. stats->rx_frame_errors = ndev->stats.rx_frame_errors;
  1643. stats->rx_fifo_errors = ndev->stats.rx_fifo_errors;
  1644. stats->tx_aborted_errors = ndev->stats.tx_aborted_errors;
  1645. stats->tx_carrier_errors = ndev->stats.tx_carrier_errors;
  1646. stats->tx_fifo_errors = ndev->stats.tx_fifo_errors;
  1647. stats->tx_heartbeat_errors = ndev->stats.tx_heartbeat_errors;
  1648. stats->tx_window_errors = ndev->stats.tx_window_errors;
  1649. stats->rx_compressed = ndev->stats.rx_compressed;
  1650. stats->tx_compressed = ndev->stats.tx_compressed;
  1651. }
  1652. static u16
  1653. hns_nic_select_queue(struct net_device *ndev, struct sk_buff *skb,
  1654. void *accel_priv, select_queue_fallback_t fallback)
  1655. {
  1656. struct ethhdr *eth_hdr = (struct ethhdr *)skb->data;
  1657. struct hns_nic_priv *priv = netdev_priv(ndev);
  1658. /* fix hardware broadcast/multicast packets queue loopback */
  1659. if (!AE_IS_VER1(priv->enet_ver) &&
  1660. is_multicast_ether_addr(eth_hdr->h_dest))
  1661. return 0;
  1662. else
  1663. return fallback(ndev, skb);
  1664. }
  1665. static const struct net_device_ops hns_nic_netdev_ops = {
  1666. .ndo_open = hns_nic_net_open,
  1667. .ndo_stop = hns_nic_net_stop,
  1668. .ndo_start_xmit = hns_nic_net_xmit,
  1669. .ndo_tx_timeout = hns_nic_net_timeout,
  1670. .ndo_set_mac_address = hns_nic_net_set_mac_address,
  1671. .ndo_change_mtu = hns_nic_change_mtu,
  1672. .ndo_do_ioctl = hns_nic_do_ioctl,
  1673. .ndo_set_features = hns_nic_set_features,
  1674. .ndo_fix_features = hns_nic_fix_features,
  1675. .ndo_get_stats64 = hns_nic_get_stats64,
  1676. #ifdef CONFIG_NET_POLL_CONTROLLER
  1677. .ndo_poll_controller = hns_nic_poll_controller,
  1678. #endif
  1679. .ndo_set_rx_mode = hns_nic_set_rx_mode,
  1680. .ndo_select_queue = hns_nic_select_queue,
  1681. };
  1682. static void hns_nic_update_link_status(struct net_device *netdev)
  1683. {
  1684. struct hns_nic_priv *priv = netdev_priv(netdev);
  1685. struct hnae_handle *h = priv->ae_handle;
  1686. if (h->phy_dev) {
  1687. if (h->phy_if != PHY_INTERFACE_MODE_XGMII)
  1688. return;
  1689. (void)genphy_read_status(h->phy_dev);
  1690. }
  1691. hns_nic_adjust_link(netdev);
  1692. }
  1693. /* for dumping key regs*/
  1694. static void hns_nic_dump(struct hns_nic_priv *priv)
  1695. {
  1696. struct hnae_handle *h = priv->ae_handle;
  1697. struct hnae_ae_ops *ops = h->dev->ops;
  1698. u32 *data, reg_num, i;
  1699. if (ops->get_regs_len && ops->get_regs) {
  1700. reg_num = ops->get_regs_len(priv->ae_handle);
  1701. reg_num = (reg_num + 3ul) & ~3ul;
  1702. data = kcalloc(reg_num, sizeof(u32), GFP_KERNEL);
  1703. if (data) {
  1704. ops->get_regs(priv->ae_handle, data);
  1705. for (i = 0; i < reg_num; i += 4)
  1706. pr_info("0x%08x: 0x%08x 0x%08x 0x%08x 0x%08x\n",
  1707. i, data[i], data[i + 1],
  1708. data[i + 2], data[i + 3]);
  1709. kfree(data);
  1710. }
  1711. }
  1712. for (i = 0; i < h->q_num; i++) {
  1713. pr_info("tx_queue%d_next_to_clean:%d\n",
  1714. i, h->qs[i]->tx_ring.next_to_clean);
  1715. pr_info("tx_queue%d_next_to_use:%d\n",
  1716. i, h->qs[i]->tx_ring.next_to_use);
  1717. pr_info("rx_queue%d_next_to_clean:%d\n",
  1718. i, h->qs[i]->rx_ring.next_to_clean);
  1719. pr_info("rx_queue%d_next_to_use:%d\n",
  1720. i, h->qs[i]->rx_ring.next_to_use);
  1721. }
  1722. }
  1723. /* for resetting subtask */
  1724. static void hns_nic_reset_subtask(struct hns_nic_priv *priv)
  1725. {
  1726. enum hnae_port_type type = priv->ae_handle->port_type;
  1727. if (!test_bit(NIC_STATE2_RESET_REQUESTED, &priv->state))
  1728. return;
  1729. clear_bit(NIC_STATE2_RESET_REQUESTED, &priv->state);
  1730. /* If we're already down, removing or resetting, just bail */
  1731. if (test_bit(NIC_STATE_DOWN, &priv->state) ||
  1732. test_bit(NIC_STATE_REMOVING, &priv->state) ||
  1733. test_bit(NIC_STATE_RESETTING, &priv->state))
  1734. return;
  1735. hns_nic_dump(priv);
  1736. netdev_info(priv->netdev, "try to reset %s port!\n",
  1737. (type == HNAE_PORT_DEBUG ? "debug" : "service"));
  1738. rtnl_lock();
  1739. /* put off any impending NetWatchDogTimeout */
  1740. netif_trans_update(priv->netdev);
  1741. hns_nic_net_reinit(priv->netdev);
  1742. rtnl_unlock();
  1743. }
  1744. /* for doing service complete*/
  1745. static void hns_nic_service_event_complete(struct hns_nic_priv *priv)
  1746. {
  1747. WARN_ON(!test_bit(NIC_STATE_SERVICE_SCHED, &priv->state));
  1748. /* make sure to commit the things */
  1749. smp_mb__before_atomic();
  1750. clear_bit(NIC_STATE_SERVICE_SCHED, &priv->state);
  1751. }
  1752. static void hns_nic_service_task(struct work_struct *work)
  1753. {
  1754. struct hns_nic_priv *priv
  1755. = container_of(work, struct hns_nic_priv, service_task);
  1756. struct hnae_handle *h = priv->ae_handle;
  1757. hns_nic_update_link_status(priv->netdev);
  1758. h->dev->ops->update_led_status(h);
  1759. hns_nic_update_stats(priv->netdev);
  1760. hns_nic_reset_subtask(priv);
  1761. hns_nic_service_event_complete(priv);
  1762. }
  1763. static void hns_nic_task_schedule(struct hns_nic_priv *priv)
  1764. {
  1765. if (!test_bit(NIC_STATE_DOWN, &priv->state) &&
  1766. !test_bit(NIC_STATE_REMOVING, &priv->state) &&
  1767. !test_and_set_bit(NIC_STATE_SERVICE_SCHED, &priv->state))
  1768. (void)schedule_work(&priv->service_task);
  1769. }
  1770. static void hns_nic_service_timer(struct timer_list *t)
  1771. {
  1772. struct hns_nic_priv *priv = from_timer(priv, t, service_timer);
  1773. (void)mod_timer(&priv->service_timer, jiffies + SERVICE_TIMER_HZ);
  1774. hns_nic_task_schedule(priv);
  1775. }
  1776. /**
  1777. * hns_tx_timeout_reset - initiate reset due to Tx timeout
  1778. * @priv: driver private struct
  1779. **/
  1780. static void hns_tx_timeout_reset(struct hns_nic_priv *priv)
  1781. {
  1782. /* Do the reset outside of interrupt context */
  1783. if (!test_bit(NIC_STATE_DOWN, &priv->state)) {
  1784. set_bit(NIC_STATE2_RESET_REQUESTED, &priv->state);
  1785. netdev_warn(priv->netdev,
  1786. "initiating reset due to tx timeout(%llu,0x%lx)\n",
  1787. priv->tx_timeout_count, priv->state);
  1788. priv->tx_timeout_count++;
  1789. hns_nic_task_schedule(priv);
  1790. }
  1791. }
  1792. static int hns_nic_init_ring_data(struct hns_nic_priv *priv)
  1793. {
  1794. struct hnae_handle *h = priv->ae_handle;
  1795. struct hns_nic_ring_data *rd;
  1796. bool is_ver1 = AE_IS_VER1(priv->enet_ver);
  1797. int i;
  1798. if (h->q_num > NIC_MAX_Q_PER_VF) {
  1799. netdev_err(priv->netdev, "too much queue (%d)\n", h->q_num);
  1800. return -EINVAL;
  1801. }
  1802. priv->ring_data = kzalloc(h->q_num * sizeof(*priv->ring_data) * 2,
  1803. GFP_KERNEL);
  1804. if (!priv->ring_data)
  1805. return -ENOMEM;
  1806. for (i = 0; i < h->q_num; i++) {
  1807. rd = &priv->ring_data[i];
  1808. rd->queue_index = i;
  1809. rd->ring = &h->qs[i]->tx_ring;
  1810. rd->poll_one = hns_nic_tx_poll_one;
  1811. rd->fini_process = is_ver1 ? hns_nic_tx_fini_pro :
  1812. hns_nic_tx_fini_pro_v2;
  1813. netif_napi_add(priv->netdev, &rd->napi,
  1814. hns_nic_common_poll, NIC_TX_CLEAN_MAX_NUM);
  1815. rd->ring->irq_init_flag = RCB_IRQ_NOT_INITED;
  1816. }
  1817. for (i = h->q_num; i < h->q_num * 2; i++) {
  1818. rd = &priv->ring_data[i];
  1819. rd->queue_index = i - h->q_num;
  1820. rd->ring = &h->qs[i - h->q_num]->rx_ring;
  1821. rd->poll_one = hns_nic_rx_poll_one;
  1822. rd->ex_process = hns_nic_rx_up_pro;
  1823. rd->fini_process = is_ver1 ? hns_nic_rx_fini_pro :
  1824. hns_nic_rx_fini_pro_v2;
  1825. netif_napi_add(priv->netdev, &rd->napi,
  1826. hns_nic_common_poll, NIC_RX_CLEAN_MAX_NUM);
  1827. rd->ring->irq_init_flag = RCB_IRQ_NOT_INITED;
  1828. }
  1829. return 0;
  1830. }
  1831. static void hns_nic_uninit_ring_data(struct hns_nic_priv *priv)
  1832. {
  1833. struct hnae_handle *h = priv->ae_handle;
  1834. int i;
  1835. for (i = 0; i < h->q_num * 2; i++) {
  1836. netif_napi_del(&priv->ring_data[i].napi);
  1837. if (priv->ring_data[i].ring->irq_init_flag == RCB_IRQ_INITED) {
  1838. (void)irq_set_affinity_hint(
  1839. priv->ring_data[i].ring->irq,
  1840. NULL);
  1841. free_irq(priv->ring_data[i].ring->irq,
  1842. &priv->ring_data[i]);
  1843. }
  1844. priv->ring_data[i].ring->irq_init_flag = RCB_IRQ_NOT_INITED;
  1845. }
  1846. kfree(priv->ring_data);
  1847. }
  1848. static void hns_nic_set_priv_ops(struct net_device *netdev)
  1849. {
  1850. struct hns_nic_priv *priv = netdev_priv(netdev);
  1851. struct hnae_handle *h = priv->ae_handle;
  1852. if (AE_IS_VER1(priv->enet_ver)) {
  1853. priv->ops.fill_desc = fill_desc;
  1854. priv->ops.get_rxd_bnum = get_rx_desc_bnum;
  1855. priv->ops.maybe_stop_tx = hns_nic_maybe_stop_tx;
  1856. } else {
  1857. priv->ops.get_rxd_bnum = get_v2rx_desc_bnum;
  1858. if ((netdev->features & NETIF_F_TSO) ||
  1859. (netdev->features & NETIF_F_TSO6)) {
  1860. priv->ops.fill_desc = fill_tso_desc;
  1861. priv->ops.maybe_stop_tx = hns_nic_maybe_stop_tso;
  1862. /* This chip only support 7*4096 */
  1863. netif_set_gso_max_size(netdev, 7 * 4096);
  1864. } else {
  1865. priv->ops.fill_desc = fill_v2_desc;
  1866. priv->ops.maybe_stop_tx = hns_nic_maybe_stop_tx;
  1867. }
  1868. /* enable tso when init
  1869. * control tso on/off through TSE bit in bd
  1870. */
  1871. h->dev->ops->set_tso_stats(h, 1);
  1872. }
  1873. }
  1874. static int hns_nic_try_get_ae(struct net_device *ndev)
  1875. {
  1876. struct hns_nic_priv *priv = netdev_priv(ndev);
  1877. struct hnae_handle *h;
  1878. int ret;
  1879. h = hnae_get_handle(&priv->netdev->dev,
  1880. priv->fwnode, priv->port_id, NULL);
  1881. if (IS_ERR_OR_NULL(h)) {
  1882. ret = -ENODEV;
  1883. dev_dbg(priv->dev, "has not handle, register notifier!\n");
  1884. goto out;
  1885. }
  1886. priv->ae_handle = h;
  1887. ret = hns_nic_init_phy(ndev, h);
  1888. if (ret) {
  1889. dev_err(priv->dev, "probe phy device fail!\n");
  1890. goto out_init_phy;
  1891. }
  1892. ret = hns_nic_init_ring_data(priv);
  1893. if (ret) {
  1894. ret = -ENOMEM;
  1895. goto out_init_ring_data;
  1896. }
  1897. hns_nic_set_priv_ops(ndev);
  1898. ret = register_netdev(ndev);
  1899. if (ret) {
  1900. dev_err(priv->dev, "probe register netdev fail!\n");
  1901. goto out_reg_ndev_fail;
  1902. }
  1903. return 0;
  1904. out_reg_ndev_fail:
  1905. hns_nic_uninit_ring_data(priv);
  1906. priv->ring_data = NULL;
  1907. out_init_phy:
  1908. out_init_ring_data:
  1909. hnae_put_handle(priv->ae_handle);
  1910. priv->ae_handle = NULL;
  1911. out:
  1912. return ret;
  1913. }
  1914. static int hns_nic_notifier_action(struct notifier_block *nb,
  1915. unsigned long action, void *data)
  1916. {
  1917. struct hns_nic_priv *priv =
  1918. container_of(nb, struct hns_nic_priv, notifier_block);
  1919. assert(action == HNAE_AE_REGISTER);
  1920. if (!hns_nic_try_get_ae(priv->netdev)) {
  1921. hnae_unregister_notifier(&priv->notifier_block);
  1922. priv->notifier_block.notifier_call = NULL;
  1923. }
  1924. return 0;
  1925. }
  1926. static int hns_nic_dev_probe(struct platform_device *pdev)
  1927. {
  1928. struct device *dev = &pdev->dev;
  1929. struct net_device *ndev;
  1930. struct hns_nic_priv *priv;
  1931. u32 port_id;
  1932. int ret;
  1933. ndev = alloc_etherdev_mq(sizeof(struct hns_nic_priv), NIC_MAX_Q_PER_VF);
  1934. if (!ndev)
  1935. return -ENOMEM;
  1936. platform_set_drvdata(pdev, ndev);
  1937. priv = netdev_priv(ndev);
  1938. priv->dev = dev;
  1939. priv->netdev = ndev;
  1940. if (dev_of_node(dev)) {
  1941. struct device_node *ae_node;
  1942. if (of_device_is_compatible(dev->of_node,
  1943. "hisilicon,hns-nic-v1"))
  1944. priv->enet_ver = AE_VERSION_1;
  1945. else
  1946. priv->enet_ver = AE_VERSION_2;
  1947. ae_node = of_parse_phandle(dev->of_node, "ae-handle", 0);
  1948. if (!ae_node) {
  1949. ret = -ENODEV;
  1950. dev_err(dev, "not find ae-handle\n");
  1951. goto out_read_prop_fail;
  1952. }
  1953. priv->fwnode = &ae_node->fwnode;
  1954. } else if (is_acpi_node(dev->fwnode)) {
  1955. struct acpi_reference_args args;
  1956. if (acpi_dev_found(hns_enet_acpi_match[0].id))
  1957. priv->enet_ver = AE_VERSION_1;
  1958. else if (acpi_dev_found(hns_enet_acpi_match[1].id))
  1959. priv->enet_ver = AE_VERSION_2;
  1960. else
  1961. return -ENXIO;
  1962. /* try to find port-idx-in-ae first */
  1963. ret = acpi_node_get_property_reference(dev->fwnode,
  1964. "ae-handle", 0, &args);
  1965. if (ret) {
  1966. dev_err(dev, "not find ae-handle\n");
  1967. goto out_read_prop_fail;
  1968. }
  1969. priv->fwnode = acpi_fwnode_handle(args.adev);
  1970. } else {
  1971. dev_err(dev, "cannot read cfg data from OF or acpi\n");
  1972. return -ENXIO;
  1973. }
  1974. ret = device_property_read_u32(dev, "port-idx-in-ae", &port_id);
  1975. if (ret) {
  1976. /* only for old code compatible */
  1977. ret = device_property_read_u32(dev, "port-id", &port_id);
  1978. if (ret)
  1979. goto out_read_prop_fail;
  1980. /* for old dts, we need to caculate the port offset */
  1981. port_id = port_id < HNS_SRV_OFFSET ? port_id + HNS_DEBUG_OFFSET
  1982. : port_id - HNS_SRV_OFFSET;
  1983. }
  1984. priv->port_id = port_id;
  1985. hns_init_mac_addr(ndev);
  1986. ndev->watchdog_timeo = HNS_NIC_TX_TIMEOUT;
  1987. ndev->priv_flags |= IFF_UNICAST_FLT;
  1988. ndev->netdev_ops = &hns_nic_netdev_ops;
  1989. hns_ethtool_set_ops(ndev);
  1990. ndev->features |= NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
  1991. NETIF_F_RXCSUM | NETIF_F_SG | NETIF_F_GSO |
  1992. NETIF_F_GRO;
  1993. ndev->vlan_features |=
  1994. NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_RXCSUM;
  1995. ndev->vlan_features |= NETIF_F_SG | NETIF_F_GSO | NETIF_F_GRO;
  1996. /* MTU range: 68 - 9578 (v1) or 9706 (v2) */
  1997. ndev->min_mtu = MAC_MIN_MTU;
  1998. switch (priv->enet_ver) {
  1999. case AE_VERSION_2:
  2000. ndev->features |= NETIF_F_TSO | NETIF_F_TSO6;
  2001. ndev->hw_features |= NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
  2002. NETIF_F_RXCSUM | NETIF_F_SG | NETIF_F_GSO |
  2003. NETIF_F_GRO | NETIF_F_TSO | NETIF_F_TSO6;
  2004. ndev->max_mtu = MAC_MAX_MTU_V2 -
  2005. (ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN);
  2006. break;
  2007. default:
  2008. ndev->max_mtu = MAC_MAX_MTU -
  2009. (ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN);
  2010. break;
  2011. }
  2012. SET_NETDEV_DEV(ndev, dev);
  2013. if (!dma_set_mask_and_coherent(dev, DMA_BIT_MASK(64)))
  2014. dev_dbg(dev, "set mask to 64bit\n");
  2015. else
  2016. dev_err(dev, "set mask to 64bit fail!\n");
  2017. /* carrier off reporting is important to ethtool even BEFORE open */
  2018. netif_carrier_off(ndev);
  2019. timer_setup(&priv->service_timer, hns_nic_service_timer, 0);
  2020. INIT_WORK(&priv->service_task, hns_nic_service_task);
  2021. set_bit(NIC_STATE_SERVICE_INITED, &priv->state);
  2022. clear_bit(NIC_STATE_SERVICE_SCHED, &priv->state);
  2023. set_bit(NIC_STATE_DOWN, &priv->state);
  2024. if (hns_nic_try_get_ae(priv->netdev)) {
  2025. priv->notifier_block.notifier_call = hns_nic_notifier_action;
  2026. ret = hnae_register_notifier(&priv->notifier_block);
  2027. if (ret) {
  2028. dev_err(dev, "register notifier fail!\n");
  2029. goto out_notify_fail;
  2030. }
  2031. dev_dbg(dev, "has not handle, register notifier!\n");
  2032. }
  2033. return 0;
  2034. out_notify_fail:
  2035. (void)cancel_work_sync(&priv->service_task);
  2036. out_read_prop_fail:
  2037. free_netdev(ndev);
  2038. return ret;
  2039. }
  2040. static int hns_nic_dev_remove(struct platform_device *pdev)
  2041. {
  2042. struct net_device *ndev = platform_get_drvdata(pdev);
  2043. struct hns_nic_priv *priv = netdev_priv(ndev);
  2044. if (ndev->reg_state != NETREG_UNINITIALIZED)
  2045. unregister_netdev(ndev);
  2046. if (priv->ring_data)
  2047. hns_nic_uninit_ring_data(priv);
  2048. priv->ring_data = NULL;
  2049. if (ndev->phydev)
  2050. phy_disconnect(ndev->phydev);
  2051. if (!IS_ERR_OR_NULL(priv->ae_handle))
  2052. hnae_put_handle(priv->ae_handle);
  2053. priv->ae_handle = NULL;
  2054. if (priv->notifier_block.notifier_call)
  2055. hnae_unregister_notifier(&priv->notifier_block);
  2056. priv->notifier_block.notifier_call = NULL;
  2057. set_bit(NIC_STATE_REMOVING, &priv->state);
  2058. (void)cancel_work_sync(&priv->service_task);
  2059. free_netdev(ndev);
  2060. return 0;
  2061. }
  2062. static const struct of_device_id hns_enet_of_match[] = {
  2063. {.compatible = "hisilicon,hns-nic-v1",},
  2064. {.compatible = "hisilicon,hns-nic-v2",},
  2065. {},
  2066. };
  2067. MODULE_DEVICE_TABLE(of, hns_enet_of_match);
  2068. static struct platform_driver hns_nic_dev_driver = {
  2069. .driver = {
  2070. .name = "hns-nic",
  2071. .of_match_table = hns_enet_of_match,
  2072. .acpi_match_table = ACPI_PTR(hns_enet_acpi_match),
  2073. },
  2074. .probe = hns_nic_dev_probe,
  2075. .remove = hns_nic_dev_remove,
  2076. };
  2077. module_platform_driver(hns_nic_dev_driver);
  2078. MODULE_DESCRIPTION("HISILICON HNS Ethernet driver");
  2079. MODULE_AUTHOR("Hisilicon, Inc.");
  2080. MODULE_LICENSE("GPL");
  2081. MODULE_ALIAS("platform:hns-nic");