efx.c 61 KB

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