~ubuntu-branches/ubuntu/raring/ipxe/raring

« back to all changes in this revision

Viewing changes to src/drivers/net/e1000/e1000_main.c

  • Committer: Package Import Robot
  • Author(s): James Page
  • Date: 2012-11-14 15:47:31 UTC
  • mfrom: (1.1.3)
  • Revision ID: package-import@ubuntu.com-20121114154731-jhuy5d1h2jw75qe9
Tags: 1.0.0+git-4.d6b0b76-0ubuntu1
* New upstream snapshot:
  - d/p/iscsi*.patch: Dropped - included in snapshot.
  - Refreshed all other patches.
* d/p/enable-https.patch: Enable HTTPS support (LP: #1025239).

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*******************************************************************************
2
 
 
3
 
  Intel PRO/1000 Linux driver
4
 
  Copyright(c) 1999 - 2008 Intel Corporation.
5
 
 
6
 
  Portions Copyright(c) 2010 Marty Connor <mdc@etherboot.org>
7
 
  Portions Copyright(c) 2010 Entity Cyber, Inc.
8
 
 
9
 
  This program is free software; you can redistribute it and/or modify it
10
 
  under the terms and conditions of the GNU General Public License,
11
 
  version 2, as published by the Free Software Foundation.
12
 
 
13
 
  This program is distributed in the hope it will be useful, but WITHOUT
14
 
  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15
 
  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
16
 
  more details.
17
 
 
18
 
  You should have received a copy of the GNU General Public License along with
19
 
  this program; if not, write to the Free Software Foundation, Inc.,
20
 
  51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
21
 
 
22
 
  The full GNU General Public License is included in this distribution in
23
 
  the file called "COPYING".
24
 
 
25
 
  Contact Information:
26
 
  Linux NICS <linux.nics@intel.com>
27
 
  e1000-devel Mailing List <e1000-devel@lists.sourceforge.net>
28
 
  Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
29
 
 
30
 
*******************************************************************************/
31
 
 
32
 
FILE_LICENCE ( GPL2_ONLY );
33
 
 
34
 
#include "e1000.h"
35
 
 
36
 
/**
37
 
 * e1000_irq_disable - Disable interrupt generation
38
 
 *
39
 
 * @adapter: board private structure
40
 
 **/
41
 
static void e1000_irq_disable ( struct e1000_adapter *adapter )
42
 
{
43
 
        E1000_WRITE_REG ( &adapter->hw, E1000_IMC, ~0 );
44
 
        E1000_WRITE_FLUSH ( &adapter->hw );
45
 
}
46
 
 
47
 
/**
48
 
 * e1000_irq_enable - Enable interrupt generation
49
 
 *
50
 
 * @adapter: board private structure
51
 
 **/
52
 
static void e1000_irq_enable ( struct e1000_adapter *adapter )
53
 
{
54
 
        E1000_WRITE_REG(&adapter->hw, E1000_IMS, IMS_ENABLE_MASK);
55
 
        E1000_WRITE_FLUSH(&adapter->hw);
56
 
}
57
 
 
58
 
/**
59
 
 * e1000_sw_init - Initialize general software structures (struct e1000_adapter)
60
 
 * @adapter: board private structure to initialize
61
 
 *
62
 
 * e1000_sw_init initializes the Adapter private data structure.
63
 
 * Fields are initialized based on PCI device information and
64
 
 * OS network device settings (MTU size).
65
 
 **/
66
 
static int e1000_sw_init(struct e1000_adapter *adapter)
67
 
{
68
 
        struct e1000_hw *hw = &adapter->hw;
69
 
        struct pci_device  *pdev = adapter->pdev;
70
 
 
71
 
        /* PCI config space info */
72
 
 
73
 
        hw->vendor_id = pdev->vendor;
74
 
        hw->device_id = pdev->device;
75
 
 
76
 
        pci_read_config_word(pdev, PCI_SUBSYSTEM_VENDOR_ID, &hw->subsystem_vendor_id);
77
 
        pci_read_config_word(pdev, PCI_SUBSYSTEM_ID, &hw->subsystem_device_id);
78
 
 
79
 
        pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
80
 
 
81
 
        pci_read_config_word(pdev, PCI_COMMAND, &hw->bus.pci_cmd_word);
82
 
 
83
 
        adapter->rx_buffer_len = MAXIMUM_ETHERNET_VLAN_SIZE;
84
 
        adapter->max_frame_size = MAXIMUM_ETHERNET_VLAN_SIZE +
85
 
                                  ETH_HLEN + ETH_FCS_LEN;
86
 
        adapter->min_frame_size = ETH_ZLEN + ETH_FCS_LEN;
87
 
 
88
 
        hw->fc.requested_mode = e1000_fc_none;
89
 
 
90
 
        /* Initialize the hardware-specific values */
91
 
        if (e1000_setup_init_funcs(hw, false)) {
92
 
                DBG ("Hardware Initialization Failure\n");
93
 
                return -EIO;
94
 
        }
95
 
 
96
 
        /* Explicitly disable IRQ since the NIC can be in any state. */
97
 
        e1000_irq_disable ( adapter );
98
 
 
99
 
        return 0;
100
 
}
101
 
 
102
 
int32_t e1000_read_pcie_cap_reg(struct e1000_hw *hw, uint32_t reg, uint16_t *value)
103
 
{
104
 
    struct e1000_adapter *adapter = hw->back;
105
 
    uint16_t cap_offset;
106
 
 
107
 
#define  PCI_CAP_ID_EXP        0x10    /* PCI Express */
108
 
    cap_offset = pci_find_capability(adapter->pdev, PCI_CAP_ID_EXP);
109
 
    if (!cap_offset)
110
 
        return -E1000_ERR_CONFIG;
111
 
 
112
 
    pci_read_config_word(adapter->pdev, cap_offset + reg, value);
113
 
 
114
 
    return 0;
115
 
}
116
 
 
117
 
void e1000_pci_clear_mwi ( struct e1000_hw *hw )
118
 
{
119
 
        struct e1000_adapter *adapter = hw->back;
120
 
 
121
 
        pci_write_config_word ( adapter->pdev, PCI_COMMAND,
122
 
                                hw->bus.pci_cmd_word & ~PCI_COMMAND_INVALIDATE );
123
 
}
124
 
 
125
 
void e1000_pci_set_mwi ( struct e1000_hw *hw )
126
 
{
127
 
        struct e1000_adapter *adapter = hw->back;
128
 
 
129
 
        pci_write_config_word ( adapter->pdev, PCI_COMMAND,
130
 
                                hw->bus.pci_cmd_word );
131
 
}
132
 
 
133
 
void e1000_read_pci_cfg ( struct e1000_hw *hw, uint32_t reg, uint16_t *value )
134
 
{
135
 
        struct e1000_adapter *adapter = hw->back;
136
 
 
137
 
        pci_read_config_word ( adapter->pdev, reg, value );
138
 
}
139
 
 
140
 
void e1000_write_pci_cfg ( struct e1000_hw *hw, uint32_t reg, uint16_t *value )
141
 
{
142
 
        struct e1000_adapter *adapter = hw->back;
143
 
 
144
 
        pci_write_config_word ( adapter->pdev, reg, *value );
145
 
}
146
 
 
147
 
/**
148
 
 * e1000_init_manageability - disable interception of ARP packets
149
 
 *
150
 
 * @v adapter   e1000 private structure
151
 
 **/
152
 
static void e1000_init_manageability ( struct e1000_adapter *adapter )
153
 
{
154
 
        if (adapter->en_mng_pt) {
155
 
                u32 manc = E1000_READ_REG(&adapter->hw, E1000_MANC);
156
 
 
157
 
                /* disable hardware interception of ARP */
158
 
                manc &= ~(E1000_MANC_ARP_EN);
159
 
 
160
 
                E1000_WRITE_REG(&adapter->hw, E1000_MANC, manc);
161
 
        }
162
 
}
163
 
 
164
 
/**
165
 
 * e1000_setup_tx_resources - allocate Tx resources (Descriptors)
166
 
 *
167
 
 * @v adapter   e1000 private structure
168
 
 *
169
 
 * @ret rc       Returns 0 on success, negative on failure
170
 
 **/
171
 
static int e1000_setup_tx_resources ( struct e1000_adapter *adapter )
172
 
{
173
 
        DBG ( "e1000_setup_tx_resources\n" );
174
 
 
175
 
        /* Allocate transmit descriptor ring memory.
176
 
           It must not cross a 64K boundary because of hardware errata #23
177
 
           so we use malloc_dma() requesting a 128 byte block that is
178
 
           128 byte aligned. This should guarantee that the memory
179
 
           allocated will not cross a 64K boundary, because 128 is an
180
 
           even multiple of 65536 ( 65536 / 128 == 512 ), so all possible
181
 
           allocations of 128 bytes on a 128 byte boundary will not
182
 
           cross 64K bytes.
183
 
         */
184
 
 
185
 
        adapter->tx_base =
186
 
                malloc_dma ( adapter->tx_ring_size, adapter->tx_ring_size );
187
 
 
188
 
        if ( ! adapter->tx_base ) {
189
 
                return -ENOMEM;
190
 
        }
191
 
 
192
 
        memset ( adapter->tx_base, 0, adapter->tx_ring_size );
193
 
 
194
 
        DBG ( "adapter->tx_base = %#08lx\n", virt_to_bus ( adapter->tx_base ) );
195
 
 
196
 
        return 0;
197
 
}
198
 
 
199
 
/**
200
 
 * e1000_process_tx_packets - process transmitted packets
201
 
 *
202
 
 * @v netdev    network interface device structure
203
 
 **/
204
 
static void e1000_process_tx_packets ( struct net_device *netdev )
205
 
{
206
 
        struct e1000_adapter *adapter = netdev_priv ( netdev );
207
 
        uint32_t i;
208
 
        uint32_t tx_status;
209
 
        struct e1000_tx_desc *tx_curr_desc;
210
 
 
211
 
        /* Check status of transmitted packets
212
 
         */
213
 
        while ( ( i = adapter->tx_head ) != adapter->tx_tail ) {
214
 
 
215
 
                tx_curr_desc = ( void * )  ( adapter->tx_base ) +
216
 
                                           ( i * sizeof ( *adapter->tx_base ) );
217
 
 
218
 
                tx_status = tx_curr_desc->upper.data;
219
 
 
220
 
                /* if the packet at tx_head is not owned by hardware it is for us */
221
 
                if ( ! ( tx_status & E1000_TXD_STAT_DD ) )
222
 
                        break;
223
 
 
224
 
                DBG ( "Sent packet. tx_head: %d tx_tail: %d tx_status: %#08x\n",
225
 
                      adapter->tx_head, adapter->tx_tail, tx_status );
226
 
 
227
 
                if ( tx_status & ( E1000_TXD_STAT_EC | E1000_TXD_STAT_LC |
228
 
                                   E1000_TXD_STAT_TU ) ) {
229
 
                        netdev_tx_complete_err ( netdev, adapter->tx_iobuf[i], -EINVAL );
230
 
                        DBG ( "Error transmitting packet, tx_status: %#08x\n",
231
 
                              tx_status );
232
 
                } else {
233
 
                        netdev_tx_complete ( netdev, adapter->tx_iobuf[i] );
234
 
                        DBG ( "Success transmitting packet, tx_status: %#08x\n",
235
 
                              tx_status );
236
 
                }
237
 
 
238
 
                /* Decrement count of used descriptors, clear this descriptor
239
 
                 */
240
 
                adapter->tx_fill_ctr--;
241
 
                memset ( tx_curr_desc, 0, sizeof ( *tx_curr_desc ) );
242
 
 
243
 
                adapter->tx_head = ( adapter->tx_head + 1 ) % NUM_TX_DESC;
244
 
        }
245
 
}
246
 
 
247
 
static void e1000_free_tx_resources ( struct e1000_adapter *adapter )
248
 
{
249
 
        DBG ( "e1000_free_tx_resources\n" );
250
 
 
251
 
        free_dma ( adapter->tx_base, adapter->tx_ring_size );
252
 
}
253
 
 
254
 
/**
255
 
 * e1000_configure_tx - Configure 8254x Transmit Unit after Reset
256
 
 * @adapter: board private structure
257
 
 *
258
 
 * Configure the Tx unit of the MAC after a reset.
259
 
 **/
260
 
static void e1000_configure_tx ( struct e1000_adapter *adapter )
261
 
{
262
 
        struct e1000_hw *hw = &adapter->hw;
263
 
        uint32_t tctl;
264
 
 
265
 
        DBG ( "e1000_configure_tx\n" );
266
 
 
267
 
        E1000_WRITE_REG ( hw, E1000_TDBAH(0), 0 );
268
 
        E1000_WRITE_REG ( hw, E1000_TDBAL(0), virt_to_bus ( adapter->tx_base ) );
269
 
        E1000_WRITE_REG ( hw, E1000_TDLEN(0), adapter->tx_ring_size );
270
 
 
271
 
        DBG ( "E1000_TDBAL(0): %#08x\n",  E1000_READ_REG ( hw, E1000_TDBAL(0) ) );
272
 
        DBG ( "E1000_TDLEN(0): %d\n",     E1000_READ_REG ( hw, E1000_TDLEN(0) ) );
273
 
 
274
 
        /* Setup the HW Tx Head and Tail descriptor pointers */
275
 
        E1000_WRITE_REG ( hw, E1000_TDH(0), 0 );
276
 
        E1000_WRITE_REG ( hw, E1000_TDT(0), 0 );
277
 
 
278
 
        adapter->tx_head = 0;
279
 
        adapter->tx_tail = 0;
280
 
        adapter->tx_fill_ctr = 0;
281
 
 
282
 
        /* Setup Transmit Descriptor Settings for eop descriptor */
283
 
        tctl = E1000_TCTL_PSP | E1000_TCTL_EN |
284
 
                (E1000_COLLISION_THRESHOLD << E1000_CT_SHIFT) |
285
 
                (E1000_COLLISION_DISTANCE  << E1000_COLD_SHIFT);
286
 
 
287
 
        e1000_config_collision_dist ( hw );
288
 
 
289
 
        E1000_WRITE_REG ( hw, E1000_TCTL, tctl );
290
 
        E1000_WRITE_FLUSH ( hw );
291
 
}
292
 
 
293
 
static void e1000_free_rx_resources ( struct e1000_adapter *adapter )
294
 
{
295
 
        int i;
296
 
 
297
 
        DBG ( "e1000_free_rx_resources\n" );
298
 
 
299
 
        free_dma ( adapter->rx_base, adapter->rx_ring_size );
300
 
 
301
 
        for ( i = 0; i < NUM_RX_DESC; i++ ) {
302
 
                free_iob ( adapter->rx_iobuf[i] );
303
 
        }
304
 
}
305
 
 
306
 
/**
307
 
 * e1000_refill_rx_ring - allocate Rx io_buffers
308
 
 *
309
 
 * @v adapter   e1000 private structure
310
 
 *
311
 
 * @ret rc       Returns 0 on success, negative on failure
312
 
 **/
313
 
static int e1000_refill_rx_ring ( struct e1000_adapter *adapter )
314
 
{
315
 
        int i, rx_curr;
316
 
        int rc = 0;
317
 
        struct e1000_rx_desc *rx_curr_desc;
318
 
        struct e1000_hw *hw = &adapter->hw;
319
 
        struct io_buffer *iob;
320
 
 
321
 
        DBG ("e1000_refill_rx_ring\n");
322
 
 
323
 
        for ( i = 0; i < NUM_RX_DESC; i++ ) {
324
 
                rx_curr = ( ( adapter->rx_curr + i ) % NUM_RX_DESC );
325
 
                rx_curr_desc = adapter->rx_base + rx_curr;
326
 
 
327
 
                if ( rx_curr_desc->status & E1000_RXD_STAT_DD )
328
 
                        continue;
329
 
 
330
 
                if ( adapter->rx_iobuf[rx_curr] != NULL )
331
 
                        continue;
332
 
 
333
 
                DBG2 ( "Refilling rx desc %d\n", rx_curr );
334
 
 
335
 
                iob = alloc_iob ( MAXIMUM_ETHERNET_VLAN_SIZE );
336
 
                adapter->rx_iobuf[rx_curr] = iob;
337
 
 
338
 
                if ( ! iob ) {
339
 
                        DBG ( "alloc_iob failed\n" );
340
 
                        rc = -ENOMEM;
341
 
                        break;
342
 
                } else {
343
 
                        rx_curr_desc->buffer_addr = virt_to_bus ( iob->data );
344
 
 
345
 
                        E1000_WRITE_REG ( hw, E1000_RDT(0), rx_curr );
346
 
                }
347
 
        }
348
 
        return rc;
349
 
}
350
 
 
351
 
/**
352
 
 * e1000_setup_rx_resources - allocate Rx resources (Descriptors)
353
 
 *
354
 
 * @v adapter   e1000 private structure
355
 
 *
356
 
 * @ret rc       Returns 0 on success, negative on failure
357
 
 **/
358
 
static int e1000_setup_rx_resources ( struct e1000_adapter *adapter )
359
 
{
360
 
        int i, rc = 0;
361
 
 
362
 
        DBG ( "e1000_setup_rx_resources\n" );
363
 
 
364
 
        /* Allocate receive descriptor ring memory.
365
 
           It must not cross a 64K boundary because of hardware errata
366
 
         */
367
 
 
368
 
        adapter->rx_base =
369
 
                malloc_dma ( adapter->rx_ring_size, adapter->rx_ring_size );
370
 
 
371
 
        if ( ! adapter->rx_base ) {
372
 
                return -ENOMEM;
373
 
        }
374
 
        memset ( adapter->rx_base, 0, adapter->rx_ring_size );
375
 
 
376
 
        for ( i = 0; i < NUM_RX_DESC; i++ ) {
377
 
                /* let e1000_refill_rx_ring() io_buffer allocations */
378
 
                adapter->rx_iobuf[i] = NULL;
379
 
        }
380
 
 
381
 
        /* allocate io_buffers */
382
 
        rc = e1000_refill_rx_ring ( adapter );
383
 
        if ( rc < 0 )
384
 
                e1000_free_rx_resources ( adapter );
385
 
 
386
 
        return rc;
387
 
}
388
 
 
389
 
/**
390
 
 * e1000_configure_rx - Configure 8254x Receive Unit after Reset
391
 
 * @adapter: board private structure
392
 
 *
393
 
 * Configure the Rx unit of the MAC after a reset.
394
 
 **/
395
 
static void e1000_configure_rx ( struct e1000_adapter *adapter )
396
 
{
397
 
        struct e1000_hw *hw = &adapter->hw;
398
 
        uint32_t rctl;
399
 
 
400
 
        DBG ( "e1000_configure_rx\n" );
401
 
 
402
 
        /* disable receives while setting up the descriptors */
403
 
        rctl = E1000_READ_REG ( hw, E1000_RCTL );
404
 
        E1000_WRITE_REG ( hw, E1000_RCTL, rctl & ~E1000_RCTL_EN );
405
 
        E1000_WRITE_FLUSH ( hw );
406
 
        mdelay(10);
407
 
 
408
 
        adapter->rx_curr = 0;
409
 
 
410
 
        /* Setup the HW Rx Head and Tail Descriptor Pointers and
411
 
         * the Base and Length of the Rx Descriptor Ring */
412
 
 
413
 
        E1000_WRITE_REG ( hw, E1000_RDBAL(0), virt_to_bus ( adapter->rx_base ) );
414
 
        E1000_WRITE_REG ( hw, E1000_RDBAH(0), 0 );
415
 
        E1000_WRITE_REG ( hw, E1000_RDLEN(0), adapter->rx_ring_size );
416
 
 
417
 
        E1000_WRITE_REG ( hw, E1000_RDH(0), 0 );
418
 
        E1000_WRITE_REG ( hw, E1000_RDT(0), NUM_RX_DESC - 1 );
419
 
 
420
 
        /* Enable Receives */
421
 
        rctl |=  E1000_RCTL_EN | E1000_RCTL_BAM | E1000_RCTL_SZ_2048 |
422
 
                 E1000_RCTL_MPE | E1000_RCTL_SECRC;
423
 
        E1000_WRITE_REG ( hw, E1000_RCTL, rctl );
424
 
        E1000_WRITE_FLUSH ( hw );
425
 
 
426
 
        DBG ( "E1000_RDBAL(0): %#08x\n",  E1000_READ_REG ( hw, E1000_RDBAL(0) ) );
427
 
        DBG ( "E1000_RDLEN(0): %d\n",     E1000_READ_REG ( hw, E1000_RDLEN(0) ) );
428
 
        DBG ( "E1000_RCTL:  %#08x\n",  E1000_READ_REG ( hw, E1000_RCTL ) );
429
 
}
430
 
 
431
 
/**
432
 
 * e1000_process_rx_packets - process received packets
433
 
 *
434
 
 * @v netdev    network interface device structure
435
 
 **/
436
 
static void e1000_process_rx_packets ( struct net_device *netdev )
437
 
{
438
 
        struct e1000_adapter *adapter = netdev_priv ( netdev );
439
 
        uint32_t i;
440
 
        uint32_t rx_status;
441
 
        uint32_t rx_len;
442
 
        uint32_t rx_err;
443
 
        struct e1000_rx_desc *rx_curr_desc;
444
 
 
445
 
        /* Process received packets
446
 
         */
447
 
        while ( 1 ) {
448
 
 
449
 
                i = adapter->rx_curr;
450
 
 
451
 
                rx_curr_desc = ( void * )  ( adapter->rx_base ) +
452
 
                                  ( i * sizeof ( *adapter->rx_base ) );
453
 
                rx_status = rx_curr_desc->status;
454
 
 
455
 
                DBG2 ( "Before DD Check RX_status: %#08x\n", rx_status );
456
 
 
457
 
                if ( ! ( rx_status & E1000_RXD_STAT_DD ) )
458
 
                        break;
459
 
 
460
 
                if ( adapter->rx_iobuf[i] == NULL )
461
 
                        break;
462
 
 
463
 
                DBG ( "E1000_RCTL = %#08x\n", E1000_READ_REG ( &adapter->hw, E1000_RCTL ) );
464
 
 
465
 
                rx_len = rx_curr_desc->length;
466
 
 
467
 
                DBG ( "Received packet, rx_curr: %d  rx_status: %#08x  rx_len: %d\n",
468
 
                      i, rx_status, rx_len );
469
 
 
470
 
                rx_err = rx_curr_desc->errors;
471
 
 
472
 
                iob_put ( adapter->rx_iobuf[i], rx_len );
473
 
 
474
 
                if ( rx_err & E1000_RXD_ERR_FRAME_ERR_MASK ) {
475
 
 
476
 
                        netdev_rx_err ( netdev, adapter->rx_iobuf[i], -EINVAL );
477
 
                        DBG ( "e1000_poll: Corrupted packet received!"
478
 
                              " rx_err: %#08x\n", rx_err );
479
 
                } else {
480
 
                        /* Add this packet to the receive queue. */
481
 
                        netdev_rx ( netdev, adapter->rx_iobuf[i] );
482
 
                }
483
 
                adapter->rx_iobuf[i] = NULL;
484
 
 
485
 
                memset ( rx_curr_desc, 0, sizeof ( *rx_curr_desc ) );
486
 
 
487
 
                adapter->rx_curr = ( adapter->rx_curr + 1 ) % NUM_RX_DESC;
488
 
        }
489
 
}
490
 
 
491
 
/**
492
 
 * e1000_reset - Put e1000 NIC in known initial state
493
 
 *
494
 
 * @v adapter   e1000 private structure
495
 
 **/
496
 
void e1000_reset ( struct e1000_adapter *adapter )
497
 
{
498
 
        struct e1000_mac_info *mac = &adapter->hw.mac;
499
 
        u32 pba = 0;
500
 
 
501
 
        DBG ( "e1000_reset\n" );
502
 
 
503
 
        switch (mac->type) {
504
 
        case e1000_82542:
505
 
        case e1000_82543:
506
 
        case e1000_82544:
507
 
        case e1000_82540:
508
 
        case e1000_82541:
509
 
        case e1000_82541_rev_2:
510
 
                pba = E1000_PBA_48K;
511
 
                break;
512
 
        case e1000_82545:
513
 
        case e1000_82545_rev_3:
514
 
        case e1000_82546:
515
 
        case e1000_82546_rev_3:
516
 
                pba = E1000_PBA_48K;
517
 
                break;
518
 
        case e1000_82547:
519
 
        case e1000_82547_rev_2:
520
 
                pba = E1000_PBA_30K;
521
 
                break;
522
 
        case e1000_undefined:
523
 
        case e1000_num_macs:
524
 
                break;
525
 
        }
526
 
 
527
 
        E1000_WRITE_REG ( &adapter->hw, E1000_PBA, pba );
528
 
 
529
 
        /* Allow time for pending master requests to run */
530
 
        e1000_reset_hw ( &adapter->hw );
531
 
 
532
 
        if ( mac->type >= e1000_82544 )
533
 
                E1000_WRITE_REG ( &adapter->hw, E1000_WUC, 0 );
534
 
 
535
 
        if ( e1000_init_hw ( &adapter->hw ) )
536
 
                DBG ( "Hardware Error\n" );
537
 
 
538
 
        e1000_reset_adaptive ( &adapter->hw );
539
 
        e1000_get_phy_info ( &adapter->hw );
540
 
 
541
 
        e1000_init_manageability ( adapter );
542
 
}
543
 
 
544
 
/** Functions that implement the iPXE driver API **/
545
 
 
546
 
/**
547
 
 * e1000_close - Disables a network interface
548
 
 *
549
 
 * @v netdev    network interface device structure
550
 
 *
551
 
 **/
552
 
static void e1000_close ( struct net_device *netdev )
553
 
{
554
 
        struct e1000_adapter *adapter = netdev_priv ( netdev );
555
 
        struct e1000_hw *hw = &adapter->hw;
556
 
        uint32_t rctl;
557
 
 
558
 
        DBG ( "e1000_close\n" );
559
 
 
560
 
        /* Disable and acknowledge interrupts */
561
 
        e1000_irq_disable ( adapter );
562
 
        E1000_READ_REG ( hw, E1000_ICR );
563
 
 
564
 
        /* disable receives */
565
 
        rctl = E1000_READ_REG ( hw, E1000_RCTL );
566
 
        E1000_WRITE_REG ( hw, E1000_RCTL, rctl & ~E1000_RCTL_EN );
567
 
        E1000_WRITE_FLUSH ( hw );
568
 
 
569
 
        e1000_reset_hw ( hw );
570
 
 
571
 
        e1000_free_tx_resources ( adapter );
572
 
        e1000_free_rx_resources ( adapter );
573
 
}
574
 
 
575
 
/**
576
 
 * e1000_transmit - Transmit a packet
577
 
 *
578
 
 * @v netdev    Network device
579
 
 * @v iobuf     I/O buffer
580
 
 *
581
 
 * @ret rc       Returns 0 on success, negative on failure
582
 
 */
583
 
static int e1000_transmit ( struct net_device *netdev, struct io_buffer *iobuf )
584
 
{
585
 
        struct e1000_adapter *adapter = netdev_priv( netdev );
586
 
        struct e1000_hw *hw = &adapter->hw;
587
 
        uint32_t tx_curr = adapter->tx_tail;
588
 
        struct e1000_tx_desc *tx_curr_desc;
589
 
 
590
 
        DBG ("e1000_transmit\n");
591
 
 
592
 
        if ( adapter->tx_fill_ctr == NUM_TX_DESC ) {
593
 
                DBG ("TX overflow\n");
594
 
                return -ENOBUFS;
595
 
        }
596
 
 
597
 
        /* Save pointer to iobuf we have been given to transmit,
598
 
           netdev_tx_complete() will need it later
599
 
         */
600
 
        adapter->tx_iobuf[tx_curr] = iobuf;
601
 
 
602
 
        tx_curr_desc = ( void * ) ( adapter->tx_base ) +
603
 
                       ( tx_curr * sizeof ( *adapter->tx_base ) );
604
 
 
605
 
        DBG ( "tx_curr_desc = %#08lx\n", virt_to_bus ( tx_curr_desc ) );
606
 
        DBG ( "tx_curr_desc + 16 = %#08lx\n", virt_to_bus ( tx_curr_desc ) + 16 );
607
 
        DBG ( "iobuf->data = %#08lx\n", virt_to_bus ( iobuf->data ) );
608
 
 
609
 
        /* Add the packet to TX ring
610
 
         */
611
 
        tx_curr_desc->buffer_addr =
612
 
                virt_to_bus ( iobuf->data );
613
 
        tx_curr_desc->lower.data =
614
 
                E1000_TXD_CMD_RS | E1000_TXD_CMD_EOP |
615
 
                E1000_TXD_CMD_IFCS | iob_len ( iobuf );
616
 
        tx_curr_desc->upper.data = 0;
617
 
 
618
 
        DBG ( "TX fill: %d tx_curr: %d addr: %#08lx len: %zd\n", adapter->tx_fill_ctr,
619
 
              tx_curr, virt_to_bus ( iobuf->data ), iob_len ( iobuf ) );
620
 
 
621
 
        /* Point to next free descriptor */
622
 
        adapter->tx_tail = ( adapter->tx_tail + 1 ) % NUM_TX_DESC;
623
 
        adapter->tx_fill_ctr++;
624
 
 
625
 
        /* Write new tail to NIC, making packet available for transmit
626
 
         */
627
 
        wmb();
628
 
        E1000_WRITE_REG ( hw, E1000_TDT(0), adapter->tx_tail );
629
 
 
630
 
        return 0;
631
 
}
632
 
 
633
 
/**
634
 
 * e1000_poll - Poll for received packets
635
 
 *
636
 
 * @v netdev    Network device
637
 
 */
638
 
static void e1000_poll ( struct net_device *netdev )
639
 
{
640
 
        struct e1000_adapter *adapter = netdev_priv( netdev );
641
 
        struct e1000_hw *hw = &adapter->hw;
642
 
 
643
 
        uint32_t icr;
644
 
 
645
 
        DBGP ( "e1000_poll\n" );
646
 
 
647
 
        /* Acknowledge interrupts */
648
 
        icr = E1000_READ_REG ( hw, E1000_ICR );
649
 
        if ( ! icr )
650
 
                return;
651
 
 
652
 
        DBG ( "e1000_poll: intr_status = %#08x\n", icr );
653
 
 
654
 
        e1000_process_tx_packets ( netdev );
655
 
 
656
 
        e1000_process_rx_packets ( netdev );
657
 
 
658
 
        e1000_refill_rx_ring(adapter);
659
 
}
660
 
 
661
 
/**
662
 
 * e1000_irq - enable or Disable interrupts
663
 
 *
664
 
 * @v adapter   e1000 adapter
665
 
 * @v action    requested interrupt action
666
 
 **/
667
 
static void e1000_irq ( struct net_device *netdev, int enable )
668
 
{
669
 
        struct e1000_adapter *adapter = netdev_priv ( netdev );
670
 
 
671
 
        DBG ( "e1000_irq\n" );
672
 
 
673
 
        if ( enable ) {
674
 
                e1000_irq_enable ( adapter );
675
 
        } else {
676
 
                e1000_irq_disable ( adapter );
677
 
        }
678
 
}
679
 
 
680
 
static struct net_device_operations e1000_operations;
681
 
 
682
 
/**
683
 
 * e1000_probe - Initial configuration of e1000 NIC
684
 
 *
685
 
 * @v pci       PCI device
686
 
 * @v id        PCI IDs
687
 
 *
688
 
 * @ret rc      Return status code
689
 
 **/
690
 
int e1000_probe ( struct pci_device *pdev )
691
 
{
692
 
        int i, err;
693
 
        struct net_device *netdev;
694
 
        struct e1000_adapter *adapter;
695
 
        unsigned long mmio_start, mmio_len;
696
 
 
697
 
        DBG ( "e1000_probe\n" );
698
 
 
699
 
        err = -ENOMEM;
700
 
 
701
 
        /* Allocate net device ( also allocates memory for netdev->priv
702
 
           and makes netdev-priv point to it ) */
703
 
        netdev = alloc_etherdev ( sizeof ( struct e1000_adapter ) );
704
 
        if ( ! netdev )
705
 
                goto err_alloc_etherdev;
706
 
 
707
 
        /* Associate e1000-specific network operations operations with
708
 
         * generic network device layer */
709
 
        netdev_init ( netdev, &e1000_operations );
710
 
 
711
 
        /* Associate this network device with given PCI device */
712
 
        pci_set_drvdata ( pdev, netdev );
713
 
        netdev->dev = &pdev->dev;
714
 
 
715
 
        /* Initialize driver private storage */
716
 
        adapter = netdev_priv ( netdev );
717
 
        memset ( adapter, 0, ( sizeof ( *adapter ) ) );
718
 
 
719
 
        adapter->pdev       = pdev;
720
 
 
721
 
        adapter->ioaddr     = pdev->ioaddr;
722
 
        adapter->hw.io_base = pdev->ioaddr;
723
 
 
724
 
        adapter->irqno      = pdev->irq;
725
 
        adapter->netdev     = netdev;
726
 
        adapter->hw.back    = adapter;
727
 
 
728
 
        adapter->tx_ring_size = sizeof ( *adapter->tx_base ) * NUM_TX_DESC;
729
 
        adapter->rx_ring_size = sizeof ( *adapter->rx_base ) * NUM_RX_DESC;
730
 
 
731
 
        mmio_start = pci_bar_start ( pdev, PCI_BASE_ADDRESS_0 );
732
 
        mmio_len   = pci_bar_size  ( pdev, PCI_BASE_ADDRESS_0 );
733
 
 
734
 
        DBG ( "mmio_start: %#08lx\n", mmio_start );
735
 
        DBG ( "mmio_len: %#08lx\n", mmio_len );
736
 
 
737
 
        /* Fix up PCI device */
738
 
        adjust_pci_device ( pdev );
739
 
 
740
 
        err = -EIO;
741
 
 
742
 
        adapter->hw.hw_addr = ioremap ( mmio_start, mmio_len );
743
 
        DBG ( "adapter->hw.hw_addr: %p\n", adapter->hw.hw_addr );
744
 
 
745
 
        if ( ! adapter->hw.hw_addr )
746
 
                goto err_ioremap;
747
 
 
748
 
        /* Hardware features, flags and workarounds */
749
 
        if (adapter->hw.mac.type >= e1000_82540) {
750
 
                adapter->flags |= E1000_FLAG_HAS_SMBUS;
751
 
                adapter->flags |= E1000_FLAG_HAS_INTR_MODERATION;
752
 
        }
753
 
 
754
 
        if (adapter->hw.mac.type == e1000_82543)
755
 
                adapter->flags |= E1000_FLAG_BAD_TX_CARRIER_STATS_FD;
756
 
 
757
 
        adapter->hw.phy.autoneg_wait_to_complete = true;
758
 
        adapter->hw.mac.adaptive_ifs = true;
759
 
 
760
 
        /* setup the private structure */
761
 
        if ( ( err = e1000_sw_init ( adapter ) ) )
762
 
                goto err_sw_init;
763
 
 
764
 
        if ((err = e1000_init_mac_params(&adapter->hw)))
765
 
                goto err_hw_init;
766
 
 
767
 
        if ((err = e1000_init_nvm_params(&adapter->hw)))
768
 
                goto err_hw_init;
769
 
 
770
 
        /* Force auto-negotiated speed and duplex */
771
 
        adapter->hw.mac.autoneg = 1;
772
 
 
773
 
        if ((err = e1000_init_phy_params(&adapter->hw)))
774
 
                goto err_hw_init;
775
 
 
776
 
        DBG ( "adapter->hw.mac.type: %#08x\n", adapter->hw.mac.type );
777
 
 
778
 
        /* before reading the EEPROM, reset the controller to
779
 
         * put the device in a known good starting state
780
 
         */
781
 
        err = e1000_reset_hw ( &adapter->hw );
782
 
        if ( err < 0 ) {
783
 
                DBG ( "Hardware Initialization Failed\n" );
784
 
                goto err_reset;
785
 
        }
786
 
        /* make sure the NVM is good */
787
 
 
788
 
        if ( e1000_validate_nvm_checksum(&adapter->hw) < 0 ) {
789
 
                DBG ( "The NVM Checksum Is Not Valid\n" );
790
 
                err = -EIO;
791
 
                goto err_eeprom;
792
 
        }
793
 
 
794
 
        /* copy the MAC address out of the EEPROM */
795
 
        if ( e1000_read_mac_addr ( &adapter->hw ) )
796
 
                DBG ( "EEPROM Read Error\n" );
797
 
 
798
 
        memcpy ( netdev->hw_addr, adapter->hw.mac.perm_addr, ETH_ALEN );
799
 
 
800
 
        /* reset the hardware with the new settings */
801
 
        e1000_reset ( adapter );
802
 
 
803
 
        if ( ( err = register_netdev ( netdev ) ) != 0)
804
 
                goto err_register;
805
 
 
806
 
        /* Mark as link up; we don't yet handle link state */
807
 
        netdev_link_up ( netdev );
808
 
 
809
 
        for (i = 0; i < 6; i++)
810
 
                DBG ("%02x%s", netdev->ll_addr[i], i == 5 ? "\n" : ":");
811
 
 
812
 
        DBG ( "e1000_probe succeeded!\n" );
813
 
 
814
 
        /* No errors, return success */
815
 
        return 0;
816
 
 
817
 
/* Error return paths */
818
 
err_reset:
819
 
err_register:
820
 
err_hw_init:
821
 
err_eeprom:
822
 
        if (!e1000_check_reset_block(&adapter->hw))
823
 
                e1000_phy_hw_reset(&adapter->hw);
824
 
        if (adapter->hw.flash_address)
825
 
                iounmap(adapter->hw.flash_address);
826
 
err_sw_init:
827
 
        iounmap ( adapter->hw.hw_addr );
828
 
err_ioremap:
829
 
        netdev_put ( netdev );
830
 
err_alloc_etherdev:
831
 
        return err;
832
 
}
833
 
 
834
 
/**
835
 
 * e1000_remove - Device Removal Routine
836
 
 *
837
 
 * @v pdev PCI device information struct
838
 
 *
839
 
 **/
840
 
void e1000_remove ( struct pci_device *pdev )
841
 
{
842
 
        struct net_device *netdev = pci_get_drvdata ( pdev );
843
 
        struct e1000_adapter *adapter = netdev_priv ( netdev );
844
 
 
845
 
        DBG ( "e1000_remove\n" );
846
 
 
847
 
        if ( adapter->hw.flash_address )
848
 
                iounmap ( adapter->hw.flash_address );
849
 
        if  ( adapter->hw.hw_addr )
850
 
                iounmap ( adapter->hw.hw_addr );
851
 
 
852
 
        unregister_netdev ( netdev );
853
 
        e1000_reset_hw ( &adapter->hw );
854
 
        netdev_nullify ( netdev );
855
 
        netdev_put ( netdev );
856
 
}
857
 
 
858
 
/**
859
 
 * e1000_open - Called when a network interface is made active
860
 
 *
861
 
 * @v netdev    network interface device structure
862
 
 * @ret rc      Return status code, 0 on success, negative value on failure
863
 
 *
864
 
 **/
865
 
static int e1000_open ( struct net_device *netdev )
866
 
{
867
 
        struct e1000_adapter *adapter = netdev_priv(netdev);
868
 
        int err;
869
 
 
870
 
        DBG ( "e1000_open\n" );
871
 
 
872
 
        /* allocate transmit descriptors */
873
 
        err = e1000_setup_tx_resources ( adapter );
874
 
        if ( err ) {
875
 
                DBG ( "Error setting up TX resources!\n" );
876
 
                goto err_setup_tx;
877
 
        }
878
 
 
879
 
        /* allocate receive descriptors */
880
 
        err = e1000_setup_rx_resources ( adapter );
881
 
        if ( err ) {
882
 
                DBG ( "Error setting up RX resources!\n" );
883
 
                goto err_setup_rx;
884
 
        }
885
 
 
886
 
        e1000_configure_tx ( adapter );
887
 
 
888
 
        e1000_configure_rx ( adapter );
889
 
 
890
 
        DBG ( "E1000_RXDCTL(0): %#08x\n",  E1000_READ_REG ( &adapter->hw, E1000_RXDCTL(0) ) );
891
 
 
892
 
        return 0;
893
 
 
894
 
err_setup_rx:
895
 
        e1000_free_tx_resources ( adapter );
896
 
err_setup_tx:
897
 
        e1000_reset ( adapter );
898
 
 
899
 
        return err;
900
 
}
901
 
 
902
 
/** e1000 net device operations */
903
 
static struct net_device_operations e1000_operations = {
904
 
        .open           = e1000_open,
905
 
        .close          = e1000_close,
906
 
        .transmit       = e1000_transmit,
907
 
        .poll           = e1000_poll,
908
 
        .irq            = e1000_irq,
909
 
};