i40evf_main.c 64 KB

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