xref: /freebsd/sys/dev/isci/isci_controller.c (revision 685dc743dc3b5645e34836464128e1c0558b404b)
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 #include <dev/isci/isci.h>
35 
36 #include <sys/conf.h>
37 #include <sys/malloc.h>
38 
39 #include <cam/cam_periph.h>
40 #include <cam/cam_xpt_periph.h>
41 
42 #include <dev/isci/scil/sci_memory_descriptor_list.h>
43 #include <dev/isci/scil/sci_memory_descriptor_list_decorator.h>
44 
45 #include <dev/isci/scil/scif_controller.h>
46 #include <dev/isci/scil/scif_library.h>
47 #include <dev/isci/scil/scif_io_request.h>
48 #include <dev/isci/scil/scif_task_request.h>
49 #include <dev/isci/scil/scif_remote_device.h>
50 #include <dev/isci/scil/scif_domain.h>
51 #include <dev/isci/scil/scif_user_callback.h>
52 #include <dev/isci/scil/scic_sgpio.h>
53 
54 #include <dev/led/led.h>
55 
56 void isci_action(struct cam_sim *sim, union ccb *ccb);
57 void isci_poll(struct cam_sim *sim);
58 
59 #define ccb_sim_ptr sim_priv.entries[0].ptr
60 
61 /**
62  * @brief This user callback will inform the user that the controller has
63  *        had a serious unexpected error.  The user should not the error,
64  *        disable interrupts, and wait for current ongoing processing to
65  *        complete.  Subsequently, the user should reset the controller.
66  *
67  * @param[in]  controller This parameter specifies the controller that had
68  *                        an error.
69  *
70  * @return none
71  */
scif_cb_controller_error(SCI_CONTROLLER_HANDLE_T controller,SCI_CONTROLLER_ERROR error)72 void scif_cb_controller_error(SCI_CONTROLLER_HANDLE_T controller,
73     SCI_CONTROLLER_ERROR error)
74 {
75 
76 	isci_log_message(0, "ISCI", "scif_cb_controller_error: 0x%x\n",
77 	    error);
78 }
79 
80 /**
81  * @brief This user callback will inform the user that the controller has
82  *        finished the start process.
83  *
84  * @param[in]  controller This parameter specifies the controller that was
85  *             started.
86  * @param[in]  completion_status This parameter specifies the results of
87  *             the start operation.  SCI_SUCCESS indicates successful
88  *             completion.
89  *
90  * @return none
91  */
scif_cb_controller_start_complete(SCI_CONTROLLER_HANDLE_T controller,SCI_STATUS completion_status)92 void scif_cb_controller_start_complete(SCI_CONTROLLER_HANDLE_T controller,
93     SCI_STATUS completion_status)
94 {
95 	uint32_t index;
96 	struct ISCI_CONTROLLER *isci_controller = (struct ISCI_CONTROLLER *)
97 	    sci_object_get_association(controller);
98 
99 	isci_controller->is_started = TRUE;
100 
101 	/* Set bits for all domains.  We will clear them one-by-one once
102 	 *  the domains complete discovery, or return error when calling
103 	 *  scif_domain_discover.  Once all bits are clear, we will register
104 	 *  the controller with CAM.
105 	 */
106 	isci_controller->initial_discovery_mask = (1 << SCI_MAX_DOMAINS) - 1;
107 
108 	for(index = 0; index < SCI_MAX_DOMAINS; index++) {
109 		SCI_STATUS status;
110 		SCI_DOMAIN_HANDLE_T domain =
111 		    isci_controller->domain[index].sci_object;
112 
113 		status = scif_domain_discover(
114 			domain,
115 			scif_domain_get_suggested_discover_timeout(domain),
116 			DEVICE_TIMEOUT
117 		);
118 
119 		if (status != SCI_SUCCESS)
120 		{
121 			isci_controller_domain_discovery_complete(
122 			    isci_controller, &isci_controller->domain[index]);
123 		}
124 	}
125 }
126 
127 /**
128  * @brief This user callback will inform the user that the controller has
129  *        finished the stop process. Note, after user calls
130  *        scif_controller_stop(), before user receives this controller stop
131  *        complete callback, user should not expect any callback from
132  *        framework, such like scif_cb_domain_change_notification().
133  *
134  * @param[in]  controller This parameter specifies the controller that was
135  *             stopped.
136  * @param[in]  completion_status This parameter specifies the results of
137  *             the stop operation.  SCI_SUCCESS indicates successful
138  *             completion.
139  *
140  * @return none
141  */
scif_cb_controller_stop_complete(SCI_CONTROLLER_HANDLE_T controller,SCI_STATUS completion_status)142 void scif_cb_controller_stop_complete(SCI_CONTROLLER_HANDLE_T controller,
143     SCI_STATUS completion_status)
144 {
145 	struct ISCI_CONTROLLER *isci_controller = (struct ISCI_CONTROLLER *)
146 	    sci_object_get_association(controller);
147 
148 	isci_controller->is_started = FALSE;
149 }
150 
151 static void
isci_single_map(void * arg,bus_dma_segment_t * seg,int nseg,int error)152 isci_single_map(void *arg, bus_dma_segment_t *seg, int nseg, int error)
153 {
154 	SCI_PHYSICAL_ADDRESS *phys_addr = arg;
155 
156 	*phys_addr = seg[0].ds_addr;
157 }
158 
159 /**
160  * @brief This method will be invoked to allocate memory dynamically.
161  *
162  * @param[in]  controller This parameter represents the controller
163  *             object for which to allocate memory.
164  * @param[out] mde This parameter represents the memory descriptor to
165  *             be filled in by the user that will reference the newly
166  *             allocated memory.
167  *
168  * @return none
169  */
scif_cb_controller_allocate_memory(SCI_CONTROLLER_HANDLE_T controller,SCI_PHYSICAL_MEMORY_DESCRIPTOR_T * mde)170 void scif_cb_controller_allocate_memory(SCI_CONTROLLER_HANDLE_T controller,
171     SCI_PHYSICAL_MEMORY_DESCRIPTOR_T *mde)
172 {
173 	struct ISCI_CONTROLLER *isci_controller = (struct ISCI_CONTROLLER *)
174 	    sci_object_get_association(controller);
175 
176 	/*
177 	 * Note this routine is only used for buffers needed to translate
178 	 * SCSI UNMAP commands to ATA DSM commands for SATA disks.
179 	 *
180 	 * We first try to pull a buffer from the controller's pool, and only
181 	 * call contigmalloc if one isn't there.
182 	 */
183 	if (!sci_pool_empty(isci_controller->unmap_buffer_pool)) {
184 		sci_pool_get(isci_controller->unmap_buffer_pool,
185 		    mde->virtual_address);
186 	} else
187 		mde->virtual_address = contigmalloc(PAGE_SIZE,
188 		    M_ISCI, M_NOWAIT, 0, BUS_SPACE_MAXADDR,
189 		    mde->constant_memory_alignment, 0);
190 
191 	if (mde->virtual_address != NULL)
192 		bus_dmamap_load(isci_controller->buffer_dma_tag,
193 		    NULL, mde->virtual_address, PAGE_SIZE,
194 		    isci_single_map, &mde->physical_address,
195 		    BUS_DMA_NOWAIT);
196 }
197 
198 /**
199  * @brief This method will be invoked to allocate memory dynamically.
200  *
201  * @param[in]  controller This parameter represents the controller
202  *             object for which to allocate memory.
203  * @param[out] mde This parameter represents the memory descriptor to
204  *             be filled in by the user that will reference the newly
205  *             allocated memory.
206  *
207  * @return none
208  */
scif_cb_controller_free_memory(SCI_CONTROLLER_HANDLE_T controller,SCI_PHYSICAL_MEMORY_DESCRIPTOR_T * mde)209 void scif_cb_controller_free_memory(SCI_CONTROLLER_HANDLE_T controller,
210     SCI_PHYSICAL_MEMORY_DESCRIPTOR_T * mde)
211 {
212 	struct ISCI_CONTROLLER *isci_controller = (struct ISCI_CONTROLLER *)
213 	    sci_object_get_association(controller);
214 
215 	/*
216 	 * Put the buffer back into the controller's buffer pool, rather
217 	 * than invoking configfree.  This helps reduce chance we won't
218 	 * have buffers available when system is under memory pressure.
219 	 */
220 	sci_pool_put(isci_controller->unmap_buffer_pool,
221 	    mde->virtual_address);
222 }
223 
isci_controller_construct(struct ISCI_CONTROLLER * controller,struct isci_softc * isci)224 void isci_controller_construct(struct ISCI_CONTROLLER *controller,
225     struct isci_softc *isci)
226 {
227 	SCI_CONTROLLER_HANDLE_T scif_controller_handle;
228 
229 	scif_library_allocate_controller(isci->sci_library_handle,
230 	    &scif_controller_handle);
231 
232 	scif_controller_construct(isci->sci_library_handle,
233 	    scif_controller_handle, NULL);
234 
235 	controller->isci = isci;
236 	controller->scif_controller_handle = scif_controller_handle;
237 
238 	/* This allows us to later use
239 	 *  sci_object_get_association(scif_controller_handle)
240 	 * inside of a callback routine to get our struct ISCI_CONTROLLER object
241 	 */
242 	sci_object_set_association(scif_controller_handle, (void *)controller);
243 
244 	controller->is_started = FALSE;
245 	controller->is_frozen = FALSE;
246 	controller->release_queued_ccbs = FALSE;
247 	controller->sim = NULL;
248 	controller->initial_discovery_mask = 0;
249 
250 	sci_fast_list_init(&controller->pending_device_reset_list);
251 
252 	mtx_init(&controller->lock, "isci", NULL, MTX_DEF);
253 
254 	uint32_t domain_index;
255 
256 	for(domain_index = 0; domain_index < SCI_MAX_DOMAINS; domain_index++) {
257 		isci_domain_construct( &controller->domain[domain_index],
258 		    domain_index, controller);
259 	}
260 
261 	controller->timer_memory = malloc(
262 	    sizeof(struct ISCI_TIMER) * SCI_MAX_TIMERS, M_ISCI,
263 	    M_NOWAIT | M_ZERO);
264 
265 	sci_pool_initialize(controller->timer_pool);
266 
267 	struct ISCI_TIMER *timer = (struct ISCI_TIMER *)
268 	    controller->timer_memory;
269 
270 	for ( int i = 0; i < SCI_MAX_TIMERS; i++ ) {
271 		sci_pool_put(controller->timer_pool, timer++);
272 	}
273 
274 	sci_pool_initialize(controller->unmap_buffer_pool);
275 }
276 
isci_led_fault_func(void * priv,int onoff)277 static void isci_led_fault_func(void *priv, int onoff)
278 {
279 	struct ISCI_PHY *phy = priv;
280 
281 	/* map onoff to the fault LED */
282 	phy->led_fault = onoff;
283 	scic_sgpio_update_led_state(phy->handle, 1 << phy->index,
284 		phy->led_fault, phy->led_locate, 0);
285 }
286 
isci_led_locate_func(void * priv,int onoff)287 static void isci_led_locate_func(void *priv, int onoff)
288 {
289 	struct ISCI_PHY *phy = priv;
290 
291 	/* map onoff to the locate LED */
292 	phy->led_locate = onoff;
293 	scic_sgpio_update_led_state(phy->handle, 1 << phy->index,
294 		phy->led_fault, phy->led_locate, 0);
295 }
296 
isci_controller_initialize(struct ISCI_CONTROLLER * controller)297 SCI_STATUS isci_controller_initialize(struct ISCI_CONTROLLER *controller)
298 {
299 	SCIC_USER_PARAMETERS_T scic_user_parameters;
300 	SCI_CONTROLLER_HANDLE_T scic_controller_handle;
301 	char led_name[64];
302 	unsigned long tunable;
303 	uint32_t io_shortage;
304 	uint32_t fail_on_timeout;
305 	int i;
306 
307 	scic_controller_handle =
308 	    scif_controller_get_scic_handle(controller->scif_controller_handle);
309 
310 	if (controller->isci->oem_parameters_found == TRUE)
311 	{
312 		scic_oem_parameters_set(
313 		    scic_controller_handle,
314 		    &controller->oem_parameters,
315 		    (uint8_t)(controller->oem_parameters_version));
316 	}
317 
318 	scic_user_parameters_get(scic_controller_handle, &scic_user_parameters);
319 
320 	if (TUNABLE_ULONG_FETCH("hw.isci.no_outbound_task_timeout", &tunable))
321 		scic_user_parameters.sds1.no_outbound_task_timeout =
322 		    (uint8_t)tunable;
323 
324 	if (TUNABLE_ULONG_FETCH("hw.isci.ssp_max_occupancy_timeout", &tunable))
325 		scic_user_parameters.sds1.ssp_max_occupancy_timeout =
326 		    (uint16_t)tunable;
327 
328 	if (TUNABLE_ULONG_FETCH("hw.isci.stp_max_occupancy_timeout", &tunable))
329 		scic_user_parameters.sds1.stp_max_occupancy_timeout =
330 		    (uint16_t)tunable;
331 
332 	if (TUNABLE_ULONG_FETCH("hw.isci.ssp_inactivity_timeout", &tunable))
333 		scic_user_parameters.sds1.ssp_inactivity_timeout =
334 		    (uint16_t)tunable;
335 
336 	if (TUNABLE_ULONG_FETCH("hw.isci.stp_inactivity_timeout", &tunable))
337 		scic_user_parameters.sds1.stp_inactivity_timeout =
338 		    (uint16_t)tunable;
339 
340 	if (TUNABLE_ULONG_FETCH("hw.isci.max_speed_generation", &tunable))
341 		for (i = 0; i < SCI_MAX_PHYS; i++)
342 			scic_user_parameters.sds1.phys[i].max_speed_generation =
343 			    (uint8_t)tunable;
344 
345 	scic_user_parameters_set(scic_controller_handle, &scic_user_parameters);
346 
347 	/* Scheduler bug in SCU requires SCIL to reserve some task contexts as a
348 	 *  a workaround - one per domain.
349 	 */
350 	controller->queue_depth = SCI_MAX_IO_REQUESTS - SCI_MAX_DOMAINS;
351 
352 	if (TUNABLE_INT_FETCH("hw.isci.controller_queue_depth",
353 	    &controller->queue_depth)) {
354 		controller->queue_depth = max(1, min(controller->queue_depth,
355 		    SCI_MAX_IO_REQUESTS - SCI_MAX_DOMAINS));
356 	}
357 
358 	/* Reserve one request so that we can ensure we have one available TC
359 	 *  to do internal device resets.
360 	 */
361 	controller->sim_queue_depth = controller->queue_depth - 1;
362 
363 	/* Although we save one TC to do internal device resets, it is possible
364 	 *  we could end up using several TCs for simultaneous device resets
365 	 *  while at the same time having CAM fill our controller queue.  To
366 	 *  simulate this condition, and how our driver handles it, we can set
367 	 *  this io_shortage parameter, which will tell CAM that we have a
368 	 *  large queue depth than we really do.
369 	 */
370 	io_shortage = 0;
371 	TUNABLE_INT_FETCH("hw.isci.io_shortage", &io_shortage);
372 	controller->sim_queue_depth += io_shortage;
373 
374 	fail_on_timeout = 1;
375 	TUNABLE_INT_FETCH("hw.isci.fail_on_task_timeout", &fail_on_timeout);
376 	controller->fail_on_task_timeout = fail_on_timeout;
377 
378 	/* Attach to CAM using xpt_bus_register now, then immediately freeze
379 	 *  the simq.  It will get released later when initial domain discovery
380 	 *  is complete.
381 	 */
382 	controller->has_been_scanned = FALSE;
383 	mtx_lock(&controller->lock);
384 	isci_controller_attach_to_cam(controller);
385 	xpt_freeze_simq(controller->sim, 1);
386 	mtx_unlock(&controller->lock);
387 
388 	for (i = 0; i < SCI_MAX_PHYS; i++) {
389 		controller->phys[i].handle = scic_controller_handle;
390 		controller->phys[i].index = i;
391 
392 		/* fault */
393 		controller->phys[i].led_fault = 0;
394 		sprintf(led_name, "isci.bus%d.port%d.fault", controller->index, i);
395 		controller->phys[i].cdev_fault = led_create(isci_led_fault_func,
396 		    &controller->phys[i], led_name);
397 
398 		/* locate */
399 		controller->phys[i].led_locate = 0;
400 		sprintf(led_name, "isci.bus%d.port%d.locate", controller->index, i);
401 		controller->phys[i].cdev_locate = led_create(isci_led_locate_func,
402 		    &controller->phys[i], led_name);
403 	}
404 
405 	return (scif_controller_initialize(controller->scif_controller_handle));
406 }
407 
isci_controller_allocate_memory(struct ISCI_CONTROLLER * controller)408 int isci_controller_allocate_memory(struct ISCI_CONTROLLER *controller)
409 {
410 	int error;
411 	device_t device =  controller->isci->device;
412 	uint32_t max_segment_size = isci_io_request_get_max_io_size();
413 	struct ISCI_MEMORY *uncached_controller_memory =
414 	    &controller->uncached_controller_memory;
415 	struct ISCI_MEMORY *cached_controller_memory =
416 	    &controller->cached_controller_memory;
417 	struct ISCI_MEMORY *request_memory =
418 	    &controller->request_memory;
419 	POINTER_UINT virtual_address;
420 	bus_addr_t physical_address;
421 
422 	controller->mdl = sci_controller_get_memory_descriptor_list_handle(
423 	    controller->scif_controller_handle);
424 
425 	uncached_controller_memory->size = sci_mdl_decorator_get_memory_size(
426 	    controller->mdl, SCI_MDE_ATTRIBUTE_PHYSICALLY_CONTIGUOUS);
427 
428 	error = isci_allocate_dma_buffer(device, controller,
429 	    uncached_controller_memory);
430 
431 	if (error != 0)
432 	    return (error);
433 
434 	sci_mdl_decorator_assign_memory( controller->mdl,
435 	    SCI_MDE_ATTRIBUTE_PHYSICALLY_CONTIGUOUS,
436 	    uncached_controller_memory->virtual_address,
437 	    uncached_controller_memory->physical_address);
438 
439 	cached_controller_memory->size = sci_mdl_decorator_get_memory_size(
440 	    controller->mdl,
441 	    SCI_MDE_ATTRIBUTE_CACHEABLE | SCI_MDE_ATTRIBUTE_PHYSICALLY_CONTIGUOUS
442 	);
443 
444 	error = isci_allocate_dma_buffer(device, controller,
445 	    cached_controller_memory);
446 
447 	if (error != 0)
448 	    return (error);
449 
450 	sci_mdl_decorator_assign_memory(controller->mdl,
451 	    SCI_MDE_ATTRIBUTE_CACHEABLE | SCI_MDE_ATTRIBUTE_PHYSICALLY_CONTIGUOUS,
452 	    cached_controller_memory->virtual_address,
453 	    cached_controller_memory->physical_address);
454 
455 	request_memory->size =
456 	    controller->queue_depth * isci_io_request_get_object_size();
457 
458 	error = isci_allocate_dma_buffer(device, controller, request_memory);
459 
460 	if (error != 0)
461 	    return (error);
462 
463 	/* For STP PIO testing, we want to ensure we can force multiple SGLs
464 	 *  since this has been a problem area in SCIL.  This tunable parameter
465 	 *  will allow us to force DMA segments to a smaller size, ensuring
466 	 *  that even if a physically contiguous buffer is attached to this
467 	 *  I/O, the DMA subsystem will pass us multiple segments in our DMA
468 	 *  load callback.
469 	 */
470 	TUNABLE_INT_FETCH("hw.isci.max_segment_size", &max_segment_size);
471 
472 	/* Create DMA tag for our I/O requests.  Then we can create DMA maps based off
473 	 *  of this tag and store them in each of our ISCI_IO_REQUEST objects.  This
474 	 *  will enable better performance than creating the DMA maps every time we get
475 	 *  an I/O.
476 	 */
477 	error = bus_dma_tag_create(bus_get_dma_tag(device), 0x1,
478 	    ISCI_DMA_BOUNDARY, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR,
479 	    NULL, NULL, isci_io_request_get_max_io_size(),
480 	    SCI_MAX_SCATTER_GATHER_ELEMENTS, max_segment_size, 0,
481 	    busdma_lock_mutex, &controller->lock,
482 	    &controller->buffer_dma_tag);
483 
484 	if (error != 0)
485 	    return (error);
486 
487 	sci_pool_initialize(controller->request_pool);
488 
489 	virtual_address = request_memory->virtual_address;
490 	physical_address = request_memory->physical_address;
491 
492 	for (int i = 0; i < controller->queue_depth; i++) {
493 		struct ISCI_REQUEST *request =
494 		    (struct ISCI_REQUEST *)virtual_address;
495 
496 		isci_request_construct(request,
497 		    controller->scif_controller_handle,
498 		    controller->buffer_dma_tag, physical_address);
499 
500 		sci_pool_put(controller->request_pool, request);
501 
502 		virtual_address += isci_request_get_object_size();
503 		physical_address += isci_request_get_object_size();
504 	}
505 
506 	uint32_t remote_device_size = sizeof(struct ISCI_REMOTE_DEVICE) +
507 	    scif_remote_device_get_object_size();
508 
509 	controller->remote_device_memory = (uint8_t *) malloc(
510 	    remote_device_size * SCI_MAX_REMOTE_DEVICES, M_ISCI,
511 	    M_NOWAIT | M_ZERO);
512 
513 	sci_pool_initialize(controller->remote_device_pool);
514 
515 	uint8_t *remote_device_memory_ptr = controller->remote_device_memory;
516 
517 	for (int i = 0; i < SCI_MAX_REMOTE_DEVICES; i++) {
518 		struct ISCI_REMOTE_DEVICE *remote_device =
519 		    (struct ISCI_REMOTE_DEVICE *)remote_device_memory_ptr;
520 
521 		controller->remote_device[i] = NULL;
522 		remote_device->index = i;
523 		remote_device->is_resetting = FALSE;
524 		remote_device->frozen_lun_mask = 0;
525 		sci_fast_list_element_init(remote_device,
526 		    &remote_device->pending_device_reset_element);
527 		TAILQ_INIT(&remote_device->queued_ccbs);
528 		remote_device->release_queued_ccb = FALSE;
529 		remote_device->queued_ccb_in_progress = NULL;
530 
531 		/*
532 		 * For the first SCI_MAX_DOMAINS device objects, do not put
533 		 *  them in the pool, rather assign them to each domain.  This
534 		 *  ensures that any device attached directly to port "i" will
535 		 *  always get CAM target id "i".
536 		 */
537 		if (i < SCI_MAX_DOMAINS)
538 			controller->domain[i].da_remote_device = remote_device;
539 		else
540 			sci_pool_put(controller->remote_device_pool,
541 			    remote_device);
542 		remote_device_memory_ptr += remote_device_size;
543 	}
544 
545 	return (0);
546 }
547 
isci_controller_start(void * controller_handle)548 void isci_controller_start(void *controller_handle)
549 {
550 	struct ISCI_CONTROLLER *controller =
551 	    (struct ISCI_CONTROLLER *)controller_handle;
552 	SCI_CONTROLLER_HANDLE_T scif_controller_handle =
553 	    controller->scif_controller_handle;
554 
555 	scif_controller_start(scif_controller_handle,
556 	    scif_controller_get_suggested_start_timeout(scif_controller_handle));
557 
558 	scic_controller_enable_interrupts(
559 	    scif_controller_get_scic_handle(controller->scif_controller_handle));
560 }
561 
isci_controller_domain_discovery_complete(struct ISCI_CONTROLLER * isci_controller,struct ISCI_DOMAIN * isci_domain)562 void isci_controller_domain_discovery_complete(
563     struct ISCI_CONTROLLER *isci_controller, struct ISCI_DOMAIN *isci_domain)
564 {
565 	if (!isci_controller->has_been_scanned)
566 	{
567 		/* Controller has not been scanned yet.  We'll clear
568 		 *  the discovery bit for this domain, then check if all bits
569 		 *  are now clear.  That would indicate that all domains are
570 		 *  done with discovery and we can then proceed with initial
571 		 *  scan.
572 		 */
573 
574 		isci_controller->initial_discovery_mask &=
575 		    ~(1 << isci_domain->index);
576 
577 		if (isci_controller->initial_discovery_mask == 0) {
578 			struct isci_softc *driver = isci_controller->isci;
579 			uint8_t next_index = isci_controller->index + 1;
580 
581 			isci_controller->has_been_scanned = TRUE;
582 
583 			/* Unfreeze simq to allow initial scan to proceed. */
584 			xpt_release_simq(isci_controller->sim, TRUE);
585 
586 			if (next_index < driver->controller_count) {
587 				/*  There are more controllers that need to
588 				 *   start.  So start the next one.
589 				 */
590 				isci_controller_start(
591 				    &driver->controllers[next_index]);
592 			}
593 			else
594 			{
595 				/* All controllers have been started and completed discovery.
596 				 *  Disestablish the config hook while will signal to the
597 				 *  kernel during boot that it is safe to try to find and
598 				 *  mount the root partition.
599 				 */
600 				config_intrhook_disestablish(
601 				    &driver->config_hook);
602 			}
603 		}
604 	}
605 }
606 
isci_controller_attach_to_cam(struct ISCI_CONTROLLER * controller)607 int isci_controller_attach_to_cam(struct ISCI_CONTROLLER *controller)
608 {
609 	struct isci_softc *isci = controller->isci;
610 	device_t parent = device_get_parent(isci->device);
611 	int unit = device_get_unit(isci->device);
612 	struct cam_devq *isci_devq = cam_simq_alloc(controller->sim_queue_depth);
613 
614 	if(isci_devq == NULL) {
615 		isci_log_message(0, "ISCI", "isci_devq is NULL \n");
616 		return (-1);
617 	}
618 
619 	controller->sim = cam_sim_alloc(isci_action, isci_poll, "isci",
620 	    controller, unit, &controller->lock, controller->sim_queue_depth,
621 	    controller->sim_queue_depth, isci_devq);
622 
623 	if(controller->sim == NULL) {
624 		isci_log_message(0, "ISCI", "cam_sim_alloc... fails\n");
625 		cam_simq_free(isci_devq);
626 		return (-1);
627 	}
628 
629 	if(xpt_bus_register(controller->sim, parent, controller->index)
630 	    != CAM_SUCCESS) {
631 		isci_log_message(0, "ISCI", "xpt_bus_register...fails \n");
632 		cam_sim_free(controller->sim, TRUE);
633 		mtx_unlock(&controller->lock);
634 		return (-1);
635 	}
636 
637 	if(xpt_create_path(&controller->path, NULL,
638 	    cam_sim_path(controller->sim), CAM_TARGET_WILDCARD,
639 	    CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
640 		isci_log_message(0, "ISCI", "xpt_create_path....fails\n");
641 		xpt_bus_deregister(cam_sim_path(controller->sim));
642 		cam_sim_free(controller->sim, TRUE);
643 		mtx_unlock(&controller->lock);
644 		return (-1);
645 	}
646 
647 	return (0);
648 }
649 
isci_poll(struct cam_sim * sim)650 void isci_poll(struct cam_sim *sim)
651 {
652 	struct ISCI_CONTROLLER *controller =
653 	    (struct ISCI_CONTROLLER *)cam_sim_softc(sim);
654 
655 	isci_interrupt_poll_handler(controller);
656 }
657 
isci_action(struct cam_sim * sim,union ccb * ccb)658 void isci_action(struct cam_sim *sim, union ccb *ccb)
659 {
660 	struct ISCI_CONTROLLER *controller =
661 	    (struct ISCI_CONTROLLER *)cam_sim_softc(sim);
662 
663 	switch ( ccb->ccb_h.func_code ) {
664 	case XPT_PATH_INQ:
665 		{
666 			struct ccb_pathinq *cpi = &ccb->cpi;
667 			int bus = cam_sim_bus(sim);
668 			ccb->ccb_h.ccb_sim_ptr = sim;
669 			cpi->version_num = 1;
670 			cpi->hba_inquiry = PI_TAG_ABLE;
671 			cpi->target_sprt = 0;
672 			cpi->hba_misc = PIM_NOBUSRESET | PIM_SEQSCAN |
673 			    PIM_UNMAPPED;
674 			cpi->hba_eng_cnt = 0;
675 			cpi->max_target = SCI_MAX_REMOTE_DEVICES - 1;
676 			cpi->max_lun = ISCI_MAX_LUN;
677 			cpi->maxio = isci_io_request_get_max_io_size();
678 			cpi->unit_number = cam_sim_unit(sim);
679 			cpi->bus_id = bus;
680 			cpi->initiator_id = SCI_MAX_REMOTE_DEVICES;
681 			cpi->base_transfer_speed = 300000;
682 			strlcpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
683 			strlcpy(cpi->hba_vid, "Intel Corp.", HBA_IDLEN);
684 			strlcpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
685 			cpi->transport = XPORT_SAS;
686 			cpi->transport_version = 0;
687 			cpi->protocol = PROTO_SCSI;
688 			cpi->protocol_version = SCSI_REV_SPC2;
689 			cpi->ccb_h.status = CAM_REQ_CMP;
690 			xpt_done(ccb);
691 		}
692 		break;
693 	case XPT_GET_TRAN_SETTINGS:
694 		{
695 			struct ccb_trans_settings *general_settings = &ccb->cts;
696 			struct ccb_trans_settings_sas *sas_settings =
697 			    &general_settings->xport_specific.sas;
698 			struct ccb_trans_settings_scsi *scsi_settings =
699 			    &general_settings->proto_specific.scsi;
700 			struct ISCI_REMOTE_DEVICE *remote_device;
701 
702 			remote_device = controller->remote_device[ccb->ccb_h.target_id];
703 
704 			if (remote_device == NULL) {
705 				ccb->ccb_h.status &= ~CAM_SIM_QUEUED;
706 				ccb->ccb_h.status &= ~CAM_STATUS_MASK;
707 				ccb->ccb_h.status |= CAM_DEV_NOT_THERE;
708 				xpt_done(ccb);
709 				break;
710 			}
711 
712 			general_settings->protocol = PROTO_SCSI;
713 			general_settings->transport = XPORT_SAS;
714 			general_settings->protocol_version = SCSI_REV_SPC2;
715 			general_settings->transport_version = 0;
716 			scsi_settings->valid = CTS_SCSI_VALID_TQ;
717 			scsi_settings->flags = CTS_SCSI_FLAGS_TAG_ENB;
718 			ccb->ccb_h.status &= ~CAM_STATUS_MASK;
719 			ccb->ccb_h.status |= CAM_REQ_CMP;
720 
721 			sas_settings->bitrate =
722 			    isci_remote_device_get_bitrate(remote_device);
723 
724 			if (sas_settings->bitrate != 0)
725 				sas_settings->valid = CTS_SAS_VALID_SPEED;
726 
727 			xpt_done(ccb);
728 		}
729 		break;
730 	case XPT_SCSI_IO:
731 		if (ccb->ccb_h.flags & CAM_CDB_PHYS) {
732 			ccb->ccb_h.status = CAM_REQ_INVALID;
733 			xpt_done(ccb);
734 			break;
735 		}
736 		isci_io_request_execute_scsi_io(ccb, controller);
737 		break;
738 	case XPT_SMP_IO:
739 		isci_io_request_execute_smp_io(ccb, controller);
740 		break;
741 	case XPT_SET_TRAN_SETTINGS:
742 		ccb->ccb_h.status &= ~CAM_STATUS_MASK;
743 		ccb->ccb_h.status |= CAM_REQ_CMP;
744 		xpt_done(ccb);
745 		break;
746 	case XPT_CALC_GEOMETRY:
747 		cam_calc_geometry(&ccb->ccg, /*extended*/1);
748 		xpt_done(ccb);
749 		break;
750 	case XPT_RESET_DEV:
751 		{
752 			struct ISCI_REMOTE_DEVICE *remote_device =
753 			    controller->remote_device[ccb->ccb_h.target_id];
754 
755 			if (remote_device != NULL)
756 				isci_remote_device_reset(remote_device, ccb);
757 			else {
758 				ccb->ccb_h.status &= ~CAM_SIM_QUEUED;
759 				ccb->ccb_h.status &= ~CAM_STATUS_MASK;
760 				ccb->ccb_h.status |= CAM_DEV_NOT_THERE;
761 				xpt_done(ccb);
762 			}
763 		}
764 		break;
765 	case XPT_RESET_BUS:
766 		ccb->ccb_h.status = CAM_REQ_CMP;
767 		xpt_done(ccb);
768 		break;
769 	default:
770 		isci_log_message(0, "ISCI", "Unhandled func_code 0x%x\n",
771 		    ccb->ccb_h.func_code);
772 		ccb->ccb_h.status &= ~CAM_SIM_QUEUED;
773 		ccb->ccb_h.status &= ~CAM_STATUS_MASK;
774 		ccb->ccb_h.status |= CAM_REQ_INVALID;
775 		xpt_done(ccb);
776 		break;
777 	}
778 }
779 
780 /*
781  * Unfortunately, SCIL doesn't cleanly handle retry conditions.
782  *  CAM_REQUEUE_REQ works only when no one is using the pass(4) interface.  So
783  *  when SCIL denotes an I/O needs to be retried (typically because of mixing
784  *  tagged/non-tagged ATA commands, or running out of NCQ slots), we queue
785  *  these I/O internally.  Once SCIL completes an I/O to this device, or we get
786  *  a ready notification, we will retry the first I/O on the queue.
787  *  Unfortunately, SCIL also doesn't cleanly handle starting the new I/O within
788  *  the context of the completion handler, so we need to retry these I/O after
789  *  the completion handler is done executing.
790  */
791 void
isci_controller_release_queued_ccbs(struct ISCI_CONTROLLER * controller)792 isci_controller_release_queued_ccbs(struct ISCI_CONTROLLER *controller)
793 {
794 	struct ISCI_REMOTE_DEVICE *dev;
795 	struct ccb_hdr *ccb_h;
796 	uint8_t *ptr;
797 	int dev_idx;
798 
799 	KASSERT(mtx_owned(&controller->lock), ("controller lock not owned"));
800 
801 	controller->release_queued_ccbs = FALSE;
802 	for (dev_idx = 0;
803 	     dev_idx < SCI_MAX_REMOTE_DEVICES;
804 	     dev_idx++) {
805 
806 		dev = controller->remote_device[dev_idx];
807 		if (dev != NULL &&
808 		    dev->release_queued_ccb == TRUE &&
809 		    dev->queued_ccb_in_progress == NULL) {
810 			dev->release_queued_ccb = FALSE;
811 			ccb_h = TAILQ_FIRST(&dev->queued_ccbs);
812 
813 			if (ccb_h == NULL)
814 				continue;
815 
816 			ptr = scsiio_cdb_ptr(&((union ccb *)ccb_h)->csio);
817 			isci_log_message(1, "ISCI", "release %p %x\n", ccb_h, *ptr);
818 
819 			dev->queued_ccb_in_progress = (union ccb *)ccb_h;
820 			isci_io_request_execute_scsi_io(
821 			    (union ccb *)ccb_h, controller);
822 		}
823 	}
824 }
825