efx.c 64 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452
  1. /****************************************************************************
  2. * Driver for Solarflare Solarstorm network controllers and boards
  3. * Copyright 2005-2006 Fen Systems Ltd.
  4. * Copyright 2005-2009 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 "net_driver.h"
  24. #include "efx.h"
  25. #include "mdio_10g.h"
  26. #include "nic.h"
  27. #include "mcdi.h"
  28. #include "workarounds.h"
  29. /**************************************************************************
  30. *
  31. * Type name strings
  32. *
  33. **************************************************************************
  34. */
  35. /* Loopback mode names (see LOOPBACK_MODE()) */
  36. const unsigned int efx_loopback_mode_max = LOOPBACK_MAX;
  37. const char *efx_loopback_mode_names[] = {
  38. [LOOPBACK_NONE] = "NONE",
  39. [LOOPBACK_DATA] = "DATAPATH",
  40. [LOOPBACK_GMAC] = "GMAC",
  41. [LOOPBACK_XGMII] = "XGMII",
  42. [LOOPBACK_XGXS] = "XGXS",
  43. [LOOPBACK_XAUI] = "XAUI",
  44. [LOOPBACK_GMII] = "GMII",
  45. [LOOPBACK_SGMII] = "SGMII",
  46. [LOOPBACK_XGBR] = "XGBR",
  47. [LOOPBACK_XFI] = "XFI",
  48. [LOOPBACK_XAUI_FAR] = "XAUI_FAR",
  49. [LOOPBACK_GMII_FAR] = "GMII_FAR",
  50. [LOOPBACK_SGMII_FAR] = "SGMII_FAR",
  51. [LOOPBACK_XFI_FAR] = "XFI_FAR",
  52. [LOOPBACK_GPHY] = "GPHY",
  53. [LOOPBACK_PHYXS] = "PHYXS",
  54. [LOOPBACK_PCS] = "PCS",
  55. [LOOPBACK_PMAPMD] = "PMA/PMD",
  56. [LOOPBACK_XPORT] = "XPORT",
  57. [LOOPBACK_XGMII_WS] = "XGMII_WS",
  58. [LOOPBACK_XAUI_WS] = "XAUI_WS",
  59. [LOOPBACK_XAUI_WS_FAR] = "XAUI_WS_FAR",
  60. [LOOPBACK_XAUI_WS_NEAR] = "XAUI_WS_NEAR",
  61. [LOOPBACK_GMII_WS] = "GMII_WS",
  62. [LOOPBACK_XFI_WS] = "XFI_WS",
  63. [LOOPBACK_XFI_WS_FAR] = "XFI_WS_FAR",
  64. [LOOPBACK_PHYXS_WS] = "PHYXS_WS",
  65. };
  66. /* Interrupt mode names (see INT_MODE())) */
  67. const unsigned int efx_interrupt_mode_max = EFX_INT_MODE_MAX;
  68. const char *efx_interrupt_mode_names[] = {
  69. [EFX_INT_MODE_MSIX] = "MSI-X",
  70. [EFX_INT_MODE_MSI] = "MSI",
  71. [EFX_INT_MODE_LEGACY] = "legacy",
  72. };
  73. const unsigned int efx_reset_type_max = RESET_TYPE_MAX;
  74. const char *efx_reset_type_names[] = {
  75. [RESET_TYPE_INVISIBLE] = "INVISIBLE",
  76. [RESET_TYPE_ALL] = "ALL",
  77. [RESET_TYPE_WORLD] = "WORLD",
  78. [RESET_TYPE_DISABLE] = "DISABLE",
  79. [RESET_TYPE_TX_WATCHDOG] = "TX_WATCHDOG",
  80. [RESET_TYPE_INT_ERROR] = "INT_ERROR",
  81. [RESET_TYPE_RX_RECOVERY] = "RX_RECOVERY",
  82. [RESET_TYPE_RX_DESC_FETCH] = "RX_DESC_FETCH",
  83. [RESET_TYPE_TX_DESC_FETCH] = "TX_DESC_FETCH",
  84. [RESET_TYPE_TX_SKIP] = "TX_SKIP",
  85. [RESET_TYPE_MC_FAILURE] = "MC_FAILURE",
  86. };
  87. #define EFX_MAX_MTU (9 * 1024)
  88. /* Reset workqueue. If any NIC has a hardware failure then a reset will be
  89. * queued onto this work queue. This is not a per-nic work queue, because
  90. * efx_reset_work() acquires the rtnl lock, so resets are naturally serialised.
  91. */
  92. static struct workqueue_struct *reset_workqueue;
  93. /**************************************************************************
  94. *
  95. * Configurable values
  96. *
  97. *************************************************************************/
  98. /*
  99. * Use separate channels for TX and RX events
  100. *
  101. * Set this to 1 to use separate channels for TX and RX. It allows us
  102. * to control interrupt affinity separately for TX and RX.
  103. *
  104. * This is only used in MSI-X interrupt mode
  105. */
  106. static unsigned int separate_tx_channels;
  107. module_param(separate_tx_channels, uint, 0644);
  108. MODULE_PARM_DESC(separate_tx_channels,
  109. "Use separate channels for TX and RX");
  110. /* This is the weight assigned to each of the (per-channel) virtual
  111. * NAPI devices.
  112. */
  113. static int napi_weight = 64;
  114. /* This is the time (in jiffies) between invocations of the hardware
  115. * monitor, which checks for known hardware bugs and resets the
  116. * hardware and driver as necessary.
  117. */
  118. unsigned int efx_monitor_interval = 1 * HZ;
  119. /* This controls whether or not the driver will initialise devices
  120. * with invalid MAC addresses stored in the EEPROM or flash. If true,
  121. * such devices will be initialised with a random locally-generated
  122. * MAC address. This allows for loading the sfc_mtd driver to
  123. * reprogram the flash, even if the flash contents (including the MAC
  124. * address) have previously been erased.
  125. */
  126. static unsigned int allow_bad_hwaddr;
  127. /* Initial interrupt moderation settings. They can be modified after
  128. * module load with ethtool.
  129. *
  130. * The default for RX should strike a balance between increasing the
  131. * round-trip latency and reducing overhead.
  132. */
  133. static unsigned int rx_irq_mod_usec = 60;
  134. /* Initial interrupt moderation settings. They can be modified after
  135. * module load with ethtool.
  136. *
  137. * This default is chosen to ensure that a 10G link does not go idle
  138. * while a TX queue is stopped after it has become full. A queue is
  139. * restarted when it drops below half full. The time this takes (assuming
  140. * worst case 3 descriptors per packet and 1024 descriptors) is
  141. * 512 / 3 * 1.2 = 205 usec.
  142. */
  143. static unsigned int tx_irq_mod_usec = 150;
  144. /* This is the first interrupt mode to try out of:
  145. * 0 => MSI-X
  146. * 1 => MSI
  147. * 2 => legacy
  148. */
  149. static unsigned int interrupt_mode;
  150. /* This is the requested number of CPUs to use for Receive-Side Scaling (RSS),
  151. * i.e. the number of CPUs among which we may distribute simultaneous
  152. * interrupt handling.
  153. *
  154. * Cards without MSI-X will only target one CPU via legacy or MSI interrupt.
  155. * The default (0) means to assign an interrupt to each package (level II cache)
  156. */
  157. static unsigned int rss_cpus;
  158. module_param(rss_cpus, uint, 0444);
  159. MODULE_PARM_DESC(rss_cpus, "Number of CPUs to use for Receive-Side Scaling");
  160. static int phy_flash_cfg;
  161. module_param(phy_flash_cfg, int, 0644);
  162. MODULE_PARM_DESC(phy_flash_cfg, "Set PHYs into reflash mode initially");
  163. static unsigned irq_adapt_low_thresh = 10000;
  164. module_param(irq_adapt_low_thresh, uint, 0644);
  165. MODULE_PARM_DESC(irq_adapt_low_thresh,
  166. "Threshold score for reducing IRQ moderation");
  167. static unsigned irq_adapt_high_thresh = 20000;
  168. module_param(irq_adapt_high_thresh, uint, 0644);
  169. MODULE_PARM_DESC(irq_adapt_high_thresh,
  170. "Threshold score for increasing IRQ moderation");
  171. /**************************************************************************
  172. *
  173. * Utility functions and prototypes
  174. *
  175. *************************************************************************/
  176. static void efx_remove_channel(struct efx_channel *channel);
  177. static void efx_remove_port(struct efx_nic *efx);
  178. static void efx_fini_napi(struct efx_nic *efx);
  179. static void efx_fini_channels(struct efx_nic *efx);
  180. #define EFX_ASSERT_RESET_SERIALISED(efx) \
  181. do { \
  182. if ((efx->state == STATE_RUNNING) || \
  183. (efx->state == STATE_DISABLED)) \
  184. ASSERT_RTNL(); \
  185. } while (0)
  186. /**************************************************************************
  187. *
  188. * Event queue processing
  189. *
  190. *************************************************************************/
  191. /* Process channel's event queue
  192. *
  193. * This function is responsible for processing the event queue of a
  194. * single channel. The caller must guarantee that this function will
  195. * never be concurrently called more than once on the same channel,
  196. * though different channels may be being processed concurrently.
  197. */
  198. static int efx_process_channel(struct efx_channel *channel, int budget)
  199. {
  200. struct efx_nic *efx = channel->efx;
  201. int spent;
  202. if (unlikely(efx->reset_pending != RESET_TYPE_NONE ||
  203. !channel->enabled))
  204. return 0;
  205. spent = efx_nic_process_eventq(channel, budget);
  206. if (spent == 0)
  207. return 0;
  208. /* Deliver last RX packet. */
  209. if (channel->rx_pkt) {
  210. __efx_rx_packet(channel, channel->rx_pkt,
  211. channel->rx_pkt_csummed);
  212. channel->rx_pkt = NULL;
  213. }
  214. efx_rx_strategy(channel);
  215. efx_fast_push_rx_descriptors(&efx->rx_queue[channel->channel]);
  216. return spent;
  217. }
  218. /* Mark channel as finished processing
  219. *
  220. * Note that since we will not receive further interrupts for this
  221. * channel before we finish processing and call the eventq_read_ack()
  222. * method, there is no need to use the interrupt hold-off timers.
  223. */
  224. static inline void efx_channel_processed(struct efx_channel *channel)
  225. {
  226. /* The interrupt handler for this channel may set work_pending
  227. * as soon as we acknowledge the events we've seen. Make sure
  228. * it's cleared before then. */
  229. channel->work_pending = false;
  230. smp_wmb();
  231. efx_nic_eventq_read_ack(channel);
  232. }
  233. /* NAPI poll handler
  234. *
  235. * NAPI guarantees serialisation of polls of the same device, which
  236. * provides the guarantee required by efx_process_channel().
  237. */
  238. static int efx_poll(struct napi_struct *napi, int budget)
  239. {
  240. struct efx_channel *channel =
  241. container_of(napi, struct efx_channel, napi_str);
  242. int spent;
  243. EFX_TRACE(channel->efx, "channel %d NAPI poll executing on CPU %d\n",
  244. channel->channel, raw_smp_processor_id());
  245. spent = efx_process_channel(channel, budget);
  246. if (spent < budget) {
  247. struct efx_nic *efx = channel->efx;
  248. if (channel->channel < efx->n_rx_channels &&
  249. efx->irq_rx_adaptive &&
  250. unlikely(++channel->irq_count == 1000)) {
  251. if (unlikely(channel->irq_mod_score <
  252. irq_adapt_low_thresh)) {
  253. if (channel->irq_moderation > 1) {
  254. channel->irq_moderation -= 1;
  255. efx->type->push_irq_moderation(channel);
  256. }
  257. } else if (unlikely(channel->irq_mod_score >
  258. irq_adapt_high_thresh)) {
  259. if (channel->irq_moderation <
  260. efx->irq_rx_moderation) {
  261. channel->irq_moderation += 1;
  262. efx->type->push_irq_moderation(channel);
  263. }
  264. }
  265. channel->irq_count = 0;
  266. channel->irq_mod_score = 0;
  267. }
  268. /* There is no race here; although napi_disable() will
  269. * only wait for napi_complete(), this isn't a problem
  270. * since efx_channel_processed() will have no effect if
  271. * interrupts have already been disabled.
  272. */
  273. napi_complete(napi);
  274. efx_channel_processed(channel);
  275. }
  276. return spent;
  277. }
  278. /* Process the eventq of the specified channel immediately on this CPU
  279. *
  280. * Disable hardware generated interrupts, wait for any existing
  281. * processing to finish, then directly poll (and ack ) the eventq.
  282. * Finally reenable NAPI and interrupts.
  283. *
  284. * Since we are touching interrupts the caller should hold the suspend lock
  285. */
  286. void efx_process_channel_now(struct efx_channel *channel)
  287. {
  288. struct efx_nic *efx = channel->efx;
  289. BUG_ON(!channel->enabled);
  290. /* Disable interrupts and wait for ISRs to complete */
  291. efx_nic_disable_interrupts(efx);
  292. if (efx->legacy_irq)
  293. synchronize_irq(efx->legacy_irq);
  294. if (channel->irq)
  295. synchronize_irq(channel->irq);
  296. /* Wait for any NAPI processing to complete */
  297. napi_disable(&channel->napi_str);
  298. /* Poll the channel */
  299. efx_process_channel(channel, EFX_EVQ_SIZE);
  300. /* Ack the eventq. This may cause an interrupt to be generated
  301. * when they are reenabled */
  302. efx_channel_processed(channel);
  303. napi_enable(&channel->napi_str);
  304. efx_nic_enable_interrupts(efx);
  305. }
  306. /* Create event queue
  307. * Event queue memory allocations are done only once. If the channel
  308. * is reset, the memory buffer will be reused; this guards against
  309. * errors during channel reset and also simplifies interrupt handling.
  310. */
  311. static int efx_probe_eventq(struct efx_channel *channel)
  312. {
  313. EFX_LOG(channel->efx, "chan %d create event queue\n", channel->channel);
  314. return efx_nic_probe_eventq(channel);
  315. }
  316. /* Prepare channel's event queue */
  317. static void efx_init_eventq(struct efx_channel *channel)
  318. {
  319. EFX_LOG(channel->efx, "chan %d init event queue\n", channel->channel);
  320. channel->eventq_read_ptr = 0;
  321. efx_nic_init_eventq(channel);
  322. }
  323. static void efx_fini_eventq(struct efx_channel *channel)
  324. {
  325. EFX_LOG(channel->efx, "chan %d fini event queue\n", channel->channel);
  326. efx_nic_fini_eventq(channel);
  327. }
  328. static void efx_remove_eventq(struct efx_channel *channel)
  329. {
  330. EFX_LOG(channel->efx, "chan %d remove event queue\n", channel->channel);
  331. efx_nic_remove_eventq(channel);
  332. }
  333. /**************************************************************************
  334. *
  335. * Channel handling
  336. *
  337. *************************************************************************/
  338. static int efx_probe_channel(struct efx_channel *channel)
  339. {
  340. struct efx_tx_queue *tx_queue;
  341. struct efx_rx_queue *rx_queue;
  342. int rc;
  343. EFX_LOG(channel->efx, "creating channel %d\n", channel->channel);
  344. rc = efx_probe_eventq(channel);
  345. if (rc)
  346. goto fail1;
  347. efx_for_each_channel_tx_queue(tx_queue, channel) {
  348. rc = efx_probe_tx_queue(tx_queue);
  349. if (rc)
  350. goto fail2;
  351. }
  352. efx_for_each_channel_rx_queue(rx_queue, channel) {
  353. rc = efx_probe_rx_queue(rx_queue);
  354. if (rc)
  355. goto fail3;
  356. }
  357. channel->n_rx_frm_trunc = 0;
  358. return 0;
  359. fail3:
  360. efx_for_each_channel_rx_queue(rx_queue, channel)
  361. efx_remove_rx_queue(rx_queue);
  362. fail2:
  363. efx_for_each_channel_tx_queue(tx_queue, channel)
  364. efx_remove_tx_queue(tx_queue);
  365. fail1:
  366. return rc;
  367. }
  368. static void efx_set_channel_names(struct efx_nic *efx)
  369. {
  370. struct efx_channel *channel;
  371. const char *type = "";
  372. int number;
  373. efx_for_each_channel(channel, efx) {
  374. number = channel->channel;
  375. if (efx->n_channels > efx->n_rx_channels) {
  376. if (channel->channel < efx->n_rx_channels) {
  377. type = "-rx";
  378. } else {
  379. type = "-tx";
  380. number -= efx->n_rx_channels;
  381. }
  382. }
  383. snprintf(channel->name, sizeof(channel->name),
  384. "%s%s-%d", efx->name, type, number);
  385. }
  386. }
  387. /* Channels are shutdown and reinitialised whilst the NIC is running
  388. * to propagate configuration changes (mtu, checksum offload), or
  389. * to clear hardware error conditions
  390. */
  391. static void efx_init_channels(struct efx_nic *efx)
  392. {
  393. struct efx_tx_queue *tx_queue;
  394. struct efx_rx_queue *rx_queue;
  395. struct efx_channel *channel;
  396. /* Calculate the rx buffer allocation parameters required to
  397. * support the current MTU, including padding for header
  398. * alignment and overruns.
  399. */
  400. efx->rx_buffer_len = (max(EFX_PAGE_IP_ALIGN, NET_IP_ALIGN) +
  401. EFX_MAX_FRAME_LEN(efx->net_dev->mtu) +
  402. efx->type->rx_buffer_padding);
  403. efx->rx_buffer_order = get_order(efx->rx_buffer_len +
  404. sizeof(struct efx_rx_page_state));
  405. /* Initialise the channels */
  406. efx_for_each_channel(channel, efx) {
  407. EFX_LOG(channel->efx, "init chan %d\n", channel->channel);
  408. efx_init_eventq(channel);
  409. efx_for_each_channel_tx_queue(tx_queue, channel)
  410. efx_init_tx_queue(tx_queue);
  411. /* The rx buffer allocation strategy is MTU dependent */
  412. efx_rx_strategy(channel);
  413. efx_for_each_channel_rx_queue(rx_queue, channel)
  414. efx_init_rx_queue(rx_queue);
  415. WARN_ON(channel->rx_pkt != NULL);
  416. efx_rx_strategy(channel);
  417. }
  418. }
  419. /* This enables event queue processing and packet transmission.
  420. *
  421. * Note that this function is not allowed to fail, since that would
  422. * introduce too much complexity into the suspend/resume path.
  423. */
  424. static void efx_start_channel(struct efx_channel *channel)
  425. {
  426. struct efx_rx_queue *rx_queue;
  427. EFX_LOG(channel->efx, "starting chan %d\n", channel->channel);
  428. /* The interrupt handler for this channel may set work_pending
  429. * as soon as we enable it. Make sure it's cleared before
  430. * then. Similarly, make sure it sees the enabled flag set. */
  431. channel->work_pending = false;
  432. channel->enabled = true;
  433. smp_wmb();
  434. /* Fill the queues before enabling NAPI */
  435. efx_for_each_channel_rx_queue(rx_queue, channel)
  436. efx_fast_push_rx_descriptors(rx_queue);
  437. napi_enable(&channel->napi_str);
  438. }
  439. /* This disables event queue processing and packet transmission.
  440. * This function does not guarantee that all queue processing
  441. * (e.g. RX refill) is complete.
  442. */
  443. static void efx_stop_channel(struct efx_channel *channel)
  444. {
  445. if (!channel->enabled)
  446. return;
  447. EFX_LOG(channel->efx, "stop chan %d\n", channel->channel);
  448. channel->enabled = false;
  449. napi_disable(&channel->napi_str);
  450. }
  451. static void efx_fini_channels(struct efx_nic *efx)
  452. {
  453. struct efx_channel *channel;
  454. struct efx_tx_queue *tx_queue;
  455. struct efx_rx_queue *rx_queue;
  456. int rc;
  457. EFX_ASSERT_RESET_SERIALISED(efx);
  458. BUG_ON(efx->port_enabled);
  459. rc = efx_nic_flush_queues(efx);
  460. if (rc && EFX_WORKAROUND_7803(efx)) {
  461. /* Schedule a reset to recover from the flush failure. The
  462. * descriptor caches reference memory we're about to free,
  463. * but falcon_reconfigure_mac_wrapper() won't reconnect
  464. * the MACs because of the pending reset. */
  465. EFX_ERR(efx, "Resetting to recover from flush failure\n");
  466. efx_schedule_reset(efx, RESET_TYPE_ALL);
  467. } else if (rc) {
  468. EFX_ERR(efx, "failed to flush queues\n");
  469. } else {
  470. EFX_LOG(efx, "successfully flushed all queues\n");
  471. }
  472. efx_for_each_channel(channel, efx) {
  473. EFX_LOG(channel->efx, "shut down chan %d\n", channel->channel);
  474. efx_for_each_channel_rx_queue(rx_queue, channel)
  475. efx_fini_rx_queue(rx_queue);
  476. efx_for_each_channel_tx_queue(tx_queue, channel)
  477. efx_fini_tx_queue(tx_queue);
  478. efx_fini_eventq(channel);
  479. }
  480. }
  481. static void efx_remove_channel(struct efx_channel *channel)
  482. {
  483. struct efx_tx_queue *tx_queue;
  484. struct efx_rx_queue *rx_queue;
  485. EFX_LOG(channel->efx, "destroy chan %d\n", channel->channel);
  486. efx_for_each_channel_rx_queue(rx_queue, channel)
  487. efx_remove_rx_queue(rx_queue);
  488. efx_for_each_channel_tx_queue(tx_queue, channel)
  489. efx_remove_tx_queue(tx_queue);
  490. efx_remove_eventq(channel);
  491. }
  492. void efx_schedule_slow_fill(struct efx_rx_queue *rx_queue)
  493. {
  494. mod_timer(&rx_queue->slow_fill, jiffies + msecs_to_jiffies(100));
  495. }
  496. /**************************************************************************
  497. *
  498. * Port handling
  499. *
  500. **************************************************************************/
  501. /* This ensures that the kernel is kept informed (via
  502. * netif_carrier_on/off) of the link status, and also maintains the
  503. * link status's stop on the port's TX queue.
  504. */
  505. void efx_link_status_changed(struct efx_nic *efx)
  506. {
  507. struct efx_link_state *link_state = &efx->link_state;
  508. /* SFC Bug 5356: A net_dev notifier is registered, so we must ensure
  509. * that no events are triggered between unregister_netdev() and the
  510. * driver unloading. A more general condition is that NETDEV_CHANGE
  511. * can only be generated between NETDEV_UP and NETDEV_DOWN */
  512. if (!netif_running(efx->net_dev))
  513. return;
  514. if (efx->port_inhibited) {
  515. netif_carrier_off(efx->net_dev);
  516. return;
  517. }
  518. if (link_state->up != netif_carrier_ok(efx->net_dev)) {
  519. efx->n_link_state_changes++;
  520. if (link_state->up)
  521. netif_carrier_on(efx->net_dev);
  522. else
  523. netif_carrier_off(efx->net_dev);
  524. }
  525. /* Status message for kernel log */
  526. if (link_state->up) {
  527. EFX_INFO(efx, "link up at %uMbps %s-duplex (MTU %d)%s\n",
  528. link_state->speed, link_state->fd ? "full" : "half",
  529. efx->net_dev->mtu,
  530. (efx->promiscuous ? " [PROMISC]" : ""));
  531. } else {
  532. EFX_INFO(efx, "link down\n");
  533. }
  534. }
  535. void efx_link_set_advertising(struct efx_nic *efx, u32 advertising)
  536. {
  537. efx->link_advertising = advertising;
  538. if (advertising) {
  539. if (advertising & ADVERTISED_Pause)
  540. efx->wanted_fc |= (EFX_FC_TX | EFX_FC_RX);
  541. else
  542. efx->wanted_fc &= ~(EFX_FC_TX | EFX_FC_RX);
  543. if (advertising & ADVERTISED_Asym_Pause)
  544. efx->wanted_fc ^= EFX_FC_TX;
  545. }
  546. }
  547. void efx_link_set_wanted_fc(struct efx_nic *efx, enum efx_fc_type wanted_fc)
  548. {
  549. efx->wanted_fc = wanted_fc;
  550. if (efx->link_advertising) {
  551. if (wanted_fc & EFX_FC_RX)
  552. efx->link_advertising |= (ADVERTISED_Pause |
  553. ADVERTISED_Asym_Pause);
  554. else
  555. efx->link_advertising &= ~(ADVERTISED_Pause |
  556. ADVERTISED_Asym_Pause);
  557. if (wanted_fc & EFX_FC_TX)
  558. efx->link_advertising ^= ADVERTISED_Asym_Pause;
  559. }
  560. }
  561. static void efx_fini_port(struct efx_nic *efx);
  562. /* Push loopback/power/transmit disable settings to the PHY, and reconfigure
  563. * the MAC appropriately. All other PHY configuration changes are pushed
  564. * through phy_op->set_settings(), and pushed asynchronously to the MAC
  565. * through efx_monitor().
  566. *
  567. * Callers must hold the mac_lock
  568. */
  569. int __efx_reconfigure_port(struct efx_nic *efx)
  570. {
  571. enum efx_phy_mode phy_mode;
  572. int rc;
  573. WARN_ON(!mutex_is_locked(&efx->mac_lock));
  574. /* Serialise the promiscuous flag with efx_set_multicast_list. */
  575. if (efx_dev_registered(efx)) {
  576. netif_addr_lock_bh(efx->net_dev);
  577. netif_addr_unlock_bh(efx->net_dev);
  578. }
  579. /* Disable PHY transmit in mac level loopbacks */
  580. phy_mode = efx->phy_mode;
  581. if (LOOPBACK_INTERNAL(efx))
  582. efx->phy_mode |= PHY_MODE_TX_DISABLED;
  583. else
  584. efx->phy_mode &= ~PHY_MODE_TX_DISABLED;
  585. rc = efx->type->reconfigure_port(efx);
  586. if (rc)
  587. efx->phy_mode = phy_mode;
  588. return rc;
  589. }
  590. /* Reinitialise the MAC to pick up new PHY settings, even if the port is
  591. * disabled. */
  592. int efx_reconfigure_port(struct efx_nic *efx)
  593. {
  594. int rc;
  595. EFX_ASSERT_RESET_SERIALISED(efx);
  596. mutex_lock(&efx->mac_lock);
  597. rc = __efx_reconfigure_port(efx);
  598. mutex_unlock(&efx->mac_lock);
  599. return rc;
  600. }
  601. /* Asynchronous work item for changing MAC promiscuity and multicast
  602. * hash. Avoid a drain/rx_ingress enable by reconfiguring the current
  603. * MAC directly. */
  604. static void efx_mac_work(struct work_struct *data)
  605. {
  606. struct efx_nic *efx = container_of(data, struct efx_nic, mac_work);
  607. mutex_lock(&efx->mac_lock);
  608. if (efx->port_enabled) {
  609. efx->type->push_multicast_hash(efx);
  610. efx->mac_op->reconfigure(efx);
  611. }
  612. mutex_unlock(&efx->mac_lock);
  613. }
  614. static int efx_probe_port(struct efx_nic *efx)
  615. {
  616. int rc;
  617. EFX_LOG(efx, "create port\n");
  618. if (phy_flash_cfg)
  619. efx->phy_mode = PHY_MODE_SPECIAL;
  620. /* Connect up MAC/PHY operations table */
  621. rc = efx->type->probe_port(efx);
  622. if (rc)
  623. goto err;
  624. /* Sanity check MAC address */
  625. if (is_valid_ether_addr(efx->mac_address)) {
  626. memcpy(efx->net_dev->dev_addr, efx->mac_address, ETH_ALEN);
  627. } else {
  628. EFX_ERR(efx, "invalid MAC address %pM\n",
  629. efx->mac_address);
  630. if (!allow_bad_hwaddr) {
  631. rc = -EINVAL;
  632. goto err;
  633. }
  634. random_ether_addr(efx->net_dev->dev_addr);
  635. EFX_INFO(efx, "using locally-generated MAC %pM\n",
  636. efx->net_dev->dev_addr);
  637. }
  638. return 0;
  639. err:
  640. efx_remove_port(efx);
  641. return rc;
  642. }
  643. static int efx_init_port(struct efx_nic *efx)
  644. {
  645. int rc;
  646. EFX_LOG(efx, "init port\n");
  647. mutex_lock(&efx->mac_lock);
  648. rc = efx->phy_op->init(efx);
  649. if (rc)
  650. goto fail1;
  651. efx->port_initialized = true;
  652. /* Reconfigure the MAC before creating dma queues (required for
  653. * Falcon/A1 where RX_INGR_EN/TX_DRAIN_EN isn't supported) */
  654. efx->mac_op->reconfigure(efx);
  655. /* Ensure the PHY advertises the correct flow control settings */
  656. rc = efx->phy_op->reconfigure(efx);
  657. if (rc)
  658. goto fail2;
  659. mutex_unlock(&efx->mac_lock);
  660. return 0;
  661. fail2:
  662. efx->phy_op->fini(efx);
  663. fail1:
  664. mutex_unlock(&efx->mac_lock);
  665. return rc;
  666. }
  667. static void efx_start_port(struct efx_nic *efx)
  668. {
  669. EFX_LOG(efx, "start port\n");
  670. BUG_ON(efx->port_enabled);
  671. mutex_lock(&efx->mac_lock);
  672. efx->port_enabled = true;
  673. /* efx_mac_work() might have been scheduled after efx_stop_port(),
  674. * and then cancelled by efx_flush_all() */
  675. efx->type->push_multicast_hash(efx);
  676. efx->mac_op->reconfigure(efx);
  677. mutex_unlock(&efx->mac_lock);
  678. }
  679. /* Prevent efx_mac_work() and efx_monitor() from working */
  680. static void efx_stop_port(struct efx_nic *efx)
  681. {
  682. EFX_LOG(efx, "stop port\n");
  683. mutex_lock(&efx->mac_lock);
  684. efx->port_enabled = false;
  685. mutex_unlock(&efx->mac_lock);
  686. /* Serialise against efx_set_multicast_list() */
  687. if (efx_dev_registered(efx)) {
  688. netif_addr_lock_bh(efx->net_dev);
  689. netif_addr_unlock_bh(efx->net_dev);
  690. }
  691. }
  692. static void efx_fini_port(struct efx_nic *efx)
  693. {
  694. EFX_LOG(efx, "shut down port\n");
  695. if (!efx->port_initialized)
  696. return;
  697. efx->phy_op->fini(efx);
  698. efx->port_initialized = false;
  699. efx->link_state.up = false;
  700. efx_link_status_changed(efx);
  701. }
  702. static void efx_remove_port(struct efx_nic *efx)
  703. {
  704. EFX_LOG(efx, "destroying port\n");
  705. efx->type->remove_port(efx);
  706. }
  707. /**************************************************************************
  708. *
  709. * NIC handling
  710. *
  711. **************************************************************************/
  712. /* This configures the PCI device to enable I/O and DMA. */
  713. static int efx_init_io(struct efx_nic *efx)
  714. {
  715. struct pci_dev *pci_dev = efx->pci_dev;
  716. dma_addr_t dma_mask = efx->type->max_dma_mask;
  717. int rc;
  718. EFX_LOG(efx, "initialising I/O\n");
  719. rc = pci_enable_device(pci_dev);
  720. if (rc) {
  721. EFX_ERR(efx, "failed to enable PCI device\n");
  722. goto fail1;
  723. }
  724. pci_set_master(pci_dev);
  725. /* Set the PCI DMA mask. Try all possibilities from our
  726. * genuine mask down to 32 bits, because some architectures
  727. * (e.g. x86_64 with iommu_sac_force set) will allow 40 bit
  728. * masks event though they reject 46 bit masks.
  729. */
  730. while (dma_mask > 0x7fffffffUL) {
  731. if (pci_dma_supported(pci_dev, dma_mask) &&
  732. ((rc = pci_set_dma_mask(pci_dev, dma_mask)) == 0))
  733. break;
  734. dma_mask >>= 1;
  735. }
  736. if (rc) {
  737. EFX_ERR(efx, "could not find a suitable DMA mask\n");
  738. goto fail2;
  739. }
  740. EFX_LOG(efx, "using DMA mask %llx\n", (unsigned long long) dma_mask);
  741. rc = pci_set_consistent_dma_mask(pci_dev, dma_mask);
  742. if (rc) {
  743. /* pci_set_consistent_dma_mask() is not *allowed* to
  744. * fail with a mask that pci_set_dma_mask() accepted,
  745. * but just in case...
  746. */
  747. EFX_ERR(efx, "failed to set consistent DMA mask\n");
  748. goto fail2;
  749. }
  750. efx->membase_phys = pci_resource_start(efx->pci_dev, EFX_MEM_BAR);
  751. rc = pci_request_region(pci_dev, EFX_MEM_BAR, "sfc");
  752. if (rc) {
  753. EFX_ERR(efx, "request for memory BAR failed\n");
  754. rc = -EIO;
  755. goto fail3;
  756. }
  757. efx->membase = ioremap_nocache(efx->membase_phys,
  758. efx->type->mem_map_size);
  759. if (!efx->membase) {
  760. EFX_ERR(efx, "could not map memory BAR at %llx+%x\n",
  761. (unsigned long long)efx->membase_phys,
  762. efx->type->mem_map_size);
  763. rc = -ENOMEM;
  764. goto fail4;
  765. }
  766. EFX_LOG(efx, "memory BAR at %llx+%x (virtual %p)\n",
  767. (unsigned long long)efx->membase_phys,
  768. efx->type->mem_map_size, efx->membase);
  769. return 0;
  770. fail4:
  771. pci_release_region(efx->pci_dev, EFX_MEM_BAR);
  772. fail3:
  773. efx->membase_phys = 0;
  774. fail2:
  775. pci_disable_device(efx->pci_dev);
  776. fail1:
  777. return rc;
  778. }
  779. static void efx_fini_io(struct efx_nic *efx)
  780. {
  781. EFX_LOG(efx, "shutting down I/O\n");
  782. if (efx->membase) {
  783. iounmap(efx->membase);
  784. efx->membase = NULL;
  785. }
  786. if (efx->membase_phys) {
  787. pci_release_region(efx->pci_dev, EFX_MEM_BAR);
  788. efx->membase_phys = 0;
  789. }
  790. pci_disable_device(efx->pci_dev);
  791. }
  792. /* Get number of channels wanted. Each channel will have its own IRQ,
  793. * 1 RX queue and/or 2 TX queues. */
  794. static int efx_wanted_channels(void)
  795. {
  796. cpumask_var_t core_mask;
  797. int count;
  798. int cpu;
  799. if (unlikely(!zalloc_cpumask_var(&core_mask, GFP_KERNEL))) {
  800. printk(KERN_WARNING
  801. "sfc: RSS disabled due to allocation failure\n");
  802. return 1;
  803. }
  804. count = 0;
  805. for_each_online_cpu(cpu) {
  806. if (!cpumask_test_cpu(cpu, core_mask)) {
  807. ++count;
  808. cpumask_or(core_mask, core_mask,
  809. topology_core_cpumask(cpu));
  810. }
  811. }
  812. free_cpumask_var(core_mask);
  813. return count;
  814. }
  815. /* Probe the number and type of interrupts we are able to obtain, and
  816. * the resulting numbers of channels and RX queues.
  817. */
  818. static void efx_probe_interrupts(struct efx_nic *efx)
  819. {
  820. int max_channels =
  821. min_t(int, efx->type->phys_addr_channels, EFX_MAX_CHANNELS);
  822. int rc, i;
  823. if (efx->interrupt_mode == EFX_INT_MODE_MSIX) {
  824. struct msix_entry xentries[EFX_MAX_CHANNELS];
  825. int n_channels;
  826. n_channels = efx_wanted_channels();
  827. if (separate_tx_channels)
  828. n_channels *= 2;
  829. n_channels = min(n_channels, max_channels);
  830. for (i = 0; i < n_channels; i++)
  831. xentries[i].entry = i;
  832. rc = pci_enable_msix(efx->pci_dev, xentries, n_channels);
  833. if (rc > 0) {
  834. EFX_ERR(efx, "WARNING: Insufficient MSI-X vectors"
  835. " available (%d < %d).\n", rc, n_channels);
  836. EFX_ERR(efx, "WARNING: Performance may be reduced.\n");
  837. EFX_BUG_ON_PARANOID(rc >= n_channels);
  838. n_channels = rc;
  839. rc = pci_enable_msix(efx->pci_dev, xentries,
  840. n_channels);
  841. }
  842. if (rc == 0) {
  843. efx->n_channels = n_channels;
  844. if (separate_tx_channels) {
  845. efx->n_tx_channels =
  846. max(efx->n_channels / 2, 1U);
  847. efx->n_rx_channels =
  848. max(efx->n_channels -
  849. efx->n_tx_channels, 1U);
  850. } else {
  851. efx->n_tx_channels = efx->n_channels;
  852. efx->n_rx_channels = efx->n_channels;
  853. }
  854. for (i = 0; i < n_channels; i++)
  855. efx->channel[i].irq = xentries[i].vector;
  856. } else {
  857. /* Fall back to single channel MSI */
  858. efx->interrupt_mode = EFX_INT_MODE_MSI;
  859. EFX_ERR(efx, "could not enable MSI-X\n");
  860. }
  861. }
  862. /* Try single interrupt MSI */
  863. if (efx->interrupt_mode == EFX_INT_MODE_MSI) {
  864. efx->n_channels = 1;
  865. efx->n_rx_channels = 1;
  866. efx->n_tx_channels = 1;
  867. rc = pci_enable_msi(efx->pci_dev);
  868. if (rc == 0) {
  869. efx->channel[0].irq = efx->pci_dev->irq;
  870. } else {
  871. EFX_ERR(efx, "could not enable MSI\n");
  872. efx->interrupt_mode = EFX_INT_MODE_LEGACY;
  873. }
  874. }
  875. /* Assume legacy interrupts */
  876. if (efx->interrupt_mode == EFX_INT_MODE_LEGACY) {
  877. efx->n_channels = 1 + (separate_tx_channels ? 1 : 0);
  878. efx->n_rx_channels = 1;
  879. efx->n_tx_channels = 1;
  880. efx->legacy_irq = efx->pci_dev->irq;
  881. }
  882. }
  883. static void efx_remove_interrupts(struct efx_nic *efx)
  884. {
  885. struct efx_channel *channel;
  886. /* Remove MSI/MSI-X interrupts */
  887. efx_for_each_channel(channel, efx)
  888. channel->irq = 0;
  889. pci_disable_msi(efx->pci_dev);
  890. pci_disable_msix(efx->pci_dev);
  891. /* Remove legacy interrupt */
  892. efx->legacy_irq = 0;
  893. }
  894. static void efx_set_channels(struct efx_nic *efx)
  895. {
  896. struct efx_channel *channel;
  897. struct efx_tx_queue *tx_queue;
  898. struct efx_rx_queue *rx_queue;
  899. unsigned tx_channel_offset =
  900. separate_tx_channels ? efx->n_channels - efx->n_tx_channels : 0;
  901. efx_for_each_channel(channel, efx) {
  902. if (channel->channel - tx_channel_offset < efx->n_tx_channels) {
  903. channel->tx_queue = &efx->tx_queue[
  904. (channel->channel - tx_channel_offset) *
  905. EFX_TXQ_TYPES];
  906. efx_for_each_channel_tx_queue(tx_queue, channel)
  907. tx_queue->channel = channel;
  908. }
  909. }
  910. efx_for_each_rx_queue(rx_queue, efx)
  911. rx_queue->channel = &efx->channel[rx_queue->queue];
  912. }
  913. static int efx_probe_nic(struct efx_nic *efx)
  914. {
  915. int rc;
  916. EFX_LOG(efx, "creating NIC\n");
  917. /* Carry out hardware-type specific initialisation */
  918. rc = efx->type->probe(efx);
  919. if (rc)
  920. return rc;
  921. /* Determine the number of channels and queues by trying to hook
  922. * in MSI-X interrupts. */
  923. efx_probe_interrupts(efx);
  924. efx_set_channels(efx);
  925. efx->net_dev->real_num_tx_queues = efx->n_tx_channels;
  926. /* Initialise the interrupt moderation settings */
  927. efx_init_irq_moderation(efx, tx_irq_mod_usec, rx_irq_mod_usec, true);
  928. return 0;
  929. }
  930. static void efx_remove_nic(struct efx_nic *efx)
  931. {
  932. EFX_LOG(efx, "destroying NIC\n");
  933. efx_remove_interrupts(efx);
  934. efx->type->remove(efx);
  935. }
  936. /**************************************************************************
  937. *
  938. * NIC startup/shutdown
  939. *
  940. *************************************************************************/
  941. static int efx_probe_all(struct efx_nic *efx)
  942. {
  943. struct efx_channel *channel;
  944. int rc;
  945. /* Create NIC */
  946. rc = efx_probe_nic(efx);
  947. if (rc) {
  948. EFX_ERR(efx, "failed to create NIC\n");
  949. goto fail1;
  950. }
  951. /* Create port */
  952. rc = efx_probe_port(efx);
  953. if (rc) {
  954. EFX_ERR(efx, "failed to create port\n");
  955. goto fail2;
  956. }
  957. /* Create channels */
  958. efx_for_each_channel(channel, efx) {
  959. rc = efx_probe_channel(channel);
  960. if (rc) {
  961. EFX_ERR(efx, "failed to create channel %d\n",
  962. channel->channel);
  963. goto fail3;
  964. }
  965. }
  966. efx_set_channel_names(efx);
  967. return 0;
  968. fail3:
  969. efx_for_each_channel(channel, efx)
  970. efx_remove_channel(channel);
  971. efx_remove_port(efx);
  972. fail2:
  973. efx_remove_nic(efx);
  974. fail1:
  975. return rc;
  976. }
  977. /* Called after previous invocation(s) of efx_stop_all, restarts the
  978. * port, kernel transmit queue, NAPI processing and hardware interrupts,
  979. * and ensures that the port is scheduled to be reconfigured.
  980. * This function is safe to call multiple times when the NIC is in any
  981. * state. */
  982. static void efx_start_all(struct efx_nic *efx)
  983. {
  984. struct efx_channel *channel;
  985. EFX_ASSERT_RESET_SERIALISED(efx);
  986. /* Check that it is appropriate to restart the interface. All
  987. * of these flags are safe to read under just the rtnl lock */
  988. if (efx->port_enabled)
  989. return;
  990. if ((efx->state != STATE_RUNNING) && (efx->state != STATE_INIT))
  991. return;
  992. if (efx_dev_registered(efx) && !netif_running(efx->net_dev))
  993. return;
  994. /* Mark the port as enabled so port reconfigurations can start, then
  995. * restart the transmit interface early so the watchdog timer stops */
  996. efx_start_port(efx);
  997. efx_for_each_channel(channel, efx) {
  998. if (efx_dev_registered(efx))
  999. efx_wake_queue(channel);
  1000. efx_start_channel(channel);
  1001. }
  1002. efx_nic_enable_interrupts(efx);
  1003. /* Switch to event based MCDI completions after enabling interrupts.
  1004. * If a reset has been scheduled, then we need to stay in polled mode.
  1005. * Rather than serialising efx_mcdi_mode_event() [which sleeps] and
  1006. * reset_pending [modified from an atomic context], we instead guarantee
  1007. * that efx_mcdi_mode_poll() isn't reverted erroneously */
  1008. efx_mcdi_mode_event(efx);
  1009. if (efx->reset_pending != RESET_TYPE_NONE)
  1010. efx_mcdi_mode_poll(efx);
  1011. /* Start the hardware monitor if there is one. Otherwise (we're link
  1012. * event driven), we have to poll the PHY because after an event queue
  1013. * flush, we could have a missed a link state change */
  1014. if (efx->type->monitor != NULL) {
  1015. queue_delayed_work(efx->workqueue, &efx->monitor_work,
  1016. efx_monitor_interval);
  1017. } else {
  1018. mutex_lock(&efx->mac_lock);
  1019. if (efx->phy_op->poll(efx))
  1020. efx_link_status_changed(efx);
  1021. mutex_unlock(&efx->mac_lock);
  1022. }
  1023. efx->type->start_stats(efx);
  1024. }
  1025. /* Flush all delayed work. Should only be called when no more delayed work
  1026. * will be scheduled. This doesn't flush pending online resets (efx_reset),
  1027. * since we're holding the rtnl_lock at this point. */
  1028. static void efx_flush_all(struct efx_nic *efx)
  1029. {
  1030. /* Make sure the hardware monitor is stopped */
  1031. cancel_delayed_work_sync(&efx->monitor_work);
  1032. /* Stop scheduled port reconfigurations */
  1033. cancel_work_sync(&efx->mac_work);
  1034. }
  1035. /* Quiesce hardware and software without bringing the link down.
  1036. * Safe to call multiple times, when the nic and interface is in any
  1037. * state. The caller is guaranteed to subsequently be in a position
  1038. * to modify any hardware and software state they see fit without
  1039. * taking locks. */
  1040. static void efx_stop_all(struct efx_nic *efx)
  1041. {
  1042. struct efx_channel *channel;
  1043. EFX_ASSERT_RESET_SERIALISED(efx);
  1044. /* port_enabled can be read safely under the rtnl lock */
  1045. if (!efx->port_enabled)
  1046. return;
  1047. efx->type->stop_stats(efx);
  1048. /* Switch to MCDI polling on Siena before disabling interrupts */
  1049. efx_mcdi_mode_poll(efx);
  1050. /* Disable interrupts and wait for ISR to complete */
  1051. efx_nic_disable_interrupts(efx);
  1052. if (efx->legacy_irq)
  1053. synchronize_irq(efx->legacy_irq);
  1054. efx_for_each_channel(channel, efx) {
  1055. if (channel->irq)
  1056. synchronize_irq(channel->irq);
  1057. }
  1058. /* Stop all NAPI processing and synchronous rx refills */
  1059. efx_for_each_channel(channel, efx)
  1060. efx_stop_channel(channel);
  1061. /* Stop all asynchronous port reconfigurations. Since all
  1062. * event processing has already been stopped, there is no
  1063. * window to loose phy events */
  1064. efx_stop_port(efx);
  1065. /* Flush efx_mac_work(), refill_workqueue, monitor_work */
  1066. efx_flush_all(efx);
  1067. /* Stop the kernel transmit interface late, so the watchdog
  1068. * timer isn't ticking over the flush */
  1069. if (efx_dev_registered(efx)) {
  1070. struct efx_channel *channel;
  1071. efx_for_each_channel(channel, efx)
  1072. efx_stop_queue(channel);
  1073. netif_tx_lock_bh(efx->net_dev);
  1074. netif_tx_unlock_bh(efx->net_dev);
  1075. }
  1076. }
  1077. static void efx_remove_all(struct efx_nic *efx)
  1078. {
  1079. struct efx_channel *channel;
  1080. efx_for_each_channel(channel, efx)
  1081. efx_remove_channel(channel);
  1082. efx_remove_port(efx);
  1083. efx_remove_nic(efx);
  1084. }
  1085. /**************************************************************************
  1086. *
  1087. * Interrupt moderation
  1088. *
  1089. **************************************************************************/
  1090. static unsigned irq_mod_ticks(int usecs, int resolution)
  1091. {
  1092. if (usecs <= 0)
  1093. return 0; /* cannot receive interrupts ahead of time :-) */
  1094. if (usecs < resolution)
  1095. return 1; /* never round down to 0 */
  1096. return usecs / resolution;
  1097. }
  1098. /* Set interrupt moderation parameters */
  1099. void efx_init_irq_moderation(struct efx_nic *efx, int tx_usecs, int rx_usecs,
  1100. bool rx_adaptive)
  1101. {
  1102. struct efx_tx_queue *tx_queue;
  1103. struct efx_rx_queue *rx_queue;
  1104. unsigned tx_ticks = irq_mod_ticks(tx_usecs, EFX_IRQ_MOD_RESOLUTION);
  1105. unsigned rx_ticks = irq_mod_ticks(rx_usecs, EFX_IRQ_MOD_RESOLUTION);
  1106. EFX_ASSERT_RESET_SERIALISED(efx);
  1107. efx_for_each_tx_queue(tx_queue, efx)
  1108. tx_queue->channel->irq_moderation = tx_ticks;
  1109. efx->irq_rx_adaptive = rx_adaptive;
  1110. efx->irq_rx_moderation = rx_ticks;
  1111. efx_for_each_rx_queue(rx_queue, efx)
  1112. rx_queue->channel->irq_moderation = rx_ticks;
  1113. }
  1114. /**************************************************************************
  1115. *
  1116. * Hardware monitor
  1117. *
  1118. **************************************************************************/
  1119. /* Run periodically off the general workqueue. Serialised against
  1120. * efx_reconfigure_port via the mac_lock */
  1121. static void efx_monitor(struct work_struct *data)
  1122. {
  1123. struct efx_nic *efx = container_of(data, struct efx_nic,
  1124. monitor_work.work);
  1125. EFX_TRACE(efx, "hardware monitor executing on CPU %d\n",
  1126. raw_smp_processor_id());
  1127. BUG_ON(efx->type->monitor == NULL);
  1128. /* If the mac_lock is already held then it is likely a port
  1129. * reconfiguration is already in place, which will likely do
  1130. * most of the work of check_hw() anyway. */
  1131. if (!mutex_trylock(&efx->mac_lock))
  1132. goto out_requeue;
  1133. if (!efx->port_enabled)
  1134. goto out_unlock;
  1135. efx->type->monitor(efx);
  1136. out_unlock:
  1137. mutex_unlock(&efx->mac_lock);
  1138. out_requeue:
  1139. queue_delayed_work(efx->workqueue, &efx->monitor_work,
  1140. efx_monitor_interval);
  1141. }
  1142. /**************************************************************************
  1143. *
  1144. * ioctls
  1145. *
  1146. *************************************************************************/
  1147. /* Net device ioctl
  1148. * Context: process, rtnl_lock() held.
  1149. */
  1150. static int efx_ioctl(struct net_device *net_dev, struct ifreq *ifr, int cmd)
  1151. {
  1152. struct efx_nic *efx = netdev_priv(net_dev);
  1153. struct mii_ioctl_data *data = if_mii(ifr);
  1154. EFX_ASSERT_RESET_SERIALISED(efx);
  1155. /* Convert phy_id from older PRTAD/DEVAD format */
  1156. if ((cmd == SIOCGMIIREG || cmd == SIOCSMIIREG) &&
  1157. (data->phy_id & 0xfc00) == 0x0400)
  1158. data->phy_id ^= MDIO_PHY_ID_C45 | 0x0400;
  1159. return mdio_mii_ioctl(&efx->mdio, data, cmd);
  1160. }
  1161. /**************************************************************************
  1162. *
  1163. * NAPI interface
  1164. *
  1165. **************************************************************************/
  1166. static int efx_init_napi(struct efx_nic *efx)
  1167. {
  1168. struct efx_channel *channel;
  1169. efx_for_each_channel(channel, efx) {
  1170. channel->napi_dev = efx->net_dev;
  1171. netif_napi_add(channel->napi_dev, &channel->napi_str,
  1172. efx_poll, napi_weight);
  1173. }
  1174. return 0;
  1175. }
  1176. static void efx_fini_napi(struct efx_nic *efx)
  1177. {
  1178. struct efx_channel *channel;
  1179. efx_for_each_channel(channel, efx) {
  1180. if (channel->napi_dev)
  1181. netif_napi_del(&channel->napi_str);
  1182. channel->napi_dev = NULL;
  1183. }
  1184. }
  1185. /**************************************************************************
  1186. *
  1187. * Kernel netpoll interface
  1188. *
  1189. *************************************************************************/
  1190. #ifdef CONFIG_NET_POLL_CONTROLLER
  1191. /* Although in the common case interrupts will be disabled, this is not
  1192. * guaranteed. However, all our work happens inside the NAPI callback,
  1193. * so no locking is required.
  1194. */
  1195. static void efx_netpoll(struct net_device *net_dev)
  1196. {
  1197. struct efx_nic *efx = netdev_priv(net_dev);
  1198. struct efx_channel *channel;
  1199. efx_for_each_channel(channel, efx)
  1200. efx_schedule_channel(channel);
  1201. }
  1202. #endif
  1203. /**************************************************************************
  1204. *
  1205. * Kernel net device interface
  1206. *
  1207. *************************************************************************/
  1208. /* Context: process, rtnl_lock() held. */
  1209. static int efx_net_open(struct net_device *net_dev)
  1210. {
  1211. struct efx_nic *efx = netdev_priv(net_dev);
  1212. EFX_ASSERT_RESET_SERIALISED(efx);
  1213. EFX_LOG(efx, "opening device %s on CPU %d\n", net_dev->name,
  1214. raw_smp_processor_id());
  1215. if (efx->state == STATE_DISABLED)
  1216. return -EIO;
  1217. if (efx->phy_mode & PHY_MODE_SPECIAL)
  1218. return -EBUSY;
  1219. if (efx_mcdi_poll_reboot(efx) && efx_reset(efx, RESET_TYPE_ALL))
  1220. return -EIO;
  1221. /* Notify the kernel of the link state polled during driver load,
  1222. * before the monitor starts running */
  1223. efx_link_status_changed(efx);
  1224. efx_start_all(efx);
  1225. return 0;
  1226. }
  1227. /* Context: process, rtnl_lock() held.
  1228. * Note that the kernel will ignore our return code; this method
  1229. * should really be a void.
  1230. */
  1231. static int efx_net_stop(struct net_device *net_dev)
  1232. {
  1233. struct efx_nic *efx = netdev_priv(net_dev);
  1234. EFX_LOG(efx, "closing %s on CPU %d\n", net_dev->name,
  1235. raw_smp_processor_id());
  1236. if (efx->state != STATE_DISABLED) {
  1237. /* Stop the device and flush all the channels */
  1238. efx_stop_all(efx);
  1239. efx_fini_channels(efx);
  1240. efx_init_channels(efx);
  1241. }
  1242. return 0;
  1243. }
  1244. /* Context: process, dev_base_lock or RTNL held, non-blocking. */
  1245. static struct rtnl_link_stats64 *efx_net_stats(struct net_device *net_dev)
  1246. {
  1247. struct efx_nic *efx = netdev_priv(net_dev);
  1248. struct efx_mac_stats *mac_stats = &efx->mac_stats;
  1249. struct rtnl_link_stats64 *stats = &net_dev->stats64;
  1250. spin_lock_bh(&efx->stats_lock);
  1251. efx->type->update_stats(efx);
  1252. spin_unlock_bh(&efx->stats_lock);
  1253. stats->rx_packets = mac_stats->rx_packets;
  1254. stats->tx_packets = mac_stats->tx_packets;
  1255. stats->rx_bytes = mac_stats->rx_bytes;
  1256. stats->tx_bytes = mac_stats->tx_bytes;
  1257. stats->multicast = mac_stats->rx_multicast;
  1258. stats->collisions = mac_stats->tx_collision;
  1259. stats->rx_length_errors = (mac_stats->rx_gtjumbo +
  1260. mac_stats->rx_length_error);
  1261. stats->rx_over_errors = efx->n_rx_nodesc_drop_cnt;
  1262. stats->rx_crc_errors = mac_stats->rx_bad;
  1263. stats->rx_frame_errors = mac_stats->rx_align_error;
  1264. stats->rx_fifo_errors = mac_stats->rx_overflow;
  1265. stats->rx_missed_errors = mac_stats->rx_missed;
  1266. stats->tx_window_errors = mac_stats->tx_late_collision;
  1267. stats->rx_errors = (stats->rx_length_errors +
  1268. stats->rx_crc_errors +
  1269. stats->rx_frame_errors +
  1270. mac_stats->rx_symbol_error);
  1271. stats->tx_errors = (stats->tx_window_errors +
  1272. mac_stats->tx_bad);
  1273. return stats;
  1274. }
  1275. /* Context: netif_tx_lock held, BHs disabled. */
  1276. static void efx_watchdog(struct net_device *net_dev)
  1277. {
  1278. struct efx_nic *efx = netdev_priv(net_dev);
  1279. EFX_ERR(efx, "TX stuck with port_enabled=%d: resetting channels\n",
  1280. efx->port_enabled);
  1281. efx_schedule_reset(efx, RESET_TYPE_TX_WATCHDOG);
  1282. }
  1283. /* Context: process, rtnl_lock() held. */
  1284. static int efx_change_mtu(struct net_device *net_dev, int new_mtu)
  1285. {
  1286. struct efx_nic *efx = netdev_priv(net_dev);
  1287. int rc = 0;
  1288. EFX_ASSERT_RESET_SERIALISED(efx);
  1289. if (new_mtu > EFX_MAX_MTU)
  1290. return -EINVAL;
  1291. efx_stop_all(efx);
  1292. EFX_LOG(efx, "changing MTU to %d\n", new_mtu);
  1293. efx_fini_channels(efx);
  1294. mutex_lock(&efx->mac_lock);
  1295. /* Reconfigure the MAC before enabling the dma queues so that
  1296. * the RX buffers don't overflow */
  1297. net_dev->mtu = new_mtu;
  1298. efx->mac_op->reconfigure(efx);
  1299. mutex_unlock(&efx->mac_lock);
  1300. efx_init_channels(efx);
  1301. efx_start_all(efx);
  1302. return rc;
  1303. }
  1304. static int efx_set_mac_address(struct net_device *net_dev, void *data)
  1305. {
  1306. struct efx_nic *efx = netdev_priv(net_dev);
  1307. struct sockaddr *addr = data;
  1308. char *new_addr = addr->sa_data;
  1309. EFX_ASSERT_RESET_SERIALISED(efx);
  1310. if (!is_valid_ether_addr(new_addr)) {
  1311. EFX_ERR(efx, "invalid ethernet MAC address requested: %pM\n",
  1312. new_addr);
  1313. return -EINVAL;
  1314. }
  1315. memcpy(net_dev->dev_addr, new_addr, net_dev->addr_len);
  1316. /* Reconfigure the MAC */
  1317. mutex_lock(&efx->mac_lock);
  1318. efx->mac_op->reconfigure(efx);
  1319. mutex_unlock(&efx->mac_lock);
  1320. return 0;
  1321. }
  1322. /* Context: netif_addr_lock held, BHs disabled. */
  1323. static void efx_set_multicast_list(struct net_device *net_dev)
  1324. {
  1325. struct efx_nic *efx = netdev_priv(net_dev);
  1326. struct netdev_hw_addr *ha;
  1327. union efx_multicast_hash *mc_hash = &efx->multicast_hash;
  1328. u32 crc;
  1329. int bit;
  1330. efx->promiscuous = !!(net_dev->flags & IFF_PROMISC);
  1331. /* Build multicast hash table */
  1332. if (efx->promiscuous || (net_dev->flags & IFF_ALLMULTI)) {
  1333. memset(mc_hash, 0xff, sizeof(*mc_hash));
  1334. } else {
  1335. memset(mc_hash, 0x00, sizeof(*mc_hash));
  1336. netdev_for_each_mc_addr(ha, net_dev) {
  1337. crc = ether_crc_le(ETH_ALEN, ha->addr);
  1338. bit = crc & (EFX_MCAST_HASH_ENTRIES - 1);
  1339. set_bit_le(bit, mc_hash->byte);
  1340. }
  1341. /* Broadcast packets go through the multicast hash filter.
  1342. * ether_crc_le() of the broadcast address is 0xbe2612ff
  1343. * so we always add bit 0xff to the mask.
  1344. */
  1345. set_bit_le(0xff, mc_hash->byte);
  1346. }
  1347. if (efx->port_enabled)
  1348. queue_work(efx->workqueue, &efx->mac_work);
  1349. /* Otherwise efx_start_port() will do this */
  1350. }
  1351. static const struct net_device_ops efx_netdev_ops = {
  1352. .ndo_open = efx_net_open,
  1353. .ndo_stop = efx_net_stop,
  1354. .ndo_get_stats64 = efx_net_stats,
  1355. .ndo_tx_timeout = efx_watchdog,
  1356. .ndo_start_xmit = efx_hard_start_xmit,
  1357. .ndo_validate_addr = eth_validate_addr,
  1358. .ndo_do_ioctl = efx_ioctl,
  1359. .ndo_change_mtu = efx_change_mtu,
  1360. .ndo_set_mac_address = efx_set_mac_address,
  1361. .ndo_set_multicast_list = efx_set_multicast_list,
  1362. #ifdef CONFIG_NET_POLL_CONTROLLER
  1363. .ndo_poll_controller = efx_netpoll,
  1364. #endif
  1365. };
  1366. static void efx_update_name(struct efx_nic *efx)
  1367. {
  1368. strcpy(efx->name, efx->net_dev->name);
  1369. efx_mtd_rename(efx);
  1370. efx_set_channel_names(efx);
  1371. }
  1372. static int efx_netdev_event(struct notifier_block *this,
  1373. unsigned long event, void *ptr)
  1374. {
  1375. struct net_device *net_dev = ptr;
  1376. if (net_dev->netdev_ops == &efx_netdev_ops &&
  1377. event == NETDEV_CHANGENAME)
  1378. efx_update_name(netdev_priv(net_dev));
  1379. return NOTIFY_DONE;
  1380. }
  1381. static struct notifier_block efx_netdev_notifier = {
  1382. .notifier_call = efx_netdev_event,
  1383. };
  1384. static ssize_t
  1385. show_phy_type(struct device *dev, struct device_attribute *attr, char *buf)
  1386. {
  1387. struct efx_nic *efx = pci_get_drvdata(to_pci_dev(dev));
  1388. return sprintf(buf, "%d\n", efx->phy_type);
  1389. }
  1390. static DEVICE_ATTR(phy_type, 0644, show_phy_type, NULL);
  1391. static int efx_register_netdev(struct efx_nic *efx)
  1392. {
  1393. struct net_device *net_dev = efx->net_dev;
  1394. int rc;
  1395. net_dev->watchdog_timeo = 5 * HZ;
  1396. net_dev->irq = efx->pci_dev->irq;
  1397. net_dev->netdev_ops = &efx_netdev_ops;
  1398. SET_NETDEV_DEV(net_dev, &efx->pci_dev->dev);
  1399. SET_ETHTOOL_OPS(net_dev, &efx_ethtool_ops);
  1400. /* Clear MAC statistics */
  1401. efx->mac_op->update_stats(efx);
  1402. memset(&efx->mac_stats, 0, sizeof(efx->mac_stats));
  1403. rtnl_lock();
  1404. rc = dev_alloc_name(net_dev, net_dev->name);
  1405. if (rc < 0)
  1406. goto fail_locked;
  1407. efx_update_name(efx);
  1408. rc = register_netdevice(net_dev);
  1409. if (rc)
  1410. goto fail_locked;
  1411. /* Always start with carrier off; PHY events will detect the link */
  1412. netif_carrier_off(efx->net_dev);
  1413. rtnl_unlock();
  1414. rc = device_create_file(&efx->pci_dev->dev, &dev_attr_phy_type);
  1415. if (rc) {
  1416. EFX_ERR(efx, "failed to init net dev attributes\n");
  1417. goto fail_registered;
  1418. }
  1419. return 0;
  1420. fail_locked:
  1421. rtnl_unlock();
  1422. EFX_ERR(efx, "could not register net dev\n");
  1423. return rc;
  1424. fail_registered:
  1425. unregister_netdev(net_dev);
  1426. return rc;
  1427. }
  1428. static void efx_unregister_netdev(struct efx_nic *efx)
  1429. {
  1430. struct efx_tx_queue *tx_queue;
  1431. if (!efx->net_dev)
  1432. return;
  1433. BUG_ON(netdev_priv(efx->net_dev) != efx);
  1434. /* Free up any skbs still remaining. This has to happen before
  1435. * we try to unregister the netdev as running their destructors
  1436. * may be needed to get the device ref. count to 0. */
  1437. efx_for_each_tx_queue(tx_queue, efx)
  1438. efx_release_tx_buffers(tx_queue);
  1439. if (efx_dev_registered(efx)) {
  1440. strlcpy(efx->name, pci_name(efx->pci_dev), sizeof(efx->name));
  1441. device_remove_file(&efx->pci_dev->dev, &dev_attr_phy_type);
  1442. unregister_netdev(efx->net_dev);
  1443. }
  1444. }
  1445. /**************************************************************************
  1446. *
  1447. * Device reset and suspend
  1448. *
  1449. **************************************************************************/
  1450. /* Tears down the entire software state and most of the hardware state
  1451. * before reset. */
  1452. void efx_reset_down(struct efx_nic *efx, enum reset_type method)
  1453. {
  1454. EFX_ASSERT_RESET_SERIALISED(efx);
  1455. efx_stop_all(efx);
  1456. mutex_lock(&efx->mac_lock);
  1457. mutex_lock(&efx->spi_lock);
  1458. efx_fini_channels(efx);
  1459. if (efx->port_initialized && method != RESET_TYPE_INVISIBLE)
  1460. efx->phy_op->fini(efx);
  1461. efx->type->fini(efx);
  1462. }
  1463. /* This function will always ensure that the locks acquired in
  1464. * efx_reset_down() are released. A failure return code indicates
  1465. * that we were unable to reinitialise the hardware, and the
  1466. * driver should be disabled. If ok is false, then the rx and tx
  1467. * engines are not restarted, pending a RESET_DISABLE. */
  1468. int efx_reset_up(struct efx_nic *efx, enum reset_type method, bool ok)
  1469. {
  1470. int rc;
  1471. EFX_ASSERT_RESET_SERIALISED(efx);
  1472. rc = efx->type->init(efx);
  1473. if (rc) {
  1474. EFX_ERR(efx, "failed to initialise NIC\n");
  1475. goto fail;
  1476. }
  1477. if (!ok)
  1478. goto fail;
  1479. if (efx->port_initialized && method != RESET_TYPE_INVISIBLE) {
  1480. rc = efx->phy_op->init(efx);
  1481. if (rc)
  1482. goto fail;
  1483. if (efx->phy_op->reconfigure(efx))
  1484. EFX_ERR(efx, "could not restore PHY settings\n");
  1485. }
  1486. efx->mac_op->reconfigure(efx);
  1487. efx_init_channels(efx);
  1488. mutex_unlock(&efx->spi_lock);
  1489. mutex_unlock(&efx->mac_lock);
  1490. efx_start_all(efx);
  1491. return 0;
  1492. fail:
  1493. efx->port_initialized = false;
  1494. mutex_unlock(&efx->spi_lock);
  1495. mutex_unlock(&efx->mac_lock);
  1496. return rc;
  1497. }
  1498. /* Reset the NIC using the specified method. Note that the reset may
  1499. * fail, in which case the card will be left in an unusable state.
  1500. *
  1501. * Caller must hold the rtnl_lock.
  1502. */
  1503. int efx_reset(struct efx_nic *efx, enum reset_type method)
  1504. {
  1505. int rc, rc2;
  1506. bool disabled;
  1507. EFX_INFO(efx, "resetting (%s)\n", RESET_TYPE(method));
  1508. efx_reset_down(efx, method);
  1509. rc = efx->type->reset(efx, method);
  1510. if (rc) {
  1511. EFX_ERR(efx, "failed to reset hardware\n");
  1512. goto out;
  1513. }
  1514. /* Allow resets to be rescheduled. */
  1515. efx->reset_pending = RESET_TYPE_NONE;
  1516. /* Reinitialise bus-mastering, which may have been turned off before
  1517. * the reset was scheduled. This is still appropriate, even in the
  1518. * RESET_TYPE_DISABLE since this driver generally assumes the hardware
  1519. * can respond to requests. */
  1520. pci_set_master(efx->pci_dev);
  1521. out:
  1522. /* Leave device stopped if necessary */
  1523. disabled = rc || method == RESET_TYPE_DISABLE;
  1524. rc2 = efx_reset_up(efx, method, !disabled);
  1525. if (rc2) {
  1526. disabled = true;
  1527. if (!rc)
  1528. rc = rc2;
  1529. }
  1530. if (disabled) {
  1531. dev_close(efx->net_dev);
  1532. EFX_ERR(efx, "has been disabled\n");
  1533. efx->state = STATE_DISABLED;
  1534. } else {
  1535. EFX_LOG(efx, "reset complete\n");
  1536. }
  1537. return rc;
  1538. }
  1539. /* The worker thread exists so that code that cannot sleep can
  1540. * schedule a reset for later.
  1541. */
  1542. static void efx_reset_work(struct work_struct *data)
  1543. {
  1544. struct efx_nic *efx = container_of(data, struct efx_nic, reset_work);
  1545. if (efx->reset_pending == RESET_TYPE_NONE)
  1546. return;
  1547. /* If we're not RUNNING then don't reset. Leave the reset_pending
  1548. * flag set so that efx_pci_probe_main will be retried */
  1549. if (efx->state != STATE_RUNNING) {
  1550. EFX_INFO(efx, "scheduled reset quenched. NIC not RUNNING\n");
  1551. return;
  1552. }
  1553. rtnl_lock();
  1554. (void)efx_reset(efx, efx->reset_pending);
  1555. rtnl_unlock();
  1556. }
  1557. void efx_schedule_reset(struct efx_nic *efx, enum reset_type type)
  1558. {
  1559. enum reset_type method;
  1560. if (efx->reset_pending != RESET_TYPE_NONE) {
  1561. EFX_INFO(efx, "quenching already scheduled reset\n");
  1562. return;
  1563. }
  1564. switch (type) {
  1565. case RESET_TYPE_INVISIBLE:
  1566. case RESET_TYPE_ALL:
  1567. case RESET_TYPE_WORLD:
  1568. case RESET_TYPE_DISABLE:
  1569. method = type;
  1570. break;
  1571. case RESET_TYPE_RX_RECOVERY:
  1572. case RESET_TYPE_RX_DESC_FETCH:
  1573. case RESET_TYPE_TX_DESC_FETCH:
  1574. case RESET_TYPE_TX_SKIP:
  1575. method = RESET_TYPE_INVISIBLE;
  1576. break;
  1577. case RESET_TYPE_MC_FAILURE:
  1578. default:
  1579. method = RESET_TYPE_ALL;
  1580. break;
  1581. }
  1582. if (method != type)
  1583. EFX_LOG(efx, "scheduling %s reset for %s\n",
  1584. RESET_TYPE(method), RESET_TYPE(type));
  1585. else
  1586. EFX_LOG(efx, "scheduling %s reset\n", RESET_TYPE(method));
  1587. efx->reset_pending = method;
  1588. /* efx_process_channel() will no longer read events once a
  1589. * reset is scheduled. So switch back to poll'd MCDI completions. */
  1590. efx_mcdi_mode_poll(efx);
  1591. queue_work(reset_workqueue, &efx->reset_work);
  1592. }
  1593. /**************************************************************************
  1594. *
  1595. * List of NICs we support
  1596. *
  1597. **************************************************************************/
  1598. /* PCI device ID table */
  1599. static DEFINE_PCI_DEVICE_TABLE(efx_pci_table) = {
  1600. {PCI_DEVICE(EFX_VENDID_SFC, FALCON_A_P_DEVID),
  1601. .driver_data = (unsigned long) &falcon_a1_nic_type},
  1602. {PCI_DEVICE(EFX_VENDID_SFC, FALCON_B_P_DEVID),
  1603. .driver_data = (unsigned long) &falcon_b0_nic_type},
  1604. {PCI_DEVICE(EFX_VENDID_SFC, BETHPAGE_A_P_DEVID),
  1605. .driver_data = (unsigned long) &siena_a0_nic_type},
  1606. {PCI_DEVICE(EFX_VENDID_SFC, SIENA_A_P_DEVID),
  1607. .driver_data = (unsigned long) &siena_a0_nic_type},
  1608. {0} /* end of list */
  1609. };
  1610. /**************************************************************************
  1611. *
  1612. * Dummy PHY/MAC operations
  1613. *
  1614. * Can be used for some unimplemented operations
  1615. * Needed so all function pointers are valid and do not have to be tested
  1616. * before use
  1617. *
  1618. **************************************************************************/
  1619. int efx_port_dummy_op_int(struct efx_nic *efx)
  1620. {
  1621. return 0;
  1622. }
  1623. void efx_port_dummy_op_void(struct efx_nic *efx) {}
  1624. void efx_port_dummy_op_set_id_led(struct efx_nic *efx, enum efx_led_mode mode)
  1625. {
  1626. }
  1627. bool efx_port_dummy_op_poll(struct efx_nic *efx)
  1628. {
  1629. return false;
  1630. }
  1631. static struct efx_phy_operations efx_dummy_phy_operations = {
  1632. .init = efx_port_dummy_op_int,
  1633. .reconfigure = efx_port_dummy_op_int,
  1634. .poll = efx_port_dummy_op_poll,
  1635. .fini = efx_port_dummy_op_void,
  1636. };
  1637. /**************************************************************************
  1638. *
  1639. * Data housekeeping
  1640. *
  1641. **************************************************************************/
  1642. /* This zeroes out and then fills in the invariants in a struct
  1643. * efx_nic (including all sub-structures).
  1644. */
  1645. static int efx_init_struct(struct efx_nic *efx, struct efx_nic_type *type,
  1646. struct pci_dev *pci_dev, struct net_device *net_dev)
  1647. {
  1648. struct efx_channel *channel;
  1649. struct efx_tx_queue *tx_queue;
  1650. struct efx_rx_queue *rx_queue;
  1651. int i;
  1652. /* Initialise common structures */
  1653. memset(efx, 0, sizeof(*efx));
  1654. spin_lock_init(&efx->biu_lock);
  1655. mutex_init(&efx->mdio_lock);
  1656. mutex_init(&efx->spi_lock);
  1657. #ifdef CONFIG_SFC_MTD
  1658. INIT_LIST_HEAD(&efx->mtd_list);
  1659. #endif
  1660. INIT_WORK(&efx->reset_work, efx_reset_work);
  1661. INIT_DELAYED_WORK(&efx->monitor_work, efx_monitor);
  1662. efx->pci_dev = pci_dev;
  1663. efx->state = STATE_INIT;
  1664. efx->reset_pending = RESET_TYPE_NONE;
  1665. strlcpy(efx->name, pci_name(pci_dev), sizeof(efx->name));
  1666. efx->net_dev = net_dev;
  1667. efx->rx_checksum_enabled = true;
  1668. spin_lock_init(&efx->stats_lock);
  1669. mutex_init(&efx->mac_lock);
  1670. efx->mac_op = type->default_mac_ops;
  1671. efx->phy_op = &efx_dummy_phy_operations;
  1672. efx->mdio.dev = net_dev;
  1673. INIT_WORK(&efx->mac_work, efx_mac_work);
  1674. for (i = 0; i < EFX_MAX_CHANNELS; i++) {
  1675. channel = &efx->channel[i];
  1676. channel->efx = efx;
  1677. channel->channel = i;
  1678. channel->work_pending = false;
  1679. spin_lock_init(&channel->tx_stop_lock);
  1680. atomic_set(&channel->tx_stop_count, 1);
  1681. }
  1682. for (i = 0; i < EFX_MAX_TX_QUEUES; i++) {
  1683. tx_queue = &efx->tx_queue[i];
  1684. tx_queue->efx = efx;
  1685. tx_queue->queue = i;
  1686. tx_queue->buffer = NULL;
  1687. tx_queue->channel = &efx->channel[0]; /* for safety */
  1688. tx_queue->tso_headers_free = NULL;
  1689. }
  1690. for (i = 0; i < EFX_MAX_RX_QUEUES; i++) {
  1691. rx_queue = &efx->rx_queue[i];
  1692. rx_queue->efx = efx;
  1693. rx_queue->queue = i;
  1694. rx_queue->channel = &efx->channel[0]; /* for safety */
  1695. rx_queue->buffer = NULL;
  1696. setup_timer(&rx_queue->slow_fill, efx_rx_slow_fill,
  1697. (unsigned long)rx_queue);
  1698. }
  1699. efx->type = type;
  1700. /* As close as we can get to guaranteeing that we don't overflow */
  1701. BUILD_BUG_ON(EFX_EVQ_SIZE < EFX_TXQ_SIZE + EFX_RXQ_SIZE);
  1702. EFX_BUG_ON_PARANOID(efx->type->phys_addr_channels > EFX_MAX_CHANNELS);
  1703. /* Higher numbered interrupt modes are less capable! */
  1704. efx->interrupt_mode = max(efx->type->max_interrupt_mode,
  1705. interrupt_mode);
  1706. /* Would be good to use the net_dev name, but we're too early */
  1707. snprintf(efx->workqueue_name, sizeof(efx->workqueue_name), "sfc%s",
  1708. pci_name(pci_dev));
  1709. efx->workqueue = create_singlethread_workqueue(efx->workqueue_name);
  1710. if (!efx->workqueue)
  1711. return -ENOMEM;
  1712. return 0;
  1713. }
  1714. static void efx_fini_struct(struct efx_nic *efx)
  1715. {
  1716. if (efx->workqueue) {
  1717. destroy_workqueue(efx->workqueue);
  1718. efx->workqueue = NULL;
  1719. }
  1720. }
  1721. /**************************************************************************
  1722. *
  1723. * PCI interface
  1724. *
  1725. **************************************************************************/
  1726. /* Main body of final NIC shutdown code
  1727. * This is called only at module unload (or hotplug removal).
  1728. */
  1729. static void efx_pci_remove_main(struct efx_nic *efx)
  1730. {
  1731. efx_nic_fini_interrupt(efx);
  1732. efx_fini_channels(efx);
  1733. efx_fini_port(efx);
  1734. efx->type->fini(efx);
  1735. efx_fini_napi(efx);
  1736. efx_remove_all(efx);
  1737. }
  1738. /* Final NIC shutdown
  1739. * This is called only at module unload (or hotplug removal).
  1740. */
  1741. static void efx_pci_remove(struct pci_dev *pci_dev)
  1742. {
  1743. struct efx_nic *efx;
  1744. efx = pci_get_drvdata(pci_dev);
  1745. if (!efx)
  1746. return;
  1747. /* Mark the NIC as fini, then stop the interface */
  1748. rtnl_lock();
  1749. efx->state = STATE_FINI;
  1750. dev_close(efx->net_dev);
  1751. /* Allow any queued efx_resets() to complete */
  1752. rtnl_unlock();
  1753. efx_unregister_netdev(efx);
  1754. efx_mtd_remove(efx);
  1755. /* Wait for any scheduled resets to complete. No more will be
  1756. * scheduled from this point because efx_stop_all() has been
  1757. * called, we are no longer registered with driverlink, and
  1758. * the net_device's have been removed. */
  1759. cancel_work_sync(&efx->reset_work);
  1760. efx_pci_remove_main(efx);
  1761. efx_fini_io(efx);
  1762. EFX_LOG(efx, "shutdown successful\n");
  1763. pci_set_drvdata(pci_dev, NULL);
  1764. efx_fini_struct(efx);
  1765. free_netdev(efx->net_dev);
  1766. };
  1767. /* Main body of NIC initialisation
  1768. * This is called at module load (or hotplug insertion, theoretically).
  1769. */
  1770. static int efx_pci_probe_main(struct efx_nic *efx)
  1771. {
  1772. int rc;
  1773. /* Do start-of-day initialisation */
  1774. rc = efx_probe_all(efx);
  1775. if (rc)
  1776. goto fail1;
  1777. rc = efx_init_napi(efx);
  1778. if (rc)
  1779. goto fail2;
  1780. rc = efx->type->init(efx);
  1781. if (rc) {
  1782. EFX_ERR(efx, "failed to initialise NIC\n");
  1783. goto fail3;
  1784. }
  1785. rc = efx_init_port(efx);
  1786. if (rc) {
  1787. EFX_ERR(efx, "failed to initialise port\n");
  1788. goto fail4;
  1789. }
  1790. efx_init_channels(efx);
  1791. rc = efx_nic_init_interrupt(efx);
  1792. if (rc)
  1793. goto fail5;
  1794. return 0;
  1795. fail5:
  1796. efx_fini_channels(efx);
  1797. efx_fini_port(efx);
  1798. fail4:
  1799. efx->type->fini(efx);
  1800. fail3:
  1801. efx_fini_napi(efx);
  1802. fail2:
  1803. efx_remove_all(efx);
  1804. fail1:
  1805. return rc;
  1806. }
  1807. /* NIC initialisation
  1808. *
  1809. * This is called at module load (or hotplug insertion,
  1810. * theoretically). It sets up PCI mappings, tests and resets the NIC,
  1811. * sets up and registers the network devices with the kernel and hooks
  1812. * the interrupt service routine. It does not prepare the device for
  1813. * transmission; this is left to the first time one of the network
  1814. * interfaces is brought up (i.e. efx_net_open).
  1815. */
  1816. static int __devinit efx_pci_probe(struct pci_dev *pci_dev,
  1817. const struct pci_device_id *entry)
  1818. {
  1819. struct efx_nic_type *type = (struct efx_nic_type *) entry->driver_data;
  1820. struct net_device *net_dev;
  1821. struct efx_nic *efx;
  1822. int i, rc;
  1823. /* Allocate and initialise a struct net_device and struct efx_nic */
  1824. net_dev = alloc_etherdev_mq(sizeof(*efx), EFX_MAX_CORE_TX_QUEUES);
  1825. if (!net_dev)
  1826. return -ENOMEM;
  1827. net_dev->features |= (type->offload_features | NETIF_F_SG |
  1828. NETIF_F_HIGHDMA | NETIF_F_TSO |
  1829. NETIF_F_GRO);
  1830. if (type->offload_features & NETIF_F_V6_CSUM)
  1831. net_dev->features |= NETIF_F_TSO6;
  1832. /* Mask for features that also apply to VLAN devices */
  1833. net_dev->vlan_features |= (NETIF_F_ALL_CSUM | NETIF_F_SG |
  1834. NETIF_F_HIGHDMA | NETIF_F_TSO);
  1835. efx = netdev_priv(net_dev);
  1836. pci_set_drvdata(pci_dev, efx);
  1837. rc = efx_init_struct(efx, type, pci_dev, net_dev);
  1838. if (rc)
  1839. goto fail1;
  1840. EFX_INFO(efx, "Solarflare Communications NIC detected\n");
  1841. /* Set up basic I/O (BAR mappings etc) */
  1842. rc = efx_init_io(efx);
  1843. if (rc)
  1844. goto fail2;
  1845. /* No serialisation is required with the reset path because
  1846. * we're in STATE_INIT. */
  1847. for (i = 0; i < 5; i++) {
  1848. rc = efx_pci_probe_main(efx);
  1849. /* Serialise against efx_reset(). No more resets will be
  1850. * scheduled since efx_stop_all() has been called, and we
  1851. * have not and never have been registered with either
  1852. * the rtnetlink or driverlink layers. */
  1853. cancel_work_sync(&efx->reset_work);
  1854. if (rc == 0) {
  1855. if (efx->reset_pending != RESET_TYPE_NONE) {
  1856. /* If there was a scheduled reset during
  1857. * probe, the NIC is probably hosed anyway */
  1858. efx_pci_remove_main(efx);
  1859. rc = -EIO;
  1860. } else {
  1861. break;
  1862. }
  1863. }
  1864. /* Retry if a recoverably reset event has been scheduled */
  1865. if ((efx->reset_pending != RESET_TYPE_INVISIBLE) &&
  1866. (efx->reset_pending != RESET_TYPE_ALL))
  1867. goto fail3;
  1868. efx->reset_pending = RESET_TYPE_NONE;
  1869. }
  1870. if (rc) {
  1871. EFX_ERR(efx, "Could not reset NIC\n");
  1872. goto fail4;
  1873. }
  1874. /* Switch to the running state before we expose the device to the OS,
  1875. * so that dev_open()|efx_start_all() will actually start the device */
  1876. efx->state = STATE_RUNNING;
  1877. rc = efx_register_netdev(efx);
  1878. if (rc)
  1879. goto fail5;
  1880. EFX_LOG(efx, "initialisation successful\n");
  1881. rtnl_lock();
  1882. efx_mtd_probe(efx); /* allowed to fail */
  1883. rtnl_unlock();
  1884. return 0;
  1885. fail5:
  1886. efx_pci_remove_main(efx);
  1887. fail4:
  1888. fail3:
  1889. efx_fini_io(efx);
  1890. fail2:
  1891. efx_fini_struct(efx);
  1892. fail1:
  1893. WARN_ON(rc > 0);
  1894. EFX_LOG(efx, "initialisation failed. rc=%d\n", rc);
  1895. free_netdev(net_dev);
  1896. return rc;
  1897. }
  1898. static int efx_pm_freeze(struct device *dev)
  1899. {
  1900. struct efx_nic *efx = pci_get_drvdata(to_pci_dev(dev));
  1901. efx->state = STATE_FINI;
  1902. netif_device_detach(efx->net_dev);
  1903. efx_stop_all(efx);
  1904. efx_fini_channels(efx);
  1905. return 0;
  1906. }
  1907. static int efx_pm_thaw(struct device *dev)
  1908. {
  1909. struct efx_nic *efx = pci_get_drvdata(to_pci_dev(dev));
  1910. efx->state = STATE_INIT;
  1911. efx_init_channels(efx);
  1912. mutex_lock(&efx->mac_lock);
  1913. efx->phy_op->reconfigure(efx);
  1914. mutex_unlock(&efx->mac_lock);
  1915. efx_start_all(efx);
  1916. netif_device_attach(efx->net_dev);
  1917. efx->state = STATE_RUNNING;
  1918. efx->type->resume_wol(efx);
  1919. /* Reschedule any quenched resets scheduled during efx_pm_freeze() */
  1920. queue_work(reset_workqueue, &efx->reset_work);
  1921. return 0;
  1922. }
  1923. static int efx_pm_poweroff(struct device *dev)
  1924. {
  1925. struct pci_dev *pci_dev = to_pci_dev(dev);
  1926. struct efx_nic *efx = pci_get_drvdata(pci_dev);
  1927. efx->type->fini(efx);
  1928. efx->reset_pending = RESET_TYPE_NONE;
  1929. pci_save_state(pci_dev);
  1930. return pci_set_power_state(pci_dev, PCI_D3hot);
  1931. }
  1932. /* Used for both resume and restore */
  1933. static int efx_pm_resume(struct device *dev)
  1934. {
  1935. struct pci_dev *pci_dev = to_pci_dev(dev);
  1936. struct efx_nic *efx = pci_get_drvdata(pci_dev);
  1937. int rc;
  1938. rc = pci_set_power_state(pci_dev, PCI_D0);
  1939. if (rc)
  1940. return rc;
  1941. pci_restore_state(pci_dev);
  1942. rc = pci_enable_device(pci_dev);
  1943. if (rc)
  1944. return rc;
  1945. pci_set_master(efx->pci_dev);
  1946. rc = efx->type->reset(efx, RESET_TYPE_ALL);
  1947. if (rc)
  1948. return rc;
  1949. rc = efx->type->init(efx);
  1950. if (rc)
  1951. return rc;
  1952. efx_pm_thaw(dev);
  1953. return 0;
  1954. }
  1955. static int efx_pm_suspend(struct device *dev)
  1956. {
  1957. int rc;
  1958. efx_pm_freeze(dev);
  1959. rc = efx_pm_poweroff(dev);
  1960. if (rc)
  1961. efx_pm_resume(dev);
  1962. return rc;
  1963. }
  1964. static struct dev_pm_ops efx_pm_ops = {
  1965. .suspend = efx_pm_suspend,
  1966. .resume = efx_pm_resume,
  1967. .freeze = efx_pm_freeze,
  1968. .thaw = efx_pm_thaw,
  1969. .poweroff = efx_pm_poweroff,
  1970. .restore = efx_pm_resume,
  1971. };
  1972. static struct pci_driver efx_pci_driver = {
  1973. .name = EFX_DRIVER_NAME,
  1974. .id_table = efx_pci_table,
  1975. .probe = efx_pci_probe,
  1976. .remove = efx_pci_remove,
  1977. .driver.pm = &efx_pm_ops,
  1978. };
  1979. /**************************************************************************
  1980. *
  1981. * Kernel module interface
  1982. *
  1983. *************************************************************************/
  1984. module_param(interrupt_mode, uint, 0444);
  1985. MODULE_PARM_DESC(interrupt_mode,
  1986. "Interrupt mode (0=>MSIX 1=>MSI 2=>legacy)");
  1987. static int __init efx_init_module(void)
  1988. {
  1989. int rc;
  1990. printk(KERN_INFO "Solarflare NET driver v" EFX_DRIVER_VERSION "\n");
  1991. rc = register_netdevice_notifier(&efx_netdev_notifier);
  1992. if (rc)
  1993. goto err_notifier;
  1994. reset_workqueue = create_singlethread_workqueue("sfc_reset");
  1995. if (!reset_workqueue) {
  1996. rc = -ENOMEM;
  1997. goto err_reset;
  1998. }
  1999. rc = pci_register_driver(&efx_pci_driver);
  2000. if (rc < 0)
  2001. goto err_pci;
  2002. return 0;
  2003. err_pci:
  2004. destroy_workqueue(reset_workqueue);
  2005. err_reset:
  2006. unregister_netdevice_notifier(&efx_netdev_notifier);
  2007. err_notifier:
  2008. return rc;
  2009. }
  2010. static void __exit efx_exit_module(void)
  2011. {
  2012. printk(KERN_INFO "Solarflare NET driver unloading\n");
  2013. pci_unregister_driver(&efx_pci_driver);
  2014. destroy_workqueue(reset_workqueue);
  2015. unregister_netdevice_notifier(&efx_netdev_notifier);
  2016. }
  2017. module_init(efx_init_module);
  2018. module_exit(efx_exit_module);
  2019. MODULE_AUTHOR("Solarflare Communications and "
  2020. "Michael Brown <mbrown@fensystems.co.uk>");
  2021. MODULE_DESCRIPTION("Solarflare Communications network driver");
  2022. MODULE_LICENSE("GPL");
  2023. MODULE_DEVICE_TABLE(pci, efx_pci_table);