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