xref: /freebsd/sys/dev/isci/isci.h (revision cfd6422a5217410fbd66f7a7a8a64d9d85e61229)
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  * $FreeBSD$
33  */
34 
35 #ifndef _ISCI_H
36 #define _ISCI_H
37 
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/kernel.h>
41 #include <sys/bus.h>
42 #include <sys/lock.h>
43 #include <sys/mutex.h>
44 #include <sys/types.h>
45 #include <sys/malloc.h>
46 #include <sys/rman.h>
47 
48 #include <machine/bus.h>
49 #include <machine/resource.h>
50 
51 #include <cam/cam.h>
52 #include <cam/cam_ccb.h>
53 #include <cam/cam_sim.h>
54 #include <cam/cam_xpt_sim.h>
55 
56 #include <dev/isci/environment.h>
57 #include <dev/isci/scil/intel_pci.h>
58 
59 #include <dev/isci/scil/sci_types.h>
60 #include <dev/isci/scil/sci_object.h>
61 #include <dev/isci/scil/sci_status.h>
62 #include <dev/isci/scil/sci_pool.h>
63 #include <dev/isci/scil/sci_fast_list.h>
64 
65 #include <dev/isci/scil/sci_controller_constants.h>
66 
67 #include <dev/isci/scil/scic_controller.h>
68 #include <dev/isci/scil/scic_config_parameters.h>
69 
70 #define DEVICE2SOFTC(dev) ((struct isci_softc *) device_get_softc(dev))
71 
72 #define DEVICE_TIMEOUT 1000
73 #define SCI_MAX_TIMERS  32
74 
75 #define ISCI_NUM_PCI_BARS  2
76 #define ISCI_MAX_LUN		 8
77 
78 /* This device cannot DMA across a 4GB boundary */
79 #define	ISCI_DMA_BOUNDARY		((bus_addr_t)((uint64_t)1 << 32))
80 
81 MALLOC_DECLARE(M_ISCI);
82 
83 struct ISCI_TIMER {
84 	struct callout		callout;
85 	SCI_TIMER_CALLBACK_T	callback;
86 	void			*cookie;
87 	BOOL			is_started;
88 };
89 
90 struct ISCI_REMOTE_DEVICE {
91 	uint32_t			index;
92 	struct ISCI_DOMAIN 		*domain;
93 	SCI_REMOTE_DEVICE_HANDLE_T	sci_object;
94 	BOOL				is_resetting;
95 	uint32_t			frozen_lun_mask;
96 	SCI_FAST_LIST_ELEMENT_T		pending_device_reset_element;
97 
98 	/*
99 	 * This queue maintains CCBs that have been returned with
100 	 *  SCI_IO_FAILURE_INVALID_STATE from the SCI layer.  These CCBs
101 	 *  need to be retried, but we cannot return CAM_REQUEUE_REQ because
102 	 *  this status gets passed all the way back up to users of the pass(4)
103 	 *  interface and breaks things like smartctl.  So instead, we queue
104 	 *  these CCBs internally.
105 	 */
106 	TAILQ_HEAD(,ccb_hdr)		queued_ccbs;
107 
108 	/*
109 	 * Marker denoting this remote device needs its first queued ccb to
110 	 *  be retried.
111 	 */
112 	BOOL				release_queued_ccb;
113 
114 	/*
115 	 * Points to a CCB in the queue that is currently being processed by
116 	 *  SCIL.  This allows us to keep in flight CCBs in the queue so as to
117 	 *  maintain ordering (i.e. in case we retry an I/O and then find out
118 	 *  it needs to be retried again - it just keeps its same place in the
119 	 *  queue.
120 	 */
121 	union ccb *			queued_ccb_in_progress;
122 };
123 
124 struct ISCI_DOMAIN {
125 	struct ISCI_CONTROLLER		*controller;
126 	SCI_DOMAIN_HANDLE_T		sci_object;
127 	uint8_t				index;
128 	struct ISCI_REMOTE_DEVICE	*da_remote_device;
129 };
130 
131 struct ISCI_MEMORY
132 {
133 	bus_addr_t	physical_address;
134 	bus_dma_tag_t	dma_tag;
135 	bus_dmamap_t	dma_map;
136 	POINTER_UINT	virtual_address;
137 	uint32_t	size;
138 	int		error;
139 };
140 
141 struct ISCI_INTERRUPT_INFO
142 {
143 	SCIC_CONTROLLER_HANDLER_METHODS_T 	*handlers;
144 	void					*interrupt_target_handle;
145 	struct resource				*res;
146 	int					rid;
147 	void					*tag;
148 
149 };
150 
151 struct ISCI_PHY
152 {
153 	struct cdev		*cdev_fault;
154 	struct cdev		*cdev_locate;
155 	SCI_CONTROLLER_HANDLE_T	handle;
156 	int			index;
157 	int			led_fault;
158 	int			led_locate;
159 };
160 
161 struct ISCI_CONTROLLER
162 {
163 	struct isci_softc 	*isci;
164 	uint8_t			index;
165 	SCI_CONTROLLER_HANDLE_T	scif_controller_handle;
166 	struct ISCI_DOMAIN	domain[SCI_MAX_DOMAINS];
167 	BOOL			is_started;
168 	BOOL			has_been_scanned;
169 	uint32_t		initial_discovery_mask;
170 	BOOL			is_frozen;
171 	BOOL			release_queued_ccbs;
172 	BOOL			fail_on_task_timeout;
173 	uint8_t			*remote_device_memory;
174 	struct ISCI_MEMORY	cached_controller_memory;
175 	struct ISCI_MEMORY	uncached_controller_memory;
176 	struct ISCI_MEMORY	request_memory;
177 	bus_dma_tag_t		buffer_dma_tag;
178 	struct mtx		lock;
179 	struct cam_sim		*sim;
180 	struct cam_path		*path;
181 	struct ISCI_REMOTE_DEVICE *remote_device[SCI_MAX_REMOTE_DEVICES];
182 	void 			*timer_memory;
183 	SCIC_OEM_PARAMETERS_T	oem_parameters;
184 	uint32_t		oem_parameters_version;
185 	uint32_t		queue_depth;
186 	uint32_t		sim_queue_depth;
187 	SCI_FAST_LIST_T		pending_device_reset_list;
188 	struct ISCI_PHY		phys[SCI_MAX_PHYS];
189 
190 	SCI_MEMORY_DESCRIPTOR_LIST_HANDLE_T mdl;
191 
192 	SCI_POOL_CREATE(remote_device_pool, struct ISCI_REMOTE_DEVICE *, SCI_MAX_REMOTE_DEVICES);
193 	SCI_POOL_CREATE(request_pool, struct ISCI_REQUEST *, SCI_MAX_IO_REQUESTS);
194 	SCI_POOL_CREATE(timer_pool, struct ISCI_TIMER *, SCI_MAX_TIMERS);
195 	SCI_POOL_CREATE(unmap_buffer_pool, void *, SCI_MAX_REMOTE_DEVICES);
196 };
197 
198 struct ISCI_REQUEST
199 {
200 	SCI_CONTROLLER_HANDLE_T		controller_handle;
201 	SCI_REMOTE_DEVICE_HANDLE_T	remote_device_handle;
202 	bus_dma_tag_t			dma_tag;
203 	bus_dmamap_t			dma_map;
204 	SCI_PHYSICAL_ADDRESS		physical_address;
205 	struct callout			timer;
206 };
207 
208 struct ISCI_IO_REQUEST
209 {
210 	struct ISCI_REQUEST	parent;
211 	SCI_IO_REQUEST_HANDLE_T	sci_object;
212 	union ccb		*ccb;
213 	uint32_t		num_segments;
214 	uint32_t		current_sge_index;
215 	bus_dma_segment_t	*sge;
216 };
217 
218 struct ISCI_TASK_REQUEST
219 {
220 	struct ISCI_REQUEST		parent;
221 	struct scsi_sense_data		sense_data;
222 	SCI_TASK_REQUEST_HANDLE_T	sci_object;
223 	union ccb			*ccb;
224 
225 };
226 
227 struct ISCI_PCI_BAR {
228 
229 	bus_space_tag_t		bus_tag;
230 	bus_space_handle_t	bus_handle;
231 	int			resource_id;
232 	struct resource		*resource;
233 
234 };
235 
236 /*
237  * One of these per allocated PCI device.
238  */
239 struct isci_softc {
240 
241 	struct ISCI_PCI_BAR			pci_bar[ISCI_NUM_PCI_BARS];
242 	struct ISCI_CONTROLLER			controllers[SCI_MAX_CONTROLLERS];
243 	SCI_LIBRARY_HANDLE_T			sci_library_handle;
244 	void *					sci_library_memory;
245 	SCIC_CONTROLLER_HANDLER_METHODS_T	handlers[4];
246 	struct ISCI_INTERRUPT_INFO		interrupt_info[4];
247 	uint32_t				controller_count;
248 	uint32_t				num_interrupts;
249 	uint32_t				coalesce_number;
250 	uint32_t				coalesce_timeout;
251 	device_t				device;
252 	SCI_PCI_COMMON_HEADER_T			pci_common_header;
253 	BOOL					oem_parameters_found;
254 	struct intr_config_hook			config_hook;
255 };
256 
257 int isci_allocate_resources(device_t device);
258 
259 int isci_allocate_dma_buffer(device_t device, struct ISCI_CONTROLLER *lock,
260     struct ISCI_MEMORY *memory);
261 
262 void isci_remote_device_reset(struct ISCI_REMOTE_DEVICE *remote_device,
263     union ccb *ccb);
264 
265 /**
266  *  Returns the negotiated link rate (in KB/s) for the associated
267  *	remote device.  Used to fill out bitrate field for GET_TRANS_SETTINGS.
268  *	Will match the negotiated link rate for the lowest numbered local phy
269  *	in the port/domain containing this remote device.
270  */
271 uint32_t isci_remote_device_get_bitrate(
272     struct ISCI_REMOTE_DEVICE *remote_device);
273 
274 void isci_remote_device_freeze_lun_queue(
275     struct ISCI_REMOTE_DEVICE *remote_device, lun_id_t lun);
276 
277 void isci_remote_device_release_lun_queue(
278     struct ISCI_REMOTE_DEVICE *remote_device, lun_id_t lun);
279 
280 void isci_remote_device_release_device_queue(
281     struct ISCI_REMOTE_DEVICE * remote_device);
282 
283 void isci_request_construct(struct ISCI_REQUEST *request,
284     SCI_CONTROLLER_HANDLE_T scif_controller_handle,
285     bus_dma_tag_t io_buffer_dma_tag, bus_addr_t physical_address);
286 
287 #define isci_io_request_get_max_io_size() \
288 	((SCI_MAX_SCATTER_GATHER_ELEMENTS - 1) * PAGE_SIZE)
289 
290 #define isci_task_request_get_object_size() \
291 	(sizeof(struct ISCI_TASK_REQUEST) + scif_task_request_get_object_size())
292 
293 #define isci_io_request_get_object_size() \
294 	(sizeof(struct ISCI_IO_REQUEST) + scif_io_request_get_object_size())
295 
296 #define isci_request_get_object_size() \
297 	max( \
298 	    isci_task_request_get_object_size(), \
299 	    isci_io_request_get_object_size() \
300 	)
301 
302 
303 void isci_io_request_execute_scsi_io(union ccb *ccb,
304     struct ISCI_CONTROLLER *controller);
305 
306 void isci_io_request_execute_smp_io(
307     union ccb *ccb, struct ISCI_CONTROLLER *controller);
308 
309 void isci_io_request_timeout(void *);
310 
311 void isci_get_oem_parameters(struct isci_softc *isci);
312 
313 void isci_io_request_complete(
314     SCI_CONTROLLER_HANDLE_T scif_controller,
315     SCI_REMOTE_DEVICE_HANDLE_T remote_device,
316     struct ISCI_IO_REQUEST * isci_request, SCI_IO_STATUS completion_status);
317 
318 void isci_task_request_complete(
319     SCI_CONTROLLER_HANDLE_T scif_controller,
320     SCI_REMOTE_DEVICE_HANDLE_T remote_device,
321     SCI_TASK_REQUEST_HANDLE_T io_request, SCI_TASK_STATUS completion_status);
322 
323 void isci_sysctl_initialize(struct isci_softc *isci);
324 
325 void isci_controller_construct(struct ISCI_CONTROLLER *controller,
326     struct isci_softc *isci);
327 
328 SCI_STATUS isci_controller_initialize(struct ISCI_CONTROLLER *controller);
329 
330 int isci_controller_allocate_memory(struct ISCI_CONTROLLER *controller);
331 
332 void isci_controller_domain_discovery_complete(
333     struct ISCI_CONTROLLER *isci_controller, struct ISCI_DOMAIN *isci_domain);
334 
335 int isci_controller_attach_to_cam(struct ISCI_CONTROLLER *controller);
336 
337 void isci_controller_start(void *controller);
338 
339 void isci_controller_release_queued_ccbs(struct ISCI_CONTROLLER *controller);
340 
341 void isci_domain_construct(struct ISCI_DOMAIN *domain, uint32_t domain_index,
342     struct ISCI_CONTROLLER *controller);
343 
344 void isci_interrupt_setup(struct isci_softc *isci);
345 void isci_interrupt_poll_handler(struct ISCI_CONTROLLER *controller);
346 
347 void isci_log_message(uint32_t	verbosity, char *log_message_prefix,
348     char *log_message, ...);
349 
350 extern uint32_t g_isci_debug_level;
351 
352 #endif /* #ifndef _ISCI_H */
353