i40evf_main.c 80 KB

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