efx.c 84 KB

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