efx.c 84 KB

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