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