blob: 448bba9eed09619bc2163105cb61bc59744153af [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 Hutchings8ceee662008-04-27 12:55:59 +010042 */
43struct falcon_nic_data {
44 unsigned next_buffer_table;
45 struct pci_dev *pci_dev2;
Ben Hutchings37b5a602008-05-30 22:27:04 +010046 struct i2c_algo_bit_data i2c_data;
Ben Hutchings8ceee662008-04-27 12:55:59 +010047};
48
49/**************************************************************************
50 *
51 * Configurable values
52 *
53 **************************************************************************
54 */
55
56static int disable_dma_stats;
57
58/* This is set to 16 for a good reason. In summary, if larger than
59 * 16, the descriptor cache holds more than a default socket
60 * buffer's worth of packets (for UDP we can only have at most one
61 * socket buffer's worth outstanding). This combined with the fact
62 * that we only get 1 TX event per descriptor cache means the NIC
63 * goes idle.
64 */
65#define TX_DC_ENTRIES 16
66#define TX_DC_ENTRIES_ORDER 0
67#define TX_DC_BASE 0x130000
68
69#define RX_DC_ENTRIES 64
70#define RX_DC_ENTRIES_ORDER 2
71#define RX_DC_BASE 0x100000
72
Ben Hutchings2f7f5732008-12-12 21:34:25 -080073static const unsigned int
74/* "Large" EEPROM device: Atmel AT25640 or similar
75 * 8 KB, 16-bit address, 32 B write block */
76large_eeprom_type = ((13 << SPI_DEV_TYPE_SIZE_LBN)
77 | (2 << SPI_DEV_TYPE_ADDR_LEN_LBN)
78 | (5 << SPI_DEV_TYPE_BLOCK_SIZE_LBN)),
79/* Default flash device: Atmel AT25F1024
80 * 128 KB, 24-bit address, 32 KB erase block, 256 B write block */
81default_flash_type = ((17 << SPI_DEV_TYPE_SIZE_LBN)
82 | (3 << SPI_DEV_TYPE_ADDR_LEN_LBN)
83 | (0x52 << SPI_DEV_TYPE_ERASE_CMD_LBN)
84 | (15 << SPI_DEV_TYPE_ERASE_SIZE_LBN)
85 | (8 << SPI_DEV_TYPE_BLOCK_SIZE_LBN));
86
Ben Hutchings8ceee662008-04-27 12:55:59 +010087/* RX FIFO XOFF watermark
88 *
89 * When the amount of the RX FIFO increases used increases past this
90 * watermark send XOFF. Only used if RX flow control is enabled (ethtool -A)
91 * This also has an effect on RX/TX arbitration
92 */
93static int rx_xoff_thresh_bytes = -1;
94module_param(rx_xoff_thresh_bytes, int, 0644);
95MODULE_PARM_DESC(rx_xoff_thresh_bytes, "RX fifo XOFF threshold");
96
97/* RX FIFO XON watermark
98 *
99 * When the amount of the RX FIFO used decreases below this
100 * watermark send XON. Only used if TX flow control is enabled (ethtool -A)
101 * This also has an effect on RX/TX arbitration
102 */
103static int rx_xon_thresh_bytes = -1;
104module_param(rx_xon_thresh_bytes, int, 0644);
105MODULE_PARM_DESC(rx_xon_thresh_bytes, "RX fifo XON threshold");
106
107/* TX descriptor ring size - min 512 max 4k */
108#define FALCON_TXD_RING_ORDER TX_DESCQ_SIZE_1K
109#define FALCON_TXD_RING_SIZE 1024
110#define FALCON_TXD_RING_MASK (FALCON_TXD_RING_SIZE - 1)
111
112/* RX descriptor ring size - min 512 max 4k */
113#define FALCON_RXD_RING_ORDER RX_DESCQ_SIZE_1K
114#define FALCON_RXD_RING_SIZE 1024
115#define FALCON_RXD_RING_MASK (FALCON_RXD_RING_SIZE - 1)
116
117/* Event queue size - max 32k */
118#define FALCON_EVQ_ORDER EVQ_SIZE_4K
119#define FALCON_EVQ_SIZE 4096
120#define FALCON_EVQ_MASK (FALCON_EVQ_SIZE - 1)
121
122/* Max number of internal errors. After this resets will not be performed */
123#define FALCON_MAX_INT_ERRORS 4
124
Ben Hutchings6bc5d3a2008-09-01 12:49:37 +0100125/* We poll for events every FLUSH_INTERVAL ms, and check FLUSH_POLL_COUNT times
126 */
127#define FALCON_FLUSH_INTERVAL 10
128#define FALCON_FLUSH_POLL_COUNT 100
Ben Hutchings8ceee662008-04-27 12:55:59 +0100129
130/**************************************************************************
131 *
132 * Falcon constants
133 *
134 **************************************************************************
135 */
136
Ben Hutchings9bbd7d92008-05-16 21:18:48 +0100137/* DMA address mask */
138#define FALCON_DMA_MASK DMA_BIT_MASK(46)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100139
140/* TX DMA length mask (13-bit) */
141#define FALCON_TX_DMA_MASK (4096 - 1)
142
143/* Size and alignment of special buffers (4KB) */
144#define FALCON_BUF_SIZE 4096
145
146/* Dummy SRAM size code */
147#define SRM_NB_BSZ_ONCHIP_ONLY (-1)
148
149/* Be nice if these (or equiv.) were in linux/pci_regs.h, but they're not. */
150#define PCI_EXP_DEVCAP_PWR_VAL_LBN 18
151#define PCI_EXP_DEVCAP_PWR_SCL_LBN 26
152#define PCI_EXP_DEVCTL_PAYLOAD_LBN 5
153#define PCI_EXP_LNKSTA_LNK_WID 0x3f0
154#define PCI_EXP_LNKSTA_LNK_WID_LBN 4
155
156#define FALCON_IS_DUAL_FUNC(efx) \
Ben Hutchings55668612008-05-16 21:16:10 +0100157 (falcon_rev(efx) < FALCON_REV_B0)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100158
159/**************************************************************************
160 *
161 * Falcon hardware access
162 *
163 **************************************************************************/
164
165/* Read the current event from the event queue */
166static inline efx_qword_t *falcon_event(struct efx_channel *channel,
167 unsigned int index)
168{
169 return (((efx_qword_t *) (channel->eventq.addr)) + index);
170}
171
172/* See if an event is present
173 *
174 * We check both the high and low dword of the event for all ones. We
175 * wrote all ones when we cleared the event, and no valid event can
176 * have all ones in either its high or low dwords. This approach is
177 * robust against reordering.
178 *
179 * Note that using a single 64-bit comparison is incorrect; even
180 * though the CPU read will be atomic, the DMA write may not be.
181 */
182static inline int falcon_event_present(efx_qword_t *event)
183{
184 return (!(EFX_DWORD_IS_ALL_ONES(event->dword[0]) |
185 EFX_DWORD_IS_ALL_ONES(event->dword[1])));
186}
187
188/**************************************************************************
189 *
190 * I2C bus - this is a bit-bashing interface using GPIO pins
191 * Note that it uses the output enables to tristate the outputs
192 * SDA is the data pin and SCL is the clock
193 *
194 **************************************************************************
195 */
Ben Hutchings37b5a602008-05-30 22:27:04 +0100196static void falcon_setsda(void *data, int state)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100197{
Ben Hutchings37b5a602008-05-30 22:27:04 +0100198 struct efx_nic *efx = (struct efx_nic *)data;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100199 efx_oword_t reg;
200
Ben Hutchings37b5a602008-05-30 22:27:04 +0100201 falcon_read(efx, &reg, GPIO_CTL_REG_KER);
202 EFX_SET_OWORD_FIELD(reg, GPIO3_OEN, !state);
203 falcon_write(efx, &reg, GPIO_CTL_REG_KER);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100204}
205
Ben Hutchings37b5a602008-05-30 22:27:04 +0100206static void falcon_setscl(void *data, int state)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100207{
Ben Hutchings37b5a602008-05-30 22:27:04 +0100208 struct efx_nic *efx = (struct efx_nic *)data;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100209 efx_oword_t reg;
210
Ben Hutchings37b5a602008-05-30 22:27:04 +0100211 falcon_read(efx, &reg, GPIO_CTL_REG_KER);
212 EFX_SET_OWORD_FIELD(reg, GPIO0_OEN, !state);
213 falcon_write(efx, &reg, GPIO_CTL_REG_KER);
214}
215
216static int falcon_getsda(void *data)
217{
218 struct efx_nic *efx = (struct efx_nic *)data;
219 efx_oword_t reg;
220
221 falcon_read(efx, &reg, GPIO_CTL_REG_KER);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100222 return EFX_OWORD_FIELD(reg, GPIO3_IN);
223}
224
Ben Hutchings37b5a602008-05-30 22:27:04 +0100225static int falcon_getscl(void *data)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100226{
Ben Hutchings37b5a602008-05-30 22:27:04 +0100227 struct efx_nic *efx = (struct efx_nic *)data;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100228 efx_oword_t reg;
229
Ben Hutchings37b5a602008-05-30 22:27:04 +0100230 falcon_read(efx, &reg, GPIO_CTL_REG_KER);
231 return EFX_OWORD_FIELD(reg, GPIO0_IN);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100232}
233
Ben Hutchings37b5a602008-05-30 22:27:04 +0100234static struct i2c_algo_bit_data falcon_i2c_bit_operations = {
235 .setsda = falcon_setsda,
236 .setscl = falcon_setscl,
Ben Hutchings8ceee662008-04-27 12:55:59 +0100237 .getsda = falcon_getsda,
238 .getscl = falcon_getscl,
Ben Hutchings62c78322008-05-30 22:27:46 +0100239 .udelay = 5,
Ben Hutchings9dadae62008-07-18 18:59:12 +0100240 /* Wait up to 50 ms for slave to let us pull SCL high */
241 .timeout = DIV_ROUND_UP(HZ, 20),
Ben Hutchings8ceee662008-04-27 12:55:59 +0100242};
243
244/**************************************************************************
245 *
246 * Falcon special buffer handling
247 * Special buffers are used for event queues and the TX and RX
248 * descriptor rings.
249 *
250 *************************************************************************/
251
252/*
253 * Initialise a Falcon special buffer
254 *
255 * This will define a buffer (previously allocated via
256 * falcon_alloc_special_buffer()) in Falcon's buffer table, allowing
257 * it to be used for event queues, descriptor rings etc.
258 */
Ben Hutchingsbc3c90a2008-09-01 12:48:46 +0100259static void
Ben Hutchings8ceee662008-04-27 12:55:59 +0100260falcon_init_special_buffer(struct efx_nic *efx,
261 struct efx_special_buffer *buffer)
262{
263 efx_qword_t buf_desc;
264 int index;
265 dma_addr_t dma_addr;
266 int i;
267
268 EFX_BUG_ON_PARANOID(!buffer->addr);
269
270 /* Write buffer descriptors to NIC */
271 for (i = 0; i < buffer->entries; i++) {
272 index = buffer->index + i;
273 dma_addr = buffer->dma_addr + (i * 4096);
274 EFX_LOG(efx, "mapping special buffer %d at %llx\n",
275 index, (unsigned long long)dma_addr);
276 EFX_POPULATE_QWORD_4(buf_desc,
277 IP_DAT_BUF_SIZE, IP_DAT_BUF_SIZE_4K,
278 BUF_ADR_REGION, 0,
279 BUF_ADR_FBUF, (dma_addr >> 12),
280 BUF_OWNER_ID_FBUF, 0);
281 falcon_write_sram(efx, &buf_desc, index);
282 }
Ben Hutchings8ceee662008-04-27 12:55:59 +0100283}
284
285/* Unmaps a buffer from Falcon and clears the buffer table entries */
286static void
287falcon_fini_special_buffer(struct efx_nic *efx,
288 struct efx_special_buffer *buffer)
289{
290 efx_oword_t buf_tbl_upd;
291 unsigned int start = buffer->index;
292 unsigned int end = (buffer->index + buffer->entries - 1);
293
294 if (!buffer->entries)
295 return;
296
297 EFX_LOG(efx, "unmapping special buffers %d-%d\n",
298 buffer->index, buffer->index + buffer->entries - 1);
299
300 EFX_POPULATE_OWORD_4(buf_tbl_upd,
301 BUF_UPD_CMD, 0,
302 BUF_CLR_CMD, 1,
303 BUF_CLR_END_ID, end,
304 BUF_CLR_START_ID, start);
305 falcon_write(efx, &buf_tbl_upd, BUF_TBL_UPD_REG_KER);
306}
307
308/*
309 * Allocate a new Falcon special buffer
310 *
311 * This allocates memory for a new buffer, clears it and allocates a
312 * new buffer ID range. It does not write into Falcon's buffer table.
313 *
314 * This call will allocate 4KB buffers, since Falcon can't use 8KB
315 * buffers for event queues and descriptor rings.
316 */
317static int falcon_alloc_special_buffer(struct efx_nic *efx,
318 struct efx_special_buffer *buffer,
319 unsigned int len)
320{
321 struct falcon_nic_data *nic_data = efx->nic_data;
322
323 len = ALIGN(len, FALCON_BUF_SIZE);
324
325 buffer->addr = pci_alloc_consistent(efx->pci_dev, len,
326 &buffer->dma_addr);
327 if (!buffer->addr)
328 return -ENOMEM;
329 buffer->len = len;
330 buffer->entries = len / FALCON_BUF_SIZE;
331 BUG_ON(buffer->dma_addr & (FALCON_BUF_SIZE - 1));
332
333 /* All zeros is a potentially valid event so memset to 0xff */
334 memset(buffer->addr, 0xff, len);
335
336 /* Select new buffer ID */
337 buffer->index = nic_data->next_buffer_table;
338 nic_data->next_buffer_table += buffer->entries;
339
340 EFX_LOG(efx, "allocating special buffers %d-%d at %llx+%x "
341 "(virt %p phys %lx)\n", buffer->index,
342 buffer->index + buffer->entries - 1,
343 (unsigned long long)buffer->dma_addr, len,
344 buffer->addr, virt_to_phys(buffer->addr));
345
346 return 0;
347}
348
349static void falcon_free_special_buffer(struct efx_nic *efx,
350 struct efx_special_buffer *buffer)
351{
352 if (!buffer->addr)
353 return;
354
355 EFX_LOG(efx, "deallocating special buffers %d-%d at %llx+%x "
356 "(virt %p phys %lx)\n", buffer->index,
357 buffer->index + buffer->entries - 1,
358 (unsigned long long)buffer->dma_addr, buffer->len,
359 buffer->addr, virt_to_phys(buffer->addr));
360
361 pci_free_consistent(efx->pci_dev, buffer->len, buffer->addr,
362 buffer->dma_addr);
363 buffer->addr = NULL;
364 buffer->entries = 0;
365}
366
367/**************************************************************************
368 *
369 * Falcon generic buffer handling
370 * These buffers are used for interrupt status and MAC stats
371 *
372 **************************************************************************/
373
374static int falcon_alloc_buffer(struct efx_nic *efx,
375 struct efx_buffer *buffer, unsigned int len)
376{
377 buffer->addr = pci_alloc_consistent(efx->pci_dev, len,
378 &buffer->dma_addr);
379 if (!buffer->addr)
380 return -ENOMEM;
381 buffer->len = len;
382 memset(buffer->addr, 0, len);
383 return 0;
384}
385
386static void falcon_free_buffer(struct efx_nic *efx, struct efx_buffer *buffer)
387{
388 if (buffer->addr) {
389 pci_free_consistent(efx->pci_dev, buffer->len,
390 buffer->addr, buffer->dma_addr);
391 buffer->addr = NULL;
392 }
393}
394
395/**************************************************************************
396 *
397 * Falcon TX path
398 *
399 **************************************************************************/
400
401/* Returns a pointer to the specified transmit descriptor in the TX
402 * descriptor queue belonging to the specified channel.
403 */
404static inline efx_qword_t *falcon_tx_desc(struct efx_tx_queue *tx_queue,
405 unsigned int index)
406{
407 return (((efx_qword_t *) (tx_queue->txd.addr)) + index);
408}
409
410/* This writes to the TX_DESC_WPTR; write pointer for TX descriptor ring */
411static inline void falcon_notify_tx_desc(struct efx_tx_queue *tx_queue)
412{
413 unsigned write_ptr;
414 efx_dword_t reg;
415
416 write_ptr = tx_queue->write_count & FALCON_TXD_RING_MASK;
417 EFX_POPULATE_DWORD_1(reg, TX_DESC_WPTR_DWORD, write_ptr);
418 falcon_writel_page(tx_queue->efx, &reg,
419 TX_DESC_UPD_REG_KER_DWORD, tx_queue->queue);
420}
421
422
423/* For each entry inserted into the software descriptor ring, create a
424 * descriptor in the hardware TX descriptor ring (in host memory), and
425 * write a doorbell.
426 */
427void falcon_push_buffers(struct efx_tx_queue *tx_queue)
428{
429
430 struct efx_tx_buffer *buffer;
431 efx_qword_t *txd;
432 unsigned write_ptr;
433
434 BUG_ON(tx_queue->write_count == tx_queue->insert_count);
435
436 do {
437 write_ptr = tx_queue->write_count & FALCON_TXD_RING_MASK;
438 buffer = &tx_queue->buffer[write_ptr];
439 txd = falcon_tx_desc(tx_queue, write_ptr);
440 ++tx_queue->write_count;
441
442 /* Create TX descriptor ring entry */
443 EFX_POPULATE_QWORD_5(*txd,
444 TX_KER_PORT, 0,
445 TX_KER_CONT, buffer->continuation,
446 TX_KER_BYTE_CNT, buffer->len,
447 TX_KER_BUF_REGION, 0,
448 TX_KER_BUF_ADR, buffer->dma_addr);
449 } while (tx_queue->write_count != tx_queue->insert_count);
450
451 wmb(); /* Ensure descriptors are written before they are fetched */
452 falcon_notify_tx_desc(tx_queue);
453}
454
455/* Allocate hardware resources for a TX queue */
456int falcon_probe_tx(struct efx_tx_queue *tx_queue)
457{
458 struct efx_nic *efx = tx_queue->efx;
459 return falcon_alloc_special_buffer(efx, &tx_queue->txd,
460 FALCON_TXD_RING_SIZE *
461 sizeof(efx_qword_t));
462}
463
Ben Hutchingsbc3c90a2008-09-01 12:48:46 +0100464void falcon_init_tx(struct efx_tx_queue *tx_queue)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100465{
466 efx_oword_t tx_desc_ptr;
467 struct efx_nic *efx = tx_queue->efx;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100468
Ben Hutchings6bc5d3a2008-09-01 12:49:37 +0100469 tx_queue->flushed = false;
470
Ben Hutchings8ceee662008-04-27 12:55:59 +0100471 /* Pin TX descriptor ring */
Ben Hutchingsbc3c90a2008-09-01 12:48:46 +0100472 falcon_init_special_buffer(efx, &tx_queue->txd);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100473
474 /* Push TX descriptor ring to card */
475 EFX_POPULATE_OWORD_10(tx_desc_ptr,
476 TX_DESCQ_EN, 1,
477 TX_ISCSI_DDIG_EN, 0,
478 TX_ISCSI_HDIG_EN, 0,
479 TX_DESCQ_BUF_BASE_ID, tx_queue->txd.index,
Ben Hutchingsd3074022008-09-01 12:48:03 +0100480 TX_DESCQ_EVQ_ID, tx_queue->channel->channel,
Ben Hutchings8ceee662008-04-27 12:55:59 +0100481 TX_DESCQ_OWNER_ID, 0,
482 TX_DESCQ_LABEL, tx_queue->queue,
483 TX_DESCQ_SIZE, FALCON_TXD_RING_ORDER,
484 TX_DESCQ_TYPE, 0,
485 TX_NON_IP_DROP_DIS_B0, 1);
486
Ben Hutchings55668612008-05-16 21:16:10 +0100487 if (falcon_rev(efx) >= FALCON_REV_B0) {
Ben Hutchings60ac1062008-09-01 12:44:59 +0100488 int csum = tx_queue->queue == EFX_TX_QUEUE_OFFLOAD_CSUM;
489 EFX_SET_OWORD_FIELD(tx_desc_ptr, TX_IP_CHKSM_DIS_B0, !csum);
490 EFX_SET_OWORD_FIELD(tx_desc_ptr, TX_TCP_CHKSM_DIS_B0, !csum);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100491 }
492
493 falcon_write_table(efx, &tx_desc_ptr, efx->type->txd_ptr_tbl_base,
494 tx_queue->queue);
495
Ben Hutchings55668612008-05-16 21:16:10 +0100496 if (falcon_rev(efx) < FALCON_REV_B0) {
Ben Hutchings8ceee662008-04-27 12:55:59 +0100497 efx_oword_t reg;
498
Ben Hutchings60ac1062008-09-01 12:44:59 +0100499 /* Only 128 bits in this register */
500 BUILD_BUG_ON(EFX_TX_QUEUE_COUNT >= 128);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100501
502 falcon_read(efx, &reg, TX_CHKSM_CFG_REG_KER_A1);
Ben Hutchings60ac1062008-09-01 12:44:59 +0100503 if (tx_queue->queue == EFX_TX_QUEUE_OFFLOAD_CSUM)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100504 clear_bit_le(tx_queue->queue, (void *)&reg);
505 else
506 set_bit_le(tx_queue->queue, (void *)&reg);
507 falcon_write(efx, &reg, TX_CHKSM_CFG_REG_KER_A1);
508 }
Ben Hutchings8ceee662008-04-27 12:55:59 +0100509}
510
Ben Hutchings6bc5d3a2008-09-01 12:49:37 +0100511static void falcon_flush_tx_queue(struct efx_tx_queue *tx_queue)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100512{
513 struct efx_nic *efx = tx_queue->efx;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100514 efx_oword_t tx_flush_descq;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100515
516 /* Post a flush command */
517 EFX_POPULATE_OWORD_2(tx_flush_descq,
518 TX_FLUSH_DESCQ_CMD, 1,
519 TX_FLUSH_DESCQ, tx_queue->queue);
520 falcon_write(efx, &tx_flush_descq, TX_FLUSH_DESCQ_REG_KER);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100521}
522
523void falcon_fini_tx(struct efx_tx_queue *tx_queue)
524{
525 struct efx_nic *efx = tx_queue->efx;
526 efx_oword_t tx_desc_ptr;
527
Ben Hutchings6bc5d3a2008-09-01 12:49:37 +0100528 /* The queue should have been flushed */
529 WARN_ON(!tx_queue->flushed);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100530
531 /* Remove TX descriptor ring from card */
532 EFX_ZERO_OWORD(tx_desc_ptr);
533 falcon_write_table(efx, &tx_desc_ptr, efx->type->txd_ptr_tbl_base,
534 tx_queue->queue);
535
536 /* Unpin TX descriptor ring */
537 falcon_fini_special_buffer(efx, &tx_queue->txd);
538}
539
540/* Free buffers backing TX queue */
541void falcon_remove_tx(struct efx_tx_queue *tx_queue)
542{
543 falcon_free_special_buffer(tx_queue->efx, &tx_queue->txd);
544}
545
546/**************************************************************************
547 *
548 * Falcon RX path
549 *
550 **************************************************************************/
551
552/* Returns a pointer to the specified descriptor in the RX descriptor queue */
553static inline efx_qword_t *falcon_rx_desc(struct efx_rx_queue *rx_queue,
554 unsigned int index)
555{
556 return (((efx_qword_t *) (rx_queue->rxd.addr)) + index);
557}
558
559/* This creates an entry in the RX descriptor queue */
560static inline void falcon_build_rx_desc(struct efx_rx_queue *rx_queue,
561 unsigned index)
562{
563 struct efx_rx_buffer *rx_buf;
564 efx_qword_t *rxd;
565
566 rxd = falcon_rx_desc(rx_queue, index);
567 rx_buf = efx_rx_buffer(rx_queue, index);
568 EFX_POPULATE_QWORD_3(*rxd,
569 RX_KER_BUF_SIZE,
570 rx_buf->len -
571 rx_queue->efx->type->rx_buffer_padding,
572 RX_KER_BUF_REGION, 0,
573 RX_KER_BUF_ADR, rx_buf->dma_addr);
574}
575
576/* This writes to the RX_DESC_WPTR register for the specified receive
577 * descriptor ring.
578 */
579void falcon_notify_rx_desc(struct efx_rx_queue *rx_queue)
580{
581 efx_dword_t reg;
582 unsigned write_ptr;
583
584 while (rx_queue->notified_count != rx_queue->added_count) {
585 falcon_build_rx_desc(rx_queue,
586 rx_queue->notified_count &
587 FALCON_RXD_RING_MASK);
588 ++rx_queue->notified_count;
589 }
590
591 wmb();
592 write_ptr = rx_queue->added_count & FALCON_RXD_RING_MASK;
593 EFX_POPULATE_DWORD_1(reg, RX_DESC_WPTR_DWORD, write_ptr);
594 falcon_writel_page(rx_queue->efx, &reg,
595 RX_DESC_UPD_REG_KER_DWORD, rx_queue->queue);
596}
597
598int falcon_probe_rx(struct efx_rx_queue *rx_queue)
599{
600 struct efx_nic *efx = rx_queue->efx;
601 return falcon_alloc_special_buffer(efx, &rx_queue->rxd,
602 FALCON_RXD_RING_SIZE *
603 sizeof(efx_qword_t));
604}
605
Ben Hutchingsbc3c90a2008-09-01 12:48:46 +0100606void falcon_init_rx(struct efx_rx_queue *rx_queue)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100607{
608 efx_oword_t rx_desc_ptr;
609 struct efx_nic *efx = rx_queue->efx;
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +0100610 bool is_b0 = falcon_rev(efx) >= FALCON_REV_B0;
611 bool iscsi_digest_en = is_b0;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100612
613 EFX_LOG(efx, "RX queue %d ring in special buffers %d-%d\n",
614 rx_queue->queue, rx_queue->rxd.index,
615 rx_queue->rxd.index + rx_queue->rxd.entries - 1);
616
Ben Hutchings6bc5d3a2008-09-01 12:49:37 +0100617 rx_queue->flushed = false;
618
Ben Hutchings8ceee662008-04-27 12:55:59 +0100619 /* Pin RX descriptor ring */
Ben Hutchingsbc3c90a2008-09-01 12:48:46 +0100620 falcon_init_special_buffer(efx, &rx_queue->rxd);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100621
622 /* Push RX descriptor ring to card */
623 EFX_POPULATE_OWORD_10(rx_desc_ptr,
624 RX_ISCSI_DDIG_EN, iscsi_digest_en,
625 RX_ISCSI_HDIG_EN, iscsi_digest_en,
626 RX_DESCQ_BUF_BASE_ID, rx_queue->rxd.index,
Ben Hutchingsd3074022008-09-01 12:48:03 +0100627 RX_DESCQ_EVQ_ID, rx_queue->channel->channel,
Ben Hutchings8ceee662008-04-27 12:55:59 +0100628 RX_DESCQ_OWNER_ID, 0,
629 RX_DESCQ_LABEL, rx_queue->queue,
630 RX_DESCQ_SIZE, FALCON_RXD_RING_ORDER,
631 RX_DESCQ_TYPE, 0 /* kernel queue */ ,
632 /* For >=B0 this is scatter so disable */
633 RX_DESCQ_JUMBO, !is_b0,
634 RX_DESCQ_EN, 1);
635 falcon_write_table(efx, &rx_desc_ptr, efx->type->rxd_ptr_tbl_base,
636 rx_queue->queue);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100637}
638
Ben Hutchings6bc5d3a2008-09-01 12:49:37 +0100639static void falcon_flush_rx_queue(struct efx_rx_queue *rx_queue)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100640{
641 struct efx_nic *efx = rx_queue->efx;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100642 efx_oword_t rx_flush_descq;
643
644 /* Post a flush command */
645 EFX_POPULATE_OWORD_2(rx_flush_descq,
646 RX_FLUSH_DESCQ_CMD, 1,
647 RX_FLUSH_DESCQ, rx_queue->queue);
648 falcon_write(efx, &rx_flush_descq, RX_FLUSH_DESCQ_REG_KER);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100649}
650
651void falcon_fini_rx(struct efx_rx_queue *rx_queue)
652{
653 efx_oword_t rx_desc_ptr;
654 struct efx_nic *efx = rx_queue->efx;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100655
Ben Hutchings6bc5d3a2008-09-01 12:49:37 +0100656 /* The queue should already have been flushed */
657 WARN_ON(!rx_queue->flushed);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100658
659 /* Remove RX descriptor ring from card */
660 EFX_ZERO_OWORD(rx_desc_ptr);
661 falcon_write_table(efx, &rx_desc_ptr, efx->type->rxd_ptr_tbl_base,
662 rx_queue->queue);
663
664 /* Unpin RX descriptor ring */
665 falcon_fini_special_buffer(efx, &rx_queue->rxd);
666}
667
668/* Free buffers backing RX queue */
669void falcon_remove_rx(struct efx_rx_queue *rx_queue)
670{
671 falcon_free_special_buffer(rx_queue->efx, &rx_queue->rxd);
672}
673
674/**************************************************************************
675 *
676 * Falcon event queue processing
677 * Event queues are processed by per-channel tasklets.
678 *
679 **************************************************************************/
680
681/* Update a channel's event queue's read pointer (RPTR) register
682 *
683 * This writes the EVQ_RPTR_REG register for the specified channel's
684 * event queue.
685 *
686 * Note that EVQ_RPTR_REG contains the index of the "last read" event,
687 * whereas channel->eventq_read_ptr contains the index of the "next to
688 * read" event.
689 */
690void falcon_eventq_read_ack(struct efx_channel *channel)
691{
692 efx_dword_t reg;
693 struct efx_nic *efx = channel->efx;
694
695 EFX_POPULATE_DWORD_1(reg, EVQ_RPTR_DWORD, channel->eventq_read_ptr);
696 falcon_writel_table(efx, &reg, efx->type->evq_rptr_tbl_base,
Ben Hutchingsd3074022008-09-01 12:48:03 +0100697 channel->channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100698}
699
700/* Use HW to insert a SW defined event */
701void falcon_generate_event(struct efx_channel *channel, efx_qword_t *event)
702{
703 efx_oword_t drv_ev_reg;
704
705 EFX_POPULATE_OWORD_2(drv_ev_reg,
Ben Hutchingsd3074022008-09-01 12:48:03 +0100706 DRV_EV_QID, channel->channel,
Ben Hutchings8ceee662008-04-27 12:55:59 +0100707 DRV_EV_DATA,
708 EFX_QWORD_FIELD64(*event, WHOLE_EVENT));
709 falcon_write(channel->efx, &drv_ev_reg, DRV_EV_REG_KER);
710}
711
712/* Handle a transmit completion event
713 *
714 * Falcon batches TX completion events; the message we receive is of
715 * the form "complete all TX events up to this index".
716 */
Ben Hutchings4d566062008-09-01 12:47:12 +0100717static void falcon_handle_tx_event(struct efx_channel *channel,
718 efx_qword_t *event)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100719{
720 unsigned int tx_ev_desc_ptr;
721 unsigned int tx_ev_q_label;
722 struct efx_tx_queue *tx_queue;
723 struct efx_nic *efx = channel->efx;
724
725 if (likely(EFX_QWORD_FIELD(*event, TX_EV_COMP))) {
726 /* Transmit completion */
727 tx_ev_desc_ptr = EFX_QWORD_FIELD(*event, TX_EV_DESC_PTR);
728 tx_ev_q_label = EFX_QWORD_FIELD(*event, TX_EV_Q_LABEL);
729 tx_queue = &efx->tx_queue[tx_ev_q_label];
730 efx_xmit_done(tx_queue, tx_ev_desc_ptr);
731 } else if (EFX_QWORD_FIELD(*event, TX_EV_WQ_FF_FULL)) {
732 /* Rewrite the FIFO write pointer */
733 tx_ev_q_label = EFX_QWORD_FIELD(*event, TX_EV_Q_LABEL);
734 tx_queue = &efx->tx_queue[tx_ev_q_label];
735
Ben Hutchings55668612008-05-16 21:16:10 +0100736 if (efx_dev_registered(efx))
Ben Hutchings8ceee662008-04-27 12:55:59 +0100737 netif_tx_lock(efx->net_dev);
738 falcon_notify_tx_desc(tx_queue);
Ben Hutchings55668612008-05-16 21:16:10 +0100739 if (efx_dev_registered(efx))
Ben Hutchings8ceee662008-04-27 12:55:59 +0100740 netif_tx_unlock(efx->net_dev);
741 } else if (EFX_QWORD_FIELD(*event, TX_EV_PKT_ERR) &&
742 EFX_WORKAROUND_10727(efx)) {
743 efx_schedule_reset(efx, RESET_TYPE_TX_DESC_FETCH);
744 } else {
745 EFX_ERR(efx, "channel %d unexpected TX event "
746 EFX_QWORD_FMT"\n", channel->channel,
747 EFX_QWORD_VAL(*event));
748 }
749}
750
Ben Hutchings8ceee662008-04-27 12:55:59 +0100751/* Detect errors included in the rx_evt_pkt_ok bit. */
752static void falcon_handle_rx_not_ok(struct efx_rx_queue *rx_queue,
753 const efx_qword_t *event,
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +0100754 bool *rx_ev_pkt_ok,
755 bool *discard)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100756{
757 struct efx_nic *efx = rx_queue->efx;
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +0100758 bool rx_ev_buf_owner_id_err, rx_ev_ip_hdr_chksum_err;
759 bool rx_ev_tcp_udp_chksum_err, rx_ev_eth_crc_err;
760 bool rx_ev_frm_trunc, rx_ev_drib_nib, rx_ev_tobe_disc;
761 bool rx_ev_other_err, rx_ev_pause_frm;
762 bool rx_ev_ip_frag_err, rx_ev_hdr_type, rx_ev_mcast_pkt;
763 unsigned rx_ev_pkt_type;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100764
765 rx_ev_hdr_type = EFX_QWORD_FIELD(*event, RX_EV_HDR_TYPE);
766 rx_ev_mcast_pkt = EFX_QWORD_FIELD(*event, RX_EV_MCAST_PKT);
767 rx_ev_tobe_disc = EFX_QWORD_FIELD(*event, RX_EV_TOBE_DISC);
768 rx_ev_pkt_type = EFX_QWORD_FIELD(*event, RX_EV_PKT_TYPE);
769 rx_ev_buf_owner_id_err = EFX_QWORD_FIELD(*event,
770 RX_EV_BUF_OWNER_ID_ERR);
771 rx_ev_ip_frag_err = EFX_QWORD_FIELD(*event, RX_EV_IF_FRAG_ERR);
772 rx_ev_ip_hdr_chksum_err = EFX_QWORD_FIELD(*event,
773 RX_EV_IP_HDR_CHKSUM_ERR);
774 rx_ev_tcp_udp_chksum_err = EFX_QWORD_FIELD(*event,
775 RX_EV_TCP_UDP_CHKSUM_ERR);
776 rx_ev_eth_crc_err = EFX_QWORD_FIELD(*event, RX_EV_ETH_CRC_ERR);
777 rx_ev_frm_trunc = EFX_QWORD_FIELD(*event, RX_EV_FRM_TRUNC);
Ben Hutchings55668612008-05-16 21:16:10 +0100778 rx_ev_drib_nib = ((falcon_rev(efx) >= FALCON_REV_B0) ?
Ben Hutchings8ceee662008-04-27 12:55:59 +0100779 0 : EFX_QWORD_FIELD(*event, RX_EV_DRIB_NIB));
780 rx_ev_pause_frm = EFX_QWORD_FIELD(*event, RX_EV_PAUSE_FRM_ERR);
781
782 /* Every error apart from tobe_disc and pause_frm */
783 rx_ev_other_err = (rx_ev_drib_nib | rx_ev_tcp_udp_chksum_err |
784 rx_ev_buf_owner_id_err | rx_ev_eth_crc_err |
785 rx_ev_frm_trunc | rx_ev_ip_hdr_chksum_err);
786
Ben Hutchings50050872008-12-12 21:42:42 -0800787 /* Count errors that are not in MAC stats. Ignore expected
788 * checksum errors during self-test. */
Ben Hutchings8ceee662008-04-27 12:55:59 +0100789 if (rx_ev_frm_trunc)
790 ++rx_queue->channel->n_rx_frm_trunc;
791 else if (rx_ev_tobe_disc)
792 ++rx_queue->channel->n_rx_tobe_disc;
Ben Hutchings50050872008-12-12 21:42:42 -0800793 else if (!efx->loopback_selftest) {
794 if (rx_ev_ip_hdr_chksum_err)
795 ++rx_queue->channel->n_rx_ip_hdr_chksum_err;
796 else if (rx_ev_tcp_udp_chksum_err)
797 ++rx_queue->channel->n_rx_tcp_udp_chksum_err;
798 }
Ben Hutchings8ceee662008-04-27 12:55:59 +0100799 if (rx_ev_ip_frag_err)
800 ++rx_queue->channel->n_rx_ip_frag_err;
801
802 /* The frame must be discarded if any of these are true. */
803 *discard = (rx_ev_eth_crc_err | rx_ev_frm_trunc | rx_ev_drib_nib |
804 rx_ev_tobe_disc | rx_ev_pause_frm);
805
806 /* TOBE_DISC is expected on unicast mismatches; don't print out an
807 * error message. FRM_TRUNC indicates RXDP dropped the packet due
808 * to a FIFO overflow.
809 */
810#ifdef EFX_ENABLE_DEBUG
811 if (rx_ev_other_err) {
812 EFX_INFO_RL(efx, " RX queue %d unexpected RX event "
Ben Hutchings5b39fe32008-09-01 12:46:03 +0100813 EFX_QWORD_FMT "%s%s%s%s%s%s%s%s\n",
Ben Hutchings8ceee662008-04-27 12:55:59 +0100814 rx_queue->queue, EFX_QWORD_VAL(*event),
815 rx_ev_buf_owner_id_err ? " [OWNER_ID_ERR]" : "",
816 rx_ev_ip_hdr_chksum_err ?
817 " [IP_HDR_CHKSUM_ERR]" : "",
818 rx_ev_tcp_udp_chksum_err ?
819 " [TCP_UDP_CHKSUM_ERR]" : "",
820 rx_ev_eth_crc_err ? " [ETH_CRC_ERR]" : "",
821 rx_ev_frm_trunc ? " [FRM_TRUNC]" : "",
822 rx_ev_drib_nib ? " [DRIB_NIB]" : "",
823 rx_ev_tobe_disc ? " [TOBE_DISC]" : "",
Ben Hutchings5b39fe32008-09-01 12:46:03 +0100824 rx_ev_pause_frm ? " [PAUSE]" : "");
Ben Hutchings8ceee662008-04-27 12:55:59 +0100825 }
826#endif
827
828 if (unlikely(rx_ev_eth_crc_err && EFX_WORKAROUND_10750(efx) &&
829 efx->phy_type == PHY_TYPE_10XPRESS))
830 tenxpress_crc_err(efx);
831}
832
833/* Handle receive events that are not in-order. */
834static void falcon_handle_rx_bad_index(struct efx_rx_queue *rx_queue,
835 unsigned index)
836{
837 struct efx_nic *efx = rx_queue->efx;
838 unsigned expected, dropped;
839
840 expected = rx_queue->removed_count & FALCON_RXD_RING_MASK;
841 dropped = ((index + FALCON_RXD_RING_SIZE - expected) &
842 FALCON_RXD_RING_MASK);
843 EFX_INFO(efx, "dropped %d events (index=%d expected=%d)\n",
844 dropped, index, expected);
845
846 efx_schedule_reset(efx, EFX_WORKAROUND_5676(efx) ?
847 RESET_TYPE_RX_RECOVERY : RESET_TYPE_DISABLE);
848}
849
850/* Handle a packet received event
851 *
852 * Falcon silicon gives a "discard" flag if it's a unicast packet with the
853 * wrong destination address
854 * Also "is multicast" and "matches multicast filter" flags can be used to
855 * discard non-matching multicast packets.
856 */
Ben Hutchings42cbe2d2008-09-01 12:48:08 +0100857static void falcon_handle_rx_event(struct efx_channel *channel,
858 const efx_qword_t *event)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100859{
Ben Hutchings42cbe2d2008-09-01 12:48:08 +0100860 unsigned int rx_ev_desc_ptr, rx_ev_byte_cnt;
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +0100861 unsigned int rx_ev_hdr_type, rx_ev_mcast_pkt;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100862 unsigned expected_ptr;
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +0100863 bool rx_ev_pkt_ok, discard = false, checksummed;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100864 struct efx_rx_queue *rx_queue;
865 struct efx_nic *efx = channel->efx;
866
867 /* Basic packet information */
868 rx_ev_byte_cnt = EFX_QWORD_FIELD(*event, RX_EV_BYTE_CNT);
869 rx_ev_pkt_ok = EFX_QWORD_FIELD(*event, RX_EV_PKT_OK);
870 rx_ev_hdr_type = EFX_QWORD_FIELD(*event, RX_EV_HDR_TYPE);
871 WARN_ON(EFX_QWORD_FIELD(*event, RX_EV_JUMBO_CONT));
872 WARN_ON(EFX_QWORD_FIELD(*event, RX_EV_SOP) != 1);
Ben Hutchings42cbe2d2008-09-01 12:48:08 +0100873 WARN_ON(EFX_QWORD_FIELD(*event, RX_EV_Q_LABEL) != channel->channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100874
Ben Hutchings42cbe2d2008-09-01 12:48:08 +0100875 rx_queue = &efx->rx_queue[channel->channel];
Ben Hutchings8ceee662008-04-27 12:55:59 +0100876
877 rx_ev_desc_ptr = EFX_QWORD_FIELD(*event, RX_EV_DESC_PTR);
878 expected_ptr = rx_queue->removed_count & FALCON_RXD_RING_MASK;
Ben Hutchings42cbe2d2008-09-01 12:48:08 +0100879 if (unlikely(rx_ev_desc_ptr != expected_ptr))
Ben Hutchings8ceee662008-04-27 12:55:59 +0100880 falcon_handle_rx_bad_index(rx_queue, rx_ev_desc_ptr);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100881
882 if (likely(rx_ev_pkt_ok)) {
883 /* If packet is marked as OK and packet type is TCP/IPv4 or
884 * UDP/IPv4, then we can rely on the hardware checksum.
885 */
886 checksummed = RX_EV_HDR_TYPE_HAS_CHECKSUMS(rx_ev_hdr_type);
887 } else {
888 falcon_handle_rx_not_ok(rx_queue, event, &rx_ev_pkt_ok,
Ben Hutchings5b39fe32008-09-01 12:46:03 +0100889 &discard);
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +0100890 checksummed = false;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100891 }
892
893 /* Detect multicast packets that didn't match the filter */
894 rx_ev_mcast_pkt = EFX_QWORD_FIELD(*event, RX_EV_MCAST_PKT);
895 if (rx_ev_mcast_pkt) {
896 unsigned int rx_ev_mcast_hash_match =
897 EFX_QWORD_FIELD(*event, RX_EV_MCAST_HASH_MATCH);
898
899 if (unlikely(!rx_ev_mcast_hash_match))
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +0100900 discard = true;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100901 }
902
903 /* Handle received packet */
904 efx_rx_packet(rx_queue, rx_ev_desc_ptr, rx_ev_byte_cnt,
905 checksummed, discard);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100906}
907
908/* Global events are basically PHY events */
909static void falcon_handle_global_event(struct efx_channel *channel,
910 efx_qword_t *event)
911{
912 struct efx_nic *efx = channel->efx;
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +0100913 bool is_phy_event = false, handled = false;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100914
915 /* Check for interrupt on either port. Some boards have a
916 * single PHY wired to the interrupt line for port 1. */
917 if (EFX_QWORD_FIELD(*event, G_PHY0_INTR) ||
918 EFX_QWORD_FIELD(*event, G_PHY1_INTR) ||
919 EFX_QWORD_FIELD(*event, XG_PHY_INTR))
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +0100920 is_phy_event = true;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100921
Ben Hutchings55668612008-05-16 21:16:10 +0100922 if ((falcon_rev(efx) >= FALCON_REV_B0) &&
Steve Hodgson92ade882008-09-01 12:49:29 +0100923 EFX_QWORD_FIELD(*event, XG_MNT_INTR_B0))
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +0100924 is_phy_event = true;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100925
926 if (is_phy_event) {
927 efx->phy_op->clear_interrupt(efx);
928 queue_work(efx->workqueue, &efx->reconfigure_work);
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +0100929 handled = true;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100930 }
931
932 if (EFX_QWORD_FIELD_VER(efx, *event, RX_RECOVERY)) {
933 EFX_ERR(efx, "channel %d seen global RX_RESET "
934 "event. Resetting.\n", channel->channel);
935
936 atomic_inc(&efx->rx_reset);
937 efx_schedule_reset(efx, EFX_WORKAROUND_6555(efx) ?
938 RESET_TYPE_RX_RECOVERY : RESET_TYPE_DISABLE);
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +0100939 handled = true;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100940 }
941
942 if (!handled)
943 EFX_ERR(efx, "channel %d unknown global event "
944 EFX_QWORD_FMT "\n", channel->channel,
945 EFX_QWORD_VAL(*event));
946}
947
948static void falcon_handle_driver_event(struct efx_channel *channel,
949 efx_qword_t *event)
950{
951 struct efx_nic *efx = channel->efx;
952 unsigned int ev_sub_code;
953 unsigned int ev_sub_data;
954
955 ev_sub_code = EFX_QWORD_FIELD(*event, DRIVER_EV_SUB_CODE);
956 ev_sub_data = EFX_QWORD_FIELD(*event, DRIVER_EV_SUB_DATA);
957
958 switch (ev_sub_code) {
959 case TX_DESCQ_FLS_DONE_EV_DECODE:
960 EFX_TRACE(efx, "channel %d TXQ %d flushed\n",
961 channel->channel, ev_sub_data);
962 break;
963 case RX_DESCQ_FLS_DONE_EV_DECODE:
964 EFX_TRACE(efx, "channel %d RXQ %d flushed\n",
965 channel->channel, ev_sub_data);
966 break;
967 case EVQ_INIT_DONE_EV_DECODE:
968 EFX_LOG(efx, "channel %d EVQ %d initialised\n",
969 channel->channel, ev_sub_data);
970 break;
971 case SRM_UPD_DONE_EV_DECODE:
972 EFX_TRACE(efx, "channel %d SRAM update done\n",
973 channel->channel);
974 break;
975 case WAKE_UP_EV_DECODE:
976 EFX_TRACE(efx, "channel %d RXQ %d wakeup event\n",
977 channel->channel, ev_sub_data);
978 break;
979 case TIMER_EV_DECODE:
980 EFX_TRACE(efx, "channel %d RX queue %d timer expired\n",
981 channel->channel, ev_sub_data);
982 break;
983 case RX_RECOVERY_EV_DECODE:
984 EFX_ERR(efx, "channel %d seen DRIVER RX_RESET event. "
985 "Resetting.\n", channel->channel);
Ben Hutchings05e3ec02008-05-07 13:00:39 +0100986 atomic_inc(&efx->rx_reset);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100987 efx_schedule_reset(efx,
988 EFX_WORKAROUND_6555(efx) ?
989 RESET_TYPE_RX_RECOVERY :
990 RESET_TYPE_DISABLE);
991 break;
992 case RX_DSC_ERROR_EV_DECODE:
993 EFX_ERR(efx, "RX DMA Q %d reports descriptor fetch error."
994 " RX Q %d is disabled.\n", ev_sub_data, ev_sub_data);
995 efx_schedule_reset(efx, RESET_TYPE_RX_DESC_FETCH);
996 break;
997 case TX_DSC_ERROR_EV_DECODE:
998 EFX_ERR(efx, "TX DMA Q %d reports descriptor fetch error."
999 " TX Q %d is disabled.\n", ev_sub_data, ev_sub_data);
1000 efx_schedule_reset(efx, RESET_TYPE_TX_DESC_FETCH);
1001 break;
1002 default:
1003 EFX_TRACE(efx, "channel %d unknown driver event code %d "
1004 "data %04x\n", channel->channel, ev_sub_code,
1005 ev_sub_data);
1006 break;
1007 }
1008}
1009
Ben Hutchings42cbe2d2008-09-01 12:48:08 +01001010int falcon_process_eventq(struct efx_channel *channel, int rx_quota)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001011{
1012 unsigned int read_ptr;
1013 efx_qword_t event, *p_event;
1014 int ev_code;
Ben Hutchings42cbe2d2008-09-01 12:48:08 +01001015 int rx_packets = 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001016
1017 read_ptr = channel->eventq_read_ptr;
1018
1019 do {
1020 p_event = falcon_event(channel, read_ptr);
1021 event = *p_event;
1022
1023 if (!falcon_event_present(&event))
1024 /* End of events */
1025 break;
1026
1027 EFX_TRACE(channel->efx, "channel %d event is "EFX_QWORD_FMT"\n",
1028 channel->channel, EFX_QWORD_VAL(event));
1029
1030 /* Clear this event by marking it all ones */
1031 EFX_SET_QWORD(*p_event);
1032
1033 ev_code = EFX_QWORD_FIELD(event, EV_CODE);
1034
1035 switch (ev_code) {
1036 case RX_IP_EV_DECODE:
Ben Hutchings42cbe2d2008-09-01 12:48:08 +01001037 falcon_handle_rx_event(channel, &event);
1038 ++rx_packets;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001039 break;
1040 case TX_IP_EV_DECODE:
1041 falcon_handle_tx_event(channel, &event);
1042 break;
1043 case DRV_GEN_EV_DECODE:
1044 channel->eventq_magic
1045 = EFX_QWORD_FIELD(event, EVQ_MAGIC);
1046 EFX_LOG(channel->efx, "channel %d received generated "
1047 "event "EFX_QWORD_FMT"\n", channel->channel,
1048 EFX_QWORD_VAL(event));
1049 break;
1050 case GLOBAL_EV_DECODE:
1051 falcon_handle_global_event(channel, &event);
1052 break;
1053 case DRIVER_EV_DECODE:
1054 falcon_handle_driver_event(channel, &event);
1055 break;
1056 default:
1057 EFX_ERR(channel->efx, "channel %d unknown event type %d"
1058 " (data " EFX_QWORD_FMT ")\n", channel->channel,
1059 ev_code, EFX_QWORD_VAL(event));
1060 }
1061
1062 /* Increment read pointer */
1063 read_ptr = (read_ptr + 1) & FALCON_EVQ_MASK;
1064
Ben Hutchings42cbe2d2008-09-01 12:48:08 +01001065 } while (rx_packets < rx_quota);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001066
1067 channel->eventq_read_ptr = read_ptr;
Ben Hutchings42cbe2d2008-09-01 12:48:08 +01001068 return rx_packets;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001069}
1070
1071void falcon_set_int_moderation(struct efx_channel *channel)
1072{
1073 efx_dword_t timer_cmd;
1074 struct efx_nic *efx = channel->efx;
1075
1076 /* Set timer register */
1077 if (channel->irq_moderation) {
1078 /* Round to resolution supported by hardware. The value we
1079 * program is based at 0. So actual interrupt moderation
1080 * achieved is ((x + 1) * res).
1081 */
1082 unsigned int res = 5;
1083 channel->irq_moderation -= (channel->irq_moderation % res);
1084 if (channel->irq_moderation < res)
1085 channel->irq_moderation = res;
1086 EFX_POPULATE_DWORD_2(timer_cmd,
1087 TIMER_MODE, TIMER_MODE_INT_HLDOFF,
1088 TIMER_VAL,
1089 (channel->irq_moderation / res) - 1);
1090 } else {
1091 EFX_POPULATE_DWORD_2(timer_cmd,
1092 TIMER_MODE, TIMER_MODE_DIS,
1093 TIMER_VAL, 0);
1094 }
1095 falcon_writel_page_locked(efx, &timer_cmd, TIMER_CMD_REG_KER,
Ben Hutchingsd3074022008-09-01 12:48:03 +01001096 channel->channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001097
1098}
1099
1100/* Allocate buffer table entries for event queue */
1101int falcon_probe_eventq(struct efx_channel *channel)
1102{
1103 struct efx_nic *efx = channel->efx;
1104 unsigned int evq_size;
1105
1106 evq_size = FALCON_EVQ_SIZE * sizeof(efx_qword_t);
1107 return falcon_alloc_special_buffer(efx, &channel->eventq, evq_size);
1108}
1109
Ben Hutchingsbc3c90a2008-09-01 12:48:46 +01001110void falcon_init_eventq(struct efx_channel *channel)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001111{
1112 efx_oword_t evq_ptr;
1113 struct efx_nic *efx = channel->efx;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001114
1115 EFX_LOG(efx, "channel %d event queue in special buffers %d-%d\n",
1116 channel->channel, channel->eventq.index,
1117 channel->eventq.index + channel->eventq.entries - 1);
1118
1119 /* Pin event queue buffer */
Ben Hutchingsbc3c90a2008-09-01 12:48:46 +01001120 falcon_init_special_buffer(efx, &channel->eventq);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001121
1122 /* Fill event queue with all ones (i.e. empty events) */
1123 memset(channel->eventq.addr, 0xff, channel->eventq.len);
1124
1125 /* Push event queue to card */
1126 EFX_POPULATE_OWORD_3(evq_ptr,
1127 EVQ_EN, 1,
1128 EVQ_SIZE, FALCON_EVQ_ORDER,
1129 EVQ_BUF_BASE_ID, channel->eventq.index);
1130 falcon_write_table(efx, &evq_ptr, efx->type->evq_ptr_tbl_base,
Ben Hutchingsd3074022008-09-01 12:48:03 +01001131 channel->channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001132
1133 falcon_set_int_moderation(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001134}
1135
1136void falcon_fini_eventq(struct efx_channel *channel)
1137{
1138 efx_oword_t eventq_ptr;
1139 struct efx_nic *efx = channel->efx;
1140
1141 /* Remove event queue from card */
1142 EFX_ZERO_OWORD(eventq_ptr);
1143 falcon_write_table(efx, &eventq_ptr, efx->type->evq_ptr_tbl_base,
Ben Hutchingsd3074022008-09-01 12:48:03 +01001144 channel->channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001145
1146 /* Unpin event queue */
1147 falcon_fini_special_buffer(efx, &channel->eventq);
1148}
1149
1150/* Free buffers backing event queue */
1151void falcon_remove_eventq(struct efx_channel *channel)
1152{
1153 falcon_free_special_buffer(channel->efx, &channel->eventq);
1154}
1155
1156
1157/* Generates a test event on the event queue. A subsequent call to
1158 * process_eventq() should pick up the event and place the value of
1159 * "magic" into channel->eventq_magic;
1160 */
1161void falcon_generate_test_event(struct efx_channel *channel, unsigned int magic)
1162{
1163 efx_qword_t test_event;
1164
1165 EFX_POPULATE_QWORD_2(test_event,
1166 EV_CODE, DRV_GEN_EV_DECODE,
1167 EVQ_MAGIC, magic);
1168 falcon_generate_event(channel, &test_event);
1169}
1170
Ben Hutchings6bc5d3a2008-09-01 12:49:37 +01001171/**************************************************************************
1172 *
1173 * Flush handling
1174 *
1175 **************************************************************************/
1176
1177
1178static void falcon_poll_flush_events(struct efx_nic *efx)
1179{
1180 struct efx_channel *channel = &efx->channel[0];
1181 struct efx_tx_queue *tx_queue;
1182 struct efx_rx_queue *rx_queue;
1183 unsigned int read_ptr, i;
1184
1185 read_ptr = channel->eventq_read_ptr;
1186 for (i = 0; i < FALCON_EVQ_SIZE; ++i) {
1187 efx_qword_t *event = falcon_event(channel, read_ptr);
1188 int ev_code, ev_sub_code, ev_queue;
1189 bool ev_failed;
1190 if (!falcon_event_present(event))
1191 break;
1192
1193 ev_code = EFX_QWORD_FIELD(*event, EV_CODE);
1194 if (ev_code != DRIVER_EV_DECODE)
1195 continue;
1196
1197 ev_sub_code = EFX_QWORD_FIELD(*event, DRIVER_EV_SUB_CODE);
1198 switch (ev_sub_code) {
1199 case TX_DESCQ_FLS_DONE_EV_DECODE:
1200 ev_queue = EFX_QWORD_FIELD(*event,
1201 DRIVER_EV_TX_DESCQ_ID);
1202 if (ev_queue < EFX_TX_QUEUE_COUNT) {
1203 tx_queue = efx->tx_queue + ev_queue;
1204 tx_queue->flushed = true;
1205 }
1206 break;
1207 case RX_DESCQ_FLS_DONE_EV_DECODE:
1208 ev_queue = EFX_QWORD_FIELD(*event,
1209 DRIVER_EV_RX_DESCQ_ID);
1210 ev_failed = EFX_QWORD_FIELD(*event,
1211 DRIVER_EV_RX_FLUSH_FAIL);
1212 if (ev_queue < efx->n_rx_queues) {
1213 rx_queue = efx->rx_queue + ev_queue;
1214
1215 /* retry the rx flush */
1216 if (ev_failed)
1217 falcon_flush_rx_queue(rx_queue);
1218 else
1219 rx_queue->flushed = true;
1220 }
1221 break;
1222 }
1223
1224 read_ptr = (read_ptr + 1) & FALCON_EVQ_MASK;
1225 }
1226}
1227
1228/* Handle tx and rx flushes at the same time, since they run in
1229 * parallel in the hardware and there's no reason for us to
1230 * serialise them */
1231int falcon_flush_queues(struct efx_nic *efx)
1232{
1233 struct efx_rx_queue *rx_queue;
1234 struct efx_tx_queue *tx_queue;
1235 int i;
1236 bool outstanding;
1237
1238 /* Issue flush requests */
1239 efx_for_each_tx_queue(tx_queue, efx) {
1240 tx_queue->flushed = false;
1241 falcon_flush_tx_queue(tx_queue);
1242 }
1243 efx_for_each_rx_queue(rx_queue, efx) {
1244 rx_queue->flushed = false;
1245 falcon_flush_rx_queue(rx_queue);
1246 }
1247
1248 /* Poll the evq looking for flush completions. Since we're not pushing
1249 * any more rx or tx descriptors at this point, we're in no danger of
1250 * overflowing the evq whilst we wait */
1251 for (i = 0; i < FALCON_FLUSH_POLL_COUNT; ++i) {
1252 msleep(FALCON_FLUSH_INTERVAL);
1253 falcon_poll_flush_events(efx);
1254
1255 /* Check if every queue has been succesfully flushed */
1256 outstanding = false;
1257 efx_for_each_tx_queue(tx_queue, efx)
1258 outstanding |= !tx_queue->flushed;
1259 efx_for_each_rx_queue(rx_queue, efx)
1260 outstanding |= !rx_queue->flushed;
1261 if (!outstanding)
1262 return 0;
1263 }
1264
1265 /* Mark the queues as all flushed. We're going to return failure
1266 * leading to a reset, or fake up success anyway. "flushed" now
1267 * indicates that we tried to flush. */
1268 efx_for_each_tx_queue(tx_queue, efx) {
1269 if (!tx_queue->flushed)
1270 EFX_ERR(efx, "tx queue %d flush command timed out\n",
1271 tx_queue->queue);
1272 tx_queue->flushed = true;
1273 }
1274 efx_for_each_rx_queue(rx_queue, efx) {
1275 if (!rx_queue->flushed)
1276 EFX_ERR(efx, "rx queue %d flush command timed out\n",
1277 rx_queue->queue);
1278 rx_queue->flushed = true;
1279 }
1280
1281 if (EFX_WORKAROUND_7803(efx))
1282 return 0;
1283
1284 return -ETIMEDOUT;
1285}
Ben Hutchings8ceee662008-04-27 12:55:59 +01001286
1287/**************************************************************************
1288 *
1289 * Falcon hardware interrupts
1290 * The hardware interrupt handler does very little work; all the event
1291 * queue processing is carried out by per-channel tasklets.
1292 *
1293 **************************************************************************/
1294
1295/* Enable/disable/generate Falcon interrupts */
1296static inline void falcon_interrupts(struct efx_nic *efx, int enabled,
1297 int force)
1298{
1299 efx_oword_t int_en_reg_ker;
1300
1301 EFX_POPULATE_OWORD_2(int_en_reg_ker,
1302 KER_INT_KER, force,
1303 DRV_INT_EN_KER, enabled);
1304 falcon_write(efx, &int_en_reg_ker, INT_EN_REG_KER);
1305}
1306
1307void falcon_enable_interrupts(struct efx_nic *efx)
1308{
1309 efx_oword_t int_adr_reg_ker;
1310 struct efx_channel *channel;
1311
1312 EFX_ZERO_OWORD(*((efx_oword_t *) efx->irq_status.addr));
1313 wmb(); /* Ensure interrupt vector is clear before interrupts enabled */
1314
1315 /* Program address */
1316 EFX_POPULATE_OWORD_2(int_adr_reg_ker,
1317 NORM_INT_VEC_DIS_KER, EFX_INT_MODE_USE_MSI(efx),
1318 INT_ADR_KER, efx->irq_status.dma_addr);
1319 falcon_write(efx, &int_adr_reg_ker, INT_ADR_REG_KER);
1320
1321 /* Enable interrupts */
1322 falcon_interrupts(efx, 1, 0);
1323
1324 /* Force processing of all the channels to get the EVQ RPTRs up to
1325 date */
Ben Hutchings64ee3122008-09-01 12:47:38 +01001326 efx_for_each_channel(channel, efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001327 efx_schedule_channel(channel);
1328}
1329
1330void falcon_disable_interrupts(struct efx_nic *efx)
1331{
1332 /* Disable interrupts */
1333 falcon_interrupts(efx, 0, 0);
1334}
1335
1336/* Generate a Falcon test interrupt
1337 * Interrupt must already have been enabled, otherwise nasty things
1338 * may happen.
1339 */
1340void falcon_generate_interrupt(struct efx_nic *efx)
1341{
1342 falcon_interrupts(efx, 1, 1);
1343}
1344
1345/* Acknowledge a legacy interrupt from Falcon
1346 *
1347 * This acknowledges a legacy (not MSI) interrupt via INT_ACK_KER_REG.
1348 *
1349 * Due to SFC bug 3706 (silicon revision <=A1) reads can be duplicated in the
1350 * BIU. Interrupt acknowledge is read sensitive so must write instead
1351 * (then read to ensure the BIU collector is flushed)
1352 *
1353 * NB most hardware supports MSI interrupts
1354 */
1355static inline void falcon_irq_ack_a1(struct efx_nic *efx)
1356{
1357 efx_dword_t reg;
1358
1359 EFX_POPULATE_DWORD_1(reg, INT_ACK_DUMMY_DATA, 0xb7eb7e);
1360 falcon_writel(efx, &reg, INT_ACK_REG_KER_A1);
1361 falcon_readl(efx, &reg, WORK_AROUND_BROKEN_PCI_READS_REG_KER_A1);
1362}
1363
1364/* Process a fatal interrupt
1365 * Disable bus mastering ASAP and schedule a reset
1366 */
1367static irqreturn_t falcon_fatal_interrupt(struct efx_nic *efx)
1368{
1369 struct falcon_nic_data *nic_data = efx->nic_data;
Ben Hutchingsd3208b52008-05-16 21:20:00 +01001370 efx_oword_t *int_ker = efx->irq_status.addr;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001371 efx_oword_t fatal_intr;
1372 int error, mem_perr;
1373 static int n_int_errors;
1374
1375 falcon_read(efx, &fatal_intr, FATAL_INTR_REG_KER);
1376 error = EFX_OWORD_FIELD(fatal_intr, INT_KER_ERROR);
1377
1378 EFX_ERR(efx, "SYSTEM ERROR " EFX_OWORD_FMT " status "
1379 EFX_OWORD_FMT ": %s\n", EFX_OWORD_VAL(*int_ker),
1380 EFX_OWORD_VAL(fatal_intr),
1381 error ? "disabling bus mastering" : "no recognised error");
1382 if (error == 0)
1383 goto out;
1384
1385 /* If this is a memory parity error dump which blocks are offending */
1386 mem_perr = EFX_OWORD_FIELD(fatal_intr, MEM_PERR_INT_KER);
1387 if (mem_perr) {
1388 efx_oword_t reg;
1389 falcon_read(efx, &reg, MEM_STAT_REG_KER);
1390 EFX_ERR(efx, "SYSTEM ERROR: memory parity error "
1391 EFX_OWORD_FMT "\n", EFX_OWORD_VAL(reg));
1392 }
1393
Ben Hutchings0a62f1a2008-09-01 12:50:14 +01001394 /* Disable both devices */
Ben Hutchings8ceee662008-04-27 12:55:59 +01001395 pci_disable_device(efx->pci_dev);
1396 if (FALCON_IS_DUAL_FUNC(efx))
1397 pci_disable_device(nic_data->pci_dev2);
Ben Hutchings0a62f1a2008-09-01 12:50:14 +01001398 falcon_disable_interrupts(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001399
1400 if (++n_int_errors < FALCON_MAX_INT_ERRORS) {
1401 EFX_ERR(efx, "SYSTEM ERROR - reset scheduled\n");
1402 efx_schedule_reset(efx, RESET_TYPE_INT_ERROR);
1403 } else {
1404 EFX_ERR(efx, "SYSTEM ERROR - max number of errors seen."
1405 "NIC will be disabled\n");
1406 efx_schedule_reset(efx, RESET_TYPE_DISABLE);
1407 }
1408out:
1409 return IRQ_HANDLED;
1410}
1411
1412/* Handle a legacy interrupt from Falcon
1413 * Acknowledges the interrupt and schedule event queue processing.
1414 */
1415static irqreturn_t falcon_legacy_interrupt_b0(int irq, void *dev_id)
1416{
Ben Hutchingsd3208b52008-05-16 21:20:00 +01001417 struct efx_nic *efx = dev_id;
1418 efx_oword_t *int_ker = efx->irq_status.addr;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001419 struct efx_channel *channel;
1420 efx_dword_t reg;
1421 u32 queues;
1422 int syserr;
1423
1424 /* Read the ISR which also ACKs the interrupts */
1425 falcon_readl(efx, &reg, INT_ISR0_B0);
1426 queues = EFX_EXTRACT_DWORD(reg, 0, 31);
1427
1428 /* Check to see if we have a serious error condition */
1429 syserr = EFX_OWORD_FIELD(*int_ker, FATAL_INT);
1430 if (unlikely(syserr))
1431 return falcon_fatal_interrupt(efx);
1432
1433 if (queues == 0)
1434 return IRQ_NONE;
1435
1436 efx->last_irq_cpu = raw_smp_processor_id();
1437 EFX_TRACE(efx, "IRQ %d on CPU %d status " EFX_DWORD_FMT "\n",
1438 irq, raw_smp_processor_id(), EFX_DWORD_VAL(reg));
1439
1440 /* Schedule processing of any interrupting queues */
1441 channel = &efx->channel[0];
1442 while (queues) {
1443 if (queues & 0x01)
1444 efx_schedule_channel(channel);
1445 channel++;
1446 queues >>= 1;
1447 }
1448
1449 return IRQ_HANDLED;
1450}
1451
1452
1453static irqreturn_t falcon_legacy_interrupt_a1(int irq, void *dev_id)
1454{
Ben Hutchingsd3208b52008-05-16 21:20:00 +01001455 struct efx_nic *efx = dev_id;
1456 efx_oword_t *int_ker = efx->irq_status.addr;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001457 struct efx_channel *channel;
1458 int syserr;
1459 int queues;
1460
1461 /* Check to see if this is our interrupt. If it isn't, we
1462 * exit without having touched the hardware.
1463 */
1464 if (unlikely(EFX_OWORD_IS_ZERO(*int_ker))) {
1465 EFX_TRACE(efx, "IRQ %d on CPU %d not for me\n", irq,
1466 raw_smp_processor_id());
1467 return IRQ_NONE;
1468 }
1469 efx->last_irq_cpu = raw_smp_processor_id();
1470 EFX_TRACE(efx, "IRQ %d on CPU %d status " EFX_OWORD_FMT "\n",
1471 irq, raw_smp_processor_id(), EFX_OWORD_VAL(*int_ker));
1472
1473 /* Check to see if we have a serious error condition */
1474 syserr = EFX_OWORD_FIELD(*int_ker, FATAL_INT);
1475 if (unlikely(syserr))
1476 return falcon_fatal_interrupt(efx);
1477
1478 /* Determine interrupting queues, clear interrupt status
1479 * register and acknowledge the device interrupt.
1480 */
1481 BUILD_BUG_ON(INT_EVQS_WIDTH > EFX_MAX_CHANNELS);
1482 queues = EFX_OWORD_FIELD(*int_ker, INT_EVQS);
1483 EFX_ZERO_OWORD(*int_ker);
1484 wmb(); /* Ensure the vector is cleared before interrupt ack */
1485 falcon_irq_ack_a1(efx);
1486
1487 /* Schedule processing of any interrupting queues */
1488 channel = &efx->channel[0];
1489 while (queues) {
1490 if (queues & 0x01)
1491 efx_schedule_channel(channel);
1492 channel++;
1493 queues >>= 1;
1494 }
1495
1496 return IRQ_HANDLED;
1497}
1498
1499/* Handle an MSI interrupt from Falcon
1500 *
1501 * Handle an MSI hardware interrupt. This routine schedules event
1502 * queue processing. No interrupt acknowledgement cycle is necessary.
1503 * Also, we never need to check that the interrupt is for us, since
1504 * MSI interrupts cannot be shared.
1505 */
1506static irqreturn_t falcon_msi_interrupt(int irq, void *dev_id)
1507{
Ben Hutchingsd3208b52008-05-16 21:20:00 +01001508 struct efx_channel *channel = dev_id;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001509 struct efx_nic *efx = channel->efx;
Ben Hutchingsd3208b52008-05-16 21:20:00 +01001510 efx_oword_t *int_ker = efx->irq_status.addr;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001511 int syserr;
1512
1513 efx->last_irq_cpu = raw_smp_processor_id();
1514 EFX_TRACE(efx, "IRQ %d on CPU %d status " EFX_OWORD_FMT "\n",
1515 irq, raw_smp_processor_id(), EFX_OWORD_VAL(*int_ker));
1516
1517 /* Check to see if we have a serious error condition */
1518 syserr = EFX_OWORD_FIELD(*int_ker, FATAL_INT);
1519 if (unlikely(syserr))
1520 return falcon_fatal_interrupt(efx);
1521
1522 /* Schedule processing of the channel */
1523 efx_schedule_channel(channel);
1524
1525 return IRQ_HANDLED;
1526}
1527
1528
1529/* Setup RSS indirection table.
1530 * This maps from the hash value of the packet to RXQ
1531 */
1532static void falcon_setup_rss_indir_table(struct efx_nic *efx)
1533{
1534 int i = 0;
1535 unsigned long offset;
1536 efx_dword_t dword;
1537
Ben Hutchings55668612008-05-16 21:16:10 +01001538 if (falcon_rev(efx) < FALCON_REV_B0)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001539 return;
1540
1541 for (offset = RX_RSS_INDIR_TBL_B0;
1542 offset < RX_RSS_INDIR_TBL_B0 + 0x800;
1543 offset += 0x10) {
1544 EFX_POPULATE_DWORD_1(dword, RX_RSS_INDIR_ENT_B0,
Ben Hutchings8831da72008-09-01 12:47:48 +01001545 i % efx->n_rx_queues);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001546 falcon_writel(efx, &dword, offset);
1547 i++;
1548 }
1549}
1550
1551/* Hook interrupt handler(s)
1552 * Try MSI and then legacy interrupts.
1553 */
1554int falcon_init_interrupt(struct efx_nic *efx)
1555{
1556 struct efx_channel *channel;
1557 int rc;
1558
1559 if (!EFX_INT_MODE_USE_MSI(efx)) {
1560 irq_handler_t handler;
Ben Hutchings55668612008-05-16 21:16:10 +01001561 if (falcon_rev(efx) >= FALCON_REV_B0)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001562 handler = falcon_legacy_interrupt_b0;
1563 else
1564 handler = falcon_legacy_interrupt_a1;
1565
1566 rc = request_irq(efx->legacy_irq, handler, IRQF_SHARED,
1567 efx->name, efx);
1568 if (rc) {
1569 EFX_ERR(efx, "failed to hook legacy IRQ %d\n",
1570 efx->pci_dev->irq);
1571 goto fail1;
1572 }
1573 return 0;
1574 }
1575
1576 /* Hook MSI or MSI-X interrupt */
Ben Hutchings64ee3122008-09-01 12:47:38 +01001577 efx_for_each_channel(channel, efx) {
Ben Hutchings8ceee662008-04-27 12:55:59 +01001578 rc = request_irq(channel->irq, falcon_msi_interrupt,
1579 IRQF_PROBE_SHARED, /* Not shared */
Ben Hutchings56536e92008-12-12 21:37:02 -08001580 channel->name, channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001581 if (rc) {
1582 EFX_ERR(efx, "failed to hook IRQ %d\n", channel->irq);
1583 goto fail2;
1584 }
1585 }
1586
1587 return 0;
1588
1589 fail2:
Ben Hutchings64ee3122008-09-01 12:47:38 +01001590 efx_for_each_channel(channel, efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001591 free_irq(channel->irq, channel);
1592 fail1:
1593 return rc;
1594}
1595
1596void falcon_fini_interrupt(struct efx_nic *efx)
1597{
1598 struct efx_channel *channel;
1599 efx_oword_t reg;
1600
1601 /* Disable MSI/MSI-X interrupts */
Ben Hutchings64ee3122008-09-01 12:47:38 +01001602 efx_for_each_channel(channel, efx) {
Ben Hutchings8ceee662008-04-27 12:55:59 +01001603 if (channel->irq)
1604 free_irq(channel->irq, channel);
Ben Hutchingsb3475642008-05-16 21:15:49 +01001605 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01001606
1607 /* ACK legacy interrupt */
Ben Hutchings55668612008-05-16 21:16:10 +01001608 if (falcon_rev(efx) >= FALCON_REV_B0)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001609 falcon_read(efx, &reg, INT_ISR0_B0);
1610 else
1611 falcon_irq_ack_a1(efx);
1612
1613 /* Disable legacy interrupt */
1614 if (efx->legacy_irq)
1615 free_irq(efx->legacy_irq, efx);
1616}
1617
1618/**************************************************************************
1619 *
1620 * EEPROM/flash
1621 *
1622 **************************************************************************
1623 */
1624
Ben Hutchings23d30f02008-12-12 21:56:11 -08001625#define FALCON_SPI_MAX_LEN sizeof(efx_oword_t)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001626
Ben Hutchingsbe4ea892008-12-12 21:33:50 -08001627static int falcon_spi_poll(struct efx_nic *efx)
1628{
1629 efx_oword_t reg;
1630 falcon_read(efx, &reg, EE_SPI_HCMD_REG_KER);
1631 return EFX_OWORD_FIELD(reg, EE_SPI_HCMD_CMD_EN) ? -EBUSY : 0;
1632}
1633
Ben Hutchings8ceee662008-04-27 12:55:59 +01001634/* Wait for SPI command completion */
1635static int falcon_spi_wait(struct efx_nic *efx)
1636{
Ben Hutchingsbe4ea892008-12-12 21:33:50 -08001637 /* Most commands will finish quickly, so we start polling at
1638 * very short intervals. Sometimes the command may have to
1639 * wait for VPD or expansion ROM access outside of our
1640 * control, so we allow up to 100 ms. */
1641 unsigned long timeout = jiffies + 1 + DIV_ROUND_UP(HZ, 10);
1642 int i;
1643
1644 for (i = 0; i < 10; i++) {
1645 if (!falcon_spi_poll(efx))
1646 return 0;
1647 udelay(10);
1648 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01001649
Ben Hutchings4a5b5042008-09-01 12:47:16 +01001650 for (;;) {
Ben Hutchingsbe4ea892008-12-12 21:33:50 -08001651 if (!falcon_spi_poll(efx))
Ben Hutchings8ceee662008-04-27 12:55:59 +01001652 return 0;
Ben Hutchings4a5b5042008-09-01 12:47:16 +01001653 if (time_after_eq(jiffies, timeout)) {
1654 EFX_ERR(efx, "timed out waiting for SPI\n");
1655 return -ETIMEDOUT;
1656 }
Ben Hutchingsbe4ea892008-12-12 21:33:50 -08001657 schedule_timeout_uninterruptible(1);
Ben Hutchings4a5b5042008-09-01 12:47:16 +01001658 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01001659}
1660
Ben Hutchingsf4150722008-11-04 20:34:28 +00001661int falcon_spi_cmd(const struct efx_spi_device *spi,
1662 unsigned int command, int address,
Ben Hutchings23d30f02008-12-12 21:56:11 -08001663 const void *in, void *out, size_t len)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001664{
Ben Hutchings4a5b5042008-09-01 12:47:16 +01001665 struct efx_nic *efx = spi->efx;
1666 bool addressed = (address >= 0);
1667 bool reading = (out != NULL);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001668 efx_oword_t reg;
1669 int rc;
1670
Ben Hutchings4a5b5042008-09-01 12:47:16 +01001671 /* Input validation */
1672 if (len > FALCON_SPI_MAX_LEN)
1673 return -EINVAL;
Ben Hutchingsf4150722008-11-04 20:34:28 +00001674 BUG_ON(!mutex_is_locked(&efx->spi_lock));
Ben Hutchings8ceee662008-04-27 12:55:59 +01001675
Ben Hutchingsbe4ea892008-12-12 21:33:50 -08001676 /* Check that previous command is not still running */
1677 rc = falcon_spi_poll(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001678 if (rc)
1679 return rc;
1680
Ben Hutchings4a5b5042008-09-01 12:47:16 +01001681 /* Program address register, if we have an address */
1682 if (addressed) {
1683 EFX_POPULATE_OWORD_1(reg, EE_SPI_HADR_ADR, address);
1684 falcon_write(efx, &reg, EE_SPI_HADR_REG_KER);
1685 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01001686
Ben Hutchings4a5b5042008-09-01 12:47:16 +01001687 /* Program data register, if we have data */
1688 if (in != NULL) {
1689 memcpy(&reg, in, len);
1690 falcon_write(efx, &reg, EE_SPI_HDATA_REG_KER);
1691 }
1692
1693 /* Issue read/write command */
Ben Hutchings8ceee662008-04-27 12:55:59 +01001694 EFX_POPULATE_OWORD_7(reg,
1695 EE_SPI_HCMD_CMD_EN, 1,
Ben Hutchings4a5b5042008-09-01 12:47:16 +01001696 EE_SPI_HCMD_SF_SEL, spi->device_id,
Ben Hutchings8ceee662008-04-27 12:55:59 +01001697 EE_SPI_HCMD_DABCNT, len,
Ben Hutchings4a5b5042008-09-01 12:47:16 +01001698 EE_SPI_HCMD_READ, reading,
Ben Hutchings8ceee662008-04-27 12:55:59 +01001699 EE_SPI_HCMD_DUBCNT, 0,
Ben Hutchings4a5b5042008-09-01 12:47:16 +01001700 EE_SPI_HCMD_ADBCNT,
1701 (addressed ? spi->addr_len : 0),
Ben Hutchings8ceee662008-04-27 12:55:59 +01001702 EE_SPI_HCMD_ENC, command);
1703 falcon_write(efx, &reg, EE_SPI_HCMD_REG_KER);
1704
Ben Hutchings4a5b5042008-09-01 12:47:16 +01001705 /* Wait for read/write to complete */
Ben Hutchings8ceee662008-04-27 12:55:59 +01001706 rc = falcon_spi_wait(efx);
1707 if (rc)
1708 return rc;
1709
1710 /* Read data */
Ben Hutchings4a5b5042008-09-01 12:47:16 +01001711 if (out != NULL) {
1712 falcon_read(efx, &reg, EE_SPI_HDATA_REG_KER);
1713 memcpy(out, &reg, len);
1714 }
1715
Ben Hutchings8ceee662008-04-27 12:55:59 +01001716 return 0;
1717}
1718
Ben Hutchings23d30f02008-12-12 21:56:11 -08001719static size_t
1720falcon_spi_write_limit(const struct efx_spi_device *spi, size_t start)
Ben Hutchings4a5b5042008-09-01 12:47:16 +01001721{
1722 return min(FALCON_SPI_MAX_LEN,
1723 (spi->block_size - (start & (spi->block_size - 1))));
1724}
1725
1726static inline u8
1727efx_spi_munge_command(const struct efx_spi_device *spi,
1728 const u8 command, const unsigned int address)
1729{
1730 return command | (((address >> 8) & spi->munge_address) << 3);
1731}
1732
Ben Hutchingsbe4ea892008-12-12 21:33:50 -08001733/* Wait up to 10 ms for buffered write completion */
1734int falcon_spi_wait_write(const struct efx_spi_device *spi)
Ben Hutchings4a5b5042008-09-01 12:47:16 +01001735{
Ben Hutchingsbe4ea892008-12-12 21:33:50 -08001736 struct efx_nic *efx = spi->efx;
1737 unsigned long timeout = jiffies + 1 + DIV_ROUND_UP(HZ, 100);
Ben Hutchings4a5b5042008-09-01 12:47:16 +01001738 u8 status;
Ben Hutchingsbe4ea892008-12-12 21:33:50 -08001739 int rc;
Ben Hutchings4a5b5042008-09-01 12:47:16 +01001740
Ben Hutchingsbe4ea892008-12-12 21:33:50 -08001741 for (;;) {
Ben Hutchings4a5b5042008-09-01 12:47:16 +01001742 rc = falcon_spi_cmd(spi, SPI_RDSR, -1, NULL,
1743 &status, sizeof(status));
1744 if (rc)
1745 return rc;
1746 if (!(status & SPI_STATUS_NRDY))
1747 return 0;
Ben Hutchingsbe4ea892008-12-12 21:33:50 -08001748 if (time_after_eq(jiffies, timeout)) {
1749 EFX_ERR(efx, "SPI write timeout on device %d"
1750 " last status=0x%02x\n",
1751 spi->device_id, status);
1752 return -ETIMEDOUT;
1753 }
1754 schedule_timeout_uninterruptible(1);
Ben Hutchings4a5b5042008-09-01 12:47:16 +01001755 }
Ben Hutchings4a5b5042008-09-01 12:47:16 +01001756}
1757
1758int falcon_spi_read(const struct efx_spi_device *spi, loff_t start,
1759 size_t len, size_t *retlen, u8 *buffer)
1760{
Ben Hutchings23d30f02008-12-12 21:56:11 -08001761 size_t block_len, pos = 0;
1762 unsigned int command;
Ben Hutchings4a5b5042008-09-01 12:47:16 +01001763 int rc = 0;
1764
1765 while (pos < len) {
Ben Hutchings23d30f02008-12-12 21:56:11 -08001766 block_len = min(len - pos, FALCON_SPI_MAX_LEN);
Ben Hutchings4a5b5042008-09-01 12:47:16 +01001767
1768 command = efx_spi_munge_command(spi, SPI_READ, start + pos);
1769 rc = falcon_spi_cmd(spi, command, start + pos, NULL,
1770 buffer + pos, block_len);
1771 if (rc)
1772 break;
1773 pos += block_len;
1774
1775 /* Avoid locking up the system */
1776 cond_resched();
1777 if (signal_pending(current)) {
1778 rc = -EINTR;
1779 break;
1780 }
1781 }
1782
1783 if (retlen)
1784 *retlen = pos;
1785 return rc;
1786}
1787
1788int falcon_spi_write(const struct efx_spi_device *spi, loff_t start,
1789 size_t len, size_t *retlen, const u8 *buffer)
1790{
1791 u8 verify_buffer[FALCON_SPI_MAX_LEN];
Ben Hutchings23d30f02008-12-12 21:56:11 -08001792 size_t block_len, pos = 0;
1793 unsigned int command;
Ben Hutchings4a5b5042008-09-01 12:47:16 +01001794 int rc = 0;
1795
1796 while (pos < len) {
1797 rc = falcon_spi_cmd(spi, SPI_WREN, -1, NULL, NULL, 0);
1798 if (rc)
1799 break;
1800
Ben Hutchings23d30f02008-12-12 21:56:11 -08001801 block_len = min(len - pos,
Ben Hutchings4a5b5042008-09-01 12:47:16 +01001802 falcon_spi_write_limit(spi, start + pos));
1803 command = efx_spi_munge_command(spi, SPI_WRITE, start + pos);
1804 rc = falcon_spi_cmd(spi, command, start + pos,
1805 buffer + pos, NULL, block_len);
1806 if (rc)
1807 break;
1808
Ben Hutchingsbe4ea892008-12-12 21:33:50 -08001809 rc = falcon_spi_wait_write(spi);
Ben Hutchings4a5b5042008-09-01 12:47:16 +01001810 if (rc)
1811 break;
1812
1813 command = efx_spi_munge_command(spi, SPI_READ, start + pos);
1814 rc = falcon_spi_cmd(spi, command, start + pos,
1815 NULL, verify_buffer, block_len);
1816 if (memcmp(verify_buffer, buffer + pos, block_len)) {
1817 rc = -EIO;
1818 break;
1819 }
1820
1821 pos += block_len;
1822
1823 /* Avoid locking up the system */
1824 cond_resched();
1825 if (signal_pending(current)) {
1826 rc = -EINTR;
1827 break;
1828 }
1829 }
1830
1831 if (retlen)
1832 *retlen = pos;
1833 return rc;
1834}
1835
Ben Hutchings8ceee662008-04-27 12:55:59 +01001836/**************************************************************************
1837 *
1838 * MAC wrapper
1839 *
1840 **************************************************************************
1841 */
1842void falcon_drain_tx_fifo(struct efx_nic *efx)
1843{
1844 efx_oword_t temp;
1845 int count;
1846
Ben Hutchings55668612008-05-16 21:16:10 +01001847 if ((falcon_rev(efx) < FALCON_REV_B0) ||
Ben Hutchings3273c2e2008-05-07 13:36:19 +01001848 (efx->loopback_mode != LOOPBACK_NONE))
Ben Hutchings8ceee662008-04-27 12:55:59 +01001849 return;
1850
1851 falcon_read(efx, &temp, MAC0_CTRL_REG_KER);
1852 /* There is no point in draining more than once */
1853 if (EFX_OWORD_FIELD(temp, TXFIFO_DRAIN_EN_B0))
1854 return;
1855
1856 /* MAC stats will fail whilst the TX fifo is draining. Serialise
1857 * the drain sequence with the statistics fetch */
1858 spin_lock(&efx->stats_lock);
1859
1860 EFX_SET_OWORD_FIELD(temp, TXFIFO_DRAIN_EN_B0, 1);
1861 falcon_write(efx, &temp, MAC0_CTRL_REG_KER);
1862
1863 /* Reset the MAC and EM block. */
1864 falcon_read(efx, &temp, GLB_CTL_REG_KER);
1865 EFX_SET_OWORD_FIELD(temp, RST_XGTX, 1);
1866 EFX_SET_OWORD_FIELD(temp, RST_XGRX, 1);
1867 EFX_SET_OWORD_FIELD(temp, RST_EM, 1);
1868 falcon_write(efx, &temp, GLB_CTL_REG_KER);
1869
1870 count = 0;
1871 while (1) {
1872 falcon_read(efx, &temp, GLB_CTL_REG_KER);
1873 if (!EFX_OWORD_FIELD(temp, RST_XGTX) &&
1874 !EFX_OWORD_FIELD(temp, RST_XGRX) &&
1875 !EFX_OWORD_FIELD(temp, RST_EM)) {
1876 EFX_LOG(efx, "Completed MAC reset after %d loops\n",
1877 count);
1878 break;
1879 }
1880 if (count > 20) {
1881 EFX_ERR(efx, "MAC reset failed\n");
1882 break;
1883 }
1884 count++;
1885 udelay(10);
1886 }
1887
1888 spin_unlock(&efx->stats_lock);
1889
1890 /* If we've reset the EM block and the link is up, then
1891 * we'll have to kick the XAUI link so the PHY can recover */
1892 if (efx->link_up && EFX_WORKAROUND_5147(efx))
1893 falcon_reset_xaui(efx);
1894}
1895
1896void falcon_deconfigure_mac_wrapper(struct efx_nic *efx)
1897{
1898 efx_oword_t temp;
1899
Ben Hutchings55668612008-05-16 21:16:10 +01001900 if (falcon_rev(efx) < FALCON_REV_B0)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001901 return;
1902
1903 /* Isolate the MAC -> RX */
1904 falcon_read(efx, &temp, RX_CFG_REG_KER);
1905 EFX_SET_OWORD_FIELD(temp, RX_INGR_EN_B0, 0);
1906 falcon_write(efx, &temp, RX_CFG_REG_KER);
1907
1908 if (!efx->link_up)
1909 falcon_drain_tx_fifo(efx);
1910}
1911
1912void falcon_reconfigure_mac_wrapper(struct efx_nic *efx)
1913{
1914 efx_oword_t reg;
1915 int link_speed;
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +01001916 bool tx_fc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001917
Ben Hutchingsf31a45d2008-12-12 21:43:33 -08001918 switch (efx->link_speed) {
1919 case 10000: link_speed = 3; break;
1920 case 1000: link_speed = 2; break;
1921 case 100: link_speed = 1; break;
1922 default: link_speed = 0; break;
1923 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01001924 /* MAC_LINK_STATUS controls MAC backpressure but doesn't work
1925 * as advertised. Disable to ensure packets are not
1926 * indefinitely held and TX queue can be flushed at any point
1927 * while the link is down. */
1928 EFX_POPULATE_OWORD_5(reg,
1929 MAC_XOFF_VAL, 0xffff /* max pause time */,
1930 MAC_BCAD_ACPT, 1,
1931 MAC_UC_PROM, efx->promiscuous,
1932 MAC_LINK_STATUS, 1, /* always set */
1933 MAC_SPEED, link_speed);
1934 /* On B0, MAC backpressure can be disabled and packets get
1935 * discarded. */
Ben Hutchings55668612008-05-16 21:16:10 +01001936 if (falcon_rev(efx) >= FALCON_REV_B0) {
Ben Hutchings8ceee662008-04-27 12:55:59 +01001937 EFX_SET_OWORD_FIELD(reg, TXFIFO_DRAIN_EN_B0,
1938 !efx->link_up);
1939 }
1940
1941 falcon_write(efx, &reg, MAC0_CTRL_REG_KER);
1942
1943 /* Restore the multicast hash registers. */
1944 falcon_set_multicast_hash(efx);
1945
1946 /* Transmission of pause frames when RX crosses the threshold is
1947 * covered by RX_XOFF_MAC_EN and XM_TX_CFG_REG:XM_FCNTL.
1948 * Action on receipt of pause frames is controller by XM_DIS_FCNTL */
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +01001949 tx_fc = !!(efx->flow_control & EFX_FC_TX);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001950 falcon_read(efx, &reg, RX_CFG_REG_KER);
1951 EFX_SET_OWORD_FIELD_VER(efx, reg, RX_XOFF_MAC_EN, tx_fc);
1952
1953 /* Unisolate the MAC -> RX */
Ben Hutchings55668612008-05-16 21:16:10 +01001954 if (falcon_rev(efx) >= FALCON_REV_B0)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001955 EFX_SET_OWORD_FIELD(reg, RX_INGR_EN_B0, 1);
1956 falcon_write(efx, &reg, RX_CFG_REG_KER);
1957}
1958
1959int falcon_dma_stats(struct efx_nic *efx, unsigned int done_offset)
1960{
1961 efx_oword_t reg;
1962 u32 *dma_done;
1963 int i;
1964
1965 if (disable_dma_stats)
1966 return 0;
1967
1968 /* Statistics fetch will fail if the MAC is in TX drain */
Ben Hutchings55668612008-05-16 21:16:10 +01001969 if (falcon_rev(efx) >= FALCON_REV_B0) {
Ben Hutchings8ceee662008-04-27 12:55:59 +01001970 efx_oword_t temp;
1971 falcon_read(efx, &temp, MAC0_CTRL_REG_KER);
1972 if (EFX_OWORD_FIELD(temp, TXFIFO_DRAIN_EN_B0))
1973 return 0;
1974 }
1975
1976 dma_done = (efx->stats_buffer.addr + done_offset);
1977 *dma_done = FALCON_STATS_NOT_DONE;
1978 wmb(); /* ensure done flag is clear */
1979
1980 /* Initiate DMA transfer of stats */
1981 EFX_POPULATE_OWORD_2(reg,
1982 MAC_STAT_DMA_CMD, 1,
1983 MAC_STAT_DMA_ADR,
1984 efx->stats_buffer.dma_addr);
1985 falcon_write(efx, &reg, MAC0_STAT_DMA_REG_KER);
1986
1987 /* Wait for transfer to complete */
1988 for (i = 0; i < 400; i++) {
Ben Hutchings1d0680f2008-09-01 12:50:08 +01001989 if (*(volatile u32 *)dma_done == FALCON_STATS_DONE) {
1990 rmb(); /* Ensure the stats are valid. */
Ben Hutchings8ceee662008-04-27 12:55:59 +01001991 return 0;
Ben Hutchings1d0680f2008-09-01 12:50:08 +01001992 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01001993 udelay(10);
1994 }
1995
1996 EFX_ERR(efx, "timed out waiting for statistics\n");
1997 return -ETIMEDOUT;
1998}
1999
2000/**************************************************************************
2001 *
2002 * PHY access via GMII
2003 *
2004 **************************************************************************
2005 */
2006
2007/* Use the top bit of the MII PHY id to indicate the PHY type
2008 * (1G/10G), with the remaining bits as the actual PHY id.
2009 *
2010 * This allows us to avoid leaking information from the mii_if_info
2011 * structure into other data structures.
2012 */
2013#define FALCON_PHY_ID_ID_WIDTH EFX_WIDTH(MD_PRT_DEV_ADR)
2014#define FALCON_PHY_ID_ID_MASK ((1 << FALCON_PHY_ID_ID_WIDTH) - 1)
2015#define FALCON_PHY_ID_WIDTH (FALCON_PHY_ID_ID_WIDTH + 1)
2016#define FALCON_PHY_ID_MASK ((1 << FALCON_PHY_ID_WIDTH) - 1)
2017#define FALCON_PHY_ID_10G (1 << (FALCON_PHY_ID_WIDTH - 1))
2018
2019
2020/* Packing the clause 45 port and device fields into a single value */
2021#define MD_PRT_ADR_COMP_LBN (MD_PRT_ADR_LBN - MD_DEV_ADR_LBN)
2022#define MD_PRT_ADR_COMP_WIDTH MD_PRT_ADR_WIDTH
2023#define MD_DEV_ADR_COMP_LBN 0
2024#define MD_DEV_ADR_COMP_WIDTH MD_DEV_ADR_WIDTH
2025
2026
2027/* Wait for GMII access to complete */
2028static int falcon_gmii_wait(struct efx_nic *efx)
2029{
2030 efx_dword_t md_stat;
2031 int count;
2032
2033 for (count = 0; count < 1000; count++) { /* wait upto 10ms */
2034 falcon_readl(efx, &md_stat, MD_STAT_REG_KER);
2035 if (EFX_DWORD_FIELD(md_stat, MD_BSY) == 0) {
2036 if (EFX_DWORD_FIELD(md_stat, MD_LNFL) != 0 ||
2037 EFX_DWORD_FIELD(md_stat, MD_BSERR) != 0) {
2038 EFX_ERR(efx, "error from GMII access "
2039 EFX_DWORD_FMT"\n",
2040 EFX_DWORD_VAL(md_stat));
2041 return -EIO;
2042 }
2043 return 0;
2044 }
2045 udelay(10);
2046 }
2047 EFX_ERR(efx, "timed out waiting for GMII\n");
2048 return -ETIMEDOUT;
2049}
2050
2051/* Writes a GMII register of a PHY connected to Falcon using MDIO. */
2052static void falcon_mdio_write(struct net_device *net_dev, int phy_id,
2053 int addr, int value)
2054{
Ben Hutchings767e4682008-09-01 12:43:14 +01002055 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002056 unsigned int phy_id2 = phy_id & FALCON_PHY_ID_ID_MASK;
2057 efx_oword_t reg;
2058
2059 /* The 'generic' prt/dev packing in mdio_10g.h is conveniently
2060 * chosen so that the only current user, Falcon, can take the
2061 * packed value and use them directly.
2062 * Fail to build if this assumption is broken.
2063 */
2064 BUILD_BUG_ON(FALCON_PHY_ID_10G != MDIO45_XPRT_ID_IS10G);
2065 BUILD_BUG_ON(FALCON_PHY_ID_ID_WIDTH != MDIO45_PRT_DEV_WIDTH);
2066 BUILD_BUG_ON(MD_PRT_ADR_COMP_LBN != MDIO45_PRT_ID_COMP_LBN);
2067 BUILD_BUG_ON(MD_DEV_ADR_COMP_LBN != MDIO45_DEV_ID_COMP_LBN);
2068
2069 if (phy_id2 == PHY_ADDR_INVALID)
2070 return;
2071
2072 /* See falcon_mdio_read for an explanation. */
2073 if (!(phy_id & FALCON_PHY_ID_10G)) {
2074 int mmd = ffs(efx->phy_op->mmds) - 1;
2075 EFX_TRACE(efx, "Fixing erroneous clause22 write\n");
2076 phy_id2 = mdio_clause45_pack(phy_id2, mmd)
2077 & FALCON_PHY_ID_ID_MASK;
2078 }
2079
2080 EFX_REGDUMP(efx, "writing GMII %d register %02x with %04x\n", phy_id,
2081 addr, value);
2082
2083 spin_lock_bh(&efx->phy_lock);
2084
2085 /* Check MII not currently being accessed */
2086 if (falcon_gmii_wait(efx) != 0)
2087 goto out;
2088
2089 /* Write the address/ID register */
2090 EFX_POPULATE_OWORD_1(reg, MD_PHY_ADR, addr);
2091 falcon_write(efx, &reg, MD_PHY_ADR_REG_KER);
2092
2093 EFX_POPULATE_OWORD_1(reg, MD_PRT_DEV_ADR, phy_id2);
2094 falcon_write(efx, &reg, MD_ID_REG_KER);
2095
2096 /* Write data */
2097 EFX_POPULATE_OWORD_1(reg, MD_TXD, value);
2098 falcon_write(efx, &reg, MD_TXD_REG_KER);
2099
2100 EFX_POPULATE_OWORD_2(reg,
2101 MD_WRC, 1,
2102 MD_GC, 0);
2103 falcon_write(efx, &reg, MD_CS_REG_KER);
2104
2105 /* Wait for data to be written */
2106 if (falcon_gmii_wait(efx) != 0) {
2107 /* Abort the write operation */
2108 EFX_POPULATE_OWORD_2(reg,
2109 MD_WRC, 0,
2110 MD_GC, 1);
2111 falcon_write(efx, &reg, MD_CS_REG_KER);
2112 udelay(10);
2113 }
2114
2115 out:
2116 spin_unlock_bh(&efx->phy_lock);
2117}
2118
2119/* Reads a GMII register from a PHY connected to Falcon. If no value
2120 * could be read, -1 will be returned. */
2121static int falcon_mdio_read(struct net_device *net_dev, int phy_id, int addr)
2122{
Ben Hutchings767e4682008-09-01 12:43:14 +01002123 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002124 unsigned int phy_addr = phy_id & FALCON_PHY_ID_ID_MASK;
2125 efx_oword_t reg;
2126 int value = -1;
2127
2128 if (phy_addr == PHY_ADDR_INVALID)
2129 return -1;
2130
2131 /* Our PHY code knows whether it needs to talk clause 22(1G) or 45(10G)
2132 * but the generic Linux code does not make any distinction or have
2133 * any state for this.
2134 * We spot the case where someone tried to talk 22 to a 45 PHY and
2135 * redirect the request to the lowest numbered MMD as a clause45
2136 * request. This is enough to allow simple queries like id and link
2137 * state to succeed. TODO: We may need to do more in future.
2138 */
2139 if (!(phy_id & FALCON_PHY_ID_10G)) {
2140 int mmd = ffs(efx->phy_op->mmds) - 1;
2141 EFX_TRACE(efx, "Fixing erroneous clause22 read\n");
2142 phy_addr = mdio_clause45_pack(phy_addr, mmd)
2143 & FALCON_PHY_ID_ID_MASK;
2144 }
2145
2146 spin_lock_bh(&efx->phy_lock);
2147
2148 /* Check MII not currently being accessed */
2149 if (falcon_gmii_wait(efx) != 0)
2150 goto out;
2151
2152 EFX_POPULATE_OWORD_1(reg, MD_PHY_ADR, addr);
2153 falcon_write(efx, &reg, MD_PHY_ADR_REG_KER);
2154
2155 EFX_POPULATE_OWORD_1(reg, MD_PRT_DEV_ADR, phy_addr);
2156 falcon_write(efx, &reg, MD_ID_REG_KER);
2157
2158 /* Request data to be read */
2159 EFX_POPULATE_OWORD_2(reg, MD_RDC, 1, MD_GC, 0);
2160 falcon_write(efx, &reg, MD_CS_REG_KER);
2161
2162 /* Wait for data to become available */
2163 value = falcon_gmii_wait(efx);
2164 if (value == 0) {
2165 falcon_read(efx, &reg, MD_RXD_REG_KER);
2166 value = EFX_OWORD_FIELD(reg, MD_RXD);
2167 EFX_REGDUMP(efx, "read from GMII %d register %02x, got %04x\n",
2168 phy_id, addr, value);
2169 } else {
2170 /* Abort the read operation */
2171 EFX_POPULATE_OWORD_2(reg,
2172 MD_RIC, 0,
2173 MD_GC, 1);
2174 falcon_write(efx, &reg, MD_CS_REG_KER);
2175
2176 EFX_LOG(efx, "read from GMII 0x%x register %02x, got "
2177 "error %d\n", phy_id, addr, value);
2178 }
2179
2180 out:
2181 spin_unlock_bh(&efx->phy_lock);
2182
2183 return value;
2184}
2185
2186static void falcon_init_mdio(struct mii_if_info *gmii)
2187{
2188 gmii->mdio_read = falcon_mdio_read;
2189 gmii->mdio_write = falcon_mdio_write;
2190 gmii->phy_id_mask = FALCON_PHY_ID_MASK;
2191 gmii->reg_num_mask = ((1 << EFX_WIDTH(MD_PHY_ADR)) - 1);
2192}
2193
2194static int falcon_probe_phy(struct efx_nic *efx)
2195{
2196 switch (efx->phy_type) {
2197 case PHY_TYPE_10XPRESS:
2198 efx->phy_op = &falcon_tenxpress_phy_ops;
2199 break;
2200 case PHY_TYPE_XFP:
2201 efx->phy_op = &falcon_xfp_phy_ops;
2202 break;
2203 default:
2204 EFX_ERR(efx, "Unknown PHY type %d\n",
2205 efx->phy_type);
2206 return -1;
2207 }
Ben Hutchings3273c2e2008-05-07 13:36:19 +01002208
2209 efx->loopback_modes = LOOPBACKS_10G_INTERNAL | efx->phy_op->loopbacks;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002210 return 0;
2211}
2212
2213/* This call is responsible for hooking in the MAC and PHY operations */
2214int falcon_probe_port(struct efx_nic *efx)
2215{
2216 int rc;
2217
2218 /* Hook in PHY operations table */
2219 rc = falcon_probe_phy(efx);
2220 if (rc)
2221 return rc;
2222
2223 /* Set up GMII structure for PHY */
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +01002224 efx->mii.supports_gmii = true;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002225 falcon_init_mdio(&efx->mii);
2226
2227 /* Hardware flow ctrl. FalconA RX FIFO too small for pause generation */
Ben Hutchings55668612008-05-16 21:16:10 +01002228 if (falcon_rev(efx) >= FALCON_REV_B0)
Ben Hutchings8ceee662008-04-27 12:55:59 +01002229 efx->flow_control = EFX_FC_RX | EFX_FC_TX;
2230 else
2231 efx->flow_control = EFX_FC_RX;
2232
2233 /* Allocate buffer for stats */
2234 rc = falcon_alloc_buffer(efx, &efx->stats_buffer,
2235 FALCON_MAC_STATS_SIZE);
2236 if (rc)
2237 return rc;
2238 EFX_LOG(efx, "stats buffer at %llx (virt %p phys %lx)\n",
2239 (unsigned long long)efx->stats_buffer.dma_addr,
2240 efx->stats_buffer.addr,
2241 virt_to_phys(efx->stats_buffer.addr));
2242
2243 return 0;
2244}
2245
2246void falcon_remove_port(struct efx_nic *efx)
2247{
2248 falcon_free_buffer(efx, &efx->stats_buffer);
2249}
2250
2251/**************************************************************************
2252 *
2253 * Multicast filtering
2254 *
2255 **************************************************************************
2256 */
2257
2258void falcon_set_multicast_hash(struct efx_nic *efx)
2259{
2260 union efx_multicast_hash *mc_hash = &efx->multicast_hash;
2261
2262 /* Broadcast packets go through the multicast hash filter.
2263 * ether_crc_le() of the broadcast address is 0xbe2612ff
2264 * so we always add bit 0xff to the mask.
2265 */
2266 set_bit_le(0xff, mc_hash->byte);
2267
2268 falcon_write(efx, &mc_hash->oword[0], MAC_MCAST_HASH_REG0_KER);
2269 falcon_write(efx, &mc_hash->oword[1], MAC_MCAST_HASH_REG1_KER);
2270}
2271
Ben Hutchings8c8661e2008-09-01 12:49:02 +01002272
2273/**************************************************************************
2274 *
2275 * Falcon test code
2276 *
2277 **************************************************************************/
2278
2279int falcon_read_nvram(struct efx_nic *efx, struct falcon_nvconfig *nvconfig_out)
2280{
2281 struct falcon_nvconfig *nvconfig;
2282 struct efx_spi_device *spi;
2283 void *region;
2284 int rc, magic_num, struct_ver;
2285 __le16 *word, *limit;
2286 u32 csum;
2287
Ben Hutchings2f7f5732008-12-12 21:34:25 -08002288 spi = efx->spi_flash ? efx->spi_flash : efx->spi_eeprom;
2289 if (!spi)
2290 return -EINVAL;
2291
Ben Hutchings0a95f562008-11-04 20:33:11 +00002292 region = kmalloc(FALCON_NVCONFIG_END, GFP_KERNEL);
Ben Hutchings8c8661e2008-09-01 12:49:02 +01002293 if (!region)
2294 return -ENOMEM;
2295 nvconfig = region + NVCONFIG_OFFSET;
2296
Ben Hutchingsf4150722008-11-04 20:34:28 +00002297 mutex_lock(&efx->spi_lock);
Ben Hutchings0a95f562008-11-04 20:33:11 +00002298 rc = falcon_spi_read(spi, 0, FALCON_NVCONFIG_END, NULL, region);
Ben Hutchingsf4150722008-11-04 20:34:28 +00002299 mutex_unlock(&efx->spi_lock);
Ben Hutchings8c8661e2008-09-01 12:49:02 +01002300 if (rc) {
2301 EFX_ERR(efx, "Failed to read %s\n",
2302 efx->spi_flash ? "flash" : "EEPROM");
2303 rc = -EIO;
2304 goto out;
2305 }
2306
2307 magic_num = le16_to_cpu(nvconfig->board_magic_num);
2308 struct_ver = le16_to_cpu(nvconfig->board_struct_ver);
2309
2310 rc = -EINVAL;
2311 if (magic_num != NVCONFIG_BOARD_MAGIC_NUM) {
2312 EFX_ERR(efx, "NVRAM bad magic 0x%x\n", magic_num);
2313 goto out;
2314 }
2315 if (struct_ver < 2) {
2316 EFX_ERR(efx, "NVRAM has ancient version 0x%x\n", struct_ver);
2317 goto out;
2318 } else if (struct_ver < 4) {
2319 word = &nvconfig->board_magic_num;
2320 limit = (__le16 *) (nvconfig + 1);
2321 } else {
2322 word = region;
Ben Hutchings0a95f562008-11-04 20:33:11 +00002323 limit = region + FALCON_NVCONFIG_END;
Ben Hutchings8c8661e2008-09-01 12:49:02 +01002324 }
2325 for (csum = 0; word < limit; ++word)
2326 csum += le16_to_cpu(*word);
2327
2328 if (~csum & 0xffff) {
2329 EFX_ERR(efx, "NVRAM has incorrect checksum\n");
2330 goto out;
2331 }
2332
2333 rc = 0;
2334 if (nvconfig_out)
2335 memcpy(nvconfig_out, nvconfig, sizeof(*nvconfig));
2336
2337 out:
2338 kfree(region);
2339 return rc;
2340}
2341
2342/* Registers tested in the falcon register test */
2343static struct {
2344 unsigned address;
2345 efx_oword_t mask;
2346} efx_test_registers[] = {
2347 { ADR_REGION_REG_KER,
2348 EFX_OWORD32(0x0001FFFF, 0x0001FFFF, 0x0001FFFF, 0x0001FFFF) },
2349 { RX_CFG_REG_KER,
2350 EFX_OWORD32(0xFFFFFFFE, 0x00017FFF, 0x00000000, 0x00000000) },
2351 { TX_CFG_REG_KER,
2352 EFX_OWORD32(0x7FFF0037, 0x00000000, 0x00000000, 0x00000000) },
2353 { TX_CFG2_REG_KER,
2354 EFX_OWORD32(0xFFFEFE80, 0x1FFFFFFF, 0x020000FE, 0x007FFFFF) },
2355 { MAC0_CTRL_REG_KER,
2356 EFX_OWORD32(0xFFFF0000, 0x00000000, 0x00000000, 0x00000000) },
2357 { SRM_TX_DC_CFG_REG_KER,
2358 EFX_OWORD32(0x001FFFFF, 0x00000000, 0x00000000, 0x00000000) },
2359 { RX_DC_CFG_REG_KER,
2360 EFX_OWORD32(0x0000000F, 0x00000000, 0x00000000, 0x00000000) },
2361 { RX_DC_PF_WM_REG_KER,
2362 EFX_OWORD32(0x000003FF, 0x00000000, 0x00000000, 0x00000000) },
2363 { DP_CTRL_REG,
2364 EFX_OWORD32(0x00000FFF, 0x00000000, 0x00000000, 0x00000000) },
2365 { XM_GLB_CFG_REG,
2366 EFX_OWORD32(0x00000C68, 0x00000000, 0x00000000, 0x00000000) },
2367 { XM_TX_CFG_REG,
2368 EFX_OWORD32(0x00080164, 0x00000000, 0x00000000, 0x00000000) },
2369 { XM_RX_CFG_REG,
2370 EFX_OWORD32(0x07100A0C, 0x00000000, 0x00000000, 0x00000000) },
2371 { XM_RX_PARAM_REG,
2372 EFX_OWORD32(0x00001FF8, 0x00000000, 0x00000000, 0x00000000) },
2373 { XM_FC_REG,
2374 EFX_OWORD32(0xFFFF0001, 0x00000000, 0x00000000, 0x00000000) },
2375 { XM_ADR_LO_REG,
2376 EFX_OWORD32(0xFFFFFFFF, 0x00000000, 0x00000000, 0x00000000) },
2377 { XX_SD_CTL_REG,
2378 EFX_OWORD32(0x0003FF0F, 0x00000000, 0x00000000, 0x00000000) },
2379};
2380
2381static bool efx_masked_compare_oword(const efx_oword_t *a, const efx_oword_t *b,
2382 const efx_oword_t *mask)
2383{
2384 return ((a->u64[0] ^ b->u64[0]) & mask->u64[0]) ||
2385 ((a->u64[1] ^ b->u64[1]) & mask->u64[1]);
2386}
2387
2388int falcon_test_registers(struct efx_nic *efx)
2389{
2390 unsigned address = 0, i, j;
2391 efx_oword_t mask, imask, original, reg, buf;
2392
2393 /* Falcon should be in loopback to isolate the XMAC from the PHY */
2394 WARN_ON(!LOOPBACK_INTERNAL(efx));
2395
2396 for (i = 0; i < ARRAY_SIZE(efx_test_registers); ++i) {
2397 address = efx_test_registers[i].address;
2398 mask = imask = efx_test_registers[i].mask;
2399 EFX_INVERT_OWORD(imask);
2400
2401 falcon_read(efx, &original, address);
2402
2403 /* bit sweep on and off */
2404 for (j = 0; j < 128; j++) {
2405 if (!EFX_EXTRACT_OWORD32(mask, j, j))
2406 continue;
2407
2408 /* Test this testable bit can be set in isolation */
2409 EFX_AND_OWORD(reg, original, mask);
2410 EFX_SET_OWORD32(reg, j, j, 1);
2411
2412 falcon_write(efx, &reg, address);
2413 falcon_read(efx, &buf, address);
2414
2415 if (efx_masked_compare_oword(&reg, &buf, &mask))
2416 goto fail;
2417
2418 /* Test this testable bit can be cleared in isolation */
2419 EFX_OR_OWORD(reg, original, mask);
2420 EFX_SET_OWORD32(reg, j, j, 0);
2421
2422 falcon_write(efx, &reg, address);
2423 falcon_read(efx, &buf, address);
2424
2425 if (efx_masked_compare_oword(&reg, &buf, &mask))
2426 goto fail;
2427 }
2428
2429 falcon_write(efx, &original, address);
2430 }
2431
2432 return 0;
2433
2434fail:
2435 EFX_ERR(efx, "wrote "EFX_OWORD_FMT" read "EFX_OWORD_FMT
2436 " at address 0x%x mask "EFX_OWORD_FMT"\n", EFX_OWORD_VAL(reg),
2437 EFX_OWORD_VAL(buf), address, EFX_OWORD_VAL(mask));
2438 return -EIO;
2439}
2440
Ben Hutchings8ceee662008-04-27 12:55:59 +01002441/**************************************************************************
2442 *
2443 * Device reset
2444 *
2445 **************************************************************************
2446 */
2447
2448/* Resets NIC to known state. This routine must be called in process
2449 * context and is allowed to sleep. */
2450int falcon_reset_hw(struct efx_nic *efx, enum reset_type method)
2451{
2452 struct falcon_nic_data *nic_data = efx->nic_data;
2453 efx_oword_t glb_ctl_reg_ker;
2454 int rc;
2455
2456 EFX_LOG(efx, "performing hardware reset (%d)\n", method);
2457
2458 /* Initiate device reset */
2459 if (method == RESET_TYPE_WORLD) {
2460 rc = pci_save_state(efx->pci_dev);
2461 if (rc) {
2462 EFX_ERR(efx, "failed to backup PCI state of primary "
2463 "function prior to hardware reset\n");
2464 goto fail1;
2465 }
2466 if (FALCON_IS_DUAL_FUNC(efx)) {
2467 rc = pci_save_state(nic_data->pci_dev2);
2468 if (rc) {
2469 EFX_ERR(efx, "failed to backup PCI state of "
2470 "secondary function prior to "
2471 "hardware reset\n");
2472 goto fail2;
2473 }
2474 }
2475
2476 EFX_POPULATE_OWORD_2(glb_ctl_reg_ker,
2477 EXT_PHY_RST_DUR, 0x7,
2478 SWRST, 1);
2479 } else {
2480 int reset_phy = (method == RESET_TYPE_INVISIBLE ?
2481 EXCLUDE_FROM_RESET : 0);
2482
2483 EFX_POPULATE_OWORD_7(glb_ctl_reg_ker,
2484 EXT_PHY_RST_CTL, reset_phy,
2485 PCIE_CORE_RST_CTL, EXCLUDE_FROM_RESET,
2486 PCIE_NSTCK_RST_CTL, EXCLUDE_FROM_RESET,
2487 PCIE_SD_RST_CTL, EXCLUDE_FROM_RESET,
2488 EE_RST_CTL, EXCLUDE_FROM_RESET,
2489 EXT_PHY_RST_DUR, 0x7 /* 10ms */,
2490 SWRST, 1);
2491 }
2492 falcon_write(efx, &glb_ctl_reg_ker, GLB_CTL_REG_KER);
2493
2494 EFX_LOG(efx, "waiting for hardware reset\n");
2495 schedule_timeout_uninterruptible(HZ / 20);
2496
2497 /* Restore PCI configuration if needed */
2498 if (method == RESET_TYPE_WORLD) {
2499 if (FALCON_IS_DUAL_FUNC(efx)) {
2500 rc = pci_restore_state(nic_data->pci_dev2);
2501 if (rc) {
2502 EFX_ERR(efx, "failed to restore PCI config for "
2503 "the secondary function\n");
2504 goto fail3;
2505 }
2506 }
2507 rc = pci_restore_state(efx->pci_dev);
2508 if (rc) {
2509 EFX_ERR(efx, "failed to restore PCI config for the "
2510 "primary function\n");
2511 goto fail4;
2512 }
2513 EFX_LOG(efx, "successfully restored PCI config\n");
2514 }
2515
2516 /* Assert that reset complete */
2517 falcon_read(efx, &glb_ctl_reg_ker, GLB_CTL_REG_KER);
2518 if (EFX_OWORD_FIELD(glb_ctl_reg_ker, SWRST) != 0) {
2519 rc = -ETIMEDOUT;
2520 EFX_ERR(efx, "timed out waiting for hardware reset\n");
2521 goto fail5;
2522 }
2523 EFX_LOG(efx, "hardware reset complete\n");
2524
2525 return 0;
2526
2527 /* pci_save_state() and pci_restore_state() MUST be called in pairs */
2528fail2:
2529fail3:
2530 pci_restore_state(efx->pci_dev);
2531fail1:
2532fail4:
2533fail5:
2534 return rc;
2535}
2536
2537/* Zeroes out the SRAM contents. This routine must be called in
2538 * process context and is allowed to sleep.
2539 */
2540static int falcon_reset_sram(struct efx_nic *efx)
2541{
2542 efx_oword_t srm_cfg_reg_ker, gpio_cfg_reg_ker;
2543 int count;
2544
2545 /* Set the SRAM wake/sleep GPIO appropriately. */
2546 falcon_read(efx, &gpio_cfg_reg_ker, GPIO_CTL_REG_KER);
2547 EFX_SET_OWORD_FIELD(gpio_cfg_reg_ker, GPIO1_OEN, 1);
2548 EFX_SET_OWORD_FIELD(gpio_cfg_reg_ker, GPIO1_OUT, 1);
2549 falcon_write(efx, &gpio_cfg_reg_ker, GPIO_CTL_REG_KER);
2550
2551 /* Initiate SRAM reset */
2552 EFX_POPULATE_OWORD_2(srm_cfg_reg_ker,
2553 SRAM_OOB_BT_INIT_EN, 1,
2554 SRM_NUM_BANKS_AND_BANK_SIZE, 0);
2555 falcon_write(efx, &srm_cfg_reg_ker, SRM_CFG_REG_KER);
2556
2557 /* Wait for SRAM reset to complete */
2558 count = 0;
2559 do {
2560 EFX_LOG(efx, "waiting for SRAM reset (attempt %d)...\n", count);
2561
2562 /* SRAM reset is slow; expect around 16ms */
2563 schedule_timeout_uninterruptible(HZ / 50);
2564
2565 /* Check for reset complete */
2566 falcon_read(efx, &srm_cfg_reg_ker, SRM_CFG_REG_KER);
2567 if (!EFX_OWORD_FIELD(srm_cfg_reg_ker, SRAM_OOB_BT_INIT_EN)) {
2568 EFX_LOG(efx, "SRAM reset complete\n");
2569
2570 return 0;
2571 }
2572 } while (++count < 20); /* wait upto 0.4 sec */
2573
2574 EFX_ERR(efx, "timed out waiting for SRAM reset\n");
2575 return -ETIMEDOUT;
2576}
2577
Ben Hutchings4a5b5042008-09-01 12:47:16 +01002578static int falcon_spi_device_init(struct efx_nic *efx,
2579 struct efx_spi_device **spi_device_ret,
2580 unsigned int device_id, u32 device_type)
2581{
2582 struct efx_spi_device *spi_device;
2583
2584 if (device_type != 0) {
2585 spi_device = kmalloc(sizeof(*spi_device), GFP_KERNEL);
2586 if (!spi_device)
2587 return -ENOMEM;
2588 spi_device->device_id = device_id;
2589 spi_device->size =
2590 1 << SPI_DEV_TYPE_FIELD(device_type, SPI_DEV_TYPE_SIZE);
2591 spi_device->addr_len =
2592 SPI_DEV_TYPE_FIELD(device_type, SPI_DEV_TYPE_ADDR_LEN);
2593 spi_device->munge_address = (spi_device->size == 1 << 9 &&
2594 spi_device->addr_len == 1);
Ben Hutchingsf4150722008-11-04 20:34:28 +00002595 spi_device->erase_command =
2596 SPI_DEV_TYPE_FIELD(device_type, SPI_DEV_TYPE_ERASE_CMD);
2597 spi_device->erase_size =
2598 1 << SPI_DEV_TYPE_FIELD(device_type,
2599 SPI_DEV_TYPE_ERASE_SIZE);
Ben Hutchings4a5b5042008-09-01 12:47:16 +01002600 spi_device->block_size =
2601 1 << SPI_DEV_TYPE_FIELD(device_type,
2602 SPI_DEV_TYPE_BLOCK_SIZE);
2603
2604 spi_device->efx = efx;
2605 } else {
2606 spi_device = NULL;
2607 }
2608
2609 kfree(*spi_device_ret);
2610 *spi_device_ret = spi_device;
2611 return 0;
2612}
2613
2614
2615static void falcon_remove_spi_devices(struct efx_nic *efx)
2616{
2617 kfree(efx->spi_eeprom);
2618 efx->spi_eeprom = NULL;
2619 kfree(efx->spi_flash);
2620 efx->spi_flash = NULL;
2621}
2622
Ben Hutchings8ceee662008-04-27 12:55:59 +01002623/* Extract non-volatile configuration */
2624static int falcon_probe_nvconfig(struct efx_nic *efx)
2625{
2626 struct falcon_nvconfig *nvconfig;
Ben Hutchings8c8661e2008-09-01 12:49:02 +01002627 int board_rev;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002628 int rc;
2629
Ben Hutchings8ceee662008-04-27 12:55:59 +01002630 nvconfig = kmalloc(sizeof(*nvconfig), GFP_KERNEL);
Ben Hutchings4a5b5042008-09-01 12:47:16 +01002631 if (!nvconfig)
2632 return -ENOMEM;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002633
Ben Hutchings8c8661e2008-09-01 12:49:02 +01002634 rc = falcon_read_nvram(efx, nvconfig);
2635 if (rc == -EINVAL) {
2636 EFX_ERR(efx, "NVRAM is invalid therefore using defaults\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01002637 efx->phy_type = PHY_TYPE_NONE;
2638 efx->mii.phy_id = PHY_ADDR_INVALID;
2639 board_rev = 0;
Ben Hutchings8c8661e2008-09-01 12:49:02 +01002640 rc = 0;
2641 } else if (rc) {
2642 goto fail1;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002643 } else {
2644 struct falcon_nvconfig_board_v2 *v2 = &nvconfig->board_v2;
Ben Hutchings4a5b5042008-09-01 12:47:16 +01002645 struct falcon_nvconfig_board_v3 *v3 = &nvconfig->board_v3;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002646
2647 efx->phy_type = v2->port0_phy_type;
2648 efx->mii.phy_id = v2->port0_phy_addr;
2649 board_rev = le16_to_cpu(v2->board_revision);
Ben Hutchings4a5b5042008-09-01 12:47:16 +01002650
Ben Hutchings8c8661e2008-09-01 12:49:02 +01002651 if (le16_to_cpu(nvconfig->board_struct_ver) >= 3) {
Ben Hutchings4a5b5042008-09-01 12:47:16 +01002652 __le32 fl = v3->spi_device_type[EE_SPI_FLASH];
2653 __le32 ee = v3->spi_device_type[EE_SPI_EEPROM];
2654 rc = falcon_spi_device_init(efx, &efx->spi_flash,
2655 EE_SPI_FLASH,
2656 le32_to_cpu(fl));
2657 if (rc)
2658 goto fail2;
2659 rc = falcon_spi_device_init(efx, &efx->spi_eeprom,
2660 EE_SPI_EEPROM,
2661 le32_to_cpu(ee));
2662 if (rc)
2663 goto fail2;
2664 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01002665 }
2666
Ben Hutchings8c8661e2008-09-01 12:49:02 +01002667 /* Read the MAC addresses */
2668 memcpy(efx->mac_address, nvconfig->mac_address[0], ETH_ALEN);
2669
Ben Hutchings8ceee662008-04-27 12:55:59 +01002670 EFX_LOG(efx, "PHY is %d phy_id %d\n", efx->phy_type, efx->mii.phy_id);
2671
2672 efx_set_board_info(efx, board_rev);
2673
Ben Hutchings4a5b5042008-09-01 12:47:16 +01002674 kfree(nvconfig);
2675 return 0;
2676
2677 fail2:
2678 falcon_remove_spi_devices(efx);
2679 fail1:
Ben Hutchings8ceee662008-04-27 12:55:59 +01002680 kfree(nvconfig);
2681 return rc;
2682}
2683
2684/* Probe the NIC variant (revision, ASIC vs FPGA, function count, port
2685 * count, port speed). Set workaround and feature flags accordingly.
2686 */
2687static int falcon_probe_nic_variant(struct efx_nic *efx)
2688{
2689 efx_oword_t altera_build;
2690
2691 falcon_read(efx, &altera_build, ALTERA_BUILD_REG_KER);
2692 if (EFX_OWORD_FIELD(altera_build, VER_ALL)) {
2693 EFX_ERR(efx, "Falcon FPGA not supported\n");
2694 return -ENODEV;
2695 }
2696
Ben Hutchings55668612008-05-16 21:16:10 +01002697 switch (falcon_rev(efx)) {
Ben Hutchings8ceee662008-04-27 12:55:59 +01002698 case FALCON_REV_A0:
2699 case 0xff:
2700 EFX_ERR(efx, "Falcon rev A0 not supported\n");
2701 return -ENODEV;
2702
2703 case FALCON_REV_A1:{
2704 efx_oword_t nic_stat;
2705
2706 falcon_read(efx, &nic_stat, NIC_STAT_REG);
2707
2708 if (EFX_OWORD_FIELD(nic_stat, STRAP_PCIE) == 0) {
2709 EFX_ERR(efx, "Falcon rev A1 PCI-X not supported\n");
2710 return -ENODEV;
2711 }
2712 if (!EFX_OWORD_FIELD(nic_stat, STRAP_10G)) {
2713 EFX_ERR(efx, "1G mode not supported\n");
2714 return -ENODEV;
2715 }
2716 break;
2717 }
2718
2719 case FALCON_REV_B0:
2720 break;
2721
2722 default:
Ben Hutchings55668612008-05-16 21:16:10 +01002723 EFX_ERR(efx, "Unknown Falcon rev %d\n", falcon_rev(efx));
Ben Hutchings8ceee662008-04-27 12:55:59 +01002724 return -ENODEV;
2725 }
2726
2727 return 0;
2728}
2729
Ben Hutchings4a5b5042008-09-01 12:47:16 +01002730/* Probe all SPI devices on the NIC */
2731static void falcon_probe_spi_devices(struct efx_nic *efx)
2732{
2733 efx_oword_t nic_stat, gpio_ctl, ee_vpd_cfg;
Ben Hutchings2f7f5732008-12-12 21:34:25 -08002734 int boot_dev;
Ben Hutchings4a5b5042008-09-01 12:47:16 +01002735
2736 falcon_read(efx, &gpio_ctl, GPIO_CTL_REG_KER);
2737 falcon_read(efx, &nic_stat, NIC_STAT_REG);
2738 falcon_read(efx, &ee_vpd_cfg, EE_VPD_CFG_REG_KER);
2739
Ben Hutchings2f7f5732008-12-12 21:34:25 -08002740 if (EFX_OWORD_FIELD(gpio_ctl, BOOTED_USING_NVDEVICE)) {
2741 boot_dev = (EFX_OWORD_FIELD(nic_stat, SF_PRST) ?
2742 EE_SPI_FLASH : EE_SPI_EEPROM);
2743 EFX_LOG(efx, "Booted from %s\n",
2744 boot_dev == EE_SPI_FLASH ? "flash" : "EEPROM");
2745 } else {
2746 /* Disable VPD and set clock dividers to safe
2747 * values for initial programming. */
2748 boot_dev = -1;
2749 EFX_LOG(efx, "Booted from internal ASIC settings;"
2750 " setting SPI config\n");
2751 EFX_POPULATE_OWORD_3(ee_vpd_cfg, EE_VPD_EN, 0,
2752 /* 125 MHz / 7 ~= 20 MHz */
2753 EE_SF_CLOCK_DIV, 7,
2754 /* 125 MHz / 63 ~= 2 MHz */
2755 EE_EE_CLOCK_DIV, 63);
2756 falcon_write(efx, &ee_vpd_cfg, EE_VPD_CFG_REG_KER);
Ben Hutchings4a5b5042008-09-01 12:47:16 +01002757 }
2758
Ben Hutchings2f7f5732008-12-12 21:34:25 -08002759 if (boot_dev == EE_SPI_FLASH)
2760 falcon_spi_device_init(efx, &efx->spi_flash, EE_SPI_FLASH,
2761 default_flash_type);
2762 if (boot_dev == EE_SPI_EEPROM)
2763 falcon_spi_device_init(efx, &efx->spi_eeprom, EE_SPI_EEPROM,
2764 large_eeprom_type);
Ben Hutchings4a5b5042008-09-01 12:47:16 +01002765}
2766
Ben Hutchings8ceee662008-04-27 12:55:59 +01002767int falcon_probe_nic(struct efx_nic *efx)
2768{
2769 struct falcon_nic_data *nic_data;
2770 int rc;
2771
Ben Hutchings8ceee662008-04-27 12:55:59 +01002772 /* Allocate storage for hardware specific data */
2773 nic_data = kzalloc(sizeof(*nic_data), GFP_KERNEL);
Ben Hutchings88c59422008-09-03 15:07:50 +01002774 if (!nic_data)
2775 return -ENOMEM;
Ben Hutchings5daab962008-05-16 21:19:43 +01002776 efx->nic_data = nic_data;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002777
2778 /* Determine number of ports etc. */
2779 rc = falcon_probe_nic_variant(efx);
2780 if (rc)
2781 goto fail1;
2782
2783 /* Probe secondary function if expected */
2784 if (FALCON_IS_DUAL_FUNC(efx)) {
2785 struct pci_dev *dev = pci_dev_get(efx->pci_dev);
2786
2787 while ((dev = pci_get_device(EFX_VENDID_SFC, FALCON_A_S_DEVID,
2788 dev))) {
2789 if (dev->bus == efx->pci_dev->bus &&
2790 dev->devfn == efx->pci_dev->devfn + 1) {
2791 nic_data->pci_dev2 = dev;
2792 break;
2793 }
2794 }
2795 if (!nic_data->pci_dev2) {
2796 EFX_ERR(efx, "failed to find secondary function\n");
2797 rc = -ENODEV;
2798 goto fail2;
2799 }
2800 }
2801
2802 /* Now we can reset the NIC */
2803 rc = falcon_reset_hw(efx, RESET_TYPE_ALL);
2804 if (rc) {
2805 EFX_ERR(efx, "failed to reset NIC\n");
2806 goto fail3;
2807 }
2808
2809 /* Allocate memory for INT_KER */
2810 rc = falcon_alloc_buffer(efx, &efx->irq_status, sizeof(efx_oword_t));
2811 if (rc)
2812 goto fail4;
2813 BUG_ON(efx->irq_status.dma_addr & 0x0f);
2814
2815 EFX_LOG(efx, "INT_KER at %llx (virt %p phys %lx)\n",
2816 (unsigned long long)efx->irq_status.dma_addr,
2817 efx->irq_status.addr, virt_to_phys(efx->irq_status.addr));
2818
Ben Hutchings4a5b5042008-09-01 12:47:16 +01002819 falcon_probe_spi_devices(efx);
2820
Ben Hutchings8ceee662008-04-27 12:55:59 +01002821 /* Read in the non-volatile configuration */
2822 rc = falcon_probe_nvconfig(efx);
2823 if (rc)
2824 goto fail5;
2825
Ben Hutchings37b5a602008-05-30 22:27:04 +01002826 /* Initialise I2C adapter */
2827 efx->i2c_adap.owner = THIS_MODULE;
Ben Hutchings37b5a602008-05-30 22:27:04 +01002828 nic_data->i2c_data = falcon_i2c_bit_operations;
2829 nic_data->i2c_data.data = efx;
2830 efx->i2c_adap.algo_data = &nic_data->i2c_data;
2831 efx->i2c_adap.dev.parent = &efx->pci_dev->dev;
Ben Hutchings9dadae62008-07-18 18:59:12 +01002832 strlcpy(efx->i2c_adap.name, "SFC4000 GPIO", sizeof(efx->i2c_adap.name));
Ben Hutchings37b5a602008-05-30 22:27:04 +01002833 rc = i2c_bit_add_bus(&efx->i2c_adap);
2834 if (rc)
2835 goto fail5;
2836
Ben Hutchings8ceee662008-04-27 12:55:59 +01002837 return 0;
2838
2839 fail5:
Ben Hutchings4a5b5042008-09-01 12:47:16 +01002840 falcon_remove_spi_devices(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002841 falcon_free_buffer(efx, &efx->irq_status);
2842 fail4:
Ben Hutchings8ceee662008-04-27 12:55:59 +01002843 fail3:
2844 if (nic_data->pci_dev2) {
2845 pci_dev_put(nic_data->pci_dev2);
2846 nic_data->pci_dev2 = NULL;
2847 }
2848 fail2:
Ben Hutchings8ceee662008-04-27 12:55:59 +01002849 fail1:
2850 kfree(efx->nic_data);
2851 return rc;
2852}
2853
2854/* This call performs hardware-specific global initialisation, such as
2855 * defining the descriptor cache sizes and number of RSS channels.
2856 * It does not set up any buffers, descriptor rings or event queues.
2857 */
2858int falcon_init_nic(struct efx_nic *efx)
2859{
Ben Hutchings8ceee662008-04-27 12:55:59 +01002860 efx_oword_t temp;
2861 unsigned thresh;
2862 int rc;
2863
Ben Hutchings8ceee662008-04-27 12:55:59 +01002864 /* Use on-chip SRAM */
2865 falcon_read(efx, &temp, NIC_STAT_REG);
2866 EFX_SET_OWORD_FIELD(temp, ONCHIP_SRAM, 1);
2867 falcon_write(efx, &temp, NIC_STAT_REG);
2868
2869 /* Set buffer table mode */
2870 EFX_POPULATE_OWORD_1(temp, BUF_TBL_MODE, BUF_TBL_MODE_FULL);
2871 falcon_write(efx, &temp, BUF_TBL_CFG_REG_KER);
2872
2873 rc = falcon_reset_sram(efx);
2874 if (rc)
2875 return rc;
2876
2877 /* Set positions of descriptor caches in SRAM. */
2878 EFX_POPULATE_OWORD_1(temp, SRM_TX_DC_BASE_ADR, TX_DC_BASE / 8);
2879 falcon_write(efx, &temp, SRM_TX_DC_CFG_REG_KER);
2880 EFX_POPULATE_OWORD_1(temp, SRM_RX_DC_BASE_ADR, RX_DC_BASE / 8);
2881 falcon_write(efx, &temp, SRM_RX_DC_CFG_REG_KER);
2882
2883 /* Set TX descriptor cache size. */
2884 BUILD_BUG_ON(TX_DC_ENTRIES != (16 << TX_DC_ENTRIES_ORDER));
2885 EFX_POPULATE_OWORD_1(temp, TX_DC_SIZE, TX_DC_ENTRIES_ORDER);
2886 falcon_write(efx, &temp, TX_DC_CFG_REG_KER);
2887
2888 /* Set RX descriptor cache size. Set low watermark to size-8, as
2889 * this allows most efficient prefetching.
2890 */
2891 BUILD_BUG_ON(RX_DC_ENTRIES != (16 << RX_DC_ENTRIES_ORDER));
2892 EFX_POPULATE_OWORD_1(temp, RX_DC_SIZE, RX_DC_ENTRIES_ORDER);
2893 falcon_write(efx, &temp, RX_DC_CFG_REG_KER);
2894 EFX_POPULATE_OWORD_1(temp, RX_DC_PF_LWM, RX_DC_ENTRIES - 8);
2895 falcon_write(efx, &temp, RX_DC_PF_WM_REG_KER);
2896
2897 /* Clear the parity enables on the TX data fifos as
2898 * they produce false parity errors because of timing issues
2899 */
2900 if (EFX_WORKAROUND_5129(efx)) {
2901 falcon_read(efx, &temp, SPARE_REG_KER);
2902 EFX_SET_OWORD_FIELD(temp, MEM_PERR_EN_TX_DATA, 0);
2903 falcon_write(efx, &temp, SPARE_REG_KER);
2904 }
2905
2906 /* Enable all the genuinely fatal interrupts. (They are still
2907 * masked by the overall interrupt mask, controlled by
2908 * falcon_interrupts()).
2909 *
2910 * Note: All other fatal interrupts are enabled
2911 */
2912 EFX_POPULATE_OWORD_3(temp,
2913 ILL_ADR_INT_KER_EN, 1,
2914 RBUF_OWN_INT_KER_EN, 1,
2915 TBUF_OWN_INT_KER_EN, 1);
2916 EFX_INVERT_OWORD(temp);
2917 falcon_write(efx, &temp, FATAL_INTR_REG_KER);
2918
Ben Hutchings8ceee662008-04-27 12:55:59 +01002919 if (EFX_WORKAROUND_7244(efx)) {
Ben Hutchings955f0a72008-09-01 12:47:52 +01002920 falcon_read(efx, &temp, RX_FILTER_CTL_REG);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002921 EFX_SET_OWORD_FIELD(temp, UDP_FULL_SRCH_LIMIT, 8);
2922 EFX_SET_OWORD_FIELD(temp, UDP_WILD_SRCH_LIMIT, 8);
2923 EFX_SET_OWORD_FIELD(temp, TCP_FULL_SRCH_LIMIT, 8);
2924 EFX_SET_OWORD_FIELD(temp, TCP_WILD_SRCH_LIMIT, 8);
Ben Hutchings955f0a72008-09-01 12:47:52 +01002925 falcon_write(efx, &temp, RX_FILTER_CTL_REG);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002926 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01002927
2928 falcon_setup_rss_indir_table(efx);
2929
2930 /* Setup RX. Wait for descriptor is broken and must
2931 * be disabled. RXDP recovery shouldn't be needed, but is.
2932 */
2933 falcon_read(efx, &temp, RX_SELF_RST_REG_KER);
2934 EFX_SET_OWORD_FIELD(temp, RX_NODESC_WAIT_DIS, 1);
2935 EFX_SET_OWORD_FIELD(temp, RX_RECOVERY_EN, 1);
2936 if (EFX_WORKAROUND_5583(efx))
2937 EFX_SET_OWORD_FIELD(temp, RX_ISCSI_DIS, 1);
2938 falcon_write(efx, &temp, RX_SELF_RST_REG_KER);
2939
2940 /* Disable the ugly timer-based TX DMA backoff and allow TX DMA to be
2941 * controlled by the RX FIFO fill level. Set arbitration to one pkt/Q.
2942 */
2943 falcon_read(efx, &temp, TX_CFG2_REG_KER);
2944 EFX_SET_OWORD_FIELD(temp, TX_RX_SPACER, 0xfe);
2945 EFX_SET_OWORD_FIELD(temp, TX_RX_SPACER_EN, 1);
2946 EFX_SET_OWORD_FIELD(temp, TX_ONE_PKT_PER_Q, 1);
2947 EFX_SET_OWORD_FIELD(temp, TX_CSR_PUSH_EN, 0);
2948 EFX_SET_OWORD_FIELD(temp, TX_DIS_NON_IP_EV, 1);
2949 /* Enable SW_EV to inherit in char driver - assume harmless here */
2950 EFX_SET_OWORD_FIELD(temp, TX_SW_EV_EN, 1);
2951 /* Prefetch threshold 2 => fetch when descriptor cache half empty */
2952 EFX_SET_OWORD_FIELD(temp, TX_PREF_THRESHOLD, 2);
2953 /* Squash TX of packets of 16 bytes or less */
Ben Hutchings55668612008-05-16 21:16:10 +01002954 if (falcon_rev(efx) >= FALCON_REV_B0 && EFX_WORKAROUND_9141(efx))
Ben Hutchings8ceee662008-04-27 12:55:59 +01002955 EFX_SET_OWORD_FIELD(temp, TX_FLUSH_MIN_LEN_EN_B0, 1);
2956 falcon_write(efx, &temp, TX_CFG2_REG_KER);
2957
2958 /* Do not enable TX_NO_EOP_DISC_EN, since it limits packets to 16
2959 * descriptors (which is bad).
2960 */
2961 falcon_read(efx, &temp, TX_CFG_REG_KER);
2962 EFX_SET_OWORD_FIELD(temp, TX_NO_EOP_DISC_EN, 0);
2963 falcon_write(efx, &temp, TX_CFG_REG_KER);
2964
2965 /* RX config */
2966 falcon_read(efx, &temp, RX_CFG_REG_KER);
2967 EFX_SET_OWORD_FIELD_VER(efx, temp, RX_DESC_PUSH_EN, 0);
2968 if (EFX_WORKAROUND_7575(efx))
2969 EFX_SET_OWORD_FIELD_VER(efx, temp, RX_USR_BUF_SIZE,
2970 (3 * 4096) / 32);
Ben Hutchings55668612008-05-16 21:16:10 +01002971 if (falcon_rev(efx) >= FALCON_REV_B0)
Ben Hutchings8ceee662008-04-27 12:55:59 +01002972 EFX_SET_OWORD_FIELD(temp, RX_INGR_EN_B0, 1);
2973
2974 /* RX FIFO flow control thresholds */
2975 thresh = ((rx_xon_thresh_bytes >= 0) ?
2976 rx_xon_thresh_bytes : efx->type->rx_xon_thresh);
2977 EFX_SET_OWORD_FIELD_VER(efx, temp, RX_XON_MAC_TH, thresh / 256);
2978 thresh = ((rx_xoff_thresh_bytes >= 0) ?
2979 rx_xoff_thresh_bytes : efx->type->rx_xoff_thresh);
2980 EFX_SET_OWORD_FIELD_VER(efx, temp, RX_XOFF_MAC_TH, thresh / 256);
2981 /* RX control FIFO thresholds [32 entries] */
Ben Hutchingsc84a6f12008-09-01 12:46:21 +01002982 EFX_SET_OWORD_FIELD_VER(efx, temp, RX_XON_TX_TH, 20);
2983 EFX_SET_OWORD_FIELD_VER(efx, temp, RX_XOFF_TX_TH, 25);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002984 falcon_write(efx, &temp, RX_CFG_REG_KER);
2985
2986 /* Set destination of both TX and RX Flush events */
Ben Hutchings55668612008-05-16 21:16:10 +01002987 if (falcon_rev(efx) >= FALCON_REV_B0) {
Ben Hutchings8ceee662008-04-27 12:55:59 +01002988 EFX_POPULATE_OWORD_1(temp, FLS_EVQ_ID, 0);
2989 falcon_write(efx, &temp, DP_CTRL_REG);
2990 }
2991
2992 return 0;
2993}
2994
2995void falcon_remove_nic(struct efx_nic *efx)
2996{
2997 struct falcon_nic_data *nic_data = efx->nic_data;
Ben Hutchings37b5a602008-05-30 22:27:04 +01002998 int rc;
2999
3000 rc = i2c_del_adapter(&efx->i2c_adap);
3001 BUG_ON(rc);
Ben Hutchings8ceee662008-04-27 12:55:59 +01003002
Ben Hutchings4a5b5042008-09-01 12:47:16 +01003003 falcon_remove_spi_devices(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01003004 falcon_free_buffer(efx, &efx->irq_status);
3005
Ben Hutchings91ad7572008-05-16 21:14:27 +01003006 falcon_reset_hw(efx, RESET_TYPE_ALL);
Ben Hutchings8ceee662008-04-27 12:55:59 +01003007
3008 /* Release the second function after the reset */
3009 if (nic_data->pci_dev2) {
3010 pci_dev_put(nic_data->pci_dev2);
3011 nic_data->pci_dev2 = NULL;
3012 }
3013
3014 /* Tear down the private nic state */
3015 kfree(efx->nic_data);
3016 efx->nic_data = NULL;
3017}
3018
3019void falcon_update_nic_stats(struct efx_nic *efx)
3020{
3021 efx_oword_t cnt;
3022
3023 falcon_read(efx, &cnt, RX_NODESC_DROP_REG_KER);
3024 efx->n_rx_nodesc_drop_cnt += EFX_OWORD_FIELD(cnt, RX_NODESC_DROP_CNT);
3025}
3026
3027/**************************************************************************
3028 *
3029 * Revision-dependent attributes used by efx.c
3030 *
3031 **************************************************************************
3032 */
3033
3034struct efx_nic_type falcon_a_nic_type = {
3035 .mem_bar = 2,
3036 .mem_map_size = 0x20000,
3037 .txd_ptr_tbl_base = TX_DESC_PTR_TBL_KER_A1,
3038 .rxd_ptr_tbl_base = RX_DESC_PTR_TBL_KER_A1,
3039 .buf_tbl_base = BUF_TBL_KER_A1,
3040 .evq_ptr_tbl_base = EVQ_PTR_TBL_KER_A1,
3041 .evq_rptr_tbl_base = EVQ_RPTR_REG_KER_A1,
3042 .txd_ring_mask = FALCON_TXD_RING_MASK,
3043 .rxd_ring_mask = FALCON_RXD_RING_MASK,
3044 .evq_size = FALCON_EVQ_SIZE,
3045 .max_dma_mask = FALCON_DMA_MASK,
3046 .tx_dma_mask = FALCON_TX_DMA_MASK,
3047 .bug5391_mask = 0xf,
3048 .rx_xoff_thresh = 2048,
3049 .rx_xon_thresh = 512,
3050 .rx_buffer_padding = 0x24,
3051 .max_interrupt_mode = EFX_INT_MODE_MSI,
3052 .phys_addr_channels = 4,
3053};
3054
3055struct efx_nic_type falcon_b_nic_type = {
3056 .mem_bar = 2,
3057 /* Map everything up to and including the RSS indirection
3058 * table. Don't map MSI-X table, MSI-X PBA since Linux
3059 * requires that they not be mapped. */
3060 .mem_map_size = RX_RSS_INDIR_TBL_B0 + 0x800,
3061 .txd_ptr_tbl_base = TX_DESC_PTR_TBL_KER_B0,
3062 .rxd_ptr_tbl_base = RX_DESC_PTR_TBL_KER_B0,
3063 .buf_tbl_base = BUF_TBL_KER_B0,
3064 .evq_ptr_tbl_base = EVQ_PTR_TBL_KER_B0,
3065 .evq_rptr_tbl_base = EVQ_RPTR_REG_KER_B0,
3066 .txd_ring_mask = FALCON_TXD_RING_MASK,
3067 .rxd_ring_mask = FALCON_RXD_RING_MASK,
3068 .evq_size = FALCON_EVQ_SIZE,
3069 .max_dma_mask = FALCON_DMA_MASK,
3070 .tx_dma_mask = FALCON_TX_DMA_MASK,
3071 .bug5391_mask = 0,
3072 .rx_xoff_thresh = 54272, /* ~80Kb - 3*max MTU */
3073 .rx_xon_thresh = 27648, /* ~3*max MTU */
3074 .rx_buffer_padding = 0,
3075 .max_interrupt_mode = EFX_INT_MODE_MSIX,
3076 .phys_addr_channels = 32, /* Hardware limit is 64, but the legacy
3077 * interrupt handler only supports 32
3078 * channels */
3079};
3080