efx.c 58 KB

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