2 * Copyright (c) 2017 Broadcom. All rights reserved.
3 * The term "Broadcom" refers to Broadcom Limited and/or its subsidiaries.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright notice,
12 * this list of conditions and the following disclaimer in the documentation
13 * and/or other materials provided with the distribution.
15 * 3. Neither the name of the copyright holder nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
23 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
35 * @defgroup scsi_api_target SCSI Target API
36 * @defgroup scsi_api_initiator SCSI Initiator API
37 * @defgroup cam_api Common Access Method (CAM) API
38 * @defgroup cam_io CAM IO
43 * Provides CAM functionality.
48 #include "ocs_device.h"
50 /* Default IO timeout value for initiators is 30 seconds */
51 #define OCS_CAM_IO_TIMEOUT 30
58 } ocs_dmamap_load_arg_t;
60 static void ocs_action(struct cam_sim *, union ccb *);
61 static void ocs_poll(struct cam_sim *);
63 static ocs_tgt_resource_t *ocs_tgt_resource_get(ocs_fcport *,
64 struct ccb_hdr *, uint32_t *);
65 static int32_t ocs_tgt_resource_abort(struct ocs_softc *, ocs_tgt_resource_t *);
66 static uint32_t ocs_abort_initiator_io(struct ocs_softc *ocs, union ccb *accb);
67 static void ocs_abort_inot(struct ocs_softc *ocs, union ccb *ccb);
68 static void ocs_abort_atio(struct ocs_softc *ocs, union ccb *ccb);
69 static int32_t ocs_target_tmf_cb(ocs_io_t *, ocs_scsi_io_status_e, uint32_t, void *);
70 static int32_t ocs_io_abort_cb(ocs_io_t *, ocs_scsi_io_status_e, uint32_t, void *);
71 static int32_t ocs_task_set_full_or_busy(ocs_io_t *io);
72 static int32_t ocs_initiator_tmf_cb(ocs_io_t *, ocs_scsi_io_status_e,
73 ocs_scsi_cmd_resp_t *, uint32_t, void *);
75 ocs_fcp_change_role(struct ocs_softc *ocs, ocs_fcport *fcp, uint32_t new_role);
77 static void ocs_ldt(void *arg);
78 static void ocs_ldt_task(void *arg, int pending);
79 static void ocs_delete_target(ocs_t *ocs, ocs_fcport *fcp, int tgt);
80 uint32_t ocs_add_new_tgt(ocs_node_t *node, ocs_fcport *fcp);
81 uint32_t ocs_update_tgt(ocs_node_t *node, ocs_fcport *fcp, uint32_t tgt_id);
83 int32_t ocs_tgt_find(ocs_fcport *fcp, ocs_node_t *node);
85 static inline ocs_io_t *ocs_scsi_find_io(struct ocs_softc *ocs, uint32_t tag)
88 return ocs_io_get_instance(ocs, tag);
91 static inline void ocs_target_io_free(ocs_io_t *io)
93 io->tgt_io.state = OCS_CAM_IO_FREE;
95 io->tgt_io.app = NULL;
96 ocs_scsi_io_complete(io);
97 if(io->ocs->io_in_use != 0)
98 atomic_subtract_acq_32(&io->ocs->io_in_use, 1);
102 ocs_attach_port(ocs_t *ocs, int chan)
105 struct cam_sim *sim = NULL;
106 struct cam_path *path = NULL;
107 uint32_t max_io = ocs_scsi_get_property(ocs, OCS_SCSI_MAX_IOS);
108 ocs_fcport *fcp = FCPORT(ocs, chan);
110 if (NULL == (sim = cam_sim_alloc(ocs_action, ocs_poll,
111 device_get_name(ocs->dev), ocs,
112 device_get_unit(ocs->dev), &ocs->sim_lock,
113 max_io, max_io, ocs->devq))) {
114 device_printf(ocs->dev, "Can't allocate SIM\n");
118 mtx_lock(&ocs->sim_lock);
119 if (CAM_SUCCESS != xpt_bus_register(sim, ocs->dev, chan)) {
120 device_printf(ocs->dev, "Can't register bus %d\n", 0);
121 mtx_unlock(&ocs->sim_lock);
122 cam_sim_free(sim, FALSE);
125 mtx_unlock(&ocs->sim_lock);
127 if (CAM_REQ_CMP != xpt_create_path(&path, NULL, cam_sim_path(sim),
128 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD)) {
129 device_printf(ocs->dev, "Can't create path\n");
130 xpt_bus_deregister(cam_sim_path(sim));
131 mtx_unlock(&ocs->sim_lock);
132 cam_sim_free(sim, FALSE);
140 callout_init_mtx(&fcp->ldt, &ocs->sim_lock, 0);
141 TASK_INIT(&fcp->ltask, 1, ocs_ldt_task, fcp);
147 ocs_detach_port(ocs_t *ocs, int32_t chan)
149 ocs_fcport *fcp = NULL;
150 struct cam_sim *sim = NULL;
151 struct cam_path *path = NULL;
152 fcp = FCPORT(ocs, chan);
157 callout_drain(&fcp->ldt);
158 ocs_ldt_task(fcp, 0);
161 mtx_lock(&ocs->sim_lock);
162 ocs_tgt_resource_abort(ocs, &fcp->targ_rsrc_wildcard);
164 xpt_async(AC_LOST_DEVICE, path, NULL);
168 xpt_bus_deregister(cam_sim_path(sim));
170 cam_sim_free(sim, FALSE);
172 mtx_unlock(&ocs->sim_lock);
179 ocs_cam_attach(ocs_t *ocs)
181 struct cam_devq *devq = NULL;
183 uint32_t max_io = ocs_scsi_get_property(ocs, OCS_SCSI_MAX_IOS);
185 if (NULL == (devq = cam_simq_alloc(max_io))) {
186 device_printf(ocs->dev, "Can't allocate SIMQ\n");
192 if (mtx_initialized(&ocs->sim_lock) == 0) {
193 mtx_init(&ocs->sim_lock, "ocs_sim_lock", NULL, MTX_DEF);
196 for (i = 0; i < (ocs->num_vports + 1); i++) {
197 if (ocs_attach_port(ocs, i)) {
198 ocs_log_err(ocs, "Attach port failed for chan: %d\n", i);
203 ocs->io_high_watermark = max_io;
209 ocs_detach_port(ocs, i);
212 cam_simq_free(ocs->devq);
214 if (mtx_initialized(&ocs->sim_lock))
215 mtx_destroy(&ocs->sim_lock);
221 ocs_cam_detach(ocs_t *ocs)
225 for (i = (ocs->num_vports); i >= 0; i--) {
226 ocs_detach_port(ocs, i);
229 cam_simq_free(ocs->devq);
231 if (mtx_initialized(&ocs->sim_lock))
232 mtx_destroy(&ocs->sim_lock);
237 /***************************************************************************
238 * Functions required by SCSI base driver API
242 * @ingroup scsi_api_target
243 * @brief Attach driver to the BSD SCSI layer (a.k.a CAM)
245 * Allocates + initializes CAM related resources and attaches to the CAM
247 * @param ocs the driver instance's software context
249 * @return 0 on success, non-zero otherwise
252 ocs_scsi_tgt_new_device(ocs_t *ocs)
254 ocs->enable_task_set_full = ocs_scsi_get_property(ocs,
255 OCS_SCSI_ENABLE_TASK_SET_FULL);
256 ocs_log_debug(ocs, "task set full processing is %s\n",
257 ocs->enable_task_set_full ? "enabled" : "disabled");
263 * @ingroup scsi_api_target
264 * @brief Tears down target members of ocs structure.
266 * Called by OS code when device is removed.
268 * @param ocs pointer to ocs
270 * @return returns 0 for success, a negative error code value for failure.
273 ocs_scsi_tgt_del_device(ocs_t *ocs)
280 * @ingroup scsi_api_target
281 * @brief accept new domain notification
283 * Called by base drive when new domain is discovered. A target-server
284 * will use this call to prepare for new remote node notifications
285 * arising from ocs_scsi_new_initiator().
287 * The domain context has an element <b>ocs_scsi_tgt_domain_t tgt_domain</b>
288 * which is declared by the target-server code and is used for target-server
291 * This function will only be called if the base-driver has been enabled for
294 * Note that this call is made to target-server backends,
295 * the ocs_scsi_ini_new_domain() function is called to initiator-client backends.
297 * @param domain pointer to domain
299 * @return returns 0 for success, a negative error code value for failure.
302 ocs_scsi_tgt_new_domain(ocs_domain_t *domain)
308 * @ingroup scsi_api_target
309 * @brief accept domain lost notification
311 * Called by base-driver when a domain goes away. A target-server will
312 * use this call to clean up all domain scoped resources.
314 * Note that this call is made to target-server backends,
315 * the ocs_scsi_ini_del_domain() function is called to initiator-client backends.
317 * @param domain pointer to domain
319 * @return returns 0 for success, a negative error code value for failure.
322 ocs_scsi_tgt_del_domain(ocs_domain_t *domain)
328 * @ingroup scsi_api_target
329 * @brief accept new sli port (sport) notification
331 * Called by base drive when new sport is discovered. A target-server
332 * will use this call to prepare for new remote node notifications
333 * arising from ocs_scsi_new_initiator().
335 * The domain context has an element <b>ocs_scsi_tgt_sport_t tgt_sport</b>
336 * which is declared by the target-server code and is used for
337 * target-server private data.
339 * This function will only be called if the base-driver has been enabled for
342 * Note that this call is made to target-server backends,
343 * the ocs_scsi_tgt_new_domain() is called to initiator-client backends.
345 * @param sport pointer to SLI port
347 * @return returns 0 for success, a negative error code value for failure.
350 ocs_scsi_tgt_new_sport(ocs_sport_t *sport)
352 ocs_t *ocs = sport->ocs;
354 if(!sport->is_vport) {
355 sport->tgt_data = FCPORT(ocs, 0);
362 * @ingroup scsi_api_target
363 * @brief accept SLI port gone notification
365 * Called by base-driver when a sport goes away. A target-server will
366 * use this call to clean up all sport scoped resources.
368 * Note that this call is made to target-server backends,
369 * the ocs_scsi_ini_del_sport() is called to initiator-client backends.
371 * @param sport pointer to SLI port
373 * @return returns 0 for success, a negative error code value for failure.
376 ocs_scsi_tgt_del_sport(ocs_sport_t *sport)
382 * @ingroup scsi_api_target
383 * @brief receive notification of a new SCSI initiator node
385 * Sent by base driver to notify a target-server of the presense of a new
386 * remote initiator. The target-server may use this call to prepare for
387 * inbound IO from this node.
389 * The ocs_node_t structure has and elment of type ocs_scsi_tgt_node_t named
390 * tgt_node that is declared and used by a target-server for private
393 * This function is only called if the target capability is enabled in driver.
395 * @param node pointer to new remote initiator node
397 * @return returns 0 for success, a negative error code value for failure.
402 ocs_scsi_new_initiator(ocs_node_t *node)
404 ocs_t *ocs = node->ocs;
405 struct ac_contract ac;
406 struct ac_device_changed *adc;
408 ocs_fcport *fcp = NULL;
410 fcp = node->sport->tgt_data;
412 ocs_log_err(ocs, "FCP is NULL \n");
417 * Update the IO watermark by decrementing it by the
418 * number of IOs reserved for each initiator.
420 atomic_subtract_acq_32(&ocs->io_high_watermark, OCS_RSVD_INI_IO);
422 ac.contract_number = AC_CONTRACT_DEV_CHG;
423 adc = (struct ac_device_changed *) ac.contract_data;
424 adc->wwpn = ocs_node_get_wwpn(node);
425 adc->port = node->rnode.fc_id;
426 adc->target = node->instance_index;
428 xpt_async(AC_CONTRACT, fcp->path, &ac);
434 * @ingroup scsi_api_target
435 * @brief validate new initiator
437 * Sent by base driver to validate a remote initiatiator. The target-server
438 * returns TRUE if this initiator should be accepted.
440 * This function is only called if the target capability is enabled in driver.
442 * @param node pointer to remote initiator node to validate
444 * @return TRUE if initiator should be accepted, FALSE if it should be rejected
450 ocs_scsi_validate_initiator(ocs_node_t *node)
456 * @ingroup scsi_api_target
457 * @brief Delete a SCSI initiator node
459 * Sent by base driver to notify a target-server that a remote initiator
460 * is now gone. The base driver will have terminated all outstanding IOs
461 * and the target-server will receive appropriate completions.
463 * This function is only called if the base driver is enabled for
466 * @param node pointer node being deleted
467 * @param reason Reason why initiator is gone.
469 * @return OCS_SCSI_CALL_COMPLETE to indicate that all work was completed
474 ocs_scsi_del_initiator(ocs_node_t *node, ocs_scsi_del_initiator_reason_e reason)
476 ocs_t *ocs = node->ocs;
478 struct ac_contract ac;
479 struct ac_device_changed *adc;
480 ocs_fcport *fcp = NULL;
482 fcp = node->sport->tgt_data;
484 ocs_log_err(ocs, "FCP is NULL \n");
488 ac.contract_number = AC_CONTRACT_DEV_CHG;
489 adc = (struct ac_device_changed *) ac.contract_data;
490 adc->wwpn = ocs_node_get_wwpn(node);
491 adc->port = node->rnode.fc_id;
492 adc->target = node->instance_index;
494 xpt_async(AC_CONTRACT, fcp->path, &ac);
497 if (reason == OCS_SCSI_INITIATOR_MISSING) {
498 return OCS_SCSI_CALL_COMPLETE;
502 * Update the IO watermark by incrementing it by the
503 * number of IOs reserved for each initiator.
505 atomic_add_acq_32(&ocs->io_high_watermark, OCS_RSVD_INI_IO);
507 return OCS_SCSI_CALL_COMPLETE;
511 * @ingroup scsi_api_target
512 * @brief receive FCP SCSI Command
514 * Called by the base driver when a new SCSI command has been received. The
515 * target-server will process the command, and issue data and/or response phase
516 * requests to the base driver.
518 * The IO context (ocs_io_t) structure has and element of type
519 * ocs_scsi_tgt_io_t named tgt_io that is declared and used by
520 * a target-server for private information.
522 * @param io pointer to IO context
523 * @param lun LUN for this IO
524 * @param cdb pointer to SCSI CDB
525 * @param cdb_len length of CDB in bytes
526 * @param flags command flags
528 * @return returns 0 for success, a negative error code value for failure.
530 int32_t ocs_scsi_recv_cmd(ocs_io_t *io, uint64_t lun, uint8_t *cdb,
531 uint32_t cdb_len, uint32_t flags)
533 ocs_t *ocs = io->ocs;
534 struct ccb_accept_tio *atio = NULL;
535 ocs_node_t *node = io->node;
536 ocs_tgt_resource_t *trsrc = NULL;
538 ocs_fcport *fcp = NULL;
540 fcp = node->sport->tgt_data;
542 ocs_log_err(ocs, "FCP is NULL \n");
546 atomic_add_acq_32(&ocs->io_in_use, 1);
548 /* set target io timeout */
549 io->timeout = ocs->target_io_timer_sec;
551 if (ocs->enable_task_set_full &&
552 (ocs->io_in_use >= ocs->io_high_watermark)) {
553 return ocs_task_set_full_or_busy(io);
555 atomic_store_rel_32(&io->node->tgt_node.busy_sent, FALSE);
558 if ((lun < OCS_MAX_LUN) && fcp->targ_rsrc[lun].enabled) {
559 trsrc = &fcp->targ_rsrc[lun];
560 } else if (fcp->targ_rsrc_wildcard.enabled) {
561 trsrc = &fcp->targ_rsrc_wildcard;
565 atio = (struct ccb_accept_tio *)STAILQ_FIRST(&trsrc->atio);
570 STAILQ_REMOVE_HEAD(&trsrc->atio, sim_links.stqe);
572 atio->ccb_h.status = CAM_CDB_RECVD;
573 atio->ccb_h.target_lun = lun;
576 atio->init_id = node->instance_index;
577 atio->tag_id = io->tag;
578 atio->ccb_h.ccb_io_ptr = io;
580 if (flags & OCS_SCSI_CMD_SIMPLE)
581 atio->tag_action = MSG_SIMPLE_Q_TAG;
582 else if (flags & FCP_TASK_ATTR_HEAD_OF_QUEUE)
583 atio->tag_action = MSG_HEAD_OF_Q_TAG;
584 else if (flags & FCP_TASK_ATTR_ORDERED)
585 atio->tag_action = MSG_ORDERED_Q_TAG;
587 atio->tag_action = 0;
589 atio->cdb_len = cdb_len;
590 ocs_memcpy(atio->cdb_io.cdb_bytes, cdb, cdb_len);
592 io->tgt_io.flags = 0;
593 io->tgt_io.state = OCS_CAM_IO_COMMAND;
594 io->tgt_io.lun = lun;
596 xpt_done((union ccb *)atio);
601 ocs->dev, "%s: no ATIO for LUN %lx (en=%s) OX_ID %#x\n",
602 __func__, (unsigned long)lun,
603 trsrc ? (trsrc->enabled ? "T" : "F") : "X",
604 be16toh(io->init_task_tag));
606 io->tgt_io.state = OCS_CAM_IO_MAX;
607 ocs_target_io_free(io);
614 * @ingroup scsi_api_target
615 * @brief receive FCP SCSI Command with first burst data.
617 * Receive a new FCP SCSI command from the base driver with first burst data.
619 * @param io pointer to IO context
620 * @param lun LUN for this IO
621 * @param cdb pointer to SCSI CDB
622 * @param cdb_len length of CDB in bytes
623 * @param flags command flags
624 * @param first_burst_buffers first burst buffers
625 * @param first_burst_buffer_count The number of bytes received in the first burst
627 * @return returns 0 for success, a negative error code value for failure.
629 int32_t ocs_scsi_recv_cmd_first_burst(ocs_io_t *io, uint64_t lun, uint8_t *cdb,
630 uint32_t cdb_len, uint32_t flags,
631 ocs_dma_t first_burst_buffers[],
632 uint32_t first_burst_buffer_count)
638 * @ingroup scsi_api_target
639 * @brief receive a TMF command IO
641 * Called by the base driver when a SCSI TMF command has been received. The
642 * target-server will process the command, aborting commands as needed, and post
643 * a response using ocs_scsi_send_resp()
645 * The IO context (ocs_io_t) structure has and element of type ocs_scsi_tgt_io_t named
646 * tgt_io that is declared and used by a target-server for private information.
648 * If the target-server walks the nodes active_ios linked list, and starts IO
649 * abort processing, the code <b>must</b> be sure not to abort the IO passed into the
650 * ocs_scsi_recv_tmf() command.
652 * @param tmfio pointer to IO context
653 * @param lun logical unit value
654 * @param cmd command request
655 * @param abortio pointer to IO object to abort for TASK_ABORT (NULL for all other TMF)
658 * @return returns 0 for success, a negative error code value for failure.
660 int32_t ocs_scsi_recv_tmf(ocs_io_t *tmfio, uint64_t lun, ocs_scsi_tmf_cmd_e cmd,
661 ocs_io_t *abortio, uint32_t flags)
663 ocs_t *ocs = tmfio->ocs;
664 ocs_node_t *node = tmfio->node;
665 ocs_tgt_resource_t *trsrc = NULL;
666 struct ccb_immediate_notify *inot = NULL;
668 ocs_fcport *fcp = NULL;
670 fcp = node->sport->tgt_data;
672 ocs_log_err(ocs, "FCP is NULL \n");
676 if ((lun < OCS_MAX_LUN) && fcp->targ_rsrc[lun].enabled) {
677 trsrc = &fcp->targ_rsrc[lun];
678 } else if (fcp->targ_rsrc_wildcard.enabled) {
679 trsrc = &fcp->targ_rsrc_wildcard;
682 device_printf(tmfio->ocs->dev, "%s: io=%p cmd=%#x LU=%lx en=%s\n",
683 __func__, tmfio, cmd, (unsigned long)lun,
684 trsrc ? (trsrc->enabled ? "T" : "F") : "X");
686 inot = (struct ccb_immediate_notify *)STAILQ_FIRST(&trsrc->inot);
691 ocs->dev, "%s: no INOT for LUN %llx (en=%s) OX_ID %#x\n",
692 __func__, (unsigned long long)lun, trsrc ? (trsrc->enabled ? "T" : "F") : "X",
693 be16toh(tmfio->init_task_tag));
696 ocs_scsi_io_complete(abortio);
698 ocs_scsi_io_complete(tmfio);
699 goto ocs_scsi_recv_tmf_out;
703 tmfio->tgt_io.app = abortio;
705 STAILQ_REMOVE_HEAD(&trsrc->inot, sim_links.stqe);
707 inot->tag_id = tmfio->tag;
708 inot->seq_id = tmfio->tag;
710 if ((lun < OCS_MAX_LUN) && fcp->targ_rsrc[lun].enabled) {
711 inot->initiator_id = node->instance_index;
713 inot->initiator_id = CAM_TARGET_WILDCARD;
716 inot->ccb_h.status = CAM_MESSAGE_RECV;
717 inot->ccb_h.target_lun = lun;
720 case OCS_SCSI_TMF_ABORT_TASK:
721 inot->arg = MSG_ABORT_TASK;
722 inot->seq_id = abortio->tag;
723 device_printf(ocs->dev, "%s: ABTS IO.%#x st=%#x\n",
724 __func__, abortio->tag, abortio->tgt_io.state);
725 abortio->tgt_io.flags |= OCS_CAM_IO_F_ABORT_RECV;
726 abortio->tgt_io.flags |= OCS_CAM_IO_F_ABORT_NOTIFY;
728 case OCS_SCSI_TMF_QUERY_TASK_SET:
729 device_printf(ocs->dev,
730 "%s: OCS_SCSI_TMF_QUERY_TASK_SET not supported\n",
732 STAILQ_INSERT_TAIL(&trsrc->inot, &inot->ccb_h, sim_links.stqe);
733 ocs_scsi_io_complete(tmfio);
734 goto ocs_scsi_recv_tmf_out;
736 case OCS_SCSI_TMF_ABORT_TASK_SET:
737 inot->arg = MSG_ABORT_TASK_SET;
739 case OCS_SCSI_TMF_CLEAR_TASK_SET:
740 inot->arg = MSG_CLEAR_TASK_SET;
742 case OCS_SCSI_TMF_QUERY_ASYNCHRONOUS_EVENT:
743 inot->arg = MSG_QUERY_ASYNC_EVENT;
745 case OCS_SCSI_TMF_LOGICAL_UNIT_RESET:
746 inot->arg = MSG_LOGICAL_UNIT_RESET;
748 case OCS_SCSI_TMF_CLEAR_ACA:
749 inot->arg = MSG_CLEAR_ACA;
751 case OCS_SCSI_TMF_TARGET_RESET:
752 inot->arg = MSG_TARGET_RESET;
755 device_printf(ocs->dev, "%s: unsupported TMF %#x\n",
757 STAILQ_INSERT_TAIL(&trsrc->inot, &inot->ccb_h, sim_links.stqe);
758 goto ocs_scsi_recv_tmf_out;
763 xpt_print(inot->ccb_h.path, "%s: func=%#x stat=%#x id=%#x lun=%#x"
764 " flags=%#x tag=%#x seq=%#x ini=%#x arg=%#x\n",
765 __func__, inot->ccb_h.func_code, inot->ccb_h.status,
766 inot->ccb_h.target_id,
767 (unsigned int)inot->ccb_h.target_lun, inot->ccb_h.flags,
768 inot->tag_id, inot->seq_id, inot->initiator_id,
770 xpt_done((union ccb *)inot);
773 abortio->tgt_io.flags |= OCS_CAM_IO_F_ABORT_DEV;
774 rc = ocs_scsi_tgt_abort_io(abortio, ocs_io_abort_cb, tmfio);
777 ocs_scsi_recv_tmf_out:
782 * @ingroup scsi_api_initiator
783 * @brief Initializes any initiator fields on the ocs structure.
785 * Called by OS initialization code when a new device is discovered.
787 * @param ocs pointer to ocs
789 * @return returns 0 for success, a negative error code value for failure.
792 ocs_scsi_ini_new_device(ocs_t *ocs)
799 * @ingroup scsi_api_initiator
800 * @brief Tears down initiator members of ocs structure.
802 * Called by OS code when device is removed.
804 * @param ocs pointer to ocs
806 * @return returns 0 for success, a negative error code value for failure.
810 ocs_scsi_ini_del_device(ocs_t *ocs)
818 * @ingroup scsi_api_initiator
819 * @brief accept new domain notification
821 * Called by base drive when new domain is discovered. An initiator-client
822 * will accept this call to prepare for new remote node notifications
823 * arising from ocs_scsi_new_target().
825 * The domain context has the element <b>ocs_scsi_ini_domain_t ini_domain</b>
826 * which is declared by the initiator-client code and is used for
827 * initiator-client private data.
829 * This function will only be called if the base-driver has been enabled for
830 * initiator capability.
832 * Note that this call is made to initiator-client backends,
833 * the ocs_scsi_tgt_new_domain() function is called to target-server backends.
835 * @param domain pointer to domain
837 * @return returns 0 for success, a negative error code value for failure.
840 ocs_scsi_ini_new_domain(ocs_domain_t *domain)
846 * @ingroup scsi_api_initiator
847 * @brief accept domain lost notification
849 * Called by base-driver when a domain goes away. An initiator-client will
850 * use this call to clean up all domain scoped resources.
852 * This function will only be called if the base-driver has been enabled for
853 * initiator capability.
855 * Note that this call is made to initiator-client backends,
856 * the ocs_scsi_tgt_del_domain() function is called to target-server backends.
858 * @param domain pointer to domain
860 * @return returns 0 for success, a negative error code value for failure.
863 ocs_scsi_ini_del_domain(ocs_domain_t *domain)
868 * @ingroup scsi_api_initiator
869 * @brief accept new sli port notification
871 * Called by base drive when new sli port (sport) is discovered.
872 * A target-server will use this call to prepare for new remote node
873 * notifications arising from ocs_scsi_new_initiator().
875 * This function will only be called if the base-driver has been enabled for
878 * Note that this call is made to target-server backends,
879 * the ocs_scsi_ini_new_sport() function is called to initiator-client backends.
881 * @param sport pointer to sport
883 * @return returns 0 for success, a negative error code value for failure.
886 ocs_scsi_ini_new_sport(ocs_sport_t *sport)
888 ocs_t *ocs = sport->ocs;
890 if(!sport->is_vport) {
891 sport->tgt_data = FCPORT(ocs, 0);
898 * @ingroup scsi_api_initiator
899 * @brief accept sli port gone notification
901 * Called by base-driver when a sport goes away. A target-server will
902 * use this call to clean up all sport scoped resources.
904 * Note that this call is made to target-server backends,
905 * the ocs_scsi_ini_del_sport() function is called to initiator-client backends.
907 * @param sport pointer to SLI port
909 * @return returns 0 for success, a negative error code value for failure.
912 ocs_scsi_ini_del_sport(ocs_sport_t *sport)
917 ocs_scsi_sport_deleted(ocs_sport_t *sport)
919 ocs_t *ocs = sport->ocs;
920 ocs_fcport *fcp = NULL;
922 ocs_xport_stats_t value;
924 if (!sport->is_vport) {
928 fcp = sport->tgt_data;
930 ocs_xport_status(ocs->xport, OCS_XPORT_PORT_STATUS, &value);
932 if (value.value == 0) {
933 ocs_log_debug(ocs, "PORT offline,.. skipping\n");
937 if ((fcp->role != KNOB_ROLE_NONE)) {
938 if(fcp->vport->sport != NULL) {
939 ocs_log_debug(ocs,"sport is not NULL, skipping\n");
943 ocs_sport_vport_alloc(ocs->domain, fcp->vport);
950 ocs_tgt_find(ocs_fcport *fcp, ocs_node_t *node)
952 ocs_fc_target_t *tgt = NULL;
955 for (i = 0; i < OCS_MAX_TARGETS; i++) {
958 if (tgt->state == OCS_TGT_STATE_NONE)
961 if (ocs_node_get_wwpn(node) == tgt->wwpn) {
970 * @ingroup scsi_api_initiator
971 * @brief receive notification of a new SCSI target node
973 * Sent by base driver to notify an initiator-client of the presense of a new
974 * remote target. The initiator-server may use this call to prepare for
975 * inbound IO from this node.
977 * This function is only called if the base driver is enabled for
978 * initiator capability.
980 * @param node pointer to new remote initiator node
988 ocs_update_tgt(ocs_node_t *node, ocs_fcport *fcp, uint32_t tgt_id)
990 ocs_fc_target_t *tgt = NULL;
992 tgt = &fcp->tgt[tgt_id];
994 tgt->node_id = node->instance_index;
995 tgt->state = OCS_TGT_STATE_VALID;
997 tgt->port_id = node->rnode.fc_id;
998 tgt->wwpn = ocs_node_get_wwpn(node);
999 tgt->wwnn = ocs_node_get_wwnn(node);
1004 ocs_add_new_tgt(ocs_node_t *node, ocs_fcport *fcp)
1008 struct ocs_softc *ocs = node->ocs;
1009 union ccb *ccb = NULL;
1010 for (i = 0; i < OCS_MAX_TARGETS; i++) {
1011 if (fcp->tgt[i].state == OCS_TGT_STATE_NONE)
1015 if (NULL == (ccb = xpt_alloc_ccb_nowait())) {
1016 device_printf(ocs->dev, "%s: ccb allocation failed\n", __func__);
1020 if (CAM_REQ_CMP != xpt_create_path(&ccb->ccb_h.path, xpt_periph,
1021 cam_sim_path(fcp->sim),
1022 i, CAM_LUN_WILDCARD)) {
1024 ocs->dev, "%s: target path creation failed\n", __func__);
1029 ocs_update_tgt(node, fcp, i);
1035 ocs_scsi_new_target(ocs_node_t *node)
1037 ocs_fcport *fcp = NULL;
1040 fcp = node->sport->tgt_data;
1042 printf("%s:FCP is NULL \n", __func__);
1046 i = ocs_tgt_find(fcp, node);
1049 ocs_add_new_tgt(node, fcp);
1053 ocs_update_tgt(node, fcp, i);
1058 ocs_delete_target(ocs_t *ocs, ocs_fcport *fcp, int tgt)
1060 struct cam_path *cpath = NULL;
1063 device_printf(ocs->dev, "%s: calling with NULL sim\n", __func__);
1067 if (CAM_REQ_CMP == xpt_create_path(&cpath, NULL, cam_sim_path(fcp->sim),
1068 tgt, CAM_LUN_WILDCARD)) {
1069 xpt_async(AC_LOST_DEVICE, cpath, NULL);
1071 xpt_free_path(cpath);
1076 * Device Lost Timer Function- when we have decided that a device was lost,
1077 * we wait a specific period of time prior to telling the OS about lost device.
1079 * This timer function gets activated when the device was lost.
1080 * This function fires once a second and then scans the port database
1081 * for devices that are marked dead but still have a virtual target assigned.
1082 * We decrement a counter for that port database entry, and when it hits zero,
1083 * we tell the OS the device was lost. Timer will be stopped when the device
1084 * comes back active or removed from the OS.
1089 ocs_fcport *fcp = arg;
1090 taskqueue_enqueue(taskqueue_thread, &fcp->ltask);
1094 ocs_ldt_task(void *arg, int pending)
1096 ocs_fcport *fcp = arg;
1097 ocs_t *ocs = fcp->ocs;
1098 int i, more_to_do = 0;
1099 ocs_fc_target_t *tgt = NULL;
1101 for (i = 0; i < OCS_MAX_TARGETS; i++) {
1104 if (tgt->state != OCS_TGT_STATE_LOST) {
1108 if ((tgt->gone_timer != 0) && (ocs->attached)){
1109 tgt->gone_timer -= 1;
1114 if (tgt->is_target) {
1116 ocs_delete_target(ocs, fcp, i);
1119 tgt->state = OCS_TGT_STATE_NONE;
1123 callout_reset(&fcp->ldt, hz, ocs_ldt, fcp);
1125 callout_deactivate(&fcp->ldt);
1131 * @ingroup scsi_api_initiator
1132 * @brief Delete a SCSI target node
1134 * Sent by base driver to notify a initiator-client that a remote target
1135 * is now gone. The base driver will have terminated all outstanding IOs
1136 * and the initiator-client will receive appropriate completions.
1138 * The ocs_node_t structure has and elment of type ocs_scsi_ini_node_t named
1139 * ini_node that is declared and used by a target-server for private
1142 * This function is only called if the base driver is enabled for
1143 * initiator capability.
1145 * @param node pointer node being deleted
1146 * @param reason reason for deleting the target
1148 * @return Returns OCS_SCSI_CALL_ASYNC if target delete is queued for async
1149 * completion and OCS_SCSI_CALL_COMPLETE if call completed or error.
1154 ocs_scsi_del_target(ocs_node_t *node, ocs_scsi_del_target_reason_e reason)
1156 struct ocs_softc *ocs = node->ocs;
1157 ocs_fcport *fcp = NULL;
1158 ocs_fc_target_t *tgt = NULL;
1161 fcp = node->sport->tgt_data;
1163 ocs_log_err(ocs,"FCP is NULL \n");
1167 tgt_id = ocs_tgt_find(fcp, node);
1169 tgt = &fcp->tgt[tgt_id];
1171 // IF in shutdown delete target.
1172 if(!ocs->attached) {
1173 ocs_delete_target(ocs, fcp, tgt_id);
1176 tgt->state = OCS_TGT_STATE_LOST;
1177 tgt->gone_timer = 30;
1178 if (!callout_active(&fcp->ldt)) {
1179 callout_reset(&fcp->ldt, hz, ocs_ldt, fcp);
1187 * @brief Initialize SCSI IO
1189 * Initialize SCSI IO, this function is called once per IO during IO pool
1190 * allocation so that the target server may initialize any of its own private
1193 * @param io pointer to SCSI IO object
1195 * @return returns 0 for success, a negative error code value for failure.
1198 ocs_scsi_tgt_io_init(ocs_io_t *io)
1204 * @brief Uninitialize SCSI IO
1206 * Uninitialize target server private data in a SCSI io object
1208 * @param io pointer to SCSI IO object
1210 * @return returns 0 for success, a negative error code value for failure.
1213 ocs_scsi_tgt_io_exit(ocs_io_t *io)
1219 * @brief Initialize SCSI IO
1221 * Initialize SCSI IO, this function is called once per IO during IO pool
1222 * allocation so that the initiator client may initialize any of its own private
1225 * @param io pointer to SCSI IO object
1227 * @return returns 0 for success, a negative error code value for failure.
1230 ocs_scsi_ini_io_init(ocs_io_t *io)
1236 * @brief Uninitialize SCSI IO
1238 * Uninitialize initiator client private data in a SCSI io object
1240 * @param io pointer to SCSI IO object
1242 * @return returns 0 for success, a negative error code value for failure.
1245 ocs_scsi_ini_io_exit(ocs_io_t *io)
1250 * End of functions required by SCSI base driver API
1251 ***************************************************************************/
1253 static __inline void
1254 ocs_set_ccb_status(union ccb *ccb, cam_status status)
1256 ccb->ccb_h.status &= ~CAM_STATUS_MASK;
1257 ccb->ccb_h.status |= status;
1261 ocs_task_set_full_or_busy_cb(ocs_io_t *io, ocs_scsi_io_status_e scsi_status,
1262 uint32_t flags, void *arg)
1265 ocs_target_io_free(io);
1271 * @brief send SCSI task set full or busy status
1273 * A SCSI task set full or busy response is sent depending on whether
1274 * another IO is already active on the LUN.
1276 * @param io pointer to IO context
1278 * @return returns 0 for success, a negative error code value for failure.
1282 ocs_task_set_full_or_busy(ocs_io_t *io)
1284 ocs_scsi_cmd_resp_t rsp = { 0 };
1285 ocs_t *ocs = io->ocs;
1288 * If there is another command for the LUN, then send task set full,
1289 * if this is the first one, then send the busy status.
1291 * if 'busy sent' is FALSE, set it to TRUE and send BUSY
1292 * otherwise send FULL
1294 if (atomic_cmpset_acq_32(&io->node->tgt_node.busy_sent, FALSE, TRUE)) {
1295 rsp.scsi_status = SCSI_STATUS_BUSY; /* Busy */
1296 printf("%s: busy [%s] tag=%x iiu=%d ihw=%d\n", __func__,
1297 io->node->display_name, io->tag,
1298 io->ocs->io_in_use, io->ocs->io_high_watermark);
1300 rsp.scsi_status = SCSI_STATUS_TASK_SET_FULL; /* Task set full */
1301 printf("%s: full tag=%x iiu=%d\n", __func__, io->tag,
1302 io->ocs->io_in_use);
1305 /* Log a message here indicating a busy or task set full state */
1306 if (OCS_LOG_ENABLE_Q_FULL_BUSY_MSG(ocs)) {
1307 /* Log Task Set Full */
1308 if (rsp.scsi_status == SCSI_STATUS_TASK_SET_FULL) {
1309 /* Task Set Full Message */
1310 ocs_log_info(ocs, "OCS CAM TASK SET FULL. Tasks >= %d\n",
1311 ocs->io_high_watermark);
1313 else if (rsp.scsi_status == SCSI_STATUS_BUSY) {
1314 /* Log Busy Message */
1315 ocs_log_info(ocs, "OCS CAM SCSI BUSY\n");
1319 /* Send the response */
1321 ocs_scsi_send_resp(io, 0, &rsp, ocs_task_set_full_or_busy_cb, NULL);
1326 * @brief Process target IO completions
1329 * @param scsi_status did the IO complete successfully
1331 * @param arg application specific pointer provided in the call to ocs_target_io()
1335 static int32_t ocs_scsi_target_io_cb(ocs_io_t *io,
1336 ocs_scsi_io_status_e scsi_status,
1337 uint32_t flags, void *arg)
1339 union ccb *ccb = arg;
1340 struct ccb_scsiio *csio = &ccb->csio;
1341 struct ocs_softc *ocs = csio->ccb_h.ccb_ocs_ptr;
1342 uint32_t cam_dir = ccb->ccb_h.flags & CAM_DIR_MASK;
1343 uint32_t io_is_done =
1344 (ccb->ccb_h.flags & CAM_SEND_STATUS) == CAM_SEND_STATUS;
1346 ccb->ccb_h.status &= ~CAM_SIM_QUEUED;
1348 if (CAM_DIR_NONE != cam_dir) {
1349 bus_dmasync_op_t op;
1351 if (CAM_DIR_IN == cam_dir) {
1352 op = BUS_DMASYNC_POSTREAD;
1354 op = BUS_DMASYNC_POSTWRITE;
1356 /* Synchronize the DMA memory with the CPU and free the mapping */
1357 bus_dmamap_sync(ocs->buf_dmat, io->tgt_io.dmap, op);
1358 if (io->tgt_io.flags & OCS_CAM_IO_F_DMAPPED) {
1359 bus_dmamap_unload(ocs->buf_dmat, io->tgt_io.dmap);
1363 if (io->tgt_io.sendresp) {
1364 io->tgt_io.sendresp = 0;
1365 ocs_scsi_cmd_resp_t resp = { 0 };
1366 io->tgt_io.state = OCS_CAM_IO_RESP;
1367 resp.scsi_status = scsi_status;
1368 if (ccb->ccb_h.flags & CAM_SEND_SENSE) {
1369 resp.sense_data = (uint8_t *)&csio->sense_data;
1370 resp.sense_data_length = csio->sense_len;
1372 resp.residual = io->exp_xfer_len - io->transferred;
1374 return ocs_scsi_send_resp(io, 0, &resp, ocs_scsi_target_io_cb, ccb);
1377 switch (scsi_status) {
1378 case OCS_SCSI_STATUS_GOOD:
1379 ocs_set_ccb_status(ccb, CAM_REQ_CMP);
1381 case OCS_SCSI_STATUS_ABORTED:
1382 ocs_set_ccb_status(ccb, CAM_REQ_ABORTED);
1385 ocs_set_ccb_status(ccb, CAM_REQ_CMP_ERR);
1389 if ((io->tgt_io.flags & OCS_CAM_IO_F_ABORT_NOTIFY) == 0) {
1390 ocs_target_io_free(io);
1393 io->tgt_io.state = OCS_CAM_IO_DATA_DONE;
1394 /*device_printf(ocs->dev, "%s: CTIO state=%d tag=%#x\n",
1395 __func__, io->tgt_io.state, io->tag);*/
1404 * @note 1. Since the CCB is assigned to the ocs_io_t on an XPT_CONT_TARGET_IO
1405 * action, if an initiator aborts a command prior to the SIM receiving
1406 * a CTIO, the IO's CCB will be NULL.
1409 ocs_io_abort_cb(ocs_io_t *io, ocs_scsi_io_status_e scsi_status, uint32_t flags, void *arg)
1411 struct ocs_softc *ocs = NULL;
1412 ocs_io_t *tmfio = arg;
1413 ocs_scsi_tmf_resp_e tmf_resp = OCS_SCSI_TMF_FUNCTION_COMPLETE;
1418 io->tgt_io.flags &= ~OCS_CAM_IO_F_ABORT_DEV;
1420 /* A good status indicates the IO was aborted and will be completed in
1421 * the IO's completion handler. Handle the other cases here. */
1422 switch (scsi_status) {
1423 case OCS_SCSI_STATUS_GOOD:
1425 case OCS_SCSI_STATUS_NO_IO:
1428 device_printf(ocs->dev, "%s: unhandled status %d\n",
1429 __func__, scsi_status);
1430 tmf_resp = OCS_SCSI_TMF_FUNCTION_REJECTED;
1434 ocs_scsi_send_tmf_resp(tmfio, tmf_resp, NULL, ocs_target_tmf_cb, NULL);
1441 * @brief Process initiator IO completions
1444 * @param scsi_status did the IO complete successfully
1445 * @param rsp pointer to response buffer
1447 * @param arg application specific pointer provided in the call to ocs_target_io()
1451 static int32_t ocs_scsi_initiator_io_cb(ocs_io_t *io,
1452 ocs_scsi_io_status_e scsi_status,
1453 ocs_scsi_cmd_resp_t *rsp,
1454 uint32_t flags, void *arg)
1456 union ccb *ccb = arg;
1457 struct ccb_scsiio *csio = &ccb->csio;
1458 struct ocs_softc *ocs = csio->ccb_h.ccb_ocs_ptr;
1459 uint32_t cam_dir = ccb->ccb_h.flags & CAM_DIR_MASK;
1460 cam_status ccb_status= CAM_REQ_CMP_ERR;
1462 if (CAM_DIR_NONE != cam_dir) {
1463 bus_dmasync_op_t op;
1465 if (CAM_DIR_IN == cam_dir) {
1466 op = BUS_DMASYNC_POSTREAD;
1468 op = BUS_DMASYNC_POSTWRITE;
1470 /* Synchronize the DMA memory with the CPU and free the mapping */
1471 bus_dmamap_sync(ocs->buf_dmat, io->tgt_io.dmap, op);
1472 if (io->tgt_io.flags & OCS_CAM_IO_F_DMAPPED) {
1473 bus_dmamap_unload(ocs->buf_dmat, io->tgt_io.dmap);
1477 if (scsi_status == OCS_SCSI_STATUS_CHECK_RESPONSE) {
1478 csio->scsi_status = rsp->scsi_status;
1479 if (SCSI_STATUS_OK != rsp->scsi_status) {
1480 ccb_status = CAM_SCSI_STATUS_ERROR;
1483 csio->resid = rsp->residual;
1484 if (rsp->residual > 0) {
1485 uint32_t length = rsp->response_wire_length;
1487 if (csio->dxfer_len == (length + csio->resid)) {
1488 ccb_status = CAM_REQ_CMP;
1490 } else if (rsp->residual < 0) {
1491 ccb_status = CAM_DATA_RUN_ERR;
1494 if ((rsp->sense_data_length) &&
1495 !(ccb->ccb_h.flags & (CAM_SENSE_PHYS | CAM_SENSE_PTR))) {
1496 uint32_t sense_len = 0;
1498 ccb->ccb_h.status |= CAM_AUTOSNS_VALID;
1499 if (rsp->sense_data_length < csio->sense_len) {
1501 csio->sense_len - rsp->sense_data_length;
1502 sense_len = rsp->sense_data_length;
1504 csio->sense_resid = 0;
1505 sense_len = csio->sense_len;
1507 ocs_memcpy(&csio->sense_data, rsp->sense_data, sense_len);
1509 } else if (scsi_status != OCS_SCSI_STATUS_GOOD) {
1510 ccb_status = CAM_REQ_CMP_ERR;
1511 ocs_set_ccb_status(ccb, ccb_status);
1512 csio->ccb_h.status |= CAM_DEV_QFRZN;
1513 xpt_freeze_devq(csio->ccb_h.path, 1);
1516 ccb_status = CAM_REQ_CMP;
1519 ocs_set_ccb_status(ccb, ccb_status);
1521 ocs_scsi_io_free(io);
1523 csio->ccb_h.ccb_io_ptr = NULL;
1524 csio->ccb_h.ccb_ocs_ptr = NULL;
1525 ccb->ccb_h.status &= ~CAM_SIM_QUEUED;
1533 * @brief Load scatter-gather list entries into an IO
1535 * This routine relies on the driver instance's software context pointer and
1536 * the IO object pointer having been already assigned to hooks in the CCB.
1537 * Although the routine does not return success/fail, callers can look at the
1538 * n_sge member to determine if the mapping failed (0 on failure).
1540 * @param arg pointer to the CAM ccb for this IO
1541 * @param seg DMA address/length pairs
1542 * @param nseg number of DMA address/length pairs
1543 * @param error any errors while mapping the IO
1546 ocs_scsi_dmamap_load(void *arg, bus_dma_segment_t *seg, int nseg, int error)
1548 ocs_dmamap_load_arg_t *sglarg = (ocs_dmamap_load_arg_t*) arg;
1551 printf("%s: seg=%p nseg=%d error=%d\n",
1552 __func__, seg, nseg, error);
1558 if ((sglarg->sgl_count + nseg) > sglarg->sgl_max) {
1559 printf("%s: sgl_count=%d nseg=%d max=%d\n", __func__,
1560 sglarg->sgl_count, nseg, sglarg->sgl_max);
1565 for (i = 0, c = sglarg->sgl_count; i < nseg; i++, c++) {
1566 sglarg->sgl[c].addr = seg[i].ds_addr;
1567 sglarg->sgl[c].len = seg[i].ds_len;
1570 sglarg->sgl_count = c;
1577 * @brief Build a scatter-gather list from a CAM CCB
1579 * @param ocs the driver instance's software context
1580 * @param ccb pointer to the CCB
1581 * @param io pointer to the previously allocated IO object
1582 * @param sgl pointer to SGL
1583 * @param sgl_max number of entries in sgl
1585 * @return 0 on success, non-zero otherwise
1588 ocs_build_scsi_sgl(struct ocs_softc *ocs, union ccb *ccb, ocs_io_t *io,
1589 ocs_scsi_sgl_t *sgl, uint32_t sgl_max)
1591 ocs_dmamap_load_arg_t dmaarg;
1594 if (!ocs || !ccb || !io || !sgl) {
1595 printf("%s: bad param o=%p c=%p i=%p s=%p\n", __func__,
1600 io->tgt_io.flags &= ~OCS_CAM_IO_F_DMAPPED;
1603 dmaarg.sgl_count = 0;
1604 dmaarg.sgl_max = sgl_max;
1607 err = bus_dmamap_load_ccb(ocs->buf_dmat, io->tgt_io.dmap, ccb,
1608 ocs_scsi_dmamap_load, &dmaarg, 0);
1610 if (err || dmaarg.rc) {
1612 ocs->dev, "%s: bus_dmamap_load_ccb error (%d %d)\n",
1613 __func__, err, dmaarg.rc);
1617 io->tgt_io.flags |= OCS_CAM_IO_F_DMAPPED;
1618 return dmaarg.sgl_count;
1623 * @brief Send a target IO
1625 * @param ocs the driver instance's software context
1626 * @param ccb pointer to the CCB
1628 * @return 0 on success, non-zero otherwise
1631 ocs_target_io(struct ocs_softc *ocs, union ccb *ccb)
1633 struct ccb_scsiio *csio = &ccb->csio;
1634 ocs_io_t *io = NULL;
1635 uint32_t cam_dir = ccb->ccb_h.flags & CAM_DIR_MASK;
1636 bool sendstatus = ccb->ccb_h.flags & CAM_SEND_STATUS;
1637 uint32_t xferlen = csio->dxfer_len;
1640 io = ocs_scsi_find_io(ocs, csio->tag_id);
1642 ocs_set_ccb_status(ccb, CAM_REQ_CMP_ERR);
1643 panic("bad tag value");
1647 /* Received an ABORT TASK for this IO */
1648 if (io->tgt_io.flags & OCS_CAM_IO_F_ABORT_RECV) {
1649 /*device_printf(ocs->dev,
1650 "%s: XPT_CONT_TARGET_IO state=%d tag=%#x xid=%#x flags=%#x\n",
1651 __func__, io->tgt_io.state, io->tag, io->init_task_tag,
1652 io->tgt_io.flags);*/
1653 io->tgt_io.flags |= OCS_CAM_IO_F_ABORT_CAM;
1655 if (ccb->ccb_h.flags & CAM_SEND_STATUS) {
1656 ocs_set_ccb_status(ccb, CAM_REQ_CMP);
1657 ocs_target_io_free(io);
1661 ocs_set_ccb_status(ccb, CAM_REQ_ABORTED);
1666 io->tgt_io.app = ccb;
1668 ocs_set_ccb_status(ccb, CAM_REQ_INPROG);
1669 ccb->ccb_h.status |= CAM_SIM_QUEUED;
1671 csio->ccb_h.ccb_ocs_ptr = ocs;
1672 csio->ccb_h.ccb_io_ptr = io;
1674 if ((sendstatus && (xferlen == 0))) {
1675 ocs_scsi_cmd_resp_t resp = { 0 };
1677 ocs_assert(ccb->ccb_h.flags & CAM_SEND_STATUS, -1);
1679 io->tgt_io.state = OCS_CAM_IO_RESP;
1681 resp.scsi_status = csio->scsi_status;
1683 if (ccb->ccb_h.flags & CAM_SEND_SENSE) {
1684 resp.sense_data = (uint8_t *)&csio->sense_data;
1685 resp.sense_data_length = csio->sense_len;
1688 resp.residual = io->exp_xfer_len - io->transferred;
1689 rc = ocs_scsi_send_resp(io, 0, &resp, ocs_scsi_target_io_cb, ccb);
1691 } else if (xferlen != 0) {
1692 ocs_scsi_sgl_t sgl[OCS_FC_MAX_SGL];
1693 int32_t sgl_count = 0;
1695 io->tgt_io.state = OCS_CAM_IO_DATA;
1698 io->tgt_io.sendresp = 1;
1700 sgl_count = ocs_build_scsi_sgl(ocs, ccb, io, sgl, ARRAY_SIZE(sgl));
1701 if (sgl_count > 0) {
1702 if (cam_dir == CAM_DIR_IN) {
1703 rc = ocs_scsi_send_rd_data(io, 0, NULL, sgl,
1704 sgl_count, csio->dxfer_len,
1705 ocs_scsi_target_io_cb, ccb);
1706 } else if (cam_dir == CAM_DIR_OUT) {
1707 rc = ocs_scsi_recv_wr_data(io, 0, NULL, sgl,
1708 sgl_count, csio->dxfer_len,
1709 ocs_scsi_target_io_cb, ccb);
1711 device_printf(ocs->dev, "%s:"
1712 " unknown CAM direction %#x\n",
1714 ocs_set_ccb_status(ccb, CAM_REQ_INVALID);
1718 device_printf(ocs->dev, "%s: building SGL failed\n",
1720 ocs_set_ccb_status(ccb, CAM_REQ_CMP_ERR);
1724 device_printf(ocs->dev, "%s: Wrong value xfer and sendstatus"
1725 " are 0 \n", __func__);
1726 ocs_set_ccb_status(ccb, CAM_REQ_INVALID);
1732 ocs_set_ccb_status(ccb, CAM_REQ_CMP_ERR);
1733 ccb->ccb_h.status &= ~CAM_SIM_QUEUED;
1734 io->tgt_io.state = OCS_CAM_IO_DATA_DONE;
1735 device_printf(ocs->dev, "%s: CTIO state=%d tag=%#x\n",
1736 __func__, io->tgt_io.state, io->tag);
1737 if ((sendstatus && (xferlen == 0))) {
1738 ocs_target_io_free(io);
1746 ocs_target_tmf_cb(ocs_io_t *io, ocs_scsi_io_status_e scsi_status, uint32_t flags,
1750 /*device_printf(io->ocs->dev, "%s: tag=%x io=%p s=%#x\n",
1751 __func__, io->tag, io, scsi_status);*/
1752 ocs_scsi_io_complete(io);
1759 * @brief Send an initiator IO
1761 * @param ocs the driver instance's software context
1762 * @param ccb pointer to the CCB
1764 * @return 0 on success, non-zero otherwise
1767 ocs_initiator_io(struct ocs_softc *ocs, union ccb *ccb)
1770 struct ccb_scsiio *csio = &ccb->csio;
1771 struct ccb_hdr *ccb_h = &csio->ccb_h;
1772 ocs_node_t *node = NULL;
1773 ocs_io_t *io = NULL;
1774 ocs_scsi_sgl_t sgl[OCS_FC_MAX_SGL];
1777 ocs_fcport *fcp = NULL;
1778 fcp = FCPORT(ocs, cam_sim_bus(xpt_path_sim((ccb)->ccb_h.path)));
1780 device_printf(ocs->dev, "%s: fcp is NULL\n", __func__);
1784 if (fcp->tgt[ccb_h->target_id].state == OCS_TGT_STATE_LOST) {
1785 device_printf(ocs->dev, "%s: device LOST %d\n", __func__,
1787 return CAM_REQUEUE_REQ;
1790 if (fcp->tgt[ccb_h->target_id].state == OCS_TGT_STATE_NONE) {
1791 device_printf(ocs->dev, "%s: device not ready %d\n", __func__,
1793 return CAM_SEL_TIMEOUT;
1796 node = ocs_node_get_instance(ocs, fcp->tgt[ccb_h->target_id].node_id);
1798 device_printf(ocs->dev, "%s: no device %d\n", __func__,
1800 return CAM_SEL_TIMEOUT;
1804 device_printf(ocs->dev, "%s: not target device %d\n", __func__,
1806 return CAM_SEL_TIMEOUT;
1809 io = ocs_scsi_io_alloc(node, OCS_SCSI_IO_ROLE_ORIGINATOR);
1811 device_printf(ocs->dev, "%s: unable to alloc IO\n", __func__);
1815 /* eventhough this is INI, use target structure as ocs_build_scsi_sgl
1816 * only references the tgt_io part of an ocs_io_t */
1817 io->tgt_io.app = ccb;
1819 csio->ccb_h.ccb_ocs_ptr = ocs;
1820 csio->ccb_h.ccb_io_ptr = io;
1822 sgl_count = ocs_build_scsi_sgl(ocs, ccb, io, sgl, ARRAY_SIZE(sgl));
1823 if (sgl_count < 0) {
1824 ocs_scsi_io_free(io);
1825 device_printf(ocs->dev, "%s: building SGL failed\n", __func__);
1829 if (ccb->ccb_h.timeout == CAM_TIME_INFINITY) {
1831 } else if (ccb->ccb_h.timeout == CAM_TIME_DEFAULT) {
1832 io->timeout = OCS_CAM_IO_TIMEOUT;
1834 io->timeout = ccb->ccb_h.timeout;
1837 switch (ccb->ccb_h.flags & CAM_DIR_MASK) {
1839 rc = ocs_scsi_send_nodata_io(node, io, ccb_h->target_lun,
1840 ccb->ccb_h.flags & CAM_CDB_POINTER ?
1841 csio->cdb_io.cdb_ptr: csio->cdb_io.cdb_bytes,
1843 ocs_scsi_initiator_io_cb, ccb);
1846 rc = ocs_scsi_send_rd_io(node, io, ccb_h->target_lun,
1847 ccb->ccb_h.flags & CAM_CDB_POINTER ?
1848 csio->cdb_io.cdb_ptr: csio->cdb_io.cdb_bytes,
1851 sgl, sgl_count, csio->dxfer_len,
1852 ocs_scsi_initiator_io_cb, ccb);
1855 rc = ocs_scsi_send_wr_io(node, io, ccb_h->target_lun,
1856 ccb->ccb_h.flags & CAM_CDB_POINTER ?
1857 csio->cdb_io.cdb_ptr: csio->cdb_io.cdb_bytes,
1860 sgl, sgl_count, csio->dxfer_len,
1861 ocs_scsi_initiator_io_cb, ccb);
1864 panic("%s invalid data direction %08x\n", __func__,
1873 ocs_fcp_change_role(struct ocs_softc *ocs, ocs_fcport *fcp, uint32_t new_role)
1876 uint32_t rc = 0, was = 0, i = 0;
1877 ocs_vport_spec_t *vport = fcp->vport;
1879 for (was = 0, i = 0; i < (ocs->num_vports + 1); i++) {
1880 if (FCPORT(ocs, i)->role != KNOB_ROLE_NONE)
1885 if ((was == 0) || (vport == NULL)) {
1886 fcp->role = new_role;
1887 if (vport == NULL) {
1888 ocs->enable_ini = (new_role & KNOB_ROLE_INITIATOR)? 1:0;
1889 ocs->enable_tgt = (new_role & KNOB_ROLE_TARGET)? 1:0;
1891 vport->enable_ini = (new_role & KNOB_ROLE_INITIATOR)? 1:0;
1892 vport->enable_tgt = (new_role & KNOB_ROLE_TARGET)? 1:0;
1895 rc = ocs_xport_control(ocs->xport, OCS_XPORT_PORT_OFFLINE);
1897 ocs_log_debug(ocs, "port offline failed : %d\n", rc);
1900 rc = ocs_xport_control(ocs->xport, OCS_XPORT_PORT_ONLINE);
1902 ocs_log_debug(ocs, "port online failed : %d\n", rc);
1908 if ((fcp->role != KNOB_ROLE_NONE)){
1909 fcp->role = new_role;
1910 vport->enable_ini = (new_role & KNOB_ROLE_INITIATOR)? 1:0;
1911 vport->enable_tgt = (new_role & KNOB_ROLE_TARGET)? 1:0;
1912 /* New Sport will be created in sport deleted cb */
1913 return ocs_sport_vport_del(ocs, ocs->domain, vport->wwpn, vport->wwnn);
1916 fcp->role = new_role;
1918 vport->enable_ini = (new_role & KNOB_ROLE_INITIATOR)? 1:0;
1919 vport->enable_tgt = (new_role & KNOB_ROLE_TARGET)? 1:0;
1921 if (fcp->role != KNOB_ROLE_NONE) {
1922 return ocs_sport_vport_alloc(ocs->domain, vport);
1930 * @brief Process CAM actions
1932 * The driver supplies this routine to the CAM during intialization and
1933 * is the main entry point for processing CAM Control Blocks (CCB)
1935 * @param sim pointer to the SCSI Interface Module
1936 * @param ccb CAM control block
1939 * - populate path inquiry data via info retrieved from SLI port
1942 ocs_action(struct cam_sim *sim, union ccb *ccb)
1944 struct ocs_softc *ocs = (struct ocs_softc *)cam_sim_softc(sim);
1945 struct ccb_hdr *ccb_h = &ccb->ccb_h;
1948 bus = cam_sim_bus(sim);
1950 switch (ccb_h->func_code) {
1953 if ((ccb->ccb_h.flags & CAM_CDB_POINTER) != 0) {
1954 if ((ccb->ccb_h.flags & CAM_CDB_PHYS) != 0) {
1955 ccb->ccb_h.status = CAM_REQ_INVALID;
1961 rc = ocs_initiator_io(ocs, ccb);
1963 ocs_set_ccb_status(ccb, CAM_REQ_INPROG | CAM_SIM_QUEUED);
1966 if (rc == CAM_REQUEUE_REQ) {
1967 cam_freeze_devq(ccb->ccb_h.path);
1968 cam_release_devq(ccb->ccb_h.path, RELSIM_RELEASE_AFTER_TIMEOUT, 0, 100, 0);
1969 ccb->ccb_h.status = CAM_REQUEUE_REQ;
1974 ccb->ccb_h.status &= ~CAM_SIM_QUEUED;
1976 ocs_set_ccb_status(ccb, rc);
1978 ocs_set_ccb_status(ccb, CAM_SEL_TIMEOUT);
1985 struct ccb_pathinq *cpi = &ccb->cpi;
1986 struct ccb_pathinq_settings_fc *fc = &cpi->xport_specific.fc;
1989 ocs_xport_stats_t value;
1991 cpi->version_num = 1;
1993 cpi->protocol = PROTO_SCSI;
1994 cpi->protocol_version = SCSI_REV_SPC;
1996 if (ocs->ocs_xport == OCS_XPORT_FC) {
1997 cpi->transport = XPORT_FC;
1999 cpi->transport = XPORT_UNKNOWN;
2002 cpi->transport_version = 0;
2004 /* Set the transport parameters of the SIM */
2005 ocs_xport_status(ocs->xport, OCS_XPORT_LINK_SPEED, &value);
2006 fc->bitrate = value.value * 1000; /* speed in Mbps */
2008 wwn = *((uint64_t *)ocs_scsi_get_property_ptr(ocs, OCS_SCSI_WWPN));
2009 fc->wwpn = be64toh(wwn);
2011 wwn = *((uint64_t *)ocs_scsi_get_property_ptr(ocs, OCS_SCSI_WWNN));
2012 fc->wwnn = be64toh(wwn);
2014 if (ocs->domain && ocs->domain->attached) {
2015 fc->port = ocs->domain->sport->fc_id;
2018 if (ocs->config_tgt) {
2020 PIT_PROCESSOR | PIT_DISCONNECT | PIT_TERM_IO;
2023 cpi->hba_misc = PIM_NOBUSRESET | PIM_UNMAPPED;
2024 cpi->hba_misc |= PIM_EXTLUNS | PIM_NOSCAN;
2026 cpi->hba_inquiry = PI_TAG_ABLE;
2027 cpi->max_target = OCS_MAX_TARGETS;
2028 cpi->initiator_id = ocs->max_remote_nodes + 1;
2030 if (!ocs->enable_ini) {
2031 cpi->hba_misc |= PIM_NOINITIATOR;
2034 cpi->max_lun = OCS_MAX_LUN;
2035 cpi->bus_id = cam_sim_bus(sim);
2037 /* Need to supply a base transfer speed prior to linking up
2038 * Worst case, this would be FC 1Gbps */
2039 cpi->base_transfer_speed = 1 * 1000 * 1000;
2041 /* Calculate the max IO supported
2042 * Worst case would be an OS page per SGL entry */
2043 cpi->maxio = PAGE_SIZE *
2044 (ocs_scsi_get_property(ocs, OCS_SCSI_MAX_SGL) - 1);
2046 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
2047 strncpy(cpi->hba_vid, "Emulex", HBA_IDLEN);
2048 strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
2049 cpi->unit_number = cam_sim_unit(sim);
2051 cpi->ccb_h.status = CAM_REQ_CMP;
2055 case XPT_GET_TRAN_SETTINGS:
2057 struct ccb_trans_settings *cts = &ccb->cts;
2058 struct ccb_trans_settings_scsi *scsi = &cts->proto_specific.scsi;
2059 struct ccb_trans_settings_fc *fc = &cts->xport_specific.fc;
2060 ocs_xport_stats_t value;
2061 ocs_fcport *fcp = FCPORT(ocs, bus);
2062 ocs_node_t *fnode = NULL;
2064 if (ocs->ocs_xport != OCS_XPORT_FC) {
2065 ocs_set_ccb_status(ccb, CAM_REQ_INVALID);
2070 fnode = ocs_node_get_instance(ocs, fcp->tgt[cts->ccb_h.target_id].node_id);
2071 if (fnode == NULL) {
2072 ocs_set_ccb_status(ccb, CAM_DEV_NOT_THERE);
2077 cts->protocol = PROTO_SCSI;
2078 cts->protocol_version = SCSI_REV_SPC2;
2079 cts->transport = XPORT_FC;
2080 cts->transport_version = 2;
2082 scsi->valid = CTS_SCSI_VALID_TQ;
2083 scsi->flags = CTS_SCSI_FLAGS_TAG_ENB;
2086 ocs_xport_status(ocs->xport, OCS_XPORT_LINK_SPEED, &value);
2087 fc->bitrate = value.value * 100;
2089 fc->wwpn = ocs_node_get_wwpn(fnode);
2091 fc->wwnn = ocs_node_get_wwnn(fnode);
2093 fc->port = fnode->rnode.fc_id;
2095 fc->valid = CTS_FC_VALID_SPEED |
2100 ocs_set_ccb_status(ccb, CAM_REQ_CMP);
2104 case XPT_SET_TRAN_SETTINGS:
2105 ocs_set_ccb_status(ccb, CAM_REQ_CMP);
2109 case XPT_CALC_GEOMETRY:
2110 cam_calc_geometry(&ccb->ccg, TRUE);
2114 case XPT_GET_SIM_KNOB:
2116 struct ccb_sim_knob *knob = &ccb->knob;
2118 ocs_fcport *fcp = FCPORT(ocs, bus);
2120 if (ocs->ocs_xport != OCS_XPORT_FC) {
2121 ocs_set_ccb_status(ccb, CAM_REQ_INVALID);
2127 wwn = *((uint64_t *)ocs_scsi_get_property_ptr(ocs,
2129 knob->xport_specific.fc.wwnn = be64toh(wwn);
2131 wwn = *((uint64_t *)ocs_scsi_get_property_ptr(ocs,
2133 knob->xport_specific.fc.wwpn = be64toh(wwn);
2135 knob->xport_specific.fc.wwnn = fcp->vport->wwnn;
2136 knob->xport_specific.fc.wwpn = fcp->vport->wwpn;
2139 knob->xport_specific.fc.role = fcp->role;
2140 knob->xport_specific.fc.valid = KNOB_VALID_ADDRESS |
2143 ocs_set_ccb_status(ccb, CAM_REQ_CMP);
2147 case XPT_SET_SIM_KNOB:
2149 struct ccb_sim_knob *knob = &ccb->knob;
2150 bool role_changed = FALSE;
2151 ocs_fcport *fcp = FCPORT(ocs, bus);
2153 if (ocs->ocs_xport != OCS_XPORT_FC) {
2154 ocs_set_ccb_status(ccb, CAM_REQ_INVALID);
2159 if (knob->xport_specific.fc.valid & KNOB_VALID_ADDRESS) {
2160 device_printf(ocs->dev,
2161 "%s: XPT_SET_SIM_KNOB wwnn=%llx wwpn=%llx\n",
2163 (unsigned long long)knob->xport_specific.fc.wwnn,
2164 (unsigned long long)knob->xport_specific.fc.wwpn);
2167 if (knob->xport_specific.fc.valid & KNOB_VALID_ROLE) {
2168 switch (knob->xport_specific.fc.role) {
2169 case KNOB_ROLE_NONE:
2170 if (fcp->role != KNOB_ROLE_NONE) {
2171 role_changed = TRUE;
2174 case KNOB_ROLE_TARGET:
2175 if (fcp->role != KNOB_ROLE_TARGET) {
2176 role_changed = TRUE;
2179 case KNOB_ROLE_INITIATOR:
2180 if (fcp->role != KNOB_ROLE_INITIATOR) {
2181 role_changed = TRUE;
2184 case KNOB_ROLE_BOTH:
2185 if (fcp->role != KNOB_ROLE_BOTH) {
2186 role_changed = TRUE;
2190 device_printf(ocs->dev,
2191 "%s: XPT_SET_SIM_KNOB unsupported role: %d\n",
2192 __func__, knob->xport_specific.fc.role);
2196 device_printf(ocs->dev,
2197 "BUS:%d XPT_SET_SIM_KNOB old_role: %d new_role: %d\n",
2198 bus, fcp->role, knob->xport_specific.fc.role);
2200 ocs_fcp_change_role(ocs, fcp, knob->xport_specific.fc.role);
2206 ocs_set_ccb_status(ccb, CAM_REQ_CMP);
2212 union ccb *accb = ccb->cab.abort_ccb;
2214 switch (accb->ccb_h.func_code) {
2215 case XPT_ACCEPT_TARGET_IO:
2216 ocs_abort_atio(ocs, ccb);
2218 case XPT_IMMEDIATE_NOTIFY:
2219 ocs_abort_inot(ocs, ccb);
2222 rc = ocs_abort_initiator_io(ocs, accb);
2224 ccb->ccb_h.status = CAM_UA_ABORT;
2226 ccb->ccb_h.status = CAM_REQ_CMP;
2231 printf("abort of unknown func %#x\n",
2232 accb->ccb_h.func_code);
2233 ccb->ccb_h.status = CAM_REQ_INVALID;
2239 if (ocs_xport_control(ocs->xport, OCS_XPORT_PORT_OFFLINE) == 0) {
2240 ocs_xport_control(ocs->xport, OCS_XPORT_PORT_ONLINE);
2242 ocs_set_ccb_status(ccb, CAM_REQ_CMP);
2244 ocs_set_ccb_status(ccb, CAM_REQ_CMP_ERR);
2250 ocs_node_t *node = NULL;
2251 ocs_io_t *io = NULL;
2253 ocs_fcport *fcp = FCPORT(ocs, bus);
2255 node = ocs_node_get_instance(ocs, fcp->tgt[ccb_h->target_id].node_id);
2257 device_printf(ocs->dev, "%s: no device %d\n",
2258 __func__, ccb_h->target_id);
2259 ocs_set_ccb_status(ccb, CAM_DEV_NOT_THERE);
2264 io = ocs_scsi_io_alloc(node, OCS_SCSI_IO_ROLE_ORIGINATOR);
2266 device_printf(ocs->dev, "%s: unable to alloc IO\n",
2268 ocs_set_ccb_status(ccb, CAM_REQ_CMP_ERR);
2273 rc = ocs_scsi_send_tmf(node, io, NULL, ccb_h->target_lun,
2274 OCS_SCSI_TMF_LOGICAL_UNIT_RESET,
2275 NULL, 0, 0, /* sgl, sgl_count, length */
2276 ocs_initiator_tmf_cb, NULL/*arg*/);
2279 ocs_set_ccb_status(ccb, CAM_REQ_CMP_ERR);
2281 ocs_set_ccb_status(ccb, CAM_REQ_CMP);
2284 if (node->fcp2device) {
2285 ocs_reset_crn(node, ccb_h->target_lun);
2291 case XPT_EN_LUN: /* target support */
2293 ocs_tgt_resource_t *trsrc = NULL;
2294 uint32_t status = 0;
2295 ocs_fcport *fcp = FCPORT(ocs, bus);
2297 device_printf(ocs->dev, "XPT_EN_LUN %sable %d:%d\n",
2298 ccb->cel.enable ? "en" : "dis",
2299 ccb->ccb_h.target_id,
2300 (unsigned int)ccb->ccb_h.target_lun);
2302 trsrc = ocs_tgt_resource_get(fcp, &ccb->ccb_h, &status);
2304 trsrc->enabled = ccb->cel.enable;
2306 /* Abort all ATIO/INOT on LUN disable */
2307 if (trsrc->enabled == FALSE) {
2308 ocs_tgt_resource_abort(ocs, trsrc);
2310 STAILQ_INIT(&trsrc->atio);
2311 STAILQ_INIT(&trsrc->inot);
2313 status = CAM_REQ_CMP;
2316 ocs_set_ccb_status(ccb, status);
2321 * The flow of target IOs in CAM is:
2322 * - CAM supplies a number of CCBs to the driver used for received
2324 * - when the driver receives a command, it copies the relevant
2325 * information to the CCB and returns it to the CAM using xpt_done()
2326 * - after the target server processes the request, it creates
2327 * a new CCB containing information on how to continue the IO and
2328 * passes that to the driver
2329 * - the driver processes the "continue IO" (a.k.a CTIO) CCB
2330 * - once the IO completes, the driver returns the CTIO to the CAM
2333 case XPT_ACCEPT_TARGET_IO: /* used to inform upper layer of
2334 received CDB (a.k.a. ATIO) */
2335 case XPT_IMMEDIATE_NOTIFY: /* used to inform upper layer of other
2336 event (a.k.a. INOT) */
2338 ocs_tgt_resource_t *trsrc = NULL;
2339 uint32_t status = 0;
2340 ocs_fcport *fcp = FCPORT(ocs, bus);
2342 /*printf("XPT_%s %p\n", ccb_h->func_code == XPT_ACCEPT_TARGET_IO ?
2343 "ACCEPT_TARGET_IO" : "IMMEDIATE_NOTIFY", ccb);*/
2344 trsrc = ocs_tgt_resource_get(fcp, &ccb->ccb_h, &status);
2345 if (trsrc == NULL) {
2346 ocs_set_ccb_status(ccb, CAM_DEV_NOT_THERE);
2351 if (XPT_ACCEPT_TARGET_IO == ccb->ccb_h.func_code) {
2352 struct ccb_accept_tio *atio = NULL;
2354 atio = (struct ccb_accept_tio *)ccb;
2355 atio->init_id = 0x0badbeef;
2356 atio->tag_id = 0xdeadc0de;
2358 STAILQ_INSERT_TAIL(&trsrc->atio, &ccb->ccb_h,
2361 STAILQ_INSERT_TAIL(&trsrc->inot, &ccb->ccb_h,
2364 ccb->ccb_h.ccb_io_ptr = NULL;
2365 ccb->ccb_h.ccb_ocs_ptr = ocs;
2366 ocs_set_ccb_status(ccb, CAM_REQ_INPROG);
2368 * These actions give resources to the target driver.
2369 * If we didn't return here, this function would call
2370 * xpt_done(), signaling to the upper layers that an
2371 * IO or other event had arrived.
2375 case XPT_NOTIFY_ACKNOWLEDGE:
2377 ocs_io_t *io = NULL;
2378 ocs_io_t *abortio = NULL;
2380 /* Get the IO reference for this tag */
2381 io = ocs_scsi_find_io(ocs, ccb->cna2.tag_id);
2383 device_printf(ocs->dev,
2384 "%s: XPT_NOTIFY_ACKNOWLEDGE no IO with tag %#x\n",
2385 __func__, ccb->cna2.tag_id);
2386 ocs_set_ccb_status(ccb, CAM_REQ_CMP_ERR);
2391 abortio = io->tgt_io.app;
2393 abortio->tgt_io.flags &= ~OCS_CAM_IO_F_ABORT_NOTIFY;
2394 device_printf(ocs->dev,
2395 "%s: XPT_NOTIFY_ACK state=%d tag=%#x xid=%#x"
2396 " flags=%#x\n", __func__, abortio->tgt_io.state,
2397 abortio->tag, abortio->init_task_tag,
2398 abortio->tgt_io.flags);
2399 /* TMF response was sent in abort callback */
2401 ocs_scsi_send_tmf_resp(io,
2402 OCS_SCSI_TMF_FUNCTION_COMPLETE,
2403 NULL, ocs_target_tmf_cb, NULL);
2406 ocs_set_ccb_status(ccb, CAM_REQ_CMP);
2410 case XPT_CONT_TARGET_IO: /* continue target IO, sending data/response (a.k.a. CTIO) */
2411 if (ocs_target_io(ocs, ccb)) {
2412 device_printf(ocs->dev,
2413 "XPT_CONT_TARGET_IO failed flags=%x tag=%#x\n",
2414 ccb->ccb_h.flags, ccb->csio.tag_id);
2419 device_printf(ocs->dev, "unhandled func_code = %#x\n",
2421 ccb_h->status = CAM_REQ_INVALID;
2429 * @brief Process events
2431 * @param sim pointer to the SCSI Interface Module
2435 ocs_poll(struct cam_sim *sim)
2437 printf("%s\n", __func__);
2441 ocs_initiator_tmf_cb(ocs_io_t *io, ocs_scsi_io_status_e scsi_status,
2442 ocs_scsi_cmd_resp_t *rsp, uint32_t flags, void *arg)
2446 switch (scsi_status) {
2447 case OCS_SCSI_STATUS_GOOD:
2448 case OCS_SCSI_STATUS_NO_IO:
2450 case OCS_SCSI_STATUS_CHECK_RESPONSE:
2451 if (rsp->response_data_length == 0) {
2452 ocs_log_test(io->ocs, "check response without data?!?\n");
2457 if (rsp->response_data[3] != 0) {
2458 ocs_log_test(io->ocs, "TMF status %08x\n",
2459 be32toh(*((uint32_t *)rsp->response_data)));
2465 ocs_log_test(io->ocs, "status=%#x\n", scsi_status);
2469 ocs_scsi_io_free(io);
2475 * @brief lookup target resource structure
2477 * Arbitrarily support
2478 * - wildcard target ID + LU
2479 * - 0 target ID + non-wildcard LU
2481 * @param ocs the driver instance's software context
2482 * @param ccb_h pointer to the CCB header
2483 * @param status returned status value
2485 * @return pointer to the target resource, NULL if none available (e.g. if LU
2488 static ocs_tgt_resource_t *ocs_tgt_resource_get(ocs_fcport *fcp,
2489 struct ccb_hdr *ccb_h, uint32_t *status)
2491 target_id_t tid = ccb_h->target_id;
2492 lun_id_t lun = ccb_h->target_lun;
2494 if (CAM_TARGET_WILDCARD == tid) {
2495 if (CAM_LUN_WILDCARD != lun) {
2496 *status = CAM_LUN_INVALID;
2499 return &fcp->targ_rsrc_wildcard;
2501 if (lun < OCS_MAX_LUN) {
2502 return &fcp->targ_rsrc[lun];
2504 *status = CAM_LUN_INVALID;
2512 ocs_tgt_resource_abort(struct ocs_softc *ocs, ocs_tgt_resource_t *trsrc)
2514 union ccb *ccb = NULL;
2519 ccb = (union ccb *)STAILQ_FIRST(&trsrc->atio);
2521 STAILQ_REMOVE_HEAD(&trsrc->atio, sim_links.stqe);
2522 ccb->ccb_h.status = CAM_REQ_ABORTED;
2530 ccb = (union ccb *)STAILQ_FIRST(&trsrc->inot);
2532 STAILQ_REMOVE_HEAD(&trsrc->inot, sim_links.stqe);
2533 ccb->ccb_h.status = CAM_REQ_ABORTED;
2543 ocs_abort_atio(struct ocs_softc *ocs, union ccb *ccb)
2546 ocs_io_t *aio = NULL;
2547 ocs_tgt_resource_t *trsrc = NULL;
2548 uint32_t status = CAM_REQ_INVALID;
2549 struct ccb_hdr *cur = NULL;
2550 union ccb *accb = ccb->cab.abort_ccb;
2552 int bus = cam_sim_bus(xpt_path_sim((ccb)->ccb_h.path));
2553 ocs_fcport *fcp = FCPORT(ocs, bus);
2555 trsrc = ocs_tgt_resource_get(fcp, &accb->ccb_h, &status);
2556 if (trsrc != NULL) {
2557 STAILQ_FOREACH(cur, &trsrc->atio, sim_links.stqe) {
2558 if (cur != &accb->ccb_h)
2561 STAILQ_REMOVE(&trsrc->atio, cur, ccb_hdr,
2563 accb->ccb_h.status = CAM_REQ_ABORTED;
2565 ocs_set_ccb_status(ccb, CAM_REQ_CMP);
2570 /* if the ATIO has a valid IO pointer, CAM is telling
2571 * the driver that the ATIO (which represents the entire
2572 * exchange) has been aborted. */
2574 aio = accb->ccb_h.ccb_io_ptr;
2576 ccb->ccb_h.status = CAM_UA_ABORT;
2580 device_printf(ocs->dev,
2581 "%s: XPT_ABORT ATIO state=%d tag=%#x"
2582 " xid=%#x flags=%#x\n", __func__,
2583 aio->tgt_io.state, aio->tag,
2584 aio->init_task_tag, aio->tgt_io.flags);
2585 /* Expectations are:
2586 * - abort task was received
2587 * - already aborted IO in the DEVICE
2588 * - already received NOTIFY ACKNOWLEDGE */
2590 if ((aio->tgt_io.flags & OCS_CAM_IO_F_ABORT_RECV) == 0) {
2591 device_printf(ocs->dev, "%s: abort not received or io completed \n", __func__);
2592 ocs_set_ccb_status(ccb, CAM_REQ_CMP);
2596 aio->tgt_io.flags |= OCS_CAM_IO_F_ABORT_CAM;
2597 ocs_target_io_free(aio);
2598 ocs_set_ccb_status(ccb, CAM_REQ_CMP);
2604 ocs_abort_inot(struct ocs_softc *ocs, union ccb *ccb)
2606 ocs_tgt_resource_t *trsrc = NULL;
2607 uint32_t status = CAM_REQ_INVALID;
2608 struct ccb_hdr *cur = NULL;
2609 union ccb *accb = ccb->cab.abort_ccb;
2611 int bus = cam_sim_bus(xpt_path_sim((ccb)->ccb_h.path));
2612 ocs_fcport *fcp = FCPORT(ocs, bus);
2614 trsrc = ocs_tgt_resource_get(fcp, &accb->ccb_h, &status);
2616 STAILQ_FOREACH(cur, &trsrc->inot, sim_links.stqe) {
2617 if (cur != &accb->ccb_h)
2620 STAILQ_REMOVE(&trsrc->inot, cur, ccb_hdr,
2622 accb->ccb_h.status = CAM_REQ_ABORTED;
2624 ocs_set_ccb_status(ccb, CAM_REQ_CMP);
2629 ocs_set_ccb_status(ccb, CAM_UA_ABORT);
2634 ocs_abort_initiator_io(struct ocs_softc *ocs, union ccb *accb)
2637 ocs_node_t *node = NULL;
2638 ocs_io_t *io = NULL;
2640 struct ccb_scsiio *csio = &accb->csio;
2642 ocs_fcport *fcp = FCPORT(ocs, cam_sim_bus(xpt_path_sim((accb)->ccb_h.path)));
2643 node = ocs_node_get_instance(ocs, fcp->tgt[accb->ccb_h.target_id].node_id);
2645 device_printf(ocs->dev, "%s: no device %d\n",
2646 __func__, accb->ccb_h.target_id);
2647 ocs_set_ccb_status(accb, CAM_DEV_NOT_THERE);
2652 io = ocs_scsi_io_alloc(node, OCS_SCSI_IO_ROLE_ORIGINATOR);
2654 device_printf(ocs->dev,
2655 "%s: unable to alloc IO\n", __func__);
2656 ocs_set_ccb_status(accb, CAM_REQ_CMP_ERR);
2661 rc = ocs_scsi_send_tmf(node, io,
2662 (ocs_io_t *)csio->ccb_h.ccb_io_ptr,
2663 accb->ccb_h.target_lun,
2664 OCS_SCSI_TMF_ABORT_TASK,
2666 ocs_initiator_tmf_cb, NULL/*arg*/);
2672 ocs_scsi_ini_ddump(ocs_textbuf_t *textbuf, ocs_scsi_ddump_type_e type, void *obj)
2675 case OCS_SCSI_DDUMP_DEVICE: {
2679 case OCS_SCSI_DDUMP_DOMAIN: {
2680 //ocs_domain_t *domain = obj;
2683 case OCS_SCSI_DDUMP_SPORT: {
2684 //ocs_sport_t *sport = obj;
2687 case OCS_SCSI_DDUMP_NODE: {
2688 //ocs_node_t *node = obj;
2691 case OCS_SCSI_DDUMP_IO: {
2692 //ocs_io_t *io = obj;
2702 ocs_scsi_tgt_ddump(ocs_textbuf_t *textbuf, ocs_scsi_ddump_type_e type, void *obj)
2705 case OCS_SCSI_DDUMP_DEVICE: {
2709 case OCS_SCSI_DDUMP_DOMAIN: {
2710 //ocs_domain_t *domain = obj;
2713 case OCS_SCSI_DDUMP_SPORT: {
2714 //ocs_sport_t *sport = obj;
2717 case OCS_SCSI_DDUMP_NODE: {
2718 //ocs_node_t *node = obj;
2721 case OCS_SCSI_DDUMP_IO: {
2723 char *state_str = NULL;
2725 switch (io->tgt_io.state) {
2726 case OCS_CAM_IO_FREE:
2729 case OCS_CAM_IO_COMMAND:
2730 state_str = "COMMAND";
2732 case OCS_CAM_IO_DATA:
2735 case OCS_CAM_IO_DATA_DONE:
2736 state_str = "DATA_DONE";
2738 case OCS_CAM_IO_RESP:
2742 state_str = "xxx BAD xxx";
2744 ocs_ddump_value(textbuf, "cam_st", "%s", state_str);
2745 if (io->tgt_io.app) {
2746 ocs_ddump_value(textbuf, "cam_flags", "%#x",
2747 ((union ccb *)(io->tgt_io.app))->ccb_h.flags);
2748 ocs_ddump_value(textbuf, "cam_status", "%#x",
2749 ((union ccb *)(io->tgt_io.app))->ccb_h.status);
2760 int32_t ocs_scsi_get_block_vaddr(ocs_io_t *io, uint64_t blocknumber,
2761 ocs_scsi_vaddr_len_t addrlen[],
2762 uint32_t max_addrlen, void **dif_vaddr)
2768 ocs_get_crn(ocs_node_t *node, uint8_t *crn, uint64_t lun)
2771 struct ocs_lun_crn *lcrn = NULL;
2772 idx = lun % OCS_MAX_LUN;
2774 lcrn = node->ini_node.lun_crn[idx];
2777 lcrn = ocs_malloc(node->ocs, sizeof(struct ocs_lun_crn),
2784 node->ini_node.lun_crn[idx] = lcrn;
2787 if (lcrn->lun != lun) {
2791 if (lcrn->crnseed == 0)
2794 *crn = lcrn->crnseed++;
2799 ocs_del_crn(ocs_node_t *node)
2802 struct ocs_lun_crn *lcrn = NULL;
2804 for(i = 0; i < OCS_MAX_LUN; i++) {
2805 lcrn = node->ini_node.lun_crn[i];
2807 ocs_free(node->ocs, lcrn, sizeof(*lcrn));
2815 ocs_reset_crn(ocs_node_t *node, uint64_t lun)
2818 struct ocs_lun_crn *lcrn = NULL;
2819 idx = lun % OCS_MAX_LUN;
2821 lcrn = node->ini_node.lun_crn[idx];