efx.c 60 KB

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