efx.c 60 KB

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