netdev.c 76 KB

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