i40evf_main.c 67 KB

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