efx.c 71 KB

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