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/pmap.h>
38#include <machine/vmparam.h>
39#include <machine/pc/bios.h>
40#include <dev/isci/scil/scu_bios_definitions.h>
41
42struct pcir_header
43{
44	uint32_t	signature;
45	uint16_t	vendor_id;
46	uint16_t	device_id;
47	uint16_t	reserved;
48	uint16_t	struct_length;
49	uint8_t		struct_revision;
50	uint8_t		cc_interface;
51	uint8_t		cc_subclass;
52	uint8_t		cc_baseclass;
53	uint16_t	image_length;
54	uint16_t	code_revision;
55	uint8_t		code_type;
56	uint8_t		indicator;
57	uint16_t	reserved1;
58};
59
60struct rom_header
61{
62	uint8_t		signature_byte[2];
63	uint8_t		rom_length;
64	uint8_t		jmp_code;
65	uint16_t	entry_address;
66	uint8_t		reserved[0x12];
67	uint16_t	pcir_pointer;
68	uint16_t	pnp_pointer;
69};
70
71struct oem_parameters_table
72{
73	uint8_t		signature[4];  /* "$OEM" */
74	struct revision
75	{
76		uint16_t	major:8;  /* bits [7:0] */
77		uint16_t	minor:8;  /* bits [8:15] */
78	} revision;
79
80	uint16_t	length;
81	uint8_t		checksum;
82	uint8_t		reserved1;
83	uint16_t	reserved2;
84	uint8_t		data[1];
85};
86
87void
88isci_get_oem_parameters(struct isci_softc *isci)
89{
90	uint32_t OROM_PHYSICAL_ADDRESS_START = 0xC0000;
91	uint32_t OROM_SEARCH_LENGTH = 0x30000;
92	uint16_t OROM_SIGNATURE = 0xAA55;
93	uint32_t OROM_SIZE = 512;
94	uint8_t *orom_start =
95	    (uint8_t *)BIOS_PADDRTOVADDR(OROM_PHYSICAL_ADDRESS_START);
96	uint32_t offset = 0;
97
98	while (offset < OROM_SEARCH_LENGTH) {
99
100		/* Look for the OROM signature at the beginning of every
101		 *  512-byte block in the OROM region
102		 */
103		if (*(uint16_t*)(orom_start + offset) == OROM_SIGNATURE) {
104			uint32_t *rom;
105			struct rom_header *rom_header;
106			struct pcir_header *pcir_header;
107			uint16_t vendor_id = isci->pci_common_header.vendor_id;
108			uint16_t device_id = isci->pci_common_header.device_id;
109
110			rom = (uint32_t *)(orom_start + offset);
111			rom_header = (struct rom_header *)rom;
112			pcir_header = (struct pcir_header *)
113			    ((uint8_t*)rom + rom_header->pcir_pointer);
114
115			/* OROM signature was found.  Now check if the PCI
116			 *  device and vendor IDs match.
117			 */
118			if (pcir_header->vendor_id == vendor_id &&
119			    pcir_header->device_id == device_id)
120			{
121				/* OROM for this PCI device was found.  Search
122				 *  this 512-byte block for the $OEM string,
123				 *  which will mark the beginning of the OEM
124				 *  parameter block.
125				 */
126				uint8_t oem_sig[4] = {'$', 'O', 'E', 'M'};
127				int dword_index;
128
129				for (dword_index = 0;
130				    dword_index < OROM_SIZE/sizeof(uint32_t);
131				    dword_index++)
132					if (rom[dword_index] == *(uint32_t *)oem_sig) {
133						/* $OEM signature string was found.  Now copy the OEM parameter block
134						 *  into the struct ISCI_CONTROLLER objects.  After the controllers are
135						 *  constructed, we will pass this OEM parameter data to the SCI core
136						 *  controller.
137						 */
138						struct oem_parameters_table *oem =
139							(struct oem_parameters_table *)&rom[dword_index];
140						SCI_BIOS_OEM_PARAM_BLOCK_T *oem_data =
141							(SCI_BIOS_OEM_PARAM_BLOCK_T *)oem->data;
142						int index;
143
144						isci->oem_parameters_found = TRUE;
145						isci_log_message(1, "ISCI", "oem_data->header.num_elements = %d\n",
146						    oem_data->header.num_elements);
147
148						for (index = 0; index < oem_data->header.num_elements; index++)
149						{
150							memcpy(&isci->controllers[index].oem_parameters.sds1,
151							       &oem_data->controller_element[index],
152							       sizeof(SCIC_SDS_OEM_PARAMETERS_T));
153
154							isci_log_message(1, "ISCI", "OEM Parameter Data for controller %d\n",
155							    index);
156
157							for (int i = 0; i < sizeof(SCIC_SDS_OEM_PARAMETERS_T); i++) {
158								uint8_t val = ((uint8_t *)&oem_data->controller_element[index])[i];
159								isci_log_message(1, "ISCI", "%02x ", val);
160							}
161							isci_log_message(1, "ISCI", "\n");
162							isci->controllers[index].oem_parameters_version = oem_data->header.version;
163						}
164					}
165
166				/* No need to continue searching for another
167				 *  OROM that matches this PCI device, so return
168				 *  immediately.
169				 */
170				return;
171			}
172		}
173
174		offset += OROM_SIZE;
175	}
176}
177