efx.c 70 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707
  1. /****************************************************************************
  2. * Driver for Solarflare Solarstorm network controllers and boards
  3. * Copyright 2005-2006 Fen Systems Ltd.
  4. * Copyright 2005-2011 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 <linux/gfp.h>
  23. #include <linux/cpu_rmap.h>
  24. #include "net_driver.h"
  25. #include "efx.h"
  26. #include "nic.h"
  27. #include "mcdi.h"
  28. #include "workarounds.h"
  29. /**************************************************************************
  30. *
  31. * Type name strings
  32. *
  33. **************************************************************************
  34. */
  35. /* Loopback mode names (see LOOPBACK_MODE()) */
  36. const unsigned int efx_loopback_mode_max = LOOPBACK_MAX;
  37. const char *const efx_loopback_mode_names[] = {
  38. [LOOPBACK_NONE] = "NONE",
  39. [LOOPBACK_DATA] = "DATAPATH",
  40. [LOOPBACK_GMAC] = "GMAC",
  41. [LOOPBACK_XGMII] = "XGMII",
  42. [LOOPBACK_XGXS] = "XGXS",
  43. [LOOPBACK_XAUI] = "XAUI",
  44. [LOOPBACK_GMII] = "GMII",
  45. [LOOPBACK_SGMII] = "SGMII",
  46. [LOOPBACK_XGBR] = "XGBR",
  47. [LOOPBACK_XFI] = "XFI",
  48. [LOOPBACK_XAUI_FAR] = "XAUI_FAR",
  49. [LOOPBACK_GMII_FAR] = "GMII_FAR",
  50. [LOOPBACK_SGMII_FAR] = "SGMII_FAR",
  51. [LOOPBACK_XFI_FAR] = "XFI_FAR",
  52. [LOOPBACK_GPHY] = "GPHY",
  53. [LOOPBACK_PHYXS] = "PHYXS",
  54. [LOOPBACK_PCS] = "PCS",
  55. [LOOPBACK_PMAPMD] = "PMA/PMD",
  56. [LOOPBACK_XPORT] = "XPORT",
  57. [LOOPBACK_XGMII_WS] = "XGMII_WS",
  58. [LOOPBACK_XAUI_WS] = "XAUI_WS",
  59. [LOOPBACK_XAUI_WS_FAR] = "XAUI_WS_FAR",
  60. [LOOPBACK_XAUI_WS_NEAR] = "XAUI_WS_NEAR",
  61. [LOOPBACK_GMII_WS] = "GMII_WS",
  62. [LOOPBACK_XFI_WS] = "XFI_WS",
  63. [LOOPBACK_XFI_WS_FAR] = "XFI_WS_FAR",
  64. [LOOPBACK_PHYXS_WS] = "PHYXS_WS",
  65. };
  66. const unsigned int efx_reset_type_max = RESET_TYPE_MAX;
  67. const char *const efx_reset_type_names[] = {
  68. [RESET_TYPE_INVISIBLE] = "INVISIBLE",
  69. [RESET_TYPE_ALL] = "ALL",
  70. [RESET_TYPE_WORLD] = "WORLD",
  71. [RESET_TYPE_DISABLE] = "DISABLE",
  72. [RESET_TYPE_TX_WATCHDOG] = "TX_WATCHDOG",
  73. [RESET_TYPE_INT_ERROR] = "INT_ERROR",
  74. [RESET_TYPE_RX_RECOVERY] = "RX_RECOVERY",
  75. [RESET_TYPE_RX_DESC_FETCH] = "RX_DESC_FETCH",
  76. [RESET_TYPE_TX_DESC_FETCH] = "TX_DESC_FETCH",
  77. [RESET_TYPE_TX_SKIP] = "TX_SKIP",
  78. [RESET_TYPE_MC_FAILURE] = "MC_FAILURE",
  79. };
  80. #define EFX_MAX_MTU (9 * 1024)
  81. /* Reset workqueue. If any NIC has a hardware failure then a reset will be
  82. * queued onto this work queue. This is not a per-nic work queue, because
  83. * efx_reset_work() acquires the rtnl lock, so resets are naturally serialised.
  84. */
  85. static struct workqueue_struct *reset_workqueue;
  86. /**************************************************************************
  87. *
  88. * Configurable values
  89. *
  90. *************************************************************************/
  91. /*
  92. * Use separate channels for TX and RX events
  93. *
  94. * Set this to 1 to use separate channels for TX and RX. It allows us
  95. * to control interrupt affinity separately for TX and RX.
  96. *
  97. * This is only used in MSI-X interrupt mode
  98. */
  99. static unsigned int separate_tx_channels;
  100. module_param(separate_tx_channels, uint, 0444);
  101. MODULE_PARM_DESC(separate_tx_channels,
  102. "Use separate channels for TX and RX");
  103. /* This is the weight assigned to each of the (per-channel) virtual
  104. * NAPI devices.
  105. */
  106. static int napi_weight = 64;
  107. /* This is the time (in jiffies) between invocations of the hardware
  108. * monitor. On Falcon-based NICs, this will:
  109. * - Check the on-board hardware monitor;
  110. * - Poll the link state and reconfigure the hardware as necessary.
  111. */
  112. static unsigned int efx_monitor_interval = 1 * HZ;
  113. /* Initial interrupt moderation settings. They can be modified after
  114. * module load with ethtool.
  115. *
  116. * The default for RX should strike a balance between increasing the
  117. * round-trip latency and reducing overhead.
  118. */
  119. static unsigned int rx_irq_mod_usec = 60;
  120. /* Initial interrupt moderation settings. They can be modified after
  121. * module load with ethtool.
  122. *
  123. * This default is chosen to ensure that a 10G link does not go idle
  124. * while a TX queue is stopped after it has become full. A queue is
  125. * restarted when it drops below half full. The time this takes (assuming
  126. * worst case 3 descriptors per packet and 1024 descriptors) is
  127. * 512 / 3 * 1.2 = 205 usec.
  128. */
  129. static unsigned int tx_irq_mod_usec = 150;
  130. /* This is the first interrupt mode to try out of:
  131. * 0 => MSI-X
  132. * 1 => MSI
  133. * 2 => legacy
  134. */
  135. static unsigned int interrupt_mode;
  136. /* This is the requested number of CPUs to use for Receive-Side Scaling (RSS),
  137. * i.e. the number of CPUs among which we may distribute simultaneous
  138. * interrupt handling.
  139. *
  140. * Cards without MSI-X will only target one CPU via legacy or MSI interrupt.
  141. * The default (0) means to assign an interrupt to each core.
  142. */
  143. static unsigned int rss_cpus;
  144. module_param(rss_cpus, uint, 0444);
  145. MODULE_PARM_DESC(rss_cpus, "Number of CPUs to use for Receive-Side Scaling");
  146. static int phy_flash_cfg;
  147. module_param(phy_flash_cfg, int, 0644);
  148. MODULE_PARM_DESC(phy_flash_cfg, "Set PHYs into reflash mode initially");
  149. static unsigned irq_adapt_low_thresh = 10000;
  150. module_param(irq_adapt_low_thresh, uint, 0644);
  151. MODULE_PARM_DESC(irq_adapt_low_thresh,
  152. "Threshold score for reducing IRQ moderation");
  153. static unsigned irq_adapt_high_thresh = 20000;
  154. module_param(irq_adapt_high_thresh, uint, 0644);
  155. MODULE_PARM_DESC(irq_adapt_high_thresh,
  156. "Threshold score for increasing IRQ moderation");
  157. static unsigned debug = (NETIF_MSG_DRV | NETIF_MSG_PROBE |
  158. NETIF_MSG_LINK | NETIF_MSG_IFDOWN |
  159. NETIF_MSG_IFUP | NETIF_MSG_RX_ERR |
  160. NETIF_MSG_TX_ERR | NETIF_MSG_HW);
  161. module_param(debug, uint, 0);
  162. MODULE_PARM_DESC(debug, "Bitmapped debugging message enable value");
  163. /**************************************************************************
  164. *
  165. * Utility functions and prototypes
  166. *
  167. *************************************************************************/
  168. static void efx_remove_channels(struct efx_nic *efx);
  169. static void efx_remove_port(struct efx_nic *efx);
  170. static void efx_init_napi(struct efx_nic *efx);
  171. static void efx_fini_napi(struct efx_nic *efx);
  172. static void efx_fini_napi_channel(struct efx_channel *channel);
  173. static void efx_fini_struct(struct efx_nic *efx);
  174. static void efx_start_all(struct efx_nic *efx);
  175. static void efx_stop_all(struct efx_nic *efx);
  176. #define EFX_ASSERT_RESET_SERIALISED(efx) \
  177. do { \
  178. if ((efx->state == STATE_RUNNING) || \
  179. (efx->state == STATE_DISABLED)) \
  180. ASSERT_RTNL(); \
  181. } while (0)
  182. /**************************************************************************
  183. *
  184. * Event queue processing
  185. *
  186. *************************************************************************/
  187. /* Process channel's event queue
  188. *
  189. * This function is responsible for processing the event queue of a
  190. * single channel. The caller must guarantee that this function will
  191. * never be concurrently called more than once on the same channel,
  192. * though different channels may be being processed concurrently.
  193. */
  194. static int efx_process_channel(struct efx_channel *channel, int budget)
  195. {
  196. struct efx_nic *efx = channel->efx;
  197. int spent;
  198. if (unlikely(efx->reset_pending || !channel->enabled))
  199. return 0;
  200. spent = efx_nic_process_eventq(channel, budget);
  201. if (spent == 0)
  202. return 0;
  203. /* Deliver last RX packet. */
  204. if (channel->rx_pkt) {
  205. __efx_rx_packet(channel, channel->rx_pkt,
  206. channel->rx_pkt_csummed);
  207. channel->rx_pkt = NULL;
  208. }
  209. efx_rx_strategy(channel);
  210. efx_fast_push_rx_descriptors(efx_channel_get_rx_queue(channel));
  211. return spent;
  212. }
  213. /* Mark channel as finished processing
  214. *
  215. * Note that since we will not receive further interrupts for this
  216. * channel before we finish processing and call the eventq_read_ack()
  217. * method, there is no need to use the interrupt hold-off timers.
  218. */
  219. static inline void efx_channel_processed(struct efx_channel *channel)
  220. {
  221. /* The interrupt handler for this channel may set work_pending
  222. * as soon as we acknowledge the events we've seen. Make sure
  223. * it's cleared before then. */
  224. channel->work_pending = false;
  225. smp_wmb();
  226. efx_nic_eventq_read_ack(channel);
  227. }
  228. /* NAPI poll handler
  229. *
  230. * NAPI guarantees serialisation of polls of the same device, which
  231. * provides the guarantee required by efx_process_channel().
  232. */
  233. static int efx_poll(struct napi_struct *napi, int budget)
  234. {
  235. struct efx_channel *channel =
  236. container_of(napi, struct efx_channel, napi_str);
  237. struct efx_nic *efx = channel->efx;
  238. int spent;
  239. netif_vdbg(efx, intr, efx->net_dev,
  240. "channel %d NAPI poll executing on CPU %d\n",
  241. channel->channel, raw_smp_processor_id());
  242. spent = efx_process_channel(channel, budget);
  243. if (spent < budget) {
  244. if (channel->channel < efx->n_rx_channels &&
  245. efx->irq_rx_adaptive &&
  246. unlikely(++channel->irq_count == 1000)) {
  247. if (unlikely(channel->irq_mod_score <
  248. irq_adapt_low_thresh)) {
  249. if (channel->irq_moderation > 1) {
  250. channel->irq_moderation -= 1;
  251. efx->type->push_irq_moderation(channel);
  252. }
  253. } else if (unlikely(channel->irq_mod_score >
  254. irq_adapt_high_thresh)) {
  255. if (channel->irq_moderation <
  256. efx->irq_rx_moderation) {
  257. channel->irq_moderation += 1;
  258. efx->type->push_irq_moderation(channel);
  259. }
  260. }
  261. channel->irq_count = 0;
  262. channel->irq_mod_score = 0;
  263. }
  264. efx_filter_rfs_expire(channel);
  265. /* There is no race here; although napi_disable() will
  266. * only wait for napi_complete(), this isn't a problem
  267. * since efx_channel_processed() will have no effect if
  268. * interrupts have already been disabled.
  269. */
  270. napi_complete(napi);
  271. efx_channel_processed(channel);
  272. }
  273. return spent;
  274. }
  275. /* Process the eventq of the specified channel immediately on this CPU
  276. *
  277. * Disable hardware generated interrupts, wait for any existing
  278. * processing to finish, then directly poll (and ack ) the eventq.
  279. * Finally reenable NAPI and interrupts.
  280. *
  281. * This is for use only during a loopback self-test. It must not
  282. * deliver any packets up the stack as this can result in deadlock.
  283. */
  284. void efx_process_channel_now(struct efx_channel *channel)
  285. {
  286. struct efx_nic *efx = channel->efx;
  287. BUG_ON(channel->channel >= efx->n_channels);
  288. BUG_ON(!channel->enabled);
  289. BUG_ON(!efx->loopback_selftest);
  290. /* Disable interrupts and wait for ISRs to complete */
  291. efx_nic_disable_interrupts(efx);
  292. if (efx->legacy_irq) {
  293. synchronize_irq(efx->legacy_irq);
  294. efx->legacy_irq_enabled = false;
  295. }
  296. if (channel->irq)
  297. synchronize_irq(channel->irq);
  298. /* Wait for any NAPI processing to complete */
  299. napi_disable(&channel->napi_str);
  300. /* Poll the channel */
  301. efx_process_channel(channel, channel->eventq_mask + 1);
  302. /* Ack the eventq. This may cause an interrupt to be generated
  303. * when they are reenabled */
  304. efx_channel_processed(channel);
  305. napi_enable(&channel->napi_str);
  306. if (efx->legacy_irq)
  307. efx->legacy_irq_enabled = true;
  308. efx_nic_enable_interrupts(efx);
  309. }
  310. /* Create event queue
  311. * Event queue memory allocations are done only once. If the channel
  312. * is reset, the memory buffer will be reused; this guards against
  313. * errors during channel reset and also simplifies interrupt handling.
  314. */
  315. static int efx_probe_eventq(struct efx_channel *channel)
  316. {
  317. struct efx_nic *efx = channel->efx;
  318. unsigned long entries;
  319. netif_dbg(channel->efx, probe, channel->efx->net_dev,
  320. "chan %d create event queue\n", channel->channel);
  321. /* Build an event queue with room for one event per tx and rx buffer,
  322. * plus some extra for link state events and MCDI completions. */
  323. entries = roundup_pow_of_two(efx->rxq_entries + efx->txq_entries + 128);
  324. EFX_BUG_ON_PARANOID(entries > EFX_MAX_EVQ_SIZE);
  325. channel->eventq_mask = max(entries, EFX_MIN_EVQ_SIZE) - 1;
  326. return efx_nic_probe_eventq(channel);
  327. }
  328. /* Prepare channel's event queue */
  329. static void efx_init_eventq(struct efx_channel *channel)
  330. {
  331. netif_dbg(channel->efx, drv, channel->efx->net_dev,
  332. "chan %d init event queue\n", channel->channel);
  333. channel->eventq_read_ptr = 0;
  334. efx_nic_init_eventq(channel);
  335. }
  336. static void efx_fini_eventq(struct efx_channel *channel)
  337. {
  338. netif_dbg(channel->efx, drv, channel->efx->net_dev,
  339. "chan %d fini event queue\n", channel->channel);
  340. efx_nic_fini_eventq(channel);
  341. }
  342. static void efx_remove_eventq(struct efx_channel *channel)
  343. {
  344. netif_dbg(channel->efx, drv, channel->efx->net_dev,
  345. "chan %d remove event queue\n", channel->channel);
  346. efx_nic_remove_eventq(channel);
  347. }
  348. /**************************************************************************
  349. *
  350. * Channel handling
  351. *
  352. *************************************************************************/
  353. /* Allocate and initialise a channel structure, optionally copying
  354. * parameters (but not resources) from an old channel structure. */
  355. static struct efx_channel *
  356. efx_alloc_channel(struct efx_nic *efx, int i, struct efx_channel *old_channel)
  357. {
  358. struct efx_channel *channel;
  359. struct efx_rx_queue *rx_queue;
  360. struct efx_tx_queue *tx_queue;
  361. int j;
  362. if (old_channel) {
  363. channel = kmalloc(sizeof(*channel), GFP_KERNEL);
  364. if (!channel)
  365. return NULL;
  366. *channel = *old_channel;
  367. channel->napi_dev = NULL;
  368. memset(&channel->eventq, 0, sizeof(channel->eventq));
  369. rx_queue = &channel->rx_queue;
  370. rx_queue->buffer = NULL;
  371. memset(&rx_queue->rxd, 0, sizeof(rx_queue->rxd));
  372. for (j = 0; j < EFX_TXQ_TYPES; j++) {
  373. tx_queue = &channel->tx_queue[j];
  374. if (tx_queue->channel)
  375. tx_queue->channel = channel;
  376. tx_queue->buffer = NULL;
  377. memset(&tx_queue->txd, 0, sizeof(tx_queue->txd));
  378. }
  379. } else {
  380. channel = kzalloc(sizeof(*channel), GFP_KERNEL);
  381. if (!channel)
  382. return NULL;
  383. channel->efx = efx;
  384. channel->channel = i;
  385. for (j = 0; j < EFX_TXQ_TYPES; j++) {
  386. tx_queue = &channel->tx_queue[j];
  387. tx_queue->efx = efx;
  388. tx_queue->queue = i * EFX_TXQ_TYPES + j;
  389. tx_queue->channel = channel;
  390. }
  391. }
  392. rx_queue = &channel->rx_queue;
  393. rx_queue->efx = efx;
  394. setup_timer(&rx_queue->slow_fill, efx_rx_slow_fill,
  395. (unsigned long)rx_queue);
  396. return channel;
  397. }
  398. static int efx_probe_channel(struct efx_channel *channel)
  399. {
  400. struct efx_tx_queue *tx_queue;
  401. struct efx_rx_queue *rx_queue;
  402. int rc;
  403. netif_dbg(channel->efx, probe, channel->efx->net_dev,
  404. "creating channel %d\n", channel->channel);
  405. rc = efx_probe_eventq(channel);
  406. if (rc)
  407. goto fail1;
  408. efx_for_each_channel_tx_queue(tx_queue, channel) {
  409. rc = efx_probe_tx_queue(tx_queue);
  410. if (rc)
  411. goto fail2;
  412. }
  413. efx_for_each_channel_rx_queue(rx_queue, channel) {
  414. rc = efx_probe_rx_queue(rx_queue);
  415. if (rc)
  416. goto fail3;
  417. }
  418. channel->n_rx_frm_trunc = 0;
  419. return 0;
  420. fail3:
  421. efx_for_each_channel_rx_queue(rx_queue, channel)
  422. efx_remove_rx_queue(rx_queue);
  423. fail2:
  424. efx_for_each_channel_tx_queue(tx_queue, channel)
  425. efx_remove_tx_queue(tx_queue);
  426. fail1:
  427. return rc;
  428. }
  429. static void efx_set_channel_names(struct efx_nic *efx)
  430. {
  431. struct efx_channel *channel;
  432. const char *type = "";
  433. int number;
  434. efx_for_each_channel(channel, efx) {
  435. number = channel->channel;
  436. if (efx->n_channels > efx->n_rx_channels) {
  437. if (channel->channel < efx->n_rx_channels) {
  438. type = "-rx";
  439. } else {
  440. type = "-tx";
  441. number -= efx->n_rx_channels;
  442. }
  443. }
  444. snprintf(efx->channel_name[channel->channel],
  445. sizeof(efx->channel_name[0]),
  446. "%s%s-%d", efx->name, type, number);
  447. }
  448. }
  449. static int efx_probe_channels(struct efx_nic *efx)
  450. {
  451. struct efx_channel *channel;
  452. int rc;
  453. /* Restart special buffer allocation */
  454. efx->next_buffer_table = 0;
  455. efx_for_each_channel(channel, efx) {
  456. rc = efx_probe_channel(channel);
  457. if (rc) {
  458. netif_err(efx, probe, efx->net_dev,
  459. "failed to create channel %d\n",
  460. channel->channel);
  461. goto fail;
  462. }
  463. }
  464. efx_set_channel_names(efx);
  465. return 0;
  466. fail:
  467. efx_remove_channels(efx);
  468. return rc;
  469. }
  470. /* Channels are shutdown and reinitialised whilst the NIC is running
  471. * to propagate configuration changes (mtu, checksum offload), or
  472. * to clear hardware error conditions
  473. */
  474. static void efx_init_channels(struct efx_nic *efx)
  475. {
  476. struct efx_tx_queue *tx_queue;
  477. struct efx_rx_queue *rx_queue;
  478. struct efx_channel *channel;
  479. /* Calculate the rx buffer allocation parameters required to
  480. * support the current MTU, including padding for header
  481. * alignment and overruns.
  482. */
  483. efx->rx_buffer_len = (max(EFX_PAGE_IP_ALIGN, NET_IP_ALIGN) +
  484. EFX_MAX_FRAME_LEN(efx->net_dev->mtu) +
  485. efx->type->rx_buffer_hash_size +
  486. efx->type->rx_buffer_padding);
  487. efx->rx_buffer_order = get_order(efx->rx_buffer_len +
  488. sizeof(struct efx_rx_page_state));
  489. /* Initialise the channels */
  490. efx_for_each_channel(channel, efx) {
  491. netif_dbg(channel->efx, drv, channel->efx->net_dev,
  492. "init chan %d\n", channel->channel);
  493. efx_init_eventq(channel);
  494. efx_for_each_channel_tx_queue(tx_queue, channel)
  495. efx_init_tx_queue(tx_queue);
  496. /* The rx buffer allocation strategy is MTU dependent */
  497. efx_rx_strategy(channel);
  498. efx_for_each_channel_rx_queue(rx_queue, channel)
  499. efx_init_rx_queue(rx_queue);
  500. WARN_ON(channel->rx_pkt != NULL);
  501. efx_rx_strategy(channel);
  502. }
  503. }
  504. /* This enables event queue processing and packet transmission.
  505. *
  506. * Note that this function is not allowed to fail, since that would
  507. * introduce too much complexity into the suspend/resume path.
  508. */
  509. static void efx_start_channel(struct efx_channel *channel)
  510. {
  511. struct efx_rx_queue *rx_queue;
  512. netif_dbg(channel->efx, ifup, channel->efx->net_dev,
  513. "starting chan %d\n", channel->channel);
  514. /* The interrupt handler for this channel may set work_pending
  515. * as soon as we enable it. Make sure it's cleared before
  516. * then. Similarly, make sure it sees the enabled flag set. */
  517. channel->work_pending = false;
  518. channel->enabled = true;
  519. smp_wmb();
  520. /* Fill the queues before enabling NAPI */
  521. efx_for_each_channel_rx_queue(rx_queue, channel)
  522. efx_fast_push_rx_descriptors(rx_queue);
  523. napi_enable(&channel->napi_str);
  524. }
  525. /* This disables event queue processing and packet transmission.
  526. * This function does not guarantee that all queue processing
  527. * (e.g. RX refill) is complete.
  528. */
  529. static void efx_stop_channel(struct efx_channel *channel)
  530. {
  531. if (!channel->enabled)
  532. return;
  533. netif_dbg(channel->efx, ifdown, channel->efx->net_dev,
  534. "stop chan %d\n", channel->channel);
  535. channel->enabled = false;
  536. napi_disable(&channel->napi_str);
  537. }
  538. static void efx_fini_channels(struct efx_nic *efx)
  539. {
  540. struct efx_channel *channel;
  541. struct efx_tx_queue *tx_queue;
  542. struct efx_rx_queue *rx_queue;
  543. int rc;
  544. EFX_ASSERT_RESET_SERIALISED(efx);
  545. BUG_ON(efx->port_enabled);
  546. rc = efx_nic_flush_queues(efx);
  547. if (rc && EFX_WORKAROUND_7803(efx)) {
  548. /* Schedule a reset to recover from the flush failure. The
  549. * descriptor caches reference memory we're about to free,
  550. * but falcon_reconfigure_mac_wrapper() won't reconnect
  551. * the MACs because of the pending reset. */
  552. netif_err(efx, drv, efx->net_dev,
  553. "Resetting to recover from flush failure\n");
  554. efx_schedule_reset(efx, RESET_TYPE_ALL);
  555. } else if (rc) {
  556. netif_err(efx, drv, efx->net_dev, "failed to flush queues\n");
  557. } else {
  558. netif_dbg(efx, drv, efx->net_dev,
  559. "successfully flushed all queues\n");
  560. }
  561. efx_for_each_channel(channel, efx) {
  562. netif_dbg(channel->efx, drv, channel->efx->net_dev,
  563. "shut down chan %d\n", channel->channel);
  564. efx_for_each_channel_rx_queue(rx_queue, channel)
  565. efx_fini_rx_queue(rx_queue);
  566. efx_for_each_possible_channel_tx_queue(tx_queue, channel)
  567. efx_fini_tx_queue(tx_queue);
  568. efx_fini_eventq(channel);
  569. }
  570. }
  571. static void efx_remove_channel(struct efx_channel *channel)
  572. {
  573. struct efx_tx_queue *tx_queue;
  574. struct efx_rx_queue *rx_queue;
  575. netif_dbg(channel->efx, drv, channel->efx->net_dev,
  576. "destroy chan %d\n", channel->channel);
  577. efx_for_each_channel_rx_queue(rx_queue, channel)
  578. efx_remove_rx_queue(rx_queue);
  579. efx_for_each_possible_channel_tx_queue(tx_queue, channel)
  580. efx_remove_tx_queue(tx_queue);
  581. efx_remove_eventq(channel);
  582. }
  583. static void efx_remove_channels(struct efx_nic *efx)
  584. {
  585. struct efx_channel *channel;
  586. efx_for_each_channel(channel, efx)
  587. efx_remove_channel(channel);
  588. }
  589. int
  590. efx_realloc_channels(struct efx_nic *efx, u32 rxq_entries, u32 txq_entries)
  591. {
  592. struct efx_channel *other_channel[EFX_MAX_CHANNELS], *channel;
  593. u32 old_rxq_entries, old_txq_entries;
  594. unsigned i;
  595. int rc;
  596. efx_stop_all(efx);
  597. efx_fini_channels(efx);
  598. /* Clone channels */
  599. memset(other_channel, 0, sizeof(other_channel));
  600. for (i = 0; i < efx->n_channels; i++) {
  601. channel = efx_alloc_channel(efx, i, efx->channel[i]);
  602. if (!channel) {
  603. rc = -ENOMEM;
  604. goto out;
  605. }
  606. other_channel[i] = channel;
  607. }
  608. /* Swap entry counts and channel pointers */
  609. old_rxq_entries = efx->rxq_entries;
  610. old_txq_entries = efx->txq_entries;
  611. efx->rxq_entries = rxq_entries;
  612. efx->txq_entries = txq_entries;
  613. for (i = 0; i < efx->n_channels; i++) {
  614. channel = efx->channel[i];
  615. efx->channel[i] = other_channel[i];
  616. other_channel[i] = channel;
  617. }
  618. rc = efx_probe_channels(efx);
  619. if (rc)
  620. goto rollback;
  621. efx_init_napi(efx);
  622. /* Destroy old channels */
  623. for (i = 0; i < efx->n_channels; i++) {
  624. efx_fini_napi_channel(other_channel[i]);
  625. efx_remove_channel(other_channel[i]);
  626. }
  627. out:
  628. /* Free unused channel structures */
  629. for (i = 0; i < efx->n_channels; i++)
  630. kfree(other_channel[i]);
  631. efx_init_channels(efx);
  632. efx_start_all(efx);
  633. return rc;
  634. rollback:
  635. /* Swap back */
  636. efx->rxq_entries = old_rxq_entries;
  637. efx->txq_entries = old_txq_entries;
  638. for (i = 0; i < efx->n_channels; i++) {
  639. channel = efx->channel[i];
  640. efx->channel[i] = other_channel[i];
  641. other_channel[i] = channel;
  642. }
  643. goto out;
  644. }
  645. void efx_schedule_slow_fill(struct efx_rx_queue *rx_queue)
  646. {
  647. mod_timer(&rx_queue->slow_fill, jiffies + msecs_to_jiffies(100));
  648. }
  649. /**************************************************************************
  650. *
  651. * Port handling
  652. *
  653. **************************************************************************/
  654. /* This ensures that the kernel is kept informed (via
  655. * netif_carrier_on/off) of the link status, and also maintains the
  656. * link status's stop on the port's TX queue.
  657. */
  658. void efx_link_status_changed(struct efx_nic *efx)
  659. {
  660. struct efx_link_state *link_state = &efx->link_state;
  661. /* SFC Bug 5356: A net_dev notifier is registered, so we must ensure
  662. * that no events are triggered between unregister_netdev() and the
  663. * driver unloading. A more general condition is that NETDEV_CHANGE
  664. * can only be generated between NETDEV_UP and NETDEV_DOWN */
  665. if (!netif_running(efx->net_dev))
  666. return;
  667. if (link_state->up != netif_carrier_ok(efx->net_dev)) {
  668. efx->n_link_state_changes++;
  669. if (link_state->up)
  670. netif_carrier_on(efx->net_dev);
  671. else
  672. netif_carrier_off(efx->net_dev);
  673. }
  674. /* Status message for kernel log */
  675. if (link_state->up) {
  676. netif_info(efx, link, efx->net_dev,
  677. "link up at %uMbps %s-duplex (MTU %d)%s\n",
  678. link_state->speed, link_state->fd ? "full" : "half",
  679. efx->net_dev->mtu,
  680. (efx->promiscuous ? " [PROMISC]" : ""));
  681. } else {
  682. netif_info(efx, link, efx->net_dev, "link down\n");
  683. }
  684. }
  685. void efx_link_set_advertising(struct efx_nic *efx, u32 advertising)
  686. {
  687. efx->link_advertising = advertising;
  688. if (advertising) {
  689. if (advertising & ADVERTISED_Pause)
  690. efx->wanted_fc |= (EFX_FC_TX | EFX_FC_RX);
  691. else
  692. efx->wanted_fc &= ~(EFX_FC_TX | EFX_FC_RX);
  693. if (advertising & ADVERTISED_Asym_Pause)
  694. efx->wanted_fc ^= EFX_FC_TX;
  695. }
  696. }
  697. void efx_link_set_wanted_fc(struct efx_nic *efx, u8 wanted_fc)
  698. {
  699. efx->wanted_fc = wanted_fc;
  700. if (efx->link_advertising) {
  701. if (wanted_fc & EFX_FC_RX)
  702. efx->link_advertising |= (ADVERTISED_Pause |
  703. ADVERTISED_Asym_Pause);
  704. else
  705. efx->link_advertising &= ~(ADVERTISED_Pause |
  706. ADVERTISED_Asym_Pause);
  707. if (wanted_fc & EFX_FC_TX)
  708. efx->link_advertising ^= ADVERTISED_Asym_Pause;
  709. }
  710. }
  711. static void efx_fini_port(struct efx_nic *efx);
  712. /* Push loopback/power/transmit disable settings to the PHY, and reconfigure
  713. * the MAC appropriately. All other PHY configuration changes are pushed
  714. * through phy_op->set_settings(), and pushed asynchronously to the MAC
  715. * through efx_monitor().
  716. *
  717. * Callers must hold the mac_lock
  718. */
  719. int __efx_reconfigure_port(struct efx_nic *efx)
  720. {
  721. enum efx_phy_mode phy_mode;
  722. int rc;
  723. WARN_ON(!mutex_is_locked(&efx->mac_lock));
  724. /* Serialise the promiscuous flag with efx_set_multicast_list. */
  725. if (efx_dev_registered(efx)) {
  726. netif_addr_lock_bh(efx->net_dev);
  727. netif_addr_unlock_bh(efx->net_dev);
  728. }
  729. /* Disable PHY transmit in mac level loopbacks */
  730. phy_mode = efx->phy_mode;
  731. if (LOOPBACK_INTERNAL(efx))
  732. efx->phy_mode |= PHY_MODE_TX_DISABLED;
  733. else
  734. efx->phy_mode &= ~PHY_MODE_TX_DISABLED;
  735. rc = efx->type->reconfigure_port(efx);
  736. if (rc)
  737. efx->phy_mode = phy_mode;
  738. return rc;
  739. }
  740. /* Reinitialise the MAC to pick up new PHY settings, even if the port is
  741. * disabled. */
  742. int efx_reconfigure_port(struct efx_nic *efx)
  743. {
  744. int rc;
  745. EFX_ASSERT_RESET_SERIALISED(efx);
  746. mutex_lock(&efx->mac_lock);
  747. rc = __efx_reconfigure_port(efx);
  748. mutex_unlock(&efx->mac_lock);
  749. return rc;
  750. }
  751. /* Asynchronous work item for changing MAC promiscuity and multicast
  752. * hash. Avoid a drain/rx_ingress enable by reconfiguring the current
  753. * MAC directly. */
  754. static void efx_mac_work(struct work_struct *data)
  755. {
  756. struct efx_nic *efx = container_of(data, struct efx_nic, mac_work);
  757. mutex_lock(&efx->mac_lock);
  758. if (efx->port_enabled) {
  759. efx->type->push_multicast_hash(efx);
  760. efx->mac_op->reconfigure(efx);
  761. }
  762. mutex_unlock(&efx->mac_lock);
  763. }
  764. static int efx_probe_port(struct efx_nic *efx)
  765. {
  766. int rc;
  767. netif_dbg(efx, probe, efx->net_dev, "create port\n");
  768. if (phy_flash_cfg)
  769. efx->phy_mode = PHY_MODE_SPECIAL;
  770. /* Connect up MAC/PHY operations table */
  771. rc = efx->type->probe_port(efx);
  772. if (rc)
  773. return rc;
  774. /* Initialise MAC address to permanent address */
  775. memcpy(efx->net_dev->dev_addr, efx->net_dev->perm_addr, ETH_ALEN);
  776. return 0;
  777. }
  778. static int efx_init_port(struct efx_nic *efx)
  779. {
  780. int rc;
  781. netif_dbg(efx, drv, efx->net_dev, "init port\n");
  782. mutex_lock(&efx->mac_lock);
  783. rc = efx->phy_op->init(efx);
  784. if (rc)
  785. goto fail1;
  786. efx->port_initialized = true;
  787. /* Reconfigure the MAC before creating dma queues (required for
  788. * Falcon/A1 where RX_INGR_EN/TX_DRAIN_EN isn't supported) */
  789. efx->mac_op->reconfigure(efx);
  790. /* Ensure the PHY advertises the correct flow control settings */
  791. rc = efx->phy_op->reconfigure(efx);
  792. if (rc)
  793. goto fail2;
  794. mutex_unlock(&efx->mac_lock);
  795. return 0;
  796. fail2:
  797. efx->phy_op->fini(efx);
  798. fail1:
  799. mutex_unlock(&efx->mac_lock);
  800. return rc;
  801. }
  802. static void efx_start_port(struct efx_nic *efx)
  803. {
  804. netif_dbg(efx, ifup, efx->net_dev, "start port\n");
  805. BUG_ON(efx->port_enabled);
  806. mutex_lock(&efx->mac_lock);
  807. efx->port_enabled = true;
  808. /* efx_mac_work() might have been scheduled after efx_stop_port(),
  809. * and then cancelled by efx_flush_all() */
  810. efx->type->push_multicast_hash(efx);
  811. efx->mac_op->reconfigure(efx);
  812. mutex_unlock(&efx->mac_lock);
  813. }
  814. /* Prevent efx_mac_work() and efx_monitor() from working */
  815. static void efx_stop_port(struct efx_nic *efx)
  816. {
  817. netif_dbg(efx, ifdown, efx->net_dev, "stop port\n");
  818. mutex_lock(&efx->mac_lock);
  819. efx->port_enabled = false;
  820. mutex_unlock(&efx->mac_lock);
  821. /* Serialise against efx_set_multicast_list() */
  822. if (efx_dev_registered(efx)) {
  823. netif_addr_lock_bh(efx->net_dev);
  824. netif_addr_unlock_bh(efx->net_dev);
  825. }
  826. }
  827. static void efx_fini_port(struct efx_nic *efx)
  828. {
  829. netif_dbg(efx, drv, efx->net_dev, "shut down port\n");
  830. if (!efx->port_initialized)
  831. return;
  832. efx->phy_op->fini(efx);
  833. efx->port_initialized = false;
  834. efx->link_state.up = false;
  835. efx_link_status_changed(efx);
  836. }
  837. static void efx_remove_port(struct efx_nic *efx)
  838. {
  839. netif_dbg(efx, drv, efx->net_dev, "destroying port\n");
  840. efx->type->remove_port(efx);
  841. }
  842. /**************************************************************************
  843. *
  844. * NIC handling
  845. *
  846. **************************************************************************/
  847. /* This configures the PCI device to enable I/O and DMA. */
  848. static int efx_init_io(struct efx_nic *efx)
  849. {
  850. struct pci_dev *pci_dev = efx->pci_dev;
  851. dma_addr_t dma_mask = efx->type->max_dma_mask;
  852. int rc;
  853. netif_dbg(efx, probe, efx->net_dev, "initialising I/O\n");
  854. rc = pci_enable_device(pci_dev);
  855. if (rc) {
  856. netif_err(efx, probe, efx->net_dev,
  857. "failed to enable PCI device\n");
  858. goto fail1;
  859. }
  860. pci_set_master(pci_dev);
  861. /* Set the PCI DMA mask. Try all possibilities from our
  862. * genuine mask down to 32 bits, because some architectures
  863. * (e.g. x86_64 with iommu_sac_force set) will allow 40 bit
  864. * masks event though they reject 46 bit masks.
  865. */
  866. while (dma_mask > 0x7fffffffUL) {
  867. if (pci_dma_supported(pci_dev, dma_mask)) {
  868. rc = pci_set_dma_mask(pci_dev, dma_mask);
  869. if (rc == 0)
  870. break;
  871. }
  872. dma_mask >>= 1;
  873. }
  874. if (rc) {
  875. netif_err(efx, probe, efx->net_dev,
  876. "could not find a suitable DMA mask\n");
  877. goto fail2;
  878. }
  879. netif_dbg(efx, probe, efx->net_dev,
  880. "using DMA mask %llx\n", (unsigned long long) dma_mask);
  881. rc = pci_set_consistent_dma_mask(pci_dev, dma_mask);
  882. if (rc) {
  883. /* pci_set_consistent_dma_mask() is not *allowed* to
  884. * fail with a mask that pci_set_dma_mask() accepted,
  885. * but just in case...
  886. */
  887. netif_err(efx, probe, efx->net_dev,
  888. "failed to set consistent DMA mask\n");
  889. goto fail2;
  890. }
  891. efx->membase_phys = pci_resource_start(efx->pci_dev, EFX_MEM_BAR);
  892. rc = pci_request_region(pci_dev, EFX_MEM_BAR, "sfc");
  893. if (rc) {
  894. netif_err(efx, probe, efx->net_dev,
  895. "request for memory BAR failed\n");
  896. rc = -EIO;
  897. goto fail3;
  898. }
  899. efx->membase = ioremap_nocache(efx->membase_phys,
  900. efx->type->mem_map_size);
  901. if (!efx->membase) {
  902. netif_err(efx, probe, efx->net_dev,
  903. "could not map memory BAR at %llx+%x\n",
  904. (unsigned long long)efx->membase_phys,
  905. efx->type->mem_map_size);
  906. rc = -ENOMEM;
  907. goto fail4;
  908. }
  909. netif_dbg(efx, probe, efx->net_dev,
  910. "memory BAR at %llx+%x (virtual %p)\n",
  911. (unsigned long long)efx->membase_phys,
  912. efx->type->mem_map_size, efx->membase);
  913. return 0;
  914. fail4:
  915. pci_release_region(efx->pci_dev, EFX_MEM_BAR);
  916. fail3:
  917. efx->membase_phys = 0;
  918. fail2:
  919. pci_disable_device(efx->pci_dev);
  920. fail1:
  921. return rc;
  922. }
  923. static void efx_fini_io(struct efx_nic *efx)
  924. {
  925. netif_dbg(efx, drv, efx->net_dev, "shutting down I/O\n");
  926. if (efx->membase) {
  927. iounmap(efx->membase);
  928. efx->membase = NULL;
  929. }
  930. if (efx->membase_phys) {
  931. pci_release_region(efx->pci_dev, EFX_MEM_BAR);
  932. efx->membase_phys = 0;
  933. }
  934. pci_disable_device(efx->pci_dev);
  935. }
  936. static int efx_wanted_parallelism(void)
  937. {
  938. cpumask_var_t thread_mask;
  939. int count;
  940. int cpu;
  941. if (rss_cpus)
  942. return rss_cpus;
  943. if (unlikely(!zalloc_cpumask_var(&thread_mask, GFP_KERNEL))) {
  944. printk(KERN_WARNING
  945. "sfc: RSS disabled due to allocation failure\n");
  946. return 1;
  947. }
  948. count = 0;
  949. for_each_online_cpu(cpu) {
  950. if (!cpumask_test_cpu(cpu, thread_mask)) {
  951. ++count;
  952. cpumask_or(thread_mask, thread_mask,
  953. topology_thread_cpumask(cpu));
  954. }
  955. }
  956. free_cpumask_var(thread_mask);
  957. return count;
  958. }
  959. static int
  960. efx_init_rx_cpu_rmap(struct efx_nic *efx, struct msix_entry *xentries)
  961. {
  962. #ifdef CONFIG_RFS_ACCEL
  963. int i, rc;
  964. efx->net_dev->rx_cpu_rmap = alloc_irq_cpu_rmap(efx->n_rx_channels);
  965. if (!efx->net_dev->rx_cpu_rmap)
  966. return -ENOMEM;
  967. for (i = 0; i < efx->n_rx_channels; i++) {
  968. rc = irq_cpu_rmap_add(efx->net_dev->rx_cpu_rmap,
  969. xentries[i].vector);
  970. if (rc) {
  971. free_irq_cpu_rmap(efx->net_dev->rx_cpu_rmap);
  972. efx->net_dev->rx_cpu_rmap = NULL;
  973. return rc;
  974. }
  975. }
  976. #endif
  977. return 0;
  978. }
  979. /* Probe the number and type of interrupts we are able to obtain, and
  980. * the resulting numbers of channels and RX queues.
  981. */
  982. static int efx_probe_interrupts(struct efx_nic *efx)
  983. {
  984. int max_channels =
  985. min_t(int, efx->type->phys_addr_channels, EFX_MAX_CHANNELS);
  986. int rc, i;
  987. if (efx->interrupt_mode == EFX_INT_MODE_MSIX) {
  988. struct msix_entry xentries[EFX_MAX_CHANNELS];
  989. int n_channels;
  990. n_channels = efx_wanted_parallelism();
  991. if (separate_tx_channels)
  992. n_channels *= 2;
  993. n_channels = min(n_channels, max_channels);
  994. for (i = 0; i < n_channels; i++)
  995. xentries[i].entry = i;
  996. rc = pci_enable_msix(efx->pci_dev, xentries, n_channels);
  997. if (rc > 0) {
  998. netif_err(efx, drv, efx->net_dev,
  999. "WARNING: Insufficient MSI-X vectors"
  1000. " available (%d < %d).\n", rc, n_channels);
  1001. netif_err(efx, drv, efx->net_dev,
  1002. "WARNING: Performance may be reduced.\n");
  1003. EFX_BUG_ON_PARANOID(rc >= n_channels);
  1004. n_channels = rc;
  1005. rc = pci_enable_msix(efx->pci_dev, xentries,
  1006. n_channels);
  1007. }
  1008. if (rc == 0) {
  1009. efx->n_channels = n_channels;
  1010. if (separate_tx_channels) {
  1011. efx->n_tx_channels =
  1012. max(efx->n_channels / 2, 1U);
  1013. efx->n_rx_channels =
  1014. max(efx->n_channels -
  1015. efx->n_tx_channels, 1U);
  1016. } else {
  1017. efx->n_tx_channels = efx->n_channels;
  1018. efx->n_rx_channels = efx->n_channels;
  1019. }
  1020. rc = efx_init_rx_cpu_rmap(efx, xentries);
  1021. if (rc) {
  1022. pci_disable_msix(efx->pci_dev);
  1023. return rc;
  1024. }
  1025. for (i = 0; i < n_channels; i++)
  1026. efx_get_channel(efx, i)->irq =
  1027. xentries[i].vector;
  1028. } else {
  1029. /* Fall back to single channel MSI */
  1030. efx->interrupt_mode = EFX_INT_MODE_MSI;
  1031. netif_err(efx, drv, efx->net_dev,
  1032. "could not enable MSI-X\n");
  1033. }
  1034. }
  1035. /* Try single interrupt MSI */
  1036. if (efx->interrupt_mode == EFX_INT_MODE_MSI) {
  1037. efx->n_channels = 1;
  1038. efx->n_rx_channels = 1;
  1039. efx->n_tx_channels = 1;
  1040. rc = pci_enable_msi(efx->pci_dev);
  1041. if (rc == 0) {
  1042. efx_get_channel(efx, 0)->irq = efx->pci_dev->irq;
  1043. } else {
  1044. netif_err(efx, drv, efx->net_dev,
  1045. "could not enable MSI\n");
  1046. efx->interrupt_mode = EFX_INT_MODE_LEGACY;
  1047. }
  1048. }
  1049. /* Assume legacy interrupts */
  1050. if (efx->interrupt_mode == EFX_INT_MODE_LEGACY) {
  1051. efx->n_channels = 1 + (separate_tx_channels ? 1 : 0);
  1052. efx->n_rx_channels = 1;
  1053. efx->n_tx_channels = 1;
  1054. efx->legacy_irq = efx->pci_dev->irq;
  1055. }
  1056. return 0;
  1057. }
  1058. static void efx_remove_interrupts(struct efx_nic *efx)
  1059. {
  1060. struct efx_channel *channel;
  1061. /* Remove MSI/MSI-X interrupts */
  1062. efx_for_each_channel(channel, efx)
  1063. channel->irq = 0;
  1064. pci_disable_msi(efx->pci_dev);
  1065. pci_disable_msix(efx->pci_dev);
  1066. /* Remove legacy interrupt */
  1067. efx->legacy_irq = 0;
  1068. }
  1069. static void efx_set_channels(struct efx_nic *efx)
  1070. {
  1071. struct efx_channel *channel;
  1072. struct efx_tx_queue *tx_queue;
  1073. efx->tx_channel_offset =
  1074. separate_tx_channels ? efx->n_channels - efx->n_tx_channels : 0;
  1075. /* We need to adjust the TX queue numbers if we have separate
  1076. * RX-only and TX-only channels.
  1077. */
  1078. efx_for_each_channel(channel, efx) {
  1079. efx_for_each_channel_tx_queue(tx_queue, channel)
  1080. tx_queue->queue -= (efx->tx_channel_offset *
  1081. EFX_TXQ_TYPES);
  1082. }
  1083. }
  1084. static int efx_probe_nic(struct efx_nic *efx)
  1085. {
  1086. size_t i;
  1087. int rc;
  1088. netif_dbg(efx, probe, efx->net_dev, "creating NIC\n");
  1089. /* Carry out hardware-type specific initialisation */
  1090. rc = efx->type->probe(efx);
  1091. if (rc)
  1092. return rc;
  1093. /* Determine the number of channels and queues by trying to hook
  1094. * in MSI-X interrupts. */
  1095. rc = efx_probe_interrupts(efx);
  1096. if (rc)
  1097. goto fail;
  1098. if (efx->n_channels > 1)
  1099. get_random_bytes(&efx->rx_hash_key, sizeof(efx->rx_hash_key));
  1100. for (i = 0; i < ARRAY_SIZE(efx->rx_indir_table); i++)
  1101. efx->rx_indir_table[i] =
  1102. ethtool_rxfh_indir_default(i, efx->n_rx_channels);
  1103. efx_set_channels(efx);
  1104. netif_set_real_num_tx_queues(efx->net_dev, efx->n_tx_channels);
  1105. netif_set_real_num_rx_queues(efx->net_dev, efx->n_rx_channels);
  1106. /* Initialise the interrupt moderation settings */
  1107. efx_init_irq_moderation(efx, tx_irq_mod_usec, rx_irq_mod_usec, true,
  1108. true);
  1109. return 0;
  1110. fail:
  1111. efx->type->remove(efx);
  1112. return rc;
  1113. }
  1114. static void efx_remove_nic(struct efx_nic *efx)
  1115. {
  1116. netif_dbg(efx, drv, efx->net_dev, "destroying NIC\n");
  1117. efx_remove_interrupts(efx);
  1118. efx->type->remove(efx);
  1119. }
  1120. /**************************************************************************
  1121. *
  1122. * NIC startup/shutdown
  1123. *
  1124. *************************************************************************/
  1125. static int efx_probe_all(struct efx_nic *efx)
  1126. {
  1127. int rc;
  1128. rc = efx_probe_nic(efx);
  1129. if (rc) {
  1130. netif_err(efx, probe, efx->net_dev, "failed to create NIC\n");
  1131. goto fail1;
  1132. }
  1133. rc = efx_probe_port(efx);
  1134. if (rc) {
  1135. netif_err(efx, probe, efx->net_dev, "failed to create port\n");
  1136. goto fail2;
  1137. }
  1138. efx->rxq_entries = efx->txq_entries = EFX_DEFAULT_DMAQ_SIZE;
  1139. rc = efx_probe_channels(efx);
  1140. if (rc)
  1141. goto fail3;
  1142. rc = efx_probe_filters(efx);
  1143. if (rc) {
  1144. netif_err(efx, probe, efx->net_dev,
  1145. "failed to create filter tables\n");
  1146. goto fail4;
  1147. }
  1148. return 0;
  1149. fail4:
  1150. efx_remove_channels(efx);
  1151. fail3:
  1152. efx_remove_port(efx);
  1153. fail2:
  1154. efx_remove_nic(efx);
  1155. fail1:
  1156. return rc;
  1157. }
  1158. /* Called after previous invocation(s) of efx_stop_all, restarts the
  1159. * port, kernel transmit queue, NAPI processing and hardware interrupts,
  1160. * and ensures that the port is scheduled to be reconfigured.
  1161. * This function is safe to call multiple times when the NIC is in any
  1162. * state. */
  1163. static void efx_start_all(struct efx_nic *efx)
  1164. {
  1165. struct efx_channel *channel;
  1166. EFX_ASSERT_RESET_SERIALISED(efx);
  1167. /* Check that it is appropriate to restart the interface. All
  1168. * of these flags are safe to read under just the rtnl lock */
  1169. if (efx->port_enabled)
  1170. return;
  1171. if ((efx->state != STATE_RUNNING) && (efx->state != STATE_INIT))
  1172. return;
  1173. if (efx_dev_registered(efx) && !netif_running(efx->net_dev))
  1174. return;
  1175. /* Mark the port as enabled so port reconfigurations can start, then
  1176. * restart the transmit interface early so the watchdog timer stops */
  1177. efx_start_port(efx);
  1178. if (efx_dev_registered(efx) && netif_device_present(efx->net_dev))
  1179. netif_tx_wake_all_queues(efx->net_dev);
  1180. efx_for_each_channel(channel, efx)
  1181. efx_start_channel(channel);
  1182. if (efx->legacy_irq)
  1183. efx->legacy_irq_enabled = true;
  1184. efx_nic_enable_interrupts(efx);
  1185. /* Switch to event based MCDI completions after enabling interrupts.
  1186. * If a reset has been scheduled, then we need to stay in polled mode.
  1187. * Rather than serialising efx_mcdi_mode_event() [which sleeps] and
  1188. * reset_pending [modified from an atomic context], we instead guarantee
  1189. * that efx_mcdi_mode_poll() isn't reverted erroneously */
  1190. efx_mcdi_mode_event(efx);
  1191. if (efx->reset_pending)
  1192. efx_mcdi_mode_poll(efx);
  1193. /* Start the hardware monitor if there is one. Otherwise (we're link
  1194. * event driven), we have to poll the PHY because after an event queue
  1195. * flush, we could have a missed a link state change */
  1196. if (efx->type->monitor != NULL) {
  1197. queue_delayed_work(efx->workqueue, &efx->monitor_work,
  1198. efx_monitor_interval);
  1199. } else {
  1200. mutex_lock(&efx->mac_lock);
  1201. if (efx->phy_op->poll(efx))
  1202. efx_link_status_changed(efx);
  1203. mutex_unlock(&efx->mac_lock);
  1204. }
  1205. efx->type->start_stats(efx);
  1206. }
  1207. /* Flush all delayed work. Should only be called when no more delayed work
  1208. * will be scheduled. This doesn't flush pending online resets (efx_reset),
  1209. * since we're holding the rtnl_lock at this point. */
  1210. static void efx_flush_all(struct efx_nic *efx)
  1211. {
  1212. /* Make sure the hardware monitor is stopped */
  1213. cancel_delayed_work_sync(&efx->monitor_work);
  1214. /* Stop scheduled port reconfigurations */
  1215. cancel_work_sync(&efx->mac_work);
  1216. }
  1217. /* Quiesce hardware and software without bringing the link down.
  1218. * Safe to call multiple times, when the nic and interface is in any
  1219. * state. The caller is guaranteed to subsequently be in a position
  1220. * to modify any hardware and software state they see fit without
  1221. * taking locks. */
  1222. static void efx_stop_all(struct efx_nic *efx)
  1223. {
  1224. struct efx_channel *channel;
  1225. EFX_ASSERT_RESET_SERIALISED(efx);
  1226. /* port_enabled can be read safely under the rtnl lock */
  1227. if (!efx->port_enabled)
  1228. return;
  1229. efx->type->stop_stats(efx);
  1230. /* Switch to MCDI polling on Siena before disabling interrupts */
  1231. efx_mcdi_mode_poll(efx);
  1232. /* Disable interrupts and wait for ISR to complete */
  1233. efx_nic_disable_interrupts(efx);
  1234. if (efx->legacy_irq) {
  1235. synchronize_irq(efx->legacy_irq);
  1236. efx->legacy_irq_enabled = false;
  1237. }
  1238. efx_for_each_channel(channel, efx) {
  1239. if (channel->irq)
  1240. synchronize_irq(channel->irq);
  1241. }
  1242. /* Stop all NAPI processing and synchronous rx refills */
  1243. efx_for_each_channel(channel, efx)
  1244. efx_stop_channel(channel);
  1245. /* Stop all asynchronous port reconfigurations. Since all
  1246. * event processing has already been stopped, there is no
  1247. * window to loose phy events */
  1248. efx_stop_port(efx);
  1249. /* Flush efx_mac_work(), refill_workqueue, monitor_work */
  1250. efx_flush_all(efx);
  1251. /* Stop the kernel transmit interface late, so the watchdog
  1252. * timer isn't ticking over the flush */
  1253. if (efx_dev_registered(efx)) {
  1254. netif_tx_stop_all_queues(efx->net_dev);
  1255. netif_tx_lock_bh(efx->net_dev);
  1256. netif_tx_unlock_bh(efx->net_dev);
  1257. }
  1258. }
  1259. static void efx_remove_all(struct efx_nic *efx)
  1260. {
  1261. efx_remove_filters(efx);
  1262. efx_remove_channels(efx);
  1263. efx_remove_port(efx);
  1264. efx_remove_nic(efx);
  1265. }
  1266. /**************************************************************************
  1267. *
  1268. * Interrupt moderation
  1269. *
  1270. **************************************************************************/
  1271. static unsigned int irq_mod_ticks(unsigned int usecs, unsigned int resolution)
  1272. {
  1273. if (usecs == 0)
  1274. return 0;
  1275. if (usecs < resolution)
  1276. return 1; /* never round down to 0 */
  1277. return usecs / resolution;
  1278. }
  1279. /* Set interrupt moderation parameters */
  1280. int efx_init_irq_moderation(struct efx_nic *efx, unsigned int tx_usecs,
  1281. unsigned int rx_usecs, bool rx_adaptive,
  1282. bool rx_may_override_tx)
  1283. {
  1284. struct efx_channel *channel;
  1285. unsigned tx_ticks = irq_mod_ticks(tx_usecs, EFX_IRQ_MOD_RESOLUTION);
  1286. unsigned rx_ticks = irq_mod_ticks(rx_usecs, EFX_IRQ_MOD_RESOLUTION);
  1287. EFX_ASSERT_RESET_SERIALISED(efx);
  1288. if (tx_ticks > EFX_IRQ_MOD_MAX || rx_ticks > EFX_IRQ_MOD_MAX)
  1289. return -EINVAL;
  1290. if (tx_ticks != rx_ticks && efx->tx_channel_offset == 0 &&
  1291. !rx_may_override_tx) {
  1292. netif_err(efx, drv, efx->net_dev, "Channels are shared. "
  1293. "RX and TX IRQ moderation must be equal\n");
  1294. return -EINVAL;
  1295. }
  1296. efx->irq_rx_adaptive = rx_adaptive;
  1297. efx->irq_rx_moderation = rx_ticks;
  1298. efx_for_each_channel(channel, efx) {
  1299. if (efx_channel_has_rx_queue(channel))
  1300. channel->irq_moderation = rx_ticks;
  1301. else if (efx_channel_has_tx_queues(channel))
  1302. channel->irq_moderation = tx_ticks;
  1303. }
  1304. return 0;
  1305. }
  1306. void efx_get_irq_moderation(struct efx_nic *efx, unsigned int *tx_usecs,
  1307. unsigned int *rx_usecs, bool *rx_adaptive)
  1308. {
  1309. *rx_adaptive = efx->irq_rx_adaptive;
  1310. *rx_usecs = efx->irq_rx_moderation * EFX_IRQ_MOD_RESOLUTION;
  1311. /* If channels are shared between RX and TX, so is IRQ
  1312. * moderation. Otherwise, IRQ moderation is the same for all
  1313. * TX channels and is not adaptive.
  1314. */
  1315. if (efx->tx_channel_offset == 0)
  1316. *tx_usecs = *rx_usecs;
  1317. else
  1318. *tx_usecs =
  1319. efx->channel[efx->tx_channel_offset]->irq_moderation *
  1320. EFX_IRQ_MOD_RESOLUTION;
  1321. }
  1322. /**************************************************************************
  1323. *
  1324. * Hardware monitor
  1325. *
  1326. **************************************************************************/
  1327. /* Run periodically off the general workqueue */
  1328. static void efx_monitor(struct work_struct *data)
  1329. {
  1330. struct efx_nic *efx = container_of(data, struct efx_nic,
  1331. monitor_work.work);
  1332. netif_vdbg(efx, timer, efx->net_dev,
  1333. "hardware monitor executing on CPU %d\n",
  1334. raw_smp_processor_id());
  1335. BUG_ON(efx->type->monitor == NULL);
  1336. /* If the mac_lock is already held then it is likely a port
  1337. * reconfiguration is already in place, which will likely do
  1338. * most of the work of monitor() anyway. */
  1339. if (mutex_trylock(&efx->mac_lock)) {
  1340. if (efx->port_enabled)
  1341. efx->type->monitor(efx);
  1342. mutex_unlock(&efx->mac_lock);
  1343. }
  1344. queue_delayed_work(efx->workqueue, &efx->monitor_work,
  1345. efx_monitor_interval);
  1346. }
  1347. /**************************************************************************
  1348. *
  1349. * ioctls
  1350. *
  1351. *************************************************************************/
  1352. /* Net device ioctl
  1353. * Context: process, rtnl_lock() held.
  1354. */
  1355. static int efx_ioctl(struct net_device *net_dev, struct ifreq *ifr, int cmd)
  1356. {
  1357. struct efx_nic *efx = netdev_priv(net_dev);
  1358. struct mii_ioctl_data *data = if_mii(ifr);
  1359. EFX_ASSERT_RESET_SERIALISED(efx);
  1360. /* Convert phy_id from older PRTAD/DEVAD format */
  1361. if ((cmd == SIOCGMIIREG || cmd == SIOCSMIIREG) &&
  1362. (data->phy_id & 0xfc00) == 0x0400)
  1363. data->phy_id ^= MDIO_PHY_ID_C45 | 0x0400;
  1364. return mdio_mii_ioctl(&efx->mdio, data, cmd);
  1365. }
  1366. /**************************************************************************
  1367. *
  1368. * NAPI interface
  1369. *
  1370. **************************************************************************/
  1371. static void efx_init_napi(struct efx_nic *efx)
  1372. {
  1373. struct efx_channel *channel;
  1374. efx_for_each_channel(channel, efx) {
  1375. channel->napi_dev = efx->net_dev;
  1376. netif_napi_add(channel->napi_dev, &channel->napi_str,
  1377. efx_poll, napi_weight);
  1378. }
  1379. }
  1380. static void efx_fini_napi_channel(struct efx_channel *channel)
  1381. {
  1382. if (channel->napi_dev)
  1383. netif_napi_del(&channel->napi_str);
  1384. channel->napi_dev = NULL;
  1385. }
  1386. static void efx_fini_napi(struct efx_nic *efx)
  1387. {
  1388. struct efx_channel *channel;
  1389. efx_for_each_channel(channel, efx)
  1390. efx_fini_napi_channel(channel);
  1391. }
  1392. /**************************************************************************
  1393. *
  1394. * Kernel netpoll interface
  1395. *
  1396. *************************************************************************/
  1397. #ifdef CONFIG_NET_POLL_CONTROLLER
  1398. /* Although in the common case interrupts will be disabled, this is not
  1399. * guaranteed. However, all our work happens inside the NAPI callback,
  1400. * so no locking is required.
  1401. */
  1402. static void efx_netpoll(struct net_device *net_dev)
  1403. {
  1404. struct efx_nic *efx = netdev_priv(net_dev);
  1405. struct efx_channel *channel;
  1406. efx_for_each_channel(channel, efx)
  1407. efx_schedule_channel(channel);
  1408. }
  1409. #endif
  1410. /**************************************************************************
  1411. *
  1412. * Kernel net device interface
  1413. *
  1414. *************************************************************************/
  1415. /* Context: process, rtnl_lock() held. */
  1416. static int efx_net_open(struct net_device *net_dev)
  1417. {
  1418. struct efx_nic *efx = netdev_priv(net_dev);
  1419. EFX_ASSERT_RESET_SERIALISED(efx);
  1420. netif_dbg(efx, ifup, efx->net_dev, "opening device on CPU %d\n",
  1421. raw_smp_processor_id());
  1422. if (efx->state == STATE_DISABLED)
  1423. return -EIO;
  1424. if (efx->phy_mode & PHY_MODE_SPECIAL)
  1425. return -EBUSY;
  1426. if (efx_mcdi_poll_reboot(efx) && efx_reset(efx, RESET_TYPE_ALL))
  1427. return -EIO;
  1428. /* Notify the kernel of the link state polled during driver load,
  1429. * before the monitor starts running */
  1430. efx_link_status_changed(efx);
  1431. efx_start_all(efx);
  1432. return 0;
  1433. }
  1434. /* Context: process, rtnl_lock() held.
  1435. * Note that the kernel will ignore our return code; this method
  1436. * should really be a void.
  1437. */
  1438. static int efx_net_stop(struct net_device *net_dev)
  1439. {
  1440. struct efx_nic *efx = netdev_priv(net_dev);
  1441. netif_dbg(efx, ifdown, efx->net_dev, "closing on CPU %d\n",
  1442. raw_smp_processor_id());
  1443. if (efx->state != STATE_DISABLED) {
  1444. /* Stop the device and flush all the channels */
  1445. efx_stop_all(efx);
  1446. efx_fini_channels(efx);
  1447. efx_init_channels(efx);
  1448. }
  1449. return 0;
  1450. }
  1451. /* Context: process, dev_base_lock or RTNL held, non-blocking. */
  1452. static struct rtnl_link_stats64 *efx_net_stats(struct net_device *net_dev, struct rtnl_link_stats64 *stats)
  1453. {
  1454. struct efx_nic *efx = netdev_priv(net_dev);
  1455. struct efx_mac_stats *mac_stats = &efx->mac_stats;
  1456. spin_lock_bh(&efx->stats_lock);
  1457. efx->type->update_stats(efx);
  1458. spin_unlock_bh(&efx->stats_lock);
  1459. stats->rx_packets = mac_stats->rx_packets;
  1460. stats->tx_packets = mac_stats->tx_packets;
  1461. stats->rx_bytes = mac_stats->rx_bytes;
  1462. stats->tx_bytes = mac_stats->tx_bytes;
  1463. stats->rx_dropped = efx->n_rx_nodesc_drop_cnt;
  1464. stats->multicast = mac_stats->rx_multicast;
  1465. stats->collisions = mac_stats->tx_collision;
  1466. stats->rx_length_errors = (mac_stats->rx_gtjumbo +
  1467. mac_stats->rx_length_error);
  1468. stats->rx_crc_errors = mac_stats->rx_bad;
  1469. stats->rx_frame_errors = mac_stats->rx_align_error;
  1470. stats->rx_fifo_errors = mac_stats->rx_overflow;
  1471. stats->rx_missed_errors = mac_stats->rx_missed;
  1472. stats->tx_window_errors = mac_stats->tx_late_collision;
  1473. stats->rx_errors = (stats->rx_length_errors +
  1474. stats->rx_crc_errors +
  1475. stats->rx_frame_errors +
  1476. mac_stats->rx_symbol_error);
  1477. stats->tx_errors = (stats->tx_window_errors +
  1478. mac_stats->tx_bad);
  1479. return stats;
  1480. }
  1481. /* Context: netif_tx_lock held, BHs disabled. */
  1482. static void efx_watchdog(struct net_device *net_dev)
  1483. {
  1484. struct efx_nic *efx = netdev_priv(net_dev);
  1485. netif_err(efx, tx_err, efx->net_dev,
  1486. "TX stuck with port_enabled=%d: resetting channels\n",
  1487. efx->port_enabled);
  1488. efx_schedule_reset(efx, RESET_TYPE_TX_WATCHDOG);
  1489. }
  1490. /* Context: process, rtnl_lock() held. */
  1491. static int efx_change_mtu(struct net_device *net_dev, int new_mtu)
  1492. {
  1493. struct efx_nic *efx = netdev_priv(net_dev);
  1494. int rc = 0;
  1495. EFX_ASSERT_RESET_SERIALISED(efx);
  1496. if (new_mtu > EFX_MAX_MTU)
  1497. return -EINVAL;
  1498. efx_stop_all(efx);
  1499. netif_dbg(efx, drv, efx->net_dev, "changing MTU to %d\n", new_mtu);
  1500. efx_fini_channels(efx);
  1501. mutex_lock(&efx->mac_lock);
  1502. /* Reconfigure the MAC before enabling the dma queues so that
  1503. * the RX buffers don't overflow */
  1504. net_dev->mtu = new_mtu;
  1505. efx->mac_op->reconfigure(efx);
  1506. mutex_unlock(&efx->mac_lock);
  1507. efx_init_channels(efx);
  1508. efx_start_all(efx);
  1509. return rc;
  1510. }
  1511. static int efx_set_mac_address(struct net_device *net_dev, void *data)
  1512. {
  1513. struct efx_nic *efx = netdev_priv(net_dev);
  1514. struct sockaddr *addr = data;
  1515. char *new_addr = addr->sa_data;
  1516. EFX_ASSERT_RESET_SERIALISED(efx);
  1517. if (!is_valid_ether_addr(new_addr)) {
  1518. netif_err(efx, drv, efx->net_dev,
  1519. "invalid ethernet MAC address requested: %pM\n",
  1520. new_addr);
  1521. return -EINVAL;
  1522. }
  1523. memcpy(net_dev->dev_addr, new_addr, net_dev->addr_len);
  1524. /* Reconfigure the MAC */
  1525. mutex_lock(&efx->mac_lock);
  1526. efx->mac_op->reconfigure(efx);
  1527. mutex_unlock(&efx->mac_lock);
  1528. return 0;
  1529. }
  1530. /* Context: netif_addr_lock held, BHs disabled. */
  1531. static void efx_set_multicast_list(struct net_device *net_dev)
  1532. {
  1533. struct efx_nic *efx = netdev_priv(net_dev);
  1534. struct netdev_hw_addr *ha;
  1535. union efx_multicast_hash *mc_hash = &efx->multicast_hash;
  1536. u32 crc;
  1537. int bit;
  1538. efx->promiscuous = !!(net_dev->flags & IFF_PROMISC);
  1539. /* Build multicast hash table */
  1540. if (efx->promiscuous || (net_dev->flags & IFF_ALLMULTI)) {
  1541. memset(mc_hash, 0xff, sizeof(*mc_hash));
  1542. } else {
  1543. memset(mc_hash, 0x00, sizeof(*mc_hash));
  1544. netdev_for_each_mc_addr(ha, net_dev) {
  1545. crc = ether_crc_le(ETH_ALEN, ha->addr);
  1546. bit = crc & (EFX_MCAST_HASH_ENTRIES - 1);
  1547. set_bit_le(bit, mc_hash->byte);
  1548. }
  1549. /* Broadcast packets go through the multicast hash filter.
  1550. * ether_crc_le() of the broadcast address is 0xbe2612ff
  1551. * so we always add bit 0xff to the mask.
  1552. */
  1553. set_bit_le(0xff, mc_hash->byte);
  1554. }
  1555. if (efx->port_enabled)
  1556. queue_work(efx->workqueue, &efx->mac_work);
  1557. /* Otherwise efx_start_port() will do this */
  1558. }
  1559. static int efx_set_features(struct net_device *net_dev, netdev_features_t data)
  1560. {
  1561. struct efx_nic *efx = netdev_priv(net_dev);
  1562. /* If disabling RX n-tuple filtering, clear existing filters */
  1563. if (net_dev->features & ~data & NETIF_F_NTUPLE)
  1564. efx_filter_clear_rx(efx, EFX_FILTER_PRI_MANUAL);
  1565. return 0;
  1566. }
  1567. static const struct net_device_ops efx_netdev_ops = {
  1568. .ndo_open = efx_net_open,
  1569. .ndo_stop = efx_net_stop,
  1570. .ndo_get_stats64 = efx_net_stats,
  1571. .ndo_tx_timeout = efx_watchdog,
  1572. .ndo_start_xmit = efx_hard_start_xmit,
  1573. .ndo_validate_addr = eth_validate_addr,
  1574. .ndo_do_ioctl = efx_ioctl,
  1575. .ndo_change_mtu = efx_change_mtu,
  1576. .ndo_set_mac_address = efx_set_mac_address,
  1577. .ndo_set_rx_mode = efx_set_multicast_list,
  1578. .ndo_set_features = efx_set_features,
  1579. #ifdef CONFIG_NET_POLL_CONTROLLER
  1580. .ndo_poll_controller = efx_netpoll,
  1581. #endif
  1582. .ndo_setup_tc = efx_setup_tc,
  1583. #ifdef CONFIG_RFS_ACCEL
  1584. .ndo_rx_flow_steer = efx_filter_rfs,
  1585. #endif
  1586. };
  1587. static void efx_update_name(struct efx_nic *efx)
  1588. {
  1589. strcpy(efx->name, efx->net_dev->name);
  1590. efx_mtd_rename(efx);
  1591. efx_set_channel_names(efx);
  1592. }
  1593. static int efx_netdev_event(struct notifier_block *this,
  1594. unsigned long event, void *ptr)
  1595. {
  1596. struct net_device *net_dev = ptr;
  1597. if (net_dev->netdev_ops == &efx_netdev_ops &&
  1598. event == NETDEV_CHANGENAME)
  1599. efx_update_name(netdev_priv(net_dev));
  1600. return NOTIFY_DONE;
  1601. }
  1602. static struct notifier_block efx_netdev_notifier = {
  1603. .notifier_call = efx_netdev_event,
  1604. };
  1605. static ssize_t
  1606. show_phy_type(struct device *dev, struct device_attribute *attr, char *buf)
  1607. {
  1608. struct efx_nic *efx = pci_get_drvdata(to_pci_dev(dev));
  1609. return sprintf(buf, "%d\n", efx->phy_type);
  1610. }
  1611. static DEVICE_ATTR(phy_type, 0644, show_phy_type, NULL);
  1612. static int efx_register_netdev(struct efx_nic *efx)
  1613. {
  1614. struct net_device *net_dev = efx->net_dev;
  1615. struct efx_channel *channel;
  1616. int rc;
  1617. net_dev->watchdog_timeo = 5 * HZ;
  1618. net_dev->irq = efx->pci_dev->irq;
  1619. net_dev->netdev_ops = &efx_netdev_ops;
  1620. SET_ETHTOOL_OPS(net_dev, &efx_ethtool_ops);
  1621. /* Clear MAC statistics */
  1622. efx->mac_op->update_stats(efx);
  1623. memset(&efx->mac_stats, 0, sizeof(efx->mac_stats));
  1624. rtnl_lock();
  1625. rc = dev_alloc_name(net_dev, net_dev->name);
  1626. if (rc < 0)
  1627. goto fail_locked;
  1628. efx_update_name(efx);
  1629. rc = register_netdevice(net_dev);
  1630. if (rc)
  1631. goto fail_locked;
  1632. efx_for_each_channel(channel, efx) {
  1633. struct efx_tx_queue *tx_queue;
  1634. efx_for_each_channel_tx_queue(tx_queue, channel)
  1635. efx_init_tx_queue_core_txq(tx_queue);
  1636. }
  1637. /* Always start with carrier off; PHY events will detect the link */
  1638. netif_carrier_off(efx->net_dev);
  1639. rtnl_unlock();
  1640. rc = device_create_file(&efx->pci_dev->dev, &dev_attr_phy_type);
  1641. if (rc) {
  1642. netif_err(efx, drv, efx->net_dev,
  1643. "failed to init net dev attributes\n");
  1644. goto fail_registered;
  1645. }
  1646. return 0;
  1647. fail_locked:
  1648. rtnl_unlock();
  1649. netif_err(efx, drv, efx->net_dev, "could not register net dev\n");
  1650. return rc;
  1651. fail_registered:
  1652. unregister_netdev(net_dev);
  1653. return rc;
  1654. }
  1655. static void efx_unregister_netdev(struct efx_nic *efx)
  1656. {
  1657. struct efx_channel *channel;
  1658. struct efx_tx_queue *tx_queue;
  1659. if (!efx->net_dev)
  1660. return;
  1661. BUG_ON(netdev_priv(efx->net_dev) != efx);
  1662. /* Free up any skbs still remaining. This has to happen before
  1663. * we try to unregister the netdev as running their destructors
  1664. * may be needed to get the device ref. count to 0. */
  1665. efx_for_each_channel(channel, efx) {
  1666. efx_for_each_channel_tx_queue(tx_queue, channel)
  1667. efx_release_tx_buffers(tx_queue);
  1668. }
  1669. if (efx_dev_registered(efx)) {
  1670. strlcpy(efx->name, pci_name(efx->pci_dev), sizeof(efx->name));
  1671. device_remove_file(&efx->pci_dev->dev, &dev_attr_phy_type);
  1672. unregister_netdev(efx->net_dev);
  1673. }
  1674. }
  1675. /**************************************************************************
  1676. *
  1677. * Device reset and suspend
  1678. *
  1679. **************************************************************************/
  1680. /* Tears down the entire software state and most of the hardware state
  1681. * before reset. */
  1682. void efx_reset_down(struct efx_nic *efx, enum reset_type method)
  1683. {
  1684. EFX_ASSERT_RESET_SERIALISED(efx);
  1685. efx_stop_all(efx);
  1686. mutex_lock(&efx->mac_lock);
  1687. efx_fini_channels(efx);
  1688. if (efx->port_initialized && method != RESET_TYPE_INVISIBLE)
  1689. efx->phy_op->fini(efx);
  1690. efx->type->fini(efx);
  1691. }
  1692. /* This function will always ensure that the locks acquired in
  1693. * efx_reset_down() are released. A failure return code indicates
  1694. * that we were unable to reinitialise the hardware, and the
  1695. * driver should be disabled. If ok is false, then the rx and tx
  1696. * engines are not restarted, pending a RESET_DISABLE. */
  1697. int efx_reset_up(struct efx_nic *efx, enum reset_type method, bool ok)
  1698. {
  1699. int rc;
  1700. EFX_ASSERT_RESET_SERIALISED(efx);
  1701. rc = efx->type->init(efx);
  1702. if (rc) {
  1703. netif_err(efx, drv, efx->net_dev, "failed to initialise NIC\n");
  1704. goto fail;
  1705. }
  1706. if (!ok)
  1707. goto fail;
  1708. if (efx->port_initialized && method != RESET_TYPE_INVISIBLE) {
  1709. rc = efx->phy_op->init(efx);
  1710. if (rc)
  1711. goto fail;
  1712. if (efx->phy_op->reconfigure(efx))
  1713. netif_err(efx, drv, efx->net_dev,
  1714. "could not restore PHY settings\n");
  1715. }
  1716. efx->mac_op->reconfigure(efx);
  1717. efx_init_channels(efx);
  1718. efx_restore_filters(efx);
  1719. mutex_unlock(&efx->mac_lock);
  1720. efx_start_all(efx);
  1721. return 0;
  1722. fail:
  1723. efx->port_initialized = false;
  1724. mutex_unlock(&efx->mac_lock);
  1725. return rc;
  1726. }
  1727. /* Reset the NIC using the specified method. Note that the reset may
  1728. * fail, in which case the card will be left in an unusable state.
  1729. *
  1730. * Caller must hold the rtnl_lock.
  1731. */
  1732. int efx_reset(struct efx_nic *efx, enum reset_type method)
  1733. {
  1734. int rc, rc2;
  1735. bool disabled;
  1736. netif_info(efx, drv, efx->net_dev, "resetting (%s)\n",
  1737. RESET_TYPE(method));
  1738. netif_device_detach(efx->net_dev);
  1739. efx_reset_down(efx, method);
  1740. rc = efx->type->reset(efx, method);
  1741. if (rc) {
  1742. netif_err(efx, drv, efx->net_dev, "failed to reset hardware\n");
  1743. goto out;
  1744. }
  1745. /* Clear flags for the scopes we covered. We assume the NIC and
  1746. * driver are now quiescent so that there is no race here.
  1747. */
  1748. efx->reset_pending &= -(1 << (method + 1));
  1749. /* Reinitialise bus-mastering, which may have been turned off before
  1750. * the reset was scheduled. This is still appropriate, even in the
  1751. * RESET_TYPE_DISABLE since this driver generally assumes the hardware
  1752. * can respond to requests. */
  1753. pci_set_master(efx->pci_dev);
  1754. out:
  1755. /* Leave device stopped if necessary */
  1756. disabled = rc || method == RESET_TYPE_DISABLE;
  1757. rc2 = efx_reset_up(efx, method, !disabled);
  1758. if (rc2) {
  1759. disabled = true;
  1760. if (!rc)
  1761. rc = rc2;
  1762. }
  1763. if (disabled) {
  1764. dev_close(efx->net_dev);
  1765. netif_err(efx, drv, efx->net_dev, "has been disabled\n");
  1766. efx->state = STATE_DISABLED;
  1767. } else {
  1768. netif_dbg(efx, drv, efx->net_dev, "reset complete\n");
  1769. netif_device_attach(efx->net_dev);
  1770. }
  1771. return rc;
  1772. }
  1773. /* The worker thread exists so that code that cannot sleep can
  1774. * schedule a reset for later.
  1775. */
  1776. static void efx_reset_work(struct work_struct *data)
  1777. {
  1778. struct efx_nic *efx = container_of(data, struct efx_nic, reset_work);
  1779. unsigned long pending = ACCESS_ONCE(efx->reset_pending);
  1780. if (!pending)
  1781. return;
  1782. /* If we're not RUNNING then don't reset. Leave the reset_pending
  1783. * flags set so that efx_pci_probe_main will be retried */
  1784. if (efx->state != STATE_RUNNING) {
  1785. netif_info(efx, drv, efx->net_dev,
  1786. "scheduled reset quenched. NIC not RUNNING\n");
  1787. return;
  1788. }
  1789. rtnl_lock();
  1790. (void)efx_reset(efx, fls(pending) - 1);
  1791. rtnl_unlock();
  1792. }
  1793. void efx_schedule_reset(struct efx_nic *efx, enum reset_type type)
  1794. {
  1795. enum reset_type method;
  1796. switch (type) {
  1797. case RESET_TYPE_INVISIBLE:
  1798. case RESET_TYPE_ALL:
  1799. case RESET_TYPE_WORLD:
  1800. case RESET_TYPE_DISABLE:
  1801. method = type;
  1802. netif_dbg(efx, drv, efx->net_dev, "scheduling %s reset\n",
  1803. RESET_TYPE(method));
  1804. break;
  1805. default:
  1806. method = efx->type->map_reset_reason(type);
  1807. netif_dbg(efx, drv, efx->net_dev,
  1808. "scheduling %s reset for %s\n",
  1809. RESET_TYPE(method), RESET_TYPE(type));
  1810. break;
  1811. }
  1812. set_bit(method, &efx->reset_pending);
  1813. /* efx_process_channel() will no longer read events once a
  1814. * reset is scheduled. So switch back to poll'd MCDI completions. */
  1815. efx_mcdi_mode_poll(efx);
  1816. queue_work(reset_workqueue, &efx->reset_work);
  1817. }
  1818. /**************************************************************************
  1819. *
  1820. * List of NICs we support
  1821. *
  1822. **************************************************************************/
  1823. /* PCI device ID table */
  1824. static DEFINE_PCI_DEVICE_TABLE(efx_pci_table) = {
  1825. {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE,
  1826. PCI_DEVICE_ID_SOLARFLARE_SFC4000A_0),
  1827. .driver_data = (unsigned long) &falcon_a1_nic_type},
  1828. {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE,
  1829. PCI_DEVICE_ID_SOLARFLARE_SFC4000B),
  1830. .driver_data = (unsigned long) &falcon_b0_nic_type},
  1831. {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0803), /* SFC9020 */
  1832. .driver_data = (unsigned long) &siena_a0_nic_type},
  1833. {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0813), /* SFL9021 */
  1834. .driver_data = (unsigned long) &siena_a0_nic_type},
  1835. {0} /* end of list */
  1836. };
  1837. /**************************************************************************
  1838. *
  1839. * Dummy PHY/MAC operations
  1840. *
  1841. * Can be used for some unimplemented operations
  1842. * Needed so all function pointers are valid and do not have to be tested
  1843. * before use
  1844. *
  1845. **************************************************************************/
  1846. int efx_port_dummy_op_int(struct efx_nic *efx)
  1847. {
  1848. return 0;
  1849. }
  1850. void efx_port_dummy_op_void(struct efx_nic *efx) {}
  1851. static bool efx_port_dummy_op_poll(struct efx_nic *efx)
  1852. {
  1853. return false;
  1854. }
  1855. static const struct efx_phy_operations efx_dummy_phy_operations = {
  1856. .init = efx_port_dummy_op_int,
  1857. .reconfigure = efx_port_dummy_op_int,
  1858. .poll = efx_port_dummy_op_poll,
  1859. .fini = efx_port_dummy_op_void,
  1860. };
  1861. /**************************************************************************
  1862. *
  1863. * Data housekeeping
  1864. *
  1865. **************************************************************************/
  1866. /* This zeroes out and then fills in the invariants in a struct
  1867. * efx_nic (including all sub-structures).
  1868. */
  1869. static int efx_init_struct(struct efx_nic *efx, const struct efx_nic_type *type,
  1870. struct pci_dev *pci_dev, struct net_device *net_dev)
  1871. {
  1872. int i;
  1873. /* Initialise common structures */
  1874. memset(efx, 0, sizeof(*efx));
  1875. spin_lock_init(&efx->biu_lock);
  1876. #ifdef CONFIG_SFC_MTD
  1877. INIT_LIST_HEAD(&efx->mtd_list);
  1878. #endif
  1879. INIT_WORK(&efx->reset_work, efx_reset_work);
  1880. INIT_DELAYED_WORK(&efx->monitor_work, efx_monitor);
  1881. efx->pci_dev = pci_dev;
  1882. efx->msg_enable = debug;
  1883. efx->state = STATE_INIT;
  1884. strlcpy(efx->name, pci_name(pci_dev), sizeof(efx->name));
  1885. efx->net_dev = net_dev;
  1886. spin_lock_init(&efx->stats_lock);
  1887. mutex_init(&efx->mac_lock);
  1888. efx->mac_op = type->default_mac_ops;
  1889. efx->phy_op = &efx_dummy_phy_operations;
  1890. efx->mdio.dev = net_dev;
  1891. INIT_WORK(&efx->mac_work, efx_mac_work);
  1892. for (i = 0; i < EFX_MAX_CHANNELS; i++) {
  1893. efx->channel[i] = efx_alloc_channel(efx, i, NULL);
  1894. if (!efx->channel[i])
  1895. goto fail;
  1896. }
  1897. efx->type = type;
  1898. EFX_BUG_ON_PARANOID(efx->type->phys_addr_channels > EFX_MAX_CHANNELS);
  1899. /* Higher numbered interrupt modes are less capable! */
  1900. efx->interrupt_mode = max(efx->type->max_interrupt_mode,
  1901. interrupt_mode);
  1902. /* Would be good to use the net_dev name, but we're too early */
  1903. snprintf(efx->workqueue_name, sizeof(efx->workqueue_name), "sfc%s",
  1904. pci_name(pci_dev));
  1905. efx->workqueue = create_singlethread_workqueue(efx->workqueue_name);
  1906. if (!efx->workqueue)
  1907. goto fail;
  1908. return 0;
  1909. fail:
  1910. efx_fini_struct(efx);
  1911. return -ENOMEM;
  1912. }
  1913. static void efx_fini_struct(struct efx_nic *efx)
  1914. {
  1915. int i;
  1916. for (i = 0; i < EFX_MAX_CHANNELS; i++)
  1917. kfree(efx->channel[i]);
  1918. if (efx->workqueue) {
  1919. destroy_workqueue(efx->workqueue);
  1920. efx->workqueue = NULL;
  1921. }
  1922. }
  1923. /**************************************************************************
  1924. *
  1925. * PCI interface
  1926. *
  1927. **************************************************************************/
  1928. /* Main body of final NIC shutdown code
  1929. * This is called only at module unload (or hotplug removal).
  1930. */
  1931. static void efx_pci_remove_main(struct efx_nic *efx)
  1932. {
  1933. #ifdef CONFIG_RFS_ACCEL
  1934. free_irq_cpu_rmap(efx->net_dev->rx_cpu_rmap);
  1935. efx->net_dev->rx_cpu_rmap = NULL;
  1936. #endif
  1937. efx_nic_fini_interrupt(efx);
  1938. efx_fini_channels(efx);
  1939. efx_fini_port(efx);
  1940. efx->type->fini(efx);
  1941. efx_fini_napi(efx);
  1942. efx_remove_all(efx);
  1943. }
  1944. /* Final NIC shutdown
  1945. * This is called only at module unload (or hotplug removal).
  1946. */
  1947. static void efx_pci_remove(struct pci_dev *pci_dev)
  1948. {
  1949. struct efx_nic *efx;
  1950. efx = pci_get_drvdata(pci_dev);
  1951. if (!efx)
  1952. return;
  1953. /* Mark the NIC as fini, then stop the interface */
  1954. rtnl_lock();
  1955. efx->state = STATE_FINI;
  1956. dev_close(efx->net_dev);
  1957. /* Allow any queued efx_resets() to complete */
  1958. rtnl_unlock();
  1959. efx_unregister_netdev(efx);
  1960. efx_mtd_remove(efx);
  1961. /* Wait for any scheduled resets to complete. No more will be
  1962. * scheduled from this point because efx_stop_all() has been
  1963. * called, we are no longer registered with driverlink, and
  1964. * the net_device's have been removed. */
  1965. cancel_work_sync(&efx->reset_work);
  1966. efx_pci_remove_main(efx);
  1967. efx_fini_io(efx);
  1968. netif_dbg(efx, drv, efx->net_dev, "shutdown successful\n");
  1969. pci_set_drvdata(pci_dev, NULL);
  1970. efx_fini_struct(efx);
  1971. free_netdev(efx->net_dev);
  1972. };
  1973. /* Main body of NIC initialisation
  1974. * This is called at module load (or hotplug insertion, theoretically).
  1975. */
  1976. static int efx_pci_probe_main(struct efx_nic *efx)
  1977. {
  1978. int rc;
  1979. /* Do start-of-day initialisation */
  1980. rc = efx_probe_all(efx);
  1981. if (rc)
  1982. goto fail1;
  1983. efx_init_napi(efx);
  1984. rc = efx->type->init(efx);
  1985. if (rc) {
  1986. netif_err(efx, probe, efx->net_dev,
  1987. "failed to initialise NIC\n");
  1988. goto fail3;
  1989. }
  1990. rc = efx_init_port(efx);
  1991. if (rc) {
  1992. netif_err(efx, probe, efx->net_dev,
  1993. "failed to initialise port\n");
  1994. goto fail4;
  1995. }
  1996. efx_init_channels(efx);
  1997. rc = efx_nic_init_interrupt(efx);
  1998. if (rc)
  1999. goto fail5;
  2000. return 0;
  2001. fail5:
  2002. efx_fini_channels(efx);
  2003. efx_fini_port(efx);
  2004. fail4:
  2005. efx->type->fini(efx);
  2006. fail3:
  2007. efx_fini_napi(efx);
  2008. efx_remove_all(efx);
  2009. fail1:
  2010. return rc;
  2011. }
  2012. /* NIC initialisation
  2013. *
  2014. * This is called at module load (or hotplug insertion,
  2015. * theoretically). It sets up PCI mappings, tests and resets the NIC,
  2016. * sets up and registers the network devices with the kernel and hooks
  2017. * the interrupt service routine. It does not prepare the device for
  2018. * transmission; this is left to the first time one of the network
  2019. * interfaces is brought up (i.e. efx_net_open).
  2020. */
  2021. static int __devinit efx_pci_probe(struct pci_dev *pci_dev,
  2022. const struct pci_device_id *entry)
  2023. {
  2024. const struct efx_nic_type *type = (const struct efx_nic_type *) entry->driver_data;
  2025. struct net_device *net_dev;
  2026. struct efx_nic *efx;
  2027. int i, rc;
  2028. /* Allocate and initialise a struct net_device and struct efx_nic */
  2029. net_dev = alloc_etherdev_mqs(sizeof(*efx), EFX_MAX_CORE_TX_QUEUES,
  2030. EFX_MAX_RX_QUEUES);
  2031. if (!net_dev)
  2032. return -ENOMEM;
  2033. net_dev->features |= (type->offload_features | NETIF_F_SG |
  2034. NETIF_F_HIGHDMA | NETIF_F_TSO |
  2035. NETIF_F_RXCSUM);
  2036. if (type->offload_features & NETIF_F_V6_CSUM)
  2037. net_dev->features |= NETIF_F_TSO6;
  2038. /* Mask for features that also apply to VLAN devices */
  2039. net_dev->vlan_features |= (NETIF_F_ALL_CSUM | NETIF_F_SG |
  2040. NETIF_F_HIGHDMA | NETIF_F_ALL_TSO |
  2041. NETIF_F_RXCSUM);
  2042. /* All offloads can be toggled */
  2043. net_dev->hw_features = net_dev->features & ~NETIF_F_HIGHDMA;
  2044. efx = netdev_priv(net_dev);
  2045. pci_set_drvdata(pci_dev, efx);
  2046. SET_NETDEV_DEV(net_dev, &pci_dev->dev);
  2047. rc = efx_init_struct(efx, type, pci_dev, net_dev);
  2048. if (rc)
  2049. goto fail1;
  2050. netif_info(efx, probe, efx->net_dev,
  2051. "Solarflare NIC detected\n");
  2052. /* Set up basic I/O (BAR mappings etc) */
  2053. rc = efx_init_io(efx);
  2054. if (rc)
  2055. goto fail2;
  2056. /* No serialisation is required with the reset path because
  2057. * we're in STATE_INIT. */
  2058. for (i = 0; i < 5; i++) {
  2059. rc = efx_pci_probe_main(efx);
  2060. /* Serialise against efx_reset(). No more resets will be
  2061. * scheduled since efx_stop_all() has been called, and we
  2062. * have not and never have been registered with either
  2063. * the rtnetlink or driverlink layers. */
  2064. cancel_work_sync(&efx->reset_work);
  2065. if (rc == 0) {
  2066. if (efx->reset_pending) {
  2067. /* If there was a scheduled reset during
  2068. * probe, the NIC is probably hosed anyway */
  2069. efx_pci_remove_main(efx);
  2070. rc = -EIO;
  2071. } else {
  2072. break;
  2073. }
  2074. }
  2075. /* Retry if a recoverably reset event has been scheduled */
  2076. if (efx->reset_pending &
  2077. ~(1 << RESET_TYPE_INVISIBLE | 1 << RESET_TYPE_ALL) ||
  2078. !efx->reset_pending)
  2079. goto fail3;
  2080. efx->reset_pending = 0;
  2081. }
  2082. if (rc) {
  2083. netif_err(efx, probe, efx->net_dev, "Could not reset NIC\n");
  2084. goto fail4;
  2085. }
  2086. /* Switch to the running state before we expose the device to the OS,
  2087. * so that dev_open()|efx_start_all() will actually start the device */
  2088. efx->state = STATE_RUNNING;
  2089. rc = efx_register_netdev(efx);
  2090. if (rc)
  2091. goto fail5;
  2092. netif_dbg(efx, probe, efx->net_dev, "initialisation successful\n");
  2093. rtnl_lock();
  2094. efx_mtd_probe(efx); /* allowed to fail */
  2095. rtnl_unlock();
  2096. return 0;
  2097. fail5:
  2098. efx_pci_remove_main(efx);
  2099. fail4:
  2100. fail3:
  2101. efx_fini_io(efx);
  2102. fail2:
  2103. efx_fini_struct(efx);
  2104. fail1:
  2105. WARN_ON(rc > 0);
  2106. netif_dbg(efx, drv, efx->net_dev, "initialisation failed. rc=%d\n", rc);
  2107. free_netdev(net_dev);
  2108. return rc;
  2109. }
  2110. static int efx_pm_freeze(struct device *dev)
  2111. {
  2112. struct efx_nic *efx = pci_get_drvdata(to_pci_dev(dev));
  2113. efx->state = STATE_FINI;
  2114. netif_device_detach(efx->net_dev);
  2115. efx_stop_all(efx);
  2116. efx_fini_channels(efx);
  2117. return 0;
  2118. }
  2119. static int efx_pm_thaw(struct device *dev)
  2120. {
  2121. struct efx_nic *efx = pci_get_drvdata(to_pci_dev(dev));
  2122. efx->state = STATE_INIT;
  2123. efx_init_channels(efx);
  2124. mutex_lock(&efx->mac_lock);
  2125. efx->phy_op->reconfigure(efx);
  2126. mutex_unlock(&efx->mac_lock);
  2127. efx_start_all(efx);
  2128. netif_device_attach(efx->net_dev);
  2129. efx->state = STATE_RUNNING;
  2130. efx->type->resume_wol(efx);
  2131. /* Reschedule any quenched resets scheduled during efx_pm_freeze() */
  2132. queue_work(reset_workqueue, &efx->reset_work);
  2133. return 0;
  2134. }
  2135. static int efx_pm_poweroff(struct device *dev)
  2136. {
  2137. struct pci_dev *pci_dev = to_pci_dev(dev);
  2138. struct efx_nic *efx = pci_get_drvdata(pci_dev);
  2139. efx->type->fini(efx);
  2140. efx->reset_pending = 0;
  2141. pci_save_state(pci_dev);
  2142. return pci_set_power_state(pci_dev, PCI_D3hot);
  2143. }
  2144. /* Used for both resume and restore */
  2145. static int efx_pm_resume(struct device *dev)
  2146. {
  2147. struct pci_dev *pci_dev = to_pci_dev(dev);
  2148. struct efx_nic *efx = pci_get_drvdata(pci_dev);
  2149. int rc;
  2150. rc = pci_set_power_state(pci_dev, PCI_D0);
  2151. if (rc)
  2152. return rc;
  2153. pci_restore_state(pci_dev);
  2154. rc = pci_enable_device(pci_dev);
  2155. if (rc)
  2156. return rc;
  2157. pci_set_master(efx->pci_dev);
  2158. rc = efx->type->reset(efx, RESET_TYPE_ALL);
  2159. if (rc)
  2160. return rc;
  2161. rc = efx->type->init(efx);
  2162. if (rc)
  2163. return rc;
  2164. efx_pm_thaw(dev);
  2165. return 0;
  2166. }
  2167. static int efx_pm_suspend(struct device *dev)
  2168. {
  2169. int rc;
  2170. efx_pm_freeze(dev);
  2171. rc = efx_pm_poweroff(dev);
  2172. if (rc)
  2173. efx_pm_resume(dev);
  2174. return rc;
  2175. }
  2176. static const struct dev_pm_ops efx_pm_ops = {
  2177. .suspend = efx_pm_suspend,
  2178. .resume = efx_pm_resume,
  2179. .freeze = efx_pm_freeze,
  2180. .thaw = efx_pm_thaw,
  2181. .poweroff = efx_pm_poweroff,
  2182. .restore = efx_pm_resume,
  2183. };
  2184. static struct pci_driver efx_pci_driver = {
  2185. .name = KBUILD_MODNAME,
  2186. .id_table = efx_pci_table,
  2187. .probe = efx_pci_probe,
  2188. .remove = efx_pci_remove,
  2189. .driver.pm = &efx_pm_ops,
  2190. };
  2191. /**************************************************************************
  2192. *
  2193. * Kernel module interface
  2194. *
  2195. *************************************************************************/
  2196. module_param(interrupt_mode, uint, 0444);
  2197. MODULE_PARM_DESC(interrupt_mode,
  2198. "Interrupt mode (0=>MSIX 1=>MSI 2=>legacy)");
  2199. static int __init efx_init_module(void)
  2200. {
  2201. int rc;
  2202. printk(KERN_INFO "Solarflare NET driver v" EFX_DRIVER_VERSION "\n");
  2203. rc = register_netdevice_notifier(&efx_netdev_notifier);
  2204. if (rc)
  2205. goto err_notifier;
  2206. reset_workqueue = create_singlethread_workqueue("sfc_reset");
  2207. if (!reset_workqueue) {
  2208. rc = -ENOMEM;
  2209. goto err_reset;
  2210. }
  2211. rc = pci_register_driver(&efx_pci_driver);
  2212. if (rc < 0)
  2213. goto err_pci;
  2214. return 0;
  2215. err_pci:
  2216. destroy_workqueue(reset_workqueue);
  2217. err_reset:
  2218. unregister_netdevice_notifier(&efx_netdev_notifier);
  2219. err_notifier:
  2220. return rc;
  2221. }
  2222. static void __exit efx_exit_module(void)
  2223. {
  2224. printk(KERN_INFO "Solarflare NET driver unloading\n");
  2225. pci_unregister_driver(&efx_pci_driver);
  2226. destroy_workqueue(reset_workqueue);
  2227. unregister_netdevice_notifier(&efx_netdev_notifier);
  2228. }
  2229. module_init(efx_init_module);
  2230. module_exit(efx_exit_module);
  2231. MODULE_AUTHOR("Solarflare Communications and "
  2232. "Michael Brown <mbrown@fensystems.co.uk>");
  2233. MODULE_DESCRIPTION("Solarflare Communications network driver");
  2234. MODULE_LICENSE("GPL");
  2235. MODULE_DEVICE_TABLE(pci, efx_pci_table);