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>
30 #include <target/target_core_base.h>
31 #include <target/target_core_fabric.h>
34 #include "qla_target.h"
36 static int ql2xtgt_tape_enable;
37 module_param(ql2xtgt_tape_enable, int, S_IRUGO|S_IWUSR);
38 MODULE_PARM_DESC(ql2xtgt_tape_enable,
39 "Enables Sequence level error recovery (aka FC Tape). Default is 0 - no SLER. 1 - Enable SLER.");
41 static char *qlini_mode = QLA2XXX_INI_MODE_STR_ENABLED;
42 module_param(qlini_mode, charp, S_IRUGO);
43 MODULE_PARM_DESC(qlini_mode,
44 "Determines when initiator mode will be enabled. Possible values: "
45 "\"exclusive\" - initiator mode will be enabled on load, "
46 "disabled on enabling target mode and then on disabling target mode "
48 "\"disabled\" - initiator mode will never be enabled; "
49 "\"dual\" - Initiator Modes will be enabled. Target Mode can be activated "
51 "\"enabled\" (default) - initiator mode will always stay enabled.");
53 static int ql_dm_tgt_ex_pct = 0;
54 module_param(ql_dm_tgt_ex_pct, int, S_IRUGO|S_IWUSR);
55 MODULE_PARM_DESC(ql_dm_tgt_ex_pct,
56 "For Dual Mode (qlini_mode=dual), this parameter determines "
57 "the percentage of exchanges/cmds FW will allocate resources "
61 module_param(ql2xuctrlirq, int, 0644);
62 MODULE_PARM_DESC(ql2xuctrlirq,
63 "User to control IRQ placement via smp_affinity."
64 "Valid with qlini_mode=disabled."
65 "1(default): enable");
67 int ql2x_ini_mode = QLA2XXX_INI_MODE_EXCLUSIVE;
69 static int qla_sam_status = SAM_STAT_BUSY;
70 static int tc_sam_status = SAM_STAT_TASK_SET_FULL; /* target core */
73 * From scsi/fc/fc_fcp.h
75 enum fcp_resp_rsp_codes {
77 FCP_DATA_LEN_INVALID = 1,
78 FCP_CMND_FIELDS_INVALID = 2,
79 FCP_DATA_PARAM_MISMATCH = 3,
82 FCP_TMF_INVALID_LUN = 9,
86 * fc_pri_ta from scsi/fc/fc_fcp.h
88 #define FCP_PTA_SIMPLE 0 /* simple task attribute */
89 #define FCP_PTA_HEADQ 1 /* head of queue task attribute */
90 #define FCP_PTA_ORDERED 2 /* ordered task attribute */
91 #define FCP_PTA_ACA 4 /* auto. contingent allegiance */
92 #define FCP_PTA_MASK 7 /* mask for task attribute field */
93 #define FCP_PRI_SHIFT 3 /* priority field starts in bit 3 */
94 #define FCP_PRI_RESVD_MASK 0x80 /* reserved bits in priority field */
97 * This driver calls qla2x00_alloc_iocbs() and qla2x00_issue_marker(), which
98 * must be called under HW lock and could unlock/lock it inside.
99 * It isn't an issue, since in the current implementation on the time when
100 * those functions are called:
102 * - Either context is IRQ and only IRQ handler can modify HW data,
103 * including rings related fields,
105 * - Or access to target mode variables from struct qla_tgt doesn't
106 * cross those functions boundaries, except tgt_stop, which
107 * additionally protected by irq_cmd_count.
109 /* Predefs for callbacks handed to qla2xxx LLD */
110 static void qlt_24xx_atio_pkt(struct scsi_qla_host *ha,
111 struct atio_from_isp *pkt, uint8_t);
112 static void qlt_response_pkt(struct scsi_qla_host *ha, struct rsp_que *rsp,
114 static int qlt_issue_task_mgmt(struct fc_port *sess, u64 lun,
115 int fn, void *iocb, int flags);
116 static void qlt_send_term_exchange(struct qla_qpair *, struct qla_tgt_cmd
117 *cmd, struct atio_from_isp *atio, int ha_locked, int ul_abort);
118 static void qlt_alloc_qfull_cmd(struct scsi_qla_host *vha,
119 struct atio_from_isp *atio, uint16_t status, int qfull);
120 static void qlt_disable_vha(struct scsi_qla_host *vha);
121 static void qlt_clear_tgt_db(struct qla_tgt *tgt);
122 static void qlt_send_notify_ack(struct qla_qpair *qpair,
123 struct imm_ntfy_from_isp *ntfy,
124 uint32_t add_flags, uint16_t resp_code, int resp_code_valid,
125 uint16_t srr_flags, uint16_t srr_reject_code, uint8_t srr_explan);
126 static void qlt_send_term_imm_notif(struct scsi_qla_host *vha,
127 struct imm_ntfy_from_isp *imm, int ha_locked);
128 static struct fc_port *qlt_create_sess(struct scsi_qla_host *vha,
129 fc_port_t *fcport, bool local);
130 void qlt_unreg_sess(struct fc_port *sess);
131 static void qlt_24xx_handle_abts(struct scsi_qla_host *,
132 struct abts_recv_from_24xx *);
133 static void qlt_send_busy(struct qla_qpair *, struct atio_from_isp *,
135 static int qlt_check_reserve_free_req(struct qla_qpair *qpair, uint32_t);
136 static inline uint32_t qlt_make_handle(struct qla_qpair *);
141 static struct kmem_cache *qla_tgt_mgmt_cmd_cachep;
142 struct kmem_cache *qla_tgt_plogi_cachep;
143 static mempool_t *qla_tgt_mgmt_cmd_mempool;
144 static struct workqueue_struct *qla_tgt_wq;
145 static DEFINE_MUTEX(qla_tgt_mutex);
146 static LIST_HEAD(qla_tgt_glist);
148 static const char *prot_op_str(u32 prot_op)
151 case TARGET_PROT_NORMAL: return "NORMAL";
152 case TARGET_PROT_DIN_INSERT: return "DIN_INSERT";
153 case TARGET_PROT_DOUT_INSERT: return "DOUT_INSERT";
154 case TARGET_PROT_DIN_STRIP: return "DIN_STRIP";
155 case TARGET_PROT_DOUT_STRIP: return "DOUT_STRIP";
156 case TARGET_PROT_DIN_PASS: return "DIN_PASS";
157 case TARGET_PROT_DOUT_PASS: return "DOUT_PASS";
158 default: return "UNKNOWN";
162 /* This API intentionally takes dest as a parameter, rather than returning
163 * int value to avoid caller forgetting to issue wmb() after the store */
164 void qlt_do_generation_tick(struct scsi_qla_host *vha, int *dest)
166 scsi_qla_host_t *base_vha = pci_get_drvdata(vha->hw->pdev);
167 *dest = atomic_inc_return(&base_vha->generation_tick);
172 /* Might release hw lock, then reaquire!! */
173 static inline int qlt_issue_marker(struct scsi_qla_host *vha, int vha_locked)
175 /* Send marker if required */
176 if (unlikely(vha->marker_needed != 0)) {
177 int rc = qla2x00_issue_marker(vha, vha_locked);
179 if (rc != QLA_SUCCESS) {
180 ql_dbg(ql_dbg_tgt, vha, 0xe03d,
181 "qla_target(%d): issue_marker() failed\n",
190 struct scsi_qla_host *qlt_find_host_by_d_id(struct scsi_qla_host *vha,
193 struct scsi_qla_host *host;
196 if ((vha->d_id.b.area == d_id[1]) && (vha->d_id.b.domain == d_id[0]) &&
197 (vha->d_id.b.al_pa == d_id[2]))
200 key = (uint32_t)d_id[0] << 16;
201 key |= (uint32_t)d_id[1] << 8;
202 key |= (uint32_t)d_id[2];
204 host = btree_lookup32(&vha->hw->tgt.host_map, key);
206 ql_dbg(ql_dbg_tgt_mgt + ql_dbg_verbose, vha, 0xf005,
207 "Unable to find host %06x\n", key);
213 struct scsi_qla_host *qlt_find_host_by_vp_idx(struct scsi_qla_host *vha,
216 struct qla_hw_data *ha = vha->hw;
218 if (vha->vp_idx == vp_idx)
221 BUG_ON(ha->tgt.tgt_vp_map == NULL);
222 if (likely(test_bit(vp_idx, ha->vp_idx_map)))
223 return ha->tgt.tgt_vp_map[vp_idx].vha;
228 static inline void qlt_incr_num_pend_cmds(struct scsi_qla_host *vha)
232 spin_lock_irqsave(&vha->hw->tgt.q_full_lock, flags);
234 vha->hw->tgt.num_pend_cmds++;
235 if (vha->hw->tgt.num_pend_cmds > vha->qla_stats.stat_max_pend_cmds)
236 vha->qla_stats.stat_max_pend_cmds =
237 vha->hw->tgt.num_pend_cmds;
238 spin_unlock_irqrestore(&vha->hw->tgt.q_full_lock, flags);
240 static inline void qlt_decr_num_pend_cmds(struct scsi_qla_host *vha)
244 spin_lock_irqsave(&vha->hw->tgt.q_full_lock, flags);
245 vha->hw->tgt.num_pend_cmds--;
246 spin_unlock_irqrestore(&vha->hw->tgt.q_full_lock, flags);
250 static void qlt_queue_unknown_atio(scsi_qla_host_t *vha,
251 struct atio_from_isp *atio, uint8_t ha_locked)
253 struct qla_tgt_sess_op *u;
254 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
258 ql_dbg(ql_dbg_async, vha, 0x502c,
259 "qla_target(%d): dropping unknown ATIO_TYPE7, because tgt is being stopped",
264 u = kzalloc(sizeof(*u), GFP_ATOMIC);
269 memcpy(&u->atio, atio, sizeof(*atio));
270 INIT_LIST_HEAD(&u->cmd_list);
272 spin_lock_irqsave(&vha->cmd_list_lock, flags);
273 list_add_tail(&u->cmd_list, &vha->unknown_atio_list);
274 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
276 schedule_delayed_work(&vha->unknown_atio_work, 1);
282 qlt_send_term_exchange(vha->hw->base_qpair, NULL, atio, ha_locked, 0);
286 static void qlt_try_to_dequeue_unknown_atios(struct scsi_qla_host *vha,
289 struct qla_tgt_sess_op *u, *t;
290 scsi_qla_host_t *host;
291 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
295 list_for_each_entry_safe(u, t, &vha->unknown_atio_list, cmd_list) {
297 ql_dbg(ql_dbg_async, vha, 0x502e,
298 "Freeing unknown %s %p, because of Abort\n",
300 qlt_send_term_exchange(vha->hw->base_qpair, NULL,
301 &u->atio, ha_locked, 0);
305 host = qlt_find_host_by_d_id(vha, u->atio.u.isp24.fcp_hdr.d_id);
307 ql_dbg(ql_dbg_async + ql_dbg_verbose, vha, 0x502f,
308 "Requeuing unknown ATIO_TYPE7 %p\n", u);
309 qlt_24xx_atio_pkt(host, &u->atio, ha_locked);
310 } else if (tgt->tgt_stop) {
311 ql_dbg(ql_dbg_async + ql_dbg_verbose, vha, 0x503a,
312 "Freeing unknown %s %p, because tgt is being stopped\n",
314 qlt_send_term_exchange(vha->hw->base_qpair, NULL,
315 &u->atio, ha_locked, 0);
317 ql_dbg(ql_dbg_async + ql_dbg_verbose, vha, 0x503d,
318 "Reschedule u %p, vha %p, host %p\n", u, vha, host);
321 schedule_delayed_work(&vha->unknown_atio_work,
328 spin_lock_irqsave(&vha->cmd_list_lock, flags);
329 list_del(&u->cmd_list);
330 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
335 void qlt_unknown_atio_work_fn(struct work_struct *work)
337 struct scsi_qla_host *vha = container_of(to_delayed_work(work),
338 struct scsi_qla_host, unknown_atio_work);
340 qlt_try_to_dequeue_unknown_atios(vha, 0);
343 static bool qlt_24xx_atio_pkt_all_vps(struct scsi_qla_host *vha,
344 struct atio_from_isp *atio, uint8_t ha_locked)
346 ql_dbg(ql_dbg_tgt, vha, 0xe072,
347 "%s: qla_target(%d): type %x ox_id %04x\n",
348 __func__, vha->vp_idx, atio->u.raw.entry_type,
349 be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id));
351 switch (atio->u.raw.entry_type) {
354 struct scsi_qla_host *host = qlt_find_host_by_d_id(vha,
355 atio->u.isp24.fcp_hdr.d_id);
356 if (unlikely(NULL == host)) {
357 ql_dbg(ql_dbg_tgt, vha, 0xe03e,
358 "qla_target(%d): Received ATIO_TYPE7 "
359 "with unknown d_id %x:%x:%x\n", vha->vp_idx,
360 atio->u.isp24.fcp_hdr.d_id[0],
361 atio->u.isp24.fcp_hdr.d_id[1],
362 atio->u.isp24.fcp_hdr.d_id[2]);
365 qlt_queue_unknown_atio(vha, atio, ha_locked);
368 if (unlikely(!list_empty(&vha->unknown_atio_list)))
369 qlt_try_to_dequeue_unknown_atios(vha, ha_locked);
371 qlt_24xx_atio_pkt(host, atio, ha_locked);
375 case IMMED_NOTIFY_TYPE:
377 struct scsi_qla_host *host = vha;
378 struct imm_ntfy_from_isp *entry =
379 (struct imm_ntfy_from_isp *)atio;
381 qlt_issue_marker(vha, ha_locked);
383 if ((entry->u.isp24.vp_index != 0xFF) &&
384 (entry->u.isp24.nport_handle != 0xFFFF)) {
385 host = qlt_find_host_by_vp_idx(vha,
386 entry->u.isp24.vp_index);
387 if (unlikely(!host)) {
388 ql_dbg(ql_dbg_tgt, vha, 0xe03f,
389 "qla_target(%d): Received "
390 "ATIO (IMMED_NOTIFY_TYPE) "
391 "with unknown vp_index %d\n",
392 vha->vp_idx, entry->u.isp24.vp_index);
396 qlt_24xx_atio_pkt(host, atio, ha_locked);
400 case VP_RPT_ID_IOCB_TYPE:
401 qla24xx_report_id_acquisition(vha,
402 (struct vp_rpt_id_entry_24xx *)atio);
407 struct abts_recv_from_24xx *entry =
408 (struct abts_recv_from_24xx *)atio;
409 struct scsi_qla_host *host = qlt_find_host_by_vp_idx(vha,
413 if (unlikely(!host)) {
414 ql_dbg(ql_dbg_tgt, vha, 0xe00a,
415 "qla_target(%d): Response pkt (ABTS_RECV_24XX) "
416 "received, with unknown vp_index %d\n",
417 vha->vp_idx, entry->vp_index);
421 spin_lock_irqsave(&host->hw->hardware_lock, flags);
422 qlt_24xx_handle_abts(host, (struct abts_recv_from_24xx *)atio);
424 spin_unlock_irqrestore(&host->hw->hardware_lock, flags);
428 /* case PUREX_IOCB_TYPE: ql2xmvasynctoatio */
431 ql_dbg(ql_dbg_tgt, vha, 0xe040,
432 "qla_target(%d): Received unknown ATIO atio "
433 "type %x\n", vha->vp_idx, atio->u.raw.entry_type);
440 void qlt_response_pkt_all_vps(struct scsi_qla_host *vha,
441 struct rsp_que *rsp, response_t *pkt)
443 switch (pkt->entry_type) {
445 ql_dbg(ql_dbg_tgt, vha, 0xe073,
446 "qla_target(%d):%s: CRC2 Response pkt\n",
447 vha->vp_idx, __func__);
451 struct ctio7_from_24xx *entry = (struct ctio7_from_24xx *)pkt;
452 struct scsi_qla_host *host = qlt_find_host_by_vp_idx(vha,
454 if (unlikely(!host)) {
455 ql_dbg(ql_dbg_tgt, vha, 0xe041,
456 "qla_target(%d): Response pkt (CTIO_TYPE7) "
457 "received, with unknown vp_index %d\n",
458 vha->vp_idx, entry->vp_index);
461 qlt_response_pkt(host, rsp, pkt);
465 case IMMED_NOTIFY_TYPE:
467 struct scsi_qla_host *host = vha;
468 struct imm_ntfy_from_isp *entry =
469 (struct imm_ntfy_from_isp *)pkt;
471 host = qlt_find_host_by_vp_idx(vha, entry->u.isp24.vp_index);
472 if (unlikely(!host)) {
473 ql_dbg(ql_dbg_tgt, vha, 0xe042,
474 "qla_target(%d): Response pkt (IMMED_NOTIFY_TYPE) "
475 "received, with unknown vp_index %d\n",
476 vha->vp_idx, entry->u.isp24.vp_index);
479 qlt_response_pkt(host, rsp, pkt);
483 case NOTIFY_ACK_TYPE:
485 struct scsi_qla_host *host = vha;
486 struct nack_to_isp *entry = (struct nack_to_isp *)pkt;
488 if (0xFF != entry->u.isp24.vp_index) {
489 host = qlt_find_host_by_vp_idx(vha,
490 entry->u.isp24.vp_index);
491 if (unlikely(!host)) {
492 ql_dbg(ql_dbg_tgt, vha, 0xe043,
493 "qla_target(%d): Response "
494 "pkt (NOTIFY_ACK_TYPE) "
495 "received, with unknown "
496 "vp_index %d\n", vha->vp_idx,
497 entry->u.isp24.vp_index);
501 qlt_response_pkt(host, rsp, pkt);
507 struct abts_recv_from_24xx *entry =
508 (struct abts_recv_from_24xx *)pkt;
509 struct scsi_qla_host *host = qlt_find_host_by_vp_idx(vha,
511 if (unlikely(!host)) {
512 ql_dbg(ql_dbg_tgt, vha, 0xe044,
513 "qla_target(%d): Response pkt "
514 "(ABTS_RECV_24XX) received, with unknown "
515 "vp_index %d\n", vha->vp_idx, entry->vp_index);
518 qlt_response_pkt(host, rsp, pkt);
524 struct abts_resp_to_24xx *entry =
525 (struct abts_resp_to_24xx *)pkt;
526 struct scsi_qla_host *host = qlt_find_host_by_vp_idx(vha,
528 if (unlikely(!host)) {
529 ql_dbg(ql_dbg_tgt, vha, 0xe045,
530 "qla_target(%d): Response pkt "
531 "(ABTS_RECV_24XX) received, with unknown "
532 "vp_index %d\n", vha->vp_idx, entry->vp_index);
535 qlt_response_pkt(host, rsp, pkt);
539 qlt_response_pkt(vha, rsp, pkt);
546 * All qlt_plogi_ack_t operations are protected by hardware_lock
548 static int qla24xx_post_nack_work(struct scsi_qla_host *vha, fc_port_t *fcport,
549 struct imm_ntfy_from_isp *ntfy, int type)
551 struct qla_work_evt *e;
553 e = qla2x00_alloc_work(vha, QLA_EVT_NACK);
555 return QLA_FUNCTION_FAILED;
557 e->u.nack.fcport = fcport;
558 e->u.nack.type = type;
559 memcpy(e->u.nack.iocb, ntfy, sizeof(struct imm_ntfy_from_isp));
560 return qla2x00_post_work(vha, e);
564 void qla2x00_async_nack_sp_done(void *s, int res)
566 struct srb *sp = (struct srb *)s;
567 struct scsi_qla_host *vha = sp->vha;
570 ql_dbg(ql_dbg_disc, vha, 0x20f2,
571 "Async done-%s res %x %8phC type %d\n",
572 sp->name, res, sp->fcport->port_name, sp->type);
574 spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
575 sp->fcport->flags &= ~FCF_ASYNC_SENT;
576 sp->fcport->chip_reset = vha->hw->base_qpair->chip_reset;
580 sp->fcport->login_gen++;
581 sp->fcport->fw_login_state = DSC_LS_PLOGI_COMP;
582 sp->fcport->logout_on_delete = 1;
583 sp->fcport->plogi_nack_done_deadline = jiffies + HZ;
584 sp->fcport->send_els_logo = 0;
588 sp->fcport->fw_login_state = DSC_LS_PRLI_COMP;
589 sp->fcport->deleted = 0;
590 sp->fcport->send_els_logo = 0;
592 if (!sp->fcport->login_succ &&
593 !IS_SW_RESV_ADDR(sp->fcport->d_id)) {
594 sp->fcport->login_succ = 1;
597 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
598 qla24xx_sched_upd_fcport(sp->fcport);
599 spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
601 sp->fcport->login_retry = 0;
602 sp->fcport->disc_state = DSC_LOGIN_COMPLETE;
603 sp->fcport->deleted = 0;
604 sp->fcport->logout_on_delete = 1;
609 sp->fcport->login_gen++;
610 sp->fcport->fw_login_state = DSC_LS_PORT_UNAVAIL;
611 qlt_logo_completion_handler(sp->fcport, MBS_COMMAND_COMPLETE);
614 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
619 int qla24xx_async_notify_ack(scsi_qla_host_t *vha, fc_port_t *fcport,
620 struct imm_ntfy_from_isp *ntfy, int type)
622 int rval = QLA_FUNCTION_FAILED;
626 fcport->flags |= FCF_ASYNC_SENT;
629 fcport->fw_login_state = DSC_LS_PLOGI_PEND;
633 fcport->fw_login_state = DSC_LS_PRLI_PEND;
638 fcport->fw_login_state = DSC_LS_LOGO_PEND;
643 sp = qla2x00_get_sp(vha, fcport, GFP_ATOMIC);
650 sp->u.iocb_cmd.timeout = qla2x00_async_iocb_timeout;
651 qla2x00_init_timer(sp, qla2x00_get_async_timeout(vha)+2);
653 sp->u.iocb_cmd.u.nack.ntfy = ntfy;
654 sp->done = qla2x00_async_nack_sp_done;
656 ql_dbg(ql_dbg_disc, vha, 0x20f4,
657 "Async-%s %8phC hndl %x %s\n",
658 sp->name, fcport->port_name, sp->handle, c);
660 rval = qla2x00_start_sp(sp);
661 if (rval != QLA_SUCCESS)
669 fcport->flags &= ~FCF_ASYNC_SENT;
673 void qla24xx_do_nack_work(struct scsi_qla_host *vha, struct qla_work_evt *e)
677 switch (e->u.nack.type) {
679 t = e->u.nack.fcport;
680 flush_work(&t->del_work);
681 flush_work(&t->free_work);
682 mutex_lock(&vha->vha_tgt.tgt_mutex);
683 t = qlt_create_sess(vha, e->u.nack.fcport, 0);
684 mutex_unlock(&vha->vha_tgt.tgt_mutex);
686 ql_log(ql_log_info, vha, 0xd034,
687 "%s create sess success %p", __func__, t);
688 /* create sess has an extra kref */
689 vha->hw->tgt.tgt_ops->put_sess(e->u.nack.fcport);
693 qla24xx_async_notify_ack(vha, e->u.nack.fcport,
694 (struct imm_ntfy_from_isp *)e->u.nack.iocb, e->u.nack.type);
697 void qla24xx_delete_sess_fn(struct work_struct *work)
699 fc_port_t *fcport = container_of(work, struct fc_port, del_work);
700 struct qla_hw_data *ha = fcport->vha->hw;
702 if (fcport->se_sess) {
703 ha->tgt.tgt_ops->shutdown_sess(fcport);
704 ha->tgt.tgt_ops->put_sess(fcport);
706 qlt_unreg_sess(fcport);
711 * Called from qla2x00_reg_remote_port()
713 void qlt_fc_port_added(struct scsi_qla_host *vha, fc_port_t *fcport)
715 struct qla_hw_data *ha = vha->hw;
716 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
717 struct fc_port *sess = fcport;
720 if (!vha->hw->tgt.tgt_ops)
723 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
725 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
729 if (fcport->disc_state == DSC_DELETE_PEND) {
730 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
734 if (!sess->se_sess) {
735 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
737 mutex_lock(&vha->vha_tgt.tgt_mutex);
738 sess = qlt_create_sess(vha, fcport, false);
739 mutex_unlock(&vha->vha_tgt.tgt_mutex);
741 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
743 if (fcport->fw_login_state == DSC_LS_PRLI_COMP) {
744 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
748 if (!kref_get_unless_zero(&sess->sess_kref)) {
749 ql_dbg(ql_dbg_disc, vha, 0x2107,
750 "%s: kref_get fail sess %8phC \n",
751 __func__, sess->port_name);
752 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
756 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04c,
757 "qla_target(%u): %ssession for port %8phC "
758 "(loop ID %d) reappeared\n", vha->vp_idx,
759 sess->local ? "local " : "", sess->port_name, sess->loop_id);
761 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf007,
762 "Reappeared sess %p\n", sess);
764 ha->tgt.tgt_ops->update_sess(sess, fcport->d_id,
766 (fcport->flags & FCF_CONF_COMP_SUPPORTED));
769 if (sess && sess->local) {
770 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04d,
771 "qla_target(%u): local session for "
772 "port %8phC (loop ID %d) became global\n", vha->vp_idx,
773 fcport->port_name, sess->loop_id);
776 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
778 ha->tgt.tgt_ops->put_sess(sess);
782 * This is a zero-base ref-counting solution, since hardware_lock
783 * guarantees that ref_count is not modified concurrently.
784 * Upon successful return content of iocb is undefined
786 static struct qlt_plogi_ack_t *
787 qlt_plogi_ack_find_add(struct scsi_qla_host *vha, port_id_t *id,
788 struct imm_ntfy_from_isp *iocb)
790 struct qlt_plogi_ack_t *pla;
792 lockdep_assert_held(&vha->hw->hardware_lock);
794 list_for_each_entry(pla, &vha->plogi_ack_list, list) {
795 if (pla->id.b24 == id->b24) {
796 ql_dbg(ql_dbg_disc + ql_dbg_verbose, vha, 0x210d,
797 "%s %d %8phC Term INOT due to new INOT",
799 pla->iocb.u.isp24.port_name);
800 qlt_send_term_imm_notif(vha, &pla->iocb, 1);
801 memcpy(&pla->iocb, iocb, sizeof(pla->iocb));
806 pla = kmem_cache_zalloc(qla_tgt_plogi_cachep, GFP_ATOMIC);
808 ql_dbg(ql_dbg_async, vha, 0x5088,
809 "qla_target(%d): Allocation of plogi_ack failed\n",
814 memcpy(&pla->iocb, iocb, sizeof(pla->iocb));
816 list_add_tail(&pla->list, &vha->plogi_ack_list);
821 void qlt_plogi_ack_unref(struct scsi_qla_host *vha,
822 struct qlt_plogi_ack_t *pla)
824 struct imm_ntfy_from_isp *iocb = &pla->iocb;
827 fc_port_t *fcport = pla->fcport;
829 BUG_ON(!pla->ref_count);
835 ql_dbg(ql_dbg_disc, vha, 0x5089,
836 "Sending PLOGI ACK to wwn %8phC s_id %02x:%02x:%02x loop_id %#04x"
837 " exch %#x ox_id %#x\n", iocb->u.isp24.port_name,
838 iocb->u.isp24.port_id[2], iocb->u.isp24.port_id[1],
839 iocb->u.isp24.port_id[0],
840 le16_to_cpu(iocb->u.isp24.nport_handle),
841 iocb->u.isp24.exchange_address, iocb->ox_id);
843 port_id.b.domain = iocb->u.isp24.port_id[2];
844 port_id.b.area = iocb->u.isp24.port_id[1];
845 port_id.b.al_pa = iocb->u.isp24.port_id[0];
846 port_id.b.rsvd_1 = 0;
848 loop_id = le16_to_cpu(iocb->u.isp24.nport_handle);
850 fcport->loop_id = loop_id;
851 fcport->d_id = port_id;
852 if (iocb->u.isp24.status_subcode == ELS_PLOGI)
853 qla24xx_post_nack_work(vha, fcport, iocb, SRB_NACK_PLOGI);
855 qla24xx_post_nack_work(vha, fcport, iocb, SRB_NACK_PRLI);
857 list_for_each_entry(fcport, &vha->vp_fcports, list) {
858 if (fcport->plogi_link[QLT_PLOGI_LINK_SAME_WWN] == pla)
859 fcport->plogi_link[QLT_PLOGI_LINK_SAME_WWN] = NULL;
860 if (fcport->plogi_link[QLT_PLOGI_LINK_CONFLICT] == pla)
861 fcport->plogi_link[QLT_PLOGI_LINK_CONFLICT] = NULL;
864 list_del(&pla->list);
865 kmem_cache_free(qla_tgt_plogi_cachep, pla);
869 qlt_plogi_ack_link(struct scsi_qla_host *vha, struct qlt_plogi_ack_t *pla,
870 struct fc_port *sess, enum qlt_plogi_link_t link)
872 struct imm_ntfy_from_isp *iocb = &pla->iocb;
873 /* Inc ref_count first because link might already be pointing at pla */
876 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf097,
877 "Linking sess %p [%d] wwn %8phC with PLOGI ACK to wwn %8phC"
878 " s_id %02x:%02x:%02x, ref=%d pla %p link %d\n",
879 sess, link, sess->port_name,
880 iocb->u.isp24.port_name, iocb->u.isp24.port_id[2],
881 iocb->u.isp24.port_id[1], iocb->u.isp24.port_id[0],
882 pla->ref_count, pla, link);
884 if (link == QLT_PLOGI_LINK_CONFLICT) {
885 switch (sess->disc_state) {
887 case DSC_DELETE_PEND:
895 if (sess->plogi_link[link])
896 qlt_plogi_ack_unref(vha, sess->plogi_link[link]);
898 if (link == QLT_PLOGI_LINK_SAME_WWN)
901 sess->plogi_link[link] = pla;
905 /* These fields must be initialized by the caller */
908 * number of cmds dropped while we were waiting for
909 * initiator to ack LOGO initialize to 1 if LOGO is
910 * triggered by a command, otherwise, to 0
914 /* These fields are used by callee */
915 struct list_head list;
919 qlt_send_first_logo(struct scsi_qla_host *vha, qlt_port_logo_t *logo)
921 qlt_port_logo_t *tmp;
924 mutex_lock(&vha->vha_tgt.tgt_mutex);
926 list_for_each_entry(tmp, &vha->logo_list, list) {
927 if (tmp->id.b24 == logo->id.b24) {
928 tmp->cmd_count += logo->cmd_count;
929 mutex_unlock(&vha->vha_tgt.tgt_mutex);
934 list_add_tail(&logo->list, &vha->logo_list);
936 mutex_unlock(&vha->vha_tgt.tgt_mutex);
938 res = qla24xx_els_dcmd_iocb(vha, ELS_DCMD_LOGO, logo->id);
940 mutex_lock(&vha->vha_tgt.tgt_mutex);
941 list_del(&logo->list);
942 mutex_unlock(&vha->vha_tgt.tgt_mutex);
944 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf098,
945 "Finished LOGO to %02x:%02x:%02x, dropped %d cmds, res = %#x\n",
946 logo->id.b.domain, logo->id.b.area, logo->id.b.al_pa,
947 logo->cmd_count, res);
950 void qlt_free_session_done(struct work_struct *work)
952 struct fc_port *sess = container_of(work, struct fc_port,
954 struct qla_tgt *tgt = sess->tgt;
955 struct scsi_qla_host *vha = sess->vha;
956 struct qla_hw_data *ha = vha->hw;
958 bool logout_started = false;
959 scsi_qla_host_t *base_vha;
960 struct qlt_plogi_ack_t *own =
961 sess->plogi_link[QLT_PLOGI_LINK_SAME_WWN];
963 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf084,
964 "%s: se_sess %p / sess %p from port %8phC loop_id %#04x"
965 " s_id %02x:%02x:%02x logout %d keep %d els_logo %d\n",
966 __func__, sess->se_sess, sess, sess->port_name, sess->loop_id,
967 sess->d_id.b.domain, sess->d_id.b.area, sess->d_id.b.al_pa,
968 sess->logout_on_delete, sess->keep_nport_handle,
969 sess->send_els_logo);
971 if (!IS_SW_RESV_ADDR(sess->d_id)) {
972 qla2x00_mark_device_lost(vha, sess, 0, 0);
974 if (sess->send_els_logo) {
975 qlt_port_logo_t logo;
977 logo.id = sess->d_id;
980 qlt_send_first_logo(vha, &logo);
981 sess->send_els_logo = 0;
984 if (sess->logout_on_delete && sess->loop_id != FC_NO_LOOP_ID) {
989 (own->iocb.u.isp24.status_subcode == ELS_PLOGI))) {
990 rc = qla2x00_post_async_logout_work(vha, sess,
992 if (rc != QLA_SUCCESS)
993 ql_log(ql_log_warn, vha, 0xf085,
994 "Schedule logo failed sess %p rc %d\n",
997 logout_started = true;
998 } else if (own && (own->iocb.u.isp24.status_subcode ==
999 ELS_PRLI) && ha->flags.rida_fmt2) {
1000 rc = qla2x00_post_async_prlo_work(vha, sess,
1002 if (rc != QLA_SUCCESS)
1003 ql_log(ql_log_warn, vha, 0xf085,
1004 "Schedule PRLO failed sess %p rc %d\n",
1007 logout_started = true;
1009 } /* if sess->logout_on_delete */
1011 if (sess->nvme_flag & NVME_FLAG_REGISTERED &&
1012 !(sess->nvme_flag & NVME_FLAG_DELETING)) {
1013 sess->nvme_flag |= NVME_FLAG_DELETING;
1014 qla_nvme_unregister_remote_port(sess);
1019 * Release the target session for FC Nexus from fabric module code.
1021 if (sess->se_sess != NULL)
1022 ha->tgt.tgt_ops->free_session(sess);
1024 if (logout_started) {
1025 bool traced = false;
1027 while (!READ_ONCE(sess->logout_completed)) {
1029 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf086,
1030 "%s: waiting for sess %p logout\n",
1037 ql_dbg(ql_dbg_disc, vha, 0xf087,
1038 "%s: sess %p logout completed\n", __func__, sess);
1041 if (sess->logo_ack_needed) {
1042 sess->logo_ack_needed = 0;
1043 qla24xx_async_notify_ack(vha, sess,
1044 (struct imm_ntfy_from_isp *)sess->iocb, SRB_NACK_LOGO);
1047 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
1048 if (sess->se_sess) {
1049 sess->se_sess = NULL;
1050 if (tgt && !IS_SW_RESV_ADDR(sess->d_id))
1054 sess->disc_state = DSC_DELETED;
1055 sess->fw_login_state = DSC_LS_PORT_UNAVAIL;
1056 sess->deleted = QLA_SESS_DELETED;
1058 if (sess->login_succ && !IS_SW_RESV_ADDR(sess->d_id)) {
1059 vha->fcport_count--;
1060 sess->login_succ = 0;
1063 qla2x00_clear_loop_id(sess);
1065 if (sess->conflict) {
1066 sess->conflict->login_pause = 0;
1067 sess->conflict = NULL;
1068 if (!test_bit(UNLOADING, &vha->dpc_flags))
1069 set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
1073 struct qlt_plogi_ack_t *con =
1074 sess->plogi_link[QLT_PLOGI_LINK_CONFLICT];
1075 struct imm_ntfy_from_isp *iocb;
1077 own = sess->plogi_link[QLT_PLOGI_LINK_SAME_WWN];
1081 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf099,
1082 "se_sess %p / sess %p port %8phC is gone,"
1083 " %s (ref=%d), releasing PLOGI for %8phC (ref=%d)\n",
1084 sess->se_sess, sess, sess->port_name,
1085 own ? "releasing own PLOGI" : "no own PLOGI pending",
1086 own ? own->ref_count : -1,
1087 iocb->u.isp24.port_name, con->ref_count);
1088 qlt_plogi_ack_unref(vha, con);
1089 sess->plogi_link[QLT_PLOGI_LINK_CONFLICT] = NULL;
1091 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf09a,
1092 "se_sess %p / sess %p port %8phC is gone, %s (ref=%d)\n",
1093 sess->se_sess, sess, sess->port_name,
1094 own ? "releasing own PLOGI" :
1095 "no own PLOGI pending",
1096 own ? own->ref_count : -1);
1100 sess->fw_login_state = DSC_LS_PLOGI_PEND;
1101 qlt_plogi_ack_unref(vha, own);
1102 sess->plogi_link[QLT_PLOGI_LINK_SAME_WWN] = NULL;
1106 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
1108 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf001,
1109 "Unregistration of sess %p %8phC finished fcp_cnt %d\n",
1110 sess, sess->port_name, vha->fcport_count);
1112 if (tgt && (tgt->sess_count == 0))
1113 wake_up_all(&tgt->waitQ);
1115 if (vha->fcport_count == 0)
1116 wake_up_all(&vha->fcport_waitQ);
1118 base_vha = pci_get_drvdata(ha->pdev);
1120 sess->free_pending = 0;
1122 if (test_bit(PFLG_DRIVER_REMOVING, &base_vha->pci_flags))
1125 if ((!tgt || !tgt->tgt_stop) && !LOOP_TRANSITION(vha)) {
1126 switch (vha->host->active_mode) {
1127 case MODE_INITIATOR:
1129 set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
1130 qla2xxx_wake_dpc(vha);
1140 /* ha->tgt.sess_lock supposed to be held on entry */
1141 void qlt_unreg_sess(struct fc_port *sess)
1143 struct scsi_qla_host *vha = sess->vha;
1144 unsigned long flags;
1146 ql_dbg(ql_dbg_disc, sess->vha, 0x210a,
1147 "%s sess %p for deletion %8phC\n",
1148 __func__, sess, sess->port_name);
1150 spin_lock_irqsave(&sess->vha->work_lock, flags);
1151 if (sess->free_pending) {
1152 spin_unlock_irqrestore(&sess->vha->work_lock, flags);
1155 sess->free_pending = 1;
1156 spin_unlock_irqrestore(&sess->vha->work_lock, flags);
1159 vha->hw->tgt.tgt_ops->clear_nacl_from_fcport_map(sess);
1161 sess->deleted = QLA_SESS_DELETION_IN_PROGRESS;
1162 sess->disc_state = DSC_DELETE_PEND;
1163 sess->last_rscn_gen = sess->rscn_gen;
1164 sess->last_login_gen = sess->login_gen;
1166 INIT_WORK(&sess->free_work, qlt_free_session_done);
1167 schedule_work(&sess->free_work);
1169 EXPORT_SYMBOL(qlt_unreg_sess);
1171 static int qlt_reset(struct scsi_qla_host *vha, void *iocb, int mcmd)
1173 struct qla_hw_data *ha = vha->hw;
1174 struct fc_port *sess = NULL;
1177 struct imm_ntfy_from_isp *n = (struct imm_ntfy_from_isp *)iocb;
1178 unsigned long flags;
1180 loop_id = le16_to_cpu(n->u.isp24.nport_handle);
1181 if (loop_id == 0xFFFF) {
1183 atomic_inc(&vha->vha_tgt.qla_tgt->tgt_global_resets_count);
1184 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
1185 qlt_clear_tgt_db(vha->vha_tgt.qla_tgt);
1186 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
1188 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
1189 sess = ha->tgt.tgt_ops->find_sess_by_loop_id(vha, loop_id);
1190 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
1193 ql_dbg(ql_dbg_tgt, vha, 0xe000,
1194 "Using sess for qla_tgt_reset: %p\n", sess);
1200 ql_dbg(ql_dbg_tgt, vha, 0xe047,
1201 "scsi(%ld): resetting (session %p from port %8phC mcmd %x, "
1202 "loop_id %d)\n", vha->host_no, sess, sess->port_name,
1205 return qlt_issue_task_mgmt(sess, 0, mcmd, iocb, QLA24XX_MGMT_SEND_NACK);
1208 static void qla24xx_chk_fcp_state(struct fc_port *sess)
1210 if (sess->chip_reset != sess->vha->hw->base_qpair->chip_reset) {
1211 sess->logout_on_delete = 0;
1212 sess->logo_ack_needed = 0;
1213 sess->fw_login_state = DSC_LS_PORT_UNAVAIL;
1217 void qlt_schedule_sess_for_deletion(struct fc_port *sess)
1219 struct qla_tgt *tgt = sess->tgt;
1220 unsigned long flags;
1223 switch (sess->disc_state) {
1224 case DSC_DELETE_PEND:
1227 if (tgt && tgt->tgt_stop && (tgt->sess_count == 0))
1228 wake_up_all(&tgt->waitQ);
1229 if (sess->vha->fcport_count == 0)
1230 wake_up_all(&sess->vha->fcport_waitQ);
1232 if (!sess->plogi_link[QLT_PLOGI_LINK_SAME_WWN] &&
1233 !sess->plogi_link[QLT_PLOGI_LINK_CONFLICT])
1236 case DSC_UPD_FCPORT:
1238 * This port is not done reporting to upper layer.
1241 sess->next_disc_state = DSC_DELETE_PEND;
1242 sec = jiffies_to_msecs(jiffies -
1243 sess->jiffies_at_registration)/1000;
1244 if (sess->sec_since_registration < sec && sec && !(sec % 5)) {
1245 sess->sec_since_registration = sec;
1246 ql_dbg(ql_dbg_disc, sess->vha, 0xffff,
1247 "%s %8phC : Slow Rport registration(%d Sec)\n",
1248 __func__, sess->port_name, sec);
1255 spin_lock_irqsave(&sess->vha->work_lock, flags);
1256 if (sess->deleted == QLA_SESS_DELETION_IN_PROGRESS) {
1257 spin_unlock_irqrestore(&sess->vha->work_lock, flags);
1260 sess->deleted = QLA_SESS_DELETION_IN_PROGRESS;
1261 spin_unlock_irqrestore(&sess->vha->work_lock, flags);
1263 sess->disc_state = DSC_DELETE_PEND;
1265 qla24xx_chk_fcp_state(sess);
1267 ql_dbg(ql_dbg_tgt, sess->vha, 0xe001,
1268 "Scheduling sess %p for deletion %8phC\n",
1269 sess, sess->port_name);
1271 INIT_WORK(&sess->del_work, qla24xx_delete_sess_fn);
1272 WARN_ON(!queue_work(sess->vha->hw->wq, &sess->del_work));
1275 static void qlt_clear_tgt_db(struct qla_tgt *tgt)
1277 struct fc_port *sess;
1278 scsi_qla_host_t *vha = tgt->vha;
1280 list_for_each_entry(sess, &vha->vp_fcports, list) {
1282 qlt_schedule_sess_for_deletion(sess);
1285 /* At this point tgt could be already dead */
1288 static int qla24xx_get_loop_id(struct scsi_qla_host *vha, const uint8_t *s_id,
1291 struct qla_hw_data *ha = vha->hw;
1292 dma_addr_t gid_list_dma;
1293 struct gid_list_info *gid_list, *gid;
1297 gid_list = dma_alloc_coherent(&ha->pdev->dev, qla2x00_gid_list_size(ha),
1298 &gid_list_dma, GFP_KERNEL);
1300 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf044,
1301 "qla_target(%d): DMA Alloc failed of %u\n",
1302 vha->vp_idx, qla2x00_gid_list_size(ha));
1306 /* Get list of logged in devices */
1307 rc = qla24xx_gidlist_wait(vha, gid_list, gid_list_dma, &entries);
1308 if (rc != QLA_SUCCESS) {
1309 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf045,
1310 "qla_target(%d): get_id_list() failed: %x\n",
1313 goto out_free_id_list;
1318 for (i = 0; i < entries; i++) {
1319 if ((gid->al_pa == s_id[2]) &&
1320 (gid->area == s_id[1]) &&
1321 (gid->domain == s_id[0])) {
1322 *loop_id = le16_to_cpu(gid->loop_id);
1326 gid = (void *)gid + ha->gid_list_info_size;
1330 dma_free_coherent(&ha->pdev->dev, qla2x00_gid_list_size(ha),
1331 gid_list, gid_list_dma);
1336 * Adds an extra ref to allow to drop hw lock after adding sess to the list.
1337 * Caller must put it.
1339 static struct fc_port *qlt_create_sess(
1340 struct scsi_qla_host *vha,
1344 struct qla_hw_data *ha = vha->hw;
1345 struct fc_port *sess = fcport;
1346 unsigned long flags;
1348 if (vha->vha_tgt.qla_tgt->tgt_stop)
1351 if (fcport->se_sess) {
1352 if (!kref_get_unless_zero(&sess->sess_kref)) {
1353 ql_dbg(ql_dbg_disc, vha, 0x20f6,
1354 "%s: kref_get_unless_zero failed for %8phC\n",
1355 __func__, sess->port_name);
1360 sess->tgt = vha->vha_tgt.qla_tgt;
1361 sess->local = local;
1364 * Under normal circumstances we want to logout from firmware when
1365 * session eventually ends and release corresponding nport handle.
1366 * In the exception cases (e.g. when new PLOGI is waiting) corresponding
1367 * code will adjust these flags as necessary.
1369 sess->logout_on_delete = 1;
1370 sess->keep_nport_handle = 0;
1371 sess->logout_completed = 0;
1373 if (ha->tgt.tgt_ops->check_initiator_node_acl(vha,
1374 &fcport->port_name[0], sess) < 0) {
1375 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf015,
1376 "(%d) %8phC check_initiator_node_acl failed\n",
1377 vha->vp_idx, fcport->port_name);
1380 kref_init(&fcport->sess_kref);
1382 * Take an extra reference to ->sess_kref here to handle
1383 * fc_port access across ->tgt.sess_lock reaquire.
1385 if (!kref_get_unless_zero(&sess->sess_kref)) {
1386 ql_dbg(ql_dbg_disc, vha, 0x20f7,
1387 "%s: kref_get_unless_zero failed for %8phC\n",
1388 __func__, sess->port_name);
1392 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
1393 if (!IS_SW_RESV_ADDR(sess->d_id))
1394 vha->vha_tgt.qla_tgt->sess_count++;
1396 qlt_do_generation_tick(vha, &sess->generation);
1397 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
1400 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf006,
1401 "Adding sess %p se_sess %p to tgt %p sess_count %d\n",
1402 sess, sess->se_sess, vha->vha_tgt.qla_tgt,
1403 vha->vha_tgt.qla_tgt->sess_count);
1405 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04b,
1406 "qla_target(%d): %ssession for wwn %8phC (loop_id %d, "
1407 "s_id %x:%x:%x, confirmed completion %ssupported) added\n",
1408 vha->vp_idx, local ? "local " : "", fcport->port_name,
1409 fcport->loop_id, sess->d_id.b.domain, sess->d_id.b.area,
1410 sess->d_id.b.al_pa, sess->conf_compl_supported ? "" : "not ");
1416 * max_gen - specifies maximum session generation
1417 * at which this deletion requestion is still valid
1420 qlt_fc_port_deleted(struct scsi_qla_host *vha, fc_port_t *fcport, int max_gen)
1422 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
1423 struct fc_port *sess = fcport;
1424 unsigned long flags;
1426 if (!vha->hw->tgt.tgt_ops)
1432 spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
1433 if (tgt->tgt_stop) {
1434 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
1437 if (!sess->se_sess) {
1438 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
1442 if (max_gen - sess->generation < 0) {
1443 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
1444 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf092,
1445 "Ignoring stale deletion request for se_sess %p / sess %p"
1446 " for port %8phC, req_gen %d, sess_gen %d\n",
1447 sess->se_sess, sess, sess->port_name, max_gen,
1452 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf008, "qla_tgt_fc_port_deleted %p", sess);
1455 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
1456 qlt_schedule_sess_for_deletion(sess);
1459 static inline int test_tgt_sess_count(struct qla_tgt *tgt)
1461 struct qla_hw_data *ha = tgt->ha;
1462 unsigned long flags;
1465 * We need to protect against race, when tgt is freed before or
1468 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
1469 ql_dbg(ql_dbg_tgt, tgt->vha, 0xe002,
1470 "tgt %p, sess_count=%d\n",
1471 tgt, tgt->sess_count);
1472 res = (tgt->sess_count == 0);
1473 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
1478 /* Called by tcm_qla2xxx configfs code */
1479 int qlt_stop_phase1(struct qla_tgt *tgt)
1481 struct scsi_qla_host *vha = tgt->vha;
1482 struct qla_hw_data *ha = tgt->ha;
1483 unsigned long flags;
1485 mutex_lock(&ha->optrom_mutex);
1486 mutex_lock(&qla_tgt_mutex);
1488 if (tgt->tgt_stop || tgt->tgt_stopped) {
1489 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04e,
1490 "Already in tgt->tgt_stop or tgt_stopped state\n");
1491 mutex_unlock(&qla_tgt_mutex);
1492 mutex_unlock(&ha->optrom_mutex);
1496 ql_dbg(ql_dbg_tgt_mgt, vha, 0xe003, "Stopping target for host %ld(%p)\n",
1499 * Mutex needed to sync with qla_tgt_fc_port_[added,deleted].
1500 * Lock is needed, because we still can get an incoming packet.
1502 mutex_lock(&vha->vha_tgt.tgt_mutex);
1504 qlt_clear_tgt_db(tgt);
1505 mutex_unlock(&vha->vha_tgt.tgt_mutex);
1506 mutex_unlock(&qla_tgt_mutex);
1508 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf009,
1509 "Waiting for sess works (tgt %p)", tgt);
1510 spin_lock_irqsave(&tgt->sess_work_lock, flags);
1511 while (!list_empty(&tgt->sess_works_list)) {
1512 spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
1513 flush_scheduled_work();
1514 spin_lock_irqsave(&tgt->sess_work_lock, flags);
1516 spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
1518 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00a,
1519 "Waiting for tgt %p: sess_count=%d\n", tgt, tgt->sess_count);
1521 wait_event_timeout(tgt->waitQ, test_tgt_sess_count(tgt), 10*HZ);
1524 if (!ha->flags.host_shutting_down &&
1525 (qla_tgt_mode_enabled(vha) || qla_dual_mode_enabled(vha)))
1526 qlt_disable_vha(vha);
1528 /* Wait for sessions to clear out (just in case) */
1529 wait_event_timeout(tgt->waitQ, test_tgt_sess_count(tgt), 10*HZ);
1530 mutex_unlock(&ha->optrom_mutex);
1534 EXPORT_SYMBOL(qlt_stop_phase1);
1536 /* Called by tcm_qla2xxx configfs code */
1537 void qlt_stop_phase2(struct qla_tgt *tgt)
1539 scsi_qla_host_t *vha = tgt->vha;
1541 if (tgt->tgt_stopped) {
1542 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04f,
1543 "Already in tgt->tgt_stopped state\n");
1547 if (!tgt->tgt_stop) {
1548 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00b,
1549 "%s: phase1 stop is not completed\n", __func__);
1554 mutex_lock(&vha->vha_tgt.tgt_mutex);
1556 tgt->tgt_stopped = 1;
1557 mutex_unlock(&vha->vha_tgt.tgt_mutex);
1559 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00c, "Stop of tgt %p finished\n",
1562 switch (vha->qlini_mode) {
1563 case QLA2XXX_INI_MODE_EXCLUSIVE:
1564 vha->flags.online = 1;
1565 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
1571 EXPORT_SYMBOL(qlt_stop_phase2);
1573 /* Called from qlt_remove_target() -> qla2x00_remove_one() */
1574 static void qlt_release(struct qla_tgt *tgt)
1576 scsi_qla_host_t *vha = tgt->vha;
1580 struct qla_qpair_hint *h;
1581 struct qla_hw_data *ha = vha->hw;
1583 if (!tgt->tgt_stop && !tgt->tgt_stopped)
1584 qlt_stop_phase1(tgt);
1586 if (!tgt->tgt_stopped)
1587 qlt_stop_phase2(tgt);
1589 for (i = 0; i < vha->hw->max_qpairs + 1; i++) {
1590 unsigned long flags;
1592 h = &tgt->qphints[i];
1594 spin_lock_irqsave(h->qpair->qp_lock_ptr, flags);
1595 list_del(&h->hint_elem);
1596 spin_unlock_irqrestore(h->qpair->qp_lock_ptr, flags);
1600 kfree(tgt->qphints);
1601 mutex_lock(&qla_tgt_mutex);
1602 list_del(&vha->vha_tgt.qla_tgt->tgt_list_entry);
1603 mutex_unlock(&qla_tgt_mutex);
1605 btree_for_each_safe64(&tgt->lun_qpair_map, key, node)
1606 btree_remove64(&tgt->lun_qpair_map, key);
1608 btree_destroy64(&tgt->lun_qpair_map);
1611 if (ha->tgt.tgt_ops &&
1612 ha->tgt.tgt_ops->remove_target &&
1613 vha->vha_tgt.target_lport_ptr)
1614 ha->tgt.tgt_ops->remove_target(vha);
1616 vha->vha_tgt.qla_tgt = NULL;
1618 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00d,
1619 "Release of tgt %p finished\n", tgt);
1624 /* ha->hardware_lock supposed to be held on entry */
1625 static int qlt_sched_sess_work(struct qla_tgt *tgt, int type,
1626 const void *param, unsigned int param_size)
1628 struct qla_tgt_sess_work_param *prm;
1629 unsigned long flags;
1631 prm = kzalloc(sizeof(*prm), GFP_ATOMIC);
1633 ql_dbg(ql_dbg_tgt_mgt, tgt->vha, 0xf050,
1634 "qla_target(%d): Unable to create session "
1635 "work, command will be refused", 0);
1639 ql_dbg(ql_dbg_tgt_mgt, tgt->vha, 0xf00e,
1640 "Scheduling work (type %d, prm %p)"
1641 " to find session for param %p (size %d, tgt %p)\n",
1642 type, prm, param, param_size, tgt);
1645 memcpy(&prm->tm_iocb, param, param_size);
1647 spin_lock_irqsave(&tgt->sess_work_lock, flags);
1648 list_add_tail(&prm->sess_works_list_entry, &tgt->sess_works_list);
1649 spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
1651 schedule_work(&tgt->sess_work);
1657 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
1659 static void qlt_send_notify_ack(struct qla_qpair *qpair,
1660 struct imm_ntfy_from_isp *ntfy,
1661 uint32_t add_flags, uint16_t resp_code, int resp_code_valid,
1662 uint16_t srr_flags, uint16_t srr_reject_code, uint8_t srr_explan)
1664 struct scsi_qla_host *vha = qpair->vha;
1665 struct qla_hw_data *ha = vha->hw;
1667 struct nack_to_isp *nack;
1669 if (!ha->flags.fw_started)
1672 ql_dbg(ql_dbg_tgt, vha, 0xe004, "Sending NOTIFY_ACK (ha=%p)\n", ha);
1674 pkt = (request_t *)__qla2x00_alloc_iocbs(qpair, NULL);
1676 ql_dbg(ql_dbg_tgt, vha, 0xe049,
1677 "qla_target(%d): %s failed: unable to allocate "
1678 "request packet\n", vha->vp_idx, __func__);
1682 if (vha->vha_tgt.qla_tgt != NULL)
1683 vha->vha_tgt.qla_tgt->notify_ack_expected++;
1685 pkt->entry_type = NOTIFY_ACK_TYPE;
1686 pkt->entry_count = 1;
1688 nack = (struct nack_to_isp *)pkt;
1689 nack->ox_id = ntfy->ox_id;
1691 nack->u.isp24.handle = QLA_TGT_SKIP_HANDLE;
1692 nack->u.isp24.nport_handle = ntfy->u.isp24.nport_handle;
1693 if (le16_to_cpu(ntfy->u.isp24.status) == IMM_NTFY_ELS) {
1694 nack->u.isp24.flags = ntfy->u.isp24.flags &
1695 cpu_to_le32(NOTIFY24XX_FLAGS_PUREX_IOCB);
1697 nack->u.isp24.srr_rx_id = ntfy->u.isp24.srr_rx_id;
1698 nack->u.isp24.status = ntfy->u.isp24.status;
1699 nack->u.isp24.status_subcode = ntfy->u.isp24.status_subcode;
1700 nack->u.isp24.fw_handle = ntfy->u.isp24.fw_handle;
1701 nack->u.isp24.exchange_address = ntfy->u.isp24.exchange_address;
1702 nack->u.isp24.srr_rel_offs = ntfy->u.isp24.srr_rel_offs;
1703 nack->u.isp24.srr_ui = ntfy->u.isp24.srr_ui;
1704 nack->u.isp24.srr_flags = cpu_to_le16(srr_flags);
1705 nack->u.isp24.srr_reject_code = srr_reject_code;
1706 nack->u.isp24.srr_reject_code_expl = srr_explan;
1707 nack->u.isp24.vp_index = ntfy->u.isp24.vp_index;
1709 ql_dbg(ql_dbg_tgt, vha, 0xe005,
1710 "qla_target(%d): Sending 24xx Notify Ack %d\n",
1711 vha->vp_idx, nack->u.isp24.status);
1713 /* Memory Barrier */
1715 qla2x00_start_iocbs(vha, qpair->req);
1718 static int qlt_build_abts_resp_iocb(struct qla_tgt_mgmt_cmd *mcmd)
1720 struct scsi_qla_host *vha = mcmd->vha;
1721 struct qla_hw_data *ha = vha->hw;
1722 struct abts_resp_to_24xx *resp;
1726 struct abts_recv_from_24xx *abts = &mcmd->orig_iocb.abts;
1727 struct qla_qpair *qpair = mcmd->qpair;
1729 ql_dbg(ql_dbg_tgt, vha, 0xe006,
1730 "Sending task mgmt ABTS response (ha=%p, status=%x)\n",
1731 ha, mcmd->fc_tm_rsp);
1733 rc = qlt_check_reserve_free_req(qpair, 1);
1735 ql_dbg(ql_dbg_tgt, vha, 0xe04a,
1736 "qla_target(%d): %s failed: unable to allocate request packet\n",
1737 vha->vp_idx, __func__);
1741 resp = (struct abts_resp_to_24xx *)qpair->req->ring_ptr;
1742 memset(resp, 0, sizeof(*resp));
1744 h = qlt_make_handle(qpair);
1745 if (unlikely(h == QLA_TGT_NULL_HANDLE)) {
1747 * CTIO type 7 from the firmware doesn't provide a way to
1748 * know the initiator's LOOP ID, hence we can't find
1749 * the session and, so, the command.
1753 qpair->req->outstanding_cmds[h] = (srb_t *)mcmd;
1756 resp->handle = MAKE_HANDLE(qpair->req->id, h);
1757 resp->entry_type = ABTS_RESP_24XX;
1758 resp->entry_count = 1;
1759 resp->nport_handle = abts->nport_handle;
1760 resp->vp_index = vha->vp_idx;
1761 resp->sof_type = abts->sof_type;
1762 resp->exchange_address = abts->exchange_address;
1763 resp->fcp_hdr_le = abts->fcp_hdr_le;
1764 f_ctl = cpu_to_le32(F_CTL_EXCH_CONTEXT_RESP |
1765 F_CTL_LAST_SEQ | F_CTL_END_SEQ |
1766 F_CTL_SEQ_INITIATIVE);
1767 p = (uint8_t *)&f_ctl;
1768 resp->fcp_hdr_le.f_ctl[0] = *p++;
1769 resp->fcp_hdr_le.f_ctl[1] = *p++;
1770 resp->fcp_hdr_le.f_ctl[2] = *p;
1772 resp->fcp_hdr_le.d_id[0] = abts->fcp_hdr_le.s_id[0];
1773 resp->fcp_hdr_le.d_id[1] = abts->fcp_hdr_le.s_id[1];
1774 resp->fcp_hdr_le.d_id[2] = abts->fcp_hdr_le.s_id[2];
1775 resp->fcp_hdr_le.s_id[0] = abts->fcp_hdr_le.d_id[0];
1776 resp->fcp_hdr_le.s_id[1] = abts->fcp_hdr_le.d_id[1];
1777 resp->fcp_hdr_le.s_id[2] = abts->fcp_hdr_le.d_id[2];
1779 resp->exchange_addr_to_abort = abts->exchange_addr_to_abort;
1780 if (mcmd->fc_tm_rsp == FCP_TMF_CMPL) {
1781 resp->fcp_hdr_le.r_ctl = R_CTL_BASIC_LINK_SERV | R_CTL_B_ACC;
1782 resp->payload.ba_acct.seq_id_valid = SEQ_ID_INVALID;
1783 resp->payload.ba_acct.low_seq_cnt = 0x0000;
1784 resp->payload.ba_acct.high_seq_cnt = 0xFFFF;
1785 resp->payload.ba_acct.ox_id = abts->fcp_hdr_le.ox_id;
1786 resp->payload.ba_acct.rx_id = abts->fcp_hdr_le.rx_id;
1788 resp->fcp_hdr_le.r_ctl = R_CTL_BASIC_LINK_SERV | R_CTL_B_RJT;
1789 resp->payload.ba_rjt.reason_code =
1790 BA_RJT_REASON_CODE_UNABLE_TO_PERFORM;
1791 /* Other bytes are zero */
1794 vha->vha_tgt.qla_tgt->abts_resp_expected++;
1796 /* Memory Barrier */
1798 if (qpair->reqq_start_iocbs)
1799 qpair->reqq_start_iocbs(qpair);
1801 qla2x00_start_iocbs(vha, qpair->req);
1807 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
1809 static void qlt_24xx_send_abts_resp(struct qla_qpair *qpair,
1810 struct abts_recv_from_24xx *abts, uint32_t status,
1813 struct scsi_qla_host *vha = qpair->vha;
1814 struct qla_hw_data *ha = vha->hw;
1815 struct abts_resp_to_24xx *resp;
1819 ql_dbg(ql_dbg_tgt, vha, 0xe006,
1820 "Sending task mgmt ABTS response (ha=%p, atio=%p, status=%x\n",
1823 resp = (struct abts_resp_to_24xx *)qla2x00_alloc_iocbs_ready(qpair,
1826 ql_dbg(ql_dbg_tgt, vha, 0xe04a,
1827 "qla_target(%d): %s failed: unable to allocate "
1828 "request packet", vha->vp_idx, __func__);
1832 resp->entry_type = ABTS_RESP_24XX;
1833 resp->handle = QLA_TGT_SKIP_HANDLE;
1834 resp->entry_count = 1;
1835 resp->nport_handle = abts->nport_handle;
1836 resp->vp_index = vha->vp_idx;
1837 resp->sof_type = abts->sof_type;
1838 resp->exchange_address = abts->exchange_address;
1839 resp->fcp_hdr_le = abts->fcp_hdr_le;
1840 f_ctl = cpu_to_le32(F_CTL_EXCH_CONTEXT_RESP |
1841 F_CTL_LAST_SEQ | F_CTL_END_SEQ |
1842 F_CTL_SEQ_INITIATIVE);
1843 p = (uint8_t *)&f_ctl;
1844 resp->fcp_hdr_le.f_ctl[0] = *p++;
1845 resp->fcp_hdr_le.f_ctl[1] = *p++;
1846 resp->fcp_hdr_le.f_ctl[2] = *p;
1848 resp->fcp_hdr_le.d_id[0] = abts->fcp_hdr_le.d_id[0];
1849 resp->fcp_hdr_le.d_id[1] = abts->fcp_hdr_le.d_id[1];
1850 resp->fcp_hdr_le.d_id[2] = abts->fcp_hdr_le.d_id[2];
1851 resp->fcp_hdr_le.s_id[0] = abts->fcp_hdr_le.s_id[0];
1852 resp->fcp_hdr_le.s_id[1] = abts->fcp_hdr_le.s_id[1];
1853 resp->fcp_hdr_le.s_id[2] = abts->fcp_hdr_le.s_id[2];
1855 resp->fcp_hdr_le.d_id[0] = abts->fcp_hdr_le.s_id[0];
1856 resp->fcp_hdr_le.d_id[1] = abts->fcp_hdr_le.s_id[1];
1857 resp->fcp_hdr_le.d_id[2] = abts->fcp_hdr_le.s_id[2];
1858 resp->fcp_hdr_le.s_id[0] = abts->fcp_hdr_le.d_id[0];
1859 resp->fcp_hdr_le.s_id[1] = abts->fcp_hdr_le.d_id[1];
1860 resp->fcp_hdr_le.s_id[2] = abts->fcp_hdr_le.d_id[2];
1862 resp->exchange_addr_to_abort = abts->exchange_addr_to_abort;
1863 if (status == FCP_TMF_CMPL) {
1864 resp->fcp_hdr_le.r_ctl = R_CTL_BASIC_LINK_SERV | R_CTL_B_ACC;
1865 resp->payload.ba_acct.seq_id_valid = SEQ_ID_INVALID;
1866 resp->payload.ba_acct.low_seq_cnt = 0x0000;
1867 resp->payload.ba_acct.high_seq_cnt = 0xFFFF;
1868 resp->payload.ba_acct.ox_id = abts->fcp_hdr_le.ox_id;
1869 resp->payload.ba_acct.rx_id = abts->fcp_hdr_le.rx_id;
1871 resp->fcp_hdr_le.r_ctl = R_CTL_BASIC_LINK_SERV | R_CTL_B_RJT;
1872 resp->payload.ba_rjt.reason_code =
1873 BA_RJT_REASON_CODE_UNABLE_TO_PERFORM;
1874 /* Other bytes are zero */
1877 vha->vha_tgt.qla_tgt->abts_resp_expected++;
1879 /* Memory Barrier */
1881 if (qpair->reqq_start_iocbs)
1882 qpair->reqq_start_iocbs(qpair);
1884 qla2x00_start_iocbs(vha, qpair->req);
1888 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
1890 static void qlt_24xx_retry_term_exchange(struct scsi_qla_host *vha,
1891 struct qla_qpair *qpair, response_t *pkt, struct qla_tgt_mgmt_cmd *mcmd)
1893 struct ctio7_to_24xx *ctio;
1895 struct abts_recv_from_24xx *entry;
1897 ctio = (struct ctio7_to_24xx *)qla2x00_alloc_iocbs_ready(qpair, NULL);
1899 ql_dbg(ql_dbg_tgt, vha, 0xe04b,
1900 "qla_target(%d): %s failed: unable to allocate "
1901 "request packet\n", vha->vp_idx, __func__);
1906 /* abts from remote port */
1907 entry = &mcmd->orig_iocb.abts;
1909 /* abts from this driver. */
1910 entry = (struct abts_recv_from_24xx *)pkt;
1913 * We've got on entrance firmware's response on by us generated
1914 * ABTS response. So, in it ID fields are reversed.
1917 ctio->entry_type = CTIO_TYPE7;
1918 ctio->entry_count = 1;
1919 ctio->nport_handle = entry->nport_handle;
1920 ctio->handle = QLA_TGT_SKIP_HANDLE | CTIO_COMPLETION_HANDLE_MARK;
1921 ctio->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
1922 ctio->vp_index = vha->vp_idx;
1923 ctio->exchange_addr = entry->exchange_addr_to_abort;
1924 tmp = (CTIO7_FLAGS_STATUS_MODE_1 | CTIO7_FLAGS_TERMINATE);
1927 ctio->initiator_id[0] = entry->fcp_hdr_le.s_id[0];
1928 ctio->initiator_id[1] = entry->fcp_hdr_le.s_id[1];
1929 ctio->initiator_id[2] = entry->fcp_hdr_le.s_id[2];
1931 if (mcmd->flags & QLA24XX_MGMT_ABORT_IO_ATTR_VALID)
1932 tmp |= (mcmd->abort_io_attr << 9);
1933 else if (qpair->retry_term_cnt & 1)
1936 ctio->initiator_id[0] = entry->fcp_hdr_le.d_id[0];
1937 ctio->initiator_id[1] = entry->fcp_hdr_le.d_id[1];
1938 ctio->initiator_id[2] = entry->fcp_hdr_le.d_id[2];
1940 if (qpair->retry_term_cnt & 1)
1943 ctio->u.status1.flags = cpu_to_le16(tmp);
1944 ctio->u.status1.ox_id = entry->fcp_hdr_le.ox_id;
1946 ql_dbg(ql_dbg_tgt, vha, 0xe007,
1947 "Sending retry TERM EXCH CTIO7 flags %04xh oxid %04xh attr valid %x\n",
1948 le16_to_cpu(ctio->u.status1.flags),
1949 le16_to_cpu(ctio->u.status1.ox_id),
1950 (mcmd && mcmd->flags & QLA24XX_MGMT_ABORT_IO_ATTR_VALID) ? 1 : 0);
1952 /* Memory Barrier */
1954 if (qpair->reqq_start_iocbs)
1955 qpair->reqq_start_iocbs(qpair);
1957 qla2x00_start_iocbs(vha, qpair->req);
1960 qlt_build_abts_resp_iocb(mcmd);
1962 qlt_24xx_send_abts_resp(qpair,
1963 (struct abts_recv_from_24xx *)entry, FCP_TMF_CMPL, true);
1967 /* drop cmds for the given lun
1968 * XXX only looks for cmds on the port through which lun reset was recieved
1969 * XXX does not go through the list of other port (which may have cmds
1972 static void abort_cmds_for_lun(struct scsi_qla_host *vha,
1973 u64 lun, uint8_t *s_id)
1975 struct qla_tgt_sess_op *op;
1976 struct qla_tgt_cmd *cmd;
1978 unsigned long flags;
1980 key = sid_to_key(s_id);
1981 spin_lock_irqsave(&vha->cmd_list_lock, flags);
1982 list_for_each_entry(op, &vha->qla_sess_op_cmd_list, cmd_list) {
1986 op_key = sid_to_key(op->atio.u.isp24.fcp_hdr.s_id);
1987 op_lun = scsilun_to_int(
1988 (struct scsi_lun *)&op->atio.u.isp24.fcp_cmnd.lun);
1989 if (op_key == key && op_lun == lun)
1993 list_for_each_entry(op, &vha->unknown_atio_list, cmd_list) {
1997 op_key = sid_to_key(op->atio.u.isp24.fcp_hdr.s_id);
1998 op_lun = scsilun_to_int(
1999 (struct scsi_lun *)&op->atio.u.isp24.fcp_cmnd.lun);
2000 if (op_key == key && op_lun == lun)
2004 list_for_each_entry(cmd, &vha->qla_cmd_list, cmd_list) {
2008 cmd_key = sid_to_key(cmd->atio.u.isp24.fcp_hdr.s_id);
2009 cmd_lun = scsilun_to_int(
2010 (struct scsi_lun *)&cmd->atio.u.isp24.fcp_cmnd.lun);
2011 if (cmd_key == key && cmd_lun == lun)
2014 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
2017 static struct qla_qpair_hint *qlt_find_qphint(struct scsi_qla_host *vha,
2018 uint64_t unpacked_lun)
2020 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
2021 struct qla_qpair_hint *h = NULL;
2023 if (vha->flags.qpairs_available) {
2024 h = btree_lookup64(&tgt->lun_qpair_map, unpacked_lun);
2026 h = &tgt->qphints[0];
2028 h = &tgt->qphints[0];
2034 static void qlt_do_tmr_work(struct work_struct *work)
2036 struct qla_tgt_mgmt_cmd *mcmd =
2037 container_of(work, struct qla_tgt_mgmt_cmd, work);
2038 struct qla_hw_data *ha = mcmd->vha->hw;
2041 unsigned long flags;
2043 switch (mcmd->tmr_func) {
2045 tag = mcmd->orig_iocb.abts.exchange_addr_to_abort;
2052 rc = ha->tgt.tgt_ops->handle_tmr(mcmd, mcmd->unpacked_lun,
2053 mcmd->tmr_func, tag);
2056 spin_lock_irqsave(mcmd->qpair->qp_lock_ptr, flags);
2057 switch (mcmd->tmr_func) {
2059 mcmd->fc_tm_rsp = FCP_TMF_REJECTED;
2060 qlt_build_abts_resp_iocb(mcmd);
2062 case QLA_TGT_LUN_RESET:
2063 case QLA_TGT_CLEAR_TS:
2064 case QLA_TGT_ABORT_TS:
2065 case QLA_TGT_CLEAR_ACA:
2066 case QLA_TGT_TARGET_RESET:
2067 qlt_send_busy(mcmd->qpair, &mcmd->orig_iocb.atio,
2071 case QLA_TGT_ABORT_ALL:
2072 case QLA_TGT_NEXUS_LOSS_SESS:
2073 case QLA_TGT_NEXUS_LOSS:
2074 qlt_send_notify_ack(mcmd->qpair,
2075 &mcmd->orig_iocb.imm_ntfy, 0, 0, 0, 0, 0, 0);
2078 spin_unlock_irqrestore(mcmd->qpair->qp_lock_ptr, flags);
2080 ql_dbg(ql_dbg_tgt_mgt, mcmd->vha, 0xf052,
2081 "qla_target(%d): tgt_ops->handle_tmr() failed: %d\n",
2082 mcmd->vha->vp_idx, rc);
2083 mempool_free(mcmd, qla_tgt_mgmt_cmd_mempool);
2087 /* ha->hardware_lock supposed to be held on entry */
2088 static int __qlt_24xx_handle_abts(struct scsi_qla_host *vha,
2089 struct abts_recv_from_24xx *abts, struct fc_port *sess)
2091 struct qla_hw_data *ha = vha->hw;
2092 struct qla_tgt_mgmt_cmd *mcmd;
2093 struct qla_qpair_hint *h = &vha->vha_tgt.qla_tgt->qphints[0];
2095 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00f,
2096 "qla_target(%d): task abort (tag=%d)\n",
2097 vha->vp_idx, abts->exchange_addr_to_abort);
2099 mcmd = mempool_alloc(qla_tgt_mgmt_cmd_mempool, GFP_ATOMIC);
2101 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf051,
2102 "qla_target(%d): %s: Allocation of ABORT cmd failed",
2103 vha->vp_idx, __func__);
2106 memset(mcmd, 0, sizeof(*mcmd));
2107 mcmd->cmd_type = TYPE_TGT_TMCMD;
2109 memcpy(&mcmd->orig_iocb.abts, abts, sizeof(mcmd->orig_iocb.abts));
2110 mcmd->reset_count = ha->base_qpair->chip_reset;
2111 mcmd->tmr_func = QLA_TGT_ABTS;
2112 mcmd->qpair = h->qpair;
2116 * LUN is looked up by target-core internally based on the passed
2117 * abts->exchange_addr_to_abort tag.
2119 mcmd->se_cmd.cpuid = h->cpuid;
2121 if (ha->tgt.tgt_ops->find_cmd_by_tag) {
2122 struct qla_tgt_cmd *abort_cmd;
2124 abort_cmd = ha->tgt.tgt_ops->find_cmd_by_tag(sess,
2125 abts->exchange_addr_to_abort);
2126 if (abort_cmd && abort_cmd->qpair) {
2127 mcmd->qpair = abort_cmd->qpair;
2128 mcmd->se_cmd.cpuid = abort_cmd->se_cmd.cpuid;
2129 mcmd->abort_io_attr = abort_cmd->atio.u.isp24.attr;
2130 mcmd->flags = QLA24XX_MGMT_ABORT_IO_ATTR_VALID;
2134 INIT_WORK(&mcmd->work, qlt_do_tmr_work);
2135 queue_work_on(mcmd->se_cmd.cpuid, qla_tgt_wq, &mcmd->work);
2141 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
2143 static void qlt_24xx_handle_abts(struct scsi_qla_host *vha,
2144 struct abts_recv_from_24xx *abts)
2146 struct qla_hw_data *ha = vha->hw;
2147 struct fc_port *sess;
2148 uint32_t tag = abts->exchange_addr_to_abort;
2151 unsigned long flags;
2153 if (le32_to_cpu(abts->fcp_hdr_le.parameter) & ABTS_PARAM_ABORT_SEQ) {
2154 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf053,
2155 "qla_target(%d): ABTS: Abort Sequence not "
2156 "supported\n", vha->vp_idx);
2157 qlt_24xx_send_abts_resp(ha->base_qpair, abts, FCP_TMF_REJECTED,
2162 if (tag == ATIO_EXCHANGE_ADDRESS_UNKNOWN) {
2163 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf010,
2164 "qla_target(%d): ABTS: Unknown Exchange "
2165 "Address received\n", vha->vp_idx);
2166 qlt_24xx_send_abts_resp(ha->base_qpair, abts, FCP_TMF_REJECTED,
2171 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf011,
2172 "qla_target(%d): task abort (s_id=%x:%x:%x, "
2173 "tag=%d, param=%x)\n", vha->vp_idx, abts->fcp_hdr_le.s_id[2],
2174 abts->fcp_hdr_le.s_id[1], abts->fcp_hdr_le.s_id[0], tag,
2175 le32_to_cpu(abts->fcp_hdr_le.parameter));
2177 s_id[0] = abts->fcp_hdr_le.s_id[2];
2178 s_id[1] = abts->fcp_hdr_le.s_id[1];
2179 s_id[2] = abts->fcp_hdr_le.s_id[0];
2181 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
2182 sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha, s_id);
2184 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf012,
2185 "qla_target(%d): task abort for non-existent session\n",
2187 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
2189 qlt_24xx_send_abts_resp(ha->base_qpair, abts, FCP_TMF_REJECTED,
2193 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
2196 if (sess->deleted) {
2197 qlt_24xx_send_abts_resp(ha->base_qpair, abts, FCP_TMF_REJECTED,
2202 rc = __qlt_24xx_handle_abts(vha, abts, sess);
2204 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf054,
2205 "qla_target(%d): __qlt_24xx_handle_abts() failed: %d\n",
2207 qlt_24xx_send_abts_resp(ha->base_qpair, abts, FCP_TMF_REJECTED,
2214 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
2216 static void qlt_24xx_send_task_mgmt_ctio(struct qla_qpair *qpair,
2217 struct qla_tgt_mgmt_cmd *mcmd, uint32_t resp_code)
2219 struct scsi_qla_host *ha = mcmd->vha;
2220 struct atio_from_isp *atio = &mcmd->orig_iocb.atio;
2221 struct ctio7_to_24xx *ctio;
2224 ql_dbg(ql_dbg_tgt, ha, 0xe008,
2225 "Sending task mgmt CTIO7 (ha=%p, atio=%p, resp_code=%x\n",
2226 ha, atio, resp_code);
2229 ctio = (struct ctio7_to_24xx *)__qla2x00_alloc_iocbs(qpair, NULL);
2231 ql_dbg(ql_dbg_tgt, ha, 0xe04c,
2232 "qla_target(%d): %s failed: unable to allocate "
2233 "request packet\n", ha->vp_idx, __func__);
2237 ctio->entry_type = CTIO_TYPE7;
2238 ctio->entry_count = 1;
2239 ctio->handle = QLA_TGT_SKIP_HANDLE | CTIO_COMPLETION_HANDLE_MARK;
2240 ctio->nport_handle = mcmd->sess->loop_id;
2241 ctio->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
2242 ctio->vp_index = ha->vp_idx;
2243 ctio->initiator_id[0] = atio->u.isp24.fcp_hdr.s_id[2];
2244 ctio->initiator_id[1] = atio->u.isp24.fcp_hdr.s_id[1];
2245 ctio->initiator_id[2] = atio->u.isp24.fcp_hdr.s_id[0];
2246 ctio->exchange_addr = atio->u.isp24.exchange_addr;
2247 temp = (atio->u.isp24.attr << 9)|
2248 CTIO7_FLAGS_STATUS_MODE_1 | CTIO7_FLAGS_SEND_STATUS;
2249 ctio->u.status1.flags = cpu_to_le16(temp);
2250 temp = be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id);
2251 ctio->u.status1.ox_id = cpu_to_le16(temp);
2252 ctio->u.status1.scsi_status =
2253 cpu_to_le16(SS_RESPONSE_INFO_LEN_VALID);
2254 ctio->u.status1.response_len = cpu_to_le16(8);
2255 ctio->u.status1.sense_data[0] = resp_code;
2257 /* Memory Barrier */
2259 if (qpair->reqq_start_iocbs)
2260 qpair->reqq_start_iocbs(qpair);
2262 qla2x00_start_iocbs(ha, qpair->req);
2265 void qlt_free_mcmd(struct qla_tgt_mgmt_cmd *mcmd)
2267 mempool_free(mcmd, qla_tgt_mgmt_cmd_mempool);
2269 EXPORT_SYMBOL(qlt_free_mcmd);
2272 * ha->hardware_lock supposed to be held on entry. Might drop it, then
2275 void qlt_send_resp_ctio(struct qla_qpair *qpair, struct qla_tgt_cmd *cmd,
2276 uint8_t scsi_status, uint8_t sense_key, uint8_t asc, uint8_t ascq)
2278 struct atio_from_isp *atio = &cmd->atio;
2279 struct ctio7_to_24xx *ctio;
2281 struct scsi_qla_host *vha = cmd->vha;
2283 ql_dbg(ql_dbg_tgt_dif, vha, 0x3066,
2284 "Sending response CTIO7 (vha=%p, atio=%p, scsi_status=%02x, "
2285 "sense_key=%02x, asc=%02x, ascq=%02x",
2286 vha, atio, scsi_status, sense_key, asc, ascq);
2288 ctio = (struct ctio7_to_24xx *)qla2x00_alloc_iocbs(vha, NULL);
2290 ql_dbg(ql_dbg_async, vha, 0x3067,
2291 "qla2x00t(%ld): %s failed: unable to allocate request packet",
2292 vha->host_no, __func__);
2296 ctio->entry_type = CTIO_TYPE7;
2297 ctio->entry_count = 1;
2298 ctio->handle = QLA_TGT_SKIP_HANDLE;
2299 ctio->nport_handle = cmd->sess->loop_id;
2300 ctio->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
2301 ctio->vp_index = vha->vp_idx;
2302 ctio->initiator_id[0] = atio->u.isp24.fcp_hdr.s_id[2];
2303 ctio->initiator_id[1] = atio->u.isp24.fcp_hdr.s_id[1];
2304 ctio->initiator_id[2] = atio->u.isp24.fcp_hdr.s_id[0];
2305 ctio->exchange_addr = atio->u.isp24.exchange_addr;
2306 temp = (atio->u.isp24.attr << 9) |
2307 CTIO7_FLAGS_STATUS_MODE_1 | CTIO7_FLAGS_SEND_STATUS;
2308 ctio->u.status1.flags = cpu_to_le16(temp);
2309 temp = be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id);
2310 ctio->u.status1.ox_id = cpu_to_le16(temp);
2311 ctio->u.status1.scsi_status =
2312 cpu_to_le16(SS_RESPONSE_INFO_LEN_VALID | scsi_status);
2313 ctio->u.status1.response_len = cpu_to_le16(18);
2314 ctio->u.status1.residual = cpu_to_le32(get_datalen_for_atio(atio));
2316 if (ctio->u.status1.residual != 0)
2317 ctio->u.status1.scsi_status |=
2318 cpu_to_le16(SS_RESIDUAL_UNDER);
2320 /* Fixed format sense data. */
2321 ctio->u.status1.sense_data[0] = 0x70;
2322 ctio->u.status1.sense_data[2] = sense_key;
2323 /* Additional sense length */
2324 ctio->u.status1.sense_data[7] = 0xa;
2326 ctio->u.status1.sense_data[12] = asc;
2327 ctio->u.status1.sense_data[13] = ascq;
2329 /* Memory Barrier */
2332 if (qpair->reqq_start_iocbs)
2333 qpair->reqq_start_iocbs(qpair);
2335 qla2x00_start_iocbs(vha, qpair->req);
2341 /* callback from target fabric module code */
2342 void qlt_xmit_tm_rsp(struct qla_tgt_mgmt_cmd *mcmd)
2344 struct scsi_qla_host *vha = mcmd->sess->vha;
2345 struct qla_hw_data *ha = vha->hw;
2346 unsigned long flags;
2347 struct qla_qpair *qpair = mcmd->qpair;
2348 bool free_mcmd = true;
2350 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf013,
2351 "TM response mcmd (%p) status %#x state %#x",
2352 mcmd, mcmd->fc_tm_rsp, mcmd->flags);
2354 spin_lock_irqsave(qpair->qp_lock_ptr, flags);
2356 if (!vha->flags.online || mcmd->reset_count != qpair->chip_reset) {
2358 * Either the port is not online or this request was from
2359 * previous life, just abort the processing.
2361 ql_dbg(ql_dbg_async, vha, 0xe100,
2362 "RESET-TMR online/active/old-count/new-count = %d/%d/%d/%d.\n",
2363 vha->flags.online, qla2x00_reset_active(vha),
2364 mcmd->reset_count, qpair->chip_reset);
2365 ha->tgt.tgt_ops->free_mcmd(mcmd);
2366 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
2370 if (mcmd->flags == QLA24XX_MGMT_SEND_NACK) {
2371 switch (mcmd->orig_iocb.imm_ntfy.u.isp24.status_subcode) {
2375 ql_dbg(ql_dbg_disc, vha, 0x2106,
2376 "TM response logo %8phC status %#x state %#x",
2377 mcmd->sess->port_name, mcmd->fc_tm_rsp,
2379 qlt_schedule_sess_for_deletion(mcmd->sess);
2382 qlt_send_notify_ack(vha->hw->base_qpair,
2383 &mcmd->orig_iocb.imm_ntfy, 0, 0, 0, 0, 0, 0);
2387 if (mcmd->orig_iocb.atio.u.raw.entry_type == ABTS_RECV_24XX) {
2388 qlt_build_abts_resp_iocb(mcmd);
2391 qlt_24xx_send_task_mgmt_ctio(qpair, mcmd,
2395 * Make the callback for ->free_mcmd() to queue_work() and invoke
2396 * target_put_sess_cmd() to drop cmd_kref to 1. The final
2397 * target_put_sess_cmd() call will be made from TFO->check_stop_free()
2398 * -> tcm_qla2xxx_check_stop_free() to release the TMR associated se_cmd
2399 * descriptor after TFO->queue_tm_rsp() -> tcm_qla2xxx_queue_tm_rsp() ->
2400 * qlt_xmit_tm_rsp() returns here..
2403 ha->tgt.tgt_ops->free_mcmd(mcmd);
2405 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
2407 EXPORT_SYMBOL(qlt_xmit_tm_rsp);
2410 static int qlt_pci_map_calc_cnt(struct qla_tgt_prm *prm)
2412 struct qla_tgt_cmd *cmd = prm->cmd;
2414 BUG_ON(cmd->sg_cnt == 0);
2416 prm->sg = (struct scatterlist *)cmd->sg;
2417 prm->seg_cnt = dma_map_sg(&cmd->qpair->pdev->dev, cmd->sg,
2418 cmd->sg_cnt, cmd->dma_data_direction);
2419 if (unlikely(prm->seg_cnt == 0))
2422 prm->cmd->sg_mapped = 1;
2424 if (cmd->se_cmd.prot_op == TARGET_PROT_NORMAL) {
2426 * If greater than four sg entries then we need to allocate
2427 * the continuation entries
2429 if (prm->seg_cnt > QLA_TGT_DATASEGS_PER_CMD_24XX)
2430 prm->req_cnt += DIV_ROUND_UP(prm->seg_cnt -
2431 QLA_TGT_DATASEGS_PER_CMD_24XX,
2432 QLA_TGT_DATASEGS_PER_CONT_24XX);
2435 if ((cmd->se_cmd.prot_op == TARGET_PROT_DIN_INSERT) ||
2436 (cmd->se_cmd.prot_op == TARGET_PROT_DOUT_STRIP)) {
2437 prm->seg_cnt = DIV_ROUND_UP(cmd->bufflen, cmd->blk_sz);
2438 prm->tot_dsds = prm->seg_cnt;
2440 prm->tot_dsds = prm->seg_cnt;
2442 if (cmd->prot_sg_cnt) {
2443 prm->prot_sg = cmd->prot_sg;
2444 prm->prot_seg_cnt = dma_map_sg(&cmd->qpair->pdev->dev,
2445 cmd->prot_sg, cmd->prot_sg_cnt,
2446 cmd->dma_data_direction);
2447 if (unlikely(prm->prot_seg_cnt == 0))
2450 if ((cmd->se_cmd.prot_op == TARGET_PROT_DIN_INSERT) ||
2451 (cmd->se_cmd.prot_op == TARGET_PROT_DOUT_STRIP)) {
2452 /* Dif Bundling not support here */
2453 prm->prot_seg_cnt = DIV_ROUND_UP(cmd->bufflen,
2455 prm->tot_dsds += prm->prot_seg_cnt;
2457 prm->tot_dsds += prm->prot_seg_cnt;
2464 ql_dbg_qp(ql_dbg_tgt, prm->cmd->qpair, 0xe04d,
2465 "qla_target(%d): PCI mapping failed: sg_cnt=%d",
2466 0, prm->cmd->sg_cnt);
2470 static void qlt_unmap_sg(struct scsi_qla_host *vha, struct qla_tgt_cmd *cmd)
2472 struct qla_hw_data *ha;
2473 struct qla_qpair *qpair;
2475 if (!cmd->sg_mapped)
2480 dma_unmap_sg(&qpair->pdev->dev, cmd->sg, cmd->sg_cnt,
2481 cmd->dma_data_direction);
2484 if (cmd->prot_sg_cnt)
2485 dma_unmap_sg(&qpair->pdev->dev, cmd->prot_sg, cmd->prot_sg_cnt,
2486 cmd->dma_data_direction);
2491 if (cmd->ctx_dsd_alloced)
2492 qla2x00_clean_dsd_pool(ha, cmd->ctx);
2494 dma_pool_free(ha->dl_dma_pool, cmd->ctx, cmd->ctx->crc_ctx_dma);
2497 static int qlt_check_reserve_free_req(struct qla_qpair *qpair,
2501 struct req_que *req = qpair->req;
2503 if (req->cnt < (req_cnt + 2)) {
2504 cnt = (uint16_t)(qpair->use_shadow_reg ? *req->out_ptr :
2505 RD_REG_DWORD_RELAXED(req->req_q_out));
2507 if (req->ring_index < cnt)
2508 req->cnt = cnt - req->ring_index;
2510 req->cnt = req->length - (req->ring_index - cnt);
2512 if (unlikely(req->cnt < (req_cnt + 2)))
2516 req->cnt -= req_cnt;
2522 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
2524 static inline void *qlt_get_req_pkt(struct req_que *req)
2526 /* Adjust ring index. */
2528 if (req->ring_index == req->length) {
2529 req->ring_index = 0;
2530 req->ring_ptr = req->ring;
2534 return (cont_entry_t *)req->ring_ptr;
2537 /* ha->hardware_lock supposed to be held on entry */
2538 static inline uint32_t qlt_make_handle(struct qla_qpair *qpair)
2543 struct req_que *req = qpair->req;
2545 h = req->current_outstanding_cmd;
2547 for (index = 1; index < req->num_outstanding_cmds; index++) {
2549 if (h == req->num_outstanding_cmds)
2552 if (h == QLA_TGT_SKIP_HANDLE)
2555 if (!req->outstanding_cmds[h]) {
2562 req->current_outstanding_cmd = h;
2564 ql_dbg(ql_dbg_io, qpair->vha, 0x305b,
2565 "qla_target(%d): Ran out of empty cmd slots\n",
2566 qpair->vha->vp_idx);
2567 h = QLA_TGT_NULL_HANDLE;
2573 /* ha->hardware_lock supposed to be held on entry */
2574 static int qlt_24xx_build_ctio_pkt(struct qla_qpair *qpair,
2575 struct qla_tgt_prm *prm)
2578 struct ctio7_to_24xx *pkt;
2579 struct atio_from_isp *atio = &prm->cmd->atio;
2582 pkt = (struct ctio7_to_24xx *)qpair->req->ring_ptr;
2584 memset(pkt, 0, sizeof(*pkt));
2586 pkt->entry_type = CTIO_TYPE7;
2587 pkt->entry_count = (uint8_t)prm->req_cnt;
2588 pkt->vp_index = prm->cmd->vp_idx;
2590 h = qlt_make_handle(qpair);
2591 if (unlikely(h == QLA_TGT_NULL_HANDLE)) {
2593 * CTIO type 7 from the firmware doesn't provide a way to
2594 * know the initiator's LOOP ID, hence we can't find
2595 * the session and, so, the command.
2599 qpair->req->outstanding_cmds[h] = (srb_t *)prm->cmd;
2601 pkt->handle = MAKE_HANDLE(qpair->req->id, h);
2602 pkt->handle |= CTIO_COMPLETION_HANDLE_MARK;
2603 pkt->nport_handle = cpu_to_le16(prm->cmd->loop_id);
2604 pkt->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
2605 pkt->initiator_id[0] = atio->u.isp24.fcp_hdr.s_id[2];
2606 pkt->initiator_id[1] = atio->u.isp24.fcp_hdr.s_id[1];
2607 pkt->initiator_id[2] = atio->u.isp24.fcp_hdr.s_id[0];
2608 pkt->exchange_addr = atio->u.isp24.exchange_addr;
2609 temp = atio->u.isp24.attr << 9;
2610 pkt->u.status0.flags |= cpu_to_le16(temp);
2611 temp = be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id);
2612 pkt->u.status0.ox_id = cpu_to_le16(temp);
2613 pkt->u.status0.relative_offset = cpu_to_le32(prm->cmd->offset);
2619 * ha->hardware_lock supposed to be held on entry. We have already made sure
2620 * that there is sufficient amount of request entries to not drop it.
2622 static void qlt_load_cont_data_segments(struct qla_tgt_prm *prm)
2625 struct dsd64 *cur_dsd;
2627 /* Build continuation packets */
2628 while (prm->seg_cnt > 0) {
2629 cont_a64_entry_t *cont_pkt64 =
2630 (cont_a64_entry_t *)qlt_get_req_pkt(
2631 prm->cmd->qpair->req);
2634 * Make sure that from cont_pkt64 none of
2635 * 64-bit specific fields used for 32-bit
2636 * addressing. Cast to (cont_entry_t *) for
2640 memset(cont_pkt64, 0, sizeof(*cont_pkt64));
2642 cont_pkt64->entry_count = 1;
2643 cont_pkt64->sys_define = 0;
2645 cont_pkt64->entry_type = CONTINUE_A64_TYPE;
2646 cur_dsd = cont_pkt64->dsd;
2648 /* Load continuation entry data segments */
2650 cnt < QLA_TGT_DATASEGS_PER_CONT_24XX && prm->seg_cnt;
2651 cnt++, prm->seg_cnt--) {
2652 append_dsd64(&cur_dsd, prm->sg);
2653 prm->sg = sg_next(prm->sg);
2659 * ha->hardware_lock supposed to be held on entry. We have already made sure
2660 * that there is sufficient amount of request entries to not drop it.
2662 static void qlt_load_data_segments(struct qla_tgt_prm *prm)
2665 struct dsd64 *cur_dsd;
2666 struct ctio7_to_24xx *pkt24 = (struct ctio7_to_24xx *)prm->pkt;
2668 pkt24->u.status0.transfer_length = cpu_to_le32(prm->cmd->bufflen);
2670 /* Setup packet address segment pointer */
2671 cur_dsd = &pkt24->u.status0.dsd;
2673 /* Set total data segment count */
2675 pkt24->dseg_count = cpu_to_le16(prm->seg_cnt);
2677 if (prm->seg_cnt == 0) {
2678 /* No data transfer */
2679 cur_dsd->address = 0;
2680 cur_dsd->length = 0;
2684 /* If scatter gather */
2686 /* Load command entry data segments */
2688 (cnt < QLA_TGT_DATASEGS_PER_CMD_24XX) && prm->seg_cnt;
2689 cnt++, prm->seg_cnt--) {
2690 append_dsd64(&cur_dsd, prm->sg);
2691 prm->sg = sg_next(prm->sg);
2694 qlt_load_cont_data_segments(prm);
2697 static inline int qlt_has_data(struct qla_tgt_cmd *cmd)
2699 return cmd->bufflen > 0;
2702 static void qlt_print_dif_err(struct qla_tgt_prm *prm)
2704 struct qla_tgt_cmd *cmd;
2705 struct scsi_qla_host *vha;
2707 /* asc 0x10=dif error */
2708 if (prm->sense_buffer && (prm->sense_buffer[12] == 0x10)) {
2712 switch (prm->sense_buffer[13]) {
2714 ql_dbg(ql_dbg_tgt_dif, vha, 0xe00b,
2715 "BE detected Guard TAG ERR: lba[0x%llx|%lld] len[0x%x] "
2716 "se_cmd=%p tag[%x]",
2717 cmd->lba, cmd->lba, cmd->num_blks, &cmd->se_cmd,
2718 cmd->atio.u.isp24.exchange_addr);
2721 ql_dbg(ql_dbg_tgt_dif, vha, 0xe00c,
2722 "BE detected APP TAG ERR: lba[0x%llx|%lld] len[0x%x] "
2723 "se_cmd=%p tag[%x]",
2724 cmd->lba, cmd->lba, cmd->num_blks, &cmd->se_cmd,
2725 cmd->atio.u.isp24.exchange_addr);
2728 ql_dbg(ql_dbg_tgt_dif, vha, 0xe00f,
2729 "BE detected REF TAG ERR: lba[0x%llx|%lld] len[0x%x] "
2730 "se_cmd=%p tag[%x]",
2731 cmd->lba, cmd->lba, cmd->num_blks, &cmd->se_cmd,
2732 cmd->atio.u.isp24.exchange_addr);
2735 ql_dbg(ql_dbg_tgt_dif, vha, 0xe010,
2736 "BE detected Dif ERR: lba[%llx|%lld] len[%x] "
2737 "se_cmd=%p tag[%x]",
2738 cmd->lba, cmd->lba, cmd->num_blks, &cmd->se_cmd,
2739 cmd->atio.u.isp24.exchange_addr);
2742 ql_dump_buffer(ql_dbg_tgt_dif, vha, 0xe011, cmd->cdb, 16);
2747 * Called without ha->hardware_lock held
2749 static int qlt_pre_xmit_response(struct qla_tgt_cmd *cmd,
2750 struct qla_tgt_prm *prm, int xmit_type, uint8_t scsi_status,
2751 uint32_t *full_req_cnt)
2753 struct se_cmd *se_cmd = &cmd->se_cmd;
2754 struct qla_qpair *qpair = cmd->qpair;
2757 prm->tgt = cmd->tgt;
2759 prm->rq_result = scsi_status;
2760 prm->sense_buffer = &cmd->sense_buffer[0];
2761 prm->sense_buffer_len = TRANSPORT_SENSE_BUFFER;
2766 prm->add_status_pkt = 0;
2767 prm->prot_sg = NULL;
2768 prm->prot_seg_cnt = 0;
2771 if ((xmit_type & QLA_TGT_XMIT_DATA) && qlt_has_data(cmd)) {
2772 if (qlt_pci_map_calc_cnt(prm) != 0)
2776 *full_req_cnt = prm->req_cnt;
2778 if (se_cmd->se_cmd_flags & SCF_UNDERFLOW_BIT) {
2779 prm->residual = se_cmd->residual_count;
2780 ql_dbg_qp(ql_dbg_io + ql_dbg_verbose, qpair, 0x305c,
2781 "Residual underflow: %d (tag %lld, op %x, bufflen %d, rq_result %x)\n",
2782 prm->residual, se_cmd->tag,
2783 se_cmd->t_task_cdb ? se_cmd->t_task_cdb[0] : 0,
2784 cmd->bufflen, prm->rq_result);
2785 prm->rq_result |= SS_RESIDUAL_UNDER;
2786 } else if (se_cmd->se_cmd_flags & SCF_OVERFLOW_BIT) {
2787 prm->residual = se_cmd->residual_count;
2788 ql_dbg_qp(ql_dbg_io, qpair, 0x305d,
2789 "Residual overflow: %d (tag %lld, op %x, bufflen %d, rq_result %x)\n",
2790 prm->residual, se_cmd->tag, se_cmd->t_task_cdb ?
2791 se_cmd->t_task_cdb[0] : 0, cmd->bufflen, prm->rq_result);
2792 prm->rq_result |= SS_RESIDUAL_OVER;
2795 if (xmit_type & QLA_TGT_XMIT_STATUS) {
2797 * If QLA_TGT_XMIT_DATA is not set, add_status_pkt will be
2798 * ignored in *xmit_response() below
2800 if (qlt_has_data(cmd)) {
2801 if (QLA_TGT_SENSE_VALID(prm->sense_buffer) ||
2802 (IS_FWI2_CAPABLE(cmd->vha->hw) &&
2803 (prm->rq_result != 0))) {
2804 prm->add_status_pkt = 1;
2813 static inline int qlt_need_explicit_conf(struct qla_tgt_cmd *cmd,
2816 if (cmd->qpair->enable_class_2)
2820 return cmd->conf_compl_supported;
2822 return cmd->qpair->enable_explicit_conf &&
2823 cmd->conf_compl_supported;
2826 static void qlt_24xx_init_ctio_to_isp(struct ctio7_to_24xx *ctio,
2827 struct qla_tgt_prm *prm)
2829 prm->sense_buffer_len = min_t(uint32_t, prm->sense_buffer_len,
2830 (uint32_t)sizeof(ctio->u.status1.sense_data));
2831 ctio->u.status0.flags |= cpu_to_le16(CTIO7_FLAGS_SEND_STATUS);
2832 if (qlt_need_explicit_conf(prm->cmd, 0)) {
2833 ctio->u.status0.flags |= cpu_to_le16(
2834 CTIO7_FLAGS_EXPLICIT_CONFORM |
2835 CTIO7_FLAGS_CONFORM_REQ);
2837 ctio->u.status0.residual = cpu_to_le32(prm->residual);
2838 ctio->u.status0.scsi_status = cpu_to_le16(prm->rq_result);
2839 if (QLA_TGT_SENSE_VALID(prm->sense_buffer)) {
2842 if (qlt_need_explicit_conf(prm->cmd, 1)) {
2843 if ((prm->rq_result & SS_SCSI_STATUS_BYTE) != 0) {
2844 ql_dbg_qp(ql_dbg_tgt, prm->cmd->qpair, 0xe017,
2845 "Skipping EXPLICIT_CONFORM and "
2846 "CTIO7_FLAGS_CONFORM_REQ for FCP READ w/ "
2847 "non GOOD status\n");
2848 goto skip_explict_conf;
2850 ctio->u.status1.flags |= cpu_to_le16(
2851 CTIO7_FLAGS_EXPLICIT_CONFORM |
2852 CTIO7_FLAGS_CONFORM_REQ);
2855 ctio->u.status1.flags &=
2856 ~cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_0);
2857 ctio->u.status1.flags |=
2858 cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_1);
2859 ctio->u.status1.scsi_status |=
2860 cpu_to_le16(SS_SENSE_LEN_VALID);
2861 ctio->u.status1.sense_length =
2862 cpu_to_le16(prm->sense_buffer_len);
2863 for (i = 0; i < prm->sense_buffer_len/4; i++)
2864 ((uint32_t *)ctio->u.status1.sense_data)[i] =
2865 cpu_to_be32(((uint32_t *)prm->sense_buffer)[i]);
2867 qlt_print_dif_err(prm);
2870 ctio->u.status1.flags &=
2871 ~cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_0);
2872 ctio->u.status1.flags |=
2873 cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_1);
2874 ctio->u.status1.sense_length = 0;
2875 memset(ctio->u.status1.sense_data, 0,
2876 sizeof(ctio->u.status1.sense_data));
2879 /* Sense with len > 24, is it possible ??? */
2883 qlt_hba_err_chk_enabled(struct se_cmd *se_cmd)
2885 switch (se_cmd->prot_op) {
2886 case TARGET_PROT_DOUT_INSERT:
2887 case TARGET_PROT_DIN_STRIP:
2888 if (ql2xenablehba_err_chk >= 1)
2891 case TARGET_PROT_DOUT_PASS:
2892 case TARGET_PROT_DIN_PASS:
2893 if (ql2xenablehba_err_chk >= 2)
2896 case TARGET_PROT_DIN_INSERT:
2897 case TARGET_PROT_DOUT_STRIP:
2906 qla_tgt_ref_mask_check(struct se_cmd *se_cmd)
2908 switch (se_cmd->prot_op) {
2909 case TARGET_PROT_DIN_INSERT:
2910 case TARGET_PROT_DOUT_INSERT:
2911 case TARGET_PROT_DIN_STRIP:
2912 case TARGET_PROT_DOUT_STRIP:
2913 case TARGET_PROT_DIN_PASS:
2914 case TARGET_PROT_DOUT_PASS:
2923 * qla_tgt_set_dif_tags - Extract Ref and App tags from SCSI command
2926 qla_tgt_set_dif_tags(struct qla_tgt_cmd *cmd, struct crc_context *ctx,
2927 uint16_t *pfw_prot_opts)
2929 struct se_cmd *se_cmd = &cmd->se_cmd;
2930 uint32_t lba = 0xffffffff & se_cmd->t_task_lba;
2931 scsi_qla_host_t *vha = cmd->tgt->vha;
2932 struct qla_hw_data *ha = vha->hw;
2936 * wait till Mode Sense/Select cmd, modepage Ah, subpage 2
2937 * have been immplemented by TCM, before AppTag is avail.
2938 * Look for modesense_handlers[]
2941 ctx->app_tag_mask[0] = 0x0;
2942 ctx->app_tag_mask[1] = 0x0;
2944 if (IS_PI_UNINIT_CAPABLE(ha)) {
2945 if ((se_cmd->prot_type == TARGET_DIF_TYPE1_PROT) ||
2946 (se_cmd->prot_type == TARGET_DIF_TYPE2_PROT))
2947 *pfw_prot_opts |= PO_DIS_VALD_APP_ESC;
2948 else if (se_cmd->prot_type == TARGET_DIF_TYPE3_PROT)
2949 *pfw_prot_opts |= PO_DIS_VALD_APP_REF_ESC;
2952 t32 = ha->tgt.tgt_ops->get_dif_tags(cmd, pfw_prot_opts);
2954 switch (se_cmd->prot_type) {
2955 case TARGET_DIF_TYPE0_PROT:
2957 * No check for ql2xenablehba_err_chk, as it
2958 * would be an I/O error if hba tag generation
2961 ctx->ref_tag = cpu_to_le32(lba);
2962 /* enable ALL bytes of the ref tag */
2963 ctx->ref_tag_mask[0] = 0xff;
2964 ctx->ref_tag_mask[1] = 0xff;
2965 ctx->ref_tag_mask[2] = 0xff;
2966 ctx->ref_tag_mask[3] = 0xff;
2968 case TARGET_DIF_TYPE1_PROT:
2970 * For TYPE 1 protection: 16 bit GUARD tag, 32 bit
2971 * REF tag, and 16 bit app tag.
2973 ctx->ref_tag = cpu_to_le32(lba);
2974 if (!qla_tgt_ref_mask_check(se_cmd) ||
2975 !(ha->tgt.tgt_ops->chk_dif_tags(t32))) {
2976 *pfw_prot_opts |= PO_DIS_REF_TAG_VALD;
2979 /* enable ALL bytes of the ref tag */
2980 ctx->ref_tag_mask[0] = 0xff;
2981 ctx->ref_tag_mask[1] = 0xff;
2982 ctx->ref_tag_mask[2] = 0xff;
2983 ctx->ref_tag_mask[3] = 0xff;
2985 case TARGET_DIF_TYPE2_PROT:
2987 * For TYPE 2 protection: 16 bit GUARD + 32 bit REF
2988 * tag has to match LBA in CDB + N
2990 ctx->ref_tag = cpu_to_le32(lba);
2991 if (!qla_tgt_ref_mask_check(se_cmd) ||
2992 !(ha->tgt.tgt_ops->chk_dif_tags(t32))) {
2993 *pfw_prot_opts |= PO_DIS_REF_TAG_VALD;
2996 /* enable ALL bytes of the ref tag */
2997 ctx->ref_tag_mask[0] = 0xff;
2998 ctx->ref_tag_mask[1] = 0xff;
2999 ctx->ref_tag_mask[2] = 0xff;
3000 ctx->ref_tag_mask[3] = 0xff;
3002 case TARGET_DIF_TYPE3_PROT:
3003 /* For TYPE 3 protection: 16 bit GUARD only */
3004 *pfw_prot_opts |= PO_DIS_REF_TAG_VALD;
3005 ctx->ref_tag_mask[0] = ctx->ref_tag_mask[1] =
3006 ctx->ref_tag_mask[2] = ctx->ref_tag_mask[3] = 0x00;
3012 qlt_build_ctio_crc2_pkt(struct qla_qpair *qpair, struct qla_tgt_prm *prm)
3014 struct dsd64 *cur_dsd;
3015 uint32_t transfer_length = 0;
3016 uint32_t data_bytes;
3018 uint8_t bundling = 1;
3019 struct crc_context *crc_ctx_pkt = NULL;
3020 struct qla_hw_data *ha;
3021 struct ctio_crc2_to_fw *pkt;
3022 dma_addr_t crc_ctx_dma;
3023 uint16_t fw_prot_opts = 0;
3024 struct qla_tgt_cmd *cmd = prm->cmd;
3025 struct se_cmd *se_cmd = &cmd->se_cmd;
3027 struct atio_from_isp *atio = &prm->cmd->atio;
3028 struct qla_tc_param tc;
3030 scsi_qla_host_t *vha = cmd->vha;
3034 pkt = (struct ctio_crc2_to_fw *)qpair->req->ring_ptr;
3036 memset(pkt, 0, sizeof(*pkt));
3038 ql_dbg_qp(ql_dbg_tgt, cmd->qpair, 0xe071,
3039 "qla_target(%d):%s: se_cmd[%p] CRC2 prot_op[0x%x] cmd prot sg:cnt[%p:%x] lba[%llu]\n",
3040 cmd->vp_idx, __func__, se_cmd, se_cmd->prot_op,
3041 prm->prot_sg, prm->prot_seg_cnt, se_cmd->t_task_lba);
3043 if ((se_cmd->prot_op == TARGET_PROT_DIN_INSERT) ||
3044 (se_cmd->prot_op == TARGET_PROT_DOUT_STRIP))
3047 /* Compute dif len and adjust data len to incude protection */
3048 data_bytes = cmd->bufflen;
3049 dif_bytes = (data_bytes / cmd->blk_sz) * 8;
3051 switch (se_cmd->prot_op) {
3052 case TARGET_PROT_DIN_INSERT:
3053 case TARGET_PROT_DOUT_STRIP:
3054 transfer_length = data_bytes;
3055 if (cmd->prot_sg_cnt)
3056 data_bytes += dif_bytes;
3058 case TARGET_PROT_DIN_STRIP:
3059 case TARGET_PROT_DOUT_INSERT:
3060 case TARGET_PROT_DIN_PASS:
3061 case TARGET_PROT_DOUT_PASS:
3062 transfer_length = data_bytes + dif_bytes;
3069 if (!qlt_hba_err_chk_enabled(se_cmd))
3070 fw_prot_opts |= 0x10; /* Disable Guard tag checking */
3071 /* HBA error checking enabled */
3072 else if (IS_PI_UNINIT_CAPABLE(ha)) {
3073 if ((se_cmd->prot_type == TARGET_DIF_TYPE1_PROT) ||
3074 (se_cmd->prot_type == TARGET_DIF_TYPE2_PROT))
3075 fw_prot_opts |= PO_DIS_VALD_APP_ESC;
3076 else if (se_cmd->prot_type == TARGET_DIF_TYPE3_PROT)
3077 fw_prot_opts |= PO_DIS_VALD_APP_REF_ESC;
3080 switch (se_cmd->prot_op) {
3081 case TARGET_PROT_DIN_INSERT:
3082 case TARGET_PROT_DOUT_INSERT:
3083 fw_prot_opts |= PO_MODE_DIF_INSERT;
3085 case TARGET_PROT_DIN_STRIP:
3086 case TARGET_PROT_DOUT_STRIP:
3087 fw_prot_opts |= PO_MODE_DIF_REMOVE;
3089 case TARGET_PROT_DIN_PASS:
3090 case TARGET_PROT_DOUT_PASS:
3091 fw_prot_opts |= PO_MODE_DIF_PASS;
3092 /* FUTURE: does tcm require T10CRC<->IPCKSUM conversion? */
3094 default:/* Normal Request */
3095 fw_prot_opts |= PO_MODE_DIF_PASS;
3100 /* Update entry type to indicate Command Type CRC_2 IOCB */
3101 pkt->entry_type = CTIO_CRC2;
3102 pkt->entry_count = 1;
3103 pkt->vp_index = cmd->vp_idx;
3105 h = qlt_make_handle(qpair);
3106 if (unlikely(h == QLA_TGT_NULL_HANDLE)) {
3108 * CTIO type 7 from the firmware doesn't provide a way to
3109 * know the initiator's LOOP ID, hence we can't find
3110 * the session and, so, the command.
3114 qpair->req->outstanding_cmds[h] = (srb_t *)prm->cmd;
3116 pkt->handle = MAKE_HANDLE(qpair->req->id, h);
3117 pkt->handle |= CTIO_COMPLETION_HANDLE_MARK;
3118 pkt->nport_handle = cpu_to_le16(prm->cmd->loop_id);
3119 pkt->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
3120 pkt->initiator_id[0] = atio->u.isp24.fcp_hdr.s_id[2];
3121 pkt->initiator_id[1] = atio->u.isp24.fcp_hdr.s_id[1];
3122 pkt->initiator_id[2] = atio->u.isp24.fcp_hdr.s_id[0];
3123 pkt->exchange_addr = atio->u.isp24.exchange_addr;
3125 /* silence compile warning */
3126 t16 = be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id);
3127 pkt->ox_id = cpu_to_le16(t16);
3129 t16 = (atio->u.isp24.attr << 9);
3130 pkt->flags |= cpu_to_le16(t16);
3131 pkt->relative_offset = cpu_to_le32(prm->cmd->offset);
3133 /* Set transfer direction */
3134 if (cmd->dma_data_direction == DMA_TO_DEVICE)
3135 pkt->flags = cpu_to_le16(CTIO7_FLAGS_DATA_IN);
3136 else if (cmd->dma_data_direction == DMA_FROM_DEVICE)
3137 pkt->flags = cpu_to_le16(CTIO7_FLAGS_DATA_OUT);
3139 pkt->dseg_count = prm->tot_dsds;
3140 /* Fibre channel byte count */
3141 pkt->transfer_length = cpu_to_le32(transfer_length);
3143 /* ----- CRC context -------- */
3145 /* Allocate CRC context from global pool */
3146 crc_ctx_pkt = cmd->ctx =
3147 dma_pool_zalloc(ha->dl_dma_pool, GFP_ATOMIC, &crc_ctx_dma);
3150 goto crc_queuing_error;
3152 crc_ctx_pkt->crc_ctx_dma = crc_ctx_dma;
3153 INIT_LIST_HEAD(&crc_ctx_pkt->dsd_list);
3156 crc_ctx_pkt->handle = pkt->handle;
3158 qla_tgt_set_dif_tags(cmd, crc_ctx_pkt, &fw_prot_opts);
3160 put_unaligned_le64(crc_ctx_dma, &pkt->crc_context_address);
3161 pkt->crc_context_len = CRC_CONTEXT_LEN_FW;
3164 cur_dsd = &crc_ctx_pkt->u.nobundling.data_dsd[0];
3167 * Configure Bundling if we need to fetch interlaving
3168 * protection PCI accesses
3170 fw_prot_opts |= PO_ENABLE_DIF_BUNDLING;
3171 crc_ctx_pkt->u.bundling.dif_byte_count = cpu_to_le32(dif_bytes);
3172 crc_ctx_pkt->u.bundling.dseg_count =
3173 cpu_to_le16(prm->tot_dsds - prm->prot_seg_cnt);
3174 cur_dsd = &crc_ctx_pkt->u.bundling.data_dsd[0];
3177 /* Finish the common fields of CRC pkt */
3178 crc_ctx_pkt->blk_size = cpu_to_le16(cmd->blk_sz);
3179 crc_ctx_pkt->prot_opts = cpu_to_le16(fw_prot_opts);
3180 crc_ctx_pkt->byte_count = cpu_to_le32(data_bytes);
3181 crc_ctx_pkt->guard_seed = cpu_to_le16(0);
3183 memset((uint8_t *)&tc, 0 , sizeof(tc));
3185 tc.blk_sz = cmd->blk_sz;
3186 tc.bufflen = cmd->bufflen;
3188 tc.prot_sg = cmd->prot_sg;
3189 tc.ctx = crc_ctx_pkt;
3190 tc.ctx_dsd_alloced = &cmd->ctx_dsd_alloced;
3192 /* Walks data segments */
3193 pkt->flags |= cpu_to_le16(CTIO7_FLAGS_DSD_PTR);
3195 if (!bundling && prm->prot_seg_cnt) {
3196 if (qla24xx_walk_and_build_sglist_no_difb(ha, NULL, cur_dsd,
3197 prm->tot_dsds, &tc))
3198 goto crc_queuing_error;
3199 } else if (qla24xx_walk_and_build_sglist(ha, NULL, cur_dsd,
3200 (prm->tot_dsds - prm->prot_seg_cnt), &tc))
3201 goto crc_queuing_error;
3203 if (bundling && prm->prot_seg_cnt) {
3204 /* Walks dif segments */
3205 pkt->add_flags |= CTIO_CRC2_AF_DIF_DSD_ENA;
3207 cur_dsd = &crc_ctx_pkt->u.bundling.dif_dsd;
3208 if (qla24xx_walk_and_build_prot_sglist(ha, NULL, cur_dsd,
3209 prm->prot_seg_cnt, cmd))
3210 goto crc_queuing_error;
3215 /* Cleanup will be performed by the caller */
3216 qpair->req->outstanding_cmds[h] = NULL;
3218 return QLA_FUNCTION_FAILED;
3222 * Callback to setup response of xmit_type of QLA_TGT_XMIT_DATA and *
3223 * QLA_TGT_XMIT_STATUS for >= 24xx silicon
3225 int qlt_xmit_response(struct qla_tgt_cmd *cmd, int xmit_type,
3226 uint8_t scsi_status)
3228 struct scsi_qla_host *vha = cmd->vha;
3229 struct qla_qpair *qpair = cmd->qpair;
3230 struct ctio7_to_24xx *pkt;
3231 struct qla_tgt_prm prm;
3232 uint32_t full_req_cnt = 0;
3233 unsigned long flags = 0;
3236 if (!qpair->fw_started || (cmd->reset_count != qpair->chip_reset) ||
3237 (cmd->sess && cmd->sess->deleted)) {
3238 cmd->state = QLA_TGT_STATE_PROCESSED;
3242 ql_dbg_qp(ql_dbg_tgt, qpair, 0xe018,
3243 "is_send_status=%d, cmd->bufflen=%d, cmd->sg_cnt=%d, cmd->dma_data_direction=%d se_cmd[%p] qp %d\n",
3244 (xmit_type & QLA_TGT_XMIT_STATUS) ?
3245 1 : 0, cmd->bufflen, cmd->sg_cnt, cmd->dma_data_direction,
3246 &cmd->se_cmd, qpair->id);
3248 res = qlt_pre_xmit_response(cmd, &prm, xmit_type, scsi_status,
3250 if (unlikely(res != 0)) {
3254 spin_lock_irqsave(qpair->qp_lock_ptr, flags);
3256 if (xmit_type == QLA_TGT_XMIT_STATUS)
3257 qpair->tgt_counters.core_qla_snd_status++;
3259 qpair->tgt_counters.core_qla_que_buf++;
3261 if (!qpair->fw_started || cmd->reset_count != qpair->chip_reset) {
3263 * Either the port is not online or this request was from
3264 * previous life, just abort the processing.
3266 cmd->state = QLA_TGT_STATE_PROCESSED;
3267 ql_dbg_qp(ql_dbg_async, qpair, 0xe101,
3268 "RESET-RSP online/active/old-count/new-count = %d/%d/%d/%d.\n",
3269 vha->flags.online, qla2x00_reset_active(vha),
3270 cmd->reset_count, qpair->chip_reset);
3271 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
3275 /* Does F/W have an IOCBs for this request */
3276 res = qlt_check_reserve_free_req(qpair, full_req_cnt);
3278 goto out_unmap_unlock;
3280 if (cmd->se_cmd.prot_op && (xmit_type & QLA_TGT_XMIT_DATA))
3281 res = qlt_build_ctio_crc2_pkt(qpair, &prm);
3283 res = qlt_24xx_build_ctio_pkt(qpair, &prm);
3284 if (unlikely(res != 0)) {
3285 qpair->req->cnt += full_req_cnt;
3286 goto out_unmap_unlock;
3289 pkt = (struct ctio7_to_24xx *)prm.pkt;
3291 if (qlt_has_data(cmd) && (xmit_type & QLA_TGT_XMIT_DATA)) {
3292 pkt->u.status0.flags |=
3293 cpu_to_le16(CTIO7_FLAGS_DATA_IN |
3294 CTIO7_FLAGS_STATUS_MODE_0);
3296 if (cmd->se_cmd.prot_op == TARGET_PROT_NORMAL)
3297 qlt_load_data_segments(&prm);
3299 if (prm.add_status_pkt == 0) {
3300 if (xmit_type & QLA_TGT_XMIT_STATUS) {
3301 pkt->u.status0.scsi_status =
3302 cpu_to_le16(prm.rq_result);
3303 pkt->u.status0.residual =
3304 cpu_to_le32(prm.residual);
3305 pkt->u.status0.flags |= cpu_to_le16(
3306 CTIO7_FLAGS_SEND_STATUS);
3307 if (qlt_need_explicit_conf(cmd, 0)) {
3308 pkt->u.status0.flags |=
3310 CTIO7_FLAGS_EXPLICIT_CONFORM |
3311 CTIO7_FLAGS_CONFORM_REQ);
3317 * We have already made sure that there is sufficient
3318 * amount of request entries to not drop HW lock in
3321 struct ctio7_to_24xx *ctio =
3322 (struct ctio7_to_24xx *)qlt_get_req_pkt(
3325 ql_dbg_qp(ql_dbg_tgt, qpair, 0x305e,
3326 "Building additional status packet 0x%p.\n",
3330 * T10Dif: ctio_crc2_to_fw overlay ontop of
3333 memcpy(ctio, pkt, sizeof(*ctio));
3334 /* reset back to CTIO7 */
3335 ctio->entry_count = 1;
3336 ctio->entry_type = CTIO_TYPE7;
3337 ctio->dseg_count = 0;
3338 ctio->u.status1.flags &= ~cpu_to_le16(
3339 CTIO7_FLAGS_DATA_IN);
3341 /* Real finish is ctio_m1's finish */
3342 pkt->handle |= CTIO_INTERMEDIATE_HANDLE_MARK;
3343 pkt->u.status0.flags |= cpu_to_le16(
3344 CTIO7_FLAGS_DONT_RET_CTIO);
3346 /* qlt_24xx_init_ctio_to_isp will correct
3347 * all neccessary fields that's part of CTIO7.
3348 * There should be no residual of CTIO-CRC2 data.
3350 qlt_24xx_init_ctio_to_isp((struct ctio7_to_24xx *)ctio,
3354 qlt_24xx_init_ctio_to_isp(pkt, &prm);
3357 cmd->state = QLA_TGT_STATE_PROCESSED; /* Mid-level is done processing */
3358 cmd->cmd_sent_to_fw = 1;
3359 cmd->ctio_flags = le16_to_cpu(pkt->u.status0.flags);
3361 /* Memory Barrier */
3363 if (qpair->reqq_start_iocbs)
3364 qpair->reqq_start_iocbs(qpair);
3366 qla2x00_start_iocbs(vha, qpair->req);
3367 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
3372 qlt_unmap_sg(vha, cmd);
3373 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
3377 EXPORT_SYMBOL(qlt_xmit_response);
3379 int qlt_rdy_to_xfer(struct qla_tgt_cmd *cmd)
3381 struct ctio7_to_24xx *pkt;
3382 struct scsi_qla_host *vha = cmd->vha;
3383 struct qla_tgt *tgt = cmd->tgt;
3384 struct qla_tgt_prm prm;
3385 unsigned long flags = 0;
3387 struct qla_qpair *qpair = cmd->qpair;
3389 memset(&prm, 0, sizeof(prm));
3395 /* Calculate number of entries and segments required */
3396 if (qlt_pci_map_calc_cnt(&prm) != 0)
3399 if (!qpair->fw_started || (cmd->reset_count != qpair->chip_reset) ||
3400 (cmd->sess && cmd->sess->deleted)) {
3402 * Either the port is not online or this request was from
3403 * previous life, just abort the processing.
3406 cmd->write_data_transferred = 0;
3407 cmd->state = QLA_TGT_STATE_DATA_IN;
3408 vha->hw->tgt.tgt_ops->handle_data(cmd);
3409 ql_dbg_qp(ql_dbg_async, qpair, 0xe102,
3410 "RESET-XFR online/active/old-count/new-count = %d/%d/%d/%d.\n",
3411 vha->flags.online, qla2x00_reset_active(vha),
3412 cmd->reset_count, qpair->chip_reset);
3416 spin_lock_irqsave(qpair->qp_lock_ptr, flags);
3417 /* Does F/W have an IOCBs for this request */
3418 res = qlt_check_reserve_free_req(qpair, prm.req_cnt);
3420 goto out_unlock_free_unmap;
3421 if (cmd->se_cmd.prot_op)
3422 res = qlt_build_ctio_crc2_pkt(qpair, &prm);
3424 res = qlt_24xx_build_ctio_pkt(qpair, &prm);
3426 if (unlikely(res != 0)) {
3427 qpair->req->cnt += prm.req_cnt;
3428 goto out_unlock_free_unmap;
3431 pkt = (struct ctio7_to_24xx *)prm.pkt;
3432 pkt->u.status0.flags |= cpu_to_le16(CTIO7_FLAGS_DATA_OUT |
3433 CTIO7_FLAGS_STATUS_MODE_0);
3435 if (cmd->se_cmd.prot_op == TARGET_PROT_NORMAL)
3436 qlt_load_data_segments(&prm);
3438 cmd->state = QLA_TGT_STATE_NEED_DATA;
3439 cmd->cmd_sent_to_fw = 1;
3440 cmd->ctio_flags = le16_to_cpu(pkt->u.status0.flags);
3442 /* Memory Barrier */
3444 if (qpair->reqq_start_iocbs)
3445 qpair->reqq_start_iocbs(qpair);
3447 qla2x00_start_iocbs(vha, qpair->req);
3448 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
3452 out_unlock_free_unmap:
3453 qlt_unmap_sg(vha, cmd);
3454 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
3458 EXPORT_SYMBOL(qlt_rdy_to_xfer);
3462 * it is assumed either hardware_lock or qpair lock is held.
3465 qlt_handle_dif_error(struct qla_qpair *qpair, struct qla_tgt_cmd *cmd,
3466 struct ctio_crc_from_fw *sts)
3468 uint8_t *ap = &sts->actual_dif[0];
3469 uint8_t *ep = &sts->expected_dif[0];
3470 uint64_t lba = cmd->se_cmd.t_task_lba;
3471 uint8_t scsi_status, sense_key, asc, ascq;
3472 unsigned long flags;
3473 struct scsi_qla_host *vha = cmd->vha;
3475 cmd->trc_flags |= TRC_DIF_ERR;
3477 cmd->a_guard = be16_to_cpu(*(uint16_t *)(ap + 0));
3478 cmd->a_app_tag = be16_to_cpu(*(uint16_t *)(ap + 2));
3479 cmd->a_ref_tag = be32_to_cpu(*(uint32_t *)(ap + 4));
3481 cmd->e_guard = be16_to_cpu(*(uint16_t *)(ep + 0));
3482 cmd->e_app_tag = be16_to_cpu(*(uint16_t *)(ep + 2));
3483 cmd->e_ref_tag = be32_to_cpu(*(uint32_t *)(ep + 4));
3485 ql_dbg(ql_dbg_tgt_dif, vha, 0xf075,
3486 "%s: aborted %d state %d\n", __func__, cmd->aborted, cmd->state);
3488 scsi_status = sense_key = asc = ascq = 0;
3490 /* check appl tag */
3491 if (cmd->e_app_tag != cmd->a_app_tag) {
3492 ql_dbg(ql_dbg_tgt_dif, vha, 0xe00d,
3493 "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]",
3494 cmd->cdb[0], lba, (lba+cmd->num_blks), cmd->num_blks,
3495 cmd->a_ref_tag, cmd->e_ref_tag, cmd->a_app_tag,
3496 cmd->e_app_tag, cmd->a_guard, cmd->e_guard, cmd,
3497 cmd->atio.u.isp24.fcp_hdr.ox_id);
3499 cmd->dif_err_code = DIF_ERR_APP;
3500 scsi_status = SAM_STAT_CHECK_CONDITION;
3501 sense_key = ABORTED_COMMAND;
3507 if (cmd->e_ref_tag != cmd->a_ref_tag) {
3508 ql_dbg(ql_dbg_tgt_dif, vha, 0xe00e,
3509 "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] ",
3510 cmd->cdb[0], lba, (lba+cmd->num_blks), cmd->num_blks,
3511 cmd->a_ref_tag, cmd->e_ref_tag, cmd->a_app_tag,
3512 cmd->e_app_tag, cmd->a_guard, cmd->e_guard, cmd,
3513 cmd->atio.u.isp24.fcp_hdr.ox_id);
3515 cmd->dif_err_code = DIF_ERR_REF;
3516 scsi_status = SAM_STAT_CHECK_CONDITION;
3517 sense_key = ABORTED_COMMAND;
3524 if (cmd->e_guard != cmd->a_guard) {
3525 ql_dbg(ql_dbg_tgt_dif, vha, 0xe012,
3526 "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]",
3527 cmd->cdb[0], lba, (lba+cmd->num_blks), cmd->num_blks,
3528 cmd->a_ref_tag, cmd->e_ref_tag, cmd->a_app_tag,
3529 cmd->e_app_tag, cmd->a_guard, cmd->e_guard, cmd,
3530 cmd->atio.u.isp24.fcp_hdr.ox_id);
3532 cmd->dif_err_code = DIF_ERR_GRD;
3533 scsi_status = SAM_STAT_CHECK_CONDITION;
3534 sense_key = ABORTED_COMMAND;
3539 switch (cmd->state) {
3540 case QLA_TGT_STATE_NEED_DATA:
3541 /* handle_data will load DIF error code */
3542 cmd->state = QLA_TGT_STATE_DATA_IN;
3543 vha->hw->tgt.tgt_ops->handle_data(cmd);
3546 spin_lock_irqsave(&cmd->cmd_lock, flags);
3548 spin_unlock_irqrestore(&cmd->cmd_lock, flags);
3549 vha->hw->tgt.tgt_ops->free_cmd(cmd);
3552 spin_unlock_irqrestore(&cmd->cmd_lock, flags);
3554 qlt_send_resp_ctio(qpair, cmd, scsi_status, sense_key, asc,
3556 /* assume scsi status gets out on the wire.
3557 * Will not wait for completion.
3559 vha->hw->tgt.tgt_ops->free_cmd(cmd);
3564 /* If hardware_lock held on entry, might drop it, then reaquire */
3565 /* This function sends the appropriate CTIO to ISP 2xxx or 24xx */
3566 static int __qlt_send_term_imm_notif(struct scsi_qla_host *vha,
3567 struct imm_ntfy_from_isp *ntfy)
3569 struct nack_to_isp *nack;
3570 struct qla_hw_data *ha = vha->hw;
3574 ql_dbg(ql_dbg_tgt_tmr, vha, 0xe01c,
3575 "Sending TERM ELS CTIO (ha=%p)\n", ha);
3577 pkt = (request_t *)qla2x00_alloc_iocbs(vha, NULL);
3579 ql_dbg(ql_dbg_tgt, vha, 0xe080,
3580 "qla_target(%d): %s failed: unable to allocate "
3581 "request packet\n", vha->vp_idx, __func__);
3585 pkt->entry_type = NOTIFY_ACK_TYPE;
3586 pkt->entry_count = 1;
3587 pkt->handle = QLA_TGT_SKIP_HANDLE;
3589 nack = (struct nack_to_isp *)pkt;
3590 nack->ox_id = ntfy->ox_id;
3592 nack->u.isp24.nport_handle = ntfy->u.isp24.nport_handle;
3593 if (le16_to_cpu(ntfy->u.isp24.status) == IMM_NTFY_ELS) {
3594 nack->u.isp24.flags = ntfy->u.isp24.flags &
3595 __constant_cpu_to_le32(NOTIFY24XX_FLAGS_PUREX_IOCB);
3599 nack->u.isp24.flags |=
3600 __constant_cpu_to_le16(NOTIFY_ACK_FLAGS_TERMINATE);
3602 nack->u.isp24.srr_rx_id = ntfy->u.isp24.srr_rx_id;
3603 nack->u.isp24.status = ntfy->u.isp24.status;
3604 nack->u.isp24.status_subcode = ntfy->u.isp24.status_subcode;
3605 nack->u.isp24.fw_handle = ntfy->u.isp24.fw_handle;
3606 nack->u.isp24.exchange_address = ntfy->u.isp24.exchange_address;
3607 nack->u.isp24.srr_rel_offs = ntfy->u.isp24.srr_rel_offs;
3608 nack->u.isp24.srr_ui = ntfy->u.isp24.srr_ui;
3609 nack->u.isp24.vp_index = ntfy->u.isp24.vp_index;
3611 qla2x00_start_iocbs(vha, vha->req);
3615 static void qlt_send_term_imm_notif(struct scsi_qla_host *vha,
3616 struct imm_ntfy_from_isp *imm, int ha_locked)
3620 WARN_ON_ONCE(!ha_locked);
3621 rc = __qlt_send_term_imm_notif(vha, imm);
3622 pr_debug("rc = %d\n", rc);
3626 * If hardware_lock held on entry, might drop it, then reaquire
3627 * This function sends the appropriate CTIO to ISP 2xxx or 24xx
3629 static int __qlt_send_term_exchange(struct qla_qpair *qpair,
3630 struct qla_tgt_cmd *cmd,
3631 struct atio_from_isp *atio)
3633 struct scsi_qla_host *vha = qpair->vha;
3634 struct ctio7_to_24xx *ctio24;
3635 struct qla_hw_data *ha = vha->hw;
3640 ql_dbg(ql_dbg_tgt, vha, 0xe009, "Sending TERM EXCH CTIO (ha=%p)\n", ha);
3645 pkt = (request_t *)qla2x00_alloc_iocbs_ready(qpair, NULL);
3647 ql_dbg(ql_dbg_tgt, vha, 0xe050,
3648 "qla_target(%d): %s failed: unable to allocate "
3649 "request packet\n", vha->vp_idx, __func__);
3654 if (cmd->state < QLA_TGT_STATE_PROCESSED) {
3655 ql_dbg(ql_dbg_tgt, vha, 0xe051,
3656 "qla_target(%d): Terminating cmd %p with "
3657 "incorrect state %d\n", vha->vp_idx, cmd,
3663 qpair->tgt_counters.num_term_xchg_sent++;
3664 pkt->entry_count = 1;
3665 pkt->handle = QLA_TGT_SKIP_HANDLE | CTIO_COMPLETION_HANDLE_MARK;
3667 ctio24 = (struct ctio7_to_24xx *)pkt;
3668 ctio24->entry_type = CTIO_TYPE7;
3669 ctio24->nport_handle = CTIO7_NHANDLE_UNRECOGNIZED;
3670 ctio24->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
3671 ctio24->vp_index = vha->vp_idx;
3672 ctio24->initiator_id[0] = atio->u.isp24.fcp_hdr.s_id[2];
3673 ctio24->initiator_id[1] = atio->u.isp24.fcp_hdr.s_id[1];
3674 ctio24->initiator_id[2] = atio->u.isp24.fcp_hdr.s_id[0];
3675 ctio24->exchange_addr = atio->u.isp24.exchange_addr;
3676 temp = (atio->u.isp24.attr << 9) | CTIO7_FLAGS_STATUS_MODE_1 |
3677 CTIO7_FLAGS_TERMINATE;
3678 ctio24->u.status1.flags = cpu_to_le16(temp);
3679 temp = be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id);
3680 ctio24->u.status1.ox_id = cpu_to_le16(temp);
3682 /* Memory Barrier */
3684 if (qpair->reqq_start_iocbs)
3685 qpair->reqq_start_iocbs(qpair);
3687 qla2x00_start_iocbs(vha, qpair->req);
3691 static void qlt_send_term_exchange(struct qla_qpair *qpair,
3692 struct qla_tgt_cmd *cmd, struct atio_from_isp *atio, int ha_locked,
3695 struct scsi_qla_host *vha;
3696 unsigned long flags = 0;
3699 /* why use different vha? NPIV */
3706 rc = __qlt_send_term_exchange(qpair, cmd, atio);
3708 qlt_alloc_qfull_cmd(vha, atio, 0, 0);
3711 spin_lock_irqsave(qpair->qp_lock_ptr, flags);
3712 rc = __qlt_send_term_exchange(qpair, cmd, atio);
3714 qlt_alloc_qfull_cmd(vha, atio, 0, 0);
3717 if (cmd && !ul_abort && !cmd->aborted) {
3719 qlt_unmap_sg(vha, cmd);
3720 vha->hw->tgt.tgt_ops->free_cmd(cmd);
3724 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
3729 static void qlt_init_term_exchange(struct scsi_qla_host *vha)
3731 struct list_head free_list;
3732 struct qla_tgt_cmd *cmd, *tcmd;
3734 vha->hw->tgt.leak_exchg_thresh_hold =
3735 (vha->hw->cur_fw_xcb_count/100) * LEAK_EXCHG_THRESH_HOLD_PERCENT;
3738 if (!list_empty(&vha->hw->tgt.q_full_list)) {
3739 INIT_LIST_HEAD(&free_list);
3740 list_splice_init(&vha->hw->tgt.q_full_list, &free_list);
3742 list_for_each_entry_safe(cmd, tcmd, &free_list, cmd_list) {
3743 list_del(&cmd->cmd_list);
3744 /* This cmd was never sent to TCM. There is no need
3745 * to schedule free or call free_cmd
3748 vha->hw->tgt.num_qfull_cmds_alloc--;
3751 vha->hw->tgt.num_qfull_cmds_dropped = 0;
3754 static void qlt_chk_exch_leak_thresh_hold(struct scsi_qla_host *vha)
3756 uint32_t total_leaked;
3758 total_leaked = vha->hw->tgt.num_qfull_cmds_dropped;
3760 if (vha->hw->tgt.leak_exchg_thresh_hold &&
3761 (total_leaked > vha->hw->tgt.leak_exchg_thresh_hold)) {
3763 ql_dbg(ql_dbg_tgt, vha, 0xe079,
3764 "Chip reset due to exchange starvation: %d/%d.\n",
3765 total_leaked, vha->hw->cur_fw_xcb_count);
3767 if (IS_P3P_TYPE(vha->hw))
3768 set_bit(FCOE_CTX_RESET_NEEDED, &vha->dpc_flags);
3770 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
3771 qla2xxx_wake_dpc(vha);
3776 int qlt_abort_cmd(struct qla_tgt_cmd *cmd)
3778 struct qla_tgt *tgt = cmd->tgt;
3779 struct scsi_qla_host *vha = tgt->vha;
3780 struct se_cmd *se_cmd = &cmd->se_cmd;
3781 unsigned long flags;
3783 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf014,
3784 "qla_target(%d): terminating exchange for aborted cmd=%p "
3785 "(se_cmd=%p, tag=%llu)", vha->vp_idx, cmd, &cmd->se_cmd,
3788 spin_lock_irqsave(&cmd->cmd_lock, flags);
3790 spin_unlock_irqrestore(&cmd->cmd_lock, flags);
3792 * It's normal to see 2 calls in this path:
3793 * 1) XFER Rdy completion + CMD_T_ABORT
3794 * 2) TCM TMR - drain_state_list
3796 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf016,
3797 "multiple abort. %p transport_state %x, t_state %x, "
3798 "se_cmd_flags %x\n", cmd, cmd->se_cmd.transport_state,
3799 cmd->se_cmd.t_state, cmd->se_cmd.se_cmd_flags);
3803 cmd->trc_flags |= TRC_ABORT;
3804 spin_unlock_irqrestore(&cmd->cmd_lock, flags);
3806 qlt_send_term_exchange(cmd->qpair, cmd, &cmd->atio, 0, 1);
3809 EXPORT_SYMBOL(qlt_abort_cmd);
3811 void qlt_free_cmd(struct qla_tgt_cmd *cmd)
3813 struct fc_port *sess = cmd->sess;
3815 ql_dbg(ql_dbg_tgt, cmd->vha, 0xe074,
3816 "%s: se_cmd[%p] ox_id %04x\n",
3817 __func__, &cmd->se_cmd,
3818 be16_to_cpu(cmd->atio.u.isp24.fcp_hdr.ox_id));
3820 BUG_ON(cmd->cmd_in_wq);
3823 qlt_unmap_sg(cmd->vha, cmd);
3826 qlt_decr_num_pend_cmds(cmd->vha);
3828 BUG_ON(cmd->sg_mapped);
3829 cmd->jiffies_at_free = get_jiffies_64();
3830 if (unlikely(cmd->free_sg))
3833 if (!sess || !sess->se_sess) {
3837 cmd->jiffies_at_free = get_jiffies_64();
3838 target_free_tag(sess->se_sess, &cmd->se_cmd);
3840 EXPORT_SYMBOL(qlt_free_cmd);
3843 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
3845 static int qlt_term_ctio_exchange(struct qla_qpair *qpair, void *ctio,
3846 struct qla_tgt_cmd *cmd, uint32_t status)
3849 struct scsi_qla_host *vha = qpair->vha;
3851 if (cmd->se_cmd.prot_op)
3852 ql_dbg(ql_dbg_tgt_dif, vha, 0xe013,
3853 "Term DIF cmd: lba[0x%llx|%lld] len[0x%x] "
3854 "se_cmd=%p tag[%x] op %#x/%s",
3856 cmd->num_blks, &cmd->se_cmd,
3857 cmd->atio.u.isp24.exchange_addr,
3858 cmd->se_cmd.prot_op,
3859 prot_op_str(cmd->se_cmd.prot_op));
3862 struct ctio7_from_24xx *c = (struct ctio7_from_24xx *)ctio;
3865 cpu_to_le16(OF_TERM_EXCH));
3870 qlt_send_term_exchange(qpair, cmd, &cmd->atio, 1, 0);
3876 /* ha->hardware_lock supposed to be held on entry */
3877 static void *qlt_ctio_to_cmd(struct scsi_qla_host *vha,
3878 struct rsp_que *rsp, uint32_t handle, void *ctio)
3881 struct req_que *req;
3882 int qid = GET_QID(handle);
3883 uint32_t h = handle & ~QLA_TGT_HANDLE_MASK;
3885 if (unlikely(h == QLA_TGT_SKIP_HANDLE))
3888 if (qid == rsp->req->id) {
3890 } else if (vha->hw->req_q_map[qid]) {
3891 ql_dbg(ql_dbg_tgt_mgt, vha, 0x1000a,
3892 "qla_target(%d): CTIO completion with different QID %d handle %x\n",
3893 vha->vp_idx, rsp->id, handle);
3894 req = vha->hw->req_q_map[qid];
3899 h &= QLA_CMD_HANDLE_MASK;
3901 if (h != QLA_TGT_NULL_HANDLE) {
3902 if (unlikely(h >= req->num_outstanding_cmds)) {
3903 ql_dbg(ql_dbg_tgt, vha, 0xe052,
3904 "qla_target(%d): Wrong handle %x received\n",
3905 vha->vp_idx, handle);
3909 cmd = (void *) req->outstanding_cmds[h];
3910 if (unlikely(cmd == NULL)) {
3911 ql_dbg(ql_dbg_async, vha, 0xe053,
3912 "qla_target(%d): Suspicious: unable to find the command with handle %x req->id %d rsp->id %d\n",
3913 vha->vp_idx, handle, req->id, rsp->id);
3916 req->outstanding_cmds[h] = NULL;
3917 } else if (ctio != NULL) {
3918 /* We can't get loop ID from CTIO7 */
3919 ql_dbg(ql_dbg_tgt, vha, 0xe054,
3920 "qla_target(%d): Wrong CTIO received: QLA24xx doesn't "
3921 "support NULL handles\n", vha->vp_idx);
3929 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
3931 static void qlt_do_ctio_completion(struct scsi_qla_host *vha,
3932 struct rsp_que *rsp, uint32_t handle, uint32_t status, void *ctio)
3934 struct qla_hw_data *ha = vha->hw;
3935 struct se_cmd *se_cmd;
3936 struct qla_tgt_cmd *cmd;
3937 struct qla_qpair *qpair = rsp->qpair;
3939 if (handle & CTIO_INTERMEDIATE_HANDLE_MARK) {
3940 /* That could happen only in case of an error/reset/abort */
3941 if (status != CTIO_SUCCESS) {
3942 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01d,
3943 "Intermediate CTIO received"
3944 " (status %x)\n", status);
3949 cmd = qlt_ctio_to_cmd(vha, rsp, handle, ctio);
3953 se_cmd = &cmd->se_cmd;
3954 cmd->cmd_sent_to_fw = 0;
3956 qlt_unmap_sg(vha, cmd);
3958 if (unlikely(status != CTIO_SUCCESS)) {
3959 switch (status & 0xFFFF) {
3960 case CTIO_INVALID_RX_ID:
3961 if (printk_ratelimit())
3962 dev_info(&vha->hw->pdev->dev,
3963 "qla_target(%d): CTIO with INVALID_RX_ID ATIO attr %x CTIO Flags %x|%x\n",
3964 vha->vp_idx, cmd->atio.u.isp24.attr,
3965 ((cmd->ctio_flags >> 9) & 0xf),
3969 case CTIO_LIP_RESET:
3970 case CTIO_TARGET_RESET:
3972 /* driver request abort via Terminate exchange */
3975 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf058,
3976 "qla_target(%d): CTIO with "
3977 "status %#x received, state %x, se_cmd %p, "
3978 "(LIP_RESET=e, ABORTED=2, TARGET_RESET=17, "
3979 "TIMEOUT=b, INVALID_RX_ID=8)\n", vha->vp_idx,
3980 status, cmd->state, se_cmd);
3983 case CTIO_PORT_LOGGED_OUT:
3984 case CTIO_PORT_UNAVAILABLE:
3987 (status & 0xFFFF) == CTIO_PORT_LOGGED_OUT;
3989 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf059,
3990 "qla_target(%d): CTIO with %s status %x "
3991 "received (state %x, se_cmd %p)\n", vha->vp_idx,
3992 logged_out ? "PORT LOGGED OUT" : "PORT UNAVAILABLE",
3993 status, cmd->state, se_cmd);
3995 if (logged_out && cmd->sess) {
3997 * Session is already logged out, but we need
3998 * to notify initiator, who's not aware of this
4000 cmd->sess->send_els_logo = 1;
4001 ql_dbg(ql_dbg_disc, vha, 0x20f8,
4002 "%s %d %8phC post del sess\n",
4003 __func__, __LINE__, cmd->sess->port_name);
4005 qlt_schedule_sess_for_deletion(cmd->sess);
4009 case CTIO_DIF_ERROR: {
4010 struct ctio_crc_from_fw *crc =
4011 (struct ctio_crc_from_fw *)ctio;
4012 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf073,
4013 "qla_target(%d): CTIO with DIF_ERROR status %x "
4014 "received (state %x, ulp_cmd %p) actual_dif[0x%llx] "
4015 "expect_dif[0x%llx]\n",
4016 vha->vp_idx, status, cmd->state, se_cmd,
4017 *((u64 *)&crc->actual_dif[0]),
4018 *((u64 *)&crc->expected_dif[0]));
4020 qlt_handle_dif_error(qpair, cmd, ctio);
4024 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05b,
4025 "qla_target(%d): CTIO with error status 0x%x received (state %x, se_cmd %p\n",
4026 vha->vp_idx, status, cmd->state, se_cmd);
4031 /* "cmd->aborted" means
4032 * cmd is already aborted/terminated, we don't
4033 * need to terminate again. The exchange is already
4034 * cleaned up/freed at FW level. Just cleanup at driver
4037 if ((cmd->state != QLA_TGT_STATE_NEED_DATA) &&
4039 cmd->trc_flags |= TRC_CTIO_ERR;
4040 if (qlt_term_ctio_exchange(qpair, ctio, cmd, status))
4045 if (cmd->state == QLA_TGT_STATE_PROCESSED) {
4046 cmd->trc_flags |= TRC_CTIO_DONE;
4047 } else if (cmd->state == QLA_TGT_STATE_NEED_DATA) {
4048 cmd->state = QLA_TGT_STATE_DATA_IN;
4050 if (status == CTIO_SUCCESS)
4051 cmd->write_data_transferred = 1;
4053 ha->tgt.tgt_ops->handle_data(cmd);
4055 } else if (cmd->aborted) {
4056 cmd->trc_flags |= TRC_CTIO_ABORTED;
4057 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01e,
4058 "Aborted command %p (tag %lld) finished\n", cmd, se_cmd->tag);
4060 cmd->trc_flags |= TRC_CTIO_STRANGE;
4061 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05c,
4062 "qla_target(%d): A command in state (%d) should "
4063 "not return a CTIO complete\n", vha->vp_idx, cmd->state);
4066 if (unlikely(status != CTIO_SUCCESS) &&
4068 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01f, "Finishing failed CTIO\n");
4072 ha->tgt.tgt_ops->free_cmd(cmd);
4075 static inline int qlt_get_fcp_task_attr(struct scsi_qla_host *vha,
4080 switch (task_codes) {
4081 case ATIO_SIMPLE_QUEUE:
4082 fcp_task_attr = TCM_SIMPLE_TAG;
4084 case ATIO_HEAD_OF_QUEUE:
4085 fcp_task_attr = TCM_HEAD_TAG;
4087 case ATIO_ORDERED_QUEUE:
4088 fcp_task_attr = TCM_ORDERED_TAG;
4090 case ATIO_ACA_QUEUE:
4091 fcp_task_attr = TCM_ACA_TAG;
4094 fcp_task_attr = TCM_SIMPLE_TAG;
4097 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05d,
4098 "qla_target: unknown task code %x, use ORDERED instead\n",
4100 fcp_task_attr = TCM_ORDERED_TAG;
4104 return fcp_task_attr;
4108 * Process context for I/O path into tcm_qla2xxx code
4110 static void __qlt_do_work(struct qla_tgt_cmd *cmd)
4112 scsi_qla_host_t *vha = cmd->vha;
4113 struct qla_hw_data *ha = vha->hw;
4114 struct fc_port *sess = cmd->sess;
4115 struct atio_from_isp *atio = &cmd->atio;
4117 unsigned long flags;
4118 uint32_t data_length;
4119 int ret, fcp_task_attr, data_dir, bidi = 0;
4120 struct qla_qpair *qpair = cmd->qpair;
4123 cmd->trc_flags |= TRC_DO_WORK;
4126 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf082,
4127 "cmd with tag %u is aborted\n",
4128 cmd->atio.u.isp24.exchange_addr);
4132 spin_lock_init(&cmd->cmd_lock);
4133 cdb = &atio->u.isp24.fcp_cmnd.cdb[0];
4134 cmd->se_cmd.tag = atio->u.isp24.exchange_addr;
4136 if (atio->u.isp24.fcp_cmnd.rddata &&
4137 atio->u.isp24.fcp_cmnd.wrdata) {
4139 data_dir = DMA_TO_DEVICE;
4140 } else if (atio->u.isp24.fcp_cmnd.rddata)
4141 data_dir = DMA_FROM_DEVICE;
4142 else if (atio->u.isp24.fcp_cmnd.wrdata)
4143 data_dir = DMA_TO_DEVICE;
4145 data_dir = DMA_NONE;
4147 fcp_task_attr = qlt_get_fcp_task_attr(vha,
4148 atio->u.isp24.fcp_cmnd.task_attr);
4149 data_length = get_datalen_for_atio(atio);
4151 ret = ha->tgt.tgt_ops->handle_cmd(vha, cmd, cdb, data_length,
4152 fcp_task_attr, data_dir, bidi);
4156 * Drop extra session reference from qlt_handle_cmd_for_atio().
4158 ha->tgt.tgt_ops->put_sess(sess);
4162 ql_dbg(ql_dbg_io, vha, 0x3060, "Terminating work cmd %p", cmd);
4164 * cmd has not sent to target yet, so pass NULL as the second
4165 * argument to qlt_send_term_exchange() and free the memory here.
4167 cmd->trc_flags |= TRC_DO_WORK_ERR;
4168 spin_lock_irqsave(qpair->qp_lock_ptr, flags);
4169 qlt_send_term_exchange(qpair, NULL, &cmd->atio, 1, 0);
4171 qlt_decr_num_pend_cmds(vha);
4172 target_free_tag(sess->se_sess, &cmd->se_cmd);
4173 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
4175 ha->tgt.tgt_ops->put_sess(sess);
4178 static void qlt_do_work(struct work_struct *work)
4180 struct qla_tgt_cmd *cmd = container_of(work, struct qla_tgt_cmd, work);
4181 scsi_qla_host_t *vha = cmd->vha;
4182 unsigned long flags;
4184 spin_lock_irqsave(&vha->cmd_list_lock, flags);
4185 list_del(&cmd->cmd_list);
4186 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
4191 void qlt_clr_qp_table(struct scsi_qla_host *vha)
4193 unsigned long flags;
4194 struct qla_hw_data *ha = vha->hw;
4195 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
4199 ql_log(ql_log_info, vha, 0x706c,
4200 "User update Number of Active Qpairs %d\n",
4201 ha->tgt.num_act_qpairs);
4203 spin_lock_irqsave(&ha->tgt.atio_lock, flags);
4205 btree_for_each_safe64(&tgt->lun_qpair_map, key, node)
4206 btree_remove64(&tgt->lun_qpair_map, key);
4208 ha->base_qpair->lun_cnt = 0;
4209 for (key = 0; key < ha->max_qpairs; key++)
4210 if (ha->queue_pair_map[key])
4211 ha->queue_pair_map[key]->lun_cnt = 0;
4213 spin_unlock_irqrestore(&ha->tgt.atio_lock, flags);
4216 static void qlt_assign_qpair(struct scsi_qla_host *vha,
4217 struct qla_tgt_cmd *cmd)
4219 struct qla_qpair *qpair, *qp;
4220 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
4221 struct qla_qpair_hint *h;
4223 if (vha->flags.qpairs_available) {
4224 h = btree_lookup64(&tgt->lun_qpair_map, cmd->unpacked_lun);
4226 /* spread lun to qpair ratio evently */
4228 struct scsi_qla_host *base_vha =
4229 pci_get_drvdata(vha->hw->pdev);
4231 qpair = vha->hw->base_qpair;
4232 if (qpair->lun_cnt == 0) {
4234 h = qla_qpair_to_hint(tgt, qpair);
4236 rc = btree_insert64(&tgt->lun_qpair_map,
4237 cmd->unpacked_lun, h, GFP_ATOMIC);
4240 ql_log(ql_log_info, vha, 0xd037,
4241 "Unable to insert lun %llx into lun_qpair_map\n",
4246 lcnt = qpair->lun_cnt;
4250 list_for_each_entry(qp, &base_vha->qp_list,
4252 if (qp->lun_cnt == 0) {
4254 h = qla_qpair_to_hint(tgt, qp);
4256 rc = btree_insert64(&tgt->lun_qpair_map,
4257 cmd->unpacked_lun, h, GFP_ATOMIC);
4260 ql_log(ql_log_info, vha, 0xd038,
4261 "Unable to insert lun %llx into lun_qpair_map\n",
4267 if (qp->lun_cnt < lcnt) {
4276 h = qla_qpair_to_hint(tgt, qpair);
4278 rc = btree_insert64(&tgt->lun_qpair_map,
4279 cmd->unpacked_lun, h, GFP_ATOMIC);
4282 ql_log(ql_log_info, vha, 0xd039,
4283 "Unable to insert lun %llx into lun_qpair_map\n",
4288 h = &tgt->qphints[0];
4291 cmd->qpair = h->qpair;
4292 cmd->se_cmd.cpuid = h->cpuid;
4295 static struct qla_tgt_cmd *qlt_get_tag(scsi_qla_host_t *vha,
4296 struct fc_port *sess,
4297 struct atio_from_isp *atio)
4299 struct se_session *se_sess = sess->se_sess;
4300 struct qla_tgt_cmd *cmd;
4303 tag = sbitmap_queue_get(&se_sess->sess_tag_pool, &cpu);
4307 cmd = &((struct qla_tgt_cmd *)se_sess->sess_cmd_map)[tag];
4308 memset(cmd, 0, sizeof(struct qla_tgt_cmd));
4309 cmd->cmd_type = TYPE_TGT_CMD;
4310 memcpy(&cmd->atio, atio, sizeof(*atio));
4311 cmd->state = QLA_TGT_STATE_NEW;
4312 cmd->tgt = vha->vha_tgt.qla_tgt;
4313 qlt_incr_num_pend_cmds(vha);
4315 cmd->se_cmd.map_tag = tag;
4316 cmd->se_cmd.map_cpu = cpu;
4318 cmd->loop_id = sess->loop_id;
4319 cmd->conf_compl_supported = sess->conf_compl_supported;
4322 cmd->jiffies_at_alloc = get_jiffies_64();
4324 cmd->unpacked_lun = scsilun_to_int(
4325 (struct scsi_lun *)&atio->u.isp24.fcp_cmnd.lun);
4326 qlt_assign_qpair(vha, cmd);
4327 cmd->reset_count = vha->hw->base_qpair->chip_reset;
4328 cmd->vp_idx = vha->vp_idx;
4333 /* ha->hardware_lock supposed to be held on entry */
4334 static int qlt_handle_cmd_for_atio(struct scsi_qla_host *vha,
4335 struct atio_from_isp *atio)
4337 struct qla_hw_data *ha = vha->hw;
4338 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
4339 struct fc_port *sess;
4340 struct qla_tgt_cmd *cmd;
4341 unsigned long flags;
4344 if (unlikely(tgt->tgt_stop)) {
4345 ql_dbg(ql_dbg_io, vha, 0x3061,
4346 "New command while device %p is shutting down\n", tgt);
4350 id.b.al_pa = atio->u.isp24.fcp_hdr.s_id[2];
4351 id.b.area = atio->u.isp24.fcp_hdr.s_id[1];
4352 id.b.domain = atio->u.isp24.fcp_hdr.s_id[0];
4353 if (IS_SW_RESV_ADDR(id))
4356 sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha, atio->u.isp24.fcp_hdr.s_id);
4357 if (unlikely(!sess))
4360 /* Another WWN used to have our s_id. Our PLOGI scheduled its
4361 * session deletion, but it's still in sess_del_work wq */
4362 if (sess->deleted) {
4363 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf002,
4364 "New command while old session %p is being deleted\n",
4370 * Do kref_get() before returning + dropping qla_hw_data->hardware_lock.
4372 if (!kref_get_unless_zero(&sess->sess_kref)) {
4373 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf004,
4374 "%s: kref_get fail, %8phC oxid %x \n",
4375 __func__, sess->port_name,
4376 be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id));
4380 cmd = qlt_get_tag(vha, sess, atio);
4382 ql_dbg(ql_dbg_io, vha, 0x3062,
4383 "qla_target(%d): Allocation of cmd failed\n", vha->vp_idx);
4384 ha->tgt.tgt_ops->put_sess(sess);
4389 cmd->trc_flags |= TRC_NEW_CMD;
4391 spin_lock_irqsave(&vha->cmd_list_lock, flags);
4392 list_add_tail(&cmd->cmd_list, &vha->qla_cmd_list);
4393 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
4395 INIT_WORK(&cmd->work, qlt_do_work);
4396 if (vha->flags.qpairs_available) {
4397 queue_work_on(cmd->se_cmd.cpuid, qla_tgt_wq, &cmd->work);
4398 } else if (ha->msix_count) {
4399 if (cmd->atio.u.isp24.fcp_cmnd.rddata)
4400 queue_work_on(smp_processor_id(), qla_tgt_wq,
4403 queue_work_on(cmd->se_cmd.cpuid, qla_tgt_wq,
4406 queue_work(qla_tgt_wq, &cmd->work);
4412 /* ha->hardware_lock supposed to be held on entry */
4413 static int qlt_issue_task_mgmt(struct fc_port *sess, u64 lun,
4414 int fn, void *iocb, int flags)
4416 struct scsi_qla_host *vha = sess->vha;
4417 struct qla_hw_data *ha = vha->hw;
4418 struct qla_tgt_mgmt_cmd *mcmd;
4419 struct atio_from_isp *a = (struct atio_from_isp *)iocb;
4420 struct qla_qpair_hint *h = &vha->vha_tgt.qla_tgt->qphints[0];
4422 mcmd = mempool_alloc(qla_tgt_mgmt_cmd_mempool, GFP_ATOMIC);
4424 ql_dbg(ql_dbg_tgt_tmr, vha, 0x10009,
4425 "qla_target(%d): Allocation of management "
4426 "command failed, some commands and their data could "
4427 "leak\n", vha->vp_idx);
4430 memset(mcmd, 0, sizeof(*mcmd));
4434 memcpy(&mcmd->orig_iocb.imm_ntfy, iocb,
4435 sizeof(mcmd->orig_iocb.imm_ntfy));
4437 mcmd->tmr_func = fn;
4438 mcmd->flags = flags;
4439 mcmd->reset_count = ha->base_qpair->chip_reset;
4440 mcmd->qpair = h->qpair;
4442 mcmd->se_cmd.cpuid = h->cpuid;
4443 mcmd->unpacked_lun = lun;
4446 case QLA_TGT_LUN_RESET:
4447 case QLA_TGT_CLEAR_TS:
4448 case QLA_TGT_ABORT_TS:
4449 abort_cmds_for_lun(vha, lun, a->u.isp24.fcp_hdr.s_id);
4451 case QLA_TGT_CLEAR_ACA:
4452 h = qlt_find_qphint(vha, mcmd->unpacked_lun);
4453 mcmd->qpair = h->qpair;
4454 mcmd->se_cmd.cpuid = h->cpuid;
4457 case QLA_TGT_TARGET_RESET:
4458 case QLA_TGT_NEXUS_LOSS_SESS:
4459 case QLA_TGT_NEXUS_LOSS:
4460 case QLA_TGT_ABORT_ALL:
4466 INIT_WORK(&mcmd->work, qlt_do_tmr_work);
4467 queue_work_on(mcmd->se_cmd.cpuid, qla_tgt_wq,
4473 /* ha->hardware_lock supposed to be held on entry */
4474 static int qlt_handle_task_mgmt(struct scsi_qla_host *vha, void *iocb)
4476 struct atio_from_isp *a = (struct atio_from_isp *)iocb;
4477 struct qla_hw_data *ha = vha->hw;
4478 struct fc_port *sess;
4481 unsigned long flags;
4483 fn = a->u.isp24.fcp_cmnd.task_mgmt_flags;
4485 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
4486 sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha,
4487 a->u.isp24.fcp_hdr.s_id);
4488 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
4491 scsilun_to_int((struct scsi_lun *)&a->u.isp24.fcp_cmnd.lun);
4493 if (sess == NULL || sess->deleted)
4496 return qlt_issue_task_mgmt(sess, unpacked_lun, fn, iocb, 0);
4499 /* ha->hardware_lock supposed to be held on entry */
4500 static int __qlt_abort_task(struct scsi_qla_host *vha,
4501 struct imm_ntfy_from_isp *iocb, struct fc_port *sess)
4503 struct atio_from_isp *a = (struct atio_from_isp *)iocb;
4504 struct qla_hw_data *ha = vha->hw;
4505 struct qla_tgt_mgmt_cmd *mcmd;
4509 mcmd = mempool_alloc(qla_tgt_mgmt_cmd_mempool, GFP_ATOMIC);
4511 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05f,
4512 "qla_target(%d): %s: Allocation of ABORT cmd failed\n",
4513 vha->vp_idx, __func__);
4516 memset(mcmd, 0, sizeof(*mcmd));
4519 memcpy(&mcmd->orig_iocb.imm_ntfy, iocb,
4520 sizeof(mcmd->orig_iocb.imm_ntfy));
4523 scsilun_to_int((struct scsi_lun *)&a->u.isp24.fcp_cmnd.lun);
4524 mcmd->reset_count = ha->base_qpair->chip_reset;
4525 mcmd->tmr_func = QLA_TGT_2G_ABORT_TASK;
4526 mcmd->qpair = ha->base_qpair;
4528 rc = ha->tgt.tgt_ops->handle_tmr(mcmd, unpacked_lun, mcmd->tmr_func,
4529 le16_to_cpu(iocb->u.isp2x.seq_id));
4531 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf060,
4532 "qla_target(%d): tgt_ops->handle_tmr() failed: %d\n",
4534 mempool_free(mcmd, qla_tgt_mgmt_cmd_mempool);
4541 /* ha->hardware_lock supposed to be held on entry */
4542 static int qlt_abort_task(struct scsi_qla_host *vha,
4543 struct imm_ntfy_from_isp *iocb)
4545 struct qla_hw_data *ha = vha->hw;
4546 struct fc_port *sess;
4548 unsigned long flags;
4550 loop_id = GET_TARGET_ID(ha, (struct atio_from_isp *)iocb);
4552 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
4553 sess = ha->tgt.tgt_ops->find_sess_by_loop_id(vha, loop_id);
4554 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
4557 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf025,
4558 "qla_target(%d): task abort for unexisting "
4559 "session\n", vha->vp_idx);
4560 return qlt_sched_sess_work(vha->vha_tgt.qla_tgt,
4561 QLA_TGT_SESS_WORK_ABORT, iocb, sizeof(*iocb));
4564 return __qlt_abort_task(vha, iocb, sess);
4567 void qlt_logo_completion_handler(fc_port_t *fcport, int rc)
4569 if (rc != MBS_COMMAND_COMPLETE) {
4570 ql_dbg(ql_dbg_tgt_mgt, fcport->vha, 0xf093,
4571 "%s: se_sess %p / sess %p from"
4572 " port %8phC loop_id %#04x s_id %02x:%02x:%02x"
4573 " LOGO failed: %#x\n",
4577 fcport->port_name, fcport->loop_id,
4578 fcport->d_id.b.domain, fcport->d_id.b.area,
4579 fcport->d_id.b.al_pa, rc);
4582 fcport->logout_completed = 1;
4586 * ha->hardware_lock supposed to be held on entry (to protect tgt->sess_list)
4588 * Schedules sessions with matching port_id/loop_id but different wwn for
4589 * deletion. Returns existing session with matching wwn if present.
4593 qlt_find_sess_invalidate_other(scsi_qla_host_t *vha, uint64_t wwn,
4594 port_id_t port_id, uint16_t loop_id, struct fc_port **conflict_sess)
4596 struct fc_port *sess = NULL, *other_sess;
4599 *conflict_sess = NULL;
4601 list_for_each_entry(other_sess, &vha->vp_fcports, list) {
4603 other_wwn = wwn_to_u64(other_sess->port_name);
4605 if (wwn == other_wwn) {
4611 /* find other sess with nport_id collision */
4612 if (port_id.b24 == other_sess->d_id.b24) {
4613 if (loop_id != other_sess->loop_id) {
4614 ql_dbg(ql_dbg_tgt_tmr, vha, 0x1000c,
4615 "Invalidating sess %p loop_id %d wwn %llx.\n",
4616 other_sess, other_sess->loop_id, other_wwn);
4619 * logout_on_delete is set by default, but another
4620 * session that has the same s_id/loop_id combo
4621 * might have cleared it when requested this session
4622 * deletion, so don't touch it
4624 qlt_schedule_sess_for_deletion(other_sess);
4627 * Another wwn used to have our s_id/loop_id
4628 * kill the session, but don't free the loop_id
4630 ql_dbg(ql_dbg_tgt_tmr, vha, 0xf01b,
4631 "Invalidating sess %p loop_id %d wwn %llx.\n",
4632 other_sess, other_sess->loop_id, other_wwn);
4634 other_sess->keep_nport_handle = 1;
4635 if (other_sess->disc_state != DSC_DELETED)
4636 *conflict_sess = other_sess;
4637 qlt_schedule_sess_for_deletion(other_sess);
4642 /* find other sess with nport handle collision */
4643 if ((loop_id == other_sess->loop_id) &&
4644 (loop_id != FC_NO_LOOP_ID)) {
4645 ql_dbg(ql_dbg_tgt_tmr, vha, 0x1000d,
4646 "Invalidating sess %p loop_id %d wwn %llx.\n",
4647 other_sess, other_sess->loop_id, other_wwn);
4649 /* Same loop_id but different s_id
4650 * Ok to kill and logout */
4651 qlt_schedule_sess_for_deletion(other_sess);
4658 /* Abort any commands for this s_id waiting on qla_tgt_wq workqueue */
4659 static int abort_cmds_for_s_id(struct scsi_qla_host *vha, port_id_t *s_id)
4661 struct qla_tgt_sess_op *op;
4662 struct qla_tgt_cmd *cmd;
4665 unsigned long flags;
4667 key = (((u32)s_id->b.domain << 16) |
4668 ((u32)s_id->b.area << 8) |
4669 ((u32)s_id->b.al_pa));
4671 spin_lock_irqsave(&vha->cmd_list_lock, flags);
4672 list_for_each_entry(op, &vha->qla_sess_op_cmd_list, cmd_list) {
4673 uint32_t op_key = sid_to_key(op->atio.u.isp24.fcp_hdr.s_id);
4675 if (op_key == key) {
4681 list_for_each_entry(op, &vha->unknown_atio_list, cmd_list) {
4682 uint32_t op_key = sid_to_key(op->atio.u.isp24.fcp_hdr.s_id);
4684 if (op_key == key) {
4690 list_for_each_entry(cmd, &vha->qla_cmd_list, cmd_list) {
4691 uint32_t cmd_key = sid_to_key(cmd->atio.u.isp24.fcp_hdr.s_id);
4693 if (cmd_key == key) {
4698 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
4703 static int qlt_handle_login(struct scsi_qla_host *vha,
4704 struct imm_ntfy_from_isp *iocb)
4706 struct fc_port *sess = NULL, *conflict_sess = NULL;
4709 uint16_t loop_id, wd3_lo;
4711 struct qlt_plogi_ack_t *pla;
4712 unsigned long flags;
4714 lockdep_assert_held(&vha->hw->hardware_lock);
4716 wwn = wwn_to_u64(iocb->u.isp24.port_name);
4718 port_id.b.domain = iocb->u.isp24.port_id[2];
4719 port_id.b.area = iocb->u.isp24.port_id[1];
4720 port_id.b.al_pa = iocb->u.isp24.port_id[0];
4721 port_id.b.rsvd_1 = 0;
4723 loop_id = le16_to_cpu(iocb->u.isp24.nport_handle);
4725 /* Mark all stale commands sitting in qla_tgt_wq for deletion */
4726 abort_cmds_for_s_id(vha, &port_id);
4729 spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
4730 sess = qlt_find_sess_invalidate_other(vha, wwn,
4731 port_id, loop_id, &conflict_sess);
4732 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
4734 ql_dbg(ql_dbg_disc, vha, 0xffff,
4735 "%s %d Term INOT due to WWN=0 lid=%d, NportID %06X ",
4736 __func__, __LINE__, loop_id, port_id.b24);
4737 qlt_send_term_imm_notif(vha, iocb, 1);
4741 if (IS_SW_RESV_ADDR(port_id)) {
4746 pla = qlt_plogi_ack_find_add(vha, &port_id, iocb);
4748 ql_dbg(ql_dbg_disc + ql_dbg_verbose, vha, 0xffff,
4749 "%s %d %8phC Term INOT due to mem alloc fail",
4751 iocb->u.isp24.port_name);
4752 qlt_send_term_imm_notif(vha, iocb, 1);
4756 if (conflict_sess) {
4757 conflict_sess->login_gen++;
4758 qlt_plogi_ack_link(vha, pla, conflict_sess,
4759 QLT_PLOGI_LINK_CONFLICT);
4764 ql_dbg(ql_dbg_disc, vha, 0xffff,
4765 "%s %d %8phC post new sess\n",
4766 __func__, __LINE__, iocb->u.isp24.port_name);
4767 if (iocb->u.isp24.status_subcode == ELS_PLOGI)
4768 qla24xx_post_newsess_work(vha, &port_id,
4769 iocb->u.isp24.port_name,
4770 iocb->u.isp24.u.plogi.node_name,
4771 pla, FC4_TYPE_UNKNOWN);
4773 qla24xx_post_newsess_work(vha, &port_id,
4774 iocb->u.isp24.port_name, NULL,
4775 pla, FC4_TYPE_UNKNOWN);
4780 if (sess->disc_state == DSC_UPD_FCPORT) {
4784 * Remote port registration is still going on from
4785 * previous login. Allow it to finish before we
4786 * accept the new login.
4788 sess->next_disc_state = DSC_DELETE_PEND;
4789 sec = jiffies_to_msecs(jiffies -
4790 sess->jiffies_at_registration) / 1000;
4791 if (sess->sec_since_registration < sec && sec &&
4793 sess->sec_since_registration = sec;
4794 ql_dbg(ql_dbg_disc, vha, 0xffff,
4795 "%s %8phC - Slow Rport registration (%d Sec)\n",
4796 __func__, sess->port_name, sec);
4800 kmem_cache_free(qla_tgt_plogi_cachep, pla);
4802 qlt_send_term_imm_notif(vha, iocb, 1);
4806 qlt_plogi_ack_link(vha, pla, sess, QLT_PLOGI_LINK_SAME_WWN);
4807 sess->d_id = port_id;
4810 if (iocb->u.isp24.status_subcode == ELS_PRLI) {
4811 sess->fw_login_state = DSC_LS_PRLI_PEND;
4813 sess->loop_id = loop_id;
4814 sess->d_id = port_id;
4815 sess->fw_login_state = DSC_LS_PRLI_PEND;
4816 wd3_lo = le16_to_cpu(iocb->u.isp24.u.prli.wd3_lo);
4819 sess->conf_compl_supported = 1;
4821 if ((wd3_lo & BIT_4) == 0)
4822 sess->port_type = FCT_INITIATOR;
4824 sess->port_type = FCT_TARGET;
4827 sess->fw_login_state = DSC_LS_PLOGI_PEND;
4830 ql_dbg(ql_dbg_disc, vha, 0x20f9,
4831 "%s %d %8phC DS %d\n",
4832 __func__, __LINE__, sess->port_name, sess->disc_state);
4834 switch (sess->disc_state) {
4836 qlt_plogi_ack_unref(vha, pla);
4841 * Under normal circumstances we want to release nport handle
4842 * during LOGO process to avoid nport handle leaks inside FW.
4843 * The exception is when LOGO is done while another PLOGI with
4844 * the same nport handle is waiting as might be the case here.
4845 * Note: there is always a possibily of a race where session
4846 * deletion has already started for other reasons (e.g. ACL
4847 * removal) and now PLOGI arrives:
4848 * 1. if PLOGI arrived in FW after nport handle has been freed,
4849 * FW must have assigned this PLOGI a new/same handle and we
4850 * can proceed ACK'ing it as usual when session deletion
4852 * 2. if PLOGI arrived in FW before LOGO with LCF_FREE_NPORT
4853 * bit reached it, the handle has now been released. We'll
4854 * get an error when we ACK this PLOGI. Nothing will be sent
4855 * back to initiator. Initiator should eventually retry
4856 * PLOGI and situation will correct itself.
4858 sess->keep_nport_handle = ((sess->loop_id == loop_id) &&
4859 (sess->d_id.b24 == port_id.b24));
4861 ql_dbg(ql_dbg_disc, vha, 0x20f9,
4862 "%s %d %8phC post del sess\n",
4863 __func__, __LINE__, sess->port_name);
4866 qlt_schedule_sess_for_deletion(sess);
4874 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
4876 static int qlt_24xx_handle_els(struct scsi_qla_host *vha,
4877 struct imm_ntfy_from_isp *iocb)
4879 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
4880 struct qla_hw_data *ha = vha->hw;
4881 struct fc_port *sess = NULL, *conflict_sess = NULL;
4887 unsigned long flags;
4889 lockdep_assert_held(&ha->hardware_lock);
4891 wwn = wwn_to_u64(iocb->u.isp24.port_name);
4893 port_id.b.domain = iocb->u.isp24.port_id[2];
4894 port_id.b.area = iocb->u.isp24.port_id[1];
4895 port_id.b.al_pa = iocb->u.isp24.port_id[0];
4896 port_id.b.rsvd_1 = 0;
4898 loop_id = le16_to_cpu(iocb->u.isp24.nport_handle);
4900 ql_dbg(ql_dbg_disc, vha, 0xf026,
4901 "qla_target(%d): Port ID: %02x:%02x:%02x ELS opcode: 0x%02x lid %d %8phC\n",
4902 vha->vp_idx, iocb->u.isp24.port_id[2],
4903 iocb->u.isp24.port_id[1], iocb->u.isp24.port_id[0],
4904 iocb->u.isp24.status_subcode, loop_id,
4905 iocb->u.isp24.port_name);
4907 /* res = 1 means ack at the end of thread
4908 * res = 0 means ack async/later.
4910 switch (iocb->u.isp24.status_subcode) {
4912 res = qlt_handle_login(vha, iocb);
4917 sess = qla2x00_find_fcport_by_wwpn(vha,
4918 iocb->u.isp24.port_name, 1);
4920 if (sess && sess->plogi_link[QLT_PLOGI_LINK_SAME_WWN]) {
4921 ql_dbg(ql_dbg_disc, vha, 0xffff,
4922 "%s %d %8phC Term PRLI due to PLOGI ACK not completed\n",
4924 iocb->u.isp24.port_name);
4925 qlt_send_term_imm_notif(vha, iocb, 1);
4929 res = qlt_handle_login(vha, iocb);
4933 if (IS_SW_RESV_ADDR(port_id)) {
4938 wd3_lo = le16_to_cpu(iocb->u.isp24.u.prli.wd3_lo);
4941 spin_lock_irqsave(&tgt->ha->tgt.sess_lock, flags);
4942 sess = qlt_find_sess_invalidate_other(vha, wwn, port_id,
4943 loop_id, &conflict_sess);
4944 spin_unlock_irqrestore(&tgt->ha->tgt.sess_lock, flags);
4947 if (conflict_sess) {
4948 switch (conflict_sess->disc_state) {
4950 case DSC_DELETE_PEND:
4953 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf09b,
4954 "PRLI with conflicting sess %p port %8phC\n",
4955 conflict_sess, conflict_sess->port_name);
4956 conflict_sess->fw_login_state =
4957 DSC_LS_PORT_UNAVAIL;
4958 qlt_send_term_imm_notif(vha, iocb, 1);
4965 bool delete = false;
4968 spin_lock_irqsave(&tgt->ha->tgt.sess_lock, flags);
4969 switch (sess->fw_login_state) {
4970 case DSC_LS_PLOGI_PEND:
4971 case DSC_LS_PLOGI_COMP:
4972 case DSC_LS_PRLI_COMP:
4979 switch (sess->disc_state) {
4980 case DSC_UPD_FCPORT:
4981 spin_unlock_irqrestore(&tgt->ha->tgt.sess_lock,
4984 sec = jiffies_to_msecs(jiffies -
4985 sess->jiffies_at_registration)/1000;
4986 if (sess->sec_since_registration < sec && sec &&
4988 sess->sec_since_registration = sec;
4989 ql_dbg(ql_dbg_disc, sess->vha, 0xffff,
4990 "%s %8phC : Slow Rport registration(%d Sec)\n",
4991 __func__, sess->port_name, sec);
4993 qlt_send_term_imm_notif(vha, iocb, 1);
4996 case DSC_LOGIN_PEND:
4998 case DSC_LOGIN_COMPLETE:
5007 spin_unlock_irqrestore(&tgt->ha->tgt.sess_lock,
5010 * Impatient initiator sent PRLI before last
5011 * PLOGI could finish. Will force him to re-try,
5012 * while last one finishes.
5014 ql_log(ql_log_warn, sess->vha, 0xf095,
5015 "sess %p PRLI received, before plogi ack.\n",
5017 qlt_send_term_imm_notif(vha, iocb, 1);
5023 * This shouldn't happen under normal circumstances,
5024 * since we have deleted the old session during PLOGI
5026 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf096,
5027 "PRLI (loop_id %#04x) for existing sess %p (loop_id %#04x)\n",
5028 sess->loop_id, sess, iocb->u.isp24.nport_handle);
5031 sess->loop_id = loop_id;
5032 sess->d_id = port_id;
5033 sess->fw_login_state = DSC_LS_PRLI_PEND;
5036 sess->conf_compl_supported = 1;
5038 if ((wd3_lo & BIT_4) == 0)
5039 sess->port_type = FCT_INITIATOR;
5041 sess->port_type = FCT_TARGET;
5043 spin_unlock_irqrestore(&tgt->ha->tgt.sess_lock, flags);
5045 res = 1; /* send notify ack */
5047 /* Make session global (not used in fabric mode) */
5048 if (ha->current_topology != ISP_CFG_F) {
5050 ql_dbg(ql_dbg_disc, vha, 0x20fa,
5051 "%s %d %8phC post nack\n",
5052 __func__, __LINE__, sess->port_name);
5053 qla24xx_post_nack_work(vha, sess, iocb,
5057 set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
5058 set_bit(LOCAL_LOOP_UPDATE, &vha->dpc_flags);
5059 qla2xxx_wake_dpc(vha);
5063 ql_dbg(ql_dbg_disc, vha, 0x20fb,
5064 "%s %d %8phC post nack\n",
5065 __func__, __LINE__, sess->port_name);
5066 qla24xx_post_nack_work(vha, sess, iocb,
5074 if (le16_to_cpu(iocb->u.isp24.flags) &
5075 NOTIFY24XX_FLAGS_GLOBAL_TPRLO) {
5077 qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS);
5084 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
5085 sess = qla2x00_find_fcport_by_loopid(vha, loop_id);
5086 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
5090 sess->fw_login_state = DSC_LS_LOGO_PEND;
5091 sess->logo_ack_needed = 1;
5092 memcpy(sess->iocb, iocb, IOCB_SIZE);
5095 res = qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS_SESS);
5097 ql_dbg(ql_dbg_disc, vha, 0x20fc,
5098 "%s: logo %llx res %d sess %p ",
5099 __func__, wwn, res, sess);
5102 * cmd went upper layer, look for qlt_xmit_tm_rsp()
5103 * for LOGO_ACK & sess delete
5108 /* cmd did not go to upper layer. */
5110 qlt_schedule_sess_for_deletion(sess);
5113 /* else logo will be ack */
5119 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
5121 if (tgt->link_reinit_iocb_pending) {
5122 qlt_send_notify_ack(ha->base_qpair,
5123 &tgt->link_reinit_iocb, 0, 0, 0, 0, 0, 0);
5124 tgt->link_reinit_iocb_pending = 0;
5127 sess = qla2x00_find_fcport_by_wwpn(vha,
5128 iocb->u.isp24.port_name, 1);
5130 ql_dbg(ql_dbg_disc, vha, 0x20fd,
5131 "sess %p lid %d|%d DS %d LS %d\n",
5132 sess, sess->loop_id, loop_id,
5133 sess->disc_state, sess->fw_login_state);
5136 res = 1; /* send notify ack */
5140 case ELS_FLOGI: /* should never happen */
5142 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf061,
5143 "qla_target(%d): Unsupported ELS command %x "
5144 "received\n", vha->vp_idx, iocb->u.isp24.status_subcode);
5145 res = qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS_SESS);
5149 ql_dbg(ql_dbg_disc, vha, 0xf026,
5150 "qla_target(%d): Exit ELS opcode: 0x%02x res %d\n",
5151 vha->vp_idx, iocb->u.isp24.status_subcode, res);
5157 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
5159 static void qlt_handle_imm_notify(struct scsi_qla_host *vha,
5160 struct imm_ntfy_from_isp *iocb)
5162 struct qla_hw_data *ha = vha->hw;
5163 uint32_t add_flags = 0;
5164 int send_notify_ack = 1;
5167 lockdep_assert_held(&ha->hardware_lock);
5169 status = le16_to_cpu(iocb->u.isp2x.status);
5171 case IMM_NTFY_LIP_RESET:
5173 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf032,
5174 "qla_target(%d): LIP reset (loop %#x), subcode %x\n",
5175 vha->vp_idx, le16_to_cpu(iocb->u.isp24.nport_handle),
5176 iocb->u.isp24.status_subcode);
5178 if (qlt_reset(vha, iocb, QLA_TGT_ABORT_ALL) == 0)
5179 send_notify_ack = 0;
5183 case IMM_NTFY_LIP_LINK_REINIT:
5185 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
5187 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf033,
5188 "qla_target(%d): LINK REINIT (loop %#x, "
5189 "subcode %x)\n", vha->vp_idx,
5190 le16_to_cpu(iocb->u.isp24.nport_handle),
5191 iocb->u.isp24.status_subcode);
5192 if (tgt->link_reinit_iocb_pending) {
5193 qlt_send_notify_ack(ha->base_qpair,
5194 &tgt->link_reinit_iocb, 0, 0, 0, 0, 0, 0);
5196 memcpy(&tgt->link_reinit_iocb, iocb, sizeof(*iocb));
5197 tgt->link_reinit_iocb_pending = 1;
5199 * QLogic requires to wait after LINK REINIT for possible
5200 * PDISC or ADISC ELS commands
5202 send_notify_ack = 0;
5206 case IMM_NTFY_PORT_LOGOUT:
5207 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf034,
5208 "qla_target(%d): Port logout (loop "
5209 "%#x, subcode %x)\n", vha->vp_idx,
5210 le16_to_cpu(iocb->u.isp24.nport_handle),
5211 iocb->u.isp24.status_subcode);
5213 if (qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS_SESS) == 0)
5214 send_notify_ack = 0;
5215 /* The sessions will be cleared in the callback, if needed */
5218 case IMM_NTFY_GLBL_TPRLO:
5219 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf035,
5220 "qla_target(%d): Global TPRLO (%x)\n", vha->vp_idx, status);
5221 if (qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS) == 0)
5222 send_notify_ack = 0;
5223 /* The sessions will be cleared in the callback, if needed */
5226 case IMM_NTFY_PORT_CONFIG:
5227 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf036,
5228 "qla_target(%d): Port config changed (%x)\n", vha->vp_idx,
5230 if (qlt_reset(vha, iocb, QLA_TGT_ABORT_ALL) == 0)
5231 send_notify_ack = 0;
5232 /* The sessions will be cleared in the callback, if needed */
5235 case IMM_NTFY_GLBL_LOGO:
5236 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06a,
5237 "qla_target(%d): Link failure detected\n",
5239 /* I_T nexus loss */
5240 if (qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS) == 0)
5241 send_notify_ack = 0;
5244 case IMM_NTFY_IOCB_OVERFLOW:
5245 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06b,
5246 "qla_target(%d): Cannot provide requested "
5247 "capability (IOCB overflowed the immediate notify "
5248 "resource count)\n", vha->vp_idx);
5251 case IMM_NTFY_ABORT_TASK:
5252 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf037,
5253 "qla_target(%d): Abort Task (S %08x I %#x -> "
5254 "L %#x)\n", vha->vp_idx,
5255 le16_to_cpu(iocb->u.isp2x.seq_id),
5256 GET_TARGET_ID(ha, (struct atio_from_isp *)iocb),
5257 le16_to_cpu(iocb->u.isp2x.lun));
5258 if (qlt_abort_task(vha, iocb) == 0)
5259 send_notify_ack = 0;
5262 case IMM_NTFY_RESOURCE:
5263 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06c,
5264 "qla_target(%d): Out of resources, host %ld\n",
5265 vha->vp_idx, vha->host_no);
5268 case IMM_NTFY_MSG_RX:
5269 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf038,
5270 "qla_target(%d): Immediate notify task %x\n",
5271 vha->vp_idx, iocb->u.isp2x.task_flags);
5275 if (qlt_24xx_handle_els(vha, iocb) == 0)
5276 send_notify_ack = 0;
5279 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06d,
5280 "qla_target(%d): Received unknown immediate "
5281 "notify status %x\n", vha->vp_idx, status);
5285 if (send_notify_ack)
5286 qlt_send_notify_ack(ha->base_qpair, iocb, add_flags, 0, 0, 0,
5291 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
5292 * This function sends busy to ISP 2xxx or 24xx.
5294 static int __qlt_send_busy(struct qla_qpair *qpair,
5295 struct atio_from_isp *atio, uint16_t status)
5297 struct scsi_qla_host *vha = qpair->vha;
5298 struct ctio7_to_24xx *ctio24;
5299 struct qla_hw_data *ha = vha->hw;
5301 struct fc_port *sess = NULL;
5302 unsigned long flags;
5306 id.b.al_pa = atio->u.isp24.fcp_hdr.s_id[2];
5307 id.b.area = atio->u.isp24.fcp_hdr.s_id[1];
5308 id.b.domain = atio->u.isp24.fcp_hdr.s_id[0];
5311 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
5312 sess = qla2x00_find_fcport_by_nportid(vha, &id, 1);
5313 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
5315 qlt_send_term_exchange(qpair, NULL, atio, 1, 0);
5318 /* Sending marker isn't necessary, since we called from ISR */
5320 pkt = (request_t *)__qla2x00_alloc_iocbs(qpair, NULL);
5322 ql_dbg(ql_dbg_io, vha, 0x3063,
5323 "qla_target(%d): %s failed: unable to allocate "
5324 "request packet", vha->vp_idx, __func__);
5328 qpair->tgt_counters.num_q_full_sent++;
5329 pkt->entry_count = 1;
5330 pkt->handle = QLA_TGT_SKIP_HANDLE | CTIO_COMPLETION_HANDLE_MARK;
5332 ctio24 = (struct ctio7_to_24xx *)pkt;
5333 ctio24->entry_type = CTIO_TYPE7;
5334 ctio24->nport_handle = sess->loop_id;
5335 ctio24->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
5336 ctio24->vp_index = vha->vp_idx;
5337 ctio24->initiator_id[0] = atio->u.isp24.fcp_hdr.s_id[2];
5338 ctio24->initiator_id[1] = atio->u.isp24.fcp_hdr.s_id[1];
5339 ctio24->initiator_id[2] = atio->u.isp24.fcp_hdr.s_id[0];
5340 ctio24->exchange_addr = atio->u.isp24.exchange_addr;
5341 temp = (atio->u.isp24.attr << 9) |
5342 CTIO7_FLAGS_STATUS_MODE_1 | CTIO7_FLAGS_SEND_STATUS |
5343 CTIO7_FLAGS_DONT_RET_CTIO;
5344 ctio24->u.status1.flags = cpu_to_le16(temp);
5346 * CTIO from fw w/o se_cmd doesn't provide enough info to retry it,
5347 * if the explicit conformation is used.
5349 ctio24->u.status1.ox_id = swab16(atio->u.isp24.fcp_hdr.ox_id);
5350 ctio24->u.status1.scsi_status = cpu_to_le16(status);
5352 ctio24->u.status1.residual = get_datalen_for_atio(atio);
5354 if (ctio24->u.status1.residual != 0)
5355 ctio24->u.status1.scsi_status |= SS_RESIDUAL_UNDER;
5357 /* Memory Barrier */
5359 if (qpair->reqq_start_iocbs)
5360 qpair->reqq_start_iocbs(qpair);
5362 qla2x00_start_iocbs(vha, qpair->req);
5367 * This routine is used to allocate a command for either a QFull condition
5368 * (ie reply SAM_STAT_BUSY) or to terminate an exchange that did not go
5372 qlt_alloc_qfull_cmd(struct scsi_qla_host *vha,
5373 struct atio_from_isp *atio, uint16_t status, int qfull)
5375 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
5376 struct qla_hw_data *ha = vha->hw;
5377 struct fc_port *sess;
5378 struct se_session *se_sess;
5379 struct qla_tgt_cmd *cmd;
5381 unsigned long flags;
5383 if (unlikely(tgt->tgt_stop)) {
5384 ql_dbg(ql_dbg_io, vha, 0x300a,
5385 "New command while device %p is shutting down\n", tgt);
5389 if ((vha->hw->tgt.num_qfull_cmds_alloc + 1) > MAX_QFULL_CMDS_ALLOC) {
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 ql_dbg(ql_dbg_io, vha, 0x3068,
5397 "qla_target(%d): %s: QFull CMD dropped[%d]\n",
5398 vha->vp_idx, __func__,
5399 vha->hw->tgt.num_qfull_cmds_dropped);
5401 qlt_chk_exch_leak_thresh_hold(vha);
5405 sess = ha->tgt.tgt_ops->find_sess_by_s_id
5406 (vha, atio->u.isp24.fcp_hdr.s_id);
5410 se_sess = sess->se_sess;
5412 tag = sbitmap_queue_get(&se_sess->sess_tag_pool, &cpu);
5414 ql_dbg(ql_dbg_io, vha, 0x3009,
5415 "qla_target(%d): %s: Allocation of cmd failed\n",
5416 vha->vp_idx, __func__);
5418 vha->hw->tgt.num_qfull_cmds_dropped++;
5419 if (vha->hw->tgt.num_qfull_cmds_dropped >
5420 vha->qla_stats.stat_max_qfull_cmds_dropped)
5421 vha->qla_stats.stat_max_qfull_cmds_dropped =
5422 vha->hw->tgt.num_qfull_cmds_dropped;
5424 qlt_chk_exch_leak_thresh_hold(vha);
5428 cmd = &((struct qla_tgt_cmd *)se_sess->sess_cmd_map)[tag];
5429 memset(cmd, 0, sizeof(struct qla_tgt_cmd));
5431 qlt_incr_num_pend_cmds(vha);
5432 INIT_LIST_HEAD(&cmd->cmd_list);
5433 memcpy(&cmd->atio, atio, sizeof(*atio));
5435 cmd->tgt = vha->vha_tgt.qla_tgt;
5437 cmd->reset_count = ha->base_qpair->chip_reset;
5439 cmd->qpair = ha->base_qpair;
5440 cmd->se_cmd.map_cpu = cpu;
5444 /* NOTE: borrowing the state field to carry the status */
5445 cmd->state = status;
5447 cmd->term_exchg = 1;
5449 spin_lock_irqsave(&vha->hw->tgt.q_full_lock, flags);
5450 list_add_tail(&cmd->cmd_list, &vha->hw->tgt.q_full_list);
5452 vha->hw->tgt.num_qfull_cmds_alloc++;
5453 if (vha->hw->tgt.num_qfull_cmds_alloc >
5454 vha->qla_stats.stat_max_qfull_cmds_alloc)
5455 vha->qla_stats.stat_max_qfull_cmds_alloc =
5456 vha->hw->tgt.num_qfull_cmds_alloc;
5457 spin_unlock_irqrestore(&vha->hw->tgt.q_full_lock, flags);
5461 qlt_free_qfull_cmds(struct qla_qpair *qpair)
5463 struct scsi_qla_host *vha = qpair->vha;
5464 struct qla_hw_data *ha = vha->hw;
5465 unsigned long flags;
5466 struct qla_tgt_cmd *cmd, *tcmd;
5467 struct list_head free_list, q_full_list;
5470 if (list_empty(&ha->tgt.q_full_list))
5473 INIT_LIST_HEAD(&free_list);
5474 INIT_LIST_HEAD(&q_full_list);
5476 spin_lock_irqsave(&vha->hw->tgt.q_full_lock, flags);
5477 if (list_empty(&ha->tgt.q_full_list)) {
5478 spin_unlock_irqrestore(&vha->hw->tgt.q_full_lock, flags);
5482 list_splice_init(&vha->hw->tgt.q_full_list, &q_full_list);
5483 spin_unlock_irqrestore(&vha->hw->tgt.q_full_lock, flags);
5485 spin_lock_irqsave(qpair->qp_lock_ptr, flags);
5486 list_for_each_entry_safe(cmd, tcmd, &q_full_list, cmd_list) {
5488 /* cmd->state is a borrowed field to hold status */
5489 rc = __qlt_send_busy(qpair, &cmd->atio, cmd->state);
5490 else if (cmd->term_exchg)
5491 rc = __qlt_send_term_exchange(qpair, NULL, &cmd->atio);
5497 ql_dbg(ql_dbg_io, vha, 0x3006,
5498 "%s: busy sent for ox_id[%04x]\n", __func__,
5499 be16_to_cpu(cmd->atio.u.isp24.fcp_hdr.ox_id));
5500 else if (cmd->term_exchg)
5501 ql_dbg(ql_dbg_io, vha, 0x3007,
5502 "%s: Term exchg sent for ox_id[%04x]\n", __func__,
5503 be16_to_cpu(cmd->atio.u.isp24.fcp_hdr.ox_id));
5505 ql_dbg(ql_dbg_io, vha, 0x3008,
5506 "%s: Unexpected cmd in QFull list %p\n", __func__,
5509 list_del(&cmd->cmd_list);
5510 list_add_tail(&cmd->cmd_list, &free_list);
5512 /* piggy back on hardware_lock for protection */
5513 vha->hw->tgt.num_qfull_cmds_alloc--;
5515 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
5519 list_for_each_entry_safe(cmd, tcmd, &free_list, cmd_list) {
5520 list_del(&cmd->cmd_list);
5521 /* This cmd was never sent to TCM. There is no need
5522 * to schedule free or call free_cmd
5527 if (!list_empty(&q_full_list)) {
5528 spin_lock_irqsave(&vha->hw->tgt.q_full_lock, flags);
5529 list_splice(&q_full_list, &vha->hw->tgt.q_full_list);
5530 spin_unlock_irqrestore(&vha->hw->tgt.q_full_lock, flags);
5537 qlt_send_busy(struct qla_qpair *qpair, struct atio_from_isp *atio,
5541 struct scsi_qla_host *vha = qpair->vha;
5543 rc = __qlt_send_busy(qpair, atio, status);
5545 qlt_alloc_qfull_cmd(vha, atio, status, 1);
5549 qlt_chk_qfull_thresh_hold(struct scsi_qla_host *vha, struct qla_qpair *qpair,
5550 struct atio_from_isp *atio, uint8_t ha_locked)
5552 struct qla_hw_data *ha = vha->hw;
5553 unsigned long flags;
5555 if (ha->tgt.num_pend_cmds < Q_FULL_THRESH_HOLD(ha))
5559 spin_lock_irqsave(&ha->hardware_lock, flags);
5560 qlt_send_busy(qpair, atio, qla_sam_status);
5562 spin_unlock_irqrestore(&ha->hardware_lock, flags);
5567 /* ha->hardware_lock supposed to be held on entry */
5568 /* called via callback from qla2xxx */
5569 static void qlt_24xx_atio_pkt(struct scsi_qla_host *vha,
5570 struct atio_from_isp *atio, uint8_t ha_locked)
5572 struct qla_hw_data *ha = vha->hw;
5573 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
5575 unsigned long flags = 0;
5577 if (unlikely(tgt == NULL)) {
5578 ql_dbg(ql_dbg_tgt, vha, 0x3064,
5579 "ATIO pkt, but no tgt (ha %p)", ha);
5583 * In tgt_stop mode we also should allow all requests to pass.
5584 * Otherwise, some commands can stuck.
5587 tgt->atio_irq_cmd_count++;
5589 switch (atio->u.raw.entry_type) {
5591 if (unlikely(atio->u.isp24.exchange_addr ==
5592 ATIO_EXCHANGE_ADDRESS_UNKNOWN)) {
5593 ql_dbg(ql_dbg_io, vha, 0x3065,
5594 "qla_target(%d): ATIO_TYPE7 "
5595 "received with UNKNOWN exchange address, "
5596 "sending QUEUE_FULL\n", vha->vp_idx);
5598 spin_lock_irqsave(&ha->hardware_lock, flags);
5599 qlt_send_busy(ha->base_qpair, atio, qla_sam_status);
5601 spin_unlock_irqrestore(&ha->hardware_lock,
5606 if (likely(atio->u.isp24.fcp_cmnd.task_mgmt_flags == 0)) {
5607 rc = qlt_chk_qfull_thresh_hold(vha, ha->base_qpair,
5610 tgt->atio_irq_cmd_count--;
5613 rc = qlt_handle_cmd_for_atio(vha, atio);
5615 rc = qlt_handle_task_mgmt(vha, atio);
5617 if (unlikely(rc != 0)) {
5619 spin_lock_irqsave(&ha->hardware_lock, flags);
5622 ql_dbg(ql_dbg_tgt, vha, 0xe05f,
5623 "qla_target: Unable to send command to target\n");
5626 ql_dbg(ql_dbg_tgt, vha, 0xe05f,
5627 "qla_target: Unable to send command to target, sending TERM EXCHANGE for rsp\n");
5628 qlt_send_term_exchange(ha->base_qpair, NULL,
5632 ql_dbg(ql_dbg_tgt, vha, 0xe060,
5633 "qla_target(%d): Unable to send command to target, sending BUSY status\n",
5635 qlt_send_busy(ha->base_qpair, atio,
5639 ql_dbg(ql_dbg_tgt, vha, 0xe060,
5640 "qla_target(%d): Unable to send command to target, sending BUSY status\n",
5642 qlt_send_busy(ha->base_qpair, atio,
5647 spin_unlock_irqrestore(&ha->hardware_lock,
5652 case IMMED_NOTIFY_TYPE:
5654 if (unlikely(atio->u.isp2x.entry_status != 0)) {
5655 ql_dbg(ql_dbg_tgt, vha, 0xe05b,
5656 "qla_target(%d): Received ATIO packet %x "
5657 "with error status %x\n", vha->vp_idx,
5658 atio->u.raw.entry_type,
5659 atio->u.isp2x.entry_status);
5662 ql_dbg(ql_dbg_tgt, vha, 0xe02e, "%s", "IMMED_NOTIFY ATIO");
5665 spin_lock_irqsave(&ha->hardware_lock, flags);
5666 qlt_handle_imm_notify(vha, (struct imm_ntfy_from_isp *)atio);
5668 spin_unlock_irqrestore(&ha->hardware_lock, flags);
5673 ql_dbg(ql_dbg_tgt, vha, 0xe05c,
5674 "qla_target(%d): Received unknown ATIO atio "
5675 "type %x\n", vha->vp_idx, atio->u.raw.entry_type);
5679 tgt->atio_irq_cmd_count--;
5683 * qpair lock is assume to be held
5684 * rc = 0 : send terminate & abts respond
5685 * rc != 0: do not send term & abts respond
5687 static int qlt_chk_unresolv_exchg(struct scsi_qla_host *vha,
5688 struct qla_qpair *qpair, struct abts_resp_from_24xx_fw *entry)
5690 struct qla_hw_data *ha = vha->hw;
5694 * Detect unresolved exchange. If the same ABTS is unable
5695 * to terminate an existing command and the same ABTS loops
5696 * between FW & Driver, then force FW dump. Under 1 jiff,
5697 * we should see multiple loops.
5699 if (qpair->retry_term_exchg_addr == entry->exchange_addr_to_abort &&
5700 qpair->retry_term_jiff == jiffies) {
5701 /* found existing exchange */
5702 qpair->retry_term_cnt++;
5703 if (qpair->retry_term_cnt >= 5) {
5705 qpair->retry_term_cnt = 0;
5706 ql_log(ql_log_warn, vha, 0xffff,
5707 "Unable to send ABTS Respond. Dumping firmware.\n");
5708 ql_dump_buffer(ql_dbg_tgt_mgt + ql_dbg_buffer,
5709 vha, 0xffff, (uint8_t *)entry, sizeof(*entry));
5711 if (qpair == ha->base_qpair)
5712 ha->isp_ops->fw_dump(vha, 1);
5714 ha->isp_ops->fw_dump(vha, 0);
5716 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
5717 qla2xxx_wake_dpc(vha);
5719 } else if (qpair->retry_term_jiff != jiffies) {
5720 qpair->retry_term_exchg_addr = entry->exchange_addr_to_abort;
5721 qpair->retry_term_cnt = 0;
5722 qpair->retry_term_jiff = jiffies;
5729 static void qlt_handle_abts_completion(struct scsi_qla_host *vha,
5730 struct rsp_que *rsp, response_t *pkt)
5732 struct abts_resp_from_24xx_fw *entry =
5733 (struct abts_resp_from_24xx_fw *)pkt;
5734 u32 h = pkt->handle & ~QLA_TGT_HANDLE_MASK;
5735 struct qla_tgt_mgmt_cmd *mcmd;
5736 struct qla_hw_data *ha = vha->hw;
5738 mcmd = qlt_ctio_to_cmd(vha, rsp, pkt->handle, pkt);
5739 if (mcmd == NULL && h != QLA_TGT_SKIP_HANDLE) {
5740 ql_dbg(ql_dbg_async, vha, 0xe064,
5741 "qla_target(%d): ABTS Comp without mcmd\n",
5748 vha->vha_tgt.qla_tgt->abts_resp_expected--;
5750 ql_dbg(ql_dbg_tgt, vha, 0xe038,
5751 "ABTS_RESP_24XX: compl_status %x\n",
5752 entry->compl_status);
5754 if (le16_to_cpu(entry->compl_status) != ABTS_RESP_COMPL_SUCCESS) {
5755 if ((entry->error_subcode1 == 0x1E) &&
5756 (entry->error_subcode2 == 0)) {
5757 if (qlt_chk_unresolv_exchg(vha, rsp->qpair, entry)) {
5758 ha->tgt.tgt_ops->free_mcmd(mcmd);
5761 qlt_24xx_retry_term_exchange(vha, rsp->qpair,
5764 ql_dbg(ql_dbg_tgt, vha, 0xe063,
5765 "qla_target(%d): ABTS_RESP_24XX failed %x (subcode %x:%x)",
5766 vha->vp_idx, entry->compl_status,
5767 entry->error_subcode1,
5768 entry->error_subcode2);
5769 ha->tgt.tgt_ops->free_mcmd(mcmd);
5772 ha->tgt.tgt_ops->free_mcmd(mcmd);
5776 /* ha->hardware_lock supposed to be held on entry */
5777 /* called via callback from qla2xxx */
5778 static void qlt_response_pkt(struct scsi_qla_host *vha,
5779 struct rsp_que *rsp, response_t *pkt)
5781 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
5783 if (unlikely(tgt == NULL)) {
5784 ql_dbg(ql_dbg_tgt, vha, 0xe05d,
5785 "qla_target(%d): Response pkt %x received, but no tgt (ha %p)\n",
5786 vha->vp_idx, pkt->entry_type, vha->hw);
5791 * In tgt_stop mode we also should allow all requests to pass.
5792 * Otherwise, some commands can stuck.
5795 switch (pkt->entry_type) {
5799 struct ctio7_from_24xx *entry = (struct ctio7_from_24xx *)pkt;
5801 qlt_do_ctio_completion(vha, rsp, entry->handle,
5802 le16_to_cpu(entry->status)|(pkt->entry_status << 16),
5807 case ACCEPT_TGT_IO_TYPE:
5809 struct atio_from_isp *atio = (struct atio_from_isp *)pkt;
5812 if (atio->u.isp2x.status !=
5813 cpu_to_le16(ATIO_CDB_VALID)) {
5814 ql_dbg(ql_dbg_tgt, vha, 0xe05e,
5815 "qla_target(%d): ATIO with error "
5816 "status %x received\n", vha->vp_idx,
5817 le16_to_cpu(atio->u.isp2x.status));
5821 rc = qlt_chk_qfull_thresh_hold(vha, rsp->qpair, atio, 1);
5825 rc = qlt_handle_cmd_for_atio(vha, atio);
5826 if (unlikely(rc != 0)) {
5829 ql_dbg(ql_dbg_tgt, vha, 0xe05f,
5830 "qla_target: Unable to send command to target\n");
5833 ql_dbg(ql_dbg_tgt, vha, 0xe05f,
5834 "qla_target: Unable to send command to target, sending TERM EXCHANGE for rsp\n");
5835 qlt_send_term_exchange(rsp->qpair, NULL,
5839 ql_dbg(ql_dbg_tgt, vha, 0xe060,
5840 "qla_target(%d): Unable to send command to target, sending BUSY status\n",
5842 qlt_send_busy(rsp->qpair, atio,
5846 ql_dbg(ql_dbg_tgt, vha, 0xe060,
5847 "qla_target(%d): Unable to send command to target, sending BUSY status\n",
5849 qlt_send_busy(rsp->qpair, atio,
5857 case CONTINUE_TGT_IO_TYPE:
5859 struct ctio_to_2xxx *entry = (struct ctio_to_2xxx *)pkt;
5861 qlt_do_ctio_completion(vha, rsp, entry->handle,
5862 le16_to_cpu(entry->status)|(pkt->entry_status << 16),
5869 struct ctio_to_2xxx *entry = (struct ctio_to_2xxx *)pkt;
5871 qlt_do_ctio_completion(vha, rsp, entry->handle,
5872 le16_to_cpu(entry->status)|(pkt->entry_status << 16),
5877 case IMMED_NOTIFY_TYPE:
5878 ql_dbg(ql_dbg_tgt, vha, 0xe035, "%s", "IMMED_NOTIFY\n");
5879 qlt_handle_imm_notify(vha, (struct imm_ntfy_from_isp *)pkt);
5882 case NOTIFY_ACK_TYPE:
5883 if (tgt->notify_ack_expected > 0) {
5884 struct nack_to_isp *entry = (struct nack_to_isp *)pkt;
5886 ql_dbg(ql_dbg_tgt, vha, 0xe036,
5887 "NOTIFY_ACK seq %08x status %x\n",
5888 le16_to_cpu(entry->u.isp2x.seq_id),
5889 le16_to_cpu(entry->u.isp2x.status));
5890 tgt->notify_ack_expected--;
5891 if (entry->u.isp2x.status !=
5892 cpu_to_le16(NOTIFY_ACK_SUCCESS)) {
5893 ql_dbg(ql_dbg_tgt, vha, 0xe061,
5894 "qla_target(%d): NOTIFY_ACK "
5895 "failed %x\n", vha->vp_idx,
5896 le16_to_cpu(entry->u.isp2x.status));
5899 ql_dbg(ql_dbg_tgt, vha, 0xe062,
5900 "qla_target(%d): Unexpected NOTIFY_ACK received\n",
5905 case ABTS_RECV_24XX:
5906 ql_dbg(ql_dbg_tgt, vha, 0xe037,
5907 "ABTS_RECV_24XX: instance %d\n", vha->vp_idx);
5908 qlt_24xx_handle_abts(vha, (struct abts_recv_from_24xx *)pkt);
5911 case ABTS_RESP_24XX:
5912 if (tgt->abts_resp_expected > 0) {
5913 qlt_handle_abts_completion(vha, rsp, pkt);
5915 ql_dbg(ql_dbg_tgt, vha, 0xe064,
5916 "qla_target(%d): Unexpected ABTS_RESP_24XX "
5917 "received\n", vha->vp_idx);
5922 ql_dbg(ql_dbg_tgt, vha, 0xe065,
5923 "qla_target(%d): Received unknown response pkt "
5924 "type %x\n", vha->vp_idx, pkt->entry_type);
5931 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
5933 void qlt_async_event(uint16_t code, struct scsi_qla_host *vha,
5936 struct qla_hw_data *ha = vha->hw;
5937 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
5940 if (!tgt || tgt->tgt_stop || tgt->tgt_stopped)
5943 if (((code == MBA_POINT_TO_POINT) || (code == MBA_CHG_IN_CONNECTION)) &&
5947 * In tgt_stop mode we also should allow all requests to pass.
5948 * Otherwise, some commands can stuck.
5953 case MBA_RESET: /* Reset */
5954 case MBA_SYSTEM_ERR: /* System Error */
5955 case MBA_REQ_TRANSFER_ERR: /* Request Transfer Error */
5956 case MBA_RSP_TRANSFER_ERR: /* Response Transfer Error */
5957 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03a,
5958 "qla_target(%d): System error async event %#x "
5959 "occurred", vha->vp_idx, code);
5961 case MBA_WAKEUP_THRES: /* Request Queue Wake-up. */
5962 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
5967 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03b,
5968 "qla_target(%d): Async LOOP_UP occurred "
5969 "(m[0]=%x, m[1]=%x, m[2]=%x, m[3]=%x)", vha->vp_idx,
5970 le16_to_cpu(mailbox[0]), le16_to_cpu(mailbox[1]),
5971 le16_to_cpu(mailbox[2]), le16_to_cpu(mailbox[3]));
5972 if (tgt->link_reinit_iocb_pending) {
5973 qlt_send_notify_ack(ha->base_qpair,
5974 (void *)&tgt->link_reinit_iocb,
5976 tgt->link_reinit_iocb_pending = 0;
5981 case MBA_LIP_OCCURRED:
5984 case MBA_RSCN_UPDATE:
5985 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03c,
5986 "qla_target(%d): Async event %#x occurred "
5987 "(m[0]=%x, m[1]=%x, m[2]=%x, m[3]=%x)", vha->vp_idx, code,
5988 le16_to_cpu(mailbox[0]), le16_to_cpu(mailbox[1]),
5989 le16_to_cpu(mailbox[2]), le16_to_cpu(mailbox[3]));
5992 case MBA_REJECTED_FCP_CMD:
5993 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf017,
5994 "qla_target(%d): Async event LS_REJECT occurred (m[0]=%x, m[1]=%x, m[2]=%x, m[3]=%x)",
5996 le16_to_cpu(mailbox[0]), le16_to_cpu(mailbox[1]),
5997 le16_to_cpu(mailbox[2]), le16_to_cpu(mailbox[3]));
5999 if (le16_to_cpu(mailbox[3]) == 1) {
6000 /* exchange starvation. */
6001 vha->hw->exch_starvation++;
6002 if (vha->hw->exch_starvation > 5) {
6003 ql_log(ql_log_warn, vha, 0xd03a,
6004 "Exchange starvation-. Resetting RISC\n");
6006 vha->hw->exch_starvation = 0;
6007 if (IS_P3P_TYPE(vha->hw))
6008 set_bit(FCOE_CTX_RESET_NEEDED,
6011 set_bit(ISP_ABORT_NEEDED,
6013 qla2xxx_wake_dpc(vha);
6018 case MBA_PORT_UPDATE:
6019 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03d,
6020 "qla_target(%d): Port update async event %#x "
6021 "occurred: updating the ports database (m[0]=%x, m[1]=%x, "
6022 "m[2]=%x, m[3]=%x)", vha->vp_idx, code,
6023 le16_to_cpu(mailbox[0]), le16_to_cpu(mailbox[1]),
6024 le16_to_cpu(mailbox[2]), le16_to_cpu(mailbox[3]));
6026 login_code = le16_to_cpu(mailbox[2]);
6027 if (login_code == 0x4) {
6028 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03e,
6029 "Async MB 2: Got PLOGI Complete\n");
6030 vha->hw->exch_starvation = 0;
6031 } else if (login_code == 0x7)
6032 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03f,
6033 "Async MB 2: Port Logged Out\n");
6041 static fc_port_t *qlt_get_port_database(struct scsi_qla_host *vha,
6044 fc_port_t *fcport, *tfcp, *del;
6046 unsigned long flags;
6049 fcport = qla2x00_alloc_fcport(vha, GFP_KERNEL);
6051 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06f,
6052 "qla_target(%d): Allocation of tmp FC port failed",
6057 fcport->loop_id = loop_id;
6059 rc = qla24xx_gpdb_wait(vha, fcport, 0);
6060 if (rc != QLA_SUCCESS) {
6061 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf070,
6062 "qla_target(%d): Failed to retrieve fcport "
6063 "information -- get_port_database() returned %x "
6064 "(loop_id=0x%04x)", vha->vp_idx, rc, loop_id);
6070 spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
6071 tfcp = qla2x00_find_fcport_by_wwpn(vha, fcport->port_name, 1);
6074 tfcp->d_id = fcport->d_id;
6075 tfcp->port_type = fcport->port_type;
6076 tfcp->supported_classes = fcport->supported_classes;
6077 tfcp->flags |= fcport->flags;
6078 tfcp->scan_state = QLA_FCPORT_FOUND;
6083 if (vha->hw->current_topology == ISP_CFG_F)
6084 fcport->flags |= FCF_FABRIC_DEVICE;
6086 list_add_tail(&fcport->list, &vha->vp_fcports);
6087 if (!IS_SW_RESV_ADDR(fcport->d_id))
6088 vha->fcport_count++;
6089 fcport->login_gen++;
6090 fcport->disc_state = DSC_LOGIN_COMPLETE;
6091 fcport->login_succ = 1;
6095 fcport->deleted = 0;
6096 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
6098 switch (vha->host->active_mode) {
6099 case MODE_INITIATOR:
6102 if (!IS_IIDMA_CAPABLE(vha->hw) || !vha->hw->flags.gpsc_supported) {
6103 qla24xx_sched_upd_fcport(fcport);
6105 ql_dbg(ql_dbg_disc, vha, 0x20ff,
6106 "%s %d %8phC post gpsc fcp_cnt %d\n",
6107 __func__, __LINE__, fcport->port_name, vha->fcport_count);
6108 qla24xx_post_gpsc_work(vha, fcport);
6118 qla2x00_free_fcport(del);
6123 /* Must be called under tgt_mutex */
6124 static struct fc_port *qlt_make_local_sess(struct scsi_qla_host *vha,
6127 struct fc_port *sess = NULL;
6128 fc_port_t *fcport = NULL;
6129 int rc, global_resets;
6130 uint16_t loop_id = 0;
6132 if ((s_id[0] == 0xFF) && (s_id[1] == 0xFC)) {
6134 * This is Domain Controller, so it should be
6135 * OK to drop SCSI commands from it.
6137 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf042,
6138 "Unable to find initiator with S_ID %x:%x:%x",
6139 s_id[0], s_id[1], s_id[2]);
6143 mutex_lock(&vha->vha_tgt.tgt_mutex);
6147 atomic_read(&vha->vha_tgt.qla_tgt->tgt_global_resets_count);
6149 rc = qla24xx_get_loop_id(vha, s_id, &loop_id);
6151 mutex_unlock(&vha->vha_tgt.tgt_mutex);
6153 ql_log(ql_log_info, vha, 0xf071,
6154 "qla_target(%d): Unable to find "
6155 "initiator with S_ID %x:%x:%x",
6156 vha->vp_idx, s_id[0], s_id[1],
6159 if (rc == -ENOENT) {
6160 qlt_port_logo_t logo;
6162 sid_to_portid(s_id, &logo.id);
6164 qlt_send_first_logo(vha, &logo);
6170 fcport = qlt_get_port_database(vha, loop_id);
6172 mutex_unlock(&vha->vha_tgt.tgt_mutex);
6176 if (global_resets !=
6177 atomic_read(&vha->vha_tgt.qla_tgt->tgt_global_resets_count)) {
6178 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf043,
6179 "qla_target(%d): global reset during session discovery "
6180 "(counter was %d, new %d), retrying", vha->vp_idx,
6182 atomic_read(&vha->vha_tgt.
6183 qla_tgt->tgt_global_resets_count));
6187 sess = qlt_create_sess(vha, fcport, true);
6189 mutex_unlock(&vha->vha_tgt.tgt_mutex);
6194 static void qlt_abort_work(struct qla_tgt *tgt,
6195 struct qla_tgt_sess_work_param *prm)
6197 struct scsi_qla_host *vha = tgt->vha;
6198 struct qla_hw_data *ha = vha->hw;
6199 struct fc_port *sess = NULL;
6200 unsigned long flags = 0, flags2 = 0;
6205 spin_lock_irqsave(&ha->tgt.sess_lock, flags2);
6210 s_id[0] = prm->abts.fcp_hdr_le.s_id[2];
6211 s_id[1] = prm->abts.fcp_hdr_le.s_id[1];
6212 s_id[2] = prm->abts.fcp_hdr_le.s_id[0];
6214 sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha,
6215 (unsigned char *)&be_s_id);
6217 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags2);
6219 sess = qlt_make_local_sess(vha, s_id);
6220 /* sess has got an extra creation ref */
6222 spin_lock_irqsave(&ha->tgt.sess_lock, flags2);
6226 if (sess->deleted) {
6231 if (!kref_get_unless_zero(&sess->sess_kref)) {
6232 ql_dbg(ql_dbg_tgt_tmr, vha, 0xf01c,
6233 "%s: kref_get fail %8phC \n",
6234 __func__, sess->port_name);
6240 rc = __qlt_24xx_handle_abts(vha, &prm->abts, sess);
6241 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags2);
6243 ha->tgt.tgt_ops->put_sess(sess);
6250 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags2);
6253 ha->tgt.tgt_ops->put_sess(sess);
6256 spin_lock_irqsave(&ha->hardware_lock, flags);
6257 qlt_24xx_send_abts_resp(ha->base_qpair, &prm->abts,
6258 FCP_TMF_REJECTED, false);
6259 spin_unlock_irqrestore(&ha->hardware_lock, flags);
6262 static void qlt_tmr_work(struct qla_tgt *tgt,
6263 struct qla_tgt_sess_work_param *prm)
6265 struct atio_from_isp *a = &prm->tm_iocb2;
6266 struct scsi_qla_host *vha = tgt->vha;
6267 struct qla_hw_data *ha = vha->hw;
6268 struct fc_port *sess;
6269 unsigned long flags;
6270 uint8_t *s_id = NULL; /* to hide compiler warnings */
6276 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
6281 s_id = prm->tm_iocb2.u.isp24.fcp_hdr.s_id;
6282 sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha, s_id);
6284 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
6286 sess = qlt_make_local_sess(vha, s_id);
6287 /* sess has got an extra creation ref */
6289 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
6293 if (sess->deleted) {
6297 if (!kref_get_unless_zero(&sess->sess_kref)) {
6298 ql_dbg(ql_dbg_tgt_tmr, vha, 0xf020,
6299 "%s: kref_get fail %8phC\n",
6300 __func__, sess->port_name);
6306 fn = a->u.isp24.fcp_cmnd.task_mgmt_flags;
6308 scsilun_to_int((struct scsi_lun *)&a->u.isp24.fcp_cmnd.lun);
6310 rc = qlt_issue_task_mgmt(sess, unpacked_lun, fn, iocb, 0);
6311 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
6313 ha->tgt.tgt_ops->put_sess(sess);
6320 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
6322 qlt_send_term_exchange(ha->base_qpair, NULL, &prm->tm_iocb2, 1, 0);
6325 static void qlt_sess_work_fn(struct work_struct *work)
6327 struct qla_tgt *tgt = container_of(work, struct qla_tgt, sess_work);
6328 struct scsi_qla_host *vha = tgt->vha;
6329 unsigned long flags;
6331 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf000, "Sess work (tgt %p)", tgt);
6333 spin_lock_irqsave(&tgt->sess_work_lock, flags);
6334 while (!list_empty(&tgt->sess_works_list)) {
6335 struct qla_tgt_sess_work_param *prm = list_entry(
6336 tgt->sess_works_list.next, typeof(*prm),
6337 sess_works_list_entry);
6340 * This work can be scheduled on several CPUs at time, so we
6341 * must delete the entry to eliminate double processing
6343 list_del(&prm->sess_works_list_entry);
6345 spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
6347 switch (prm->type) {
6348 case QLA_TGT_SESS_WORK_ABORT:
6349 qlt_abort_work(tgt, prm);
6351 case QLA_TGT_SESS_WORK_TM:
6352 qlt_tmr_work(tgt, prm);
6359 spin_lock_irqsave(&tgt->sess_work_lock, flags);
6363 spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
6366 /* Must be called under tgt_host_action_mutex */
6367 int qlt_add_target(struct qla_hw_data *ha, struct scsi_qla_host *base_vha)
6369 struct qla_tgt *tgt;
6371 struct qla_qpair_hint *h;
6373 if (!QLA_TGT_MODE_ENABLED())
6376 if (!IS_TGT_MODE_CAPABLE(ha)) {
6377 ql_log(ql_log_warn, base_vha, 0xe070,
6378 "This adapter does not support target mode.\n");
6382 ql_dbg(ql_dbg_tgt, base_vha, 0xe03b,
6383 "Registering target for host %ld(%p).\n", base_vha->host_no, ha);
6385 BUG_ON(base_vha->vha_tgt.qla_tgt != NULL);
6387 tgt = kzalloc(sizeof(struct qla_tgt), GFP_KERNEL);
6389 ql_dbg(ql_dbg_tgt, base_vha, 0xe066,
6390 "Unable to allocate struct qla_tgt\n");
6394 tgt->qphints = kcalloc(ha->max_qpairs + 1,
6395 sizeof(struct qla_qpair_hint),
6397 if (!tgt->qphints) {
6399 ql_log(ql_log_warn, base_vha, 0x0197,
6400 "Unable to allocate qpair hints.\n");
6404 if (!(base_vha->host->hostt->supported_mode & MODE_TARGET))
6405 base_vha->host->hostt->supported_mode |= MODE_TARGET;
6407 rc = btree_init64(&tgt->lun_qpair_map);
6409 kfree(tgt->qphints);
6411 ql_log(ql_log_info, base_vha, 0x0198,
6412 "Unable to initialize lun_qpair_map btree\n");
6415 h = &tgt->qphints[0];
6416 h->qpair = ha->base_qpair;
6417 INIT_LIST_HEAD(&h->hint_elem);
6418 h->cpuid = ha->base_qpair->cpuid;
6419 list_add_tail(&h->hint_elem, &ha->base_qpair->hints_list);
6421 for (i = 0; i < ha->max_qpairs; i++) {
6422 unsigned long flags;
6424 struct qla_qpair *qpair = ha->queue_pair_map[i];
6426 h = &tgt->qphints[i + 1];
6427 INIT_LIST_HEAD(&h->hint_elem);
6430 spin_lock_irqsave(qpair->qp_lock_ptr, flags);
6431 list_add_tail(&h->hint_elem, &qpair->hints_list);
6432 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
6433 h->cpuid = qpair->cpuid;
6438 tgt->vha = base_vha;
6439 init_waitqueue_head(&tgt->waitQ);
6440 INIT_LIST_HEAD(&tgt->del_sess_list);
6441 spin_lock_init(&tgt->sess_work_lock);
6442 INIT_WORK(&tgt->sess_work, qlt_sess_work_fn);
6443 INIT_LIST_HEAD(&tgt->sess_works_list);
6444 atomic_set(&tgt->tgt_global_resets_count, 0);
6446 base_vha->vha_tgt.qla_tgt = tgt;
6448 ql_dbg(ql_dbg_tgt, base_vha, 0xe067,
6449 "qla_target(%d): using 64 Bit PCI addressing",
6452 tgt->sg_tablesize = QLA_TGT_MAX_SG_24XX(base_vha->req->length - 3);
6454 mutex_lock(&qla_tgt_mutex);
6455 list_add_tail(&tgt->tgt_list_entry, &qla_tgt_glist);
6456 mutex_unlock(&qla_tgt_mutex);
6458 if (ha->tgt.tgt_ops && ha->tgt.tgt_ops->add_target)
6459 ha->tgt.tgt_ops->add_target(base_vha);
6464 /* Must be called under tgt_host_action_mutex */
6465 int qlt_remove_target(struct qla_hw_data *ha, struct scsi_qla_host *vha)
6467 if (!vha->vha_tgt.qla_tgt)
6470 if (vha->fc_vport) {
6471 qlt_release(vha->vha_tgt.qla_tgt);
6475 /* free left over qfull cmds */
6476 qlt_init_term_exchange(vha);
6478 ql_dbg(ql_dbg_tgt, vha, 0xe03c, "Unregistering target for host %ld(%p)",
6480 qlt_release(vha->vha_tgt.qla_tgt);
6485 void qlt_remove_target_resources(struct qla_hw_data *ha)
6487 struct scsi_qla_host *node;
6490 btree_for_each_safe32(&ha->tgt.host_map, key, node)
6491 btree_remove32(&ha->tgt.host_map, key);
6493 btree_destroy32(&ha->tgt.host_map);
6496 static void qlt_lport_dump(struct scsi_qla_host *vha, u64 wwpn,
6499 pr_debug("qla2xxx HW vha->node_name: %8phC\n", vha->node_name);
6500 pr_debug("qla2xxx HW vha->port_name: %8phC\n", vha->port_name);
6501 put_unaligned_be64(wwpn, b);
6502 pr_debug("qla2xxx passed configfs WWPN: %8phC\n", b);
6506 * qla_tgt_lport_register - register lport with external module
6508 * @target_lport_ptr: pointer for tcm_qla2xxx specific lport data
6509 * @phys_wwpn: physical port WWPN
6510 * @npiv_wwpn: NPIV WWPN
6511 * @npiv_wwnn: NPIV WWNN
6512 * @callback: lport initialization callback for tcm_qla2xxx code
6514 int qlt_lport_register(void *target_lport_ptr, u64 phys_wwpn,
6515 u64 npiv_wwpn, u64 npiv_wwnn,
6516 int (*callback)(struct scsi_qla_host *, void *, u64, u64))
6518 struct qla_tgt *tgt;
6519 struct scsi_qla_host *vha;
6520 struct qla_hw_data *ha;
6521 struct Scsi_Host *host;
6522 unsigned long flags;
6526 mutex_lock(&qla_tgt_mutex);
6527 list_for_each_entry(tgt, &qla_tgt_glist, tgt_list_entry) {
6535 if (!(host->hostt->supported_mode & MODE_TARGET))
6538 if (vha->qlini_mode == QLA2XXX_INI_MODE_ENABLED)
6541 spin_lock_irqsave(&ha->hardware_lock, flags);
6542 if ((!npiv_wwpn || !npiv_wwnn) && host->active_mode & MODE_TARGET) {
6543 pr_debug("MODE_TARGET already active on qla2xxx(%d)\n",
6545 spin_unlock_irqrestore(&ha->hardware_lock, flags);
6548 if (tgt->tgt_stop) {
6549 pr_debug("MODE_TARGET in shutdown on qla2xxx(%d)\n",
6551 spin_unlock_irqrestore(&ha->hardware_lock, flags);
6554 spin_unlock_irqrestore(&ha->hardware_lock, flags);
6556 if (!scsi_host_get(host)) {
6557 ql_dbg(ql_dbg_tgt, vha, 0xe068,
6558 "Unable to scsi_host_get() for"
6559 " qla2xxx scsi_host\n");
6562 qlt_lport_dump(vha, phys_wwpn, b);
6564 if (memcmp(vha->port_name, b, WWN_SIZE)) {
6565 scsi_host_put(host);
6568 rc = (*callback)(vha, target_lport_ptr, npiv_wwpn, npiv_wwnn);
6570 scsi_host_put(host);
6572 mutex_unlock(&qla_tgt_mutex);
6575 mutex_unlock(&qla_tgt_mutex);
6579 EXPORT_SYMBOL(qlt_lport_register);
6582 * qla_tgt_lport_deregister - Degister lport
6584 * @vha: Registered scsi_qla_host pointer
6586 void qlt_lport_deregister(struct scsi_qla_host *vha)
6588 struct qla_hw_data *ha = vha->hw;
6589 struct Scsi_Host *sh = vha->host;
6591 * Clear the target_lport_ptr qla_target_template pointer in qla_hw_data
6593 vha->vha_tgt.target_lport_ptr = NULL;
6594 ha->tgt.tgt_ops = NULL;
6596 * Release the Scsi_Host reference for the underlying qla2xxx host
6600 EXPORT_SYMBOL(qlt_lport_deregister);
6602 /* Must be called under HW lock */
6603 void qlt_set_mode(struct scsi_qla_host *vha)
6605 switch (vha->qlini_mode) {
6606 case QLA2XXX_INI_MODE_DISABLED:
6607 case QLA2XXX_INI_MODE_EXCLUSIVE:
6608 vha->host->active_mode = MODE_TARGET;
6610 case QLA2XXX_INI_MODE_ENABLED:
6611 vha->host->active_mode = MODE_INITIATOR;
6613 case QLA2XXX_INI_MODE_DUAL:
6614 vha->host->active_mode = MODE_DUAL;
6621 /* Must be called under HW lock */
6622 static void qlt_clear_mode(struct scsi_qla_host *vha)
6624 switch (vha->qlini_mode) {
6625 case QLA2XXX_INI_MODE_DISABLED:
6626 vha->host->active_mode = MODE_UNKNOWN;
6628 case QLA2XXX_INI_MODE_EXCLUSIVE:
6629 vha->host->active_mode = MODE_INITIATOR;
6631 case QLA2XXX_INI_MODE_ENABLED:
6632 case QLA2XXX_INI_MODE_DUAL:
6633 vha->host->active_mode = MODE_INITIATOR;
6641 * qla_tgt_enable_vha - NO LOCK HELD
6643 * host_reset, bring up w/ Target Mode Enabled
6646 qlt_enable_vha(struct scsi_qla_host *vha)
6648 struct qla_hw_data *ha = vha->hw;
6649 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
6650 unsigned long flags;
6651 scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
6654 ql_dbg(ql_dbg_tgt, vha, 0xe069,
6655 "Unable to locate qla_tgt pointer from"
6656 " struct qla_hw_data\n");
6660 if (vha->qlini_mode == QLA2XXX_INI_MODE_ENABLED)
6663 if (ha->tgt.num_act_qpairs > ha->max_qpairs)
6664 ha->tgt.num_act_qpairs = ha->max_qpairs;
6665 spin_lock_irqsave(&ha->hardware_lock, flags);
6666 tgt->tgt_stopped = 0;
6668 spin_unlock_irqrestore(&ha->hardware_lock, flags);
6670 mutex_lock(&ha->optrom_mutex);
6671 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf021,
6674 qla24xx_disable_vp(vha);
6675 qla24xx_enable_vp(vha);
6677 set_bit(ISP_ABORT_NEEDED, &base_vha->dpc_flags);
6678 qla2xxx_wake_dpc(base_vha);
6679 qla2x00_wait_for_hba_online(base_vha);
6681 mutex_unlock(&ha->optrom_mutex);
6683 EXPORT_SYMBOL(qlt_enable_vha);
6686 * qla_tgt_disable_vha - NO LOCK HELD
6688 * Disable Target Mode and reset the adapter
6690 static void qlt_disable_vha(struct scsi_qla_host *vha)
6692 struct qla_hw_data *ha = vha->hw;
6693 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
6694 unsigned long flags;
6697 ql_dbg(ql_dbg_tgt, vha, 0xe06a,
6698 "Unable to locate qla_tgt pointer from"
6699 " struct qla_hw_data\n");
6704 spin_lock_irqsave(&ha->hardware_lock, flags);
6705 qlt_clear_mode(vha);
6706 spin_unlock_irqrestore(&ha->hardware_lock, flags);
6708 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
6709 qla2xxx_wake_dpc(vha);
6710 qla2x00_wait_for_hba_online(vha);
6714 * Called from qla_init.c:qla24xx_vport_create() contex to setup
6715 * the target mode specific struct scsi_qla_host and struct qla_hw_data
6719 qlt_vport_create(struct scsi_qla_host *vha, struct qla_hw_data *ha)
6721 vha->vha_tgt.qla_tgt = NULL;
6723 mutex_init(&vha->vha_tgt.tgt_mutex);
6724 mutex_init(&vha->vha_tgt.tgt_host_action_mutex);
6726 qlt_clear_mode(vha);
6729 * NOTE: Currently the value is kept the same for <24xx and
6730 * >=24xx ISPs. If it is necessary to change it,
6731 * the check should be added for specific ISPs,
6732 * assigning the value appropriately.
6734 ha->tgt.atio_q_length = ATIO_ENTRY_CNT_24XX;
6736 qlt_add_target(ha, vha);
6740 qlt_rff_id(struct scsi_qla_host *vha)
6744 * FC-4 Feature bit 0 indicates target functionality to the name server.
6746 if (qla_tgt_mode_enabled(vha)) {
6747 fc4_feature = BIT_0;
6748 } else if (qla_ini_mode_enabled(vha)) {
6749 fc4_feature = BIT_1;
6750 } else if (qla_dual_mode_enabled(vha))
6751 fc4_feature = BIT_0 | BIT_1;
6757 * qlt_init_atio_q_entries() - Initializes ATIO queue entries.
6760 * Beginning of ATIO ring has initialization control block already built
6761 * by nvram config routine.
6763 * Returns 0 on success.
6766 qlt_init_atio_q_entries(struct scsi_qla_host *vha)
6768 struct qla_hw_data *ha = vha->hw;
6770 struct atio_from_isp *pkt = (struct atio_from_isp *)ha->tgt.atio_ring;
6772 if (qla_ini_mode_enabled(vha))
6775 for (cnt = 0; cnt < ha->tgt.atio_q_length; cnt++) {
6776 pkt->u.raw.signature = ATIO_PROCESSED;
6783 * qlt_24xx_process_atio_queue() - Process ATIO queue entries.
6784 * @ha: SCSI driver HA context
6787 qlt_24xx_process_atio_queue(struct scsi_qla_host *vha, uint8_t ha_locked)
6789 struct qla_hw_data *ha = vha->hw;
6790 struct atio_from_isp *pkt;
6793 if (!ha->flags.fw_started)
6796 while ((ha->tgt.atio_ring_ptr->signature != ATIO_PROCESSED) ||
6797 fcpcmd_is_corrupted(ha->tgt.atio_ring_ptr)) {
6798 pkt = (struct atio_from_isp *)ha->tgt.atio_ring_ptr;
6799 cnt = pkt->u.raw.entry_count;
6801 if (unlikely(fcpcmd_is_corrupted(ha->tgt.atio_ring_ptr))) {
6803 * This packet is corrupted. The header + payload
6804 * can not be trusted. There is no point in passing
6807 ql_log(ql_log_warn, vha, 0xd03c,
6808 "corrupted fcp frame SID[%3phN] OXID[%04x] EXCG[%x] %64phN\n",
6809 pkt->u.isp24.fcp_hdr.s_id,
6810 be16_to_cpu(pkt->u.isp24.fcp_hdr.ox_id),
6811 le32_to_cpu(pkt->u.isp24.exchange_addr), pkt);
6813 adjust_corrupted_atio(pkt);
6814 qlt_send_term_exchange(ha->base_qpair, NULL, pkt,
6817 qlt_24xx_atio_pkt_all_vps(vha,
6818 (struct atio_from_isp *)pkt, ha_locked);
6821 for (i = 0; i < cnt; i++) {
6822 ha->tgt.atio_ring_index++;
6823 if (ha->tgt.atio_ring_index == ha->tgt.atio_q_length) {
6824 ha->tgt.atio_ring_index = 0;
6825 ha->tgt.atio_ring_ptr = ha->tgt.atio_ring;
6827 ha->tgt.atio_ring_ptr++;
6829 pkt->u.raw.signature = ATIO_PROCESSED;
6830 pkt = (struct atio_from_isp *)ha->tgt.atio_ring_ptr;
6835 /* Adjust ring index */
6836 WRT_REG_DWORD(ISP_ATIO_Q_OUT(vha), ha->tgt.atio_ring_index);
6840 qlt_24xx_config_rings(struct scsi_qla_host *vha)
6842 struct qla_hw_data *ha = vha->hw;
6843 struct qla_msix_entry *msix = &ha->msix_entries[2];
6844 struct init_cb_24xx *icb = (struct init_cb_24xx *)ha->init_cb;
6846 if (!QLA_TGT_MODE_ENABLED())
6849 WRT_REG_DWORD(ISP_ATIO_Q_IN(vha), 0);
6850 WRT_REG_DWORD(ISP_ATIO_Q_OUT(vha), 0);
6851 RD_REG_DWORD(ISP_ATIO_Q_OUT(vha));
6853 if (ha->flags.msix_enabled) {
6854 if (IS_QLA83XX(ha) || IS_QLA27XX(ha) || IS_QLA28XX(ha)) {
6855 if (IS_QLA2071(ha)) {
6856 /* 4 ports Baker: Enable Interrupt Handshake */
6858 icb->firmware_options_2 |= BIT_26;
6860 icb->msix_atio = cpu_to_le16(msix->entry);
6861 icb->firmware_options_2 &= ~BIT_26;
6863 ql_dbg(ql_dbg_init, vha, 0xf072,
6864 "Registering ICB vector 0x%x for atio que.\n",
6869 if (IS_QLA83XX(ha) || IS_QLA27XX(ha) || IS_QLA28XX(ha)) {
6871 icb->firmware_options_2 |= BIT_26;
6872 ql_dbg(ql_dbg_init, vha, 0xf072,
6873 "%s: Use INTx for ATIOQ.\n", __func__);
6879 qlt_24xx_config_nvram_stage1(struct scsi_qla_host *vha, struct nvram_24xx *nv)
6881 struct qla_hw_data *ha = vha->hw;
6884 if (!QLA_TGT_MODE_ENABLED())
6887 if (qla_tgt_mode_enabled(vha) || qla_dual_mode_enabled(vha)) {
6888 if (!ha->tgt.saved_set) {
6889 /* We save only once */
6890 ha->tgt.saved_exchange_count = nv->exchange_count;
6891 ha->tgt.saved_firmware_options_1 =
6892 nv->firmware_options_1;
6893 ha->tgt.saved_firmware_options_2 =
6894 nv->firmware_options_2;
6895 ha->tgt.saved_firmware_options_3 =
6896 nv->firmware_options_3;
6897 ha->tgt.saved_set = 1;
6900 if (qla_tgt_mode_enabled(vha))
6901 nv->exchange_count = cpu_to_le16(0xFFFF);
6903 nv->exchange_count = cpu_to_le16(vha->ql2xexchoffld);
6905 /* Enable target mode */
6906 nv->firmware_options_1 |= cpu_to_le32(BIT_4);
6908 /* Disable ini mode, if requested */
6909 if (qla_tgt_mode_enabled(vha))
6910 nv->firmware_options_1 |= cpu_to_le32(BIT_5);
6912 /* Disable Full Login after LIP */
6913 nv->firmware_options_1 &= cpu_to_le32(~BIT_13);
6914 /* Enable initial LIP */
6915 nv->firmware_options_1 &= cpu_to_le32(~BIT_9);
6916 if (ql2xtgt_tape_enable)
6917 /* Enable FC Tape support */
6918 nv->firmware_options_2 |= cpu_to_le32(BIT_12);
6920 /* Disable FC Tape support */
6921 nv->firmware_options_2 &= cpu_to_le32(~BIT_12);
6923 /* Disable Full Login after LIP */
6924 nv->host_p &= cpu_to_le32(~BIT_10);
6927 * clear BIT 15 explicitly as we have seen at least
6928 * a couple of instances where this was set and this
6929 * was causing the firmware to not be initialized.
6931 nv->firmware_options_1 &= cpu_to_le32(~BIT_15);
6932 /* Enable target PRLI control */
6933 nv->firmware_options_2 |= cpu_to_le32(BIT_14);
6935 if (IS_QLA25XX(ha)) {
6936 /* Change Loop-prefer to Pt-Pt */
6937 tmp = ~(BIT_4|BIT_5|BIT_6);
6938 nv->firmware_options_2 &= cpu_to_le32(tmp);
6940 nv->firmware_options_2 |= cpu_to_le32(tmp);
6943 if (ha->tgt.saved_set) {
6944 nv->exchange_count = ha->tgt.saved_exchange_count;
6945 nv->firmware_options_1 =
6946 ha->tgt.saved_firmware_options_1;
6947 nv->firmware_options_2 =
6948 ha->tgt.saved_firmware_options_2;
6949 nv->firmware_options_3 =
6950 ha->tgt.saved_firmware_options_3;
6955 if (ha->base_qpair->enable_class_2) {
6956 if (vha->flags.init_done)
6957 fc_host_supported_classes(vha->host) =
6958 FC_COS_CLASS2 | FC_COS_CLASS3;
6960 nv->firmware_options_2 |= cpu_to_le32(BIT_8);
6962 if (vha->flags.init_done)
6963 fc_host_supported_classes(vha->host) = FC_COS_CLASS3;
6965 nv->firmware_options_2 &= ~cpu_to_le32(BIT_8);
6970 qlt_24xx_config_nvram_stage2(struct scsi_qla_host *vha,
6971 struct init_cb_24xx *icb)
6973 struct qla_hw_data *ha = vha->hw;
6975 if (!QLA_TGT_MODE_ENABLED())
6978 if (ha->tgt.node_name_set) {
6979 memcpy(icb->node_name, ha->tgt.tgt_node_name, WWN_SIZE);
6980 icb->firmware_options_1 |= cpu_to_le32(BIT_14);
6985 qlt_81xx_config_nvram_stage1(struct scsi_qla_host *vha, struct nvram_81xx *nv)
6987 struct qla_hw_data *ha = vha->hw;
6990 if (!QLA_TGT_MODE_ENABLED())
6993 if (qla_tgt_mode_enabled(vha) || qla_dual_mode_enabled(vha)) {
6994 if (!ha->tgt.saved_set) {
6995 /* We save only once */
6996 ha->tgt.saved_exchange_count = nv->exchange_count;
6997 ha->tgt.saved_firmware_options_1 =
6998 nv->firmware_options_1;
6999 ha->tgt.saved_firmware_options_2 =
7000 nv->firmware_options_2;
7001 ha->tgt.saved_firmware_options_3 =
7002 nv->firmware_options_3;
7003 ha->tgt.saved_set = 1;
7006 if (qla_tgt_mode_enabled(vha))
7007 nv->exchange_count = cpu_to_le16(0xFFFF);
7009 nv->exchange_count = cpu_to_le16(vha->ql2xexchoffld);
7011 /* Enable target mode */
7012 nv->firmware_options_1 |= cpu_to_le32(BIT_4);
7014 /* Disable ini mode, if requested */
7015 if (qla_tgt_mode_enabled(vha))
7016 nv->firmware_options_1 |= cpu_to_le32(BIT_5);
7017 /* Disable Full Login after LIP */
7018 nv->firmware_options_1 &= cpu_to_le32(~BIT_13);
7019 /* Enable initial LIP */
7020 nv->firmware_options_1 &= cpu_to_le32(~BIT_9);
7022 * clear BIT 15 explicitly as we have seen at
7023 * least a couple of instances where this was set
7024 * and this was causing the firmware to not be
7027 nv->firmware_options_1 &= cpu_to_le32(~BIT_15);
7028 if (ql2xtgt_tape_enable)
7029 /* Enable FC tape support */
7030 nv->firmware_options_2 |= cpu_to_le32(BIT_12);
7032 /* Disable FC tape support */
7033 nv->firmware_options_2 &= cpu_to_le32(~BIT_12);
7035 /* Disable Full Login after LIP */
7036 nv->host_p &= cpu_to_le32(~BIT_10);
7037 /* Enable target PRLI control */
7038 nv->firmware_options_2 |= cpu_to_le32(BIT_14);
7040 /* Change Loop-prefer to Pt-Pt */
7041 tmp = ~(BIT_4|BIT_5|BIT_6);
7042 nv->firmware_options_2 &= cpu_to_le32(tmp);
7044 nv->firmware_options_2 |= cpu_to_le32(tmp);
7046 if (ha->tgt.saved_set) {
7047 nv->exchange_count = ha->tgt.saved_exchange_count;
7048 nv->firmware_options_1 =
7049 ha->tgt.saved_firmware_options_1;
7050 nv->firmware_options_2 =
7051 ha->tgt.saved_firmware_options_2;
7052 nv->firmware_options_3 =
7053 ha->tgt.saved_firmware_options_3;
7058 if (ha->base_qpair->enable_class_2) {
7059 if (vha->flags.init_done)
7060 fc_host_supported_classes(vha->host) =
7061 FC_COS_CLASS2 | FC_COS_CLASS3;
7063 nv->firmware_options_2 |= cpu_to_le32(BIT_8);
7065 if (vha->flags.init_done)
7066 fc_host_supported_classes(vha->host) = FC_COS_CLASS3;
7068 nv->firmware_options_2 &= ~cpu_to_le32(BIT_8);
7073 qlt_81xx_config_nvram_stage2(struct scsi_qla_host *vha,
7074 struct init_cb_81xx *icb)
7076 struct qla_hw_data *ha = vha->hw;
7078 if (!QLA_TGT_MODE_ENABLED())
7081 if (ha->tgt.node_name_set) {
7082 memcpy(icb->node_name, ha->tgt.tgt_node_name, WWN_SIZE);
7083 icb->firmware_options_1 |= cpu_to_le32(BIT_14);
7088 qlt_83xx_iospace_config(struct qla_hw_data *ha)
7090 if (!QLA_TGT_MODE_ENABLED())
7093 ha->msix_count += 1; /* For ATIO Q */
7098 qlt_modify_vp_config(struct scsi_qla_host *vha,
7099 struct vp_config_entry_24xx *vpmod)
7101 /* enable target mode. Bit5 = 1 => disable */
7102 if (qla_tgt_mode_enabled(vha) || qla_dual_mode_enabled(vha))
7103 vpmod->options_idx1 &= ~BIT_5;
7105 /* Disable ini mode, if requested. bit4 = 1 => disable */
7106 if (qla_tgt_mode_enabled(vha))
7107 vpmod->options_idx1 &= ~BIT_4;
7111 qlt_probe_one_stage1(struct scsi_qla_host *base_vha, struct qla_hw_data *ha)
7115 if (!QLA_TGT_MODE_ENABLED())
7118 if ((ql2xenablemsix == 0) || IS_QLA83XX(ha) || IS_QLA27XX(ha) ||
7120 ISP_ATIO_Q_IN(base_vha) = &ha->mqiobase->isp25mq.atio_q_in;
7121 ISP_ATIO_Q_OUT(base_vha) = &ha->mqiobase->isp25mq.atio_q_out;
7123 ISP_ATIO_Q_IN(base_vha) = &ha->iobase->isp24.atio_q_in;
7124 ISP_ATIO_Q_OUT(base_vha) = &ha->iobase->isp24.atio_q_out;
7127 mutex_init(&base_vha->vha_tgt.tgt_mutex);
7128 mutex_init(&base_vha->vha_tgt.tgt_host_action_mutex);
7130 INIT_LIST_HEAD(&base_vha->unknown_atio_list);
7131 INIT_DELAYED_WORK(&base_vha->unknown_atio_work,
7132 qlt_unknown_atio_work_fn);
7134 qlt_clear_mode(base_vha);
7136 rc = btree_init32(&ha->tgt.host_map);
7138 ql_log(ql_log_info, base_vha, 0xd03d,
7139 "Unable to initialize ha->host_map btree\n");
7141 qlt_update_vp_map(base_vha, SET_VP_IDX);
7145 qla83xx_msix_atio_q(int irq, void *dev_id)
7147 struct rsp_que *rsp;
7148 scsi_qla_host_t *vha;
7149 struct qla_hw_data *ha;
7150 unsigned long flags;
7152 rsp = (struct rsp_que *) dev_id;
7154 vha = pci_get_drvdata(ha->pdev);
7156 spin_lock_irqsave(&ha->tgt.atio_lock, flags);
7158 qlt_24xx_process_atio_queue(vha, 0);
7160 spin_unlock_irqrestore(&ha->tgt.atio_lock, flags);
7166 qlt_handle_abts_recv_work(struct work_struct *work)
7168 struct qla_tgt_sess_op *op = container_of(work,
7169 struct qla_tgt_sess_op, work);
7170 scsi_qla_host_t *vha = op->vha;
7171 struct qla_hw_data *ha = vha->hw;
7172 unsigned long flags;
7174 if (qla2x00_reset_active(vha) ||
7175 (op->chip_reset != ha->base_qpair->chip_reset))
7178 spin_lock_irqsave(&ha->tgt.atio_lock, flags);
7179 qlt_24xx_process_atio_queue(vha, 0);
7180 spin_unlock_irqrestore(&ha->tgt.atio_lock, flags);
7182 spin_lock_irqsave(&ha->hardware_lock, flags);
7183 qlt_response_pkt_all_vps(vha, op->rsp, (response_t *)&op->atio);
7184 spin_unlock_irqrestore(&ha->hardware_lock, flags);
7190 qlt_handle_abts_recv(struct scsi_qla_host *vha, struct rsp_que *rsp,
7193 struct qla_tgt_sess_op *op;
7195 op = kzalloc(sizeof(*op), GFP_ATOMIC);
7198 /* do not reach for ATIO queue here. This is best effort err
7199 * recovery at this point.
7201 qlt_response_pkt_all_vps(vha, rsp, pkt);
7205 memcpy(&op->atio, pkt, sizeof(*pkt));
7207 op->chip_reset = vha->hw->base_qpair->chip_reset;
7209 INIT_WORK(&op->work, qlt_handle_abts_recv_work);
7210 queue_work(qla_tgt_wq, &op->work);
7215 qlt_mem_alloc(struct qla_hw_data *ha)
7217 if (!QLA_TGT_MODE_ENABLED())
7220 ha->tgt.tgt_vp_map = kcalloc(MAX_MULTI_ID_FABRIC,
7221 sizeof(struct qla_tgt_vp_map),
7223 if (!ha->tgt.tgt_vp_map)
7226 ha->tgt.atio_ring = dma_alloc_coherent(&ha->pdev->dev,
7227 (ha->tgt.atio_q_length + 1) * sizeof(struct atio_from_isp),
7228 &ha->tgt.atio_dma, GFP_KERNEL);
7229 if (!ha->tgt.atio_ring) {
7230 kfree(ha->tgt.tgt_vp_map);
7237 qlt_mem_free(struct qla_hw_data *ha)
7239 if (!QLA_TGT_MODE_ENABLED())
7242 if (ha->tgt.atio_ring) {
7243 dma_free_coherent(&ha->pdev->dev, (ha->tgt.atio_q_length + 1) *
7244 sizeof(struct atio_from_isp), ha->tgt.atio_ring,
7247 ha->tgt.atio_ring = NULL;
7248 ha->tgt.atio_dma = 0;
7249 kfree(ha->tgt.tgt_vp_map);
7250 ha->tgt.tgt_vp_map = NULL;
7253 /* vport_slock to be held by the caller */
7255 qlt_update_vp_map(struct scsi_qla_host *vha, int cmd)
7261 if (!QLA_TGT_MODE_ENABLED())
7264 key = vha->d_id.b24;
7268 vha->hw->tgt.tgt_vp_map[vha->vp_idx].vha = vha;
7271 slot = btree_lookup32(&vha->hw->tgt.host_map, key);
7273 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf018,
7274 "Save vha in host_map %p %06x\n", vha, key);
7275 rc = btree_insert32(&vha->hw->tgt.host_map,
7276 key, vha, GFP_ATOMIC);
7278 ql_log(ql_log_info, vha, 0xd03e,
7279 "Unable to insert s_id into host_map: %06x\n",
7283 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf019,
7284 "replace existing vha in host_map %p %06x\n", vha, key);
7285 btree_update32(&vha->hw->tgt.host_map, key, vha);
7288 vha->hw->tgt.tgt_vp_map[vha->vp_idx].vha = NULL;
7291 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01a,
7292 "clear vha in host_map %p %06x\n", vha, key);
7293 slot = btree_lookup32(&vha->hw->tgt.host_map, key);
7295 btree_remove32(&vha->hw->tgt.host_map, key);
7301 void qlt_update_host_map(struct scsi_qla_host *vha, port_id_t id)
7304 if (!vha->d_id.b24) {
7306 qlt_update_vp_map(vha, SET_AL_PA);
7307 } else if (vha->d_id.b24 != id.b24) {
7308 qlt_update_vp_map(vha, RESET_AL_PA);
7310 qlt_update_vp_map(vha, SET_AL_PA);
7314 static int __init qlt_parse_ini_mode(void)
7316 if (strcasecmp(qlini_mode, QLA2XXX_INI_MODE_STR_EXCLUSIVE) == 0)
7317 ql2x_ini_mode = QLA2XXX_INI_MODE_EXCLUSIVE;
7318 else if (strcasecmp(qlini_mode, QLA2XXX_INI_MODE_STR_DISABLED) == 0)
7319 ql2x_ini_mode = QLA2XXX_INI_MODE_DISABLED;
7320 else if (strcasecmp(qlini_mode, QLA2XXX_INI_MODE_STR_ENABLED) == 0)
7321 ql2x_ini_mode = QLA2XXX_INI_MODE_ENABLED;
7322 else if (strcasecmp(qlini_mode, QLA2XXX_INI_MODE_STR_DUAL) == 0)
7323 ql2x_ini_mode = QLA2XXX_INI_MODE_DUAL;
7330 int __init qlt_init(void)
7334 BUILD_BUG_ON(sizeof(struct ctio7_to_24xx) != 64);
7335 BUILD_BUG_ON(sizeof(struct ctio_to_2xxx) != 64);
7337 if (!qlt_parse_ini_mode()) {
7338 ql_log(ql_log_fatal, NULL, 0xe06b,
7339 "qlt_parse_ini_mode() failed\n");
7343 if (!QLA_TGT_MODE_ENABLED())
7346 qla_tgt_mgmt_cmd_cachep = kmem_cache_create("qla_tgt_mgmt_cmd_cachep",
7347 sizeof(struct qla_tgt_mgmt_cmd), __alignof__(struct
7348 qla_tgt_mgmt_cmd), 0, NULL);
7349 if (!qla_tgt_mgmt_cmd_cachep) {
7350 ql_log(ql_log_fatal, NULL, 0xd04b,
7351 "kmem_cache_create for qla_tgt_mgmt_cmd_cachep failed\n");
7355 qla_tgt_plogi_cachep = kmem_cache_create("qla_tgt_plogi_cachep",
7356 sizeof(struct qlt_plogi_ack_t), __alignof__(struct qlt_plogi_ack_t),
7359 if (!qla_tgt_plogi_cachep) {
7360 ql_log(ql_log_fatal, NULL, 0xe06d,
7361 "kmem_cache_create for qla_tgt_plogi_cachep failed\n");
7363 goto out_mgmt_cmd_cachep;
7366 qla_tgt_mgmt_cmd_mempool = mempool_create(25, mempool_alloc_slab,
7367 mempool_free_slab, qla_tgt_mgmt_cmd_cachep);
7368 if (!qla_tgt_mgmt_cmd_mempool) {
7369 ql_log(ql_log_fatal, NULL, 0xe06e,
7370 "mempool_create for qla_tgt_mgmt_cmd_mempool failed\n");
7372 goto out_plogi_cachep;
7375 qla_tgt_wq = alloc_workqueue("qla_tgt_wq", 0, 0);
7377 ql_log(ql_log_fatal, NULL, 0xe06f,
7378 "alloc_workqueue for qla_tgt_wq failed\n");
7380 goto out_cmd_mempool;
7383 * Return 1 to signal that initiator-mode is being disabled
7385 return (ql2x_ini_mode == QLA2XXX_INI_MODE_DISABLED) ? 1 : 0;
7388 mempool_destroy(qla_tgt_mgmt_cmd_mempool);
7390 kmem_cache_destroy(qla_tgt_plogi_cachep);
7391 out_mgmt_cmd_cachep:
7392 kmem_cache_destroy(qla_tgt_mgmt_cmd_cachep);
7398 if (!QLA_TGT_MODE_ENABLED())
7401 destroy_workqueue(qla_tgt_wq);
7402 mempool_destroy(qla_tgt_mgmt_cmd_mempool);
7403 kmem_cache_destroy(qla_tgt_plogi_cachep);
7404 kmem_cache_destroy(qla_tgt_mgmt_cmd_cachep);