i40evf_main.c 64 KB

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