ixgbevf_main.c 94 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593
  1. /*******************************************************************************
  2. Intel 82599 Virtual Function driver
  3. Copyright(c) 1999 - 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. /******************************************************************************
  21. Copyright (c)2006 - 2007 Myricom, Inc. for some LRO specific code
  22. ******************************************************************************/
  23. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  24. #include <linux/types.h>
  25. #include <linux/bitops.h>
  26. #include <linux/module.h>
  27. #include <linux/pci.h>
  28. #include <linux/netdevice.h>
  29. #include <linux/vmalloc.h>
  30. #include <linux/string.h>
  31. #include <linux/in.h>
  32. #include <linux/ip.h>
  33. #include <linux/tcp.h>
  34. #include <linux/sctp.h>
  35. #include <linux/ipv6.h>
  36. #include <linux/slab.h>
  37. #include <net/checksum.h>
  38. #include <net/ip6_checksum.h>
  39. #include <linux/ethtool.h>
  40. #include <linux/if.h>
  41. #include <linux/if_vlan.h>
  42. #include <linux/prefetch.h>
  43. #include "ixgbevf.h"
  44. const char ixgbevf_driver_name[] = "ixgbevf";
  45. static const char ixgbevf_driver_string[] =
  46. "Intel(R) 10 Gigabit PCI Express Virtual Function Network Driver";
  47. #define DRV_VERSION "2.6.0-k"
  48. const char ixgbevf_driver_version[] = DRV_VERSION;
  49. static char ixgbevf_copyright[] =
  50. "Copyright (c) 2009 - 2012 Intel Corporation.";
  51. static const struct ixgbevf_info *ixgbevf_info_tbl[] = {
  52. [board_82599_vf] = &ixgbevf_82599_vf_info,
  53. [board_X540_vf] = &ixgbevf_X540_vf_info,
  54. };
  55. /* ixgbevf_pci_tbl - PCI Device ID Table
  56. *
  57. * Wildcard entries (PCI_ANY_ID) should come last
  58. * Last entry must be all 0s
  59. *
  60. * { Vendor ID, Device ID, SubVendor ID, SubDevice ID,
  61. * Class, Class Mask, private data (not used) }
  62. */
  63. static struct pci_device_id ixgbevf_pci_tbl[] = {
  64. {PCI_VDEVICE(INTEL, IXGBE_DEV_ID_82599_VF),
  65. board_82599_vf},
  66. {PCI_VDEVICE(INTEL, IXGBE_DEV_ID_X540_VF),
  67. board_X540_vf},
  68. /* required last entry */
  69. {0, }
  70. };
  71. MODULE_DEVICE_TABLE(pci, ixgbevf_pci_tbl);
  72. MODULE_AUTHOR("Intel Corporation, <linux.nics@intel.com>");
  73. MODULE_DESCRIPTION("Intel(R) 82599 Virtual Function Driver");
  74. MODULE_LICENSE("GPL");
  75. MODULE_VERSION(DRV_VERSION);
  76. #define DEFAULT_MSG_ENABLE (NETIF_MSG_DRV|NETIF_MSG_PROBE|NETIF_MSG_LINK)
  77. static int debug = -1;
  78. module_param(debug, int, 0);
  79. MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all)");
  80. /* forward decls */
  81. static void ixgbevf_set_itr(struct ixgbevf_q_vector *q_vector);
  82. static void ixgbevf_free_all_rx_resources(struct ixgbevf_adapter *adapter);
  83. static inline void ixgbevf_release_rx_desc(struct ixgbe_hw *hw,
  84. struct ixgbevf_ring *rx_ring,
  85. u32 val)
  86. {
  87. /*
  88. * Force memory writes to complete before letting h/w
  89. * know there are new descriptors to fetch. (Only
  90. * applicable for weak-ordered memory model archs,
  91. * such as IA-64).
  92. */
  93. wmb();
  94. IXGBE_WRITE_REG(hw, IXGBE_VFRDT(rx_ring->reg_idx), val);
  95. }
  96. /**
  97. * ixgbevf_set_ivar - set IVAR registers - maps interrupt causes to vectors
  98. * @adapter: pointer to adapter struct
  99. * @direction: 0 for Rx, 1 for Tx, -1 for other causes
  100. * @queue: queue to map the corresponding interrupt to
  101. * @msix_vector: the vector to map to the corresponding queue
  102. *
  103. */
  104. static void ixgbevf_set_ivar(struct ixgbevf_adapter *adapter, s8 direction,
  105. u8 queue, u8 msix_vector)
  106. {
  107. u32 ivar, index;
  108. struct ixgbe_hw *hw = &adapter->hw;
  109. if (direction == -1) {
  110. /* other causes */
  111. msix_vector |= IXGBE_IVAR_ALLOC_VAL;
  112. ivar = IXGBE_READ_REG(hw, IXGBE_VTIVAR_MISC);
  113. ivar &= ~0xFF;
  114. ivar |= msix_vector;
  115. IXGBE_WRITE_REG(hw, IXGBE_VTIVAR_MISC, ivar);
  116. } else {
  117. /* tx or rx causes */
  118. msix_vector |= IXGBE_IVAR_ALLOC_VAL;
  119. index = ((16 * (queue & 1)) + (8 * direction));
  120. ivar = IXGBE_READ_REG(hw, IXGBE_VTIVAR(queue >> 1));
  121. ivar &= ~(0xFF << index);
  122. ivar |= (msix_vector << index);
  123. IXGBE_WRITE_REG(hw, IXGBE_VTIVAR(queue >> 1), ivar);
  124. }
  125. }
  126. static void ixgbevf_unmap_and_free_tx_resource(struct ixgbevf_ring *tx_ring,
  127. struct ixgbevf_tx_buffer
  128. *tx_buffer_info)
  129. {
  130. if (tx_buffer_info->dma) {
  131. if (tx_buffer_info->mapped_as_page)
  132. dma_unmap_page(tx_ring->dev,
  133. tx_buffer_info->dma,
  134. tx_buffer_info->length,
  135. DMA_TO_DEVICE);
  136. else
  137. dma_unmap_single(tx_ring->dev,
  138. tx_buffer_info->dma,
  139. tx_buffer_info->length,
  140. DMA_TO_DEVICE);
  141. tx_buffer_info->dma = 0;
  142. }
  143. if (tx_buffer_info->skb) {
  144. dev_kfree_skb_any(tx_buffer_info->skb);
  145. tx_buffer_info->skb = NULL;
  146. }
  147. tx_buffer_info->time_stamp = 0;
  148. /* tx_buffer_info must be completely set up in the transmit path */
  149. }
  150. #define IXGBE_MAX_TXD_PWR 14
  151. #define IXGBE_MAX_DATA_PER_TXD (1 << IXGBE_MAX_TXD_PWR)
  152. /* Tx Descriptors needed, worst case */
  153. #define TXD_USE_COUNT(S) DIV_ROUND_UP((S), IXGBE_MAX_DATA_PER_TXD)
  154. #define DESC_NEEDED (MAX_SKB_FRAGS + 4)
  155. static void ixgbevf_tx_timeout(struct net_device *netdev);
  156. /**
  157. * ixgbevf_clean_tx_irq - Reclaim resources after transmit completes
  158. * @q_vector: board private structure
  159. * @tx_ring: tx ring to clean
  160. **/
  161. static bool ixgbevf_clean_tx_irq(struct ixgbevf_q_vector *q_vector,
  162. struct ixgbevf_ring *tx_ring)
  163. {
  164. struct ixgbevf_adapter *adapter = q_vector->adapter;
  165. union ixgbe_adv_tx_desc *tx_desc, *eop_desc;
  166. struct ixgbevf_tx_buffer *tx_buffer_info;
  167. unsigned int i, eop, count = 0;
  168. unsigned int total_bytes = 0, total_packets = 0;
  169. if (test_bit(__IXGBEVF_DOWN, &adapter->state))
  170. return true;
  171. i = tx_ring->next_to_clean;
  172. eop = tx_ring->tx_buffer_info[i].next_to_watch;
  173. eop_desc = IXGBEVF_TX_DESC(tx_ring, eop);
  174. while ((eop_desc->wb.status & cpu_to_le32(IXGBE_TXD_STAT_DD)) &&
  175. (count < tx_ring->count)) {
  176. bool cleaned = false;
  177. rmb(); /* read buffer_info after eop_desc */
  178. /* eop could change between read and DD-check */
  179. if (unlikely(eop != tx_ring->tx_buffer_info[i].next_to_watch))
  180. goto cont_loop;
  181. for ( ; !cleaned; count++) {
  182. struct sk_buff *skb;
  183. tx_desc = IXGBEVF_TX_DESC(tx_ring, i);
  184. tx_buffer_info = &tx_ring->tx_buffer_info[i];
  185. cleaned = (i == eop);
  186. skb = tx_buffer_info->skb;
  187. if (cleaned && skb) {
  188. unsigned int segs, bytecount;
  189. /* gso_segs is currently only valid for tcp */
  190. segs = skb_shinfo(skb)->gso_segs ?: 1;
  191. /* multiply data chunks by size of headers */
  192. bytecount = ((segs - 1) * skb_headlen(skb)) +
  193. skb->len;
  194. total_packets += segs;
  195. total_bytes += bytecount;
  196. }
  197. ixgbevf_unmap_and_free_tx_resource(tx_ring,
  198. tx_buffer_info);
  199. tx_desc->wb.status = 0;
  200. i++;
  201. if (i == tx_ring->count)
  202. i = 0;
  203. }
  204. cont_loop:
  205. eop = tx_ring->tx_buffer_info[i].next_to_watch;
  206. eop_desc = IXGBEVF_TX_DESC(tx_ring, eop);
  207. }
  208. tx_ring->next_to_clean = i;
  209. #define TX_WAKE_THRESHOLD (DESC_NEEDED * 2)
  210. if (unlikely(count && netif_carrier_ok(tx_ring->netdev) &&
  211. (IXGBE_DESC_UNUSED(tx_ring) >= TX_WAKE_THRESHOLD))) {
  212. /* Make sure that anybody stopping the queue after this
  213. * sees the new next_to_clean.
  214. */
  215. smp_mb();
  216. if (__netif_subqueue_stopped(tx_ring->netdev,
  217. tx_ring->queue_index) &&
  218. !test_bit(__IXGBEVF_DOWN, &adapter->state)) {
  219. netif_wake_subqueue(tx_ring->netdev,
  220. tx_ring->queue_index);
  221. ++adapter->restart_queue;
  222. }
  223. }
  224. u64_stats_update_begin(&tx_ring->syncp);
  225. tx_ring->total_bytes += total_bytes;
  226. tx_ring->total_packets += total_packets;
  227. u64_stats_update_end(&tx_ring->syncp);
  228. q_vector->tx.total_bytes += total_bytes;
  229. q_vector->tx.total_packets += total_packets;
  230. return count < tx_ring->count;
  231. }
  232. /**
  233. * ixgbevf_receive_skb - Send a completed packet up the stack
  234. * @q_vector: structure containing interrupt and ring information
  235. * @skb: packet to send up
  236. * @status: hardware indication of status of receive
  237. * @rx_desc: rx descriptor
  238. **/
  239. static void ixgbevf_receive_skb(struct ixgbevf_q_vector *q_vector,
  240. struct sk_buff *skb, u8 status,
  241. union ixgbe_adv_rx_desc *rx_desc)
  242. {
  243. struct ixgbevf_adapter *adapter = q_vector->adapter;
  244. bool is_vlan = (status & IXGBE_RXD_STAT_VP);
  245. u16 tag = le16_to_cpu(rx_desc->wb.upper.vlan);
  246. if (is_vlan && test_bit(tag & VLAN_VID_MASK, adapter->active_vlans))
  247. __vlan_hwaccel_put_tag(skb, tag);
  248. napi_gro_receive(&q_vector->napi, skb);
  249. }
  250. /**
  251. * ixgbevf_rx_checksum - indicate in skb if hw indicated a good cksum
  252. * @adapter: address of board private structure
  253. * @status_err: hardware indication of status of receive
  254. * @skb: skb currently being received and modified
  255. **/
  256. static inline void ixgbevf_rx_checksum(struct ixgbevf_adapter *adapter,
  257. struct ixgbevf_ring *ring,
  258. u32 status_err, struct sk_buff *skb)
  259. {
  260. skb_checksum_none_assert(skb);
  261. /* Rx csum disabled */
  262. if (!(ring->netdev->features & NETIF_F_RXCSUM))
  263. return;
  264. /* if IP and error */
  265. if ((status_err & IXGBE_RXD_STAT_IPCS) &&
  266. (status_err & IXGBE_RXDADV_ERR_IPE)) {
  267. adapter->hw_csum_rx_error++;
  268. return;
  269. }
  270. if (!(status_err & IXGBE_RXD_STAT_L4CS))
  271. return;
  272. if (status_err & IXGBE_RXDADV_ERR_TCPE) {
  273. adapter->hw_csum_rx_error++;
  274. return;
  275. }
  276. /* It must be a TCP or UDP packet with a valid checksum */
  277. skb->ip_summed = CHECKSUM_UNNECESSARY;
  278. adapter->hw_csum_rx_good++;
  279. }
  280. /**
  281. * ixgbevf_alloc_rx_buffers - Replace used receive buffers; packet split
  282. * @adapter: address of board private structure
  283. **/
  284. static void ixgbevf_alloc_rx_buffers(struct ixgbevf_adapter *adapter,
  285. struct ixgbevf_ring *rx_ring,
  286. int cleaned_count)
  287. {
  288. struct pci_dev *pdev = adapter->pdev;
  289. union ixgbe_adv_rx_desc *rx_desc;
  290. struct ixgbevf_rx_buffer *bi;
  291. struct sk_buff *skb;
  292. unsigned int i = rx_ring->next_to_use;
  293. bi = &rx_ring->rx_buffer_info[i];
  294. while (cleaned_count--) {
  295. rx_desc = IXGBEVF_RX_DESC(rx_ring, i);
  296. skb = bi->skb;
  297. if (!skb) {
  298. skb = netdev_alloc_skb_ip_align(rx_ring->netdev,
  299. rx_ring->rx_buf_len);
  300. if (!skb) {
  301. adapter->alloc_rx_buff_failed++;
  302. goto no_buffers;
  303. }
  304. bi->skb = skb;
  305. }
  306. if (!bi->dma) {
  307. bi->dma = dma_map_single(&pdev->dev, skb->data,
  308. rx_ring->rx_buf_len,
  309. DMA_FROM_DEVICE);
  310. }
  311. rx_desc->read.pkt_addr = cpu_to_le64(bi->dma);
  312. i++;
  313. if (i == rx_ring->count)
  314. i = 0;
  315. bi = &rx_ring->rx_buffer_info[i];
  316. }
  317. no_buffers:
  318. if (rx_ring->next_to_use != i) {
  319. rx_ring->next_to_use = i;
  320. ixgbevf_release_rx_desc(&adapter->hw, rx_ring, i);
  321. }
  322. }
  323. static inline void ixgbevf_irq_enable_queues(struct ixgbevf_adapter *adapter,
  324. u32 qmask)
  325. {
  326. struct ixgbe_hw *hw = &adapter->hw;
  327. IXGBE_WRITE_REG(hw, IXGBE_VTEIMS, qmask);
  328. }
  329. static bool ixgbevf_clean_rx_irq(struct ixgbevf_q_vector *q_vector,
  330. struct ixgbevf_ring *rx_ring,
  331. int budget)
  332. {
  333. struct ixgbevf_adapter *adapter = q_vector->adapter;
  334. struct pci_dev *pdev = adapter->pdev;
  335. union ixgbe_adv_rx_desc *rx_desc, *next_rxd;
  336. struct ixgbevf_rx_buffer *rx_buffer_info, *next_buffer;
  337. struct sk_buff *skb;
  338. unsigned int i;
  339. u32 len, staterr;
  340. int cleaned_count = 0;
  341. unsigned int total_rx_bytes = 0, total_rx_packets = 0;
  342. i = rx_ring->next_to_clean;
  343. rx_desc = IXGBEVF_RX_DESC(rx_ring, i);
  344. staterr = le32_to_cpu(rx_desc->wb.upper.status_error);
  345. rx_buffer_info = &rx_ring->rx_buffer_info[i];
  346. while (staterr & IXGBE_RXD_STAT_DD) {
  347. if (!budget)
  348. break;
  349. budget--;
  350. rmb(); /* read descriptor and rx_buffer_info after status DD */
  351. len = le16_to_cpu(rx_desc->wb.upper.length);
  352. skb = rx_buffer_info->skb;
  353. prefetch(skb->data - NET_IP_ALIGN);
  354. rx_buffer_info->skb = NULL;
  355. if (rx_buffer_info->dma) {
  356. dma_unmap_single(&pdev->dev, rx_buffer_info->dma,
  357. rx_ring->rx_buf_len,
  358. DMA_FROM_DEVICE);
  359. rx_buffer_info->dma = 0;
  360. skb_put(skb, len);
  361. }
  362. i++;
  363. if (i == rx_ring->count)
  364. i = 0;
  365. next_rxd = IXGBEVF_RX_DESC(rx_ring, i);
  366. prefetch(next_rxd);
  367. cleaned_count++;
  368. next_buffer = &rx_ring->rx_buffer_info[i];
  369. if (!(staterr & IXGBE_RXD_STAT_EOP)) {
  370. skb->next = next_buffer->skb;
  371. IXGBE_CB(skb->next)->prev = skb;
  372. adapter->non_eop_descs++;
  373. goto next_desc;
  374. }
  375. /* we should not be chaining buffers, if we did drop the skb */
  376. if (IXGBE_CB(skb)->prev) {
  377. do {
  378. struct sk_buff *this = skb;
  379. skb = IXGBE_CB(skb)->prev;
  380. dev_kfree_skb(this);
  381. } while (skb);
  382. goto next_desc;
  383. }
  384. /* ERR_MASK will only have valid bits if EOP set */
  385. if (unlikely(staterr & IXGBE_RXDADV_ERR_FRAME_ERR_MASK)) {
  386. dev_kfree_skb_irq(skb);
  387. goto next_desc;
  388. }
  389. ixgbevf_rx_checksum(adapter, rx_ring, staterr, skb);
  390. /* probably a little skewed due to removing CRC */
  391. total_rx_bytes += skb->len;
  392. total_rx_packets++;
  393. /*
  394. * Work around issue of some types of VM to VM loop back
  395. * packets not getting split correctly
  396. */
  397. if (staterr & IXGBE_RXD_STAT_LB) {
  398. u32 header_fixup_len = skb_headlen(skb);
  399. if (header_fixup_len < 14)
  400. skb_push(skb, header_fixup_len);
  401. }
  402. skb->protocol = eth_type_trans(skb, rx_ring->netdev);
  403. ixgbevf_receive_skb(q_vector, skb, staterr, rx_desc);
  404. next_desc:
  405. rx_desc->wb.upper.status_error = 0;
  406. /* return some buffers to hardware, one at a time is too slow */
  407. if (cleaned_count >= IXGBEVF_RX_BUFFER_WRITE) {
  408. ixgbevf_alloc_rx_buffers(adapter, rx_ring,
  409. cleaned_count);
  410. cleaned_count = 0;
  411. }
  412. /* use prefetched values */
  413. rx_desc = next_rxd;
  414. rx_buffer_info = &rx_ring->rx_buffer_info[i];
  415. staterr = le32_to_cpu(rx_desc->wb.upper.status_error);
  416. }
  417. rx_ring->next_to_clean = i;
  418. cleaned_count = IXGBE_DESC_UNUSED(rx_ring);
  419. if (cleaned_count)
  420. ixgbevf_alloc_rx_buffers(adapter, rx_ring, cleaned_count);
  421. u64_stats_update_begin(&rx_ring->syncp);
  422. rx_ring->total_packets += total_rx_packets;
  423. rx_ring->total_bytes += total_rx_bytes;
  424. u64_stats_update_end(&rx_ring->syncp);
  425. q_vector->rx.total_packets += total_rx_packets;
  426. q_vector->rx.total_bytes += total_rx_bytes;
  427. return !!budget;
  428. }
  429. /**
  430. * ixgbevf_poll - NAPI polling calback
  431. * @napi: napi struct with our devices info in it
  432. * @budget: amount of work driver is allowed to do this pass, in packets
  433. *
  434. * This function will clean more than one or more rings associated with a
  435. * q_vector.
  436. **/
  437. static int ixgbevf_poll(struct napi_struct *napi, int budget)
  438. {
  439. struct ixgbevf_q_vector *q_vector =
  440. container_of(napi, struct ixgbevf_q_vector, napi);
  441. struct ixgbevf_adapter *adapter = q_vector->adapter;
  442. struct ixgbevf_ring *ring;
  443. int per_ring_budget;
  444. bool clean_complete = true;
  445. ixgbevf_for_each_ring(ring, q_vector->tx)
  446. clean_complete &= ixgbevf_clean_tx_irq(q_vector, ring);
  447. /* attempt to distribute budget to each queue fairly, but don't allow
  448. * the budget to go below 1 because we'll exit polling */
  449. if (q_vector->rx.count > 1)
  450. per_ring_budget = max(budget/q_vector->rx.count, 1);
  451. else
  452. per_ring_budget = budget;
  453. ixgbevf_for_each_ring(ring, q_vector->rx)
  454. clean_complete &= ixgbevf_clean_rx_irq(q_vector, ring,
  455. per_ring_budget);
  456. /* If all work not completed, return budget and keep polling */
  457. if (!clean_complete)
  458. return budget;
  459. /* all work done, exit the polling mode */
  460. napi_complete(napi);
  461. if (adapter->rx_itr_setting & 1)
  462. ixgbevf_set_itr(q_vector);
  463. if (!test_bit(__IXGBEVF_DOWN, &adapter->state))
  464. ixgbevf_irq_enable_queues(adapter,
  465. 1 << q_vector->v_idx);
  466. return 0;
  467. }
  468. /**
  469. * ixgbevf_write_eitr - write VTEITR register in hardware specific way
  470. * @q_vector: structure containing interrupt and ring information
  471. */
  472. static void ixgbevf_write_eitr(struct ixgbevf_q_vector *q_vector)
  473. {
  474. struct ixgbevf_adapter *adapter = q_vector->adapter;
  475. struct ixgbe_hw *hw = &adapter->hw;
  476. int v_idx = q_vector->v_idx;
  477. u32 itr_reg = q_vector->itr & IXGBE_MAX_EITR;
  478. /*
  479. * set the WDIS bit to not clear the timer bits and cause an
  480. * immediate assertion of the interrupt
  481. */
  482. itr_reg |= IXGBE_EITR_CNT_WDIS;
  483. IXGBE_WRITE_REG(hw, IXGBE_VTEITR(v_idx), itr_reg);
  484. }
  485. /**
  486. * ixgbevf_configure_msix - Configure MSI-X hardware
  487. * @adapter: board private structure
  488. *
  489. * ixgbevf_configure_msix sets up the hardware to properly generate MSI-X
  490. * interrupts.
  491. **/
  492. static void ixgbevf_configure_msix(struct ixgbevf_adapter *adapter)
  493. {
  494. struct ixgbevf_q_vector *q_vector;
  495. int q_vectors, v_idx;
  496. q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
  497. adapter->eims_enable_mask = 0;
  498. /*
  499. * Populate the IVAR table and set the ITR values to the
  500. * corresponding register.
  501. */
  502. for (v_idx = 0; v_idx < q_vectors; v_idx++) {
  503. struct ixgbevf_ring *ring;
  504. q_vector = adapter->q_vector[v_idx];
  505. ixgbevf_for_each_ring(ring, q_vector->rx)
  506. ixgbevf_set_ivar(adapter, 0, ring->reg_idx, v_idx);
  507. ixgbevf_for_each_ring(ring, q_vector->tx)
  508. ixgbevf_set_ivar(adapter, 1, ring->reg_idx, v_idx);
  509. if (q_vector->tx.ring && !q_vector->rx.ring) {
  510. /* tx only vector */
  511. if (adapter->tx_itr_setting == 1)
  512. q_vector->itr = IXGBE_10K_ITR;
  513. else
  514. q_vector->itr = adapter->tx_itr_setting;
  515. } else {
  516. /* rx or rx/tx vector */
  517. if (adapter->rx_itr_setting == 1)
  518. q_vector->itr = IXGBE_20K_ITR;
  519. else
  520. q_vector->itr = adapter->rx_itr_setting;
  521. }
  522. /* add q_vector eims value to global eims_enable_mask */
  523. adapter->eims_enable_mask |= 1 << v_idx;
  524. ixgbevf_write_eitr(q_vector);
  525. }
  526. ixgbevf_set_ivar(adapter, -1, 1, v_idx);
  527. /* setup eims_other and add value to global eims_enable_mask */
  528. adapter->eims_other = 1 << v_idx;
  529. adapter->eims_enable_mask |= adapter->eims_other;
  530. }
  531. enum latency_range {
  532. lowest_latency = 0,
  533. low_latency = 1,
  534. bulk_latency = 2,
  535. latency_invalid = 255
  536. };
  537. /**
  538. * ixgbevf_update_itr - update the dynamic ITR value based on statistics
  539. * @q_vector: structure containing interrupt and ring information
  540. * @ring_container: structure containing ring performance data
  541. *
  542. * Stores a new ITR value based on packets and byte
  543. * counts during the last interrupt. The advantage of per interrupt
  544. * computation is faster updates and more accurate ITR for the current
  545. * traffic pattern. Constants in this function were computed
  546. * based on theoretical maximum wire speed and thresholds were set based
  547. * on testing data as well as attempting to minimize response time
  548. * while increasing bulk throughput.
  549. **/
  550. static void ixgbevf_update_itr(struct ixgbevf_q_vector *q_vector,
  551. struct ixgbevf_ring_container *ring_container)
  552. {
  553. int bytes = ring_container->total_bytes;
  554. int packets = ring_container->total_packets;
  555. u32 timepassed_us;
  556. u64 bytes_perint;
  557. u8 itr_setting = ring_container->itr;
  558. if (packets == 0)
  559. return;
  560. /* simple throttlerate management
  561. * 0-20MB/s lowest (100000 ints/s)
  562. * 20-100MB/s low (20000 ints/s)
  563. * 100-1249MB/s bulk (8000 ints/s)
  564. */
  565. /* what was last interrupt timeslice? */
  566. timepassed_us = q_vector->itr >> 2;
  567. bytes_perint = bytes / timepassed_us; /* bytes/usec */
  568. switch (itr_setting) {
  569. case lowest_latency:
  570. if (bytes_perint > 10)
  571. itr_setting = low_latency;
  572. break;
  573. case low_latency:
  574. if (bytes_perint > 20)
  575. itr_setting = bulk_latency;
  576. else if (bytes_perint <= 10)
  577. itr_setting = lowest_latency;
  578. break;
  579. case bulk_latency:
  580. if (bytes_perint <= 20)
  581. itr_setting = low_latency;
  582. break;
  583. }
  584. /* clear work counters since we have the values we need */
  585. ring_container->total_bytes = 0;
  586. ring_container->total_packets = 0;
  587. /* write updated itr to ring container */
  588. ring_container->itr = itr_setting;
  589. }
  590. static void ixgbevf_set_itr(struct ixgbevf_q_vector *q_vector)
  591. {
  592. u32 new_itr = q_vector->itr;
  593. u8 current_itr;
  594. ixgbevf_update_itr(q_vector, &q_vector->tx);
  595. ixgbevf_update_itr(q_vector, &q_vector->rx);
  596. current_itr = max(q_vector->rx.itr, q_vector->tx.itr);
  597. switch (current_itr) {
  598. /* counts and packets in update_itr are dependent on these numbers */
  599. case lowest_latency:
  600. new_itr = IXGBE_100K_ITR;
  601. break;
  602. case low_latency:
  603. new_itr = IXGBE_20K_ITR;
  604. break;
  605. case bulk_latency:
  606. default:
  607. new_itr = IXGBE_8K_ITR;
  608. break;
  609. }
  610. if (new_itr != q_vector->itr) {
  611. /* do an exponential smoothing */
  612. new_itr = (10 * new_itr * q_vector->itr) /
  613. ((9 * new_itr) + q_vector->itr);
  614. /* save the algorithm value here */
  615. q_vector->itr = new_itr;
  616. ixgbevf_write_eitr(q_vector);
  617. }
  618. }
  619. static irqreturn_t ixgbevf_msix_other(int irq, void *data)
  620. {
  621. struct ixgbevf_adapter *adapter = data;
  622. struct ixgbe_hw *hw = &adapter->hw;
  623. hw->mac.get_link_status = 1;
  624. if (!test_bit(__IXGBEVF_DOWN, &adapter->state))
  625. mod_timer(&adapter->watchdog_timer, jiffies);
  626. IXGBE_WRITE_REG(hw, IXGBE_VTEIMS, adapter->eims_other);
  627. return IRQ_HANDLED;
  628. }
  629. /**
  630. * ixgbevf_msix_clean_rings - single unshared vector rx clean (all queues)
  631. * @irq: unused
  632. * @data: pointer to our q_vector struct for this interrupt vector
  633. **/
  634. static irqreturn_t ixgbevf_msix_clean_rings(int irq, void *data)
  635. {
  636. struct ixgbevf_q_vector *q_vector = data;
  637. /* EIAM disabled interrupts (on this vector) for us */
  638. if (q_vector->rx.ring || q_vector->tx.ring)
  639. napi_schedule(&q_vector->napi);
  640. return IRQ_HANDLED;
  641. }
  642. static inline void map_vector_to_rxq(struct ixgbevf_adapter *a, int v_idx,
  643. int r_idx)
  644. {
  645. struct ixgbevf_q_vector *q_vector = a->q_vector[v_idx];
  646. a->rx_ring[r_idx].next = q_vector->rx.ring;
  647. q_vector->rx.ring = &a->rx_ring[r_idx];
  648. q_vector->rx.count++;
  649. }
  650. static inline void map_vector_to_txq(struct ixgbevf_adapter *a, int v_idx,
  651. int t_idx)
  652. {
  653. struct ixgbevf_q_vector *q_vector = a->q_vector[v_idx];
  654. a->tx_ring[t_idx].next = q_vector->tx.ring;
  655. q_vector->tx.ring = &a->tx_ring[t_idx];
  656. q_vector->tx.count++;
  657. }
  658. /**
  659. * ixgbevf_map_rings_to_vectors - Maps descriptor rings to vectors
  660. * @adapter: board private structure to initialize
  661. *
  662. * This function maps descriptor rings to the queue-specific vectors
  663. * we were allotted through the MSI-X enabling code. Ideally, we'd have
  664. * one vector per ring/queue, but on a constrained vector budget, we
  665. * group the rings as "efficiently" as possible. You would add new
  666. * mapping configurations in here.
  667. **/
  668. static int ixgbevf_map_rings_to_vectors(struct ixgbevf_adapter *adapter)
  669. {
  670. int q_vectors;
  671. int v_start = 0;
  672. int rxr_idx = 0, txr_idx = 0;
  673. int rxr_remaining = adapter->num_rx_queues;
  674. int txr_remaining = adapter->num_tx_queues;
  675. int i, j;
  676. int rqpv, tqpv;
  677. int err = 0;
  678. q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
  679. /*
  680. * The ideal configuration...
  681. * We have enough vectors to map one per queue.
  682. */
  683. if (q_vectors == adapter->num_rx_queues + adapter->num_tx_queues) {
  684. for (; rxr_idx < rxr_remaining; v_start++, rxr_idx++)
  685. map_vector_to_rxq(adapter, v_start, rxr_idx);
  686. for (; txr_idx < txr_remaining; v_start++, txr_idx++)
  687. map_vector_to_txq(adapter, v_start, txr_idx);
  688. goto out;
  689. }
  690. /*
  691. * If we don't have enough vectors for a 1-to-1
  692. * mapping, we'll have to group them so there are
  693. * multiple queues per vector.
  694. */
  695. /* Re-adjusting *qpv takes care of the remainder. */
  696. for (i = v_start; i < q_vectors; i++) {
  697. rqpv = DIV_ROUND_UP(rxr_remaining, q_vectors - i);
  698. for (j = 0; j < rqpv; j++) {
  699. map_vector_to_rxq(adapter, i, rxr_idx);
  700. rxr_idx++;
  701. rxr_remaining--;
  702. }
  703. }
  704. for (i = v_start; i < q_vectors; i++) {
  705. tqpv = DIV_ROUND_UP(txr_remaining, q_vectors - i);
  706. for (j = 0; j < tqpv; j++) {
  707. map_vector_to_txq(adapter, i, txr_idx);
  708. txr_idx++;
  709. txr_remaining--;
  710. }
  711. }
  712. out:
  713. return err;
  714. }
  715. /**
  716. * ixgbevf_request_msix_irqs - Initialize MSI-X interrupts
  717. * @adapter: board private structure
  718. *
  719. * ixgbevf_request_msix_irqs allocates MSI-X vectors and requests
  720. * interrupts from the kernel.
  721. **/
  722. static int ixgbevf_request_msix_irqs(struct ixgbevf_adapter *adapter)
  723. {
  724. struct net_device *netdev = adapter->netdev;
  725. int q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
  726. int vector, err;
  727. int ri = 0, ti = 0;
  728. for (vector = 0; vector < q_vectors; vector++) {
  729. struct ixgbevf_q_vector *q_vector = adapter->q_vector[vector];
  730. struct msix_entry *entry = &adapter->msix_entries[vector];
  731. if (q_vector->tx.ring && q_vector->rx.ring) {
  732. snprintf(q_vector->name, sizeof(q_vector->name) - 1,
  733. "%s-%s-%d", netdev->name, "TxRx", ri++);
  734. ti++;
  735. } else if (q_vector->rx.ring) {
  736. snprintf(q_vector->name, sizeof(q_vector->name) - 1,
  737. "%s-%s-%d", netdev->name, "rx", ri++);
  738. } else if (q_vector->tx.ring) {
  739. snprintf(q_vector->name, sizeof(q_vector->name) - 1,
  740. "%s-%s-%d", netdev->name, "tx", ti++);
  741. } else {
  742. /* skip this unused q_vector */
  743. continue;
  744. }
  745. err = request_irq(entry->vector, &ixgbevf_msix_clean_rings, 0,
  746. q_vector->name, q_vector);
  747. if (err) {
  748. hw_dbg(&adapter->hw,
  749. "request_irq failed for MSIX interrupt "
  750. "Error: %d\n", err);
  751. goto free_queue_irqs;
  752. }
  753. }
  754. err = request_irq(adapter->msix_entries[vector].vector,
  755. &ixgbevf_msix_other, 0, netdev->name, adapter);
  756. if (err) {
  757. hw_dbg(&adapter->hw,
  758. "request_irq for msix_other failed: %d\n", err);
  759. goto free_queue_irqs;
  760. }
  761. return 0;
  762. free_queue_irqs:
  763. while (vector) {
  764. vector--;
  765. free_irq(adapter->msix_entries[vector].vector,
  766. adapter->q_vector[vector]);
  767. }
  768. pci_disable_msix(adapter->pdev);
  769. kfree(adapter->msix_entries);
  770. adapter->msix_entries = NULL;
  771. return err;
  772. }
  773. static inline void ixgbevf_reset_q_vectors(struct ixgbevf_adapter *adapter)
  774. {
  775. int i, q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
  776. for (i = 0; i < q_vectors; i++) {
  777. struct ixgbevf_q_vector *q_vector = adapter->q_vector[i];
  778. q_vector->rx.ring = NULL;
  779. q_vector->tx.ring = NULL;
  780. q_vector->rx.count = 0;
  781. q_vector->tx.count = 0;
  782. }
  783. }
  784. /**
  785. * ixgbevf_request_irq - initialize interrupts
  786. * @adapter: board private structure
  787. *
  788. * Attempts to configure interrupts using the best available
  789. * capabilities of the hardware and kernel.
  790. **/
  791. static int ixgbevf_request_irq(struct ixgbevf_adapter *adapter)
  792. {
  793. int err = 0;
  794. err = ixgbevf_request_msix_irqs(adapter);
  795. if (err)
  796. hw_dbg(&adapter->hw,
  797. "request_irq failed, Error %d\n", err);
  798. return err;
  799. }
  800. static void ixgbevf_free_irq(struct ixgbevf_adapter *adapter)
  801. {
  802. int i, q_vectors;
  803. q_vectors = adapter->num_msix_vectors;
  804. i = q_vectors - 1;
  805. free_irq(adapter->msix_entries[i].vector, adapter);
  806. i--;
  807. for (; i >= 0; i--) {
  808. /* free only the irqs that were actually requested */
  809. if (!adapter->q_vector[i]->rx.ring &&
  810. !adapter->q_vector[i]->tx.ring)
  811. continue;
  812. free_irq(adapter->msix_entries[i].vector,
  813. adapter->q_vector[i]);
  814. }
  815. ixgbevf_reset_q_vectors(adapter);
  816. }
  817. /**
  818. * ixgbevf_irq_disable - Mask off interrupt generation on the NIC
  819. * @adapter: board private structure
  820. **/
  821. static inline void ixgbevf_irq_disable(struct ixgbevf_adapter *adapter)
  822. {
  823. struct ixgbe_hw *hw = &adapter->hw;
  824. int i;
  825. IXGBE_WRITE_REG(hw, IXGBE_VTEIAM, 0);
  826. IXGBE_WRITE_REG(hw, IXGBE_VTEIMC, ~0);
  827. IXGBE_WRITE_REG(hw, IXGBE_VTEIAC, 0);
  828. IXGBE_WRITE_FLUSH(hw);
  829. for (i = 0; i < adapter->num_msix_vectors; i++)
  830. synchronize_irq(adapter->msix_entries[i].vector);
  831. }
  832. /**
  833. * ixgbevf_irq_enable - Enable default interrupt generation settings
  834. * @adapter: board private structure
  835. **/
  836. static inline void ixgbevf_irq_enable(struct ixgbevf_adapter *adapter)
  837. {
  838. struct ixgbe_hw *hw = &adapter->hw;
  839. IXGBE_WRITE_REG(hw, IXGBE_VTEIAM, adapter->eims_enable_mask);
  840. IXGBE_WRITE_REG(hw, IXGBE_VTEIAC, adapter->eims_enable_mask);
  841. IXGBE_WRITE_REG(hw, IXGBE_VTEIMS, adapter->eims_enable_mask);
  842. }
  843. /**
  844. * ixgbevf_configure_tx - Configure 82599 VF Transmit Unit after Reset
  845. * @adapter: board private structure
  846. *
  847. * Configure the Tx unit of the MAC after a reset.
  848. **/
  849. static void ixgbevf_configure_tx(struct ixgbevf_adapter *adapter)
  850. {
  851. u64 tdba;
  852. struct ixgbe_hw *hw = &adapter->hw;
  853. u32 i, j, tdlen, txctrl;
  854. /* Setup the HW Tx Head and Tail descriptor pointers */
  855. for (i = 0; i < adapter->num_tx_queues; i++) {
  856. struct ixgbevf_ring *ring = &adapter->tx_ring[i];
  857. j = ring->reg_idx;
  858. tdba = ring->dma;
  859. tdlen = ring->count * sizeof(union ixgbe_adv_tx_desc);
  860. IXGBE_WRITE_REG(hw, IXGBE_VFTDBAL(j),
  861. (tdba & DMA_BIT_MASK(32)));
  862. IXGBE_WRITE_REG(hw, IXGBE_VFTDBAH(j), (tdba >> 32));
  863. IXGBE_WRITE_REG(hw, IXGBE_VFTDLEN(j), tdlen);
  864. IXGBE_WRITE_REG(hw, IXGBE_VFTDH(j), 0);
  865. IXGBE_WRITE_REG(hw, IXGBE_VFTDT(j), 0);
  866. adapter->tx_ring[i].head = IXGBE_VFTDH(j);
  867. adapter->tx_ring[i].tail = IXGBE_VFTDT(j);
  868. /* Disable Tx Head Writeback RO bit, since this hoses
  869. * bookkeeping if things aren't delivered in order.
  870. */
  871. txctrl = IXGBE_READ_REG(hw, IXGBE_VFDCA_TXCTRL(j));
  872. txctrl &= ~IXGBE_DCA_TXCTRL_TX_WB_RO_EN;
  873. IXGBE_WRITE_REG(hw, IXGBE_VFDCA_TXCTRL(j), txctrl);
  874. }
  875. }
  876. #define IXGBE_SRRCTL_BSIZEHDRSIZE_SHIFT 2
  877. static void ixgbevf_configure_srrctl(struct ixgbevf_adapter *adapter, int index)
  878. {
  879. struct ixgbevf_ring *rx_ring;
  880. struct ixgbe_hw *hw = &adapter->hw;
  881. u32 srrctl;
  882. rx_ring = &adapter->rx_ring[index];
  883. srrctl = IXGBE_SRRCTL_DROP_EN;
  884. srrctl |= IXGBE_SRRCTL_DESCTYPE_ADV_ONEBUF;
  885. srrctl |= ALIGN(rx_ring->rx_buf_len, 1024) >>
  886. IXGBE_SRRCTL_BSIZEPKT_SHIFT;
  887. IXGBE_WRITE_REG(hw, IXGBE_VFSRRCTL(index), srrctl);
  888. }
  889. static void ixgbevf_set_rx_buffer_len(struct ixgbevf_adapter *adapter)
  890. {
  891. struct ixgbe_hw *hw = &adapter->hw;
  892. struct net_device *netdev = adapter->netdev;
  893. int max_frame = netdev->mtu + ETH_HLEN + ETH_FCS_LEN;
  894. int i;
  895. u16 rx_buf_len;
  896. /* notify the PF of our intent to use this size of frame */
  897. ixgbevf_rlpml_set_vf(hw, max_frame);
  898. /* PF will allow an extra 4 bytes past for vlan tagged frames */
  899. max_frame += VLAN_HLEN;
  900. /*
  901. * Make best use of allocation by using all but 1K of a
  902. * power of 2 allocation that will be used for skb->head.
  903. */
  904. if ((hw->mac.type == ixgbe_mac_X540_vf) &&
  905. (max_frame <= MAXIMUM_ETHERNET_VLAN_SIZE))
  906. rx_buf_len = MAXIMUM_ETHERNET_VLAN_SIZE;
  907. else if (max_frame <= IXGBEVF_RXBUFFER_3K)
  908. rx_buf_len = IXGBEVF_RXBUFFER_3K;
  909. else if (max_frame <= IXGBEVF_RXBUFFER_7K)
  910. rx_buf_len = IXGBEVF_RXBUFFER_7K;
  911. else if (max_frame <= IXGBEVF_RXBUFFER_15K)
  912. rx_buf_len = IXGBEVF_RXBUFFER_15K;
  913. else
  914. rx_buf_len = IXGBEVF_MAX_RXBUFFER;
  915. for (i = 0; i < adapter->num_rx_queues; i++)
  916. adapter->rx_ring[i].rx_buf_len = rx_buf_len;
  917. }
  918. /**
  919. * ixgbevf_configure_rx - Configure 82599 VF Receive Unit after Reset
  920. * @adapter: board private structure
  921. *
  922. * Configure the Rx unit of the MAC after a reset.
  923. **/
  924. static void ixgbevf_configure_rx(struct ixgbevf_adapter *adapter)
  925. {
  926. u64 rdba;
  927. struct ixgbe_hw *hw = &adapter->hw;
  928. int i, j;
  929. u32 rdlen;
  930. /* PSRTYPE must be initialized in 82599 */
  931. IXGBE_WRITE_REG(hw, IXGBE_VFPSRTYPE, 0);
  932. /* set_rx_buffer_len must be called before ring initialization */
  933. ixgbevf_set_rx_buffer_len(adapter);
  934. rdlen = adapter->rx_ring[0].count * sizeof(union ixgbe_adv_rx_desc);
  935. /* Setup the HW Rx Head and Tail Descriptor Pointers and
  936. * the Base and Length of the Rx Descriptor Ring */
  937. for (i = 0; i < adapter->num_rx_queues; i++) {
  938. rdba = adapter->rx_ring[i].dma;
  939. j = adapter->rx_ring[i].reg_idx;
  940. IXGBE_WRITE_REG(hw, IXGBE_VFRDBAL(j),
  941. (rdba & DMA_BIT_MASK(32)));
  942. IXGBE_WRITE_REG(hw, IXGBE_VFRDBAH(j), (rdba >> 32));
  943. IXGBE_WRITE_REG(hw, IXGBE_VFRDLEN(j), rdlen);
  944. IXGBE_WRITE_REG(hw, IXGBE_VFRDH(j), 0);
  945. IXGBE_WRITE_REG(hw, IXGBE_VFRDT(j), 0);
  946. adapter->rx_ring[i].head = IXGBE_VFRDH(j);
  947. adapter->rx_ring[i].tail = IXGBE_VFRDT(j);
  948. ixgbevf_configure_srrctl(adapter, j);
  949. }
  950. }
  951. static int ixgbevf_vlan_rx_add_vid(struct net_device *netdev, u16 vid)
  952. {
  953. struct ixgbevf_adapter *adapter = netdev_priv(netdev);
  954. struct ixgbe_hw *hw = &adapter->hw;
  955. int err;
  956. if (!hw->mac.ops.set_vfta)
  957. return -EOPNOTSUPP;
  958. spin_lock(&adapter->mbx_lock);
  959. /* add VID to filter table */
  960. err = hw->mac.ops.set_vfta(hw, vid, 0, true);
  961. spin_unlock(&adapter->mbx_lock);
  962. /* translate error return types so error makes sense */
  963. if (err == IXGBE_ERR_MBX)
  964. return -EIO;
  965. if (err == IXGBE_ERR_INVALID_ARGUMENT)
  966. return -EACCES;
  967. set_bit(vid, adapter->active_vlans);
  968. return err;
  969. }
  970. static int ixgbevf_vlan_rx_kill_vid(struct net_device *netdev, u16 vid)
  971. {
  972. struct ixgbevf_adapter *adapter = netdev_priv(netdev);
  973. struct ixgbe_hw *hw = &adapter->hw;
  974. int err = -EOPNOTSUPP;
  975. spin_lock(&adapter->mbx_lock);
  976. /* remove VID from filter table */
  977. if (hw->mac.ops.set_vfta)
  978. err = hw->mac.ops.set_vfta(hw, vid, 0, false);
  979. spin_unlock(&adapter->mbx_lock);
  980. clear_bit(vid, adapter->active_vlans);
  981. return err;
  982. }
  983. static void ixgbevf_restore_vlan(struct ixgbevf_adapter *adapter)
  984. {
  985. u16 vid;
  986. for_each_set_bit(vid, adapter->active_vlans, VLAN_N_VID)
  987. ixgbevf_vlan_rx_add_vid(adapter->netdev, vid);
  988. }
  989. static int ixgbevf_write_uc_addr_list(struct net_device *netdev)
  990. {
  991. struct ixgbevf_adapter *adapter = netdev_priv(netdev);
  992. struct ixgbe_hw *hw = &adapter->hw;
  993. int count = 0;
  994. if ((netdev_uc_count(netdev)) > 10) {
  995. pr_err("Too many unicast filters - No Space\n");
  996. return -ENOSPC;
  997. }
  998. if (!netdev_uc_empty(netdev)) {
  999. struct netdev_hw_addr *ha;
  1000. netdev_for_each_uc_addr(ha, netdev) {
  1001. hw->mac.ops.set_uc_addr(hw, ++count, ha->addr);
  1002. udelay(200);
  1003. }
  1004. } else {
  1005. /*
  1006. * If the list is empty then send message to PF driver to
  1007. * clear all macvlans on this VF.
  1008. */
  1009. hw->mac.ops.set_uc_addr(hw, 0, NULL);
  1010. }
  1011. return count;
  1012. }
  1013. /**
  1014. * ixgbevf_set_rx_mode - Multicast set
  1015. * @netdev: network interface device structure
  1016. *
  1017. * The set_rx_method entry point is called whenever the multicast address
  1018. * list or the network interface flags are updated. This routine is
  1019. * responsible for configuring the hardware for proper multicast mode.
  1020. **/
  1021. static void ixgbevf_set_rx_mode(struct net_device *netdev)
  1022. {
  1023. struct ixgbevf_adapter *adapter = netdev_priv(netdev);
  1024. struct ixgbe_hw *hw = &adapter->hw;
  1025. spin_lock(&adapter->mbx_lock);
  1026. /* reprogram multicast list */
  1027. if (hw->mac.ops.update_mc_addr_list)
  1028. hw->mac.ops.update_mc_addr_list(hw, netdev);
  1029. ixgbevf_write_uc_addr_list(netdev);
  1030. spin_unlock(&adapter->mbx_lock);
  1031. }
  1032. static void ixgbevf_napi_enable_all(struct ixgbevf_adapter *adapter)
  1033. {
  1034. int q_idx;
  1035. struct ixgbevf_q_vector *q_vector;
  1036. int q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
  1037. for (q_idx = 0; q_idx < q_vectors; q_idx++) {
  1038. q_vector = adapter->q_vector[q_idx];
  1039. napi_enable(&q_vector->napi);
  1040. }
  1041. }
  1042. static void ixgbevf_napi_disable_all(struct ixgbevf_adapter *adapter)
  1043. {
  1044. int q_idx;
  1045. struct ixgbevf_q_vector *q_vector;
  1046. int q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
  1047. for (q_idx = 0; q_idx < q_vectors; q_idx++) {
  1048. q_vector = adapter->q_vector[q_idx];
  1049. napi_disable(&q_vector->napi);
  1050. }
  1051. }
  1052. static void ixgbevf_configure(struct ixgbevf_adapter *adapter)
  1053. {
  1054. struct net_device *netdev = adapter->netdev;
  1055. int i;
  1056. ixgbevf_set_rx_mode(netdev);
  1057. ixgbevf_restore_vlan(adapter);
  1058. ixgbevf_configure_tx(adapter);
  1059. ixgbevf_configure_rx(adapter);
  1060. for (i = 0; i < adapter->num_rx_queues; i++) {
  1061. struct ixgbevf_ring *ring = &adapter->rx_ring[i];
  1062. ixgbevf_alloc_rx_buffers(adapter, ring,
  1063. IXGBE_DESC_UNUSED(ring));
  1064. }
  1065. }
  1066. #define IXGBE_MAX_RX_DESC_POLL 10
  1067. static inline void ixgbevf_rx_desc_queue_enable(struct ixgbevf_adapter *adapter,
  1068. int rxr)
  1069. {
  1070. struct ixgbe_hw *hw = &adapter->hw;
  1071. int j = adapter->rx_ring[rxr].reg_idx;
  1072. int k;
  1073. for (k = 0; k < IXGBE_MAX_RX_DESC_POLL; k++) {
  1074. if (IXGBE_READ_REG(hw, IXGBE_VFRXDCTL(j)) & IXGBE_RXDCTL_ENABLE)
  1075. break;
  1076. else
  1077. msleep(1);
  1078. }
  1079. if (k >= IXGBE_MAX_RX_DESC_POLL) {
  1080. hw_dbg(hw, "RXDCTL.ENABLE on Rx queue %d "
  1081. "not set within the polling period\n", rxr);
  1082. }
  1083. ixgbevf_release_rx_desc(&adapter->hw, &adapter->rx_ring[rxr],
  1084. (adapter->rx_ring[rxr].count - 1));
  1085. }
  1086. static void ixgbevf_save_reset_stats(struct ixgbevf_adapter *adapter)
  1087. {
  1088. /* Only save pre-reset stats if there are some */
  1089. if (adapter->stats.vfgprc || adapter->stats.vfgptc) {
  1090. adapter->stats.saved_reset_vfgprc += adapter->stats.vfgprc -
  1091. adapter->stats.base_vfgprc;
  1092. adapter->stats.saved_reset_vfgptc += adapter->stats.vfgptc -
  1093. adapter->stats.base_vfgptc;
  1094. adapter->stats.saved_reset_vfgorc += adapter->stats.vfgorc -
  1095. adapter->stats.base_vfgorc;
  1096. adapter->stats.saved_reset_vfgotc += adapter->stats.vfgotc -
  1097. adapter->stats.base_vfgotc;
  1098. adapter->stats.saved_reset_vfmprc += adapter->stats.vfmprc -
  1099. adapter->stats.base_vfmprc;
  1100. }
  1101. }
  1102. static void ixgbevf_init_last_counter_stats(struct ixgbevf_adapter *adapter)
  1103. {
  1104. struct ixgbe_hw *hw = &adapter->hw;
  1105. adapter->stats.last_vfgprc = IXGBE_READ_REG(hw, IXGBE_VFGPRC);
  1106. adapter->stats.last_vfgorc = IXGBE_READ_REG(hw, IXGBE_VFGORC_LSB);
  1107. adapter->stats.last_vfgorc |=
  1108. (((u64)(IXGBE_READ_REG(hw, IXGBE_VFGORC_MSB))) << 32);
  1109. adapter->stats.last_vfgptc = IXGBE_READ_REG(hw, IXGBE_VFGPTC);
  1110. adapter->stats.last_vfgotc = IXGBE_READ_REG(hw, IXGBE_VFGOTC_LSB);
  1111. adapter->stats.last_vfgotc |=
  1112. (((u64)(IXGBE_READ_REG(hw, IXGBE_VFGOTC_MSB))) << 32);
  1113. adapter->stats.last_vfmprc = IXGBE_READ_REG(hw, IXGBE_VFMPRC);
  1114. adapter->stats.base_vfgprc = adapter->stats.last_vfgprc;
  1115. adapter->stats.base_vfgorc = adapter->stats.last_vfgorc;
  1116. adapter->stats.base_vfgptc = adapter->stats.last_vfgptc;
  1117. adapter->stats.base_vfgotc = adapter->stats.last_vfgotc;
  1118. adapter->stats.base_vfmprc = adapter->stats.last_vfmprc;
  1119. }
  1120. static void ixgbevf_negotiate_api(struct ixgbevf_adapter *adapter)
  1121. {
  1122. struct ixgbe_hw *hw = &adapter->hw;
  1123. int api[] = { ixgbe_mbox_api_11,
  1124. ixgbe_mbox_api_10,
  1125. ixgbe_mbox_api_unknown };
  1126. int err = 0, idx = 0;
  1127. spin_lock(&adapter->mbx_lock);
  1128. while (api[idx] != ixgbe_mbox_api_unknown) {
  1129. err = ixgbevf_negotiate_api_version(hw, api[idx]);
  1130. if (!err)
  1131. break;
  1132. idx++;
  1133. }
  1134. spin_unlock(&adapter->mbx_lock);
  1135. }
  1136. static void ixgbevf_up_complete(struct ixgbevf_adapter *adapter)
  1137. {
  1138. struct net_device *netdev = adapter->netdev;
  1139. struct ixgbe_hw *hw = &adapter->hw;
  1140. int i, j = 0;
  1141. int num_rx_rings = adapter->num_rx_queues;
  1142. u32 txdctl, rxdctl;
  1143. for (i = 0; i < adapter->num_tx_queues; i++) {
  1144. j = adapter->tx_ring[i].reg_idx;
  1145. txdctl = IXGBE_READ_REG(hw, IXGBE_VFTXDCTL(j));
  1146. /* enable WTHRESH=8 descriptors, to encourage burst writeback */
  1147. txdctl |= (8 << 16);
  1148. IXGBE_WRITE_REG(hw, IXGBE_VFTXDCTL(j), txdctl);
  1149. }
  1150. for (i = 0; i < adapter->num_tx_queues; i++) {
  1151. j = adapter->tx_ring[i].reg_idx;
  1152. txdctl = IXGBE_READ_REG(hw, IXGBE_VFTXDCTL(j));
  1153. txdctl |= IXGBE_TXDCTL_ENABLE;
  1154. IXGBE_WRITE_REG(hw, IXGBE_VFTXDCTL(j), txdctl);
  1155. }
  1156. for (i = 0; i < num_rx_rings; i++) {
  1157. j = adapter->rx_ring[i].reg_idx;
  1158. rxdctl = IXGBE_READ_REG(hw, IXGBE_VFRXDCTL(j));
  1159. rxdctl |= IXGBE_RXDCTL_ENABLE | IXGBE_RXDCTL_VME;
  1160. if (hw->mac.type == ixgbe_mac_X540_vf) {
  1161. rxdctl &= ~IXGBE_RXDCTL_RLPMLMASK;
  1162. rxdctl |= ((netdev->mtu + ETH_HLEN + ETH_FCS_LEN) |
  1163. IXGBE_RXDCTL_RLPML_EN);
  1164. }
  1165. IXGBE_WRITE_REG(hw, IXGBE_VFRXDCTL(j), rxdctl);
  1166. ixgbevf_rx_desc_queue_enable(adapter, i);
  1167. }
  1168. ixgbevf_configure_msix(adapter);
  1169. spin_lock(&adapter->mbx_lock);
  1170. if (hw->mac.ops.set_rar) {
  1171. if (is_valid_ether_addr(hw->mac.addr))
  1172. hw->mac.ops.set_rar(hw, 0, hw->mac.addr, 0);
  1173. else
  1174. hw->mac.ops.set_rar(hw, 0, hw->mac.perm_addr, 0);
  1175. }
  1176. spin_unlock(&adapter->mbx_lock);
  1177. clear_bit(__IXGBEVF_DOWN, &adapter->state);
  1178. ixgbevf_napi_enable_all(adapter);
  1179. /* enable transmits */
  1180. netif_tx_start_all_queues(netdev);
  1181. ixgbevf_save_reset_stats(adapter);
  1182. ixgbevf_init_last_counter_stats(adapter);
  1183. hw->mac.get_link_status = 1;
  1184. mod_timer(&adapter->watchdog_timer, jiffies);
  1185. }
  1186. static int ixgbevf_reset_queues(struct ixgbevf_adapter *adapter)
  1187. {
  1188. struct ixgbe_hw *hw = &adapter->hw;
  1189. struct ixgbevf_ring *rx_ring;
  1190. unsigned int def_q = 0;
  1191. unsigned int num_tcs = 0;
  1192. unsigned int num_rx_queues = 1;
  1193. int err, i;
  1194. spin_lock(&adapter->mbx_lock);
  1195. /* fetch queue configuration from the PF */
  1196. err = ixgbevf_get_queues(hw, &num_tcs, &def_q);
  1197. spin_unlock(&adapter->mbx_lock);
  1198. if (err)
  1199. return err;
  1200. if (num_tcs > 1) {
  1201. /* update default Tx ring register index */
  1202. adapter->tx_ring[0].reg_idx = def_q;
  1203. /* we need as many queues as traffic classes */
  1204. num_rx_queues = num_tcs;
  1205. }
  1206. /* nothing to do if we have the correct number of queues */
  1207. if (adapter->num_rx_queues == num_rx_queues)
  1208. return 0;
  1209. /* allocate new rings */
  1210. rx_ring = kcalloc(num_rx_queues,
  1211. sizeof(struct ixgbevf_ring), GFP_KERNEL);
  1212. if (!rx_ring)
  1213. return -ENOMEM;
  1214. /* setup ring fields */
  1215. for (i = 0; i < num_rx_queues; i++) {
  1216. rx_ring[i].count = adapter->rx_ring_count;
  1217. rx_ring[i].queue_index = i;
  1218. rx_ring[i].reg_idx = i;
  1219. rx_ring[i].dev = &adapter->pdev->dev;
  1220. rx_ring[i].netdev = adapter->netdev;
  1221. /* allocate resources on the ring */
  1222. err = ixgbevf_setup_rx_resources(adapter, &rx_ring[i]);
  1223. if (err) {
  1224. while (i) {
  1225. i--;
  1226. ixgbevf_free_rx_resources(adapter, &rx_ring[i]);
  1227. }
  1228. kfree(rx_ring);
  1229. return err;
  1230. }
  1231. }
  1232. /* free the existing rings and queues */
  1233. ixgbevf_free_all_rx_resources(adapter);
  1234. adapter->num_rx_queues = 0;
  1235. kfree(adapter->rx_ring);
  1236. /* move new rings into position on the adapter struct */
  1237. adapter->rx_ring = rx_ring;
  1238. adapter->num_rx_queues = num_rx_queues;
  1239. /* reset ring to vector mapping */
  1240. ixgbevf_reset_q_vectors(adapter);
  1241. ixgbevf_map_rings_to_vectors(adapter);
  1242. return 0;
  1243. }
  1244. void ixgbevf_up(struct ixgbevf_adapter *adapter)
  1245. {
  1246. struct ixgbe_hw *hw = &adapter->hw;
  1247. ixgbevf_negotiate_api(adapter);
  1248. ixgbevf_reset_queues(adapter);
  1249. ixgbevf_configure(adapter);
  1250. ixgbevf_up_complete(adapter);
  1251. /* clear any pending interrupts, may auto mask */
  1252. IXGBE_READ_REG(hw, IXGBE_VTEICR);
  1253. ixgbevf_irq_enable(adapter);
  1254. }
  1255. /**
  1256. * ixgbevf_clean_rx_ring - Free Rx Buffers per Queue
  1257. * @adapter: board private structure
  1258. * @rx_ring: ring to free buffers from
  1259. **/
  1260. static void ixgbevf_clean_rx_ring(struct ixgbevf_adapter *adapter,
  1261. struct ixgbevf_ring *rx_ring)
  1262. {
  1263. struct pci_dev *pdev = adapter->pdev;
  1264. unsigned long size;
  1265. unsigned int i;
  1266. if (!rx_ring->rx_buffer_info)
  1267. return;
  1268. /* Free all the Rx ring sk_buffs */
  1269. for (i = 0; i < rx_ring->count; i++) {
  1270. struct ixgbevf_rx_buffer *rx_buffer_info;
  1271. rx_buffer_info = &rx_ring->rx_buffer_info[i];
  1272. if (rx_buffer_info->dma) {
  1273. dma_unmap_single(&pdev->dev, rx_buffer_info->dma,
  1274. rx_ring->rx_buf_len,
  1275. DMA_FROM_DEVICE);
  1276. rx_buffer_info->dma = 0;
  1277. }
  1278. if (rx_buffer_info->skb) {
  1279. struct sk_buff *skb = rx_buffer_info->skb;
  1280. rx_buffer_info->skb = NULL;
  1281. do {
  1282. struct sk_buff *this = skb;
  1283. skb = IXGBE_CB(skb)->prev;
  1284. dev_kfree_skb(this);
  1285. } while (skb);
  1286. }
  1287. }
  1288. size = sizeof(struct ixgbevf_rx_buffer) * rx_ring->count;
  1289. memset(rx_ring->rx_buffer_info, 0, size);
  1290. /* Zero out the descriptor ring */
  1291. memset(rx_ring->desc, 0, rx_ring->size);
  1292. rx_ring->next_to_clean = 0;
  1293. rx_ring->next_to_use = 0;
  1294. if (rx_ring->head)
  1295. writel(0, adapter->hw.hw_addr + rx_ring->head);
  1296. if (rx_ring->tail)
  1297. writel(0, adapter->hw.hw_addr + rx_ring->tail);
  1298. }
  1299. /**
  1300. * ixgbevf_clean_tx_ring - Free Tx Buffers
  1301. * @adapter: board private structure
  1302. * @tx_ring: ring to be cleaned
  1303. **/
  1304. static void ixgbevf_clean_tx_ring(struct ixgbevf_adapter *adapter,
  1305. struct ixgbevf_ring *tx_ring)
  1306. {
  1307. struct ixgbevf_tx_buffer *tx_buffer_info;
  1308. unsigned long size;
  1309. unsigned int i;
  1310. if (!tx_ring->tx_buffer_info)
  1311. return;
  1312. /* Free all the Tx ring sk_buffs */
  1313. for (i = 0; i < tx_ring->count; i++) {
  1314. tx_buffer_info = &tx_ring->tx_buffer_info[i];
  1315. ixgbevf_unmap_and_free_tx_resource(tx_ring, tx_buffer_info);
  1316. }
  1317. size = sizeof(struct ixgbevf_tx_buffer) * tx_ring->count;
  1318. memset(tx_ring->tx_buffer_info, 0, size);
  1319. memset(tx_ring->desc, 0, tx_ring->size);
  1320. tx_ring->next_to_use = 0;
  1321. tx_ring->next_to_clean = 0;
  1322. if (tx_ring->head)
  1323. writel(0, adapter->hw.hw_addr + tx_ring->head);
  1324. if (tx_ring->tail)
  1325. writel(0, adapter->hw.hw_addr + tx_ring->tail);
  1326. }
  1327. /**
  1328. * ixgbevf_clean_all_rx_rings - Free Rx Buffers for all queues
  1329. * @adapter: board private structure
  1330. **/
  1331. static void ixgbevf_clean_all_rx_rings(struct ixgbevf_adapter *adapter)
  1332. {
  1333. int i;
  1334. for (i = 0; i < adapter->num_rx_queues; i++)
  1335. ixgbevf_clean_rx_ring(adapter, &adapter->rx_ring[i]);
  1336. }
  1337. /**
  1338. * ixgbevf_clean_all_tx_rings - Free Tx Buffers for all queues
  1339. * @adapter: board private structure
  1340. **/
  1341. static void ixgbevf_clean_all_tx_rings(struct ixgbevf_adapter *adapter)
  1342. {
  1343. int i;
  1344. for (i = 0; i < adapter->num_tx_queues; i++)
  1345. ixgbevf_clean_tx_ring(adapter, &adapter->tx_ring[i]);
  1346. }
  1347. void ixgbevf_down(struct ixgbevf_adapter *adapter)
  1348. {
  1349. struct net_device *netdev = adapter->netdev;
  1350. struct ixgbe_hw *hw = &adapter->hw;
  1351. u32 txdctl;
  1352. int i, j;
  1353. /* signal that we are down to the interrupt handler */
  1354. set_bit(__IXGBEVF_DOWN, &adapter->state);
  1355. /* disable receives */
  1356. netif_tx_disable(netdev);
  1357. msleep(10);
  1358. netif_tx_stop_all_queues(netdev);
  1359. ixgbevf_irq_disable(adapter);
  1360. ixgbevf_napi_disable_all(adapter);
  1361. del_timer_sync(&adapter->watchdog_timer);
  1362. /* can't call flush scheduled work here because it can deadlock
  1363. * if linkwatch_event tries to acquire the rtnl_lock which we are
  1364. * holding */
  1365. while (adapter->flags & IXGBE_FLAG_IN_WATCHDOG_TASK)
  1366. msleep(1);
  1367. /* disable transmits in the hardware now that interrupts are off */
  1368. for (i = 0; i < adapter->num_tx_queues; i++) {
  1369. j = adapter->tx_ring[i].reg_idx;
  1370. txdctl = IXGBE_READ_REG(hw, IXGBE_VFTXDCTL(j));
  1371. IXGBE_WRITE_REG(hw, IXGBE_VFTXDCTL(j),
  1372. (txdctl & ~IXGBE_TXDCTL_ENABLE));
  1373. }
  1374. netif_carrier_off(netdev);
  1375. if (!pci_channel_offline(adapter->pdev))
  1376. ixgbevf_reset(adapter);
  1377. ixgbevf_clean_all_tx_rings(adapter);
  1378. ixgbevf_clean_all_rx_rings(adapter);
  1379. }
  1380. void ixgbevf_reinit_locked(struct ixgbevf_adapter *adapter)
  1381. {
  1382. WARN_ON(in_interrupt());
  1383. while (test_and_set_bit(__IXGBEVF_RESETTING, &adapter->state))
  1384. msleep(1);
  1385. /*
  1386. * Check if PF is up before re-init. If not then skip until
  1387. * later when the PF is up and ready to service requests from
  1388. * the VF via mailbox. If the VF is up and running then the
  1389. * watchdog task will continue to schedule reset tasks until
  1390. * the PF is up and running.
  1391. */
  1392. ixgbevf_down(adapter);
  1393. ixgbevf_up(adapter);
  1394. clear_bit(__IXGBEVF_RESETTING, &adapter->state);
  1395. }
  1396. void ixgbevf_reset(struct ixgbevf_adapter *adapter)
  1397. {
  1398. struct ixgbe_hw *hw = &adapter->hw;
  1399. struct net_device *netdev = adapter->netdev;
  1400. spin_lock(&adapter->mbx_lock);
  1401. if (hw->mac.ops.reset_hw(hw))
  1402. hw_dbg(hw, "PF still resetting\n");
  1403. else
  1404. hw->mac.ops.init_hw(hw);
  1405. spin_unlock(&adapter->mbx_lock);
  1406. if (is_valid_ether_addr(adapter->hw.mac.addr)) {
  1407. memcpy(netdev->dev_addr, adapter->hw.mac.addr,
  1408. netdev->addr_len);
  1409. memcpy(netdev->perm_addr, adapter->hw.mac.addr,
  1410. netdev->addr_len);
  1411. }
  1412. }
  1413. static void ixgbevf_acquire_msix_vectors(struct ixgbevf_adapter *adapter,
  1414. int vectors)
  1415. {
  1416. int err, vector_threshold;
  1417. /* We'll want at least 2 (vector_threshold):
  1418. * 1) TxQ[0] + RxQ[0] handler
  1419. * 2) Other (Link Status Change, etc.)
  1420. */
  1421. vector_threshold = MIN_MSIX_COUNT;
  1422. /* The more we get, the more we will assign to Tx/Rx Cleanup
  1423. * for the separate queues...where Rx Cleanup >= Tx Cleanup.
  1424. * Right now, we simply care about how many we'll get; we'll
  1425. * set them up later while requesting irq's.
  1426. */
  1427. while (vectors >= vector_threshold) {
  1428. err = pci_enable_msix(adapter->pdev, adapter->msix_entries,
  1429. vectors);
  1430. if (!err) /* Success in acquiring all requested vectors. */
  1431. break;
  1432. else if (err < 0)
  1433. vectors = 0; /* Nasty failure, quit now */
  1434. else /* err == number of vectors we should try again with */
  1435. vectors = err;
  1436. }
  1437. if (vectors < vector_threshold) {
  1438. /* Can't allocate enough MSI-X interrupts? Oh well.
  1439. * This just means we'll go with either a single MSI
  1440. * vector or fall back to legacy interrupts.
  1441. */
  1442. hw_dbg(&adapter->hw,
  1443. "Unable to allocate MSI-X interrupts\n");
  1444. kfree(adapter->msix_entries);
  1445. adapter->msix_entries = NULL;
  1446. } else {
  1447. /*
  1448. * Adjust for only the vectors we'll use, which is minimum
  1449. * of max_msix_q_vectors + NON_Q_VECTORS, or the number of
  1450. * vectors we were allocated.
  1451. */
  1452. adapter->num_msix_vectors = vectors;
  1453. }
  1454. }
  1455. /**
  1456. * ixgbevf_set_num_queues - Allocate queues for device, feature dependent
  1457. * @adapter: board private structure to initialize
  1458. *
  1459. * This is the top level queue allocation routine. The order here is very
  1460. * important, starting with the "most" number of features turned on at once,
  1461. * and ending with the smallest set of features. This way large combinations
  1462. * can be allocated if they're turned on, and smaller combinations are the
  1463. * fallthrough conditions.
  1464. *
  1465. **/
  1466. static void ixgbevf_set_num_queues(struct ixgbevf_adapter *adapter)
  1467. {
  1468. /* Start with base case */
  1469. adapter->num_rx_queues = 1;
  1470. adapter->num_tx_queues = 1;
  1471. }
  1472. /**
  1473. * ixgbevf_alloc_queues - Allocate memory for all rings
  1474. * @adapter: board private structure to initialize
  1475. *
  1476. * We allocate one ring per queue at run-time since we don't know the
  1477. * number of queues at compile-time. The polling_netdev array is
  1478. * intended for Multiqueue, but should work fine with a single queue.
  1479. **/
  1480. static int ixgbevf_alloc_queues(struct ixgbevf_adapter *adapter)
  1481. {
  1482. int i;
  1483. adapter->tx_ring = kcalloc(adapter->num_tx_queues,
  1484. sizeof(struct ixgbevf_ring), GFP_KERNEL);
  1485. if (!adapter->tx_ring)
  1486. goto err_tx_ring_allocation;
  1487. adapter->rx_ring = kcalloc(adapter->num_rx_queues,
  1488. sizeof(struct ixgbevf_ring), GFP_KERNEL);
  1489. if (!adapter->rx_ring)
  1490. goto err_rx_ring_allocation;
  1491. for (i = 0; i < adapter->num_tx_queues; i++) {
  1492. adapter->tx_ring[i].count = adapter->tx_ring_count;
  1493. adapter->tx_ring[i].queue_index = i;
  1494. /* reg_idx may be remapped later by DCB config */
  1495. adapter->tx_ring[i].reg_idx = i;
  1496. adapter->tx_ring[i].dev = &adapter->pdev->dev;
  1497. adapter->tx_ring[i].netdev = adapter->netdev;
  1498. }
  1499. for (i = 0; i < adapter->num_rx_queues; i++) {
  1500. adapter->rx_ring[i].count = adapter->rx_ring_count;
  1501. adapter->rx_ring[i].queue_index = i;
  1502. adapter->rx_ring[i].reg_idx = i;
  1503. adapter->rx_ring[i].dev = &adapter->pdev->dev;
  1504. adapter->rx_ring[i].netdev = adapter->netdev;
  1505. }
  1506. return 0;
  1507. err_rx_ring_allocation:
  1508. kfree(adapter->tx_ring);
  1509. err_tx_ring_allocation:
  1510. return -ENOMEM;
  1511. }
  1512. /**
  1513. * ixgbevf_set_interrupt_capability - set MSI-X or FAIL if not supported
  1514. * @adapter: board private structure to initialize
  1515. *
  1516. * Attempt to configure the interrupts using the best available
  1517. * capabilities of the hardware and the kernel.
  1518. **/
  1519. static int ixgbevf_set_interrupt_capability(struct ixgbevf_adapter *adapter)
  1520. {
  1521. struct net_device *netdev = adapter->netdev;
  1522. int err = 0;
  1523. int vector, v_budget;
  1524. /*
  1525. * It's easy to be greedy for MSI-X vectors, but it really
  1526. * doesn't do us much good if we have a lot more vectors
  1527. * than CPU's. So let's be conservative and only ask for
  1528. * (roughly) the same number of vectors as there are CPU's.
  1529. * The default is to use pairs of vectors.
  1530. */
  1531. v_budget = max(adapter->num_rx_queues, adapter->num_tx_queues);
  1532. v_budget = min_t(int, v_budget, num_online_cpus());
  1533. v_budget += NON_Q_VECTORS;
  1534. /* A failure in MSI-X entry allocation isn't fatal, but it does
  1535. * mean we disable MSI-X capabilities of the adapter. */
  1536. adapter->msix_entries = kcalloc(v_budget,
  1537. sizeof(struct msix_entry), GFP_KERNEL);
  1538. if (!adapter->msix_entries) {
  1539. err = -ENOMEM;
  1540. goto out;
  1541. }
  1542. for (vector = 0; vector < v_budget; vector++)
  1543. adapter->msix_entries[vector].entry = vector;
  1544. ixgbevf_acquire_msix_vectors(adapter, v_budget);
  1545. err = netif_set_real_num_tx_queues(netdev, adapter->num_tx_queues);
  1546. if (err)
  1547. goto out;
  1548. err = netif_set_real_num_rx_queues(netdev, adapter->num_rx_queues);
  1549. out:
  1550. return err;
  1551. }
  1552. /**
  1553. * ixgbevf_alloc_q_vectors - Allocate memory for interrupt vectors
  1554. * @adapter: board private structure to initialize
  1555. *
  1556. * We allocate one q_vector per queue interrupt. If allocation fails we
  1557. * return -ENOMEM.
  1558. **/
  1559. static int ixgbevf_alloc_q_vectors(struct ixgbevf_adapter *adapter)
  1560. {
  1561. int q_idx, num_q_vectors;
  1562. struct ixgbevf_q_vector *q_vector;
  1563. num_q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
  1564. for (q_idx = 0; q_idx < num_q_vectors; q_idx++) {
  1565. q_vector = kzalloc(sizeof(struct ixgbevf_q_vector), GFP_KERNEL);
  1566. if (!q_vector)
  1567. goto err_out;
  1568. q_vector->adapter = adapter;
  1569. q_vector->v_idx = q_idx;
  1570. netif_napi_add(adapter->netdev, &q_vector->napi,
  1571. ixgbevf_poll, 64);
  1572. adapter->q_vector[q_idx] = q_vector;
  1573. }
  1574. return 0;
  1575. err_out:
  1576. while (q_idx) {
  1577. q_idx--;
  1578. q_vector = adapter->q_vector[q_idx];
  1579. netif_napi_del(&q_vector->napi);
  1580. kfree(q_vector);
  1581. adapter->q_vector[q_idx] = NULL;
  1582. }
  1583. return -ENOMEM;
  1584. }
  1585. /**
  1586. * ixgbevf_free_q_vectors - Free memory allocated for interrupt vectors
  1587. * @adapter: board private structure to initialize
  1588. *
  1589. * This function frees the memory allocated to the q_vectors. In addition if
  1590. * NAPI is enabled it will delete any references to the NAPI struct prior
  1591. * to freeing the q_vector.
  1592. **/
  1593. static void ixgbevf_free_q_vectors(struct ixgbevf_adapter *adapter)
  1594. {
  1595. int q_idx, num_q_vectors;
  1596. int napi_vectors;
  1597. num_q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
  1598. napi_vectors = adapter->num_rx_queues;
  1599. for (q_idx = 0; q_idx < num_q_vectors; q_idx++) {
  1600. struct ixgbevf_q_vector *q_vector = adapter->q_vector[q_idx];
  1601. adapter->q_vector[q_idx] = NULL;
  1602. if (q_idx < napi_vectors)
  1603. netif_napi_del(&q_vector->napi);
  1604. kfree(q_vector);
  1605. }
  1606. }
  1607. /**
  1608. * ixgbevf_reset_interrupt_capability - Reset MSIX setup
  1609. * @adapter: board private structure
  1610. *
  1611. **/
  1612. static void ixgbevf_reset_interrupt_capability(struct ixgbevf_adapter *adapter)
  1613. {
  1614. pci_disable_msix(adapter->pdev);
  1615. kfree(adapter->msix_entries);
  1616. adapter->msix_entries = NULL;
  1617. }
  1618. /**
  1619. * ixgbevf_init_interrupt_scheme - Determine if MSIX is supported and init
  1620. * @adapter: board private structure to initialize
  1621. *
  1622. **/
  1623. static int ixgbevf_init_interrupt_scheme(struct ixgbevf_adapter *adapter)
  1624. {
  1625. int err;
  1626. /* Number of supported queues */
  1627. ixgbevf_set_num_queues(adapter);
  1628. err = ixgbevf_set_interrupt_capability(adapter);
  1629. if (err) {
  1630. hw_dbg(&adapter->hw,
  1631. "Unable to setup interrupt capabilities\n");
  1632. goto err_set_interrupt;
  1633. }
  1634. err = ixgbevf_alloc_q_vectors(adapter);
  1635. if (err) {
  1636. hw_dbg(&adapter->hw, "Unable to allocate memory for queue "
  1637. "vectors\n");
  1638. goto err_alloc_q_vectors;
  1639. }
  1640. err = ixgbevf_alloc_queues(adapter);
  1641. if (err) {
  1642. pr_err("Unable to allocate memory for queues\n");
  1643. goto err_alloc_queues;
  1644. }
  1645. hw_dbg(&adapter->hw, "Multiqueue %s: Rx Queue count = %u, "
  1646. "Tx Queue count = %u\n",
  1647. (adapter->num_rx_queues > 1) ? "Enabled" :
  1648. "Disabled", adapter->num_rx_queues, adapter->num_tx_queues);
  1649. set_bit(__IXGBEVF_DOWN, &adapter->state);
  1650. return 0;
  1651. err_alloc_queues:
  1652. ixgbevf_free_q_vectors(adapter);
  1653. err_alloc_q_vectors:
  1654. ixgbevf_reset_interrupt_capability(adapter);
  1655. err_set_interrupt:
  1656. return err;
  1657. }
  1658. /**
  1659. * ixgbevf_clear_interrupt_scheme - Clear the current interrupt scheme settings
  1660. * @adapter: board private structure to clear interrupt scheme on
  1661. *
  1662. * We go through and clear interrupt specific resources and reset the structure
  1663. * to pre-load conditions
  1664. **/
  1665. static void ixgbevf_clear_interrupt_scheme(struct ixgbevf_adapter *adapter)
  1666. {
  1667. adapter->num_tx_queues = 0;
  1668. adapter->num_rx_queues = 0;
  1669. ixgbevf_free_q_vectors(adapter);
  1670. ixgbevf_reset_interrupt_capability(adapter);
  1671. }
  1672. /**
  1673. * ixgbevf_sw_init - Initialize general software structures
  1674. * (struct ixgbevf_adapter)
  1675. * @adapter: board private structure to initialize
  1676. *
  1677. * ixgbevf_sw_init initializes the Adapter private data structure.
  1678. * Fields are initialized based on PCI device information and
  1679. * OS network device settings (MTU size).
  1680. **/
  1681. static int __devinit ixgbevf_sw_init(struct ixgbevf_adapter *adapter)
  1682. {
  1683. struct ixgbe_hw *hw = &adapter->hw;
  1684. struct pci_dev *pdev = adapter->pdev;
  1685. int err;
  1686. /* PCI config space info */
  1687. hw->vendor_id = pdev->vendor;
  1688. hw->device_id = pdev->device;
  1689. hw->revision_id = pdev->revision;
  1690. hw->subsystem_vendor_id = pdev->subsystem_vendor;
  1691. hw->subsystem_device_id = pdev->subsystem_device;
  1692. hw->mbx.ops.init_params(hw);
  1693. /* assume legacy case in which PF would only give VF 2 queues */
  1694. hw->mac.max_tx_queues = 2;
  1695. hw->mac.max_rx_queues = 2;
  1696. err = hw->mac.ops.reset_hw(hw);
  1697. if (err) {
  1698. dev_info(&pdev->dev,
  1699. "PF still in reset state, assigning new address\n");
  1700. eth_hw_addr_random(adapter->netdev);
  1701. memcpy(adapter->hw.mac.addr, adapter->netdev->dev_addr,
  1702. adapter->netdev->addr_len);
  1703. } else {
  1704. err = hw->mac.ops.init_hw(hw);
  1705. if (err) {
  1706. pr_err("init_shared_code failed: %d\n", err);
  1707. goto out;
  1708. }
  1709. memcpy(adapter->netdev->dev_addr, adapter->hw.mac.addr,
  1710. adapter->netdev->addr_len);
  1711. }
  1712. /* lock to protect mailbox accesses */
  1713. spin_lock_init(&adapter->mbx_lock);
  1714. /* Enable dynamic interrupt throttling rates */
  1715. adapter->rx_itr_setting = 1;
  1716. adapter->tx_itr_setting = 1;
  1717. /* set default ring sizes */
  1718. adapter->tx_ring_count = IXGBEVF_DEFAULT_TXD;
  1719. adapter->rx_ring_count = IXGBEVF_DEFAULT_RXD;
  1720. set_bit(__IXGBEVF_DOWN, &adapter->state);
  1721. return 0;
  1722. out:
  1723. return err;
  1724. }
  1725. #define UPDATE_VF_COUNTER_32bit(reg, last_counter, counter) \
  1726. { \
  1727. u32 current_counter = IXGBE_READ_REG(hw, reg); \
  1728. if (current_counter < last_counter) \
  1729. counter += 0x100000000LL; \
  1730. last_counter = current_counter; \
  1731. counter &= 0xFFFFFFFF00000000LL; \
  1732. counter |= current_counter; \
  1733. }
  1734. #define UPDATE_VF_COUNTER_36bit(reg_lsb, reg_msb, last_counter, counter) \
  1735. { \
  1736. u64 current_counter_lsb = IXGBE_READ_REG(hw, reg_lsb); \
  1737. u64 current_counter_msb = IXGBE_READ_REG(hw, reg_msb); \
  1738. u64 current_counter = (current_counter_msb << 32) | \
  1739. current_counter_lsb; \
  1740. if (current_counter < last_counter) \
  1741. counter += 0x1000000000LL; \
  1742. last_counter = current_counter; \
  1743. counter &= 0xFFFFFFF000000000LL; \
  1744. counter |= current_counter; \
  1745. }
  1746. /**
  1747. * ixgbevf_update_stats - Update the board statistics counters.
  1748. * @adapter: board private structure
  1749. **/
  1750. void ixgbevf_update_stats(struct ixgbevf_adapter *adapter)
  1751. {
  1752. struct ixgbe_hw *hw = &adapter->hw;
  1753. UPDATE_VF_COUNTER_32bit(IXGBE_VFGPRC, adapter->stats.last_vfgprc,
  1754. adapter->stats.vfgprc);
  1755. UPDATE_VF_COUNTER_32bit(IXGBE_VFGPTC, adapter->stats.last_vfgptc,
  1756. adapter->stats.vfgptc);
  1757. UPDATE_VF_COUNTER_36bit(IXGBE_VFGORC_LSB, IXGBE_VFGORC_MSB,
  1758. adapter->stats.last_vfgorc,
  1759. adapter->stats.vfgorc);
  1760. UPDATE_VF_COUNTER_36bit(IXGBE_VFGOTC_LSB, IXGBE_VFGOTC_MSB,
  1761. adapter->stats.last_vfgotc,
  1762. adapter->stats.vfgotc);
  1763. UPDATE_VF_COUNTER_32bit(IXGBE_VFMPRC, adapter->stats.last_vfmprc,
  1764. adapter->stats.vfmprc);
  1765. }
  1766. /**
  1767. * ixgbevf_watchdog - Timer Call-back
  1768. * @data: pointer to adapter cast into an unsigned long
  1769. **/
  1770. static void ixgbevf_watchdog(unsigned long data)
  1771. {
  1772. struct ixgbevf_adapter *adapter = (struct ixgbevf_adapter *)data;
  1773. struct ixgbe_hw *hw = &adapter->hw;
  1774. u32 eics = 0;
  1775. int i;
  1776. /*
  1777. * Do the watchdog outside of interrupt context due to the lovely
  1778. * delays that some of the newer hardware requires
  1779. */
  1780. if (test_bit(__IXGBEVF_DOWN, &adapter->state))
  1781. goto watchdog_short_circuit;
  1782. /* get one bit for every active tx/rx interrupt vector */
  1783. for (i = 0; i < adapter->num_msix_vectors - NON_Q_VECTORS; i++) {
  1784. struct ixgbevf_q_vector *qv = adapter->q_vector[i];
  1785. if (qv->rx.ring || qv->tx.ring)
  1786. eics |= 1 << i;
  1787. }
  1788. IXGBE_WRITE_REG(hw, IXGBE_VTEICS, eics);
  1789. watchdog_short_circuit:
  1790. schedule_work(&adapter->watchdog_task);
  1791. }
  1792. /**
  1793. * ixgbevf_tx_timeout - Respond to a Tx Hang
  1794. * @netdev: network interface device structure
  1795. **/
  1796. static void ixgbevf_tx_timeout(struct net_device *netdev)
  1797. {
  1798. struct ixgbevf_adapter *adapter = netdev_priv(netdev);
  1799. /* Do the reset outside of interrupt context */
  1800. schedule_work(&adapter->reset_task);
  1801. }
  1802. static void ixgbevf_reset_task(struct work_struct *work)
  1803. {
  1804. struct ixgbevf_adapter *adapter;
  1805. adapter = container_of(work, struct ixgbevf_adapter, reset_task);
  1806. /* If we're already down or resetting, just bail */
  1807. if (test_bit(__IXGBEVF_DOWN, &adapter->state) ||
  1808. test_bit(__IXGBEVF_RESETTING, &adapter->state))
  1809. return;
  1810. adapter->tx_timeout_count++;
  1811. ixgbevf_reinit_locked(adapter);
  1812. }
  1813. /**
  1814. * ixgbevf_watchdog_task - worker thread to bring link up
  1815. * @work: pointer to work_struct containing our data
  1816. **/
  1817. static void ixgbevf_watchdog_task(struct work_struct *work)
  1818. {
  1819. struct ixgbevf_adapter *adapter = container_of(work,
  1820. struct ixgbevf_adapter,
  1821. watchdog_task);
  1822. struct net_device *netdev = adapter->netdev;
  1823. struct ixgbe_hw *hw = &adapter->hw;
  1824. u32 link_speed = adapter->link_speed;
  1825. bool link_up = adapter->link_up;
  1826. adapter->flags |= IXGBE_FLAG_IN_WATCHDOG_TASK;
  1827. /*
  1828. * Always check the link on the watchdog because we have
  1829. * no LSC interrupt
  1830. */
  1831. if (hw->mac.ops.check_link) {
  1832. s32 need_reset;
  1833. spin_lock(&adapter->mbx_lock);
  1834. need_reset = hw->mac.ops.check_link(hw, &link_speed,
  1835. &link_up, false);
  1836. spin_unlock(&adapter->mbx_lock);
  1837. if (need_reset) {
  1838. adapter->link_up = link_up;
  1839. adapter->link_speed = link_speed;
  1840. netif_carrier_off(netdev);
  1841. netif_tx_stop_all_queues(netdev);
  1842. schedule_work(&adapter->reset_task);
  1843. goto pf_has_reset;
  1844. }
  1845. } else {
  1846. /* always assume link is up, if no check link
  1847. * function */
  1848. link_speed = IXGBE_LINK_SPEED_10GB_FULL;
  1849. link_up = true;
  1850. }
  1851. adapter->link_up = link_up;
  1852. adapter->link_speed = link_speed;
  1853. if (link_up) {
  1854. if (!netif_carrier_ok(netdev)) {
  1855. hw_dbg(&adapter->hw, "NIC Link is Up, %u Gbps\n",
  1856. (link_speed == IXGBE_LINK_SPEED_10GB_FULL) ?
  1857. 10 : 1);
  1858. netif_carrier_on(netdev);
  1859. netif_tx_wake_all_queues(netdev);
  1860. }
  1861. } else {
  1862. adapter->link_up = false;
  1863. adapter->link_speed = 0;
  1864. if (netif_carrier_ok(netdev)) {
  1865. hw_dbg(&adapter->hw, "NIC Link is Down\n");
  1866. netif_carrier_off(netdev);
  1867. netif_tx_stop_all_queues(netdev);
  1868. }
  1869. }
  1870. ixgbevf_update_stats(adapter);
  1871. pf_has_reset:
  1872. /* Reset the timer */
  1873. if (!test_bit(__IXGBEVF_DOWN, &adapter->state))
  1874. mod_timer(&adapter->watchdog_timer,
  1875. round_jiffies(jiffies + (2 * HZ)));
  1876. adapter->flags &= ~IXGBE_FLAG_IN_WATCHDOG_TASK;
  1877. }
  1878. /**
  1879. * ixgbevf_free_tx_resources - Free Tx Resources per Queue
  1880. * @adapter: board private structure
  1881. * @tx_ring: Tx descriptor ring for a specific queue
  1882. *
  1883. * Free all transmit software resources
  1884. **/
  1885. void ixgbevf_free_tx_resources(struct ixgbevf_adapter *adapter,
  1886. struct ixgbevf_ring *tx_ring)
  1887. {
  1888. struct pci_dev *pdev = adapter->pdev;
  1889. ixgbevf_clean_tx_ring(adapter, tx_ring);
  1890. vfree(tx_ring->tx_buffer_info);
  1891. tx_ring->tx_buffer_info = NULL;
  1892. dma_free_coherent(&pdev->dev, tx_ring->size, tx_ring->desc,
  1893. tx_ring->dma);
  1894. tx_ring->desc = NULL;
  1895. }
  1896. /**
  1897. * ixgbevf_free_all_tx_resources - Free Tx Resources for All Queues
  1898. * @adapter: board private structure
  1899. *
  1900. * Free all transmit software resources
  1901. **/
  1902. static void ixgbevf_free_all_tx_resources(struct ixgbevf_adapter *adapter)
  1903. {
  1904. int i;
  1905. for (i = 0; i < adapter->num_tx_queues; i++)
  1906. if (adapter->tx_ring[i].desc)
  1907. ixgbevf_free_tx_resources(adapter,
  1908. &adapter->tx_ring[i]);
  1909. }
  1910. /**
  1911. * ixgbevf_setup_tx_resources - allocate Tx resources (Descriptors)
  1912. * @adapter: board private structure
  1913. * @tx_ring: tx descriptor ring (for a specific queue) to setup
  1914. *
  1915. * Return 0 on success, negative on failure
  1916. **/
  1917. int ixgbevf_setup_tx_resources(struct ixgbevf_adapter *adapter,
  1918. struct ixgbevf_ring *tx_ring)
  1919. {
  1920. struct pci_dev *pdev = adapter->pdev;
  1921. int size;
  1922. size = sizeof(struct ixgbevf_tx_buffer) * tx_ring->count;
  1923. tx_ring->tx_buffer_info = vzalloc(size);
  1924. if (!tx_ring->tx_buffer_info)
  1925. goto err;
  1926. /* round up to nearest 4K */
  1927. tx_ring->size = tx_ring->count * sizeof(union ixgbe_adv_tx_desc);
  1928. tx_ring->size = ALIGN(tx_ring->size, 4096);
  1929. tx_ring->desc = dma_alloc_coherent(&pdev->dev, tx_ring->size,
  1930. &tx_ring->dma, GFP_KERNEL);
  1931. if (!tx_ring->desc)
  1932. goto err;
  1933. tx_ring->next_to_use = 0;
  1934. tx_ring->next_to_clean = 0;
  1935. return 0;
  1936. err:
  1937. vfree(tx_ring->tx_buffer_info);
  1938. tx_ring->tx_buffer_info = NULL;
  1939. hw_dbg(&adapter->hw, "Unable to allocate memory for the transmit "
  1940. "descriptor ring\n");
  1941. return -ENOMEM;
  1942. }
  1943. /**
  1944. * ixgbevf_setup_all_tx_resources - allocate all queues Tx resources
  1945. * @adapter: board private structure
  1946. *
  1947. * If this function returns with an error, then it's possible one or
  1948. * more of the rings is populated (while the rest are not). It is the
  1949. * callers duty to clean those orphaned rings.
  1950. *
  1951. * Return 0 on success, negative on failure
  1952. **/
  1953. static int ixgbevf_setup_all_tx_resources(struct ixgbevf_adapter *adapter)
  1954. {
  1955. int i, err = 0;
  1956. for (i = 0; i < adapter->num_tx_queues; i++) {
  1957. err = ixgbevf_setup_tx_resources(adapter, &adapter->tx_ring[i]);
  1958. if (!err)
  1959. continue;
  1960. hw_dbg(&adapter->hw,
  1961. "Allocation for Tx Queue %u failed\n", i);
  1962. break;
  1963. }
  1964. return err;
  1965. }
  1966. /**
  1967. * ixgbevf_setup_rx_resources - allocate Rx resources (Descriptors)
  1968. * @adapter: board private structure
  1969. * @rx_ring: rx descriptor ring (for a specific queue) to setup
  1970. *
  1971. * Returns 0 on success, negative on failure
  1972. **/
  1973. int ixgbevf_setup_rx_resources(struct ixgbevf_adapter *adapter,
  1974. struct ixgbevf_ring *rx_ring)
  1975. {
  1976. struct pci_dev *pdev = adapter->pdev;
  1977. int size;
  1978. size = sizeof(struct ixgbevf_rx_buffer) * rx_ring->count;
  1979. rx_ring->rx_buffer_info = vzalloc(size);
  1980. if (!rx_ring->rx_buffer_info)
  1981. goto alloc_failed;
  1982. /* Round up to nearest 4K */
  1983. rx_ring->size = rx_ring->count * sizeof(union ixgbe_adv_rx_desc);
  1984. rx_ring->size = ALIGN(rx_ring->size, 4096);
  1985. rx_ring->desc = dma_alloc_coherent(&pdev->dev, rx_ring->size,
  1986. &rx_ring->dma, GFP_KERNEL);
  1987. if (!rx_ring->desc) {
  1988. hw_dbg(&adapter->hw,
  1989. "Unable to allocate memory for "
  1990. "the receive descriptor ring\n");
  1991. vfree(rx_ring->rx_buffer_info);
  1992. rx_ring->rx_buffer_info = NULL;
  1993. goto alloc_failed;
  1994. }
  1995. rx_ring->next_to_clean = 0;
  1996. rx_ring->next_to_use = 0;
  1997. return 0;
  1998. alloc_failed:
  1999. return -ENOMEM;
  2000. }
  2001. /**
  2002. * ixgbevf_setup_all_rx_resources - allocate all queues Rx resources
  2003. * @adapter: board private structure
  2004. *
  2005. * If this function returns with an error, then it's possible one or
  2006. * more of the rings is populated (while the rest are not). It is the
  2007. * callers duty to clean those orphaned rings.
  2008. *
  2009. * Return 0 on success, negative on failure
  2010. **/
  2011. static int ixgbevf_setup_all_rx_resources(struct ixgbevf_adapter *adapter)
  2012. {
  2013. int i, err = 0;
  2014. for (i = 0; i < adapter->num_rx_queues; i++) {
  2015. err = ixgbevf_setup_rx_resources(adapter, &adapter->rx_ring[i]);
  2016. if (!err)
  2017. continue;
  2018. hw_dbg(&adapter->hw,
  2019. "Allocation for Rx Queue %u failed\n", i);
  2020. break;
  2021. }
  2022. return err;
  2023. }
  2024. /**
  2025. * ixgbevf_free_rx_resources - Free Rx Resources
  2026. * @adapter: board private structure
  2027. * @rx_ring: ring to clean the resources from
  2028. *
  2029. * Free all receive software resources
  2030. **/
  2031. void ixgbevf_free_rx_resources(struct ixgbevf_adapter *adapter,
  2032. struct ixgbevf_ring *rx_ring)
  2033. {
  2034. struct pci_dev *pdev = adapter->pdev;
  2035. ixgbevf_clean_rx_ring(adapter, rx_ring);
  2036. vfree(rx_ring->rx_buffer_info);
  2037. rx_ring->rx_buffer_info = NULL;
  2038. dma_free_coherent(&pdev->dev, rx_ring->size, rx_ring->desc,
  2039. rx_ring->dma);
  2040. rx_ring->desc = NULL;
  2041. }
  2042. /**
  2043. * ixgbevf_free_all_rx_resources - Free Rx Resources for All Queues
  2044. * @adapter: board private structure
  2045. *
  2046. * Free all receive software resources
  2047. **/
  2048. static void ixgbevf_free_all_rx_resources(struct ixgbevf_adapter *adapter)
  2049. {
  2050. int i;
  2051. for (i = 0; i < adapter->num_rx_queues; i++)
  2052. if (adapter->rx_ring[i].desc)
  2053. ixgbevf_free_rx_resources(adapter,
  2054. &adapter->rx_ring[i]);
  2055. }
  2056. static int ixgbevf_setup_queues(struct ixgbevf_adapter *adapter)
  2057. {
  2058. struct ixgbe_hw *hw = &adapter->hw;
  2059. struct ixgbevf_ring *rx_ring;
  2060. unsigned int def_q = 0;
  2061. unsigned int num_tcs = 0;
  2062. unsigned int num_rx_queues = 1;
  2063. int err, i;
  2064. spin_lock(&adapter->mbx_lock);
  2065. /* fetch queue configuration from the PF */
  2066. err = ixgbevf_get_queues(hw, &num_tcs, &def_q);
  2067. spin_unlock(&adapter->mbx_lock);
  2068. if (err)
  2069. return err;
  2070. if (num_tcs > 1) {
  2071. /* update default Tx ring register index */
  2072. adapter->tx_ring[0].reg_idx = def_q;
  2073. /* we need as many queues as traffic classes */
  2074. num_rx_queues = num_tcs;
  2075. }
  2076. /* nothing to do if we have the correct number of queues */
  2077. if (adapter->num_rx_queues == num_rx_queues)
  2078. return 0;
  2079. /* allocate new rings */
  2080. rx_ring = kcalloc(num_rx_queues,
  2081. sizeof(struct ixgbevf_ring), GFP_KERNEL);
  2082. if (!rx_ring)
  2083. return -ENOMEM;
  2084. /* setup ring fields */
  2085. for (i = 0; i < num_rx_queues; i++) {
  2086. rx_ring[i].count = adapter->rx_ring_count;
  2087. rx_ring[i].queue_index = i;
  2088. rx_ring[i].reg_idx = i;
  2089. rx_ring[i].dev = &adapter->pdev->dev;
  2090. rx_ring[i].netdev = adapter->netdev;
  2091. }
  2092. /* free the existing ring and queues */
  2093. adapter->num_rx_queues = 0;
  2094. kfree(adapter->rx_ring);
  2095. /* move new rings into position on the adapter struct */
  2096. adapter->rx_ring = rx_ring;
  2097. adapter->num_rx_queues = num_rx_queues;
  2098. return 0;
  2099. }
  2100. /**
  2101. * ixgbevf_open - Called when a network interface is made active
  2102. * @netdev: network interface device structure
  2103. *
  2104. * Returns 0 on success, negative value on failure
  2105. *
  2106. * The open entry point is called when a network interface is made
  2107. * active by the system (IFF_UP). At this point all resources needed
  2108. * for transmit and receive operations are allocated, the interrupt
  2109. * handler is registered with the OS, the watchdog timer is started,
  2110. * and the stack is notified that the interface is ready.
  2111. **/
  2112. static int ixgbevf_open(struct net_device *netdev)
  2113. {
  2114. struct ixgbevf_adapter *adapter = netdev_priv(netdev);
  2115. struct ixgbe_hw *hw = &adapter->hw;
  2116. int err;
  2117. /* disallow open during test */
  2118. if (test_bit(__IXGBEVF_TESTING, &adapter->state))
  2119. return -EBUSY;
  2120. if (hw->adapter_stopped) {
  2121. ixgbevf_reset(adapter);
  2122. /* if adapter is still stopped then PF isn't up and
  2123. * the vf can't start. */
  2124. if (hw->adapter_stopped) {
  2125. err = IXGBE_ERR_MBX;
  2126. pr_err("Unable to start - perhaps the PF Driver isn't "
  2127. "up yet\n");
  2128. goto err_setup_reset;
  2129. }
  2130. }
  2131. ixgbevf_negotiate_api(adapter);
  2132. /* setup queue reg_idx and Rx queue count */
  2133. err = ixgbevf_setup_queues(adapter);
  2134. if (err)
  2135. goto err_setup_queues;
  2136. /* allocate transmit descriptors */
  2137. err = ixgbevf_setup_all_tx_resources(adapter);
  2138. if (err)
  2139. goto err_setup_tx;
  2140. /* allocate receive descriptors */
  2141. err = ixgbevf_setup_all_rx_resources(adapter);
  2142. if (err)
  2143. goto err_setup_rx;
  2144. ixgbevf_configure(adapter);
  2145. /*
  2146. * Map the Tx/Rx rings to the vectors we were allotted.
  2147. * if request_irq will be called in this function map_rings
  2148. * must be called *before* up_complete
  2149. */
  2150. ixgbevf_map_rings_to_vectors(adapter);
  2151. ixgbevf_up_complete(adapter);
  2152. /* clear any pending interrupts, may auto mask */
  2153. IXGBE_READ_REG(hw, IXGBE_VTEICR);
  2154. err = ixgbevf_request_irq(adapter);
  2155. if (err)
  2156. goto err_req_irq;
  2157. ixgbevf_irq_enable(adapter);
  2158. return 0;
  2159. err_req_irq:
  2160. ixgbevf_down(adapter);
  2161. ixgbevf_free_irq(adapter);
  2162. err_setup_rx:
  2163. ixgbevf_free_all_rx_resources(adapter);
  2164. err_setup_tx:
  2165. ixgbevf_free_all_tx_resources(adapter);
  2166. err_setup_queues:
  2167. ixgbevf_reset(adapter);
  2168. err_setup_reset:
  2169. return err;
  2170. }
  2171. /**
  2172. * ixgbevf_close - Disables a network interface
  2173. * @netdev: network interface device structure
  2174. *
  2175. * Returns 0, this is not allowed to fail
  2176. *
  2177. * The close entry point is called when an interface is de-activated
  2178. * by the OS. The hardware is still under the drivers control, but
  2179. * needs to be disabled. A global MAC reset is issued to stop the
  2180. * hardware, and all transmit and receive resources are freed.
  2181. **/
  2182. static int ixgbevf_close(struct net_device *netdev)
  2183. {
  2184. struct ixgbevf_adapter *adapter = netdev_priv(netdev);
  2185. ixgbevf_down(adapter);
  2186. ixgbevf_free_irq(adapter);
  2187. ixgbevf_free_all_tx_resources(adapter);
  2188. ixgbevf_free_all_rx_resources(adapter);
  2189. return 0;
  2190. }
  2191. static void ixgbevf_tx_ctxtdesc(struct ixgbevf_ring *tx_ring,
  2192. u32 vlan_macip_lens, u32 type_tucmd,
  2193. u32 mss_l4len_idx)
  2194. {
  2195. struct ixgbe_adv_tx_context_desc *context_desc;
  2196. u16 i = tx_ring->next_to_use;
  2197. context_desc = IXGBEVF_TX_CTXTDESC(tx_ring, i);
  2198. i++;
  2199. tx_ring->next_to_use = (i < tx_ring->count) ? i : 0;
  2200. /* set bits to identify this as an advanced context descriptor */
  2201. type_tucmd |= IXGBE_TXD_CMD_DEXT | IXGBE_ADVTXD_DTYP_CTXT;
  2202. context_desc->vlan_macip_lens = cpu_to_le32(vlan_macip_lens);
  2203. context_desc->seqnum_seed = 0;
  2204. context_desc->type_tucmd_mlhl = cpu_to_le32(type_tucmd);
  2205. context_desc->mss_l4len_idx = cpu_to_le32(mss_l4len_idx);
  2206. }
  2207. static int ixgbevf_tso(struct ixgbevf_ring *tx_ring,
  2208. struct sk_buff *skb, u32 tx_flags, u8 *hdr_len)
  2209. {
  2210. u32 vlan_macip_lens, type_tucmd;
  2211. u32 mss_l4len_idx, l4len;
  2212. if (!skb_is_gso(skb))
  2213. return 0;
  2214. if (skb_header_cloned(skb)) {
  2215. int err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
  2216. if (err)
  2217. return err;
  2218. }
  2219. /* ADV DTYP TUCMD MKRLOC/ISCSIHEDLEN */
  2220. type_tucmd = IXGBE_ADVTXD_TUCMD_L4T_TCP;
  2221. if (skb->protocol == htons(ETH_P_IP)) {
  2222. struct iphdr *iph = ip_hdr(skb);
  2223. iph->tot_len = 0;
  2224. iph->check = 0;
  2225. tcp_hdr(skb)->check = ~csum_tcpudp_magic(iph->saddr,
  2226. iph->daddr, 0,
  2227. IPPROTO_TCP,
  2228. 0);
  2229. type_tucmd |= IXGBE_ADVTXD_TUCMD_IPV4;
  2230. } else if (skb_is_gso_v6(skb)) {
  2231. ipv6_hdr(skb)->payload_len = 0;
  2232. tcp_hdr(skb)->check =
  2233. ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
  2234. &ipv6_hdr(skb)->daddr,
  2235. 0, IPPROTO_TCP, 0);
  2236. }
  2237. /* compute header lengths */
  2238. l4len = tcp_hdrlen(skb);
  2239. *hdr_len += l4len;
  2240. *hdr_len = skb_transport_offset(skb) + l4len;
  2241. /* mss_l4len_id: use 1 as index for TSO */
  2242. mss_l4len_idx = l4len << IXGBE_ADVTXD_L4LEN_SHIFT;
  2243. mss_l4len_idx |= skb_shinfo(skb)->gso_size << IXGBE_ADVTXD_MSS_SHIFT;
  2244. mss_l4len_idx |= 1 << IXGBE_ADVTXD_IDX_SHIFT;
  2245. /* vlan_macip_lens: HEADLEN, MACLEN, VLAN tag */
  2246. vlan_macip_lens = skb_network_header_len(skb);
  2247. vlan_macip_lens |= skb_network_offset(skb) << IXGBE_ADVTXD_MACLEN_SHIFT;
  2248. vlan_macip_lens |= tx_flags & IXGBE_TX_FLAGS_VLAN_MASK;
  2249. ixgbevf_tx_ctxtdesc(tx_ring, vlan_macip_lens,
  2250. type_tucmd, mss_l4len_idx);
  2251. return 1;
  2252. }
  2253. static bool ixgbevf_tx_csum(struct ixgbevf_ring *tx_ring,
  2254. struct sk_buff *skb, u32 tx_flags)
  2255. {
  2256. u32 vlan_macip_lens = 0;
  2257. u32 mss_l4len_idx = 0;
  2258. u32 type_tucmd = 0;
  2259. if (skb->ip_summed == CHECKSUM_PARTIAL) {
  2260. u8 l4_hdr = 0;
  2261. switch (skb->protocol) {
  2262. case __constant_htons(ETH_P_IP):
  2263. vlan_macip_lens |= skb_network_header_len(skb);
  2264. type_tucmd |= IXGBE_ADVTXD_TUCMD_IPV4;
  2265. l4_hdr = ip_hdr(skb)->protocol;
  2266. break;
  2267. case __constant_htons(ETH_P_IPV6):
  2268. vlan_macip_lens |= skb_network_header_len(skb);
  2269. l4_hdr = ipv6_hdr(skb)->nexthdr;
  2270. break;
  2271. default:
  2272. if (unlikely(net_ratelimit())) {
  2273. dev_warn(tx_ring->dev,
  2274. "partial checksum but proto=%x!\n",
  2275. skb->protocol);
  2276. }
  2277. break;
  2278. }
  2279. switch (l4_hdr) {
  2280. case IPPROTO_TCP:
  2281. type_tucmd |= IXGBE_ADVTXD_TUCMD_L4T_TCP;
  2282. mss_l4len_idx = tcp_hdrlen(skb) <<
  2283. IXGBE_ADVTXD_L4LEN_SHIFT;
  2284. break;
  2285. case IPPROTO_SCTP:
  2286. type_tucmd |= IXGBE_ADVTXD_TUCMD_L4T_SCTP;
  2287. mss_l4len_idx = sizeof(struct sctphdr) <<
  2288. IXGBE_ADVTXD_L4LEN_SHIFT;
  2289. break;
  2290. case IPPROTO_UDP:
  2291. mss_l4len_idx = sizeof(struct udphdr) <<
  2292. IXGBE_ADVTXD_L4LEN_SHIFT;
  2293. break;
  2294. default:
  2295. if (unlikely(net_ratelimit())) {
  2296. dev_warn(tx_ring->dev,
  2297. "partial checksum but l4 proto=%x!\n",
  2298. l4_hdr);
  2299. }
  2300. break;
  2301. }
  2302. }
  2303. /* vlan_macip_lens: MACLEN, VLAN tag */
  2304. vlan_macip_lens |= skb_network_offset(skb) << IXGBE_ADVTXD_MACLEN_SHIFT;
  2305. vlan_macip_lens |= tx_flags & IXGBE_TX_FLAGS_VLAN_MASK;
  2306. ixgbevf_tx_ctxtdesc(tx_ring, vlan_macip_lens,
  2307. type_tucmd, mss_l4len_idx);
  2308. return (skb->ip_summed == CHECKSUM_PARTIAL);
  2309. }
  2310. static int ixgbevf_tx_map(struct ixgbevf_ring *tx_ring,
  2311. struct sk_buff *skb, u32 tx_flags,
  2312. unsigned int first)
  2313. {
  2314. struct ixgbevf_tx_buffer *tx_buffer_info;
  2315. unsigned int len;
  2316. unsigned int total = skb->len;
  2317. unsigned int offset = 0, size;
  2318. int count = 0;
  2319. unsigned int nr_frags = skb_shinfo(skb)->nr_frags;
  2320. unsigned int f;
  2321. int i;
  2322. i = tx_ring->next_to_use;
  2323. len = min(skb_headlen(skb), total);
  2324. while (len) {
  2325. tx_buffer_info = &tx_ring->tx_buffer_info[i];
  2326. size = min(len, (unsigned int)IXGBE_MAX_DATA_PER_TXD);
  2327. tx_buffer_info->length = size;
  2328. tx_buffer_info->mapped_as_page = false;
  2329. tx_buffer_info->dma = dma_map_single(tx_ring->dev,
  2330. skb->data + offset,
  2331. size, DMA_TO_DEVICE);
  2332. if (dma_mapping_error(tx_ring->dev, tx_buffer_info->dma))
  2333. goto dma_error;
  2334. tx_buffer_info->next_to_watch = i;
  2335. len -= size;
  2336. total -= size;
  2337. offset += size;
  2338. count++;
  2339. i++;
  2340. if (i == tx_ring->count)
  2341. i = 0;
  2342. }
  2343. for (f = 0; f < nr_frags; f++) {
  2344. const struct skb_frag_struct *frag;
  2345. frag = &skb_shinfo(skb)->frags[f];
  2346. len = min((unsigned int)skb_frag_size(frag), total);
  2347. offset = 0;
  2348. while (len) {
  2349. tx_buffer_info = &tx_ring->tx_buffer_info[i];
  2350. size = min(len, (unsigned int)IXGBE_MAX_DATA_PER_TXD);
  2351. tx_buffer_info->length = size;
  2352. tx_buffer_info->dma =
  2353. skb_frag_dma_map(tx_ring->dev, frag,
  2354. offset, size, DMA_TO_DEVICE);
  2355. tx_buffer_info->mapped_as_page = true;
  2356. if (dma_mapping_error(tx_ring->dev,
  2357. tx_buffer_info->dma))
  2358. goto dma_error;
  2359. tx_buffer_info->next_to_watch = i;
  2360. len -= size;
  2361. total -= size;
  2362. offset += size;
  2363. count++;
  2364. i++;
  2365. if (i == tx_ring->count)
  2366. i = 0;
  2367. }
  2368. if (total == 0)
  2369. break;
  2370. }
  2371. if (i == 0)
  2372. i = tx_ring->count - 1;
  2373. else
  2374. i = i - 1;
  2375. tx_ring->tx_buffer_info[i].skb = skb;
  2376. tx_ring->tx_buffer_info[first].next_to_watch = i;
  2377. tx_ring->tx_buffer_info[first].time_stamp = jiffies;
  2378. return count;
  2379. dma_error:
  2380. dev_err(tx_ring->dev, "TX DMA map failed\n");
  2381. /* clear timestamp and dma mappings for failed tx_buffer_info map */
  2382. tx_buffer_info->dma = 0;
  2383. tx_buffer_info->next_to_watch = 0;
  2384. count--;
  2385. /* clear timestamp and dma mappings for remaining portion of packet */
  2386. while (count >= 0) {
  2387. count--;
  2388. i--;
  2389. if (i < 0)
  2390. i += tx_ring->count;
  2391. tx_buffer_info = &tx_ring->tx_buffer_info[i];
  2392. ixgbevf_unmap_and_free_tx_resource(tx_ring, tx_buffer_info);
  2393. }
  2394. return count;
  2395. }
  2396. static void ixgbevf_tx_queue(struct ixgbevf_ring *tx_ring, int tx_flags,
  2397. int count, u32 paylen, u8 hdr_len)
  2398. {
  2399. union ixgbe_adv_tx_desc *tx_desc = NULL;
  2400. struct ixgbevf_tx_buffer *tx_buffer_info;
  2401. u32 olinfo_status = 0, cmd_type_len = 0;
  2402. unsigned int i;
  2403. u32 txd_cmd = IXGBE_TXD_CMD_EOP | IXGBE_TXD_CMD_RS | IXGBE_TXD_CMD_IFCS;
  2404. cmd_type_len |= IXGBE_ADVTXD_DTYP_DATA;
  2405. cmd_type_len |= IXGBE_ADVTXD_DCMD_IFCS | IXGBE_ADVTXD_DCMD_DEXT;
  2406. if (tx_flags & IXGBE_TX_FLAGS_VLAN)
  2407. cmd_type_len |= IXGBE_ADVTXD_DCMD_VLE;
  2408. if (tx_flags & IXGBE_TX_FLAGS_CSUM)
  2409. olinfo_status |= IXGBE_ADVTXD_POPTS_TXSM;
  2410. if (tx_flags & IXGBE_TX_FLAGS_TSO) {
  2411. cmd_type_len |= IXGBE_ADVTXD_DCMD_TSE;
  2412. /* use index 1 context for tso */
  2413. olinfo_status |= (1 << IXGBE_ADVTXD_IDX_SHIFT);
  2414. if (tx_flags & IXGBE_TX_FLAGS_IPV4)
  2415. olinfo_status |= IXGBE_ADVTXD_POPTS_IXSM;
  2416. }
  2417. /*
  2418. * Check Context must be set if Tx switch is enabled, which it
  2419. * always is for case where virtual functions are running
  2420. */
  2421. olinfo_status |= IXGBE_ADVTXD_CC;
  2422. olinfo_status |= ((paylen - hdr_len) << IXGBE_ADVTXD_PAYLEN_SHIFT);
  2423. i = tx_ring->next_to_use;
  2424. while (count--) {
  2425. tx_buffer_info = &tx_ring->tx_buffer_info[i];
  2426. tx_desc = IXGBEVF_TX_DESC(tx_ring, i);
  2427. tx_desc->read.buffer_addr = cpu_to_le64(tx_buffer_info->dma);
  2428. tx_desc->read.cmd_type_len =
  2429. cpu_to_le32(cmd_type_len | tx_buffer_info->length);
  2430. tx_desc->read.olinfo_status = cpu_to_le32(olinfo_status);
  2431. i++;
  2432. if (i == tx_ring->count)
  2433. i = 0;
  2434. }
  2435. tx_desc->read.cmd_type_len |= cpu_to_le32(txd_cmd);
  2436. tx_ring->next_to_use = i;
  2437. }
  2438. static int __ixgbevf_maybe_stop_tx(struct ixgbevf_ring *tx_ring, int size)
  2439. {
  2440. struct ixgbevf_adapter *adapter = netdev_priv(tx_ring->netdev);
  2441. netif_stop_subqueue(tx_ring->netdev, tx_ring->queue_index);
  2442. /* Herbert's original patch had:
  2443. * smp_mb__after_netif_stop_queue();
  2444. * but since that doesn't exist yet, just open code it. */
  2445. smp_mb();
  2446. /* We need to check again in a case another CPU has just
  2447. * made room available. */
  2448. if (likely(IXGBE_DESC_UNUSED(tx_ring) < size))
  2449. return -EBUSY;
  2450. /* A reprieve! - use start_queue because it doesn't call schedule */
  2451. netif_start_subqueue(tx_ring->netdev, tx_ring->queue_index);
  2452. ++adapter->restart_queue;
  2453. return 0;
  2454. }
  2455. static int ixgbevf_maybe_stop_tx(struct ixgbevf_ring *tx_ring, int size)
  2456. {
  2457. if (likely(IXGBE_DESC_UNUSED(tx_ring) >= size))
  2458. return 0;
  2459. return __ixgbevf_maybe_stop_tx(tx_ring, size);
  2460. }
  2461. static int ixgbevf_xmit_frame(struct sk_buff *skb, struct net_device *netdev)
  2462. {
  2463. struct ixgbevf_adapter *adapter = netdev_priv(netdev);
  2464. struct ixgbevf_ring *tx_ring;
  2465. unsigned int first;
  2466. unsigned int tx_flags = 0;
  2467. u8 hdr_len = 0;
  2468. int r_idx = 0, tso;
  2469. u16 count = TXD_USE_COUNT(skb_headlen(skb));
  2470. #if PAGE_SIZE > IXGBE_MAX_DATA_PER_TXD
  2471. unsigned short f;
  2472. #endif
  2473. tx_ring = &adapter->tx_ring[r_idx];
  2474. /*
  2475. * need: 1 descriptor per page * PAGE_SIZE/IXGBE_MAX_DATA_PER_TXD,
  2476. * + 1 desc for skb_headlen/IXGBE_MAX_DATA_PER_TXD,
  2477. * + 2 desc gap to keep tail from touching head,
  2478. * + 1 desc for context descriptor,
  2479. * otherwise try next time
  2480. */
  2481. #if PAGE_SIZE > IXGBE_MAX_DATA_PER_TXD
  2482. for (f = 0; f < skb_shinfo(skb)->nr_frags; f++)
  2483. count += TXD_USE_COUNT(skb_shinfo(skb)->frags[f].size);
  2484. #else
  2485. count += skb_shinfo(skb)->nr_frags;
  2486. #endif
  2487. if (ixgbevf_maybe_stop_tx(tx_ring, count + 3)) {
  2488. adapter->tx_busy++;
  2489. return NETDEV_TX_BUSY;
  2490. }
  2491. if (vlan_tx_tag_present(skb)) {
  2492. tx_flags |= vlan_tx_tag_get(skb);
  2493. tx_flags <<= IXGBE_TX_FLAGS_VLAN_SHIFT;
  2494. tx_flags |= IXGBE_TX_FLAGS_VLAN;
  2495. }
  2496. first = tx_ring->next_to_use;
  2497. if (skb->protocol == htons(ETH_P_IP))
  2498. tx_flags |= IXGBE_TX_FLAGS_IPV4;
  2499. tso = ixgbevf_tso(tx_ring, skb, tx_flags, &hdr_len);
  2500. if (tso < 0) {
  2501. dev_kfree_skb_any(skb);
  2502. return NETDEV_TX_OK;
  2503. }
  2504. if (tso)
  2505. tx_flags |= IXGBE_TX_FLAGS_TSO | IXGBE_TX_FLAGS_CSUM;
  2506. else if (ixgbevf_tx_csum(tx_ring, skb, tx_flags))
  2507. tx_flags |= IXGBE_TX_FLAGS_CSUM;
  2508. ixgbevf_tx_queue(tx_ring, tx_flags,
  2509. ixgbevf_tx_map(tx_ring, skb, tx_flags, first),
  2510. skb->len, hdr_len);
  2511. /*
  2512. * Force memory writes to complete before letting h/w
  2513. * know there are new descriptors to fetch. (Only
  2514. * applicable for weak-ordered memory model archs,
  2515. * such as IA-64).
  2516. */
  2517. wmb();
  2518. writel(tx_ring->next_to_use, adapter->hw.hw_addr + tx_ring->tail);
  2519. ixgbevf_maybe_stop_tx(tx_ring, DESC_NEEDED);
  2520. return NETDEV_TX_OK;
  2521. }
  2522. /**
  2523. * ixgbevf_set_mac - Change the Ethernet Address of the NIC
  2524. * @netdev: network interface device structure
  2525. * @p: pointer to an address structure
  2526. *
  2527. * Returns 0 on success, negative on failure
  2528. **/
  2529. static int ixgbevf_set_mac(struct net_device *netdev, void *p)
  2530. {
  2531. struct ixgbevf_adapter *adapter = netdev_priv(netdev);
  2532. struct ixgbe_hw *hw = &adapter->hw;
  2533. struct sockaddr *addr = p;
  2534. if (!is_valid_ether_addr(addr->sa_data))
  2535. return -EADDRNOTAVAIL;
  2536. memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
  2537. memcpy(hw->mac.addr, addr->sa_data, netdev->addr_len);
  2538. spin_lock(&adapter->mbx_lock);
  2539. if (hw->mac.ops.set_rar)
  2540. hw->mac.ops.set_rar(hw, 0, hw->mac.addr, 0);
  2541. spin_unlock(&adapter->mbx_lock);
  2542. return 0;
  2543. }
  2544. /**
  2545. * ixgbevf_change_mtu - Change the Maximum Transfer Unit
  2546. * @netdev: network interface device structure
  2547. * @new_mtu: new value for maximum frame size
  2548. *
  2549. * Returns 0 on success, negative on failure
  2550. **/
  2551. static int ixgbevf_change_mtu(struct net_device *netdev, int new_mtu)
  2552. {
  2553. struct ixgbevf_adapter *adapter = netdev_priv(netdev);
  2554. int max_frame = new_mtu + ETH_HLEN + ETH_FCS_LEN;
  2555. int max_possible_frame = MAXIMUM_ETHERNET_VLAN_SIZE;
  2556. switch (adapter->hw.api_version) {
  2557. case ixgbe_mbox_api_11:
  2558. max_possible_frame = IXGBE_MAX_JUMBO_FRAME_SIZE;
  2559. break;
  2560. default:
  2561. if (adapter->hw.mac.type == ixgbe_mac_X540_vf)
  2562. max_possible_frame = IXGBE_MAX_JUMBO_FRAME_SIZE;
  2563. break;
  2564. }
  2565. /* MTU < 68 is an error and causes problems on some kernels */
  2566. if ((new_mtu < 68) || (max_frame > max_possible_frame))
  2567. return -EINVAL;
  2568. hw_dbg(&adapter->hw, "changing MTU from %d to %d\n",
  2569. netdev->mtu, new_mtu);
  2570. /* must set new MTU before calling down or up */
  2571. netdev->mtu = new_mtu;
  2572. if (netif_running(netdev))
  2573. ixgbevf_reinit_locked(adapter);
  2574. return 0;
  2575. }
  2576. static int ixgbevf_suspend(struct pci_dev *pdev, pm_message_t state)
  2577. {
  2578. struct net_device *netdev = pci_get_drvdata(pdev);
  2579. struct ixgbevf_adapter *adapter = netdev_priv(netdev);
  2580. #ifdef CONFIG_PM
  2581. int retval = 0;
  2582. #endif
  2583. netif_device_detach(netdev);
  2584. if (netif_running(netdev)) {
  2585. rtnl_lock();
  2586. ixgbevf_down(adapter);
  2587. ixgbevf_free_irq(adapter);
  2588. ixgbevf_free_all_tx_resources(adapter);
  2589. ixgbevf_free_all_rx_resources(adapter);
  2590. rtnl_unlock();
  2591. }
  2592. ixgbevf_clear_interrupt_scheme(adapter);
  2593. #ifdef CONFIG_PM
  2594. retval = pci_save_state(pdev);
  2595. if (retval)
  2596. return retval;
  2597. #endif
  2598. pci_disable_device(pdev);
  2599. return 0;
  2600. }
  2601. #ifdef CONFIG_PM
  2602. static int ixgbevf_resume(struct pci_dev *pdev)
  2603. {
  2604. struct ixgbevf_adapter *adapter = pci_get_drvdata(pdev);
  2605. struct net_device *netdev = adapter->netdev;
  2606. u32 err;
  2607. pci_set_power_state(pdev, PCI_D0);
  2608. pci_restore_state(pdev);
  2609. /*
  2610. * pci_restore_state clears dev->state_saved so call
  2611. * pci_save_state to restore it.
  2612. */
  2613. pci_save_state(pdev);
  2614. err = pci_enable_device_mem(pdev);
  2615. if (err) {
  2616. dev_err(&pdev->dev, "Cannot enable PCI device from suspend\n");
  2617. return err;
  2618. }
  2619. pci_set_master(pdev);
  2620. rtnl_lock();
  2621. err = ixgbevf_init_interrupt_scheme(adapter);
  2622. rtnl_unlock();
  2623. if (err) {
  2624. dev_err(&pdev->dev, "Cannot initialize interrupts\n");
  2625. return err;
  2626. }
  2627. ixgbevf_reset(adapter);
  2628. if (netif_running(netdev)) {
  2629. err = ixgbevf_open(netdev);
  2630. if (err)
  2631. return err;
  2632. }
  2633. netif_device_attach(netdev);
  2634. return err;
  2635. }
  2636. #endif /* CONFIG_PM */
  2637. static void ixgbevf_shutdown(struct pci_dev *pdev)
  2638. {
  2639. ixgbevf_suspend(pdev, PMSG_SUSPEND);
  2640. }
  2641. static struct rtnl_link_stats64 *ixgbevf_get_stats(struct net_device *netdev,
  2642. struct rtnl_link_stats64 *stats)
  2643. {
  2644. struct ixgbevf_adapter *adapter = netdev_priv(netdev);
  2645. unsigned int start;
  2646. u64 bytes, packets;
  2647. const struct ixgbevf_ring *ring;
  2648. int i;
  2649. ixgbevf_update_stats(adapter);
  2650. stats->multicast = adapter->stats.vfmprc - adapter->stats.base_vfmprc;
  2651. for (i = 0; i < adapter->num_rx_queues; i++) {
  2652. ring = &adapter->rx_ring[i];
  2653. do {
  2654. start = u64_stats_fetch_begin_bh(&ring->syncp);
  2655. bytes = ring->total_bytes;
  2656. packets = ring->total_packets;
  2657. } while (u64_stats_fetch_retry_bh(&ring->syncp, start));
  2658. stats->rx_bytes += bytes;
  2659. stats->rx_packets += packets;
  2660. }
  2661. for (i = 0; i < adapter->num_tx_queues; i++) {
  2662. ring = &adapter->tx_ring[i];
  2663. do {
  2664. start = u64_stats_fetch_begin_bh(&ring->syncp);
  2665. bytes = ring->total_bytes;
  2666. packets = ring->total_packets;
  2667. } while (u64_stats_fetch_retry_bh(&ring->syncp, start));
  2668. stats->tx_bytes += bytes;
  2669. stats->tx_packets += packets;
  2670. }
  2671. return stats;
  2672. }
  2673. static const struct net_device_ops ixgbevf_netdev_ops = {
  2674. .ndo_open = ixgbevf_open,
  2675. .ndo_stop = ixgbevf_close,
  2676. .ndo_start_xmit = ixgbevf_xmit_frame,
  2677. .ndo_set_rx_mode = ixgbevf_set_rx_mode,
  2678. .ndo_get_stats64 = ixgbevf_get_stats,
  2679. .ndo_validate_addr = eth_validate_addr,
  2680. .ndo_set_mac_address = ixgbevf_set_mac,
  2681. .ndo_change_mtu = ixgbevf_change_mtu,
  2682. .ndo_tx_timeout = ixgbevf_tx_timeout,
  2683. .ndo_vlan_rx_add_vid = ixgbevf_vlan_rx_add_vid,
  2684. .ndo_vlan_rx_kill_vid = ixgbevf_vlan_rx_kill_vid,
  2685. };
  2686. static void ixgbevf_assign_netdev_ops(struct net_device *dev)
  2687. {
  2688. dev->netdev_ops = &ixgbevf_netdev_ops;
  2689. ixgbevf_set_ethtool_ops(dev);
  2690. dev->watchdog_timeo = 5 * HZ;
  2691. }
  2692. /**
  2693. * ixgbevf_probe - Device Initialization Routine
  2694. * @pdev: PCI device information struct
  2695. * @ent: entry in ixgbevf_pci_tbl
  2696. *
  2697. * Returns 0 on success, negative on failure
  2698. *
  2699. * ixgbevf_probe initializes an adapter identified by a pci_dev structure.
  2700. * The OS initialization, configuring of the adapter private structure,
  2701. * and a hardware reset occur.
  2702. **/
  2703. static int __devinit ixgbevf_probe(struct pci_dev *pdev,
  2704. const struct pci_device_id *ent)
  2705. {
  2706. struct net_device *netdev;
  2707. struct ixgbevf_adapter *adapter = NULL;
  2708. struct ixgbe_hw *hw = NULL;
  2709. const struct ixgbevf_info *ii = ixgbevf_info_tbl[ent->driver_data];
  2710. static int cards_found;
  2711. int err, pci_using_dac;
  2712. err = pci_enable_device(pdev);
  2713. if (err)
  2714. return err;
  2715. if (!dma_set_mask(&pdev->dev, DMA_BIT_MASK(64)) &&
  2716. !dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(64))) {
  2717. pci_using_dac = 1;
  2718. } else {
  2719. err = dma_set_mask(&pdev->dev, DMA_BIT_MASK(32));
  2720. if (err) {
  2721. err = dma_set_coherent_mask(&pdev->dev,
  2722. DMA_BIT_MASK(32));
  2723. if (err) {
  2724. dev_err(&pdev->dev, "No usable DMA "
  2725. "configuration, aborting\n");
  2726. goto err_dma;
  2727. }
  2728. }
  2729. pci_using_dac = 0;
  2730. }
  2731. err = pci_request_regions(pdev, ixgbevf_driver_name);
  2732. if (err) {
  2733. dev_err(&pdev->dev, "pci_request_regions failed 0x%x\n", err);
  2734. goto err_pci_reg;
  2735. }
  2736. pci_set_master(pdev);
  2737. netdev = alloc_etherdev_mq(sizeof(struct ixgbevf_adapter),
  2738. MAX_TX_QUEUES);
  2739. if (!netdev) {
  2740. err = -ENOMEM;
  2741. goto err_alloc_etherdev;
  2742. }
  2743. SET_NETDEV_DEV(netdev, &pdev->dev);
  2744. pci_set_drvdata(pdev, netdev);
  2745. adapter = netdev_priv(netdev);
  2746. adapter->netdev = netdev;
  2747. adapter->pdev = pdev;
  2748. hw = &adapter->hw;
  2749. hw->back = adapter;
  2750. adapter->msg_enable = netif_msg_init(debug, DEFAULT_MSG_ENABLE);
  2751. /*
  2752. * call save state here in standalone driver because it relies on
  2753. * adapter struct to exist, and needs to call netdev_priv
  2754. */
  2755. pci_save_state(pdev);
  2756. hw->hw_addr = ioremap(pci_resource_start(pdev, 0),
  2757. pci_resource_len(pdev, 0));
  2758. if (!hw->hw_addr) {
  2759. err = -EIO;
  2760. goto err_ioremap;
  2761. }
  2762. ixgbevf_assign_netdev_ops(netdev);
  2763. adapter->bd_number = cards_found;
  2764. /* Setup hw api */
  2765. memcpy(&hw->mac.ops, ii->mac_ops, sizeof(hw->mac.ops));
  2766. hw->mac.type = ii->mac;
  2767. memcpy(&hw->mbx.ops, &ixgbevf_mbx_ops,
  2768. sizeof(struct ixgbe_mbx_operations));
  2769. /* setup the private structure */
  2770. err = ixgbevf_sw_init(adapter);
  2771. if (err)
  2772. goto err_sw_init;
  2773. /* The HW MAC address was set and/or determined in sw_init */
  2774. memcpy(netdev->perm_addr, adapter->hw.mac.addr, netdev->addr_len);
  2775. if (!is_valid_ether_addr(netdev->dev_addr)) {
  2776. pr_err("invalid MAC address\n");
  2777. err = -EIO;
  2778. goto err_sw_init;
  2779. }
  2780. netdev->hw_features = NETIF_F_SG |
  2781. NETIF_F_IP_CSUM |
  2782. NETIF_F_IPV6_CSUM |
  2783. NETIF_F_TSO |
  2784. NETIF_F_TSO6 |
  2785. NETIF_F_RXCSUM;
  2786. netdev->features = netdev->hw_features |
  2787. NETIF_F_HW_VLAN_TX |
  2788. NETIF_F_HW_VLAN_RX |
  2789. NETIF_F_HW_VLAN_FILTER;
  2790. netdev->vlan_features |= NETIF_F_TSO;
  2791. netdev->vlan_features |= NETIF_F_TSO6;
  2792. netdev->vlan_features |= NETIF_F_IP_CSUM;
  2793. netdev->vlan_features |= NETIF_F_IPV6_CSUM;
  2794. netdev->vlan_features |= NETIF_F_SG;
  2795. if (pci_using_dac)
  2796. netdev->features |= NETIF_F_HIGHDMA;
  2797. netdev->priv_flags |= IFF_UNICAST_FLT;
  2798. init_timer(&adapter->watchdog_timer);
  2799. adapter->watchdog_timer.function = ixgbevf_watchdog;
  2800. adapter->watchdog_timer.data = (unsigned long)adapter;
  2801. INIT_WORK(&adapter->reset_task, ixgbevf_reset_task);
  2802. INIT_WORK(&adapter->watchdog_task, ixgbevf_watchdog_task);
  2803. err = ixgbevf_init_interrupt_scheme(adapter);
  2804. if (err)
  2805. goto err_sw_init;
  2806. /* pick up the PCI bus settings for reporting later */
  2807. if (hw->mac.ops.get_bus_info)
  2808. hw->mac.ops.get_bus_info(hw);
  2809. strcpy(netdev->name, "eth%d");
  2810. err = register_netdev(netdev);
  2811. if (err)
  2812. goto err_register;
  2813. netif_carrier_off(netdev);
  2814. ixgbevf_init_last_counter_stats(adapter);
  2815. /* print the MAC address */
  2816. hw_dbg(hw, "%pM\n", netdev->dev_addr);
  2817. hw_dbg(hw, "MAC: %d\n", hw->mac.type);
  2818. hw_dbg(hw, "Intel(R) 82599 Virtual Function\n");
  2819. cards_found++;
  2820. return 0;
  2821. err_register:
  2822. ixgbevf_clear_interrupt_scheme(adapter);
  2823. err_sw_init:
  2824. ixgbevf_reset_interrupt_capability(adapter);
  2825. iounmap(hw->hw_addr);
  2826. err_ioremap:
  2827. free_netdev(netdev);
  2828. err_alloc_etherdev:
  2829. pci_release_regions(pdev);
  2830. err_pci_reg:
  2831. err_dma:
  2832. pci_disable_device(pdev);
  2833. return err;
  2834. }
  2835. /**
  2836. * ixgbevf_remove - Device Removal Routine
  2837. * @pdev: PCI device information struct
  2838. *
  2839. * ixgbevf_remove is called by the PCI subsystem to alert the driver
  2840. * that it should release a PCI device. The could be caused by a
  2841. * Hot-Plug event, or because the driver is going to be removed from
  2842. * memory.
  2843. **/
  2844. static void __devexit ixgbevf_remove(struct pci_dev *pdev)
  2845. {
  2846. struct net_device *netdev = pci_get_drvdata(pdev);
  2847. struct ixgbevf_adapter *adapter = netdev_priv(netdev);
  2848. set_bit(__IXGBEVF_DOWN, &adapter->state);
  2849. del_timer_sync(&adapter->watchdog_timer);
  2850. cancel_work_sync(&adapter->reset_task);
  2851. cancel_work_sync(&adapter->watchdog_task);
  2852. if (netdev->reg_state == NETREG_REGISTERED)
  2853. unregister_netdev(netdev);
  2854. ixgbevf_clear_interrupt_scheme(adapter);
  2855. ixgbevf_reset_interrupt_capability(adapter);
  2856. iounmap(adapter->hw.hw_addr);
  2857. pci_release_regions(pdev);
  2858. hw_dbg(&adapter->hw, "Remove complete\n");
  2859. kfree(adapter->tx_ring);
  2860. kfree(adapter->rx_ring);
  2861. free_netdev(netdev);
  2862. pci_disable_device(pdev);
  2863. }
  2864. /**
  2865. * ixgbevf_io_error_detected - called when PCI error is detected
  2866. * @pdev: Pointer to PCI device
  2867. * @state: The current pci connection state
  2868. *
  2869. * This function is called after a PCI bus error affecting
  2870. * this device has been detected.
  2871. */
  2872. static pci_ers_result_t ixgbevf_io_error_detected(struct pci_dev *pdev,
  2873. pci_channel_state_t state)
  2874. {
  2875. struct net_device *netdev = pci_get_drvdata(pdev);
  2876. struct ixgbevf_adapter *adapter = netdev_priv(netdev);
  2877. netif_device_detach(netdev);
  2878. if (state == pci_channel_io_perm_failure)
  2879. return PCI_ERS_RESULT_DISCONNECT;
  2880. if (netif_running(netdev))
  2881. ixgbevf_down(adapter);
  2882. pci_disable_device(pdev);
  2883. /* Request a slot slot reset. */
  2884. return PCI_ERS_RESULT_NEED_RESET;
  2885. }
  2886. /**
  2887. * ixgbevf_io_slot_reset - called after the pci bus has been reset.
  2888. * @pdev: Pointer to PCI device
  2889. *
  2890. * Restart the card from scratch, as if from a cold-boot. Implementation
  2891. * resembles the first-half of the ixgbevf_resume routine.
  2892. */
  2893. static pci_ers_result_t ixgbevf_io_slot_reset(struct pci_dev *pdev)
  2894. {
  2895. struct net_device *netdev = pci_get_drvdata(pdev);
  2896. struct ixgbevf_adapter *adapter = netdev_priv(netdev);
  2897. if (pci_enable_device_mem(pdev)) {
  2898. dev_err(&pdev->dev,
  2899. "Cannot re-enable PCI device after reset.\n");
  2900. return PCI_ERS_RESULT_DISCONNECT;
  2901. }
  2902. pci_set_master(pdev);
  2903. ixgbevf_reset(adapter);
  2904. return PCI_ERS_RESULT_RECOVERED;
  2905. }
  2906. /**
  2907. * ixgbevf_io_resume - called when traffic can start flowing again.
  2908. * @pdev: Pointer to PCI device
  2909. *
  2910. * This callback is called when the error recovery driver tells us that
  2911. * its OK to resume normal operation. Implementation resembles the
  2912. * second-half of the ixgbevf_resume routine.
  2913. */
  2914. static void ixgbevf_io_resume(struct pci_dev *pdev)
  2915. {
  2916. struct net_device *netdev = pci_get_drvdata(pdev);
  2917. struct ixgbevf_adapter *adapter = netdev_priv(netdev);
  2918. if (netif_running(netdev))
  2919. ixgbevf_up(adapter);
  2920. netif_device_attach(netdev);
  2921. }
  2922. /* PCI Error Recovery (ERS) */
  2923. static const struct pci_error_handlers ixgbevf_err_handler = {
  2924. .error_detected = ixgbevf_io_error_detected,
  2925. .slot_reset = ixgbevf_io_slot_reset,
  2926. .resume = ixgbevf_io_resume,
  2927. };
  2928. static struct pci_driver ixgbevf_driver = {
  2929. .name = ixgbevf_driver_name,
  2930. .id_table = ixgbevf_pci_tbl,
  2931. .probe = ixgbevf_probe,
  2932. .remove = __devexit_p(ixgbevf_remove),
  2933. #ifdef CONFIG_PM
  2934. /* Power Management Hooks */
  2935. .suspend = ixgbevf_suspend,
  2936. .resume = ixgbevf_resume,
  2937. #endif
  2938. .shutdown = ixgbevf_shutdown,
  2939. .err_handler = &ixgbevf_err_handler
  2940. };
  2941. /**
  2942. * ixgbevf_init_module - Driver Registration Routine
  2943. *
  2944. * ixgbevf_init_module is the first routine called when the driver is
  2945. * loaded. All it does is register with the PCI subsystem.
  2946. **/
  2947. static int __init ixgbevf_init_module(void)
  2948. {
  2949. int ret;
  2950. pr_info("%s - version %s\n", ixgbevf_driver_string,
  2951. ixgbevf_driver_version);
  2952. pr_info("%s\n", ixgbevf_copyright);
  2953. ret = pci_register_driver(&ixgbevf_driver);
  2954. return ret;
  2955. }
  2956. module_init(ixgbevf_init_module);
  2957. /**
  2958. * ixgbevf_exit_module - Driver Exit Cleanup Routine
  2959. *
  2960. * ixgbevf_exit_module is called just before the driver is removed
  2961. * from memory.
  2962. **/
  2963. static void __exit ixgbevf_exit_module(void)
  2964. {
  2965. pci_unregister_driver(&ixgbevf_driver);
  2966. }
  2967. #ifdef DEBUG
  2968. /**
  2969. * ixgbevf_get_hw_dev_name - return device name string
  2970. * used by hardware layer to print debugging information
  2971. **/
  2972. char *ixgbevf_get_hw_dev_name(struct ixgbe_hw *hw)
  2973. {
  2974. struct ixgbevf_adapter *adapter = hw->back;
  2975. return adapter->netdev->name;
  2976. }
  2977. #endif
  2978. module_exit(ixgbevf_exit_module);
  2979. /* ixgbevf_main.c */