netdev.c 76 KB

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