efx.c 88 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481
  1. /****************************************************************************
  2. * Driver for Solarflare network controllers and boards
  3. * Copyright 2005-2006 Fen Systems Ltd.
  4. * Copyright 2005-2013 Solarflare Communications Inc.
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License version 2 as published
  8. * by the Free Software Foundation, incorporated herein by reference.
  9. */
  10. #include <linux/module.h>
  11. #include <linux/pci.h>
  12. #include <linux/netdevice.h>
  13. #include <linux/etherdevice.h>
  14. #include <linux/delay.h>
  15. #include <linux/notifier.h>
  16. #include <linux/ip.h>
  17. #include <linux/tcp.h>
  18. #include <linux/in.h>
  19. #include <linux/ethtool.h>
  20. #include <linux/topology.h>
  21. #include <linux/gfp.h>
  22. #include <linux/aer.h>
  23. #include <linux/interrupt.h>
  24. #include "net_driver.h"
  25. #include "efx.h"
  26. #include "nic.h"
  27. #include "selftest.h"
  28. #include "sriov.h"
  29. #include "mcdi.h"
  30. #include "workarounds.h"
  31. /**************************************************************************
  32. *
  33. * Type name strings
  34. *
  35. **************************************************************************
  36. */
  37. /* Loopback mode names (see LOOPBACK_MODE()) */
  38. const unsigned int efx_loopback_mode_max = LOOPBACK_MAX;
  39. const char *const efx_loopback_mode_names[] = {
  40. [LOOPBACK_NONE] = "NONE",
  41. [LOOPBACK_DATA] = "DATAPATH",
  42. [LOOPBACK_GMAC] = "GMAC",
  43. [LOOPBACK_XGMII] = "XGMII",
  44. [LOOPBACK_XGXS] = "XGXS",
  45. [LOOPBACK_XAUI] = "XAUI",
  46. [LOOPBACK_GMII] = "GMII",
  47. [LOOPBACK_SGMII] = "SGMII",
  48. [LOOPBACK_XGBR] = "XGBR",
  49. [LOOPBACK_XFI] = "XFI",
  50. [LOOPBACK_XAUI_FAR] = "XAUI_FAR",
  51. [LOOPBACK_GMII_FAR] = "GMII_FAR",
  52. [LOOPBACK_SGMII_FAR] = "SGMII_FAR",
  53. [LOOPBACK_XFI_FAR] = "XFI_FAR",
  54. [LOOPBACK_GPHY] = "GPHY",
  55. [LOOPBACK_PHYXS] = "PHYXS",
  56. [LOOPBACK_PCS] = "PCS",
  57. [LOOPBACK_PMAPMD] = "PMA/PMD",
  58. [LOOPBACK_XPORT] = "XPORT",
  59. [LOOPBACK_XGMII_WS] = "XGMII_WS",
  60. [LOOPBACK_XAUI_WS] = "XAUI_WS",
  61. [LOOPBACK_XAUI_WS_FAR] = "XAUI_WS_FAR",
  62. [LOOPBACK_XAUI_WS_NEAR] = "XAUI_WS_NEAR",
  63. [LOOPBACK_GMII_WS] = "GMII_WS",
  64. [LOOPBACK_XFI_WS] = "XFI_WS",
  65. [LOOPBACK_XFI_WS_FAR] = "XFI_WS_FAR",
  66. [LOOPBACK_PHYXS_WS] = "PHYXS_WS",
  67. };
  68. const unsigned int efx_reset_type_max = RESET_TYPE_MAX;
  69. const char *const efx_reset_type_names[] = {
  70. [RESET_TYPE_INVISIBLE] = "INVISIBLE",
  71. [RESET_TYPE_ALL] = "ALL",
  72. [RESET_TYPE_RECOVER_OR_ALL] = "RECOVER_OR_ALL",
  73. [RESET_TYPE_WORLD] = "WORLD",
  74. [RESET_TYPE_RECOVER_OR_DISABLE] = "RECOVER_OR_DISABLE",
  75. [RESET_TYPE_DATAPATH] = "DATAPATH",
  76. [RESET_TYPE_MC_BIST] = "MC_BIST",
  77. [RESET_TYPE_DISABLE] = "DISABLE",
  78. [RESET_TYPE_TX_WATCHDOG] = "TX_WATCHDOG",
  79. [RESET_TYPE_INT_ERROR] = "INT_ERROR",
  80. [RESET_TYPE_RX_RECOVERY] = "RX_RECOVERY",
  81. [RESET_TYPE_DMA_ERROR] = "DMA_ERROR",
  82. [RESET_TYPE_TX_SKIP] = "TX_SKIP",
  83. [RESET_TYPE_MC_FAILURE] = "MC_FAILURE",
  84. [RESET_TYPE_MCDI_TIMEOUT] = "MCDI_TIMEOUT (FLR)",
  85. };
  86. /* Reset workqueue. If any NIC has a hardware failure then a reset will be
  87. * queued onto this work queue. This is not a per-nic work queue, because
  88. * efx_reset_work() acquires the rtnl lock, so resets are naturally serialised.
  89. */
  90. static struct workqueue_struct *reset_workqueue;
  91. /* How often and how many times to poll for a reset while waiting for a
  92. * BIST that another function started to complete.
  93. */
  94. #define BIST_WAIT_DELAY_MS 100
  95. #define BIST_WAIT_DELAY_COUNT 100
  96. /**************************************************************************
  97. *
  98. * Configurable values
  99. *
  100. *************************************************************************/
  101. /*
  102. * Use separate channels for TX and RX events
  103. *
  104. * Set this to 1 to use separate channels for TX and RX. It allows us
  105. * to control interrupt affinity separately for TX and RX.
  106. *
  107. * This is only used in MSI-X interrupt mode
  108. */
  109. static bool separate_tx_channels;
  110. module_param(separate_tx_channels, bool, 0444);
  111. MODULE_PARM_DESC(separate_tx_channels,
  112. "Use separate channels for TX and RX");
  113. /* This is the weight assigned to each of the (per-channel) virtual
  114. * NAPI devices.
  115. */
  116. static int napi_weight = 64;
  117. /* This is the time (in jiffies) between invocations of the hardware
  118. * monitor.
  119. * On Falcon-based NICs, this will:
  120. * - Check the on-board hardware monitor;
  121. * - Poll the link state and reconfigure the hardware as necessary.
  122. * On Siena-based NICs for power systems with EEH support, this will give EEH a
  123. * chance to start.
  124. */
  125. static unsigned int efx_monitor_interval = 1 * HZ;
  126. /* Initial interrupt moderation settings. They can be modified after
  127. * module load with ethtool.
  128. *
  129. * The default for RX should strike a balance between increasing the
  130. * round-trip latency and reducing overhead.
  131. */
  132. static unsigned int rx_irq_mod_usec = 60;
  133. /* Initial interrupt moderation settings. They can be modified after
  134. * module load with ethtool.
  135. *
  136. * This default is chosen to ensure that a 10G link does not go idle
  137. * while a TX queue is stopped after it has become full. A queue is
  138. * restarted when it drops below half full. The time this takes (assuming
  139. * worst case 3 descriptors per packet and 1024 descriptors) is
  140. * 512 / 3 * 1.2 = 205 usec.
  141. */
  142. static unsigned int tx_irq_mod_usec = 150;
  143. /* This is the first interrupt mode to try out of:
  144. * 0 => MSI-X
  145. * 1 => MSI
  146. * 2 => legacy
  147. */
  148. static unsigned int interrupt_mode;
  149. /* This is the requested number of CPUs to use for Receive-Side Scaling (RSS),
  150. * i.e. the number of CPUs among which we may distribute simultaneous
  151. * interrupt handling.
  152. *
  153. * Cards without MSI-X will only target one CPU via legacy or MSI interrupt.
  154. * The default (0) means to assign an interrupt to each core.
  155. */
  156. static unsigned int rss_cpus;
  157. module_param(rss_cpus, uint, 0444);
  158. MODULE_PARM_DESC(rss_cpus, "Number of CPUs to use for Receive-Side Scaling");
  159. static bool phy_flash_cfg;
  160. module_param(phy_flash_cfg, bool, 0644);
  161. MODULE_PARM_DESC(phy_flash_cfg, "Set PHYs into reflash mode initially");
  162. static unsigned irq_adapt_low_thresh = 8000;
  163. module_param(irq_adapt_low_thresh, uint, 0644);
  164. MODULE_PARM_DESC(irq_adapt_low_thresh,
  165. "Threshold score for reducing IRQ moderation");
  166. static unsigned irq_adapt_high_thresh = 16000;
  167. module_param(irq_adapt_high_thresh, uint, 0644);
  168. MODULE_PARM_DESC(irq_adapt_high_thresh,
  169. "Threshold score for increasing IRQ moderation");
  170. static unsigned debug = (NETIF_MSG_DRV | NETIF_MSG_PROBE |
  171. NETIF_MSG_LINK | NETIF_MSG_IFDOWN |
  172. NETIF_MSG_IFUP | NETIF_MSG_RX_ERR |
  173. NETIF_MSG_TX_ERR | NETIF_MSG_HW);
  174. module_param(debug, uint, 0);
  175. MODULE_PARM_DESC(debug, "Bitmapped debugging message enable value");
  176. /**************************************************************************
  177. *
  178. * Utility functions and prototypes
  179. *
  180. *************************************************************************/
  181. static int efx_soft_enable_interrupts(struct efx_nic *efx);
  182. static void efx_soft_disable_interrupts(struct efx_nic *efx);
  183. static void efx_remove_channel(struct efx_channel *channel);
  184. static void efx_remove_channels(struct efx_nic *efx);
  185. static const struct efx_channel_type efx_default_channel_type;
  186. static void efx_remove_port(struct efx_nic *efx);
  187. static void efx_init_napi_channel(struct efx_channel *channel);
  188. static void efx_fini_napi(struct efx_nic *efx);
  189. static void efx_fini_napi_channel(struct efx_channel *channel);
  190. static void efx_fini_struct(struct efx_nic *efx);
  191. static void efx_start_all(struct efx_nic *efx);
  192. static void efx_stop_all(struct efx_nic *efx);
  193. #define EFX_ASSERT_RESET_SERIALISED(efx) \
  194. do { \
  195. if ((efx->state == STATE_READY) || \
  196. (efx->state == STATE_RECOVERY) || \
  197. (efx->state == STATE_DISABLED)) \
  198. ASSERT_RTNL(); \
  199. } while (0)
  200. static int efx_check_disabled(struct efx_nic *efx)
  201. {
  202. if (efx->state == STATE_DISABLED || efx->state == STATE_RECOVERY) {
  203. netif_err(efx, drv, efx->net_dev,
  204. "device is disabled due to earlier errors\n");
  205. return -EIO;
  206. }
  207. return 0;
  208. }
  209. /**************************************************************************
  210. *
  211. * Event queue processing
  212. *
  213. *************************************************************************/
  214. /* Process channel's event queue
  215. *
  216. * This function is responsible for processing the event queue of a
  217. * single channel. The caller must guarantee that this function will
  218. * never be concurrently called more than once on the same channel,
  219. * though different channels may be being processed concurrently.
  220. */
  221. static int efx_process_channel(struct efx_channel *channel, int budget)
  222. {
  223. int spent;
  224. if (unlikely(!channel->enabled))
  225. return 0;
  226. spent = efx_nic_process_eventq(channel, budget);
  227. if (spent && efx_channel_has_rx_queue(channel)) {
  228. struct efx_rx_queue *rx_queue =
  229. efx_channel_get_rx_queue(channel);
  230. efx_rx_flush_packet(channel);
  231. efx_fast_push_rx_descriptors(rx_queue, true);
  232. }
  233. return spent;
  234. }
  235. /* NAPI poll handler
  236. *
  237. * NAPI guarantees serialisation of polls of the same device, which
  238. * provides the guarantee required by efx_process_channel().
  239. */
  240. static int efx_poll(struct napi_struct *napi, int budget)
  241. {
  242. struct efx_channel *channel =
  243. container_of(napi, struct efx_channel, napi_str);
  244. struct efx_nic *efx = channel->efx;
  245. int spent;
  246. if (!efx_channel_lock_napi(channel))
  247. return budget;
  248. netif_vdbg(efx, intr, efx->net_dev,
  249. "channel %d NAPI poll executing on CPU %d\n",
  250. channel->channel, raw_smp_processor_id());
  251. spent = efx_process_channel(channel, budget);
  252. if (spent < budget) {
  253. if (efx_channel_has_rx_queue(channel) &&
  254. efx->irq_rx_adaptive &&
  255. unlikely(++channel->irq_count == 1000)) {
  256. if (unlikely(channel->irq_mod_score <
  257. irq_adapt_low_thresh)) {
  258. if (channel->irq_moderation > 1) {
  259. channel->irq_moderation -= 1;
  260. efx->type->push_irq_moderation(channel);
  261. }
  262. } else if (unlikely(channel->irq_mod_score >
  263. irq_adapt_high_thresh)) {
  264. if (channel->irq_moderation <
  265. efx->irq_rx_moderation) {
  266. channel->irq_moderation += 1;
  267. efx->type->push_irq_moderation(channel);
  268. }
  269. }
  270. channel->irq_count = 0;
  271. channel->irq_mod_score = 0;
  272. }
  273. efx_filter_rfs_expire(channel);
  274. /* There is no race here; although napi_disable() will
  275. * only wait for napi_complete(), this isn't a problem
  276. * since efx_nic_eventq_read_ack() will have no effect if
  277. * interrupts have already been disabled.
  278. */
  279. napi_complete(napi);
  280. efx_nic_eventq_read_ack(channel);
  281. }
  282. efx_channel_unlock_napi(channel);
  283. return spent;
  284. }
  285. /* Create event queue
  286. * Event queue memory allocations are done only once. If the channel
  287. * is reset, the memory buffer will be reused; this guards against
  288. * errors during channel reset and also simplifies interrupt handling.
  289. */
  290. static int efx_probe_eventq(struct efx_channel *channel)
  291. {
  292. struct efx_nic *efx = channel->efx;
  293. unsigned long entries;
  294. netif_dbg(efx, probe, efx->net_dev,
  295. "chan %d create event queue\n", channel->channel);
  296. /* Build an event queue with room for one event per tx and rx buffer,
  297. * plus some extra for link state events and MCDI completions. */
  298. entries = roundup_pow_of_two(efx->rxq_entries + efx->txq_entries + 128);
  299. EFX_BUG_ON_PARANOID(entries > EFX_MAX_EVQ_SIZE);
  300. channel->eventq_mask = max(entries, EFX_MIN_EVQ_SIZE) - 1;
  301. return efx_nic_probe_eventq(channel);
  302. }
  303. /* Prepare channel's event queue */
  304. static int efx_init_eventq(struct efx_channel *channel)
  305. {
  306. struct efx_nic *efx = channel->efx;
  307. int rc;
  308. EFX_WARN_ON_PARANOID(channel->eventq_init);
  309. netif_dbg(efx, drv, efx->net_dev,
  310. "chan %d init event queue\n", channel->channel);
  311. rc = efx_nic_init_eventq(channel);
  312. if (rc == 0) {
  313. efx->type->push_irq_moderation(channel);
  314. channel->eventq_read_ptr = 0;
  315. channel->eventq_init = true;
  316. }
  317. return rc;
  318. }
  319. /* Enable event queue processing and NAPI */
  320. void efx_start_eventq(struct efx_channel *channel)
  321. {
  322. netif_dbg(channel->efx, ifup, channel->efx->net_dev,
  323. "chan %d start event queue\n", channel->channel);
  324. /* Make sure the NAPI handler sees the enabled flag set */
  325. channel->enabled = true;
  326. smp_wmb();
  327. efx_channel_enable(channel);
  328. napi_enable(&channel->napi_str);
  329. efx_nic_eventq_read_ack(channel);
  330. }
  331. /* Disable event queue processing and NAPI */
  332. void efx_stop_eventq(struct efx_channel *channel)
  333. {
  334. if (!channel->enabled)
  335. return;
  336. napi_disable(&channel->napi_str);
  337. while (!efx_channel_disable(channel))
  338. usleep_range(1000, 20000);
  339. channel->enabled = false;
  340. }
  341. static void efx_fini_eventq(struct efx_channel *channel)
  342. {
  343. if (!channel->eventq_init)
  344. return;
  345. netif_dbg(channel->efx, drv, channel->efx->net_dev,
  346. "chan %d fini event queue\n", channel->channel);
  347. efx_nic_fini_eventq(channel);
  348. channel->eventq_init = false;
  349. }
  350. static void efx_remove_eventq(struct efx_channel *channel)
  351. {
  352. netif_dbg(channel->efx, drv, channel->efx->net_dev,
  353. "chan %d remove event queue\n", channel->channel);
  354. efx_nic_remove_eventq(channel);
  355. }
  356. /**************************************************************************
  357. *
  358. * Channel handling
  359. *
  360. *************************************************************************/
  361. /* Allocate and initialise a channel structure. */
  362. static struct efx_channel *
  363. efx_alloc_channel(struct efx_nic *efx, int i, struct efx_channel *old_channel)
  364. {
  365. struct efx_channel *channel;
  366. struct efx_rx_queue *rx_queue;
  367. struct efx_tx_queue *tx_queue;
  368. int j;
  369. channel = kzalloc(sizeof(*channel), GFP_KERNEL);
  370. if (!channel)
  371. return NULL;
  372. channel->efx = efx;
  373. channel->channel = i;
  374. channel->type = &efx_default_channel_type;
  375. for (j = 0; j < EFX_TXQ_TYPES; j++) {
  376. tx_queue = &channel->tx_queue[j];
  377. tx_queue->efx = efx;
  378. tx_queue->queue = i * EFX_TXQ_TYPES + j;
  379. tx_queue->channel = channel;
  380. }
  381. rx_queue = &channel->rx_queue;
  382. rx_queue->efx = efx;
  383. setup_timer(&rx_queue->slow_fill, efx_rx_slow_fill,
  384. (unsigned long)rx_queue);
  385. return channel;
  386. }
  387. /* Allocate and initialise a channel structure, copying parameters
  388. * (but not resources) from an old channel structure.
  389. */
  390. static struct efx_channel *
  391. efx_copy_channel(const struct efx_channel *old_channel)
  392. {
  393. struct efx_channel *channel;
  394. struct efx_rx_queue *rx_queue;
  395. struct efx_tx_queue *tx_queue;
  396. int j;
  397. channel = kmalloc(sizeof(*channel), GFP_KERNEL);
  398. if (!channel)
  399. return NULL;
  400. *channel = *old_channel;
  401. channel->napi_dev = NULL;
  402. memset(&channel->eventq, 0, sizeof(channel->eventq));
  403. for (j = 0; j < EFX_TXQ_TYPES; j++) {
  404. tx_queue = &channel->tx_queue[j];
  405. if (tx_queue->channel)
  406. tx_queue->channel = channel;
  407. tx_queue->buffer = NULL;
  408. memset(&tx_queue->txd, 0, sizeof(tx_queue->txd));
  409. }
  410. rx_queue = &channel->rx_queue;
  411. rx_queue->buffer = NULL;
  412. memset(&rx_queue->rxd, 0, sizeof(rx_queue->rxd));
  413. setup_timer(&rx_queue->slow_fill, efx_rx_slow_fill,
  414. (unsigned long)rx_queue);
  415. return channel;
  416. }
  417. static int efx_probe_channel(struct efx_channel *channel)
  418. {
  419. struct efx_tx_queue *tx_queue;
  420. struct efx_rx_queue *rx_queue;
  421. int rc;
  422. netif_dbg(channel->efx, probe, channel->efx->net_dev,
  423. "creating channel %d\n", channel->channel);
  424. rc = channel->type->pre_probe(channel);
  425. if (rc)
  426. goto fail;
  427. rc = efx_probe_eventq(channel);
  428. if (rc)
  429. goto fail;
  430. efx_for_each_channel_tx_queue(tx_queue, channel) {
  431. rc = efx_probe_tx_queue(tx_queue);
  432. if (rc)
  433. goto fail;
  434. }
  435. efx_for_each_channel_rx_queue(rx_queue, channel) {
  436. rc = efx_probe_rx_queue(rx_queue);
  437. if (rc)
  438. goto fail;
  439. }
  440. return 0;
  441. fail:
  442. efx_remove_channel(channel);
  443. return rc;
  444. }
  445. static void
  446. efx_get_channel_name(struct efx_channel *channel, char *buf, size_t len)
  447. {
  448. struct efx_nic *efx = channel->efx;
  449. const char *type;
  450. int number;
  451. number = channel->channel;
  452. if (efx->tx_channel_offset == 0) {
  453. type = "";
  454. } else if (channel->channel < efx->tx_channel_offset) {
  455. type = "-rx";
  456. } else {
  457. type = "-tx";
  458. number -= efx->tx_channel_offset;
  459. }
  460. snprintf(buf, len, "%s%s-%d", efx->name, type, number);
  461. }
  462. static void efx_set_channel_names(struct efx_nic *efx)
  463. {
  464. struct efx_channel *channel;
  465. efx_for_each_channel(channel, efx)
  466. channel->type->get_name(channel,
  467. efx->msi_context[channel->channel].name,
  468. sizeof(efx->msi_context[0].name));
  469. }
  470. static int efx_probe_channels(struct efx_nic *efx)
  471. {
  472. struct efx_channel *channel;
  473. int rc;
  474. /* Restart special buffer allocation */
  475. efx->next_buffer_table = 0;
  476. /* Probe channels in reverse, so that any 'extra' channels
  477. * use the start of the buffer table. This allows the traffic
  478. * channels to be resized without moving them or wasting the
  479. * entries before them.
  480. */
  481. efx_for_each_channel_rev(channel, efx) {
  482. rc = efx_probe_channel(channel);
  483. if (rc) {
  484. netif_err(efx, probe, efx->net_dev,
  485. "failed to create channel %d\n",
  486. channel->channel);
  487. goto fail;
  488. }
  489. }
  490. efx_set_channel_names(efx);
  491. return 0;
  492. fail:
  493. efx_remove_channels(efx);
  494. return rc;
  495. }
  496. /* Channels are shutdown and reinitialised whilst the NIC is running
  497. * to propagate configuration changes (mtu, checksum offload), or
  498. * to clear hardware error conditions
  499. */
  500. static void efx_start_datapath(struct efx_nic *efx)
  501. {
  502. bool old_rx_scatter = efx->rx_scatter;
  503. struct efx_tx_queue *tx_queue;
  504. struct efx_rx_queue *rx_queue;
  505. struct efx_channel *channel;
  506. size_t rx_buf_len;
  507. /* Calculate the rx buffer allocation parameters required to
  508. * support the current MTU, including padding for header
  509. * alignment and overruns.
  510. */
  511. efx->rx_dma_len = (efx->rx_prefix_size +
  512. EFX_MAX_FRAME_LEN(efx->net_dev->mtu) +
  513. efx->type->rx_buffer_padding);
  514. rx_buf_len = (sizeof(struct efx_rx_page_state) +
  515. efx->rx_ip_align + efx->rx_dma_len);
  516. if (rx_buf_len <= PAGE_SIZE) {
  517. efx->rx_scatter = efx->type->always_rx_scatter;
  518. efx->rx_buffer_order = 0;
  519. } else if (efx->type->can_rx_scatter) {
  520. BUILD_BUG_ON(EFX_RX_USR_BUF_SIZE % L1_CACHE_BYTES);
  521. BUILD_BUG_ON(sizeof(struct efx_rx_page_state) +
  522. 2 * ALIGN(NET_IP_ALIGN + EFX_RX_USR_BUF_SIZE,
  523. EFX_RX_BUF_ALIGNMENT) >
  524. PAGE_SIZE);
  525. efx->rx_scatter = true;
  526. efx->rx_dma_len = EFX_RX_USR_BUF_SIZE;
  527. efx->rx_buffer_order = 0;
  528. } else {
  529. efx->rx_scatter = false;
  530. efx->rx_buffer_order = get_order(rx_buf_len);
  531. }
  532. efx_rx_config_page_split(efx);
  533. if (efx->rx_buffer_order)
  534. netif_dbg(efx, drv, efx->net_dev,
  535. "RX buf len=%u; page order=%u batch=%u\n",
  536. efx->rx_dma_len, efx->rx_buffer_order,
  537. efx->rx_pages_per_batch);
  538. else
  539. netif_dbg(efx, drv, efx->net_dev,
  540. "RX buf len=%u step=%u bpp=%u; page batch=%u\n",
  541. efx->rx_dma_len, efx->rx_page_buf_step,
  542. efx->rx_bufs_per_page, efx->rx_pages_per_batch);
  543. /* RX filters may also have scatter-enabled flags */
  544. if (efx->rx_scatter != old_rx_scatter)
  545. efx->type->filter_update_rx_scatter(efx);
  546. /* We must keep at least one descriptor in a TX ring empty.
  547. * We could avoid this when the queue size does not exactly
  548. * match the hardware ring size, but it's not that important.
  549. * Therefore we stop the queue when one more skb might fill
  550. * the ring completely. We wake it when half way back to
  551. * empty.
  552. */
  553. efx->txq_stop_thresh = efx->txq_entries - efx_tx_max_skb_descs(efx);
  554. efx->txq_wake_thresh = efx->txq_stop_thresh / 2;
  555. /* Initialise the channels */
  556. efx_for_each_channel(channel, efx) {
  557. efx_for_each_channel_tx_queue(tx_queue, channel) {
  558. efx_init_tx_queue(tx_queue);
  559. atomic_inc(&efx->active_queues);
  560. }
  561. efx_for_each_channel_rx_queue(rx_queue, channel) {
  562. efx_init_rx_queue(rx_queue);
  563. atomic_inc(&efx->active_queues);
  564. efx_stop_eventq(channel);
  565. efx_fast_push_rx_descriptors(rx_queue, false);
  566. efx_start_eventq(channel);
  567. }
  568. WARN_ON(channel->rx_pkt_n_frags);
  569. }
  570. efx_ptp_start_datapath(efx);
  571. if (netif_device_present(efx->net_dev))
  572. netif_tx_wake_all_queues(efx->net_dev);
  573. }
  574. static void efx_stop_datapath(struct efx_nic *efx)
  575. {
  576. struct efx_channel *channel;
  577. struct efx_tx_queue *tx_queue;
  578. struct efx_rx_queue *rx_queue;
  579. int rc;
  580. EFX_ASSERT_RESET_SERIALISED(efx);
  581. BUG_ON(efx->port_enabled);
  582. efx_ptp_stop_datapath(efx);
  583. /* Stop RX refill */
  584. efx_for_each_channel(channel, efx) {
  585. efx_for_each_channel_rx_queue(rx_queue, channel)
  586. rx_queue->refill_enabled = false;
  587. }
  588. efx_for_each_channel(channel, efx) {
  589. /* RX packet processing is pipelined, so wait for the
  590. * NAPI handler to complete. At least event queue 0
  591. * might be kept active by non-data events, so don't
  592. * use napi_synchronize() but actually disable NAPI
  593. * temporarily.
  594. */
  595. if (efx_channel_has_rx_queue(channel)) {
  596. efx_stop_eventq(channel);
  597. efx_start_eventq(channel);
  598. }
  599. }
  600. rc = efx->type->fini_dmaq(efx);
  601. if (rc && EFX_WORKAROUND_7803(efx)) {
  602. /* Schedule a reset to recover from the flush failure. The
  603. * descriptor caches reference memory we're about to free,
  604. * but falcon_reconfigure_mac_wrapper() won't reconnect
  605. * the MACs because of the pending reset.
  606. */
  607. netif_err(efx, drv, efx->net_dev,
  608. "Resetting to recover from flush failure\n");
  609. efx_schedule_reset(efx, RESET_TYPE_ALL);
  610. } else if (rc) {
  611. netif_err(efx, drv, efx->net_dev, "failed to flush queues\n");
  612. } else {
  613. netif_dbg(efx, drv, efx->net_dev,
  614. "successfully flushed all queues\n");
  615. }
  616. efx_for_each_channel(channel, efx) {
  617. efx_for_each_channel_rx_queue(rx_queue, channel)
  618. efx_fini_rx_queue(rx_queue);
  619. efx_for_each_possible_channel_tx_queue(tx_queue, channel)
  620. efx_fini_tx_queue(tx_queue);
  621. }
  622. }
  623. static void efx_remove_channel(struct efx_channel *channel)
  624. {
  625. struct efx_tx_queue *tx_queue;
  626. struct efx_rx_queue *rx_queue;
  627. netif_dbg(channel->efx, drv, channel->efx->net_dev,
  628. "destroy chan %d\n", channel->channel);
  629. efx_for_each_channel_rx_queue(rx_queue, channel)
  630. efx_remove_rx_queue(rx_queue);
  631. efx_for_each_possible_channel_tx_queue(tx_queue, channel)
  632. efx_remove_tx_queue(tx_queue);
  633. efx_remove_eventq(channel);
  634. channel->type->post_remove(channel);
  635. }
  636. static void efx_remove_channels(struct efx_nic *efx)
  637. {
  638. struct efx_channel *channel;
  639. efx_for_each_channel(channel, efx)
  640. efx_remove_channel(channel);
  641. }
  642. int
  643. efx_realloc_channels(struct efx_nic *efx, u32 rxq_entries, u32 txq_entries)
  644. {
  645. struct efx_channel *other_channel[EFX_MAX_CHANNELS], *channel;
  646. u32 old_rxq_entries, old_txq_entries;
  647. unsigned i, next_buffer_table = 0;
  648. int rc, rc2;
  649. rc = efx_check_disabled(efx);
  650. if (rc)
  651. return rc;
  652. /* Not all channels should be reallocated. We must avoid
  653. * reallocating their buffer table entries.
  654. */
  655. efx_for_each_channel(channel, efx) {
  656. struct efx_rx_queue *rx_queue;
  657. struct efx_tx_queue *tx_queue;
  658. if (channel->type->copy)
  659. continue;
  660. next_buffer_table = max(next_buffer_table,
  661. channel->eventq.index +
  662. channel->eventq.entries);
  663. efx_for_each_channel_rx_queue(rx_queue, channel)
  664. next_buffer_table = max(next_buffer_table,
  665. rx_queue->rxd.index +
  666. rx_queue->rxd.entries);
  667. efx_for_each_channel_tx_queue(tx_queue, channel)
  668. next_buffer_table = max(next_buffer_table,
  669. tx_queue->txd.index +
  670. tx_queue->txd.entries);
  671. }
  672. efx_device_detach_sync(efx);
  673. efx_stop_all(efx);
  674. efx_soft_disable_interrupts(efx);
  675. /* Clone channels (where possible) */
  676. memset(other_channel, 0, sizeof(other_channel));
  677. for (i = 0; i < efx->n_channels; i++) {
  678. channel = efx->channel[i];
  679. if (channel->type->copy)
  680. channel = channel->type->copy(channel);
  681. if (!channel) {
  682. rc = -ENOMEM;
  683. goto out;
  684. }
  685. other_channel[i] = channel;
  686. }
  687. /* Swap entry counts and channel pointers */
  688. old_rxq_entries = efx->rxq_entries;
  689. old_txq_entries = efx->txq_entries;
  690. efx->rxq_entries = rxq_entries;
  691. efx->txq_entries = txq_entries;
  692. for (i = 0; i < efx->n_channels; i++) {
  693. channel = efx->channel[i];
  694. efx->channel[i] = other_channel[i];
  695. other_channel[i] = channel;
  696. }
  697. /* Restart buffer table allocation */
  698. efx->next_buffer_table = next_buffer_table;
  699. for (i = 0; i < efx->n_channels; i++) {
  700. channel = efx->channel[i];
  701. if (!channel->type->copy)
  702. continue;
  703. rc = efx_probe_channel(channel);
  704. if (rc)
  705. goto rollback;
  706. efx_init_napi_channel(efx->channel[i]);
  707. }
  708. out:
  709. /* Destroy unused channel structures */
  710. for (i = 0; i < efx->n_channels; i++) {
  711. channel = other_channel[i];
  712. if (channel && channel->type->copy) {
  713. efx_fini_napi_channel(channel);
  714. efx_remove_channel(channel);
  715. kfree(channel);
  716. }
  717. }
  718. rc2 = efx_soft_enable_interrupts(efx);
  719. if (rc2) {
  720. rc = rc ? rc : rc2;
  721. netif_err(efx, drv, efx->net_dev,
  722. "unable to restart interrupts on channel reallocation\n");
  723. efx_schedule_reset(efx, RESET_TYPE_DISABLE);
  724. } else {
  725. efx_start_all(efx);
  726. netif_device_attach(efx->net_dev);
  727. }
  728. return rc;
  729. rollback:
  730. /* Swap back */
  731. efx->rxq_entries = old_rxq_entries;
  732. efx->txq_entries = old_txq_entries;
  733. for (i = 0; i < efx->n_channels; i++) {
  734. channel = efx->channel[i];
  735. efx->channel[i] = other_channel[i];
  736. other_channel[i] = channel;
  737. }
  738. goto out;
  739. }
  740. void efx_schedule_slow_fill(struct efx_rx_queue *rx_queue)
  741. {
  742. mod_timer(&rx_queue->slow_fill, jiffies + msecs_to_jiffies(100));
  743. }
  744. static const struct efx_channel_type efx_default_channel_type = {
  745. .pre_probe = efx_channel_dummy_op_int,
  746. .post_remove = efx_channel_dummy_op_void,
  747. .get_name = efx_get_channel_name,
  748. .copy = efx_copy_channel,
  749. .keep_eventq = false,
  750. };
  751. int efx_channel_dummy_op_int(struct efx_channel *channel)
  752. {
  753. return 0;
  754. }
  755. void efx_channel_dummy_op_void(struct efx_channel *channel)
  756. {
  757. }
  758. /**************************************************************************
  759. *
  760. * Port handling
  761. *
  762. **************************************************************************/
  763. /* This ensures that the kernel is kept informed (via
  764. * netif_carrier_on/off) of the link status, and also maintains the
  765. * link status's stop on the port's TX queue.
  766. */
  767. void efx_link_status_changed(struct efx_nic *efx)
  768. {
  769. struct efx_link_state *link_state = &efx->link_state;
  770. /* SFC Bug 5356: A net_dev notifier is registered, so we must ensure
  771. * that no events are triggered between unregister_netdev() and the
  772. * driver unloading. A more general condition is that NETDEV_CHANGE
  773. * can only be generated between NETDEV_UP and NETDEV_DOWN */
  774. if (!netif_running(efx->net_dev))
  775. return;
  776. if (link_state->up != netif_carrier_ok(efx->net_dev)) {
  777. efx->n_link_state_changes++;
  778. if (link_state->up)
  779. netif_carrier_on(efx->net_dev);
  780. else
  781. netif_carrier_off(efx->net_dev);
  782. }
  783. /* Status message for kernel log */
  784. if (link_state->up)
  785. netif_info(efx, link, efx->net_dev,
  786. "link up at %uMbps %s-duplex (MTU %d)\n",
  787. link_state->speed, link_state->fd ? "full" : "half",
  788. efx->net_dev->mtu);
  789. else
  790. netif_info(efx, link, efx->net_dev, "link down\n");
  791. }
  792. void efx_link_set_advertising(struct efx_nic *efx, u32 advertising)
  793. {
  794. efx->link_advertising = advertising;
  795. if (advertising) {
  796. if (advertising & ADVERTISED_Pause)
  797. efx->wanted_fc |= (EFX_FC_TX | EFX_FC_RX);
  798. else
  799. efx->wanted_fc &= ~(EFX_FC_TX | EFX_FC_RX);
  800. if (advertising & ADVERTISED_Asym_Pause)
  801. efx->wanted_fc ^= EFX_FC_TX;
  802. }
  803. }
  804. void efx_link_set_wanted_fc(struct efx_nic *efx, u8 wanted_fc)
  805. {
  806. efx->wanted_fc = wanted_fc;
  807. if (efx->link_advertising) {
  808. if (wanted_fc & EFX_FC_RX)
  809. efx->link_advertising |= (ADVERTISED_Pause |
  810. ADVERTISED_Asym_Pause);
  811. else
  812. efx->link_advertising &= ~(ADVERTISED_Pause |
  813. ADVERTISED_Asym_Pause);
  814. if (wanted_fc & EFX_FC_TX)
  815. efx->link_advertising ^= ADVERTISED_Asym_Pause;
  816. }
  817. }
  818. static void efx_fini_port(struct efx_nic *efx);
  819. /* We assume that efx->type->reconfigure_mac will always try to sync RX
  820. * filters and therefore needs to read-lock the filter table against freeing
  821. */
  822. void efx_mac_reconfigure(struct efx_nic *efx)
  823. {
  824. down_read(&efx->filter_sem);
  825. efx->type->reconfigure_mac(efx);
  826. up_read(&efx->filter_sem);
  827. }
  828. /* Push loopback/power/transmit disable settings to the PHY, and reconfigure
  829. * the MAC appropriately. All other PHY configuration changes are pushed
  830. * through phy_op->set_settings(), and pushed asynchronously to the MAC
  831. * through efx_monitor().
  832. *
  833. * Callers must hold the mac_lock
  834. */
  835. int __efx_reconfigure_port(struct efx_nic *efx)
  836. {
  837. enum efx_phy_mode phy_mode;
  838. int rc;
  839. WARN_ON(!mutex_is_locked(&efx->mac_lock));
  840. /* Disable PHY transmit in mac level loopbacks */
  841. phy_mode = efx->phy_mode;
  842. if (LOOPBACK_INTERNAL(efx))
  843. efx->phy_mode |= PHY_MODE_TX_DISABLED;
  844. else
  845. efx->phy_mode &= ~PHY_MODE_TX_DISABLED;
  846. rc = efx->type->reconfigure_port(efx);
  847. if (rc)
  848. efx->phy_mode = phy_mode;
  849. return rc;
  850. }
  851. /* Reinitialise the MAC to pick up new PHY settings, even if the port is
  852. * disabled. */
  853. int efx_reconfigure_port(struct efx_nic *efx)
  854. {
  855. int rc;
  856. EFX_ASSERT_RESET_SERIALISED(efx);
  857. mutex_lock(&efx->mac_lock);
  858. rc = __efx_reconfigure_port(efx);
  859. mutex_unlock(&efx->mac_lock);
  860. return rc;
  861. }
  862. /* Asynchronous work item for changing MAC promiscuity and multicast
  863. * hash. Avoid a drain/rx_ingress enable by reconfiguring the current
  864. * MAC directly. */
  865. static void efx_mac_work(struct work_struct *data)
  866. {
  867. struct efx_nic *efx = container_of(data, struct efx_nic, mac_work);
  868. mutex_lock(&efx->mac_lock);
  869. if (efx->port_enabled)
  870. efx_mac_reconfigure(efx);
  871. mutex_unlock(&efx->mac_lock);
  872. }
  873. static int efx_probe_port(struct efx_nic *efx)
  874. {
  875. int rc;
  876. netif_dbg(efx, probe, efx->net_dev, "create port\n");
  877. if (phy_flash_cfg)
  878. efx->phy_mode = PHY_MODE_SPECIAL;
  879. /* Connect up MAC/PHY operations table */
  880. rc = efx->type->probe_port(efx);
  881. if (rc)
  882. return rc;
  883. /* Initialise MAC address to permanent address */
  884. ether_addr_copy(efx->net_dev->dev_addr, efx->net_dev->perm_addr);
  885. return 0;
  886. }
  887. static int efx_init_port(struct efx_nic *efx)
  888. {
  889. int rc;
  890. netif_dbg(efx, drv, efx->net_dev, "init port\n");
  891. mutex_lock(&efx->mac_lock);
  892. rc = efx->phy_op->init(efx);
  893. if (rc)
  894. goto fail1;
  895. efx->port_initialized = true;
  896. /* Reconfigure the MAC before creating dma queues (required for
  897. * Falcon/A1 where RX_INGR_EN/TX_DRAIN_EN isn't supported) */
  898. efx_mac_reconfigure(efx);
  899. /* Ensure the PHY advertises the correct flow control settings */
  900. rc = efx->phy_op->reconfigure(efx);
  901. if (rc && rc != -EPERM)
  902. goto fail2;
  903. mutex_unlock(&efx->mac_lock);
  904. return 0;
  905. fail2:
  906. efx->phy_op->fini(efx);
  907. fail1:
  908. mutex_unlock(&efx->mac_lock);
  909. return rc;
  910. }
  911. static void efx_start_port(struct efx_nic *efx)
  912. {
  913. netif_dbg(efx, ifup, efx->net_dev, "start port\n");
  914. BUG_ON(efx->port_enabled);
  915. mutex_lock(&efx->mac_lock);
  916. efx->port_enabled = true;
  917. /* Ensure MAC ingress/egress is enabled */
  918. efx_mac_reconfigure(efx);
  919. mutex_unlock(&efx->mac_lock);
  920. }
  921. /* Cancel work for MAC reconfiguration, periodic hardware monitoring
  922. * and the async self-test, wait for them to finish and prevent them
  923. * being scheduled again. This doesn't cover online resets, which
  924. * should only be cancelled when removing the device.
  925. */
  926. static void efx_stop_port(struct efx_nic *efx)
  927. {
  928. netif_dbg(efx, ifdown, efx->net_dev, "stop port\n");
  929. EFX_ASSERT_RESET_SERIALISED(efx);
  930. mutex_lock(&efx->mac_lock);
  931. efx->port_enabled = false;
  932. mutex_unlock(&efx->mac_lock);
  933. /* Serialise against efx_set_multicast_list() */
  934. netif_addr_lock_bh(efx->net_dev);
  935. netif_addr_unlock_bh(efx->net_dev);
  936. cancel_delayed_work_sync(&efx->monitor_work);
  937. efx_selftest_async_cancel(efx);
  938. cancel_work_sync(&efx->mac_work);
  939. }
  940. static void efx_fini_port(struct efx_nic *efx)
  941. {
  942. netif_dbg(efx, drv, efx->net_dev, "shut down port\n");
  943. if (!efx->port_initialized)
  944. return;
  945. efx->phy_op->fini(efx);
  946. efx->port_initialized = false;
  947. efx->link_state.up = false;
  948. efx_link_status_changed(efx);
  949. }
  950. static void efx_remove_port(struct efx_nic *efx)
  951. {
  952. netif_dbg(efx, drv, efx->net_dev, "destroying port\n");
  953. efx->type->remove_port(efx);
  954. }
  955. /**************************************************************************
  956. *
  957. * NIC handling
  958. *
  959. **************************************************************************/
  960. static LIST_HEAD(efx_primary_list);
  961. static LIST_HEAD(efx_unassociated_list);
  962. static bool efx_same_controller(struct efx_nic *left, struct efx_nic *right)
  963. {
  964. return left->type == right->type &&
  965. left->vpd_sn && right->vpd_sn &&
  966. !strcmp(left->vpd_sn, right->vpd_sn);
  967. }
  968. static void efx_associate(struct efx_nic *efx)
  969. {
  970. struct efx_nic *other, *next;
  971. if (efx->primary == efx) {
  972. /* Adding primary function; look for secondaries */
  973. netif_dbg(efx, probe, efx->net_dev, "adding to primary list\n");
  974. list_add_tail(&efx->node, &efx_primary_list);
  975. list_for_each_entry_safe(other, next, &efx_unassociated_list,
  976. node) {
  977. if (efx_same_controller(efx, other)) {
  978. list_del(&other->node);
  979. netif_dbg(other, probe, other->net_dev,
  980. "moving to secondary list of %s %s\n",
  981. pci_name(efx->pci_dev),
  982. efx->net_dev->name);
  983. list_add_tail(&other->node,
  984. &efx->secondary_list);
  985. other->primary = efx;
  986. }
  987. }
  988. } else {
  989. /* Adding secondary function; look for primary */
  990. list_for_each_entry(other, &efx_primary_list, node) {
  991. if (efx_same_controller(efx, other)) {
  992. netif_dbg(efx, probe, efx->net_dev,
  993. "adding to secondary list of %s %s\n",
  994. pci_name(other->pci_dev),
  995. other->net_dev->name);
  996. list_add_tail(&efx->node,
  997. &other->secondary_list);
  998. efx->primary = other;
  999. return;
  1000. }
  1001. }
  1002. netif_dbg(efx, probe, efx->net_dev,
  1003. "adding to unassociated list\n");
  1004. list_add_tail(&efx->node, &efx_unassociated_list);
  1005. }
  1006. }
  1007. static void efx_dissociate(struct efx_nic *efx)
  1008. {
  1009. struct efx_nic *other, *next;
  1010. list_del(&efx->node);
  1011. efx->primary = NULL;
  1012. list_for_each_entry_safe(other, next, &efx->secondary_list, node) {
  1013. list_del(&other->node);
  1014. netif_dbg(other, probe, other->net_dev,
  1015. "moving to unassociated list\n");
  1016. list_add_tail(&other->node, &efx_unassociated_list);
  1017. other->primary = NULL;
  1018. }
  1019. }
  1020. /* This configures the PCI device to enable I/O and DMA. */
  1021. static int efx_init_io(struct efx_nic *efx)
  1022. {
  1023. struct pci_dev *pci_dev = efx->pci_dev;
  1024. dma_addr_t dma_mask = efx->type->max_dma_mask;
  1025. unsigned int mem_map_size = efx->type->mem_map_size(efx);
  1026. int rc, bar;
  1027. netif_dbg(efx, probe, efx->net_dev, "initialising I/O\n");
  1028. bar = efx->type->mem_bar;
  1029. rc = pci_enable_device(pci_dev);
  1030. if (rc) {
  1031. netif_err(efx, probe, efx->net_dev,
  1032. "failed to enable PCI device\n");
  1033. goto fail1;
  1034. }
  1035. pci_set_master(pci_dev);
  1036. /* Set the PCI DMA mask. Try all possibilities from our
  1037. * genuine mask down to 32 bits, because some architectures
  1038. * (e.g. x86_64 with iommu_sac_force set) will allow 40 bit
  1039. * masks event though they reject 46 bit masks.
  1040. */
  1041. while (dma_mask > 0x7fffffffUL) {
  1042. if (dma_supported(&pci_dev->dev, dma_mask)) {
  1043. rc = dma_set_mask_and_coherent(&pci_dev->dev, dma_mask);
  1044. if (rc == 0)
  1045. break;
  1046. }
  1047. dma_mask >>= 1;
  1048. }
  1049. if (rc) {
  1050. netif_err(efx, probe, efx->net_dev,
  1051. "could not find a suitable DMA mask\n");
  1052. goto fail2;
  1053. }
  1054. netif_dbg(efx, probe, efx->net_dev,
  1055. "using DMA mask %llx\n", (unsigned long long) dma_mask);
  1056. efx->membase_phys = pci_resource_start(efx->pci_dev, bar);
  1057. rc = pci_request_region(pci_dev, bar, "sfc");
  1058. if (rc) {
  1059. netif_err(efx, probe, efx->net_dev,
  1060. "request for memory BAR failed\n");
  1061. rc = -EIO;
  1062. goto fail3;
  1063. }
  1064. efx->membase = ioremap_nocache(efx->membase_phys, mem_map_size);
  1065. if (!efx->membase) {
  1066. netif_err(efx, probe, efx->net_dev,
  1067. "could not map memory BAR at %llx+%x\n",
  1068. (unsigned long long)efx->membase_phys, mem_map_size);
  1069. rc = -ENOMEM;
  1070. goto fail4;
  1071. }
  1072. netif_dbg(efx, probe, efx->net_dev,
  1073. "memory BAR at %llx+%x (virtual %p)\n",
  1074. (unsigned long long)efx->membase_phys, mem_map_size,
  1075. efx->membase);
  1076. return 0;
  1077. fail4:
  1078. pci_release_region(efx->pci_dev, bar);
  1079. fail3:
  1080. efx->membase_phys = 0;
  1081. fail2:
  1082. pci_disable_device(efx->pci_dev);
  1083. fail1:
  1084. return rc;
  1085. }
  1086. static void efx_fini_io(struct efx_nic *efx)
  1087. {
  1088. int bar;
  1089. netif_dbg(efx, drv, efx->net_dev, "shutting down I/O\n");
  1090. if (efx->membase) {
  1091. iounmap(efx->membase);
  1092. efx->membase = NULL;
  1093. }
  1094. if (efx->membase_phys) {
  1095. bar = efx->type->mem_bar;
  1096. pci_release_region(efx->pci_dev, bar);
  1097. efx->membase_phys = 0;
  1098. }
  1099. /* Don't disable bus-mastering if VFs are assigned */
  1100. if (!pci_vfs_assigned(efx->pci_dev))
  1101. pci_disable_device(efx->pci_dev);
  1102. }
  1103. void efx_set_default_rx_indir_table(struct efx_nic *efx)
  1104. {
  1105. size_t i;
  1106. for (i = 0; i < ARRAY_SIZE(efx->rx_indir_table); i++)
  1107. efx->rx_indir_table[i] =
  1108. ethtool_rxfh_indir_default(i, efx->rss_spread);
  1109. }
  1110. static unsigned int efx_wanted_parallelism(struct efx_nic *efx)
  1111. {
  1112. cpumask_var_t thread_mask;
  1113. unsigned int count;
  1114. int cpu;
  1115. if (rss_cpus) {
  1116. count = rss_cpus;
  1117. } else {
  1118. if (unlikely(!zalloc_cpumask_var(&thread_mask, GFP_KERNEL))) {
  1119. netif_warn(efx, probe, efx->net_dev,
  1120. "RSS disabled due to allocation failure\n");
  1121. return 1;
  1122. }
  1123. count = 0;
  1124. for_each_online_cpu(cpu) {
  1125. if (!cpumask_test_cpu(cpu, thread_mask)) {
  1126. ++count;
  1127. cpumask_or(thread_mask, thread_mask,
  1128. topology_sibling_cpumask(cpu));
  1129. }
  1130. }
  1131. free_cpumask_var(thread_mask);
  1132. }
  1133. /* If RSS is requested for the PF *and* VFs then we can't write RSS
  1134. * table entries that are inaccessible to VFs
  1135. */
  1136. #ifdef CONFIG_SFC_SRIOV
  1137. if (efx->type->sriov_wanted) {
  1138. if (efx->type->sriov_wanted(efx) && efx_vf_size(efx) > 1 &&
  1139. count > efx_vf_size(efx)) {
  1140. netif_warn(efx, probe, efx->net_dev,
  1141. "Reducing number of RSS channels from %u to %u for "
  1142. "VF support. Increase vf-msix-limit to use more "
  1143. "channels on the PF.\n",
  1144. count, efx_vf_size(efx));
  1145. count = efx_vf_size(efx);
  1146. }
  1147. }
  1148. #endif
  1149. return count;
  1150. }
  1151. /* Probe the number and type of interrupts we are able to obtain, and
  1152. * the resulting numbers of channels and RX queues.
  1153. */
  1154. static int efx_probe_interrupts(struct efx_nic *efx)
  1155. {
  1156. unsigned int extra_channels = 0;
  1157. unsigned int i, j;
  1158. int rc;
  1159. for (i = 0; i < EFX_MAX_EXTRA_CHANNELS; i++)
  1160. if (efx->extra_channel_type[i])
  1161. ++extra_channels;
  1162. if (efx->interrupt_mode == EFX_INT_MODE_MSIX) {
  1163. struct msix_entry xentries[EFX_MAX_CHANNELS];
  1164. unsigned int n_channels;
  1165. n_channels = efx_wanted_parallelism(efx);
  1166. if (separate_tx_channels)
  1167. n_channels *= 2;
  1168. n_channels += extra_channels;
  1169. n_channels = min(n_channels, efx->max_channels);
  1170. for (i = 0; i < n_channels; i++)
  1171. xentries[i].entry = i;
  1172. rc = pci_enable_msix_range(efx->pci_dev,
  1173. xentries, 1, n_channels);
  1174. if (rc < 0) {
  1175. /* Fall back to single channel MSI */
  1176. efx->interrupt_mode = EFX_INT_MODE_MSI;
  1177. netif_err(efx, drv, efx->net_dev,
  1178. "could not enable MSI-X\n");
  1179. } else if (rc < n_channels) {
  1180. netif_err(efx, drv, efx->net_dev,
  1181. "WARNING: Insufficient MSI-X vectors"
  1182. " available (%d < %u).\n", rc, n_channels);
  1183. netif_err(efx, drv, efx->net_dev,
  1184. "WARNING: Performance may be reduced.\n");
  1185. n_channels = rc;
  1186. }
  1187. if (rc > 0) {
  1188. efx->n_channels = n_channels;
  1189. if (n_channels > extra_channels)
  1190. n_channels -= extra_channels;
  1191. if (separate_tx_channels) {
  1192. efx->n_tx_channels = max(n_channels / 2, 1U);
  1193. efx->n_rx_channels = max(n_channels -
  1194. efx->n_tx_channels,
  1195. 1U);
  1196. } else {
  1197. efx->n_tx_channels = n_channels;
  1198. efx->n_rx_channels = n_channels;
  1199. }
  1200. for (i = 0; i < efx->n_channels; i++)
  1201. efx_get_channel(efx, i)->irq =
  1202. xentries[i].vector;
  1203. }
  1204. }
  1205. /* Try single interrupt MSI */
  1206. if (efx->interrupt_mode == EFX_INT_MODE_MSI) {
  1207. efx->n_channels = 1;
  1208. efx->n_rx_channels = 1;
  1209. efx->n_tx_channels = 1;
  1210. rc = pci_enable_msi(efx->pci_dev);
  1211. if (rc == 0) {
  1212. efx_get_channel(efx, 0)->irq = efx->pci_dev->irq;
  1213. } else {
  1214. netif_err(efx, drv, efx->net_dev,
  1215. "could not enable MSI\n");
  1216. efx->interrupt_mode = EFX_INT_MODE_LEGACY;
  1217. }
  1218. }
  1219. /* Assume legacy interrupts */
  1220. if (efx->interrupt_mode == EFX_INT_MODE_LEGACY) {
  1221. efx->n_channels = 1 + (separate_tx_channels ? 1 : 0);
  1222. efx->n_rx_channels = 1;
  1223. efx->n_tx_channels = 1;
  1224. efx->legacy_irq = efx->pci_dev->irq;
  1225. }
  1226. /* Assign extra channels if possible */
  1227. j = efx->n_channels;
  1228. for (i = 0; i < EFX_MAX_EXTRA_CHANNELS; i++) {
  1229. if (!efx->extra_channel_type[i])
  1230. continue;
  1231. if (efx->interrupt_mode != EFX_INT_MODE_MSIX ||
  1232. efx->n_channels <= extra_channels) {
  1233. efx->extra_channel_type[i]->handle_no_channel(efx);
  1234. } else {
  1235. --j;
  1236. efx_get_channel(efx, j)->type =
  1237. efx->extra_channel_type[i];
  1238. }
  1239. }
  1240. /* RSS might be usable on VFs even if it is disabled on the PF */
  1241. #ifdef CONFIG_SFC_SRIOV
  1242. if (efx->type->sriov_wanted) {
  1243. efx->rss_spread = ((efx->n_rx_channels > 1 ||
  1244. !efx->type->sriov_wanted(efx)) ?
  1245. efx->n_rx_channels : efx_vf_size(efx));
  1246. return 0;
  1247. }
  1248. #endif
  1249. efx->rss_spread = efx->n_rx_channels;
  1250. return 0;
  1251. }
  1252. static int efx_soft_enable_interrupts(struct efx_nic *efx)
  1253. {
  1254. struct efx_channel *channel, *end_channel;
  1255. int rc;
  1256. BUG_ON(efx->state == STATE_DISABLED);
  1257. efx->irq_soft_enabled = true;
  1258. smp_wmb();
  1259. efx_for_each_channel(channel, efx) {
  1260. if (!channel->type->keep_eventq) {
  1261. rc = efx_init_eventq(channel);
  1262. if (rc)
  1263. goto fail;
  1264. }
  1265. efx_start_eventq(channel);
  1266. }
  1267. efx_mcdi_mode_event(efx);
  1268. return 0;
  1269. fail:
  1270. end_channel = channel;
  1271. efx_for_each_channel(channel, efx) {
  1272. if (channel == end_channel)
  1273. break;
  1274. efx_stop_eventq(channel);
  1275. if (!channel->type->keep_eventq)
  1276. efx_fini_eventq(channel);
  1277. }
  1278. return rc;
  1279. }
  1280. static void efx_soft_disable_interrupts(struct efx_nic *efx)
  1281. {
  1282. struct efx_channel *channel;
  1283. if (efx->state == STATE_DISABLED)
  1284. return;
  1285. efx_mcdi_mode_poll(efx);
  1286. efx->irq_soft_enabled = false;
  1287. smp_wmb();
  1288. if (efx->legacy_irq)
  1289. synchronize_irq(efx->legacy_irq);
  1290. efx_for_each_channel(channel, efx) {
  1291. if (channel->irq)
  1292. synchronize_irq(channel->irq);
  1293. efx_stop_eventq(channel);
  1294. if (!channel->type->keep_eventq)
  1295. efx_fini_eventq(channel);
  1296. }
  1297. /* Flush the asynchronous MCDI request queue */
  1298. efx_mcdi_flush_async(efx);
  1299. }
  1300. static int efx_enable_interrupts(struct efx_nic *efx)
  1301. {
  1302. struct efx_channel *channel, *end_channel;
  1303. int rc;
  1304. BUG_ON(efx->state == STATE_DISABLED);
  1305. if (efx->eeh_disabled_legacy_irq) {
  1306. enable_irq(efx->legacy_irq);
  1307. efx->eeh_disabled_legacy_irq = false;
  1308. }
  1309. efx->type->irq_enable_master(efx);
  1310. efx_for_each_channel(channel, efx) {
  1311. if (channel->type->keep_eventq) {
  1312. rc = efx_init_eventq(channel);
  1313. if (rc)
  1314. goto fail;
  1315. }
  1316. }
  1317. rc = efx_soft_enable_interrupts(efx);
  1318. if (rc)
  1319. goto fail;
  1320. return 0;
  1321. fail:
  1322. end_channel = channel;
  1323. efx_for_each_channel(channel, efx) {
  1324. if (channel == end_channel)
  1325. break;
  1326. if (channel->type->keep_eventq)
  1327. efx_fini_eventq(channel);
  1328. }
  1329. efx->type->irq_disable_non_ev(efx);
  1330. return rc;
  1331. }
  1332. static void efx_disable_interrupts(struct efx_nic *efx)
  1333. {
  1334. struct efx_channel *channel;
  1335. efx_soft_disable_interrupts(efx);
  1336. efx_for_each_channel(channel, efx) {
  1337. if (channel->type->keep_eventq)
  1338. efx_fini_eventq(channel);
  1339. }
  1340. efx->type->irq_disable_non_ev(efx);
  1341. }
  1342. static void efx_remove_interrupts(struct efx_nic *efx)
  1343. {
  1344. struct efx_channel *channel;
  1345. /* Remove MSI/MSI-X interrupts */
  1346. efx_for_each_channel(channel, efx)
  1347. channel->irq = 0;
  1348. pci_disable_msi(efx->pci_dev);
  1349. pci_disable_msix(efx->pci_dev);
  1350. /* Remove legacy interrupt */
  1351. efx->legacy_irq = 0;
  1352. }
  1353. static void efx_set_channels(struct efx_nic *efx)
  1354. {
  1355. struct efx_channel *channel;
  1356. struct efx_tx_queue *tx_queue;
  1357. efx->tx_channel_offset =
  1358. separate_tx_channels ? efx->n_channels - efx->n_tx_channels : 0;
  1359. /* We need to mark which channels really have RX and TX
  1360. * queues, and adjust the TX queue numbers if we have separate
  1361. * RX-only and TX-only channels.
  1362. */
  1363. efx_for_each_channel(channel, efx) {
  1364. if (channel->channel < efx->n_rx_channels)
  1365. channel->rx_queue.core_index = channel->channel;
  1366. else
  1367. channel->rx_queue.core_index = -1;
  1368. efx_for_each_channel_tx_queue(tx_queue, channel)
  1369. tx_queue->queue -= (efx->tx_channel_offset *
  1370. EFX_TXQ_TYPES);
  1371. }
  1372. }
  1373. static int efx_probe_nic(struct efx_nic *efx)
  1374. {
  1375. int rc;
  1376. netif_dbg(efx, probe, efx->net_dev, "creating NIC\n");
  1377. /* Carry out hardware-type specific initialisation */
  1378. rc = efx->type->probe(efx);
  1379. if (rc)
  1380. return rc;
  1381. /* Determine the number of channels and queues by trying to hook
  1382. * in MSI-X interrupts. */
  1383. rc = efx_probe_interrupts(efx);
  1384. if (rc)
  1385. goto fail1;
  1386. efx_set_channels(efx);
  1387. rc = efx->type->dimension_resources(efx);
  1388. if (rc)
  1389. goto fail2;
  1390. if (efx->n_channels > 1)
  1391. netdev_rss_key_fill(&efx->rx_hash_key,
  1392. sizeof(efx->rx_hash_key));
  1393. efx_set_default_rx_indir_table(efx);
  1394. netif_set_real_num_tx_queues(efx->net_dev, efx->n_tx_channels);
  1395. netif_set_real_num_rx_queues(efx->net_dev, efx->n_rx_channels);
  1396. /* Initialise the interrupt moderation settings */
  1397. efx_init_irq_moderation(efx, tx_irq_mod_usec, rx_irq_mod_usec, true,
  1398. true);
  1399. return 0;
  1400. fail2:
  1401. efx_remove_interrupts(efx);
  1402. fail1:
  1403. efx->type->remove(efx);
  1404. return rc;
  1405. }
  1406. static void efx_remove_nic(struct efx_nic *efx)
  1407. {
  1408. netif_dbg(efx, drv, efx->net_dev, "destroying NIC\n");
  1409. efx_remove_interrupts(efx);
  1410. efx->type->remove(efx);
  1411. }
  1412. static int efx_probe_filters(struct efx_nic *efx)
  1413. {
  1414. int rc;
  1415. spin_lock_init(&efx->filter_lock);
  1416. init_rwsem(&efx->filter_sem);
  1417. down_write(&efx->filter_sem);
  1418. rc = efx->type->filter_table_probe(efx);
  1419. if (rc)
  1420. goto out_unlock;
  1421. #ifdef CONFIG_RFS_ACCEL
  1422. if (efx->type->offload_features & NETIF_F_NTUPLE) {
  1423. efx->rps_flow_id = kcalloc(efx->type->max_rx_ip_filters,
  1424. sizeof(*efx->rps_flow_id),
  1425. GFP_KERNEL);
  1426. if (!efx->rps_flow_id) {
  1427. efx->type->filter_table_remove(efx);
  1428. rc = -ENOMEM;
  1429. goto out_unlock;
  1430. }
  1431. }
  1432. #endif
  1433. out_unlock:
  1434. up_write(&efx->filter_sem);
  1435. return rc;
  1436. }
  1437. static void efx_remove_filters(struct efx_nic *efx)
  1438. {
  1439. #ifdef CONFIG_RFS_ACCEL
  1440. kfree(efx->rps_flow_id);
  1441. #endif
  1442. down_write(&efx->filter_sem);
  1443. efx->type->filter_table_remove(efx);
  1444. up_write(&efx->filter_sem);
  1445. }
  1446. static void efx_restore_filters(struct efx_nic *efx)
  1447. {
  1448. down_read(&efx->filter_sem);
  1449. efx->type->filter_table_restore(efx);
  1450. up_read(&efx->filter_sem);
  1451. }
  1452. /**************************************************************************
  1453. *
  1454. * NIC startup/shutdown
  1455. *
  1456. *************************************************************************/
  1457. static int efx_probe_all(struct efx_nic *efx)
  1458. {
  1459. int rc;
  1460. rc = efx_probe_nic(efx);
  1461. if (rc) {
  1462. netif_err(efx, probe, efx->net_dev, "failed to create NIC\n");
  1463. goto fail1;
  1464. }
  1465. rc = efx_probe_port(efx);
  1466. if (rc) {
  1467. netif_err(efx, probe, efx->net_dev, "failed to create port\n");
  1468. goto fail2;
  1469. }
  1470. BUILD_BUG_ON(EFX_DEFAULT_DMAQ_SIZE < EFX_RXQ_MIN_ENT);
  1471. if (WARN_ON(EFX_DEFAULT_DMAQ_SIZE < EFX_TXQ_MIN_ENT(efx))) {
  1472. rc = -EINVAL;
  1473. goto fail3;
  1474. }
  1475. efx->rxq_entries = efx->txq_entries = EFX_DEFAULT_DMAQ_SIZE;
  1476. #ifdef CONFIG_SFC_SRIOV
  1477. rc = efx->type->vswitching_probe(efx);
  1478. if (rc) /* not fatal; the PF will still work fine */
  1479. netif_warn(efx, probe, efx->net_dev,
  1480. "failed to setup vswitching rc=%d;"
  1481. " VFs may not function\n", rc);
  1482. #endif
  1483. rc = efx_probe_filters(efx);
  1484. if (rc) {
  1485. netif_err(efx, probe, efx->net_dev,
  1486. "failed to create filter tables\n");
  1487. goto fail4;
  1488. }
  1489. rc = efx_probe_channels(efx);
  1490. if (rc)
  1491. goto fail5;
  1492. return 0;
  1493. fail5:
  1494. efx_remove_filters(efx);
  1495. fail4:
  1496. #ifdef CONFIG_SFC_SRIOV
  1497. efx->type->vswitching_remove(efx);
  1498. #endif
  1499. fail3:
  1500. efx_remove_port(efx);
  1501. fail2:
  1502. efx_remove_nic(efx);
  1503. fail1:
  1504. return rc;
  1505. }
  1506. /* If the interface is supposed to be running but is not, start
  1507. * the hardware and software data path, regular activity for the port
  1508. * (MAC statistics, link polling, etc.) and schedule the port to be
  1509. * reconfigured. Interrupts must already be enabled. This function
  1510. * is safe to call multiple times, so long as the NIC is not disabled.
  1511. * Requires the RTNL lock.
  1512. */
  1513. static void efx_start_all(struct efx_nic *efx)
  1514. {
  1515. EFX_ASSERT_RESET_SERIALISED(efx);
  1516. BUG_ON(efx->state == STATE_DISABLED);
  1517. /* Check that it is appropriate to restart the interface. All
  1518. * of these flags are safe to read under just the rtnl lock */
  1519. if (efx->port_enabled || !netif_running(efx->net_dev) ||
  1520. efx->reset_pending)
  1521. return;
  1522. efx_start_port(efx);
  1523. efx_start_datapath(efx);
  1524. /* Start the hardware monitor if there is one */
  1525. if (efx->type->monitor != NULL)
  1526. queue_delayed_work(efx->workqueue, &efx->monitor_work,
  1527. efx_monitor_interval);
  1528. /* If link state detection is normally event-driven, we have
  1529. * to poll now because we could have missed a change
  1530. */
  1531. if (efx_nic_rev(efx) >= EFX_REV_SIENA_A0) {
  1532. mutex_lock(&efx->mac_lock);
  1533. if (efx->phy_op->poll(efx))
  1534. efx_link_status_changed(efx);
  1535. mutex_unlock(&efx->mac_lock);
  1536. }
  1537. efx->type->start_stats(efx);
  1538. efx->type->pull_stats(efx);
  1539. spin_lock_bh(&efx->stats_lock);
  1540. efx->type->update_stats(efx, NULL, NULL);
  1541. spin_unlock_bh(&efx->stats_lock);
  1542. }
  1543. /* Quiesce the hardware and software data path, and regular activity
  1544. * for the port without bringing the link down. Safe to call multiple
  1545. * times with the NIC in almost any state, but interrupts should be
  1546. * enabled. Requires the RTNL lock.
  1547. */
  1548. static void efx_stop_all(struct efx_nic *efx)
  1549. {
  1550. EFX_ASSERT_RESET_SERIALISED(efx);
  1551. /* port_enabled can be read safely under the rtnl lock */
  1552. if (!efx->port_enabled)
  1553. return;
  1554. /* update stats before we go down so we can accurately count
  1555. * rx_nodesc_drops
  1556. */
  1557. efx->type->pull_stats(efx);
  1558. spin_lock_bh(&efx->stats_lock);
  1559. efx->type->update_stats(efx, NULL, NULL);
  1560. spin_unlock_bh(&efx->stats_lock);
  1561. efx->type->stop_stats(efx);
  1562. efx_stop_port(efx);
  1563. /* Stop the kernel transmit interface. This is only valid if
  1564. * the device is stopped or detached; otherwise the watchdog
  1565. * may fire immediately.
  1566. */
  1567. WARN_ON(netif_running(efx->net_dev) &&
  1568. netif_device_present(efx->net_dev));
  1569. netif_tx_disable(efx->net_dev);
  1570. efx_stop_datapath(efx);
  1571. }
  1572. static void efx_remove_all(struct efx_nic *efx)
  1573. {
  1574. efx_remove_channels(efx);
  1575. efx_remove_filters(efx);
  1576. #ifdef CONFIG_SFC_SRIOV
  1577. efx->type->vswitching_remove(efx);
  1578. #endif
  1579. efx_remove_port(efx);
  1580. efx_remove_nic(efx);
  1581. }
  1582. /**************************************************************************
  1583. *
  1584. * Interrupt moderation
  1585. *
  1586. **************************************************************************/
  1587. static unsigned int irq_mod_ticks(unsigned int usecs, unsigned int quantum_ns)
  1588. {
  1589. if (usecs == 0)
  1590. return 0;
  1591. if (usecs * 1000 < quantum_ns)
  1592. return 1; /* never round down to 0 */
  1593. return usecs * 1000 / quantum_ns;
  1594. }
  1595. /* Set interrupt moderation parameters */
  1596. int efx_init_irq_moderation(struct efx_nic *efx, unsigned int tx_usecs,
  1597. unsigned int rx_usecs, bool rx_adaptive,
  1598. bool rx_may_override_tx)
  1599. {
  1600. struct efx_channel *channel;
  1601. unsigned int irq_mod_max = DIV_ROUND_UP(efx->type->timer_period_max *
  1602. efx->timer_quantum_ns,
  1603. 1000);
  1604. unsigned int tx_ticks;
  1605. unsigned int rx_ticks;
  1606. EFX_ASSERT_RESET_SERIALISED(efx);
  1607. if (tx_usecs > irq_mod_max || rx_usecs > irq_mod_max)
  1608. return -EINVAL;
  1609. tx_ticks = irq_mod_ticks(tx_usecs, efx->timer_quantum_ns);
  1610. rx_ticks = irq_mod_ticks(rx_usecs, efx->timer_quantum_ns);
  1611. if (tx_ticks != rx_ticks && efx->tx_channel_offset == 0 &&
  1612. !rx_may_override_tx) {
  1613. netif_err(efx, drv, efx->net_dev, "Channels are shared. "
  1614. "RX and TX IRQ moderation must be equal\n");
  1615. return -EINVAL;
  1616. }
  1617. efx->irq_rx_adaptive = rx_adaptive;
  1618. efx->irq_rx_moderation = rx_ticks;
  1619. efx_for_each_channel(channel, efx) {
  1620. if (efx_channel_has_rx_queue(channel))
  1621. channel->irq_moderation = rx_ticks;
  1622. else if (efx_channel_has_tx_queues(channel))
  1623. channel->irq_moderation = tx_ticks;
  1624. }
  1625. return 0;
  1626. }
  1627. void efx_get_irq_moderation(struct efx_nic *efx, unsigned int *tx_usecs,
  1628. unsigned int *rx_usecs, bool *rx_adaptive)
  1629. {
  1630. /* We must round up when converting ticks to microseconds
  1631. * because we round down when converting the other way.
  1632. */
  1633. *rx_adaptive = efx->irq_rx_adaptive;
  1634. *rx_usecs = DIV_ROUND_UP(efx->irq_rx_moderation *
  1635. efx->timer_quantum_ns,
  1636. 1000);
  1637. /* If channels are shared between RX and TX, so is IRQ
  1638. * moderation. Otherwise, IRQ moderation is the same for all
  1639. * TX channels and is not adaptive.
  1640. */
  1641. if (efx->tx_channel_offset == 0)
  1642. *tx_usecs = *rx_usecs;
  1643. else
  1644. *tx_usecs = DIV_ROUND_UP(
  1645. efx->channel[efx->tx_channel_offset]->irq_moderation *
  1646. efx->timer_quantum_ns,
  1647. 1000);
  1648. }
  1649. /**************************************************************************
  1650. *
  1651. * Hardware monitor
  1652. *
  1653. **************************************************************************/
  1654. /* Run periodically off the general workqueue */
  1655. static void efx_monitor(struct work_struct *data)
  1656. {
  1657. struct efx_nic *efx = container_of(data, struct efx_nic,
  1658. monitor_work.work);
  1659. netif_vdbg(efx, timer, efx->net_dev,
  1660. "hardware monitor executing on CPU %d\n",
  1661. raw_smp_processor_id());
  1662. BUG_ON(efx->type->monitor == NULL);
  1663. /* If the mac_lock is already held then it is likely a port
  1664. * reconfiguration is already in place, which will likely do
  1665. * most of the work of monitor() anyway. */
  1666. if (mutex_trylock(&efx->mac_lock)) {
  1667. if (efx->port_enabled)
  1668. efx->type->monitor(efx);
  1669. mutex_unlock(&efx->mac_lock);
  1670. }
  1671. queue_delayed_work(efx->workqueue, &efx->monitor_work,
  1672. efx_monitor_interval);
  1673. }
  1674. /**************************************************************************
  1675. *
  1676. * ioctls
  1677. *
  1678. *************************************************************************/
  1679. /* Net device ioctl
  1680. * Context: process, rtnl_lock() held.
  1681. */
  1682. static int efx_ioctl(struct net_device *net_dev, struct ifreq *ifr, int cmd)
  1683. {
  1684. struct efx_nic *efx = netdev_priv(net_dev);
  1685. struct mii_ioctl_data *data = if_mii(ifr);
  1686. if (cmd == SIOCSHWTSTAMP)
  1687. return efx_ptp_set_ts_config(efx, ifr);
  1688. if (cmd == SIOCGHWTSTAMP)
  1689. return efx_ptp_get_ts_config(efx, ifr);
  1690. /* Convert phy_id from older PRTAD/DEVAD format */
  1691. if ((cmd == SIOCGMIIREG || cmd == SIOCSMIIREG) &&
  1692. (data->phy_id & 0xfc00) == 0x0400)
  1693. data->phy_id ^= MDIO_PHY_ID_C45 | 0x0400;
  1694. return mdio_mii_ioctl(&efx->mdio, data, cmd);
  1695. }
  1696. /**************************************************************************
  1697. *
  1698. * NAPI interface
  1699. *
  1700. **************************************************************************/
  1701. static void efx_init_napi_channel(struct efx_channel *channel)
  1702. {
  1703. struct efx_nic *efx = channel->efx;
  1704. channel->napi_dev = efx->net_dev;
  1705. netif_napi_add(channel->napi_dev, &channel->napi_str,
  1706. efx_poll, napi_weight);
  1707. napi_hash_add(&channel->napi_str);
  1708. efx_channel_init_lock(channel);
  1709. }
  1710. static void efx_init_napi(struct efx_nic *efx)
  1711. {
  1712. struct efx_channel *channel;
  1713. efx_for_each_channel(channel, efx)
  1714. efx_init_napi_channel(channel);
  1715. }
  1716. static void efx_fini_napi_channel(struct efx_channel *channel)
  1717. {
  1718. if (channel->napi_dev) {
  1719. netif_napi_del(&channel->napi_str);
  1720. napi_hash_del(&channel->napi_str);
  1721. }
  1722. channel->napi_dev = NULL;
  1723. }
  1724. static void efx_fini_napi(struct efx_nic *efx)
  1725. {
  1726. struct efx_channel *channel;
  1727. efx_for_each_channel(channel, efx)
  1728. efx_fini_napi_channel(channel);
  1729. }
  1730. /**************************************************************************
  1731. *
  1732. * Kernel netpoll interface
  1733. *
  1734. *************************************************************************/
  1735. #ifdef CONFIG_NET_POLL_CONTROLLER
  1736. /* Although in the common case interrupts will be disabled, this is not
  1737. * guaranteed. However, all our work happens inside the NAPI callback,
  1738. * so no locking is required.
  1739. */
  1740. static void efx_netpoll(struct net_device *net_dev)
  1741. {
  1742. struct efx_nic *efx = netdev_priv(net_dev);
  1743. struct efx_channel *channel;
  1744. efx_for_each_channel(channel, efx)
  1745. efx_schedule_channel(channel);
  1746. }
  1747. #endif
  1748. #ifdef CONFIG_NET_RX_BUSY_POLL
  1749. static int efx_busy_poll(struct napi_struct *napi)
  1750. {
  1751. struct efx_channel *channel =
  1752. container_of(napi, struct efx_channel, napi_str);
  1753. struct efx_nic *efx = channel->efx;
  1754. int budget = 4;
  1755. int old_rx_packets, rx_packets;
  1756. if (!netif_running(efx->net_dev))
  1757. return LL_FLUSH_FAILED;
  1758. if (!efx_channel_lock_poll(channel))
  1759. return LL_FLUSH_BUSY;
  1760. old_rx_packets = channel->rx_queue.rx_packets;
  1761. efx_process_channel(channel, budget);
  1762. rx_packets = channel->rx_queue.rx_packets - old_rx_packets;
  1763. /* There is no race condition with NAPI here.
  1764. * NAPI will automatically be rescheduled if it yielded during busy
  1765. * polling, because it was not able to take the lock and thus returned
  1766. * the full budget.
  1767. */
  1768. efx_channel_unlock_poll(channel);
  1769. return rx_packets;
  1770. }
  1771. #endif
  1772. /**************************************************************************
  1773. *
  1774. * Kernel net device interface
  1775. *
  1776. *************************************************************************/
  1777. /* Context: process, rtnl_lock() held. */
  1778. int efx_net_open(struct net_device *net_dev)
  1779. {
  1780. struct efx_nic *efx = netdev_priv(net_dev);
  1781. int rc;
  1782. netif_dbg(efx, ifup, efx->net_dev, "opening device on CPU %d\n",
  1783. raw_smp_processor_id());
  1784. rc = efx_check_disabled(efx);
  1785. if (rc)
  1786. return rc;
  1787. if (efx->phy_mode & PHY_MODE_SPECIAL)
  1788. return -EBUSY;
  1789. if (efx_mcdi_poll_reboot(efx) && efx_reset(efx, RESET_TYPE_ALL))
  1790. return -EIO;
  1791. /* Notify the kernel of the link state polled during driver load,
  1792. * before the monitor starts running */
  1793. efx_link_status_changed(efx);
  1794. efx_start_all(efx);
  1795. efx_selftest_async_start(efx);
  1796. return 0;
  1797. }
  1798. /* Context: process, rtnl_lock() held.
  1799. * Note that the kernel will ignore our return code; this method
  1800. * should really be a void.
  1801. */
  1802. int efx_net_stop(struct net_device *net_dev)
  1803. {
  1804. struct efx_nic *efx = netdev_priv(net_dev);
  1805. netif_dbg(efx, ifdown, efx->net_dev, "closing on CPU %d\n",
  1806. raw_smp_processor_id());
  1807. /* Stop the device and flush all the channels */
  1808. efx_stop_all(efx);
  1809. return 0;
  1810. }
  1811. /* Context: process, dev_base_lock or RTNL held, non-blocking. */
  1812. static struct rtnl_link_stats64 *efx_net_stats(struct net_device *net_dev,
  1813. struct rtnl_link_stats64 *stats)
  1814. {
  1815. struct efx_nic *efx = netdev_priv(net_dev);
  1816. spin_lock_bh(&efx->stats_lock);
  1817. efx->type->update_stats(efx, NULL, stats);
  1818. spin_unlock_bh(&efx->stats_lock);
  1819. return stats;
  1820. }
  1821. /* Context: netif_tx_lock held, BHs disabled. */
  1822. static void efx_watchdog(struct net_device *net_dev)
  1823. {
  1824. struct efx_nic *efx = netdev_priv(net_dev);
  1825. netif_err(efx, tx_err, efx->net_dev,
  1826. "TX stuck with port_enabled=%d: resetting channels\n",
  1827. efx->port_enabled);
  1828. efx_schedule_reset(efx, RESET_TYPE_TX_WATCHDOG);
  1829. }
  1830. /* Context: process, rtnl_lock() held. */
  1831. static int efx_change_mtu(struct net_device *net_dev, int new_mtu)
  1832. {
  1833. struct efx_nic *efx = netdev_priv(net_dev);
  1834. int rc;
  1835. rc = efx_check_disabled(efx);
  1836. if (rc)
  1837. return rc;
  1838. if (new_mtu > EFX_MAX_MTU)
  1839. return -EINVAL;
  1840. netif_dbg(efx, drv, efx->net_dev, "changing MTU to %d\n", new_mtu);
  1841. efx_device_detach_sync(efx);
  1842. efx_stop_all(efx);
  1843. mutex_lock(&efx->mac_lock);
  1844. net_dev->mtu = new_mtu;
  1845. efx_mac_reconfigure(efx);
  1846. mutex_unlock(&efx->mac_lock);
  1847. efx_start_all(efx);
  1848. netif_device_attach(efx->net_dev);
  1849. return 0;
  1850. }
  1851. static int efx_set_mac_address(struct net_device *net_dev, void *data)
  1852. {
  1853. struct efx_nic *efx = netdev_priv(net_dev);
  1854. struct sockaddr *addr = data;
  1855. u8 *new_addr = addr->sa_data;
  1856. u8 old_addr[6];
  1857. int rc;
  1858. if (!is_valid_ether_addr(new_addr)) {
  1859. netif_err(efx, drv, efx->net_dev,
  1860. "invalid ethernet MAC address requested: %pM\n",
  1861. new_addr);
  1862. return -EADDRNOTAVAIL;
  1863. }
  1864. /* save old address */
  1865. ether_addr_copy(old_addr, net_dev->dev_addr);
  1866. ether_addr_copy(net_dev->dev_addr, new_addr);
  1867. if (efx->type->set_mac_address) {
  1868. rc = efx->type->set_mac_address(efx);
  1869. if (rc) {
  1870. ether_addr_copy(net_dev->dev_addr, old_addr);
  1871. return rc;
  1872. }
  1873. }
  1874. /* Reconfigure the MAC */
  1875. mutex_lock(&efx->mac_lock);
  1876. efx_mac_reconfigure(efx);
  1877. mutex_unlock(&efx->mac_lock);
  1878. return 0;
  1879. }
  1880. /* Context: netif_addr_lock held, BHs disabled. */
  1881. static void efx_set_rx_mode(struct net_device *net_dev)
  1882. {
  1883. struct efx_nic *efx = netdev_priv(net_dev);
  1884. if (efx->port_enabled)
  1885. queue_work(efx->workqueue, &efx->mac_work);
  1886. /* Otherwise efx_start_port() will do this */
  1887. }
  1888. static int efx_set_features(struct net_device *net_dev, netdev_features_t data)
  1889. {
  1890. struct efx_nic *efx = netdev_priv(net_dev);
  1891. /* If disabling RX n-tuple filtering, clear existing filters */
  1892. if (net_dev->features & ~data & NETIF_F_NTUPLE)
  1893. return efx->type->filter_clear_rx(efx, EFX_FILTER_PRI_MANUAL);
  1894. return 0;
  1895. }
  1896. static const struct net_device_ops efx_netdev_ops = {
  1897. .ndo_open = efx_net_open,
  1898. .ndo_stop = efx_net_stop,
  1899. .ndo_get_stats64 = efx_net_stats,
  1900. .ndo_tx_timeout = efx_watchdog,
  1901. .ndo_start_xmit = efx_hard_start_xmit,
  1902. .ndo_validate_addr = eth_validate_addr,
  1903. .ndo_do_ioctl = efx_ioctl,
  1904. .ndo_change_mtu = efx_change_mtu,
  1905. .ndo_set_mac_address = efx_set_mac_address,
  1906. .ndo_set_rx_mode = efx_set_rx_mode,
  1907. .ndo_set_features = efx_set_features,
  1908. #ifdef CONFIG_SFC_SRIOV
  1909. .ndo_set_vf_mac = efx_sriov_set_vf_mac,
  1910. .ndo_set_vf_vlan = efx_sriov_set_vf_vlan,
  1911. .ndo_set_vf_spoofchk = efx_sriov_set_vf_spoofchk,
  1912. .ndo_get_vf_config = efx_sriov_get_vf_config,
  1913. .ndo_set_vf_link_state = efx_sriov_set_vf_link_state,
  1914. .ndo_get_phys_port_id = efx_sriov_get_phys_port_id,
  1915. #endif
  1916. #ifdef CONFIG_NET_POLL_CONTROLLER
  1917. .ndo_poll_controller = efx_netpoll,
  1918. #endif
  1919. .ndo_setup_tc = efx_setup_tc,
  1920. #ifdef CONFIG_NET_RX_BUSY_POLL
  1921. .ndo_busy_poll = efx_busy_poll,
  1922. #endif
  1923. #ifdef CONFIG_RFS_ACCEL
  1924. .ndo_rx_flow_steer = efx_filter_rfs,
  1925. #endif
  1926. };
  1927. static void efx_update_name(struct efx_nic *efx)
  1928. {
  1929. strcpy(efx->name, efx->net_dev->name);
  1930. efx_mtd_rename(efx);
  1931. efx_set_channel_names(efx);
  1932. }
  1933. static int efx_netdev_event(struct notifier_block *this,
  1934. unsigned long event, void *ptr)
  1935. {
  1936. struct net_device *net_dev = netdev_notifier_info_to_dev(ptr);
  1937. if ((net_dev->netdev_ops == &efx_netdev_ops) &&
  1938. event == NETDEV_CHANGENAME)
  1939. efx_update_name(netdev_priv(net_dev));
  1940. return NOTIFY_DONE;
  1941. }
  1942. static struct notifier_block efx_netdev_notifier = {
  1943. .notifier_call = efx_netdev_event,
  1944. };
  1945. static ssize_t
  1946. show_phy_type(struct device *dev, struct device_attribute *attr, char *buf)
  1947. {
  1948. struct efx_nic *efx = pci_get_drvdata(to_pci_dev(dev));
  1949. return sprintf(buf, "%d\n", efx->phy_type);
  1950. }
  1951. static DEVICE_ATTR(phy_type, 0444, show_phy_type, NULL);
  1952. #ifdef CONFIG_SFC_MCDI_LOGGING
  1953. static ssize_t show_mcdi_log(struct device *dev, struct device_attribute *attr,
  1954. char *buf)
  1955. {
  1956. struct efx_nic *efx = pci_get_drvdata(to_pci_dev(dev));
  1957. struct efx_mcdi_iface *mcdi = efx_mcdi(efx);
  1958. return scnprintf(buf, PAGE_SIZE, "%d\n", mcdi->logging_enabled);
  1959. }
  1960. static ssize_t set_mcdi_log(struct device *dev, struct device_attribute *attr,
  1961. const char *buf, size_t count)
  1962. {
  1963. struct efx_nic *efx = pci_get_drvdata(to_pci_dev(dev));
  1964. struct efx_mcdi_iface *mcdi = efx_mcdi(efx);
  1965. bool enable = count > 0 && *buf != '0';
  1966. mcdi->logging_enabled = enable;
  1967. return count;
  1968. }
  1969. static DEVICE_ATTR(mcdi_logging, 0644, show_mcdi_log, set_mcdi_log);
  1970. #endif
  1971. static int efx_register_netdev(struct efx_nic *efx)
  1972. {
  1973. struct net_device *net_dev = efx->net_dev;
  1974. struct efx_channel *channel;
  1975. int rc;
  1976. net_dev->watchdog_timeo = 5 * HZ;
  1977. net_dev->irq = efx->pci_dev->irq;
  1978. net_dev->netdev_ops = &efx_netdev_ops;
  1979. if (efx_nic_rev(efx) >= EFX_REV_HUNT_A0)
  1980. net_dev->priv_flags |= IFF_UNICAST_FLT;
  1981. net_dev->ethtool_ops = &efx_ethtool_ops;
  1982. net_dev->gso_max_segs = EFX_TSO_MAX_SEGS;
  1983. rtnl_lock();
  1984. /* Enable resets to be scheduled and check whether any were
  1985. * already requested. If so, the NIC is probably hosed so we
  1986. * abort.
  1987. */
  1988. efx->state = STATE_READY;
  1989. smp_mb(); /* ensure we change state before checking reset_pending */
  1990. if (efx->reset_pending) {
  1991. netif_err(efx, probe, efx->net_dev,
  1992. "aborting probe due to scheduled reset\n");
  1993. rc = -EIO;
  1994. goto fail_locked;
  1995. }
  1996. rc = dev_alloc_name(net_dev, net_dev->name);
  1997. if (rc < 0)
  1998. goto fail_locked;
  1999. efx_update_name(efx);
  2000. /* Always start with carrier off; PHY events will detect the link */
  2001. netif_carrier_off(net_dev);
  2002. rc = register_netdevice(net_dev);
  2003. if (rc)
  2004. goto fail_locked;
  2005. efx_for_each_channel(channel, efx) {
  2006. struct efx_tx_queue *tx_queue;
  2007. efx_for_each_channel_tx_queue(tx_queue, channel)
  2008. efx_init_tx_queue_core_txq(tx_queue);
  2009. }
  2010. efx_associate(efx);
  2011. rtnl_unlock();
  2012. rc = device_create_file(&efx->pci_dev->dev, &dev_attr_phy_type);
  2013. if (rc) {
  2014. netif_err(efx, drv, efx->net_dev,
  2015. "failed to init net dev attributes\n");
  2016. goto fail_registered;
  2017. }
  2018. #ifdef CONFIG_SFC_MCDI_LOGGING
  2019. rc = device_create_file(&efx->pci_dev->dev, &dev_attr_mcdi_logging);
  2020. if (rc) {
  2021. netif_err(efx, drv, efx->net_dev,
  2022. "failed to init net dev attributes\n");
  2023. goto fail_attr_mcdi_logging;
  2024. }
  2025. #endif
  2026. return 0;
  2027. #ifdef CONFIG_SFC_MCDI_LOGGING
  2028. fail_attr_mcdi_logging:
  2029. device_remove_file(&efx->pci_dev->dev, &dev_attr_phy_type);
  2030. #endif
  2031. fail_registered:
  2032. rtnl_lock();
  2033. efx_dissociate(efx);
  2034. unregister_netdevice(net_dev);
  2035. fail_locked:
  2036. efx->state = STATE_UNINIT;
  2037. rtnl_unlock();
  2038. netif_err(efx, drv, efx->net_dev, "could not register net dev\n");
  2039. return rc;
  2040. }
  2041. static void efx_unregister_netdev(struct efx_nic *efx)
  2042. {
  2043. if (!efx->net_dev)
  2044. return;
  2045. BUG_ON(netdev_priv(efx->net_dev) != efx);
  2046. if (efx_dev_registered(efx)) {
  2047. strlcpy(efx->name, pci_name(efx->pci_dev), sizeof(efx->name));
  2048. #ifdef CONFIG_SFC_MCDI_LOGGING
  2049. device_remove_file(&efx->pci_dev->dev, &dev_attr_mcdi_logging);
  2050. #endif
  2051. device_remove_file(&efx->pci_dev->dev, &dev_attr_phy_type);
  2052. unregister_netdev(efx->net_dev);
  2053. }
  2054. }
  2055. /**************************************************************************
  2056. *
  2057. * Device reset and suspend
  2058. *
  2059. **************************************************************************/
  2060. /* Tears down the entire software state and most of the hardware state
  2061. * before reset. */
  2062. void efx_reset_down(struct efx_nic *efx, enum reset_type method)
  2063. {
  2064. EFX_ASSERT_RESET_SERIALISED(efx);
  2065. if (method == RESET_TYPE_MCDI_TIMEOUT)
  2066. efx->type->prepare_flr(efx);
  2067. efx_stop_all(efx);
  2068. efx_disable_interrupts(efx);
  2069. mutex_lock(&efx->mac_lock);
  2070. if (efx->port_initialized && method != RESET_TYPE_INVISIBLE &&
  2071. method != RESET_TYPE_DATAPATH)
  2072. efx->phy_op->fini(efx);
  2073. efx->type->fini(efx);
  2074. }
  2075. /* This function will always ensure that the locks acquired in
  2076. * efx_reset_down() are released. A failure return code indicates
  2077. * that we were unable to reinitialise the hardware, and the
  2078. * driver should be disabled. If ok is false, then the rx and tx
  2079. * engines are not restarted, pending a RESET_DISABLE. */
  2080. int efx_reset_up(struct efx_nic *efx, enum reset_type method, bool ok)
  2081. {
  2082. int rc;
  2083. EFX_ASSERT_RESET_SERIALISED(efx);
  2084. if (method == RESET_TYPE_MCDI_TIMEOUT)
  2085. efx->type->finish_flr(efx);
  2086. /* Ensure that SRAM is initialised even if we're disabling the device */
  2087. rc = efx->type->init(efx);
  2088. if (rc) {
  2089. netif_err(efx, drv, efx->net_dev, "failed to initialise NIC\n");
  2090. goto fail;
  2091. }
  2092. if (!ok)
  2093. goto fail;
  2094. if (efx->port_initialized && method != RESET_TYPE_INVISIBLE &&
  2095. method != RESET_TYPE_DATAPATH) {
  2096. rc = efx->phy_op->init(efx);
  2097. if (rc)
  2098. goto fail;
  2099. rc = efx->phy_op->reconfigure(efx);
  2100. if (rc && rc != -EPERM)
  2101. netif_err(efx, drv, efx->net_dev,
  2102. "could not restore PHY settings\n");
  2103. }
  2104. rc = efx_enable_interrupts(efx);
  2105. if (rc)
  2106. goto fail;
  2107. #ifdef CONFIG_SFC_SRIOV
  2108. rc = efx->type->vswitching_restore(efx);
  2109. if (rc) /* not fatal; the PF will still work fine */
  2110. netif_warn(efx, probe, efx->net_dev,
  2111. "failed to restore vswitching rc=%d;"
  2112. " VFs may not function\n", rc);
  2113. #endif
  2114. down_read(&efx->filter_sem);
  2115. efx_restore_filters(efx);
  2116. up_read(&efx->filter_sem);
  2117. if (efx->type->sriov_reset)
  2118. efx->type->sriov_reset(efx);
  2119. mutex_unlock(&efx->mac_lock);
  2120. efx_start_all(efx);
  2121. return 0;
  2122. fail:
  2123. efx->port_initialized = false;
  2124. mutex_unlock(&efx->mac_lock);
  2125. return rc;
  2126. }
  2127. /* Reset the NIC using the specified method. Note that the reset may
  2128. * fail, in which case the card will be left in an unusable state.
  2129. *
  2130. * Caller must hold the rtnl_lock.
  2131. */
  2132. int efx_reset(struct efx_nic *efx, enum reset_type method)
  2133. {
  2134. int rc, rc2;
  2135. bool disabled;
  2136. netif_info(efx, drv, efx->net_dev, "resetting (%s)\n",
  2137. RESET_TYPE(method));
  2138. efx_device_detach_sync(efx);
  2139. efx_reset_down(efx, method);
  2140. rc = efx->type->reset(efx, method);
  2141. if (rc) {
  2142. netif_err(efx, drv, efx->net_dev, "failed to reset hardware\n");
  2143. goto out;
  2144. }
  2145. /* Clear flags for the scopes we covered. We assume the NIC and
  2146. * driver are now quiescent so that there is no race here.
  2147. */
  2148. if (method < RESET_TYPE_MAX_METHOD)
  2149. efx->reset_pending &= -(1 << (method + 1));
  2150. else /* it doesn't fit into the well-ordered scope hierarchy */
  2151. __clear_bit(method, &efx->reset_pending);
  2152. /* Reinitialise bus-mastering, which may have been turned off before
  2153. * the reset was scheduled. This is still appropriate, even in the
  2154. * RESET_TYPE_DISABLE since this driver generally assumes the hardware
  2155. * can respond to requests. */
  2156. pci_set_master(efx->pci_dev);
  2157. out:
  2158. /* Leave device stopped if necessary */
  2159. disabled = rc ||
  2160. method == RESET_TYPE_DISABLE ||
  2161. method == RESET_TYPE_RECOVER_OR_DISABLE;
  2162. rc2 = efx_reset_up(efx, method, !disabled);
  2163. if (rc2) {
  2164. disabled = true;
  2165. if (!rc)
  2166. rc = rc2;
  2167. }
  2168. if (disabled) {
  2169. dev_close(efx->net_dev);
  2170. netif_err(efx, drv, efx->net_dev, "has been disabled\n");
  2171. efx->state = STATE_DISABLED;
  2172. } else {
  2173. netif_dbg(efx, drv, efx->net_dev, "reset complete\n");
  2174. netif_device_attach(efx->net_dev);
  2175. }
  2176. return rc;
  2177. }
  2178. /* Try recovery mechanisms.
  2179. * For now only EEH is supported.
  2180. * Returns 0 if the recovery mechanisms are unsuccessful.
  2181. * Returns a non-zero value otherwise.
  2182. */
  2183. int efx_try_recovery(struct efx_nic *efx)
  2184. {
  2185. #ifdef CONFIG_EEH
  2186. /* A PCI error can occur and not be seen by EEH because nothing
  2187. * happens on the PCI bus. In this case the driver may fail and
  2188. * schedule a 'recover or reset', leading to this recovery handler.
  2189. * Manually call the eeh failure check function.
  2190. */
  2191. struct eeh_dev *eehdev = pci_dev_to_eeh_dev(efx->pci_dev);
  2192. if (eeh_dev_check_failure(eehdev)) {
  2193. /* The EEH mechanisms will handle the error and reset the
  2194. * device if necessary.
  2195. */
  2196. return 1;
  2197. }
  2198. #endif
  2199. return 0;
  2200. }
  2201. static void efx_wait_for_bist_end(struct efx_nic *efx)
  2202. {
  2203. int i;
  2204. for (i = 0; i < BIST_WAIT_DELAY_COUNT; ++i) {
  2205. if (efx_mcdi_poll_reboot(efx))
  2206. goto out;
  2207. msleep(BIST_WAIT_DELAY_MS);
  2208. }
  2209. netif_err(efx, drv, efx->net_dev, "Warning: No MC reboot after BIST mode\n");
  2210. out:
  2211. /* Either way unset the BIST flag. If we found no reboot we probably
  2212. * won't recover, but we should try.
  2213. */
  2214. efx->mc_bist_for_other_fn = false;
  2215. }
  2216. /* The worker thread exists so that code that cannot sleep can
  2217. * schedule a reset for later.
  2218. */
  2219. static void efx_reset_work(struct work_struct *data)
  2220. {
  2221. struct efx_nic *efx = container_of(data, struct efx_nic, reset_work);
  2222. unsigned long pending;
  2223. enum reset_type method;
  2224. pending = ACCESS_ONCE(efx->reset_pending);
  2225. method = fls(pending) - 1;
  2226. if (method == RESET_TYPE_MC_BIST)
  2227. efx_wait_for_bist_end(efx);
  2228. if ((method == RESET_TYPE_RECOVER_OR_DISABLE ||
  2229. method == RESET_TYPE_RECOVER_OR_ALL) &&
  2230. efx_try_recovery(efx))
  2231. return;
  2232. if (!pending)
  2233. return;
  2234. rtnl_lock();
  2235. /* We checked the state in efx_schedule_reset() but it may
  2236. * have changed by now. Now that we have the RTNL lock,
  2237. * it cannot change again.
  2238. */
  2239. if (efx->state == STATE_READY)
  2240. (void)efx_reset(efx, method);
  2241. rtnl_unlock();
  2242. }
  2243. void efx_schedule_reset(struct efx_nic *efx, enum reset_type type)
  2244. {
  2245. enum reset_type method;
  2246. if (efx->state == STATE_RECOVERY) {
  2247. netif_dbg(efx, drv, efx->net_dev,
  2248. "recovering: skip scheduling %s reset\n",
  2249. RESET_TYPE(type));
  2250. return;
  2251. }
  2252. switch (type) {
  2253. case RESET_TYPE_INVISIBLE:
  2254. case RESET_TYPE_ALL:
  2255. case RESET_TYPE_RECOVER_OR_ALL:
  2256. case RESET_TYPE_WORLD:
  2257. case RESET_TYPE_DISABLE:
  2258. case RESET_TYPE_RECOVER_OR_DISABLE:
  2259. case RESET_TYPE_DATAPATH:
  2260. case RESET_TYPE_MC_BIST:
  2261. case RESET_TYPE_MCDI_TIMEOUT:
  2262. method = type;
  2263. netif_dbg(efx, drv, efx->net_dev, "scheduling %s reset\n",
  2264. RESET_TYPE(method));
  2265. break;
  2266. default:
  2267. method = efx->type->map_reset_reason(type);
  2268. netif_dbg(efx, drv, efx->net_dev,
  2269. "scheduling %s reset for %s\n",
  2270. RESET_TYPE(method), RESET_TYPE(type));
  2271. break;
  2272. }
  2273. set_bit(method, &efx->reset_pending);
  2274. smp_mb(); /* ensure we change reset_pending before checking state */
  2275. /* If we're not READY then just leave the flags set as the cue
  2276. * to abort probing or reschedule the reset later.
  2277. */
  2278. if (ACCESS_ONCE(efx->state) != STATE_READY)
  2279. return;
  2280. /* efx_process_channel() will no longer read events once a
  2281. * reset is scheduled. So switch back to poll'd MCDI completions. */
  2282. efx_mcdi_mode_poll(efx);
  2283. queue_work(reset_workqueue, &efx->reset_work);
  2284. }
  2285. /**************************************************************************
  2286. *
  2287. * List of NICs we support
  2288. *
  2289. **************************************************************************/
  2290. /* PCI device ID table */
  2291. static const struct pci_device_id efx_pci_table[] = {
  2292. {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE,
  2293. PCI_DEVICE_ID_SOLARFLARE_SFC4000A_0),
  2294. .driver_data = (unsigned long) &falcon_a1_nic_type},
  2295. {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE,
  2296. PCI_DEVICE_ID_SOLARFLARE_SFC4000B),
  2297. .driver_data = (unsigned long) &falcon_b0_nic_type},
  2298. {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0803), /* SFC9020 */
  2299. .driver_data = (unsigned long) &siena_a0_nic_type},
  2300. {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0813), /* SFL9021 */
  2301. .driver_data = (unsigned long) &siena_a0_nic_type},
  2302. {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0903), /* SFC9120 PF */
  2303. .driver_data = (unsigned long) &efx_hunt_a0_nic_type},
  2304. {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x1903), /* SFC9120 VF */
  2305. .driver_data = (unsigned long) &efx_hunt_a0_vf_nic_type},
  2306. {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0923), /* SFC9140 PF */
  2307. .driver_data = (unsigned long) &efx_hunt_a0_nic_type},
  2308. {0} /* end of list */
  2309. };
  2310. /**************************************************************************
  2311. *
  2312. * Dummy PHY/MAC operations
  2313. *
  2314. * Can be used for some unimplemented operations
  2315. * Needed so all function pointers are valid and do not have to be tested
  2316. * before use
  2317. *
  2318. **************************************************************************/
  2319. int efx_port_dummy_op_int(struct efx_nic *efx)
  2320. {
  2321. return 0;
  2322. }
  2323. void efx_port_dummy_op_void(struct efx_nic *efx) {}
  2324. static bool efx_port_dummy_op_poll(struct efx_nic *efx)
  2325. {
  2326. return false;
  2327. }
  2328. static const struct efx_phy_operations efx_dummy_phy_operations = {
  2329. .init = efx_port_dummy_op_int,
  2330. .reconfigure = efx_port_dummy_op_int,
  2331. .poll = efx_port_dummy_op_poll,
  2332. .fini = efx_port_dummy_op_void,
  2333. };
  2334. /**************************************************************************
  2335. *
  2336. * Data housekeeping
  2337. *
  2338. **************************************************************************/
  2339. /* This zeroes out and then fills in the invariants in a struct
  2340. * efx_nic (including all sub-structures).
  2341. */
  2342. static int efx_init_struct(struct efx_nic *efx,
  2343. struct pci_dev *pci_dev, struct net_device *net_dev)
  2344. {
  2345. int i;
  2346. /* Initialise common structures */
  2347. INIT_LIST_HEAD(&efx->node);
  2348. INIT_LIST_HEAD(&efx->secondary_list);
  2349. spin_lock_init(&efx->biu_lock);
  2350. #ifdef CONFIG_SFC_MTD
  2351. INIT_LIST_HEAD(&efx->mtd_list);
  2352. #endif
  2353. INIT_WORK(&efx->reset_work, efx_reset_work);
  2354. INIT_DELAYED_WORK(&efx->monitor_work, efx_monitor);
  2355. INIT_DELAYED_WORK(&efx->selftest_work, efx_selftest_async_work);
  2356. efx->pci_dev = pci_dev;
  2357. efx->msg_enable = debug;
  2358. efx->state = STATE_UNINIT;
  2359. strlcpy(efx->name, pci_name(pci_dev), sizeof(efx->name));
  2360. efx->net_dev = net_dev;
  2361. efx->rx_prefix_size = efx->type->rx_prefix_size;
  2362. efx->rx_ip_align =
  2363. NET_IP_ALIGN ? (efx->rx_prefix_size + NET_IP_ALIGN) % 4 : 0;
  2364. efx->rx_packet_hash_offset =
  2365. efx->type->rx_hash_offset - efx->type->rx_prefix_size;
  2366. efx->rx_packet_ts_offset =
  2367. efx->type->rx_ts_offset - efx->type->rx_prefix_size;
  2368. spin_lock_init(&efx->stats_lock);
  2369. mutex_init(&efx->mac_lock);
  2370. efx->phy_op = &efx_dummy_phy_operations;
  2371. efx->mdio.dev = net_dev;
  2372. INIT_WORK(&efx->mac_work, efx_mac_work);
  2373. init_waitqueue_head(&efx->flush_wq);
  2374. for (i = 0; i < EFX_MAX_CHANNELS; i++) {
  2375. efx->channel[i] = efx_alloc_channel(efx, i, NULL);
  2376. if (!efx->channel[i])
  2377. goto fail;
  2378. efx->msi_context[i].efx = efx;
  2379. efx->msi_context[i].index = i;
  2380. }
  2381. /* Higher numbered interrupt modes are less capable! */
  2382. efx->interrupt_mode = max(efx->type->max_interrupt_mode,
  2383. interrupt_mode);
  2384. /* Would be good to use the net_dev name, but we're too early */
  2385. snprintf(efx->workqueue_name, sizeof(efx->workqueue_name), "sfc%s",
  2386. pci_name(pci_dev));
  2387. efx->workqueue = create_singlethread_workqueue(efx->workqueue_name);
  2388. if (!efx->workqueue)
  2389. goto fail;
  2390. return 0;
  2391. fail:
  2392. efx_fini_struct(efx);
  2393. return -ENOMEM;
  2394. }
  2395. static void efx_fini_struct(struct efx_nic *efx)
  2396. {
  2397. int i;
  2398. for (i = 0; i < EFX_MAX_CHANNELS; i++)
  2399. kfree(efx->channel[i]);
  2400. kfree(efx->vpd_sn);
  2401. if (efx->workqueue) {
  2402. destroy_workqueue(efx->workqueue);
  2403. efx->workqueue = NULL;
  2404. }
  2405. }
  2406. void efx_update_sw_stats(struct efx_nic *efx, u64 *stats)
  2407. {
  2408. u64 n_rx_nodesc_trunc = 0;
  2409. struct efx_channel *channel;
  2410. efx_for_each_channel(channel, efx)
  2411. n_rx_nodesc_trunc += channel->n_rx_nodesc_trunc;
  2412. stats[GENERIC_STAT_rx_nodesc_trunc] = n_rx_nodesc_trunc;
  2413. stats[GENERIC_STAT_rx_noskb_drops] = atomic_read(&efx->n_rx_noskb_drops);
  2414. }
  2415. /**************************************************************************
  2416. *
  2417. * PCI interface
  2418. *
  2419. **************************************************************************/
  2420. /* Main body of final NIC shutdown code
  2421. * This is called only at module unload (or hotplug removal).
  2422. */
  2423. static void efx_pci_remove_main(struct efx_nic *efx)
  2424. {
  2425. /* Flush reset_work. It can no longer be scheduled since we
  2426. * are not READY.
  2427. */
  2428. BUG_ON(efx->state == STATE_READY);
  2429. cancel_work_sync(&efx->reset_work);
  2430. efx_disable_interrupts(efx);
  2431. efx_nic_fini_interrupt(efx);
  2432. efx_fini_port(efx);
  2433. efx->type->fini(efx);
  2434. efx_fini_napi(efx);
  2435. efx_remove_all(efx);
  2436. }
  2437. /* Final NIC shutdown
  2438. * This is called only at module unload (or hotplug removal). A PF can call
  2439. * this on its VFs to ensure they are unbound first.
  2440. */
  2441. static void efx_pci_remove(struct pci_dev *pci_dev)
  2442. {
  2443. struct efx_nic *efx;
  2444. efx = pci_get_drvdata(pci_dev);
  2445. if (!efx)
  2446. return;
  2447. /* Mark the NIC as fini, then stop the interface */
  2448. rtnl_lock();
  2449. efx_dissociate(efx);
  2450. dev_close(efx->net_dev);
  2451. efx_disable_interrupts(efx);
  2452. efx->state = STATE_UNINIT;
  2453. rtnl_unlock();
  2454. if (efx->type->sriov_fini)
  2455. efx->type->sriov_fini(efx);
  2456. efx_unregister_netdev(efx);
  2457. efx_mtd_remove(efx);
  2458. efx_pci_remove_main(efx);
  2459. efx_fini_io(efx);
  2460. netif_dbg(efx, drv, efx->net_dev, "shutdown successful\n");
  2461. efx_fini_struct(efx);
  2462. free_netdev(efx->net_dev);
  2463. pci_disable_pcie_error_reporting(pci_dev);
  2464. };
  2465. /* NIC VPD information
  2466. * Called during probe to display the part number of the
  2467. * installed NIC. VPD is potentially very large but this should
  2468. * always appear within the first 512 bytes.
  2469. */
  2470. #define SFC_VPD_LEN 512
  2471. static void efx_probe_vpd_strings(struct efx_nic *efx)
  2472. {
  2473. struct pci_dev *dev = efx->pci_dev;
  2474. char vpd_data[SFC_VPD_LEN];
  2475. ssize_t vpd_size;
  2476. int ro_start, ro_size, i, j;
  2477. /* Get the vpd data from the device */
  2478. vpd_size = pci_read_vpd(dev, 0, sizeof(vpd_data), vpd_data);
  2479. if (vpd_size <= 0) {
  2480. netif_err(efx, drv, efx->net_dev, "Unable to read VPD\n");
  2481. return;
  2482. }
  2483. /* Get the Read only section */
  2484. ro_start = pci_vpd_find_tag(vpd_data, 0, vpd_size, PCI_VPD_LRDT_RO_DATA);
  2485. if (ro_start < 0) {
  2486. netif_err(efx, drv, efx->net_dev, "VPD Read-only not found\n");
  2487. return;
  2488. }
  2489. ro_size = pci_vpd_lrdt_size(&vpd_data[ro_start]);
  2490. j = ro_size;
  2491. i = ro_start + PCI_VPD_LRDT_TAG_SIZE;
  2492. if (i + j > vpd_size)
  2493. j = vpd_size - i;
  2494. /* Get the Part number */
  2495. i = pci_vpd_find_info_keyword(vpd_data, i, j, "PN");
  2496. if (i < 0) {
  2497. netif_err(efx, drv, efx->net_dev, "Part number not found\n");
  2498. return;
  2499. }
  2500. j = pci_vpd_info_field_size(&vpd_data[i]);
  2501. i += PCI_VPD_INFO_FLD_HDR_SIZE;
  2502. if (i + j > vpd_size) {
  2503. netif_err(efx, drv, efx->net_dev, "Incomplete part number\n");
  2504. return;
  2505. }
  2506. netif_info(efx, drv, efx->net_dev,
  2507. "Part Number : %.*s\n", j, &vpd_data[i]);
  2508. i = ro_start + PCI_VPD_LRDT_TAG_SIZE;
  2509. j = ro_size;
  2510. i = pci_vpd_find_info_keyword(vpd_data, i, j, "SN");
  2511. if (i < 0) {
  2512. netif_err(efx, drv, efx->net_dev, "Serial number not found\n");
  2513. return;
  2514. }
  2515. j = pci_vpd_info_field_size(&vpd_data[i]);
  2516. i += PCI_VPD_INFO_FLD_HDR_SIZE;
  2517. if (i + j > vpd_size) {
  2518. netif_err(efx, drv, efx->net_dev, "Incomplete serial number\n");
  2519. return;
  2520. }
  2521. efx->vpd_sn = kmalloc(j + 1, GFP_KERNEL);
  2522. if (!efx->vpd_sn)
  2523. return;
  2524. snprintf(efx->vpd_sn, j + 1, "%s", &vpd_data[i]);
  2525. }
  2526. /* Main body of NIC initialisation
  2527. * This is called at module load (or hotplug insertion, theoretically).
  2528. */
  2529. static int efx_pci_probe_main(struct efx_nic *efx)
  2530. {
  2531. int rc;
  2532. /* Do start-of-day initialisation */
  2533. rc = efx_probe_all(efx);
  2534. if (rc)
  2535. goto fail1;
  2536. efx_init_napi(efx);
  2537. rc = efx->type->init(efx);
  2538. if (rc) {
  2539. netif_err(efx, probe, efx->net_dev,
  2540. "failed to initialise NIC\n");
  2541. goto fail3;
  2542. }
  2543. rc = efx_init_port(efx);
  2544. if (rc) {
  2545. netif_err(efx, probe, efx->net_dev,
  2546. "failed to initialise port\n");
  2547. goto fail4;
  2548. }
  2549. rc = efx_nic_init_interrupt(efx);
  2550. if (rc)
  2551. goto fail5;
  2552. rc = efx_enable_interrupts(efx);
  2553. if (rc)
  2554. goto fail6;
  2555. return 0;
  2556. fail6:
  2557. efx_nic_fini_interrupt(efx);
  2558. fail5:
  2559. efx_fini_port(efx);
  2560. fail4:
  2561. efx->type->fini(efx);
  2562. fail3:
  2563. efx_fini_napi(efx);
  2564. efx_remove_all(efx);
  2565. fail1:
  2566. return rc;
  2567. }
  2568. /* NIC initialisation
  2569. *
  2570. * This is called at module load (or hotplug insertion,
  2571. * theoretically). It sets up PCI mappings, resets the NIC,
  2572. * sets up and registers the network devices with the kernel and hooks
  2573. * the interrupt service routine. It does not prepare the device for
  2574. * transmission; this is left to the first time one of the network
  2575. * interfaces is brought up (i.e. efx_net_open).
  2576. */
  2577. static int efx_pci_probe(struct pci_dev *pci_dev,
  2578. const struct pci_device_id *entry)
  2579. {
  2580. struct net_device *net_dev;
  2581. struct efx_nic *efx;
  2582. int rc;
  2583. /* Allocate and initialise a struct net_device and struct efx_nic */
  2584. net_dev = alloc_etherdev_mqs(sizeof(*efx), EFX_MAX_CORE_TX_QUEUES,
  2585. EFX_MAX_RX_QUEUES);
  2586. if (!net_dev)
  2587. return -ENOMEM;
  2588. efx = netdev_priv(net_dev);
  2589. efx->type = (const struct efx_nic_type *) entry->driver_data;
  2590. net_dev->features |= (efx->type->offload_features | NETIF_F_SG |
  2591. NETIF_F_HIGHDMA | NETIF_F_TSO |
  2592. NETIF_F_RXCSUM);
  2593. if (efx->type->offload_features & NETIF_F_V6_CSUM)
  2594. net_dev->features |= NETIF_F_TSO6;
  2595. /* Mask for features that also apply to VLAN devices */
  2596. net_dev->vlan_features |= (NETIF_F_ALL_CSUM | NETIF_F_SG |
  2597. NETIF_F_HIGHDMA | NETIF_F_ALL_TSO |
  2598. NETIF_F_RXCSUM);
  2599. /* All offloads can be toggled */
  2600. net_dev->hw_features = net_dev->features & ~NETIF_F_HIGHDMA;
  2601. pci_set_drvdata(pci_dev, efx);
  2602. SET_NETDEV_DEV(net_dev, &pci_dev->dev);
  2603. rc = efx_init_struct(efx, pci_dev, net_dev);
  2604. if (rc)
  2605. goto fail1;
  2606. netif_info(efx, probe, efx->net_dev,
  2607. "Solarflare NIC detected\n");
  2608. if (!efx->type->is_vf)
  2609. efx_probe_vpd_strings(efx);
  2610. /* Set up basic I/O (BAR mappings etc) */
  2611. rc = efx_init_io(efx);
  2612. if (rc)
  2613. goto fail2;
  2614. rc = efx_pci_probe_main(efx);
  2615. if (rc)
  2616. goto fail3;
  2617. rc = efx_register_netdev(efx);
  2618. if (rc)
  2619. goto fail4;
  2620. if (efx->type->sriov_init) {
  2621. rc = efx->type->sriov_init(efx);
  2622. if (rc)
  2623. netif_err(efx, probe, efx->net_dev,
  2624. "SR-IOV can't be enabled rc %d\n", rc);
  2625. }
  2626. netif_dbg(efx, probe, efx->net_dev, "initialisation successful\n");
  2627. /* Try to create MTDs, but allow this to fail */
  2628. rtnl_lock();
  2629. rc = efx_mtd_probe(efx);
  2630. rtnl_unlock();
  2631. if (rc)
  2632. netif_warn(efx, probe, efx->net_dev,
  2633. "failed to create MTDs (%d)\n", rc);
  2634. rc = pci_enable_pcie_error_reporting(pci_dev);
  2635. if (rc && rc != -EINVAL)
  2636. netif_warn(efx, probe, efx->net_dev,
  2637. "pci_enable_pcie_error_reporting failed (%d)\n", rc);
  2638. return 0;
  2639. fail4:
  2640. efx_pci_remove_main(efx);
  2641. fail3:
  2642. efx_fini_io(efx);
  2643. fail2:
  2644. efx_fini_struct(efx);
  2645. fail1:
  2646. WARN_ON(rc > 0);
  2647. netif_dbg(efx, drv, efx->net_dev, "initialisation failed. rc=%d\n", rc);
  2648. free_netdev(net_dev);
  2649. return rc;
  2650. }
  2651. /* efx_pci_sriov_configure returns the actual number of Virtual Functions
  2652. * enabled on success
  2653. */
  2654. #ifdef CONFIG_SFC_SRIOV
  2655. static int efx_pci_sriov_configure(struct pci_dev *dev, int num_vfs)
  2656. {
  2657. int rc;
  2658. struct efx_nic *efx = pci_get_drvdata(dev);
  2659. if (efx->type->sriov_configure) {
  2660. rc = efx->type->sriov_configure(efx, num_vfs);
  2661. if (rc)
  2662. return rc;
  2663. else
  2664. return num_vfs;
  2665. } else
  2666. return -EOPNOTSUPP;
  2667. }
  2668. #endif
  2669. static int efx_pm_freeze(struct device *dev)
  2670. {
  2671. struct efx_nic *efx = pci_get_drvdata(to_pci_dev(dev));
  2672. rtnl_lock();
  2673. if (efx->state != STATE_DISABLED) {
  2674. efx->state = STATE_UNINIT;
  2675. efx_device_detach_sync(efx);
  2676. efx_stop_all(efx);
  2677. efx_disable_interrupts(efx);
  2678. }
  2679. rtnl_unlock();
  2680. return 0;
  2681. }
  2682. static int efx_pm_thaw(struct device *dev)
  2683. {
  2684. int rc;
  2685. struct efx_nic *efx = pci_get_drvdata(to_pci_dev(dev));
  2686. rtnl_lock();
  2687. if (efx->state != STATE_DISABLED) {
  2688. rc = efx_enable_interrupts(efx);
  2689. if (rc)
  2690. goto fail;
  2691. mutex_lock(&efx->mac_lock);
  2692. efx->phy_op->reconfigure(efx);
  2693. mutex_unlock(&efx->mac_lock);
  2694. efx_start_all(efx);
  2695. netif_device_attach(efx->net_dev);
  2696. efx->state = STATE_READY;
  2697. efx->type->resume_wol(efx);
  2698. }
  2699. rtnl_unlock();
  2700. /* Reschedule any quenched resets scheduled during efx_pm_freeze() */
  2701. queue_work(reset_workqueue, &efx->reset_work);
  2702. return 0;
  2703. fail:
  2704. rtnl_unlock();
  2705. return rc;
  2706. }
  2707. static int efx_pm_poweroff(struct device *dev)
  2708. {
  2709. struct pci_dev *pci_dev = to_pci_dev(dev);
  2710. struct efx_nic *efx = pci_get_drvdata(pci_dev);
  2711. efx->type->fini(efx);
  2712. efx->reset_pending = 0;
  2713. pci_save_state(pci_dev);
  2714. return pci_set_power_state(pci_dev, PCI_D3hot);
  2715. }
  2716. /* Used for both resume and restore */
  2717. static int efx_pm_resume(struct device *dev)
  2718. {
  2719. struct pci_dev *pci_dev = to_pci_dev(dev);
  2720. struct efx_nic *efx = pci_get_drvdata(pci_dev);
  2721. int rc;
  2722. rc = pci_set_power_state(pci_dev, PCI_D0);
  2723. if (rc)
  2724. return rc;
  2725. pci_restore_state(pci_dev);
  2726. rc = pci_enable_device(pci_dev);
  2727. if (rc)
  2728. return rc;
  2729. pci_set_master(efx->pci_dev);
  2730. rc = efx->type->reset(efx, RESET_TYPE_ALL);
  2731. if (rc)
  2732. return rc;
  2733. rc = efx->type->init(efx);
  2734. if (rc)
  2735. return rc;
  2736. rc = efx_pm_thaw(dev);
  2737. return rc;
  2738. }
  2739. static int efx_pm_suspend(struct device *dev)
  2740. {
  2741. int rc;
  2742. efx_pm_freeze(dev);
  2743. rc = efx_pm_poweroff(dev);
  2744. if (rc)
  2745. efx_pm_resume(dev);
  2746. return rc;
  2747. }
  2748. static const struct dev_pm_ops efx_pm_ops = {
  2749. .suspend = efx_pm_suspend,
  2750. .resume = efx_pm_resume,
  2751. .freeze = efx_pm_freeze,
  2752. .thaw = efx_pm_thaw,
  2753. .poweroff = efx_pm_poweroff,
  2754. .restore = efx_pm_resume,
  2755. };
  2756. /* A PCI error affecting this device was detected.
  2757. * At this point MMIO and DMA may be disabled.
  2758. * Stop the software path and request a slot reset.
  2759. */
  2760. static pci_ers_result_t efx_io_error_detected(struct pci_dev *pdev,
  2761. enum pci_channel_state state)
  2762. {
  2763. pci_ers_result_t status = PCI_ERS_RESULT_RECOVERED;
  2764. struct efx_nic *efx = pci_get_drvdata(pdev);
  2765. if (state == pci_channel_io_perm_failure)
  2766. return PCI_ERS_RESULT_DISCONNECT;
  2767. rtnl_lock();
  2768. if (efx->state != STATE_DISABLED) {
  2769. efx->state = STATE_RECOVERY;
  2770. efx->reset_pending = 0;
  2771. efx_device_detach_sync(efx);
  2772. efx_stop_all(efx);
  2773. efx_disable_interrupts(efx);
  2774. status = PCI_ERS_RESULT_NEED_RESET;
  2775. } else {
  2776. /* If the interface is disabled we don't want to do anything
  2777. * with it.
  2778. */
  2779. status = PCI_ERS_RESULT_RECOVERED;
  2780. }
  2781. rtnl_unlock();
  2782. pci_disable_device(pdev);
  2783. return status;
  2784. }
  2785. /* Fake a successful reset, which will be performed later in efx_io_resume. */
  2786. static pci_ers_result_t efx_io_slot_reset(struct pci_dev *pdev)
  2787. {
  2788. struct efx_nic *efx = pci_get_drvdata(pdev);
  2789. pci_ers_result_t status = PCI_ERS_RESULT_RECOVERED;
  2790. int rc;
  2791. if (pci_enable_device(pdev)) {
  2792. netif_err(efx, hw, efx->net_dev,
  2793. "Cannot re-enable PCI device after reset.\n");
  2794. status = PCI_ERS_RESULT_DISCONNECT;
  2795. }
  2796. rc = pci_cleanup_aer_uncorrect_error_status(pdev);
  2797. if (rc) {
  2798. netif_err(efx, hw, efx->net_dev,
  2799. "pci_cleanup_aer_uncorrect_error_status failed (%d)\n", rc);
  2800. /* Non-fatal error. Continue. */
  2801. }
  2802. return status;
  2803. }
  2804. /* Perform the actual reset and resume I/O operations. */
  2805. static void efx_io_resume(struct pci_dev *pdev)
  2806. {
  2807. struct efx_nic *efx = pci_get_drvdata(pdev);
  2808. int rc;
  2809. rtnl_lock();
  2810. if (efx->state == STATE_DISABLED)
  2811. goto out;
  2812. rc = efx_reset(efx, RESET_TYPE_ALL);
  2813. if (rc) {
  2814. netif_err(efx, hw, efx->net_dev,
  2815. "efx_reset failed after PCI error (%d)\n", rc);
  2816. } else {
  2817. efx->state = STATE_READY;
  2818. netif_dbg(efx, hw, efx->net_dev,
  2819. "Done resetting and resuming IO after PCI error.\n");
  2820. }
  2821. out:
  2822. rtnl_unlock();
  2823. }
  2824. /* For simplicity and reliability, we always require a slot reset and try to
  2825. * reset the hardware when a pci error affecting the device is detected.
  2826. * We leave both the link_reset and mmio_enabled callback unimplemented:
  2827. * with our request for slot reset the mmio_enabled callback will never be
  2828. * called, and the link_reset callback is not used by AER or EEH mechanisms.
  2829. */
  2830. static struct pci_error_handlers efx_err_handlers = {
  2831. .error_detected = efx_io_error_detected,
  2832. .slot_reset = efx_io_slot_reset,
  2833. .resume = efx_io_resume,
  2834. };
  2835. static struct pci_driver efx_pci_driver = {
  2836. .name = KBUILD_MODNAME,
  2837. .id_table = efx_pci_table,
  2838. .probe = efx_pci_probe,
  2839. .remove = efx_pci_remove,
  2840. .driver.pm = &efx_pm_ops,
  2841. .err_handler = &efx_err_handlers,
  2842. #ifdef CONFIG_SFC_SRIOV
  2843. .sriov_configure = efx_pci_sriov_configure,
  2844. #endif
  2845. };
  2846. /**************************************************************************
  2847. *
  2848. * Kernel module interface
  2849. *
  2850. *************************************************************************/
  2851. module_param(interrupt_mode, uint, 0444);
  2852. MODULE_PARM_DESC(interrupt_mode,
  2853. "Interrupt mode (0=>MSIX 1=>MSI 2=>legacy)");
  2854. static int __init efx_init_module(void)
  2855. {
  2856. int rc;
  2857. printk(KERN_INFO "Solarflare NET driver v" EFX_DRIVER_VERSION "\n");
  2858. rc = register_netdevice_notifier(&efx_netdev_notifier);
  2859. if (rc)
  2860. goto err_notifier;
  2861. #ifdef CONFIG_SFC_SRIOV
  2862. rc = efx_init_sriov();
  2863. if (rc)
  2864. goto err_sriov;
  2865. #endif
  2866. reset_workqueue = create_singlethread_workqueue("sfc_reset");
  2867. if (!reset_workqueue) {
  2868. rc = -ENOMEM;
  2869. goto err_reset;
  2870. }
  2871. rc = pci_register_driver(&efx_pci_driver);
  2872. if (rc < 0)
  2873. goto err_pci;
  2874. return 0;
  2875. err_pci:
  2876. destroy_workqueue(reset_workqueue);
  2877. err_reset:
  2878. #ifdef CONFIG_SFC_SRIOV
  2879. efx_fini_sriov();
  2880. err_sriov:
  2881. #endif
  2882. unregister_netdevice_notifier(&efx_netdev_notifier);
  2883. err_notifier:
  2884. return rc;
  2885. }
  2886. static void __exit efx_exit_module(void)
  2887. {
  2888. printk(KERN_INFO "Solarflare NET driver unloading\n");
  2889. pci_unregister_driver(&efx_pci_driver);
  2890. destroy_workqueue(reset_workqueue);
  2891. #ifdef CONFIG_SFC_SRIOV
  2892. efx_fini_sriov();
  2893. #endif
  2894. unregister_netdevice_notifier(&efx_netdev_notifier);
  2895. }
  2896. module_init(efx_init_module);
  2897. module_exit(efx_exit_module);
  2898. MODULE_AUTHOR("Solarflare Communications and "
  2899. "Michael Brown <mbrown@fensystems.co.uk>");
  2900. MODULE_DESCRIPTION("Solarflare network driver");
  2901. MODULE_LICENSE("GPL");
  2902. MODULE_DEVICE_TABLE(pci, efx_pci_table);