netdev.c 78 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Copyright(c) 2009 - 2018 Intel Corporation. */
  3. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  4. #include <linux/module.h>
  5. #include <linux/types.h>
  6. #include <linux/init.h>
  7. #include <linux/pci.h>
  8. #include <linux/vmalloc.h>
  9. #include <linux/pagemap.h>
  10. #include <linux/delay.h>
  11. #include <linux/netdevice.h>
  12. #include <linux/tcp.h>
  13. #include <linux/ipv6.h>
  14. #include <linux/slab.h>
  15. #include <net/checksum.h>
  16. #include <net/ip6_checksum.h>
  17. #include <linux/mii.h>
  18. #include <linux/ethtool.h>
  19. #include <linux/if_vlan.h>
  20. #include <linux/prefetch.h>
  21. #include <linux/sctp.h>
  22. #include "igbvf.h"
  23. #define DRV_VERSION "2.4.0-k"
  24. char igbvf_driver_name[] = "igbvf";
  25. const char igbvf_driver_version[] = DRV_VERSION;
  26. static const char igbvf_driver_string[] =
  27. "Intel(R) Gigabit Virtual Function Network Driver";
  28. static const char igbvf_copyright[] =
  29. "Copyright (c) 2009 - 2012 Intel Corporation.";
  30. #define DEFAULT_MSG_ENABLE (NETIF_MSG_DRV|NETIF_MSG_PROBE|NETIF_MSG_LINK)
  31. static int debug = -1;
  32. module_param(debug, int, 0);
  33. MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all)");
  34. static int igbvf_poll(struct napi_struct *napi, int budget);
  35. static void igbvf_reset(struct igbvf_adapter *);
  36. static void igbvf_set_interrupt_capability(struct igbvf_adapter *);
  37. static void igbvf_reset_interrupt_capability(struct igbvf_adapter *);
  38. static struct igbvf_info igbvf_vf_info = {
  39. .mac = e1000_vfadapt,
  40. .flags = 0,
  41. .pba = 10,
  42. .init_ops = e1000_init_function_pointers_vf,
  43. };
  44. static struct igbvf_info igbvf_i350_vf_info = {
  45. .mac = e1000_vfadapt_i350,
  46. .flags = 0,
  47. .pba = 10,
  48. .init_ops = e1000_init_function_pointers_vf,
  49. };
  50. static const struct igbvf_info *igbvf_info_tbl[] = {
  51. [board_vf] = &igbvf_vf_info,
  52. [board_i350_vf] = &igbvf_i350_vf_info,
  53. };
  54. /**
  55. * igbvf_desc_unused - calculate if we have unused descriptors
  56. * @rx_ring: address of receive ring structure
  57. **/
  58. static int igbvf_desc_unused(struct igbvf_ring *ring)
  59. {
  60. if (ring->next_to_clean > ring->next_to_use)
  61. return ring->next_to_clean - ring->next_to_use - 1;
  62. return ring->count + ring->next_to_clean - ring->next_to_use - 1;
  63. }
  64. /**
  65. * igbvf_receive_skb - helper function to handle Rx indications
  66. * @adapter: board private structure
  67. * @status: descriptor status field as written by hardware
  68. * @vlan: descriptor vlan field as written by hardware (no le/be conversion)
  69. * @skb: pointer to sk_buff to be indicated to stack
  70. **/
  71. static void igbvf_receive_skb(struct igbvf_adapter *adapter,
  72. struct net_device *netdev,
  73. struct sk_buff *skb,
  74. u32 status, u16 vlan)
  75. {
  76. u16 vid;
  77. if (status & E1000_RXD_STAT_VP) {
  78. if ((adapter->flags & IGBVF_FLAG_RX_LB_VLAN_BSWAP) &&
  79. (status & E1000_RXDEXT_STATERR_LB))
  80. vid = be16_to_cpu(vlan) & E1000_RXD_SPC_VLAN_MASK;
  81. else
  82. vid = le16_to_cpu(vlan) & E1000_RXD_SPC_VLAN_MASK;
  83. if (test_bit(vid, adapter->active_vlans))
  84. __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), vid);
  85. }
  86. napi_gro_receive(&adapter->rx_ring->napi, skb);
  87. }
  88. static inline void igbvf_rx_checksum_adv(struct igbvf_adapter *adapter,
  89. u32 status_err, struct sk_buff *skb)
  90. {
  91. skb_checksum_none_assert(skb);
  92. /* Ignore Checksum bit is set or checksum is disabled through ethtool */
  93. if ((status_err & E1000_RXD_STAT_IXSM) ||
  94. (adapter->flags & IGBVF_FLAG_RX_CSUM_DISABLED))
  95. return;
  96. /* TCP/UDP checksum error bit is set */
  97. if (status_err &
  98. (E1000_RXDEXT_STATERR_TCPE | E1000_RXDEXT_STATERR_IPE)) {
  99. /* let the stack verify checksum errors */
  100. adapter->hw_csum_err++;
  101. return;
  102. }
  103. /* It must be a TCP or UDP packet with a valid checksum */
  104. if (status_err & (E1000_RXD_STAT_TCPCS | E1000_RXD_STAT_UDPCS))
  105. skb->ip_summed = CHECKSUM_UNNECESSARY;
  106. adapter->hw_csum_good++;
  107. }
  108. /**
  109. * igbvf_alloc_rx_buffers - Replace used receive buffers; packet split
  110. * @rx_ring: address of ring structure to repopulate
  111. * @cleaned_count: number of buffers to repopulate
  112. **/
  113. static void igbvf_alloc_rx_buffers(struct igbvf_ring *rx_ring,
  114. int cleaned_count)
  115. {
  116. struct igbvf_adapter *adapter = rx_ring->adapter;
  117. struct net_device *netdev = adapter->netdev;
  118. struct pci_dev *pdev = adapter->pdev;
  119. union e1000_adv_rx_desc *rx_desc;
  120. struct igbvf_buffer *buffer_info;
  121. struct sk_buff *skb;
  122. unsigned int i;
  123. int bufsz;
  124. i = rx_ring->next_to_use;
  125. buffer_info = &rx_ring->buffer_info[i];
  126. if (adapter->rx_ps_hdr_size)
  127. bufsz = adapter->rx_ps_hdr_size;
  128. else
  129. bufsz = adapter->rx_buffer_len;
  130. while (cleaned_count--) {
  131. rx_desc = IGBVF_RX_DESC_ADV(*rx_ring, i);
  132. if (adapter->rx_ps_hdr_size && !buffer_info->page_dma) {
  133. if (!buffer_info->page) {
  134. buffer_info->page = alloc_page(GFP_ATOMIC);
  135. if (!buffer_info->page) {
  136. adapter->alloc_rx_buff_failed++;
  137. goto no_buffers;
  138. }
  139. buffer_info->page_offset = 0;
  140. } else {
  141. buffer_info->page_offset ^= PAGE_SIZE / 2;
  142. }
  143. buffer_info->page_dma =
  144. dma_map_page(&pdev->dev, buffer_info->page,
  145. buffer_info->page_offset,
  146. PAGE_SIZE / 2,
  147. DMA_FROM_DEVICE);
  148. if (dma_mapping_error(&pdev->dev,
  149. buffer_info->page_dma)) {
  150. __free_page(buffer_info->page);
  151. buffer_info->page = NULL;
  152. dev_err(&pdev->dev, "RX DMA map failed\n");
  153. break;
  154. }
  155. }
  156. if (!buffer_info->skb) {
  157. skb = netdev_alloc_skb_ip_align(netdev, bufsz);
  158. if (!skb) {
  159. adapter->alloc_rx_buff_failed++;
  160. goto no_buffers;
  161. }
  162. buffer_info->skb = skb;
  163. buffer_info->dma = dma_map_single(&pdev->dev, skb->data,
  164. bufsz,
  165. DMA_FROM_DEVICE);
  166. if (dma_mapping_error(&pdev->dev, buffer_info->dma)) {
  167. dev_kfree_skb(buffer_info->skb);
  168. buffer_info->skb = NULL;
  169. dev_err(&pdev->dev, "RX DMA map failed\n");
  170. goto no_buffers;
  171. }
  172. }
  173. /* Refresh the desc even if buffer_addrs didn't change because
  174. * each write-back erases this info.
  175. */
  176. if (adapter->rx_ps_hdr_size) {
  177. rx_desc->read.pkt_addr =
  178. cpu_to_le64(buffer_info->page_dma);
  179. rx_desc->read.hdr_addr = cpu_to_le64(buffer_info->dma);
  180. } else {
  181. rx_desc->read.pkt_addr = cpu_to_le64(buffer_info->dma);
  182. rx_desc->read.hdr_addr = 0;
  183. }
  184. i++;
  185. if (i == rx_ring->count)
  186. i = 0;
  187. buffer_info = &rx_ring->buffer_info[i];
  188. }
  189. no_buffers:
  190. if (rx_ring->next_to_use != i) {
  191. rx_ring->next_to_use = i;
  192. if (i == 0)
  193. i = (rx_ring->count - 1);
  194. else
  195. i--;
  196. /* Force memory writes to complete before letting h/w
  197. * know there are new descriptors to fetch. (Only
  198. * applicable for weak-ordered memory model archs,
  199. * such as IA-64).
  200. */
  201. wmb();
  202. writel(i, adapter->hw.hw_addr + rx_ring->tail);
  203. }
  204. }
  205. /**
  206. * igbvf_clean_rx_irq - Send received data up the network stack; legacy
  207. * @adapter: board private structure
  208. *
  209. * the return value indicates whether actual cleaning was done, there
  210. * is no guarantee that everything was cleaned
  211. **/
  212. static bool igbvf_clean_rx_irq(struct igbvf_adapter *adapter,
  213. int *work_done, int work_to_do)
  214. {
  215. struct igbvf_ring *rx_ring = adapter->rx_ring;
  216. struct net_device *netdev = adapter->netdev;
  217. struct pci_dev *pdev = adapter->pdev;
  218. union e1000_adv_rx_desc *rx_desc, *next_rxd;
  219. struct igbvf_buffer *buffer_info, *next_buffer;
  220. struct sk_buff *skb;
  221. bool cleaned = false;
  222. int cleaned_count = 0;
  223. unsigned int total_bytes = 0, total_packets = 0;
  224. unsigned int i;
  225. u32 length, hlen, staterr;
  226. i = rx_ring->next_to_clean;
  227. rx_desc = IGBVF_RX_DESC_ADV(*rx_ring, i);
  228. staterr = le32_to_cpu(rx_desc->wb.upper.status_error);
  229. while (staterr & E1000_RXD_STAT_DD) {
  230. if (*work_done >= work_to_do)
  231. break;
  232. (*work_done)++;
  233. rmb(); /* read descriptor and rx_buffer_info after status DD */
  234. buffer_info = &rx_ring->buffer_info[i];
  235. /* HW will not DMA in data larger than the given buffer, even
  236. * if it parses the (NFS, of course) header to be larger. In
  237. * that case, it fills the header buffer and spills the rest
  238. * into the page.
  239. */
  240. hlen = (le16_to_cpu(rx_desc->wb.lower.lo_dword.hs_rss.hdr_info)
  241. & E1000_RXDADV_HDRBUFLEN_MASK) >>
  242. E1000_RXDADV_HDRBUFLEN_SHIFT;
  243. if (hlen > adapter->rx_ps_hdr_size)
  244. hlen = adapter->rx_ps_hdr_size;
  245. length = le16_to_cpu(rx_desc->wb.upper.length);
  246. cleaned = true;
  247. cleaned_count++;
  248. skb = buffer_info->skb;
  249. prefetch(skb->data - NET_IP_ALIGN);
  250. buffer_info->skb = NULL;
  251. if (!adapter->rx_ps_hdr_size) {
  252. dma_unmap_single(&pdev->dev, buffer_info->dma,
  253. adapter->rx_buffer_len,
  254. DMA_FROM_DEVICE);
  255. buffer_info->dma = 0;
  256. skb_put(skb, length);
  257. goto send_up;
  258. }
  259. if (!skb_shinfo(skb)->nr_frags) {
  260. dma_unmap_single(&pdev->dev, buffer_info->dma,
  261. adapter->rx_ps_hdr_size,
  262. DMA_FROM_DEVICE);
  263. buffer_info->dma = 0;
  264. skb_put(skb, hlen);
  265. }
  266. if (length) {
  267. dma_unmap_page(&pdev->dev, buffer_info->page_dma,
  268. PAGE_SIZE / 2,
  269. DMA_FROM_DEVICE);
  270. buffer_info->page_dma = 0;
  271. skb_fill_page_desc(skb, skb_shinfo(skb)->nr_frags,
  272. buffer_info->page,
  273. buffer_info->page_offset,
  274. length);
  275. if ((adapter->rx_buffer_len > (PAGE_SIZE / 2)) ||
  276. (page_count(buffer_info->page) != 1))
  277. buffer_info->page = NULL;
  278. else
  279. get_page(buffer_info->page);
  280. skb->len += length;
  281. skb->data_len += length;
  282. skb->truesize += PAGE_SIZE / 2;
  283. }
  284. send_up:
  285. i++;
  286. if (i == rx_ring->count)
  287. i = 0;
  288. next_rxd = IGBVF_RX_DESC_ADV(*rx_ring, i);
  289. prefetch(next_rxd);
  290. next_buffer = &rx_ring->buffer_info[i];
  291. if (!(staterr & E1000_RXD_STAT_EOP)) {
  292. buffer_info->skb = next_buffer->skb;
  293. buffer_info->dma = next_buffer->dma;
  294. next_buffer->skb = skb;
  295. next_buffer->dma = 0;
  296. goto next_desc;
  297. }
  298. if (staterr & E1000_RXDEXT_ERR_FRAME_ERR_MASK) {
  299. dev_kfree_skb_irq(skb);
  300. goto next_desc;
  301. }
  302. total_bytes += skb->len;
  303. total_packets++;
  304. igbvf_rx_checksum_adv(adapter, staterr, skb);
  305. skb->protocol = eth_type_trans(skb, netdev);
  306. igbvf_receive_skb(adapter, netdev, skb, staterr,
  307. rx_desc->wb.upper.vlan);
  308. next_desc:
  309. rx_desc->wb.upper.status_error = 0;
  310. /* return some buffers to hardware, one at a time is too slow */
  311. if (cleaned_count >= IGBVF_RX_BUFFER_WRITE) {
  312. igbvf_alloc_rx_buffers(rx_ring, cleaned_count);
  313. cleaned_count = 0;
  314. }
  315. /* use prefetched values */
  316. rx_desc = next_rxd;
  317. buffer_info = next_buffer;
  318. staterr = le32_to_cpu(rx_desc->wb.upper.status_error);
  319. }
  320. rx_ring->next_to_clean = i;
  321. cleaned_count = igbvf_desc_unused(rx_ring);
  322. if (cleaned_count)
  323. igbvf_alloc_rx_buffers(rx_ring, cleaned_count);
  324. adapter->total_rx_packets += total_packets;
  325. adapter->total_rx_bytes += total_bytes;
  326. netdev->stats.rx_bytes += total_bytes;
  327. netdev->stats.rx_packets += total_packets;
  328. return cleaned;
  329. }
  330. static void igbvf_put_txbuf(struct igbvf_adapter *adapter,
  331. struct igbvf_buffer *buffer_info)
  332. {
  333. if (buffer_info->dma) {
  334. if (buffer_info->mapped_as_page)
  335. dma_unmap_page(&adapter->pdev->dev,
  336. buffer_info->dma,
  337. buffer_info->length,
  338. DMA_TO_DEVICE);
  339. else
  340. dma_unmap_single(&adapter->pdev->dev,
  341. buffer_info->dma,
  342. buffer_info->length,
  343. DMA_TO_DEVICE);
  344. buffer_info->dma = 0;
  345. }
  346. if (buffer_info->skb) {
  347. dev_kfree_skb_any(buffer_info->skb);
  348. buffer_info->skb = NULL;
  349. }
  350. buffer_info->time_stamp = 0;
  351. }
  352. /**
  353. * igbvf_setup_tx_resources - allocate Tx resources (Descriptors)
  354. * @adapter: board private structure
  355. *
  356. * Return 0 on success, negative on failure
  357. **/
  358. int igbvf_setup_tx_resources(struct igbvf_adapter *adapter,
  359. struct igbvf_ring *tx_ring)
  360. {
  361. struct pci_dev *pdev = adapter->pdev;
  362. int size;
  363. size = sizeof(struct igbvf_buffer) * tx_ring->count;
  364. tx_ring->buffer_info = vzalloc(size);
  365. if (!tx_ring->buffer_info)
  366. goto err;
  367. /* round up to nearest 4K */
  368. tx_ring->size = tx_ring->count * sizeof(union e1000_adv_tx_desc);
  369. tx_ring->size = ALIGN(tx_ring->size, 4096);
  370. tx_ring->desc = dma_alloc_coherent(&pdev->dev, tx_ring->size,
  371. &tx_ring->dma, GFP_KERNEL);
  372. if (!tx_ring->desc)
  373. goto err;
  374. tx_ring->adapter = adapter;
  375. tx_ring->next_to_use = 0;
  376. tx_ring->next_to_clean = 0;
  377. return 0;
  378. err:
  379. vfree(tx_ring->buffer_info);
  380. dev_err(&adapter->pdev->dev,
  381. "Unable to allocate memory for the transmit descriptor ring\n");
  382. return -ENOMEM;
  383. }
  384. /**
  385. * igbvf_setup_rx_resources - allocate Rx resources (Descriptors)
  386. * @adapter: board private structure
  387. *
  388. * Returns 0 on success, negative on failure
  389. **/
  390. int igbvf_setup_rx_resources(struct igbvf_adapter *adapter,
  391. struct igbvf_ring *rx_ring)
  392. {
  393. struct pci_dev *pdev = adapter->pdev;
  394. int size, desc_len;
  395. size = sizeof(struct igbvf_buffer) * rx_ring->count;
  396. rx_ring->buffer_info = vzalloc(size);
  397. if (!rx_ring->buffer_info)
  398. goto err;
  399. desc_len = sizeof(union e1000_adv_rx_desc);
  400. /* Round up to nearest 4K */
  401. rx_ring->size = rx_ring->count * desc_len;
  402. rx_ring->size = ALIGN(rx_ring->size, 4096);
  403. rx_ring->desc = dma_alloc_coherent(&pdev->dev, rx_ring->size,
  404. &rx_ring->dma, GFP_KERNEL);
  405. if (!rx_ring->desc)
  406. goto err;
  407. rx_ring->next_to_clean = 0;
  408. rx_ring->next_to_use = 0;
  409. rx_ring->adapter = adapter;
  410. return 0;
  411. err:
  412. vfree(rx_ring->buffer_info);
  413. rx_ring->buffer_info = NULL;
  414. dev_err(&adapter->pdev->dev,
  415. "Unable to allocate memory for the receive descriptor ring\n");
  416. return -ENOMEM;
  417. }
  418. /**
  419. * igbvf_clean_tx_ring - Free Tx Buffers
  420. * @tx_ring: ring to be cleaned
  421. **/
  422. static void igbvf_clean_tx_ring(struct igbvf_ring *tx_ring)
  423. {
  424. struct igbvf_adapter *adapter = tx_ring->adapter;
  425. struct igbvf_buffer *buffer_info;
  426. unsigned long size;
  427. unsigned int i;
  428. if (!tx_ring->buffer_info)
  429. return;
  430. /* Free all the Tx ring sk_buffs */
  431. for (i = 0; i < tx_ring->count; i++) {
  432. buffer_info = &tx_ring->buffer_info[i];
  433. igbvf_put_txbuf(adapter, buffer_info);
  434. }
  435. size = sizeof(struct igbvf_buffer) * tx_ring->count;
  436. memset(tx_ring->buffer_info, 0, size);
  437. /* Zero out the descriptor ring */
  438. memset(tx_ring->desc, 0, tx_ring->size);
  439. tx_ring->next_to_use = 0;
  440. tx_ring->next_to_clean = 0;
  441. writel(0, adapter->hw.hw_addr + tx_ring->head);
  442. writel(0, adapter->hw.hw_addr + tx_ring->tail);
  443. }
  444. /**
  445. * igbvf_free_tx_resources - Free Tx Resources per Queue
  446. * @tx_ring: ring to free resources from
  447. *
  448. * Free all transmit software resources
  449. **/
  450. void igbvf_free_tx_resources(struct igbvf_ring *tx_ring)
  451. {
  452. struct pci_dev *pdev = tx_ring->adapter->pdev;
  453. igbvf_clean_tx_ring(tx_ring);
  454. vfree(tx_ring->buffer_info);
  455. tx_ring->buffer_info = NULL;
  456. dma_free_coherent(&pdev->dev, tx_ring->size, tx_ring->desc,
  457. tx_ring->dma);
  458. tx_ring->desc = NULL;
  459. }
  460. /**
  461. * igbvf_clean_rx_ring - Free Rx Buffers per Queue
  462. * @adapter: board private structure
  463. **/
  464. static void igbvf_clean_rx_ring(struct igbvf_ring *rx_ring)
  465. {
  466. struct igbvf_adapter *adapter = rx_ring->adapter;
  467. struct igbvf_buffer *buffer_info;
  468. struct pci_dev *pdev = adapter->pdev;
  469. unsigned long size;
  470. unsigned int i;
  471. if (!rx_ring->buffer_info)
  472. return;
  473. /* Free all the Rx ring sk_buffs */
  474. for (i = 0; i < rx_ring->count; i++) {
  475. buffer_info = &rx_ring->buffer_info[i];
  476. if (buffer_info->dma) {
  477. if (adapter->rx_ps_hdr_size) {
  478. dma_unmap_single(&pdev->dev, buffer_info->dma,
  479. adapter->rx_ps_hdr_size,
  480. DMA_FROM_DEVICE);
  481. } else {
  482. dma_unmap_single(&pdev->dev, buffer_info->dma,
  483. adapter->rx_buffer_len,
  484. DMA_FROM_DEVICE);
  485. }
  486. buffer_info->dma = 0;
  487. }
  488. if (buffer_info->skb) {
  489. dev_kfree_skb(buffer_info->skb);
  490. buffer_info->skb = NULL;
  491. }
  492. if (buffer_info->page) {
  493. if (buffer_info->page_dma)
  494. dma_unmap_page(&pdev->dev,
  495. buffer_info->page_dma,
  496. PAGE_SIZE / 2,
  497. DMA_FROM_DEVICE);
  498. put_page(buffer_info->page);
  499. buffer_info->page = NULL;
  500. buffer_info->page_dma = 0;
  501. buffer_info->page_offset = 0;
  502. }
  503. }
  504. size = sizeof(struct igbvf_buffer) * rx_ring->count;
  505. memset(rx_ring->buffer_info, 0, size);
  506. /* Zero out the descriptor ring */
  507. memset(rx_ring->desc, 0, rx_ring->size);
  508. rx_ring->next_to_clean = 0;
  509. rx_ring->next_to_use = 0;
  510. writel(0, adapter->hw.hw_addr + rx_ring->head);
  511. writel(0, adapter->hw.hw_addr + rx_ring->tail);
  512. }
  513. /**
  514. * igbvf_free_rx_resources - Free Rx Resources
  515. * @rx_ring: ring to clean the resources from
  516. *
  517. * Free all receive software resources
  518. **/
  519. void igbvf_free_rx_resources(struct igbvf_ring *rx_ring)
  520. {
  521. struct pci_dev *pdev = rx_ring->adapter->pdev;
  522. igbvf_clean_rx_ring(rx_ring);
  523. vfree(rx_ring->buffer_info);
  524. rx_ring->buffer_info = NULL;
  525. dma_free_coherent(&pdev->dev, rx_ring->size, rx_ring->desc,
  526. rx_ring->dma);
  527. rx_ring->desc = NULL;
  528. }
  529. /**
  530. * igbvf_update_itr - update the dynamic ITR value based on statistics
  531. * @adapter: pointer to adapter
  532. * @itr_setting: current adapter->itr
  533. * @packets: the number of packets during this measurement interval
  534. * @bytes: the number of bytes during this measurement interval
  535. *
  536. * Stores a new ITR value based on packets and byte counts during the last
  537. * interrupt. The advantage of per interrupt computation is faster updates
  538. * and more accurate ITR for the current traffic pattern. Constants in this
  539. * function were computed based on theoretical maximum wire speed and thresholds
  540. * were set based on testing data as well as attempting to minimize response
  541. * time while increasing bulk throughput.
  542. **/
  543. static enum latency_range igbvf_update_itr(struct igbvf_adapter *adapter,
  544. enum latency_range itr_setting,
  545. int packets, int bytes)
  546. {
  547. enum latency_range retval = itr_setting;
  548. if (packets == 0)
  549. goto update_itr_done;
  550. switch (itr_setting) {
  551. case lowest_latency:
  552. /* handle TSO and jumbo frames */
  553. if (bytes/packets > 8000)
  554. retval = bulk_latency;
  555. else if ((packets < 5) && (bytes > 512))
  556. retval = low_latency;
  557. break;
  558. case low_latency: /* 50 usec aka 20000 ints/s */
  559. if (bytes > 10000) {
  560. /* this if handles the TSO accounting */
  561. if (bytes/packets > 8000)
  562. retval = bulk_latency;
  563. else if ((packets < 10) || ((bytes/packets) > 1200))
  564. retval = bulk_latency;
  565. else if ((packets > 35))
  566. retval = lowest_latency;
  567. } else if (bytes/packets > 2000) {
  568. retval = bulk_latency;
  569. } else if (packets <= 2 && bytes < 512) {
  570. retval = lowest_latency;
  571. }
  572. break;
  573. case bulk_latency: /* 250 usec aka 4000 ints/s */
  574. if (bytes > 25000) {
  575. if (packets > 35)
  576. retval = low_latency;
  577. } else if (bytes < 6000) {
  578. retval = low_latency;
  579. }
  580. break;
  581. default:
  582. break;
  583. }
  584. update_itr_done:
  585. return retval;
  586. }
  587. static int igbvf_range_to_itr(enum latency_range current_range)
  588. {
  589. int new_itr;
  590. switch (current_range) {
  591. /* counts and packets in update_itr are dependent on these numbers */
  592. case lowest_latency:
  593. new_itr = IGBVF_70K_ITR;
  594. break;
  595. case low_latency:
  596. new_itr = IGBVF_20K_ITR;
  597. break;
  598. case bulk_latency:
  599. new_itr = IGBVF_4K_ITR;
  600. break;
  601. default:
  602. new_itr = IGBVF_START_ITR;
  603. break;
  604. }
  605. return new_itr;
  606. }
  607. static void igbvf_set_itr(struct igbvf_adapter *adapter)
  608. {
  609. u32 new_itr;
  610. adapter->tx_ring->itr_range =
  611. igbvf_update_itr(adapter,
  612. adapter->tx_ring->itr_val,
  613. adapter->total_tx_packets,
  614. adapter->total_tx_bytes);
  615. /* conservative mode (itr 3) eliminates the lowest_latency setting */
  616. if (adapter->requested_itr == 3 &&
  617. adapter->tx_ring->itr_range == lowest_latency)
  618. adapter->tx_ring->itr_range = low_latency;
  619. new_itr = igbvf_range_to_itr(adapter->tx_ring->itr_range);
  620. if (new_itr != adapter->tx_ring->itr_val) {
  621. u32 current_itr = adapter->tx_ring->itr_val;
  622. /* this attempts to bias the interrupt rate towards Bulk
  623. * by adding intermediate steps when interrupt rate is
  624. * increasing
  625. */
  626. new_itr = new_itr > current_itr ?
  627. min(current_itr + (new_itr >> 2), new_itr) :
  628. new_itr;
  629. adapter->tx_ring->itr_val = new_itr;
  630. adapter->tx_ring->set_itr = 1;
  631. }
  632. adapter->rx_ring->itr_range =
  633. igbvf_update_itr(adapter, adapter->rx_ring->itr_val,
  634. adapter->total_rx_packets,
  635. adapter->total_rx_bytes);
  636. if (adapter->requested_itr == 3 &&
  637. adapter->rx_ring->itr_range == lowest_latency)
  638. adapter->rx_ring->itr_range = low_latency;
  639. new_itr = igbvf_range_to_itr(adapter->rx_ring->itr_range);
  640. if (new_itr != adapter->rx_ring->itr_val) {
  641. u32 current_itr = adapter->rx_ring->itr_val;
  642. new_itr = new_itr > current_itr ?
  643. min(current_itr + (new_itr >> 2), new_itr) :
  644. new_itr;
  645. adapter->rx_ring->itr_val = new_itr;
  646. adapter->rx_ring->set_itr = 1;
  647. }
  648. }
  649. /**
  650. * igbvf_clean_tx_irq - Reclaim resources after transmit completes
  651. * @adapter: board private structure
  652. *
  653. * returns true if ring is completely cleaned
  654. **/
  655. static bool igbvf_clean_tx_irq(struct igbvf_ring *tx_ring)
  656. {
  657. struct igbvf_adapter *adapter = tx_ring->adapter;
  658. struct net_device *netdev = adapter->netdev;
  659. struct igbvf_buffer *buffer_info;
  660. struct sk_buff *skb;
  661. union e1000_adv_tx_desc *tx_desc, *eop_desc;
  662. unsigned int total_bytes = 0, total_packets = 0;
  663. unsigned int i, count = 0;
  664. bool cleaned = false;
  665. i = tx_ring->next_to_clean;
  666. buffer_info = &tx_ring->buffer_info[i];
  667. eop_desc = buffer_info->next_to_watch;
  668. do {
  669. /* if next_to_watch is not set then there is no work pending */
  670. if (!eop_desc)
  671. break;
  672. /* prevent any other reads prior to eop_desc */
  673. smp_rmb();
  674. /* if DD is not set pending work has not been completed */
  675. if (!(eop_desc->wb.status & cpu_to_le32(E1000_TXD_STAT_DD)))
  676. break;
  677. /* clear next_to_watch to prevent false hangs */
  678. buffer_info->next_to_watch = NULL;
  679. for (cleaned = false; !cleaned; count++) {
  680. tx_desc = IGBVF_TX_DESC_ADV(*tx_ring, i);
  681. cleaned = (tx_desc == eop_desc);
  682. skb = buffer_info->skb;
  683. if (skb) {
  684. unsigned int segs, bytecount;
  685. /* gso_segs is currently only valid for tcp */
  686. segs = skb_shinfo(skb)->gso_segs ?: 1;
  687. /* multiply data chunks by size of headers */
  688. bytecount = ((segs - 1) * skb_headlen(skb)) +
  689. skb->len;
  690. total_packets += segs;
  691. total_bytes += bytecount;
  692. }
  693. igbvf_put_txbuf(adapter, buffer_info);
  694. tx_desc->wb.status = 0;
  695. i++;
  696. if (i == tx_ring->count)
  697. i = 0;
  698. buffer_info = &tx_ring->buffer_info[i];
  699. }
  700. eop_desc = buffer_info->next_to_watch;
  701. } while (count < tx_ring->count);
  702. tx_ring->next_to_clean = i;
  703. if (unlikely(count && netif_carrier_ok(netdev) &&
  704. igbvf_desc_unused(tx_ring) >= IGBVF_TX_QUEUE_WAKE)) {
  705. /* Make sure that anybody stopping the queue after this
  706. * sees the new next_to_clean.
  707. */
  708. smp_mb();
  709. if (netif_queue_stopped(netdev) &&
  710. !(test_bit(__IGBVF_DOWN, &adapter->state))) {
  711. netif_wake_queue(netdev);
  712. ++adapter->restart_queue;
  713. }
  714. }
  715. netdev->stats.tx_bytes += total_bytes;
  716. netdev->stats.tx_packets += total_packets;
  717. return count < tx_ring->count;
  718. }
  719. static irqreturn_t igbvf_msix_other(int irq, void *data)
  720. {
  721. struct net_device *netdev = data;
  722. struct igbvf_adapter *adapter = netdev_priv(netdev);
  723. struct e1000_hw *hw = &adapter->hw;
  724. adapter->int_counter1++;
  725. hw->mac.get_link_status = 1;
  726. if (!test_bit(__IGBVF_DOWN, &adapter->state))
  727. mod_timer(&adapter->watchdog_timer, jiffies + 1);
  728. ew32(EIMS, adapter->eims_other);
  729. return IRQ_HANDLED;
  730. }
  731. static irqreturn_t igbvf_intr_msix_tx(int irq, void *data)
  732. {
  733. struct net_device *netdev = data;
  734. struct igbvf_adapter *adapter = netdev_priv(netdev);
  735. struct e1000_hw *hw = &adapter->hw;
  736. struct igbvf_ring *tx_ring = adapter->tx_ring;
  737. if (tx_ring->set_itr) {
  738. writel(tx_ring->itr_val,
  739. adapter->hw.hw_addr + tx_ring->itr_register);
  740. adapter->tx_ring->set_itr = 0;
  741. }
  742. adapter->total_tx_bytes = 0;
  743. adapter->total_tx_packets = 0;
  744. /* auto mask will automatically re-enable the interrupt when we write
  745. * EICS
  746. */
  747. if (!igbvf_clean_tx_irq(tx_ring))
  748. /* Ring was not completely cleaned, so fire another interrupt */
  749. ew32(EICS, tx_ring->eims_value);
  750. else
  751. ew32(EIMS, tx_ring->eims_value);
  752. return IRQ_HANDLED;
  753. }
  754. static irqreturn_t igbvf_intr_msix_rx(int irq, void *data)
  755. {
  756. struct net_device *netdev = data;
  757. struct igbvf_adapter *adapter = netdev_priv(netdev);
  758. adapter->int_counter0++;
  759. /* Write the ITR value calculated at the end of the
  760. * previous interrupt.
  761. */
  762. if (adapter->rx_ring->set_itr) {
  763. writel(adapter->rx_ring->itr_val,
  764. adapter->hw.hw_addr + adapter->rx_ring->itr_register);
  765. adapter->rx_ring->set_itr = 0;
  766. }
  767. if (napi_schedule_prep(&adapter->rx_ring->napi)) {
  768. adapter->total_rx_bytes = 0;
  769. adapter->total_rx_packets = 0;
  770. __napi_schedule(&adapter->rx_ring->napi);
  771. }
  772. return IRQ_HANDLED;
  773. }
  774. #define IGBVF_NO_QUEUE -1
  775. static void igbvf_assign_vector(struct igbvf_adapter *adapter, int rx_queue,
  776. int tx_queue, int msix_vector)
  777. {
  778. struct e1000_hw *hw = &adapter->hw;
  779. u32 ivar, index;
  780. /* 82576 uses a table-based method for assigning vectors.
  781. * Each queue has a single entry in the table to which we write
  782. * a vector number along with a "valid" bit. Sadly, the layout
  783. * of the table is somewhat counterintuitive.
  784. */
  785. if (rx_queue > IGBVF_NO_QUEUE) {
  786. index = (rx_queue >> 1);
  787. ivar = array_er32(IVAR0, index);
  788. if (rx_queue & 0x1) {
  789. /* vector goes into third byte of register */
  790. ivar = ivar & 0xFF00FFFF;
  791. ivar |= (msix_vector | E1000_IVAR_VALID) << 16;
  792. } else {
  793. /* vector goes into low byte of register */
  794. ivar = ivar & 0xFFFFFF00;
  795. ivar |= msix_vector | E1000_IVAR_VALID;
  796. }
  797. adapter->rx_ring[rx_queue].eims_value = BIT(msix_vector);
  798. array_ew32(IVAR0, index, ivar);
  799. }
  800. if (tx_queue > IGBVF_NO_QUEUE) {
  801. index = (tx_queue >> 1);
  802. ivar = array_er32(IVAR0, index);
  803. if (tx_queue & 0x1) {
  804. /* vector goes into high byte of register */
  805. ivar = ivar & 0x00FFFFFF;
  806. ivar |= (msix_vector | E1000_IVAR_VALID) << 24;
  807. } else {
  808. /* vector goes into second byte of register */
  809. ivar = ivar & 0xFFFF00FF;
  810. ivar |= (msix_vector | E1000_IVAR_VALID) << 8;
  811. }
  812. adapter->tx_ring[tx_queue].eims_value = BIT(msix_vector);
  813. array_ew32(IVAR0, index, ivar);
  814. }
  815. }
  816. /**
  817. * igbvf_configure_msix - Configure MSI-X hardware
  818. * @adapter: board private structure
  819. *
  820. * igbvf_configure_msix sets up the hardware to properly
  821. * generate MSI-X interrupts.
  822. **/
  823. static void igbvf_configure_msix(struct igbvf_adapter *adapter)
  824. {
  825. u32 tmp;
  826. struct e1000_hw *hw = &adapter->hw;
  827. struct igbvf_ring *tx_ring = adapter->tx_ring;
  828. struct igbvf_ring *rx_ring = adapter->rx_ring;
  829. int vector = 0;
  830. adapter->eims_enable_mask = 0;
  831. igbvf_assign_vector(adapter, IGBVF_NO_QUEUE, 0, vector++);
  832. adapter->eims_enable_mask |= tx_ring->eims_value;
  833. writel(tx_ring->itr_val, hw->hw_addr + tx_ring->itr_register);
  834. igbvf_assign_vector(adapter, 0, IGBVF_NO_QUEUE, vector++);
  835. adapter->eims_enable_mask |= rx_ring->eims_value;
  836. writel(rx_ring->itr_val, hw->hw_addr + rx_ring->itr_register);
  837. /* set vector for other causes, i.e. link changes */
  838. tmp = (vector++ | E1000_IVAR_VALID);
  839. ew32(IVAR_MISC, tmp);
  840. adapter->eims_enable_mask = GENMASK(vector - 1, 0);
  841. adapter->eims_other = BIT(vector - 1);
  842. e1e_flush();
  843. }
  844. static void igbvf_reset_interrupt_capability(struct igbvf_adapter *adapter)
  845. {
  846. if (adapter->msix_entries) {
  847. pci_disable_msix(adapter->pdev);
  848. kfree(adapter->msix_entries);
  849. adapter->msix_entries = NULL;
  850. }
  851. }
  852. /**
  853. * igbvf_set_interrupt_capability - set MSI or MSI-X if supported
  854. * @adapter: board private structure
  855. *
  856. * Attempt to configure interrupts using the best available
  857. * capabilities of the hardware and kernel.
  858. **/
  859. static void igbvf_set_interrupt_capability(struct igbvf_adapter *adapter)
  860. {
  861. int err = -ENOMEM;
  862. int i;
  863. /* we allocate 3 vectors, 1 for Tx, 1 for Rx, one for PF messages */
  864. adapter->msix_entries = kcalloc(3, sizeof(struct msix_entry),
  865. GFP_KERNEL);
  866. if (adapter->msix_entries) {
  867. for (i = 0; i < 3; i++)
  868. adapter->msix_entries[i].entry = i;
  869. err = pci_enable_msix_range(adapter->pdev,
  870. adapter->msix_entries, 3, 3);
  871. }
  872. if (err < 0) {
  873. /* MSI-X failed */
  874. dev_err(&adapter->pdev->dev,
  875. "Failed to initialize MSI-X interrupts.\n");
  876. igbvf_reset_interrupt_capability(adapter);
  877. }
  878. }
  879. /**
  880. * igbvf_request_msix - Initialize MSI-X interrupts
  881. * @adapter: board private structure
  882. *
  883. * igbvf_request_msix allocates MSI-X vectors and requests interrupts from the
  884. * kernel.
  885. **/
  886. static int igbvf_request_msix(struct igbvf_adapter *adapter)
  887. {
  888. struct net_device *netdev = adapter->netdev;
  889. int err = 0, vector = 0;
  890. if (strlen(netdev->name) < (IFNAMSIZ - 5)) {
  891. sprintf(adapter->tx_ring->name, "%s-tx-0", netdev->name);
  892. sprintf(adapter->rx_ring->name, "%s-rx-0", netdev->name);
  893. } else {
  894. memcpy(adapter->tx_ring->name, netdev->name, IFNAMSIZ);
  895. memcpy(adapter->rx_ring->name, netdev->name, IFNAMSIZ);
  896. }
  897. err = request_irq(adapter->msix_entries[vector].vector,
  898. igbvf_intr_msix_tx, 0, adapter->tx_ring->name,
  899. netdev);
  900. if (err)
  901. goto out;
  902. adapter->tx_ring->itr_register = E1000_EITR(vector);
  903. adapter->tx_ring->itr_val = adapter->current_itr;
  904. vector++;
  905. err = request_irq(adapter->msix_entries[vector].vector,
  906. igbvf_intr_msix_rx, 0, adapter->rx_ring->name,
  907. netdev);
  908. if (err)
  909. goto out;
  910. adapter->rx_ring->itr_register = E1000_EITR(vector);
  911. adapter->rx_ring->itr_val = adapter->current_itr;
  912. vector++;
  913. err = request_irq(adapter->msix_entries[vector].vector,
  914. igbvf_msix_other, 0, netdev->name, netdev);
  915. if (err)
  916. goto out;
  917. igbvf_configure_msix(adapter);
  918. return 0;
  919. out:
  920. return err;
  921. }
  922. /**
  923. * igbvf_alloc_queues - Allocate memory for all rings
  924. * @adapter: board private structure to initialize
  925. **/
  926. static int igbvf_alloc_queues(struct igbvf_adapter *adapter)
  927. {
  928. struct net_device *netdev = adapter->netdev;
  929. adapter->tx_ring = kzalloc(sizeof(struct igbvf_ring), GFP_KERNEL);
  930. if (!adapter->tx_ring)
  931. return -ENOMEM;
  932. adapter->rx_ring = kzalloc(sizeof(struct igbvf_ring), GFP_KERNEL);
  933. if (!adapter->rx_ring) {
  934. kfree(adapter->tx_ring);
  935. return -ENOMEM;
  936. }
  937. netif_napi_add(netdev, &adapter->rx_ring->napi, igbvf_poll, 64);
  938. return 0;
  939. }
  940. /**
  941. * igbvf_request_irq - initialize interrupts
  942. * @adapter: board private structure
  943. *
  944. * Attempts to configure interrupts using the best available
  945. * capabilities of the hardware and kernel.
  946. **/
  947. static int igbvf_request_irq(struct igbvf_adapter *adapter)
  948. {
  949. int err = -1;
  950. /* igbvf supports msi-x only */
  951. if (adapter->msix_entries)
  952. err = igbvf_request_msix(adapter);
  953. if (!err)
  954. return err;
  955. dev_err(&adapter->pdev->dev,
  956. "Unable to allocate interrupt, Error: %d\n", err);
  957. return err;
  958. }
  959. static void igbvf_free_irq(struct igbvf_adapter *adapter)
  960. {
  961. struct net_device *netdev = adapter->netdev;
  962. int vector;
  963. if (adapter->msix_entries) {
  964. for (vector = 0; vector < 3; vector++)
  965. free_irq(adapter->msix_entries[vector].vector, netdev);
  966. }
  967. }
  968. /**
  969. * igbvf_irq_disable - Mask off interrupt generation on the NIC
  970. * @adapter: board private structure
  971. **/
  972. static void igbvf_irq_disable(struct igbvf_adapter *adapter)
  973. {
  974. struct e1000_hw *hw = &adapter->hw;
  975. ew32(EIMC, ~0);
  976. if (adapter->msix_entries)
  977. ew32(EIAC, 0);
  978. }
  979. /**
  980. * igbvf_irq_enable - Enable default interrupt generation settings
  981. * @adapter: board private structure
  982. **/
  983. static void igbvf_irq_enable(struct igbvf_adapter *adapter)
  984. {
  985. struct e1000_hw *hw = &adapter->hw;
  986. ew32(EIAC, adapter->eims_enable_mask);
  987. ew32(EIAM, adapter->eims_enable_mask);
  988. ew32(EIMS, adapter->eims_enable_mask);
  989. }
  990. /**
  991. * igbvf_poll - NAPI Rx polling callback
  992. * @napi: struct associated with this polling callback
  993. * @budget: amount of packets driver is allowed to process this poll
  994. **/
  995. static int igbvf_poll(struct napi_struct *napi, int budget)
  996. {
  997. struct igbvf_ring *rx_ring = container_of(napi, struct igbvf_ring, napi);
  998. struct igbvf_adapter *adapter = rx_ring->adapter;
  999. struct e1000_hw *hw = &adapter->hw;
  1000. int work_done = 0;
  1001. igbvf_clean_rx_irq(adapter, &work_done, budget);
  1002. /* If not enough Rx work done, exit the polling mode */
  1003. if (work_done < budget) {
  1004. napi_complete_done(napi, work_done);
  1005. if (adapter->requested_itr & 3)
  1006. igbvf_set_itr(adapter);
  1007. if (!test_bit(__IGBVF_DOWN, &adapter->state))
  1008. ew32(EIMS, adapter->rx_ring->eims_value);
  1009. }
  1010. return work_done;
  1011. }
  1012. /**
  1013. * igbvf_set_rlpml - set receive large packet maximum length
  1014. * @adapter: board private structure
  1015. *
  1016. * Configure the maximum size of packets that will be received
  1017. */
  1018. static void igbvf_set_rlpml(struct igbvf_adapter *adapter)
  1019. {
  1020. int max_frame_size;
  1021. struct e1000_hw *hw = &adapter->hw;
  1022. max_frame_size = adapter->max_frame_size + VLAN_TAG_SIZE;
  1023. spin_lock_bh(&hw->mbx_lock);
  1024. e1000_rlpml_set_vf(hw, max_frame_size);
  1025. spin_unlock_bh(&hw->mbx_lock);
  1026. }
  1027. static int igbvf_vlan_rx_add_vid(struct net_device *netdev,
  1028. __be16 proto, u16 vid)
  1029. {
  1030. struct igbvf_adapter *adapter = netdev_priv(netdev);
  1031. struct e1000_hw *hw = &adapter->hw;
  1032. spin_lock_bh(&hw->mbx_lock);
  1033. if (hw->mac.ops.set_vfta(hw, vid, true)) {
  1034. dev_err(&adapter->pdev->dev, "Failed to add vlan id %d\n", vid);
  1035. spin_unlock_bh(&hw->mbx_lock);
  1036. return -EINVAL;
  1037. }
  1038. spin_unlock_bh(&hw->mbx_lock);
  1039. set_bit(vid, adapter->active_vlans);
  1040. return 0;
  1041. }
  1042. static int igbvf_vlan_rx_kill_vid(struct net_device *netdev,
  1043. __be16 proto, u16 vid)
  1044. {
  1045. struct igbvf_adapter *adapter = netdev_priv(netdev);
  1046. struct e1000_hw *hw = &adapter->hw;
  1047. spin_lock_bh(&hw->mbx_lock);
  1048. if (hw->mac.ops.set_vfta(hw, vid, false)) {
  1049. dev_err(&adapter->pdev->dev,
  1050. "Failed to remove vlan id %d\n", vid);
  1051. spin_unlock_bh(&hw->mbx_lock);
  1052. return -EINVAL;
  1053. }
  1054. spin_unlock_bh(&hw->mbx_lock);
  1055. clear_bit(vid, adapter->active_vlans);
  1056. return 0;
  1057. }
  1058. static void igbvf_restore_vlan(struct igbvf_adapter *adapter)
  1059. {
  1060. u16 vid;
  1061. for_each_set_bit(vid, adapter->active_vlans, VLAN_N_VID)
  1062. igbvf_vlan_rx_add_vid(adapter->netdev, htons(ETH_P_8021Q), vid);
  1063. }
  1064. /**
  1065. * igbvf_configure_tx - Configure Transmit Unit after Reset
  1066. * @adapter: board private structure
  1067. *
  1068. * Configure the Tx unit of the MAC after a reset.
  1069. **/
  1070. static void igbvf_configure_tx(struct igbvf_adapter *adapter)
  1071. {
  1072. struct e1000_hw *hw = &adapter->hw;
  1073. struct igbvf_ring *tx_ring = adapter->tx_ring;
  1074. u64 tdba;
  1075. u32 txdctl, dca_txctrl;
  1076. /* disable transmits */
  1077. txdctl = er32(TXDCTL(0));
  1078. ew32(TXDCTL(0), txdctl & ~E1000_TXDCTL_QUEUE_ENABLE);
  1079. e1e_flush();
  1080. msleep(10);
  1081. /* Setup the HW Tx Head and Tail descriptor pointers */
  1082. ew32(TDLEN(0), tx_ring->count * sizeof(union e1000_adv_tx_desc));
  1083. tdba = tx_ring->dma;
  1084. ew32(TDBAL(0), (tdba & DMA_BIT_MASK(32)));
  1085. ew32(TDBAH(0), (tdba >> 32));
  1086. ew32(TDH(0), 0);
  1087. ew32(TDT(0), 0);
  1088. tx_ring->head = E1000_TDH(0);
  1089. tx_ring->tail = E1000_TDT(0);
  1090. /* Turn off Relaxed Ordering on head write-backs. The writebacks
  1091. * MUST be delivered in order or it will completely screw up
  1092. * our bookkeeping.
  1093. */
  1094. dca_txctrl = er32(DCA_TXCTRL(0));
  1095. dca_txctrl &= ~E1000_DCA_TXCTRL_TX_WB_RO_EN;
  1096. ew32(DCA_TXCTRL(0), dca_txctrl);
  1097. /* enable transmits */
  1098. txdctl |= E1000_TXDCTL_QUEUE_ENABLE;
  1099. ew32(TXDCTL(0), txdctl);
  1100. /* Setup Transmit Descriptor Settings for eop descriptor */
  1101. adapter->txd_cmd = E1000_ADVTXD_DCMD_EOP | E1000_ADVTXD_DCMD_IFCS;
  1102. /* enable Report Status bit */
  1103. adapter->txd_cmd |= E1000_ADVTXD_DCMD_RS;
  1104. }
  1105. /**
  1106. * igbvf_setup_srrctl - configure the receive control registers
  1107. * @adapter: Board private structure
  1108. **/
  1109. static void igbvf_setup_srrctl(struct igbvf_adapter *adapter)
  1110. {
  1111. struct e1000_hw *hw = &adapter->hw;
  1112. u32 srrctl = 0;
  1113. srrctl &= ~(E1000_SRRCTL_DESCTYPE_MASK |
  1114. E1000_SRRCTL_BSIZEHDR_MASK |
  1115. E1000_SRRCTL_BSIZEPKT_MASK);
  1116. /* Enable queue drop to avoid head of line blocking */
  1117. srrctl |= E1000_SRRCTL_DROP_EN;
  1118. /* Setup buffer sizes */
  1119. srrctl |= ALIGN(adapter->rx_buffer_len, 1024) >>
  1120. E1000_SRRCTL_BSIZEPKT_SHIFT;
  1121. if (adapter->rx_buffer_len < 2048) {
  1122. adapter->rx_ps_hdr_size = 0;
  1123. srrctl |= E1000_SRRCTL_DESCTYPE_ADV_ONEBUF;
  1124. } else {
  1125. adapter->rx_ps_hdr_size = 128;
  1126. srrctl |= adapter->rx_ps_hdr_size <<
  1127. E1000_SRRCTL_BSIZEHDRSIZE_SHIFT;
  1128. srrctl |= E1000_SRRCTL_DESCTYPE_HDR_SPLIT_ALWAYS;
  1129. }
  1130. ew32(SRRCTL(0), srrctl);
  1131. }
  1132. /**
  1133. * igbvf_configure_rx - Configure Receive Unit after Reset
  1134. * @adapter: board private structure
  1135. *
  1136. * Configure the Rx unit of the MAC after a reset.
  1137. **/
  1138. static void igbvf_configure_rx(struct igbvf_adapter *adapter)
  1139. {
  1140. struct e1000_hw *hw = &adapter->hw;
  1141. struct igbvf_ring *rx_ring = adapter->rx_ring;
  1142. u64 rdba;
  1143. u32 rxdctl;
  1144. /* disable receives */
  1145. rxdctl = er32(RXDCTL(0));
  1146. ew32(RXDCTL(0), rxdctl & ~E1000_RXDCTL_QUEUE_ENABLE);
  1147. e1e_flush();
  1148. msleep(10);
  1149. /* Setup the HW Rx Head and Tail Descriptor Pointers and
  1150. * the Base and Length of the Rx Descriptor Ring
  1151. */
  1152. rdba = rx_ring->dma;
  1153. ew32(RDBAL(0), (rdba & DMA_BIT_MASK(32)));
  1154. ew32(RDBAH(0), (rdba >> 32));
  1155. ew32(RDLEN(0), rx_ring->count * sizeof(union e1000_adv_rx_desc));
  1156. rx_ring->head = E1000_RDH(0);
  1157. rx_ring->tail = E1000_RDT(0);
  1158. ew32(RDH(0), 0);
  1159. ew32(RDT(0), 0);
  1160. rxdctl |= E1000_RXDCTL_QUEUE_ENABLE;
  1161. rxdctl &= 0xFFF00000;
  1162. rxdctl |= IGBVF_RX_PTHRESH;
  1163. rxdctl |= IGBVF_RX_HTHRESH << 8;
  1164. rxdctl |= IGBVF_RX_WTHRESH << 16;
  1165. igbvf_set_rlpml(adapter);
  1166. /* enable receives */
  1167. ew32(RXDCTL(0), rxdctl);
  1168. }
  1169. /**
  1170. * igbvf_set_multi - Multicast and Promiscuous mode set
  1171. * @netdev: network interface device structure
  1172. *
  1173. * The set_multi entry point is called whenever the multicast address
  1174. * list or the network interface flags are updated. This routine is
  1175. * responsible for configuring the hardware for proper multicast,
  1176. * promiscuous mode, and all-multi behavior.
  1177. **/
  1178. static void igbvf_set_multi(struct net_device *netdev)
  1179. {
  1180. struct igbvf_adapter *adapter = netdev_priv(netdev);
  1181. struct e1000_hw *hw = &adapter->hw;
  1182. struct netdev_hw_addr *ha;
  1183. u8 *mta_list = NULL;
  1184. int i;
  1185. if (!netdev_mc_empty(netdev)) {
  1186. mta_list = kmalloc_array(netdev_mc_count(netdev), ETH_ALEN,
  1187. GFP_ATOMIC);
  1188. if (!mta_list)
  1189. return;
  1190. }
  1191. /* prepare a packed array of only addresses. */
  1192. i = 0;
  1193. netdev_for_each_mc_addr(ha, netdev)
  1194. memcpy(mta_list + (i++ * ETH_ALEN), ha->addr, ETH_ALEN);
  1195. spin_lock_bh(&hw->mbx_lock);
  1196. hw->mac.ops.update_mc_addr_list(hw, mta_list, i, 0, 0);
  1197. spin_unlock_bh(&hw->mbx_lock);
  1198. kfree(mta_list);
  1199. }
  1200. /**
  1201. * igbvf_set_uni - Configure unicast MAC filters
  1202. * @netdev: network interface device structure
  1203. *
  1204. * This routine is responsible for configuring the hardware for proper
  1205. * unicast filters.
  1206. **/
  1207. static int igbvf_set_uni(struct net_device *netdev)
  1208. {
  1209. struct igbvf_adapter *adapter = netdev_priv(netdev);
  1210. struct e1000_hw *hw = &adapter->hw;
  1211. if (netdev_uc_count(netdev) > IGBVF_MAX_MAC_FILTERS) {
  1212. pr_err("Too many unicast filters - No Space\n");
  1213. return -ENOSPC;
  1214. }
  1215. spin_lock_bh(&hw->mbx_lock);
  1216. /* Clear all unicast MAC filters */
  1217. hw->mac.ops.set_uc_addr(hw, E1000_VF_MAC_FILTER_CLR, NULL);
  1218. spin_unlock_bh(&hw->mbx_lock);
  1219. if (!netdev_uc_empty(netdev)) {
  1220. struct netdev_hw_addr *ha;
  1221. /* Add MAC filters one by one */
  1222. netdev_for_each_uc_addr(ha, netdev) {
  1223. spin_lock_bh(&hw->mbx_lock);
  1224. hw->mac.ops.set_uc_addr(hw, E1000_VF_MAC_FILTER_ADD,
  1225. ha->addr);
  1226. spin_unlock_bh(&hw->mbx_lock);
  1227. udelay(200);
  1228. }
  1229. }
  1230. return 0;
  1231. }
  1232. static void igbvf_set_rx_mode(struct net_device *netdev)
  1233. {
  1234. igbvf_set_multi(netdev);
  1235. igbvf_set_uni(netdev);
  1236. }
  1237. /**
  1238. * igbvf_configure - configure the hardware for Rx and Tx
  1239. * @adapter: private board structure
  1240. **/
  1241. static void igbvf_configure(struct igbvf_adapter *adapter)
  1242. {
  1243. igbvf_set_rx_mode(adapter->netdev);
  1244. igbvf_restore_vlan(adapter);
  1245. igbvf_configure_tx(adapter);
  1246. igbvf_setup_srrctl(adapter);
  1247. igbvf_configure_rx(adapter);
  1248. igbvf_alloc_rx_buffers(adapter->rx_ring,
  1249. igbvf_desc_unused(adapter->rx_ring));
  1250. }
  1251. /* igbvf_reset - bring the hardware into a known good state
  1252. * @adapter: private board structure
  1253. *
  1254. * This function boots the hardware and enables some settings that
  1255. * require a configuration cycle of the hardware - those cannot be
  1256. * set/changed during runtime. After reset the device needs to be
  1257. * properly configured for Rx, Tx etc.
  1258. */
  1259. static void igbvf_reset(struct igbvf_adapter *adapter)
  1260. {
  1261. struct e1000_mac_info *mac = &adapter->hw.mac;
  1262. struct net_device *netdev = adapter->netdev;
  1263. struct e1000_hw *hw = &adapter->hw;
  1264. spin_lock_bh(&hw->mbx_lock);
  1265. /* Allow time for pending master requests to run */
  1266. if (mac->ops.reset_hw(hw))
  1267. dev_err(&adapter->pdev->dev, "PF still resetting\n");
  1268. mac->ops.init_hw(hw);
  1269. spin_unlock_bh(&hw->mbx_lock);
  1270. if (is_valid_ether_addr(adapter->hw.mac.addr)) {
  1271. memcpy(netdev->dev_addr, adapter->hw.mac.addr,
  1272. netdev->addr_len);
  1273. memcpy(netdev->perm_addr, adapter->hw.mac.addr,
  1274. netdev->addr_len);
  1275. }
  1276. adapter->last_reset = jiffies;
  1277. }
  1278. int igbvf_up(struct igbvf_adapter *adapter)
  1279. {
  1280. struct e1000_hw *hw = &adapter->hw;
  1281. /* hardware has been reset, we need to reload some things */
  1282. igbvf_configure(adapter);
  1283. clear_bit(__IGBVF_DOWN, &adapter->state);
  1284. napi_enable(&adapter->rx_ring->napi);
  1285. if (adapter->msix_entries)
  1286. igbvf_configure_msix(adapter);
  1287. /* Clear any pending interrupts. */
  1288. er32(EICR);
  1289. igbvf_irq_enable(adapter);
  1290. /* start the watchdog */
  1291. hw->mac.get_link_status = 1;
  1292. mod_timer(&adapter->watchdog_timer, jiffies + 1);
  1293. return 0;
  1294. }
  1295. void igbvf_down(struct igbvf_adapter *adapter)
  1296. {
  1297. struct net_device *netdev = adapter->netdev;
  1298. struct e1000_hw *hw = &adapter->hw;
  1299. u32 rxdctl, txdctl;
  1300. /* signal that we're down so the interrupt handler does not
  1301. * reschedule our watchdog timer
  1302. */
  1303. set_bit(__IGBVF_DOWN, &adapter->state);
  1304. /* disable receives in the hardware */
  1305. rxdctl = er32(RXDCTL(0));
  1306. ew32(RXDCTL(0), rxdctl & ~E1000_RXDCTL_QUEUE_ENABLE);
  1307. netif_carrier_off(netdev);
  1308. netif_stop_queue(netdev);
  1309. /* disable transmits in the hardware */
  1310. txdctl = er32(TXDCTL(0));
  1311. ew32(TXDCTL(0), txdctl & ~E1000_TXDCTL_QUEUE_ENABLE);
  1312. /* flush both disables and wait for them to finish */
  1313. e1e_flush();
  1314. msleep(10);
  1315. napi_disable(&adapter->rx_ring->napi);
  1316. igbvf_irq_disable(adapter);
  1317. del_timer_sync(&adapter->watchdog_timer);
  1318. /* record the stats before reset*/
  1319. igbvf_update_stats(adapter);
  1320. adapter->link_speed = 0;
  1321. adapter->link_duplex = 0;
  1322. igbvf_reset(adapter);
  1323. igbvf_clean_tx_ring(adapter->tx_ring);
  1324. igbvf_clean_rx_ring(adapter->rx_ring);
  1325. }
  1326. void igbvf_reinit_locked(struct igbvf_adapter *adapter)
  1327. {
  1328. might_sleep();
  1329. while (test_and_set_bit(__IGBVF_RESETTING, &adapter->state))
  1330. usleep_range(1000, 2000);
  1331. igbvf_down(adapter);
  1332. igbvf_up(adapter);
  1333. clear_bit(__IGBVF_RESETTING, &adapter->state);
  1334. }
  1335. /**
  1336. * igbvf_sw_init - Initialize general software structures (struct igbvf_adapter)
  1337. * @adapter: board private structure to initialize
  1338. *
  1339. * igbvf_sw_init initializes the Adapter private data structure.
  1340. * Fields are initialized based on PCI device information and
  1341. * OS network device settings (MTU size).
  1342. **/
  1343. static int igbvf_sw_init(struct igbvf_adapter *adapter)
  1344. {
  1345. struct net_device *netdev = adapter->netdev;
  1346. s32 rc;
  1347. adapter->rx_buffer_len = ETH_FRAME_LEN + VLAN_HLEN + ETH_FCS_LEN;
  1348. adapter->rx_ps_hdr_size = 0;
  1349. adapter->max_frame_size = netdev->mtu + ETH_HLEN + ETH_FCS_LEN;
  1350. adapter->min_frame_size = ETH_ZLEN + ETH_FCS_LEN;
  1351. adapter->tx_int_delay = 8;
  1352. adapter->tx_abs_int_delay = 32;
  1353. adapter->rx_int_delay = 0;
  1354. adapter->rx_abs_int_delay = 8;
  1355. adapter->requested_itr = 3;
  1356. adapter->current_itr = IGBVF_START_ITR;
  1357. /* Set various function pointers */
  1358. adapter->ei->init_ops(&adapter->hw);
  1359. rc = adapter->hw.mac.ops.init_params(&adapter->hw);
  1360. if (rc)
  1361. return rc;
  1362. rc = adapter->hw.mbx.ops.init_params(&adapter->hw);
  1363. if (rc)
  1364. return rc;
  1365. igbvf_set_interrupt_capability(adapter);
  1366. if (igbvf_alloc_queues(adapter))
  1367. return -ENOMEM;
  1368. spin_lock_init(&adapter->tx_queue_lock);
  1369. /* Explicitly disable IRQ since the NIC can be in any state. */
  1370. igbvf_irq_disable(adapter);
  1371. spin_lock_init(&adapter->stats_lock);
  1372. spin_lock_init(&adapter->hw.mbx_lock);
  1373. set_bit(__IGBVF_DOWN, &adapter->state);
  1374. return 0;
  1375. }
  1376. static void igbvf_initialize_last_counter_stats(struct igbvf_adapter *adapter)
  1377. {
  1378. struct e1000_hw *hw = &adapter->hw;
  1379. adapter->stats.last_gprc = er32(VFGPRC);
  1380. adapter->stats.last_gorc = er32(VFGORC);
  1381. adapter->stats.last_gptc = er32(VFGPTC);
  1382. adapter->stats.last_gotc = er32(VFGOTC);
  1383. adapter->stats.last_mprc = er32(VFMPRC);
  1384. adapter->stats.last_gotlbc = er32(VFGOTLBC);
  1385. adapter->stats.last_gptlbc = er32(VFGPTLBC);
  1386. adapter->stats.last_gorlbc = er32(VFGORLBC);
  1387. adapter->stats.last_gprlbc = er32(VFGPRLBC);
  1388. adapter->stats.base_gprc = er32(VFGPRC);
  1389. adapter->stats.base_gorc = er32(VFGORC);
  1390. adapter->stats.base_gptc = er32(VFGPTC);
  1391. adapter->stats.base_gotc = er32(VFGOTC);
  1392. adapter->stats.base_mprc = er32(VFMPRC);
  1393. adapter->stats.base_gotlbc = er32(VFGOTLBC);
  1394. adapter->stats.base_gptlbc = er32(VFGPTLBC);
  1395. adapter->stats.base_gorlbc = er32(VFGORLBC);
  1396. adapter->stats.base_gprlbc = er32(VFGPRLBC);
  1397. }
  1398. /**
  1399. * igbvf_open - Called when a network interface is made active
  1400. * @netdev: network interface device structure
  1401. *
  1402. * Returns 0 on success, negative value on failure
  1403. *
  1404. * The open entry point is called when a network interface is made
  1405. * active by the system (IFF_UP). At this point all resources needed
  1406. * for transmit and receive operations are allocated, the interrupt
  1407. * handler is registered with the OS, the watchdog timer is started,
  1408. * and the stack is notified that the interface is ready.
  1409. **/
  1410. static int igbvf_open(struct net_device *netdev)
  1411. {
  1412. struct igbvf_adapter *adapter = netdev_priv(netdev);
  1413. struct e1000_hw *hw = &adapter->hw;
  1414. int err;
  1415. /* disallow open during test */
  1416. if (test_bit(__IGBVF_TESTING, &adapter->state))
  1417. return -EBUSY;
  1418. /* allocate transmit descriptors */
  1419. err = igbvf_setup_tx_resources(adapter, adapter->tx_ring);
  1420. if (err)
  1421. goto err_setup_tx;
  1422. /* allocate receive descriptors */
  1423. err = igbvf_setup_rx_resources(adapter, adapter->rx_ring);
  1424. if (err)
  1425. goto err_setup_rx;
  1426. /* before we allocate an interrupt, we must be ready to handle it.
  1427. * Setting DEBUG_SHIRQ in the kernel makes it fire an interrupt
  1428. * as soon as we call pci_request_irq, so we have to setup our
  1429. * clean_rx handler before we do so.
  1430. */
  1431. igbvf_configure(adapter);
  1432. err = igbvf_request_irq(adapter);
  1433. if (err)
  1434. goto err_req_irq;
  1435. /* From here on the code is the same as igbvf_up() */
  1436. clear_bit(__IGBVF_DOWN, &adapter->state);
  1437. napi_enable(&adapter->rx_ring->napi);
  1438. /* clear any pending interrupts */
  1439. er32(EICR);
  1440. igbvf_irq_enable(adapter);
  1441. /* start the watchdog */
  1442. hw->mac.get_link_status = 1;
  1443. mod_timer(&adapter->watchdog_timer, jiffies + 1);
  1444. return 0;
  1445. err_req_irq:
  1446. igbvf_free_rx_resources(adapter->rx_ring);
  1447. err_setup_rx:
  1448. igbvf_free_tx_resources(adapter->tx_ring);
  1449. err_setup_tx:
  1450. igbvf_reset(adapter);
  1451. return err;
  1452. }
  1453. /**
  1454. * igbvf_close - Disables a network interface
  1455. * @netdev: network interface device structure
  1456. *
  1457. * Returns 0, this is not allowed to fail
  1458. *
  1459. * The close entry point is called when an interface is de-activated
  1460. * by the OS. The hardware is still under the drivers control, but
  1461. * needs to be disabled. A global MAC reset is issued to stop the
  1462. * hardware, and all transmit and receive resources are freed.
  1463. **/
  1464. static int igbvf_close(struct net_device *netdev)
  1465. {
  1466. struct igbvf_adapter *adapter = netdev_priv(netdev);
  1467. WARN_ON(test_bit(__IGBVF_RESETTING, &adapter->state));
  1468. igbvf_down(adapter);
  1469. igbvf_free_irq(adapter);
  1470. igbvf_free_tx_resources(adapter->tx_ring);
  1471. igbvf_free_rx_resources(adapter->rx_ring);
  1472. return 0;
  1473. }
  1474. /**
  1475. * igbvf_set_mac - Change the Ethernet Address of the NIC
  1476. * @netdev: network interface device structure
  1477. * @p: pointer to an address structure
  1478. *
  1479. * Returns 0 on success, negative on failure
  1480. **/
  1481. static int igbvf_set_mac(struct net_device *netdev, void *p)
  1482. {
  1483. struct igbvf_adapter *adapter = netdev_priv(netdev);
  1484. struct e1000_hw *hw = &adapter->hw;
  1485. struct sockaddr *addr = p;
  1486. if (!is_valid_ether_addr(addr->sa_data))
  1487. return -EADDRNOTAVAIL;
  1488. memcpy(hw->mac.addr, addr->sa_data, netdev->addr_len);
  1489. spin_lock_bh(&hw->mbx_lock);
  1490. hw->mac.ops.rar_set(hw, hw->mac.addr, 0);
  1491. spin_unlock_bh(&hw->mbx_lock);
  1492. if (!ether_addr_equal(addr->sa_data, hw->mac.addr))
  1493. return -EADDRNOTAVAIL;
  1494. memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
  1495. return 0;
  1496. }
  1497. #define UPDATE_VF_COUNTER(reg, name) \
  1498. { \
  1499. u32 current_counter = er32(reg); \
  1500. if (current_counter < adapter->stats.last_##name) \
  1501. adapter->stats.name += 0x100000000LL; \
  1502. adapter->stats.last_##name = current_counter; \
  1503. adapter->stats.name &= 0xFFFFFFFF00000000LL; \
  1504. adapter->stats.name |= current_counter; \
  1505. }
  1506. /**
  1507. * igbvf_update_stats - Update the board statistics counters
  1508. * @adapter: board private structure
  1509. **/
  1510. void igbvf_update_stats(struct igbvf_adapter *adapter)
  1511. {
  1512. struct e1000_hw *hw = &adapter->hw;
  1513. struct pci_dev *pdev = adapter->pdev;
  1514. /* Prevent stats update while adapter is being reset, link is down
  1515. * or if the pci connection is down.
  1516. */
  1517. if (adapter->link_speed == 0)
  1518. return;
  1519. if (test_bit(__IGBVF_RESETTING, &adapter->state))
  1520. return;
  1521. if (pci_channel_offline(pdev))
  1522. return;
  1523. UPDATE_VF_COUNTER(VFGPRC, gprc);
  1524. UPDATE_VF_COUNTER(VFGORC, gorc);
  1525. UPDATE_VF_COUNTER(VFGPTC, gptc);
  1526. UPDATE_VF_COUNTER(VFGOTC, gotc);
  1527. UPDATE_VF_COUNTER(VFMPRC, mprc);
  1528. UPDATE_VF_COUNTER(VFGOTLBC, gotlbc);
  1529. UPDATE_VF_COUNTER(VFGPTLBC, gptlbc);
  1530. UPDATE_VF_COUNTER(VFGORLBC, gorlbc);
  1531. UPDATE_VF_COUNTER(VFGPRLBC, gprlbc);
  1532. /* Fill out the OS statistics structure */
  1533. adapter->netdev->stats.multicast = adapter->stats.mprc;
  1534. }
  1535. static void igbvf_print_link_info(struct igbvf_adapter *adapter)
  1536. {
  1537. dev_info(&adapter->pdev->dev, "Link is Up %d Mbps %s Duplex\n",
  1538. adapter->link_speed,
  1539. adapter->link_duplex == FULL_DUPLEX ? "Full" : "Half");
  1540. }
  1541. static bool igbvf_has_link(struct igbvf_adapter *adapter)
  1542. {
  1543. struct e1000_hw *hw = &adapter->hw;
  1544. s32 ret_val = E1000_SUCCESS;
  1545. bool link_active;
  1546. /* If interface is down, stay link down */
  1547. if (test_bit(__IGBVF_DOWN, &adapter->state))
  1548. return false;
  1549. spin_lock_bh(&hw->mbx_lock);
  1550. ret_val = hw->mac.ops.check_for_link(hw);
  1551. spin_unlock_bh(&hw->mbx_lock);
  1552. link_active = !hw->mac.get_link_status;
  1553. /* if check for link returns error we will need to reset */
  1554. if (ret_val && time_after(jiffies, adapter->last_reset + (10 * HZ)))
  1555. schedule_work(&adapter->reset_task);
  1556. return link_active;
  1557. }
  1558. /**
  1559. * igbvf_watchdog - Timer Call-back
  1560. * @data: pointer to adapter cast into an unsigned long
  1561. **/
  1562. static void igbvf_watchdog(struct timer_list *t)
  1563. {
  1564. struct igbvf_adapter *adapter = from_timer(adapter, t, watchdog_timer);
  1565. /* Do the rest outside of interrupt context */
  1566. schedule_work(&adapter->watchdog_task);
  1567. }
  1568. static void igbvf_watchdog_task(struct work_struct *work)
  1569. {
  1570. struct igbvf_adapter *adapter = container_of(work,
  1571. struct igbvf_adapter,
  1572. watchdog_task);
  1573. struct net_device *netdev = adapter->netdev;
  1574. struct e1000_mac_info *mac = &adapter->hw.mac;
  1575. struct igbvf_ring *tx_ring = adapter->tx_ring;
  1576. struct e1000_hw *hw = &adapter->hw;
  1577. u32 link;
  1578. int tx_pending = 0;
  1579. link = igbvf_has_link(adapter);
  1580. if (link) {
  1581. if (!netif_carrier_ok(netdev)) {
  1582. mac->ops.get_link_up_info(&adapter->hw,
  1583. &adapter->link_speed,
  1584. &adapter->link_duplex);
  1585. igbvf_print_link_info(adapter);
  1586. netif_carrier_on(netdev);
  1587. netif_wake_queue(netdev);
  1588. }
  1589. } else {
  1590. if (netif_carrier_ok(netdev)) {
  1591. adapter->link_speed = 0;
  1592. adapter->link_duplex = 0;
  1593. dev_info(&adapter->pdev->dev, "Link is Down\n");
  1594. netif_carrier_off(netdev);
  1595. netif_stop_queue(netdev);
  1596. }
  1597. }
  1598. if (netif_carrier_ok(netdev)) {
  1599. igbvf_update_stats(adapter);
  1600. } else {
  1601. tx_pending = (igbvf_desc_unused(tx_ring) + 1 <
  1602. tx_ring->count);
  1603. if (tx_pending) {
  1604. /* We've lost link, so the controller stops DMA,
  1605. * but we've got queued Tx work that's never going
  1606. * to get done, so reset controller to flush Tx.
  1607. * (Do the reset outside of interrupt context).
  1608. */
  1609. adapter->tx_timeout_count++;
  1610. schedule_work(&adapter->reset_task);
  1611. }
  1612. }
  1613. /* Cause software interrupt to ensure Rx ring is cleaned */
  1614. ew32(EICS, adapter->rx_ring->eims_value);
  1615. /* Reset the timer */
  1616. if (!test_bit(__IGBVF_DOWN, &adapter->state))
  1617. mod_timer(&adapter->watchdog_timer,
  1618. round_jiffies(jiffies + (2 * HZ)));
  1619. }
  1620. #define IGBVF_TX_FLAGS_CSUM 0x00000001
  1621. #define IGBVF_TX_FLAGS_VLAN 0x00000002
  1622. #define IGBVF_TX_FLAGS_TSO 0x00000004
  1623. #define IGBVF_TX_FLAGS_IPV4 0x00000008
  1624. #define IGBVF_TX_FLAGS_VLAN_MASK 0xffff0000
  1625. #define IGBVF_TX_FLAGS_VLAN_SHIFT 16
  1626. static void igbvf_tx_ctxtdesc(struct igbvf_ring *tx_ring, u32 vlan_macip_lens,
  1627. u32 type_tucmd, u32 mss_l4len_idx)
  1628. {
  1629. struct e1000_adv_tx_context_desc *context_desc;
  1630. struct igbvf_buffer *buffer_info;
  1631. u16 i = tx_ring->next_to_use;
  1632. context_desc = IGBVF_TX_CTXTDESC_ADV(*tx_ring, i);
  1633. buffer_info = &tx_ring->buffer_info[i];
  1634. i++;
  1635. tx_ring->next_to_use = (i < tx_ring->count) ? i : 0;
  1636. /* set bits to identify this as an advanced context descriptor */
  1637. type_tucmd |= E1000_TXD_CMD_DEXT | E1000_ADVTXD_DTYP_CTXT;
  1638. context_desc->vlan_macip_lens = cpu_to_le32(vlan_macip_lens);
  1639. context_desc->seqnum_seed = 0;
  1640. context_desc->type_tucmd_mlhl = cpu_to_le32(type_tucmd);
  1641. context_desc->mss_l4len_idx = cpu_to_le32(mss_l4len_idx);
  1642. buffer_info->time_stamp = jiffies;
  1643. buffer_info->dma = 0;
  1644. }
  1645. static int igbvf_tso(struct igbvf_ring *tx_ring,
  1646. struct sk_buff *skb, u32 tx_flags, u8 *hdr_len)
  1647. {
  1648. u32 vlan_macip_lens, type_tucmd, mss_l4len_idx;
  1649. union {
  1650. struct iphdr *v4;
  1651. struct ipv6hdr *v6;
  1652. unsigned char *hdr;
  1653. } ip;
  1654. union {
  1655. struct tcphdr *tcp;
  1656. unsigned char *hdr;
  1657. } l4;
  1658. u32 paylen, l4_offset;
  1659. int err;
  1660. if (skb->ip_summed != CHECKSUM_PARTIAL)
  1661. return 0;
  1662. if (!skb_is_gso(skb))
  1663. return 0;
  1664. err = skb_cow_head(skb, 0);
  1665. if (err < 0)
  1666. return err;
  1667. ip.hdr = skb_network_header(skb);
  1668. l4.hdr = skb_checksum_start(skb);
  1669. /* ADV DTYP TUCMD MKRLOC/ISCSIHEDLEN */
  1670. type_tucmd = E1000_ADVTXD_TUCMD_L4T_TCP;
  1671. /* initialize outer IP header fields */
  1672. if (ip.v4->version == 4) {
  1673. unsigned char *csum_start = skb_checksum_start(skb);
  1674. unsigned char *trans_start = ip.hdr + (ip.v4->ihl * 4);
  1675. /* IP header will have to cancel out any data that
  1676. * is not a part of the outer IP header
  1677. */
  1678. ip.v4->check = csum_fold(csum_partial(trans_start,
  1679. csum_start - trans_start,
  1680. 0));
  1681. type_tucmd |= E1000_ADVTXD_TUCMD_IPV4;
  1682. ip.v4->tot_len = 0;
  1683. } else {
  1684. ip.v6->payload_len = 0;
  1685. }
  1686. /* determine offset of inner transport header */
  1687. l4_offset = l4.hdr - skb->data;
  1688. /* compute length of segmentation header */
  1689. *hdr_len = (l4.tcp->doff * 4) + l4_offset;
  1690. /* remove payload length from inner checksum */
  1691. paylen = skb->len - l4_offset;
  1692. csum_replace_by_diff(&l4.tcp->check, htonl(paylen));
  1693. /* MSS L4LEN IDX */
  1694. mss_l4len_idx = (*hdr_len - l4_offset) << E1000_ADVTXD_L4LEN_SHIFT;
  1695. mss_l4len_idx |= skb_shinfo(skb)->gso_size << E1000_ADVTXD_MSS_SHIFT;
  1696. /* VLAN MACLEN IPLEN */
  1697. vlan_macip_lens = l4.hdr - ip.hdr;
  1698. vlan_macip_lens |= (ip.hdr - skb->data) << E1000_ADVTXD_MACLEN_SHIFT;
  1699. vlan_macip_lens |= tx_flags & IGBVF_TX_FLAGS_VLAN_MASK;
  1700. igbvf_tx_ctxtdesc(tx_ring, vlan_macip_lens, type_tucmd, mss_l4len_idx);
  1701. return 1;
  1702. }
  1703. static inline bool igbvf_ipv6_csum_is_sctp(struct sk_buff *skb)
  1704. {
  1705. unsigned int offset = 0;
  1706. ipv6_find_hdr(skb, &offset, IPPROTO_SCTP, NULL, NULL);
  1707. return offset == skb_checksum_start_offset(skb);
  1708. }
  1709. static bool igbvf_tx_csum(struct igbvf_ring *tx_ring, struct sk_buff *skb,
  1710. u32 tx_flags, __be16 protocol)
  1711. {
  1712. u32 vlan_macip_lens = 0;
  1713. u32 type_tucmd = 0;
  1714. if (skb->ip_summed != CHECKSUM_PARTIAL) {
  1715. csum_failed:
  1716. if (!(tx_flags & IGBVF_TX_FLAGS_VLAN))
  1717. return false;
  1718. goto no_csum;
  1719. }
  1720. switch (skb->csum_offset) {
  1721. case offsetof(struct tcphdr, check):
  1722. type_tucmd = E1000_ADVTXD_TUCMD_L4T_TCP;
  1723. /* fall through */
  1724. case offsetof(struct udphdr, check):
  1725. break;
  1726. case offsetof(struct sctphdr, checksum):
  1727. /* validate that this is actually an SCTP request */
  1728. if (((protocol == htons(ETH_P_IP)) &&
  1729. (ip_hdr(skb)->protocol == IPPROTO_SCTP)) ||
  1730. ((protocol == htons(ETH_P_IPV6)) &&
  1731. igbvf_ipv6_csum_is_sctp(skb))) {
  1732. type_tucmd = E1000_ADVTXD_TUCMD_L4T_SCTP;
  1733. break;
  1734. }
  1735. /* fall through */
  1736. default:
  1737. skb_checksum_help(skb);
  1738. goto csum_failed;
  1739. }
  1740. vlan_macip_lens = skb_checksum_start_offset(skb) -
  1741. skb_network_offset(skb);
  1742. no_csum:
  1743. vlan_macip_lens |= skb_network_offset(skb) << E1000_ADVTXD_MACLEN_SHIFT;
  1744. vlan_macip_lens |= tx_flags & IGBVF_TX_FLAGS_VLAN_MASK;
  1745. igbvf_tx_ctxtdesc(tx_ring, vlan_macip_lens, type_tucmd, 0);
  1746. return true;
  1747. }
  1748. static int igbvf_maybe_stop_tx(struct net_device *netdev, int size)
  1749. {
  1750. struct igbvf_adapter *adapter = netdev_priv(netdev);
  1751. /* there is enough descriptors then we don't need to worry */
  1752. if (igbvf_desc_unused(adapter->tx_ring) >= size)
  1753. return 0;
  1754. netif_stop_queue(netdev);
  1755. /* Herbert's original patch had:
  1756. * smp_mb__after_netif_stop_queue();
  1757. * but since that doesn't exist yet, just open code it.
  1758. */
  1759. smp_mb();
  1760. /* We need to check again just in case room has been made available */
  1761. if (igbvf_desc_unused(adapter->tx_ring) < size)
  1762. return -EBUSY;
  1763. netif_wake_queue(netdev);
  1764. ++adapter->restart_queue;
  1765. return 0;
  1766. }
  1767. #define IGBVF_MAX_TXD_PWR 16
  1768. #define IGBVF_MAX_DATA_PER_TXD (1u << IGBVF_MAX_TXD_PWR)
  1769. static inline int igbvf_tx_map_adv(struct igbvf_adapter *adapter,
  1770. struct igbvf_ring *tx_ring,
  1771. struct sk_buff *skb)
  1772. {
  1773. struct igbvf_buffer *buffer_info;
  1774. struct pci_dev *pdev = adapter->pdev;
  1775. unsigned int len = skb_headlen(skb);
  1776. unsigned int count = 0, i;
  1777. unsigned int f;
  1778. i = tx_ring->next_to_use;
  1779. buffer_info = &tx_ring->buffer_info[i];
  1780. BUG_ON(len >= IGBVF_MAX_DATA_PER_TXD);
  1781. buffer_info->length = len;
  1782. /* set time_stamp *before* dma to help avoid a possible race */
  1783. buffer_info->time_stamp = jiffies;
  1784. buffer_info->mapped_as_page = false;
  1785. buffer_info->dma = dma_map_single(&pdev->dev, skb->data, len,
  1786. DMA_TO_DEVICE);
  1787. if (dma_mapping_error(&pdev->dev, buffer_info->dma))
  1788. goto dma_error;
  1789. for (f = 0; f < skb_shinfo(skb)->nr_frags; f++) {
  1790. const struct skb_frag_struct *frag;
  1791. count++;
  1792. i++;
  1793. if (i == tx_ring->count)
  1794. i = 0;
  1795. frag = &skb_shinfo(skb)->frags[f];
  1796. len = skb_frag_size(frag);
  1797. buffer_info = &tx_ring->buffer_info[i];
  1798. BUG_ON(len >= IGBVF_MAX_DATA_PER_TXD);
  1799. buffer_info->length = len;
  1800. buffer_info->time_stamp = jiffies;
  1801. buffer_info->mapped_as_page = true;
  1802. buffer_info->dma = skb_frag_dma_map(&pdev->dev, frag, 0, len,
  1803. DMA_TO_DEVICE);
  1804. if (dma_mapping_error(&pdev->dev, buffer_info->dma))
  1805. goto dma_error;
  1806. }
  1807. tx_ring->buffer_info[i].skb = skb;
  1808. return ++count;
  1809. dma_error:
  1810. dev_err(&pdev->dev, "TX DMA map failed\n");
  1811. /* clear timestamp and dma mappings for failed buffer_info mapping */
  1812. buffer_info->dma = 0;
  1813. buffer_info->time_stamp = 0;
  1814. buffer_info->length = 0;
  1815. buffer_info->mapped_as_page = false;
  1816. if (count)
  1817. count--;
  1818. /* clear timestamp and dma mappings for remaining portion of packet */
  1819. while (count--) {
  1820. if (i == 0)
  1821. i += tx_ring->count;
  1822. i--;
  1823. buffer_info = &tx_ring->buffer_info[i];
  1824. igbvf_put_txbuf(adapter, buffer_info);
  1825. }
  1826. return 0;
  1827. }
  1828. static inline void igbvf_tx_queue_adv(struct igbvf_adapter *adapter,
  1829. struct igbvf_ring *tx_ring,
  1830. int tx_flags, int count,
  1831. unsigned int first, u32 paylen,
  1832. u8 hdr_len)
  1833. {
  1834. union e1000_adv_tx_desc *tx_desc = NULL;
  1835. struct igbvf_buffer *buffer_info;
  1836. u32 olinfo_status = 0, cmd_type_len;
  1837. unsigned int i;
  1838. cmd_type_len = (E1000_ADVTXD_DTYP_DATA | E1000_ADVTXD_DCMD_IFCS |
  1839. E1000_ADVTXD_DCMD_DEXT);
  1840. if (tx_flags & IGBVF_TX_FLAGS_VLAN)
  1841. cmd_type_len |= E1000_ADVTXD_DCMD_VLE;
  1842. if (tx_flags & IGBVF_TX_FLAGS_TSO) {
  1843. cmd_type_len |= E1000_ADVTXD_DCMD_TSE;
  1844. /* insert tcp checksum */
  1845. olinfo_status |= E1000_TXD_POPTS_TXSM << 8;
  1846. /* insert ip checksum */
  1847. if (tx_flags & IGBVF_TX_FLAGS_IPV4)
  1848. olinfo_status |= E1000_TXD_POPTS_IXSM << 8;
  1849. } else if (tx_flags & IGBVF_TX_FLAGS_CSUM) {
  1850. olinfo_status |= E1000_TXD_POPTS_TXSM << 8;
  1851. }
  1852. olinfo_status |= ((paylen - hdr_len) << E1000_ADVTXD_PAYLEN_SHIFT);
  1853. i = tx_ring->next_to_use;
  1854. while (count--) {
  1855. buffer_info = &tx_ring->buffer_info[i];
  1856. tx_desc = IGBVF_TX_DESC_ADV(*tx_ring, i);
  1857. tx_desc->read.buffer_addr = cpu_to_le64(buffer_info->dma);
  1858. tx_desc->read.cmd_type_len =
  1859. cpu_to_le32(cmd_type_len | buffer_info->length);
  1860. tx_desc->read.olinfo_status = cpu_to_le32(olinfo_status);
  1861. i++;
  1862. if (i == tx_ring->count)
  1863. i = 0;
  1864. }
  1865. tx_desc->read.cmd_type_len |= cpu_to_le32(adapter->txd_cmd);
  1866. /* Force memory writes to complete before letting h/w
  1867. * know there are new descriptors to fetch. (Only
  1868. * applicable for weak-ordered memory model archs,
  1869. * such as IA-64).
  1870. */
  1871. wmb();
  1872. tx_ring->buffer_info[first].next_to_watch = tx_desc;
  1873. tx_ring->next_to_use = i;
  1874. writel(i, adapter->hw.hw_addr + tx_ring->tail);
  1875. /* we need this if more than one processor can write to our tail
  1876. * at a time, it synchronizes IO on IA64/Altix systems
  1877. */
  1878. mmiowb();
  1879. }
  1880. static netdev_tx_t igbvf_xmit_frame_ring_adv(struct sk_buff *skb,
  1881. struct net_device *netdev,
  1882. struct igbvf_ring *tx_ring)
  1883. {
  1884. struct igbvf_adapter *adapter = netdev_priv(netdev);
  1885. unsigned int first, tx_flags = 0;
  1886. u8 hdr_len = 0;
  1887. int count = 0;
  1888. int tso = 0;
  1889. __be16 protocol = vlan_get_protocol(skb);
  1890. if (test_bit(__IGBVF_DOWN, &adapter->state)) {
  1891. dev_kfree_skb_any(skb);
  1892. return NETDEV_TX_OK;
  1893. }
  1894. if (skb->len <= 0) {
  1895. dev_kfree_skb_any(skb);
  1896. return NETDEV_TX_OK;
  1897. }
  1898. /* need: count + 4 desc gap to keep tail from touching
  1899. * + 2 desc gap to keep tail from touching head,
  1900. * + 1 desc for skb->data,
  1901. * + 1 desc for context descriptor,
  1902. * head, otherwise try next time
  1903. */
  1904. if (igbvf_maybe_stop_tx(netdev, skb_shinfo(skb)->nr_frags + 4)) {
  1905. /* this is a hard error */
  1906. return NETDEV_TX_BUSY;
  1907. }
  1908. if (skb_vlan_tag_present(skb)) {
  1909. tx_flags |= IGBVF_TX_FLAGS_VLAN;
  1910. tx_flags |= (skb_vlan_tag_get(skb) <<
  1911. IGBVF_TX_FLAGS_VLAN_SHIFT);
  1912. }
  1913. if (protocol == htons(ETH_P_IP))
  1914. tx_flags |= IGBVF_TX_FLAGS_IPV4;
  1915. first = tx_ring->next_to_use;
  1916. tso = igbvf_tso(tx_ring, skb, tx_flags, &hdr_len);
  1917. if (unlikely(tso < 0)) {
  1918. dev_kfree_skb_any(skb);
  1919. return NETDEV_TX_OK;
  1920. }
  1921. if (tso)
  1922. tx_flags |= IGBVF_TX_FLAGS_TSO;
  1923. else if (igbvf_tx_csum(tx_ring, skb, tx_flags, protocol) &&
  1924. (skb->ip_summed == CHECKSUM_PARTIAL))
  1925. tx_flags |= IGBVF_TX_FLAGS_CSUM;
  1926. /* count reflects descriptors mapped, if 0 then mapping error
  1927. * has occurred and we need to rewind the descriptor queue
  1928. */
  1929. count = igbvf_tx_map_adv(adapter, tx_ring, skb);
  1930. if (count) {
  1931. igbvf_tx_queue_adv(adapter, tx_ring, tx_flags, count,
  1932. first, skb->len, hdr_len);
  1933. /* Make sure there is space in the ring for the next send. */
  1934. igbvf_maybe_stop_tx(netdev, MAX_SKB_FRAGS + 4);
  1935. } else {
  1936. dev_kfree_skb_any(skb);
  1937. tx_ring->buffer_info[first].time_stamp = 0;
  1938. tx_ring->next_to_use = first;
  1939. }
  1940. return NETDEV_TX_OK;
  1941. }
  1942. static netdev_tx_t igbvf_xmit_frame(struct sk_buff *skb,
  1943. struct net_device *netdev)
  1944. {
  1945. struct igbvf_adapter *adapter = netdev_priv(netdev);
  1946. struct igbvf_ring *tx_ring;
  1947. if (test_bit(__IGBVF_DOWN, &adapter->state)) {
  1948. dev_kfree_skb_any(skb);
  1949. return NETDEV_TX_OK;
  1950. }
  1951. tx_ring = &adapter->tx_ring[0];
  1952. return igbvf_xmit_frame_ring_adv(skb, netdev, tx_ring);
  1953. }
  1954. /**
  1955. * igbvf_tx_timeout - Respond to a Tx Hang
  1956. * @netdev: network interface device structure
  1957. **/
  1958. static void igbvf_tx_timeout(struct net_device *netdev)
  1959. {
  1960. struct igbvf_adapter *adapter = netdev_priv(netdev);
  1961. /* Do the reset outside of interrupt context */
  1962. adapter->tx_timeout_count++;
  1963. schedule_work(&adapter->reset_task);
  1964. }
  1965. static void igbvf_reset_task(struct work_struct *work)
  1966. {
  1967. struct igbvf_adapter *adapter;
  1968. adapter = container_of(work, struct igbvf_adapter, reset_task);
  1969. igbvf_reinit_locked(adapter);
  1970. }
  1971. /**
  1972. * igbvf_change_mtu - Change the Maximum Transfer Unit
  1973. * @netdev: network interface device structure
  1974. * @new_mtu: new value for maximum frame size
  1975. *
  1976. * Returns 0 on success, negative on failure
  1977. **/
  1978. static int igbvf_change_mtu(struct net_device *netdev, int new_mtu)
  1979. {
  1980. struct igbvf_adapter *adapter = netdev_priv(netdev);
  1981. int max_frame = new_mtu + ETH_HLEN + ETH_FCS_LEN;
  1982. while (test_and_set_bit(__IGBVF_RESETTING, &adapter->state))
  1983. usleep_range(1000, 2000);
  1984. /* igbvf_down has a dependency on max_frame_size */
  1985. adapter->max_frame_size = max_frame;
  1986. if (netif_running(netdev))
  1987. igbvf_down(adapter);
  1988. /* NOTE: netdev_alloc_skb reserves 16 bytes, and typically NET_IP_ALIGN
  1989. * means we reserve 2 more, this pushes us to allocate from the next
  1990. * larger slab size.
  1991. * i.e. RXBUFFER_2048 --> size-4096 slab
  1992. * However with the new *_jumbo_rx* routines, jumbo receives will use
  1993. * fragmented skbs
  1994. */
  1995. if (max_frame <= 1024)
  1996. adapter->rx_buffer_len = 1024;
  1997. else if (max_frame <= 2048)
  1998. adapter->rx_buffer_len = 2048;
  1999. else
  2000. #if (PAGE_SIZE / 2) > 16384
  2001. adapter->rx_buffer_len = 16384;
  2002. #else
  2003. adapter->rx_buffer_len = PAGE_SIZE / 2;
  2004. #endif
  2005. /* adjust allocation if LPE protects us, and we aren't using SBP */
  2006. if ((max_frame == ETH_FRAME_LEN + ETH_FCS_LEN) ||
  2007. (max_frame == ETH_FRAME_LEN + VLAN_HLEN + ETH_FCS_LEN))
  2008. adapter->rx_buffer_len = ETH_FRAME_LEN + VLAN_HLEN +
  2009. ETH_FCS_LEN;
  2010. dev_info(&adapter->pdev->dev, "changing MTU from %d to %d\n",
  2011. netdev->mtu, new_mtu);
  2012. netdev->mtu = new_mtu;
  2013. if (netif_running(netdev))
  2014. igbvf_up(adapter);
  2015. else
  2016. igbvf_reset(adapter);
  2017. clear_bit(__IGBVF_RESETTING, &adapter->state);
  2018. return 0;
  2019. }
  2020. static int igbvf_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
  2021. {
  2022. switch (cmd) {
  2023. default:
  2024. return -EOPNOTSUPP;
  2025. }
  2026. }
  2027. static int igbvf_suspend(struct pci_dev *pdev, pm_message_t state)
  2028. {
  2029. struct net_device *netdev = pci_get_drvdata(pdev);
  2030. struct igbvf_adapter *adapter = netdev_priv(netdev);
  2031. #ifdef CONFIG_PM
  2032. int retval = 0;
  2033. #endif
  2034. netif_device_detach(netdev);
  2035. if (netif_running(netdev)) {
  2036. WARN_ON(test_bit(__IGBVF_RESETTING, &adapter->state));
  2037. igbvf_down(adapter);
  2038. igbvf_free_irq(adapter);
  2039. }
  2040. #ifdef CONFIG_PM
  2041. retval = pci_save_state(pdev);
  2042. if (retval)
  2043. return retval;
  2044. #endif
  2045. pci_disable_device(pdev);
  2046. return 0;
  2047. }
  2048. #ifdef CONFIG_PM
  2049. static int igbvf_resume(struct pci_dev *pdev)
  2050. {
  2051. struct net_device *netdev = pci_get_drvdata(pdev);
  2052. struct igbvf_adapter *adapter = netdev_priv(netdev);
  2053. u32 err;
  2054. pci_restore_state(pdev);
  2055. err = pci_enable_device_mem(pdev);
  2056. if (err) {
  2057. dev_err(&pdev->dev, "Cannot enable PCI device from suspend\n");
  2058. return err;
  2059. }
  2060. pci_set_master(pdev);
  2061. if (netif_running(netdev)) {
  2062. err = igbvf_request_irq(adapter);
  2063. if (err)
  2064. return err;
  2065. }
  2066. igbvf_reset(adapter);
  2067. if (netif_running(netdev))
  2068. igbvf_up(adapter);
  2069. netif_device_attach(netdev);
  2070. return 0;
  2071. }
  2072. #endif
  2073. static void igbvf_shutdown(struct pci_dev *pdev)
  2074. {
  2075. igbvf_suspend(pdev, PMSG_SUSPEND);
  2076. }
  2077. #ifdef CONFIG_NET_POLL_CONTROLLER
  2078. /* Polling 'interrupt' - used by things like netconsole to send skbs
  2079. * without having to re-enable interrupts. It's not called while
  2080. * the interrupt routine is executing.
  2081. */
  2082. static void igbvf_netpoll(struct net_device *netdev)
  2083. {
  2084. struct igbvf_adapter *adapter = netdev_priv(netdev);
  2085. disable_irq(adapter->pdev->irq);
  2086. igbvf_clean_tx_irq(adapter->tx_ring);
  2087. enable_irq(adapter->pdev->irq);
  2088. }
  2089. #endif
  2090. /**
  2091. * igbvf_io_error_detected - called when PCI error is detected
  2092. * @pdev: Pointer to PCI device
  2093. * @state: The current pci connection state
  2094. *
  2095. * This function is called after a PCI bus error affecting
  2096. * this device has been detected.
  2097. */
  2098. static pci_ers_result_t igbvf_io_error_detected(struct pci_dev *pdev,
  2099. pci_channel_state_t state)
  2100. {
  2101. struct net_device *netdev = pci_get_drvdata(pdev);
  2102. struct igbvf_adapter *adapter = netdev_priv(netdev);
  2103. netif_device_detach(netdev);
  2104. if (state == pci_channel_io_perm_failure)
  2105. return PCI_ERS_RESULT_DISCONNECT;
  2106. if (netif_running(netdev))
  2107. igbvf_down(adapter);
  2108. pci_disable_device(pdev);
  2109. /* Request a slot slot reset. */
  2110. return PCI_ERS_RESULT_NEED_RESET;
  2111. }
  2112. /**
  2113. * igbvf_io_slot_reset - called after the pci bus has been reset.
  2114. * @pdev: Pointer to PCI device
  2115. *
  2116. * Restart the card from scratch, as if from a cold-boot. Implementation
  2117. * resembles the first-half of the igbvf_resume routine.
  2118. */
  2119. static pci_ers_result_t igbvf_io_slot_reset(struct pci_dev *pdev)
  2120. {
  2121. struct net_device *netdev = pci_get_drvdata(pdev);
  2122. struct igbvf_adapter *adapter = netdev_priv(netdev);
  2123. if (pci_enable_device_mem(pdev)) {
  2124. dev_err(&pdev->dev,
  2125. "Cannot re-enable PCI device after reset.\n");
  2126. return PCI_ERS_RESULT_DISCONNECT;
  2127. }
  2128. pci_set_master(pdev);
  2129. igbvf_reset(adapter);
  2130. return PCI_ERS_RESULT_RECOVERED;
  2131. }
  2132. /**
  2133. * igbvf_io_resume - called when traffic can start flowing again.
  2134. * @pdev: Pointer to PCI device
  2135. *
  2136. * This callback is called when the error recovery driver tells us that
  2137. * its OK to resume normal operation. Implementation resembles the
  2138. * second-half of the igbvf_resume routine.
  2139. */
  2140. static void igbvf_io_resume(struct pci_dev *pdev)
  2141. {
  2142. struct net_device *netdev = pci_get_drvdata(pdev);
  2143. struct igbvf_adapter *adapter = netdev_priv(netdev);
  2144. if (netif_running(netdev)) {
  2145. if (igbvf_up(adapter)) {
  2146. dev_err(&pdev->dev,
  2147. "can't bring device back up after reset\n");
  2148. return;
  2149. }
  2150. }
  2151. netif_device_attach(netdev);
  2152. }
  2153. static void igbvf_print_device_info(struct igbvf_adapter *adapter)
  2154. {
  2155. struct e1000_hw *hw = &adapter->hw;
  2156. struct net_device *netdev = adapter->netdev;
  2157. struct pci_dev *pdev = adapter->pdev;
  2158. if (hw->mac.type == e1000_vfadapt_i350)
  2159. dev_info(&pdev->dev, "Intel(R) I350 Virtual Function\n");
  2160. else
  2161. dev_info(&pdev->dev, "Intel(R) 82576 Virtual Function\n");
  2162. dev_info(&pdev->dev, "Address: %pM\n", netdev->dev_addr);
  2163. }
  2164. static int igbvf_set_features(struct net_device *netdev,
  2165. netdev_features_t features)
  2166. {
  2167. struct igbvf_adapter *adapter = netdev_priv(netdev);
  2168. if (features & NETIF_F_RXCSUM)
  2169. adapter->flags &= ~IGBVF_FLAG_RX_CSUM_DISABLED;
  2170. else
  2171. adapter->flags |= IGBVF_FLAG_RX_CSUM_DISABLED;
  2172. return 0;
  2173. }
  2174. #define IGBVF_MAX_MAC_HDR_LEN 127
  2175. #define IGBVF_MAX_NETWORK_HDR_LEN 511
  2176. static netdev_features_t
  2177. igbvf_features_check(struct sk_buff *skb, struct net_device *dev,
  2178. netdev_features_t features)
  2179. {
  2180. unsigned int network_hdr_len, mac_hdr_len;
  2181. /* Make certain the headers can be described by a context descriptor */
  2182. mac_hdr_len = skb_network_header(skb) - skb->data;
  2183. if (unlikely(mac_hdr_len > IGBVF_MAX_MAC_HDR_LEN))
  2184. return features & ~(NETIF_F_HW_CSUM |
  2185. NETIF_F_SCTP_CRC |
  2186. NETIF_F_HW_VLAN_CTAG_TX |
  2187. NETIF_F_TSO |
  2188. NETIF_F_TSO6);
  2189. network_hdr_len = skb_checksum_start(skb) - skb_network_header(skb);
  2190. if (unlikely(network_hdr_len > IGBVF_MAX_NETWORK_HDR_LEN))
  2191. return features & ~(NETIF_F_HW_CSUM |
  2192. NETIF_F_SCTP_CRC |
  2193. NETIF_F_TSO |
  2194. NETIF_F_TSO6);
  2195. /* We can only support IPV4 TSO in tunnels if we can mangle the
  2196. * inner IP ID field, so strip TSO if MANGLEID is not supported.
  2197. */
  2198. if (skb->encapsulation && !(features & NETIF_F_TSO_MANGLEID))
  2199. features &= ~NETIF_F_TSO;
  2200. return features;
  2201. }
  2202. static const struct net_device_ops igbvf_netdev_ops = {
  2203. .ndo_open = igbvf_open,
  2204. .ndo_stop = igbvf_close,
  2205. .ndo_start_xmit = igbvf_xmit_frame,
  2206. .ndo_set_rx_mode = igbvf_set_rx_mode,
  2207. .ndo_set_mac_address = igbvf_set_mac,
  2208. .ndo_change_mtu = igbvf_change_mtu,
  2209. .ndo_do_ioctl = igbvf_ioctl,
  2210. .ndo_tx_timeout = igbvf_tx_timeout,
  2211. .ndo_vlan_rx_add_vid = igbvf_vlan_rx_add_vid,
  2212. .ndo_vlan_rx_kill_vid = igbvf_vlan_rx_kill_vid,
  2213. #ifdef CONFIG_NET_POLL_CONTROLLER
  2214. .ndo_poll_controller = igbvf_netpoll,
  2215. #endif
  2216. .ndo_set_features = igbvf_set_features,
  2217. .ndo_features_check = igbvf_features_check,
  2218. };
  2219. /**
  2220. * igbvf_probe - Device Initialization Routine
  2221. * @pdev: PCI device information struct
  2222. * @ent: entry in igbvf_pci_tbl
  2223. *
  2224. * Returns 0 on success, negative on failure
  2225. *
  2226. * igbvf_probe initializes an adapter identified by a pci_dev structure.
  2227. * The OS initialization, configuring of the adapter private structure,
  2228. * and a hardware reset occur.
  2229. **/
  2230. static int igbvf_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
  2231. {
  2232. struct net_device *netdev;
  2233. struct igbvf_adapter *adapter;
  2234. struct e1000_hw *hw;
  2235. const struct igbvf_info *ei = igbvf_info_tbl[ent->driver_data];
  2236. static int cards_found;
  2237. int err, pci_using_dac;
  2238. err = pci_enable_device_mem(pdev);
  2239. if (err)
  2240. return err;
  2241. pci_using_dac = 0;
  2242. err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
  2243. if (!err) {
  2244. pci_using_dac = 1;
  2245. } else {
  2246. err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
  2247. if (err) {
  2248. dev_err(&pdev->dev,
  2249. "No usable DMA configuration, aborting\n");
  2250. goto err_dma;
  2251. }
  2252. }
  2253. err = pci_request_regions(pdev, igbvf_driver_name);
  2254. if (err)
  2255. goto err_pci_reg;
  2256. pci_set_master(pdev);
  2257. err = -ENOMEM;
  2258. netdev = alloc_etherdev(sizeof(struct igbvf_adapter));
  2259. if (!netdev)
  2260. goto err_alloc_etherdev;
  2261. SET_NETDEV_DEV(netdev, &pdev->dev);
  2262. pci_set_drvdata(pdev, netdev);
  2263. adapter = netdev_priv(netdev);
  2264. hw = &adapter->hw;
  2265. adapter->netdev = netdev;
  2266. adapter->pdev = pdev;
  2267. adapter->ei = ei;
  2268. adapter->pba = ei->pba;
  2269. adapter->flags = ei->flags;
  2270. adapter->hw.back = adapter;
  2271. adapter->hw.mac.type = ei->mac;
  2272. adapter->msg_enable = netif_msg_init(debug, DEFAULT_MSG_ENABLE);
  2273. /* PCI config space info */
  2274. hw->vendor_id = pdev->vendor;
  2275. hw->device_id = pdev->device;
  2276. hw->subsystem_vendor_id = pdev->subsystem_vendor;
  2277. hw->subsystem_device_id = pdev->subsystem_device;
  2278. hw->revision_id = pdev->revision;
  2279. err = -EIO;
  2280. adapter->hw.hw_addr = ioremap(pci_resource_start(pdev, 0),
  2281. pci_resource_len(pdev, 0));
  2282. if (!adapter->hw.hw_addr)
  2283. goto err_ioremap;
  2284. if (ei->get_variants) {
  2285. err = ei->get_variants(adapter);
  2286. if (err)
  2287. goto err_get_variants;
  2288. }
  2289. /* setup adapter struct */
  2290. err = igbvf_sw_init(adapter);
  2291. if (err)
  2292. goto err_sw_init;
  2293. /* construct the net_device struct */
  2294. netdev->netdev_ops = &igbvf_netdev_ops;
  2295. igbvf_set_ethtool_ops(netdev);
  2296. netdev->watchdog_timeo = 5 * HZ;
  2297. strncpy(netdev->name, pci_name(pdev), sizeof(netdev->name) - 1);
  2298. adapter->bd_number = cards_found++;
  2299. netdev->hw_features = NETIF_F_SG |
  2300. NETIF_F_TSO |
  2301. NETIF_F_TSO6 |
  2302. NETIF_F_RXCSUM |
  2303. NETIF_F_HW_CSUM |
  2304. NETIF_F_SCTP_CRC;
  2305. #define IGBVF_GSO_PARTIAL_FEATURES (NETIF_F_GSO_GRE | \
  2306. NETIF_F_GSO_GRE_CSUM | \
  2307. NETIF_F_GSO_IPXIP4 | \
  2308. NETIF_F_GSO_IPXIP6 | \
  2309. NETIF_F_GSO_UDP_TUNNEL | \
  2310. NETIF_F_GSO_UDP_TUNNEL_CSUM)
  2311. netdev->gso_partial_features = IGBVF_GSO_PARTIAL_FEATURES;
  2312. netdev->hw_features |= NETIF_F_GSO_PARTIAL |
  2313. IGBVF_GSO_PARTIAL_FEATURES;
  2314. netdev->features = netdev->hw_features;
  2315. if (pci_using_dac)
  2316. netdev->features |= NETIF_F_HIGHDMA;
  2317. netdev->vlan_features |= netdev->features | NETIF_F_TSO_MANGLEID;
  2318. netdev->mpls_features |= NETIF_F_HW_CSUM;
  2319. netdev->hw_enc_features |= netdev->vlan_features;
  2320. /* set this bit last since it cannot be part of vlan_features */
  2321. netdev->features |= NETIF_F_HW_VLAN_CTAG_FILTER |
  2322. NETIF_F_HW_VLAN_CTAG_RX |
  2323. NETIF_F_HW_VLAN_CTAG_TX;
  2324. /* MTU range: 68 - 9216 */
  2325. netdev->min_mtu = ETH_MIN_MTU;
  2326. netdev->max_mtu = MAX_STD_JUMBO_FRAME_SIZE;
  2327. spin_lock_bh(&hw->mbx_lock);
  2328. /*reset the controller to put the device in a known good state */
  2329. err = hw->mac.ops.reset_hw(hw);
  2330. if (err) {
  2331. dev_info(&pdev->dev,
  2332. "PF still in reset state. Is the PF interface up?\n");
  2333. } else {
  2334. err = hw->mac.ops.read_mac_addr(hw);
  2335. if (err)
  2336. dev_info(&pdev->dev, "Error reading MAC address.\n");
  2337. else if (is_zero_ether_addr(adapter->hw.mac.addr))
  2338. dev_info(&pdev->dev,
  2339. "MAC address not assigned by administrator.\n");
  2340. memcpy(netdev->dev_addr, adapter->hw.mac.addr,
  2341. netdev->addr_len);
  2342. }
  2343. spin_unlock_bh(&hw->mbx_lock);
  2344. if (!is_valid_ether_addr(netdev->dev_addr)) {
  2345. dev_info(&pdev->dev, "Assigning random MAC address.\n");
  2346. eth_hw_addr_random(netdev);
  2347. memcpy(adapter->hw.mac.addr, netdev->dev_addr,
  2348. netdev->addr_len);
  2349. }
  2350. timer_setup(&adapter->watchdog_timer, igbvf_watchdog, 0);
  2351. INIT_WORK(&adapter->reset_task, igbvf_reset_task);
  2352. INIT_WORK(&adapter->watchdog_task, igbvf_watchdog_task);
  2353. /* ring size defaults */
  2354. adapter->rx_ring->count = 1024;
  2355. adapter->tx_ring->count = 1024;
  2356. /* reset the hardware with the new settings */
  2357. igbvf_reset(adapter);
  2358. /* set hardware-specific flags */
  2359. if (adapter->hw.mac.type == e1000_vfadapt_i350)
  2360. adapter->flags |= IGBVF_FLAG_RX_LB_VLAN_BSWAP;
  2361. strcpy(netdev->name, "eth%d");
  2362. err = register_netdev(netdev);
  2363. if (err)
  2364. goto err_hw_init;
  2365. /* tell the stack to leave us alone until igbvf_open() is called */
  2366. netif_carrier_off(netdev);
  2367. netif_stop_queue(netdev);
  2368. igbvf_print_device_info(adapter);
  2369. igbvf_initialize_last_counter_stats(adapter);
  2370. return 0;
  2371. err_hw_init:
  2372. kfree(adapter->tx_ring);
  2373. kfree(adapter->rx_ring);
  2374. err_sw_init:
  2375. igbvf_reset_interrupt_capability(adapter);
  2376. err_get_variants:
  2377. iounmap(adapter->hw.hw_addr);
  2378. err_ioremap:
  2379. free_netdev(netdev);
  2380. err_alloc_etherdev:
  2381. pci_release_regions(pdev);
  2382. err_pci_reg:
  2383. err_dma:
  2384. pci_disable_device(pdev);
  2385. return err;
  2386. }
  2387. /**
  2388. * igbvf_remove - Device Removal Routine
  2389. * @pdev: PCI device information struct
  2390. *
  2391. * igbvf_remove is called by the PCI subsystem to alert the driver
  2392. * that it should release a PCI device. The could be caused by a
  2393. * Hot-Plug event, or because the driver is going to be removed from
  2394. * memory.
  2395. **/
  2396. static void igbvf_remove(struct pci_dev *pdev)
  2397. {
  2398. struct net_device *netdev = pci_get_drvdata(pdev);
  2399. struct igbvf_adapter *adapter = netdev_priv(netdev);
  2400. struct e1000_hw *hw = &adapter->hw;
  2401. /* The watchdog timer may be rescheduled, so explicitly
  2402. * disable it from being rescheduled.
  2403. */
  2404. set_bit(__IGBVF_DOWN, &adapter->state);
  2405. del_timer_sync(&adapter->watchdog_timer);
  2406. cancel_work_sync(&adapter->reset_task);
  2407. cancel_work_sync(&adapter->watchdog_task);
  2408. unregister_netdev(netdev);
  2409. igbvf_reset_interrupt_capability(adapter);
  2410. /* it is important to delete the NAPI struct prior to freeing the
  2411. * Rx ring so that you do not end up with null pointer refs
  2412. */
  2413. netif_napi_del(&adapter->rx_ring->napi);
  2414. kfree(adapter->tx_ring);
  2415. kfree(adapter->rx_ring);
  2416. iounmap(hw->hw_addr);
  2417. if (hw->flash_address)
  2418. iounmap(hw->flash_address);
  2419. pci_release_regions(pdev);
  2420. free_netdev(netdev);
  2421. pci_disable_device(pdev);
  2422. }
  2423. /* PCI Error Recovery (ERS) */
  2424. static const struct pci_error_handlers igbvf_err_handler = {
  2425. .error_detected = igbvf_io_error_detected,
  2426. .slot_reset = igbvf_io_slot_reset,
  2427. .resume = igbvf_io_resume,
  2428. };
  2429. static const struct pci_device_id igbvf_pci_tbl[] = {
  2430. { PCI_VDEVICE(INTEL, E1000_DEV_ID_82576_VF), board_vf },
  2431. { PCI_VDEVICE(INTEL, E1000_DEV_ID_I350_VF), board_i350_vf },
  2432. { } /* terminate list */
  2433. };
  2434. MODULE_DEVICE_TABLE(pci, igbvf_pci_tbl);
  2435. /* PCI Device API Driver */
  2436. static struct pci_driver igbvf_driver = {
  2437. .name = igbvf_driver_name,
  2438. .id_table = igbvf_pci_tbl,
  2439. .probe = igbvf_probe,
  2440. .remove = igbvf_remove,
  2441. #ifdef CONFIG_PM
  2442. /* Power Management Hooks */
  2443. .suspend = igbvf_suspend,
  2444. .resume = igbvf_resume,
  2445. #endif
  2446. .shutdown = igbvf_shutdown,
  2447. .err_handler = &igbvf_err_handler
  2448. };
  2449. /**
  2450. * igbvf_init_module - Driver Registration Routine
  2451. *
  2452. * igbvf_init_module is the first routine called when the driver is
  2453. * loaded. All it does is register with the PCI subsystem.
  2454. **/
  2455. static int __init igbvf_init_module(void)
  2456. {
  2457. int ret;
  2458. pr_info("%s - version %s\n", igbvf_driver_string, igbvf_driver_version);
  2459. pr_info("%s\n", igbvf_copyright);
  2460. ret = pci_register_driver(&igbvf_driver);
  2461. return ret;
  2462. }
  2463. module_init(igbvf_init_module);
  2464. /**
  2465. * igbvf_exit_module - Driver Exit Cleanup Routine
  2466. *
  2467. * igbvf_exit_module is called just before the driver is removed
  2468. * from memory.
  2469. **/
  2470. static void __exit igbvf_exit_module(void)
  2471. {
  2472. pci_unregister_driver(&igbvf_driver);
  2473. }
  2474. module_exit(igbvf_exit_module);
  2475. MODULE_AUTHOR("Intel Corporation, <e1000-devel@lists.sourceforge.net>");
  2476. MODULE_DESCRIPTION("Intel(R) Gigabit Virtual Function Network Driver");
  2477. MODULE_LICENSE("GPL");
  2478. MODULE_VERSION(DRV_VERSION);
  2479. /* netdev.c */