hns_enet.c 62 KB

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