falcon.c 87 KB

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