hns_enet.c 56 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190
  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. int 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 device *dev = priv->dev;
  256. struct hnae_ring *ring = ring_data->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. wmb(); /* commit all data before submit */
  306. assert(skb->queue_mapping < priv->ae_handle->q_num);
  307. hnae_queue_xmit(priv->ae_handle->qs[skb->queue_mapping], buf_num);
  308. ring->stats.tx_pkts++;
  309. ring->stats.tx_bytes += skb->len;
  310. return NETDEV_TX_OK;
  311. out_map_frag_fail:
  312. while (ring->next_to_use != next_to_use) {
  313. unfill_desc(ring);
  314. if (ring->next_to_use != next_to_use)
  315. dma_unmap_page(dev,
  316. ring->desc_cb[ring->next_to_use].dma,
  317. ring->desc_cb[ring->next_to_use].length,
  318. DMA_TO_DEVICE);
  319. else
  320. dma_unmap_single(dev,
  321. ring->desc_cb[next_to_use].dma,
  322. ring->desc_cb[next_to_use].length,
  323. DMA_TO_DEVICE);
  324. }
  325. out_err_tx_ok:
  326. dev_kfree_skb_any(skb);
  327. return NETDEV_TX_OK;
  328. out_net_tx_busy:
  329. netif_stop_subqueue(ndev, skb->queue_mapping);
  330. /* Herbert's original patch had:
  331. * smp_mb__after_netif_stop_queue();
  332. * but since that doesn't exist yet, just open code it.
  333. */
  334. smp_mb();
  335. return NETDEV_TX_BUSY;
  336. }
  337. /**
  338. * hns_nic_get_headlen - determine size of header for RSC/LRO/GRO/FCOE
  339. * @data: pointer to the start of the headers
  340. * @max: total length of section to find headers in
  341. *
  342. * This function is meant to determine the length of headers that will
  343. * be recognized by hardware for LRO, GRO, and RSC offloads. The main
  344. * motivation of doing this is to only perform one pull for IPv4 TCP
  345. * packets so that we can do basic things like calculating the gso_size
  346. * based on the average data per packet.
  347. **/
  348. static unsigned int hns_nic_get_headlen(unsigned char *data, u32 flag,
  349. unsigned int max_size)
  350. {
  351. unsigned char *network;
  352. u8 hlen;
  353. /* this should never happen, but better safe than sorry */
  354. if (max_size < ETH_HLEN)
  355. return max_size;
  356. /* initialize network frame pointer */
  357. network = data;
  358. /* set first protocol and move network header forward */
  359. network += ETH_HLEN;
  360. /* handle any vlan tag if present */
  361. if (hnae_get_field(flag, HNS_RXD_VLAN_M, HNS_RXD_VLAN_S)
  362. == HNS_RX_FLAG_VLAN_PRESENT) {
  363. if ((typeof(max_size))(network - data) > (max_size - VLAN_HLEN))
  364. return max_size;
  365. network += VLAN_HLEN;
  366. }
  367. /* handle L3 protocols */
  368. if (hnae_get_field(flag, HNS_RXD_L3ID_M, HNS_RXD_L3ID_S)
  369. == HNS_RX_FLAG_L3ID_IPV4) {
  370. if ((typeof(max_size))(network - data) >
  371. (max_size - sizeof(struct iphdr)))
  372. return max_size;
  373. /* access ihl as a u8 to avoid unaligned access on ia64 */
  374. hlen = (network[0] & 0x0F) << 2;
  375. /* verify hlen meets minimum size requirements */
  376. if (hlen < sizeof(struct iphdr))
  377. return network - data;
  378. /* record next protocol if header is present */
  379. } else if (hnae_get_field(flag, HNS_RXD_L3ID_M, HNS_RXD_L3ID_S)
  380. == HNS_RX_FLAG_L3ID_IPV6) {
  381. if ((typeof(max_size))(network - data) >
  382. (max_size - sizeof(struct ipv6hdr)))
  383. return max_size;
  384. /* record next protocol */
  385. hlen = sizeof(struct ipv6hdr);
  386. } else {
  387. return network - data;
  388. }
  389. /* relocate pointer to start of L4 header */
  390. network += hlen;
  391. /* finally sort out TCP/UDP */
  392. if (hnae_get_field(flag, HNS_RXD_L4ID_M, HNS_RXD_L4ID_S)
  393. == HNS_RX_FLAG_L4ID_TCP) {
  394. if ((typeof(max_size))(network - data) >
  395. (max_size - sizeof(struct tcphdr)))
  396. return max_size;
  397. /* access doff as a u8 to avoid unaligned access on ia64 */
  398. hlen = (network[12] & 0xF0) >> 2;
  399. /* verify hlen meets minimum size requirements */
  400. if (hlen < sizeof(struct tcphdr))
  401. return network - data;
  402. network += hlen;
  403. } else if (hnae_get_field(flag, HNS_RXD_L4ID_M, HNS_RXD_L4ID_S)
  404. == HNS_RX_FLAG_L4ID_UDP) {
  405. if ((typeof(max_size))(network - data) >
  406. (max_size - sizeof(struct udphdr)))
  407. return max_size;
  408. network += sizeof(struct udphdr);
  409. }
  410. /* If everything has gone correctly network should be the
  411. * data section of the packet and will be the end of the header.
  412. * If not then it probably represents the end of the last recognized
  413. * header.
  414. */
  415. if ((typeof(max_size))(network - data) < max_size)
  416. return network - data;
  417. else
  418. return max_size;
  419. }
  420. static void hns_nic_reuse_page(struct sk_buff *skb, int i,
  421. struct hnae_ring *ring, int pull_len,
  422. struct hnae_desc_cb *desc_cb)
  423. {
  424. struct hnae_desc *desc;
  425. int truesize, size;
  426. int last_offset;
  427. bool twobufs;
  428. twobufs = ((PAGE_SIZE < 8192) && hnae_buf_size(ring) == HNS_BUFFER_SIZE_2048);
  429. desc = &ring->desc[ring->next_to_clean];
  430. size = le16_to_cpu(desc->rx.size);
  431. if (twobufs) {
  432. truesize = hnae_buf_size(ring);
  433. } else {
  434. truesize = ALIGN(size, L1_CACHE_BYTES);
  435. last_offset = hnae_page_size(ring) - hnae_buf_size(ring);
  436. }
  437. skb_add_rx_frag(skb, i, desc_cb->priv, desc_cb->page_offset + pull_len,
  438. size - pull_len, truesize - pull_len);
  439. /* avoid re-using remote pages,flag default unreuse */
  440. if (unlikely(page_to_nid(desc_cb->priv) != numa_node_id()))
  441. return;
  442. if (twobufs) {
  443. /* if we are only owner of page we can reuse it */
  444. if (likely(page_count(desc_cb->priv) == 1)) {
  445. /* flip page offset to other buffer */
  446. desc_cb->page_offset ^= truesize;
  447. desc_cb->reuse_flag = 1;
  448. /* bump ref count on page before it is given*/
  449. get_page(desc_cb->priv);
  450. }
  451. return;
  452. }
  453. /* move offset up to the next cache line */
  454. desc_cb->page_offset += truesize;
  455. if (desc_cb->page_offset <= last_offset) {
  456. desc_cb->reuse_flag = 1;
  457. /* bump ref count on page before it is given*/
  458. get_page(desc_cb->priv);
  459. }
  460. }
  461. static void get_v2rx_desc_bnum(u32 bnum_flag, int *out_bnum)
  462. {
  463. *out_bnum = hnae_get_field(bnum_flag,
  464. HNS_RXD_BUFNUM_M, HNS_RXD_BUFNUM_S) + 1;
  465. }
  466. static void get_rx_desc_bnum(u32 bnum_flag, int *out_bnum)
  467. {
  468. *out_bnum = hnae_get_field(bnum_flag,
  469. HNS_RXD_BUFNUM_M, HNS_RXD_BUFNUM_S);
  470. }
  471. static void hns_nic_rx_checksum(struct hns_nic_ring_data *ring_data,
  472. struct sk_buff *skb, u32 flag)
  473. {
  474. struct net_device *netdev = ring_data->napi.dev;
  475. u32 l3id;
  476. u32 l4id;
  477. /* check if RX checksum offload is enabled */
  478. if (unlikely(!(netdev->features & NETIF_F_RXCSUM)))
  479. return;
  480. /* In hardware, we only support checksum for the following protocols:
  481. * 1) IPv4,
  482. * 2) TCP(over IPv4 or IPv6),
  483. * 3) UDP(over IPv4 or IPv6),
  484. * 4) SCTP(over IPv4 or IPv6)
  485. * but we support many L3(IPv4, IPv6, MPLS, PPPoE etc) and L4(TCP,
  486. * UDP, GRE, SCTP, IGMP, ICMP etc.) protocols.
  487. *
  488. * Hardware limitation:
  489. * Our present hardware RX Descriptor lacks L3/L4 checksum "Status &
  490. * Error" bit (which usually can be used to indicate whether checksum
  491. * was calculated by the hardware and if there was any error encountered
  492. * during checksum calculation).
  493. *
  494. * Software workaround:
  495. * We do get info within the RX descriptor about the kind of L3/L4
  496. * protocol coming in the packet and the error status. These errors
  497. * might not just be checksum errors but could be related to version,
  498. * length of IPv4, UDP, TCP etc.
  499. * Because there is no-way of knowing if it is a L3/L4 error due to bad
  500. * checksum or any other L3/L4 error, we will not (cannot) convey
  501. * checksum status for such cases to upper stack and will not maintain
  502. * the RX L3/L4 checksum counters as well.
  503. */
  504. l3id = hnae_get_field(flag, HNS_RXD_L3ID_M, HNS_RXD_L3ID_S);
  505. l4id = hnae_get_field(flag, HNS_RXD_L4ID_M, HNS_RXD_L4ID_S);
  506. /* check L3 protocol for which checksum is supported */
  507. if ((l3id != HNS_RX_FLAG_L3ID_IPV4) && (l3id != HNS_RX_FLAG_L3ID_IPV6))
  508. return;
  509. /* check for any(not just checksum)flagged L3 protocol errors */
  510. if (unlikely(hnae_get_bit(flag, HNS_RXD_L3E_B)))
  511. return;
  512. /* we do not support checksum of fragmented packets */
  513. if (unlikely(hnae_get_bit(flag, HNS_RXD_FRAG_B)))
  514. return;
  515. /* check L4 protocol for which checksum is supported */
  516. if ((l4id != HNS_RX_FLAG_L4ID_TCP) &&
  517. (l4id != HNS_RX_FLAG_L4ID_UDP) &&
  518. (l4id != HNS_RX_FLAG_L4ID_SCTP))
  519. return;
  520. /* check for any(not just checksum)flagged L4 protocol errors */
  521. if (unlikely(hnae_get_bit(flag, HNS_RXD_L4E_B)))
  522. return;
  523. /* now, this has to be a packet with valid RX checksum */
  524. skb->ip_summed = CHECKSUM_UNNECESSARY;
  525. }
  526. static int hns_nic_poll_rx_skb(struct hns_nic_ring_data *ring_data,
  527. struct sk_buff **out_skb, int *out_bnum)
  528. {
  529. struct hnae_ring *ring = ring_data->ring;
  530. struct net_device *ndev = ring_data->napi.dev;
  531. struct hns_nic_priv *priv = netdev_priv(ndev);
  532. struct sk_buff *skb;
  533. struct hnae_desc *desc;
  534. struct hnae_desc_cb *desc_cb;
  535. unsigned char *va;
  536. int bnum, length, i;
  537. int pull_len;
  538. u32 bnum_flag;
  539. desc = &ring->desc[ring->next_to_clean];
  540. desc_cb = &ring->desc_cb[ring->next_to_clean];
  541. prefetch(desc);
  542. va = (unsigned char *)desc_cb->buf + desc_cb->page_offset;
  543. /* prefetch first cache line of first page */
  544. prefetch(va);
  545. #if L1_CACHE_BYTES < 128
  546. prefetch(va + L1_CACHE_BYTES);
  547. #endif
  548. skb = *out_skb = napi_alloc_skb(&ring_data->napi,
  549. HNS_RX_HEAD_SIZE);
  550. if (unlikely(!skb)) {
  551. netdev_err(ndev, "alloc rx skb fail\n");
  552. ring->stats.sw_err_cnt++;
  553. return -ENOMEM;
  554. }
  555. prefetchw(skb->data);
  556. length = le16_to_cpu(desc->rx.pkt_len);
  557. bnum_flag = le32_to_cpu(desc->rx.ipoff_bnum_pid_flag);
  558. priv->ops.get_rxd_bnum(bnum_flag, &bnum);
  559. *out_bnum = bnum;
  560. if (length <= HNS_RX_HEAD_SIZE) {
  561. memcpy(__skb_put(skb, length), va, ALIGN(length, sizeof(long)));
  562. /* we can reuse buffer as-is, just make sure it is local */
  563. if (likely(page_to_nid(desc_cb->priv) == numa_node_id()))
  564. desc_cb->reuse_flag = 1;
  565. else /* this page cannot be reused so discard it */
  566. put_page(desc_cb->priv);
  567. ring_ptr_move_fw(ring, next_to_clean);
  568. if (unlikely(bnum != 1)) { /* check err*/
  569. *out_bnum = 1;
  570. goto out_bnum_err;
  571. }
  572. } else {
  573. ring->stats.seg_pkt_cnt++;
  574. pull_len = hns_nic_get_headlen(va, bnum_flag, HNS_RX_HEAD_SIZE);
  575. memcpy(__skb_put(skb, pull_len), va,
  576. ALIGN(pull_len, sizeof(long)));
  577. hns_nic_reuse_page(skb, 0, ring, pull_len, desc_cb);
  578. ring_ptr_move_fw(ring, next_to_clean);
  579. if (unlikely(bnum >= (int)MAX_SKB_FRAGS)) { /* check err*/
  580. *out_bnum = 1;
  581. goto out_bnum_err;
  582. }
  583. for (i = 1; i < bnum; i++) {
  584. desc = &ring->desc[ring->next_to_clean];
  585. desc_cb = &ring->desc_cb[ring->next_to_clean];
  586. hns_nic_reuse_page(skb, i, ring, 0, desc_cb);
  587. ring_ptr_move_fw(ring, next_to_clean);
  588. }
  589. }
  590. /* check except process, free skb and jump the desc */
  591. if (unlikely((!bnum) || (bnum > ring->max_desc_num_per_pkt))) {
  592. out_bnum_err:
  593. *out_bnum = *out_bnum ? *out_bnum : 1; /* ntc moved,cannot 0*/
  594. netdev_err(ndev, "invalid bnum(%d,%d,%d,%d),%016llx,%016llx\n",
  595. bnum, ring->max_desc_num_per_pkt,
  596. length, (int)MAX_SKB_FRAGS,
  597. ((u64 *)desc)[0], ((u64 *)desc)[1]);
  598. ring->stats.err_bd_num++;
  599. dev_kfree_skb_any(skb);
  600. return -EDOM;
  601. }
  602. bnum_flag = le32_to_cpu(desc->rx.ipoff_bnum_pid_flag);
  603. if (unlikely(!hnae_get_bit(bnum_flag, HNS_RXD_VLD_B))) {
  604. netdev_err(ndev, "no valid bd,%016llx,%016llx\n",
  605. ((u64 *)desc)[0], ((u64 *)desc)[1]);
  606. ring->stats.non_vld_descs++;
  607. dev_kfree_skb_any(skb);
  608. return -EINVAL;
  609. }
  610. if (unlikely((!desc->rx.pkt_len) ||
  611. hnae_get_bit(bnum_flag, HNS_RXD_DROP_B))) {
  612. ring->stats.err_pkt_len++;
  613. dev_kfree_skb_any(skb);
  614. return -EFAULT;
  615. }
  616. if (unlikely(hnae_get_bit(bnum_flag, HNS_RXD_L2E_B))) {
  617. ring->stats.l2_err++;
  618. dev_kfree_skb_any(skb);
  619. return -EFAULT;
  620. }
  621. ring->stats.rx_pkts++;
  622. ring->stats.rx_bytes += skb->len;
  623. /* indicate to upper stack if our hardware has already calculated
  624. * the RX checksum
  625. */
  626. hns_nic_rx_checksum(ring_data, skb, bnum_flag);
  627. return 0;
  628. }
  629. static void
  630. hns_nic_alloc_rx_buffers(struct hns_nic_ring_data *ring_data, int cleand_count)
  631. {
  632. int i, ret;
  633. struct hnae_desc_cb res_cbs;
  634. struct hnae_desc_cb *desc_cb;
  635. struct hnae_ring *ring = ring_data->ring;
  636. struct net_device *ndev = ring_data->napi.dev;
  637. for (i = 0; i < cleand_count; i++) {
  638. desc_cb = &ring->desc_cb[ring->next_to_use];
  639. if (desc_cb->reuse_flag) {
  640. ring->stats.reuse_pg_cnt++;
  641. hnae_reuse_buffer(ring, ring->next_to_use);
  642. } else {
  643. ret = hnae_reserve_buffer_map(ring, &res_cbs);
  644. if (ret) {
  645. ring->stats.sw_err_cnt++;
  646. netdev_err(ndev, "hnae reserve buffer map failed.\n");
  647. break;
  648. }
  649. hnae_replace_buffer(ring, ring->next_to_use, &res_cbs);
  650. }
  651. ring_ptr_move_fw(ring, next_to_use);
  652. }
  653. wmb(); /* make all data has been write before submit */
  654. writel_relaxed(i, ring->io_base + RCB_REG_HEAD);
  655. }
  656. /* return error number for error or number of desc left to take
  657. */
  658. static void hns_nic_rx_up_pro(struct hns_nic_ring_data *ring_data,
  659. struct sk_buff *skb)
  660. {
  661. struct net_device *ndev = ring_data->napi.dev;
  662. skb->protocol = eth_type_trans(skb, ndev);
  663. (void)napi_gro_receive(&ring_data->napi, skb);
  664. ndev->last_rx = jiffies;
  665. }
  666. static int hns_desc_unused(struct hnae_ring *ring)
  667. {
  668. int ntc = ring->next_to_clean;
  669. int ntu = ring->next_to_use;
  670. return ((ntc >= ntu) ? 0 : ring->desc_num) + ntc - ntu;
  671. }
  672. static int hns_nic_rx_poll_one(struct hns_nic_ring_data *ring_data,
  673. int budget, void *v)
  674. {
  675. struct hnae_ring *ring = ring_data->ring;
  676. struct sk_buff *skb;
  677. int num, bnum;
  678. #define RCB_NOF_ALLOC_RX_BUFF_ONCE 16
  679. int recv_pkts, recv_bds, clean_count, err;
  680. int unused_count = hns_desc_unused(ring);
  681. num = readl_relaxed(ring->io_base + RCB_REG_FBDNUM);
  682. rmb(); /* make sure num taken effect before the other data is touched */
  683. recv_pkts = 0, recv_bds = 0, clean_count = 0;
  684. num -= unused_count;
  685. while (recv_pkts < budget && recv_bds < num) {
  686. /* reuse or realloc buffers */
  687. if (clean_count + unused_count >= RCB_NOF_ALLOC_RX_BUFF_ONCE) {
  688. hns_nic_alloc_rx_buffers(ring_data,
  689. clean_count + unused_count);
  690. clean_count = 0;
  691. unused_count = hns_desc_unused(ring);
  692. }
  693. /* poll one pkt */
  694. err = hns_nic_poll_rx_skb(ring_data, &skb, &bnum);
  695. if (unlikely(!skb)) /* this fault cannot be repaired */
  696. goto out;
  697. recv_bds += bnum;
  698. clean_count += bnum;
  699. if (unlikely(err)) { /* do jump the err */
  700. recv_pkts++;
  701. continue;
  702. }
  703. /* do update ip stack process*/
  704. ((void (*)(struct hns_nic_ring_data *, struct sk_buff *))v)(
  705. ring_data, skb);
  706. recv_pkts++;
  707. }
  708. out:
  709. /* make all data has been write before submit */
  710. if (clean_count + unused_count > 0)
  711. hns_nic_alloc_rx_buffers(ring_data,
  712. clean_count + unused_count);
  713. return recv_pkts;
  714. }
  715. static void hns_nic_rx_fini_pro(struct hns_nic_ring_data *ring_data)
  716. {
  717. struct hnae_ring *ring = ring_data->ring;
  718. int num = 0;
  719. ring_data->ring->q->handle->dev->ops->toggle_ring_irq(ring, 0);
  720. /* for hardware bug fixed */
  721. num = readl_relaxed(ring->io_base + RCB_REG_FBDNUM);
  722. if (num > 0) {
  723. ring_data->ring->q->handle->dev->ops->toggle_ring_irq(
  724. ring_data->ring, 1);
  725. napi_schedule(&ring_data->napi);
  726. }
  727. }
  728. static void hns_nic_rx_fini_pro_v2(struct hns_nic_ring_data *ring_data)
  729. {
  730. struct hnae_ring *ring = ring_data->ring;
  731. int num = 0;
  732. num = readl_relaxed(ring->io_base + RCB_REG_FBDNUM);
  733. if (num == 0)
  734. ring_data->ring->q->handle->dev->ops->toggle_ring_irq(
  735. ring, 0);
  736. else
  737. napi_schedule(&ring_data->napi);
  738. }
  739. static inline void hns_nic_reclaim_one_desc(struct hnae_ring *ring,
  740. int *bytes, int *pkts)
  741. {
  742. struct hnae_desc_cb *desc_cb = &ring->desc_cb[ring->next_to_clean];
  743. (*pkts) += (desc_cb->type == DESC_TYPE_SKB);
  744. (*bytes) += desc_cb->length;
  745. /* desc_cb will be cleaned, after hnae_free_buffer_detach*/
  746. hnae_free_buffer_detach(ring, ring->next_to_clean);
  747. ring_ptr_move_fw(ring, next_to_clean);
  748. }
  749. static int is_valid_clean_head(struct hnae_ring *ring, int h)
  750. {
  751. int u = ring->next_to_use;
  752. int c = ring->next_to_clean;
  753. if (unlikely(h > ring->desc_num))
  754. return 0;
  755. assert(u > 0 && u < ring->desc_num);
  756. assert(c > 0 && c < ring->desc_num);
  757. assert(u != c && h != c); /* must be checked before call this func */
  758. return u > c ? (h > c && h <= u) : (h > c || h <= u);
  759. }
  760. /* netif_tx_lock will turn down the performance, set only when necessary */
  761. #ifdef CONFIG_NET_POLL_CONTROLLER
  762. #define NETIF_TX_LOCK(ndev) netif_tx_lock(ndev)
  763. #define NETIF_TX_UNLOCK(ndev) netif_tx_unlock(ndev)
  764. #else
  765. #define NETIF_TX_LOCK(ndev)
  766. #define NETIF_TX_UNLOCK(ndev)
  767. #endif
  768. /* reclaim all desc in one budget
  769. * return error or number of desc left
  770. */
  771. static int hns_nic_tx_poll_one(struct hns_nic_ring_data *ring_data,
  772. int budget, void *v)
  773. {
  774. struct hnae_ring *ring = ring_data->ring;
  775. struct net_device *ndev = ring_data->napi.dev;
  776. struct netdev_queue *dev_queue;
  777. struct hns_nic_priv *priv = netdev_priv(ndev);
  778. int head;
  779. int bytes, pkts;
  780. NETIF_TX_LOCK(ndev);
  781. head = readl_relaxed(ring->io_base + RCB_REG_HEAD);
  782. rmb(); /* make sure head is ready before touch any data */
  783. if (is_ring_empty(ring) || head == ring->next_to_clean) {
  784. NETIF_TX_UNLOCK(ndev);
  785. return 0; /* no data to poll */
  786. }
  787. if (!is_valid_clean_head(ring, head)) {
  788. netdev_err(ndev, "wrong head (%d, %d-%d)\n", head,
  789. ring->next_to_use, ring->next_to_clean);
  790. ring->stats.io_err_cnt++;
  791. NETIF_TX_UNLOCK(ndev);
  792. return -EIO;
  793. }
  794. bytes = 0;
  795. pkts = 0;
  796. while (head != ring->next_to_clean) {
  797. hns_nic_reclaim_one_desc(ring, &bytes, &pkts);
  798. /* issue prefetch for next Tx descriptor */
  799. prefetch(&ring->desc_cb[ring->next_to_clean]);
  800. }
  801. NETIF_TX_UNLOCK(ndev);
  802. dev_queue = netdev_get_tx_queue(ndev, ring_data->queue_index);
  803. netdev_tx_completed_queue(dev_queue, pkts, bytes);
  804. if (unlikely(priv->link && !netif_carrier_ok(ndev)))
  805. netif_carrier_on(ndev);
  806. if (unlikely(pkts && netif_carrier_ok(ndev) &&
  807. (ring_space(ring) >= ring->max_desc_num_per_pkt * 2))) {
  808. /* Make sure that anybody stopping the queue after this
  809. * sees the new next_to_clean.
  810. */
  811. smp_mb();
  812. if (netif_tx_queue_stopped(dev_queue) &&
  813. !test_bit(NIC_STATE_DOWN, &priv->state)) {
  814. netif_tx_wake_queue(dev_queue);
  815. ring->stats.restart_queue++;
  816. }
  817. }
  818. return 0;
  819. }
  820. static void hns_nic_tx_fini_pro(struct hns_nic_ring_data *ring_data)
  821. {
  822. struct hnae_ring *ring = ring_data->ring;
  823. int head;
  824. ring_data->ring->q->handle->dev->ops->toggle_ring_irq(ring, 0);
  825. head = readl_relaxed(ring->io_base + RCB_REG_HEAD);
  826. if (head != ring->next_to_clean) {
  827. ring_data->ring->q->handle->dev->ops->toggle_ring_irq(
  828. ring_data->ring, 1);
  829. napi_schedule(&ring_data->napi);
  830. }
  831. }
  832. static void hns_nic_tx_fini_pro_v2(struct hns_nic_ring_data *ring_data)
  833. {
  834. struct hnae_ring *ring = ring_data->ring;
  835. int head = readl_relaxed(ring->io_base + RCB_REG_HEAD);
  836. if (head == ring->next_to_clean)
  837. ring_data->ring->q->handle->dev->ops->toggle_ring_irq(
  838. ring, 0);
  839. else
  840. napi_schedule(&ring_data->napi);
  841. }
  842. static void hns_nic_tx_clr_all_bufs(struct hns_nic_ring_data *ring_data)
  843. {
  844. struct hnae_ring *ring = ring_data->ring;
  845. struct net_device *ndev = ring_data->napi.dev;
  846. struct netdev_queue *dev_queue;
  847. int head;
  848. int bytes, pkts;
  849. NETIF_TX_LOCK(ndev);
  850. head = ring->next_to_use; /* ntu :soft setted ring position*/
  851. bytes = 0;
  852. pkts = 0;
  853. while (head != ring->next_to_clean)
  854. hns_nic_reclaim_one_desc(ring, &bytes, &pkts);
  855. NETIF_TX_UNLOCK(ndev);
  856. dev_queue = netdev_get_tx_queue(ndev, ring_data->queue_index);
  857. netdev_tx_reset_queue(dev_queue);
  858. }
  859. static int hns_nic_common_poll(struct napi_struct *napi, int budget)
  860. {
  861. struct hns_nic_ring_data *ring_data =
  862. container_of(napi, struct hns_nic_ring_data, napi);
  863. int clean_complete = ring_data->poll_one(
  864. ring_data, budget, ring_data->ex_process);
  865. if (clean_complete >= 0 && clean_complete < budget) {
  866. napi_complete(napi);
  867. ring_data->fini_process(ring_data);
  868. return 0;
  869. }
  870. return clean_complete;
  871. }
  872. static irqreturn_t hns_irq_handle(int irq, void *dev)
  873. {
  874. struct hns_nic_ring_data *ring_data = (struct hns_nic_ring_data *)dev;
  875. ring_data->ring->q->handle->dev->ops->toggle_ring_irq(
  876. ring_data->ring, 1);
  877. napi_schedule(&ring_data->napi);
  878. return IRQ_HANDLED;
  879. }
  880. /**
  881. *hns_nic_adjust_link - adjust net work mode by the phy stat or new param
  882. *@ndev: net device
  883. */
  884. static void hns_nic_adjust_link(struct net_device *ndev)
  885. {
  886. struct hns_nic_priv *priv = netdev_priv(ndev);
  887. struct hnae_handle *h = priv->ae_handle;
  888. int state = 1;
  889. if (ndev->phydev) {
  890. h->dev->ops->adjust_link(h, ndev->phydev->speed,
  891. ndev->phydev->duplex);
  892. state = ndev->phydev->link;
  893. }
  894. state = state && h->dev->ops->get_status(h);
  895. if (state != priv->link) {
  896. if (state) {
  897. netif_carrier_on(ndev);
  898. netif_tx_wake_all_queues(ndev);
  899. netdev_info(ndev, "link up\n");
  900. } else {
  901. netif_carrier_off(ndev);
  902. netdev_info(ndev, "link down\n");
  903. }
  904. priv->link = state;
  905. }
  906. }
  907. /**
  908. *hns_nic_init_phy - init phy
  909. *@ndev: net device
  910. *@h: ae handle
  911. * Return 0 on success, negative on failure
  912. */
  913. int hns_nic_init_phy(struct net_device *ndev, struct hnae_handle *h)
  914. {
  915. struct phy_device *phy_dev = h->phy_dev;
  916. int ret;
  917. if (!h->phy_dev)
  918. return 0;
  919. if (h->phy_if != PHY_INTERFACE_MODE_XGMII) {
  920. phy_dev->dev_flags = 0;
  921. ret = phy_connect_direct(ndev, phy_dev, hns_nic_adjust_link,
  922. h->phy_if);
  923. } else {
  924. ret = phy_attach_direct(ndev, phy_dev, 0, h->phy_if);
  925. }
  926. if (unlikely(ret))
  927. return -ENODEV;
  928. phy_dev->supported &= h->if_support;
  929. phy_dev->advertising = phy_dev->supported;
  930. if (h->phy_if == PHY_INTERFACE_MODE_XGMII)
  931. phy_dev->autoneg = false;
  932. return 0;
  933. }
  934. static int hns_nic_ring_open(struct net_device *netdev, int idx)
  935. {
  936. struct hns_nic_priv *priv = netdev_priv(netdev);
  937. struct hnae_handle *h = priv->ae_handle;
  938. napi_enable(&priv->ring_data[idx].napi);
  939. enable_irq(priv->ring_data[idx].ring->irq);
  940. h->dev->ops->toggle_ring_irq(priv->ring_data[idx].ring, 0);
  941. return 0;
  942. }
  943. static int hns_nic_net_set_mac_address(struct net_device *ndev, void *p)
  944. {
  945. struct hns_nic_priv *priv = netdev_priv(ndev);
  946. struct hnae_handle *h = priv->ae_handle;
  947. struct sockaddr *mac_addr = p;
  948. int ret;
  949. if (!mac_addr || !is_valid_ether_addr((const u8 *)mac_addr->sa_data))
  950. return -EADDRNOTAVAIL;
  951. ret = h->dev->ops->set_mac_addr(h, mac_addr->sa_data);
  952. if (ret) {
  953. netdev_err(ndev, "set_mac_address fail, ret=%d!\n", ret);
  954. return ret;
  955. }
  956. memcpy(ndev->dev_addr, mac_addr->sa_data, ndev->addr_len);
  957. return 0;
  958. }
  959. void hns_nic_update_stats(struct net_device *netdev)
  960. {
  961. struct hns_nic_priv *priv = netdev_priv(netdev);
  962. struct hnae_handle *h = priv->ae_handle;
  963. h->dev->ops->update_stats(h, &netdev->stats);
  964. }
  965. /* set mac addr if it is configed. or leave it to the AE driver */
  966. static void hns_init_mac_addr(struct net_device *ndev)
  967. {
  968. struct hns_nic_priv *priv = netdev_priv(ndev);
  969. if (!device_get_mac_address(priv->dev, ndev->dev_addr, ETH_ALEN)) {
  970. eth_hw_addr_random(ndev);
  971. dev_warn(priv->dev, "No valid mac, use random mac %pM",
  972. ndev->dev_addr);
  973. }
  974. }
  975. static void hns_nic_ring_close(struct net_device *netdev, int idx)
  976. {
  977. struct hns_nic_priv *priv = netdev_priv(netdev);
  978. struct hnae_handle *h = priv->ae_handle;
  979. h->dev->ops->toggle_ring_irq(priv->ring_data[idx].ring, 1);
  980. disable_irq(priv->ring_data[idx].ring->irq);
  981. napi_disable(&priv->ring_data[idx].napi);
  982. }
  983. static void hns_set_irq_affinity(struct hns_nic_priv *priv)
  984. {
  985. struct hnae_handle *h = priv->ae_handle;
  986. struct hns_nic_ring_data *rd;
  987. int i;
  988. int cpu;
  989. cpumask_t mask;
  990. /*diffrent irq banlance for 16core and 32core*/
  991. if (h->q_num == num_possible_cpus()) {
  992. for (i = 0; i < h->q_num * 2; i++) {
  993. rd = &priv->ring_data[i];
  994. if (cpu_online(rd->queue_index)) {
  995. cpumask_clear(&mask);
  996. cpu = rd->queue_index;
  997. cpumask_set_cpu(cpu, &mask);
  998. (void)irq_set_affinity_hint(rd->ring->irq,
  999. &mask);
  1000. }
  1001. }
  1002. } else {
  1003. for (i = 0; i < h->q_num; i++) {
  1004. rd = &priv->ring_data[i];
  1005. if (cpu_online(rd->queue_index * 2)) {
  1006. cpumask_clear(&mask);
  1007. cpu = rd->queue_index * 2;
  1008. cpumask_set_cpu(cpu, &mask);
  1009. (void)irq_set_affinity_hint(rd->ring->irq,
  1010. &mask);
  1011. }
  1012. }
  1013. for (i = h->q_num; i < h->q_num * 2; i++) {
  1014. rd = &priv->ring_data[i];
  1015. if (cpu_online(rd->queue_index * 2 + 1)) {
  1016. cpumask_clear(&mask);
  1017. cpu = rd->queue_index * 2 + 1;
  1018. cpumask_set_cpu(cpu, &mask);
  1019. (void)irq_set_affinity_hint(rd->ring->irq,
  1020. &mask);
  1021. }
  1022. }
  1023. }
  1024. }
  1025. static int hns_nic_init_irq(struct hns_nic_priv *priv)
  1026. {
  1027. struct hnae_handle *h = priv->ae_handle;
  1028. struct hns_nic_ring_data *rd;
  1029. int i;
  1030. int ret;
  1031. for (i = 0; i < h->q_num * 2; i++) {
  1032. rd = &priv->ring_data[i];
  1033. if (rd->ring->irq_init_flag == RCB_IRQ_INITED)
  1034. break;
  1035. snprintf(rd->ring->ring_name, RCB_RING_NAME_LEN,
  1036. "%s-%s%d", priv->netdev->name,
  1037. (i < h->q_num ? "tx" : "rx"), rd->queue_index);
  1038. rd->ring->ring_name[RCB_RING_NAME_LEN - 1] = '\0';
  1039. ret = request_irq(rd->ring->irq,
  1040. hns_irq_handle, 0, rd->ring->ring_name, rd);
  1041. if (ret) {
  1042. netdev_err(priv->netdev, "request irq(%d) fail\n",
  1043. rd->ring->irq);
  1044. return ret;
  1045. }
  1046. disable_irq(rd->ring->irq);
  1047. rd->ring->irq_init_flag = RCB_IRQ_INITED;
  1048. }
  1049. /*set cpu affinity*/
  1050. hns_set_irq_affinity(priv);
  1051. return 0;
  1052. }
  1053. static int hns_nic_net_up(struct net_device *ndev)
  1054. {
  1055. struct hns_nic_priv *priv = netdev_priv(ndev);
  1056. struct hnae_handle *h = priv->ae_handle;
  1057. int i, j;
  1058. int ret;
  1059. ret = hns_nic_init_irq(priv);
  1060. if (ret != 0) {
  1061. netdev_err(ndev, "hns init irq failed! ret=%d\n", ret);
  1062. return ret;
  1063. }
  1064. for (i = 0; i < h->q_num * 2; i++) {
  1065. ret = hns_nic_ring_open(ndev, i);
  1066. if (ret)
  1067. goto out_has_some_queues;
  1068. }
  1069. ret = h->dev->ops->set_mac_addr(h, ndev->dev_addr);
  1070. if (ret)
  1071. goto out_set_mac_addr_err;
  1072. ret = h->dev->ops->start ? h->dev->ops->start(h) : 0;
  1073. if (ret)
  1074. goto out_start_err;
  1075. if (ndev->phydev)
  1076. phy_start(ndev->phydev);
  1077. clear_bit(NIC_STATE_DOWN, &priv->state);
  1078. (void)mod_timer(&priv->service_timer, jiffies + SERVICE_TIMER_HZ);
  1079. return 0;
  1080. out_start_err:
  1081. netif_stop_queue(ndev);
  1082. out_set_mac_addr_err:
  1083. out_has_some_queues:
  1084. for (j = i - 1; j >= 0; j--)
  1085. hns_nic_ring_close(ndev, j);
  1086. set_bit(NIC_STATE_DOWN, &priv->state);
  1087. return ret;
  1088. }
  1089. static void hns_nic_net_down(struct net_device *ndev)
  1090. {
  1091. int i;
  1092. struct hnae_ae_ops *ops;
  1093. struct hns_nic_priv *priv = netdev_priv(ndev);
  1094. if (test_and_set_bit(NIC_STATE_DOWN, &priv->state))
  1095. return;
  1096. (void)del_timer_sync(&priv->service_timer);
  1097. netif_tx_stop_all_queues(ndev);
  1098. netif_carrier_off(ndev);
  1099. netif_tx_disable(ndev);
  1100. priv->link = 0;
  1101. if (ndev->phydev)
  1102. phy_stop(ndev->phydev);
  1103. ops = priv->ae_handle->dev->ops;
  1104. if (ops->stop)
  1105. ops->stop(priv->ae_handle);
  1106. netif_tx_stop_all_queues(ndev);
  1107. for (i = priv->ae_handle->q_num - 1; i >= 0; i--) {
  1108. hns_nic_ring_close(ndev, i);
  1109. hns_nic_ring_close(ndev, i + priv->ae_handle->q_num);
  1110. /* clean tx buffers*/
  1111. hns_nic_tx_clr_all_bufs(priv->ring_data + i);
  1112. }
  1113. }
  1114. void hns_nic_net_reset(struct net_device *ndev)
  1115. {
  1116. struct hns_nic_priv *priv = netdev_priv(ndev);
  1117. struct hnae_handle *handle = priv->ae_handle;
  1118. while (test_and_set_bit(NIC_STATE_RESETTING, &priv->state))
  1119. usleep_range(1000, 2000);
  1120. (void)hnae_reinit_handle(handle);
  1121. clear_bit(NIC_STATE_RESETTING, &priv->state);
  1122. }
  1123. void hns_nic_net_reinit(struct net_device *netdev)
  1124. {
  1125. struct hns_nic_priv *priv = netdev_priv(netdev);
  1126. netif_trans_update(priv->netdev);
  1127. while (test_and_set_bit(NIC_STATE_REINITING, &priv->state))
  1128. usleep_range(1000, 2000);
  1129. hns_nic_net_down(netdev);
  1130. hns_nic_net_reset(netdev);
  1131. (void)hns_nic_net_up(netdev);
  1132. clear_bit(NIC_STATE_REINITING, &priv->state);
  1133. }
  1134. static int hns_nic_net_open(struct net_device *ndev)
  1135. {
  1136. struct hns_nic_priv *priv = netdev_priv(ndev);
  1137. struct hnae_handle *h = priv->ae_handle;
  1138. int ret;
  1139. if (test_bit(NIC_STATE_TESTING, &priv->state))
  1140. return -EBUSY;
  1141. priv->link = 0;
  1142. netif_carrier_off(ndev);
  1143. ret = netif_set_real_num_tx_queues(ndev, h->q_num);
  1144. if (ret < 0) {
  1145. netdev_err(ndev, "netif_set_real_num_tx_queues fail, ret=%d!\n",
  1146. ret);
  1147. return ret;
  1148. }
  1149. ret = netif_set_real_num_rx_queues(ndev, h->q_num);
  1150. if (ret < 0) {
  1151. netdev_err(ndev,
  1152. "netif_set_real_num_rx_queues fail, ret=%d!\n", ret);
  1153. return ret;
  1154. }
  1155. ret = hns_nic_net_up(ndev);
  1156. if (ret) {
  1157. netdev_err(ndev,
  1158. "hns net up fail, ret=%d!\n", ret);
  1159. return ret;
  1160. }
  1161. return 0;
  1162. }
  1163. static int hns_nic_net_stop(struct net_device *ndev)
  1164. {
  1165. hns_nic_net_down(ndev);
  1166. return 0;
  1167. }
  1168. static void hns_tx_timeout_reset(struct hns_nic_priv *priv);
  1169. static void hns_nic_net_timeout(struct net_device *ndev)
  1170. {
  1171. struct hns_nic_priv *priv = netdev_priv(ndev);
  1172. hns_tx_timeout_reset(priv);
  1173. }
  1174. static int hns_nic_do_ioctl(struct net_device *netdev, struct ifreq *ifr,
  1175. int cmd)
  1176. {
  1177. struct phy_device *phy_dev = netdev->phydev;
  1178. if (!netif_running(netdev))
  1179. return -EINVAL;
  1180. if (!phy_dev)
  1181. return -ENOTSUPP;
  1182. return phy_mii_ioctl(phy_dev, ifr, cmd);
  1183. }
  1184. /* use only for netconsole to poll with the device without interrupt */
  1185. #ifdef CONFIG_NET_POLL_CONTROLLER
  1186. void hns_nic_poll_controller(struct net_device *ndev)
  1187. {
  1188. struct hns_nic_priv *priv = netdev_priv(ndev);
  1189. unsigned long flags;
  1190. int i;
  1191. local_irq_save(flags);
  1192. for (i = 0; i < priv->ae_handle->q_num * 2; i++)
  1193. napi_schedule(&priv->ring_data[i].napi);
  1194. local_irq_restore(flags);
  1195. }
  1196. #endif
  1197. static netdev_tx_t hns_nic_net_xmit(struct sk_buff *skb,
  1198. struct net_device *ndev)
  1199. {
  1200. struct hns_nic_priv *priv = netdev_priv(ndev);
  1201. int ret;
  1202. assert(skb->queue_mapping < ndev->ae_handle->q_num);
  1203. ret = hns_nic_net_xmit_hw(ndev, skb,
  1204. &tx_ring_data(priv, skb->queue_mapping));
  1205. if (ret == NETDEV_TX_OK) {
  1206. netif_trans_update(ndev);
  1207. ndev->stats.tx_bytes += skb->len;
  1208. ndev->stats.tx_packets++;
  1209. }
  1210. return (netdev_tx_t)ret;
  1211. }
  1212. static int hns_nic_change_mtu(struct net_device *ndev, int new_mtu)
  1213. {
  1214. struct hns_nic_priv *priv = netdev_priv(ndev);
  1215. struct hnae_handle *h = priv->ae_handle;
  1216. int ret;
  1217. if (!h->dev->ops->set_mtu)
  1218. return -ENOTSUPP;
  1219. if (netif_running(ndev)) {
  1220. (void)hns_nic_net_stop(ndev);
  1221. msleep(100);
  1222. ret = h->dev->ops->set_mtu(h, new_mtu);
  1223. if (ret)
  1224. netdev_err(ndev, "set mtu fail, return value %d\n",
  1225. ret);
  1226. if (hns_nic_net_open(ndev))
  1227. netdev_err(ndev, "hns net open fail\n");
  1228. } else {
  1229. ret = h->dev->ops->set_mtu(h, new_mtu);
  1230. }
  1231. if (!ret)
  1232. ndev->mtu = new_mtu;
  1233. return ret;
  1234. }
  1235. static int hns_nic_set_features(struct net_device *netdev,
  1236. netdev_features_t features)
  1237. {
  1238. struct hns_nic_priv *priv = netdev_priv(netdev);
  1239. switch (priv->enet_ver) {
  1240. case AE_VERSION_1:
  1241. if (features & (NETIF_F_TSO | NETIF_F_TSO6))
  1242. netdev_info(netdev, "enet v1 do not support tso!\n");
  1243. break;
  1244. default:
  1245. if (features & (NETIF_F_TSO | NETIF_F_TSO6)) {
  1246. priv->ops.fill_desc = fill_tso_desc;
  1247. priv->ops.maybe_stop_tx = hns_nic_maybe_stop_tso;
  1248. /* The chip only support 7*4096 */
  1249. netif_set_gso_max_size(netdev, 7 * 4096);
  1250. } else {
  1251. priv->ops.fill_desc = fill_v2_desc;
  1252. priv->ops.maybe_stop_tx = hns_nic_maybe_stop_tx;
  1253. }
  1254. break;
  1255. }
  1256. netdev->features = features;
  1257. return 0;
  1258. }
  1259. static netdev_features_t hns_nic_fix_features(
  1260. struct net_device *netdev, netdev_features_t features)
  1261. {
  1262. struct hns_nic_priv *priv = netdev_priv(netdev);
  1263. switch (priv->enet_ver) {
  1264. case AE_VERSION_1:
  1265. features &= ~(NETIF_F_TSO | NETIF_F_TSO6 |
  1266. NETIF_F_HW_VLAN_CTAG_FILTER);
  1267. break;
  1268. default:
  1269. break;
  1270. }
  1271. return features;
  1272. }
  1273. static int hns_nic_uc_sync(struct net_device *netdev, const unsigned char *addr)
  1274. {
  1275. struct hns_nic_priv *priv = netdev_priv(netdev);
  1276. struct hnae_handle *h = priv->ae_handle;
  1277. if (h->dev->ops->add_uc_addr)
  1278. return h->dev->ops->add_uc_addr(h, addr);
  1279. return 0;
  1280. }
  1281. static int hns_nic_uc_unsync(struct net_device *netdev,
  1282. const unsigned char *addr)
  1283. {
  1284. struct hns_nic_priv *priv = netdev_priv(netdev);
  1285. struct hnae_handle *h = priv->ae_handle;
  1286. if (h->dev->ops->rm_uc_addr)
  1287. return h->dev->ops->rm_uc_addr(h, addr);
  1288. return 0;
  1289. }
  1290. /**
  1291. * nic_set_multicast_list - set mutl mac address
  1292. * @netdev: net device
  1293. * @p: mac address
  1294. *
  1295. * return void
  1296. */
  1297. void hns_set_multicast_list(struct net_device *ndev)
  1298. {
  1299. struct hns_nic_priv *priv = netdev_priv(ndev);
  1300. struct hnae_handle *h = priv->ae_handle;
  1301. struct netdev_hw_addr *ha = NULL;
  1302. if (!h) {
  1303. netdev_err(ndev, "hnae handle is null\n");
  1304. return;
  1305. }
  1306. if (h->dev->ops->clr_mc_addr)
  1307. if (h->dev->ops->clr_mc_addr(h))
  1308. netdev_err(ndev, "clear multicast address fail\n");
  1309. if (h->dev->ops->set_mc_addr) {
  1310. netdev_for_each_mc_addr(ha, ndev)
  1311. if (h->dev->ops->set_mc_addr(h, ha->addr))
  1312. netdev_err(ndev, "set multicast fail\n");
  1313. }
  1314. }
  1315. void hns_nic_set_rx_mode(struct net_device *ndev)
  1316. {
  1317. struct hns_nic_priv *priv = netdev_priv(ndev);
  1318. struct hnae_handle *h = priv->ae_handle;
  1319. if (h->dev->ops->set_promisc_mode) {
  1320. if (ndev->flags & IFF_PROMISC)
  1321. h->dev->ops->set_promisc_mode(h, 1);
  1322. else
  1323. h->dev->ops->set_promisc_mode(h, 0);
  1324. }
  1325. hns_set_multicast_list(ndev);
  1326. if (__dev_uc_sync(ndev, hns_nic_uc_sync, hns_nic_uc_unsync))
  1327. netdev_err(ndev, "sync uc address fail\n");
  1328. }
  1329. struct rtnl_link_stats64 *hns_nic_get_stats64(struct net_device *ndev,
  1330. struct rtnl_link_stats64 *stats)
  1331. {
  1332. int idx = 0;
  1333. u64 tx_bytes = 0;
  1334. u64 rx_bytes = 0;
  1335. u64 tx_pkts = 0;
  1336. u64 rx_pkts = 0;
  1337. struct hns_nic_priv *priv = netdev_priv(ndev);
  1338. struct hnae_handle *h = priv->ae_handle;
  1339. for (idx = 0; idx < h->q_num; idx++) {
  1340. tx_bytes += h->qs[idx]->tx_ring.stats.tx_bytes;
  1341. tx_pkts += h->qs[idx]->tx_ring.stats.tx_pkts;
  1342. rx_bytes += h->qs[idx]->rx_ring.stats.rx_bytes;
  1343. rx_pkts += h->qs[idx]->rx_ring.stats.rx_pkts;
  1344. }
  1345. stats->tx_bytes = tx_bytes;
  1346. stats->tx_packets = tx_pkts;
  1347. stats->rx_bytes = rx_bytes;
  1348. stats->rx_packets = rx_pkts;
  1349. stats->rx_errors = ndev->stats.rx_errors;
  1350. stats->multicast = ndev->stats.multicast;
  1351. stats->rx_length_errors = ndev->stats.rx_length_errors;
  1352. stats->rx_crc_errors = ndev->stats.rx_crc_errors;
  1353. stats->rx_missed_errors = ndev->stats.rx_missed_errors;
  1354. stats->tx_errors = ndev->stats.tx_errors;
  1355. stats->rx_dropped = ndev->stats.rx_dropped;
  1356. stats->tx_dropped = ndev->stats.tx_dropped;
  1357. stats->collisions = ndev->stats.collisions;
  1358. stats->rx_over_errors = ndev->stats.rx_over_errors;
  1359. stats->rx_frame_errors = ndev->stats.rx_frame_errors;
  1360. stats->rx_fifo_errors = ndev->stats.rx_fifo_errors;
  1361. stats->tx_aborted_errors = ndev->stats.tx_aborted_errors;
  1362. stats->tx_carrier_errors = ndev->stats.tx_carrier_errors;
  1363. stats->tx_fifo_errors = ndev->stats.tx_fifo_errors;
  1364. stats->tx_heartbeat_errors = ndev->stats.tx_heartbeat_errors;
  1365. stats->tx_window_errors = ndev->stats.tx_window_errors;
  1366. stats->rx_compressed = ndev->stats.rx_compressed;
  1367. stats->tx_compressed = ndev->stats.tx_compressed;
  1368. return stats;
  1369. }
  1370. static u16
  1371. hns_nic_select_queue(struct net_device *ndev, struct sk_buff *skb,
  1372. void *accel_priv, select_queue_fallback_t fallback)
  1373. {
  1374. struct ethhdr *eth_hdr = (struct ethhdr *)skb->data;
  1375. struct hns_nic_priv *priv = netdev_priv(ndev);
  1376. /* fix hardware broadcast/multicast packets queue loopback */
  1377. if (!AE_IS_VER1(priv->enet_ver) &&
  1378. is_multicast_ether_addr(eth_hdr->h_dest))
  1379. return 0;
  1380. else
  1381. return fallback(ndev, skb);
  1382. }
  1383. static const struct net_device_ops hns_nic_netdev_ops = {
  1384. .ndo_open = hns_nic_net_open,
  1385. .ndo_stop = hns_nic_net_stop,
  1386. .ndo_start_xmit = hns_nic_net_xmit,
  1387. .ndo_tx_timeout = hns_nic_net_timeout,
  1388. .ndo_set_mac_address = hns_nic_net_set_mac_address,
  1389. .ndo_change_mtu = hns_nic_change_mtu,
  1390. .ndo_do_ioctl = hns_nic_do_ioctl,
  1391. .ndo_set_features = hns_nic_set_features,
  1392. .ndo_fix_features = hns_nic_fix_features,
  1393. .ndo_get_stats64 = hns_nic_get_stats64,
  1394. #ifdef CONFIG_NET_POLL_CONTROLLER
  1395. .ndo_poll_controller = hns_nic_poll_controller,
  1396. #endif
  1397. .ndo_set_rx_mode = hns_nic_set_rx_mode,
  1398. .ndo_select_queue = hns_nic_select_queue,
  1399. };
  1400. static void hns_nic_update_link_status(struct net_device *netdev)
  1401. {
  1402. struct hns_nic_priv *priv = netdev_priv(netdev);
  1403. struct hnae_handle *h = priv->ae_handle;
  1404. if (h->phy_dev) {
  1405. if (h->phy_if != PHY_INTERFACE_MODE_XGMII)
  1406. return;
  1407. (void)genphy_read_status(h->phy_dev);
  1408. }
  1409. hns_nic_adjust_link(netdev);
  1410. }
  1411. /* for dumping key regs*/
  1412. static void hns_nic_dump(struct hns_nic_priv *priv)
  1413. {
  1414. struct hnae_handle *h = priv->ae_handle;
  1415. struct hnae_ae_ops *ops = h->dev->ops;
  1416. u32 *data, reg_num, i;
  1417. if (ops->get_regs_len && ops->get_regs) {
  1418. reg_num = ops->get_regs_len(priv->ae_handle);
  1419. reg_num = (reg_num + 3ul) & ~3ul;
  1420. data = kcalloc(reg_num, sizeof(u32), GFP_KERNEL);
  1421. if (data) {
  1422. ops->get_regs(priv->ae_handle, data);
  1423. for (i = 0; i < reg_num; i += 4)
  1424. pr_info("0x%08x: 0x%08x 0x%08x 0x%08x 0x%08x\n",
  1425. i, data[i], data[i + 1],
  1426. data[i + 2], data[i + 3]);
  1427. kfree(data);
  1428. }
  1429. }
  1430. for (i = 0; i < h->q_num; i++) {
  1431. pr_info("tx_queue%d_next_to_clean:%d\n",
  1432. i, h->qs[i]->tx_ring.next_to_clean);
  1433. pr_info("tx_queue%d_next_to_use:%d\n",
  1434. i, h->qs[i]->tx_ring.next_to_use);
  1435. pr_info("rx_queue%d_next_to_clean:%d\n",
  1436. i, h->qs[i]->rx_ring.next_to_clean);
  1437. pr_info("rx_queue%d_next_to_use:%d\n",
  1438. i, h->qs[i]->rx_ring.next_to_use);
  1439. }
  1440. }
  1441. /* for resetting subtask */
  1442. static void hns_nic_reset_subtask(struct hns_nic_priv *priv)
  1443. {
  1444. enum hnae_port_type type = priv->ae_handle->port_type;
  1445. if (!test_bit(NIC_STATE2_RESET_REQUESTED, &priv->state))
  1446. return;
  1447. clear_bit(NIC_STATE2_RESET_REQUESTED, &priv->state);
  1448. /* If we're already down, removing or resetting, just bail */
  1449. if (test_bit(NIC_STATE_DOWN, &priv->state) ||
  1450. test_bit(NIC_STATE_REMOVING, &priv->state) ||
  1451. test_bit(NIC_STATE_RESETTING, &priv->state))
  1452. return;
  1453. hns_nic_dump(priv);
  1454. netdev_info(priv->netdev, "try to reset %s port!\n",
  1455. (type == HNAE_PORT_DEBUG ? "debug" : "service"));
  1456. rtnl_lock();
  1457. /* put off any impending NetWatchDogTimeout */
  1458. netif_trans_update(priv->netdev);
  1459. if (type == HNAE_PORT_DEBUG) {
  1460. hns_nic_net_reinit(priv->netdev);
  1461. } else {
  1462. netif_carrier_off(priv->netdev);
  1463. netif_tx_disable(priv->netdev);
  1464. }
  1465. rtnl_unlock();
  1466. }
  1467. /* for doing service complete*/
  1468. static void hns_nic_service_event_complete(struct hns_nic_priv *priv)
  1469. {
  1470. WARN_ON(!test_bit(NIC_STATE_SERVICE_SCHED, &priv->state));
  1471. smp_mb__before_atomic();
  1472. clear_bit(NIC_STATE_SERVICE_SCHED, &priv->state);
  1473. }
  1474. static void hns_nic_service_task(struct work_struct *work)
  1475. {
  1476. struct hns_nic_priv *priv
  1477. = container_of(work, struct hns_nic_priv, service_task);
  1478. struct hnae_handle *h = priv->ae_handle;
  1479. hns_nic_update_link_status(priv->netdev);
  1480. h->dev->ops->update_led_status(h);
  1481. hns_nic_update_stats(priv->netdev);
  1482. hns_nic_reset_subtask(priv);
  1483. hns_nic_service_event_complete(priv);
  1484. }
  1485. static void hns_nic_task_schedule(struct hns_nic_priv *priv)
  1486. {
  1487. if (!test_bit(NIC_STATE_DOWN, &priv->state) &&
  1488. !test_bit(NIC_STATE_REMOVING, &priv->state) &&
  1489. !test_and_set_bit(NIC_STATE_SERVICE_SCHED, &priv->state))
  1490. (void)schedule_work(&priv->service_task);
  1491. }
  1492. static void hns_nic_service_timer(unsigned long data)
  1493. {
  1494. struct hns_nic_priv *priv = (struct hns_nic_priv *)data;
  1495. (void)mod_timer(&priv->service_timer, jiffies + SERVICE_TIMER_HZ);
  1496. hns_nic_task_schedule(priv);
  1497. }
  1498. /**
  1499. * hns_tx_timeout_reset - initiate reset due to Tx timeout
  1500. * @priv: driver private struct
  1501. **/
  1502. static void hns_tx_timeout_reset(struct hns_nic_priv *priv)
  1503. {
  1504. /* Do the reset outside of interrupt context */
  1505. if (!test_bit(NIC_STATE_DOWN, &priv->state)) {
  1506. set_bit(NIC_STATE2_RESET_REQUESTED, &priv->state);
  1507. netdev_warn(priv->netdev,
  1508. "initiating reset due to tx timeout(%llu,0x%lx)\n",
  1509. priv->tx_timeout_count, priv->state);
  1510. priv->tx_timeout_count++;
  1511. hns_nic_task_schedule(priv);
  1512. }
  1513. }
  1514. static int hns_nic_init_ring_data(struct hns_nic_priv *priv)
  1515. {
  1516. struct hnae_handle *h = priv->ae_handle;
  1517. struct hns_nic_ring_data *rd;
  1518. bool is_ver1 = AE_IS_VER1(priv->enet_ver);
  1519. int i;
  1520. if (h->q_num > NIC_MAX_Q_PER_VF) {
  1521. netdev_err(priv->netdev, "too much queue (%d)\n", h->q_num);
  1522. return -EINVAL;
  1523. }
  1524. priv->ring_data = kzalloc(h->q_num * sizeof(*priv->ring_data) * 2,
  1525. GFP_KERNEL);
  1526. if (!priv->ring_data)
  1527. return -ENOMEM;
  1528. for (i = 0; i < h->q_num; i++) {
  1529. rd = &priv->ring_data[i];
  1530. rd->queue_index = i;
  1531. rd->ring = &h->qs[i]->tx_ring;
  1532. rd->poll_one = hns_nic_tx_poll_one;
  1533. rd->fini_process = is_ver1 ? hns_nic_tx_fini_pro :
  1534. hns_nic_tx_fini_pro_v2;
  1535. netif_napi_add(priv->netdev, &rd->napi,
  1536. hns_nic_common_poll, NIC_TX_CLEAN_MAX_NUM);
  1537. rd->ring->irq_init_flag = RCB_IRQ_NOT_INITED;
  1538. }
  1539. for (i = h->q_num; i < h->q_num * 2; i++) {
  1540. rd = &priv->ring_data[i];
  1541. rd->queue_index = i - h->q_num;
  1542. rd->ring = &h->qs[i - h->q_num]->rx_ring;
  1543. rd->poll_one = hns_nic_rx_poll_one;
  1544. rd->ex_process = hns_nic_rx_up_pro;
  1545. rd->fini_process = is_ver1 ? hns_nic_rx_fini_pro :
  1546. hns_nic_rx_fini_pro_v2;
  1547. netif_napi_add(priv->netdev, &rd->napi,
  1548. hns_nic_common_poll, NIC_RX_CLEAN_MAX_NUM);
  1549. rd->ring->irq_init_flag = RCB_IRQ_NOT_INITED;
  1550. }
  1551. return 0;
  1552. }
  1553. static void hns_nic_uninit_ring_data(struct hns_nic_priv *priv)
  1554. {
  1555. struct hnae_handle *h = priv->ae_handle;
  1556. int i;
  1557. for (i = 0; i < h->q_num * 2; i++) {
  1558. netif_napi_del(&priv->ring_data[i].napi);
  1559. if (priv->ring_data[i].ring->irq_init_flag == RCB_IRQ_INITED) {
  1560. (void)irq_set_affinity_hint(
  1561. priv->ring_data[i].ring->irq,
  1562. NULL);
  1563. free_irq(priv->ring_data[i].ring->irq,
  1564. &priv->ring_data[i]);
  1565. }
  1566. priv->ring_data[i].ring->irq_init_flag = RCB_IRQ_NOT_INITED;
  1567. }
  1568. kfree(priv->ring_data);
  1569. }
  1570. static void hns_nic_set_priv_ops(struct net_device *netdev)
  1571. {
  1572. struct hns_nic_priv *priv = netdev_priv(netdev);
  1573. struct hnae_handle *h = priv->ae_handle;
  1574. if (AE_IS_VER1(priv->enet_ver)) {
  1575. priv->ops.fill_desc = fill_desc;
  1576. priv->ops.get_rxd_bnum = get_rx_desc_bnum;
  1577. priv->ops.maybe_stop_tx = hns_nic_maybe_stop_tx;
  1578. } else {
  1579. priv->ops.get_rxd_bnum = get_v2rx_desc_bnum;
  1580. if ((netdev->features & NETIF_F_TSO) ||
  1581. (netdev->features & NETIF_F_TSO6)) {
  1582. priv->ops.fill_desc = fill_tso_desc;
  1583. priv->ops.maybe_stop_tx = hns_nic_maybe_stop_tso;
  1584. /* This chip only support 7*4096 */
  1585. netif_set_gso_max_size(netdev, 7 * 4096);
  1586. } else {
  1587. priv->ops.fill_desc = fill_v2_desc;
  1588. priv->ops.maybe_stop_tx = hns_nic_maybe_stop_tx;
  1589. }
  1590. /* enable tso when init
  1591. * control tso on/off through TSE bit in bd
  1592. */
  1593. h->dev->ops->set_tso_stats(h, 1);
  1594. }
  1595. }
  1596. static int hns_nic_try_get_ae(struct net_device *ndev)
  1597. {
  1598. struct hns_nic_priv *priv = netdev_priv(ndev);
  1599. struct hnae_handle *h;
  1600. int ret;
  1601. h = hnae_get_handle(&priv->netdev->dev,
  1602. priv->fwnode, priv->port_id, NULL);
  1603. if (IS_ERR_OR_NULL(h)) {
  1604. ret = -ENODEV;
  1605. dev_dbg(priv->dev, "has not handle, register notifier!\n");
  1606. goto out;
  1607. }
  1608. priv->ae_handle = h;
  1609. ret = hns_nic_init_phy(ndev, h);
  1610. if (ret) {
  1611. dev_err(priv->dev, "probe phy device fail!\n");
  1612. goto out_init_phy;
  1613. }
  1614. ret = hns_nic_init_ring_data(priv);
  1615. if (ret) {
  1616. ret = -ENOMEM;
  1617. goto out_init_ring_data;
  1618. }
  1619. hns_nic_set_priv_ops(ndev);
  1620. ret = register_netdev(ndev);
  1621. if (ret) {
  1622. dev_err(priv->dev, "probe register netdev fail!\n");
  1623. goto out_reg_ndev_fail;
  1624. }
  1625. return 0;
  1626. out_reg_ndev_fail:
  1627. hns_nic_uninit_ring_data(priv);
  1628. priv->ring_data = NULL;
  1629. out_init_phy:
  1630. out_init_ring_data:
  1631. hnae_put_handle(priv->ae_handle);
  1632. priv->ae_handle = NULL;
  1633. out:
  1634. return ret;
  1635. }
  1636. static int hns_nic_notifier_action(struct notifier_block *nb,
  1637. unsigned long action, void *data)
  1638. {
  1639. struct hns_nic_priv *priv =
  1640. container_of(nb, struct hns_nic_priv, notifier_block);
  1641. assert(action == HNAE_AE_REGISTER);
  1642. if (!hns_nic_try_get_ae(priv->netdev)) {
  1643. hnae_unregister_notifier(&priv->notifier_block);
  1644. priv->notifier_block.notifier_call = NULL;
  1645. }
  1646. return 0;
  1647. }
  1648. static int hns_nic_dev_probe(struct platform_device *pdev)
  1649. {
  1650. struct device *dev = &pdev->dev;
  1651. struct net_device *ndev;
  1652. struct hns_nic_priv *priv;
  1653. u32 port_id;
  1654. int ret;
  1655. ndev = alloc_etherdev_mq(sizeof(struct hns_nic_priv), NIC_MAX_Q_PER_VF);
  1656. if (!ndev)
  1657. return -ENOMEM;
  1658. platform_set_drvdata(pdev, ndev);
  1659. priv = netdev_priv(ndev);
  1660. priv->dev = dev;
  1661. priv->netdev = ndev;
  1662. if (dev_of_node(dev)) {
  1663. struct device_node *ae_node;
  1664. if (of_device_is_compatible(dev->of_node,
  1665. "hisilicon,hns-nic-v1"))
  1666. priv->enet_ver = AE_VERSION_1;
  1667. else
  1668. priv->enet_ver = AE_VERSION_2;
  1669. ae_node = of_parse_phandle(dev->of_node, "ae-handle", 0);
  1670. if (IS_ERR_OR_NULL(ae_node)) {
  1671. ret = PTR_ERR(ae_node);
  1672. dev_err(dev, "not find ae-handle\n");
  1673. goto out_read_prop_fail;
  1674. }
  1675. priv->fwnode = &ae_node->fwnode;
  1676. } else if (is_acpi_node(dev->fwnode)) {
  1677. struct acpi_reference_args args;
  1678. if (acpi_dev_found(hns_enet_acpi_match[0].id))
  1679. priv->enet_ver = AE_VERSION_1;
  1680. else if (acpi_dev_found(hns_enet_acpi_match[1].id))
  1681. priv->enet_ver = AE_VERSION_2;
  1682. else
  1683. return -ENXIO;
  1684. /* try to find port-idx-in-ae first */
  1685. ret = acpi_node_get_property_reference(dev->fwnode,
  1686. "ae-handle", 0, &args);
  1687. if (ret) {
  1688. dev_err(dev, "not find ae-handle\n");
  1689. goto out_read_prop_fail;
  1690. }
  1691. priv->fwnode = acpi_fwnode_handle(args.adev);
  1692. } else {
  1693. dev_err(dev, "cannot read cfg data from OF or acpi\n");
  1694. return -ENXIO;
  1695. }
  1696. ret = device_property_read_u32(dev, "port-idx-in-ae", &port_id);
  1697. if (ret) {
  1698. /* only for old code compatible */
  1699. ret = device_property_read_u32(dev, "port-id", &port_id);
  1700. if (ret)
  1701. goto out_read_prop_fail;
  1702. /* for old dts, we need to caculate the port offset */
  1703. port_id = port_id < HNS_SRV_OFFSET ? port_id + HNS_DEBUG_OFFSET
  1704. : port_id - HNS_SRV_OFFSET;
  1705. }
  1706. priv->port_id = port_id;
  1707. hns_init_mac_addr(ndev);
  1708. ndev->watchdog_timeo = HNS_NIC_TX_TIMEOUT;
  1709. ndev->priv_flags |= IFF_UNICAST_FLT;
  1710. ndev->netdev_ops = &hns_nic_netdev_ops;
  1711. hns_ethtool_set_ops(ndev);
  1712. ndev->features |= NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
  1713. NETIF_F_RXCSUM | NETIF_F_SG | NETIF_F_GSO |
  1714. NETIF_F_GRO;
  1715. ndev->vlan_features |=
  1716. NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_RXCSUM;
  1717. ndev->vlan_features |= NETIF_F_SG | NETIF_F_GSO | NETIF_F_GRO;
  1718. /* MTU range: 68 - 9578 (v1) or 9706 (v2) */
  1719. ndev->min_mtu = MAC_MIN_MTU;
  1720. switch (priv->enet_ver) {
  1721. case AE_VERSION_2:
  1722. ndev->features |= NETIF_F_TSO | NETIF_F_TSO6;
  1723. ndev->hw_features |= NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
  1724. NETIF_F_RXCSUM | NETIF_F_SG | NETIF_F_GSO |
  1725. NETIF_F_GRO | NETIF_F_TSO | NETIF_F_TSO6;
  1726. ndev->max_mtu = MAC_MAX_MTU_V2 -
  1727. (ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN);
  1728. break;
  1729. default:
  1730. ndev->max_mtu = MAC_MAX_MTU -
  1731. (ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN);
  1732. break;
  1733. }
  1734. SET_NETDEV_DEV(ndev, dev);
  1735. if (!dma_set_mask_and_coherent(dev, DMA_BIT_MASK(64)))
  1736. dev_dbg(dev, "set mask to 64bit\n");
  1737. else
  1738. dev_err(dev, "set mask to 64bit fail!\n");
  1739. /* carrier off reporting is important to ethtool even BEFORE open */
  1740. netif_carrier_off(ndev);
  1741. setup_timer(&priv->service_timer, hns_nic_service_timer,
  1742. (unsigned long)priv);
  1743. INIT_WORK(&priv->service_task, hns_nic_service_task);
  1744. set_bit(NIC_STATE_SERVICE_INITED, &priv->state);
  1745. clear_bit(NIC_STATE_SERVICE_SCHED, &priv->state);
  1746. set_bit(NIC_STATE_DOWN, &priv->state);
  1747. if (hns_nic_try_get_ae(priv->netdev)) {
  1748. priv->notifier_block.notifier_call = hns_nic_notifier_action;
  1749. ret = hnae_register_notifier(&priv->notifier_block);
  1750. if (ret) {
  1751. dev_err(dev, "register notifier fail!\n");
  1752. goto out_notify_fail;
  1753. }
  1754. dev_dbg(dev, "has not handle, register notifier!\n");
  1755. }
  1756. return 0;
  1757. out_notify_fail:
  1758. (void)cancel_work_sync(&priv->service_task);
  1759. out_read_prop_fail:
  1760. free_netdev(ndev);
  1761. return ret;
  1762. }
  1763. static int hns_nic_dev_remove(struct platform_device *pdev)
  1764. {
  1765. struct net_device *ndev = platform_get_drvdata(pdev);
  1766. struct hns_nic_priv *priv = netdev_priv(ndev);
  1767. if (ndev->reg_state != NETREG_UNINITIALIZED)
  1768. unregister_netdev(ndev);
  1769. if (priv->ring_data)
  1770. hns_nic_uninit_ring_data(priv);
  1771. priv->ring_data = NULL;
  1772. if (ndev->phydev)
  1773. phy_disconnect(ndev->phydev);
  1774. if (!IS_ERR_OR_NULL(priv->ae_handle))
  1775. hnae_put_handle(priv->ae_handle);
  1776. priv->ae_handle = NULL;
  1777. if (priv->notifier_block.notifier_call)
  1778. hnae_unregister_notifier(&priv->notifier_block);
  1779. priv->notifier_block.notifier_call = NULL;
  1780. set_bit(NIC_STATE_REMOVING, &priv->state);
  1781. (void)cancel_work_sync(&priv->service_task);
  1782. free_netdev(ndev);
  1783. return 0;
  1784. }
  1785. static const struct of_device_id hns_enet_of_match[] = {
  1786. {.compatible = "hisilicon,hns-nic-v1",},
  1787. {.compatible = "hisilicon,hns-nic-v2",},
  1788. {},
  1789. };
  1790. MODULE_DEVICE_TABLE(of, hns_enet_of_match);
  1791. static struct platform_driver hns_nic_dev_driver = {
  1792. .driver = {
  1793. .name = "hns-nic",
  1794. .of_match_table = hns_enet_of_match,
  1795. .acpi_match_table = ACPI_PTR(hns_enet_acpi_match),
  1796. },
  1797. .probe = hns_nic_dev_probe,
  1798. .remove = hns_nic_dev_remove,
  1799. };
  1800. module_platform_driver(hns_nic_dev_driver);
  1801. MODULE_DESCRIPTION("HISILICON HNS Ethernet driver");
  1802. MODULE_AUTHOR("Hisilicon, Inc.");
  1803. MODULE_LICENSE("GPL");
  1804. MODULE_ALIAS("platform:hns-nic");