i40evf_main.c 64 KB

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