i40evf_main.c 83 KB

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