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