1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2021 Broadcom. All Rights Reserved. The term
4 * “Broadcom” refers to Broadcom Inc. and/or its subsidiaries.
7 #include "efct_driver.h"
10 #define enable_tsend_auto_resp(efct) 1
11 #define enable_treceive_auto_resp(efct) 0
13 #define SCSI_IOFMT "[%04x][i:%04x t:%04x h:%04x]"
15 #define scsi_io_printf(io, fmt, ...) \
16 efc_log_debug(io->efct, "[%s]" SCSI_IOFMT fmt, \
17 io->node->display_name, io->instance_index,\
18 io->init_task_tag, io->tgt_task_tag, io->hw_tag, ##__VA_ARGS__)
20 #define EFCT_LOG_ENABLE_SCSI_TRACE(efct) \
21 (((efct) != NULL) ? (((efct)->logmask & (1U << 2)) != 0) : 0)
23 #define scsi_io_trace(io, fmt, ...) \
25 if (EFCT_LOG_ENABLE_SCSI_TRACE(io->efct)) \
26 scsi_io_printf(io, fmt, ##__VA_ARGS__); \
30 efct_scsi_io_alloc(struct efct_node *node)
33 struct efct_xport *xport;
35 unsigned long flags = 0;
41 spin_lock_irqsave(&node->active_ios_lock, flags);
43 io = efct_io_pool_io_alloc(efct->xport->io_pool);
45 efc_log_err(efct, "IO alloc Failed\n");
46 atomic_add_return(1, &xport->io_alloc_failed_count);
47 spin_unlock_irqrestore(&node->active_ios_lock, flags);
51 /* initialize refcount */
53 io->release = _efct_scsi_io_free;
55 /* set generic fields */
60 /* set type and name */
61 io->io_type = EFCT_IO_TYPE_IO;
62 io->display_name = "scsi_io";
67 /* Add to node's active_ios list */
68 INIT_LIST_HEAD(&io->list_entry);
69 list_add(&io->list_entry, &node->active_ios);
71 spin_unlock_irqrestore(&node->active_ios_lock, flags);
77 _efct_scsi_io_free(struct kref *arg)
79 struct efct_io *io = container_of(arg, struct efct_io, ref);
80 struct efct *efct = io->efct;
81 struct efct_node *node = io->node;
82 unsigned long flags = 0;
84 scsi_io_trace(io, "freeing io 0x%p %s\n", io, io->display_name);
87 efc_log_err(efct, "IO already freed.\n");
91 spin_lock_irqsave(&node->active_ios_lock, flags);
92 list_del_init(&io->list_entry);
93 spin_unlock_irqrestore(&node->active_ios_lock, flags);
95 kref_put(&node->ref, node->release);
97 efct_io_pool_io_free(efct->xport->io_pool, io);
101 efct_scsi_io_free(struct efct_io *io)
103 scsi_io_trace(io, "freeing io 0x%p %s\n", io, io->display_name);
104 WARN_ON(!refcount_read(&io->ref.refcount));
105 kref_put(&io->ref, io->release);
109 efct_target_io_cb(struct efct_hw_io *hio, u32 length, int status,
110 u32 ext_status, void *app)
113 struct efct_io *io = app;
115 enum efct_scsi_io_status scsi_stat = EFCT_SCSI_STATUS_GOOD;
116 efct_scsi_io_cb_t cb;
118 if (!io || !io->efct) {
119 pr_err("%s: IO can not be NULL\n", __func__);
123 scsi_io_trace(io, "status x%x ext_status x%x\n", status, ext_status);
127 io->transferred += length;
129 if (!io->scsi_tgt_cb) {
130 efct_scsi_check_pending(efct);
134 /* Call target server completion */
135 cb = io->scsi_tgt_cb;
137 /* Clear the callback before invoking the callback */
138 io->scsi_tgt_cb = NULL;
140 /* if status was good, and auto-good-response was set,
141 * then callback target-server with IO_CMPL_RSP_SENT,
142 * otherwise send IO_CMPL
144 if (status == 0 && io->auto_resp)
145 flags |= EFCT_SCSI_IO_CMPL_RSP_SENT;
147 flags |= EFCT_SCSI_IO_CMPL;
150 case SLI4_FC_WCQE_STATUS_SUCCESS:
151 scsi_stat = EFCT_SCSI_STATUS_GOOD;
153 case SLI4_FC_WCQE_STATUS_DI_ERROR:
154 if (ext_status & SLI4_FC_DI_ERROR_GE)
155 scsi_stat = EFCT_SCSI_STATUS_DIF_GUARD_ERR;
156 else if (ext_status & SLI4_FC_DI_ERROR_AE)
157 scsi_stat = EFCT_SCSI_STATUS_DIF_APP_TAG_ERROR;
158 else if (ext_status & SLI4_FC_DI_ERROR_RE)
159 scsi_stat = EFCT_SCSI_STATUS_DIF_REF_TAG_ERROR;
161 scsi_stat = EFCT_SCSI_STATUS_DIF_UNKNOWN_ERROR;
163 case SLI4_FC_WCQE_STATUS_LOCAL_REJECT:
164 switch (ext_status) {
165 case SLI4_FC_LOCAL_REJECT_INVALID_RELOFFSET:
166 case SLI4_FC_LOCAL_REJECT_ABORT_REQUESTED:
167 scsi_stat = EFCT_SCSI_STATUS_ABORTED;
169 case SLI4_FC_LOCAL_REJECT_INVALID_RPI:
170 scsi_stat = EFCT_SCSI_STATUS_NEXUS_LOST;
172 case SLI4_FC_LOCAL_REJECT_NO_XRI:
173 scsi_stat = EFCT_SCSI_STATUS_NO_IO;
176 /*we have seen 0x0d(TX_DMA_FAILED err)*/
177 scsi_stat = EFCT_SCSI_STATUS_ERROR;
182 case SLI4_FC_WCQE_STATUS_TARGET_WQE_TIMEOUT:
183 /* target IO timed out */
184 scsi_stat = EFCT_SCSI_STATUS_TIMEDOUT_AND_ABORTED;
187 case SLI4_FC_WCQE_STATUS_SHUTDOWN:
188 /* Target IO cancelled by HW */
189 scsi_stat = EFCT_SCSI_STATUS_SHUTDOWN;
193 scsi_stat = EFCT_SCSI_STATUS_ERROR;
197 cb(io, scsi_stat, flags, io->scsi_tgt_cb_arg);
199 efct_scsi_check_pending(efct);
203 efct_scsi_build_sgls(struct efct_hw *hw, struct efct_hw_io *hio,
204 struct efct_scsi_sgl *sgl, u32 sgl_count,
205 enum efct_hw_io_type type)
209 struct efct *efct = hw->os;
211 /* Initialize HW SGL */
212 rc = efct_hw_io_init_sges(hw, hio, type);
214 efc_log_err(efct, "efct_hw_io_init_sges failed: %d\n", rc);
218 for (i = 0; i < sgl_count; i++) {
220 rc = efct_hw_io_add_sge(hw, hio, sgl[i].addr, sgl[i].len);
222 efc_log_err(efct, "add sge failed cnt=%d rc=%d\n",
231 static void efc_log_sgl(struct efct_io *io)
233 struct efct_hw_io *hio = io->hio;
234 struct sli4_sge *data = NULL;
239 scsi_io_trace(io, "def_sgl at 0x%x 0x%08x\n",
240 upper_32_bits(hio->def_sgl.phys),
241 lower_32_bits(hio->def_sgl.phys));
242 n_sge = (hio->sgl == &hio->def_sgl) ? hio->n_sge : hio->def_sgl_count;
243 for (i = 0, data = hio->def_sgl.virt; i < n_sge; i++, data++) {
246 scsi_io_trace(io, "SGL %2d 0x%08x 0x%08x 0x%08x 0x%08x\n",
247 i, dword[0], dword[1], dword[2], dword[3]);
249 if (dword[2] & (1U << 31))
255 efct_scsi_check_pending_async_cb(struct efct_hw *hw, int status,
258 struct efct_io *io = arg;
261 efct_hw_done_t cb = io->hw_cb;
267 (cb)(io->hio, 0, SLI4_FC_WCQE_STATUS_DISPATCH_ERROR, 0, io);
272 efct_scsi_io_dispatch_hw_io(struct efct_io *io, struct efct_hw_io *hio)
275 struct efct *efct = io->efct;
278 * update ini/tgt_task_tag with HW IO info and dispatch
282 io->tgt_task_tag = hio->indicator;
283 else if (io->cmd_ini)
284 io->init_task_tag = hio->indicator;
285 io->hw_tag = hio->reqtag;
287 hio->eq = io->hw_priv;
289 /* Copy WQ steering */
290 switch (io->wq_steering) {
291 case EFCT_SCSI_WQ_STEERING_CLASS >> EFCT_SCSI_WQ_STEERING_SHIFT:
292 hio->wq_steering = EFCT_HW_WQ_STEERING_CLASS;
294 case EFCT_SCSI_WQ_STEERING_REQUEST >> EFCT_SCSI_WQ_STEERING_SHIFT:
295 hio->wq_steering = EFCT_HW_WQ_STEERING_REQUEST;
297 case EFCT_SCSI_WQ_STEERING_CPU >> EFCT_SCSI_WQ_STEERING_SHIFT:
298 hio->wq_steering = EFCT_HW_WQ_STEERING_CPU;
302 switch (io->io_type) {
303 case EFCT_IO_TYPE_IO:
304 rc = efct_scsi_build_sgls(&efct->hw, io->hio,
305 io->sgl, io->sgl_count, io->hio_type);
309 if (EFCT_LOG_ENABLE_SCSI_TRACE(efct))
313 io->iparam.fcp_tgt.app_id = io->app_id;
315 io->iparam.fcp_tgt.vpi = io->node->vpi;
316 io->iparam.fcp_tgt.rpi = io->node->rpi;
317 io->iparam.fcp_tgt.s_id = io->node->port_fc_id;
318 io->iparam.fcp_tgt.d_id = io->node->node_fc_id;
319 io->iparam.fcp_tgt.xmit_len = io->wire_len;
321 rc = efct_hw_io_send(&io->efct->hw, io->hio_type, io->hio,
322 &io->iparam, io->hw_cb, io);
325 scsi_io_printf(io, "Unknown IO type=%d\n", io->io_type);
333 efct_scsi_io_dispatch_no_hw_io(struct efct_io *io)
337 switch (io->io_type) {
338 case EFCT_IO_TYPE_ABORT: {
339 struct efct_hw_io *hio_to_abort = NULL;
341 hio_to_abort = io->io_to_abort->hio;
345 * If "IO to abort" does not have an
346 * associated HW IO, immediately make callback with
347 * success. The command must have been sent to
348 * the backend, but the data phase has not yet
349 * started, so we don't have a HW IO.
351 * Note: since the backend shims should be
352 * taking a reference on io_to_abort, it should not
353 * be possible to have been completed and freed by
354 * the backend before the abort got here.
356 scsi_io_printf(io, "IO: not active\n");
357 ((efct_hw_done_t)io->hw_cb)(io->hio, 0,
358 SLI4_FC_WCQE_STATUS_SUCCESS, 0, io);
363 /* HW IO is valid, abort it */
364 scsi_io_printf(io, "aborting\n");
365 rc = efct_hw_io_abort(&io->efct->hw, hio_to_abort,
366 io->send_abts, io->hw_cb, io);
368 int status = SLI4_FC_WCQE_STATUS_SUCCESS;
369 efct_hw_done_t cb = io->hw_cb;
371 if (rc != -ENOENT && rc != -EINPROGRESS) {
373 scsi_io_printf(io, "Failed to abort IO rc=%d\n",
376 cb(io->hio, 0, status, 0, io);
383 scsi_io_printf(io, "Unknown IO type=%d\n", io->io_type);
390 static struct efct_io *
391 efct_scsi_dispatch_pending(struct efct *efct)
393 struct efct_xport *xport = efct->xport;
394 struct efct_io *io = NULL;
395 struct efct_hw_io *hio;
396 unsigned long flags = 0;
399 spin_lock_irqsave(&xport->io_pending_lock, flags);
401 if (!list_empty(&xport->io_pending_list)) {
402 io = list_first_entry(&xport->io_pending_list, struct efct_io,
404 list_del_init(&io->io_pending_link);
408 spin_unlock_irqrestore(&xport->io_pending_lock, flags);
412 if (io->io_type == EFCT_IO_TYPE_ABORT) {
415 hio = efct_hw_io_alloc(&efct->hw);
418 * No HW IO available.Put IO back on
419 * the front of pending list
421 list_add(&xport->io_pending_list, &io->io_pending_link);
424 hio->eq = io->hw_priv;
428 /* Must drop the lock before dispatching the IO */
429 spin_unlock_irqrestore(&xport->io_pending_lock, flags);
435 * We pulled an IO off the pending list,
436 * and either got an HW IO or don't need one
438 atomic_sub_return(1, &xport->io_pending_count);
440 status = efct_scsi_io_dispatch_no_hw_io(io);
442 status = efct_scsi_io_dispatch_hw_io(io, hio);
445 * Invoke the HW callback, but do so in the
446 * separate execution context,provided by the
447 * NOP mailbox completion processing context
448 * by using efct_hw_async_call()
450 if (efct_hw_async_call(&efct->hw,
451 efct_scsi_check_pending_async_cb, io)) {
452 efc_log_debug(efct, "call hw async failed\n");
460 efct_scsi_check_pending(struct efct *efct)
462 struct efct_xport *xport = efct->xport;
463 struct efct_io *io = NULL;
465 unsigned long flags = 0;
468 /* Guard against recursion */
469 if (atomic_add_return(1, &xport->io_pending_recursing)) {
470 /* This function is already running. Decrement and return. */
471 atomic_sub_return(1, &xport->io_pending_recursing);
475 while (efct_scsi_dispatch_pending(efct))
479 atomic_sub_return(1, &xport->io_pending_recursing);
484 * If nothing was removed from the list,
485 * we might be in a case where we need to abort an
486 * active IO and the abort is on the pending list.
487 * Look for an abort we can dispatch.
490 spin_lock_irqsave(&xport->io_pending_lock, flags);
492 list_for_each_entry(io, &xport->io_pending_list, io_pending_link) {
493 if (io->io_type == EFCT_IO_TYPE_ABORT && io->io_to_abort->hio) {
494 /* This IO has a HW IO, so it is
495 * active. Dispatch the abort.
498 list_del_init(&io->io_pending_link);
499 atomic_sub_return(1, &xport->io_pending_count);
504 spin_unlock_irqrestore(&xport->io_pending_lock, flags);
507 if (efct_scsi_io_dispatch_no_hw_io(io)) {
508 if (efct_hw_async_call(&efct->hw,
509 efct_scsi_check_pending_async_cb, io)) {
510 efc_log_debug(efct, "hw async failed\n");
515 atomic_sub_return(1, &xport->io_pending_recursing);
519 efct_scsi_io_dispatch(struct efct_io *io, void *cb)
521 struct efct_hw_io *hio;
522 struct efct *efct = io->efct;
523 struct efct_xport *xport = efct->xport;
524 unsigned long flags = 0;
529 * if this IO already has a HW IO, then this is either
530 * not the first phase of the IO. Send it to the HW.
533 return efct_scsi_io_dispatch_hw_io(io, io->hio);
536 * We don't already have a HW IO associated with the IO. First check
537 * the pending list. If not empty, add IO to the tail and process the
540 spin_lock_irqsave(&xport->io_pending_lock, flags);
541 if (!list_empty(&xport->io_pending_list)) {
543 * If this is a low latency request,
544 * the put at the front of the IO pending
545 * queue, otherwise put it at the end of the queue.
547 if (io->low_latency) {
548 INIT_LIST_HEAD(&io->io_pending_link);
549 list_add(&xport->io_pending_list, &io->io_pending_link);
551 INIT_LIST_HEAD(&io->io_pending_link);
552 list_add_tail(&io->io_pending_link,
553 &xport->io_pending_list);
555 spin_unlock_irqrestore(&xport->io_pending_lock, flags);
556 atomic_add_return(1, &xport->io_pending_count);
557 atomic_add_return(1, &xport->io_total_pending);
559 /* process pending list */
560 efct_scsi_check_pending(efct);
563 spin_unlock_irqrestore(&xport->io_pending_lock, flags);
566 * We don't have a HW IO associated with the IO and there's nothing
567 * on the pending list. Attempt to allocate a HW IO and dispatch it.
569 hio = efct_hw_io_alloc(&io->efct->hw);
571 /* Couldn't get a HW IO. Save this IO on the pending list */
572 spin_lock_irqsave(&xport->io_pending_lock, flags);
573 INIT_LIST_HEAD(&io->io_pending_link);
574 list_add_tail(&io->io_pending_link, &xport->io_pending_list);
575 spin_unlock_irqrestore(&xport->io_pending_lock, flags);
577 atomic_add_return(1, &xport->io_total_pending);
578 atomic_add_return(1, &xport->io_pending_count);
582 /* We successfully allocated a HW IO; dispatch to HW */
583 return efct_scsi_io_dispatch_hw_io(io, hio);
587 efct_scsi_io_dispatch_abort(struct efct_io *io, void *cb)
589 struct efct *efct = io->efct;
590 struct efct_xport *xport = efct->xport;
591 unsigned long flags = 0;
596 * For aborts, we don't need a HW IO, but we still want
597 * to pass through the pending list to preserve ordering.
598 * Thus, if the pending list is not empty, add this abort
599 * to the pending list and process the pending list.
601 spin_lock_irqsave(&xport->io_pending_lock, flags);
602 if (!list_empty(&xport->io_pending_list)) {
603 INIT_LIST_HEAD(&io->io_pending_link);
604 list_add_tail(&io->io_pending_link, &xport->io_pending_list);
605 spin_unlock_irqrestore(&xport->io_pending_lock, flags);
606 atomic_add_return(1, &xport->io_pending_count);
607 atomic_add_return(1, &xport->io_total_pending);
609 /* process pending list */
610 efct_scsi_check_pending(efct);
613 spin_unlock_irqrestore(&xport->io_pending_lock, flags);
615 /* nothing on pending list, dispatch abort */
616 return efct_scsi_io_dispatch_no_hw_io(io);
620 efct_scsi_xfer_data(struct efct_io *io, u32 flags,
621 struct efct_scsi_sgl *sgl, u32 sgl_count, u64 xwire_len,
622 enum efct_hw_io_type type, int enable_ar,
623 efct_scsi_io_cb_t cb, void *arg)
628 io->sgl_count = sgl_count;
632 scsi_io_trace(io, "%s wire_len %llu\n",
633 (type == EFCT_HW_IO_TARGET_READ) ? "send" : "recv",
638 io->scsi_tgt_cb = cb;
639 io->scsi_tgt_cb_arg = arg;
641 residual = io->exp_xfer_len - io->transferred;
642 io->wire_len = (xwire_len < residual) ? xwire_len : residual;
643 residual = (xwire_len - io->wire_len);
645 memset(&io->iparam, 0, sizeof(io->iparam));
646 io->iparam.fcp_tgt.ox_id = io->init_task_tag;
647 io->iparam.fcp_tgt.offset = io->transferred;
648 io->iparam.fcp_tgt.cs_ctl = io->cs_ctl;
649 io->iparam.fcp_tgt.timeout = io->timeout;
651 /* if this is the last data phase and there is no residual, enable
654 if (enable_ar && (flags & EFCT_SCSI_LAST_DATAPHASE) && residual == 0 &&
655 ((io->transferred + io->wire_len) == io->exp_xfer_len) &&
656 (!(flags & EFCT_SCSI_NO_AUTO_RESPONSE))) {
657 io->iparam.fcp_tgt.flags |= SLI4_IO_AUTO_GOOD_RESPONSE;
658 io->auto_resp = true;
660 io->auto_resp = false;
663 /* save this transfer length */
664 io->xfer_req = io->wire_len;
666 /* Adjust the transferred count to account for overrun
667 * when the residual is calculated in efct_scsi_send_resp
669 io->transferred += residual;
671 /* Adjust the SGL size if there is overrun */
674 struct efct_scsi_sgl *sgl_ptr = &io->sgl[sgl_count - 1];
677 size_t len = sgl_ptr->len;
679 if (len > residual) {
680 sgl_ptr->len = len - residual;
691 /* Set latency and WQ steering */
692 io->low_latency = (flags & EFCT_SCSI_LOW_LATENCY) != 0;
693 io->wq_steering = (flags & EFCT_SCSI_WQ_STEERING_MASK) >>
694 EFCT_SCSI_WQ_STEERING_SHIFT;
695 io->wq_class = (flags & EFCT_SCSI_WQ_CLASS_MASK) >>
696 EFCT_SCSI_WQ_CLASS_SHIFT;
699 struct efct_xport *xport = efct->xport;
701 if (type == EFCT_HW_IO_TARGET_READ) {
702 xport->fcp_stats.input_requests++;
703 xport->fcp_stats.input_bytes += xwire_len;
704 } else if (type == EFCT_HW_IO_TARGET_WRITE) {
705 xport->fcp_stats.output_requests++;
706 xport->fcp_stats.output_bytes += xwire_len;
709 return efct_scsi_io_dispatch(io, efct_target_io_cb);
713 efct_scsi_send_rd_data(struct efct_io *io, u32 flags,
714 struct efct_scsi_sgl *sgl, u32 sgl_count, u64 len,
715 efct_scsi_io_cb_t cb, void *arg)
717 return efct_scsi_xfer_data(io, flags, sgl, sgl_count,
718 len, EFCT_HW_IO_TARGET_READ,
719 enable_tsend_auto_resp(io->efct), cb, arg);
723 efct_scsi_recv_wr_data(struct efct_io *io, u32 flags,
724 struct efct_scsi_sgl *sgl, u32 sgl_count, u64 len,
725 efct_scsi_io_cb_t cb, void *arg)
727 return efct_scsi_xfer_data(io, flags, sgl, sgl_count, len,
728 EFCT_HW_IO_TARGET_WRITE,
729 enable_treceive_auto_resp(io->efct), cb, arg);
733 efct_scsi_send_resp(struct efct_io *io, u32 flags,
734 struct efct_scsi_cmd_resp *rsp,
735 efct_scsi_io_cb_t cb, void *arg)
739 /* Always try auto resp */
740 bool auto_resp = true;
742 u16 scsi_status_qualifier = 0;
743 u8 *sense_data = NULL;
744 u32 sense_data_length = 0;
749 scsi_status = rsp->scsi_status;
750 scsi_status_qualifier = rsp->scsi_status_qualifier;
751 sense_data = rsp->sense_data;
752 sense_data_length = rsp->sense_data_length;
753 residual = rsp->residual;
755 residual = io->exp_xfer_len - io->transferred;
759 io->hio_type = EFCT_HW_IO_TARGET_RSP;
761 io->scsi_tgt_cb = cb;
762 io->scsi_tgt_cb_arg = arg;
764 memset(&io->iparam, 0, sizeof(io->iparam));
765 io->iparam.fcp_tgt.ox_id = io->init_task_tag;
766 io->iparam.fcp_tgt.offset = 0;
767 io->iparam.fcp_tgt.cs_ctl = io->cs_ctl;
768 io->iparam.fcp_tgt.timeout = io->timeout;
770 /* Set low latency queueing request */
771 io->low_latency = (flags & EFCT_SCSI_LOW_LATENCY) != 0;
772 io->wq_steering = (flags & EFCT_SCSI_WQ_STEERING_MASK) >>
773 EFCT_SCSI_WQ_STEERING_SHIFT;
774 io->wq_class = (flags & EFCT_SCSI_WQ_CLASS_MASK) >>
775 EFCT_SCSI_WQ_CLASS_SHIFT;
777 if (scsi_status != 0 || residual || sense_data_length) {
778 struct fcp_resp_with_ext *fcprsp = io->rspbuf.virt;
782 efc_log_err(efct, "NULL response buffer\n");
786 sns_data = (u8 *)io->rspbuf.virt + sizeof(*fcprsp);
790 memset(fcprsp, 0, sizeof(*fcprsp));
792 io->wire_len += sizeof(*fcprsp);
794 fcprsp->resp.fr_status = scsi_status;
795 fcprsp->resp.fr_retry_delay =
796 cpu_to_be16(scsi_status_qualifier);
798 /* set residual status if necessary */
800 /* FCP: if data transferred is less than the
801 * amount expected, then this is an underflow.
802 * If data transferred would have been greater
803 * than the amount expected this is an overflow
806 fcprsp->resp.fr_flags |= FCP_RESID_UNDER;
807 fcprsp->ext.fr_resid = cpu_to_be32(residual);
809 fcprsp->resp.fr_flags |= FCP_RESID_OVER;
810 fcprsp->ext.fr_resid = cpu_to_be32(-residual);
814 if (EFCT_SCSI_SNS_BUF_VALID(sense_data) && sense_data_length) {
815 if (sense_data_length > SCSI_SENSE_BUFFERSIZE) {
816 efc_log_err(efct, "Sense exceeds max size.\n");
820 fcprsp->resp.fr_flags |= FCP_SNS_LEN_VAL;
821 memcpy(sns_data, sense_data, sense_data_length);
822 fcprsp->ext.fr_sns_len = cpu_to_be32(sense_data_length);
823 io->wire_len += sense_data_length;
826 io->sgl[0].addr = io->rspbuf.phys;
827 io->sgl[0].dif_addr = 0;
828 io->sgl[0].len = io->wire_len;
833 io->iparam.fcp_tgt.flags |= SLI4_IO_AUTO_GOOD_RESPONSE;
835 return efct_scsi_io_dispatch(io, efct_target_io_cb);
839 efct_target_bls_resp_cb(struct efct_hw_io *hio, u32 length, int status,
840 u32 ext_status, void *app)
842 struct efct_io *io = app;
844 enum efct_scsi_io_status bls_status;
848 /* BLS isn't really a "SCSI" concept, but use SCSI status */
850 io_error_log(io, "s=%#x x=%#x\n", status, ext_status);
851 bls_status = EFCT_SCSI_STATUS_ERROR;
853 bls_status = EFCT_SCSI_STATUS_GOOD;
857 efct_scsi_io_cb_t bls_cb = io->bls_cb;
858 void *bls_cb_arg = io->bls_cb_arg;
861 io->bls_cb_arg = NULL;
863 /* invoke callback */
864 bls_cb(io, bls_status, 0, bls_cb_arg);
867 efct_scsi_check_pending(efct);
872 efct_target_send_bls_resp(struct efct_io *io,
873 efct_scsi_io_cb_t cb, void *arg)
875 struct efct_node *node = io->node;
876 struct sli_bls_params *bls = &io->iparam.bls;
877 struct efct *efct = node->efct;
878 struct fc_ba_acc *acc;
881 /* fill out IO structure with everything needed to send BA_ACC */
882 memset(&io->iparam, 0, sizeof(io->iparam));
883 bls->ox_id = io->init_task_tag;
884 bls->rx_id = io->abort_rx_id;
885 bls->vpi = io->node->vpi;
886 bls->rpi = io->node->rpi;
888 bls->d_id = io->node->node_fc_id;
889 bls->rpi_registered = true;
891 acc = (void *)bls->payload;
892 acc->ba_ox_id = cpu_to_be16(bls->ox_id);
893 acc->ba_rx_id = cpu_to_be16(bls->rx_id);
894 acc->ba_high_seq_cnt = cpu_to_be16(U16_MAX);
896 /* generic io fields have already been populated */
898 /* set type and BLS-specific fields */
899 io->io_type = EFCT_IO_TYPE_BLS_RESP;
900 io->display_name = "bls_rsp";
901 io->hio_type = EFCT_HW_BLS_ACC;
903 io->bls_cb_arg = arg;
906 rc = efct_hw_bls_send(efct, FC_RCTL_BA_ACC, bls,
907 efct_target_bls_resp_cb, io);
911 static int efct_bls_send_rjt_cb(struct efct_hw_io *hio, u32 length, int status,
912 u32 ext_status, void *app)
914 struct efct_io *io = app;
916 efct_scsi_io_free(io);
921 efct_bls_send_rjt(struct efct_io *io, struct fc_frame_header *hdr)
923 struct efct_node *node = io->node;
924 struct sli_bls_params *bls = &io->iparam.bls;
925 struct efct *efct = node->efct;
926 struct fc_ba_rjt *acc;
929 /* fill out BLS Response-specific fields */
930 io->io_type = EFCT_IO_TYPE_BLS_RESP;
931 io->display_name = "ba_rjt";
932 io->hio_type = EFCT_HW_BLS_RJT;
933 io->init_task_tag = be16_to_cpu(hdr->fh_ox_id);
935 /* fill out iparam fields */
936 memset(&io->iparam, 0, sizeof(io->iparam));
937 bls->ox_id = be16_to_cpu(hdr->fh_ox_id);
938 bls->rx_id = be16_to_cpu(hdr->fh_rx_id);
939 bls->vpi = io->node->vpi;
940 bls->rpi = io->node->rpi;
942 bls->d_id = io->node->node_fc_id;
943 bls->rpi_registered = true;
945 acc = (void *)bls->payload;
946 acc->br_reason = ELS_RJT_UNAB;
947 acc->br_explan = ELS_EXPL_NONE;
949 rc = efct_hw_bls_send(efct, FC_RCTL_BA_RJT, bls, efct_bls_send_rjt_cb,
952 efc_log_err(efct, "efct_scsi_io_dispatch() failed: %d\n", rc);
953 efct_scsi_io_free(io);
960 efct_scsi_send_tmf_resp(struct efct_io *io,
961 enum efct_scsi_tmf_resp rspcode,
963 efct_scsi_io_cb_t cb, void *arg)
967 struct fcp_resp_with_ext rsp_ext;
968 struct fcp_resp_rsp_info info;
975 case EFCT_SCSI_TMF_FUNCTION_COMPLETE:
976 fcp_rspcode = FCP_TMF_CMPL;
978 case EFCT_SCSI_TMF_FUNCTION_SUCCEEDED:
979 case EFCT_SCSI_TMF_FUNCTION_IO_NOT_FOUND:
980 fcp_rspcode = FCP_TMF_CMPL;
982 case EFCT_SCSI_TMF_FUNCTION_REJECTED:
983 fcp_rspcode = FCP_TMF_REJECTED;
985 case EFCT_SCSI_TMF_INCORRECT_LOGICAL_UNIT_NUMBER:
986 fcp_rspcode = FCP_TMF_INVALID_LUN;
988 case EFCT_SCSI_TMF_SERVICE_DELIVERY:
989 fcp_rspcode = FCP_TMF_FAILED;
992 fcp_rspcode = FCP_TMF_REJECTED;
996 io->hio_type = EFCT_HW_IO_TARGET_RSP;
998 io->scsi_tgt_cb = cb;
999 io->scsi_tgt_cb_arg = arg;
1001 if (io->tmf_cmd == EFCT_SCSI_TMF_ABORT_TASK) {
1002 rc = efct_target_send_bls_resp(io, cb, arg);
1006 /* populate the FCP TMF response */
1007 fcprsp = io->rspbuf.virt;
1008 memset(fcprsp, 0, sizeof(*fcprsp));
1010 fcprsp->rsp_ext.resp.fr_flags |= FCP_SNS_LEN_VAL;
1012 if (addl_rsp_info) {
1013 memcpy(fcprsp->info._fr_resvd, addl_rsp_info,
1014 sizeof(fcprsp->info._fr_resvd));
1016 fcprsp->info.rsp_code = fcp_rspcode;
1018 io->wire_len = sizeof(*fcprsp);
1020 fcprsp->rsp_ext.ext.fr_rsp_len =
1021 cpu_to_be32(sizeof(struct fcp_resp_rsp_info));
1023 io->sgl[0].addr = io->rspbuf.phys;
1024 io->sgl[0].dif_addr = 0;
1025 io->sgl[0].len = io->wire_len;
1028 memset(&io->iparam, 0, sizeof(io->iparam));
1029 io->iparam.fcp_tgt.ox_id = io->init_task_tag;
1030 io->iparam.fcp_tgt.offset = 0;
1031 io->iparam.fcp_tgt.cs_ctl = io->cs_ctl;
1032 io->iparam.fcp_tgt.timeout = io->timeout;
1034 rc = efct_scsi_io_dispatch(io, efct_target_io_cb);
1040 efct_target_abort_cb(struct efct_hw_io *hio, u32 length, int status,
1041 u32 ext_status, void *app)
1043 struct efct_io *io = app;
1045 enum efct_scsi_io_status scsi_status;
1046 efct_scsi_io_cb_t abort_cb;
1054 abort_cb = io->abort_cb;
1055 abort_cb_arg = io->abort_cb_arg;
1057 io->abort_cb = NULL;
1058 io->abort_cb_arg = NULL;
1061 case SLI4_FC_WCQE_STATUS_SUCCESS:
1062 scsi_status = EFCT_SCSI_STATUS_GOOD;
1064 case SLI4_FC_WCQE_STATUS_LOCAL_REJECT:
1065 switch (ext_status) {
1066 case SLI4_FC_LOCAL_REJECT_NO_XRI:
1067 scsi_status = EFCT_SCSI_STATUS_NO_IO;
1069 case SLI4_FC_LOCAL_REJECT_ABORT_IN_PROGRESS:
1070 scsi_status = EFCT_SCSI_STATUS_ABORT_IN_PROGRESS;
1073 /*we have seen 0x15 (abort in progress)*/
1074 scsi_status = EFCT_SCSI_STATUS_ERROR;
1078 case SLI4_FC_WCQE_STATUS_FCP_RSP_FAILURE:
1079 scsi_status = EFCT_SCSI_STATUS_CHECK_RESPONSE;
1082 scsi_status = EFCT_SCSI_STATUS_ERROR;
1085 /* invoke callback */
1086 abort_cb(io->io_to_abort, scsi_status, 0, abort_cb_arg);
1089 /* done with IO to abort,efct_ref_get(): efct_scsi_tgt_abort_io() */
1090 kref_put(&io->io_to_abort->ref, io->io_to_abort->release);
1092 efct_io_pool_io_free(efct->xport->io_pool, io);
1094 efct_scsi_check_pending(efct);
1099 efct_scsi_tgt_abort_io(struct efct_io *io, efct_scsi_io_cb_t cb, void *arg)
1102 struct efct_xport *xport;
1104 struct efct_io *abort_io = NULL;
1107 xport = efct->xport;
1109 /* take a reference on IO being aborted */
1110 if (kref_get_unless_zero(&io->ref) == 0) {
1111 /* command no longer active */
1112 scsi_io_printf(io, "command no longer active\n");
1117 * allocate a new IO to send the abort request. Use efct_io_alloc()
1118 * directly, as we need an IO object that will not fail allocation
1119 * due to allocations being disabled (in efct_scsi_io_alloc())
1121 abort_io = efct_io_pool_io_alloc(efct->xport->io_pool);
1123 atomic_add_return(1, &xport->io_alloc_failed_count);
1124 kref_put(&io->ref, io->release);
1128 /* Save the target server callback and argument */
1129 /* set generic fields */
1130 abort_io->cmd_tgt = true;
1131 abort_io->node = io->node;
1133 /* set type and abort-specific fields */
1134 abort_io->io_type = EFCT_IO_TYPE_ABORT;
1135 abort_io->display_name = "tgt_abort";
1136 abort_io->io_to_abort = io;
1137 abort_io->send_abts = false;
1138 abort_io->abort_cb = cb;
1139 abort_io->abort_cb_arg = arg;
1141 /* now dispatch IO */
1142 rc = efct_scsi_io_dispatch_abort(abort_io, efct_target_abort_cb);
1144 kref_put(&io->ref, io->release);
1149 efct_scsi_io_complete(struct efct_io *io)
1152 efc_log_debug(io->efct, "completion for non-busy io tag 0x%x\n",
1157 scsi_io_trace(io, "freeing io 0x%p %s\n", io, io->display_name);
1158 kref_put(&io->ref, io->release);