hns_enet.c 51 KB

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