staging: et131x: Remove unnecessary NULL pointer assignments
[deliverable/linux.git] / drivers / staging / et131x / et131x.c
CommitLineData
d2796743
ME
1/*
2 * Agere Systems Inc.
3 * 10/100/1000 Base-T Ethernet Driver for the ET1301 and ET131x series MACs
4 *
5 * Copyright © 2005 Agere Systems Inc.
6 * All rights reserved.
7 * http://www.agere.com
8 *
9 * Copyright (c) 2011 Mark Einon <mark.einon@gmail.com>
10 *
11 *------------------------------------------------------------------------------
12 *
13 * SOFTWARE LICENSE
14 *
15 * This software is provided subject to the following terms and conditions,
16 * which you should read carefully before using the software. Using this
17 * software indicates your acceptance of these terms and conditions. If you do
18 * not agree with these terms and conditions, do not use the software.
19 *
20 * Copyright © 2005 Agere Systems Inc.
21 * All rights reserved.
22 *
23 * Redistribution and use in source or binary forms, with or without
24 * modifications, are permitted provided that the following conditions are met:
25 *
26 * . Redistributions of source code must retain the above copyright notice, this
27 * list of conditions and the following Disclaimer as comments in the code as
28 * well as in the documentation and/or other materials provided with the
29 * distribution.
30 *
31 * . Redistributions in binary form must reproduce the above copyright notice,
32 * this list of conditions and the following Disclaimer in the documentation
33 * and/or other materials provided with the distribution.
34 *
35 * . Neither the name of Agere Systems Inc. nor the names of the contributors
36 * may be used to endorse or promote products derived from this software
37 * without specific prior written permission.
38 *
39 * Disclaimer
40 *
41 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
42 * INCLUDING, BUT NOT LIMITED TO, INFRINGEMENT AND THE IMPLIED WARRANTIES OF
43 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. ANY
44 * USE, MODIFICATION OR DISTRIBUTION OF THIS SOFTWARE IS SOLELY AT THE USERS OWN
45 * RISK. IN NO EVENT SHALL AGERE SYSTEMS INC. OR CONTRIBUTORS BE LIABLE FOR ANY
46 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
47 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
48 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
49 * ON ANY THEORY OF LIABILITY, INCLUDING, BUT NOT LIMITED TO, CONTRACT, STRICT
50 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
51 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
52 * DAMAGE.
53 *
54 */
55
e58b89da
TY
56#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
57
d2796743
ME
58#include <linux/pci.h>
59#include <linux/init.h>
60#include <linux/module.h>
61#include <linux/types.h>
62#include <linux/kernel.h>
63
64#include <linux/sched.h>
65#include <linux/ptrace.h>
66#include <linux/slab.h>
67#include <linux/ctype.h>
68#include <linux/string.h>
69#include <linux/timer.h>
70#include <linux/interrupt.h>
71#include <linux/in.h>
72#include <linux/delay.h>
73#include <linux/bitops.h>
74#include <linux/io.h>
d2796743
ME
75
76#include <linux/netdevice.h>
77#include <linux/etherdevice.h>
78#include <linux/skbuff.h>
79#include <linux/if_arp.h>
80#include <linux/ioport.h>
81#include <linux/crc32.h>
82#include <linux/random.h>
83#include <linux/phy.h>
84
d2796743
ME
85#include "et131x.h"
86
87MODULE_AUTHOR("Victor Soriano <vjsoriano@agere.com>");
88MODULE_AUTHOR("Mark Einon <mark.einon@gmail.com>");
89MODULE_LICENSE("Dual BSD/GPL");
397d3e60 90MODULE_DESCRIPTION("10/100/1000 Base-T Ethernet Driver for the ET1310 by Agere Systems");
d2796743 91
bd156af6
ME
92/* EEPROM defines */
93#define MAX_NUM_REGISTER_POLLS 1000
94#define MAX_NUM_WRITE_RETRIES 2
95
96/* MAC defines */
97#define COUNTER_WRAP_16_BIT 0x10000
98#define COUNTER_WRAP_12_BIT 0x1000
99
100/* PCI defines */
101#define INTERNAL_MEM_SIZE 0x400 /* 1024 of internal memory */
102#define INTERNAL_MEM_RX_OFFSET 0x1FF /* 50% Tx, 50% Rx */
103
104/* ISR defines */
105/*
106 * For interrupts, normal running is:
107 * rxdma_xfr_done, phy_interrupt, mac_stat_interrupt,
108 * watchdog_interrupt & txdma_xfer_done
109 *
110 * In both cases, when flow control is enabled for either Tx or bi-direction,
111 * we additional enable rx_fbr0_low and rx_fbr1_low, so we know when the
112 * buffer rings are running low.
113 */
114#define INT_MASK_DISABLE 0xffffffff
115
116/* NOTE: Masking out MAC_STAT Interrupt for now...
117 * #define INT_MASK_ENABLE 0xfff6bf17
118 * #define INT_MASK_ENABLE_NO_FLOW 0xfff6bfd7
119 */
120#define INT_MASK_ENABLE 0xfffebf17
121#define INT_MASK_ENABLE_NO_FLOW 0xfffebfd7
122
1c1c1b5f
ME
123/* General defines */
124/* Packet and header sizes */
125#define NIC_MIN_PACKET_SIZE 60
126
127/* Multicast list size */
128#define NIC_MAX_MCAST_LIST 128
129
130/* Supported Filters */
131#define ET131X_PACKET_TYPE_DIRECTED 0x0001
132#define ET131X_PACKET_TYPE_MULTICAST 0x0002
133#define ET131X_PACKET_TYPE_BROADCAST 0x0004
134#define ET131X_PACKET_TYPE_PROMISCUOUS 0x0008
135#define ET131X_PACKET_TYPE_ALL_MULTICAST 0x0010
136
137/* Tx Timeout */
138#define ET131X_TX_TIMEOUT (1 * HZ)
139#define NIC_SEND_HANG_THRESHOLD 0
140
141/* MP_TCB flags */
142#define fMP_DEST_MULTI 0x00000001
143#define fMP_DEST_BROAD 0x00000002
144
145/* MP_ADAPTER flags */
146#define fMP_ADAPTER_RECV_LOOKASIDE 0x00000004
147#define fMP_ADAPTER_INTERRUPT_IN_USE 0x00000008
1c1c1b5f
ME
148
149/* MP_SHARED flags */
1c1c1b5f
ME
150#define fMP_ADAPTER_LOWER_POWER 0x00200000
151
152#define fMP_ADAPTER_NON_RECOVER_ERROR 0x00800000
1c1c1b5f 153#define fMP_ADAPTER_HARDWARE_ERROR 0x04000000
1c1c1b5f
ME
154
155#define fMP_ADAPTER_FAIL_SEND_MASK 0x3ff00000
1c1c1b5f
ME
156
157/* Some offsets in PCI config space that are actually used. */
1c1c1b5f
ME
158#define ET1310_PCI_MAC_ADDRESS 0xA4
159#define ET1310_PCI_EEPROM_STATUS 0xB2
160#define ET1310_PCI_ACK_NACK 0xC0
161#define ET1310_PCI_REPLAY 0xC2
162#define ET1310_PCI_L0L1LATENCY 0xCF
163
26d19bf6 164/* PCI Product IDs */
1c1c1b5f
ME
165#define ET131X_PCI_DEVICE_ID_GIG 0xED00 /* ET1310 1000 Base-T 8 */
166#define ET131X_PCI_DEVICE_ID_FAST 0xED01 /* ET1310 100 Base-T */
167
168/* Define order of magnitude converter */
169#define NANO_IN_A_MICRO 1000
170
171#define PARM_RX_NUM_BUFS_DEF 4
172#define PARM_RX_TIME_INT_DEF 10
173#define PARM_RX_MEM_END_DEF 0x2bc
174#define PARM_TX_TIME_INT_DEF 40
175#define PARM_TX_NUM_BUFS_DEF 4
176#define PARM_DMA_CACHE_DEF 0
177
562550b0 178/* RX defines */
788ca84a
ME
179#define FBR_CHUNKS 32
180#define MAX_DESC_PER_RING_RX 1024
562550b0
ME
181
182/* number of RFDs - default and min */
562550b0 183#define RFD_LOW_WATER_MARK 40
562550b0 184#define NIC_DEFAULT_NUM_RFD 1024
6abafc16 185#define NUM_FBRS 2
562550b0 186
6abafc16 187#define NIC_MIN_NUM_RFD 64
562550b0
ME
188#define NUM_PACKETS_HANDLED 256
189
562550b0
ME
190#define ALCATEL_MULTICAST_PKT 0x01000000
191#define ALCATEL_BROADCAST_PKT 0x02000000
192
193/* typedefs for Free Buffer Descriptors */
194struct fbr_desc {
195 u32 addr_lo;
196 u32 addr_hi;
197 u32 word2; /* Bits 10-31 reserved, 0-9 descriptor */
198};
199
200/* Packet Status Ring Descriptors
201 *
202 * Word 0:
203 *
204 * top 16 bits are from the Alcatel Status Word as enumerated in
205 * PE-MCXMAC Data Sheet IPD DS54 0210-1 (also IPD-DS80 0205-2)
206 *
207 * 0: hp hash pass
208 * 1: ipa IP checksum assist
209 * 2: ipp IP checksum pass
210 * 3: tcpa TCP checksum assist
211 * 4: tcpp TCP checksum pass
212 * 5: wol WOL Event
213 * 6: rxmac_error RXMAC Error Indicator
214 * 7: drop Drop packet
215 * 8: ft Frame Truncated
216 * 9: jp Jumbo Packet
217 * 10: vp VLAN Packet
218 * 11-15: unused
219 * 16: asw_prev_pkt_dropped e.g. IFG too small on previous
220 * 17: asw_RX_DV_event short receive event detected
221 * 18: asw_false_carrier_event bad carrier since last good packet
222 * 19: asw_code_err one or more nibbles signalled as errors
223 * 20: asw_CRC_err CRC error
224 * 21: asw_len_chk_err frame length field incorrect
225 * 22: asw_too_long frame length > 1518 bytes
226 * 23: asw_OK valid CRC + no code error
227 * 24: asw_multicast has a multicast address
228 * 25: asw_broadcast has a broadcast address
229 * 26: asw_dribble_nibble spurious bits after EOP
230 * 27: asw_control_frame is a control frame
231 * 28: asw_pause_frame is a pause frame
232 * 29: asw_unsupported_op unsupported OP code
233 * 30: asw_VLAN_tag VLAN tag detected
234 * 31: asw_long_evt Rx long event
235 *
236 * Word 1:
237 * 0-15: length length in bytes
238 * 16-25: bi Buffer Index
239 * 26-27: ri Ring Index
240 * 28-31: reserved
241 */
242
243struct pkt_stat_desc {
244 u32 word0;
245 u32 word1;
246};
247
248/* Typedefs for the RX DMA status word */
249
250/*
251 * rx status word 0 holds part of the status bits of the Rx DMA engine
252 * that get copied out to memory by the ET-1310. Word 0 is a 32 bit word
253 * which contains the Free Buffer ring 0 and 1 available offset.
254 *
255 * bit 0-9 FBR1 offset
256 * bit 10 Wrap flag for FBR1
257 * bit 16-25 FBR0 offset
258 * bit 26 Wrap flag for FBR0
259 */
260
261/*
262 * RXSTAT_WORD1_t structure holds part of the status bits of the Rx DMA engine
263 * that get copied out to memory by the ET-1310. Word 3 is a 32 bit word
264 * which contains the Packet Status Ring available offset.
265 *
266 * bit 0-15 reserved
267 * bit 16-27 PSRoffset
268 * bit 28 PSRwrap
269 * bit 29-31 unused
270 */
271
272/*
273 * struct rx_status_block is a structure representing the status of the Rx
274 * DMA engine it sits in free memory, and is pointed to by 0x101c / 0x1020
275 */
276struct rx_status_block {
277 u32 word0;
278 u32 word1;
279};
280
281/*
6abafc16
ME
282 * Structure for look-up table holding free buffer ring pointers, addresses
283 * and state.
562550b0
ME
284 */
285struct fbr_lookup {
6abafc16
ME
286 void *virt[MAX_DESC_PER_RING_RX];
287 void *buffer1[MAX_DESC_PER_RING_RX];
288 void *buffer2[MAX_DESC_PER_RING_RX];
289 u32 bus_high[MAX_DESC_PER_RING_RX];
290 u32 bus_low[MAX_DESC_PER_RING_RX];
291 void *ring_virtaddr;
292 dma_addr_t ring_physaddr;
293 void *mem_virtaddrs[MAX_DESC_PER_RING_RX / FBR_CHUNKS];
294 dma_addr_t mem_physaddrs[MAX_DESC_PER_RING_RX / FBR_CHUNKS];
983e4b35 295 dma_addr_t offset;
6abafc16
ME
296 u32 local_full;
297 u32 num_entries;
983e4b35 298 dma_addr_t buffsize;
562550b0
ME
299};
300
301/*
302 * struct rx_ring is the sructure representing the adaptor's local
303 * reference(s) to the rings
e592a9b0
ME
304 *
305 ******************************************************************************
306 * IMPORTANT NOTE :- fbr_lookup *fbr[NUM_FBRS] uses index 0 to refer to FBR1
307 * and index 1 to refer to FRB0
308 ******************************************************************************
562550b0
ME
309 */
310struct rx_ring {
6abafc16 311 struct fbr_lookup *fbr[NUM_FBRS];
562550b0
ME
312 void *ps_ring_virtaddr;
313 dma_addr_t ps_ring_physaddr;
314 u32 local_psr_full;
315 u32 psr_num_entries;
316
317 struct rx_status_block *rx_status_block;
318 dma_addr_t rx_status_bus;
319
562550b0
ME
320 /* RECV */
321 struct list_head recv_list;
322 u32 num_ready_recv;
323
324 u32 num_rfd;
325
326 bool unfinished_receives;
327
562550b0
ME
328 /* lookaside lists */
329 struct kmem_cache *recv_lookaside;
330};
331
17ec9ff3
ME
332/* TX defines */
333/*
334 * word 2 of the control bits in the Tx Descriptor ring for the ET-1310
335 *
336 * 0-15: length of packet
337 * 16-27: VLAN tag
338 * 28: VLAN CFI
339 * 29-31: VLAN priority
340 *
341 * word 3 of the control bits in the Tx Descriptor ring for the ET-1310
342 *
343 * 0: last packet in the sequence
344 * 1: first packet in the sequence
345 * 2: interrupt the processor when this pkt sent
346 * 3: Control word - no packet data
347 * 4: Issue half-duplex backpressure : XON/XOFF
348 * 5: send pause frame
349 * 6: Tx frame has error
350 * 7: append CRC
351 * 8: MAC override
352 * 9: pad packet
353 * 10: Packet is a Huge packet
354 * 11: append VLAN tag
355 * 12: IP checksum assist
356 * 13: TCP checksum assist
357 * 14: UDP checksum assist
358 */
359
360/* struct tx_desc represents each descriptor on the ring */
361struct tx_desc {
362 u32 addr_hi;
363 u32 addr_lo;
364 u32 len_vlan; /* control words how to xmit the */
365 u32 flags; /* data (detailed above) */
366};
367
368/*
369 * The status of the Tx DMA engine it sits in free memory, and is pointed to
370 * by 0x101c / 0x1020. This is a DMA10 type
371 */
372
373/* TCB (Transmit Control Block: Host Side) */
374struct tcb {
375 struct tcb *next; /* Next entry in ring */
376 u32 flags; /* Our flags for the packet */
377 u32 count; /* Used to spot stuck/lost packets */
378 u32 stale; /* Used to spot stuck/lost packets */
379 struct sk_buff *skb; /* Network skb we are tied to */
380 u32 index; /* Ring indexes */
381 u32 index_start;
382};
383
384/* Structure representing our local reference(s) to the ring */
385struct tx_ring {
386 /* TCB (Transmit Control Block) memory and lists */
387 struct tcb *tcb_ring;
388
389 /* List of TCBs that are ready to be used */
390 struct tcb *tcb_qhead;
391 struct tcb *tcb_qtail;
392
393 /* list of TCBs that are currently being sent. NOTE that access to all
394 * three of these (including used) are controlled via the
395 * TCBSendQLock. This lock should be secured prior to incementing /
396 * decrementing used, or any queue manipulation on send_head /
397 * tail
398 */
399 struct tcb *send_head;
400 struct tcb *send_tail;
401 int used;
402
403 /* The actual descriptor ring */
404 struct tx_desc *tx_desc_ring;
405 dma_addr_t tx_desc_ring_pa;
406
407 /* send_idx indicates where we last wrote to in the descriptor ring. */
408 u32 send_idx;
409
410 /* The location of the write-back status block */
411 u32 *tx_status;
412 dma_addr_t tx_status_pa;
413
414 /* Packets since the last IRQ: used for interrupt coalescing */
415 int since_irq;
416};
417
fd0651a6
ME
418/*
419 * Do not change these values: if changed, then change also in respective
420 * TXdma and Rxdma engines
421 */
422#define NUM_DESC_PER_RING_TX 512 /* TX Do not change these values */
423#define NUM_TCB 64
424
425/*
426 * These values are all superseded by registry entries to facilitate tuning.
427 * Once the desired performance has been achieved, the optimal registry values
428 * should be re-populated to these #defines:
429 */
fd0651a6
ME
430#define TX_ERROR_PERIOD 1000
431
432#define LO_MARK_PERCENT_FOR_PSR 15
433#define LO_MARK_PERCENT_FOR_RX 15
434
435/* RFD (Receive Frame Descriptor) */
436struct rfd {
437 struct list_head list_node;
438 struct sk_buff *skb;
439 u32 len; /* total size of receive frame */
440 u16 bufferindex;
441 u8 ringindex;
442};
443
444/* Flow Control */
445#define FLOW_BOTH 0
446#define FLOW_TXONLY 1
447#define FLOW_RXONLY 2
448#define FLOW_NONE 3
449
450/* Struct to define some device statistics */
451struct ce_stats {
452 /* MIB II variables
453 *
454 * NOTE: atomic_t types are only guaranteed to store 24-bits; if we
455 * MUST have 32, then we'll need another way to perform atomic
456 * operations
457 */
458 u32 unicast_pkts_rcvd;
459 atomic_t unicast_pkts_xmtd;
460 u32 multicast_pkts_rcvd;
461 atomic_t multicast_pkts_xmtd;
462 u32 broadcast_pkts_rcvd;
463 atomic_t broadcast_pkts_xmtd;
464 u32 rcvd_pkts_dropped;
465
466 /* Tx Statistics. */
467 u32 tx_underflows;
468
469 u32 tx_collisions;
470 u32 tx_excessive_collisions;
471 u32 tx_first_collisions;
472 u32 tx_late_collisions;
473 u32 tx_max_pkt_errs;
474 u32 tx_deferred;
475
476 /* Rx Statistics. */
477 u32 rx_overflows;
478
479 u32 rx_length_errs;
480 u32 rx_align_errs;
481 u32 rx_crc_errs;
482 u32 rx_code_violations;
483 u32 rx_other_errs;
484
485 u32 synchronous_iterations;
486 u32 interrupt_status;
487};
488
489/* The private adapter structure */
490struct et131x_adapter {
491 struct net_device *netdev;
492 struct pci_dev *pdev;
493 struct mii_bus *mii_bus;
494 struct phy_device *phydev;
495 struct work_struct task;
496
497 /* Flags that indicate current state of the adapter */
498 u32 flags;
499
500 /* local link state, to determine if a state change has occurred */
501 int link;
502
503 /* Configuration */
504 u8 rom_addr[ETH_ALEN];
505 u8 addr[ETH_ALEN];
506 bool has_eeprom;
507 u8 eeprom_data[2];
508
509 /* Spinlocks */
510 spinlock_t lock;
511
512 spinlock_t tcb_send_qlock;
513 spinlock_t tcb_ready_qlock;
514 spinlock_t send_hw_lock;
515
516 spinlock_t rcv_lock;
517 spinlock_t rcv_pend_lock;
518 spinlock_t fbr_lock;
519
520 spinlock_t phy_lock;
521
522 /* Packet Filter and look ahead size */
523 u32 packet_filter;
524
525 /* multicast list */
526 u32 multicast_addr_count;
527 u8 multicast_list[NIC_MAX_MCAST_LIST][ETH_ALEN];
528
529 /* Pointer to the device's PCI register space */
530 struct address_map __iomem *regs;
531
532 /* Registry parameters */
533 u8 wanted_flow; /* Flow we want for 802.3x flow control */
534 u32 registry_jumbo_packet; /* Max supported ethernet packet size */
535
536 /* Derived from the registry: */
537 u8 flowcontrol; /* flow control validated by the far-end */
538
539 /* Minimize init-time */
540 struct timer_list error_timer;
541
542 /* variable putting the phy into coma mode when boot up with no cable
543 * plugged in after 5 seconds
544 */
545 u8 boot_coma;
546
547 /* Next two used to save power information at power down. This
548 * information will be used during power up to set up parts of Power
549 * Management in JAGCore
550 */
551 u16 pdown_speed;
552 u8 pdown_duplex;
553
554 /* Tx Memory Variables */
555 struct tx_ring tx_ring;
556
557 /* Rx Memory Variables */
558 struct rx_ring rx_ring;
559
560 /* Stats */
561 struct ce_stats stats;
562
563 struct net_device_stats net_stats;
564};
565
d2796743
ME
566static int eeprom_wait_ready(struct pci_dev *pdev, u32 *status)
567{
568 u32 reg;
569 int i;
570
571 /*
572 * 1. Check LBCIF Status Register for bits 6 & 3:2 all equal to 0 and
573 * bits 7,1:0 both equal to 1, at least once after reset.
574 * Subsequent operations need only to check that bits 1:0 are equal
575 * to 1 prior to starting a single byte read/write
576 */
577
578 for (i = 0; i < MAX_NUM_REGISTER_POLLS; i++) {
579 /* Read registers grouped in DWORD1 */
580 if (pci_read_config_dword(pdev, LBCIF_DWORD1_GROUP, &reg))
581 return -EIO;
582
583 /* I2C idle and Phy Queue Avail both true */
584 if ((reg & 0x3000) == 0x3000) {
585 if (status)
586 *status = reg;
587 return reg & 0xFF;
588 }
589 }
590 return -ETIMEDOUT;
591}
592
593
594/**
595 * eeprom_write - Write a byte to the ET1310's EEPROM
596 * @adapter: pointer to our private adapter structure
597 * @addr: the address to write
598 * @data: the value to write
599 *
600 * Returns 1 for a successful write.
601 */
602static int eeprom_write(struct et131x_adapter *adapter, u32 addr, u8 data)
603{
604 struct pci_dev *pdev = adapter->pdev;
605 int index = 0;
606 int retries;
607 int err = 0;
608 int i2c_wack = 0;
609 int writeok = 0;
610 u32 status;
611 u32 val = 0;
612
613 /*
614 * For an EEPROM, an I2C single byte write is defined as a START
615 * condition followed by the device address, EEPROM address, one byte
616 * of data and a STOP condition. The STOP condition will trigger the
617 * EEPROM's internally timed write cycle to the nonvolatile memory.
618 * All inputs are disabled during this write cycle and the EEPROM will
619 * not respond to any access until the internal write is complete.
620 */
621
622 err = eeprom_wait_ready(pdev, NULL);
623 if (err)
624 return err;
625
626 /*
627 * 2. Write to the LBCIF Control Register: bit 7=1, bit 6=1, bit 3=0,
628 * and bits 1:0 both =0. Bit 5 should be set according to the
629 * type of EEPROM being accessed (1=two byte addressing, 0=one
630 * byte addressing).
631 */
632 if (pci_write_config_byte(pdev, LBCIF_CONTROL_REGISTER,
633 LBCIF_CONTROL_LBCIF_ENABLE | LBCIF_CONTROL_I2C_WRITE))
634 return -EIO;
635
636 i2c_wack = 1;
637
638 /* Prepare EEPROM address for Step 3 */
639
640 for (retries = 0; retries < MAX_NUM_WRITE_RETRIES; retries++) {
641 /* Write the address to the LBCIF Address Register */
642 if (pci_write_config_dword(pdev, LBCIF_ADDRESS_REGISTER, addr))
643 break;
644 /*
645 * Write the data to the LBCIF Data Register (the I2C write
646 * will begin).
647 */
648 if (pci_write_config_byte(pdev, LBCIF_DATA_REGISTER, data))
649 break;
650 /*
651 * Monitor bit 1:0 of the LBCIF Status Register. When bits
652 * 1:0 are both equal to 1, the I2C write has completed and the
653 * internal write cycle of the EEPROM is about to start.
654 * (bits 1:0 = 01 is a legal state while waiting from both
655 * equal to 1, but bits 1:0 = 10 is invalid and implies that
656 * something is broken).
657 */
658 err = eeprom_wait_ready(pdev, &status);
659 if (err < 0)
660 return 0;
661
662 /*
663 * Check bit 3 of the LBCIF Status Register. If equal to 1,
664 * an error has occurred.Don't break here if we are revision
665 * 1, this is so we do a blind write for load bug.
666 */
667 if ((status & LBCIF_STATUS_GENERAL_ERROR)
668 && adapter->pdev->revision == 0)
669 break;
670
671 /*
672 * Check bit 2 of the LBCIF Status Register. If equal to 1 an
673 * ACK error has occurred on the address phase of the write.
674 * This could be due to an actual hardware failure or the
675 * EEPROM may still be in its internal write cycle from a
676 * previous write. This write operation was ignored and must be
677 *repeated later.
678 */
679 if (status & LBCIF_STATUS_ACK_ERROR) {
680 /*
681 * This could be due to an actual hardware failure
682 * or the EEPROM may still be in its internal write
683 * cycle from a previous write. This write operation
684 * was ignored and must be repeated later.
685 */
686 udelay(10);
687 continue;
688 }
689
690 writeok = 1;
691 break;
692 }
693
694 /*
695 * Set bit 6 of the LBCIF Control Register = 0.
696 */
697 udelay(10);
698
699 while (i2c_wack) {
700 if (pci_write_config_byte(pdev, LBCIF_CONTROL_REGISTER,
701 LBCIF_CONTROL_LBCIF_ENABLE))
702 writeok = 0;
703
704 /* Do read until internal ACK_ERROR goes away meaning write
705 * completed
706 */
707 do {
708 pci_write_config_dword(pdev,
709 LBCIF_ADDRESS_REGISTER,
710 addr);
711 do {
712 pci_read_config_dword(pdev,
713 LBCIF_DATA_REGISTER, &val);
714 } while ((val & 0x00010000) == 0);
715 } while (val & 0x00040000);
716
717 if ((val & 0xFF00) != 0xC000 || index == 10000)
718 break;
719 index++;
720 }
721 return writeok ? 0 : -EIO;
722}
723
724/**
725 * eeprom_read - Read a byte from the ET1310's EEPROM
726 * @adapter: pointer to our private adapter structure
727 * @addr: the address from which to read
728 * @pdata: a pointer to a byte in which to store the value of the read
729 * @eeprom_id: the ID of the EEPROM
730 * @addrmode: how the EEPROM is to be accessed
731 *
732 * Returns 1 for a successful read
733 */
734static int eeprom_read(struct et131x_adapter *adapter, u32 addr, u8 *pdata)
735{
736 struct pci_dev *pdev = adapter->pdev;
737 int err;
738 u32 status;
739
740 /*
741 * A single byte read is similar to the single byte write, with the
742 * exception of the data flow:
743 */
744
745 err = eeprom_wait_ready(pdev, NULL);
746 if (err)
747 return err;
748 /*
749 * Write to the LBCIF Control Register: bit 7=1, bit 6=0, bit 3=0,
750 * and bits 1:0 both =0. Bit 5 should be set according to the type
751 * of EEPROM being accessed (1=two byte addressing, 0=one byte
752 * addressing).
753 */
754 if (pci_write_config_byte(pdev, LBCIF_CONTROL_REGISTER,
755 LBCIF_CONTROL_LBCIF_ENABLE))
756 return -EIO;
757 /*
758 * Write the address to the LBCIF Address Register (I2C read will
759 * begin).
760 */
761 if (pci_write_config_dword(pdev, LBCIF_ADDRESS_REGISTER, addr))
762 return -EIO;
763 /*
764 * Monitor bit 0 of the LBCIF Status Register. When = 1, I2C read
765 * is complete. (if bit 1 =1 and bit 0 stays = 0, a hardware failure
766 * has occurred).
767 */
768 err = eeprom_wait_ready(pdev, &status);
769 if (err < 0)
770 return err;
771 /*
772 * Regardless of error status, read data byte from LBCIF Data
773 * Register.
774 */
775 *pdata = err;
776 /*
777 * Check bit 2 of the LBCIF Status Register. If = 1,
778 * then an error has occurred.
779 */
780 return (status & LBCIF_STATUS_ACK_ERROR) ? -EIO : 0;
781}
782
eb7a6ca6 783static int et131x_init_eeprom(struct et131x_adapter *adapter)
d2796743
ME
784{
785 struct pci_dev *pdev = adapter->pdev;
786 u8 eestatus;
787
788 /* We first need to check the EEPROM Status code located at offset
789 * 0xB2 of config space
790 */
791 pci_read_config_byte(pdev, ET1310_PCI_EEPROM_STATUS,
792 &eestatus);
793
794 /* THIS IS A WORKAROUND:
795 * I need to call this function twice to get my card in a
796 * LG M1 Express Dual running. I tried also a msleep before this
ac399bc0 797 * function, because I thought there could be some time condidions
d2796743
ME
798 * but it didn't work. Call the whole function twice also work.
799 */
800 if (pci_read_config_byte(pdev, ET1310_PCI_EEPROM_STATUS, &eestatus)) {
801 dev_err(&pdev->dev,
802 "Could not read PCI config space for EEPROM Status\n");
803 return -EIO;
804 }
805
806 /* Determine if the error(s) we care about are present. If they are
807 * present we need to fail.
808 */
809 if (eestatus & 0x4C) {
810 int write_failed = 0;
811 if (pdev->revision == 0x01) {
812 int i;
813 static const u8 eedata[4] = { 0xFE, 0x13, 0x10, 0xFF };
814
815 /* Re-write the first 4 bytes if we have an eeprom
816 * present and the revision id is 1, this fixes the
817 * corruption seen with 1310 B Silicon
818 */
819 for (i = 0; i < 3; i++)
820 if (eeprom_write(adapter, i, eedata[i]) < 0)
821 write_failed = 1;
822 }
823 if (pdev->revision != 0x01 || write_failed) {
824 dev_err(&pdev->dev,
825 "Fatal EEPROM Status Error - 0x%04x\n", eestatus);
826
827 /* This error could mean that there was an error
828 * reading the eeprom or that the eeprom doesn't exist.
829 * We will treat each case the same and not try to
830 * gather additional information that normally would
831 * come from the eeprom, like MAC Address
832 */
833 adapter->has_eeprom = 0;
834 return -EIO;
835 }
836 }
837 adapter->has_eeprom = 1;
838
839 /* Read the EEPROM for information regarding LED behavior. Refer to
840 * ET1310_phy.c, et131x_xcvr_init(), for its use.
841 */
842 eeprom_read(adapter, 0x70, &adapter->eeprom_data[0]);
843 eeprom_read(adapter, 0x71, &adapter->eeprom_data[1]);
844
845 if (adapter->eeprom_data[0] != 0xcd)
846 /* Disable all optional features */
847 adapter->eeprom_data[1] = 0x00;
848
849 return 0;
850}
851
8310c602
ME
852/**
853 * et131x_rx_dma_enable - re-start of Rx_DMA on the ET1310.
854 * @adapter: pointer to our adapter structure
855 */
eb7a6ca6 856static void et131x_rx_dma_enable(struct et131x_adapter *adapter)
8310c602
ME
857{
858 /* Setup the receive dma configuration register for normal operation */
859 u32 csr = 0x2000; /* FBR1 enable */
860
861 if (adapter->rx_ring.fbr[0]->buffsize == 4096)
862 csr |= 0x0800;
863 else if (adapter->rx_ring.fbr[0]->buffsize == 8192)
864 csr |= 0x1000;
865 else if (adapter->rx_ring.fbr[0]->buffsize == 16384)
866 csr |= 0x1800;
b5254867 867
8310c602
ME
868 csr |= 0x0400; /* FBR0 enable */
869 if (adapter->rx_ring.fbr[1]->buffsize == 256)
870 csr |= 0x0100;
871 else if (adapter->rx_ring.fbr[1]->buffsize == 512)
872 csr |= 0x0200;
873 else if (adapter->rx_ring.fbr[1]->buffsize == 1024)
874 csr |= 0x0300;
8310c602
ME
875 writel(csr, &adapter->regs->rxdma.csr);
876
877 csr = readl(&adapter->regs->rxdma.csr);
878 if ((csr & 0x00020000) != 0) {
879 udelay(5);
880 csr = readl(&adapter->regs->rxdma.csr);
881 if ((csr & 0x00020000) != 0) {
882 dev_err(&adapter->pdev->dev,
883 "RX Dma failed to exit halt state. CSR 0x%08x\n",
884 csr);
885 }
886 }
887}
888
889/**
890 * et131x_rx_dma_disable - Stop of Rx_DMA on the ET1310
891 * @adapter: pointer to our adapter structure
892 */
eb7a6ca6 893static void et131x_rx_dma_disable(struct et131x_adapter *adapter)
8310c602
ME
894{
895 u32 csr;
896 /* Setup the receive dma configuration register */
897 writel(0x00002001, &adapter->regs->rxdma.csr);
898 csr = readl(&adapter->regs->rxdma.csr);
899 if ((csr & 0x00020000) == 0) { /* Check halt status (bit 17) */
900 udelay(5);
901 csr = readl(&adapter->regs->rxdma.csr);
902 if ((csr & 0x00020000) == 0)
903 dev_err(&adapter->pdev->dev,
904 "RX Dma failed to enter halt state. CSR 0x%08x\n",
905 csr);
906 }
907}
908
909/**
910 * et131x_tx_dma_enable - re-start of Tx_DMA on the ET1310.
911 * @adapter: pointer to our adapter structure
912 *
913 * Mainly used after a return to the D0 (full-power) state from a lower state.
914 */
eb7a6ca6 915static void et131x_tx_dma_enable(struct et131x_adapter *adapter)
8310c602
ME
916{
917 /* Setup the transmit dma configuration register for normal
918 * operation
919 */
920 writel(ET_TXDMA_SNGL_EPKT|(PARM_DMA_CACHE_DEF << ET_TXDMA_CACHE_SHIFT),
921 &adapter->regs->txdma.csr);
922}
923
924static inline void add_10bit(u32 *v, int n)
925{
926 *v = INDEX10(*v + n) | (*v & ET_DMA10_WRAP);
927}
928
929static inline void add_12bit(u32 *v, int n)
930{
931 *v = INDEX12(*v + n) | (*v & ET_DMA12_WRAP);
932}
933
d2796743
ME
934/**
935 * et1310_config_mac_regs1 - Initialize the first part of MAC regs
936 * @adapter: pointer to our adapter structure
937 */
eb7a6ca6 938static void et1310_config_mac_regs1(struct et131x_adapter *adapter)
d2796743
ME
939{
940 struct mac_regs __iomem *macregs = &adapter->regs->mac;
941 u32 station1;
942 u32 station2;
943 u32 ipg;
944
945 /* First we need to reset everything. Write to MAC configuration
946 * register 1 to perform reset.
947 */
948 writel(0xC00F0000, &macregs->cfg1);
949
950 /* Next lets configure the MAC Inter-packet gap register */
951 ipg = 0x38005860; /* IPG1 0x38 IPG2 0x58 B2B 0x60 */
952 ipg |= 0x50 << 8; /* ifg enforce 0x50 */
953 writel(ipg, &macregs->ipg);
954
955 /* Next lets configure the MAC Half Duplex register */
956 /* BEB trunc 0xA, Ex Defer, Rexmit 0xF Coll 0x37 */
957 writel(0x00A1F037, &macregs->hfdp);
958
959 /* Next lets configure the MAC Interface Control register */
960 writel(0, &macregs->if_ctrl);
961
962 /* Let's move on to setting up the mii management configuration */
963 writel(0x07, &macregs->mii_mgmt_cfg); /* Clock reset 0x7 */
964
965 /* Next lets configure the MAC Station Address register. These
966 * values are read from the EEPROM during initialization and stored
967 * in the adapter structure. We write what is stored in the adapter
968 * structure to the MAC Station Address registers high and low. This
969 * station address is used for generating and checking pause control
970 * packets.
971 */
972 station2 = (adapter->addr[1] << ET_MAC_STATION_ADDR2_OC2_SHIFT) |
973 (adapter->addr[0] << ET_MAC_STATION_ADDR2_OC1_SHIFT);
974 station1 = (adapter->addr[5] << ET_MAC_STATION_ADDR1_OC6_SHIFT) |
975 (adapter->addr[4] << ET_MAC_STATION_ADDR1_OC5_SHIFT) |
976 (adapter->addr[3] << ET_MAC_STATION_ADDR1_OC4_SHIFT) |
977 adapter->addr[2];
978 writel(station1, &macregs->station_addr_1);
979 writel(station2, &macregs->station_addr_2);
980
ac399bc0 981 /* Max ethernet packet in bytes that will be passed by the mac without
d2796743
ME
982 * being truncated. Allow the MAC to pass 4 more than our max packet
983 * size. This is 4 for the Ethernet CRC.
984 *
985 * Packets larger than (registry_jumbo_packet) that do not contain a
986 * VLAN ID will be dropped by the Rx function.
987 */
988 writel(adapter->registry_jumbo_packet + 4, &macregs->max_fm_len);
989
990 /* clear out MAC config reset */
991 writel(0, &macregs->cfg1);
992}
993
994/**
995 * et1310_config_mac_regs2 - Initialize the second part of MAC regs
996 * @adapter: pointer to our adapter structure
997 */
eb7a6ca6 998static void et1310_config_mac_regs2(struct et131x_adapter *adapter)
d2796743
ME
999{
1000 int32_t delay = 0;
1001 struct mac_regs __iomem *mac = &adapter->regs->mac;
1002 struct phy_device *phydev = adapter->phydev;
1003 u32 cfg1;
1004 u32 cfg2;
1005 u32 ifctrl;
1006 u32 ctl;
1007
1008 ctl = readl(&adapter->regs->txmac.ctl);
1009 cfg1 = readl(&mac->cfg1);
1010 cfg2 = readl(&mac->cfg2);
1011 ifctrl = readl(&mac->if_ctrl);
1012
1013 /* Set up the if mode bits */
1014 cfg2 &= ~0x300;
1015 if (phydev && phydev->speed == SPEED_1000) {
1016 cfg2 |= 0x200;
1017 /* Phy mode bit */
1018 ifctrl &= ~(1 << 24);
1019 } else {
1020 cfg2 |= 0x100;
1021 ifctrl |= (1 << 24);
1022 }
1023
1024 /* We need to enable Rx/Tx */
1025 cfg1 |= CFG1_RX_ENABLE | CFG1_TX_ENABLE | CFG1_TX_FLOW;
1026 /* Initialize loop back to off */
1027 cfg1 &= ~(CFG1_LOOPBACK | CFG1_RX_FLOW);
1028 if (adapter->flowcontrol == FLOW_RXONLY ||
1029 adapter->flowcontrol == FLOW_BOTH)
1030 cfg1 |= CFG1_RX_FLOW;
1031 writel(cfg1, &mac->cfg1);
1032
1033 /* Now we need to initialize the MAC Configuration 2 register */
1034 /* preamble 7, check length, huge frame off, pad crc, crc enable
1035 full duplex off */
1036 cfg2 |= 0x7016;
1037 cfg2 &= ~0x0021;
1038
1039 /* Turn on duplex if needed */
1040 if (phydev && phydev->duplex == DUPLEX_FULL)
1041 cfg2 |= 0x01;
1042
1043 ifctrl &= ~(1 << 26);
1044 if (phydev && phydev->duplex == DUPLEX_HALF)
1045 ifctrl |= (1<<26); /* Enable ghd */
1046
1047 writel(ifctrl, &mac->if_ctrl);
1048 writel(cfg2, &mac->cfg2);
1049
1050 do {
1051 udelay(10);
1052 delay++;
1053 cfg1 = readl(&mac->cfg1);
1054 } while ((cfg1 & CFG1_WAIT) != CFG1_WAIT && delay < 100);
1055
1056 if (delay == 100) {
1057 dev_warn(&adapter->pdev->dev,
1058 "Syncd bits did not respond correctly cfg1 word 0x%08x\n",
1059 cfg1);
1060 }
1061
1062 /* Enable txmac */
1063 ctl |= 0x09; /* TX mac enable, FC disable */
1064 writel(ctl, &adapter->regs->txmac.ctl);
1065
1066 /* Ready to start the RXDMA/TXDMA engine */
1067 if (adapter->flags & fMP_ADAPTER_LOWER_POWER) {
1068 et131x_rx_dma_enable(adapter);
1069 et131x_tx_dma_enable(adapter);
1070 }
1071}
1072
2288760e
ME
1073/**
1074 * et1310_in_phy_coma - check if the device is in phy coma
1075 * @adapter: pointer to our adapter structure
1076 *
1077 * Returns 0 if the device is not in phy coma, 1 if it is in phy coma
1078 */
eb7a6ca6 1079static int et1310_in_phy_coma(struct et131x_adapter *adapter)
2288760e
ME
1080{
1081 u32 pmcsr;
1082
1083 pmcsr = readl(&adapter->regs->global.pm_csr);
1084
1085 return ET_PM_PHY_SW_COMA & pmcsr ? 1 : 0;
1086}
1087
eb7a6ca6 1088static void et1310_setup_device_for_multicast(struct et131x_adapter *adapter)
a4d444bd
ME
1089{
1090 struct rxmac_regs __iomem *rxmac = &adapter->regs->rxmac;
834d0ee3
FR
1091 u32 hash1 = 0;
1092 u32 hash2 = 0;
1093 u32 hash3 = 0;
1094 u32 hash4 = 0;
a4d444bd
ME
1095 u32 pm_csr;
1096
1097 /* If ET131X_PACKET_TYPE_MULTICAST is specified, then we provision
1098 * the multi-cast LIST. If it is NOT specified, (and "ALL" is not
1099 * specified) then we should pass NO multi-cast addresses to the
1100 * driver.
1101 */
1102 if (adapter->packet_filter & ET131X_PACKET_TYPE_MULTICAST) {
834d0ee3
FR
1103 int i;
1104
a4d444bd 1105 /* Loop through our multicast array and set up the device */
834d0ee3
FR
1106 for (i = 0; i < adapter->multicast_addr_count; i++) {
1107 u32 result;
1108
1109 result = ether_crc(6, adapter->multicast_list[i]);
a4d444bd
ME
1110
1111 result = (result & 0x3F800000) >> 23;
1112
1113 if (result < 32) {
1114 hash1 |= (1 << result);
1115 } else if ((31 < result) && (result < 64)) {
1116 result -= 32;
1117 hash2 |= (1 << result);
1118 } else if ((63 < result) && (result < 96)) {
1119 result -= 64;
1120 hash3 |= (1 << result);
1121 } else {
1122 result -= 96;
1123 hash4 |= (1 << result);
1124 }
1125 }
1126 }
1127
1128 /* Write out the new hash to the device */
1129 pm_csr = readl(&adapter->regs->global.pm_csr);
1130 if (!et1310_in_phy_coma(adapter)) {
1131 writel(hash1, &rxmac->multi_hash1);
1132 writel(hash2, &rxmac->multi_hash2);
1133 writel(hash3, &rxmac->multi_hash3);
1134 writel(hash4, &rxmac->multi_hash4);
1135 }
1136}
1137
eb7a6ca6 1138static void et1310_setup_device_for_unicast(struct et131x_adapter *adapter)
a4d444bd
ME
1139{
1140 struct rxmac_regs __iomem *rxmac = &adapter->regs->rxmac;
1141 u32 uni_pf1;
1142 u32 uni_pf2;
1143 u32 uni_pf3;
1144 u32 pm_csr;
1145
1146 /* Set up unicast packet filter reg 3 to be the first two octets of
1147 * the MAC address for both address
1148 *
1149 * Set up unicast packet filter reg 2 to be the octets 2 - 5 of the
1150 * MAC address for second address
1151 *
1152 * Set up unicast packet filter reg 3 to be the octets 2 - 5 of the
1153 * MAC address for first address
1154 */
1155 uni_pf3 = (adapter->addr[0] << ET_UNI_PF_ADDR2_1_SHIFT) |
1156 (adapter->addr[1] << ET_UNI_PF_ADDR2_2_SHIFT) |
1157 (adapter->addr[0] << ET_UNI_PF_ADDR1_1_SHIFT) |
1158 adapter->addr[1];
1159
1160 uni_pf2 = (adapter->addr[2] << ET_UNI_PF_ADDR2_3_SHIFT) |
1161 (adapter->addr[3] << ET_UNI_PF_ADDR2_4_SHIFT) |
1162 (adapter->addr[4] << ET_UNI_PF_ADDR2_5_SHIFT) |
1163 adapter->addr[5];
1164
1165 uni_pf1 = (adapter->addr[2] << ET_UNI_PF_ADDR1_3_SHIFT) |
1166 (adapter->addr[3] << ET_UNI_PF_ADDR1_4_SHIFT) |
1167 (adapter->addr[4] << ET_UNI_PF_ADDR1_5_SHIFT) |
1168 adapter->addr[5];
1169
1170 pm_csr = readl(&adapter->regs->global.pm_csr);
1171 if (!et1310_in_phy_coma(adapter)) {
1172 writel(uni_pf1, &rxmac->uni_pf_addr1);
1173 writel(uni_pf2, &rxmac->uni_pf_addr2);
1174 writel(uni_pf3, &rxmac->uni_pf_addr3);
1175 }
1176}
1177
eb7a6ca6 1178static void et1310_config_rxmac_regs(struct et131x_adapter *adapter)
d2796743
ME
1179{
1180 struct rxmac_regs __iomem *rxmac = &adapter->regs->rxmac;
1181 struct phy_device *phydev = adapter->phydev;
1182 u32 sa_lo;
1183 u32 sa_hi = 0;
1184 u32 pf_ctrl = 0;
1185
1186 /* Disable the MAC while it is being configured (also disable WOL) */
1187 writel(0x8, &rxmac->ctrl);
1188
1189 /* Initialize WOL to disabled. */
1190 writel(0, &rxmac->crc0);
1191 writel(0, &rxmac->crc12);
1192 writel(0, &rxmac->crc34);
1193
1194 /* We need to set the WOL mask0 - mask4 next. We initialize it to
1195 * its default Values of 0x00000000 because there are not WOL masks
1196 * as of this time.
1197 */
1198 writel(0, &rxmac->mask0_word0);
1199 writel(0, &rxmac->mask0_word1);
1200 writel(0, &rxmac->mask0_word2);
1201 writel(0, &rxmac->mask0_word3);
1202
1203 writel(0, &rxmac->mask1_word0);
1204 writel(0, &rxmac->mask1_word1);
1205 writel(0, &rxmac->mask1_word2);
1206 writel(0, &rxmac->mask1_word3);
1207
1208 writel(0, &rxmac->mask2_word0);
1209 writel(0, &rxmac->mask2_word1);
1210 writel(0, &rxmac->mask2_word2);
1211 writel(0, &rxmac->mask2_word3);
1212
1213 writel(0, &rxmac->mask3_word0);
1214 writel(0, &rxmac->mask3_word1);
1215 writel(0, &rxmac->mask3_word2);
1216 writel(0, &rxmac->mask3_word3);
1217
1218 writel(0, &rxmac->mask4_word0);
1219 writel(0, &rxmac->mask4_word1);
1220 writel(0, &rxmac->mask4_word2);
1221 writel(0, &rxmac->mask4_word3);
1222
1223 /* Lets setup the WOL Source Address */
1224 sa_lo = (adapter->addr[2] << ET_WOL_LO_SA3_SHIFT) |
1225 (adapter->addr[3] << ET_WOL_LO_SA4_SHIFT) |
1226 (adapter->addr[4] << ET_WOL_LO_SA5_SHIFT) |
1227 adapter->addr[5];
1228 writel(sa_lo, &rxmac->sa_lo);
1229
1230 sa_hi = (u32) (adapter->addr[0] << ET_WOL_HI_SA1_SHIFT) |
1231 adapter->addr[1];
1232 writel(sa_hi, &rxmac->sa_hi);
1233
1234 /* Disable all Packet Filtering */
1235 writel(0, &rxmac->pf_ctrl);
1236
1237 /* Let's initialize the Unicast Packet filtering address */
1238 if (adapter->packet_filter & ET131X_PACKET_TYPE_DIRECTED) {
1239 et1310_setup_device_for_unicast(adapter);
1240 pf_ctrl |= 4; /* Unicast filter */
1241 } else {
1242 writel(0, &rxmac->uni_pf_addr1);
1243 writel(0, &rxmac->uni_pf_addr2);
1244 writel(0, &rxmac->uni_pf_addr3);
1245 }
1246
1247 /* Let's initialize the Multicast hash */
1248 if (!(adapter->packet_filter & ET131X_PACKET_TYPE_ALL_MULTICAST)) {
1249 pf_ctrl |= 2; /* Multicast filter */
1250 et1310_setup_device_for_multicast(adapter);
1251 }
1252
1253 /* Runt packet filtering. Didn't work in version A silicon. */
1254 pf_ctrl |= (NIC_MIN_PACKET_SIZE + 4) << 16;
1255 pf_ctrl |= 8; /* Fragment filter */
1256
1257 if (adapter->registry_jumbo_packet > 8192)
1258 /* In order to transmit jumbo packets greater than 8k, the
1259 * FIFO between RxMAC and RxDMA needs to be reduced in size
1260 * to (16k - Jumbo packet size). In order to implement this,
1261 * we must use "cut through" mode in the RxMAC, which chops
1262 * packets down into segments which are (max_size * 16). In
1263 * this case we selected 256 bytes, since this is the size of
1264 * the PCI-Express TLP's that the 1310 uses.
1265 *
1266 * seg_en on, fc_en off, size 0x10
1267 */
1268 writel(0x41, &rxmac->mcif_ctrl_max_seg);
1269 else
1270 writel(0, &rxmac->mcif_ctrl_max_seg);
1271
1272 /* Initialize the MCIF water marks */
1273 writel(0, &rxmac->mcif_water_mark);
1274
1275 /* Initialize the MIF control */
1276 writel(0, &rxmac->mif_ctrl);
1277
1278 /* Initialize the Space Available Register */
1279 writel(0, &rxmac->space_avail);
1280
1281 /* Initialize the the mif_ctrl register
1282 * bit 3: Receive code error. One or more nibbles were signaled as
1283 * errors during the reception of the packet. Clear this
1284 * bit in Gigabit, set it in 100Mbit. This was derived
1285 * experimentally at UNH.
1286 * bit 4: Receive CRC error. The packet's CRC did not match the
1287 * internally generated CRC.
1288 * bit 5: Receive length check error. Indicates that frame length
1289 * field value in the packet does not match the actual data
1290 * byte length and is not a type field.
1291 * bit 16: Receive frame truncated.
1292 * bit 17: Drop packet enable
1293 */
1294 if (phydev && phydev->speed == SPEED_100)
1295 writel(0x30038, &rxmac->mif_ctrl);
1296 else
1297 writel(0x30030, &rxmac->mif_ctrl);
1298
1299 /* Finally we initialize RxMac to be enabled & WOL disabled. Packet
1300 * filter is always enabled since it is where the runt packets are
1301 * supposed to be dropped. For version A silicon, runt packet
1302 * dropping doesn't work, so it is disabled in the pf_ctrl register,
1303 * but we still leave the packet filter on.
1304 */
1305 writel(pf_ctrl, &rxmac->pf_ctrl);
1306 writel(0x9, &rxmac->ctrl);
1307}
1308
eb7a6ca6 1309static void et1310_config_txmac_regs(struct et131x_adapter *adapter)
d2796743
ME
1310{
1311 struct txmac_regs __iomem *txmac = &adapter->regs->txmac;
1312
1313 /* We need to update the Control Frame Parameters
1314 * cfpt - control frame pause timer set to 64 (0x40)
1315 * cfep - control frame extended pause timer set to 0x0
1316 */
1317 if (adapter->flowcontrol == FLOW_NONE)
1318 writel(0, &txmac->cf_param);
1319 else
1320 writel(0x40, &txmac->cf_param);
1321}
1322
eb7a6ca6 1323static void et1310_config_macstat_regs(struct et131x_adapter *adapter)
d2796743
ME
1324{
1325 struct macstat_regs __iomem *macstat =
1326 &adapter->regs->macstat;
1327
1328 /* Next we need to initialize all the macstat registers to zero on
1329 * the device.
1330 */
1331 writel(0, &macstat->txrx_0_64_byte_frames);
1332 writel(0, &macstat->txrx_65_127_byte_frames);
1333 writel(0, &macstat->txrx_128_255_byte_frames);
1334 writel(0, &macstat->txrx_256_511_byte_frames);
1335 writel(0, &macstat->txrx_512_1023_byte_frames);
1336 writel(0, &macstat->txrx_1024_1518_byte_frames);
1337 writel(0, &macstat->txrx_1519_1522_gvln_frames);
1338
1339 writel(0, &macstat->rx_bytes);
1340 writel(0, &macstat->rx_packets);
1341 writel(0, &macstat->rx_fcs_errs);
1342 writel(0, &macstat->rx_multicast_packets);
1343 writel(0, &macstat->rx_broadcast_packets);
1344 writel(0, &macstat->rx_control_frames);
1345 writel(0, &macstat->rx_pause_frames);
1346 writel(0, &macstat->rx_unknown_opcodes);
1347 writel(0, &macstat->rx_align_errs);
1348 writel(0, &macstat->rx_frame_len_errs);
1349 writel(0, &macstat->rx_code_errs);
1350 writel(0, &macstat->rx_carrier_sense_errs);
1351 writel(0, &macstat->rx_undersize_packets);
1352 writel(0, &macstat->rx_oversize_packets);
1353 writel(0, &macstat->rx_fragment_packets);
1354 writel(0, &macstat->rx_jabbers);
1355 writel(0, &macstat->rx_drops);
1356
1357 writel(0, &macstat->tx_bytes);
1358 writel(0, &macstat->tx_packets);
1359 writel(0, &macstat->tx_multicast_packets);
1360 writel(0, &macstat->tx_broadcast_packets);
1361 writel(0, &macstat->tx_pause_frames);
1362 writel(0, &macstat->tx_deferred);
1363 writel(0, &macstat->tx_excessive_deferred);
1364 writel(0, &macstat->tx_single_collisions);
1365 writel(0, &macstat->tx_multiple_collisions);
1366 writel(0, &macstat->tx_late_collisions);
1367 writel(0, &macstat->tx_excessive_collisions);
1368 writel(0, &macstat->tx_total_collisions);
1369 writel(0, &macstat->tx_pause_honored_frames);
1370 writel(0, &macstat->tx_drops);
1371 writel(0, &macstat->tx_jabbers);
1372 writel(0, &macstat->tx_fcs_errs);
1373 writel(0, &macstat->tx_control_frames);
1374 writel(0, &macstat->tx_oversize_frames);
1375 writel(0, &macstat->tx_undersize_frames);
1376 writel(0, &macstat->tx_fragments);
1377 writel(0, &macstat->carry_reg1);
1378 writel(0, &macstat->carry_reg2);
1379
1380 /* Unmask any counters that we want to track the overflow of.
1381 * Initially this will be all counters. It may become clear later
1382 * that we do not need to track all counters.
1383 */
1384 writel(0xFFFFBE32, &macstat->carry_reg1_mask);
1385 writel(0xFFFE7E8B, &macstat->carry_reg2_mask);
1386}
1387
2288760e
ME
1388/**
1389 * et131x_phy_mii_read - Read from the PHY through the MII Interface on the MAC
1390 * @adapter: pointer to our private adapter structure
1391 * @addr: the address of the transceiver
1392 * @reg: the register to read
1393 * @value: pointer to a 16-bit value in which the value will be stored
1394 *
1395 * Returns 0 on success, errno on failure (as defined in errno.h)
1396 */
eb7a6ca6 1397static int et131x_phy_mii_read(struct et131x_adapter *adapter, u8 addr,
2288760e
ME
1398 u8 reg, u16 *value)
1399{
1400 struct mac_regs __iomem *mac = &adapter->regs->mac;
1401 int status = 0;
1402 u32 delay = 0;
1403 u32 mii_addr;
1404 u32 mii_cmd;
1405 u32 mii_indicator;
1406
1407 /* Save a local copy of the registers we are dealing with so we can
1408 * set them back
1409 */
1410 mii_addr = readl(&mac->mii_mgmt_addr);
1411 mii_cmd = readl(&mac->mii_mgmt_cmd);
1412
1413 /* Stop the current operation */
1414 writel(0, &mac->mii_mgmt_cmd);
1415
1416 /* Set up the register we need to read from on the correct PHY */
1417 writel(MII_ADDR(addr, reg), &mac->mii_mgmt_addr);
1418
1419 writel(0x1, &mac->mii_mgmt_cmd);
1420
1421 do {
1422 udelay(50);
1423 delay++;
1424 mii_indicator = readl(&mac->mii_mgmt_indicator);
1425 } while ((mii_indicator & MGMT_WAIT) && delay < 50);
1426
1427 /* If we hit the max delay, we could not read the register */
1428 if (delay == 50) {
1429 dev_warn(&adapter->pdev->dev,
1430 "reg 0x%08x could not be read\n", reg);
1431 dev_warn(&adapter->pdev->dev, "status is 0x%08x\n",
1432 mii_indicator);
1433
1434 status = -EIO;
1435 }
1436
1437 /* If we hit here we were able to read the register and we need to
1438 * return the value to the caller */
1439 *value = readl(&mac->mii_mgmt_stat) & 0xFFFF;
1440
1441 /* Stop the read operation */
1442 writel(0, &mac->mii_mgmt_cmd);
1443
1444 /* set the registers we touched back to the state at which we entered
1445 * this function
1446 */
1447 writel(mii_addr, &mac->mii_mgmt_addr);
1448 writel(mii_cmd, &mac->mii_mgmt_cmd);
1449
1450 return status;
1451}
1452
eb7a6ca6 1453static int et131x_mii_read(struct et131x_adapter *adapter, u8 reg, u16 *value)
2288760e
ME
1454{
1455 struct phy_device *phydev = adapter->phydev;
1456
1457 if (!phydev)
1458 return -EIO;
1459
1460 return et131x_phy_mii_read(adapter, phydev->addr, reg, value);
1461}
1462
1463/**
1464 * et131x_mii_write - Write to a PHY register through the MII interface of the MAC
1465 * @adapter: pointer to our private adapter structure
1466 * @reg: the register to read
1467 * @value: 16-bit value to write
1468 *
1469 * FIXME: one caller in netdev still
1470 *
1471 * Return 0 on success, errno on failure (as defined in errno.h)
1472 */
eb7a6ca6 1473static int et131x_mii_write(struct et131x_adapter *adapter, u8 reg, u16 value)
2288760e
ME
1474{
1475 struct mac_regs __iomem *mac = &adapter->regs->mac;
1476 struct phy_device *phydev = adapter->phydev;
1477 int status = 0;
1478 u8 addr;
1479 u32 delay = 0;
1480 u32 mii_addr;
1481 u32 mii_cmd;
1482 u32 mii_indicator;
1483
1484 if (!phydev)
1485 return -EIO;
1486
1487 addr = phydev->addr;
1488
1489 /* Save a local copy of the registers we are dealing with so we can
1490 * set them back
1491 */
1492 mii_addr = readl(&mac->mii_mgmt_addr);
1493 mii_cmd = readl(&mac->mii_mgmt_cmd);
1494
1495 /* Stop the current operation */
1496 writel(0, &mac->mii_mgmt_cmd);
1497
1498 /* Set up the register we need to write to on the correct PHY */
1499 writel(MII_ADDR(addr, reg), &mac->mii_mgmt_addr);
1500
1501 /* Add the value to write to the registers to the mac */
1502 writel(value, &mac->mii_mgmt_ctrl);
1503
1504 do {
1505 udelay(50);
1506 delay++;
1507 mii_indicator = readl(&mac->mii_mgmt_indicator);
1508 } while ((mii_indicator & MGMT_BUSY) && delay < 100);
1509
1510 /* If we hit the max delay, we could not write the register */
1511 if (delay == 100) {
1512 u16 tmp;
1513
1514 dev_warn(&adapter->pdev->dev,
1515 "reg 0x%08x could not be written", reg);
1516 dev_warn(&adapter->pdev->dev, "status is 0x%08x\n",
1517 mii_indicator);
1518 dev_warn(&adapter->pdev->dev, "command is 0x%08x\n",
1519 readl(&mac->mii_mgmt_cmd));
1520
1521 et131x_mii_read(adapter, reg, &tmp);
1522
1523 status = -EIO;
1524 }
1525 /* Stop the write operation */
1526 writel(0, &mac->mii_mgmt_cmd);
1527
1528 /*
1529 * set the registers we touched back to the state at which we entered
1530 * this function
1531 */
1532 writel(mii_addr, &mac->mii_mgmt_addr);
1533 writel(mii_cmd, &mac->mii_mgmt_cmd);
1534
1535 return status;
1536}
1537
1538/* Still used from _mac for BIT_READ */
eb7a6ca6
FR
1539static void et1310_phy_access_mii_bit(struct et131x_adapter *adapter,
1540 u16 action, u16 regnum, u16 bitnum,
1541 u8 *value)
2288760e
ME
1542{
1543 u16 reg;
1544 u16 mask = 0x0001 << bitnum;
1545
1546 /* Read the requested register */
1547 et131x_mii_read(adapter, regnum, &reg);
1548
1549 switch (action) {
1550 case TRUEPHY_BIT_READ:
1551 *value = (reg & mask) >> bitnum;
1552 break;
1553
1554 case TRUEPHY_BIT_SET:
1555 et131x_mii_write(adapter, regnum, reg | mask);
1556 break;
1557
1558 case TRUEPHY_BIT_CLEAR:
1559 et131x_mii_write(adapter, regnum, reg & ~mask);
1560 break;
1561
1562 default:
1563 break;
1564 }
1565}
1566
eb7a6ca6 1567static void et1310_config_flow_control(struct et131x_adapter *adapter)
d2796743
ME
1568{
1569 struct phy_device *phydev = adapter->phydev;
1570
1571 if (phydev->duplex == DUPLEX_HALF) {
1572 adapter->flowcontrol = FLOW_NONE;
1573 } else {
1574 char remote_pause, remote_async_pause;
1575
1576 et1310_phy_access_mii_bit(adapter,
1577 TRUEPHY_BIT_READ, 5, 10, &remote_pause);
1578 et1310_phy_access_mii_bit(adapter,
1579 TRUEPHY_BIT_READ, 5, 11,
1580 &remote_async_pause);
1581
1582 if ((remote_pause == TRUEPHY_BIT_SET) &&
1583 (remote_async_pause == TRUEPHY_BIT_SET)) {
1584 adapter->flowcontrol = adapter->wanted_flow;
1585 } else if ((remote_pause == TRUEPHY_BIT_SET) &&
1586 (remote_async_pause == TRUEPHY_BIT_CLEAR)) {
1587 if (adapter->wanted_flow == FLOW_BOTH)
1588 adapter->flowcontrol = FLOW_BOTH;
1589 else
1590 adapter->flowcontrol = FLOW_NONE;
1591 } else if ((remote_pause == TRUEPHY_BIT_CLEAR) &&
1592 (remote_async_pause == TRUEPHY_BIT_CLEAR)) {
1593 adapter->flowcontrol = FLOW_NONE;
1594 } else {/* if (remote_pause == TRUEPHY_CLEAR_BIT &&
1595 remote_async_pause == TRUEPHY_SET_BIT) */
1596 if (adapter->wanted_flow == FLOW_BOTH)
1597 adapter->flowcontrol = FLOW_RXONLY;
1598 else
1599 adapter->flowcontrol = FLOW_NONE;
1600 }
1601 }
1602}
1603
1604/**
1605 * et1310_update_macstat_host_counters - Update the local copy of the statistics
1606 * @adapter: pointer to the adapter structure
1607 */
eb7a6ca6 1608static void et1310_update_macstat_host_counters(struct et131x_adapter *adapter)
d2796743
ME
1609{
1610 struct ce_stats *stats = &adapter->stats;
1611 struct macstat_regs __iomem *macstat =
1612 &adapter->regs->macstat;
1613
1614 stats->tx_collisions += readl(&macstat->tx_total_collisions);
1615 stats->tx_first_collisions += readl(&macstat->tx_single_collisions);
1616 stats->tx_deferred += readl(&macstat->tx_deferred);
1617 stats->tx_excessive_collisions +=
1618 readl(&macstat->tx_multiple_collisions);
1619 stats->tx_late_collisions += readl(&macstat->tx_late_collisions);
1620 stats->tx_underflows += readl(&macstat->tx_undersize_frames);
1621 stats->tx_max_pkt_errs += readl(&macstat->tx_oversize_frames);
1622
1623 stats->rx_align_errs += readl(&macstat->rx_align_errs);
1624 stats->rx_crc_errs += readl(&macstat->rx_code_errs);
1625 stats->rcvd_pkts_dropped += readl(&macstat->rx_drops);
1626 stats->rx_overflows += readl(&macstat->rx_oversize_packets);
1627 stats->rx_code_violations += readl(&macstat->rx_fcs_errs);
1628 stats->rx_length_errs += readl(&macstat->rx_frame_len_errs);
1629 stats->rx_other_errs += readl(&macstat->rx_fragment_packets);
1630}
1631
1632/**
1633 * et1310_handle_macstat_interrupt
1634 * @adapter: pointer to the adapter structure
1635 *
1636 * One of the MACSTAT counters has wrapped. Update the local copy of
1637 * the statistics held in the adapter structure, checking the "wrap"
1638 * bit for each counter.
1639 */
eb7a6ca6 1640static void et1310_handle_macstat_interrupt(struct et131x_adapter *adapter)
d2796743
ME
1641{
1642 u32 carry_reg1;
1643 u32 carry_reg2;
1644
1645 /* Read the interrupt bits from the register(s). These are Clear On
1646 * Write.
1647 */
1648 carry_reg1 = readl(&adapter->regs->macstat.carry_reg1);
1649 carry_reg2 = readl(&adapter->regs->macstat.carry_reg2);
1650
1651 writel(carry_reg1, &adapter->regs->macstat.carry_reg1);
1652 writel(carry_reg2, &adapter->regs->macstat.carry_reg2);
1653
1654 /* We need to do update the host copy of all the MAC_STAT counters.
1655 * For each counter, check it's overflow bit. If the overflow bit is
1656 * set, then increment the host version of the count by one complete
1657 * revolution of the counter. This routine is called when the counter
1658 * block indicates that one of the counters has wrapped.
1659 */
1660 if (carry_reg1 & (1 << 14))
1661 adapter->stats.rx_code_violations += COUNTER_WRAP_16_BIT;
1662 if (carry_reg1 & (1 << 8))
1663 adapter->stats.rx_align_errs += COUNTER_WRAP_12_BIT;
1664 if (carry_reg1 & (1 << 7))
1665 adapter->stats.rx_length_errs += COUNTER_WRAP_16_BIT;
1666 if (carry_reg1 & (1 << 2))
1667 adapter->stats.rx_other_errs += COUNTER_WRAP_16_BIT;
1668 if (carry_reg1 & (1 << 6))
1669 adapter->stats.rx_crc_errs += COUNTER_WRAP_16_BIT;
1670 if (carry_reg1 & (1 << 3))
1671 adapter->stats.rx_overflows += COUNTER_WRAP_16_BIT;
1672 if (carry_reg1 & (1 << 0))
1673 adapter->stats.rcvd_pkts_dropped += COUNTER_WRAP_16_BIT;
1674 if (carry_reg2 & (1 << 16))
1675 adapter->stats.tx_max_pkt_errs += COUNTER_WRAP_12_BIT;
1676 if (carry_reg2 & (1 << 15))
1677 adapter->stats.tx_underflows += COUNTER_WRAP_12_BIT;
1678 if (carry_reg2 & (1 << 6))
1679 adapter->stats.tx_first_collisions += COUNTER_WRAP_12_BIT;
1680 if (carry_reg2 & (1 << 8))
1681 adapter->stats.tx_deferred += COUNTER_WRAP_12_BIT;
1682 if (carry_reg2 & (1 << 5))
1683 adapter->stats.tx_excessive_collisions += COUNTER_WRAP_12_BIT;
1684 if (carry_reg2 & (1 << 4))
1685 adapter->stats.tx_late_collisions += COUNTER_WRAP_12_BIT;
1686 if (carry_reg2 & (1 << 2))
1687 adapter->stats.tx_collisions += COUNTER_WRAP_12_BIT;
1688}
1689
eb7a6ca6 1690static int et131x_mdio_read(struct mii_bus *bus, int phy_addr, int reg)
d2796743
ME
1691{
1692 struct net_device *netdev = bus->priv;
1693 struct et131x_adapter *adapter = netdev_priv(netdev);
1694 u16 value;
1695 int ret;
1696
1697 ret = et131x_phy_mii_read(adapter, phy_addr, reg, &value);
1698
1699 if (ret < 0)
1700 return ret;
1701 else
1702 return value;
1703}
1704
bf3313a1 1705static int et131x_mdio_write(struct mii_bus *bus, int phy_addr,
1706 int reg, u16 value)
d2796743
ME
1707{
1708 struct net_device *netdev = bus->priv;
1709 struct et131x_adapter *adapter = netdev_priv(netdev);
1710
1711 return et131x_mii_write(adapter, reg, value);
1712}
1713
eb7a6ca6 1714static int et131x_mdio_reset(struct mii_bus *bus)
d2796743
ME
1715{
1716 struct net_device *netdev = bus->priv;
1717 struct et131x_adapter *adapter = netdev_priv(netdev);
1718
1719 et131x_mii_write(adapter, MII_BMCR, BMCR_RESET);
1720
1721 return 0;
1722}
1723
d2796743
ME
1724/**
1725 * et1310_phy_power_down - PHY power control
1726 * @adapter: device to control
1727 * @down: true for off/false for back on
1728 *
1729 * one hundred, ten, one thousand megs
1730 * How would you like to have your LAN accessed
1731 * Can't you see that this code processed
1732 * Phy power, phy power..
1733 */
eb7a6ca6 1734static void et1310_phy_power_down(struct et131x_adapter *adapter, bool down)
d2796743
ME
1735{
1736 u16 data;
1737
1738 et131x_mii_read(adapter, MII_BMCR, &data);
1739 data &= ~BMCR_PDOWN;
1740 if (down)
1741 data |= BMCR_PDOWN;
1742 et131x_mii_write(adapter, MII_BMCR, data);
1743}
1744
d2796743
ME
1745/**
1746 * et131x_xcvr_init - Init the phy if we are setting it into force mode
1747 * @adapter: pointer to our private adapter structure
1748 *
1749 */
eb7a6ca6 1750static void et131x_xcvr_init(struct et131x_adapter *adapter)
d2796743
ME
1751{
1752 u16 imr;
1753 u16 isr;
1754 u16 lcr2;
1755
1756 et131x_mii_read(adapter, PHY_INTERRUPT_STATUS, &isr);
1757 et131x_mii_read(adapter, PHY_INTERRUPT_MASK, &imr);
1758
1759 /* Set the link status interrupt only. Bad behavior when link status
1760 * and auto neg are set, we run into a nested interrupt problem
1761 */
b5b86a4d
DC
1762 imr |= (ET_PHY_INT_MASK_AUTONEGSTAT |
1763 ET_PHY_INT_MASK_LINKSTAT |
d2796743
ME
1764 ET_PHY_INT_MASK_ENABLE);
1765
1766 et131x_mii_write(adapter, PHY_INTERRUPT_MASK, imr);
1767
1768 /* Set the LED behavior such that LED 1 indicates speed (off =
1769 * 10Mbits, blink = 100Mbits, on = 1000Mbits) and LED 2 indicates
1770 * link and activity (on for link, blink off for activity).
1771 *
1772 * NOTE: Some customizations have been added here for specific
1773 * vendors; The LED behavior is now determined by vendor data in the
1774 * EEPROM. However, the above description is the default.
1775 */
1776 if ((adapter->eeprom_data[1] & 0x4) == 0) {
1777 et131x_mii_read(adapter, PHY_LED_2, &lcr2);
1778
b5b86a4d 1779 lcr2 &= (ET_LED2_LED_100TX | ET_LED2_LED_1000T);
d2796743
ME
1780 lcr2 |= (LED_VAL_LINKON_ACTIVE << LED_LINK_SHIFT);
1781
1782 if ((adapter->eeprom_data[1] & 0x8) == 0)
1783 lcr2 |= (LED_VAL_1000BT_100BTX << LED_TXRX_SHIFT);
1784 else
1785 lcr2 |= (LED_VAL_LINKON << LED_TXRX_SHIFT);
1786
1787 et131x_mii_write(adapter, PHY_LED_2, lcr2);
1788 }
1789}
1790
36f2771a
ME
1791/**
1792 * et131x_configure_global_regs - configure JAGCore global regs
1793 * @adapter: pointer to our adapter structure
1794 *
1795 * Used to configure the global registers on the JAGCore
1796 */
eb7a6ca6 1797static void et131x_configure_global_regs(struct et131x_adapter *adapter)
36f2771a
ME
1798{
1799 struct global_regs __iomem *regs = &adapter->regs->global;
1800
1801 writel(0, &regs->rxq_start_addr);
1802 writel(INTERNAL_MEM_SIZE - 1, &regs->txq_end_addr);
1803
1804 if (adapter->registry_jumbo_packet < 2048) {
1805 /* Tx / RxDMA and Tx/Rx MAC interfaces have a 1k word
1806 * block of RAM that the driver can split between Tx
1807 * and Rx as it desires. Our default is to split it
1808 * 50/50:
1809 */
1810 writel(PARM_RX_MEM_END_DEF, &regs->rxq_end_addr);
1811 writel(PARM_RX_MEM_END_DEF + 1, &regs->txq_start_addr);
1812 } else if (adapter->registry_jumbo_packet < 8192) {
1813 /* For jumbo packets > 2k but < 8k, split 50-50. */
1814 writel(INTERNAL_MEM_RX_OFFSET, &regs->rxq_end_addr);
1815 writel(INTERNAL_MEM_RX_OFFSET + 1, &regs->txq_start_addr);
1816 } else {
1817 /* 9216 is the only packet size greater than 8k that
1818 * is available. The Tx buffer has to be big enough
1819 * for one whole packet on the Tx side. We'll make
1820 * the Tx 9408, and give the rest to Rx
1821 */
1822 writel(0x01b3, &regs->rxq_end_addr);
1823 writel(0x01b4, &regs->txq_start_addr);
1824 }
1825
1826 /* Initialize the loopback register. Disable all loopbacks. */
1827 writel(0, &regs->loopback);
1828
1829 /* MSI Register */
1830 writel(0, &regs->msi_config);
1831
1832 /* By default, disable the watchdog timer. It will be enabled when
1833 * a packet is queued.
1834 */
1835 writel(0, &regs->watchdog_timer);
1836}
1837
36f2771a
ME
1838/**
1839 * et131x_config_rx_dma_regs - Start of Rx_DMA init sequence
1840 * @adapter: pointer to our adapter structure
1841 */
eb7a6ca6 1842static void et131x_config_rx_dma_regs(struct et131x_adapter *adapter)
36f2771a
ME
1843{
1844 struct rxdma_regs __iomem *rx_dma = &adapter->regs->rxdma;
1845 struct rx_ring *rx_local = &adapter->rx_ring;
1846 struct fbr_desc *fbr_entry;
1847 u32 entry;
1848 u32 psr_num_des;
1849 unsigned long flags;
788ca84a 1850 u8 id;
36f2771a
ME
1851
1852 /* Halt RXDMA to perform the reconfigure. */
1853 et131x_rx_dma_disable(adapter);
1854
25e8e8ab
ME
1855 /* Load the completion writeback physical address */
1856 writel(upper_32_bits(rx_local->rx_status_bus), &rx_dma->dma_wb_base_hi);
1857 writel(lower_32_bits(rx_local->rx_status_bus), &rx_dma->dma_wb_base_lo);
36f2771a
ME
1858
1859 memset(rx_local->rx_status_block, 0, sizeof(struct rx_status_block));
1860
1861 /* Set the address and parameters of the packet status ring into the
1862 * 1310's registers
1863 */
25e8e8ab
ME
1864 writel(upper_32_bits(rx_local->ps_ring_physaddr), &rx_dma->psr_base_hi);
1865 writel(lower_32_bits(rx_local->ps_ring_physaddr), &rx_dma->psr_base_lo);
36f2771a
ME
1866 writel(rx_local->psr_num_entries - 1, &rx_dma->psr_num_des);
1867 writel(0, &rx_dma->psr_full_offset);
1868
1869 psr_num_des = readl(&rx_dma->psr_num_des) & 0xFFF;
1870 writel((psr_num_des * LO_MARK_PERCENT_FOR_PSR) / 100,
1871 &rx_dma->psr_min_des);
1872
1873 spin_lock_irqsave(&adapter->rcv_lock, flags);
1874
1875 /* These local variables track the PSR in the adapter structure */
1876 rx_local->local_psr_full = 0;
1877
788ca84a
ME
1878 for (id = 0; id < NUM_FBRS; id++) {
1879 u32 *num_des;
1880 u32 *full_offset;
1881 u32 *min_des;
1882 u32 *base_hi;
1883 u32 *base_lo;
1884
1885 if (id == 0) {
1886 num_des = &rx_dma->fbr1_num_des;
1887 full_offset = &rx_dma->fbr1_full_offset;
1888 min_des = &rx_dma->fbr1_min_des;
1889 base_hi = &rx_dma->fbr1_base_hi;
1890 base_lo = &rx_dma->fbr1_base_lo;
1891 } else {
1892 num_des = &rx_dma->fbr0_num_des;
1893 full_offset = &rx_dma->fbr0_full_offset;
1894 min_des = &rx_dma->fbr0_min_des;
1895 base_hi = &rx_dma->fbr0_base_hi;
1896 base_lo = &rx_dma->fbr0_base_lo;
1897 }
36f2771a 1898
788ca84a
ME
1899 /* Now's the best time to initialize FBR contents */
1900 fbr_entry = (struct fbr_desc *) rx_local->fbr[id]->ring_virtaddr;
1901 for (entry = 0; entry < rx_local->fbr[id]->num_entries; entry++) {
1902 fbr_entry->addr_hi = rx_local->fbr[id]->bus_high[entry];
1903 fbr_entry->addr_lo = rx_local->fbr[id]->bus_low[entry];
1904 fbr_entry->word2 = entry;
1905 fbr_entry++;
1906 }
36f2771a 1907
788ca84a
ME
1908 /* Set the address and parameters of Free buffer ring 1 and 0
1909 * into the 1310's registers
1910 */
1911 writel(upper_32_bits(rx_local->fbr[id]->ring_physaddr), base_hi);
1912 writel(lower_32_bits(rx_local->fbr[id]->ring_physaddr), base_lo);
1913 writel(rx_local->fbr[id]->num_entries - 1, num_des);
1914 writel(ET_DMA10_WRAP, full_offset);
36f2771a 1915
788ca84a
ME
1916 /* This variable tracks the free buffer ring 1 full position,
1917 * so it has to match the above.
1918 */
1919 rx_local->fbr[id]->local_full = ET_DMA10_WRAP;
1920 writel(((rx_local->fbr[id]->num_entries *
1921 LO_MARK_PERCENT_FOR_RX) / 100) - 1,
1922 min_des);
1923 }
36f2771a
ME
1924
1925 /* Program the number of packets we will receive before generating an
1926 * interrupt.
1927 * For version B silicon, this value gets updated once autoneg is
1928 *complete.
1929 */
1930 writel(PARM_RX_NUM_BUFS_DEF, &rx_dma->num_pkt_done);
1931
1932 /* The "time_done" is not working correctly to coalesce interrupts
1933 * after a given time period, but rather is giving us an interrupt
1934 * regardless of whether we have received packets.
1935 * This value gets updated once autoneg is complete.
1936 */
1937 writel(PARM_RX_TIME_INT_DEF, &rx_dma->max_pkt_time);
1938
1939 spin_unlock_irqrestore(&adapter->rcv_lock, flags);
1940}
1941
1942/**
1943 * et131x_config_tx_dma_regs - Set up the tx dma section of the JAGCore.
1944 * @adapter: pointer to our private adapter structure
1945 *
1946 * Configure the transmit engine with the ring buffers we have created
1947 * and prepare it for use.
1948 */
eb7a6ca6 1949static void et131x_config_tx_dma_regs(struct et131x_adapter *adapter)
36f2771a
ME
1950{
1951 struct txdma_regs __iomem *txdma = &adapter->regs->txdma;
1952
1953 /* Load the hardware with the start of the transmit descriptor ring. */
25e8e8ab 1954 writel(upper_32_bits(adapter->tx_ring.tx_desc_ring_pa),
36f2771a 1955 &txdma->pr_base_hi);
25e8e8ab 1956 writel(lower_32_bits(adapter->tx_ring.tx_desc_ring_pa),
36f2771a
ME
1957 &txdma->pr_base_lo);
1958
1959 /* Initialise the transmit DMA engine */
1960 writel(NUM_DESC_PER_RING_TX - 1, &txdma->pr_num_des);
1961
1962 /* Load the completion writeback physical address */
25e8e8ab
ME
1963 writel(upper_32_bits(adapter->tx_ring.tx_status_pa),
1964 &txdma->dma_wb_base_hi);
1965 writel(lower_32_bits(adapter->tx_ring.tx_status_pa),
1966 &txdma->dma_wb_base_lo);
36f2771a
ME
1967
1968 *adapter->tx_ring.tx_status = 0;
1969
1970 writel(0, &txdma->service_request);
1971 adapter->tx_ring.send_idx = 0;
1972}
1973
1974/**
1975 * et131x_adapter_setup - Set the adapter up as per cassini+ documentation
1976 * @adapter: pointer to our private adapter structure
1977 *
1978 * Returns 0 on success, errno on failure (as defined in errno.h)
1979 */
eb7a6ca6 1980static void et131x_adapter_setup(struct et131x_adapter *adapter)
36f2771a
ME
1981{
1982 /* Configure the JAGCore */
1983 et131x_configure_global_regs(adapter);
1984
1985 et1310_config_mac_regs1(adapter);
1986
1987 /* Configure the MMC registers */
1988 /* All we need to do is initialize the Memory Control Register */
1989 writel(ET_MMC_ENABLE, &adapter->regs->mmc.mmc_ctrl);
1990
1991 et1310_config_rxmac_regs(adapter);
1992 et1310_config_txmac_regs(adapter);
1993
1994 et131x_config_rx_dma_regs(adapter);
1995 et131x_config_tx_dma_regs(adapter);
1996
1997 et1310_config_macstat_regs(adapter);
1998
1999 et1310_phy_power_down(adapter, 0);
2000 et131x_xcvr_init(adapter);
2001}
2002
5da2b158
ME
2003/**
2004 * et131x_soft_reset - Issue a soft reset to the hardware, complete for ET1310
2005 * @adapter: pointer to our private adapter structure
2006 */
eb7a6ca6 2007static void et131x_soft_reset(struct et131x_adapter *adapter)
5da2b158
ME
2008{
2009 /* Disable MAC Core */
2010 writel(0xc00f0000, &adapter->regs->mac.cfg1);
2011
2012 /* Set everything to a reset value */
2013 writel(0x7F, &adapter->regs->global.sw_reset);
2014 writel(0x000f0000, &adapter->regs->mac.cfg1);
2015 writel(0x00000000, &adapter->regs->mac.cfg1);
2016}
2017
a4d444bd
ME
2018/**
2019 * et131x_enable_interrupts - enable interrupt
2020 * @adapter: et131x device
2021 *
2022 * Enable the appropriate interrupts on the ET131x according to our
2023 * configuration
2024 */
eb7a6ca6 2025static void et131x_enable_interrupts(struct et131x_adapter *adapter)
a4d444bd
ME
2026{
2027 u32 mask;
2028
2029 /* Enable all global interrupts */
2030 if (adapter->flowcontrol == FLOW_TXONLY ||
2031 adapter->flowcontrol == FLOW_BOTH)
2032 mask = INT_MASK_ENABLE;
2033 else
2034 mask = INT_MASK_ENABLE_NO_FLOW;
2035
2036 writel(mask, &adapter->regs->global.int_mask);
2037}
2038
2039/**
2040 * et131x_disable_interrupts - interrupt disable
2041 * @adapter: et131x device
2042 *
2043 * Block all interrupts from the et131x device at the device itself
2044 */
eb7a6ca6 2045static void et131x_disable_interrupts(struct et131x_adapter *adapter)
a4d444bd
ME
2046{
2047 /* Disable all global interrupts */
2048 writel(INT_MASK_DISABLE, &adapter->regs->global.int_mask);
2049}
2050
2051/**
2052 * et131x_tx_dma_disable - Stop of Tx_DMA on the ET1310
2053 * @adapter: pointer to our adapter structure
2054 */
eb7a6ca6 2055static void et131x_tx_dma_disable(struct et131x_adapter *adapter)
a4d444bd
ME
2056{
2057 /* Setup the tramsmit dma configuration register */
2058 writel(ET_TXDMA_CSR_HALT|ET_TXDMA_SNGL_EPKT,
2059 &adapter->regs->txdma.csr);
2060}
2061
a4d444bd
ME
2062/**
2063 * et131x_enable_txrx - Enable tx/rx queues
2064 * @netdev: device to be enabled
2065 */
eb7a6ca6 2066static void et131x_enable_txrx(struct net_device *netdev)
a4d444bd
ME
2067{
2068 struct et131x_adapter *adapter = netdev_priv(netdev);
2069
2070 /* Enable the Tx and Rx DMA engines (if not already enabled) */
2071 et131x_rx_dma_enable(adapter);
2072 et131x_tx_dma_enable(adapter);
2073
2074 /* Enable device interrupts */
2075 if (adapter->flags & fMP_ADAPTER_INTERRUPT_IN_USE)
2076 et131x_enable_interrupts(adapter);
2077
2078 /* We're ready to move some data, so start the queue */
2079 netif_start_queue(netdev);
2080}
2081
2082/**
2083 * et131x_disable_txrx - Disable tx/rx queues
2084 * @netdev: device to be disabled
2085 */
eb7a6ca6 2086static void et131x_disable_txrx(struct net_device *netdev)
a4d444bd
ME
2087{
2088 struct et131x_adapter *adapter = netdev_priv(netdev);
2089
2090 /* First thing is to stop the queue */
2091 netif_stop_queue(netdev);
2092
2093 /* Stop the Tx and Rx DMA engines */
2094 et131x_rx_dma_disable(adapter);
2095 et131x_tx_dma_disable(adapter);
2096
2097 /* Disable device interrupts */
2098 et131x_disable_interrupts(adapter);
2099}
2100
8310c602
ME
2101/**
2102 * et131x_init_send - Initialize send data structures
2103 * @adapter: pointer to our private adapter structure
2104 */
eb7a6ca6 2105static void et131x_init_send(struct et131x_adapter *adapter)
8310c602
ME
2106{
2107 struct tcb *tcb;
2108 u32 ct;
2109 struct tx_ring *tx_ring;
2110
2111 /* Setup some convenience pointers */
2112 tx_ring = &adapter->tx_ring;
2113 tcb = adapter->tx_ring.tcb_ring;
2114
2115 tx_ring->tcb_qhead = tcb;
2116
2117 memset(tcb, 0, sizeof(struct tcb) * NUM_TCB);
2118
2119 /* Go through and set up each TCB */
2120 for (ct = 0; ct++ < NUM_TCB; tcb++)
2121 /* Set the link pointer in HW TCB to the next TCB in the
2122 * chain
2123 */
2124 tcb->next = tcb + 1;
2125
2126 /* Set the tail pointer */
2127 tcb--;
2128 tx_ring->tcb_qtail = tcb;
2129 tcb->next = NULL;
2130 /* Curr send queue should now be empty */
2131 tx_ring->send_head = NULL;
2132 tx_ring->send_tail = NULL;
2133}
2134
d2796743
ME
2135/**
2136 * et1310_enable_phy_coma - called when network cable is unplugged
2137 * @adapter: pointer to our adapter structure
2138 *
2139 * driver receive an phy status change interrupt while in D0 and check that
2140 * phy_status is down.
2141 *
2142 * -- gate off JAGCore;
2143 * -- set gigE PHY in Coma mode
2144 * -- wake on phy_interrupt; Perform software reset JAGCore,
2145 * re-initialize jagcore and gigE PHY
2146 *
2147 * Add D0-ASPM-PhyLinkDown Support:
2148 * -- while in D0, when there is a phy_interrupt indicating phy link
2149 * down status, call the MPSetPhyComa routine to enter this active
2150 * state power saving mode
2151 * -- while in D0-ASPM-PhyLinkDown mode, when there is a phy_interrupt
2152 * indicating linkup status, call the MPDisablePhyComa routine to
2153 * restore JAGCore and gigE PHY
2154 */
eb7a6ca6 2155static void et1310_enable_phy_coma(struct et131x_adapter *adapter)
d2796743
ME
2156{
2157 unsigned long flags;
2158 u32 pmcsr;
2159
2160 pmcsr = readl(&adapter->regs->global.pm_csr);
2161
2162 /* Save the GbE PHY speed and duplex modes. Need to restore this
2163 * when cable is plugged back in
2164 */
2165 /*
2166 * TODO - when PM is re-enabled, check if we need to
2167 * perform a similar task as this -
2168 * adapter->pdown_speed = adapter->ai_force_speed;
2169 * adapter->pdown_duplex = adapter->ai_force_duplex;
2170 */
2171
2172 /* Stop sending packets. */
2173 spin_lock_irqsave(&adapter->send_hw_lock, flags);
2174 adapter->flags |= fMP_ADAPTER_LOWER_POWER;
2175 spin_unlock_irqrestore(&adapter->send_hw_lock, flags);
2176
2177 /* Wait for outstanding Receive packets */
2178
2179 et131x_disable_txrx(adapter->netdev);
2180
2181 /* Gate off JAGCore 3 clock domains */
2182 pmcsr &= ~ET_PMCSR_INIT;
2183 writel(pmcsr, &adapter->regs->global.pm_csr);
2184
2185 /* Program gigE PHY in to Coma mode */
2186 pmcsr |= ET_PM_PHY_SW_COMA;
2187 writel(pmcsr, &adapter->regs->global.pm_csr);
2188}
2189
2190/**
2191 * et1310_disable_phy_coma - Disable the Phy Coma Mode
2192 * @adapter: pointer to our adapter structure
2193 */
eb7a6ca6 2194static void et1310_disable_phy_coma(struct et131x_adapter *adapter)
d2796743
ME
2195{
2196 u32 pmcsr;
2197
2198 pmcsr = readl(&adapter->regs->global.pm_csr);
2199
2200 /* Disable phy_sw_coma register and re-enable JAGCore clocks */
2201 pmcsr |= ET_PMCSR_INIT;
2202 pmcsr &= ~ET_PM_PHY_SW_COMA;
2203 writel(pmcsr, &adapter->regs->global.pm_csr);
2204
2205 /* Restore the GbE PHY speed and duplex modes;
2206 * Reset JAGCore; re-configure and initialize JAGCore and gigE PHY
2207 */
2208 /* TODO - when PM is re-enabled, check if we need to
2209 * perform a similar task as this -
2210 * adapter->ai_force_speed = adapter->pdown_speed;
2211 * adapter->ai_force_duplex = adapter->pdown_duplex;
2212 */
2213
2214 /* Re-initialize the send structures */
2215 et131x_init_send(adapter);
2216
d2796743
ME
2217 /* Bring the device back to the state it was during init prior to
2218 * autonegotiation being complete. This way, when we get the auto-neg
2219 * complete interrupt, we can complete init by calling ConfigMacREGS2.
2220 */
2221 et131x_soft_reset(adapter);
2222
2223 /* setup et1310 as per the documentation ?? */
2224 et131x_adapter_setup(adapter);
2225
2226 /* Allow Tx to restart */
2227 adapter->flags &= ~fMP_ADAPTER_LOWER_POWER;
2228
2229 et131x_enable_txrx(adapter->netdev);
2230}
2231
d2796743
ME
2232static inline u32 bump_free_buff_ring(u32 *free_buff_ring, u32 limit)
2233{
2234 u32 tmp_free_buff_ring = *free_buff_ring;
2235 tmp_free_buff_ring++;
2236 /* This works for all cases where limit < 1024. The 1023 case
2237 works because 1023++ is 1024 which means the if condition is not
2238 taken but the carry of the bit into the wrap bit toggles the wrap
2239 value correctly */
2240 if ((tmp_free_buff_ring & ET_DMA10_MASK) > limit) {
2241 tmp_free_buff_ring &= ~ET_DMA10_MASK;
2242 tmp_free_buff_ring ^= ET_DMA10_WRAP;
2243 }
2244 /* For the 1023 case */
2245 tmp_free_buff_ring &= (ET_DMA10_MASK|ET_DMA10_WRAP);
2246 *free_buff_ring = tmp_free_buff_ring;
2247 return tmp_free_buff_ring;
2248}
2249
44012dfe
ME
2250/**
2251 * et131x_align_allocated_memory - Align allocated memory on a given boundary
2252 * @adapter: pointer to our adapter structure
2253 * @phys_addr: pointer to Physical address
2254 * @offset: pointer to the offset variable
2255 * @mask: correct mask
2256 */
eb7a6ca6 2257static void et131x_align_allocated_memory(struct et131x_adapter *adapter,
788ca84a
ME
2258 dma_addr_t *phys_addr,
2259 dma_addr_t *offset,
e0134715 2260 u64 mask)
44012dfe 2261{
e0134715 2262 u64 new_addr = *phys_addr & ~mask;
44012dfe
ME
2263
2264 *offset = 0;
2265
44012dfe
ME
2266 if (new_addr != *phys_addr) {
2267 /* Move to next aligned block */
2268 new_addr += mask + 1;
2269 /* Return offset for adjusting virt addr */
2270 *offset = new_addr - *phys_addr;
2271 /* Return new physical address */
2272 *phys_addr = new_addr;
2273 }
2274}
2275
d2796743
ME
2276/**
2277 * et131x_rx_dma_memory_alloc
2278 * @adapter: pointer to our private adapter structure
2279 *
2280 * Returns 0 on success and errno on failure (as defined in errno.h)
2281 *
2282 * Allocates Free buffer ring 1 for sure, free buffer ring 0 if required,
2283 * and the Packet Status Ring.
2284 */
eb7a6ca6 2285static int et131x_rx_dma_memory_alloc(struct et131x_adapter *adapter)
d2796743 2286{
788ca84a 2287 u8 id;
d2796743
ME
2288 u32 i, j;
2289 u32 bufsize;
2290 u32 pktstat_ringsize, fbr_chunksize;
2291 struct rx_ring *rx_ring;
2292
2293 /* Setup some convenience pointers */
2294 rx_ring = &adapter->rx_ring;
2295
2296 /* Alloc memory for the lookup table */
d2796743 2297 rx_ring->fbr[1] = kmalloc(sizeof(struct fbr_lookup), GFP_KERNEL);
e592a9b0 2298 rx_ring->fbr[0] = kmalloc(sizeof(struct fbr_lookup), GFP_KERNEL);
d2796743
ME
2299
2300 /* The first thing we will do is configure the sizes of the buffer
2301 * rings. These will change based on jumbo packet support. Larger
2302 * jumbo packets increases the size of each entry in FBR0, and the
2303 * number of entries in FBR0, while at the same time decreasing the
2304 * number of entries in FBR1.
2305 *
2306 * FBR1 holds "large" frames, FBR0 holds "small" frames. If FBR1
2307 * entries are huge in order to accommodate a "jumbo" frame, then it
2308 * will have less entries. Conversely, FBR1 will now be relied upon
2309 * to carry more "normal" frames, thus it's entry size also increases
2310 * and the number of entries goes up too (since it now carries
2311 * "small" + "regular" packets.
2312 *
2313 * In this scheme, we try to maintain 512 entries between the two
2314 * rings. Also, FBR1 remains a constant size - when it's size doubles
2315 * the number of entries halves. FBR0 increases in size, however.
2316 */
2317
2318 if (adapter->registry_jumbo_packet < 2048) {
e592a9b0 2319 rx_ring->fbr[1]->buffsize = 256;
6abafc16 2320 rx_ring->fbr[1]->num_entries = 512;
e592a9b0
ME
2321 rx_ring->fbr[0]->buffsize = 2048;
2322 rx_ring->fbr[0]->num_entries = 512;
d2796743 2323 } else if (adapter->registry_jumbo_packet < 4096) {
e592a9b0
ME
2324 rx_ring->fbr[1]->buffsize = 512;
2325 rx_ring->fbr[1]->num_entries = 1024;
e592a9b0
ME
2326 rx_ring->fbr[0]->buffsize = 4096;
2327 rx_ring->fbr[0]->num_entries = 512;
d2796743 2328 } else {
e592a9b0
ME
2329 rx_ring->fbr[1]->buffsize = 1024;
2330 rx_ring->fbr[1]->num_entries = 768;
e592a9b0
ME
2331 rx_ring->fbr[0]->buffsize = 16384;
2332 rx_ring->fbr[0]->num_entries = 128;
d2796743
ME
2333 }
2334
09a3fc2b
ME
2335 adapter->rx_ring.psr_num_entries =
2336 adapter->rx_ring.fbr[1]->num_entries +
2337 adapter->rx_ring.fbr[0]->num_entries;
d2796743 2338
788ca84a
ME
2339 for (id = 0; id < NUM_FBRS; id++) {
2340 /* Allocate an area of memory for Free Buffer Ring */
2341 bufsize = (sizeof(struct fbr_desc) *
2342 rx_ring->fbr[id]->num_entries) + 0xfff;
2343 rx_ring->fbr[id]->ring_virtaddr =
2344 dma_alloc_coherent(&adapter->pdev->dev,
0d1b7a84 2345 bufsize,
788ca84a 2346 &rx_ring->fbr[id]->ring_physaddr,
0d1b7a84 2347 GFP_KERNEL);
788ca84a 2348 if (!rx_ring->fbr[id]->ring_virtaddr) {
d2796743 2349 dev_err(&adapter->pdev->dev,
788ca84a 2350 "Cannot alloc memory for Free Buffer Ring %d\n", id);
d2796743
ME
2351 return -ENOMEM;
2352 }
2353
788ca84a 2354 /* Align Free Buffer Ring on a 4K boundary */
d2796743 2355 et131x_align_allocated_memory(adapter,
788ca84a
ME
2356 &rx_ring->fbr[id]->ring_physaddr,
2357 &rx_ring->fbr[id]->offset, 0x0FFF);
d2796743 2358
788ca84a
ME
2359 rx_ring->fbr[id]->ring_virtaddr =
2360 (void *)((u8 *) rx_ring->fbr[id]->ring_virtaddr +
2361 rx_ring->fbr[id]->offset);
d2796743
ME
2362 }
2363
788ca84a
ME
2364 for (id = 0; id < NUM_FBRS; id++) {
2365 for (i = 0; i < (rx_ring->fbr[id]->num_entries / FBR_CHUNKS); i++) {
2366 dma_addr_t fbr_tmp_physaddr;
2367 dma_addr_t fbr_offset;
2368 u32 fbr_align;
2369
2370 /* This code allocates an area of memory big enough for
2371 * N free buffers + (buffer_size - 1) so that the
2372 * buffers can be aligned on 4k boundaries. If each
2373 * buffer were aligned to a buffer_size boundary, the
2374 * effect would be to double the size of FBR0. By
2375 * allocating N buffers at once, we reduce this overhead
2376 */
2377 if (id == 0 && rx_ring->fbr[id]->buffsize > 4096)
2378 fbr_align = 4096;
2379 else
2380 fbr_align = rx_ring->fbr[id]->buffsize;
d2796743 2381
788ca84a
ME
2382 fbr_chunksize = (FBR_CHUNKS *
2383 rx_ring->fbr[id]->buffsize) + fbr_align - 1;
2384 rx_ring->fbr[id]->mem_virtaddrs[i] = dma_alloc_coherent(
2385 &adapter->pdev->dev, fbr_chunksize,
2386 &rx_ring->fbr[id]->mem_physaddrs[i],
2387 GFP_KERNEL);
d2796743 2388
788ca84a
ME
2389 if (!rx_ring->fbr[id]->mem_virtaddrs[i]) {
2390 dev_err(&adapter->pdev->dev,
2391 "Could not alloc memory\n");
2392 return -ENOMEM;
2393 }
d2796743 2394
788ca84a
ME
2395 /* See NOTE in "Save Physical Address" comment above */
2396 fbr_tmp_physaddr = rx_ring->fbr[id]->mem_physaddrs[i];
2397
2398 et131x_align_allocated_memory(adapter,
2399 &fbr_tmp_physaddr,
2400 &fbr_offset,
2401 (fbr_align - 1));
2402
2403 for (j = 0; j < FBR_CHUNKS; j++) {
2404 u32 index = (i * FBR_CHUNKS) + j;
2405
2406 /* Save the Virtual address of this index for
2407 * quick access later
2408 */
2409 rx_ring->fbr[id]->virt[index] =
2410 (u8 *) rx_ring->fbr[id]->mem_virtaddrs[i] +
2411 (j * rx_ring->fbr[id]->buffsize) + fbr_offset;
2412
2413 /* now store the physical address in the
2414 * descriptor so the device can access it
2415 */
2416 rx_ring->fbr[id]->bus_high[index] =
2417 upper_32_bits(fbr_tmp_physaddr);
2418 rx_ring->fbr[id]->bus_low[index] =
2419 lower_32_bits(fbr_tmp_physaddr);
2420
2421 fbr_tmp_physaddr += rx_ring->fbr[id]->buffsize;
2422
2423 rx_ring->fbr[id]->buffer1[index] =
2424 rx_ring->fbr[id]->virt[index];
2425 rx_ring->fbr[id]->buffer2[index] =
2426 rx_ring->fbr[id]->virt[index] - 4;
2427 }
d2796743
ME
2428 }
2429 }
d2796743
ME
2430
2431 /* Allocate an area of memory for FIFO of Packet Status ring entries */
2432 pktstat_ringsize =
2433 sizeof(struct pkt_stat_desc) * adapter->rx_ring.psr_num_entries;
2434
0d1b7a84 2435 rx_ring->ps_ring_virtaddr = dma_alloc_coherent(&adapter->pdev->dev,
d2796743 2436 pktstat_ringsize,
0d1b7a84
ME
2437 &rx_ring->ps_ring_physaddr,
2438 GFP_KERNEL);
d2796743
ME
2439
2440 if (!rx_ring->ps_ring_virtaddr) {
2441 dev_err(&adapter->pdev->dev,
2442 "Cannot alloc memory for Packet Status Ring\n");
2443 return -ENOMEM;
2444 }
e58b89da
TY
2445 pr_info("Packet Status Ring %llx\n",
2446 (unsigned long long) rx_ring->ps_ring_physaddr);
d2796743
ME
2447
2448 /*
26dc751e 2449 * NOTE : dma_alloc_coherent(), used above to alloc DMA regions,
d2796743
ME
2450 * ALWAYS returns SAC (32-bit) addresses. If DAC (64-bit) addresses
2451 * are ever returned, make sure the high part is retrieved here before
2452 * storing the adjusted address.
2453 */
2454
2455 /* Allocate an area of memory for writeback of status information */
0d1b7a84 2456 rx_ring->rx_status_block = dma_alloc_coherent(&adapter->pdev->dev,
d2796743 2457 sizeof(struct rx_status_block),
0d1b7a84
ME
2458 &rx_ring->rx_status_bus,
2459 GFP_KERNEL);
d2796743
ME
2460 if (!rx_ring->rx_status_block) {
2461 dev_err(&adapter->pdev->dev,
2462 "Cannot alloc memory for Status Block\n");
2463 return -ENOMEM;
2464 }
2465 rx_ring->num_rfd = NIC_DEFAULT_NUM_RFD;
e58b89da 2466 pr_info("PRS %llx\n", (unsigned long long)rx_ring->rx_status_bus);
d2796743
ME
2467
2468 /* Recv
26dc751e 2469 * kmem_cache_create initializes a lookaside list. After successful
d2796743
ME
2470 * creation, nonpaged fixed-size blocks can be allocated from and
2471 * freed to the lookaside list.
2472 * RFDs will be allocated from this pool.
2473 */
2474 rx_ring->recv_lookaside = kmem_cache_create(adapter->netdev->name,
2475 sizeof(struct rfd),
2476 0,
2477 SLAB_CACHE_DMA |
2478 SLAB_HWCACHE_ALIGN,
2479 NULL);
2480
2481 adapter->flags |= fMP_ADAPTER_RECV_LOOKASIDE;
2482
2483 /* The RFDs are going to be put on lists later on, so initialize the
2484 * lists now.
2485 */
2486 INIT_LIST_HEAD(&rx_ring->recv_list);
2487 return 0;
2488}
2489
2490/**
2491 * et131x_rx_dma_memory_free - Free all memory allocated within this module.
2492 * @adapter: pointer to our private adapter structure
2493 */
eb7a6ca6 2494static void et131x_rx_dma_memory_free(struct et131x_adapter *adapter)
d2796743 2495{
788ca84a 2496 u8 id;
d2796743
ME
2497 u32 index;
2498 u32 bufsize;
2499 u32 pktstat_ringsize;
2500 struct rfd *rfd;
2501 struct rx_ring *rx_ring;
2502
2503 /* Setup some convenience pointers */
2504 rx_ring = &adapter->rx_ring;
2505
2506 /* Free RFDs and associated packet descriptors */
2507 WARN_ON(rx_ring->num_ready_recv != rx_ring->num_rfd);
2508
2509 while (!list_empty(&rx_ring->recv_list)) {
2510 rfd = (struct rfd *) list_entry(rx_ring->recv_list.next,
2511 struct rfd, list_node);
2512
2513 list_del(&rfd->list_node);
2514 rfd->skb = NULL;
2515 kmem_cache_free(adapter->rx_ring.recv_lookaside, rfd);
2516 }
2517
788ca84a
ME
2518 /* Free Free Buffer Rings */
2519 for (id = 0; id < NUM_FBRS; id++) {
2520 if (rx_ring->fbr[id]->ring_virtaddr) {
2521 /* First the packet memory */
2522 for (index = 0; index <
2523 (rx_ring->fbr[id]->num_entries / FBR_CHUNKS);
2524 index++) {
2525 if (rx_ring->fbr[id]->mem_virtaddrs[index]) {
2526 u32 fbr_align;
2527
2528 if (rx_ring->fbr[id]->buffsize > 4096)
2529 fbr_align = 4096;
2530 else
2531 fbr_align = rx_ring->fbr[id]->buffsize;
2532
2533 bufsize =
2534 (rx_ring->fbr[id]->buffsize * FBR_CHUNKS) +
2535 fbr_align - 1;
2536
2537 dma_free_coherent(&adapter->pdev->dev,
2538 bufsize,
2539 rx_ring->fbr[id]->mem_virtaddrs[index],
2540 rx_ring->fbr[id]->mem_physaddrs[index]);
d2796743 2541
788ca84a
ME
2542 rx_ring->fbr[id]->mem_virtaddrs[index] = NULL;
2543 }
d2796743 2544 }
d2796743 2545
788ca84a
ME
2546 /* Now the FIFO itself */
2547 rx_ring->fbr[id]->ring_virtaddr = (void *)((u8 *)
2548 rx_ring->fbr[id]->ring_virtaddr - rx_ring->fbr[id]->offset);
d2796743 2549
788ca84a
ME
2550 bufsize =
2551 (sizeof(struct fbr_desc) * rx_ring->fbr[id]->num_entries) +
2552 0xfff;
d2796743 2553
788ca84a
ME
2554 dma_free_coherent(&adapter->pdev->dev, bufsize,
2555 rx_ring->fbr[id]->ring_virtaddr,
2556 rx_ring->fbr[id]->ring_physaddr);
d2796743 2557
788ca84a 2558 rx_ring->fbr[id]->ring_virtaddr = NULL;
d2796743 2559 }
d2796743 2560 }
d2796743
ME
2561
2562 /* Free Packet Status Ring */
2563 if (rx_ring->ps_ring_virtaddr) {
242187aa
ME
2564 pktstat_ringsize = sizeof(struct pkt_stat_desc) *
2565 adapter->rx_ring.psr_num_entries;
d2796743 2566
675c8f68 2567 dma_free_coherent(&adapter->pdev->dev, pktstat_ringsize,
d2796743
ME
2568 rx_ring->ps_ring_virtaddr,
2569 rx_ring->ps_ring_physaddr);
2570
2571 rx_ring->ps_ring_virtaddr = NULL;
2572 }
2573
2574 /* Free area of memory for the writeback of status information */
2575 if (rx_ring->rx_status_block) {
675c8f68 2576 dma_free_coherent(&adapter->pdev->dev,
d2796743
ME
2577 sizeof(struct rx_status_block),
2578 rx_ring->rx_status_block, rx_ring->rx_status_bus);
2579 rx_ring->rx_status_block = NULL;
2580 }
2581
d2796743
ME
2582 /* Destroy the lookaside (RFD) pool */
2583 if (adapter->flags & fMP_ADAPTER_RECV_LOOKASIDE) {
2584 kmem_cache_destroy(rx_ring->recv_lookaside);
2585 adapter->flags &= ~fMP_ADAPTER_RECV_LOOKASIDE;
2586 }
2587
2588 /* Free the FBR Lookup Table */
e592a9b0 2589 kfree(rx_ring->fbr[1]);
e592a9b0 2590 kfree(rx_ring->fbr[0]);
d2796743
ME
2591
2592 /* Reset Counters */
2593 rx_ring->num_ready_recv = 0;
2594}
2595
2596/**
2597 * et131x_init_recv - Initialize receive data structures.
2598 * @adapter: pointer to our private adapter structure
2599 *
2600 * Returns 0 on success and errno on failure (as defined in errno.h)
2601 */
eb7a6ca6 2602static int et131x_init_recv(struct et131x_adapter *adapter)
d2796743
ME
2603{
2604 int status = -ENOMEM;
2605 struct rfd *rfd = NULL;
2606 u32 rfdct;
2607 u32 numrfd = 0;
2608 struct rx_ring *rx_ring;
2609
2610 /* Setup some convenience pointers */
2611 rx_ring = &adapter->rx_ring;
2612
2613 /* Setup each RFD */
2614 for (rfdct = 0; rfdct < rx_ring->num_rfd; rfdct++) {
2615 rfd = kmem_cache_alloc(rx_ring->recv_lookaside,
2616 GFP_ATOMIC | GFP_DMA);
2617
2618 if (!rfd) {
2619 dev_err(&adapter->pdev->dev,
2620 "Couldn't alloc RFD out of kmem_cache\n");
2621 status = -ENOMEM;
2622 continue;
2623 }
2624
2625 rfd->skb = NULL;
2626
2627 /* Add this RFD to the recv_list */
2628 list_add_tail(&rfd->list_node, &rx_ring->recv_list);
2629
2630 /* Increment both the available RFD's, and the total RFD's. */
2631 rx_ring->num_ready_recv++;
2632 numrfd++;
2633 }
2634
2635 if (numrfd > NIC_MIN_NUM_RFD)
2636 status = 0;
2637
2638 rx_ring->num_rfd = numrfd;
2639
2640 if (status != 0) {
2641 kmem_cache_free(rx_ring->recv_lookaside, rfd);
2642 dev_err(&adapter->pdev->dev,
2643 "Allocation problems in et131x_init_recv\n");
2644 }
2645 return status;
2646}
2647
d2796743
ME
2648/**
2649 * et131x_set_rx_dma_timer - Set the heartbeat timer according to line rate.
2650 * @adapter: pointer to our adapter structure
2651 */
eb7a6ca6 2652static void et131x_set_rx_dma_timer(struct et131x_adapter *adapter)
d2796743
ME
2653{
2654 struct phy_device *phydev = adapter->phydev;
2655
2656 if (!phydev)
2657 return;
2658
2659 /* For version B silicon, we do not use the RxDMA timer for 10 and 100
2660 * Mbits/s line rates. We do not enable and RxDMA interrupt coalescing.
2661 */
2662 if ((phydev->speed == SPEED_100) || (phydev->speed == SPEED_10)) {
2663 writel(0, &adapter->regs->rxdma.max_pkt_time);
2664 writel(1, &adapter->regs->rxdma.num_pkt_done);
2665 }
2666}
2667
2668/**
2669 * NICReturnRFD - Recycle a RFD and put it back onto the receive list
2670 * @adapter: pointer to our adapter
2671 * @rfd: pointer to the RFD
2672 */
2673static void nic_return_rfd(struct et131x_adapter *adapter, struct rfd *rfd)
2674{
2675 struct rx_ring *rx_local = &adapter->rx_ring;
2676 struct rxdma_regs __iomem *rx_dma = &adapter->regs->rxdma;
2677 u16 buff_index = rfd->bufferindex;
2678 u8 ring_index = rfd->ringindex;
2679 unsigned long flags;
2680
2681 /* We don't use any of the OOB data besides status. Otherwise, we
2682 * need to clean up OOB data
2683 */
186c426d 2684 if ((ring_index == 0 && buff_index < rx_local->fbr[1]->num_entries) ||
e592a9b0 2685 (ring_index == 1 && buff_index < rx_local->fbr[0]->num_entries)) {
788ca84a
ME
2686 u32 *offset;
2687 u8 id;
2688 struct fbr_desc *next;
2689
d2796743
ME
2690 spin_lock_irqsave(&adapter->fbr_lock, flags);
2691
2692 if (ring_index == 1) {
788ca84a
ME
2693 id = 0;
2694 offset = &rx_dma->fbr1_full_offset;
b5254867 2695 } else {
788ca84a
ME
2696 id = 1;
2697 offset = &rx_dma->fbr0_full_offset;
d2796743 2698 }
788ca84a
ME
2699
2700 next = (struct fbr_desc *) (rx_local->fbr[id]->ring_virtaddr) +
2701 INDEX10(rx_local->fbr[id]->local_full);
2702
2703 /* Handle the Free Buffer Ring advancement here. Write
2704 * the PA / Buffer Index for the returned buffer into
2705 * the oldest (next to be freed)FBR entry
2706 */
2707 next->addr_hi = rx_local->fbr[id]->bus_high[buff_index];
2708 next->addr_lo = rx_local->fbr[id]->bus_low[buff_index];
2709 next->word2 = buff_index;
2710
186c426d
ME
2711 writel(bump_free_buff_ring(&rx_local->fbr[id]->local_full,
2712 rx_local->fbr[id]->num_entries - 1),
2713 offset);
788ca84a 2714
d2796743
ME
2715 spin_unlock_irqrestore(&adapter->fbr_lock, flags);
2716 } else {
2717 dev_err(&adapter->pdev->dev,
2718 "%s illegal Buffer Index returned\n", __func__);
2719 }
2720
2721 /* The processing on this RFD is done, so put it back on the tail of
2722 * our list
2723 */
2724 spin_lock_irqsave(&adapter->rcv_lock, flags);
2725 list_add_tail(&rfd->list_node, &rx_local->recv_list);
2726 rx_local->num_ready_recv++;
2727 spin_unlock_irqrestore(&adapter->rcv_lock, flags);
2728
2729 WARN_ON(rx_local->num_ready_recv > rx_local->num_rfd);
2730}
2731
54dbf04f
ME
2732/**
2733 * nic_rx_pkts - Checks the hardware for available packets
2734 * @adapter: pointer to our adapter
2735 *
2736 * Returns rfd, a pointer to our MPRFD.
2737 *
2738 * Checks the hardware for available packets, using completion ring
2739 * If packets are available, it gets an RFD from the recv_list, attaches
2740 * the packet to it, puts the RFD in the RecvPendList, and also returns
2741 * the pointer to the RFD.
2742 */
d2796743
ME
2743static struct rfd *nic_rx_pkts(struct et131x_adapter *adapter)
2744{
2745 struct rx_ring *rx_local = &adapter->rx_ring;
2746 struct rx_status_block *status;
2747 struct pkt_stat_desc *psr;
186c426d 2748 struct rfd *rfd;
d2796743
ME
2749 u32 i;
2750 u8 *buf;
2751 unsigned long flags;
2752 struct list_head *element;
2753 u8 ring_index;
2754 u16 buff_index;
2755 u32 len;
2756 u32 word0;
2757 u32 word1;
186c426d 2758 struct sk_buff *skb;
d2796743
ME
2759
2760 /* RX Status block is written by the DMA engine prior to every
2761 * interrupt. It contains the next to be used entry in the Packet
2762 * Status Ring, and also the two Free Buffer rings.
2763 */
2764 status = rx_local->rx_status_block;
2765 word1 = status->word1 >> 16; /* Get the useful bits */
2766
2767 /* Check the PSR and wrap bits do not match */
2768 if ((word1 & 0x1FFF) == (rx_local->local_psr_full & 0x1FFF))
242187aa 2769 return NULL; /* Looks like this ring is not updated yet */
d2796743
ME
2770
2771 /* The packet status ring indicates that data is available. */
2772 psr = (struct pkt_stat_desc *) (rx_local->ps_ring_virtaddr) +
2773 (rx_local->local_psr_full & 0xFFF);
2774
242187aa
ME
2775 /* Grab any information that is required once the PSR is advanced,
2776 * since we can no longer rely on the memory being accurate
d2796743
ME
2777 */
2778 len = psr->word1 & 0xFFFF;
2779 ring_index = (psr->word1 >> 26) & 0x03;
2780 buff_index = (psr->word1 >> 16) & 0x3FF;
2781 word0 = psr->word0;
2782
2783 /* Indicate that we have used this PSR entry. */
2784 /* FIXME wrap 12 */
2785 add_12bit(&rx_local->local_psr_full, 1);
2786 if (
2787 (rx_local->local_psr_full & 0xFFF) > rx_local->psr_num_entries - 1) {
2788 /* Clear psr full and toggle the wrap bit */
2789 rx_local->local_psr_full &= ~0xFFF;
2790 rx_local->local_psr_full ^= 0x1000;
2791 }
2792
242187aa 2793 writel(rx_local->local_psr_full, &adapter->regs->rxdma.psr_full_offset);
d2796743 2794
d2796743 2795 if (ring_index > 1 ||
b5254867
ME
2796 (ring_index == 0 &&
2797 buff_index > rx_local->fbr[1]->num_entries - 1) ||
2798 (ring_index == 1 &&
2799 buff_index > rx_local->fbr[0]->num_entries - 1)) {
d2796743
ME
2800 /* Illegal buffer or ring index cannot be used by S/W*/
2801 dev_err(&adapter->pdev->dev,
242187aa
ME
2802 "NICRxPkts PSR Entry %d indicates length of %d and/or bad bi(%d)\n",
2803 rx_local->local_psr_full & 0xFFF, len, buff_index);
d2796743
ME
2804 return NULL;
2805 }
2806
2807 /* Get and fill the RFD. */
2808 spin_lock_irqsave(&adapter->rcv_lock, flags);
2809
d2796743
ME
2810 element = rx_local->recv_list.next;
2811 rfd = (struct rfd *) list_entry(element, struct rfd, list_node);
2812
242187aa 2813 if (!rfd) {
d2796743
ME
2814 spin_unlock_irqrestore(&adapter->rcv_lock, flags);
2815 return NULL;
2816 }
2817
2818 list_del(&rfd->list_node);
2819 rx_local->num_ready_recv--;
2820
2821 spin_unlock_irqrestore(&adapter->rcv_lock, flags);
2822
2823 rfd->bufferindex = buff_index;
2824 rfd->ringindex = ring_index;
2825
242187aa
ME
2826 /* In V1 silicon, there is a bug which screws up filtering of runt
2827 * packets. Therefore runt packet filtering is disabled in the MAC and
2828 * the packets are dropped here. They are also counted here.
d2796743
ME
2829 */
2830 if (len < (NIC_MIN_PACKET_SIZE + 4)) {
2831 adapter->stats.rx_other_errs++;
2832 len = 0;
2833 }
2834
242187aa
ME
2835 if (len == 0) {
2836 rfd->len = 0;
2837 goto out;
2838 }
2839
2840 /* Determine if this is a multicast packet coming in */
2841 if ((word0 & ALCATEL_MULTICAST_PKT) &&
2842 !(word0 & ALCATEL_BROADCAST_PKT)) {
2843 /* Promiscuous mode and Multicast mode are not mutually
2844 * exclusive as was first thought. I guess Promiscuous is just
2845 * considered a super-set of the other filters. Generally filter
2846 * is 0x2b when in promiscuous mode.
2847 */
2848 if ((adapter->packet_filter & ET131X_PACKET_TYPE_MULTICAST)
2849 && !(adapter->packet_filter & ET131X_PACKET_TYPE_PROMISCUOUS)
2850 && !(adapter->packet_filter &
d2796743 2851 ET131X_PACKET_TYPE_ALL_MULTICAST)) {
242187aa
ME
2852 /*
2853 * Note - ring_index for fbr[] array is reversed
2854 * 1 for FBR0 etc
2855 */
2856 buf = rx_local->fbr[(ring_index == 0 ? 1 : 0)]->
2857 virt[buff_index];
d2796743 2858
242187aa
ME
2859 /* Loop through our list to see if the destination
2860 * address of this packet matches one in our list.
2861 */
2862 for (i = 0; i < adapter->multicast_addr_count; i++) {
2863 if (buf[0] == adapter->multicast_list[i][0]
2864 && buf[1] == adapter->multicast_list[i][1]
2865 && buf[2] == adapter->multicast_list[i][2]
2866 && buf[3] == adapter->multicast_list[i][3]
2867 && buf[4] == adapter->multicast_list[i][4]
2868 && buf[5] == adapter->multicast_list[i][5]) {
2869 break;
d2796743 2870 }
d2796743
ME
2871 }
2872
242187aa
ME
2873 /* If our index is equal to the number of Multicast
2874 * address we have, then this means we did not find this
2875 * packet's matching address in our list. Set the len to
2876 * zero, so we free our RFD when we return from this
2877 * function.
d2796743 2878 */
242187aa
ME
2879 if (i == adapter->multicast_addr_count)
2880 len = 0;
2881 }
d2796743 2882
242187aa
ME
2883 if (len > 0)
2884 adapter->stats.multicast_pkts_rcvd++;
2885 } else if (word0 & ALCATEL_BROADCAST_PKT) {
2886 adapter->stats.broadcast_pkts_rcvd++;
2887 } else {
2888 /* Not sure what this counter measures in promiscuous mode.
2889 * Perhaps we should check the MAC address to see if it is
2890 * directed to us in promiscuous mode.
2891 */
2892 adapter->stats.unicast_pkts_rcvd++;
2893 }
d2796743 2894
242187aa
ME
2895 if (len == 0) {
2896 rfd->len = 0;
2897 goto out;
2898 }
d2796743 2899
242187aa 2900 rfd->len = len;
d2796743 2901
242187aa
ME
2902 skb = dev_alloc_skb(rfd->len + 2);
2903 if (!skb) {
2904 dev_err(&adapter->pdev->dev, "Couldn't alloc an SKB for Rx\n");
2905 return NULL;
2906 }
d2796743 2907
242187aa 2908 adapter->net_stats.rx_bytes += rfd->len;
d2796743 2909
242187aa
ME
2910 /* Note - ring_index for fbr[] array is reversed, 1 for FBR0 etc */
2911 memcpy(skb_put(skb, rfd->len),
2912 rx_local->fbr[(ring_index == 0 ? 1 : 0)]->virt[buff_index],
2913 rfd->len);
d2796743 2914
242187aa
ME
2915 skb->dev = adapter->netdev;
2916 skb->protocol = eth_type_trans(skb, adapter->netdev);
2917 skb->ip_summed = CHECKSUM_NONE;
2918 netif_rx_ni(skb);
d2796743 2919
242187aa 2920out:
d2796743
ME
2921 nic_return_rfd(adapter, rfd);
2922 return rfd;
2923}
2924
d2796743
ME
2925/**
2926 * et131x_handle_recv_interrupt - Interrupt handler for receive processing
2927 * @adapter: pointer to our adapter
2928 *
2929 * Assumption, Rcv spinlock has been acquired.
2930 */
eb7a6ca6 2931static void et131x_handle_recv_interrupt(struct et131x_adapter *adapter)
d2796743
ME
2932{
2933 struct rfd *rfd = NULL;
2934 u32 count = 0;
2935 bool done = true;
2936
2937 /* Process up to available RFD's */
2938 while (count < NUM_PACKETS_HANDLED) {
2939 if (list_empty(&adapter->rx_ring.recv_list)) {
2940 WARN_ON(adapter->rx_ring.num_ready_recv != 0);
2941 done = false;
2942 break;
2943 }
2944
2945 rfd = nic_rx_pkts(adapter);
2946
2947 if (rfd == NULL)
2948 break;
2949
2950 /* Do not receive any packets until a filter has been set.
2951 * Do not receive any packets until we have link.
2952 * If length is zero, return the RFD in order to advance the
2953 * Free buffer ring.
2954 */
2955 if (!adapter->packet_filter ||
2956 !netif_carrier_ok(adapter->netdev) ||
2957 rfd->len == 0)
2958 continue;
2959
2960 /* Increment the number of packets we received */
2961 adapter->net_stats.rx_packets++;
2962
2963 /* Set the status on the packet, either resources or success */
2964 if (adapter->rx_ring.num_ready_recv < RFD_LOW_WATER_MARK) {
2965 dev_warn(&adapter->pdev->dev,
2966 "RFD's are running out\n");
2967 }
2968 count++;
2969 }
2970
2971 if (count == NUM_PACKETS_HANDLED || !done) {
2972 adapter->rx_ring.unfinished_receives = true;
2973 writel(PARM_TX_TIME_INT_DEF * NANO_IN_A_MICRO,
2974 &adapter->regs->global.watchdog_timer);
2975 } else
2976 /* Watchdog timer will disable itself if appropriate. */
2977 adapter->rx_ring.unfinished_receives = false;
2978}
2979
d2796743
ME
2980/**
2981 * et131x_tx_dma_memory_alloc
2982 * @adapter: pointer to our private adapter structure
2983 *
2984 * Returns 0 on success and errno on failure (as defined in errno.h).
2985 *
2986 * Allocates memory that will be visible both to the device and to the CPU.
2987 * The OS will pass us packets, pointers to which we will insert in the Tx
2988 * Descriptor queue. The device will read this queue to find the packets in
2989 * memory. The device will update the "status" in memory each time it xmits a
2990 * packet.
2991 */
eb7a6ca6 2992static int et131x_tx_dma_memory_alloc(struct et131x_adapter *adapter)
d2796743
ME
2993{
2994 int desc_size = 0;
2995 struct tx_ring *tx_ring = &adapter->tx_ring;
2996
2997 /* Allocate memory for the TCB's (Transmit Control Block) */
2998 adapter->tx_ring.tcb_ring =
2999 kcalloc(NUM_TCB, sizeof(struct tcb), GFP_ATOMIC | GFP_DMA);
3000 if (!adapter->tx_ring.tcb_ring) {
3001 dev_err(&adapter->pdev->dev, "Cannot alloc memory for TCBs\n");
3002 return -ENOMEM;
3003 }
3004
3005 /* Allocate enough memory for the Tx descriptor ring, and allocate
3006 * some extra so that the ring can be aligned on a 4k boundary.
3007 */
3008 desc_size = (sizeof(struct tx_desc) * NUM_DESC_PER_RING_TX) + 4096 - 1;
3009 tx_ring->tx_desc_ring =
09a3fc2b
ME
3010 (struct tx_desc *) dma_alloc_coherent(&adapter->pdev->dev,
3011 desc_size,
3012 &tx_ring->tx_desc_ring_pa,
3013 GFP_KERNEL);
d2796743
ME
3014 if (!adapter->tx_ring.tx_desc_ring) {
3015 dev_err(&adapter->pdev->dev,
09a3fc2b 3016 "Cannot alloc memory for Tx Ring\n");
d2796743
ME
3017 return -ENOMEM;
3018 }
3019
3020 /* Save physical address
3021 *
26dc751e 3022 * NOTE: dma_alloc_coherent(), used above to alloc DMA regions,
d2796743
ME
3023 * ALWAYS returns SAC (32-bit) addresses. If DAC (64-bit) addresses
3024 * are ever returned, make sure the high part is retrieved here before
3025 * storing the adjusted address.
3026 */
3027 /* Allocate memory for the Tx status block */
0d1b7a84 3028 tx_ring->tx_status = dma_alloc_coherent(&adapter->pdev->dev,
d2796743 3029 sizeof(u32),
0d1b7a84
ME
3030 &tx_ring->tx_status_pa,
3031 GFP_KERNEL);
d2796743
ME
3032 if (!adapter->tx_ring.tx_status_pa) {
3033 dev_err(&adapter->pdev->dev,
3034 "Cannot alloc memory for Tx status block\n");
3035 return -ENOMEM;
3036 }
3037 return 0;
3038}
3039
3040/**
3041 * et131x_tx_dma_memory_free - Free all memory allocated within this module
3042 * @adapter: pointer to our private adapter structure
3043 *
3044 * Returns 0 on success and errno on failure (as defined in errno.h).
3045 */
eb7a6ca6 3046static void et131x_tx_dma_memory_free(struct et131x_adapter *adapter)
d2796743
ME
3047{
3048 int desc_size = 0;
3049
3050 if (adapter->tx_ring.tx_desc_ring) {
3051 /* Free memory relating to Tx rings here */
3052 desc_size = (sizeof(struct tx_desc) * NUM_DESC_PER_RING_TX)
3053 + 4096 - 1;
675c8f68 3054 dma_free_coherent(&adapter->pdev->dev,
d2796743
ME
3055 desc_size,
3056 adapter->tx_ring.tx_desc_ring,
3057 adapter->tx_ring.tx_desc_ring_pa);
3058 adapter->tx_ring.tx_desc_ring = NULL;
3059 }
3060
3061 /* Free memory for the Tx status block */
3062 if (adapter->tx_ring.tx_status) {
675c8f68 3063 dma_free_coherent(&adapter->pdev->dev,
d2796743
ME
3064 sizeof(u32),
3065 adapter->tx_ring.tx_status,
3066 adapter->tx_ring.tx_status_pa);
3067
3068 adapter->tx_ring.tx_status = NULL;
3069 }
3070 /* Free the memory for the tcb structures */
3071 kfree(adapter->tx_ring.tcb_ring);
3072}
3073
d2796743
ME
3074/**
3075 * nic_send_packet - NIC specific send handler for version B silicon.
3076 * @adapter: pointer to our adapter
3077 * @tcb: pointer to struct tcb
3078 *
3079 * Returns 0 or errno.
3080 */
3081static int nic_send_packet(struct et131x_adapter *adapter, struct tcb *tcb)
3082{
3083 u32 i;
3084 struct tx_desc desc[24]; /* 24 x 16 byte */
3085 u32 frag = 0;
3086 u32 thiscopy, remainder;
3087 struct sk_buff *skb = tcb->skb;
3088 u32 nr_frags = skb_shinfo(skb)->nr_frags + 1;
3089 struct skb_frag_struct *frags = &skb_shinfo(skb)->frags[0];
3090 unsigned long flags;
3091 struct phy_device *phydev = adapter->phydev;
998f6dfb 3092 dma_addr_t dma_addr;
d2796743
ME
3093
3094 /* Part of the optimizations of this send routine restrict us to
3095 * sending 24 fragments at a pass. In practice we should never see
3096 * more than 5 fragments.
3097 *
3098 * NOTE: The older version of this function (below) can handle any
3099 * number of fragments. If needed, we can call this function,
3100 * although it is less efficient.
3101 */
3102 if (nr_frags > 23)
3103 return -EIO;
3104
3105 memset(desc, 0, sizeof(struct tx_desc) * (nr_frags + 1));
3106
3107 for (i = 0; i < nr_frags; i++) {
3108 /* If there is something in this element, lets get a
3109 * descriptor from the ring and get the necessary data
3110 */
3111 if (i == 0) {
3112 /* If the fragments are smaller than a standard MTU,
3113 * then map them to a single descriptor in the Tx
3114 * Desc ring. However, if they're larger, as is
3115 * possible with support for jumbo packets, then
3116 * split them each across 2 descriptors.
3117 *
3118 * This will work until we determine why the hardware
3119 * doesn't seem to like large fragments.
3120 */
f1b540b9 3121 if (skb_headlen(skb) <= 1514) {
d2796743
ME
3122 /* Low 16bits are length, high is vlan and
3123 unused currently so zero */
f1b540b9 3124 desc[frag].len_vlan = skb_headlen(skb);
998f6dfb
ME
3125 dma_addr = dma_map_single(&adapter->pdev->dev,
3126 skb->data,
3127 skb_headlen(skb),
3128 DMA_TO_DEVICE);
3129 desc[frag].addr_lo = lower_32_bits(dma_addr);
3130 desc[frag].addr_hi = upper_32_bits(dma_addr);
3131 frag++;
d2796743 3132 } else {
f1b540b9 3133 desc[frag].len_vlan = skb_headlen(skb) / 2;
998f6dfb
ME
3134 dma_addr = dma_map_single(&adapter->pdev->dev,
3135 skb->data,
3136 (skb_headlen(skb) / 2),
3137 DMA_TO_DEVICE);
3138 desc[frag].addr_lo = lower_32_bits(dma_addr);
3139 desc[frag].addr_hi = upper_32_bits(dma_addr);
3140 frag++;
d2796743 3141
f1b540b9 3142 desc[frag].len_vlan = skb_headlen(skb) / 2;
998f6dfb
ME
3143 dma_addr = dma_map_single(&adapter->pdev->dev,
3144 skb->data +
3145 (skb_headlen(skb) / 2),
3146 (skb_headlen(skb) / 2),
3147 DMA_TO_DEVICE);
3148 desc[frag].addr_lo = lower_32_bits(dma_addr);
3149 desc[frag].addr_hi = upper_32_bits(dma_addr);
3150 frag++;
d2796743
ME
3151 }
3152 } else {
998f6dfb
ME
3153 desc[frag].len_vlan = frags[i - 1].size;
3154 dma_addr = skb_frag_dma_map(&adapter->pdev->dev,
3155 &frags[i - 1],
3156 0,
3157 frags[i - 1].size,
3158 DMA_TO_DEVICE);
3159 desc[frag].addr_lo = lower_32_bits(dma_addr);
3160 desc[frag].addr_hi = upper_32_bits(dma_addr);
3161 frag++;
d2796743
ME
3162 }
3163 }
3164
d2796743
ME
3165 if (phydev && phydev->speed == SPEED_1000) {
3166 if (++adapter->tx_ring.since_irq == PARM_TX_NUM_BUFS_DEF) {
3167 /* Last element & Interrupt flag */
3168 desc[frag - 1].flags = 0x5;
3169 adapter->tx_ring.since_irq = 0;
3170 } else { /* Last element */
3171 desc[frag - 1].flags = 0x1;
3172 }
3173 } else
3174 desc[frag - 1].flags = 0x5;
3175
3176 desc[0].flags |= 2; /* First element flag */
3177
3178 tcb->index_start = adapter->tx_ring.send_idx;
3179 tcb->stale = 0;
3180
3181 spin_lock_irqsave(&adapter->send_hw_lock, flags);
3182
3183 thiscopy = NUM_DESC_PER_RING_TX -
3184 INDEX10(adapter->tx_ring.send_idx);
3185
3186 if (thiscopy >= frag) {
3187 remainder = 0;
3188 thiscopy = frag;
3189 } else {
3190 remainder = frag - thiscopy;
3191 }
3192
3193 memcpy(adapter->tx_ring.tx_desc_ring +
3194 INDEX10(adapter->tx_ring.send_idx), desc,
3195 sizeof(struct tx_desc) * thiscopy);
3196
3197 add_10bit(&adapter->tx_ring.send_idx, thiscopy);
3198
3199 if (INDEX10(adapter->tx_ring.send_idx) == 0 ||
3200 INDEX10(adapter->tx_ring.send_idx) == NUM_DESC_PER_RING_TX) {
3201 adapter->tx_ring.send_idx &= ~ET_DMA10_MASK;
3202 adapter->tx_ring.send_idx ^= ET_DMA10_WRAP;
3203 }
3204
3205 if (remainder) {
3206 memcpy(adapter->tx_ring.tx_desc_ring,
3207 desc + thiscopy,
3208 sizeof(struct tx_desc) * remainder);
3209
3210 add_10bit(&adapter->tx_ring.send_idx, remainder);
3211 }
3212
3213 if (INDEX10(adapter->tx_ring.send_idx) == 0) {
3214 if (adapter->tx_ring.send_idx)
3215 tcb->index = NUM_DESC_PER_RING_TX - 1;
3216 else
3217 tcb->index = ET_DMA10_WRAP|(NUM_DESC_PER_RING_TX - 1);
3218 } else
3219 tcb->index = adapter->tx_ring.send_idx - 1;
3220
3221 spin_lock(&adapter->tcb_send_qlock);
3222
3223 if (adapter->tx_ring.send_tail)
3224 adapter->tx_ring.send_tail->next = tcb;
3225 else
3226 adapter->tx_ring.send_head = tcb;
3227
3228 adapter->tx_ring.send_tail = tcb;
3229
3230 WARN_ON(tcb->next != NULL);
3231
3232 adapter->tx_ring.used++;
3233
3234 spin_unlock(&adapter->tcb_send_qlock);
3235
3236 /* Write the new write pointer back to the device. */
3237 writel(adapter->tx_ring.send_idx,
3238 &adapter->regs->txdma.service_request);
3239
3240 /* For Gig only, we use Tx Interrupt coalescing. Enable the software
3241 * timer to wake us up if this packet isn't followed by N more.
3242 */
3243 if (phydev && phydev->speed == SPEED_1000) {
3244 writel(PARM_TX_TIME_INT_DEF * NANO_IN_A_MICRO,
3245 &adapter->regs->global.watchdog_timer);
3246 }
3247 spin_unlock_irqrestore(&adapter->send_hw_lock, flags);
3248
3249 return 0;
3250}
3251
3252/**
3253 * send_packet - Do the work to send a packet
3254 * @skb: the packet(s) to send
3255 * @adapter: a pointer to the device's private adapter structure
3256 *
3257 * Return 0 in almost all cases; non-zero value in extreme hard failure only.
3258 *
3259 * Assumption: Send spinlock has been acquired
3260 */
3261static int send_packet(struct sk_buff *skb, struct et131x_adapter *adapter)
3262{
3263 int status;
3264 struct tcb *tcb = NULL;
3265 u16 *shbufva;
3266 unsigned long flags;
3267
3268 /* All packets must have at least a MAC address and a protocol type */
3269 if (skb->len < ETH_HLEN)
3270 return -EIO;
3271
3272 /* Get a TCB for this packet */
3273 spin_lock_irqsave(&adapter->tcb_ready_qlock, flags);
3274
3275 tcb = adapter->tx_ring.tcb_qhead;
3276
3277 if (tcb == NULL) {
3278 spin_unlock_irqrestore(&adapter->tcb_ready_qlock, flags);
3279 return -ENOMEM;
3280 }
3281
3282 adapter->tx_ring.tcb_qhead = tcb->next;
3283
3284 if (adapter->tx_ring.tcb_qhead == NULL)
3285 adapter->tx_ring.tcb_qtail = NULL;
3286
3287 spin_unlock_irqrestore(&adapter->tcb_ready_qlock, flags);
3288
3289 tcb->skb = skb;
3290
f1b540b9 3291 if (skb->data != NULL && skb_headlen(skb) >= 6) {
d2796743
ME
3292 shbufva = (u16 *) skb->data;
3293
3294 if ((shbufva[0] == 0xffff) &&
3295 (shbufva[1] == 0xffff) && (shbufva[2] == 0xffff)) {
3296 tcb->flags |= fMP_DEST_BROAD;
3297 } else if ((shbufva[0] & 0x3) == 0x0001) {
3298 tcb->flags |= fMP_DEST_MULTI;
3299 }
3300 }
3301
3302 tcb->next = NULL;
3303
3304 /* Call the NIC specific send handler. */
3305 status = nic_send_packet(adapter, tcb);
3306
3307 if (status != 0) {
3308 spin_lock_irqsave(&adapter->tcb_ready_qlock, flags);
3309
3310 if (adapter->tx_ring.tcb_qtail)
3311 adapter->tx_ring.tcb_qtail->next = tcb;
3312 else
3313 /* Apparently ready Q is empty. */
3314 adapter->tx_ring.tcb_qhead = tcb;
3315
3316 adapter->tx_ring.tcb_qtail = tcb;
3317 spin_unlock_irqrestore(&adapter->tcb_ready_qlock, flags);
3318 return status;
3319 }
3320 WARN_ON(adapter->tx_ring.used > NUM_TCB);
3321 return 0;
3322}
3323
3324/**
3325 * et131x_send_packets - This function is called by the OS to send packets
3326 * @skb: the packet(s) to send
3327 * @netdev:device on which to TX the above packet(s)
3328 *
3329 * Return 0 in almost all cases; non-zero value in extreme hard failure only
3330 */
eb7a6ca6 3331static int et131x_send_packets(struct sk_buff *skb, struct net_device *netdev)
d2796743
ME
3332{
3333 int status = 0;
06709e96 3334 struct et131x_adapter *adapter = netdev_priv(netdev);
d2796743
ME
3335
3336 /* Send these packets
3337 *
3338 * NOTE: The Linux Tx entry point is only given one packet at a time
3339 * to Tx, so the PacketCount and it's array used makes no sense here
3340 */
3341
3342 /* TCB is not available */
3343 if (adapter->tx_ring.used >= NUM_TCB) {
3344 /* NOTE: If there's an error on send, no need to queue the
3345 * packet under Linux; if we just send an error up to the
3346 * netif layer, it will resend the skb to us.
3347 */
3348 status = -ENOMEM;
3349 } else {
3350 /* We need to see if the link is up; if it's not, make the
3351 * netif layer think we're good and drop the packet
3352 */
3353 if ((adapter->flags & fMP_ADAPTER_FAIL_SEND_MASK) ||
3354 !netif_carrier_ok(netdev)) {
3355 dev_kfree_skb_any(skb);
3356 skb = NULL;
3357
3358 adapter->net_stats.tx_dropped++;
3359 } else {
3360 status = send_packet(skb, adapter);
3361 if (status != 0 && status != -ENOMEM) {
3362 /* On any other error, make netif think we're
3363 * OK and drop the packet
3364 */
3365 dev_kfree_skb_any(skb);
3366 skb = NULL;
3367 adapter->net_stats.tx_dropped++;
3368 }
3369 }
3370 }
3371 return status;
3372}
3373
3374/**
3375 * free_send_packet - Recycle a struct tcb
3376 * @adapter: pointer to our adapter
3377 * @tcb: pointer to struct tcb
3378 *
3379 * Complete the packet if necessary
3380 * Assumption - Send spinlock has been acquired
3381 */
3382static inline void free_send_packet(struct et131x_adapter *adapter,
3383 struct tcb *tcb)
3384{
3385 unsigned long flags;
3386 struct tx_desc *desc = NULL;
3387 struct net_device_stats *stats = &adapter->net_stats;
983e4b35 3388 u64 dma_addr;
d2796743
ME
3389
3390 if (tcb->flags & fMP_DEST_BROAD)
3391 atomic_inc(&adapter->stats.broadcast_pkts_xmtd);
3392 else if (tcb->flags & fMP_DEST_MULTI)
3393 atomic_inc(&adapter->stats.multicast_pkts_xmtd);
3394 else
3395 atomic_inc(&adapter->stats.unicast_pkts_xmtd);
3396
3397 if (tcb->skb) {
3398 stats->tx_bytes += tcb->skb->len;
3399
3400 /* Iterate through the TX descriptors on the ring
3401 * corresponding to this packet and umap the fragments
3402 * they point to
3403 */
3404 do {
3405 desc = (struct tx_desc *)
3406 (adapter->tx_ring.tx_desc_ring +
3407 INDEX10(tcb->index_start));
3408
998f6dfb 3409 dma_addr = desc->addr_lo;
983e4b35 3410 dma_addr |= (u64)desc->addr_hi << 32;
998f6dfb 3411
26dc751e 3412 dma_unmap_single(&adapter->pdev->dev,
998f6dfb 3413 dma_addr,
26dc751e 3414 desc->len_vlan, DMA_TO_DEVICE);
d2796743
ME
3415
3416 add_10bit(&tcb->index_start, 1);
3417 if (INDEX10(tcb->index_start) >=
3418 NUM_DESC_PER_RING_TX) {
3419 tcb->index_start &= ~ET_DMA10_MASK;
3420 tcb->index_start ^= ET_DMA10_WRAP;
3421 }
3422 } while (desc != (adapter->tx_ring.tx_desc_ring +
3423 INDEX10(tcb->index)));
3424
3425 dev_kfree_skb_any(tcb->skb);
3426 }
3427
3428 memset(tcb, 0, sizeof(struct tcb));
3429
3430 /* Add the TCB to the Ready Q */
3431 spin_lock_irqsave(&adapter->tcb_ready_qlock, flags);
3432
3433 adapter->net_stats.tx_packets++;
3434
3435 if (adapter->tx_ring.tcb_qtail)
3436 adapter->tx_ring.tcb_qtail->next = tcb;
3437 else
3438 /* Apparently ready Q is empty. */
3439 adapter->tx_ring.tcb_qhead = tcb;
3440
3441 adapter->tx_ring.tcb_qtail = tcb;
3442
3443 spin_unlock_irqrestore(&adapter->tcb_ready_qlock, flags);
3444 WARN_ON(adapter->tx_ring.used < 0);
3445}
3446
3447/**
3448 * et131x_free_busy_send_packets - Free and complete the stopped active sends
3449 * @adapter: pointer to our adapter
3450 *
3451 * Assumption - Send spinlock has been acquired
3452 */
eb7a6ca6 3453static void et131x_free_busy_send_packets(struct et131x_adapter *adapter)
d2796743
ME
3454{
3455 struct tcb *tcb;
3456 unsigned long flags;
3457 u32 freed = 0;
3458
3459 /* Any packets being sent? Check the first TCB on the send list */
3460 spin_lock_irqsave(&adapter->tcb_send_qlock, flags);
3461
3462 tcb = adapter->tx_ring.send_head;
3463
3464 while (tcb != NULL && freed < NUM_TCB) {
3465 struct tcb *next = tcb->next;
3466
3467 adapter->tx_ring.send_head = next;
3468
3469 if (next == NULL)
3470 adapter->tx_ring.send_tail = NULL;
3471
3472 adapter->tx_ring.used--;
3473
3474 spin_unlock_irqrestore(&adapter->tcb_send_qlock, flags);
3475
3476 freed++;
3477 free_send_packet(adapter, tcb);
3478
3479 spin_lock_irqsave(&adapter->tcb_send_qlock, flags);
3480
3481 tcb = adapter->tx_ring.send_head;
3482 }
3483
3484 WARN_ON(freed == NUM_TCB);
3485
3486 spin_unlock_irqrestore(&adapter->tcb_send_qlock, flags);
3487
3488 adapter->tx_ring.used = 0;
3489}
3490
3491/**
3492 * et131x_handle_send_interrupt - Interrupt handler for sending processing
3493 * @adapter: pointer to our adapter
3494 *
3495 * Re-claim the send resources, complete sends and get more to send from
3496 * the send wait queue.
3497 *
3498 * Assumption - Send spinlock has been acquired
3499 */
eb7a6ca6 3500static void et131x_handle_send_interrupt(struct et131x_adapter *adapter)
d2796743
ME
3501{
3502 unsigned long flags;
3503 u32 serviced;
3504 struct tcb *tcb;
3505 u32 index;
3506
3507 serviced = readl(&adapter->regs->txdma.new_service_complete);
3508 index = INDEX10(serviced);
3509
3510 /* Has the ring wrapped? Process any descriptors that do not have
3511 * the same "wrap" indicator as the current completion indicator
3512 */
3513 spin_lock_irqsave(&adapter->tcb_send_qlock, flags);
3514
3515 tcb = adapter->tx_ring.send_head;
3516
3517 while (tcb &&
3518 ((serviced ^ tcb->index) & ET_DMA10_WRAP) &&
3519 index < INDEX10(tcb->index)) {
3520 adapter->tx_ring.used--;
3521 adapter->tx_ring.send_head = tcb->next;
3522 if (tcb->next == NULL)
3523 adapter->tx_ring.send_tail = NULL;
3524
3525 spin_unlock_irqrestore(&adapter->tcb_send_qlock, flags);
3526 free_send_packet(adapter, tcb);
3527 spin_lock_irqsave(&adapter->tcb_send_qlock, flags);
3528
3529 /* Goto the next packet */
3530 tcb = adapter->tx_ring.send_head;
3531 }
3532 while (tcb &&
3533 !((serviced ^ tcb->index) & ET_DMA10_WRAP)
3534 && index > (tcb->index & ET_DMA10_MASK)) {
3535 adapter->tx_ring.used--;
3536 adapter->tx_ring.send_head = tcb->next;
3537 if (tcb->next == NULL)
3538 adapter->tx_ring.send_tail = NULL;
3539
3540 spin_unlock_irqrestore(&adapter->tcb_send_qlock, flags);
3541 free_send_packet(adapter, tcb);
3542 spin_lock_irqsave(&adapter->tcb_send_qlock, flags);
3543
3544 /* Goto the next packet */
3545 tcb = adapter->tx_ring.send_head;
3546 }
3547
3548 /* Wake up the queue when we hit a low-water mark */
3549 if (adapter->tx_ring.used <= NUM_TCB / 3)
3550 netif_wake_queue(adapter->netdev);
3551
3552 spin_unlock_irqrestore(&adapter->tcb_send_qlock, flags);
3553}
3554
d2796743
ME
3555static int et131x_get_settings(struct net_device *netdev,
3556 struct ethtool_cmd *cmd)
3557{
3558 struct et131x_adapter *adapter = netdev_priv(netdev);
3559
3560 return phy_ethtool_gset(adapter->phydev, cmd);
3561}
3562
3563static int et131x_set_settings(struct net_device *netdev,
3564 struct ethtool_cmd *cmd)
3565{
3566 struct et131x_adapter *adapter = netdev_priv(netdev);
3567
3568 return phy_ethtool_sset(adapter->phydev, cmd);
3569}
3570
3571static int et131x_get_regs_len(struct net_device *netdev)
3572{
3573#define ET131X_REGS_LEN 256
3574 return ET131X_REGS_LEN * sizeof(u32);
3575}
3576
3577static void et131x_get_regs(struct net_device *netdev,
3578 struct ethtool_regs *regs, void *regs_data)
3579{
3580 struct et131x_adapter *adapter = netdev_priv(netdev);
3581 struct address_map __iomem *aregs = adapter->regs;
3582 u32 *regs_buff = regs_data;
3583 u32 num = 0;
3584
3585 memset(regs_data, 0, et131x_get_regs_len(netdev));
3586
3587 regs->version = (1 << 24) | (adapter->pdev->revision << 16) |
3588 adapter->pdev->device;
3589
3590 /* PHY regs */
3591 et131x_mii_read(adapter, MII_BMCR, (u16 *)&regs_buff[num++]);
3592 et131x_mii_read(adapter, MII_BMSR, (u16 *)&regs_buff[num++]);
3593 et131x_mii_read(adapter, MII_PHYSID1, (u16 *)&regs_buff[num++]);
3594 et131x_mii_read(adapter, MII_PHYSID2, (u16 *)&regs_buff[num++]);
3595 et131x_mii_read(adapter, MII_ADVERTISE, (u16 *)&regs_buff[num++]);
3596 et131x_mii_read(adapter, MII_LPA, (u16 *)&regs_buff[num++]);
3597 et131x_mii_read(adapter, MII_EXPANSION, (u16 *)&regs_buff[num++]);
3598 /* Autoneg next page transmit reg */
3599 et131x_mii_read(adapter, 0x07, (u16 *)&regs_buff[num++]);
3600 /* Link partner next page reg */
3601 et131x_mii_read(adapter, 0x08, (u16 *)&regs_buff[num++]);
3602 et131x_mii_read(adapter, MII_CTRL1000, (u16 *)&regs_buff[num++]);
3603 et131x_mii_read(adapter, MII_STAT1000, (u16 *)&regs_buff[num++]);
3604 et131x_mii_read(adapter, MII_ESTATUS, (u16 *)&regs_buff[num++]);
3605 et131x_mii_read(adapter, PHY_INDEX_REG, (u16 *)&regs_buff[num++]);
3606 et131x_mii_read(adapter, PHY_DATA_REG, (u16 *)&regs_buff[num++]);
3607 et131x_mii_read(adapter, PHY_MPHY_CONTROL_REG,
3608 (u16 *)&regs_buff[num++]);
3609 et131x_mii_read(adapter, PHY_LOOPBACK_CONTROL,
3610 (u16 *)&regs_buff[num++]);
3611 et131x_mii_read(adapter, PHY_LOOPBACK_CONTROL+1,
3612 (u16 *)&regs_buff[num++]);
3613 et131x_mii_read(adapter, PHY_REGISTER_MGMT_CONTROL,
3614 (u16 *)&regs_buff[num++]);
3615 et131x_mii_read(adapter, PHY_CONFIG, (u16 *)&regs_buff[num++]);
3616 et131x_mii_read(adapter, PHY_PHY_CONTROL, (u16 *)&regs_buff[num++]);
3617 et131x_mii_read(adapter, PHY_INTERRUPT_MASK, (u16 *)&regs_buff[num++]);
3618 et131x_mii_read(adapter, PHY_INTERRUPT_STATUS,
3619 (u16 *)&regs_buff[num++]);
3620 et131x_mii_read(adapter, PHY_PHY_STATUS, (u16 *)&regs_buff[num++]);
3621 et131x_mii_read(adapter, PHY_LED_1, (u16 *)&regs_buff[num++]);
3622 et131x_mii_read(adapter, PHY_LED_2, (u16 *)&regs_buff[num++]);
3623
3624 /* Global regs */
3625 regs_buff[num++] = readl(&aregs->global.txq_start_addr);
3626 regs_buff[num++] = readl(&aregs->global.txq_end_addr);
3627 regs_buff[num++] = readl(&aregs->global.rxq_start_addr);
3628 regs_buff[num++] = readl(&aregs->global.rxq_end_addr);
3629 regs_buff[num++] = readl(&aregs->global.pm_csr);
3630 regs_buff[num++] = adapter->stats.interrupt_status;
3631 regs_buff[num++] = readl(&aregs->global.int_mask);
3632 regs_buff[num++] = readl(&aregs->global.int_alias_clr_en);
3633 regs_buff[num++] = readl(&aregs->global.int_status_alias);
3634 regs_buff[num++] = readl(&aregs->global.sw_reset);
3635 regs_buff[num++] = readl(&aregs->global.slv_timer);
3636 regs_buff[num++] = readl(&aregs->global.msi_config);
3637 regs_buff[num++] = readl(&aregs->global.loopback);
3638 regs_buff[num++] = readl(&aregs->global.watchdog_timer);
3639
3640 /* TXDMA regs */
3641 regs_buff[num++] = readl(&aregs->txdma.csr);
3642 regs_buff[num++] = readl(&aregs->txdma.pr_base_hi);
3643 regs_buff[num++] = readl(&aregs->txdma.pr_base_lo);
3644 regs_buff[num++] = readl(&aregs->txdma.pr_num_des);
3645 regs_buff[num++] = readl(&aregs->txdma.txq_wr_addr);
3646 regs_buff[num++] = readl(&aregs->txdma.txq_wr_addr_ext);
3647 regs_buff[num++] = readl(&aregs->txdma.txq_rd_addr);
3648 regs_buff[num++] = readl(&aregs->txdma.dma_wb_base_hi);
3649 regs_buff[num++] = readl(&aregs->txdma.dma_wb_base_lo);
3650 regs_buff[num++] = readl(&aregs->txdma.service_request);
3651 regs_buff[num++] = readl(&aregs->txdma.service_complete);
3652 regs_buff[num++] = readl(&aregs->txdma.cache_rd_index);
3653 regs_buff[num++] = readl(&aregs->txdma.cache_wr_index);
3654 regs_buff[num++] = readl(&aregs->txdma.tx_dma_error);
3655 regs_buff[num++] = readl(&aregs->txdma.desc_abort_cnt);
3656 regs_buff[num++] = readl(&aregs->txdma.payload_abort_cnt);
3657 regs_buff[num++] = readl(&aregs->txdma.writeback_abort_cnt);
3658 regs_buff[num++] = readl(&aregs->txdma.desc_timeout_cnt);
3659 regs_buff[num++] = readl(&aregs->txdma.payload_timeout_cnt);
3660 regs_buff[num++] = readl(&aregs->txdma.writeback_timeout_cnt);
3661 regs_buff[num++] = readl(&aregs->txdma.desc_error_cnt);
3662 regs_buff[num++] = readl(&aregs->txdma.payload_error_cnt);
3663 regs_buff[num++] = readl(&aregs->txdma.writeback_error_cnt);
3664 regs_buff[num++] = readl(&aregs->txdma.dropped_tlp_cnt);
3665 regs_buff[num++] = readl(&aregs->txdma.new_service_complete);
3666 regs_buff[num++] = readl(&aregs->txdma.ethernet_packet_cnt);
3667
3668 /* RXDMA regs */
3669 regs_buff[num++] = readl(&aregs->rxdma.csr);
3670 regs_buff[num++] = readl(&aregs->rxdma.dma_wb_base_hi);
3671 regs_buff[num++] = readl(&aregs->rxdma.dma_wb_base_lo);
3672 regs_buff[num++] = readl(&aregs->rxdma.num_pkt_done);
3673 regs_buff[num++] = readl(&aregs->rxdma.max_pkt_time);
3674 regs_buff[num++] = readl(&aregs->rxdma.rxq_rd_addr);
3675 regs_buff[num++] = readl(&aregs->rxdma.rxq_rd_addr_ext);
3676 regs_buff[num++] = readl(&aregs->rxdma.rxq_wr_addr);
3677 regs_buff[num++] = readl(&aregs->rxdma.psr_base_hi);
3678 regs_buff[num++] = readl(&aregs->rxdma.psr_base_lo);
3679 regs_buff[num++] = readl(&aregs->rxdma.psr_num_des);
3680 regs_buff[num++] = readl(&aregs->rxdma.psr_avail_offset);
3681 regs_buff[num++] = readl(&aregs->rxdma.psr_full_offset);
3682 regs_buff[num++] = readl(&aregs->rxdma.psr_access_index);
3683 regs_buff[num++] = readl(&aregs->rxdma.psr_min_des);
3684 regs_buff[num++] = readl(&aregs->rxdma.fbr0_base_lo);
3685 regs_buff[num++] = readl(&aregs->rxdma.fbr0_base_hi);
3686 regs_buff[num++] = readl(&aregs->rxdma.fbr0_num_des);
3687 regs_buff[num++] = readl(&aregs->rxdma.fbr0_avail_offset);
3688 regs_buff[num++] = readl(&aregs->rxdma.fbr0_full_offset);
3689 regs_buff[num++] = readl(&aregs->rxdma.fbr0_rd_index);
3690 regs_buff[num++] = readl(&aregs->rxdma.fbr0_min_des);
3691 regs_buff[num++] = readl(&aregs->rxdma.fbr1_base_lo);
3692 regs_buff[num++] = readl(&aregs->rxdma.fbr1_base_hi);
3693 regs_buff[num++] = readl(&aregs->rxdma.fbr1_num_des);
3694 regs_buff[num++] = readl(&aregs->rxdma.fbr1_avail_offset);
3695 regs_buff[num++] = readl(&aregs->rxdma.fbr1_full_offset);
3696 regs_buff[num++] = readl(&aregs->rxdma.fbr1_rd_index);
3697 regs_buff[num++] = readl(&aregs->rxdma.fbr1_min_des);
3698}
3699
3700#define ET131X_DRVINFO_LEN 32 /* value from ethtool.h */
3701static void et131x_get_drvinfo(struct net_device *netdev,
3702 struct ethtool_drvinfo *info)
3703{
3704 struct et131x_adapter *adapter = netdev_priv(netdev);
3705
3706 strncpy(info->driver, DRIVER_NAME, ET131X_DRVINFO_LEN);
3707 strncpy(info->version, DRIVER_VERSION, ET131X_DRVINFO_LEN);
3708 strncpy(info->bus_info, pci_name(adapter->pdev), ET131X_DRVINFO_LEN);
3709}
3710
3711static struct ethtool_ops et131x_ethtool_ops = {
3712 .get_settings = et131x_get_settings,
3713 .set_settings = et131x_set_settings,
3714 .get_drvinfo = et131x_get_drvinfo,
3715 .get_regs_len = et131x_get_regs_len,
3716 .get_regs = et131x_get_regs,
242187aa 3717 .get_link = ethtool_op_get_link,
d2796743 3718};
d2796743
ME
3719/**
3720 * et131x_hwaddr_init - set up the MAC Address on the ET1310
3721 * @adapter: pointer to our private adapter structure
3722 */
eb7a6ca6 3723static void et131x_hwaddr_init(struct et131x_adapter *adapter)
d2796743
ME
3724{
3725 /* If have our default mac from init and no mac address from
3726 * EEPROM then we need to generate the last octet and set it on the
3727 * device
3728 */
c14d01b8 3729 if (is_zero_ether_addr(adapter->rom_addr)) {
d2796743
ME
3730 /*
3731 * We need to randomly generate the last octet so we
3732 * decrease our chances of setting the mac address to
3733 * same as another one of our cards in the system
3734 */
3735 get_random_bytes(&adapter->addr[5], 1);
3736 /*
3737 * We have the default value in the register we are
3738 * working with so we need to copy the current
3739 * address into the permanent address
3740 */
3741 memcpy(adapter->rom_addr,
3742 adapter->addr, ETH_ALEN);
3743 } else {
3744 /* We do not have an override address, so set the
3745 * current address to the permanent address and add
3746 * it to the device
3747 */
3748 memcpy(adapter->addr,
3749 adapter->rom_addr, ETH_ALEN);
3750 }
3751}
3752
3753/**
3754 * et131x_pci_init - initial PCI setup
3755 * @adapter: pointer to our private adapter structure
3756 * @pdev: our PCI device
3757 *
3758 * Perform the initial setup of PCI registers and if possible initialise
3759 * the MAC address. At this point the I/O registers have yet to be mapped
3760 */
3761static int et131x_pci_init(struct et131x_adapter *adapter,
3762 struct pci_dev *pdev)
3763{
d14e3d05 3764 u16 max_payload;
d14e3d05
FR
3765 int i, rc;
3766
3767 rc = et131x_init_eeprom(adapter);
3768 if (rc < 0)
3769 goto out;
3770
532c5f69 3771 if (!pci_is_pcie(pdev)) {
d14e3d05
FR
3772 dev_err(&pdev->dev, "Missing PCIe capabilities\n");
3773 goto err_out;
3774 }
bf3313a1 3775
d2796743
ME
3776 /* Let's set up the PORT LOGIC Register. First we need to know what
3777 * the max_payload_size is
3778 */
532c5f69 3779 if (pcie_capability_read_word(pdev, PCI_EXP_DEVCAP, &max_payload)) {
d2796743
ME
3780 dev_err(&pdev->dev,
3781 "Could not read PCI config space for Max Payload Size\n");
d14e3d05 3782 goto err_out;
d2796743
ME
3783 }
3784
3785 /* Program the Ack/Nak latency and replay timers */
d14e3d05 3786 max_payload &= 0x07;
d2796743
ME
3787
3788 if (max_payload < 2) {
3789 static const u16 acknak[2] = { 0x76, 0xD0 };
3790 static const u16 replay[2] = { 0x1E0, 0x2ED };
3791
3792 if (pci_write_config_word(pdev, ET1310_PCI_ACK_NACK,
3793 acknak[max_payload])) {
3794 dev_err(&pdev->dev,
3795 "Could not write PCI config space for ACK/NAK\n");
d14e3d05 3796 goto err_out;
d2796743
ME
3797 }
3798 if (pci_write_config_word(pdev, ET1310_PCI_REPLAY,
3799 replay[max_payload])) {
3800 dev_err(&pdev->dev,
3801 "Could not write PCI config space for Replay Timer\n");
d14e3d05 3802 goto err_out;
d2796743
ME
3803 }
3804 }
3805
3806 /* l0s and l1 latency timers. We are using default values.
3807 * Representing 001 for L0s and 010 for L1
3808 */
3809 if (pci_write_config_byte(pdev, ET1310_PCI_L0L1LATENCY, 0x11)) {
3810 dev_err(&pdev->dev,
3811 "Could not write PCI config space for Latency Timers\n");
d14e3d05 3812 goto err_out;
d2796743
ME
3813 }
3814
3815 /* Change the max read size to 2k */
532c5f69
JL
3816 if (pcie_capability_clear_and_set_word(pdev, PCI_EXP_DEVCTL,
3817 PCI_EXP_DEVCTL_READRQ, 0x4 << 12)) {
d2796743 3818 dev_err(&pdev->dev,
532c5f69 3819 "Couldn't change PCI config space for Max read size\n");
d14e3d05 3820 goto err_out;
d2796743
ME
3821 }
3822
3823 /* Get MAC address from config space if an eeprom exists, otherwise
3824 * the MAC address there will not be valid
3825 */
3826 if (!adapter->has_eeprom) {
3827 et131x_hwaddr_init(adapter);
3828 return 0;
3829 }
3830
3831 for (i = 0; i < ETH_ALEN; i++) {
3832 if (pci_read_config_byte(pdev, ET1310_PCI_MAC_ADDRESS + i,
3833 adapter->rom_addr + i)) {
3834 dev_err(&pdev->dev, "Could not read PCI config space for MAC address\n");
d14e3d05 3835 goto err_out;
d2796743
ME
3836 }
3837 }
3838 memcpy(adapter->addr, adapter->rom_addr, ETH_ALEN);
d14e3d05
FR
3839out:
3840 return rc;
3841err_out:
3842 rc = -EIO;
3843 goto out;
d2796743
ME
3844}
3845
3846/**
3847 * et131x_error_timer_handler
3848 * @data: timer-specific variable; here a pointer to our adapter structure
3849 *
3850 * The routine called when the error timer expires, to track the number of
3851 * recurring errors.
3852 */
eb7a6ca6 3853static void et131x_error_timer_handler(unsigned long data)
d2796743
ME
3854{
3855 struct et131x_adapter *adapter = (struct et131x_adapter *) data;
3856 struct phy_device *phydev = adapter->phydev;
3857
3858 if (et1310_in_phy_coma(adapter)) {
3859 /* Bring the device immediately out of coma, to
3860 * prevent it from sleeping indefinitely, this
3861 * mechanism could be improved! */
3862 et1310_disable_phy_coma(adapter);
3863 adapter->boot_coma = 20;
3864 } else {
3865 et1310_update_macstat_host_counters(adapter);
3866 }
3867
3868 if (!phydev->link && adapter->boot_coma < 11)
3869 adapter->boot_coma++;
3870
3871 if (adapter->boot_coma == 10) {
3872 if (!phydev->link) {
3873 if (!et1310_in_phy_coma(adapter)) {
3874 /* NOTE - This was originally a 'sync with
3875 * interrupt'. How to do that under Linux?
3876 */
3877 et131x_enable_interrupts(adapter);
3878 et1310_enable_phy_coma(adapter);
3879 }
3880 }
3881 }
3882
3883 /* This is a periodic timer, so reschedule */
242187aa 3884 mod_timer(&adapter->error_timer, jiffies + TX_ERROR_PERIOD * HZ / 1000);
d2796743
ME
3885}
3886
d2796743
ME
3887/**
3888 * et131x_adapter_memory_alloc
3889 * @adapter: pointer to our private adapter structure
3890 *
3891 * Returns 0 on success, errno on failure (as defined in errno.h).
3892 *
3893 * Allocate all the memory blocks for send, receive and others.
3894 */
eb7a6ca6 3895static int et131x_adapter_memory_alloc(struct et131x_adapter *adapter)
d2796743
ME
3896{
3897 int status;
3898
3899 /* Allocate memory for the Tx Ring */
3900 status = et131x_tx_dma_memory_alloc(adapter);
3901 if (status != 0) {
3902 dev_err(&adapter->pdev->dev,
3903 "et131x_tx_dma_memory_alloc FAILED\n");
3904 return status;
3905 }
3906 /* Receive buffer memory allocation */
3907 status = et131x_rx_dma_memory_alloc(adapter);
3908 if (status != 0) {
3909 dev_err(&adapter->pdev->dev,
3910 "et131x_rx_dma_memory_alloc FAILED\n");
3911 et131x_tx_dma_memory_free(adapter);
3912 return status;
3913 }
3914
3915 /* Init receive data structures */
3916 status = et131x_init_recv(adapter);
3917 if (status != 0) {
3918 dev_err(&adapter->pdev->dev,
3919 "et131x_init_recv FAILED\n");
3920 et131x_tx_dma_memory_free(adapter);
3921 et131x_rx_dma_memory_free(adapter);
3922 }
3923 return status;
3924}
3925
3926/**
3927 * et131x_adapter_memory_free - Free all memory allocated for use by Tx & Rx
3928 * @adapter: pointer to our private adapter structure
3929 */
eb7a6ca6 3930static void et131x_adapter_memory_free(struct et131x_adapter *adapter)
d2796743
ME
3931{
3932 /* Free DMA memory */
3933 et131x_tx_dma_memory_free(adapter);
3934 et131x_rx_dma_memory_free(adapter);
3935}
3936
3937static void et131x_adjust_link(struct net_device *netdev)
3938{
3939 struct et131x_adapter *adapter = netdev_priv(netdev);
3940 struct phy_device *phydev = adapter->phydev;
3941
3942 if (netif_carrier_ok(netdev)) {
3943 adapter->boot_coma = 20;
3944
3945 if (phydev && phydev->speed == SPEED_10) {
3946 /*
3947 * NOTE - Is there a way to query this without
3948 * TruePHY?
3949 * && TRU_QueryCoreType(adapter->hTruePhy, 0)==
3950 * EMI_TRUEPHY_A13O) {
3951 */
3952 u16 register18;
3953
3954 et131x_mii_read(adapter, PHY_MPHY_CONTROL_REG,
3955 &register18);
3956 et131x_mii_write(adapter, PHY_MPHY_CONTROL_REG,
3957 register18 | 0x4);
3958 et131x_mii_write(adapter, PHY_INDEX_REG,
3959 register18 | 0x8402);
3960 et131x_mii_write(adapter, PHY_DATA_REG,
3961 register18 | 511);
3962 et131x_mii_write(adapter, PHY_MPHY_CONTROL_REG,
3963 register18);
3964 }
3965
3966 et1310_config_flow_control(adapter);
3967
3968 if (phydev && phydev->speed == SPEED_1000 &&
3969 adapter->registry_jumbo_packet > 2048) {
3970 u16 reg;
3971
3972 et131x_mii_read(adapter, PHY_CONFIG, &reg);
3973 reg &= ~ET_PHY_CONFIG_TX_FIFO_DEPTH;
3974 reg |= ET_PHY_CONFIG_FIFO_DEPTH_32;
3975 et131x_mii_write(adapter, PHY_CONFIG, reg);
3976 }
3977
3978 et131x_set_rx_dma_timer(adapter);
3979 et1310_config_mac_regs2(adapter);
3980 }
3981
3982 if (phydev && phydev->link != adapter->link) {
3983 /*
3984 * Check to see if we are in coma mode and if
3985 * so, disable it because we will not be able
3986 * to read PHY values until we are out.
3987 */
3988 if (et1310_in_phy_coma(adapter))
3989 et1310_disable_phy_coma(adapter);
3990
3991 if (phydev->link) {
3992 adapter->boot_coma = 20;
3993 } else {
3994 dev_warn(&adapter->pdev->dev,
3995 "Link down - cable problem ?\n");
3996 adapter->boot_coma = 0;
3997
3998 if (phydev->speed == SPEED_10) {
3999 /* NOTE - Is there a way to query this without
4000 * TruePHY?
4001 * && TRU_QueryCoreType(adapter->hTruePhy, 0) ==
4002 * EMI_TRUEPHY_A13O)
4003 */
4004 u16 register18;
4005
4006 et131x_mii_read(adapter, PHY_MPHY_CONTROL_REG,
4007 &register18);
4008 et131x_mii_write(adapter, PHY_MPHY_CONTROL_REG,
4009 register18 | 0x4);
4010 et131x_mii_write(adapter, PHY_INDEX_REG,
4011 register18 | 0x8402);
4012 et131x_mii_write(adapter, PHY_DATA_REG,
4013 register18 | 511);
4014 et131x_mii_write(adapter, PHY_MPHY_CONTROL_REG,
4015 register18);
4016 }
4017
4018 /* Free the packets being actively sent & stopped */
4019 et131x_free_busy_send_packets(adapter);
4020
4021 /* Re-initialize the send structures */
4022 et131x_init_send(adapter);
4023
d2796743
ME
4024 /*
4025 * Bring the device back to the state it was during
4026 * init prior to autonegotiation being complete. This
4027 * way, when we get the auto-neg complete interrupt,
4028 * we can complete init by calling config_mac_regs2.
4029 */
4030 et131x_soft_reset(adapter);
4031
4032 /* Setup ET1310 as per the documentation */
4033 et131x_adapter_setup(adapter);
4034
4035 /* perform reset of tx/rx */
4036 et131x_disable_txrx(netdev);
4037 et131x_enable_txrx(netdev);
4038 }
4039
4040 adapter->link = phydev->link;
4041
4042 phy_print_status(phydev);
4043 }
4044}
4045
4046static int et131x_mii_probe(struct net_device *netdev)
4047{
4048 struct et131x_adapter *adapter = netdev_priv(netdev);
4049 struct phy_device *phydev = NULL;
4050
4051 phydev = phy_find_first(adapter->mii_bus);
4052 if (!phydev) {
4053 dev_err(&adapter->pdev->dev, "no PHY found\n");
4054 return -ENODEV;
4055 }
4056
4057 phydev = phy_connect(netdev, dev_name(&phydev->dev),
4058 &et131x_adjust_link, 0, PHY_INTERFACE_MODE_MII);
4059
4060 if (IS_ERR(phydev)) {
4061 dev_err(&adapter->pdev->dev, "Could not attach to PHY\n");
4062 return PTR_ERR(phydev);
4063 }
4064
4065 phydev->supported &= (SUPPORTED_10baseT_Half
4066 | SUPPORTED_10baseT_Full
4067 | SUPPORTED_100baseT_Half
4068 | SUPPORTED_100baseT_Full
4069 | SUPPORTED_Autoneg
4070 | SUPPORTED_MII
4071 | SUPPORTED_TP);
4072
4073 if (adapter->pdev->device != ET131X_PCI_DEVICE_ID_FAST)
4074 phydev->supported |= SUPPORTED_1000baseT_Full;
4075
4076 phydev->advertising = phydev->supported;
4077 adapter->phydev = phydev;
4078
397d3e60 4079 dev_info(&adapter->pdev->dev, "attached PHY driver [%s] (mii_bus:phy_addr=%s)\n",
d2796743
ME
4080 phydev->drv->name, dev_name(&phydev->dev));
4081
4082 return 0;
4083}
4084
4085/**
4086 * et131x_adapter_init
4087 * @adapter: pointer to the private adapter struct
4088 * @pdev: pointer to the PCI device
4089 *
4090 * Initialize the data structures for the et131x_adapter object and link
4091 * them together with the platform provided device structures.
4092 */
4093static struct et131x_adapter *et131x_adapter_init(struct net_device *netdev,
4094 struct pci_dev *pdev)
4095{
4096 static const u8 default_mac[] = { 0x00, 0x05, 0x3d, 0x00, 0x02, 0x00 };
4097
4098 struct et131x_adapter *adapter;
4099
4100 /* Allocate private adapter struct and copy in relevant information */
4101 adapter = netdev_priv(netdev);
4102 adapter->pdev = pci_dev_get(pdev);
4103 adapter->netdev = netdev;
4104
d2796743
ME
4105 /* Initialize spinlocks here */
4106 spin_lock_init(&adapter->lock);
4107 spin_lock_init(&adapter->tcb_send_qlock);
4108 spin_lock_init(&adapter->tcb_ready_qlock);
4109 spin_lock_init(&adapter->send_hw_lock);
4110 spin_lock_init(&adapter->rcv_lock);
4111 spin_lock_init(&adapter->rcv_pend_lock);
4112 spin_lock_init(&adapter->fbr_lock);
4113 spin_lock_init(&adapter->phy_lock);
4114
4115 adapter->registry_jumbo_packet = 1514; /* 1514-9216 */
4116
4117 /* Set the MAC address to a default */
4118 memcpy(adapter->addr, default_mac, ETH_ALEN);
4119
4120 return adapter;
4121}
4122
d2796743
ME
4123/**
4124 * et131x_pci_remove
4125 * @pdev: a pointer to the device's pci_dev structure
4126 *
4127 * Registered in the pci_driver structure, this function is called when the
4128 * PCI subsystem detects that a PCI device which matches the information
4129 * contained in the pci_device_id table has been removed.
4130 */
4131static void __devexit et131x_pci_remove(struct pci_dev *pdev)
4132{
4133 struct net_device *netdev = pci_get_drvdata(pdev);
4134 struct et131x_adapter *adapter = netdev_priv(netdev);
4135
4136 unregister_netdev(netdev);
fa9f0a65 4137 phy_disconnect(adapter->phydev);
d2796743
ME
4138 mdiobus_unregister(adapter->mii_bus);
4139 kfree(adapter->mii_bus->irq);
4140 mdiobus_free(adapter->mii_bus);
4141
4142 et131x_adapter_memory_free(adapter);
4143 iounmap(adapter->regs);
4144 pci_dev_put(pdev);
4145
4146 free_netdev(netdev);
4147 pci_release_regions(pdev);
4148 pci_disable_device(pdev);
4149}
4150
a4d444bd
ME
4151/**
4152 * et131x_up - Bring up a device for use.
4153 * @netdev: device to be opened
4154 */
eb7a6ca6 4155static void et131x_up(struct net_device *netdev)
a4d444bd
ME
4156{
4157 struct et131x_adapter *adapter = netdev_priv(netdev);
4158
4159 et131x_enable_txrx(netdev);
4160 phy_start(adapter->phydev);
4161}
4162
4163/**
4164 * et131x_down - Bring down the device
ac399bc0 4165 * @netdev: device to be brought down
a4d444bd 4166 */
eb7a6ca6 4167static void et131x_down(struct net_device *netdev)
a4d444bd
ME
4168{
4169 struct et131x_adapter *adapter = netdev_priv(netdev);
4170
4171 /* Save the timestamp for the TX watchdog, prevent a timeout */
4172 netdev->trans_start = jiffies;
4173
4174 phy_stop(adapter->phydev);
4175 et131x_disable_txrx(netdev);
4176}
4177
d2796743
ME
4178#ifdef CONFIG_PM_SLEEP
4179static int et131x_suspend(struct device *dev)
4180{
4181 struct pci_dev *pdev = to_pci_dev(dev);
4182 struct net_device *netdev = pci_get_drvdata(pdev);
4183
4184 if (netif_running(netdev)) {
4185 netif_device_detach(netdev);
4186 et131x_down(netdev);
4187 pci_save_state(pdev);
4188 }
4189
4190 return 0;
4191}
4192
4193static int et131x_resume(struct device *dev)
4194{
4195 struct pci_dev *pdev = to_pci_dev(dev);
4196 struct net_device *netdev = pci_get_drvdata(pdev);
4197
4198 if (netif_running(netdev)) {
4199 pci_restore_state(pdev);
4200 et131x_up(netdev);
4201 netif_device_attach(netdev);
4202 }
4203
4204 return 0;
4205}
4206
2e9ff8d9
ME
4207static SIMPLE_DEV_PM_OPS(et131x_pm_ops, et131x_suspend, et131x_resume);
4208#define ET131X_PM_OPS (&et131x_pm_ops)
4209#else
4210#define ET131X_PM_OPS NULL
4211#endif
4212
d2796743
ME
4213/**
4214 * et131x_isr - The Interrupt Service Routine for the driver.
4215 * @irq: the IRQ on which the interrupt was received.
4216 * @dev_id: device-specific info (here a pointer to a net_device struct)
4217 *
4218 * Returns a value indicating if the interrupt was handled.
4219 */
4220irqreturn_t et131x_isr(int irq, void *dev_id)
4221{
4222 bool handled = true;
4223 struct net_device *netdev = (struct net_device *)dev_id;
4224 struct et131x_adapter *adapter = NULL;
4225 u32 status;
4226
4227 if (!netif_device_present(netdev)) {
4228 handled = false;
4229 goto out;
4230 }
4231
4232 adapter = netdev_priv(netdev);
4233
4234 /* If the adapter is in low power state, then it should not
4235 * recognize any interrupt
4236 */
4237
4238 /* Disable Device Interrupts */
4239 et131x_disable_interrupts(adapter);
4240
4241 /* Get a copy of the value in the interrupt status register
4242 * so we can process the interrupting section
4243 */
4244 status = readl(&adapter->regs->global.int_status);
4245
4246 if (adapter->flowcontrol == FLOW_TXONLY ||
4247 adapter->flowcontrol == FLOW_BOTH) {
4248 status &= ~INT_MASK_ENABLE;
4249 } else {
4250 status &= ~INT_MASK_ENABLE_NO_FLOW;
4251 }
4252
4253 /* Make sure this is our interrupt */
4254 if (!status) {
4255 handled = false;
4256 et131x_enable_interrupts(adapter);
4257 goto out;
4258 }
4259
4260 /* This is our interrupt, so process accordingly */
4261
4262 if (status & ET_INTR_WATCHDOG) {
4263 struct tcb *tcb = adapter->tx_ring.send_head;
4264
4265 if (tcb)
4266 if (++tcb->stale > 1)
4267 status |= ET_INTR_TXDMA_ISR;
4268
4269 if (adapter->rx_ring.unfinished_receives)
4270 status |= ET_INTR_RXDMA_XFR_DONE;
4271 else if (tcb == NULL)
4272 writel(0, &adapter->regs->global.watchdog_timer);
4273
4274 status &= ~ET_INTR_WATCHDOG;
4275 }
4276
4277 if (status == 0) {
4278 /* This interrupt has in some way been "handled" by
4279 * the ISR. Either it was a spurious Rx interrupt, or
4280 * it was a Tx interrupt that has been filtered by
4281 * the ISR.
4282 */
4283 et131x_enable_interrupts(adapter);
4284 goto out;
4285 }
4286
4287 /* We need to save the interrupt status value for use in our
4288 * DPC. We will clear the software copy of that in that
4289 * routine.
4290 */
4291 adapter->stats.interrupt_status = status;
4292
4293 /* Schedule the ISR handler as a bottom-half task in the
4294 * kernel's tq_immediate queue, and mark the queue for
4295 * execution
4296 */
4297 schedule_work(&adapter->task);
4298out:
4299 return IRQ_RETVAL(handled);
4300}
4301
4302/**
4303 * et131x_isr_handler - The ISR handler
4304 * @p_adapter, a pointer to the device's private adapter structure
4305 *
4306 * scheduled to run in a deferred context by the ISR. This is where the ISR's
4307 * work actually gets done.
4308 */
eb7a6ca6 4309static void et131x_isr_handler(struct work_struct *work)
d2796743
ME
4310{
4311 struct et131x_adapter *adapter =
4312 container_of(work, struct et131x_adapter, task);
4313 u32 status = adapter->stats.interrupt_status;
4314 struct address_map __iomem *iomem = adapter->regs;
4315
4316 /*
4317 * These first two are by far the most common. Once handled, we clear
4318 * their two bits in the status word. If the word is now zero, we
4319 * exit.
4320 */
4321 /* Handle all the completed Transmit interrupts */
4322 if (status & ET_INTR_TXDMA_ISR)
4323 et131x_handle_send_interrupt(adapter);
4324
4325 /* Handle all the completed Receives interrupts */
4326 if (status & ET_INTR_RXDMA_XFR_DONE)
4327 et131x_handle_recv_interrupt(adapter);
4328
4329 status &= 0xffffffd7;
4330
4331 if (status) {
4332 /* Handle the TXDMA Error interrupt */
4333 if (status & ET_INTR_TXDMA_ERR) {
4334 u32 txdma_err;
4335
4336 /* Following read also clears the register (COR) */
4337 txdma_err = readl(&iomem->txdma.tx_dma_error);
4338
4339 dev_warn(&adapter->pdev->dev,
4340 "TXDMA_ERR interrupt, error = %d\n",
4341 txdma_err);
4342 }
4343
4344 /* Handle Free Buffer Ring 0 and 1 Low interrupt */
4345 if (status &
4346 (ET_INTR_RXDMA_FB_R0_LOW | ET_INTR_RXDMA_FB_R1_LOW)) {
4347 /*
4348 * This indicates the number of unused buffers in
4349 * RXDMA free buffer ring 0 is <= the limit you
4350 * programmed. Free buffer resources need to be
4351 * returned. Free buffers are consumed as packets
4352 * are passed from the network to the host. The host
4353 * becomes aware of the packets from the contents of
4354 * the packet status ring. This ring is queried when
4355 * the packet done interrupt occurs. Packets are then
4356 * passed to the OS. When the OS is done with the
4357 * packets the resources can be returned to the
4358 * ET1310 for re-use. This interrupt is one method of
4359 * returning resources.
4360 */
4361
4362 /* If the user has flow control on, then we will
4363 * send a pause packet, otherwise just exit
4364 */
4365 if (adapter->flowcontrol == FLOW_TXONLY ||
4366 adapter->flowcontrol == FLOW_BOTH) {
4367 u32 pm_csr;
4368
4369 /* Tell the device to send a pause packet via
4370 * the back pressure register (bp req and
4371 * bp xon/xoff)
4372 */
4373 pm_csr = readl(&iomem->global.pm_csr);
4374 if (!et1310_in_phy_coma(adapter))
4375 writel(3, &iomem->txmac.bp_ctrl);
4376 }
4377 }
4378
4379 /* Handle Packet Status Ring Low Interrupt */
4380 if (status & ET_INTR_RXDMA_STAT_LOW) {
4381
4382 /*
4383 * Same idea as with the two Free Buffer Rings.
4384 * Packets going from the network to the host each
4385 * consume a free buffer resource and a packet status
4386 * resource. These resoures are passed to the OS.
4387 * When the OS is done with the resources, they need
4388 * to be returned to the ET1310. This is one method
4389 * of returning the resources.
4390 */
4391 }
4392
4393 /* Handle RXDMA Error Interrupt */
4394 if (status & ET_INTR_RXDMA_ERR) {
4395 /*
4396 * The rxdma_error interrupt is sent when a time-out
4397 * on a request issued by the JAGCore has occurred or
4398 * a completion is returned with an un-successful
4399 * status. In both cases the request is considered
4400 * complete. The JAGCore will automatically re-try the
4401 * request in question. Normally information on events
4402 * like these are sent to the host using the "Advanced
4403 * Error Reporting" capability. This interrupt is
4404 * another way of getting similar information. The
4405 * only thing required is to clear the interrupt by
4406 * reading the ISR in the global resources. The
4407 * JAGCore will do a re-try on the request. Normally
4408 * you should never see this interrupt. If you start
4409 * to see this interrupt occurring frequently then
4410 * something bad has occurred. A reset might be the
4411 * thing to do.
4412 */
4413 /* TRAP();*/
4414
4415 dev_warn(&adapter->pdev->dev,
4416 "RxDMA_ERR interrupt, error %x\n",
4417 readl(&iomem->txmac.tx_test));
4418 }
4419
4420 /* Handle the Wake on LAN Event */
4421 if (status & ET_INTR_WOL) {
4422 /*
4423 * This is a secondary interrupt for wake on LAN.
4424 * The driver should never see this, if it does,
4425 * something serious is wrong. We will TRAP the
4426 * message when we are in DBG mode, otherwise we
4427 * will ignore it.
4428 */
4429 dev_err(&adapter->pdev->dev, "WAKE_ON_LAN interrupt\n");
4430 }
4431
4432 /* Let's move on to the TxMac */
4433 if (status & ET_INTR_TXMAC) {
4434 u32 err = readl(&iomem->txmac.err);
4435
4436 /*
4437 * When any of the errors occur and TXMAC generates
4438 * an interrupt to report these errors, it usually
4439 * means that TXMAC has detected an error in the data
4440 * stream retrieved from the on-chip Tx Q. All of
4441 * these errors are catastrophic and TXMAC won't be
4442 * able to recover data when these errors occur. In
4443 * a nutshell, the whole Tx path will have to be reset
4444 * and re-configured afterwards.
4445 */
4446 dev_warn(&adapter->pdev->dev,
4447 "TXMAC interrupt, error 0x%08x\n",
4448 err);
4449
4450 /* If we are debugging, we want to see this error,
4451 * otherwise we just want the device to be reset and
4452 * continue
4453 */
4454 }
4455
4456 /* Handle RXMAC Interrupt */
4457 if (status & ET_INTR_RXMAC) {
4458 /*
4459 * These interrupts are catastrophic to the device,
4460 * what we need to do is disable the interrupts and
4461 * set the flag to cause us to reset so we can solve
4462 * this issue.
4463 */
4464 /* MP_SET_FLAG( adapter,
4465 fMP_ADAPTER_HARDWARE_ERROR); */
4466
4467 dev_warn(&adapter->pdev->dev,
4468 "RXMAC interrupt, error 0x%08x. Requesting reset\n",
4469 readl(&iomem->rxmac.err_reg));
4470
4471 dev_warn(&adapter->pdev->dev,
4472 "Enable 0x%08x, Diag 0x%08x\n",
4473 readl(&iomem->rxmac.ctrl),
4474 readl(&iomem->rxmac.rxq_diag));
4475
4476 /*
4477 * If we are debugging, we want to see this error,
4478 * otherwise we just want the device to be reset and
4479 * continue
4480 */
4481 }
4482
4483 /* Handle MAC_STAT Interrupt */
4484 if (status & ET_INTR_MAC_STAT) {
4485 /*
4486 * This means at least one of the un-masked counters
4487 * in the MAC_STAT block has rolled over. Use this
4488 * to maintain the top, software managed bits of the
4489 * counter(s).
4490 */
4491 et1310_handle_macstat_interrupt(adapter);
4492 }
4493
4494 /* Handle SLV Timeout Interrupt */
4495 if (status & ET_INTR_SLV_TIMEOUT) {
4496 /*
4497 * This means a timeout has occurred on a read or
4498 * write request to one of the JAGCore registers. The
4499 * Global Resources block has terminated the request
4500 * and on a read request, returned a "fake" value.
4501 * The most likely reasons are: Bad Address or the
4502 * addressed module is in a power-down state and
4503 * can't respond.
4504 */
4505 }
4506 }
4507 et131x_enable_interrupts(adapter);
4508}
4509
d2796743
ME
4510/**
4511 * et131x_stats - Return the current device statistics.
4512 * @netdev: device whose stats are being queried
4513 *
4514 * Returns 0 on success, errno on failure (as defined in errno.h)
4515 */
4516static struct net_device_stats *et131x_stats(struct net_device *netdev)
4517{
4518 struct et131x_adapter *adapter = netdev_priv(netdev);
4519 struct net_device_stats *stats = &adapter->net_stats;
4520 struct ce_stats *devstat = &adapter->stats;
4521
4522 stats->rx_errors = devstat->rx_length_errs +
4523 devstat->rx_align_errs +
4524 devstat->rx_crc_errs +
4525 devstat->rx_code_violations +
4526 devstat->rx_other_errs;
4527 stats->tx_errors = devstat->tx_max_pkt_errs;
4528 stats->multicast = devstat->multicast_pkts_rcvd;
4529 stats->collisions = devstat->tx_collisions;
4530
4531 stats->rx_length_errors = devstat->rx_length_errs;
4532 stats->rx_over_errors = devstat->rx_overflows;
4533 stats->rx_crc_errors = devstat->rx_crc_errs;
4534
4535 /* NOTE: These stats don't have corresponding values in CE_STATS,
4536 * so we're going to have to update these directly from within the
4537 * TX/RX code
4538 */
4539 /* stats->rx_bytes = 20; devstat->; */
4540 /* stats->tx_bytes = 20; devstat->; */
4541 /* stats->rx_dropped = devstat->; */
4542 /* stats->tx_dropped = devstat->; */
4543
4544 /* NOTE: Not used, can't find analogous statistics */
4545 /* stats->rx_frame_errors = devstat->; */
4546 /* stats->rx_fifo_errors = devstat->; */
4547 /* stats->rx_missed_errors = devstat->; */
4548
4549 /* stats->tx_aborted_errors = devstat->; */
4550 /* stats->tx_carrier_errors = devstat->; */
4551 /* stats->tx_fifo_errors = devstat->; */
4552 /* stats->tx_heartbeat_errors = devstat->; */
4553 /* stats->tx_window_errors = devstat->; */
4554 return stats;
4555}
4556
d2796743
ME
4557/**
4558 * et131x_open - Open the device for use.
4559 * @netdev: device to be opened
4560 *
4561 * Returns 0 on success, errno on failure (as defined in errno.h)
4562 */
eb7a6ca6 4563static int et131x_open(struct net_device *netdev)
d2796743 4564{
d2796743 4565 struct et131x_adapter *adapter = netdev_priv(netdev);
5f3eb881
FR
4566 struct pci_dev *pdev = adapter->pdev;
4567 unsigned int irq = pdev->irq;
4568 int result;
d2796743
ME
4569
4570 /* Start the timer to track NIC errors */
4571 init_timer(&adapter->error_timer);
4572 adapter->error_timer.expires = jiffies + TX_ERROR_PERIOD * HZ / 1000;
4573 adapter->error_timer.function = et131x_error_timer_handler;
4574 adapter->error_timer.data = (unsigned long)adapter;
4575 add_timer(&adapter->error_timer);
4576
bf3313a1 4577 result = request_irq(irq, et131x_isr,
4578 IRQF_SHARED, netdev->name, netdev);
d2796743 4579 if (result) {
5f3eb881 4580 dev_err(&pdev->dev, "could not register IRQ %d\n", irq);
d2796743
ME
4581 return result;
4582 }
4583
4584 adapter->flags |= fMP_ADAPTER_INTERRUPT_IN_USE;
4585
4586 et131x_up(netdev);
4587
4588 return result;
4589}
4590
d2796743
ME
4591/**
4592 * et131x_close - Close the device
4593 * @netdev: device to be closed
4594 *
4595 * Returns 0 on success, errno on failure (as defined in errno.h)
4596 */
eb7a6ca6 4597static int et131x_close(struct net_device *netdev)
d2796743
ME
4598{
4599 struct et131x_adapter *adapter = netdev_priv(netdev);
4600
4601 et131x_down(netdev);
4602
4603 adapter->flags &= ~fMP_ADAPTER_INTERRUPT_IN_USE;
5f3eb881 4604 free_irq(adapter->pdev->irq, netdev);
d2796743
ME
4605
4606 /* Stop the error timer */
4607 return del_timer_sync(&adapter->error_timer);
4608}
4609
4610/**
4611 * et131x_ioctl - The I/O Control handler for the driver
4612 * @netdev: device on which the control request is being made
4613 * @reqbuf: a pointer to the IOCTL request buffer
4614 * @cmd: the IOCTL command code
4615 *
4616 * Returns 0 on success, errno on failure (as defined in errno.h)
4617 */
09a3fc2b
ME
4618static int et131x_ioctl(struct net_device *netdev, struct ifreq *reqbuf,
4619 int cmd)
d2796743
ME
4620{
4621 struct et131x_adapter *adapter = netdev_priv(netdev);
4622
4623 if (!adapter->phydev)
4624 return -EINVAL;
4625
4626 return phy_mii_ioctl(adapter->phydev, reqbuf, cmd);
4627}
4628
4629/**
4630 * et131x_set_packet_filter - Configures the Rx Packet filtering on the device
4631 * @adapter: pointer to our private adapter structure
4632 *
4633 * FIXME: lot of dups with MAC code
4634 *
4635 * Returns 0 on success, errno on failure
4636 */
4637static int et131x_set_packet_filter(struct et131x_adapter *adapter)
4638{
834d0ee3 4639 int filter = adapter->packet_filter;
d2796743 4640 int status = 0;
d2796743
ME
4641 u32 ctrl;
4642 u32 pf_ctrl;
4643
4644 ctrl = readl(&adapter->regs->rxmac.ctrl);
4645 pf_ctrl = readl(&adapter->regs->rxmac.pf_ctrl);
4646
4647 /* Default to disabled packet filtering. Enable it in the individual
4648 * case statements that require the device to filter something
4649 */
4650 ctrl |= 0x04;
4651
4652 /* Set us to be in promiscuous mode so we receive everything, this
4653 * is also true when we get a packet filter of 0
4654 */
4655 if ((filter & ET131X_PACKET_TYPE_PROMISCUOUS) || filter == 0)
4656 pf_ctrl &= ~7; /* Clear filter bits */
4657 else {
4658 /*
4659 * Set us up with Multicast packet filtering. Three cases are
4660 * possible - (1) we have a multi-cast list, (2) we receive ALL
4661 * multicast entries or (3) we receive none.
4662 */
4663 if (filter & ET131X_PACKET_TYPE_ALL_MULTICAST)
4664 pf_ctrl &= ~2; /* Multicast filter bit */
4665 else {
4666 et1310_setup_device_for_multicast(adapter);
4667 pf_ctrl |= 2;
4668 ctrl &= ~0x04;
4669 }
4670
4671 /* Set us up with Unicast packet filtering */
4672 if (filter & ET131X_PACKET_TYPE_DIRECTED) {
4673 et1310_setup_device_for_unicast(adapter);
4674 pf_ctrl |= 4;
4675 ctrl &= ~0x04;
4676 }
4677
4678 /* Set us up with Broadcast packet filtering */
4679 if (filter & ET131X_PACKET_TYPE_BROADCAST) {
4680 pf_ctrl |= 1; /* Broadcast filter bit */
4681 ctrl &= ~0x04;
4682 } else
4683 pf_ctrl &= ~1;
4684
4685 /* Setup the receive mac configuration registers - Packet
4686 * Filter control + the enable / disable for packet filter
4687 * in the control reg.
4688 */
4689 writel(pf_ctrl, &adapter->regs->rxmac.pf_ctrl);
4690 writel(ctrl, &adapter->regs->rxmac.ctrl);
4691 }
4692 return status;
4693}
4694
4695/**
4696 * et131x_multicast - The handler to configure multicasting on the interface
4697 * @netdev: a pointer to a net_device struct representing the device
4698 */
4699static void et131x_multicast(struct net_device *netdev)
4700{
4701 struct et131x_adapter *adapter = netdev_priv(netdev);
834d0ee3 4702 int packet_filter;
d2796743
ME
4703 unsigned long flags;
4704 struct netdev_hw_addr *ha;
4705 int i;
4706
4707 spin_lock_irqsave(&adapter->lock, flags);
4708
4709 /* Before we modify the platform-independent filter flags, store them
4710 * locally. This allows us to determine if anything's changed and if
4711 * we even need to bother the hardware
4712 */
4713 packet_filter = adapter->packet_filter;
4714
4715 /* Clear the 'multicast' flag locally; because we only have a single
4716 * flag to check multicast, and multiple multicast addresses can be
4717 * set, this is the easiest way to determine if more than one
4718 * multicast address is being set.
4719 */
4720 packet_filter &= ~ET131X_PACKET_TYPE_MULTICAST;
4721
4722 /* Check the net_device flags and set the device independent flags
4723 * accordingly
4724 */
4725
4726 if (netdev->flags & IFF_PROMISC)
4727 adapter->packet_filter |= ET131X_PACKET_TYPE_PROMISCUOUS;
4728 else
4729 adapter->packet_filter &= ~ET131X_PACKET_TYPE_PROMISCUOUS;
4730
4731 if (netdev->flags & IFF_ALLMULTI)
4732 adapter->packet_filter |= ET131X_PACKET_TYPE_ALL_MULTICAST;
4733
4734 if (netdev_mc_count(netdev) > NIC_MAX_MCAST_LIST)
4735 adapter->packet_filter |= ET131X_PACKET_TYPE_ALL_MULTICAST;
4736
4737 if (netdev_mc_count(netdev) < 1) {
4738 adapter->packet_filter &= ~ET131X_PACKET_TYPE_ALL_MULTICAST;
4739 adapter->packet_filter &= ~ET131X_PACKET_TYPE_MULTICAST;
4740 } else
4741 adapter->packet_filter |= ET131X_PACKET_TYPE_MULTICAST;
4742
4743 /* Set values in the private adapter struct */
4744 i = 0;
4745 netdev_for_each_mc_addr(ha, netdev) {
4746 if (i == NIC_MAX_MCAST_LIST)
4747 break;
4748 memcpy(adapter->multicast_list[i++], ha->addr, ETH_ALEN);
4749 }
4750 adapter->multicast_addr_count = i;
4751
4752 /* Are the new flags different from the previous ones? If not, then no
4753 * action is required
4754 *
4755 * NOTE - This block will always update the multicast_list with the
4756 * hardware, even if the addresses aren't the same.
4757 */
4758 if (packet_filter != adapter->packet_filter) {
4759 /* Call the device's filter function */
4760 et131x_set_packet_filter(adapter);
4761 }
4762 spin_unlock_irqrestore(&adapter->lock, flags);
4763}
4764
4765/**
4766 * et131x_tx - The handler to tx a packet on the device
4767 * @skb: data to be Tx'd
4768 * @netdev: device on which data is to be Tx'd
4769 *
4770 * Returns 0 on success, errno on failure (as defined in errno.h)
4771 */
4772static int et131x_tx(struct sk_buff *skb, struct net_device *netdev)
4773{
4774 int status = 0;
06709e96
ME
4775 struct et131x_adapter *adapter = netdev_priv(netdev);
4776
4777 /* stop the queue if it's getting full */
09a3fc2b
ME
4778 if (adapter->tx_ring.used >= NUM_TCB - 1 &&
4779 !netif_queue_stopped(netdev))
06709e96 4780 netif_stop_queue(netdev);
d2796743
ME
4781
4782 /* Save the timestamp for the TX timeout watchdog */
4783 netdev->trans_start = jiffies;
4784
4785 /* Call the device-specific data Tx routine */
4786 status = et131x_send_packets(skb, netdev);
4787
4788 /* Check status and manage the netif queue if necessary */
4789 if (status != 0) {
09a3fc2b 4790 if (status == -ENOMEM)
d2796743 4791 status = NETDEV_TX_BUSY;
09a3fc2b 4792 else
d2796743 4793 status = NETDEV_TX_OK;
d2796743
ME
4794 }
4795 return status;
4796}
4797
4798/**
4799 * et131x_tx_timeout - Timeout handler
4800 * @netdev: a pointer to a net_device struct representing the device
4801 *
4802 * The handler called when a Tx request times out. The timeout period is
4803 * specified by the 'tx_timeo" element in the net_device structure (see
4804 * et131x_alloc_device() to see how this value is set).
4805 */
4806static void et131x_tx_timeout(struct net_device *netdev)
4807{
4808 struct et131x_adapter *adapter = netdev_priv(netdev);
4809 struct tcb *tcb;
4810 unsigned long flags;
4811
4812 /* If the device is closed, ignore the timeout */
4813 if (~(adapter->flags & fMP_ADAPTER_INTERRUPT_IN_USE))
4814 return;
4815
4816 /* Any nonrecoverable hardware error?
4817 * Checks adapter->flags for any failure in phy reading
4818 */
4819 if (adapter->flags & fMP_ADAPTER_NON_RECOVER_ERROR)
4820 return;
4821
4822 /* Hardware failure? */
4823 if (adapter->flags & fMP_ADAPTER_HARDWARE_ERROR) {
4824 dev_err(&adapter->pdev->dev, "hardware error - reset\n");
4825 return;
4826 }
4827
4828 /* Is send stuck? */
4829 spin_lock_irqsave(&adapter->tcb_send_qlock, flags);
4830
4831 tcb = adapter->tx_ring.send_head;
4832
4833 if (tcb != NULL) {
4834 tcb->count++;
4835
4836 if (tcb->count > NIC_SEND_HANG_THRESHOLD) {
4837 spin_unlock_irqrestore(&adapter->tcb_send_qlock,
4838 flags);
4839
4840 dev_warn(&adapter->pdev->dev,
4841 "Send stuck - reset. tcb->WrIndex %x, flags 0x%08x\n",
4842 tcb->index,
4843 tcb->flags);
4844
4845 adapter->net_stats.tx_errors++;
4846
4847 /* perform reset of tx/rx */
4848 et131x_disable_txrx(netdev);
4849 et131x_enable_txrx(netdev);
4850 return;
4851 }
4852 }
4853
4854 spin_unlock_irqrestore(&adapter->tcb_send_qlock, flags);
4855}
4856
4857/**
4858 * et131x_change_mtu - The handler called to change the MTU for the device
4859 * @netdev: device whose MTU is to be changed
4860 * @new_mtu: the desired MTU
4861 *
4862 * Returns 0 on success, errno on failure (as defined in errno.h)
4863 */
4864static int et131x_change_mtu(struct net_device *netdev, int new_mtu)
4865{
4866 int result = 0;
4867 struct et131x_adapter *adapter = netdev_priv(netdev);
4868
4869 /* Make sure the requested MTU is valid */
4870 if (new_mtu < 64 || new_mtu > 9216)
4871 return -EINVAL;
4872
4873 et131x_disable_txrx(netdev);
4874 et131x_handle_send_interrupt(adapter);
4875 et131x_handle_recv_interrupt(adapter);
4876
4877 /* Set the new MTU */
4878 netdev->mtu = new_mtu;
4879
4880 /* Free Rx DMA memory */
4881 et131x_adapter_memory_free(adapter);
4882
4883 /* Set the config parameter for Jumbo Packet support */
4884 adapter->registry_jumbo_packet = new_mtu + 14;
4885 et131x_soft_reset(adapter);
4886
4887 /* Alloc and init Rx DMA memory */
4888 result = et131x_adapter_memory_alloc(adapter);
4889 if (result != 0) {
4890 dev_warn(&adapter->pdev->dev,
4891 "Change MTU failed; couldn't re-alloc DMA memory\n");
4892 return result;
4893 }
4894
4895 et131x_init_send(adapter);
4896
4897 et131x_hwaddr_init(adapter);
4898 memcpy(netdev->dev_addr, adapter->addr, ETH_ALEN);
4899
4900 /* Init the device with the new settings */
4901 et131x_adapter_setup(adapter);
4902
4903 et131x_enable_txrx(netdev);
4904
4905 return result;
4906}
4907
4908/**
4909 * et131x_set_mac_addr - handler to change the MAC address for the device
4910 * @netdev: device whose MAC is to be changed
4911 * @new_mac: the desired MAC address
4912 *
4913 * Returns 0 on success, errno on failure (as defined in errno.h)
4914 *
4915 * IMPLEMENTED BY : blux http://berndlux.de 22.01.2007 21:14
4916 */
4917static int et131x_set_mac_addr(struct net_device *netdev, void *new_mac)
4918{
4919 int result = 0;
4920 struct et131x_adapter *adapter = netdev_priv(netdev);
4921 struct sockaddr *address = new_mac;
4922
4923 /* begin blux */
4924
4925 if (adapter == NULL)
4926 return -ENODEV;
4927
4928 /* Make sure the requested MAC is valid */
4929 if (!is_valid_ether_addr(address->sa_data))
d8aa3e26 4930 return -EADDRNOTAVAIL;
d2796743
ME
4931
4932 et131x_disable_txrx(netdev);
4933 et131x_handle_send_interrupt(adapter);
4934 et131x_handle_recv_interrupt(adapter);
4935
4936 /* Set the new MAC */
4937 /* netdev->set_mac_address = &new_mac; */
4938
4939 memcpy(netdev->dev_addr, address->sa_data, netdev->addr_len);
4940
e58b89da
TY
4941 netdev_info(netdev, "Setting MAC address to %pM\n",
4942 netdev->dev_addr);
d2796743
ME
4943
4944 /* Free Rx DMA memory */
4945 et131x_adapter_memory_free(adapter);
4946
4947 et131x_soft_reset(adapter);
4948
4949 /* Alloc and init Rx DMA memory */
4950 result = et131x_adapter_memory_alloc(adapter);
4951 if (result != 0) {
4952 dev_err(&adapter->pdev->dev,
4953 "Change MAC failed; couldn't re-alloc DMA memory\n");
4954 return result;
4955 }
4956
4957 et131x_init_send(adapter);
4958
4959 et131x_hwaddr_init(adapter);
4960
4961 /* Init the device with the new settings */
4962 et131x_adapter_setup(adapter);
4963
4964 et131x_enable_txrx(netdev);
4965
4966 return result;
4967}
4968
4969static const struct net_device_ops et131x_netdev_ops = {
4970 .ndo_open = et131x_open,
4971 .ndo_stop = et131x_close,
4972 .ndo_start_xmit = et131x_tx,
aa77677e 4973 .ndo_set_rx_mode = et131x_multicast,
d2796743
ME
4974 .ndo_tx_timeout = et131x_tx_timeout,
4975 .ndo_change_mtu = et131x_change_mtu,
4976 .ndo_set_mac_address = et131x_set_mac_addr,
4977 .ndo_validate_addr = eth_validate_addr,
4978 .ndo_get_stats = et131x_stats,
4979 .ndo_do_ioctl = et131x_ioctl,
4980};
4981
5da2b158
ME
4982/**
4983 * et131x_pci_setup - Perform device initialization
4984 * @pdev: a pointer to the device's pci_dev structure
4985 * @ent: this device's entry in the pci_device_id table
4986 *
4987 * Returns 0 on success, errno on failure (as defined in errno.h)
4988 *
4989 * Registered in the pci_driver structure, this function is called when the
4990 * PCI subsystem finds a new PCI device which matches the information
4991 * contained in the pci_device_id table. This routine is the equivalent to
4992 * a device insertion routine.
4993 */
4994static int __devinit et131x_pci_setup(struct pci_dev *pdev,
4995 const struct pci_device_id *ent)
4996{
5da2b158
ME
4997 struct net_device *netdev;
4998 struct et131x_adapter *adapter;
fa9f0a65 4999 int rc;
5da2b158
ME
5000 int ii;
5001
fa9f0a65
FR
5002 rc = pci_enable_device(pdev);
5003 if (rc < 0) {
5da2b158 5004 dev_err(&pdev->dev, "pci_enable_device() failed\n");
fa9f0a65 5005 goto out;
5da2b158
ME
5006 }
5007
5008 /* Perform some basic PCI checks */
5009 if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
5010 dev_err(&pdev->dev, "Can't find PCI device's base address\n");
fa9f0a65 5011 rc = -ENODEV;
5da2b158
ME
5012 goto err_disable;
5013 }
5014
fa9f0a65
FR
5015 rc = pci_request_regions(pdev, DRIVER_NAME);
5016 if (rc < 0) {
5da2b158
ME
5017 dev_err(&pdev->dev, "Can't get PCI resources\n");
5018 goto err_disable;
5019 }
5020
5021 pci_set_master(pdev);
5022
5023 /* Check the DMA addressing support of this device */
5024 if (!dma_set_mask(&pdev->dev, DMA_BIT_MASK(64))) {
fa9f0a65
FR
5025 rc = dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(64));
5026 if (rc < 0) {
5da2b158
ME
5027 dev_err(&pdev->dev,
5028 "Unable to obtain 64 bit DMA for consistent allocations\n");
5029 goto err_release_res;
5030 }
5031 } else if (!dma_set_mask(&pdev->dev, DMA_BIT_MASK(32))) {
fa9f0a65
FR
5032 rc = dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(32));
5033 if (rc < 0) {
5da2b158
ME
5034 dev_err(&pdev->dev,
5035 "Unable to obtain 32 bit DMA for consistent allocations\n");
5036 goto err_release_res;
5037 }
5038 } else {
5039 dev_err(&pdev->dev, "No usable DMA addressing method\n");
fa9f0a65 5040 rc = -EIO;
5da2b158
ME
5041 goto err_release_res;
5042 }
5043
5044 /* Allocate netdev and private adapter structs */
fa9f0a65 5045 netdev = alloc_etherdev(sizeof(struct et131x_adapter));
5da2b158
ME
5046 if (!netdev) {
5047 dev_err(&pdev->dev, "Couldn't alloc netdev struct\n");
fa9f0a65 5048 rc = -ENOMEM;
5da2b158
ME
5049 goto err_release_res;
5050 }
5051
fa9f0a65
FR
5052 netdev->watchdog_timeo = ET131X_TX_TIMEOUT;
5053 netdev->netdev_ops = &et131x_netdev_ops;
5054
5da2b158 5055 SET_NETDEV_DEV(netdev, &pdev->dev);
a6e28b34 5056 SET_ETHTOOL_OPS(netdev, &et131x_ethtool_ops);
5da2b158
ME
5057
5058 adapter = et131x_adapter_init(netdev, pdev);
5059
fa9f0a65
FR
5060 rc = et131x_pci_init(adapter, pdev);
5061 if (rc < 0)
5062 goto err_free_dev;
5da2b158
ME
5063
5064 /* Map the bus-relative registers to system virtual memory */
5065 adapter->regs = pci_ioremap_bar(pdev, 0);
5066 if (!adapter->regs) {
5067 dev_err(&pdev->dev, "Cannot map device registers\n");
fa9f0a65 5068 rc = -ENOMEM;
5da2b158
ME
5069 goto err_free_dev;
5070 }
5071
5072 /* If Phy COMA mode was enabled when we went down, disable it here. */
5073 writel(ET_PMCSR_INIT, &adapter->regs->global.pm_csr);
5074
5075 /* Issue a global reset to the et1310 */
5076 et131x_soft_reset(adapter);
5077
5078 /* Disable all interrupts (paranoid) */
5079 et131x_disable_interrupts(adapter);
5080
5081 /* Allocate DMA memory */
fa9f0a65
FR
5082 rc = et131x_adapter_memory_alloc(adapter);
5083 if (rc < 0) {
5da2b158
ME
5084 dev_err(&pdev->dev, "Could not alloc adapater memory (DMA)\n");
5085 goto err_iounmap;
5086 }
5087
5088 /* Init send data structures */
5089 et131x_init_send(adapter);
5090
5091 /* Set up the task structure for the ISR's deferred handler */
5092 INIT_WORK(&adapter->task, et131x_isr_handler);
5093
5094 /* Copy address into the net_device struct */
5095 memcpy(netdev->dev_addr, adapter->addr, ETH_ALEN);
5096
5097 /* Init variable for counting how long we do not have link status */
5098 adapter->boot_coma = 0;
5099 et1310_disable_phy_coma(adapter);
5100
fa9f0a65
FR
5101 rc = -ENOMEM;
5102
5da2b158
ME
5103 /* Setup the mii_bus struct */
5104 adapter->mii_bus = mdiobus_alloc();
5105 if (!adapter->mii_bus) {
5106 dev_err(&pdev->dev, "Alloc of mii_bus struct failed\n");
5107 goto err_mem_free;
5108 }
5109
5110 adapter->mii_bus->name = "et131x_eth_mii";
5111 snprintf(adapter->mii_bus->id, MII_BUS_ID_SIZE, "%x",
5112 (adapter->pdev->bus->number << 8) | adapter->pdev->devfn);
5113 adapter->mii_bus->priv = netdev;
5114 adapter->mii_bus->read = et131x_mdio_read;
5115 adapter->mii_bus->write = et131x_mdio_write;
5116 adapter->mii_bus->reset = et131x_mdio_reset;
5117 adapter->mii_bus->irq = kmalloc(sizeof(int)*PHY_MAX_ADDR, GFP_KERNEL);
5118 if (!adapter->mii_bus->irq) {
5119 dev_err(&pdev->dev, "mii_bus irq allocation failed\n");
5120 goto err_mdio_free;
5121 }
5122
5123 for (ii = 0; ii < PHY_MAX_ADDR; ii++)
5124 adapter->mii_bus->irq[ii] = PHY_POLL;
5125
fa9f0a65
FR
5126 rc = mdiobus_register(adapter->mii_bus);
5127 if (rc < 0) {
5da2b158 5128 dev_err(&pdev->dev, "failed to register MII bus\n");
5da2b158
ME
5129 goto err_mdio_free_irq;
5130 }
5131
fa9f0a65
FR
5132 rc = et131x_mii_probe(netdev);
5133 if (rc < 0) {
5da2b158
ME
5134 dev_err(&pdev->dev, "failed to probe MII bus\n");
5135 goto err_mdio_unregister;
5136 }
5137
5138 /* Setup et1310 as per the documentation */
5139 et131x_adapter_setup(adapter);
5140
5141 /* We can enable interrupts now
5142 *
5143 * NOTE - Because registration of interrupt handler is done in the
5144 * device's open(), defer enabling device interrupts to that
5145 * point
5146 */
5147
5148 /* Register the net_device struct with the Linux network layer */
fa9f0a65
FR
5149 rc = register_netdev(netdev);
5150 if (rc < 0) {
5da2b158 5151 dev_err(&pdev->dev, "register_netdev() failed\n");
fa9f0a65 5152 goto err_phy_disconnect;
5da2b158
ME
5153 }
5154
5155 /* Register the net_device struct with the PCI subsystem. Save a copy
5156 * of the PCI config space for this device now that the device has
5157 * been initialized, just in case it needs to be quickly restored.
5158 */
5159 pci_set_drvdata(pdev, netdev);
fa9f0a65
FR
5160out:
5161 return rc;
5da2b158 5162
fa9f0a65
FR
5163err_phy_disconnect:
5164 phy_disconnect(adapter->phydev);
5da2b158
ME
5165err_mdio_unregister:
5166 mdiobus_unregister(adapter->mii_bus);
5167err_mdio_free_irq:
5168 kfree(adapter->mii_bus->irq);
5169err_mdio_free:
5170 mdiobus_free(adapter->mii_bus);
5171err_mem_free:
5172 et131x_adapter_memory_free(adapter);
5173err_iounmap:
5174 iounmap(adapter->regs);
5175err_free_dev:
5176 pci_dev_put(pdev);
5177 free_netdev(netdev);
5178err_release_res:
5179 pci_release_regions(pdev);
5180err_disable:
5181 pci_disable_device(pdev);
fa9f0a65 5182 goto out;
5da2b158
ME
5183}
5184
5da2b158
ME
5185static DEFINE_PCI_DEVICE_TABLE(et131x_pci_table) = {
5186 { PCI_VDEVICE(ATT, ET131X_PCI_DEVICE_ID_GIG), 0UL},
5187 { PCI_VDEVICE(ATT, ET131X_PCI_DEVICE_ID_FAST), 0UL},
5188 {0,}
5189};
5190MODULE_DEVICE_TABLE(pci, et131x_pci_table);
5191
5192static struct pci_driver et131x_driver = {
5193 .name = DRIVER_NAME,
5194 .id_table = et131x_pci_table,
5195 .probe = et131x_pci_setup,
5196 .remove = __devexit_p(et131x_pci_remove),
5197 .driver.pm = ET131X_PM_OPS,
5198};
5199
89812b1f 5200module_pci_driver(et131x_driver);
This page took 0.37521 seconds and 5 git commands to generate.