i40evf_main.c 64 KB

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