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