efx.c 85 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350
  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. if (!ef4_channel_lock_napi(channel))
  272. return budget;
  273. netif_vdbg(efx, intr, efx->net_dev,
  274. "channel %d NAPI poll executing on CPU %d\n",
  275. channel->channel, raw_smp_processor_id());
  276. spent = ef4_process_channel(channel, budget);
  277. if (spent < budget) {
  278. if (ef4_channel_has_rx_queue(channel) &&
  279. efx->irq_rx_adaptive &&
  280. unlikely(++channel->irq_count == 1000)) {
  281. ef4_update_irq_mod(efx, channel);
  282. }
  283. ef4_filter_rfs_expire(channel);
  284. /* There is no race here; although napi_disable() will
  285. * only wait for napi_complete(), this isn't a problem
  286. * since ef4_nic_eventq_read_ack() will have no effect if
  287. * interrupts have already been disabled.
  288. */
  289. napi_complete(napi);
  290. ef4_nic_eventq_read_ack(channel);
  291. }
  292. ef4_channel_unlock_napi(channel);
  293. return spent;
  294. }
  295. /* Create event queue
  296. * Event queue memory allocations are done only once. If the channel
  297. * is reset, the memory buffer will be reused; this guards against
  298. * errors during channel reset and also simplifies interrupt handling.
  299. */
  300. static int ef4_probe_eventq(struct ef4_channel *channel)
  301. {
  302. struct ef4_nic *efx = channel->efx;
  303. unsigned long entries;
  304. netif_dbg(efx, probe, efx->net_dev,
  305. "chan %d create event queue\n", channel->channel);
  306. /* Build an event queue with room for one event per tx and rx buffer,
  307. * plus some extra for link state events and MCDI completions. */
  308. entries = roundup_pow_of_two(efx->rxq_entries + efx->txq_entries + 128);
  309. EF4_BUG_ON_PARANOID(entries > EF4_MAX_EVQ_SIZE);
  310. channel->eventq_mask = max(entries, EF4_MIN_EVQ_SIZE) - 1;
  311. return ef4_nic_probe_eventq(channel);
  312. }
  313. /* Prepare channel's event queue */
  314. static int ef4_init_eventq(struct ef4_channel *channel)
  315. {
  316. struct ef4_nic *efx = channel->efx;
  317. int rc;
  318. EF4_WARN_ON_PARANOID(channel->eventq_init);
  319. netif_dbg(efx, drv, efx->net_dev,
  320. "chan %d init event queue\n", channel->channel);
  321. rc = ef4_nic_init_eventq(channel);
  322. if (rc == 0) {
  323. efx->type->push_irq_moderation(channel);
  324. channel->eventq_read_ptr = 0;
  325. channel->eventq_init = true;
  326. }
  327. return rc;
  328. }
  329. /* Enable event queue processing and NAPI */
  330. void ef4_start_eventq(struct ef4_channel *channel)
  331. {
  332. netif_dbg(channel->efx, ifup, channel->efx->net_dev,
  333. "chan %d start event queue\n", channel->channel);
  334. /* Make sure the NAPI handler sees the enabled flag set */
  335. channel->enabled = true;
  336. smp_wmb();
  337. ef4_channel_enable(channel);
  338. napi_enable(&channel->napi_str);
  339. ef4_nic_eventq_read_ack(channel);
  340. }
  341. /* Disable event queue processing and NAPI */
  342. void ef4_stop_eventq(struct ef4_channel *channel)
  343. {
  344. if (!channel->enabled)
  345. return;
  346. napi_disable(&channel->napi_str);
  347. while (!ef4_channel_disable(channel))
  348. usleep_range(1000, 20000);
  349. channel->enabled = false;
  350. }
  351. static void ef4_fini_eventq(struct ef4_channel *channel)
  352. {
  353. if (!channel->eventq_init)
  354. return;
  355. netif_dbg(channel->efx, drv, channel->efx->net_dev,
  356. "chan %d fini event queue\n", channel->channel);
  357. ef4_nic_fini_eventq(channel);
  358. channel->eventq_init = false;
  359. }
  360. static void ef4_remove_eventq(struct ef4_channel *channel)
  361. {
  362. netif_dbg(channel->efx, drv, channel->efx->net_dev,
  363. "chan %d remove event queue\n", channel->channel);
  364. ef4_nic_remove_eventq(channel);
  365. }
  366. /**************************************************************************
  367. *
  368. * Channel handling
  369. *
  370. *************************************************************************/
  371. /* Allocate and initialise a channel structure. */
  372. static struct ef4_channel *
  373. ef4_alloc_channel(struct ef4_nic *efx, int i, struct ef4_channel *old_channel)
  374. {
  375. struct ef4_channel *channel;
  376. struct ef4_rx_queue *rx_queue;
  377. struct ef4_tx_queue *tx_queue;
  378. int j;
  379. channel = kzalloc(sizeof(*channel), GFP_KERNEL);
  380. if (!channel)
  381. return NULL;
  382. channel->efx = efx;
  383. channel->channel = i;
  384. channel->type = &ef4_default_channel_type;
  385. for (j = 0; j < EF4_TXQ_TYPES; j++) {
  386. tx_queue = &channel->tx_queue[j];
  387. tx_queue->efx = efx;
  388. tx_queue->queue = i * EF4_TXQ_TYPES + j;
  389. tx_queue->channel = channel;
  390. }
  391. rx_queue = &channel->rx_queue;
  392. rx_queue->efx = efx;
  393. setup_timer(&rx_queue->slow_fill, ef4_rx_slow_fill,
  394. (unsigned long)rx_queue);
  395. return channel;
  396. }
  397. /* Allocate and initialise a channel structure, copying parameters
  398. * (but not resources) from an old channel structure.
  399. */
  400. static struct ef4_channel *
  401. ef4_copy_channel(const struct ef4_channel *old_channel)
  402. {
  403. struct ef4_channel *channel;
  404. struct ef4_rx_queue *rx_queue;
  405. struct ef4_tx_queue *tx_queue;
  406. int j;
  407. channel = kmalloc(sizeof(*channel), GFP_KERNEL);
  408. if (!channel)
  409. return NULL;
  410. *channel = *old_channel;
  411. channel->napi_dev = NULL;
  412. INIT_HLIST_NODE(&channel->napi_str.napi_hash_node);
  413. channel->napi_str.napi_id = 0;
  414. channel->napi_str.state = 0;
  415. memset(&channel->eventq, 0, sizeof(channel->eventq));
  416. for (j = 0; j < EF4_TXQ_TYPES; j++) {
  417. tx_queue = &channel->tx_queue[j];
  418. if (tx_queue->channel)
  419. tx_queue->channel = channel;
  420. tx_queue->buffer = NULL;
  421. memset(&tx_queue->txd, 0, sizeof(tx_queue->txd));
  422. }
  423. rx_queue = &channel->rx_queue;
  424. rx_queue->buffer = NULL;
  425. memset(&rx_queue->rxd, 0, sizeof(rx_queue->rxd));
  426. setup_timer(&rx_queue->slow_fill, ef4_rx_slow_fill,
  427. (unsigned long)rx_queue);
  428. return channel;
  429. }
  430. static int ef4_probe_channel(struct ef4_channel *channel)
  431. {
  432. struct ef4_tx_queue *tx_queue;
  433. struct ef4_rx_queue *rx_queue;
  434. int rc;
  435. netif_dbg(channel->efx, probe, channel->efx->net_dev,
  436. "creating channel %d\n", channel->channel);
  437. rc = channel->type->pre_probe(channel);
  438. if (rc)
  439. goto fail;
  440. rc = ef4_probe_eventq(channel);
  441. if (rc)
  442. goto fail;
  443. ef4_for_each_channel_tx_queue(tx_queue, channel) {
  444. rc = ef4_probe_tx_queue(tx_queue);
  445. if (rc)
  446. goto fail;
  447. }
  448. ef4_for_each_channel_rx_queue(rx_queue, channel) {
  449. rc = ef4_probe_rx_queue(rx_queue);
  450. if (rc)
  451. goto fail;
  452. }
  453. return 0;
  454. fail:
  455. ef4_remove_channel(channel);
  456. return rc;
  457. }
  458. static void
  459. ef4_get_channel_name(struct ef4_channel *channel, char *buf, size_t len)
  460. {
  461. struct ef4_nic *efx = channel->efx;
  462. const char *type;
  463. int number;
  464. number = channel->channel;
  465. if (efx->tx_channel_offset == 0) {
  466. type = "";
  467. } else if (channel->channel < efx->tx_channel_offset) {
  468. type = "-rx";
  469. } else {
  470. type = "-tx";
  471. number -= efx->tx_channel_offset;
  472. }
  473. snprintf(buf, len, "%s%s-%d", efx->name, type, number);
  474. }
  475. static void ef4_set_channel_names(struct ef4_nic *efx)
  476. {
  477. struct ef4_channel *channel;
  478. ef4_for_each_channel(channel, efx)
  479. channel->type->get_name(channel,
  480. efx->msi_context[channel->channel].name,
  481. sizeof(efx->msi_context[0].name));
  482. }
  483. static int ef4_probe_channels(struct ef4_nic *efx)
  484. {
  485. struct ef4_channel *channel;
  486. int rc;
  487. /* Restart special buffer allocation */
  488. efx->next_buffer_table = 0;
  489. /* Probe channels in reverse, so that any 'extra' channels
  490. * use the start of the buffer table. This allows the traffic
  491. * channels to be resized without moving them or wasting the
  492. * entries before them.
  493. */
  494. ef4_for_each_channel_rev(channel, efx) {
  495. rc = ef4_probe_channel(channel);
  496. if (rc) {
  497. netif_err(efx, probe, efx->net_dev,
  498. "failed to create channel %d\n",
  499. channel->channel);
  500. goto fail;
  501. }
  502. }
  503. ef4_set_channel_names(efx);
  504. return 0;
  505. fail:
  506. ef4_remove_channels(efx);
  507. return rc;
  508. }
  509. /* Channels are shutdown and reinitialised whilst the NIC is running
  510. * to propagate configuration changes (mtu, checksum offload), or
  511. * to clear hardware error conditions
  512. */
  513. static void ef4_start_datapath(struct ef4_nic *efx)
  514. {
  515. netdev_features_t old_features = efx->net_dev->features;
  516. bool old_rx_scatter = efx->rx_scatter;
  517. struct ef4_tx_queue *tx_queue;
  518. struct ef4_rx_queue *rx_queue;
  519. struct ef4_channel *channel;
  520. size_t rx_buf_len;
  521. /* Calculate the rx buffer allocation parameters required to
  522. * support the current MTU, including padding for header
  523. * alignment and overruns.
  524. */
  525. efx->rx_dma_len = (efx->rx_prefix_size +
  526. EF4_MAX_FRAME_LEN(efx->net_dev->mtu) +
  527. efx->type->rx_buffer_padding);
  528. rx_buf_len = (sizeof(struct ef4_rx_page_state) +
  529. efx->rx_ip_align + efx->rx_dma_len);
  530. if (rx_buf_len <= PAGE_SIZE) {
  531. efx->rx_scatter = efx->type->always_rx_scatter;
  532. efx->rx_buffer_order = 0;
  533. } else if (efx->type->can_rx_scatter) {
  534. BUILD_BUG_ON(EF4_RX_USR_BUF_SIZE % L1_CACHE_BYTES);
  535. BUILD_BUG_ON(sizeof(struct ef4_rx_page_state) +
  536. 2 * ALIGN(NET_IP_ALIGN + EF4_RX_USR_BUF_SIZE,
  537. EF4_RX_BUF_ALIGNMENT) >
  538. PAGE_SIZE);
  539. efx->rx_scatter = true;
  540. efx->rx_dma_len = EF4_RX_USR_BUF_SIZE;
  541. efx->rx_buffer_order = 0;
  542. } else {
  543. efx->rx_scatter = false;
  544. efx->rx_buffer_order = get_order(rx_buf_len);
  545. }
  546. ef4_rx_config_page_split(efx);
  547. if (efx->rx_buffer_order)
  548. netif_dbg(efx, drv, efx->net_dev,
  549. "RX buf len=%u; page order=%u batch=%u\n",
  550. efx->rx_dma_len, efx->rx_buffer_order,
  551. efx->rx_pages_per_batch);
  552. else
  553. netif_dbg(efx, drv, efx->net_dev,
  554. "RX buf len=%u step=%u bpp=%u; page batch=%u\n",
  555. efx->rx_dma_len, efx->rx_page_buf_step,
  556. efx->rx_bufs_per_page, efx->rx_pages_per_batch);
  557. /* Restore previously fixed features in hw_features and remove
  558. * features which are fixed now
  559. */
  560. efx->net_dev->hw_features |= efx->net_dev->features;
  561. efx->net_dev->hw_features &= ~efx->fixed_features;
  562. efx->net_dev->features |= efx->fixed_features;
  563. if (efx->net_dev->features != old_features)
  564. netdev_features_change(efx->net_dev);
  565. /* RX filters may also have scatter-enabled flags */
  566. if (efx->rx_scatter != old_rx_scatter)
  567. efx->type->filter_update_rx_scatter(efx);
  568. /* We must keep at least one descriptor in a TX ring empty.
  569. * We could avoid this when the queue size does not exactly
  570. * match the hardware ring size, but it's not that important.
  571. * Therefore we stop the queue when one more skb might fill
  572. * the ring completely. We wake it when half way back to
  573. * empty.
  574. */
  575. efx->txq_stop_thresh = efx->txq_entries - ef4_tx_max_skb_descs(efx);
  576. efx->txq_wake_thresh = efx->txq_stop_thresh / 2;
  577. /* Initialise the channels */
  578. ef4_for_each_channel(channel, efx) {
  579. ef4_for_each_channel_tx_queue(tx_queue, channel) {
  580. ef4_init_tx_queue(tx_queue);
  581. atomic_inc(&efx->active_queues);
  582. }
  583. ef4_for_each_channel_rx_queue(rx_queue, channel) {
  584. ef4_init_rx_queue(rx_queue);
  585. atomic_inc(&efx->active_queues);
  586. ef4_stop_eventq(channel);
  587. ef4_fast_push_rx_descriptors(rx_queue, false);
  588. ef4_start_eventq(channel);
  589. }
  590. WARN_ON(channel->rx_pkt_n_frags);
  591. }
  592. if (netif_device_present(efx->net_dev))
  593. netif_tx_wake_all_queues(efx->net_dev);
  594. }
  595. static void ef4_stop_datapath(struct ef4_nic *efx)
  596. {
  597. struct ef4_channel *channel;
  598. struct ef4_tx_queue *tx_queue;
  599. struct ef4_rx_queue *rx_queue;
  600. int rc;
  601. EF4_ASSERT_RESET_SERIALISED(efx);
  602. BUG_ON(efx->port_enabled);
  603. /* Stop RX refill */
  604. ef4_for_each_channel(channel, efx) {
  605. ef4_for_each_channel_rx_queue(rx_queue, channel)
  606. rx_queue->refill_enabled = false;
  607. }
  608. ef4_for_each_channel(channel, efx) {
  609. /* RX packet processing is pipelined, so wait for the
  610. * NAPI handler to complete. At least event queue 0
  611. * might be kept active by non-data events, so don't
  612. * use napi_synchronize() but actually disable NAPI
  613. * temporarily.
  614. */
  615. if (ef4_channel_has_rx_queue(channel)) {
  616. ef4_stop_eventq(channel);
  617. ef4_start_eventq(channel);
  618. }
  619. }
  620. rc = efx->type->fini_dmaq(efx);
  621. if (rc && EF4_WORKAROUND_7803(efx)) {
  622. /* Schedule a reset to recover from the flush failure. The
  623. * descriptor caches reference memory we're about to free,
  624. * but falcon_reconfigure_mac_wrapper() won't reconnect
  625. * the MACs because of the pending reset.
  626. */
  627. netif_err(efx, drv, efx->net_dev,
  628. "Resetting to recover from flush failure\n");
  629. ef4_schedule_reset(efx, RESET_TYPE_ALL);
  630. } else if (rc) {
  631. netif_err(efx, drv, efx->net_dev, "failed to flush queues\n");
  632. } else {
  633. netif_dbg(efx, drv, efx->net_dev,
  634. "successfully flushed all queues\n");
  635. }
  636. ef4_for_each_channel(channel, efx) {
  637. ef4_for_each_channel_rx_queue(rx_queue, channel)
  638. ef4_fini_rx_queue(rx_queue);
  639. ef4_for_each_possible_channel_tx_queue(tx_queue, channel)
  640. ef4_fini_tx_queue(tx_queue);
  641. }
  642. }
  643. static void ef4_remove_channel(struct ef4_channel *channel)
  644. {
  645. struct ef4_tx_queue *tx_queue;
  646. struct ef4_rx_queue *rx_queue;
  647. netif_dbg(channel->efx, drv, channel->efx->net_dev,
  648. "destroy chan %d\n", channel->channel);
  649. ef4_for_each_channel_rx_queue(rx_queue, channel)
  650. ef4_remove_rx_queue(rx_queue);
  651. ef4_for_each_possible_channel_tx_queue(tx_queue, channel)
  652. ef4_remove_tx_queue(tx_queue);
  653. ef4_remove_eventq(channel);
  654. channel->type->post_remove(channel);
  655. }
  656. static void ef4_remove_channels(struct ef4_nic *efx)
  657. {
  658. struct ef4_channel *channel;
  659. ef4_for_each_channel(channel, efx)
  660. ef4_remove_channel(channel);
  661. }
  662. int
  663. ef4_realloc_channels(struct ef4_nic *efx, u32 rxq_entries, u32 txq_entries)
  664. {
  665. struct ef4_channel *other_channel[EF4_MAX_CHANNELS], *channel;
  666. u32 old_rxq_entries, old_txq_entries;
  667. unsigned i, next_buffer_table = 0;
  668. int rc, rc2;
  669. rc = ef4_check_disabled(efx);
  670. if (rc)
  671. return rc;
  672. /* Not all channels should be reallocated. We must avoid
  673. * reallocating their buffer table entries.
  674. */
  675. ef4_for_each_channel(channel, efx) {
  676. struct ef4_rx_queue *rx_queue;
  677. struct ef4_tx_queue *tx_queue;
  678. if (channel->type->copy)
  679. continue;
  680. next_buffer_table = max(next_buffer_table,
  681. channel->eventq.index +
  682. channel->eventq.entries);
  683. ef4_for_each_channel_rx_queue(rx_queue, channel)
  684. next_buffer_table = max(next_buffer_table,
  685. rx_queue->rxd.index +
  686. rx_queue->rxd.entries);
  687. ef4_for_each_channel_tx_queue(tx_queue, channel)
  688. next_buffer_table = max(next_buffer_table,
  689. tx_queue->txd.index +
  690. tx_queue->txd.entries);
  691. }
  692. ef4_device_detach_sync(efx);
  693. ef4_stop_all(efx);
  694. ef4_soft_disable_interrupts(efx);
  695. /* Clone channels (where possible) */
  696. memset(other_channel, 0, sizeof(other_channel));
  697. for (i = 0; i < efx->n_channels; i++) {
  698. channel = efx->channel[i];
  699. if (channel->type->copy)
  700. channel = channel->type->copy(channel);
  701. if (!channel) {
  702. rc = -ENOMEM;
  703. goto out;
  704. }
  705. other_channel[i] = channel;
  706. }
  707. /* Swap entry counts and channel pointers */
  708. old_rxq_entries = efx->rxq_entries;
  709. old_txq_entries = efx->txq_entries;
  710. efx->rxq_entries = rxq_entries;
  711. efx->txq_entries = txq_entries;
  712. for (i = 0; i < efx->n_channels; i++) {
  713. channel = efx->channel[i];
  714. efx->channel[i] = other_channel[i];
  715. other_channel[i] = channel;
  716. }
  717. /* Restart buffer table allocation */
  718. efx->next_buffer_table = next_buffer_table;
  719. for (i = 0; i < efx->n_channels; i++) {
  720. channel = efx->channel[i];
  721. if (!channel->type->copy)
  722. continue;
  723. rc = ef4_probe_channel(channel);
  724. if (rc)
  725. goto rollback;
  726. ef4_init_napi_channel(efx->channel[i]);
  727. }
  728. out:
  729. /* Destroy unused channel structures */
  730. for (i = 0; i < efx->n_channels; i++) {
  731. channel = other_channel[i];
  732. if (channel && channel->type->copy) {
  733. ef4_fini_napi_channel(channel);
  734. ef4_remove_channel(channel);
  735. kfree(channel);
  736. }
  737. }
  738. rc2 = ef4_soft_enable_interrupts(efx);
  739. if (rc2) {
  740. rc = rc ? rc : rc2;
  741. netif_err(efx, drv, efx->net_dev,
  742. "unable to restart interrupts on channel reallocation\n");
  743. ef4_schedule_reset(efx, RESET_TYPE_DISABLE);
  744. } else {
  745. ef4_start_all(efx);
  746. netif_device_attach(efx->net_dev);
  747. }
  748. return rc;
  749. rollback:
  750. /* Swap back */
  751. efx->rxq_entries = old_rxq_entries;
  752. efx->txq_entries = old_txq_entries;
  753. for (i = 0; i < efx->n_channels; i++) {
  754. channel = efx->channel[i];
  755. efx->channel[i] = other_channel[i];
  756. other_channel[i] = channel;
  757. }
  758. goto out;
  759. }
  760. void ef4_schedule_slow_fill(struct ef4_rx_queue *rx_queue)
  761. {
  762. mod_timer(&rx_queue->slow_fill, jiffies + msecs_to_jiffies(100));
  763. }
  764. static const struct ef4_channel_type ef4_default_channel_type = {
  765. .pre_probe = ef4_channel_dummy_op_int,
  766. .post_remove = ef4_channel_dummy_op_void,
  767. .get_name = ef4_get_channel_name,
  768. .copy = ef4_copy_channel,
  769. .keep_eventq = false,
  770. };
  771. int ef4_channel_dummy_op_int(struct ef4_channel *channel)
  772. {
  773. return 0;
  774. }
  775. void ef4_channel_dummy_op_void(struct ef4_channel *channel)
  776. {
  777. }
  778. /**************************************************************************
  779. *
  780. * Port handling
  781. *
  782. **************************************************************************/
  783. /* This ensures that the kernel is kept informed (via
  784. * netif_carrier_on/off) of the link status, and also maintains the
  785. * link status's stop on the port's TX queue.
  786. */
  787. void ef4_link_status_changed(struct ef4_nic *efx)
  788. {
  789. struct ef4_link_state *link_state = &efx->link_state;
  790. /* SFC Bug 5356: A net_dev notifier is registered, so we must ensure
  791. * that no events are triggered between unregister_netdev() and the
  792. * driver unloading. A more general condition is that NETDEV_CHANGE
  793. * can only be generated between NETDEV_UP and NETDEV_DOWN */
  794. if (!netif_running(efx->net_dev))
  795. return;
  796. if (link_state->up != netif_carrier_ok(efx->net_dev)) {
  797. efx->n_link_state_changes++;
  798. if (link_state->up)
  799. netif_carrier_on(efx->net_dev);
  800. else
  801. netif_carrier_off(efx->net_dev);
  802. }
  803. /* Status message for kernel log */
  804. if (link_state->up)
  805. netif_info(efx, link, efx->net_dev,
  806. "link up at %uMbps %s-duplex (MTU %d)\n",
  807. link_state->speed, link_state->fd ? "full" : "half",
  808. efx->net_dev->mtu);
  809. else
  810. netif_info(efx, link, efx->net_dev, "link down\n");
  811. }
  812. void ef4_link_set_advertising(struct ef4_nic *efx, u32 advertising)
  813. {
  814. efx->link_advertising = advertising;
  815. if (advertising) {
  816. if (advertising & ADVERTISED_Pause)
  817. efx->wanted_fc |= (EF4_FC_TX | EF4_FC_RX);
  818. else
  819. efx->wanted_fc &= ~(EF4_FC_TX | EF4_FC_RX);
  820. if (advertising & ADVERTISED_Asym_Pause)
  821. efx->wanted_fc ^= EF4_FC_TX;
  822. }
  823. }
  824. void ef4_link_set_wanted_fc(struct ef4_nic *efx, u8 wanted_fc)
  825. {
  826. efx->wanted_fc = wanted_fc;
  827. if (efx->link_advertising) {
  828. if (wanted_fc & EF4_FC_RX)
  829. efx->link_advertising |= (ADVERTISED_Pause |
  830. ADVERTISED_Asym_Pause);
  831. else
  832. efx->link_advertising &= ~(ADVERTISED_Pause |
  833. ADVERTISED_Asym_Pause);
  834. if (wanted_fc & EF4_FC_TX)
  835. efx->link_advertising ^= ADVERTISED_Asym_Pause;
  836. }
  837. }
  838. static void ef4_fini_port(struct ef4_nic *efx);
  839. /* We assume that efx->type->reconfigure_mac will always try to sync RX
  840. * filters and therefore needs to read-lock the filter table against freeing
  841. */
  842. void ef4_mac_reconfigure(struct ef4_nic *efx)
  843. {
  844. down_read(&efx->filter_sem);
  845. efx->type->reconfigure_mac(efx);
  846. up_read(&efx->filter_sem);
  847. }
  848. /* Push loopback/power/transmit disable settings to the PHY, and reconfigure
  849. * the MAC appropriately. All other PHY configuration changes are pushed
  850. * through phy_op->set_settings(), and pushed asynchronously to the MAC
  851. * through ef4_monitor().
  852. *
  853. * Callers must hold the mac_lock
  854. */
  855. int __ef4_reconfigure_port(struct ef4_nic *efx)
  856. {
  857. enum ef4_phy_mode phy_mode;
  858. int rc;
  859. WARN_ON(!mutex_is_locked(&efx->mac_lock));
  860. /* Disable PHY transmit in mac level loopbacks */
  861. phy_mode = efx->phy_mode;
  862. if (LOOPBACK_INTERNAL(efx))
  863. efx->phy_mode |= PHY_MODE_TX_DISABLED;
  864. else
  865. efx->phy_mode &= ~PHY_MODE_TX_DISABLED;
  866. rc = efx->type->reconfigure_port(efx);
  867. if (rc)
  868. efx->phy_mode = phy_mode;
  869. return rc;
  870. }
  871. /* Reinitialise the MAC to pick up new PHY settings, even if the port is
  872. * disabled. */
  873. int ef4_reconfigure_port(struct ef4_nic *efx)
  874. {
  875. int rc;
  876. EF4_ASSERT_RESET_SERIALISED(efx);
  877. mutex_lock(&efx->mac_lock);
  878. rc = __ef4_reconfigure_port(efx);
  879. mutex_unlock(&efx->mac_lock);
  880. return rc;
  881. }
  882. /* Asynchronous work item for changing MAC promiscuity and multicast
  883. * hash. Avoid a drain/rx_ingress enable by reconfiguring the current
  884. * MAC directly. */
  885. static void ef4_mac_work(struct work_struct *data)
  886. {
  887. struct ef4_nic *efx = container_of(data, struct ef4_nic, mac_work);
  888. mutex_lock(&efx->mac_lock);
  889. if (efx->port_enabled)
  890. ef4_mac_reconfigure(efx);
  891. mutex_unlock(&efx->mac_lock);
  892. }
  893. static int ef4_probe_port(struct ef4_nic *efx)
  894. {
  895. int rc;
  896. netif_dbg(efx, probe, efx->net_dev, "create port\n");
  897. if (phy_flash_cfg)
  898. efx->phy_mode = PHY_MODE_SPECIAL;
  899. /* Connect up MAC/PHY operations table */
  900. rc = efx->type->probe_port(efx);
  901. if (rc)
  902. return rc;
  903. /* Initialise MAC address to permanent address */
  904. ether_addr_copy(efx->net_dev->dev_addr, efx->net_dev->perm_addr);
  905. return 0;
  906. }
  907. static int ef4_init_port(struct ef4_nic *efx)
  908. {
  909. int rc;
  910. netif_dbg(efx, drv, efx->net_dev, "init port\n");
  911. mutex_lock(&efx->mac_lock);
  912. rc = efx->phy_op->init(efx);
  913. if (rc)
  914. goto fail1;
  915. efx->port_initialized = true;
  916. /* Reconfigure the MAC before creating dma queues (required for
  917. * Falcon/A1 where RX_INGR_EN/TX_DRAIN_EN isn't supported) */
  918. ef4_mac_reconfigure(efx);
  919. /* Ensure the PHY advertises the correct flow control settings */
  920. rc = efx->phy_op->reconfigure(efx);
  921. if (rc && rc != -EPERM)
  922. goto fail2;
  923. mutex_unlock(&efx->mac_lock);
  924. return 0;
  925. fail2:
  926. efx->phy_op->fini(efx);
  927. fail1:
  928. mutex_unlock(&efx->mac_lock);
  929. return rc;
  930. }
  931. static void ef4_start_port(struct ef4_nic *efx)
  932. {
  933. netif_dbg(efx, ifup, efx->net_dev, "start port\n");
  934. BUG_ON(efx->port_enabled);
  935. mutex_lock(&efx->mac_lock);
  936. efx->port_enabled = true;
  937. /* Ensure MAC ingress/egress is enabled */
  938. ef4_mac_reconfigure(efx);
  939. mutex_unlock(&efx->mac_lock);
  940. }
  941. /* Cancel work for MAC reconfiguration, periodic hardware monitoring
  942. * and the async self-test, wait for them to finish and prevent them
  943. * being scheduled again. This doesn't cover online resets, which
  944. * should only be cancelled when removing the device.
  945. */
  946. static void ef4_stop_port(struct ef4_nic *efx)
  947. {
  948. netif_dbg(efx, ifdown, efx->net_dev, "stop port\n");
  949. EF4_ASSERT_RESET_SERIALISED(efx);
  950. mutex_lock(&efx->mac_lock);
  951. efx->port_enabled = false;
  952. mutex_unlock(&efx->mac_lock);
  953. /* Serialise against ef4_set_multicast_list() */
  954. netif_addr_lock_bh(efx->net_dev);
  955. netif_addr_unlock_bh(efx->net_dev);
  956. cancel_delayed_work_sync(&efx->monitor_work);
  957. ef4_selftest_async_cancel(efx);
  958. cancel_work_sync(&efx->mac_work);
  959. }
  960. static void ef4_fini_port(struct ef4_nic *efx)
  961. {
  962. netif_dbg(efx, drv, efx->net_dev, "shut down port\n");
  963. if (!efx->port_initialized)
  964. return;
  965. efx->phy_op->fini(efx);
  966. efx->port_initialized = false;
  967. efx->link_state.up = false;
  968. ef4_link_status_changed(efx);
  969. }
  970. static void ef4_remove_port(struct ef4_nic *efx)
  971. {
  972. netif_dbg(efx, drv, efx->net_dev, "destroying port\n");
  973. efx->type->remove_port(efx);
  974. }
  975. /**************************************************************************
  976. *
  977. * NIC handling
  978. *
  979. **************************************************************************/
  980. static LIST_HEAD(ef4_primary_list);
  981. static LIST_HEAD(ef4_unassociated_list);
  982. static bool ef4_same_controller(struct ef4_nic *left, struct ef4_nic *right)
  983. {
  984. return left->type == right->type &&
  985. left->vpd_sn && right->vpd_sn &&
  986. !strcmp(left->vpd_sn, right->vpd_sn);
  987. }
  988. static void ef4_associate(struct ef4_nic *efx)
  989. {
  990. struct ef4_nic *other, *next;
  991. if (efx->primary == efx) {
  992. /* Adding primary function; look for secondaries */
  993. netif_dbg(efx, probe, efx->net_dev, "adding to primary list\n");
  994. list_add_tail(&efx->node, &ef4_primary_list);
  995. list_for_each_entry_safe(other, next, &ef4_unassociated_list,
  996. node) {
  997. if (ef4_same_controller(efx, other)) {
  998. list_del(&other->node);
  999. netif_dbg(other, probe, other->net_dev,
  1000. "moving to secondary list of %s %s\n",
  1001. pci_name(efx->pci_dev),
  1002. efx->net_dev->name);
  1003. list_add_tail(&other->node,
  1004. &efx->secondary_list);
  1005. other->primary = efx;
  1006. }
  1007. }
  1008. } else {
  1009. /* Adding secondary function; look for primary */
  1010. list_for_each_entry(other, &ef4_primary_list, node) {
  1011. if (ef4_same_controller(efx, other)) {
  1012. netif_dbg(efx, probe, efx->net_dev,
  1013. "adding to secondary list of %s %s\n",
  1014. pci_name(other->pci_dev),
  1015. other->net_dev->name);
  1016. list_add_tail(&efx->node,
  1017. &other->secondary_list);
  1018. efx->primary = other;
  1019. return;
  1020. }
  1021. }
  1022. netif_dbg(efx, probe, efx->net_dev,
  1023. "adding to unassociated list\n");
  1024. list_add_tail(&efx->node, &ef4_unassociated_list);
  1025. }
  1026. }
  1027. static void ef4_dissociate(struct ef4_nic *efx)
  1028. {
  1029. struct ef4_nic *other, *next;
  1030. list_del(&efx->node);
  1031. efx->primary = NULL;
  1032. list_for_each_entry_safe(other, next, &efx->secondary_list, node) {
  1033. list_del(&other->node);
  1034. netif_dbg(other, probe, other->net_dev,
  1035. "moving to unassociated list\n");
  1036. list_add_tail(&other->node, &ef4_unassociated_list);
  1037. other->primary = NULL;
  1038. }
  1039. }
  1040. /* This configures the PCI device to enable I/O and DMA. */
  1041. static int ef4_init_io(struct ef4_nic *efx)
  1042. {
  1043. struct pci_dev *pci_dev = efx->pci_dev;
  1044. dma_addr_t dma_mask = efx->type->max_dma_mask;
  1045. unsigned int mem_map_size = efx->type->mem_map_size(efx);
  1046. int rc, bar;
  1047. netif_dbg(efx, probe, efx->net_dev, "initialising I/O\n");
  1048. bar = efx->type->mem_bar;
  1049. rc = pci_enable_device(pci_dev);
  1050. if (rc) {
  1051. netif_err(efx, probe, efx->net_dev,
  1052. "failed to enable PCI device\n");
  1053. goto fail1;
  1054. }
  1055. pci_set_master(pci_dev);
  1056. /* Set the PCI DMA mask. Try all possibilities from our
  1057. * genuine mask down to 32 bits, because some architectures
  1058. * (e.g. x86_64 with iommu_sac_force set) will allow 40 bit
  1059. * masks event though they reject 46 bit masks.
  1060. */
  1061. while (dma_mask > 0x7fffffffUL) {
  1062. rc = dma_set_mask_and_coherent(&pci_dev->dev, dma_mask);
  1063. if (rc == 0)
  1064. break;
  1065. dma_mask >>= 1;
  1066. }
  1067. if (rc) {
  1068. netif_err(efx, probe, efx->net_dev,
  1069. "could not find a suitable DMA mask\n");
  1070. goto fail2;
  1071. }
  1072. netif_dbg(efx, probe, efx->net_dev,
  1073. "using DMA mask %llx\n", (unsigned long long) dma_mask);
  1074. efx->membase_phys = pci_resource_start(efx->pci_dev, bar);
  1075. rc = pci_request_region(pci_dev, bar, "sfc");
  1076. if (rc) {
  1077. netif_err(efx, probe, efx->net_dev,
  1078. "request for memory BAR failed\n");
  1079. rc = -EIO;
  1080. goto fail3;
  1081. }
  1082. efx->membase = ioremap_nocache(efx->membase_phys, mem_map_size);
  1083. if (!efx->membase) {
  1084. netif_err(efx, probe, efx->net_dev,
  1085. "could not map memory BAR at %llx+%x\n",
  1086. (unsigned long long)efx->membase_phys, mem_map_size);
  1087. rc = -ENOMEM;
  1088. goto fail4;
  1089. }
  1090. netif_dbg(efx, probe, efx->net_dev,
  1091. "memory BAR at %llx+%x (virtual %p)\n",
  1092. (unsigned long long)efx->membase_phys, mem_map_size,
  1093. efx->membase);
  1094. return 0;
  1095. fail4:
  1096. pci_release_region(efx->pci_dev, bar);
  1097. fail3:
  1098. efx->membase_phys = 0;
  1099. fail2:
  1100. pci_disable_device(efx->pci_dev);
  1101. fail1:
  1102. return rc;
  1103. }
  1104. static void ef4_fini_io(struct ef4_nic *efx)
  1105. {
  1106. int bar;
  1107. netif_dbg(efx, drv, efx->net_dev, "shutting down I/O\n");
  1108. if (efx->membase) {
  1109. iounmap(efx->membase);
  1110. efx->membase = NULL;
  1111. }
  1112. if (efx->membase_phys) {
  1113. bar = efx->type->mem_bar;
  1114. pci_release_region(efx->pci_dev, bar);
  1115. efx->membase_phys = 0;
  1116. }
  1117. /* Don't disable bus-mastering if VFs are assigned */
  1118. if (!pci_vfs_assigned(efx->pci_dev))
  1119. pci_disable_device(efx->pci_dev);
  1120. }
  1121. void ef4_set_default_rx_indir_table(struct ef4_nic *efx)
  1122. {
  1123. size_t i;
  1124. for (i = 0; i < ARRAY_SIZE(efx->rx_indir_table); i++)
  1125. efx->rx_indir_table[i] =
  1126. ethtool_rxfh_indir_default(i, efx->rss_spread);
  1127. }
  1128. static unsigned int ef4_wanted_parallelism(struct ef4_nic *efx)
  1129. {
  1130. cpumask_var_t thread_mask;
  1131. unsigned int count;
  1132. int cpu;
  1133. if (rss_cpus) {
  1134. count = rss_cpus;
  1135. } else {
  1136. if (unlikely(!zalloc_cpumask_var(&thread_mask, GFP_KERNEL))) {
  1137. netif_warn(efx, probe, efx->net_dev,
  1138. "RSS disabled due to allocation failure\n");
  1139. return 1;
  1140. }
  1141. count = 0;
  1142. for_each_online_cpu(cpu) {
  1143. if (!cpumask_test_cpu(cpu, thread_mask)) {
  1144. ++count;
  1145. cpumask_or(thread_mask, thread_mask,
  1146. topology_sibling_cpumask(cpu));
  1147. }
  1148. }
  1149. free_cpumask_var(thread_mask);
  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. ef4_channel_busy_poll_init(channel);
  1707. }
  1708. static void ef4_init_napi(struct ef4_nic *efx)
  1709. {
  1710. struct ef4_channel *channel;
  1711. ef4_for_each_channel(channel, efx)
  1712. ef4_init_napi_channel(channel);
  1713. }
  1714. static void ef4_fini_napi_channel(struct ef4_channel *channel)
  1715. {
  1716. if (channel->napi_dev)
  1717. netif_napi_del(&channel->napi_str);
  1718. channel->napi_dev = NULL;
  1719. }
  1720. static void ef4_fini_napi(struct ef4_nic *efx)
  1721. {
  1722. struct ef4_channel *channel;
  1723. ef4_for_each_channel(channel, efx)
  1724. ef4_fini_napi_channel(channel);
  1725. }
  1726. /**************************************************************************
  1727. *
  1728. * Kernel netpoll interface
  1729. *
  1730. *************************************************************************/
  1731. #ifdef CONFIG_NET_POLL_CONTROLLER
  1732. /* Although in the common case interrupts will be disabled, this is not
  1733. * guaranteed. However, all our work happens inside the NAPI callback,
  1734. * so no locking is required.
  1735. */
  1736. static void ef4_netpoll(struct net_device *net_dev)
  1737. {
  1738. struct ef4_nic *efx = netdev_priv(net_dev);
  1739. struct ef4_channel *channel;
  1740. ef4_for_each_channel(channel, efx)
  1741. ef4_schedule_channel(channel);
  1742. }
  1743. #endif
  1744. #ifdef CONFIG_NET_RX_BUSY_POLL
  1745. static int ef4_busy_poll(struct napi_struct *napi)
  1746. {
  1747. struct ef4_channel *channel =
  1748. container_of(napi, struct ef4_channel, napi_str);
  1749. struct ef4_nic *efx = channel->efx;
  1750. int budget = 4;
  1751. int old_rx_packets, rx_packets;
  1752. if (!netif_running(efx->net_dev))
  1753. return LL_FLUSH_FAILED;
  1754. if (!ef4_channel_try_lock_poll(channel))
  1755. return LL_FLUSH_BUSY;
  1756. old_rx_packets = channel->rx_queue.rx_packets;
  1757. ef4_process_channel(channel, budget);
  1758. rx_packets = channel->rx_queue.rx_packets - old_rx_packets;
  1759. /* There is no race condition with NAPI here.
  1760. * NAPI will automatically be rescheduled if it yielded during busy
  1761. * polling, because it was not able to take the lock and thus returned
  1762. * the full budget.
  1763. */
  1764. ef4_channel_unlock_poll(channel);
  1765. return rx_packets;
  1766. }
  1767. #endif
  1768. /**************************************************************************
  1769. *
  1770. * Kernel net device interface
  1771. *
  1772. *************************************************************************/
  1773. /* Context: process, rtnl_lock() held. */
  1774. int ef4_net_open(struct net_device *net_dev)
  1775. {
  1776. struct ef4_nic *efx = netdev_priv(net_dev);
  1777. int rc;
  1778. netif_dbg(efx, ifup, efx->net_dev, "opening device on CPU %d\n",
  1779. raw_smp_processor_id());
  1780. rc = ef4_check_disabled(efx);
  1781. if (rc)
  1782. return rc;
  1783. if (efx->phy_mode & PHY_MODE_SPECIAL)
  1784. return -EBUSY;
  1785. /* Notify the kernel of the link state polled during driver load,
  1786. * before the monitor starts running */
  1787. ef4_link_status_changed(efx);
  1788. ef4_start_all(efx);
  1789. ef4_selftest_async_start(efx);
  1790. return 0;
  1791. }
  1792. /* Context: process, rtnl_lock() held.
  1793. * Note that the kernel will ignore our return code; this method
  1794. * should really be a void.
  1795. */
  1796. int ef4_net_stop(struct net_device *net_dev)
  1797. {
  1798. struct ef4_nic *efx = netdev_priv(net_dev);
  1799. netif_dbg(efx, ifdown, efx->net_dev, "closing on CPU %d\n",
  1800. raw_smp_processor_id());
  1801. /* Stop the device and flush all the channels */
  1802. ef4_stop_all(efx);
  1803. return 0;
  1804. }
  1805. /* Context: process, dev_base_lock or RTNL held, non-blocking. */
  1806. static struct rtnl_link_stats64 *ef4_net_stats(struct net_device *net_dev,
  1807. struct rtnl_link_stats64 *stats)
  1808. {
  1809. struct ef4_nic *efx = netdev_priv(net_dev);
  1810. spin_lock_bh(&efx->stats_lock);
  1811. efx->type->update_stats(efx, NULL, stats);
  1812. spin_unlock_bh(&efx->stats_lock);
  1813. return stats;
  1814. }
  1815. /* Context: netif_tx_lock held, BHs disabled. */
  1816. static void ef4_watchdog(struct net_device *net_dev)
  1817. {
  1818. struct ef4_nic *efx = netdev_priv(net_dev);
  1819. netif_err(efx, tx_err, efx->net_dev,
  1820. "TX stuck with port_enabled=%d: resetting channels\n",
  1821. efx->port_enabled);
  1822. ef4_schedule_reset(efx, RESET_TYPE_TX_WATCHDOG);
  1823. }
  1824. /* Context: process, rtnl_lock() held. */
  1825. static int ef4_change_mtu(struct net_device *net_dev, int new_mtu)
  1826. {
  1827. struct ef4_nic *efx = netdev_priv(net_dev);
  1828. int rc;
  1829. rc = ef4_check_disabled(efx);
  1830. if (rc)
  1831. return rc;
  1832. netif_dbg(efx, drv, efx->net_dev, "changing MTU to %d\n", new_mtu);
  1833. ef4_device_detach_sync(efx);
  1834. ef4_stop_all(efx);
  1835. mutex_lock(&efx->mac_lock);
  1836. net_dev->mtu = new_mtu;
  1837. ef4_mac_reconfigure(efx);
  1838. mutex_unlock(&efx->mac_lock);
  1839. ef4_start_all(efx);
  1840. netif_device_attach(efx->net_dev);
  1841. return 0;
  1842. }
  1843. static int ef4_set_mac_address(struct net_device *net_dev, void *data)
  1844. {
  1845. struct ef4_nic *efx = netdev_priv(net_dev);
  1846. struct sockaddr *addr = data;
  1847. u8 *new_addr = addr->sa_data;
  1848. u8 old_addr[6];
  1849. int rc;
  1850. if (!is_valid_ether_addr(new_addr)) {
  1851. netif_err(efx, drv, efx->net_dev,
  1852. "invalid ethernet MAC address requested: %pM\n",
  1853. new_addr);
  1854. return -EADDRNOTAVAIL;
  1855. }
  1856. /* save old address */
  1857. ether_addr_copy(old_addr, net_dev->dev_addr);
  1858. ether_addr_copy(net_dev->dev_addr, new_addr);
  1859. if (efx->type->set_mac_address) {
  1860. rc = efx->type->set_mac_address(efx);
  1861. if (rc) {
  1862. ether_addr_copy(net_dev->dev_addr, old_addr);
  1863. return rc;
  1864. }
  1865. }
  1866. /* Reconfigure the MAC */
  1867. mutex_lock(&efx->mac_lock);
  1868. ef4_mac_reconfigure(efx);
  1869. mutex_unlock(&efx->mac_lock);
  1870. return 0;
  1871. }
  1872. /* Context: netif_addr_lock held, BHs disabled. */
  1873. static void ef4_set_rx_mode(struct net_device *net_dev)
  1874. {
  1875. struct ef4_nic *efx = netdev_priv(net_dev);
  1876. if (efx->port_enabled)
  1877. queue_work(efx->workqueue, &efx->mac_work);
  1878. /* Otherwise ef4_start_port() will do this */
  1879. }
  1880. static int ef4_set_features(struct net_device *net_dev, netdev_features_t data)
  1881. {
  1882. struct ef4_nic *efx = netdev_priv(net_dev);
  1883. int rc;
  1884. /* If disabling RX n-tuple filtering, clear existing filters */
  1885. if (net_dev->features & ~data & NETIF_F_NTUPLE) {
  1886. rc = efx->type->filter_clear_rx(efx, EF4_FILTER_PRI_MANUAL);
  1887. if (rc)
  1888. return rc;
  1889. }
  1890. /* If Rx VLAN filter is changed, update filters via mac_reconfigure */
  1891. if ((net_dev->features ^ data) & NETIF_F_HW_VLAN_CTAG_FILTER) {
  1892. /* ef4_set_rx_mode() will schedule MAC work to update filters
  1893. * when a new features are finally set in net_dev.
  1894. */
  1895. ef4_set_rx_mode(net_dev);
  1896. }
  1897. return 0;
  1898. }
  1899. static const struct net_device_ops ef4_netdev_ops = {
  1900. .ndo_open = ef4_net_open,
  1901. .ndo_stop = ef4_net_stop,
  1902. .ndo_get_stats64 = ef4_net_stats,
  1903. .ndo_tx_timeout = ef4_watchdog,
  1904. .ndo_start_xmit = ef4_hard_start_xmit,
  1905. .ndo_validate_addr = eth_validate_addr,
  1906. .ndo_do_ioctl = ef4_ioctl,
  1907. .ndo_change_mtu = ef4_change_mtu,
  1908. .ndo_set_mac_address = ef4_set_mac_address,
  1909. .ndo_set_rx_mode = ef4_set_rx_mode,
  1910. .ndo_set_features = ef4_set_features,
  1911. #ifdef CONFIG_NET_POLL_CONTROLLER
  1912. .ndo_poll_controller = ef4_netpoll,
  1913. #endif
  1914. .ndo_setup_tc = ef4_setup_tc,
  1915. #ifdef CONFIG_NET_RX_BUSY_POLL
  1916. .ndo_busy_poll = ef4_busy_poll,
  1917. #endif
  1918. #ifdef CONFIG_RFS_ACCEL
  1919. .ndo_rx_flow_steer = ef4_filter_rfs,
  1920. #endif
  1921. };
  1922. static void ef4_update_name(struct ef4_nic *efx)
  1923. {
  1924. strcpy(efx->name, efx->net_dev->name);
  1925. ef4_mtd_rename(efx);
  1926. ef4_set_channel_names(efx);
  1927. }
  1928. static int ef4_netdev_event(struct notifier_block *this,
  1929. unsigned long event, void *ptr)
  1930. {
  1931. struct net_device *net_dev = netdev_notifier_info_to_dev(ptr);
  1932. if ((net_dev->netdev_ops == &ef4_netdev_ops) &&
  1933. event == NETDEV_CHANGENAME)
  1934. ef4_update_name(netdev_priv(net_dev));
  1935. return NOTIFY_DONE;
  1936. }
  1937. static struct notifier_block ef4_netdev_notifier = {
  1938. .notifier_call = ef4_netdev_event,
  1939. };
  1940. static ssize_t
  1941. show_phy_type(struct device *dev, struct device_attribute *attr, char *buf)
  1942. {
  1943. struct ef4_nic *efx = pci_get_drvdata(to_pci_dev(dev));
  1944. return sprintf(buf, "%d\n", efx->phy_type);
  1945. }
  1946. static DEVICE_ATTR(phy_type, 0444, show_phy_type, NULL);
  1947. static int ef4_register_netdev(struct ef4_nic *efx)
  1948. {
  1949. struct net_device *net_dev = efx->net_dev;
  1950. struct ef4_channel *channel;
  1951. int rc;
  1952. net_dev->watchdog_timeo = 5 * HZ;
  1953. net_dev->irq = efx->pci_dev->irq;
  1954. net_dev->netdev_ops = &ef4_netdev_ops;
  1955. net_dev->ethtool_ops = &ef4_ethtool_ops;
  1956. net_dev->gso_max_segs = EF4_TSO_MAX_SEGS;
  1957. net_dev->min_mtu = EF4_MIN_MTU;
  1958. net_dev->max_mtu = EF4_MAX_MTU;
  1959. rtnl_lock();
  1960. /* Enable resets to be scheduled and check whether any were
  1961. * already requested. If so, the NIC is probably hosed so we
  1962. * abort.
  1963. */
  1964. efx->state = STATE_READY;
  1965. smp_mb(); /* ensure we change state before checking reset_pending */
  1966. if (efx->reset_pending) {
  1967. netif_err(efx, probe, efx->net_dev,
  1968. "aborting probe due to scheduled reset\n");
  1969. rc = -EIO;
  1970. goto fail_locked;
  1971. }
  1972. rc = dev_alloc_name(net_dev, net_dev->name);
  1973. if (rc < 0)
  1974. goto fail_locked;
  1975. ef4_update_name(efx);
  1976. /* Always start with carrier off; PHY events will detect the link */
  1977. netif_carrier_off(net_dev);
  1978. rc = register_netdevice(net_dev);
  1979. if (rc)
  1980. goto fail_locked;
  1981. ef4_for_each_channel(channel, efx) {
  1982. struct ef4_tx_queue *tx_queue;
  1983. ef4_for_each_channel_tx_queue(tx_queue, channel)
  1984. ef4_init_tx_queue_core_txq(tx_queue);
  1985. }
  1986. ef4_associate(efx);
  1987. rtnl_unlock();
  1988. rc = device_create_file(&efx->pci_dev->dev, &dev_attr_phy_type);
  1989. if (rc) {
  1990. netif_err(efx, drv, efx->net_dev,
  1991. "failed to init net dev attributes\n");
  1992. goto fail_registered;
  1993. }
  1994. return 0;
  1995. fail_registered:
  1996. rtnl_lock();
  1997. ef4_dissociate(efx);
  1998. unregister_netdevice(net_dev);
  1999. fail_locked:
  2000. efx->state = STATE_UNINIT;
  2001. rtnl_unlock();
  2002. netif_err(efx, drv, efx->net_dev, "could not register net dev\n");
  2003. return rc;
  2004. }
  2005. static void ef4_unregister_netdev(struct ef4_nic *efx)
  2006. {
  2007. if (!efx->net_dev)
  2008. return;
  2009. BUG_ON(netdev_priv(efx->net_dev) != efx);
  2010. if (ef4_dev_registered(efx)) {
  2011. strlcpy(efx->name, pci_name(efx->pci_dev), sizeof(efx->name));
  2012. device_remove_file(&efx->pci_dev->dev, &dev_attr_phy_type);
  2013. unregister_netdev(efx->net_dev);
  2014. }
  2015. }
  2016. /**************************************************************************
  2017. *
  2018. * Device reset and suspend
  2019. *
  2020. **************************************************************************/
  2021. /* Tears down the entire software state and most of the hardware state
  2022. * before reset. */
  2023. void ef4_reset_down(struct ef4_nic *efx, enum reset_type method)
  2024. {
  2025. EF4_ASSERT_RESET_SERIALISED(efx);
  2026. ef4_stop_all(efx);
  2027. ef4_disable_interrupts(efx);
  2028. mutex_lock(&efx->mac_lock);
  2029. if (efx->port_initialized && method != RESET_TYPE_INVISIBLE &&
  2030. method != RESET_TYPE_DATAPATH)
  2031. efx->phy_op->fini(efx);
  2032. efx->type->fini(efx);
  2033. }
  2034. /* This function will always ensure that the locks acquired in
  2035. * ef4_reset_down() are released. A failure return code indicates
  2036. * that we were unable to reinitialise the hardware, and the
  2037. * driver should be disabled. If ok is false, then the rx and tx
  2038. * engines are not restarted, pending a RESET_DISABLE. */
  2039. int ef4_reset_up(struct ef4_nic *efx, enum reset_type method, bool ok)
  2040. {
  2041. int rc;
  2042. EF4_ASSERT_RESET_SERIALISED(efx);
  2043. /* Ensure that SRAM is initialised even if we're disabling the device */
  2044. rc = efx->type->init(efx);
  2045. if (rc) {
  2046. netif_err(efx, drv, efx->net_dev, "failed to initialise NIC\n");
  2047. goto fail;
  2048. }
  2049. if (!ok)
  2050. goto fail;
  2051. if (efx->port_initialized && method != RESET_TYPE_INVISIBLE &&
  2052. method != RESET_TYPE_DATAPATH) {
  2053. rc = efx->phy_op->init(efx);
  2054. if (rc)
  2055. goto fail;
  2056. rc = efx->phy_op->reconfigure(efx);
  2057. if (rc && rc != -EPERM)
  2058. netif_err(efx, drv, efx->net_dev,
  2059. "could not restore PHY settings\n");
  2060. }
  2061. rc = ef4_enable_interrupts(efx);
  2062. if (rc)
  2063. goto fail;
  2064. down_read(&efx->filter_sem);
  2065. ef4_restore_filters(efx);
  2066. up_read(&efx->filter_sem);
  2067. mutex_unlock(&efx->mac_lock);
  2068. ef4_start_all(efx);
  2069. return 0;
  2070. fail:
  2071. efx->port_initialized = false;
  2072. mutex_unlock(&efx->mac_lock);
  2073. return rc;
  2074. }
  2075. /* Reset the NIC using the specified method. Note that the reset may
  2076. * fail, in which case the card will be left in an unusable state.
  2077. *
  2078. * Caller must hold the rtnl_lock.
  2079. */
  2080. int ef4_reset(struct ef4_nic *efx, enum reset_type method)
  2081. {
  2082. int rc, rc2;
  2083. bool disabled;
  2084. netif_info(efx, drv, efx->net_dev, "resetting (%s)\n",
  2085. RESET_TYPE(method));
  2086. ef4_device_detach_sync(efx);
  2087. ef4_reset_down(efx, method);
  2088. rc = efx->type->reset(efx, method);
  2089. if (rc) {
  2090. netif_err(efx, drv, efx->net_dev, "failed to reset hardware\n");
  2091. goto out;
  2092. }
  2093. /* Clear flags for the scopes we covered. We assume the NIC and
  2094. * driver are now quiescent so that there is no race here.
  2095. */
  2096. if (method < RESET_TYPE_MAX_METHOD)
  2097. efx->reset_pending &= -(1 << (method + 1));
  2098. else /* it doesn't fit into the well-ordered scope hierarchy */
  2099. __clear_bit(method, &efx->reset_pending);
  2100. /* Reinitialise bus-mastering, which may have been turned off before
  2101. * the reset was scheduled. This is still appropriate, even in the
  2102. * RESET_TYPE_DISABLE since this driver generally assumes the hardware
  2103. * can respond to requests. */
  2104. pci_set_master(efx->pci_dev);
  2105. out:
  2106. /* Leave device stopped if necessary */
  2107. disabled = rc ||
  2108. method == RESET_TYPE_DISABLE ||
  2109. method == RESET_TYPE_RECOVER_OR_DISABLE;
  2110. rc2 = ef4_reset_up(efx, method, !disabled);
  2111. if (rc2) {
  2112. disabled = true;
  2113. if (!rc)
  2114. rc = rc2;
  2115. }
  2116. if (disabled) {
  2117. dev_close(efx->net_dev);
  2118. netif_err(efx, drv, efx->net_dev, "has been disabled\n");
  2119. efx->state = STATE_DISABLED;
  2120. } else {
  2121. netif_dbg(efx, drv, efx->net_dev, "reset complete\n");
  2122. netif_device_attach(efx->net_dev);
  2123. }
  2124. return rc;
  2125. }
  2126. /* Try recovery mechanisms.
  2127. * For now only EEH is supported.
  2128. * Returns 0 if the recovery mechanisms are unsuccessful.
  2129. * Returns a non-zero value otherwise.
  2130. */
  2131. int ef4_try_recovery(struct ef4_nic *efx)
  2132. {
  2133. #ifdef CONFIG_EEH
  2134. /* A PCI error can occur and not be seen by EEH because nothing
  2135. * happens on the PCI bus. In this case the driver may fail and
  2136. * schedule a 'recover or reset', leading to this recovery handler.
  2137. * Manually call the eeh failure check function.
  2138. */
  2139. struct eeh_dev *eehdev = pci_dev_to_eeh_dev(efx->pci_dev);
  2140. if (eeh_dev_check_failure(eehdev)) {
  2141. /* The EEH mechanisms will handle the error and reset the
  2142. * device if necessary.
  2143. */
  2144. return 1;
  2145. }
  2146. #endif
  2147. return 0;
  2148. }
  2149. /* The worker thread exists so that code that cannot sleep can
  2150. * schedule a reset for later.
  2151. */
  2152. static void ef4_reset_work(struct work_struct *data)
  2153. {
  2154. struct ef4_nic *efx = container_of(data, struct ef4_nic, reset_work);
  2155. unsigned long pending;
  2156. enum reset_type method;
  2157. pending = ACCESS_ONCE(efx->reset_pending);
  2158. method = fls(pending) - 1;
  2159. if ((method == RESET_TYPE_RECOVER_OR_DISABLE ||
  2160. method == RESET_TYPE_RECOVER_OR_ALL) &&
  2161. ef4_try_recovery(efx))
  2162. return;
  2163. if (!pending)
  2164. return;
  2165. rtnl_lock();
  2166. /* We checked the state in ef4_schedule_reset() but it may
  2167. * have changed by now. Now that we have the RTNL lock,
  2168. * it cannot change again.
  2169. */
  2170. if (efx->state == STATE_READY)
  2171. (void)ef4_reset(efx, method);
  2172. rtnl_unlock();
  2173. }
  2174. void ef4_schedule_reset(struct ef4_nic *efx, enum reset_type type)
  2175. {
  2176. enum reset_type method;
  2177. if (efx->state == STATE_RECOVERY) {
  2178. netif_dbg(efx, drv, efx->net_dev,
  2179. "recovering: skip scheduling %s reset\n",
  2180. RESET_TYPE(type));
  2181. return;
  2182. }
  2183. switch (type) {
  2184. case RESET_TYPE_INVISIBLE:
  2185. case RESET_TYPE_ALL:
  2186. case RESET_TYPE_RECOVER_OR_ALL:
  2187. case RESET_TYPE_WORLD:
  2188. case RESET_TYPE_DISABLE:
  2189. case RESET_TYPE_RECOVER_OR_DISABLE:
  2190. case RESET_TYPE_DATAPATH:
  2191. method = type;
  2192. netif_dbg(efx, drv, efx->net_dev, "scheduling %s reset\n",
  2193. RESET_TYPE(method));
  2194. break;
  2195. default:
  2196. method = efx->type->map_reset_reason(type);
  2197. netif_dbg(efx, drv, efx->net_dev,
  2198. "scheduling %s reset for %s\n",
  2199. RESET_TYPE(method), RESET_TYPE(type));
  2200. break;
  2201. }
  2202. set_bit(method, &efx->reset_pending);
  2203. smp_mb(); /* ensure we change reset_pending before checking state */
  2204. /* If we're not READY then just leave the flags set as the cue
  2205. * to abort probing or reschedule the reset later.
  2206. */
  2207. if (ACCESS_ONCE(efx->state) != STATE_READY)
  2208. return;
  2209. queue_work(reset_workqueue, &efx->reset_work);
  2210. }
  2211. /**************************************************************************
  2212. *
  2213. * List of NICs we support
  2214. *
  2215. **************************************************************************/
  2216. /* PCI device ID table */
  2217. static const struct pci_device_id ef4_pci_table[] = {
  2218. {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE,
  2219. PCI_DEVICE_ID_SOLARFLARE_SFC4000A_0),
  2220. .driver_data = (unsigned long) &falcon_a1_nic_type},
  2221. {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE,
  2222. PCI_DEVICE_ID_SOLARFLARE_SFC4000B),
  2223. .driver_data = (unsigned long) &falcon_b0_nic_type},
  2224. {0} /* end of list */
  2225. };
  2226. /**************************************************************************
  2227. *
  2228. * Dummy PHY/MAC operations
  2229. *
  2230. * Can be used for some unimplemented operations
  2231. * Needed so all function pointers are valid and do not have to be tested
  2232. * before use
  2233. *
  2234. **************************************************************************/
  2235. int ef4_port_dummy_op_int(struct ef4_nic *efx)
  2236. {
  2237. return 0;
  2238. }
  2239. void ef4_port_dummy_op_void(struct ef4_nic *efx) {}
  2240. static bool ef4_port_dummy_op_poll(struct ef4_nic *efx)
  2241. {
  2242. return false;
  2243. }
  2244. static const struct ef4_phy_operations ef4_dummy_phy_operations = {
  2245. .init = ef4_port_dummy_op_int,
  2246. .reconfigure = ef4_port_dummy_op_int,
  2247. .poll = ef4_port_dummy_op_poll,
  2248. .fini = ef4_port_dummy_op_void,
  2249. };
  2250. /**************************************************************************
  2251. *
  2252. * Data housekeeping
  2253. *
  2254. **************************************************************************/
  2255. /* This zeroes out and then fills in the invariants in a struct
  2256. * ef4_nic (including all sub-structures).
  2257. */
  2258. static int ef4_init_struct(struct ef4_nic *efx,
  2259. struct pci_dev *pci_dev, struct net_device *net_dev)
  2260. {
  2261. int i;
  2262. /* Initialise common structures */
  2263. INIT_LIST_HEAD(&efx->node);
  2264. INIT_LIST_HEAD(&efx->secondary_list);
  2265. spin_lock_init(&efx->biu_lock);
  2266. #ifdef CONFIG_SFC_FALCON_MTD
  2267. INIT_LIST_HEAD(&efx->mtd_list);
  2268. #endif
  2269. INIT_WORK(&efx->reset_work, ef4_reset_work);
  2270. INIT_DELAYED_WORK(&efx->monitor_work, ef4_monitor);
  2271. INIT_DELAYED_WORK(&efx->selftest_work, ef4_selftest_async_work);
  2272. efx->pci_dev = pci_dev;
  2273. efx->msg_enable = debug;
  2274. efx->state = STATE_UNINIT;
  2275. strlcpy(efx->name, pci_name(pci_dev), sizeof(efx->name));
  2276. efx->net_dev = net_dev;
  2277. efx->rx_prefix_size = efx->type->rx_prefix_size;
  2278. efx->rx_ip_align =
  2279. NET_IP_ALIGN ? (efx->rx_prefix_size + NET_IP_ALIGN) % 4 : 0;
  2280. efx->rx_packet_hash_offset =
  2281. efx->type->rx_hash_offset - efx->type->rx_prefix_size;
  2282. efx->rx_packet_ts_offset =
  2283. efx->type->rx_ts_offset - efx->type->rx_prefix_size;
  2284. spin_lock_init(&efx->stats_lock);
  2285. mutex_init(&efx->mac_lock);
  2286. efx->phy_op = &ef4_dummy_phy_operations;
  2287. efx->mdio.dev = net_dev;
  2288. INIT_WORK(&efx->mac_work, ef4_mac_work);
  2289. init_waitqueue_head(&efx->flush_wq);
  2290. for (i = 0; i < EF4_MAX_CHANNELS; i++) {
  2291. efx->channel[i] = ef4_alloc_channel(efx, i, NULL);
  2292. if (!efx->channel[i])
  2293. goto fail;
  2294. efx->msi_context[i].efx = efx;
  2295. efx->msi_context[i].index = i;
  2296. }
  2297. /* Higher numbered interrupt modes are less capable! */
  2298. efx->interrupt_mode = max(efx->type->max_interrupt_mode,
  2299. interrupt_mode);
  2300. /* Would be good to use the net_dev name, but we're too early */
  2301. snprintf(efx->workqueue_name, sizeof(efx->workqueue_name), "sfc%s",
  2302. pci_name(pci_dev));
  2303. efx->workqueue = create_singlethread_workqueue(efx->workqueue_name);
  2304. if (!efx->workqueue)
  2305. goto fail;
  2306. return 0;
  2307. fail:
  2308. ef4_fini_struct(efx);
  2309. return -ENOMEM;
  2310. }
  2311. static void ef4_fini_struct(struct ef4_nic *efx)
  2312. {
  2313. int i;
  2314. for (i = 0; i < EF4_MAX_CHANNELS; i++)
  2315. kfree(efx->channel[i]);
  2316. kfree(efx->vpd_sn);
  2317. if (efx->workqueue) {
  2318. destroy_workqueue(efx->workqueue);
  2319. efx->workqueue = NULL;
  2320. }
  2321. }
  2322. void ef4_update_sw_stats(struct ef4_nic *efx, u64 *stats)
  2323. {
  2324. u64 n_rx_nodesc_trunc = 0;
  2325. struct ef4_channel *channel;
  2326. ef4_for_each_channel(channel, efx)
  2327. n_rx_nodesc_trunc += channel->n_rx_nodesc_trunc;
  2328. stats[GENERIC_STAT_rx_nodesc_trunc] = n_rx_nodesc_trunc;
  2329. stats[GENERIC_STAT_rx_noskb_drops] = atomic_read(&efx->n_rx_noskb_drops);
  2330. }
  2331. /**************************************************************************
  2332. *
  2333. * PCI interface
  2334. *
  2335. **************************************************************************/
  2336. /* Main body of final NIC shutdown code
  2337. * This is called only at module unload (or hotplug removal).
  2338. */
  2339. static void ef4_pci_remove_main(struct ef4_nic *efx)
  2340. {
  2341. /* Flush reset_work. It can no longer be scheduled since we
  2342. * are not READY.
  2343. */
  2344. BUG_ON(efx->state == STATE_READY);
  2345. cancel_work_sync(&efx->reset_work);
  2346. ef4_disable_interrupts(efx);
  2347. ef4_nic_fini_interrupt(efx);
  2348. ef4_fini_port(efx);
  2349. efx->type->fini(efx);
  2350. ef4_fini_napi(efx);
  2351. ef4_remove_all(efx);
  2352. }
  2353. /* Final NIC shutdown
  2354. * This is called only at module unload (or hotplug removal). A PF can call
  2355. * this on its VFs to ensure they are unbound first.
  2356. */
  2357. static void ef4_pci_remove(struct pci_dev *pci_dev)
  2358. {
  2359. struct ef4_nic *efx;
  2360. efx = pci_get_drvdata(pci_dev);
  2361. if (!efx)
  2362. return;
  2363. /* Mark the NIC as fini, then stop the interface */
  2364. rtnl_lock();
  2365. ef4_dissociate(efx);
  2366. dev_close(efx->net_dev);
  2367. ef4_disable_interrupts(efx);
  2368. efx->state = STATE_UNINIT;
  2369. rtnl_unlock();
  2370. ef4_unregister_netdev(efx);
  2371. ef4_mtd_remove(efx);
  2372. ef4_pci_remove_main(efx);
  2373. ef4_fini_io(efx);
  2374. netif_dbg(efx, drv, efx->net_dev, "shutdown successful\n");
  2375. ef4_fini_struct(efx);
  2376. free_netdev(efx->net_dev);
  2377. pci_disable_pcie_error_reporting(pci_dev);
  2378. };
  2379. /* NIC VPD information
  2380. * Called during probe to display the part number of the
  2381. * installed NIC. VPD is potentially very large but this should
  2382. * always appear within the first 512 bytes.
  2383. */
  2384. #define SFC_VPD_LEN 512
  2385. static void ef4_probe_vpd_strings(struct ef4_nic *efx)
  2386. {
  2387. struct pci_dev *dev = efx->pci_dev;
  2388. char vpd_data[SFC_VPD_LEN];
  2389. ssize_t vpd_size;
  2390. int ro_start, ro_size, i, j;
  2391. /* Get the vpd data from the device */
  2392. vpd_size = pci_read_vpd(dev, 0, sizeof(vpd_data), vpd_data);
  2393. if (vpd_size <= 0) {
  2394. netif_err(efx, drv, efx->net_dev, "Unable to read VPD\n");
  2395. return;
  2396. }
  2397. /* Get the Read only section */
  2398. ro_start = pci_vpd_find_tag(vpd_data, 0, vpd_size, PCI_VPD_LRDT_RO_DATA);
  2399. if (ro_start < 0) {
  2400. netif_err(efx, drv, efx->net_dev, "VPD Read-only not found\n");
  2401. return;
  2402. }
  2403. ro_size = pci_vpd_lrdt_size(&vpd_data[ro_start]);
  2404. j = ro_size;
  2405. i = ro_start + PCI_VPD_LRDT_TAG_SIZE;
  2406. if (i + j > vpd_size)
  2407. j = vpd_size - i;
  2408. /* Get the Part number */
  2409. i = pci_vpd_find_info_keyword(vpd_data, i, j, "PN");
  2410. if (i < 0) {
  2411. netif_err(efx, drv, efx->net_dev, "Part number not found\n");
  2412. return;
  2413. }
  2414. j = pci_vpd_info_field_size(&vpd_data[i]);
  2415. i += PCI_VPD_INFO_FLD_HDR_SIZE;
  2416. if (i + j > vpd_size) {
  2417. netif_err(efx, drv, efx->net_dev, "Incomplete part number\n");
  2418. return;
  2419. }
  2420. netif_info(efx, drv, efx->net_dev,
  2421. "Part Number : %.*s\n", j, &vpd_data[i]);
  2422. i = ro_start + PCI_VPD_LRDT_TAG_SIZE;
  2423. j = ro_size;
  2424. i = pci_vpd_find_info_keyword(vpd_data, i, j, "SN");
  2425. if (i < 0) {
  2426. netif_err(efx, drv, efx->net_dev, "Serial number not found\n");
  2427. return;
  2428. }
  2429. j = pci_vpd_info_field_size(&vpd_data[i]);
  2430. i += PCI_VPD_INFO_FLD_HDR_SIZE;
  2431. if (i + j > vpd_size) {
  2432. netif_err(efx, drv, efx->net_dev, "Incomplete serial number\n");
  2433. return;
  2434. }
  2435. efx->vpd_sn = kmalloc(j + 1, GFP_KERNEL);
  2436. if (!efx->vpd_sn)
  2437. return;
  2438. snprintf(efx->vpd_sn, j + 1, "%s", &vpd_data[i]);
  2439. }
  2440. /* Main body of NIC initialisation
  2441. * This is called at module load (or hotplug insertion, theoretically).
  2442. */
  2443. static int ef4_pci_probe_main(struct ef4_nic *efx)
  2444. {
  2445. int rc;
  2446. /* Do start-of-day initialisation */
  2447. rc = ef4_probe_all(efx);
  2448. if (rc)
  2449. goto fail1;
  2450. ef4_init_napi(efx);
  2451. rc = efx->type->init(efx);
  2452. if (rc) {
  2453. netif_err(efx, probe, efx->net_dev,
  2454. "failed to initialise NIC\n");
  2455. goto fail3;
  2456. }
  2457. rc = ef4_init_port(efx);
  2458. if (rc) {
  2459. netif_err(efx, probe, efx->net_dev,
  2460. "failed to initialise port\n");
  2461. goto fail4;
  2462. }
  2463. rc = ef4_nic_init_interrupt(efx);
  2464. if (rc)
  2465. goto fail5;
  2466. rc = ef4_enable_interrupts(efx);
  2467. if (rc)
  2468. goto fail6;
  2469. return 0;
  2470. fail6:
  2471. ef4_nic_fini_interrupt(efx);
  2472. fail5:
  2473. ef4_fini_port(efx);
  2474. fail4:
  2475. efx->type->fini(efx);
  2476. fail3:
  2477. ef4_fini_napi(efx);
  2478. ef4_remove_all(efx);
  2479. fail1:
  2480. return rc;
  2481. }
  2482. /* NIC initialisation
  2483. *
  2484. * This is called at module load (or hotplug insertion,
  2485. * theoretically). It sets up PCI mappings, resets the NIC,
  2486. * sets up and registers the network devices with the kernel and hooks
  2487. * the interrupt service routine. It does not prepare the device for
  2488. * transmission; this is left to the first time one of the network
  2489. * interfaces is brought up (i.e. ef4_net_open).
  2490. */
  2491. static int ef4_pci_probe(struct pci_dev *pci_dev,
  2492. const struct pci_device_id *entry)
  2493. {
  2494. struct net_device *net_dev;
  2495. struct ef4_nic *efx;
  2496. int rc;
  2497. /* Allocate and initialise a struct net_device and struct ef4_nic */
  2498. net_dev = alloc_etherdev_mqs(sizeof(*efx), EF4_MAX_CORE_TX_QUEUES,
  2499. EF4_MAX_RX_QUEUES);
  2500. if (!net_dev)
  2501. return -ENOMEM;
  2502. efx = netdev_priv(net_dev);
  2503. efx->type = (const struct ef4_nic_type *) entry->driver_data;
  2504. efx->fixed_features |= NETIF_F_HIGHDMA;
  2505. pci_set_drvdata(pci_dev, efx);
  2506. SET_NETDEV_DEV(net_dev, &pci_dev->dev);
  2507. rc = ef4_init_struct(efx, pci_dev, net_dev);
  2508. if (rc)
  2509. goto fail1;
  2510. netif_info(efx, probe, efx->net_dev,
  2511. "Solarflare NIC detected\n");
  2512. ef4_probe_vpd_strings(efx);
  2513. /* Set up basic I/O (BAR mappings etc) */
  2514. rc = ef4_init_io(efx);
  2515. if (rc)
  2516. goto fail2;
  2517. rc = ef4_pci_probe_main(efx);
  2518. if (rc)
  2519. goto fail3;
  2520. net_dev->features |= (efx->type->offload_features | NETIF_F_SG |
  2521. NETIF_F_RXCSUM);
  2522. /* Mask for features that also apply to VLAN devices */
  2523. net_dev->vlan_features |= (NETIF_F_HW_CSUM | NETIF_F_SG |
  2524. NETIF_F_HIGHDMA | NETIF_F_RXCSUM);
  2525. net_dev->hw_features = net_dev->features & ~efx->fixed_features;
  2526. /* Disable VLAN filtering by default. It may be enforced if
  2527. * the feature is fixed (i.e. VLAN filters are required to
  2528. * receive VLAN tagged packets due to vPort restrictions).
  2529. */
  2530. net_dev->features &= ~NETIF_F_HW_VLAN_CTAG_FILTER;
  2531. net_dev->features |= efx->fixed_features;
  2532. rc = ef4_register_netdev(efx);
  2533. if (rc)
  2534. goto fail4;
  2535. netif_dbg(efx, probe, efx->net_dev, "initialisation successful\n");
  2536. /* Try to create MTDs, but allow this to fail */
  2537. rtnl_lock();
  2538. rc = ef4_mtd_probe(efx);
  2539. rtnl_unlock();
  2540. if (rc && rc != -EPERM)
  2541. netif_warn(efx, probe, efx->net_dev,
  2542. "failed to create MTDs (%d)\n", rc);
  2543. rc = pci_enable_pcie_error_reporting(pci_dev);
  2544. if (rc && rc != -EINVAL)
  2545. netif_notice(efx, probe, efx->net_dev,
  2546. "PCIE error reporting unavailable (%d).\n",
  2547. rc);
  2548. return 0;
  2549. fail4:
  2550. ef4_pci_remove_main(efx);
  2551. fail3:
  2552. ef4_fini_io(efx);
  2553. fail2:
  2554. ef4_fini_struct(efx);
  2555. fail1:
  2556. WARN_ON(rc > 0);
  2557. netif_dbg(efx, drv, efx->net_dev, "initialisation failed. rc=%d\n", rc);
  2558. free_netdev(net_dev);
  2559. return rc;
  2560. }
  2561. static int ef4_pm_freeze(struct device *dev)
  2562. {
  2563. struct ef4_nic *efx = pci_get_drvdata(to_pci_dev(dev));
  2564. rtnl_lock();
  2565. if (efx->state != STATE_DISABLED) {
  2566. efx->state = STATE_UNINIT;
  2567. ef4_device_detach_sync(efx);
  2568. ef4_stop_all(efx);
  2569. ef4_disable_interrupts(efx);
  2570. }
  2571. rtnl_unlock();
  2572. return 0;
  2573. }
  2574. static int ef4_pm_thaw(struct device *dev)
  2575. {
  2576. int rc;
  2577. struct ef4_nic *efx = pci_get_drvdata(to_pci_dev(dev));
  2578. rtnl_lock();
  2579. if (efx->state != STATE_DISABLED) {
  2580. rc = ef4_enable_interrupts(efx);
  2581. if (rc)
  2582. goto fail;
  2583. mutex_lock(&efx->mac_lock);
  2584. efx->phy_op->reconfigure(efx);
  2585. mutex_unlock(&efx->mac_lock);
  2586. ef4_start_all(efx);
  2587. netif_device_attach(efx->net_dev);
  2588. efx->state = STATE_READY;
  2589. efx->type->resume_wol(efx);
  2590. }
  2591. rtnl_unlock();
  2592. /* Reschedule any quenched resets scheduled during ef4_pm_freeze() */
  2593. queue_work(reset_workqueue, &efx->reset_work);
  2594. return 0;
  2595. fail:
  2596. rtnl_unlock();
  2597. return rc;
  2598. }
  2599. static int ef4_pm_poweroff(struct device *dev)
  2600. {
  2601. struct pci_dev *pci_dev = to_pci_dev(dev);
  2602. struct ef4_nic *efx = pci_get_drvdata(pci_dev);
  2603. efx->type->fini(efx);
  2604. efx->reset_pending = 0;
  2605. pci_save_state(pci_dev);
  2606. return pci_set_power_state(pci_dev, PCI_D3hot);
  2607. }
  2608. /* Used for both resume and restore */
  2609. static int ef4_pm_resume(struct device *dev)
  2610. {
  2611. struct pci_dev *pci_dev = to_pci_dev(dev);
  2612. struct ef4_nic *efx = pci_get_drvdata(pci_dev);
  2613. int rc;
  2614. rc = pci_set_power_state(pci_dev, PCI_D0);
  2615. if (rc)
  2616. return rc;
  2617. pci_restore_state(pci_dev);
  2618. rc = pci_enable_device(pci_dev);
  2619. if (rc)
  2620. return rc;
  2621. pci_set_master(efx->pci_dev);
  2622. rc = efx->type->reset(efx, RESET_TYPE_ALL);
  2623. if (rc)
  2624. return rc;
  2625. rc = efx->type->init(efx);
  2626. if (rc)
  2627. return rc;
  2628. rc = ef4_pm_thaw(dev);
  2629. return rc;
  2630. }
  2631. static int ef4_pm_suspend(struct device *dev)
  2632. {
  2633. int rc;
  2634. ef4_pm_freeze(dev);
  2635. rc = ef4_pm_poweroff(dev);
  2636. if (rc)
  2637. ef4_pm_resume(dev);
  2638. return rc;
  2639. }
  2640. static const struct dev_pm_ops ef4_pm_ops = {
  2641. .suspend = ef4_pm_suspend,
  2642. .resume = ef4_pm_resume,
  2643. .freeze = ef4_pm_freeze,
  2644. .thaw = ef4_pm_thaw,
  2645. .poweroff = ef4_pm_poweroff,
  2646. .restore = ef4_pm_resume,
  2647. };
  2648. /* A PCI error affecting this device was detected.
  2649. * At this point MMIO and DMA may be disabled.
  2650. * Stop the software path and request a slot reset.
  2651. */
  2652. static pci_ers_result_t ef4_io_error_detected(struct pci_dev *pdev,
  2653. enum pci_channel_state state)
  2654. {
  2655. pci_ers_result_t status = PCI_ERS_RESULT_RECOVERED;
  2656. struct ef4_nic *efx = pci_get_drvdata(pdev);
  2657. if (state == pci_channel_io_perm_failure)
  2658. return PCI_ERS_RESULT_DISCONNECT;
  2659. rtnl_lock();
  2660. if (efx->state != STATE_DISABLED) {
  2661. efx->state = STATE_RECOVERY;
  2662. efx->reset_pending = 0;
  2663. ef4_device_detach_sync(efx);
  2664. ef4_stop_all(efx);
  2665. ef4_disable_interrupts(efx);
  2666. status = PCI_ERS_RESULT_NEED_RESET;
  2667. } else {
  2668. /* If the interface is disabled we don't want to do anything
  2669. * with it.
  2670. */
  2671. status = PCI_ERS_RESULT_RECOVERED;
  2672. }
  2673. rtnl_unlock();
  2674. pci_disable_device(pdev);
  2675. return status;
  2676. }
  2677. /* Fake a successful reset, which will be performed later in ef4_io_resume. */
  2678. static pci_ers_result_t ef4_io_slot_reset(struct pci_dev *pdev)
  2679. {
  2680. struct ef4_nic *efx = pci_get_drvdata(pdev);
  2681. pci_ers_result_t status = PCI_ERS_RESULT_RECOVERED;
  2682. int rc;
  2683. if (pci_enable_device(pdev)) {
  2684. netif_err(efx, hw, efx->net_dev,
  2685. "Cannot re-enable PCI device after reset.\n");
  2686. status = PCI_ERS_RESULT_DISCONNECT;
  2687. }
  2688. rc = pci_cleanup_aer_uncorrect_error_status(pdev);
  2689. if (rc) {
  2690. netif_err(efx, hw, efx->net_dev,
  2691. "pci_cleanup_aer_uncorrect_error_status failed (%d)\n", rc);
  2692. /* Non-fatal error. Continue. */
  2693. }
  2694. return status;
  2695. }
  2696. /* Perform the actual reset and resume I/O operations. */
  2697. static void ef4_io_resume(struct pci_dev *pdev)
  2698. {
  2699. struct ef4_nic *efx = pci_get_drvdata(pdev);
  2700. int rc;
  2701. rtnl_lock();
  2702. if (efx->state == STATE_DISABLED)
  2703. goto out;
  2704. rc = ef4_reset(efx, RESET_TYPE_ALL);
  2705. if (rc) {
  2706. netif_err(efx, hw, efx->net_dev,
  2707. "ef4_reset failed after PCI error (%d)\n", rc);
  2708. } else {
  2709. efx->state = STATE_READY;
  2710. netif_dbg(efx, hw, efx->net_dev,
  2711. "Done resetting and resuming IO after PCI error.\n");
  2712. }
  2713. out:
  2714. rtnl_unlock();
  2715. }
  2716. /* For simplicity and reliability, we always require a slot reset and try to
  2717. * reset the hardware when a pci error affecting the device is detected.
  2718. * We leave both the link_reset and mmio_enabled callback unimplemented:
  2719. * with our request for slot reset the mmio_enabled callback will never be
  2720. * called, and the link_reset callback is not used by AER or EEH mechanisms.
  2721. */
  2722. static const struct pci_error_handlers ef4_err_handlers = {
  2723. .error_detected = ef4_io_error_detected,
  2724. .slot_reset = ef4_io_slot_reset,
  2725. .resume = ef4_io_resume,
  2726. };
  2727. static struct pci_driver ef4_pci_driver = {
  2728. .name = KBUILD_MODNAME,
  2729. .id_table = ef4_pci_table,
  2730. .probe = ef4_pci_probe,
  2731. .remove = ef4_pci_remove,
  2732. .driver.pm = &ef4_pm_ops,
  2733. .err_handler = &ef4_err_handlers,
  2734. };
  2735. /**************************************************************************
  2736. *
  2737. * Kernel module interface
  2738. *
  2739. *************************************************************************/
  2740. module_param(interrupt_mode, uint, 0444);
  2741. MODULE_PARM_DESC(interrupt_mode,
  2742. "Interrupt mode (0=>MSIX 1=>MSI 2=>legacy)");
  2743. static int __init ef4_init_module(void)
  2744. {
  2745. int rc;
  2746. printk(KERN_INFO "Solarflare Falcon driver v" EF4_DRIVER_VERSION "\n");
  2747. rc = register_netdevice_notifier(&ef4_netdev_notifier);
  2748. if (rc)
  2749. goto err_notifier;
  2750. reset_workqueue = create_singlethread_workqueue("sfc_reset");
  2751. if (!reset_workqueue) {
  2752. rc = -ENOMEM;
  2753. goto err_reset;
  2754. }
  2755. rc = pci_register_driver(&ef4_pci_driver);
  2756. if (rc < 0)
  2757. goto err_pci;
  2758. return 0;
  2759. err_pci:
  2760. destroy_workqueue(reset_workqueue);
  2761. err_reset:
  2762. unregister_netdevice_notifier(&ef4_netdev_notifier);
  2763. err_notifier:
  2764. return rc;
  2765. }
  2766. static void __exit ef4_exit_module(void)
  2767. {
  2768. printk(KERN_INFO "Solarflare Falcon driver unloading\n");
  2769. pci_unregister_driver(&ef4_pci_driver);
  2770. destroy_workqueue(reset_workqueue);
  2771. unregister_netdevice_notifier(&ef4_netdev_notifier);
  2772. }
  2773. module_init(ef4_init_module);
  2774. module_exit(ef4_exit_module);
  2775. MODULE_AUTHOR("Solarflare Communications and "
  2776. "Michael Brown <mbrown@fensystems.co.uk>");
  2777. MODULE_DESCRIPTION("Solarflare Falcon network driver");
  2778. MODULE_LICENSE("GPL");
  2779. MODULE_DEVICE_TABLE(pci, ef4_pci_table);