xref: /freebsd/sys/dev/isci/isci_io_request.c (revision b7c60aadbbd5c846a250c05791fe7406d6d78bf4)
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 
36 #include <cam/scsi/scsi_all.h>
37 #include <cam/scsi/scsi_message.h>
38 
39 #include <dev/isci/scil/intel_sas.h>
40 
41 #include <dev/isci/scil/sci_util.h>
42 
43 #include <dev/isci/scil/scif_io_request.h>
44 #include <dev/isci/scil/scif_controller.h>
45 #include <dev/isci/scil/scif_remote_device.h>
46 #include <dev/isci/scil/scif_user_callback.h>
47 
48 #include <dev/isci/scil/scic_io_request.h>
49 #include <dev/isci/scil/scic_user_callback.h>
50 
51 /**
52  * @brief This user callback will inform the user that an IO request has
53  *        completed.
54  *
55  * @param[in]  controller This parameter specifies the controller on
56  *             which the IO request is completing.
57  * @param[in]  remote_device This parameter specifies the remote device on
58  *             which this request is completing.
59  * @param[in]  io_request This parameter specifies the IO request that has
60  *             completed.
61  * @param[in]  completion_status This parameter specifies the results of
62  *             the IO request operation.  SCI_IO_SUCCESS indicates
63  *             successful completion.
64  *
65  * @return none
66  */
67 void
68 scif_cb_io_request_complete(SCI_CONTROLLER_HANDLE_T scif_controller,
69     SCI_REMOTE_DEVICE_HANDLE_T remote_device,
70     SCI_IO_REQUEST_HANDLE_T io_request, SCI_IO_STATUS completion_status)
71 {
72 	struct ISCI_IO_REQUEST *isci_request =
73 	    (struct ISCI_IO_REQUEST *)sci_object_get_association(io_request);
74 
75 	scif_controller_complete_io(scif_controller, remote_device, io_request);
76 	isci_io_request_complete(scif_controller, remote_device, isci_request,
77 	    completion_status);
78 }
79 
80 void
81 isci_io_request_complete(SCI_CONTROLLER_HANDLE_T scif_controller,
82     SCI_REMOTE_DEVICE_HANDLE_T remote_device,
83     struct ISCI_IO_REQUEST *isci_request, SCI_IO_STATUS completion_status)
84 {
85 	struct ISCI_CONTROLLER *isci_controller;
86 	struct ISCI_REMOTE_DEVICE *isci_remote_device;
87 	union ccb *ccb;
88 
89 	isci_controller = (struct ISCI_CONTROLLER *) sci_object_get_association(scif_controller);
90 	isci_remote_device =
91 		(struct ISCI_REMOTE_DEVICE *) sci_object_get_association(remote_device);
92 
93 	ccb = isci_request->ccb;
94 
95 	ccb->ccb_h.status &= ~CAM_STATUS_MASK;
96 
97 	switch (completion_status) {
98 	case SCI_IO_SUCCESS:
99 	case SCI_IO_SUCCESS_COMPLETE_BEFORE_START:
100 #if __FreeBSD_version >= 900026
101 		if (ccb->ccb_h.func_code == XPT_SMP_IO) {
102 			void *smp_response =
103 			    scif_io_request_get_response_iu_address(
104 			        isci_request->sci_object);
105 
106 			memcpy(ccb->smpio.smp_response, smp_response,
107 			    ccb->smpio.smp_response_len);
108 		}
109 #endif
110 		ccb->ccb_h.status |= CAM_REQ_CMP;
111 		break;
112 
113 	case SCI_IO_SUCCESS_IO_DONE_EARLY:
114 		ccb->ccb_h.status |= CAM_REQ_CMP;
115 		ccb->csio.resid = ccb->csio.dxfer_len -
116 		    scif_io_request_get_number_of_bytes_transferred(
117 		        isci_request->sci_object);
118 		break;
119 
120 	case SCI_IO_FAILURE_RESPONSE_VALID:
121 	{
122 		SCI_SSP_RESPONSE_IU_T * response_buffer;
123 		uint32_t sense_length;
124 		int error_code, sense_key, asc, ascq;
125 		struct ccb_scsiio *csio = &ccb->csio;
126 
127 		response_buffer = (SCI_SSP_RESPONSE_IU_T *)
128 		    scif_io_request_get_response_iu_address(
129 		        isci_request->sci_object);
130 
131 		sense_length = sci_ssp_get_sense_data_length(
132 		    response_buffer->sense_data_length);
133 
134 		sense_length = MIN(csio->sense_len, sense_length);
135 
136 		memcpy(&csio->sense_data, response_buffer->data, sense_length);
137 
138 		csio->sense_resid = csio->sense_len - sense_length;
139 		csio->scsi_status = response_buffer->status;
140 		ccb->ccb_h.status |= CAM_SCSI_STATUS_ERROR;
141 		ccb->ccb_h.status |= CAM_AUTOSNS_VALID;
142 		scsi_extract_sense( &csio->sense_data, &error_code, &sense_key,
143 		    &asc, &ascq );
144 		isci_log_message(1, "ISCI",
145 		    "isci: bus=%x target=%x lun=%x cdb[0]=%x status=%x key=%x asc=%x ascq=%x\n",
146 		    ccb->ccb_h.path_id, ccb->ccb_h.target_id,
147 		    ccb->ccb_h.target_lun, csio->cdb_io.cdb_bytes[0],
148 		    csio->scsi_status, sense_key, asc, ascq);
149 		break;
150 	}
151 
152 	case SCI_IO_FAILURE_REMOTE_DEVICE_RESET_REQUIRED:
153 		isci_remote_device_reset(isci_remote_device, NULL);
154 
155 		/* drop through */
156 	case SCI_IO_FAILURE_TERMINATED:
157 		ccb->ccb_h.status |= CAM_REQ_TERMIO;
158 		isci_log_message(1, "ISCI",
159 		    "isci: bus=%x target=%x lun=%x cdb[0]=%x terminated\n",
160 		    ccb->ccb_h.path_id, ccb->ccb_h.target_id,
161 		    ccb->ccb_h.target_lun, ccb->csio.cdb_io.cdb_bytes[0]);
162 		break;
163 
164 	case SCI_IO_FAILURE_INVALID_STATE:
165 	case SCI_IO_FAILURE_INSUFFICIENT_RESOURCES:
166 		ccb->ccb_h.status |= CAM_REQUEUE_REQ;
167 		isci_remote_device_freeze_lun_queue(isci_remote_device,
168 		    ccb->ccb_h.target_lun);
169 		break;
170 
171 	case SCI_IO_FAILURE_INVALID_REMOTE_DEVICE:
172 		ccb->ccb_h.status |= CAM_DEV_NOT_THERE;
173 		break;
174 
175 	case SCI_IO_FAILURE_NO_NCQ_TAG_AVAILABLE:
176 		{
177 			struct ccb_relsim ccb_relsim;
178 			struct cam_path *path;
179 
180 			xpt_create_path(&path, NULL,
181 			    cam_sim_path(isci_controller->sim),
182 			    isci_remote_device->index, 0);
183 
184 			xpt_setup_ccb(&ccb_relsim.ccb_h, path, 5);
185 			ccb_relsim.ccb_h.func_code = XPT_REL_SIMQ;
186 			ccb_relsim.ccb_h.flags = CAM_DEV_QFREEZE;
187 			ccb_relsim.release_flags = RELSIM_ADJUST_OPENINGS;
188 			ccb_relsim.openings =
189 			    scif_remote_device_get_max_queue_depth(remote_device);
190 			xpt_action((union ccb *)&ccb_relsim);
191 			xpt_free_path(path);
192 			ccb->ccb_h.status |= CAM_REQUEUE_REQ;
193 		}
194 		break;
195 
196 	case SCI_IO_FAILURE:
197 	case SCI_IO_FAILURE_REQUIRES_SCSI_ABORT:
198 	case SCI_IO_FAILURE_UNSUPPORTED_PROTOCOL:
199 	case SCI_IO_FAILURE_PROTOCOL_VIOLATION:
200 	case SCI_IO_FAILURE_INVALID_PARAMETER_VALUE:
201 	case SCI_IO_FAILURE_CONTROLLER_SPECIFIC_ERR:
202 	default:
203 		isci_log_message(1, "ISCI",
204 		    "isci: bus=%x target=%x lun=%x cdb[0]=%x completion status=%x\n",
205 		    ccb->ccb_h.path_id, ccb->ccb_h.target_id,
206 		    ccb->ccb_h.target_lun, ccb->csio.cdb_io.cdb_bytes[0],
207 		    completion_status);
208 		ccb->ccb_h.status |= CAM_REQ_CMP_ERR;
209 		break;
210 	}
211 
212 	if (ccb->ccb_h.status != CAM_REQ_CMP) {
213 		/* ccb will be completed with some type of non-success
214 		 *  status.  So temporarily freeze the queue until the
215 		 *  upper layers can act on the status.  The CAM_DEV_QFRZN
216 		 *  flag will then release the queue after the status is
217 		 *  acted upon.
218 		 */
219 		ccb->ccb_h.status |= CAM_DEV_QFRZN;
220 		xpt_freeze_devq(ccb->ccb_h.path, 1);
221 	}
222 
223 	callout_stop(&isci_request->parent.timer);
224 	bus_dmamap_sync(isci_request->parent.dma_tag,
225 	    isci_request->parent.dma_map,
226 	    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
227 
228 	bus_dmamap_unload(isci_request->parent.dma_tag,
229 	    isci_request->parent.dma_map);
230 
231 	if (isci_remote_device->frozen_lun_mask != 0 &&
232 	    !(ccb->ccb_h.status & CAM_REQUEUE_REQ))
233 		isci_remote_device_release_device_queue(isci_remote_device);
234 
235 	xpt_done(ccb);
236 	isci_request->ccb = NULL;
237 
238 	if (isci_controller->is_frozen == TRUE) {
239 		isci_controller->is_frozen = FALSE;
240 		xpt_release_simq(isci_controller->sim, TRUE);
241 	}
242 
243 	sci_pool_put(isci_controller->request_pool,
244 	    (struct ISCI_REQUEST *)isci_request);
245 }
246 
247 /**
248  * @brief This callback method asks the user to provide the physical
249  *        address for the supplied virtual address when building an
250  *        io request object.
251  *
252  * @param[in] controller This parameter is the core controller object
253  *            handle.
254  * @param[in] io_request This parameter is the io request object handle
255  *            for which the physical address is being requested.
256  * @param[in] virtual_address This paramter is the virtual address which
257  *            is to be returned as a physical address.
258  * @param[out] physical_address The physical address for the supplied virtual
259  *             address.
260  *
261  * @return None.
262  */
263 void
264 scic_cb_io_request_get_physical_address(SCI_CONTROLLER_HANDLE_T	controller,
265     SCI_IO_REQUEST_HANDLE_T io_request, void *virtual_address,
266     SCI_PHYSICAL_ADDRESS *physical_address)
267 {
268 	SCI_IO_REQUEST_HANDLE_T scif_request =
269 	    sci_object_get_association(io_request);
270 	struct ISCI_REQUEST *isci_request =
271 	    sci_object_get_association(scif_request);
272 
273 	if(isci_request != NULL) {
274 		/* isci_request is not NULL, meaning this is a request initiated
275 		 *  by CAM or the isci layer (i.e. device reset for I/O
276 		 *  timeout).  Therefore we can calculate the physical address
277 		 *  based on the address we stored in the struct ISCI_REQUEST
278 		 *  object.
279 		 */
280 		*physical_address = isci_request->physical_address +
281 		    (uintptr_t)virtual_address -
282 		    (uintptr_t)isci_request;
283 	} else {
284 		/* isci_request is NULL, meaning this is a request generated
285 		 *  internally by SCIL (i.e. for SMP requests or NCQ error
286 		 *  recovery).  Therefore we calculate the physical address
287 		 *  based on the controller's uncached controller memory buffer,
288 		 *  since we know that this is what SCIL uses for internal
289 		 *  framework requests.
290 		 */
291 		SCI_CONTROLLER_HANDLE_T scif_controller =
292 		    (SCI_CONTROLLER_HANDLE_T) sci_object_get_association(controller);
293 		struct ISCI_CONTROLLER *isci_controller =
294 		    (struct ISCI_CONTROLLER *)sci_object_get_association(scif_controller);
295 		U64 virt_addr_offset = (uintptr_t)virtual_address -
296 		    (U64)isci_controller->uncached_controller_memory.virtual_address;
297 
298 		*physical_address =
299 		    isci_controller->uncached_controller_memory.physical_address
300 		    + virt_addr_offset;
301 	}
302 }
303 
304 /**
305  * @brief This callback method asks the user to provide the address for
306  *        the command descriptor block (CDB) associated with this IO request.
307  *
308  * @param[in] scif_user_io_request This parameter points to the user's
309  *            IO request object.  It is a cookie that allows the user to
310  *            provide the necessary information for this callback.
311  *
312  * @return This method returns the virtual address of the CDB.
313  */
314 void *
315 scif_cb_io_request_get_cdb_address(void * scif_user_io_request)
316 {
317 	struct ISCI_IO_REQUEST *isci_request =
318 	    (struct ISCI_IO_REQUEST *)scif_user_io_request;
319 
320 	return (isci_request->ccb->csio.cdb_io.cdb_bytes);
321 }
322 
323 /**
324  * @brief This callback method asks the user to provide the length of
325  *        the command descriptor block (CDB) associated with this IO request.
326  *
327  * @param[in] scif_user_io_request This parameter points to the user's
328  *            IO request object.  It is a cookie that allows the user to
329  *            provide the necessary information for this callback.
330  *
331  * @return This method returns the length of the CDB.
332  */
333 uint32_t
334 scif_cb_io_request_get_cdb_length(void * scif_user_io_request)
335 {
336 	struct ISCI_IO_REQUEST *isci_request =
337 	    (struct ISCI_IO_REQUEST *)scif_user_io_request;
338 
339 	return (isci_request->ccb->csio.cdb_len);
340 }
341 
342 /**
343  * @brief This callback method asks the user to provide the Logical Unit (LUN)
344  *        associated with this IO request.
345  *
346  * @note The contents of the value returned from this callback are defined
347  *       by the protocol standard (e.g. T10 SAS specification).  Please
348  *       refer to the transport command information unit description
349  *       in the associated standard.
350  *
351  * @param[in] scif_user_io_request This parameter points to the user's
352  *            IO request object.  It is a cookie that allows the user to
353  *            provide the necessary information for this callback.
354  *
355  * @return This method returns the LUN associated with this request.
356  */
357 uint32_t
358 scif_cb_io_request_get_lun(void * scif_user_io_request)
359 {
360 	struct ISCI_IO_REQUEST *isci_request =
361 	    (struct ISCI_IO_REQUEST *)scif_user_io_request;
362 
363 	return (isci_request->ccb->ccb_h.target_lun);
364 }
365 
366 /**
367  * @brief This callback method asks the user to provide the task attribute
368  *        associated with this IO request.
369  *
370  * @note The contents of the value returned from this callback are defined
371  *       by the protocol standard (e.g. T10 SAS specification).  Please
372  *       refer to the transport command information unit description
373  *       in the associated standard.
374  *
375  * @param[in] scif_user_io_request This parameter points to the user's
376  *            IO request object.  It is a cookie that allows the user to
377  *            provide the necessary information for this callback.
378  *
379  * @return This method returns the task attribute associated with this
380  *         IO request.
381  */
382 uint32_t
383 scif_cb_io_request_get_task_attribute(void * scif_user_io_request)
384 {
385 	struct ISCI_IO_REQUEST *isci_request =
386 	    (struct ISCI_IO_REQUEST *)scif_user_io_request;
387 	uint32_t task_attribute;
388 
389 	if((isci_request->ccb->ccb_h.flags & CAM_TAG_ACTION_VALID) != 0)
390 		switch(isci_request->ccb->csio.tag_action) {
391 		case MSG_HEAD_OF_Q_TAG:
392 			task_attribute = SCI_SAS_HEAD_OF_QUEUE_ATTRIBUTE;
393 			break;
394 
395 		case MSG_ORDERED_Q_TAG:
396 			task_attribute = SCI_SAS_ORDERED_ATTRIBUTE;
397 			break;
398 
399 		case MSG_ACA_TASK:
400 			task_attribute = SCI_SAS_ACA_ATTRIBUTE;
401 			break;
402 
403 		default:
404 			task_attribute = SCI_SAS_SIMPLE_ATTRIBUTE;
405 			break;
406 		}
407 	else
408 		task_attribute = SCI_SAS_SIMPLE_ATTRIBUTE;
409 
410 	return (task_attribute);
411 }
412 
413 /**
414  * @brief This callback method asks the user to provide the command priority
415  *        associated with this IO request.
416  *
417  * @note The contents of the value returned from this callback are defined
418  *       by the protocol standard (e.g. T10 SAS specification).  Please
419  *       refer to the transport command information unit description
420  *       in the associated standard.
421  *
422  * @param[in] scif_user_io_request This parameter points to the user's
423  *            IO request object.  It is a cookie that allows the user to
424  *            provide the necessary information for this callback.
425  *
426  * @return This method returns the command priority associated with this
427  *         IO request.
428  */
429 uint32_t
430 scif_cb_io_request_get_command_priority(void * scif_user_io_request)
431 {
432 	return (0);
433 }
434 
435 /**
436  * @brief This method simply returns the virtual address associated
437  *        with the scsi_io and byte_offset supplied parameters.
438  *
439  * @note This callback is not utilized in the fast path.  The expectation
440  *       is that this method is utilized for items such as SCSI to ATA
441  *       translation for commands like INQUIRY, READ CAPACITY, etc.
442  *
443  * @param[in] scif_user_io_request This parameter points to the user's
444  *            IO request object.  It is a cookie that allows the user to
445  *            provide the necessary information for this callback.
446  * @param[in] byte_offset This parameter specifies the offset into the data
447  *            buffers pointed to by the SGL.  The byte offset starts at 0
448  *            and continues until the last byte pointed to be the last SGL
449  *            element.
450  *
451  * @return A virtual address pointer to the location specified by the
452  *         parameters.
453  */
454 uint8_t *
455 scif_cb_io_request_get_virtual_address_from_sgl(void * scif_user_io_request,
456     uint32_t byte_offset)
457 {
458 	struct ISCI_IO_REQUEST *isci_request =
459 	    (struct ISCI_IO_REQUEST *)scif_user_io_request;
460 
461 	return (isci_request->ccb->csio.data_ptr + byte_offset);
462 }
463 
464 /**
465  * @brief This callback method asks the user to provide the number of
466  *        bytes to be transfered as part of this request.
467  *
468  * @param[in] scif_user_io_request This parameter points to the user's
469  *            IO request object.  It is a cookie that allows the user to
470  *            provide the necessary information for this callback.
471  *
472  * @return This method returns the number of payload data bytes to be
473  *         transfered for this IO request.
474  */
475 uint32_t
476 scif_cb_io_request_get_transfer_length(void * scif_user_io_request)
477 {
478 	struct ISCI_IO_REQUEST *isci_request =
479 	    (struct ISCI_IO_REQUEST *)scif_user_io_request;
480 
481 	return (isci_request->ccb->csio.dxfer_len);
482 
483 }
484 
485 /**
486  * @brief This callback method asks the user to provide the data direction
487  *        for this request.
488  *
489  * @param[in] scif_user_io_request This parameter points to the user's
490  *            IO request object.  It is a cookie that allows the user to
491  *            provide the necessary information for this callback.
492  *
493  * @return This method returns the value of SCI_IO_REQUEST_DATA_OUT,
494  *         SCI_IO_REQUEST_DATA_IN, or SCI_IO_REQUEST_NO_DATA.
495  */
496 SCI_IO_REQUEST_DATA_DIRECTION
497 scif_cb_io_request_get_data_direction(void * scif_user_io_request)
498 {
499 	struct ISCI_IO_REQUEST *isci_request =
500 	    (struct ISCI_IO_REQUEST *)scif_user_io_request;
501 
502 	switch (isci_request->ccb->ccb_h.flags & CAM_DIR_MASK) {
503 	case CAM_DIR_IN:
504 		return (SCI_IO_REQUEST_DATA_IN);
505 	case CAM_DIR_OUT:
506 		return (SCI_IO_REQUEST_DATA_OUT);
507 	default:
508 		return (SCI_IO_REQUEST_NO_DATA);
509 	}
510 }
511 
512 /**
513  * @brief This callback method asks the user to provide the address
514  *        to where the next Scatter-Gather Element is located.
515  *
516  * Details regarding usage:
517  *   - Regarding the first SGE: the user should initialize an index,
518  *     or a pointer, prior to construction of the request that will
519  *     reference the very first scatter-gather element.  This is
520  *     important since this method is called for every scatter-gather
521  *     element, including the first element.
522  *   - Regarding the last SGE: the user should return NULL from this
523  *     method when this method is called and the SGL has exhausted
524  *     all elements.
525  *
526  * @param[in] scif_user_io_request This parameter points to the user's
527  *            IO request object.  It is a cookie that allows the user to
528  *            provide the necessary information for this callback.
529  * @param[in] current_sge_address This parameter specifies the address for
530  *            the current SGE (i.e. the one that has just processed).
531  * @param[out] next_sge An address specifying the location for the next scatter
532  *             gather element to be processed.
533  *
534  * @return None.
535  */
536 void
537 scif_cb_io_request_get_next_sge(void * scif_user_io_request,
538     void * current_sge_address, void ** next_sge)
539 {
540 	struct ISCI_IO_REQUEST *isci_request =
541 	    (struct ISCI_IO_REQUEST *)scif_user_io_request;
542 
543 	if (isci_request->current_sge_index == isci_request->num_segments)
544 		*next_sge = NULL;
545 	else {
546 		bus_dma_segment_t *sge =
547 		    &isci_request->sge[isci_request->current_sge_index];
548 
549 		isci_request->current_sge_index++;
550 		*next_sge = sge;
551 	}
552 }
553 
554 /**
555  * @brief This callback method asks the user to provide the contents of the
556  *        "address" field in the Scatter-Gather Element.
557  *
558  * @param[in] scif_user_io_request This parameter points to the user's
559  *            IO request object.  It is a cookie that allows the user to
560  *            provide the necessary information for this callback.
561  * @param[in] sge_address This parameter specifies the address for the
562  *            SGE from which to retrieve the address field.
563  *
564  * @return A physical address specifying the contents of the SGE's address
565  *         field.
566  */
567 SCI_PHYSICAL_ADDRESS
568 scif_cb_sge_get_address_field(void *scif_user_io_request, void *sge_address)
569 {
570 	bus_dma_segment_t *sge = (bus_dma_segment_t *)sge_address;
571 
572 	return ((SCI_PHYSICAL_ADDRESS)sge->ds_addr);
573 }
574 
575 /**
576  * @brief This callback method asks the user to provide the contents of the
577  *        "length" field in the Scatter-Gather Element.
578  *
579  * @param[in] scif_user_io_request This parameter points to the user's
580  *            IO request object.  It is a cookie that allows the user to
581  *            provide the necessary information for this callback.
582  * @param[in] sge_address This parameter specifies the address for the
583  *            SGE from which to retrieve the address field.
584  *
585  * @return This method returns the length field specified inside the SGE
586  *         referenced by the sge_address parameter.
587  */
588 uint32_t
589 scif_cb_sge_get_length_field(void *scif_user_io_request, void *sge_address)
590 {
591 	bus_dma_segment_t *sge = (bus_dma_segment_t *)sge_address;
592 
593 	return ((uint32_t)sge->ds_len);
594 }
595 
596 void
597 isci_request_construct(struct ISCI_REQUEST *request,
598     SCI_CONTROLLER_HANDLE_T scif_controller_handle,
599     bus_dma_tag_t io_buffer_dma_tag, bus_addr_t physical_address)
600 {
601 
602 	request->controller_handle = scif_controller_handle;
603 	request->dma_tag = io_buffer_dma_tag;
604 	request->physical_address = physical_address;
605 	bus_dmamap_create(request->dma_tag, 0, &request->dma_map);
606 	callout_init(&request->timer, CALLOUT_MPSAFE);
607 }
608 
609 static void
610 isci_io_request_construct(void *arg, bus_dma_segment_t *seg, int nseg,
611     int error)
612 {
613 	union ccb *ccb;
614 	struct ISCI_IO_REQUEST *io_request = (struct ISCI_IO_REQUEST *)arg;
615 	SCI_REMOTE_DEVICE_HANDLE_T *device = io_request->parent.remote_device_handle;
616 	SCI_STATUS status;
617 
618 	io_request->num_segments = nseg;
619 	io_request->sge = seg;
620 	ccb = io_request->ccb;
621 
622 	/* XXX More cleanup is needed here */
623 	if ((nseg == 0) || (error != 0)) {
624 		ccb->ccb_h.status = CAM_REQ_INVALID;
625 		xpt_done(ccb);
626 		return;
627 	}
628 
629 	io_request->status = scif_io_request_construct(
630 	    io_request->parent.controller_handle,
631 	    io_request->parent.remote_device_handle,
632 	    SCI_CONTROLLER_INVALID_IO_TAG, (void *)io_request,
633 	    (void *)((char*)io_request + sizeof(struct ISCI_IO_REQUEST)),
634 	    &io_request->sci_object);
635 
636 	if (io_request->status != SCI_SUCCESS) {
637 		isci_io_request_complete(io_request->parent.controller_handle,
638 		    device, io_request, io_request->status);
639 		return;
640 	}
641 
642 	sci_object_set_association(io_request->sci_object, io_request);
643 
644 	bus_dmamap_sync(io_request->parent.dma_tag, io_request->parent.dma_map,
645 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
646 
647 	status = (SCI_STATUS)scif_controller_start_io(
648 	    io_request->parent.controller_handle, device,
649 	    io_request->sci_object, SCI_CONTROLLER_INVALID_IO_TAG);
650 
651 	if (status != SCI_SUCCESS) {
652 		isci_io_request_complete(io_request->parent.controller_handle,
653 		    device, io_request, status);
654 		return;
655 	}
656 
657 	if (ccb->ccb_h.timeout != CAM_TIME_INFINITY)
658 		callout_reset(&io_request->parent.timer, ccb->ccb_h.timeout,
659 		    isci_io_request_timeout, io_request);
660 }
661 
662 void
663 isci_io_request_execute_scsi_io(union ccb *ccb,
664     struct ISCI_CONTROLLER *controller)
665 {
666 	struct ccb_scsiio *csio = &ccb->csio;
667 	target_id_t target_id = ccb->ccb_h.target_id;
668 	struct ISCI_REQUEST *request;
669 	struct ISCI_IO_REQUEST *io_request;
670 	struct ISCI_REMOTE_DEVICE *device =
671 	    controller->remote_device[target_id];
672 	int error;
673 
674 	if (device == NULL) {
675 		ccb->ccb_h.status &= ~CAM_SIM_QUEUED;
676 		ccb->ccb_h.status &= ~CAM_STATUS_MASK;
677 		ccb->ccb_h.status |= CAM_DEV_NOT_THERE;
678 		xpt_done(ccb);
679 		return;
680 	}
681 
682 	if (sci_pool_empty(controller->request_pool)) {
683 		ccb->ccb_h.status &= ~CAM_SIM_QUEUED;
684 		ccb->ccb_h.status &= ~CAM_STATUS_MASK;
685 		ccb->ccb_h.status |= CAM_REQUEUE_REQ;
686 		xpt_freeze_simq(controller->sim, 1);
687 		controller->is_frozen = TRUE;
688 		xpt_done(ccb);
689 		return;
690 	}
691 
692 	ASSERT(device->is_resetting == FALSE);
693 
694 	sci_pool_get(controller->request_pool, request);
695 	io_request = (struct ISCI_IO_REQUEST *)request;
696 
697 	io_request->ccb = ccb;
698 	io_request->current_sge_index = 0;
699 	io_request->parent.remote_device_handle = device->sci_object;
700 
701 	if ((ccb->ccb_h.flags & CAM_SCATTER_VALID) != 0)
702 		panic("Unexpected CAM_SCATTER_VALID flag!  flags = 0x%x\n",
703 		    ccb->ccb_h.flags);
704 
705 	if ((ccb->ccb_h.flags & CAM_DATA_PHYS) != 0)
706 		panic("Unexpected CAM_DATA_PHYS flag!  flags = 0x%x\n",
707 		    ccb->ccb_h.flags);
708 
709 	error = bus_dmamap_load(io_request->parent.dma_tag,
710 	    io_request->parent.dma_map, csio->data_ptr, csio->dxfer_len,
711 	    isci_io_request_construct, io_request, 0x0);
712 
713 	/* A resource shortage from BUSDMA will be automatically
714 	 * continued at a later point, pushing the CCB processing
715 	 * forward, which will in turn unfreeze the simq.
716 	 */
717 	if (error == EINPROGRESS) {
718 		xpt_freeze_simq(controller->sim, 1);
719 		ccb->ccb_h.flags |= CAM_RELEASE_SIMQ;
720 	}
721 }
722 
723 void
724 isci_io_request_timeout(void *arg)
725 {
726 	struct ISCI_IO_REQUEST *request = (struct ISCI_IO_REQUEST *)arg;
727 	struct ISCI_REMOTE_DEVICE *remote_device = (struct ISCI_REMOTE_DEVICE *)
728 		sci_object_get_association(request->parent.remote_device_handle);
729 	struct ISCI_CONTROLLER *controller = remote_device->domain->controller;
730 
731 	mtx_lock(&controller->lock);
732 	isci_remote_device_reset(remote_device, NULL);
733 	mtx_unlock(&controller->lock);
734 }
735 
736 #if __FreeBSD_version >= 900026
737 /**
738  * @brief This callback method gets the size of and pointer to the buffer
739  *         (if any) containing the request buffer for an SMP request.
740  *
741  * @param[in]  core_request This parameter specifies the SCI core's request
742  *             object associated with the SMP request.
743  * @param[out] smp_request_buffer This parameter returns a pointer to the
744  *             payload portion of the SMP request - i.e. everything after
745  *             the SMP request header.
746  *
747  * @return Size of the request buffer in bytes.  This does *not* include
748  *          the size of the SMP request header.
749  */
750 static uint32_t
751 smp_io_request_cb_get_request_buffer(SCI_IO_REQUEST_HANDLE_T core_request,
752     uint8_t ** smp_request_buffer)
753 {
754 	struct ISCI_IO_REQUEST *isci_request = (struct ISCI_IO_REQUEST *)
755 	    sci_object_get_association(sci_object_get_association(core_request));
756 
757 	*smp_request_buffer = isci_request->ccb->smpio.smp_request +
758 	    sizeof(SMP_REQUEST_HEADER_T);
759 
760 	return (isci_request->ccb->smpio.smp_request_len -
761 	    sizeof(SMP_REQUEST_HEADER_T));
762 }
763 
764 /**
765  * @brief This callback method gets the SMP function for an SMP request.
766  *
767  * @param[in]  core_request This parameter specifies the SCI core's request
768  *             object associated with the SMP request.
769  *
770  * @return SMP function for the SMP request.
771  */
772 static uint8_t
773 smp_io_request_cb_get_function(SCI_IO_REQUEST_HANDLE_T core_request)
774 {
775 	struct ISCI_IO_REQUEST *isci_request = (struct ISCI_IO_REQUEST *)
776 	    sci_object_get_association(sci_object_get_association(core_request));
777 	SMP_REQUEST_HEADER_T *header =
778 	    (SMP_REQUEST_HEADER_T *)isci_request->ccb->smpio.smp_request;
779 
780 	return (header->function);
781 }
782 
783 /**
784  * @brief This callback method gets the SMP frame type for an SMP request.
785  *
786  * @param[in]  core_request This parameter specifies the SCI core's request
787  *             object associated with the SMP request.
788  *
789  * @return SMP frame type for the SMP request.
790  */
791 static uint8_t
792 smp_io_request_cb_get_frame_type(SCI_IO_REQUEST_HANDLE_T core_request)
793 {
794 	struct ISCI_IO_REQUEST *isci_request = (struct ISCI_IO_REQUEST *)
795 	    sci_object_get_association(sci_object_get_association(core_request));
796 	SMP_REQUEST_HEADER_T *header =
797 	    (SMP_REQUEST_HEADER_T *)isci_request->ccb->smpio.smp_request;
798 
799 	return (header->smp_frame_type);
800 }
801 
802 /**
803  * @brief This callback method gets the allocated response length for an SMP request.
804  *
805  * @param[in]  core_request This parameter specifies the SCI core's request
806  *             object associated with the SMP request.
807  *
808  * @return Allocated response length for the SMP request.
809  */
810 static uint8_t
811 smp_io_request_cb_get_allocated_response_length(
812     SCI_IO_REQUEST_HANDLE_T core_request)
813 {
814 	struct ISCI_IO_REQUEST *isci_request = (struct ISCI_IO_REQUEST *)
815 	    sci_object_get_association(sci_object_get_association(core_request));
816 	SMP_REQUEST_HEADER_T *header =
817 	    (SMP_REQUEST_HEADER_T *)isci_request->ccb->smpio.smp_request;
818 
819 	return (header->allocated_response_length);
820 }
821 
822 static SCI_STATUS
823 isci_smp_request_construct(struct ISCI_IO_REQUEST *request)
824 {
825 	SCI_STATUS status;
826 	SCIC_SMP_PASSTHRU_REQUEST_CALLBACKS_T callbacks;
827 
828 	status = scif_request_construct(request->parent.controller_handle,
829 	    request->parent.remote_device_handle, SCI_CONTROLLER_INVALID_IO_TAG,
830 	    (void *)request,
831 	    (void *)((char*)request + sizeof(struct ISCI_IO_REQUEST)),
832 	    &request->sci_object);
833 
834 	if (status == SCI_SUCCESS) {
835 		callbacks.scic_cb_smp_passthru_get_request =
836 		    &smp_io_request_cb_get_request_buffer;
837 		callbacks.scic_cb_smp_passthru_get_function =
838 		    &smp_io_request_cb_get_function;
839 		callbacks.scic_cb_smp_passthru_get_frame_type =
840 		    &smp_io_request_cb_get_frame_type;
841 		callbacks.scic_cb_smp_passthru_get_allocated_response_length =
842 		    &smp_io_request_cb_get_allocated_response_length;
843 
844 		/* create the smp passthrough part of the io request */
845 		status = scic_io_request_construct_smp_pass_through(
846 		    scif_io_request_get_scic_handle(request->sci_object),
847 		    &callbacks);
848 	}
849 
850 	return (status);
851 }
852 
853 void
854 isci_io_request_execute_smp_io(union ccb *ccb,
855     struct ISCI_CONTROLLER *controller)
856 {
857 	SCI_STATUS status;
858 	target_id_t target_id = ccb->ccb_h.target_id;
859 	struct ISCI_REQUEST *request;
860 	struct ISCI_IO_REQUEST *io_request;
861 	SCI_REMOTE_DEVICE_HANDLE_T smp_device_handle;
862 	struct ISCI_REMOTE_DEVICE *end_device = controller->remote_device[target_id];
863 
864 	/* SMP commands are sent to an end device, because SMP devices are not
865 	 *  exposed to the kernel.  It is our responsibility to use this method
866 	 *  to get the SMP device that contains the specified end device.  If
867 	 *  the device is direct-attached, the handle will come back NULL, and
868 	 *  we'll just fail the SMP_IO with DEV_NOT_THERE.
869 	 */
870 	scif_remote_device_get_containing_device(end_device->sci_object,
871 	    &smp_device_handle);
872 
873 	if (smp_device_handle == NULL) {
874 		ccb->ccb_h.status &= ~CAM_SIM_QUEUED;
875 		ccb->ccb_h.status &= ~CAM_STATUS_MASK;
876 		ccb->ccb_h.status |= CAM_DEV_NOT_THERE;
877 		xpt_done(ccb);
878 		return;
879 	}
880 
881 	if (sci_pool_empty(controller->request_pool)) {
882 		ccb->ccb_h.status &= ~CAM_SIM_QUEUED;
883 		ccb->ccb_h.status &= ~CAM_STATUS_MASK;
884 		ccb->ccb_h.status |= CAM_REQUEUE_REQ;
885 		xpt_freeze_simq(controller->sim, 1);
886 		controller->is_frozen = TRUE;
887 		xpt_done(ccb);
888 		return;
889 	}
890 
891 	ASSERT(device->is_resetting == FALSE);
892 
893 	sci_pool_get(controller->request_pool, request);
894 	io_request = (struct ISCI_IO_REQUEST *)request;
895 
896 	io_request->ccb = ccb;
897 	io_request->parent.remote_device_handle = smp_device_handle;
898 
899 	status = isci_smp_request_construct(io_request);
900 
901 	if (status != SCI_SUCCESS) {
902 		isci_io_request_complete(controller->scif_controller_handle,
903 		    smp_device_handle, io_request, status);
904 		return;
905 	}
906 
907 	sci_object_set_association(io_request->sci_object, io_request);
908 
909 	status = (SCI_STATUS) scif_controller_start_io(
910 	    controller->scif_controller_handle, smp_device_handle,
911 	    io_request->sci_object, SCI_CONTROLLER_INVALID_IO_TAG);
912 
913 	if (status != SCI_SUCCESS) {
914 		isci_io_request_complete(controller->scif_controller_handle,
915 		    smp_device_handle, io_request, status);
916 		return;
917 	}
918 
919 	if (ccb->ccb_h.timeout != CAM_TIME_INFINITY)
920 		callout_reset(&io_request->parent.timer, ccb->ccb_h.timeout,
921 		    isci_io_request_timeout, request);
922 }
923 #endif
924