efx.c 70 KB

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