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