1 // SPDX-License-Identifier: GPL-2.0-only
3 * qla_target.c SCSI LLD infrastructure for QLogic 22xx/23xx/24xx/25xx
5 * based on qla2x00t.c code:
7 * Copyright (C) 2004 - 2010 Vladislav Bolkhovitin <vst@vlnb.net>
8 * Copyright (C) 2004 - 2005 Leonid Stoljar
9 * Copyright (C) 2006 Nathaniel Clark <nate@misrule.us>
10 * Copyright (C) 2006 - 2010 ID7 Ltd.
12 * Forward port and refactoring to modern qla2xxx and target/configfs
14 * Copyright (C) 2010-2013 Nicholas A. Bellinger <nab@kernel.org>
17 #include <linux/module.h>
18 #include <linux/init.h>
19 #include <linux/types.h>
20 #include <linux/blkdev.h>
21 #include <linux/interrupt.h>
22 #include <linux/pci.h>
23 #include <linux/delay.h>
24 #include <linux/list.h>
25 #include <linux/workqueue.h>
26 #include <asm/unaligned.h>
27 #include <scsi/scsi.h>
28 #include <scsi/scsi_host.h>
29 #include <scsi/scsi_tcq.h>
32 #include "qla_target.h"
34 static int ql2xtgt_tape_enable;
35 module_param(ql2xtgt_tape_enable, int, S_IRUGO|S_IWUSR);
36 MODULE_PARM_DESC(ql2xtgt_tape_enable,
37 "Enables Sequence level error recovery (aka FC Tape). Default is 0 - no SLER. 1 - Enable SLER.");
39 static char *qlini_mode = QLA2XXX_INI_MODE_STR_ENABLED;
40 module_param(qlini_mode, charp, S_IRUGO);
41 MODULE_PARM_DESC(qlini_mode,
42 "Determines when initiator mode will be enabled. Possible values: "
43 "\"exclusive\" - initiator mode will be enabled on load, "
44 "disabled on enabling target mode and then on disabling target mode "
46 "\"disabled\" - initiator mode will never be enabled; "
47 "\"dual\" - Initiator Modes will be enabled. Target Mode can be activated "
49 "\"enabled\" (default) - initiator mode will always stay enabled.");
51 static int ql_dm_tgt_ex_pct = 0;
52 module_param(ql_dm_tgt_ex_pct, int, S_IRUGO|S_IWUSR);
53 MODULE_PARM_DESC(ql_dm_tgt_ex_pct,
54 "For Dual Mode (qlini_mode=dual), this parameter determines "
55 "the percentage of exchanges/cmds FW will allocate resources "
59 module_param(ql2xuctrlirq, int, 0644);
60 MODULE_PARM_DESC(ql2xuctrlirq,
61 "User to control IRQ placement via smp_affinity."
62 "Valid with qlini_mode=disabled."
63 "1(default): enable");
65 int ql2x_ini_mode = QLA2XXX_INI_MODE_EXCLUSIVE;
67 static int qla_sam_status = SAM_STAT_BUSY;
68 static int tc_sam_status = SAM_STAT_TASK_SET_FULL; /* target core */
71 * From scsi/fc/fc_fcp.h
73 enum fcp_resp_rsp_codes {
75 FCP_DATA_LEN_INVALID = 1,
76 FCP_CMND_FIELDS_INVALID = 2,
77 FCP_DATA_PARAM_MISMATCH = 3,
80 FCP_TMF_INVALID_LUN = 9,
84 * fc_pri_ta from scsi/fc/fc_fcp.h
86 #define FCP_PTA_SIMPLE 0 /* simple task attribute */
87 #define FCP_PTA_HEADQ 1 /* head of queue task attribute */
88 #define FCP_PTA_ORDERED 2 /* ordered task attribute */
89 #define FCP_PTA_ACA 4 /* auto. contingent allegiance */
90 #define FCP_PTA_MASK 7 /* mask for task attribute field */
91 #define FCP_PRI_SHIFT 3 /* priority field starts in bit 3 */
92 #define FCP_PRI_RESVD_MASK 0x80 /* reserved bits in priority field */
95 * This driver calls qla2x00_alloc_iocbs() and qla2x00_issue_marker(), which
96 * must be called under HW lock and could unlock/lock it inside.
97 * It isn't an issue, since in the current implementation on the time when
98 * those functions are called:
100 * - Either context is IRQ and only IRQ handler can modify HW data,
101 * including rings related fields,
103 * - Or access to target mode variables from struct qla_tgt doesn't
104 * cross those functions boundaries, except tgt_stop, which
105 * additionally protected by irq_cmd_count.
107 /* Predefs for callbacks handed to qla2xxx LLD */
108 static void qlt_24xx_atio_pkt(struct scsi_qla_host *ha,
109 struct atio_from_isp *pkt, uint8_t);
110 static void qlt_response_pkt(struct scsi_qla_host *ha, struct rsp_que *rsp,
112 static int qlt_issue_task_mgmt(struct fc_port *sess, u64 lun,
113 int fn, void *iocb, int flags);
114 static void qlt_send_term_exchange(struct qla_qpair *, struct qla_tgt_cmd
115 *cmd, struct atio_from_isp *atio, int ha_locked, int ul_abort);
116 static void qlt_alloc_qfull_cmd(struct scsi_qla_host *vha,
117 struct atio_from_isp *atio, uint16_t status, int qfull);
118 static void qlt_disable_vha(struct scsi_qla_host *vha);
119 static void qlt_clear_tgt_db(struct qla_tgt *tgt);
120 static void qlt_send_notify_ack(struct qla_qpair *qpair,
121 struct imm_ntfy_from_isp *ntfy,
122 uint32_t add_flags, uint16_t resp_code, int resp_code_valid,
123 uint16_t srr_flags, uint16_t srr_reject_code, uint8_t srr_explan);
124 static void qlt_send_term_imm_notif(struct scsi_qla_host *vha,
125 struct imm_ntfy_from_isp *imm, int ha_locked);
126 static struct fc_port *qlt_create_sess(struct scsi_qla_host *vha,
127 fc_port_t *fcport, bool local);
128 void qlt_unreg_sess(struct fc_port *sess);
129 static void qlt_24xx_handle_abts(struct scsi_qla_host *,
130 struct abts_recv_from_24xx *);
131 static void qlt_send_busy(struct qla_qpair *, struct atio_from_isp *,
133 static int qlt_check_reserve_free_req(struct qla_qpair *qpair, uint32_t);
134 static inline uint32_t qlt_make_handle(struct qla_qpair *);
139 static struct kmem_cache *qla_tgt_mgmt_cmd_cachep;
140 struct kmem_cache *qla_tgt_plogi_cachep;
141 static mempool_t *qla_tgt_mgmt_cmd_mempool;
142 static struct workqueue_struct *qla_tgt_wq;
143 static DEFINE_MUTEX(qla_tgt_mutex);
144 static LIST_HEAD(qla_tgt_glist);
146 static const char *prot_op_str(u32 prot_op)
149 case TARGET_PROT_NORMAL: return "NORMAL";
150 case TARGET_PROT_DIN_INSERT: return "DIN_INSERT";
151 case TARGET_PROT_DOUT_INSERT: return "DOUT_INSERT";
152 case TARGET_PROT_DIN_STRIP: return "DIN_STRIP";
153 case TARGET_PROT_DOUT_STRIP: return "DOUT_STRIP";
154 case TARGET_PROT_DIN_PASS: return "DIN_PASS";
155 case TARGET_PROT_DOUT_PASS: return "DOUT_PASS";
156 default: return "UNKNOWN";
160 /* This API intentionally takes dest as a parameter, rather than returning
161 * int value to avoid caller forgetting to issue wmb() after the store */
162 void qlt_do_generation_tick(struct scsi_qla_host *vha, int *dest)
164 scsi_qla_host_t *base_vha = pci_get_drvdata(vha->hw->pdev);
165 *dest = atomic_inc_return(&base_vha->generation_tick);
170 /* Might release hw lock, then reaquire!! */
171 static inline int qlt_issue_marker(struct scsi_qla_host *vha, int vha_locked)
173 /* Send marker if required */
174 if (unlikely(vha->marker_needed != 0)) {
175 int rc = qla2x00_issue_marker(vha, vha_locked);
177 if (rc != QLA_SUCCESS) {
178 ql_dbg(ql_dbg_tgt, vha, 0xe03d,
179 "qla_target(%d): issue_marker() failed\n",
188 struct scsi_qla_host *qlt_find_host_by_d_id(struct scsi_qla_host *vha,
191 struct scsi_qla_host *host;
194 if (vha->d_id.b.area == d_id.area &&
195 vha->d_id.b.domain == d_id.domain &&
196 vha->d_id.b.al_pa == d_id.al_pa)
199 key = be_to_port_id(d_id).b24;
201 host = btree_lookup32(&vha->hw->tgt.host_map, key);
203 ql_dbg(ql_dbg_tgt_mgt + ql_dbg_verbose, vha, 0xf005,
204 "Unable to find host %06x\n", key);
210 struct scsi_qla_host *qlt_find_host_by_vp_idx(struct scsi_qla_host *vha,
213 struct qla_hw_data *ha = vha->hw;
215 if (vha->vp_idx == vp_idx)
218 BUG_ON(ha->tgt.tgt_vp_map == NULL);
219 if (likely(test_bit(vp_idx, ha->vp_idx_map)))
220 return ha->tgt.tgt_vp_map[vp_idx].vha;
225 static inline void qlt_incr_num_pend_cmds(struct scsi_qla_host *vha)
229 spin_lock_irqsave(&vha->hw->tgt.q_full_lock, flags);
231 vha->hw->tgt.num_pend_cmds++;
232 if (vha->hw->tgt.num_pend_cmds > vha->qla_stats.stat_max_pend_cmds)
233 vha->qla_stats.stat_max_pend_cmds =
234 vha->hw->tgt.num_pend_cmds;
235 spin_unlock_irqrestore(&vha->hw->tgt.q_full_lock, flags);
237 static inline void qlt_decr_num_pend_cmds(struct scsi_qla_host *vha)
241 spin_lock_irqsave(&vha->hw->tgt.q_full_lock, flags);
242 vha->hw->tgt.num_pend_cmds--;
243 spin_unlock_irqrestore(&vha->hw->tgt.q_full_lock, flags);
247 static void qlt_queue_unknown_atio(scsi_qla_host_t *vha,
248 struct atio_from_isp *atio, uint8_t ha_locked)
250 struct qla_tgt_sess_op *u;
251 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
255 ql_dbg(ql_dbg_async, vha, 0x502c,
256 "qla_target(%d): dropping unknown ATIO_TYPE7, because tgt is being stopped",
261 u = kzalloc(sizeof(*u), GFP_ATOMIC);
266 memcpy(&u->atio, atio, sizeof(*atio));
267 INIT_LIST_HEAD(&u->cmd_list);
269 spin_lock_irqsave(&vha->cmd_list_lock, flags);
270 list_add_tail(&u->cmd_list, &vha->unknown_atio_list);
271 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
273 schedule_delayed_work(&vha->unknown_atio_work, 1);
279 qlt_send_term_exchange(vha->hw->base_qpair, NULL, atio, ha_locked, 0);
283 static void qlt_try_to_dequeue_unknown_atios(struct scsi_qla_host *vha,
286 struct qla_tgt_sess_op *u, *t;
287 scsi_qla_host_t *host;
288 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
292 list_for_each_entry_safe(u, t, &vha->unknown_atio_list, cmd_list) {
294 ql_dbg(ql_dbg_async, vha, 0x502e,
295 "Freeing unknown %s %p, because of Abort\n",
297 qlt_send_term_exchange(vha->hw->base_qpair, NULL,
298 &u->atio, ha_locked, 0);
302 host = qlt_find_host_by_d_id(vha, u->atio.u.isp24.fcp_hdr.d_id);
304 ql_dbg(ql_dbg_async + ql_dbg_verbose, vha, 0x502f,
305 "Requeuing unknown ATIO_TYPE7 %p\n", u);
306 qlt_24xx_atio_pkt(host, &u->atio, ha_locked);
307 } else if (tgt->tgt_stop) {
308 ql_dbg(ql_dbg_async + ql_dbg_verbose, vha, 0x503a,
309 "Freeing unknown %s %p, because tgt is being stopped\n",
311 qlt_send_term_exchange(vha->hw->base_qpair, NULL,
312 &u->atio, ha_locked, 0);
314 ql_dbg(ql_dbg_async + ql_dbg_verbose, vha, 0x503d,
315 "Reschedule u %p, vha %p, host %p\n", u, vha, host);
318 schedule_delayed_work(&vha->unknown_atio_work,
325 spin_lock_irqsave(&vha->cmd_list_lock, flags);
326 list_del(&u->cmd_list);
327 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
332 void qlt_unknown_atio_work_fn(struct work_struct *work)
334 struct scsi_qla_host *vha = container_of(to_delayed_work(work),
335 struct scsi_qla_host, unknown_atio_work);
337 qlt_try_to_dequeue_unknown_atios(vha, 0);
340 static bool qlt_24xx_atio_pkt_all_vps(struct scsi_qla_host *vha,
341 struct atio_from_isp *atio, uint8_t ha_locked)
343 ql_dbg(ql_dbg_tgt, vha, 0xe072,
344 "%s: qla_target(%d): type %x ox_id %04x\n",
345 __func__, vha->vp_idx, atio->u.raw.entry_type,
346 be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id));
348 switch (atio->u.raw.entry_type) {
351 struct scsi_qla_host *host = qlt_find_host_by_d_id(vha,
352 atio->u.isp24.fcp_hdr.d_id);
353 if (unlikely(NULL == host)) {
354 ql_dbg(ql_dbg_tgt, vha, 0xe03e,
355 "qla_target(%d): Received ATIO_TYPE7 "
356 "with unknown d_id %x:%x:%x\n", vha->vp_idx,
357 atio->u.isp24.fcp_hdr.d_id.domain,
358 atio->u.isp24.fcp_hdr.d_id.area,
359 atio->u.isp24.fcp_hdr.d_id.al_pa);
362 qlt_queue_unknown_atio(vha, atio, ha_locked);
365 if (unlikely(!list_empty(&vha->unknown_atio_list)))
366 qlt_try_to_dequeue_unknown_atios(vha, ha_locked);
368 qlt_24xx_atio_pkt(host, atio, ha_locked);
372 case IMMED_NOTIFY_TYPE:
374 struct scsi_qla_host *host = vha;
375 struct imm_ntfy_from_isp *entry =
376 (struct imm_ntfy_from_isp *)atio;
378 qlt_issue_marker(vha, ha_locked);
380 if ((entry->u.isp24.vp_index != 0xFF) &&
381 (entry->u.isp24.nport_handle != cpu_to_le16(0xFFFF))) {
382 host = qlt_find_host_by_vp_idx(vha,
383 entry->u.isp24.vp_index);
384 if (unlikely(!host)) {
385 ql_dbg(ql_dbg_tgt, vha, 0xe03f,
386 "qla_target(%d): Received "
387 "ATIO (IMMED_NOTIFY_TYPE) "
388 "with unknown vp_index %d\n",
389 vha->vp_idx, entry->u.isp24.vp_index);
393 qlt_24xx_atio_pkt(host, atio, ha_locked);
397 case VP_RPT_ID_IOCB_TYPE:
398 qla24xx_report_id_acquisition(vha,
399 (struct vp_rpt_id_entry_24xx *)atio);
404 struct abts_recv_from_24xx *entry =
405 (struct abts_recv_from_24xx *)atio;
406 struct scsi_qla_host *host = qlt_find_host_by_vp_idx(vha,
410 if (unlikely(!host)) {
411 ql_dbg(ql_dbg_tgt, vha, 0xe00a,
412 "qla_target(%d): Response pkt (ABTS_RECV_24XX) "
413 "received, with unknown vp_index %d\n",
414 vha->vp_idx, entry->vp_index);
418 spin_lock_irqsave(&host->hw->hardware_lock, flags);
419 qlt_24xx_handle_abts(host, (struct abts_recv_from_24xx *)atio);
421 spin_unlock_irqrestore(&host->hw->hardware_lock, flags);
425 /* case PUREX_IOCB_TYPE: ql2xmvasynctoatio */
428 ql_dbg(ql_dbg_tgt, vha, 0xe040,
429 "qla_target(%d): Received unknown ATIO atio "
430 "type %x\n", vha->vp_idx, atio->u.raw.entry_type);
437 void qlt_response_pkt_all_vps(struct scsi_qla_host *vha,
438 struct rsp_que *rsp, response_t *pkt)
440 switch (pkt->entry_type) {
442 ql_dbg(ql_dbg_tgt, vha, 0xe073,
443 "qla_target(%d):%s: CRC2 Response pkt\n",
444 vha->vp_idx, __func__);
448 struct ctio7_from_24xx *entry = (struct ctio7_from_24xx *)pkt;
449 struct scsi_qla_host *host = qlt_find_host_by_vp_idx(vha,
451 if (unlikely(!host)) {
452 ql_dbg(ql_dbg_tgt, vha, 0xe041,
453 "qla_target(%d): Response pkt (CTIO_TYPE7) "
454 "received, with unknown vp_index %d\n",
455 vha->vp_idx, entry->vp_index);
458 qlt_response_pkt(host, rsp, pkt);
462 case IMMED_NOTIFY_TYPE:
464 struct scsi_qla_host *host;
465 struct imm_ntfy_from_isp *entry =
466 (struct imm_ntfy_from_isp *)pkt;
468 host = qlt_find_host_by_vp_idx(vha, entry->u.isp24.vp_index);
469 if (unlikely(!host)) {
470 ql_dbg(ql_dbg_tgt, vha, 0xe042,
471 "qla_target(%d): Response pkt (IMMED_NOTIFY_TYPE) "
472 "received, with unknown vp_index %d\n",
473 vha->vp_idx, entry->u.isp24.vp_index);
476 qlt_response_pkt(host, rsp, pkt);
480 case NOTIFY_ACK_TYPE:
482 struct scsi_qla_host *host = vha;
483 struct nack_to_isp *entry = (struct nack_to_isp *)pkt;
485 if (0xFF != entry->u.isp24.vp_index) {
486 host = qlt_find_host_by_vp_idx(vha,
487 entry->u.isp24.vp_index);
488 if (unlikely(!host)) {
489 ql_dbg(ql_dbg_tgt, vha, 0xe043,
490 "qla_target(%d): Response "
491 "pkt (NOTIFY_ACK_TYPE) "
492 "received, with unknown "
493 "vp_index %d\n", vha->vp_idx,
494 entry->u.isp24.vp_index);
498 qlt_response_pkt(host, rsp, pkt);
504 struct abts_recv_from_24xx *entry =
505 (struct abts_recv_from_24xx *)pkt;
506 struct scsi_qla_host *host = qlt_find_host_by_vp_idx(vha,
508 if (unlikely(!host)) {
509 ql_dbg(ql_dbg_tgt, vha, 0xe044,
510 "qla_target(%d): Response pkt "
511 "(ABTS_RECV_24XX) received, with unknown "
512 "vp_index %d\n", vha->vp_idx, entry->vp_index);
515 qlt_response_pkt(host, rsp, pkt);
521 struct abts_resp_to_24xx *entry =
522 (struct abts_resp_to_24xx *)pkt;
523 struct scsi_qla_host *host = qlt_find_host_by_vp_idx(vha,
525 if (unlikely(!host)) {
526 ql_dbg(ql_dbg_tgt, vha, 0xe045,
527 "qla_target(%d): Response pkt "
528 "(ABTS_RECV_24XX) received, with unknown "
529 "vp_index %d\n", vha->vp_idx, entry->vp_index);
532 qlt_response_pkt(host, rsp, pkt);
536 qlt_response_pkt(vha, rsp, pkt);
543 * All qlt_plogi_ack_t operations are protected by hardware_lock
545 static int qla24xx_post_nack_work(struct scsi_qla_host *vha, fc_port_t *fcport,
546 struct imm_ntfy_from_isp *ntfy, int type)
548 struct qla_work_evt *e;
550 e = qla2x00_alloc_work(vha, QLA_EVT_NACK);
552 return QLA_FUNCTION_FAILED;
554 e->u.nack.fcport = fcport;
555 e->u.nack.type = type;
556 memcpy(e->u.nack.iocb, ntfy, sizeof(struct imm_ntfy_from_isp));
557 return qla2x00_post_work(vha, e);
560 static void qla2x00_async_nack_sp_done(srb_t *sp, int res)
562 struct scsi_qla_host *vha = sp->vha;
565 ql_dbg(ql_dbg_disc, vha, 0x20f2,
566 "Async done-%s res %x %8phC type %d\n",
567 sp->name, res, sp->fcport->port_name, sp->type);
569 spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
570 sp->fcport->flags &= ~FCF_ASYNC_SENT;
571 sp->fcport->chip_reset = vha->hw->base_qpair->chip_reset;
575 sp->fcport->login_gen++;
576 sp->fcport->fw_login_state = DSC_LS_PLOGI_COMP;
577 sp->fcport->logout_on_delete = 1;
578 sp->fcport->plogi_nack_done_deadline = jiffies + HZ;
579 sp->fcport->send_els_logo = 0;
583 sp->fcport->fw_login_state = DSC_LS_PRLI_COMP;
584 sp->fcport->deleted = 0;
585 sp->fcport->send_els_logo = 0;
587 if (!sp->fcport->login_succ &&
588 !IS_SW_RESV_ADDR(sp->fcport->d_id)) {
589 sp->fcport->login_succ = 1;
592 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
593 qla24xx_sched_upd_fcport(sp->fcport);
594 spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
596 sp->fcport->login_retry = 0;
597 qla2x00_set_fcport_disc_state(sp->fcport,
599 sp->fcport->deleted = 0;
600 sp->fcport->logout_on_delete = 1;
605 sp->fcport->login_gen++;
606 sp->fcport->fw_login_state = DSC_LS_PORT_UNAVAIL;
607 qlt_logo_completion_handler(sp->fcport, MBS_COMMAND_COMPLETE);
610 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
615 int qla24xx_async_notify_ack(scsi_qla_host_t *vha, fc_port_t *fcport,
616 struct imm_ntfy_from_isp *ntfy, int type)
618 int rval = QLA_FUNCTION_FAILED;
622 fcport->flags |= FCF_ASYNC_SENT;
625 fcport->fw_login_state = DSC_LS_PLOGI_PEND;
629 fcport->fw_login_state = DSC_LS_PRLI_PEND;
634 fcport->fw_login_state = DSC_LS_LOGO_PEND;
639 sp = qla2x00_get_sp(vha, fcport, GFP_ATOMIC);
646 sp->u.iocb_cmd.timeout = qla2x00_async_iocb_timeout;
647 qla2x00_init_timer(sp, qla2x00_get_async_timeout(vha)+2);
649 sp->u.iocb_cmd.u.nack.ntfy = ntfy;
650 sp->done = qla2x00_async_nack_sp_done;
652 ql_dbg(ql_dbg_disc, vha, 0x20f4,
653 "Async-%s %8phC hndl %x %s\n",
654 sp->name, fcport->port_name, sp->handle, c);
656 rval = qla2x00_start_sp(sp);
657 if (rval != QLA_SUCCESS)
665 fcport->flags &= ~FCF_ASYNC_SENT;
669 void qla24xx_do_nack_work(struct scsi_qla_host *vha, struct qla_work_evt *e)
673 switch (e->u.nack.type) {
675 t = e->u.nack.fcport;
676 flush_work(&t->del_work);
677 flush_work(&t->free_work);
678 mutex_lock(&vha->vha_tgt.tgt_mutex);
679 t = qlt_create_sess(vha, e->u.nack.fcport, 0);
680 mutex_unlock(&vha->vha_tgt.tgt_mutex);
682 ql_log(ql_log_info, vha, 0xd034,
683 "%s create sess success %p", __func__, t);
684 /* create sess has an extra kref */
685 vha->hw->tgt.tgt_ops->put_sess(e->u.nack.fcport);
689 qla24xx_async_notify_ack(vha, e->u.nack.fcport,
690 (struct imm_ntfy_from_isp *)e->u.nack.iocb, e->u.nack.type);
693 void qla24xx_delete_sess_fn(struct work_struct *work)
695 fc_port_t *fcport = container_of(work, struct fc_port, del_work);
696 struct qla_hw_data *ha = fcport->vha->hw;
698 if (fcport->se_sess) {
699 ha->tgt.tgt_ops->shutdown_sess(fcport);
700 ha->tgt.tgt_ops->put_sess(fcport);
702 qlt_unreg_sess(fcport);
707 * Called from qla2x00_reg_remote_port()
709 void qlt_fc_port_added(struct scsi_qla_host *vha, fc_port_t *fcport)
711 struct qla_hw_data *ha = vha->hw;
712 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
713 struct fc_port *sess = fcport;
716 if (!vha->hw->tgt.tgt_ops)
719 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
721 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
725 if (fcport->disc_state == DSC_DELETE_PEND) {
726 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
730 if (!sess->se_sess) {
731 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
733 mutex_lock(&vha->vha_tgt.tgt_mutex);
734 sess = qlt_create_sess(vha, fcport, false);
735 mutex_unlock(&vha->vha_tgt.tgt_mutex);
737 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
739 if (fcport->fw_login_state == DSC_LS_PRLI_COMP) {
740 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
744 if (!kref_get_unless_zero(&sess->sess_kref)) {
745 ql_dbg(ql_dbg_disc, vha, 0x2107,
746 "%s: kref_get fail sess %8phC \n",
747 __func__, sess->port_name);
748 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
752 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04c,
753 "qla_target(%u): %ssession for port %8phC "
754 "(loop ID %d) reappeared\n", vha->vp_idx,
755 sess->local ? "local " : "", sess->port_name, sess->loop_id);
757 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf007,
758 "Reappeared sess %p\n", sess);
760 ha->tgt.tgt_ops->update_sess(sess, fcport->d_id,
762 (fcport->flags & FCF_CONF_COMP_SUPPORTED));
765 if (sess && sess->local) {
766 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04d,
767 "qla_target(%u): local session for "
768 "port %8phC (loop ID %d) became global\n", vha->vp_idx,
769 fcport->port_name, sess->loop_id);
772 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
774 ha->tgt.tgt_ops->put_sess(sess);
778 * This is a zero-base ref-counting solution, since hardware_lock
779 * guarantees that ref_count is not modified concurrently.
780 * Upon successful return content of iocb is undefined
782 static struct qlt_plogi_ack_t *
783 qlt_plogi_ack_find_add(struct scsi_qla_host *vha, port_id_t *id,
784 struct imm_ntfy_from_isp *iocb)
786 struct qlt_plogi_ack_t *pla;
788 lockdep_assert_held(&vha->hw->hardware_lock);
790 list_for_each_entry(pla, &vha->plogi_ack_list, list) {
791 if (pla->id.b24 == id->b24) {
792 ql_dbg(ql_dbg_disc + ql_dbg_verbose, vha, 0x210d,
793 "%s %d %8phC Term INOT due to new INOT",
795 pla->iocb.u.isp24.port_name);
796 qlt_send_term_imm_notif(vha, &pla->iocb, 1);
797 memcpy(&pla->iocb, iocb, sizeof(pla->iocb));
802 pla = kmem_cache_zalloc(qla_tgt_plogi_cachep, GFP_ATOMIC);
804 ql_dbg(ql_dbg_async, vha, 0x5088,
805 "qla_target(%d): Allocation of plogi_ack failed\n",
810 memcpy(&pla->iocb, iocb, sizeof(pla->iocb));
812 list_add_tail(&pla->list, &vha->plogi_ack_list);
817 void qlt_plogi_ack_unref(struct scsi_qla_host *vha,
818 struct qlt_plogi_ack_t *pla)
820 struct imm_ntfy_from_isp *iocb = &pla->iocb;
823 fc_port_t *fcport = pla->fcport;
825 BUG_ON(!pla->ref_count);
831 ql_dbg(ql_dbg_disc, vha, 0x5089,
832 "Sending PLOGI ACK to wwn %8phC s_id %02x:%02x:%02x loop_id %#04x"
833 " exch %#x ox_id %#x\n", iocb->u.isp24.port_name,
834 iocb->u.isp24.port_id[2], iocb->u.isp24.port_id[1],
835 iocb->u.isp24.port_id[0],
836 le16_to_cpu(iocb->u.isp24.nport_handle),
837 iocb->u.isp24.exchange_address, iocb->ox_id);
839 port_id.b.domain = iocb->u.isp24.port_id[2];
840 port_id.b.area = iocb->u.isp24.port_id[1];
841 port_id.b.al_pa = iocb->u.isp24.port_id[0];
842 port_id.b.rsvd_1 = 0;
844 loop_id = le16_to_cpu(iocb->u.isp24.nport_handle);
846 fcport->loop_id = loop_id;
847 fcport->d_id = port_id;
848 if (iocb->u.isp24.status_subcode == ELS_PLOGI)
849 qla24xx_post_nack_work(vha, fcport, iocb, SRB_NACK_PLOGI);
851 qla24xx_post_nack_work(vha, fcport, iocb, SRB_NACK_PRLI);
853 list_for_each_entry(fcport, &vha->vp_fcports, list) {
854 if (fcport->plogi_link[QLT_PLOGI_LINK_SAME_WWN] == pla)
855 fcport->plogi_link[QLT_PLOGI_LINK_SAME_WWN] = NULL;
856 if (fcport->plogi_link[QLT_PLOGI_LINK_CONFLICT] == pla)
857 fcport->plogi_link[QLT_PLOGI_LINK_CONFLICT] = NULL;
860 list_del(&pla->list);
861 kmem_cache_free(qla_tgt_plogi_cachep, pla);
865 qlt_plogi_ack_link(struct scsi_qla_host *vha, struct qlt_plogi_ack_t *pla,
866 struct fc_port *sess, enum qlt_plogi_link_t link)
868 struct imm_ntfy_from_isp *iocb = &pla->iocb;
869 /* Inc ref_count first because link might already be pointing at pla */
872 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf097,
873 "Linking sess %p [%d] wwn %8phC with PLOGI ACK to wwn %8phC"
874 " s_id %02x:%02x:%02x, ref=%d pla %p link %d\n",
875 sess, link, sess->port_name,
876 iocb->u.isp24.port_name, iocb->u.isp24.port_id[2],
877 iocb->u.isp24.port_id[1], iocb->u.isp24.port_id[0],
878 pla->ref_count, pla, link);
880 if (link == QLT_PLOGI_LINK_CONFLICT) {
881 switch (sess->disc_state) {
883 case DSC_DELETE_PEND:
891 if (sess->plogi_link[link])
892 qlt_plogi_ack_unref(vha, sess->plogi_link[link]);
894 if (link == QLT_PLOGI_LINK_SAME_WWN)
897 sess->plogi_link[link] = pla;
901 /* These fields must be initialized by the caller */
904 * number of cmds dropped while we were waiting for
905 * initiator to ack LOGO initialize to 1 if LOGO is
906 * triggered by a command, otherwise, to 0
910 /* These fields are used by callee */
911 struct list_head list;
915 qlt_send_first_logo(struct scsi_qla_host *vha, qlt_port_logo_t *logo)
917 qlt_port_logo_t *tmp;
920 mutex_lock(&vha->vha_tgt.tgt_mutex);
922 list_for_each_entry(tmp, &vha->logo_list, list) {
923 if (tmp->id.b24 == logo->id.b24) {
924 tmp->cmd_count += logo->cmd_count;
925 mutex_unlock(&vha->vha_tgt.tgt_mutex);
930 list_add_tail(&logo->list, &vha->logo_list);
932 mutex_unlock(&vha->vha_tgt.tgt_mutex);
934 res = qla24xx_els_dcmd_iocb(vha, ELS_DCMD_LOGO, logo->id);
936 mutex_lock(&vha->vha_tgt.tgt_mutex);
937 list_del(&logo->list);
938 mutex_unlock(&vha->vha_tgt.tgt_mutex);
940 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf098,
941 "Finished LOGO to %02x:%02x:%02x, dropped %d cmds, res = %#x\n",
942 logo->id.b.domain, logo->id.b.area, logo->id.b.al_pa,
943 logo->cmd_count, res);
946 void qlt_free_session_done(struct work_struct *work)
948 struct fc_port *sess = container_of(work, struct fc_port,
950 struct qla_tgt *tgt = sess->tgt;
951 struct scsi_qla_host *vha = sess->vha;
952 struct qla_hw_data *ha = vha->hw;
954 bool logout_started = false;
955 scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
956 struct qlt_plogi_ack_t *own =
957 sess->plogi_link[QLT_PLOGI_LINK_SAME_WWN];
959 ql_dbg(ql_dbg_disc, vha, 0xf084,
960 "%s: se_sess %p / sess %p from port %8phC loop_id %#04x"
961 " s_id %02x:%02x:%02x logout %d keep %d els_logo %d\n",
962 __func__, sess->se_sess, sess, sess->port_name, sess->loop_id,
963 sess->d_id.b.domain, sess->d_id.b.area, sess->d_id.b.al_pa,
964 sess->logout_on_delete, sess->keep_nport_handle,
965 sess->send_els_logo);
967 if (!IS_SW_RESV_ADDR(sess->d_id)) {
968 qla2x00_mark_device_lost(vha, sess, 0);
970 if (sess->send_els_logo) {
971 qlt_port_logo_t logo;
973 logo.id = sess->d_id;
976 qlt_send_first_logo(vha, &logo);
977 sess->send_els_logo = 0;
980 if (sess->logout_on_delete && sess->loop_id != FC_NO_LOOP_ID) {
984 (own->iocb.u.isp24.status_subcode == ELS_PLOGI)) {
985 rc = qla2x00_post_async_logout_work(vha, sess,
987 if (rc != QLA_SUCCESS)
988 ql_log(ql_log_warn, vha, 0xf085,
989 "Schedule logo failed sess %p rc %d\n",
992 logout_started = true;
993 } else if (own && (own->iocb.u.isp24.status_subcode ==
994 ELS_PRLI) && ha->flags.rida_fmt2) {
995 rc = qla2x00_post_async_prlo_work(vha, sess,
997 if (rc != QLA_SUCCESS)
998 ql_log(ql_log_warn, vha, 0xf085,
999 "Schedule PRLO failed sess %p rc %d\n",
1002 logout_started = true;
1004 } /* if sess->logout_on_delete */
1006 if (sess->nvme_flag & NVME_FLAG_REGISTERED &&
1007 !(sess->nvme_flag & NVME_FLAG_DELETING)) {
1008 sess->nvme_flag |= NVME_FLAG_DELETING;
1009 qla_nvme_unregister_remote_port(sess);
1014 * Release the target session for FC Nexus from fabric module code.
1016 if (sess->se_sess != NULL)
1017 ha->tgt.tgt_ops->free_session(sess);
1019 if (logout_started) {
1020 bool traced = false;
1023 while (!READ_ONCE(sess->logout_completed)) {
1025 ql_dbg(ql_dbg_disc, vha, 0xf086,
1026 "%s: waiting for sess %p logout\n",
1036 ql_dbg(ql_dbg_disc, vha, 0xf087,
1037 "%s: sess %p logout completed\n", __func__, sess);
1040 if (sess->logo_ack_needed) {
1041 sess->logo_ack_needed = 0;
1042 qla24xx_async_notify_ack(vha, sess,
1043 (struct imm_ntfy_from_isp *)sess->iocb, SRB_NACK_LOGO);
1046 spin_lock_irqsave(&vha->work_lock, flags);
1047 sess->flags &= ~FCF_ASYNC_SENT;
1048 spin_unlock_irqrestore(&vha->work_lock, flags);
1050 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
1051 if (sess->se_sess) {
1052 sess->se_sess = NULL;
1053 if (tgt && !IS_SW_RESV_ADDR(sess->d_id))
1057 qla2x00_set_fcport_disc_state(sess, DSC_DELETED);
1058 sess->fw_login_state = DSC_LS_PORT_UNAVAIL;
1059 sess->deleted = QLA_SESS_DELETED;
1061 if (sess->login_succ && !IS_SW_RESV_ADDR(sess->d_id)) {
1062 vha->fcport_count--;
1063 sess->login_succ = 0;
1066 qla2x00_clear_loop_id(sess);
1068 if (sess->conflict) {
1069 sess->conflict->login_pause = 0;
1070 sess->conflict = NULL;
1071 if (!test_bit(UNLOADING, &vha->dpc_flags))
1072 set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
1076 struct qlt_plogi_ack_t *con =
1077 sess->plogi_link[QLT_PLOGI_LINK_CONFLICT];
1078 struct imm_ntfy_from_isp *iocb;
1080 own = sess->plogi_link[QLT_PLOGI_LINK_SAME_WWN];
1084 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf099,
1085 "se_sess %p / sess %p port %8phC is gone,"
1086 " %s (ref=%d), releasing PLOGI for %8phC (ref=%d)\n",
1087 sess->se_sess, sess, sess->port_name,
1088 own ? "releasing own PLOGI" : "no own PLOGI pending",
1089 own ? own->ref_count : -1,
1090 iocb->u.isp24.port_name, con->ref_count);
1091 qlt_plogi_ack_unref(vha, con);
1092 sess->plogi_link[QLT_PLOGI_LINK_CONFLICT] = NULL;
1094 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf09a,
1095 "se_sess %p / sess %p port %8phC is gone, %s (ref=%d)\n",
1096 sess->se_sess, sess, sess->port_name,
1097 own ? "releasing own PLOGI" :
1098 "no own PLOGI pending",
1099 own ? own->ref_count : -1);
1103 sess->fw_login_state = DSC_LS_PLOGI_PEND;
1104 qlt_plogi_ack_unref(vha, own);
1105 sess->plogi_link[QLT_PLOGI_LINK_SAME_WWN] = NULL;
1109 sess->explicit_logout = 0;
1110 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
1111 sess->free_pending = 0;
1113 qla2x00_dfs_remove_rport(vha, sess);
1115 ql_dbg(ql_dbg_disc, vha, 0xf001,
1116 "Unregistration of sess %p %8phC finished fcp_cnt %d\n",
1117 sess, sess->port_name, vha->fcport_count);
1119 if (tgt && (tgt->sess_count == 0))
1120 wake_up_all(&tgt->waitQ);
1122 if (!test_bit(PFLG_DRIVER_REMOVING, &base_vha->pci_flags) &&
1123 !(vha->vp_idx && test_bit(VPORT_DELETE, &vha->dpc_flags)) &&
1124 (!tgt || !tgt->tgt_stop) && !LOOP_TRANSITION(vha)) {
1125 switch (vha->host->active_mode) {
1126 case MODE_INITIATOR:
1128 set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
1129 qla2xxx_wake_dpc(vha);
1138 if (vha->fcport_count == 0)
1139 wake_up_all(&vha->fcport_waitQ);
1142 /* ha->tgt.sess_lock supposed to be held on entry */
1143 void qlt_unreg_sess(struct fc_port *sess)
1145 struct scsi_qla_host *vha = sess->vha;
1146 unsigned long flags;
1148 ql_dbg(ql_dbg_disc, sess->vha, 0x210a,
1149 "%s sess %p for deletion %8phC\n",
1150 __func__, sess, sess->port_name);
1152 spin_lock_irqsave(&sess->vha->work_lock, flags);
1153 if (sess->free_pending) {
1154 spin_unlock_irqrestore(&sess->vha->work_lock, flags);
1157 sess->free_pending = 1;
1159 * Use FCF_ASYNC_SENT flag to block other cmds used in sess
1160 * management from being sent.
1162 sess->flags |= FCF_ASYNC_SENT;
1163 spin_unlock_irqrestore(&sess->vha->work_lock, flags);
1166 vha->hw->tgt.tgt_ops->clear_nacl_from_fcport_map(sess);
1168 sess->deleted = QLA_SESS_DELETION_IN_PROGRESS;
1169 qla2x00_set_fcport_disc_state(sess, DSC_DELETE_PEND);
1170 sess->last_rscn_gen = sess->rscn_gen;
1171 sess->last_login_gen = sess->login_gen;
1173 queue_work(sess->vha->hw->wq, &sess->free_work);
1175 EXPORT_SYMBOL(qlt_unreg_sess);
1177 static int qlt_reset(struct scsi_qla_host *vha, void *iocb, int mcmd)
1179 struct qla_hw_data *ha = vha->hw;
1180 struct fc_port *sess = NULL;
1183 struct imm_ntfy_from_isp *n = (struct imm_ntfy_from_isp *)iocb;
1184 unsigned long flags;
1186 loop_id = le16_to_cpu(n->u.isp24.nport_handle);
1187 if (loop_id == 0xFFFF) {
1189 atomic_inc(&vha->vha_tgt.qla_tgt->tgt_global_resets_count);
1190 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
1191 qlt_clear_tgt_db(vha->vha_tgt.qla_tgt);
1192 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
1194 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
1195 sess = ha->tgt.tgt_ops->find_sess_by_loop_id(vha, loop_id);
1196 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
1199 ql_dbg(ql_dbg_tgt, vha, 0xe000,
1200 "Using sess for qla_tgt_reset: %p\n", sess);
1206 ql_dbg(ql_dbg_tgt, vha, 0xe047,
1207 "scsi(%ld): resetting (session %p from port %8phC mcmd %x, "
1208 "loop_id %d)\n", vha->host_no, sess, sess->port_name,
1211 return qlt_issue_task_mgmt(sess, 0, mcmd, iocb, QLA24XX_MGMT_SEND_NACK);
1214 static void qla24xx_chk_fcp_state(struct fc_port *sess)
1216 if (sess->chip_reset != sess->vha->hw->base_qpair->chip_reset) {
1217 sess->logout_on_delete = 0;
1218 sess->logo_ack_needed = 0;
1219 sess->fw_login_state = DSC_LS_PORT_UNAVAIL;
1223 void qlt_schedule_sess_for_deletion(struct fc_port *sess)
1225 struct qla_tgt *tgt = sess->tgt;
1226 unsigned long flags;
1229 switch (sess->disc_state) {
1230 case DSC_DELETE_PEND:
1233 if (!sess->plogi_link[QLT_PLOGI_LINK_SAME_WWN] &&
1234 !sess->plogi_link[QLT_PLOGI_LINK_CONFLICT]) {
1235 if (tgt && tgt->tgt_stop && tgt->sess_count == 0)
1236 wake_up_all(&tgt->waitQ);
1238 if (sess->vha->fcport_count == 0)
1239 wake_up_all(&sess->vha->fcport_waitQ);
1243 case DSC_UPD_FCPORT:
1245 * This port is not done reporting to upper layer.
1248 sess->next_disc_state = DSC_DELETE_PEND;
1249 sec = jiffies_to_msecs(jiffies -
1250 sess->jiffies_at_registration)/1000;
1251 if (sess->sec_since_registration < sec && sec && !(sec % 5)) {
1252 sess->sec_since_registration = sec;
1253 ql_dbg(ql_dbg_disc, sess->vha, 0xffff,
1254 "%s %8phC : Slow Rport registration(%d Sec)\n",
1255 __func__, sess->port_name, sec);
1262 spin_lock_irqsave(&sess->vha->work_lock, flags);
1263 if (sess->deleted == QLA_SESS_DELETION_IN_PROGRESS) {
1264 spin_unlock_irqrestore(&sess->vha->work_lock, flags);
1267 sess->deleted = QLA_SESS_DELETION_IN_PROGRESS;
1268 spin_unlock_irqrestore(&sess->vha->work_lock, flags);
1270 sess->prli_pend_timer = 0;
1271 qla2x00_set_fcport_disc_state(sess, DSC_DELETE_PEND);
1273 qla24xx_chk_fcp_state(sess);
1275 ql_dbg(ql_log_warn, sess->vha, 0xe001,
1276 "Scheduling sess %p for deletion %8phC\n",
1277 sess, sess->port_name);
1279 WARN_ON(!queue_work(sess->vha->hw->wq, &sess->del_work));
1282 static void qlt_clear_tgt_db(struct qla_tgt *tgt)
1284 struct fc_port *sess;
1285 scsi_qla_host_t *vha = tgt->vha;
1287 list_for_each_entry(sess, &vha->vp_fcports, list) {
1289 qlt_schedule_sess_for_deletion(sess);
1292 /* At this point tgt could be already dead */
1295 static int qla24xx_get_loop_id(struct scsi_qla_host *vha, be_id_t s_id,
1298 struct qla_hw_data *ha = vha->hw;
1299 dma_addr_t gid_list_dma;
1300 struct gid_list_info *gid_list, *gid;
1304 gid_list = dma_alloc_coherent(&ha->pdev->dev, qla2x00_gid_list_size(ha),
1305 &gid_list_dma, GFP_KERNEL);
1307 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf044,
1308 "qla_target(%d): DMA Alloc failed of %u\n",
1309 vha->vp_idx, qla2x00_gid_list_size(ha));
1313 /* Get list of logged in devices */
1314 rc = qla24xx_gidlist_wait(vha, gid_list, gid_list_dma, &entries);
1315 if (rc != QLA_SUCCESS) {
1316 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf045,
1317 "qla_target(%d): get_id_list() failed: %x\n",
1320 goto out_free_id_list;
1325 for (i = 0; i < entries; i++) {
1326 if (gid->al_pa == s_id.al_pa &&
1327 gid->area == s_id.area &&
1328 gid->domain == s_id.domain) {
1329 *loop_id = le16_to_cpu(gid->loop_id);
1333 gid = (void *)gid + ha->gid_list_info_size;
1337 dma_free_coherent(&ha->pdev->dev, qla2x00_gid_list_size(ha),
1338 gid_list, gid_list_dma);
1343 * Adds an extra ref to allow to drop hw lock after adding sess to the list.
1344 * Caller must put it.
1346 static struct fc_port *qlt_create_sess(
1347 struct scsi_qla_host *vha,
1351 struct qla_hw_data *ha = vha->hw;
1352 struct fc_port *sess = fcport;
1353 unsigned long flags;
1355 if (vha->vha_tgt.qla_tgt->tgt_stop)
1358 if (fcport->se_sess) {
1359 if (!kref_get_unless_zero(&sess->sess_kref)) {
1360 ql_dbg(ql_dbg_disc, vha, 0x20f6,
1361 "%s: kref_get_unless_zero failed for %8phC\n",
1362 __func__, sess->port_name);
1367 sess->tgt = vha->vha_tgt.qla_tgt;
1368 sess->local = local;
1371 * Under normal circumstances we want to logout from firmware when
1372 * session eventually ends and release corresponding nport handle.
1373 * In the exception cases (e.g. when new PLOGI is waiting) corresponding
1374 * code will adjust these flags as necessary.
1376 sess->logout_on_delete = 1;
1377 sess->keep_nport_handle = 0;
1378 sess->logout_completed = 0;
1380 if (ha->tgt.tgt_ops->check_initiator_node_acl(vha,
1381 &fcport->port_name[0], sess) < 0) {
1382 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf015,
1383 "(%d) %8phC check_initiator_node_acl failed\n",
1384 vha->vp_idx, fcport->port_name);
1387 kref_init(&fcport->sess_kref);
1389 * Take an extra reference to ->sess_kref here to handle
1390 * fc_port access across ->tgt.sess_lock reaquire.
1392 if (!kref_get_unless_zero(&sess->sess_kref)) {
1393 ql_dbg(ql_dbg_disc, vha, 0x20f7,
1394 "%s: kref_get_unless_zero failed for %8phC\n",
1395 __func__, sess->port_name);
1399 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
1400 if (!IS_SW_RESV_ADDR(sess->d_id))
1401 vha->vha_tgt.qla_tgt->sess_count++;
1403 qlt_do_generation_tick(vha, &sess->generation);
1404 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
1407 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf006,
1408 "Adding sess %p se_sess %p to tgt %p sess_count %d\n",
1409 sess, sess->se_sess, vha->vha_tgt.qla_tgt,
1410 vha->vha_tgt.qla_tgt->sess_count);
1412 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04b,
1413 "qla_target(%d): %ssession for wwn %8phC (loop_id %d, "
1414 "s_id %x:%x:%x, confirmed completion %ssupported) added\n",
1415 vha->vp_idx, local ? "local " : "", fcport->port_name,
1416 fcport->loop_id, sess->d_id.b.domain, sess->d_id.b.area,
1417 sess->d_id.b.al_pa, sess->conf_compl_supported ? "" : "not ");
1423 * max_gen - specifies maximum session generation
1424 * at which this deletion requestion is still valid
1427 qlt_fc_port_deleted(struct scsi_qla_host *vha, fc_port_t *fcport, int max_gen)
1429 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
1430 struct fc_port *sess = fcport;
1431 unsigned long flags;
1433 if (!vha->hw->tgt.tgt_ops)
1439 spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
1440 if (tgt->tgt_stop) {
1441 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
1444 if (!sess->se_sess) {
1445 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
1449 if (max_gen - sess->generation < 0) {
1450 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
1451 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf092,
1452 "Ignoring stale deletion request for se_sess %p / sess %p"
1453 " for port %8phC, req_gen %d, sess_gen %d\n",
1454 sess->se_sess, sess, sess->port_name, max_gen,
1459 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf008, "qla_tgt_fc_port_deleted %p", sess);
1462 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
1463 qlt_schedule_sess_for_deletion(sess);
1466 static inline int test_tgt_sess_count(struct qla_tgt *tgt)
1468 struct qla_hw_data *ha = tgt->ha;
1469 unsigned long flags;
1472 * We need to protect against race, when tgt is freed before or
1475 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
1476 ql_dbg(ql_dbg_tgt, tgt->vha, 0xe002,
1477 "tgt %p, sess_count=%d\n",
1478 tgt, tgt->sess_count);
1479 res = (tgt->sess_count == 0);
1480 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
1485 /* Called by tcm_qla2xxx configfs code */
1486 int qlt_stop_phase1(struct qla_tgt *tgt)
1488 struct scsi_qla_host *vha = tgt->vha;
1489 struct qla_hw_data *ha = tgt->ha;
1490 unsigned long flags;
1492 mutex_lock(&ha->optrom_mutex);
1493 mutex_lock(&qla_tgt_mutex);
1495 if (tgt->tgt_stop || tgt->tgt_stopped) {
1496 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04e,
1497 "Already in tgt->tgt_stop or tgt_stopped state\n");
1498 mutex_unlock(&qla_tgt_mutex);
1499 mutex_unlock(&ha->optrom_mutex);
1503 ql_dbg(ql_dbg_tgt_mgt, vha, 0xe003, "Stopping target for host %ld(%p)\n",
1506 * Mutex needed to sync with qla_tgt_fc_port_[added,deleted].
1507 * Lock is needed, because we still can get an incoming packet.
1509 mutex_lock(&vha->vha_tgt.tgt_mutex);
1511 qlt_clear_tgt_db(tgt);
1512 mutex_unlock(&vha->vha_tgt.tgt_mutex);
1513 mutex_unlock(&qla_tgt_mutex);
1515 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf009,
1516 "Waiting for sess works (tgt %p)", tgt);
1517 spin_lock_irqsave(&tgt->sess_work_lock, flags);
1518 while (!list_empty(&tgt->sess_works_list)) {
1519 spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
1520 flush_scheduled_work();
1521 spin_lock_irqsave(&tgt->sess_work_lock, flags);
1523 spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
1525 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00a,
1526 "Waiting for tgt %p: sess_count=%d\n", tgt, tgt->sess_count);
1528 wait_event_timeout(tgt->waitQ, test_tgt_sess_count(tgt), 10*HZ);
1531 if (!ha->flags.host_shutting_down &&
1532 (qla_tgt_mode_enabled(vha) || qla_dual_mode_enabled(vha)))
1533 qlt_disable_vha(vha);
1535 /* Wait for sessions to clear out (just in case) */
1536 wait_event_timeout(tgt->waitQ, test_tgt_sess_count(tgt), 10*HZ);
1537 mutex_unlock(&ha->optrom_mutex);
1541 EXPORT_SYMBOL(qlt_stop_phase1);
1543 /* Called by tcm_qla2xxx configfs code */
1544 void qlt_stop_phase2(struct qla_tgt *tgt)
1546 scsi_qla_host_t *vha = tgt->vha;
1548 if (tgt->tgt_stopped) {
1549 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04f,
1550 "Already in tgt->tgt_stopped state\n");
1554 if (!tgt->tgt_stop) {
1555 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00b,
1556 "%s: phase1 stop is not completed\n", __func__);
1561 mutex_lock(&vha->vha_tgt.tgt_mutex);
1563 tgt->tgt_stopped = 1;
1564 mutex_unlock(&vha->vha_tgt.tgt_mutex);
1566 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00c, "Stop of tgt %p finished\n",
1569 switch (vha->qlini_mode) {
1570 case QLA2XXX_INI_MODE_EXCLUSIVE:
1571 vha->flags.online = 1;
1572 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
1578 EXPORT_SYMBOL(qlt_stop_phase2);
1580 /* Called from qlt_remove_target() -> qla2x00_remove_one() */
1581 static void qlt_release(struct qla_tgt *tgt)
1583 scsi_qla_host_t *vha = tgt->vha;
1587 struct qla_qpair_hint *h;
1588 struct qla_hw_data *ha = vha->hw;
1590 if (!tgt->tgt_stop && !tgt->tgt_stopped)
1591 qlt_stop_phase1(tgt);
1593 if (!tgt->tgt_stopped)
1594 qlt_stop_phase2(tgt);
1596 for (i = 0; i < vha->hw->max_qpairs + 1; i++) {
1597 unsigned long flags;
1599 h = &tgt->qphints[i];
1601 spin_lock_irqsave(h->qpair->qp_lock_ptr, flags);
1602 list_del(&h->hint_elem);
1603 spin_unlock_irqrestore(h->qpair->qp_lock_ptr, flags);
1607 kfree(tgt->qphints);
1608 mutex_lock(&qla_tgt_mutex);
1609 list_del(&vha->vha_tgt.qla_tgt->tgt_list_entry);
1610 mutex_unlock(&qla_tgt_mutex);
1612 btree_for_each_safe64(&tgt->lun_qpair_map, key, node)
1613 btree_remove64(&tgt->lun_qpair_map, key);
1615 btree_destroy64(&tgt->lun_qpair_map);
1618 if (ha->tgt.tgt_ops &&
1619 ha->tgt.tgt_ops->remove_target &&
1620 vha->vha_tgt.target_lport_ptr)
1621 ha->tgt.tgt_ops->remove_target(vha);
1623 vha->vha_tgt.qla_tgt = NULL;
1625 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00d,
1626 "Release of tgt %p finished\n", tgt);
1631 /* ha->hardware_lock supposed to be held on entry */
1632 static int qlt_sched_sess_work(struct qla_tgt *tgt, int type,
1633 const void *param, unsigned int param_size)
1635 struct qla_tgt_sess_work_param *prm;
1636 unsigned long flags;
1638 prm = kzalloc(sizeof(*prm), GFP_ATOMIC);
1640 ql_dbg(ql_dbg_tgt_mgt, tgt->vha, 0xf050,
1641 "qla_target(%d): Unable to create session "
1642 "work, command will be refused", 0);
1646 ql_dbg(ql_dbg_tgt_mgt, tgt->vha, 0xf00e,
1647 "Scheduling work (type %d, prm %p)"
1648 " to find session for param %p (size %d, tgt %p)\n",
1649 type, prm, param, param_size, tgt);
1652 memcpy(&prm->tm_iocb, param, param_size);
1654 spin_lock_irqsave(&tgt->sess_work_lock, flags);
1655 list_add_tail(&prm->sess_works_list_entry, &tgt->sess_works_list);
1656 spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
1658 schedule_work(&tgt->sess_work);
1664 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
1666 static void qlt_send_notify_ack(struct qla_qpair *qpair,
1667 struct imm_ntfy_from_isp *ntfy,
1668 uint32_t add_flags, uint16_t resp_code, int resp_code_valid,
1669 uint16_t srr_flags, uint16_t srr_reject_code, uint8_t srr_explan)
1671 struct scsi_qla_host *vha = qpair->vha;
1672 struct qla_hw_data *ha = vha->hw;
1674 struct nack_to_isp *nack;
1676 if (!ha->flags.fw_started)
1679 ql_dbg(ql_dbg_tgt, vha, 0xe004, "Sending NOTIFY_ACK (ha=%p)\n", ha);
1681 pkt = (request_t *)__qla2x00_alloc_iocbs(qpair, NULL);
1683 ql_dbg(ql_dbg_tgt, vha, 0xe049,
1684 "qla_target(%d): %s failed: unable to allocate "
1685 "request packet\n", vha->vp_idx, __func__);
1689 if (vha->vha_tgt.qla_tgt != NULL)
1690 vha->vha_tgt.qla_tgt->notify_ack_expected++;
1692 pkt->entry_type = NOTIFY_ACK_TYPE;
1693 pkt->entry_count = 1;
1695 nack = (struct nack_to_isp *)pkt;
1696 nack->ox_id = ntfy->ox_id;
1698 nack->u.isp24.handle = QLA_TGT_SKIP_HANDLE;
1699 nack->u.isp24.nport_handle = ntfy->u.isp24.nport_handle;
1700 if (le16_to_cpu(ntfy->u.isp24.status) == IMM_NTFY_ELS) {
1701 nack->u.isp24.flags = ntfy->u.isp24.flags &
1702 cpu_to_le16(NOTIFY24XX_FLAGS_PUREX_IOCB);
1704 nack->u.isp24.srr_rx_id = ntfy->u.isp24.srr_rx_id;
1705 nack->u.isp24.status = ntfy->u.isp24.status;
1706 nack->u.isp24.status_subcode = ntfy->u.isp24.status_subcode;
1707 nack->u.isp24.fw_handle = ntfy->u.isp24.fw_handle;
1708 nack->u.isp24.exchange_address = ntfy->u.isp24.exchange_address;
1709 nack->u.isp24.srr_rel_offs = ntfy->u.isp24.srr_rel_offs;
1710 nack->u.isp24.srr_ui = ntfy->u.isp24.srr_ui;
1711 nack->u.isp24.srr_flags = cpu_to_le16(srr_flags);
1712 nack->u.isp24.srr_reject_code = srr_reject_code;
1713 nack->u.isp24.srr_reject_code_expl = srr_explan;
1714 nack->u.isp24.vp_index = ntfy->u.isp24.vp_index;
1716 ql_dbg(ql_dbg_tgt, vha, 0xe005,
1717 "qla_target(%d): Sending 24xx Notify Ack %d\n",
1718 vha->vp_idx, nack->u.isp24.status);
1720 /* Memory Barrier */
1722 qla2x00_start_iocbs(vha, qpair->req);
1725 static int qlt_build_abts_resp_iocb(struct qla_tgt_mgmt_cmd *mcmd)
1727 struct scsi_qla_host *vha = mcmd->vha;
1728 struct qla_hw_data *ha = vha->hw;
1729 struct abts_resp_to_24xx *resp;
1734 struct abts_recv_from_24xx *abts = &mcmd->orig_iocb.abts;
1735 struct qla_qpair *qpair = mcmd->qpair;
1737 ql_dbg(ql_dbg_tgt, vha, 0xe006,
1738 "Sending task mgmt ABTS response (ha=%p, status=%x)\n",
1739 ha, mcmd->fc_tm_rsp);
1741 rc = qlt_check_reserve_free_req(qpair, 1);
1743 ql_dbg(ql_dbg_tgt, vha, 0xe04a,
1744 "qla_target(%d): %s failed: unable to allocate request packet\n",
1745 vha->vp_idx, __func__);
1749 resp = (struct abts_resp_to_24xx *)qpair->req->ring_ptr;
1750 memset(resp, 0, sizeof(*resp));
1752 h = qlt_make_handle(qpair);
1753 if (unlikely(h == QLA_TGT_NULL_HANDLE)) {
1755 * CTIO type 7 from the firmware doesn't provide a way to
1756 * know the initiator's LOOP ID, hence we can't find
1757 * the session and, so, the command.
1761 qpair->req->outstanding_cmds[h] = (srb_t *)mcmd;
1764 resp->handle = make_handle(qpair->req->id, h);
1765 resp->entry_type = ABTS_RESP_24XX;
1766 resp->entry_count = 1;
1767 resp->nport_handle = abts->nport_handle;
1768 resp->vp_index = vha->vp_idx;
1769 resp->sof_type = abts->sof_type;
1770 resp->exchange_address = abts->exchange_address;
1771 resp->fcp_hdr_le = abts->fcp_hdr_le;
1772 f_ctl = cpu_to_le32(F_CTL_EXCH_CONTEXT_RESP |
1773 F_CTL_LAST_SEQ | F_CTL_END_SEQ |
1774 F_CTL_SEQ_INITIATIVE);
1775 p = (uint8_t *)&f_ctl;
1776 resp->fcp_hdr_le.f_ctl[0] = *p++;
1777 resp->fcp_hdr_le.f_ctl[1] = *p++;
1778 resp->fcp_hdr_le.f_ctl[2] = *p;
1780 resp->fcp_hdr_le.d_id = abts->fcp_hdr_le.s_id;
1781 resp->fcp_hdr_le.s_id = abts->fcp_hdr_le.d_id;
1783 resp->exchange_addr_to_abort = abts->exchange_addr_to_abort;
1784 if (mcmd->fc_tm_rsp == FCP_TMF_CMPL) {
1785 resp->fcp_hdr_le.r_ctl = R_CTL_BASIC_LINK_SERV | R_CTL_B_ACC;
1786 resp->payload.ba_acct.seq_id_valid = SEQ_ID_INVALID;
1787 resp->payload.ba_acct.low_seq_cnt = 0x0000;
1788 resp->payload.ba_acct.high_seq_cnt = cpu_to_le16(0xFFFF);
1789 resp->payload.ba_acct.ox_id = abts->fcp_hdr_le.ox_id;
1790 resp->payload.ba_acct.rx_id = abts->fcp_hdr_le.rx_id;
1792 resp->fcp_hdr_le.r_ctl = R_CTL_BASIC_LINK_SERV | R_CTL_B_RJT;
1793 resp->payload.ba_rjt.reason_code =
1794 BA_RJT_REASON_CODE_UNABLE_TO_PERFORM;
1795 /* Other bytes are zero */
1798 vha->vha_tgt.qla_tgt->abts_resp_expected++;
1800 /* Memory Barrier */
1802 if (qpair->reqq_start_iocbs)
1803 qpair->reqq_start_iocbs(qpair);
1805 qla2x00_start_iocbs(vha, qpair->req);
1811 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
1813 static void qlt_24xx_send_abts_resp(struct qla_qpair *qpair,
1814 struct abts_recv_from_24xx *abts, uint32_t status,
1817 struct scsi_qla_host *vha = qpair->vha;
1818 struct qla_hw_data *ha = vha->hw;
1819 struct abts_resp_to_24xx *resp;
1823 ql_dbg(ql_dbg_tgt, vha, 0xe006,
1824 "Sending task mgmt ABTS response (ha=%p, atio=%p, status=%x\n",
1827 resp = (struct abts_resp_to_24xx *)qla2x00_alloc_iocbs_ready(qpair,
1830 ql_dbg(ql_dbg_tgt, vha, 0xe04a,
1831 "qla_target(%d): %s failed: unable to allocate "
1832 "request packet", vha->vp_idx, __func__);
1836 resp->entry_type = ABTS_RESP_24XX;
1837 resp->handle = QLA_TGT_SKIP_HANDLE;
1838 resp->entry_count = 1;
1839 resp->nport_handle = abts->nport_handle;
1840 resp->vp_index = vha->vp_idx;
1841 resp->sof_type = abts->sof_type;
1842 resp->exchange_address = abts->exchange_address;
1843 resp->fcp_hdr_le = abts->fcp_hdr_le;
1844 f_ctl = cpu_to_le32(F_CTL_EXCH_CONTEXT_RESP |
1845 F_CTL_LAST_SEQ | F_CTL_END_SEQ |
1846 F_CTL_SEQ_INITIATIVE);
1847 p = (uint8_t *)&f_ctl;
1848 resp->fcp_hdr_le.f_ctl[0] = *p++;
1849 resp->fcp_hdr_le.f_ctl[1] = *p++;
1850 resp->fcp_hdr_le.f_ctl[2] = *p;
1852 resp->fcp_hdr_le.d_id = abts->fcp_hdr_le.d_id;
1853 resp->fcp_hdr_le.s_id = abts->fcp_hdr_le.s_id;
1855 resp->fcp_hdr_le.d_id = abts->fcp_hdr_le.s_id;
1856 resp->fcp_hdr_le.s_id = abts->fcp_hdr_le.d_id;
1858 resp->exchange_addr_to_abort = abts->exchange_addr_to_abort;
1859 if (status == FCP_TMF_CMPL) {
1860 resp->fcp_hdr_le.r_ctl = R_CTL_BASIC_LINK_SERV | R_CTL_B_ACC;
1861 resp->payload.ba_acct.seq_id_valid = SEQ_ID_INVALID;
1862 resp->payload.ba_acct.low_seq_cnt = 0x0000;
1863 resp->payload.ba_acct.high_seq_cnt = cpu_to_le16(0xFFFF);
1864 resp->payload.ba_acct.ox_id = abts->fcp_hdr_le.ox_id;
1865 resp->payload.ba_acct.rx_id = abts->fcp_hdr_le.rx_id;
1867 resp->fcp_hdr_le.r_ctl = R_CTL_BASIC_LINK_SERV | R_CTL_B_RJT;
1868 resp->payload.ba_rjt.reason_code =
1869 BA_RJT_REASON_CODE_UNABLE_TO_PERFORM;
1870 /* Other bytes are zero */
1873 vha->vha_tgt.qla_tgt->abts_resp_expected++;
1875 /* Memory Barrier */
1877 if (qpair->reqq_start_iocbs)
1878 qpair->reqq_start_iocbs(qpair);
1880 qla2x00_start_iocbs(vha, qpair->req);
1884 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
1886 static void qlt_24xx_retry_term_exchange(struct scsi_qla_host *vha,
1887 struct qla_qpair *qpair, response_t *pkt, struct qla_tgt_mgmt_cmd *mcmd)
1889 struct ctio7_to_24xx *ctio;
1891 struct abts_recv_from_24xx *entry;
1893 ctio = (struct ctio7_to_24xx *)qla2x00_alloc_iocbs_ready(qpair, NULL);
1895 ql_dbg(ql_dbg_tgt, vha, 0xe04b,
1896 "qla_target(%d): %s failed: unable to allocate "
1897 "request packet\n", vha->vp_idx, __func__);
1902 /* abts from remote port */
1903 entry = &mcmd->orig_iocb.abts;
1905 /* abts from this driver. */
1906 entry = (struct abts_recv_from_24xx *)pkt;
1909 * We've got on entrance firmware's response on by us generated
1910 * ABTS response. So, in it ID fields are reversed.
1913 ctio->entry_type = CTIO_TYPE7;
1914 ctio->entry_count = 1;
1915 ctio->nport_handle = entry->nport_handle;
1916 ctio->handle = QLA_TGT_SKIP_HANDLE | CTIO_COMPLETION_HANDLE_MARK;
1917 ctio->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
1918 ctio->vp_index = vha->vp_idx;
1919 ctio->exchange_addr = entry->exchange_addr_to_abort;
1920 tmp = (CTIO7_FLAGS_STATUS_MODE_1 | CTIO7_FLAGS_TERMINATE);
1923 ctio->initiator_id = entry->fcp_hdr_le.s_id;
1925 if (mcmd->flags & QLA24XX_MGMT_ABORT_IO_ATTR_VALID)
1926 tmp |= (mcmd->abort_io_attr << 9);
1927 else if (qpair->retry_term_cnt & 1)
1930 ctio->initiator_id = entry->fcp_hdr_le.d_id;
1932 if (qpair->retry_term_cnt & 1)
1935 ctio->u.status1.flags = cpu_to_le16(tmp);
1936 ctio->u.status1.ox_id = entry->fcp_hdr_le.ox_id;
1938 ql_dbg(ql_dbg_tgt, vha, 0xe007,
1939 "Sending retry TERM EXCH CTIO7 flags %04xh oxid %04xh attr valid %x\n",
1940 le16_to_cpu(ctio->u.status1.flags),
1941 le16_to_cpu(ctio->u.status1.ox_id),
1942 (mcmd && mcmd->flags & QLA24XX_MGMT_ABORT_IO_ATTR_VALID) ? 1 : 0);
1944 /* Memory Barrier */
1946 if (qpair->reqq_start_iocbs)
1947 qpair->reqq_start_iocbs(qpair);
1949 qla2x00_start_iocbs(vha, qpair->req);
1952 qlt_build_abts_resp_iocb(mcmd);
1954 qlt_24xx_send_abts_resp(qpair,
1955 (struct abts_recv_from_24xx *)entry, FCP_TMF_CMPL, true);
1959 /* drop cmds for the given lun
1960 * XXX only looks for cmds on the port through which lun reset was recieved
1961 * XXX does not go through the list of other port (which may have cmds
1964 static void abort_cmds_for_lun(struct scsi_qla_host *vha, u64 lun, be_id_t s_id)
1966 struct qla_tgt_sess_op *op;
1967 struct qla_tgt_cmd *cmd;
1969 unsigned long flags;
1971 key = sid_to_key(s_id);
1972 spin_lock_irqsave(&vha->cmd_list_lock, flags);
1973 list_for_each_entry(op, &vha->qla_sess_op_cmd_list, cmd_list) {
1977 op_key = sid_to_key(op->atio.u.isp24.fcp_hdr.s_id);
1978 op_lun = scsilun_to_int(
1979 (struct scsi_lun *)&op->atio.u.isp24.fcp_cmnd.lun);
1980 if (op_key == key && op_lun == lun)
1984 list_for_each_entry(op, &vha->unknown_atio_list, cmd_list) {
1988 op_key = sid_to_key(op->atio.u.isp24.fcp_hdr.s_id);
1989 op_lun = scsilun_to_int(
1990 (struct scsi_lun *)&op->atio.u.isp24.fcp_cmnd.lun);
1991 if (op_key == key && op_lun == lun)
1995 list_for_each_entry(cmd, &vha->qla_cmd_list, cmd_list) {
1999 cmd_key = sid_to_key(cmd->atio.u.isp24.fcp_hdr.s_id);
2000 cmd_lun = scsilun_to_int(
2001 (struct scsi_lun *)&cmd->atio.u.isp24.fcp_cmnd.lun);
2002 if (cmd_key == key && cmd_lun == lun)
2005 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
2008 static struct qla_qpair_hint *qlt_find_qphint(struct scsi_qla_host *vha,
2009 uint64_t unpacked_lun)
2011 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
2012 struct qla_qpair_hint *h = NULL;
2014 if (vha->flags.qpairs_available) {
2015 h = btree_lookup64(&tgt->lun_qpair_map, unpacked_lun);
2017 h = &tgt->qphints[0];
2019 h = &tgt->qphints[0];
2025 static void qlt_do_tmr_work(struct work_struct *work)
2027 struct qla_tgt_mgmt_cmd *mcmd =
2028 container_of(work, struct qla_tgt_mgmt_cmd, work);
2029 struct qla_hw_data *ha = mcmd->vha->hw;
2032 unsigned long flags;
2034 switch (mcmd->tmr_func) {
2036 tag = le32_to_cpu(mcmd->orig_iocb.abts.exchange_addr_to_abort);
2043 rc = ha->tgt.tgt_ops->handle_tmr(mcmd, mcmd->unpacked_lun,
2044 mcmd->tmr_func, tag);
2047 spin_lock_irqsave(mcmd->qpair->qp_lock_ptr, flags);
2048 switch (mcmd->tmr_func) {
2050 mcmd->fc_tm_rsp = FCP_TMF_REJECTED;
2051 qlt_build_abts_resp_iocb(mcmd);
2053 case QLA_TGT_LUN_RESET:
2054 case QLA_TGT_CLEAR_TS:
2055 case QLA_TGT_ABORT_TS:
2056 case QLA_TGT_CLEAR_ACA:
2057 case QLA_TGT_TARGET_RESET:
2058 qlt_send_busy(mcmd->qpair, &mcmd->orig_iocb.atio,
2062 case QLA_TGT_ABORT_ALL:
2063 case QLA_TGT_NEXUS_LOSS_SESS:
2064 case QLA_TGT_NEXUS_LOSS:
2065 qlt_send_notify_ack(mcmd->qpair,
2066 &mcmd->orig_iocb.imm_ntfy, 0, 0, 0, 0, 0, 0);
2069 spin_unlock_irqrestore(mcmd->qpair->qp_lock_ptr, flags);
2071 ql_dbg(ql_dbg_tgt_mgt, mcmd->vha, 0xf052,
2072 "qla_target(%d): tgt_ops->handle_tmr() failed: %d\n",
2073 mcmd->vha->vp_idx, rc);
2074 mempool_free(mcmd, qla_tgt_mgmt_cmd_mempool);
2078 /* ha->hardware_lock supposed to be held on entry */
2079 static int __qlt_24xx_handle_abts(struct scsi_qla_host *vha,
2080 struct abts_recv_from_24xx *abts, struct fc_port *sess)
2082 struct qla_hw_data *ha = vha->hw;
2083 struct qla_tgt_mgmt_cmd *mcmd;
2084 struct qla_qpair_hint *h = &vha->vha_tgt.qla_tgt->qphints[0];
2085 struct qla_tgt_cmd *abort_cmd;
2087 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00f,
2088 "qla_target(%d): task abort (tag=%d)\n",
2089 vha->vp_idx, abts->exchange_addr_to_abort);
2091 mcmd = mempool_alloc(qla_tgt_mgmt_cmd_mempool, GFP_ATOMIC);
2093 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf051,
2094 "qla_target(%d): %s: Allocation of ABORT cmd failed",
2095 vha->vp_idx, __func__);
2098 memset(mcmd, 0, sizeof(*mcmd));
2099 mcmd->cmd_type = TYPE_TGT_TMCMD;
2101 memcpy(&mcmd->orig_iocb.abts, abts, sizeof(mcmd->orig_iocb.abts));
2102 mcmd->reset_count = ha->base_qpair->chip_reset;
2103 mcmd->tmr_func = QLA_TGT_ABTS;
2104 mcmd->qpair = h->qpair;
2108 * LUN is looked up by target-core internally based on the passed
2109 * abts->exchange_addr_to_abort tag.
2111 mcmd->se_cmd.cpuid = h->cpuid;
2113 abort_cmd = ha->tgt.tgt_ops->find_cmd_by_tag(sess,
2114 le32_to_cpu(abts->exchange_addr_to_abort));
2117 mcmd->unpacked_lun = abort_cmd->se_cmd.orig_fe_lun;
2119 if (abort_cmd->qpair) {
2120 mcmd->qpair = abort_cmd->qpair;
2121 mcmd->se_cmd.cpuid = abort_cmd->se_cmd.cpuid;
2122 mcmd->abort_io_attr = abort_cmd->atio.u.isp24.attr;
2123 mcmd->flags = QLA24XX_MGMT_ABORT_IO_ATTR_VALID;
2126 INIT_WORK(&mcmd->work, qlt_do_tmr_work);
2127 queue_work_on(mcmd->se_cmd.cpuid, qla_tgt_wq, &mcmd->work);
2133 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
2135 static void qlt_24xx_handle_abts(struct scsi_qla_host *vha,
2136 struct abts_recv_from_24xx *abts)
2138 struct qla_hw_data *ha = vha->hw;
2139 struct fc_port *sess;
2140 uint32_t tag = le32_to_cpu(abts->exchange_addr_to_abort);
2143 unsigned long flags;
2145 if (le32_to_cpu(abts->fcp_hdr_le.parameter) & ABTS_PARAM_ABORT_SEQ) {
2146 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf053,
2147 "qla_target(%d): ABTS: Abort Sequence not "
2148 "supported\n", vha->vp_idx);
2149 qlt_24xx_send_abts_resp(ha->base_qpair, abts, FCP_TMF_REJECTED,
2154 if (tag == ATIO_EXCHANGE_ADDRESS_UNKNOWN) {
2155 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf010,
2156 "qla_target(%d): ABTS: Unknown Exchange "
2157 "Address received\n", vha->vp_idx);
2158 qlt_24xx_send_abts_resp(ha->base_qpair, abts, FCP_TMF_REJECTED,
2163 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf011,
2164 "qla_target(%d): task abort (s_id=%x:%x:%x, "
2165 "tag=%d, param=%x)\n", vha->vp_idx, abts->fcp_hdr_le.s_id.domain,
2166 abts->fcp_hdr_le.s_id.area, abts->fcp_hdr_le.s_id.al_pa, tag,
2167 le32_to_cpu(abts->fcp_hdr_le.parameter));
2169 s_id = le_id_to_be(abts->fcp_hdr_le.s_id);
2171 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
2172 sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha, s_id);
2174 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf012,
2175 "qla_target(%d): task abort for non-existent session\n",
2177 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
2179 qlt_24xx_send_abts_resp(ha->base_qpair, abts, FCP_TMF_REJECTED,
2183 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
2186 if (sess->deleted) {
2187 qlt_24xx_send_abts_resp(ha->base_qpair, abts, FCP_TMF_REJECTED,
2192 rc = __qlt_24xx_handle_abts(vha, abts, sess);
2194 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf054,
2195 "qla_target(%d): __qlt_24xx_handle_abts() failed: %d\n",
2197 qlt_24xx_send_abts_resp(ha->base_qpair, abts, FCP_TMF_REJECTED,
2204 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
2206 static void qlt_24xx_send_task_mgmt_ctio(struct qla_qpair *qpair,
2207 struct qla_tgt_mgmt_cmd *mcmd, uint32_t resp_code)
2209 struct scsi_qla_host *ha = mcmd->vha;
2210 struct atio_from_isp *atio = &mcmd->orig_iocb.atio;
2211 struct ctio7_to_24xx *ctio;
2214 ql_dbg(ql_dbg_tgt, ha, 0xe008,
2215 "Sending task mgmt CTIO7 (ha=%p, atio=%p, resp_code=%x\n",
2216 ha, atio, resp_code);
2219 ctio = (struct ctio7_to_24xx *)__qla2x00_alloc_iocbs(qpair, NULL);
2221 ql_dbg(ql_dbg_tgt, ha, 0xe04c,
2222 "qla_target(%d): %s failed: unable to allocate "
2223 "request packet\n", ha->vp_idx, __func__);
2227 ctio->entry_type = CTIO_TYPE7;
2228 ctio->entry_count = 1;
2229 ctio->handle = QLA_TGT_SKIP_HANDLE | CTIO_COMPLETION_HANDLE_MARK;
2230 ctio->nport_handle = cpu_to_le16(mcmd->sess->loop_id);
2231 ctio->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
2232 ctio->vp_index = ha->vp_idx;
2233 ctio->initiator_id = be_id_to_le(atio->u.isp24.fcp_hdr.s_id);
2234 ctio->exchange_addr = atio->u.isp24.exchange_addr;
2235 temp = (atio->u.isp24.attr << 9)|
2236 CTIO7_FLAGS_STATUS_MODE_1 | CTIO7_FLAGS_SEND_STATUS;
2237 ctio->u.status1.flags = cpu_to_le16(temp);
2238 temp = be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id);
2239 ctio->u.status1.ox_id = cpu_to_le16(temp);
2240 ctio->u.status1.scsi_status =
2241 cpu_to_le16(SS_RESPONSE_INFO_LEN_VALID);
2242 ctio->u.status1.response_len = cpu_to_le16(8);
2243 ctio->u.status1.sense_data[0] = resp_code;
2245 /* Memory Barrier */
2247 if (qpair->reqq_start_iocbs)
2248 qpair->reqq_start_iocbs(qpair);
2250 qla2x00_start_iocbs(ha, qpair->req);
2253 void qlt_free_mcmd(struct qla_tgt_mgmt_cmd *mcmd)
2255 mempool_free(mcmd, qla_tgt_mgmt_cmd_mempool);
2257 EXPORT_SYMBOL(qlt_free_mcmd);
2260 * ha->hardware_lock supposed to be held on entry. Might drop it, then
2263 void qlt_send_resp_ctio(struct qla_qpair *qpair, struct qla_tgt_cmd *cmd,
2264 uint8_t scsi_status, uint8_t sense_key, uint8_t asc, uint8_t ascq)
2266 struct atio_from_isp *atio = &cmd->atio;
2267 struct ctio7_to_24xx *ctio;
2269 struct scsi_qla_host *vha = cmd->vha;
2271 ql_dbg(ql_dbg_tgt_dif, vha, 0x3066,
2272 "Sending response CTIO7 (vha=%p, atio=%p, scsi_status=%02x, "
2273 "sense_key=%02x, asc=%02x, ascq=%02x",
2274 vha, atio, scsi_status, sense_key, asc, ascq);
2276 ctio = (struct ctio7_to_24xx *)qla2x00_alloc_iocbs(vha, NULL);
2278 ql_dbg(ql_dbg_async, vha, 0x3067,
2279 "qla2x00t(%ld): %s failed: unable to allocate request packet",
2280 vha->host_no, __func__);
2284 ctio->entry_type = CTIO_TYPE7;
2285 ctio->entry_count = 1;
2286 ctio->handle = QLA_TGT_SKIP_HANDLE;
2287 ctio->nport_handle = cpu_to_le16(cmd->sess->loop_id);
2288 ctio->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
2289 ctio->vp_index = vha->vp_idx;
2290 ctio->initiator_id = be_id_to_le(atio->u.isp24.fcp_hdr.s_id);
2291 ctio->exchange_addr = atio->u.isp24.exchange_addr;
2292 temp = (atio->u.isp24.attr << 9) |
2293 CTIO7_FLAGS_STATUS_MODE_1 | CTIO7_FLAGS_SEND_STATUS;
2294 ctio->u.status1.flags = cpu_to_le16(temp);
2295 temp = be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id);
2296 ctio->u.status1.ox_id = cpu_to_le16(temp);
2297 ctio->u.status1.scsi_status =
2298 cpu_to_le16(SS_RESPONSE_INFO_LEN_VALID | scsi_status);
2299 ctio->u.status1.response_len = cpu_to_le16(18);
2300 ctio->u.status1.residual = cpu_to_le32(get_datalen_for_atio(atio));
2302 if (ctio->u.status1.residual != 0)
2303 ctio->u.status1.scsi_status |=
2304 cpu_to_le16(SS_RESIDUAL_UNDER);
2306 /* Fixed format sense data. */
2307 ctio->u.status1.sense_data[0] = 0x70;
2308 ctio->u.status1.sense_data[2] = sense_key;
2309 /* Additional sense length */
2310 ctio->u.status1.sense_data[7] = 0xa;
2312 ctio->u.status1.sense_data[12] = asc;
2313 ctio->u.status1.sense_data[13] = ascq;
2315 /* Memory Barrier */
2318 if (qpair->reqq_start_iocbs)
2319 qpair->reqq_start_iocbs(qpair);
2321 qla2x00_start_iocbs(vha, qpair->req);
2327 /* callback from target fabric module code */
2328 void qlt_xmit_tm_rsp(struct qla_tgt_mgmt_cmd *mcmd)
2330 struct scsi_qla_host *vha = mcmd->sess->vha;
2331 struct qla_hw_data *ha = vha->hw;
2332 unsigned long flags;
2333 struct qla_qpair *qpair = mcmd->qpair;
2334 bool free_mcmd = true;
2336 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf013,
2337 "TM response mcmd (%p) status %#x state %#x",
2338 mcmd, mcmd->fc_tm_rsp, mcmd->flags);
2340 spin_lock_irqsave(qpair->qp_lock_ptr, flags);
2342 if (!vha->flags.online || mcmd->reset_count != qpair->chip_reset) {
2344 * Either the port is not online or this request was from
2345 * previous life, just abort the processing.
2347 ql_dbg(ql_dbg_async, vha, 0xe100,
2348 "RESET-TMR online/active/old-count/new-count = %d/%d/%d/%d.\n",
2349 vha->flags.online, qla2x00_reset_active(vha),
2350 mcmd->reset_count, qpair->chip_reset);
2351 ha->tgt.tgt_ops->free_mcmd(mcmd);
2352 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
2356 if (mcmd->flags == QLA24XX_MGMT_SEND_NACK) {
2357 switch (mcmd->orig_iocb.imm_ntfy.u.isp24.status_subcode) {
2361 ql_dbg(ql_dbg_disc, vha, 0x2106,
2362 "TM response logo %8phC status %#x state %#x",
2363 mcmd->sess->port_name, mcmd->fc_tm_rsp,
2365 qlt_schedule_sess_for_deletion(mcmd->sess);
2368 qlt_send_notify_ack(vha->hw->base_qpair,
2369 &mcmd->orig_iocb.imm_ntfy, 0, 0, 0, 0, 0, 0);
2373 if (mcmd->orig_iocb.atio.u.raw.entry_type == ABTS_RECV_24XX) {
2374 qlt_build_abts_resp_iocb(mcmd);
2377 qlt_24xx_send_task_mgmt_ctio(qpair, mcmd,
2381 * Make the callback for ->free_mcmd() to queue_work() and invoke
2382 * target_put_sess_cmd() to drop cmd_kref to 1. The final
2383 * target_put_sess_cmd() call will be made from TFO->check_stop_free()
2384 * -> tcm_qla2xxx_check_stop_free() to release the TMR associated se_cmd
2385 * descriptor after TFO->queue_tm_rsp() -> tcm_qla2xxx_queue_tm_rsp() ->
2386 * qlt_xmit_tm_rsp() returns here..
2389 ha->tgt.tgt_ops->free_mcmd(mcmd);
2391 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
2393 EXPORT_SYMBOL(qlt_xmit_tm_rsp);
2396 static int qlt_pci_map_calc_cnt(struct qla_tgt_prm *prm)
2398 struct qla_tgt_cmd *cmd = prm->cmd;
2400 BUG_ON(cmd->sg_cnt == 0);
2402 prm->sg = (struct scatterlist *)cmd->sg;
2403 prm->seg_cnt = dma_map_sg(&cmd->qpair->pdev->dev, cmd->sg,
2404 cmd->sg_cnt, cmd->dma_data_direction);
2405 if (unlikely(prm->seg_cnt == 0))
2408 prm->cmd->sg_mapped = 1;
2410 if (cmd->se_cmd.prot_op == TARGET_PROT_NORMAL) {
2412 * If greater than four sg entries then we need to allocate
2413 * the continuation entries
2415 if (prm->seg_cnt > QLA_TGT_DATASEGS_PER_CMD_24XX)
2416 prm->req_cnt += DIV_ROUND_UP(prm->seg_cnt -
2417 QLA_TGT_DATASEGS_PER_CMD_24XX,
2418 QLA_TGT_DATASEGS_PER_CONT_24XX);
2421 if ((cmd->se_cmd.prot_op == TARGET_PROT_DIN_INSERT) ||
2422 (cmd->se_cmd.prot_op == TARGET_PROT_DOUT_STRIP)) {
2423 prm->seg_cnt = DIV_ROUND_UP(cmd->bufflen, cmd->blk_sz);
2424 prm->tot_dsds = prm->seg_cnt;
2426 prm->tot_dsds = prm->seg_cnt;
2428 if (cmd->prot_sg_cnt) {
2429 prm->prot_sg = cmd->prot_sg;
2430 prm->prot_seg_cnt = dma_map_sg(&cmd->qpair->pdev->dev,
2431 cmd->prot_sg, cmd->prot_sg_cnt,
2432 cmd->dma_data_direction);
2433 if (unlikely(prm->prot_seg_cnt == 0))
2436 if ((cmd->se_cmd.prot_op == TARGET_PROT_DIN_INSERT) ||
2437 (cmd->se_cmd.prot_op == TARGET_PROT_DOUT_STRIP)) {
2438 /* Dif Bundling not support here */
2439 prm->prot_seg_cnt = DIV_ROUND_UP(cmd->bufflen,
2441 prm->tot_dsds += prm->prot_seg_cnt;
2443 prm->tot_dsds += prm->prot_seg_cnt;
2450 ql_dbg_qp(ql_dbg_tgt, prm->cmd->qpair, 0xe04d,
2451 "qla_target(%d): PCI mapping failed: sg_cnt=%d",
2452 0, prm->cmd->sg_cnt);
2456 static void qlt_unmap_sg(struct scsi_qla_host *vha, struct qla_tgt_cmd *cmd)
2458 struct qla_hw_data *ha;
2459 struct qla_qpair *qpair;
2461 if (!cmd->sg_mapped)
2466 dma_unmap_sg(&qpair->pdev->dev, cmd->sg, cmd->sg_cnt,
2467 cmd->dma_data_direction);
2470 if (cmd->prot_sg_cnt)
2471 dma_unmap_sg(&qpair->pdev->dev, cmd->prot_sg, cmd->prot_sg_cnt,
2472 cmd->dma_data_direction);
2477 if (cmd->ctx_dsd_alloced)
2478 qla2x00_clean_dsd_pool(ha, cmd->ctx);
2480 dma_pool_free(ha->dl_dma_pool, cmd->ctx, cmd->ctx->crc_ctx_dma);
2483 static int qlt_check_reserve_free_req(struct qla_qpair *qpair,
2487 struct req_que *req = qpair->req;
2489 if (req->cnt < (req_cnt + 2)) {
2490 cnt = (uint16_t)(qpair->use_shadow_reg ? *req->out_ptr :
2491 rd_reg_dword_relaxed(req->req_q_out));
2493 if (req->ring_index < cnt)
2494 req->cnt = cnt - req->ring_index;
2496 req->cnt = req->length - (req->ring_index - cnt);
2498 if (unlikely(req->cnt < (req_cnt + 2)))
2502 req->cnt -= req_cnt;
2508 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
2510 static inline void *qlt_get_req_pkt(struct req_que *req)
2512 /* Adjust ring index. */
2514 if (req->ring_index == req->length) {
2515 req->ring_index = 0;
2516 req->ring_ptr = req->ring;
2520 return (cont_entry_t *)req->ring_ptr;
2523 /* ha->hardware_lock supposed to be held on entry */
2524 static inline uint32_t qlt_make_handle(struct qla_qpair *qpair)
2529 struct req_que *req = qpair->req;
2531 h = req->current_outstanding_cmd;
2533 for (index = 1; index < req->num_outstanding_cmds; index++) {
2535 if (h == req->num_outstanding_cmds)
2538 if (h == QLA_TGT_SKIP_HANDLE)
2541 if (!req->outstanding_cmds[h]) {
2548 req->current_outstanding_cmd = h;
2550 ql_dbg(ql_dbg_io, qpair->vha, 0x305b,
2551 "qla_target(%d): Ran out of empty cmd slots\n",
2552 qpair->vha->vp_idx);
2553 h = QLA_TGT_NULL_HANDLE;
2559 /* ha->hardware_lock supposed to be held on entry */
2560 static int qlt_24xx_build_ctio_pkt(struct qla_qpair *qpair,
2561 struct qla_tgt_prm *prm)
2564 struct ctio7_to_24xx *pkt;
2565 struct atio_from_isp *atio = &prm->cmd->atio;
2568 pkt = (struct ctio7_to_24xx *)qpair->req->ring_ptr;
2570 memset(pkt, 0, sizeof(*pkt));
2572 pkt->entry_type = CTIO_TYPE7;
2573 pkt->entry_count = (uint8_t)prm->req_cnt;
2574 pkt->vp_index = prm->cmd->vp_idx;
2576 h = qlt_make_handle(qpair);
2577 if (unlikely(h == QLA_TGT_NULL_HANDLE)) {
2579 * CTIO type 7 from the firmware doesn't provide a way to
2580 * know the initiator's LOOP ID, hence we can't find
2581 * the session and, so, the command.
2585 qpair->req->outstanding_cmds[h] = (srb_t *)prm->cmd;
2587 pkt->handle = make_handle(qpair->req->id, h);
2588 pkt->handle |= CTIO_COMPLETION_HANDLE_MARK;
2589 pkt->nport_handle = cpu_to_le16(prm->cmd->loop_id);
2590 pkt->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
2591 pkt->initiator_id = be_id_to_le(atio->u.isp24.fcp_hdr.s_id);
2592 pkt->exchange_addr = atio->u.isp24.exchange_addr;
2593 temp = atio->u.isp24.attr << 9;
2594 pkt->u.status0.flags |= cpu_to_le16(temp);
2595 temp = be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id);
2596 pkt->u.status0.ox_id = cpu_to_le16(temp);
2597 pkt->u.status0.relative_offset = cpu_to_le32(prm->cmd->offset);
2603 * ha->hardware_lock supposed to be held on entry. We have already made sure
2604 * that there is sufficient amount of request entries to not drop it.
2606 static void qlt_load_cont_data_segments(struct qla_tgt_prm *prm)
2609 struct dsd64 *cur_dsd;
2611 /* Build continuation packets */
2612 while (prm->seg_cnt > 0) {
2613 cont_a64_entry_t *cont_pkt64 =
2614 (cont_a64_entry_t *)qlt_get_req_pkt(
2615 prm->cmd->qpair->req);
2618 * Make sure that from cont_pkt64 none of
2619 * 64-bit specific fields used for 32-bit
2620 * addressing. Cast to (cont_entry_t *) for
2624 memset(cont_pkt64, 0, sizeof(*cont_pkt64));
2626 cont_pkt64->entry_count = 1;
2627 cont_pkt64->sys_define = 0;
2629 cont_pkt64->entry_type = CONTINUE_A64_TYPE;
2630 cur_dsd = cont_pkt64->dsd;
2632 /* Load continuation entry data segments */
2634 cnt < QLA_TGT_DATASEGS_PER_CONT_24XX && prm->seg_cnt;
2635 cnt++, prm->seg_cnt--) {
2636 append_dsd64(&cur_dsd, prm->sg);
2637 prm->sg = sg_next(prm->sg);
2643 * ha->hardware_lock supposed to be held on entry. We have already made sure
2644 * that there is sufficient amount of request entries to not drop it.
2646 static void qlt_load_data_segments(struct qla_tgt_prm *prm)
2649 struct dsd64 *cur_dsd;
2650 struct ctio7_to_24xx *pkt24 = (struct ctio7_to_24xx *)prm->pkt;
2652 pkt24->u.status0.transfer_length = cpu_to_le32(prm->cmd->bufflen);
2654 /* Setup packet address segment pointer */
2655 cur_dsd = &pkt24->u.status0.dsd;
2657 /* Set total data segment count */
2659 pkt24->dseg_count = cpu_to_le16(prm->seg_cnt);
2661 if (prm->seg_cnt == 0) {
2662 /* No data transfer */
2663 cur_dsd->address = 0;
2664 cur_dsd->length = 0;
2668 /* If scatter gather */
2670 /* Load command entry data segments */
2672 (cnt < QLA_TGT_DATASEGS_PER_CMD_24XX) && prm->seg_cnt;
2673 cnt++, prm->seg_cnt--) {
2674 append_dsd64(&cur_dsd, prm->sg);
2675 prm->sg = sg_next(prm->sg);
2678 qlt_load_cont_data_segments(prm);
2681 static inline int qlt_has_data(struct qla_tgt_cmd *cmd)
2683 return cmd->bufflen > 0;
2686 static void qlt_print_dif_err(struct qla_tgt_prm *prm)
2688 struct qla_tgt_cmd *cmd;
2689 struct scsi_qla_host *vha;
2691 /* asc 0x10=dif error */
2692 if (prm->sense_buffer && (prm->sense_buffer[12] == 0x10)) {
2696 switch (prm->sense_buffer[13]) {
2698 ql_dbg(ql_dbg_tgt_dif, vha, 0xe00b,
2699 "BE detected Guard TAG ERR: lba[0x%llx|%lld] len[0x%x] "
2700 "se_cmd=%p tag[%x]",
2701 cmd->lba, cmd->lba, cmd->num_blks, &cmd->se_cmd,
2702 cmd->atio.u.isp24.exchange_addr);
2705 ql_dbg(ql_dbg_tgt_dif, vha, 0xe00c,
2706 "BE detected APP TAG ERR: lba[0x%llx|%lld] len[0x%x] "
2707 "se_cmd=%p tag[%x]",
2708 cmd->lba, cmd->lba, cmd->num_blks, &cmd->se_cmd,
2709 cmd->atio.u.isp24.exchange_addr);
2712 ql_dbg(ql_dbg_tgt_dif, vha, 0xe00f,
2713 "BE detected REF TAG ERR: lba[0x%llx|%lld] len[0x%x] "
2714 "se_cmd=%p tag[%x]",
2715 cmd->lba, cmd->lba, cmd->num_blks, &cmd->se_cmd,
2716 cmd->atio.u.isp24.exchange_addr);
2719 ql_dbg(ql_dbg_tgt_dif, vha, 0xe010,
2720 "BE detected Dif ERR: lba[%llx|%lld] len[%x] "
2721 "se_cmd=%p tag[%x]",
2722 cmd->lba, cmd->lba, cmd->num_blks, &cmd->se_cmd,
2723 cmd->atio.u.isp24.exchange_addr);
2726 ql_dump_buffer(ql_dbg_tgt_dif, vha, 0xe011, cmd->cdb, 16);
2731 * Called without ha->hardware_lock held
2733 static int qlt_pre_xmit_response(struct qla_tgt_cmd *cmd,
2734 struct qla_tgt_prm *prm, int xmit_type, uint8_t scsi_status,
2735 uint32_t *full_req_cnt)
2737 struct se_cmd *se_cmd = &cmd->se_cmd;
2738 struct qla_qpair *qpair = cmd->qpair;
2741 prm->tgt = cmd->tgt;
2743 prm->rq_result = scsi_status;
2744 prm->sense_buffer = &cmd->sense_buffer[0];
2745 prm->sense_buffer_len = TRANSPORT_SENSE_BUFFER;
2750 prm->add_status_pkt = 0;
2751 prm->prot_sg = NULL;
2752 prm->prot_seg_cnt = 0;
2755 if ((xmit_type & QLA_TGT_XMIT_DATA) && qlt_has_data(cmd)) {
2756 if (qlt_pci_map_calc_cnt(prm) != 0)
2760 *full_req_cnt = prm->req_cnt;
2762 if (se_cmd->se_cmd_flags & SCF_UNDERFLOW_BIT) {
2763 prm->residual = se_cmd->residual_count;
2764 ql_dbg_qp(ql_dbg_io + ql_dbg_verbose, qpair, 0x305c,
2765 "Residual underflow: %d (tag %lld, op %x, bufflen %d, rq_result %x)\n",
2766 prm->residual, se_cmd->tag,
2767 se_cmd->t_task_cdb ? se_cmd->t_task_cdb[0] : 0,
2768 cmd->bufflen, prm->rq_result);
2769 prm->rq_result |= SS_RESIDUAL_UNDER;
2770 } else if (se_cmd->se_cmd_flags & SCF_OVERFLOW_BIT) {
2771 prm->residual = se_cmd->residual_count;
2772 ql_dbg_qp(ql_dbg_io, qpair, 0x305d,
2773 "Residual overflow: %d (tag %lld, op %x, bufflen %d, rq_result %x)\n",
2774 prm->residual, se_cmd->tag, se_cmd->t_task_cdb ?
2775 se_cmd->t_task_cdb[0] : 0, cmd->bufflen, prm->rq_result);
2776 prm->rq_result |= SS_RESIDUAL_OVER;
2779 if (xmit_type & QLA_TGT_XMIT_STATUS) {
2781 * If QLA_TGT_XMIT_DATA is not set, add_status_pkt will be
2782 * ignored in *xmit_response() below
2784 if (qlt_has_data(cmd)) {
2785 if (QLA_TGT_SENSE_VALID(prm->sense_buffer) ||
2786 (IS_FWI2_CAPABLE(cmd->vha->hw) &&
2787 (prm->rq_result != 0))) {
2788 prm->add_status_pkt = 1;
2797 static inline int qlt_need_explicit_conf(struct qla_tgt_cmd *cmd,
2800 if (cmd->qpair->enable_class_2)
2804 return cmd->conf_compl_supported;
2806 return cmd->qpair->enable_explicit_conf &&
2807 cmd->conf_compl_supported;
2810 static void qlt_24xx_init_ctio_to_isp(struct ctio7_to_24xx *ctio,
2811 struct qla_tgt_prm *prm)
2813 prm->sense_buffer_len = min_t(uint32_t, prm->sense_buffer_len,
2814 (uint32_t)sizeof(ctio->u.status1.sense_data));
2815 ctio->u.status0.flags |= cpu_to_le16(CTIO7_FLAGS_SEND_STATUS);
2816 if (qlt_need_explicit_conf(prm->cmd, 0)) {
2817 ctio->u.status0.flags |= cpu_to_le16(
2818 CTIO7_FLAGS_EXPLICIT_CONFORM |
2819 CTIO7_FLAGS_CONFORM_REQ);
2821 ctio->u.status0.residual = cpu_to_le32(prm->residual);
2822 ctio->u.status0.scsi_status = cpu_to_le16(prm->rq_result);
2823 if (QLA_TGT_SENSE_VALID(prm->sense_buffer)) {
2826 if (qlt_need_explicit_conf(prm->cmd, 1)) {
2827 if ((prm->rq_result & SS_SCSI_STATUS_BYTE) != 0) {
2828 ql_dbg_qp(ql_dbg_tgt, prm->cmd->qpair, 0xe017,
2829 "Skipping EXPLICIT_CONFORM and "
2830 "CTIO7_FLAGS_CONFORM_REQ for FCP READ w/ "
2831 "non GOOD status\n");
2832 goto skip_explict_conf;
2834 ctio->u.status1.flags |= cpu_to_le16(
2835 CTIO7_FLAGS_EXPLICIT_CONFORM |
2836 CTIO7_FLAGS_CONFORM_REQ);
2839 ctio->u.status1.flags &=
2840 ~cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_0);
2841 ctio->u.status1.flags |=
2842 cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_1);
2843 ctio->u.status1.scsi_status |=
2844 cpu_to_le16(SS_SENSE_LEN_VALID);
2845 ctio->u.status1.sense_length =
2846 cpu_to_le16(prm->sense_buffer_len);
2847 for (i = 0; i < prm->sense_buffer_len/4; i++) {
2850 v = get_unaligned_be32(
2851 &((uint32_t *)prm->sense_buffer)[i]);
2852 put_unaligned_le32(v,
2853 &((uint32_t *)ctio->u.status1.sense_data)[i]);
2855 qlt_print_dif_err(prm);
2858 ctio->u.status1.flags &=
2859 ~cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_0);
2860 ctio->u.status1.flags |=
2861 cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_1);
2862 ctio->u.status1.sense_length = 0;
2863 memset(ctio->u.status1.sense_data, 0,
2864 sizeof(ctio->u.status1.sense_data));
2867 /* Sense with len > 24, is it possible ??? */
2871 qlt_hba_err_chk_enabled(struct se_cmd *se_cmd)
2873 switch (se_cmd->prot_op) {
2874 case TARGET_PROT_DOUT_INSERT:
2875 case TARGET_PROT_DIN_STRIP:
2876 if (ql2xenablehba_err_chk >= 1)
2879 case TARGET_PROT_DOUT_PASS:
2880 case TARGET_PROT_DIN_PASS:
2881 if (ql2xenablehba_err_chk >= 2)
2884 case TARGET_PROT_DIN_INSERT:
2885 case TARGET_PROT_DOUT_STRIP:
2894 qla_tgt_ref_mask_check(struct se_cmd *se_cmd)
2896 switch (se_cmd->prot_op) {
2897 case TARGET_PROT_DIN_INSERT:
2898 case TARGET_PROT_DOUT_INSERT:
2899 case TARGET_PROT_DIN_STRIP:
2900 case TARGET_PROT_DOUT_STRIP:
2901 case TARGET_PROT_DIN_PASS:
2902 case TARGET_PROT_DOUT_PASS:
2911 * qla_tgt_set_dif_tags - Extract Ref and App tags from SCSI command
2914 qla_tgt_set_dif_tags(struct qla_tgt_cmd *cmd, struct crc_context *ctx,
2915 uint16_t *pfw_prot_opts)
2917 struct se_cmd *se_cmd = &cmd->se_cmd;
2918 uint32_t lba = 0xffffffff & se_cmd->t_task_lba;
2919 scsi_qla_host_t *vha = cmd->tgt->vha;
2920 struct qla_hw_data *ha = vha->hw;
2924 * wait till Mode Sense/Select cmd, modepage Ah, subpage 2
2925 * have been immplemented by TCM, before AppTag is avail.
2926 * Look for modesense_handlers[]
2929 ctx->app_tag_mask[0] = 0x0;
2930 ctx->app_tag_mask[1] = 0x0;
2932 if (IS_PI_UNINIT_CAPABLE(ha)) {
2933 if ((se_cmd->prot_type == TARGET_DIF_TYPE1_PROT) ||
2934 (se_cmd->prot_type == TARGET_DIF_TYPE2_PROT))
2935 *pfw_prot_opts |= PO_DIS_VALD_APP_ESC;
2936 else if (se_cmd->prot_type == TARGET_DIF_TYPE3_PROT)
2937 *pfw_prot_opts |= PO_DIS_VALD_APP_REF_ESC;
2940 t32 = ha->tgt.tgt_ops->get_dif_tags(cmd, pfw_prot_opts);
2942 switch (se_cmd->prot_type) {
2943 case TARGET_DIF_TYPE0_PROT:
2945 * No check for ql2xenablehba_err_chk, as it
2946 * would be an I/O error if hba tag generation
2949 ctx->ref_tag = cpu_to_le32(lba);
2950 /* enable ALL bytes of the ref tag */
2951 ctx->ref_tag_mask[0] = 0xff;
2952 ctx->ref_tag_mask[1] = 0xff;
2953 ctx->ref_tag_mask[2] = 0xff;
2954 ctx->ref_tag_mask[3] = 0xff;
2956 case TARGET_DIF_TYPE1_PROT:
2958 * For TYPE 1 protection: 16 bit GUARD tag, 32 bit
2959 * REF tag, and 16 bit app tag.
2961 ctx->ref_tag = cpu_to_le32(lba);
2962 if (!qla_tgt_ref_mask_check(se_cmd) ||
2963 !(ha->tgt.tgt_ops->chk_dif_tags(t32))) {
2964 *pfw_prot_opts |= PO_DIS_REF_TAG_VALD;
2967 /* enable ALL bytes of the ref tag */
2968 ctx->ref_tag_mask[0] = 0xff;
2969 ctx->ref_tag_mask[1] = 0xff;
2970 ctx->ref_tag_mask[2] = 0xff;
2971 ctx->ref_tag_mask[3] = 0xff;
2973 case TARGET_DIF_TYPE2_PROT:
2975 * For TYPE 2 protection: 16 bit GUARD + 32 bit REF
2976 * tag has to match LBA in CDB + N
2978 ctx->ref_tag = cpu_to_le32(lba);
2979 if (!qla_tgt_ref_mask_check(se_cmd) ||
2980 !(ha->tgt.tgt_ops->chk_dif_tags(t32))) {
2981 *pfw_prot_opts |= PO_DIS_REF_TAG_VALD;
2984 /* enable ALL bytes of the ref tag */
2985 ctx->ref_tag_mask[0] = 0xff;
2986 ctx->ref_tag_mask[1] = 0xff;
2987 ctx->ref_tag_mask[2] = 0xff;
2988 ctx->ref_tag_mask[3] = 0xff;
2990 case TARGET_DIF_TYPE3_PROT:
2991 /* For TYPE 3 protection: 16 bit GUARD only */
2992 *pfw_prot_opts |= PO_DIS_REF_TAG_VALD;
2993 ctx->ref_tag_mask[0] = ctx->ref_tag_mask[1] =
2994 ctx->ref_tag_mask[2] = ctx->ref_tag_mask[3] = 0x00;
3000 qlt_build_ctio_crc2_pkt(struct qla_qpair *qpair, struct qla_tgt_prm *prm)
3002 struct dsd64 *cur_dsd;
3003 uint32_t transfer_length = 0;
3004 uint32_t data_bytes;
3006 uint8_t bundling = 1;
3007 struct crc_context *crc_ctx_pkt = NULL;
3008 struct qla_hw_data *ha;
3009 struct ctio_crc2_to_fw *pkt;
3010 dma_addr_t crc_ctx_dma;
3011 uint16_t fw_prot_opts = 0;
3012 struct qla_tgt_cmd *cmd = prm->cmd;
3013 struct se_cmd *se_cmd = &cmd->se_cmd;
3015 struct atio_from_isp *atio = &prm->cmd->atio;
3016 struct qla_tc_param tc;
3018 scsi_qla_host_t *vha = cmd->vha;
3022 pkt = (struct ctio_crc2_to_fw *)qpair->req->ring_ptr;
3024 memset(pkt, 0, sizeof(*pkt));
3026 ql_dbg_qp(ql_dbg_tgt, cmd->qpair, 0xe071,
3027 "qla_target(%d):%s: se_cmd[%p] CRC2 prot_op[0x%x] cmd prot sg:cnt[%p:%x] lba[%llu]\n",
3028 cmd->vp_idx, __func__, se_cmd, se_cmd->prot_op,
3029 prm->prot_sg, prm->prot_seg_cnt, se_cmd->t_task_lba);
3031 if ((se_cmd->prot_op == TARGET_PROT_DIN_INSERT) ||
3032 (se_cmd->prot_op == TARGET_PROT_DOUT_STRIP))
3035 /* Compute dif len and adjust data len to incude protection */
3036 data_bytes = cmd->bufflen;
3037 dif_bytes = (data_bytes / cmd->blk_sz) * 8;
3039 switch (se_cmd->prot_op) {
3040 case TARGET_PROT_DIN_INSERT:
3041 case TARGET_PROT_DOUT_STRIP:
3042 transfer_length = data_bytes;
3043 if (cmd->prot_sg_cnt)
3044 data_bytes += dif_bytes;
3046 case TARGET_PROT_DIN_STRIP:
3047 case TARGET_PROT_DOUT_INSERT:
3048 case TARGET_PROT_DIN_PASS:
3049 case TARGET_PROT_DOUT_PASS:
3050 transfer_length = data_bytes + dif_bytes;
3057 if (!qlt_hba_err_chk_enabled(se_cmd))
3058 fw_prot_opts |= 0x10; /* Disable Guard tag checking */
3059 /* HBA error checking enabled */
3060 else if (IS_PI_UNINIT_CAPABLE(ha)) {
3061 if ((se_cmd->prot_type == TARGET_DIF_TYPE1_PROT) ||
3062 (se_cmd->prot_type == TARGET_DIF_TYPE2_PROT))
3063 fw_prot_opts |= PO_DIS_VALD_APP_ESC;
3064 else if (se_cmd->prot_type == TARGET_DIF_TYPE3_PROT)
3065 fw_prot_opts |= PO_DIS_VALD_APP_REF_ESC;
3068 switch (se_cmd->prot_op) {
3069 case TARGET_PROT_DIN_INSERT:
3070 case TARGET_PROT_DOUT_INSERT:
3071 fw_prot_opts |= PO_MODE_DIF_INSERT;
3073 case TARGET_PROT_DIN_STRIP:
3074 case TARGET_PROT_DOUT_STRIP:
3075 fw_prot_opts |= PO_MODE_DIF_REMOVE;
3077 case TARGET_PROT_DIN_PASS:
3078 case TARGET_PROT_DOUT_PASS:
3079 fw_prot_opts |= PO_MODE_DIF_PASS;
3080 /* FUTURE: does tcm require T10CRC<->IPCKSUM conversion? */
3082 default:/* Normal Request */
3083 fw_prot_opts |= PO_MODE_DIF_PASS;
3088 /* Update entry type to indicate Command Type CRC_2 IOCB */
3089 pkt->entry_type = CTIO_CRC2;
3090 pkt->entry_count = 1;
3091 pkt->vp_index = cmd->vp_idx;
3093 h = qlt_make_handle(qpair);
3094 if (unlikely(h == QLA_TGT_NULL_HANDLE)) {
3096 * CTIO type 7 from the firmware doesn't provide a way to
3097 * know the initiator's LOOP ID, hence we can't find
3098 * the session and, so, the command.
3102 qpair->req->outstanding_cmds[h] = (srb_t *)prm->cmd;
3104 pkt->handle = make_handle(qpair->req->id, h);
3105 pkt->handle |= CTIO_COMPLETION_HANDLE_MARK;
3106 pkt->nport_handle = cpu_to_le16(prm->cmd->loop_id);
3107 pkt->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
3108 pkt->initiator_id = be_id_to_le(atio->u.isp24.fcp_hdr.s_id);
3109 pkt->exchange_addr = atio->u.isp24.exchange_addr;
3111 /* silence compile warning */
3112 t16 = be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id);
3113 pkt->ox_id = cpu_to_le16(t16);
3115 t16 = (atio->u.isp24.attr << 9);
3116 pkt->flags |= cpu_to_le16(t16);
3117 pkt->relative_offset = cpu_to_le32(prm->cmd->offset);
3119 /* Set transfer direction */
3120 if (cmd->dma_data_direction == DMA_TO_DEVICE)
3121 pkt->flags = cpu_to_le16(CTIO7_FLAGS_DATA_IN);
3122 else if (cmd->dma_data_direction == DMA_FROM_DEVICE)
3123 pkt->flags = cpu_to_le16(CTIO7_FLAGS_DATA_OUT);
3125 pkt->dseg_count = cpu_to_le16(prm->tot_dsds);
3126 /* Fibre channel byte count */
3127 pkt->transfer_length = cpu_to_le32(transfer_length);
3129 /* ----- CRC context -------- */
3131 /* Allocate CRC context from global pool */
3132 crc_ctx_pkt = cmd->ctx =
3133 dma_pool_zalloc(ha->dl_dma_pool, GFP_ATOMIC, &crc_ctx_dma);
3136 goto crc_queuing_error;
3138 crc_ctx_pkt->crc_ctx_dma = crc_ctx_dma;
3139 INIT_LIST_HEAD(&crc_ctx_pkt->dsd_list);
3142 crc_ctx_pkt->handle = pkt->handle;
3144 qla_tgt_set_dif_tags(cmd, crc_ctx_pkt, &fw_prot_opts);
3146 put_unaligned_le64(crc_ctx_dma, &pkt->crc_context_address);
3147 pkt->crc_context_len = cpu_to_le16(CRC_CONTEXT_LEN_FW);
3150 cur_dsd = &crc_ctx_pkt->u.nobundling.data_dsd[0];
3153 * Configure Bundling if we need to fetch interlaving
3154 * protection PCI accesses
3156 fw_prot_opts |= PO_ENABLE_DIF_BUNDLING;
3157 crc_ctx_pkt->u.bundling.dif_byte_count = cpu_to_le32(dif_bytes);
3158 crc_ctx_pkt->u.bundling.dseg_count =
3159 cpu_to_le16(prm->tot_dsds - prm->prot_seg_cnt);
3160 cur_dsd = &crc_ctx_pkt->u.bundling.data_dsd[0];
3163 /* Finish the common fields of CRC pkt */
3164 crc_ctx_pkt->blk_size = cpu_to_le16(cmd->blk_sz);
3165 crc_ctx_pkt->prot_opts = cpu_to_le16(fw_prot_opts);
3166 crc_ctx_pkt->byte_count = cpu_to_le32(data_bytes);
3167 crc_ctx_pkt->guard_seed = cpu_to_le16(0);
3169 memset((uint8_t *)&tc, 0 , sizeof(tc));
3171 tc.blk_sz = cmd->blk_sz;
3172 tc.bufflen = cmd->bufflen;
3174 tc.prot_sg = cmd->prot_sg;
3175 tc.ctx = crc_ctx_pkt;
3176 tc.ctx_dsd_alloced = &cmd->ctx_dsd_alloced;
3178 /* Walks data segments */
3179 pkt->flags |= cpu_to_le16(CTIO7_FLAGS_DSD_PTR);
3181 if (!bundling && prm->prot_seg_cnt) {
3182 if (qla24xx_walk_and_build_sglist_no_difb(ha, NULL, cur_dsd,
3183 prm->tot_dsds, &tc))
3184 goto crc_queuing_error;
3185 } else if (qla24xx_walk_and_build_sglist(ha, NULL, cur_dsd,
3186 (prm->tot_dsds - prm->prot_seg_cnt), &tc))
3187 goto crc_queuing_error;
3189 if (bundling && prm->prot_seg_cnt) {
3190 /* Walks dif segments */
3191 pkt->add_flags |= CTIO_CRC2_AF_DIF_DSD_ENA;
3193 cur_dsd = &crc_ctx_pkt->u.bundling.dif_dsd;
3194 if (qla24xx_walk_and_build_prot_sglist(ha, NULL, cur_dsd,
3195 prm->prot_seg_cnt, cmd))
3196 goto crc_queuing_error;
3201 /* Cleanup will be performed by the caller */
3202 qpair->req->outstanding_cmds[h] = NULL;
3204 return QLA_FUNCTION_FAILED;
3208 * Callback to setup response of xmit_type of QLA_TGT_XMIT_DATA and *
3209 * QLA_TGT_XMIT_STATUS for >= 24xx silicon
3211 int qlt_xmit_response(struct qla_tgt_cmd *cmd, int xmit_type,
3212 uint8_t scsi_status)
3214 struct scsi_qla_host *vha = cmd->vha;
3215 struct qla_qpair *qpair = cmd->qpair;
3216 struct ctio7_to_24xx *pkt;
3217 struct qla_tgt_prm prm;
3218 uint32_t full_req_cnt = 0;
3219 unsigned long flags = 0;
3222 if (!qpair->fw_started || (cmd->reset_count != qpair->chip_reset) ||
3223 (cmd->sess && cmd->sess->deleted)) {
3224 cmd->state = QLA_TGT_STATE_PROCESSED;
3228 ql_dbg_qp(ql_dbg_tgt, qpair, 0xe018,
3229 "is_send_status=%d, cmd->bufflen=%d, cmd->sg_cnt=%d, cmd->dma_data_direction=%d se_cmd[%p] qp %d\n",
3230 (xmit_type & QLA_TGT_XMIT_STATUS) ?
3231 1 : 0, cmd->bufflen, cmd->sg_cnt, cmd->dma_data_direction,
3232 &cmd->se_cmd, qpair->id);
3234 res = qlt_pre_xmit_response(cmd, &prm, xmit_type, scsi_status,
3236 if (unlikely(res != 0)) {
3240 spin_lock_irqsave(qpair->qp_lock_ptr, flags);
3242 if (xmit_type == QLA_TGT_XMIT_STATUS)
3243 qpair->tgt_counters.core_qla_snd_status++;
3245 qpair->tgt_counters.core_qla_que_buf++;
3247 if (!qpair->fw_started || cmd->reset_count != qpair->chip_reset) {
3249 * Either the port is not online or this request was from
3250 * previous life, just abort the processing.
3252 cmd->state = QLA_TGT_STATE_PROCESSED;
3253 ql_dbg_qp(ql_dbg_async, qpair, 0xe101,
3254 "RESET-RSP online/active/old-count/new-count = %d/%d/%d/%d.\n",
3255 vha->flags.online, qla2x00_reset_active(vha),
3256 cmd->reset_count, qpair->chip_reset);
3257 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
3261 /* Does F/W have an IOCBs for this request */
3262 res = qlt_check_reserve_free_req(qpair, full_req_cnt);
3264 goto out_unmap_unlock;
3266 if (cmd->se_cmd.prot_op && (xmit_type & QLA_TGT_XMIT_DATA))
3267 res = qlt_build_ctio_crc2_pkt(qpair, &prm);
3269 res = qlt_24xx_build_ctio_pkt(qpair, &prm);
3270 if (unlikely(res != 0)) {
3271 qpair->req->cnt += full_req_cnt;
3272 goto out_unmap_unlock;
3275 pkt = (struct ctio7_to_24xx *)prm.pkt;
3277 if (qlt_has_data(cmd) && (xmit_type & QLA_TGT_XMIT_DATA)) {
3278 pkt->u.status0.flags |=
3279 cpu_to_le16(CTIO7_FLAGS_DATA_IN |
3280 CTIO7_FLAGS_STATUS_MODE_0);
3282 if (cmd->se_cmd.prot_op == TARGET_PROT_NORMAL)
3283 qlt_load_data_segments(&prm);
3285 if (prm.add_status_pkt == 0) {
3286 if (xmit_type & QLA_TGT_XMIT_STATUS) {
3287 pkt->u.status0.scsi_status =
3288 cpu_to_le16(prm.rq_result);
3289 pkt->u.status0.residual =
3290 cpu_to_le32(prm.residual);
3291 pkt->u.status0.flags |= cpu_to_le16(
3292 CTIO7_FLAGS_SEND_STATUS);
3293 if (qlt_need_explicit_conf(cmd, 0)) {
3294 pkt->u.status0.flags |=
3296 CTIO7_FLAGS_EXPLICIT_CONFORM |
3297 CTIO7_FLAGS_CONFORM_REQ);
3303 * We have already made sure that there is sufficient
3304 * amount of request entries to not drop HW lock in
3307 struct ctio7_to_24xx *ctio =
3308 (struct ctio7_to_24xx *)qlt_get_req_pkt(
3311 ql_dbg_qp(ql_dbg_tgt, qpair, 0x305e,
3312 "Building additional status packet 0x%p.\n",
3316 * T10Dif: ctio_crc2_to_fw overlay ontop of
3319 memcpy(ctio, pkt, sizeof(*ctio));
3320 /* reset back to CTIO7 */
3321 ctio->entry_count = 1;
3322 ctio->entry_type = CTIO_TYPE7;
3323 ctio->dseg_count = 0;
3324 ctio->u.status1.flags &= ~cpu_to_le16(
3325 CTIO7_FLAGS_DATA_IN);
3327 /* Real finish is ctio_m1's finish */
3328 pkt->handle |= CTIO_INTERMEDIATE_HANDLE_MARK;
3329 pkt->u.status0.flags |= cpu_to_le16(
3330 CTIO7_FLAGS_DONT_RET_CTIO);
3332 /* qlt_24xx_init_ctio_to_isp will correct
3333 * all neccessary fields that's part of CTIO7.
3334 * There should be no residual of CTIO-CRC2 data.
3336 qlt_24xx_init_ctio_to_isp((struct ctio7_to_24xx *)ctio,
3340 qlt_24xx_init_ctio_to_isp(pkt, &prm);
3343 cmd->state = QLA_TGT_STATE_PROCESSED; /* Mid-level is done processing */
3344 cmd->cmd_sent_to_fw = 1;
3345 cmd->ctio_flags = le16_to_cpu(pkt->u.status0.flags);
3347 /* Memory Barrier */
3349 if (qpair->reqq_start_iocbs)
3350 qpair->reqq_start_iocbs(qpair);
3352 qla2x00_start_iocbs(vha, qpair->req);
3353 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
3358 qlt_unmap_sg(vha, cmd);
3359 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
3363 EXPORT_SYMBOL(qlt_xmit_response);
3365 int qlt_rdy_to_xfer(struct qla_tgt_cmd *cmd)
3367 struct ctio7_to_24xx *pkt;
3368 struct scsi_qla_host *vha = cmd->vha;
3369 struct qla_tgt *tgt = cmd->tgt;
3370 struct qla_tgt_prm prm;
3371 unsigned long flags = 0;
3373 struct qla_qpair *qpair = cmd->qpair;
3375 memset(&prm, 0, sizeof(prm));
3381 /* Calculate number of entries and segments required */
3382 if (qlt_pci_map_calc_cnt(&prm) != 0)
3385 if (!qpair->fw_started || (cmd->reset_count != qpair->chip_reset) ||
3386 (cmd->sess && cmd->sess->deleted)) {
3388 * Either the port is not online or this request was from
3389 * previous life, just abort the processing.
3392 cmd->write_data_transferred = 0;
3393 cmd->state = QLA_TGT_STATE_DATA_IN;
3394 vha->hw->tgt.tgt_ops->handle_data(cmd);
3395 ql_dbg_qp(ql_dbg_async, qpair, 0xe102,
3396 "RESET-XFR online/active/old-count/new-count = %d/%d/%d/%d.\n",
3397 vha->flags.online, qla2x00_reset_active(vha),
3398 cmd->reset_count, qpair->chip_reset);
3402 spin_lock_irqsave(qpair->qp_lock_ptr, flags);
3403 /* Does F/W have an IOCBs for this request */
3404 res = qlt_check_reserve_free_req(qpair, prm.req_cnt);
3406 goto out_unlock_free_unmap;
3407 if (cmd->se_cmd.prot_op)
3408 res = qlt_build_ctio_crc2_pkt(qpair, &prm);
3410 res = qlt_24xx_build_ctio_pkt(qpair, &prm);
3412 if (unlikely(res != 0)) {
3413 qpair->req->cnt += prm.req_cnt;
3414 goto out_unlock_free_unmap;
3417 pkt = (struct ctio7_to_24xx *)prm.pkt;
3418 pkt->u.status0.flags |= cpu_to_le16(CTIO7_FLAGS_DATA_OUT |
3419 CTIO7_FLAGS_STATUS_MODE_0);
3421 if (cmd->se_cmd.prot_op == TARGET_PROT_NORMAL)
3422 qlt_load_data_segments(&prm);
3424 cmd->state = QLA_TGT_STATE_NEED_DATA;
3425 cmd->cmd_sent_to_fw = 1;
3426 cmd->ctio_flags = le16_to_cpu(pkt->u.status0.flags);
3428 /* Memory Barrier */
3430 if (qpair->reqq_start_iocbs)
3431 qpair->reqq_start_iocbs(qpair);
3433 qla2x00_start_iocbs(vha, qpair->req);
3434 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
3438 out_unlock_free_unmap:
3439 qlt_unmap_sg(vha, cmd);
3440 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
3444 EXPORT_SYMBOL(qlt_rdy_to_xfer);
3448 * it is assumed either hardware_lock or qpair lock is held.
3451 qlt_handle_dif_error(struct qla_qpair *qpair, struct qla_tgt_cmd *cmd,
3452 struct ctio_crc_from_fw *sts)
3454 uint8_t *ap = &sts->actual_dif[0];
3455 uint8_t *ep = &sts->expected_dif[0];
3456 uint64_t lba = cmd->se_cmd.t_task_lba;
3457 uint8_t scsi_status, sense_key, asc, ascq;
3458 unsigned long flags;
3459 struct scsi_qla_host *vha = cmd->vha;
3461 cmd->trc_flags |= TRC_DIF_ERR;
3463 cmd->a_guard = get_unaligned_be16(ap + 0);
3464 cmd->a_app_tag = get_unaligned_be16(ap + 2);
3465 cmd->a_ref_tag = get_unaligned_be32(ap + 4);
3467 cmd->e_guard = get_unaligned_be16(ep + 0);
3468 cmd->e_app_tag = get_unaligned_be16(ep + 2);
3469 cmd->e_ref_tag = get_unaligned_be32(ep + 4);
3471 ql_dbg(ql_dbg_tgt_dif, vha, 0xf075,
3472 "%s: aborted %d state %d\n", __func__, cmd->aborted, cmd->state);
3474 scsi_status = sense_key = asc = ascq = 0;
3476 /* check appl tag */
3477 if (cmd->e_app_tag != cmd->a_app_tag) {
3478 ql_dbg(ql_dbg_tgt_dif, vha, 0xe00d,
3479 "App Tag ERR: cdb[%x] lba[%llx %llx] blks[%x] [Actual|Expected] Ref[%x|%x], App[%x|%x], Guard [%x|%x] cmd=%p ox_id[%04x]",
3480 cmd->cdb[0], lba, (lba+cmd->num_blks), cmd->num_blks,
3481 cmd->a_ref_tag, cmd->e_ref_tag, cmd->a_app_tag,
3482 cmd->e_app_tag, cmd->a_guard, cmd->e_guard, cmd,
3483 cmd->atio.u.isp24.fcp_hdr.ox_id);
3485 cmd->dif_err_code = DIF_ERR_APP;
3486 scsi_status = SAM_STAT_CHECK_CONDITION;
3487 sense_key = ABORTED_COMMAND;
3493 if (cmd->e_ref_tag != cmd->a_ref_tag) {
3494 ql_dbg(ql_dbg_tgt_dif, vha, 0xe00e,
3495 "Ref Tag ERR: cdb[%x] lba[%llx %llx] blks[%x] [Actual|Expected] Ref[%x|%x], App[%x|%x], Guard[%x|%x] cmd=%p ox_id[%04x] ",
3496 cmd->cdb[0], lba, (lba+cmd->num_blks), cmd->num_blks,
3497 cmd->a_ref_tag, cmd->e_ref_tag, cmd->a_app_tag,
3498 cmd->e_app_tag, cmd->a_guard, cmd->e_guard, cmd,
3499 cmd->atio.u.isp24.fcp_hdr.ox_id);
3501 cmd->dif_err_code = DIF_ERR_REF;
3502 scsi_status = SAM_STAT_CHECK_CONDITION;
3503 sense_key = ABORTED_COMMAND;
3510 if (cmd->e_guard != cmd->a_guard) {
3511 ql_dbg(ql_dbg_tgt_dif, vha, 0xe012,
3512 "Guard ERR: cdb[%x] lba[%llx %llx] blks[%x] [Actual|Expected] Ref[%x|%x], App[%x|%x], Guard [%x|%x] cmd=%p ox_id[%04x]",
3513 cmd->cdb[0], lba, (lba+cmd->num_blks), cmd->num_blks,
3514 cmd->a_ref_tag, cmd->e_ref_tag, cmd->a_app_tag,
3515 cmd->e_app_tag, cmd->a_guard, cmd->e_guard, cmd,
3516 cmd->atio.u.isp24.fcp_hdr.ox_id);
3518 cmd->dif_err_code = DIF_ERR_GRD;
3519 scsi_status = SAM_STAT_CHECK_CONDITION;
3520 sense_key = ABORTED_COMMAND;
3525 switch (cmd->state) {
3526 case QLA_TGT_STATE_NEED_DATA:
3527 /* handle_data will load DIF error code */
3528 cmd->state = QLA_TGT_STATE_DATA_IN;
3529 vha->hw->tgt.tgt_ops->handle_data(cmd);
3532 spin_lock_irqsave(&cmd->cmd_lock, flags);
3534 spin_unlock_irqrestore(&cmd->cmd_lock, flags);
3535 vha->hw->tgt.tgt_ops->free_cmd(cmd);
3538 spin_unlock_irqrestore(&cmd->cmd_lock, flags);
3540 qlt_send_resp_ctio(qpair, cmd, scsi_status, sense_key, asc,
3542 /* assume scsi status gets out on the wire.
3543 * Will not wait for completion.
3545 vha->hw->tgt.tgt_ops->free_cmd(cmd);
3550 /* If hardware_lock held on entry, might drop it, then reaquire */
3551 /* This function sends the appropriate CTIO to ISP 2xxx or 24xx */
3552 static int __qlt_send_term_imm_notif(struct scsi_qla_host *vha,
3553 struct imm_ntfy_from_isp *ntfy)
3555 struct nack_to_isp *nack;
3556 struct qla_hw_data *ha = vha->hw;
3560 ql_dbg(ql_dbg_tgt_tmr, vha, 0xe01c,
3561 "Sending TERM ELS CTIO (ha=%p)\n", ha);
3563 pkt = (request_t *)qla2x00_alloc_iocbs(vha, NULL);
3565 ql_dbg(ql_dbg_tgt, vha, 0xe080,
3566 "qla_target(%d): %s failed: unable to allocate "
3567 "request packet\n", vha->vp_idx, __func__);
3571 pkt->entry_type = NOTIFY_ACK_TYPE;
3572 pkt->entry_count = 1;
3573 pkt->handle = QLA_TGT_SKIP_HANDLE;
3575 nack = (struct nack_to_isp *)pkt;
3576 nack->ox_id = ntfy->ox_id;
3578 nack->u.isp24.nport_handle = ntfy->u.isp24.nport_handle;
3579 if (le16_to_cpu(ntfy->u.isp24.status) == IMM_NTFY_ELS) {
3580 nack->u.isp24.flags = ntfy->u.isp24.flags &
3581 cpu_to_le16(NOTIFY24XX_FLAGS_PUREX_IOCB);
3585 nack->u.isp24.flags |=
3586 __constant_cpu_to_le16(NOTIFY_ACK_FLAGS_TERMINATE);
3588 nack->u.isp24.srr_rx_id = ntfy->u.isp24.srr_rx_id;
3589 nack->u.isp24.status = ntfy->u.isp24.status;
3590 nack->u.isp24.status_subcode = ntfy->u.isp24.status_subcode;
3591 nack->u.isp24.fw_handle = ntfy->u.isp24.fw_handle;
3592 nack->u.isp24.exchange_address = ntfy->u.isp24.exchange_address;
3593 nack->u.isp24.srr_rel_offs = ntfy->u.isp24.srr_rel_offs;
3594 nack->u.isp24.srr_ui = ntfy->u.isp24.srr_ui;
3595 nack->u.isp24.vp_index = ntfy->u.isp24.vp_index;
3597 qla2x00_start_iocbs(vha, vha->req);
3601 static void qlt_send_term_imm_notif(struct scsi_qla_host *vha,
3602 struct imm_ntfy_from_isp *imm, int ha_locked)
3606 WARN_ON_ONCE(!ha_locked);
3607 rc = __qlt_send_term_imm_notif(vha, imm);
3608 pr_debug("rc = %d\n", rc);
3612 * If hardware_lock held on entry, might drop it, then reaquire
3613 * This function sends the appropriate CTIO to ISP 2xxx or 24xx
3615 static int __qlt_send_term_exchange(struct qla_qpair *qpair,
3616 struct qla_tgt_cmd *cmd,
3617 struct atio_from_isp *atio)
3619 struct scsi_qla_host *vha = qpair->vha;
3620 struct ctio7_to_24xx *ctio24;
3621 struct qla_hw_data *ha = vha->hw;
3626 ql_dbg(ql_dbg_tgt, vha, 0xe009, "Sending TERM EXCH CTIO (ha=%p)\n", ha);
3631 pkt = (request_t *)qla2x00_alloc_iocbs_ready(qpair, NULL);
3633 ql_dbg(ql_dbg_tgt, vha, 0xe050,
3634 "qla_target(%d): %s failed: unable to allocate "
3635 "request packet\n", vha->vp_idx, __func__);
3640 if (cmd->state < QLA_TGT_STATE_PROCESSED) {
3641 ql_dbg(ql_dbg_tgt, vha, 0xe051,
3642 "qla_target(%d): Terminating cmd %p with "
3643 "incorrect state %d\n", vha->vp_idx, cmd,
3649 qpair->tgt_counters.num_term_xchg_sent++;
3650 pkt->entry_count = 1;
3651 pkt->handle = QLA_TGT_SKIP_HANDLE | CTIO_COMPLETION_HANDLE_MARK;
3653 ctio24 = (struct ctio7_to_24xx *)pkt;
3654 ctio24->entry_type = CTIO_TYPE7;
3655 ctio24->nport_handle = cpu_to_le16(CTIO7_NHANDLE_UNRECOGNIZED);
3656 ctio24->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
3657 ctio24->vp_index = vha->vp_idx;
3658 ctio24->initiator_id = be_id_to_le(atio->u.isp24.fcp_hdr.s_id);
3659 ctio24->exchange_addr = atio->u.isp24.exchange_addr;
3660 temp = (atio->u.isp24.attr << 9) | CTIO7_FLAGS_STATUS_MODE_1 |
3661 CTIO7_FLAGS_TERMINATE;
3662 ctio24->u.status1.flags = cpu_to_le16(temp);
3663 temp = be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id);
3664 ctio24->u.status1.ox_id = cpu_to_le16(temp);
3666 /* Memory Barrier */
3668 if (qpair->reqq_start_iocbs)
3669 qpair->reqq_start_iocbs(qpair);
3671 qla2x00_start_iocbs(vha, qpair->req);
3675 static void qlt_send_term_exchange(struct qla_qpair *qpair,
3676 struct qla_tgt_cmd *cmd, struct atio_from_isp *atio, int ha_locked,
3679 struct scsi_qla_host *vha;
3680 unsigned long flags = 0;
3683 /* why use different vha? NPIV */
3690 rc = __qlt_send_term_exchange(qpair, cmd, atio);
3692 qlt_alloc_qfull_cmd(vha, atio, 0, 0);
3695 spin_lock_irqsave(qpair->qp_lock_ptr, flags);
3696 rc = __qlt_send_term_exchange(qpair, cmd, atio);
3698 qlt_alloc_qfull_cmd(vha, atio, 0, 0);
3701 if (cmd && !ul_abort && !cmd->aborted) {
3703 qlt_unmap_sg(vha, cmd);
3704 vha->hw->tgt.tgt_ops->free_cmd(cmd);
3708 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
3713 static void qlt_init_term_exchange(struct scsi_qla_host *vha)
3715 struct list_head free_list;
3716 struct qla_tgt_cmd *cmd, *tcmd;
3718 vha->hw->tgt.leak_exchg_thresh_hold =
3719 (vha->hw->cur_fw_xcb_count/100) * LEAK_EXCHG_THRESH_HOLD_PERCENT;
3722 if (!list_empty(&vha->hw->tgt.q_full_list)) {
3723 INIT_LIST_HEAD(&free_list);
3724 list_splice_init(&vha->hw->tgt.q_full_list, &free_list);
3726 list_for_each_entry_safe(cmd, tcmd, &free_list, cmd_list) {
3727 list_del(&cmd->cmd_list);
3728 /* This cmd was never sent to TCM. There is no need
3729 * to schedule free or call free_cmd
3732 vha->hw->tgt.num_qfull_cmds_alloc--;
3735 vha->hw->tgt.num_qfull_cmds_dropped = 0;
3738 static void qlt_chk_exch_leak_thresh_hold(struct scsi_qla_host *vha)
3740 uint32_t total_leaked;
3742 total_leaked = vha->hw->tgt.num_qfull_cmds_dropped;
3744 if (vha->hw->tgt.leak_exchg_thresh_hold &&
3745 (total_leaked > vha->hw->tgt.leak_exchg_thresh_hold)) {
3747 ql_dbg(ql_dbg_tgt, vha, 0xe079,
3748 "Chip reset due to exchange starvation: %d/%d.\n",
3749 total_leaked, vha->hw->cur_fw_xcb_count);
3751 if (IS_P3P_TYPE(vha->hw))
3752 set_bit(FCOE_CTX_RESET_NEEDED, &vha->dpc_flags);
3754 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
3755 qla2xxx_wake_dpc(vha);
3760 int qlt_abort_cmd(struct qla_tgt_cmd *cmd)
3762 struct qla_tgt *tgt = cmd->tgt;
3763 struct scsi_qla_host *vha = tgt->vha;
3764 struct se_cmd *se_cmd = &cmd->se_cmd;
3765 unsigned long flags;
3767 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf014,
3768 "qla_target(%d): terminating exchange for aborted cmd=%p "
3769 "(se_cmd=%p, tag=%llu)", vha->vp_idx, cmd, &cmd->se_cmd,
3772 spin_lock_irqsave(&cmd->cmd_lock, flags);
3774 spin_unlock_irqrestore(&cmd->cmd_lock, flags);
3776 * It's normal to see 2 calls in this path:
3777 * 1) XFER Rdy completion + CMD_T_ABORT
3778 * 2) TCM TMR - drain_state_list
3780 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf016,
3781 "multiple abort. %p transport_state %x, t_state %x, "
3782 "se_cmd_flags %x\n", cmd, cmd->se_cmd.transport_state,
3783 cmd->se_cmd.t_state, cmd->se_cmd.se_cmd_flags);
3787 cmd->trc_flags |= TRC_ABORT;
3788 spin_unlock_irqrestore(&cmd->cmd_lock, flags);
3790 qlt_send_term_exchange(cmd->qpair, cmd, &cmd->atio, 0, 1);
3793 EXPORT_SYMBOL(qlt_abort_cmd);
3795 void qlt_free_cmd(struct qla_tgt_cmd *cmd)
3797 struct fc_port *sess = cmd->sess;
3799 ql_dbg(ql_dbg_tgt, cmd->vha, 0xe074,
3800 "%s: se_cmd[%p] ox_id %04x\n",
3801 __func__, &cmd->se_cmd,
3802 be16_to_cpu(cmd->atio.u.isp24.fcp_hdr.ox_id));
3804 BUG_ON(cmd->cmd_in_wq);
3807 qlt_unmap_sg(cmd->vha, cmd);
3810 qlt_decr_num_pend_cmds(cmd->vha);
3812 BUG_ON(cmd->sg_mapped);
3813 cmd->jiffies_at_free = get_jiffies_64();
3814 if (unlikely(cmd->free_sg))
3817 if (!sess || !sess->se_sess) {
3821 cmd->jiffies_at_free = get_jiffies_64();
3822 cmd->vha->hw->tgt.tgt_ops->rel_cmd(cmd);
3824 EXPORT_SYMBOL(qlt_free_cmd);
3827 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
3829 static int qlt_term_ctio_exchange(struct qla_qpair *qpair, void *ctio,
3830 struct qla_tgt_cmd *cmd, uint32_t status)
3833 struct scsi_qla_host *vha = qpair->vha;
3835 if (cmd->se_cmd.prot_op)
3836 ql_dbg(ql_dbg_tgt_dif, vha, 0xe013,
3837 "Term DIF cmd: lba[0x%llx|%lld] len[0x%x] "
3838 "se_cmd=%p tag[%x] op %#x/%s",
3840 cmd->num_blks, &cmd->se_cmd,
3841 cmd->atio.u.isp24.exchange_addr,
3842 cmd->se_cmd.prot_op,
3843 prot_op_str(cmd->se_cmd.prot_op));
3846 struct ctio7_from_24xx *c = (struct ctio7_from_24xx *)ctio;
3849 cpu_to_le16(OF_TERM_EXCH));
3854 qlt_send_term_exchange(qpair, cmd, &cmd->atio, 1, 0);
3860 /* ha->hardware_lock supposed to be held on entry */
3861 static void *qlt_ctio_to_cmd(struct scsi_qla_host *vha,
3862 struct rsp_que *rsp, uint32_t handle, void *ctio)
3865 struct req_que *req;
3866 int qid = GET_QID(handle);
3867 uint32_t h = handle & ~QLA_TGT_HANDLE_MASK;
3869 if (unlikely(h == QLA_TGT_SKIP_HANDLE))
3872 if (qid == rsp->req->id) {
3874 } else if (vha->hw->req_q_map[qid]) {
3875 ql_dbg(ql_dbg_tgt_mgt, vha, 0x1000a,
3876 "qla_target(%d): CTIO completion with different QID %d handle %x\n",
3877 vha->vp_idx, rsp->id, handle);
3878 req = vha->hw->req_q_map[qid];
3883 h &= QLA_CMD_HANDLE_MASK;
3885 if (h != QLA_TGT_NULL_HANDLE) {
3886 if (unlikely(h >= req->num_outstanding_cmds)) {
3887 ql_dbg(ql_dbg_tgt, vha, 0xe052,
3888 "qla_target(%d): Wrong handle %x received\n",
3889 vha->vp_idx, handle);
3893 cmd = req->outstanding_cmds[h];
3894 if (unlikely(cmd == NULL)) {
3895 ql_dbg(ql_dbg_async, vha, 0xe053,
3896 "qla_target(%d): Suspicious: unable to find the command with handle %x req->id %d rsp->id %d\n",
3897 vha->vp_idx, handle, req->id, rsp->id);
3900 req->outstanding_cmds[h] = NULL;
3901 } else if (ctio != NULL) {
3902 /* We can't get loop ID from CTIO7 */
3903 ql_dbg(ql_dbg_tgt, vha, 0xe054,
3904 "qla_target(%d): Wrong CTIO received: QLA24xx doesn't "
3905 "support NULL handles\n", vha->vp_idx);
3913 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
3915 static void qlt_do_ctio_completion(struct scsi_qla_host *vha,
3916 struct rsp_que *rsp, uint32_t handle, uint32_t status, void *ctio)
3918 struct qla_hw_data *ha = vha->hw;
3919 struct se_cmd *se_cmd;
3920 struct qla_tgt_cmd *cmd;
3921 struct qla_qpair *qpair = rsp->qpair;
3923 if (handle & CTIO_INTERMEDIATE_HANDLE_MARK) {
3924 /* That could happen only in case of an error/reset/abort */
3925 if (status != CTIO_SUCCESS) {
3926 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01d,
3927 "Intermediate CTIO received"
3928 " (status %x)\n", status);
3933 cmd = qlt_ctio_to_cmd(vha, rsp, handle, ctio);
3937 se_cmd = &cmd->se_cmd;
3938 cmd->cmd_sent_to_fw = 0;
3940 qlt_unmap_sg(vha, cmd);
3942 if (unlikely(status != CTIO_SUCCESS)) {
3943 switch (status & 0xFFFF) {
3944 case CTIO_INVALID_RX_ID:
3945 if (printk_ratelimit())
3946 dev_info(&vha->hw->pdev->dev,
3947 "qla_target(%d): CTIO with INVALID_RX_ID ATIO attr %x CTIO Flags %x|%x\n",
3948 vha->vp_idx, cmd->atio.u.isp24.attr,
3949 ((cmd->ctio_flags >> 9) & 0xf),
3953 case CTIO_LIP_RESET:
3954 case CTIO_TARGET_RESET:
3956 /* driver request abort via Terminate exchange */
3959 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf058,
3960 "qla_target(%d): CTIO with "
3961 "status %#x received, state %x, se_cmd %p, "
3962 "(LIP_RESET=e, ABORTED=2, TARGET_RESET=17, "
3963 "TIMEOUT=b, INVALID_RX_ID=8)\n", vha->vp_idx,
3964 status, cmd->state, se_cmd);
3967 case CTIO_PORT_LOGGED_OUT:
3968 case CTIO_PORT_UNAVAILABLE:
3971 (status & 0xFFFF) == CTIO_PORT_LOGGED_OUT;
3973 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf059,
3974 "qla_target(%d): CTIO with %s status %x "
3975 "received (state %x, se_cmd %p)\n", vha->vp_idx,
3976 logged_out ? "PORT LOGGED OUT" : "PORT UNAVAILABLE",
3977 status, cmd->state, se_cmd);
3979 if (logged_out && cmd->sess) {
3981 * Session is already logged out, but we need
3982 * to notify initiator, who's not aware of this
3984 cmd->sess->send_els_logo = 1;
3985 ql_dbg(ql_dbg_disc, vha, 0x20f8,
3986 "%s %d %8phC post del sess\n",
3987 __func__, __LINE__, cmd->sess->port_name);
3989 qlt_schedule_sess_for_deletion(cmd->sess);
3993 case CTIO_DIF_ERROR: {
3994 struct ctio_crc_from_fw *crc =
3995 (struct ctio_crc_from_fw *)ctio;
3996 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf073,
3997 "qla_target(%d): CTIO with DIF_ERROR status %x "
3998 "received (state %x, ulp_cmd %p) actual_dif[0x%llx] "
3999 "expect_dif[0x%llx]\n",
4000 vha->vp_idx, status, cmd->state, se_cmd,
4001 *((u64 *)&crc->actual_dif[0]),
4002 *((u64 *)&crc->expected_dif[0]));
4004 qlt_handle_dif_error(qpair, cmd, ctio);
4008 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05b,
4009 "qla_target(%d): CTIO with error status 0x%x received (state %x, se_cmd %p\n",
4010 vha->vp_idx, status, cmd->state, se_cmd);
4015 /* "cmd->aborted" means
4016 * cmd is already aborted/terminated, we don't
4017 * need to terminate again. The exchange is already
4018 * cleaned up/freed at FW level. Just cleanup at driver
4021 if ((cmd->state != QLA_TGT_STATE_NEED_DATA) &&
4023 cmd->trc_flags |= TRC_CTIO_ERR;
4024 if (qlt_term_ctio_exchange(qpair, ctio, cmd, status))
4029 if (cmd->state == QLA_TGT_STATE_PROCESSED) {
4030 cmd->trc_flags |= TRC_CTIO_DONE;
4031 } else if (cmd->state == QLA_TGT_STATE_NEED_DATA) {
4032 cmd->state = QLA_TGT_STATE_DATA_IN;
4034 if (status == CTIO_SUCCESS)
4035 cmd->write_data_transferred = 1;
4037 ha->tgt.tgt_ops->handle_data(cmd);
4039 } else if (cmd->aborted) {
4040 cmd->trc_flags |= TRC_CTIO_ABORTED;
4041 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01e,
4042 "Aborted command %p (tag %lld) finished\n", cmd, se_cmd->tag);
4044 cmd->trc_flags |= TRC_CTIO_STRANGE;
4045 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05c,
4046 "qla_target(%d): A command in state (%d) should "
4047 "not return a CTIO complete\n", vha->vp_idx, cmd->state);
4050 if (unlikely(status != CTIO_SUCCESS) &&
4052 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01f, "Finishing failed CTIO\n");
4056 ha->tgt.tgt_ops->free_cmd(cmd);
4059 static inline int qlt_get_fcp_task_attr(struct scsi_qla_host *vha,
4064 switch (task_codes) {
4065 case ATIO_SIMPLE_QUEUE:
4066 fcp_task_attr = TCM_SIMPLE_TAG;
4068 case ATIO_HEAD_OF_QUEUE:
4069 fcp_task_attr = TCM_HEAD_TAG;
4071 case ATIO_ORDERED_QUEUE:
4072 fcp_task_attr = TCM_ORDERED_TAG;
4074 case ATIO_ACA_QUEUE:
4075 fcp_task_attr = TCM_ACA_TAG;
4078 fcp_task_attr = TCM_SIMPLE_TAG;
4081 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05d,
4082 "qla_target: unknown task code %x, use ORDERED instead\n",
4084 fcp_task_attr = TCM_ORDERED_TAG;
4088 return fcp_task_attr;
4092 * Process context for I/O path into tcm_qla2xxx code
4094 static void __qlt_do_work(struct qla_tgt_cmd *cmd)
4096 scsi_qla_host_t *vha = cmd->vha;
4097 struct qla_hw_data *ha = vha->hw;
4098 struct fc_port *sess = cmd->sess;
4099 struct atio_from_isp *atio = &cmd->atio;
4101 unsigned long flags;
4102 uint32_t data_length;
4103 int ret, fcp_task_attr, data_dir, bidi = 0;
4104 struct qla_qpair *qpair = cmd->qpair;
4107 cmd->trc_flags |= TRC_DO_WORK;
4110 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf082,
4111 "cmd with tag %u is aborted\n",
4112 cmd->atio.u.isp24.exchange_addr);
4116 spin_lock_init(&cmd->cmd_lock);
4117 cdb = &atio->u.isp24.fcp_cmnd.cdb[0];
4118 cmd->se_cmd.tag = le32_to_cpu(atio->u.isp24.exchange_addr);
4120 if (atio->u.isp24.fcp_cmnd.rddata &&
4121 atio->u.isp24.fcp_cmnd.wrdata) {
4123 data_dir = DMA_TO_DEVICE;
4124 } else if (atio->u.isp24.fcp_cmnd.rddata)
4125 data_dir = DMA_FROM_DEVICE;
4126 else if (atio->u.isp24.fcp_cmnd.wrdata)
4127 data_dir = DMA_TO_DEVICE;
4129 data_dir = DMA_NONE;
4131 fcp_task_attr = qlt_get_fcp_task_attr(vha,
4132 atio->u.isp24.fcp_cmnd.task_attr);
4133 data_length = get_datalen_for_atio(atio);
4135 ret = ha->tgt.tgt_ops->handle_cmd(vha, cmd, cdb, data_length,
4136 fcp_task_attr, data_dir, bidi);
4140 * Drop extra session reference from qlt_handle_cmd_for_atio().
4142 ha->tgt.tgt_ops->put_sess(sess);
4146 ql_dbg(ql_dbg_io, vha, 0x3060, "Terminating work cmd %p", cmd);
4148 * cmd has not sent to target yet, so pass NULL as the second
4149 * argument to qlt_send_term_exchange() and free the memory here.
4151 cmd->trc_flags |= TRC_DO_WORK_ERR;
4152 spin_lock_irqsave(qpair->qp_lock_ptr, flags);
4153 qlt_send_term_exchange(qpair, NULL, &cmd->atio, 1, 0);
4155 qlt_decr_num_pend_cmds(vha);
4156 cmd->vha->hw->tgt.tgt_ops->rel_cmd(cmd);
4157 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
4159 ha->tgt.tgt_ops->put_sess(sess);
4162 static void qlt_do_work(struct work_struct *work)
4164 struct qla_tgt_cmd *cmd = container_of(work, struct qla_tgt_cmd, work);
4165 scsi_qla_host_t *vha = cmd->vha;
4166 unsigned long flags;
4168 spin_lock_irqsave(&vha->cmd_list_lock, flags);
4169 list_del(&cmd->cmd_list);
4170 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
4175 void qlt_clr_qp_table(struct scsi_qla_host *vha)
4177 unsigned long flags;
4178 struct qla_hw_data *ha = vha->hw;
4179 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
4183 ql_log(ql_log_info, vha, 0x706c,
4184 "User update Number of Active Qpairs %d\n",
4185 ha->tgt.num_act_qpairs);
4187 spin_lock_irqsave(&ha->tgt.atio_lock, flags);
4189 btree_for_each_safe64(&tgt->lun_qpair_map, key, node)
4190 btree_remove64(&tgt->lun_qpair_map, key);
4192 ha->base_qpair->lun_cnt = 0;
4193 for (key = 0; key < ha->max_qpairs; key++)
4194 if (ha->queue_pair_map[key])
4195 ha->queue_pair_map[key]->lun_cnt = 0;
4197 spin_unlock_irqrestore(&ha->tgt.atio_lock, flags);
4200 static void qlt_assign_qpair(struct scsi_qla_host *vha,
4201 struct qla_tgt_cmd *cmd)
4203 struct qla_qpair *qpair, *qp;
4204 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
4205 struct qla_qpair_hint *h;
4207 if (vha->flags.qpairs_available) {
4208 h = btree_lookup64(&tgt->lun_qpair_map, cmd->unpacked_lun);
4210 /* spread lun to qpair ratio evently */
4212 struct scsi_qla_host *base_vha =
4213 pci_get_drvdata(vha->hw->pdev);
4215 qpair = vha->hw->base_qpair;
4216 if (qpair->lun_cnt == 0) {
4218 h = qla_qpair_to_hint(tgt, qpair);
4220 rc = btree_insert64(&tgt->lun_qpair_map,
4221 cmd->unpacked_lun, h, GFP_ATOMIC);
4224 ql_log(ql_log_info, vha, 0xd037,
4225 "Unable to insert lun %llx into lun_qpair_map\n",
4230 lcnt = qpair->lun_cnt;
4234 list_for_each_entry(qp, &base_vha->qp_list,
4236 if (qp->lun_cnt == 0) {
4238 h = qla_qpair_to_hint(tgt, qp);
4240 rc = btree_insert64(&tgt->lun_qpair_map,
4241 cmd->unpacked_lun, h, GFP_ATOMIC);
4244 ql_log(ql_log_info, vha, 0xd038,
4245 "Unable to insert lun %llx into lun_qpair_map\n",
4251 if (qp->lun_cnt < lcnt) {
4260 h = qla_qpair_to_hint(tgt, qpair);
4262 rc = btree_insert64(&tgt->lun_qpair_map,
4263 cmd->unpacked_lun, h, GFP_ATOMIC);
4266 ql_log(ql_log_info, vha, 0xd039,
4267 "Unable to insert lun %llx into lun_qpair_map\n",
4272 h = &tgt->qphints[0];
4275 cmd->qpair = h->qpair;
4276 cmd->se_cmd.cpuid = h->cpuid;
4279 static struct qla_tgt_cmd *qlt_get_tag(scsi_qla_host_t *vha,
4280 struct fc_port *sess,
4281 struct atio_from_isp *atio)
4283 struct qla_tgt_cmd *cmd;
4285 cmd = vha->hw->tgt.tgt_ops->get_cmd(sess);
4289 cmd->cmd_type = TYPE_TGT_CMD;
4290 memcpy(&cmd->atio, atio, sizeof(*atio));
4291 INIT_LIST_HEAD(&cmd->sess_cmd_list);
4292 cmd->state = QLA_TGT_STATE_NEW;
4293 cmd->tgt = vha->vha_tgt.qla_tgt;
4294 qlt_incr_num_pend_cmds(vha);
4297 cmd->loop_id = sess->loop_id;
4298 cmd->conf_compl_supported = sess->conf_compl_supported;
4301 cmd->jiffies_at_alloc = get_jiffies_64();
4303 cmd->unpacked_lun = scsilun_to_int(
4304 (struct scsi_lun *)&atio->u.isp24.fcp_cmnd.lun);
4305 qlt_assign_qpair(vha, cmd);
4306 cmd->reset_count = vha->hw->base_qpair->chip_reset;
4307 cmd->vp_idx = vha->vp_idx;
4312 /* ha->hardware_lock supposed to be held on entry */
4313 static int qlt_handle_cmd_for_atio(struct scsi_qla_host *vha,
4314 struct atio_from_isp *atio)
4316 struct qla_hw_data *ha = vha->hw;
4317 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
4318 struct fc_port *sess;
4319 struct qla_tgt_cmd *cmd;
4320 unsigned long flags;
4323 if (unlikely(tgt->tgt_stop)) {
4324 ql_dbg(ql_dbg_io, vha, 0x3061,
4325 "New command while device %p is shutting down\n", tgt);
4329 id = be_to_port_id(atio->u.isp24.fcp_hdr.s_id);
4330 if (IS_SW_RESV_ADDR(id))
4333 sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha, atio->u.isp24.fcp_hdr.s_id);
4334 if (unlikely(!sess))
4337 /* Another WWN used to have our s_id. Our PLOGI scheduled its
4338 * session deletion, but it's still in sess_del_work wq */
4339 if (sess->deleted) {
4340 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf002,
4341 "New command while old session %p is being deleted\n",
4347 * Do kref_get() before returning + dropping qla_hw_data->hardware_lock.
4349 if (!kref_get_unless_zero(&sess->sess_kref)) {
4350 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf004,
4351 "%s: kref_get fail, %8phC oxid %x \n",
4352 __func__, sess->port_name,
4353 be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id));
4357 cmd = qlt_get_tag(vha, sess, atio);
4359 ql_dbg(ql_dbg_io, vha, 0x3062,
4360 "qla_target(%d): Allocation of cmd failed\n", vha->vp_idx);
4361 ha->tgt.tgt_ops->put_sess(sess);
4366 cmd->trc_flags |= TRC_NEW_CMD;
4368 spin_lock_irqsave(&vha->cmd_list_lock, flags);
4369 list_add_tail(&cmd->cmd_list, &vha->qla_cmd_list);
4370 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
4372 INIT_WORK(&cmd->work, qlt_do_work);
4373 if (vha->flags.qpairs_available) {
4374 queue_work_on(cmd->se_cmd.cpuid, qla_tgt_wq, &cmd->work);
4375 } else if (ha->msix_count) {
4376 if (cmd->atio.u.isp24.fcp_cmnd.rddata)
4377 queue_work_on(smp_processor_id(), qla_tgt_wq,
4380 queue_work_on(cmd->se_cmd.cpuid, qla_tgt_wq,
4383 queue_work(qla_tgt_wq, &cmd->work);
4389 /* ha->hardware_lock supposed to be held on entry */
4390 static int qlt_issue_task_mgmt(struct fc_port *sess, u64 lun,
4391 int fn, void *iocb, int flags)
4393 struct scsi_qla_host *vha = sess->vha;
4394 struct qla_hw_data *ha = vha->hw;
4395 struct qla_tgt_mgmt_cmd *mcmd;
4396 struct atio_from_isp *a = (struct atio_from_isp *)iocb;
4397 struct qla_qpair_hint *h = &vha->vha_tgt.qla_tgt->qphints[0];
4399 mcmd = mempool_alloc(qla_tgt_mgmt_cmd_mempool, GFP_ATOMIC);
4401 ql_dbg(ql_dbg_tgt_tmr, vha, 0x10009,
4402 "qla_target(%d): Allocation of management "
4403 "command failed, some commands and their data could "
4404 "leak\n", vha->vp_idx);
4407 memset(mcmd, 0, sizeof(*mcmd));
4411 memcpy(&mcmd->orig_iocb.imm_ntfy, iocb,
4412 sizeof(mcmd->orig_iocb.imm_ntfy));
4414 mcmd->tmr_func = fn;
4415 mcmd->flags = flags;
4416 mcmd->reset_count = ha->base_qpair->chip_reset;
4417 mcmd->qpair = h->qpair;
4419 mcmd->se_cmd.cpuid = h->cpuid;
4420 mcmd->unpacked_lun = lun;
4423 case QLA_TGT_LUN_RESET:
4424 case QLA_TGT_CLEAR_TS:
4425 case QLA_TGT_ABORT_TS:
4426 abort_cmds_for_lun(vha, lun, a->u.isp24.fcp_hdr.s_id);
4428 case QLA_TGT_CLEAR_ACA:
4429 h = qlt_find_qphint(vha, mcmd->unpacked_lun);
4430 mcmd->qpair = h->qpair;
4431 mcmd->se_cmd.cpuid = h->cpuid;
4434 case QLA_TGT_TARGET_RESET:
4435 case QLA_TGT_NEXUS_LOSS_SESS:
4436 case QLA_TGT_NEXUS_LOSS:
4437 case QLA_TGT_ABORT_ALL:
4443 INIT_WORK(&mcmd->work, qlt_do_tmr_work);
4444 queue_work_on(mcmd->se_cmd.cpuid, qla_tgt_wq,
4450 /* ha->hardware_lock supposed to be held on entry */
4451 static int qlt_handle_task_mgmt(struct scsi_qla_host *vha, void *iocb)
4453 struct atio_from_isp *a = (struct atio_from_isp *)iocb;
4454 struct qla_hw_data *ha = vha->hw;
4455 struct fc_port *sess;
4458 unsigned long flags;
4460 fn = a->u.isp24.fcp_cmnd.task_mgmt_flags;
4462 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
4463 sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha,
4464 a->u.isp24.fcp_hdr.s_id);
4465 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
4468 scsilun_to_int((struct scsi_lun *)&a->u.isp24.fcp_cmnd.lun);
4470 if (sess == NULL || sess->deleted)
4473 return qlt_issue_task_mgmt(sess, unpacked_lun, fn, iocb, 0);
4476 /* ha->hardware_lock supposed to be held on entry */
4477 static int __qlt_abort_task(struct scsi_qla_host *vha,
4478 struct imm_ntfy_from_isp *iocb, struct fc_port *sess)
4480 struct atio_from_isp *a = (struct atio_from_isp *)iocb;
4481 struct qla_hw_data *ha = vha->hw;
4482 struct qla_tgt_mgmt_cmd *mcmd;
4486 mcmd = mempool_alloc(qla_tgt_mgmt_cmd_mempool, GFP_ATOMIC);
4488 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05f,
4489 "qla_target(%d): %s: Allocation of ABORT cmd failed\n",
4490 vha->vp_idx, __func__);
4493 memset(mcmd, 0, sizeof(*mcmd));
4496 memcpy(&mcmd->orig_iocb.imm_ntfy, iocb,
4497 sizeof(mcmd->orig_iocb.imm_ntfy));
4500 scsilun_to_int((struct scsi_lun *)&a->u.isp24.fcp_cmnd.lun);
4501 mcmd->reset_count = ha->base_qpair->chip_reset;
4502 mcmd->tmr_func = QLA_TGT_2G_ABORT_TASK;
4503 mcmd->qpair = ha->base_qpair;
4505 rc = ha->tgt.tgt_ops->handle_tmr(mcmd, unpacked_lun, mcmd->tmr_func,
4506 le16_to_cpu(iocb->u.isp2x.seq_id));
4508 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf060,
4509 "qla_target(%d): tgt_ops->handle_tmr() failed: %d\n",
4511 mempool_free(mcmd, qla_tgt_mgmt_cmd_mempool);
4518 /* ha->hardware_lock supposed to be held on entry */
4519 static int qlt_abort_task(struct scsi_qla_host *vha,
4520 struct imm_ntfy_from_isp *iocb)
4522 struct qla_hw_data *ha = vha->hw;
4523 struct fc_port *sess;
4525 unsigned long flags;
4527 loop_id = GET_TARGET_ID(ha, (struct atio_from_isp *)iocb);
4529 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
4530 sess = ha->tgt.tgt_ops->find_sess_by_loop_id(vha, loop_id);
4531 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
4534 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf025,
4535 "qla_target(%d): task abort for unexisting "
4536 "session\n", vha->vp_idx);
4537 return qlt_sched_sess_work(vha->vha_tgt.qla_tgt,
4538 QLA_TGT_SESS_WORK_ABORT, iocb, sizeof(*iocb));
4541 return __qlt_abort_task(vha, iocb, sess);
4544 void qlt_logo_completion_handler(fc_port_t *fcport, int rc)
4546 if (rc != MBS_COMMAND_COMPLETE) {
4547 ql_dbg(ql_dbg_tgt_mgt, fcport->vha, 0xf093,
4548 "%s: se_sess %p / sess %p from"
4549 " port %8phC loop_id %#04x s_id %02x:%02x:%02x"
4550 " LOGO failed: %#x\n",
4554 fcport->port_name, fcport->loop_id,
4555 fcport->d_id.b.domain, fcport->d_id.b.area,
4556 fcport->d_id.b.al_pa, rc);
4559 fcport->logout_completed = 1;
4563 * ha->hardware_lock supposed to be held on entry (to protect tgt->sess_list)
4565 * Schedules sessions with matching port_id/loop_id but different wwn for
4566 * deletion. Returns existing session with matching wwn if present.
4570 qlt_find_sess_invalidate_other(scsi_qla_host_t *vha, uint64_t wwn,
4571 port_id_t port_id, uint16_t loop_id, struct fc_port **conflict_sess)
4573 struct fc_port *sess = NULL, *other_sess;
4576 *conflict_sess = NULL;
4578 list_for_each_entry(other_sess, &vha->vp_fcports, list) {
4580 other_wwn = wwn_to_u64(other_sess->port_name);
4582 if (wwn == other_wwn) {
4588 /* find other sess with nport_id collision */
4589 if (port_id.b24 == other_sess->d_id.b24) {
4590 if (loop_id != other_sess->loop_id) {
4591 ql_dbg(ql_dbg_disc, vha, 0x1000c,
4592 "Invalidating sess %p loop_id %d wwn %llx.\n",
4593 other_sess, other_sess->loop_id, other_wwn);
4596 * logout_on_delete is set by default, but another
4597 * session that has the same s_id/loop_id combo
4598 * might have cleared it when requested this session
4599 * deletion, so don't touch it
4601 qlt_schedule_sess_for_deletion(other_sess);
4604 * Another wwn used to have our s_id/loop_id
4605 * kill the session, but don't free the loop_id
4607 ql_dbg(ql_dbg_disc, vha, 0xf01b,
4608 "Invalidating sess %p loop_id %d wwn %llx.\n",
4609 other_sess, other_sess->loop_id, other_wwn);
4611 other_sess->keep_nport_handle = 1;
4612 if (other_sess->disc_state != DSC_DELETED)
4613 *conflict_sess = other_sess;
4614 qlt_schedule_sess_for_deletion(other_sess);
4619 /* find other sess with nport handle collision */
4620 if ((loop_id == other_sess->loop_id) &&
4621 (loop_id != FC_NO_LOOP_ID)) {
4622 ql_dbg(ql_dbg_disc, vha, 0x1000d,
4623 "Invalidating sess %p loop_id %d wwn %llx.\n",
4624 other_sess, other_sess->loop_id, other_wwn);
4626 /* Same loop_id but different s_id
4627 * Ok to kill and logout */
4628 qlt_schedule_sess_for_deletion(other_sess);
4635 /* Abort any commands for this s_id waiting on qla_tgt_wq workqueue */
4636 static int abort_cmds_for_s_id(struct scsi_qla_host *vha, port_id_t *s_id)
4638 struct qla_tgt_sess_op *op;
4639 struct qla_tgt_cmd *cmd;
4642 unsigned long flags;
4644 key = (((u32)s_id->b.domain << 16) |
4645 ((u32)s_id->b.area << 8) |
4646 ((u32)s_id->b.al_pa));
4648 spin_lock_irqsave(&vha->cmd_list_lock, flags);
4649 list_for_each_entry(op, &vha->qla_sess_op_cmd_list, cmd_list) {
4650 uint32_t op_key = sid_to_key(op->atio.u.isp24.fcp_hdr.s_id);
4652 if (op_key == key) {
4658 list_for_each_entry(op, &vha->unknown_atio_list, cmd_list) {
4659 uint32_t op_key = sid_to_key(op->atio.u.isp24.fcp_hdr.s_id);
4661 if (op_key == key) {
4667 list_for_each_entry(cmd, &vha->qla_cmd_list, cmd_list) {
4668 uint32_t cmd_key = sid_to_key(cmd->atio.u.isp24.fcp_hdr.s_id);
4670 if (cmd_key == key) {
4675 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
4680 static int qlt_handle_login(struct scsi_qla_host *vha,
4681 struct imm_ntfy_from_isp *iocb)
4683 struct fc_port *sess = NULL, *conflict_sess = NULL;
4686 uint16_t loop_id, wd3_lo;
4688 struct qlt_plogi_ack_t *pla;
4689 unsigned long flags;
4691 lockdep_assert_held(&vha->hw->hardware_lock);
4693 wwn = wwn_to_u64(iocb->u.isp24.port_name);
4695 port_id.b.domain = iocb->u.isp24.port_id[2];
4696 port_id.b.area = iocb->u.isp24.port_id[1];
4697 port_id.b.al_pa = iocb->u.isp24.port_id[0];
4698 port_id.b.rsvd_1 = 0;
4700 loop_id = le16_to_cpu(iocb->u.isp24.nport_handle);
4702 /* Mark all stale commands sitting in qla_tgt_wq for deletion */
4703 abort_cmds_for_s_id(vha, &port_id);
4706 spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
4707 sess = qlt_find_sess_invalidate_other(vha, wwn,
4708 port_id, loop_id, &conflict_sess);
4709 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
4711 ql_dbg(ql_dbg_disc, vha, 0xffff,
4712 "%s %d Term INOT due to WWN=0 lid=%d, NportID %06X ",
4713 __func__, __LINE__, loop_id, port_id.b24);
4714 qlt_send_term_imm_notif(vha, iocb, 1);
4718 if (IS_SW_RESV_ADDR(port_id)) {
4723 pla = qlt_plogi_ack_find_add(vha, &port_id, iocb);
4725 ql_dbg(ql_dbg_disc + ql_dbg_verbose, vha, 0xffff,
4726 "%s %d %8phC Term INOT due to mem alloc fail",
4728 iocb->u.isp24.port_name);
4729 qlt_send_term_imm_notif(vha, iocb, 1);
4733 if (conflict_sess) {
4734 conflict_sess->login_gen++;
4735 qlt_plogi_ack_link(vha, pla, conflict_sess,
4736 QLT_PLOGI_LINK_CONFLICT);
4741 ql_dbg(ql_dbg_disc, vha, 0xffff,
4742 "%s %d %8phC post new sess\n",
4743 __func__, __LINE__, iocb->u.isp24.port_name);
4744 if (iocb->u.isp24.status_subcode == ELS_PLOGI)
4745 qla24xx_post_newsess_work(vha, &port_id,
4746 iocb->u.isp24.port_name,
4747 iocb->u.isp24.u.plogi.node_name,
4750 qla24xx_post_newsess_work(vha, &port_id,
4751 iocb->u.isp24.port_name, NULL,
4757 if (sess->disc_state == DSC_UPD_FCPORT) {
4761 * Remote port registration is still going on from
4762 * previous login. Allow it to finish before we
4763 * accept the new login.
4765 sess->next_disc_state = DSC_DELETE_PEND;
4766 sec = jiffies_to_msecs(jiffies -
4767 sess->jiffies_at_registration) / 1000;
4768 if (sess->sec_since_registration < sec && sec &&
4770 sess->sec_since_registration = sec;
4771 ql_dbg(ql_dbg_disc, vha, 0xffff,
4772 "%s %8phC - Slow Rport registration (%d Sec)\n",
4773 __func__, sess->port_name, sec);
4776 if (!conflict_sess) {
4777 list_del(&pla->list);
4778 kmem_cache_free(qla_tgt_plogi_cachep, pla);
4781 qlt_send_term_imm_notif(vha, iocb, 1);
4785 qlt_plogi_ack_link(vha, pla, sess, QLT_PLOGI_LINK_SAME_WWN);
4786 sess->d_id = port_id;
4789 if (iocb->u.isp24.status_subcode == ELS_PRLI) {
4790 sess->fw_login_state = DSC_LS_PRLI_PEND;
4792 sess->loop_id = loop_id;
4793 sess->d_id = port_id;
4794 sess->fw_login_state = DSC_LS_PRLI_PEND;
4795 wd3_lo = le16_to_cpu(iocb->u.isp24.u.prli.wd3_lo);
4798 sess->conf_compl_supported = 1;
4800 if ((wd3_lo & BIT_4) == 0)
4801 sess->port_type = FCT_INITIATOR;
4803 sess->port_type = FCT_TARGET;
4806 sess->fw_login_state = DSC_LS_PLOGI_PEND;
4809 ql_dbg(ql_dbg_disc, vha, 0x20f9,
4810 "%s %d %8phC DS %d\n",
4811 __func__, __LINE__, sess->port_name, sess->disc_state);
4813 switch (sess->disc_state) {
4815 case DSC_LOGIN_PEND:
4816 qlt_plogi_ack_unref(vha, pla);
4821 * Under normal circumstances we want to release nport handle
4822 * during LOGO process to avoid nport handle leaks inside FW.
4823 * The exception is when LOGO is done while another PLOGI with
4824 * the same nport handle is waiting as might be the case here.
4825 * Note: there is always a possibily of a race where session
4826 * deletion has already started for other reasons (e.g. ACL
4827 * removal) and now PLOGI arrives:
4828 * 1. if PLOGI arrived in FW after nport handle has been freed,
4829 * FW must have assigned this PLOGI a new/same handle and we
4830 * can proceed ACK'ing it as usual when session deletion
4832 * 2. if PLOGI arrived in FW before LOGO with LCF_FREE_NPORT
4833 * bit reached it, the handle has now been released. We'll
4834 * get an error when we ACK this PLOGI. Nothing will be sent
4835 * back to initiator. Initiator should eventually retry
4836 * PLOGI and situation will correct itself.
4838 sess->keep_nport_handle = ((sess->loop_id == loop_id) &&
4839 (sess->d_id.b24 == port_id.b24));
4841 ql_dbg(ql_dbg_disc, vha, 0x20f9,
4842 "%s %d %8phC post del sess\n",
4843 __func__, __LINE__, sess->port_name);
4846 qlt_schedule_sess_for_deletion(sess);
4854 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
4856 static int qlt_24xx_handle_els(struct scsi_qla_host *vha,
4857 struct imm_ntfy_from_isp *iocb)
4859 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
4860 struct qla_hw_data *ha = vha->hw;
4861 struct fc_port *sess = NULL, *conflict_sess = NULL;
4867 unsigned long flags;
4869 lockdep_assert_held(&ha->hardware_lock);
4871 wwn = wwn_to_u64(iocb->u.isp24.port_name);
4873 port_id.b.domain = iocb->u.isp24.port_id[2];
4874 port_id.b.area = iocb->u.isp24.port_id[1];
4875 port_id.b.al_pa = iocb->u.isp24.port_id[0];
4876 port_id.b.rsvd_1 = 0;
4878 loop_id = le16_to_cpu(iocb->u.isp24.nport_handle);
4880 ql_dbg(ql_dbg_disc, vha, 0xf026,
4881 "qla_target(%d): Port ID: %02x:%02x:%02x ELS opcode: 0x%02x lid %d %8phC\n",
4882 vha->vp_idx, iocb->u.isp24.port_id[2],
4883 iocb->u.isp24.port_id[1], iocb->u.isp24.port_id[0],
4884 iocb->u.isp24.status_subcode, loop_id,
4885 iocb->u.isp24.port_name);
4887 /* res = 1 means ack at the end of thread
4888 * res = 0 means ack async/later.
4890 switch (iocb->u.isp24.status_subcode) {
4892 res = qlt_handle_login(vha, iocb);
4897 sess = qla2x00_find_fcport_by_wwpn(vha,
4898 iocb->u.isp24.port_name, 1);
4900 if (sess && sess->plogi_link[QLT_PLOGI_LINK_SAME_WWN]) {
4901 ql_dbg(ql_dbg_disc, vha, 0xffff,
4902 "%s %d %8phC Term PRLI due to PLOGI ACK not completed\n",
4904 iocb->u.isp24.port_name);
4905 qlt_send_term_imm_notif(vha, iocb, 1);
4909 res = qlt_handle_login(vha, iocb);
4913 if (IS_SW_RESV_ADDR(port_id)) {
4918 wd3_lo = le16_to_cpu(iocb->u.isp24.u.prli.wd3_lo);
4921 spin_lock_irqsave(&tgt->ha->tgt.sess_lock, flags);
4922 sess = qlt_find_sess_invalidate_other(vha, wwn, port_id,
4923 loop_id, &conflict_sess);
4924 spin_unlock_irqrestore(&tgt->ha->tgt.sess_lock, flags);
4927 if (conflict_sess) {
4928 switch (conflict_sess->disc_state) {
4930 case DSC_DELETE_PEND:
4933 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf09b,
4934 "PRLI with conflicting sess %p port %8phC\n",
4935 conflict_sess, conflict_sess->port_name);
4936 conflict_sess->fw_login_state =
4937 DSC_LS_PORT_UNAVAIL;
4938 qlt_send_term_imm_notif(vha, iocb, 1);
4945 bool delete = false;
4948 spin_lock_irqsave(&tgt->ha->tgt.sess_lock, flags);
4949 switch (sess->fw_login_state) {
4950 case DSC_LS_PLOGI_PEND:
4951 case DSC_LS_PLOGI_COMP:
4952 case DSC_LS_PRLI_COMP:
4959 switch (sess->disc_state) {
4960 case DSC_UPD_FCPORT:
4961 spin_unlock_irqrestore(&tgt->ha->tgt.sess_lock,
4964 sec = jiffies_to_msecs(jiffies -
4965 sess->jiffies_at_registration)/1000;
4966 if (sess->sec_since_registration < sec && sec &&
4968 sess->sec_since_registration = sec;
4969 ql_dbg(ql_dbg_disc, sess->vha, 0xffff,
4970 "%s %8phC : Slow Rport registration(%d Sec)\n",
4971 __func__, sess->port_name, sec);
4973 qlt_send_term_imm_notif(vha, iocb, 1);
4976 case DSC_LOGIN_PEND:
4978 case DSC_LOGIN_COMPLETE:
4987 spin_unlock_irqrestore(&tgt->ha->tgt.sess_lock,
4990 * Impatient initiator sent PRLI before last
4991 * PLOGI could finish. Will force him to re-try,
4992 * while last one finishes.
4994 ql_log(ql_log_warn, sess->vha, 0xf095,
4995 "sess %p PRLI received, before plogi ack.\n",
4997 qlt_send_term_imm_notif(vha, iocb, 1);
5003 * This shouldn't happen under normal circumstances,
5004 * since we have deleted the old session during PLOGI
5006 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf096,
5007 "PRLI (loop_id %#04x) for existing sess %p (loop_id %#04x)\n",
5008 sess->loop_id, sess, iocb->u.isp24.nport_handle);
5011 sess->loop_id = loop_id;
5012 sess->d_id = port_id;
5013 sess->fw_login_state = DSC_LS_PRLI_PEND;
5016 sess->conf_compl_supported = 1;
5018 if ((wd3_lo & BIT_4) == 0)
5019 sess->port_type = FCT_INITIATOR;
5021 sess->port_type = FCT_TARGET;
5023 spin_unlock_irqrestore(&tgt->ha->tgt.sess_lock, flags);
5025 res = 1; /* send notify ack */
5027 /* Make session global (not used in fabric mode) */
5028 if (ha->current_topology != ISP_CFG_F) {
5030 ql_dbg(ql_dbg_disc, vha, 0x20fa,
5031 "%s %d %8phC post nack\n",
5032 __func__, __LINE__, sess->port_name);
5033 qla24xx_post_nack_work(vha, sess, iocb,
5037 set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
5038 set_bit(LOCAL_LOOP_UPDATE, &vha->dpc_flags);
5039 qla2xxx_wake_dpc(vha);
5043 ql_dbg(ql_dbg_disc, vha, 0x20fb,
5044 "%s %d %8phC post nack\n",
5045 __func__, __LINE__, sess->port_name);
5046 qla24xx_post_nack_work(vha, sess, iocb,
5054 if (le16_to_cpu(iocb->u.isp24.flags) &
5055 NOTIFY24XX_FLAGS_GLOBAL_TPRLO) {
5057 qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS);
5064 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
5065 sess = qla2x00_find_fcport_by_loopid(vha, loop_id);
5066 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
5070 sess->fw_login_state = DSC_LS_LOGO_PEND;
5071 sess->logo_ack_needed = 1;
5072 memcpy(sess->iocb, iocb, IOCB_SIZE);
5075 res = qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS_SESS);
5077 ql_dbg(ql_dbg_disc, vha, 0x20fc,
5078 "%s: logo %llx res %d sess %p ",
5079 __func__, wwn, res, sess);
5082 * cmd went upper layer, look for qlt_xmit_tm_rsp()
5083 * for LOGO_ACK & sess delete
5088 /* cmd did not go to upper layer. */
5090 qlt_schedule_sess_for_deletion(sess);
5093 /* else logo will be ack */
5099 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
5101 if (tgt->link_reinit_iocb_pending) {
5102 qlt_send_notify_ack(ha->base_qpair,
5103 &tgt->link_reinit_iocb, 0, 0, 0, 0, 0, 0);
5104 tgt->link_reinit_iocb_pending = 0;
5107 sess = qla2x00_find_fcport_by_wwpn(vha,
5108 iocb->u.isp24.port_name, 1);
5110 ql_dbg(ql_dbg_disc, vha, 0x20fd,
5111 "sess %p lid %d|%d DS %d LS %d\n",
5112 sess, sess->loop_id, loop_id,
5113 sess->disc_state, sess->fw_login_state);
5116 res = 1; /* send notify ack */
5120 case ELS_FLOGI: /* should never happen */
5122 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf061,
5123 "qla_target(%d): Unsupported ELS command %x "
5124 "received\n", vha->vp_idx, iocb->u.isp24.status_subcode);
5125 res = qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS_SESS);
5129 ql_dbg(ql_dbg_disc, vha, 0xf026,
5130 "qla_target(%d): Exit ELS opcode: 0x%02x res %d\n",
5131 vha->vp_idx, iocb->u.isp24.status_subcode, res);
5137 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
5139 static void qlt_handle_imm_notify(struct scsi_qla_host *vha,
5140 struct imm_ntfy_from_isp *iocb)
5142 struct qla_hw_data *ha = vha->hw;
5143 uint32_t add_flags = 0;
5144 int send_notify_ack = 1;
5147 lockdep_assert_held(&ha->hardware_lock);
5149 status = le16_to_cpu(iocb->u.isp2x.status);
5151 case IMM_NTFY_LIP_RESET:
5153 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf032,
5154 "qla_target(%d): LIP reset (loop %#x), subcode %x\n",
5155 vha->vp_idx, le16_to_cpu(iocb->u.isp24.nport_handle),
5156 iocb->u.isp24.status_subcode);
5158 if (qlt_reset(vha, iocb, QLA_TGT_ABORT_ALL) == 0)
5159 send_notify_ack = 0;
5163 case IMM_NTFY_LIP_LINK_REINIT:
5165 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
5167 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf033,
5168 "qla_target(%d): LINK REINIT (loop %#x, "
5169 "subcode %x)\n", vha->vp_idx,
5170 le16_to_cpu(iocb->u.isp24.nport_handle),
5171 iocb->u.isp24.status_subcode);
5172 if (tgt->link_reinit_iocb_pending) {
5173 qlt_send_notify_ack(ha->base_qpair,
5174 &tgt->link_reinit_iocb, 0, 0, 0, 0, 0, 0);
5176 memcpy(&tgt->link_reinit_iocb, iocb, sizeof(*iocb));
5177 tgt->link_reinit_iocb_pending = 1;
5179 * QLogic requires to wait after LINK REINIT for possible
5180 * PDISC or ADISC ELS commands
5182 send_notify_ack = 0;
5186 case IMM_NTFY_PORT_LOGOUT:
5187 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf034,
5188 "qla_target(%d): Port logout (loop "
5189 "%#x, subcode %x)\n", vha->vp_idx,
5190 le16_to_cpu(iocb->u.isp24.nport_handle),
5191 iocb->u.isp24.status_subcode);
5193 if (qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS_SESS) == 0)
5194 send_notify_ack = 0;
5195 /* The sessions will be cleared in the callback, if needed */
5198 case IMM_NTFY_GLBL_TPRLO:
5199 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf035,
5200 "qla_target(%d): Global TPRLO (%x)\n", vha->vp_idx, status);
5201 if (qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS) == 0)
5202 send_notify_ack = 0;
5203 /* The sessions will be cleared in the callback, if needed */
5206 case IMM_NTFY_PORT_CONFIG:
5207 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf036,
5208 "qla_target(%d): Port config changed (%x)\n", vha->vp_idx,
5210 if (qlt_reset(vha, iocb, QLA_TGT_ABORT_ALL) == 0)
5211 send_notify_ack = 0;
5212 /* The sessions will be cleared in the callback, if needed */
5215 case IMM_NTFY_GLBL_LOGO:
5216 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06a,
5217 "qla_target(%d): Link failure detected\n",
5219 /* I_T nexus loss */
5220 if (qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS) == 0)
5221 send_notify_ack = 0;
5224 case IMM_NTFY_IOCB_OVERFLOW:
5225 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06b,
5226 "qla_target(%d): Cannot provide requested "
5227 "capability (IOCB overflowed the immediate notify "
5228 "resource count)\n", vha->vp_idx);
5231 case IMM_NTFY_ABORT_TASK:
5232 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf037,
5233 "qla_target(%d): Abort Task (S %08x I %#x -> "
5234 "L %#x)\n", vha->vp_idx,
5235 le16_to_cpu(iocb->u.isp2x.seq_id),
5236 GET_TARGET_ID(ha, (struct atio_from_isp *)iocb),
5237 le16_to_cpu(iocb->u.isp2x.lun));
5238 if (qlt_abort_task(vha, iocb) == 0)
5239 send_notify_ack = 0;
5242 case IMM_NTFY_RESOURCE:
5243 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06c,
5244 "qla_target(%d): Out of resources, host %ld\n",
5245 vha->vp_idx, vha->host_no);
5248 case IMM_NTFY_MSG_RX:
5249 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf038,
5250 "qla_target(%d): Immediate notify task %x\n",
5251 vha->vp_idx, iocb->u.isp2x.task_flags);
5255 if (qlt_24xx_handle_els(vha, iocb) == 0)
5256 send_notify_ack = 0;
5259 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06d,
5260 "qla_target(%d): Received unknown immediate "
5261 "notify status %x\n", vha->vp_idx, status);
5265 if (send_notify_ack)
5266 qlt_send_notify_ack(ha->base_qpair, iocb, add_flags, 0, 0, 0,
5271 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
5272 * This function sends busy to ISP 2xxx or 24xx.
5274 static int __qlt_send_busy(struct qla_qpair *qpair,
5275 struct atio_from_isp *atio, uint16_t status)
5277 struct scsi_qla_host *vha = qpair->vha;
5278 struct ctio7_to_24xx *ctio24;
5279 struct qla_hw_data *ha = vha->hw;
5281 struct fc_port *sess = NULL;
5282 unsigned long flags;
5286 id = be_to_port_id(atio->u.isp24.fcp_hdr.s_id);
5288 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
5289 sess = qla2x00_find_fcport_by_nportid(vha, &id, 1);
5290 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
5292 qlt_send_term_exchange(qpair, NULL, atio, 1, 0);
5295 /* Sending marker isn't necessary, since we called from ISR */
5297 pkt = (request_t *)__qla2x00_alloc_iocbs(qpair, NULL);
5299 ql_dbg(ql_dbg_io, vha, 0x3063,
5300 "qla_target(%d): %s failed: unable to allocate "
5301 "request packet", vha->vp_idx, __func__);
5305 qpair->tgt_counters.num_q_full_sent++;
5306 pkt->entry_count = 1;
5307 pkt->handle = QLA_TGT_SKIP_HANDLE | CTIO_COMPLETION_HANDLE_MARK;
5309 ctio24 = (struct ctio7_to_24xx *)pkt;
5310 ctio24->entry_type = CTIO_TYPE7;
5311 ctio24->nport_handle = cpu_to_le16(sess->loop_id);
5312 ctio24->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
5313 ctio24->vp_index = vha->vp_idx;
5314 ctio24->initiator_id = be_id_to_le(atio->u.isp24.fcp_hdr.s_id);
5315 ctio24->exchange_addr = atio->u.isp24.exchange_addr;
5316 temp = (atio->u.isp24.attr << 9) |
5317 CTIO7_FLAGS_STATUS_MODE_1 | CTIO7_FLAGS_SEND_STATUS |
5318 CTIO7_FLAGS_DONT_RET_CTIO;
5319 ctio24->u.status1.flags = cpu_to_le16(temp);
5321 * CTIO from fw w/o se_cmd doesn't provide enough info to retry it,
5322 * if the explicit conformation is used.
5324 ctio24->u.status1.ox_id =
5325 cpu_to_le16(be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id));
5326 ctio24->u.status1.scsi_status = cpu_to_le16(status);
5328 ctio24->u.status1.residual = cpu_to_le32(get_datalen_for_atio(atio));
5330 if (ctio24->u.status1.residual != 0)
5331 ctio24->u.status1.scsi_status |= cpu_to_le16(SS_RESIDUAL_UNDER);
5333 /* Memory Barrier */
5335 if (qpair->reqq_start_iocbs)
5336 qpair->reqq_start_iocbs(qpair);
5338 qla2x00_start_iocbs(vha, qpair->req);
5343 * This routine is used to allocate a command for either a QFull condition
5344 * (ie reply SAM_STAT_BUSY) or to terminate an exchange that did not go
5348 qlt_alloc_qfull_cmd(struct scsi_qla_host *vha,
5349 struct atio_from_isp *atio, uint16_t status, int qfull)
5351 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
5352 struct qla_hw_data *ha = vha->hw;
5353 struct fc_port *sess;
5354 struct qla_tgt_cmd *cmd;
5355 unsigned long flags;
5357 if (unlikely(tgt->tgt_stop)) {
5358 ql_dbg(ql_dbg_io, vha, 0x300a,
5359 "New command while device %p is shutting down\n", tgt);
5363 if ((vha->hw->tgt.num_qfull_cmds_alloc + 1) > MAX_QFULL_CMDS_ALLOC) {
5364 vha->hw->tgt.num_qfull_cmds_dropped++;
5365 if (vha->hw->tgt.num_qfull_cmds_dropped >
5366 vha->qla_stats.stat_max_qfull_cmds_dropped)
5367 vha->qla_stats.stat_max_qfull_cmds_dropped =
5368 vha->hw->tgt.num_qfull_cmds_dropped;
5370 ql_dbg(ql_dbg_io, vha, 0x3068,
5371 "qla_target(%d): %s: QFull CMD dropped[%d]\n",
5372 vha->vp_idx, __func__,
5373 vha->hw->tgt.num_qfull_cmds_dropped);
5375 qlt_chk_exch_leak_thresh_hold(vha);
5379 sess = ha->tgt.tgt_ops->find_sess_by_s_id
5380 (vha, atio->u.isp24.fcp_hdr.s_id);
5384 cmd = ha->tgt.tgt_ops->get_cmd(sess);
5386 ql_dbg(ql_dbg_io, vha, 0x3009,
5387 "qla_target(%d): %s: Allocation of cmd failed\n",
5388 vha->vp_idx, __func__);
5390 vha->hw->tgt.num_qfull_cmds_dropped++;
5391 if (vha->hw->tgt.num_qfull_cmds_dropped >
5392 vha->qla_stats.stat_max_qfull_cmds_dropped)
5393 vha->qla_stats.stat_max_qfull_cmds_dropped =
5394 vha->hw->tgt.num_qfull_cmds_dropped;
5396 qlt_chk_exch_leak_thresh_hold(vha);
5400 qlt_incr_num_pend_cmds(vha);
5401 INIT_LIST_HEAD(&cmd->cmd_list);
5402 memcpy(&cmd->atio, atio, sizeof(*atio));
5404 cmd->tgt = vha->vha_tgt.qla_tgt;
5406 cmd->reset_count = ha->base_qpair->chip_reset;
5408 cmd->qpair = ha->base_qpair;
5412 /* NOTE: borrowing the state field to carry the status */
5413 cmd->state = status;
5415 cmd->term_exchg = 1;
5417 spin_lock_irqsave(&vha->hw->tgt.q_full_lock, flags);
5418 list_add_tail(&cmd->cmd_list, &vha->hw->tgt.q_full_list);
5420 vha->hw->tgt.num_qfull_cmds_alloc++;
5421 if (vha->hw->tgt.num_qfull_cmds_alloc >
5422 vha->qla_stats.stat_max_qfull_cmds_alloc)
5423 vha->qla_stats.stat_max_qfull_cmds_alloc =
5424 vha->hw->tgt.num_qfull_cmds_alloc;
5425 spin_unlock_irqrestore(&vha->hw->tgt.q_full_lock, flags);
5429 qlt_free_qfull_cmds(struct qla_qpair *qpair)
5431 struct scsi_qla_host *vha = qpair->vha;
5432 struct qla_hw_data *ha = vha->hw;
5433 unsigned long flags;
5434 struct qla_tgt_cmd *cmd, *tcmd;
5435 struct list_head free_list, q_full_list;
5438 if (list_empty(&ha->tgt.q_full_list))
5441 INIT_LIST_HEAD(&free_list);
5442 INIT_LIST_HEAD(&q_full_list);
5444 spin_lock_irqsave(&vha->hw->tgt.q_full_lock, flags);
5445 if (list_empty(&ha->tgt.q_full_list)) {
5446 spin_unlock_irqrestore(&vha->hw->tgt.q_full_lock, flags);
5450 list_splice_init(&vha->hw->tgt.q_full_list, &q_full_list);
5451 spin_unlock_irqrestore(&vha->hw->tgt.q_full_lock, flags);
5453 spin_lock_irqsave(qpair->qp_lock_ptr, flags);
5454 list_for_each_entry_safe(cmd, tcmd, &q_full_list, cmd_list) {
5456 /* cmd->state is a borrowed field to hold status */
5457 rc = __qlt_send_busy(qpair, &cmd->atio, cmd->state);
5458 else if (cmd->term_exchg)
5459 rc = __qlt_send_term_exchange(qpair, NULL, &cmd->atio);
5465 ql_dbg(ql_dbg_io, vha, 0x3006,
5466 "%s: busy sent for ox_id[%04x]\n", __func__,
5467 be16_to_cpu(cmd->atio.u.isp24.fcp_hdr.ox_id));
5468 else if (cmd->term_exchg)
5469 ql_dbg(ql_dbg_io, vha, 0x3007,
5470 "%s: Term exchg sent for ox_id[%04x]\n", __func__,
5471 be16_to_cpu(cmd->atio.u.isp24.fcp_hdr.ox_id));
5473 ql_dbg(ql_dbg_io, vha, 0x3008,
5474 "%s: Unexpected cmd in QFull list %p\n", __func__,
5477 list_del(&cmd->cmd_list);
5478 list_add_tail(&cmd->cmd_list, &free_list);
5480 /* piggy back on hardware_lock for protection */
5481 vha->hw->tgt.num_qfull_cmds_alloc--;
5483 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
5487 list_for_each_entry_safe(cmd, tcmd, &free_list, cmd_list) {
5488 list_del(&cmd->cmd_list);
5489 /* This cmd was never sent to TCM. There is no need
5490 * to schedule free or call free_cmd
5495 if (!list_empty(&q_full_list)) {
5496 spin_lock_irqsave(&vha->hw->tgt.q_full_lock, flags);
5497 list_splice(&q_full_list, &vha->hw->tgt.q_full_list);
5498 spin_unlock_irqrestore(&vha->hw->tgt.q_full_lock, flags);
5505 qlt_send_busy(struct qla_qpair *qpair, struct atio_from_isp *atio,
5509 struct scsi_qla_host *vha = qpair->vha;
5511 rc = __qlt_send_busy(qpair, atio, status);
5513 qlt_alloc_qfull_cmd(vha, atio, status, 1);
5517 qlt_chk_qfull_thresh_hold(struct scsi_qla_host *vha, struct qla_qpair *qpair,
5518 struct atio_from_isp *atio, uint8_t ha_locked)
5520 struct qla_hw_data *ha = vha->hw;
5521 unsigned long flags;
5523 if (ha->tgt.num_pend_cmds < Q_FULL_THRESH_HOLD(ha))
5527 spin_lock_irqsave(&ha->hardware_lock, flags);
5528 qlt_send_busy(qpair, atio, qla_sam_status);
5530 spin_unlock_irqrestore(&ha->hardware_lock, flags);
5535 /* ha->hardware_lock supposed to be held on entry */
5536 /* called via callback from qla2xxx */
5537 static void qlt_24xx_atio_pkt(struct scsi_qla_host *vha,
5538 struct atio_from_isp *atio, uint8_t ha_locked)
5540 struct qla_hw_data *ha = vha->hw;
5541 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
5543 unsigned long flags = 0;
5545 if (unlikely(tgt == NULL)) {
5546 ql_dbg(ql_dbg_tgt, vha, 0x3064,
5547 "ATIO pkt, but no tgt (ha %p)", ha);
5551 * In tgt_stop mode we also should allow all requests to pass.
5552 * Otherwise, some commands can stuck.
5555 tgt->atio_irq_cmd_count++;
5557 switch (atio->u.raw.entry_type) {
5559 if (unlikely(atio->u.isp24.exchange_addr ==
5560 cpu_to_le32(ATIO_EXCHANGE_ADDRESS_UNKNOWN))) {
5561 ql_dbg(ql_dbg_io, vha, 0x3065,
5562 "qla_target(%d): ATIO_TYPE7 "
5563 "received with UNKNOWN exchange address, "
5564 "sending QUEUE_FULL\n", vha->vp_idx);
5566 spin_lock_irqsave(&ha->hardware_lock, flags);
5567 qlt_send_busy(ha->base_qpair, atio, qla_sam_status);
5569 spin_unlock_irqrestore(&ha->hardware_lock,
5574 if (likely(atio->u.isp24.fcp_cmnd.task_mgmt_flags == 0)) {
5575 rc = qlt_chk_qfull_thresh_hold(vha, ha->base_qpair,
5578 tgt->atio_irq_cmd_count--;
5581 rc = qlt_handle_cmd_for_atio(vha, atio);
5583 rc = qlt_handle_task_mgmt(vha, atio);
5585 if (unlikely(rc != 0)) {
5587 spin_lock_irqsave(&ha->hardware_lock, flags);
5590 ql_dbg(ql_dbg_tgt, vha, 0xe05f,
5591 "qla_target: Unable to send command to target\n");
5594 ql_dbg(ql_dbg_tgt, vha, 0xe05f,
5595 "qla_target: Unable to send command to target, sending TERM EXCHANGE for rsp\n");
5596 qlt_send_term_exchange(ha->base_qpair, NULL,
5600 ql_dbg(ql_dbg_tgt, vha, 0xe060,
5601 "qla_target(%d): Unable to send command to target, sending BUSY status\n",
5603 qlt_send_busy(ha->base_qpair, atio,
5607 ql_dbg(ql_dbg_tgt, vha, 0xe060,
5608 "qla_target(%d): Unable to send command to target, sending BUSY status\n",
5610 qlt_send_busy(ha->base_qpair, atio,
5615 spin_unlock_irqrestore(&ha->hardware_lock,
5620 case IMMED_NOTIFY_TYPE:
5622 if (unlikely(atio->u.isp2x.entry_status != 0)) {
5623 ql_dbg(ql_dbg_tgt, vha, 0xe05b,
5624 "qla_target(%d): Received ATIO packet %x "
5625 "with error status %x\n", vha->vp_idx,
5626 atio->u.raw.entry_type,
5627 atio->u.isp2x.entry_status);
5630 ql_dbg(ql_dbg_tgt, vha, 0xe02e, "%s", "IMMED_NOTIFY ATIO");
5633 spin_lock_irqsave(&ha->hardware_lock, flags);
5634 qlt_handle_imm_notify(vha, (struct imm_ntfy_from_isp *)atio);
5636 spin_unlock_irqrestore(&ha->hardware_lock, flags);
5641 ql_dbg(ql_dbg_tgt, vha, 0xe05c,
5642 "qla_target(%d): Received unknown ATIO atio "
5643 "type %x\n", vha->vp_idx, atio->u.raw.entry_type);
5647 tgt->atio_irq_cmd_count--;
5651 * qpair lock is assume to be held
5652 * rc = 0 : send terminate & abts respond
5653 * rc != 0: do not send term & abts respond
5655 static int qlt_chk_unresolv_exchg(struct scsi_qla_host *vha,
5656 struct qla_qpair *qpair, struct abts_resp_from_24xx_fw *entry)
5658 struct qla_hw_data *ha = vha->hw;
5662 * Detect unresolved exchange. If the same ABTS is unable
5663 * to terminate an existing command and the same ABTS loops
5664 * between FW & Driver, then force FW dump. Under 1 jiff,
5665 * we should see multiple loops.
5667 if (qpair->retry_term_exchg_addr == entry->exchange_addr_to_abort &&
5668 qpair->retry_term_jiff == jiffies) {
5669 /* found existing exchange */
5670 qpair->retry_term_cnt++;
5671 if (qpair->retry_term_cnt >= 5) {
5673 qpair->retry_term_cnt = 0;
5674 ql_log(ql_log_warn, vha, 0xffff,
5675 "Unable to send ABTS Respond. Dumping firmware.\n");
5676 ql_dump_buffer(ql_dbg_tgt_mgt + ql_dbg_buffer,
5677 vha, 0xffff, (uint8_t *)entry, sizeof(*entry));
5679 if (qpair == ha->base_qpair)
5680 ha->isp_ops->fw_dump(vha);
5682 qla2xxx_dump_fw(vha);
5684 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
5685 qla2xxx_wake_dpc(vha);
5687 } else if (qpair->retry_term_jiff != jiffies) {
5688 qpair->retry_term_exchg_addr = entry->exchange_addr_to_abort;
5689 qpair->retry_term_cnt = 0;
5690 qpair->retry_term_jiff = jiffies;
5697 static void qlt_handle_abts_completion(struct scsi_qla_host *vha,
5698 struct rsp_que *rsp, response_t *pkt)
5700 struct abts_resp_from_24xx_fw *entry =
5701 (struct abts_resp_from_24xx_fw *)pkt;
5702 u32 h = pkt->handle & ~QLA_TGT_HANDLE_MASK;
5703 struct qla_tgt_mgmt_cmd *mcmd;
5704 struct qla_hw_data *ha = vha->hw;
5706 mcmd = qlt_ctio_to_cmd(vha, rsp, pkt->handle, pkt);
5707 if (mcmd == NULL && h != QLA_TGT_SKIP_HANDLE) {
5708 ql_dbg(ql_dbg_async, vha, 0xe064,
5709 "qla_target(%d): ABTS Comp without mcmd\n",
5716 vha->vha_tgt.qla_tgt->abts_resp_expected--;
5718 ql_dbg(ql_dbg_tgt, vha, 0xe038,
5719 "ABTS_RESP_24XX: compl_status %x\n",
5720 entry->compl_status);
5722 if (le16_to_cpu(entry->compl_status) != ABTS_RESP_COMPL_SUCCESS) {
5723 if (le32_to_cpu(entry->error_subcode1) == 0x1E &&
5724 le32_to_cpu(entry->error_subcode2) == 0) {
5725 if (qlt_chk_unresolv_exchg(vha, rsp->qpair, entry)) {
5726 ha->tgt.tgt_ops->free_mcmd(mcmd);
5729 qlt_24xx_retry_term_exchange(vha, rsp->qpair,
5732 ql_dbg(ql_dbg_tgt, vha, 0xe063,
5733 "qla_target(%d): ABTS_RESP_24XX failed %x (subcode %x:%x)",
5734 vha->vp_idx, entry->compl_status,
5735 entry->error_subcode1,
5736 entry->error_subcode2);
5737 ha->tgt.tgt_ops->free_mcmd(mcmd);
5740 ha->tgt.tgt_ops->free_mcmd(mcmd);
5744 /* ha->hardware_lock supposed to be held on entry */
5745 /* called via callback from qla2xxx */
5746 static void qlt_response_pkt(struct scsi_qla_host *vha,
5747 struct rsp_que *rsp, response_t *pkt)
5749 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
5751 if (unlikely(tgt == NULL)) {
5752 ql_dbg(ql_dbg_tgt, vha, 0xe05d,
5753 "qla_target(%d): Response pkt %x received, but no tgt (ha %p)\n",
5754 vha->vp_idx, pkt->entry_type, vha->hw);
5759 * In tgt_stop mode we also should allow all requests to pass.
5760 * Otherwise, some commands can stuck.
5763 switch (pkt->entry_type) {
5767 struct ctio7_from_24xx *entry = (struct ctio7_from_24xx *)pkt;
5769 qlt_do_ctio_completion(vha, rsp, entry->handle,
5770 le16_to_cpu(entry->status)|(pkt->entry_status << 16),
5775 case ACCEPT_TGT_IO_TYPE:
5777 struct atio_from_isp *atio = (struct atio_from_isp *)pkt;
5780 if (atio->u.isp2x.status !=
5781 cpu_to_le16(ATIO_CDB_VALID)) {
5782 ql_dbg(ql_dbg_tgt, vha, 0xe05e,
5783 "qla_target(%d): ATIO with error "
5784 "status %x received\n", vha->vp_idx,
5785 le16_to_cpu(atio->u.isp2x.status));
5789 rc = qlt_chk_qfull_thresh_hold(vha, rsp->qpair, atio, 1);
5793 rc = qlt_handle_cmd_for_atio(vha, atio);
5794 if (unlikely(rc != 0)) {
5797 ql_dbg(ql_dbg_tgt, vha, 0xe05f,
5798 "qla_target: Unable to send command to target\n");
5801 ql_dbg(ql_dbg_tgt, vha, 0xe05f,
5802 "qla_target: Unable to send command to target, sending TERM EXCHANGE for rsp\n");
5803 qlt_send_term_exchange(rsp->qpair, NULL,
5807 ql_dbg(ql_dbg_tgt, vha, 0xe060,
5808 "qla_target(%d): Unable to send command to target, sending BUSY status\n",
5810 qlt_send_busy(rsp->qpair, atio,
5814 ql_dbg(ql_dbg_tgt, vha, 0xe060,
5815 "qla_target(%d): Unable to send command to target, sending BUSY status\n",
5817 qlt_send_busy(rsp->qpair, atio,
5825 case CONTINUE_TGT_IO_TYPE:
5827 struct ctio_to_2xxx *entry = (struct ctio_to_2xxx *)pkt;
5829 qlt_do_ctio_completion(vha, rsp, entry->handle,
5830 le16_to_cpu(entry->status)|(pkt->entry_status << 16),
5837 struct ctio_to_2xxx *entry = (struct ctio_to_2xxx *)pkt;
5839 qlt_do_ctio_completion(vha, rsp, entry->handle,
5840 le16_to_cpu(entry->status)|(pkt->entry_status << 16),
5845 case IMMED_NOTIFY_TYPE:
5846 ql_dbg(ql_dbg_tgt, vha, 0xe035, "%s", "IMMED_NOTIFY\n");
5847 qlt_handle_imm_notify(vha, (struct imm_ntfy_from_isp *)pkt);
5850 case NOTIFY_ACK_TYPE:
5851 if (tgt->notify_ack_expected > 0) {
5852 struct nack_to_isp *entry = (struct nack_to_isp *)pkt;
5854 ql_dbg(ql_dbg_tgt, vha, 0xe036,
5855 "NOTIFY_ACK seq %08x status %x\n",
5856 le16_to_cpu(entry->u.isp2x.seq_id),
5857 le16_to_cpu(entry->u.isp2x.status));
5858 tgt->notify_ack_expected--;
5859 if (entry->u.isp2x.status !=
5860 cpu_to_le16(NOTIFY_ACK_SUCCESS)) {
5861 ql_dbg(ql_dbg_tgt, vha, 0xe061,
5862 "qla_target(%d): NOTIFY_ACK "
5863 "failed %x\n", vha->vp_idx,
5864 le16_to_cpu(entry->u.isp2x.status));
5867 ql_dbg(ql_dbg_tgt, vha, 0xe062,
5868 "qla_target(%d): Unexpected NOTIFY_ACK received\n",
5873 case ABTS_RECV_24XX:
5874 ql_dbg(ql_dbg_tgt, vha, 0xe037,
5875 "ABTS_RECV_24XX: instance %d\n", vha->vp_idx);
5876 qlt_24xx_handle_abts(vha, (struct abts_recv_from_24xx *)pkt);
5879 case ABTS_RESP_24XX:
5880 if (tgt->abts_resp_expected > 0) {
5881 qlt_handle_abts_completion(vha, rsp, pkt);
5883 ql_dbg(ql_dbg_tgt, vha, 0xe064,
5884 "qla_target(%d): Unexpected ABTS_RESP_24XX "
5885 "received\n", vha->vp_idx);
5890 ql_dbg(ql_dbg_tgt, vha, 0xe065,
5891 "qla_target(%d): Received unknown response pkt "
5892 "type %x\n", vha->vp_idx, pkt->entry_type);
5899 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
5901 void qlt_async_event(uint16_t code, struct scsi_qla_host *vha,
5904 struct qla_hw_data *ha = vha->hw;
5905 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
5908 if (!tgt || tgt->tgt_stop || tgt->tgt_stopped)
5911 if (((code == MBA_POINT_TO_POINT) || (code == MBA_CHG_IN_CONNECTION)) &&
5915 * In tgt_stop mode we also should allow all requests to pass.
5916 * Otherwise, some commands can stuck.
5921 case MBA_RESET: /* Reset */
5922 case MBA_SYSTEM_ERR: /* System Error */
5923 case MBA_REQ_TRANSFER_ERR: /* Request Transfer Error */
5924 case MBA_RSP_TRANSFER_ERR: /* Response Transfer Error */
5925 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03a,
5926 "qla_target(%d): System error async event %#x "
5927 "occurred", vha->vp_idx, code);
5929 case MBA_WAKEUP_THRES: /* Request Queue Wake-up. */
5930 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
5935 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03b,
5936 "qla_target(%d): Async LOOP_UP occurred "
5937 "(m[0]=%x, m[1]=%x, m[2]=%x, m[3]=%x)", vha->vp_idx,
5938 mailbox[0], mailbox[1], mailbox[2], mailbox[3]);
5939 if (tgt->link_reinit_iocb_pending) {
5940 qlt_send_notify_ack(ha->base_qpair,
5941 &tgt->link_reinit_iocb,
5943 tgt->link_reinit_iocb_pending = 0;
5948 case MBA_LIP_OCCURRED:
5951 case MBA_RSCN_UPDATE:
5952 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03c,
5953 "qla_target(%d): Async event %#x occurred "
5954 "(m[0]=%x, m[1]=%x, m[2]=%x, m[3]=%x)", vha->vp_idx, code,
5955 mailbox[0], mailbox[1], mailbox[2], mailbox[3]);
5958 case MBA_REJECTED_FCP_CMD:
5959 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf017,
5960 "qla_target(%d): Async event LS_REJECT occurred (m[0]=%x, m[1]=%x, m[2]=%x, m[3]=%x)",
5962 mailbox[0], mailbox[1], mailbox[2], mailbox[3]);
5964 if (mailbox[3] == 1) {
5965 /* exchange starvation. */
5966 vha->hw->exch_starvation++;
5967 if (vha->hw->exch_starvation > 5) {
5968 ql_log(ql_log_warn, vha, 0xd03a,
5969 "Exchange starvation-. Resetting RISC\n");
5971 vha->hw->exch_starvation = 0;
5972 if (IS_P3P_TYPE(vha->hw))
5973 set_bit(FCOE_CTX_RESET_NEEDED,
5976 set_bit(ISP_ABORT_NEEDED,
5978 qla2xxx_wake_dpc(vha);
5983 case MBA_PORT_UPDATE:
5984 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03d,
5985 "qla_target(%d): Port update async event %#x "
5986 "occurred: updating the ports database (m[0]=%x, m[1]=%x, "
5987 "m[2]=%x, m[3]=%x)", vha->vp_idx, code,
5988 mailbox[0], mailbox[1], mailbox[2], mailbox[3]);
5990 login_code = mailbox[2];
5991 if (login_code == 0x4) {
5992 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03e,
5993 "Async MB 2: Got PLOGI Complete\n");
5994 vha->hw->exch_starvation = 0;
5995 } else if (login_code == 0x7)
5996 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03f,
5997 "Async MB 2: Port Logged Out\n");
6005 static fc_port_t *qlt_get_port_database(struct scsi_qla_host *vha,
6008 fc_port_t *fcport, *tfcp, *del;
6010 unsigned long flags;
6013 fcport = qla2x00_alloc_fcport(vha, GFP_KERNEL);
6015 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06f,
6016 "qla_target(%d): Allocation of tmp FC port failed",
6021 fcport->loop_id = loop_id;
6023 rc = qla24xx_gpdb_wait(vha, fcport, 0);
6024 if (rc != QLA_SUCCESS) {
6025 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf070,
6026 "qla_target(%d): Failed to retrieve fcport "
6027 "information -- get_port_database() returned %x "
6028 "(loop_id=0x%04x)", vha->vp_idx, rc, loop_id);
6034 spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
6035 tfcp = qla2x00_find_fcport_by_wwpn(vha, fcport->port_name, 1);
6038 tfcp->d_id = fcport->d_id;
6039 tfcp->port_type = fcport->port_type;
6040 tfcp->supported_classes = fcport->supported_classes;
6041 tfcp->flags |= fcport->flags;
6042 tfcp->scan_state = QLA_FCPORT_FOUND;
6047 if (vha->hw->current_topology == ISP_CFG_F)
6048 fcport->flags |= FCF_FABRIC_DEVICE;
6050 list_add_tail(&fcport->list, &vha->vp_fcports);
6051 if (!IS_SW_RESV_ADDR(fcport->d_id))
6052 vha->fcport_count++;
6053 fcport->login_gen++;
6054 qla2x00_set_fcport_disc_state(fcport, DSC_LOGIN_COMPLETE);
6055 fcport->login_succ = 1;
6059 fcport->deleted = 0;
6060 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
6062 switch (vha->host->active_mode) {
6063 case MODE_INITIATOR:
6066 if (!IS_IIDMA_CAPABLE(vha->hw) || !vha->hw->flags.gpsc_supported) {
6067 qla24xx_sched_upd_fcport(fcport);
6069 ql_dbg(ql_dbg_disc, vha, 0x20ff,
6070 "%s %d %8phC post gpsc fcp_cnt %d\n",
6071 __func__, __LINE__, fcport->port_name, vha->fcport_count);
6072 qla24xx_post_gpsc_work(vha, fcport);
6082 qla2x00_free_fcport(del);
6087 /* Must be called under tgt_mutex */
6088 static struct fc_port *qlt_make_local_sess(struct scsi_qla_host *vha,
6091 struct fc_port *sess = NULL;
6092 fc_port_t *fcport = NULL;
6093 int rc, global_resets;
6094 uint16_t loop_id = 0;
6096 if (s_id.domain == 0xFF && s_id.area == 0xFC) {
6098 * This is Domain Controller, so it should be
6099 * OK to drop SCSI commands from it.
6101 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf042,
6102 "Unable to find initiator with S_ID %x:%x:%x",
6103 s_id.domain, s_id.area, s_id.al_pa);
6107 mutex_lock(&vha->vha_tgt.tgt_mutex);
6111 atomic_read(&vha->vha_tgt.qla_tgt->tgt_global_resets_count);
6113 rc = qla24xx_get_loop_id(vha, s_id, &loop_id);
6115 mutex_unlock(&vha->vha_tgt.tgt_mutex);
6117 ql_log(ql_log_info, vha, 0xf071,
6118 "qla_target(%d): Unable to find "
6119 "initiator with S_ID %x:%x:%x",
6120 vha->vp_idx, s_id.domain, s_id.area, s_id.al_pa);
6122 if (rc == -ENOENT) {
6123 qlt_port_logo_t logo;
6125 logo.id = be_to_port_id(s_id);
6127 qlt_send_first_logo(vha, &logo);
6133 fcport = qlt_get_port_database(vha, loop_id);
6135 mutex_unlock(&vha->vha_tgt.tgt_mutex);
6139 if (global_resets !=
6140 atomic_read(&vha->vha_tgt.qla_tgt->tgt_global_resets_count)) {
6141 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf043,
6142 "qla_target(%d): global reset during session discovery "
6143 "(counter was %d, new %d), retrying", vha->vp_idx,
6145 atomic_read(&vha->vha_tgt.
6146 qla_tgt->tgt_global_resets_count));
6150 sess = qlt_create_sess(vha, fcport, true);
6152 mutex_unlock(&vha->vha_tgt.tgt_mutex);
6157 static void qlt_abort_work(struct qla_tgt *tgt,
6158 struct qla_tgt_sess_work_param *prm)
6160 struct scsi_qla_host *vha = tgt->vha;
6161 struct qla_hw_data *ha = vha->hw;
6162 struct fc_port *sess = NULL;
6163 unsigned long flags = 0, flags2 = 0;
6167 spin_lock_irqsave(&ha->tgt.sess_lock, flags2);
6172 s_id = le_id_to_be(prm->abts.fcp_hdr_le.s_id);
6174 sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha, s_id);
6176 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags2);
6178 sess = qlt_make_local_sess(vha, s_id);
6179 /* sess has got an extra creation ref */
6181 spin_lock_irqsave(&ha->tgt.sess_lock, flags2);
6185 if (sess->deleted) {
6190 if (!kref_get_unless_zero(&sess->sess_kref)) {
6191 ql_dbg(ql_dbg_tgt_tmr, vha, 0xf01c,
6192 "%s: kref_get fail %8phC \n",
6193 __func__, sess->port_name);
6199 rc = __qlt_24xx_handle_abts(vha, &prm->abts, sess);
6200 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags2);
6202 ha->tgt.tgt_ops->put_sess(sess);
6209 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags2);
6212 spin_lock_irqsave(&ha->hardware_lock, flags);
6213 qlt_24xx_send_abts_resp(ha->base_qpair, &prm->abts,
6214 FCP_TMF_REJECTED, false);
6215 spin_unlock_irqrestore(&ha->hardware_lock, flags);
6218 static void qlt_tmr_work(struct qla_tgt *tgt,
6219 struct qla_tgt_sess_work_param *prm)
6221 struct atio_from_isp *a = &prm->tm_iocb2;
6222 struct scsi_qla_host *vha = tgt->vha;
6223 struct qla_hw_data *ha = vha->hw;
6224 struct fc_port *sess;
6225 unsigned long flags;
6232 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
6237 s_id = prm->tm_iocb2.u.isp24.fcp_hdr.s_id;
6238 sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha, s_id);
6240 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
6242 sess = qlt_make_local_sess(vha, s_id);
6243 /* sess has got an extra creation ref */
6245 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
6249 if (sess->deleted) {
6253 if (!kref_get_unless_zero(&sess->sess_kref)) {
6254 ql_dbg(ql_dbg_tgt_tmr, vha, 0xf020,
6255 "%s: kref_get fail %8phC\n",
6256 __func__, sess->port_name);
6262 fn = a->u.isp24.fcp_cmnd.task_mgmt_flags;
6264 scsilun_to_int((struct scsi_lun *)&a->u.isp24.fcp_cmnd.lun);
6266 rc = qlt_issue_task_mgmt(sess, unpacked_lun, fn, iocb, 0);
6267 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
6269 ha->tgt.tgt_ops->put_sess(sess);
6276 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
6278 qlt_send_term_exchange(ha->base_qpair, NULL, &prm->tm_iocb2, 1, 0);
6281 static void qlt_sess_work_fn(struct work_struct *work)
6283 struct qla_tgt *tgt = container_of(work, struct qla_tgt, sess_work);
6284 struct scsi_qla_host *vha = tgt->vha;
6285 unsigned long flags;
6287 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf000, "Sess work (tgt %p)", tgt);
6289 spin_lock_irqsave(&tgt->sess_work_lock, flags);
6290 while (!list_empty(&tgt->sess_works_list)) {
6291 struct qla_tgt_sess_work_param *prm = list_entry(
6292 tgt->sess_works_list.next, typeof(*prm),
6293 sess_works_list_entry);
6296 * This work can be scheduled on several CPUs at time, so we
6297 * must delete the entry to eliminate double processing
6299 list_del(&prm->sess_works_list_entry);
6301 spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
6303 switch (prm->type) {
6304 case QLA_TGT_SESS_WORK_ABORT:
6305 qlt_abort_work(tgt, prm);
6307 case QLA_TGT_SESS_WORK_TM:
6308 qlt_tmr_work(tgt, prm);
6315 spin_lock_irqsave(&tgt->sess_work_lock, flags);
6319 spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
6322 /* Must be called under tgt_host_action_mutex */
6323 int qlt_add_target(struct qla_hw_data *ha, struct scsi_qla_host *base_vha)
6325 struct qla_tgt *tgt;
6327 struct qla_qpair_hint *h;
6329 if (!QLA_TGT_MODE_ENABLED())
6332 if (!IS_TGT_MODE_CAPABLE(ha)) {
6333 ql_log(ql_log_warn, base_vha, 0xe070,
6334 "This adapter does not support target mode.\n");
6338 ql_dbg(ql_dbg_tgt, base_vha, 0xe03b,
6339 "Registering target for host %ld(%p).\n", base_vha->host_no, ha);
6341 BUG_ON(base_vha->vha_tgt.qla_tgt != NULL);
6343 tgt = kzalloc(sizeof(struct qla_tgt), GFP_KERNEL);
6345 ql_dbg(ql_dbg_tgt, base_vha, 0xe066,
6346 "Unable to allocate struct qla_tgt\n");
6350 tgt->qphints = kcalloc(ha->max_qpairs + 1,
6351 sizeof(struct qla_qpair_hint),
6353 if (!tgt->qphints) {
6355 ql_log(ql_log_warn, base_vha, 0x0197,
6356 "Unable to allocate qpair hints.\n");
6360 if (!(base_vha->host->hostt->supported_mode & MODE_TARGET))
6361 base_vha->host->hostt->supported_mode |= MODE_TARGET;
6363 rc = btree_init64(&tgt->lun_qpair_map);
6365 kfree(tgt->qphints);
6367 ql_log(ql_log_info, base_vha, 0x0198,
6368 "Unable to initialize lun_qpair_map btree\n");
6371 h = &tgt->qphints[0];
6372 h->qpair = ha->base_qpair;
6373 INIT_LIST_HEAD(&h->hint_elem);
6374 h->cpuid = ha->base_qpair->cpuid;
6375 list_add_tail(&h->hint_elem, &ha->base_qpair->hints_list);
6377 for (i = 0; i < ha->max_qpairs; i++) {
6378 unsigned long flags;
6380 struct qla_qpair *qpair = ha->queue_pair_map[i];
6382 h = &tgt->qphints[i + 1];
6383 INIT_LIST_HEAD(&h->hint_elem);
6386 spin_lock_irqsave(qpair->qp_lock_ptr, flags);
6387 list_add_tail(&h->hint_elem, &qpair->hints_list);
6388 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
6389 h->cpuid = qpair->cpuid;
6394 tgt->vha = base_vha;
6395 init_waitqueue_head(&tgt->waitQ);
6396 INIT_LIST_HEAD(&tgt->del_sess_list);
6397 spin_lock_init(&tgt->sess_work_lock);
6398 INIT_WORK(&tgt->sess_work, qlt_sess_work_fn);
6399 INIT_LIST_HEAD(&tgt->sess_works_list);
6400 atomic_set(&tgt->tgt_global_resets_count, 0);
6402 base_vha->vha_tgt.qla_tgt = tgt;
6404 ql_dbg(ql_dbg_tgt, base_vha, 0xe067,
6405 "qla_target(%d): using 64 Bit PCI addressing",
6408 tgt->sg_tablesize = QLA_TGT_MAX_SG_24XX(base_vha->req->length - 3);
6410 mutex_lock(&qla_tgt_mutex);
6411 list_add_tail(&tgt->tgt_list_entry, &qla_tgt_glist);
6412 mutex_unlock(&qla_tgt_mutex);
6414 if (ha->tgt.tgt_ops && ha->tgt.tgt_ops->add_target)
6415 ha->tgt.tgt_ops->add_target(base_vha);
6420 /* Must be called under tgt_host_action_mutex */
6421 int qlt_remove_target(struct qla_hw_data *ha, struct scsi_qla_host *vha)
6423 if (!vha->vha_tgt.qla_tgt)
6426 if (vha->fc_vport) {
6427 qlt_release(vha->vha_tgt.qla_tgt);
6431 /* free left over qfull cmds */
6432 qlt_init_term_exchange(vha);
6434 ql_dbg(ql_dbg_tgt, vha, 0xe03c, "Unregistering target for host %ld(%p)",
6436 qlt_release(vha->vha_tgt.qla_tgt);
6441 void qlt_remove_target_resources(struct qla_hw_data *ha)
6443 struct scsi_qla_host *node;
6446 btree_for_each_safe32(&ha->tgt.host_map, key, node)
6447 btree_remove32(&ha->tgt.host_map, key);
6449 btree_destroy32(&ha->tgt.host_map);
6452 static void qlt_lport_dump(struct scsi_qla_host *vha, u64 wwpn,
6455 pr_debug("qla2xxx HW vha->node_name: %8phC\n", vha->node_name);
6456 pr_debug("qla2xxx HW vha->port_name: %8phC\n", vha->port_name);
6457 put_unaligned_be64(wwpn, b);
6458 pr_debug("qla2xxx passed configfs WWPN: %8phC\n", b);
6462 * qla_tgt_lport_register - register lport with external module
6464 * @target_lport_ptr: pointer for tcm_qla2xxx specific lport data
6465 * @phys_wwpn: physical port WWPN
6466 * @npiv_wwpn: NPIV WWPN
6467 * @npiv_wwnn: NPIV WWNN
6468 * @callback: lport initialization callback for tcm_qla2xxx code
6470 int qlt_lport_register(void *target_lport_ptr, u64 phys_wwpn,
6471 u64 npiv_wwpn, u64 npiv_wwnn,
6472 int (*callback)(struct scsi_qla_host *, void *, u64, u64))
6474 struct qla_tgt *tgt;
6475 struct scsi_qla_host *vha;
6476 struct qla_hw_data *ha;
6477 struct Scsi_Host *host;
6478 unsigned long flags;
6482 mutex_lock(&qla_tgt_mutex);
6483 list_for_each_entry(tgt, &qla_tgt_glist, tgt_list_entry) {
6491 if (!(host->hostt->supported_mode & MODE_TARGET))
6494 if (vha->qlini_mode == QLA2XXX_INI_MODE_ENABLED)
6497 spin_lock_irqsave(&ha->hardware_lock, flags);
6498 if ((!npiv_wwpn || !npiv_wwnn) && host->active_mode & MODE_TARGET) {
6499 pr_debug("MODE_TARGET already active on qla2xxx(%d)\n",
6501 spin_unlock_irqrestore(&ha->hardware_lock, flags);
6504 if (tgt->tgt_stop) {
6505 pr_debug("MODE_TARGET in shutdown on qla2xxx(%d)\n",
6507 spin_unlock_irqrestore(&ha->hardware_lock, flags);
6510 spin_unlock_irqrestore(&ha->hardware_lock, flags);
6512 if (!scsi_host_get(host)) {
6513 ql_dbg(ql_dbg_tgt, vha, 0xe068,
6514 "Unable to scsi_host_get() for"
6515 " qla2xxx scsi_host\n");
6518 qlt_lport_dump(vha, phys_wwpn, b);
6520 if (memcmp(vha->port_name, b, WWN_SIZE)) {
6521 scsi_host_put(host);
6524 rc = (*callback)(vha, target_lport_ptr, npiv_wwpn, npiv_wwnn);
6526 scsi_host_put(host);
6528 mutex_unlock(&qla_tgt_mutex);
6531 mutex_unlock(&qla_tgt_mutex);
6535 EXPORT_SYMBOL(qlt_lport_register);
6538 * qla_tgt_lport_deregister - Degister lport
6540 * @vha: Registered scsi_qla_host pointer
6542 void qlt_lport_deregister(struct scsi_qla_host *vha)
6544 struct qla_hw_data *ha = vha->hw;
6545 struct Scsi_Host *sh = vha->host;
6547 * Clear the target_lport_ptr qla_target_template pointer in qla_hw_data
6549 vha->vha_tgt.target_lport_ptr = NULL;
6550 ha->tgt.tgt_ops = NULL;
6552 * Release the Scsi_Host reference for the underlying qla2xxx host
6556 EXPORT_SYMBOL(qlt_lport_deregister);
6558 /* Must be called under HW lock */
6559 void qlt_set_mode(struct scsi_qla_host *vha)
6561 switch (vha->qlini_mode) {
6562 case QLA2XXX_INI_MODE_DISABLED:
6563 case QLA2XXX_INI_MODE_EXCLUSIVE:
6564 vha->host->active_mode = MODE_TARGET;
6566 case QLA2XXX_INI_MODE_ENABLED:
6567 vha->host->active_mode = MODE_INITIATOR;
6569 case QLA2XXX_INI_MODE_DUAL:
6570 vha->host->active_mode = MODE_DUAL;
6577 /* Must be called under HW lock */
6578 static void qlt_clear_mode(struct scsi_qla_host *vha)
6580 switch (vha->qlini_mode) {
6581 case QLA2XXX_INI_MODE_DISABLED:
6582 vha->host->active_mode = MODE_UNKNOWN;
6584 case QLA2XXX_INI_MODE_EXCLUSIVE:
6585 vha->host->active_mode = MODE_INITIATOR;
6587 case QLA2XXX_INI_MODE_ENABLED:
6588 case QLA2XXX_INI_MODE_DUAL:
6589 vha->host->active_mode = MODE_INITIATOR;
6597 * qla_tgt_enable_vha - NO LOCK HELD
6599 * host_reset, bring up w/ Target Mode Enabled
6602 qlt_enable_vha(struct scsi_qla_host *vha)
6604 struct qla_hw_data *ha = vha->hw;
6605 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
6606 unsigned long flags;
6607 scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
6610 ql_dbg(ql_dbg_tgt, vha, 0xe069,
6611 "Unable to locate qla_tgt pointer from"
6612 " struct qla_hw_data\n");
6616 if (vha->qlini_mode == QLA2XXX_INI_MODE_ENABLED)
6619 if (ha->tgt.num_act_qpairs > ha->max_qpairs)
6620 ha->tgt.num_act_qpairs = ha->max_qpairs;
6621 spin_lock_irqsave(&ha->hardware_lock, flags);
6622 tgt->tgt_stopped = 0;
6624 spin_unlock_irqrestore(&ha->hardware_lock, flags);
6626 mutex_lock(&ha->optrom_mutex);
6627 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf021,
6630 qla24xx_disable_vp(vha);
6631 qla24xx_enable_vp(vha);
6633 set_bit(ISP_ABORT_NEEDED, &base_vha->dpc_flags);
6634 qla2xxx_wake_dpc(base_vha);
6635 WARN_ON_ONCE(qla2x00_wait_for_hba_online(base_vha) !=
6638 mutex_unlock(&ha->optrom_mutex);
6640 EXPORT_SYMBOL(qlt_enable_vha);
6643 * qla_tgt_disable_vha - NO LOCK HELD
6645 * Disable Target Mode and reset the adapter
6647 static void qlt_disable_vha(struct scsi_qla_host *vha)
6649 struct qla_hw_data *ha = vha->hw;
6650 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
6651 unsigned long flags;
6654 ql_dbg(ql_dbg_tgt, vha, 0xe06a,
6655 "Unable to locate qla_tgt pointer from"
6656 " struct qla_hw_data\n");
6661 spin_lock_irqsave(&ha->hardware_lock, flags);
6662 qlt_clear_mode(vha);
6663 spin_unlock_irqrestore(&ha->hardware_lock, flags);
6665 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
6666 qla2xxx_wake_dpc(vha);
6669 * We are expecting the offline state.
6670 * QLA_FUNCTION_FAILED means that adapter is offline.
6672 if (qla2x00_wait_for_hba_online(vha) != QLA_SUCCESS)
6673 ql_dbg(ql_dbg_tgt, vha, 0xe081,
6674 "adapter is offline\n");
6678 * Called from qla_init.c:qla24xx_vport_create() contex to setup
6679 * the target mode specific struct scsi_qla_host and struct qla_hw_data
6683 qlt_vport_create(struct scsi_qla_host *vha, struct qla_hw_data *ha)
6685 vha->vha_tgt.qla_tgt = NULL;
6687 mutex_init(&vha->vha_tgt.tgt_mutex);
6688 mutex_init(&vha->vha_tgt.tgt_host_action_mutex);
6690 qlt_clear_mode(vha);
6693 * NOTE: Currently the value is kept the same for <24xx and
6694 * >=24xx ISPs. If it is necessary to change it,
6695 * the check should be added for specific ISPs,
6696 * assigning the value appropriately.
6698 ha->tgt.atio_q_length = ATIO_ENTRY_CNT_24XX;
6700 qlt_add_target(ha, vha);
6704 qlt_rff_id(struct scsi_qla_host *vha)
6708 * FC-4 Feature bit 0 indicates target functionality to the name server.
6710 if (qla_tgt_mode_enabled(vha)) {
6711 fc4_feature = BIT_0;
6712 } else if (qla_ini_mode_enabled(vha)) {
6713 fc4_feature = BIT_1;
6714 } else if (qla_dual_mode_enabled(vha))
6715 fc4_feature = BIT_0 | BIT_1;
6721 * qlt_init_atio_q_entries() - Initializes ATIO queue entries.
6724 * Beginning of ATIO ring has initialization control block already built
6725 * by nvram config routine.
6727 * Returns 0 on success.
6730 qlt_init_atio_q_entries(struct scsi_qla_host *vha)
6732 struct qla_hw_data *ha = vha->hw;
6734 struct atio_from_isp *pkt = (struct atio_from_isp *)ha->tgt.atio_ring;
6736 if (qla_ini_mode_enabled(vha))
6739 for (cnt = 0; cnt < ha->tgt.atio_q_length; cnt++) {
6740 pkt->u.raw.signature = cpu_to_le32(ATIO_PROCESSED);
6747 * qlt_24xx_process_atio_queue() - Process ATIO queue entries.
6748 * @ha: SCSI driver HA context
6751 qlt_24xx_process_atio_queue(struct scsi_qla_host *vha, uint8_t ha_locked)
6753 struct qla_hw_data *ha = vha->hw;
6754 struct atio_from_isp *pkt;
6757 if (!ha->flags.fw_started)
6760 while ((ha->tgt.atio_ring_ptr->signature != ATIO_PROCESSED) ||
6761 fcpcmd_is_corrupted(ha->tgt.atio_ring_ptr)) {
6762 pkt = (struct atio_from_isp *)ha->tgt.atio_ring_ptr;
6763 cnt = pkt->u.raw.entry_count;
6765 if (unlikely(fcpcmd_is_corrupted(ha->tgt.atio_ring_ptr))) {
6767 * This packet is corrupted. The header + payload
6768 * can not be trusted. There is no point in passing
6771 ql_log(ql_log_warn, vha, 0xd03c,
6772 "corrupted fcp frame SID[%3phN] OXID[%04x] EXCG[%x] %64phN\n",
6773 &pkt->u.isp24.fcp_hdr.s_id,
6774 be16_to_cpu(pkt->u.isp24.fcp_hdr.ox_id),
6775 pkt->u.isp24.exchange_addr, pkt);
6777 adjust_corrupted_atio(pkt);
6778 qlt_send_term_exchange(ha->base_qpair, NULL, pkt,
6781 qlt_24xx_atio_pkt_all_vps(vha,
6782 (struct atio_from_isp *)pkt, ha_locked);
6785 for (i = 0; i < cnt; i++) {
6786 ha->tgt.atio_ring_index++;
6787 if (ha->tgt.atio_ring_index == ha->tgt.atio_q_length) {
6788 ha->tgt.atio_ring_index = 0;
6789 ha->tgt.atio_ring_ptr = ha->tgt.atio_ring;
6791 ha->tgt.atio_ring_ptr++;
6793 pkt->u.raw.signature = cpu_to_le32(ATIO_PROCESSED);
6794 pkt = (struct atio_from_isp *)ha->tgt.atio_ring_ptr;
6799 /* Adjust ring index */
6800 wrt_reg_dword(ISP_ATIO_Q_OUT(vha), ha->tgt.atio_ring_index);
6804 qlt_24xx_config_rings(struct scsi_qla_host *vha)
6806 struct qla_hw_data *ha = vha->hw;
6807 struct qla_msix_entry *msix = &ha->msix_entries[2];
6808 struct init_cb_24xx *icb = (struct init_cb_24xx *)ha->init_cb;
6810 if (!QLA_TGT_MODE_ENABLED())
6813 wrt_reg_dword(ISP_ATIO_Q_IN(vha), 0);
6814 wrt_reg_dword(ISP_ATIO_Q_OUT(vha), 0);
6815 rd_reg_dword(ISP_ATIO_Q_OUT(vha));
6817 if (ha->flags.msix_enabled) {
6818 if (IS_QLA83XX(ha) || IS_QLA27XX(ha) || IS_QLA28XX(ha)) {
6819 if (IS_QLA2071(ha)) {
6820 /* 4 ports Baker: Enable Interrupt Handshake */
6822 icb->firmware_options_2 |= cpu_to_le32(BIT_26);
6824 icb->msix_atio = cpu_to_le16(msix->entry);
6825 icb->firmware_options_2 &= cpu_to_le32(~BIT_26);
6827 ql_dbg(ql_dbg_init, vha, 0xf072,
6828 "Registering ICB vector 0x%x for atio que.\n",
6833 if (IS_QLA83XX(ha) || IS_QLA27XX(ha) || IS_QLA28XX(ha)) {
6835 icb->firmware_options_2 |= cpu_to_le32(BIT_26);
6836 ql_dbg(ql_dbg_init, vha, 0xf072,
6837 "%s: Use INTx for ATIOQ.\n", __func__);
6843 qlt_24xx_config_nvram_stage1(struct scsi_qla_host *vha, struct nvram_24xx *nv)
6845 struct qla_hw_data *ha = vha->hw;
6848 if (!QLA_TGT_MODE_ENABLED())
6851 if (qla_tgt_mode_enabled(vha) || qla_dual_mode_enabled(vha)) {
6852 if (!ha->tgt.saved_set) {
6853 /* We save only once */
6854 ha->tgt.saved_exchange_count = nv->exchange_count;
6855 ha->tgt.saved_firmware_options_1 =
6856 nv->firmware_options_1;
6857 ha->tgt.saved_firmware_options_2 =
6858 nv->firmware_options_2;
6859 ha->tgt.saved_firmware_options_3 =
6860 nv->firmware_options_3;
6861 ha->tgt.saved_set = 1;
6864 if (qla_tgt_mode_enabled(vha))
6865 nv->exchange_count = cpu_to_le16(0xFFFF);
6867 nv->exchange_count = cpu_to_le16(vha->ql2xexchoffld);
6869 /* Enable target mode */
6870 nv->firmware_options_1 |= cpu_to_le32(BIT_4);
6872 /* Disable ini mode, if requested */
6873 if (qla_tgt_mode_enabled(vha))
6874 nv->firmware_options_1 |= cpu_to_le32(BIT_5);
6876 /* Disable Full Login after LIP */
6877 nv->firmware_options_1 &= cpu_to_le32(~BIT_13);
6878 /* Enable initial LIP */
6879 nv->firmware_options_1 &= cpu_to_le32(~BIT_9);
6880 if (ql2xtgt_tape_enable)
6881 /* Enable FC Tape support */
6882 nv->firmware_options_2 |= cpu_to_le32(BIT_12);
6884 /* Disable FC Tape support */
6885 nv->firmware_options_2 &= cpu_to_le32(~BIT_12);
6887 /* Disable Full Login after LIP */
6888 nv->host_p &= cpu_to_le32(~BIT_10);
6891 * clear BIT 15 explicitly as we have seen at least
6892 * a couple of instances where this was set and this
6893 * was causing the firmware to not be initialized.
6895 nv->firmware_options_1 &= cpu_to_le32(~BIT_15);
6896 /* Enable target PRLI control */
6897 nv->firmware_options_2 |= cpu_to_le32(BIT_14);
6899 if (IS_QLA25XX(ha)) {
6900 /* Change Loop-prefer to Pt-Pt */
6901 tmp = ~(BIT_4|BIT_5|BIT_6);
6902 nv->firmware_options_2 &= cpu_to_le32(tmp);
6904 nv->firmware_options_2 |= cpu_to_le32(tmp);
6907 if (ha->tgt.saved_set) {
6908 nv->exchange_count = ha->tgt.saved_exchange_count;
6909 nv->firmware_options_1 =
6910 ha->tgt.saved_firmware_options_1;
6911 nv->firmware_options_2 =
6912 ha->tgt.saved_firmware_options_2;
6913 nv->firmware_options_3 =
6914 ha->tgt.saved_firmware_options_3;
6919 if (ha->base_qpair->enable_class_2) {
6920 if (vha->flags.init_done)
6921 fc_host_supported_classes(vha->host) =
6922 FC_COS_CLASS2 | FC_COS_CLASS3;
6924 nv->firmware_options_2 |= cpu_to_le32(BIT_8);
6926 if (vha->flags.init_done)
6927 fc_host_supported_classes(vha->host) = FC_COS_CLASS3;
6929 nv->firmware_options_2 &= ~cpu_to_le32(BIT_8);
6934 qlt_24xx_config_nvram_stage2(struct scsi_qla_host *vha,
6935 struct init_cb_24xx *icb)
6937 struct qla_hw_data *ha = vha->hw;
6939 if (!QLA_TGT_MODE_ENABLED())
6942 if (ha->tgt.node_name_set) {
6943 memcpy(icb->node_name, ha->tgt.tgt_node_name, WWN_SIZE);
6944 icb->firmware_options_1 |= cpu_to_le32(BIT_14);
6949 qlt_81xx_config_nvram_stage1(struct scsi_qla_host *vha, struct nvram_81xx *nv)
6951 struct qla_hw_data *ha = vha->hw;
6954 if (!QLA_TGT_MODE_ENABLED())
6957 if (qla_tgt_mode_enabled(vha) || qla_dual_mode_enabled(vha)) {
6958 if (!ha->tgt.saved_set) {
6959 /* We save only once */
6960 ha->tgt.saved_exchange_count = nv->exchange_count;
6961 ha->tgt.saved_firmware_options_1 =
6962 nv->firmware_options_1;
6963 ha->tgt.saved_firmware_options_2 =
6964 nv->firmware_options_2;
6965 ha->tgt.saved_firmware_options_3 =
6966 nv->firmware_options_3;
6967 ha->tgt.saved_set = 1;
6970 if (qla_tgt_mode_enabled(vha))
6971 nv->exchange_count = cpu_to_le16(0xFFFF);
6973 nv->exchange_count = cpu_to_le16(vha->ql2xexchoffld);
6975 /* Enable target mode */
6976 nv->firmware_options_1 |= cpu_to_le32(BIT_4);
6978 /* Disable ini mode, if requested */
6979 if (qla_tgt_mode_enabled(vha))
6980 nv->firmware_options_1 |= cpu_to_le32(BIT_5);
6981 /* Disable Full Login after LIP */
6982 nv->firmware_options_1 &= cpu_to_le32(~BIT_13);
6983 /* Enable initial LIP */
6984 nv->firmware_options_1 &= cpu_to_le32(~BIT_9);
6986 * clear BIT 15 explicitly as we have seen at
6987 * least a couple of instances where this was set
6988 * and this was causing the firmware to not be
6991 nv->firmware_options_1 &= cpu_to_le32(~BIT_15);
6992 if (ql2xtgt_tape_enable)
6993 /* Enable FC tape support */
6994 nv->firmware_options_2 |= cpu_to_le32(BIT_12);
6996 /* Disable FC tape support */
6997 nv->firmware_options_2 &= cpu_to_le32(~BIT_12);
6999 /* Disable Full Login after LIP */
7000 nv->host_p &= cpu_to_le32(~BIT_10);
7001 /* Enable target PRLI control */
7002 nv->firmware_options_2 |= cpu_to_le32(BIT_14);
7004 /* Change Loop-prefer to Pt-Pt */
7005 tmp = ~(BIT_4|BIT_5|BIT_6);
7006 nv->firmware_options_2 &= cpu_to_le32(tmp);
7008 nv->firmware_options_2 |= cpu_to_le32(tmp);
7010 if (ha->tgt.saved_set) {
7011 nv->exchange_count = ha->tgt.saved_exchange_count;
7012 nv->firmware_options_1 =
7013 ha->tgt.saved_firmware_options_1;
7014 nv->firmware_options_2 =
7015 ha->tgt.saved_firmware_options_2;
7016 nv->firmware_options_3 =
7017 ha->tgt.saved_firmware_options_3;
7022 if (ha->base_qpair->enable_class_2) {
7023 if (vha->flags.init_done)
7024 fc_host_supported_classes(vha->host) =
7025 FC_COS_CLASS2 | FC_COS_CLASS3;
7027 nv->firmware_options_2 |= cpu_to_le32(BIT_8);
7029 if (vha->flags.init_done)
7030 fc_host_supported_classes(vha->host) = FC_COS_CLASS3;
7032 nv->firmware_options_2 &= ~cpu_to_le32(BIT_8);
7037 qlt_81xx_config_nvram_stage2(struct scsi_qla_host *vha,
7038 struct init_cb_81xx *icb)
7040 struct qla_hw_data *ha = vha->hw;
7042 if (!QLA_TGT_MODE_ENABLED())
7045 if (ha->tgt.node_name_set) {
7046 memcpy(icb->node_name, ha->tgt.tgt_node_name, WWN_SIZE);
7047 icb->firmware_options_1 |= cpu_to_le32(BIT_14);
7052 qlt_83xx_iospace_config(struct qla_hw_data *ha)
7054 if (!QLA_TGT_MODE_ENABLED())
7057 ha->msix_count += 1; /* For ATIO Q */
7062 qlt_modify_vp_config(struct scsi_qla_host *vha,
7063 struct vp_config_entry_24xx *vpmod)
7065 /* enable target mode. Bit5 = 1 => disable */
7066 if (qla_tgt_mode_enabled(vha) || qla_dual_mode_enabled(vha))
7067 vpmod->options_idx1 &= ~BIT_5;
7069 /* Disable ini mode, if requested. bit4 = 1 => disable */
7070 if (qla_tgt_mode_enabled(vha))
7071 vpmod->options_idx1 &= ~BIT_4;
7075 qlt_probe_one_stage1(struct scsi_qla_host *base_vha, struct qla_hw_data *ha)
7079 if (!QLA_TGT_MODE_ENABLED())
7082 if ((ql2xenablemsix == 0) || IS_QLA83XX(ha) || IS_QLA27XX(ha) ||
7084 ISP_ATIO_Q_IN(base_vha) = &ha->mqiobase->isp25mq.atio_q_in;
7085 ISP_ATIO_Q_OUT(base_vha) = &ha->mqiobase->isp25mq.atio_q_out;
7087 ISP_ATIO_Q_IN(base_vha) = &ha->iobase->isp24.atio_q_in;
7088 ISP_ATIO_Q_OUT(base_vha) = &ha->iobase->isp24.atio_q_out;
7091 mutex_init(&base_vha->vha_tgt.tgt_mutex);
7092 mutex_init(&base_vha->vha_tgt.tgt_host_action_mutex);
7094 INIT_LIST_HEAD(&base_vha->unknown_atio_list);
7095 INIT_DELAYED_WORK(&base_vha->unknown_atio_work,
7096 qlt_unknown_atio_work_fn);
7098 qlt_clear_mode(base_vha);
7100 rc = btree_init32(&ha->tgt.host_map);
7102 ql_log(ql_log_info, base_vha, 0xd03d,
7103 "Unable to initialize ha->host_map btree\n");
7105 qlt_update_vp_map(base_vha, SET_VP_IDX);
7109 qla83xx_msix_atio_q(int irq, void *dev_id)
7111 struct rsp_que *rsp;
7112 scsi_qla_host_t *vha;
7113 struct qla_hw_data *ha;
7114 unsigned long flags;
7116 rsp = (struct rsp_que *) dev_id;
7118 vha = pci_get_drvdata(ha->pdev);
7120 spin_lock_irqsave(&ha->tgt.atio_lock, flags);
7122 qlt_24xx_process_atio_queue(vha, 0);
7124 spin_unlock_irqrestore(&ha->tgt.atio_lock, flags);
7130 qlt_handle_abts_recv_work(struct work_struct *work)
7132 struct qla_tgt_sess_op *op = container_of(work,
7133 struct qla_tgt_sess_op, work);
7134 scsi_qla_host_t *vha = op->vha;
7135 struct qla_hw_data *ha = vha->hw;
7136 unsigned long flags;
7138 if (qla2x00_reset_active(vha) ||
7139 (op->chip_reset != ha->base_qpair->chip_reset))
7142 spin_lock_irqsave(&ha->tgt.atio_lock, flags);
7143 qlt_24xx_process_atio_queue(vha, 0);
7144 spin_unlock_irqrestore(&ha->tgt.atio_lock, flags);
7146 spin_lock_irqsave(&ha->hardware_lock, flags);
7147 qlt_response_pkt_all_vps(vha, op->rsp, (response_t *)&op->atio);
7148 spin_unlock_irqrestore(&ha->hardware_lock, flags);
7154 qlt_handle_abts_recv(struct scsi_qla_host *vha, struct rsp_que *rsp,
7157 struct qla_tgt_sess_op *op;
7159 op = kzalloc(sizeof(*op), GFP_ATOMIC);
7162 /* do not reach for ATIO queue here. This is best effort err
7163 * recovery at this point.
7165 qlt_response_pkt_all_vps(vha, rsp, pkt);
7169 memcpy(&op->atio, pkt, sizeof(*pkt));
7171 op->chip_reset = vha->hw->base_qpair->chip_reset;
7173 INIT_WORK(&op->work, qlt_handle_abts_recv_work);
7174 queue_work(qla_tgt_wq, &op->work);
7179 qlt_mem_alloc(struct qla_hw_data *ha)
7181 if (!QLA_TGT_MODE_ENABLED())
7184 ha->tgt.tgt_vp_map = kcalloc(MAX_MULTI_ID_FABRIC,
7185 sizeof(struct qla_tgt_vp_map),
7187 if (!ha->tgt.tgt_vp_map)
7190 ha->tgt.atio_ring = dma_alloc_coherent(&ha->pdev->dev,
7191 (ha->tgt.atio_q_length + 1) * sizeof(struct atio_from_isp),
7192 &ha->tgt.atio_dma, GFP_KERNEL);
7193 if (!ha->tgt.atio_ring) {
7194 kfree(ha->tgt.tgt_vp_map);
7201 qlt_mem_free(struct qla_hw_data *ha)
7203 if (!QLA_TGT_MODE_ENABLED())
7206 if (ha->tgt.atio_ring) {
7207 dma_free_coherent(&ha->pdev->dev, (ha->tgt.atio_q_length + 1) *
7208 sizeof(struct atio_from_isp), ha->tgt.atio_ring,
7211 ha->tgt.atio_ring = NULL;
7212 ha->tgt.atio_dma = 0;
7213 kfree(ha->tgt.tgt_vp_map);
7214 ha->tgt.tgt_vp_map = NULL;
7217 /* vport_slock to be held by the caller */
7219 qlt_update_vp_map(struct scsi_qla_host *vha, int cmd)
7225 if (!QLA_TGT_MODE_ENABLED())
7228 key = vha->d_id.b24;
7232 vha->hw->tgt.tgt_vp_map[vha->vp_idx].vha = vha;
7235 slot = btree_lookup32(&vha->hw->tgt.host_map, key);
7237 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf018,
7238 "Save vha in host_map %p %06x\n", vha, key);
7239 rc = btree_insert32(&vha->hw->tgt.host_map,
7240 key, vha, GFP_ATOMIC);
7242 ql_log(ql_log_info, vha, 0xd03e,
7243 "Unable to insert s_id into host_map: %06x\n",
7247 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf019,
7248 "replace existing vha in host_map %p %06x\n", vha, key);
7249 btree_update32(&vha->hw->tgt.host_map, key, vha);
7252 vha->hw->tgt.tgt_vp_map[vha->vp_idx].vha = NULL;
7255 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01a,
7256 "clear vha in host_map %p %06x\n", vha, key);
7257 slot = btree_lookup32(&vha->hw->tgt.host_map, key);
7259 btree_remove32(&vha->hw->tgt.host_map, key);
7265 void qlt_update_host_map(struct scsi_qla_host *vha, port_id_t id)
7268 if (!vha->d_id.b24) {
7270 qlt_update_vp_map(vha, SET_AL_PA);
7271 } else if (vha->d_id.b24 != id.b24) {
7272 qlt_update_vp_map(vha, RESET_AL_PA);
7274 qlt_update_vp_map(vha, SET_AL_PA);
7278 static int __init qlt_parse_ini_mode(void)
7280 if (strcasecmp(qlini_mode, QLA2XXX_INI_MODE_STR_EXCLUSIVE) == 0)
7281 ql2x_ini_mode = QLA2XXX_INI_MODE_EXCLUSIVE;
7282 else if (strcasecmp(qlini_mode, QLA2XXX_INI_MODE_STR_DISABLED) == 0)
7283 ql2x_ini_mode = QLA2XXX_INI_MODE_DISABLED;
7284 else if (strcasecmp(qlini_mode, QLA2XXX_INI_MODE_STR_ENABLED) == 0)
7285 ql2x_ini_mode = QLA2XXX_INI_MODE_ENABLED;
7286 else if (strcasecmp(qlini_mode, QLA2XXX_INI_MODE_STR_DUAL) == 0)
7287 ql2x_ini_mode = QLA2XXX_INI_MODE_DUAL;
7294 int __init qlt_init(void)
7298 BUILD_BUG_ON(sizeof(struct ctio7_to_24xx) != 64);
7299 BUILD_BUG_ON(sizeof(struct ctio_to_2xxx) != 64);
7301 if (!qlt_parse_ini_mode()) {
7302 ql_log(ql_log_fatal, NULL, 0xe06b,
7303 "qlt_parse_ini_mode() failed\n");
7307 if (!QLA_TGT_MODE_ENABLED())
7310 qla_tgt_mgmt_cmd_cachep = kmem_cache_create("qla_tgt_mgmt_cmd_cachep",
7311 sizeof(struct qla_tgt_mgmt_cmd), __alignof__(struct
7312 qla_tgt_mgmt_cmd), 0, NULL);
7313 if (!qla_tgt_mgmt_cmd_cachep) {
7314 ql_log(ql_log_fatal, NULL, 0xd04b,
7315 "kmem_cache_create for qla_tgt_mgmt_cmd_cachep failed\n");
7319 qla_tgt_plogi_cachep = kmem_cache_create("qla_tgt_plogi_cachep",
7320 sizeof(struct qlt_plogi_ack_t), __alignof__(struct qlt_plogi_ack_t),
7323 if (!qla_tgt_plogi_cachep) {
7324 ql_log(ql_log_fatal, NULL, 0xe06d,
7325 "kmem_cache_create for qla_tgt_plogi_cachep failed\n");
7327 goto out_mgmt_cmd_cachep;
7330 qla_tgt_mgmt_cmd_mempool = mempool_create(25, mempool_alloc_slab,
7331 mempool_free_slab, qla_tgt_mgmt_cmd_cachep);
7332 if (!qla_tgt_mgmt_cmd_mempool) {
7333 ql_log(ql_log_fatal, NULL, 0xe06e,
7334 "mempool_create for qla_tgt_mgmt_cmd_mempool failed\n");
7336 goto out_plogi_cachep;
7339 qla_tgt_wq = alloc_workqueue("qla_tgt_wq", 0, 0);
7341 ql_log(ql_log_fatal, NULL, 0xe06f,
7342 "alloc_workqueue for qla_tgt_wq failed\n");
7344 goto out_cmd_mempool;
7347 * Return 1 to signal that initiator-mode is being disabled
7349 return (ql2x_ini_mode == QLA2XXX_INI_MODE_DISABLED) ? 1 : 0;
7352 mempool_destroy(qla_tgt_mgmt_cmd_mempool);
7354 kmem_cache_destroy(qla_tgt_plogi_cachep);
7355 out_mgmt_cmd_cachep:
7356 kmem_cache_destroy(qla_tgt_mgmt_cmd_cachep);
7362 if (!QLA_TGT_MODE_ENABLED())
7365 destroy_workqueue(qla_tgt_wq);
7366 mempool_destroy(qla_tgt_mgmt_cmd_mempool);
7367 kmem_cache_destroy(qla_tgt_plogi_cachep);
7368 kmem_cache_destroy(qla_tgt_mgmt_cmd_cachep);