efx.c 56 KB

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