hns_enet.c 61 KB

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