i40evf_main.c 83 KB

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