falcon.c 87 KB

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