i40evf_main.c 72 KB

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