efx.c 85 KB

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