efx.c 83 KB

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