efx.c 76 KB

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