1/*-
2 * BSD LICENSE
3 *
4 * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 *   * Redistributions of source code must retain the above copyright
12 *     notice, this list of conditions and the following disclaimer.
13 *   * Redistributions in binary form must reproduce the above copyright
14 *     notice, this list of conditions and the following disclaimer in
15 *     the documentation and/or other materials provided with the
16 *     distribution.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31#include <sys/cdefs.h>
32__FBSDID("$FreeBSD$");
33
34#include <dev/isci/isci.h>
35#include <vm/vm.h>
36#include <vm/pmap.h>
37#include <machine/vmparam.h>
38#include <machine/pc/bios.h>
39#include <dev/isci/scil/scu_bios_definitions.h>
40
41struct pcir_header
42{
43	uint32_t	signature;
44	uint16_t	vendor_id;
45	uint16_t	device_id;
46	uint16_t	reserved;
47	uint16_t	struct_length;
48	uint8_t		struct_revision;
49	uint8_t		cc_interface;
50	uint8_t		cc_subclass;
51	uint8_t		cc_baseclass;
52	uint16_t	image_length;
53	uint16_t	code_revision;
54	uint8_t		code_type;
55	uint8_t		indicator;
56	uint16_t	reserved1;
57};
58
59struct rom_header
60{
61	uint8_t		signature_byte[2];
62	uint8_t		rom_length;
63	uint8_t		jmp_code;
64	uint16_t	entry_address;
65	uint8_t		reserved[0x12];
66	uint16_t	pcir_pointer;
67	uint16_t	pnp_pointer;
68};
69
70struct oem_parameters_table
71{
72	uint8_t		signature[4];  /* "$OEM" */
73	struct revision
74	{
75		uint16_t	major:8;  /* bits [7:0] */
76		uint16_t	minor:8;  /* bits [8:15] */
77	} revision;
78
79	uint16_t	length;
80	uint8_t		checksum;
81	uint8_t		reserved1;
82	uint16_t	reserved2;
83	uint8_t		data[1];
84};
85
86void
87isci_get_oem_parameters(struct isci_softc *isci)
88{
89	uint32_t OROM_PHYSICAL_ADDRESS_START = 0xC0000;
90	uint32_t OROM_SEARCH_LENGTH = 0x30000;
91	uint16_t OROM_SIGNATURE = 0xAA55;
92	uint32_t OROM_SIZE = 512;
93	uint8_t *orom_start =
94	    (uint8_t *)BIOS_PADDRTOVADDR(OROM_PHYSICAL_ADDRESS_START);
95	uint32_t offset = 0;
96
97	while (offset < OROM_SEARCH_LENGTH) {
98
99		/* Look for the OROM signature at the beginning of every
100		 *  512-byte block in the OROM region
101		 */
102		if (*(uint16_t*)(orom_start + offset) == OROM_SIGNATURE) {
103			uint32_t *rom;
104			struct rom_header *rom_header;
105			struct pcir_header *pcir_header;
106			uint16_t vendor_id = isci->pci_common_header.vendor_id;
107			uint16_t device_id = isci->pci_common_header.device_id;
108
109			rom = (uint32_t *)(orom_start + offset);
110			rom_header = (struct rom_header *)rom;
111			pcir_header = (struct pcir_header *)
112			    ((uint8_t*)rom + rom_header->pcir_pointer);
113
114			/* OROM signature was found.  Now check if the PCI
115			 *  device and vendor IDs match.
116			 */
117			if (pcir_header->vendor_id == vendor_id &&
118			    pcir_header->device_id == device_id)
119			{
120				/* OROM for this PCI device was found.  Search
121				 *  this 512-byte block for the $OEM string,
122				 *  which will mark the beginning of the OEM
123				 *  parameter block.
124				 */
125				uint8_t oem_sig[4] = {'$', 'O', 'E', 'M'};
126				int dword_index;
127
128				for (dword_index = 0;
129				    dword_index < OROM_SIZE/sizeof(uint32_t);
130				    dword_index++)
131					if (rom[dword_index] == *(uint32_t *)oem_sig) {
132						/* $OEM signature string was found.  Now copy the OEM parameter block
133						 *  into the struct ISCI_CONTROLLER objects.  After the controllers are
134						 *  constructed, we will pass this OEM parameter data to the SCI core
135						 *  controller.
136						 */
137						struct oem_parameters_table *oem =
138							(struct oem_parameters_table *)&rom[dword_index];
139						SCI_BIOS_OEM_PARAM_BLOCK_T *oem_data =
140							(SCI_BIOS_OEM_PARAM_BLOCK_T *)oem->data;
141						int index;
142
143						isci->oem_parameters_found = TRUE;
144						isci_log_message(1, "ISCI", "oem_data->header.num_elements = %d\n",
145						    oem_data->header.num_elements);
146
147						for (index = 0; index < oem_data->header.num_elements; index++)
148						{
149							memcpy(&isci->controllers[index].oem_parameters.sds1,
150							       &oem_data->controller_element[index],
151							       sizeof(SCIC_SDS_OEM_PARAMETERS_T));
152
153							isci_log_message(1, "ISCI", "OEM Parameter Data for controller %d\n",
154							    index);
155
156							for (int i = 0; i < sizeof(SCIC_SDS_OEM_PARAMETERS_T); i++) {
157								uint8_t val = ((uint8_t *)&oem_data->controller_element[index])[i];
158								isci_log_message(1, "ISCI", "%02x ", val);
159							}
160							isci_log_message(1, "ISCI", "\n");
161							isci->controllers[index].oem_parameters_version = oem_data->header.version;
162						}
163					}
164
165				/* No need to continue searching for another
166				 *  OROM that matches this PCI device, so return
167				 *  immediately.
168				 */
169				return;
170			}
171		}
172
173		offset += OROM_SIZE;
174	}
175}
176