efx.c 72 KB

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