netdev.c 78 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016
  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. default:
  1736. skb_checksum_help(skb);
  1737. goto csum_failed;
  1738. }
  1739. vlan_macip_lens = skb_checksum_start_offset(skb) -
  1740. skb_network_offset(skb);
  1741. no_csum:
  1742. vlan_macip_lens |= skb_network_offset(skb) << E1000_ADVTXD_MACLEN_SHIFT;
  1743. vlan_macip_lens |= tx_flags & IGBVF_TX_FLAGS_VLAN_MASK;
  1744. igbvf_tx_ctxtdesc(tx_ring, vlan_macip_lens, type_tucmd, 0);
  1745. return true;
  1746. }
  1747. static int igbvf_maybe_stop_tx(struct net_device *netdev, int size)
  1748. {
  1749. struct igbvf_adapter *adapter = netdev_priv(netdev);
  1750. /* there is enough descriptors then we don't need to worry */
  1751. if (igbvf_desc_unused(adapter->tx_ring) >= size)
  1752. return 0;
  1753. netif_stop_queue(netdev);
  1754. /* Herbert's original patch had:
  1755. * smp_mb__after_netif_stop_queue();
  1756. * but since that doesn't exist yet, just open code it.
  1757. */
  1758. smp_mb();
  1759. /* We need to check again just in case room has been made available */
  1760. if (igbvf_desc_unused(adapter->tx_ring) < size)
  1761. return -EBUSY;
  1762. netif_wake_queue(netdev);
  1763. ++adapter->restart_queue;
  1764. return 0;
  1765. }
  1766. #define IGBVF_MAX_TXD_PWR 16
  1767. #define IGBVF_MAX_DATA_PER_TXD (1u << IGBVF_MAX_TXD_PWR)
  1768. static inline int igbvf_tx_map_adv(struct igbvf_adapter *adapter,
  1769. struct igbvf_ring *tx_ring,
  1770. struct sk_buff *skb)
  1771. {
  1772. struct igbvf_buffer *buffer_info;
  1773. struct pci_dev *pdev = adapter->pdev;
  1774. unsigned int len = skb_headlen(skb);
  1775. unsigned int count = 0, i;
  1776. unsigned int f;
  1777. i = tx_ring->next_to_use;
  1778. buffer_info = &tx_ring->buffer_info[i];
  1779. BUG_ON(len >= IGBVF_MAX_DATA_PER_TXD);
  1780. buffer_info->length = len;
  1781. /* set time_stamp *before* dma to help avoid a possible race */
  1782. buffer_info->time_stamp = jiffies;
  1783. buffer_info->mapped_as_page = false;
  1784. buffer_info->dma = dma_map_single(&pdev->dev, skb->data, len,
  1785. DMA_TO_DEVICE);
  1786. if (dma_mapping_error(&pdev->dev, buffer_info->dma))
  1787. goto dma_error;
  1788. for (f = 0; f < skb_shinfo(skb)->nr_frags; f++) {
  1789. const struct skb_frag_struct *frag;
  1790. count++;
  1791. i++;
  1792. if (i == tx_ring->count)
  1793. i = 0;
  1794. frag = &skb_shinfo(skb)->frags[f];
  1795. len = skb_frag_size(frag);
  1796. buffer_info = &tx_ring->buffer_info[i];
  1797. BUG_ON(len >= IGBVF_MAX_DATA_PER_TXD);
  1798. buffer_info->length = len;
  1799. buffer_info->time_stamp = jiffies;
  1800. buffer_info->mapped_as_page = true;
  1801. buffer_info->dma = skb_frag_dma_map(&pdev->dev, frag, 0, len,
  1802. DMA_TO_DEVICE);
  1803. if (dma_mapping_error(&pdev->dev, buffer_info->dma))
  1804. goto dma_error;
  1805. }
  1806. tx_ring->buffer_info[i].skb = skb;
  1807. return ++count;
  1808. dma_error:
  1809. dev_err(&pdev->dev, "TX DMA map failed\n");
  1810. /* clear timestamp and dma mappings for failed buffer_info mapping */
  1811. buffer_info->dma = 0;
  1812. buffer_info->time_stamp = 0;
  1813. buffer_info->length = 0;
  1814. buffer_info->mapped_as_page = false;
  1815. if (count)
  1816. count--;
  1817. /* clear timestamp and dma mappings for remaining portion of packet */
  1818. while (count--) {
  1819. if (i == 0)
  1820. i += tx_ring->count;
  1821. i--;
  1822. buffer_info = &tx_ring->buffer_info[i];
  1823. igbvf_put_txbuf(adapter, buffer_info);
  1824. }
  1825. return 0;
  1826. }
  1827. static inline void igbvf_tx_queue_adv(struct igbvf_adapter *adapter,
  1828. struct igbvf_ring *tx_ring,
  1829. int tx_flags, int count,
  1830. unsigned int first, u32 paylen,
  1831. u8 hdr_len)
  1832. {
  1833. union e1000_adv_tx_desc *tx_desc = NULL;
  1834. struct igbvf_buffer *buffer_info;
  1835. u32 olinfo_status = 0, cmd_type_len;
  1836. unsigned int i;
  1837. cmd_type_len = (E1000_ADVTXD_DTYP_DATA | E1000_ADVTXD_DCMD_IFCS |
  1838. E1000_ADVTXD_DCMD_DEXT);
  1839. if (tx_flags & IGBVF_TX_FLAGS_VLAN)
  1840. cmd_type_len |= E1000_ADVTXD_DCMD_VLE;
  1841. if (tx_flags & IGBVF_TX_FLAGS_TSO) {
  1842. cmd_type_len |= E1000_ADVTXD_DCMD_TSE;
  1843. /* insert tcp checksum */
  1844. olinfo_status |= E1000_TXD_POPTS_TXSM << 8;
  1845. /* insert ip checksum */
  1846. if (tx_flags & IGBVF_TX_FLAGS_IPV4)
  1847. olinfo_status |= E1000_TXD_POPTS_IXSM << 8;
  1848. } else if (tx_flags & IGBVF_TX_FLAGS_CSUM) {
  1849. olinfo_status |= E1000_TXD_POPTS_TXSM << 8;
  1850. }
  1851. olinfo_status |= ((paylen - hdr_len) << E1000_ADVTXD_PAYLEN_SHIFT);
  1852. i = tx_ring->next_to_use;
  1853. while (count--) {
  1854. buffer_info = &tx_ring->buffer_info[i];
  1855. tx_desc = IGBVF_TX_DESC_ADV(*tx_ring, i);
  1856. tx_desc->read.buffer_addr = cpu_to_le64(buffer_info->dma);
  1857. tx_desc->read.cmd_type_len =
  1858. cpu_to_le32(cmd_type_len | buffer_info->length);
  1859. tx_desc->read.olinfo_status = cpu_to_le32(olinfo_status);
  1860. i++;
  1861. if (i == tx_ring->count)
  1862. i = 0;
  1863. }
  1864. tx_desc->read.cmd_type_len |= cpu_to_le32(adapter->txd_cmd);
  1865. /* Force memory writes to complete before letting h/w
  1866. * know there are new descriptors to fetch. (Only
  1867. * applicable for weak-ordered memory model archs,
  1868. * such as IA-64).
  1869. */
  1870. wmb();
  1871. tx_ring->buffer_info[first].next_to_watch = tx_desc;
  1872. tx_ring->next_to_use = i;
  1873. writel(i, adapter->hw.hw_addr + tx_ring->tail);
  1874. /* we need this if more than one processor can write to our tail
  1875. * at a time, it synchronizes IO on IA64/Altix systems
  1876. */
  1877. mmiowb();
  1878. }
  1879. static netdev_tx_t igbvf_xmit_frame_ring_adv(struct sk_buff *skb,
  1880. struct net_device *netdev,
  1881. struct igbvf_ring *tx_ring)
  1882. {
  1883. struct igbvf_adapter *adapter = netdev_priv(netdev);
  1884. unsigned int first, tx_flags = 0;
  1885. u8 hdr_len = 0;
  1886. int count = 0;
  1887. int tso = 0;
  1888. __be16 protocol = vlan_get_protocol(skb);
  1889. if (test_bit(__IGBVF_DOWN, &adapter->state)) {
  1890. dev_kfree_skb_any(skb);
  1891. return NETDEV_TX_OK;
  1892. }
  1893. if (skb->len <= 0) {
  1894. dev_kfree_skb_any(skb);
  1895. return NETDEV_TX_OK;
  1896. }
  1897. /* need: count + 4 desc gap to keep tail from touching
  1898. * + 2 desc gap to keep tail from touching head,
  1899. * + 1 desc for skb->data,
  1900. * + 1 desc for context descriptor,
  1901. * head, otherwise try next time
  1902. */
  1903. if (igbvf_maybe_stop_tx(netdev, skb_shinfo(skb)->nr_frags + 4)) {
  1904. /* this is a hard error */
  1905. return NETDEV_TX_BUSY;
  1906. }
  1907. if (skb_vlan_tag_present(skb)) {
  1908. tx_flags |= IGBVF_TX_FLAGS_VLAN;
  1909. tx_flags |= (skb_vlan_tag_get(skb) <<
  1910. IGBVF_TX_FLAGS_VLAN_SHIFT);
  1911. }
  1912. if (protocol == htons(ETH_P_IP))
  1913. tx_flags |= IGBVF_TX_FLAGS_IPV4;
  1914. first = tx_ring->next_to_use;
  1915. tso = igbvf_tso(tx_ring, skb, tx_flags, &hdr_len);
  1916. if (unlikely(tso < 0)) {
  1917. dev_kfree_skb_any(skb);
  1918. return NETDEV_TX_OK;
  1919. }
  1920. if (tso)
  1921. tx_flags |= IGBVF_TX_FLAGS_TSO;
  1922. else if (igbvf_tx_csum(tx_ring, skb, tx_flags, protocol) &&
  1923. (skb->ip_summed == CHECKSUM_PARTIAL))
  1924. tx_flags |= IGBVF_TX_FLAGS_CSUM;
  1925. /* count reflects descriptors mapped, if 0 then mapping error
  1926. * has occurred and we need to rewind the descriptor queue
  1927. */
  1928. count = igbvf_tx_map_adv(adapter, tx_ring, skb);
  1929. if (count) {
  1930. igbvf_tx_queue_adv(adapter, tx_ring, tx_flags, count,
  1931. first, skb->len, hdr_len);
  1932. /* Make sure there is space in the ring for the next send. */
  1933. igbvf_maybe_stop_tx(netdev, MAX_SKB_FRAGS + 4);
  1934. } else {
  1935. dev_kfree_skb_any(skb);
  1936. tx_ring->buffer_info[first].time_stamp = 0;
  1937. tx_ring->next_to_use = first;
  1938. }
  1939. return NETDEV_TX_OK;
  1940. }
  1941. static netdev_tx_t igbvf_xmit_frame(struct sk_buff *skb,
  1942. struct net_device *netdev)
  1943. {
  1944. struct igbvf_adapter *adapter = netdev_priv(netdev);
  1945. struct igbvf_ring *tx_ring;
  1946. if (test_bit(__IGBVF_DOWN, &adapter->state)) {
  1947. dev_kfree_skb_any(skb);
  1948. return NETDEV_TX_OK;
  1949. }
  1950. tx_ring = &adapter->tx_ring[0];
  1951. return igbvf_xmit_frame_ring_adv(skb, netdev, tx_ring);
  1952. }
  1953. /**
  1954. * igbvf_tx_timeout - Respond to a Tx Hang
  1955. * @netdev: network interface device structure
  1956. **/
  1957. static void igbvf_tx_timeout(struct net_device *netdev)
  1958. {
  1959. struct igbvf_adapter *adapter = netdev_priv(netdev);
  1960. /* Do the reset outside of interrupt context */
  1961. adapter->tx_timeout_count++;
  1962. schedule_work(&adapter->reset_task);
  1963. }
  1964. static void igbvf_reset_task(struct work_struct *work)
  1965. {
  1966. struct igbvf_adapter *adapter;
  1967. adapter = container_of(work, struct igbvf_adapter, reset_task);
  1968. igbvf_reinit_locked(adapter);
  1969. }
  1970. /**
  1971. * igbvf_change_mtu - Change the Maximum Transfer Unit
  1972. * @netdev: network interface device structure
  1973. * @new_mtu: new value for maximum frame size
  1974. *
  1975. * Returns 0 on success, negative on failure
  1976. **/
  1977. static int igbvf_change_mtu(struct net_device *netdev, int new_mtu)
  1978. {
  1979. struct igbvf_adapter *adapter = netdev_priv(netdev);
  1980. int max_frame = new_mtu + ETH_HLEN + ETH_FCS_LEN;
  1981. while (test_and_set_bit(__IGBVF_RESETTING, &adapter->state))
  1982. usleep_range(1000, 2000);
  1983. /* igbvf_down has a dependency on max_frame_size */
  1984. adapter->max_frame_size = max_frame;
  1985. if (netif_running(netdev))
  1986. igbvf_down(adapter);
  1987. /* NOTE: netdev_alloc_skb reserves 16 bytes, and typically NET_IP_ALIGN
  1988. * means we reserve 2 more, this pushes us to allocate from the next
  1989. * larger slab size.
  1990. * i.e. RXBUFFER_2048 --> size-4096 slab
  1991. * However with the new *_jumbo_rx* routines, jumbo receives will use
  1992. * fragmented skbs
  1993. */
  1994. if (max_frame <= 1024)
  1995. adapter->rx_buffer_len = 1024;
  1996. else if (max_frame <= 2048)
  1997. adapter->rx_buffer_len = 2048;
  1998. else
  1999. #if (PAGE_SIZE / 2) > 16384
  2000. adapter->rx_buffer_len = 16384;
  2001. #else
  2002. adapter->rx_buffer_len = PAGE_SIZE / 2;
  2003. #endif
  2004. /* adjust allocation if LPE protects us, and we aren't using SBP */
  2005. if ((max_frame == ETH_FRAME_LEN + ETH_FCS_LEN) ||
  2006. (max_frame == ETH_FRAME_LEN + VLAN_HLEN + ETH_FCS_LEN))
  2007. adapter->rx_buffer_len = ETH_FRAME_LEN + VLAN_HLEN +
  2008. ETH_FCS_LEN;
  2009. dev_info(&adapter->pdev->dev, "changing MTU from %d to %d\n",
  2010. netdev->mtu, new_mtu);
  2011. netdev->mtu = new_mtu;
  2012. if (netif_running(netdev))
  2013. igbvf_up(adapter);
  2014. else
  2015. igbvf_reset(adapter);
  2016. clear_bit(__IGBVF_RESETTING, &adapter->state);
  2017. return 0;
  2018. }
  2019. static int igbvf_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
  2020. {
  2021. switch (cmd) {
  2022. default:
  2023. return -EOPNOTSUPP;
  2024. }
  2025. }
  2026. static int igbvf_suspend(struct pci_dev *pdev, pm_message_t state)
  2027. {
  2028. struct net_device *netdev = pci_get_drvdata(pdev);
  2029. struct igbvf_adapter *adapter = netdev_priv(netdev);
  2030. #ifdef CONFIG_PM
  2031. int retval = 0;
  2032. #endif
  2033. netif_device_detach(netdev);
  2034. if (netif_running(netdev)) {
  2035. WARN_ON(test_bit(__IGBVF_RESETTING, &adapter->state));
  2036. igbvf_down(adapter);
  2037. igbvf_free_irq(adapter);
  2038. }
  2039. #ifdef CONFIG_PM
  2040. retval = pci_save_state(pdev);
  2041. if (retval)
  2042. return retval;
  2043. #endif
  2044. pci_disable_device(pdev);
  2045. return 0;
  2046. }
  2047. #ifdef CONFIG_PM
  2048. static int igbvf_resume(struct pci_dev *pdev)
  2049. {
  2050. struct net_device *netdev = pci_get_drvdata(pdev);
  2051. struct igbvf_adapter *adapter = netdev_priv(netdev);
  2052. u32 err;
  2053. pci_restore_state(pdev);
  2054. err = pci_enable_device_mem(pdev);
  2055. if (err) {
  2056. dev_err(&pdev->dev, "Cannot enable PCI device from suspend\n");
  2057. return err;
  2058. }
  2059. pci_set_master(pdev);
  2060. if (netif_running(netdev)) {
  2061. err = igbvf_request_irq(adapter);
  2062. if (err)
  2063. return err;
  2064. }
  2065. igbvf_reset(adapter);
  2066. if (netif_running(netdev))
  2067. igbvf_up(adapter);
  2068. netif_device_attach(netdev);
  2069. return 0;
  2070. }
  2071. #endif
  2072. static void igbvf_shutdown(struct pci_dev *pdev)
  2073. {
  2074. igbvf_suspend(pdev, PMSG_SUSPEND);
  2075. }
  2076. #ifdef CONFIG_NET_POLL_CONTROLLER
  2077. /* Polling 'interrupt' - used by things like netconsole to send skbs
  2078. * without having to re-enable interrupts. It's not called while
  2079. * the interrupt routine is executing.
  2080. */
  2081. static void igbvf_netpoll(struct net_device *netdev)
  2082. {
  2083. struct igbvf_adapter *adapter = netdev_priv(netdev);
  2084. disable_irq(adapter->pdev->irq);
  2085. igbvf_clean_tx_irq(adapter->tx_ring);
  2086. enable_irq(adapter->pdev->irq);
  2087. }
  2088. #endif
  2089. /**
  2090. * igbvf_io_error_detected - called when PCI error is detected
  2091. * @pdev: Pointer to PCI device
  2092. * @state: The current pci connection state
  2093. *
  2094. * This function is called after a PCI bus error affecting
  2095. * this device has been detected.
  2096. */
  2097. static pci_ers_result_t igbvf_io_error_detected(struct pci_dev *pdev,
  2098. pci_channel_state_t state)
  2099. {
  2100. struct net_device *netdev = pci_get_drvdata(pdev);
  2101. struct igbvf_adapter *adapter = netdev_priv(netdev);
  2102. netif_device_detach(netdev);
  2103. if (state == pci_channel_io_perm_failure)
  2104. return PCI_ERS_RESULT_DISCONNECT;
  2105. if (netif_running(netdev))
  2106. igbvf_down(adapter);
  2107. pci_disable_device(pdev);
  2108. /* Request a slot slot reset. */
  2109. return PCI_ERS_RESULT_NEED_RESET;
  2110. }
  2111. /**
  2112. * igbvf_io_slot_reset - called after the pci bus has been reset.
  2113. * @pdev: Pointer to PCI device
  2114. *
  2115. * Restart the card from scratch, as if from a cold-boot. Implementation
  2116. * resembles the first-half of the igbvf_resume routine.
  2117. */
  2118. static pci_ers_result_t igbvf_io_slot_reset(struct pci_dev *pdev)
  2119. {
  2120. struct net_device *netdev = pci_get_drvdata(pdev);
  2121. struct igbvf_adapter *adapter = netdev_priv(netdev);
  2122. if (pci_enable_device_mem(pdev)) {
  2123. dev_err(&pdev->dev,
  2124. "Cannot re-enable PCI device after reset.\n");
  2125. return PCI_ERS_RESULT_DISCONNECT;
  2126. }
  2127. pci_set_master(pdev);
  2128. igbvf_reset(adapter);
  2129. return PCI_ERS_RESULT_RECOVERED;
  2130. }
  2131. /**
  2132. * igbvf_io_resume - called when traffic can start flowing again.
  2133. * @pdev: Pointer to PCI device
  2134. *
  2135. * This callback is called when the error recovery driver tells us that
  2136. * its OK to resume normal operation. Implementation resembles the
  2137. * second-half of the igbvf_resume routine.
  2138. */
  2139. static void igbvf_io_resume(struct pci_dev *pdev)
  2140. {
  2141. struct net_device *netdev = pci_get_drvdata(pdev);
  2142. struct igbvf_adapter *adapter = netdev_priv(netdev);
  2143. if (netif_running(netdev)) {
  2144. if (igbvf_up(adapter)) {
  2145. dev_err(&pdev->dev,
  2146. "can't bring device back up after reset\n");
  2147. return;
  2148. }
  2149. }
  2150. netif_device_attach(netdev);
  2151. }
  2152. static void igbvf_print_device_info(struct igbvf_adapter *adapter)
  2153. {
  2154. struct e1000_hw *hw = &adapter->hw;
  2155. struct net_device *netdev = adapter->netdev;
  2156. struct pci_dev *pdev = adapter->pdev;
  2157. if (hw->mac.type == e1000_vfadapt_i350)
  2158. dev_info(&pdev->dev, "Intel(R) I350 Virtual Function\n");
  2159. else
  2160. dev_info(&pdev->dev, "Intel(R) 82576 Virtual Function\n");
  2161. dev_info(&pdev->dev, "Address: %pM\n", netdev->dev_addr);
  2162. }
  2163. static int igbvf_set_features(struct net_device *netdev,
  2164. netdev_features_t features)
  2165. {
  2166. struct igbvf_adapter *adapter = netdev_priv(netdev);
  2167. if (features & NETIF_F_RXCSUM)
  2168. adapter->flags &= ~IGBVF_FLAG_RX_CSUM_DISABLED;
  2169. else
  2170. adapter->flags |= IGBVF_FLAG_RX_CSUM_DISABLED;
  2171. return 0;
  2172. }
  2173. #define IGBVF_MAX_MAC_HDR_LEN 127
  2174. #define IGBVF_MAX_NETWORK_HDR_LEN 511
  2175. static netdev_features_t
  2176. igbvf_features_check(struct sk_buff *skb, struct net_device *dev,
  2177. netdev_features_t features)
  2178. {
  2179. unsigned int network_hdr_len, mac_hdr_len;
  2180. /* Make certain the headers can be described by a context descriptor */
  2181. mac_hdr_len = skb_network_header(skb) - skb->data;
  2182. if (unlikely(mac_hdr_len > IGBVF_MAX_MAC_HDR_LEN))
  2183. return features & ~(NETIF_F_HW_CSUM |
  2184. NETIF_F_SCTP_CRC |
  2185. NETIF_F_HW_VLAN_CTAG_TX |
  2186. NETIF_F_TSO |
  2187. NETIF_F_TSO6);
  2188. network_hdr_len = skb_checksum_start(skb) - skb_network_header(skb);
  2189. if (unlikely(network_hdr_len > IGBVF_MAX_NETWORK_HDR_LEN))
  2190. return features & ~(NETIF_F_HW_CSUM |
  2191. NETIF_F_SCTP_CRC |
  2192. NETIF_F_TSO |
  2193. NETIF_F_TSO6);
  2194. /* We can only support IPV4 TSO in tunnels if we can mangle the
  2195. * inner IP ID field, so strip TSO if MANGLEID is not supported.
  2196. */
  2197. if (skb->encapsulation && !(features & NETIF_F_TSO_MANGLEID))
  2198. features &= ~NETIF_F_TSO;
  2199. return features;
  2200. }
  2201. static const struct net_device_ops igbvf_netdev_ops = {
  2202. .ndo_open = igbvf_open,
  2203. .ndo_stop = igbvf_close,
  2204. .ndo_start_xmit = igbvf_xmit_frame,
  2205. .ndo_set_rx_mode = igbvf_set_rx_mode,
  2206. .ndo_set_mac_address = igbvf_set_mac,
  2207. .ndo_change_mtu = igbvf_change_mtu,
  2208. .ndo_do_ioctl = igbvf_ioctl,
  2209. .ndo_tx_timeout = igbvf_tx_timeout,
  2210. .ndo_vlan_rx_add_vid = igbvf_vlan_rx_add_vid,
  2211. .ndo_vlan_rx_kill_vid = igbvf_vlan_rx_kill_vid,
  2212. #ifdef CONFIG_NET_POLL_CONTROLLER
  2213. .ndo_poll_controller = igbvf_netpoll,
  2214. #endif
  2215. .ndo_set_features = igbvf_set_features,
  2216. .ndo_features_check = igbvf_features_check,
  2217. };
  2218. /**
  2219. * igbvf_probe - Device Initialization Routine
  2220. * @pdev: PCI device information struct
  2221. * @ent: entry in igbvf_pci_tbl
  2222. *
  2223. * Returns 0 on success, negative on failure
  2224. *
  2225. * igbvf_probe initializes an adapter identified by a pci_dev structure.
  2226. * The OS initialization, configuring of the adapter private structure,
  2227. * and a hardware reset occur.
  2228. **/
  2229. static int igbvf_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
  2230. {
  2231. struct net_device *netdev;
  2232. struct igbvf_adapter *adapter;
  2233. struct e1000_hw *hw;
  2234. const struct igbvf_info *ei = igbvf_info_tbl[ent->driver_data];
  2235. static int cards_found;
  2236. int err, pci_using_dac;
  2237. err = pci_enable_device_mem(pdev);
  2238. if (err)
  2239. return err;
  2240. pci_using_dac = 0;
  2241. err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
  2242. if (!err) {
  2243. pci_using_dac = 1;
  2244. } else {
  2245. err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
  2246. if (err) {
  2247. dev_err(&pdev->dev,
  2248. "No usable DMA configuration, aborting\n");
  2249. goto err_dma;
  2250. }
  2251. }
  2252. err = pci_request_regions(pdev, igbvf_driver_name);
  2253. if (err)
  2254. goto err_pci_reg;
  2255. pci_set_master(pdev);
  2256. err = -ENOMEM;
  2257. netdev = alloc_etherdev(sizeof(struct igbvf_adapter));
  2258. if (!netdev)
  2259. goto err_alloc_etherdev;
  2260. SET_NETDEV_DEV(netdev, &pdev->dev);
  2261. pci_set_drvdata(pdev, netdev);
  2262. adapter = netdev_priv(netdev);
  2263. hw = &adapter->hw;
  2264. adapter->netdev = netdev;
  2265. adapter->pdev = pdev;
  2266. adapter->ei = ei;
  2267. adapter->pba = ei->pba;
  2268. adapter->flags = ei->flags;
  2269. adapter->hw.back = adapter;
  2270. adapter->hw.mac.type = ei->mac;
  2271. adapter->msg_enable = netif_msg_init(debug, DEFAULT_MSG_ENABLE);
  2272. /* PCI config space info */
  2273. hw->vendor_id = pdev->vendor;
  2274. hw->device_id = pdev->device;
  2275. hw->subsystem_vendor_id = pdev->subsystem_vendor;
  2276. hw->subsystem_device_id = pdev->subsystem_device;
  2277. hw->revision_id = pdev->revision;
  2278. err = -EIO;
  2279. adapter->hw.hw_addr = ioremap(pci_resource_start(pdev, 0),
  2280. pci_resource_len(pdev, 0));
  2281. if (!adapter->hw.hw_addr)
  2282. goto err_ioremap;
  2283. if (ei->get_variants) {
  2284. err = ei->get_variants(adapter);
  2285. if (err)
  2286. goto err_get_variants;
  2287. }
  2288. /* setup adapter struct */
  2289. err = igbvf_sw_init(adapter);
  2290. if (err)
  2291. goto err_sw_init;
  2292. /* construct the net_device struct */
  2293. netdev->netdev_ops = &igbvf_netdev_ops;
  2294. igbvf_set_ethtool_ops(netdev);
  2295. netdev->watchdog_timeo = 5 * HZ;
  2296. strncpy(netdev->name, pci_name(pdev), sizeof(netdev->name) - 1);
  2297. adapter->bd_number = cards_found++;
  2298. netdev->hw_features = NETIF_F_SG |
  2299. NETIF_F_TSO |
  2300. NETIF_F_TSO6 |
  2301. NETIF_F_RXCSUM |
  2302. NETIF_F_HW_CSUM |
  2303. NETIF_F_SCTP_CRC;
  2304. #define IGBVF_GSO_PARTIAL_FEATURES (NETIF_F_GSO_GRE | \
  2305. NETIF_F_GSO_GRE_CSUM | \
  2306. NETIF_F_GSO_IPXIP4 | \
  2307. NETIF_F_GSO_IPXIP6 | \
  2308. NETIF_F_GSO_UDP_TUNNEL | \
  2309. NETIF_F_GSO_UDP_TUNNEL_CSUM)
  2310. netdev->gso_partial_features = IGBVF_GSO_PARTIAL_FEATURES;
  2311. netdev->hw_features |= NETIF_F_GSO_PARTIAL |
  2312. IGBVF_GSO_PARTIAL_FEATURES;
  2313. netdev->features = netdev->hw_features;
  2314. if (pci_using_dac)
  2315. netdev->features |= NETIF_F_HIGHDMA;
  2316. netdev->vlan_features |= netdev->features | NETIF_F_TSO_MANGLEID;
  2317. netdev->mpls_features |= NETIF_F_HW_CSUM;
  2318. netdev->hw_enc_features |= netdev->vlan_features;
  2319. /* set this bit last since it cannot be part of vlan_features */
  2320. netdev->features |= NETIF_F_HW_VLAN_CTAG_FILTER |
  2321. NETIF_F_HW_VLAN_CTAG_RX |
  2322. NETIF_F_HW_VLAN_CTAG_TX;
  2323. /* MTU range: 68 - 9216 */
  2324. netdev->min_mtu = ETH_MIN_MTU;
  2325. netdev->max_mtu = MAX_STD_JUMBO_FRAME_SIZE;
  2326. spin_lock_bh(&hw->mbx_lock);
  2327. /*reset the controller to put the device in a known good state */
  2328. err = hw->mac.ops.reset_hw(hw);
  2329. if (err) {
  2330. dev_info(&pdev->dev,
  2331. "PF still in reset state. Is the PF interface up?\n");
  2332. } else {
  2333. err = hw->mac.ops.read_mac_addr(hw);
  2334. if (err)
  2335. dev_info(&pdev->dev, "Error reading MAC address.\n");
  2336. else if (is_zero_ether_addr(adapter->hw.mac.addr))
  2337. dev_info(&pdev->dev,
  2338. "MAC address not assigned by administrator.\n");
  2339. memcpy(netdev->dev_addr, adapter->hw.mac.addr,
  2340. netdev->addr_len);
  2341. }
  2342. spin_unlock_bh(&hw->mbx_lock);
  2343. if (!is_valid_ether_addr(netdev->dev_addr)) {
  2344. dev_info(&pdev->dev, "Assigning random MAC address.\n");
  2345. eth_hw_addr_random(netdev);
  2346. memcpy(adapter->hw.mac.addr, netdev->dev_addr,
  2347. netdev->addr_len);
  2348. }
  2349. timer_setup(&adapter->watchdog_timer, igbvf_watchdog, 0);
  2350. INIT_WORK(&adapter->reset_task, igbvf_reset_task);
  2351. INIT_WORK(&adapter->watchdog_task, igbvf_watchdog_task);
  2352. /* ring size defaults */
  2353. adapter->rx_ring->count = 1024;
  2354. adapter->tx_ring->count = 1024;
  2355. /* reset the hardware with the new settings */
  2356. igbvf_reset(adapter);
  2357. /* set hardware-specific flags */
  2358. if (adapter->hw.mac.type == e1000_vfadapt_i350)
  2359. adapter->flags |= IGBVF_FLAG_RX_LB_VLAN_BSWAP;
  2360. strcpy(netdev->name, "eth%d");
  2361. err = register_netdev(netdev);
  2362. if (err)
  2363. goto err_hw_init;
  2364. /* tell the stack to leave us alone until igbvf_open() is called */
  2365. netif_carrier_off(netdev);
  2366. netif_stop_queue(netdev);
  2367. igbvf_print_device_info(adapter);
  2368. igbvf_initialize_last_counter_stats(adapter);
  2369. return 0;
  2370. err_hw_init:
  2371. kfree(adapter->tx_ring);
  2372. kfree(adapter->rx_ring);
  2373. err_sw_init:
  2374. igbvf_reset_interrupt_capability(adapter);
  2375. err_get_variants:
  2376. iounmap(adapter->hw.hw_addr);
  2377. err_ioremap:
  2378. free_netdev(netdev);
  2379. err_alloc_etherdev:
  2380. pci_release_regions(pdev);
  2381. err_pci_reg:
  2382. err_dma:
  2383. pci_disable_device(pdev);
  2384. return err;
  2385. }
  2386. /**
  2387. * igbvf_remove - Device Removal Routine
  2388. * @pdev: PCI device information struct
  2389. *
  2390. * igbvf_remove is called by the PCI subsystem to alert the driver
  2391. * that it should release a PCI device. The could be caused by a
  2392. * Hot-Plug event, or because the driver is going to be removed from
  2393. * memory.
  2394. **/
  2395. static void igbvf_remove(struct pci_dev *pdev)
  2396. {
  2397. struct net_device *netdev = pci_get_drvdata(pdev);
  2398. struct igbvf_adapter *adapter = netdev_priv(netdev);
  2399. struct e1000_hw *hw = &adapter->hw;
  2400. /* The watchdog timer may be rescheduled, so explicitly
  2401. * disable it from being rescheduled.
  2402. */
  2403. set_bit(__IGBVF_DOWN, &adapter->state);
  2404. del_timer_sync(&adapter->watchdog_timer);
  2405. cancel_work_sync(&adapter->reset_task);
  2406. cancel_work_sync(&adapter->watchdog_task);
  2407. unregister_netdev(netdev);
  2408. igbvf_reset_interrupt_capability(adapter);
  2409. /* it is important to delete the NAPI struct prior to freeing the
  2410. * Rx ring so that you do not end up with null pointer refs
  2411. */
  2412. netif_napi_del(&adapter->rx_ring->napi);
  2413. kfree(adapter->tx_ring);
  2414. kfree(adapter->rx_ring);
  2415. iounmap(hw->hw_addr);
  2416. if (hw->flash_address)
  2417. iounmap(hw->flash_address);
  2418. pci_release_regions(pdev);
  2419. free_netdev(netdev);
  2420. pci_disable_device(pdev);
  2421. }
  2422. /* PCI Error Recovery (ERS) */
  2423. static const struct pci_error_handlers igbvf_err_handler = {
  2424. .error_detected = igbvf_io_error_detected,
  2425. .slot_reset = igbvf_io_slot_reset,
  2426. .resume = igbvf_io_resume,
  2427. };
  2428. static const struct pci_device_id igbvf_pci_tbl[] = {
  2429. { PCI_VDEVICE(INTEL, E1000_DEV_ID_82576_VF), board_vf },
  2430. { PCI_VDEVICE(INTEL, E1000_DEV_ID_I350_VF), board_i350_vf },
  2431. { } /* terminate list */
  2432. };
  2433. MODULE_DEVICE_TABLE(pci, igbvf_pci_tbl);
  2434. /* PCI Device API Driver */
  2435. static struct pci_driver igbvf_driver = {
  2436. .name = igbvf_driver_name,
  2437. .id_table = igbvf_pci_tbl,
  2438. .probe = igbvf_probe,
  2439. .remove = igbvf_remove,
  2440. #ifdef CONFIG_PM
  2441. /* Power Management Hooks */
  2442. .suspend = igbvf_suspend,
  2443. .resume = igbvf_resume,
  2444. #endif
  2445. .shutdown = igbvf_shutdown,
  2446. .err_handler = &igbvf_err_handler
  2447. };
  2448. /**
  2449. * igbvf_init_module - Driver Registration Routine
  2450. *
  2451. * igbvf_init_module is the first routine called when the driver is
  2452. * loaded. All it does is register with the PCI subsystem.
  2453. **/
  2454. static int __init igbvf_init_module(void)
  2455. {
  2456. int ret;
  2457. pr_info("%s - version %s\n", igbvf_driver_string, igbvf_driver_version);
  2458. pr_info("%s\n", igbvf_copyright);
  2459. ret = pci_register_driver(&igbvf_driver);
  2460. return ret;
  2461. }
  2462. module_init(igbvf_init_module);
  2463. /**
  2464. * igbvf_exit_module - Driver Exit Cleanup Routine
  2465. *
  2466. * igbvf_exit_module is called just before the driver is removed
  2467. * from memory.
  2468. **/
  2469. static void __exit igbvf_exit_module(void)
  2470. {
  2471. pci_unregister_driver(&igbvf_driver);
  2472. }
  2473. module_exit(igbvf_exit_module);
  2474. MODULE_AUTHOR("Intel Corporation, <e1000-devel@lists.sourceforge.net>");
  2475. MODULE_DESCRIPTION("Intel(R) Gigabit Virtual Function Network Driver");
  2476. MODULE_LICENSE("GPL");
  2477. MODULE_VERSION(DRV_VERSION);
  2478. /* netdev.c */