blob: f42fc60d1df4a7cab84130e09dc0542488f1e1f7 [file] [log] [blame]
Ben Hutchings8ceee662008-04-27 12:55:59 +01001/****************************************************************************
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
11#include <linux/bitops.h>
12#include <linux/delay.h>
13#include <linux/pci.h>
14#include <linux/module.h>
15#include <linux/seq_file.h>
Ben Hutchings37b5a602008-05-30 22:27:04 +010016#include <linux/i2c.h>
17#include <linux/i2c-algo-bit.h>
Ben Hutchingsf31a45d2008-12-12 21:43:33 -080018#include <linux/mii.h>
Ben Hutchings8ceee662008-04-27 12:55:59 +010019#include "net_driver.h"
20#include "bitfield.h"
21#include "efx.h"
22#include "mac.h"
Ben Hutchings8ceee662008-04-27 12:55:59 +010023#include "spi.h"
24#include "falcon.h"
25#include "falcon_hwdefs.h"
26#include "falcon_io.h"
27#include "mdio_10g.h"
28#include "phy.h"
29#include "boards.h"
30#include "workarounds.h"
31
32/* Falcon hardware control.
33 * Falcon is the internal codename for the SFC4000 controller that is
34 * present in SFE400X evaluation boards
35 */
36
37/**
38 * struct falcon_nic_data - Falcon NIC state
39 * @next_buffer_table: First available buffer table id
40 * @pci_dev2: The secondary PCI device if present
Ben Hutchings37b5a602008-05-30 22:27:04 +010041 * @i2c_data: Operations and state for I2C bit-bashing algorithm
Ben Hutchings2c3c3d02009-03-04 10:01:57 +000042 * @int_error_count: Number of internal errors seen recently
43 * @int_error_expire: Time at which error count will be expired
Ben Hutchings8ceee662008-04-27 12:55:59 +010044 */
45struct falcon_nic_data {
46 unsigned next_buffer_table;
47 struct pci_dev *pci_dev2;
Ben Hutchings37b5a602008-05-30 22:27:04 +010048 struct i2c_algo_bit_data i2c_data;
Ben Hutchings2c3c3d02009-03-04 10:01:57 +000049
50 unsigned int_error_count;
51 unsigned long int_error_expire;
Ben Hutchings8ceee662008-04-27 12:55:59 +010052};
53
54/**************************************************************************
55 *
56 * Configurable values
57 *
58 **************************************************************************
59 */
60
61static int disable_dma_stats;
62
63/* This is set to 16 for a good reason. In summary, if larger than
64 * 16, the descriptor cache holds more than a default socket
65 * buffer's worth of packets (for UDP we can only have at most one
66 * socket buffer's worth outstanding). This combined with the fact
67 * that we only get 1 TX event per descriptor cache means the NIC
68 * goes idle.
69 */
70#define TX_DC_ENTRIES 16
71#define TX_DC_ENTRIES_ORDER 0
72#define TX_DC_BASE 0x130000
73
74#define RX_DC_ENTRIES 64
75#define RX_DC_ENTRIES_ORDER 2
76#define RX_DC_BASE 0x100000
77
Ben Hutchings2f7f5732008-12-12 21:34:25 -080078static const unsigned int
79/* "Large" EEPROM device: Atmel AT25640 or similar
80 * 8 KB, 16-bit address, 32 B write block */
81large_eeprom_type = ((13 << SPI_DEV_TYPE_SIZE_LBN)
82 | (2 << SPI_DEV_TYPE_ADDR_LEN_LBN)
83 | (5 << SPI_DEV_TYPE_BLOCK_SIZE_LBN)),
84/* Default flash device: Atmel AT25F1024
85 * 128 KB, 24-bit address, 32 KB erase block, 256 B write block */
86default_flash_type = ((17 << SPI_DEV_TYPE_SIZE_LBN)
87 | (3 << SPI_DEV_TYPE_ADDR_LEN_LBN)
88 | (0x52 << SPI_DEV_TYPE_ERASE_CMD_LBN)
89 | (15 << SPI_DEV_TYPE_ERASE_SIZE_LBN)
90 | (8 << SPI_DEV_TYPE_BLOCK_SIZE_LBN));
91
Ben Hutchings8ceee662008-04-27 12:55:59 +010092/* RX FIFO XOFF watermark
93 *
94 * When the amount of the RX FIFO increases used increases past this
95 * watermark send XOFF. Only used if RX flow control is enabled (ethtool -A)
96 * This also has an effect on RX/TX arbitration
97 */
98static int rx_xoff_thresh_bytes = -1;
99module_param(rx_xoff_thresh_bytes, int, 0644);
100MODULE_PARM_DESC(rx_xoff_thresh_bytes, "RX fifo XOFF threshold");
101
102/* RX FIFO XON watermark
103 *
104 * When the amount of the RX FIFO used decreases below this
105 * watermark send XON. Only used if TX flow control is enabled (ethtool -A)
106 * This also has an effect on RX/TX arbitration
107 */
108static int rx_xon_thresh_bytes = -1;
109module_param(rx_xon_thresh_bytes, int, 0644);
110MODULE_PARM_DESC(rx_xon_thresh_bytes, "RX fifo XON threshold");
111
112/* TX descriptor ring size - min 512 max 4k */
113#define FALCON_TXD_RING_ORDER TX_DESCQ_SIZE_1K
114#define FALCON_TXD_RING_SIZE 1024
115#define FALCON_TXD_RING_MASK (FALCON_TXD_RING_SIZE - 1)
116
117/* RX descriptor ring size - min 512 max 4k */
118#define FALCON_RXD_RING_ORDER RX_DESCQ_SIZE_1K
119#define FALCON_RXD_RING_SIZE 1024
120#define FALCON_RXD_RING_MASK (FALCON_RXD_RING_SIZE - 1)
121
122/* Event queue size - max 32k */
123#define FALCON_EVQ_ORDER EVQ_SIZE_4K
124#define FALCON_EVQ_SIZE 4096
125#define FALCON_EVQ_MASK (FALCON_EVQ_SIZE - 1)
126
Ben Hutchings2c3c3d02009-03-04 10:01:57 +0000127/* If FALCON_MAX_INT_ERRORS internal errors occur within
128 * FALCON_INT_ERROR_EXPIRE seconds, we consider the NIC broken and
129 * disable it.
130 */
131#define FALCON_INT_ERROR_EXPIRE 3600
132#define FALCON_MAX_INT_ERRORS 5
Ben Hutchings8ceee662008-04-27 12:55:59 +0100133
Ben Hutchings6bc5d3a2008-09-01 12:49:37 +0100134/* We poll for events every FLUSH_INTERVAL ms, and check FLUSH_POLL_COUNT times
135 */
136#define FALCON_FLUSH_INTERVAL 10
137#define FALCON_FLUSH_POLL_COUNT 100
Ben Hutchings8ceee662008-04-27 12:55:59 +0100138
139/**************************************************************************
140 *
141 * Falcon constants
142 *
143 **************************************************************************
144 */
145
Ben Hutchings9bbd7d92008-05-16 21:18:48 +0100146/* DMA address mask */
147#define FALCON_DMA_MASK DMA_BIT_MASK(46)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100148
149/* TX DMA length mask (13-bit) */
150#define FALCON_TX_DMA_MASK (4096 - 1)
151
152/* Size and alignment of special buffers (4KB) */
153#define FALCON_BUF_SIZE 4096
154
155/* Dummy SRAM size code */
156#define SRM_NB_BSZ_ONCHIP_ONLY (-1)
157
Ben Hutchings8ceee662008-04-27 12:55:59 +0100158#define FALCON_IS_DUAL_FUNC(efx) \
Ben Hutchings55668612008-05-16 21:16:10 +0100159 (falcon_rev(efx) < FALCON_REV_B0)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100160
161/**************************************************************************
162 *
163 * Falcon hardware access
164 *
165 **************************************************************************/
166
167/* Read the current event from the event queue */
168static inline efx_qword_t *falcon_event(struct efx_channel *channel,
169 unsigned int index)
170{
171 return (((efx_qword_t *) (channel->eventq.addr)) + index);
172}
173
174/* See if an event is present
175 *
176 * We check both the high and low dword of the event for all ones. We
177 * wrote all ones when we cleared the event, and no valid event can
178 * have all ones in either its high or low dwords. This approach is
179 * robust against reordering.
180 *
181 * Note that using a single 64-bit comparison is incorrect; even
182 * though the CPU read will be atomic, the DMA write may not be.
183 */
184static inline int falcon_event_present(efx_qword_t *event)
185{
186 return (!(EFX_DWORD_IS_ALL_ONES(event->dword[0]) |
187 EFX_DWORD_IS_ALL_ONES(event->dword[1])));
188}
189
190/**************************************************************************
191 *
192 * I2C bus - this is a bit-bashing interface using GPIO pins
193 * Note that it uses the output enables to tristate the outputs
194 * SDA is the data pin and SCL is the clock
195 *
196 **************************************************************************
197 */
Ben Hutchings37b5a602008-05-30 22:27:04 +0100198static void falcon_setsda(void *data, int state)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100199{
Ben Hutchings37b5a602008-05-30 22:27:04 +0100200 struct efx_nic *efx = (struct efx_nic *)data;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100201 efx_oword_t reg;
202
Ben Hutchings37b5a602008-05-30 22:27:04 +0100203 falcon_read(efx, &reg, GPIO_CTL_REG_KER);
204 EFX_SET_OWORD_FIELD(reg, GPIO3_OEN, !state);
205 falcon_write(efx, &reg, GPIO_CTL_REG_KER);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100206}
207
Ben Hutchings37b5a602008-05-30 22:27:04 +0100208static void falcon_setscl(void *data, int state)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100209{
Ben Hutchings37b5a602008-05-30 22:27:04 +0100210 struct efx_nic *efx = (struct efx_nic *)data;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100211 efx_oword_t reg;
212
Ben Hutchings37b5a602008-05-30 22:27:04 +0100213 falcon_read(efx, &reg, GPIO_CTL_REG_KER);
214 EFX_SET_OWORD_FIELD(reg, GPIO0_OEN, !state);
215 falcon_write(efx, &reg, GPIO_CTL_REG_KER);
216}
217
218static int falcon_getsda(void *data)
219{
220 struct efx_nic *efx = (struct efx_nic *)data;
221 efx_oword_t reg;
222
223 falcon_read(efx, &reg, GPIO_CTL_REG_KER);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100224 return EFX_OWORD_FIELD(reg, GPIO3_IN);
225}
226
Ben Hutchings37b5a602008-05-30 22:27:04 +0100227static int falcon_getscl(void *data)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100228{
Ben Hutchings37b5a602008-05-30 22:27:04 +0100229 struct efx_nic *efx = (struct efx_nic *)data;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100230 efx_oword_t reg;
231
Ben Hutchings37b5a602008-05-30 22:27:04 +0100232 falcon_read(efx, &reg, GPIO_CTL_REG_KER);
233 return EFX_OWORD_FIELD(reg, GPIO0_IN);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100234}
235
Ben Hutchings37b5a602008-05-30 22:27:04 +0100236static struct i2c_algo_bit_data falcon_i2c_bit_operations = {
237 .setsda = falcon_setsda,
238 .setscl = falcon_setscl,
Ben Hutchings8ceee662008-04-27 12:55:59 +0100239 .getsda = falcon_getsda,
240 .getscl = falcon_getscl,
Ben Hutchings62c78322008-05-30 22:27:46 +0100241 .udelay = 5,
Ben Hutchings9dadae62008-07-18 18:59:12 +0100242 /* Wait up to 50 ms for slave to let us pull SCL high */
243 .timeout = DIV_ROUND_UP(HZ, 20),
Ben Hutchings8ceee662008-04-27 12:55:59 +0100244};
245
246/**************************************************************************
247 *
248 * Falcon special buffer handling
249 * Special buffers are used for event queues and the TX and RX
250 * descriptor rings.
251 *
252 *************************************************************************/
253
254/*
255 * Initialise a Falcon special buffer
256 *
257 * This will define a buffer (previously allocated via
258 * falcon_alloc_special_buffer()) in Falcon's buffer table, allowing
259 * it to be used for event queues, descriptor rings etc.
260 */
Ben Hutchingsbc3c90a2008-09-01 12:48:46 +0100261static void
Ben Hutchings8ceee662008-04-27 12:55:59 +0100262falcon_init_special_buffer(struct efx_nic *efx,
263 struct efx_special_buffer *buffer)
264{
265 efx_qword_t buf_desc;
266 int index;
267 dma_addr_t dma_addr;
268 int i;
269
270 EFX_BUG_ON_PARANOID(!buffer->addr);
271
272 /* Write buffer descriptors to NIC */
273 for (i = 0; i < buffer->entries; i++) {
274 index = buffer->index + i;
275 dma_addr = buffer->dma_addr + (i * 4096);
276 EFX_LOG(efx, "mapping special buffer %d at %llx\n",
277 index, (unsigned long long)dma_addr);
278 EFX_POPULATE_QWORD_4(buf_desc,
279 IP_DAT_BUF_SIZE, IP_DAT_BUF_SIZE_4K,
280 BUF_ADR_REGION, 0,
281 BUF_ADR_FBUF, (dma_addr >> 12),
282 BUF_OWNER_ID_FBUF, 0);
283 falcon_write_sram(efx, &buf_desc, index);
284 }
Ben Hutchings8ceee662008-04-27 12:55:59 +0100285}
286
287/* Unmaps a buffer from Falcon and clears the buffer table entries */
288static void
289falcon_fini_special_buffer(struct efx_nic *efx,
290 struct efx_special_buffer *buffer)
291{
292 efx_oword_t buf_tbl_upd;
293 unsigned int start = buffer->index;
294 unsigned int end = (buffer->index + buffer->entries - 1);
295
296 if (!buffer->entries)
297 return;
298
299 EFX_LOG(efx, "unmapping special buffers %d-%d\n",
300 buffer->index, buffer->index + buffer->entries - 1);
301
302 EFX_POPULATE_OWORD_4(buf_tbl_upd,
303 BUF_UPD_CMD, 0,
304 BUF_CLR_CMD, 1,
305 BUF_CLR_END_ID, end,
306 BUF_CLR_START_ID, start);
307 falcon_write(efx, &buf_tbl_upd, BUF_TBL_UPD_REG_KER);
308}
309
310/*
311 * Allocate a new Falcon special buffer
312 *
313 * This allocates memory for a new buffer, clears it and allocates a
314 * new buffer ID range. It does not write into Falcon's buffer table.
315 *
316 * This call will allocate 4KB buffers, since Falcon can't use 8KB
317 * buffers for event queues and descriptor rings.
318 */
319static int falcon_alloc_special_buffer(struct efx_nic *efx,
320 struct efx_special_buffer *buffer,
321 unsigned int len)
322{
323 struct falcon_nic_data *nic_data = efx->nic_data;
324
325 len = ALIGN(len, FALCON_BUF_SIZE);
326
327 buffer->addr = pci_alloc_consistent(efx->pci_dev, len,
328 &buffer->dma_addr);
329 if (!buffer->addr)
330 return -ENOMEM;
331 buffer->len = len;
332 buffer->entries = len / FALCON_BUF_SIZE;
333 BUG_ON(buffer->dma_addr & (FALCON_BUF_SIZE - 1));
334
335 /* All zeros is a potentially valid event so memset to 0xff */
336 memset(buffer->addr, 0xff, len);
337
338 /* Select new buffer ID */
339 buffer->index = nic_data->next_buffer_table;
340 nic_data->next_buffer_table += buffer->entries;
341
342 EFX_LOG(efx, "allocating special buffers %d-%d at %llx+%x "
343 "(virt %p phys %lx)\n", buffer->index,
344 buffer->index + buffer->entries - 1,
345 (unsigned long long)buffer->dma_addr, len,
346 buffer->addr, virt_to_phys(buffer->addr));
347
348 return 0;
349}
350
351static void falcon_free_special_buffer(struct efx_nic *efx,
352 struct efx_special_buffer *buffer)
353{
354 if (!buffer->addr)
355 return;
356
357 EFX_LOG(efx, "deallocating special buffers %d-%d at %llx+%x "
358 "(virt %p phys %lx)\n", buffer->index,
359 buffer->index + buffer->entries - 1,
360 (unsigned long long)buffer->dma_addr, buffer->len,
361 buffer->addr, virt_to_phys(buffer->addr));
362
363 pci_free_consistent(efx->pci_dev, buffer->len, buffer->addr,
364 buffer->dma_addr);
365 buffer->addr = NULL;
366 buffer->entries = 0;
367}
368
369/**************************************************************************
370 *
371 * Falcon generic buffer handling
372 * These buffers are used for interrupt status and MAC stats
373 *
374 **************************************************************************/
375
376static int falcon_alloc_buffer(struct efx_nic *efx,
377 struct efx_buffer *buffer, unsigned int len)
378{
379 buffer->addr = pci_alloc_consistent(efx->pci_dev, len,
380 &buffer->dma_addr);
381 if (!buffer->addr)
382 return -ENOMEM;
383 buffer->len = len;
384 memset(buffer->addr, 0, len);
385 return 0;
386}
387
388static void falcon_free_buffer(struct efx_nic *efx, struct efx_buffer *buffer)
389{
390 if (buffer->addr) {
391 pci_free_consistent(efx->pci_dev, buffer->len,
392 buffer->addr, buffer->dma_addr);
393 buffer->addr = NULL;
394 }
395}
396
397/**************************************************************************
398 *
399 * Falcon TX path
400 *
401 **************************************************************************/
402
403/* Returns a pointer to the specified transmit descriptor in the TX
404 * descriptor queue belonging to the specified channel.
405 */
406static inline efx_qword_t *falcon_tx_desc(struct efx_tx_queue *tx_queue,
407 unsigned int index)
408{
409 return (((efx_qword_t *) (tx_queue->txd.addr)) + index);
410}
411
412/* This writes to the TX_DESC_WPTR; write pointer for TX descriptor ring */
413static inline void falcon_notify_tx_desc(struct efx_tx_queue *tx_queue)
414{
415 unsigned write_ptr;
416 efx_dword_t reg;
417
418 write_ptr = tx_queue->write_count & FALCON_TXD_RING_MASK;
419 EFX_POPULATE_DWORD_1(reg, TX_DESC_WPTR_DWORD, write_ptr);
420 falcon_writel_page(tx_queue->efx, &reg,
421 TX_DESC_UPD_REG_KER_DWORD, tx_queue->queue);
422}
423
424
425/* For each entry inserted into the software descriptor ring, create a
426 * descriptor in the hardware TX descriptor ring (in host memory), and
427 * write a doorbell.
428 */
429void falcon_push_buffers(struct efx_tx_queue *tx_queue)
430{
431
432 struct efx_tx_buffer *buffer;
433 efx_qword_t *txd;
434 unsigned write_ptr;
435
436 BUG_ON(tx_queue->write_count == tx_queue->insert_count);
437
438 do {
439 write_ptr = tx_queue->write_count & FALCON_TXD_RING_MASK;
440 buffer = &tx_queue->buffer[write_ptr];
441 txd = falcon_tx_desc(tx_queue, write_ptr);
442 ++tx_queue->write_count;
443
444 /* Create TX descriptor ring entry */
445 EFX_POPULATE_QWORD_5(*txd,
446 TX_KER_PORT, 0,
447 TX_KER_CONT, buffer->continuation,
448 TX_KER_BYTE_CNT, buffer->len,
449 TX_KER_BUF_REGION, 0,
450 TX_KER_BUF_ADR, buffer->dma_addr);
451 } while (tx_queue->write_count != tx_queue->insert_count);
452
453 wmb(); /* Ensure descriptors are written before they are fetched */
454 falcon_notify_tx_desc(tx_queue);
455}
456
457/* Allocate hardware resources for a TX queue */
458int falcon_probe_tx(struct efx_tx_queue *tx_queue)
459{
460 struct efx_nic *efx = tx_queue->efx;
461 return falcon_alloc_special_buffer(efx, &tx_queue->txd,
462 FALCON_TXD_RING_SIZE *
463 sizeof(efx_qword_t));
464}
465
Ben Hutchingsbc3c90a2008-09-01 12:48:46 +0100466void falcon_init_tx(struct efx_tx_queue *tx_queue)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100467{
468 efx_oword_t tx_desc_ptr;
469 struct efx_nic *efx = tx_queue->efx;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100470
Ben Hutchings6bc5d3a2008-09-01 12:49:37 +0100471 tx_queue->flushed = false;
472
Ben Hutchings8ceee662008-04-27 12:55:59 +0100473 /* Pin TX descriptor ring */
Ben Hutchingsbc3c90a2008-09-01 12:48:46 +0100474 falcon_init_special_buffer(efx, &tx_queue->txd);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100475
476 /* Push TX descriptor ring to card */
477 EFX_POPULATE_OWORD_10(tx_desc_ptr,
478 TX_DESCQ_EN, 1,
479 TX_ISCSI_DDIG_EN, 0,
480 TX_ISCSI_HDIG_EN, 0,
481 TX_DESCQ_BUF_BASE_ID, tx_queue->txd.index,
Ben Hutchingsd3074022008-09-01 12:48:03 +0100482 TX_DESCQ_EVQ_ID, tx_queue->channel->channel,
Ben Hutchings8ceee662008-04-27 12:55:59 +0100483 TX_DESCQ_OWNER_ID, 0,
484 TX_DESCQ_LABEL, tx_queue->queue,
485 TX_DESCQ_SIZE, FALCON_TXD_RING_ORDER,
486 TX_DESCQ_TYPE, 0,
487 TX_NON_IP_DROP_DIS_B0, 1);
488
Ben Hutchings55668612008-05-16 21:16:10 +0100489 if (falcon_rev(efx) >= FALCON_REV_B0) {
Ben Hutchings60ac1062008-09-01 12:44:59 +0100490 int csum = tx_queue->queue == EFX_TX_QUEUE_OFFLOAD_CSUM;
491 EFX_SET_OWORD_FIELD(tx_desc_ptr, TX_IP_CHKSM_DIS_B0, !csum);
492 EFX_SET_OWORD_FIELD(tx_desc_ptr, TX_TCP_CHKSM_DIS_B0, !csum);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100493 }
494
495 falcon_write_table(efx, &tx_desc_ptr, efx->type->txd_ptr_tbl_base,
496 tx_queue->queue);
497
Ben Hutchings55668612008-05-16 21:16:10 +0100498 if (falcon_rev(efx) < FALCON_REV_B0) {
Ben Hutchings8ceee662008-04-27 12:55:59 +0100499 efx_oword_t reg;
500
Ben Hutchings60ac1062008-09-01 12:44:59 +0100501 /* Only 128 bits in this register */
502 BUILD_BUG_ON(EFX_TX_QUEUE_COUNT >= 128);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100503
504 falcon_read(efx, &reg, TX_CHKSM_CFG_REG_KER_A1);
Ben Hutchings60ac1062008-09-01 12:44:59 +0100505 if (tx_queue->queue == EFX_TX_QUEUE_OFFLOAD_CSUM)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100506 clear_bit_le(tx_queue->queue, (void *)&reg);
507 else
508 set_bit_le(tx_queue->queue, (void *)&reg);
509 falcon_write(efx, &reg, TX_CHKSM_CFG_REG_KER_A1);
510 }
Ben Hutchings8ceee662008-04-27 12:55:59 +0100511}
512
Ben Hutchings6bc5d3a2008-09-01 12:49:37 +0100513static void falcon_flush_tx_queue(struct efx_tx_queue *tx_queue)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100514{
515 struct efx_nic *efx = tx_queue->efx;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100516 efx_oword_t tx_flush_descq;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100517
518 /* Post a flush command */
519 EFX_POPULATE_OWORD_2(tx_flush_descq,
520 TX_FLUSH_DESCQ_CMD, 1,
521 TX_FLUSH_DESCQ, tx_queue->queue);
522 falcon_write(efx, &tx_flush_descq, TX_FLUSH_DESCQ_REG_KER);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100523}
524
525void falcon_fini_tx(struct efx_tx_queue *tx_queue)
526{
527 struct efx_nic *efx = tx_queue->efx;
528 efx_oword_t tx_desc_ptr;
529
Ben Hutchings6bc5d3a2008-09-01 12:49:37 +0100530 /* The queue should have been flushed */
531 WARN_ON(!tx_queue->flushed);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100532
533 /* Remove TX descriptor ring from card */
534 EFX_ZERO_OWORD(tx_desc_ptr);
535 falcon_write_table(efx, &tx_desc_ptr, efx->type->txd_ptr_tbl_base,
536 tx_queue->queue);
537
538 /* Unpin TX descriptor ring */
539 falcon_fini_special_buffer(efx, &tx_queue->txd);
540}
541
542/* Free buffers backing TX queue */
543void falcon_remove_tx(struct efx_tx_queue *tx_queue)
544{
545 falcon_free_special_buffer(tx_queue->efx, &tx_queue->txd);
546}
547
548/**************************************************************************
549 *
550 * Falcon RX path
551 *
552 **************************************************************************/
553
554/* Returns a pointer to the specified descriptor in the RX descriptor queue */
555static inline efx_qword_t *falcon_rx_desc(struct efx_rx_queue *rx_queue,
556 unsigned int index)
557{
558 return (((efx_qword_t *) (rx_queue->rxd.addr)) + index);
559}
560
561/* This creates an entry in the RX descriptor queue */
562static inline void falcon_build_rx_desc(struct efx_rx_queue *rx_queue,
563 unsigned index)
564{
565 struct efx_rx_buffer *rx_buf;
566 efx_qword_t *rxd;
567
568 rxd = falcon_rx_desc(rx_queue, index);
569 rx_buf = efx_rx_buffer(rx_queue, index);
570 EFX_POPULATE_QWORD_3(*rxd,
571 RX_KER_BUF_SIZE,
572 rx_buf->len -
573 rx_queue->efx->type->rx_buffer_padding,
574 RX_KER_BUF_REGION, 0,
575 RX_KER_BUF_ADR, rx_buf->dma_addr);
576}
577
578/* This writes to the RX_DESC_WPTR register for the specified receive
579 * descriptor ring.
580 */
581void falcon_notify_rx_desc(struct efx_rx_queue *rx_queue)
582{
583 efx_dword_t reg;
584 unsigned write_ptr;
585
586 while (rx_queue->notified_count != rx_queue->added_count) {
587 falcon_build_rx_desc(rx_queue,
588 rx_queue->notified_count &
589 FALCON_RXD_RING_MASK);
590 ++rx_queue->notified_count;
591 }
592
593 wmb();
594 write_ptr = rx_queue->added_count & FALCON_RXD_RING_MASK;
595 EFX_POPULATE_DWORD_1(reg, RX_DESC_WPTR_DWORD, write_ptr);
596 falcon_writel_page(rx_queue->efx, &reg,
597 RX_DESC_UPD_REG_KER_DWORD, rx_queue->queue);
598}
599
600int falcon_probe_rx(struct efx_rx_queue *rx_queue)
601{
602 struct efx_nic *efx = rx_queue->efx;
603 return falcon_alloc_special_buffer(efx, &rx_queue->rxd,
604 FALCON_RXD_RING_SIZE *
605 sizeof(efx_qword_t));
606}
607
Ben Hutchingsbc3c90a2008-09-01 12:48:46 +0100608void falcon_init_rx(struct efx_rx_queue *rx_queue)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100609{
610 efx_oword_t rx_desc_ptr;
611 struct efx_nic *efx = rx_queue->efx;
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +0100612 bool is_b0 = falcon_rev(efx) >= FALCON_REV_B0;
613 bool iscsi_digest_en = is_b0;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100614
615 EFX_LOG(efx, "RX queue %d ring in special buffers %d-%d\n",
616 rx_queue->queue, rx_queue->rxd.index,
617 rx_queue->rxd.index + rx_queue->rxd.entries - 1);
618
Ben Hutchings6bc5d3a2008-09-01 12:49:37 +0100619 rx_queue->flushed = false;
620
Ben Hutchings8ceee662008-04-27 12:55:59 +0100621 /* Pin RX descriptor ring */
Ben Hutchingsbc3c90a2008-09-01 12:48:46 +0100622 falcon_init_special_buffer(efx, &rx_queue->rxd);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100623
624 /* Push RX descriptor ring to card */
625 EFX_POPULATE_OWORD_10(rx_desc_ptr,
626 RX_ISCSI_DDIG_EN, iscsi_digest_en,
627 RX_ISCSI_HDIG_EN, iscsi_digest_en,
628 RX_DESCQ_BUF_BASE_ID, rx_queue->rxd.index,
Ben Hutchingsd3074022008-09-01 12:48:03 +0100629 RX_DESCQ_EVQ_ID, rx_queue->channel->channel,
Ben Hutchings8ceee662008-04-27 12:55:59 +0100630 RX_DESCQ_OWNER_ID, 0,
631 RX_DESCQ_LABEL, rx_queue->queue,
632 RX_DESCQ_SIZE, FALCON_RXD_RING_ORDER,
633 RX_DESCQ_TYPE, 0 /* kernel queue */ ,
634 /* For >=B0 this is scatter so disable */
635 RX_DESCQ_JUMBO, !is_b0,
636 RX_DESCQ_EN, 1);
637 falcon_write_table(efx, &rx_desc_ptr, efx->type->rxd_ptr_tbl_base,
638 rx_queue->queue);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100639}
640
Ben Hutchings6bc5d3a2008-09-01 12:49:37 +0100641static void falcon_flush_rx_queue(struct efx_rx_queue *rx_queue)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100642{
643 struct efx_nic *efx = rx_queue->efx;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100644 efx_oword_t rx_flush_descq;
645
646 /* Post a flush command */
647 EFX_POPULATE_OWORD_2(rx_flush_descq,
648 RX_FLUSH_DESCQ_CMD, 1,
649 RX_FLUSH_DESCQ, rx_queue->queue);
650 falcon_write(efx, &rx_flush_descq, RX_FLUSH_DESCQ_REG_KER);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100651}
652
653void falcon_fini_rx(struct efx_rx_queue *rx_queue)
654{
655 efx_oword_t rx_desc_ptr;
656 struct efx_nic *efx = rx_queue->efx;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100657
Ben Hutchings6bc5d3a2008-09-01 12:49:37 +0100658 /* The queue should already have been flushed */
659 WARN_ON(!rx_queue->flushed);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100660
661 /* Remove RX descriptor ring from card */
662 EFX_ZERO_OWORD(rx_desc_ptr);
663 falcon_write_table(efx, &rx_desc_ptr, efx->type->rxd_ptr_tbl_base,
664 rx_queue->queue);
665
666 /* Unpin RX descriptor ring */
667 falcon_fini_special_buffer(efx, &rx_queue->rxd);
668}
669
670/* Free buffers backing RX queue */
671void falcon_remove_rx(struct efx_rx_queue *rx_queue)
672{
673 falcon_free_special_buffer(rx_queue->efx, &rx_queue->rxd);
674}
675
676/**************************************************************************
677 *
678 * Falcon event queue processing
679 * Event queues are processed by per-channel tasklets.
680 *
681 **************************************************************************/
682
683/* Update a channel's event queue's read pointer (RPTR) register
684 *
685 * This writes the EVQ_RPTR_REG register for the specified channel's
686 * event queue.
687 *
688 * Note that EVQ_RPTR_REG contains the index of the "last read" event,
689 * whereas channel->eventq_read_ptr contains the index of the "next to
690 * read" event.
691 */
692void falcon_eventq_read_ack(struct efx_channel *channel)
693{
694 efx_dword_t reg;
695 struct efx_nic *efx = channel->efx;
696
697 EFX_POPULATE_DWORD_1(reg, EVQ_RPTR_DWORD, channel->eventq_read_ptr);
698 falcon_writel_table(efx, &reg, efx->type->evq_rptr_tbl_base,
Ben Hutchingsd3074022008-09-01 12:48:03 +0100699 channel->channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100700}
701
702/* Use HW to insert a SW defined event */
703void falcon_generate_event(struct efx_channel *channel, efx_qword_t *event)
704{
705 efx_oword_t drv_ev_reg;
706
707 EFX_POPULATE_OWORD_2(drv_ev_reg,
Ben Hutchingsd3074022008-09-01 12:48:03 +0100708 DRV_EV_QID, channel->channel,
Ben Hutchings8ceee662008-04-27 12:55:59 +0100709 DRV_EV_DATA,
710 EFX_QWORD_FIELD64(*event, WHOLE_EVENT));
711 falcon_write(channel->efx, &drv_ev_reg, DRV_EV_REG_KER);
712}
713
714/* Handle a transmit completion event
715 *
716 * Falcon batches TX completion events; the message we receive is of
717 * the form "complete all TX events up to this index".
718 */
Ben Hutchings4d566062008-09-01 12:47:12 +0100719static void falcon_handle_tx_event(struct efx_channel *channel,
720 efx_qword_t *event)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100721{
722 unsigned int tx_ev_desc_ptr;
723 unsigned int tx_ev_q_label;
724 struct efx_tx_queue *tx_queue;
725 struct efx_nic *efx = channel->efx;
726
727 if (likely(EFX_QWORD_FIELD(*event, TX_EV_COMP))) {
728 /* Transmit completion */
729 tx_ev_desc_ptr = EFX_QWORD_FIELD(*event, TX_EV_DESC_PTR);
730 tx_ev_q_label = EFX_QWORD_FIELD(*event, TX_EV_Q_LABEL);
731 tx_queue = &efx->tx_queue[tx_ev_q_label];
732 efx_xmit_done(tx_queue, tx_ev_desc_ptr);
733 } else if (EFX_QWORD_FIELD(*event, TX_EV_WQ_FF_FULL)) {
734 /* Rewrite the FIFO write pointer */
735 tx_ev_q_label = EFX_QWORD_FIELD(*event, TX_EV_Q_LABEL);
736 tx_queue = &efx->tx_queue[tx_ev_q_label];
737
Ben Hutchings55668612008-05-16 21:16:10 +0100738 if (efx_dev_registered(efx))
Ben Hutchings8ceee662008-04-27 12:55:59 +0100739 netif_tx_lock(efx->net_dev);
740 falcon_notify_tx_desc(tx_queue);
Ben Hutchings55668612008-05-16 21:16:10 +0100741 if (efx_dev_registered(efx))
Ben Hutchings8ceee662008-04-27 12:55:59 +0100742 netif_tx_unlock(efx->net_dev);
743 } else if (EFX_QWORD_FIELD(*event, TX_EV_PKT_ERR) &&
744 EFX_WORKAROUND_10727(efx)) {
745 efx_schedule_reset(efx, RESET_TYPE_TX_DESC_FETCH);
746 } else {
747 EFX_ERR(efx, "channel %d unexpected TX event "
748 EFX_QWORD_FMT"\n", channel->channel,
749 EFX_QWORD_VAL(*event));
750 }
751}
752
Ben Hutchings8ceee662008-04-27 12:55:59 +0100753/* Detect errors included in the rx_evt_pkt_ok bit. */
754static void falcon_handle_rx_not_ok(struct efx_rx_queue *rx_queue,
755 const efx_qword_t *event,
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +0100756 bool *rx_ev_pkt_ok,
757 bool *discard)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100758{
759 struct efx_nic *efx = rx_queue->efx;
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +0100760 bool rx_ev_buf_owner_id_err, rx_ev_ip_hdr_chksum_err;
761 bool rx_ev_tcp_udp_chksum_err, rx_ev_eth_crc_err;
762 bool rx_ev_frm_trunc, rx_ev_drib_nib, rx_ev_tobe_disc;
763 bool rx_ev_other_err, rx_ev_pause_frm;
764 bool rx_ev_ip_frag_err, rx_ev_hdr_type, rx_ev_mcast_pkt;
765 unsigned rx_ev_pkt_type;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100766
767 rx_ev_hdr_type = EFX_QWORD_FIELD(*event, RX_EV_HDR_TYPE);
768 rx_ev_mcast_pkt = EFX_QWORD_FIELD(*event, RX_EV_MCAST_PKT);
769 rx_ev_tobe_disc = EFX_QWORD_FIELD(*event, RX_EV_TOBE_DISC);
770 rx_ev_pkt_type = EFX_QWORD_FIELD(*event, RX_EV_PKT_TYPE);
771 rx_ev_buf_owner_id_err = EFX_QWORD_FIELD(*event,
772 RX_EV_BUF_OWNER_ID_ERR);
773 rx_ev_ip_frag_err = EFX_QWORD_FIELD(*event, RX_EV_IF_FRAG_ERR);
774 rx_ev_ip_hdr_chksum_err = EFX_QWORD_FIELD(*event,
775 RX_EV_IP_HDR_CHKSUM_ERR);
776 rx_ev_tcp_udp_chksum_err = EFX_QWORD_FIELD(*event,
777 RX_EV_TCP_UDP_CHKSUM_ERR);
778 rx_ev_eth_crc_err = EFX_QWORD_FIELD(*event, RX_EV_ETH_CRC_ERR);
779 rx_ev_frm_trunc = EFX_QWORD_FIELD(*event, RX_EV_FRM_TRUNC);
Ben Hutchings55668612008-05-16 21:16:10 +0100780 rx_ev_drib_nib = ((falcon_rev(efx) >= FALCON_REV_B0) ?
Ben Hutchings8ceee662008-04-27 12:55:59 +0100781 0 : EFX_QWORD_FIELD(*event, RX_EV_DRIB_NIB));
782 rx_ev_pause_frm = EFX_QWORD_FIELD(*event, RX_EV_PAUSE_FRM_ERR);
783
784 /* Every error apart from tobe_disc and pause_frm */
785 rx_ev_other_err = (rx_ev_drib_nib | rx_ev_tcp_udp_chksum_err |
786 rx_ev_buf_owner_id_err | rx_ev_eth_crc_err |
787 rx_ev_frm_trunc | rx_ev_ip_hdr_chksum_err);
788
Ben Hutchings50050872008-12-12 21:42:42 -0800789 /* Count errors that are not in MAC stats. Ignore expected
790 * checksum errors during self-test. */
Ben Hutchings8ceee662008-04-27 12:55:59 +0100791 if (rx_ev_frm_trunc)
792 ++rx_queue->channel->n_rx_frm_trunc;
793 else if (rx_ev_tobe_disc)
794 ++rx_queue->channel->n_rx_tobe_disc;
Ben Hutchings50050872008-12-12 21:42:42 -0800795 else if (!efx->loopback_selftest) {
796 if (rx_ev_ip_hdr_chksum_err)
797 ++rx_queue->channel->n_rx_ip_hdr_chksum_err;
798 else if (rx_ev_tcp_udp_chksum_err)
799 ++rx_queue->channel->n_rx_tcp_udp_chksum_err;
800 }
Ben Hutchings8ceee662008-04-27 12:55:59 +0100801 if (rx_ev_ip_frag_err)
802 ++rx_queue->channel->n_rx_ip_frag_err;
803
804 /* The frame must be discarded if any of these are true. */
805 *discard = (rx_ev_eth_crc_err | rx_ev_frm_trunc | rx_ev_drib_nib |
806 rx_ev_tobe_disc | rx_ev_pause_frm);
807
808 /* TOBE_DISC is expected on unicast mismatches; don't print out an
809 * error message. FRM_TRUNC indicates RXDP dropped the packet due
810 * to a FIFO overflow.
811 */
812#ifdef EFX_ENABLE_DEBUG
813 if (rx_ev_other_err) {
814 EFX_INFO_RL(efx, " RX queue %d unexpected RX event "
Ben Hutchings5b39fe32008-09-01 12:46:03 +0100815 EFX_QWORD_FMT "%s%s%s%s%s%s%s%s\n",
Ben Hutchings8ceee662008-04-27 12:55:59 +0100816 rx_queue->queue, EFX_QWORD_VAL(*event),
817 rx_ev_buf_owner_id_err ? " [OWNER_ID_ERR]" : "",
818 rx_ev_ip_hdr_chksum_err ?
819 " [IP_HDR_CHKSUM_ERR]" : "",
820 rx_ev_tcp_udp_chksum_err ?
821 " [TCP_UDP_CHKSUM_ERR]" : "",
822 rx_ev_eth_crc_err ? " [ETH_CRC_ERR]" : "",
823 rx_ev_frm_trunc ? " [FRM_TRUNC]" : "",
824 rx_ev_drib_nib ? " [DRIB_NIB]" : "",
825 rx_ev_tobe_disc ? " [TOBE_DISC]" : "",
Ben Hutchings5b39fe32008-09-01 12:46:03 +0100826 rx_ev_pause_frm ? " [PAUSE]" : "");
Ben Hutchings8ceee662008-04-27 12:55:59 +0100827 }
828#endif
Ben Hutchings8ceee662008-04-27 12:55:59 +0100829}
830
831/* Handle receive events that are not in-order. */
832static void falcon_handle_rx_bad_index(struct efx_rx_queue *rx_queue,
833 unsigned index)
834{
835 struct efx_nic *efx = rx_queue->efx;
836 unsigned expected, dropped;
837
838 expected = rx_queue->removed_count & FALCON_RXD_RING_MASK;
839 dropped = ((index + FALCON_RXD_RING_SIZE - expected) &
840 FALCON_RXD_RING_MASK);
841 EFX_INFO(efx, "dropped %d events (index=%d expected=%d)\n",
842 dropped, index, expected);
843
844 efx_schedule_reset(efx, EFX_WORKAROUND_5676(efx) ?
845 RESET_TYPE_RX_RECOVERY : RESET_TYPE_DISABLE);
846}
847
848/* Handle a packet received event
849 *
850 * Falcon silicon gives a "discard" flag if it's a unicast packet with the
851 * wrong destination address
852 * Also "is multicast" and "matches multicast filter" flags can be used to
853 * discard non-matching multicast packets.
854 */
Ben Hutchings42cbe2d2008-09-01 12:48:08 +0100855static void falcon_handle_rx_event(struct efx_channel *channel,
856 const efx_qword_t *event)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100857{
Ben Hutchings42cbe2d2008-09-01 12:48:08 +0100858 unsigned int rx_ev_desc_ptr, rx_ev_byte_cnt;
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +0100859 unsigned int rx_ev_hdr_type, rx_ev_mcast_pkt;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100860 unsigned expected_ptr;
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +0100861 bool rx_ev_pkt_ok, discard = false, checksummed;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100862 struct efx_rx_queue *rx_queue;
863 struct efx_nic *efx = channel->efx;
864
865 /* Basic packet information */
866 rx_ev_byte_cnt = EFX_QWORD_FIELD(*event, RX_EV_BYTE_CNT);
867 rx_ev_pkt_ok = EFX_QWORD_FIELD(*event, RX_EV_PKT_OK);
868 rx_ev_hdr_type = EFX_QWORD_FIELD(*event, RX_EV_HDR_TYPE);
869 WARN_ON(EFX_QWORD_FIELD(*event, RX_EV_JUMBO_CONT));
870 WARN_ON(EFX_QWORD_FIELD(*event, RX_EV_SOP) != 1);
Ben Hutchings42cbe2d2008-09-01 12:48:08 +0100871 WARN_ON(EFX_QWORD_FIELD(*event, RX_EV_Q_LABEL) != channel->channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100872
Ben Hutchings42cbe2d2008-09-01 12:48:08 +0100873 rx_queue = &efx->rx_queue[channel->channel];
Ben Hutchings8ceee662008-04-27 12:55:59 +0100874
875 rx_ev_desc_ptr = EFX_QWORD_FIELD(*event, RX_EV_DESC_PTR);
876 expected_ptr = rx_queue->removed_count & FALCON_RXD_RING_MASK;
Ben Hutchings42cbe2d2008-09-01 12:48:08 +0100877 if (unlikely(rx_ev_desc_ptr != expected_ptr))
Ben Hutchings8ceee662008-04-27 12:55:59 +0100878 falcon_handle_rx_bad_index(rx_queue, rx_ev_desc_ptr);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100879
880 if (likely(rx_ev_pkt_ok)) {
881 /* If packet is marked as OK and packet type is TCP/IPv4 or
882 * UDP/IPv4, then we can rely on the hardware checksum.
883 */
884 checksummed = RX_EV_HDR_TYPE_HAS_CHECKSUMS(rx_ev_hdr_type);
885 } else {
886 falcon_handle_rx_not_ok(rx_queue, event, &rx_ev_pkt_ok,
Ben Hutchings5b39fe32008-09-01 12:46:03 +0100887 &discard);
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +0100888 checksummed = false;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100889 }
890
891 /* Detect multicast packets that didn't match the filter */
892 rx_ev_mcast_pkt = EFX_QWORD_FIELD(*event, RX_EV_MCAST_PKT);
893 if (rx_ev_mcast_pkt) {
894 unsigned int rx_ev_mcast_hash_match =
895 EFX_QWORD_FIELD(*event, RX_EV_MCAST_HASH_MATCH);
896
897 if (unlikely(!rx_ev_mcast_hash_match))
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +0100898 discard = true;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100899 }
900
901 /* Handle received packet */
902 efx_rx_packet(rx_queue, rx_ev_desc_ptr, rx_ev_byte_cnt,
903 checksummed, discard);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100904}
905
906/* Global events are basically PHY events */
907static void falcon_handle_global_event(struct efx_channel *channel,
908 efx_qword_t *event)
909{
910 struct efx_nic *efx = channel->efx;
Ben Hutchings766ca0f2008-12-12 21:59:24 -0800911 bool handled = false;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100912
Ben Hutchings8ceee662008-04-27 12:55:59 +0100913 if (EFX_QWORD_FIELD(*event, G_PHY0_INTR) ||
914 EFX_QWORD_FIELD(*event, G_PHY1_INTR) ||
Ben Hutchings766ca0f2008-12-12 21:59:24 -0800915 EFX_QWORD_FIELD(*event, XG_PHY_INTR) ||
916 EFX_QWORD_FIELD(*event, XFP_PHY_INTR)) {
917 efx->phy_op->clear_interrupt(efx);
918 queue_work(efx->workqueue, &efx->phy_work);
919 handled = true;
920 }
Ben Hutchings8ceee662008-04-27 12:55:59 +0100921
Ben Hutchings55668612008-05-16 21:16:10 +0100922 if ((falcon_rev(efx) >= FALCON_REV_B0) &&
Ben Hutchings766ca0f2008-12-12 21:59:24 -0800923 EFX_QWORD_FIELD(*event, XG_MNT_INTR_B0)) {
924 queue_work(efx->workqueue, &efx->mac_work);
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +0100925 handled = true;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100926 }
927
928 if (EFX_QWORD_FIELD_VER(efx, *event, RX_RECOVERY)) {
929 EFX_ERR(efx, "channel %d seen global RX_RESET "
930 "event. Resetting.\n", channel->channel);
931
932 atomic_inc(&efx->rx_reset);
933 efx_schedule_reset(efx, EFX_WORKAROUND_6555(efx) ?
934 RESET_TYPE_RX_RECOVERY : RESET_TYPE_DISABLE);
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +0100935 handled = true;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100936 }
937
938 if (!handled)
939 EFX_ERR(efx, "channel %d unknown global event "
940 EFX_QWORD_FMT "\n", channel->channel,
941 EFX_QWORD_VAL(*event));
942}
943
944static void falcon_handle_driver_event(struct efx_channel *channel,
945 efx_qword_t *event)
946{
947 struct efx_nic *efx = channel->efx;
948 unsigned int ev_sub_code;
949 unsigned int ev_sub_data;
950
951 ev_sub_code = EFX_QWORD_FIELD(*event, DRIVER_EV_SUB_CODE);
952 ev_sub_data = EFX_QWORD_FIELD(*event, DRIVER_EV_SUB_DATA);
953
954 switch (ev_sub_code) {
955 case TX_DESCQ_FLS_DONE_EV_DECODE:
956 EFX_TRACE(efx, "channel %d TXQ %d flushed\n",
957 channel->channel, ev_sub_data);
958 break;
959 case RX_DESCQ_FLS_DONE_EV_DECODE:
960 EFX_TRACE(efx, "channel %d RXQ %d flushed\n",
961 channel->channel, ev_sub_data);
962 break;
963 case EVQ_INIT_DONE_EV_DECODE:
964 EFX_LOG(efx, "channel %d EVQ %d initialised\n",
965 channel->channel, ev_sub_data);
966 break;
967 case SRM_UPD_DONE_EV_DECODE:
968 EFX_TRACE(efx, "channel %d SRAM update done\n",
969 channel->channel);
970 break;
971 case WAKE_UP_EV_DECODE:
972 EFX_TRACE(efx, "channel %d RXQ %d wakeup event\n",
973 channel->channel, ev_sub_data);
974 break;
975 case TIMER_EV_DECODE:
976 EFX_TRACE(efx, "channel %d RX queue %d timer expired\n",
977 channel->channel, ev_sub_data);
978 break;
979 case RX_RECOVERY_EV_DECODE:
980 EFX_ERR(efx, "channel %d seen DRIVER RX_RESET event. "
981 "Resetting.\n", channel->channel);
Ben Hutchings05e3ec02008-05-07 13:00:39 +0100982 atomic_inc(&efx->rx_reset);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100983 efx_schedule_reset(efx,
984 EFX_WORKAROUND_6555(efx) ?
985 RESET_TYPE_RX_RECOVERY :
986 RESET_TYPE_DISABLE);
987 break;
988 case RX_DSC_ERROR_EV_DECODE:
989 EFX_ERR(efx, "RX DMA Q %d reports descriptor fetch error."
990 " RX Q %d is disabled.\n", ev_sub_data, ev_sub_data);
991 efx_schedule_reset(efx, RESET_TYPE_RX_DESC_FETCH);
992 break;
993 case TX_DSC_ERROR_EV_DECODE:
994 EFX_ERR(efx, "TX DMA Q %d reports descriptor fetch error."
995 " TX Q %d is disabled.\n", ev_sub_data, ev_sub_data);
996 efx_schedule_reset(efx, RESET_TYPE_TX_DESC_FETCH);
997 break;
998 default:
999 EFX_TRACE(efx, "channel %d unknown driver event code %d "
1000 "data %04x\n", channel->channel, ev_sub_code,
1001 ev_sub_data);
1002 break;
1003 }
1004}
1005
Ben Hutchings42cbe2d2008-09-01 12:48:08 +01001006int falcon_process_eventq(struct efx_channel *channel, int rx_quota)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001007{
1008 unsigned int read_ptr;
1009 efx_qword_t event, *p_event;
1010 int ev_code;
Ben Hutchings42cbe2d2008-09-01 12:48:08 +01001011 int rx_packets = 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001012
1013 read_ptr = channel->eventq_read_ptr;
1014
1015 do {
1016 p_event = falcon_event(channel, read_ptr);
1017 event = *p_event;
1018
1019 if (!falcon_event_present(&event))
1020 /* End of events */
1021 break;
1022
1023 EFX_TRACE(channel->efx, "channel %d event is "EFX_QWORD_FMT"\n",
1024 channel->channel, EFX_QWORD_VAL(event));
1025
1026 /* Clear this event by marking it all ones */
1027 EFX_SET_QWORD(*p_event);
1028
1029 ev_code = EFX_QWORD_FIELD(event, EV_CODE);
1030
1031 switch (ev_code) {
1032 case RX_IP_EV_DECODE:
Ben Hutchings42cbe2d2008-09-01 12:48:08 +01001033 falcon_handle_rx_event(channel, &event);
1034 ++rx_packets;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001035 break;
1036 case TX_IP_EV_DECODE:
1037 falcon_handle_tx_event(channel, &event);
1038 break;
1039 case DRV_GEN_EV_DECODE:
1040 channel->eventq_magic
1041 = EFX_QWORD_FIELD(event, EVQ_MAGIC);
1042 EFX_LOG(channel->efx, "channel %d received generated "
1043 "event "EFX_QWORD_FMT"\n", channel->channel,
1044 EFX_QWORD_VAL(event));
1045 break;
1046 case GLOBAL_EV_DECODE:
1047 falcon_handle_global_event(channel, &event);
1048 break;
1049 case DRIVER_EV_DECODE:
1050 falcon_handle_driver_event(channel, &event);
1051 break;
1052 default:
1053 EFX_ERR(channel->efx, "channel %d unknown event type %d"
1054 " (data " EFX_QWORD_FMT ")\n", channel->channel,
1055 ev_code, EFX_QWORD_VAL(event));
1056 }
1057
1058 /* Increment read pointer */
1059 read_ptr = (read_ptr + 1) & FALCON_EVQ_MASK;
1060
Ben Hutchings42cbe2d2008-09-01 12:48:08 +01001061 } while (rx_packets < rx_quota);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001062
1063 channel->eventq_read_ptr = read_ptr;
Ben Hutchings42cbe2d2008-09-01 12:48:08 +01001064 return rx_packets;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001065}
1066
1067void falcon_set_int_moderation(struct efx_channel *channel)
1068{
1069 efx_dword_t timer_cmd;
1070 struct efx_nic *efx = channel->efx;
1071
1072 /* Set timer register */
1073 if (channel->irq_moderation) {
1074 /* Round to resolution supported by hardware. The value we
1075 * program is based at 0. So actual interrupt moderation
1076 * achieved is ((x + 1) * res).
1077 */
1078 unsigned int res = 5;
1079 channel->irq_moderation -= (channel->irq_moderation % res);
1080 if (channel->irq_moderation < res)
1081 channel->irq_moderation = res;
1082 EFX_POPULATE_DWORD_2(timer_cmd,
1083 TIMER_MODE, TIMER_MODE_INT_HLDOFF,
1084 TIMER_VAL,
1085 (channel->irq_moderation / res) - 1);
1086 } else {
1087 EFX_POPULATE_DWORD_2(timer_cmd,
1088 TIMER_MODE, TIMER_MODE_DIS,
1089 TIMER_VAL, 0);
1090 }
1091 falcon_writel_page_locked(efx, &timer_cmd, TIMER_CMD_REG_KER,
Ben Hutchingsd3074022008-09-01 12:48:03 +01001092 channel->channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001093
1094}
1095
1096/* Allocate buffer table entries for event queue */
1097int falcon_probe_eventq(struct efx_channel *channel)
1098{
1099 struct efx_nic *efx = channel->efx;
1100 unsigned int evq_size;
1101
1102 evq_size = FALCON_EVQ_SIZE * sizeof(efx_qword_t);
1103 return falcon_alloc_special_buffer(efx, &channel->eventq, evq_size);
1104}
1105
Ben Hutchingsbc3c90a2008-09-01 12:48:46 +01001106void falcon_init_eventq(struct efx_channel *channel)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001107{
1108 efx_oword_t evq_ptr;
1109 struct efx_nic *efx = channel->efx;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001110
1111 EFX_LOG(efx, "channel %d event queue in special buffers %d-%d\n",
1112 channel->channel, channel->eventq.index,
1113 channel->eventq.index + channel->eventq.entries - 1);
1114
1115 /* Pin event queue buffer */
Ben Hutchingsbc3c90a2008-09-01 12:48:46 +01001116 falcon_init_special_buffer(efx, &channel->eventq);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001117
1118 /* Fill event queue with all ones (i.e. empty events) */
1119 memset(channel->eventq.addr, 0xff, channel->eventq.len);
1120
1121 /* Push event queue to card */
1122 EFX_POPULATE_OWORD_3(evq_ptr,
1123 EVQ_EN, 1,
1124 EVQ_SIZE, FALCON_EVQ_ORDER,
1125 EVQ_BUF_BASE_ID, channel->eventq.index);
1126 falcon_write_table(efx, &evq_ptr, efx->type->evq_ptr_tbl_base,
Ben Hutchingsd3074022008-09-01 12:48:03 +01001127 channel->channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001128
1129 falcon_set_int_moderation(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001130}
1131
1132void falcon_fini_eventq(struct efx_channel *channel)
1133{
1134 efx_oword_t eventq_ptr;
1135 struct efx_nic *efx = channel->efx;
1136
1137 /* Remove event queue from card */
1138 EFX_ZERO_OWORD(eventq_ptr);
1139 falcon_write_table(efx, &eventq_ptr, efx->type->evq_ptr_tbl_base,
Ben Hutchingsd3074022008-09-01 12:48:03 +01001140 channel->channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001141
1142 /* Unpin event queue */
1143 falcon_fini_special_buffer(efx, &channel->eventq);
1144}
1145
1146/* Free buffers backing event queue */
1147void falcon_remove_eventq(struct efx_channel *channel)
1148{
1149 falcon_free_special_buffer(channel->efx, &channel->eventq);
1150}
1151
1152
1153/* Generates a test event on the event queue. A subsequent call to
1154 * process_eventq() should pick up the event and place the value of
1155 * "magic" into channel->eventq_magic;
1156 */
1157void falcon_generate_test_event(struct efx_channel *channel, unsigned int magic)
1158{
1159 efx_qword_t test_event;
1160
1161 EFX_POPULATE_QWORD_2(test_event,
1162 EV_CODE, DRV_GEN_EV_DECODE,
1163 EVQ_MAGIC, magic);
1164 falcon_generate_event(channel, &test_event);
1165}
1166
Ben Hutchings177dfcd2008-12-12 21:50:08 -08001167void falcon_sim_phy_event(struct efx_nic *efx)
1168{
1169 efx_qword_t phy_event;
1170
1171 EFX_POPULATE_QWORD_1(phy_event, EV_CODE, GLOBAL_EV_DECODE);
1172 if (EFX_IS10G(efx))
1173 EFX_SET_OWORD_FIELD(phy_event, XG_PHY_INTR, 1);
1174 else
1175 EFX_SET_OWORD_FIELD(phy_event, G_PHY0_INTR, 1);
1176
1177 falcon_generate_event(&efx->channel[0], &phy_event);
1178}
1179
Ben Hutchings6bc5d3a2008-09-01 12:49:37 +01001180/**************************************************************************
1181 *
1182 * Flush handling
1183 *
1184 **************************************************************************/
1185
1186
1187static void falcon_poll_flush_events(struct efx_nic *efx)
1188{
1189 struct efx_channel *channel = &efx->channel[0];
1190 struct efx_tx_queue *tx_queue;
1191 struct efx_rx_queue *rx_queue;
Ben Hutchings4720bc62009-03-04 10:01:15 +00001192 unsigned int read_ptr = channel->eventq_read_ptr;
1193 unsigned int end_ptr = (read_ptr - 1) & FALCON_EVQ_MASK;
Ben Hutchings6bc5d3a2008-09-01 12:49:37 +01001194
Ben Hutchings4720bc62009-03-04 10:01:15 +00001195 do {
Ben Hutchings6bc5d3a2008-09-01 12:49:37 +01001196 efx_qword_t *event = falcon_event(channel, read_ptr);
1197 int ev_code, ev_sub_code, ev_queue;
1198 bool ev_failed;
Ben Hutchings4720bc62009-03-04 10:01:15 +00001199
Ben Hutchings6bc5d3a2008-09-01 12:49:37 +01001200 if (!falcon_event_present(event))
1201 break;
1202
1203 ev_code = EFX_QWORD_FIELD(*event, EV_CODE);
Ben Hutchings6bc5d3a2008-09-01 12:49:37 +01001204 ev_sub_code = EFX_QWORD_FIELD(*event, DRIVER_EV_SUB_CODE);
Ben Hutchings4720bc62009-03-04 10:01:15 +00001205 if (ev_code == DRIVER_EV_DECODE &&
1206 ev_sub_code == TX_DESCQ_FLS_DONE_EV_DECODE) {
Ben Hutchings6bc5d3a2008-09-01 12:49:37 +01001207 ev_queue = EFX_QWORD_FIELD(*event,
1208 DRIVER_EV_TX_DESCQ_ID);
1209 if (ev_queue < EFX_TX_QUEUE_COUNT) {
1210 tx_queue = efx->tx_queue + ev_queue;
1211 tx_queue->flushed = true;
1212 }
Ben Hutchings4720bc62009-03-04 10:01:15 +00001213 } else if (ev_code == DRIVER_EV_DECODE &&
1214 ev_sub_code == RX_DESCQ_FLS_DONE_EV_DECODE) {
Ben Hutchings6bc5d3a2008-09-01 12:49:37 +01001215 ev_queue = EFX_QWORD_FIELD(*event,
1216 DRIVER_EV_RX_DESCQ_ID);
1217 ev_failed = EFX_QWORD_FIELD(*event,
1218 DRIVER_EV_RX_FLUSH_FAIL);
1219 if (ev_queue < efx->n_rx_queues) {
1220 rx_queue = efx->rx_queue + ev_queue;
1221
1222 /* retry the rx flush */
1223 if (ev_failed)
1224 falcon_flush_rx_queue(rx_queue);
1225 else
1226 rx_queue->flushed = true;
1227 }
Ben Hutchings6bc5d3a2008-09-01 12:49:37 +01001228 }
1229
1230 read_ptr = (read_ptr + 1) & FALCON_EVQ_MASK;
Ben Hutchings4720bc62009-03-04 10:01:15 +00001231 } while (read_ptr != end_ptr);
Ben Hutchings6bc5d3a2008-09-01 12:49:37 +01001232}
1233
1234/* Handle tx and rx flushes at the same time, since they run in
1235 * parallel in the hardware and there's no reason for us to
1236 * serialise them */
1237int falcon_flush_queues(struct efx_nic *efx)
1238{
1239 struct efx_rx_queue *rx_queue;
1240 struct efx_tx_queue *tx_queue;
1241 int i;
1242 bool outstanding;
1243
1244 /* Issue flush requests */
1245 efx_for_each_tx_queue(tx_queue, efx) {
1246 tx_queue->flushed = false;
1247 falcon_flush_tx_queue(tx_queue);
1248 }
1249 efx_for_each_rx_queue(rx_queue, efx) {
1250 rx_queue->flushed = false;
1251 falcon_flush_rx_queue(rx_queue);
1252 }
1253
1254 /* Poll the evq looking for flush completions. Since we're not pushing
1255 * any more rx or tx descriptors at this point, we're in no danger of
1256 * overflowing the evq whilst we wait */
1257 for (i = 0; i < FALCON_FLUSH_POLL_COUNT; ++i) {
1258 msleep(FALCON_FLUSH_INTERVAL);
1259 falcon_poll_flush_events(efx);
1260
1261 /* Check if every queue has been succesfully flushed */
1262 outstanding = false;
1263 efx_for_each_tx_queue(tx_queue, efx)
1264 outstanding |= !tx_queue->flushed;
1265 efx_for_each_rx_queue(rx_queue, efx)
1266 outstanding |= !rx_queue->flushed;
1267 if (!outstanding)
1268 return 0;
1269 }
1270
1271 /* Mark the queues as all flushed. We're going to return failure
1272 * leading to a reset, or fake up success anyway. "flushed" now
1273 * indicates that we tried to flush. */
1274 efx_for_each_tx_queue(tx_queue, efx) {
1275 if (!tx_queue->flushed)
1276 EFX_ERR(efx, "tx queue %d flush command timed out\n",
1277 tx_queue->queue);
1278 tx_queue->flushed = true;
1279 }
1280 efx_for_each_rx_queue(rx_queue, efx) {
1281 if (!rx_queue->flushed)
1282 EFX_ERR(efx, "rx queue %d flush command timed out\n",
1283 rx_queue->queue);
1284 rx_queue->flushed = true;
1285 }
1286
1287 if (EFX_WORKAROUND_7803(efx))
1288 return 0;
1289
1290 return -ETIMEDOUT;
1291}
Ben Hutchings8ceee662008-04-27 12:55:59 +01001292
1293/**************************************************************************
1294 *
1295 * Falcon hardware interrupts
1296 * The hardware interrupt handler does very little work; all the event
1297 * queue processing is carried out by per-channel tasklets.
1298 *
1299 **************************************************************************/
1300
1301/* Enable/disable/generate Falcon interrupts */
1302static inline void falcon_interrupts(struct efx_nic *efx, int enabled,
1303 int force)
1304{
1305 efx_oword_t int_en_reg_ker;
1306
1307 EFX_POPULATE_OWORD_2(int_en_reg_ker,
1308 KER_INT_KER, force,
1309 DRV_INT_EN_KER, enabled);
1310 falcon_write(efx, &int_en_reg_ker, INT_EN_REG_KER);
1311}
1312
1313void falcon_enable_interrupts(struct efx_nic *efx)
1314{
1315 efx_oword_t int_adr_reg_ker;
1316 struct efx_channel *channel;
1317
1318 EFX_ZERO_OWORD(*((efx_oword_t *) efx->irq_status.addr));
1319 wmb(); /* Ensure interrupt vector is clear before interrupts enabled */
1320
1321 /* Program address */
1322 EFX_POPULATE_OWORD_2(int_adr_reg_ker,
1323 NORM_INT_VEC_DIS_KER, EFX_INT_MODE_USE_MSI(efx),
1324 INT_ADR_KER, efx->irq_status.dma_addr);
1325 falcon_write(efx, &int_adr_reg_ker, INT_ADR_REG_KER);
1326
1327 /* Enable interrupts */
1328 falcon_interrupts(efx, 1, 0);
1329
1330 /* Force processing of all the channels to get the EVQ RPTRs up to
1331 date */
Ben Hutchings64ee3122008-09-01 12:47:38 +01001332 efx_for_each_channel(channel, efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001333 efx_schedule_channel(channel);
1334}
1335
1336void falcon_disable_interrupts(struct efx_nic *efx)
1337{
1338 /* Disable interrupts */
1339 falcon_interrupts(efx, 0, 0);
1340}
1341
1342/* Generate a Falcon test interrupt
1343 * Interrupt must already have been enabled, otherwise nasty things
1344 * may happen.
1345 */
1346void falcon_generate_interrupt(struct efx_nic *efx)
1347{
1348 falcon_interrupts(efx, 1, 1);
1349}
1350
1351/* Acknowledge a legacy interrupt from Falcon
1352 *
1353 * This acknowledges a legacy (not MSI) interrupt via INT_ACK_KER_REG.
1354 *
1355 * Due to SFC bug 3706 (silicon revision <=A1) reads can be duplicated in the
1356 * BIU. Interrupt acknowledge is read sensitive so must write instead
1357 * (then read to ensure the BIU collector is flushed)
1358 *
1359 * NB most hardware supports MSI interrupts
1360 */
1361static inline void falcon_irq_ack_a1(struct efx_nic *efx)
1362{
1363 efx_dword_t reg;
1364
1365 EFX_POPULATE_DWORD_1(reg, INT_ACK_DUMMY_DATA, 0xb7eb7e);
1366 falcon_writel(efx, &reg, INT_ACK_REG_KER_A1);
1367 falcon_readl(efx, &reg, WORK_AROUND_BROKEN_PCI_READS_REG_KER_A1);
1368}
1369
1370/* Process a fatal interrupt
1371 * Disable bus mastering ASAP and schedule a reset
1372 */
1373static irqreturn_t falcon_fatal_interrupt(struct efx_nic *efx)
1374{
1375 struct falcon_nic_data *nic_data = efx->nic_data;
Ben Hutchingsd3208b52008-05-16 21:20:00 +01001376 efx_oword_t *int_ker = efx->irq_status.addr;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001377 efx_oword_t fatal_intr;
1378 int error, mem_perr;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001379
1380 falcon_read(efx, &fatal_intr, FATAL_INTR_REG_KER);
1381 error = EFX_OWORD_FIELD(fatal_intr, INT_KER_ERROR);
1382
1383 EFX_ERR(efx, "SYSTEM ERROR " EFX_OWORD_FMT " status "
1384 EFX_OWORD_FMT ": %s\n", EFX_OWORD_VAL(*int_ker),
1385 EFX_OWORD_VAL(fatal_intr),
1386 error ? "disabling bus mastering" : "no recognised error");
1387 if (error == 0)
1388 goto out;
1389
1390 /* If this is a memory parity error dump which blocks are offending */
1391 mem_perr = EFX_OWORD_FIELD(fatal_intr, MEM_PERR_INT_KER);
1392 if (mem_perr) {
1393 efx_oword_t reg;
1394 falcon_read(efx, &reg, MEM_STAT_REG_KER);
1395 EFX_ERR(efx, "SYSTEM ERROR: memory parity error "
1396 EFX_OWORD_FMT "\n", EFX_OWORD_VAL(reg));
1397 }
1398
Ben Hutchings0a62f1a2008-09-01 12:50:14 +01001399 /* Disable both devices */
Ben Hutchingsef1bba22008-12-23 03:09:53 +00001400 pci_clear_master(efx->pci_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001401 if (FALCON_IS_DUAL_FUNC(efx))
Ben Hutchingsef1bba22008-12-23 03:09:53 +00001402 pci_clear_master(nic_data->pci_dev2);
Ben Hutchings0a62f1a2008-09-01 12:50:14 +01001403 falcon_disable_interrupts(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001404
Ben Hutchings2c3c3d02009-03-04 10:01:57 +00001405 /* Count errors and reset or disable the NIC accordingly */
1406 if (nic_data->int_error_count == 0 ||
1407 time_after(jiffies, nic_data->int_error_expire)) {
1408 nic_data->int_error_count = 0;
1409 nic_data->int_error_expire =
1410 jiffies + FALCON_INT_ERROR_EXPIRE * HZ;
1411 }
1412 if (++nic_data->int_error_count < FALCON_MAX_INT_ERRORS) {
Ben Hutchings8ceee662008-04-27 12:55:59 +01001413 EFX_ERR(efx, "SYSTEM ERROR - reset scheduled\n");
1414 efx_schedule_reset(efx, RESET_TYPE_INT_ERROR);
1415 } else {
1416 EFX_ERR(efx, "SYSTEM ERROR - max number of errors seen."
1417 "NIC will be disabled\n");
1418 efx_schedule_reset(efx, RESET_TYPE_DISABLE);
1419 }
1420out:
1421 return IRQ_HANDLED;
1422}
1423
1424/* Handle a legacy interrupt from Falcon
1425 * Acknowledges the interrupt and schedule event queue processing.
1426 */
1427static irqreturn_t falcon_legacy_interrupt_b0(int irq, void *dev_id)
1428{
Ben Hutchingsd3208b52008-05-16 21:20:00 +01001429 struct efx_nic *efx = dev_id;
1430 efx_oword_t *int_ker = efx->irq_status.addr;
Ben Hutchingsa9de9a72009-03-20 13:26:41 +00001431 irqreturn_t result = IRQ_NONE;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001432 struct efx_channel *channel;
1433 efx_dword_t reg;
1434 u32 queues;
1435 int syserr;
1436
1437 /* Read the ISR which also ACKs the interrupts */
1438 falcon_readl(efx, &reg, INT_ISR0_B0);
1439 queues = EFX_EXTRACT_DWORD(reg, 0, 31);
1440
1441 /* Check to see if we have a serious error condition */
1442 syserr = EFX_OWORD_FIELD(*int_ker, FATAL_INT);
1443 if (unlikely(syserr))
1444 return falcon_fatal_interrupt(efx);
1445
Ben Hutchings8ceee662008-04-27 12:55:59 +01001446 /* Schedule processing of any interrupting queues */
Ben Hutchingsa9de9a72009-03-20 13:26:41 +00001447 efx_for_each_channel(channel, efx) {
1448 if ((queues & 1) ||
1449 falcon_event_present(
1450 falcon_event(channel, channel->eventq_read_ptr))) {
Ben Hutchings8ceee662008-04-27 12:55:59 +01001451 efx_schedule_channel(channel);
Ben Hutchingsa9de9a72009-03-20 13:26:41 +00001452 result = IRQ_HANDLED;
1453 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01001454 queues >>= 1;
1455 }
1456
Ben Hutchingsa9de9a72009-03-20 13:26:41 +00001457 if (result == IRQ_HANDLED) {
1458 efx->last_irq_cpu = raw_smp_processor_id();
1459 EFX_TRACE(efx, "IRQ %d on CPU %d status " EFX_DWORD_FMT "\n",
1460 irq, raw_smp_processor_id(), EFX_DWORD_VAL(reg));
1461 }
1462
1463 return result;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001464}
1465
1466
1467static irqreturn_t falcon_legacy_interrupt_a1(int irq, void *dev_id)
1468{
Ben Hutchingsd3208b52008-05-16 21:20:00 +01001469 struct efx_nic *efx = dev_id;
1470 efx_oword_t *int_ker = efx->irq_status.addr;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001471 struct efx_channel *channel;
1472 int syserr;
1473 int queues;
1474
1475 /* Check to see if this is our interrupt. If it isn't, we
1476 * exit without having touched the hardware.
1477 */
1478 if (unlikely(EFX_OWORD_IS_ZERO(*int_ker))) {
1479 EFX_TRACE(efx, "IRQ %d on CPU %d not for me\n", irq,
1480 raw_smp_processor_id());
1481 return IRQ_NONE;
1482 }
1483 efx->last_irq_cpu = raw_smp_processor_id();
1484 EFX_TRACE(efx, "IRQ %d on CPU %d status " EFX_OWORD_FMT "\n",
1485 irq, raw_smp_processor_id(), EFX_OWORD_VAL(*int_ker));
1486
1487 /* Check to see if we have a serious error condition */
1488 syserr = EFX_OWORD_FIELD(*int_ker, FATAL_INT);
1489 if (unlikely(syserr))
1490 return falcon_fatal_interrupt(efx);
1491
1492 /* Determine interrupting queues, clear interrupt status
1493 * register and acknowledge the device interrupt.
1494 */
1495 BUILD_BUG_ON(INT_EVQS_WIDTH > EFX_MAX_CHANNELS);
1496 queues = EFX_OWORD_FIELD(*int_ker, INT_EVQS);
1497 EFX_ZERO_OWORD(*int_ker);
1498 wmb(); /* Ensure the vector is cleared before interrupt ack */
1499 falcon_irq_ack_a1(efx);
1500
1501 /* Schedule processing of any interrupting queues */
1502 channel = &efx->channel[0];
1503 while (queues) {
1504 if (queues & 0x01)
1505 efx_schedule_channel(channel);
1506 channel++;
1507 queues >>= 1;
1508 }
1509
1510 return IRQ_HANDLED;
1511}
1512
1513/* Handle an MSI interrupt from Falcon
1514 *
1515 * Handle an MSI hardware interrupt. This routine schedules event
1516 * queue processing. No interrupt acknowledgement cycle is necessary.
1517 * Also, we never need to check that the interrupt is for us, since
1518 * MSI interrupts cannot be shared.
1519 */
1520static irqreturn_t falcon_msi_interrupt(int irq, void *dev_id)
1521{
Ben Hutchingsd3208b52008-05-16 21:20:00 +01001522 struct efx_channel *channel = dev_id;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001523 struct efx_nic *efx = channel->efx;
Ben Hutchingsd3208b52008-05-16 21:20:00 +01001524 efx_oword_t *int_ker = efx->irq_status.addr;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001525 int syserr;
1526
1527 efx->last_irq_cpu = raw_smp_processor_id();
1528 EFX_TRACE(efx, "IRQ %d on CPU %d status " EFX_OWORD_FMT "\n",
1529 irq, raw_smp_processor_id(), EFX_OWORD_VAL(*int_ker));
1530
1531 /* Check to see if we have a serious error condition */
1532 syserr = EFX_OWORD_FIELD(*int_ker, FATAL_INT);
1533 if (unlikely(syserr))
1534 return falcon_fatal_interrupt(efx);
1535
1536 /* Schedule processing of the channel */
1537 efx_schedule_channel(channel);
1538
1539 return IRQ_HANDLED;
1540}
1541
1542
1543/* Setup RSS indirection table.
1544 * This maps from the hash value of the packet to RXQ
1545 */
1546static void falcon_setup_rss_indir_table(struct efx_nic *efx)
1547{
1548 int i = 0;
1549 unsigned long offset;
1550 efx_dword_t dword;
1551
Ben Hutchings55668612008-05-16 21:16:10 +01001552 if (falcon_rev(efx) < FALCON_REV_B0)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001553 return;
1554
1555 for (offset = RX_RSS_INDIR_TBL_B0;
1556 offset < RX_RSS_INDIR_TBL_B0 + 0x800;
1557 offset += 0x10) {
1558 EFX_POPULATE_DWORD_1(dword, RX_RSS_INDIR_ENT_B0,
Ben Hutchings8831da72008-09-01 12:47:48 +01001559 i % efx->n_rx_queues);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001560 falcon_writel(efx, &dword, offset);
1561 i++;
1562 }
1563}
1564
1565/* Hook interrupt handler(s)
1566 * Try MSI and then legacy interrupts.
1567 */
1568int falcon_init_interrupt(struct efx_nic *efx)
1569{
1570 struct efx_channel *channel;
1571 int rc;
1572
1573 if (!EFX_INT_MODE_USE_MSI(efx)) {
1574 irq_handler_t handler;
Ben Hutchings55668612008-05-16 21:16:10 +01001575 if (falcon_rev(efx) >= FALCON_REV_B0)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001576 handler = falcon_legacy_interrupt_b0;
1577 else
1578 handler = falcon_legacy_interrupt_a1;
1579
1580 rc = request_irq(efx->legacy_irq, handler, IRQF_SHARED,
1581 efx->name, efx);
1582 if (rc) {
1583 EFX_ERR(efx, "failed to hook legacy IRQ %d\n",
1584 efx->pci_dev->irq);
1585 goto fail1;
1586 }
1587 return 0;
1588 }
1589
1590 /* Hook MSI or MSI-X interrupt */
Ben Hutchings64ee3122008-09-01 12:47:38 +01001591 efx_for_each_channel(channel, efx) {
Ben Hutchings8ceee662008-04-27 12:55:59 +01001592 rc = request_irq(channel->irq, falcon_msi_interrupt,
1593 IRQF_PROBE_SHARED, /* Not shared */
Ben Hutchings56536e92008-12-12 21:37:02 -08001594 channel->name, channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001595 if (rc) {
1596 EFX_ERR(efx, "failed to hook IRQ %d\n", channel->irq);
1597 goto fail2;
1598 }
1599 }
1600
1601 return 0;
1602
1603 fail2:
Ben Hutchings64ee3122008-09-01 12:47:38 +01001604 efx_for_each_channel(channel, efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001605 free_irq(channel->irq, channel);
1606 fail1:
1607 return rc;
1608}
1609
1610void falcon_fini_interrupt(struct efx_nic *efx)
1611{
1612 struct efx_channel *channel;
1613 efx_oword_t reg;
1614
1615 /* Disable MSI/MSI-X interrupts */
Ben Hutchings64ee3122008-09-01 12:47:38 +01001616 efx_for_each_channel(channel, efx) {
Ben Hutchings8ceee662008-04-27 12:55:59 +01001617 if (channel->irq)
1618 free_irq(channel->irq, channel);
Ben Hutchingsb3475642008-05-16 21:15:49 +01001619 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01001620
1621 /* ACK legacy interrupt */
Ben Hutchings55668612008-05-16 21:16:10 +01001622 if (falcon_rev(efx) >= FALCON_REV_B0)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001623 falcon_read(efx, &reg, INT_ISR0_B0);
1624 else
1625 falcon_irq_ack_a1(efx);
1626
1627 /* Disable legacy interrupt */
1628 if (efx->legacy_irq)
1629 free_irq(efx->legacy_irq, efx);
1630}
1631
1632/**************************************************************************
1633 *
1634 * EEPROM/flash
1635 *
1636 **************************************************************************
1637 */
1638
Ben Hutchings23d30f02008-12-12 21:56:11 -08001639#define FALCON_SPI_MAX_LEN sizeof(efx_oword_t)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001640
Ben Hutchingsbe4ea892008-12-12 21:33:50 -08001641static int falcon_spi_poll(struct efx_nic *efx)
1642{
1643 efx_oword_t reg;
1644 falcon_read(efx, &reg, EE_SPI_HCMD_REG_KER);
1645 return EFX_OWORD_FIELD(reg, EE_SPI_HCMD_CMD_EN) ? -EBUSY : 0;
1646}
1647
Ben Hutchings8ceee662008-04-27 12:55:59 +01001648/* Wait for SPI command completion */
1649static int falcon_spi_wait(struct efx_nic *efx)
1650{
Ben Hutchingsbe4ea892008-12-12 21:33:50 -08001651 /* Most commands will finish quickly, so we start polling at
1652 * very short intervals. Sometimes the command may have to
1653 * wait for VPD or expansion ROM access outside of our
1654 * control, so we allow up to 100 ms. */
1655 unsigned long timeout = jiffies + 1 + DIV_ROUND_UP(HZ, 10);
1656 int i;
1657
1658 for (i = 0; i < 10; i++) {
1659 if (!falcon_spi_poll(efx))
1660 return 0;
1661 udelay(10);
1662 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01001663
Ben Hutchings4a5b5042008-09-01 12:47:16 +01001664 for (;;) {
Ben Hutchingsbe4ea892008-12-12 21:33:50 -08001665 if (!falcon_spi_poll(efx))
Ben Hutchings8ceee662008-04-27 12:55:59 +01001666 return 0;
Ben Hutchings4a5b5042008-09-01 12:47:16 +01001667 if (time_after_eq(jiffies, timeout)) {
1668 EFX_ERR(efx, "timed out waiting for SPI\n");
1669 return -ETIMEDOUT;
1670 }
Ben Hutchingsbe4ea892008-12-12 21:33:50 -08001671 schedule_timeout_uninterruptible(1);
Ben Hutchings4a5b5042008-09-01 12:47:16 +01001672 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01001673}
1674
Ben Hutchingsf4150722008-11-04 20:34:28 +00001675int falcon_spi_cmd(const struct efx_spi_device *spi,
1676 unsigned int command, int address,
Ben Hutchings23d30f02008-12-12 21:56:11 -08001677 const void *in, void *out, size_t len)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001678{
Ben Hutchings4a5b5042008-09-01 12:47:16 +01001679 struct efx_nic *efx = spi->efx;
1680 bool addressed = (address >= 0);
1681 bool reading = (out != NULL);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001682 efx_oword_t reg;
1683 int rc;
1684
Ben Hutchings4a5b5042008-09-01 12:47:16 +01001685 /* Input validation */
1686 if (len > FALCON_SPI_MAX_LEN)
1687 return -EINVAL;
Ben Hutchingsf4150722008-11-04 20:34:28 +00001688 BUG_ON(!mutex_is_locked(&efx->spi_lock));
Ben Hutchings8ceee662008-04-27 12:55:59 +01001689
Ben Hutchingsbe4ea892008-12-12 21:33:50 -08001690 /* Check that previous command is not still running */
1691 rc = falcon_spi_poll(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001692 if (rc)
1693 return rc;
1694
Ben Hutchings4a5b5042008-09-01 12:47:16 +01001695 /* Program address register, if we have an address */
1696 if (addressed) {
1697 EFX_POPULATE_OWORD_1(reg, EE_SPI_HADR_ADR, address);
1698 falcon_write(efx, &reg, EE_SPI_HADR_REG_KER);
1699 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01001700
Ben Hutchings4a5b5042008-09-01 12:47:16 +01001701 /* Program data register, if we have data */
1702 if (in != NULL) {
1703 memcpy(&reg, in, len);
1704 falcon_write(efx, &reg, EE_SPI_HDATA_REG_KER);
1705 }
1706
1707 /* Issue read/write command */
Ben Hutchings8ceee662008-04-27 12:55:59 +01001708 EFX_POPULATE_OWORD_7(reg,
1709 EE_SPI_HCMD_CMD_EN, 1,
Ben Hutchings4a5b5042008-09-01 12:47:16 +01001710 EE_SPI_HCMD_SF_SEL, spi->device_id,
Ben Hutchings8ceee662008-04-27 12:55:59 +01001711 EE_SPI_HCMD_DABCNT, len,
Ben Hutchings4a5b5042008-09-01 12:47:16 +01001712 EE_SPI_HCMD_READ, reading,
Ben Hutchings8ceee662008-04-27 12:55:59 +01001713 EE_SPI_HCMD_DUBCNT, 0,
Ben Hutchings4a5b5042008-09-01 12:47:16 +01001714 EE_SPI_HCMD_ADBCNT,
1715 (addressed ? spi->addr_len : 0),
Ben Hutchings8ceee662008-04-27 12:55:59 +01001716 EE_SPI_HCMD_ENC, command);
1717 falcon_write(efx, &reg, EE_SPI_HCMD_REG_KER);
1718
Ben Hutchings4a5b5042008-09-01 12:47:16 +01001719 /* Wait for read/write to complete */
Ben Hutchings8ceee662008-04-27 12:55:59 +01001720 rc = falcon_spi_wait(efx);
1721 if (rc)
1722 return rc;
1723
1724 /* Read data */
Ben Hutchings4a5b5042008-09-01 12:47:16 +01001725 if (out != NULL) {
1726 falcon_read(efx, &reg, EE_SPI_HDATA_REG_KER);
1727 memcpy(out, &reg, len);
1728 }
1729
Ben Hutchings8ceee662008-04-27 12:55:59 +01001730 return 0;
1731}
1732
Ben Hutchings23d30f02008-12-12 21:56:11 -08001733static size_t
1734falcon_spi_write_limit(const struct efx_spi_device *spi, size_t start)
Ben Hutchings4a5b5042008-09-01 12:47:16 +01001735{
1736 return min(FALCON_SPI_MAX_LEN,
1737 (spi->block_size - (start & (spi->block_size - 1))));
1738}
1739
1740static inline u8
1741efx_spi_munge_command(const struct efx_spi_device *spi,
1742 const u8 command, const unsigned int address)
1743{
1744 return command | (((address >> 8) & spi->munge_address) << 3);
1745}
1746
Ben Hutchingsbe4ea892008-12-12 21:33:50 -08001747/* Wait up to 10 ms for buffered write completion */
1748int falcon_spi_wait_write(const struct efx_spi_device *spi)
Ben Hutchings4a5b5042008-09-01 12:47:16 +01001749{
Ben Hutchingsbe4ea892008-12-12 21:33:50 -08001750 struct efx_nic *efx = spi->efx;
1751 unsigned long timeout = jiffies + 1 + DIV_ROUND_UP(HZ, 100);
Ben Hutchings4a5b5042008-09-01 12:47:16 +01001752 u8 status;
Ben Hutchingsbe4ea892008-12-12 21:33:50 -08001753 int rc;
Ben Hutchings4a5b5042008-09-01 12:47:16 +01001754
Ben Hutchingsbe4ea892008-12-12 21:33:50 -08001755 for (;;) {
Ben Hutchings4a5b5042008-09-01 12:47:16 +01001756 rc = falcon_spi_cmd(spi, SPI_RDSR, -1, NULL,
1757 &status, sizeof(status));
1758 if (rc)
1759 return rc;
1760 if (!(status & SPI_STATUS_NRDY))
1761 return 0;
Ben Hutchingsbe4ea892008-12-12 21:33:50 -08001762 if (time_after_eq(jiffies, timeout)) {
1763 EFX_ERR(efx, "SPI write timeout on device %d"
1764 " last status=0x%02x\n",
1765 spi->device_id, status);
1766 return -ETIMEDOUT;
1767 }
1768 schedule_timeout_uninterruptible(1);
Ben Hutchings4a5b5042008-09-01 12:47:16 +01001769 }
Ben Hutchings4a5b5042008-09-01 12:47:16 +01001770}
1771
1772int falcon_spi_read(const struct efx_spi_device *spi, loff_t start,
1773 size_t len, size_t *retlen, u8 *buffer)
1774{
Ben Hutchings23d30f02008-12-12 21:56:11 -08001775 size_t block_len, pos = 0;
1776 unsigned int command;
Ben Hutchings4a5b5042008-09-01 12:47:16 +01001777 int rc = 0;
1778
1779 while (pos < len) {
Ben Hutchings23d30f02008-12-12 21:56:11 -08001780 block_len = min(len - pos, FALCON_SPI_MAX_LEN);
Ben Hutchings4a5b5042008-09-01 12:47:16 +01001781
1782 command = efx_spi_munge_command(spi, SPI_READ, start + pos);
1783 rc = falcon_spi_cmd(spi, command, start + pos, NULL,
1784 buffer + pos, block_len);
1785 if (rc)
1786 break;
1787 pos += block_len;
1788
1789 /* Avoid locking up the system */
1790 cond_resched();
1791 if (signal_pending(current)) {
1792 rc = -EINTR;
1793 break;
1794 }
1795 }
1796
1797 if (retlen)
1798 *retlen = pos;
1799 return rc;
1800}
1801
1802int falcon_spi_write(const struct efx_spi_device *spi, loff_t start,
1803 size_t len, size_t *retlen, const u8 *buffer)
1804{
1805 u8 verify_buffer[FALCON_SPI_MAX_LEN];
Ben Hutchings23d30f02008-12-12 21:56:11 -08001806 size_t block_len, pos = 0;
1807 unsigned int command;
Ben Hutchings4a5b5042008-09-01 12:47:16 +01001808 int rc = 0;
1809
1810 while (pos < len) {
1811 rc = falcon_spi_cmd(spi, SPI_WREN, -1, NULL, NULL, 0);
1812 if (rc)
1813 break;
1814
Ben Hutchings23d30f02008-12-12 21:56:11 -08001815 block_len = min(len - pos,
Ben Hutchings4a5b5042008-09-01 12:47:16 +01001816 falcon_spi_write_limit(spi, start + pos));
1817 command = efx_spi_munge_command(spi, SPI_WRITE, start + pos);
1818 rc = falcon_spi_cmd(spi, command, start + pos,
1819 buffer + pos, NULL, block_len);
1820 if (rc)
1821 break;
1822
Ben Hutchingsbe4ea892008-12-12 21:33:50 -08001823 rc = falcon_spi_wait_write(spi);
Ben Hutchings4a5b5042008-09-01 12:47:16 +01001824 if (rc)
1825 break;
1826
1827 command = efx_spi_munge_command(spi, SPI_READ, start + pos);
1828 rc = falcon_spi_cmd(spi, command, start + pos,
1829 NULL, verify_buffer, block_len);
1830 if (memcmp(verify_buffer, buffer + pos, block_len)) {
1831 rc = -EIO;
1832 break;
1833 }
1834
1835 pos += block_len;
1836
1837 /* Avoid locking up the system */
1838 cond_resched();
1839 if (signal_pending(current)) {
1840 rc = -EINTR;
1841 break;
1842 }
1843 }
1844
1845 if (retlen)
1846 *retlen = pos;
1847 return rc;
1848}
1849
Ben Hutchings8ceee662008-04-27 12:55:59 +01001850/**************************************************************************
1851 *
1852 * MAC wrapper
1853 *
1854 **************************************************************************
1855 */
Ben Hutchings177dfcd2008-12-12 21:50:08 -08001856
1857static int falcon_reset_macs(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001858{
Ben Hutchings177dfcd2008-12-12 21:50:08 -08001859 efx_oword_t reg;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001860 int count;
1861
Ben Hutchings177dfcd2008-12-12 21:50:08 -08001862 if (falcon_rev(efx) < FALCON_REV_B0) {
1863 /* It's not safe to use GLB_CTL_REG to reset the
1864 * macs, so instead use the internal MAC resets
1865 */
1866 if (!EFX_IS10G(efx)) {
1867 EFX_POPULATE_OWORD_1(reg, GM_SW_RST, 1);
1868 falcon_write(efx, &reg, GM_CFG1_REG);
1869 udelay(1000);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001870
Ben Hutchings177dfcd2008-12-12 21:50:08 -08001871 EFX_POPULATE_OWORD_1(reg, GM_SW_RST, 0);
1872 falcon_write(efx, &reg, GM_CFG1_REG);
1873 udelay(1000);
1874 return 0;
1875 } else {
1876 EFX_POPULATE_OWORD_1(reg, XM_CORE_RST, 1);
1877 falcon_write(efx, &reg, XM_GLB_CFG_REG);
1878
1879 for (count = 0; count < 10000; count++) {
1880 falcon_read(efx, &reg, XM_GLB_CFG_REG);
1881 if (EFX_OWORD_FIELD(reg, XM_CORE_RST) == 0)
1882 return 0;
1883 udelay(10);
1884 }
1885
1886 EFX_ERR(efx, "timed out waiting for XMAC core reset\n");
1887 return -ETIMEDOUT;
1888 }
1889 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01001890
1891 /* MAC stats will fail whilst the TX fifo is draining. Serialise
1892 * the drain sequence with the statistics fetch */
Ben Hutchings1974cc22009-01-29 18:00:07 +00001893 efx_stats_disable(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001894
Ben Hutchings177dfcd2008-12-12 21:50:08 -08001895 falcon_read(efx, &reg, MAC0_CTRL_REG_KER);
1896 EFX_SET_OWORD_FIELD(reg, TXFIFO_DRAIN_EN_B0, 1);
1897 falcon_write(efx, &reg, MAC0_CTRL_REG_KER);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001898
Ben Hutchings177dfcd2008-12-12 21:50:08 -08001899 falcon_read(efx, &reg, GLB_CTL_REG_KER);
1900 EFX_SET_OWORD_FIELD(reg, RST_XGTX, 1);
1901 EFX_SET_OWORD_FIELD(reg, RST_XGRX, 1);
1902 EFX_SET_OWORD_FIELD(reg, RST_EM, 1);
1903 falcon_write(efx, &reg, GLB_CTL_REG_KER);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001904
1905 count = 0;
1906 while (1) {
Ben Hutchings177dfcd2008-12-12 21:50:08 -08001907 falcon_read(efx, &reg, GLB_CTL_REG_KER);
1908 if (!EFX_OWORD_FIELD(reg, RST_XGTX) &&
1909 !EFX_OWORD_FIELD(reg, RST_XGRX) &&
1910 !EFX_OWORD_FIELD(reg, RST_EM)) {
Ben Hutchings8ceee662008-04-27 12:55:59 +01001911 EFX_LOG(efx, "Completed MAC reset after %d loops\n",
1912 count);
1913 break;
1914 }
1915 if (count > 20) {
1916 EFX_ERR(efx, "MAC reset failed\n");
1917 break;
1918 }
1919 count++;
1920 udelay(10);
1921 }
1922
Ben Hutchings1974cc22009-01-29 18:00:07 +00001923 efx_stats_enable(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001924
1925 /* If we've reset the EM block and the link is up, then
1926 * we'll have to kick the XAUI link so the PHY can recover */
Ben Hutchings177dfcd2008-12-12 21:50:08 -08001927 if (efx->link_up && EFX_IS10G(efx) && EFX_WORKAROUND_5147(efx))
Ben Hutchings8ceee662008-04-27 12:55:59 +01001928 falcon_reset_xaui(efx);
Ben Hutchings177dfcd2008-12-12 21:50:08 -08001929
1930 return 0;
1931}
1932
1933void falcon_drain_tx_fifo(struct efx_nic *efx)
1934{
1935 efx_oword_t reg;
1936
1937 if ((falcon_rev(efx) < FALCON_REV_B0) ||
1938 (efx->loopback_mode != LOOPBACK_NONE))
1939 return;
1940
1941 falcon_read(efx, &reg, MAC0_CTRL_REG_KER);
1942 /* There is no point in draining more than once */
1943 if (EFX_OWORD_FIELD(reg, TXFIFO_DRAIN_EN_B0))
1944 return;
1945
1946 falcon_reset_macs(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001947}
1948
1949void falcon_deconfigure_mac_wrapper(struct efx_nic *efx)
1950{
Ben Hutchings177dfcd2008-12-12 21:50:08 -08001951 efx_oword_t reg;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001952
Ben Hutchings55668612008-05-16 21:16:10 +01001953 if (falcon_rev(efx) < FALCON_REV_B0)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001954 return;
1955
1956 /* Isolate the MAC -> RX */
Ben Hutchings177dfcd2008-12-12 21:50:08 -08001957 falcon_read(efx, &reg, RX_CFG_REG_KER);
1958 EFX_SET_OWORD_FIELD(reg, RX_INGR_EN_B0, 0);
1959 falcon_write(efx, &reg, RX_CFG_REG_KER);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001960
1961 if (!efx->link_up)
1962 falcon_drain_tx_fifo(efx);
1963}
1964
1965void falcon_reconfigure_mac_wrapper(struct efx_nic *efx)
1966{
1967 efx_oword_t reg;
1968 int link_speed;
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +01001969 bool tx_fc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001970
Ben Hutchingsf31a45d2008-12-12 21:43:33 -08001971 switch (efx->link_speed) {
1972 case 10000: link_speed = 3; break;
1973 case 1000: link_speed = 2; break;
1974 case 100: link_speed = 1; break;
1975 default: link_speed = 0; break;
1976 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01001977 /* MAC_LINK_STATUS controls MAC backpressure but doesn't work
1978 * as advertised. Disable to ensure packets are not
1979 * indefinitely held and TX queue can be flushed at any point
1980 * while the link is down. */
1981 EFX_POPULATE_OWORD_5(reg,
1982 MAC_XOFF_VAL, 0xffff /* max pause time */,
1983 MAC_BCAD_ACPT, 1,
1984 MAC_UC_PROM, efx->promiscuous,
1985 MAC_LINK_STATUS, 1, /* always set */
1986 MAC_SPEED, link_speed);
1987 /* On B0, MAC backpressure can be disabled and packets get
1988 * discarded. */
Ben Hutchings55668612008-05-16 21:16:10 +01001989 if (falcon_rev(efx) >= FALCON_REV_B0) {
Ben Hutchings8ceee662008-04-27 12:55:59 +01001990 EFX_SET_OWORD_FIELD(reg, TXFIFO_DRAIN_EN_B0,
1991 !efx->link_up);
1992 }
1993
1994 falcon_write(efx, &reg, MAC0_CTRL_REG_KER);
1995
1996 /* Restore the multicast hash registers. */
1997 falcon_set_multicast_hash(efx);
1998
1999 /* Transmission of pause frames when RX crosses the threshold is
2000 * covered by RX_XOFF_MAC_EN and XM_TX_CFG_REG:XM_FCNTL.
2001 * Action on receipt of pause frames is controller by XM_DIS_FCNTL */
Ben Hutchings04cc8ca2008-12-12 21:50:46 -08002002 tx_fc = !!(efx->link_fc & EFX_FC_TX);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002003 falcon_read(efx, &reg, RX_CFG_REG_KER);
2004 EFX_SET_OWORD_FIELD_VER(efx, reg, RX_XOFF_MAC_EN, tx_fc);
2005
2006 /* Unisolate the MAC -> RX */
Ben Hutchings55668612008-05-16 21:16:10 +01002007 if (falcon_rev(efx) >= FALCON_REV_B0)
Ben Hutchings8ceee662008-04-27 12:55:59 +01002008 EFX_SET_OWORD_FIELD(reg, RX_INGR_EN_B0, 1);
2009 falcon_write(efx, &reg, RX_CFG_REG_KER);
2010}
2011
2012int falcon_dma_stats(struct efx_nic *efx, unsigned int done_offset)
2013{
2014 efx_oword_t reg;
2015 u32 *dma_done;
2016 int i;
2017
2018 if (disable_dma_stats)
2019 return 0;
2020
2021 /* Statistics fetch will fail if the MAC is in TX drain */
Ben Hutchings55668612008-05-16 21:16:10 +01002022 if (falcon_rev(efx) >= FALCON_REV_B0) {
Ben Hutchings8ceee662008-04-27 12:55:59 +01002023 efx_oword_t temp;
2024 falcon_read(efx, &temp, MAC0_CTRL_REG_KER);
2025 if (EFX_OWORD_FIELD(temp, TXFIFO_DRAIN_EN_B0))
2026 return 0;
2027 }
2028
2029 dma_done = (efx->stats_buffer.addr + done_offset);
2030 *dma_done = FALCON_STATS_NOT_DONE;
2031 wmb(); /* ensure done flag is clear */
2032
2033 /* Initiate DMA transfer of stats */
2034 EFX_POPULATE_OWORD_2(reg,
2035 MAC_STAT_DMA_CMD, 1,
2036 MAC_STAT_DMA_ADR,
2037 efx->stats_buffer.dma_addr);
2038 falcon_write(efx, &reg, MAC0_STAT_DMA_REG_KER);
2039
2040 /* Wait for transfer to complete */
2041 for (i = 0; i < 400; i++) {
Ben Hutchings1d0680f2008-09-01 12:50:08 +01002042 if (*(volatile u32 *)dma_done == FALCON_STATS_DONE) {
2043 rmb(); /* Ensure the stats are valid. */
Ben Hutchings8ceee662008-04-27 12:55:59 +01002044 return 0;
Ben Hutchings1d0680f2008-09-01 12:50:08 +01002045 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01002046 udelay(10);
2047 }
2048
2049 EFX_ERR(efx, "timed out waiting for statistics\n");
2050 return -ETIMEDOUT;
2051}
2052
2053/**************************************************************************
2054 *
2055 * PHY access via GMII
2056 *
2057 **************************************************************************
2058 */
2059
2060/* Use the top bit of the MII PHY id to indicate the PHY type
2061 * (1G/10G), with the remaining bits as the actual PHY id.
2062 *
2063 * This allows us to avoid leaking information from the mii_if_info
2064 * structure into other data structures.
2065 */
2066#define FALCON_PHY_ID_ID_WIDTH EFX_WIDTH(MD_PRT_DEV_ADR)
2067#define FALCON_PHY_ID_ID_MASK ((1 << FALCON_PHY_ID_ID_WIDTH) - 1)
2068#define FALCON_PHY_ID_WIDTH (FALCON_PHY_ID_ID_WIDTH + 1)
2069#define FALCON_PHY_ID_MASK ((1 << FALCON_PHY_ID_WIDTH) - 1)
2070#define FALCON_PHY_ID_10G (1 << (FALCON_PHY_ID_WIDTH - 1))
2071
2072
2073/* Packing the clause 45 port and device fields into a single value */
2074#define MD_PRT_ADR_COMP_LBN (MD_PRT_ADR_LBN - MD_DEV_ADR_LBN)
2075#define MD_PRT_ADR_COMP_WIDTH MD_PRT_ADR_WIDTH
2076#define MD_DEV_ADR_COMP_LBN 0
2077#define MD_DEV_ADR_COMP_WIDTH MD_DEV_ADR_WIDTH
2078
2079
2080/* Wait for GMII access to complete */
2081static int falcon_gmii_wait(struct efx_nic *efx)
2082{
2083 efx_dword_t md_stat;
2084 int count;
2085
Ben Hutchings177dfcd2008-12-12 21:50:08 -08002086 /* wait upto 50ms - taken max from datasheet */
2087 for (count = 0; count < 5000; count++) {
Ben Hutchings8ceee662008-04-27 12:55:59 +01002088 falcon_readl(efx, &md_stat, MD_STAT_REG_KER);
2089 if (EFX_DWORD_FIELD(md_stat, MD_BSY) == 0) {
2090 if (EFX_DWORD_FIELD(md_stat, MD_LNFL) != 0 ||
2091 EFX_DWORD_FIELD(md_stat, MD_BSERR) != 0) {
2092 EFX_ERR(efx, "error from GMII access "
2093 EFX_DWORD_FMT"\n",
2094 EFX_DWORD_VAL(md_stat));
2095 return -EIO;
2096 }
2097 return 0;
2098 }
2099 udelay(10);
2100 }
2101 EFX_ERR(efx, "timed out waiting for GMII\n");
2102 return -ETIMEDOUT;
2103}
2104
2105/* Writes a GMII register of a PHY connected to Falcon using MDIO. */
2106static void falcon_mdio_write(struct net_device *net_dev, int phy_id,
2107 int addr, int value)
2108{
Ben Hutchings767e4682008-09-01 12:43:14 +01002109 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002110 unsigned int phy_id2 = phy_id & FALCON_PHY_ID_ID_MASK;
2111 efx_oword_t reg;
2112
2113 /* The 'generic' prt/dev packing in mdio_10g.h is conveniently
2114 * chosen so that the only current user, Falcon, can take the
2115 * packed value and use them directly.
2116 * Fail to build if this assumption is broken.
2117 */
2118 BUILD_BUG_ON(FALCON_PHY_ID_10G != MDIO45_XPRT_ID_IS10G);
2119 BUILD_BUG_ON(FALCON_PHY_ID_ID_WIDTH != MDIO45_PRT_DEV_WIDTH);
2120 BUILD_BUG_ON(MD_PRT_ADR_COMP_LBN != MDIO45_PRT_ID_COMP_LBN);
2121 BUILD_BUG_ON(MD_DEV_ADR_COMP_LBN != MDIO45_DEV_ID_COMP_LBN);
2122
2123 if (phy_id2 == PHY_ADDR_INVALID)
2124 return;
2125
2126 /* See falcon_mdio_read for an explanation. */
2127 if (!(phy_id & FALCON_PHY_ID_10G)) {
2128 int mmd = ffs(efx->phy_op->mmds) - 1;
2129 EFX_TRACE(efx, "Fixing erroneous clause22 write\n");
2130 phy_id2 = mdio_clause45_pack(phy_id2, mmd)
2131 & FALCON_PHY_ID_ID_MASK;
2132 }
2133
2134 EFX_REGDUMP(efx, "writing GMII %d register %02x with %04x\n", phy_id,
2135 addr, value);
2136
2137 spin_lock_bh(&efx->phy_lock);
2138
2139 /* Check MII not currently being accessed */
2140 if (falcon_gmii_wait(efx) != 0)
2141 goto out;
2142
2143 /* Write the address/ID register */
2144 EFX_POPULATE_OWORD_1(reg, MD_PHY_ADR, addr);
2145 falcon_write(efx, &reg, MD_PHY_ADR_REG_KER);
2146
2147 EFX_POPULATE_OWORD_1(reg, MD_PRT_DEV_ADR, phy_id2);
2148 falcon_write(efx, &reg, MD_ID_REG_KER);
2149
2150 /* Write data */
2151 EFX_POPULATE_OWORD_1(reg, MD_TXD, value);
2152 falcon_write(efx, &reg, MD_TXD_REG_KER);
2153
2154 EFX_POPULATE_OWORD_2(reg,
2155 MD_WRC, 1,
2156 MD_GC, 0);
2157 falcon_write(efx, &reg, MD_CS_REG_KER);
2158
2159 /* Wait for data to be written */
2160 if (falcon_gmii_wait(efx) != 0) {
2161 /* Abort the write operation */
2162 EFX_POPULATE_OWORD_2(reg,
2163 MD_WRC, 0,
2164 MD_GC, 1);
2165 falcon_write(efx, &reg, MD_CS_REG_KER);
2166 udelay(10);
2167 }
2168
2169 out:
2170 spin_unlock_bh(&efx->phy_lock);
2171}
2172
2173/* Reads a GMII register from a PHY connected to Falcon. If no value
2174 * could be read, -1 will be returned. */
2175static int falcon_mdio_read(struct net_device *net_dev, int phy_id, int addr)
2176{
Ben Hutchings767e4682008-09-01 12:43:14 +01002177 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002178 unsigned int phy_addr = phy_id & FALCON_PHY_ID_ID_MASK;
2179 efx_oword_t reg;
2180 int value = -1;
2181
2182 if (phy_addr == PHY_ADDR_INVALID)
2183 return -1;
2184
2185 /* Our PHY code knows whether it needs to talk clause 22(1G) or 45(10G)
2186 * but the generic Linux code does not make any distinction or have
2187 * any state for this.
2188 * We spot the case where someone tried to talk 22 to a 45 PHY and
2189 * redirect the request to the lowest numbered MMD as a clause45
2190 * request. This is enough to allow simple queries like id and link
2191 * state to succeed. TODO: We may need to do more in future.
2192 */
2193 if (!(phy_id & FALCON_PHY_ID_10G)) {
2194 int mmd = ffs(efx->phy_op->mmds) - 1;
2195 EFX_TRACE(efx, "Fixing erroneous clause22 read\n");
2196 phy_addr = mdio_clause45_pack(phy_addr, mmd)
2197 & FALCON_PHY_ID_ID_MASK;
2198 }
2199
2200 spin_lock_bh(&efx->phy_lock);
2201
2202 /* Check MII not currently being accessed */
2203 if (falcon_gmii_wait(efx) != 0)
2204 goto out;
2205
2206 EFX_POPULATE_OWORD_1(reg, MD_PHY_ADR, addr);
2207 falcon_write(efx, &reg, MD_PHY_ADR_REG_KER);
2208
2209 EFX_POPULATE_OWORD_1(reg, MD_PRT_DEV_ADR, phy_addr);
2210 falcon_write(efx, &reg, MD_ID_REG_KER);
2211
2212 /* Request data to be read */
2213 EFX_POPULATE_OWORD_2(reg, MD_RDC, 1, MD_GC, 0);
2214 falcon_write(efx, &reg, MD_CS_REG_KER);
2215
2216 /* Wait for data to become available */
2217 value = falcon_gmii_wait(efx);
2218 if (value == 0) {
2219 falcon_read(efx, &reg, MD_RXD_REG_KER);
2220 value = EFX_OWORD_FIELD(reg, MD_RXD);
2221 EFX_REGDUMP(efx, "read from GMII %d register %02x, got %04x\n",
2222 phy_id, addr, value);
2223 } else {
2224 /* Abort the read operation */
2225 EFX_POPULATE_OWORD_2(reg,
2226 MD_RIC, 0,
2227 MD_GC, 1);
2228 falcon_write(efx, &reg, MD_CS_REG_KER);
2229
2230 EFX_LOG(efx, "read from GMII 0x%x register %02x, got "
2231 "error %d\n", phy_id, addr, value);
2232 }
2233
2234 out:
2235 spin_unlock_bh(&efx->phy_lock);
2236
2237 return value;
2238}
2239
2240static void falcon_init_mdio(struct mii_if_info *gmii)
2241{
2242 gmii->mdio_read = falcon_mdio_read;
2243 gmii->mdio_write = falcon_mdio_write;
2244 gmii->phy_id_mask = FALCON_PHY_ID_MASK;
2245 gmii->reg_num_mask = ((1 << EFX_WIDTH(MD_PHY_ADR)) - 1);
2246}
2247
2248static int falcon_probe_phy(struct efx_nic *efx)
2249{
2250 switch (efx->phy_type) {
Ben Hutchingse6fa2eb2008-12-12 22:00:17 -08002251 case PHY_TYPE_SFX7101:
2252 efx->phy_op = &falcon_sfx7101_phy_ops;
2253 break;
2254 case PHY_TYPE_SFT9001A:
2255 case PHY_TYPE_SFT9001B:
2256 efx->phy_op = &falcon_sft9001_phy_ops;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002257 break;
Ben Hutchingsab377352008-12-12 22:06:54 -08002258 case PHY_TYPE_QT2022C2:
Ben Hutchingsd2d2c372009-02-27 13:07:33 +00002259 case PHY_TYPE_QT2025C:
Ben Hutchings8ceee662008-04-27 12:55:59 +01002260 efx->phy_op = &falcon_xfp_phy_ops;
2261 break;
2262 default:
2263 EFX_ERR(efx, "Unknown PHY type %d\n",
2264 efx->phy_type);
2265 return -1;
2266 }
Ben Hutchings3273c2e2008-05-07 13:36:19 +01002267
Ben Hutchings177dfcd2008-12-12 21:50:08 -08002268 if (efx->phy_op->macs & EFX_XMAC)
2269 efx->loopback_modes |= ((1 << LOOPBACK_XGMII) |
2270 (1 << LOOPBACK_XGXS) |
2271 (1 << LOOPBACK_XAUI));
2272 if (efx->phy_op->macs & EFX_GMAC)
2273 efx->loopback_modes |= (1 << LOOPBACK_GMAC);
2274 efx->loopback_modes |= efx->phy_op->loopbacks;
2275
Ben Hutchings8ceee662008-04-27 12:55:59 +01002276 return 0;
2277}
2278
Ben Hutchings177dfcd2008-12-12 21:50:08 -08002279int falcon_switch_mac(struct efx_nic *efx)
2280{
2281 struct efx_mac_operations *old_mac_op = efx->mac_op;
2282 efx_oword_t nic_stat;
2283 unsigned strap_val;
Ben Hutchings1974cc22009-01-29 18:00:07 +00002284 int rc = 0;
2285
2286 /* Don't try to fetch MAC stats while we're switching MACs */
2287 efx_stats_disable(efx);
Ben Hutchings177dfcd2008-12-12 21:50:08 -08002288
2289 /* Internal loopbacks override the phy speed setting */
2290 if (efx->loopback_mode == LOOPBACK_GMAC) {
2291 efx->link_speed = 1000;
2292 efx->link_fd = true;
2293 } else if (LOOPBACK_INTERNAL(efx)) {
2294 efx->link_speed = 10000;
2295 efx->link_fd = true;
2296 }
2297
Steve Hodgson0cc1283872009-01-29 17:49:59 +00002298 WARN_ON(!mutex_is_locked(&efx->mac_lock));
Ben Hutchings177dfcd2008-12-12 21:50:08 -08002299 efx->mac_op = (EFX_IS10G(efx) ?
2300 &falcon_xmac_operations : &falcon_gmac_operations);
Ben Hutchings177dfcd2008-12-12 21:50:08 -08002301
Steve Hodgson0cc1283872009-01-29 17:49:59 +00002302 /* Always push the NIC_STAT_REG setting even if the mac hasn't
2303 * changed, because this function is run post online reset */
Ben Hutchings177dfcd2008-12-12 21:50:08 -08002304 falcon_read(efx, &nic_stat, NIC_STAT_REG);
2305 strap_val = EFX_IS10G(efx) ? 5 : 3;
2306 if (falcon_rev(efx) >= FALCON_REV_B0) {
2307 EFX_SET_OWORD_FIELD(nic_stat, EE_STRAP_EN, 1);
2308 EFX_SET_OWORD_FIELD(nic_stat, EE_STRAP_OVR, strap_val);
2309 falcon_write(efx, &nic_stat, NIC_STAT_REG);
2310 } else {
2311 /* Falcon A1 does not support 1G/10G speed switching
2312 * and must not be used with a PHY that does. */
2313 BUG_ON(EFX_OWORD_FIELD(nic_stat, STRAP_PINS) != strap_val);
2314 }
2315
Steve Hodgson0cc1283872009-01-29 17:49:59 +00002316 if (old_mac_op == efx->mac_op)
Ben Hutchings1974cc22009-01-29 18:00:07 +00002317 goto out;
Ben Hutchings177dfcd2008-12-12 21:50:08 -08002318
2319 EFX_LOG(efx, "selected %cMAC\n", EFX_IS10G(efx) ? 'X' : 'G');
Steve Hodgson0cc1283872009-01-29 17:49:59 +00002320 /* Not all macs support a mac-level link state */
2321 efx->mac_up = true;
2322
Ben Hutchings1974cc22009-01-29 18:00:07 +00002323 rc = falcon_reset_macs(efx);
2324out:
2325 efx_stats_enable(efx);
2326 return rc;
Ben Hutchings177dfcd2008-12-12 21:50:08 -08002327}
2328
Ben Hutchings8ceee662008-04-27 12:55:59 +01002329/* This call is responsible for hooking in the MAC and PHY operations */
2330int falcon_probe_port(struct efx_nic *efx)
2331{
2332 int rc;
2333
2334 /* Hook in PHY operations table */
2335 rc = falcon_probe_phy(efx);
2336 if (rc)
2337 return rc;
2338
2339 /* Set up GMII structure for PHY */
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +01002340 efx->mii.supports_gmii = true;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002341 falcon_init_mdio(&efx->mii);
2342
2343 /* Hardware flow ctrl. FalconA RX FIFO too small for pause generation */
Ben Hutchings55668612008-05-16 21:16:10 +01002344 if (falcon_rev(efx) >= FALCON_REV_B0)
Ben Hutchings04cc8ca2008-12-12 21:50:46 -08002345 efx->wanted_fc = EFX_FC_RX | EFX_FC_TX;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002346 else
Ben Hutchings04cc8ca2008-12-12 21:50:46 -08002347 efx->wanted_fc = EFX_FC_RX;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002348
2349 /* Allocate buffer for stats */
2350 rc = falcon_alloc_buffer(efx, &efx->stats_buffer,
2351 FALCON_MAC_STATS_SIZE);
2352 if (rc)
2353 return rc;
2354 EFX_LOG(efx, "stats buffer at %llx (virt %p phys %lx)\n",
2355 (unsigned long long)efx->stats_buffer.dma_addr,
2356 efx->stats_buffer.addr,
2357 virt_to_phys(efx->stats_buffer.addr));
2358
2359 return 0;
2360}
2361
2362void falcon_remove_port(struct efx_nic *efx)
2363{
2364 falcon_free_buffer(efx, &efx->stats_buffer);
2365}
2366
2367/**************************************************************************
2368 *
2369 * Multicast filtering
2370 *
2371 **************************************************************************
2372 */
2373
2374void falcon_set_multicast_hash(struct efx_nic *efx)
2375{
2376 union efx_multicast_hash *mc_hash = &efx->multicast_hash;
2377
2378 /* Broadcast packets go through the multicast hash filter.
2379 * ether_crc_le() of the broadcast address is 0xbe2612ff
2380 * so we always add bit 0xff to the mask.
2381 */
2382 set_bit_le(0xff, mc_hash->byte);
2383
2384 falcon_write(efx, &mc_hash->oword[0], MAC_MCAST_HASH_REG0_KER);
2385 falcon_write(efx, &mc_hash->oword[1], MAC_MCAST_HASH_REG1_KER);
2386}
2387
Ben Hutchings8c8661e2008-09-01 12:49:02 +01002388
2389/**************************************************************************
2390 *
2391 * Falcon test code
2392 *
2393 **************************************************************************/
2394
2395int falcon_read_nvram(struct efx_nic *efx, struct falcon_nvconfig *nvconfig_out)
2396{
2397 struct falcon_nvconfig *nvconfig;
2398 struct efx_spi_device *spi;
2399 void *region;
2400 int rc, magic_num, struct_ver;
2401 __le16 *word, *limit;
2402 u32 csum;
2403
Ben Hutchings2f7f5732008-12-12 21:34:25 -08002404 spi = efx->spi_flash ? efx->spi_flash : efx->spi_eeprom;
2405 if (!spi)
2406 return -EINVAL;
2407
Ben Hutchings0a95f562008-11-04 20:33:11 +00002408 region = kmalloc(FALCON_NVCONFIG_END, GFP_KERNEL);
Ben Hutchings8c8661e2008-09-01 12:49:02 +01002409 if (!region)
2410 return -ENOMEM;
2411 nvconfig = region + NVCONFIG_OFFSET;
2412
Ben Hutchingsf4150722008-11-04 20:34:28 +00002413 mutex_lock(&efx->spi_lock);
Ben Hutchings0a95f562008-11-04 20:33:11 +00002414 rc = falcon_spi_read(spi, 0, FALCON_NVCONFIG_END, NULL, region);
Ben Hutchingsf4150722008-11-04 20:34:28 +00002415 mutex_unlock(&efx->spi_lock);
Ben Hutchings8c8661e2008-09-01 12:49:02 +01002416 if (rc) {
2417 EFX_ERR(efx, "Failed to read %s\n",
2418 efx->spi_flash ? "flash" : "EEPROM");
2419 rc = -EIO;
2420 goto out;
2421 }
2422
2423 magic_num = le16_to_cpu(nvconfig->board_magic_num);
2424 struct_ver = le16_to_cpu(nvconfig->board_struct_ver);
2425
2426 rc = -EINVAL;
2427 if (magic_num != NVCONFIG_BOARD_MAGIC_NUM) {
2428 EFX_ERR(efx, "NVRAM bad magic 0x%x\n", magic_num);
2429 goto out;
2430 }
2431 if (struct_ver < 2) {
2432 EFX_ERR(efx, "NVRAM has ancient version 0x%x\n", struct_ver);
2433 goto out;
2434 } else if (struct_ver < 4) {
2435 word = &nvconfig->board_magic_num;
2436 limit = (__le16 *) (nvconfig + 1);
2437 } else {
2438 word = region;
Ben Hutchings0a95f562008-11-04 20:33:11 +00002439 limit = region + FALCON_NVCONFIG_END;
Ben Hutchings8c8661e2008-09-01 12:49:02 +01002440 }
2441 for (csum = 0; word < limit; ++word)
2442 csum += le16_to_cpu(*word);
2443
2444 if (~csum & 0xffff) {
2445 EFX_ERR(efx, "NVRAM has incorrect checksum\n");
2446 goto out;
2447 }
2448
2449 rc = 0;
2450 if (nvconfig_out)
2451 memcpy(nvconfig_out, nvconfig, sizeof(*nvconfig));
2452
2453 out:
2454 kfree(region);
2455 return rc;
2456}
2457
2458/* Registers tested in the falcon register test */
2459static struct {
2460 unsigned address;
2461 efx_oword_t mask;
2462} efx_test_registers[] = {
2463 { ADR_REGION_REG_KER,
2464 EFX_OWORD32(0x0001FFFF, 0x0001FFFF, 0x0001FFFF, 0x0001FFFF) },
2465 { RX_CFG_REG_KER,
2466 EFX_OWORD32(0xFFFFFFFE, 0x00017FFF, 0x00000000, 0x00000000) },
2467 { TX_CFG_REG_KER,
2468 EFX_OWORD32(0x7FFF0037, 0x00000000, 0x00000000, 0x00000000) },
2469 { TX_CFG2_REG_KER,
2470 EFX_OWORD32(0xFFFEFE80, 0x1FFFFFFF, 0x020000FE, 0x007FFFFF) },
2471 { MAC0_CTRL_REG_KER,
2472 EFX_OWORD32(0xFFFF0000, 0x00000000, 0x00000000, 0x00000000) },
2473 { SRM_TX_DC_CFG_REG_KER,
2474 EFX_OWORD32(0x001FFFFF, 0x00000000, 0x00000000, 0x00000000) },
2475 { RX_DC_CFG_REG_KER,
2476 EFX_OWORD32(0x0000000F, 0x00000000, 0x00000000, 0x00000000) },
2477 { RX_DC_PF_WM_REG_KER,
2478 EFX_OWORD32(0x000003FF, 0x00000000, 0x00000000, 0x00000000) },
2479 { DP_CTRL_REG,
2480 EFX_OWORD32(0x00000FFF, 0x00000000, 0x00000000, 0x00000000) },
Ben Hutchings177dfcd2008-12-12 21:50:08 -08002481 { GM_CFG2_REG,
2482 EFX_OWORD32(0x00007337, 0x00000000, 0x00000000, 0x00000000) },
2483 { GMF_CFG0_REG,
2484 EFX_OWORD32(0x00001F1F, 0x00000000, 0x00000000, 0x00000000) },
Ben Hutchings8c8661e2008-09-01 12:49:02 +01002485 { XM_GLB_CFG_REG,
2486 EFX_OWORD32(0x00000C68, 0x00000000, 0x00000000, 0x00000000) },
2487 { XM_TX_CFG_REG,
2488 EFX_OWORD32(0x00080164, 0x00000000, 0x00000000, 0x00000000) },
2489 { XM_RX_CFG_REG,
2490 EFX_OWORD32(0x07100A0C, 0x00000000, 0x00000000, 0x00000000) },
2491 { XM_RX_PARAM_REG,
2492 EFX_OWORD32(0x00001FF8, 0x00000000, 0x00000000, 0x00000000) },
2493 { XM_FC_REG,
2494 EFX_OWORD32(0xFFFF0001, 0x00000000, 0x00000000, 0x00000000) },
2495 { XM_ADR_LO_REG,
2496 EFX_OWORD32(0xFFFFFFFF, 0x00000000, 0x00000000, 0x00000000) },
2497 { XX_SD_CTL_REG,
2498 EFX_OWORD32(0x0003FF0F, 0x00000000, 0x00000000, 0x00000000) },
2499};
2500
2501static bool efx_masked_compare_oword(const efx_oword_t *a, const efx_oword_t *b,
2502 const efx_oword_t *mask)
2503{
2504 return ((a->u64[0] ^ b->u64[0]) & mask->u64[0]) ||
2505 ((a->u64[1] ^ b->u64[1]) & mask->u64[1]);
2506}
2507
2508int falcon_test_registers(struct efx_nic *efx)
2509{
2510 unsigned address = 0, i, j;
2511 efx_oword_t mask, imask, original, reg, buf;
2512
2513 /* Falcon should be in loopback to isolate the XMAC from the PHY */
2514 WARN_ON(!LOOPBACK_INTERNAL(efx));
2515
2516 for (i = 0; i < ARRAY_SIZE(efx_test_registers); ++i) {
2517 address = efx_test_registers[i].address;
2518 mask = imask = efx_test_registers[i].mask;
2519 EFX_INVERT_OWORD(imask);
2520
2521 falcon_read(efx, &original, address);
2522
2523 /* bit sweep on and off */
2524 for (j = 0; j < 128; j++) {
2525 if (!EFX_EXTRACT_OWORD32(mask, j, j))
2526 continue;
2527
2528 /* Test this testable bit can be set in isolation */
2529 EFX_AND_OWORD(reg, original, mask);
2530 EFX_SET_OWORD32(reg, j, j, 1);
2531
2532 falcon_write(efx, &reg, address);
2533 falcon_read(efx, &buf, address);
2534
2535 if (efx_masked_compare_oword(&reg, &buf, &mask))
2536 goto fail;
2537
2538 /* Test this testable bit can be cleared in isolation */
2539 EFX_OR_OWORD(reg, original, mask);
2540 EFX_SET_OWORD32(reg, j, j, 0);
2541
2542 falcon_write(efx, &reg, address);
2543 falcon_read(efx, &buf, address);
2544
2545 if (efx_masked_compare_oword(&reg, &buf, &mask))
2546 goto fail;
2547 }
2548
2549 falcon_write(efx, &original, address);
2550 }
2551
2552 return 0;
2553
2554fail:
2555 EFX_ERR(efx, "wrote "EFX_OWORD_FMT" read "EFX_OWORD_FMT
2556 " at address 0x%x mask "EFX_OWORD_FMT"\n", EFX_OWORD_VAL(reg),
2557 EFX_OWORD_VAL(buf), address, EFX_OWORD_VAL(mask));
2558 return -EIO;
2559}
2560
Ben Hutchings8ceee662008-04-27 12:55:59 +01002561/**************************************************************************
2562 *
2563 * Device reset
2564 *
2565 **************************************************************************
2566 */
2567
2568/* Resets NIC to known state. This routine must be called in process
2569 * context and is allowed to sleep. */
2570int falcon_reset_hw(struct efx_nic *efx, enum reset_type method)
2571{
2572 struct falcon_nic_data *nic_data = efx->nic_data;
2573 efx_oword_t glb_ctl_reg_ker;
2574 int rc;
2575
2576 EFX_LOG(efx, "performing hardware reset (%d)\n", method);
2577
2578 /* Initiate device reset */
2579 if (method == RESET_TYPE_WORLD) {
2580 rc = pci_save_state(efx->pci_dev);
2581 if (rc) {
2582 EFX_ERR(efx, "failed to backup PCI state of primary "
2583 "function prior to hardware reset\n");
2584 goto fail1;
2585 }
2586 if (FALCON_IS_DUAL_FUNC(efx)) {
2587 rc = pci_save_state(nic_data->pci_dev2);
2588 if (rc) {
2589 EFX_ERR(efx, "failed to backup PCI state of "
2590 "secondary function prior to "
2591 "hardware reset\n");
2592 goto fail2;
2593 }
2594 }
2595
2596 EFX_POPULATE_OWORD_2(glb_ctl_reg_ker,
2597 EXT_PHY_RST_DUR, 0x7,
2598 SWRST, 1);
2599 } else {
2600 int reset_phy = (method == RESET_TYPE_INVISIBLE ?
2601 EXCLUDE_FROM_RESET : 0);
2602
2603 EFX_POPULATE_OWORD_7(glb_ctl_reg_ker,
2604 EXT_PHY_RST_CTL, reset_phy,
2605 PCIE_CORE_RST_CTL, EXCLUDE_FROM_RESET,
2606 PCIE_NSTCK_RST_CTL, EXCLUDE_FROM_RESET,
2607 PCIE_SD_RST_CTL, EXCLUDE_FROM_RESET,
2608 EE_RST_CTL, EXCLUDE_FROM_RESET,
2609 EXT_PHY_RST_DUR, 0x7 /* 10ms */,
2610 SWRST, 1);
2611 }
2612 falcon_write(efx, &glb_ctl_reg_ker, GLB_CTL_REG_KER);
2613
2614 EFX_LOG(efx, "waiting for hardware reset\n");
2615 schedule_timeout_uninterruptible(HZ / 20);
2616
2617 /* Restore PCI configuration if needed */
2618 if (method == RESET_TYPE_WORLD) {
2619 if (FALCON_IS_DUAL_FUNC(efx)) {
2620 rc = pci_restore_state(nic_data->pci_dev2);
2621 if (rc) {
2622 EFX_ERR(efx, "failed to restore PCI config for "
2623 "the secondary function\n");
2624 goto fail3;
2625 }
2626 }
2627 rc = pci_restore_state(efx->pci_dev);
2628 if (rc) {
2629 EFX_ERR(efx, "failed to restore PCI config for the "
2630 "primary function\n");
2631 goto fail4;
2632 }
2633 EFX_LOG(efx, "successfully restored PCI config\n");
2634 }
2635
2636 /* Assert that reset complete */
2637 falcon_read(efx, &glb_ctl_reg_ker, GLB_CTL_REG_KER);
2638 if (EFX_OWORD_FIELD(glb_ctl_reg_ker, SWRST) != 0) {
2639 rc = -ETIMEDOUT;
2640 EFX_ERR(efx, "timed out waiting for hardware reset\n");
2641 goto fail5;
2642 }
2643 EFX_LOG(efx, "hardware reset complete\n");
2644
2645 return 0;
2646
2647 /* pci_save_state() and pci_restore_state() MUST be called in pairs */
2648fail2:
2649fail3:
2650 pci_restore_state(efx->pci_dev);
2651fail1:
2652fail4:
2653fail5:
2654 return rc;
2655}
2656
2657/* Zeroes out the SRAM contents. This routine must be called in
2658 * process context and is allowed to sleep.
2659 */
2660static int falcon_reset_sram(struct efx_nic *efx)
2661{
2662 efx_oword_t srm_cfg_reg_ker, gpio_cfg_reg_ker;
2663 int count;
2664
2665 /* Set the SRAM wake/sleep GPIO appropriately. */
2666 falcon_read(efx, &gpio_cfg_reg_ker, GPIO_CTL_REG_KER);
2667 EFX_SET_OWORD_FIELD(gpio_cfg_reg_ker, GPIO1_OEN, 1);
2668 EFX_SET_OWORD_FIELD(gpio_cfg_reg_ker, GPIO1_OUT, 1);
2669 falcon_write(efx, &gpio_cfg_reg_ker, GPIO_CTL_REG_KER);
2670
2671 /* Initiate SRAM reset */
2672 EFX_POPULATE_OWORD_2(srm_cfg_reg_ker,
2673 SRAM_OOB_BT_INIT_EN, 1,
2674 SRM_NUM_BANKS_AND_BANK_SIZE, 0);
2675 falcon_write(efx, &srm_cfg_reg_ker, SRM_CFG_REG_KER);
2676
2677 /* Wait for SRAM reset to complete */
2678 count = 0;
2679 do {
2680 EFX_LOG(efx, "waiting for SRAM reset (attempt %d)...\n", count);
2681
2682 /* SRAM reset is slow; expect around 16ms */
2683 schedule_timeout_uninterruptible(HZ / 50);
2684
2685 /* Check for reset complete */
2686 falcon_read(efx, &srm_cfg_reg_ker, SRM_CFG_REG_KER);
2687 if (!EFX_OWORD_FIELD(srm_cfg_reg_ker, SRAM_OOB_BT_INIT_EN)) {
2688 EFX_LOG(efx, "SRAM reset complete\n");
2689
2690 return 0;
2691 }
2692 } while (++count < 20); /* wait upto 0.4 sec */
2693
2694 EFX_ERR(efx, "timed out waiting for SRAM reset\n");
2695 return -ETIMEDOUT;
2696}
2697
Ben Hutchings4a5b5042008-09-01 12:47:16 +01002698static int falcon_spi_device_init(struct efx_nic *efx,
2699 struct efx_spi_device **spi_device_ret,
2700 unsigned int device_id, u32 device_type)
2701{
2702 struct efx_spi_device *spi_device;
2703
2704 if (device_type != 0) {
Ben Hutchings0c53d8c2008-12-12 22:08:50 -08002705 spi_device = kzalloc(sizeof(*spi_device), GFP_KERNEL);
Ben Hutchings4a5b5042008-09-01 12:47:16 +01002706 if (!spi_device)
2707 return -ENOMEM;
2708 spi_device->device_id = device_id;
2709 spi_device->size =
2710 1 << SPI_DEV_TYPE_FIELD(device_type, SPI_DEV_TYPE_SIZE);
2711 spi_device->addr_len =
2712 SPI_DEV_TYPE_FIELD(device_type, SPI_DEV_TYPE_ADDR_LEN);
2713 spi_device->munge_address = (spi_device->size == 1 << 9 &&
2714 spi_device->addr_len == 1);
Ben Hutchingsf4150722008-11-04 20:34:28 +00002715 spi_device->erase_command =
2716 SPI_DEV_TYPE_FIELD(device_type, SPI_DEV_TYPE_ERASE_CMD);
2717 spi_device->erase_size =
2718 1 << SPI_DEV_TYPE_FIELD(device_type,
2719 SPI_DEV_TYPE_ERASE_SIZE);
Ben Hutchings4a5b5042008-09-01 12:47:16 +01002720 spi_device->block_size =
2721 1 << SPI_DEV_TYPE_FIELD(device_type,
2722 SPI_DEV_TYPE_BLOCK_SIZE);
2723
2724 spi_device->efx = efx;
2725 } else {
2726 spi_device = NULL;
2727 }
2728
2729 kfree(*spi_device_ret);
2730 *spi_device_ret = spi_device;
2731 return 0;
2732}
2733
2734
2735static void falcon_remove_spi_devices(struct efx_nic *efx)
2736{
2737 kfree(efx->spi_eeprom);
2738 efx->spi_eeprom = NULL;
2739 kfree(efx->spi_flash);
2740 efx->spi_flash = NULL;
2741}
2742
Ben Hutchings8ceee662008-04-27 12:55:59 +01002743/* Extract non-volatile configuration */
2744static int falcon_probe_nvconfig(struct efx_nic *efx)
2745{
2746 struct falcon_nvconfig *nvconfig;
Ben Hutchings8c8661e2008-09-01 12:49:02 +01002747 int board_rev;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002748 int rc;
2749
Ben Hutchings8ceee662008-04-27 12:55:59 +01002750 nvconfig = kmalloc(sizeof(*nvconfig), GFP_KERNEL);
Ben Hutchings4a5b5042008-09-01 12:47:16 +01002751 if (!nvconfig)
2752 return -ENOMEM;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002753
Ben Hutchings8c8661e2008-09-01 12:49:02 +01002754 rc = falcon_read_nvram(efx, nvconfig);
2755 if (rc == -EINVAL) {
2756 EFX_ERR(efx, "NVRAM is invalid therefore using defaults\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01002757 efx->phy_type = PHY_TYPE_NONE;
2758 efx->mii.phy_id = PHY_ADDR_INVALID;
2759 board_rev = 0;
Ben Hutchings8c8661e2008-09-01 12:49:02 +01002760 rc = 0;
2761 } else if (rc) {
2762 goto fail1;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002763 } else {
2764 struct falcon_nvconfig_board_v2 *v2 = &nvconfig->board_v2;
Ben Hutchings4a5b5042008-09-01 12:47:16 +01002765 struct falcon_nvconfig_board_v3 *v3 = &nvconfig->board_v3;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002766
2767 efx->phy_type = v2->port0_phy_type;
2768 efx->mii.phy_id = v2->port0_phy_addr;
2769 board_rev = le16_to_cpu(v2->board_revision);
Ben Hutchings4a5b5042008-09-01 12:47:16 +01002770
Ben Hutchings8c8661e2008-09-01 12:49:02 +01002771 if (le16_to_cpu(nvconfig->board_struct_ver) >= 3) {
Ben Hutchings4a5b5042008-09-01 12:47:16 +01002772 __le32 fl = v3->spi_device_type[EE_SPI_FLASH];
2773 __le32 ee = v3->spi_device_type[EE_SPI_EEPROM];
2774 rc = falcon_spi_device_init(efx, &efx->spi_flash,
2775 EE_SPI_FLASH,
2776 le32_to_cpu(fl));
2777 if (rc)
2778 goto fail2;
2779 rc = falcon_spi_device_init(efx, &efx->spi_eeprom,
2780 EE_SPI_EEPROM,
2781 le32_to_cpu(ee));
2782 if (rc)
2783 goto fail2;
2784 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01002785 }
2786
Ben Hutchings8c8661e2008-09-01 12:49:02 +01002787 /* Read the MAC addresses */
2788 memcpy(efx->mac_address, nvconfig->mac_address[0], ETH_ALEN);
2789
Ben Hutchings8ceee662008-04-27 12:55:59 +01002790 EFX_LOG(efx, "PHY is %d phy_id %d\n", efx->phy_type, efx->mii.phy_id);
2791
2792 efx_set_board_info(efx, board_rev);
2793
Ben Hutchings4a5b5042008-09-01 12:47:16 +01002794 kfree(nvconfig);
2795 return 0;
2796
2797 fail2:
2798 falcon_remove_spi_devices(efx);
2799 fail1:
Ben Hutchings8ceee662008-04-27 12:55:59 +01002800 kfree(nvconfig);
2801 return rc;
2802}
2803
2804/* Probe the NIC variant (revision, ASIC vs FPGA, function count, port
2805 * count, port speed). Set workaround and feature flags accordingly.
2806 */
2807static int falcon_probe_nic_variant(struct efx_nic *efx)
2808{
2809 efx_oword_t altera_build;
Ben Hutchings177dfcd2008-12-12 21:50:08 -08002810 efx_oword_t nic_stat;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002811
2812 falcon_read(efx, &altera_build, ALTERA_BUILD_REG_KER);
2813 if (EFX_OWORD_FIELD(altera_build, VER_ALL)) {
2814 EFX_ERR(efx, "Falcon FPGA not supported\n");
2815 return -ENODEV;
2816 }
2817
Ben Hutchings177dfcd2008-12-12 21:50:08 -08002818 falcon_read(efx, &nic_stat, NIC_STAT_REG);
2819
Ben Hutchings55668612008-05-16 21:16:10 +01002820 switch (falcon_rev(efx)) {
Ben Hutchings8ceee662008-04-27 12:55:59 +01002821 case FALCON_REV_A0:
2822 case 0xff:
2823 EFX_ERR(efx, "Falcon rev A0 not supported\n");
2824 return -ENODEV;
2825
Ben Hutchings177dfcd2008-12-12 21:50:08 -08002826 case FALCON_REV_A1:
Ben Hutchings8ceee662008-04-27 12:55:59 +01002827 if (EFX_OWORD_FIELD(nic_stat, STRAP_PCIE) == 0) {
2828 EFX_ERR(efx, "Falcon rev A1 PCI-X not supported\n");
2829 return -ENODEV;
2830 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01002831 break;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002832
2833 case FALCON_REV_B0:
2834 break;
2835
2836 default:
Ben Hutchings55668612008-05-16 21:16:10 +01002837 EFX_ERR(efx, "Unknown Falcon rev %d\n", falcon_rev(efx));
Ben Hutchings8ceee662008-04-27 12:55:59 +01002838 return -ENODEV;
2839 }
2840
Ben Hutchings177dfcd2008-12-12 21:50:08 -08002841 /* Initial assumed speed */
2842 efx->link_speed = EFX_OWORD_FIELD(nic_stat, STRAP_10G) ? 10000 : 1000;
2843
Ben Hutchings8ceee662008-04-27 12:55:59 +01002844 return 0;
2845}
2846
Ben Hutchings4a5b5042008-09-01 12:47:16 +01002847/* Probe all SPI devices on the NIC */
2848static void falcon_probe_spi_devices(struct efx_nic *efx)
2849{
2850 efx_oword_t nic_stat, gpio_ctl, ee_vpd_cfg;
Ben Hutchings2f7f5732008-12-12 21:34:25 -08002851 int boot_dev;
Ben Hutchings4a5b5042008-09-01 12:47:16 +01002852
2853 falcon_read(efx, &gpio_ctl, GPIO_CTL_REG_KER);
2854 falcon_read(efx, &nic_stat, NIC_STAT_REG);
2855 falcon_read(efx, &ee_vpd_cfg, EE_VPD_CFG_REG_KER);
2856
Ben Hutchings2f7f5732008-12-12 21:34:25 -08002857 if (EFX_OWORD_FIELD(gpio_ctl, BOOTED_USING_NVDEVICE)) {
2858 boot_dev = (EFX_OWORD_FIELD(nic_stat, SF_PRST) ?
2859 EE_SPI_FLASH : EE_SPI_EEPROM);
2860 EFX_LOG(efx, "Booted from %s\n",
2861 boot_dev == EE_SPI_FLASH ? "flash" : "EEPROM");
2862 } else {
2863 /* Disable VPD and set clock dividers to safe
2864 * values for initial programming. */
2865 boot_dev = -1;
2866 EFX_LOG(efx, "Booted from internal ASIC settings;"
2867 " setting SPI config\n");
2868 EFX_POPULATE_OWORD_3(ee_vpd_cfg, EE_VPD_EN, 0,
2869 /* 125 MHz / 7 ~= 20 MHz */
2870 EE_SF_CLOCK_DIV, 7,
2871 /* 125 MHz / 63 ~= 2 MHz */
2872 EE_EE_CLOCK_DIV, 63);
2873 falcon_write(efx, &ee_vpd_cfg, EE_VPD_CFG_REG_KER);
Ben Hutchings4a5b5042008-09-01 12:47:16 +01002874 }
2875
Ben Hutchings2f7f5732008-12-12 21:34:25 -08002876 if (boot_dev == EE_SPI_FLASH)
2877 falcon_spi_device_init(efx, &efx->spi_flash, EE_SPI_FLASH,
2878 default_flash_type);
2879 if (boot_dev == EE_SPI_EEPROM)
2880 falcon_spi_device_init(efx, &efx->spi_eeprom, EE_SPI_EEPROM,
2881 large_eeprom_type);
Ben Hutchings4a5b5042008-09-01 12:47:16 +01002882}
2883
Ben Hutchings8ceee662008-04-27 12:55:59 +01002884int falcon_probe_nic(struct efx_nic *efx)
2885{
2886 struct falcon_nic_data *nic_data;
2887 int rc;
2888
Ben Hutchings8ceee662008-04-27 12:55:59 +01002889 /* Allocate storage for hardware specific data */
2890 nic_data = kzalloc(sizeof(*nic_data), GFP_KERNEL);
Ben Hutchings88c59422008-09-03 15:07:50 +01002891 if (!nic_data)
2892 return -ENOMEM;
Ben Hutchings5daab962008-05-16 21:19:43 +01002893 efx->nic_data = nic_data;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002894
2895 /* Determine number of ports etc. */
2896 rc = falcon_probe_nic_variant(efx);
2897 if (rc)
2898 goto fail1;
2899
2900 /* Probe secondary function if expected */
2901 if (FALCON_IS_DUAL_FUNC(efx)) {
2902 struct pci_dev *dev = pci_dev_get(efx->pci_dev);
2903
2904 while ((dev = pci_get_device(EFX_VENDID_SFC, FALCON_A_S_DEVID,
2905 dev))) {
2906 if (dev->bus == efx->pci_dev->bus &&
2907 dev->devfn == efx->pci_dev->devfn + 1) {
2908 nic_data->pci_dev2 = dev;
2909 break;
2910 }
2911 }
2912 if (!nic_data->pci_dev2) {
2913 EFX_ERR(efx, "failed to find secondary function\n");
2914 rc = -ENODEV;
2915 goto fail2;
2916 }
2917 }
2918
2919 /* Now we can reset the NIC */
2920 rc = falcon_reset_hw(efx, RESET_TYPE_ALL);
2921 if (rc) {
2922 EFX_ERR(efx, "failed to reset NIC\n");
2923 goto fail3;
2924 }
2925
2926 /* Allocate memory for INT_KER */
2927 rc = falcon_alloc_buffer(efx, &efx->irq_status, sizeof(efx_oword_t));
2928 if (rc)
2929 goto fail4;
2930 BUG_ON(efx->irq_status.dma_addr & 0x0f);
2931
2932 EFX_LOG(efx, "INT_KER at %llx (virt %p phys %lx)\n",
2933 (unsigned long long)efx->irq_status.dma_addr,
2934 efx->irq_status.addr, virt_to_phys(efx->irq_status.addr));
2935
Ben Hutchings4a5b5042008-09-01 12:47:16 +01002936 falcon_probe_spi_devices(efx);
2937
Ben Hutchings8ceee662008-04-27 12:55:59 +01002938 /* Read in the non-volatile configuration */
2939 rc = falcon_probe_nvconfig(efx);
2940 if (rc)
2941 goto fail5;
2942
Ben Hutchings37b5a602008-05-30 22:27:04 +01002943 /* Initialise I2C adapter */
Ben Hutchingsb4531932008-12-12 22:05:01 -08002944 efx->i2c_adap.owner = THIS_MODULE;
Ben Hutchings37b5a602008-05-30 22:27:04 +01002945 nic_data->i2c_data = falcon_i2c_bit_operations;
2946 nic_data->i2c_data.data = efx;
Ben Hutchingsb4531932008-12-12 22:05:01 -08002947 efx->i2c_adap.algo_data = &nic_data->i2c_data;
Ben Hutchings37b5a602008-05-30 22:27:04 +01002948 efx->i2c_adap.dev.parent = &efx->pci_dev->dev;
Ben Hutchings9dadae62008-07-18 18:59:12 +01002949 strlcpy(efx->i2c_adap.name, "SFC4000 GPIO", sizeof(efx->i2c_adap.name));
Ben Hutchings37b5a602008-05-30 22:27:04 +01002950 rc = i2c_bit_add_bus(&efx->i2c_adap);
2951 if (rc)
2952 goto fail5;
2953
Ben Hutchings8ceee662008-04-27 12:55:59 +01002954 return 0;
2955
2956 fail5:
Ben Hutchings4a5b5042008-09-01 12:47:16 +01002957 falcon_remove_spi_devices(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002958 falcon_free_buffer(efx, &efx->irq_status);
2959 fail4:
Ben Hutchings8ceee662008-04-27 12:55:59 +01002960 fail3:
2961 if (nic_data->pci_dev2) {
2962 pci_dev_put(nic_data->pci_dev2);
2963 nic_data->pci_dev2 = NULL;
2964 }
2965 fail2:
Ben Hutchings8ceee662008-04-27 12:55:59 +01002966 fail1:
2967 kfree(efx->nic_data);
2968 return rc;
2969}
2970
2971/* This call performs hardware-specific global initialisation, such as
2972 * defining the descriptor cache sizes and number of RSS channels.
2973 * It does not set up any buffers, descriptor rings or event queues.
2974 */
2975int falcon_init_nic(struct efx_nic *efx)
2976{
Ben Hutchings8ceee662008-04-27 12:55:59 +01002977 efx_oword_t temp;
2978 unsigned thresh;
2979 int rc;
2980
Ben Hutchings8ceee662008-04-27 12:55:59 +01002981 /* Use on-chip SRAM */
2982 falcon_read(efx, &temp, NIC_STAT_REG);
2983 EFX_SET_OWORD_FIELD(temp, ONCHIP_SRAM, 1);
2984 falcon_write(efx, &temp, NIC_STAT_REG);
2985
Ben Hutchings6f158d52008-12-12 22:00:49 -08002986 /* Set the source of the GMAC clock */
2987 if (falcon_rev(efx) == FALCON_REV_B0) {
2988 falcon_read(efx, &temp, GPIO_CTL_REG_KER);
2989 EFX_SET_OWORD_FIELD(temp, GPIO_USE_NIC_CLK, true);
2990 falcon_write(efx, &temp, GPIO_CTL_REG_KER);
2991 }
2992
Ben Hutchings8ceee662008-04-27 12:55:59 +01002993 /* Set buffer table mode */
2994 EFX_POPULATE_OWORD_1(temp, BUF_TBL_MODE, BUF_TBL_MODE_FULL);
2995 falcon_write(efx, &temp, BUF_TBL_CFG_REG_KER);
2996
2997 rc = falcon_reset_sram(efx);
2998 if (rc)
2999 return rc;
3000
3001 /* Set positions of descriptor caches in SRAM. */
3002 EFX_POPULATE_OWORD_1(temp, SRM_TX_DC_BASE_ADR, TX_DC_BASE / 8);
3003 falcon_write(efx, &temp, SRM_TX_DC_CFG_REG_KER);
3004 EFX_POPULATE_OWORD_1(temp, SRM_RX_DC_BASE_ADR, RX_DC_BASE / 8);
3005 falcon_write(efx, &temp, SRM_RX_DC_CFG_REG_KER);
3006
3007 /* Set TX descriptor cache size. */
3008 BUILD_BUG_ON(TX_DC_ENTRIES != (16 << TX_DC_ENTRIES_ORDER));
3009 EFX_POPULATE_OWORD_1(temp, TX_DC_SIZE, TX_DC_ENTRIES_ORDER);
3010 falcon_write(efx, &temp, TX_DC_CFG_REG_KER);
3011
3012 /* Set RX descriptor cache size. Set low watermark to size-8, as
3013 * this allows most efficient prefetching.
3014 */
3015 BUILD_BUG_ON(RX_DC_ENTRIES != (16 << RX_DC_ENTRIES_ORDER));
3016 EFX_POPULATE_OWORD_1(temp, RX_DC_SIZE, RX_DC_ENTRIES_ORDER);
3017 falcon_write(efx, &temp, RX_DC_CFG_REG_KER);
3018 EFX_POPULATE_OWORD_1(temp, RX_DC_PF_LWM, RX_DC_ENTRIES - 8);
3019 falcon_write(efx, &temp, RX_DC_PF_WM_REG_KER);
3020
3021 /* Clear the parity enables on the TX data fifos as
3022 * they produce false parity errors because of timing issues
3023 */
3024 if (EFX_WORKAROUND_5129(efx)) {
3025 falcon_read(efx, &temp, SPARE_REG_KER);
3026 EFX_SET_OWORD_FIELD(temp, MEM_PERR_EN_TX_DATA, 0);
3027 falcon_write(efx, &temp, SPARE_REG_KER);
3028 }
3029
3030 /* Enable all the genuinely fatal interrupts. (They are still
3031 * masked by the overall interrupt mask, controlled by
3032 * falcon_interrupts()).
3033 *
3034 * Note: All other fatal interrupts are enabled
3035 */
3036 EFX_POPULATE_OWORD_3(temp,
3037 ILL_ADR_INT_KER_EN, 1,
3038 RBUF_OWN_INT_KER_EN, 1,
3039 TBUF_OWN_INT_KER_EN, 1);
3040 EFX_INVERT_OWORD(temp);
3041 falcon_write(efx, &temp, FATAL_INTR_REG_KER);
3042
Ben Hutchings8ceee662008-04-27 12:55:59 +01003043 if (EFX_WORKAROUND_7244(efx)) {
Ben Hutchings955f0a72008-09-01 12:47:52 +01003044 falcon_read(efx, &temp, RX_FILTER_CTL_REG);
Ben Hutchings8ceee662008-04-27 12:55:59 +01003045 EFX_SET_OWORD_FIELD(temp, UDP_FULL_SRCH_LIMIT, 8);
3046 EFX_SET_OWORD_FIELD(temp, UDP_WILD_SRCH_LIMIT, 8);
3047 EFX_SET_OWORD_FIELD(temp, TCP_FULL_SRCH_LIMIT, 8);
3048 EFX_SET_OWORD_FIELD(temp, TCP_WILD_SRCH_LIMIT, 8);
Ben Hutchings955f0a72008-09-01 12:47:52 +01003049 falcon_write(efx, &temp, RX_FILTER_CTL_REG);
Ben Hutchings8ceee662008-04-27 12:55:59 +01003050 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01003051
3052 falcon_setup_rss_indir_table(efx);
3053
3054 /* Setup RX. Wait for descriptor is broken and must
3055 * be disabled. RXDP recovery shouldn't be needed, but is.
3056 */
3057 falcon_read(efx, &temp, RX_SELF_RST_REG_KER);
3058 EFX_SET_OWORD_FIELD(temp, RX_NODESC_WAIT_DIS, 1);
3059 EFX_SET_OWORD_FIELD(temp, RX_RECOVERY_EN, 1);
3060 if (EFX_WORKAROUND_5583(efx))
3061 EFX_SET_OWORD_FIELD(temp, RX_ISCSI_DIS, 1);
3062 falcon_write(efx, &temp, RX_SELF_RST_REG_KER);
3063
3064 /* Disable the ugly timer-based TX DMA backoff and allow TX DMA to be
3065 * controlled by the RX FIFO fill level. Set arbitration to one pkt/Q.
3066 */
3067 falcon_read(efx, &temp, TX_CFG2_REG_KER);
3068 EFX_SET_OWORD_FIELD(temp, TX_RX_SPACER, 0xfe);
3069 EFX_SET_OWORD_FIELD(temp, TX_RX_SPACER_EN, 1);
3070 EFX_SET_OWORD_FIELD(temp, TX_ONE_PKT_PER_Q, 1);
3071 EFX_SET_OWORD_FIELD(temp, TX_CSR_PUSH_EN, 0);
3072 EFX_SET_OWORD_FIELD(temp, TX_DIS_NON_IP_EV, 1);
3073 /* Enable SW_EV to inherit in char driver - assume harmless here */
3074 EFX_SET_OWORD_FIELD(temp, TX_SW_EV_EN, 1);
3075 /* Prefetch threshold 2 => fetch when descriptor cache half empty */
3076 EFX_SET_OWORD_FIELD(temp, TX_PREF_THRESHOLD, 2);
3077 /* Squash TX of packets of 16 bytes or less */
Ben Hutchings55668612008-05-16 21:16:10 +01003078 if (falcon_rev(efx) >= FALCON_REV_B0 && EFX_WORKAROUND_9141(efx))
Ben Hutchings8ceee662008-04-27 12:55:59 +01003079 EFX_SET_OWORD_FIELD(temp, TX_FLUSH_MIN_LEN_EN_B0, 1);
3080 falcon_write(efx, &temp, TX_CFG2_REG_KER);
3081
3082 /* Do not enable TX_NO_EOP_DISC_EN, since it limits packets to 16
3083 * descriptors (which is bad).
3084 */
3085 falcon_read(efx, &temp, TX_CFG_REG_KER);
3086 EFX_SET_OWORD_FIELD(temp, TX_NO_EOP_DISC_EN, 0);
3087 falcon_write(efx, &temp, TX_CFG_REG_KER);
3088
3089 /* RX config */
3090 falcon_read(efx, &temp, RX_CFG_REG_KER);
3091 EFX_SET_OWORD_FIELD_VER(efx, temp, RX_DESC_PUSH_EN, 0);
3092 if (EFX_WORKAROUND_7575(efx))
3093 EFX_SET_OWORD_FIELD_VER(efx, temp, RX_USR_BUF_SIZE,
3094 (3 * 4096) / 32);
Ben Hutchings55668612008-05-16 21:16:10 +01003095 if (falcon_rev(efx) >= FALCON_REV_B0)
Ben Hutchings8ceee662008-04-27 12:55:59 +01003096 EFX_SET_OWORD_FIELD(temp, RX_INGR_EN_B0, 1);
3097
3098 /* RX FIFO flow control thresholds */
3099 thresh = ((rx_xon_thresh_bytes >= 0) ?
3100 rx_xon_thresh_bytes : efx->type->rx_xon_thresh);
3101 EFX_SET_OWORD_FIELD_VER(efx, temp, RX_XON_MAC_TH, thresh / 256);
3102 thresh = ((rx_xoff_thresh_bytes >= 0) ?
3103 rx_xoff_thresh_bytes : efx->type->rx_xoff_thresh);
3104 EFX_SET_OWORD_FIELD_VER(efx, temp, RX_XOFF_MAC_TH, thresh / 256);
3105 /* RX control FIFO thresholds [32 entries] */
Ben Hutchingsc84a6f12008-09-01 12:46:21 +01003106 EFX_SET_OWORD_FIELD_VER(efx, temp, RX_XON_TX_TH, 20);
3107 EFX_SET_OWORD_FIELD_VER(efx, temp, RX_XOFF_TX_TH, 25);
Ben Hutchings8ceee662008-04-27 12:55:59 +01003108 falcon_write(efx, &temp, RX_CFG_REG_KER);
3109
3110 /* Set destination of both TX and RX Flush events */
Ben Hutchings55668612008-05-16 21:16:10 +01003111 if (falcon_rev(efx) >= FALCON_REV_B0) {
Ben Hutchings8ceee662008-04-27 12:55:59 +01003112 EFX_POPULATE_OWORD_1(temp, FLS_EVQ_ID, 0);
3113 falcon_write(efx, &temp, DP_CTRL_REG);
3114 }
3115
3116 return 0;
3117}
3118
3119void falcon_remove_nic(struct efx_nic *efx)
3120{
3121 struct falcon_nic_data *nic_data = efx->nic_data;
Ben Hutchings37b5a602008-05-30 22:27:04 +01003122 int rc;
3123
Ben Hutchings8c870372009-03-04 09:53:02 +00003124 /* Remove I2C adapter and clear it in preparation for a retry */
Ben Hutchings37b5a602008-05-30 22:27:04 +01003125 rc = i2c_del_adapter(&efx->i2c_adap);
3126 BUG_ON(rc);
Ben Hutchings8c870372009-03-04 09:53:02 +00003127 memset(&efx->i2c_adap, 0, sizeof(efx->i2c_adap));
Ben Hutchings8ceee662008-04-27 12:55:59 +01003128
Ben Hutchings4a5b5042008-09-01 12:47:16 +01003129 falcon_remove_spi_devices(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01003130 falcon_free_buffer(efx, &efx->irq_status);
3131
Ben Hutchings91ad7572008-05-16 21:14:27 +01003132 falcon_reset_hw(efx, RESET_TYPE_ALL);
Ben Hutchings8ceee662008-04-27 12:55:59 +01003133
3134 /* Release the second function after the reset */
3135 if (nic_data->pci_dev2) {
3136 pci_dev_put(nic_data->pci_dev2);
3137 nic_data->pci_dev2 = NULL;
3138 }
3139
3140 /* Tear down the private nic state */
3141 kfree(efx->nic_data);
3142 efx->nic_data = NULL;
3143}
3144
3145void falcon_update_nic_stats(struct efx_nic *efx)
3146{
3147 efx_oword_t cnt;
3148
3149 falcon_read(efx, &cnt, RX_NODESC_DROP_REG_KER);
3150 efx->n_rx_nodesc_drop_cnt += EFX_OWORD_FIELD(cnt, RX_NODESC_DROP_CNT);
3151}
3152
3153/**************************************************************************
3154 *
3155 * Revision-dependent attributes used by efx.c
3156 *
3157 **************************************************************************
3158 */
3159
3160struct efx_nic_type falcon_a_nic_type = {
3161 .mem_bar = 2,
3162 .mem_map_size = 0x20000,
3163 .txd_ptr_tbl_base = TX_DESC_PTR_TBL_KER_A1,
3164 .rxd_ptr_tbl_base = RX_DESC_PTR_TBL_KER_A1,
3165 .buf_tbl_base = BUF_TBL_KER_A1,
3166 .evq_ptr_tbl_base = EVQ_PTR_TBL_KER_A1,
3167 .evq_rptr_tbl_base = EVQ_RPTR_REG_KER_A1,
3168 .txd_ring_mask = FALCON_TXD_RING_MASK,
3169 .rxd_ring_mask = FALCON_RXD_RING_MASK,
3170 .evq_size = FALCON_EVQ_SIZE,
3171 .max_dma_mask = FALCON_DMA_MASK,
3172 .tx_dma_mask = FALCON_TX_DMA_MASK,
3173 .bug5391_mask = 0xf,
3174 .rx_xoff_thresh = 2048,
3175 .rx_xon_thresh = 512,
3176 .rx_buffer_padding = 0x24,
3177 .max_interrupt_mode = EFX_INT_MODE_MSI,
3178 .phys_addr_channels = 4,
3179};
3180
3181struct efx_nic_type falcon_b_nic_type = {
3182 .mem_bar = 2,
3183 /* Map everything up to and including the RSS indirection
3184 * table. Don't map MSI-X table, MSI-X PBA since Linux
3185 * requires that they not be mapped. */
3186 .mem_map_size = RX_RSS_INDIR_TBL_B0 + 0x800,
3187 .txd_ptr_tbl_base = TX_DESC_PTR_TBL_KER_B0,
3188 .rxd_ptr_tbl_base = RX_DESC_PTR_TBL_KER_B0,
3189 .buf_tbl_base = BUF_TBL_KER_B0,
3190 .evq_ptr_tbl_base = EVQ_PTR_TBL_KER_B0,
3191 .evq_rptr_tbl_base = EVQ_RPTR_REG_KER_B0,
3192 .txd_ring_mask = FALCON_TXD_RING_MASK,
3193 .rxd_ring_mask = FALCON_RXD_RING_MASK,
3194 .evq_size = FALCON_EVQ_SIZE,
3195 .max_dma_mask = FALCON_DMA_MASK,
3196 .tx_dma_mask = FALCON_TX_DMA_MASK,
3197 .bug5391_mask = 0,
3198 .rx_xoff_thresh = 54272, /* ~80Kb - 3*max MTU */
3199 .rx_xon_thresh = 27648, /* ~3*max MTU */
3200 .rx_buffer_padding = 0,
3201 .max_interrupt_mode = EFX_INT_MODE_MSIX,
3202 .phys_addr_channels = 32, /* Hardware limit is 64, but the legacy
3203 * interrupt handler only supports 32
3204 * channels */
3205};
3206