netdev.c 76 KB

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