efx.c 81 KB

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