GNU Linux-libre 5.10.215-gnu1
[releases.git] / drivers / net / ethernet / intel / e100.c
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright(c) 1999 - 2006 Intel Corporation. */
3
4 /*
5  *      e100.c: Intel(R) PRO/100 ethernet driver
6  *
7  *      (Re)written 2003 by scott.feldman@intel.com.  Based loosely on
8  *      original e100 driver, but better described as a munging of
9  *      e100, e1000, eepro100, tg3, 8139cp, and other drivers.
10  *
11  *      References:
12  *              Intel 8255x 10/100 Mbps Ethernet Controller Family,
13  *              Open Source Software Developers Manual,
14  *              http://sourceforge.net/projects/e1000
15  *
16  *
17  *                            Theory of Operation
18  *
19  *      I.   General
20  *
21  *      The driver supports Intel(R) 10/100 Mbps PCI Fast Ethernet
22  *      controller family, which includes the 82557, 82558, 82559, 82550,
23  *      82551, and 82562 devices.  82558 and greater controllers
24  *      integrate the Intel 82555 PHY.  The controllers are used in
25  *      server and client network interface cards, as well as in
26  *      LAN-On-Motherboard (LOM), CardBus, MiniPCI, and ICHx
27  *      configurations.  8255x supports a 32-bit linear addressing
28  *      mode and operates at 33Mhz PCI clock rate.
29  *
30  *      II.  Driver Operation
31  *
32  *      Memory-mapped mode is used exclusively to access the device's
33  *      shared-memory structure, the Control/Status Registers (CSR). All
34  *      setup, configuration, and control of the device, including queuing
35  *      of Tx, Rx, and configuration commands is through the CSR.
36  *      cmd_lock serializes accesses to the CSR command register.  cb_lock
37  *      protects the shared Command Block List (CBL).
38  *
39  *      8255x is highly MII-compliant and all access to the PHY go
40  *      through the Management Data Interface (MDI).  Consequently, the
41  *      driver leverages the mii.c library shared with other MII-compliant
42  *      devices.
43  *
44  *      Big- and Little-Endian byte order as well as 32- and 64-bit
45  *      archs are supported.  Weak-ordered memory and non-cache-coherent
46  *      archs are supported.
47  *
48  *      III. Transmit
49  *
50  *      A Tx skb is mapped and hangs off of a TCB.  TCBs are linked
51  *      together in a fixed-size ring (CBL) thus forming the flexible mode
52  *      memory structure.  A TCB marked with the suspend-bit indicates
53  *      the end of the ring.  The last TCB processed suspends the
54  *      controller, and the controller can be restarted by issue a CU
55  *      resume command to continue from the suspend point, or a CU start
56  *      command to start at a given position in the ring.
57  *
58  *      Non-Tx commands (config, multicast setup, etc) are linked
59  *      into the CBL ring along with Tx commands.  The common structure
60  *      used for both Tx and non-Tx commands is the Command Block (CB).
61  *
62  *      cb_to_use is the next CB to use for queuing a command; cb_to_clean
63  *      is the next CB to check for completion; cb_to_send is the first
64  *      CB to start on in case of a previous failure to resume.  CB clean
65  *      up happens in interrupt context in response to a CU interrupt.
66  *      cbs_avail keeps track of number of free CB resources available.
67  *
68  *      Hardware padding of short packets to minimum packet size is
69  *      enabled.  82557 pads with 7Eh, while the later controllers pad
70  *      with 00h.
71  *
72  *      IV.  Receive
73  *
74  *      The Receive Frame Area (RFA) comprises a ring of Receive Frame
75  *      Descriptors (RFD) + data buffer, thus forming the simplified mode
76  *      memory structure.  Rx skbs are allocated to contain both the RFD
77  *      and the data buffer, but the RFD is pulled off before the skb is
78  *      indicated.  The data buffer is aligned such that encapsulated
79  *      protocol headers are u32-aligned.  Since the RFD is part of the
80  *      mapped shared memory, and completion status is contained within
81  *      the RFD, the RFD must be dma_sync'ed to maintain a consistent
82  *      view from software and hardware.
83  *
84  *      In order to keep updates to the RFD link field from colliding with
85  *      hardware writes to mark packets complete, we use the feature that
86  *      hardware will not write to a size 0 descriptor and mark the previous
87  *      packet as end-of-list (EL).   After updating the link, we remove EL
88  *      and only then restore the size such that hardware may use the
89  *      previous-to-end RFD.
90  *
91  *      Under typical operation, the  receive unit (RU) is start once,
92  *      and the controller happily fills RFDs as frames arrive.  If
93  *      replacement RFDs cannot be allocated, or the RU goes non-active,
94  *      the RU must be restarted.  Frame arrival generates an interrupt,
95  *      and Rx indication and re-allocation happen in the same context,
96  *      therefore no locking is required.  A software-generated interrupt
97  *      is generated from the watchdog to recover from a failed allocation
98  *      scenario where all Rx resources have been indicated and none re-
99  *      placed.
100  *
101  *      V.   Miscellaneous
102  *
103  *      VLAN offloading of tagging, stripping and filtering is not
104  *      supported, but driver will accommodate the extra 4-byte VLAN tag
105  *      for processing by upper layers.  Tx/Rx Checksum offloading is not
106  *      supported.  Tx Scatter/Gather is not supported.  Jumbo Frames is
107  *      not supported (hardware limitation).
108  *
109  *      MagicPacket(tm) WoL support is enabled/disabled via ethtool.
110  *
111  *      Thanks to JC (jchapman@katalix.com) for helping with
112  *      testing/troubleshooting the development driver.
113  *
114  *      TODO:
115  *      o several entry points race with dev->close
116  *      o check for tx-no-resources/stop Q races with tx clean/wake Q
117  *
118  *      FIXES:
119  * 2005/12/02 - Michael O'Donnell <Michael.ODonnell at stratus dot com>
120  *      - Stratus87247: protect MDI control register manipulations
121  * 2009/06/01 - Andreas Mohr <andi at lisas dot de>
122  *      - add clean lowlevel I/O emulation for cards with MII-lacking PHYs
123  */
124
125 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
126
127 #include <linux/hardirq.h>
128 #include <linux/interrupt.h>
129 #include <linux/module.h>
130 #include <linux/moduleparam.h>
131 #include <linux/kernel.h>
132 #include <linux/types.h>
133 #include <linux/sched.h>
134 #include <linux/slab.h>
135 #include <linux/delay.h>
136 #include <linux/init.h>
137 #include <linux/pci.h>
138 #include <linux/dma-mapping.h>
139 #include <linux/dmapool.h>
140 #include <linux/netdevice.h>
141 #include <linux/etherdevice.h>
142 #include <linux/mii.h>
143 #include <linux/if_vlan.h>
144 #include <linux/skbuff.h>
145 #include <linux/ethtool.h>
146 #include <linux/string.h>
147 #include <linux/firmware.h>
148 #include <linux/rtnetlink.h>
149 #include <asm/unaligned.h>
150
151
152 #define DRV_NAME                "e100"
153 #define DRV_DESCRIPTION         "Intel(R) PRO/100 Network Driver"
154 #define DRV_COPYRIGHT           "Copyright(c) 1999-2006 Intel Corporation"
155
156 #define E100_WATCHDOG_PERIOD    (2 * HZ)
157 #define E100_NAPI_WEIGHT        16
158
159 #define FIRMWARE_D101M          "/*(DEBLOBBED)*/"
160 #define FIRMWARE_D101S          "/*(DEBLOBBED)*/"
161 #define FIRMWARE_D102E          "/*(DEBLOBBED)*/"
162
163 MODULE_DESCRIPTION(DRV_DESCRIPTION);
164 MODULE_AUTHOR(DRV_COPYRIGHT);
165 MODULE_LICENSE("GPL v2");
166 /*(DEBLOBBED)*/
167
168 static int debug = 3;
169 static int eeprom_bad_csum_allow = 0;
170 static int use_io = 0;
171 module_param(debug, int, 0);
172 module_param(eeprom_bad_csum_allow, int, 0);
173 module_param(use_io, int, 0);
174 MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all)");
175 MODULE_PARM_DESC(eeprom_bad_csum_allow, "Allow bad eeprom checksums");
176 MODULE_PARM_DESC(use_io, "Force use of i/o access mode");
177
178 #define INTEL_8255X_ETHERNET_DEVICE(device_id, ich) {\
179         PCI_VENDOR_ID_INTEL, device_id, PCI_ANY_ID, PCI_ANY_ID, \
180         PCI_CLASS_NETWORK_ETHERNET << 8, 0xFFFF00, ich }
181 static const struct pci_device_id e100_id_table[] = {
182         INTEL_8255X_ETHERNET_DEVICE(0x1029, 0),
183         INTEL_8255X_ETHERNET_DEVICE(0x1030, 0),
184         INTEL_8255X_ETHERNET_DEVICE(0x1031, 3),
185         INTEL_8255X_ETHERNET_DEVICE(0x1032, 3),
186         INTEL_8255X_ETHERNET_DEVICE(0x1033, 3),
187         INTEL_8255X_ETHERNET_DEVICE(0x1034, 3),
188         INTEL_8255X_ETHERNET_DEVICE(0x1038, 3),
189         INTEL_8255X_ETHERNET_DEVICE(0x1039, 4),
190         INTEL_8255X_ETHERNET_DEVICE(0x103A, 4),
191         INTEL_8255X_ETHERNET_DEVICE(0x103B, 4),
192         INTEL_8255X_ETHERNET_DEVICE(0x103C, 4),
193         INTEL_8255X_ETHERNET_DEVICE(0x103D, 4),
194         INTEL_8255X_ETHERNET_DEVICE(0x103E, 4),
195         INTEL_8255X_ETHERNET_DEVICE(0x1050, 5),
196         INTEL_8255X_ETHERNET_DEVICE(0x1051, 5),
197         INTEL_8255X_ETHERNET_DEVICE(0x1052, 5),
198         INTEL_8255X_ETHERNET_DEVICE(0x1053, 5),
199         INTEL_8255X_ETHERNET_DEVICE(0x1054, 5),
200         INTEL_8255X_ETHERNET_DEVICE(0x1055, 5),
201         INTEL_8255X_ETHERNET_DEVICE(0x1056, 5),
202         INTEL_8255X_ETHERNET_DEVICE(0x1057, 5),
203         INTEL_8255X_ETHERNET_DEVICE(0x1059, 0),
204         INTEL_8255X_ETHERNET_DEVICE(0x1064, 6),
205         INTEL_8255X_ETHERNET_DEVICE(0x1065, 6),
206         INTEL_8255X_ETHERNET_DEVICE(0x1066, 6),
207         INTEL_8255X_ETHERNET_DEVICE(0x1067, 6),
208         INTEL_8255X_ETHERNET_DEVICE(0x1068, 6),
209         INTEL_8255X_ETHERNET_DEVICE(0x1069, 6),
210         INTEL_8255X_ETHERNET_DEVICE(0x106A, 6),
211         INTEL_8255X_ETHERNET_DEVICE(0x106B, 6),
212         INTEL_8255X_ETHERNET_DEVICE(0x1091, 7),
213         INTEL_8255X_ETHERNET_DEVICE(0x1092, 7),
214         INTEL_8255X_ETHERNET_DEVICE(0x1093, 7),
215         INTEL_8255X_ETHERNET_DEVICE(0x1094, 7),
216         INTEL_8255X_ETHERNET_DEVICE(0x1095, 7),
217         INTEL_8255X_ETHERNET_DEVICE(0x10fe, 7),
218         INTEL_8255X_ETHERNET_DEVICE(0x1209, 0),
219         INTEL_8255X_ETHERNET_DEVICE(0x1229, 0),
220         INTEL_8255X_ETHERNET_DEVICE(0x2449, 2),
221         INTEL_8255X_ETHERNET_DEVICE(0x2459, 2),
222         INTEL_8255X_ETHERNET_DEVICE(0x245D, 2),
223         INTEL_8255X_ETHERNET_DEVICE(0x27DC, 7),
224         { 0, }
225 };
226 MODULE_DEVICE_TABLE(pci, e100_id_table);
227
228 enum mac {
229         mac_82557_D100_A  = 0,
230         mac_82557_D100_B  = 1,
231         mac_82557_D100_C  = 2,
232         mac_82558_D101_A4 = 4,
233         mac_82558_D101_B0 = 5,
234         mac_82559_D101M   = 8,
235         mac_82559_D101S   = 9,
236         mac_82550_D102    = 12,
237         mac_82550_D102_C  = 13,
238         mac_82551_E       = 14,
239         mac_82551_F       = 15,
240         mac_82551_10      = 16,
241         mac_unknown       = 0xFF,
242 };
243
244 enum phy {
245         phy_100a     = 0x000003E0,
246         phy_100c     = 0x035002A8,
247         phy_82555_tx = 0x015002A8,
248         phy_nsc_tx   = 0x5C002000,
249         phy_82562_et = 0x033002A8,
250         phy_82562_em = 0x032002A8,
251         phy_82562_ek = 0x031002A8,
252         phy_82562_eh = 0x017002A8,
253         phy_82552_v  = 0xd061004d,
254         phy_unknown  = 0xFFFFFFFF,
255 };
256
257 /* CSR (Control/Status Registers) */
258 struct csr {
259         struct {
260                 u8 status;
261                 u8 stat_ack;
262                 u8 cmd_lo;
263                 u8 cmd_hi;
264                 u32 gen_ptr;
265         } scb;
266         u32 port;
267         u16 flash_ctrl;
268         u8 eeprom_ctrl_lo;
269         u8 eeprom_ctrl_hi;
270         u32 mdi_ctrl;
271         u32 rx_dma_count;
272 };
273
274 enum scb_status {
275         rus_no_res       = 0x08,
276         rus_ready        = 0x10,
277         rus_mask         = 0x3C,
278 };
279
280 enum ru_state  {
281         RU_SUSPENDED = 0,
282         RU_RUNNING       = 1,
283         RU_UNINITIALIZED = -1,
284 };
285
286 enum scb_stat_ack {
287         stat_ack_not_ours    = 0x00,
288         stat_ack_sw_gen      = 0x04,
289         stat_ack_rnr         = 0x10,
290         stat_ack_cu_idle     = 0x20,
291         stat_ack_frame_rx    = 0x40,
292         stat_ack_cu_cmd_done = 0x80,
293         stat_ack_not_present = 0xFF,
294         stat_ack_rx = (stat_ack_sw_gen | stat_ack_rnr | stat_ack_frame_rx),
295         stat_ack_tx = (stat_ack_cu_idle | stat_ack_cu_cmd_done),
296 };
297
298 enum scb_cmd_hi {
299         irq_mask_none = 0x00,
300         irq_mask_all  = 0x01,
301         irq_sw_gen    = 0x02,
302 };
303
304 enum scb_cmd_lo {
305         cuc_nop        = 0x00,
306         ruc_start      = 0x01,
307         ruc_load_base  = 0x06,
308         cuc_start      = 0x10,
309         cuc_resume     = 0x20,
310         cuc_dump_addr  = 0x40,
311         cuc_dump_stats = 0x50,
312         cuc_load_base  = 0x60,
313         cuc_dump_reset = 0x70,
314 };
315
316 enum cuc_dump {
317         cuc_dump_complete       = 0x0000A005,
318         cuc_dump_reset_complete = 0x0000A007,
319 };
320
321 enum port {
322         software_reset  = 0x0000,
323         selftest        = 0x0001,
324         selective_reset = 0x0002,
325 };
326
327 enum eeprom_ctrl_lo {
328         eesk = 0x01,
329         eecs = 0x02,
330         eedi = 0x04,
331         eedo = 0x08,
332 };
333
334 enum mdi_ctrl {
335         mdi_write = 0x04000000,
336         mdi_read  = 0x08000000,
337         mdi_ready = 0x10000000,
338 };
339
340 enum eeprom_op {
341         op_write = 0x05,
342         op_read  = 0x06,
343         op_ewds  = 0x10,
344         op_ewen  = 0x13,
345 };
346
347 enum eeprom_offsets {
348         eeprom_cnfg_mdix  = 0x03,
349         eeprom_phy_iface  = 0x06,
350         eeprom_id         = 0x0A,
351         eeprom_config_asf = 0x0D,
352         eeprom_smbus_addr = 0x90,
353 };
354
355 enum eeprom_cnfg_mdix {
356         eeprom_mdix_enabled = 0x0080,
357 };
358
359 enum eeprom_phy_iface {
360         NoSuchPhy = 0,
361         I82553AB,
362         I82553C,
363         I82503,
364         DP83840,
365         S80C240,
366         S80C24,
367         I82555,
368         DP83840A = 10,
369 };
370
371 enum eeprom_id {
372         eeprom_id_wol = 0x0020,
373 };
374
375 enum eeprom_config_asf {
376         eeprom_asf = 0x8000,
377         eeprom_gcl = 0x4000,
378 };
379
380 enum cb_status {
381         cb_complete = 0x8000,
382         cb_ok       = 0x2000,
383 };
384
385 /*
386  * cb_command - Command Block flags
387  * @cb_tx_nc:  0: controller does CRC (normal),  1: CRC from skb memory
388  */
389 enum cb_command {
390         cb_nop    = 0x0000,
391         cb_iaaddr = 0x0001,
392         cb_config = 0x0002,
393         cb_multi  = 0x0003,
394         cb_tx     = 0x0004,
395         cb_ucode  = 0x0005,
396         cb_dump   = 0x0006,
397         cb_tx_sf  = 0x0008,
398         cb_tx_nc  = 0x0010,
399         cb_cid    = 0x1f00,
400         cb_i      = 0x2000,
401         cb_s      = 0x4000,
402         cb_el     = 0x8000,
403 };
404
405 struct rfd {
406         __le16 status;
407         __le16 command;
408         __le32 link;
409         __le32 rbd;
410         __le16 actual_size;
411         __le16 size;
412 };
413
414 struct rx {
415         struct rx *next, *prev;
416         struct sk_buff *skb;
417         dma_addr_t dma_addr;
418 };
419
420 #if defined(__BIG_ENDIAN_BITFIELD)
421 #define X(a,b)  b,a
422 #else
423 #define X(a,b)  a,b
424 #endif
425 struct config {
426 /*0*/   u8 X(byte_count:6, pad0:2);
427 /*1*/   u8 X(X(rx_fifo_limit:4, tx_fifo_limit:3), pad1:1);
428 /*2*/   u8 adaptive_ifs;
429 /*3*/   u8 X(X(X(X(mwi_enable:1, type_enable:1), read_align_enable:1),
430            term_write_cache_line:1), pad3:4);
431 /*4*/   u8 X(rx_dma_max_count:7, pad4:1);
432 /*5*/   u8 X(tx_dma_max_count:7, dma_max_count_enable:1);
433 /*6*/   u8 X(X(X(X(X(X(X(late_scb_update:1, direct_rx_dma:1),
434            tno_intr:1), cna_intr:1), standard_tcb:1), standard_stat_counter:1),
435            rx_save_overruns : 1), rx_save_bad_frames : 1);
436 /*7*/   u8 X(X(X(X(X(rx_discard_short_frames:1, tx_underrun_retry:2),
437            pad7:2), rx_extended_rfd:1), tx_two_frames_in_fifo:1),
438            tx_dynamic_tbd:1);
439 /*8*/   u8 X(X(mii_mode:1, pad8:6), csma_disabled:1);
440 /*9*/   u8 X(X(X(X(X(rx_tcpudp_checksum:1, pad9:3), vlan_arp_tco:1),
441            link_status_wake:1), arp_wake:1), mcmatch_wake:1);
442 /*10*/  u8 X(X(X(pad10:3, no_source_addr_insertion:1), preamble_length:2),
443            loopback:2);
444 /*11*/  u8 X(linear_priority:3, pad11:5);
445 /*12*/  u8 X(X(linear_priority_mode:1, pad12:3), ifs:4);
446 /*13*/  u8 ip_addr_lo;
447 /*14*/  u8 ip_addr_hi;
448 /*15*/  u8 X(X(X(X(X(X(X(promiscuous_mode:1, broadcast_disabled:1),
449            wait_after_win:1), pad15_1:1), ignore_ul_bit:1), crc_16_bit:1),
450            pad15_2:1), crs_or_cdt:1);
451 /*16*/  u8 fc_delay_lo;
452 /*17*/  u8 fc_delay_hi;
453 /*18*/  u8 X(X(X(X(X(rx_stripping:1, tx_padding:1), rx_crc_transfer:1),
454            rx_long_ok:1), fc_priority_threshold:3), pad18:1);
455 /*19*/  u8 X(X(X(X(X(X(X(addr_wake:1, magic_packet_disable:1),
456            fc_disable:1), fc_restop:1), fc_restart:1), fc_reject:1),
457            full_duplex_force:1), full_duplex_pin:1);
458 /*20*/  u8 X(X(X(pad20_1:5, fc_priority_location:1), multi_ia:1), pad20_2:1);
459 /*21*/  u8 X(X(pad21_1:3, multicast_all:1), pad21_2:4);
460 /*22*/  u8 X(X(rx_d102_mode:1, rx_vlan_drop:1), pad22:6);
461         u8 pad_d102[9];
462 };
463
464 #define E100_MAX_MULTICAST_ADDRS        64
465 struct multi {
466         __le16 count;
467         u8 addr[E100_MAX_MULTICAST_ADDRS * ETH_ALEN + 2/*pad*/];
468 };
469
470 /* Important: keep total struct u32-aligned */
471 #define UCODE_SIZE                      134
472 struct cb {
473         __le16 status;
474         __le16 command;
475         __le32 link;
476         union {
477                 u8 iaaddr[ETH_ALEN];
478                 __le32 ucode[UCODE_SIZE];
479                 struct config config;
480                 struct multi multi;
481                 struct {
482                         u32 tbd_array;
483                         u16 tcb_byte_count;
484                         u8 threshold;
485                         u8 tbd_count;
486                         struct {
487                                 __le32 buf_addr;
488                                 __le16 size;
489                                 u16 eol;
490                         } tbd;
491                 } tcb;
492                 __le32 dump_buffer_addr;
493         } u;
494         struct cb *next, *prev;
495         dma_addr_t dma_addr;
496         struct sk_buff *skb;
497 };
498
499 enum loopback {
500         lb_none = 0, lb_mac = 1, lb_phy = 3,
501 };
502
503 struct stats {
504         __le32 tx_good_frames, tx_max_collisions, tx_late_collisions,
505                 tx_underruns, tx_lost_crs, tx_deferred, tx_single_collisions,
506                 tx_multiple_collisions, tx_total_collisions;
507         __le32 rx_good_frames, rx_crc_errors, rx_alignment_errors,
508                 rx_resource_errors, rx_overrun_errors, rx_cdt_errors,
509                 rx_short_frame_errors;
510         __le32 fc_xmt_pause, fc_rcv_pause, fc_rcv_unsupported;
511         __le16 xmt_tco_frames, rcv_tco_frames;
512         __le32 complete;
513 };
514
515 struct mem {
516         struct {
517                 u32 signature;
518                 u32 result;
519         } selftest;
520         struct stats stats;
521         u8 dump_buf[596];
522 };
523
524 struct param_range {
525         u32 min;
526         u32 max;
527         u32 count;
528 };
529
530 struct params {
531         struct param_range rfds;
532         struct param_range cbs;
533 };
534
535 struct nic {
536         /* Begin: frequently used values: keep adjacent for cache effect */
537         u32 msg_enable                          ____cacheline_aligned;
538         struct net_device *netdev;
539         struct pci_dev *pdev;
540         u16 (*mdio_ctrl)(struct nic *nic, u32 addr, u32 dir, u32 reg, u16 data);
541
542         struct rx *rxs                          ____cacheline_aligned;
543         struct rx *rx_to_use;
544         struct rx *rx_to_clean;
545         struct rfd blank_rfd;
546         enum ru_state ru_running;
547
548         spinlock_t cb_lock                      ____cacheline_aligned;
549         spinlock_t cmd_lock;
550         struct csr __iomem *csr;
551         enum scb_cmd_lo cuc_cmd;
552         unsigned int cbs_avail;
553         struct napi_struct napi;
554         struct cb *cbs;
555         struct cb *cb_to_use;
556         struct cb *cb_to_send;
557         struct cb *cb_to_clean;
558         __le16 tx_command;
559         /* End: frequently used values: keep adjacent for cache effect */
560
561         enum {
562                 ich                = (1 << 0),
563                 promiscuous        = (1 << 1),
564                 multicast_all      = (1 << 2),
565                 wol_magic          = (1 << 3),
566                 ich_10h_workaround = (1 << 4),
567         } flags                                 ____cacheline_aligned;
568
569         enum mac mac;
570         enum phy phy;
571         struct params params;
572         struct timer_list watchdog;
573         struct mii_if_info mii;
574         struct work_struct tx_timeout_task;
575         enum loopback loopback;
576
577         struct mem *mem;
578         dma_addr_t dma_addr;
579
580         struct dma_pool *cbs_pool;
581         dma_addr_t cbs_dma_addr;
582         u8 adaptive_ifs;
583         u8 tx_threshold;
584         u32 tx_frames;
585         u32 tx_collisions;
586         u32 tx_deferred;
587         u32 tx_single_collisions;
588         u32 tx_multiple_collisions;
589         u32 tx_fc_pause;
590         u32 tx_tco_frames;
591
592         u32 rx_fc_pause;
593         u32 rx_fc_unsupported;
594         u32 rx_tco_frames;
595         u32 rx_short_frame_errors;
596         u32 rx_over_length_errors;
597
598         u16 eeprom_wc;
599         __le16 eeprom[256];
600         spinlock_t mdio_lock;
601         const struct firmware *fw;
602 };
603
604 static inline void e100_write_flush(struct nic *nic)
605 {
606         /* Flush previous PCI writes through intermediate bridges
607          * by doing a benign read */
608         (void)ioread8(&nic->csr->scb.status);
609 }
610
611 static void e100_enable_irq(struct nic *nic)
612 {
613         unsigned long flags;
614
615         spin_lock_irqsave(&nic->cmd_lock, flags);
616         iowrite8(irq_mask_none, &nic->csr->scb.cmd_hi);
617         e100_write_flush(nic);
618         spin_unlock_irqrestore(&nic->cmd_lock, flags);
619 }
620
621 static void e100_disable_irq(struct nic *nic)
622 {
623         unsigned long flags;
624
625         spin_lock_irqsave(&nic->cmd_lock, flags);
626         iowrite8(irq_mask_all, &nic->csr->scb.cmd_hi);
627         e100_write_flush(nic);
628         spin_unlock_irqrestore(&nic->cmd_lock, flags);
629 }
630
631 static void e100_hw_reset(struct nic *nic)
632 {
633         /* Put CU and RU into idle with a selective reset to get
634          * device off of PCI bus */
635         iowrite32(selective_reset, &nic->csr->port);
636         e100_write_flush(nic); udelay(20);
637
638         /* Now fully reset device */
639         iowrite32(software_reset, &nic->csr->port);
640         e100_write_flush(nic); udelay(20);
641
642         /* Mask off our interrupt line - it's unmasked after reset */
643         e100_disable_irq(nic);
644 }
645
646 static int e100_self_test(struct nic *nic)
647 {
648         u32 dma_addr = nic->dma_addr + offsetof(struct mem, selftest);
649
650         /* Passing the self-test is a pretty good indication
651          * that the device can DMA to/from host memory */
652
653         nic->mem->selftest.signature = 0;
654         nic->mem->selftest.result = 0xFFFFFFFF;
655
656         iowrite32(selftest | dma_addr, &nic->csr->port);
657         e100_write_flush(nic);
658         /* Wait 10 msec for self-test to complete */
659         msleep(10);
660
661         /* Interrupts are enabled after self-test */
662         e100_disable_irq(nic);
663
664         /* Check results of self-test */
665         if (nic->mem->selftest.result != 0) {
666                 netif_err(nic, hw, nic->netdev,
667                           "Self-test failed: result=0x%08X\n",
668                           nic->mem->selftest.result);
669                 return -ETIMEDOUT;
670         }
671         if (nic->mem->selftest.signature == 0) {
672                 netif_err(nic, hw, nic->netdev, "Self-test failed: timed out\n");
673                 return -ETIMEDOUT;
674         }
675
676         return 0;
677 }
678
679 static void e100_eeprom_write(struct nic *nic, u16 addr_len, u16 addr, __le16 data)
680 {
681         u32 cmd_addr_data[3];
682         u8 ctrl;
683         int i, j;
684
685         /* Three cmds: write/erase enable, write data, write/erase disable */
686         cmd_addr_data[0] = op_ewen << (addr_len - 2);
687         cmd_addr_data[1] = (((op_write << addr_len) | addr) << 16) |
688                 le16_to_cpu(data);
689         cmd_addr_data[2] = op_ewds << (addr_len - 2);
690
691         /* Bit-bang cmds to write word to eeprom */
692         for (j = 0; j < 3; j++) {
693
694                 /* Chip select */
695                 iowrite8(eecs | eesk, &nic->csr->eeprom_ctrl_lo);
696                 e100_write_flush(nic); udelay(4);
697
698                 for (i = 31; i >= 0; i--) {
699                         ctrl = (cmd_addr_data[j] & (1 << i)) ?
700                                 eecs | eedi : eecs;
701                         iowrite8(ctrl, &nic->csr->eeprom_ctrl_lo);
702                         e100_write_flush(nic); udelay(4);
703
704                         iowrite8(ctrl | eesk, &nic->csr->eeprom_ctrl_lo);
705                         e100_write_flush(nic); udelay(4);
706                 }
707                 /* Wait 10 msec for cmd to complete */
708                 msleep(10);
709
710                 /* Chip deselect */
711                 iowrite8(0, &nic->csr->eeprom_ctrl_lo);
712                 e100_write_flush(nic); udelay(4);
713         }
714 };
715
716 /* General technique stolen from the eepro100 driver - very clever */
717 static __le16 e100_eeprom_read(struct nic *nic, u16 *addr_len, u16 addr)
718 {
719         u32 cmd_addr_data;
720         u16 data = 0;
721         u8 ctrl;
722         int i;
723
724         cmd_addr_data = ((op_read << *addr_len) | addr) << 16;
725
726         /* Chip select */
727         iowrite8(eecs | eesk, &nic->csr->eeprom_ctrl_lo);
728         e100_write_flush(nic); udelay(4);
729
730         /* Bit-bang to read word from eeprom */
731         for (i = 31; i >= 0; i--) {
732                 ctrl = (cmd_addr_data & (1 << i)) ? eecs | eedi : eecs;
733                 iowrite8(ctrl, &nic->csr->eeprom_ctrl_lo);
734                 e100_write_flush(nic); udelay(4);
735
736                 iowrite8(ctrl | eesk, &nic->csr->eeprom_ctrl_lo);
737                 e100_write_flush(nic); udelay(4);
738
739                 /* Eeprom drives a dummy zero to EEDO after receiving
740                  * complete address.  Use this to adjust addr_len. */
741                 ctrl = ioread8(&nic->csr->eeprom_ctrl_lo);
742                 if (!(ctrl & eedo) && i > 16) {
743                         *addr_len -= (i - 16);
744                         i = 17;
745                 }
746
747                 data = (data << 1) | (ctrl & eedo ? 1 : 0);
748         }
749
750         /* Chip deselect */
751         iowrite8(0, &nic->csr->eeprom_ctrl_lo);
752         e100_write_flush(nic); udelay(4);
753
754         return cpu_to_le16(data);
755 };
756
757 /* Load entire EEPROM image into driver cache and validate checksum */
758 static int e100_eeprom_load(struct nic *nic)
759 {
760         u16 addr, addr_len = 8, checksum = 0;
761
762         /* Try reading with an 8-bit addr len to discover actual addr len */
763         e100_eeprom_read(nic, &addr_len, 0);
764         nic->eeprom_wc = 1 << addr_len;
765
766         for (addr = 0; addr < nic->eeprom_wc; addr++) {
767                 nic->eeprom[addr] = e100_eeprom_read(nic, &addr_len, addr);
768                 if (addr < nic->eeprom_wc - 1)
769                         checksum += le16_to_cpu(nic->eeprom[addr]);
770         }
771
772         /* The checksum, stored in the last word, is calculated such that
773          * the sum of words should be 0xBABA */
774         if (cpu_to_le16(0xBABA - checksum) != nic->eeprom[nic->eeprom_wc - 1]) {
775                 netif_err(nic, probe, nic->netdev, "EEPROM corrupted\n");
776                 if (!eeprom_bad_csum_allow)
777                         return -EAGAIN;
778         }
779
780         return 0;
781 }
782
783 /* Save (portion of) driver EEPROM cache to device and update checksum */
784 static int e100_eeprom_save(struct nic *nic, u16 start, u16 count)
785 {
786         u16 addr, addr_len = 8, checksum = 0;
787
788         /* Try reading with an 8-bit addr len to discover actual addr len */
789         e100_eeprom_read(nic, &addr_len, 0);
790         nic->eeprom_wc = 1 << addr_len;
791
792         if (start + count >= nic->eeprom_wc)
793                 return -EINVAL;
794
795         for (addr = start; addr < start + count; addr++)
796                 e100_eeprom_write(nic, addr_len, addr, nic->eeprom[addr]);
797
798         /* The checksum, stored in the last word, is calculated such that
799          * the sum of words should be 0xBABA */
800         for (addr = 0; addr < nic->eeprom_wc - 1; addr++)
801                 checksum += le16_to_cpu(nic->eeprom[addr]);
802         nic->eeprom[nic->eeprom_wc - 1] = cpu_to_le16(0xBABA - checksum);
803         e100_eeprom_write(nic, addr_len, nic->eeprom_wc - 1,
804                 nic->eeprom[nic->eeprom_wc - 1]);
805
806         return 0;
807 }
808
809 #define E100_WAIT_SCB_TIMEOUT 20000 /* we might have to wait 100ms!!! */
810 #define E100_WAIT_SCB_FAST 20       /* delay like the old code */
811 static int e100_exec_cmd(struct nic *nic, u8 cmd, dma_addr_t dma_addr)
812 {
813         unsigned long flags;
814         unsigned int i;
815         int err = 0;
816
817         spin_lock_irqsave(&nic->cmd_lock, flags);
818
819         /* Previous command is accepted when SCB clears */
820         for (i = 0; i < E100_WAIT_SCB_TIMEOUT; i++) {
821                 if (likely(!ioread8(&nic->csr->scb.cmd_lo)))
822                         break;
823                 cpu_relax();
824                 if (unlikely(i > E100_WAIT_SCB_FAST))
825                         udelay(5);
826         }
827         if (unlikely(i == E100_WAIT_SCB_TIMEOUT)) {
828                 err = -EAGAIN;
829                 goto err_unlock;
830         }
831
832         if (unlikely(cmd != cuc_resume))
833                 iowrite32(dma_addr, &nic->csr->scb.gen_ptr);
834         iowrite8(cmd, &nic->csr->scb.cmd_lo);
835
836 err_unlock:
837         spin_unlock_irqrestore(&nic->cmd_lock, flags);
838
839         return err;
840 }
841
842 static int e100_exec_cb(struct nic *nic, struct sk_buff *skb,
843         int (*cb_prepare)(struct nic *, struct cb *, struct sk_buff *))
844 {
845         struct cb *cb;
846         unsigned long flags;
847         int err;
848
849         spin_lock_irqsave(&nic->cb_lock, flags);
850
851         if (unlikely(!nic->cbs_avail)) {
852                 err = -ENOMEM;
853                 goto err_unlock;
854         }
855
856         cb = nic->cb_to_use;
857         nic->cb_to_use = cb->next;
858         nic->cbs_avail--;
859         cb->skb = skb;
860
861         err = cb_prepare(nic, cb, skb);
862         if (err)
863                 goto err_unlock;
864
865         if (unlikely(!nic->cbs_avail))
866                 err = -ENOSPC;
867
868
869         /* Order is important otherwise we'll be in a race with h/w:
870          * set S-bit in current first, then clear S-bit in previous. */
871         cb->command |= cpu_to_le16(cb_s);
872         dma_wmb();
873         cb->prev->command &= cpu_to_le16(~cb_s);
874
875         while (nic->cb_to_send != nic->cb_to_use) {
876                 if (unlikely(e100_exec_cmd(nic, nic->cuc_cmd,
877                         nic->cb_to_send->dma_addr))) {
878                         /* Ok, here's where things get sticky.  It's
879                          * possible that we can't schedule the command
880                          * because the controller is too busy, so
881                          * let's just queue the command and try again
882                          * when another command is scheduled. */
883                         if (err == -ENOSPC) {
884                                 //request a reset
885                                 schedule_work(&nic->tx_timeout_task);
886                         }
887                         break;
888                 } else {
889                         nic->cuc_cmd = cuc_resume;
890                         nic->cb_to_send = nic->cb_to_send->next;
891                 }
892         }
893
894 err_unlock:
895         spin_unlock_irqrestore(&nic->cb_lock, flags);
896
897         return err;
898 }
899
900 static int mdio_read(struct net_device *netdev, int addr, int reg)
901 {
902         struct nic *nic = netdev_priv(netdev);
903         return nic->mdio_ctrl(nic, addr, mdi_read, reg, 0);
904 }
905
906 static void mdio_write(struct net_device *netdev, int addr, int reg, int data)
907 {
908         struct nic *nic = netdev_priv(netdev);
909
910         nic->mdio_ctrl(nic, addr, mdi_write, reg, data);
911 }
912
913 /* the standard mdio_ctrl() function for usual MII-compliant hardware */
914 static u16 mdio_ctrl_hw(struct nic *nic, u32 addr, u32 dir, u32 reg, u16 data)
915 {
916         u32 data_out = 0;
917         unsigned int i;
918         unsigned long flags;
919
920
921         /*
922          * Stratus87247: we shouldn't be writing the MDI control
923          * register until the Ready bit shows True.  Also, since
924          * manipulation of the MDI control registers is a multi-step
925          * procedure it should be done under lock.
926          */
927         spin_lock_irqsave(&nic->mdio_lock, flags);
928         for (i = 100; i; --i) {
929                 if (ioread32(&nic->csr->mdi_ctrl) & mdi_ready)
930                         break;
931                 udelay(20);
932         }
933         if (unlikely(!i)) {
934                 netdev_err(nic->netdev, "e100.mdio_ctrl won't go Ready\n");
935                 spin_unlock_irqrestore(&nic->mdio_lock, flags);
936                 return 0;               /* No way to indicate timeout error */
937         }
938         iowrite32((reg << 16) | (addr << 21) | dir | data, &nic->csr->mdi_ctrl);
939
940         for (i = 0; i < 100; i++) {
941                 udelay(20);
942                 if ((data_out = ioread32(&nic->csr->mdi_ctrl)) & mdi_ready)
943                         break;
944         }
945         spin_unlock_irqrestore(&nic->mdio_lock, flags);
946         netif_printk(nic, hw, KERN_DEBUG, nic->netdev,
947                      "%s:addr=%d, reg=%d, data_in=0x%04X, data_out=0x%04X\n",
948                      dir == mdi_read ? "READ" : "WRITE",
949                      addr, reg, data, data_out);
950         return (u16)data_out;
951 }
952
953 /* slightly tweaked mdio_ctrl() function for phy_82552_v specifics */
954 static u16 mdio_ctrl_phy_82552_v(struct nic *nic,
955                                  u32 addr,
956                                  u32 dir,
957                                  u32 reg,
958                                  u16 data)
959 {
960         if ((reg == MII_BMCR) && (dir == mdi_write)) {
961                 if (data & (BMCR_ANRESTART | BMCR_ANENABLE)) {
962                         u16 advert = mdio_read(nic->netdev, nic->mii.phy_id,
963                                                         MII_ADVERTISE);
964
965                         /*
966                          * Workaround Si issue where sometimes the part will not
967                          * autoneg to 100Mbps even when advertised.
968                          */
969                         if (advert & ADVERTISE_100FULL)
970                                 data |= BMCR_SPEED100 | BMCR_FULLDPLX;
971                         else if (advert & ADVERTISE_100HALF)
972                                 data |= BMCR_SPEED100;
973                 }
974         }
975         return mdio_ctrl_hw(nic, addr, dir, reg, data);
976 }
977
978 /* Fully software-emulated mdio_ctrl() function for cards without
979  * MII-compliant PHYs.
980  * For now, this is mainly geared towards 80c24 support; in case of further
981  * requirements for other types (i82503, ...?) either extend this mechanism
982  * or split it, whichever is cleaner.
983  */
984 static u16 mdio_ctrl_phy_mii_emulated(struct nic *nic,
985                                       u32 addr,
986                                       u32 dir,
987                                       u32 reg,
988                                       u16 data)
989 {
990         /* might need to allocate a netdev_priv'ed register array eventually
991          * to be able to record state changes, but for now
992          * some fully hardcoded register handling ought to be ok I guess. */
993
994         if (dir == mdi_read) {
995                 switch (reg) {
996                 case MII_BMCR:
997                         /* Auto-negotiation, right? */
998                         return  BMCR_ANENABLE |
999                                 BMCR_FULLDPLX;
1000                 case MII_BMSR:
1001                         return  BMSR_LSTATUS /* for mii_link_ok() */ |
1002                                 BMSR_ANEGCAPABLE |
1003                                 BMSR_10FULL;
1004                 case MII_ADVERTISE:
1005                         /* 80c24 is a "combo card" PHY, right? */
1006                         return  ADVERTISE_10HALF |
1007                                 ADVERTISE_10FULL;
1008                 default:
1009                         netif_printk(nic, hw, KERN_DEBUG, nic->netdev,
1010                                      "%s:addr=%d, reg=%d, data=0x%04X: unimplemented emulation!\n",
1011                                      dir == mdi_read ? "READ" : "WRITE",
1012                                      addr, reg, data);
1013                         return 0xFFFF;
1014                 }
1015         } else {
1016                 switch (reg) {
1017                 default:
1018                         netif_printk(nic, hw, KERN_DEBUG, nic->netdev,
1019                                      "%s:addr=%d, reg=%d, data=0x%04X: unimplemented emulation!\n",
1020                                      dir == mdi_read ? "READ" : "WRITE",
1021                                      addr, reg, data);
1022                         return 0xFFFF;
1023                 }
1024         }
1025 }
1026 static inline int e100_phy_supports_mii(struct nic *nic)
1027 {
1028         /* for now, just check it by comparing whether we
1029            are using MII software emulation.
1030         */
1031         return (nic->mdio_ctrl != mdio_ctrl_phy_mii_emulated);
1032 }
1033
1034 static void e100_get_defaults(struct nic *nic)
1035 {
1036         struct param_range rfds = { .min = 16, .max = 256, .count = 256 };
1037         struct param_range cbs  = { .min = 64, .max = 256, .count = 128 };
1038
1039         /* MAC type is encoded as rev ID; exception: ICH is treated as 82559 */
1040         nic->mac = (nic->flags & ich) ? mac_82559_D101M : nic->pdev->revision;
1041         if (nic->mac == mac_unknown)
1042                 nic->mac = mac_82557_D100_A;
1043
1044         nic->params.rfds = rfds;
1045         nic->params.cbs = cbs;
1046
1047         /* Quadwords to DMA into FIFO before starting frame transmit */
1048         nic->tx_threshold = 0xE0;
1049
1050         /* no interrupt for every tx completion, delay = 256us if not 557 */
1051         nic->tx_command = cpu_to_le16(cb_tx | cb_tx_sf |
1052                 ((nic->mac >= mac_82558_D101_A4) ? cb_cid : cb_i));
1053
1054         /* Template for a freshly allocated RFD */
1055         nic->blank_rfd.command = 0;
1056         nic->blank_rfd.rbd = cpu_to_le32(0xFFFFFFFF);
1057         nic->blank_rfd.size = cpu_to_le16(VLAN_ETH_FRAME_LEN + ETH_FCS_LEN);
1058
1059         /* MII setup */
1060         nic->mii.phy_id_mask = 0x1F;
1061         nic->mii.reg_num_mask = 0x1F;
1062         nic->mii.dev = nic->netdev;
1063         nic->mii.mdio_read = mdio_read;
1064         nic->mii.mdio_write = mdio_write;
1065 }
1066
1067 static int e100_configure(struct nic *nic, struct cb *cb, struct sk_buff *skb)
1068 {
1069         struct config *config = &cb->u.config;
1070         u8 *c = (u8 *)config;
1071         struct net_device *netdev = nic->netdev;
1072
1073         cb->command = cpu_to_le16(cb_config);
1074
1075         memset(config, 0, sizeof(struct config));
1076
1077         config->byte_count = 0x16;              /* bytes in this struct */
1078         config->rx_fifo_limit = 0x8;            /* bytes in FIFO before DMA */
1079         config->direct_rx_dma = 0x1;            /* reserved */
1080         config->standard_tcb = 0x1;             /* 1=standard, 0=extended */
1081         config->standard_stat_counter = 0x1;    /* 1=standard, 0=extended */
1082         config->rx_discard_short_frames = 0x1;  /* 1=discard, 0=pass */
1083         config->tx_underrun_retry = 0x3;        /* # of underrun retries */
1084         if (e100_phy_supports_mii(nic))
1085                 config->mii_mode = 1;           /* 1=MII mode, 0=i82503 mode */
1086         config->pad10 = 0x6;
1087         config->no_source_addr_insertion = 0x1; /* 1=no, 0=yes */
1088         config->preamble_length = 0x2;          /* 0=1, 1=3, 2=7, 3=15 bytes */
1089         config->ifs = 0x6;                      /* x16 = inter frame spacing */
1090         config->ip_addr_hi = 0xF2;              /* ARP IP filter - not used */
1091         config->pad15_1 = 0x1;
1092         config->pad15_2 = 0x1;
1093         config->crs_or_cdt = 0x0;               /* 0=CRS only, 1=CRS or CDT */
1094         config->fc_delay_hi = 0x40;             /* time delay for fc frame */
1095         config->tx_padding = 0x1;               /* 1=pad short frames */
1096         config->fc_priority_threshold = 0x7;    /* 7=priority fc disabled */
1097         config->pad18 = 0x1;
1098         config->full_duplex_pin = 0x1;          /* 1=examine FDX# pin */
1099         config->pad20_1 = 0x1F;
1100         config->fc_priority_location = 0x1;     /* 1=byte#31, 0=byte#19 */
1101         config->pad21_1 = 0x5;
1102
1103         config->adaptive_ifs = nic->adaptive_ifs;
1104         config->loopback = nic->loopback;
1105
1106         if (nic->mii.force_media && nic->mii.full_duplex)
1107                 config->full_duplex_force = 0x1;        /* 1=force, 0=auto */
1108
1109         if (nic->flags & promiscuous || nic->loopback) {
1110                 config->rx_save_bad_frames = 0x1;       /* 1=save, 0=discard */
1111                 config->rx_discard_short_frames = 0x0;  /* 1=discard, 0=save */
1112                 config->promiscuous_mode = 0x1;         /* 1=on, 0=off */
1113         }
1114
1115         if (unlikely(netdev->features & NETIF_F_RXFCS))
1116                 config->rx_crc_transfer = 0x1;  /* 1=save, 0=discard */
1117
1118         if (nic->flags & multicast_all)
1119                 config->multicast_all = 0x1;            /* 1=accept, 0=no */
1120
1121         /* disable WoL when up */
1122         if (netif_running(nic->netdev) || !(nic->flags & wol_magic))
1123                 config->magic_packet_disable = 0x1;     /* 1=off, 0=on */
1124
1125         if (nic->mac >= mac_82558_D101_A4) {
1126                 config->fc_disable = 0x1;       /* 1=Tx fc off, 0=Tx fc on */
1127                 config->mwi_enable = 0x1;       /* 1=enable, 0=disable */
1128                 config->standard_tcb = 0x0;     /* 1=standard, 0=extended */
1129                 config->rx_long_ok = 0x1;       /* 1=VLANs ok, 0=standard */
1130                 if (nic->mac >= mac_82559_D101M) {
1131                         config->tno_intr = 0x1;         /* TCO stats enable */
1132                         /* Enable TCO in extended config */
1133                         if (nic->mac >= mac_82551_10) {
1134                                 config->byte_count = 0x20; /* extended bytes */
1135                                 config->rx_d102_mode = 0x1; /* GMRC for TCO */
1136                         }
1137                 } else {
1138                         config->standard_stat_counter = 0x0;
1139                 }
1140         }
1141
1142         if (netdev->features & NETIF_F_RXALL) {
1143                 config->rx_save_overruns = 0x1; /* 1=save, 0=discard */
1144                 config->rx_save_bad_frames = 0x1;       /* 1=save, 0=discard */
1145                 config->rx_discard_short_frames = 0x0;  /* 1=discard, 0=save */
1146         }
1147
1148         netif_printk(nic, hw, KERN_DEBUG, nic->netdev, "[00-07]=%8ph\n",
1149                      c + 0);
1150         netif_printk(nic, hw, KERN_DEBUG, nic->netdev, "[08-15]=%8ph\n",
1151                      c + 8);
1152         netif_printk(nic, hw, KERN_DEBUG, nic->netdev, "[16-23]=%8ph\n",
1153                      c + 16);
1154         return 0;
1155 }
1156
1157 /*************************************************************************
1158 *  CPUSaver parameters
1159 *
1160 *  All CPUSaver parameters are 16-bit literals that are part of a
1161 *  "move immediate value" instruction.  By changing the value of
1162 *  the literal in the instruction before the code is loaded, the
1163 *  driver can change the algorithm.
1164 *
1165 *  INTDELAY - This loads the dead-man timer with its initial value.
1166 *    When this timer expires the interrupt is asserted, and the
1167 *    timer is reset each time a new packet is received.  (see
1168 *    BUNDLEMAX below to set the limit on number of chained packets)
1169 *    The current default is 0x600 or 1536.  Experiments show that
1170 *    the value should probably stay within the 0x200 - 0x1000.
1171 *
1172 *  BUNDLEMAX -
1173 *    This sets the maximum number of frames that will be bundled.  In
1174 *    some situations, such as the TCP windowing algorithm, it may be
1175 *    better to limit the growth of the bundle size than let it go as
1176 *    high as it can, because that could cause too much added latency.
1177 *    The default is six, because this is the number of packets in the
1178 *    default TCP window size.  A value of 1 would make CPUSaver indicate
1179 *    an interrupt for every frame received.  If you do not want to put
1180 *    a limit on the bundle size, set this value to xFFFF.
1181 *
1182 *  BUNDLESMALL -
1183 *    This contains a bit-mask describing the minimum size frame that
1184 *    will be bundled.  The default masks the lower 7 bits, which means
1185 *    that any frame less than 128 bytes in length will not be bundled,
1186 *    but will instead immediately generate an interrupt.  This does
1187 *    not affect the current bundle in any way.  Any frame that is 128
1188 *    bytes or large will be bundled normally.  This feature is meant
1189 *    to provide immediate indication of ACK frames in a TCP environment.
1190 *    Customers were seeing poor performance when a machine with CPUSaver
1191 *    enabled was sending but not receiving.  The delay introduced when
1192 *    the ACKs were received was enough to reduce total throughput, because
1193 *    the sender would sit idle until the ACK was finally seen.
1194 *
1195 *    The current default is 0xFF80, which masks out the lower 7 bits.
1196 *    This means that any frame which is x7F (127) bytes or smaller
1197 *    will cause an immediate interrupt.  Because this value must be a
1198 *    bit mask, there are only a few valid values that can be used.  To
1199 *    turn this feature off, the driver can write the value xFFFF to the
1200 *    lower word of this instruction (in the same way that the other
1201 *    parameters are used).  Likewise, a value of 0xF800 (2047) would
1202 *    cause an interrupt to be generated for every frame, because all
1203 *    standard Ethernet frames are <= 2047 bytes in length.
1204 *************************************************************************/
1205
1206 /* if you wish to disable the ucode functionality, while maintaining the
1207  * workarounds it provides, set the following defines to:
1208  * BUNDLESMALL 0
1209  * BUNDLEMAX 1
1210  * INTDELAY 1
1211  */
1212 #define BUNDLESMALL 1
1213 #define BUNDLEMAX (u16)6
1214 #define INTDELAY (u16)1536 /* 0x600 */
1215
1216 /* Initialize firmware */
1217 static const struct firmware *e100_request_firmware(struct nic *nic)
1218 {
1219         const char *fw_name;
1220         const struct firmware *fw = nic->fw;
1221         u8 timer, bundle, min_size;
1222         int err = 0;
1223         bool required = false;
1224
1225         /* do not load u-code for ICH devices */
1226         if (nic->flags & ich)
1227                 return NULL;
1228
1229         /* Search for ucode match against h/w revision
1230          *
1231          * Based on comments in the source code for the FreeBSD fxp
1232          * driver, the FIRMWARE_D102E ucode includes both CPUSaver and
1233          *
1234          *    "fixes for bugs in the B-step hardware (specifically, bugs
1235          *     with Inline Receive)."
1236          *
1237          * So we must fail if it cannot be loaded.
1238          *
1239          * The other microcode files are only required for the optional
1240          * CPUSaver feature.  Nice to have, but no reason to fail.
1241          */
1242         if (nic->mac == mac_82559_D101M) {
1243                 fw_name = FIRMWARE_D101M;
1244         } else if (nic->mac == mac_82559_D101S) {
1245                 fw_name = FIRMWARE_D101S;
1246         } else if (nic->mac == mac_82551_F || nic->mac == mac_82551_10) {
1247                 fw_name = FIRMWARE_D102E;
1248                 required = true;
1249         } else { /* No ucode on other devices */
1250                 return NULL;
1251         }
1252
1253         /* If the firmware has not previously been loaded, request a pointer
1254          * to it. If it was previously loaded, we are reinitializing the
1255          * adapter, possibly in a resume from hibernate, in which case
1256          * reject_firmware() cannot be used.
1257          */
1258         if (!fw)
1259                 err = reject_firmware(&fw, fw_name, &nic->pdev->dev);
1260
1261         if (err) {
1262                 if (required) {
1263                         netif_err(nic, probe, nic->netdev,
1264                                   "Failed to load firmware \"%s\": %d\n",
1265                                   fw_name, err);
1266                         netif_err(nic, probe, nic->netdev, "Proceeding without firmware\n");
1267                         return NULL;
1268                 } else {
1269                         netif_info(nic, probe, nic->netdev,
1270                                    "CPUSaver disabled. Needs \"%s\": %d\n",
1271                                    fw_name, err);
1272                         return NULL;
1273                 }
1274         }
1275
1276         /* Firmware should be precisely UCODE_SIZE (words) plus three bytes
1277            indicating the offsets for BUNDLESMALL, BUNDLEMAX, INTDELAY */
1278         if (fw->size != UCODE_SIZE * 4 + 3) {
1279                 netif_err(nic, probe, nic->netdev,
1280                           "Firmware \"%s\" has wrong size %zu\n",
1281                           fw_name, fw->size);
1282                 release_firmware(fw);
1283                 return ERR_PTR(-EINVAL);
1284         }
1285
1286         /* Read timer, bundle and min_size from end of firmware blob */
1287         timer = fw->data[UCODE_SIZE * 4];
1288         bundle = fw->data[UCODE_SIZE * 4 + 1];
1289         min_size = fw->data[UCODE_SIZE * 4 + 2];
1290
1291         if (timer >= UCODE_SIZE || bundle >= UCODE_SIZE ||
1292             min_size >= UCODE_SIZE) {
1293                 netif_err(nic, probe, nic->netdev,
1294                           "\"%s\" has bogus offset values (0x%x,0x%x,0x%x)\n",
1295                           fw_name, timer, bundle, min_size);
1296                 release_firmware(fw);
1297                 return ERR_PTR(-EINVAL);
1298         }
1299
1300         /* OK, firmware is validated and ready to use. Save a pointer
1301          * to it in the nic */
1302         nic->fw = fw;
1303         return fw;
1304 }
1305
1306 static int e100_setup_ucode(struct nic *nic, struct cb *cb,
1307                              struct sk_buff *skb)
1308 {
1309         const struct firmware *fw = (void *)skb;
1310         u8 timer, bundle, min_size;
1311
1312         /* It's not a real skb; we just abused the fact that e100_exec_cb
1313            will pass it through to here... */
1314         cb->skb = NULL;
1315
1316         /* firmware is stored as little endian already */
1317         memcpy(cb->u.ucode, fw->data, UCODE_SIZE * 4);
1318
1319         /* Read timer, bundle and min_size from end of firmware blob */
1320         timer = fw->data[UCODE_SIZE * 4];
1321         bundle = fw->data[UCODE_SIZE * 4 + 1];
1322         min_size = fw->data[UCODE_SIZE * 4 + 2];
1323
1324         /* Insert user-tunable settings in cb->u.ucode */
1325         cb->u.ucode[timer] &= cpu_to_le32(0xFFFF0000);
1326         cb->u.ucode[timer] |= cpu_to_le32(INTDELAY);
1327         cb->u.ucode[bundle] &= cpu_to_le32(0xFFFF0000);
1328         cb->u.ucode[bundle] |= cpu_to_le32(BUNDLEMAX);
1329         cb->u.ucode[min_size] &= cpu_to_le32(0xFFFF0000);
1330         cb->u.ucode[min_size] |= cpu_to_le32((BUNDLESMALL) ? 0xFFFF : 0xFF80);
1331
1332         cb->command = cpu_to_le16(cb_ucode | cb_el);
1333         return 0;
1334 }
1335
1336 static inline int e100_load_ucode_wait(struct nic *nic)
1337 {
1338         const struct firmware *fw;
1339         int err = 0, counter = 50;
1340         struct cb *cb = nic->cb_to_clean;
1341
1342         fw = e100_request_firmware(nic);
1343         /* If it's NULL, then no ucode is required */
1344         if (IS_ERR_OR_NULL(fw))
1345                 return PTR_ERR_OR_ZERO(fw);
1346
1347         if ((err = e100_exec_cb(nic, (void *)fw, e100_setup_ucode)))
1348                 netif_err(nic, probe, nic->netdev,
1349                           "ucode cmd failed with error %d\n", err);
1350
1351         /* must restart cuc */
1352         nic->cuc_cmd = cuc_start;
1353
1354         /* wait for completion */
1355         e100_write_flush(nic);
1356         udelay(10);
1357
1358         /* wait for possibly (ouch) 500ms */
1359         while (!(cb->status & cpu_to_le16(cb_complete))) {
1360                 msleep(10);
1361                 if (!--counter) break;
1362         }
1363
1364         /* ack any interrupts, something could have been set */
1365         iowrite8(~0, &nic->csr->scb.stat_ack);
1366
1367         /* if the command failed, or is not OK, notify and return */
1368         if (!counter || !(cb->status & cpu_to_le16(cb_ok))) {
1369                 netif_err(nic, probe, nic->netdev, "ucode load failed\n");
1370                 err = -EPERM;
1371         }
1372
1373         return err;
1374 }
1375
1376 static int e100_setup_iaaddr(struct nic *nic, struct cb *cb,
1377         struct sk_buff *skb)
1378 {
1379         cb->command = cpu_to_le16(cb_iaaddr);
1380         memcpy(cb->u.iaaddr, nic->netdev->dev_addr, ETH_ALEN);
1381         return 0;
1382 }
1383
1384 static int e100_dump(struct nic *nic, struct cb *cb, struct sk_buff *skb)
1385 {
1386         cb->command = cpu_to_le16(cb_dump);
1387         cb->u.dump_buffer_addr = cpu_to_le32(nic->dma_addr +
1388                 offsetof(struct mem, dump_buf));
1389         return 0;
1390 }
1391
1392 static int e100_phy_check_without_mii(struct nic *nic)
1393 {
1394         u8 phy_type;
1395         int without_mii;
1396
1397         phy_type = (le16_to_cpu(nic->eeprom[eeprom_phy_iface]) >> 8) & 0x0f;
1398
1399         switch (phy_type) {
1400         case NoSuchPhy: /* Non-MII PHY; UNTESTED! */
1401         case I82503: /* Non-MII PHY; UNTESTED! */
1402         case S80C24: /* Non-MII PHY; tested and working */
1403                 /* paragraph from the FreeBSD driver, "FXP_PHY_80C24":
1404                  * The Seeq 80c24 AutoDUPLEX(tm) Ethernet Interface Adapter
1405                  * doesn't have a programming interface of any sort.  The
1406                  * media is sensed automatically based on how the link partner
1407                  * is configured.  This is, in essence, manual configuration.
1408                  */
1409                 netif_info(nic, probe, nic->netdev,
1410                            "found MII-less i82503 or 80c24 or other PHY\n");
1411
1412                 nic->mdio_ctrl = mdio_ctrl_phy_mii_emulated;
1413                 nic->mii.phy_id = 0; /* is this ok for an MII-less PHY? */
1414
1415                 /* these might be needed for certain MII-less cards...
1416                  * nic->flags |= ich;
1417                  * nic->flags |= ich_10h_workaround; */
1418
1419                 without_mii = 1;
1420                 break;
1421         default:
1422                 without_mii = 0;
1423                 break;
1424         }
1425         return without_mii;
1426 }
1427
1428 #define NCONFIG_AUTO_SWITCH     0x0080
1429 #define MII_NSC_CONG            MII_RESV1
1430 #define NSC_CONG_ENABLE         0x0100
1431 #define NSC_CONG_TXREADY        0x0400
1432 #define ADVERTISE_FC_SUPPORTED  0x0400
1433 static int e100_phy_init(struct nic *nic)
1434 {
1435         struct net_device *netdev = nic->netdev;
1436         u32 addr;
1437         u16 bmcr, stat, id_lo, id_hi, cong;
1438
1439         /* Discover phy addr by searching addrs in order {1,0,2,..., 31} */
1440         for (addr = 0; addr < 32; addr++) {
1441                 nic->mii.phy_id = (addr == 0) ? 1 : (addr == 1) ? 0 : addr;
1442                 bmcr = mdio_read(netdev, nic->mii.phy_id, MII_BMCR);
1443                 stat = mdio_read(netdev, nic->mii.phy_id, MII_BMSR);
1444                 stat = mdio_read(netdev, nic->mii.phy_id, MII_BMSR);
1445                 if (!((bmcr == 0xFFFF) || ((stat == 0) && (bmcr == 0))))
1446                         break;
1447         }
1448         if (addr == 32) {
1449                 /* uhoh, no PHY detected: check whether we seem to be some
1450                  * weird, rare variant which is *known* to not have any MII.
1451                  * But do this AFTER MII checking only, since this does
1452                  * lookup of EEPROM values which may easily be unreliable. */
1453                 if (e100_phy_check_without_mii(nic))
1454                         return 0; /* simply return and hope for the best */
1455                 else {
1456                         /* for unknown cases log a fatal error */
1457                         netif_err(nic, hw, nic->netdev,
1458                                   "Failed to locate any known PHY, aborting\n");
1459                         return -EAGAIN;
1460                 }
1461         } else
1462                 netif_printk(nic, hw, KERN_DEBUG, nic->netdev,
1463                              "phy_addr = %d\n", nic->mii.phy_id);
1464
1465         /* Get phy ID */
1466         id_lo = mdio_read(netdev, nic->mii.phy_id, MII_PHYSID1);
1467         id_hi = mdio_read(netdev, nic->mii.phy_id, MII_PHYSID2);
1468         nic->phy = (u32)id_hi << 16 | (u32)id_lo;
1469         netif_printk(nic, hw, KERN_DEBUG, nic->netdev,
1470                      "phy ID = 0x%08X\n", nic->phy);
1471
1472         /* Select the phy and isolate the rest */
1473         for (addr = 0; addr < 32; addr++) {
1474                 if (addr != nic->mii.phy_id) {
1475                         mdio_write(netdev, addr, MII_BMCR, BMCR_ISOLATE);
1476                 } else if (nic->phy != phy_82552_v) {
1477                         bmcr = mdio_read(netdev, addr, MII_BMCR);
1478                         mdio_write(netdev, addr, MII_BMCR,
1479                                 bmcr & ~BMCR_ISOLATE);
1480                 }
1481         }
1482         /*
1483          * Workaround for 82552:
1484          * Clear the ISOLATE bit on selected phy_id last (mirrored on all
1485          * other phy_id's) using bmcr value from addr discovery loop above.
1486          */
1487         if (nic->phy == phy_82552_v)
1488                 mdio_write(netdev, nic->mii.phy_id, MII_BMCR,
1489                         bmcr & ~BMCR_ISOLATE);
1490
1491         /* Handle National tx phys */
1492 #define NCS_PHY_MODEL_MASK      0xFFF0FFFF
1493         if ((nic->phy & NCS_PHY_MODEL_MASK) == phy_nsc_tx) {
1494                 /* Disable congestion control */
1495                 cong = mdio_read(netdev, nic->mii.phy_id, MII_NSC_CONG);
1496                 cong |= NSC_CONG_TXREADY;
1497                 cong &= ~NSC_CONG_ENABLE;
1498                 mdio_write(netdev, nic->mii.phy_id, MII_NSC_CONG, cong);
1499         }
1500
1501         if (nic->phy == phy_82552_v) {
1502                 u16 advert = mdio_read(netdev, nic->mii.phy_id, MII_ADVERTISE);
1503
1504                 /* assign special tweaked mdio_ctrl() function */
1505                 nic->mdio_ctrl = mdio_ctrl_phy_82552_v;
1506
1507                 /* Workaround Si not advertising flow-control during autoneg */
1508                 advert |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
1509                 mdio_write(netdev, nic->mii.phy_id, MII_ADVERTISE, advert);
1510
1511                 /* Reset for the above changes to take effect */
1512                 bmcr = mdio_read(netdev, nic->mii.phy_id, MII_BMCR);
1513                 bmcr |= BMCR_RESET;
1514                 mdio_write(netdev, nic->mii.phy_id, MII_BMCR, bmcr);
1515         } else if ((nic->mac >= mac_82550_D102) || ((nic->flags & ich) &&
1516            (mdio_read(netdev, nic->mii.phy_id, MII_TPISTATUS) & 0x8000) &&
1517            (le16_to_cpu(nic->eeprom[eeprom_cnfg_mdix]) & eeprom_mdix_enabled))) {
1518                 /* enable/disable MDI/MDI-X auto-switching. */
1519                 mdio_write(netdev, nic->mii.phy_id, MII_NCONFIG,
1520                                 nic->mii.force_media ? 0 : NCONFIG_AUTO_SWITCH);
1521         }
1522
1523         return 0;
1524 }
1525
1526 static int e100_hw_init(struct nic *nic)
1527 {
1528         int err = 0;
1529
1530         e100_hw_reset(nic);
1531
1532         netif_err(nic, hw, nic->netdev, "e100_hw_init\n");
1533         if ((err = e100_self_test(nic)))
1534                 return err;
1535
1536         if ((err = e100_phy_init(nic)))
1537                 return err;
1538         if ((err = e100_exec_cmd(nic, cuc_load_base, 0)))
1539                 return err;
1540         if ((err = e100_exec_cmd(nic, ruc_load_base, 0)))
1541                 return err;
1542         if ((err = e100_load_ucode_wait(nic)))
1543                 return err;
1544         if ((err = e100_exec_cb(nic, NULL, e100_configure)))
1545                 return err;
1546         if ((err = e100_exec_cb(nic, NULL, e100_setup_iaaddr)))
1547                 return err;
1548         if ((err = e100_exec_cmd(nic, cuc_dump_addr,
1549                 nic->dma_addr + offsetof(struct mem, stats))))
1550                 return err;
1551         if ((err = e100_exec_cmd(nic, cuc_dump_reset, 0)))
1552                 return err;
1553
1554         e100_disable_irq(nic);
1555
1556         return 0;
1557 }
1558
1559 static int e100_multi(struct nic *nic, struct cb *cb, struct sk_buff *skb)
1560 {
1561         struct net_device *netdev = nic->netdev;
1562         struct netdev_hw_addr *ha;
1563         u16 i, count = min(netdev_mc_count(netdev), E100_MAX_MULTICAST_ADDRS);
1564
1565         cb->command = cpu_to_le16(cb_multi);
1566         cb->u.multi.count = cpu_to_le16(count * ETH_ALEN);
1567         i = 0;
1568         netdev_for_each_mc_addr(ha, netdev) {
1569                 if (i == count)
1570                         break;
1571                 memcpy(&cb->u.multi.addr[i++ * ETH_ALEN], &ha->addr,
1572                         ETH_ALEN);
1573         }
1574         return 0;
1575 }
1576
1577 static void e100_set_multicast_list(struct net_device *netdev)
1578 {
1579         struct nic *nic = netdev_priv(netdev);
1580
1581         netif_printk(nic, hw, KERN_DEBUG, nic->netdev,
1582                      "mc_count=%d, flags=0x%04X\n",
1583                      netdev_mc_count(netdev), netdev->flags);
1584
1585         if (netdev->flags & IFF_PROMISC)
1586                 nic->flags |= promiscuous;
1587         else
1588                 nic->flags &= ~promiscuous;
1589
1590         if (netdev->flags & IFF_ALLMULTI ||
1591                 netdev_mc_count(netdev) > E100_MAX_MULTICAST_ADDRS)
1592                 nic->flags |= multicast_all;
1593         else
1594                 nic->flags &= ~multicast_all;
1595
1596         e100_exec_cb(nic, NULL, e100_configure);
1597         e100_exec_cb(nic, NULL, e100_multi);
1598 }
1599
1600 static void e100_update_stats(struct nic *nic)
1601 {
1602         struct net_device *dev = nic->netdev;
1603         struct net_device_stats *ns = &dev->stats;
1604         struct stats *s = &nic->mem->stats;
1605         __le32 *complete = (nic->mac < mac_82558_D101_A4) ? &s->fc_xmt_pause :
1606                 (nic->mac < mac_82559_D101M) ? (__le32 *)&s->xmt_tco_frames :
1607                 &s->complete;
1608
1609         /* Device's stats reporting may take several microseconds to
1610          * complete, so we're always waiting for results of the
1611          * previous command. */
1612
1613         if (*complete == cpu_to_le32(cuc_dump_reset_complete)) {
1614                 *complete = 0;
1615                 nic->tx_frames = le32_to_cpu(s->tx_good_frames);
1616                 nic->tx_collisions = le32_to_cpu(s->tx_total_collisions);
1617                 ns->tx_aborted_errors += le32_to_cpu(s->tx_max_collisions);
1618                 ns->tx_window_errors += le32_to_cpu(s->tx_late_collisions);
1619                 ns->tx_carrier_errors += le32_to_cpu(s->tx_lost_crs);
1620                 ns->tx_fifo_errors += le32_to_cpu(s->tx_underruns);
1621                 ns->collisions += nic->tx_collisions;
1622                 ns->tx_errors += le32_to_cpu(s->tx_max_collisions) +
1623                         le32_to_cpu(s->tx_lost_crs);
1624                 nic->rx_short_frame_errors +=
1625                         le32_to_cpu(s->rx_short_frame_errors);
1626                 ns->rx_length_errors = nic->rx_short_frame_errors +
1627                         nic->rx_over_length_errors;
1628                 ns->rx_crc_errors += le32_to_cpu(s->rx_crc_errors);
1629                 ns->rx_frame_errors += le32_to_cpu(s->rx_alignment_errors);
1630                 ns->rx_over_errors += le32_to_cpu(s->rx_overrun_errors);
1631                 ns->rx_fifo_errors += le32_to_cpu(s->rx_overrun_errors);
1632                 ns->rx_missed_errors += le32_to_cpu(s->rx_resource_errors);
1633                 ns->rx_errors += le32_to_cpu(s->rx_crc_errors) +
1634                         le32_to_cpu(s->rx_alignment_errors) +
1635                         le32_to_cpu(s->rx_short_frame_errors) +
1636                         le32_to_cpu(s->rx_cdt_errors);
1637                 nic->tx_deferred += le32_to_cpu(s->tx_deferred);
1638                 nic->tx_single_collisions +=
1639                         le32_to_cpu(s->tx_single_collisions);
1640                 nic->tx_multiple_collisions +=
1641                         le32_to_cpu(s->tx_multiple_collisions);
1642                 if (nic->mac >= mac_82558_D101_A4) {
1643                         nic->tx_fc_pause += le32_to_cpu(s->fc_xmt_pause);
1644                         nic->rx_fc_pause += le32_to_cpu(s->fc_rcv_pause);
1645                         nic->rx_fc_unsupported +=
1646                                 le32_to_cpu(s->fc_rcv_unsupported);
1647                         if (nic->mac >= mac_82559_D101M) {
1648                                 nic->tx_tco_frames +=
1649                                         le16_to_cpu(s->xmt_tco_frames);
1650                                 nic->rx_tco_frames +=
1651                                         le16_to_cpu(s->rcv_tco_frames);
1652                         }
1653                 }
1654         }
1655
1656
1657         if (e100_exec_cmd(nic, cuc_dump_reset, 0))
1658                 netif_printk(nic, tx_err, KERN_DEBUG, nic->netdev,
1659                              "exec cuc_dump_reset failed\n");
1660 }
1661
1662 static void e100_adjust_adaptive_ifs(struct nic *nic, int speed, int duplex)
1663 {
1664         /* Adjust inter-frame-spacing (IFS) between two transmits if
1665          * we're getting collisions on a half-duplex connection. */
1666
1667         if (duplex == DUPLEX_HALF) {
1668                 u32 prev = nic->adaptive_ifs;
1669                 u32 min_frames = (speed == SPEED_100) ? 1000 : 100;
1670
1671                 if ((nic->tx_frames / 32 < nic->tx_collisions) &&
1672                    (nic->tx_frames > min_frames)) {
1673                         if (nic->adaptive_ifs < 60)
1674                                 nic->adaptive_ifs += 5;
1675                 } else if (nic->tx_frames < min_frames) {
1676                         if (nic->adaptive_ifs >= 5)
1677                                 nic->adaptive_ifs -= 5;
1678                 }
1679                 if (nic->adaptive_ifs != prev)
1680                         e100_exec_cb(nic, NULL, e100_configure);
1681         }
1682 }
1683
1684 static void e100_watchdog(struct timer_list *t)
1685 {
1686         struct nic *nic = from_timer(nic, t, watchdog);
1687         struct ethtool_cmd cmd = { .cmd = ETHTOOL_GSET };
1688         u32 speed;
1689
1690         netif_printk(nic, timer, KERN_DEBUG, nic->netdev,
1691                      "right now = %ld\n", jiffies);
1692
1693         /* mii library handles link maintenance tasks */
1694
1695         mii_ethtool_gset(&nic->mii, &cmd);
1696         speed = ethtool_cmd_speed(&cmd);
1697
1698         if (mii_link_ok(&nic->mii) && !netif_carrier_ok(nic->netdev)) {
1699                 netdev_info(nic->netdev, "NIC Link is Up %u Mbps %s Duplex\n",
1700                             speed == SPEED_100 ? 100 : 10,
1701                             cmd.duplex == DUPLEX_FULL ? "Full" : "Half");
1702         } else if (!mii_link_ok(&nic->mii) && netif_carrier_ok(nic->netdev)) {
1703                 netdev_info(nic->netdev, "NIC Link is Down\n");
1704         }
1705
1706         mii_check_link(&nic->mii);
1707
1708         /* Software generated interrupt to recover from (rare) Rx
1709          * allocation failure.
1710          * Unfortunately have to use a spinlock to not re-enable interrupts
1711          * accidentally, due to hardware that shares a register between the
1712          * interrupt mask bit and the SW Interrupt generation bit */
1713         spin_lock_irq(&nic->cmd_lock);
1714         iowrite8(ioread8(&nic->csr->scb.cmd_hi) | irq_sw_gen,&nic->csr->scb.cmd_hi);
1715         e100_write_flush(nic);
1716         spin_unlock_irq(&nic->cmd_lock);
1717
1718         e100_update_stats(nic);
1719         e100_adjust_adaptive_ifs(nic, speed, cmd.duplex);
1720
1721         if (nic->mac <= mac_82557_D100_C)
1722                 /* Issue a multicast command to workaround a 557 lock up */
1723                 e100_set_multicast_list(nic->netdev);
1724
1725         if (nic->flags & ich && speed == SPEED_10 && cmd.duplex == DUPLEX_HALF)
1726                 /* Need SW workaround for ICH[x] 10Mbps/half duplex Tx hang. */
1727                 nic->flags |= ich_10h_workaround;
1728         else
1729                 nic->flags &= ~ich_10h_workaround;
1730
1731         mod_timer(&nic->watchdog,
1732                   round_jiffies(jiffies + E100_WATCHDOG_PERIOD));
1733 }
1734
1735 static int e100_xmit_prepare(struct nic *nic, struct cb *cb,
1736         struct sk_buff *skb)
1737 {
1738         dma_addr_t dma_addr;
1739         cb->command = nic->tx_command;
1740
1741         dma_addr = dma_map_single(&nic->pdev->dev, skb->data, skb->len,
1742                                   DMA_TO_DEVICE);
1743         /* If we can't map the skb, have the upper layer try later */
1744         if (dma_mapping_error(&nic->pdev->dev, dma_addr))
1745                 return -ENOMEM;
1746
1747         /*
1748          * Use the last 4 bytes of the SKB payload packet as the CRC, used for
1749          * testing, ie sending frames with bad CRC.
1750          */
1751         if (unlikely(skb->no_fcs))
1752                 cb->command |= cpu_to_le16(cb_tx_nc);
1753         else
1754                 cb->command &= ~cpu_to_le16(cb_tx_nc);
1755
1756         /* interrupt every 16 packets regardless of delay */
1757         if ((nic->cbs_avail & ~15) == nic->cbs_avail)
1758                 cb->command |= cpu_to_le16(cb_i);
1759         cb->u.tcb.tbd_array = cb->dma_addr + offsetof(struct cb, u.tcb.tbd);
1760         cb->u.tcb.tcb_byte_count = 0;
1761         cb->u.tcb.threshold = nic->tx_threshold;
1762         cb->u.tcb.tbd_count = 1;
1763         cb->u.tcb.tbd.buf_addr = cpu_to_le32(dma_addr);
1764         cb->u.tcb.tbd.size = cpu_to_le16(skb->len);
1765         skb_tx_timestamp(skb);
1766         return 0;
1767 }
1768
1769 static netdev_tx_t e100_xmit_frame(struct sk_buff *skb,
1770                                    struct net_device *netdev)
1771 {
1772         struct nic *nic = netdev_priv(netdev);
1773         int err;
1774
1775         if (nic->flags & ich_10h_workaround) {
1776                 /* SW workaround for ICH[x] 10Mbps/half duplex Tx hang.
1777                    Issue a NOP command followed by a 1us delay before
1778                    issuing the Tx command. */
1779                 if (e100_exec_cmd(nic, cuc_nop, 0))
1780                         netif_printk(nic, tx_err, KERN_DEBUG, nic->netdev,
1781                                      "exec cuc_nop failed\n");
1782                 udelay(1);
1783         }
1784
1785         err = e100_exec_cb(nic, skb, e100_xmit_prepare);
1786
1787         switch (err) {
1788         case -ENOSPC:
1789                 /* We queued the skb, but now we're out of space. */
1790                 netif_printk(nic, tx_err, KERN_DEBUG, nic->netdev,
1791                              "No space for CB\n");
1792                 netif_stop_queue(netdev);
1793                 break;
1794         case -ENOMEM:
1795                 /* This is a hard error - log it. */
1796                 netif_printk(nic, tx_err, KERN_DEBUG, nic->netdev,
1797                              "Out of Tx resources, returning skb\n");
1798                 netif_stop_queue(netdev);
1799                 return NETDEV_TX_BUSY;
1800         }
1801
1802         return NETDEV_TX_OK;
1803 }
1804
1805 static int e100_tx_clean(struct nic *nic)
1806 {
1807         struct net_device *dev = nic->netdev;
1808         struct cb *cb;
1809         int tx_cleaned = 0;
1810
1811         spin_lock(&nic->cb_lock);
1812
1813         /* Clean CBs marked complete */
1814         for (cb = nic->cb_to_clean;
1815             cb->status & cpu_to_le16(cb_complete);
1816             cb = nic->cb_to_clean = cb->next) {
1817                 dma_rmb(); /* read skb after status */
1818                 netif_printk(nic, tx_done, KERN_DEBUG, nic->netdev,
1819                              "cb[%d]->status = 0x%04X\n",
1820                              (int)(((void*)cb - (void*)nic->cbs)/sizeof(struct cb)),
1821                              cb->status);
1822
1823                 if (likely(cb->skb != NULL)) {
1824                         dev->stats.tx_packets++;
1825                         dev->stats.tx_bytes += cb->skb->len;
1826
1827                         dma_unmap_single(&nic->pdev->dev,
1828                                          le32_to_cpu(cb->u.tcb.tbd.buf_addr),
1829                                          le16_to_cpu(cb->u.tcb.tbd.size),
1830                                          DMA_TO_DEVICE);
1831                         dev_kfree_skb_any(cb->skb);
1832                         cb->skb = NULL;
1833                         tx_cleaned = 1;
1834                 }
1835                 cb->status = 0;
1836                 nic->cbs_avail++;
1837         }
1838
1839         spin_unlock(&nic->cb_lock);
1840
1841         /* Recover from running out of Tx resources in xmit_frame */
1842         if (unlikely(tx_cleaned && netif_queue_stopped(nic->netdev)))
1843                 netif_wake_queue(nic->netdev);
1844
1845         return tx_cleaned;
1846 }
1847
1848 static void e100_clean_cbs(struct nic *nic)
1849 {
1850         if (nic->cbs) {
1851                 while (nic->cbs_avail != nic->params.cbs.count) {
1852                         struct cb *cb = nic->cb_to_clean;
1853                         if (cb->skb) {
1854                                 dma_unmap_single(&nic->pdev->dev,
1855                                                  le32_to_cpu(cb->u.tcb.tbd.buf_addr),
1856                                                  le16_to_cpu(cb->u.tcb.tbd.size),
1857                                                  DMA_TO_DEVICE);
1858                                 dev_kfree_skb(cb->skb);
1859                         }
1860                         nic->cb_to_clean = nic->cb_to_clean->next;
1861                         nic->cbs_avail++;
1862                 }
1863                 dma_pool_free(nic->cbs_pool, nic->cbs, nic->cbs_dma_addr);
1864                 nic->cbs = NULL;
1865                 nic->cbs_avail = 0;
1866         }
1867         nic->cuc_cmd = cuc_start;
1868         nic->cb_to_use = nic->cb_to_send = nic->cb_to_clean =
1869                 nic->cbs;
1870 }
1871
1872 static int e100_alloc_cbs(struct nic *nic)
1873 {
1874         struct cb *cb;
1875         unsigned int i, count = nic->params.cbs.count;
1876
1877         nic->cuc_cmd = cuc_start;
1878         nic->cb_to_use = nic->cb_to_send = nic->cb_to_clean = NULL;
1879         nic->cbs_avail = 0;
1880
1881         nic->cbs = dma_pool_zalloc(nic->cbs_pool, GFP_KERNEL,
1882                                    &nic->cbs_dma_addr);
1883         if (!nic->cbs)
1884                 return -ENOMEM;
1885
1886         for (cb = nic->cbs, i = 0; i < count; cb++, i++) {
1887                 cb->next = (i + 1 < count) ? cb + 1 : nic->cbs;
1888                 cb->prev = (i == 0) ? nic->cbs + count - 1 : cb - 1;
1889
1890                 cb->dma_addr = nic->cbs_dma_addr + i * sizeof(struct cb);
1891                 cb->link = cpu_to_le32(nic->cbs_dma_addr +
1892                         ((i+1) % count) * sizeof(struct cb));
1893         }
1894
1895         nic->cb_to_use = nic->cb_to_send = nic->cb_to_clean = nic->cbs;
1896         nic->cbs_avail = count;
1897
1898         return 0;
1899 }
1900
1901 static inline void e100_start_receiver(struct nic *nic, struct rx *rx)
1902 {
1903         if (!nic->rxs) return;
1904         if (RU_SUSPENDED != nic->ru_running) return;
1905
1906         /* handle init time starts */
1907         if (!rx) rx = nic->rxs;
1908
1909         /* (Re)start RU if suspended or idle and RFA is non-NULL */
1910         if (rx->skb) {
1911                 e100_exec_cmd(nic, ruc_start, rx->dma_addr);
1912                 nic->ru_running = RU_RUNNING;
1913         }
1914 }
1915
1916 #define RFD_BUF_LEN (sizeof(struct rfd) + VLAN_ETH_FRAME_LEN + ETH_FCS_LEN)
1917 static int e100_rx_alloc_skb(struct nic *nic, struct rx *rx)
1918 {
1919         if (!(rx->skb = netdev_alloc_skb_ip_align(nic->netdev, RFD_BUF_LEN)))
1920                 return -ENOMEM;
1921
1922         /* Init, and map the RFD. */
1923         skb_copy_to_linear_data(rx->skb, &nic->blank_rfd, sizeof(struct rfd));
1924         rx->dma_addr = dma_map_single(&nic->pdev->dev, rx->skb->data,
1925                                       RFD_BUF_LEN, DMA_BIDIRECTIONAL);
1926
1927         if (dma_mapping_error(&nic->pdev->dev, rx->dma_addr)) {
1928                 dev_kfree_skb_any(rx->skb);
1929                 rx->skb = NULL;
1930                 rx->dma_addr = 0;
1931                 return -ENOMEM;
1932         }
1933
1934         /* Link the RFD to end of RFA by linking previous RFD to
1935          * this one.  We are safe to touch the previous RFD because
1936          * it is protected by the before last buffer's el bit being set */
1937         if (rx->prev->skb) {
1938                 struct rfd *prev_rfd = (struct rfd *)rx->prev->skb->data;
1939                 put_unaligned_le32(rx->dma_addr, &prev_rfd->link);
1940                 dma_sync_single_for_device(&nic->pdev->dev,
1941                                            rx->prev->dma_addr,
1942                                            sizeof(struct rfd),
1943                                            DMA_BIDIRECTIONAL);
1944         }
1945
1946         return 0;
1947 }
1948
1949 static int e100_rx_indicate(struct nic *nic, struct rx *rx,
1950         unsigned int *work_done, unsigned int work_to_do)
1951 {
1952         struct net_device *dev = nic->netdev;
1953         struct sk_buff *skb = rx->skb;
1954         struct rfd *rfd = (struct rfd *)skb->data;
1955         u16 rfd_status, actual_size;
1956         u16 fcs_pad = 0;
1957
1958         if (unlikely(work_done && *work_done >= work_to_do))
1959                 return -EAGAIN;
1960
1961         /* Need to sync before taking a peek at cb_complete bit */
1962         dma_sync_single_for_cpu(&nic->pdev->dev, rx->dma_addr,
1963                                 sizeof(struct rfd), DMA_BIDIRECTIONAL);
1964         rfd_status = le16_to_cpu(rfd->status);
1965
1966         netif_printk(nic, rx_status, KERN_DEBUG, nic->netdev,
1967                      "status=0x%04X\n", rfd_status);
1968         dma_rmb(); /* read size after status bit */
1969
1970         /* If data isn't ready, nothing to indicate */
1971         if (unlikely(!(rfd_status & cb_complete))) {
1972                 /* If the next buffer has the el bit, but we think the receiver
1973                  * is still running, check to see if it really stopped while
1974                  * we had interrupts off.
1975                  * This allows for a fast restart without re-enabling
1976                  * interrupts */
1977                 if ((le16_to_cpu(rfd->command) & cb_el) &&
1978                     (RU_RUNNING == nic->ru_running))
1979
1980                         if (ioread8(&nic->csr->scb.status) & rus_no_res)
1981                                 nic->ru_running = RU_SUSPENDED;
1982                 dma_sync_single_for_device(&nic->pdev->dev, rx->dma_addr,
1983                                            sizeof(struct rfd),
1984                                            DMA_FROM_DEVICE);
1985                 return -ENODATA;
1986         }
1987
1988         /* Get actual data size */
1989         if (unlikely(dev->features & NETIF_F_RXFCS))
1990                 fcs_pad = 4;
1991         actual_size = le16_to_cpu(rfd->actual_size) & 0x3FFF;
1992         if (unlikely(actual_size > RFD_BUF_LEN - sizeof(struct rfd)))
1993                 actual_size = RFD_BUF_LEN - sizeof(struct rfd);
1994
1995         /* Get data */
1996         dma_unmap_single(&nic->pdev->dev, rx->dma_addr, RFD_BUF_LEN,
1997                          DMA_BIDIRECTIONAL);
1998
1999         /* If this buffer has the el bit, but we think the receiver
2000          * is still running, check to see if it really stopped while
2001          * we had interrupts off.
2002          * This allows for a fast restart without re-enabling interrupts.
2003          * This can happen when the RU sees the size change but also sees
2004          * the el bit set. */
2005         if ((le16_to_cpu(rfd->command) & cb_el) &&
2006             (RU_RUNNING == nic->ru_running)) {
2007
2008             if (ioread8(&nic->csr->scb.status) & rus_no_res)
2009                 nic->ru_running = RU_SUSPENDED;
2010         }
2011
2012         /* Pull off the RFD and put the actual data (minus eth hdr) */
2013         skb_reserve(skb, sizeof(struct rfd));
2014         skb_put(skb, actual_size);
2015         skb->protocol = eth_type_trans(skb, nic->netdev);
2016
2017         /* If we are receiving all frames, then don't bother
2018          * checking for errors.
2019          */
2020         if (unlikely(dev->features & NETIF_F_RXALL)) {
2021                 if (actual_size > ETH_DATA_LEN + VLAN_ETH_HLEN + fcs_pad)
2022                         /* Received oversized frame, but keep it. */
2023                         nic->rx_over_length_errors++;
2024                 goto process_skb;
2025         }
2026
2027         if (unlikely(!(rfd_status & cb_ok))) {
2028                 /* Don't indicate if hardware indicates errors */
2029                 dev_kfree_skb_any(skb);
2030         } else if (actual_size > ETH_DATA_LEN + VLAN_ETH_HLEN + fcs_pad) {
2031                 /* Don't indicate oversized frames */
2032                 nic->rx_over_length_errors++;
2033                 dev_kfree_skb_any(skb);
2034         } else {
2035 process_skb:
2036                 dev->stats.rx_packets++;
2037                 dev->stats.rx_bytes += (actual_size - fcs_pad);
2038                 netif_receive_skb(skb);
2039                 if (work_done)
2040                         (*work_done)++;
2041         }
2042
2043         rx->skb = NULL;
2044
2045         return 0;
2046 }
2047
2048 static void e100_rx_clean(struct nic *nic, unsigned int *work_done,
2049         unsigned int work_to_do)
2050 {
2051         struct rx *rx;
2052         int restart_required = 0, err = 0;
2053         struct rx *old_before_last_rx, *new_before_last_rx;
2054         struct rfd *old_before_last_rfd, *new_before_last_rfd;
2055
2056         /* Indicate newly arrived packets */
2057         for (rx = nic->rx_to_clean; rx->skb; rx = nic->rx_to_clean = rx->next) {
2058                 err = e100_rx_indicate(nic, rx, work_done, work_to_do);
2059                 /* Hit quota or no more to clean */
2060                 if (-EAGAIN == err || -ENODATA == err)
2061                         break;
2062         }
2063
2064
2065         /* On EAGAIN, hit quota so have more work to do, restart once
2066          * cleanup is complete.
2067          * Else, are we already rnr? then pay attention!!! this ensures that
2068          * the state machine progression never allows a start with a
2069          * partially cleaned list, avoiding a race between hardware
2070          * and rx_to_clean when in NAPI mode */
2071         if (-EAGAIN != err && RU_SUSPENDED == nic->ru_running)
2072                 restart_required = 1;
2073
2074         old_before_last_rx = nic->rx_to_use->prev->prev;
2075         old_before_last_rfd = (struct rfd *)old_before_last_rx->skb->data;
2076
2077         /* Alloc new skbs to refill list */
2078         for (rx = nic->rx_to_use; !rx->skb; rx = nic->rx_to_use = rx->next) {
2079                 if (unlikely(e100_rx_alloc_skb(nic, rx)))
2080                         break; /* Better luck next time (see watchdog) */
2081         }
2082
2083         new_before_last_rx = nic->rx_to_use->prev->prev;
2084         if (new_before_last_rx != old_before_last_rx) {
2085                 /* Set the el-bit on the buffer that is before the last buffer.
2086                  * This lets us update the next pointer on the last buffer
2087                  * without worrying about hardware touching it.
2088                  * We set the size to 0 to prevent hardware from touching this
2089                  * buffer.
2090                  * When the hardware hits the before last buffer with el-bit
2091                  * and size of 0, it will RNR interrupt, the RUS will go into
2092                  * the No Resources state.  It will not complete nor write to
2093                  * this buffer. */
2094                 new_before_last_rfd =
2095                         (struct rfd *)new_before_last_rx->skb->data;
2096                 new_before_last_rfd->size = 0;
2097                 new_before_last_rfd->command |= cpu_to_le16(cb_el);
2098                 dma_sync_single_for_device(&nic->pdev->dev,
2099                                            new_before_last_rx->dma_addr,
2100                                            sizeof(struct rfd),
2101                                            DMA_BIDIRECTIONAL);
2102
2103                 /* Now that we have a new stopping point, we can clear the old
2104                  * stopping point.  We must sync twice to get the proper
2105                  * ordering on the hardware side of things. */
2106                 old_before_last_rfd->command &= ~cpu_to_le16(cb_el);
2107                 dma_sync_single_for_device(&nic->pdev->dev,
2108                                            old_before_last_rx->dma_addr,
2109                                            sizeof(struct rfd),
2110                                            DMA_BIDIRECTIONAL);
2111                 old_before_last_rfd->size = cpu_to_le16(VLAN_ETH_FRAME_LEN
2112                                                         + ETH_FCS_LEN);
2113                 dma_sync_single_for_device(&nic->pdev->dev,
2114                                            old_before_last_rx->dma_addr,
2115                                            sizeof(struct rfd),
2116                                            DMA_BIDIRECTIONAL);
2117         }
2118
2119         if (restart_required) {
2120                 // ack the rnr?
2121                 iowrite8(stat_ack_rnr, &nic->csr->scb.stat_ack);
2122                 e100_start_receiver(nic, nic->rx_to_clean);
2123                 if (work_done)
2124                         (*work_done)++;
2125         }
2126 }
2127
2128 static void e100_rx_clean_list(struct nic *nic)
2129 {
2130         struct rx *rx;
2131         unsigned int i, count = nic->params.rfds.count;
2132
2133         nic->ru_running = RU_UNINITIALIZED;
2134
2135         if (nic->rxs) {
2136                 for (rx = nic->rxs, i = 0; i < count; rx++, i++) {
2137                         if (rx->skb) {
2138                                 dma_unmap_single(&nic->pdev->dev,
2139                                                  rx->dma_addr, RFD_BUF_LEN,
2140                                                  DMA_BIDIRECTIONAL);
2141                                 dev_kfree_skb(rx->skb);
2142                         }
2143                 }
2144                 kfree(nic->rxs);
2145                 nic->rxs = NULL;
2146         }
2147
2148         nic->rx_to_use = nic->rx_to_clean = NULL;
2149 }
2150
2151 static int e100_rx_alloc_list(struct nic *nic)
2152 {
2153         struct rx *rx;
2154         unsigned int i, count = nic->params.rfds.count;
2155         struct rfd *before_last;
2156
2157         nic->rx_to_use = nic->rx_to_clean = NULL;
2158         nic->ru_running = RU_UNINITIALIZED;
2159
2160         if (!(nic->rxs = kcalloc(count, sizeof(struct rx), GFP_KERNEL)))
2161                 return -ENOMEM;
2162
2163         for (rx = nic->rxs, i = 0; i < count; rx++, i++) {
2164                 rx->next = (i + 1 < count) ? rx + 1 : nic->rxs;
2165                 rx->prev = (i == 0) ? nic->rxs + count - 1 : rx - 1;
2166                 if (e100_rx_alloc_skb(nic, rx)) {
2167                         e100_rx_clean_list(nic);
2168                         return -ENOMEM;
2169                 }
2170         }
2171         /* Set the el-bit on the buffer that is before the last buffer.
2172          * This lets us update the next pointer on the last buffer without
2173          * worrying about hardware touching it.
2174          * We set the size to 0 to prevent hardware from touching this buffer.
2175          * When the hardware hits the before last buffer with el-bit and size
2176          * of 0, it will RNR interrupt, the RU will go into the No Resources
2177          * state.  It will not complete nor write to this buffer. */
2178         rx = nic->rxs->prev->prev;
2179         before_last = (struct rfd *)rx->skb->data;
2180         before_last->command |= cpu_to_le16(cb_el);
2181         before_last->size = 0;
2182         dma_sync_single_for_device(&nic->pdev->dev, rx->dma_addr,
2183                                    sizeof(struct rfd), DMA_BIDIRECTIONAL);
2184
2185         nic->rx_to_use = nic->rx_to_clean = nic->rxs;
2186         nic->ru_running = RU_SUSPENDED;
2187
2188         return 0;
2189 }
2190
2191 static irqreturn_t e100_intr(int irq, void *dev_id)
2192 {
2193         struct net_device *netdev = dev_id;
2194         struct nic *nic = netdev_priv(netdev);
2195         u8 stat_ack = ioread8(&nic->csr->scb.stat_ack);
2196
2197         netif_printk(nic, intr, KERN_DEBUG, nic->netdev,
2198                      "stat_ack = 0x%02X\n", stat_ack);
2199
2200         if (stat_ack == stat_ack_not_ours ||    /* Not our interrupt */
2201            stat_ack == stat_ack_not_present)    /* Hardware is ejected */
2202                 return IRQ_NONE;
2203
2204         /* Ack interrupt(s) */
2205         iowrite8(stat_ack, &nic->csr->scb.stat_ack);
2206
2207         /* We hit Receive No Resource (RNR); restart RU after cleaning */
2208         if (stat_ack & stat_ack_rnr)
2209                 nic->ru_running = RU_SUSPENDED;
2210
2211         if (likely(napi_schedule_prep(&nic->napi))) {
2212                 e100_disable_irq(nic);
2213                 __napi_schedule(&nic->napi);
2214         }
2215
2216         return IRQ_HANDLED;
2217 }
2218
2219 static int e100_poll(struct napi_struct *napi, int budget)
2220 {
2221         struct nic *nic = container_of(napi, struct nic, napi);
2222         unsigned int work_done = 0;
2223
2224         e100_rx_clean(nic, &work_done, budget);
2225         e100_tx_clean(nic);
2226
2227         /* If budget fully consumed, continue polling */
2228         if (work_done == budget)
2229                 return budget;
2230
2231         /* only re-enable interrupt if stack agrees polling is really done */
2232         if (likely(napi_complete_done(napi, work_done)))
2233                 e100_enable_irq(nic);
2234
2235         return work_done;
2236 }
2237
2238 #ifdef CONFIG_NET_POLL_CONTROLLER
2239 static void e100_netpoll(struct net_device *netdev)
2240 {
2241         struct nic *nic = netdev_priv(netdev);
2242
2243         e100_disable_irq(nic);
2244         e100_intr(nic->pdev->irq, netdev);
2245         e100_tx_clean(nic);
2246         e100_enable_irq(nic);
2247 }
2248 #endif
2249
2250 static int e100_set_mac_address(struct net_device *netdev, void *p)
2251 {
2252         struct nic *nic = netdev_priv(netdev);
2253         struct sockaddr *addr = p;
2254
2255         if (!is_valid_ether_addr(addr->sa_data))
2256                 return -EADDRNOTAVAIL;
2257
2258         memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
2259         e100_exec_cb(nic, NULL, e100_setup_iaaddr);
2260
2261         return 0;
2262 }
2263
2264 static int e100_asf(struct nic *nic)
2265 {
2266         /* ASF can be enabled from eeprom */
2267         return (nic->pdev->device >= 0x1050) && (nic->pdev->device <= 0x1057) &&
2268            (le16_to_cpu(nic->eeprom[eeprom_config_asf]) & eeprom_asf) &&
2269            !(le16_to_cpu(nic->eeprom[eeprom_config_asf]) & eeprom_gcl) &&
2270            ((le16_to_cpu(nic->eeprom[eeprom_smbus_addr]) & 0xFF) != 0xFE);
2271 }
2272
2273 static int e100_up(struct nic *nic)
2274 {
2275         int err;
2276
2277         if ((err = e100_rx_alloc_list(nic)))
2278                 return err;
2279         if ((err = e100_alloc_cbs(nic)))
2280                 goto err_rx_clean_list;
2281         if ((err = e100_hw_init(nic)))
2282                 goto err_clean_cbs;
2283         e100_set_multicast_list(nic->netdev);
2284         e100_start_receiver(nic, NULL);
2285         mod_timer(&nic->watchdog, jiffies);
2286         if ((err = request_irq(nic->pdev->irq, e100_intr, IRQF_SHARED,
2287                 nic->netdev->name, nic->netdev)))
2288                 goto err_no_irq;
2289         netif_wake_queue(nic->netdev);
2290         napi_enable(&nic->napi);
2291         /* enable ints _after_ enabling poll, preventing a race between
2292          * disable ints+schedule */
2293         e100_enable_irq(nic);
2294         return 0;
2295
2296 err_no_irq:
2297         del_timer_sync(&nic->watchdog);
2298 err_clean_cbs:
2299         e100_clean_cbs(nic);
2300 err_rx_clean_list:
2301         e100_rx_clean_list(nic);
2302         return err;
2303 }
2304
2305 static void e100_down(struct nic *nic)
2306 {
2307         /* wait here for poll to complete */
2308         napi_disable(&nic->napi);
2309         netif_stop_queue(nic->netdev);
2310         e100_hw_reset(nic);
2311         free_irq(nic->pdev->irq, nic->netdev);
2312         del_timer_sync(&nic->watchdog);
2313         netif_carrier_off(nic->netdev);
2314         e100_clean_cbs(nic);
2315         e100_rx_clean_list(nic);
2316 }
2317
2318 static void e100_tx_timeout(struct net_device *netdev, unsigned int txqueue)
2319 {
2320         struct nic *nic = netdev_priv(netdev);
2321
2322         /* Reset outside of interrupt context, to avoid request_irq
2323          * in interrupt context */
2324         schedule_work(&nic->tx_timeout_task);
2325 }
2326
2327 static void e100_tx_timeout_task(struct work_struct *work)
2328 {
2329         struct nic *nic = container_of(work, struct nic, tx_timeout_task);
2330         struct net_device *netdev = nic->netdev;
2331
2332         netif_printk(nic, tx_err, KERN_DEBUG, nic->netdev,
2333                      "scb.status=0x%02X\n", ioread8(&nic->csr->scb.status));
2334
2335         rtnl_lock();
2336         if (netif_running(netdev)) {
2337                 e100_down(netdev_priv(netdev));
2338                 e100_up(netdev_priv(netdev));
2339         }
2340         rtnl_unlock();
2341 }
2342
2343 static int e100_loopback_test(struct nic *nic, enum loopback loopback_mode)
2344 {
2345         int err;
2346         struct sk_buff *skb;
2347
2348         /* Use driver resources to perform internal MAC or PHY
2349          * loopback test.  A single packet is prepared and transmitted
2350          * in loopback mode, and the test passes if the received
2351          * packet compares byte-for-byte to the transmitted packet. */
2352
2353         if ((err = e100_rx_alloc_list(nic)))
2354                 return err;
2355         if ((err = e100_alloc_cbs(nic)))
2356                 goto err_clean_rx;
2357
2358         /* ICH PHY loopback is broken so do MAC loopback instead */
2359         if (nic->flags & ich && loopback_mode == lb_phy)
2360                 loopback_mode = lb_mac;
2361
2362         nic->loopback = loopback_mode;
2363         if ((err = e100_hw_init(nic)))
2364                 goto err_loopback_none;
2365
2366         if (loopback_mode == lb_phy)
2367                 mdio_write(nic->netdev, nic->mii.phy_id, MII_BMCR,
2368                         BMCR_LOOPBACK);
2369
2370         e100_start_receiver(nic, NULL);
2371
2372         if (!(skb = netdev_alloc_skb(nic->netdev, ETH_DATA_LEN))) {
2373                 err = -ENOMEM;
2374                 goto err_loopback_none;
2375         }
2376         skb_put(skb, ETH_DATA_LEN);
2377         memset(skb->data, 0xFF, ETH_DATA_LEN);
2378         e100_xmit_frame(skb, nic->netdev);
2379
2380         msleep(10);
2381
2382         dma_sync_single_for_cpu(&nic->pdev->dev, nic->rx_to_clean->dma_addr,
2383                                 RFD_BUF_LEN, DMA_BIDIRECTIONAL);
2384
2385         if (memcmp(nic->rx_to_clean->skb->data + sizeof(struct rfd),
2386            skb->data, ETH_DATA_LEN))
2387                 err = -EAGAIN;
2388
2389 err_loopback_none:
2390         mdio_write(nic->netdev, nic->mii.phy_id, MII_BMCR, 0);
2391         nic->loopback = lb_none;
2392         e100_clean_cbs(nic);
2393         e100_hw_reset(nic);
2394 err_clean_rx:
2395         e100_rx_clean_list(nic);
2396         return err;
2397 }
2398
2399 #define MII_LED_CONTROL 0x1B
2400 #define E100_82552_LED_OVERRIDE 0x19
2401 #define E100_82552_LED_ON       0x000F /* LEDTX and LED_RX both on */
2402 #define E100_82552_LED_OFF      0x000A /* LEDTX and LED_RX both off */
2403
2404 static int e100_get_link_ksettings(struct net_device *netdev,
2405                                    struct ethtool_link_ksettings *cmd)
2406 {
2407         struct nic *nic = netdev_priv(netdev);
2408
2409         mii_ethtool_get_link_ksettings(&nic->mii, cmd);
2410
2411         return 0;
2412 }
2413
2414 static int e100_set_link_ksettings(struct net_device *netdev,
2415                                    const struct ethtool_link_ksettings *cmd)
2416 {
2417         struct nic *nic = netdev_priv(netdev);
2418         int err;
2419
2420         mdio_write(netdev, nic->mii.phy_id, MII_BMCR, BMCR_RESET);
2421         err = mii_ethtool_set_link_ksettings(&nic->mii, cmd);
2422         e100_exec_cb(nic, NULL, e100_configure);
2423
2424         return err;
2425 }
2426
2427 static void e100_get_drvinfo(struct net_device *netdev,
2428         struct ethtool_drvinfo *info)
2429 {
2430         struct nic *nic = netdev_priv(netdev);
2431         strlcpy(info->driver, DRV_NAME, sizeof(info->driver));
2432         strlcpy(info->bus_info, pci_name(nic->pdev),
2433                 sizeof(info->bus_info));
2434 }
2435
2436 #define E100_PHY_REGS 0x1D
2437 static int e100_get_regs_len(struct net_device *netdev)
2438 {
2439         struct nic *nic = netdev_priv(netdev);
2440
2441         /* We know the number of registers, and the size of the dump buffer.
2442          * Calculate the total size in bytes.
2443          */
2444         return (1 + E100_PHY_REGS) * sizeof(u32) + sizeof(nic->mem->dump_buf);
2445 }
2446
2447 static void e100_get_regs(struct net_device *netdev,
2448         struct ethtool_regs *regs, void *p)
2449 {
2450         struct nic *nic = netdev_priv(netdev);
2451         u32 *buff = p;
2452         int i;
2453
2454         regs->version = (1 << 24) | nic->pdev->revision;
2455         buff[0] = ioread8(&nic->csr->scb.cmd_hi) << 24 |
2456                 ioread8(&nic->csr->scb.cmd_lo) << 16 |
2457                 ioread16(&nic->csr->scb.status);
2458         for (i = 0; i < E100_PHY_REGS; i++)
2459                 /* Note that we read the registers in reverse order. This
2460                  * ordering is the ABI apparently used by ethtool and other
2461                  * applications.
2462                  */
2463                 buff[1 + i] = mdio_read(netdev, nic->mii.phy_id,
2464                                         E100_PHY_REGS - 1 - i);
2465         memset(nic->mem->dump_buf, 0, sizeof(nic->mem->dump_buf));
2466         e100_exec_cb(nic, NULL, e100_dump);
2467         msleep(10);
2468         memcpy(&buff[1 + E100_PHY_REGS], nic->mem->dump_buf,
2469                sizeof(nic->mem->dump_buf));
2470 }
2471
2472 static void e100_get_wol(struct net_device *netdev, struct ethtool_wolinfo *wol)
2473 {
2474         struct nic *nic = netdev_priv(netdev);
2475         wol->supported = (nic->mac >= mac_82558_D101_A4) ?  WAKE_MAGIC : 0;
2476         wol->wolopts = (nic->flags & wol_magic) ? WAKE_MAGIC : 0;
2477 }
2478
2479 static int e100_set_wol(struct net_device *netdev, struct ethtool_wolinfo *wol)
2480 {
2481         struct nic *nic = netdev_priv(netdev);
2482
2483         if ((wol->wolopts && wol->wolopts != WAKE_MAGIC) ||
2484             !device_can_wakeup(&nic->pdev->dev))
2485                 return -EOPNOTSUPP;
2486
2487         if (wol->wolopts)
2488                 nic->flags |= wol_magic;
2489         else
2490                 nic->flags &= ~wol_magic;
2491
2492         device_set_wakeup_enable(&nic->pdev->dev, wol->wolopts);
2493
2494         e100_exec_cb(nic, NULL, e100_configure);
2495
2496         return 0;
2497 }
2498
2499 static u32 e100_get_msglevel(struct net_device *netdev)
2500 {
2501         struct nic *nic = netdev_priv(netdev);
2502         return nic->msg_enable;
2503 }
2504
2505 static void e100_set_msglevel(struct net_device *netdev, u32 value)
2506 {
2507         struct nic *nic = netdev_priv(netdev);
2508         nic->msg_enable = value;
2509 }
2510
2511 static int e100_nway_reset(struct net_device *netdev)
2512 {
2513         struct nic *nic = netdev_priv(netdev);
2514         return mii_nway_restart(&nic->mii);
2515 }
2516
2517 static u32 e100_get_link(struct net_device *netdev)
2518 {
2519         struct nic *nic = netdev_priv(netdev);
2520         return mii_link_ok(&nic->mii);
2521 }
2522
2523 static int e100_get_eeprom_len(struct net_device *netdev)
2524 {
2525         struct nic *nic = netdev_priv(netdev);
2526         return nic->eeprom_wc << 1;
2527 }
2528
2529 #define E100_EEPROM_MAGIC       0x1234
2530 static int e100_get_eeprom(struct net_device *netdev,
2531         struct ethtool_eeprom *eeprom, u8 *bytes)
2532 {
2533         struct nic *nic = netdev_priv(netdev);
2534
2535         eeprom->magic = E100_EEPROM_MAGIC;
2536         memcpy(bytes, &((u8 *)nic->eeprom)[eeprom->offset], eeprom->len);
2537
2538         return 0;
2539 }
2540
2541 static int e100_set_eeprom(struct net_device *netdev,
2542         struct ethtool_eeprom *eeprom, u8 *bytes)
2543 {
2544         struct nic *nic = netdev_priv(netdev);
2545
2546         if (eeprom->magic != E100_EEPROM_MAGIC)
2547                 return -EINVAL;
2548
2549         memcpy(&((u8 *)nic->eeprom)[eeprom->offset], bytes, eeprom->len);
2550
2551         return e100_eeprom_save(nic, eeprom->offset >> 1,
2552                 (eeprom->len >> 1) + 1);
2553 }
2554
2555 static void e100_get_ringparam(struct net_device *netdev,
2556         struct ethtool_ringparam *ring)
2557 {
2558         struct nic *nic = netdev_priv(netdev);
2559         struct param_range *rfds = &nic->params.rfds;
2560         struct param_range *cbs = &nic->params.cbs;
2561
2562         ring->rx_max_pending = rfds->max;
2563         ring->tx_max_pending = cbs->max;
2564         ring->rx_pending = rfds->count;
2565         ring->tx_pending = cbs->count;
2566 }
2567
2568 static int e100_set_ringparam(struct net_device *netdev,
2569         struct ethtool_ringparam *ring)
2570 {
2571         struct nic *nic = netdev_priv(netdev);
2572         struct param_range *rfds = &nic->params.rfds;
2573         struct param_range *cbs = &nic->params.cbs;
2574
2575         if ((ring->rx_mini_pending) || (ring->rx_jumbo_pending))
2576                 return -EINVAL;
2577
2578         if (netif_running(netdev))
2579                 e100_down(nic);
2580         rfds->count = max(ring->rx_pending, rfds->min);
2581         rfds->count = min(rfds->count, rfds->max);
2582         cbs->count = max(ring->tx_pending, cbs->min);
2583         cbs->count = min(cbs->count, cbs->max);
2584         netif_info(nic, drv, nic->netdev, "Ring Param settings: rx: %d, tx %d\n",
2585                    rfds->count, cbs->count);
2586         if (netif_running(netdev))
2587                 e100_up(nic);
2588
2589         return 0;
2590 }
2591
2592 static const char e100_gstrings_test[][ETH_GSTRING_LEN] = {
2593         "Link test     (on/offline)",
2594         "Eeprom test   (on/offline)",
2595         "Self test        (offline)",
2596         "Mac loopback     (offline)",
2597         "Phy loopback     (offline)",
2598 };
2599 #define E100_TEST_LEN   ARRAY_SIZE(e100_gstrings_test)
2600
2601 static void e100_diag_test(struct net_device *netdev,
2602         struct ethtool_test *test, u64 *data)
2603 {
2604         struct ethtool_cmd cmd;
2605         struct nic *nic = netdev_priv(netdev);
2606         int i;
2607
2608         memset(data, 0, E100_TEST_LEN * sizeof(u64));
2609         data[0] = !mii_link_ok(&nic->mii);
2610         data[1] = e100_eeprom_load(nic);
2611         if (test->flags & ETH_TEST_FL_OFFLINE) {
2612
2613                 /* save speed, duplex & autoneg settings */
2614                 mii_ethtool_gset(&nic->mii, &cmd);
2615
2616                 if (netif_running(netdev))
2617                         e100_down(nic);
2618                 data[2] = e100_self_test(nic);
2619                 data[3] = e100_loopback_test(nic, lb_mac);
2620                 data[4] = e100_loopback_test(nic, lb_phy);
2621
2622                 /* restore speed, duplex & autoneg settings */
2623                 mii_ethtool_sset(&nic->mii, &cmd);
2624
2625                 if (netif_running(netdev))
2626                         e100_up(nic);
2627         }
2628         for (i = 0; i < E100_TEST_LEN; i++)
2629                 test->flags |= data[i] ? ETH_TEST_FL_FAILED : 0;
2630
2631         msleep_interruptible(4 * 1000);
2632 }
2633
2634 static int e100_set_phys_id(struct net_device *netdev,
2635                             enum ethtool_phys_id_state state)
2636 {
2637         struct nic *nic = netdev_priv(netdev);
2638         enum led_state {
2639                 led_on     = 0x01,
2640                 led_off    = 0x04,
2641                 led_on_559 = 0x05,
2642                 led_on_557 = 0x07,
2643         };
2644         u16 led_reg = (nic->phy == phy_82552_v) ? E100_82552_LED_OVERRIDE :
2645                 MII_LED_CONTROL;
2646         u16 leds = 0;
2647
2648         switch (state) {
2649         case ETHTOOL_ID_ACTIVE:
2650                 return 2;
2651
2652         case ETHTOOL_ID_ON:
2653                 leds = (nic->phy == phy_82552_v) ? E100_82552_LED_ON :
2654                        (nic->mac < mac_82559_D101M) ? led_on_557 : led_on_559;
2655                 break;
2656
2657         case ETHTOOL_ID_OFF:
2658                 leds = (nic->phy == phy_82552_v) ? E100_82552_LED_OFF : led_off;
2659                 break;
2660
2661         case ETHTOOL_ID_INACTIVE:
2662                 break;
2663         }
2664
2665         mdio_write(netdev, nic->mii.phy_id, led_reg, leds);
2666         return 0;
2667 }
2668
2669 static const char e100_gstrings_stats[][ETH_GSTRING_LEN] = {
2670         "rx_packets", "tx_packets", "rx_bytes", "tx_bytes", "rx_errors",
2671         "tx_errors", "rx_dropped", "tx_dropped", "multicast", "collisions",
2672         "rx_length_errors", "rx_over_errors", "rx_crc_errors",
2673         "rx_frame_errors", "rx_fifo_errors", "rx_missed_errors",
2674         "tx_aborted_errors", "tx_carrier_errors", "tx_fifo_errors",
2675         "tx_heartbeat_errors", "tx_window_errors",
2676         /* device-specific stats */
2677         "tx_deferred", "tx_single_collisions", "tx_multi_collisions",
2678         "tx_flow_control_pause", "rx_flow_control_pause",
2679         "rx_flow_control_unsupported", "tx_tco_packets", "rx_tco_packets",
2680         "rx_short_frame_errors", "rx_over_length_errors",
2681 };
2682 #define E100_NET_STATS_LEN      21
2683 #define E100_STATS_LEN  ARRAY_SIZE(e100_gstrings_stats)
2684
2685 static int e100_get_sset_count(struct net_device *netdev, int sset)
2686 {
2687         switch (sset) {
2688         case ETH_SS_TEST:
2689                 return E100_TEST_LEN;
2690         case ETH_SS_STATS:
2691                 return E100_STATS_LEN;
2692         default:
2693                 return -EOPNOTSUPP;
2694         }
2695 }
2696
2697 static void e100_get_ethtool_stats(struct net_device *netdev,
2698         struct ethtool_stats *stats, u64 *data)
2699 {
2700         struct nic *nic = netdev_priv(netdev);
2701         int i;
2702
2703         for (i = 0; i < E100_NET_STATS_LEN; i++)
2704                 data[i] = ((unsigned long *)&netdev->stats)[i];
2705
2706         data[i++] = nic->tx_deferred;
2707         data[i++] = nic->tx_single_collisions;
2708         data[i++] = nic->tx_multiple_collisions;
2709         data[i++] = nic->tx_fc_pause;
2710         data[i++] = nic->rx_fc_pause;
2711         data[i++] = nic->rx_fc_unsupported;
2712         data[i++] = nic->tx_tco_frames;
2713         data[i++] = nic->rx_tco_frames;
2714         data[i++] = nic->rx_short_frame_errors;
2715         data[i++] = nic->rx_over_length_errors;
2716 }
2717
2718 static void e100_get_strings(struct net_device *netdev, u32 stringset, u8 *data)
2719 {
2720         switch (stringset) {
2721         case ETH_SS_TEST:
2722                 memcpy(data, *e100_gstrings_test, sizeof(e100_gstrings_test));
2723                 break;
2724         case ETH_SS_STATS:
2725                 memcpy(data, *e100_gstrings_stats, sizeof(e100_gstrings_stats));
2726                 break;
2727         }
2728 }
2729
2730 static const struct ethtool_ops e100_ethtool_ops = {
2731         .get_drvinfo            = e100_get_drvinfo,
2732         .get_regs_len           = e100_get_regs_len,
2733         .get_regs               = e100_get_regs,
2734         .get_wol                = e100_get_wol,
2735         .set_wol                = e100_set_wol,
2736         .get_msglevel           = e100_get_msglevel,
2737         .set_msglevel           = e100_set_msglevel,
2738         .nway_reset             = e100_nway_reset,
2739         .get_link               = e100_get_link,
2740         .get_eeprom_len         = e100_get_eeprom_len,
2741         .get_eeprom             = e100_get_eeprom,
2742         .set_eeprom             = e100_set_eeprom,
2743         .get_ringparam          = e100_get_ringparam,
2744         .set_ringparam          = e100_set_ringparam,
2745         .self_test              = e100_diag_test,
2746         .get_strings            = e100_get_strings,
2747         .set_phys_id            = e100_set_phys_id,
2748         .get_ethtool_stats      = e100_get_ethtool_stats,
2749         .get_sset_count         = e100_get_sset_count,
2750         .get_ts_info            = ethtool_op_get_ts_info,
2751         .get_link_ksettings     = e100_get_link_ksettings,
2752         .set_link_ksettings     = e100_set_link_ksettings,
2753 };
2754
2755 static int e100_do_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
2756 {
2757         struct nic *nic = netdev_priv(netdev);
2758
2759         return generic_mii_ioctl(&nic->mii, if_mii(ifr), cmd, NULL);
2760 }
2761
2762 static int e100_alloc(struct nic *nic)
2763 {
2764         nic->mem = dma_alloc_coherent(&nic->pdev->dev, sizeof(struct mem),
2765                                       &nic->dma_addr, GFP_KERNEL);
2766         return nic->mem ? 0 : -ENOMEM;
2767 }
2768
2769 static void e100_free(struct nic *nic)
2770 {
2771         if (nic->mem) {
2772                 dma_free_coherent(&nic->pdev->dev, sizeof(struct mem),
2773                                   nic->mem, nic->dma_addr);
2774                 nic->mem = NULL;
2775         }
2776 }
2777
2778 static int e100_open(struct net_device *netdev)
2779 {
2780         struct nic *nic = netdev_priv(netdev);
2781         int err = 0;
2782
2783         netif_carrier_off(netdev);
2784         if ((err = e100_up(nic)))
2785                 netif_err(nic, ifup, nic->netdev, "Cannot open interface, aborting\n");
2786         return err;
2787 }
2788
2789 static int e100_close(struct net_device *netdev)
2790 {
2791         e100_down(netdev_priv(netdev));
2792         return 0;
2793 }
2794
2795 static int e100_set_features(struct net_device *netdev,
2796                              netdev_features_t features)
2797 {
2798         struct nic *nic = netdev_priv(netdev);
2799         netdev_features_t changed = features ^ netdev->features;
2800
2801         if (!(changed & (NETIF_F_RXFCS | NETIF_F_RXALL)))
2802                 return 0;
2803
2804         netdev->features = features;
2805         e100_exec_cb(nic, NULL, e100_configure);
2806         return 1;
2807 }
2808
2809 static const struct net_device_ops e100_netdev_ops = {
2810         .ndo_open               = e100_open,
2811         .ndo_stop               = e100_close,
2812         .ndo_start_xmit         = e100_xmit_frame,
2813         .ndo_validate_addr      = eth_validate_addr,
2814         .ndo_set_rx_mode        = e100_set_multicast_list,
2815         .ndo_set_mac_address    = e100_set_mac_address,
2816         .ndo_do_ioctl           = e100_do_ioctl,
2817         .ndo_tx_timeout         = e100_tx_timeout,
2818 #ifdef CONFIG_NET_POLL_CONTROLLER
2819         .ndo_poll_controller    = e100_netpoll,
2820 #endif
2821         .ndo_set_features       = e100_set_features,
2822 };
2823
2824 static int e100_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
2825 {
2826         struct net_device *netdev;
2827         struct nic *nic;
2828         int err;
2829
2830         if (!(netdev = alloc_etherdev(sizeof(struct nic))))
2831                 return -ENOMEM;
2832
2833         netdev->hw_features |= NETIF_F_RXFCS;
2834         netdev->priv_flags |= IFF_SUPP_NOFCS;
2835         netdev->hw_features |= NETIF_F_RXALL;
2836
2837         netdev->netdev_ops = &e100_netdev_ops;
2838         netdev->ethtool_ops = &e100_ethtool_ops;
2839         netdev->watchdog_timeo = E100_WATCHDOG_PERIOD;
2840         strncpy(netdev->name, pci_name(pdev), sizeof(netdev->name) - 1);
2841
2842         nic = netdev_priv(netdev);
2843         netif_napi_add(netdev, &nic->napi, e100_poll, E100_NAPI_WEIGHT);
2844         nic->netdev = netdev;
2845         nic->pdev = pdev;
2846         nic->msg_enable = (1 << debug) - 1;
2847         nic->mdio_ctrl = mdio_ctrl_hw;
2848         pci_set_drvdata(pdev, netdev);
2849
2850         if ((err = pci_enable_device(pdev))) {
2851                 netif_err(nic, probe, nic->netdev, "Cannot enable PCI device, aborting\n");
2852                 goto err_out_free_dev;
2853         }
2854
2855         if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
2856                 netif_err(nic, probe, nic->netdev, "Cannot find proper PCI device base address, aborting\n");
2857                 err = -ENODEV;
2858                 goto err_out_disable_pdev;
2859         }
2860
2861         if ((err = pci_request_regions(pdev, DRV_NAME))) {
2862                 netif_err(nic, probe, nic->netdev, "Cannot obtain PCI resources, aborting\n");
2863                 goto err_out_disable_pdev;
2864         }
2865
2866         if ((err = dma_set_mask(&pdev->dev, DMA_BIT_MASK(32)))) {
2867                 netif_err(nic, probe, nic->netdev, "No usable DMA configuration, aborting\n");
2868                 goto err_out_free_res;
2869         }
2870
2871         SET_NETDEV_DEV(netdev, &pdev->dev);
2872
2873         if (use_io)
2874                 netif_info(nic, probe, nic->netdev, "using i/o access mode\n");
2875
2876         nic->csr = pci_iomap(pdev, (use_io ? 1 : 0), sizeof(struct csr));
2877         if (!nic->csr) {
2878                 netif_err(nic, probe, nic->netdev, "Cannot map device registers, aborting\n");
2879                 err = -ENOMEM;
2880                 goto err_out_free_res;
2881         }
2882
2883         if (ent->driver_data)
2884                 nic->flags |= ich;
2885         else
2886                 nic->flags &= ~ich;
2887
2888         e100_get_defaults(nic);
2889
2890         /* D100 MAC doesn't allow rx of vlan packets with normal MTU */
2891         if (nic->mac < mac_82558_D101_A4)
2892                 netdev->features |= NETIF_F_VLAN_CHALLENGED;
2893
2894         /* locks must be initialized before calling hw_reset */
2895         spin_lock_init(&nic->cb_lock);
2896         spin_lock_init(&nic->cmd_lock);
2897         spin_lock_init(&nic->mdio_lock);
2898
2899         /* Reset the device before pci_set_master() in case device is in some
2900          * funky state and has an interrupt pending - hint: we don't have the
2901          * interrupt handler registered yet. */
2902         e100_hw_reset(nic);
2903
2904         pci_set_master(pdev);
2905
2906         timer_setup(&nic->watchdog, e100_watchdog, 0);
2907
2908         INIT_WORK(&nic->tx_timeout_task, e100_tx_timeout_task);
2909
2910         if ((err = e100_alloc(nic))) {
2911                 netif_err(nic, probe, nic->netdev, "Cannot alloc driver memory, aborting\n");
2912                 goto err_out_iounmap;
2913         }
2914
2915         if ((err = e100_eeprom_load(nic)))
2916                 goto err_out_free;
2917
2918         e100_phy_init(nic);
2919
2920         memcpy(netdev->dev_addr, nic->eeprom, ETH_ALEN);
2921         if (!is_valid_ether_addr(netdev->dev_addr)) {
2922                 if (!eeprom_bad_csum_allow) {
2923                         netif_err(nic, probe, nic->netdev, "Invalid MAC address from EEPROM, aborting\n");
2924                         err = -EAGAIN;
2925                         goto err_out_free;
2926                 } else {
2927                         netif_err(nic, probe, nic->netdev, "Invalid MAC address from EEPROM, you MUST configure one.\n");
2928                 }
2929         }
2930
2931         /* Wol magic packet can be enabled from eeprom */
2932         if ((nic->mac >= mac_82558_D101_A4) &&
2933            (le16_to_cpu(nic->eeprom[eeprom_id]) & eeprom_id_wol)) {
2934                 nic->flags |= wol_magic;
2935                 device_set_wakeup_enable(&pdev->dev, true);
2936         }
2937
2938         /* ack any pending wake events, disable PME */
2939         pci_pme_active(pdev, false);
2940
2941         strcpy(netdev->name, "eth%d");
2942         if ((err = register_netdev(netdev))) {
2943                 netif_err(nic, probe, nic->netdev, "Cannot register net device, aborting\n");
2944                 goto err_out_free;
2945         }
2946         nic->cbs_pool = dma_pool_create(netdev->name,
2947                            &nic->pdev->dev,
2948                            nic->params.cbs.max * sizeof(struct cb),
2949                            sizeof(u32),
2950                            0);
2951         if (!nic->cbs_pool) {
2952                 netif_err(nic, probe, nic->netdev, "Cannot create DMA pool, aborting\n");
2953                 err = -ENOMEM;
2954                 goto err_out_pool;
2955         }
2956         netif_info(nic, probe, nic->netdev,
2957                    "addr 0x%llx, irq %d, MAC addr %pM\n",
2958                    (unsigned long long)pci_resource_start(pdev, use_io ? 1 : 0),
2959                    pdev->irq, netdev->dev_addr);
2960
2961         return 0;
2962
2963 err_out_pool:
2964         unregister_netdev(netdev);
2965 err_out_free:
2966         e100_free(nic);
2967 err_out_iounmap:
2968         pci_iounmap(pdev, nic->csr);
2969 err_out_free_res:
2970         pci_release_regions(pdev);
2971 err_out_disable_pdev:
2972         pci_disable_device(pdev);
2973 err_out_free_dev:
2974         free_netdev(netdev);
2975         return err;
2976 }
2977
2978 static void e100_remove(struct pci_dev *pdev)
2979 {
2980         struct net_device *netdev = pci_get_drvdata(pdev);
2981
2982         if (netdev) {
2983                 struct nic *nic = netdev_priv(netdev);
2984                 unregister_netdev(netdev);
2985                 e100_free(nic);
2986                 pci_iounmap(pdev, nic->csr);
2987                 dma_pool_destroy(nic->cbs_pool);
2988                 free_netdev(netdev);
2989                 pci_release_regions(pdev);
2990                 pci_disable_device(pdev);
2991         }
2992 }
2993
2994 #define E100_82552_SMARTSPEED   0x14   /* SmartSpeed Ctrl register */
2995 #define E100_82552_REV_ANEG     0x0200 /* Reverse auto-negotiation */
2996 #define E100_82552_ANEG_NOW     0x0400 /* Auto-negotiate now */
2997 static void __e100_shutdown(struct pci_dev *pdev, bool *enable_wake)
2998 {
2999         struct net_device *netdev = pci_get_drvdata(pdev);
3000         struct nic *nic = netdev_priv(netdev);
3001
3002         netif_device_detach(netdev);
3003
3004         if (netif_running(netdev))
3005                 e100_down(nic);
3006
3007         if ((nic->flags & wol_magic) | e100_asf(nic)) {
3008                 /* enable reverse auto-negotiation */
3009                 if (nic->phy == phy_82552_v) {
3010                         u16 smartspeed = mdio_read(netdev, nic->mii.phy_id,
3011                                                    E100_82552_SMARTSPEED);
3012
3013                         mdio_write(netdev, nic->mii.phy_id,
3014                                    E100_82552_SMARTSPEED, smartspeed |
3015                                    E100_82552_REV_ANEG | E100_82552_ANEG_NOW);
3016                 }
3017                 *enable_wake = true;
3018         } else {
3019                 *enable_wake = false;
3020         }
3021
3022         pci_disable_device(pdev);
3023 }
3024
3025 static int __e100_power_off(struct pci_dev *pdev, bool wake)
3026 {
3027         if (wake)
3028                 return pci_prepare_to_sleep(pdev);
3029
3030         pci_wake_from_d3(pdev, false);
3031         pci_set_power_state(pdev, PCI_D3hot);
3032
3033         return 0;
3034 }
3035
3036 static int __maybe_unused e100_suspend(struct device *dev_d)
3037 {
3038         bool wake;
3039
3040         __e100_shutdown(to_pci_dev(dev_d), &wake);
3041
3042         return 0;
3043 }
3044
3045 static int __maybe_unused e100_resume(struct device *dev_d)
3046 {
3047         struct net_device *netdev = dev_get_drvdata(dev_d);
3048         struct nic *nic = netdev_priv(netdev);
3049         int err;
3050
3051         err = pci_enable_device(to_pci_dev(dev_d));
3052         if (err) {
3053                 netdev_err(netdev, "Resume cannot enable PCI device, aborting\n");
3054                 return err;
3055         }
3056         pci_set_master(to_pci_dev(dev_d));
3057
3058         /* disable reverse auto-negotiation */
3059         if (nic->phy == phy_82552_v) {
3060                 u16 smartspeed = mdio_read(netdev, nic->mii.phy_id,
3061                                            E100_82552_SMARTSPEED);
3062
3063                 mdio_write(netdev, nic->mii.phy_id,
3064                            E100_82552_SMARTSPEED,
3065                            smartspeed & ~(E100_82552_REV_ANEG));
3066         }
3067
3068         if (netif_running(netdev))
3069                 e100_up(nic);
3070
3071         netif_device_attach(netdev);
3072
3073         return 0;
3074 }
3075
3076 static void e100_shutdown(struct pci_dev *pdev)
3077 {
3078         bool wake;
3079         __e100_shutdown(pdev, &wake);
3080         if (system_state == SYSTEM_POWER_OFF)
3081                 __e100_power_off(pdev, wake);
3082 }
3083
3084 /* ------------------ PCI Error Recovery infrastructure  -------------- */
3085 /**
3086  * e100_io_error_detected - called when PCI error is detected.
3087  * @pdev: Pointer to PCI device
3088  * @state: The current pci connection state
3089  */
3090 static pci_ers_result_t e100_io_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
3091 {
3092         struct net_device *netdev = pci_get_drvdata(pdev);
3093         struct nic *nic = netdev_priv(netdev);
3094
3095         netif_device_detach(netdev);
3096
3097         if (state == pci_channel_io_perm_failure)
3098                 return PCI_ERS_RESULT_DISCONNECT;
3099
3100         if (netif_running(netdev))
3101                 e100_down(nic);
3102         pci_disable_device(pdev);
3103
3104         /* Request a slot reset. */
3105         return PCI_ERS_RESULT_NEED_RESET;
3106 }
3107
3108 /**
3109  * e100_io_slot_reset - called after the pci bus has been reset.
3110  * @pdev: Pointer to PCI device
3111  *
3112  * Restart the card from scratch.
3113  */
3114 static pci_ers_result_t e100_io_slot_reset(struct pci_dev *pdev)
3115 {
3116         struct net_device *netdev = pci_get_drvdata(pdev);
3117         struct nic *nic = netdev_priv(netdev);
3118
3119         if (pci_enable_device(pdev)) {
3120                 pr_err("Cannot re-enable PCI device after reset\n");
3121                 return PCI_ERS_RESULT_DISCONNECT;
3122         }
3123         pci_set_master(pdev);
3124
3125         /* Only one device per card can do a reset */
3126         if (0 != PCI_FUNC(pdev->devfn))
3127                 return PCI_ERS_RESULT_RECOVERED;
3128         e100_hw_reset(nic);
3129         e100_phy_init(nic);
3130
3131         return PCI_ERS_RESULT_RECOVERED;
3132 }
3133
3134 /**
3135  * e100_io_resume - resume normal operations
3136  * @pdev: Pointer to PCI device
3137  *
3138  * Resume normal operations after an error recovery
3139  * sequence has been completed.
3140  */
3141 static void e100_io_resume(struct pci_dev *pdev)
3142 {
3143         struct net_device *netdev = pci_get_drvdata(pdev);
3144         struct nic *nic = netdev_priv(netdev);
3145
3146         /* ack any pending wake events, disable PME */
3147         pci_enable_wake(pdev, PCI_D0, 0);
3148
3149         netif_device_attach(netdev);
3150         if (netif_running(netdev)) {
3151                 e100_open(netdev);
3152                 mod_timer(&nic->watchdog, jiffies);
3153         }
3154 }
3155
3156 static const struct pci_error_handlers e100_err_handler = {
3157         .error_detected = e100_io_error_detected,
3158         .slot_reset = e100_io_slot_reset,
3159         .resume = e100_io_resume,
3160 };
3161
3162 static SIMPLE_DEV_PM_OPS(e100_pm_ops, e100_suspend, e100_resume);
3163
3164 static struct pci_driver e100_driver = {
3165         .name =         DRV_NAME,
3166         .id_table =     e100_id_table,
3167         .probe =        e100_probe,
3168         .remove =       e100_remove,
3169
3170         /* Power Management hooks */
3171         .driver.pm =    &e100_pm_ops,
3172
3173         .shutdown =     e100_shutdown,
3174         .err_handler = &e100_err_handler,
3175 };
3176
3177 static int __init e100_init_module(void)
3178 {
3179         if (((1 << debug) - 1) & NETIF_MSG_DRV) {
3180                 pr_info("%s\n", DRV_DESCRIPTION);
3181                 pr_info("%s\n", DRV_COPYRIGHT);
3182         }
3183         return pci_register_driver(&e100_driver);
3184 }
3185
3186 static void __exit e100_cleanup_module(void)
3187 {
3188         pci_unregister_driver(&e100_driver);
3189 }
3190
3191 module_init(e100_init_module);
3192 module_exit(e100_cleanup_module);