efx.c 57 KB

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