Add qemu 2.4.0
[kvmfornfv.git] / qemu / roms / ipxe / src / tests / bofm_test.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 <stdio.h>
24 #include <string.h>
25 #include <ipxe/uaccess.h>
26 #include <ipxe/init.h>
27 #include <ipxe/pci.h>
28 #include <ipxe/ethernet.h>
29 #include <ipxe/bofm.h>
30
31 /** @file
32  *
33  * IBM BladeCenter Open Fabric Manager (BOFM) tests
34  *
35  */
36
37 /** Harvest test table */
38 static struct {
39         struct bofm_global_header header;
40         struct bofm_section_header en_header;
41         struct bofm_en en;
42         struct bofm_section_header done;
43 } __attribute__ (( packed )) bofmtab_harvest = {
44         .header = {
45                 .magic = BOFM_IOAA_MAGIC,
46                 .action = BOFM_ACTION_HVST,
47                 .version = 0x01,
48                 .level = 0x01,
49                 .length = sizeof ( bofmtab_harvest ),
50                 .profile = "Harvest test profile",
51         },
52         .en_header = {
53                 .magic = BOFM_EN_MAGIC,
54                 .length = sizeof ( bofmtab_harvest.en ),
55         },
56         .en = {
57                 .options = ( BOFM_EN_MAP_PFA | BOFM_EN_USAGE_HARVEST |
58                              BOFM_EN_RQ_HVST_ACTIVE ),
59                 .mport = 1,
60         },
61         .done = {
62                 .magic = BOFM_DONE_MAGIC,
63         },
64 };
65
66 /** Update test table */
67 static struct {
68         struct bofm_global_header header;
69         struct bofm_section_header en_header;
70         struct bofm_en en;
71         struct bofm_section_header done;
72 } __attribute__ (( packed )) bofmtab_update = {
73         .header = {
74                 .magic = BOFM_IOAA_MAGIC,
75                 .action = BOFM_ACTION_UPDT,
76                 .version = 0x01,
77                 .level = 0x01,
78                 .length = sizeof ( bofmtab_update ),
79                 .profile = "Update test profile",
80         },
81         .en_header = {
82                 .magic = BOFM_EN_MAGIC,
83                 .length = sizeof ( bofmtab_update.en ),
84         },
85         .en = {
86                 .options = ( BOFM_EN_MAP_PFA | BOFM_EN_EN_A |
87                              BOFM_EN_USAGE_ENTRY ),
88                 .mport = 1,
89                 .mac_a = { 0x02, 0x00, 0x69, 0x50, 0x58, 0x45 },
90         },
91         .done = {
92                 .magic = BOFM_DONE_MAGIC,
93         },
94 };
95
96 /**
97  * Perform BOFM test
98  *
99  * @v pci               PCI device
100  */
101 void bofm_test ( struct pci_device *pci ) {
102         int bofmrc;
103
104         printf ( "BOFMTEST using " PCI_FMT "\n", PCI_ARGS ( pci ) );
105
106         /* Perform harvest test */
107         printf ( "BOFMTEST performing harvest\n" );
108         bofmtab_harvest.en.busdevfn = pci->busdevfn;
109         DBG_HDA ( 0, &bofmtab_harvest, sizeof ( bofmtab_harvest ) );
110         bofmrc = bofm ( virt_to_user ( &bofmtab_harvest ), pci );
111         printf ( "BOFMTEST harvest result %08x\n", bofmrc );
112         if ( bofmtab_harvest.en.options & BOFM_EN_HVST ) {
113                 printf ( "BOFMTEST harvested MAC address %s\n",
114                          eth_ntoa ( &bofmtab_harvest.en.mac_a ) );
115         } else {
116                 printf ( "BOFMTEST failed to harvest a MAC address\n" );
117         }
118         DBG_HDA ( 0, &bofmtab_harvest, sizeof ( bofmtab_harvest ) );
119
120         /* Perform update test */
121         printf ( "BOFMTEST performing update\n" );
122         bofmtab_update.en.busdevfn = pci->busdevfn;
123         DBG_HDA ( 0, &bofmtab_update, sizeof ( bofmtab_update ) );
124         bofmrc = bofm ( virt_to_user ( &bofmtab_update ), pci );
125         printf ( "BOFMTEST update result %08x\n", bofmrc );
126         if ( bofmtab_update.en.options & BOFM_EN_CSM_SUCCESS ) {
127                 printf ( "BOFMTEST updated MAC address to %s\n",
128                          eth_ntoa ( &bofmtab_update.en.mac_a ) );
129         } else {
130                 printf ( "BOFMTEST failed to update MAC address\n" );
131         }
132         DBG_HDA ( 0, &bofmtab_update, sizeof ( bofmtab_update ) );
133 }
134
135 /**
136  * Perform BOFM test at initialisation time
137  *
138  */
139 static void bofm_test_init ( void ) {
140         struct pci_device pci;
141         int busdevfn = -1;
142         int rc;
143
144         /* Uncomment the following line and specify the correct PCI
145          * bus:dev.fn address in order to perform a BOFM test at
146          * initialisation time.
147          */
148         // busdevfn = PCI_BUSDEVFN ( <bus>, <dev>, <fn> );
149
150         /* Skip test if no PCI bus:dev.fn is defined */
151         if ( busdevfn < 0 )
152                 return;
153
154         /* Initialise PCI device */
155         memset ( &pci, 0, sizeof ( pci ) );
156         pci_init ( &pci, busdevfn );
157         if ( ( rc = pci_read_config ( &pci ) ) != 0 ) {
158                 printf ( "BOFMTEST could not create " PCI_FMT " device: %s\n",
159                          PCI_ARGS ( &pci ), strerror ( rc ) );
160                 return;
161         }
162
163         /* Perform test */
164         bofm_test ( &pci );
165 }
166
167 /** BOFM test initialisation function */
168 struct init_fn bofm_test_init_fn __init_fn ( INIT_NORMAL ) = {
169         .initialise = bofm_test_init,
170 };