efx.c 79 KB

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