efx.c 71 KB

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