hns_enet.c 53 KB

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