falcon.c 87 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089
  1. /****************************************************************************
  2. * Driver for Solarflare Solarstorm network controllers and boards
  3. * Copyright 2005-2006 Fen Systems Ltd.
  4. * Copyright 2006-2008 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/bitops.h>
  11. #include <linux/delay.h>
  12. #include <linux/pci.h>
  13. #include <linux/module.h>
  14. #include <linux/seq_file.h>
  15. #include <linux/i2c.h>
  16. #include <linux/i2c-algo-bit.h>
  17. #include "net_driver.h"
  18. #include "bitfield.h"
  19. #include "efx.h"
  20. #include "mac.h"
  21. #include "gmii.h"
  22. #include "spi.h"
  23. #include "falcon.h"
  24. #include "falcon_hwdefs.h"
  25. #include "falcon_io.h"
  26. #include "mdio_10g.h"
  27. #include "phy.h"
  28. #include "boards.h"
  29. #include "workarounds.h"
  30. /* Falcon hardware control.
  31. * Falcon is the internal codename for the SFC4000 controller that is
  32. * present in SFE400X evaluation boards
  33. */
  34. /**
  35. * struct falcon_nic_data - Falcon NIC state
  36. * @next_buffer_table: First available buffer table id
  37. * @pci_dev2: The secondary PCI device if present
  38. * @i2c_data: Operations and state for I2C bit-bashing algorithm
  39. */
  40. struct falcon_nic_data {
  41. unsigned next_buffer_table;
  42. struct pci_dev *pci_dev2;
  43. struct i2c_algo_bit_data i2c_data;
  44. };
  45. /**************************************************************************
  46. *
  47. * Configurable values
  48. *
  49. **************************************************************************
  50. */
  51. static int disable_dma_stats;
  52. /* This is set to 16 for a good reason. In summary, if larger than
  53. * 16, the descriptor cache holds more than a default socket
  54. * buffer's worth of packets (for UDP we can only have at most one
  55. * socket buffer's worth outstanding). This combined with the fact
  56. * that we only get 1 TX event per descriptor cache means the NIC
  57. * goes idle.
  58. */
  59. #define TX_DC_ENTRIES 16
  60. #define TX_DC_ENTRIES_ORDER 0
  61. #define TX_DC_BASE 0x130000
  62. #define RX_DC_ENTRIES 64
  63. #define RX_DC_ENTRIES_ORDER 2
  64. #define RX_DC_BASE 0x100000
  65. /* RX FIFO XOFF watermark
  66. *
  67. * When the amount of the RX FIFO increases used increases past this
  68. * watermark send XOFF. Only used if RX flow control is enabled (ethtool -A)
  69. * This also has an effect on RX/TX arbitration
  70. */
  71. static int rx_xoff_thresh_bytes = -1;
  72. module_param(rx_xoff_thresh_bytes, int, 0644);
  73. MODULE_PARM_DESC(rx_xoff_thresh_bytes, "RX fifo XOFF threshold");
  74. /* RX FIFO XON watermark
  75. *
  76. * When the amount of the RX FIFO used decreases below this
  77. * watermark send XON. Only used if TX flow control is enabled (ethtool -A)
  78. * This also has an effect on RX/TX arbitration
  79. */
  80. static int rx_xon_thresh_bytes = -1;
  81. module_param(rx_xon_thresh_bytes, int, 0644);
  82. MODULE_PARM_DESC(rx_xon_thresh_bytes, "RX fifo XON threshold");
  83. /* TX descriptor ring size - min 512 max 4k */
  84. #define FALCON_TXD_RING_ORDER TX_DESCQ_SIZE_1K
  85. #define FALCON_TXD_RING_SIZE 1024
  86. #define FALCON_TXD_RING_MASK (FALCON_TXD_RING_SIZE - 1)
  87. /* RX descriptor ring size - min 512 max 4k */
  88. #define FALCON_RXD_RING_ORDER RX_DESCQ_SIZE_1K
  89. #define FALCON_RXD_RING_SIZE 1024
  90. #define FALCON_RXD_RING_MASK (FALCON_RXD_RING_SIZE - 1)
  91. /* Event queue size - max 32k */
  92. #define FALCON_EVQ_ORDER EVQ_SIZE_4K
  93. #define FALCON_EVQ_SIZE 4096
  94. #define FALCON_EVQ_MASK (FALCON_EVQ_SIZE - 1)
  95. /* Max number of internal errors. After this resets will not be performed */
  96. #define FALCON_MAX_INT_ERRORS 4
  97. /* We poll for events every FLUSH_INTERVAL ms, and check FLUSH_POLL_COUNT times
  98. */
  99. #define FALCON_FLUSH_INTERVAL 10
  100. #define FALCON_FLUSH_POLL_COUNT 100
  101. /**************************************************************************
  102. *
  103. * Falcon constants
  104. *
  105. **************************************************************************
  106. */
  107. /* DMA address mask */
  108. #define FALCON_DMA_MASK DMA_BIT_MASK(46)
  109. /* TX DMA length mask (13-bit) */
  110. #define FALCON_TX_DMA_MASK (4096 - 1)
  111. /* Size and alignment of special buffers (4KB) */
  112. #define FALCON_BUF_SIZE 4096
  113. /* Dummy SRAM size code */
  114. #define SRM_NB_BSZ_ONCHIP_ONLY (-1)
  115. /* Be nice if these (or equiv.) were in linux/pci_regs.h, but they're not. */
  116. #define PCI_EXP_DEVCAP_PWR_VAL_LBN 18
  117. #define PCI_EXP_DEVCAP_PWR_SCL_LBN 26
  118. #define PCI_EXP_DEVCTL_PAYLOAD_LBN 5
  119. #define PCI_EXP_LNKSTA_LNK_WID 0x3f0
  120. #define PCI_EXP_LNKSTA_LNK_WID_LBN 4
  121. #define FALCON_IS_DUAL_FUNC(efx) \
  122. (falcon_rev(efx) < FALCON_REV_B0)
  123. /**************************************************************************
  124. *
  125. * Falcon hardware access
  126. *
  127. **************************************************************************/
  128. /* Read the current event from the event queue */
  129. static inline efx_qword_t *falcon_event(struct efx_channel *channel,
  130. unsigned int index)
  131. {
  132. return (((efx_qword_t *) (channel->eventq.addr)) + index);
  133. }
  134. /* See if an event is present
  135. *
  136. * We check both the high and low dword of the event for all ones. We
  137. * wrote all ones when we cleared the event, and no valid event can
  138. * have all ones in either its high or low dwords. This approach is
  139. * robust against reordering.
  140. *
  141. * Note that using a single 64-bit comparison is incorrect; even
  142. * though the CPU read will be atomic, the DMA write may not be.
  143. */
  144. static inline int falcon_event_present(efx_qword_t *event)
  145. {
  146. return (!(EFX_DWORD_IS_ALL_ONES(event->dword[0]) |
  147. EFX_DWORD_IS_ALL_ONES(event->dword[1])));
  148. }
  149. /**************************************************************************
  150. *
  151. * I2C bus - this is a bit-bashing interface using GPIO pins
  152. * Note that it uses the output enables to tristate the outputs
  153. * SDA is the data pin and SCL is the clock
  154. *
  155. **************************************************************************
  156. */
  157. static void falcon_setsda(void *data, int state)
  158. {
  159. struct efx_nic *efx = (struct efx_nic *)data;
  160. efx_oword_t reg;
  161. falcon_read(efx, &reg, GPIO_CTL_REG_KER);
  162. EFX_SET_OWORD_FIELD(reg, GPIO3_OEN, !state);
  163. falcon_write(efx, &reg, GPIO_CTL_REG_KER);
  164. }
  165. static void falcon_setscl(void *data, int state)
  166. {
  167. struct efx_nic *efx = (struct efx_nic *)data;
  168. efx_oword_t reg;
  169. falcon_read(efx, &reg, GPIO_CTL_REG_KER);
  170. EFX_SET_OWORD_FIELD(reg, GPIO0_OEN, !state);
  171. falcon_write(efx, &reg, GPIO_CTL_REG_KER);
  172. }
  173. static int falcon_getsda(void *data)
  174. {
  175. struct efx_nic *efx = (struct efx_nic *)data;
  176. efx_oword_t reg;
  177. falcon_read(efx, &reg, GPIO_CTL_REG_KER);
  178. return EFX_OWORD_FIELD(reg, GPIO3_IN);
  179. }
  180. static int falcon_getscl(void *data)
  181. {
  182. struct efx_nic *efx = (struct efx_nic *)data;
  183. efx_oword_t reg;
  184. falcon_read(efx, &reg, GPIO_CTL_REG_KER);
  185. return EFX_OWORD_FIELD(reg, GPIO0_IN);
  186. }
  187. static struct i2c_algo_bit_data falcon_i2c_bit_operations = {
  188. .setsda = falcon_setsda,
  189. .setscl = falcon_setscl,
  190. .getsda = falcon_getsda,
  191. .getscl = falcon_getscl,
  192. .udelay = 5,
  193. /* Wait up to 50 ms for slave to let us pull SCL high */
  194. .timeout = DIV_ROUND_UP(HZ, 20),
  195. };
  196. /**************************************************************************
  197. *
  198. * Falcon special buffer handling
  199. * Special buffers are used for event queues and the TX and RX
  200. * descriptor rings.
  201. *
  202. *************************************************************************/
  203. /*
  204. * Initialise a Falcon special buffer
  205. *
  206. * This will define a buffer (previously allocated via
  207. * falcon_alloc_special_buffer()) in Falcon's buffer table, allowing
  208. * it to be used for event queues, descriptor rings etc.
  209. */
  210. static void
  211. falcon_init_special_buffer(struct efx_nic *efx,
  212. struct efx_special_buffer *buffer)
  213. {
  214. efx_qword_t buf_desc;
  215. int index;
  216. dma_addr_t dma_addr;
  217. int i;
  218. EFX_BUG_ON_PARANOID(!buffer->addr);
  219. /* Write buffer descriptors to NIC */
  220. for (i = 0; i < buffer->entries; i++) {
  221. index = buffer->index + i;
  222. dma_addr = buffer->dma_addr + (i * 4096);
  223. EFX_LOG(efx, "mapping special buffer %d at %llx\n",
  224. index, (unsigned long long)dma_addr);
  225. EFX_POPULATE_QWORD_4(buf_desc,
  226. IP_DAT_BUF_SIZE, IP_DAT_BUF_SIZE_4K,
  227. BUF_ADR_REGION, 0,
  228. BUF_ADR_FBUF, (dma_addr >> 12),
  229. BUF_OWNER_ID_FBUF, 0);
  230. falcon_write_sram(efx, &buf_desc, index);
  231. }
  232. }
  233. /* Unmaps a buffer from Falcon and clears the buffer table entries */
  234. static void
  235. falcon_fini_special_buffer(struct efx_nic *efx,
  236. struct efx_special_buffer *buffer)
  237. {
  238. efx_oword_t buf_tbl_upd;
  239. unsigned int start = buffer->index;
  240. unsigned int end = (buffer->index + buffer->entries - 1);
  241. if (!buffer->entries)
  242. return;
  243. EFX_LOG(efx, "unmapping special buffers %d-%d\n",
  244. buffer->index, buffer->index + buffer->entries - 1);
  245. EFX_POPULATE_OWORD_4(buf_tbl_upd,
  246. BUF_UPD_CMD, 0,
  247. BUF_CLR_CMD, 1,
  248. BUF_CLR_END_ID, end,
  249. BUF_CLR_START_ID, start);
  250. falcon_write(efx, &buf_tbl_upd, BUF_TBL_UPD_REG_KER);
  251. }
  252. /*
  253. * Allocate a new Falcon special buffer
  254. *
  255. * This allocates memory for a new buffer, clears it and allocates a
  256. * new buffer ID range. It does not write into Falcon's buffer table.
  257. *
  258. * This call will allocate 4KB buffers, since Falcon can't use 8KB
  259. * buffers for event queues and descriptor rings.
  260. */
  261. static int falcon_alloc_special_buffer(struct efx_nic *efx,
  262. struct efx_special_buffer *buffer,
  263. unsigned int len)
  264. {
  265. struct falcon_nic_data *nic_data = efx->nic_data;
  266. len = ALIGN(len, FALCON_BUF_SIZE);
  267. buffer->addr = pci_alloc_consistent(efx->pci_dev, len,
  268. &buffer->dma_addr);
  269. if (!buffer->addr)
  270. return -ENOMEM;
  271. buffer->len = len;
  272. buffer->entries = len / FALCON_BUF_SIZE;
  273. BUG_ON(buffer->dma_addr & (FALCON_BUF_SIZE - 1));
  274. /* All zeros is a potentially valid event so memset to 0xff */
  275. memset(buffer->addr, 0xff, len);
  276. /* Select new buffer ID */
  277. buffer->index = nic_data->next_buffer_table;
  278. nic_data->next_buffer_table += buffer->entries;
  279. EFX_LOG(efx, "allocating special buffers %d-%d at %llx+%x "
  280. "(virt %p phys %lx)\n", buffer->index,
  281. buffer->index + buffer->entries - 1,
  282. (unsigned long long)buffer->dma_addr, len,
  283. buffer->addr, virt_to_phys(buffer->addr));
  284. return 0;
  285. }
  286. static void falcon_free_special_buffer(struct efx_nic *efx,
  287. struct efx_special_buffer *buffer)
  288. {
  289. if (!buffer->addr)
  290. return;
  291. EFX_LOG(efx, "deallocating special buffers %d-%d at %llx+%x "
  292. "(virt %p phys %lx)\n", buffer->index,
  293. buffer->index + buffer->entries - 1,
  294. (unsigned long long)buffer->dma_addr, buffer->len,
  295. buffer->addr, virt_to_phys(buffer->addr));
  296. pci_free_consistent(efx->pci_dev, buffer->len, buffer->addr,
  297. buffer->dma_addr);
  298. buffer->addr = NULL;
  299. buffer->entries = 0;
  300. }
  301. /**************************************************************************
  302. *
  303. * Falcon generic buffer handling
  304. * These buffers are used for interrupt status and MAC stats
  305. *
  306. **************************************************************************/
  307. static int falcon_alloc_buffer(struct efx_nic *efx,
  308. struct efx_buffer *buffer, unsigned int len)
  309. {
  310. buffer->addr = pci_alloc_consistent(efx->pci_dev, len,
  311. &buffer->dma_addr);
  312. if (!buffer->addr)
  313. return -ENOMEM;
  314. buffer->len = len;
  315. memset(buffer->addr, 0, len);
  316. return 0;
  317. }
  318. static void falcon_free_buffer(struct efx_nic *efx, struct efx_buffer *buffer)
  319. {
  320. if (buffer->addr) {
  321. pci_free_consistent(efx->pci_dev, buffer->len,
  322. buffer->addr, buffer->dma_addr);
  323. buffer->addr = NULL;
  324. }
  325. }
  326. /**************************************************************************
  327. *
  328. * Falcon TX path
  329. *
  330. **************************************************************************/
  331. /* Returns a pointer to the specified transmit descriptor in the TX
  332. * descriptor queue belonging to the specified channel.
  333. */
  334. static inline efx_qword_t *falcon_tx_desc(struct efx_tx_queue *tx_queue,
  335. unsigned int index)
  336. {
  337. return (((efx_qword_t *) (tx_queue->txd.addr)) + index);
  338. }
  339. /* This writes to the TX_DESC_WPTR; write pointer for TX descriptor ring */
  340. static inline void falcon_notify_tx_desc(struct efx_tx_queue *tx_queue)
  341. {
  342. unsigned write_ptr;
  343. efx_dword_t reg;
  344. write_ptr = tx_queue->write_count & FALCON_TXD_RING_MASK;
  345. EFX_POPULATE_DWORD_1(reg, TX_DESC_WPTR_DWORD, write_ptr);
  346. falcon_writel_page(tx_queue->efx, &reg,
  347. TX_DESC_UPD_REG_KER_DWORD, tx_queue->queue);
  348. }
  349. /* For each entry inserted into the software descriptor ring, create a
  350. * descriptor in the hardware TX descriptor ring (in host memory), and
  351. * write a doorbell.
  352. */
  353. void falcon_push_buffers(struct efx_tx_queue *tx_queue)
  354. {
  355. struct efx_tx_buffer *buffer;
  356. efx_qword_t *txd;
  357. unsigned write_ptr;
  358. BUG_ON(tx_queue->write_count == tx_queue->insert_count);
  359. do {
  360. write_ptr = tx_queue->write_count & FALCON_TXD_RING_MASK;
  361. buffer = &tx_queue->buffer[write_ptr];
  362. txd = falcon_tx_desc(tx_queue, write_ptr);
  363. ++tx_queue->write_count;
  364. /* Create TX descriptor ring entry */
  365. EFX_POPULATE_QWORD_5(*txd,
  366. TX_KER_PORT, 0,
  367. TX_KER_CONT, buffer->continuation,
  368. TX_KER_BYTE_CNT, buffer->len,
  369. TX_KER_BUF_REGION, 0,
  370. TX_KER_BUF_ADR, buffer->dma_addr);
  371. } while (tx_queue->write_count != tx_queue->insert_count);
  372. wmb(); /* Ensure descriptors are written before they are fetched */
  373. falcon_notify_tx_desc(tx_queue);
  374. }
  375. /* Allocate hardware resources for a TX queue */
  376. int falcon_probe_tx(struct efx_tx_queue *tx_queue)
  377. {
  378. struct efx_nic *efx = tx_queue->efx;
  379. return falcon_alloc_special_buffer(efx, &tx_queue->txd,
  380. FALCON_TXD_RING_SIZE *
  381. sizeof(efx_qword_t));
  382. }
  383. void falcon_init_tx(struct efx_tx_queue *tx_queue)
  384. {
  385. efx_oword_t tx_desc_ptr;
  386. struct efx_nic *efx = tx_queue->efx;
  387. tx_queue->flushed = false;
  388. /* Pin TX descriptor ring */
  389. falcon_init_special_buffer(efx, &tx_queue->txd);
  390. /* Push TX descriptor ring to card */
  391. EFX_POPULATE_OWORD_10(tx_desc_ptr,
  392. TX_DESCQ_EN, 1,
  393. TX_ISCSI_DDIG_EN, 0,
  394. TX_ISCSI_HDIG_EN, 0,
  395. TX_DESCQ_BUF_BASE_ID, tx_queue->txd.index,
  396. TX_DESCQ_EVQ_ID, tx_queue->channel->channel,
  397. TX_DESCQ_OWNER_ID, 0,
  398. TX_DESCQ_LABEL, tx_queue->queue,
  399. TX_DESCQ_SIZE, FALCON_TXD_RING_ORDER,
  400. TX_DESCQ_TYPE, 0,
  401. TX_NON_IP_DROP_DIS_B0, 1);
  402. if (falcon_rev(efx) >= FALCON_REV_B0) {
  403. int csum = tx_queue->queue == EFX_TX_QUEUE_OFFLOAD_CSUM;
  404. EFX_SET_OWORD_FIELD(tx_desc_ptr, TX_IP_CHKSM_DIS_B0, !csum);
  405. EFX_SET_OWORD_FIELD(tx_desc_ptr, TX_TCP_CHKSM_DIS_B0, !csum);
  406. }
  407. falcon_write_table(efx, &tx_desc_ptr, efx->type->txd_ptr_tbl_base,
  408. tx_queue->queue);
  409. if (falcon_rev(efx) < FALCON_REV_B0) {
  410. efx_oword_t reg;
  411. /* Only 128 bits in this register */
  412. BUILD_BUG_ON(EFX_TX_QUEUE_COUNT >= 128);
  413. falcon_read(efx, &reg, TX_CHKSM_CFG_REG_KER_A1);
  414. if (tx_queue->queue == EFX_TX_QUEUE_OFFLOAD_CSUM)
  415. clear_bit_le(tx_queue->queue, (void *)&reg);
  416. else
  417. set_bit_le(tx_queue->queue, (void *)&reg);
  418. falcon_write(efx, &reg, TX_CHKSM_CFG_REG_KER_A1);
  419. }
  420. }
  421. static void falcon_flush_tx_queue(struct efx_tx_queue *tx_queue)
  422. {
  423. struct efx_nic *efx = tx_queue->efx;
  424. efx_oword_t tx_flush_descq;
  425. /* Post a flush command */
  426. EFX_POPULATE_OWORD_2(tx_flush_descq,
  427. TX_FLUSH_DESCQ_CMD, 1,
  428. TX_FLUSH_DESCQ, tx_queue->queue);
  429. falcon_write(efx, &tx_flush_descq, TX_FLUSH_DESCQ_REG_KER);
  430. }
  431. void falcon_fini_tx(struct efx_tx_queue *tx_queue)
  432. {
  433. struct efx_nic *efx = tx_queue->efx;
  434. efx_oword_t tx_desc_ptr;
  435. /* The queue should have been flushed */
  436. WARN_ON(!tx_queue->flushed);
  437. /* Remove TX descriptor ring from card */
  438. EFX_ZERO_OWORD(tx_desc_ptr);
  439. falcon_write_table(efx, &tx_desc_ptr, efx->type->txd_ptr_tbl_base,
  440. tx_queue->queue);
  441. /* Unpin TX descriptor ring */
  442. falcon_fini_special_buffer(efx, &tx_queue->txd);
  443. }
  444. /* Free buffers backing TX queue */
  445. void falcon_remove_tx(struct efx_tx_queue *tx_queue)
  446. {
  447. falcon_free_special_buffer(tx_queue->efx, &tx_queue->txd);
  448. }
  449. /**************************************************************************
  450. *
  451. * Falcon RX path
  452. *
  453. **************************************************************************/
  454. /* Returns a pointer to the specified descriptor in the RX descriptor queue */
  455. static inline efx_qword_t *falcon_rx_desc(struct efx_rx_queue *rx_queue,
  456. unsigned int index)
  457. {
  458. return (((efx_qword_t *) (rx_queue->rxd.addr)) + index);
  459. }
  460. /* This creates an entry in the RX descriptor queue */
  461. static inline void falcon_build_rx_desc(struct efx_rx_queue *rx_queue,
  462. unsigned index)
  463. {
  464. struct efx_rx_buffer *rx_buf;
  465. efx_qword_t *rxd;
  466. rxd = falcon_rx_desc(rx_queue, index);
  467. rx_buf = efx_rx_buffer(rx_queue, index);
  468. EFX_POPULATE_QWORD_3(*rxd,
  469. RX_KER_BUF_SIZE,
  470. rx_buf->len -
  471. rx_queue->efx->type->rx_buffer_padding,
  472. RX_KER_BUF_REGION, 0,
  473. RX_KER_BUF_ADR, rx_buf->dma_addr);
  474. }
  475. /* This writes to the RX_DESC_WPTR register for the specified receive
  476. * descriptor ring.
  477. */
  478. void falcon_notify_rx_desc(struct efx_rx_queue *rx_queue)
  479. {
  480. efx_dword_t reg;
  481. unsigned write_ptr;
  482. while (rx_queue->notified_count != rx_queue->added_count) {
  483. falcon_build_rx_desc(rx_queue,
  484. rx_queue->notified_count &
  485. FALCON_RXD_RING_MASK);
  486. ++rx_queue->notified_count;
  487. }
  488. wmb();
  489. write_ptr = rx_queue->added_count & FALCON_RXD_RING_MASK;
  490. EFX_POPULATE_DWORD_1(reg, RX_DESC_WPTR_DWORD, write_ptr);
  491. falcon_writel_page(rx_queue->efx, &reg,
  492. RX_DESC_UPD_REG_KER_DWORD, rx_queue->queue);
  493. }
  494. int falcon_probe_rx(struct efx_rx_queue *rx_queue)
  495. {
  496. struct efx_nic *efx = rx_queue->efx;
  497. return falcon_alloc_special_buffer(efx, &rx_queue->rxd,
  498. FALCON_RXD_RING_SIZE *
  499. sizeof(efx_qword_t));
  500. }
  501. void falcon_init_rx(struct efx_rx_queue *rx_queue)
  502. {
  503. efx_oword_t rx_desc_ptr;
  504. struct efx_nic *efx = rx_queue->efx;
  505. bool is_b0 = falcon_rev(efx) >= FALCON_REV_B0;
  506. bool iscsi_digest_en = is_b0;
  507. EFX_LOG(efx, "RX queue %d ring in special buffers %d-%d\n",
  508. rx_queue->queue, rx_queue->rxd.index,
  509. rx_queue->rxd.index + rx_queue->rxd.entries - 1);
  510. rx_queue->flushed = false;
  511. /* Pin RX descriptor ring */
  512. falcon_init_special_buffer(efx, &rx_queue->rxd);
  513. /* Push RX descriptor ring to card */
  514. EFX_POPULATE_OWORD_10(rx_desc_ptr,
  515. RX_ISCSI_DDIG_EN, iscsi_digest_en,
  516. RX_ISCSI_HDIG_EN, iscsi_digest_en,
  517. RX_DESCQ_BUF_BASE_ID, rx_queue->rxd.index,
  518. RX_DESCQ_EVQ_ID, rx_queue->channel->channel,
  519. RX_DESCQ_OWNER_ID, 0,
  520. RX_DESCQ_LABEL, rx_queue->queue,
  521. RX_DESCQ_SIZE, FALCON_RXD_RING_ORDER,
  522. RX_DESCQ_TYPE, 0 /* kernel queue */ ,
  523. /* For >=B0 this is scatter so disable */
  524. RX_DESCQ_JUMBO, !is_b0,
  525. RX_DESCQ_EN, 1);
  526. falcon_write_table(efx, &rx_desc_ptr, efx->type->rxd_ptr_tbl_base,
  527. rx_queue->queue);
  528. }
  529. static void falcon_flush_rx_queue(struct efx_rx_queue *rx_queue)
  530. {
  531. struct efx_nic *efx = rx_queue->efx;
  532. efx_oword_t rx_flush_descq;
  533. /* Post a flush command */
  534. EFX_POPULATE_OWORD_2(rx_flush_descq,
  535. RX_FLUSH_DESCQ_CMD, 1,
  536. RX_FLUSH_DESCQ, rx_queue->queue);
  537. falcon_write(efx, &rx_flush_descq, RX_FLUSH_DESCQ_REG_KER);
  538. }
  539. void falcon_fini_rx(struct efx_rx_queue *rx_queue)
  540. {
  541. efx_oword_t rx_desc_ptr;
  542. struct efx_nic *efx = rx_queue->efx;
  543. /* The queue should already have been flushed */
  544. WARN_ON(!rx_queue->flushed);
  545. /* Remove RX descriptor ring from card */
  546. EFX_ZERO_OWORD(rx_desc_ptr);
  547. falcon_write_table(efx, &rx_desc_ptr, efx->type->rxd_ptr_tbl_base,
  548. rx_queue->queue);
  549. /* Unpin RX descriptor ring */
  550. falcon_fini_special_buffer(efx, &rx_queue->rxd);
  551. }
  552. /* Free buffers backing RX queue */
  553. void falcon_remove_rx(struct efx_rx_queue *rx_queue)
  554. {
  555. falcon_free_special_buffer(rx_queue->efx, &rx_queue->rxd);
  556. }
  557. /**************************************************************************
  558. *
  559. * Falcon event queue processing
  560. * Event queues are processed by per-channel tasklets.
  561. *
  562. **************************************************************************/
  563. /* Update a channel's event queue's read pointer (RPTR) register
  564. *
  565. * This writes the EVQ_RPTR_REG register for the specified channel's
  566. * event queue.
  567. *
  568. * Note that EVQ_RPTR_REG contains the index of the "last read" event,
  569. * whereas channel->eventq_read_ptr contains the index of the "next to
  570. * read" event.
  571. */
  572. void falcon_eventq_read_ack(struct efx_channel *channel)
  573. {
  574. efx_dword_t reg;
  575. struct efx_nic *efx = channel->efx;
  576. EFX_POPULATE_DWORD_1(reg, EVQ_RPTR_DWORD, channel->eventq_read_ptr);
  577. falcon_writel_table(efx, &reg, efx->type->evq_rptr_tbl_base,
  578. channel->channel);
  579. }
  580. /* Use HW to insert a SW defined event */
  581. void falcon_generate_event(struct efx_channel *channel, efx_qword_t *event)
  582. {
  583. efx_oword_t drv_ev_reg;
  584. EFX_POPULATE_OWORD_2(drv_ev_reg,
  585. DRV_EV_QID, channel->channel,
  586. DRV_EV_DATA,
  587. EFX_QWORD_FIELD64(*event, WHOLE_EVENT));
  588. falcon_write(channel->efx, &drv_ev_reg, DRV_EV_REG_KER);
  589. }
  590. /* Handle a transmit completion event
  591. *
  592. * Falcon batches TX completion events; the message we receive is of
  593. * the form "complete all TX events up to this index".
  594. */
  595. static void falcon_handle_tx_event(struct efx_channel *channel,
  596. efx_qword_t *event)
  597. {
  598. unsigned int tx_ev_desc_ptr;
  599. unsigned int tx_ev_q_label;
  600. struct efx_tx_queue *tx_queue;
  601. struct efx_nic *efx = channel->efx;
  602. if (likely(EFX_QWORD_FIELD(*event, TX_EV_COMP))) {
  603. /* Transmit completion */
  604. tx_ev_desc_ptr = EFX_QWORD_FIELD(*event, TX_EV_DESC_PTR);
  605. tx_ev_q_label = EFX_QWORD_FIELD(*event, TX_EV_Q_LABEL);
  606. tx_queue = &efx->tx_queue[tx_ev_q_label];
  607. efx_xmit_done(tx_queue, tx_ev_desc_ptr);
  608. } else if (EFX_QWORD_FIELD(*event, TX_EV_WQ_FF_FULL)) {
  609. /* Rewrite the FIFO write pointer */
  610. tx_ev_q_label = EFX_QWORD_FIELD(*event, TX_EV_Q_LABEL);
  611. tx_queue = &efx->tx_queue[tx_ev_q_label];
  612. if (efx_dev_registered(efx))
  613. netif_tx_lock(efx->net_dev);
  614. falcon_notify_tx_desc(tx_queue);
  615. if (efx_dev_registered(efx))
  616. netif_tx_unlock(efx->net_dev);
  617. } else if (EFX_QWORD_FIELD(*event, TX_EV_PKT_ERR) &&
  618. EFX_WORKAROUND_10727(efx)) {
  619. efx_schedule_reset(efx, RESET_TYPE_TX_DESC_FETCH);
  620. } else {
  621. EFX_ERR(efx, "channel %d unexpected TX event "
  622. EFX_QWORD_FMT"\n", channel->channel,
  623. EFX_QWORD_VAL(*event));
  624. }
  625. }
  626. /* Detect errors included in the rx_evt_pkt_ok bit. */
  627. static void falcon_handle_rx_not_ok(struct efx_rx_queue *rx_queue,
  628. const efx_qword_t *event,
  629. bool *rx_ev_pkt_ok,
  630. bool *discard)
  631. {
  632. struct efx_nic *efx = rx_queue->efx;
  633. bool rx_ev_buf_owner_id_err, rx_ev_ip_hdr_chksum_err;
  634. bool rx_ev_tcp_udp_chksum_err, rx_ev_eth_crc_err;
  635. bool rx_ev_frm_trunc, rx_ev_drib_nib, rx_ev_tobe_disc;
  636. bool rx_ev_other_err, rx_ev_pause_frm;
  637. bool rx_ev_ip_frag_err, rx_ev_hdr_type, rx_ev_mcast_pkt;
  638. unsigned rx_ev_pkt_type;
  639. rx_ev_hdr_type = EFX_QWORD_FIELD(*event, RX_EV_HDR_TYPE);
  640. rx_ev_mcast_pkt = EFX_QWORD_FIELD(*event, RX_EV_MCAST_PKT);
  641. rx_ev_tobe_disc = EFX_QWORD_FIELD(*event, RX_EV_TOBE_DISC);
  642. rx_ev_pkt_type = EFX_QWORD_FIELD(*event, RX_EV_PKT_TYPE);
  643. rx_ev_buf_owner_id_err = EFX_QWORD_FIELD(*event,
  644. RX_EV_BUF_OWNER_ID_ERR);
  645. rx_ev_ip_frag_err = EFX_QWORD_FIELD(*event, RX_EV_IF_FRAG_ERR);
  646. rx_ev_ip_hdr_chksum_err = EFX_QWORD_FIELD(*event,
  647. RX_EV_IP_HDR_CHKSUM_ERR);
  648. rx_ev_tcp_udp_chksum_err = EFX_QWORD_FIELD(*event,
  649. RX_EV_TCP_UDP_CHKSUM_ERR);
  650. rx_ev_eth_crc_err = EFX_QWORD_FIELD(*event, RX_EV_ETH_CRC_ERR);
  651. rx_ev_frm_trunc = EFX_QWORD_FIELD(*event, RX_EV_FRM_TRUNC);
  652. rx_ev_drib_nib = ((falcon_rev(efx) >= FALCON_REV_B0) ?
  653. 0 : EFX_QWORD_FIELD(*event, RX_EV_DRIB_NIB));
  654. rx_ev_pause_frm = EFX_QWORD_FIELD(*event, RX_EV_PAUSE_FRM_ERR);
  655. /* Every error apart from tobe_disc and pause_frm */
  656. rx_ev_other_err = (rx_ev_drib_nib | rx_ev_tcp_udp_chksum_err |
  657. rx_ev_buf_owner_id_err | rx_ev_eth_crc_err |
  658. rx_ev_frm_trunc | rx_ev_ip_hdr_chksum_err);
  659. /* Count errors that are not in MAC stats. */
  660. if (rx_ev_frm_trunc)
  661. ++rx_queue->channel->n_rx_frm_trunc;
  662. else if (rx_ev_tobe_disc)
  663. ++rx_queue->channel->n_rx_tobe_disc;
  664. else if (rx_ev_ip_hdr_chksum_err)
  665. ++rx_queue->channel->n_rx_ip_hdr_chksum_err;
  666. else if (rx_ev_tcp_udp_chksum_err)
  667. ++rx_queue->channel->n_rx_tcp_udp_chksum_err;
  668. if (rx_ev_ip_frag_err)
  669. ++rx_queue->channel->n_rx_ip_frag_err;
  670. /* The frame must be discarded if any of these are true. */
  671. *discard = (rx_ev_eth_crc_err | rx_ev_frm_trunc | rx_ev_drib_nib |
  672. rx_ev_tobe_disc | rx_ev_pause_frm);
  673. /* TOBE_DISC is expected on unicast mismatches; don't print out an
  674. * error message. FRM_TRUNC indicates RXDP dropped the packet due
  675. * to a FIFO overflow.
  676. */
  677. #ifdef EFX_ENABLE_DEBUG
  678. if (rx_ev_other_err) {
  679. EFX_INFO_RL(efx, " RX queue %d unexpected RX event "
  680. EFX_QWORD_FMT "%s%s%s%s%s%s%s%s\n",
  681. rx_queue->queue, EFX_QWORD_VAL(*event),
  682. rx_ev_buf_owner_id_err ? " [OWNER_ID_ERR]" : "",
  683. rx_ev_ip_hdr_chksum_err ?
  684. " [IP_HDR_CHKSUM_ERR]" : "",
  685. rx_ev_tcp_udp_chksum_err ?
  686. " [TCP_UDP_CHKSUM_ERR]" : "",
  687. rx_ev_eth_crc_err ? " [ETH_CRC_ERR]" : "",
  688. rx_ev_frm_trunc ? " [FRM_TRUNC]" : "",
  689. rx_ev_drib_nib ? " [DRIB_NIB]" : "",
  690. rx_ev_tobe_disc ? " [TOBE_DISC]" : "",
  691. rx_ev_pause_frm ? " [PAUSE]" : "");
  692. }
  693. #endif
  694. if (unlikely(rx_ev_eth_crc_err && EFX_WORKAROUND_10750(efx) &&
  695. efx->phy_type == PHY_TYPE_10XPRESS))
  696. tenxpress_crc_err(efx);
  697. }
  698. /* Handle receive events that are not in-order. */
  699. static void falcon_handle_rx_bad_index(struct efx_rx_queue *rx_queue,
  700. unsigned index)
  701. {
  702. struct efx_nic *efx = rx_queue->efx;
  703. unsigned expected, dropped;
  704. expected = rx_queue->removed_count & FALCON_RXD_RING_MASK;
  705. dropped = ((index + FALCON_RXD_RING_SIZE - expected) &
  706. FALCON_RXD_RING_MASK);
  707. EFX_INFO(efx, "dropped %d events (index=%d expected=%d)\n",
  708. dropped, index, expected);
  709. efx_schedule_reset(efx, EFX_WORKAROUND_5676(efx) ?
  710. RESET_TYPE_RX_RECOVERY : RESET_TYPE_DISABLE);
  711. }
  712. /* Handle a packet received event
  713. *
  714. * Falcon silicon gives a "discard" flag if it's a unicast packet with the
  715. * wrong destination address
  716. * Also "is multicast" and "matches multicast filter" flags can be used to
  717. * discard non-matching multicast packets.
  718. */
  719. static void falcon_handle_rx_event(struct efx_channel *channel,
  720. const efx_qword_t *event)
  721. {
  722. unsigned int rx_ev_desc_ptr, rx_ev_byte_cnt;
  723. unsigned int rx_ev_hdr_type, rx_ev_mcast_pkt;
  724. unsigned expected_ptr;
  725. bool rx_ev_pkt_ok, discard = false, checksummed;
  726. struct efx_rx_queue *rx_queue;
  727. struct efx_nic *efx = channel->efx;
  728. /* Basic packet information */
  729. rx_ev_byte_cnt = EFX_QWORD_FIELD(*event, RX_EV_BYTE_CNT);
  730. rx_ev_pkt_ok = EFX_QWORD_FIELD(*event, RX_EV_PKT_OK);
  731. rx_ev_hdr_type = EFX_QWORD_FIELD(*event, RX_EV_HDR_TYPE);
  732. WARN_ON(EFX_QWORD_FIELD(*event, RX_EV_JUMBO_CONT));
  733. WARN_ON(EFX_QWORD_FIELD(*event, RX_EV_SOP) != 1);
  734. WARN_ON(EFX_QWORD_FIELD(*event, RX_EV_Q_LABEL) != channel->channel);
  735. rx_queue = &efx->rx_queue[channel->channel];
  736. rx_ev_desc_ptr = EFX_QWORD_FIELD(*event, RX_EV_DESC_PTR);
  737. expected_ptr = rx_queue->removed_count & FALCON_RXD_RING_MASK;
  738. if (unlikely(rx_ev_desc_ptr != expected_ptr))
  739. falcon_handle_rx_bad_index(rx_queue, rx_ev_desc_ptr);
  740. if (likely(rx_ev_pkt_ok)) {
  741. /* If packet is marked as OK and packet type is TCP/IPv4 or
  742. * UDP/IPv4, then we can rely on the hardware checksum.
  743. */
  744. checksummed = RX_EV_HDR_TYPE_HAS_CHECKSUMS(rx_ev_hdr_type);
  745. } else {
  746. falcon_handle_rx_not_ok(rx_queue, event, &rx_ev_pkt_ok,
  747. &discard);
  748. checksummed = false;
  749. }
  750. /* Detect multicast packets that didn't match the filter */
  751. rx_ev_mcast_pkt = EFX_QWORD_FIELD(*event, RX_EV_MCAST_PKT);
  752. if (rx_ev_mcast_pkt) {
  753. unsigned int rx_ev_mcast_hash_match =
  754. EFX_QWORD_FIELD(*event, RX_EV_MCAST_HASH_MATCH);
  755. if (unlikely(!rx_ev_mcast_hash_match))
  756. discard = true;
  757. }
  758. /* Handle received packet */
  759. efx_rx_packet(rx_queue, rx_ev_desc_ptr, rx_ev_byte_cnt,
  760. checksummed, discard);
  761. }
  762. /* Global events are basically PHY events */
  763. static void falcon_handle_global_event(struct efx_channel *channel,
  764. efx_qword_t *event)
  765. {
  766. struct efx_nic *efx = channel->efx;
  767. bool is_phy_event = false, handled = false;
  768. /* Check for interrupt on either port. Some boards have a
  769. * single PHY wired to the interrupt line for port 1. */
  770. if (EFX_QWORD_FIELD(*event, G_PHY0_INTR) ||
  771. EFX_QWORD_FIELD(*event, G_PHY1_INTR) ||
  772. EFX_QWORD_FIELD(*event, XG_PHY_INTR))
  773. is_phy_event = true;
  774. if ((falcon_rev(efx) >= FALCON_REV_B0) &&
  775. EFX_QWORD_FIELD(*event, XG_MNT_INTR_B0))
  776. is_phy_event = true;
  777. if (is_phy_event) {
  778. efx->phy_op->clear_interrupt(efx);
  779. queue_work(efx->workqueue, &efx->reconfigure_work);
  780. handled = true;
  781. }
  782. if (EFX_QWORD_FIELD_VER(efx, *event, RX_RECOVERY)) {
  783. EFX_ERR(efx, "channel %d seen global RX_RESET "
  784. "event. Resetting.\n", channel->channel);
  785. atomic_inc(&efx->rx_reset);
  786. efx_schedule_reset(efx, EFX_WORKAROUND_6555(efx) ?
  787. RESET_TYPE_RX_RECOVERY : RESET_TYPE_DISABLE);
  788. handled = true;
  789. }
  790. if (!handled)
  791. EFX_ERR(efx, "channel %d unknown global event "
  792. EFX_QWORD_FMT "\n", channel->channel,
  793. EFX_QWORD_VAL(*event));
  794. }
  795. static void falcon_handle_driver_event(struct efx_channel *channel,
  796. efx_qword_t *event)
  797. {
  798. struct efx_nic *efx = channel->efx;
  799. unsigned int ev_sub_code;
  800. unsigned int ev_sub_data;
  801. ev_sub_code = EFX_QWORD_FIELD(*event, DRIVER_EV_SUB_CODE);
  802. ev_sub_data = EFX_QWORD_FIELD(*event, DRIVER_EV_SUB_DATA);
  803. switch (ev_sub_code) {
  804. case TX_DESCQ_FLS_DONE_EV_DECODE:
  805. EFX_TRACE(efx, "channel %d TXQ %d flushed\n",
  806. channel->channel, ev_sub_data);
  807. break;
  808. case RX_DESCQ_FLS_DONE_EV_DECODE:
  809. EFX_TRACE(efx, "channel %d RXQ %d flushed\n",
  810. channel->channel, ev_sub_data);
  811. break;
  812. case EVQ_INIT_DONE_EV_DECODE:
  813. EFX_LOG(efx, "channel %d EVQ %d initialised\n",
  814. channel->channel, ev_sub_data);
  815. break;
  816. case SRM_UPD_DONE_EV_DECODE:
  817. EFX_TRACE(efx, "channel %d SRAM update done\n",
  818. channel->channel);
  819. break;
  820. case WAKE_UP_EV_DECODE:
  821. EFX_TRACE(efx, "channel %d RXQ %d wakeup event\n",
  822. channel->channel, ev_sub_data);
  823. break;
  824. case TIMER_EV_DECODE:
  825. EFX_TRACE(efx, "channel %d RX queue %d timer expired\n",
  826. channel->channel, ev_sub_data);
  827. break;
  828. case RX_RECOVERY_EV_DECODE:
  829. EFX_ERR(efx, "channel %d seen DRIVER RX_RESET event. "
  830. "Resetting.\n", channel->channel);
  831. atomic_inc(&efx->rx_reset);
  832. efx_schedule_reset(efx,
  833. EFX_WORKAROUND_6555(efx) ?
  834. RESET_TYPE_RX_RECOVERY :
  835. RESET_TYPE_DISABLE);
  836. break;
  837. case RX_DSC_ERROR_EV_DECODE:
  838. EFX_ERR(efx, "RX DMA Q %d reports descriptor fetch error."
  839. " RX Q %d is disabled.\n", ev_sub_data, ev_sub_data);
  840. efx_schedule_reset(efx, RESET_TYPE_RX_DESC_FETCH);
  841. break;
  842. case TX_DSC_ERROR_EV_DECODE:
  843. EFX_ERR(efx, "TX DMA Q %d reports descriptor fetch error."
  844. " TX Q %d is disabled.\n", ev_sub_data, ev_sub_data);
  845. efx_schedule_reset(efx, RESET_TYPE_TX_DESC_FETCH);
  846. break;
  847. default:
  848. EFX_TRACE(efx, "channel %d unknown driver event code %d "
  849. "data %04x\n", channel->channel, ev_sub_code,
  850. ev_sub_data);
  851. break;
  852. }
  853. }
  854. int falcon_process_eventq(struct efx_channel *channel, int rx_quota)
  855. {
  856. unsigned int read_ptr;
  857. efx_qword_t event, *p_event;
  858. int ev_code;
  859. int rx_packets = 0;
  860. read_ptr = channel->eventq_read_ptr;
  861. do {
  862. p_event = falcon_event(channel, read_ptr);
  863. event = *p_event;
  864. if (!falcon_event_present(&event))
  865. /* End of events */
  866. break;
  867. EFX_TRACE(channel->efx, "channel %d event is "EFX_QWORD_FMT"\n",
  868. channel->channel, EFX_QWORD_VAL(event));
  869. /* Clear this event by marking it all ones */
  870. EFX_SET_QWORD(*p_event);
  871. ev_code = EFX_QWORD_FIELD(event, EV_CODE);
  872. switch (ev_code) {
  873. case RX_IP_EV_DECODE:
  874. falcon_handle_rx_event(channel, &event);
  875. ++rx_packets;
  876. break;
  877. case TX_IP_EV_DECODE:
  878. falcon_handle_tx_event(channel, &event);
  879. break;
  880. case DRV_GEN_EV_DECODE:
  881. channel->eventq_magic
  882. = EFX_QWORD_FIELD(event, EVQ_MAGIC);
  883. EFX_LOG(channel->efx, "channel %d received generated "
  884. "event "EFX_QWORD_FMT"\n", channel->channel,
  885. EFX_QWORD_VAL(event));
  886. break;
  887. case GLOBAL_EV_DECODE:
  888. falcon_handle_global_event(channel, &event);
  889. break;
  890. case DRIVER_EV_DECODE:
  891. falcon_handle_driver_event(channel, &event);
  892. break;
  893. default:
  894. EFX_ERR(channel->efx, "channel %d unknown event type %d"
  895. " (data " EFX_QWORD_FMT ")\n", channel->channel,
  896. ev_code, EFX_QWORD_VAL(event));
  897. }
  898. /* Increment read pointer */
  899. read_ptr = (read_ptr + 1) & FALCON_EVQ_MASK;
  900. } while (rx_packets < rx_quota);
  901. channel->eventq_read_ptr = read_ptr;
  902. return rx_packets;
  903. }
  904. void falcon_set_int_moderation(struct efx_channel *channel)
  905. {
  906. efx_dword_t timer_cmd;
  907. struct efx_nic *efx = channel->efx;
  908. /* Set timer register */
  909. if (channel->irq_moderation) {
  910. /* Round to resolution supported by hardware. The value we
  911. * program is based at 0. So actual interrupt moderation
  912. * achieved is ((x + 1) * res).
  913. */
  914. unsigned int res = 5;
  915. channel->irq_moderation -= (channel->irq_moderation % res);
  916. if (channel->irq_moderation < res)
  917. channel->irq_moderation = res;
  918. EFX_POPULATE_DWORD_2(timer_cmd,
  919. TIMER_MODE, TIMER_MODE_INT_HLDOFF,
  920. TIMER_VAL,
  921. (channel->irq_moderation / res) - 1);
  922. } else {
  923. EFX_POPULATE_DWORD_2(timer_cmd,
  924. TIMER_MODE, TIMER_MODE_DIS,
  925. TIMER_VAL, 0);
  926. }
  927. falcon_writel_page_locked(efx, &timer_cmd, TIMER_CMD_REG_KER,
  928. channel->channel);
  929. }
  930. /* Allocate buffer table entries for event queue */
  931. int falcon_probe_eventq(struct efx_channel *channel)
  932. {
  933. struct efx_nic *efx = channel->efx;
  934. unsigned int evq_size;
  935. evq_size = FALCON_EVQ_SIZE * sizeof(efx_qword_t);
  936. return falcon_alloc_special_buffer(efx, &channel->eventq, evq_size);
  937. }
  938. void falcon_init_eventq(struct efx_channel *channel)
  939. {
  940. efx_oword_t evq_ptr;
  941. struct efx_nic *efx = channel->efx;
  942. EFX_LOG(efx, "channel %d event queue in special buffers %d-%d\n",
  943. channel->channel, channel->eventq.index,
  944. channel->eventq.index + channel->eventq.entries - 1);
  945. /* Pin event queue buffer */
  946. falcon_init_special_buffer(efx, &channel->eventq);
  947. /* Fill event queue with all ones (i.e. empty events) */
  948. memset(channel->eventq.addr, 0xff, channel->eventq.len);
  949. /* Push event queue to card */
  950. EFX_POPULATE_OWORD_3(evq_ptr,
  951. EVQ_EN, 1,
  952. EVQ_SIZE, FALCON_EVQ_ORDER,
  953. EVQ_BUF_BASE_ID, channel->eventq.index);
  954. falcon_write_table(efx, &evq_ptr, efx->type->evq_ptr_tbl_base,
  955. channel->channel);
  956. falcon_set_int_moderation(channel);
  957. }
  958. void falcon_fini_eventq(struct efx_channel *channel)
  959. {
  960. efx_oword_t eventq_ptr;
  961. struct efx_nic *efx = channel->efx;
  962. /* Remove event queue from card */
  963. EFX_ZERO_OWORD(eventq_ptr);
  964. falcon_write_table(efx, &eventq_ptr, efx->type->evq_ptr_tbl_base,
  965. channel->channel);
  966. /* Unpin event queue */
  967. falcon_fini_special_buffer(efx, &channel->eventq);
  968. }
  969. /* Free buffers backing event queue */
  970. void falcon_remove_eventq(struct efx_channel *channel)
  971. {
  972. falcon_free_special_buffer(channel->efx, &channel->eventq);
  973. }
  974. /* Generates a test event on the event queue. A subsequent call to
  975. * process_eventq() should pick up the event and place the value of
  976. * "magic" into channel->eventq_magic;
  977. */
  978. void falcon_generate_test_event(struct efx_channel *channel, unsigned int magic)
  979. {
  980. efx_qword_t test_event;
  981. EFX_POPULATE_QWORD_2(test_event,
  982. EV_CODE, DRV_GEN_EV_DECODE,
  983. EVQ_MAGIC, magic);
  984. falcon_generate_event(channel, &test_event);
  985. }
  986. /**************************************************************************
  987. *
  988. * Flush handling
  989. *
  990. **************************************************************************/
  991. static void falcon_poll_flush_events(struct efx_nic *efx)
  992. {
  993. struct efx_channel *channel = &efx->channel[0];
  994. struct efx_tx_queue *tx_queue;
  995. struct efx_rx_queue *rx_queue;
  996. unsigned int read_ptr, i;
  997. read_ptr = channel->eventq_read_ptr;
  998. for (i = 0; i < FALCON_EVQ_SIZE; ++i) {
  999. efx_qword_t *event = falcon_event(channel, read_ptr);
  1000. int ev_code, ev_sub_code, ev_queue;
  1001. bool ev_failed;
  1002. if (!falcon_event_present(event))
  1003. break;
  1004. ev_code = EFX_QWORD_FIELD(*event, EV_CODE);
  1005. if (ev_code != DRIVER_EV_DECODE)
  1006. continue;
  1007. ev_sub_code = EFX_QWORD_FIELD(*event, DRIVER_EV_SUB_CODE);
  1008. switch (ev_sub_code) {
  1009. case TX_DESCQ_FLS_DONE_EV_DECODE:
  1010. ev_queue = EFX_QWORD_FIELD(*event,
  1011. DRIVER_EV_TX_DESCQ_ID);
  1012. if (ev_queue < EFX_TX_QUEUE_COUNT) {
  1013. tx_queue = efx->tx_queue + ev_queue;
  1014. tx_queue->flushed = true;
  1015. }
  1016. break;
  1017. case RX_DESCQ_FLS_DONE_EV_DECODE:
  1018. ev_queue = EFX_QWORD_FIELD(*event,
  1019. DRIVER_EV_RX_DESCQ_ID);
  1020. ev_failed = EFX_QWORD_FIELD(*event,
  1021. DRIVER_EV_RX_FLUSH_FAIL);
  1022. if (ev_queue < efx->n_rx_queues) {
  1023. rx_queue = efx->rx_queue + ev_queue;
  1024. /* retry the rx flush */
  1025. if (ev_failed)
  1026. falcon_flush_rx_queue(rx_queue);
  1027. else
  1028. rx_queue->flushed = true;
  1029. }
  1030. break;
  1031. }
  1032. read_ptr = (read_ptr + 1) & FALCON_EVQ_MASK;
  1033. }
  1034. }
  1035. /* Handle tx and rx flushes at the same time, since they run in
  1036. * parallel in the hardware and there's no reason for us to
  1037. * serialise them */
  1038. int falcon_flush_queues(struct efx_nic *efx)
  1039. {
  1040. struct efx_rx_queue *rx_queue;
  1041. struct efx_tx_queue *tx_queue;
  1042. int i;
  1043. bool outstanding;
  1044. /* Issue flush requests */
  1045. efx_for_each_tx_queue(tx_queue, efx) {
  1046. tx_queue->flushed = false;
  1047. falcon_flush_tx_queue(tx_queue);
  1048. }
  1049. efx_for_each_rx_queue(rx_queue, efx) {
  1050. rx_queue->flushed = false;
  1051. falcon_flush_rx_queue(rx_queue);
  1052. }
  1053. /* Poll the evq looking for flush completions. Since we're not pushing
  1054. * any more rx or tx descriptors at this point, we're in no danger of
  1055. * overflowing the evq whilst we wait */
  1056. for (i = 0; i < FALCON_FLUSH_POLL_COUNT; ++i) {
  1057. msleep(FALCON_FLUSH_INTERVAL);
  1058. falcon_poll_flush_events(efx);
  1059. /* Check if every queue has been succesfully flushed */
  1060. outstanding = false;
  1061. efx_for_each_tx_queue(tx_queue, efx)
  1062. outstanding |= !tx_queue->flushed;
  1063. efx_for_each_rx_queue(rx_queue, efx)
  1064. outstanding |= !rx_queue->flushed;
  1065. if (!outstanding)
  1066. return 0;
  1067. }
  1068. /* Mark the queues as all flushed. We're going to return failure
  1069. * leading to a reset, or fake up success anyway. "flushed" now
  1070. * indicates that we tried to flush. */
  1071. efx_for_each_tx_queue(tx_queue, efx) {
  1072. if (!tx_queue->flushed)
  1073. EFX_ERR(efx, "tx queue %d flush command timed out\n",
  1074. tx_queue->queue);
  1075. tx_queue->flushed = true;
  1076. }
  1077. efx_for_each_rx_queue(rx_queue, efx) {
  1078. if (!rx_queue->flushed)
  1079. EFX_ERR(efx, "rx queue %d flush command timed out\n",
  1080. rx_queue->queue);
  1081. rx_queue->flushed = true;
  1082. }
  1083. if (EFX_WORKAROUND_7803(efx))
  1084. return 0;
  1085. return -ETIMEDOUT;
  1086. }
  1087. /**************************************************************************
  1088. *
  1089. * Falcon hardware interrupts
  1090. * The hardware interrupt handler does very little work; all the event
  1091. * queue processing is carried out by per-channel tasklets.
  1092. *
  1093. **************************************************************************/
  1094. /* Enable/disable/generate Falcon interrupts */
  1095. static inline void falcon_interrupts(struct efx_nic *efx, int enabled,
  1096. int force)
  1097. {
  1098. efx_oword_t int_en_reg_ker;
  1099. EFX_POPULATE_OWORD_2(int_en_reg_ker,
  1100. KER_INT_KER, force,
  1101. DRV_INT_EN_KER, enabled);
  1102. falcon_write(efx, &int_en_reg_ker, INT_EN_REG_KER);
  1103. }
  1104. void falcon_enable_interrupts(struct efx_nic *efx)
  1105. {
  1106. efx_oword_t int_adr_reg_ker;
  1107. struct efx_channel *channel;
  1108. EFX_ZERO_OWORD(*((efx_oword_t *) efx->irq_status.addr));
  1109. wmb(); /* Ensure interrupt vector is clear before interrupts enabled */
  1110. /* Program address */
  1111. EFX_POPULATE_OWORD_2(int_adr_reg_ker,
  1112. NORM_INT_VEC_DIS_KER, EFX_INT_MODE_USE_MSI(efx),
  1113. INT_ADR_KER, efx->irq_status.dma_addr);
  1114. falcon_write(efx, &int_adr_reg_ker, INT_ADR_REG_KER);
  1115. /* Enable interrupts */
  1116. falcon_interrupts(efx, 1, 0);
  1117. /* Force processing of all the channels to get the EVQ RPTRs up to
  1118. date */
  1119. efx_for_each_channel(channel, efx)
  1120. efx_schedule_channel(channel);
  1121. }
  1122. void falcon_disable_interrupts(struct efx_nic *efx)
  1123. {
  1124. /* Disable interrupts */
  1125. falcon_interrupts(efx, 0, 0);
  1126. }
  1127. /* Generate a Falcon test interrupt
  1128. * Interrupt must already have been enabled, otherwise nasty things
  1129. * may happen.
  1130. */
  1131. void falcon_generate_interrupt(struct efx_nic *efx)
  1132. {
  1133. falcon_interrupts(efx, 1, 1);
  1134. }
  1135. /* Acknowledge a legacy interrupt from Falcon
  1136. *
  1137. * This acknowledges a legacy (not MSI) interrupt via INT_ACK_KER_REG.
  1138. *
  1139. * Due to SFC bug 3706 (silicon revision <=A1) reads can be duplicated in the
  1140. * BIU. Interrupt acknowledge is read sensitive so must write instead
  1141. * (then read to ensure the BIU collector is flushed)
  1142. *
  1143. * NB most hardware supports MSI interrupts
  1144. */
  1145. static inline void falcon_irq_ack_a1(struct efx_nic *efx)
  1146. {
  1147. efx_dword_t reg;
  1148. EFX_POPULATE_DWORD_1(reg, INT_ACK_DUMMY_DATA, 0xb7eb7e);
  1149. falcon_writel(efx, &reg, INT_ACK_REG_KER_A1);
  1150. falcon_readl(efx, &reg, WORK_AROUND_BROKEN_PCI_READS_REG_KER_A1);
  1151. }
  1152. /* Process a fatal interrupt
  1153. * Disable bus mastering ASAP and schedule a reset
  1154. */
  1155. static irqreturn_t falcon_fatal_interrupt(struct efx_nic *efx)
  1156. {
  1157. struct falcon_nic_data *nic_data = efx->nic_data;
  1158. efx_oword_t *int_ker = efx->irq_status.addr;
  1159. efx_oword_t fatal_intr;
  1160. int error, mem_perr;
  1161. static int n_int_errors;
  1162. falcon_read(efx, &fatal_intr, FATAL_INTR_REG_KER);
  1163. error = EFX_OWORD_FIELD(fatal_intr, INT_KER_ERROR);
  1164. EFX_ERR(efx, "SYSTEM ERROR " EFX_OWORD_FMT " status "
  1165. EFX_OWORD_FMT ": %s\n", EFX_OWORD_VAL(*int_ker),
  1166. EFX_OWORD_VAL(fatal_intr),
  1167. error ? "disabling bus mastering" : "no recognised error");
  1168. if (error == 0)
  1169. goto out;
  1170. /* If this is a memory parity error dump which blocks are offending */
  1171. mem_perr = EFX_OWORD_FIELD(fatal_intr, MEM_PERR_INT_KER);
  1172. if (mem_perr) {
  1173. efx_oword_t reg;
  1174. falcon_read(efx, &reg, MEM_STAT_REG_KER);
  1175. EFX_ERR(efx, "SYSTEM ERROR: memory parity error "
  1176. EFX_OWORD_FMT "\n", EFX_OWORD_VAL(reg));
  1177. }
  1178. /* Disable DMA bus mastering on both devices */
  1179. pci_disable_device(efx->pci_dev);
  1180. if (FALCON_IS_DUAL_FUNC(efx))
  1181. pci_disable_device(nic_data->pci_dev2);
  1182. if (++n_int_errors < FALCON_MAX_INT_ERRORS) {
  1183. EFX_ERR(efx, "SYSTEM ERROR - reset scheduled\n");
  1184. efx_schedule_reset(efx, RESET_TYPE_INT_ERROR);
  1185. } else {
  1186. EFX_ERR(efx, "SYSTEM ERROR - max number of errors seen."
  1187. "NIC will be disabled\n");
  1188. efx_schedule_reset(efx, RESET_TYPE_DISABLE);
  1189. }
  1190. out:
  1191. return IRQ_HANDLED;
  1192. }
  1193. /* Handle a legacy interrupt from Falcon
  1194. * Acknowledges the interrupt and schedule event queue processing.
  1195. */
  1196. static irqreturn_t falcon_legacy_interrupt_b0(int irq, void *dev_id)
  1197. {
  1198. struct efx_nic *efx = dev_id;
  1199. efx_oword_t *int_ker = efx->irq_status.addr;
  1200. struct efx_channel *channel;
  1201. efx_dword_t reg;
  1202. u32 queues;
  1203. int syserr;
  1204. /* Read the ISR which also ACKs the interrupts */
  1205. falcon_readl(efx, &reg, INT_ISR0_B0);
  1206. queues = EFX_EXTRACT_DWORD(reg, 0, 31);
  1207. /* Check to see if we have a serious error condition */
  1208. syserr = EFX_OWORD_FIELD(*int_ker, FATAL_INT);
  1209. if (unlikely(syserr))
  1210. return falcon_fatal_interrupt(efx);
  1211. if (queues == 0)
  1212. return IRQ_NONE;
  1213. efx->last_irq_cpu = raw_smp_processor_id();
  1214. EFX_TRACE(efx, "IRQ %d on CPU %d status " EFX_DWORD_FMT "\n",
  1215. irq, raw_smp_processor_id(), EFX_DWORD_VAL(reg));
  1216. /* Schedule processing of any interrupting queues */
  1217. channel = &efx->channel[0];
  1218. while (queues) {
  1219. if (queues & 0x01)
  1220. efx_schedule_channel(channel);
  1221. channel++;
  1222. queues >>= 1;
  1223. }
  1224. return IRQ_HANDLED;
  1225. }
  1226. static irqreturn_t falcon_legacy_interrupt_a1(int irq, void *dev_id)
  1227. {
  1228. struct efx_nic *efx = dev_id;
  1229. efx_oword_t *int_ker = efx->irq_status.addr;
  1230. struct efx_channel *channel;
  1231. int syserr;
  1232. int queues;
  1233. /* Check to see if this is our interrupt. If it isn't, we
  1234. * exit without having touched the hardware.
  1235. */
  1236. if (unlikely(EFX_OWORD_IS_ZERO(*int_ker))) {
  1237. EFX_TRACE(efx, "IRQ %d on CPU %d not for me\n", irq,
  1238. raw_smp_processor_id());
  1239. return IRQ_NONE;
  1240. }
  1241. efx->last_irq_cpu = raw_smp_processor_id();
  1242. EFX_TRACE(efx, "IRQ %d on CPU %d status " EFX_OWORD_FMT "\n",
  1243. irq, raw_smp_processor_id(), EFX_OWORD_VAL(*int_ker));
  1244. /* Check to see if we have a serious error condition */
  1245. syserr = EFX_OWORD_FIELD(*int_ker, FATAL_INT);
  1246. if (unlikely(syserr))
  1247. return falcon_fatal_interrupt(efx);
  1248. /* Determine interrupting queues, clear interrupt status
  1249. * register and acknowledge the device interrupt.
  1250. */
  1251. BUILD_BUG_ON(INT_EVQS_WIDTH > EFX_MAX_CHANNELS);
  1252. queues = EFX_OWORD_FIELD(*int_ker, INT_EVQS);
  1253. EFX_ZERO_OWORD(*int_ker);
  1254. wmb(); /* Ensure the vector is cleared before interrupt ack */
  1255. falcon_irq_ack_a1(efx);
  1256. /* Schedule processing of any interrupting queues */
  1257. channel = &efx->channel[0];
  1258. while (queues) {
  1259. if (queues & 0x01)
  1260. efx_schedule_channel(channel);
  1261. channel++;
  1262. queues >>= 1;
  1263. }
  1264. return IRQ_HANDLED;
  1265. }
  1266. /* Handle an MSI interrupt from Falcon
  1267. *
  1268. * Handle an MSI hardware interrupt. This routine schedules event
  1269. * queue processing. No interrupt acknowledgement cycle is necessary.
  1270. * Also, we never need to check that the interrupt is for us, since
  1271. * MSI interrupts cannot be shared.
  1272. */
  1273. static irqreturn_t falcon_msi_interrupt(int irq, void *dev_id)
  1274. {
  1275. struct efx_channel *channel = dev_id;
  1276. struct efx_nic *efx = channel->efx;
  1277. efx_oword_t *int_ker = efx->irq_status.addr;
  1278. int syserr;
  1279. efx->last_irq_cpu = raw_smp_processor_id();
  1280. EFX_TRACE(efx, "IRQ %d on CPU %d status " EFX_OWORD_FMT "\n",
  1281. irq, raw_smp_processor_id(), EFX_OWORD_VAL(*int_ker));
  1282. /* Check to see if we have a serious error condition */
  1283. syserr = EFX_OWORD_FIELD(*int_ker, FATAL_INT);
  1284. if (unlikely(syserr))
  1285. return falcon_fatal_interrupt(efx);
  1286. /* Schedule processing of the channel */
  1287. efx_schedule_channel(channel);
  1288. return IRQ_HANDLED;
  1289. }
  1290. /* Setup RSS indirection table.
  1291. * This maps from the hash value of the packet to RXQ
  1292. */
  1293. static void falcon_setup_rss_indir_table(struct efx_nic *efx)
  1294. {
  1295. int i = 0;
  1296. unsigned long offset;
  1297. efx_dword_t dword;
  1298. if (falcon_rev(efx) < FALCON_REV_B0)
  1299. return;
  1300. for (offset = RX_RSS_INDIR_TBL_B0;
  1301. offset < RX_RSS_INDIR_TBL_B0 + 0x800;
  1302. offset += 0x10) {
  1303. EFX_POPULATE_DWORD_1(dword, RX_RSS_INDIR_ENT_B0,
  1304. i % efx->n_rx_queues);
  1305. falcon_writel(efx, &dword, offset);
  1306. i++;
  1307. }
  1308. }
  1309. /* Hook interrupt handler(s)
  1310. * Try MSI and then legacy interrupts.
  1311. */
  1312. int falcon_init_interrupt(struct efx_nic *efx)
  1313. {
  1314. struct efx_channel *channel;
  1315. int rc;
  1316. if (!EFX_INT_MODE_USE_MSI(efx)) {
  1317. irq_handler_t handler;
  1318. if (falcon_rev(efx) >= FALCON_REV_B0)
  1319. handler = falcon_legacy_interrupt_b0;
  1320. else
  1321. handler = falcon_legacy_interrupt_a1;
  1322. rc = request_irq(efx->legacy_irq, handler, IRQF_SHARED,
  1323. efx->name, efx);
  1324. if (rc) {
  1325. EFX_ERR(efx, "failed to hook legacy IRQ %d\n",
  1326. efx->pci_dev->irq);
  1327. goto fail1;
  1328. }
  1329. return 0;
  1330. }
  1331. /* Hook MSI or MSI-X interrupt */
  1332. efx_for_each_channel(channel, efx) {
  1333. rc = request_irq(channel->irq, falcon_msi_interrupt,
  1334. IRQF_PROBE_SHARED, /* Not shared */
  1335. efx->name, channel);
  1336. if (rc) {
  1337. EFX_ERR(efx, "failed to hook IRQ %d\n", channel->irq);
  1338. goto fail2;
  1339. }
  1340. }
  1341. return 0;
  1342. fail2:
  1343. efx_for_each_channel(channel, efx)
  1344. free_irq(channel->irq, channel);
  1345. fail1:
  1346. return rc;
  1347. }
  1348. void falcon_fini_interrupt(struct efx_nic *efx)
  1349. {
  1350. struct efx_channel *channel;
  1351. efx_oword_t reg;
  1352. /* Disable MSI/MSI-X interrupts */
  1353. efx_for_each_channel(channel, efx) {
  1354. if (channel->irq)
  1355. free_irq(channel->irq, channel);
  1356. }
  1357. /* ACK legacy interrupt */
  1358. if (falcon_rev(efx) >= FALCON_REV_B0)
  1359. falcon_read(efx, &reg, INT_ISR0_B0);
  1360. else
  1361. falcon_irq_ack_a1(efx);
  1362. /* Disable legacy interrupt */
  1363. if (efx->legacy_irq)
  1364. free_irq(efx->legacy_irq, efx);
  1365. }
  1366. /**************************************************************************
  1367. *
  1368. * EEPROM/flash
  1369. *
  1370. **************************************************************************
  1371. */
  1372. #define FALCON_SPI_MAX_LEN sizeof(efx_oword_t)
  1373. /* Wait for SPI command completion */
  1374. static int falcon_spi_wait(struct efx_nic *efx)
  1375. {
  1376. unsigned long timeout = jiffies + DIV_ROUND_UP(HZ, 10);
  1377. efx_oword_t reg;
  1378. bool cmd_en, timer_active;
  1379. for (;;) {
  1380. falcon_read(efx, &reg, EE_SPI_HCMD_REG_KER);
  1381. cmd_en = EFX_OWORD_FIELD(reg, EE_SPI_HCMD_CMD_EN);
  1382. timer_active = EFX_OWORD_FIELD(reg, EE_WR_TIMER_ACTIVE);
  1383. if (!cmd_en && !timer_active)
  1384. return 0;
  1385. if (time_after_eq(jiffies, timeout)) {
  1386. EFX_ERR(efx, "timed out waiting for SPI\n");
  1387. return -ETIMEDOUT;
  1388. }
  1389. cpu_relax();
  1390. }
  1391. }
  1392. static int falcon_spi_cmd(const struct efx_spi_device *spi,
  1393. unsigned int command, int address,
  1394. const void *in, void *out, unsigned int len)
  1395. {
  1396. struct efx_nic *efx = spi->efx;
  1397. bool addressed = (address >= 0);
  1398. bool reading = (out != NULL);
  1399. efx_oword_t reg;
  1400. int rc;
  1401. /* Input validation */
  1402. if (len > FALCON_SPI_MAX_LEN)
  1403. return -EINVAL;
  1404. /* Check SPI not currently being accessed */
  1405. rc = falcon_spi_wait(efx);
  1406. if (rc)
  1407. return rc;
  1408. /* Program address register, if we have an address */
  1409. if (addressed) {
  1410. EFX_POPULATE_OWORD_1(reg, EE_SPI_HADR_ADR, address);
  1411. falcon_write(efx, &reg, EE_SPI_HADR_REG_KER);
  1412. }
  1413. /* Program data register, if we have data */
  1414. if (in != NULL) {
  1415. memcpy(&reg, in, len);
  1416. falcon_write(efx, &reg, EE_SPI_HDATA_REG_KER);
  1417. }
  1418. /* Issue read/write command */
  1419. EFX_POPULATE_OWORD_7(reg,
  1420. EE_SPI_HCMD_CMD_EN, 1,
  1421. EE_SPI_HCMD_SF_SEL, spi->device_id,
  1422. EE_SPI_HCMD_DABCNT, len,
  1423. EE_SPI_HCMD_READ, reading,
  1424. EE_SPI_HCMD_DUBCNT, 0,
  1425. EE_SPI_HCMD_ADBCNT,
  1426. (addressed ? spi->addr_len : 0),
  1427. EE_SPI_HCMD_ENC, command);
  1428. falcon_write(efx, &reg, EE_SPI_HCMD_REG_KER);
  1429. /* Wait for read/write to complete */
  1430. rc = falcon_spi_wait(efx);
  1431. if (rc)
  1432. return rc;
  1433. /* Read data */
  1434. if (out != NULL) {
  1435. falcon_read(efx, &reg, EE_SPI_HDATA_REG_KER);
  1436. memcpy(out, &reg, len);
  1437. }
  1438. return 0;
  1439. }
  1440. static unsigned int
  1441. falcon_spi_write_limit(const struct efx_spi_device *spi, unsigned int start)
  1442. {
  1443. return min(FALCON_SPI_MAX_LEN,
  1444. (spi->block_size - (start & (spi->block_size - 1))));
  1445. }
  1446. static inline u8
  1447. efx_spi_munge_command(const struct efx_spi_device *spi,
  1448. const u8 command, const unsigned int address)
  1449. {
  1450. return command | (((address >> 8) & spi->munge_address) << 3);
  1451. }
  1452. static int falcon_spi_fast_wait(const struct efx_spi_device *spi)
  1453. {
  1454. u8 status;
  1455. int i, rc;
  1456. /* Wait up to 1000us for flash/EEPROM to finish a fast operation. */
  1457. for (i = 0; i < 50; i++) {
  1458. udelay(20);
  1459. rc = falcon_spi_cmd(spi, SPI_RDSR, -1, NULL,
  1460. &status, sizeof(status));
  1461. if (rc)
  1462. return rc;
  1463. if (!(status & SPI_STATUS_NRDY))
  1464. return 0;
  1465. }
  1466. EFX_ERR(spi->efx,
  1467. "timed out waiting for device %d last status=0x%02x\n",
  1468. spi->device_id, status);
  1469. return -ETIMEDOUT;
  1470. }
  1471. int falcon_spi_read(const struct efx_spi_device *spi, loff_t start,
  1472. size_t len, size_t *retlen, u8 *buffer)
  1473. {
  1474. unsigned int command, block_len, pos = 0;
  1475. int rc = 0;
  1476. while (pos < len) {
  1477. block_len = min((unsigned int)len - pos,
  1478. FALCON_SPI_MAX_LEN);
  1479. command = efx_spi_munge_command(spi, SPI_READ, start + pos);
  1480. rc = falcon_spi_cmd(spi, command, start + pos, NULL,
  1481. buffer + pos, block_len);
  1482. if (rc)
  1483. break;
  1484. pos += block_len;
  1485. /* Avoid locking up the system */
  1486. cond_resched();
  1487. if (signal_pending(current)) {
  1488. rc = -EINTR;
  1489. break;
  1490. }
  1491. }
  1492. if (retlen)
  1493. *retlen = pos;
  1494. return rc;
  1495. }
  1496. int falcon_spi_write(const struct efx_spi_device *spi, loff_t start,
  1497. size_t len, size_t *retlen, const u8 *buffer)
  1498. {
  1499. u8 verify_buffer[FALCON_SPI_MAX_LEN];
  1500. unsigned int command, block_len, pos = 0;
  1501. int rc = 0;
  1502. while (pos < len) {
  1503. rc = falcon_spi_cmd(spi, SPI_WREN, -1, NULL, NULL, 0);
  1504. if (rc)
  1505. break;
  1506. block_len = min((unsigned int)len - pos,
  1507. falcon_spi_write_limit(spi, start + pos));
  1508. command = efx_spi_munge_command(spi, SPI_WRITE, start + pos);
  1509. rc = falcon_spi_cmd(spi, command, start + pos,
  1510. buffer + pos, NULL, block_len);
  1511. if (rc)
  1512. break;
  1513. rc = falcon_spi_fast_wait(spi);
  1514. if (rc)
  1515. break;
  1516. command = efx_spi_munge_command(spi, SPI_READ, start + pos);
  1517. rc = falcon_spi_cmd(spi, command, start + pos,
  1518. NULL, verify_buffer, block_len);
  1519. if (memcmp(verify_buffer, buffer + pos, block_len)) {
  1520. rc = -EIO;
  1521. break;
  1522. }
  1523. pos += block_len;
  1524. /* Avoid locking up the system */
  1525. cond_resched();
  1526. if (signal_pending(current)) {
  1527. rc = -EINTR;
  1528. break;
  1529. }
  1530. }
  1531. if (retlen)
  1532. *retlen = pos;
  1533. return rc;
  1534. }
  1535. /**************************************************************************
  1536. *
  1537. * MAC wrapper
  1538. *
  1539. **************************************************************************
  1540. */
  1541. void falcon_drain_tx_fifo(struct efx_nic *efx)
  1542. {
  1543. efx_oword_t temp;
  1544. int count;
  1545. if ((falcon_rev(efx) < FALCON_REV_B0) ||
  1546. (efx->loopback_mode != LOOPBACK_NONE))
  1547. return;
  1548. falcon_read(efx, &temp, MAC0_CTRL_REG_KER);
  1549. /* There is no point in draining more than once */
  1550. if (EFX_OWORD_FIELD(temp, TXFIFO_DRAIN_EN_B0))
  1551. return;
  1552. /* MAC stats will fail whilst the TX fifo is draining. Serialise
  1553. * the drain sequence with the statistics fetch */
  1554. spin_lock(&efx->stats_lock);
  1555. EFX_SET_OWORD_FIELD(temp, TXFIFO_DRAIN_EN_B0, 1);
  1556. falcon_write(efx, &temp, MAC0_CTRL_REG_KER);
  1557. /* Reset the MAC and EM block. */
  1558. falcon_read(efx, &temp, GLB_CTL_REG_KER);
  1559. EFX_SET_OWORD_FIELD(temp, RST_XGTX, 1);
  1560. EFX_SET_OWORD_FIELD(temp, RST_XGRX, 1);
  1561. EFX_SET_OWORD_FIELD(temp, RST_EM, 1);
  1562. falcon_write(efx, &temp, GLB_CTL_REG_KER);
  1563. count = 0;
  1564. while (1) {
  1565. falcon_read(efx, &temp, GLB_CTL_REG_KER);
  1566. if (!EFX_OWORD_FIELD(temp, RST_XGTX) &&
  1567. !EFX_OWORD_FIELD(temp, RST_XGRX) &&
  1568. !EFX_OWORD_FIELD(temp, RST_EM)) {
  1569. EFX_LOG(efx, "Completed MAC reset after %d loops\n",
  1570. count);
  1571. break;
  1572. }
  1573. if (count > 20) {
  1574. EFX_ERR(efx, "MAC reset failed\n");
  1575. break;
  1576. }
  1577. count++;
  1578. udelay(10);
  1579. }
  1580. spin_unlock(&efx->stats_lock);
  1581. /* If we've reset the EM block and the link is up, then
  1582. * we'll have to kick the XAUI link so the PHY can recover */
  1583. if (efx->link_up && EFX_WORKAROUND_5147(efx))
  1584. falcon_reset_xaui(efx);
  1585. }
  1586. void falcon_deconfigure_mac_wrapper(struct efx_nic *efx)
  1587. {
  1588. efx_oword_t temp;
  1589. if (falcon_rev(efx) < FALCON_REV_B0)
  1590. return;
  1591. /* Isolate the MAC -> RX */
  1592. falcon_read(efx, &temp, RX_CFG_REG_KER);
  1593. EFX_SET_OWORD_FIELD(temp, RX_INGR_EN_B0, 0);
  1594. falcon_write(efx, &temp, RX_CFG_REG_KER);
  1595. if (!efx->link_up)
  1596. falcon_drain_tx_fifo(efx);
  1597. }
  1598. void falcon_reconfigure_mac_wrapper(struct efx_nic *efx)
  1599. {
  1600. efx_oword_t reg;
  1601. int link_speed;
  1602. bool tx_fc;
  1603. if (efx->link_options & GM_LPA_10000)
  1604. link_speed = 0x3;
  1605. else if (efx->link_options & GM_LPA_1000)
  1606. link_speed = 0x2;
  1607. else if (efx->link_options & GM_LPA_100)
  1608. link_speed = 0x1;
  1609. else
  1610. link_speed = 0x0;
  1611. /* MAC_LINK_STATUS controls MAC backpressure but doesn't work
  1612. * as advertised. Disable to ensure packets are not
  1613. * indefinitely held and TX queue can be flushed at any point
  1614. * while the link is down. */
  1615. EFX_POPULATE_OWORD_5(reg,
  1616. MAC_XOFF_VAL, 0xffff /* max pause time */,
  1617. MAC_BCAD_ACPT, 1,
  1618. MAC_UC_PROM, efx->promiscuous,
  1619. MAC_LINK_STATUS, 1, /* always set */
  1620. MAC_SPEED, link_speed);
  1621. /* On B0, MAC backpressure can be disabled and packets get
  1622. * discarded. */
  1623. if (falcon_rev(efx) >= FALCON_REV_B0) {
  1624. EFX_SET_OWORD_FIELD(reg, TXFIFO_DRAIN_EN_B0,
  1625. !efx->link_up);
  1626. }
  1627. falcon_write(efx, &reg, MAC0_CTRL_REG_KER);
  1628. /* Restore the multicast hash registers. */
  1629. falcon_set_multicast_hash(efx);
  1630. /* Transmission of pause frames when RX crosses the threshold is
  1631. * covered by RX_XOFF_MAC_EN and XM_TX_CFG_REG:XM_FCNTL.
  1632. * Action on receipt of pause frames is controller by XM_DIS_FCNTL */
  1633. tx_fc = !!(efx->flow_control & EFX_FC_TX);
  1634. falcon_read(efx, &reg, RX_CFG_REG_KER);
  1635. EFX_SET_OWORD_FIELD_VER(efx, reg, RX_XOFF_MAC_EN, tx_fc);
  1636. /* Unisolate the MAC -> RX */
  1637. if (falcon_rev(efx) >= FALCON_REV_B0)
  1638. EFX_SET_OWORD_FIELD(reg, RX_INGR_EN_B0, 1);
  1639. falcon_write(efx, &reg, RX_CFG_REG_KER);
  1640. }
  1641. int falcon_dma_stats(struct efx_nic *efx, unsigned int done_offset)
  1642. {
  1643. efx_oword_t reg;
  1644. u32 *dma_done;
  1645. int i;
  1646. if (disable_dma_stats)
  1647. return 0;
  1648. /* Statistics fetch will fail if the MAC is in TX drain */
  1649. if (falcon_rev(efx) >= FALCON_REV_B0) {
  1650. efx_oword_t temp;
  1651. falcon_read(efx, &temp, MAC0_CTRL_REG_KER);
  1652. if (EFX_OWORD_FIELD(temp, TXFIFO_DRAIN_EN_B0))
  1653. return 0;
  1654. }
  1655. dma_done = (efx->stats_buffer.addr + done_offset);
  1656. *dma_done = FALCON_STATS_NOT_DONE;
  1657. wmb(); /* ensure done flag is clear */
  1658. /* Initiate DMA transfer of stats */
  1659. EFX_POPULATE_OWORD_2(reg,
  1660. MAC_STAT_DMA_CMD, 1,
  1661. MAC_STAT_DMA_ADR,
  1662. efx->stats_buffer.dma_addr);
  1663. falcon_write(efx, &reg, MAC0_STAT_DMA_REG_KER);
  1664. /* Wait for transfer to complete */
  1665. for (i = 0; i < 400; i++) {
  1666. if (*(volatile u32 *)dma_done == FALCON_STATS_DONE) {
  1667. rmb(); /* Ensure the stats are valid. */
  1668. return 0;
  1669. }
  1670. udelay(10);
  1671. }
  1672. EFX_ERR(efx, "timed out waiting for statistics\n");
  1673. return -ETIMEDOUT;
  1674. }
  1675. /**************************************************************************
  1676. *
  1677. * PHY access via GMII
  1678. *
  1679. **************************************************************************
  1680. */
  1681. /* Use the top bit of the MII PHY id to indicate the PHY type
  1682. * (1G/10G), with the remaining bits as the actual PHY id.
  1683. *
  1684. * This allows us to avoid leaking information from the mii_if_info
  1685. * structure into other data structures.
  1686. */
  1687. #define FALCON_PHY_ID_ID_WIDTH EFX_WIDTH(MD_PRT_DEV_ADR)
  1688. #define FALCON_PHY_ID_ID_MASK ((1 << FALCON_PHY_ID_ID_WIDTH) - 1)
  1689. #define FALCON_PHY_ID_WIDTH (FALCON_PHY_ID_ID_WIDTH + 1)
  1690. #define FALCON_PHY_ID_MASK ((1 << FALCON_PHY_ID_WIDTH) - 1)
  1691. #define FALCON_PHY_ID_10G (1 << (FALCON_PHY_ID_WIDTH - 1))
  1692. /* Packing the clause 45 port and device fields into a single value */
  1693. #define MD_PRT_ADR_COMP_LBN (MD_PRT_ADR_LBN - MD_DEV_ADR_LBN)
  1694. #define MD_PRT_ADR_COMP_WIDTH MD_PRT_ADR_WIDTH
  1695. #define MD_DEV_ADR_COMP_LBN 0
  1696. #define MD_DEV_ADR_COMP_WIDTH MD_DEV_ADR_WIDTH
  1697. /* Wait for GMII access to complete */
  1698. static int falcon_gmii_wait(struct efx_nic *efx)
  1699. {
  1700. efx_dword_t md_stat;
  1701. int count;
  1702. for (count = 0; count < 1000; count++) { /* wait upto 10ms */
  1703. falcon_readl(efx, &md_stat, MD_STAT_REG_KER);
  1704. if (EFX_DWORD_FIELD(md_stat, MD_BSY) == 0) {
  1705. if (EFX_DWORD_FIELD(md_stat, MD_LNFL) != 0 ||
  1706. EFX_DWORD_FIELD(md_stat, MD_BSERR) != 0) {
  1707. EFX_ERR(efx, "error from GMII access "
  1708. EFX_DWORD_FMT"\n",
  1709. EFX_DWORD_VAL(md_stat));
  1710. return -EIO;
  1711. }
  1712. return 0;
  1713. }
  1714. udelay(10);
  1715. }
  1716. EFX_ERR(efx, "timed out waiting for GMII\n");
  1717. return -ETIMEDOUT;
  1718. }
  1719. /* Writes a GMII register of a PHY connected to Falcon using MDIO. */
  1720. static void falcon_mdio_write(struct net_device *net_dev, int phy_id,
  1721. int addr, int value)
  1722. {
  1723. struct efx_nic *efx = netdev_priv(net_dev);
  1724. unsigned int phy_id2 = phy_id & FALCON_PHY_ID_ID_MASK;
  1725. efx_oword_t reg;
  1726. /* The 'generic' prt/dev packing in mdio_10g.h is conveniently
  1727. * chosen so that the only current user, Falcon, can take the
  1728. * packed value and use them directly.
  1729. * Fail to build if this assumption is broken.
  1730. */
  1731. BUILD_BUG_ON(FALCON_PHY_ID_10G != MDIO45_XPRT_ID_IS10G);
  1732. BUILD_BUG_ON(FALCON_PHY_ID_ID_WIDTH != MDIO45_PRT_DEV_WIDTH);
  1733. BUILD_BUG_ON(MD_PRT_ADR_COMP_LBN != MDIO45_PRT_ID_COMP_LBN);
  1734. BUILD_BUG_ON(MD_DEV_ADR_COMP_LBN != MDIO45_DEV_ID_COMP_LBN);
  1735. if (phy_id2 == PHY_ADDR_INVALID)
  1736. return;
  1737. /* See falcon_mdio_read for an explanation. */
  1738. if (!(phy_id & FALCON_PHY_ID_10G)) {
  1739. int mmd = ffs(efx->phy_op->mmds) - 1;
  1740. EFX_TRACE(efx, "Fixing erroneous clause22 write\n");
  1741. phy_id2 = mdio_clause45_pack(phy_id2, mmd)
  1742. & FALCON_PHY_ID_ID_MASK;
  1743. }
  1744. EFX_REGDUMP(efx, "writing GMII %d register %02x with %04x\n", phy_id,
  1745. addr, value);
  1746. spin_lock_bh(&efx->phy_lock);
  1747. /* Check MII not currently being accessed */
  1748. if (falcon_gmii_wait(efx) != 0)
  1749. goto out;
  1750. /* Write the address/ID register */
  1751. EFX_POPULATE_OWORD_1(reg, MD_PHY_ADR, addr);
  1752. falcon_write(efx, &reg, MD_PHY_ADR_REG_KER);
  1753. EFX_POPULATE_OWORD_1(reg, MD_PRT_DEV_ADR, phy_id2);
  1754. falcon_write(efx, &reg, MD_ID_REG_KER);
  1755. /* Write data */
  1756. EFX_POPULATE_OWORD_1(reg, MD_TXD, value);
  1757. falcon_write(efx, &reg, MD_TXD_REG_KER);
  1758. EFX_POPULATE_OWORD_2(reg,
  1759. MD_WRC, 1,
  1760. MD_GC, 0);
  1761. falcon_write(efx, &reg, MD_CS_REG_KER);
  1762. /* Wait for data to be written */
  1763. if (falcon_gmii_wait(efx) != 0) {
  1764. /* Abort the write operation */
  1765. EFX_POPULATE_OWORD_2(reg,
  1766. MD_WRC, 0,
  1767. MD_GC, 1);
  1768. falcon_write(efx, &reg, MD_CS_REG_KER);
  1769. udelay(10);
  1770. }
  1771. out:
  1772. spin_unlock_bh(&efx->phy_lock);
  1773. }
  1774. /* Reads a GMII register from a PHY connected to Falcon. If no value
  1775. * could be read, -1 will be returned. */
  1776. static int falcon_mdio_read(struct net_device *net_dev, int phy_id, int addr)
  1777. {
  1778. struct efx_nic *efx = netdev_priv(net_dev);
  1779. unsigned int phy_addr = phy_id & FALCON_PHY_ID_ID_MASK;
  1780. efx_oword_t reg;
  1781. int value = -1;
  1782. if (phy_addr == PHY_ADDR_INVALID)
  1783. return -1;
  1784. /* Our PHY code knows whether it needs to talk clause 22(1G) or 45(10G)
  1785. * but the generic Linux code does not make any distinction or have
  1786. * any state for this.
  1787. * We spot the case where someone tried to talk 22 to a 45 PHY and
  1788. * redirect the request to the lowest numbered MMD as a clause45
  1789. * request. This is enough to allow simple queries like id and link
  1790. * state to succeed. TODO: We may need to do more in future.
  1791. */
  1792. if (!(phy_id & FALCON_PHY_ID_10G)) {
  1793. int mmd = ffs(efx->phy_op->mmds) - 1;
  1794. EFX_TRACE(efx, "Fixing erroneous clause22 read\n");
  1795. phy_addr = mdio_clause45_pack(phy_addr, mmd)
  1796. & FALCON_PHY_ID_ID_MASK;
  1797. }
  1798. spin_lock_bh(&efx->phy_lock);
  1799. /* Check MII not currently being accessed */
  1800. if (falcon_gmii_wait(efx) != 0)
  1801. goto out;
  1802. EFX_POPULATE_OWORD_1(reg, MD_PHY_ADR, addr);
  1803. falcon_write(efx, &reg, MD_PHY_ADR_REG_KER);
  1804. EFX_POPULATE_OWORD_1(reg, MD_PRT_DEV_ADR, phy_addr);
  1805. falcon_write(efx, &reg, MD_ID_REG_KER);
  1806. /* Request data to be read */
  1807. EFX_POPULATE_OWORD_2(reg, MD_RDC, 1, MD_GC, 0);
  1808. falcon_write(efx, &reg, MD_CS_REG_KER);
  1809. /* Wait for data to become available */
  1810. value = falcon_gmii_wait(efx);
  1811. if (value == 0) {
  1812. falcon_read(efx, &reg, MD_RXD_REG_KER);
  1813. value = EFX_OWORD_FIELD(reg, MD_RXD);
  1814. EFX_REGDUMP(efx, "read from GMII %d register %02x, got %04x\n",
  1815. phy_id, addr, value);
  1816. } else {
  1817. /* Abort the read operation */
  1818. EFX_POPULATE_OWORD_2(reg,
  1819. MD_RIC, 0,
  1820. MD_GC, 1);
  1821. falcon_write(efx, &reg, MD_CS_REG_KER);
  1822. EFX_LOG(efx, "read from GMII 0x%x register %02x, got "
  1823. "error %d\n", phy_id, addr, value);
  1824. }
  1825. out:
  1826. spin_unlock_bh(&efx->phy_lock);
  1827. return value;
  1828. }
  1829. static void falcon_init_mdio(struct mii_if_info *gmii)
  1830. {
  1831. gmii->mdio_read = falcon_mdio_read;
  1832. gmii->mdio_write = falcon_mdio_write;
  1833. gmii->phy_id_mask = FALCON_PHY_ID_MASK;
  1834. gmii->reg_num_mask = ((1 << EFX_WIDTH(MD_PHY_ADR)) - 1);
  1835. }
  1836. static int falcon_probe_phy(struct efx_nic *efx)
  1837. {
  1838. switch (efx->phy_type) {
  1839. case PHY_TYPE_10XPRESS:
  1840. efx->phy_op = &falcon_tenxpress_phy_ops;
  1841. break;
  1842. case PHY_TYPE_XFP:
  1843. efx->phy_op = &falcon_xfp_phy_ops;
  1844. break;
  1845. default:
  1846. EFX_ERR(efx, "Unknown PHY type %d\n",
  1847. efx->phy_type);
  1848. return -1;
  1849. }
  1850. efx->loopback_modes = LOOPBACKS_10G_INTERNAL | efx->phy_op->loopbacks;
  1851. return 0;
  1852. }
  1853. /* This call is responsible for hooking in the MAC and PHY operations */
  1854. int falcon_probe_port(struct efx_nic *efx)
  1855. {
  1856. int rc;
  1857. /* Hook in PHY operations table */
  1858. rc = falcon_probe_phy(efx);
  1859. if (rc)
  1860. return rc;
  1861. /* Set up GMII structure for PHY */
  1862. efx->mii.supports_gmii = true;
  1863. falcon_init_mdio(&efx->mii);
  1864. /* Hardware flow ctrl. FalconA RX FIFO too small for pause generation */
  1865. if (falcon_rev(efx) >= FALCON_REV_B0)
  1866. efx->flow_control = EFX_FC_RX | EFX_FC_TX;
  1867. else
  1868. efx->flow_control = EFX_FC_RX;
  1869. /* Allocate buffer for stats */
  1870. rc = falcon_alloc_buffer(efx, &efx->stats_buffer,
  1871. FALCON_MAC_STATS_SIZE);
  1872. if (rc)
  1873. return rc;
  1874. EFX_LOG(efx, "stats buffer at %llx (virt %p phys %lx)\n",
  1875. (unsigned long long)efx->stats_buffer.dma_addr,
  1876. efx->stats_buffer.addr,
  1877. virt_to_phys(efx->stats_buffer.addr));
  1878. return 0;
  1879. }
  1880. void falcon_remove_port(struct efx_nic *efx)
  1881. {
  1882. falcon_free_buffer(efx, &efx->stats_buffer);
  1883. }
  1884. /**************************************************************************
  1885. *
  1886. * Multicast filtering
  1887. *
  1888. **************************************************************************
  1889. */
  1890. void falcon_set_multicast_hash(struct efx_nic *efx)
  1891. {
  1892. union efx_multicast_hash *mc_hash = &efx->multicast_hash;
  1893. /* Broadcast packets go through the multicast hash filter.
  1894. * ether_crc_le() of the broadcast address is 0xbe2612ff
  1895. * so we always add bit 0xff to the mask.
  1896. */
  1897. set_bit_le(0xff, mc_hash->byte);
  1898. falcon_write(efx, &mc_hash->oword[0], MAC_MCAST_HASH_REG0_KER);
  1899. falcon_write(efx, &mc_hash->oword[1], MAC_MCAST_HASH_REG1_KER);
  1900. }
  1901. /**************************************************************************
  1902. *
  1903. * Falcon test code
  1904. *
  1905. **************************************************************************/
  1906. int falcon_read_nvram(struct efx_nic *efx, struct falcon_nvconfig *nvconfig_out)
  1907. {
  1908. struct falcon_nvconfig *nvconfig;
  1909. struct efx_spi_device *spi;
  1910. void *region;
  1911. int rc, magic_num, struct_ver;
  1912. __le16 *word, *limit;
  1913. u32 csum;
  1914. region = kmalloc(NVCONFIG_END, GFP_KERNEL);
  1915. if (!region)
  1916. return -ENOMEM;
  1917. nvconfig = region + NVCONFIG_OFFSET;
  1918. spi = efx->spi_flash ? efx->spi_flash : efx->spi_eeprom;
  1919. rc = falcon_spi_read(spi, 0, NVCONFIG_END, NULL, region);
  1920. if (rc) {
  1921. EFX_ERR(efx, "Failed to read %s\n",
  1922. efx->spi_flash ? "flash" : "EEPROM");
  1923. rc = -EIO;
  1924. goto out;
  1925. }
  1926. magic_num = le16_to_cpu(nvconfig->board_magic_num);
  1927. struct_ver = le16_to_cpu(nvconfig->board_struct_ver);
  1928. rc = -EINVAL;
  1929. if (magic_num != NVCONFIG_BOARD_MAGIC_NUM) {
  1930. EFX_ERR(efx, "NVRAM bad magic 0x%x\n", magic_num);
  1931. goto out;
  1932. }
  1933. if (struct_ver < 2) {
  1934. EFX_ERR(efx, "NVRAM has ancient version 0x%x\n", struct_ver);
  1935. goto out;
  1936. } else if (struct_ver < 4) {
  1937. word = &nvconfig->board_magic_num;
  1938. limit = (__le16 *) (nvconfig + 1);
  1939. } else {
  1940. word = region;
  1941. limit = region + NVCONFIG_END;
  1942. }
  1943. for (csum = 0; word < limit; ++word)
  1944. csum += le16_to_cpu(*word);
  1945. if (~csum & 0xffff) {
  1946. EFX_ERR(efx, "NVRAM has incorrect checksum\n");
  1947. goto out;
  1948. }
  1949. rc = 0;
  1950. if (nvconfig_out)
  1951. memcpy(nvconfig_out, nvconfig, sizeof(*nvconfig));
  1952. out:
  1953. kfree(region);
  1954. return rc;
  1955. }
  1956. /* Registers tested in the falcon register test */
  1957. static struct {
  1958. unsigned address;
  1959. efx_oword_t mask;
  1960. } efx_test_registers[] = {
  1961. { ADR_REGION_REG_KER,
  1962. EFX_OWORD32(0x0001FFFF, 0x0001FFFF, 0x0001FFFF, 0x0001FFFF) },
  1963. { RX_CFG_REG_KER,
  1964. EFX_OWORD32(0xFFFFFFFE, 0x00017FFF, 0x00000000, 0x00000000) },
  1965. { TX_CFG_REG_KER,
  1966. EFX_OWORD32(0x7FFF0037, 0x00000000, 0x00000000, 0x00000000) },
  1967. { TX_CFG2_REG_KER,
  1968. EFX_OWORD32(0xFFFEFE80, 0x1FFFFFFF, 0x020000FE, 0x007FFFFF) },
  1969. { MAC0_CTRL_REG_KER,
  1970. EFX_OWORD32(0xFFFF0000, 0x00000000, 0x00000000, 0x00000000) },
  1971. { SRM_TX_DC_CFG_REG_KER,
  1972. EFX_OWORD32(0x001FFFFF, 0x00000000, 0x00000000, 0x00000000) },
  1973. { RX_DC_CFG_REG_KER,
  1974. EFX_OWORD32(0x0000000F, 0x00000000, 0x00000000, 0x00000000) },
  1975. { RX_DC_PF_WM_REG_KER,
  1976. EFX_OWORD32(0x000003FF, 0x00000000, 0x00000000, 0x00000000) },
  1977. { DP_CTRL_REG,
  1978. EFX_OWORD32(0x00000FFF, 0x00000000, 0x00000000, 0x00000000) },
  1979. { XM_GLB_CFG_REG,
  1980. EFX_OWORD32(0x00000C68, 0x00000000, 0x00000000, 0x00000000) },
  1981. { XM_TX_CFG_REG,
  1982. EFX_OWORD32(0x00080164, 0x00000000, 0x00000000, 0x00000000) },
  1983. { XM_RX_CFG_REG,
  1984. EFX_OWORD32(0x07100A0C, 0x00000000, 0x00000000, 0x00000000) },
  1985. { XM_RX_PARAM_REG,
  1986. EFX_OWORD32(0x00001FF8, 0x00000000, 0x00000000, 0x00000000) },
  1987. { XM_FC_REG,
  1988. EFX_OWORD32(0xFFFF0001, 0x00000000, 0x00000000, 0x00000000) },
  1989. { XM_ADR_LO_REG,
  1990. EFX_OWORD32(0xFFFFFFFF, 0x00000000, 0x00000000, 0x00000000) },
  1991. { XX_SD_CTL_REG,
  1992. EFX_OWORD32(0x0003FF0F, 0x00000000, 0x00000000, 0x00000000) },
  1993. };
  1994. static bool efx_masked_compare_oword(const efx_oword_t *a, const efx_oword_t *b,
  1995. const efx_oword_t *mask)
  1996. {
  1997. return ((a->u64[0] ^ b->u64[0]) & mask->u64[0]) ||
  1998. ((a->u64[1] ^ b->u64[1]) & mask->u64[1]);
  1999. }
  2000. int falcon_test_registers(struct efx_nic *efx)
  2001. {
  2002. unsigned address = 0, i, j;
  2003. efx_oword_t mask, imask, original, reg, buf;
  2004. /* Falcon should be in loopback to isolate the XMAC from the PHY */
  2005. WARN_ON(!LOOPBACK_INTERNAL(efx));
  2006. for (i = 0; i < ARRAY_SIZE(efx_test_registers); ++i) {
  2007. address = efx_test_registers[i].address;
  2008. mask = imask = efx_test_registers[i].mask;
  2009. EFX_INVERT_OWORD(imask);
  2010. falcon_read(efx, &original, address);
  2011. /* bit sweep on and off */
  2012. for (j = 0; j < 128; j++) {
  2013. if (!EFX_EXTRACT_OWORD32(mask, j, j))
  2014. continue;
  2015. /* Test this testable bit can be set in isolation */
  2016. EFX_AND_OWORD(reg, original, mask);
  2017. EFX_SET_OWORD32(reg, j, j, 1);
  2018. falcon_write(efx, &reg, address);
  2019. falcon_read(efx, &buf, address);
  2020. if (efx_masked_compare_oword(&reg, &buf, &mask))
  2021. goto fail;
  2022. /* Test this testable bit can be cleared in isolation */
  2023. EFX_OR_OWORD(reg, original, mask);
  2024. EFX_SET_OWORD32(reg, j, j, 0);
  2025. falcon_write(efx, &reg, address);
  2026. falcon_read(efx, &buf, address);
  2027. if (efx_masked_compare_oword(&reg, &buf, &mask))
  2028. goto fail;
  2029. }
  2030. falcon_write(efx, &original, address);
  2031. }
  2032. return 0;
  2033. fail:
  2034. EFX_ERR(efx, "wrote "EFX_OWORD_FMT" read "EFX_OWORD_FMT
  2035. " at address 0x%x mask "EFX_OWORD_FMT"\n", EFX_OWORD_VAL(reg),
  2036. EFX_OWORD_VAL(buf), address, EFX_OWORD_VAL(mask));
  2037. return -EIO;
  2038. }
  2039. /**************************************************************************
  2040. *
  2041. * Device reset
  2042. *
  2043. **************************************************************************
  2044. */
  2045. /* Resets NIC to known state. This routine must be called in process
  2046. * context and is allowed to sleep. */
  2047. int falcon_reset_hw(struct efx_nic *efx, enum reset_type method)
  2048. {
  2049. struct falcon_nic_data *nic_data = efx->nic_data;
  2050. efx_oword_t glb_ctl_reg_ker;
  2051. int rc;
  2052. EFX_LOG(efx, "performing hardware reset (%d)\n", method);
  2053. /* Initiate device reset */
  2054. if (method == RESET_TYPE_WORLD) {
  2055. rc = pci_save_state(efx->pci_dev);
  2056. if (rc) {
  2057. EFX_ERR(efx, "failed to backup PCI state of primary "
  2058. "function prior to hardware reset\n");
  2059. goto fail1;
  2060. }
  2061. if (FALCON_IS_DUAL_FUNC(efx)) {
  2062. rc = pci_save_state(nic_data->pci_dev2);
  2063. if (rc) {
  2064. EFX_ERR(efx, "failed to backup PCI state of "
  2065. "secondary function prior to "
  2066. "hardware reset\n");
  2067. goto fail2;
  2068. }
  2069. }
  2070. EFX_POPULATE_OWORD_2(glb_ctl_reg_ker,
  2071. EXT_PHY_RST_DUR, 0x7,
  2072. SWRST, 1);
  2073. } else {
  2074. int reset_phy = (method == RESET_TYPE_INVISIBLE ?
  2075. EXCLUDE_FROM_RESET : 0);
  2076. EFX_POPULATE_OWORD_7(glb_ctl_reg_ker,
  2077. EXT_PHY_RST_CTL, reset_phy,
  2078. PCIE_CORE_RST_CTL, EXCLUDE_FROM_RESET,
  2079. PCIE_NSTCK_RST_CTL, EXCLUDE_FROM_RESET,
  2080. PCIE_SD_RST_CTL, EXCLUDE_FROM_RESET,
  2081. EE_RST_CTL, EXCLUDE_FROM_RESET,
  2082. EXT_PHY_RST_DUR, 0x7 /* 10ms */,
  2083. SWRST, 1);
  2084. }
  2085. falcon_write(efx, &glb_ctl_reg_ker, GLB_CTL_REG_KER);
  2086. EFX_LOG(efx, "waiting for hardware reset\n");
  2087. schedule_timeout_uninterruptible(HZ / 20);
  2088. /* Restore PCI configuration if needed */
  2089. if (method == RESET_TYPE_WORLD) {
  2090. if (FALCON_IS_DUAL_FUNC(efx)) {
  2091. rc = pci_restore_state(nic_data->pci_dev2);
  2092. if (rc) {
  2093. EFX_ERR(efx, "failed to restore PCI config for "
  2094. "the secondary function\n");
  2095. goto fail3;
  2096. }
  2097. }
  2098. rc = pci_restore_state(efx->pci_dev);
  2099. if (rc) {
  2100. EFX_ERR(efx, "failed to restore PCI config for the "
  2101. "primary function\n");
  2102. goto fail4;
  2103. }
  2104. EFX_LOG(efx, "successfully restored PCI config\n");
  2105. }
  2106. /* Assert that reset complete */
  2107. falcon_read(efx, &glb_ctl_reg_ker, GLB_CTL_REG_KER);
  2108. if (EFX_OWORD_FIELD(glb_ctl_reg_ker, SWRST) != 0) {
  2109. rc = -ETIMEDOUT;
  2110. EFX_ERR(efx, "timed out waiting for hardware reset\n");
  2111. goto fail5;
  2112. }
  2113. EFX_LOG(efx, "hardware reset complete\n");
  2114. return 0;
  2115. /* pci_save_state() and pci_restore_state() MUST be called in pairs */
  2116. fail2:
  2117. fail3:
  2118. pci_restore_state(efx->pci_dev);
  2119. fail1:
  2120. fail4:
  2121. fail5:
  2122. return rc;
  2123. }
  2124. /* Zeroes out the SRAM contents. This routine must be called in
  2125. * process context and is allowed to sleep.
  2126. */
  2127. static int falcon_reset_sram(struct efx_nic *efx)
  2128. {
  2129. efx_oword_t srm_cfg_reg_ker, gpio_cfg_reg_ker;
  2130. int count;
  2131. /* Set the SRAM wake/sleep GPIO appropriately. */
  2132. falcon_read(efx, &gpio_cfg_reg_ker, GPIO_CTL_REG_KER);
  2133. EFX_SET_OWORD_FIELD(gpio_cfg_reg_ker, GPIO1_OEN, 1);
  2134. EFX_SET_OWORD_FIELD(gpio_cfg_reg_ker, GPIO1_OUT, 1);
  2135. falcon_write(efx, &gpio_cfg_reg_ker, GPIO_CTL_REG_KER);
  2136. /* Initiate SRAM reset */
  2137. EFX_POPULATE_OWORD_2(srm_cfg_reg_ker,
  2138. SRAM_OOB_BT_INIT_EN, 1,
  2139. SRM_NUM_BANKS_AND_BANK_SIZE, 0);
  2140. falcon_write(efx, &srm_cfg_reg_ker, SRM_CFG_REG_KER);
  2141. /* Wait for SRAM reset to complete */
  2142. count = 0;
  2143. do {
  2144. EFX_LOG(efx, "waiting for SRAM reset (attempt %d)...\n", count);
  2145. /* SRAM reset is slow; expect around 16ms */
  2146. schedule_timeout_uninterruptible(HZ / 50);
  2147. /* Check for reset complete */
  2148. falcon_read(efx, &srm_cfg_reg_ker, SRM_CFG_REG_KER);
  2149. if (!EFX_OWORD_FIELD(srm_cfg_reg_ker, SRAM_OOB_BT_INIT_EN)) {
  2150. EFX_LOG(efx, "SRAM reset complete\n");
  2151. return 0;
  2152. }
  2153. } while (++count < 20); /* wait upto 0.4 sec */
  2154. EFX_ERR(efx, "timed out waiting for SRAM reset\n");
  2155. return -ETIMEDOUT;
  2156. }
  2157. static int falcon_spi_device_init(struct efx_nic *efx,
  2158. struct efx_spi_device **spi_device_ret,
  2159. unsigned int device_id, u32 device_type)
  2160. {
  2161. struct efx_spi_device *spi_device;
  2162. if (device_type != 0) {
  2163. spi_device = kmalloc(sizeof(*spi_device), GFP_KERNEL);
  2164. if (!spi_device)
  2165. return -ENOMEM;
  2166. spi_device->device_id = device_id;
  2167. spi_device->size =
  2168. 1 << SPI_DEV_TYPE_FIELD(device_type, SPI_DEV_TYPE_SIZE);
  2169. spi_device->addr_len =
  2170. SPI_DEV_TYPE_FIELD(device_type, SPI_DEV_TYPE_ADDR_LEN);
  2171. spi_device->munge_address = (spi_device->size == 1 << 9 &&
  2172. spi_device->addr_len == 1);
  2173. spi_device->block_size =
  2174. 1 << SPI_DEV_TYPE_FIELD(device_type,
  2175. SPI_DEV_TYPE_BLOCK_SIZE);
  2176. spi_device->efx = efx;
  2177. } else {
  2178. spi_device = NULL;
  2179. }
  2180. kfree(*spi_device_ret);
  2181. *spi_device_ret = spi_device;
  2182. return 0;
  2183. }
  2184. static void falcon_remove_spi_devices(struct efx_nic *efx)
  2185. {
  2186. kfree(efx->spi_eeprom);
  2187. efx->spi_eeprom = NULL;
  2188. kfree(efx->spi_flash);
  2189. efx->spi_flash = NULL;
  2190. }
  2191. /* Extract non-volatile configuration */
  2192. static int falcon_probe_nvconfig(struct efx_nic *efx)
  2193. {
  2194. struct falcon_nvconfig *nvconfig;
  2195. int board_rev;
  2196. int rc;
  2197. nvconfig = kmalloc(sizeof(*nvconfig), GFP_KERNEL);
  2198. if (!nvconfig)
  2199. return -ENOMEM;
  2200. rc = falcon_read_nvram(efx, nvconfig);
  2201. if (rc == -EINVAL) {
  2202. EFX_ERR(efx, "NVRAM is invalid therefore using defaults\n");
  2203. efx->phy_type = PHY_TYPE_NONE;
  2204. efx->mii.phy_id = PHY_ADDR_INVALID;
  2205. board_rev = 0;
  2206. rc = 0;
  2207. } else if (rc) {
  2208. goto fail1;
  2209. } else {
  2210. struct falcon_nvconfig_board_v2 *v2 = &nvconfig->board_v2;
  2211. struct falcon_nvconfig_board_v3 *v3 = &nvconfig->board_v3;
  2212. efx->phy_type = v2->port0_phy_type;
  2213. efx->mii.phy_id = v2->port0_phy_addr;
  2214. board_rev = le16_to_cpu(v2->board_revision);
  2215. if (le16_to_cpu(nvconfig->board_struct_ver) >= 3) {
  2216. __le32 fl = v3->spi_device_type[EE_SPI_FLASH];
  2217. __le32 ee = v3->spi_device_type[EE_SPI_EEPROM];
  2218. rc = falcon_spi_device_init(efx, &efx->spi_flash,
  2219. EE_SPI_FLASH,
  2220. le32_to_cpu(fl));
  2221. if (rc)
  2222. goto fail2;
  2223. rc = falcon_spi_device_init(efx, &efx->spi_eeprom,
  2224. EE_SPI_EEPROM,
  2225. le32_to_cpu(ee));
  2226. if (rc)
  2227. goto fail2;
  2228. }
  2229. }
  2230. /* Read the MAC addresses */
  2231. memcpy(efx->mac_address, nvconfig->mac_address[0], ETH_ALEN);
  2232. EFX_LOG(efx, "PHY is %d phy_id %d\n", efx->phy_type, efx->mii.phy_id);
  2233. efx_set_board_info(efx, board_rev);
  2234. kfree(nvconfig);
  2235. return 0;
  2236. fail2:
  2237. falcon_remove_spi_devices(efx);
  2238. fail1:
  2239. kfree(nvconfig);
  2240. return rc;
  2241. }
  2242. /* Probe the NIC variant (revision, ASIC vs FPGA, function count, port
  2243. * count, port speed). Set workaround and feature flags accordingly.
  2244. */
  2245. static int falcon_probe_nic_variant(struct efx_nic *efx)
  2246. {
  2247. efx_oword_t altera_build;
  2248. falcon_read(efx, &altera_build, ALTERA_BUILD_REG_KER);
  2249. if (EFX_OWORD_FIELD(altera_build, VER_ALL)) {
  2250. EFX_ERR(efx, "Falcon FPGA not supported\n");
  2251. return -ENODEV;
  2252. }
  2253. switch (falcon_rev(efx)) {
  2254. case FALCON_REV_A0:
  2255. case 0xff:
  2256. EFX_ERR(efx, "Falcon rev A0 not supported\n");
  2257. return -ENODEV;
  2258. case FALCON_REV_A1:{
  2259. efx_oword_t nic_stat;
  2260. falcon_read(efx, &nic_stat, NIC_STAT_REG);
  2261. if (EFX_OWORD_FIELD(nic_stat, STRAP_PCIE) == 0) {
  2262. EFX_ERR(efx, "Falcon rev A1 PCI-X not supported\n");
  2263. return -ENODEV;
  2264. }
  2265. if (!EFX_OWORD_FIELD(nic_stat, STRAP_10G)) {
  2266. EFX_ERR(efx, "1G mode not supported\n");
  2267. return -ENODEV;
  2268. }
  2269. break;
  2270. }
  2271. case FALCON_REV_B0:
  2272. break;
  2273. default:
  2274. EFX_ERR(efx, "Unknown Falcon rev %d\n", falcon_rev(efx));
  2275. return -ENODEV;
  2276. }
  2277. return 0;
  2278. }
  2279. /* Probe all SPI devices on the NIC */
  2280. static void falcon_probe_spi_devices(struct efx_nic *efx)
  2281. {
  2282. efx_oword_t nic_stat, gpio_ctl, ee_vpd_cfg;
  2283. bool has_flash, has_eeprom, boot_is_external;
  2284. falcon_read(efx, &gpio_ctl, GPIO_CTL_REG_KER);
  2285. falcon_read(efx, &nic_stat, NIC_STAT_REG);
  2286. falcon_read(efx, &ee_vpd_cfg, EE_VPD_CFG_REG_KER);
  2287. has_flash = EFX_OWORD_FIELD(nic_stat, SF_PRST);
  2288. has_eeprom = EFX_OWORD_FIELD(nic_stat, EE_PRST);
  2289. boot_is_external = EFX_OWORD_FIELD(gpio_ctl, BOOTED_USING_NVDEVICE);
  2290. if (has_flash) {
  2291. /* Default flash SPI device: Atmel AT25F1024
  2292. * 128 KB, 24-bit address, 32 KB erase block,
  2293. * 256 B write block
  2294. */
  2295. u32 flash_device_type =
  2296. (17 << SPI_DEV_TYPE_SIZE_LBN)
  2297. | (3 << SPI_DEV_TYPE_ADDR_LEN_LBN)
  2298. | (0x52 << SPI_DEV_TYPE_ERASE_CMD_LBN)
  2299. | (15 << SPI_DEV_TYPE_ERASE_SIZE_LBN)
  2300. | (8 << SPI_DEV_TYPE_BLOCK_SIZE_LBN);
  2301. falcon_spi_device_init(efx, &efx->spi_flash,
  2302. EE_SPI_FLASH, flash_device_type);
  2303. if (!boot_is_external) {
  2304. /* Disable VPD and set clock dividers to safe
  2305. * values for initial programming.
  2306. */
  2307. EFX_LOG(efx, "Booted from internal ASIC settings;"
  2308. " setting SPI config\n");
  2309. EFX_POPULATE_OWORD_3(ee_vpd_cfg, EE_VPD_EN, 0,
  2310. /* 125 MHz / 7 ~= 20 MHz */
  2311. EE_SF_CLOCK_DIV, 7,
  2312. /* 125 MHz / 63 ~= 2 MHz */
  2313. EE_EE_CLOCK_DIV, 63);
  2314. falcon_write(efx, &ee_vpd_cfg, EE_VPD_CFG_REG_KER);
  2315. }
  2316. }
  2317. if (has_eeprom) {
  2318. u32 eeprom_device_type;
  2319. /* If it has no flash, it must have a large EEPROM
  2320. * for chip config; otherwise check whether 9-bit
  2321. * addressing is used for VPD configuration
  2322. */
  2323. if (has_flash &&
  2324. (!boot_is_external ||
  2325. EFX_OWORD_FIELD(ee_vpd_cfg, EE_VPD_EN_AD9_MODE))) {
  2326. /* Default SPI device: Atmel AT25040 or similar
  2327. * 512 B, 9-bit address, 8 B write block
  2328. */
  2329. eeprom_device_type =
  2330. (9 << SPI_DEV_TYPE_SIZE_LBN)
  2331. | (1 << SPI_DEV_TYPE_ADDR_LEN_LBN)
  2332. | (3 << SPI_DEV_TYPE_BLOCK_SIZE_LBN);
  2333. } else {
  2334. /* "Large" SPI device: Atmel AT25640 or similar
  2335. * 8 KB, 16-bit address, 32 B write block
  2336. */
  2337. eeprom_device_type =
  2338. (13 << SPI_DEV_TYPE_SIZE_LBN)
  2339. | (2 << SPI_DEV_TYPE_ADDR_LEN_LBN)
  2340. | (5 << SPI_DEV_TYPE_BLOCK_SIZE_LBN);
  2341. }
  2342. falcon_spi_device_init(efx, &efx->spi_eeprom,
  2343. EE_SPI_EEPROM, eeprom_device_type);
  2344. }
  2345. EFX_LOG(efx, "flash is %s, EEPROM is %s\n",
  2346. (has_flash ? "present" : "absent"),
  2347. (has_eeprom ? "present" : "absent"));
  2348. }
  2349. int falcon_probe_nic(struct efx_nic *efx)
  2350. {
  2351. struct falcon_nic_data *nic_data;
  2352. int rc;
  2353. /* Allocate storage for hardware specific data */
  2354. nic_data = kzalloc(sizeof(*nic_data), GFP_KERNEL);
  2355. if (!nic_data)
  2356. return -ENOMEM;
  2357. efx->nic_data = nic_data;
  2358. /* Determine number of ports etc. */
  2359. rc = falcon_probe_nic_variant(efx);
  2360. if (rc)
  2361. goto fail1;
  2362. /* Probe secondary function if expected */
  2363. if (FALCON_IS_DUAL_FUNC(efx)) {
  2364. struct pci_dev *dev = pci_dev_get(efx->pci_dev);
  2365. while ((dev = pci_get_device(EFX_VENDID_SFC, FALCON_A_S_DEVID,
  2366. dev))) {
  2367. if (dev->bus == efx->pci_dev->bus &&
  2368. dev->devfn == efx->pci_dev->devfn + 1) {
  2369. nic_data->pci_dev2 = dev;
  2370. break;
  2371. }
  2372. }
  2373. if (!nic_data->pci_dev2) {
  2374. EFX_ERR(efx, "failed to find secondary function\n");
  2375. rc = -ENODEV;
  2376. goto fail2;
  2377. }
  2378. }
  2379. /* Now we can reset the NIC */
  2380. rc = falcon_reset_hw(efx, RESET_TYPE_ALL);
  2381. if (rc) {
  2382. EFX_ERR(efx, "failed to reset NIC\n");
  2383. goto fail3;
  2384. }
  2385. /* Allocate memory for INT_KER */
  2386. rc = falcon_alloc_buffer(efx, &efx->irq_status, sizeof(efx_oword_t));
  2387. if (rc)
  2388. goto fail4;
  2389. BUG_ON(efx->irq_status.dma_addr & 0x0f);
  2390. EFX_LOG(efx, "INT_KER at %llx (virt %p phys %lx)\n",
  2391. (unsigned long long)efx->irq_status.dma_addr,
  2392. efx->irq_status.addr, virt_to_phys(efx->irq_status.addr));
  2393. falcon_probe_spi_devices(efx);
  2394. /* Read in the non-volatile configuration */
  2395. rc = falcon_probe_nvconfig(efx);
  2396. if (rc)
  2397. goto fail5;
  2398. /* Initialise I2C adapter */
  2399. efx->i2c_adap.owner = THIS_MODULE;
  2400. nic_data->i2c_data = falcon_i2c_bit_operations;
  2401. nic_data->i2c_data.data = efx;
  2402. efx->i2c_adap.algo_data = &nic_data->i2c_data;
  2403. efx->i2c_adap.dev.parent = &efx->pci_dev->dev;
  2404. strlcpy(efx->i2c_adap.name, "SFC4000 GPIO", sizeof(efx->i2c_adap.name));
  2405. rc = i2c_bit_add_bus(&efx->i2c_adap);
  2406. if (rc)
  2407. goto fail5;
  2408. return 0;
  2409. fail5:
  2410. falcon_remove_spi_devices(efx);
  2411. falcon_free_buffer(efx, &efx->irq_status);
  2412. fail4:
  2413. fail3:
  2414. if (nic_data->pci_dev2) {
  2415. pci_dev_put(nic_data->pci_dev2);
  2416. nic_data->pci_dev2 = NULL;
  2417. }
  2418. fail2:
  2419. fail1:
  2420. kfree(efx->nic_data);
  2421. return rc;
  2422. }
  2423. /* This call performs hardware-specific global initialisation, such as
  2424. * defining the descriptor cache sizes and number of RSS channels.
  2425. * It does not set up any buffers, descriptor rings or event queues.
  2426. */
  2427. int falcon_init_nic(struct efx_nic *efx)
  2428. {
  2429. efx_oword_t temp;
  2430. unsigned thresh;
  2431. int rc;
  2432. /* Set up the address region register. This is only needed
  2433. * for the B0 FPGA, but since we are just pushing in the
  2434. * reset defaults this may as well be unconditional. */
  2435. EFX_POPULATE_OWORD_4(temp, ADR_REGION0, 0,
  2436. ADR_REGION1, (1 << 16),
  2437. ADR_REGION2, (2 << 16),
  2438. ADR_REGION3, (3 << 16));
  2439. falcon_write(efx, &temp, ADR_REGION_REG_KER);
  2440. /* Use on-chip SRAM */
  2441. falcon_read(efx, &temp, NIC_STAT_REG);
  2442. EFX_SET_OWORD_FIELD(temp, ONCHIP_SRAM, 1);
  2443. falcon_write(efx, &temp, NIC_STAT_REG);
  2444. /* Set buffer table mode */
  2445. EFX_POPULATE_OWORD_1(temp, BUF_TBL_MODE, BUF_TBL_MODE_FULL);
  2446. falcon_write(efx, &temp, BUF_TBL_CFG_REG_KER);
  2447. rc = falcon_reset_sram(efx);
  2448. if (rc)
  2449. return rc;
  2450. /* Set positions of descriptor caches in SRAM. */
  2451. EFX_POPULATE_OWORD_1(temp, SRM_TX_DC_BASE_ADR, TX_DC_BASE / 8);
  2452. falcon_write(efx, &temp, SRM_TX_DC_CFG_REG_KER);
  2453. EFX_POPULATE_OWORD_1(temp, SRM_RX_DC_BASE_ADR, RX_DC_BASE / 8);
  2454. falcon_write(efx, &temp, SRM_RX_DC_CFG_REG_KER);
  2455. /* Set TX descriptor cache size. */
  2456. BUILD_BUG_ON(TX_DC_ENTRIES != (16 << TX_DC_ENTRIES_ORDER));
  2457. EFX_POPULATE_OWORD_1(temp, TX_DC_SIZE, TX_DC_ENTRIES_ORDER);
  2458. falcon_write(efx, &temp, TX_DC_CFG_REG_KER);
  2459. /* Set RX descriptor cache size. Set low watermark to size-8, as
  2460. * this allows most efficient prefetching.
  2461. */
  2462. BUILD_BUG_ON(RX_DC_ENTRIES != (16 << RX_DC_ENTRIES_ORDER));
  2463. EFX_POPULATE_OWORD_1(temp, RX_DC_SIZE, RX_DC_ENTRIES_ORDER);
  2464. falcon_write(efx, &temp, RX_DC_CFG_REG_KER);
  2465. EFX_POPULATE_OWORD_1(temp, RX_DC_PF_LWM, RX_DC_ENTRIES - 8);
  2466. falcon_write(efx, &temp, RX_DC_PF_WM_REG_KER);
  2467. /* Clear the parity enables on the TX data fifos as
  2468. * they produce false parity errors because of timing issues
  2469. */
  2470. if (EFX_WORKAROUND_5129(efx)) {
  2471. falcon_read(efx, &temp, SPARE_REG_KER);
  2472. EFX_SET_OWORD_FIELD(temp, MEM_PERR_EN_TX_DATA, 0);
  2473. falcon_write(efx, &temp, SPARE_REG_KER);
  2474. }
  2475. /* Enable all the genuinely fatal interrupts. (They are still
  2476. * masked by the overall interrupt mask, controlled by
  2477. * falcon_interrupts()).
  2478. *
  2479. * Note: All other fatal interrupts are enabled
  2480. */
  2481. EFX_POPULATE_OWORD_3(temp,
  2482. ILL_ADR_INT_KER_EN, 1,
  2483. RBUF_OWN_INT_KER_EN, 1,
  2484. TBUF_OWN_INT_KER_EN, 1);
  2485. EFX_INVERT_OWORD(temp);
  2486. falcon_write(efx, &temp, FATAL_INTR_REG_KER);
  2487. if (EFX_WORKAROUND_7244(efx)) {
  2488. falcon_read(efx, &temp, RX_FILTER_CTL_REG);
  2489. EFX_SET_OWORD_FIELD(temp, UDP_FULL_SRCH_LIMIT, 8);
  2490. EFX_SET_OWORD_FIELD(temp, UDP_WILD_SRCH_LIMIT, 8);
  2491. EFX_SET_OWORD_FIELD(temp, TCP_FULL_SRCH_LIMIT, 8);
  2492. EFX_SET_OWORD_FIELD(temp, TCP_WILD_SRCH_LIMIT, 8);
  2493. falcon_write(efx, &temp, RX_FILTER_CTL_REG);
  2494. }
  2495. falcon_setup_rss_indir_table(efx);
  2496. /* Setup RX. Wait for descriptor is broken and must
  2497. * be disabled. RXDP recovery shouldn't be needed, but is.
  2498. */
  2499. falcon_read(efx, &temp, RX_SELF_RST_REG_KER);
  2500. EFX_SET_OWORD_FIELD(temp, RX_NODESC_WAIT_DIS, 1);
  2501. EFX_SET_OWORD_FIELD(temp, RX_RECOVERY_EN, 1);
  2502. if (EFX_WORKAROUND_5583(efx))
  2503. EFX_SET_OWORD_FIELD(temp, RX_ISCSI_DIS, 1);
  2504. falcon_write(efx, &temp, RX_SELF_RST_REG_KER);
  2505. /* Disable the ugly timer-based TX DMA backoff and allow TX DMA to be
  2506. * controlled by the RX FIFO fill level. Set arbitration to one pkt/Q.
  2507. */
  2508. falcon_read(efx, &temp, TX_CFG2_REG_KER);
  2509. EFX_SET_OWORD_FIELD(temp, TX_RX_SPACER, 0xfe);
  2510. EFX_SET_OWORD_FIELD(temp, TX_RX_SPACER_EN, 1);
  2511. EFX_SET_OWORD_FIELD(temp, TX_ONE_PKT_PER_Q, 1);
  2512. EFX_SET_OWORD_FIELD(temp, TX_CSR_PUSH_EN, 0);
  2513. EFX_SET_OWORD_FIELD(temp, TX_DIS_NON_IP_EV, 1);
  2514. /* Enable SW_EV to inherit in char driver - assume harmless here */
  2515. EFX_SET_OWORD_FIELD(temp, TX_SW_EV_EN, 1);
  2516. /* Prefetch threshold 2 => fetch when descriptor cache half empty */
  2517. EFX_SET_OWORD_FIELD(temp, TX_PREF_THRESHOLD, 2);
  2518. /* Squash TX of packets of 16 bytes or less */
  2519. if (falcon_rev(efx) >= FALCON_REV_B0 && EFX_WORKAROUND_9141(efx))
  2520. EFX_SET_OWORD_FIELD(temp, TX_FLUSH_MIN_LEN_EN_B0, 1);
  2521. falcon_write(efx, &temp, TX_CFG2_REG_KER);
  2522. /* Do not enable TX_NO_EOP_DISC_EN, since it limits packets to 16
  2523. * descriptors (which is bad).
  2524. */
  2525. falcon_read(efx, &temp, TX_CFG_REG_KER);
  2526. EFX_SET_OWORD_FIELD(temp, TX_NO_EOP_DISC_EN, 0);
  2527. falcon_write(efx, &temp, TX_CFG_REG_KER);
  2528. /* RX config */
  2529. falcon_read(efx, &temp, RX_CFG_REG_KER);
  2530. EFX_SET_OWORD_FIELD_VER(efx, temp, RX_DESC_PUSH_EN, 0);
  2531. if (EFX_WORKAROUND_7575(efx))
  2532. EFX_SET_OWORD_FIELD_VER(efx, temp, RX_USR_BUF_SIZE,
  2533. (3 * 4096) / 32);
  2534. if (falcon_rev(efx) >= FALCON_REV_B0)
  2535. EFX_SET_OWORD_FIELD(temp, RX_INGR_EN_B0, 1);
  2536. /* RX FIFO flow control thresholds */
  2537. thresh = ((rx_xon_thresh_bytes >= 0) ?
  2538. rx_xon_thresh_bytes : efx->type->rx_xon_thresh);
  2539. EFX_SET_OWORD_FIELD_VER(efx, temp, RX_XON_MAC_TH, thresh / 256);
  2540. thresh = ((rx_xoff_thresh_bytes >= 0) ?
  2541. rx_xoff_thresh_bytes : efx->type->rx_xoff_thresh);
  2542. EFX_SET_OWORD_FIELD_VER(efx, temp, RX_XOFF_MAC_TH, thresh / 256);
  2543. /* RX control FIFO thresholds [32 entries] */
  2544. EFX_SET_OWORD_FIELD_VER(efx, temp, RX_XON_TX_TH, 20);
  2545. EFX_SET_OWORD_FIELD_VER(efx, temp, RX_XOFF_TX_TH, 25);
  2546. falcon_write(efx, &temp, RX_CFG_REG_KER);
  2547. /* Set destination of both TX and RX Flush events */
  2548. if (falcon_rev(efx) >= FALCON_REV_B0) {
  2549. EFX_POPULATE_OWORD_1(temp, FLS_EVQ_ID, 0);
  2550. falcon_write(efx, &temp, DP_CTRL_REG);
  2551. }
  2552. return 0;
  2553. }
  2554. void falcon_remove_nic(struct efx_nic *efx)
  2555. {
  2556. struct falcon_nic_data *nic_data = efx->nic_data;
  2557. int rc;
  2558. rc = i2c_del_adapter(&efx->i2c_adap);
  2559. BUG_ON(rc);
  2560. falcon_remove_spi_devices(efx);
  2561. falcon_free_buffer(efx, &efx->irq_status);
  2562. falcon_reset_hw(efx, RESET_TYPE_ALL);
  2563. /* Release the second function after the reset */
  2564. if (nic_data->pci_dev2) {
  2565. pci_dev_put(nic_data->pci_dev2);
  2566. nic_data->pci_dev2 = NULL;
  2567. }
  2568. /* Tear down the private nic state */
  2569. kfree(efx->nic_data);
  2570. efx->nic_data = NULL;
  2571. }
  2572. void falcon_update_nic_stats(struct efx_nic *efx)
  2573. {
  2574. efx_oword_t cnt;
  2575. falcon_read(efx, &cnt, RX_NODESC_DROP_REG_KER);
  2576. efx->n_rx_nodesc_drop_cnt += EFX_OWORD_FIELD(cnt, RX_NODESC_DROP_CNT);
  2577. }
  2578. /**************************************************************************
  2579. *
  2580. * Revision-dependent attributes used by efx.c
  2581. *
  2582. **************************************************************************
  2583. */
  2584. struct efx_nic_type falcon_a_nic_type = {
  2585. .mem_bar = 2,
  2586. .mem_map_size = 0x20000,
  2587. .txd_ptr_tbl_base = TX_DESC_PTR_TBL_KER_A1,
  2588. .rxd_ptr_tbl_base = RX_DESC_PTR_TBL_KER_A1,
  2589. .buf_tbl_base = BUF_TBL_KER_A1,
  2590. .evq_ptr_tbl_base = EVQ_PTR_TBL_KER_A1,
  2591. .evq_rptr_tbl_base = EVQ_RPTR_REG_KER_A1,
  2592. .txd_ring_mask = FALCON_TXD_RING_MASK,
  2593. .rxd_ring_mask = FALCON_RXD_RING_MASK,
  2594. .evq_size = FALCON_EVQ_SIZE,
  2595. .max_dma_mask = FALCON_DMA_MASK,
  2596. .tx_dma_mask = FALCON_TX_DMA_MASK,
  2597. .bug5391_mask = 0xf,
  2598. .rx_xoff_thresh = 2048,
  2599. .rx_xon_thresh = 512,
  2600. .rx_buffer_padding = 0x24,
  2601. .max_interrupt_mode = EFX_INT_MODE_MSI,
  2602. .phys_addr_channels = 4,
  2603. };
  2604. struct efx_nic_type falcon_b_nic_type = {
  2605. .mem_bar = 2,
  2606. /* Map everything up to and including the RSS indirection
  2607. * table. Don't map MSI-X table, MSI-X PBA since Linux
  2608. * requires that they not be mapped. */
  2609. .mem_map_size = RX_RSS_INDIR_TBL_B0 + 0x800,
  2610. .txd_ptr_tbl_base = TX_DESC_PTR_TBL_KER_B0,
  2611. .rxd_ptr_tbl_base = RX_DESC_PTR_TBL_KER_B0,
  2612. .buf_tbl_base = BUF_TBL_KER_B0,
  2613. .evq_ptr_tbl_base = EVQ_PTR_TBL_KER_B0,
  2614. .evq_rptr_tbl_base = EVQ_RPTR_REG_KER_B0,
  2615. .txd_ring_mask = FALCON_TXD_RING_MASK,
  2616. .rxd_ring_mask = FALCON_RXD_RING_MASK,
  2617. .evq_size = FALCON_EVQ_SIZE,
  2618. .max_dma_mask = FALCON_DMA_MASK,
  2619. .tx_dma_mask = FALCON_TX_DMA_MASK,
  2620. .bug5391_mask = 0,
  2621. .rx_xoff_thresh = 54272, /* ~80Kb - 3*max MTU */
  2622. .rx_xon_thresh = 27648, /* ~3*max MTU */
  2623. .rx_buffer_padding = 0,
  2624. .max_interrupt_mode = EFX_INT_MODE_MSIX,
  2625. .phys_addr_channels = 32, /* Hardware limit is 64, but the legacy
  2626. * interrupt handler only supports 32
  2627. * channels */
  2628. };