falcon.c 94 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356
  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/mii.h>
  17. #include "net_driver.h"
  18. #include "bitfield.h"
  19. #include "efx.h"
  20. #include "mac.h"
  21. #include "spi.h"
  22. #include "falcon.h"
  23. #include "regs.h"
  24. #include "io.h"
  25. #include "mdio_10g.h"
  26. #include "phy.h"
  27. #include "workarounds.h"
  28. /* Hardware control for SFC4000 (aka Falcon). */
  29. /**************************************************************************
  30. *
  31. * Configurable values
  32. *
  33. **************************************************************************
  34. */
  35. /* This is set to 16 for a good reason. In summary, if larger than
  36. * 16, the descriptor cache holds more than a default socket
  37. * buffer's worth of packets (for UDP we can only have at most one
  38. * socket buffer's worth outstanding). This combined with the fact
  39. * that we only get 1 TX event per descriptor cache means the NIC
  40. * goes idle.
  41. */
  42. #define TX_DC_ENTRIES 16
  43. #define TX_DC_ENTRIES_ORDER 1
  44. #define RX_DC_ENTRIES 64
  45. #define RX_DC_ENTRIES_ORDER 3
  46. static const unsigned int
  47. /* "Large" EEPROM device: Atmel AT25640 or similar
  48. * 8 KB, 16-bit address, 32 B write block */
  49. large_eeprom_type = ((13 << SPI_DEV_TYPE_SIZE_LBN)
  50. | (2 << SPI_DEV_TYPE_ADDR_LEN_LBN)
  51. | (5 << SPI_DEV_TYPE_BLOCK_SIZE_LBN)),
  52. /* Default flash device: Atmel AT25F1024
  53. * 128 KB, 24-bit address, 32 KB erase block, 256 B write block */
  54. default_flash_type = ((17 << SPI_DEV_TYPE_SIZE_LBN)
  55. | (3 << SPI_DEV_TYPE_ADDR_LEN_LBN)
  56. | (0x52 << SPI_DEV_TYPE_ERASE_CMD_LBN)
  57. | (15 << SPI_DEV_TYPE_ERASE_SIZE_LBN)
  58. | (8 << SPI_DEV_TYPE_BLOCK_SIZE_LBN));
  59. /* RX FIFO XOFF watermark
  60. *
  61. * When the amount of the RX FIFO increases used increases past this
  62. * watermark send XOFF. Only used if RX flow control is enabled (ethtool -A)
  63. * This also has an effect on RX/TX arbitration
  64. */
  65. int efx_nic_rx_xoff_thresh = -1;
  66. module_param_named(rx_xoff_thresh_bytes, efx_nic_rx_xoff_thresh, int, 0644);
  67. MODULE_PARM_DESC(rx_xoff_thresh_bytes, "RX fifo XOFF threshold");
  68. /* RX FIFO XON watermark
  69. *
  70. * When the amount of the RX FIFO used decreases below this
  71. * watermark send XON. Only used if TX flow control is enabled (ethtool -A)
  72. * This also has an effect on RX/TX arbitration
  73. */
  74. int efx_nic_rx_xon_thresh = -1;
  75. module_param_named(rx_xon_thresh_bytes, efx_nic_rx_xon_thresh, int, 0644);
  76. MODULE_PARM_DESC(rx_xon_thresh_bytes, "RX fifo XON threshold");
  77. /* If EFX_MAX_INT_ERRORS internal errors occur within
  78. * EFX_INT_ERROR_EXPIRE seconds, we consider the NIC broken and
  79. * disable it.
  80. */
  81. #define EFX_INT_ERROR_EXPIRE 3600
  82. #define EFX_MAX_INT_ERRORS 5
  83. /* We poll for events every FLUSH_INTERVAL ms, and check FLUSH_POLL_COUNT times
  84. */
  85. #define EFX_FLUSH_INTERVAL 10
  86. #define EFX_FLUSH_POLL_COUNT 100
  87. /**************************************************************************
  88. *
  89. * Falcon constants
  90. *
  91. **************************************************************************
  92. */
  93. /* Size and alignment of special buffers (4KB) */
  94. #define EFX_BUF_SIZE 4096
  95. /* Depth of RX flush request fifo */
  96. #define EFX_RX_FLUSH_COUNT 4
  97. /**************************************************************************
  98. *
  99. * Solarstorm hardware access
  100. *
  101. **************************************************************************/
  102. static inline void efx_write_buf_tbl(struct efx_nic *efx, efx_qword_t *value,
  103. unsigned int index)
  104. {
  105. efx_sram_writeq(efx, efx->membase + efx->type->buf_tbl_base,
  106. value, index);
  107. }
  108. /* Read the current event from the event queue */
  109. static inline efx_qword_t *efx_event(struct efx_channel *channel,
  110. unsigned int index)
  111. {
  112. return (((efx_qword_t *) (channel->eventq.addr)) + index);
  113. }
  114. /* See if an event is present
  115. *
  116. * We check both the high and low dword of the event for all ones. We
  117. * wrote all ones when we cleared the event, and no valid event can
  118. * have all ones in either its high or low dwords. This approach is
  119. * robust against reordering.
  120. *
  121. * Note that using a single 64-bit comparison is incorrect; even
  122. * though the CPU read will be atomic, the DMA write may not be.
  123. */
  124. static inline int efx_event_present(efx_qword_t *event)
  125. {
  126. return (!(EFX_DWORD_IS_ALL_ONES(event->dword[0]) |
  127. EFX_DWORD_IS_ALL_ONES(event->dword[1])));
  128. }
  129. /**************************************************************************
  130. *
  131. * I2C bus - this is a bit-bashing interface using GPIO pins
  132. * Note that it uses the output enables to tristate the outputs
  133. * SDA is the data pin and SCL is the clock
  134. *
  135. **************************************************************************
  136. */
  137. static void falcon_setsda(void *data, int state)
  138. {
  139. struct efx_nic *efx = (struct efx_nic *)data;
  140. efx_oword_t reg;
  141. efx_reado(efx, &reg, FR_AB_GPIO_CTL);
  142. EFX_SET_OWORD_FIELD(reg, FRF_AB_GPIO3_OEN, !state);
  143. efx_writeo(efx, &reg, FR_AB_GPIO_CTL);
  144. }
  145. static void falcon_setscl(void *data, int state)
  146. {
  147. struct efx_nic *efx = (struct efx_nic *)data;
  148. efx_oword_t reg;
  149. efx_reado(efx, &reg, FR_AB_GPIO_CTL);
  150. EFX_SET_OWORD_FIELD(reg, FRF_AB_GPIO0_OEN, !state);
  151. efx_writeo(efx, &reg, FR_AB_GPIO_CTL);
  152. }
  153. static int falcon_getsda(void *data)
  154. {
  155. struct efx_nic *efx = (struct efx_nic *)data;
  156. efx_oword_t reg;
  157. efx_reado(efx, &reg, FR_AB_GPIO_CTL);
  158. return EFX_OWORD_FIELD(reg, FRF_AB_GPIO3_IN);
  159. }
  160. static int falcon_getscl(void *data)
  161. {
  162. struct efx_nic *efx = (struct efx_nic *)data;
  163. efx_oword_t reg;
  164. efx_reado(efx, &reg, FR_AB_GPIO_CTL);
  165. return EFX_OWORD_FIELD(reg, FRF_AB_GPIO0_IN);
  166. }
  167. static struct i2c_algo_bit_data falcon_i2c_bit_operations = {
  168. .setsda = falcon_setsda,
  169. .setscl = falcon_setscl,
  170. .getsda = falcon_getsda,
  171. .getscl = falcon_getscl,
  172. .udelay = 5,
  173. /* Wait up to 50 ms for slave to let us pull SCL high */
  174. .timeout = DIV_ROUND_UP(HZ, 20),
  175. };
  176. /**************************************************************************
  177. *
  178. * Special buffer handling
  179. * Special buffers are used for event queues and the TX and RX
  180. * descriptor rings.
  181. *
  182. *************************************************************************/
  183. /*
  184. * Initialise a special buffer
  185. *
  186. * This will define a buffer (previously allocated via
  187. * efx_alloc_special_buffer()) in the buffer table, allowing
  188. * it to be used for event queues, descriptor rings etc.
  189. */
  190. static void
  191. efx_init_special_buffer(struct efx_nic *efx, struct efx_special_buffer *buffer)
  192. {
  193. efx_qword_t buf_desc;
  194. int index;
  195. dma_addr_t dma_addr;
  196. int i;
  197. EFX_BUG_ON_PARANOID(!buffer->addr);
  198. /* Write buffer descriptors to NIC */
  199. for (i = 0; i < buffer->entries; i++) {
  200. index = buffer->index + i;
  201. dma_addr = buffer->dma_addr + (i * 4096);
  202. EFX_LOG(efx, "mapping special buffer %d at %llx\n",
  203. index, (unsigned long long)dma_addr);
  204. EFX_POPULATE_QWORD_3(buf_desc,
  205. FRF_AZ_BUF_ADR_REGION, 0,
  206. FRF_AZ_BUF_ADR_FBUF, dma_addr >> 12,
  207. FRF_AZ_BUF_OWNER_ID_FBUF, 0);
  208. efx_write_buf_tbl(efx, &buf_desc, index);
  209. }
  210. }
  211. /* Unmaps a buffer and clears the buffer table entries */
  212. static void
  213. efx_fini_special_buffer(struct efx_nic *efx, struct efx_special_buffer *buffer)
  214. {
  215. efx_oword_t buf_tbl_upd;
  216. unsigned int start = buffer->index;
  217. unsigned int end = (buffer->index + buffer->entries - 1);
  218. if (!buffer->entries)
  219. return;
  220. EFX_LOG(efx, "unmapping special buffers %d-%d\n",
  221. buffer->index, buffer->index + buffer->entries - 1);
  222. EFX_POPULATE_OWORD_4(buf_tbl_upd,
  223. FRF_AZ_BUF_UPD_CMD, 0,
  224. FRF_AZ_BUF_CLR_CMD, 1,
  225. FRF_AZ_BUF_CLR_END_ID, end,
  226. FRF_AZ_BUF_CLR_START_ID, start);
  227. efx_writeo(efx, &buf_tbl_upd, FR_AZ_BUF_TBL_UPD);
  228. }
  229. /*
  230. * Allocate a new special buffer
  231. *
  232. * This allocates memory for a new buffer, clears it and allocates a
  233. * new buffer ID range. It does not write into the buffer table.
  234. *
  235. * This call will allocate 4KB buffers, since 8KB buffers can't be
  236. * used for event queues and descriptor rings.
  237. */
  238. static int efx_alloc_special_buffer(struct efx_nic *efx,
  239. struct efx_special_buffer *buffer,
  240. unsigned int len)
  241. {
  242. len = ALIGN(len, EFX_BUF_SIZE);
  243. buffer->addr = pci_alloc_consistent(efx->pci_dev, len,
  244. &buffer->dma_addr);
  245. if (!buffer->addr)
  246. return -ENOMEM;
  247. buffer->len = len;
  248. buffer->entries = len / EFX_BUF_SIZE;
  249. BUG_ON(buffer->dma_addr & (EFX_BUF_SIZE - 1));
  250. /* All zeros is a potentially valid event so memset to 0xff */
  251. memset(buffer->addr, 0xff, len);
  252. /* Select new buffer ID */
  253. buffer->index = efx->next_buffer_table;
  254. efx->next_buffer_table += buffer->entries;
  255. EFX_LOG(efx, "allocating special buffers %d-%d at %llx+%x "
  256. "(virt %p phys %llx)\n", buffer->index,
  257. buffer->index + buffer->entries - 1,
  258. (u64)buffer->dma_addr, len,
  259. buffer->addr, (u64)virt_to_phys(buffer->addr));
  260. return 0;
  261. }
  262. static void
  263. efx_free_special_buffer(struct efx_nic *efx, struct efx_special_buffer *buffer)
  264. {
  265. if (!buffer->addr)
  266. return;
  267. EFX_LOG(efx, "deallocating special buffers %d-%d at %llx+%x "
  268. "(virt %p phys %llx)\n", buffer->index,
  269. buffer->index + buffer->entries - 1,
  270. (u64)buffer->dma_addr, buffer->len,
  271. buffer->addr, (u64)virt_to_phys(buffer->addr));
  272. pci_free_consistent(efx->pci_dev, buffer->len, buffer->addr,
  273. buffer->dma_addr);
  274. buffer->addr = NULL;
  275. buffer->entries = 0;
  276. }
  277. /**************************************************************************
  278. *
  279. * Generic buffer handling
  280. * These buffers are used for interrupt status and MAC stats
  281. *
  282. **************************************************************************/
  283. int efx_nic_alloc_buffer(struct efx_nic *efx, struct efx_buffer *buffer,
  284. unsigned int len)
  285. {
  286. buffer->addr = pci_alloc_consistent(efx->pci_dev, len,
  287. &buffer->dma_addr);
  288. if (!buffer->addr)
  289. return -ENOMEM;
  290. buffer->len = len;
  291. memset(buffer->addr, 0, len);
  292. return 0;
  293. }
  294. void efx_nic_free_buffer(struct efx_nic *efx, struct efx_buffer *buffer)
  295. {
  296. if (buffer->addr) {
  297. pci_free_consistent(efx->pci_dev, buffer->len,
  298. buffer->addr, buffer->dma_addr);
  299. buffer->addr = NULL;
  300. }
  301. }
  302. /**************************************************************************
  303. *
  304. * TX path
  305. *
  306. **************************************************************************/
  307. /* Returns a pointer to the specified transmit descriptor in the TX
  308. * descriptor queue belonging to the specified channel.
  309. */
  310. static inline efx_qword_t *
  311. efx_tx_desc(struct efx_tx_queue *tx_queue, unsigned int index)
  312. {
  313. return (((efx_qword_t *) (tx_queue->txd.addr)) + index);
  314. }
  315. /* This writes to the TX_DESC_WPTR; write pointer for TX descriptor ring */
  316. static inline void efx_notify_tx_desc(struct efx_tx_queue *tx_queue)
  317. {
  318. unsigned write_ptr;
  319. efx_dword_t reg;
  320. write_ptr = tx_queue->write_count & EFX_TXQ_MASK;
  321. EFX_POPULATE_DWORD_1(reg, FRF_AZ_TX_DESC_WPTR_DWORD, write_ptr);
  322. efx_writed_page(tx_queue->efx, &reg,
  323. FR_AZ_TX_DESC_UPD_DWORD_P0, tx_queue->queue);
  324. }
  325. /* For each entry inserted into the software descriptor ring, create a
  326. * descriptor in the hardware TX descriptor ring (in host memory), and
  327. * write a doorbell.
  328. */
  329. void efx_nic_push_buffers(struct efx_tx_queue *tx_queue)
  330. {
  331. struct efx_tx_buffer *buffer;
  332. efx_qword_t *txd;
  333. unsigned write_ptr;
  334. BUG_ON(tx_queue->write_count == tx_queue->insert_count);
  335. do {
  336. write_ptr = tx_queue->write_count & EFX_TXQ_MASK;
  337. buffer = &tx_queue->buffer[write_ptr];
  338. txd = efx_tx_desc(tx_queue, write_ptr);
  339. ++tx_queue->write_count;
  340. /* Create TX descriptor ring entry */
  341. EFX_POPULATE_QWORD_4(*txd,
  342. FSF_AZ_TX_KER_CONT, buffer->continuation,
  343. FSF_AZ_TX_KER_BYTE_COUNT, buffer->len,
  344. FSF_AZ_TX_KER_BUF_REGION, 0,
  345. FSF_AZ_TX_KER_BUF_ADDR, buffer->dma_addr);
  346. } while (tx_queue->write_count != tx_queue->insert_count);
  347. wmb(); /* Ensure descriptors are written before they are fetched */
  348. efx_notify_tx_desc(tx_queue);
  349. }
  350. /* Allocate hardware resources for a TX queue */
  351. int efx_nic_probe_tx(struct efx_tx_queue *tx_queue)
  352. {
  353. struct efx_nic *efx = tx_queue->efx;
  354. BUILD_BUG_ON(EFX_TXQ_SIZE < 512 || EFX_TXQ_SIZE > 4096 ||
  355. EFX_TXQ_SIZE & EFX_TXQ_MASK);
  356. return efx_alloc_special_buffer(efx, &tx_queue->txd,
  357. EFX_TXQ_SIZE * sizeof(efx_qword_t));
  358. }
  359. void efx_nic_init_tx(struct efx_tx_queue *tx_queue)
  360. {
  361. efx_oword_t tx_desc_ptr;
  362. struct efx_nic *efx = tx_queue->efx;
  363. tx_queue->flushed = FLUSH_NONE;
  364. /* Pin TX descriptor ring */
  365. efx_init_special_buffer(efx, &tx_queue->txd);
  366. /* Push TX descriptor ring to card */
  367. EFX_POPULATE_OWORD_10(tx_desc_ptr,
  368. FRF_AZ_TX_DESCQ_EN, 1,
  369. FRF_AZ_TX_ISCSI_DDIG_EN, 0,
  370. FRF_AZ_TX_ISCSI_HDIG_EN, 0,
  371. FRF_AZ_TX_DESCQ_BUF_BASE_ID, tx_queue->txd.index,
  372. FRF_AZ_TX_DESCQ_EVQ_ID,
  373. tx_queue->channel->channel,
  374. FRF_AZ_TX_DESCQ_OWNER_ID, 0,
  375. FRF_AZ_TX_DESCQ_LABEL, tx_queue->queue,
  376. FRF_AZ_TX_DESCQ_SIZE,
  377. __ffs(tx_queue->txd.entries),
  378. FRF_AZ_TX_DESCQ_TYPE, 0,
  379. FRF_BZ_TX_NON_IP_DROP_DIS, 1);
  380. if (efx_nic_rev(efx) >= EFX_REV_FALCON_B0) {
  381. int csum = tx_queue->queue == EFX_TX_QUEUE_OFFLOAD_CSUM;
  382. EFX_SET_OWORD_FIELD(tx_desc_ptr, FRF_BZ_TX_IP_CHKSM_DIS, !csum);
  383. EFX_SET_OWORD_FIELD(tx_desc_ptr, FRF_BZ_TX_TCP_CHKSM_DIS,
  384. !csum);
  385. }
  386. efx_writeo_table(efx, &tx_desc_ptr, efx->type->txd_ptr_tbl_base,
  387. tx_queue->queue);
  388. if (efx_nic_rev(efx) < EFX_REV_FALCON_B0) {
  389. efx_oword_t reg;
  390. /* Only 128 bits in this register */
  391. BUILD_BUG_ON(EFX_TX_QUEUE_COUNT >= 128);
  392. efx_reado(efx, &reg, FR_AA_TX_CHKSM_CFG);
  393. if (tx_queue->queue == EFX_TX_QUEUE_OFFLOAD_CSUM)
  394. clear_bit_le(tx_queue->queue, (void *)&reg);
  395. else
  396. set_bit_le(tx_queue->queue, (void *)&reg);
  397. efx_writeo(efx, &reg, FR_AA_TX_CHKSM_CFG);
  398. }
  399. }
  400. static void efx_flush_tx_queue(struct efx_tx_queue *tx_queue)
  401. {
  402. struct efx_nic *efx = tx_queue->efx;
  403. efx_oword_t tx_flush_descq;
  404. tx_queue->flushed = FLUSH_PENDING;
  405. /* Post a flush command */
  406. EFX_POPULATE_OWORD_2(tx_flush_descq,
  407. FRF_AZ_TX_FLUSH_DESCQ_CMD, 1,
  408. FRF_AZ_TX_FLUSH_DESCQ, tx_queue->queue);
  409. efx_writeo(efx, &tx_flush_descq, FR_AZ_TX_FLUSH_DESCQ);
  410. }
  411. void efx_nic_fini_tx(struct efx_tx_queue *tx_queue)
  412. {
  413. struct efx_nic *efx = tx_queue->efx;
  414. efx_oword_t tx_desc_ptr;
  415. /* The queue should have been flushed */
  416. WARN_ON(tx_queue->flushed != FLUSH_DONE);
  417. /* Remove TX descriptor ring from card */
  418. EFX_ZERO_OWORD(tx_desc_ptr);
  419. efx_writeo_table(efx, &tx_desc_ptr, efx->type->txd_ptr_tbl_base,
  420. tx_queue->queue);
  421. /* Unpin TX descriptor ring */
  422. efx_fini_special_buffer(efx, &tx_queue->txd);
  423. }
  424. /* Free buffers backing TX queue */
  425. void efx_nic_remove_tx(struct efx_tx_queue *tx_queue)
  426. {
  427. efx_free_special_buffer(tx_queue->efx, &tx_queue->txd);
  428. }
  429. /**************************************************************************
  430. *
  431. * RX path
  432. *
  433. **************************************************************************/
  434. /* Returns a pointer to the specified descriptor in the RX descriptor queue */
  435. static inline efx_qword_t *
  436. efx_rx_desc(struct efx_rx_queue *rx_queue, unsigned int index)
  437. {
  438. return (((efx_qword_t *) (rx_queue->rxd.addr)) + index);
  439. }
  440. /* This creates an entry in the RX descriptor queue */
  441. static inline void
  442. efx_build_rx_desc(struct efx_rx_queue *rx_queue, unsigned index)
  443. {
  444. struct efx_rx_buffer *rx_buf;
  445. efx_qword_t *rxd;
  446. rxd = efx_rx_desc(rx_queue, index);
  447. rx_buf = efx_rx_buffer(rx_queue, index);
  448. EFX_POPULATE_QWORD_3(*rxd,
  449. FSF_AZ_RX_KER_BUF_SIZE,
  450. rx_buf->len -
  451. rx_queue->efx->type->rx_buffer_padding,
  452. FSF_AZ_RX_KER_BUF_REGION, 0,
  453. FSF_AZ_RX_KER_BUF_ADDR, rx_buf->dma_addr);
  454. }
  455. /* This writes to the RX_DESC_WPTR register for the specified receive
  456. * descriptor ring.
  457. */
  458. void efx_nic_notify_rx_desc(struct efx_rx_queue *rx_queue)
  459. {
  460. efx_dword_t reg;
  461. unsigned write_ptr;
  462. while (rx_queue->notified_count != rx_queue->added_count) {
  463. efx_build_rx_desc(rx_queue,
  464. rx_queue->notified_count &
  465. EFX_RXQ_MASK);
  466. ++rx_queue->notified_count;
  467. }
  468. wmb();
  469. write_ptr = rx_queue->added_count & EFX_RXQ_MASK;
  470. EFX_POPULATE_DWORD_1(reg, FRF_AZ_RX_DESC_WPTR_DWORD, write_ptr);
  471. efx_writed_page(rx_queue->efx, &reg,
  472. FR_AZ_RX_DESC_UPD_DWORD_P0, rx_queue->queue);
  473. }
  474. int efx_nic_probe_rx(struct efx_rx_queue *rx_queue)
  475. {
  476. struct efx_nic *efx = rx_queue->efx;
  477. BUILD_BUG_ON(EFX_RXQ_SIZE < 512 || EFX_RXQ_SIZE > 4096 ||
  478. EFX_RXQ_SIZE & EFX_RXQ_MASK);
  479. return efx_alloc_special_buffer(efx, &rx_queue->rxd,
  480. EFX_RXQ_SIZE * sizeof(efx_qword_t));
  481. }
  482. void efx_nic_init_rx(struct efx_rx_queue *rx_queue)
  483. {
  484. efx_oword_t rx_desc_ptr;
  485. struct efx_nic *efx = rx_queue->efx;
  486. bool is_b0 = efx_nic_rev(efx) >= EFX_REV_FALCON_B0;
  487. bool iscsi_digest_en = is_b0;
  488. EFX_LOG(efx, "RX queue %d ring in special buffers %d-%d\n",
  489. rx_queue->queue, rx_queue->rxd.index,
  490. rx_queue->rxd.index + rx_queue->rxd.entries - 1);
  491. rx_queue->flushed = FLUSH_NONE;
  492. /* Pin RX descriptor ring */
  493. efx_init_special_buffer(efx, &rx_queue->rxd);
  494. /* Push RX descriptor ring to card */
  495. EFX_POPULATE_OWORD_10(rx_desc_ptr,
  496. FRF_AZ_RX_ISCSI_DDIG_EN, iscsi_digest_en,
  497. FRF_AZ_RX_ISCSI_HDIG_EN, iscsi_digest_en,
  498. FRF_AZ_RX_DESCQ_BUF_BASE_ID, rx_queue->rxd.index,
  499. FRF_AZ_RX_DESCQ_EVQ_ID,
  500. rx_queue->channel->channel,
  501. FRF_AZ_RX_DESCQ_OWNER_ID, 0,
  502. FRF_AZ_RX_DESCQ_LABEL, rx_queue->queue,
  503. FRF_AZ_RX_DESCQ_SIZE,
  504. __ffs(rx_queue->rxd.entries),
  505. FRF_AZ_RX_DESCQ_TYPE, 0 /* kernel queue */ ,
  506. /* For >=B0 this is scatter so disable */
  507. FRF_AZ_RX_DESCQ_JUMBO, !is_b0,
  508. FRF_AZ_RX_DESCQ_EN, 1);
  509. efx_writeo_table(efx, &rx_desc_ptr, efx->type->rxd_ptr_tbl_base,
  510. rx_queue->queue);
  511. }
  512. static void efx_flush_rx_queue(struct efx_rx_queue *rx_queue)
  513. {
  514. struct efx_nic *efx = rx_queue->efx;
  515. efx_oword_t rx_flush_descq;
  516. rx_queue->flushed = FLUSH_PENDING;
  517. /* Post a flush command */
  518. EFX_POPULATE_OWORD_2(rx_flush_descq,
  519. FRF_AZ_RX_FLUSH_DESCQ_CMD, 1,
  520. FRF_AZ_RX_FLUSH_DESCQ, rx_queue->queue);
  521. efx_writeo(efx, &rx_flush_descq, FR_AZ_RX_FLUSH_DESCQ);
  522. }
  523. void efx_nic_fini_rx(struct efx_rx_queue *rx_queue)
  524. {
  525. efx_oword_t rx_desc_ptr;
  526. struct efx_nic *efx = rx_queue->efx;
  527. /* The queue should already have been flushed */
  528. WARN_ON(rx_queue->flushed != FLUSH_DONE);
  529. /* Remove RX descriptor ring from card */
  530. EFX_ZERO_OWORD(rx_desc_ptr);
  531. efx_writeo_table(efx, &rx_desc_ptr, efx->type->rxd_ptr_tbl_base,
  532. rx_queue->queue);
  533. /* Unpin RX descriptor ring */
  534. efx_fini_special_buffer(efx, &rx_queue->rxd);
  535. }
  536. /* Free buffers backing RX queue */
  537. void efx_nic_remove_rx(struct efx_rx_queue *rx_queue)
  538. {
  539. efx_free_special_buffer(rx_queue->efx, &rx_queue->rxd);
  540. }
  541. /**************************************************************************
  542. *
  543. * Event queue processing
  544. * Event queues are processed by per-channel tasklets.
  545. *
  546. **************************************************************************/
  547. /* Update a channel's event queue's read pointer (RPTR) register
  548. *
  549. * This writes the EVQ_RPTR_REG register for the specified channel's
  550. * event queue.
  551. *
  552. * Note that EVQ_RPTR_REG contains the index of the "last read" event,
  553. * whereas channel->eventq_read_ptr contains the index of the "next to
  554. * read" event.
  555. */
  556. void efx_nic_eventq_read_ack(struct efx_channel *channel)
  557. {
  558. efx_dword_t reg;
  559. struct efx_nic *efx = channel->efx;
  560. EFX_POPULATE_DWORD_1(reg, FRF_AZ_EVQ_RPTR, channel->eventq_read_ptr);
  561. efx_writed_table(efx, &reg, efx->type->evq_rptr_tbl_base,
  562. channel->channel);
  563. }
  564. /* Use HW to insert a SW defined event */
  565. void efx_generate_event(struct efx_channel *channel, efx_qword_t *event)
  566. {
  567. efx_oword_t drv_ev_reg;
  568. BUILD_BUG_ON(FRF_AZ_DRV_EV_DATA_LBN != 0 ||
  569. FRF_AZ_DRV_EV_DATA_WIDTH != 64);
  570. drv_ev_reg.u32[0] = event->u32[0];
  571. drv_ev_reg.u32[1] = event->u32[1];
  572. drv_ev_reg.u32[2] = 0;
  573. drv_ev_reg.u32[3] = 0;
  574. EFX_SET_OWORD_FIELD(drv_ev_reg, FRF_AZ_DRV_EV_QID, channel->channel);
  575. efx_writeo(channel->efx, &drv_ev_reg, FR_AZ_DRV_EV);
  576. }
  577. /* Handle a transmit completion event
  578. *
  579. * The NIC batches TX completion events; the message we receive is of
  580. * the form "complete all TX events up to this index".
  581. */
  582. static void
  583. efx_handle_tx_event(struct efx_channel *channel, efx_qword_t *event)
  584. {
  585. unsigned int tx_ev_desc_ptr;
  586. unsigned int tx_ev_q_label;
  587. struct efx_tx_queue *tx_queue;
  588. struct efx_nic *efx = channel->efx;
  589. if (likely(EFX_QWORD_FIELD(*event, FSF_AZ_TX_EV_COMP))) {
  590. /* Transmit completion */
  591. tx_ev_desc_ptr = EFX_QWORD_FIELD(*event, FSF_AZ_TX_EV_DESC_PTR);
  592. tx_ev_q_label = EFX_QWORD_FIELD(*event, FSF_AZ_TX_EV_Q_LABEL);
  593. tx_queue = &efx->tx_queue[tx_ev_q_label];
  594. channel->irq_mod_score +=
  595. (tx_ev_desc_ptr - tx_queue->read_count) &
  596. EFX_TXQ_MASK;
  597. efx_xmit_done(tx_queue, tx_ev_desc_ptr);
  598. } else if (EFX_QWORD_FIELD(*event, FSF_AZ_TX_EV_WQ_FF_FULL)) {
  599. /* Rewrite the FIFO write pointer */
  600. tx_ev_q_label = EFX_QWORD_FIELD(*event, FSF_AZ_TX_EV_Q_LABEL);
  601. tx_queue = &efx->tx_queue[tx_ev_q_label];
  602. if (efx_dev_registered(efx))
  603. netif_tx_lock(efx->net_dev);
  604. efx_notify_tx_desc(tx_queue);
  605. if (efx_dev_registered(efx))
  606. netif_tx_unlock(efx->net_dev);
  607. } else if (EFX_QWORD_FIELD(*event, FSF_AZ_TX_EV_PKT_ERR) &&
  608. EFX_WORKAROUND_10727(efx)) {
  609. efx_schedule_reset(efx, RESET_TYPE_TX_DESC_FETCH);
  610. } else {
  611. EFX_ERR(efx, "channel %d unexpected TX event "
  612. EFX_QWORD_FMT"\n", channel->channel,
  613. EFX_QWORD_VAL(*event));
  614. }
  615. }
  616. /* Detect errors included in the rx_evt_pkt_ok bit. */
  617. static void efx_handle_rx_not_ok(struct efx_rx_queue *rx_queue,
  618. const efx_qword_t *event,
  619. bool *rx_ev_pkt_ok,
  620. bool *discard)
  621. {
  622. struct efx_nic *efx = rx_queue->efx;
  623. bool rx_ev_buf_owner_id_err, rx_ev_ip_hdr_chksum_err;
  624. bool rx_ev_tcp_udp_chksum_err, rx_ev_eth_crc_err;
  625. bool rx_ev_frm_trunc, rx_ev_drib_nib, rx_ev_tobe_disc;
  626. bool rx_ev_other_err, rx_ev_pause_frm;
  627. bool rx_ev_hdr_type, rx_ev_mcast_pkt;
  628. unsigned rx_ev_pkt_type;
  629. rx_ev_hdr_type = EFX_QWORD_FIELD(*event, FSF_AZ_RX_EV_HDR_TYPE);
  630. rx_ev_mcast_pkt = EFX_QWORD_FIELD(*event, FSF_AZ_RX_EV_MCAST_PKT);
  631. rx_ev_tobe_disc = EFX_QWORD_FIELD(*event, FSF_AZ_RX_EV_TOBE_DISC);
  632. rx_ev_pkt_type = EFX_QWORD_FIELD(*event, FSF_AZ_RX_EV_PKT_TYPE);
  633. rx_ev_buf_owner_id_err = EFX_QWORD_FIELD(*event,
  634. FSF_AZ_RX_EV_BUF_OWNER_ID_ERR);
  635. rx_ev_ip_hdr_chksum_err = EFX_QWORD_FIELD(*event,
  636. FSF_AZ_RX_EV_IP_HDR_CHKSUM_ERR);
  637. rx_ev_tcp_udp_chksum_err = EFX_QWORD_FIELD(*event,
  638. FSF_AZ_RX_EV_TCP_UDP_CHKSUM_ERR);
  639. rx_ev_eth_crc_err = EFX_QWORD_FIELD(*event, FSF_AZ_RX_EV_ETH_CRC_ERR);
  640. rx_ev_frm_trunc = EFX_QWORD_FIELD(*event, FSF_AZ_RX_EV_FRM_TRUNC);
  641. rx_ev_drib_nib = ((efx_nic_rev(efx) >= EFX_REV_FALCON_B0) ?
  642. 0 : EFX_QWORD_FIELD(*event, FSF_AA_RX_EV_DRIB_NIB));
  643. rx_ev_pause_frm = EFX_QWORD_FIELD(*event, FSF_AZ_RX_EV_PAUSE_FRM_ERR);
  644. /* Every error apart from tobe_disc and pause_frm */
  645. rx_ev_other_err = (rx_ev_drib_nib | rx_ev_tcp_udp_chksum_err |
  646. rx_ev_buf_owner_id_err | rx_ev_eth_crc_err |
  647. rx_ev_frm_trunc | rx_ev_ip_hdr_chksum_err);
  648. /* Count errors that are not in MAC stats. Ignore expected
  649. * checksum errors during self-test. */
  650. if (rx_ev_frm_trunc)
  651. ++rx_queue->channel->n_rx_frm_trunc;
  652. else if (rx_ev_tobe_disc)
  653. ++rx_queue->channel->n_rx_tobe_disc;
  654. else if (!efx->loopback_selftest) {
  655. if (rx_ev_ip_hdr_chksum_err)
  656. ++rx_queue->channel->n_rx_ip_hdr_chksum_err;
  657. else if (rx_ev_tcp_udp_chksum_err)
  658. ++rx_queue->channel->n_rx_tcp_udp_chksum_err;
  659. }
  660. /* The frame must be discarded if any of these are true. */
  661. *discard = (rx_ev_eth_crc_err | rx_ev_frm_trunc | rx_ev_drib_nib |
  662. rx_ev_tobe_disc | rx_ev_pause_frm);
  663. /* TOBE_DISC is expected on unicast mismatches; don't print out an
  664. * error message. FRM_TRUNC indicates RXDP dropped the packet due
  665. * to a FIFO overflow.
  666. */
  667. #ifdef EFX_ENABLE_DEBUG
  668. if (rx_ev_other_err) {
  669. EFX_INFO_RL(efx, " RX queue %d unexpected RX event "
  670. EFX_QWORD_FMT "%s%s%s%s%s%s%s%s\n",
  671. rx_queue->queue, EFX_QWORD_VAL(*event),
  672. rx_ev_buf_owner_id_err ? " [OWNER_ID_ERR]" : "",
  673. rx_ev_ip_hdr_chksum_err ?
  674. " [IP_HDR_CHKSUM_ERR]" : "",
  675. rx_ev_tcp_udp_chksum_err ?
  676. " [TCP_UDP_CHKSUM_ERR]" : "",
  677. rx_ev_eth_crc_err ? " [ETH_CRC_ERR]" : "",
  678. rx_ev_frm_trunc ? " [FRM_TRUNC]" : "",
  679. rx_ev_drib_nib ? " [DRIB_NIB]" : "",
  680. rx_ev_tobe_disc ? " [TOBE_DISC]" : "",
  681. rx_ev_pause_frm ? " [PAUSE]" : "");
  682. }
  683. #endif
  684. }
  685. /* Handle receive events that are not in-order. */
  686. static void
  687. efx_handle_rx_bad_index(struct efx_rx_queue *rx_queue, unsigned index)
  688. {
  689. struct efx_nic *efx = rx_queue->efx;
  690. unsigned expected, dropped;
  691. expected = rx_queue->removed_count & EFX_RXQ_MASK;
  692. dropped = (index - expected) & EFX_RXQ_MASK;
  693. EFX_INFO(efx, "dropped %d events (index=%d expected=%d)\n",
  694. dropped, index, expected);
  695. efx_schedule_reset(efx, EFX_WORKAROUND_5676(efx) ?
  696. RESET_TYPE_RX_RECOVERY : RESET_TYPE_DISABLE);
  697. }
  698. /* Handle a packet received event
  699. *
  700. * The NIC gives a "discard" flag if it's a unicast packet with the
  701. * wrong destination address
  702. * Also "is multicast" and "matches multicast filter" flags can be used to
  703. * discard non-matching multicast packets.
  704. */
  705. static void
  706. efx_handle_rx_event(struct efx_channel *channel, const efx_qword_t *event)
  707. {
  708. unsigned int rx_ev_desc_ptr, rx_ev_byte_cnt;
  709. unsigned int rx_ev_hdr_type, rx_ev_mcast_pkt;
  710. unsigned expected_ptr;
  711. bool rx_ev_pkt_ok, discard = false, checksummed;
  712. struct efx_rx_queue *rx_queue;
  713. struct efx_nic *efx = channel->efx;
  714. /* Basic packet information */
  715. rx_ev_byte_cnt = EFX_QWORD_FIELD(*event, FSF_AZ_RX_EV_BYTE_CNT);
  716. rx_ev_pkt_ok = EFX_QWORD_FIELD(*event, FSF_AZ_RX_EV_PKT_OK);
  717. rx_ev_hdr_type = EFX_QWORD_FIELD(*event, FSF_AZ_RX_EV_HDR_TYPE);
  718. WARN_ON(EFX_QWORD_FIELD(*event, FSF_AZ_RX_EV_JUMBO_CONT));
  719. WARN_ON(EFX_QWORD_FIELD(*event, FSF_AZ_RX_EV_SOP) != 1);
  720. WARN_ON(EFX_QWORD_FIELD(*event, FSF_AZ_RX_EV_Q_LABEL) !=
  721. channel->channel);
  722. rx_queue = &efx->rx_queue[channel->channel];
  723. rx_ev_desc_ptr = EFX_QWORD_FIELD(*event, FSF_AZ_RX_EV_DESC_PTR);
  724. expected_ptr = rx_queue->removed_count & EFX_RXQ_MASK;
  725. if (unlikely(rx_ev_desc_ptr != expected_ptr))
  726. efx_handle_rx_bad_index(rx_queue, rx_ev_desc_ptr);
  727. if (likely(rx_ev_pkt_ok)) {
  728. /* If packet is marked as OK and packet type is TCP/IP or
  729. * UDP/IP, then we can rely on the hardware checksum.
  730. */
  731. checksummed =
  732. likely(efx->rx_checksum_enabled) &&
  733. (rx_ev_hdr_type == FSE_CZ_RX_EV_HDR_TYPE_IPV4V6_TCP ||
  734. rx_ev_hdr_type == FSE_CZ_RX_EV_HDR_TYPE_IPV4V6_UDP);
  735. } else {
  736. efx_handle_rx_not_ok(rx_queue, event, &rx_ev_pkt_ok, &discard);
  737. checksummed = false;
  738. }
  739. /* Detect multicast packets that didn't match the filter */
  740. rx_ev_mcast_pkt = EFX_QWORD_FIELD(*event, FSF_AZ_RX_EV_MCAST_PKT);
  741. if (rx_ev_mcast_pkt) {
  742. unsigned int rx_ev_mcast_hash_match =
  743. EFX_QWORD_FIELD(*event, FSF_AZ_RX_EV_MCAST_HASH_MATCH);
  744. if (unlikely(!rx_ev_mcast_hash_match)) {
  745. ++channel->n_rx_mcast_mismatch;
  746. discard = true;
  747. }
  748. }
  749. channel->irq_mod_score += 2;
  750. /* Handle received packet */
  751. efx_rx_packet(rx_queue, rx_ev_desc_ptr, rx_ev_byte_cnt,
  752. checksummed, discard);
  753. }
  754. /* Global events are basically PHY events */
  755. static void
  756. efx_handle_global_event(struct efx_channel *channel, efx_qword_t *event)
  757. {
  758. struct efx_nic *efx = channel->efx;
  759. bool handled = false;
  760. if (EFX_QWORD_FIELD(*event, FSF_AB_GLB_EV_G_PHY0_INTR) ||
  761. EFX_QWORD_FIELD(*event, FSF_AB_GLB_EV_XG_PHY0_INTR) ||
  762. EFX_QWORD_FIELD(*event, FSF_AB_GLB_EV_XFP_PHY0_INTR)) {
  763. /* Ignored */
  764. handled = true;
  765. }
  766. if ((efx_nic_rev(efx) >= EFX_REV_FALCON_B0) &&
  767. EFX_QWORD_FIELD(*event, FSF_BB_GLB_EV_XG_MGT_INTR)) {
  768. efx->xmac_poll_required = true;
  769. handled = true;
  770. }
  771. if (efx_nic_rev(efx) <= EFX_REV_FALCON_A1 ?
  772. EFX_QWORD_FIELD(*event, FSF_AA_GLB_EV_RX_RECOVERY) :
  773. EFX_QWORD_FIELD(*event, FSF_BB_GLB_EV_RX_RECOVERY)) {
  774. EFX_ERR(efx, "channel %d seen global RX_RESET "
  775. "event. Resetting.\n", channel->channel);
  776. atomic_inc(&efx->rx_reset);
  777. efx_schedule_reset(efx, EFX_WORKAROUND_6555(efx) ?
  778. RESET_TYPE_RX_RECOVERY : RESET_TYPE_DISABLE);
  779. handled = true;
  780. }
  781. if (!handled)
  782. EFX_ERR(efx, "channel %d unknown global event "
  783. EFX_QWORD_FMT "\n", channel->channel,
  784. EFX_QWORD_VAL(*event));
  785. }
  786. static void
  787. efx_handle_driver_event(struct efx_channel *channel, efx_qword_t *event)
  788. {
  789. struct efx_nic *efx = channel->efx;
  790. unsigned int ev_sub_code;
  791. unsigned int ev_sub_data;
  792. ev_sub_code = EFX_QWORD_FIELD(*event, FSF_AZ_DRIVER_EV_SUBCODE);
  793. ev_sub_data = EFX_QWORD_FIELD(*event, FSF_AZ_DRIVER_EV_SUBDATA);
  794. switch (ev_sub_code) {
  795. case FSE_AZ_TX_DESCQ_FLS_DONE_EV:
  796. EFX_TRACE(efx, "channel %d TXQ %d flushed\n",
  797. channel->channel, ev_sub_data);
  798. break;
  799. case FSE_AZ_RX_DESCQ_FLS_DONE_EV:
  800. EFX_TRACE(efx, "channel %d RXQ %d flushed\n",
  801. channel->channel, ev_sub_data);
  802. break;
  803. case FSE_AZ_EVQ_INIT_DONE_EV:
  804. EFX_LOG(efx, "channel %d EVQ %d initialised\n",
  805. channel->channel, ev_sub_data);
  806. break;
  807. case FSE_AZ_SRM_UPD_DONE_EV:
  808. EFX_TRACE(efx, "channel %d SRAM update done\n",
  809. channel->channel);
  810. break;
  811. case FSE_AZ_WAKE_UP_EV:
  812. EFX_TRACE(efx, "channel %d RXQ %d wakeup event\n",
  813. channel->channel, ev_sub_data);
  814. break;
  815. case FSE_AZ_TIMER_EV:
  816. EFX_TRACE(efx, "channel %d RX queue %d timer expired\n",
  817. channel->channel, ev_sub_data);
  818. break;
  819. case FSE_AA_RX_RECOVER_EV:
  820. EFX_ERR(efx, "channel %d seen DRIVER RX_RESET event. "
  821. "Resetting.\n", channel->channel);
  822. atomic_inc(&efx->rx_reset);
  823. efx_schedule_reset(efx,
  824. EFX_WORKAROUND_6555(efx) ?
  825. RESET_TYPE_RX_RECOVERY :
  826. RESET_TYPE_DISABLE);
  827. break;
  828. case FSE_BZ_RX_DSC_ERROR_EV:
  829. EFX_ERR(efx, "RX DMA Q %d reports descriptor fetch error."
  830. " RX Q %d is disabled.\n", ev_sub_data, ev_sub_data);
  831. efx_schedule_reset(efx, RESET_TYPE_RX_DESC_FETCH);
  832. break;
  833. case FSE_BZ_TX_DSC_ERROR_EV:
  834. EFX_ERR(efx, "TX DMA Q %d reports descriptor fetch error."
  835. " TX Q %d is disabled.\n", ev_sub_data, ev_sub_data);
  836. efx_schedule_reset(efx, RESET_TYPE_TX_DESC_FETCH);
  837. break;
  838. default:
  839. EFX_TRACE(efx, "channel %d unknown driver event code %d "
  840. "data %04x\n", channel->channel, ev_sub_code,
  841. ev_sub_data);
  842. break;
  843. }
  844. }
  845. int efx_nic_process_eventq(struct efx_channel *channel, int rx_quota)
  846. {
  847. unsigned int read_ptr;
  848. efx_qword_t event, *p_event;
  849. int ev_code;
  850. int rx_packets = 0;
  851. read_ptr = channel->eventq_read_ptr;
  852. do {
  853. p_event = efx_event(channel, read_ptr);
  854. event = *p_event;
  855. if (!efx_event_present(&event))
  856. /* End of events */
  857. break;
  858. EFX_TRACE(channel->efx, "channel %d event is "EFX_QWORD_FMT"\n",
  859. channel->channel, EFX_QWORD_VAL(event));
  860. /* Clear this event by marking it all ones */
  861. EFX_SET_QWORD(*p_event);
  862. ev_code = EFX_QWORD_FIELD(event, FSF_AZ_EV_CODE);
  863. switch (ev_code) {
  864. case FSE_AZ_EV_CODE_RX_EV:
  865. efx_handle_rx_event(channel, &event);
  866. ++rx_packets;
  867. break;
  868. case FSE_AZ_EV_CODE_TX_EV:
  869. efx_handle_tx_event(channel, &event);
  870. break;
  871. case FSE_AZ_EV_CODE_DRV_GEN_EV:
  872. channel->eventq_magic = EFX_QWORD_FIELD(
  873. event, FSF_AZ_DRV_GEN_EV_MAGIC);
  874. EFX_LOG(channel->efx, "channel %d received generated "
  875. "event "EFX_QWORD_FMT"\n", channel->channel,
  876. EFX_QWORD_VAL(event));
  877. break;
  878. case FSE_AZ_EV_CODE_GLOBAL_EV:
  879. efx_handle_global_event(channel, &event);
  880. break;
  881. case FSE_AZ_EV_CODE_DRIVER_EV:
  882. efx_handle_driver_event(channel, &event);
  883. break;
  884. default:
  885. EFX_ERR(channel->efx, "channel %d unknown event type %d"
  886. " (data " EFX_QWORD_FMT ")\n", channel->channel,
  887. ev_code, EFX_QWORD_VAL(event));
  888. }
  889. /* Increment read pointer */
  890. read_ptr = (read_ptr + 1) & EFX_EVQ_MASK;
  891. } while (rx_packets < rx_quota);
  892. channel->eventq_read_ptr = read_ptr;
  893. return rx_packets;
  894. }
  895. static void falcon_push_irq_moderation(struct efx_channel *channel)
  896. {
  897. efx_dword_t timer_cmd;
  898. struct efx_nic *efx = channel->efx;
  899. /* Set timer register */
  900. if (channel->irq_moderation) {
  901. EFX_POPULATE_DWORD_2(timer_cmd,
  902. FRF_AB_TC_TIMER_MODE,
  903. FFE_BB_TIMER_MODE_INT_HLDOFF,
  904. FRF_AB_TC_TIMER_VAL,
  905. channel->irq_moderation - 1);
  906. } else {
  907. EFX_POPULATE_DWORD_2(timer_cmd,
  908. FRF_AB_TC_TIMER_MODE,
  909. FFE_BB_TIMER_MODE_DIS,
  910. FRF_AB_TC_TIMER_VAL, 0);
  911. }
  912. BUILD_BUG_ON(FR_AA_TIMER_COMMAND_KER != FR_BZ_TIMER_COMMAND_P0);
  913. efx_writed_page_locked(efx, &timer_cmd, FR_BZ_TIMER_COMMAND_P0,
  914. channel->channel);
  915. }
  916. /* Allocate buffer table entries for event queue */
  917. int efx_nic_probe_eventq(struct efx_channel *channel)
  918. {
  919. struct efx_nic *efx = channel->efx;
  920. BUILD_BUG_ON(EFX_EVQ_SIZE < 512 || EFX_EVQ_SIZE > 32768 ||
  921. EFX_EVQ_SIZE & EFX_EVQ_MASK);
  922. return efx_alloc_special_buffer(efx, &channel->eventq,
  923. EFX_EVQ_SIZE * sizeof(efx_qword_t));
  924. }
  925. void efx_nic_init_eventq(struct efx_channel *channel)
  926. {
  927. efx_oword_t evq_ptr;
  928. struct efx_nic *efx = channel->efx;
  929. EFX_LOG(efx, "channel %d event queue in special buffers %d-%d\n",
  930. channel->channel, channel->eventq.index,
  931. channel->eventq.index + channel->eventq.entries - 1);
  932. /* Pin event queue buffer */
  933. efx_init_special_buffer(efx, &channel->eventq);
  934. /* Fill event queue with all ones (i.e. empty events) */
  935. memset(channel->eventq.addr, 0xff, channel->eventq.len);
  936. /* Push event queue to card */
  937. EFX_POPULATE_OWORD_3(evq_ptr,
  938. FRF_AZ_EVQ_EN, 1,
  939. FRF_AZ_EVQ_SIZE, __ffs(channel->eventq.entries),
  940. FRF_AZ_EVQ_BUF_BASE_ID, channel->eventq.index);
  941. efx_writeo_table(efx, &evq_ptr, efx->type->evq_ptr_tbl_base,
  942. channel->channel);
  943. efx->type->push_irq_moderation(channel);
  944. }
  945. void efx_nic_fini_eventq(struct efx_channel *channel)
  946. {
  947. efx_oword_t eventq_ptr;
  948. struct efx_nic *efx = channel->efx;
  949. /* Remove event queue from card */
  950. EFX_ZERO_OWORD(eventq_ptr);
  951. efx_writeo_table(efx, &eventq_ptr, efx->type->evq_ptr_tbl_base,
  952. channel->channel);
  953. /* Unpin event queue */
  954. efx_fini_special_buffer(efx, &channel->eventq);
  955. }
  956. /* Free buffers backing event queue */
  957. void efx_nic_remove_eventq(struct efx_channel *channel)
  958. {
  959. efx_free_special_buffer(channel->efx, &channel->eventq);
  960. }
  961. /* Generates a test event on the event queue. A subsequent call to
  962. * process_eventq() should pick up the event and place the value of
  963. * "magic" into channel->eventq_magic;
  964. */
  965. void efx_nic_generate_test_event(struct efx_channel *channel, unsigned int magic)
  966. {
  967. efx_qword_t test_event;
  968. EFX_POPULATE_QWORD_2(test_event, FSF_AZ_EV_CODE,
  969. FSE_AZ_EV_CODE_DRV_GEN_EV,
  970. FSF_AZ_DRV_GEN_EV_MAGIC, magic);
  971. efx_generate_event(channel, &test_event);
  972. }
  973. /**************************************************************************
  974. *
  975. * Flush handling
  976. *
  977. **************************************************************************/
  978. static void efx_poll_flush_events(struct efx_nic *efx)
  979. {
  980. struct efx_channel *channel = &efx->channel[0];
  981. struct efx_tx_queue *tx_queue;
  982. struct efx_rx_queue *rx_queue;
  983. unsigned int read_ptr = channel->eventq_read_ptr;
  984. unsigned int end_ptr = (read_ptr - 1) & EFX_EVQ_MASK;
  985. do {
  986. efx_qword_t *event = efx_event(channel, read_ptr);
  987. int ev_code, ev_sub_code, ev_queue;
  988. bool ev_failed;
  989. if (!efx_event_present(event))
  990. break;
  991. ev_code = EFX_QWORD_FIELD(*event, FSF_AZ_EV_CODE);
  992. ev_sub_code = EFX_QWORD_FIELD(*event,
  993. FSF_AZ_DRIVER_EV_SUBCODE);
  994. if (ev_code == FSE_AZ_EV_CODE_DRIVER_EV &&
  995. ev_sub_code == FSE_AZ_TX_DESCQ_FLS_DONE_EV) {
  996. ev_queue = EFX_QWORD_FIELD(*event,
  997. FSF_AZ_DRIVER_EV_SUBDATA);
  998. if (ev_queue < EFX_TX_QUEUE_COUNT) {
  999. tx_queue = efx->tx_queue + ev_queue;
  1000. tx_queue->flushed = FLUSH_DONE;
  1001. }
  1002. } else if (ev_code == FSE_AZ_EV_CODE_DRIVER_EV &&
  1003. ev_sub_code == FSE_AZ_RX_DESCQ_FLS_DONE_EV) {
  1004. ev_queue = EFX_QWORD_FIELD(
  1005. *event, FSF_AZ_DRIVER_EV_RX_DESCQ_ID);
  1006. ev_failed = EFX_QWORD_FIELD(
  1007. *event, FSF_AZ_DRIVER_EV_RX_FLUSH_FAIL);
  1008. if (ev_queue < efx->n_rx_queues) {
  1009. rx_queue = efx->rx_queue + ev_queue;
  1010. rx_queue->flushed =
  1011. ev_failed ? FLUSH_FAILED : FLUSH_DONE;
  1012. }
  1013. }
  1014. /* We're about to destroy the queue anyway, so
  1015. * it's ok to throw away every non-flush event */
  1016. EFX_SET_QWORD(*event);
  1017. read_ptr = (read_ptr + 1) & EFX_EVQ_MASK;
  1018. } while (read_ptr != end_ptr);
  1019. channel->eventq_read_ptr = read_ptr;
  1020. }
  1021. static void falcon_deconfigure_mac_wrapper(struct efx_nic *efx);
  1022. static void falcon_prepare_flush(struct efx_nic *efx)
  1023. {
  1024. falcon_deconfigure_mac_wrapper(efx);
  1025. /* Wait for the tx and rx fifo's to get to the next packet boundary
  1026. * (~1ms without back-pressure), then to drain the remainder of the
  1027. * fifo's at data path speeds (negligible), with a healthy margin. */
  1028. msleep(10);
  1029. }
  1030. /* Handle tx and rx flushes at the same time, since they run in
  1031. * parallel in the hardware and there's no reason for us to
  1032. * serialise them */
  1033. int efx_nic_flush_queues(struct efx_nic *efx)
  1034. {
  1035. struct efx_rx_queue *rx_queue;
  1036. struct efx_tx_queue *tx_queue;
  1037. int i, tx_pending, rx_pending;
  1038. /* If necessary prepare the hardware for flushing */
  1039. efx->type->prepare_flush(efx);
  1040. /* Flush all tx queues in parallel */
  1041. efx_for_each_tx_queue(tx_queue, efx)
  1042. efx_flush_tx_queue(tx_queue);
  1043. /* The hardware supports four concurrent rx flushes, each of which may
  1044. * need to be retried if there is an outstanding descriptor fetch */
  1045. for (i = 0; i < EFX_FLUSH_POLL_COUNT; ++i) {
  1046. rx_pending = tx_pending = 0;
  1047. efx_for_each_rx_queue(rx_queue, efx) {
  1048. if (rx_queue->flushed == FLUSH_PENDING)
  1049. ++rx_pending;
  1050. }
  1051. efx_for_each_rx_queue(rx_queue, efx) {
  1052. if (rx_pending == EFX_RX_FLUSH_COUNT)
  1053. break;
  1054. if (rx_queue->flushed == FLUSH_FAILED ||
  1055. rx_queue->flushed == FLUSH_NONE) {
  1056. efx_flush_rx_queue(rx_queue);
  1057. ++rx_pending;
  1058. }
  1059. }
  1060. efx_for_each_tx_queue(tx_queue, efx) {
  1061. if (tx_queue->flushed != FLUSH_DONE)
  1062. ++tx_pending;
  1063. }
  1064. if (rx_pending == 0 && tx_pending == 0)
  1065. return 0;
  1066. msleep(EFX_FLUSH_INTERVAL);
  1067. efx_poll_flush_events(efx);
  1068. }
  1069. /* Mark the queues as all flushed. We're going to return failure
  1070. * leading to a reset, or fake up success anyway */
  1071. efx_for_each_tx_queue(tx_queue, efx) {
  1072. if (tx_queue->flushed != FLUSH_DONE)
  1073. EFX_ERR(efx, "tx queue %d flush command timed out\n",
  1074. tx_queue->queue);
  1075. tx_queue->flushed = FLUSH_DONE;
  1076. }
  1077. efx_for_each_rx_queue(rx_queue, efx) {
  1078. if (rx_queue->flushed != FLUSH_DONE)
  1079. EFX_ERR(efx, "rx queue %d flush command timed out\n",
  1080. rx_queue->queue);
  1081. rx_queue->flushed = FLUSH_DONE;
  1082. }
  1083. if (EFX_WORKAROUND_7803(efx))
  1084. return 0;
  1085. return -ETIMEDOUT;
  1086. }
  1087. /**************************************************************************
  1088. *
  1089. * Hardware interrupts
  1090. * The hardware interrupt handler does very little work; all the event
  1091. * queue processing is carried out by per-channel tasklets.
  1092. *
  1093. **************************************************************************/
  1094. /* Enable/disable/generate interrupts */
  1095. static inline void efx_nic_interrupts(struct efx_nic *efx,
  1096. bool enabled, bool force)
  1097. {
  1098. efx_oword_t int_en_reg_ker;
  1099. EFX_POPULATE_OWORD_2(int_en_reg_ker,
  1100. FRF_AZ_KER_INT_KER, force,
  1101. FRF_AZ_DRV_INT_EN_KER, enabled);
  1102. efx_writeo(efx, &int_en_reg_ker, FR_AZ_INT_EN_KER);
  1103. }
  1104. void efx_nic_enable_interrupts(struct efx_nic *efx)
  1105. {
  1106. struct efx_channel *channel;
  1107. EFX_ZERO_OWORD(*((efx_oword_t *) efx->irq_status.addr));
  1108. wmb(); /* Ensure interrupt vector is clear before interrupts enabled */
  1109. /* Enable interrupts */
  1110. efx_nic_interrupts(efx, true, false);
  1111. /* Force processing of all the channels to get the EVQ RPTRs up to
  1112. date */
  1113. efx_for_each_channel(channel, efx)
  1114. efx_schedule_channel(channel);
  1115. }
  1116. void efx_nic_disable_interrupts(struct efx_nic *efx)
  1117. {
  1118. /* Disable interrupts */
  1119. efx_nic_interrupts(efx, false, false);
  1120. }
  1121. /* Generate a test interrupt
  1122. * Interrupt must already have been enabled, otherwise nasty things
  1123. * may happen.
  1124. */
  1125. void efx_nic_generate_interrupt(struct efx_nic *efx)
  1126. {
  1127. efx_nic_interrupts(efx, true, true);
  1128. }
  1129. /* Acknowledge a legacy interrupt from Falcon
  1130. *
  1131. * This acknowledges a legacy (not MSI) interrupt via INT_ACK_KER_REG.
  1132. *
  1133. * Due to SFC bug 3706 (silicon revision <=A1) reads can be duplicated in the
  1134. * BIU. Interrupt acknowledge is read sensitive so must write instead
  1135. * (then read to ensure the BIU collector is flushed)
  1136. *
  1137. * NB most hardware supports MSI interrupts
  1138. */
  1139. inline void falcon_irq_ack_a1(struct efx_nic *efx)
  1140. {
  1141. efx_dword_t reg;
  1142. EFX_POPULATE_DWORD_1(reg, FRF_AA_INT_ACK_KER_FIELD, 0xb7eb7e);
  1143. efx_writed(efx, &reg, FR_AA_INT_ACK_KER);
  1144. efx_readd(efx, &reg, FR_AA_WORK_AROUND_BROKEN_PCI_READS);
  1145. }
  1146. /* Process a fatal interrupt
  1147. * Disable bus mastering ASAP and schedule a reset
  1148. */
  1149. irqreturn_t efx_nic_fatal_interrupt(struct efx_nic *efx)
  1150. {
  1151. struct falcon_nic_data *nic_data = efx->nic_data;
  1152. efx_oword_t *int_ker = efx->irq_status.addr;
  1153. efx_oword_t fatal_intr;
  1154. int error, mem_perr;
  1155. efx_reado(efx, &fatal_intr, FR_AZ_FATAL_INTR_KER);
  1156. error = EFX_OWORD_FIELD(fatal_intr, FRF_AZ_FATAL_INTR);
  1157. EFX_ERR(efx, "SYSTEM ERROR " EFX_OWORD_FMT " status "
  1158. EFX_OWORD_FMT ": %s\n", EFX_OWORD_VAL(*int_ker),
  1159. EFX_OWORD_VAL(fatal_intr),
  1160. error ? "disabling bus mastering" : "no recognised error");
  1161. if (error == 0)
  1162. goto out;
  1163. /* If this is a memory parity error dump which blocks are offending */
  1164. mem_perr = EFX_OWORD_FIELD(fatal_intr, FRF_AZ_MEM_PERR_INT_KER);
  1165. if (mem_perr) {
  1166. efx_oword_t reg;
  1167. efx_reado(efx, &reg, FR_AZ_MEM_STAT);
  1168. EFX_ERR(efx, "SYSTEM ERROR: memory parity error "
  1169. EFX_OWORD_FMT "\n", EFX_OWORD_VAL(reg));
  1170. }
  1171. /* Disable both devices */
  1172. pci_clear_master(efx->pci_dev);
  1173. if (efx_nic_is_dual_func(efx))
  1174. pci_clear_master(nic_data->pci_dev2);
  1175. efx_nic_disable_interrupts(efx);
  1176. /* Count errors and reset or disable the NIC accordingly */
  1177. if (efx->int_error_count == 0 ||
  1178. time_after(jiffies, efx->int_error_expire)) {
  1179. efx->int_error_count = 0;
  1180. efx->int_error_expire =
  1181. jiffies + EFX_INT_ERROR_EXPIRE * HZ;
  1182. }
  1183. if (++efx->int_error_count < EFX_MAX_INT_ERRORS) {
  1184. EFX_ERR(efx, "SYSTEM ERROR - reset scheduled\n");
  1185. efx_schedule_reset(efx, RESET_TYPE_INT_ERROR);
  1186. } else {
  1187. EFX_ERR(efx, "SYSTEM ERROR - max number of errors seen."
  1188. "NIC will be disabled\n");
  1189. efx_schedule_reset(efx, RESET_TYPE_DISABLE);
  1190. }
  1191. out:
  1192. return IRQ_HANDLED;
  1193. }
  1194. /* Handle a legacy interrupt
  1195. * Acknowledges the interrupt and schedule event queue processing.
  1196. */
  1197. static irqreturn_t efx_legacy_interrupt(int irq, void *dev_id)
  1198. {
  1199. struct efx_nic *efx = dev_id;
  1200. efx_oword_t *int_ker = efx->irq_status.addr;
  1201. irqreturn_t result = IRQ_NONE;
  1202. struct efx_channel *channel;
  1203. efx_dword_t reg;
  1204. u32 queues;
  1205. int syserr;
  1206. /* Read the ISR which also ACKs the interrupts */
  1207. efx_readd(efx, &reg, FR_BZ_INT_ISR0);
  1208. queues = EFX_EXTRACT_DWORD(reg, 0, 31);
  1209. /* Check to see if we have a serious error condition */
  1210. syserr = EFX_OWORD_FIELD(*int_ker, FSF_AZ_NET_IVEC_FATAL_INT);
  1211. if (unlikely(syserr))
  1212. return efx_nic_fatal_interrupt(efx);
  1213. /* Schedule processing of any interrupting queues */
  1214. efx_for_each_channel(channel, efx) {
  1215. if ((queues & 1) ||
  1216. efx_event_present(
  1217. efx_event(channel, channel->eventq_read_ptr))) {
  1218. efx_schedule_channel(channel);
  1219. result = IRQ_HANDLED;
  1220. }
  1221. queues >>= 1;
  1222. }
  1223. if (result == IRQ_HANDLED) {
  1224. efx->last_irq_cpu = raw_smp_processor_id();
  1225. EFX_TRACE(efx, "IRQ %d on CPU %d status " EFX_DWORD_FMT "\n",
  1226. irq, raw_smp_processor_id(), EFX_DWORD_VAL(reg));
  1227. }
  1228. return result;
  1229. }
  1230. irqreturn_t falcon_legacy_interrupt_a1(int irq, void *dev_id)
  1231. {
  1232. struct efx_nic *efx = dev_id;
  1233. efx_oword_t *int_ker = efx->irq_status.addr;
  1234. struct efx_channel *channel;
  1235. int syserr;
  1236. int queues;
  1237. /* Check to see if this is our interrupt. If it isn't, we
  1238. * exit without having touched the hardware.
  1239. */
  1240. if (unlikely(EFX_OWORD_IS_ZERO(*int_ker))) {
  1241. EFX_TRACE(efx, "IRQ %d on CPU %d not for me\n", irq,
  1242. raw_smp_processor_id());
  1243. return IRQ_NONE;
  1244. }
  1245. efx->last_irq_cpu = raw_smp_processor_id();
  1246. EFX_TRACE(efx, "IRQ %d on CPU %d status " EFX_OWORD_FMT "\n",
  1247. irq, raw_smp_processor_id(), EFX_OWORD_VAL(*int_ker));
  1248. /* Check to see if we have a serious error condition */
  1249. syserr = EFX_OWORD_FIELD(*int_ker, FSF_AZ_NET_IVEC_FATAL_INT);
  1250. if (unlikely(syserr))
  1251. return efx_nic_fatal_interrupt(efx);
  1252. /* Determine interrupting queues, clear interrupt status
  1253. * register and acknowledge the device interrupt.
  1254. */
  1255. BUILD_BUG_ON(FSF_AZ_NET_IVEC_INT_Q_WIDTH > EFX_MAX_CHANNELS);
  1256. queues = EFX_OWORD_FIELD(*int_ker, FSF_AZ_NET_IVEC_INT_Q);
  1257. EFX_ZERO_OWORD(*int_ker);
  1258. wmb(); /* Ensure the vector is cleared before interrupt ack */
  1259. falcon_irq_ack_a1(efx);
  1260. /* Schedule processing of any interrupting queues */
  1261. channel = &efx->channel[0];
  1262. while (queues) {
  1263. if (queues & 0x01)
  1264. efx_schedule_channel(channel);
  1265. channel++;
  1266. queues >>= 1;
  1267. }
  1268. return IRQ_HANDLED;
  1269. }
  1270. /* Handle an MSI interrupt
  1271. *
  1272. * Handle an MSI hardware interrupt. This routine schedules event
  1273. * queue processing. No interrupt acknowledgement cycle is necessary.
  1274. * Also, we never need to check that the interrupt is for us, since
  1275. * MSI interrupts cannot be shared.
  1276. */
  1277. static irqreturn_t efx_msi_interrupt(int irq, void *dev_id)
  1278. {
  1279. struct efx_channel *channel = dev_id;
  1280. struct efx_nic *efx = channel->efx;
  1281. efx_oword_t *int_ker = efx->irq_status.addr;
  1282. int syserr;
  1283. efx->last_irq_cpu = raw_smp_processor_id();
  1284. EFX_TRACE(efx, "IRQ %d on CPU %d status " EFX_OWORD_FMT "\n",
  1285. irq, raw_smp_processor_id(), EFX_OWORD_VAL(*int_ker));
  1286. /* Check to see if we have a serious error condition */
  1287. syserr = EFX_OWORD_FIELD(*int_ker, FSF_AZ_NET_IVEC_FATAL_INT);
  1288. if (unlikely(syserr))
  1289. return efx_nic_fatal_interrupt(efx);
  1290. /* Schedule processing of the channel */
  1291. efx_schedule_channel(channel);
  1292. return IRQ_HANDLED;
  1293. }
  1294. /* Setup RSS indirection table.
  1295. * This maps from the hash value of the packet to RXQ
  1296. */
  1297. static void efx_setup_rss_indir_table(struct efx_nic *efx)
  1298. {
  1299. int i = 0;
  1300. unsigned long offset;
  1301. efx_dword_t dword;
  1302. if (efx_nic_rev(efx) < EFX_REV_FALCON_B0)
  1303. return;
  1304. for (offset = FR_BZ_RX_INDIRECTION_TBL;
  1305. offset < FR_BZ_RX_INDIRECTION_TBL + 0x800;
  1306. offset += 0x10) {
  1307. EFX_POPULATE_DWORD_1(dword, FRF_BZ_IT_QUEUE,
  1308. i % efx->n_rx_queues);
  1309. efx_writed(efx, &dword, offset);
  1310. i++;
  1311. }
  1312. }
  1313. /* Hook interrupt handler(s)
  1314. * Try MSI and then legacy interrupts.
  1315. */
  1316. int efx_nic_init_interrupt(struct efx_nic *efx)
  1317. {
  1318. struct efx_channel *channel;
  1319. int rc;
  1320. if (!EFX_INT_MODE_USE_MSI(efx)) {
  1321. irq_handler_t handler;
  1322. if (efx_nic_rev(efx) >= EFX_REV_FALCON_B0)
  1323. handler = efx_legacy_interrupt;
  1324. else
  1325. handler = falcon_legacy_interrupt_a1;
  1326. rc = request_irq(efx->legacy_irq, handler, IRQF_SHARED,
  1327. efx->name, efx);
  1328. if (rc) {
  1329. EFX_ERR(efx, "failed to hook legacy IRQ %d\n",
  1330. efx->pci_dev->irq);
  1331. goto fail1;
  1332. }
  1333. return 0;
  1334. }
  1335. /* Hook MSI or MSI-X interrupt */
  1336. efx_for_each_channel(channel, efx) {
  1337. rc = request_irq(channel->irq, efx_msi_interrupt,
  1338. IRQF_PROBE_SHARED, /* Not shared */
  1339. channel->name, channel);
  1340. if (rc) {
  1341. EFX_ERR(efx, "failed to hook IRQ %d\n", channel->irq);
  1342. goto fail2;
  1343. }
  1344. }
  1345. return 0;
  1346. fail2:
  1347. efx_for_each_channel(channel, efx)
  1348. free_irq(channel->irq, channel);
  1349. fail1:
  1350. return rc;
  1351. }
  1352. void efx_nic_fini_interrupt(struct efx_nic *efx)
  1353. {
  1354. struct efx_channel *channel;
  1355. efx_oword_t reg;
  1356. /* Disable MSI/MSI-X interrupts */
  1357. efx_for_each_channel(channel, efx) {
  1358. if (channel->irq)
  1359. free_irq(channel->irq, channel);
  1360. }
  1361. /* ACK legacy interrupt */
  1362. if (efx_nic_rev(efx) >= EFX_REV_FALCON_B0)
  1363. efx_reado(efx, &reg, FR_BZ_INT_ISR0);
  1364. else
  1365. falcon_irq_ack_a1(efx);
  1366. /* Disable legacy interrupt */
  1367. if (efx->legacy_irq)
  1368. free_irq(efx->legacy_irq, efx);
  1369. }
  1370. /**************************************************************************
  1371. *
  1372. * EEPROM/flash
  1373. *
  1374. **************************************************************************
  1375. */
  1376. #define FALCON_SPI_MAX_LEN sizeof(efx_oword_t)
  1377. static int falcon_spi_poll(struct efx_nic *efx)
  1378. {
  1379. efx_oword_t reg;
  1380. efx_reado(efx, &reg, FR_AB_EE_SPI_HCMD);
  1381. return EFX_OWORD_FIELD(reg, FRF_AB_EE_SPI_HCMD_CMD_EN) ? -EBUSY : 0;
  1382. }
  1383. /* Wait for SPI command completion */
  1384. static int falcon_spi_wait(struct efx_nic *efx)
  1385. {
  1386. /* Most commands will finish quickly, so we start polling at
  1387. * very short intervals. Sometimes the command may have to
  1388. * wait for VPD or expansion ROM access outside of our
  1389. * control, so we allow up to 100 ms. */
  1390. unsigned long timeout = jiffies + 1 + DIV_ROUND_UP(HZ, 10);
  1391. int i;
  1392. for (i = 0; i < 10; i++) {
  1393. if (!falcon_spi_poll(efx))
  1394. return 0;
  1395. udelay(10);
  1396. }
  1397. for (;;) {
  1398. if (!falcon_spi_poll(efx))
  1399. return 0;
  1400. if (time_after_eq(jiffies, timeout)) {
  1401. EFX_ERR(efx, "timed out waiting for SPI\n");
  1402. return -ETIMEDOUT;
  1403. }
  1404. schedule_timeout_uninterruptible(1);
  1405. }
  1406. }
  1407. int falcon_spi_cmd(struct efx_nic *efx, const struct efx_spi_device *spi,
  1408. unsigned int command, int address,
  1409. const void *in, void *out, size_t len)
  1410. {
  1411. bool addressed = (address >= 0);
  1412. bool reading = (out != NULL);
  1413. efx_oword_t reg;
  1414. int rc;
  1415. /* Input validation */
  1416. if (len > FALCON_SPI_MAX_LEN)
  1417. return -EINVAL;
  1418. BUG_ON(!mutex_is_locked(&efx->spi_lock));
  1419. /* Check that previous command is not still running */
  1420. rc = falcon_spi_poll(efx);
  1421. if (rc)
  1422. return rc;
  1423. /* Program address register, if we have an address */
  1424. if (addressed) {
  1425. EFX_POPULATE_OWORD_1(reg, FRF_AB_EE_SPI_HADR_ADR, address);
  1426. efx_writeo(efx, &reg, FR_AB_EE_SPI_HADR);
  1427. }
  1428. /* Program data register, if we have data */
  1429. if (in != NULL) {
  1430. memcpy(&reg, in, len);
  1431. efx_writeo(efx, &reg, FR_AB_EE_SPI_HDATA);
  1432. }
  1433. /* Issue read/write command */
  1434. EFX_POPULATE_OWORD_7(reg,
  1435. FRF_AB_EE_SPI_HCMD_CMD_EN, 1,
  1436. FRF_AB_EE_SPI_HCMD_SF_SEL, spi->device_id,
  1437. FRF_AB_EE_SPI_HCMD_DABCNT, len,
  1438. FRF_AB_EE_SPI_HCMD_READ, reading,
  1439. FRF_AB_EE_SPI_HCMD_DUBCNT, 0,
  1440. FRF_AB_EE_SPI_HCMD_ADBCNT,
  1441. (addressed ? spi->addr_len : 0),
  1442. FRF_AB_EE_SPI_HCMD_ENC, command);
  1443. efx_writeo(efx, &reg, FR_AB_EE_SPI_HCMD);
  1444. /* Wait for read/write to complete */
  1445. rc = falcon_spi_wait(efx);
  1446. if (rc)
  1447. return rc;
  1448. /* Read data */
  1449. if (out != NULL) {
  1450. efx_reado(efx, &reg, FR_AB_EE_SPI_HDATA);
  1451. memcpy(out, &reg, len);
  1452. }
  1453. return 0;
  1454. }
  1455. static size_t
  1456. falcon_spi_write_limit(const struct efx_spi_device *spi, size_t start)
  1457. {
  1458. return min(FALCON_SPI_MAX_LEN,
  1459. (spi->block_size - (start & (spi->block_size - 1))));
  1460. }
  1461. static inline u8
  1462. efx_spi_munge_command(const struct efx_spi_device *spi,
  1463. const u8 command, const unsigned int address)
  1464. {
  1465. return command | (((address >> 8) & spi->munge_address) << 3);
  1466. }
  1467. /* Wait up to 10 ms for buffered write completion */
  1468. int
  1469. falcon_spi_wait_write(struct efx_nic *efx, const struct efx_spi_device *spi)
  1470. {
  1471. unsigned long timeout = jiffies + 1 + DIV_ROUND_UP(HZ, 100);
  1472. u8 status;
  1473. int rc;
  1474. for (;;) {
  1475. rc = falcon_spi_cmd(efx, spi, SPI_RDSR, -1, NULL,
  1476. &status, sizeof(status));
  1477. if (rc)
  1478. return rc;
  1479. if (!(status & SPI_STATUS_NRDY))
  1480. return 0;
  1481. if (time_after_eq(jiffies, timeout)) {
  1482. EFX_ERR(efx, "SPI write timeout on device %d"
  1483. " last status=0x%02x\n",
  1484. spi->device_id, status);
  1485. return -ETIMEDOUT;
  1486. }
  1487. schedule_timeout_uninterruptible(1);
  1488. }
  1489. }
  1490. int falcon_spi_read(struct efx_nic *efx, const struct efx_spi_device *spi,
  1491. loff_t start, size_t len, size_t *retlen, u8 *buffer)
  1492. {
  1493. size_t block_len, pos = 0;
  1494. unsigned int command;
  1495. int rc = 0;
  1496. while (pos < len) {
  1497. block_len = min(len - pos, FALCON_SPI_MAX_LEN);
  1498. command = efx_spi_munge_command(spi, SPI_READ, start + pos);
  1499. rc = falcon_spi_cmd(efx, spi, command, start + pos, NULL,
  1500. buffer + pos, block_len);
  1501. if (rc)
  1502. break;
  1503. pos += block_len;
  1504. /* Avoid locking up the system */
  1505. cond_resched();
  1506. if (signal_pending(current)) {
  1507. rc = -EINTR;
  1508. break;
  1509. }
  1510. }
  1511. if (retlen)
  1512. *retlen = pos;
  1513. return rc;
  1514. }
  1515. int
  1516. falcon_spi_write(struct efx_nic *efx, const struct efx_spi_device *spi,
  1517. loff_t start, size_t len, size_t *retlen, const u8 *buffer)
  1518. {
  1519. u8 verify_buffer[FALCON_SPI_MAX_LEN];
  1520. size_t block_len, pos = 0;
  1521. unsigned int command;
  1522. int rc = 0;
  1523. while (pos < len) {
  1524. rc = falcon_spi_cmd(efx, spi, SPI_WREN, -1, NULL, NULL, 0);
  1525. if (rc)
  1526. break;
  1527. block_len = min(len - pos,
  1528. falcon_spi_write_limit(spi, start + pos));
  1529. command = efx_spi_munge_command(spi, SPI_WRITE, start + pos);
  1530. rc = falcon_spi_cmd(efx, spi, command, start + pos,
  1531. buffer + pos, NULL, block_len);
  1532. if (rc)
  1533. break;
  1534. rc = falcon_spi_wait_write(efx, spi);
  1535. if (rc)
  1536. break;
  1537. command = efx_spi_munge_command(spi, SPI_READ, start + pos);
  1538. rc = falcon_spi_cmd(efx, spi, command, start + pos,
  1539. NULL, verify_buffer, block_len);
  1540. if (memcmp(verify_buffer, buffer + pos, block_len)) {
  1541. rc = -EIO;
  1542. break;
  1543. }
  1544. pos += block_len;
  1545. /* Avoid locking up the system */
  1546. cond_resched();
  1547. if (signal_pending(current)) {
  1548. rc = -EINTR;
  1549. break;
  1550. }
  1551. }
  1552. if (retlen)
  1553. *retlen = pos;
  1554. return rc;
  1555. }
  1556. /**************************************************************************
  1557. *
  1558. * MAC wrapper
  1559. *
  1560. **************************************************************************
  1561. */
  1562. static void falcon_push_multicast_hash(struct efx_nic *efx)
  1563. {
  1564. union efx_multicast_hash *mc_hash = &efx->multicast_hash;
  1565. WARN_ON(!mutex_is_locked(&efx->mac_lock));
  1566. efx_writeo(efx, &mc_hash->oword[0], FR_AB_MAC_MC_HASH_REG0);
  1567. efx_writeo(efx, &mc_hash->oword[1], FR_AB_MAC_MC_HASH_REG1);
  1568. }
  1569. static void falcon_reset_macs(struct efx_nic *efx)
  1570. {
  1571. struct falcon_nic_data *nic_data = efx->nic_data;
  1572. efx_oword_t reg, mac_ctrl;
  1573. int count;
  1574. if (efx_nic_rev(efx) < EFX_REV_FALCON_B0) {
  1575. /* It's not safe to use GLB_CTL_REG to reset the
  1576. * macs, so instead use the internal MAC resets
  1577. */
  1578. if (!EFX_IS10G(efx)) {
  1579. EFX_POPULATE_OWORD_1(reg, FRF_AB_GM_SW_RST, 1);
  1580. efx_writeo(efx, &reg, FR_AB_GM_CFG1);
  1581. udelay(1000);
  1582. EFX_POPULATE_OWORD_1(reg, FRF_AB_GM_SW_RST, 0);
  1583. efx_writeo(efx, &reg, FR_AB_GM_CFG1);
  1584. udelay(1000);
  1585. return;
  1586. } else {
  1587. EFX_POPULATE_OWORD_1(reg, FRF_AB_XM_CORE_RST, 1);
  1588. efx_writeo(efx, &reg, FR_AB_XM_GLB_CFG);
  1589. for (count = 0; count < 10000; count++) {
  1590. efx_reado(efx, &reg, FR_AB_XM_GLB_CFG);
  1591. if (EFX_OWORD_FIELD(reg, FRF_AB_XM_CORE_RST) ==
  1592. 0)
  1593. return;
  1594. udelay(10);
  1595. }
  1596. EFX_ERR(efx, "timed out waiting for XMAC core reset\n");
  1597. }
  1598. }
  1599. /* Mac stats will fail whist the TX fifo is draining */
  1600. WARN_ON(nic_data->stats_disable_count == 0);
  1601. efx_reado(efx, &mac_ctrl, FR_AB_MAC_CTRL);
  1602. EFX_SET_OWORD_FIELD(mac_ctrl, FRF_BB_TXFIFO_DRAIN_EN, 1);
  1603. efx_writeo(efx, &mac_ctrl, FR_AB_MAC_CTRL);
  1604. efx_reado(efx, &reg, FR_AB_GLB_CTL);
  1605. EFX_SET_OWORD_FIELD(reg, FRF_AB_RST_XGTX, 1);
  1606. EFX_SET_OWORD_FIELD(reg, FRF_AB_RST_XGRX, 1);
  1607. EFX_SET_OWORD_FIELD(reg, FRF_AB_RST_EM, 1);
  1608. efx_writeo(efx, &reg, FR_AB_GLB_CTL);
  1609. count = 0;
  1610. while (1) {
  1611. efx_reado(efx, &reg, FR_AB_GLB_CTL);
  1612. if (!EFX_OWORD_FIELD(reg, FRF_AB_RST_XGTX) &&
  1613. !EFX_OWORD_FIELD(reg, FRF_AB_RST_XGRX) &&
  1614. !EFX_OWORD_FIELD(reg, FRF_AB_RST_EM)) {
  1615. EFX_LOG(efx, "Completed MAC reset after %d loops\n",
  1616. count);
  1617. break;
  1618. }
  1619. if (count > 20) {
  1620. EFX_ERR(efx, "MAC reset failed\n");
  1621. break;
  1622. }
  1623. count++;
  1624. udelay(10);
  1625. }
  1626. /* Ensure the correct MAC is selected before statistics
  1627. * are re-enabled by the caller */
  1628. efx_writeo(efx, &mac_ctrl, FR_AB_MAC_CTRL);
  1629. }
  1630. void falcon_drain_tx_fifo(struct efx_nic *efx)
  1631. {
  1632. efx_oword_t reg;
  1633. if ((efx_nic_rev(efx) < EFX_REV_FALCON_B0) ||
  1634. (efx->loopback_mode != LOOPBACK_NONE))
  1635. return;
  1636. efx_reado(efx, &reg, FR_AB_MAC_CTRL);
  1637. /* There is no point in draining more than once */
  1638. if (EFX_OWORD_FIELD(reg, FRF_BB_TXFIFO_DRAIN_EN))
  1639. return;
  1640. falcon_reset_macs(efx);
  1641. }
  1642. static void falcon_deconfigure_mac_wrapper(struct efx_nic *efx)
  1643. {
  1644. efx_oword_t reg;
  1645. if (efx_nic_rev(efx) < EFX_REV_FALCON_B0)
  1646. return;
  1647. /* Isolate the MAC -> RX */
  1648. efx_reado(efx, &reg, FR_AZ_RX_CFG);
  1649. EFX_SET_OWORD_FIELD(reg, FRF_BZ_RX_INGR_EN, 0);
  1650. efx_writeo(efx, &reg, FR_AZ_RX_CFG);
  1651. /* Isolate TX -> MAC */
  1652. falcon_drain_tx_fifo(efx);
  1653. }
  1654. void falcon_reconfigure_mac_wrapper(struct efx_nic *efx)
  1655. {
  1656. struct efx_link_state *link_state = &efx->link_state;
  1657. efx_oword_t reg;
  1658. int link_speed;
  1659. switch (link_state->speed) {
  1660. case 10000: link_speed = 3; break;
  1661. case 1000: link_speed = 2; break;
  1662. case 100: link_speed = 1; break;
  1663. default: link_speed = 0; break;
  1664. }
  1665. /* MAC_LINK_STATUS controls MAC backpressure but doesn't work
  1666. * as advertised. Disable to ensure packets are not
  1667. * indefinitely held and TX queue can be flushed at any point
  1668. * while the link is down. */
  1669. EFX_POPULATE_OWORD_5(reg,
  1670. FRF_AB_MAC_XOFF_VAL, 0xffff /* max pause time */,
  1671. FRF_AB_MAC_BCAD_ACPT, 1,
  1672. FRF_AB_MAC_UC_PROM, efx->promiscuous,
  1673. FRF_AB_MAC_LINK_STATUS, 1, /* always set */
  1674. FRF_AB_MAC_SPEED, link_speed);
  1675. /* On B0, MAC backpressure can be disabled and packets get
  1676. * discarded. */
  1677. if (efx_nic_rev(efx) >= EFX_REV_FALCON_B0) {
  1678. EFX_SET_OWORD_FIELD(reg, FRF_BB_TXFIFO_DRAIN_EN,
  1679. !link_state->up);
  1680. }
  1681. efx_writeo(efx, &reg, FR_AB_MAC_CTRL);
  1682. /* Restore the multicast hash registers. */
  1683. falcon_push_multicast_hash(efx);
  1684. efx_reado(efx, &reg, FR_AZ_RX_CFG);
  1685. /* Enable XOFF signal from RX FIFO (we enabled it during NIC
  1686. * initialisation but it may read back as 0) */
  1687. EFX_SET_OWORD_FIELD(reg, FRF_AZ_RX_XOFF_MAC_EN, 1);
  1688. /* Unisolate the MAC -> RX */
  1689. if (efx_nic_rev(efx) >= EFX_REV_FALCON_B0)
  1690. EFX_SET_OWORD_FIELD(reg, FRF_BZ_RX_INGR_EN, 1);
  1691. efx_writeo(efx, &reg, FR_AZ_RX_CFG);
  1692. }
  1693. static void falcon_stats_request(struct efx_nic *efx)
  1694. {
  1695. struct falcon_nic_data *nic_data = efx->nic_data;
  1696. efx_oword_t reg;
  1697. WARN_ON(nic_data->stats_pending);
  1698. WARN_ON(nic_data->stats_disable_count);
  1699. if (nic_data->stats_dma_done == NULL)
  1700. return; /* no mac selected */
  1701. *nic_data->stats_dma_done = FALCON_STATS_NOT_DONE;
  1702. nic_data->stats_pending = true;
  1703. wmb(); /* ensure done flag is clear */
  1704. /* Initiate DMA transfer of stats */
  1705. EFX_POPULATE_OWORD_2(reg,
  1706. FRF_AB_MAC_STAT_DMA_CMD, 1,
  1707. FRF_AB_MAC_STAT_DMA_ADR,
  1708. efx->stats_buffer.dma_addr);
  1709. efx_writeo(efx, &reg, FR_AB_MAC_STAT_DMA);
  1710. mod_timer(&nic_data->stats_timer, round_jiffies_up(jiffies + HZ / 2));
  1711. }
  1712. static void falcon_stats_complete(struct efx_nic *efx)
  1713. {
  1714. struct falcon_nic_data *nic_data = efx->nic_data;
  1715. if (!nic_data->stats_pending)
  1716. return;
  1717. nic_data->stats_pending = 0;
  1718. if (*nic_data->stats_dma_done == FALCON_STATS_DONE) {
  1719. rmb(); /* read the done flag before the stats */
  1720. efx->mac_op->update_stats(efx);
  1721. } else {
  1722. EFX_ERR(efx, "timed out waiting for statistics\n");
  1723. }
  1724. }
  1725. static void falcon_stats_timer_func(unsigned long context)
  1726. {
  1727. struct efx_nic *efx = (struct efx_nic *)context;
  1728. struct falcon_nic_data *nic_data = efx->nic_data;
  1729. spin_lock(&efx->stats_lock);
  1730. falcon_stats_complete(efx);
  1731. if (nic_data->stats_disable_count == 0)
  1732. falcon_stats_request(efx);
  1733. spin_unlock(&efx->stats_lock);
  1734. }
  1735. static void falcon_switch_mac(struct efx_nic *efx);
  1736. static bool falcon_loopback_link_poll(struct efx_nic *efx)
  1737. {
  1738. struct efx_link_state old_state = efx->link_state;
  1739. WARN_ON(!mutex_is_locked(&efx->mac_lock));
  1740. WARN_ON(!LOOPBACK_INTERNAL(efx));
  1741. efx->link_state.fd = true;
  1742. efx->link_state.fc = efx->wanted_fc;
  1743. efx->link_state.up = true;
  1744. if (efx->loopback_mode == LOOPBACK_GMAC)
  1745. efx->link_state.speed = 1000;
  1746. else
  1747. efx->link_state.speed = 10000;
  1748. return !efx_link_state_equal(&efx->link_state, &old_state);
  1749. }
  1750. static int falcon_reconfigure_port(struct efx_nic *efx)
  1751. {
  1752. int rc;
  1753. WARN_ON(efx_nic_rev(efx) > EFX_REV_FALCON_B0);
  1754. /* Poll the PHY link state *before* reconfiguring it. This means we
  1755. * will pick up the correct speed (in loopback) to select the correct
  1756. * MAC.
  1757. */
  1758. if (LOOPBACK_INTERNAL(efx))
  1759. falcon_loopback_link_poll(efx);
  1760. else
  1761. efx->phy_op->poll(efx);
  1762. falcon_stop_nic_stats(efx);
  1763. falcon_deconfigure_mac_wrapper(efx);
  1764. falcon_switch_mac(efx);
  1765. efx->phy_op->reconfigure(efx);
  1766. rc = efx->mac_op->reconfigure(efx);
  1767. BUG_ON(rc);
  1768. falcon_start_nic_stats(efx);
  1769. /* Synchronise efx->link_state with the kernel */
  1770. efx_link_status_changed(efx);
  1771. return 0;
  1772. }
  1773. /**************************************************************************
  1774. *
  1775. * PHY access via GMII
  1776. *
  1777. **************************************************************************
  1778. */
  1779. /* Wait for GMII access to complete */
  1780. static int falcon_gmii_wait(struct efx_nic *efx)
  1781. {
  1782. efx_oword_t md_stat;
  1783. int count;
  1784. /* wait upto 50ms - taken max from datasheet */
  1785. for (count = 0; count < 5000; count++) {
  1786. efx_reado(efx, &md_stat, FR_AB_MD_STAT);
  1787. if (EFX_OWORD_FIELD(md_stat, FRF_AB_MD_BSY) == 0) {
  1788. if (EFX_OWORD_FIELD(md_stat, FRF_AB_MD_LNFL) != 0 ||
  1789. EFX_OWORD_FIELD(md_stat, FRF_AB_MD_BSERR) != 0) {
  1790. EFX_ERR(efx, "error from GMII access "
  1791. EFX_OWORD_FMT"\n",
  1792. EFX_OWORD_VAL(md_stat));
  1793. return -EIO;
  1794. }
  1795. return 0;
  1796. }
  1797. udelay(10);
  1798. }
  1799. EFX_ERR(efx, "timed out waiting for GMII\n");
  1800. return -ETIMEDOUT;
  1801. }
  1802. /* Write an MDIO register of a PHY connected to Falcon. */
  1803. static int falcon_mdio_write(struct net_device *net_dev,
  1804. int prtad, int devad, u16 addr, u16 value)
  1805. {
  1806. struct efx_nic *efx = netdev_priv(net_dev);
  1807. efx_oword_t reg;
  1808. int rc;
  1809. EFX_REGDUMP(efx, "writing MDIO %d register %d.%d with 0x%04x\n",
  1810. prtad, devad, addr, value);
  1811. mutex_lock(&efx->mdio_lock);
  1812. /* Check MDIO not currently being accessed */
  1813. rc = falcon_gmii_wait(efx);
  1814. if (rc)
  1815. goto out;
  1816. /* Write the address/ID register */
  1817. EFX_POPULATE_OWORD_1(reg, FRF_AB_MD_PHY_ADR, addr);
  1818. efx_writeo(efx, &reg, FR_AB_MD_PHY_ADR);
  1819. EFX_POPULATE_OWORD_2(reg, FRF_AB_MD_PRT_ADR, prtad,
  1820. FRF_AB_MD_DEV_ADR, devad);
  1821. efx_writeo(efx, &reg, FR_AB_MD_ID);
  1822. /* Write data */
  1823. EFX_POPULATE_OWORD_1(reg, FRF_AB_MD_TXD, value);
  1824. efx_writeo(efx, &reg, FR_AB_MD_TXD);
  1825. EFX_POPULATE_OWORD_2(reg,
  1826. FRF_AB_MD_WRC, 1,
  1827. FRF_AB_MD_GC, 0);
  1828. efx_writeo(efx, &reg, FR_AB_MD_CS);
  1829. /* Wait for data to be written */
  1830. rc = falcon_gmii_wait(efx);
  1831. if (rc) {
  1832. /* Abort the write operation */
  1833. EFX_POPULATE_OWORD_2(reg,
  1834. FRF_AB_MD_WRC, 0,
  1835. FRF_AB_MD_GC, 1);
  1836. efx_writeo(efx, &reg, FR_AB_MD_CS);
  1837. udelay(10);
  1838. }
  1839. out:
  1840. mutex_unlock(&efx->mdio_lock);
  1841. return rc;
  1842. }
  1843. /* Read an MDIO register of a PHY connected to Falcon. */
  1844. static int falcon_mdio_read(struct net_device *net_dev,
  1845. int prtad, int devad, u16 addr)
  1846. {
  1847. struct efx_nic *efx = netdev_priv(net_dev);
  1848. efx_oword_t reg;
  1849. int rc;
  1850. mutex_lock(&efx->mdio_lock);
  1851. /* Check MDIO not currently being accessed */
  1852. rc = falcon_gmii_wait(efx);
  1853. if (rc)
  1854. goto out;
  1855. EFX_POPULATE_OWORD_1(reg, FRF_AB_MD_PHY_ADR, addr);
  1856. efx_writeo(efx, &reg, FR_AB_MD_PHY_ADR);
  1857. EFX_POPULATE_OWORD_2(reg, FRF_AB_MD_PRT_ADR, prtad,
  1858. FRF_AB_MD_DEV_ADR, devad);
  1859. efx_writeo(efx, &reg, FR_AB_MD_ID);
  1860. /* Request data to be read */
  1861. EFX_POPULATE_OWORD_2(reg, FRF_AB_MD_RDC, 1, FRF_AB_MD_GC, 0);
  1862. efx_writeo(efx, &reg, FR_AB_MD_CS);
  1863. /* Wait for data to become available */
  1864. rc = falcon_gmii_wait(efx);
  1865. if (rc == 0) {
  1866. efx_reado(efx, &reg, FR_AB_MD_RXD);
  1867. rc = EFX_OWORD_FIELD(reg, FRF_AB_MD_RXD);
  1868. EFX_REGDUMP(efx, "read from MDIO %d register %d.%d, got %04x\n",
  1869. prtad, devad, addr, rc);
  1870. } else {
  1871. /* Abort the read operation */
  1872. EFX_POPULATE_OWORD_2(reg,
  1873. FRF_AB_MD_RIC, 0,
  1874. FRF_AB_MD_GC, 1);
  1875. efx_writeo(efx, &reg, FR_AB_MD_CS);
  1876. EFX_LOG(efx, "read from MDIO %d register %d.%d, got error %d\n",
  1877. prtad, devad, addr, rc);
  1878. }
  1879. out:
  1880. mutex_unlock(&efx->mdio_lock);
  1881. return rc;
  1882. }
  1883. static void falcon_clock_mac(struct efx_nic *efx)
  1884. {
  1885. unsigned strap_val;
  1886. efx_oword_t nic_stat;
  1887. /* Configure the NIC generated MAC clock correctly */
  1888. efx_reado(efx, &nic_stat, FR_AB_NIC_STAT);
  1889. strap_val = EFX_IS10G(efx) ? 5 : 3;
  1890. if (efx_nic_rev(efx) >= EFX_REV_FALCON_B0) {
  1891. EFX_SET_OWORD_FIELD(nic_stat, FRF_BB_EE_STRAP_EN, 1);
  1892. EFX_SET_OWORD_FIELD(nic_stat, FRF_BB_EE_STRAP, strap_val);
  1893. efx_writeo(efx, &nic_stat, FR_AB_NIC_STAT);
  1894. } else {
  1895. /* Falcon A1 does not support 1G/10G speed switching
  1896. * and must not be used with a PHY that does. */
  1897. BUG_ON(EFX_OWORD_FIELD(nic_stat, FRF_AB_STRAP_PINS) !=
  1898. strap_val);
  1899. }
  1900. }
  1901. static void falcon_switch_mac(struct efx_nic *efx)
  1902. {
  1903. struct efx_mac_operations *old_mac_op = efx->mac_op;
  1904. struct falcon_nic_data *nic_data = efx->nic_data;
  1905. unsigned int stats_done_offset;
  1906. WARN_ON(!mutex_is_locked(&efx->mac_lock));
  1907. WARN_ON(nic_data->stats_disable_count == 0);
  1908. efx->mac_op = (EFX_IS10G(efx) ?
  1909. &falcon_xmac_operations : &falcon_gmac_operations);
  1910. if (EFX_IS10G(efx))
  1911. stats_done_offset = XgDmaDone_offset;
  1912. else
  1913. stats_done_offset = GDmaDone_offset;
  1914. nic_data->stats_dma_done = efx->stats_buffer.addr + stats_done_offset;
  1915. if (old_mac_op == efx->mac_op)
  1916. return;
  1917. falcon_clock_mac(efx);
  1918. EFX_LOG(efx, "selected %cMAC\n", EFX_IS10G(efx) ? 'X' : 'G');
  1919. /* Not all macs support a mac-level link state */
  1920. efx->xmac_poll_required = false;
  1921. falcon_reset_macs(efx);
  1922. }
  1923. /* This call is responsible for hooking in the MAC and PHY operations */
  1924. static int falcon_probe_port(struct efx_nic *efx)
  1925. {
  1926. int rc;
  1927. switch (efx->phy_type) {
  1928. case PHY_TYPE_SFX7101:
  1929. efx->phy_op = &falcon_sfx7101_phy_ops;
  1930. break;
  1931. case PHY_TYPE_SFT9001A:
  1932. case PHY_TYPE_SFT9001B:
  1933. efx->phy_op = &falcon_sft9001_phy_ops;
  1934. break;
  1935. case PHY_TYPE_QT2022C2:
  1936. case PHY_TYPE_QT2025C:
  1937. efx->phy_op = &falcon_qt202x_phy_ops;
  1938. break;
  1939. default:
  1940. EFX_ERR(efx, "Unknown PHY type %d\n",
  1941. efx->phy_type);
  1942. return -ENODEV;
  1943. }
  1944. /* Fill out MDIO structure and loopback modes */
  1945. efx->mdio.mdio_read = falcon_mdio_read;
  1946. efx->mdio.mdio_write = falcon_mdio_write;
  1947. rc = efx->phy_op->probe(efx);
  1948. if (rc != 0)
  1949. return rc;
  1950. /* Initial assumption */
  1951. efx->link_state.speed = 10000;
  1952. efx->link_state.fd = true;
  1953. /* Hardware flow ctrl. FalconA RX FIFO too small for pause generation */
  1954. if (efx_nic_rev(efx) >= EFX_REV_FALCON_B0)
  1955. efx->wanted_fc = EFX_FC_RX | EFX_FC_TX;
  1956. else
  1957. efx->wanted_fc = EFX_FC_RX;
  1958. /* Allocate buffer for stats */
  1959. rc = efx_nic_alloc_buffer(efx, &efx->stats_buffer,
  1960. FALCON_MAC_STATS_SIZE);
  1961. if (rc)
  1962. return rc;
  1963. EFX_LOG(efx, "stats buffer at %llx (virt %p phys %llx)\n",
  1964. (u64)efx->stats_buffer.dma_addr,
  1965. efx->stats_buffer.addr,
  1966. (u64)virt_to_phys(efx->stats_buffer.addr));
  1967. return 0;
  1968. }
  1969. static void falcon_remove_port(struct efx_nic *efx)
  1970. {
  1971. efx_nic_free_buffer(efx, &efx->stats_buffer);
  1972. }
  1973. /**************************************************************************
  1974. *
  1975. * Falcon test code
  1976. *
  1977. **************************************************************************/
  1978. static int
  1979. falcon_read_nvram(struct efx_nic *efx, struct falcon_nvconfig *nvconfig_out)
  1980. {
  1981. struct falcon_nvconfig *nvconfig;
  1982. struct efx_spi_device *spi;
  1983. void *region;
  1984. int rc, magic_num, struct_ver;
  1985. __le16 *word, *limit;
  1986. u32 csum;
  1987. spi = efx->spi_flash ? efx->spi_flash : efx->spi_eeprom;
  1988. if (!spi)
  1989. return -EINVAL;
  1990. region = kmalloc(FALCON_NVCONFIG_END, GFP_KERNEL);
  1991. if (!region)
  1992. return -ENOMEM;
  1993. nvconfig = region + FALCON_NVCONFIG_OFFSET;
  1994. mutex_lock(&efx->spi_lock);
  1995. rc = falcon_spi_read(efx, spi, 0, FALCON_NVCONFIG_END, NULL, region);
  1996. mutex_unlock(&efx->spi_lock);
  1997. if (rc) {
  1998. EFX_ERR(efx, "Failed to read %s\n",
  1999. efx->spi_flash ? "flash" : "EEPROM");
  2000. rc = -EIO;
  2001. goto out;
  2002. }
  2003. magic_num = le16_to_cpu(nvconfig->board_magic_num);
  2004. struct_ver = le16_to_cpu(nvconfig->board_struct_ver);
  2005. rc = -EINVAL;
  2006. if (magic_num != FALCON_NVCONFIG_BOARD_MAGIC_NUM) {
  2007. EFX_ERR(efx, "NVRAM bad magic 0x%x\n", magic_num);
  2008. goto out;
  2009. }
  2010. if (struct_ver < 2) {
  2011. EFX_ERR(efx, "NVRAM has ancient version 0x%x\n", struct_ver);
  2012. goto out;
  2013. } else if (struct_ver < 4) {
  2014. word = &nvconfig->board_magic_num;
  2015. limit = (__le16 *) (nvconfig + 1);
  2016. } else {
  2017. word = region;
  2018. limit = region + FALCON_NVCONFIG_END;
  2019. }
  2020. for (csum = 0; word < limit; ++word)
  2021. csum += le16_to_cpu(*word);
  2022. if (~csum & 0xffff) {
  2023. EFX_ERR(efx, "NVRAM has incorrect checksum\n");
  2024. goto out;
  2025. }
  2026. rc = 0;
  2027. if (nvconfig_out)
  2028. memcpy(nvconfig_out, nvconfig, sizeof(*nvconfig));
  2029. out:
  2030. kfree(region);
  2031. return rc;
  2032. }
  2033. static int falcon_test_nvram(struct efx_nic *efx)
  2034. {
  2035. return falcon_read_nvram(efx, NULL);
  2036. }
  2037. static const struct efx_nic_register_test falcon_b0_register_tests[] = {
  2038. { FR_AZ_ADR_REGION,
  2039. EFX_OWORD32(0x0001FFFF, 0x0001FFFF, 0x0001FFFF, 0x0001FFFF) },
  2040. { FR_AZ_RX_CFG,
  2041. EFX_OWORD32(0xFFFFFFFE, 0x00017FFF, 0x00000000, 0x00000000) },
  2042. { FR_AZ_TX_CFG,
  2043. EFX_OWORD32(0x7FFF0037, 0x00000000, 0x00000000, 0x00000000) },
  2044. { FR_AZ_TX_RESERVED,
  2045. EFX_OWORD32(0xFFFEFE80, 0x1FFFFFFF, 0x020000FE, 0x007FFFFF) },
  2046. { FR_AB_MAC_CTRL,
  2047. EFX_OWORD32(0xFFFF0000, 0x00000000, 0x00000000, 0x00000000) },
  2048. { FR_AZ_SRM_TX_DC_CFG,
  2049. EFX_OWORD32(0x001FFFFF, 0x00000000, 0x00000000, 0x00000000) },
  2050. { FR_AZ_RX_DC_CFG,
  2051. EFX_OWORD32(0x0000000F, 0x00000000, 0x00000000, 0x00000000) },
  2052. { FR_AZ_RX_DC_PF_WM,
  2053. EFX_OWORD32(0x000003FF, 0x00000000, 0x00000000, 0x00000000) },
  2054. { FR_BZ_DP_CTRL,
  2055. EFX_OWORD32(0x00000FFF, 0x00000000, 0x00000000, 0x00000000) },
  2056. { FR_AB_GM_CFG2,
  2057. EFX_OWORD32(0x00007337, 0x00000000, 0x00000000, 0x00000000) },
  2058. { FR_AB_GMF_CFG0,
  2059. EFX_OWORD32(0x00001F1F, 0x00000000, 0x00000000, 0x00000000) },
  2060. { FR_AB_XM_GLB_CFG,
  2061. EFX_OWORD32(0x00000C68, 0x00000000, 0x00000000, 0x00000000) },
  2062. { FR_AB_XM_TX_CFG,
  2063. EFX_OWORD32(0x00080164, 0x00000000, 0x00000000, 0x00000000) },
  2064. { FR_AB_XM_RX_CFG,
  2065. EFX_OWORD32(0x07100A0C, 0x00000000, 0x00000000, 0x00000000) },
  2066. { FR_AB_XM_RX_PARAM,
  2067. EFX_OWORD32(0x00001FF8, 0x00000000, 0x00000000, 0x00000000) },
  2068. { FR_AB_XM_FC,
  2069. EFX_OWORD32(0xFFFF0001, 0x00000000, 0x00000000, 0x00000000) },
  2070. { FR_AB_XM_ADR_LO,
  2071. EFX_OWORD32(0xFFFFFFFF, 0x00000000, 0x00000000, 0x00000000) },
  2072. { FR_AB_XX_SD_CTL,
  2073. EFX_OWORD32(0x0003FF0F, 0x00000000, 0x00000000, 0x00000000) },
  2074. };
  2075. static bool efx_masked_compare_oword(const efx_oword_t *a, const efx_oword_t *b,
  2076. const efx_oword_t *mask)
  2077. {
  2078. return ((a->u64[0] ^ b->u64[0]) & mask->u64[0]) ||
  2079. ((a->u64[1] ^ b->u64[1]) & mask->u64[1]);
  2080. }
  2081. int efx_nic_test_registers(struct efx_nic *efx,
  2082. const struct efx_nic_register_test *regs,
  2083. size_t n_regs)
  2084. {
  2085. unsigned address = 0, i, j;
  2086. efx_oword_t mask, imask, original, reg, buf;
  2087. /* Falcon should be in loopback to isolate the XMAC from the PHY */
  2088. WARN_ON(!LOOPBACK_INTERNAL(efx));
  2089. for (i = 0; i < n_regs; ++i) {
  2090. address = regs[i].address;
  2091. mask = imask = regs[i].mask;
  2092. EFX_INVERT_OWORD(imask);
  2093. efx_reado(efx, &original, address);
  2094. /* bit sweep on and off */
  2095. for (j = 0; j < 128; j++) {
  2096. if (!EFX_EXTRACT_OWORD32(mask, j, j))
  2097. continue;
  2098. /* Test this testable bit can be set in isolation */
  2099. EFX_AND_OWORD(reg, original, mask);
  2100. EFX_SET_OWORD32(reg, j, j, 1);
  2101. efx_writeo(efx, &reg, address);
  2102. efx_reado(efx, &buf, address);
  2103. if (efx_masked_compare_oword(&reg, &buf, &mask))
  2104. goto fail;
  2105. /* Test this testable bit can be cleared in isolation */
  2106. EFX_OR_OWORD(reg, original, mask);
  2107. EFX_SET_OWORD32(reg, j, j, 0);
  2108. efx_writeo(efx, &reg, address);
  2109. efx_reado(efx, &buf, address);
  2110. if (efx_masked_compare_oword(&reg, &buf, &mask))
  2111. goto fail;
  2112. }
  2113. efx_writeo(efx, &original, address);
  2114. }
  2115. return 0;
  2116. fail:
  2117. EFX_ERR(efx, "wrote "EFX_OWORD_FMT" read "EFX_OWORD_FMT
  2118. " at address 0x%x mask "EFX_OWORD_FMT"\n", EFX_OWORD_VAL(reg),
  2119. EFX_OWORD_VAL(buf), address, EFX_OWORD_VAL(mask));
  2120. return -EIO;
  2121. }
  2122. static int falcon_b0_test_registers(struct efx_nic *efx)
  2123. {
  2124. return efx_nic_test_registers(efx, falcon_b0_register_tests,
  2125. ARRAY_SIZE(falcon_b0_register_tests));
  2126. }
  2127. /**************************************************************************
  2128. *
  2129. * Device reset
  2130. *
  2131. **************************************************************************
  2132. */
  2133. /* Resets NIC to known state. This routine must be called in process
  2134. * context and is allowed to sleep. */
  2135. static int falcon_reset_hw(struct efx_nic *efx, enum reset_type method)
  2136. {
  2137. struct falcon_nic_data *nic_data = efx->nic_data;
  2138. efx_oword_t glb_ctl_reg_ker;
  2139. int rc;
  2140. EFX_LOG(efx, "performing %s hardware reset\n", RESET_TYPE(method));
  2141. /* Initiate device reset */
  2142. if (method == RESET_TYPE_WORLD) {
  2143. rc = pci_save_state(efx->pci_dev);
  2144. if (rc) {
  2145. EFX_ERR(efx, "failed to backup PCI state of primary "
  2146. "function prior to hardware reset\n");
  2147. goto fail1;
  2148. }
  2149. if (efx_nic_is_dual_func(efx)) {
  2150. rc = pci_save_state(nic_data->pci_dev2);
  2151. if (rc) {
  2152. EFX_ERR(efx, "failed to backup PCI state of "
  2153. "secondary function prior to "
  2154. "hardware reset\n");
  2155. goto fail2;
  2156. }
  2157. }
  2158. EFX_POPULATE_OWORD_2(glb_ctl_reg_ker,
  2159. FRF_AB_EXT_PHY_RST_DUR,
  2160. FFE_AB_EXT_PHY_RST_DUR_10240US,
  2161. FRF_AB_SWRST, 1);
  2162. } else {
  2163. EFX_POPULATE_OWORD_7(glb_ctl_reg_ker,
  2164. /* exclude PHY from "invisible" reset */
  2165. FRF_AB_EXT_PHY_RST_CTL,
  2166. method == RESET_TYPE_INVISIBLE,
  2167. /* exclude EEPROM/flash and PCIe */
  2168. FRF_AB_PCIE_CORE_RST_CTL, 1,
  2169. FRF_AB_PCIE_NSTKY_RST_CTL, 1,
  2170. FRF_AB_PCIE_SD_RST_CTL, 1,
  2171. FRF_AB_EE_RST_CTL, 1,
  2172. FRF_AB_EXT_PHY_RST_DUR,
  2173. FFE_AB_EXT_PHY_RST_DUR_10240US,
  2174. FRF_AB_SWRST, 1);
  2175. }
  2176. efx_writeo(efx, &glb_ctl_reg_ker, FR_AB_GLB_CTL);
  2177. EFX_LOG(efx, "waiting for hardware reset\n");
  2178. schedule_timeout_uninterruptible(HZ / 20);
  2179. /* Restore PCI configuration if needed */
  2180. if (method == RESET_TYPE_WORLD) {
  2181. if (efx_nic_is_dual_func(efx)) {
  2182. rc = pci_restore_state(nic_data->pci_dev2);
  2183. if (rc) {
  2184. EFX_ERR(efx, "failed to restore PCI config for "
  2185. "the secondary function\n");
  2186. goto fail3;
  2187. }
  2188. }
  2189. rc = pci_restore_state(efx->pci_dev);
  2190. if (rc) {
  2191. EFX_ERR(efx, "failed to restore PCI config for the "
  2192. "primary function\n");
  2193. goto fail4;
  2194. }
  2195. EFX_LOG(efx, "successfully restored PCI config\n");
  2196. }
  2197. /* Assert that reset complete */
  2198. efx_reado(efx, &glb_ctl_reg_ker, FR_AB_GLB_CTL);
  2199. if (EFX_OWORD_FIELD(glb_ctl_reg_ker, FRF_AB_SWRST) != 0) {
  2200. rc = -ETIMEDOUT;
  2201. EFX_ERR(efx, "timed out waiting for hardware reset\n");
  2202. goto fail5;
  2203. }
  2204. EFX_LOG(efx, "hardware reset complete\n");
  2205. return 0;
  2206. /* pci_save_state() and pci_restore_state() MUST be called in pairs */
  2207. fail2:
  2208. fail3:
  2209. pci_restore_state(efx->pci_dev);
  2210. fail1:
  2211. fail4:
  2212. fail5:
  2213. return rc;
  2214. }
  2215. static void falcon_monitor(struct efx_nic *efx)
  2216. {
  2217. bool link_changed;
  2218. int rc;
  2219. BUG_ON(!mutex_is_locked(&efx->mac_lock));
  2220. rc = falcon_board(efx)->type->monitor(efx);
  2221. if (rc) {
  2222. EFX_ERR(efx, "Board sensor %s; shutting down PHY\n",
  2223. (rc == -ERANGE) ? "reported fault" : "failed");
  2224. efx->phy_mode |= PHY_MODE_LOW_POWER;
  2225. rc = __efx_reconfigure_port(efx);
  2226. WARN_ON(rc);
  2227. }
  2228. if (LOOPBACK_INTERNAL(efx))
  2229. link_changed = falcon_loopback_link_poll(efx);
  2230. else
  2231. link_changed = efx->phy_op->poll(efx);
  2232. if (link_changed) {
  2233. falcon_stop_nic_stats(efx);
  2234. falcon_deconfigure_mac_wrapper(efx);
  2235. falcon_switch_mac(efx);
  2236. rc = efx->mac_op->reconfigure(efx);
  2237. BUG_ON(rc);
  2238. falcon_start_nic_stats(efx);
  2239. efx_link_status_changed(efx);
  2240. }
  2241. if (EFX_IS10G(efx))
  2242. falcon_poll_xmac(efx);
  2243. }
  2244. /* Zeroes out the SRAM contents. This routine must be called in
  2245. * process context and is allowed to sleep.
  2246. */
  2247. static int falcon_reset_sram(struct efx_nic *efx)
  2248. {
  2249. efx_oword_t srm_cfg_reg_ker, gpio_cfg_reg_ker;
  2250. int count;
  2251. /* Set the SRAM wake/sleep GPIO appropriately. */
  2252. efx_reado(efx, &gpio_cfg_reg_ker, FR_AB_GPIO_CTL);
  2253. EFX_SET_OWORD_FIELD(gpio_cfg_reg_ker, FRF_AB_GPIO1_OEN, 1);
  2254. EFX_SET_OWORD_FIELD(gpio_cfg_reg_ker, FRF_AB_GPIO1_OUT, 1);
  2255. efx_writeo(efx, &gpio_cfg_reg_ker, FR_AB_GPIO_CTL);
  2256. /* Initiate SRAM reset */
  2257. EFX_POPULATE_OWORD_2(srm_cfg_reg_ker,
  2258. FRF_AZ_SRM_INIT_EN, 1,
  2259. FRF_AZ_SRM_NB_SZ, 0);
  2260. efx_writeo(efx, &srm_cfg_reg_ker, FR_AZ_SRM_CFG);
  2261. /* Wait for SRAM reset to complete */
  2262. count = 0;
  2263. do {
  2264. EFX_LOG(efx, "waiting for SRAM reset (attempt %d)...\n", count);
  2265. /* SRAM reset is slow; expect around 16ms */
  2266. schedule_timeout_uninterruptible(HZ / 50);
  2267. /* Check for reset complete */
  2268. efx_reado(efx, &srm_cfg_reg_ker, FR_AZ_SRM_CFG);
  2269. if (!EFX_OWORD_FIELD(srm_cfg_reg_ker, FRF_AZ_SRM_INIT_EN)) {
  2270. EFX_LOG(efx, "SRAM reset complete\n");
  2271. return 0;
  2272. }
  2273. } while (++count < 20); /* wait upto 0.4 sec */
  2274. EFX_ERR(efx, "timed out waiting for SRAM reset\n");
  2275. return -ETIMEDOUT;
  2276. }
  2277. static int falcon_spi_device_init(struct efx_nic *efx,
  2278. struct efx_spi_device **spi_device_ret,
  2279. unsigned int device_id, u32 device_type)
  2280. {
  2281. struct efx_spi_device *spi_device;
  2282. if (device_type != 0) {
  2283. spi_device = kzalloc(sizeof(*spi_device), GFP_KERNEL);
  2284. if (!spi_device)
  2285. return -ENOMEM;
  2286. spi_device->device_id = device_id;
  2287. spi_device->size =
  2288. 1 << SPI_DEV_TYPE_FIELD(device_type, SPI_DEV_TYPE_SIZE);
  2289. spi_device->addr_len =
  2290. SPI_DEV_TYPE_FIELD(device_type, SPI_DEV_TYPE_ADDR_LEN);
  2291. spi_device->munge_address = (spi_device->size == 1 << 9 &&
  2292. spi_device->addr_len == 1);
  2293. spi_device->erase_command =
  2294. SPI_DEV_TYPE_FIELD(device_type, SPI_DEV_TYPE_ERASE_CMD);
  2295. spi_device->erase_size =
  2296. 1 << SPI_DEV_TYPE_FIELD(device_type,
  2297. SPI_DEV_TYPE_ERASE_SIZE);
  2298. spi_device->block_size =
  2299. 1 << SPI_DEV_TYPE_FIELD(device_type,
  2300. SPI_DEV_TYPE_BLOCK_SIZE);
  2301. } else {
  2302. spi_device = NULL;
  2303. }
  2304. kfree(*spi_device_ret);
  2305. *spi_device_ret = spi_device;
  2306. return 0;
  2307. }
  2308. static void falcon_remove_spi_devices(struct efx_nic *efx)
  2309. {
  2310. kfree(efx->spi_eeprom);
  2311. efx->spi_eeprom = NULL;
  2312. kfree(efx->spi_flash);
  2313. efx->spi_flash = NULL;
  2314. }
  2315. /* Extract non-volatile configuration */
  2316. static int falcon_probe_nvconfig(struct efx_nic *efx)
  2317. {
  2318. struct falcon_nvconfig *nvconfig;
  2319. int board_rev;
  2320. int rc;
  2321. nvconfig = kmalloc(sizeof(*nvconfig), GFP_KERNEL);
  2322. if (!nvconfig)
  2323. return -ENOMEM;
  2324. rc = falcon_read_nvram(efx, nvconfig);
  2325. if (rc == -EINVAL) {
  2326. EFX_ERR(efx, "NVRAM is invalid therefore using defaults\n");
  2327. efx->phy_type = PHY_TYPE_NONE;
  2328. efx->mdio.prtad = MDIO_PRTAD_NONE;
  2329. board_rev = 0;
  2330. rc = 0;
  2331. } else if (rc) {
  2332. goto fail1;
  2333. } else {
  2334. struct falcon_nvconfig_board_v2 *v2 = &nvconfig->board_v2;
  2335. struct falcon_nvconfig_board_v3 *v3 = &nvconfig->board_v3;
  2336. efx->phy_type = v2->port0_phy_type;
  2337. efx->mdio.prtad = v2->port0_phy_addr;
  2338. board_rev = le16_to_cpu(v2->board_revision);
  2339. if (le16_to_cpu(nvconfig->board_struct_ver) >= 3) {
  2340. rc = falcon_spi_device_init(
  2341. efx, &efx->spi_flash, FFE_AB_SPI_DEVICE_FLASH,
  2342. le32_to_cpu(v3->spi_device_type
  2343. [FFE_AB_SPI_DEVICE_FLASH]));
  2344. if (rc)
  2345. goto fail2;
  2346. rc = falcon_spi_device_init(
  2347. efx, &efx->spi_eeprom, FFE_AB_SPI_DEVICE_EEPROM,
  2348. le32_to_cpu(v3->spi_device_type
  2349. [FFE_AB_SPI_DEVICE_EEPROM]));
  2350. if (rc)
  2351. goto fail2;
  2352. }
  2353. }
  2354. /* Read the MAC addresses */
  2355. memcpy(efx->mac_address, nvconfig->mac_address[0], ETH_ALEN);
  2356. EFX_LOG(efx, "PHY is %d phy_id %d\n", efx->phy_type, efx->mdio.prtad);
  2357. falcon_probe_board(efx, board_rev);
  2358. kfree(nvconfig);
  2359. return 0;
  2360. fail2:
  2361. falcon_remove_spi_devices(efx);
  2362. fail1:
  2363. kfree(nvconfig);
  2364. return rc;
  2365. }
  2366. u32 efx_nic_fpga_ver(struct efx_nic *efx)
  2367. {
  2368. efx_oword_t altera_build;
  2369. efx_reado(efx, &altera_build, FR_AZ_ALTERA_BUILD);
  2370. return EFX_OWORD_FIELD(altera_build, FRF_AZ_ALTERA_BUILD_VER);
  2371. }
  2372. /* Probe all SPI devices on the NIC */
  2373. static void falcon_probe_spi_devices(struct efx_nic *efx)
  2374. {
  2375. efx_oword_t nic_stat, gpio_ctl, ee_vpd_cfg;
  2376. int boot_dev;
  2377. efx_reado(efx, &gpio_ctl, FR_AB_GPIO_CTL);
  2378. efx_reado(efx, &nic_stat, FR_AB_NIC_STAT);
  2379. efx_reado(efx, &ee_vpd_cfg, FR_AB_EE_VPD_CFG0);
  2380. if (EFX_OWORD_FIELD(gpio_ctl, FRF_AB_GPIO3_PWRUP_VALUE)) {
  2381. boot_dev = (EFX_OWORD_FIELD(nic_stat, FRF_AB_SF_PRST) ?
  2382. FFE_AB_SPI_DEVICE_FLASH : FFE_AB_SPI_DEVICE_EEPROM);
  2383. EFX_LOG(efx, "Booted from %s\n",
  2384. boot_dev == FFE_AB_SPI_DEVICE_FLASH ? "flash" : "EEPROM");
  2385. } else {
  2386. /* Disable VPD and set clock dividers to safe
  2387. * values for initial programming. */
  2388. boot_dev = -1;
  2389. EFX_LOG(efx, "Booted from internal ASIC settings;"
  2390. " setting SPI config\n");
  2391. EFX_POPULATE_OWORD_3(ee_vpd_cfg, FRF_AB_EE_VPD_EN, 0,
  2392. /* 125 MHz / 7 ~= 20 MHz */
  2393. FRF_AB_EE_SF_CLOCK_DIV, 7,
  2394. /* 125 MHz / 63 ~= 2 MHz */
  2395. FRF_AB_EE_EE_CLOCK_DIV, 63);
  2396. efx_writeo(efx, &ee_vpd_cfg, FR_AB_EE_VPD_CFG0);
  2397. }
  2398. if (boot_dev == FFE_AB_SPI_DEVICE_FLASH)
  2399. falcon_spi_device_init(efx, &efx->spi_flash,
  2400. FFE_AB_SPI_DEVICE_FLASH,
  2401. default_flash_type);
  2402. if (boot_dev == FFE_AB_SPI_DEVICE_EEPROM)
  2403. falcon_spi_device_init(efx, &efx->spi_eeprom,
  2404. FFE_AB_SPI_DEVICE_EEPROM,
  2405. large_eeprom_type);
  2406. }
  2407. static int falcon_probe_nic(struct efx_nic *efx)
  2408. {
  2409. struct falcon_nic_data *nic_data;
  2410. struct falcon_board *board;
  2411. int rc;
  2412. /* Allocate storage for hardware specific data */
  2413. nic_data = kzalloc(sizeof(*nic_data), GFP_KERNEL);
  2414. if (!nic_data)
  2415. return -ENOMEM;
  2416. efx->nic_data = nic_data;
  2417. rc = -ENODEV;
  2418. if (efx_nic_fpga_ver(efx) != 0) {
  2419. EFX_ERR(efx, "Falcon FPGA not supported\n");
  2420. goto fail1;
  2421. }
  2422. if (efx_nic_rev(efx) <= EFX_REV_FALCON_A1) {
  2423. efx_oword_t nic_stat;
  2424. struct pci_dev *dev;
  2425. u8 pci_rev = efx->pci_dev->revision;
  2426. if ((pci_rev == 0xff) || (pci_rev == 0)) {
  2427. EFX_ERR(efx, "Falcon rev A0 not supported\n");
  2428. goto fail1;
  2429. }
  2430. efx_reado(efx, &nic_stat, FR_AB_NIC_STAT);
  2431. if (EFX_OWORD_FIELD(nic_stat, FRF_AB_STRAP_10G) == 0) {
  2432. EFX_ERR(efx, "Falcon rev A1 1G not supported\n");
  2433. goto fail1;
  2434. }
  2435. if (EFX_OWORD_FIELD(nic_stat, FRF_AA_STRAP_PCIE) == 0) {
  2436. EFX_ERR(efx, "Falcon rev A1 PCI-X not supported\n");
  2437. goto fail1;
  2438. }
  2439. dev = pci_dev_get(efx->pci_dev);
  2440. while ((dev = pci_get_device(EFX_VENDID_SFC, FALCON_A_S_DEVID,
  2441. dev))) {
  2442. if (dev->bus == efx->pci_dev->bus &&
  2443. dev->devfn == efx->pci_dev->devfn + 1) {
  2444. nic_data->pci_dev2 = dev;
  2445. break;
  2446. }
  2447. }
  2448. if (!nic_data->pci_dev2) {
  2449. EFX_ERR(efx, "failed to find secondary function\n");
  2450. rc = -ENODEV;
  2451. goto fail2;
  2452. }
  2453. }
  2454. /* Now we can reset the NIC */
  2455. rc = falcon_reset_hw(efx, RESET_TYPE_ALL);
  2456. if (rc) {
  2457. EFX_ERR(efx, "failed to reset NIC\n");
  2458. goto fail3;
  2459. }
  2460. /* Allocate memory for INT_KER */
  2461. rc = efx_nic_alloc_buffer(efx, &efx->irq_status, sizeof(efx_oword_t));
  2462. if (rc)
  2463. goto fail4;
  2464. BUG_ON(efx->irq_status.dma_addr & 0x0f);
  2465. EFX_LOG(efx, "INT_KER at %llx (virt %p phys %llx)\n",
  2466. (u64)efx->irq_status.dma_addr,
  2467. efx->irq_status.addr, (u64)virt_to_phys(efx->irq_status.addr));
  2468. falcon_probe_spi_devices(efx);
  2469. /* Read in the non-volatile configuration */
  2470. rc = falcon_probe_nvconfig(efx);
  2471. if (rc)
  2472. goto fail5;
  2473. /* Initialise I2C adapter */
  2474. board = falcon_board(efx);
  2475. board->i2c_adap.owner = THIS_MODULE;
  2476. board->i2c_data = falcon_i2c_bit_operations;
  2477. board->i2c_data.data = efx;
  2478. board->i2c_adap.algo_data = &board->i2c_data;
  2479. board->i2c_adap.dev.parent = &efx->pci_dev->dev;
  2480. strlcpy(board->i2c_adap.name, "SFC4000 GPIO",
  2481. sizeof(board->i2c_adap.name));
  2482. rc = i2c_bit_add_bus(&board->i2c_adap);
  2483. if (rc)
  2484. goto fail5;
  2485. rc = falcon_board(efx)->type->init(efx);
  2486. if (rc) {
  2487. EFX_ERR(efx, "failed to initialise board\n");
  2488. goto fail6;
  2489. }
  2490. nic_data->stats_disable_count = 1;
  2491. setup_timer(&nic_data->stats_timer, &falcon_stats_timer_func,
  2492. (unsigned long)efx);
  2493. return 0;
  2494. fail6:
  2495. BUG_ON(i2c_del_adapter(&board->i2c_adap));
  2496. memset(&board->i2c_adap, 0, sizeof(board->i2c_adap));
  2497. fail5:
  2498. falcon_remove_spi_devices(efx);
  2499. efx_nic_free_buffer(efx, &efx->irq_status);
  2500. fail4:
  2501. fail3:
  2502. if (nic_data->pci_dev2) {
  2503. pci_dev_put(nic_data->pci_dev2);
  2504. nic_data->pci_dev2 = NULL;
  2505. }
  2506. fail2:
  2507. fail1:
  2508. kfree(efx->nic_data);
  2509. return rc;
  2510. }
  2511. static void falcon_init_rx_cfg(struct efx_nic *efx)
  2512. {
  2513. /* Prior to Siena the RX DMA engine will split each frame at
  2514. * intervals of RX_USR_BUF_SIZE (32-byte units). We set it to
  2515. * be so large that that never happens. */
  2516. const unsigned huge_buf_size = (3 * 4096) >> 5;
  2517. /* RX control FIFO thresholds (32 entries) */
  2518. const unsigned ctrl_xon_thr = 20;
  2519. const unsigned ctrl_xoff_thr = 25;
  2520. /* RX data FIFO thresholds (256-byte units; size varies) */
  2521. int data_xon_thr = efx_nic_rx_xon_thresh >> 8;
  2522. int data_xoff_thr = efx_nic_rx_xoff_thresh >> 8;
  2523. efx_oword_t reg;
  2524. efx_reado(efx, &reg, FR_AZ_RX_CFG);
  2525. if (efx_nic_rev(efx) <= EFX_REV_FALCON_A1) {
  2526. /* Data FIFO size is 5.5K */
  2527. if (data_xon_thr < 0)
  2528. data_xon_thr = 512 >> 8;
  2529. if (data_xoff_thr < 0)
  2530. data_xoff_thr = 2048 >> 8;
  2531. EFX_SET_OWORD_FIELD(reg, FRF_AA_RX_DESC_PUSH_EN, 0);
  2532. EFX_SET_OWORD_FIELD(reg, FRF_AA_RX_USR_BUF_SIZE,
  2533. huge_buf_size);
  2534. EFX_SET_OWORD_FIELD(reg, FRF_AA_RX_XON_MAC_TH, data_xon_thr);
  2535. EFX_SET_OWORD_FIELD(reg, FRF_AA_RX_XOFF_MAC_TH, data_xoff_thr);
  2536. EFX_SET_OWORD_FIELD(reg, FRF_AA_RX_XON_TX_TH, ctrl_xon_thr);
  2537. EFX_SET_OWORD_FIELD(reg, FRF_AA_RX_XOFF_TX_TH, ctrl_xoff_thr);
  2538. } else {
  2539. /* Data FIFO size is 80K; register fields moved */
  2540. if (data_xon_thr < 0)
  2541. data_xon_thr = 27648 >> 8; /* ~3*max MTU */
  2542. if (data_xoff_thr < 0)
  2543. data_xoff_thr = 54272 >> 8; /* ~80Kb - 3*max MTU */
  2544. EFX_SET_OWORD_FIELD(reg, FRF_BZ_RX_DESC_PUSH_EN, 0);
  2545. EFX_SET_OWORD_FIELD(reg, FRF_BZ_RX_USR_BUF_SIZE,
  2546. huge_buf_size);
  2547. EFX_SET_OWORD_FIELD(reg, FRF_BZ_RX_XON_MAC_TH, data_xon_thr);
  2548. EFX_SET_OWORD_FIELD(reg, FRF_BZ_RX_XOFF_MAC_TH, data_xoff_thr);
  2549. EFX_SET_OWORD_FIELD(reg, FRF_BZ_RX_XON_TX_TH, ctrl_xon_thr);
  2550. EFX_SET_OWORD_FIELD(reg, FRF_BZ_RX_XOFF_TX_TH, ctrl_xoff_thr);
  2551. EFX_SET_OWORD_FIELD(reg, FRF_BZ_RX_INGR_EN, 1);
  2552. }
  2553. /* Always enable XOFF signal from RX FIFO. We enable
  2554. * or disable transmission of pause frames at the MAC. */
  2555. EFX_SET_OWORD_FIELD(reg, FRF_AZ_RX_XOFF_MAC_EN, 1);
  2556. efx_writeo(efx, &reg, FR_AZ_RX_CFG);
  2557. }
  2558. void efx_nic_init_common(struct efx_nic *efx)
  2559. {
  2560. efx_oword_t temp;
  2561. /* Set positions of descriptor caches in SRAM. */
  2562. EFX_POPULATE_OWORD_1(temp, FRF_AZ_SRM_TX_DC_BASE_ADR,
  2563. efx->type->tx_dc_base / 8);
  2564. efx_writeo(efx, &temp, FR_AZ_SRM_TX_DC_CFG);
  2565. EFX_POPULATE_OWORD_1(temp, FRF_AZ_SRM_RX_DC_BASE_ADR,
  2566. efx->type->rx_dc_base / 8);
  2567. efx_writeo(efx, &temp, FR_AZ_SRM_RX_DC_CFG);
  2568. /* Set TX descriptor cache size. */
  2569. BUILD_BUG_ON(TX_DC_ENTRIES != (8 << TX_DC_ENTRIES_ORDER));
  2570. EFX_POPULATE_OWORD_1(temp, FRF_AZ_TX_DC_SIZE, TX_DC_ENTRIES_ORDER);
  2571. efx_writeo(efx, &temp, FR_AZ_TX_DC_CFG);
  2572. /* Set RX descriptor cache size. Set low watermark to size-8, as
  2573. * this allows most efficient prefetching.
  2574. */
  2575. BUILD_BUG_ON(RX_DC_ENTRIES != (8 << RX_DC_ENTRIES_ORDER));
  2576. EFX_POPULATE_OWORD_1(temp, FRF_AZ_RX_DC_SIZE, RX_DC_ENTRIES_ORDER);
  2577. efx_writeo(efx, &temp, FR_AZ_RX_DC_CFG);
  2578. EFX_POPULATE_OWORD_1(temp, FRF_AZ_RX_DC_PF_LWM, RX_DC_ENTRIES - 8);
  2579. efx_writeo(efx, &temp, FR_AZ_RX_DC_PF_WM);
  2580. /* Program INT_KER address */
  2581. EFX_POPULATE_OWORD_2(temp,
  2582. FRF_AZ_NORM_INT_VEC_DIS_KER,
  2583. EFX_INT_MODE_USE_MSI(efx),
  2584. FRF_AZ_INT_ADR_KER, efx->irq_status.dma_addr);
  2585. efx_writeo(efx, &temp, FR_AZ_INT_ADR_KER);
  2586. /* Enable all the genuinely fatal interrupts. (They are still
  2587. * masked by the overall interrupt mask, controlled by
  2588. * falcon_interrupts()).
  2589. *
  2590. * Note: All other fatal interrupts are enabled
  2591. */
  2592. EFX_POPULATE_OWORD_3(temp,
  2593. FRF_AZ_ILL_ADR_INT_KER_EN, 1,
  2594. FRF_AZ_RBUF_OWN_INT_KER_EN, 1,
  2595. FRF_AZ_TBUF_OWN_INT_KER_EN, 1);
  2596. EFX_INVERT_OWORD(temp);
  2597. efx_writeo(efx, &temp, FR_AZ_FATAL_INTR_KER);
  2598. efx_setup_rss_indir_table(efx);
  2599. /* Disable the ugly timer-based TX DMA backoff and allow TX DMA to be
  2600. * controlled by the RX FIFO fill level. Set arbitration to one pkt/Q.
  2601. */
  2602. efx_reado(efx, &temp, FR_AZ_TX_RESERVED);
  2603. EFX_SET_OWORD_FIELD(temp, FRF_AZ_TX_RX_SPACER, 0xfe);
  2604. EFX_SET_OWORD_FIELD(temp, FRF_AZ_TX_RX_SPACER_EN, 1);
  2605. EFX_SET_OWORD_FIELD(temp, FRF_AZ_TX_ONE_PKT_PER_Q, 1);
  2606. EFX_SET_OWORD_FIELD(temp, FRF_AZ_TX_PUSH_EN, 0);
  2607. EFX_SET_OWORD_FIELD(temp, FRF_AZ_TX_DIS_NON_IP_EV, 1);
  2608. /* Enable SW_EV to inherit in char driver - assume harmless here */
  2609. EFX_SET_OWORD_FIELD(temp, FRF_AZ_TX_SOFT_EVT_EN, 1);
  2610. /* Prefetch threshold 2 => fetch when descriptor cache half empty */
  2611. EFX_SET_OWORD_FIELD(temp, FRF_AZ_TX_PREF_THRESHOLD, 2);
  2612. /* Squash TX of packets of 16 bytes or less */
  2613. if (efx_nic_rev(efx) >= EFX_REV_FALCON_B0)
  2614. EFX_SET_OWORD_FIELD(temp, FRF_BZ_TX_FLUSH_MIN_LEN_EN, 1);
  2615. efx_writeo(efx, &temp, FR_AZ_TX_RESERVED);
  2616. }
  2617. /* This call performs hardware-specific global initialisation, such as
  2618. * defining the descriptor cache sizes and number of RSS channels.
  2619. * It does not set up any buffers, descriptor rings or event queues.
  2620. */
  2621. static int falcon_init_nic(struct efx_nic *efx)
  2622. {
  2623. efx_oword_t temp;
  2624. int rc;
  2625. /* Use on-chip SRAM */
  2626. efx_reado(efx, &temp, FR_AB_NIC_STAT);
  2627. EFX_SET_OWORD_FIELD(temp, FRF_AB_ONCHIP_SRAM, 1);
  2628. efx_writeo(efx, &temp, FR_AB_NIC_STAT);
  2629. /* Set the source of the GMAC clock */
  2630. if (efx_nic_rev(efx) == EFX_REV_FALCON_B0) {
  2631. efx_reado(efx, &temp, FR_AB_GPIO_CTL);
  2632. EFX_SET_OWORD_FIELD(temp, FRF_AB_USE_NIC_CLK, true);
  2633. efx_writeo(efx, &temp, FR_AB_GPIO_CTL);
  2634. }
  2635. /* Select the correct MAC */
  2636. falcon_clock_mac(efx);
  2637. rc = falcon_reset_sram(efx);
  2638. if (rc)
  2639. return rc;
  2640. /* Clear the parity enables on the TX data fifos as
  2641. * they produce false parity errors because of timing issues
  2642. */
  2643. if (EFX_WORKAROUND_5129(efx)) {
  2644. efx_reado(efx, &temp, FR_AZ_CSR_SPARE);
  2645. EFX_SET_OWORD_FIELD(temp, FRF_AB_MEM_PERR_EN_TX_DATA, 0);
  2646. efx_writeo(efx, &temp, FR_AZ_CSR_SPARE);
  2647. }
  2648. if (EFX_WORKAROUND_7244(efx)) {
  2649. efx_reado(efx, &temp, FR_BZ_RX_FILTER_CTL);
  2650. EFX_SET_OWORD_FIELD(temp, FRF_BZ_UDP_FULL_SRCH_LIMIT, 8);
  2651. EFX_SET_OWORD_FIELD(temp, FRF_BZ_UDP_WILD_SRCH_LIMIT, 8);
  2652. EFX_SET_OWORD_FIELD(temp, FRF_BZ_TCP_FULL_SRCH_LIMIT, 8);
  2653. EFX_SET_OWORD_FIELD(temp, FRF_BZ_TCP_WILD_SRCH_LIMIT, 8);
  2654. efx_writeo(efx, &temp, FR_BZ_RX_FILTER_CTL);
  2655. }
  2656. /* XXX This is documented only for Falcon A0/A1 */
  2657. /* Setup RX. Wait for descriptor is broken and must
  2658. * be disabled. RXDP recovery shouldn't be needed, but is.
  2659. */
  2660. efx_reado(efx, &temp, FR_AA_RX_SELF_RST);
  2661. EFX_SET_OWORD_FIELD(temp, FRF_AA_RX_NODESC_WAIT_DIS, 1);
  2662. EFX_SET_OWORD_FIELD(temp, FRF_AA_RX_SELF_RST_EN, 1);
  2663. if (EFX_WORKAROUND_5583(efx))
  2664. EFX_SET_OWORD_FIELD(temp, FRF_AA_RX_ISCSI_DIS, 1);
  2665. efx_writeo(efx, &temp, FR_AA_RX_SELF_RST);
  2666. /* Do not enable TX_NO_EOP_DISC_EN, since it limits packets to 16
  2667. * descriptors (which is bad).
  2668. */
  2669. efx_reado(efx, &temp, FR_AZ_TX_CFG);
  2670. EFX_SET_OWORD_FIELD(temp, FRF_AZ_TX_NO_EOP_DISC_EN, 0);
  2671. efx_writeo(efx, &temp, FR_AZ_TX_CFG);
  2672. falcon_init_rx_cfg(efx);
  2673. /* Set destination of both TX and RX Flush events */
  2674. if (efx_nic_rev(efx) >= EFX_REV_FALCON_B0) {
  2675. EFX_POPULATE_OWORD_1(temp, FRF_BZ_FLS_EVQ_ID, 0);
  2676. efx_writeo(efx, &temp, FR_BZ_DP_CTRL);
  2677. }
  2678. efx_nic_init_common(efx);
  2679. return 0;
  2680. }
  2681. static void falcon_remove_nic(struct efx_nic *efx)
  2682. {
  2683. struct falcon_nic_data *nic_data = efx->nic_data;
  2684. struct falcon_board *board = falcon_board(efx);
  2685. int rc;
  2686. board->type->fini(efx);
  2687. /* Remove I2C adapter and clear it in preparation for a retry */
  2688. rc = i2c_del_adapter(&board->i2c_adap);
  2689. BUG_ON(rc);
  2690. memset(&board->i2c_adap, 0, sizeof(board->i2c_adap));
  2691. falcon_remove_spi_devices(efx);
  2692. efx_nic_free_buffer(efx, &efx->irq_status);
  2693. falcon_reset_hw(efx, RESET_TYPE_ALL);
  2694. /* Release the second function after the reset */
  2695. if (nic_data->pci_dev2) {
  2696. pci_dev_put(nic_data->pci_dev2);
  2697. nic_data->pci_dev2 = NULL;
  2698. }
  2699. /* Tear down the private nic state */
  2700. kfree(efx->nic_data);
  2701. efx->nic_data = NULL;
  2702. }
  2703. static void falcon_update_nic_stats(struct efx_nic *efx)
  2704. {
  2705. struct falcon_nic_data *nic_data = efx->nic_data;
  2706. efx_oword_t cnt;
  2707. if (nic_data->stats_disable_count)
  2708. return;
  2709. efx_reado(efx, &cnt, FR_AZ_RX_NODESC_DROP);
  2710. efx->n_rx_nodesc_drop_cnt +=
  2711. EFX_OWORD_FIELD(cnt, FRF_AB_RX_NODESC_DROP_CNT);
  2712. if (nic_data->stats_pending &&
  2713. *nic_data->stats_dma_done == FALCON_STATS_DONE) {
  2714. nic_data->stats_pending = false;
  2715. rmb(); /* read the done flag before the stats */
  2716. efx->mac_op->update_stats(efx);
  2717. }
  2718. }
  2719. void falcon_start_nic_stats(struct efx_nic *efx)
  2720. {
  2721. struct falcon_nic_data *nic_data = efx->nic_data;
  2722. spin_lock_bh(&efx->stats_lock);
  2723. if (--nic_data->stats_disable_count == 0)
  2724. falcon_stats_request(efx);
  2725. spin_unlock_bh(&efx->stats_lock);
  2726. }
  2727. void falcon_stop_nic_stats(struct efx_nic *efx)
  2728. {
  2729. struct falcon_nic_data *nic_data = efx->nic_data;
  2730. int i;
  2731. might_sleep();
  2732. spin_lock_bh(&efx->stats_lock);
  2733. ++nic_data->stats_disable_count;
  2734. spin_unlock_bh(&efx->stats_lock);
  2735. del_timer_sync(&nic_data->stats_timer);
  2736. /* Wait enough time for the most recent transfer to
  2737. * complete. */
  2738. for (i = 0; i < 4 && nic_data->stats_pending; i++) {
  2739. if (*nic_data->stats_dma_done == FALCON_STATS_DONE)
  2740. break;
  2741. msleep(1);
  2742. }
  2743. spin_lock_bh(&efx->stats_lock);
  2744. falcon_stats_complete(efx);
  2745. spin_unlock_bh(&efx->stats_lock);
  2746. }
  2747. static void falcon_set_id_led(struct efx_nic *efx, enum efx_led_mode mode)
  2748. {
  2749. falcon_board(efx)->type->set_id_led(efx, mode);
  2750. }
  2751. /**************************************************************************
  2752. *
  2753. * Wake on LAN
  2754. *
  2755. **************************************************************************
  2756. */
  2757. static void falcon_get_wol(struct efx_nic *efx, struct ethtool_wolinfo *wol)
  2758. {
  2759. wol->supported = 0;
  2760. wol->wolopts = 0;
  2761. memset(&wol->sopass, 0, sizeof(wol->sopass));
  2762. }
  2763. static int falcon_set_wol(struct efx_nic *efx, u32 type)
  2764. {
  2765. if (type != 0)
  2766. return -EINVAL;
  2767. return 0;
  2768. }
  2769. /**************************************************************************
  2770. *
  2771. * Revision-dependent attributes used by efx.c
  2772. *
  2773. **************************************************************************
  2774. */
  2775. struct efx_nic_type falcon_a1_nic_type = {
  2776. .probe = falcon_probe_nic,
  2777. .remove = falcon_remove_nic,
  2778. .init = falcon_init_nic,
  2779. .fini = efx_port_dummy_op_void,
  2780. .monitor = falcon_monitor,
  2781. .reset = falcon_reset_hw,
  2782. .probe_port = falcon_probe_port,
  2783. .remove_port = falcon_remove_port,
  2784. .prepare_flush = falcon_prepare_flush,
  2785. .update_stats = falcon_update_nic_stats,
  2786. .start_stats = falcon_start_nic_stats,
  2787. .stop_stats = falcon_stop_nic_stats,
  2788. .set_id_led = falcon_set_id_led,
  2789. .push_irq_moderation = falcon_push_irq_moderation,
  2790. .push_multicast_hash = falcon_push_multicast_hash,
  2791. .reconfigure_port = falcon_reconfigure_port,
  2792. .get_wol = falcon_get_wol,
  2793. .set_wol = falcon_set_wol,
  2794. .resume_wol = efx_port_dummy_op_void,
  2795. .test_nvram = falcon_test_nvram,
  2796. .default_mac_ops = &falcon_xmac_operations,
  2797. .revision = EFX_REV_FALCON_A1,
  2798. .mem_map_size = 0x20000,
  2799. .txd_ptr_tbl_base = FR_AA_TX_DESC_PTR_TBL_KER,
  2800. .rxd_ptr_tbl_base = FR_AA_RX_DESC_PTR_TBL_KER,
  2801. .buf_tbl_base = FR_AA_BUF_FULL_TBL_KER,
  2802. .evq_ptr_tbl_base = FR_AA_EVQ_PTR_TBL_KER,
  2803. .evq_rptr_tbl_base = FR_AA_EVQ_RPTR_KER,
  2804. .max_dma_mask = DMA_BIT_MASK(FSF_AZ_TX_KER_BUF_ADDR_WIDTH),
  2805. .rx_buffer_padding = 0x24,
  2806. .max_interrupt_mode = EFX_INT_MODE_MSI,
  2807. .phys_addr_channels = 4,
  2808. .tx_dc_base = 0x130000,
  2809. .rx_dc_base = 0x100000,
  2810. .reset_world_flags = ETH_RESET_IRQ,
  2811. };
  2812. struct efx_nic_type falcon_b0_nic_type = {
  2813. .probe = falcon_probe_nic,
  2814. .remove = falcon_remove_nic,
  2815. .init = falcon_init_nic,
  2816. .fini = efx_port_dummy_op_void,
  2817. .monitor = falcon_monitor,
  2818. .reset = falcon_reset_hw,
  2819. .probe_port = falcon_probe_port,
  2820. .remove_port = falcon_remove_port,
  2821. .prepare_flush = falcon_prepare_flush,
  2822. .update_stats = falcon_update_nic_stats,
  2823. .start_stats = falcon_start_nic_stats,
  2824. .stop_stats = falcon_stop_nic_stats,
  2825. .set_id_led = falcon_set_id_led,
  2826. .push_irq_moderation = falcon_push_irq_moderation,
  2827. .push_multicast_hash = falcon_push_multicast_hash,
  2828. .reconfigure_port = falcon_reconfigure_port,
  2829. .get_wol = falcon_get_wol,
  2830. .set_wol = falcon_set_wol,
  2831. .resume_wol = efx_port_dummy_op_void,
  2832. .test_registers = falcon_b0_test_registers,
  2833. .test_nvram = falcon_test_nvram,
  2834. .default_mac_ops = &falcon_xmac_operations,
  2835. .revision = EFX_REV_FALCON_B0,
  2836. /* Map everything up to and including the RSS indirection
  2837. * table. Don't map MSI-X table, MSI-X PBA since Linux
  2838. * requires that they not be mapped. */
  2839. .mem_map_size = (FR_BZ_RX_INDIRECTION_TBL +
  2840. FR_BZ_RX_INDIRECTION_TBL_STEP *
  2841. FR_BZ_RX_INDIRECTION_TBL_ROWS),
  2842. .txd_ptr_tbl_base = FR_BZ_TX_DESC_PTR_TBL,
  2843. .rxd_ptr_tbl_base = FR_BZ_RX_DESC_PTR_TBL,
  2844. .buf_tbl_base = FR_BZ_BUF_FULL_TBL,
  2845. .evq_ptr_tbl_base = FR_BZ_EVQ_PTR_TBL,
  2846. .evq_rptr_tbl_base = FR_BZ_EVQ_RPTR,
  2847. .max_dma_mask = DMA_BIT_MASK(FSF_AZ_TX_KER_BUF_ADDR_WIDTH),
  2848. .rx_buffer_padding = 0,
  2849. .max_interrupt_mode = EFX_INT_MODE_MSIX,
  2850. .phys_addr_channels = 32, /* Hardware limit is 64, but the legacy
  2851. * interrupt handler only supports 32
  2852. * channels */
  2853. .tx_dc_base = 0x130000,
  2854. .rx_dc_base = 0x100000,
  2855. .reset_world_flags = ETH_RESET_IRQ,
  2856. };