i40evf_main.c 83 KB

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