i40evf_main.c 72 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703
  1. /*******************************************************************************
  2. *
  3. * Intel Ethernet Controller XL710 Family Linux Virtual Function Driver
  4. * Copyright(c) 2013 - 2015 Intel Corporation.
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms and conditions of the GNU General Public License,
  8. * version 2, as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope it will be useful, but WITHOUT
  11. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  13. * more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along
  16. * with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. * The full GNU General Public License is included in this distribution in
  19. * the file called "COPYING".
  20. *
  21. * Contact Information:
  22. * e1000-devel Mailing List <e1000-devel@lists.sourceforge.net>
  23. * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  24. *
  25. ******************************************************************************/
  26. #include "i40evf.h"
  27. #include "i40e_prototype.h"
  28. static int i40evf_setup_all_tx_resources(struct i40evf_adapter *adapter);
  29. static int i40evf_setup_all_rx_resources(struct i40evf_adapter *adapter);
  30. static int i40evf_close(struct net_device *netdev);
  31. char i40evf_driver_name[] = "i40evf";
  32. static const char i40evf_driver_string[] =
  33. "Intel(R) XL710/X710 Virtual Function Network Driver";
  34. #define DRV_VERSION "1.3.33"
  35. const char i40evf_driver_version[] = DRV_VERSION;
  36. static const char i40evf_copyright[] =
  37. "Copyright (c) 2013 - 2015 Intel Corporation.";
  38. /* i40evf_pci_tbl - PCI Device ID Table
  39. *
  40. * Wildcard entries (PCI_ANY_ID) should come last
  41. * Last entry must be all 0s
  42. *
  43. * { Vendor ID, Device ID, SubVendor ID, SubDevice ID,
  44. * Class, Class Mask, private data (not used) }
  45. */
  46. static const struct pci_device_id i40evf_pci_tbl[] = {
  47. {PCI_VDEVICE(INTEL, I40E_DEV_ID_VF), 0},
  48. {PCI_VDEVICE(INTEL, I40E_DEV_ID_X722_VF), 0},
  49. /* required last entry */
  50. {0, }
  51. };
  52. MODULE_DEVICE_TABLE(pci, i40evf_pci_tbl);
  53. MODULE_AUTHOR("Intel Corporation, <linux.nics@intel.com>");
  54. MODULE_DESCRIPTION("Intel(R) XL710 X710 Virtual Function Network Driver");
  55. MODULE_LICENSE("GPL");
  56. MODULE_VERSION(DRV_VERSION);
  57. /**
  58. * i40evf_allocate_dma_mem_d - OS specific memory alloc for shared code
  59. * @hw: pointer to the HW structure
  60. * @mem: ptr to mem struct to fill out
  61. * @size: size of memory requested
  62. * @alignment: what to align the allocation to
  63. **/
  64. i40e_status i40evf_allocate_dma_mem_d(struct i40e_hw *hw,
  65. struct i40e_dma_mem *mem,
  66. u64 size, u32 alignment)
  67. {
  68. struct i40evf_adapter *adapter = (struct i40evf_adapter *)hw->back;
  69. if (!mem)
  70. return I40E_ERR_PARAM;
  71. mem->size = ALIGN(size, alignment);
  72. mem->va = dma_alloc_coherent(&adapter->pdev->dev, mem->size,
  73. (dma_addr_t *)&mem->pa, GFP_KERNEL);
  74. if (mem->va)
  75. return 0;
  76. else
  77. return I40E_ERR_NO_MEMORY;
  78. }
  79. /**
  80. * i40evf_free_dma_mem_d - OS specific memory free for shared code
  81. * @hw: pointer to the HW structure
  82. * @mem: ptr to mem struct to free
  83. **/
  84. i40e_status i40evf_free_dma_mem_d(struct i40e_hw *hw, struct i40e_dma_mem *mem)
  85. {
  86. struct i40evf_adapter *adapter = (struct i40evf_adapter *)hw->back;
  87. if (!mem || !mem->va)
  88. return I40E_ERR_PARAM;
  89. dma_free_coherent(&adapter->pdev->dev, mem->size,
  90. mem->va, (dma_addr_t)mem->pa);
  91. return 0;
  92. }
  93. /**
  94. * i40evf_allocate_virt_mem_d - OS specific memory alloc for shared code
  95. * @hw: pointer to the HW structure
  96. * @mem: ptr to mem struct to fill out
  97. * @size: size of memory requested
  98. **/
  99. i40e_status i40evf_allocate_virt_mem_d(struct i40e_hw *hw,
  100. struct i40e_virt_mem *mem, u32 size)
  101. {
  102. if (!mem)
  103. return I40E_ERR_PARAM;
  104. mem->size = size;
  105. mem->va = kzalloc(size, GFP_KERNEL);
  106. if (mem->va)
  107. return 0;
  108. else
  109. return I40E_ERR_NO_MEMORY;
  110. }
  111. /**
  112. * i40evf_free_virt_mem_d - OS specific memory free for shared code
  113. * @hw: pointer to the HW structure
  114. * @mem: ptr to mem struct to free
  115. **/
  116. i40e_status i40evf_free_virt_mem_d(struct i40e_hw *hw,
  117. struct i40e_virt_mem *mem)
  118. {
  119. if (!mem)
  120. return I40E_ERR_PARAM;
  121. /* it's ok to kfree a NULL pointer */
  122. kfree(mem->va);
  123. return 0;
  124. }
  125. /**
  126. * i40evf_debug_d - OS dependent version of debug printing
  127. * @hw: pointer to the HW structure
  128. * @mask: debug level mask
  129. * @fmt_str: printf-type format description
  130. **/
  131. void i40evf_debug_d(void *hw, u32 mask, char *fmt_str, ...)
  132. {
  133. char buf[512];
  134. va_list argptr;
  135. if (!(mask & ((struct i40e_hw *)hw)->debug_mask))
  136. return;
  137. va_start(argptr, fmt_str);
  138. vsnprintf(buf, sizeof(buf), fmt_str, argptr);
  139. va_end(argptr);
  140. /* the debug string is already formatted with a newline */
  141. pr_info("%s", buf);
  142. }
  143. /**
  144. * i40evf_tx_timeout - Respond to a Tx Hang
  145. * @netdev: network interface device structure
  146. **/
  147. static void i40evf_tx_timeout(struct net_device *netdev)
  148. {
  149. struct i40evf_adapter *adapter = netdev_priv(netdev);
  150. adapter->tx_timeout_count++;
  151. if (!(adapter->flags & (I40EVF_FLAG_RESET_PENDING |
  152. I40EVF_FLAG_RESET_NEEDED))) {
  153. adapter->flags |= I40EVF_FLAG_RESET_NEEDED;
  154. schedule_work(&adapter->reset_task);
  155. }
  156. }
  157. /**
  158. * i40evf_misc_irq_disable - Mask off interrupt generation on the NIC
  159. * @adapter: board private structure
  160. **/
  161. static void i40evf_misc_irq_disable(struct i40evf_adapter *adapter)
  162. {
  163. struct i40e_hw *hw = &adapter->hw;
  164. wr32(hw, I40E_VFINT_DYN_CTL01, 0);
  165. /* read flush */
  166. rd32(hw, I40E_VFGEN_RSTAT);
  167. synchronize_irq(adapter->msix_entries[0].vector);
  168. }
  169. /**
  170. * i40evf_misc_irq_enable - Enable default interrupt generation settings
  171. * @adapter: board private structure
  172. **/
  173. static void i40evf_misc_irq_enable(struct i40evf_adapter *adapter)
  174. {
  175. struct i40e_hw *hw = &adapter->hw;
  176. wr32(hw, I40E_VFINT_DYN_CTL01, I40E_VFINT_DYN_CTL01_INTENA_MASK |
  177. I40E_VFINT_DYN_CTL01_ITR_INDX_MASK);
  178. wr32(hw, I40E_VFINT_ICR0_ENA1, I40E_VFINT_ICR0_ENA1_ADMINQ_MASK);
  179. /* read flush */
  180. rd32(hw, I40E_VFGEN_RSTAT);
  181. }
  182. /**
  183. * i40evf_irq_disable - Mask off interrupt generation on the NIC
  184. * @adapter: board private structure
  185. **/
  186. static void i40evf_irq_disable(struct i40evf_adapter *adapter)
  187. {
  188. int i;
  189. struct i40e_hw *hw = &adapter->hw;
  190. if (!adapter->msix_entries)
  191. return;
  192. for (i = 1; i < adapter->num_msix_vectors; i++) {
  193. wr32(hw, I40E_VFINT_DYN_CTLN1(i - 1), 0);
  194. synchronize_irq(adapter->msix_entries[i].vector);
  195. }
  196. /* read flush */
  197. rd32(hw, I40E_VFGEN_RSTAT);
  198. }
  199. /**
  200. * i40evf_irq_enable_queues - Enable interrupt for specified queues
  201. * @adapter: board private structure
  202. * @mask: bitmap of queues to enable
  203. **/
  204. void i40evf_irq_enable_queues(struct i40evf_adapter *adapter, u32 mask)
  205. {
  206. struct i40e_hw *hw = &adapter->hw;
  207. int i;
  208. for (i = 1; i < adapter->num_msix_vectors; i++) {
  209. if (mask & BIT(i - 1)) {
  210. wr32(hw, I40E_VFINT_DYN_CTLN1(i - 1),
  211. I40E_VFINT_DYN_CTLN1_INTENA_MASK |
  212. I40E_VFINT_DYN_CTLN1_ITR_INDX_MASK |
  213. I40E_VFINT_DYN_CTLN1_CLEARPBA_MASK);
  214. }
  215. }
  216. }
  217. /**
  218. * i40evf_fire_sw_int - Generate SW interrupt for specified vectors
  219. * @adapter: board private structure
  220. * @mask: bitmap of vectors to trigger
  221. **/
  222. static void i40evf_fire_sw_int(struct i40evf_adapter *adapter, u32 mask)
  223. {
  224. struct i40e_hw *hw = &adapter->hw;
  225. int i;
  226. uint32_t dyn_ctl;
  227. if (mask & 1) {
  228. dyn_ctl = rd32(hw, I40E_VFINT_DYN_CTL01);
  229. dyn_ctl |= I40E_VFINT_DYN_CTLN1_SWINT_TRIG_MASK |
  230. I40E_VFINT_DYN_CTLN1_ITR_INDX_MASK |
  231. I40E_VFINT_DYN_CTLN1_CLEARPBA_MASK;
  232. wr32(hw, I40E_VFINT_DYN_CTL01, dyn_ctl);
  233. }
  234. for (i = 1; i < adapter->num_msix_vectors; i++) {
  235. if (mask & BIT(i)) {
  236. dyn_ctl = rd32(hw, I40E_VFINT_DYN_CTLN1(i - 1));
  237. dyn_ctl |= I40E_VFINT_DYN_CTLN1_SWINT_TRIG_MASK |
  238. I40E_VFINT_DYN_CTLN1_ITR_INDX_MASK |
  239. I40E_VFINT_DYN_CTLN1_CLEARPBA_MASK;
  240. wr32(hw, I40E_VFINT_DYN_CTLN1(i - 1), dyn_ctl);
  241. }
  242. }
  243. }
  244. /**
  245. * i40evf_irq_enable - Enable default interrupt generation settings
  246. * @adapter: board private structure
  247. * @flush: boolean value whether to run rd32()
  248. **/
  249. void i40evf_irq_enable(struct i40evf_adapter *adapter, bool flush)
  250. {
  251. struct i40e_hw *hw = &adapter->hw;
  252. i40evf_misc_irq_enable(adapter);
  253. i40evf_irq_enable_queues(adapter, ~0);
  254. if (flush)
  255. rd32(hw, I40E_VFGEN_RSTAT);
  256. }
  257. /**
  258. * i40evf_msix_aq - Interrupt handler for vector 0
  259. * @irq: interrupt number
  260. * @data: pointer to netdev
  261. **/
  262. static irqreturn_t i40evf_msix_aq(int irq, void *data)
  263. {
  264. struct net_device *netdev = data;
  265. struct i40evf_adapter *adapter = netdev_priv(netdev);
  266. struct i40e_hw *hw = &adapter->hw;
  267. u32 val;
  268. /* handle non-queue interrupts, these reads clear the registers */
  269. val = rd32(hw, I40E_VFINT_ICR01);
  270. val = rd32(hw, I40E_VFINT_ICR0_ENA1);
  271. val = rd32(hw, I40E_VFINT_DYN_CTL01) |
  272. I40E_VFINT_DYN_CTL01_CLEARPBA_MASK;
  273. wr32(hw, I40E_VFINT_DYN_CTL01, val);
  274. /* schedule work on the private workqueue */
  275. schedule_work(&adapter->adminq_task);
  276. return IRQ_HANDLED;
  277. }
  278. /**
  279. * i40evf_msix_clean_rings - MSIX mode Interrupt Handler
  280. * @irq: interrupt number
  281. * @data: pointer to a q_vector
  282. **/
  283. static irqreturn_t i40evf_msix_clean_rings(int irq, void *data)
  284. {
  285. struct i40e_q_vector *q_vector = data;
  286. if (!q_vector->tx.ring && !q_vector->rx.ring)
  287. return IRQ_HANDLED;
  288. napi_schedule_irqoff(&q_vector->napi);
  289. return IRQ_HANDLED;
  290. }
  291. /**
  292. * i40evf_map_vector_to_rxq - associate irqs with rx queues
  293. * @adapter: board private structure
  294. * @v_idx: interrupt number
  295. * @r_idx: queue number
  296. **/
  297. static void
  298. i40evf_map_vector_to_rxq(struct i40evf_adapter *adapter, int v_idx, int r_idx)
  299. {
  300. struct i40e_q_vector *q_vector = adapter->q_vector[v_idx];
  301. struct i40e_ring *rx_ring = adapter->rx_rings[r_idx];
  302. rx_ring->q_vector = q_vector;
  303. rx_ring->next = q_vector->rx.ring;
  304. rx_ring->vsi = &adapter->vsi;
  305. q_vector->rx.ring = rx_ring;
  306. q_vector->rx.count++;
  307. q_vector->rx.latency_range = I40E_LOW_LATENCY;
  308. q_vector->itr_countdown = ITR_COUNTDOWN_START;
  309. }
  310. /**
  311. * i40evf_map_vector_to_txq - associate irqs with tx queues
  312. * @adapter: board private structure
  313. * @v_idx: interrupt number
  314. * @t_idx: queue number
  315. **/
  316. static void
  317. i40evf_map_vector_to_txq(struct i40evf_adapter *adapter, int v_idx, int t_idx)
  318. {
  319. struct i40e_q_vector *q_vector = adapter->q_vector[v_idx];
  320. struct i40e_ring *tx_ring = adapter->tx_rings[t_idx];
  321. tx_ring->q_vector = q_vector;
  322. tx_ring->next = q_vector->tx.ring;
  323. tx_ring->vsi = &adapter->vsi;
  324. q_vector->tx.ring = tx_ring;
  325. q_vector->tx.count++;
  326. q_vector->tx.latency_range = I40E_LOW_LATENCY;
  327. q_vector->itr_countdown = ITR_COUNTDOWN_START;
  328. q_vector->num_ringpairs++;
  329. q_vector->ring_mask |= BIT(t_idx);
  330. }
  331. /**
  332. * i40evf_map_rings_to_vectors - Maps descriptor rings to vectors
  333. * @adapter: board private structure to initialize
  334. *
  335. * This function maps descriptor rings to the queue-specific vectors
  336. * we were allotted through the MSI-X enabling code. Ideally, we'd have
  337. * one vector per ring/queue, but on a constrained vector budget, we
  338. * group the rings as "efficiently" as possible. You would add new
  339. * mapping configurations in here.
  340. **/
  341. static int i40evf_map_rings_to_vectors(struct i40evf_adapter *adapter)
  342. {
  343. int q_vectors;
  344. int v_start = 0;
  345. int rxr_idx = 0, txr_idx = 0;
  346. int rxr_remaining = adapter->num_active_queues;
  347. int txr_remaining = adapter->num_active_queues;
  348. int i, j;
  349. int rqpv, tqpv;
  350. int err = 0;
  351. q_vectors = adapter->num_msix_vectors - NONQ_VECS;
  352. /* The ideal configuration...
  353. * We have enough vectors to map one per queue.
  354. */
  355. if (q_vectors >= (rxr_remaining * 2)) {
  356. for (; rxr_idx < rxr_remaining; v_start++, rxr_idx++)
  357. i40evf_map_vector_to_rxq(adapter, v_start, rxr_idx);
  358. for (; txr_idx < txr_remaining; v_start++, txr_idx++)
  359. i40evf_map_vector_to_txq(adapter, v_start, txr_idx);
  360. goto out;
  361. }
  362. /* If we don't have enough vectors for a 1-to-1
  363. * mapping, we'll have to group them so there are
  364. * multiple queues per vector.
  365. * Re-adjusting *qpv takes care of the remainder.
  366. */
  367. for (i = v_start; i < q_vectors; i++) {
  368. rqpv = DIV_ROUND_UP(rxr_remaining, q_vectors - i);
  369. for (j = 0; j < rqpv; j++) {
  370. i40evf_map_vector_to_rxq(adapter, i, rxr_idx);
  371. rxr_idx++;
  372. rxr_remaining--;
  373. }
  374. }
  375. for (i = v_start; i < q_vectors; i++) {
  376. tqpv = DIV_ROUND_UP(txr_remaining, q_vectors - i);
  377. for (j = 0; j < tqpv; j++) {
  378. i40evf_map_vector_to_txq(adapter, i, txr_idx);
  379. txr_idx++;
  380. txr_remaining--;
  381. }
  382. }
  383. out:
  384. adapter->aq_required |= I40EVF_FLAG_AQ_MAP_VECTORS;
  385. return err;
  386. }
  387. #ifdef CONFIG_NET_POLL_CONTROLLER
  388. /**
  389. * i40evf_netpoll - A Polling 'interrupt' handler
  390. * @netdev: network interface device structure
  391. *
  392. * This is used by netconsole to send skbs without having to re-enable
  393. * interrupts. It's not called while the normal interrupt routine is executing.
  394. **/
  395. static void i40evf_netpoll(struct net_device *netdev)
  396. {
  397. struct i40evf_adapter *adapter = netdev_priv(netdev);
  398. int q_vectors = adapter->num_msix_vectors - NONQ_VECS;
  399. int i;
  400. /* if interface is down do nothing */
  401. if (test_bit(__I40E_DOWN, &adapter->vsi.state))
  402. return;
  403. for (i = 0; i < q_vectors; i++)
  404. i40evf_msix_clean_rings(0, adapter->q_vector[i]);
  405. }
  406. #endif
  407. /**
  408. * i40evf_request_traffic_irqs - Initialize MSI-X interrupts
  409. * @adapter: board private structure
  410. *
  411. * Allocates MSI-X vectors for tx and rx handling, and requests
  412. * interrupts from the kernel.
  413. **/
  414. static int
  415. i40evf_request_traffic_irqs(struct i40evf_adapter *adapter, char *basename)
  416. {
  417. int vector, err, q_vectors;
  418. int rx_int_idx = 0, tx_int_idx = 0;
  419. i40evf_irq_disable(adapter);
  420. /* Decrement for Other and TCP Timer vectors */
  421. q_vectors = adapter->num_msix_vectors - NONQ_VECS;
  422. for (vector = 0; vector < q_vectors; vector++) {
  423. struct i40e_q_vector *q_vector = adapter->q_vector[vector];
  424. if (q_vector->tx.ring && q_vector->rx.ring) {
  425. snprintf(q_vector->name, sizeof(q_vector->name) - 1,
  426. "i40evf-%s-%s-%d", basename,
  427. "TxRx", rx_int_idx++);
  428. tx_int_idx++;
  429. } else if (q_vector->rx.ring) {
  430. snprintf(q_vector->name, sizeof(q_vector->name) - 1,
  431. "i40evf-%s-%s-%d", basename,
  432. "rx", rx_int_idx++);
  433. } else if (q_vector->tx.ring) {
  434. snprintf(q_vector->name, sizeof(q_vector->name) - 1,
  435. "i40evf-%s-%s-%d", basename,
  436. "tx", tx_int_idx++);
  437. } else {
  438. /* skip this unused q_vector */
  439. continue;
  440. }
  441. err = request_irq(
  442. adapter->msix_entries[vector + NONQ_VECS].vector,
  443. i40evf_msix_clean_rings,
  444. 0,
  445. q_vector->name,
  446. q_vector);
  447. if (err) {
  448. dev_info(&adapter->pdev->dev,
  449. "Request_irq failed, error: %d\n", err);
  450. goto free_queue_irqs;
  451. }
  452. /* assign the mask for this irq */
  453. irq_set_affinity_hint(
  454. adapter->msix_entries[vector + NONQ_VECS].vector,
  455. q_vector->affinity_mask);
  456. }
  457. return 0;
  458. free_queue_irqs:
  459. while (vector) {
  460. vector--;
  461. irq_set_affinity_hint(
  462. adapter->msix_entries[vector + NONQ_VECS].vector,
  463. NULL);
  464. free_irq(adapter->msix_entries[vector + NONQ_VECS].vector,
  465. adapter->q_vector[vector]);
  466. }
  467. return err;
  468. }
  469. /**
  470. * i40evf_request_misc_irq - Initialize MSI-X interrupts
  471. * @adapter: board private structure
  472. *
  473. * Allocates MSI-X vector 0 and requests interrupts from the kernel. This
  474. * vector is only for the admin queue, and stays active even when the netdev
  475. * is closed.
  476. **/
  477. static int i40evf_request_misc_irq(struct i40evf_adapter *adapter)
  478. {
  479. struct net_device *netdev = adapter->netdev;
  480. int err;
  481. snprintf(adapter->misc_vector_name,
  482. sizeof(adapter->misc_vector_name) - 1, "i40evf-%s:mbx",
  483. dev_name(&adapter->pdev->dev));
  484. err = request_irq(adapter->msix_entries[0].vector,
  485. &i40evf_msix_aq, 0,
  486. adapter->misc_vector_name, netdev);
  487. if (err) {
  488. dev_err(&adapter->pdev->dev,
  489. "request_irq for %s failed: %d\n",
  490. adapter->misc_vector_name, err);
  491. free_irq(adapter->msix_entries[0].vector, netdev);
  492. }
  493. return err;
  494. }
  495. /**
  496. * i40evf_free_traffic_irqs - Free MSI-X interrupts
  497. * @adapter: board private structure
  498. *
  499. * Frees all MSI-X vectors other than 0.
  500. **/
  501. static void i40evf_free_traffic_irqs(struct i40evf_adapter *adapter)
  502. {
  503. int i;
  504. int q_vectors;
  505. q_vectors = adapter->num_msix_vectors - NONQ_VECS;
  506. for (i = 0; i < q_vectors; i++) {
  507. irq_set_affinity_hint(adapter->msix_entries[i+1].vector,
  508. NULL);
  509. free_irq(adapter->msix_entries[i+1].vector,
  510. adapter->q_vector[i]);
  511. }
  512. }
  513. /**
  514. * i40evf_free_misc_irq - Free MSI-X miscellaneous vector
  515. * @adapter: board private structure
  516. *
  517. * Frees MSI-X vector 0.
  518. **/
  519. static void i40evf_free_misc_irq(struct i40evf_adapter *adapter)
  520. {
  521. struct net_device *netdev = adapter->netdev;
  522. free_irq(adapter->msix_entries[0].vector, netdev);
  523. }
  524. /**
  525. * i40evf_configure_tx - Configure Transmit Unit after Reset
  526. * @adapter: board private structure
  527. *
  528. * Configure the Tx unit of the MAC after a reset.
  529. **/
  530. static void i40evf_configure_tx(struct i40evf_adapter *adapter)
  531. {
  532. struct i40e_hw *hw = &adapter->hw;
  533. int i;
  534. for (i = 0; i < adapter->num_active_queues; i++)
  535. adapter->tx_rings[i]->tail = hw->hw_addr + I40E_QTX_TAIL1(i);
  536. }
  537. /**
  538. * i40evf_configure_rx - Configure Receive Unit after Reset
  539. * @adapter: board private structure
  540. *
  541. * Configure the Rx unit of the MAC after a reset.
  542. **/
  543. static void i40evf_configure_rx(struct i40evf_adapter *adapter)
  544. {
  545. struct i40e_hw *hw = &adapter->hw;
  546. struct net_device *netdev = adapter->netdev;
  547. int max_frame = netdev->mtu + ETH_HLEN + ETH_FCS_LEN;
  548. int i;
  549. int rx_buf_len;
  550. adapter->flags &= ~I40EVF_FLAG_RX_PS_CAPABLE;
  551. adapter->flags |= I40EVF_FLAG_RX_1BUF_CAPABLE;
  552. /* Decide whether to use packet split mode or not */
  553. if (netdev->mtu > ETH_DATA_LEN) {
  554. if (adapter->flags & I40EVF_FLAG_RX_PS_CAPABLE)
  555. adapter->flags |= I40EVF_FLAG_RX_PS_ENABLED;
  556. else
  557. adapter->flags &= ~I40EVF_FLAG_RX_PS_ENABLED;
  558. } else {
  559. if (adapter->flags & I40EVF_FLAG_RX_1BUF_CAPABLE)
  560. adapter->flags &= ~I40EVF_FLAG_RX_PS_ENABLED;
  561. else
  562. adapter->flags |= I40EVF_FLAG_RX_PS_ENABLED;
  563. }
  564. /* Set the RX buffer length according to the mode */
  565. if (adapter->flags & I40EVF_FLAG_RX_PS_ENABLED) {
  566. rx_buf_len = I40E_RX_HDR_SIZE;
  567. } else {
  568. if (netdev->mtu <= ETH_DATA_LEN)
  569. rx_buf_len = I40EVF_RXBUFFER_2048;
  570. else
  571. rx_buf_len = ALIGN(max_frame, 1024);
  572. }
  573. for (i = 0; i < adapter->num_active_queues; i++) {
  574. adapter->rx_rings[i]->tail = hw->hw_addr + I40E_QRX_TAIL1(i);
  575. adapter->rx_rings[i]->rx_buf_len = rx_buf_len;
  576. }
  577. }
  578. /**
  579. * i40evf_find_vlan - Search filter list for specific vlan filter
  580. * @adapter: board private structure
  581. * @vlan: vlan tag
  582. *
  583. * Returns ptr to the filter object or NULL
  584. **/
  585. static struct
  586. i40evf_vlan_filter *i40evf_find_vlan(struct i40evf_adapter *adapter, u16 vlan)
  587. {
  588. struct i40evf_vlan_filter *f;
  589. list_for_each_entry(f, &adapter->vlan_filter_list, list) {
  590. if (vlan == f->vlan)
  591. return f;
  592. }
  593. return NULL;
  594. }
  595. /**
  596. * i40evf_add_vlan - Add a vlan filter to the list
  597. * @adapter: board private structure
  598. * @vlan: VLAN tag
  599. *
  600. * Returns ptr to the filter object or NULL when no memory available.
  601. **/
  602. static struct
  603. i40evf_vlan_filter *i40evf_add_vlan(struct i40evf_adapter *adapter, u16 vlan)
  604. {
  605. struct i40evf_vlan_filter *f = NULL;
  606. int count = 50;
  607. while (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK,
  608. &adapter->crit_section)) {
  609. udelay(1);
  610. if (--count == 0)
  611. goto out;
  612. }
  613. f = i40evf_find_vlan(adapter, vlan);
  614. if (!f) {
  615. f = kzalloc(sizeof(*f), GFP_ATOMIC);
  616. if (!f)
  617. goto clearout;
  618. f->vlan = vlan;
  619. INIT_LIST_HEAD(&f->list);
  620. list_add(&f->list, &adapter->vlan_filter_list);
  621. f->add = true;
  622. adapter->aq_required |= I40EVF_FLAG_AQ_ADD_VLAN_FILTER;
  623. }
  624. clearout:
  625. clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
  626. out:
  627. return f;
  628. }
  629. /**
  630. * i40evf_del_vlan - Remove a vlan filter from the list
  631. * @adapter: board private structure
  632. * @vlan: VLAN tag
  633. **/
  634. static void i40evf_del_vlan(struct i40evf_adapter *adapter, u16 vlan)
  635. {
  636. struct i40evf_vlan_filter *f;
  637. int count = 50;
  638. while (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK,
  639. &adapter->crit_section)) {
  640. udelay(1);
  641. if (--count == 0)
  642. return;
  643. }
  644. f = i40evf_find_vlan(adapter, vlan);
  645. if (f) {
  646. f->remove = true;
  647. adapter->aq_required |= I40EVF_FLAG_AQ_DEL_VLAN_FILTER;
  648. }
  649. clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
  650. }
  651. /**
  652. * i40evf_vlan_rx_add_vid - Add a VLAN filter to a device
  653. * @netdev: network device struct
  654. * @vid: VLAN tag
  655. **/
  656. static int i40evf_vlan_rx_add_vid(struct net_device *netdev,
  657. __always_unused __be16 proto, u16 vid)
  658. {
  659. struct i40evf_adapter *adapter = netdev_priv(netdev);
  660. if (!VLAN_ALLOWED(adapter))
  661. return -EIO;
  662. if (i40evf_add_vlan(adapter, vid) == NULL)
  663. return -ENOMEM;
  664. return 0;
  665. }
  666. /**
  667. * i40evf_vlan_rx_kill_vid - Remove a VLAN filter from a device
  668. * @netdev: network device struct
  669. * @vid: VLAN tag
  670. **/
  671. static int i40evf_vlan_rx_kill_vid(struct net_device *netdev,
  672. __always_unused __be16 proto, u16 vid)
  673. {
  674. struct i40evf_adapter *adapter = netdev_priv(netdev);
  675. if (VLAN_ALLOWED(adapter)) {
  676. i40evf_del_vlan(adapter, vid);
  677. return 0;
  678. }
  679. return -EIO;
  680. }
  681. /**
  682. * i40evf_find_filter - Search filter list for specific mac filter
  683. * @adapter: board private structure
  684. * @macaddr: the MAC address
  685. *
  686. * Returns ptr to the filter object or NULL
  687. **/
  688. static struct
  689. i40evf_mac_filter *i40evf_find_filter(struct i40evf_adapter *adapter,
  690. u8 *macaddr)
  691. {
  692. struct i40evf_mac_filter *f;
  693. if (!macaddr)
  694. return NULL;
  695. list_for_each_entry(f, &adapter->mac_filter_list, list) {
  696. if (ether_addr_equal(macaddr, f->macaddr))
  697. return f;
  698. }
  699. return NULL;
  700. }
  701. /**
  702. * i40e_add_filter - Add a mac filter to the filter list
  703. * @adapter: board private structure
  704. * @macaddr: the MAC address
  705. *
  706. * Returns ptr to the filter object or NULL when no memory available.
  707. **/
  708. static struct
  709. i40evf_mac_filter *i40evf_add_filter(struct i40evf_adapter *adapter,
  710. u8 *macaddr)
  711. {
  712. struct i40evf_mac_filter *f;
  713. int count = 50;
  714. if (!macaddr)
  715. return NULL;
  716. while (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK,
  717. &adapter->crit_section)) {
  718. udelay(1);
  719. if (--count == 0)
  720. return NULL;
  721. }
  722. f = i40evf_find_filter(adapter, macaddr);
  723. if (!f) {
  724. f = kzalloc(sizeof(*f), GFP_ATOMIC);
  725. if (!f) {
  726. clear_bit(__I40EVF_IN_CRITICAL_TASK,
  727. &adapter->crit_section);
  728. return NULL;
  729. }
  730. ether_addr_copy(f->macaddr, macaddr);
  731. list_add(&f->list, &adapter->mac_filter_list);
  732. f->add = true;
  733. adapter->aq_required |= I40EVF_FLAG_AQ_ADD_MAC_FILTER;
  734. }
  735. clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
  736. return f;
  737. }
  738. /**
  739. * i40evf_set_mac - NDO callback to set port mac address
  740. * @netdev: network interface device structure
  741. * @p: pointer to an address structure
  742. *
  743. * Returns 0 on success, negative on failure
  744. **/
  745. static int i40evf_set_mac(struct net_device *netdev, void *p)
  746. {
  747. struct i40evf_adapter *adapter = netdev_priv(netdev);
  748. struct i40e_hw *hw = &adapter->hw;
  749. struct i40evf_mac_filter *f;
  750. struct sockaddr *addr = p;
  751. if (!is_valid_ether_addr(addr->sa_data))
  752. return -EADDRNOTAVAIL;
  753. if (ether_addr_equal(netdev->dev_addr, addr->sa_data))
  754. return 0;
  755. if (adapter->flags & I40EVF_FLAG_ADDR_SET_BY_PF)
  756. return -EPERM;
  757. f = i40evf_find_filter(adapter, hw->mac.addr);
  758. if (f) {
  759. f->remove = true;
  760. adapter->aq_required |= I40EVF_FLAG_AQ_DEL_MAC_FILTER;
  761. }
  762. f = i40evf_add_filter(adapter, addr->sa_data);
  763. if (f) {
  764. ether_addr_copy(hw->mac.addr, addr->sa_data);
  765. ether_addr_copy(netdev->dev_addr, adapter->hw.mac.addr);
  766. }
  767. return (f == NULL) ? -ENOMEM : 0;
  768. }
  769. /**
  770. * i40evf_set_rx_mode - NDO callback to set the netdev filters
  771. * @netdev: network interface device structure
  772. **/
  773. static void i40evf_set_rx_mode(struct net_device *netdev)
  774. {
  775. struct i40evf_adapter *adapter = netdev_priv(netdev);
  776. struct i40evf_mac_filter *f, *ftmp;
  777. struct netdev_hw_addr *uca;
  778. struct netdev_hw_addr *mca;
  779. struct netdev_hw_addr *ha;
  780. int count = 50;
  781. /* add addr if not already in the filter list */
  782. netdev_for_each_uc_addr(uca, netdev) {
  783. i40evf_add_filter(adapter, uca->addr);
  784. }
  785. netdev_for_each_mc_addr(mca, netdev) {
  786. i40evf_add_filter(adapter, mca->addr);
  787. }
  788. while (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK,
  789. &adapter->crit_section)) {
  790. udelay(1);
  791. if (--count == 0) {
  792. dev_err(&adapter->pdev->dev,
  793. "Failed to get lock in %s\n", __func__);
  794. return;
  795. }
  796. }
  797. /* remove filter if not in netdev list */
  798. list_for_each_entry_safe(f, ftmp, &adapter->mac_filter_list, list) {
  799. netdev_for_each_mc_addr(mca, netdev)
  800. if (ether_addr_equal(mca->addr, f->macaddr))
  801. goto bottom_of_search_loop;
  802. netdev_for_each_uc_addr(uca, netdev)
  803. if (ether_addr_equal(uca->addr, f->macaddr))
  804. goto bottom_of_search_loop;
  805. for_each_dev_addr(netdev, ha)
  806. if (ether_addr_equal(ha->addr, f->macaddr))
  807. goto bottom_of_search_loop;
  808. if (ether_addr_equal(f->macaddr, adapter->hw.mac.addr))
  809. goto bottom_of_search_loop;
  810. /* f->macaddr wasn't found in uc, mc, or ha list so delete it */
  811. f->remove = true;
  812. adapter->aq_required |= I40EVF_FLAG_AQ_DEL_MAC_FILTER;
  813. bottom_of_search_loop:
  814. continue;
  815. }
  816. clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
  817. }
  818. /**
  819. * i40evf_napi_enable_all - enable NAPI on all queue vectors
  820. * @adapter: board private structure
  821. **/
  822. static void i40evf_napi_enable_all(struct i40evf_adapter *adapter)
  823. {
  824. int q_idx;
  825. struct i40e_q_vector *q_vector;
  826. int q_vectors = adapter->num_msix_vectors - NONQ_VECS;
  827. for (q_idx = 0; q_idx < q_vectors; q_idx++) {
  828. struct napi_struct *napi;
  829. q_vector = adapter->q_vector[q_idx];
  830. napi = &q_vector->napi;
  831. napi_enable(napi);
  832. }
  833. }
  834. /**
  835. * i40evf_napi_disable_all - disable NAPI on all queue vectors
  836. * @adapter: board private structure
  837. **/
  838. static void i40evf_napi_disable_all(struct i40evf_adapter *adapter)
  839. {
  840. int q_idx;
  841. struct i40e_q_vector *q_vector;
  842. int q_vectors = adapter->num_msix_vectors - NONQ_VECS;
  843. for (q_idx = 0; q_idx < q_vectors; q_idx++) {
  844. q_vector = adapter->q_vector[q_idx];
  845. napi_disable(&q_vector->napi);
  846. }
  847. }
  848. /**
  849. * i40evf_configure - set up transmit and receive data structures
  850. * @adapter: board private structure
  851. **/
  852. static void i40evf_configure(struct i40evf_adapter *adapter)
  853. {
  854. struct net_device *netdev = adapter->netdev;
  855. int i;
  856. i40evf_set_rx_mode(netdev);
  857. i40evf_configure_tx(adapter);
  858. i40evf_configure_rx(adapter);
  859. adapter->aq_required |= I40EVF_FLAG_AQ_CONFIGURE_QUEUES;
  860. for (i = 0; i < adapter->num_active_queues; i++) {
  861. struct i40e_ring *ring = adapter->rx_rings[i];
  862. i40evf_alloc_rx_buffers_1buf(ring, ring->count);
  863. ring->next_to_use = ring->count - 1;
  864. writel(ring->next_to_use, ring->tail);
  865. }
  866. }
  867. /**
  868. * i40evf_up_complete - Finish the last steps of bringing up a connection
  869. * @adapter: board private structure
  870. **/
  871. static int i40evf_up_complete(struct i40evf_adapter *adapter)
  872. {
  873. adapter->state = __I40EVF_RUNNING;
  874. clear_bit(__I40E_DOWN, &adapter->vsi.state);
  875. i40evf_napi_enable_all(adapter);
  876. adapter->aq_required |= I40EVF_FLAG_AQ_ENABLE_QUEUES;
  877. mod_timer_pending(&adapter->watchdog_timer, jiffies + 1);
  878. return 0;
  879. }
  880. /**
  881. * i40e_down - Shutdown the connection processing
  882. * @adapter: board private structure
  883. **/
  884. void i40evf_down(struct i40evf_adapter *adapter)
  885. {
  886. struct net_device *netdev = adapter->netdev;
  887. struct i40evf_mac_filter *f;
  888. if (adapter->state == __I40EVF_DOWN)
  889. return;
  890. while (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK,
  891. &adapter->crit_section))
  892. usleep_range(500, 1000);
  893. netif_carrier_off(netdev);
  894. netif_tx_disable(netdev);
  895. i40evf_napi_disable_all(adapter);
  896. i40evf_irq_disable(adapter);
  897. /* remove all MAC filters */
  898. list_for_each_entry(f, &adapter->mac_filter_list, list) {
  899. f->remove = true;
  900. }
  901. /* remove all VLAN filters */
  902. list_for_each_entry(f, &adapter->vlan_filter_list, list) {
  903. f->remove = true;
  904. }
  905. if (!(adapter->flags & I40EVF_FLAG_PF_COMMS_FAILED) &&
  906. adapter->state != __I40EVF_RESETTING) {
  907. /* cancel any current operation */
  908. adapter->current_op = I40E_VIRTCHNL_OP_UNKNOWN;
  909. /* Schedule operations to close down the HW. Don't wait
  910. * here for this to complete. The watchdog is still running
  911. * and it will take care of this.
  912. */
  913. adapter->aq_required = I40EVF_FLAG_AQ_DEL_MAC_FILTER;
  914. adapter->aq_required |= I40EVF_FLAG_AQ_DEL_VLAN_FILTER;
  915. adapter->aq_required |= I40EVF_FLAG_AQ_DISABLE_QUEUES;
  916. }
  917. clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
  918. }
  919. /**
  920. * i40evf_acquire_msix_vectors - Setup the MSIX capability
  921. * @adapter: board private structure
  922. * @vectors: number of vectors to request
  923. *
  924. * Work with the OS to set up the MSIX vectors needed.
  925. *
  926. * Returns 0 on success, negative on failure
  927. **/
  928. static int
  929. i40evf_acquire_msix_vectors(struct i40evf_adapter *adapter, int vectors)
  930. {
  931. int err, vector_threshold;
  932. /* We'll want at least 3 (vector_threshold):
  933. * 0) Other (Admin Queue and link, mostly)
  934. * 1) TxQ[0] Cleanup
  935. * 2) RxQ[0] Cleanup
  936. */
  937. vector_threshold = MIN_MSIX_COUNT;
  938. /* The more we get, the more we will assign to Tx/Rx Cleanup
  939. * for the separate queues...where Rx Cleanup >= Tx Cleanup.
  940. * Right now, we simply care about how many we'll get; we'll
  941. * set them up later while requesting irq's.
  942. */
  943. err = pci_enable_msix_range(adapter->pdev, adapter->msix_entries,
  944. vector_threshold, vectors);
  945. if (err < 0) {
  946. dev_err(&adapter->pdev->dev, "Unable to allocate MSI-X interrupts\n");
  947. kfree(adapter->msix_entries);
  948. adapter->msix_entries = NULL;
  949. return err;
  950. }
  951. /* Adjust for only the vectors we'll use, which is minimum
  952. * of max_msix_q_vectors + NONQ_VECS, or the number of
  953. * vectors we were allocated.
  954. */
  955. adapter->num_msix_vectors = err;
  956. return 0;
  957. }
  958. /**
  959. * i40evf_free_queues - Free memory for all rings
  960. * @adapter: board private structure to initialize
  961. *
  962. * Free all of the memory associated with queue pairs.
  963. **/
  964. static void i40evf_free_queues(struct i40evf_adapter *adapter)
  965. {
  966. int i;
  967. if (!adapter->vsi_res)
  968. return;
  969. for (i = 0; i < adapter->num_active_queues; i++) {
  970. if (adapter->tx_rings[i])
  971. kfree_rcu(adapter->tx_rings[i], rcu);
  972. adapter->tx_rings[i] = NULL;
  973. adapter->rx_rings[i] = NULL;
  974. }
  975. }
  976. /**
  977. * i40evf_alloc_queues - Allocate memory for all rings
  978. * @adapter: board private structure to initialize
  979. *
  980. * We allocate one ring per queue at run-time since we don't know the
  981. * number of queues at compile-time. The polling_netdev array is
  982. * intended for Multiqueue, but should work fine with a single queue.
  983. **/
  984. static int i40evf_alloc_queues(struct i40evf_adapter *adapter)
  985. {
  986. int i;
  987. for (i = 0; i < adapter->num_active_queues; i++) {
  988. struct i40e_ring *tx_ring;
  989. struct i40e_ring *rx_ring;
  990. tx_ring = kzalloc(sizeof(*tx_ring) * 2, GFP_KERNEL);
  991. if (!tx_ring)
  992. goto err_out;
  993. tx_ring->queue_index = i;
  994. tx_ring->netdev = adapter->netdev;
  995. tx_ring->dev = &adapter->pdev->dev;
  996. tx_ring->count = adapter->tx_desc_count;
  997. if (adapter->flags & I40E_FLAG_WB_ON_ITR_CAPABLE)
  998. tx_ring->flags |= I40E_TXR_FLAGS_WB_ON_ITR;
  999. adapter->tx_rings[i] = tx_ring;
  1000. rx_ring = &tx_ring[1];
  1001. rx_ring->queue_index = i;
  1002. rx_ring->netdev = adapter->netdev;
  1003. rx_ring->dev = &adapter->pdev->dev;
  1004. rx_ring->count = adapter->rx_desc_count;
  1005. adapter->rx_rings[i] = rx_ring;
  1006. }
  1007. return 0;
  1008. err_out:
  1009. i40evf_free_queues(adapter);
  1010. return -ENOMEM;
  1011. }
  1012. /**
  1013. * i40evf_set_interrupt_capability - set MSI-X or FAIL if not supported
  1014. * @adapter: board private structure to initialize
  1015. *
  1016. * Attempt to configure the interrupts using the best available
  1017. * capabilities of the hardware and the kernel.
  1018. **/
  1019. static int i40evf_set_interrupt_capability(struct i40evf_adapter *adapter)
  1020. {
  1021. int vector, v_budget;
  1022. int pairs = 0;
  1023. int err = 0;
  1024. if (!adapter->vsi_res) {
  1025. err = -EIO;
  1026. goto out;
  1027. }
  1028. pairs = adapter->num_active_queues;
  1029. /* It's easy to be greedy for MSI-X vectors, but it really
  1030. * doesn't do us much good if we have a lot more vectors
  1031. * than CPU's. So let's be conservative and only ask for
  1032. * (roughly) twice the number of vectors as there are CPU's.
  1033. */
  1034. v_budget = min_t(int, pairs, (int)(num_online_cpus() * 2)) + NONQ_VECS;
  1035. v_budget = min_t(int, v_budget, (int)adapter->vf_res->max_vectors);
  1036. adapter->msix_entries = kcalloc(v_budget,
  1037. sizeof(struct msix_entry), GFP_KERNEL);
  1038. if (!adapter->msix_entries) {
  1039. err = -ENOMEM;
  1040. goto out;
  1041. }
  1042. for (vector = 0; vector < v_budget; vector++)
  1043. adapter->msix_entries[vector].entry = vector;
  1044. err = i40evf_acquire_msix_vectors(adapter, v_budget);
  1045. out:
  1046. adapter->netdev->real_num_tx_queues = pairs;
  1047. return err;
  1048. }
  1049. /**
  1050. * i40e_configure_rss_aq - Prepare for RSS using AQ commands
  1051. * @vsi: vsi structure
  1052. * @seed: RSS hash seed
  1053. **/
  1054. static void i40evf_configure_rss_aq(struct i40e_vsi *vsi, const u8 *seed)
  1055. {
  1056. struct i40e_aqc_get_set_rss_key_data rss_key;
  1057. struct i40evf_adapter *adapter = vsi->back;
  1058. struct i40e_hw *hw = &adapter->hw;
  1059. int ret = 0, i;
  1060. u8 *rss_lut;
  1061. if (!vsi->id)
  1062. return;
  1063. if (adapter->current_op != I40E_VIRTCHNL_OP_UNKNOWN) {
  1064. /* bail because we already have a command pending */
  1065. dev_err(&adapter->pdev->dev, "Cannot confiure RSS, command %d pending\n",
  1066. adapter->current_op);
  1067. return;
  1068. }
  1069. memset(&rss_key, 0, sizeof(rss_key));
  1070. memcpy(&rss_key, seed, sizeof(rss_key));
  1071. rss_lut = kzalloc(((I40E_VFQF_HLUT_MAX_INDEX + 1) * 4), GFP_KERNEL);
  1072. if (!rss_lut)
  1073. return;
  1074. /* Populate the LUT with max no. PF queues in round robin fashion */
  1075. for (i = 0; i <= (I40E_VFQF_HLUT_MAX_INDEX * 4); i++)
  1076. rss_lut[i] = i % adapter->num_active_queues;
  1077. ret = i40evf_aq_set_rss_key(hw, vsi->id, &rss_key);
  1078. if (ret) {
  1079. dev_err(&adapter->pdev->dev,
  1080. "Cannot set RSS key, err %s aq_err %s\n",
  1081. i40evf_stat_str(hw, ret),
  1082. i40evf_aq_str(hw, hw->aq.asq_last_status));
  1083. return;
  1084. }
  1085. ret = i40evf_aq_set_rss_lut(hw, vsi->id, false, rss_lut,
  1086. (I40E_VFQF_HLUT_MAX_INDEX + 1) * 4);
  1087. if (ret)
  1088. dev_err(&adapter->pdev->dev,
  1089. "Cannot set RSS lut, err %s aq_err %s\n",
  1090. i40evf_stat_str(hw, ret),
  1091. i40evf_aq_str(hw, hw->aq.asq_last_status));
  1092. }
  1093. /**
  1094. * i40e_configure_rss_reg - Prepare for RSS if used
  1095. * @adapter: board private structure
  1096. * @seed: RSS hash seed
  1097. **/
  1098. static void i40evf_configure_rss_reg(struct i40evf_adapter *adapter,
  1099. const u8 *seed)
  1100. {
  1101. struct i40e_hw *hw = &adapter->hw;
  1102. u32 *seed_dw = (u32 *)seed;
  1103. u32 cqueue = 0;
  1104. u32 lut = 0;
  1105. int i, j;
  1106. /* Fill out hash function seed */
  1107. for (i = 0; i <= I40E_VFQF_HKEY_MAX_INDEX; i++)
  1108. wr32(hw, I40E_VFQF_HKEY(i), seed_dw[i]);
  1109. /* Populate the LUT with max no. PF queues in round robin fashion */
  1110. for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++) {
  1111. lut = 0;
  1112. for (j = 0; j < 4; j++) {
  1113. if (cqueue == adapter->num_active_queues)
  1114. cqueue = 0;
  1115. lut |= ((cqueue) << (8 * j));
  1116. cqueue++;
  1117. }
  1118. wr32(hw, I40E_VFQF_HLUT(i), lut);
  1119. }
  1120. i40e_flush(hw);
  1121. }
  1122. /**
  1123. * i40evf_configure_rss - Prepare for RSS
  1124. * @adapter: board private structure
  1125. **/
  1126. static void i40evf_configure_rss(struct i40evf_adapter *adapter)
  1127. {
  1128. struct i40e_hw *hw = &adapter->hw;
  1129. u8 seed[I40EVF_HKEY_ARRAY_SIZE];
  1130. u64 hena;
  1131. netdev_rss_key_fill((void *)seed, I40EVF_HKEY_ARRAY_SIZE);
  1132. /* Enable PCTYPES for RSS, TCP/UDP with IPv4/IPv6 */
  1133. hena = I40E_DEFAULT_RSS_HENA;
  1134. wr32(hw, I40E_VFQF_HENA(0), (u32)hena);
  1135. wr32(hw, I40E_VFQF_HENA(1), (u32)(hena >> 32));
  1136. if (RSS_AQ(adapter))
  1137. i40evf_configure_rss_aq(&adapter->vsi, seed);
  1138. else
  1139. i40evf_configure_rss_reg(adapter, seed);
  1140. }
  1141. /**
  1142. * i40evf_alloc_q_vectors - Allocate memory for interrupt vectors
  1143. * @adapter: board private structure to initialize
  1144. *
  1145. * We allocate one q_vector per queue interrupt. If allocation fails we
  1146. * return -ENOMEM.
  1147. **/
  1148. static int i40evf_alloc_q_vectors(struct i40evf_adapter *adapter)
  1149. {
  1150. int q_idx, num_q_vectors;
  1151. struct i40e_q_vector *q_vector;
  1152. num_q_vectors = adapter->num_msix_vectors - NONQ_VECS;
  1153. for (q_idx = 0; q_idx < num_q_vectors; q_idx++) {
  1154. q_vector = kzalloc(sizeof(*q_vector), GFP_KERNEL);
  1155. if (!q_vector)
  1156. goto err_out;
  1157. q_vector->adapter = adapter;
  1158. q_vector->vsi = &adapter->vsi;
  1159. q_vector->v_idx = q_idx;
  1160. netif_napi_add(adapter->netdev, &q_vector->napi,
  1161. i40evf_napi_poll, NAPI_POLL_WEIGHT);
  1162. adapter->q_vector[q_idx] = q_vector;
  1163. }
  1164. return 0;
  1165. err_out:
  1166. while (q_idx) {
  1167. q_idx--;
  1168. q_vector = adapter->q_vector[q_idx];
  1169. netif_napi_del(&q_vector->napi);
  1170. kfree(q_vector);
  1171. adapter->q_vector[q_idx] = NULL;
  1172. }
  1173. return -ENOMEM;
  1174. }
  1175. /**
  1176. * i40evf_free_q_vectors - Free memory allocated for interrupt vectors
  1177. * @adapter: board private structure to initialize
  1178. *
  1179. * This function frees the memory allocated to the q_vectors. In addition if
  1180. * NAPI is enabled it will delete any references to the NAPI struct prior
  1181. * to freeing the q_vector.
  1182. **/
  1183. static void i40evf_free_q_vectors(struct i40evf_adapter *adapter)
  1184. {
  1185. int q_idx, num_q_vectors;
  1186. int napi_vectors;
  1187. num_q_vectors = adapter->num_msix_vectors - NONQ_VECS;
  1188. napi_vectors = adapter->num_active_queues;
  1189. for (q_idx = 0; q_idx < num_q_vectors; q_idx++) {
  1190. struct i40e_q_vector *q_vector = adapter->q_vector[q_idx];
  1191. adapter->q_vector[q_idx] = NULL;
  1192. if (q_idx < napi_vectors)
  1193. netif_napi_del(&q_vector->napi);
  1194. kfree(q_vector);
  1195. }
  1196. }
  1197. /**
  1198. * i40evf_reset_interrupt_capability - Reset MSIX setup
  1199. * @adapter: board private structure
  1200. *
  1201. **/
  1202. void i40evf_reset_interrupt_capability(struct i40evf_adapter *adapter)
  1203. {
  1204. pci_disable_msix(adapter->pdev);
  1205. kfree(adapter->msix_entries);
  1206. adapter->msix_entries = NULL;
  1207. }
  1208. /**
  1209. * i40evf_init_interrupt_scheme - Determine if MSIX is supported and init
  1210. * @adapter: board private structure to initialize
  1211. *
  1212. **/
  1213. int i40evf_init_interrupt_scheme(struct i40evf_adapter *adapter)
  1214. {
  1215. int err;
  1216. err = i40evf_set_interrupt_capability(adapter);
  1217. if (err) {
  1218. dev_err(&adapter->pdev->dev,
  1219. "Unable to setup interrupt capabilities\n");
  1220. goto err_set_interrupt;
  1221. }
  1222. err = i40evf_alloc_q_vectors(adapter);
  1223. if (err) {
  1224. dev_err(&adapter->pdev->dev,
  1225. "Unable to allocate memory for queue vectors\n");
  1226. goto err_alloc_q_vectors;
  1227. }
  1228. err = i40evf_alloc_queues(adapter);
  1229. if (err) {
  1230. dev_err(&adapter->pdev->dev,
  1231. "Unable to allocate memory for queues\n");
  1232. goto err_alloc_queues;
  1233. }
  1234. dev_info(&adapter->pdev->dev, "Multiqueue %s: Queue pair count = %u",
  1235. (adapter->num_active_queues > 1) ? "Enabled" : "Disabled",
  1236. adapter->num_active_queues);
  1237. return 0;
  1238. err_alloc_queues:
  1239. i40evf_free_q_vectors(adapter);
  1240. err_alloc_q_vectors:
  1241. i40evf_reset_interrupt_capability(adapter);
  1242. err_set_interrupt:
  1243. return err;
  1244. }
  1245. /**
  1246. * i40evf_watchdog_timer - Periodic call-back timer
  1247. * @data: pointer to adapter disguised as unsigned long
  1248. **/
  1249. static void i40evf_watchdog_timer(unsigned long data)
  1250. {
  1251. struct i40evf_adapter *adapter = (struct i40evf_adapter *)data;
  1252. schedule_work(&adapter->watchdog_task);
  1253. /* timer will be rescheduled in watchdog task */
  1254. }
  1255. /**
  1256. * i40evf_watchdog_task - Periodic call-back task
  1257. * @work: pointer to work_struct
  1258. **/
  1259. static void i40evf_watchdog_task(struct work_struct *work)
  1260. {
  1261. struct i40evf_adapter *adapter = container_of(work,
  1262. struct i40evf_adapter,
  1263. watchdog_task);
  1264. struct i40e_hw *hw = &adapter->hw;
  1265. u32 reg_val;
  1266. if (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section))
  1267. goto restart_watchdog;
  1268. if (adapter->flags & I40EVF_FLAG_PF_COMMS_FAILED) {
  1269. reg_val = rd32(hw, I40E_VFGEN_RSTAT) &
  1270. I40E_VFGEN_RSTAT_VFR_STATE_MASK;
  1271. if ((reg_val == I40E_VFR_VFACTIVE) ||
  1272. (reg_val == I40E_VFR_COMPLETED)) {
  1273. /* A chance for redemption! */
  1274. dev_err(&adapter->pdev->dev, "Hardware came out of reset. Attempting reinit.\n");
  1275. adapter->state = __I40EVF_STARTUP;
  1276. adapter->flags &= ~I40EVF_FLAG_PF_COMMS_FAILED;
  1277. schedule_delayed_work(&adapter->init_task, 10);
  1278. clear_bit(__I40EVF_IN_CRITICAL_TASK,
  1279. &adapter->crit_section);
  1280. /* Don't reschedule the watchdog, since we've restarted
  1281. * the init task. When init_task contacts the PF and
  1282. * gets everything set up again, it'll restart the
  1283. * watchdog for us. Down, boy. Sit. Stay. Woof.
  1284. */
  1285. return;
  1286. }
  1287. adapter->aq_required = 0;
  1288. adapter->current_op = I40E_VIRTCHNL_OP_UNKNOWN;
  1289. goto watchdog_done;
  1290. }
  1291. if ((adapter->state < __I40EVF_DOWN) ||
  1292. (adapter->flags & I40EVF_FLAG_RESET_PENDING))
  1293. goto watchdog_done;
  1294. /* check for reset */
  1295. reg_val = rd32(hw, I40E_VF_ARQLEN1) & I40E_VF_ARQLEN1_ARQENABLE_MASK;
  1296. if (!(adapter->flags & I40EVF_FLAG_RESET_PENDING) && !reg_val) {
  1297. adapter->state = __I40EVF_RESETTING;
  1298. adapter->flags |= I40EVF_FLAG_RESET_PENDING;
  1299. dev_err(&adapter->pdev->dev, "Hardware reset detected\n");
  1300. schedule_work(&adapter->reset_task);
  1301. adapter->aq_required = 0;
  1302. adapter->current_op = I40E_VIRTCHNL_OP_UNKNOWN;
  1303. goto watchdog_done;
  1304. }
  1305. /* Process admin queue tasks. After init, everything gets done
  1306. * here so we don't race on the admin queue.
  1307. */
  1308. if (adapter->current_op) {
  1309. if (!i40evf_asq_done(hw)) {
  1310. dev_dbg(&adapter->pdev->dev, "Admin queue timeout\n");
  1311. i40evf_send_api_ver(adapter);
  1312. }
  1313. goto watchdog_done;
  1314. }
  1315. if (adapter->aq_required & I40EVF_FLAG_AQ_GET_CONFIG) {
  1316. i40evf_send_vf_config_msg(adapter);
  1317. goto watchdog_done;
  1318. }
  1319. if (adapter->aq_required & I40EVF_FLAG_AQ_DISABLE_QUEUES) {
  1320. i40evf_disable_queues(adapter);
  1321. goto watchdog_done;
  1322. }
  1323. if (adapter->aq_required & I40EVF_FLAG_AQ_MAP_VECTORS) {
  1324. i40evf_map_queues(adapter);
  1325. goto watchdog_done;
  1326. }
  1327. if (adapter->aq_required & I40EVF_FLAG_AQ_ADD_MAC_FILTER) {
  1328. i40evf_add_ether_addrs(adapter);
  1329. goto watchdog_done;
  1330. }
  1331. if (adapter->aq_required & I40EVF_FLAG_AQ_ADD_VLAN_FILTER) {
  1332. i40evf_add_vlans(adapter);
  1333. goto watchdog_done;
  1334. }
  1335. if (adapter->aq_required & I40EVF_FLAG_AQ_DEL_MAC_FILTER) {
  1336. i40evf_del_ether_addrs(adapter);
  1337. goto watchdog_done;
  1338. }
  1339. if (adapter->aq_required & I40EVF_FLAG_AQ_DEL_VLAN_FILTER) {
  1340. i40evf_del_vlans(adapter);
  1341. goto watchdog_done;
  1342. }
  1343. if (adapter->aq_required & I40EVF_FLAG_AQ_CONFIGURE_QUEUES) {
  1344. i40evf_configure_queues(adapter);
  1345. goto watchdog_done;
  1346. }
  1347. if (adapter->aq_required & I40EVF_FLAG_AQ_ENABLE_QUEUES) {
  1348. i40evf_enable_queues(adapter);
  1349. goto watchdog_done;
  1350. }
  1351. if (adapter->aq_required & I40EVF_FLAG_AQ_CONFIGURE_RSS) {
  1352. /* This message goes straight to the firmware, not the
  1353. * PF, so we don't have to set current_op as we will
  1354. * not get a response through the ARQ.
  1355. */
  1356. i40evf_configure_rss(adapter);
  1357. adapter->aq_required &= ~I40EVF_FLAG_AQ_CONFIGURE_RSS;
  1358. goto watchdog_done;
  1359. }
  1360. if (adapter->state == __I40EVF_RUNNING)
  1361. i40evf_request_stats(adapter);
  1362. watchdog_done:
  1363. if (adapter->state == __I40EVF_RUNNING) {
  1364. i40evf_irq_enable_queues(adapter, ~0);
  1365. i40evf_fire_sw_int(adapter, 0xFF);
  1366. } else {
  1367. i40evf_fire_sw_int(adapter, 0x1);
  1368. }
  1369. clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
  1370. restart_watchdog:
  1371. if (adapter->state == __I40EVF_REMOVE)
  1372. return;
  1373. if (adapter->aq_required)
  1374. mod_timer(&adapter->watchdog_timer,
  1375. jiffies + msecs_to_jiffies(20));
  1376. else
  1377. mod_timer(&adapter->watchdog_timer, jiffies + (HZ * 2));
  1378. schedule_work(&adapter->adminq_task);
  1379. }
  1380. #define I40EVF_RESET_WAIT_MS 10
  1381. #define I40EVF_RESET_WAIT_COUNT 500
  1382. /**
  1383. * i40evf_reset_task - Call-back task to handle hardware reset
  1384. * @work: pointer to work_struct
  1385. *
  1386. * During reset we need to shut down and reinitialize the admin queue
  1387. * before we can use it to communicate with the PF again. We also clear
  1388. * and reinit the rings because that context is lost as well.
  1389. **/
  1390. static void i40evf_reset_task(struct work_struct *work)
  1391. {
  1392. struct i40evf_adapter *adapter = container_of(work,
  1393. struct i40evf_adapter,
  1394. reset_task);
  1395. struct net_device *netdev = adapter->netdev;
  1396. struct i40e_hw *hw = &adapter->hw;
  1397. struct i40evf_vlan_filter *vlf;
  1398. struct i40evf_mac_filter *f;
  1399. u32 reg_val;
  1400. int i = 0, err;
  1401. while (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK,
  1402. &adapter->crit_section))
  1403. usleep_range(500, 1000);
  1404. i40evf_misc_irq_disable(adapter);
  1405. if (adapter->flags & I40EVF_FLAG_RESET_NEEDED) {
  1406. adapter->flags &= ~I40EVF_FLAG_RESET_NEEDED;
  1407. /* Restart the AQ here. If we have been reset but didn't
  1408. * detect it, or if the PF had to reinit, our AQ will be hosed.
  1409. */
  1410. i40evf_shutdown_adminq(hw);
  1411. i40evf_init_adminq(hw);
  1412. i40evf_request_reset(adapter);
  1413. }
  1414. adapter->flags |= I40EVF_FLAG_RESET_PENDING;
  1415. /* poll until we see the reset actually happen */
  1416. for (i = 0; i < I40EVF_RESET_WAIT_COUNT; i++) {
  1417. reg_val = rd32(hw, I40E_VF_ARQLEN1) &
  1418. I40E_VF_ARQLEN1_ARQENABLE_MASK;
  1419. if (!reg_val)
  1420. break;
  1421. usleep_range(5000, 10000);
  1422. }
  1423. if (i == I40EVF_RESET_WAIT_COUNT) {
  1424. dev_info(&adapter->pdev->dev, "Never saw reset\n");
  1425. goto continue_reset; /* act like the reset happened */
  1426. }
  1427. /* wait until the reset is complete and the PF is responding to us */
  1428. for (i = 0; i < I40EVF_RESET_WAIT_COUNT; i++) {
  1429. reg_val = rd32(hw, I40E_VFGEN_RSTAT) &
  1430. I40E_VFGEN_RSTAT_VFR_STATE_MASK;
  1431. if (reg_val == I40E_VFR_VFACTIVE)
  1432. break;
  1433. msleep(I40EVF_RESET_WAIT_MS);
  1434. }
  1435. /* extra wait to make sure minimum wait is met */
  1436. msleep(I40EVF_RESET_WAIT_MS);
  1437. if (i == I40EVF_RESET_WAIT_COUNT) {
  1438. struct i40evf_mac_filter *ftmp;
  1439. struct i40evf_vlan_filter *fv, *fvtmp;
  1440. /* reset never finished */
  1441. dev_err(&adapter->pdev->dev, "Reset never finished (%x)\n",
  1442. reg_val);
  1443. adapter->flags |= I40EVF_FLAG_PF_COMMS_FAILED;
  1444. if (netif_running(adapter->netdev)) {
  1445. set_bit(__I40E_DOWN, &adapter->vsi.state);
  1446. netif_carrier_off(netdev);
  1447. netif_tx_disable(netdev);
  1448. i40evf_napi_disable_all(adapter);
  1449. i40evf_irq_disable(adapter);
  1450. i40evf_free_traffic_irqs(adapter);
  1451. i40evf_free_all_tx_resources(adapter);
  1452. i40evf_free_all_rx_resources(adapter);
  1453. }
  1454. /* Delete all of the filters, both MAC and VLAN. */
  1455. list_for_each_entry_safe(f, ftmp, &adapter->mac_filter_list,
  1456. list) {
  1457. list_del(&f->list);
  1458. kfree(f);
  1459. }
  1460. list_for_each_entry_safe(fv, fvtmp, &adapter->vlan_filter_list,
  1461. list) {
  1462. list_del(&fv->list);
  1463. kfree(fv);
  1464. }
  1465. i40evf_free_misc_irq(adapter);
  1466. i40evf_reset_interrupt_capability(adapter);
  1467. i40evf_free_queues(adapter);
  1468. i40evf_free_q_vectors(adapter);
  1469. kfree(adapter->vf_res);
  1470. i40evf_shutdown_adminq(hw);
  1471. adapter->netdev->flags &= ~IFF_UP;
  1472. clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
  1473. adapter->flags &= ~I40EVF_FLAG_RESET_PENDING;
  1474. dev_info(&adapter->pdev->dev, "Reset task did not complete, VF disabled\n");
  1475. return; /* Do not attempt to reinit. It's dead, Jim. */
  1476. }
  1477. continue_reset:
  1478. if (netif_running(adapter->netdev)) {
  1479. netif_carrier_off(netdev);
  1480. netif_tx_stop_all_queues(netdev);
  1481. i40evf_napi_disable_all(adapter);
  1482. }
  1483. i40evf_irq_disable(adapter);
  1484. adapter->state = __I40EVF_RESETTING;
  1485. adapter->flags &= ~I40EVF_FLAG_RESET_PENDING;
  1486. /* free the Tx/Rx rings and descriptors, might be better to just
  1487. * re-use them sometime in the future
  1488. */
  1489. i40evf_free_all_rx_resources(adapter);
  1490. i40evf_free_all_tx_resources(adapter);
  1491. /* kill and reinit the admin queue */
  1492. if (i40evf_shutdown_adminq(hw))
  1493. dev_warn(&adapter->pdev->dev, "Failed to shut down adminq\n");
  1494. adapter->current_op = I40E_VIRTCHNL_OP_UNKNOWN;
  1495. err = i40evf_init_adminq(hw);
  1496. if (err)
  1497. dev_info(&adapter->pdev->dev, "Failed to init adminq: %d\n",
  1498. err);
  1499. adapter->aq_required = I40EVF_FLAG_AQ_GET_CONFIG;
  1500. adapter->aq_required |= I40EVF_FLAG_AQ_MAP_VECTORS;
  1501. /* re-add all MAC filters */
  1502. list_for_each_entry(f, &adapter->mac_filter_list, list) {
  1503. f->add = true;
  1504. }
  1505. /* re-add all VLAN filters */
  1506. list_for_each_entry(vlf, &adapter->vlan_filter_list, list) {
  1507. vlf->add = true;
  1508. }
  1509. adapter->aq_required |= I40EVF_FLAG_AQ_ADD_MAC_FILTER;
  1510. adapter->aq_required |= I40EVF_FLAG_AQ_ADD_VLAN_FILTER;
  1511. clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
  1512. i40evf_misc_irq_enable(adapter);
  1513. mod_timer(&adapter->watchdog_timer, jiffies + 2);
  1514. if (netif_running(adapter->netdev)) {
  1515. /* allocate transmit descriptors */
  1516. err = i40evf_setup_all_tx_resources(adapter);
  1517. if (err)
  1518. goto reset_err;
  1519. /* allocate receive descriptors */
  1520. err = i40evf_setup_all_rx_resources(adapter);
  1521. if (err)
  1522. goto reset_err;
  1523. i40evf_configure(adapter);
  1524. err = i40evf_up_complete(adapter);
  1525. if (err)
  1526. goto reset_err;
  1527. i40evf_irq_enable(adapter, true);
  1528. } else {
  1529. adapter->state = __I40EVF_DOWN;
  1530. }
  1531. return;
  1532. reset_err:
  1533. dev_err(&adapter->pdev->dev, "failed to allocate resources during reinit\n");
  1534. i40evf_close(adapter->netdev);
  1535. }
  1536. /**
  1537. * i40evf_adminq_task - worker thread to clean the admin queue
  1538. * @work: pointer to work_struct containing our data
  1539. **/
  1540. static void i40evf_adminq_task(struct work_struct *work)
  1541. {
  1542. struct i40evf_adapter *adapter =
  1543. container_of(work, struct i40evf_adapter, adminq_task);
  1544. struct i40e_hw *hw = &adapter->hw;
  1545. struct i40e_arq_event_info event;
  1546. struct i40e_virtchnl_msg *v_msg;
  1547. i40e_status ret;
  1548. u32 val, oldval;
  1549. u16 pending;
  1550. if (adapter->flags & I40EVF_FLAG_PF_COMMS_FAILED)
  1551. goto out;
  1552. event.buf_len = I40EVF_MAX_AQ_BUF_SIZE;
  1553. event.msg_buf = kzalloc(event.buf_len, GFP_KERNEL);
  1554. if (!event.msg_buf)
  1555. goto out;
  1556. v_msg = (struct i40e_virtchnl_msg *)&event.desc;
  1557. do {
  1558. ret = i40evf_clean_arq_element(hw, &event, &pending);
  1559. if (ret || !v_msg->v_opcode)
  1560. break; /* No event to process or error cleaning ARQ */
  1561. i40evf_virtchnl_completion(adapter, v_msg->v_opcode,
  1562. v_msg->v_retval, event.msg_buf,
  1563. event.msg_len);
  1564. if (pending != 0)
  1565. memset(event.msg_buf, 0, I40EVF_MAX_AQ_BUF_SIZE);
  1566. } while (pending);
  1567. if ((adapter->flags &
  1568. (I40EVF_FLAG_RESET_PENDING | I40EVF_FLAG_RESET_NEEDED)) ||
  1569. adapter->state == __I40EVF_RESETTING)
  1570. goto freedom;
  1571. /* check for error indications */
  1572. val = rd32(hw, hw->aq.arq.len);
  1573. oldval = val;
  1574. if (val & I40E_VF_ARQLEN1_ARQVFE_MASK) {
  1575. dev_info(&adapter->pdev->dev, "ARQ VF Error detected\n");
  1576. val &= ~I40E_VF_ARQLEN1_ARQVFE_MASK;
  1577. }
  1578. if (val & I40E_VF_ARQLEN1_ARQOVFL_MASK) {
  1579. dev_info(&adapter->pdev->dev, "ARQ Overflow Error detected\n");
  1580. val &= ~I40E_VF_ARQLEN1_ARQOVFL_MASK;
  1581. }
  1582. if (val & I40E_VF_ARQLEN1_ARQCRIT_MASK) {
  1583. dev_info(&adapter->pdev->dev, "ARQ Critical Error detected\n");
  1584. val &= ~I40E_VF_ARQLEN1_ARQCRIT_MASK;
  1585. }
  1586. if (oldval != val)
  1587. wr32(hw, hw->aq.arq.len, val);
  1588. val = rd32(hw, hw->aq.asq.len);
  1589. oldval = val;
  1590. if (val & I40E_VF_ATQLEN1_ATQVFE_MASK) {
  1591. dev_info(&adapter->pdev->dev, "ASQ VF Error detected\n");
  1592. val &= ~I40E_VF_ATQLEN1_ATQVFE_MASK;
  1593. }
  1594. if (val & I40E_VF_ATQLEN1_ATQOVFL_MASK) {
  1595. dev_info(&adapter->pdev->dev, "ASQ Overflow Error detected\n");
  1596. val &= ~I40E_VF_ATQLEN1_ATQOVFL_MASK;
  1597. }
  1598. if (val & I40E_VF_ATQLEN1_ATQCRIT_MASK) {
  1599. dev_info(&adapter->pdev->dev, "ASQ Critical Error detected\n");
  1600. val &= ~I40E_VF_ATQLEN1_ATQCRIT_MASK;
  1601. }
  1602. if (oldval != val)
  1603. wr32(hw, hw->aq.asq.len, val);
  1604. freedom:
  1605. kfree(event.msg_buf);
  1606. out:
  1607. /* re-enable Admin queue interrupt cause */
  1608. i40evf_misc_irq_enable(adapter);
  1609. }
  1610. /**
  1611. * i40evf_free_all_tx_resources - Free Tx Resources for All Queues
  1612. * @adapter: board private structure
  1613. *
  1614. * Free all transmit software resources
  1615. **/
  1616. void i40evf_free_all_tx_resources(struct i40evf_adapter *adapter)
  1617. {
  1618. int i;
  1619. for (i = 0; i < adapter->num_active_queues; i++)
  1620. if (adapter->tx_rings[i]->desc)
  1621. i40evf_free_tx_resources(adapter->tx_rings[i]);
  1622. }
  1623. /**
  1624. * i40evf_setup_all_tx_resources - allocate all queues Tx resources
  1625. * @adapter: board private structure
  1626. *
  1627. * If this function returns with an error, then it's possible one or
  1628. * more of the rings is populated (while the rest are not). It is the
  1629. * callers duty to clean those orphaned rings.
  1630. *
  1631. * Return 0 on success, negative on failure
  1632. **/
  1633. static int i40evf_setup_all_tx_resources(struct i40evf_adapter *adapter)
  1634. {
  1635. int i, err = 0;
  1636. for (i = 0; i < adapter->num_active_queues; i++) {
  1637. adapter->tx_rings[i]->count = adapter->tx_desc_count;
  1638. err = i40evf_setup_tx_descriptors(adapter->tx_rings[i]);
  1639. if (!err)
  1640. continue;
  1641. dev_err(&adapter->pdev->dev,
  1642. "Allocation for Tx Queue %u failed\n", i);
  1643. break;
  1644. }
  1645. return err;
  1646. }
  1647. /**
  1648. * i40evf_setup_all_rx_resources - allocate all queues Rx resources
  1649. * @adapter: board private structure
  1650. *
  1651. * If this function returns with an error, then it's possible one or
  1652. * more of the rings is populated (while the rest are not). It is the
  1653. * callers duty to clean those orphaned rings.
  1654. *
  1655. * Return 0 on success, negative on failure
  1656. **/
  1657. static int i40evf_setup_all_rx_resources(struct i40evf_adapter *adapter)
  1658. {
  1659. int i, err = 0;
  1660. for (i = 0; i < adapter->num_active_queues; i++) {
  1661. adapter->rx_rings[i]->count = adapter->rx_desc_count;
  1662. err = i40evf_setup_rx_descriptors(adapter->rx_rings[i]);
  1663. if (!err)
  1664. continue;
  1665. dev_err(&adapter->pdev->dev,
  1666. "Allocation for Rx Queue %u failed\n", i);
  1667. break;
  1668. }
  1669. return err;
  1670. }
  1671. /**
  1672. * i40evf_free_all_rx_resources - Free Rx Resources for All Queues
  1673. * @adapter: board private structure
  1674. *
  1675. * Free all receive software resources
  1676. **/
  1677. void i40evf_free_all_rx_resources(struct i40evf_adapter *adapter)
  1678. {
  1679. int i;
  1680. for (i = 0; i < adapter->num_active_queues; i++)
  1681. if (adapter->rx_rings[i]->desc)
  1682. i40evf_free_rx_resources(adapter->rx_rings[i]);
  1683. }
  1684. /**
  1685. * i40evf_open - Called when a network interface is made active
  1686. * @netdev: network interface device structure
  1687. *
  1688. * Returns 0 on success, negative value on failure
  1689. *
  1690. * The open entry point is called when a network interface is made
  1691. * active by the system (IFF_UP). At this point all resources needed
  1692. * for transmit and receive operations are allocated, the interrupt
  1693. * handler is registered with the OS, the watchdog timer is started,
  1694. * and the stack is notified that the interface is ready.
  1695. **/
  1696. static int i40evf_open(struct net_device *netdev)
  1697. {
  1698. struct i40evf_adapter *adapter = netdev_priv(netdev);
  1699. int err;
  1700. if (adapter->flags & I40EVF_FLAG_PF_COMMS_FAILED) {
  1701. dev_err(&adapter->pdev->dev, "Unable to open device due to PF driver failure.\n");
  1702. return -EIO;
  1703. }
  1704. if (adapter->state != __I40EVF_DOWN || adapter->aq_required)
  1705. return -EBUSY;
  1706. /* allocate transmit descriptors */
  1707. err = i40evf_setup_all_tx_resources(adapter);
  1708. if (err)
  1709. goto err_setup_tx;
  1710. /* allocate receive descriptors */
  1711. err = i40evf_setup_all_rx_resources(adapter);
  1712. if (err)
  1713. goto err_setup_rx;
  1714. /* clear any pending interrupts, may auto mask */
  1715. err = i40evf_request_traffic_irqs(adapter, netdev->name);
  1716. if (err)
  1717. goto err_req_irq;
  1718. i40evf_add_filter(adapter, adapter->hw.mac.addr);
  1719. i40evf_configure(adapter);
  1720. err = i40evf_up_complete(adapter);
  1721. if (err)
  1722. goto err_req_irq;
  1723. i40evf_irq_enable(adapter, true);
  1724. return 0;
  1725. err_req_irq:
  1726. i40evf_down(adapter);
  1727. i40evf_free_traffic_irqs(adapter);
  1728. err_setup_rx:
  1729. i40evf_free_all_rx_resources(adapter);
  1730. err_setup_tx:
  1731. i40evf_free_all_tx_resources(adapter);
  1732. return err;
  1733. }
  1734. /**
  1735. * i40evf_close - Disables a network interface
  1736. * @netdev: network interface device structure
  1737. *
  1738. * Returns 0, this is not allowed to fail
  1739. *
  1740. * The close entry point is called when an interface is de-activated
  1741. * by the OS. The hardware is still under the drivers control, but
  1742. * needs to be disabled. All IRQs except vector 0 (reserved for admin queue)
  1743. * are freed, along with all transmit and receive resources.
  1744. **/
  1745. static int i40evf_close(struct net_device *netdev)
  1746. {
  1747. struct i40evf_adapter *adapter = netdev_priv(netdev);
  1748. if (adapter->state <= __I40EVF_DOWN)
  1749. return 0;
  1750. set_bit(__I40E_DOWN, &adapter->vsi.state);
  1751. i40evf_down(adapter);
  1752. adapter->state = __I40EVF_DOWN;
  1753. i40evf_free_traffic_irqs(adapter);
  1754. return 0;
  1755. }
  1756. /**
  1757. * i40evf_get_stats - Get System Network Statistics
  1758. * @netdev: network interface device structure
  1759. *
  1760. * Returns the address of the device statistics structure.
  1761. * The statistics are actually updated from the timer callback.
  1762. **/
  1763. static struct net_device_stats *i40evf_get_stats(struct net_device *netdev)
  1764. {
  1765. struct i40evf_adapter *adapter = netdev_priv(netdev);
  1766. /* only return the current stats */
  1767. return &adapter->net_stats;
  1768. }
  1769. /**
  1770. * i40evf_change_mtu - Change the Maximum Transfer Unit
  1771. * @netdev: network interface device structure
  1772. * @new_mtu: new value for maximum frame size
  1773. *
  1774. * Returns 0 on success, negative on failure
  1775. **/
  1776. static int i40evf_change_mtu(struct net_device *netdev, int new_mtu)
  1777. {
  1778. struct i40evf_adapter *adapter = netdev_priv(netdev);
  1779. int max_frame = new_mtu + ETH_HLEN + ETH_FCS_LEN;
  1780. if ((new_mtu < 68) || (max_frame > I40E_MAX_RXBUFFER))
  1781. return -EINVAL;
  1782. netdev->mtu = new_mtu;
  1783. adapter->flags |= I40EVF_FLAG_RESET_NEEDED;
  1784. schedule_work(&adapter->reset_task);
  1785. return 0;
  1786. }
  1787. static const struct net_device_ops i40evf_netdev_ops = {
  1788. .ndo_open = i40evf_open,
  1789. .ndo_stop = i40evf_close,
  1790. .ndo_start_xmit = i40evf_xmit_frame,
  1791. .ndo_get_stats = i40evf_get_stats,
  1792. .ndo_set_rx_mode = i40evf_set_rx_mode,
  1793. .ndo_validate_addr = eth_validate_addr,
  1794. .ndo_set_mac_address = i40evf_set_mac,
  1795. .ndo_change_mtu = i40evf_change_mtu,
  1796. .ndo_tx_timeout = i40evf_tx_timeout,
  1797. .ndo_vlan_rx_add_vid = i40evf_vlan_rx_add_vid,
  1798. .ndo_vlan_rx_kill_vid = i40evf_vlan_rx_kill_vid,
  1799. #ifdef CONFIG_NET_POLL_CONTROLLER
  1800. .ndo_poll_controller = i40evf_netpoll,
  1801. #endif
  1802. };
  1803. /**
  1804. * i40evf_check_reset_complete - check that VF reset is complete
  1805. * @hw: pointer to hw struct
  1806. *
  1807. * Returns 0 if device is ready to use, or -EBUSY if it's in reset.
  1808. **/
  1809. static int i40evf_check_reset_complete(struct i40e_hw *hw)
  1810. {
  1811. u32 rstat;
  1812. int i;
  1813. for (i = 0; i < 100; i++) {
  1814. rstat = rd32(hw, I40E_VFGEN_RSTAT) &
  1815. I40E_VFGEN_RSTAT_VFR_STATE_MASK;
  1816. if ((rstat == I40E_VFR_VFACTIVE) ||
  1817. (rstat == I40E_VFR_COMPLETED))
  1818. return 0;
  1819. usleep_range(10, 20);
  1820. }
  1821. return -EBUSY;
  1822. }
  1823. /**
  1824. * i40evf_process_config - Process the config information we got from the PF
  1825. * @adapter: board private structure
  1826. *
  1827. * Verify that we have a valid config struct, and set up our netdev features
  1828. * and our VSI struct.
  1829. **/
  1830. int i40evf_process_config(struct i40evf_adapter *adapter)
  1831. {
  1832. struct net_device *netdev = adapter->netdev;
  1833. int i;
  1834. /* got VF config message back from PF, now we can parse it */
  1835. for (i = 0; i < adapter->vf_res->num_vsis; i++) {
  1836. if (adapter->vf_res->vsi_res[i].vsi_type == I40E_VSI_SRIOV)
  1837. adapter->vsi_res = &adapter->vf_res->vsi_res[i];
  1838. }
  1839. if (!adapter->vsi_res) {
  1840. dev_err(&adapter->pdev->dev, "No LAN VSI found\n");
  1841. return -ENODEV;
  1842. }
  1843. if (adapter->vf_res->vf_offload_flags
  1844. & I40E_VIRTCHNL_VF_OFFLOAD_VLAN) {
  1845. netdev->vlan_features = netdev->features &
  1846. ~(NETIF_F_HW_VLAN_CTAG_TX |
  1847. NETIF_F_HW_VLAN_CTAG_RX |
  1848. NETIF_F_HW_VLAN_CTAG_FILTER);
  1849. netdev->features |= NETIF_F_HW_VLAN_CTAG_TX |
  1850. NETIF_F_HW_VLAN_CTAG_RX |
  1851. NETIF_F_HW_VLAN_CTAG_FILTER;
  1852. }
  1853. netdev->features |= NETIF_F_HIGHDMA |
  1854. NETIF_F_SG |
  1855. NETIF_F_IP_CSUM |
  1856. NETIF_F_SCTP_CSUM |
  1857. NETIF_F_IPV6_CSUM |
  1858. NETIF_F_TSO |
  1859. NETIF_F_TSO6 |
  1860. NETIF_F_RXCSUM |
  1861. NETIF_F_GRO;
  1862. /* copy netdev features into list of user selectable features */
  1863. netdev->hw_features |= netdev->features;
  1864. netdev->hw_features &= ~NETIF_F_RXCSUM;
  1865. adapter->vsi.id = adapter->vsi_res->vsi_id;
  1866. adapter->vsi.back = adapter;
  1867. adapter->vsi.base_vector = 1;
  1868. adapter->vsi.work_limit = I40E_DEFAULT_IRQ_WORK;
  1869. adapter->vsi.rx_itr_setting = (I40E_ITR_DYNAMIC |
  1870. ITR_REG_TO_USEC(I40E_ITR_RX_DEF));
  1871. adapter->vsi.tx_itr_setting = (I40E_ITR_DYNAMIC |
  1872. ITR_REG_TO_USEC(I40E_ITR_TX_DEF));
  1873. adapter->vsi.netdev = adapter->netdev;
  1874. adapter->vsi.qs_handle = adapter->vsi_res->qset_handle;
  1875. return 0;
  1876. }
  1877. /**
  1878. * i40evf_init_task - worker thread to perform delayed initialization
  1879. * @work: pointer to work_struct containing our data
  1880. *
  1881. * This task completes the work that was begun in probe. Due to the nature
  1882. * of VF-PF communications, we may need to wait tens of milliseconds to get
  1883. * responses back from the PF. Rather than busy-wait in probe and bog down the
  1884. * whole system, we'll do it in a task so we can sleep.
  1885. * This task only runs during driver init. Once we've established
  1886. * communications with the PF driver and set up our netdev, the watchdog
  1887. * takes over.
  1888. **/
  1889. static void i40evf_init_task(struct work_struct *work)
  1890. {
  1891. struct i40evf_adapter *adapter = container_of(work,
  1892. struct i40evf_adapter,
  1893. init_task.work);
  1894. struct net_device *netdev = adapter->netdev;
  1895. struct i40e_hw *hw = &adapter->hw;
  1896. struct pci_dev *pdev = adapter->pdev;
  1897. int err, bufsz;
  1898. switch (adapter->state) {
  1899. case __I40EVF_STARTUP:
  1900. /* driver loaded, probe complete */
  1901. adapter->flags &= ~I40EVF_FLAG_PF_COMMS_FAILED;
  1902. adapter->flags &= ~I40EVF_FLAG_RESET_PENDING;
  1903. err = i40e_set_mac_type(hw);
  1904. if (err) {
  1905. dev_err(&pdev->dev, "Failed to set MAC type (%d)\n",
  1906. err);
  1907. goto err;
  1908. }
  1909. err = i40evf_check_reset_complete(hw);
  1910. if (err) {
  1911. dev_info(&pdev->dev, "Device is still in reset (%d), retrying\n",
  1912. err);
  1913. goto err;
  1914. }
  1915. hw->aq.num_arq_entries = I40EVF_AQ_LEN;
  1916. hw->aq.num_asq_entries = I40EVF_AQ_LEN;
  1917. hw->aq.arq_buf_size = I40EVF_MAX_AQ_BUF_SIZE;
  1918. hw->aq.asq_buf_size = I40EVF_MAX_AQ_BUF_SIZE;
  1919. err = i40evf_init_adminq(hw);
  1920. if (err) {
  1921. dev_err(&pdev->dev, "Failed to init Admin Queue (%d)\n",
  1922. err);
  1923. goto err;
  1924. }
  1925. err = i40evf_send_api_ver(adapter);
  1926. if (err) {
  1927. dev_err(&pdev->dev, "Unable to send to PF (%d)\n", err);
  1928. i40evf_shutdown_adminq(hw);
  1929. goto err;
  1930. }
  1931. adapter->state = __I40EVF_INIT_VERSION_CHECK;
  1932. goto restart;
  1933. case __I40EVF_INIT_VERSION_CHECK:
  1934. if (!i40evf_asq_done(hw)) {
  1935. dev_err(&pdev->dev, "Admin queue command never completed\n");
  1936. i40evf_shutdown_adminq(hw);
  1937. adapter->state = __I40EVF_STARTUP;
  1938. goto err;
  1939. }
  1940. /* aq msg sent, awaiting reply */
  1941. err = i40evf_verify_api_ver(adapter);
  1942. if (err) {
  1943. if (err == I40E_ERR_ADMIN_QUEUE_NO_WORK)
  1944. err = i40evf_send_api_ver(adapter);
  1945. else
  1946. dev_err(&pdev->dev, "Unsupported PF API version %d.%d, expected %d.%d\n",
  1947. adapter->pf_version.major,
  1948. adapter->pf_version.minor,
  1949. I40E_VIRTCHNL_VERSION_MAJOR,
  1950. I40E_VIRTCHNL_VERSION_MINOR);
  1951. goto err;
  1952. }
  1953. err = i40evf_send_vf_config_msg(adapter);
  1954. if (err) {
  1955. dev_err(&pdev->dev, "Unable to send config request (%d)\n",
  1956. err);
  1957. goto err;
  1958. }
  1959. adapter->state = __I40EVF_INIT_GET_RESOURCES;
  1960. goto restart;
  1961. case __I40EVF_INIT_GET_RESOURCES:
  1962. /* aq msg sent, awaiting reply */
  1963. if (!adapter->vf_res) {
  1964. bufsz = sizeof(struct i40e_virtchnl_vf_resource) +
  1965. (I40E_MAX_VF_VSI *
  1966. sizeof(struct i40e_virtchnl_vsi_resource));
  1967. adapter->vf_res = kzalloc(bufsz, GFP_KERNEL);
  1968. if (!adapter->vf_res)
  1969. goto err;
  1970. }
  1971. err = i40evf_get_vf_config(adapter);
  1972. if (err == I40E_ERR_ADMIN_QUEUE_NO_WORK) {
  1973. err = i40evf_send_vf_config_msg(adapter);
  1974. goto err;
  1975. }
  1976. if (err) {
  1977. dev_err(&pdev->dev, "Unable to get VF config (%d)\n",
  1978. err);
  1979. goto err_alloc;
  1980. }
  1981. adapter->state = __I40EVF_INIT_SW;
  1982. break;
  1983. default:
  1984. goto err_alloc;
  1985. }
  1986. if (i40evf_process_config(adapter))
  1987. goto err_alloc;
  1988. adapter->current_op = I40E_VIRTCHNL_OP_UNKNOWN;
  1989. adapter->flags |= I40EVF_FLAG_RX_CSUM_ENABLED;
  1990. netdev->netdev_ops = &i40evf_netdev_ops;
  1991. i40evf_set_ethtool_ops(netdev);
  1992. netdev->watchdog_timeo = 5 * HZ;
  1993. if (!is_valid_ether_addr(adapter->hw.mac.addr)) {
  1994. dev_info(&pdev->dev, "Invalid MAC address %pM, using random\n",
  1995. adapter->hw.mac.addr);
  1996. eth_hw_addr_random(netdev);
  1997. ether_addr_copy(adapter->hw.mac.addr, netdev->dev_addr);
  1998. } else {
  1999. adapter->flags |= I40EVF_FLAG_ADDR_SET_BY_PF;
  2000. ether_addr_copy(netdev->dev_addr, adapter->hw.mac.addr);
  2001. ether_addr_copy(netdev->perm_addr, adapter->hw.mac.addr);
  2002. }
  2003. init_timer(&adapter->watchdog_timer);
  2004. adapter->watchdog_timer.function = &i40evf_watchdog_timer;
  2005. adapter->watchdog_timer.data = (unsigned long)adapter;
  2006. mod_timer(&adapter->watchdog_timer, jiffies + 1);
  2007. adapter->num_active_queues = min_t(int,
  2008. adapter->vsi_res->num_queue_pairs,
  2009. (int)(num_online_cpus()));
  2010. adapter->tx_desc_count = I40EVF_DEFAULT_TXD;
  2011. adapter->rx_desc_count = I40EVF_DEFAULT_RXD;
  2012. err = i40evf_init_interrupt_scheme(adapter);
  2013. if (err)
  2014. goto err_sw_init;
  2015. i40evf_map_rings_to_vectors(adapter);
  2016. if (adapter->vf_res->vf_offload_flags &
  2017. I40E_VIRTCHNL_VF_OFFLOAD_WB_ON_ITR)
  2018. adapter->flags |= I40EVF_FLAG_WB_ON_ITR_CAPABLE;
  2019. if (!RSS_AQ(adapter))
  2020. i40evf_configure_rss(adapter);
  2021. err = i40evf_request_misc_irq(adapter);
  2022. if (err)
  2023. goto err_sw_init;
  2024. netif_carrier_off(netdev);
  2025. if (!adapter->netdev_registered) {
  2026. err = register_netdev(netdev);
  2027. if (err)
  2028. goto err_register;
  2029. }
  2030. adapter->netdev_registered = true;
  2031. netif_tx_stop_all_queues(netdev);
  2032. dev_info(&pdev->dev, "MAC address: %pM\n", adapter->hw.mac.addr);
  2033. if (netdev->features & NETIF_F_GRO)
  2034. dev_info(&pdev->dev, "GRO is enabled\n");
  2035. dev_info(&pdev->dev, "%s\n", i40evf_driver_string);
  2036. adapter->state = __I40EVF_DOWN;
  2037. set_bit(__I40E_DOWN, &adapter->vsi.state);
  2038. i40evf_misc_irq_enable(adapter);
  2039. if (RSS_AQ(adapter)) {
  2040. adapter->aq_required |= I40EVF_FLAG_AQ_CONFIGURE_RSS;
  2041. mod_timer_pending(&adapter->watchdog_timer, jiffies + 1);
  2042. } else {
  2043. i40evf_configure_rss(adapter);
  2044. }
  2045. return;
  2046. restart:
  2047. schedule_delayed_work(&adapter->init_task, msecs_to_jiffies(30));
  2048. return;
  2049. err_register:
  2050. i40evf_free_misc_irq(adapter);
  2051. err_sw_init:
  2052. i40evf_reset_interrupt_capability(adapter);
  2053. err_alloc:
  2054. kfree(adapter->vf_res);
  2055. adapter->vf_res = NULL;
  2056. err:
  2057. /* Things went into the weeds, so try again later */
  2058. if (++adapter->aq_wait_count > I40EVF_AQ_MAX_ERR) {
  2059. dev_err(&pdev->dev, "Failed to communicate with PF; waiting before retry\n");
  2060. adapter->flags |= I40EVF_FLAG_PF_COMMS_FAILED;
  2061. i40evf_shutdown_adminq(hw);
  2062. adapter->state = __I40EVF_STARTUP;
  2063. schedule_delayed_work(&adapter->init_task, HZ * 5);
  2064. return;
  2065. }
  2066. schedule_delayed_work(&adapter->init_task, HZ);
  2067. }
  2068. /**
  2069. * i40evf_shutdown - Shutdown the device in preparation for a reboot
  2070. * @pdev: pci device structure
  2071. **/
  2072. static void i40evf_shutdown(struct pci_dev *pdev)
  2073. {
  2074. struct net_device *netdev = pci_get_drvdata(pdev);
  2075. struct i40evf_adapter *adapter = netdev_priv(netdev);
  2076. netif_device_detach(netdev);
  2077. if (netif_running(netdev))
  2078. i40evf_close(netdev);
  2079. /* Prevent the watchdog from running. */
  2080. adapter->state = __I40EVF_REMOVE;
  2081. adapter->aq_required = 0;
  2082. #ifdef CONFIG_PM
  2083. pci_save_state(pdev);
  2084. #endif
  2085. pci_disable_device(pdev);
  2086. }
  2087. /**
  2088. * i40evf_probe - Device Initialization Routine
  2089. * @pdev: PCI device information struct
  2090. * @ent: entry in i40evf_pci_tbl
  2091. *
  2092. * Returns 0 on success, negative on failure
  2093. *
  2094. * i40evf_probe initializes an adapter identified by a pci_dev structure.
  2095. * The OS initialization, configuring of the adapter private structure,
  2096. * and a hardware reset occur.
  2097. **/
  2098. static int i40evf_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
  2099. {
  2100. struct net_device *netdev;
  2101. struct i40evf_adapter *adapter = NULL;
  2102. struct i40e_hw *hw = NULL;
  2103. int err;
  2104. err = pci_enable_device(pdev);
  2105. if (err)
  2106. return err;
  2107. err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
  2108. if (err) {
  2109. err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
  2110. if (err) {
  2111. dev_err(&pdev->dev,
  2112. "DMA configuration failed: 0x%x\n", err);
  2113. goto err_dma;
  2114. }
  2115. }
  2116. err = pci_request_regions(pdev, i40evf_driver_name);
  2117. if (err) {
  2118. dev_err(&pdev->dev,
  2119. "pci_request_regions failed 0x%x\n", err);
  2120. goto err_pci_reg;
  2121. }
  2122. pci_enable_pcie_error_reporting(pdev);
  2123. pci_set_master(pdev);
  2124. netdev = alloc_etherdev_mq(sizeof(struct i40evf_adapter),
  2125. MAX_TX_QUEUES);
  2126. if (!netdev) {
  2127. err = -ENOMEM;
  2128. goto err_alloc_etherdev;
  2129. }
  2130. SET_NETDEV_DEV(netdev, &pdev->dev);
  2131. pci_set_drvdata(pdev, netdev);
  2132. adapter = netdev_priv(netdev);
  2133. adapter->netdev = netdev;
  2134. adapter->pdev = pdev;
  2135. hw = &adapter->hw;
  2136. hw->back = adapter;
  2137. adapter->msg_enable = BIT(DEFAULT_DEBUG_LEVEL_SHIFT) - 1;
  2138. adapter->state = __I40EVF_STARTUP;
  2139. /* Call save state here because it relies on the adapter struct. */
  2140. pci_save_state(pdev);
  2141. hw->hw_addr = ioremap(pci_resource_start(pdev, 0),
  2142. pci_resource_len(pdev, 0));
  2143. if (!hw->hw_addr) {
  2144. err = -EIO;
  2145. goto err_ioremap;
  2146. }
  2147. hw->vendor_id = pdev->vendor;
  2148. hw->device_id = pdev->device;
  2149. pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
  2150. hw->subsystem_vendor_id = pdev->subsystem_vendor;
  2151. hw->subsystem_device_id = pdev->subsystem_device;
  2152. hw->bus.device = PCI_SLOT(pdev->devfn);
  2153. hw->bus.func = PCI_FUNC(pdev->devfn);
  2154. INIT_LIST_HEAD(&adapter->mac_filter_list);
  2155. INIT_LIST_HEAD(&adapter->vlan_filter_list);
  2156. INIT_WORK(&adapter->reset_task, i40evf_reset_task);
  2157. INIT_WORK(&adapter->adminq_task, i40evf_adminq_task);
  2158. INIT_WORK(&adapter->watchdog_task, i40evf_watchdog_task);
  2159. INIT_DELAYED_WORK(&adapter->init_task, i40evf_init_task);
  2160. schedule_delayed_work(&adapter->init_task,
  2161. msecs_to_jiffies(5 * (pdev->devfn & 0x07)));
  2162. return 0;
  2163. err_ioremap:
  2164. free_netdev(netdev);
  2165. err_alloc_etherdev:
  2166. pci_release_regions(pdev);
  2167. err_pci_reg:
  2168. err_dma:
  2169. pci_disable_device(pdev);
  2170. return err;
  2171. }
  2172. #ifdef CONFIG_PM
  2173. /**
  2174. * i40evf_suspend - Power management suspend routine
  2175. * @pdev: PCI device information struct
  2176. * @state: unused
  2177. *
  2178. * Called when the system (VM) is entering sleep/suspend.
  2179. **/
  2180. static int i40evf_suspend(struct pci_dev *pdev, pm_message_t state)
  2181. {
  2182. struct net_device *netdev = pci_get_drvdata(pdev);
  2183. struct i40evf_adapter *adapter = netdev_priv(netdev);
  2184. int retval = 0;
  2185. netif_device_detach(netdev);
  2186. if (netif_running(netdev)) {
  2187. rtnl_lock();
  2188. i40evf_down(adapter);
  2189. rtnl_unlock();
  2190. }
  2191. i40evf_free_misc_irq(adapter);
  2192. i40evf_reset_interrupt_capability(adapter);
  2193. retval = pci_save_state(pdev);
  2194. if (retval)
  2195. return retval;
  2196. pci_disable_device(pdev);
  2197. return 0;
  2198. }
  2199. /**
  2200. * i40evf_resume - Power management resume routine
  2201. * @pdev: PCI device information struct
  2202. *
  2203. * Called when the system (VM) is resumed from sleep/suspend.
  2204. **/
  2205. static int i40evf_resume(struct pci_dev *pdev)
  2206. {
  2207. struct i40evf_adapter *adapter = pci_get_drvdata(pdev);
  2208. struct net_device *netdev = adapter->netdev;
  2209. u32 err;
  2210. pci_set_power_state(pdev, PCI_D0);
  2211. pci_restore_state(pdev);
  2212. /* pci_restore_state clears dev->state_saved so call
  2213. * pci_save_state to restore it.
  2214. */
  2215. pci_save_state(pdev);
  2216. err = pci_enable_device_mem(pdev);
  2217. if (err) {
  2218. dev_err(&pdev->dev, "Cannot enable PCI device from suspend.\n");
  2219. return err;
  2220. }
  2221. pci_set_master(pdev);
  2222. rtnl_lock();
  2223. err = i40evf_set_interrupt_capability(adapter);
  2224. if (err) {
  2225. rtnl_unlock();
  2226. dev_err(&pdev->dev, "Cannot enable MSI-X interrupts.\n");
  2227. return err;
  2228. }
  2229. err = i40evf_request_misc_irq(adapter);
  2230. rtnl_unlock();
  2231. if (err) {
  2232. dev_err(&pdev->dev, "Cannot get interrupt vector.\n");
  2233. return err;
  2234. }
  2235. schedule_work(&adapter->reset_task);
  2236. netif_device_attach(netdev);
  2237. return err;
  2238. }
  2239. #endif /* CONFIG_PM */
  2240. /**
  2241. * i40evf_remove - Device Removal Routine
  2242. * @pdev: PCI device information struct
  2243. *
  2244. * i40evf_remove is called by the PCI subsystem to alert the driver
  2245. * that it should release a PCI device. The could be caused by a
  2246. * Hot-Plug event, or because the driver is going to be removed from
  2247. * memory.
  2248. **/
  2249. static void i40evf_remove(struct pci_dev *pdev)
  2250. {
  2251. struct net_device *netdev = pci_get_drvdata(pdev);
  2252. struct i40evf_adapter *adapter = netdev_priv(netdev);
  2253. struct i40evf_mac_filter *f, *ftmp;
  2254. struct i40e_hw *hw = &adapter->hw;
  2255. cancel_delayed_work_sync(&adapter->init_task);
  2256. cancel_work_sync(&adapter->reset_task);
  2257. if (adapter->netdev_registered) {
  2258. unregister_netdev(netdev);
  2259. adapter->netdev_registered = false;
  2260. }
  2261. /* Shut down all the garbage mashers on the detention level */
  2262. adapter->state = __I40EVF_REMOVE;
  2263. adapter->aq_required = 0;
  2264. i40evf_request_reset(adapter);
  2265. msleep(20);
  2266. /* If the FW isn't responding, kick it once, but only once. */
  2267. if (!i40evf_asq_done(hw)) {
  2268. i40evf_request_reset(adapter);
  2269. msleep(20);
  2270. }
  2271. if (adapter->msix_entries) {
  2272. i40evf_misc_irq_disable(adapter);
  2273. i40evf_free_misc_irq(adapter);
  2274. i40evf_reset_interrupt_capability(adapter);
  2275. i40evf_free_q_vectors(adapter);
  2276. }
  2277. if (adapter->watchdog_timer.function)
  2278. del_timer_sync(&adapter->watchdog_timer);
  2279. flush_scheduled_work();
  2280. if (hw->aq.asq.count)
  2281. i40evf_shutdown_adminq(hw);
  2282. iounmap(hw->hw_addr);
  2283. pci_release_regions(pdev);
  2284. i40evf_free_all_tx_resources(adapter);
  2285. i40evf_free_all_rx_resources(adapter);
  2286. i40evf_free_queues(adapter);
  2287. kfree(adapter->vf_res);
  2288. /* If we got removed before an up/down sequence, we've got a filter
  2289. * hanging out there that we need to get rid of.
  2290. */
  2291. list_for_each_entry_safe(f, ftmp, &adapter->mac_filter_list, list) {
  2292. list_del(&f->list);
  2293. kfree(f);
  2294. }
  2295. list_for_each_entry_safe(f, ftmp, &adapter->vlan_filter_list, list) {
  2296. list_del(&f->list);
  2297. kfree(f);
  2298. }
  2299. free_netdev(netdev);
  2300. pci_disable_pcie_error_reporting(pdev);
  2301. pci_disable_device(pdev);
  2302. }
  2303. static struct pci_driver i40evf_driver = {
  2304. .name = i40evf_driver_name,
  2305. .id_table = i40evf_pci_tbl,
  2306. .probe = i40evf_probe,
  2307. .remove = i40evf_remove,
  2308. #ifdef CONFIG_PM
  2309. .suspend = i40evf_suspend,
  2310. .resume = i40evf_resume,
  2311. #endif
  2312. .shutdown = i40evf_shutdown,
  2313. };
  2314. /**
  2315. * i40e_init_module - Driver Registration Routine
  2316. *
  2317. * i40e_init_module is the first routine called when the driver is
  2318. * loaded. All it does is register with the PCI subsystem.
  2319. **/
  2320. static int __init i40evf_init_module(void)
  2321. {
  2322. int ret;
  2323. pr_info("i40evf: %s - version %s\n", i40evf_driver_string,
  2324. i40evf_driver_version);
  2325. pr_info("%s\n", i40evf_copyright);
  2326. ret = pci_register_driver(&i40evf_driver);
  2327. return ret;
  2328. }
  2329. module_init(i40evf_init_module);
  2330. /**
  2331. * i40e_exit_module - Driver Exit Cleanup Routine
  2332. *
  2333. * i40e_exit_module is called just before the driver is removed
  2334. * from memory.
  2335. **/
  2336. static void __exit i40evf_exit_module(void)
  2337. {
  2338. pci_unregister_driver(&i40evf_driver);
  2339. }
  2340. module_exit(i40evf_exit_module);
  2341. /* i40evf_main.c */