Add qemu 2.4.0
[kvmfornfv.git] / qemu / roms / ipxe / src / drivers / net / vmxnet3.c
1 /*
2  * Copyright (C) 2011 Michael Brown <mbrown@fensystems.co.uk>.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 2 of the
7  * License, or any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17  * 02110-1301, USA.
18  */
19
20 FILE_LICENCE ( GPL2_OR_LATER );
21
22 #include <stdint.h>
23 #include <errno.h>
24 #include <assert.h>
25 #include <byteswap.h>
26 #include <ipxe/pci.h>
27 #include <ipxe/io.h>
28 #include <ipxe/malloc.h>
29 #include <ipxe/profile.h>
30 #include <ipxe/iobuf.h>
31 #include <ipxe/netdevice.h>
32 #include <ipxe/if_ether.h>
33 #include <ipxe/ethernet.h>
34 #include "vmxnet3.h"
35
36 /**
37  * @file
38  *
39  * VMware vmxnet3 virtual NIC driver
40  *
41  */
42
43 /** VM command profiler */
44 static struct profiler vmxnet3_vm_command_profiler __profiler =
45         { .name = "vmxnet3.vm_command" };
46
47 /** VM transmit profiler */
48 static struct profiler vmxnet3_vm_tx_profiler __profiler =
49         { .name = "vmxnet3.vm_tx" };
50
51 /** VM receive refill profiler */
52 static struct profiler vmxnet3_vm_refill_profiler __profiler =
53         { .name = "vmxnet3.vm_refill" };
54
55 /** VM event profiler */
56 static struct profiler vmxnet3_vm_event_profiler __profiler =
57         { .name = "vmxnet3.vm_event" };
58
59 /**
60  * Issue command
61  *
62  * @v vmxnet            vmxnet3 NIC
63  * @v command           Command to issue
64  * @ret result          Command result
65  */
66 static inline uint32_t vmxnet3_command ( struct vmxnet3_nic *vmxnet,
67                                          uint32_t command ) {
68         uint32_t result;
69
70         /* Issue command */
71         profile_start ( &vmxnet3_vm_command_profiler );
72         writel ( command, ( vmxnet->vd + VMXNET3_VD_CMD ) );
73         result = readl ( vmxnet->vd + VMXNET3_VD_CMD );
74         profile_stop ( &vmxnet3_vm_command_profiler );
75         profile_exclude ( &vmxnet3_vm_command_profiler );
76
77         return result;
78 }
79
80 /**
81  * Transmit packet
82  *
83  * @v netdev            Network device
84  * @v iobuf             I/O buffer
85  * @ret rc              Return status code
86  */
87 static int vmxnet3_transmit ( struct net_device *netdev,
88                               struct io_buffer *iobuf ) {
89         struct vmxnet3_nic *vmxnet = netdev_priv ( netdev );
90         struct vmxnet3_tx_desc *tx_desc;
91         unsigned int desc_idx;
92         unsigned int generation;
93
94         /* Check that we have a free transmit descriptor */
95         desc_idx = ( vmxnet->count.tx_prod % VMXNET3_NUM_TX_DESC );
96         generation = ( ( vmxnet->count.tx_prod & VMXNET3_NUM_TX_DESC ) ?
97                        0 : cpu_to_le32 ( VMXNET3_TXF_GEN ) );
98         if ( vmxnet->tx_iobuf[desc_idx] ) {
99                 DBGC ( vmxnet, "VMXNET3 %p out of transmit descriptors\n",
100                        vmxnet );
101                 return -ENOBUFS;
102         }
103
104         /* Increment producer counter */
105         vmxnet->count.tx_prod++;
106
107         /* Store I/O buffer for later completion */
108         vmxnet->tx_iobuf[desc_idx] = iobuf;
109
110         /* Populate transmit descriptor */
111         tx_desc = &vmxnet->dma->tx_desc[desc_idx];
112         tx_desc->address = cpu_to_le64 ( virt_to_bus ( iobuf->data ) );
113         tx_desc->flags[0] = ( generation | cpu_to_le32 ( iob_len ( iobuf ) ) );
114         tx_desc->flags[1] = cpu_to_le32 ( VMXNET3_TXF_CQ | VMXNET3_TXF_EOP );
115
116         /* Hand over descriptor to NIC */
117         wmb();
118         profile_start ( &vmxnet3_vm_tx_profiler );
119         writel ( ( vmxnet->count.tx_prod % VMXNET3_NUM_TX_DESC ),
120                  ( vmxnet->pt + VMXNET3_PT_TXPROD ) );
121         profile_stop ( &vmxnet3_vm_tx_profiler );
122         profile_exclude ( &vmxnet3_vm_tx_profiler );
123
124         return 0;
125 }
126
127 /**
128  * Poll for completed transmissions
129  *
130  * @v netdev            Network device
131  */
132 static void vmxnet3_poll_tx ( struct net_device *netdev ) {
133         struct vmxnet3_nic *vmxnet = netdev_priv ( netdev );
134         struct vmxnet3_tx_comp *tx_comp;
135         struct io_buffer *iobuf;
136         unsigned int comp_idx;
137         unsigned int desc_idx;
138         unsigned int generation;
139
140         while ( 1 ) {
141
142                 /* Look for completed descriptors */
143                 comp_idx = ( vmxnet->count.tx_cons % VMXNET3_NUM_TX_COMP );
144                 generation = ( ( vmxnet->count.tx_cons & VMXNET3_NUM_TX_COMP ) ?
145                                0 : cpu_to_le32 ( VMXNET3_TXCF_GEN ) );
146                 tx_comp = &vmxnet->dma->tx_comp[comp_idx];
147                 if ( generation != ( tx_comp->flags &
148                                      cpu_to_le32 ( VMXNET3_TXCF_GEN ) ) ) {
149                         break;
150                 }
151
152                 /* Increment consumer counter */
153                 vmxnet->count.tx_cons++;
154
155                 /* Locate corresponding transmit descriptor */
156                 desc_idx = ( le32_to_cpu ( tx_comp->index ) %
157                              VMXNET3_NUM_TX_DESC );
158                 iobuf = vmxnet->tx_iobuf[desc_idx];
159                 if ( ! iobuf ) {
160                         DBGC ( vmxnet, "VMXNET3 %p completed on empty transmit "
161                                "buffer %#x/%#x\n", vmxnet, comp_idx, desc_idx );
162                         netdev_tx_err ( netdev, NULL, -ENOTTY );
163                         continue;
164                 }
165
166                 /* Remove I/O buffer from transmit queue */
167                 vmxnet->tx_iobuf[desc_idx] = NULL;
168
169                 /* Report transmission completion to network layer */
170                 DBGC2 ( vmxnet, "VMXNET3 %p completed TX %#x/%#x (len %#zx)\n",
171                         vmxnet, comp_idx, desc_idx, iob_len ( iobuf ) );
172                 netdev_tx_complete ( netdev, iobuf );
173         }
174 }
175
176 /**
177  * Flush any uncompleted transmit buffers
178  *
179  * @v netdev            Network device
180  */
181 static void vmxnet3_flush_tx ( struct net_device *netdev ) {
182         struct vmxnet3_nic *vmxnet = netdev_priv ( netdev );
183         unsigned int i;
184
185         for ( i = 0 ; i < VMXNET3_NUM_TX_DESC ; i++ ) {
186                 if ( vmxnet->tx_iobuf[i] ) {
187                         netdev_tx_complete_err ( netdev, vmxnet->tx_iobuf[i],
188                                                  -ECANCELED );
189                         vmxnet->tx_iobuf[i] = NULL;
190                 }
191         }
192 }
193
194 /**
195  * Refill receive ring
196  *
197  * @v netdev            Network device
198  */
199 static void vmxnet3_refill_rx ( struct net_device *netdev ) {
200         struct vmxnet3_nic *vmxnet = netdev_priv ( netdev );
201         struct vmxnet3_rx_desc *rx_desc;
202         struct io_buffer *iobuf;
203         unsigned int orig_rx_prod = vmxnet->count.rx_prod;
204         unsigned int desc_idx;
205         unsigned int generation;
206
207         /* Fill receive ring to specified fill level */
208         while ( vmxnet->count.rx_fill < VMXNET3_RX_FILL ) {
209
210                 /* Locate receive descriptor */
211                 desc_idx = ( vmxnet->count.rx_prod % VMXNET3_NUM_RX_DESC );
212                 generation = ( ( vmxnet->count.rx_prod & VMXNET3_NUM_RX_DESC ) ?
213                                0 : cpu_to_le32 ( VMXNET3_RXF_GEN ) );
214                 assert ( vmxnet->rx_iobuf[desc_idx] == NULL );
215
216                 /* Allocate I/O buffer */
217                 iobuf = alloc_iob ( VMXNET3_MTU + NET_IP_ALIGN );
218                 if ( ! iobuf ) {
219                         /* Non-fatal low memory condition */
220                         break;
221                 }
222                 iob_reserve ( iobuf, NET_IP_ALIGN );
223
224                 /* Increment producer counter and fill level */
225                 vmxnet->count.rx_prod++;
226                 vmxnet->count.rx_fill++;
227
228                 /* Store I/O buffer for later completion */
229                 vmxnet->rx_iobuf[desc_idx] = iobuf;
230
231                 /* Populate receive descriptor */
232                 rx_desc = &vmxnet->dma->rx_desc[desc_idx];
233                 rx_desc->address = cpu_to_le64 ( virt_to_bus ( iobuf->data ) );
234                 rx_desc->flags = ( generation | cpu_to_le32 ( VMXNET3_MTU ) );
235
236         }
237
238         /* Hand over any new descriptors to NIC */
239         if ( vmxnet->count.rx_prod != orig_rx_prod ) {
240                 wmb();
241                 profile_start ( &vmxnet3_vm_refill_profiler );
242                 writel ( ( vmxnet->count.rx_prod % VMXNET3_NUM_RX_DESC ),
243                          ( vmxnet->pt + VMXNET3_PT_RXPROD ) );
244                 profile_stop ( &vmxnet3_vm_refill_profiler );
245                 profile_exclude ( &vmxnet3_vm_refill_profiler );
246         }
247 }
248
249 /**
250  * Poll for received packets
251  *
252  * @v netdev            Network device
253  */
254 static void vmxnet3_poll_rx ( struct net_device *netdev ) {
255         struct vmxnet3_nic *vmxnet = netdev_priv ( netdev );
256         struct vmxnet3_rx_comp *rx_comp;
257         struct io_buffer *iobuf;
258         unsigned int comp_idx;
259         unsigned int desc_idx;
260         unsigned int generation;
261         size_t len;
262
263         while ( 1 ) {
264
265                 /* Look for completed descriptors */
266                 comp_idx = ( vmxnet->count.rx_cons % VMXNET3_NUM_RX_COMP );
267                 generation = ( ( vmxnet->count.rx_cons & VMXNET3_NUM_RX_COMP ) ?
268                                0 : cpu_to_le32 ( VMXNET3_RXCF_GEN ) );
269                 rx_comp = &vmxnet->dma->rx_comp[comp_idx];
270                 if ( generation != ( rx_comp->flags &
271                                      cpu_to_le32 ( VMXNET3_RXCF_GEN ) ) ) {
272                         break;
273                 }
274
275                 /* Increment consumer counter */
276                 vmxnet->count.rx_cons++;
277
278                 /* Locate corresponding receive descriptor */
279                 desc_idx = ( le32_to_cpu ( rx_comp->index ) %
280                              VMXNET3_NUM_RX_DESC );
281                 iobuf = vmxnet->rx_iobuf[desc_idx];
282                 if ( ! iobuf ) {
283                         DBGC ( vmxnet, "VMXNET3 %p completed on empty receive "
284                                "buffer %#x/%#x\n", vmxnet, comp_idx, desc_idx );
285                         netdev_rx_err ( netdev, NULL, -ENOTTY );
286                         continue;
287                 }
288
289                 /* Remove I/O buffer from receive queue */
290                 vmxnet->rx_iobuf[desc_idx] = NULL;
291                 vmxnet->count.rx_fill--;
292
293                 /* Deliver packet to network layer */
294                 len = ( le32_to_cpu ( rx_comp->len ) &
295                         ( VMXNET3_MAX_PACKET_LEN - 1 ) );
296                 DBGC2 ( vmxnet, "VMXNET3 %p completed RX %#x/%#x (len %#zx)\n",
297                         vmxnet, comp_idx, desc_idx, len );
298                 iob_put ( iobuf, len );
299                 netdev_rx ( netdev, iobuf );
300         }
301 }
302
303 /**
304  * Flush any uncompleted receive buffers
305  *
306  * @v netdev            Network device
307  */
308 static void vmxnet3_flush_rx ( struct net_device *netdev ) {
309         struct vmxnet3_nic *vmxnet = netdev_priv ( netdev );
310         struct io_buffer *iobuf;
311         unsigned int i;
312
313         for ( i = 0 ; i < VMXNET3_NUM_RX_DESC ; i++ ) {
314                 if ( ( iobuf = vmxnet->rx_iobuf[i] ) != NULL ) {
315                         netdev_rx_err ( netdev, iobuf, -ECANCELED );
316                         vmxnet->rx_iobuf[i] = NULL;
317                 }
318         }
319 }
320
321 /**
322  * Check link state
323  *
324  * @v netdev            Network device
325  */
326 static void vmxnet3_check_link ( struct net_device *netdev ) {
327         struct vmxnet3_nic *vmxnet = netdev_priv ( netdev );
328         uint32_t state;
329         int link_up;
330         unsigned int link_speed;
331
332         /* Get link state */
333         state = vmxnet3_command ( vmxnet, VMXNET3_CMD_GET_LINK );
334         link_up = ( state & 1 );
335         link_speed = ( state >> 16 );
336
337         /* Report link state to network device */
338         if ( link_up ) {
339                 DBGC ( vmxnet, "VMXNET3 %p link is up at %d Mbps\n",
340                        vmxnet, link_speed );
341                 netdev_link_up ( netdev );
342         } else {
343                 DBGC ( vmxnet, "VMXNET3 %p link is down\n", vmxnet );
344                 netdev_link_down ( netdev );
345         }
346 }
347
348 /**
349  * Poll for events
350  *
351  * @v netdev            Network device
352  */
353 static void vmxnet3_poll_events ( struct net_device *netdev ) {
354         struct vmxnet3_nic *vmxnet = netdev_priv ( netdev );
355         uint32_t events;
356
357         /* Do nothing unless there are events to process */
358         if ( ! vmxnet->dma->shared.ecr )
359                 return;
360         events = le32_to_cpu ( vmxnet->dma->shared.ecr );
361
362         /* Acknowledge these events */
363         profile_start ( &vmxnet3_vm_event_profiler );
364         writel ( events, ( vmxnet->vd + VMXNET3_VD_ECR ) );
365         profile_stop ( &vmxnet3_vm_event_profiler );
366         profile_exclude ( &vmxnet3_vm_event_profiler );
367
368         /* Check for link state change */
369         if ( events & VMXNET3_ECR_LINK ) {
370                 vmxnet3_check_link ( netdev );
371                 events &= ~VMXNET3_ECR_LINK;
372         }
373
374         /* Check for queue errors */
375         if ( events & ( VMXNET3_ECR_TQERR | VMXNET3_ECR_RQERR ) ) {
376                 vmxnet3_command ( vmxnet, VMXNET3_CMD_GET_QUEUE_STATUS );
377                 DBGC ( vmxnet, "VMXNET3 %p queue error status (TX %08x, RX "
378                        "%08x)\n", vmxnet,
379                        le32_to_cpu ( vmxnet->dma->queues.tx.status.error ),
380                        le32_to_cpu ( vmxnet->dma->queues.rx.status.error ) );
381                 /* Report errors to allow for visibility via "ifstat" */
382                 if ( events & VMXNET3_ECR_TQERR )
383                         netdev_tx_err ( netdev, NULL, -EPIPE );
384                 if ( events & VMXNET3_ECR_RQERR )
385                         netdev_rx_err ( netdev, NULL, -EPIPE );
386                 events &= ~( VMXNET3_ECR_TQERR | VMXNET3_ECR_RQERR );
387         }
388
389         /* Check for unknown events */
390         if ( events ) {
391                 DBGC ( vmxnet, "VMXNET3 %p unknown events %08x\n",
392                        vmxnet, events );
393                 /* Report error to allow for visibility via "ifstat" */
394                 netdev_rx_err ( netdev, NULL, -ENODEV );
395         }
396 }
397
398 /**
399  * Poll network device
400  *
401  * @v netdev            Network device
402  */
403 static void vmxnet3_poll ( struct net_device *netdev ) {
404
405         vmxnet3_poll_events ( netdev );
406         vmxnet3_poll_tx ( netdev );
407         vmxnet3_poll_rx ( netdev );
408         vmxnet3_refill_rx ( netdev );
409 }
410
411 /**
412  * Enable/disable interrupts
413  *
414  * @v netdev            Network device
415  * @v enable            Interrupts should be enabled
416  */
417 static void vmxnet3_irq ( struct net_device *netdev, int enable ) {
418         struct vmxnet3_nic *vmxnet = netdev_priv ( netdev );
419
420         DBGC ( vmxnet, "VMXNET3 %p %s IRQ not implemented\n",
421                vmxnet, ( enable ? "enable" : "disable" ) );
422 }
423
424 /**
425  * Set MAC address
426  *
427  * @v vmxnet            vmxnet3 NIC
428  * @v ll_addr           Link-layer address to set
429  */
430 static void vmxnet3_set_ll_addr ( struct vmxnet3_nic *vmxnet,
431                                   const void *ll_addr ) {
432         struct {
433                 uint32_t low;
434                 uint32_t high;
435         } __attribute__ (( packed )) mac;
436
437         memset ( &mac, 0, sizeof ( mac ) );
438         memcpy ( &mac, ll_addr, ETH_ALEN );
439         writel ( cpu_to_le32 ( mac.low ), ( vmxnet->vd + VMXNET3_VD_MACL ) );
440         writel ( cpu_to_le32 ( mac.high ), ( vmxnet->vd + VMXNET3_VD_MACH ) );
441 }
442
443 /**
444  * Open NIC
445  *
446  * @v netdev            Network device
447  * @ret rc              Return status code
448  */
449 static int vmxnet3_open ( struct net_device *netdev ) {
450         struct vmxnet3_nic *vmxnet = netdev_priv ( netdev );
451         struct vmxnet3_shared *shared;
452         struct vmxnet3_queues *queues;
453         uint64_t shared_bus;
454         uint64_t queues_bus;
455         uint32_t status;
456         int rc;
457
458         /* Allocate DMA areas */
459         vmxnet->dma = malloc_dma ( sizeof ( *vmxnet->dma ), VMXNET3_DMA_ALIGN );
460         if ( ! vmxnet->dma ) {
461                 DBGC ( vmxnet, "VMXNET3 %p could not allocate DMA area\n",
462                        vmxnet );
463                 rc = -ENOMEM;
464                 goto err_alloc_dma;
465         }
466         memset ( vmxnet->dma, 0, sizeof ( *vmxnet->dma ) );
467
468         /* Populate queue descriptors */
469         queues = &vmxnet->dma->queues;
470         queues->tx.cfg.desc_address =
471                 cpu_to_le64 ( virt_to_bus ( &vmxnet->dma->tx_desc ) );
472         queues->tx.cfg.comp_address =
473                 cpu_to_le64 ( virt_to_bus ( &vmxnet->dma->tx_comp ) );
474         queues->tx.cfg.num_desc = cpu_to_le32 ( VMXNET3_NUM_TX_DESC );
475         queues->tx.cfg.num_comp = cpu_to_le32 ( VMXNET3_NUM_TX_COMP );
476         queues->rx.cfg.desc_address[0] =
477                 cpu_to_le64 ( virt_to_bus ( &vmxnet->dma->rx_desc ) );
478         queues->rx.cfg.comp_address =
479                 cpu_to_le64 ( virt_to_bus ( &vmxnet->dma->rx_comp ) );
480         queues->rx.cfg.num_desc[0] = cpu_to_le32 ( VMXNET3_NUM_RX_DESC );
481         queues->rx.cfg.num_comp = cpu_to_le32 ( VMXNET3_NUM_RX_COMP );
482         queues_bus = virt_to_bus ( queues );
483         DBGC ( vmxnet, "VMXNET3 %p queue descriptors at %08llx+%zx\n",
484                vmxnet, queues_bus, sizeof ( *queues ) );
485
486         /* Populate shared area */
487         shared = &vmxnet->dma->shared;
488         shared->magic = cpu_to_le32 ( VMXNET3_SHARED_MAGIC );
489         shared->misc.version = cpu_to_le32 ( VMXNET3_VERSION_MAGIC );
490         shared->misc.version_support = cpu_to_le32 ( VMXNET3_VERSION_SELECT );
491         shared->misc.upt_version_support =
492                 cpu_to_le32 ( VMXNET3_UPT_VERSION_SELECT );
493         shared->misc.queue_desc_address = cpu_to_le64 ( queues_bus );
494         shared->misc.queue_desc_len = cpu_to_le32 ( sizeof ( *queues ) );
495         shared->misc.mtu = cpu_to_le32 ( VMXNET3_MTU );
496         shared->misc.num_tx_queues = 1;
497         shared->misc.num_rx_queues = 1;
498         shared->interrupt.num_intrs = 1;
499         shared->interrupt.control = cpu_to_le32 ( VMXNET3_IC_DISABLE_ALL );
500         shared->rx_filter.mode = cpu_to_le32 ( VMXNET3_RXM_UCAST |
501                                                VMXNET3_RXM_BCAST |
502                                                VMXNET3_RXM_ALL_MULTI );
503         shared_bus = virt_to_bus ( shared );
504         DBGC ( vmxnet, "VMXNET3 %p shared area at %08llx+%zx\n",
505                vmxnet, shared_bus, sizeof ( *shared ) );
506
507         /* Zero counters */
508         memset ( &vmxnet->count, 0, sizeof ( vmxnet->count ) );
509
510         /* Set MAC address */
511         vmxnet3_set_ll_addr ( vmxnet, &netdev->ll_addr );
512
513         /* Pass shared area to device */
514         writel ( ( shared_bus >> 0 ), ( vmxnet->vd + VMXNET3_VD_DSAL ) );
515         writel ( ( shared_bus >> 32 ), ( vmxnet->vd + VMXNET3_VD_DSAH ) );
516
517         /* Activate device */
518         if ( ( status = vmxnet3_command ( vmxnet,
519                                           VMXNET3_CMD_ACTIVATE_DEV ) ) != 0 ) {
520                 DBGC ( vmxnet, "VMXNET3 %p could not activate (status %#x)\n",
521                        vmxnet, status );
522                 rc = -EIO;
523                 goto err_activate;
524         }
525
526         /* Fill receive ring */
527         vmxnet3_refill_rx ( netdev );
528
529         return 0;
530
531         vmxnet3_command ( vmxnet, VMXNET3_CMD_QUIESCE_DEV );
532         vmxnet3_command ( vmxnet, VMXNET3_CMD_RESET_DEV );
533  err_activate:
534         vmxnet3_flush_tx ( netdev );
535         vmxnet3_flush_rx ( netdev );
536         free_dma ( vmxnet->dma, sizeof ( *vmxnet->dma ) );
537  err_alloc_dma:
538         return rc;
539 }
540
541 /**
542  * Close NIC
543  *
544  * @v netdev            Network device
545  */
546 static void vmxnet3_close ( struct net_device *netdev ) {
547         struct vmxnet3_nic *vmxnet = netdev_priv ( netdev );
548
549         vmxnet3_command ( vmxnet, VMXNET3_CMD_QUIESCE_DEV );
550         vmxnet3_command ( vmxnet, VMXNET3_CMD_RESET_DEV );
551         vmxnet3_flush_tx ( netdev );
552         vmxnet3_flush_rx ( netdev );
553         free_dma ( vmxnet->dma, sizeof ( *vmxnet->dma ) );
554 }
555
556 /** vmxnet3 net device operations */
557 static struct net_device_operations vmxnet3_operations = {
558         .open           = vmxnet3_open,
559         .close          = vmxnet3_close,
560         .transmit       = vmxnet3_transmit,
561         .poll           = vmxnet3_poll,
562         .irq            = vmxnet3_irq,
563 };
564
565 /**
566  * Check version
567  *
568  * @v vmxnet            vmxnet3 NIC
569  * @ret rc              Return status code
570  */
571 static int vmxnet3_check_version ( struct vmxnet3_nic *vmxnet ) {
572         uint32_t version;
573         uint32_t upt_version;
574
575         /* Read version */
576         version = readl ( vmxnet->vd + VMXNET3_VD_VRRS );
577         upt_version = readl ( vmxnet->vd + VMXNET3_VD_UVRS );
578         DBGC ( vmxnet, "VMXNET3 %p is version %d (UPT version %d)\n",
579                vmxnet, version, upt_version );
580
581         /* Inform NIC of driver version */
582         writel ( VMXNET3_VERSION_SELECT, ( vmxnet->vd + VMXNET3_VD_VRRS ) );
583         writel ( VMXNET3_UPT_VERSION_SELECT, ( vmxnet->vd + VMXNET3_VD_UVRS ) );
584
585         return 0;
586 }
587
588 /**
589  * Get permanent MAC address
590  *
591  * @v vmxnet            vmxnet3 NIC
592  * @v hw_addr           Hardware address to fill in
593  */
594 static void vmxnet3_get_hw_addr ( struct vmxnet3_nic *vmxnet, void *hw_addr ) {
595         struct {
596                 uint32_t low;
597                 uint32_t high;
598         } __attribute__ (( packed )) mac;
599
600         mac.low = le32_to_cpu ( vmxnet3_command ( vmxnet,
601                                                VMXNET3_CMD_GET_PERM_MAC_LO ) );
602         mac.high = le32_to_cpu ( vmxnet3_command ( vmxnet,
603                                                VMXNET3_CMD_GET_PERM_MAC_HI ) );
604         memcpy ( hw_addr, &mac, ETH_ALEN );
605 }
606
607 /**
608  * Probe PCI device
609  *
610  * @v pci               PCI device
611  * @v id                PCI ID
612  * @ret rc              Return status code
613  */
614 static int vmxnet3_probe ( struct pci_device *pci ) {
615         struct net_device *netdev;
616         struct vmxnet3_nic *vmxnet;
617         int rc;
618
619         /* Allocate network device */
620         netdev = alloc_etherdev ( sizeof ( *vmxnet ) );
621         if ( ! netdev ) {
622                 rc = -ENOMEM;
623                 goto err_alloc_etherdev;
624         }
625         netdev_init ( netdev, &vmxnet3_operations );
626         vmxnet = netdev_priv ( netdev );
627         pci_set_drvdata ( pci, netdev );
628         netdev->dev = &pci->dev;
629         memset ( vmxnet, 0, sizeof ( *vmxnet ) );
630
631         /* Fix up PCI device */
632         adjust_pci_device ( pci );
633
634         /* Map PCI BARs */
635         vmxnet->pt = ioremap ( pci_bar_start ( pci, VMXNET3_PT_BAR ),
636                                VMXNET3_PT_LEN );
637         if ( ! vmxnet->pt ) {
638                 rc = -ENODEV;
639                 goto err_ioremap_pt;
640         }
641         vmxnet->vd = ioremap ( pci_bar_start ( pci, VMXNET3_VD_BAR ),
642                                VMXNET3_VD_LEN );
643         if ( ! vmxnet->vd ) {
644                 rc = -ENODEV;
645                 goto err_ioremap_vd;
646         }
647
648         /* Version check */
649         if ( ( rc = vmxnet3_check_version ( vmxnet ) ) != 0 )
650                 goto err_check_version;
651
652         /* Reset device */
653         if ( ( rc = vmxnet3_command ( vmxnet, VMXNET3_CMD_RESET_DEV ) ) != 0 )
654                 goto err_reset;
655
656         /* Read initial MAC address */
657         vmxnet3_get_hw_addr ( vmxnet, &netdev->hw_addr );
658
659         /* Register network device */
660         if ( ( rc = register_netdev ( netdev ) ) != 0 ) {
661                 DBGC ( vmxnet, "VMXNET3 %p could not register net device: "
662                        "%s\n", vmxnet, strerror ( rc ) );
663                 goto err_register_netdev;
664         }
665
666         /* Get initial link state */
667         vmxnet3_check_link ( netdev );
668
669         return 0;
670
671         unregister_netdev ( netdev );
672  err_register_netdev:
673  err_reset:
674  err_check_version:
675         iounmap ( vmxnet->vd );
676  err_ioremap_vd:
677         iounmap ( vmxnet->pt );
678  err_ioremap_pt:
679         netdev_nullify ( netdev );
680         netdev_put ( netdev );
681  err_alloc_etherdev:
682         return rc;
683 }
684
685 /**
686  * Remove PCI device
687  *
688  * @v pci               PCI device
689  */
690 static void vmxnet3_remove ( struct pci_device *pci ) {
691         struct net_device *netdev = pci_get_drvdata ( pci );
692         struct vmxnet3_nic *vmxnet = netdev_priv ( netdev );
693
694         unregister_netdev ( netdev );
695         iounmap ( vmxnet->vd );
696         iounmap ( vmxnet->pt );
697         netdev_nullify ( netdev );
698         netdev_put ( netdev );
699 }
700
701 /** vmxnet3 PCI IDs */
702 static struct pci_device_id vmxnet3_nics[] = {
703         PCI_ROM ( 0x15ad, 0x07b0, "vmxnet3", "vmxnet3 virtual NIC", 0 ),
704 };
705
706 /** vmxnet3 PCI driver */
707 struct pci_driver vmxnet3_driver __pci_driver = {
708         .ids = vmxnet3_nics,
709         .id_count = ( sizeof ( vmxnet3_nics ) / sizeof ( vmxnet3_nics[0] ) ),
710         .probe = vmxnet3_probe,
711         .remove = vmxnet3_remove,
712 };