efx.c 57 KB

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