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 list_for_each_entry(pla, &vha->plogi_ack_list, list) {
793 if (pla->id.b24 == id->b24) {
794 ql_dbg(ql_dbg_disc + ql_dbg_verbose, vha, 0x210d,
795 "%s %d %8phC Term INOT due to new INOT",
797 pla->iocb.u.isp24.port_name);
798 qlt_send_term_imm_notif(vha, &pla->iocb, 1);
799 memcpy(&pla->iocb, iocb, sizeof(pla->iocb));
804 pla = kmem_cache_zalloc(qla_tgt_plogi_cachep, GFP_ATOMIC);
806 ql_dbg(ql_dbg_async, vha, 0x5088,
807 "qla_target(%d): Allocation of plogi_ack failed\n",
812 memcpy(&pla->iocb, iocb, sizeof(pla->iocb));
814 list_add_tail(&pla->list, &vha->plogi_ack_list);
819 void qlt_plogi_ack_unref(struct scsi_qla_host *vha,
820 struct qlt_plogi_ack_t *pla)
822 struct imm_ntfy_from_isp *iocb = &pla->iocb;
825 fc_port_t *fcport = pla->fcport;
827 BUG_ON(!pla->ref_count);
833 ql_dbg(ql_dbg_disc, vha, 0x5089,
834 "Sending PLOGI ACK to wwn %8phC s_id %02x:%02x:%02x loop_id %#04x"
835 " exch %#x ox_id %#x\n", iocb->u.isp24.port_name,
836 iocb->u.isp24.port_id[2], iocb->u.isp24.port_id[1],
837 iocb->u.isp24.port_id[0],
838 le16_to_cpu(iocb->u.isp24.nport_handle),
839 iocb->u.isp24.exchange_address, iocb->ox_id);
841 port_id.b.domain = iocb->u.isp24.port_id[2];
842 port_id.b.area = iocb->u.isp24.port_id[1];
843 port_id.b.al_pa = iocb->u.isp24.port_id[0];
844 port_id.b.rsvd_1 = 0;
846 loop_id = le16_to_cpu(iocb->u.isp24.nport_handle);
848 fcport->loop_id = loop_id;
849 fcport->d_id = port_id;
850 if (iocb->u.isp24.status_subcode == ELS_PLOGI)
851 qla24xx_post_nack_work(vha, fcport, iocb, SRB_NACK_PLOGI);
853 qla24xx_post_nack_work(vha, fcport, iocb, SRB_NACK_PRLI);
855 list_for_each_entry(fcport, &vha->vp_fcports, list) {
856 if (fcport->plogi_link[QLT_PLOGI_LINK_SAME_WWN] == pla)
857 fcport->plogi_link[QLT_PLOGI_LINK_SAME_WWN] = NULL;
858 if (fcport->plogi_link[QLT_PLOGI_LINK_CONFLICT] == pla)
859 fcport->plogi_link[QLT_PLOGI_LINK_CONFLICT] = NULL;
862 list_del(&pla->list);
863 kmem_cache_free(qla_tgt_plogi_cachep, pla);
867 qlt_plogi_ack_link(struct scsi_qla_host *vha, struct qlt_plogi_ack_t *pla,
868 struct fc_port *sess, enum qlt_plogi_link_t link)
870 struct imm_ntfy_from_isp *iocb = &pla->iocb;
871 /* Inc ref_count first because link might already be pointing at pla */
874 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf097,
875 "Linking sess %p [%d] wwn %8phC with PLOGI ACK to wwn %8phC"
876 " s_id %02x:%02x:%02x, ref=%d pla %p link %d\n",
877 sess, link, sess->port_name,
878 iocb->u.isp24.port_name, iocb->u.isp24.port_id[2],
879 iocb->u.isp24.port_id[1], iocb->u.isp24.port_id[0],
880 pla->ref_count, pla, link);
882 if (link == QLT_PLOGI_LINK_CONFLICT) {
883 switch (sess->disc_state) {
885 case DSC_DELETE_PEND:
893 if (sess->plogi_link[link])
894 qlt_plogi_ack_unref(vha, sess->plogi_link[link]);
896 if (link == QLT_PLOGI_LINK_SAME_WWN)
899 sess->plogi_link[link] = pla;
903 /* These fields must be initialized by the caller */
906 * number of cmds dropped while we were waiting for
907 * initiator to ack LOGO initialize to 1 if LOGO is
908 * triggered by a command, otherwise, to 0
912 /* These fields are used by callee */
913 struct list_head list;
917 qlt_send_first_logo(struct scsi_qla_host *vha, qlt_port_logo_t *logo)
919 qlt_port_logo_t *tmp;
922 mutex_lock(&vha->vha_tgt.tgt_mutex);
924 list_for_each_entry(tmp, &vha->logo_list, list) {
925 if (tmp->id.b24 == logo->id.b24) {
926 tmp->cmd_count += logo->cmd_count;
927 mutex_unlock(&vha->vha_tgt.tgt_mutex);
932 list_add_tail(&logo->list, &vha->logo_list);
934 mutex_unlock(&vha->vha_tgt.tgt_mutex);
936 res = qla24xx_els_dcmd_iocb(vha, ELS_DCMD_LOGO, logo->id);
938 mutex_lock(&vha->vha_tgt.tgt_mutex);
939 list_del(&logo->list);
940 mutex_unlock(&vha->vha_tgt.tgt_mutex);
942 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf098,
943 "Finished LOGO to %02x:%02x:%02x, dropped %d cmds, res = %#x\n",
944 logo->id.b.domain, logo->id.b.area, logo->id.b.al_pa,
945 logo->cmd_count, res);
948 void qlt_free_session_done(struct work_struct *work)
950 struct fc_port *sess = container_of(work, struct fc_port,
952 struct qla_tgt *tgt = sess->tgt;
953 struct scsi_qla_host *vha = sess->vha;
954 struct qla_hw_data *ha = vha->hw;
956 bool logout_started = false;
957 scsi_qla_host_t *base_vha;
958 struct qlt_plogi_ack_t *own =
959 sess->plogi_link[QLT_PLOGI_LINK_SAME_WWN];
961 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf084,
962 "%s: se_sess %p / sess %p from port %8phC loop_id %#04x"
963 " s_id %02x:%02x:%02x logout %d keep %d els_logo %d\n",
964 __func__, sess->se_sess, sess, sess->port_name, sess->loop_id,
965 sess->d_id.b.domain, sess->d_id.b.area, sess->d_id.b.al_pa,
966 sess->logout_on_delete, sess->keep_nport_handle,
967 sess->send_els_logo);
969 if (!IS_SW_RESV_ADDR(sess->d_id)) {
970 qla2x00_mark_device_lost(vha, sess, 0, 0);
972 if (sess->send_els_logo) {
973 qlt_port_logo_t logo;
975 logo.id = sess->d_id;
978 qlt_send_first_logo(vha, &logo);
979 sess->send_els_logo = 0;
982 if (sess->logout_on_delete && sess->loop_id != FC_NO_LOOP_ID) {
987 (own->iocb.u.isp24.status_subcode == ELS_PLOGI))) {
988 rc = qla2x00_post_async_logout_work(vha, sess,
990 if (rc != QLA_SUCCESS)
991 ql_log(ql_log_warn, vha, 0xf085,
992 "Schedule logo failed sess %p rc %d\n",
995 logout_started = true;
996 } else if (own && (own->iocb.u.isp24.status_subcode ==
997 ELS_PRLI) && ha->flags.rida_fmt2) {
998 rc = qla2x00_post_async_prlo_work(vha, sess,
1000 if (rc != QLA_SUCCESS)
1001 ql_log(ql_log_warn, vha, 0xf085,
1002 "Schedule PRLO failed sess %p rc %d\n",
1005 logout_started = true;
1011 * Release the target session for FC Nexus from fabric module code.
1013 if (sess->se_sess != NULL)
1014 ha->tgt.tgt_ops->free_session(sess);
1016 if (logout_started) {
1017 bool traced = false;
1019 while (!READ_ONCE(sess->logout_completed)) {
1021 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf086,
1022 "%s: waiting for sess %p logout\n",
1029 ql_dbg(ql_dbg_disc, vha, 0xf087,
1030 "%s: sess %p logout completed\n", __func__, sess);
1033 if (sess->logo_ack_needed) {
1034 sess->logo_ack_needed = 0;
1035 qla24xx_async_notify_ack(vha, sess,
1036 (struct imm_ntfy_from_isp *)sess->iocb, SRB_NACK_LOGO);
1039 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
1040 if (sess->se_sess) {
1041 sess->se_sess = NULL;
1042 if (tgt && !IS_SW_RESV_ADDR(sess->d_id))
1046 sess->disc_state = DSC_DELETED;
1047 sess->fw_login_state = DSC_LS_PORT_UNAVAIL;
1048 sess->deleted = QLA_SESS_DELETED;
1050 if (sess->login_succ && !IS_SW_RESV_ADDR(sess->d_id)) {
1051 vha->fcport_count--;
1052 sess->login_succ = 0;
1055 qla2x00_clear_loop_id(sess);
1057 if (sess->conflict) {
1058 sess->conflict->login_pause = 0;
1059 sess->conflict = NULL;
1060 if (!test_bit(UNLOADING, &vha->dpc_flags))
1061 set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
1065 struct qlt_plogi_ack_t *con =
1066 sess->plogi_link[QLT_PLOGI_LINK_CONFLICT];
1067 struct imm_ntfy_from_isp *iocb;
1069 own = sess->plogi_link[QLT_PLOGI_LINK_SAME_WWN];
1073 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf099,
1074 "se_sess %p / sess %p port %8phC is gone,"
1075 " %s (ref=%d), releasing PLOGI for %8phC (ref=%d)\n",
1076 sess->se_sess, sess, sess->port_name,
1077 own ? "releasing own PLOGI" : "no own PLOGI pending",
1078 own ? own->ref_count : -1,
1079 iocb->u.isp24.port_name, con->ref_count);
1080 qlt_plogi_ack_unref(vha, con);
1081 sess->plogi_link[QLT_PLOGI_LINK_CONFLICT] = NULL;
1083 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf09a,
1084 "se_sess %p / sess %p port %8phC is gone, %s (ref=%d)\n",
1085 sess->se_sess, sess, sess->port_name,
1086 own ? "releasing own PLOGI" :
1087 "no own PLOGI pending",
1088 own ? own->ref_count : -1);
1092 sess->fw_login_state = DSC_LS_PLOGI_PEND;
1093 qlt_plogi_ack_unref(vha, own);
1094 sess->plogi_link[QLT_PLOGI_LINK_SAME_WWN] = NULL;
1098 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
1100 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf001,
1101 "Unregistration of sess %p %8phC finished fcp_cnt %d\n",
1102 sess, sess->port_name, vha->fcport_count);
1104 if (tgt && (tgt->sess_count == 0))
1105 wake_up_all(&tgt->waitQ);
1107 if (vha->fcport_count == 0)
1108 wake_up_all(&vha->fcport_waitQ);
1110 base_vha = pci_get_drvdata(ha->pdev);
1112 sess->free_pending = 0;
1114 if (test_bit(PFLG_DRIVER_REMOVING, &base_vha->pci_flags))
1117 if ((!tgt || !tgt->tgt_stop) && !LOOP_TRANSITION(vha)) {
1118 switch (vha->host->active_mode) {
1119 case MODE_INITIATOR:
1121 set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
1122 qla2xxx_wake_dpc(vha);
1132 /* ha->tgt.sess_lock supposed to be held on entry */
1133 void qlt_unreg_sess(struct fc_port *sess)
1135 struct scsi_qla_host *vha = sess->vha;
1136 unsigned long flags;
1138 ql_dbg(ql_dbg_disc, sess->vha, 0x210a,
1139 "%s sess %p for deletion %8phC\n",
1140 __func__, sess, sess->port_name);
1142 spin_lock_irqsave(&sess->vha->work_lock, flags);
1143 if (sess->free_pending) {
1144 spin_unlock_irqrestore(&sess->vha->work_lock, flags);
1147 sess->free_pending = 1;
1148 spin_unlock_irqrestore(&sess->vha->work_lock, flags);
1151 vha->hw->tgt.tgt_ops->clear_nacl_from_fcport_map(sess);
1153 sess->deleted = QLA_SESS_DELETION_IN_PROGRESS;
1154 sess->disc_state = DSC_DELETE_PEND;
1155 sess->last_rscn_gen = sess->rscn_gen;
1156 sess->last_login_gen = sess->login_gen;
1158 if (sess->nvme_flag & NVME_FLAG_REGISTERED &&
1159 !(sess->nvme_flag & NVME_FLAG_DELETING)) {
1160 sess->nvme_flag |= NVME_FLAG_DELETING;
1161 schedule_work(&sess->nvme_del_work);
1163 INIT_WORK(&sess->free_work, qlt_free_session_done);
1164 schedule_work(&sess->free_work);
1167 EXPORT_SYMBOL(qlt_unreg_sess);
1169 static int qlt_reset(struct scsi_qla_host *vha, void *iocb, int mcmd)
1171 struct qla_hw_data *ha = vha->hw;
1172 struct fc_port *sess = NULL;
1175 struct imm_ntfy_from_isp *n = (struct imm_ntfy_from_isp *)iocb;
1176 unsigned long flags;
1178 loop_id = le16_to_cpu(n->u.isp24.nport_handle);
1179 if (loop_id == 0xFFFF) {
1181 atomic_inc(&vha->vha_tgt.qla_tgt->tgt_global_resets_count);
1182 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
1183 qlt_clear_tgt_db(vha->vha_tgt.qla_tgt);
1184 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
1186 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
1187 sess = ha->tgt.tgt_ops->find_sess_by_loop_id(vha, loop_id);
1188 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
1191 ql_dbg(ql_dbg_tgt, vha, 0xe000,
1192 "Using sess for qla_tgt_reset: %p\n", sess);
1198 ql_dbg(ql_dbg_tgt, vha, 0xe047,
1199 "scsi(%ld): resetting (session %p from port %8phC mcmd %x, "
1200 "loop_id %d)\n", vha->host_no, sess, sess->port_name,
1203 return qlt_issue_task_mgmt(sess, 0, mcmd, iocb, QLA24XX_MGMT_SEND_NACK);
1206 static void qla24xx_chk_fcp_state(struct fc_port *sess)
1208 if (sess->chip_reset != sess->vha->hw->base_qpair->chip_reset) {
1209 sess->logout_on_delete = 0;
1210 sess->logo_ack_needed = 0;
1211 sess->fw_login_state = DSC_LS_PORT_UNAVAIL;
1212 sess->scan_state = 0;
1216 void qlt_schedule_sess_for_deletion(struct fc_port *sess)
1218 struct qla_tgt *tgt = sess->tgt;
1219 unsigned long flags;
1222 switch (sess->disc_state) {
1223 case DSC_DELETE_PEND:
1226 if (tgt && tgt->tgt_stop && (tgt->sess_count == 0))
1227 wake_up_all(&tgt->waitQ);
1228 if (sess->vha->fcport_count == 0)
1229 wake_up_all(&sess->vha->fcport_waitQ);
1231 if (!sess->plogi_link[QLT_PLOGI_LINK_SAME_WWN] &&
1232 !sess->plogi_link[QLT_PLOGI_LINK_CONFLICT])
1235 case DSC_UPD_FCPORT:
1237 * This port is not done reporting to upper layer.
1240 sess->next_disc_state = DSC_DELETE_PEND;
1241 sec = jiffies_to_msecs(jiffies -
1242 sess->jiffies_at_registration)/1000;
1243 if (sess->sec_since_registration < sec && sec && !(sec % 5)) {
1244 sess->sec_since_registration = sec;
1245 ql_dbg(ql_dbg_disc, sess->vha, 0xffff,
1246 "%s %8phC : Slow Rport registration(%d Sec)\n",
1247 __func__, sess->port_name, sec);
1254 spin_lock_irqsave(&sess->vha->work_lock, flags);
1255 if (sess->deleted == QLA_SESS_DELETION_IN_PROGRESS) {
1256 spin_unlock_irqrestore(&sess->vha->work_lock, flags);
1259 sess->deleted = QLA_SESS_DELETION_IN_PROGRESS;
1260 spin_unlock_irqrestore(&sess->vha->work_lock, flags);
1262 sess->disc_state = DSC_DELETE_PEND;
1264 qla24xx_chk_fcp_state(sess);
1266 ql_dbg(ql_dbg_tgt, sess->vha, 0xe001,
1267 "Scheduling sess %p for deletion %8phC\n",
1268 sess, sess->port_name);
1270 INIT_WORK(&sess->del_work, qla24xx_delete_sess_fn);
1271 WARN_ON(!queue_work(sess->vha->hw->wq, &sess->del_work));
1274 static void qlt_clear_tgt_db(struct qla_tgt *tgt)
1276 struct fc_port *sess;
1277 scsi_qla_host_t *vha = tgt->vha;
1279 list_for_each_entry(sess, &vha->vp_fcports, list) {
1281 qlt_schedule_sess_for_deletion(sess);
1284 /* At this point tgt could be already dead */
1287 static int qla24xx_get_loop_id(struct scsi_qla_host *vha, const uint8_t *s_id,
1290 struct qla_hw_data *ha = vha->hw;
1291 dma_addr_t gid_list_dma;
1292 struct gid_list_info *gid_list;
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;
1316 id_iter = (char *)gid_list;
1318 for (i = 0; i < entries; i++) {
1319 struct gid_list_info *gid = (struct gid_list_info *)id_iter;
1321 if ((gid->al_pa == s_id[2]) &&
1322 (gid->area == s_id[1]) &&
1323 (gid->domain == s_id[0])) {
1324 *loop_id = le16_to_cpu(gid->loop_id);
1328 id_iter += ha->gid_list_info_size;
1332 dma_free_coherent(&ha->pdev->dev, qla2x00_gid_list_size(ha),
1333 gid_list, gid_list_dma);
1338 * Adds an extra ref to allow to drop hw lock after adding sess to the list.
1339 * Caller must put it.
1341 static struct fc_port *qlt_create_sess(
1342 struct scsi_qla_host *vha,
1346 struct qla_hw_data *ha = vha->hw;
1347 struct fc_port *sess = fcport;
1348 unsigned long flags;
1350 if (vha->vha_tgt.qla_tgt->tgt_stop)
1353 if (fcport->se_sess) {
1354 if (!kref_get_unless_zero(&sess->sess_kref)) {
1355 ql_dbg(ql_dbg_disc, vha, 0x20f6,
1356 "%s: kref_get_unless_zero failed for %8phC\n",
1357 __func__, sess->port_name);
1362 sess->tgt = vha->vha_tgt.qla_tgt;
1363 sess->local = local;
1366 * Under normal circumstances we want to logout from firmware when
1367 * session eventually ends and release corresponding nport handle.
1368 * In the exception cases (e.g. when new PLOGI is waiting) corresponding
1369 * code will adjust these flags as necessary.
1371 sess->logout_on_delete = 1;
1372 sess->keep_nport_handle = 0;
1373 sess->logout_completed = 0;
1375 if (ha->tgt.tgt_ops->check_initiator_node_acl(vha,
1376 &fcport->port_name[0], sess) < 0) {
1377 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf015,
1378 "(%d) %8phC check_initiator_node_acl failed\n",
1379 vha->vp_idx, fcport->port_name);
1382 kref_init(&fcport->sess_kref);
1384 * Take an extra reference to ->sess_kref here to handle
1385 * fc_port access across ->tgt.sess_lock reaquire.
1387 if (!kref_get_unless_zero(&sess->sess_kref)) {
1388 ql_dbg(ql_dbg_disc, vha, 0x20f7,
1389 "%s: kref_get_unless_zero failed for %8phC\n",
1390 __func__, sess->port_name);
1394 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
1395 if (!IS_SW_RESV_ADDR(sess->d_id))
1396 vha->vha_tgt.qla_tgt->sess_count++;
1398 qlt_do_generation_tick(vha, &sess->generation);
1399 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
1402 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf006,
1403 "Adding sess %p se_sess %p to tgt %p sess_count %d\n",
1404 sess, sess->se_sess, vha->vha_tgt.qla_tgt,
1405 vha->vha_tgt.qla_tgt->sess_count);
1407 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04b,
1408 "qla_target(%d): %ssession for wwn %8phC (loop_id %d, "
1409 "s_id %x:%x:%x, confirmed completion %ssupported) added\n",
1410 vha->vp_idx, local ? "local " : "", fcport->port_name,
1411 fcport->loop_id, sess->d_id.b.domain, sess->d_id.b.area,
1412 sess->d_id.b.al_pa, sess->conf_compl_supported ? "" : "not ");
1418 * max_gen - specifies maximum session generation
1419 * at which this deletion requestion is still valid
1422 qlt_fc_port_deleted(struct scsi_qla_host *vha, fc_port_t *fcport, int max_gen)
1424 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
1425 struct fc_port *sess = fcport;
1426 unsigned long flags;
1428 if (!vha->hw->tgt.tgt_ops)
1434 spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
1435 if (tgt->tgt_stop) {
1436 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
1439 if (!sess->se_sess) {
1440 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
1444 if (max_gen - sess->generation < 0) {
1445 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
1446 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf092,
1447 "Ignoring stale deletion request for se_sess %p / sess %p"
1448 " for port %8phC, req_gen %d, sess_gen %d\n",
1449 sess->se_sess, sess, sess->port_name, max_gen,
1454 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf008, "qla_tgt_fc_port_deleted %p", sess);
1457 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
1458 qlt_schedule_sess_for_deletion(sess);
1461 static inline int test_tgt_sess_count(struct qla_tgt *tgt)
1463 struct qla_hw_data *ha = tgt->ha;
1464 unsigned long flags;
1467 * We need to protect against race, when tgt is freed before or
1470 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
1471 ql_dbg(ql_dbg_tgt, tgt->vha, 0xe002,
1472 "tgt %p, sess_count=%d\n",
1473 tgt, tgt->sess_count);
1474 res = (tgt->sess_count == 0);
1475 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
1480 /* Called by tcm_qla2xxx configfs code */
1481 int qlt_stop_phase1(struct qla_tgt *tgt)
1483 struct scsi_qla_host *vha = tgt->vha;
1484 struct qla_hw_data *ha = tgt->ha;
1485 unsigned long flags;
1487 mutex_lock(&ha->optrom_mutex);
1488 mutex_lock(&qla_tgt_mutex);
1490 if (tgt->tgt_stop || tgt->tgt_stopped) {
1491 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04e,
1492 "Already in tgt->tgt_stop or tgt_stopped state\n");
1493 mutex_unlock(&qla_tgt_mutex);
1494 mutex_unlock(&ha->optrom_mutex);
1498 ql_dbg(ql_dbg_tgt_mgt, vha, 0xe003, "Stopping target for host %ld(%p)\n",
1501 * Mutex needed to sync with qla_tgt_fc_port_[added,deleted].
1502 * Lock is needed, because we still can get an incoming packet.
1504 mutex_lock(&vha->vha_tgt.tgt_mutex);
1506 qlt_clear_tgt_db(tgt);
1507 mutex_unlock(&vha->vha_tgt.tgt_mutex);
1508 mutex_unlock(&qla_tgt_mutex);
1510 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf009,
1511 "Waiting for sess works (tgt %p)", tgt);
1512 spin_lock_irqsave(&tgt->sess_work_lock, flags);
1513 while (!list_empty(&tgt->sess_works_list)) {
1514 spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
1515 flush_scheduled_work();
1516 spin_lock_irqsave(&tgt->sess_work_lock, flags);
1518 spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
1520 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00a,
1521 "Waiting for tgt %p: sess_count=%d\n", tgt, tgt->sess_count);
1523 wait_event_timeout(tgt->waitQ, test_tgt_sess_count(tgt), 10*HZ);
1526 if (!ha->flags.host_shutting_down &&
1527 (qla_tgt_mode_enabled(vha) || qla_dual_mode_enabled(vha)))
1528 qlt_disable_vha(vha);
1530 /* Wait for sessions to clear out (just in case) */
1531 wait_event_timeout(tgt->waitQ, test_tgt_sess_count(tgt), 10*HZ);
1532 mutex_unlock(&ha->optrom_mutex);
1536 EXPORT_SYMBOL(qlt_stop_phase1);
1538 /* Called by tcm_qla2xxx configfs code */
1539 void qlt_stop_phase2(struct qla_tgt *tgt)
1541 scsi_qla_host_t *vha = tgt->vha;
1543 if (tgt->tgt_stopped) {
1544 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04f,
1545 "Already in tgt->tgt_stopped state\n");
1549 if (!tgt->tgt_stop) {
1550 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00b,
1551 "%s: phase1 stop is not completed\n", __func__);
1556 mutex_lock(&vha->vha_tgt.tgt_mutex);
1558 tgt->tgt_stopped = 1;
1559 mutex_unlock(&vha->vha_tgt.tgt_mutex);
1561 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00c, "Stop of tgt %p finished\n",
1564 switch (vha->qlini_mode) {
1565 case QLA2XXX_INI_MODE_EXCLUSIVE:
1566 vha->flags.online = 1;
1567 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
1573 EXPORT_SYMBOL(qlt_stop_phase2);
1575 /* Called from qlt_remove_target() -> qla2x00_remove_one() */
1576 static void qlt_release(struct qla_tgt *tgt)
1578 scsi_qla_host_t *vha = tgt->vha;
1582 struct qla_qpair_hint *h;
1583 struct qla_hw_data *ha = vha->hw;
1585 if ((vha->vha_tgt.qla_tgt != NULL) && !tgt->tgt_stop &&
1587 qlt_stop_phase1(tgt);
1589 if ((vha->vha_tgt.qla_tgt != NULL) && !tgt->tgt_stopped)
1590 qlt_stop_phase2(tgt);
1592 for (i = 0; i < vha->hw->max_qpairs + 1; i++) {
1593 unsigned long flags;
1595 h = &tgt->qphints[i];
1597 spin_lock_irqsave(h->qpair->qp_lock_ptr, flags);
1598 list_del(&h->hint_elem);
1599 spin_unlock_irqrestore(h->qpair->qp_lock_ptr, flags);
1603 kfree(tgt->qphints);
1604 mutex_lock(&qla_tgt_mutex);
1605 list_del(&vha->vha_tgt.qla_tgt->tgt_list_entry);
1606 mutex_unlock(&qla_tgt_mutex);
1608 btree_for_each_safe64(&tgt->lun_qpair_map, key, node)
1609 btree_remove64(&tgt->lun_qpair_map, key);
1611 btree_destroy64(&tgt->lun_qpair_map);
1614 if (ha->tgt.tgt_ops &&
1615 ha->tgt.tgt_ops->remove_target &&
1616 vha->vha_tgt.target_lport_ptr)
1617 ha->tgt.tgt_ops->remove_target(vha);
1619 vha->vha_tgt.qla_tgt = NULL;
1621 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00d,
1622 "Release of tgt %p finished\n", tgt);
1627 /* ha->hardware_lock supposed to be held on entry */
1628 static int qlt_sched_sess_work(struct qla_tgt *tgt, int type,
1629 const void *param, unsigned int param_size)
1631 struct qla_tgt_sess_work_param *prm;
1632 unsigned long flags;
1634 prm = kzalloc(sizeof(*prm), GFP_ATOMIC);
1636 ql_dbg(ql_dbg_tgt_mgt, tgt->vha, 0xf050,
1637 "qla_target(%d): Unable to create session "
1638 "work, command will be refused", 0);
1642 ql_dbg(ql_dbg_tgt_mgt, tgt->vha, 0xf00e,
1643 "Scheduling work (type %d, prm %p)"
1644 " to find session for param %p (size %d, tgt %p)\n",
1645 type, prm, param, param_size, tgt);
1648 memcpy(&prm->tm_iocb, param, param_size);
1650 spin_lock_irqsave(&tgt->sess_work_lock, flags);
1651 list_add_tail(&prm->sess_works_list_entry, &tgt->sess_works_list);
1652 spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
1654 schedule_work(&tgt->sess_work);
1660 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
1662 static void qlt_send_notify_ack(struct qla_qpair *qpair,
1663 struct imm_ntfy_from_isp *ntfy,
1664 uint32_t add_flags, uint16_t resp_code, int resp_code_valid,
1665 uint16_t srr_flags, uint16_t srr_reject_code, uint8_t srr_explan)
1667 struct scsi_qla_host *vha = qpair->vha;
1668 struct qla_hw_data *ha = vha->hw;
1670 struct nack_to_isp *nack;
1672 if (!ha->flags.fw_started)
1675 ql_dbg(ql_dbg_tgt, vha, 0xe004, "Sending NOTIFY_ACK (ha=%p)\n", ha);
1677 pkt = (request_t *)__qla2x00_alloc_iocbs(qpair, NULL);
1679 ql_dbg(ql_dbg_tgt, vha, 0xe049,
1680 "qla_target(%d): %s failed: unable to allocate "
1681 "request packet\n", vha->vp_idx, __func__);
1685 if (vha->vha_tgt.qla_tgt != NULL)
1686 vha->vha_tgt.qla_tgt->notify_ack_expected++;
1688 pkt->entry_type = NOTIFY_ACK_TYPE;
1689 pkt->entry_count = 1;
1691 nack = (struct nack_to_isp *)pkt;
1692 nack->ox_id = ntfy->ox_id;
1694 nack->u.isp24.handle = QLA_TGT_SKIP_HANDLE;
1695 nack->u.isp24.nport_handle = ntfy->u.isp24.nport_handle;
1696 if (le16_to_cpu(ntfy->u.isp24.status) == IMM_NTFY_ELS) {
1697 nack->u.isp24.flags = ntfy->u.isp24.flags &
1698 cpu_to_le32(NOTIFY24XX_FLAGS_PUREX_IOCB);
1700 nack->u.isp24.srr_rx_id = ntfy->u.isp24.srr_rx_id;
1701 nack->u.isp24.status = ntfy->u.isp24.status;
1702 nack->u.isp24.status_subcode = ntfy->u.isp24.status_subcode;
1703 nack->u.isp24.fw_handle = ntfy->u.isp24.fw_handle;
1704 nack->u.isp24.exchange_address = ntfy->u.isp24.exchange_address;
1705 nack->u.isp24.srr_rel_offs = ntfy->u.isp24.srr_rel_offs;
1706 nack->u.isp24.srr_ui = ntfy->u.isp24.srr_ui;
1707 nack->u.isp24.srr_flags = cpu_to_le16(srr_flags);
1708 nack->u.isp24.srr_reject_code = srr_reject_code;
1709 nack->u.isp24.srr_reject_code_expl = srr_explan;
1710 nack->u.isp24.vp_index = ntfy->u.isp24.vp_index;
1712 ql_dbg(ql_dbg_tgt, vha, 0xe005,
1713 "qla_target(%d): Sending 24xx Notify Ack %d\n",
1714 vha->vp_idx, nack->u.isp24.status);
1716 /* Memory Barrier */
1718 qla2x00_start_iocbs(vha, qpair->req);
1721 static int qlt_build_abts_resp_iocb(struct qla_tgt_mgmt_cmd *mcmd)
1723 struct scsi_qla_host *vha = mcmd->vha;
1724 struct qla_hw_data *ha = vha->hw;
1725 struct abts_resp_to_24xx *resp;
1729 struct abts_recv_from_24xx *abts = &mcmd->orig_iocb.abts;
1730 struct qla_qpair *qpair = mcmd->qpair;
1732 ql_dbg(ql_dbg_tgt, vha, 0xe006,
1733 "Sending task mgmt ABTS response (ha=%p, status=%x)\n",
1734 ha, mcmd->fc_tm_rsp);
1736 rc = qlt_check_reserve_free_req(qpair, 1);
1738 ql_dbg(ql_dbg_tgt, vha, 0xe04a,
1739 "qla_target(%d): %s failed: unable to allocate request packet\n",
1740 vha->vp_idx, __func__);
1744 resp = (struct abts_resp_to_24xx *)qpair->req->ring_ptr;
1745 memset(resp, 0, sizeof(*resp));
1747 h = qlt_make_handle(qpair);
1748 if (unlikely(h == QLA_TGT_NULL_HANDLE)) {
1750 * CTIO type 7 from the firmware doesn't provide a way to
1751 * know the initiator's LOOP ID, hence we can't find
1752 * the session and, so, the command.
1756 qpair->req->outstanding_cmds[h] = (srb_t *)mcmd;
1759 resp->handle = MAKE_HANDLE(qpair->req->id, h);
1760 resp->entry_type = ABTS_RESP_24XX;
1761 resp->entry_count = 1;
1762 resp->nport_handle = abts->nport_handle;
1763 resp->vp_index = vha->vp_idx;
1764 resp->sof_type = abts->sof_type;
1765 resp->exchange_address = abts->exchange_address;
1766 resp->fcp_hdr_le = abts->fcp_hdr_le;
1767 f_ctl = cpu_to_le32(F_CTL_EXCH_CONTEXT_RESP |
1768 F_CTL_LAST_SEQ | F_CTL_END_SEQ |
1769 F_CTL_SEQ_INITIATIVE);
1770 p = (uint8_t *)&f_ctl;
1771 resp->fcp_hdr_le.f_ctl[0] = *p++;
1772 resp->fcp_hdr_le.f_ctl[1] = *p++;
1773 resp->fcp_hdr_le.f_ctl[2] = *p;
1775 resp->fcp_hdr_le.d_id[0] = abts->fcp_hdr_le.s_id[0];
1776 resp->fcp_hdr_le.d_id[1] = abts->fcp_hdr_le.s_id[1];
1777 resp->fcp_hdr_le.d_id[2] = abts->fcp_hdr_le.s_id[2];
1778 resp->fcp_hdr_le.s_id[0] = abts->fcp_hdr_le.d_id[0];
1779 resp->fcp_hdr_le.s_id[1] = abts->fcp_hdr_le.d_id[1];
1780 resp->fcp_hdr_le.s_id[2] = abts->fcp_hdr_le.d_id[2];
1782 resp->exchange_addr_to_abort = abts->exchange_addr_to_abort;
1783 if (mcmd->fc_tm_rsp == FCP_TMF_CMPL) {
1784 resp->fcp_hdr_le.r_ctl = R_CTL_BASIC_LINK_SERV | R_CTL_B_ACC;
1785 resp->payload.ba_acct.seq_id_valid = SEQ_ID_INVALID;
1786 resp->payload.ba_acct.low_seq_cnt = 0x0000;
1787 resp->payload.ba_acct.high_seq_cnt = 0xFFFF;
1788 resp->payload.ba_acct.ox_id = abts->fcp_hdr_le.ox_id;
1789 resp->payload.ba_acct.rx_id = abts->fcp_hdr_le.rx_id;
1791 resp->fcp_hdr_le.r_ctl = R_CTL_BASIC_LINK_SERV | R_CTL_B_RJT;
1792 resp->payload.ba_rjt.reason_code =
1793 BA_RJT_REASON_CODE_UNABLE_TO_PERFORM;
1794 /* Other bytes are zero */
1797 vha->vha_tgt.qla_tgt->abts_resp_expected++;
1799 /* Memory Barrier */
1801 if (qpair->reqq_start_iocbs)
1802 qpair->reqq_start_iocbs(qpair);
1804 qla2x00_start_iocbs(vha, qpair->req);
1810 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
1812 static void qlt_24xx_send_abts_resp(struct qla_qpair *qpair,
1813 struct abts_recv_from_24xx *abts, uint32_t status,
1816 struct scsi_qla_host *vha = qpair->vha;
1817 struct qla_hw_data *ha = vha->hw;
1818 struct abts_resp_to_24xx *resp;
1822 ql_dbg(ql_dbg_tgt, vha, 0xe006,
1823 "Sending task mgmt ABTS response (ha=%p, atio=%p, status=%x\n",
1826 resp = (struct abts_resp_to_24xx *)qla2x00_alloc_iocbs_ready(qpair,
1829 ql_dbg(ql_dbg_tgt, vha, 0xe04a,
1830 "qla_target(%d): %s failed: unable to allocate "
1831 "request packet", vha->vp_idx, __func__);
1835 resp->entry_type = ABTS_RESP_24XX;
1836 resp->handle = QLA_TGT_SKIP_HANDLE;
1837 resp->entry_count = 1;
1838 resp->nport_handle = abts->nport_handle;
1839 resp->vp_index = vha->vp_idx;
1840 resp->sof_type = abts->sof_type;
1841 resp->exchange_address = abts->exchange_address;
1842 resp->fcp_hdr_le = abts->fcp_hdr_le;
1843 f_ctl = cpu_to_le32(F_CTL_EXCH_CONTEXT_RESP |
1844 F_CTL_LAST_SEQ | F_CTL_END_SEQ |
1845 F_CTL_SEQ_INITIATIVE);
1846 p = (uint8_t *)&f_ctl;
1847 resp->fcp_hdr_le.f_ctl[0] = *p++;
1848 resp->fcp_hdr_le.f_ctl[1] = *p++;
1849 resp->fcp_hdr_le.f_ctl[2] = *p;
1851 resp->fcp_hdr_le.d_id[0] = abts->fcp_hdr_le.d_id[0];
1852 resp->fcp_hdr_le.d_id[1] = abts->fcp_hdr_le.d_id[1];
1853 resp->fcp_hdr_le.d_id[2] = abts->fcp_hdr_le.d_id[2];
1854 resp->fcp_hdr_le.s_id[0] = abts->fcp_hdr_le.s_id[0];
1855 resp->fcp_hdr_le.s_id[1] = abts->fcp_hdr_le.s_id[1];
1856 resp->fcp_hdr_le.s_id[2] = abts->fcp_hdr_le.s_id[2];
1858 resp->fcp_hdr_le.d_id[0] = abts->fcp_hdr_le.s_id[0];
1859 resp->fcp_hdr_le.d_id[1] = abts->fcp_hdr_le.s_id[1];
1860 resp->fcp_hdr_le.d_id[2] = abts->fcp_hdr_le.s_id[2];
1861 resp->fcp_hdr_le.s_id[0] = abts->fcp_hdr_le.d_id[0];
1862 resp->fcp_hdr_le.s_id[1] = abts->fcp_hdr_le.d_id[1];
1863 resp->fcp_hdr_le.s_id[2] = abts->fcp_hdr_le.d_id[2];
1865 resp->exchange_addr_to_abort = abts->exchange_addr_to_abort;
1866 if (status == FCP_TMF_CMPL) {
1867 resp->fcp_hdr_le.r_ctl = R_CTL_BASIC_LINK_SERV | R_CTL_B_ACC;
1868 resp->payload.ba_acct.seq_id_valid = SEQ_ID_INVALID;
1869 resp->payload.ba_acct.low_seq_cnt = 0x0000;
1870 resp->payload.ba_acct.high_seq_cnt = 0xFFFF;
1871 resp->payload.ba_acct.ox_id = abts->fcp_hdr_le.ox_id;
1872 resp->payload.ba_acct.rx_id = abts->fcp_hdr_le.rx_id;
1874 resp->fcp_hdr_le.r_ctl = R_CTL_BASIC_LINK_SERV | R_CTL_B_RJT;
1875 resp->payload.ba_rjt.reason_code =
1876 BA_RJT_REASON_CODE_UNABLE_TO_PERFORM;
1877 /* Other bytes are zero */
1880 vha->vha_tgt.qla_tgt->abts_resp_expected++;
1882 /* Memory Barrier */
1884 if (qpair->reqq_start_iocbs)
1885 qpair->reqq_start_iocbs(qpair);
1887 qla2x00_start_iocbs(vha, qpair->req);
1891 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
1893 static void qlt_24xx_retry_term_exchange(struct scsi_qla_host *vha,
1894 struct qla_qpair *qpair, response_t *pkt, struct qla_tgt_mgmt_cmd *mcmd)
1896 struct ctio7_to_24xx *ctio;
1898 struct abts_recv_from_24xx *entry;
1900 ctio = (struct ctio7_to_24xx *)qla2x00_alloc_iocbs_ready(qpair, NULL);
1902 ql_dbg(ql_dbg_tgt, vha, 0xe04b,
1903 "qla_target(%d): %s failed: unable to allocate "
1904 "request packet\n", vha->vp_idx, __func__);
1909 /* abts from remote port */
1910 entry = &mcmd->orig_iocb.abts;
1912 /* abts from this driver. */
1913 entry = (struct abts_recv_from_24xx *)pkt;
1916 * We've got on entrance firmware's response on by us generated
1917 * ABTS response. So, in it ID fields are reversed.
1920 ctio->entry_type = CTIO_TYPE7;
1921 ctio->entry_count = 1;
1922 ctio->nport_handle = entry->nport_handle;
1923 ctio->handle = QLA_TGT_SKIP_HANDLE | CTIO_COMPLETION_HANDLE_MARK;
1924 ctio->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
1925 ctio->vp_index = vha->vp_idx;
1926 ctio->exchange_addr = entry->exchange_addr_to_abort;
1927 tmp = (CTIO7_FLAGS_STATUS_MODE_1 | CTIO7_FLAGS_TERMINATE);
1930 ctio->initiator_id[0] = entry->fcp_hdr_le.s_id[0];
1931 ctio->initiator_id[1] = entry->fcp_hdr_le.s_id[1];
1932 ctio->initiator_id[2] = entry->fcp_hdr_le.s_id[2];
1934 if (mcmd->flags & QLA24XX_MGMT_ABORT_IO_ATTR_VALID)
1935 tmp |= (mcmd->abort_io_attr << 9);
1936 else if (qpair->retry_term_cnt & 1)
1939 ctio->initiator_id[0] = entry->fcp_hdr_le.d_id[0];
1940 ctio->initiator_id[1] = entry->fcp_hdr_le.d_id[1];
1941 ctio->initiator_id[2] = entry->fcp_hdr_le.d_id[2];
1943 if (qpair->retry_term_cnt & 1)
1946 ctio->u.status1.flags = cpu_to_le16(tmp);
1947 ctio->u.status1.ox_id = entry->fcp_hdr_le.ox_id;
1949 ql_dbg(ql_dbg_tgt, vha, 0xe007,
1950 "Sending retry TERM EXCH CTIO7 flags %04xh oxid %04xh attr valid %x\n",
1951 le16_to_cpu(ctio->u.status1.flags),
1952 le16_to_cpu(ctio->u.status1.ox_id),
1953 (mcmd && mcmd->flags & QLA24XX_MGMT_ABORT_IO_ATTR_VALID) ? 1 : 0);
1955 /* Memory Barrier */
1957 if (qpair->reqq_start_iocbs)
1958 qpair->reqq_start_iocbs(qpair);
1960 qla2x00_start_iocbs(vha, qpair->req);
1963 qlt_build_abts_resp_iocb(mcmd);
1965 qlt_24xx_send_abts_resp(qpair,
1966 (struct abts_recv_from_24xx *)entry, FCP_TMF_CMPL, true);
1970 /* drop cmds for the given lun
1971 * XXX only looks for cmds on the port through which lun reset was recieved
1972 * XXX does not go through the list of other port (which may have cmds
1975 static void abort_cmds_for_lun(struct scsi_qla_host *vha,
1976 u64 lun, uint8_t *s_id)
1978 struct qla_tgt_sess_op *op;
1979 struct qla_tgt_cmd *cmd;
1981 unsigned long flags;
1983 key = sid_to_key(s_id);
1984 spin_lock_irqsave(&vha->cmd_list_lock, flags);
1985 list_for_each_entry(op, &vha->qla_sess_op_cmd_list, cmd_list) {
1989 op_key = sid_to_key(op->atio.u.isp24.fcp_hdr.s_id);
1990 op_lun = scsilun_to_int(
1991 (struct scsi_lun *)&op->atio.u.isp24.fcp_cmnd.lun);
1992 if (op_key == key && op_lun == lun)
1996 list_for_each_entry(op, &vha->unknown_atio_list, cmd_list) {
2000 op_key = sid_to_key(op->atio.u.isp24.fcp_hdr.s_id);
2001 op_lun = scsilun_to_int(
2002 (struct scsi_lun *)&op->atio.u.isp24.fcp_cmnd.lun);
2003 if (op_key == key && op_lun == lun)
2007 list_for_each_entry(cmd, &vha->qla_cmd_list, cmd_list) {
2011 cmd_key = sid_to_key(cmd->atio.u.isp24.fcp_hdr.s_id);
2012 cmd_lun = scsilun_to_int(
2013 (struct scsi_lun *)&cmd->atio.u.isp24.fcp_cmnd.lun);
2014 if (cmd_key == key && cmd_lun == lun)
2017 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
2020 static struct qla_qpair_hint *qlt_find_qphint(struct scsi_qla_host *vha,
2021 uint64_t unpacked_lun)
2023 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
2024 struct qla_qpair_hint *h = NULL;
2026 if (vha->flags.qpairs_available) {
2027 h = btree_lookup64(&tgt->lun_qpair_map, unpacked_lun);
2029 h = &tgt->qphints[0];
2031 h = &tgt->qphints[0];
2037 static void qlt_do_tmr_work(struct work_struct *work)
2039 struct qla_tgt_mgmt_cmd *mcmd =
2040 container_of(work, struct qla_tgt_mgmt_cmd, work);
2041 struct qla_hw_data *ha = mcmd->vha->hw;
2044 unsigned long flags;
2046 switch (mcmd->tmr_func) {
2048 tag = mcmd->orig_iocb.abts.exchange_addr_to_abort;
2055 rc = ha->tgt.tgt_ops->handle_tmr(mcmd, mcmd->unpacked_lun,
2056 mcmd->tmr_func, tag);
2059 spin_lock_irqsave(mcmd->qpair->qp_lock_ptr, flags);
2060 switch (mcmd->tmr_func) {
2062 mcmd->fc_tm_rsp = FCP_TMF_REJECTED;
2063 qlt_build_abts_resp_iocb(mcmd);
2065 case QLA_TGT_LUN_RESET:
2066 case QLA_TGT_CLEAR_TS:
2067 case QLA_TGT_ABORT_TS:
2068 case QLA_TGT_CLEAR_ACA:
2069 case QLA_TGT_TARGET_RESET:
2070 qlt_send_busy(mcmd->qpair, &mcmd->orig_iocb.atio,
2074 case QLA_TGT_ABORT_ALL:
2075 case QLA_TGT_NEXUS_LOSS_SESS:
2076 case QLA_TGT_NEXUS_LOSS:
2077 qlt_send_notify_ack(mcmd->qpair,
2078 &mcmd->orig_iocb.imm_ntfy, 0, 0, 0, 0, 0, 0);
2081 spin_unlock_irqrestore(mcmd->qpair->qp_lock_ptr, flags);
2083 ql_dbg(ql_dbg_tgt_mgt, mcmd->vha, 0xf052,
2084 "qla_target(%d): tgt_ops->handle_tmr() failed: %d\n",
2085 mcmd->vha->vp_idx, rc);
2086 mempool_free(mcmd, qla_tgt_mgmt_cmd_mempool);
2090 /* ha->hardware_lock supposed to be held on entry */
2091 static int __qlt_24xx_handle_abts(struct scsi_qla_host *vha,
2092 struct abts_recv_from_24xx *abts, struct fc_port *sess)
2094 struct qla_hw_data *ha = vha->hw;
2095 struct qla_tgt_mgmt_cmd *mcmd;
2096 struct qla_qpair_hint *h = &vha->vha_tgt.qla_tgt->qphints[0];
2098 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00f,
2099 "qla_target(%d): task abort (tag=%d)\n",
2100 vha->vp_idx, abts->exchange_addr_to_abort);
2102 mcmd = mempool_alloc(qla_tgt_mgmt_cmd_mempool, GFP_ATOMIC);
2104 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf051,
2105 "qla_target(%d): %s: Allocation of ABORT cmd failed",
2106 vha->vp_idx, __func__);
2109 memset(mcmd, 0, sizeof(*mcmd));
2110 mcmd->cmd_type = TYPE_TGT_TMCMD;
2112 memcpy(&mcmd->orig_iocb.abts, abts, sizeof(mcmd->orig_iocb.abts));
2113 mcmd->reset_count = ha->base_qpair->chip_reset;
2114 mcmd->tmr_func = QLA_TGT_ABTS;
2115 mcmd->qpair = h->qpair;
2119 * LUN is looked up by target-core internally based on the passed
2120 * abts->exchange_addr_to_abort tag.
2122 mcmd->se_cmd.cpuid = h->cpuid;
2124 if (ha->tgt.tgt_ops->find_cmd_by_tag) {
2125 struct qla_tgt_cmd *abort_cmd;
2127 abort_cmd = ha->tgt.tgt_ops->find_cmd_by_tag(sess,
2128 abts->exchange_addr_to_abort);
2129 if (abort_cmd && abort_cmd->qpair) {
2130 mcmd->qpair = abort_cmd->qpair;
2131 mcmd->se_cmd.cpuid = abort_cmd->se_cmd.cpuid;
2132 mcmd->abort_io_attr = abort_cmd->atio.u.isp24.attr;
2133 mcmd->flags = QLA24XX_MGMT_ABORT_IO_ATTR_VALID;
2137 INIT_WORK(&mcmd->work, qlt_do_tmr_work);
2138 queue_work_on(mcmd->se_cmd.cpuid, qla_tgt_wq, &mcmd->work);
2144 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
2146 static void qlt_24xx_handle_abts(struct scsi_qla_host *vha,
2147 struct abts_recv_from_24xx *abts)
2149 struct qla_hw_data *ha = vha->hw;
2150 struct fc_port *sess;
2151 uint32_t tag = abts->exchange_addr_to_abort;
2154 unsigned long flags;
2156 if (le32_to_cpu(abts->fcp_hdr_le.parameter) & ABTS_PARAM_ABORT_SEQ) {
2157 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf053,
2158 "qla_target(%d): ABTS: Abort Sequence not "
2159 "supported\n", vha->vp_idx);
2160 qlt_24xx_send_abts_resp(ha->base_qpair, abts, FCP_TMF_REJECTED,
2165 if (tag == ATIO_EXCHANGE_ADDRESS_UNKNOWN) {
2166 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf010,
2167 "qla_target(%d): ABTS: Unknown Exchange "
2168 "Address received\n", vha->vp_idx);
2169 qlt_24xx_send_abts_resp(ha->base_qpair, abts, FCP_TMF_REJECTED,
2174 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf011,
2175 "qla_target(%d): task abort (s_id=%x:%x:%x, "
2176 "tag=%d, param=%x)\n", vha->vp_idx, abts->fcp_hdr_le.s_id[2],
2177 abts->fcp_hdr_le.s_id[1], abts->fcp_hdr_le.s_id[0], tag,
2178 le32_to_cpu(abts->fcp_hdr_le.parameter));
2180 s_id[0] = abts->fcp_hdr_le.s_id[2];
2181 s_id[1] = abts->fcp_hdr_le.s_id[1];
2182 s_id[2] = abts->fcp_hdr_le.s_id[0];
2184 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
2185 sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha, s_id);
2187 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf012,
2188 "qla_target(%d): task abort for non-existent session\n",
2190 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
2192 qlt_24xx_send_abts_resp(ha->base_qpair, abts, FCP_TMF_REJECTED,
2196 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
2199 if (sess->deleted) {
2200 qlt_24xx_send_abts_resp(ha->base_qpair, abts, FCP_TMF_REJECTED,
2205 rc = __qlt_24xx_handle_abts(vha, abts, sess);
2207 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf054,
2208 "qla_target(%d): __qlt_24xx_handle_abts() failed: %d\n",
2210 qlt_24xx_send_abts_resp(ha->base_qpair, abts, FCP_TMF_REJECTED,
2217 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
2219 static void qlt_24xx_send_task_mgmt_ctio(struct qla_qpair *qpair,
2220 struct qla_tgt_mgmt_cmd *mcmd, uint32_t resp_code)
2222 struct scsi_qla_host *ha = mcmd->vha;
2223 struct atio_from_isp *atio = &mcmd->orig_iocb.atio;
2224 struct ctio7_to_24xx *ctio;
2227 ql_dbg(ql_dbg_tgt, ha, 0xe008,
2228 "Sending task mgmt CTIO7 (ha=%p, atio=%p, resp_code=%x\n",
2229 ha, atio, resp_code);
2232 ctio = (struct ctio7_to_24xx *)__qla2x00_alloc_iocbs(qpair, NULL);
2234 ql_dbg(ql_dbg_tgt, ha, 0xe04c,
2235 "qla_target(%d): %s failed: unable to allocate "
2236 "request packet\n", ha->vp_idx, __func__);
2240 ctio->entry_type = CTIO_TYPE7;
2241 ctio->entry_count = 1;
2242 ctio->handle = QLA_TGT_SKIP_HANDLE | CTIO_COMPLETION_HANDLE_MARK;
2243 ctio->nport_handle = mcmd->sess->loop_id;
2244 ctio->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
2245 ctio->vp_index = ha->vp_idx;
2246 ctio->initiator_id[0] = atio->u.isp24.fcp_hdr.s_id[2];
2247 ctio->initiator_id[1] = atio->u.isp24.fcp_hdr.s_id[1];
2248 ctio->initiator_id[2] = atio->u.isp24.fcp_hdr.s_id[0];
2249 ctio->exchange_addr = atio->u.isp24.exchange_addr;
2250 temp = (atio->u.isp24.attr << 9)|
2251 CTIO7_FLAGS_STATUS_MODE_1 | CTIO7_FLAGS_SEND_STATUS;
2252 ctio->u.status1.flags = cpu_to_le16(temp);
2253 temp = be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id);
2254 ctio->u.status1.ox_id = cpu_to_le16(temp);
2255 ctio->u.status1.scsi_status =
2256 cpu_to_le16(SS_RESPONSE_INFO_LEN_VALID);
2257 ctio->u.status1.response_len = cpu_to_le16(8);
2258 ctio->u.status1.sense_data[0] = resp_code;
2260 /* Memory Barrier */
2262 if (qpair->reqq_start_iocbs)
2263 qpair->reqq_start_iocbs(qpair);
2265 qla2x00_start_iocbs(ha, qpair->req);
2268 void qlt_free_mcmd(struct qla_tgt_mgmt_cmd *mcmd)
2270 mempool_free(mcmd, qla_tgt_mgmt_cmd_mempool);
2272 EXPORT_SYMBOL(qlt_free_mcmd);
2275 * ha->hardware_lock supposed to be held on entry. Might drop it, then
2278 void qlt_send_resp_ctio(struct qla_qpair *qpair, struct qla_tgt_cmd *cmd,
2279 uint8_t scsi_status, uint8_t sense_key, uint8_t asc, uint8_t ascq)
2281 struct atio_from_isp *atio = &cmd->atio;
2282 struct ctio7_to_24xx *ctio;
2284 struct scsi_qla_host *vha = cmd->vha;
2286 ql_dbg(ql_dbg_tgt_dif, vha, 0x3066,
2287 "Sending response CTIO7 (vha=%p, atio=%p, scsi_status=%02x, "
2288 "sense_key=%02x, asc=%02x, ascq=%02x",
2289 vha, atio, scsi_status, sense_key, asc, ascq);
2291 ctio = (struct ctio7_to_24xx *)qla2x00_alloc_iocbs(vha, NULL);
2293 ql_dbg(ql_dbg_async, vha, 0x3067,
2294 "qla2x00t(%ld): %s failed: unable to allocate request packet",
2295 vha->host_no, __func__);
2299 ctio->entry_type = CTIO_TYPE7;
2300 ctio->entry_count = 1;
2301 ctio->handle = QLA_TGT_SKIP_HANDLE;
2302 ctio->nport_handle = cmd->sess->loop_id;
2303 ctio->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
2304 ctio->vp_index = vha->vp_idx;
2305 ctio->initiator_id[0] = atio->u.isp24.fcp_hdr.s_id[2];
2306 ctio->initiator_id[1] = atio->u.isp24.fcp_hdr.s_id[1];
2307 ctio->initiator_id[2] = atio->u.isp24.fcp_hdr.s_id[0];
2308 ctio->exchange_addr = atio->u.isp24.exchange_addr;
2309 temp = (atio->u.isp24.attr << 9) |
2310 CTIO7_FLAGS_STATUS_MODE_1 | CTIO7_FLAGS_SEND_STATUS;
2311 ctio->u.status1.flags = cpu_to_le16(temp);
2312 temp = be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id);
2313 ctio->u.status1.ox_id = cpu_to_le16(temp);
2314 ctio->u.status1.scsi_status =
2315 cpu_to_le16(SS_RESPONSE_INFO_LEN_VALID | scsi_status);
2316 ctio->u.status1.response_len = cpu_to_le16(18);
2317 ctio->u.status1.residual = cpu_to_le32(get_datalen_for_atio(atio));
2319 if (ctio->u.status1.residual != 0)
2320 ctio->u.status1.scsi_status |=
2321 cpu_to_le16(SS_RESIDUAL_UNDER);
2323 /* Fixed format sense data. */
2324 ctio->u.status1.sense_data[0] = 0x70;
2325 ctio->u.status1.sense_data[2] = sense_key;
2326 /* Additional sense length */
2327 ctio->u.status1.sense_data[7] = 0xa;
2329 ctio->u.status1.sense_data[12] = asc;
2330 ctio->u.status1.sense_data[13] = ascq;
2332 /* Memory Barrier */
2335 if (qpair->reqq_start_iocbs)
2336 qpair->reqq_start_iocbs(qpair);
2338 qla2x00_start_iocbs(vha, qpair->req);
2344 /* callback from target fabric module code */
2345 void qlt_xmit_tm_rsp(struct qla_tgt_mgmt_cmd *mcmd)
2347 struct scsi_qla_host *vha = mcmd->sess->vha;
2348 struct qla_hw_data *ha = vha->hw;
2349 unsigned long flags;
2350 struct qla_qpair *qpair = mcmd->qpair;
2351 bool free_mcmd = true;
2353 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf013,
2354 "TM response mcmd (%p) status %#x state %#x",
2355 mcmd, mcmd->fc_tm_rsp, mcmd->flags);
2357 spin_lock_irqsave(qpair->qp_lock_ptr, flags);
2359 if (!vha->flags.online || mcmd->reset_count != qpair->chip_reset) {
2361 * Either the port is not online or this request was from
2362 * previous life, just abort the processing.
2364 ql_dbg(ql_dbg_async, vha, 0xe100,
2365 "RESET-TMR online/active/old-count/new-count = %d/%d/%d/%d.\n",
2366 vha->flags.online, qla2x00_reset_active(vha),
2367 mcmd->reset_count, qpair->chip_reset);
2368 ha->tgt.tgt_ops->free_mcmd(mcmd);
2369 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
2373 if (mcmd->flags == QLA24XX_MGMT_SEND_NACK) {
2374 switch (mcmd->orig_iocb.imm_ntfy.u.isp24.status_subcode) {
2378 ql_dbg(ql_dbg_disc, vha, 0x2106,
2379 "TM response logo %8phC status %#x state %#x",
2380 mcmd->sess->port_name, mcmd->fc_tm_rsp,
2382 qlt_schedule_sess_for_deletion(mcmd->sess);
2385 qlt_send_notify_ack(vha->hw->base_qpair,
2386 &mcmd->orig_iocb.imm_ntfy, 0, 0, 0, 0, 0, 0);
2390 if (mcmd->orig_iocb.atio.u.raw.entry_type == ABTS_RECV_24XX) {
2391 qlt_build_abts_resp_iocb(mcmd);
2394 qlt_24xx_send_task_mgmt_ctio(qpair, mcmd,
2398 * Make the callback for ->free_mcmd() to queue_work() and invoke
2399 * target_put_sess_cmd() to drop cmd_kref to 1. The final
2400 * target_put_sess_cmd() call will be made from TFO->check_stop_free()
2401 * -> tcm_qla2xxx_check_stop_free() to release the TMR associated se_cmd
2402 * descriptor after TFO->queue_tm_rsp() -> tcm_qla2xxx_queue_tm_rsp() ->
2403 * qlt_xmit_tm_rsp() returns here..
2406 ha->tgt.tgt_ops->free_mcmd(mcmd);
2408 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
2410 EXPORT_SYMBOL(qlt_xmit_tm_rsp);
2413 static int qlt_pci_map_calc_cnt(struct qla_tgt_prm *prm)
2415 struct qla_tgt_cmd *cmd = prm->cmd;
2417 BUG_ON(cmd->sg_cnt == 0);
2419 prm->sg = (struct scatterlist *)cmd->sg;
2420 prm->seg_cnt = dma_map_sg(&cmd->qpair->pdev->dev, cmd->sg,
2421 cmd->sg_cnt, cmd->dma_data_direction);
2422 if (unlikely(prm->seg_cnt == 0))
2425 prm->cmd->sg_mapped = 1;
2427 if (cmd->se_cmd.prot_op == TARGET_PROT_NORMAL) {
2429 * If greater than four sg entries then we need to allocate
2430 * the continuation entries
2432 if (prm->seg_cnt > QLA_TGT_DATASEGS_PER_CMD_24XX)
2433 prm->req_cnt += DIV_ROUND_UP(prm->seg_cnt -
2434 QLA_TGT_DATASEGS_PER_CMD_24XX,
2435 QLA_TGT_DATASEGS_PER_CONT_24XX);
2438 if ((cmd->se_cmd.prot_op == TARGET_PROT_DIN_INSERT) ||
2439 (cmd->se_cmd.prot_op == TARGET_PROT_DOUT_STRIP)) {
2440 prm->seg_cnt = DIV_ROUND_UP(cmd->bufflen, cmd->blk_sz);
2441 prm->tot_dsds = prm->seg_cnt;
2443 prm->tot_dsds = prm->seg_cnt;
2445 if (cmd->prot_sg_cnt) {
2446 prm->prot_sg = cmd->prot_sg;
2447 prm->prot_seg_cnt = dma_map_sg(&cmd->qpair->pdev->dev,
2448 cmd->prot_sg, cmd->prot_sg_cnt,
2449 cmd->dma_data_direction);
2450 if (unlikely(prm->prot_seg_cnt == 0))
2453 if ((cmd->se_cmd.prot_op == TARGET_PROT_DIN_INSERT) ||
2454 (cmd->se_cmd.prot_op == TARGET_PROT_DOUT_STRIP)) {
2455 /* Dif Bundling not support here */
2456 prm->prot_seg_cnt = DIV_ROUND_UP(cmd->bufflen,
2458 prm->tot_dsds += prm->prot_seg_cnt;
2460 prm->tot_dsds += prm->prot_seg_cnt;
2467 ql_dbg_qp(ql_dbg_tgt, prm->cmd->qpair, 0xe04d,
2468 "qla_target(%d): PCI mapping failed: sg_cnt=%d",
2469 0, prm->cmd->sg_cnt);
2473 static void qlt_unmap_sg(struct scsi_qla_host *vha, struct qla_tgt_cmd *cmd)
2475 struct qla_hw_data *ha;
2476 struct qla_qpair *qpair;
2478 if (!cmd->sg_mapped)
2483 dma_unmap_sg(&qpair->pdev->dev, cmd->sg, cmd->sg_cnt,
2484 cmd->dma_data_direction);
2487 if (cmd->prot_sg_cnt)
2488 dma_unmap_sg(&qpair->pdev->dev, cmd->prot_sg, cmd->prot_sg_cnt,
2489 cmd->dma_data_direction);
2494 if (cmd->ctx_dsd_alloced)
2495 qla2x00_clean_dsd_pool(ha, cmd->ctx);
2497 dma_pool_free(ha->dl_dma_pool, cmd->ctx, cmd->ctx->crc_ctx_dma);
2500 static int qlt_check_reserve_free_req(struct qla_qpair *qpair,
2504 struct req_que *req = qpair->req;
2506 if (req->cnt < (req_cnt + 2)) {
2507 cnt = (uint16_t)(qpair->use_shadow_reg ? *req->out_ptr :
2508 RD_REG_DWORD_RELAXED(req->req_q_out));
2510 if (req->ring_index < cnt)
2511 req->cnt = cnt - req->ring_index;
2513 req->cnt = req->length - (req->ring_index - cnt);
2515 if (unlikely(req->cnt < (req_cnt + 2)))
2519 req->cnt -= req_cnt;
2525 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
2527 static inline void *qlt_get_req_pkt(struct req_que *req)
2529 /* Adjust ring index. */
2531 if (req->ring_index == req->length) {
2532 req->ring_index = 0;
2533 req->ring_ptr = req->ring;
2537 return (cont_entry_t *)req->ring_ptr;
2540 /* ha->hardware_lock supposed to be held on entry */
2541 static inline uint32_t qlt_make_handle(struct qla_qpair *qpair)
2546 struct req_que *req = qpair->req;
2548 h = req->current_outstanding_cmd;
2550 for (index = 1; index < req->num_outstanding_cmds; index++) {
2552 if (h == req->num_outstanding_cmds)
2555 if (h == QLA_TGT_SKIP_HANDLE)
2558 if (!req->outstanding_cmds[h]) {
2565 req->current_outstanding_cmd = h;
2567 ql_dbg(ql_dbg_io, qpair->vha, 0x305b,
2568 "qla_target(%d): Ran out of empty cmd slots\n",
2569 qpair->vha->vp_idx);
2570 h = QLA_TGT_NULL_HANDLE;
2576 /* ha->hardware_lock supposed to be held on entry */
2577 static int qlt_24xx_build_ctio_pkt(struct qla_qpair *qpair,
2578 struct qla_tgt_prm *prm)
2581 struct ctio7_to_24xx *pkt;
2582 struct atio_from_isp *atio = &prm->cmd->atio;
2585 pkt = (struct ctio7_to_24xx *)qpair->req->ring_ptr;
2587 memset(pkt, 0, sizeof(*pkt));
2589 pkt->entry_type = CTIO_TYPE7;
2590 pkt->entry_count = (uint8_t)prm->req_cnt;
2591 pkt->vp_index = prm->cmd->vp_idx;
2593 h = qlt_make_handle(qpair);
2594 if (unlikely(h == QLA_TGT_NULL_HANDLE)) {
2596 * CTIO type 7 from the firmware doesn't provide a way to
2597 * know the initiator's LOOP ID, hence we can't find
2598 * the session and, so, the command.
2602 qpair->req->outstanding_cmds[h] = (srb_t *)prm->cmd;
2604 pkt->handle = MAKE_HANDLE(qpair->req->id, h);
2605 pkt->handle |= CTIO_COMPLETION_HANDLE_MARK;
2606 pkt->nport_handle = cpu_to_le16(prm->cmd->loop_id);
2607 pkt->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
2608 pkt->initiator_id[0] = atio->u.isp24.fcp_hdr.s_id[2];
2609 pkt->initiator_id[1] = atio->u.isp24.fcp_hdr.s_id[1];
2610 pkt->initiator_id[2] = atio->u.isp24.fcp_hdr.s_id[0];
2611 pkt->exchange_addr = atio->u.isp24.exchange_addr;
2612 temp = atio->u.isp24.attr << 9;
2613 pkt->u.status0.flags |= cpu_to_le16(temp);
2614 temp = be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id);
2615 pkt->u.status0.ox_id = cpu_to_le16(temp);
2616 pkt->u.status0.relative_offset = cpu_to_le32(prm->cmd->offset);
2622 * ha->hardware_lock supposed to be held on entry. We have already made sure
2623 * that there is sufficient amount of request entries to not drop it.
2625 static void qlt_load_cont_data_segments(struct qla_tgt_prm *prm)
2628 struct dsd64 *cur_dsd;
2630 /* Build continuation packets */
2631 while (prm->seg_cnt > 0) {
2632 cont_a64_entry_t *cont_pkt64 =
2633 (cont_a64_entry_t *)qlt_get_req_pkt(
2634 prm->cmd->qpair->req);
2637 * Make sure that from cont_pkt64 none of
2638 * 64-bit specific fields used for 32-bit
2639 * addressing. Cast to (cont_entry_t *) for
2643 memset(cont_pkt64, 0, sizeof(*cont_pkt64));
2645 cont_pkt64->entry_count = 1;
2646 cont_pkt64->sys_define = 0;
2648 cont_pkt64->entry_type = CONTINUE_A64_TYPE;
2649 cur_dsd = cont_pkt64->dsd;
2651 /* Load continuation entry data segments */
2653 cnt < QLA_TGT_DATASEGS_PER_CONT_24XX && prm->seg_cnt;
2654 cnt++, prm->seg_cnt--) {
2655 append_dsd64(&cur_dsd, prm->sg);
2656 prm->sg = sg_next(prm->sg);
2662 * ha->hardware_lock supposed to be held on entry. We have already made sure
2663 * that there is sufficient amount of request entries to not drop it.
2665 static void qlt_load_data_segments(struct qla_tgt_prm *prm)
2668 struct dsd64 *cur_dsd;
2669 struct ctio7_to_24xx *pkt24 = (struct ctio7_to_24xx *)prm->pkt;
2671 pkt24->u.status0.transfer_length = cpu_to_le32(prm->cmd->bufflen);
2673 /* Setup packet address segment pointer */
2674 cur_dsd = &pkt24->u.status0.dsd;
2676 /* Set total data segment count */
2678 pkt24->dseg_count = cpu_to_le16(prm->seg_cnt);
2680 if (prm->seg_cnt == 0) {
2681 /* No data transfer */
2682 cur_dsd->address = 0;
2683 cur_dsd->length = 0;
2687 /* If scatter gather */
2689 /* Load command entry data segments */
2691 (cnt < QLA_TGT_DATASEGS_PER_CMD_24XX) && prm->seg_cnt;
2692 cnt++, prm->seg_cnt--) {
2693 append_dsd64(&cur_dsd, prm->sg);
2694 prm->sg = sg_next(prm->sg);
2697 qlt_load_cont_data_segments(prm);
2700 static inline int qlt_has_data(struct qla_tgt_cmd *cmd)
2702 return cmd->bufflen > 0;
2705 static void qlt_print_dif_err(struct qla_tgt_prm *prm)
2707 struct qla_tgt_cmd *cmd;
2708 struct scsi_qla_host *vha;
2710 /* asc 0x10=dif error */
2711 if (prm->sense_buffer && (prm->sense_buffer[12] == 0x10)) {
2715 switch (prm->sense_buffer[13]) {
2717 ql_dbg(ql_dbg_tgt_dif, vha, 0xe00b,
2718 "BE detected Guard TAG ERR: lba[0x%llx|%lld] len[0x%x] "
2719 "se_cmd=%p tag[%x]",
2720 cmd->lba, cmd->lba, cmd->num_blks, &cmd->se_cmd,
2721 cmd->atio.u.isp24.exchange_addr);
2724 ql_dbg(ql_dbg_tgt_dif, vha, 0xe00c,
2725 "BE detected APP TAG ERR: lba[0x%llx|%lld] len[0x%x] "
2726 "se_cmd=%p tag[%x]",
2727 cmd->lba, cmd->lba, cmd->num_blks, &cmd->se_cmd,
2728 cmd->atio.u.isp24.exchange_addr);
2731 ql_dbg(ql_dbg_tgt_dif, vha, 0xe00f,
2732 "BE detected REF TAG ERR: lba[0x%llx|%lld] len[0x%x] "
2733 "se_cmd=%p tag[%x]",
2734 cmd->lba, cmd->lba, cmd->num_blks, &cmd->se_cmd,
2735 cmd->atio.u.isp24.exchange_addr);
2738 ql_dbg(ql_dbg_tgt_dif, vha, 0xe010,
2739 "BE detected Dif ERR: lba[%llx|%lld] len[%x] "
2740 "se_cmd=%p tag[%x]",
2741 cmd->lba, cmd->lba, cmd->num_blks, &cmd->se_cmd,
2742 cmd->atio.u.isp24.exchange_addr);
2745 ql_dump_buffer(ql_dbg_tgt_dif, vha, 0xe011, cmd->cdb, 16);
2750 * Called without ha->hardware_lock held
2752 static int qlt_pre_xmit_response(struct qla_tgt_cmd *cmd,
2753 struct qla_tgt_prm *prm, int xmit_type, uint8_t scsi_status,
2754 uint32_t *full_req_cnt)
2756 struct se_cmd *se_cmd = &cmd->se_cmd;
2757 struct qla_qpair *qpair = cmd->qpair;
2760 prm->tgt = cmd->tgt;
2762 prm->rq_result = scsi_status;
2763 prm->sense_buffer = &cmd->sense_buffer[0];
2764 prm->sense_buffer_len = TRANSPORT_SENSE_BUFFER;
2769 prm->add_status_pkt = 0;
2770 prm->prot_sg = NULL;
2771 prm->prot_seg_cnt = 0;
2774 if ((xmit_type & QLA_TGT_XMIT_DATA) && qlt_has_data(cmd)) {
2775 if (qlt_pci_map_calc_cnt(prm) != 0)
2779 *full_req_cnt = prm->req_cnt;
2781 if (se_cmd->se_cmd_flags & SCF_UNDERFLOW_BIT) {
2782 prm->residual = se_cmd->residual_count;
2783 ql_dbg_qp(ql_dbg_io + ql_dbg_verbose, qpair, 0x305c,
2784 "Residual underflow: %d (tag %lld, op %x, bufflen %d, rq_result %x)\n",
2785 prm->residual, se_cmd->tag,
2786 se_cmd->t_task_cdb ? se_cmd->t_task_cdb[0] : 0,
2787 cmd->bufflen, prm->rq_result);
2788 prm->rq_result |= SS_RESIDUAL_UNDER;
2789 } else if (se_cmd->se_cmd_flags & SCF_OVERFLOW_BIT) {
2790 prm->residual = se_cmd->residual_count;
2791 ql_dbg_qp(ql_dbg_io, qpair, 0x305d,
2792 "Residual overflow: %d (tag %lld, op %x, bufflen %d, rq_result %x)\n",
2793 prm->residual, se_cmd->tag, se_cmd->t_task_cdb ?
2794 se_cmd->t_task_cdb[0] : 0, cmd->bufflen, prm->rq_result);
2795 prm->rq_result |= SS_RESIDUAL_OVER;
2798 if (xmit_type & QLA_TGT_XMIT_STATUS) {
2800 * If QLA_TGT_XMIT_DATA is not set, add_status_pkt will be
2801 * ignored in *xmit_response() below
2803 if (qlt_has_data(cmd)) {
2804 if (QLA_TGT_SENSE_VALID(prm->sense_buffer) ||
2805 (IS_FWI2_CAPABLE(cmd->vha->hw) &&
2806 (prm->rq_result != 0))) {
2807 prm->add_status_pkt = 1;
2816 static inline int qlt_need_explicit_conf(struct qla_tgt_cmd *cmd,
2819 if (cmd->qpair->enable_class_2)
2823 return cmd->conf_compl_supported;
2825 return cmd->qpair->enable_explicit_conf &&
2826 cmd->conf_compl_supported;
2829 static void qlt_24xx_init_ctio_to_isp(struct ctio7_to_24xx *ctio,
2830 struct qla_tgt_prm *prm)
2832 prm->sense_buffer_len = min_t(uint32_t, prm->sense_buffer_len,
2833 (uint32_t)sizeof(ctio->u.status1.sense_data));
2834 ctio->u.status0.flags |= cpu_to_le16(CTIO7_FLAGS_SEND_STATUS);
2835 if (qlt_need_explicit_conf(prm->cmd, 0)) {
2836 ctio->u.status0.flags |= cpu_to_le16(
2837 CTIO7_FLAGS_EXPLICIT_CONFORM |
2838 CTIO7_FLAGS_CONFORM_REQ);
2840 ctio->u.status0.residual = cpu_to_le32(prm->residual);
2841 ctio->u.status0.scsi_status = cpu_to_le16(prm->rq_result);
2842 if (QLA_TGT_SENSE_VALID(prm->sense_buffer)) {
2845 if (qlt_need_explicit_conf(prm->cmd, 1)) {
2846 if ((prm->rq_result & SS_SCSI_STATUS_BYTE) != 0) {
2847 ql_dbg_qp(ql_dbg_tgt, prm->cmd->qpair, 0xe017,
2848 "Skipping EXPLICIT_CONFORM and "
2849 "CTIO7_FLAGS_CONFORM_REQ for FCP READ w/ "
2850 "non GOOD status\n");
2851 goto skip_explict_conf;
2853 ctio->u.status1.flags |= cpu_to_le16(
2854 CTIO7_FLAGS_EXPLICIT_CONFORM |
2855 CTIO7_FLAGS_CONFORM_REQ);
2858 ctio->u.status1.flags &=
2859 ~cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_0);
2860 ctio->u.status1.flags |=
2861 cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_1);
2862 ctio->u.status1.scsi_status |=
2863 cpu_to_le16(SS_SENSE_LEN_VALID);
2864 ctio->u.status1.sense_length =
2865 cpu_to_le16(prm->sense_buffer_len);
2866 for (i = 0; i < prm->sense_buffer_len/4; i++)
2867 ((uint32_t *)ctio->u.status1.sense_data)[i] =
2868 cpu_to_be32(((uint32_t *)prm->sense_buffer)[i]);
2870 qlt_print_dif_err(prm);
2873 ctio->u.status1.flags &=
2874 ~cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_0);
2875 ctio->u.status1.flags |=
2876 cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_1);
2877 ctio->u.status1.sense_length = 0;
2878 memset(ctio->u.status1.sense_data, 0,
2879 sizeof(ctio->u.status1.sense_data));
2882 /* Sense with len > 24, is it possible ??? */
2886 qlt_hba_err_chk_enabled(struct se_cmd *se_cmd)
2888 switch (se_cmd->prot_op) {
2889 case TARGET_PROT_DOUT_INSERT:
2890 case TARGET_PROT_DIN_STRIP:
2891 if (ql2xenablehba_err_chk >= 1)
2894 case TARGET_PROT_DOUT_PASS:
2895 case TARGET_PROT_DIN_PASS:
2896 if (ql2xenablehba_err_chk >= 2)
2899 case TARGET_PROT_DIN_INSERT:
2900 case TARGET_PROT_DOUT_STRIP:
2909 qla_tgt_ref_mask_check(struct se_cmd *se_cmd)
2911 switch (se_cmd->prot_op) {
2912 case TARGET_PROT_DIN_INSERT:
2913 case TARGET_PROT_DOUT_INSERT:
2914 case TARGET_PROT_DIN_STRIP:
2915 case TARGET_PROT_DOUT_STRIP:
2916 case TARGET_PROT_DIN_PASS:
2917 case TARGET_PROT_DOUT_PASS:
2926 * qla_tgt_set_dif_tags - Extract Ref and App tags from SCSI command
2929 qla_tgt_set_dif_tags(struct qla_tgt_cmd *cmd, struct crc_context *ctx,
2930 uint16_t *pfw_prot_opts)
2932 struct se_cmd *se_cmd = &cmd->se_cmd;
2933 uint32_t lba = 0xffffffff & se_cmd->t_task_lba;
2934 scsi_qla_host_t *vha = cmd->tgt->vha;
2935 struct qla_hw_data *ha = vha->hw;
2939 * wait till Mode Sense/Select cmd, modepage Ah, subpage 2
2940 * have been immplemented by TCM, before AppTag is avail.
2941 * Look for modesense_handlers[]
2944 ctx->app_tag_mask[0] = 0x0;
2945 ctx->app_tag_mask[1] = 0x0;
2947 if (IS_PI_UNINIT_CAPABLE(ha)) {
2948 if ((se_cmd->prot_type == TARGET_DIF_TYPE1_PROT) ||
2949 (se_cmd->prot_type == TARGET_DIF_TYPE2_PROT))
2950 *pfw_prot_opts |= PO_DIS_VALD_APP_ESC;
2951 else if (se_cmd->prot_type == TARGET_DIF_TYPE3_PROT)
2952 *pfw_prot_opts |= PO_DIS_VALD_APP_REF_ESC;
2955 t32 = ha->tgt.tgt_ops->get_dif_tags(cmd, pfw_prot_opts);
2957 switch (se_cmd->prot_type) {
2958 case TARGET_DIF_TYPE0_PROT:
2960 * No check for ql2xenablehba_err_chk, as it
2961 * would be an I/O error if hba tag generation
2964 ctx->ref_tag = cpu_to_le32(lba);
2965 /* enable ALL bytes of the ref tag */
2966 ctx->ref_tag_mask[0] = 0xff;
2967 ctx->ref_tag_mask[1] = 0xff;
2968 ctx->ref_tag_mask[2] = 0xff;
2969 ctx->ref_tag_mask[3] = 0xff;
2971 case TARGET_DIF_TYPE1_PROT:
2973 * For TYPE 1 protection: 16 bit GUARD tag, 32 bit
2974 * REF tag, and 16 bit app tag.
2976 ctx->ref_tag = cpu_to_le32(lba);
2977 if (!qla_tgt_ref_mask_check(se_cmd) ||
2978 !(ha->tgt.tgt_ops->chk_dif_tags(t32))) {
2979 *pfw_prot_opts |= PO_DIS_REF_TAG_VALD;
2982 /* enable ALL bytes of the ref tag */
2983 ctx->ref_tag_mask[0] = 0xff;
2984 ctx->ref_tag_mask[1] = 0xff;
2985 ctx->ref_tag_mask[2] = 0xff;
2986 ctx->ref_tag_mask[3] = 0xff;
2988 case TARGET_DIF_TYPE2_PROT:
2990 * For TYPE 2 protection: 16 bit GUARD + 32 bit REF
2991 * tag has to match LBA in CDB + N
2993 ctx->ref_tag = cpu_to_le32(lba);
2994 if (!qla_tgt_ref_mask_check(se_cmd) ||
2995 !(ha->tgt.tgt_ops->chk_dif_tags(t32))) {
2996 *pfw_prot_opts |= PO_DIS_REF_TAG_VALD;
2999 /* enable ALL bytes of the ref tag */
3000 ctx->ref_tag_mask[0] = 0xff;
3001 ctx->ref_tag_mask[1] = 0xff;
3002 ctx->ref_tag_mask[2] = 0xff;
3003 ctx->ref_tag_mask[3] = 0xff;
3005 case TARGET_DIF_TYPE3_PROT:
3006 /* For TYPE 3 protection: 16 bit GUARD only */
3007 *pfw_prot_opts |= PO_DIS_REF_TAG_VALD;
3008 ctx->ref_tag_mask[0] = ctx->ref_tag_mask[1] =
3009 ctx->ref_tag_mask[2] = ctx->ref_tag_mask[3] = 0x00;
3015 qlt_build_ctio_crc2_pkt(struct qla_qpair *qpair, struct qla_tgt_prm *prm)
3017 struct dsd64 *cur_dsd;
3018 uint32_t transfer_length = 0;
3019 uint32_t data_bytes;
3021 uint8_t bundling = 1;
3022 struct crc_context *crc_ctx_pkt = NULL;
3023 struct qla_hw_data *ha;
3024 struct ctio_crc2_to_fw *pkt;
3025 dma_addr_t crc_ctx_dma;
3026 uint16_t fw_prot_opts = 0;
3027 struct qla_tgt_cmd *cmd = prm->cmd;
3028 struct se_cmd *se_cmd = &cmd->se_cmd;
3030 struct atio_from_isp *atio = &prm->cmd->atio;
3031 struct qla_tc_param tc;
3033 scsi_qla_host_t *vha = cmd->vha;
3037 pkt = (struct ctio_crc2_to_fw *)qpair->req->ring_ptr;
3039 memset(pkt, 0, sizeof(*pkt));
3041 ql_dbg_qp(ql_dbg_tgt, cmd->qpair, 0xe071,
3042 "qla_target(%d):%s: se_cmd[%p] CRC2 prot_op[0x%x] cmd prot sg:cnt[%p:%x] lba[%llu]\n",
3043 cmd->vp_idx, __func__, se_cmd, se_cmd->prot_op,
3044 prm->prot_sg, prm->prot_seg_cnt, se_cmd->t_task_lba);
3046 if ((se_cmd->prot_op == TARGET_PROT_DIN_INSERT) ||
3047 (se_cmd->prot_op == TARGET_PROT_DOUT_STRIP))
3050 /* Compute dif len and adjust data len to incude protection */
3051 data_bytes = cmd->bufflen;
3052 dif_bytes = (data_bytes / cmd->blk_sz) * 8;
3054 switch (se_cmd->prot_op) {
3055 case TARGET_PROT_DIN_INSERT:
3056 case TARGET_PROT_DOUT_STRIP:
3057 transfer_length = data_bytes;
3058 if (cmd->prot_sg_cnt)
3059 data_bytes += dif_bytes;
3061 case TARGET_PROT_DIN_STRIP:
3062 case TARGET_PROT_DOUT_INSERT:
3063 case TARGET_PROT_DIN_PASS:
3064 case TARGET_PROT_DOUT_PASS:
3065 transfer_length = data_bytes + dif_bytes;
3072 if (!qlt_hba_err_chk_enabled(se_cmd))
3073 fw_prot_opts |= 0x10; /* Disable Guard tag checking */
3074 /* HBA error checking enabled */
3075 else if (IS_PI_UNINIT_CAPABLE(ha)) {
3076 if ((se_cmd->prot_type == TARGET_DIF_TYPE1_PROT) ||
3077 (se_cmd->prot_type == TARGET_DIF_TYPE2_PROT))
3078 fw_prot_opts |= PO_DIS_VALD_APP_ESC;
3079 else if (se_cmd->prot_type == TARGET_DIF_TYPE3_PROT)
3080 fw_prot_opts |= PO_DIS_VALD_APP_REF_ESC;
3083 switch (se_cmd->prot_op) {
3084 case TARGET_PROT_DIN_INSERT:
3085 case TARGET_PROT_DOUT_INSERT:
3086 fw_prot_opts |= PO_MODE_DIF_INSERT;
3088 case TARGET_PROT_DIN_STRIP:
3089 case TARGET_PROT_DOUT_STRIP:
3090 fw_prot_opts |= PO_MODE_DIF_REMOVE;
3092 case TARGET_PROT_DIN_PASS:
3093 case TARGET_PROT_DOUT_PASS:
3094 fw_prot_opts |= PO_MODE_DIF_PASS;
3095 /* FUTURE: does tcm require T10CRC<->IPCKSUM conversion? */
3097 default:/* Normal Request */
3098 fw_prot_opts |= PO_MODE_DIF_PASS;
3103 /* Update entry type to indicate Command Type CRC_2 IOCB */
3104 pkt->entry_type = CTIO_CRC2;
3105 pkt->entry_count = 1;
3106 pkt->vp_index = cmd->vp_idx;
3108 h = qlt_make_handle(qpair);
3109 if (unlikely(h == QLA_TGT_NULL_HANDLE)) {
3111 * CTIO type 7 from the firmware doesn't provide a way to
3112 * know the initiator's LOOP ID, hence we can't find
3113 * the session and, so, the command.
3117 qpair->req->outstanding_cmds[h] = (srb_t *)prm->cmd;
3119 pkt->handle = MAKE_HANDLE(qpair->req->id, h);
3120 pkt->handle |= CTIO_COMPLETION_HANDLE_MARK;
3121 pkt->nport_handle = cpu_to_le16(prm->cmd->loop_id);
3122 pkt->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
3123 pkt->initiator_id[0] = atio->u.isp24.fcp_hdr.s_id[2];
3124 pkt->initiator_id[1] = atio->u.isp24.fcp_hdr.s_id[1];
3125 pkt->initiator_id[2] = atio->u.isp24.fcp_hdr.s_id[0];
3126 pkt->exchange_addr = atio->u.isp24.exchange_addr;
3128 /* silence compile warning */
3129 t16 = be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id);
3130 pkt->ox_id = cpu_to_le16(t16);
3132 t16 = (atio->u.isp24.attr << 9);
3133 pkt->flags |= cpu_to_le16(t16);
3134 pkt->relative_offset = cpu_to_le32(prm->cmd->offset);
3136 /* Set transfer direction */
3137 if (cmd->dma_data_direction == DMA_TO_DEVICE)
3138 pkt->flags = cpu_to_le16(CTIO7_FLAGS_DATA_IN);
3139 else if (cmd->dma_data_direction == DMA_FROM_DEVICE)
3140 pkt->flags = cpu_to_le16(CTIO7_FLAGS_DATA_OUT);
3142 pkt->dseg_count = prm->tot_dsds;
3143 /* Fibre channel byte count */
3144 pkt->transfer_length = cpu_to_le32(transfer_length);
3146 /* ----- CRC context -------- */
3148 /* Allocate CRC context from global pool */
3149 crc_ctx_pkt = cmd->ctx =
3150 dma_pool_zalloc(ha->dl_dma_pool, GFP_ATOMIC, &crc_ctx_dma);
3153 goto crc_queuing_error;
3155 crc_ctx_pkt->crc_ctx_dma = crc_ctx_dma;
3156 INIT_LIST_HEAD(&crc_ctx_pkt->dsd_list);
3159 crc_ctx_pkt->handle = pkt->handle;
3161 qla_tgt_set_dif_tags(cmd, crc_ctx_pkt, &fw_prot_opts);
3163 put_unaligned_le64(crc_ctx_dma, &pkt->crc_context_address);
3164 pkt->crc_context_len = CRC_CONTEXT_LEN_FW;
3167 cur_dsd = &crc_ctx_pkt->u.nobundling.data_dsd;
3170 * Configure Bundling if we need to fetch interlaving
3171 * protection PCI accesses
3173 fw_prot_opts |= PO_ENABLE_DIF_BUNDLING;
3174 crc_ctx_pkt->u.bundling.dif_byte_count = cpu_to_le32(dif_bytes);
3175 crc_ctx_pkt->u.bundling.dseg_count =
3176 cpu_to_le16(prm->tot_dsds - prm->prot_seg_cnt);
3177 cur_dsd = &crc_ctx_pkt->u.bundling.data_dsd;
3180 /* Finish the common fields of CRC pkt */
3181 crc_ctx_pkt->blk_size = cpu_to_le16(cmd->blk_sz);
3182 crc_ctx_pkt->prot_opts = cpu_to_le16(fw_prot_opts);
3183 crc_ctx_pkt->byte_count = cpu_to_le32(data_bytes);
3184 crc_ctx_pkt->guard_seed = cpu_to_le16(0);
3186 memset((uint8_t *)&tc, 0 , sizeof(tc));
3188 tc.blk_sz = cmd->blk_sz;
3189 tc.bufflen = cmd->bufflen;
3191 tc.prot_sg = cmd->prot_sg;
3192 tc.ctx = crc_ctx_pkt;
3193 tc.ctx_dsd_alloced = &cmd->ctx_dsd_alloced;
3195 /* Walks data segments */
3196 pkt->flags |= cpu_to_le16(CTIO7_FLAGS_DSD_PTR);
3198 if (!bundling && prm->prot_seg_cnt) {
3199 if (qla24xx_walk_and_build_sglist_no_difb(ha, NULL, cur_dsd,
3200 prm->tot_dsds, &tc))
3201 goto crc_queuing_error;
3202 } else if (qla24xx_walk_and_build_sglist(ha, NULL, cur_dsd,
3203 (prm->tot_dsds - prm->prot_seg_cnt), &tc))
3204 goto crc_queuing_error;
3206 if (bundling && prm->prot_seg_cnt) {
3207 /* Walks dif segments */
3208 pkt->add_flags |= CTIO_CRC2_AF_DIF_DSD_ENA;
3210 cur_dsd = &crc_ctx_pkt->u.bundling.dif_dsd;
3211 if (qla24xx_walk_and_build_prot_sglist(ha, NULL, cur_dsd,
3212 prm->prot_seg_cnt, cmd))
3213 goto crc_queuing_error;
3218 /* Cleanup will be performed by the caller */
3219 qpair->req->outstanding_cmds[h] = NULL;
3221 return QLA_FUNCTION_FAILED;
3225 * Callback to setup response of xmit_type of QLA_TGT_XMIT_DATA and *
3226 * QLA_TGT_XMIT_STATUS for >= 24xx silicon
3228 int qlt_xmit_response(struct qla_tgt_cmd *cmd, int xmit_type,
3229 uint8_t scsi_status)
3231 struct scsi_qla_host *vha = cmd->vha;
3232 struct qla_qpair *qpair = cmd->qpair;
3233 struct ctio7_to_24xx *pkt;
3234 struct qla_tgt_prm prm;
3235 uint32_t full_req_cnt = 0;
3236 unsigned long flags = 0;
3239 if (!qpair->fw_started || (cmd->reset_count != qpair->chip_reset) ||
3240 (cmd->sess && cmd->sess->deleted)) {
3241 cmd->state = QLA_TGT_STATE_PROCESSED;
3245 ql_dbg_qp(ql_dbg_tgt, qpair, 0xe018,
3246 "is_send_status=%d, cmd->bufflen=%d, cmd->sg_cnt=%d, cmd->dma_data_direction=%d se_cmd[%p] qp %d\n",
3247 (xmit_type & QLA_TGT_XMIT_STATUS) ?
3248 1 : 0, cmd->bufflen, cmd->sg_cnt, cmd->dma_data_direction,
3249 &cmd->se_cmd, qpair->id);
3251 res = qlt_pre_xmit_response(cmd, &prm, xmit_type, scsi_status,
3253 if (unlikely(res != 0)) {
3257 spin_lock_irqsave(qpair->qp_lock_ptr, flags);
3259 if (xmit_type == QLA_TGT_XMIT_STATUS)
3260 qpair->tgt_counters.core_qla_snd_status++;
3262 qpair->tgt_counters.core_qla_que_buf++;
3264 if (!qpair->fw_started || cmd->reset_count != qpair->chip_reset) {
3266 * Either the port is not online or this request was from
3267 * previous life, just abort the processing.
3269 cmd->state = QLA_TGT_STATE_PROCESSED;
3270 ql_dbg_qp(ql_dbg_async, qpair, 0xe101,
3271 "RESET-RSP online/active/old-count/new-count = %d/%d/%d/%d.\n",
3272 vha->flags.online, qla2x00_reset_active(vha),
3273 cmd->reset_count, qpair->chip_reset);
3274 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
3278 /* Does F/W have an IOCBs for this request */
3279 res = qlt_check_reserve_free_req(qpair, full_req_cnt);
3281 goto out_unmap_unlock;
3283 if (cmd->se_cmd.prot_op && (xmit_type & QLA_TGT_XMIT_DATA))
3284 res = qlt_build_ctio_crc2_pkt(qpair, &prm);
3286 res = qlt_24xx_build_ctio_pkt(qpair, &prm);
3287 if (unlikely(res != 0)) {
3288 qpair->req->cnt += full_req_cnt;
3289 goto out_unmap_unlock;
3292 pkt = (struct ctio7_to_24xx *)prm.pkt;
3294 if (qlt_has_data(cmd) && (xmit_type & QLA_TGT_XMIT_DATA)) {
3295 pkt->u.status0.flags |=
3296 cpu_to_le16(CTIO7_FLAGS_DATA_IN |
3297 CTIO7_FLAGS_STATUS_MODE_0);
3299 if (cmd->se_cmd.prot_op == TARGET_PROT_NORMAL)
3300 qlt_load_data_segments(&prm);
3302 if (prm.add_status_pkt == 0) {
3303 if (xmit_type & QLA_TGT_XMIT_STATUS) {
3304 pkt->u.status0.scsi_status =
3305 cpu_to_le16(prm.rq_result);
3306 pkt->u.status0.residual =
3307 cpu_to_le32(prm.residual);
3308 pkt->u.status0.flags |= cpu_to_le16(
3309 CTIO7_FLAGS_SEND_STATUS);
3310 if (qlt_need_explicit_conf(cmd, 0)) {
3311 pkt->u.status0.flags |=
3313 CTIO7_FLAGS_EXPLICIT_CONFORM |
3314 CTIO7_FLAGS_CONFORM_REQ);
3320 * We have already made sure that there is sufficient
3321 * amount of request entries to not drop HW lock in
3324 struct ctio7_to_24xx *ctio =
3325 (struct ctio7_to_24xx *)qlt_get_req_pkt(
3328 ql_dbg_qp(ql_dbg_tgt, qpair, 0x305e,
3329 "Building additional status packet 0x%p.\n",
3333 * T10Dif: ctio_crc2_to_fw overlay ontop of
3336 memcpy(ctio, pkt, sizeof(*ctio));
3337 /* reset back to CTIO7 */
3338 ctio->entry_count = 1;
3339 ctio->entry_type = CTIO_TYPE7;
3340 ctio->dseg_count = 0;
3341 ctio->u.status1.flags &= ~cpu_to_le16(
3342 CTIO7_FLAGS_DATA_IN);
3344 /* Real finish is ctio_m1's finish */
3345 pkt->handle |= CTIO_INTERMEDIATE_HANDLE_MARK;
3346 pkt->u.status0.flags |= cpu_to_le16(
3347 CTIO7_FLAGS_DONT_RET_CTIO);
3349 /* qlt_24xx_init_ctio_to_isp will correct
3350 * all neccessary fields that's part of CTIO7.
3351 * There should be no residual of CTIO-CRC2 data.
3353 qlt_24xx_init_ctio_to_isp((struct ctio7_to_24xx *)ctio,
3357 qlt_24xx_init_ctio_to_isp(pkt, &prm);
3360 cmd->state = QLA_TGT_STATE_PROCESSED; /* Mid-level is done processing */
3361 cmd->cmd_sent_to_fw = 1;
3362 cmd->ctio_flags = le16_to_cpu(pkt->u.status0.flags);
3364 /* Memory Barrier */
3366 if (qpair->reqq_start_iocbs)
3367 qpair->reqq_start_iocbs(qpair);
3369 qla2x00_start_iocbs(vha, qpair->req);
3370 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
3375 qlt_unmap_sg(vha, cmd);
3376 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
3380 EXPORT_SYMBOL(qlt_xmit_response);
3382 int qlt_rdy_to_xfer(struct qla_tgt_cmd *cmd)
3384 struct ctio7_to_24xx *pkt;
3385 struct scsi_qla_host *vha = cmd->vha;
3386 struct qla_tgt *tgt = cmd->tgt;
3387 struct qla_tgt_prm prm;
3388 unsigned long flags = 0;
3390 struct qla_qpair *qpair = cmd->qpair;
3392 memset(&prm, 0, sizeof(prm));
3398 /* Calculate number of entries and segments required */
3399 if (qlt_pci_map_calc_cnt(&prm) != 0)
3402 if (!qpair->fw_started || (cmd->reset_count != qpair->chip_reset) ||
3403 (cmd->sess && cmd->sess->deleted)) {
3405 * Either the port is not online or this request was from
3406 * previous life, just abort the processing.
3409 cmd->write_data_transferred = 0;
3410 cmd->state = QLA_TGT_STATE_DATA_IN;
3411 vha->hw->tgt.tgt_ops->handle_data(cmd);
3412 ql_dbg_qp(ql_dbg_async, qpair, 0xe102,
3413 "RESET-XFR online/active/old-count/new-count = %d/%d/%d/%d.\n",
3414 vha->flags.online, qla2x00_reset_active(vha),
3415 cmd->reset_count, qpair->chip_reset);
3419 spin_lock_irqsave(qpair->qp_lock_ptr, flags);
3420 /* Does F/W have an IOCBs for this request */
3421 res = qlt_check_reserve_free_req(qpair, prm.req_cnt);
3423 goto out_unlock_free_unmap;
3424 if (cmd->se_cmd.prot_op)
3425 res = qlt_build_ctio_crc2_pkt(qpair, &prm);
3427 res = qlt_24xx_build_ctio_pkt(qpair, &prm);
3429 if (unlikely(res != 0)) {
3430 qpair->req->cnt += prm.req_cnt;
3431 goto out_unlock_free_unmap;
3434 pkt = (struct ctio7_to_24xx *)prm.pkt;
3435 pkt->u.status0.flags |= cpu_to_le16(CTIO7_FLAGS_DATA_OUT |
3436 CTIO7_FLAGS_STATUS_MODE_0);
3438 if (cmd->se_cmd.prot_op == TARGET_PROT_NORMAL)
3439 qlt_load_data_segments(&prm);
3441 cmd->state = QLA_TGT_STATE_NEED_DATA;
3442 cmd->cmd_sent_to_fw = 1;
3443 cmd->ctio_flags = le16_to_cpu(pkt->u.status0.flags);
3445 /* Memory Barrier */
3447 if (qpair->reqq_start_iocbs)
3448 qpair->reqq_start_iocbs(qpair);
3450 qla2x00_start_iocbs(vha, qpair->req);
3451 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
3455 out_unlock_free_unmap:
3456 qlt_unmap_sg(vha, cmd);
3457 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
3461 EXPORT_SYMBOL(qlt_rdy_to_xfer);
3465 * it is assumed either hardware_lock or qpair lock is held.
3468 qlt_handle_dif_error(struct qla_qpair *qpair, struct qla_tgt_cmd *cmd,
3469 struct ctio_crc_from_fw *sts)
3471 uint8_t *ap = &sts->actual_dif[0];
3472 uint8_t *ep = &sts->expected_dif[0];
3473 uint64_t lba = cmd->se_cmd.t_task_lba;
3474 uint8_t scsi_status, sense_key, asc, ascq;
3475 unsigned long flags;
3476 struct scsi_qla_host *vha = cmd->vha;
3478 cmd->trc_flags |= TRC_DIF_ERR;
3480 cmd->a_guard = be16_to_cpu(*(uint16_t *)(ap + 0));
3481 cmd->a_app_tag = be16_to_cpu(*(uint16_t *)(ap + 2));
3482 cmd->a_ref_tag = be32_to_cpu(*(uint32_t *)(ap + 4));
3484 cmd->e_guard = be16_to_cpu(*(uint16_t *)(ep + 0));
3485 cmd->e_app_tag = be16_to_cpu(*(uint16_t *)(ep + 2));
3486 cmd->e_ref_tag = be32_to_cpu(*(uint32_t *)(ep + 4));
3488 ql_dbg(ql_dbg_tgt_dif, vha, 0xf075,
3489 "%s: aborted %d state %d\n", __func__, cmd->aborted, cmd->state);
3491 scsi_status = sense_key = asc = ascq = 0;
3493 /* check appl tag */
3494 if (cmd->e_app_tag != cmd->a_app_tag) {
3495 ql_dbg(ql_dbg_tgt_dif, vha, 0xe00d,
3496 "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]",
3497 cmd->cdb[0], lba, (lba+cmd->num_blks), cmd->num_blks,
3498 cmd->a_ref_tag, cmd->e_ref_tag, cmd->a_app_tag,
3499 cmd->e_app_tag, cmd->a_guard, cmd->e_guard, cmd,
3500 cmd->atio.u.isp24.fcp_hdr.ox_id);
3502 cmd->dif_err_code = DIF_ERR_APP;
3503 scsi_status = SAM_STAT_CHECK_CONDITION;
3504 sense_key = ABORTED_COMMAND;
3510 if (cmd->e_ref_tag != cmd->a_ref_tag) {
3511 ql_dbg(ql_dbg_tgt_dif, vha, 0xe00e,
3512 "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] ",
3513 cmd->cdb[0], lba, (lba+cmd->num_blks), cmd->num_blks,
3514 cmd->a_ref_tag, cmd->e_ref_tag, cmd->a_app_tag,
3515 cmd->e_app_tag, cmd->a_guard, cmd->e_guard, cmd,
3516 cmd->atio.u.isp24.fcp_hdr.ox_id);
3518 cmd->dif_err_code = DIF_ERR_REF;
3519 scsi_status = SAM_STAT_CHECK_CONDITION;
3520 sense_key = ABORTED_COMMAND;
3527 if (cmd->e_guard != cmd->a_guard) {
3528 ql_dbg(ql_dbg_tgt_dif, vha, 0xe012,
3529 "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]",
3530 cmd->cdb[0], lba, (lba+cmd->num_blks), cmd->num_blks,
3531 cmd->a_ref_tag, cmd->e_ref_tag, cmd->a_app_tag,
3532 cmd->e_app_tag, cmd->a_guard, cmd->e_guard, cmd,
3533 cmd->atio.u.isp24.fcp_hdr.ox_id);
3535 cmd->dif_err_code = DIF_ERR_GRD;
3536 scsi_status = SAM_STAT_CHECK_CONDITION;
3537 sense_key = ABORTED_COMMAND;
3542 switch (cmd->state) {
3543 case QLA_TGT_STATE_NEED_DATA:
3544 /* handle_data will load DIF error code */
3545 cmd->state = QLA_TGT_STATE_DATA_IN;
3546 vha->hw->tgt.tgt_ops->handle_data(cmd);
3549 spin_lock_irqsave(&cmd->cmd_lock, flags);
3551 spin_unlock_irqrestore(&cmd->cmd_lock, flags);
3552 vha->hw->tgt.tgt_ops->free_cmd(cmd);
3555 spin_unlock_irqrestore(&cmd->cmd_lock, flags);
3557 qlt_send_resp_ctio(qpair, cmd, scsi_status, sense_key, asc,
3559 /* assume scsi status gets out on the wire.
3560 * Will not wait for completion.
3562 vha->hw->tgt.tgt_ops->free_cmd(cmd);
3567 /* If hardware_lock held on entry, might drop it, then reaquire */
3568 /* This function sends the appropriate CTIO to ISP 2xxx or 24xx */
3569 static int __qlt_send_term_imm_notif(struct scsi_qla_host *vha,
3570 struct imm_ntfy_from_isp *ntfy)
3572 struct nack_to_isp *nack;
3573 struct qla_hw_data *ha = vha->hw;
3577 ql_dbg(ql_dbg_tgt_tmr, vha, 0xe01c,
3578 "Sending TERM ELS CTIO (ha=%p)\n", ha);
3580 pkt = (request_t *)qla2x00_alloc_iocbs(vha, NULL);
3582 ql_dbg(ql_dbg_tgt, vha, 0xe080,
3583 "qla_target(%d): %s failed: unable to allocate "
3584 "request packet\n", vha->vp_idx, __func__);
3588 pkt->entry_type = NOTIFY_ACK_TYPE;
3589 pkt->entry_count = 1;
3590 pkt->handle = QLA_TGT_SKIP_HANDLE;
3592 nack = (struct nack_to_isp *)pkt;
3593 nack->ox_id = ntfy->ox_id;
3595 nack->u.isp24.nport_handle = ntfy->u.isp24.nport_handle;
3596 if (le16_to_cpu(ntfy->u.isp24.status) == IMM_NTFY_ELS) {
3597 nack->u.isp24.flags = ntfy->u.isp24.flags &
3598 __constant_cpu_to_le32(NOTIFY24XX_FLAGS_PUREX_IOCB);
3602 nack->u.isp24.flags |=
3603 __constant_cpu_to_le16(NOTIFY_ACK_FLAGS_TERMINATE);
3605 nack->u.isp24.srr_rx_id = ntfy->u.isp24.srr_rx_id;
3606 nack->u.isp24.status = ntfy->u.isp24.status;
3607 nack->u.isp24.status_subcode = ntfy->u.isp24.status_subcode;
3608 nack->u.isp24.fw_handle = ntfy->u.isp24.fw_handle;
3609 nack->u.isp24.exchange_address = ntfy->u.isp24.exchange_address;
3610 nack->u.isp24.srr_rel_offs = ntfy->u.isp24.srr_rel_offs;
3611 nack->u.isp24.srr_ui = ntfy->u.isp24.srr_ui;
3612 nack->u.isp24.vp_index = ntfy->u.isp24.vp_index;
3614 qla2x00_start_iocbs(vha, vha->req);
3618 static void qlt_send_term_imm_notif(struct scsi_qla_host *vha,
3619 struct imm_ntfy_from_isp *imm, int ha_locked)
3623 WARN_ON_ONCE(!ha_locked);
3624 rc = __qlt_send_term_imm_notif(vha, imm);
3625 pr_debug("rc = %d\n", rc);
3629 * If hardware_lock held on entry, might drop it, then reaquire
3630 * This function sends the appropriate CTIO to ISP 2xxx or 24xx
3632 static int __qlt_send_term_exchange(struct qla_qpair *qpair,
3633 struct qla_tgt_cmd *cmd,
3634 struct atio_from_isp *atio)
3636 struct scsi_qla_host *vha = qpair->vha;
3637 struct ctio7_to_24xx *ctio24;
3638 struct qla_hw_data *ha = vha->hw;
3643 ql_dbg(ql_dbg_tgt, vha, 0xe009, "Sending TERM EXCH CTIO (ha=%p)\n", ha);
3648 pkt = (request_t *)qla2x00_alloc_iocbs_ready(qpair, NULL);
3650 ql_dbg(ql_dbg_tgt, vha, 0xe050,
3651 "qla_target(%d): %s failed: unable to allocate "
3652 "request packet\n", vha->vp_idx, __func__);
3657 if (cmd->state < QLA_TGT_STATE_PROCESSED) {
3658 ql_dbg(ql_dbg_tgt, vha, 0xe051,
3659 "qla_target(%d): Terminating cmd %p with "
3660 "incorrect state %d\n", vha->vp_idx, cmd,
3666 qpair->tgt_counters.num_term_xchg_sent++;
3667 pkt->entry_count = 1;
3668 pkt->handle = QLA_TGT_SKIP_HANDLE | CTIO_COMPLETION_HANDLE_MARK;
3670 ctio24 = (struct ctio7_to_24xx *)pkt;
3671 ctio24->entry_type = CTIO_TYPE7;
3672 ctio24->nport_handle = CTIO7_NHANDLE_UNRECOGNIZED;
3673 ctio24->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
3674 ctio24->vp_index = vha->vp_idx;
3675 ctio24->initiator_id[0] = atio->u.isp24.fcp_hdr.s_id[2];
3676 ctio24->initiator_id[1] = atio->u.isp24.fcp_hdr.s_id[1];
3677 ctio24->initiator_id[2] = atio->u.isp24.fcp_hdr.s_id[0];
3678 ctio24->exchange_addr = atio->u.isp24.exchange_addr;
3679 temp = (atio->u.isp24.attr << 9) | CTIO7_FLAGS_STATUS_MODE_1 |
3680 CTIO7_FLAGS_TERMINATE;
3681 ctio24->u.status1.flags = cpu_to_le16(temp);
3682 temp = be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id);
3683 ctio24->u.status1.ox_id = cpu_to_le16(temp);
3685 /* Memory Barrier */
3687 if (qpair->reqq_start_iocbs)
3688 qpair->reqq_start_iocbs(qpair);
3690 qla2x00_start_iocbs(vha, qpair->req);
3694 static void qlt_send_term_exchange(struct qla_qpair *qpair,
3695 struct qla_tgt_cmd *cmd, struct atio_from_isp *atio, int ha_locked,
3698 struct scsi_qla_host *vha;
3699 unsigned long flags = 0;
3702 /* why use different vha? NPIV */
3709 rc = __qlt_send_term_exchange(qpair, cmd, atio);
3711 qlt_alloc_qfull_cmd(vha, atio, 0, 0);
3714 spin_lock_irqsave(qpair->qp_lock_ptr, flags);
3715 rc = __qlt_send_term_exchange(qpair, cmd, atio);
3717 qlt_alloc_qfull_cmd(vha, atio, 0, 0);
3720 if (cmd && !ul_abort && !cmd->aborted) {
3722 qlt_unmap_sg(vha, cmd);
3723 vha->hw->tgt.tgt_ops->free_cmd(cmd);
3727 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
3732 static void qlt_init_term_exchange(struct scsi_qla_host *vha)
3734 struct list_head free_list;
3735 struct qla_tgt_cmd *cmd, *tcmd;
3737 vha->hw->tgt.leak_exchg_thresh_hold =
3738 (vha->hw->cur_fw_xcb_count/100) * LEAK_EXCHG_THRESH_HOLD_PERCENT;
3741 if (!list_empty(&vha->hw->tgt.q_full_list)) {
3742 INIT_LIST_HEAD(&free_list);
3743 list_splice_init(&vha->hw->tgt.q_full_list, &free_list);
3745 list_for_each_entry_safe(cmd, tcmd, &free_list, cmd_list) {
3746 list_del(&cmd->cmd_list);
3747 /* This cmd was never sent to TCM. There is no need
3748 * to schedule free or call free_cmd
3751 vha->hw->tgt.num_qfull_cmds_alloc--;
3754 vha->hw->tgt.num_qfull_cmds_dropped = 0;
3757 static void qlt_chk_exch_leak_thresh_hold(struct scsi_qla_host *vha)
3759 uint32_t total_leaked;
3761 total_leaked = vha->hw->tgt.num_qfull_cmds_dropped;
3763 if (vha->hw->tgt.leak_exchg_thresh_hold &&
3764 (total_leaked > vha->hw->tgt.leak_exchg_thresh_hold)) {
3766 ql_dbg(ql_dbg_tgt, vha, 0xe079,
3767 "Chip reset due to exchange starvation: %d/%d.\n",
3768 total_leaked, vha->hw->cur_fw_xcb_count);
3770 if (IS_P3P_TYPE(vha->hw))
3771 set_bit(FCOE_CTX_RESET_NEEDED, &vha->dpc_flags);
3773 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
3774 qla2xxx_wake_dpc(vha);
3779 int qlt_abort_cmd(struct qla_tgt_cmd *cmd)
3781 struct qla_tgt *tgt = cmd->tgt;
3782 struct scsi_qla_host *vha = tgt->vha;
3783 struct se_cmd *se_cmd = &cmd->se_cmd;
3784 unsigned long flags;
3786 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf014,
3787 "qla_target(%d): terminating exchange for aborted cmd=%p "
3788 "(se_cmd=%p, tag=%llu)", vha->vp_idx, cmd, &cmd->se_cmd,
3791 spin_lock_irqsave(&cmd->cmd_lock, flags);
3793 spin_unlock_irqrestore(&cmd->cmd_lock, flags);
3795 * It's normal to see 2 calls in this path:
3796 * 1) XFER Rdy completion + CMD_T_ABORT
3797 * 2) TCM TMR - drain_state_list
3799 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf016,
3800 "multiple abort. %p transport_state %x, t_state %x, "
3801 "se_cmd_flags %x\n", cmd, cmd->se_cmd.transport_state,
3802 cmd->se_cmd.t_state, cmd->se_cmd.se_cmd_flags);
3806 cmd->trc_flags |= TRC_ABORT;
3807 spin_unlock_irqrestore(&cmd->cmd_lock, flags);
3809 qlt_send_term_exchange(cmd->qpair, cmd, &cmd->atio, 0, 1);
3812 EXPORT_SYMBOL(qlt_abort_cmd);
3814 void qlt_free_cmd(struct qla_tgt_cmd *cmd)
3816 struct fc_port *sess = cmd->sess;
3818 ql_dbg(ql_dbg_tgt, cmd->vha, 0xe074,
3819 "%s: se_cmd[%p] ox_id %04x\n",
3820 __func__, &cmd->se_cmd,
3821 be16_to_cpu(cmd->atio.u.isp24.fcp_hdr.ox_id));
3823 BUG_ON(cmd->cmd_in_wq);
3826 qlt_unmap_sg(cmd->vha, cmd);
3829 qlt_decr_num_pend_cmds(cmd->vha);
3831 BUG_ON(cmd->sg_mapped);
3832 cmd->jiffies_at_free = get_jiffies_64();
3833 if (unlikely(cmd->free_sg))
3836 if (!sess || !sess->se_sess) {
3840 cmd->jiffies_at_free = get_jiffies_64();
3841 target_free_tag(sess->se_sess, &cmd->se_cmd);
3843 EXPORT_SYMBOL(qlt_free_cmd);
3846 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
3848 static int qlt_term_ctio_exchange(struct qla_qpair *qpair, void *ctio,
3849 struct qla_tgt_cmd *cmd, uint32_t status)
3852 struct scsi_qla_host *vha = qpair->vha;
3854 if (cmd->se_cmd.prot_op)
3855 ql_dbg(ql_dbg_tgt_dif, vha, 0xe013,
3856 "Term DIF cmd: lba[0x%llx|%lld] len[0x%x] "
3857 "se_cmd=%p tag[%x] op %#x/%s",
3859 cmd->num_blks, &cmd->se_cmd,
3860 cmd->atio.u.isp24.exchange_addr,
3861 cmd->se_cmd.prot_op,
3862 prot_op_str(cmd->se_cmd.prot_op));
3865 struct ctio7_from_24xx *c = (struct ctio7_from_24xx *)ctio;
3868 cpu_to_le16(OF_TERM_EXCH));
3873 qlt_send_term_exchange(qpair, cmd, &cmd->atio, 1, 0);
3879 /* ha->hardware_lock supposed to be held on entry */
3880 static void *qlt_ctio_to_cmd(struct scsi_qla_host *vha,
3881 struct rsp_que *rsp, uint32_t handle, void *ctio)
3884 struct req_que *req;
3885 int qid = GET_QID(handle);
3886 uint32_t h = handle & ~QLA_TGT_HANDLE_MASK;
3888 if (unlikely(h == QLA_TGT_SKIP_HANDLE))
3891 if (qid == rsp->req->id) {
3893 } else if (vha->hw->req_q_map[qid]) {
3894 ql_dbg(ql_dbg_tgt_mgt, vha, 0x1000a,
3895 "qla_target(%d): CTIO completion with different QID %d handle %x\n",
3896 vha->vp_idx, rsp->id, handle);
3897 req = vha->hw->req_q_map[qid];
3902 h &= QLA_CMD_HANDLE_MASK;
3904 if (h != QLA_TGT_NULL_HANDLE) {
3905 if (unlikely(h >= req->num_outstanding_cmds)) {
3906 ql_dbg(ql_dbg_tgt, vha, 0xe052,
3907 "qla_target(%d): Wrong handle %x received\n",
3908 vha->vp_idx, handle);
3912 cmd = (void *) req->outstanding_cmds[h];
3913 if (unlikely(cmd == NULL)) {
3914 ql_dbg(ql_dbg_async, vha, 0xe053,
3915 "qla_target(%d): Suspicious: unable to find the command with handle %x req->id %d rsp->id %d\n",
3916 vha->vp_idx, handle, req->id, rsp->id);
3919 req->outstanding_cmds[h] = NULL;
3920 } else if (ctio != NULL) {
3921 /* We can't get loop ID from CTIO7 */
3922 ql_dbg(ql_dbg_tgt, vha, 0xe054,
3923 "qla_target(%d): Wrong CTIO received: QLA24xx doesn't "
3924 "support NULL handles\n", vha->vp_idx);
3932 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
3934 static void qlt_do_ctio_completion(struct scsi_qla_host *vha,
3935 struct rsp_que *rsp, uint32_t handle, uint32_t status, void *ctio)
3937 struct qla_hw_data *ha = vha->hw;
3938 struct se_cmd *se_cmd;
3939 struct qla_tgt_cmd *cmd;
3940 struct qla_qpair *qpair = rsp->qpair;
3942 if (handle & CTIO_INTERMEDIATE_HANDLE_MARK) {
3943 /* That could happen only in case of an error/reset/abort */
3944 if (status != CTIO_SUCCESS) {
3945 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01d,
3946 "Intermediate CTIO received"
3947 " (status %x)\n", status);
3952 cmd = qlt_ctio_to_cmd(vha, rsp, handle, ctio);
3956 se_cmd = &cmd->se_cmd;
3957 cmd->cmd_sent_to_fw = 0;
3959 qlt_unmap_sg(vha, cmd);
3961 if (unlikely(status != CTIO_SUCCESS)) {
3962 switch (status & 0xFFFF) {
3963 case CTIO_INVALID_RX_ID:
3964 if (printk_ratelimit())
3965 dev_info(&vha->hw->pdev->dev,
3966 "qla_target(%d): CTIO with INVALID_RX_ID ATIO attr %x CTIO Flags %x|%x\n",
3967 vha->vp_idx, cmd->atio.u.isp24.attr,
3968 ((cmd->ctio_flags >> 9) & 0xf),
3972 case CTIO_LIP_RESET:
3973 case CTIO_TARGET_RESET:
3975 /* driver request abort via Terminate exchange */
3978 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf058,
3979 "qla_target(%d): CTIO with "
3980 "status %#x received, state %x, se_cmd %p, "
3981 "(LIP_RESET=e, ABORTED=2, TARGET_RESET=17, "
3982 "TIMEOUT=b, INVALID_RX_ID=8)\n", vha->vp_idx,
3983 status, cmd->state, se_cmd);
3986 case CTIO_PORT_LOGGED_OUT:
3987 case CTIO_PORT_UNAVAILABLE:
3990 (status & 0xFFFF) == CTIO_PORT_LOGGED_OUT;
3992 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf059,
3993 "qla_target(%d): CTIO with %s status %x "
3994 "received (state %x, se_cmd %p)\n", vha->vp_idx,
3995 logged_out ? "PORT LOGGED OUT" : "PORT UNAVAILABLE",
3996 status, cmd->state, se_cmd);
3998 if (logged_out && cmd->sess) {
4000 * Session is already logged out, but we need
4001 * to notify initiator, who's not aware of this
4003 cmd->sess->send_els_logo = 1;
4004 ql_dbg(ql_dbg_disc, vha, 0x20f8,
4005 "%s %d %8phC post del sess\n",
4006 __func__, __LINE__, cmd->sess->port_name);
4008 qlt_schedule_sess_for_deletion(cmd->sess);
4012 case CTIO_DIF_ERROR: {
4013 struct ctio_crc_from_fw *crc =
4014 (struct ctio_crc_from_fw *)ctio;
4015 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf073,
4016 "qla_target(%d): CTIO with DIF_ERROR status %x "
4017 "received (state %x, ulp_cmd %p) actual_dif[0x%llx] "
4018 "expect_dif[0x%llx]\n",
4019 vha->vp_idx, status, cmd->state, se_cmd,
4020 *((u64 *)&crc->actual_dif[0]),
4021 *((u64 *)&crc->expected_dif[0]));
4023 qlt_handle_dif_error(qpair, cmd, ctio);
4027 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05b,
4028 "qla_target(%d): CTIO with error status 0x%x received (state %x, se_cmd %p\n",
4029 vha->vp_idx, status, cmd->state, se_cmd);
4034 /* "cmd->aborted" means
4035 * cmd is already aborted/terminated, we don't
4036 * need to terminate again. The exchange is already
4037 * cleaned up/freed at FW level. Just cleanup at driver
4040 if ((cmd->state != QLA_TGT_STATE_NEED_DATA) &&
4042 cmd->trc_flags |= TRC_CTIO_ERR;
4043 if (qlt_term_ctio_exchange(qpair, ctio, cmd, status))
4048 if (cmd->state == QLA_TGT_STATE_PROCESSED) {
4049 cmd->trc_flags |= TRC_CTIO_DONE;
4050 } else if (cmd->state == QLA_TGT_STATE_NEED_DATA) {
4051 cmd->state = QLA_TGT_STATE_DATA_IN;
4053 if (status == CTIO_SUCCESS)
4054 cmd->write_data_transferred = 1;
4056 ha->tgt.tgt_ops->handle_data(cmd);
4058 } else if (cmd->aborted) {
4059 cmd->trc_flags |= TRC_CTIO_ABORTED;
4060 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01e,
4061 "Aborted command %p (tag %lld) finished\n", cmd, se_cmd->tag);
4063 cmd->trc_flags |= TRC_CTIO_STRANGE;
4064 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05c,
4065 "qla_target(%d): A command in state (%d) should "
4066 "not return a CTIO complete\n", vha->vp_idx, cmd->state);
4069 if (unlikely(status != CTIO_SUCCESS) &&
4071 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01f, "Finishing failed CTIO\n");
4075 ha->tgt.tgt_ops->free_cmd(cmd);
4078 static inline int qlt_get_fcp_task_attr(struct scsi_qla_host *vha,
4083 switch (task_codes) {
4084 case ATIO_SIMPLE_QUEUE:
4085 fcp_task_attr = TCM_SIMPLE_TAG;
4087 case ATIO_HEAD_OF_QUEUE:
4088 fcp_task_attr = TCM_HEAD_TAG;
4090 case ATIO_ORDERED_QUEUE:
4091 fcp_task_attr = TCM_ORDERED_TAG;
4093 case ATIO_ACA_QUEUE:
4094 fcp_task_attr = TCM_ACA_TAG;
4097 fcp_task_attr = TCM_SIMPLE_TAG;
4100 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05d,
4101 "qla_target: unknown task code %x, use ORDERED instead\n",
4103 fcp_task_attr = TCM_ORDERED_TAG;
4107 return fcp_task_attr;
4110 static struct fc_port *qlt_make_local_sess(struct scsi_qla_host *,
4113 * Process context for I/O path into tcm_qla2xxx code
4115 static void __qlt_do_work(struct qla_tgt_cmd *cmd)
4117 scsi_qla_host_t *vha = cmd->vha;
4118 struct qla_hw_data *ha = vha->hw;
4119 struct fc_port *sess = cmd->sess;
4120 struct atio_from_isp *atio = &cmd->atio;
4122 unsigned long flags;
4123 uint32_t data_length;
4124 int ret, fcp_task_attr, data_dir, bidi = 0;
4125 struct qla_qpair *qpair = cmd->qpair;
4128 cmd->trc_flags |= TRC_DO_WORK;
4131 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf082,
4132 "cmd with tag %u is aborted\n",
4133 cmd->atio.u.isp24.exchange_addr);
4137 spin_lock_init(&cmd->cmd_lock);
4138 cdb = &atio->u.isp24.fcp_cmnd.cdb[0];
4139 cmd->se_cmd.tag = atio->u.isp24.exchange_addr;
4141 if (atio->u.isp24.fcp_cmnd.rddata &&
4142 atio->u.isp24.fcp_cmnd.wrdata) {
4144 data_dir = DMA_TO_DEVICE;
4145 } else if (atio->u.isp24.fcp_cmnd.rddata)
4146 data_dir = DMA_FROM_DEVICE;
4147 else if (atio->u.isp24.fcp_cmnd.wrdata)
4148 data_dir = DMA_TO_DEVICE;
4150 data_dir = DMA_NONE;
4152 fcp_task_attr = qlt_get_fcp_task_attr(vha,
4153 atio->u.isp24.fcp_cmnd.task_attr);
4154 data_length = get_datalen_for_atio(atio);
4156 ret = ha->tgt.tgt_ops->handle_cmd(vha, cmd, cdb, data_length,
4157 fcp_task_attr, data_dir, bidi);
4161 * Drop extra session reference from qlt_handle_cmd_for_atio().
4163 ha->tgt.tgt_ops->put_sess(sess);
4167 ql_dbg(ql_dbg_io, vha, 0x3060, "Terminating work cmd %p", cmd);
4169 * cmd has not sent to target yet, so pass NULL as the second
4170 * argument to qlt_send_term_exchange() and free the memory here.
4172 cmd->trc_flags |= TRC_DO_WORK_ERR;
4173 spin_lock_irqsave(qpair->qp_lock_ptr, flags);
4174 qlt_send_term_exchange(qpair, NULL, &cmd->atio, 1, 0);
4176 qlt_decr_num_pend_cmds(vha);
4177 target_free_tag(sess->se_sess, &cmd->se_cmd);
4178 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
4180 ha->tgt.tgt_ops->put_sess(sess);
4183 static void qlt_do_work(struct work_struct *work)
4185 struct qla_tgt_cmd *cmd = container_of(work, struct qla_tgt_cmd, work);
4186 scsi_qla_host_t *vha = cmd->vha;
4187 unsigned long flags;
4189 spin_lock_irqsave(&vha->cmd_list_lock, flags);
4190 list_del(&cmd->cmd_list);
4191 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
4196 void qlt_clr_qp_table(struct scsi_qla_host *vha)
4198 unsigned long flags;
4199 struct qla_hw_data *ha = vha->hw;
4200 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
4204 ql_log(ql_log_info, vha, 0x706c,
4205 "User update Number of Active Qpairs %d\n",
4206 ha->tgt.num_act_qpairs);
4208 spin_lock_irqsave(&ha->tgt.atio_lock, flags);
4210 btree_for_each_safe64(&tgt->lun_qpair_map, key, node)
4211 btree_remove64(&tgt->lun_qpair_map, key);
4213 ha->base_qpair->lun_cnt = 0;
4214 for (key = 0; key < ha->max_qpairs; key++)
4215 if (ha->queue_pair_map[key])
4216 ha->queue_pair_map[key]->lun_cnt = 0;
4218 spin_unlock_irqrestore(&ha->tgt.atio_lock, flags);
4221 static void qlt_assign_qpair(struct scsi_qla_host *vha,
4222 struct qla_tgt_cmd *cmd)
4224 struct qla_qpair *qpair, *qp;
4225 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
4226 struct qla_qpair_hint *h;
4228 if (vha->flags.qpairs_available) {
4229 h = btree_lookup64(&tgt->lun_qpair_map, cmd->unpacked_lun);
4231 /* spread lun to qpair ratio evently */
4233 struct scsi_qla_host *base_vha =
4234 pci_get_drvdata(vha->hw->pdev);
4236 qpair = vha->hw->base_qpair;
4237 if (qpair->lun_cnt == 0) {
4239 h = qla_qpair_to_hint(tgt, qpair);
4241 rc = btree_insert64(&tgt->lun_qpair_map,
4242 cmd->unpacked_lun, h, GFP_ATOMIC);
4245 ql_log(ql_log_info, vha, 0xd037,
4246 "Unable to insert lun %llx into lun_qpair_map\n",
4251 lcnt = qpair->lun_cnt;
4255 list_for_each_entry(qp, &base_vha->qp_list,
4257 if (qp->lun_cnt == 0) {
4259 h = qla_qpair_to_hint(tgt, qp);
4261 rc = btree_insert64(&tgt->lun_qpair_map,
4262 cmd->unpacked_lun, h, GFP_ATOMIC);
4265 ql_log(ql_log_info, vha, 0xd038,
4266 "Unable to insert lun %llx into lun_qpair_map\n",
4272 if (qp->lun_cnt < lcnt) {
4281 h = qla_qpair_to_hint(tgt, qpair);
4283 rc = btree_insert64(&tgt->lun_qpair_map,
4284 cmd->unpacked_lun, h, GFP_ATOMIC);
4287 ql_log(ql_log_info, vha, 0xd039,
4288 "Unable to insert lun %llx into lun_qpair_map\n",
4293 h = &tgt->qphints[0];
4296 cmd->qpair = h->qpair;
4297 cmd->se_cmd.cpuid = h->cpuid;
4300 static struct qla_tgt_cmd *qlt_get_tag(scsi_qla_host_t *vha,
4301 struct fc_port *sess,
4302 struct atio_from_isp *atio)
4304 struct se_session *se_sess = sess->se_sess;
4305 struct qla_tgt_cmd *cmd;
4308 tag = sbitmap_queue_get(&se_sess->sess_tag_pool, &cpu);
4312 cmd = &((struct qla_tgt_cmd *)se_sess->sess_cmd_map)[tag];
4313 memset(cmd, 0, sizeof(struct qla_tgt_cmd));
4314 cmd->cmd_type = TYPE_TGT_CMD;
4315 memcpy(&cmd->atio, atio, sizeof(*atio));
4316 cmd->state = QLA_TGT_STATE_NEW;
4317 cmd->tgt = vha->vha_tgt.qla_tgt;
4318 qlt_incr_num_pend_cmds(vha);
4320 cmd->se_cmd.map_tag = tag;
4321 cmd->se_cmd.map_cpu = cpu;
4323 cmd->loop_id = sess->loop_id;
4324 cmd->conf_compl_supported = sess->conf_compl_supported;
4327 cmd->jiffies_at_alloc = get_jiffies_64();
4329 cmd->unpacked_lun = scsilun_to_int(
4330 (struct scsi_lun *)&atio->u.isp24.fcp_cmnd.lun);
4331 qlt_assign_qpair(vha, cmd);
4332 cmd->reset_count = vha->hw->base_qpair->chip_reset;
4333 cmd->vp_idx = vha->vp_idx;
4338 /* ha->hardware_lock supposed to be held on entry */
4339 static int qlt_handle_cmd_for_atio(struct scsi_qla_host *vha,
4340 struct atio_from_isp *atio)
4342 struct qla_hw_data *ha = vha->hw;
4343 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
4344 struct fc_port *sess;
4345 struct qla_tgt_cmd *cmd;
4346 unsigned long flags;
4349 if (unlikely(tgt->tgt_stop)) {
4350 ql_dbg(ql_dbg_io, vha, 0x3061,
4351 "New command while device %p is shutting down\n", tgt);
4355 id.b.al_pa = atio->u.isp24.fcp_hdr.s_id[2];
4356 id.b.area = atio->u.isp24.fcp_hdr.s_id[1];
4357 id.b.domain = atio->u.isp24.fcp_hdr.s_id[0];
4358 if (IS_SW_RESV_ADDR(id))
4361 sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha, atio->u.isp24.fcp_hdr.s_id);
4362 if (unlikely(!sess))
4365 /* Another WWN used to have our s_id. Our PLOGI scheduled its
4366 * session deletion, but it's still in sess_del_work wq */
4367 if (sess->deleted) {
4368 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf002,
4369 "New command while old session %p is being deleted\n",
4375 * Do kref_get() before returning + dropping qla_hw_data->hardware_lock.
4377 if (!kref_get_unless_zero(&sess->sess_kref)) {
4378 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf004,
4379 "%s: kref_get fail, %8phC oxid %x \n",
4380 __func__, sess->port_name,
4381 be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id));
4385 cmd = qlt_get_tag(vha, sess, atio);
4387 ql_dbg(ql_dbg_io, vha, 0x3062,
4388 "qla_target(%d): Allocation of cmd failed\n", vha->vp_idx);
4389 ha->tgt.tgt_ops->put_sess(sess);
4394 cmd->trc_flags |= TRC_NEW_CMD;
4396 spin_lock_irqsave(&vha->cmd_list_lock, flags);
4397 list_add_tail(&cmd->cmd_list, &vha->qla_cmd_list);
4398 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
4400 INIT_WORK(&cmd->work, qlt_do_work);
4401 if (vha->flags.qpairs_available) {
4402 queue_work_on(cmd->se_cmd.cpuid, qla_tgt_wq, &cmd->work);
4403 } else if (ha->msix_count) {
4404 if (cmd->atio.u.isp24.fcp_cmnd.rddata)
4405 queue_work_on(smp_processor_id(), qla_tgt_wq,
4408 queue_work_on(cmd->se_cmd.cpuid, qla_tgt_wq,
4411 queue_work(qla_tgt_wq, &cmd->work);
4417 /* ha->hardware_lock supposed to be held on entry */
4418 static int qlt_issue_task_mgmt(struct fc_port *sess, u64 lun,
4419 int fn, void *iocb, int flags)
4421 struct scsi_qla_host *vha = sess->vha;
4422 struct qla_hw_data *ha = vha->hw;
4423 struct qla_tgt_mgmt_cmd *mcmd;
4424 struct atio_from_isp *a = (struct atio_from_isp *)iocb;
4425 struct qla_qpair_hint *h = &vha->vha_tgt.qla_tgt->qphints[0];
4427 mcmd = mempool_alloc(qla_tgt_mgmt_cmd_mempool, GFP_ATOMIC);
4429 ql_dbg(ql_dbg_tgt_tmr, vha, 0x10009,
4430 "qla_target(%d): Allocation of management "
4431 "command failed, some commands and their data could "
4432 "leak\n", vha->vp_idx);
4435 memset(mcmd, 0, sizeof(*mcmd));
4439 memcpy(&mcmd->orig_iocb.imm_ntfy, iocb,
4440 sizeof(mcmd->orig_iocb.imm_ntfy));
4442 mcmd->tmr_func = fn;
4443 mcmd->flags = flags;
4444 mcmd->reset_count = ha->base_qpair->chip_reset;
4445 mcmd->qpair = h->qpair;
4447 mcmd->se_cmd.cpuid = h->cpuid;
4448 mcmd->unpacked_lun = lun;
4451 case QLA_TGT_LUN_RESET:
4452 case QLA_TGT_CLEAR_TS:
4453 case QLA_TGT_ABORT_TS:
4454 abort_cmds_for_lun(vha, lun, a->u.isp24.fcp_hdr.s_id);
4456 case QLA_TGT_CLEAR_ACA:
4457 h = qlt_find_qphint(vha, mcmd->unpacked_lun);
4458 mcmd->qpair = h->qpair;
4459 mcmd->se_cmd.cpuid = h->cpuid;
4462 case QLA_TGT_TARGET_RESET:
4463 case QLA_TGT_NEXUS_LOSS_SESS:
4464 case QLA_TGT_NEXUS_LOSS:
4465 case QLA_TGT_ABORT_ALL:
4471 INIT_WORK(&mcmd->work, qlt_do_tmr_work);
4472 queue_work_on(mcmd->se_cmd.cpuid, qla_tgt_wq,
4478 /* ha->hardware_lock supposed to be held on entry */
4479 static int qlt_handle_task_mgmt(struct scsi_qla_host *vha, void *iocb)
4481 struct atio_from_isp *a = (struct atio_from_isp *)iocb;
4482 struct qla_hw_data *ha = vha->hw;
4483 struct fc_port *sess;
4486 unsigned long flags;
4488 fn = a->u.isp24.fcp_cmnd.task_mgmt_flags;
4490 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
4491 sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha,
4492 a->u.isp24.fcp_hdr.s_id);
4493 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
4496 scsilun_to_int((struct scsi_lun *)&a->u.isp24.fcp_cmnd.lun);
4498 if (sess == NULL || sess->deleted)
4501 return qlt_issue_task_mgmt(sess, unpacked_lun, fn, iocb, 0);
4504 /* ha->hardware_lock supposed to be held on entry */
4505 static int __qlt_abort_task(struct scsi_qla_host *vha,
4506 struct imm_ntfy_from_isp *iocb, struct fc_port *sess)
4508 struct atio_from_isp *a = (struct atio_from_isp *)iocb;
4509 struct qla_hw_data *ha = vha->hw;
4510 struct qla_tgt_mgmt_cmd *mcmd;
4514 mcmd = mempool_alloc(qla_tgt_mgmt_cmd_mempool, GFP_ATOMIC);
4516 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05f,
4517 "qla_target(%d): %s: Allocation of ABORT cmd failed\n",
4518 vha->vp_idx, __func__);
4521 memset(mcmd, 0, sizeof(*mcmd));
4524 memcpy(&mcmd->orig_iocb.imm_ntfy, iocb,
4525 sizeof(mcmd->orig_iocb.imm_ntfy));
4528 scsilun_to_int((struct scsi_lun *)&a->u.isp24.fcp_cmnd.lun);
4529 mcmd->reset_count = ha->base_qpair->chip_reset;
4530 mcmd->tmr_func = QLA_TGT_2G_ABORT_TASK;
4531 mcmd->qpair = ha->base_qpair;
4533 rc = ha->tgt.tgt_ops->handle_tmr(mcmd, unpacked_lun, mcmd->tmr_func,
4534 le16_to_cpu(iocb->u.isp2x.seq_id));
4536 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf060,
4537 "qla_target(%d): tgt_ops->handle_tmr() failed: %d\n",
4539 mempool_free(mcmd, qla_tgt_mgmt_cmd_mempool);
4546 /* ha->hardware_lock supposed to be held on entry */
4547 static int qlt_abort_task(struct scsi_qla_host *vha,
4548 struct imm_ntfy_from_isp *iocb)
4550 struct qla_hw_data *ha = vha->hw;
4551 struct fc_port *sess;
4553 unsigned long flags;
4555 loop_id = GET_TARGET_ID(ha, (struct atio_from_isp *)iocb);
4557 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
4558 sess = ha->tgt.tgt_ops->find_sess_by_loop_id(vha, loop_id);
4559 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
4562 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf025,
4563 "qla_target(%d): task abort for unexisting "
4564 "session\n", vha->vp_idx);
4565 return qlt_sched_sess_work(vha->vha_tgt.qla_tgt,
4566 QLA_TGT_SESS_WORK_ABORT, iocb, sizeof(*iocb));
4569 return __qlt_abort_task(vha, iocb, sess);
4572 void qlt_logo_completion_handler(fc_port_t *fcport, int rc)
4574 if (rc != MBS_COMMAND_COMPLETE) {
4575 ql_dbg(ql_dbg_tgt_mgt, fcport->vha, 0xf093,
4576 "%s: se_sess %p / sess %p from"
4577 " port %8phC loop_id %#04x s_id %02x:%02x:%02x"
4578 " LOGO failed: %#x\n",
4582 fcport->port_name, fcport->loop_id,
4583 fcport->d_id.b.domain, fcport->d_id.b.area,
4584 fcport->d_id.b.al_pa, rc);
4587 fcport->logout_completed = 1;
4591 * ha->hardware_lock supposed to be held on entry (to protect tgt->sess_list)
4593 * Schedules sessions with matching port_id/loop_id but different wwn for
4594 * deletion. Returns existing session with matching wwn if present.
4598 qlt_find_sess_invalidate_other(scsi_qla_host_t *vha, uint64_t wwn,
4599 port_id_t port_id, uint16_t loop_id, struct fc_port **conflict_sess)
4601 struct fc_port *sess = NULL, *other_sess;
4604 *conflict_sess = NULL;
4606 list_for_each_entry(other_sess, &vha->vp_fcports, list) {
4608 other_wwn = wwn_to_u64(other_sess->port_name);
4610 if (wwn == other_wwn) {
4616 /* find other sess with nport_id collision */
4617 if (port_id.b24 == other_sess->d_id.b24) {
4618 if (loop_id != other_sess->loop_id) {
4619 ql_dbg(ql_dbg_tgt_tmr, vha, 0x1000c,
4620 "Invalidating sess %p loop_id %d wwn %llx.\n",
4621 other_sess, other_sess->loop_id, other_wwn);
4624 * logout_on_delete is set by default, but another
4625 * session that has the same s_id/loop_id combo
4626 * might have cleared it when requested this session
4627 * deletion, so don't touch it
4629 qlt_schedule_sess_for_deletion(other_sess);
4632 * Another wwn used to have our s_id/loop_id
4633 * kill the session, but don't free the loop_id
4635 ql_dbg(ql_dbg_tgt_tmr, vha, 0xf01b,
4636 "Invalidating sess %p loop_id %d wwn %llx.\n",
4637 other_sess, other_sess->loop_id, other_wwn);
4639 other_sess->keep_nport_handle = 1;
4640 if (other_sess->disc_state != DSC_DELETED)
4641 *conflict_sess = other_sess;
4642 qlt_schedule_sess_for_deletion(other_sess);
4647 /* find other sess with nport handle collision */
4648 if ((loop_id == other_sess->loop_id) &&
4649 (loop_id != FC_NO_LOOP_ID)) {
4650 ql_dbg(ql_dbg_tgt_tmr, vha, 0x1000d,
4651 "Invalidating sess %p loop_id %d wwn %llx.\n",
4652 other_sess, other_sess->loop_id, other_wwn);
4654 /* Same loop_id but different s_id
4655 * Ok to kill and logout */
4656 qlt_schedule_sess_for_deletion(other_sess);
4663 /* Abort any commands for this s_id waiting on qla_tgt_wq workqueue */
4664 static int abort_cmds_for_s_id(struct scsi_qla_host *vha, port_id_t *s_id)
4666 struct qla_tgt_sess_op *op;
4667 struct qla_tgt_cmd *cmd;
4670 unsigned long flags;
4672 key = (((u32)s_id->b.domain << 16) |
4673 ((u32)s_id->b.area << 8) |
4674 ((u32)s_id->b.al_pa));
4676 spin_lock_irqsave(&vha->cmd_list_lock, flags);
4677 list_for_each_entry(op, &vha->qla_sess_op_cmd_list, cmd_list) {
4678 uint32_t op_key = sid_to_key(op->atio.u.isp24.fcp_hdr.s_id);
4680 if (op_key == key) {
4686 list_for_each_entry(op, &vha->unknown_atio_list, cmd_list) {
4687 uint32_t op_key = sid_to_key(op->atio.u.isp24.fcp_hdr.s_id);
4689 if (op_key == key) {
4695 list_for_each_entry(cmd, &vha->qla_cmd_list, cmd_list) {
4696 uint32_t cmd_key = sid_to_key(cmd->atio.u.isp24.fcp_hdr.s_id);
4698 if (cmd_key == key) {
4703 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
4708 static int qlt_handle_login(struct scsi_qla_host *vha,
4709 struct imm_ntfy_from_isp *iocb)
4711 struct fc_port *sess = NULL, *conflict_sess = NULL;
4714 uint16_t loop_id, wd3_lo;
4716 struct qlt_plogi_ack_t *pla;
4717 unsigned long flags;
4719 wwn = wwn_to_u64(iocb->u.isp24.port_name);
4721 port_id.b.domain = iocb->u.isp24.port_id[2];
4722 port_id.b.area = iocb->u.isp24.port_id[1];
4723 port_id.b.al_pa = iocb->u.isp24.port_id[0];
4724 port_id.b.rsvd_1 = 0;
4726 loop_id = le16_to_cpu(iocb->u.isp24.nport_handle);
4728 /* Mark all stale commands sitting in qla_tgt_wq for deletion */
4729 abort_cmds_for_s_id(vha, &port_id);
4732 spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
4733 sess = qlt_find_sess_invalidate_other(vha, wwn,
4734 port_id, loop_id, &conflict_sess);
4735 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
4737 ql_dbg(ql_dbg_disc, vha, 0xffff,
4738 "%s %d Term INOT due to WWN=0 lid=%d, NportID %06X ",
4739 __func__, __LINE__, loop_id, port_id.b24);
4740 qlt_send_term_imm_notif(vha, iocb, 1);
4744 if (IS_SW_RESV_ADDR(port_id)) {
4749 pla = qlt_plogi_ack_find_add(vha, &port_id, iocb);
4751 ql_dbg(ql_dbg_disc + ql_dbg_verbose, vha, 0xffff,
4752 "%s %d %8phC Term INOT due to mem alloc fail",
4754 iocb->u.isp24.port_name);
4755 qlt_send_term_imm_notif(vha, iocb, 1);
4759 if (conflict_sess) {
4760 conflict_sess->login_gen++;
4761 qlt_plogi_ack_link(vha, pla, conflict_sess,
4762 QLT_PLOGI_LINK_CONFLICT);
4767 ql_dbg(ql_dbg_disc, vha, 0xffff,
4768 "%s %d %8phC post new sess\n",
4769 __func__, __LINE__, iocb->u.isp24.port_name);
4770 if (iocb->u.isp24.status_subcode == ELS_PLOGI)
4771 qla24xx_post_newsess_work(vha, &port_id,
4772 iocb->u.isp24.port_name,
4773 iocb->u.isp24.u.plogi.node_name,
4774 pla, FC4_TYPE_UNKNOWN);
4776 qla24xx_post_newsess_work(vha, &port_id,
4777 iocb->u.isp24.port_name, NULL,
4778 pla, FC4_TYPE_UNKNOWN);
4783 if (sess->disc_state == DSC_UPD_FCPORT) {
4787 * Remote port registration is still going on from
4788 * previous login. Allow it to finish before we
4789 * accept the new login.
4791 sess->next_disc_state = DSC_DELETE_PEND;
4792 sec = jiffies_to_msecs(jiffies -
4793 sess->jiffies_at_registration) / 1000;
4794 if (sess->sec_since_registration < sec && sec &&
4796 sess->sec_since_registration = sec;
4797 ql_dbg(ql_dbg_disc, vha, 0xffff,
4798 "%s %8phC - Slow Rport registration (%d Sec)\n",
4799 __func__, sess->port_name, sec);
4803 kmem_cache_free(qla_tgt_plogi_cachep, pla);
4805 qlt_send_term_imm_notif(vha, iocb, 1);
4809 qlt_plogi_ack_link(vha, pla, sess, QLT_PLOGI_LINK_SAME_WWN);
4810 sess->d_id = port_id;
4813 if (iocb->u.isp24.status_subcode == ELS_PRLI) {
4814 sess->fw_login_state = DSC_LS_PRLI_PEND;
4816 sess->loop_id = loop_id;
4817 sess->d_id = port_id;
4818 sess->fw_login_state = DSC_LS_PRLI_PEND;
4819 wd3_lo = le16_to_cpu(iocb->u.isp24.u.prli.wd3_lo);
4822 sess->conf_compl_supported = 1;
4824 if ((wd3_lo & BIT_4) == 0)
4825 sess->port_type = FCT_INITIATOR;
4827 sess->port_type = FCT_TARGET;
4830 sess->fw_login_state = DSC_LS_PLOGI_PEND;
4833 ql_dbg(ql_dbg_disc, vha, 0x20f9,
4834 "%s %d %8phC DS %d\n",
4835 __func__, __LINE__, sess->port_name, sess->disc_state);
4837 switch (sess->disc_state) {
4839 qlt_plogi_ack_unref(vha, pla);
4844 * Under normal circumstances we want to release nport handle
4845 * during LOGO process to avoid nport handle leaks inside FW.
4846 * The exception is when LOGO is done while another PLOGI with
4847 * the same nport handle is waiting as might be the case here.
4848 * Note: there is always a possibily of a race where session
4849 * deletion has already started for other reasons (e.g. ACL
4850 * removal) and now PLOGI arrives:
4851 * 1. if PLOGI arrived in FW after nport handle has been freed,
4852 * FW must have assigned this PLOGI a new/same handle and we
4853 * can proceed ACK'ing it as usual when session deletion
4855 * 2. if PLOGI arrived in FW before LOGO with LCF_FREE_NPORT
4856 * bit reached it, the handle has now been released. We'll
4857 * get an error when we ACK this PLOGI. Nothing will be sent
4858 * back to initiator. Initiator should eventually retry
4859 * PLOGI and situation will correct itself.
4861 sess->keep_nport_handle = ((sess->loop_id == loop_id) &&
4862 (sess->d_id.b24 == port_id.b24));
4864 ql_dbg(ql_dbg_disc, vha, 0x20f9,
4865 "%s %d %8phC post del sess\n",
4866 __func__, __LINE__, sess->port_name);
4869 qlt_schedule_sess_for_deletion(sess);
4877 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
4879 static int qlt_24xx_handle_els(struct scsi_qla_host *vha,
4880 struct imm_ntfy_from_isp *iocb)
4882 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
4883 struct qla_hw_data *ha = vha->hw;
4884 struct fc_port *sess = NULL, *conflict_sess = NULL;
4890 unsigned long flags;
4892 wwn = wwn_to_u64(iocb->u.isp24.port_name);
4894 port_id.b.domain = iocb->u.isp24.port_id[2];
4895 port_id.b.area = iocb->u.isp24.port_id[1];
4896 port_id.b.al_pa = iocb->u.isp24.port_id[0];
4897 port_id.b.rsvd_1 = 0;
4899 loop_id = le16_to_cpu(iocb->u.isp24.nport_handle);
4901 ql_dbg(ql_dbg_disc, vha, 0xf026,
4902 "qla_target(%d): Port ID: %02x:%02x:%02x ELS opcode: 0x%02x lid %d %8phC\n",
4903 vha->vp_idx, iocb->u.isp24.port_id[2],
4904 iocb->u.isp24.port_id[1], iocb->u.isp24.port_id[0],
4905 iocb->u.isp24.status_subcode, loop_id,
4906 iocb->u.isp24.port_name);
4908 /* res = 1 means ack at the end of thread
4909 * res = 0 means ack async/later.
4911 switch (iocb->u.isp24.status_subcode) {
4913 res = qlt_handle_login(vha, iocb);
4918 sess = qla2x00_find_fcport_by_wwpn(vha,
4919 iocb->u.isp24.port_name, 1);
4921 if (sess && sess->plogi_link[QLT_PLOGI_LINK_SAME_WWN]) {
4922 ql_dbg(ql_dbg_disc, vha, 0xffff,
4923 "%s %d %8phC Term PRLI due to PLOGI ACK not completed\n",
4925 iocb->u.isp24.port_name);
4926 qlt_send_term_imm_notif(vha, iocb, 1);
4930 res = qlt_handle_login(vha, iocb);
4934 if (IS_SW_RESV_ADDR(port_id)) {
4939 wd3_lo = le16_to_cpu(iocb->u.isp24.u.prli.wd3_lo);
4942 spin_lock_irqsave(&tgt->ha->tgt.sess_lock, flags);
4943 sess = qlt_find_sess_invalidate_other(vha, wwn, port_id,
4944 loop_id, &conflict_sess);
4945 spin_unlock_irqrestore(&tgt->ha->tgt.sess_lock, flags);
4948 if (conflict_sess) {
4949 switch (conflict_sess->disc_state) {
4951 case DSC_DELETE_PEND:
4954 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf09b,
4955 "PRLI with conflicting sess %p port %8phC\n",
4956 conflict_sess, conflict_sess->port_name);
4957 conflict_sess->fw_login_state =
4958 DSC_LS_PORT_UNAVAIL;
4959 qlt_send_term_imm_notif(vha, iocb, 1);
4966 bool delete = false;
4969 spin_lock_irqsave(&tgt->ha->tgt.sess_lock, flags);
4970 switch (sess->fw_login_state) {
4971 case DSC_LS_PLOGI_PEND:
4972 case DSC_LS_PLOGI_COMP:
4973 case DSC_LS_PRLI_COMP:
4980 switch (sess->disc_state) {
4981 case DSC_UPD_FCPORT:
4982 spin_unlock_irqrestore(&tgt->ha->tgt.sess_lock,
4985 sec = jiffies_to_msecs(jiffies -
4986 sess->jiffies_at_registration)/1000;
4987 if (sess->sec_since_registration < sec && sec &&
4989 sess->sec_since_registration = sec;
4990 ql_dbg(ql_dbg_disc, sess->vha, 0xffff,
4991 "%s %8phC : Slow Rport registration(%d Sec)\n",
4992 __func__, sess->port_name, sec);
4994 qlt_send_term_imm_notif(vha, iocb, 1);
4997 case DSC_LOGIN_PEND:
4999 case DSC_LOGIN_COMPLETE:
5008 spin_unlock_irqrestore(&tgt->ha->tgt.sess_lock,
5011 * Impatient initiator sent PRLI before last
5012 * PLOGI could finish. Will force him to re-try,
5013 * while last one finishes.
5015 ql_log(ql_log_warn, sess->vha, 0xf095,
5016 "sess %p PRLI received, before plogi ack.\n",
5018 qlt_send_term_imm_notif(vha, iocb, 1);
5024 * This shouldn't happen under normal circumstances,
5025 * since we have deleted the old session during PLOGI
5027 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf096,
5028 "PRLI (loop_id %#04x) for existing sess %p (loop_id %#04x)\n",
5029 sess->loop_id, sess, iocb->u.isp24.nport_handle);
5032 sess->loop_id = loop_id;
5033 sess->d_id = port_id;
5034 sess->fw_login_state = DSC_LS_PRLI_PEND;
5037 sess->conf_compl_supported = 1;
5039 if ((wd3_lo & BIT_4) == 0)
5040 sess->port_type = FCT_INITIATOR;
5042 sess->port_type = FCT_TARGET;
5044 spin_unlock_irqrestore(&tgt->ha->tgt.sess_lock, flags);
5046 res = 1; /* send notify ack */
5048 /* Make session global (not used in fabric mode) */
5049 if (ha->current_topology != ISP_CFG_F) {
5051 ql_dbg(ql_dbg_disc, vha, 0x20fa,
5052 "%s %d %8phC post nack\n",
5053 __func__, __LINE__, sess->port_name);
5054 qla24xx_post_nack_work(vha, sess, iocb,
5058 set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
5059 set_bit(LOCAL_LOOP_UPDATE, &vha->dpc_flags);
5060 qla2xxx_wake_dpc(vha);
5064 ql_dbg(ql_dbg_disc, vha, 0x20fb,
5065 "%s %d %8phC post nack\n",
5066 __func__, __LINE__, sess->port_name);
5067 qla24xx_post_nack_work(vha, sess, iocb,
5075 if (le16_to_cpu(iocb->u.isp24.flags) &
5076 NOTIFY24XX_FLAGS_GLOBAL_TPRLO) {
5078 qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS);
5085 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
5086 sess = qla2x00_find_fcport_by_loopid(vha, loop_id);
5087 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
5091 sess->fw_login_state = DSC_LS_LOGO_PEND;
5092 sess->logo_ack_needed = 1;
5093 memcpy(sess->iocb, iocb, IOCB_SIZE);
5096 res = qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS_SESS);
5098 ql_dbg(ql_dbg_disc, vha, 0x20fc,
5099 "%s: logo %llx res %d sess %p ",
5100 __func__, wwn, res, sess);
5103 * cmd went upper layer, look for qlt_xmit_tm_rsp()
5104 * for LOGO_ACK & sess delete
5109 /* cmd did not go to upper layer. */
5111 qlt_schedule_sess_for_deletion(sess);
5114 /* else logo will be ack */
5120 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
5122 if (tgt->link_reinit_iocb_pending) {
5123 qlt_send_notify_ack(ha->base_qpair,
5124 &tgt->link_reinit_iocb, 0, 0, 0, 0, 0, 0);
5125 tgt->link_reinit_iocb_pending = 0;
5128 sess = qla2x00_find_fcport_by_wwpn(vha,
5129 iocb->u.isp24.port_name, 1);
5131 ql_dbg(ql_dbg_disc, vha, 0x20fd,
5132 "sess %p lid %d|%d DS %d LS %d\n",
5133 sess, sess->loop_id, loop_id,
5134 sess->disc_state, sess->fw_login_state);
5137 res = 1; /* send notify ack */
5141 case ELS_FLOGI: /* should never happen */
5143 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf061,
5144 "qla_target(%d): Unsupported ELS command %x "
5145 "received\n", vha->vp_idx, iocb->u.isp24.status_subcode);
5146 res = qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS_SESS);
5150 ql_dbg(ql_dbg_disc, vha, 0xf026,
5151 "qla_target(%d): Exit ELS opcode: 0x%02x res %d\n",
5152 vha->vp_idx, iocb->u.isp24.status_subcode, res);
5158 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
5160 static void qlt_handle_imm_notify(struct scsi_qla_host *vha,
5161 struct imm_ntfy_from_isp *iocb)
5163 struct qla_hw_data *ha = vha->hw;
5164 uint32_t add_flags = 0;
5165 int send_notify_ack = 1;
5168 status = le16_to_cpu(iocb->u.isp2x.status);
5170 case IMM_NTFY_LIP_RESET:
5172 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf032,
5173 "qla_target(%d): LIP reset (loop %#x), subcode %x\n",
5174 vha->vp_idx, le16_to_cpu(iocb->u.isp24.nport_handle),
5175 iocb->u.isp24.status_subcode);
5177 if (qlt_reset(vha, iocb, QLA_TGT_ABORT_ALL) == 0)
5178 send_notify_ack = 0;
5182 case IMM_NTFY_LIP_LINK_REINIT:
5184 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
5186 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf033,
5187 "qla_target(%d): LINK REINIT (loop %#x, "
5188 "subcode %x)\n", vha->vp_idx,
5189 le16_to_cpu(iocb->u.isp24.nport_handle),
5190 iocb->u.isp24.status_subcode);
5191 if (tgt->link_reinit_iocb_pending) {
5192 qlt_send_notify_ack(ha->base_qpair,
5193 &tgt->link_reinit_iocb, 0, 0, 0, 0, 0, 0);
5195 memcpy(&tgt->link_reinit_iocb, iocb, sizeof(*iocb));
5196 tgt->link_reinit_iocb_pending = 1;
5198 * QLogic requires to wait after LINK REINIT for possible
5199 * PDISC or ADISC ELS commands
5201 send_notify_ack = 0;
5205 case IMM_NTFY_PORT_LOGOUT:
5206 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf034,
5207 "qla_target(%d): Port logout (loop "
5208 "%#x, subcode %x)\n", vha->vp_idx,
5209 le16_to_cpu(iocb->u.isp24.nport_handle),
5210 iocb->u.isp24.status_subcode);
5212 if (qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS_SESS) == 0)
5213 send_notify_ack = 0;
5214 /* The sessions will be cleared in the callback, if needed */
5217 case IMM_NTFY_GLBL_TPRLO:
5218 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf035,
5219 "qla_target(%d): Global TPRLO (%x)\n", vha->vp_idx, status);
5220 if (qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS) == 0)
5221 send_notify_ack = 0;
5222 /* The sessions will be cleared in the callback, if needed */
5225 case IMM_NTFY_PORT_CONFIG:
5226 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf036,
5227 "qla_target(%d): Port config changed (%x)\n", vha->vp_idx,
5229 if (qlt_reset(vha, iocb, QLA_TGT_ABORT_ALL) == 0)
5230 send_notify_ack = 0;
5231 /* The sessions will be cleared in the callback, if needed */
5234 case IMM_NTFY_GLBL_LOGO:
5235 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06a,
5236 "qla_target(%d): Link failure detected\n",
5238 /* I_T nexus loss */
5239 if (qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS) == 0)
5240 send_notify_ack = 0;
5243 case IMM_NTFY_IOCB_OVERFLOW:
5244 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06b,
5245 "qla_target(%d): Cannot provide requested "
5246 "capability (IOCB overflowed the immediate notify "
5247 "resource count)\n", vha->vp_idx);
5250 case IMM_NTFY_ABORT_TASK:
5251 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf037,
5252 "qla_target(%d): Abort Task (S %08x I %#x -> "
5253 "L %#x)\n", vha->vp_idx,
5254 le16_to_cpu(iocb->u.isp2x.seq_id),
5255 GET_TARGET_ID(ha, (struct atio_from_isp *)iocb),
5256 le16_to_cpu(iocb->u.isp2x.lun));
5257 if (qlt_abort_task(vha, iocb) == 0)
5258 send_notify_ack = 0;
5261 case IMM_NTFY_RESOURCE:
5262 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06c,
5263 "qla_target(%d): Out of resources, host %ld\n",
5264 vha->vp_idx, vha->host_no);
5267 case IMM_NTFY_MSG_RX:
5268 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf038,
5269 "qla_target(%d): Immediate notify task %x\n",
5270 vha->vp_idx, iocb->u.isp2x.task_flags);
5274 if (qlt_24xx_handle_els(vha, iocb) == 0)
5275 send_notify_ack = 0;
5278 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06d,
5279 "qla_target(%d): Received unknown immediate "
5280 "notify status %x\n", vha->vp_idx, status);
5284 if (send_notify_ack)
5285 qlt_send_notify_ack(ha->base_qpair, iocb, add_flags, 0, 0, 0,
5290 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
5291 * This function sends busy to ISP 2xxx or 24xx.
5293 static int __qlt_send_busy(struct qla_qpair *qpair,
5294 struct atio_from_isp *atio, uint16_t status)
5296 struct scsi_qla_host *vha = qpair->vha;
5297 struct ctio7_to_24xx *ctio24;
5298 struct qla_hw_data *ha = vha->hw;
5300 struct fc_port *sess = NULL;
5301 unsigned long flags;
5305 id.b.al_pa = atio->u.isp24.fcp_hdr.s_id[2];
5306 id.b.area = atio->u.isp24.fcp_hdr.s_id[1];
5307 id.b.domain = atio->u.isp24.fcp_hdr.s_id[0];
5310 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
5311 sess = qla2x00_find_fcport_by_nportid(vha, &id, 1);
5312 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
5314 qlt_send_term_exchange(qpair, NULL, atio, 1, 0);
5317 /* Sending marker isn't necessary, since we called from ISR */
5319 pkt = (request_t *)__qla2x00_alloc_iocbs(qpair, NULL);
5321 ql_dbg(ql_dbg_io, vha, 0x3063,
5322 "qla_target(%d): %s failed: unable to allocate "
5323 "request packet", vha->vp_idx, __func__);
5327 qpair->tgt_counters.num_q_full_sent++;
5328 pkt->entry_count = 1;
5329 pkt->handle = QLA_TGT_SKIP_HANDLE | CTIO_COMPLETION_HANDLE_MARK;
5331 ctio24 = (struct ctio7_to_24xx *)pkt;
5332 ctio24->entry_type = CTIO_TYPE7;
5333 ctio24->nport_handle = sess->loop_id;
5334 ctio24->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
5335 ctio24->vp_index = vha->vp_idx;
5336 ctio24->initiator_id[0] = atio->u.isp24.fcp_hdr.s_id[2];
5337 ctio24->initiator_id[1] = atio->u.isp24.fcp_hdr.s_id[1];
5338 ctio24->initiator_id[2] = atio->u.isp24.fcp_hdr.s_id[0];
5339 ctio24->exchange_addr = atio->u.isp24.exchange_addr;
5340 temp = (atio->u.isp24.attr << 9) |
5341 CTIO7_FLAGS_STATUS_MODE_1 | CTIO7_FLAGS_SEND_STATUS |
5342 CTIO7_FLAGS_DONT_RET_CTIO;
5343 ctio24->u.status1.flags = cpu_to_le16(temp);
5345 * CTIO from fw w/o se_cmd doesn't provide enough info to retry it,
5346 * if the explicit conformation is used.
5348 ctio24->u.status1.ox_id = swab16(atio->u.isp24.fcp_hdr.ox_id);
5349 ctio24->u.status1.scsi_status = cpu_to_le16(status);
5351 ctio24->u.status1.residual = get_datalen_for_atio(atio);
5353 if (ctio24->u.status1.residual != 0)
5354 ctio24->u.status1.scsi_status |= SS_RESIDUAL_UNDER;
5356 /* Memory Barrier */
5358 if (qpair->reqq_start_iocbs)
5359 qpair->reqq_start_iocbs(qpair);
5361 qla2x00_start_iocbs(vha, qpair->req);
5366 * This routine is used to allocate a command for either a QFull condition
5367 * (ie reply SAM_STAT_BUSY) or to terminate an exchange that did not go
5371 qlt_alloc_qfull_cmd(struct scsi_qla_host *vha,
5372 struct atio_from_isp *atio, uint16_t status, int qfull)
5374 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
5375 struct qla_hw_data *ha = vha->hw;
5376 struct fc_port *sess;
5377 struct se_session *se_sess;
5378 struct qla_tgt_cmd *cmd;
5380 unsigned long flags;
5382 if (unlikely(tgt->tgt_stop)) {
5383 ql_dbg(ql_dbg_io, vha, 0x300a,
5384 "New command while device %p is shutting down\n", tgt);
5388 if ((vha->hw->tgt.num_qfull_cmds_alloc + 1) > MAX_QFULL_CMDS_ALLOC) {
5389 vha->hw->tgt.num_qfull_cmds_dropped++;
5390 if (vha->hw->tgt.num_qfull_cmds_dropped >
5391 vha->qla_stats.stat_max_qfull_cmds_dropped)
5392 vha->qla_stats.stat_max_qfull_cmds_dropped =
5393 vha->hw->tgt.num_qfull_cmds_dropped;
5395 ql_dbg(ql_dbg_io, vha, 0x3068,
5396 "qla_target(%d): %s: QFull CMD dropped[%d]\n",
5397 vha->vp_idx, __func__,
5398 vha->hw->tgt.num_qfull_cmds_dropped);
5400 qlt_chk_exch_leak_thresh_hold(vha);
5404 sess = ha->tgt.tgt_ops->find_sess_by_s_id
5405 (vha, atio->u.isp24.fcp_hdr.s_id);
5409 se_sess = sess->se_sess;
5411 tag = sbitmap_queue_get(&se_sess->sess_tag_pool, &cpu);
5413 ql_dbg(ql_dbg_io, vha, 0x3009,
5414 "qla_target(%d): %s: Allocation of cmd failed\n",
5415 vha->vp_idx, __func__);
5417 vha->hw->tgt.num_qfull_cmds_dropped++;
5418 if (vha->hw->tgt.num_qfull_cmds_dropped >
5419 vha->qla_stats.stat_max_qfull_cmds_dropped)
5420 vha->qla_stats.stat_max_qfull_cmds_dropped =
5421 vha->hw->tgt.num_qfull_cmds_dropped;
5423 qlt_chk_exch_leak_thresh_hold(vha);
5427 cmd = &((struct qla_tgt_cmd *)se_sess->sess_cmd_map)[tag];
5428 memset(cmd, 0, sizeof(struct qla_tgt_cmd));
5430 qlt_incr_num_pend_cmds(vha);
5431 INIT_LIST_HEAD(&cmd->cmd_list);
5432 memcpy(&cmd->atio, atio, sizeof(*atio));
5434 cmd->tgt = vha->vha_tgt.qla_tgt;
5436 cmd->reset_count = ha->base_qpair->chip_reset;
5438 cmd->qpair = ha->base_qpair;
5439 cmd->se_cmd.map_cpu = cpu;
5443 /* NOTE: borrowing the state field to carry the status */
5444 cmd->state = status;
5446 cmd->term_exchg = 1;
5448 spin_lock_irqsave(&vha->hw->tgt.q_full_lock, flags);
5449 list_add_tail(&cmd->cmd_list, &vha->hw->tgt.q_full_list);
5451 vha->hw->tgt.num_qfull_cmds_alloc++;
5452 if (vha->hw->tgt.num_qfull_cmds_alloc >
5453 vha->qla_stats.stat_max_qfull_cmds_alloc)
5454 vha->qla_stats.stat_max_qfull_cmds_alloc =
5455 vha->hw->tgt.num_qfull_cmds_alloc;
5456 spin_unlock_irqrestore(&vha->hw->tgt.q_full_lock, flags);
5460 qlt_free_qfull_cmds(struct qla_qpair *qpair)
5462 struct scsi_qla_host *vha = qpair->vha;
5463 struct qla_hw_data *ha = vha->hw;
5464 unsigned long flags;
5465 struct qla_tgt_cmd *cmd, *tcmd;
5466 struct list_head free_list, q_full_list;
5469 if (list_empty(&ha->tgt.q_full_list))
5472 INIT_LIST_HEAD(&free_list);
5473 INIT_LIST_HEAD(&q_full_list);
5475 spin_lock_irqsave(&vha->hw->tgt.q_full_lock, flags);
5476 if (list_empty(&ha->tgt.q_full_list)) {
5477 spin_unlock_irqrestore(&vha->hw->tgt.q_full_lock, flags);
5481 list_splice_init(&vha->hw->tgt.q_full_list, &q_full_list);
5482 spin_unlock_irqrestore(&vha->hw->tgt.q_full_lock, flags);
5484 spin_lock_irqsave(qpair->qp_lock_ptr, flags);
5485 list_for_each_entry_safe(cmd, tcmd, &q_full_list, cmd_list) {
5487 /* cmd->state is a borrowed field to hold status */
5488 rc = __qlt_send_busy(qpair, &cmd->atio, cmd->state);
5489 else if (cmd->term_exchg)
5490 rc = __qlt_send_term_exchange(qpair, NULL, &cmd->atio);
5496 ql_dbg(ql_dbg_io, vha, 0x3006,
5497 "%s: busy sent for ox_id[%04x]\n", __func__,
5498 be16_to_cpu(cmd->atio.u.isp24.fcp_hdr.ox_id));
5499 else if (cmd->term_exchg)
5500 ql_dbg(ql_dbg_io, vha, 0x3007,
5501 "%s: Term exchg sent for ox_id[%04x]\n", __func__,
5502 be16_to_cpu(cmd->atio.u.isp24.fcp_hdr.ox_id));
5504 ql_dbg(ql_dbg_io, vha, 0x3008,
5505 "%s: Unexpected cmd in QFull list %p\n", __func__,
5508 list_del(&cmd->cmd_list);
5509 list_add_tail(&cmd->cmd_list, &free_list);
5511 /* piggy back on hardware_lock for protection */
5512 vha->hw->tgt.num_qfull_cmds_alloc--;
5514 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
5518 list_for_each_entry_safe(cmd, tcmd, &free_list, cmd_list) {
5519 list_del(&cmd->cmd_list);
5520 /* This cmd was never sent to TCM. There is no need
5521 * to schedule free or call free_cmd
5526 if (!list_empty(&q_full_list)) {
5527 spin_lock_irqsave(&vha->hw->tgt.q_full_lock, flags);
5528 list_splice(&q_full_list, &vha->hw->tgt.q_full_list);
5529 spin_unlock_irqrestore(&vha->hw->tgt.q_full_lock, flags);
5536 qlt_send_busy(struct qla_qpair *qpair, struct atio_from_isp *atio,
5540 struct scsi_qla_host *vha = qpair->vha;
5542 rc = __qlt_send_busy(qpair, atio, status);
5544 qlt_alloc_qfull_cmd(vha, atio, status, 1);
5548 qlt_chk_qfull_thresh_hold(struct scsi_qla_host *vha, struct qla_qpair *qpair,
5549 struct atio_from_isp *atio, uint8_t ha_locked)
5551 struct qla_hw_data *ha = vha->hw;
5552 unsigned long flags;
5554 if (ha->tgt.num_pend_cmds < Q_FULL_THRESH_HOLD(ha))
5558 spin_lock_irqsave(&ha->hardware_lock, flags);
5559 qlt_send_busy(qpair, atio, qla_sam_status);
5561 spin_unlock_irqrestore(&ha->hardware_lock, flags);
5566 /* ha->hardware_lock supposed to be held on entry */
5567 /* called via callback from qla2xxx */
5568 static void qlt_24xx_atio_pkt(struct scsi_qla_host *vha,
5569 struct atio_from_isp *atio, uint8_t ha_locked)
5571 struct qla_hw_data *ha = vha->hw;
5572 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
5574 unsigned long flags = 0;
5576 if (unlikely(tgt == NULL)) {
5577 ql_dbg(ql_dbg_tgt, vha, 0x3064,
5578 "ATIO pkt, but no tgt (ha %p)", ha);
5582 * In tgt_stop mode we also should allow all requests to pass.
5583 * Otherwise, some commands can stuck.
5586 tgt->atio_irq_cmd_count++;
5588 switch (atio->u.raw.entry_type) {
5590 if (unlikely(atio->u.isp24.exchange_addr ==
5591 ATIO_EXCHANGE_ADDRESS_UNKNOWN)) {
5592 ql_dbg(ql_dbg_io, vha, 0x3065,
5593 "qla_target(%d): ATIO_TYPE7 "
5594 "received with UNKNOWN exchange address, "
5595 "sending QUEUE_FULL\n", vha->vp_idx);
5597 spin_lock_irqsave(&ha->hardware_lock, flags);
5598 qlt_send_busy(ha->base_qpair, atio, qla_sam_status);
5600 spin_unlock_irqrestore(&ha->hardware_lock,
5605 if (likely(atio->u.isp24.fcp_cmnd.task_mgmt_flags == 0)) {
5606 rc = qlt_chk_qfull_thresh_hold(vha, ha->base_qpair,
5609 tgt->atio_irq_cmd_count--;
5612 rc = qlt_handle_cmd_for_atio(vha, atio);
5614 rc = qlt_handle_task_mgmt(vha, atio);
5616 if (unlikely(rc != 0)) {
5618 spin_lock_irqsave(&ha->hardware_lock, flags);
5621 ql_dbg(ql_dbg_tgt, vha, 0xe05f,
5622 "qla_target: Unable to send command to target\n");
5625 ql_dbg(ql_dbg_tgt, vha, 0xe05f,
5626 "qla_target: Unable to send command to target, sending TERM EXCHANGE for rsp\n");
5627 qlt_send_term_exchange(ha->base_qpair, NULL,
5631 ql_dbg(ql_dbg_tgt, vha, 0xe060,
5632 "qla_target(%d): Unable to send command to target, sending BUSY status\n",
5634 qlt_send_busy(ha->base_qpair, atio,
5638 ql_dbg(ql_dbg_tgt, vha, 0xe060,
5639 "qla_target(%d): Unable to send command to target, sending BUSY status\n",
5641 qlt_send_busy(ha->base_qpair, atio,
5646 spin_unlock_irqrestore(&ha->hardware_lock,
5651 case IMMED_NOTIFY_TYPE:
5653 if (unlikely(atio->u.isp2x.entry_status != 0)) {
5654 ql_dbg(ql_dbg_tgt, vha, 0xe05b,
5655 "qla_target(%d): Received ATIO packet %x "
5656 "with error status %x\n", vha->vp_idx,
5657 atio->u.raw.entry_type,
5658 atio->u.isp2x.entry_status);
5661 ql_dbg(ql_dbg_tgt, vha, 0xe02e, "%s", "IMMED_NOTIFY ATIO");
5664 spin_lock_irqsave(&ha->hardware_lock, flags);
5665 qlt_handle_imm_notify(vha, (struct imm_ntfy_from_isp *)atio);
5667 spin_unlock_irqrestore(&ha->hardware_lock, flags);
5672 ql_dbg(ql_dbg_tgt, vha, 0xe05c,
5673 "qla_target(%d): Received unknown ATIO atio "
5674 "type %x\n", vha->vp_idx, atio->u.raw.entry_type);
5678 tgt->atio_irq_cmd_count--;
5682 * qpair lock is assume to be held
5683 * rc = 0 : send terminate & abts respond
5684 * rc != 0: do not send term & abts respond
5686 static int qlt_chk_unresolv_exchg(struct scsi_qla_host *vha,
5687 struct qla_qpair *qpair, struct abts_resp_from_24xx_fw *entry)
5689 struct qla_hw_data *ha = vha->hw;
5693 * Detect unresolved exchange. If the same ABTS is unable
5694 * to terminate an existing command and the same ABTS loops
5695 * between FW & Driver, then force FW dump. Under 1 jiff,
5696 * we should see multiple loops.
5698 if (qpair->retry_term_exchg_addr == entry->exchange_addr_to_abort &&
5699 qpair->retry_term_jiff == jiffies) {
5700 /* found existing exchange */
5701 qpair->retry_term_cnt++;
5702 if (qpair->retry_term_cnt >= 5) {
5704 qpair->retry_term_cnt = 0;
5705 ql_log(ql_log_warn, vha, 0xffff,
5706 "Unable to send ABTS Respond. Dumping firmware.\n");
5707 ql_dump_buffer(ql_dbg_tgt_mgt + ql_dbg_buffer,
5708 vha, 0xffff, (uint8_t *)entry, sizeof(*entry));
5710 if (qpair == ha->base_qpair)
5711 ha->isp_ops->fw_dump(vha, 1);
5713 ha->isp_ops->fw_dump(vha, 0);
5715 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
5716 qla2xxx_wake_dpc(vha);
5718 } else if (qpair->retry_term_jiff != jiffies) {
5719 qpair->retry_term_exchg_addr = entry->exchange_addr_to_abort;
5720 qpair->retry_term_cnt = 0;
5721 qpair->retry_term_jiff = jiffies;
5728 static void qlt_handle_abts_completion(struct scsi_qla_host *vha,
5729 struct rsp_que *rsp, response_t *pkt)
5731 struct abts_resp_from_24xx_fw *entry =
5732 (struct abts_resp_from_24xx_fw *)pkt;
5733 u32 h = pkt->handle & ~QLA_TGT_HANDLE_MASK;
5734 struct qla_tgt_mgmt_cmd *mcmd;
5735 struct qla_hw_data *ha = vha->hw;
5737 mcmd = qlt_ctio_to_cmd(vha, rsp, pkt->handle, pkt);
5738 if (mcmd == NULL && h != QLA_TGT_SKIP_HANDLE) {
5739 ql_dbg(ql_dbg_async, vha, 0xe064,
5740 "qla_target(%d): ABTS Comp without mcmd\n",
5747 vha->vha_tgt.qla_tgt->abts_resp_expected--;
5749 ql_dbg(ql_dbg_tgt, vha, 0xe038,
5750 "ABTS_RESP_24XX: compl_status %x\n",
5751 entry->compl_status);
5753 if (le16_to_cpu(entry->compl_status) != ABTS_RESP_COMPL_SUCCESS) {
5754 if ((entry->error_subcode1 == 0x1E) &&
5755 (entry->error_subcode2 == 0)) {
5756 if (qlt_chk_unresolv_exchg(vha, rsp->qpair, entry)) {
5757 ha->tgt.tgt_ops->free_mcmd(mcmd);
5760 qlt_24xx_retry_term_exchange(vha, rsp->qpair,
5763 ql_dbg(ql_dbg_tgt, vha, 0xe063,
5764 "qla_target(%d): ABTS_RESP_24XX failed %x (subcode %x:%x)",
5765 vha->vp_idx, entry->compl_status,
5766 entry->error_subcode1,
5767 entry->error_subcode2);
5768 ha->tgt.tgt_ops->free_mcmd(mcmd);
5771 ha->tgt.tgt_ops->free_mcmd(mcmd);
5775 /* ha->hardware_lock supposed to be held on entry */
5776 /* called via callback from qla2xxx */
5777 static void qlt_response_pkt(struct scsi_qla_host *vha,
5778 struct rsp_que *rsp, response_t *pkt)
5780 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
5782 if (unlikely(tgt == NULL)) {
5783 ql_dbg(ql_dbg_tgt, vha, 0xe05d,
5784 "qla_target(%d): Response pkt %x received, but no tgt (ha %p)\n",
5785 vha->vp_idx, pkt->entry_type, vha->hw);
5790 * In tgt_stop mode we also should allow all requests to pass.
5791 * Otherwise, some commands can stuck.
5794 switch (pkt->entry_type) {
5798 struct ctio7_from_24xx *entry = (struct ctio7_from_24xx *)pkt;
5800 qlt_do_ctio_completion(vha, rsp, entry->handle,
5801 le16_to_cpu(entry->status)|(pkt->entry_status << 16),
5806 case ACCEPT_TGT_IO_TYPE:
5808 struct atio_from_isp *atio = (struct atio_from_isp *)pkt;
5811 if (atio->u.isp2x.status !=
5812 cpu_to_le16(ATIO_CDB_VALID)) {
5813 ql_dbg(ql_dbg_tgt, vha, 0xe05e,
5814 "qla_target(%d): ATIO with error "
5815 "status %x received\n", vha->vp_idx,
5816 le16_to_cpu(atio->u.isp2x.status));
5820 rc = qlt_chk_qfull_thresh_hold(vha, rsp->qpair, atio, 1);
5824 rc = qlt_handle_cmd_for_atio(vha, atio);
5825 if (unlikely(rc != 0)) {
5828 ql_dbg(ql_dbg_tgt, vha, 0xe05f,
5829 "qla_target: Unable to send command to target\n");
5832 ql_dbg(ql_dbg_tgt, vha, 0xe05f,
5833 "qla_target: Unable to send command to target, sending TERM EXCHANGE for rsp\n");
5834 qlt_send_term_exchange(rsp->qpair, NULL,
5838 ql_dbg(ql_dbg_tgt, vha, 0xe060,
5839 "qla_target(%d): Unable to send command to target, sending BUSY status\n",
5841 qlt_send_busy(rsp->qpair, atio,
5845 ql_dbg(ql_dbg_tgt, vha, 0xe060,
5846 "qla_target(%d): Unable to send command to target, sending BUSY status\n",
5848 qlt_send_busy(rsp->qpair, atio,
5856 case CONTINUE_TGT_IO_TYPE:
5858 struct ctio_to_2xxx *entry = (struct ctio_to_2xxx *)pkt;
5860 qlt_do_ctio_completion(vha, rsp, entry->handle,
5861 le16_to_cpu(entry->status)|(pkt->entry_status << 16),
5868 struct ctio_to_2xxx *entry = (struct ctio_to_2xxx *)pkt;
5870 qlt_do_ctio_completion(vha, rsp, entry->handle,
5871 le16_to_cpu(entry->status)|(pkt->entry_status << 16),
5876 case IMMED_NOTIFY_TYPE:
5877 ql_dbg(ql_dbg_tgt, vha, 0xe035, "%s", "IMMED_NOTIFY\n");
5878 qlt_handle_imm_notify(vha, (struct imm_ntfy_from_isp *)pkt);
5881 case NOTIFY_ACK_TYPE:
5882 if (tgt->notify_ack_expected > 0) {
5883 struct nack_to_isp *entry = (struct nack_to_isp *)pkt;
5885 ql_dbg(ql_dbg_tgt, vha, 0xe036,
5886 "NOTIFY_ACK seq %08x status %x\n",
5887 le16_to_cpu(entry->u.isp2x.seq_id),
5888 le16_to_cpu(entry->u.isp2x.status));
5889 tgt->notify_ack_expected--;
5890 if (entry->u.isp2x.status !=
5891 cpu_to_le16(NOTIFY_ACK_SUCCESS)) {
5892 ql_dbg(ql_dbg_tgt, vha, 0xe061,
5893 "qla_target(%d): NOTIFY_ACK "
5894 "failed %x\n", vha->vp_idx,
5895 le16_to_cpu(entry->u.isp2x.status));
5898 ql_dbg(ql_dbg_tgt, vha, 0xe062,
5899 "qla_target(%d): Unexpected NOTIFY_ACK received\n",
5904 case ABTS_RECV_24XX:
5905 ql_dbg(ql_dbg_tgt, vha, 0xe037,
5906 "ABTS_RECV_24XX: instance %d\n", vha->vp_idx);
5907 qlt_24xx_handle_abts(vha, (struct abts_recv_from_24xx *)pkt);
5910 case ABTS_RESP_24XX:
5911 if (tgt->abts_resp_expected > 0) {
5912 qlt_handle_abts_completion(vha, rsp, pkt);
5914 ql_dbg(ql_dbg_tgt, vha, 0xe064,
5915 "qla_target(%d): Unexpected ABTS_RESP_24XX "
5916 "received\n", vha->vp_idx);
5921 ql_dbg(ql_dbg_tgt, vha, 0xe065,
5922 "qla_target(%d): Received unknown response pkt "
5923 "type %x\n", vha->vp_idx, pkt->entry_type);
5930 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
5932 void qlt_async_event(uint16_t code, struct scsi_qla_host *vha,
5935 struct qla_hw_data *ha = vha->hw;
5936 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
5939 if (!tgt || tgt->tgt_stop || tgt->tgt_stopped)
5942 if (((code == MBA_POINT_TO_POINT) || (code == MBA_CHG_IN_CONNECTION)) &&
5946 * In tgt_stop mode we also should allow all requests to pass.
5947 * Otherwise, some commands can stuck.
5952 case MBA_RESET: /* Reset */
5953 case MBA_SYSTEM_ERR: /* System Error */
5954 case MBA_REQ_TRANSFER_ERR: /* Request Transfer Error */
5955 case MBA_RSP_TRANSFER_ERR: /* Response Transfer Error */
5956 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03a,
5957 "qla_target(%d): System error async event %#x "
5958 "occurred", vha->vp_idx, code);
5960 case MBA_WAKEUP_THRES: /* Request Queue Wake-up. */
5961 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
5966 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03b,
5967 "qla_target(%d): Async LOOP_UP occurred "
5968 "(m[0]=%x, m[1]=%x, m[2]=%x, m[3]=%x)", vha->vp_idx,
5969 le16_to_cpu(mailbox[0]), le16_to_cpu(mailbox[1]),
5970 le16_to_cpu(mailbox[2]), le16_to_cpu(mailbox[3]));
5971 if (tgt->link_reinit_iocb_pending) {
5972 qlt_send_notify_ack(ha->base_qpair,
5973 (void *)&tgt->link_reinit_iocb,
5975 tgt->link_reinit_iocb_pending = 0;
5980 case MBA_LIP_OCCURRED:
5983 case MBA_RSCN_UPDATE:
5984 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03c,
5985 "qla_target(%d): Async event %#x occurred "
5986 "(m[0]=%x, m[1]=%x, m[2]=%x, m[3]=%x)", vha->vp_idx, code,
5987 le16_to_cpu(mailbox[0]), le16_to_cpu(mailbox[1]),
5988 le16_to_cpu(mailbox[2]), le16_to_cpu(mailbox[3]));
5991 case MBA_REJECTED_FCP_CMD:
5992 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf017,
5993 "qla_target(%d): Async event LS_REJECT occurred (m[0]=%x, m[1]=%x, m[2]=%x, m[3]=%x)",
5995 le16_to_cpu(mailbox[0]), le16_to_cpu(mailbox[1]),
5996 le16_to_cpu(mailbox[2]), le16_to_cpu(mailbox[3]));
5998 if (le16_to_cpu(mailbox[3]) == 1) {
5999 /* exchange starvation. */
6000 vha->hw->exch_starvation++;
6001 if (vha->hw->exch_starvation > 5) {
6002 ql_log(ql_log_warn, vha, 0xd03a,
6003 "Exchange starvation-. Resetting RISC\n");
6005 vha->hw->exch_starvation = 0;
6006 if (IS_P3P_TYPE(vha->hw))
6007 set_bit(FCOE_CTX_RESET_NEEDED,
6010 set_bit(ISP_ABORT_NEEDED,
6012 qla2xxx_wake_dpc(vha);
6017 case MBA_PORT_UPDATE:
6018 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03d,
6019 "qla_target(%d): Port update async event %#x "
6020 "occurred: updating the ports database (m[0]=%x, m[1]=%x, "
6021 "m[2]=%x, m[3]=%x)", vha->vp_idx, code,
6022 le16_to_cpu(mailbox[0]), le16_to_cpu(mailbox[1]),
6023 le16_to_cpu(mailbox[2]), le16_to_cpu(mailbox[3]));
6025 login_code = le16_to_cpu(mailbox[2]);
6026 if (login_code == 0x4) {
6027 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03e,
6028 "Async MB 2: Got PLOGI Complete\n");
6029 vha->hw->exch_starvation = 0;
6030 } else if (login_code == 0x7)
6031 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03f,
6032 "Async MB 2: Port Logged Out\n");
6040 static fc_port_t *qlt_get_port_database(struct scsi_qla_host *vha,
6043 fc_port_t *fcport, *tfcp, *del;
6045 unsigned long flags;
6048 fcport = qla2x00_alloc_fcport(vha, GFP_KERNEL);
6050 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06f,
6051 "qla_target(%d): Allocation of tmp FC port failed",
6056 fcport->loop_id = loop_id;
6058 rc = qla24xx_gpdb_wait(vha, fcport, 0);
6059 if (rc != QLA_SUCCESS) {
6060 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf070,
6061 "qla_target(%d): Failed to retrieve fcport "
6062 "information -- get_port_database() returned %x "
6063 "(loop_id=0x%04x)", vha->vp_idx, rc, loop_id);
6069 spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
6070 tfcp = qla2x00_find_fcport_by_wwpn(vha, fcport->port_name, 1);
6073 tfcp->d_id = fcport->d_id;
6074 tfcp->port_type = fcport->port_type;
6075 tfcp->supported_classes = fcport->supported_classes;
6076 tfcp->flags |= fcport->flags;
6077 tfcp->scan_state = QLA_FCPORT_FOUND;
6082 if (vha->hw->current_topology == ISP_CFG_F)
6083 fcport->flags |= FCF_FABRIC_DEVICE;
6085 list_add_tail(&fcport->list, &vha->vp_fcports);
6086 if (!IS_SW_RESV_ADDR(fcport->d_id))
6087 vha->fcport_count++;
6088 fcport->login_gen++;
6089 fcport->disc_state = DSC_LOGIN_COMPLETE;
6090 fcport->login_succ = 1;
6094 fcport->deleted = 0;
6095 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
6097 switch (vha->host->active_mode) {
6098 case MODE_INITIATOR:
6101 if (!IS_IIDMA_CAPABLE(vha->hw) || !vha->hw->flags.gpsc_supported) {
6102 qla24xx_sched_upd_fcport(fcport);
6104 ql_dbg(ql_dbg_disc, vha, 0x20ff,
6105 "%s %d %8phC post gpsc fcp_cnt %d\n",
6106 __func__, __LINE__, fcport->port_name, vha->fcport_count);
6107 qla24xx_post_gpsc_work(vha, fcport);
6117 qla2x00_free_fcport(del);
6122 /* Must be called under tgt_mutex */
6123 static struct fc_port *qlt_make_local_sess(struct scsi_qla_host *vha,
6126 struct fc_port *sess = NULL;
6127 fc_port_t *fcport = NULL;
6128 int rc, global_resets;
6129 uint16_t loop_id = 0;
6131 if ((s_id[0] == 0xFF) && (s_id[1] == 0xFC)) {
6133 * This is Domain Controller, so it should be
6134 * OK to drop SCSI commands from it.
6136 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf042,
6137 "Unable to find initiator with S_ID %x:%x:%x",
6138 s_id[0], s_id[1], s_id[2]);
6142 mutex_lock(&vha->vha_tgt.tgt_mutex);
6146 atomic_read(&vha->vha_tgt.qla_tgt->tgt_global_resets_count);
6148 rc = qla24xx_get_loop_id(vha, s_id, &loop_id);
6150 mutex_unlock(&vha->vha_tgt.tgt_mutex);
6152 ql_log(ql_log_info, vha, 0xf071,
6153 "qla_target(%d): Unable to find "
6154 "initiator with S_ID %x:%x:%x",
6155 vha->vp_idx, s_id[0], s_id[1],
6158 if (rc == -ENOENT) {
6159 qlt_port_logo_t logo;
6161 sid_to_portid(s_id, &logo.id);
6163 qlt_send_first_logo(vha, &logo);
6169 fcport = qlt_get_port_database(vha, loop_id);
6171 mutex_unlock(&vha->vha_tgt.tgt_mutex);
6175 if (global_resets !=
6176 atomic_read(&vha->vha_tgt.qla_tgt->tgt_global_resets_count)) {
6177 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf043,
6178 "qla_target(%d): global reset during session discovery "
6179 "(counter was %d, new %d), retrying", vha->vp_idx,
6181 atomic_read(&vha->vha_tgt.
6182 qla_tgt->tgt_global_resets_count));
6186 sess = qlt_create_sess(vha, fcport, true);
6188 mutex_unlock(&vha->vha_tgt.tgt_mutex);
6193 static void qlt_abort_work(struct qla_tgt *tgt,
6194 struct qla_tgt_sess_work_param *prm)
6196 struct scsi_qla_host *vha = tgt->vha;
6197 struct qla_hw_data *ha = vha->hw;
6198 struct fc_port *sess = NULL;
6199 unsigned long flags = 0, flags2 = 0;
6204 spin_lock_irqsave(&ha->tgt.sess_lock, flags2);
6209 s_id[0] = prm->abts.fcp_hdr_le.s_id[2];
6210 s_id[1] = prm->abts.fcp_hdr_le.s_id[1];
6211 s_id[2] = prm->abts.fcp_hdr_le.s_id[0];
6213 sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha,
6214 (unsigned char *)&be_s_id);
6216 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags2);
6218 sess = qlt_make_local_sess(vha, s_id);
6219 /* sess has got an extra creation ref */
6221 spin_lock_irqsave(&ha->tgt.sess_lock, flags2);
6225 if (sess->deleted) {
6230 if (!kref_get_unless_zero(&sess->sess_kref)) {
6231 ql_dbg(ql_dbg_tgt_tmr, vha, 0xf01c,
6232 "%s: kref_get fail %8phC \n",
6233 __func__, sess->port_name);
6239 rc = __qlt_24xx_handle_abts(vha, &prm->abts, sess);
6240 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags2);
6242 ha->tgt.tgt_ops->put_sess(sess);
6249 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags2);
6252 ha->tgt.tgt_ops->put_sess(sess);
6255 spin_lock_irqsave(&ha->hardware_lock, flags);
6256 qlt_24xx_send_abts_resp(ha->base_qpair, &prm->abts,
6257 FCP_TMF_REJECTED, false);
6258 spin_unlock_irqrestore(&ha->hardware_lock, flags);
6261 static void qlt_tmr_work(struct qla_tgt *tgt,
6262 struct qla_tgt_sess_work_param *prm)
6264 struct atio_from_isp *a = &prm->tm_iocb2;
6265 struct scsi_qla_host *vha = tgt->vha;
6266 struct qla_hw_data *ha = vha->hw;
6267 struct fc_port *sess;
6268 unsigned long flags;
6269 uint8_t *s_id = NULL; /* to hide compiler warnings */
6275 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
6280 s_id = prm->tm_iocb2.u.isp24.fcp_hdr.s_id;
6281 sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha, s_id);
6283 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
6285 sess = qlt_make_local_sess(vha, s_id);
6286 /* sess has got an extra creation ref */
6288 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
6292 if (sess->deleted) {
6296 if (!kref_get_unless_zero(&sess->sess_kref)) {
6297 ql_dbg(ql_dbg_tgt_tmr, vha, 0xf020,
6298 "%s: kref_get fail %8phC\n",
6299 __func__, sess->port_name);
6305 fn = a->u.isp24.fcp_cmnd.task_mgmt_flags;
6307 scsilun_to_int((struct scsi_lun *)&a->u.isp24.fcp_cmnd.lun);
6309 rc = qlt_issue_task_mgmt(sess, unpacked_lun, fn, iocb, 0);
6310 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
6312 ha->tgt.tgt_ops->put_sess(sess);
6319 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
6321 qlt_send_term_exchange(ha->base_qpair, NULL, &prm->tm_iocb2, 1, 0);
6324 static void qlt_sess_work_fn(struct work_struct *work)
6326 struct qla_tgt *tgt = container_of(work, struct qla_tgt, sess_work);
6327 struct scsi_qla_host *vha = tgt->vha;
6328 unsigned long flags;
6330 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf000, "Sess work (tgt %p)", tgt);
6332 spin_lock_irqsave(&tgt->sess_work_lock, flags);
6333 while (!list_empty(&tgt->sess_works_list)) {
6334 struct qla_tgt_sess_work_param *prm = list_entry(
6335 tgt->sess_works_list.next, typeof(*prm),
6336 sess_works_list_entry);
6339 * This work can be scheduled on several CPUs at time, so we
6340 * must delete the entry to eliminate double processing
6342 list_del(&prm->sess_works_list_entry);
6344 spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
6346 switch (prm->type) {
6347 case QLA_TGT_SESS_WORK_ABORT:
6348 qlt_abort_work(tgt, prm);
6350 case QLA_TGT_SESS_WORK_TM:
6351 qlt_tmr_work(tgt, prm);
6358 spin_lock_irqsave(&tgt->sess_work_lock, flags);
6362 spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
6365 /* Must be called under tgt_host_action_mutex */
6366 int qlt_add_target(struct qla_hw_data *ha, struct scsi_qla_host *base_vha)
6368 struct qla_tgt *tgt;
6370 struct qla_qpair_hint *h;
6372 if (!QLA_TGT_MODE_ENABLED())
6375 if (!IS_TGT_MODE_CAPABLE(ha)) {
6376 ql_log(ql_log_warn, base_vha, 0xe070,
6377 "This adapter does not support target mode.\n");
6381 ql_dbg(ql_dbg_tgt, base_vha, 0xe03b,
6382 "Registering target for host %ld(%p).\n", base_vha->host_no, ha);
6384 BUG_ON(base_vha->vha_tgt.qla_tgt != NULL);
6386 tgt = kzalloc(sizeof(struct qla_tgt), GFP_KERNEL);
6388 ql_dbg(ql_dbg_tgt, base_vha, 0xe066,
6389 "Unable to allocate struct qla_tgt\n");
6393 tgt->qphints = kcalloc(ha->max_qpairs + 1,
6394 sizeof(struct qla_qpair_hint),
6396 if (!tgt->qphints) {
6398 ql_log(ql_log_warn, base_vha, 0x0197,
6399 "Unable to allocate qpair hints.\n");
6403 if (!(base_vha->host->hostt->supported_mode & MODE_TARGET))
6404 base_vha->host->hostt->supported_mode |= MODE_TARGET;
6406 rc = btree_init64(&tgt->lun_qpair_map);
6408 kfree(tgt->qphints);
6410 ql_log(ql_log_info, base_vha, 0x0198,
6411 "Unable to initialize lun_qpair_map btree\n");
6414 h = &tgt->qphints[0];
6415 h->qpair = ha->base_qpair;
6416 INIT_LIST_HEAD(&h->hint_elem);
6417 h->cpuid = ha->base_qpair->cpuid;
6418 list_add_tail(&h->hint_elem, &ha->base_qpair->hints_list);
6420 for (i = 0; i < ha->max_qpairs; i++) {
6421 unsigned long flags;
6423 struct qla_qpair *qpair = ha->queue_pair_map[i];
6425 h = &tgt->qphints[i + 1];
6426 INIT_LIST_HEAD(&h->hint_elem);
6429 spin_lock_irqsave(qpair->qp_lock_ptr, flags);
6430 list_add_tail(&h->hint_elem, &qpair->hints_list);
6431 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
6432 h->cpuid = qpair->cpuid;
6437 tgt->vha = base_vha;
6438 init_waitqueue_head(&tgt->waitQ);
6439 INIT_LIST_HEAD(&tgt->del_sess_list);
6440 spin_lock_init(&tgt->sess_work_lock);
6441 INIT_WORK(&tgt->sess_work, qlt_sess_work_fn);
6442 INIT_LIST_HEAD(&tgt->sess_works_list);
6443 atomic_set(&tgt->tgt_global_resets_count, 0);
6445 base_vha->vha_tgt.qla_tgt = tgt;
6447 ql_dbg(ql_dbg_tgt, base_vha, 0xe067,
6448 "qla_target(%d): using 64 Bit PCI addressing",
6451 tgt->sg_tablesize = QLA_TGT_MAX_SG_24XX(base_vha->req->length - 3);
6453 mutex_lock(&qla_tgt_mutex);
6454 list_add_tail(&tgt->tgt_list_entry, &qla_tgt_glist);
6455 mutex_unlock(&qla_tgt_mutex);
6457 if (ha->tgt.tgt_ops && ha->tgt.tgt_ops->add_target)
6458 ha->tgt.tgt_ops->add_target(base_vha);
6463 /* Must be called under tgt_host_action_mutex */
6464 int qlt_remove_target(struct qla_hw_data *ha, struct scsi_qla_host *vha)
6466 if (!vha->vha_tgt.qla_tgt)
6469 if (vha->fc_vport) {
6470 qlt_release(vha->vha_tgt.qla_tgt);
6474 /* free left over qfull cmds */
6475 qlt_init_term_exchange(vha);
6477 ql_dbg(ql_dbg_tgt, vha, 0xe03c, "Unregistering target for host %ld(%p)",
6479 qlt_release(vha->vha_tgt.qla_tgt);
6484 void qlt_remove_target_resources(struct qla_hw_data *ha)
6486 struct scsi_qla_host *node;
6489 btree_for_each_safe32(&ha->tgt.host_map, key, node)
6490 btree_remove32(&ha->tgt.host_map, key);
6492 btree_destroy32(&ha->tgt.host_map);
6495 static void qlt_lport_dump(struct scsi_qla_host *vha, u64 wwpn,
6500 pr_debug("qla2xxx HW vha->node_name: ");
6501 for (i = 0; i < WWN_SIZE; i++)
6502 pr_debug("%02x ", vha->node_name[i]);
6504 pr_debug("qla2xxx HW vha->port_name: ");
6505 for (i = 0; i < WWN_SIZE; i++)
6506 pr_debug("%02x ", vha->port_name[i]);
6509 pr_debug("qla2xxx passed configfs WWPN: ");
6510 put_unaligned_be64(wwpn, b);
6511 for (i = 0; i < WWN_SIZE; i++)
6512 pr_debug("%02x ", b[i]);
6517 * qla_tgt_lport_register - register lport with external module
6519 * @target_lport_ptr: pointer for tcm_qla2xxx specific lport data
6520 * @phys_wwpn: physical port WWPN
6521 * @npiv_wwpn: NPIV WWPN
6522 * @npiv_wwnn: NPIV WWNN
6523 * @callback: lport initialization callback for tcm_qla2xxx code
6525 int qlt_lport_register(void *target_lport_ptr, u64 phys_wwpn,
6526 u64 npiv_wwpn, u64 npiv_wwnn,
6527 int (*callback)(struct scsi_qla_host *, void *, u64, u64))
6529 struct qla_tgt *tgt;
6530 struct scsi_qla_host *vha;
6531 struct qla_hw_data *ha;
6532 struct Scsi_Host *host;
6533 unsigned long flags;
6537 mutex_lock(&qla_tgt_mutex);
6538 list_for_each_entry(tgt, &qla_tgt_glist, tgt_list_entry) {
6546 if (!(host->hostt->supported_mode & MODE_TARGET))
6549 if (vha->qlini_mode == QLA2XXX_INI_MODE_ENABLED)
6552 spin_lock_irqsave(&ha->hardware_lock, flags);
6553 if ((!npiv_wwpn || !npiv_wwnn) && host->active_mode & MODE_TARGET) {
6554 pr_debug("MODE_TARGET already active on qla2xxx(%d)\n",
6556 spin_unlock_irqrestore(&ha->hardware_lock, flags);
6559 if (tgt->tgt_stop) {
6560 pr_debug("MODE_TARGET in shutdown on qla2xxx(%d)\n",
6562 spin_unlock_irqrestore(&ha->hardware_lock, flags);
6565 spin_unlock_irqrestore(&ha->hardware_lock, flags);
6567 if (!scsi_host_get(host)) {
6568 ql_dbg(ql_dbg_tgt, vha, 0xe068,
6569 "Unable to scsi_host_get() for"
6570 " qla2xxx scsi_host\n");
6573 qlt_lport_dump(vha, phys_wwpn, b);
6575 if (memcmp(vha->port_name, b, WWN_SIZE)) {
6576 scsi_host_put(host);
6579 rc = (*callback)(vha, target_lport_ptr, npiv_wwpn, npiv_wwnn);
6581 scsi_host_put(host);
6583 mutex_unlock(&qla_tgt_mutex);
6586 mutex_unlock(&qla_tgt_mutex);
6590 EXPORT_SYMBOL(qlt_lport_register);
6593 * qla_tgt_lport_deregister - Degister lport
6595 * @vha: Registered scsi_qla_host pointer
6597 void qlt_lport_deregister(struct scsi_qla_host *vha)
6599 struct qla_hw_data *ha = vha->hw;
6600 struct Scsi_Host *sh = vha->host;
6602 * Clear the target_lport_ptr qla_target_template pointer in qla_hw_data
6604 vha->vha_tgt.target_lport_ptr = NULL;
6605 ha->tgt.tgt_ops = NULL;
6607 * Release the Scsi_Host reference for the underlying qla2xxx host
6611 EXPORT_SYMBOL(qlt_lport_deregister);
6613 /* Must be called under HW lock */
6614 void qlt_set_mode(struct scsi_qla_host *vha)
6616 switch (vha->qlini_mode) {
6617 case QLA2XXX_INI_MODE_DISABLED:
6618 case QLA2XXX_INI_MODE_EXCLUSIVE:
6619 vha->host->active_mode = MODE_TARGET;
6621 case QLA2XXX_INI_MODE_ENABLED:
6622 vha->host->active_mode = MODE_INITIATOR;
6624 case QLA2XXX_INI_MODE_DUAL:
6625 vha->host->active_mode = MODE_DUAL;
6632 /* Must be called under HW lock */
6633 static void qlt_clear_mode(struct scsi_qla_host *vha)
6635 switch (vha->qlini_mode) {
6636 case QLA2XXX_INI_MODE_DISABLED:
6637 vha->host->active_mode = MODE_UNKNOWN;
6639 case QLA2XXX_INI_MODE_EXCLUSIVE:
6640 vha->host->active_mode = MODE_INITIATOR;
6642 case QLA2XXX_INI_MODE_ENABLED:
6643 case QLA2XXX_INI_MODE_DUAL:
6644 vha->host->active_mode = MODE_INITIATOR;
6652 * qla_tgt_enable_vha - NO LOCK HELD
6654 * host_reset, bring up w/ Target Mode Enabled
6657 qlt_enable_vha(struct scsi_qla_host *vha)
6659 struct qla_hw_data *ha = vha->hw;
6660 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
6661 unsigned long flags;
6662 scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
6665 ql_dbg(ql_dbg_tgt, vha, 0xe069,
6666 "Unable to locate qla_tgt pointer from"
6667 " struct qla_hw_data\n");
6671 if (vha->qlini_mode == QLA2XXX_INI_MODE_ENABLED)
6674 spin_lock_irqsave(&ha->hardware_lock, flags);
6675 tgt->tgt_stopped = 0;
6677 spin_unlock_irqrestore(&ha->hardware_lock, flags);
6679 mutex_lock(&ha->optrom_mutex);
6680 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf021,
6683 qla24xx_disable_vp(vha);
6684 qla24xx_enable_vp(vha);
6686 set_bit(ISP_ABORT_NEEDED, &base_vha->dpc_flags);
6687 qla2xxx_wake_dpc(base_vha);
6688 qla2x00_wait_for_hba_online(base_vha);
6690 mutex_unlock(&ha->optrom_mutex);
6692 EXPORT_SYMBOL(qlt_enable_vha);
6695 * qla_tgt_disable_vha - NO LOCK HELD
6697 * Disable Target Mode and reset the adapter
6699 static void qlt_disable_vha(struct scsi_qla_host *vha)
6701 struct qla_hw_data *ha = vha->hw;
6702 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
6703 unsigned long flags;
6706 ql_dbg(ql_dbg_tgt, vha, 0xe06a,
6707 "Unable to locate qla_tgt pointer from"
6708 " struct qla_hw_data\n");
6713 spin_lock_irqsave(&ha->hardware_lock, flags);
6714 qlt_clear_mode(vha);
6715 spin_unlock_irqrestore(&ha->hardware_lock, flags);
6717 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
6718 qla2xxx_wake_dpc(vha);
6719 qla2x00_wait_for_hba_online(vha);
6723 * Called from qla_init.c:qla24xx_vport_create() contex to setup
6724 * the target mode specific struct scsi_qla_host and struct qla_hw_data
6728 qlt_vport_create(struct scsi_qla_host *vha, struct qla_hw_data *ha)
6730 vha->vha_tgt.qla_tgt = NULL;
6732 mutex_init(&vha->vha_tgt.tgt_mutex);
6733 mutex_init(&vha->vha_tgt.tgt_host_action_mutex);
6735 qlt_clear_mode(vha);
6738 * NOTE: Currently the value is kept the same for <24xx and
6739 * >=24xx ISPs. If it is necessary to change it,
6740 * the check should be added for specific ISPs,
6741 * assigning the value appropriately.
6743 ha->tgt.atio_q_length = ATIO_ENTRY_CNT_24XX;
6745 qlt_add_target(ha, vha);
6749 qlt_rff_id(struct scsi_qla_host *vha)
6753 * FC-4 Feature bit 0 indicates target functionality to the name server.
6755 if (qla_tgt_mode_enabled(vha)) {
6756 fc4_feature = BIT_0;
6757 } else if (qla_ini_mode_enabled(vha)) {
6758 fc4_feature = BIT_1;
6759 } else if (qla_dual_mode_enabled(vha))
6760 fc4_feature = BIT_0 | BIT_1;
6766 * qlt_init_atio_q_entries() - Initializes ATIO queue entries.
6769 * Beginning of ATIO ring has initialization control block already built
6770 * by nvram config routine.
6772 * Returns 0 on success.
6775 qlt_init_atio_q_entries(struct scsi_qla_host *vha)
6777 struct qla_hw_data *ha = vha->hw;
6779 struct atio_from_isp *pkt = (struct atio_from_isp *)ha->tgt.atio_ring;
6781 if (qla_ini_mode_enabled(vha))
6784 for (cnt = 0; cnt < ha->tgt.atio_q_length; cnt++) {
6785 pkt->u.raw.signature = ATIO_PROCESSED;
6792 * qlt_24xx_process_atio_queue() - Process ATIO queue entries.
6793 * @ha: SCSI driver HA context
6796 qlt_24xx_process_atio_queue(struct scsi_qla_host *vha, uint8_t ha_locked)
6798 struct qla_hw_data *ha = vha->hw;
6799 struct atio_from_isp *pkt;
6802 if (!ha->flags.fw_started)
6805 while ((ha->tgt.atio_ring_ptr->signature != ATIO_PROCESSED) ||
6806 fcpcmd_is_corrupted(ha->tgt.atio_ring_ptr)) {
6807 pkt = (struct atio_from_isp *)ha->tgt.atio_ring_ptr;
6808 cnt = pkt->u.raw.entry_count;
6810 if (unlikely(fcpcmd_is_corrupted(ha->tgt.atio_ring_ptr))) {
6812 * This packet is corrupted. The header + payload
6813 * can not be trusted. There is no point in passing
6816 ql_log(ql_log_warn, vha, 0xd03c,
6817 "corrupted fcp frame SID[%3phN] OXID[%04x] EXCG[%x] %64phN\n",
6818 pkt->u.isp24.fcp_hdr.s_id,
6819 be16_to_cpu(pkt->u.isp24.fcp_hdr.ox_id),
6820 le32_to_cpu(pkt->u.isp24.exchange_addr), pkt);
6822 adjust_corrupted_atio(pkt);
6823 qlt_send_term_exchange(ha->base_qpair, NULL, pkt,
6826 qlt_24xx_atio_pkt_all_vps(vha,
6827 (struct atio_from_isp *)pkt, ha_locked);
6830 for (i = 0; i < cnt; i++) {
6831 ha->tgt.atio_ring_index++;
6832 if (ha->tgt.atio_ring_index == ha->tgt.atio_q_length) {
6833 ha->tgt.atio_ring_index = 0;
6834 ha->tgt.atio_ring_ptr = ha->tgt.atio_ring;
6836 ha->tgt.atio_ring_ptr++;
6838 pkt->u.raw.signature = ATIO_PROCESSED;
6839 pkt = (struct atio_from_isp *)ha->tgt.atio_ring_ptr;
6844 /* Adjust ring index */
6845 WRT_REG_DWORD(ISP_ATIO_Q_OUT(vha), ha->tgt.atio_ring_index);
6849 qlt_24xx_config_rings(struct scsi_qla_host *vha)
6851 struct qla_hw_data *ha = vha->hw;
6852 struct qla_msix_entry *msix = &ha->msix_entries[2];
6853 struct init_cb_24xx *icb = (struct init_cb_24xx *)ha->init_cb;
6855 if (!QLA_TGT_MODE_ENABLED())
6858 WRT_REG_DWORD(ISP_ATIO_Q_IN(vha), 0);
6859 WRT_REG_DWORD(ISP_ATIO_Q_OUT(vha), 0);
6860 RD_REG_DWORD(ISP_ATIO_Q_OUT(vha));
6862 if (ha->flags.msix_enabled) {
6863 if (IS_QLA83XX(ha) || IS_QLA27XX(ha) || IS_QLA28XX(ha)) {
6864 if (IS_QLA2071(ha)) {
6865 /* 4 ports Baker: Enable Interrupt Handshake */
6867 icb->firmware_options_2 |= BIT_26;
6869 icb->msix_atio = cpu_to_le16(msix->entry);
6870 icb->firmware_options_2 &= ~BIT_26;
6872 ql_dbg(ql_dbg_init, vha, 0xf072,
6873 "Registering ICB vector 0x%x for atio que.\n",
6878 if (IS_QLA83XX(ha) || IS_QLA27XX(ha) || IS_QLA28XX(ha)) {
6880 icb->firmware_options_2 |= BIT_26;
6881 ql_dbg(ql_dbg_init, vha, 0xf072,
6882 "%s: Use INTx for ATIOQ.\n", __func__);
6888 qlt_24xx_config_nvram_stage1(struct scsi_qla_host *vha, struct nvram_24xx *nv)
6890 struct qla_hw_data *ha = vha->hw;
6893 if (!QLA_TGT_MODE_ENABLED())
6896 if (qla_tgt_mode_enabled(vha) || qla_dual_mode_enabled(vha)) {
6897 if (!ha->tgt.saved_set) {
6898 /* We save only once */
6899 ha->tgt.saved_exchange_count = nv->exchange_count;
6900 ha->tgt.saved_firmware_options_1 =
6901 nv->firmware_options_1;
6902 ha->tgt.saved_firmware_options_2 =
6903 nv->firmware_options_2;
6904 ha->tgt.saved_firmware_options_3 =
6905 nv->firmware_options_3;
6906 ha->tgt.saved_set = 1;
6909 if (qla_tgt_mode_enabled(vha))
6910 nv->exchange_count = cpu_to_le16(0xFFFF);
6912 nv->exchange_count = cpu_to_le16(vha->ql2xexchoffld);
6914 /* Enable target mode */
6915 nv->firmware_options_1 |= cpu_to_le32(BIT_4);
6917 /* Disable ini mode, if requested */
6918 if (qla_tgt_mode_enabled(vha))
6919 nv->firmware_options_1 |= cpu_to_le32(BIT_5);
6921 /* Disable Full Login after LIP */
6922 nv->firmware_options_1 &= cpu_to_le32(~BIT_13);
6923 /* Enable initial LIP */
6924 nv->firmware_options_1 &= cpu_to_le32(~BIT_9);
6925 if (ql2xtgt_tape_enable)
6926 /* Enable FC Tape support */
6927 nv->firmware_options_2 |= cpu_to_le32(BIT_12);
6929 /* Disable FC Tape support */
6930 nv->firmware_options_2 &= cpu_to_le32(~BIT_12);
6932 /* Disable Full Login after LIP */
6933 nv->host_p &= cpu_to_le32(~BIT_10);
6936 * clear BIT 15 explicitly as we have seen at least
6937 * a couple of instances where this was set and this
6938 * was causing the firmware to not be initialized.
6940 nv->firmware_options_1 &= cpu_to_le32(~BIT_15);
6941 /* Enable target PRLI control */
6942 nv->firmware_options_2 |= cpu_to_le32(BIT_14);
6944 if (IS_QLA25XX(ha)) {
6945 /* Change Loop-prefer to Pt-Pt */
6946 tmp = ~(BIT_4|BIT_5|BIT_6);
6947 nv->firmware_options_2 &= cpu_to_le32(tmp);
6949 nv->firmware_options_2 |= cpu_to_le32(tmp);
6952 if (ha->tgt.saved_set) {
6953 nv->exchange_count = ha->tgt.saved_exchange_count;
6954 nv->firmware_options_1 =
6955 ha->tgt.saved_firmware_options_1;
6956 nv->firmware_options_2 =
6957 ha->tgt.saved_firmware_options_2;
6958 nv->firmware_options_3 =
6959 ha->tgt.saved_firmware_options_3;
6964 if (ha->base_qpair->enable_class_2) {
6965 if (vha->flags.init_done)
6966 fc_host_supported_classes(vha->host) =
6967 FC_COS_CLASS2 | FC_COS_CLASS3;
6969 nv->firmware_options_2 |= cpu_to_le32(BIT_8);
6971 if (vha->flags.init_done)
6972 fc_host_supported_classes(vha->host) = FC_COS_CLASS3;
6974 nv->firmware_options_2 &= ~cpu_to_le32(BIT_8);
6979 qlt_24xx_config_nvram_stage2(struct scsi_qla_host *vha,
6980 struct init_cb_24xx *icb)
6982 struct qla_hw_data *ha = vha->hw;
6984 if (!QLA_TGT_MODE_ENABLED())
6987 if (ha->tgt.node_name_set) {
6988 memcpy(icb->node_name, ha->tgt.tgt_node_name, WWN_SIZE);
6989 icb->firmware_options_1 |= cpu_to_le32(BIT_14);
6994 qlt_81xx_config_nvram_stage1(struct scsi_qla_host *vha, struct nvram_81xx *nv)
6996 struct qla_hw_data *ha = vha->hw;
6999 if (!QLA_TGT_MODE_ENABLED())
7002 if (qla_tgt_mode_enabled(vha) || qla_dual_mode_enabled(vha)) {
7003 if (!ha->tgt.saved_set) {
7004 /* We save only once */
7005 ha->tgt.saved_exchange_count = nv->exchange_count;
7006 ha->tgt.saved_firmware_options_1 =
7007 nv->firmware_options_1;
7008 ha->tgt.saved_firmware_options_2 =
7009 nv->firmware_options_2;
7010 ha->tgt.saved_firmware_options_3 =
7011 nv->firmware_options_3;
7012 ha->tgt.saved_set = 1;
7015 if (qla_tgt_mode_enabled(vha))
7016 nv->exchange_count = cpu_to_le16(0xFFFF);
7018 nv->exchange_count = cpu_to_le16(vha->ql2xexchoffld);
7020 /* Enable target mode */
7021 nv->firmware_options_1 |= cpu_to_le32(BIT_4);
7023 /* Disable ini mode, if requested */
7024 if (qla_tgt_mode_enabled(vha))
7025 nv->firmware_options_1 |= cpu_to_le32(BIT_5);
7026 /* Disable Full Login after LIP */
7027 nv->firmware_options_1 &= cpu_to_le32(~BIT_13);
7028 /* Enable initial LIP */
7029 nv->firmware_options_1 &= cpu_to_le32(~BIT_9);
7031 * clear BIT 15 explicitly as we have seen at
7032 * least a couple of instances where this was set
7033 * and this was causing the firmware to not be
7036 nv->firmware_options_1 &= cpu_to_le32(~BIT_15);
7037 if (ql2xtgt_tape_enable)
7038 /* Enable FC tape support */
7039 nv->firmware_options_2 |= cpu_to_le32(BIT_12);
7041 /* Disable FC tape support */
7042 nv->firmware_options_2 &= cpu_to_le32(~BIT_12);
7044 /* Disable Full Login after LIP */
7045 nv->host_p &= cpu_to_le32(~BIT_10);
7046 /* Enable target PRLI control */
7047 nv->firmware_options_2 |= cpu_to_le32(BIT_14);
7049 /* Change Loop-prefer to Pt-Pt */
7050 tmp = ~(BIT_4|BIT_5|BIT_6);
7051 nv->firmware_options_2 &= cpu_to_le32(tmp);
7053 nv->firmware_options_2 |= cpu_to_le32(tmp);
7055 if (ha->tgt.saved_set) {
7056 nv->exchange_count = ha->tgt.saved_exchange_count;
7057 nv->firmware_options_1 =
7058 ha->tgt.saved_firmware_options_1;
7059 nv->firmware_options_2 =
7060 ha->tgt.saved_firmware_options_2;
7061 nv->firmware_options_3 =
7062 ha->tgt.saved_firmware_options_3;
7067 if (ha->base_qpair->enable_class_2) {
7068 if (vha->flags.init_done)
7069 fc_host_supported_classes(vha->host) =
7070 FC_COS_CLASS2 | FC_COS_CLASS3;
7072 nv->firmware_options_2 |= cpu_to_le32(BIT_8);
7074 if (vha->flags.init_done)
7075 fc_host_supported_classes(vha->host) = FC_COS_CLASS3;
7077 nv->firmware_options_2 &= ~cpu_to_le32(BIT_8);
7082 qlt_81xx_config_nvram_stage2(struct scsi_qla_host *vha,
7083 struct init_cb_81xx *icb)
7085 struct qla_hw_data *ha = vha->hw;
7087 if (!QLA_TGT_MODE_ENABLED())
7090 if (ha->tgt.node_name_set) {
7091 memcpy(icb->node_name, ha->tgt.tgt_node_name, WWN_SIZE);
7092 icb->firmware_options_1 |= cpu_to_le32(BIT_14);
7097 qlt_83xx_iospace_config(struct qla_hw_data *ha)
7099 if (!QLA_TGT_MODE_ENABLED())
7102 ha->msix_count += 1; /* For ATIO Q */
7107 qlt_modify_vp_config(struct scsi_qla_host *vha,
7108 struct vp_config_entry_24xx *vpmod)
7110 /* enable target mode. Bit5 = 1 => disable */
7111 if (qla_tgt_mode_enabled(vha) || qla_dual_mode_enabled(vha))
7112 vpmod->options_idx1 &= ~BIT_5;
7114 /* Disable ini mode, if requested. bit4 = 1 => disable */
7115 if (qla_tgt_mode_enabled(vha))
7116 vpmod->options_idx1 &= ~BIT_4;
7120 qlt_probe_one_stage1(struct scsi_qla_host *base_vha, struct qla_hw_data *ha)
7124 if (!QLA_TGT_MODE_ENABLED())
7127 if ((ql2xenablemsix == 0) || IS_QLA83XX(ha) || IS_QLA27XX(ha) ||
7129 ISP_ATIO_Q_IN(base_vha) = &ha->mqiobase->isp25mq.atio_q_in;
7130 ISP_ATIO_Q_OUT(base_vha) = &ha->mqiobase->isp25mq.atio_q_out;
7132 ISP_ATIO_Q_IN(base_vha) = &ha->iobase->isp24.atio_q_in;
7133 ISP_ATIO_Q_OUT(base_vha) = &ha->iobase->isp24.atio_q_out;
7136 mutex_init(&base_vha->vha_tgt.tgt_mutex);
7137 mutex_init(&base_vha->vha_tgt.tgt_host_action_mutex);
7139 INIT_LIST_HEAD(&base_vha->unknown_atio_list);
7140 INIT_DELAYED_WORK(&base_vha->unknown_atio_work,
7141 qlt_unknown_atio_work_fn);
7143 qlt_clear_mode(base_vha);
7145 rc = btree_init32(&ha->tgt.host_map);
7147 ql_log(ql_log_info, base_vha, 0xd03d,
7148 "Unable to initialize ha->host_map btree\n");
7150 qlt_update_vp_map(base_vha, SET_VP_IDX);
7154 qla83xx_msix_atio_q(int irq, void *dev_id)
7156 struct rsp_que *rsp;
7157 scsi_qla_host_t *vha;
7158 struct qla_hw_data *ha;
7159 unsigned long flags;
7161 rsp = (struct rsp_que *) dev_id;
7163 vha = pci_get_drvdata(ha->pdev);
7165 spin_lock_irqsave(&ha->tgt.atio_lock, flags);
7167 qlt_24xx_process_atio_queue(vha, 0);
7169 spin_unlock_irqrestore(&ha->tgt.atio_lock, flags);
7175 qlt_handle_abts_recv_work(struct work_struct *work)
7177 struct qla_tgt_sess_op *op = container_of(work,
7178 struct qla_tgt_sess_op, work);
7179 scsi_qla_host_t *vha = op->vha;
7180 struct qla_hw_data *ha = vha->hw;
7181 unsigned long flags;
7183 if (qla2x00_reset_active(vha) ||
7184 (op->chip_reset != ha->base_qpair->chip_reset))
7187 spin_lock_irqsave(&ha->tgt.atio_lock, flags);
7188 qlt_24xx_process_atio_queue(vha, 0);
7189 spin_unlock_irqrestore(&ha->tgt.atio_lock, flags);
7191 spin_lock_irqsave(&ha->hardware_lock, flags);
7192 qlt_response_pkt_all_vps(vha, op->rsp, (response_t *)&op->atio);
7193 spin_unlock_irqrestore(&ha->hardware_lock, flags);
7199 qlt_handle_abts_recv(struct scsi_qla_host *vha, struct rsp_que *rsp,
7202 struct qla_tgt_sess_op *op;
7204 op = kzalloc(sizeof(*op), GFP_ATOMIC);
7207 /* do not reach for ATIO queue here. This is best effort err
7208 * recovery at this point.
7210 qlt_response_pkt_all_vps(vha, rsp, pkt);
7214 memcpy(&op->atio, pkt, sizeof(*pkt));
7216 op->chip_reset = vha->hw->base_qpair->chip_reset;
7218 INIT_WORK(&op->work, qlt_handle_abts_recv_work);
7219 queue_work(qla_tgt_wq, &op->work);
7224 qlt_mem_alloc(struct qla_hw_data *ha)
7226 if (!QLA_TGT_MODE_ENABLED())
7229 ha->tgt.tgt_vp_map = kcalloc(MAX_MULTI_ID_FABRIC,
7230 sizeof(struct qla_tgt_vp_map),
7232 if (!ha->tgt.tgt_vp_map)
7235 ha->tgt.atio_ring = dma_alloc_coherent(&ha->pdev->dev,
7236 (ha->tgt.atio_q_length + 1) * sizeof(struct atio_from_isp),
7237 &ha->tgt.atio_dma, GFP_KERNEL);
7238 if (!ha->tgt.atio_ring) {
7239 kfree(ha->tgt.tgt_vp_map);
7246 qlt_mem_free(struct qla_hw_data *ha)
7248 if (!QLA_TGT_MODE_ENABLED())
7251 if (ha->tgt.atio_ring) {
7252 dma_free_coherent(&ha->pdev->dev, (ha->tgt.atio_q_length + 1) *
7253 sizeof(struct atio_from_isp), ha->tgt.atio_ring,
7256 ha->tgt.atio_ring = NULL;
7257 ha->tgt.atio_dma = 0;
7258 kfree(ha->tgt.tgt_vp_map);
7259 ha->tgt.tgt_vp_map = NULL;
7262 /* vport_slock to be held by the caller */
7264 qlt_update_vp_map(struct scsi_qla_host *vha, int cmd)
7270 if (!QLA_TGT_MODE_ENABLED())
7273 key = vha->d_id.b24;
7277 vha->hw->tgt.tgt_vp_map[vha->vp_idx].vha = vha;
7280 slot = btree_lookup32(&vha->hw->tgt.host_map, key);
7282 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf018,
7283 "Save vha in host_map %p %06x\n", vha, key);
7284 rc = btree_insert32(&vha->hw->tgt.host_map,
7285 key, vha, GFP_ATOMIC);
7287 ql_log(ql_log_info, vha, 0xd03e,
7288 "Unable to insert s_id into host_map: %06x\n",
7292 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf019,
7293 "replace existing vha in host_map %p %06x\n", vha, key);
7294 btree_update32(&vha->hw->tgt.host_map, key, vha);
7297 vha->hw->tgt.tgt_vp_map[vha->vp_idx].vha = NULL;
7300 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01a,
7301 "clear vha in host_map %p %06x\n", vha, key);
7302 slot = btree_lookup32(&vha->hw->tgt.host_map, key);
7304 btree_remove32(&vha->hw->tgt.host_map, key);
7310 void qlt_update_host_map(struct scsi_qla_host *vha, port_id_t id)
7313 if (!vha->d_id.b24) {
7315 qlt_update_vp_map(vha, SET_AL_PA);
7316 } else if (vha->d_id.b24 != id.b24) {
7317 qlt_update_vp_map(vha, RESET_AL_PA);
7319 qlt_update_vp_map(vha, SET_AL_PA);
7323 static int __init qlt_parse_ini_mode(void)
7325 if (strcasecmp(qlini_mode, QLA2XXX_INI_MODE_STR_EXCLUSIVE) == 0)
7326 ql2x_ini_mode = QLA2XXX_INI_MODE_EXCLUSIVE;
7327 else if (strcasecmp(qlini_mode, QLA2XXX_INI_MODE_STR_DISABLED) == 0)
7328 ql2x_ini_mode = QLA2XXX_INI_MODE_DISABLED;
7329 else if (strcasecmp(qlini_mode, QLA2XXX_INI_MODE_STR_ENABLED) == 0)
7330 ql2x_ini_mode = QLA2XXX_INI_MODE_ENABLED;
7331 else if (strcasecmp(qlini_mode, QLA2XXX_INI_MODE_STR_DUAL) == 0)
7332 ql2x_ini_mode = QLA2XXX_INI_MODE_DUAL;
7339 int __init qlt_init(void)
7343 BUILD_BUG_ON(sizeof(struct ctio7_to_24xx) != 64);
7344 BUILD_BUG_ON(sizeof(struct ctio_to_2xxx) != 64);
7346 if (!qlt_parse_ini_mode()) {
7347 ql_log(ql_log_fatal, NULL, 0xe06b,
7348 "qlt_parse_ini_mode() failed\n");
7352 if (!QLA_TGT_MODE_ENABLED())
7355 qla_tgt_mgmt_cmd_cachep = kmem_cache_create("qla_tgt_mgmt_cmd_cachep",
7356 sizeof(struct qla_tgt_mgmt_cmd), __alignof__(struct
7357 qla_tgt_mgmt_cmd), 0, NULL);
7358 if (!qla_tgt_mgmt_cmd_cachep) {
7359 ql_log(ql_log_fatal, NULL, 0xd04b,
7360 "kmem_cache_create for qla_tgt_mgmt_cmd_cachep failed\n");
7364 qla_tgt_plogi_cachep = kmem_cache_create("qla_tgt_plogi_cachep",
7365 sizeof(struct qlt_plogi_ack_t), __alignof__(struct qlt_plogi_ack_t),
7368 if (!qla_tgt_plogi_cachep) {
7369 ql_log(ql_log_fatal, NULL, 0xe06d,
7370 "kmem_cache_create for qla_tgt_plogi_cachep failed\n");
7372 goto out_mgmt_cmd_cachep;
7375 qla_tgt_mgmt_cmd_mempool = mempool_create(25, mempool_alloc_slab,
7376 mempool_free_slab, qla_tgt_mgmt_cmd_cachep);
7377 if (!qla_tgt_mgmt_cmd_mempool) {
7378 ql_log(ql_log_fatal, NULL, 0xe06e,
7379 "mempool_create for qla_tgt_mgmt_cmd_mempool failed\n");
7381 goto out_plogi_cachep;
7384 qla_tgt_wq = alloc_workqueue("qla_tgt_wq", 0, 0);
7386 ql_log(ql_log_fatal, NULL, 0xe06f,
7387 "alloc_workqueue for qla_tgt_wq failed\n");
7389 goto out_cmd_mempool;
7392 * Return 1 to signal that initiator-mode is being disabled
7394 return (ql2x_ini_mode == QLA2XXX_INI_MODE_DISABLED) ? 1 : 0;
7397 mempool_destroy(qla_tgt_mgmt_cmd_mempool);
7399 kmem_cache_destroy(qla_tgt_plogi_cachep);
7400 out_mgmt_cmd_cachep:
7401 kmem_cache_destroy(qla_tgt_mgmt_cmd_cachep);
7407 if (!QLA_TGT_MODE_ENABLED())
7410 destroy_workqueue(qla_tgt_wq);
7411 mempool_destroy(qla_tgt_mgmt_cmd_mempool);
7412 kmem_cache_destroy(qla_tgt_plogi_cachep);
7413 kmem_cache_destroy(qla_tgt_mgmt_cmd_cachep);