efx.c 75 KB

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