i40evf_main.c 83 KB

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