efx.c 56 KB

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