i40evf_main.c 87 KB

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