Add qemu 2.4.0
[kvmfornfv.git] / qemu / roms / ipxe / src / drivers / net / igbvf / igbvf_main.c
1 /*******************************************************************************
2
3   Intel(R) 82576 Virtual Function Linux driver
4   Copyright(c) 2009 Intel Corporation.
5
6   Copyright(c) 2010 Eric Keller <ekeller@princeton.edu>
7   Copyright(c) 2010 Red Hat Inc.
8         Alex Williamson <alex.williamson@redhat.com>
9
10   This program is free software; you can redistribute it and/or modify it
11   under the terms and conditions of the GNU General Public License,
12   version 2, as published by the Free Software Foundation.
13
14   This program is distributed in the hope it will be useful, but WITHOUT
15   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16   FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
17   more details.
18
19   You should have received a copy of the GNU General Public License along with
20   this program; if not, write to the Free Software Foundation, Inc.,
21   51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
22
23   The full GNU General Public License is included in this distribution in
24   the file called "COPYING".
25
26   Contact Information:
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 "igbvf.h"
35
36 /**
37  * igbvf_setup_tx_resources - allocate Tx resources (Descriptors)
38  *
39  * @v adapter   e1000 private structure
40  *
41  * @ret rc       Returns 0 on success, negative on failure
42  **/
43 int igbvf_setup_tx_resources ( struct igbvf_adapter *adapter )
44 {
45         DBG ( "igbvf_setup_tx_resources\n" );
46
47         /* Allocate transmit descriptor ring memory.
48            It must not cross a 64K boundary because of hardware errata #23
49            so we use malloc_dma() requesting a 128 byte block that is
50            128 byte aligned. This should guarantee that the memory
51            allocated will not cross a 64K boundary, because 128 is an
52            even multiple of 65536 ( 65536 / 128 == 512 ), so all possible
53            allocations of 128 bytes on a 128 byte boundary will not
54            cross 64K bytes.
55          */
56
57         adapter->tx_base =
58                 malloc_dma ( adapter->tx_ring_size, adapter->tx_ring_size );
59
60         if ( ! adapter->tx_base ) {
61                 return -ENOMEM;
62         }
63
64         memset ( adapter->tx_base, 0, adapter->tx_ring_size );
65
66         DBG ( "adapter->tx_base = %#08lx\n", virt_to_bus ( adapter->tx_base ) );
67
68         return 0;
69 }
70
71 /**
72  * igbvf_free_tx_resources - Free Tx Resources per Queue
73  * @adapter: board private structure
74  *
75  * Free all transmit software resources
76  **/
77 void igbvf_free_tx_resources ( struct igbvf_adapter *adapter )
78 {
79         DBG ( "igbvf_free_tx_resources\n" );
80
81         free_dma ( adapter->tx_base, adapter->tx_ring_size );
82 }
83
84 /**
85  * igbvf_free_rx_resources - Free Rx Resources
86  * @adapter: board private structure
87  *
88  * Free all receive software resources
89  **/
90 void igbvf_free_rx_resources ( struct igbvf_adapter *adapter )
91 {
92         int i;
93
94         DBG ( "igbvf_free_rx_resources\n" );
95
96         free_dma ( adapter->rx_base, adapter->rx_ring_size );
97
98         for ( i = 0; i < NUM_RX_DESC; i++ ) {
99                 free_iob ( adapter->rx_iobuf[i] );
100         }
101 }
102
103 /**
104  * igbvf_refill_rx_ring - allocate Rx io_buffers
105  *
106  * @v adapter   e1000 private structure
107  *
108  * @ret rc       Returns 0 on success, negative on failure
109  **/
110 static int igbvf_refill_rx_ring ( struct igbvf_adapter *adapter )
111 {
112         int i, rx_curr;
113         int rc = 0;
114         union e1000_adv_rx_desc *rx_curr_desc;
115         struct e1000_hw *hw = &adapter->hw;
116         struct io_buffer *iob;
117
118         DBGP ("igbvf_refill_rx_ring\n");
119
120         for ( i = 0; i < NUM_RX_DESC; i++ ) {
121                 rx_curr = ( ( adapter->rx_curr + i ) % NUM_RX_DESC );
122                 rx_curr_desc = adapter->rx_base + rx_curr;
123
124                 if ( rx_curr_desc->wb.upper.status_error & E1000_RXD_STAT_DD )
125                         continue;
126
127                 if ( adapter->rx_iobuf[rx_curr] != NULL )
128                         continue;
129
130                 DBG2 ( "Refilling rx desc %d\n", rx_curr );
131
132                 iob = alloc_iob ( MAXIMUM_ETHERNET_VLAN_SIZE );
133                 adapter->rx_iobuf[rx_curr] = iob;
134
135                 rx_curr_desc->wb.upper.status_error = 0;
136
137                 if ( ! iob ) {
138                         DBG ( "alloc_iob failed\n" );
139                         rc = -ENOMEM;
140                         break;
141                 } else {
142                         rx_curr_desc->read.pkt_addr = virt_to_bus ( iob->data );
143                         rx_curr_desc->read.hdr_addr = 0;
144                         ew32 ( RDT(0), rx_curr );
145                 }
146         }
147         return rc;
148 }
149
150 /**
151  * igbvf_irq_disable - Mask off interrupt generation on the NIC
152  * @adapter: board private structure
153  **/
154 static void igbvf_irq_disable ( struct igbvf_adapter *adapter )
155 {
156         struct e1000_hw *hw = &adapter->hw;
157
158         ew32 ( EIMC, ~0 );
159 }
160
161 /**
162  * igbvf_irq_enable - Enable default interrupt generation settings
163  * @adapter: board private structure
164  **/
165 static void igbvf_irq_enable ( struct igbvf_adapter *adapter )
166 {
167         struct e1000_hw *hw = &adapter->hw;
168
169         ew32 ( EIAC, IMS_ENABLE_MASK );
170         ew32 ( EIAM, IMS_ENABLE_MASK );
171         ew32 ( EIMS, IMS_ENABLE_MASK );
172 }
173
174 /**
175  * igbvf_irq - enable or Disable interrupts
176  *
177  * @v adapter   e1000 adapter
178  * @v action    requested interrupt action
179  **/
180 static void igbvf_irq ( struct net_device *netdev, int enable )
181 {
182         struct igbvf_adapter *adapter = netdev_priv ( netdev );
183
184         DBG ( "igbvf_irq\n" );
185
186         if ( enable ) {
187                 igbvf_irq_enable ( adapter );
188         } else {
189                 igbvf_irq_disable ( adapter );
190         }
191 }
192
193 /**
194  * igbvf_process_tx_packets - process transmitted packets
195  *
196  * @v netdev    network interface device structure
197  **/
198 static void igbvf_process_tx_packets ( struct net_device *netdev )
199 {
200         struct igbvf_adapter *adapter = netdev_priv ( netdev );
201         uint32_t i;
202         uint32_t tx_status;
203         union e1000_adv_tx_desc *tx_curr_desc;
204
205         /* Check status of transmitted packets
206          */
207         DBGP ( "process_tx_packets: tx_head = %d, tx_tail = %d\n", adapter->tx_head,
208               adapter->tx_tail );
209
210         while ( ( i = adapter->tx_head ) != adapter->tx_tail ) {
211
212                 tx_curr_desc = ( void * )  ( adapter->tx_base ) +
213                                            ( i * sizeof ( *adapter->tx_base ) );
214
215                 tx_status = tx_curr_desc->wb.status;
216                 DBG ( "  tx_curr_desc = %#08lx\n", virt_to_bus ( tx_curr_desc ) );
217                 DBG ( "  tx_status = %#08x\n", tx_status );
218
219                 /* if the packet at tx_head is not owned by hardware it is for us */
220                 if ( ! ( tx_status & E1000_TXD_STAT_DD ) )
221                         break;
222
223                 DBG ( "Sent packet. tx_head: %d tx_tail: %d tx_status: %#08x\n",
224                       adapter->tx_head, adapter->tx_tail, tx_status );
225
226                 netdev_tx_complete ( netdev, adapter->tx_iobuf[i] );
227                 DBG ( "Success transmitting packet, tx_status: %#08x\n",
228                       tx_status );
229
230                 /* Decrement count of used descriptors, clear this descriptor
231                  */
232                 adapter->tx_fill_ctr--;
233                 memset ( tx_curr_desc, 0, sizeof ( *tx_curr_desc ) );
234
235                 adapter->tx_head = ( adapter->tx_head + 1 ) % NUM_TX_DESC;
236         }
237 }
238
239 /**
240  * igbvf_process_rx_packets - process received packets
241  *
242  * @v netdev    network interface device structure
243  **/
244 static void igbvf_process_rx_packets ( struct net_device *netdev )
245 {
246         struct igbvf_adapter *adapter = netdev_priv ( netdev );
247         struct e1000_hw *hw = &adapter->hw;
248         uint32_t i;
249         uint32_t rx_status;
250         uint32_t rx_len;
251         uint32_t rx_err;
252         union e1000_adv_rx_desc *rx_curr_desc;
253
254         DBGP ( "igbvf_process_rx_packets\n" );
255
256         /* Process received packets
257          */
258         while ( 1 ) {
259                 i = adapter->rx_curr;
260
261                 rx_curr_desc = ( void * )  ( adapter->rx_base ) +
262                                   ( i * sizeof ( *adapter->rx_base ) );
263                 rx_status = rx_curr_desc->wb.upper.status_error;
264
265                 DBG2 ( "Before DD Check RX_status: %#08x, rx_curr: %d\n",
266                        rx_status, i );
267
268                 if ( ! ( rx_status & E1000_RXD_STAT_DD ) )
269                         break;
270
271                 if ( adapter->rx_iobuf[i] == NULL )
272                         break;
273
274                 DBG ( "E1000_RCTL = %#08x\n", er32 (RCTL) );
275
276                 rx_len = rx_curr_desc->wb.upper.length;
277
278                 DBG ( "Received packet, rx_curr: %d  rx_status: %#08x  rx_len: %d\n",
279                       i, rx_status, rx_len );
280
281                 rx_err = rx_status;
282
283                 iob_put ( adapter->rx_iobuf[i], rx_len );
284
285                 if ( rx_err & E1000_RXDEXT_ERR_FRAME_ERR_MASK ) {
286
287                         netdev_rx_err ( netdev, adapter->rx_iobuf[i], -EINVAL );
288                         DBG ( "igbvf_process_rx_packets: Corrupted packet received!"
289                               " rx_err: %#08x\n", rx_err );
290                 } else  {
291                         /* Add this packet to the receive queue. */
292                         netdev_rx ( netdev, adapter->rx_iobuf[i] );
293                 }
294                 adapter->rx_iobuf[i] = NULL;
295
296                 memset ( rx_curr_desc, 0, sizeof ( *rx_curr_desc ) );
297
298                 adapter->rx_curr = ( adapter->rx_curr + 1 ) % NUM_RX_DESC;
299         }
300 }
301
302 /**
303  * igbvf_poll - Poll for received packets
304  *
305  * @v netdev    Network device
306  */
307 static void igbvf_poll ( struct net_device *netdev )
308 {
309         struct igbvf_adapter *adapter = netdev_priv ( netdev );
310         uint32_t rx_status;
311         union e1000_adv_rx_desc *rx_curr_desc;
312
313         DBGP ( "igbvf_poll\n" );
314
315         rx_curr_desc = ( void * )  ( adapter->rx_base ) +
316                         ( adapter->rx_curr * sizeof ( *adapter->rx_base ) );
317         rx_status = rx_curr_desc->wb.upper.status_error;
318
319         if ( ! ( rx_status & E1000_RXD_STAT_DD ) )
320                 return;
321
322         igbvf_process_tx_packets ( netdev );
323
324         igbvf_process_rx_packets ( netdev );
325
326         igbvf_refill_rx_ring ( adapter );
327 }
328
329 /**
330  *  igbvf_config_collision_dist_generic - Configure collision distance
331  *  @hw: pointer to the HW structure
332  *
333  *  Configures the collision distance to the default value and is used
334  *  during link setup. Currently no func pointer exists and all
335  *  implementations are handled in the generic version of this function.
336  **/
337 void igbvf_config_collision_dist ( struct e1000_hw *hw )
338 {
339         u32 tctl;
340
341         DBG ("igbvf_config_collision_dist");
342
343         tctl = er32 (TCTL);
344
345         tctl &= ~E1000_TCTL_COLD;
346         tctl |= E1000_COLLISION_DISTANCE << E1000_COLD_SHIFT;
347
348         ew32 (TCTL, tctl);
349         e1e_flush();
350 }
351
352 /**
353  * igbvf_configure_tx - Configure Transmit Unit after Reset
354  * @adapter: board private structure
355  *
356  * Configure the Tx unit of the MAC after a reset.
357  **/
358 static void igbvf_configure_tx ( struct igbvf_adapter *adapter )
359 {
360         struct e1000_hw *hw = &adapter->hw;
361         u32 tctl, txdctl;
362
363         DBG ( "igbvf_configure_tx\n" );
364
365         /* disable transmits while setting up the descriptors */
366         tctl = er32 ( TCTL );
367         ew32 ( TCTL, tctl & ~E1000_TCTL_EN );
368         e1e_flush();
369         mdelay (10);
370
371         ew32 ( TDBAH(0), 0 );
372         ew32 ( TDBAL(0), virt_to_bus ( adapter->tx_base ) );
373         ew32 ( TDLEN(0), adapter->tx_ring_size );
374
375         DBG ( "E1000_TDBAL(0): %#08x\n",  er32 ( TDBAL(0) ) );
376         DBG ( "E1000_TDLEN(0): %d\n",     er32 ( TDLEN(0) ) );
377
378         /* Setup the HW Tx Head and Tail descriptor pointers */
379         ew32 ( TDH(0), 0 );
380         ew32 ( TDT(0), 0 );
381
382         adapter->tx_head = 0;
383         adapter->tx_tail = 0;
384         adapter->tx_fill_ctr = 0;
385
386         txdctl = er32(TXDCTL(0));
387         txdctl |= E1000_TXDCTL_QUEUE_ENABLE;
388         ew32 ( TXDCTL(0), txdctl );
389
390         txdctl = er32 ( TXDCTL(0) );
391         txdctl |= E1000_TXDCTL_QUEUE_ENABLE;
392         ew32 ( TXDCTL(0), txdctl );
393
394         /* Setup Transmit Descriptor Settings for eop descriptor */
395         adapter->txd_cmd  = E1000_ADVTXD_DCMD_EOP | E1000_ADVTXD_DCMD_IFCS;
396
397         /* Advanced descriptor */
398         adapter->txd_cmd |= E1000_ADVTXD_DCMD_DEXT;
399
400         /* (not part of cmd, but in same 32 bit word...) */
401         adapter->txd_cmd |= E1000_ADVTXD_DTYP_DATA;
402
403         /* enable Report Status bit */
404         adapter->txd_cmd |= E1000_ADVTXD_DCMD_RS;
405
406         /* Program the Transmit Control Register */
407         tctl &= ~E1000_TCTL_CT;
408         tctl |= E1000_TCTL_PSP | E1000_TCTL_RTLC |
409                 (E1000_COLLISION_THRESHOLD << E1000_CT_SHIFT);
410
411         igbvf_config_collision_dist ( hw );
412
413         /* Enable transmits */
414         tctl |= E1000_TCTL_EN;
415         ew32(TCTL, tctl);
416         e1e_flush();
417 }
418
419 /* igbvf_reset - bring the hardware into a known good state
420  *
421  * This function boots the hardware and enables some settings that
422  * require a configuration cycle of the hardware - those cannot be
423  * set/changed during runtime. After reset the device needs to be
424  * properly configured for Rx, Tx etc.
425  */
426 void igbvf_reset ( struct igbvf_adapter *adapter )
427 {
428         struct e1000_mac_info *mac = &adapter->hw.mac;
429         struct net_device *netdev = adapter->netdev;
430         struct e1000_hw *hw = &adapter->hw;
431
432         /* Allow time for pending master requests to run */
433         if ( mac->ops.reset_hw(hw) )
434                 DBG ("PF still resetting\n");
435
436         mac->ops.init_hw ( hw );
437
438         if ( is_valid_ether_addr(adapter->hw.mac.addr) ) {
439                 memcpy ( netdev->hw_addr, adapter->hw.mac.addr, ETH_ALEN );
440         }
441 }
442
443 extern void igbvf_init_function_pointers_vf(struct e1000_hw *hw);
444
445 /**
446  * igbvf_sw_init - Initialize general software structures (struct igbvf_adapter)
447  * @adapter: board private structure to initialize
448  *
449  * igbvf_sw_init initializes the Adapter private data structure.
450  * Fields are initialized based on PCI device information and
451  * OS network device settings (MTU size).
452  **/
453 static int __devinit igbvf_sw_init ( struct igbvf_adapter *adapter )
454 {
455         struct e1000_hw *hw = &adapter->hw;
456         struct pci_device *pdev = adapter->pdev;
457         int rc;
458
459         /* PCI config space info */
460
461         hw->vendor_id = pdev->vendor;
462         hw->device_id = pdev->device;
463
464         pci_read_config_byte ( pdev, PCI_REVISION_ID, &hw->revision_id );
465
466         pci_read_config_word ( pdev, PCI_COMMAND, &hw->bus.pci_cmd_word );
467
468         adapter->max_frame_size = MAXIMUM_ETHERNET_VLAN_SIZE + ETH_HLEN + ETH_FCS_LEN;
469         adapter->min_frame_size = ETH_ZLEN + ETH_FCS_LEN;
470
471         /* Set various function pointers */
472         igbvf_init_function_pointers_vf ( &adapter->hw );
473
474         rc = adapter->hw.mac.ops.init_params ( &adapter->hw );
475         if (rc) {
476                 DBG ("hw.mac.ops.init_params(&adapter->hw) Failure\n");
477                 return rc;
478         }
479
480         rc = adapter->hw.mbx.ops.init_params ( &adapter->hw );
481         if (rc) {
482                 DBG ("hw.mbx.ops.init_params(&adapter->hw) Failure\n");
483                 return rc;
484         }
485
486         /* Explicitly disable IRQ since the NIC can be in any state. */
487         igbvf_irq_disable ( adapter );
488
489         return 0;
490 }
491
492 /**
493  * igbvf_setup_srrctl - configure the receive control registers
494  * @adapter: Board private structure
495  **/
496 static void igbvf_setup_srrctl ( struct igbvf_adapter *adapter )
497 {
498         struct e1000_hw *hw = &adapter->hw;
499         u32 srrctl = 0;
500
501         DBG ( "igbvf_setup_srrctl\n" );
502
503         srrctl &= ~(E1000_SRRCTL_DESCTYPE_MASK |
504                     E1000_SRRCTL_BSIZEHDR_MASK |
505                     E1000_SRRCTL_BSIZEPKT_MASK);
506
507         /* Enable queue drop to avoid head of line blocking */
508         srrctl |= E1000_SRRCTL_DROP_EN;
509
510         /* Setup buffer sizes */
511         srrctl |= 2048 >> E1000_SRRCTL_BSIZEPKT_SHIFT;
512         srrctl |= E1000_SRRCTL_DESCTYPE_ADV_ONEBUF;
513
514         ew32 ( SRRCTL(0), srrctl );
515 }
516
517 /**
518  * igbvf_configure_rx - Configure 8254x Receive Unit after Reset
519  * @adapter: board private structure
520  *
521  * Configure the Rx unit of the MAC after a reset.
522  **/
523 static void igbvf_configure_rx ( struct igbvf_adapter *adapter )
524 {
525         struct e1000_hw *hw = &adapter->hw;
526         u32 rxdctl;
527
528         DBG ( "igbvf_configure_rx\n" );
529
530         /* disable receives */
531         rxdctl = er32 ( RXDCTL(0) );
532         ew32 ( RXDCTL(0), rxdctl & ~E1000_RXDCTL_QUEUE_ENABLE );
533         msleep ( 10 );
534
535         /*
536          * Setup the HW Rx Head and Tail Descriptor Pointers and
537          * the Base and Length of the Rx Descriptor Ring
538          */
539         ew32 ( RDBAL(0), virt_to_bus (adapter->rx_base) );
540         ew32 ( RDBAH(0), 0 );
541         ew32 ( RDLEN(0), adapter->rx_ring_size );
542         adapter->rx_curr = 0;
543         ew32 ( RDH(0), 0 );
544         ew32 ( RDT(0), 0 );
545
546         rxdctl |= E1000_RXDCTL_QUEUE_ENABLE;
547         rxdctl &= 0xFFF00000;
548         rxdctl |= IGBVF_RX_PTHRESH;
549         rxdctl |= IGBVF_RX_HTHRESH << 8;
550         rxdctl |= IGBVF_RX_WTHRESH << 16;
551
552         igbvf_rlpml_set_vf ( hw, adapter->max_frame_size );
553
554         /* enable receives */
555         ew32 ( RXDCTL(0), rxdctl );
556         ew32 ( RDT(0), NUM_RX_DESC );
557 }
558
559 /**
560  * igbvf_setup_rx_resources - allocate Rx resources (Descriptors)
561  *
562  * @v adapter   e1000 private structure
563  **/
564 int igbvf_setup_rx_resources ( struct igbvf_adapter *adapter )
565 {
566         int i;
567         union e1000_adv_rx_desc *rx_curr_desc;
568         struct io_buffer *iob;
569
570         DBG ( "igbvf_setup_rx_resources\n" );
571
572         /* Allocate receive descriptor ring memory.
573            It must not cross a 64K boundary because of hardware errata
574          */
575
576         adapter->rx_base =
577                 malloc_dma ( adapter->rx_ring_size, adapter->rx_ring_size );
578
579         if ( ! adapter->rx_base ) {
580                 return -ENOMEM;
581         }
582         memset ( adapter->rx_base, 0, adapter->rx_ring_size );
583
584         for ( i = 0; i < NUM_RX_DESC; i++ ) {
585                 rx_curr_desc = adapter->rx_base + i;
586                 iob = alloc_iob ( MAXIMUM_ETHERNET_VLAN_SIZE );
587                 adapter->rx_iobuf[i] = iob;
588                 rx_curr_desc->wb.upper.status_error = 0;
589                 if ( ! iob ) {
590                         DBG ( "alloc_iob failed\n" );
591                         return -ENOMEM;
592                 } else {
593                         rx_curr_desc->read.pkt_addr = virt_to_bus ( iob->data );
594                         rx_curr_desc->read.hdr_addr = 0;
595                 }
596         }
597
598         return 0;
599 }
600
601 /**
602  * igbvf_open - Called when a network interface is made active
603  * @netdev: network interface device structure
604  *
605  * Returns 0 on success, negative value on failure
606  *
607  * The open entry point is called when a network interface is made
608  * active by the system (IFF_UP).  At this point all resources needed
609  * for transmit and receive operations are allocated, the interrupt
610  * handler is registered with the OS, the watchdog timer is started,
611  * and the stack is notified that the interface is ready.
612  **/
613 static int igbvf_open ( struct net_device *netdev )
614 {
615         struct igbvf_adapter *adapter = netdev_priv ( netdev );
616         int err;
617
618         DBG ("igbvf_open\n");
619
620         /* Update MAC address */
621         memcpy ( adapter->hw.mac.addr, netdev->ll_addr, ETH_ALEN );
622         igbvf_reset( adapter );
623
624         /* allocate transmit descriptors */
625         err = igbvf_setup_tx_resources ( adapter );
626         if (err) {
627                 DBG ( "Error setting up TX resources!\n" );
628                 goto err_setup_tx;
629         }
630
631         igbvf_configure_tx ( adapter );
632
633         igbvf_setup_srrctl( adapter );
634
635         err = igbvf_setup_rx_resources( adapter );
636         if (err) {
637                 DBG ( "Error setting up RX resources!\n" );
638                 goto err_setup_rx;
639         }
640
641         igbvf_configure_rx ( adapter );
642
643         return 0;
644
645 err_setup_rx:
646         DBG ( "err_setup_rx\n" );
647         igbvf_free_tx_resources ( adapter );
648         return err;
649
650 err_setup_tx:
651         DBG ( "err_setup_tx\n" );
652         igbvf_reset ( adapter );
653
654         return err;
655 }
656
657 /**
658  * igbvf_close - Disables a network interface
659  * @netdev: network interface device structure
660  *
661  * Returns 0, this is not allowed to fail
662  *
663  * The close entry point is called when an interface is de-activated
664  * by the OS.  The hardware is still under the drivers control, but
665  * needs to be disabled.  A global MAC reset is issued to stop the
666  * hardware, and all transmit and receive resources are freed.
667  **/
668 static void igbvf_close ( struct net_device *netdev )
669 {
670         struct igbvf_adapter *adapter = netdev_priv ( netdev );
671         struct e1000_hw *hw = &adapter->hw;
672         uint32_t rxdctl;
673
674         DBG ( "igbvf_close\n" );
675
676         /* Disable and acknowledge interrupts */
677         igbvf_irq_disable ( adapter );
678         er32(EICR);
679
680         /* disable receives */
681         rxdctl = er32 ( RXDCTL(0) );
682         ew32 ( RXDCTL(0), rxdctl & ~E1000_RXDCTL_QUEUE_ENABLE );
683         mdelay ( 10 );
684
685         igbvf_reset ( adapter );
686
687         igbvf_free_tx_resources( adapter );
688         igbvf_free_rx_resources( adapter );
689 }
690
691 /**
692  * igbvf_transmit - Transmit a packet
693  *
694  * @v netdev    Network device
695  * @v iobuf     I/O buffer
696  *
697  * @ret rc       Returns 0 on success, negative on failure
698  */
699 static int igbvf_transmit ( struct net_device *netdev, struct io_buffer *iobuf )
700 {
701         struct igbvf_adapter *adapter = netdev_priv ( netdev );
702         struct e1000_hw *hw = &adapter->hw;
703         uint32_t tx_curr = adapter->tx_tail;
704         union e1000_adv_tx_desc *tx_curr_desc;
705
706         DBGP ("igbvf_transmit\n");
707
708         if ( adapter->tx_fill_ctr == NUM_TX_DESC ) {
709                 DBG ("TX overflow\n");
710                 return -ENOBUFS;
711         }
712
713         /* Save pointer to iobuf we have been given to transmit,
714            netdev_tx_complete() will need it later
715          */
716         adapter->tx_iobuf[tx_curr] = iobuf;
717
718         tx_curr_desc = ( void * ) ( adapter->tx_base ) +
719                        ( tx_curr * sizeof ( *adapter->tx_base ) );
720
721         DBG ( "tx_curr_desc = %#08lx\n", virt_to_bus ( tx_curr_desc ) );
722         DBG ( "tx_curr_desc + 16 = %#08lx\n", virt_to_bus ( tx_curr_desc ) + 16 );
723         DBG ( "iobuf->data = %#08lx\n", virt_to_bus ( iobuf->data ) );
724
725         /* Add the packet to TX ring
726          */
727         tx_curr_desc->read.buffer_addr = virt_to_bus ( iobuf->data );
728         tx_curr_desc->read.cmd_type_len = adapter->txd_cmd |(iob_len ( iobuf )) ;
729         // minus hdr_len ????
730         tx_curr_desc->read.olinfo_status = ((iob_len ( iobuf )) << E1000_ADVTXD_PAYLEN_SHIFT);
731
732         DBG ( "TX fill: %d tx_curr: %d addr: %#08lx len: %zd\n", adapter->tx_fill_ctr,
733               tx_curr, virt_to_bus ( iobuf->data ), iob_len ( iobuf ) );
734
735         /* Point to next free descriptor */
736         adapter->tx_tail = ( adapter->tx_tail + 1 ) % NUM_TX_DESC;
737         adapter->tx_fill_ctr++;
738
739         /* Write new tail to NIC, making packet available for transmit
740          */
741         ew32 ( TDT(0), adapter->tx_tail );
742         e1e_flush ();
743
744         return 0;
745 }
746
747 /** igbvf net device operations */
748 static struct net_device_operations igbvf_operations = {
749         .open           = igbvf_open,
750         .close          = igbvf_close,
751         .transmit       = igbvf_transmit,
752         .poll           = igbvf_poll,
753         .irq            = igbvf_irq,
754 };
755
756 /**
757  * igbvf_get_hw_control - get control of the h/w from f/w
758  * @adapter: address of board private structure
759  *
760  * igb_get_hw_control sets CTRL_EXT:DRV_LOAD bit.
761  * For ASF and Pass Through versions of f/w this means that
762  * the driver is loaded.
763  *
764  **/
765 void igbvf_get_hw_control ( struct igbvf_adapter *adapter )
766 {
767         struct e1000_hw *hw = &adapter->hw;
768         u32 ctrl_ext;
769
770         /* Let firmware know the driver has taken over */
771         ctrl_ext = er32 ( CTRL_EXT );
772         ew32 ( CTRL_EXT, ctrl_ext | E1000_CTRL_EXT_DRV_LOAD );
773 }
774
775 /**
776  * igbvf_probe - Device Initialization Routine
777  * @pdev: PCI device information struct
778  * @ent: entry in igbvf_pci_tbl
779  *
780  * Returns 0 on success, negative on failure
781  *
782  * igbvf_probe initializes an adapter identified by a pci_dev structure.
783  * The OS initialization, configuring of the adapter private structure,
784  * and a hardware reset occur.
785  **/
786 int igbvf_probe ( struct pci_device *pdev )
787 {
788         int err;
789         struct net_device *netdev;
790         struct igbvf_adapter *adapter;
791         unsigned long mmio_start, mmio_len;
792         struct e1000_hw *hw;
793
794         DBG ( "igbvf_probe\n" );
795
796         err = -ENOMEM;
797
798         /* Allocate net device ( also allocates memory for netdev->priv
799           and makes netdev-priv point to it ) */
800         netdev = alloc_etherdev ( sizeof ( struct igbvf_adapter ) );
801         if ( ! netdev )
802                 goto err_alloc_etherdev;
803
804         /* Associate igbvf-specific network operations operations with
805          * generic network device layer */
806         netdev_init ( netdev, &igbvf_operations );
807
808         /* Associate this network device with given PCI device */
809         pci_set_drvdata ( pdev, netdev );
810         netdev->dev = &pdev->dev;
811
812         /* Initialize driver private storage */
813         adapter = netdev_priv ( netdev );
814         memset ( adapter, 0, ( sizeof ( *adapter ) ) );
815
816         adapter->pdev = pdev;
817
818         adapter->ioaddr = pdev->ioaddr;
819         adapter->hw.io_base = pdev->ioaddr;
820
821         hw = &adapter->hw;
822         hw->vendor_id = pdev->vendor;
823         hw->device_id = pdev->device;
824
825         adapter->irqno = pdev->irq;
826         adapter->netdev = netdev;
827         adapter->hw.back = adapter;
828
829         adapter->min_frame_size = ETH_ZLEN + ETH_FCS_LEN;
830         adapter->max_hw_frame_size = ETH_FRAME_LEN + ETH_FCS_LEN;
831
832         adapter->tx_ring_size = sizeof ( *adapter->tx_base ) * NUM_TX_DESC;
833         adapter->rx_ring_size = sizeof ( *adapter->rx_base ) * NUM_RX_DESC;
834
835         /* Fix up PCI device */
836         adjust_pci_device ( pdev );
837
838         err = -EIO;
839
840         mmio_start = pci_bar_start ( pdev, PCI_BASE_ADDRESS_0 );
841         mmio_len   = pci_bar_size  ( pdev, PCI_BASE_ADDRESS_0 );
842
843         DBG ( "mmio_start: %#08lx\n", mmio_start );
844         DBG ( "mmio_len: %#08lx\n", mmio_len );
845
846         adapter->hw.hw_addr = ioremap ( mmio_start, mmio_len );
847         DBG ( "adapter->hw.hw_addr: %p\n", adapter->hw.hw_addr );
848
849         if ( ! adapter->hw.hw_addr ) {
850                 DBG ( "err_ioremap\n" );
851                 goto err_ioremap;
852         }
853
854         /* setup adapter struct */
855         err = igbvf_sw_init ( adapter );
856         if (err) {
857                 DBG ( "err_sw_init\n" );
858                 goto err_sw_init;
859         }
860
861         /* reset the controller to put the device in a known good state */
862         err = hw->mac.ops.reset_hw ( hw );
863         if ( err ) {
864                 DBG ("PF still in reset state, assigning new address\n");
865                 netdev->hw_addr[0] = 0x21;
866                 netdev->hw_addr[1] = 0x21;
867                 netdev->hw_addr[2] = 0x21;
868                 netdev->hw_addr[3] = 0x21;
869                 netdev->hw_addr[4] = 0x21;
870                 netdev->hw_addr[5] = 0x21;
871                 netdev->hw_addr[6] = 0x21;
872         } else {
873                 err = hw->mac.ops.read_mac_addr(hw);
874                 if (err) {
875                         DBG ("Error reading MAC address\n");
876                         goto err_hw_init;
877                 }
878                 if ( ! is_valid_ether_addr(adapter->hw.mac.addr) ) {
879                         /* Assign random MAC address */
880                         eth_random_addr(adapter->hw.mac.addr);
881                 }
882         }
883
884         memcpy ( netdev->hw_addr, adapter->hw.mac.addr, ETH_ALEN );
885
886         /* reset the hardware with the new settings */
887         igbvf_reset ( adapter );
888
889         /* let the f/w know that the h/w is now under the control of the
890          * driver. */
891         igbvf_get_hw_control ( adapter );
892
893         /* Mark as link up; we don't yet handle link state */
894         netdev_link_up ( netdev );
895
896         if ( ( err = register_netdev ( netdev ) ) != 0) {
897                 DBG ( "err_register\n" );
898                 goto err_register;
899         }
900
901         DBG ("igbvf_probe_succeeded\n");
902
903         return 0;
904
905 err_register:
906 err_hw_init:
907 err_sw_init:
908         iounmap ( adapter->hw.hw_addr );
909 err_ioremap:
910         netdev_put ( netdev );
911 err_alloc_etherdev:
912         return err;
913 }
914
915 /**
916  * igbvf_remove - Device Removal Routine
917  * @pdev: PCI device information struct
918  *
919  * igbvf_remove is called by the PCI subsystem to alert the driver
920  * that it should release a PCI device.  The could be caused by a
921  * Hot-Plug event, or because the driver is going to be removed from
922  * memory.
923  **/
924 void igbvf_remove ( struct pci_device *pdev )
925 {
926         struct net_device *netdev = pci_get_drvdata ( pdev );
927         struct igbvf_adapter *adapter = netdev_priv ( netdev );
928
929         DBG ( "igbvf_remove\n" );
930
931         if ( adapter->hw.flash_address )
932                 iounmap ( adapter->hw.flash_address );
933         if  ( adapter->hw.hw_addr )
934                 iounmap ( adapter->hw.hw_addr );
935
936         unregister_netdev ( netdev );
937         igbvf_reset  ( adapter );
938         netdev_nullify ( netdev );
939         netdev_put ( netdev );
940 }
941
942 static struct pci_device_id igbvf_pci_tbl[] = {
943         PCI_ROM(0x8086, 0x10CA, "igbvf", "E1000_DEV_ID_82576_VF", 0),
944         PCI_ROM(0x8086, 0x1520, "i350vf", "E1000_DEV_ID_I350_VF", 0),
945 };
946
947
948 struct pci_driver igbvf_driver __pci_driver = {
949         .ids = igbvf_pci_tbl,
950         .id_count = (sizeof(igbvf_pci_tbl) / sizeof(igbvf_pci_tbl[0])),
951         .probe = igbvf_probe,
952         .remove = igbvf_remove,
953 };