falcon.c 88 KB

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