2 * qla_target.c SCSI LLD infrastructure for QLogic 22xx/23xx/24xx/25xx
4 * based on qla2x00t.c code:
6 * Copyright (C) 2004 - 2010 Vladislav Bolkhovitin <vst@vlnb.net>
7 * Copyright (C) 2004 - 2005 Leonid Stoljar
8 * Copyright (C) 2006 Nathaniel Clark <nate@misrule.us>
9 * Copyright (C) 2006 - 2010 ID7 Ltd.
11 * Forward port and refactoring to modern qla2xxx and target/configfs
13 * Copyright (C) 2010-2013 Nicholas A. Bellinger <nab@kernel.org>
15 * This program is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public License
17 * as published by the Free Software Foundation, version 2
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
26 #include <linux/module.h>
27 #include <linux/init.h>
28 #include <linux/types.h>
29 #include <linux/blkdev.h>
30 #include <linux/interrupt.h>
31 #include <linux/pci.h>
32 #include <linux/delay.h>
33 #include <linux/list.h>
34 #include <linux/workqueue.h>
35 #include <asm/unaligned.h>
36 #include <scsi/scsi.h>
37 #include <scsi/scsi_host.h>
38 #include <scsi/scsi_tcq.h>
39 #include <target/target_core_base.h>
40 #include <target/target_core_fabric.h>
43 #include "qla_target.h"
45 static int ql2xtgt_tape_enable;
46 module_param(ql2xtgt_tape_enable, int, S_IRUGO|S_IWUSR);
47 MODULE_PARM_DESC(ql2xtgt_tape_enable,
48 "Enables Sequence level error recovery (aka FC Tape). Default is 0 - no SLER. 1 - Enable SLER.");
50 static char *qlini_mode = QLA2XXX_INI_MODE_STR_ENABLED;
51 module_param(qlini_mode, charp, S_IRUGO);
52 MODULE_PARM_DESC(qlini_mode,
53 "Determines when initiator mode will be enabled. Possible values: "
54 "\"exclusive\" - initiator mode will be enabled on load, "
55 "disabled on enabling target mode and then on disabling target mode "
57 "\"disabled\" - initiator mode will never be enabled; "
58 "\"dual\" - Initiator Modes will be enabled. Target Mode can be activated "
60 "\"enabled\" (default) - initiator mode will always stay enabled.");
62 static int ql_dm_tgt_ex_pct = 0;
63 module_param(ql_dm_tgt_ex_pct, int, S_IRUGO|S_IWUSR);
64 MODULE_PARM_DESC(ql_dm_tgt_ex_pct,
65 "For Dual Mode (qlini_mode=dual), this parameter determines "
66 "the percentage of exchanges/cmds FW will allocate resources "
69 int ql2x_ini_mode = QLA2XXX_INI_MODE_EXCLUSIVE;
71 static int temp_sam_status = SAM_STAT_BUSY;
74 * From scsi/fc/fc_fcp.h
76 enum fcp_resp_rsp_codes {
78 FCP_DATA_LEN_INVALID = 1,
79 FCP_CMND_FIELDS_INVALID = 2,
80 FCP_DATA_PARAM_MISMATCH = 3,
83 FCP_TMF_INVALID_LUN = 9,
87 * fc_pri_ta from scsi/fc/fc_fcp.h
89 #define FCP_PTA_SIMPLE 0 /* simple task attribute */
90 #define FCP_PTA_HEADQ 1 /* head of queue task attribute */
91 #define FCP_PTA_ORDERED 2 /* ordered task attribute */
92 #define FCP_PTA_ACA 4 /* auto. contingent allegiance */
93 #define FCP_PTA_MASK 7 /* mask for task attribute field */
94 #define FCP_PRI_SHIFT 3 /* priority field starts in bit 3 */
95 #define FCP_PRI_RESVD_MASK 0x80 /* reserved bits in priority field */
98 * This driver calls qla2x00_alloc_iocbs() and qla2x00_issue_marker(), which
99 * must be called under HW lock and could unlock/lock it inside.
100 * It isn't an issue, since in the current implementation on the time when
101 * those functions are called:
103 * - Either context is IRQ and only IRQ handler can modify HW data,
104 * including rings related fields,
106 * - Or access to target mode variables from struct qla_tgt doesn't
107 * cross those functions boundaries, except tgt_stop, which
108 * additionally protected by irq_cmd_count.
110 /* Predefs for callbacks handed to qla2xxx LLD */
111 static void qlt_24xx_atio_pkt(struct scsi_qla_host *ha,
112 struct atio_from_isp *pkt, uint8_t);
113 static void qlt_response_pkt(struct scsi_qla_host *ha, response_t *pkt);
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 scsi_qla_host *ha, 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 scsi_qla_host *vha,
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 *);
137 static struct kmem_cache *qla_tgt_mgmt_cmd_cachep;
138 static struct kmem_cache *qla_tgt_plogi_cachep;
139 static mempool_t *qla_tgt_mgmt_cmd_mempool;
140 static struct workqueue_struct *qla_tgt_wq;
141 static DEFINE_MUTEX(qla_tgt_mutex);
142 static LIST_HEAD(qla_tgt_glist);
144 static const char *prot_op_str(u32 prot_op)
147 case TARGET_PROT_NORMAL: return "NORMAL";
148 case TARGET_PROT_DIN_INSERT: return "DIN_INSERT";
149 case TARGET_PROT_DOUT_INSERT: return "DOUT_INSERT";
150 case TARGET_PROT_DIN_STRIP: return "DIN_STRIP";
151 case TARGET_PROT_DOUT_STRIP: return "DOUT_STRIP";
152 case TARGET_PROT_DIN_PASS: return "DIN_PASS";
153 case TARGET_PROT_DOUT_PASS: return "DOUT_PASS";
154 default: return "UNKNOWN";
158 /* This API intentionally takes dest as a parameter, rather than returning
159 * int value to avoid caller forgetting to issue wmb() after the store */
160 void qlt_do_generation_tick(struct scsi_qla_host *vha, int *dest)
162 scsi_qla_host_t *base_vha = pci_get_drvdata(vha->hw->pdev);
163 *dest = atomic_inc_return(&base_vha->generation_tick);
168 /* Might release hw lock, then reaquire!! */
169 static inline int qlt_issue_marker(struct scsi_qla_host *vha, int vha_locked)
171 /* Send marker if required */
172 if (unlikely(vha->marker_needed != 0)) {
173 int rc = qla2x00_issue_marker(vha, vha_locked);
174 if (rc != QLA_SUCCESS) {
175 ql_dbg(ql_dbg_tgt, vha, 0xe03d,
176 "qla_target(%d): issue_marker() failed\n",
185 struct scsi_qla_host *qlt_find_host_by_d_id(struct scsi_qla_host *vha,
188 struct scsi_qla_host *host;
191 if ((vha->d_id.b.area == d_id[1]) && (vha->d_id.b.domain == d_id[0]) &&
192 (vha->d_id.b.al_pa == d_id[2]))
195 key = (uint32_t)d_id[0] << 16;
196 key |= (uint32_t)d_id[1] << 8;
197 key |= (uint32_t)d_id[2];
199 host = btree_lookup32(&vha->hw->tgt.host_map, key);
201 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf005,
202 "Unable to find host %06x\n", key);
208 struct scsi_qla_host *qlt_find_host_by_vp_idx(struct scsi_qla_host *vha,
211 struct qla_hw_data *ha = vha->hw;
213 if (vha->vp_idx == vp_idx)
216 BUG_ON(ha->tgt.tgt_vp_map == NULL);
217 if (likely(test_bit(vp_idx, ha->vp_idx_map)))
218 return ha->tgt.tgt_vp_map[vp_idx].vha;
223 static inline void qlt_incr_num_pend_cmds(struct scsi_qla_host *vha)
227 spin_lock_irqsave(&vha->hw->tgt.q_full_lock, flags);
229 vha->hw->tgt.num_pend_cmds++;
230 if (vha->hw->tgt.num_pend_cmds > vha->qla_stats.stat_max_pend_cmds)
231 vha->qla_stats.stat_max_pend_cmds =
232 vha->hw->tgt.num_pend_cmds;
233 spin_unlock_irqrestore(&vha->hw->tgt.q_full_lock, flags);
235 static inline void qlt_decr_num_pend_cmds(struct scsi_qla_host *vha)
239 spin_lock_irqsave(&vha->hw->tgt.q_full_lock, flags);
240 vha->hw->tgt.num_pend_cmds--;
241 spin_unlock_irqrestore(&vha->hw->tgt.q_full_lock, flags);
245 static void qlt_queue_unknown_atio(scsi_qla_host_t *vha,
246 struct atio_from_isp *atio, uint8_t ha_locked)
248 struct qla_tgt_sess_op *u;
249 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
253 ql_dbg(ql_dbg_async, vha, 0x502c,
254 "qla_target(%d): dropping unknown ATIO_TYPE7, because tgt is being stopped",
259 u = kzalloc(sizeof(*u), GFP_ATOMIC);
264 memcpy(&u->atio, atio, sizeof(*atio));
265 INIT_LIST_HEAD(&u->cmd_list);
267 spin_lock_irqsave(&vha->cmd_list_lock, flags);
268 list_add_tail(&u->cmd_list, &vha->unknown_atio_list);
269 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
271 schedule_delayed_work(&vha->unknown_atio_work, 1);
277 qlt_send_term_exchange(vha, NULL, atio, ha_locked, 0);
281 static void qlt_try_to_dequeue_unknown_atios(struct scsi_qla_host *vha,
284 struct qla_tgt_sess_op *u, *t;
285 scsi_qla_host_t *host;
286 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
290 list_for_each_entry_safe(u, t, &vha->unknown_atio_list, cmd_list) {
292 ql_dbg(ql_dbg_async, vha, 0x502e,
293 "Freeing unknown %s %p, because of Abort\n",
295 qlt_send_term_exchange(vha, NULL, &u->atio,
300 host = qlt_find_host_by_d_id(vha, u->atio.u.isp24.fcp_hdr.d_id);
302 ql_dbg(ql_dbg_async, vha, 0x502f,
303 "Requeuing unknown ATIO_TYPE7 %p\n", u);
304 qlt_24xx_atio_pkt(host, &u->atio, ha_locked);
305 } else if (tgt->tgt_stop) {
306 ql_dbg(ql_dbg_async, vha, 0x503a,
307 "Freeing unknown %s %p, because tgt is being stopped\n",
309 qlt_send_term_exchange(vha, NULL, &u->atio,
312 ql_dbg(ql_dbg_async, vha, 0x503d,
313 "Reschedule u %p, vha %p, host %p\n", u, vha, host);
316 schedule_delayed_work(&vha->unknown_atio_work,
323 spin_lock_irqsave(&vha->cmd_list_lock, flags);
324 list_del(&u->cmd_list);
325 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
330 void qlt_unknown_atio_work_fn(struct work_struct *work)
332 struct scsi_qla_host *vha = container_of(to_delayed_work(work),
333 struct scsi_qla_host, unknown_atio_work);
335 qlt_try_to_dequeue_unknown_atios(vha, 0);
338 static bool qlt_24xx_atio_pkt_all_vps(struct scsi_qla_host *vha,
339 struct atio_from_isp *atio, uint8_t ha_locked)
341 ql_dbg(ql_dbg_tgt, vha, 0xe072,
342 "%s: qla_target(%d): type %x ox_id %04x\n",
343 __func__, vha->vp_idx, atio->u.raw.entry_type,
344 be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id));
346 switch (atio->u.raw.entry_type) {
349 struct scsi_qla_host *host = qlt_find_host_by_d_id(vha,
350 atio->u.isp24.fcp_hdr.d_id);
351 if (unlikely(NULL == host)) {
352 ql_dbg(ql_dbg_tgt, vha, 0xe03e,
353 "qla_target(%d): Received ATIO_TYPE7 "
354 "with unknown d_id %x:%x:%x\n", vha->vp_idx,
355 atio->u.isp24.fcp_hdr.d_id[0],
356 atio->u.isp24.fcp_hdr.d_id[1],
357 atio->u.isp24.fcp_hdr.d_id[2]);
360 qlt_queue_unknown_atio(vha, atio, ha_locked);
363 if (unlikely(!list_empty(&vha->unknown_atio_list)))
364 qlt_try_to_dequeue_unknown_atios(vha, ha_locked);
366 qlt_24xx_atio_pkt(host, atio, ha_locked);
370 case IMMED_NOTIFY_TYPE:
372 struct scsi_qla_host *host = vha;
373 struct imm_ntfy_from_isp *entry =
374 (struct imm_ntfy_from_isp *)atio;
376 if ((entry->u.isp24.vp_index != 0xFF) &&
377 (entry->u.isp24.nport_handle != 0xFFFF)) {
378 host = qlt_find_host_by_vp_idx(vha,
379 entry->u.isp24.vp_index);
380 if (unlikely(!host)) {
381 ql_dbg(ql_dbg_tgt, vha, 0xe03f,
382 "qla_target(%d): Received "
383 "ATIO (IMMED_NOTIFY_TYPE) "
384 "with unknown vp_index %d\n",
385 vha->vp_idx, entry->u.isp24.vp_index);
389 qlt_24xx_atio_pkt(host, atio, ha_locked);
393 case VP_RPT_ID_IOCB_TYPE:
394 qla24xx_report_id_acquisition(vha,
395 (struct vp_rpt_id_entry_24xx *)atio);
400 struct abts_recv_from_24xx *entry =
401 (struct abts_recv_from_24xx *)atio;
402 struct scsi_qla_host *host = qlt_find_host_by_vp_idx(vha,
406 if (unlikely(!host)) {
407 ql_dbg(ql_dbg_tgt, vha, 0xe00a,
408 "qla_target(%d): Response pkt (ABTS_RECV_24XX) "
409 "received, with unknown vp_index %d\n",
410 vha->vp_idx, entry->vp_index);
414 spin_lock_irqsave(&host->hw->hardware_lock, flags);
415 qlt_24xx_handle_abts(host, (struct abts_recv_from_24xx *)atio);
417 spin_unlock_irqrestore(&host->hw->hardware_lock, flags);
421 /* case PUREX_IOCB_TYPE: ql2xmvasynctoatio */
424 ql_dbg(ql_dbg_tgt, vha, 0xe040,
425 "qla_target(%d): Received unknown ATIO atio "
426 "type %x\n", vha->vp_idx, atio->u.raw.entry_type);
433 void qlt_response_pkt_all_vps(struct scsi_qla_host *vha, response_t *pkt)
435 switch (pkt->entry_type) {
437 ql_dbg(ql_dbg_tgt, vha, 0xe073,
438 "qla_target(%d):%s: CRC2 Response pkt\n",
439 vha->vp_idx, __func__);
442 struct ctio7_from_24xx *entry = (struct ctio7_from_24xx *)pkt;
443 struct scsi_qla_host *host = qlt_find_host_by_vp_idx(vha,
445 if (unlikely(!host)) {
446 ql_dbg(ql_dbg_tgt, vha, 0xe041,
447 "qla_target(%d): Response pkt (CTIO_TYPE7) "
448 "received, with unknown vp_index %d\n",
449 vha->vp_idx, entry->vp_index);
452 qlt_response_pkt(host, pkt);
456 case IMMED_NOTIFY_TYPE:
458 struct scsi_qla_host *host = vha;
459 struct imm_ntfy_from_isp *entry =
460 (struct imm_ntfy_from_isp *)pkt;
462 host = qlt_find_host_by_vp_idx(vha, entry->u.isp24.vp_index);
463 if (unlikely(!host)) {
464 ql_dbg(ql_dbg_tgt, vha, 0xe042,
465 "qla_target(%d): Response pkt (IMMED_NOTIFY_TYPE) "
466 "received, with unknown vp_index %d\n",
467 vha->vp_idx, entry->u.isp24.vp_index);
470 qlt_response_pkt(host, pkt);
474 case NOTIFY_ACK_TYPE:
476 struct scsi_qla_host *host = vha;
477 struct nack_to_isp *entry = (struct nack_to_isp *)pkt;
479 if (0xFF != entry->u.isp24.vp_index) {
480 host = qlt_find_host_by_vp_idx(vha,
481 entry->u.isp24.vp_index);
482 if (unlikely(!host)) {
483 ql_dbg(ql_dbg_tgt, vha, 0xe043,
484 "qla_target(%d): Response "
485 "pkt (NOTIFY_ACK_TYPE) "
486 "received, with unknown "
487 "vp_index %d\n", vha->vp_idx,
488 entry->u.isp24.vp_index);
492 qlt_response_pkt(host, pkt);
498 struct abts_recv_from_24xx *entry =
499 (struct abts_recv_from_24xx *)pkt;
500 struct scsi_qla_host *host = qlt_find_host_by_vp_idx(vha,
502 if (unlikely(!host)) {
503 ql_dbg(ql_dbg_tgt, vha, 0xe044,
504 "qla_target(%d): Response pkt "
505 "(ABTS_RECV_24XX) received, with unknown "
506 "vp_index %d\n", vha->vp_idx, entry->vp_index);
509 qlt_response_pkt(host, pkt);
515 struct abts_resp_to_24xx *entry =
516 (struct abts_resp_to_24xx *)pkt;
517 struct scsi_qla_host *host = qlt_find_host_by_vp_idx(vha,
519 if (unlikely(!host)) {
520 ql_dbg(ql_dbg_tgt, vha, 0xe045,
521 "qla_target(%d): Response pkt "
522 "(ABTS_RECV_24XX) received, with unknown "
523 "vp_index %d\n", vha->vp_idx, entry->vp_index);
526 qlt_response_pkt(host, pkt);
531 qlt_response_pkt(vha, pkt);
538 * All qlt_plogi_ack_t operations are protected by hardware_lock
540 static int qla24xx_post_nack_work(struct scsi_qla_host *vha, fc_port_t *fcport,
541 struct imm_ntfy_from_isp *ntfy, int type)
543 struct qla_work_evt *e;
544 e = qla2x00_alloc_work(vha, QLA_EVT_NACK);
546 return QLA_FUNCTION_FAILED;
548 e->u.nack.fcport = fcport;
549 e->u.nack.type = type;
550 memcpy(e->u.nack.iocb, ntfy, sizeof(struct imm_ntfy_from_isp));
551 return qla2x00_post_work(vha, e);
555 void qla2x00_async_nack_sp_done(void *s, int res)
557 struct srb *sp = (struct srb *)s;
558 struct scsi_qla_host *vha = sp->vha;
561 ql_dbg(ql_dbg_disc, vha, 0x20f2,
562 "Async done-%s res %x %8phC type %d\n",
563 sp->name, res, sp->fcport->port_name, sp->type);
565 spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
566 sp->fcport->flags &= ~FCF_ASYNC_SENT;
567 sp->fcport->chip_reset = vha->hw->chip_reset;
571 sp->fcport->login_gen++;
572 sp->fcport->fw_login_state = DSC_LS_PLOGI_COMP;
573 sp->fcport->logout_on_delete = 1;
574 sp->fcport->plogi_nack_done_deadline = jiffies + HZ;
578 sp->fcport->fw_login_state = DSC_LS_PRLI_COMP;
579 sp->fcport->deleted = 0;
581 if (!sp->fcport->login_succ &&
582 !IS_SW_RESV_ADDR(sp->fcport->d_id)) {
583 sp->fcport->login_succ = 1;
587 if (!IS_IIDMA_CAPABLE(vha->hw) ||
588 !vha->hw->flags.gpsc_supported) {
589 ql_dbg(ql_dbg_disc, vha, 0x20f3,
590 "%s %d %8phC post upd_fcport fcp_cnt %d\n",
592 sp->fcport->port_name,
595 qla24xx_post_upd_fcport_work(vha, sp->fcport);
597 ql_dbg(ql_dbg_disc, vha, 0x20f5,
598 "%s %d %8phC post gpsc fcp_cnt %d\n",
600 sp->fcport->port_name,
603 qla24xx_post_gpsc_work(vha, sp->fcport);
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 qla2x00_init_timer(sp, qla2x00_get_async_timeout(vha)+2);
652 sp->u.iocb_cmd.u.nack.ntfy = ntfy;
654 sp->done = qla2x00_async_nack_sp_done;
656 rval = qla2x00_start_sp(sp);
657 if (rval != QLA_SUCCESS)
660 ql_dbg(ql_dbg_disc, vha, 0x20f4,
661 "Async-%s %8phC hndl %x %s\n",
662 sp->name, fcport->port_name, sp->handle, c);
669 fcport->flags &= ~FCF_ASYNC_SENT;
673 void qla24xx_do_nack_work(struct scsi_qla_host *vha, struct qla_work_evt *e)
678 switch (e->u.nack.type) {
680 mutex_lock(&vha->vha_tgt.tgt_mutex);
681 t = qlt_create_sess(vha, e->u.nack.fcport, 0);
682 mutex_unlock(&vha->vha_tgt.tgt_mutex);
684 ql_log(ql_log_info, vha, 0xd034,
685 "%s create sess success %p", __func__, t);
686 spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
687 /* create sess has an extra kref */
688 vha->hw->tgt.tgt_ops->put_sess(e->u.nack.fcport);
689 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
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;
703 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
705 if (fcport->se_sess) {
706 ha->tgt.tgt_ops->shutdown_sess(fcport);
707 ha->tgt.tgt_ops->put_sess(fcport);
709 qlt_unreg_sess(fcport);
711 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
715 * Called from qla2x00_reg_remote_port()
717 void qlt_fc_port_added(struct scsi_qla_host *vha, fc_port_t *fcport)
719 struct qla_hw_data *ha = vha->hw;
720 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
721 struct fc_port *sess = fcport;
724 if (!vha->hw->tgt.tgt_ops)
727 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
729 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
733 if (fcport->disc_state == DSC_DELETE_PEND) {
734 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
738 if (!sess->se_sess) {
739 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
741 mutex_lock(&vha->vha_tgt.tgt_mutex);
742 sess = qlt_create_sess(vha, fcport, false);
743 mutex_unlock(&vha->vha_tgt.tgt_mutex);
745 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
747 if (fcport->fw_login_state == DSC_LS_PRLI_COMP) {
748 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
752 if (!kref_get_unless_zero(&sess->sess_kref)) {
753 ql_dbg(ql_dbg_disc, vha, 0x2107,
754 "%s: kref_get fail sess %8phC \n",
755 __func__, sess->port_name);
756 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
760 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04c,
761 "qla_target(%u): %ssession for port %8phC "
762 "(loop ID %d) reappeared\n", vha->vp_idx,
763 sess->local ? "local " : "", sess->port_name, sess->loop_id);
765 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf007,
766 "Reappeared sess %p\n", sess);
768 ha->tgt.tgt_ops->update_sess(sess, fcport->d_id,
770 (fcport->flags & FCF_CONF_COMP_SUPPORTED));
773 if (sess && sess->local) {
774 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04d,
775 "qla_target(%u): local session for "
776 "port %8phC (loop ID %d) became global\n", vha->vp_idx,
777 fcport->port_name, sess->loop_id);
780 ha->tgt.tgt_ops->put_sess(sess);
781 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
785 * This is a zero-base ref-counting solution, since hardware_lock
786 * guarantees that ref_count is not modified concurrently.
787 * Upon successful return content of iocb is undefined
789 static struct qlt_plogi_ack_t *
790 qlt_plogi_ack_find_add(struct scsi_qla_host *vha, port_id_t *id,
791 struct imm_ntfy_from_isp *iocb)
793 struct qlt_plogi_ack_t *pla;
795 list_for_each_entry(pla, &vha->plogi_ack_list, list) {
796 if (pla->id.b24 == id->b24) {
797 qlt_send_term_imm_notif(vha, &pla->iocb, 1);
798 memcpy(&pla->iocb, iocb, sizeof(pla->iocb));
803 pla = kmem_cache_zalloc(qla_tgt_plogi_cachep, GFP_ATOMIC);
805 ql_dbg(ql_dbg_async, vha, 0x5088,
806 "qla_target(%d): Allocation of plogi_ack failed\n",
811 memcpy(&pla->iocb, iocb, sizeof(pla->iocb));
813 list_add_tail(&pla->list, &vha->plogi_ack_list);
818 void qlt_plogi_ack_unref(struct scsi_qla_host *vha,
819 struct qlt_plogi_ack_t *pla)
821 struct imm_ntfy_from_isp *iocb = &pla->iocb;
824 fc_port_t *fcport = pla->fcport;
826 BUG_ON(!pla->ref_count);
832 ql_dbg(ql_dbg_disc, vha, 0x5089,
833 "Sending PLOGI ACK to wwn %8phC s_id %02x:%02x:%02x loop_id %#04x"
834 " exch %#x ox_id %#x\n", iocb->u.isp24.port_name,
835 iocb->u.isp24.port_id[2], iocb->u.isp24.port_id[1],
836 iocb->u.isp24.port_id[0],
837 le16_to_cpu(iocb->u.isp24.nport_handle),
838 iocb->u.isp24.exchange_address, iocb->ox_id);
840 port_id.b.domain = iocb->u.isp24.port_id[2];
841 port_id.b.area = iocb->u.isp24.port_id[1];
842 port_id.b.al_pa = iocb->u.isp24.port_id[0];
843 port_id.b.rsvd_1 = 0;
845 loop_id = le16_to_cpu(iocb->u.isp24.nport_handle);
847 fcport->loop_id = loop_id;
848 fcport->d_id = port_id;
849 qla24xx_post_nack_work(vha, fcport, iocb, SRB_NACK_PLOGI);
851 list_for_each_entry(fcport, &vha->vp_fcports, list) {
852 if (fcport->plogi_link[QLT_PLOGI_LINK_SAME_WWN] == pla)
853 fcport->plogi_link[QLT_PLOGI_LINK_SAME_WWN] = NULL;
854 if (fcport->plogi_link[QLT_PLOGI_LINK_CONFLICT] == pla)
855 fcport->plogi_link[QLT_PLOGI_LINK_CONFLICT] = NULL;
858 list_del(&pla->list);
859 kmem_cache_free(qla_tgt_plogi_cachep, pla);
863 qlt_plogi_ack_link(struct scsi_qla_host *vha, struct qlt_plogi_ack_t *pla,
864 struct fc_port *sess, enum qlt_plogi_link_t link)
866 struct imm_ntfy_from_isp *iocb = &pla->iocb;
867 /* Inc ref_count first because link might already be pointing at pla */
870 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf097,
871 "Linking sess %p [%d] wwn %8phC with PLOGI ACK to wwn %8phC"
872 " s_id %02x:%02x:%02x, ref=%d pla %p link %d\n",
873 sess, link, sess->port_name,
874 iocb->u.isp24.port_name, iocb->u.isp24.port_id[2],
875 iocb->u.isp24.port_id[1], iocb->u.isp24.port_id[0],
876 pla->ref_count, pla, link);
878 if (sess->plogi_link[link])
879 qlt_plogi_ack_unref(vha, sess->plogi_link[link]);
881 if (link == QLT_PLOGI_LINK_SAME_WWN)
884 sess->plogi_link[link] = pla;
888 /* These fields must be initialized by the caller */
891 * number of cmds dropped while we were waiting for
892 * initiator to ack LOGO initialize to 1 if LOGO is
893 * triggered by a command, otherwise, to 0
897 /* These fields are used by callee */
898 struct list_head list;
902 qlt_send_first_logo(struct scsi_qla_host *vha, qlt_port_logo_t *logo)
904 qlt_port_logo_t *tmp;
907 mutex_lock(&vha->vha_tgt.tgt_mutex);
909 list_for_each_entry(tmp, &vha->logo_list, list) {
910 if (tmp->id.b24 == logo->id.b24) {
911 tmp->cmd_count += logo->cmd_count;
912 mutex_unlock(&vha->vha_tgt.tgt_mutex);
917 list_add_tail(&logo->list, &vha->logo_list);
919 mutex_unlock(&vha->vha_tgt.tgt_mutex);
921 res = qla24xx_els_dcmd_iocb(vha, ELS_DCMD_LOGO, logo->id);
923 mutex_lock(&vha->vha_tgt.tgt_mutex);
924 list_del(&logo->list);
925 mutex_unlock(&vha->vha_tgt.tgt_mutex);
927 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf098,
928 "Finished LOGO to %02x:%02x:%02x, dropped %d cmds, res = %#x\n",
929 logo->id.b.domain, logo->id.b.area, logo->id.b.al_pa,
930 logo->cmd_count, res);
933 static void qlt_free_session_done(struct work_struct *work)
935 struct fc_port *sess = container_of(work, struct fc_port,
937 struct qla_tgt *tgt = sess->tgt;
938 struct scsi_qla_host *vha = sess->vha;
939 struct qla_hw_data *ha = vha->hw;
941 bool logout_started = false;
943 scsi_qla_host_t *base_vha;
945 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf084,
946 "%s: se_sess %p / sess %p from port %8phC loop_id %#04x"
947 " s_id %02x:%02x:%02x logout %d keep %d els_logo %d\n",
948 __func__, sess->se_sess, sess, sess->port_name, sess->loop_id,
949 sess->d_id.b.domain, sess->d_id.b.area, sess->d_id.b.al_pa,
950 sess->logout_on_delete, sess->keep_nport_handle,
951 sess->send_els_logo);
954 if (!IS_SW_RESV_ADDR(sess->d_id)) {
955 if (sess->send_els_logo) {
956 qlt_port_logo_t logo;
958 logo.id = sess->d_id;
960 qlt_send_first_logo(vha, &logo);
963 if (sess->logout_on_delete) {
966 rc = qla2x00_post_async_logout_work(vha, sess, NULL);
967 if (rc != QLA_SUCCESS)
968 ql_log(ql_log_warn, vha, 0xf085,
969 "Schedule logo failed sess %p rc %d\n",
972 logout_started = true;
977 * Release the target session for FC Nexus from fabric module code.
979 if (sess->se_sess != NULL)
980 ha->tgt.tgt_ops->free_session(sess);
982 if (logout_started) {
985 while (!ACCESS_ONCE(sess->logout_completed)) {
987 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf086,
988 "%s: waiting for sess %p logout\n",
995 ql_dbg(ql_dbg_disc, vha, 0xf087,
996 "%s: sess %p logout completed\n",__func__, sess);
999 if (sess->logo_ack_needed) {
1000 sess->logo_ack_needed = 0;
1001 qla24xx_async_notify_ack(vha, sess,
1002 (struct imm_ntfy_from_isp *)sess->iocb, SRB_NACK_LOGO);
1005 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
1006 if (sess->se_sess) {
1007 sess->se_sess = NULL;
1008 if (tgt && !IS_SW_RESV_ADDR(sess->d_id))
1012 sess->disc_state = DSC_DELETED;
1013 sess->fw_login_state = DSC_LS_PORT_UNAVAIL;
1014 sess->deleted = QLA_SESS_DELETED;
1015 sess->login_retry = vha->hw->login_retry_count;
1017 if (sess->login_succ && !IS_SW_RESV_ADDR(sess->d_id)) {
1018 vha->fcport_count--;
1019 sess->login_succ = 0;
1022 if (sess->chip_reset != sess->vha->hw->chip_reset)
1023 qla2x00_clear_loop_id(sess);
1025 if (sess->conflict) {
1026 sess->conflict->login_pause = 0;
1027 sess->conflict = NULL;
1028 if (!test_bit(UNLOADING, &vha->dpc_flags))
1029 set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
1033 struct qlt_plogi_ack_t *own =
1034 sess->plogi_link[QLT_PLOGI_LINK_SAME_WWN];
1035 struct qlt_plogi_ack_t *con =
1036 sess->plogi_link[QLT_PLOGI_LINK_CONFLICT];
1037 struct imm_ntfy_from_isp *iocb;
1041 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf099,
1042 "se_sess %p / sess %p port %8phC is gone,"
1043 " %s (ref=%d), releasing PLOGI for %8phC (ref=%d)\n",
1044 sess->se_sess, sess, sess->port_name,
1045 own ? "releasing own PLOGI" : "no own PLOGI pending",
1046 own ? own->ref_count : -1,
1047 iocb->u.isp24.port_name, con->ref_count);
1048 qlt_plogi_ack_unref(vha, con);
1049 sess->plogi_link[QLT_PLOGI_LINK_CONFLICT] = NULL;
1051 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf09a,
1052 "se_sess %p / sess %p port %8phC is gone, %s (ref=%d)\n",
1053 sess->se_sess, sess, sess->port_name,
1054 own ? "releasing own PLOGI" :
1055 "no own PLOGI pending",
1056 own ? own->ref_count : -1);
1060 sess->fw_login_state = DSC_LS_PLOGI_PEND;
1061 qlt_plogi_ack_unref(vha, own);
1062 sess->plogi_link[QLT_PLOGI_LINK_SAME_WWN] = NULL;
1065 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
1067 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf001,
1068 "Unregistration of sess %p %8phC finished fcp_cnt %d\n",
1069 sess, sess->port_name, vha->fcport_count);
1071 if (tgt && (tgt->sess_count == 0))
1072 wake_up_all(&tgt->waitQ);
1074 if (vha->fcport_count == 0)
1075 wake_up_all(&vha->fcport_waitQ);
1077 base_vha = pci_get_drvdata(ha->pdev);
1078 if (test_bit(PFLG_DRIVER_REMOVING, &base_vha->pci_flags))
1081 if (!tgt || !tgt->tgt_stop) {
1082 memset(&ea, 0, sizeof(ea));
1083 ea.event = FCME_DELETE_DONE;
1085 qla2x00_fcport_event_handler(vha, &ea);
1089 /* ha->tgt.sess_lock supposed to be held on entry */
1090 void qlt_unreg_sess(struct fc_port *sess)
1092 struct scsi_qla_host *vha = sess->vha;
1094 ql_dbg(ql_dbg_disc, sess->vha, 0x210a,
1095 "%s sess %p for deletion %8phC\n",
1096 __func__, sess, sess->port_name);
1099 vha->hw->tgt.tgt_ops->clear_nacl_from_fcport_map(sess);
1101 qla2x00_mark_device_lost(vha, sess, 1, 1);
1103 sess->deleted = QLA_SESS_DELETION_IN_PROGRESS;
1104 sess->disc_state = DSC_DELETE_PEND;
1105 sess->last_rscn_gen = sess->rscn_gen;
1106 sess->last_login_gen = sess->login_gen;
1108 INIT_WORK(&sess->free_work, qlt_free_session_done);
1109 schedule_work(&sess->free_work);
1111 EXPORT_SYMBOL(qlt_unreg_sess);
1113 static int qlt_reset(struct scsi_qla_host *vha, void *iocb, int mcmd)
1115 struct qla_hw_data *ha = vha->hw;
1116 struct fc_port *sess = NULL;
1119 struct imm_ntfy_from_isp *n = (struct imm_ntfy_from_isp *)iocb;
1120 unsigned long flags;
1122 loop_id = le16_to_cpu(n->u.isp24.nport_handle);
1123 if (loop_id == 0xFFFF) {
1125 atomic_inc(&vha->vha_tgt.qla_tgt->tgt_global_resets_count);
1126 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
1127 qlt_clear_tgt_db(vha->vha_tgt.qla_tgt);
1128 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
1130 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
1131 sess = ha->tgt.tgt_ops->find_sess_by_loop_id(vha, loop_id);
1132 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
1135 ql_dbg(ql_dbg_tgt, vha, 0xe000,
1136 "Using sess for qla_tgt_reset: %p\n", sess);
1142 ql_dbg(ql_dbg_tgt, vha, 0xe047,
1143 "scsi(%ld): resetting (session %p from port %8phC mcmd %x, "
1144 "loop_id %d)\n", vha->host_no, sess, sess->port_name,
1147 return qlt_issue_task_mgmt(sess, 0, mcmd, iocb, QLA24XX_MGMT_SEND_NACK);
1150 static void qla24xx_chk_fcp_state(struct fc_port *sess)
1152 if (sess->chip_reset != sess->vha->hw->chip_reset) {
1153 sess->logout_on_delete = 0;
1154 sess->logo_ack_needed = 0;
1155 sess->fw_login_state = DSC_LS_PORT_UNAVAIL;
1156 sess->scan_state = 0;
1160 /* ha->tgt.sess_lock supposed to be held on entry */
1161 void qlt_schedule_sess_for_deletion(struct fc_port *sess,
1164 struct qla_tgt *tgt = sess->tgt;
1166 if (sess->disc_state == DSC_DELETE_PEND)
1169 if (sess->disc_state == DSC_DELETED) {
1170 if (tgt && tgt->tgt_stop && (tgt->sess_count == 0))
1171 wake_up_all(&tgt->waitQ);
1172 if (sess->vha->fcport_count == 0)
1173 wake_up_all(&sess->vha->fcport_waitQ);
1175 if (!sess->plogi_link[QLT_PLOGI_LINK_SAME_WWN] &&
1176 !sess->plogi_link[QLT_PLOGI_LINK_CONFLICT])
1180 sess->disc_state = DSC_DELETE_PEND;
1182 if (sess->deleted == QLA_SESS_DELETED)
1183 sess->logout_on_delete = 0;
1185 sess->deleted = QLA_SESS_DELETION_IN_PROGRESS;
1186 qla24xx_chk_fcp_state(sess);
1188 ql_dbg(ql_dbg_tgt, sess->vha, 0xe001,
1189 "Scheduling sess %p for deletion\n", sess);
1191 schedule_work(&sess->del_work);
1194 void qlt_schedule_sess_for_deletion_lock(struct fc_port *sess)
1196 unsigned long flags;
1197 struct qla_hw_data *ha = sess->vha->hw;
1198 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
1199 qlt_schedule_sess_for_deletion(sess, 1);
1200 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
1203 /* ha->tgt.sess_lock supposed to be held on entry */
1204 static void qlt_clear_tgt_db(struct qla_tgt *tgt)
1206 struct fc_port *sess;
1207 scsi_qla_host_t *vha = tgt->vha;
1209 list_for_each_entry(sess, &vha->vp_fcports, list) {
1211 qlt_schedule_sess_for_deletion(sess, 1);
1214 /* At this point tgt could be already dead */
1217 static int qla24xx_get_loop_id(struct scsi_qla_host *vha, const uint8_t *s_id,
1220 struct qla_hw_data *ha = vha->hw;
1221 dma_addr_t gid_list_dma;
1222 struct gid_list_info *gid_list;
1227 gid_list = dma_alloc_coherent(&ha->pdev->dev, qla2x00_gid_list_size(ha),
1228 &gid_list_dma, GFP_KERNEL);
1230 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf044,
1231 "qla_target(%d): DMA Alloc failed of %u\n",
1232 vha->vp_idx, qla2x00_gid_list_size(ha));
1236 /* Get list of logged in devices */
1237 rc = qla24xx_gidlist_wait(vha, gid_list, gid_list_dma, &entries);
1238 if (rc != QLA_SUCCESS) {
1239 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf045,
1240 "qla_target(%d): get_id_list() failed: %x\n",
1243 goto out_free_id_list;
1246 id_iter = (char *)gid_list;
1248 for (i = 0; i < entries; i++) {
1249 struct gid_list_info *gid = (struct gid_list_info *)id_iter;
1250 if ((gid->al_pa == s_id[2]) &&
1251 (gid->area == s_id[1]) &&
1252 (gid->domain == s_id[0])) {
1253 *loop_id = le16_to_cpu(gid->loop_id);
1257 id_iter += ha->gid_list_info_size;
1261 dma_free_coherent(&ha->pdev->dev, qla2x00_gid_list_size(ha),
1262 gid_list, gid_list_dma);
1267 * Adds an extra ref to allow to drop hw lock after adding sess to the list.
1268 * Caller must put it.
1270 static struct fc_port *qlt_create_sess(
1271 struct scsi_qla_host *vha,
1275 struct qla_hw_data *ha = vha->hw;
1276 struct fc_port *sess = fcport;
1277 unsigned long flags;
1279 if (vha->vha_tgt.qla_tgt->tgt_stop)
1282 if (fcport->se_sess) {
1283 if (!kref_get_unless_zero(&sess->sess_kref)) {
1284 ql_dbg(ql_dbg_disc, vha, 0x20f6,
1285 "%s: kref_get_unless_zero failed for %8phC\n",
1286 __func__, sess->port_name);
1291 sess->tgt = vha->vha_tgt.qla_tgt;
1292 sess->local = local;
1295 * Under normal circumstances we want to logout from firmware when
1296 * session eventually ends and release corresponding nport handle.
1297 * In the exception cases (e.g. when new PLOGI is waiting) corresponding
1298 * code will adjust these flags as necessary.
1300 sess->logout_on_delete = 1;
1301 sess->keep_nport_handle = 0;
1302 sess->logout_completed = 0;
1304 if (ha->tgt.tgt_ops->check_initiator_node_acl(vha,
1305 &fcport->port_name[0], sess) < 0) {
1306 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf015,
1307 "(%d) %8phC check_initiator_node_acl failed\n",
1308 vha->vp_idx, fcport->port_name);
1311 kref_init(&fcport->sess_kref);
1313 * Take an extra reference to ->sess_kref here to handle
1314 * fc_port access across ->tgt.sess_lock reaquire.
1316 if (!kref_get_unless_zero(&sess->sess_kref)) {
1317 ql_dbg(ql_dbg_disc, vha, 0x20f7,
1318 "%s: kref_get_unless_zero failed for %8phC\n",
1319 __func__, sess->port_name);
1323 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
1324 if (!IS_SW_RESV_ADDR(sess->d_id))
1325 vha->vha_tgt.qla_tgt->sess_count++;
1327 qlt_do_generation_tick(vha, &sess->generation);
1328 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
1331 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf006,
1332 "Adding sess %p se_sess %p to tgt %p sess_count %d\n",
1333 sess, sess->se_sess, vha->vha_tgt.qla_tgt,
1334 vha->vha_tgt.qla_tgt->sess_count);
1336 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04b,
1337 "qla_target(%d): %ssession for wwn %8phC (loop_id %d, "
1338 "s_id %x:%x:%x, confirmed completion %ssupported) added\n",
1339 vha->vp_idx, local ? "local " : "", fcport->port_name,
1340 fcport->loop_id, sess->d_id.b.domain, sess->d_id.b.area,
1341 sess->d_id.b.al_pa, sess->conf_compl_supported ? "" : "not ");
1347 * max_gen - specifies maximum session generation
1348 * at which this deletion requestion is still valid
1351 qlt_fc_port_deleted(struct scsi_qla_host *vha, fc_port_t *fcport, int max_gen)
1353 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
1354 struct fc_port *sess = fcport;
1355 unsigned long flags;
1357 if (!vha->hw->tgt.tgt_ops)
1363 spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
1364 if (tgt->tgt_stop) {
1365 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
1368 if (!sess->se_sess) {
1369 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
1373 if (max_gen - sess->generation < 0) {
1374 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
1375 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf092,
1376 "Ignoring stale deletion request for se_sess %p / sess %p"
1377 " for port %8phC, req_gen %d, sess_gen %d\n",
1378 sess->se_sess, sess, sess->port_name, max_gen,
1383 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf008, "qla_tgt_fc_port_deleted %p", sess);
1386 qlt_schedule_sess_for_deletion(sess, false);
1387 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
1390 static inline int test_tgt_sess_count(struct qla_tgt *tgt)
1392 struct qla_hw_data *ha = tgt->ha;
1393 unsigned long flags;
1396 * We need to protect against race, when tgt is freed before or
1399 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
1400 ql_dbg(ql_dbg_tgt, tgt->vha, 0xe002,
1401 "tgt %p, sess_count=%d\n",
1402 tgt, tgt->sess_count);
1403 res = (tgt->sess_count == 0);
1404 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
1409 /* Called by tcm_qla2xxx configfs code */
1410 int qlt_stop_phase1(struct qla_tgt *tgt)
1412 struct scsi_qla_host *vha = tgt->vha;
1413 struct qla_hw_data *ha = tgt->ha;
1414 unsigned long flags;
1416 mutex_lock(&qla_tgt_mutex);
1417 if (!vha->fc_vport) {
1418 struct Scsi_Host *sh = vha->host;
1419 struct fc_host_attrs *fc_host = shost_to_fc_host(sh);
1422 spin_lock_irqsave(sh->host_lock, flags);
1423 npiv_vports = (fc_host->npiv_vports_inuse);
1424 spin_unlock_irqrestore(sh->host_lock, flags);
1427 mutex_unlock(&qla_tgt_mutex);
1428 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf021,
1429 "NPIV is in use. Can not stop target\n");
1433 if (tgt->tgt_stop || tgt->tgt_stopped) {
1434 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04e,
1435 "Already in tgt->tgt_stop or tgt_stopped state\n");
1436 mutex_unlock(&qla_tgt_mutex);
1440 ql_dbg(ql_dbg_tgt_mgt, vha, 0xe003, "Stopping target for host %ld(%p)\n",
1443 * Mutex needed to sync with qla_tgt_fc_port_[added,deleted].
1444 * Lock is needed, because we still can get an incoming packet.
1446 mutex_lock(&vha->vha_tgt.tgt_mutex);
1447 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
1449 qlt_clear_tgt_db(tgt);
1450 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
1451 mutex_unlock(&vha->vha_tgt.tgt_mutex);
1452 mutex_unlock(&qla_tgt_mutex);
1454 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf009,
1455 "Waiting for sess works (tgt %p)", tgt);
1456 spin_lock_irqsave(&tgt->sess_work_lock, flags);
1457 while (!list_empty(&tgt->sess_works_list)) {
1458 spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
1459 flush_scheduled_work();
1460 spin_lock_irqsave(&tgt->sess_work_lock, flags);
1462 spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
1464 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00a,
1465 "Waiting for tgt %p: sess_count=%d\n", tgt, tgt->sess_count);
1467 wait_event(tgt->waitQ, test_tgt_sess_count(tgt));
1470 if (!ha->flags.host_shutting_down &&
1471 (qla_tgt_mode_enabled(vha) || qla_dual_mode_enabled(vha)))
1472 qlt_disable_vha(vha);
1474 /* Wait for sessions to clear out (just in case) */
1475 wait_event(tgt->waitQ, test_tgt_sess_count(tgt));
1478 EXPORT_SYMBOL(qlt_stop_phase1);
1480 /* Called by tcm_qla2xxx configfs code */
1481 void qlt_stop_phase2(struct qla_tgt *tgt)
1483 scsi_qla_host_t *vha = tgt->vha;
1485 if (tgt->tgt_stopped) {
1486 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04f,
1487 "Already in tgt->tgt_stopped state\n");
1491 if (!tgt->tgt_stop) {
1492 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00b,
1493 "%s: phase1 stop is not completed\n", __func__);
1498 mutex_lock(&vha->vha_tgt.tgt_mutex);
1500 tgt->tgt_stopped = 1;
1501 mutex_unlock(&vha->vha_tgt.tgt_mutex);
1503 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00c, "Stop of tgt %p finished\n",
1506 EXPORT_SYMBOL(qlt_stop_phase2);
1508 /* Called from qlt_remove_target() -> qla2x00_remove_one() */
1509 static void qlt_release(struct qla_tgt *tgt)
1511 scsi_qla_host_t *vha = tgt->vha;
1513 if ((vha->vha_tgt.qla_tgt != NULL) && !tgt->tgt_stop &&
1515 qlt_stop_phase1(tgt);
1517 if ((vha->vha_tgt.qla_tgt != NULL) && !tgt->tgt_stopped)
1518 qlt_stop_phase2(tgt);
1520 vha->vha_tgt.qla_tgt = NULL;
1522 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00d,
1523 "Release of tgt %p finished\n", tgt);
1528 /* ha->hardware_lock supposed to be held on entry */
1529 static int qlt_sched_sess_work(struct qla_tgt *tgt, int type,
1530 const void *param, unsigned int param_size)
1532 struct qla_tgt_sess_work_param *prm;
1533 unsigned long flags;
1535 prm = kzalloc(sizeof(*prm), GFP_ATOMIC);
1537 ql_dbg(ql_dbg_tgt_mgt, tgt->vha, 0xf050,
1538 "qla_target(%d): Unable to create session "
1539 "work, command will be refused", 0);
1543 ql_dbg(ql_dbg_tgt_mgt, tgt->vha, 0xf00e,
1544 "Scheduling work (type %d, prm %p)"
1545 " to find session for param %p (size %d, tgt %p)\n",
1546 type, prm, param, param_size, tgt);
1549 memcpy(&prm->tm_iocb, param, param_size);
1551 spin_lock_irqsave(&tgt->sess_work_lock, flags);
1552 list_add_tail(&prm->sess_works_list_entry, &tgt->sess_works_list);
1553 spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
1555 schedule_work(&tgt->sess_work);
1561 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
1563 static void qlt_send_notify_ack(struct scsi_qla_host *vha,
1564 struct imm_ntfy_from_isp *ntfy,
1565 uint32_t add_flags, uint16_t resp_code, int resp_code_valid,
1566 uint16_t srr_flags, uint16_t srr_reject_code, uint8_t srr_explan)
1568 struct qla_hw_data *ha = vha->hw;
1570 struct nack_to_isp *nack;
1572 if (!ha->flags.fw_started)
1575 ql_dbg(ql_dbg_tgt, vha, 0xe004, "Sending NOTIFY_ACK (ha=%p)\n", ha);
1577 /* Send marker if required */
1578 if (qlt_issue_marker(vha, 1) != QLA_SUCCESS)
1581 pkt = (request_t *)qla2x00_alloc_iocbs(vha, NULL);
1583 ql_dbg(ql_dbg_tgt, vha, 0xe049,
1584 "qla_target(%d): %s failed: unable to allocate "
1585 "request packet\n", vha->vp_idx, __func__);
1589 if (vha->vha_tgt.qla_tgt != NULL)
1590 vha->vha_tgt.qla_tgt->notify_ack_expected++;
1592 pkt->entry_type = NOTIFY_ACK_TYPE;
1593 pkt->entry_count = 1;
1595 nack = (struct nack_to_isp *)pkt;
1596 nack->ox_id = ntfy->ox_id;
1598 nack->u.isp24.handle = QLA_TGT_SKIP_HANDLE;
1599 nack->u.isp24.nport_handle = ntfy->u.isp24.nport_handle;
1600 if (le16_to_cpu(ntfy->u.isp24.status) == IMM_NTFY_ELS) {
1601 nack->u.isp24.flags = ntfy->u.isp24.flags &
1602 cpu_to_le32(NOTIFY24XX_FLAGS_PUREX_IOCB);
1604 nack->u.isp24.srr_rx_id = ntfy->u.isp24.srr_rx_id;
1605 nack->u.isp24.status = ntfy->u.isp24.status;
1606 nack->u.isp24.status_subcode = ntfy->u.isp24.status_subcode;
1607 nack->u.isp24.fw_handle = ntfy->u.isp24.fw_handle;
1608 nack->u.isp24.exchange_address = ntfy->u.isp24.exchange_address;
1609 nack->u.isp24.srr_rel_offs = ntfy->u.isp24.srr_rel_offs;
1610 nack->u.isp24.srr_ui = ntfy->u.isp24.srr_ui;
1611 nack->u.isp24.srr_flags = cpu_to_le16(srr_flags);
1612 nack->u.isp24.srr_reject_code = srr_reject_code;
1613 nack->u.isp24.srr_reject_code_expl = srr_explan;
1614 nack->u.isp24.vp_index = ntfy->u.isp24.vp_index;
1616 ql_dbg(ql_dbg_tgt, vha, 0xe005,
1617 "qla_target(%d): Sending 24xx Notify Ack %d\n",
1618 vha->vp_idx, nack->u.isp24.status);
1620 /* Memory Barrier */
1622 qla2x00_start_iocbs(vha, vha->req);
1626 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
1628 static void qlt_24xx_send_abts_resp(struct scsi_qla_host *vha,
1629 struct abts_recv_from_24xx *abts, uint32_t status,
1632 struct qla_hw_data *ha = vha->hw;
1633 struct abts_resp_to_24xx *resp;
1637 ql_dbg(ql_dbg_tgt, vha, 0xe006,
1638 "Sending task mgmt ABTS response (ha=%p, atio=%p, status=%x\n",
1641 /* Send marker if required */
1642 if (qlt_issue_marker(vha, 1) != QLA_SUCCESS)
1645 resp = (struct abts_resp_to_24xx *)qla2x00_alloc_iocbs_ready(vha, NULL);
1647 ql_dbg(ql_dbg_tgt, vha, 0xe04a,
1648 "qla_target(%d): %s failed: unable to allocate "
1649 "request packet", vha->vp_idx, __func__);
1653 resp->entry_type = ABTS_RESP_24XX;
1654 resp->entry_count = 1;
1655 resp->nport_handle = abts->nport_handle;
1656 resp->vp_index = vha->vp_idx;
1657 resp->sof_type = abts->sof_type;
1658 resp->exchange_address = abts->exchange_address;
1659 resp->fcp_hdr_le = abts->fcp_hdr_le;
1660 f_ctl = cpu_to_le32(F_CTL_EXCH_CONTEXT_RESP |
1661 F_CTL_LAST_SEQ | F_CTL_END_SEQ |
1662 F_CTL_SEQ_INITIATIVE);
1663 p = (uint8_t *)&f_ctl;
1664 resp->fcp_hdr_le.f_ctl[0] = *p++;
1665 resp->fcp_hdr_le.f_ctl[1] = *p++;
1666 resp->fcp_hdr_le.f_ctl[2] = *p;
1668 resp->fcp_hdr_le.d_id[0] = abts->fcp_hdr_le.d_id[0];
1669 resp->fcp_hdr_le.d_id[1] = abts->fcp_hdr_le.d_id[1];
1670 resp->fcp_hdr_le.d_id[2] = abts->fcp_hdr_le.d_id[2];
1671 resp->fcp_hdr_le.s_id[0] = abts->fcp_hdr_le.s_id[0];
1672 resp->fcp_hdr_le.s_id[1] = abts->fcp_hdr_le.s_id[1];
1673 resp->fcp_hdr_le.s_id[2] = abts->fcp_hdr_le.s_id[2];
1675 resp->fcp_hdr_le.d_id[0] = abts->fcp_hdr_le.s_id[0];
1676 resp->fcp_hdr_le.d_id[1] = abts->fcp_hdr_le.s_id[1];
1677 resp->fcp_hdr_le.d_id[2] = abts->fcp_hdr_le.s_id[2];
1678 resp->fcp_hdr_le.s_id[0] = abts->fcp_hdr_le.d_id[0];
1679 resp->fcp_hdr_le.s_id[1] = abts->fcp_hdr_le.d_id[1];
1680 resp->fcp_hdr_le.s_id[2] = abts->fcp_hdr_le.d_id[2];
1682 resp->exchange_addr_to_abort = abts->exchange_addr_to_abort;
1683 if (status == FCP_TMF_CMPL) {
1684 resp->fcp_hdr_le.r_ctl = R_CTL_BASIC_LINK_SERV | R_CTL_B_ACC;
1685 resp->payload.ba_acct.seq_id_valid = SEQ_ID_INVALID;
1686 resp->payload.ba_acct.low_seq_cnt = 0x0000;
1687 resp->payload.ba_acct.high_seq_cnt = 0xFFFF;
1688 resp->payload.ba_acct.ox_id = abts->fcp_hdr_le.ox_id;
1689 resp->payload.ba_acct.rx_id = abts->fcp_hdr_le.rx_id;
1691 resp->fcp_hdr_le.r_ctl = R_CTL_BASIC_LINK_SERV | R_CTL_B_RJT;
1692 resp->payload.ba_rjt.reason_code =
1693 BA_RJT_REASON_CODE_UNABLE_TO_PERFORM;
1694 /* Other bytes are zero */
1697 vha->vha_tgt.qla_tgt->abts_resp_expected++;
1699 /* Memory Barrier */
1701 qla2x00_start_iocbs(vha, vha->req);
1705 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
1707 static void qlt_24xx_retry_term_exchange(struct scsi_qla_host *vha,
1708 struct abts_resp_from_24xx_fw *entry)
1710 struct ctio7_to_24xx *ctio;
1712 ql_dbg(ql_dbg_tgt, vha, 0xe007,
1713 "Sending retry TERM EXCH CTIO7 (ha=%p)\n", vha->hw);
1714 /* Send marker if required */
1715 if (qlt_issue_marker(vha, 1) != QLA_SUCCESS)
1718 ctio = (struct ctio7_to_24xx *)qla2x00_alloc_iocbs_ready(vha, NULL);
1720 ql_dbg(ql_dbg_tgt, vha, 0xe04b,
1721 "qla_target(%d): %s failed: unable to allocate "
1722 "request packet\n", vha->vp_idx, __func__);
1727 * We've got on entrance firmware's response on by us generated
1728 * ABTS response. So, in it ID fields are reversed.
1731 ctio->entry_type = CTIO_TYPE7;
1732 ctio->entry_count = 1;
1733 ctio->nport_handle = entry->nport_handle;
1734 ctio->handle = QLA_TGT_SKIP_HANDLE | CTIO_COMPLETION_HANDLE_MARK;
1735 ctio->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
1736 ctio->vp_index = vha->vp_idx;
1737 ctio->initiator_id[0] = entry->fcp_hdr_le.d_id[0];
1738 ctio->initiator_id[1] = entry->fcp_hdr_le.d_id[1];
1739 ctio->initiator_id[2] = entry->fcp_hdr_le.d_id[2];
1740 ctio->exchange_addr = entry->exchange_addr_to_abort;
1741 ctio->u.status1.flags = cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_1 |
1742 CTIO7_FLAGS_TERMINATE);
1743 ctio->u.status1.ox_id = cpu_to_le16(entry->fcp_hdr_le.ox_id);
1745 /* Memory Barrier */
1747 qla2x00_start_iocbs(vha, vha->req);
1749 qlt_24xx_send_abts_resp(vha, (struct abts_recv_from_24xx *)entry,
1750 FCP_TMF_CMPL, true);
1753 static int abort_cmd_for_tag(struct scsi_qla_host *vha, uint32_t tag)
1755 struct qla_tgt_sess_op *op;
1756 struct qla_tgt_cmd *cmd;
1757 unsigned long flags;
1759 spin_lock_irqsave(&vha->cmd_list_lock, flags);
1760 list_for_each_entry(op, &vha->qla_sess_op_cmd_list, cmd_list) {
1761 if (tag == op->atio.u.isp24.exchange_addr) {
1763 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
1768 list_for_each_entry(op, &vha->unknown_atio_list, cmd_list) {
1769 if (tag == op->atio.u.isp24.exchange_addr) {
1771 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
1776 list_for_each_entry(cmd, &vha->qla_cmd_list, cmd_list) {
1777 if (tag == cmd->atio.u.isp24.exchange_addr) {
1779 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
1783 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
1788 /* drop cmds for the given lun
1789 * XXX only looks for cmds on the port through which lun reset was recieved
1790 * XXX does not go through the list of other port (which may have cmds
1793 static void abort_cmds_for_lun(struct scsi_qla_host *vha,
1794 u64 lun, uint8_t *s_id)
1796 struct qla_tgt_sess_op *op;
1797 struct qla_tgt_cmd *cmd;
1799 unsigned long flags;
1801 key = sid_to_key(s_id);
1802 spin_lock_irqsave(&vha->cmd_list_lock, flags);
1803 list_for_each_entry(op, &vha->qla_sess_op_cmd_list, cmd_list) {
1807 op_key = sid_to_key(op->atio.u.isp24.fcp_hdr.s_id);
1808 op_lun = scsilun_to_int(
1809 (struct scsi_lun *)&op->atio.u.isp24.fcp_cmnd.lun);
1810 if (op_key == key && op_lun == lun)
1814 list_for_each_entry(op, &vha->unknown_atio_list, cmd_list) {
1818 op_key = sid_to_key(op->atio.u.isp24.fcp_hdr.s_id);
1819 op_lun = scsilun_to_int(
1820 (struct scsi_lun *)&op->atio.u.isp24.fcp_cmnd.lun);
1821 if (op_key == key && op_lun == lun)
1825 list_for_each_entry(cmd, &vha->qla_cmd_list, cmd_list) {
1829 cmd_key = sid_to_key(cmd->atio.u.isp24.fcp_hdr.s_id);
1830 cmd_lun = scsilun_to_int(
1831 (struct scsi_lun *)&cmd->atio.u.isp24.fcp_cmnd.lun);
1832 if (cmd_key == key && cmd_lun == lun)
1835 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
1838 /* ha->hardware_lock supposed to be held on entry */
1839 static int __qlt_24xx_handle_abts(struct scsi_qla_host *vha,
1840 struct abts_recv_from_24xx *abts, struct fc_port *sess)
1842 struct qla_hw_data *ha = vha->hw;
1843 struct se_session *se_sess = sess->se_sess;
1844 struct qla_tgt_mgmt_cmd *mcmd;
1845 struct qla_tgt_cmd *cmd;
1846 struct se_cmd *se_cmd;
1848 bool found_lun = false;
1849 unsigned long flags;
1851 spin_lock_irqsave(&se_sess->sess_cmd_lock, flags);
1852 list_for_each_entry(se_cmd, &se_sess->sess_cmd_list, se_cmd_list) {
1853 if (se_cmd->tag == abts->exchange_addr_to_abort) {
1858 spin_unlock_irqrestore(&se_sess->sess_cmd_lock, flags);
1860 /* cmd not in LIO lists, look in qla list */
1862 if (abort_cmd_for_tag(vha, abts->exchange_addr_to_abort)) {
1863 /* send TASK_ABORT response immediately */
1864 qlt_24xx_send_abts_resp(vha, abts, FCP_TMF_CMPL, false);
1867 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf081,
1868 "unable to find cmd in driver or LIO for tag 0x%x\n",
1869 abts->exchange_addr_to_abort);
1874 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00f,
1875 "qla_target(%d): task abort (tag=%d)\n",
1876 vha->vp_idx, abts->exchange_addr_to_abort);
1878 mcmd = mempool_alloc(qla_tgt_mgmt_cmd_mempool, GFP_ATOMIC);
1880 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf051,
1881 "qla_target(%d): %s: Allocation of ABORT cmd failed",
1882 vha->vp_idx, __func__);
1885 memset(mcmd, 0, sizeof(*mcmd));
1887 cmd = container_of(se_cmd, struct qla_tgt_cmd, se_cmd);
1889 memcpy(&mcmd->orig_iocb.abts, abts, sizeof(mcmd->orig_iocb.abts));
1890 mcmd->reset_count = vha->hw->chip_reset;
1891 mcmd->tmr_func = QLA_TGT_ABTS;
1893 rc = ha->tgt.tgt_ops->handle_tmr(mcmd, cmd->unpacked_lun, mcmd->tmr_func,
1894 abts->exchange_addr_to_abort);
1896 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf052,
1897 "qla_target(%d): tgt_ops->handle_tmr()"
1898 " failed: %d", vha->vp_idx, rc);
1899 mempool_free(mcmd, qla_tgt_mgmt_cmd_mempool);
1907 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
1909 static void qlt_24xx_handle_abts(struct scsi_qla_host *vha,
1910 struct abts_recv_from_24xx *abts)
1912 struct qla_hw_data *ha = vha->hw;
1913 struct fc_port *sess;
1914 uint32_t tag = abts->exchange_addr_to_abort;
1917 unsigned long flags;
1919 if (le32_to_cpu(abts->fcp_hdr_le.parameter) & ABTS_PARAM_ABORT_SEQ) {
1920 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf053,
1921 "qla_target(%d): ABTS: Abort Sequence not "
1922 "supported\n", vha->vp_idx);
1923 qlt_24xx_send_abts_resp(vha, abts, FCP_TMF_REJECTED, false);
1927 if (tag == ATIO_EXCHANGE_ADDRESS_UNKNOWN) {
1928 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf010,
1929 "qla_target(%d): ABTS: Unknown Exchange "
1930 "Address received\n", vha->vp_idx);
1931 qlt_24xx_send_abts_resp(vha, abts, FCP_TMF_REJECTED, false);
1935 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf011,
1936 "qla_target(%d): task abort (s_id=%x:%x:%x, "
1937 "tag=%d, param=%x)\n", vha->vp_idx, abts->fcp_hdr_le.s_id[2],
1938 abts->fcp_hdr_le.s_id[1], abts->fcp_hdr_le.s_id[0], tag,
1939 le32_to_cpu(abts->fcp_hdr_le.parameter));
1941 s_id[0] = abts->fcp_hdr_le.s_id[2];
1942 s_id[1] = abts->fcp_hdr_le.s_id[1];
1943 s_id[2] = abts->fcp_hdr_le.s_id[0];
1945 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
1946 sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha, s_id);
1948 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf012,
1949 "qla_target(%d): task abort for non-existant session\n",
1951 rc = qlt_sched_sess_work(vha->vha_tgt.qla_tgt,
1952 QLA_TGT_SESS_WORK_ABORT, abts, sizeof(*abts));
1954 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
1957 qlt_24xx_send_abts_resp(vha, abts, FCP_TMF_REJECTED,
1962 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
1965 if (sess->deleted) {
1966 qlt_24xx_send_abts_resp(vha, abts, FCP_TMF_REJECTED, false);
1970 rc = __qlt_24xx_handle_abts(vha, abts, sess);
1972 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf054,
1973 "qla_target(%d): __qlt_24xx_handle_abts() failed: %d\n",
1975 qlt_24xx_send_abts_resp(vha, abts, FCP_TMF_REJECTED, false);
1981 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
1983 static void qlt_24xx_send_task_mgmt_ctio(struct scsi_qla_host *ha,
1984 struct qla_tgt_mgmt_cmd *mcmd, uint32_t resp_code)
1986 struct atio_from_isp *atio = &mcmd->orig_iocb.atio;
1987 struct ctio7_to_24xx *ctio;
1990 ql_dbg(ql_dbg_tgt, ha, 0xe008,
1991 "Sending task mgmt CTIO7 (ha=%p, atio=%p, resp_code=%x\n",
1992 ha, atio, resp_code);
1994 /* Send marker if required */
1995 if (qlt_issue_marker(ha, 1) != QLA_SUCCESS)
1998 ctio = (struct ctio7_to_24xx *)qla2x00_alloc_iocbs(ha, NULL);
2000 ql_dbg(ql_dbg_tgt, ha, 0xe04c,
2001 "qla_target(%d): %s failed: unable to allocate "
2002 "request packet\n", ha->vp_idx, __func__);
2006 ctio->entry_type = CTIO_TYPE7;
2007 ctio->entry_count = 1;
2008 ctio->handle = QLA_TGT_SKIP_HANDLE | CTIO_COMPLETION_HANDLE_MARK;
2009 ctio->nport_handle = mcmd->sess->loop_id;
2010 ctio->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
2011 ctio->vp_index = ha->vp_idx;
2012 ctio->initiator_id[0] = atio->u.isp24.fcp_hdr.s_id[2];
2013 ctio->initiator_id[1] = atio->u.isp24.fcp_hdr.s_id[1];
2014 ctio->initiator_id[2] = atio->u.isp24.fcp_hdr.s_id[0];
2015 ctio->exchange_addr = atio->u.isp24.exchange_addr;
2016 temp = (atio->u.isp24.attr << 9)|
2017 CTIO7_FLAGS_STATUS_MODE_1 | CTIO7_FLAGS_SEND_STATUS;
2018 ctio->u.status1.flags = cpu_to_le16(temp);
2019 temp = be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id);
2020 ctio->u.status1.ox_id = cpu_to_le16(temp);
2021 ctio->u.status1.scsi_status =
2022 cpu_to_le16(SS_RESPONSE_INFO_LEN_VALID);
2023 ctio->u.status1.response_len = cpu_to_le16(8);
2024 ctio->u.status1.sense_data[0] = resp_code;
2026 /* Memory Barrier */
2028 qla2x00_start_iocbs(ha, ha->req);
2031 void qlt_free_mcmd(struct qla_tgt_mgmt_cmd *mcmd)
2033 mempool_free(mcmd, qla_tgt_mgmt_cmd_mempool);
2035 EXPORT_SYMBOL(qlt_free_mcmd);
2038 * ha->hardware_lock supposed to be held on entry. Might drop it, then
2041 void qlt_send_resp_ctio(scsi_qla_host_t *vha, struct qla_tgt_cmd *cmd,
2042 uint8_t scsi_status, uint8_t sense_key, uint8_t asc, uint8_t ascq)
2044 struct atio_from_isp *atio = &cmd->atio;
2045 struct ctio7_to_24xx *ctio;
2048 ql_dbg(ql_dbg_tgt_dif, vha, 0x3066,
2049 "Sending response CTIO7 (vha=%p, atio=%p, scsi_status=%02x, "
2050 "sense_key=%02x, asc=%02x, ascq=%02x",
2051 vha, atio, scsi_status, sense_key, asc, ascq);
2053 ctio = (struct ctio7_to_24xx *)qla2x00_alloc_iocbs(vha, NULL);
2055 ql_dbg(ql_dbg_async, vha, 0x3067,
2056 "qla2x00t(%ld): %s failed: unable to allocate request packet",
2057 vha->host_no, __func__);
2061 ctio->entry_type = CTIO_TYPE7;
2062 ctio->entry_count = 1;
2063 ctio->handle = QLA_TGT_SKIP_HANDLE;
2064 ctio->nport_handle = cmd->sess->loop_id;
2065 ctio->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
2066 ctio->vp_index = vha->vp_idx;
2067 ctio->initiator_id[0] = atio->u.isp24.fcp_hdr.s_id[2];
2068 ctio->initiator_id[1] = atio->u.isp24.fcp_hdr.s_id[1];
2069 ctio->initiator_id[2] = atio->u.isp24.fcp_hdr.s_id[0];
2070 ctio->exchange_addr = atio->u.isp24.exchange_addr;
2071 temp = (atio->u.isp24.attr << 9) |
2072 CTIO7_FLAGS_STATUS_MODE_1 | CTIO7_FLAGS_SEND_STATUS;
2073 ctio->u.status1.flags = cpu_to_le16(temp);
2074 temp = be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id);
2075 ctio->u.status1.ox_id = cpu_to_le16(temp);
2076 ctio->u.status1.scsi_status =
2077 cpu_to_le16(SS_RESPONSE_INFO_LEN_VALID | scsi_status);
2078 ctio->u.status1.response_len = cpu_to_le16(18);
2079 ctio->u.status1.residual = cpu_to_le32(get_datalen_for_atio(atio));
2081 if (ctio->u.status1.residual != 0)
2082 ctio->u.status1.scsi_status |=
2083 cpu_to_le16(SS_RESIDUAL_UNDER);
2085 /* Response code and sense key */
2086 put_unaligned_le32(((0x70 << 24) | (sense_key << 8)),
2087 (&ctio->u.status1.sense_data)[0]);
2088 /* Additional sense length */
2089 put_unaligned_le32(0x0a, (&ctio->u.status1.sense_data)[1]);
2091 put_unaligned_le32(((asc << 24) | (ascq << 16)),
2092 (&ctio->u.status1.sense_data)[3]);
2094 /* Memory Barrier */
2097 qla2x00_start_iocbs(vha, vha->req);
2102 /* callback from target fabric module code */
2103 void qlt_xmit_tm_rsp(struct qla_tgt_mgmt_cmd *mcmd)
2105 struct scsi_qla_host *vha = mcmd->sess->vha;
2106 struct qla_hw_data *ha = vha->hw;
2107 unsigned long flags;
2109 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf013,
2110 "TM response mcmd (%p) status %#x state %#x",
2111 mcmd, mcmd->fc_tm_rsp, mcmd->flags);
2113 spin_lock_irqsave(&ha->hardware_lock, flags);
2115 if (!vha->flags.online || mcmd->reset_count != ha->chip_reset) {
2117 * Either the port is not online or this request was from
2118 * previous life, just abort the processing.
2120 ql_dbg(ql_dbg_async, vha, 0xe100,
2121 "RESET-TMR online/active/old-count/new-count = %d/%d/%d/%d.\n",
2122 vha->flags.online, qla2x00_reset_active(vha),
2123 mcmd->reset_count, ha->chip_reset);
2124 ha->tgt.tgt_ops->free_mcmd(mcmd);
2125 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2129 if (mcmd->flags == QLA24XX_MGMT_SEND_NACK) {
2130 if (mcmd->orig_iocb.imm_ntfy.u.isp24.status_subcode ==
2132 mcmd->orig_iocb.imm_ntfy.u.isp24.status_subcode ==
2134 mcmd->orig_iocb.imm_ntfy.u.isp24.status_subcode ==
2136 ql_dbg(ql_dbg_disc, vha, 0x2106,
2137 "TM response logo %phC status %#x state %#x",
2138 mcmd->sess->port_name, mcmd->fc_tm_rsp,
2140 qlt_schedule_sess_for_deletion_lock(mcmd->sess);
2142 qlt_send_notify_ack(vha, &mcmd->orig_iocb.imm_ntfy,
2146 if (mcmd->orig_iocb.atio.u.raw.entry_type == ABTS_RECV_24XX)
2147 qlt_24xx_send_abts_resp(vha, &mcmd->orig_iocb.abts,
2148 mcmd->fc_tm_rsp, false);
2150 qlt_24xx_send_task_mgmt_ctio(vha, mcmd,
2154 * Make the callback for ->free_mcmd() to queue_work() and invoke
2155 * target_put_sess_cmd() to drop cmd_kref to 1. The final
2156 * target_put_sess_cmd() call will be made from TFO->check_stop_free()
2157 * -> tcm_qla2xxx_check_stop_free() to release the TMR associated se_cmd
2158 * descriptor after TFO->queue_tm_rsp() -> tcm_qla2xxx_queue_tm_rsp() ->
2159 * qlt_xmit_tm_rsp() returns here..
2161 ha->tgt.tgt_ops->free_mcmd(mcmd);
2162 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2164 EXPORT_SYMBOL(qlt_xmit_tm_rsp);
2167 static int qlt_pci_map_calc_cnt(struct qla_tgt_prm *prm)
2169 struct qla_tgt_cmd *cmd = prm->cmd;
2171 BUG_ON(cmd->sg_cnt == 0);
2173 prm->sg = (struct scatterlist *)cmd->sg;
2174 prm->seg_cnt = pci_map_sg(prm->tgt->ha->pdev, cmd->sg,
2175 cmd->sg_cnt, cmd->dma_data_direction);
2176 if (unlikely(prm->seg_cnt == 0))
2179 prm->cmd->sg_mapped = 1;
2181 if (cmd->se_cmd.prot_op == TARGET_PROT_NORMAL) {
2183 * If greater than four sg entries then we need to allocate
2184 * the continuation entries
2186 if (prm->seg_cnt > prm->tgt->datasegs_per_cmd)
2187 prm->req_cnt += DIV_ROUND_UP(prm->seg_cnt -
2188 prm->tgt->datasegs_per_cmd,
2189 prm->tgt->datasegs_per_cont);
2192 if ((cmd->se_cmd.prot_op == TARGET_PROT_DIN_INSERT) ||
2193 (cmd->se_cmd.prot_op == TARGET_PROT_DOUT_STRIP)) {
2194 prm->seg_cnt = DIV_ROUND_UP(cmd->bufflen, cmd->blk_sz);
2195 prm->tot_dsds = prm->seg_cnt;
2197 prm->tot_dsds = prm->seg_cnt;
2199 if (cmd->prot_sg_cnt) {
2200 prm->prot_sg = cmd->prot_sg;
2201 prm->prot_seg_cnt = pci_map_sg(prm->tgt->ha->pdev,
2202 cmd->prot_sg, cmd->prot_sg_cnt,
2203 cmd->dma_data_direction);
2204 if (unlikely(prm->prot_seg_cnt == 0))
2207 if ((cmd->se_cmd.prot_op == TARGET_PROT_DIN_INSERT) ||
2208 (cmd->se_cmd.prot_op == TARGET_PROT_DOUT_STRIP)) {
2209 /* Dif Bundling not support here */
2210 prm->prot_seg_cnt = DIV_ROUND_UP(cmd->bufflen,
2212 prm->tot_dsds += prm->prot_seg_cnt;
2214 prm->tot_dsds += prm->prot_seg_cnt;
2221 ql_dbg(ql_dbg_tgt, prm->cmd->vha, 0xe04d,
2222 "qla_target(%d): PCI mapping failed: sg_cnt=%d",
2223 0, prm->cmd->sg_cnt);
2227 static void qlt_unmap_sg(struct scsi_qla_host *vha, struct qla_tgt_cmd *cmd)
2229 struct qla_hw_data *ha = vha->hw;
2231 if (!cmd->sg_mapped)
2234 pci_unmap_sg(ha->pdev, cmd->sg, cmd->sg_cnt, cmd->dma_data_direction);
2237 if (cmd->prot_sg_cnt)
2238 pci_unmap_sg(ha->pdev, cmd->prot_sg, cmd->prot_sg_cnt,
2239 cmd->dma_data_direction);
2244 if (cmd->ctx_dsd_alloced)
2245 qla2x00_clean_dsd_pool(ha, cmd->ctx);
2247 dma_pool_free(ha->dl_dma_pool, cmd->ctx, cmd->ctx->crc_ctx_dma);
2250 static int qlt_check_reserve_free_req(struct scsi_qla_host *vha,
2255 if (vha->req->cnt < (req_cnt + 2)) {
2256 cnt = (uint16_t)RD_REG_DWORD(vha->req->req_q_out);
2258 if (vha->req->ring_index < cnt)
2259 vha->req->cnt = cnt - vha->req->ring_index;
2261 vha->req->cnt = vha->req->length -
2262 (vha->req->ring_index - cnt);
2264 if (unlikely(vha->req->cnt < (req_cnt + 2)))
2268 vha->req->cnt -= req_cnt;
2274 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
2276 static inline void *qlt_get_req_pkt(struct scsi_qla_host *vha)
2278 /* Adjust ring index. */
2279 vha->req->ring_index++;
2280 if (vha->req->ring_index == vha->req->length) {
2281 vha->req->ring_index = 0;
2282 vha->req->ring_ptr = vha->req->ring;
2284 vha->req->ring_ptr++;
2286 return (cont_entry_t *)vha->req->ring_ptr;
2289 /* ha->hardware_lock supposed to be held on entry */
2290 static inline uint32_t qlt_make_handle(struct scsi_qla_host *vha)
2295 struct req_que *req = vha->req;
2297 h = req->current_outstanding_cmd;
2299 for (index = 1; index < req->num_outstanding_cmds; index++) {
2301 if (h == req->num_outstanding_cmds)
2304 if (h == QLA_TGT_SKIP_HANDLE)
2307 if (!req->outstanding_cmds[h]) {
2314 req->current_outstanding_cmd = h;
2316 ql_dbg(ql_dbg_io, vha, 0x305b,
2317 "qla_target(%d): Ran out of empty cmd slots\n",
2319 h = QLA_TGT_NULL_HANDLE;
2325 /* ha->hardware_lock supposed to be held on entry */
2326 static int qlt_24xx_build_ctio_pkt(struct qla_tgt_prm *prm,
2327 struct scsi_qla_host *vha)
2330 struct ctio7_to_24xx *pkt;
2331 struct atio_from_isp *atio = &prm->cmd->atio;
2334 pkt = (struct ctio7_to_24xx *)vha->req->ring_ptr;
2336 memset(pkt, 0, sizeof(*pkt));
2338 pkt->entry_type = CTIO_TYPE7;
2339 pkt->entry_count = (uint8_t)prm->req_cnt;
2340 pkt->vp_index = vha->vp_idx;
2342 h = qlt_make_handle(vha);
2343 if (unlikely(h == QLA_TGT_NULL_HANDLE)) {
2345 * CTIO type 7 from the firmware doesn't provide a way to
2346 * know the initiator's LOOP ID, hence we can't find
2347 * the session and, so, the command.
2351 vha->req->outstanding_cmds[h] = (srb_t *)prm->cmd;
2354 pkt->handle = h | CTIO_COMPLETION_HANDLE_MARK;
2355 pkt->nport_handle = prm->cmd->loop_id;
2356 pkt->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
2357 pkt->initiator_id[0] = atio->u.isp24.fcp_hdr.s_id[2];
2358 pkt->initiator_id[1] = atio->u.isp24.fcp_hdr.s_id[1];
2359 pkt->initiator_id[2] = atio->u.isp24.fcp_hdr.s_id[0];
2360 pkt->exchange_addr = atio->u.isp24.exchange_addr;
2361 temp = atio->u.isp24.attr << 9;
2362 pkt->u.status0.flags |= cpu_to_le16(temp);
2363 temp = be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id);
2364 pkt->u.status0.ox_id = cpu_to_le16(temp);
2365 pkt->u.status0.relative_offset = cpu_to_le32(prm->cmd->offset);
2371 * ha->hardware_lock supposed to be held on entry. We have already made sure
2372 * that there is sufficient amount of request entries to not drop it.
2374 static void qlt_load_cont_data_segments(struct qla_tgt_prm *prm,
2375 struct scsi_qla_host *vha)
2378 uint32_t *dword_ptr;
2379 int enable_64bit_addressing = prm->tgt->tgt_enable_64bit_addr;
2381 /* Build continuation packets */
2382 while (prm->seg_cnt > 0) {
2383 cont_a64_entry_t *cont_pkt64 =
2384 (cont_a64_entry_t *)qlt_get_req_pkt(vha);
2387 * Make sure that from cont_pkt64 none of
2388 * 64-bit specific fields used for 32-bit
2389 * addressing. Cast to (cont_entry_t *) for
2393 memset(cont_pkt64, 0, sizeof(*cont_pkt64));
2395 cont_pkt64->entry_count = 1;
2396 cont_pkt64->sys_define = 0;
2398 if (enable_64bit_addressing) {
2399 cont_pkt64->entry_type = CONTINUE_A64_TYPE;
2401 (uint32_t *)&cont_pkt64->dseg_0_address;
2403 cont_pkt64->entry_type = CONTINUE_TYPE;
2405 (uint32_t *)&((cont_entry_t *)
2406 cont_pkt64)->dseg_0_address;
2409 /* Load continuation entry data segments */
2411 cnt < prm->tgt->datasegs_per_cont && prm->seg_cnt;
2412 cnt++, prm->seg_cnt--) {
2414 cpu_to_le32(pci_dma_lo32
2415 (sg_dma_address(prm->sg)));
2416 if (enable_64bit_addressing) {
2418 cpu_to_le32(pci_dma_hi32
2422 *dword_ptr++ = cpu_to_le32(sg_dma_len(prm->sg));
2424 prm->sg = sg_next(prm->sg);
2430 * ha->hardware_lock supposed to be held on entry. We have already made sure
2431 * that there is sufficient amount of request entries to not drop it.
2433 static void qlt_load_data_segments(struct qla_tgt_prm *prm,
2434 struct scsi_qla_host *vha)
2437 uint32_t *dword_ptr;
2438 int enable_64bit_addressing = prm->tgt->tgt_enable_64bit_addr;
2439 struct ctio7_to_24xx *pkt24 = (struct ctio7_to_24xx *)prm->pkt;
2441 pkt24->u.status0.transfer_length = cpu_to_le32(prm->cmd->bufflen);
2443 /* Setup packet address segment pointer */
2444 dword_ptr = pkt24->u.status0.dseg_0_address;
2446 /* Set total data segment count */
2448 pkt24->dseg_count = cpu_to_le16(prm->seg_cnt);
2450 if (prm->seg_cnt == 0) {
2451 /* No data transfer */
2457 /* If scatter gather */
2459 /* Load command entry data segments */
2461 (cnt < prm->tgt->datasegs_per_cmd) && prm->seg_cnt;
2462 cnt++, prm->seg_cnt--) {
2464 cpu_to_le32(pci_dma_lo32(sg_dma_address(prm->sg)));
2465 if (enable_64bit_addressing) {
2467 cpu_to_le32(pci_dma_hi32(
2468 sg_dma_address(prm->sg)));
2470 *dword_ptr++ = cpu_to_le32(sg_dma_len(prm->sg));
2472 prm->sg = sg_next(prm->sg);
2475 qlt_load_cont_data_segments(prm, vha);
2478 static inline int qlt_has_data(struct qla_tgt_cmd *cmd)
2480 return cmd->bufflen > 0;
2483 static void qlt_print_dif_err(struct qla_tgt_prm *prm)
2485 struct qla_tgt_cmd *cmd;
2486 struct scsi_qla_host *vha;
2488 /* asc 0x10=dif error */
2489 if (prm->sense_buffer && (prm->sense_buffer[12] == 0x10)) {
2493 switch (prm->sense_buffer[13]) {
2495 ql_dbg(ql_dbg_tgt_dif, vha, 0xe00b,
2496 "BE detected Guard TAG ERR: lba[0x%llx|%lld] len[0x%x] "
2497 "se_cmd=%p tag[%x]",
2498 cmd->lba, cmd->lba, cmd->num_blks, &cmd->se_cmd,
2499 cmd->atio.u.isp24.exchange_addr);
2502 ql_dbg(ql_dbg_tgt_dif, vha, 0xe00c,
2503 "BE detected APP TAG ERR: lba[0x%llx|%lld] len[0x%x] "
2504 "se_cmd=%p tag[%x]",
2505 cmd->lba, cmd->lba, cmd->num_blks, &cmd->se_cmd,
2506 cmd->atio.u.isp24.exchange_addr);
2509 ql_dbg(ql_dbg_tgt_dif, vha, 0xe00f,
2510 "BE detected REF TAG ERR: lba[0x%llx|%lld] len[0x%x] "
2511 "se_cmd=%p tag[%x]",
2512 cmd->lba, cmd->lba, cmd->num_blks, &cmd->se_cmd,
2513 cmd->atio.u.isp24.exchange_addr);
2516 ql_dbg(ql_dbg_tgt_dif, vha, 0xe010,
2517 "BE detected Dif ERR: lba[%llx|%lld] len[%x] "
2518 "se_cmd=%p tag[%x]",
2519 cmd->lba, cmd->lba, cmd->num_blks, &cmd->se_cmd,
2520 cmd->atio.u.isp24.exchange_addr);
2523 ql_dump_buffer(ql_dbg_tgt_dif, vha, 0xe011, cmd->cdb, 16);
2528 * Called without ha->hardware_lock held
2530 static int qlt_pre_xmit_response(struct qla_tgt_cmd *cmd,
2531 struct qla_tgt_prm *prm, int xmit_type, uint8_t scsi_status,
2532 uint32_t *full_req_cnt)
2534 struct qla_tgt *tgt = cmd->tgt;
2535 struct scsi_qla_host *vha = tgt->vha;
2536 struct qla_hw_data *ha = vha->hw;
2537 struct se_cmd *se_cmd = &cmd->se_cmd;
2541 prm->rq_result = scsi_status;
2542 prm->sense_buffer = &cmd->sense_buffer[0];
2543 prm->sense_buffer_len = TRANSPORT_SENSE_BUFFER;
2547 prm->add_status_pkt = 0;
2549 /* Send marker if required */
2550 if (qlt_issue_marker(vha, 0) != QLA_SUCCESS)
2553 if ((xmit_type & QLA_TGT_XMIT_DATA) && qlt_has_data(cmd)) {
2554 if (qlt_pci_map_calc_cnt(prm) != 0)
2558 *full_req_cnt = prm->req_cnt;
2560 if (se_cmd->se_cmd_flags & SCF_UNDERFLOW_BIT) {
2561 prm->residual = se_cmd->residual_count;
2562 ql_dbg(ql_dbg_io + ql_dbg_verbose, vha, 0x305c,
2563 "Residual underflow: %d (tag %lld, op %x, bufflen %d, rq_result %x)\n",
2564 prm->residual, se_cmd->tag,
2565 se_cmd->t_task_cdb ? se_cmd->t_task_cdb[0] : 0,
2566 cmd->bufflen, prm->rq_result);
2567 prm->rq_result |= SS_RESIDUAL_UNDER;
2568 } else if (se_cmd->se_cmd_flags & SCF_OVERFLOW_BIT) {
2569 prm->residual = se_cmd->residual_count;
2570 ql_dbg(ql_dbg_io, vha, 0x305d,
2571 "Residual overflow: %d (tag %lld, op %x, bufflen %d, rq_result %x)\n",
2572 prm->residual, se_cmd->tag, se_cmd->t_task_cdb ?
2573 se_cmd->t_task_cdb[0] : 0, cmd->bufflen, prm->rq_result);
2574 prm->rq_result |= SS_RESIDUAL_OVER;
2577 if (xmit_type & QLA_TGT_XMIT_STATUS) {
2579 * If QLA_TGT_XMIT_DATA is not set, add_status_pkt will be
2580 * ignored in *xmit_response() below
2582 if (qlt_has_data(cmd)) {
2583 if (QLA_TGT_SENSE_VALID(prm->sense_buffer) ||
2584 (IS_FWI2_CAPABLE(ha) &&
2585 (prm->rq_result != 0))) {
2586 prm->add_status_pkt = 1;
2595 static inline int qlt_need_explicit_conf(struct qla_hw_data *ha,
2596 struct qla_tgt_cmd *cmd, int sending_sense)
2598 if (ha->tgt.enable_class_2)
2602 return cmd->conf_compl_supported;
2604 return ha->tgt.enable_explicit_conf &&
2605 cmd->conf_compl_supported;
2608 static void qlt_24xx_init_ctio_to_isp(struct ctio7_to_24xx *ctio,
2609 struct qla_tgt_prm *prm)
2611 prm->sense_buffer_len = min_t(uint32_t, prm->sense_buffer_len,
2612 (uint32_t)sizeof(ctio->u.status1.sense_data));
2613 ctio->u.status0.flags |= cpu_to_le16(CTIO7_FLAGS_SEND_STATUS);
2614 if (qlt_need_explicit_conf(prm->tgt->ha, prm->cmd, 0)) {
2615 ctio->u.status0.flags |= cpu_to_le16(
2616 CTIO7_FLAGS_EXPLICIT_CONFORM |
2617 CTIO7_FLAGS_CONFORM_REQ);
2619 ctio->u.status0.residual = cpu_to_le32(prm->residual);
2620 ctio->u.status0.scsi_status = cpu_to_le16(prm->rq_result);
2621 if (QLA_TGT_SENSE_VALID(prm->sense_buffer)) {
2624 if (qlt_need_explicit_conf(prm->tgt->ha, prm->cmd, 1)) {
2625 if ((prm->rq_result & SS_SCSI_STATUS_BYTE) != 0) {
2626 ql_dbg(ql_dbg_tgt, prm->cmd->vha, 0xe017,
2627 "Skipping EXPLICIT_CONFORM and "
2628 "CTIO7_FLAGS_CONFORM_REQ for FCP READ w/ "
2629 "non GOOD status\n");
2630 goto skip_explict_conf;
2632 ctio->u.status1.flags |= cpu_to_le16(
2633 CTIO7_FLAGS_EXPLICIT_CONFORM |
2634 CTIO7_FLAGS_CONFORM_REQ);
2637 ctio->u.status1.flags &=
2638 ~cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_0);
2639 ctio->u.status1.flags |=
2640 cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_1);
2641 ctio->u.status1.scsi_status |=
2642 cpu_to_le16(SS_SENSE_LEN_VALID);
2643 ctio->u.status1.sense_length =
2644 cpu_to_le16(prm->sense_buffer_len);
2645 for (i = 0; i < prm->sense_buffer_len/4; i++)
2646 ((uint32_t *)ctio->u.status1.sense_data)[i] =
2647 cpu_to_be32(((uint32_t *)prm->sense_buffer)[i]);
2649 qlt_print_dif_err(prm);
2652 ctio->u.status1.flags &=
2653 ~cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_0);
2654 ctio->u.status1.flags |=
2655 cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_1);
2656 ctio->u.status1.sense_length = 0;
2657 memset(ctio->u.status1.sense_data, 0,
2658 sizeof(ctio->u.status1.sense_data));
2661 /* Sense with len > 24, is it possible ??? */
2665 qlt_hba_err_chk_enabled(struct se_cmd *se_cmd)
2667 switch (se_cmd->prot_op) {
2668 case TARGET_PROT_DOUT_INSERT:
2669 case TARGET_PROT_DIN_STRIP:
2670 if (ql2xenablehba_err_chk >= 1)
2673 case TARGET_PROT_DOUT_PASS:
2674 case TARGET_PROT_DIN_PASS:
2675 if (ql2xenablehba_err_chk >= 2)
2678 case TARGET_PROT_DIN_INSERT:
2679 case TARGET_PROT_DOUT_STRIP:
2688 qla_tgt_ref_mask_check(struct se_cmd *se_cmd)
2690 switch (se_cmd->prot_op) {
2691 case TARGET_PROT_DIN_INSERT:
2692 case TARGET_PROT_DOUT_INSERT:
2693 case TARGET_PROT_DIN_STRIP:
2694 case TARGET_PROT_DOUT_STRIP:
2695 case TARGET_PROT_DIN_PASS:
2696 case TARGET_PROT_DOUT_PASS:
2705 * qla_tgt_set_dif_tags - Extract Ref and App tags from SCSI command
2708 qla_tgt_set_dif_tags(struct qla_tgt_cmd *cmd, struct crc_context *ctx,
2709 uint16_t *pfw_prot_opts)
2711 struct se_cmd *se_cmd = &cmd->se_cmd;
2712 uint32_t lba = 0xffffffff & se_cmd->t_task_lba;
2713 scsi_qla_host_t *vha = cmd->tgt->vha;
2714 struct qla_hw_data *ha = vha->hw;
2718 * wait till Mode Sense/Select cmd, modepage Ah, subpage 2
2719 * have been immplemented by TCM, before AppTag is avail.
2720 * Look for modesense_handlers[]
2723 ctx->app_tag_mask[0] = 0x0;
2724 ctx->app_tag_mask[1] = 0x0;
2726 if (IS_PI_UNINIT_CAPABLE(ha)) {
2727 if ((se_cmd->prot_type == TARGET_DIF_TYPE1_PROT) ||
2728 (se_cmd->prot_type == TARGET_DIF_TYPE2_PROT))
2729 *pfw_prot_opts |= PO_DIS_VALD_APP_ESC;
2730 else if (se_cmd->prot_type == TARGET_DIF_TYPE3_PROT)
2731 *pfw_prot_opts |= PO_DIS_VALD_APP_REF_ESC;
2734 t32 = ha->tgt.tgt_ops->get_dif_tags(cmd, pfw_prot_opts);
2736 switch (se_cmd->prot_type) {
2737 case TARGET_DIF_TYPE0_PROT:
2739 * No check for ql2xenablehba_err_chk, as it
2740 * would be an I/O error if hba tag generation
2743 ctx->ref_tag = cpu_to_le32(lba);
2744 /* enable ALL bytes of the ref tag */
2745 ctx->ref_tag_mask[0] = 0xff;
2746 ctx->ref_tag_mask[1] = 0xff;
2747 ctx->ref_tag_mask[2] = 0xff;
2748 ctx->ref_tag_mask[3] = 0xff;
2750 case TARGET_DIF_TYPE1_PROT:
2752 * For TYPE 1 protection: 16 bit GUARD tag, 32 bit
2753 * REF tag, and 16 bit app tag.
2755 ctx->ref_tag = cpu_to_le32(lba);
2756 if (!qla_tgt_ref_mask_check(se_cmd) ||
2757 !(ha->tgt.tgt_ops->chk_dif_tags(t32))) {
2758 *pfw_prot_opts |= PO_DIS_REF_TAG_VALD;
2761 /* enable ALL bytes of the ref tag */
2762 ctx->ref_tag_mask[0] = 0xff;
2763 ctx->ref_tag_mask[1] = 0xff;
2764 ctx->ref_tag_mask[2] = 0xff;
2765 ctx->ref_tag_mask[3] = 0xff;
2767 case TARGET_DIF_TYPE2_PROT:
2769 * For TYPE 2 protection: 16 bit GUARD + 32 bit REF
2770 * tag has to match LBA in CDB + N
2772 ctx->ref_tag = cpu_to_le32(lba);
2773 if (!qla_tgt_ref_mask_check(se_cmd) ||
2774 !(ha->tgt.tgt_ops->chk_dif_tags(t32))) {
2775 *pfw_prot_opts |= PO_DIS_REF_TAG_VALD;
2778 /* enable ALL bytes of the ref tag */
2779 ctx->ref_tag_mask[0] = 0xff;
2780 ctx->ref_tag_mask[1] = 0xff;
2781 ctx->ref_tag_mask[2] = 0xff;
2782 ctx->ref_tag_mask[3] = 0xff;
2784 case TARGET_DIF_TYPE3_PROT:
2785 /* For TYPE 3 protection: 16 bit GUARD only */
2786 *pfw_prot_opts |= PO_DIS_REF_TAG_VALD;
2787 ctx->ref_tag_mask[0] = ctx->ref_tag_mask[1] =
2788 ctx->ref_tag_mask[2] = ctx->ref_tag_mask[3] = 0x00;
2794 qlt_build_ctio_crc2_pkt(struct qla_tgt_prm *prm, scsi_qla_host_t *vha)
2797 uint32_t transfer_length = 0;
2798 uint32_t data_bytes;
2800 uint8_t bundling = 1;
2802 struct crc_context *crc_ctx_pkt = NULL;
2803 struct qla_hw_data *ha;
2804 struct ctio_crc2_to_fw *pkt;
2805 dma_addr_t crc_ctx_dma;
2806 uint16_t fw_prot_opts = 0;
2807 struct qla_tgt_cmd *cmd = prm->cmd;
2808 struct se_cmd *se_cmd = &cmd->se_cmd;
2810 struct atio_from_isp *atio = &prm->cmd->atio;
2811 struct qla_tc_param tc;
2816 pkt = (struct ctio_crc2_to_fw *)vha->req->ring_ptr;
2818 memset(pkt, 0, sizeof(*pkt));
2820 ql_dbg(ql_dbg_tgt, vha, 0xe071,
2821 "qla_target(%d):%s: se_cmd[%p] CRC2 prot_op[0x%x] cmd prot sg:cnt[%p:%x] lba[%llu]\n",
2822 vha->vp_idx, __func__, se_cmd, se_cmd->prot_op,
2823 prm->prot_sg, prm->prot_seg_cnt, se_cmd->t_task_lba);
2825 if ((se_cmd->prot_op == TARGET_PROT_DIN_INSERT) ||
2826 (se_cmd->prot_op == TARGET_PROT_DOUT_STRIP))
2829 /* Compute dif len and adjust data len to incude protection */
2830 data_bytes = cmd->bufflen;
2831 dif_bytes = (data_bytes / cmd->blk_sz) * 8;
2833 switch (se_cmd->prot_op) {
2834 case TARGET_PROT_DIN_INSERT:
2835 case TARGET_PROT_DOUT_STRIP:
2836 transfer_length = data_bytes;
2837 if (cmd->prot_sg_cnt)
2838 data_bytes += dif_bytes;
2840 case TARGET_PROT_DIN_STRIP:
2841 case TARGET_PROT_DOUT_INSERT:
2842 case TARGET_PROT_DIN_PASS:
2843 case TARGET_PROT_DOUT_PASS:
2844 transfer_length = data_bytes + dif_bytes;
2851 if (!qlt_hba_err_chk_enabled(se_cmd))
2852 fw_prot_opts |= 0x10; /* Disable Guard tag checking */
2853 /* HBA error checking enabled */
2854 else if (IS_PI_UNINIT_CAPABLE(ha)) {
2855 if ((se_cmd->prot_type == TARGET_DIF_TYPE1_PROT) ||
2856 (se_cmd->prot_type == TARGET_DIF_TYPE2_PROT))
2857 fw_prot_opts |= PO_DIS_VALD_APP_ESC;
2858 else if (se_cmd->prot_type == TARGET_DIF_TYPE3_PROT)
2859 fw_prot_opts |= PO_DIS_VALD_APP_REF_ESC;
2862 switch (se_cmd->prot_op) {
2863 case TARGET_PROT_DIN_INSERT:
2864 case TARGET_PROT_DOUT_INSERT:
2865 fw_prot_opts |= PO_MODE_DIF_INSERT;
2867 case TARGET_PROT_DIN_STRIP:
2868 case TARGET_PROT_DOUT_STRIP:
2869 fw_prot_opts |= PO_MODE_DIF_REMOVE;
2871 case TARGET_PROT_DIN_PASS:
2872 case TARGET_PROT_DOUT_PASS:
2873 fw_prot_opts |= PO_MODE_DIF_PASS;
2874 /* FUTURE: does tcm require T10CRC<->IPCKSUM conversion? */
2876 default:/* Normal Request */
2877 fw_prot_opts |= PO_MODE_DIF_PASS;
2882 /* Update entry type to indicate Command Type CRC_2 IOCB */
2883 pkt->entry_type = CTIO_CRC2;
2884 pkt->entry_count = 1;
2885 pkt->vp_index = vha->vp_idx;
2887 h = qlt_make_handle(vha);
2888 if (unlikely(h == QLA_TGT_NULL_HANDLE)) {
2890 * CTIO type 7 from the firmware doesn't provide a way to
2891 * know the initiator's LOOP ID, hence we can't find
2892 * the session and, so, the command.
2896 vha->req->outstanding_cmds[h] = (srb_t *)prm->cmd;
2898 pkt->handle = h | CTIO_COMPLETION_HANDLE_MARK;
2899 pkt->nport_handle = cpu_to_le16(prm->cmd->loop_id);
2900 pkt->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
2901 pkt->initiator_id[0] = atio->u.isp24.fcp_hdr.s_id[2];
2902 pkt->initiator_id[1] = atio->u.isp24.fcp_hdr.s_id[1];
2903 pkt->initiator_id[2] = atio->u.isp24.fcp_hdr.s_id[0];
2904 pkt->exchange_addr = atio->u.isp24.exchange_addr;
2906 /* silence compile warning */
2907 t16 = be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id);
2908 pkt->ox_id = cpu_to_le16(t16);
2910 t16 = (atio->u.isp24.attr << 9);
2911 pkt->flags |= cpu_to_le16(t16);
2912 pkt->relative_offset = cpu_to_le32(prm->cmd->offset);
2914 /* Set transfer direction */
2915 if (cmd->dma_data_direction == DMA_TO_DEVICE)
2916 pkt->flags = cpu_to_le16(CTIO7_FLAGS_DATA_IN);
2917 else if (cmd->dma_data_direction == DMA_FROM_DEVICE)
2918 pkt->flags = cpu_to_le16(CTIO7_FLAGS_DATA_OUT);
2920 pkt->dseg_count = prm->tot_dsds;
2921 /* Fibre channel byte count */
2922 pkt->transfer_length = cpu_to_le32(transfer_length);
2924 /* ----- CRC context -------- */
2926 /* Allocate CRC context from global pool */
2927 crc_ctx_pkt = cmd->ctx =
2928 dma_pool_alloc(ha->dl_dma_pool, GFP_ATOMIC, &crc_ctx_dma);
2931 goto crc_queuing_error;
2933 /* Zero out CTX area. */
2934 clr_ptr = (uint8_t *)crc_ctx_pkt;
2935 memset(clr_ptr, 0, sizeof(*crc_ctx_pkt));
2937 crc_ctx_pkt->crc_ctx_dma = crc_ctx_dma;
2938 INIT_LIST_HEAD(&crc_ctx_pkt->dsd_list);
2941 crc_ctx_pkt->handle = pkt->handle;
2943 qla_tgt_set_dif_tags(cmd, crc_ctx_pkt, &fw_prot_opts);
2945 pkt->crc_context_address[0] = cpu_to_le32(LSD(crc_ctx_dma));
2946 pkt->crc_context_address[1] = cpu_to_le32(MSD(crc_ctx_dma));
2947 pkt->crc_context_len = CRC_CONTEXT_LEN_FW;
2950 cur_dsd = (uint32_t *) &crc_ctx_pkt->u.nobundling.data_address;
2953 * Configure Bundling if we need to fetch interlaving
2954 * protection PCI accesses
2956 fw_prot_opts |= PO_ENABLE_DIF_BUNDLING;
2957 crc_ctx_pkt->u.bundling.dif_byte_count = cpu_to_le32(dif_bytes);
2958 crc_ctx_pkt->u.bundling.dseg_count =
2959 cpu_to_le16(prm->tot_dsds - prm->prot_seg_cnt);
2960 cur_dsd = (uint32_t *) &crc_ctx_pkt->u.bundling.data_address;
2963 /* Finish the common fields of CRC pkt */
2964 crc_ctx_pkt->blk_size = cpu_to_le16(cmd->blk_sz);
2965 crc_ctx_pkt->prot_opts = cpu_to_le16(fw_prot_opts);
2966 crc_ctx_pkt->byte_count = cpu_to_le32(data_bytes);
2967 crc_ctx_pkt->guard_seed = cpu_to_le16(0);
2969 memset((uint8_t *)&tc, 0 , sizeof(tc));
2971 tc.blk_sz = cmd->blk_sz;
2972 tc.bufflen = cmd->bufflen;
2974 tc.prot_sg = cmd->prot_sg;
2975 tc.ctx = crc_ctx_pkt;
2976 tc.ctx_dsd_alloced = &cmd->ctx_dsd_alloced;
2978 /* Walks data segments */
2979 pkt->flags |= cpu_to_le16(CTIO7_FLAGS_DSD_PTR);
2981 if (!bundling && prm->prot_seg_cnt) {
2982 if (qla24xx_walk_and_build_sglist_no_difb(ha, NULL, cur_dsd,
2983 prm->tot_dsds, &tc))
2984 goto crc_queuing_error;
2985 } else if (qla24xx_walk_and_build_sglist(ha, NULL, cur_dsd,
2986 (prm->tot_dsds - prm->prot_seg_cnt), &tc))
2987 goto crc_queuing_error;
2989 if (bundling && prm->prot_seg_cnt) {
2990 /* Walks dif segments */
2991 pkt->add_flags |= CTIO_CRC2_AF_DIF_DSD_ENA;
2993 cur_dsd = (uint32_t *) &crc_ctx_pkt->u.bundling.dif_address;
2994 if (qla24xx_walk_and_build_prot_sglist(ha, NULL, cur_dsd,
2995 prm->prot_seg_cnt, &tc))
2996 goto crc_queuing_error;
3001 /* Cleanup will be performed by the caller */
3002 vha->req->outstanding_cmds[h] = NULL;
3004 return QLA_FUNCTION_FAILED;
3008 * Callback to setup response of xmit_type of QLA_TGT_XMIT_DATA and *
3009 * QLA_TGT_XMIT_STATUS for >= 24xx silicon
3011 int qlt_xmit_response(struct qla_tgt_cmd *cmd, int xmit_type,
3012 uint8_t scsi_status)
3014 struct scsi_qla_host *vha = cmd->vha;
3015 struct qla_hw_data *ha = vha->hw;
3016 struct ctio7_to_24xx *pkt;
3017 struct qla_tgt_prm prm;
3018 uint32_t full_req_cnt = 0;
3019 unsigned long flags = 0;
3022 spin_lock_irqsave(&ha->hardware_lock, flags);
3023 if (cmd->sess && cmd->sess->deleted) {
3024 cmd->state = QLA_TGT_STATE_PROCESSED;
3025 if (cmd->sess->logout_completed)
3026 /* no need to terminate. FW already freed exchange. */
3027 qlt_abort_cmd_on_host_reset(cmd->vha, cmd);
3029 qlt_send_term_exchange(vha, cmd, &cmd->atio, 1, 0);
3030 spin_unlock_irqrestore(&ha->hardware_lock, flags);
3033 spin_unlock_irqrestore(&ha->hardware_lock, flags);
3035 memset(&prm, 0, sizeof(prm));
3037 ql_dbg(ql_dbg_tgt, cmd->vha, 0xe018,
3038 "is_send_status=%d, cmd->bufflen=%d, cmd->sg_cnt=%d, cmd->dma_data_direction=%d se_cmd[%p]\n",
3039 (xmit_type & QLA_TGT_XMIT_STATUS) ?
3040 1 : 0, cmd->bufflen, cmd->sg_cnt, cmd->dma_data_direction,
3043 res = qlt_pre_xmit_response(cmd, &prm, xmit_type, scsi_status,
3045 if (unlikely(res != 0)) {
3049 spin_lock_irqsave(&ha->hardware_lock, flags);
3051 if (xmit_type == QLA_TGT_XMIT_STATUS)
3052 vha->tgt_counters.core_qla_snd_status++;
3054 vha->tgt_counters.core_qla_que_buf++;
3056 if (!ha->flags.fw_started || cmd->reset_count != ha->chip_reset) {
3058 * Either the port is not online or this request was from
3059 * previous life, just abort the processing.
3061 cmd->state = QLA_TGT_STATE_PROCESSED;
3062 qlt_abort_cmd_on_host_reset(cmd->vha, cmd);
3063 ql_dbg(ql_dbg_async, vha, 0xe101,
3064 "RESET-RSP online/active/old-count/new-count = %d/%d/%d/%d.\n",
3065 vha->flags.online, qla2x00_reset_active(vha),
3066 cmd->reset_count, ha->chip_reset);
3067 spin_unlock_irqrestore(&ha->hardware_lock, flags);
3071 /* Does F/W have an IOCBs for this request */
3072 res = qlt_check_reserve_free_req(vha, full_req_cnt);
3074 goto out_unmap_unlock;
3076 if (cmd->se_cmd.prot_op && (xmit_type & QLA_TGT_XMIT_DATA))
3077 res = qlt_build_ctio_crc2_pkt(&prm, vha);
3079 res = qlt_24xx_build_ctio_pkt(&prm, vha);
3080 if (unlikely(res != 0)) {
3081 vha->req->cnt += full_req_cnt;
3082 goto out_unmap_unlock;
3085 pkt = (struct ctio7_to_24xx *)prm.pkt;
3087 if (qlt_has_data(cmd) && (xmit_type & QLA_TGT_XMIT_DATA)) {
3088 pkt->u.status0.flags |=
3089 cpu_to_le16(CTIO7_FLAGS_DATA_IN |
3090 CTIO7_FLAGS_STATUS_MODE_0);
3092 if (cmd->se_cmd.prot_op == TARGET_PROT_NORMAL)
3093 qlt_load_data_segments(&prm, vha);
3095 if (prm.add_status_pkt == 0) {
3096 if (xmit_type & QLA_TGT_XMIT_STATUS) {
3097 pkt->u.status0.scsi_status =
3098 cpu_to_le16(prm.rq_result);
3099 pkt->u.status0.residual =
3100 cpu_to_le32(prm.residual);
3101 pkt->u.status0.flags |= cpu_to_le16(
3102 CTIO7_FLAGS_SEND_STATUS);
3103 if (qlt_need_explicit_conf(ha, cmd, 0)) {
3104 pkt->u.status0.flags |=
3106 CTIO7_FLAGS_EXPLICIT_CONFORM |
3107 CTIO7_FLAGS_CONFORM_REQ);
3113 * We have already made sure that there is sufficient
3114 * amount of request entries to not drop HW lock in
3117 struct ctio7_to_24xx *ctio =
3118 (struct ctio7_to_24xx *)qlt_get_req_pkt(vha);
3120 ql_dbg(ql_dbg_io, vha, 0x305e,
3121 "Building additional status packet 0x%p.\n",
3125 * T10Dif: ctio_crc2_to_fw overlay ontop of
3128 memcpy(ctio, pkt, sizeof(*ctio));
3129 /* reset back to CTIO7 */
3130 ctio->entry_count = 1;
3131 ctio->entry_type = CTIO_TYPE7;
3132 ctio->dseg_count = 0;
3133 ctio->u.status1.flags &= ~cpu_to_le16(
3134 CTIO7_FLAGS_DATA_IN);
3136 /* Real finish is ctio_m1's finish */
3137 pkt->handle |= CTIO_INTERMEDIATE_HANDLE_MARK;
3138 pkt->u.status0.flags |= cpu_to_le16(
3139 CTIO7_FLAGS_DONT_RET_CTIO);
3141 /* qlt_24xx_init_ctio_to_isp will correct
3142 * all neccessary fields that's part of CTIO7.
3143 * There should be no residual of CTIO-CRC2 data.
3145 qlt_24xx_init_ctio_to_isp((struct ctio7_to_24xx *)ctio,
3147 pr_debug("Status CTIO7: %p\n", ctio);
3150 qlt_24xx_init_ctio_to_isp(pkt, &prm);
3153 cmd->state = QLA_TGT_STATE_PROCESSED; /* Mid-level is done processing */
3154 cmd->cmd_sent_to_fw = 1;
3156 /* Memory Barrier */
3158 qla2x00_start_iocbs(vha, vha->req);
3159 spin_unlock_irqrestore(&ha->hardware_lock, flags);
3164 qlt_unmap_sg(vha, cmd);
3165 spin_unlock_irqrestore(&ha->hardware_lock, flags);
3169 EXPORT_SYMBOL(qlt_xmit_response);
3171 int qlt_rdy_to_xfer(struct qla_tgt_cmd *cmd)
3173 struct ctio7_to_24xx *pkt;
3174 struct scsi_qla_host *vha = cmd->vha;
3175 struct qla_hw_data *ha = vha->hw;
3176 struct qla_tgt *tgt = cmd->tgt;
3177 struct qla_tgt_prm prm;
3178 unsigned long flags;
3181 memset(&prm, 0, sizeof(prm));
3187 /* Send marker if required */
3188 if (qlt_issue_marker(vha, 0) != QLA_SUCCESS)
3191 /* Calculate number of entries and segments required */
3192 if (qlt_pci_map_calc_cnt(&prm) != 0)
3195 spin_lock_irqsave(&ha->hardware_lock, flags);
3197 if (!ha->flags.fw_started || (cmd->reset_count != ha->chip_reset) ||
3198 (cmd->sess && cmd->sess->deleted)) {
3200 * Either the port is not online or this request was from
3201 * previous life, just abort the processing.
3203 cmd->state = QLA_TGT_STATE_NEED_DATA;
3204 qlt_abort_cmd_on_host_reset(cmd->vha, cmd);
3205 ql_dbg(ql_dbg_async, vha, 0xe102,
3206 "RESET-XFR online/active/old-count/new-count = %d/%d/%d/%d.\n",
3207 vha->flags.online, qla2x00_reset_active(vha),
3208 cmd->reset_count, ha->chip_reset);
3209 spin_unlock_irqrestore(&ha->hardware_lock, flags);
3213 /* Does F/W have an IOCBs for this request */
3214 res = qlt_check_reserve_free_req(vha, prm.req_cnt);
3216 goto out_unlock_free_unmap;
3217 if (cmd->se_cmd.prot_op)
3218 res = qlt_build_ctio_crc2_pkt(&prm, vha);
3220 res = qlt_24xx_build_ctio_pkt(&prm, vha);
3222 if (unlikely(res != 0)) {
3223 vha->req->cnt += prm.req_cnt;
3224 goto out_unlock_free_unmap;
3227 pkt = (struct ctio7_to_24xx *)prm.pkt;
3228 pkt->u.status0.flags |= cpu_to_le16(CTIO7_FLAGS_DATA_OUT |
3229 CTIO7_FLAGS_STATUS_MODE_0);
3231 if (cmd->se_cmd.prot_op == TARGET_PROT_NORMAL)
3232 qlt_load_data_segments(&prm, vha);
3234 cmd->state = QLA_TGT_STATE_NEED_DATA;
3235 cmd->cmd_sent_to_fw = 1;
3237 /* Memory Barrier */
3239 qla2x00_start_iocbs(vha, vha->req);
3240 spin_unlock_irqrestore(&ha->hardware_lock, flags);
3244 out_unlock_free_unmap:
3245 qlt_unmap_sg(vha, cmd);
3246 spin_unlock_irqrestore(&ha->hardware_lock, flags);
3250 EXPORT_SYMBOL(qlt_rdy_to_xfer);
3254 * it is assumed either hardware_lock or qpair lock is held.
3257 qlt_handle_dif_error(struct scsi_qla_host *vha, struct qla_tgt_cmd *cmd,
3258 struct ctio_crc_from_fw *sts)
3260 uint8_t *ap = &sts->actual_dif[0];
3261 uint8_t *ep = &sts->expected_dif[0];
3262 uint64_t lba = cmd->se_cmd.t_task_lba;
3263 uint8_t scsi_status, sense_key, asc, ascq;
3264 unsigned long flags;
3266 cmd->trc_flags |= TRC_DIF_ERR;
3268 cmd->a_guard = be16_to_cpu(*(uint16_t *)(ap + 0));
3269 cmd->a_app_tag = be16_to_cpu(*(uint16_t *)(ap + 2));
3270 cmd->a_ref_tag = be32_to_cpu(*(uint32_t *)(ap + 4));
3272 cmd->e_guard = be16_to_cpu(*(uint16_t *)(ep + 0));
3273 cmd->e_app_tag = be16_to_cpu(*(uint16_t *)(ep + 2));
3274 cmd->e_ref_tag = be32_to_cpu(*(uint32_t *)(ep + 4));
3276 ql_dbg(ql_dbg_tgt_dif, vha, 0xf075,
3277 "%s: aborted %d state %d\n", __func__, cmd->aborted, cmd->state);
3279 scsi_status = sense_key = asc = ascq = 0;
3281 /* check appl tag */
3282 if (cmd->e_app_tag != cmd->a_app_tag) {
3283 ql_dbg(ql_dbg_tgt_dif, vha, 0xe00d,
3284 "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]",
3285 cmd->cdb[0], lba, (lba+cmd->num_blks), cmd->num_blks,
3286 cmd->a_ref_tag, cmd->e_ref_tag, cmd->a_app_tag,
3287 cmd->e_app_tag, cmd->a_guard, cmd->e_guard, cmd,
3288 cmd->atio.u.isp24.fcp_hdr.ox_id);
3290 cmd->dif_err_code = DIF_ERR_APP;
3291 scsi_status = SAM_STAT_CHECK_CONDITION;
3292 sense_key = ABORTED_COMMAND;
3298 if (cmd->e_ref_tag != cmd->a_ref_tag) {
3299 ql_dbg(ql_dbg_tgt_dif, vha, 0xe00e,
3300 "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] ",
3301 cmd->cdb[0], lba, (lba+cmd->num_blks), cmd->num_blks,
3302 cmd->a_ref_tag, cmd->e_ref_tag, cmd->a_app_tag,
3303 cmd->e_app_tag, cmd->a_guard, cmd->e_guard, cmd,
3304 cmd->atio.u.isp24.fcp_hdr.ox_id);
3306 cmd->dif_err_code = DIF_ERR_REF;
3307 scsi_status = SAM_STAT_CHECK_CONDITION;
3308 sense_key = ABORTED_COMMAND;
3315 if (cmd->e_guard != cmd->a_guard) {
3316 ql_dbg(ql_dbg_tgt_dif, vha, 0xe012,
3317 "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]",
3318 cmd->cdb[0], lba, (lba+cmd->num_blks), cmd->num_blks,
3319 cmd->a_ref_tag, cmd->e_ref_tag, cmd->a_app_tag,
3320 cmd->e_app_tag, cmd->a_guard, cmd->e_guard, cmd,
3321 cmd->atio.u.isp24.fcp_hdr.ox_id);
3323 cmd->dif_err_code = DIF_ERR_GRD;
3324 scsi_status = SAM_STAT_CHECK_CONDITION;
3325 sense_key = ABORTED_COMMAND;
3330 switch (cmd->state) {
3331 case QLA_TGT_STATE_NEED_DATA:
3332 /* handle_data will load DIF error code */
3333 cmd->state = QLA_TGT_STATE_DATA_IN;
3334 vha->hw->tgt.tgt_ops->handle_data(cmd);
3337 spin_lock_irqsave(&cmd->cmd_lock, flags);
3339 spin_unlock_irqrestore(&cmd->cmd_lock, flags);
3340 vha->hw->tgt.tgt_ops->free_cmd(cmd);
3343 spin_unlock_irqrestore(&cmd->cmd_lock, flags);
3345 qlt_send_resp_ctio(vha, cmd, scsi_status, sense_key, asc, ascq);
3346 /* assume scsi status gets out on the wire.
3347 * Will not wait for completion.
3349 vha->hw->tgt.tgt_ops->free_cmd(cmd);
3354 /* If hardware_lock held on entry, might drop it, then reaquire */
3355 /* This function sends the appropriate CTIO to ISP 2xxx or 24xx */
3356 static int __qlt_send_term_imm_notif(struct scsi_qla_host *vha,
3357 struct imm_ntfy_from_isp *ntfy)
3359 struct nack_to_isp *nack;
3360 struct qla_hw_data *ha = vha->hw;
3364 ql_dbg(ql_dbg_tgt_tmr, vha, 0xe01c,
3365 "Sending TERM ELS CTIO (ha=%p)\n", ha);
3367 pkt = (request_t *)qla2x00_alloc_iocbs(vha, NULL);
3369 ql_dbg(ql_dbg_tgt, vha, 0xe080,
3370 "qla_target(%d): %s failed: unable to allocate "
3371 "request packet\n", vha->vp_idx, __func__);
3375 pkt->entry_type = NOTIFY_ACK_TYPE;
3376 pkt->entry_count = 1;
3377 pkt->handle = QLA_TGT_SKIP_HANDLE;
3379 nack = (struct nack_to_isp *)pkt;
3380 nack->ox_id = ntfy->ox_id;
3382 nack->u.isp24.nport_handle = ntfy->u.isp24.nport_handle;
3383 if (le16_to_cpu(ntfy->u.isp24.status) == IMM_NTFY_ELS) {
3384 nack->u.isp24.flags = ntfy->u.isp24.flags &
3385 __constant_cpu_to_le32(NOTIFY24XX_FLAGS_PUREX_IOCB);
3389 nack->u.isp24.flags |=
3390 __constant_cpu_to_le16(NOTIFY_ACK_FLAGS_TERMINATE);
3392 nack->u.isp24.srr_rx_id = ntfy->u.isp24.srr_rx_id;
3393 nack->u.isp24.status = ntfy->u.isp24.status;
3394 nack->u.isp24.status_subcode = ntfy->u.isp24.status_subcode;
3395 nack->u.isp24.fw_handle = ntfy->u.isp24.fw_handle;
3396 nack->u.isp24.exchange_address = ntfy->u.isp24.exchange_address;
3397 nack->u.isp24.srr_rel_offs = ntfy->u.isp24.srr_rel_offs;
3398 nack->u.isp24.srr_ui = ntfy->u.isp24.srr_ui;
3399 nack->u.isp24.vp_index = ntfy->u.isp24.vp_index;
3401 qla2x00_start_iocbs(vha, vha->req);
3405 static void qlt_send_term_imm_notif(struct scsi_qla_host *vha,
3406 struct imm_ntfy_from_isp *imm, int ha_locked)
3408 unsigned long flags = 0;
3411 if (qlt_issue_marker(vha, ha_locked) < 0)
3415 rc = __qlt_send_term_imm_notif(vha, imm);
3419 qlt_alloc_qfull_cmd(vha, imm, 0, 0);
3427 spin_lock_irqsave(&vha->hw->hardware_lock, flags);
3428 rc = __qlt_send_term_imm_notif(vha, imm);
3432 qlt_alloc_qfull_cmd(vha, imm, 0, 0);
3437 spin_unlock_irqrestore(&vha->hw->hardware_lock, flags);
3441 * If hardware_lock held on entry, might drop it, then reaquire
3442 * This function sends the appropriate CTIO to ISP 2xxx or 24xx
3444 static int __qlt_send_term_exchange(struct scsi_qla_host *vha,
3445 struct qla_tgt_cmd *cmd,
3446 struct atio_from_isp *atio)
3448 struct ctio7_to_24xx *ctio24;
3449 struct qla_hw_data *ha = vha->hw;
3454 ql_dbg(ql_dbg_tgt, vha, 0xe009, "Sending TERM EXCH CTIO (ha=%p)\n", ha);
3456 pkt = (request_t *)qla2x00_alloc_iocbs_ready(vha, NULL);
3458 ql_dbg(ql_dbg_tgt, vha, 0xe050,
3459 "qla_target(%d): %s failed: unable to allocate "
3460 "request packet\n", vha->vp_idx, __func__);
3465 if (cmd->state < QLA_TGT_STATE_PROCESSED) {
3466 ql_dbg(ql_dbg_tgt, vha, 0xe051,
3467 "qla_target(%d): Terminating cmd %p with "
3468 "incorrect state %d\n", vha->vp_idx, cmd,
3474 vha->tgt_counters.num_term_xchg_sent++;
3475 pkt->entry_count = 1;
3476 pkt->handle = QLA_TGT_SKIP_HANDLE | CTIO_COMPLETION_HANDLE_MARK;
3478 ctio24 = (struct ctio7_to_24xx *)pkt;
3479 ctio24->entry_type = CTIO_TYPE7;
3480 ctio24->nport_handle = CTIO7_NHANDLE_UNRECOGNIZED;
3481 ctio24->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
3482 ctio24->vp_index = vha->vp_idx;
3483 ctio24->initiator_id[0] = atio->u.isp24.fcp_hdr.s_id[2];
3484 ctio24->initiator_id[1] = atio->u.isp24.fcp_hdr.s_id[1];
3485 ctio24->initiator_id[2] = atio->u.isp24.fcp_hdr.s_id[0];
3486 ctio24->exchange_addr = atio->u.isp24.exchange_addr;
3487 temp = (atio->u.isp24.attr << 9) | CTIO7_FLAGS_STATUS_MODE_1 |
3488 CTIO7_FLAGS_TERMINATE;
3489 ctio24->u.status1.flags = cpu_to_le16(temp);
3490 temp = be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id);
3491 ctio24->u.status1.ox_id = cpu_to_le16(temp);
3493 /* Most likely, it isn't needed */
3494 ctio24->u.status1.residual = get_unaligned((uint32_t *)
3495 &atio->u.isp24.fcp_cmnd.add_cdb[
3496 atio->u.isp24.fcp_cmnd.add_cdb_len]);
3497 if (ctio24->u.status1.residual != 0)
3498 ctio24->u.status1.scsi_status |= SS_RESIDUAL_UNDER;
3500 /* Memory Barrier */
3502 qla2x00_start_iocbs(vha, vha->req);
3506 static void qlt_send_term_exchange(struct scsi_qla_host *vha,
3507 struct qla_tgt_cmd *cmd, struct atio_from_isp *atio, int ha_locked,
3510 unsigned long flags = 0;
3513 if (qlt_issue_marker(vha, ha_locked) < 0)
3517 rc = __qlt_send_term_exchange(vha, cmd, atio);
3519 qlt_alloc_qfull_cmd(vha, atio, 0, 0);
3522 spin_lock_irqsave(&vha->hw->hardware_lock, flags);
3523 rc = __qlt_send_term_exchange(vha, cmd, atio);
3525 qlt_alloc_qfull_cmd(vha, atio, 0, 0);
3528 if (cmd && !ul_abort && !cmd->aborted) {
3530 qlt_unmap_sg(vha, cmd);
3531 vha->hw->tgt.tgt_ops->free_cmd(cmd);
3535 spin_unlock_irqrestore(&vha->hw->hardware_lock, flags);
3540 static void qlt_init_term_exchange(struct scsi_qla_host *vha)
3542 struct list_head free_list;
3543 struct qla_tgt_cmd *cmd, *tcmd;
3545 vha->hw->tgt.leak_exchg_thresh_hold =
3546 (vha->hw->cur_fw_xcb_count/100) * LEAK_EXCHG_THRESH_HOLD_PERCENT;
3549 if (!list_empty(&vha->hw->tgt.q_full_list)) {
3550 INIT_LIST_HEAD(&free_list);
3551 list_splice_init(&vha->hw->tgt.q_full_list, &free_list);
3553 list_for_each_entry_safe(cmd, tcmd, &free_list, cmd_list) {
3554 list_del(&cmd->cmd_list);
3555 /* This cmd was never sent to TCM. There is no need
3556 * to schedule free or call free_cmd
3559 vha->hw->tgt.num_qfull_cmds_alloc--;
3562 vha->hw->tgt.num_qfull_cmds_dropped = 0;
3565 static void qlt_chk_exch_leak_thresh_hold(struct scsi_qla_host *vha)
3567 uint32_t total_leaked;
3569 total_leaked = vha->hw->tgt.num_qfull_cmds_dropped;
3571 if (vha->hw->tgt.leak_exchg_thresh_hold &&
3572 (total_leaked > vha->hw->tgt.leak_exchg_thresh_hold)) {
3574 ql_dbg(ql_dbg_tgt, vha, 0xe079,
3575 "Chip reset due to exchange starvation: %d/%d.\n",
3576 total_leaked, vha->hw->cur_fw_xcb_count);
3578 if (IS_P3P_TYPE(vha->hw))
3579 set_bit(FCOE_CTX_RESET_NEEDED, &vha->dpc_flags);
3581 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
3582 qla2xxx_wake_dpc(vha);
3587 int qlt_abort_cmd(struct qla_tgt_cmd *cmd)
3589 struct qla_tgt *tgt = cmd->tgt;
3590 struct scsi_qla_host *vha = tgt->vha;
3591 struct se_cmd *se_cmd = &cmd->se_cmd;
3592 unsigned long flags;
3594 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf014,
3595 "qla_target(%d): terminating exchange for aborted cmd=%p "
3596 "(se_cmd=%p, tag=%llu)", vha->vp_idx, cmd, &cmd->se_cmd,
3599 spin_lock_irqsave(&cmd->cmd_lock, flags);
3601 spin_unlock_irqrestore(&cmd->cmd_lock, flags);
3603 * It's normal to see 2 calls in this path:
3604 * 1) XFER Rdy completion + CMD_T_ABORT
3605 * 2) TCM TMR - drain_state_list
3607 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf016,
3608 "multiple abort. %p transport_state %x, t_state %x, "
3609 "se_cmd_flags %x\n", cmd, cmd->se_cmd.transport_state,
3610 cmd->se_cmd.t_state, cmd->se_cmd.se_cmd_flags);
3614 cmd->trc_flags |= TRC_ABORT;
3615 spin_unlock_irqrestore(&cmd->cmd_lock, flags);
3617 qlt_send_term_exchange(vha, cmd, &cmd->atio, 0, 1);
3620 EXPORT_SYMBOL(qlt_abort_cmd);
3622 void qlt_free_cmd(struct qla_tgt_cmd *cmd)
3624 struct fc_port *sess = cmd->sess;
3626 ql_dbg(ql_dbg_tgt, cmd->vha, 0xe074,
3627 "%s: se_cmd[%p] ox_id %04x\n",
3628 __func__, &cmd->se_cmd,
3629 be16_to_cpu(cmd->atio.u.isp24.fcp_hdr.ox_id));
3631 BUG_ON(cmd->cmd_in_wq);
3634 qlt_unmap_sg(cmd->vha, cmd);
3637 qlt_decr_num_pend_cmds(cmd->vha);
3639 BUG_ON(cmd->sg_mapped);
3640 cmd->jiffies_at_free = get_jiffies_64();
3641 if (unlikely(cmd->free_sg))
3644 if (!sess || !sess->se_sess) {
3648 cmd->jiffies_at_free = get_jiffies_64();
3649 percpu_ida_free(&sess->se_sess->sess_tag_pool, cmd->se_cmd.map_tag);
3651 EXPORT_SYMBOL(qlt_free_cmd);
3654 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
3656 static int qlt_term_ctio_exchange(struct scsi_qla_host *vha, void *ctio,
3657 struct qla_tgt_cmd *cmd, uint32_t status)
3661 if (cmd->se_cmd.prot_op)
3662 ql_dbg(ql_dbg_tgt_dif, vha, 0xe013,
3663 "Term DIF cmd: lba[0x%llx|%lld] len[0x%x] "
3664 "se_cmd=%p tag[%x] op %#x/%s",
3666 cmd->num_blks, &cmd->se_cmd,
3667 cmd->atio.u.isp24.exchange_addr,
3668 cmd->se_cmd.prot_op,
3669 prot_op_str(cmd->se_cmd.prot_op));
3672 struct ctio7_from_24xx *c = (struct ctio7_from_24xx *)ctio;
3674 cpu_to_le16(OF_TERM_EXCH));
3679 qlt_send_term_exchange(vha, cmd, &cmd->atio, 1, 0);
3685 /* ha->hardware_lock supposed to be held on entry */
3686 static struct qla_tgt_cmd *qlt_ctio_to_cmd(struct scsi_qla_host *vha,
3687 uint32_t handle, void *ctio)
3689 struct qla_tgt_cmd *cmd = NULL;
3690 struct req_que *req = vha->req;
3692 /* Clear out internal marks */
3693 handle &= ~QLA_TGT_HANDLE_MASK;
3695 if (handle != QLA_TGT_NULL_HANDLE) {
3696 if (unlikely(handle == QLA_TGT_SKIP_HANDLE))
3699 handle &= QLA_CMD_HANDLE_MASK;
3701 if (unlikely(handle > req->num_outstanding_cmds)) {
3702 ql_dbg(ql_dbg_tgt, vha, 0xe052,
3703 "qla_target(%d): Wrong handle %x received\n",
3704 vha->vp_idx, handle);
3707 cmd = (struct qla_tgt_cmd *)req->outstanding_cmds[handle];
3708 if (unlikely((cmd == NULL) ||
3709 (cmd->cmd_type != TYPE_TGT_CMD))) {
3710 ql_dbg(ql_dbg_async, vha, 0xe053,
3711 "qla_target(%d): Suspicious: unable to find the command with handle %x cmd %p\n",
3712 vha->vp_idx, handle, cmd);
3715 req->outstanding_cmds[handle] = NULL;
3716 } else if (ctio != NULL) {
3717 /* We can't get loop ID from CTIO7 */
3718 ql_dbg(ql_dbg_tgt, vha, 0xe054,
3719 "qla_target(%d): Wrong CTIO received: QLA24xx doesn't "
3720 "support NULL handles\n", vha->vp_idx);
3727 /* hardware_lock should be held by caller. */
3729 qlt_abort_cmd_on_host_reset(struct scsi_qla_host *vha, struct qla_tgt_cmd *cmd)
3731 struct qla_hw_data *ha = vha->hw;
3735 qlt_unmap_sg(vha, cmd);
3737 handle = qlt_make_handle(vha);
3739 /* TODO: fix debug message type and ids. */
3740 if (cmd->state == QLA_TGT_STATE_PROCESSED) {
3741 ql_dbg(ql_dbg_io, vha, 0xff00,
3742 "HOST-ABORT: handle=%d, state=PROCESSED.\n", handle);
3743 } else if (cmd->state == QLA_TGT_STATE_NEED_DATA) {
3744 cmd->write_data_transferred = 0;
3745 cmd->state = QLA_TGT_STATE_DATA_IN;
3747 ql_dbg(ql_dbg_io, vha, 0xff01,
3748 "HOST-ABORT: handle=%d, state=DATA_IN.\n", handle);
3750 ha->tgt.tgt_ops->handle_data(cmd);
3753 ql_dbg(ql_dbg_io, vha, 0xff03,
3754 "HOST-ABORT: handle=%d, state=BAD(%d).\n", handle,
3759 cmd->trc_flags |= TRC_FLUSH;
3760 ha->tgt.tgt_ops->free_cmd(cmd);
3764 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
3766 static void qlt_do_ctio_completion(struct scsi_qla_host *vha, uint32_t handle,
3767 uint32_t status, void *ctio)
3769 struct qla_hw_data *ha = vha->hw;
3770 struct se_cmd *se_cmd;
3771 struct qla_tgt_cmd *cmd;
3773 if (handle & CTIO_INTERMEDIATE_HANDLE_MARK) {
3774 /* That could happen only in case of an error/reset/abort */
3775 if (status != CTIO_SUCCESS) {
3776 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01d,
3777 "Intermediate CTIO received"
3778 " (status %x)\n", status);
3783 cmd = qlt_ctio_to_cmd(vha, handle, ctio);
3787 se_cmd = &cmd->se_cmd;
3788 cmd->cmd_sent_to_fw = 0;
3790 qlt_unmap_sg(vha, cmd);
3792 if (unlikely(status != CTIO_SUCCESS)) {
3793 switch (status & 0xFFFF) {
3794 case CTIO_LIP_RESET:
3795 case CTIO_TARGET_RESET:
3797 /* driver request abort via Terminate exchange */
3799 case CTIO_INVALID_RX_ID:
3801 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf058,
3802 "qla_target(%d): CTIO with "
3803 "status %#x received, state %x, se_cmd %p, "
3804 "(LIP_RESET=e, ABORTED=2, TARGET_RESET=17, "
3805 "TIMEOUT=b, INVALID_RX_ID=8)\n", vha->vp_idx,
3806 status, cmd->state, se_cmd);
3809 case CTIO_PORT_LOGGED_OUT:
3810 case CTIO_PORT_UNAVAILABLE:
3813 (status & 0xFFFF) == CTIO_PORT_LOGGED_OUT;
3815 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf059,
3816 "qla_target(%d): CTIO with %s status %x "
3817 "received (state %x, se_cmd %p)\n", vha->vp_idx,
3818 logged_out ? "PORT LOGGED OUT" : "PORT UNAVAILABLE",
3819 status, cmd->state, se_cmd);
3821 if (logged_out && cmd->sess) {
3823 * Session is already logged out, but we need
3824 * to notify initiator, who's not aware of this
3826 cmd->sess->logout_on_delete = 0;
3827 cmd->sess->send_els_logo = 1;
3828 ql_dbg(ql_dbg_disc, vha, 0x20f8,
3829 "%s %d %8phC post del sess\n",
3830 __func__, __LINE__, cmd->sess->port_name);
3832 qlt_schedule_sess_for_deletion_lock(cmd->sess);
3836 case CTIO_DIF_ERROR: {
3837 struct ctio_crc_from_fw *crc =
3838 (struct ctio_crc_from_fw *)ctio;
3839 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf073,
3840 "qla_target(%d): CTIO with DIF_ERROR status %x "
3841 "received (state %x, ulp_cmd %p) actual_dif[0x%llx] "
3842 "expect_dif[0x%llx]\n",
3843 vha->vp_idx, status, cmd->state, se_cmd,
3844 *((u64 *)&crc->actual_dif[0]),
3845 *((u64 *)&crc->expected_dif[0]));
3847 qlt_handle_dif_error(vha, cmd, ctio);
3851 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05b,
3852 "qla_target(%d): CTIO with error status 0x%x received (state %x, se_cmd %p\n",
3853 vha->vp_idx, status, cmd->state, se_cmd);
3858 /* "cmd->aborted" means
3859 * cmd is already aborted/terminated, we don't
3860 * need to terminate again. The exchange is already
3861 * cleaned up/freed at FW level. Just cleanup at driver
3864 if ((cmd->state != QLA_TGT_STATE_NEED_DATA) &&
3866 cmd->trc_flags |= TRC_CTIO_ERR;
3867 if (qlt_term_ctio_exchange(vha, ctio, cmd, status))
3872 if (cmd->state == QLA_TGT_STATE_PROCESSED) {
3873 cmd->trc_flags |= TRC_CTIO_DONE;
3874 } else if (cmd->state == QLA_TGT_STATE_NEED_DATA) {
3875 cmd->state = QLA_TGT_STATE_DATA_IN;
3877 if (status == CTIO_SUCCESS)
3878 cmd->write_data_transferred = 1;
3880 ha->tgt.tgt_ops->handle_data(cmd);
3882 } else if (cmd->aborted) {
3883 cmd->trc_flags |= TRC_CTIO_ABORTED;
3884 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01e,
3885 "Aborted command %p (tag %lld) finished\n", cmd, se_cmd->tag);
3887 cmd->trc_flags |= TRC_CTIO_STRANGE;
3888 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05c,
3889 "qla_target(%d): A command in state (%d) should "
3890 "not return a CTIO complete\n", vha->vp_idx, cmd->state);
3893 if (unlikely(status != CTIO_SUCCESS) &&
3895 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01f, "Finishing failed CTIO\n");
3899 ha->tgt.tgt_ops->free_cmd(cmd);
3902 static inline int qlt_get_fcp_task_attr(struct scsi_qla_host *vha,
3907 switch (task_codes) {
3908 case ATIO_SIMPLE_QUEUE:
3909 fcp_task_attr = TCM_SIMPLE_TAG;
3911 case ATIO_HEAD_OF_QUEUE:
3912 fcp_task_attr = TCM_HEAD_TAG;
3914 case ATIO_ORDERED_QUEUE:
3915 fcp_task_attr = TCM_ORDERED_TAG;
3917 case ATIO_ACA_QUEUE:
3918 fcp_task_attr = TCM_ACA_TAG;
3921 fcp_task_attr = TCM_SIMPLE_TAG;
3924 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05d,
3925 "qla_target: unknown task code %x, use ORDERED instead\n",
3927 fcp_task_attr = TCM_ORDERED_TAG;
3931 return fcp_task_attr;
3934 static struct fc_port *qlt_make_local_sess(struct scsi_qla_host *,
3937 * Process context for I/O path into tcm_qla2xxx code
3939 static void __qlt_do_work(struct qla_tgt_cmd *cmd)
3941 scsi_qla_host_t *vha = cmd->vha;
3942 struct qla_hw_data *ha = vha->hw;
3943 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
3944 struct fc_port *sess = cmd->sess;
3945 struct atio_from_isp *atio = &cmd->atio;
3947 unsigned long flags;
3948 uint32_t data_length;
3949 int ret, fcp_task_attr, data_dir, bidi = 0;
3952 cmd->trc_flags |= TRC_DO_WORK;
3957 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf082,
3958 "cmd with tag %u is aborted\n",
3959 cmd->atio.u.isp24.exchange_addr);
3963 spin_lock_init(&cmd->cmd_lock);
3964 cdb = &atio->u.isp24.fcp_cmnd.cdb[0];
3965 cmd->se_cmd.tag = atio->u.isp24.exchange_addr;
3966 cmd->unpacked_lun = scsilun_to_int(
3967 (struct scsi_lun *)&atio->u.isp24.fcp_cmnd.lun);
3969 if (atio->u.isp24.fcp_cmnd.rddata &&
3970 atio->u.isp24.fcp_cmnd.wrdata) {
3972 data_dir = DMA_TO_DEVICE;
3973 } else if (atio->u.isp24.fcp_cmnd.rddata)
3974 data_dir = DMA_FROM_DEVICE;
3975 else if (atio->u.isp24.fcp_cmnd.wrdata)
3976 data_dir = DMA_TO_DEVICE;
3978 data_dir = DMA_NONE;
3980 fcp_task_attr = qlt_get_fcp_task_attr(vha,
3981 atio->u.isp24.fcp_cmnd.task_attr);
3982 data_length = be32_to_cpu(get_unaligned((uint32_t *)
3983 &atio->u.isp24.fcp_cmnd.add_cdb[
3984 atio->u.isp24.fcp_cmnd.add_cdb_len]));
3986 ret = ha->tgt.tgt_ops->handle_cmd(vha, cmd, cdb, data_length,
3987 fcp_task_attr, data_dir, bidi);
3991 * Drop extra session reference from qla_tgt_handle_cmd_for_atio*(
3993 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
3994 ha->tgt.tgt_ops->put_sess(sess);
3995 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
3999 ql_dbg(ql_dbg_io, vha, 0x3060, "Terminating work cmd %p", cmd);
4001 * cmd has not sent to target yet, so pass NULL as the second
4002 * argument to qlt_send_term_exchange() and free the memory here.
4004 cmd->trc_flags |= TRC_DO_WORK_ERR;
4005 spin_lock_irqsave(&ha->hardware_lock, flags);
4006 qlt_send_term_exchange(vha, NULL, &cmd->atio, 1, 0);
4008 qlt_decr_num_pend_cmds(vha);
4009 percpu_ida_free(&sess->se_sess->sess_tag_pool, cmd->se_cmd.map_tag);
4010 spin_unlock_irqrestore(&ha->hardware_lock, flags);
4012 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
4013 ha->tgt.tgt_ops->put_sess(sess);
4014 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
4017 static void qlt_do_work(struct work_struct *work)
4019 struct qla_tgt_cmd *cmd = container_of(work, struct qla_tgt_cmd, work);
4020 scsi_qla_host_t *vha = cmd->vha;
4021 unsigned long flags;
4023 spin_lock_irqsave(&vha->cmd_list_lock, flags);
4024 list_del(&cmd->cmd_list);
4025 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
4030 static struct qla_tgt_cmd *qlt_get_tag(scsi_qla_host_t *vha,
4031 struct fc_port *sess,
4032 struct atio_from_isp *atio)
4034 struct se_session *se_sess = sess->se_sess;
4035 struct qla_tgt_cmd *cmd;
4038 tag = percpu_ida_alloc(&se_sess->sess_tag_pool, TASK_RUNNING);
4042 cmd = &((struct qla_tgt_cmd *)se_sess->sess_cmd_map)[tag];
4043 memset(cmd, 0, sizeof(struct qla_tgt_cmd));
4044 cmd->cmd_type = TYPE_TGT_CMD;
4045 memcpy(&cmd->atio, atio, sizeof(*atio));
4046 cmd->state = QLA_TGT_STATE_NEW;
4047 cmd->tgt = vha->vha_tgt.qla_tgt;
4048 qlt_incr_num_pend_cmds(vha);
4050 cmd->se_cmd.map_tag = tag;
4052 cmd->loop_id = sess->loop_id;
4053 cmd->conf_compl_supported = sess->conf_compl_supported;
4056 cmd->jiffies_at_alloc = get_jiffies_64();
4058 cmd->reset_count = vha->hw->chip_reset;
4063 static void qlt_send_busy(struct scsi_qla_host *, struct atio_from_isp *,
4066 static void qlt_create_sess_from_atio(struct work_struct *work)
4068 struct qla_tgt_sess_op *op = container_of(work,
4069 struct qla_tgt_sess_op, work);
4070 scsi_qla_host_t *vha = op->vha;
4071 struct qla_hw_data *ha = vha->hw;
4072 struct fc_port *sess;
4073 struct qla_tgt_cmd *cmd;
4074 unsigned long flags;
4075 uint8_t *s_id = op->atio.u.isp24.fcp_hdr.s_id;
4077 spin_lock_irqsave(&vha->cmd_list_lock, flags);
4078 list_del(&op->cmd_list);
4079 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
4082 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf083,
4083 "sess_op with tag %u is aborted\n",
4084 op->atio.u.isp24.exchange_addr);
4088 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf022,
4089 "qla_target(%d): Unable to find wwn login"
4090 " (s_id %x:%x:%x), trying to create it manually\n",
4091 vha->vp_idx, s_id[0], s_id[1], s_id[2]);
4093 if (op->atio.u.raw.entry_count > 1) {
4094 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf023,
4095 "Dropping multy entry atio %p\n", &op->atio);
4099 sess = qlt_make_local_sess(vha, s_id);
4100 /* sess has an extra creation ref. */
4105 * Now obtain a pre-allocated session tag using the original op->atio
4106 * packet header, and dispatch into __qlt_do_work() using the existing
4109 cmd = qlt_get_tag(vha, sess, &op->atio);
4111 spin_lock_irqsave(&ha->hardware_lock, flags);
4112 qlt_send_busy(vha, &op->atio, SAM_STAT_BUSY);
4113 ha->tgt.tgt_ops->put_sess(sess);
4114 spin_unlock_irqrestore(&ha->hardware_lock, flags);
4120 * __qlt_do_work() will call qlt_put_sess() to release
4121 * the extra reference taken above by qlt_make_local_sess()
4127 spin_lock_irqsave(&ha->hardware_lock, flags);
4128 qlt_send_term_exchange(vha, NULL, &op->atio, 1, 0);
4129 spin_unlock_irqrestore(&ha->hardware_lock, flags);
4133 /* ha->hardware_lock supposed to be held on entry */
4134 static int qlt_handle_cmd_for_atio(struct scsi_qla_host *vha,
4135 struct atio_from_isp *atio)
4137 struct qla_hw_data *ha = vha->hw;
4138 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
4139 struct fc_port *sess;
4140 struct qla_tgt_cmd *cmd;
4141 unsigned long flags;
4143 if (unlikely(tgt->tgt_stop)) {
4144 ql_dbg(ql_dbg_io, vha, 0x3061,
4145 "New command while device %p is shutting down\n", tgt);
4149 sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha, atio->u.isp24.fcp_hdr.s_id);
4150 if (unlikely(!sess)) {
4151 struct qla_tgt_sess_op *op = kzalloc(sizeof(struct qla_tgt_sess_op),
4156 memcpy(&op->atio, atio, sizeof(*atio));
4159 spin_lock_irqsave(&vha->cmd_list_lock, flags);
4160 list_add_tail(&op->cmd_list, &vha->qla_sess_op_cmd_list);
4161 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
4163 INIT_WORK(&op->work, qlt_create_sess_from_atio);
4164 queue_work(qla_tgt_wq, &op->work);
4168 /* Another WWN used to have our s_id. Our PLOGI scheduled its
4169 * session deletion, but it's still in sess_del_work wq */
4170 if (sess->deleted) {
4171 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf002,
4172 "New command while old session %p is being deleted\n",
4178 * Do kref_get() before returning + dropping qla_hw_data->hardware_lock.
4180 if (!kref_get_unless_zero(&sess->sess_kref)) {
4181 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf004,
4182 "%s: kref_get fail, %8phC oxid %x \n",
4183 __func__, sess->port_name,
4184 be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id));
4188 cmd = qlt_get_tag(vha, sess, atio);
4190 ql_dbg(ql_dbg_io, vha, 0x3062,
4191 "qla_target(%d): Allocation of cmd failed\n", vha->vp_idx);
4192 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
4193 ha->tgt.tgt_ops->put_sess(sess);
4194 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
4199 cmd->trc_flags |= TRC_NEW_CMD;
4200 cmd->se_cmd.cpuid = ha->msix_count ?
4201 ha->tgt.rspq_vector_cpuid : WORK_CPU_UNBOUND;
4203 spin_lock_irqsave(&vha->cmd_list_lock, flags);
4204 list_add_tail(&cmd->cmd_list, &vha->qla_cmd_list);
4205 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
4207 INIT_WORK(&cmd->work, qlt_do_work);
4208 if (ha->msix_count) {
4209 if (cmd->atio.u.isp24.fcp_cmnd.rddata)
4210 queue_work_on(smp_processor_id(), qla_tgt_wq,
4213 queue_work_on(cmd->se_cmd.cpuid, qla_tgt_wq,
4216 queue_work(qla_tgt_wq, &cmd->work);
4222 /* ha->hardware_lock supposed to be held on entry */
4223 static int qlt_issue_task_mgmt(struct fc_port *sess, u64 lun,
4224 int fn, void *iocb, int flags)
4226 struct scsi_qla_host *vha = sess->vha;
4227 struct qla_hw_data *ha = vha->hw;
4228 struct qla_tgt_mgmt_cmd *mcmd;
4229 struct atio_from_isp *a = (struct atio_from_isp *)iocb;
4232 mcmd = mempool_alloc(qla_tgt_mgmt_cmd_mempool, GFP_ATOMIC);
4234 ql_dbg(ql_dbg_tgt_tmr, vha, 0x10009,
4235 "qla_target(%d): Allocation of management "
4236 "command failed, some commands and their data could "
4237 "leak\n", vha->vp_idx);
4240 memset(mcmd, 0, sizeof(*mcmd));
4244 memcpy(&mcmd->orig_iocb.imm_ntfy, iocb,
4245 sizeof(mcmd->orig_iocb.imm_ntfy));
4247 mcmd->tmr_func = fn;
4248 mcmd->flags = flags;
4249 mcmd->reset_count = vha->hw->chip_reset;
4252 case QLA_TGT_LUN_RESET:
4253 abort_cmds_for_lun(vha, lun, a->u.isp24.fcp_hdr.s_id);
4257 res = ha->tgt.tgt_ops->handle_tmr(mcmd, lun, mcmd->tmr_func, 0);
4259 ql_dbg(ql_dbg_tgt_tmr, vha, 0x1000b,
4260 "qla_target(%d): tgt.tgt_ops->handle_tmr() failed: %d\n",
4261 sess->vha->vp_idx, res);
4262 mempool_free(mcmd, qla_tgt_mgmt_cmd_mempool);
4269 /* ha->hardware_lock supposed to be held on entry */
4270 static int qlt_handle_task_mgmt(struct scsi_qla_host *vha, void *iocb)
4272 struct atio_from_isp *a = (struct atio_from_isp *)iocb;
4273 struct qla_hw_data *ha = vha->hw;
4274 struct qla_tgt *tgt;
4275 struct fc_port *sess;
4278 unsigned long flags;
4280 tgt = vha->vha_tgt.qla_tgt;
4282 fn = a->u.isp24.fcp_cmnd.task_mgmt_flags;
4284 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
4285 sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha,
4286 a->u.isp24.fcp_hdr.s_id);
4287 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
4290 scsilun_to_int((struct scsi_lun *)&a->u.isp24.fcp_cmnd.lun);
4293 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf024,
4294 "qla_target(%d): task mgmt fn 0x%x for "
4295 "non-existant session\n", vha->vp_idx, fn);
4296 return qlt_sched_sess_work(tgt, QLA_TGT_SESS_WORK_TM, iocb,
4297 sizeof(struct atio_from_isp));
4303 return qlt_issue_task_mgmt(sess, unpacked_lun, fn, iocb, 0);
4306 /* ha->hardware_lock supposed to be held on entry */
4307 static int __qlt_abort_task(struct scsi_qla_host *vha,
4308 struct imm_ntfy_from_isp *iocb, struct fc_port *sess)
4310 struct atio_from_isp *a = (struct atio_from_isp *)iocb;
4311 struct qla_hw_data *ha = vha->hw;
4312 struct qla_tgt_mgmt_cmd *mcmd;
4316 mcmd = mempool_alloc(qla_tgt_mgmt_cmd_mempool, GFP_ATOMIC);
4318 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05f,
4319 "qla_target(%d): %s: Allocation of ABORT cmd failed\n",
4320 vha->vp_idx, __func__);
4323 memset(mcmd, 0, sizeof(*mcmd));
4326 memcpy(&mcmd->orig_iocb.imm_ntfy, iocb,
4327 sizeof(mcmd->orig_iocb.imm_ntfy));
4330 scsilun_to_int((struct scsi_lun *)&a->u.isp24.fcp_cmnd.lun);
4331 mcmd->reset_count = vha->hw->chip_reset;
4332 mcmd->tmr_func = QLA_TGT_2G_ABORT_TASK;
4334 rc = ha->tgt.tgt_ops->handle_tmr(mcmd, unpacked_lun, mcmd->tmr_func,
4335 le16_to_cpu(iocb->u.isp2x.seq_id));
4337 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf060,
4338 "qla_target(%d): tgt_ops->handle_tmr() failed: %d\n",
4340 mempool_free(mcmd, qla_tgt_mgmt_cmd_mempool);
4347 /* ha->hardware_lock supposed to be held on entry */
4348 static int qlt_abort_task(struct scsi_qla_host *vha,
4349 struct imm_ntfy_from_isp *iocb)
4351 struct qla_hw_data *ha = vha->hw;
4352 struct fc_port *sess;
4354 unsigned long flags;
4356 loop_id = GET_TARGET_ID(ha, (struct atio_from_isp *)iocb);
4358 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
4359 sess = ha->tgt.tgt_ops->find_sess_by_loop_id(vha, loop_id);
4360 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
4363 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf025,
4364 "qla_target(%d): task abort for unexisting "
4365 "session\n", vha->vp_idx);
4366 return qlt_sched_sess_work(vha->vha_tgt.qla_tgt,
4367 QLA_TGT_SESS_WORK_ABORT, iocb, sizeof(*iocb));
4370 return __qlt_abort_task(vha, iocb, sess);
4373 void qlt_logo_completion_handler(fc_port_t *fcport, int rc)
4375 if (rc != MBS_COMMAND_COMPLETE) {
4376 ql_dbg(ql_dbg_tgt_mgt, fcport->vha, 0xf093,
4377 "%s: se_sess %p / sess %p from"
4378 " port %8phC loop_id %#04x s_id %02x:%02x:%02x"
4379 " LOGO failed: %#x\n",
4383 fcport->port_name, fcport->loop_id,
4384 fcport->d_id.b.domain, fcport->d_id.b.area,
4385 fcport->d_id.b.al_pa, rc);
4388 fcport->logout_completed = 1;
4392 * ha->hardware_lock supposed to be held on entry (to protect tgt->sess_list)
4394 * Schedules sessions with matching port_id/loop_id but different wwn for
4395 * deletion. Returns existing session with matching wwn if present.
4399 qlt_find_sess_invalidate_other(scsi_qla_host_t *vha, uint64_t wwn,
4400 port_id_t port_id, uint16_t loop_id, struct fc_port **conflict_sess)
4402 struct fc_port *sess = NULL, *other_sess;
4405 *conflict_sess = NULL;
4407 list_for_each_entry(other_sess, &vha->vp_fcports, list) {
4409 other_wwn = wwn_to_u64(other_sess->port_name);
4411 if (wwn == other_wwn) {
4417 /* find other sess with nport_id collision */
4418 if (port_id.b24 == other_sess->d_id.b24) {
4419 if (loop_id != other_sess->loop_id) {
4420 ql_dbg(ql_dbg_tgt_tmr, vha, 0x1000c,
4421 "Invalidating sess %p loop_id %d wwn %llx.\n",
4422 other_sess, other_sess->loop_id, other_wwn);
4425 * logout_on_delete is set by default, but another
4426 * session that has the same s_id/loop_id combo
4427 * might have cleared it when requested this session
4428 * deletion, so don't touch it
4430 qlt_schedule_sess_for_deletion(other_sess, true);
4433 * Another wwn used to have our s_id/loop_id
4434 * kill the session, but don't free the loop_id
4436 ql_dbg(ql_dbg_tgt_tmr, vha, 0xf01b,
4437 "Invalidating sess %p loop_id %d wwn %llx.\n",
4438 other_sess, other_sess->loop_id, other_wwn);
4441 other_sess->keep_nport_handle = 1;
4442 *conflict_sess = other_sess;
4443 qlt_schedule_sess_for_deletion(other_sess,
4449 /* find other sess with nport handle collision */
4450 if ((loop_id == other_sess->loop_id) &&
4451 (loop_id != FC_NO_LOOP_ID)) {
4452 ql_dbg(ql_dbg_tgt_tmr, vha, 0x1000d,
4453 "Invalidating sess %p loop_id %d wwn %llx.\n",
4454 other_sess, other_sess->loop_id, other_wwn);
4456 /* Same loop_id but different s_id
4457 * Ok to kill and logout */
4458 qlt_schedule_sess_for_deletion(other_sess, true);
4465 /* Abort any commands for this s_id waiting on qla_tgt_wq workqueue */
4466 static int abort_cmds_for_s_id(struct scsi_qla_host *vha, port_id_t *s_id)
4468 struct qla_tgt_sess_op *op;
4469 struct qla_tgt_cmd *cmd;
4472 unsigned long flags;
4474 key = (((u32)s_id->b.domain << 16) |
4475 ((u32)s_id->b.area << 8) |
4476 ((u32)s_id->b.al_pa));
4478 spin_lock_irqsave(&vha->cmd_list_lock, flags);
4479 list_for_each_entry(op, &vha->qla_sess_op_cmd_list, cmd_list) {
4480 uint32_t op_key = sid_to_key(op->atio.u.isp24.fcp_hdr.s_id);
4482 if (op_key == key) {
4488 list_for_each_entry(op, &vha->unknown_atio_list, cmd_list) {
4489 uint32_t op_key = sid_to_key(op->atio.u.isp24.fcp_hdr.s_id);
4490 if (op_key == key) {
4496 list_for_each_entry(cmd, &vha->qla_cmd_list, cmd_list) {
4497 uint32_t cmd_key = sid_to_key(cmd->atio.u.isp24.fcp_hdr.s_id);
4498 if (cmd_key == key) {
4503 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
4509 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
4511 static int qlt_24xx_handle_els(struct scsi_qla_host *vha,
4512 struct imm_ntfy_from_isp *iocb)
4514 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
4515 struct qla_hw_data *ha = vha->hw;
4516 struct fc_port *sess = NULL, *conflict_sess = NULL;
4522 struct qlt_plogi_ack_t *pla;
4523 unsigned long flags;
4525 wwn = wwn_to_u64(iocb->u.isp24.port_name);
4527 port_id.b.domain = iocb->u.isp24.port_id[2];
4528 port_id.b.area = iocb->u.isp24.port_id[1];
4529 port_id.b.al_pa = iocb->u.isp24.port_id[0];
4530 port_id.b.rsvd_1 = 0;
4532 loop_id = le16_to_cpu(iocb->u.isp24.nport_handle);
4534 ql_dbg(ql_dbg_disc, vha, 0xf026,
4535 "qla_target(%d): Port ID: %02x:%02x:%02x ELS opcode: 0x%02x lid %d %8phC\n",
4536 vha->vp_idx, iocb->u.isp24.port_id[2],
4537 iocb->u.isp24.port_id[1], iocb->u.isp24.port_id[0],
4538 iocb->u.isp24.status_subcode, loop_id,
4539 iocb->u.isp24.port_name);
4541 /* res = 1 means ack at the end of thread
4542 * res = 0 means ack async/later.
4544 switch (iocb->u.isp24.status_subcode) {
4547 /* Mark all stale commands in qla_tgt_wq for deletion */
4548 abort_cmds_for_s_id(vha, &port_id);
4551 spin_lock_irqsave(&tgt->ha->tgt.sess_lock, flags);
4552 sess = qlt_find_sess_invalidate_other(vha, wwn,
4553 port_id, loop_id, &conflict_sess);
4554 spin_unlock_irqrestore(&tgt->ha->tgt.sess_lock, flags);
4557 if (IS_SW_RESV_ADDR(port_id)) {
4562 pla = qlt_plogi_ack_find_add(vha, &port_id, iocb);
4564 qlt_send_term_imm_notif(vha, iocb, 1);
4570 if (conflict_sess) {
4571 conflict_sess->login_gen++;
4572 qlt_plogi_ack_link(vha, pla, conflict_sess,
4573 QLT_PLOGI_LINK_CONFLICT);
4578 qla24xx_post_newsess_work(vha, &port_id,
4579 iocb->u.isp24.port_name, pla);
4584 qlt_plogi_ack_link(vha, pla, sess, QLT_PLOGI_LINK_SAME_WWN);
4585 sess->fw_login_state = DSC_LS_PLOGI_PEND;
4586 sess->d_id = port_id;
4589 switch (sess->disc_state) {
4591 qlt_plogi_ack_unref(vha, pla);
4596 * Under normal circumstances we want to release nport handle
4597 * during LOGO process to avoid nport handle leaks inside FW.
4598 * The exception is when LOGO is done while another PLOGI with
4599 * the same nport handle is waiting as might be the case here.
4600 * Note: there is always a possibily of a race where session
4601 * deletion has already started for other reasons (e.g. ACL
4602 * removal) and now PLOGI arrives:
4603 * 1. if PLOGI arrived in FW after nport handle has been freed,
4604 * FW must have assigned this PLOGI a new/same handle and we
4605 * can proceed ACK'ing it as usual when session deletion
4607 * 2. if PLOGI arrived in FW before LOGO with LCF_FREE_NPORT
4608 * bit reached it, the handle has now been released. We'll
4609 * get an error when we ACK this PLOGI. Nothing will be sent
4610 * back to initiator. Initiator should eventually retry
4611 * PLOGI and situation will correct itself.
4613 sess->keep_nport_handle = ((sess->loop_id == loop_id) &&
4614 (sess->d_id.b24 == port_id.b24));
4616 ql_dbg(ql_dbg_disc, vha, 0x20f9,
4617 "%s %d %8phC post del sess\n",
4618 __func__, __LINE__, sess->port_name);
4621 qlt_schedule_sess_for_deletion_lock(sess);
4628 wd3_lo = le16_to_cpu(iocb->u.isp24.u.prli.wd3_lo);
4631 spin_lock_irqsave(&tgt->ha->tgt.sess_lock, flags);
4632 sess = qlt_find_sess_invalidate_other(vha, wwn, port_id,
4633 loop_id, &conflict_sess);
4634 spin_unlock_irqrestore(&tgt->ha->tgt.sess_lock, flags);
4637 if (conflict_sess) {
4638 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf09b,
4639 "PRLI with conflicting sess %p port %8phC\n",
4640 conflict_sess, conflict_sess->port_name);
4641 qlt_send_term_imm_notif(vha, iocb, 1);
4647 if (sess->fw_login_state != DSC_LS_PLOGI_PEND &&
4648 sess->fw_login_state != DSC_LS_PLOGI_COMP) {
4650 * Impatient initiator sent PRLI before last
4651 * PLOGI could finish. Will force him to re-try,
4652 * while last one finishes.
4654 ql_log(ql_log_warn, sess->vha, 0xf095,
4655 "sess %p PRLI received, before plogi ack.\n",
4657 qlt_send_term_imm_notif(vha, iocb, 1);
4663 * This shouldn't happen under normal circumstances,
4664 * since we have deleted the old session during PLOGI
4666 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf096,
4667 "PRLI (loop_id %#04x) for existing sess %p (loop_id %#04x)\n",
4668 sess->loop_id, sess, iocb->u.isp24.nport_handle);
4671 sess->loop_id = loop_id;
4672 sess->d_id = port_id;
4673 sess->fw_login_state = DSC_LS_PRLI_PEND;
4676 sess->conf_compl_supported = 1;
4678 if ((wd3_lo & BIT_4) == 0)
4679 sess->port_type = FCT_INITIATOR;
4681 sess->port_type = FCT_TARGET;
4683 res = 1; /* send notify ack */
4685 /* Make session global (not used in fabric mode) */
4686 if (ha->current_topology != ISP_CFG_F) {
4688 ql_dbg(ql_dbg_disc, vha, 0x20fa,
4689 "%s %d %8phC post nack\n",
4690 __func__, __LINE__, sess->port_name);
4691 qla24xx_post_nack_work(vha, sess, iocb,
4695 set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
4696 set_bit(LOCAL_LOOP_UPDATE, &vha->dpc_flags);
4697 qla2xxx_wake_dpc(vha);
4701 ql_dbg(ql_dbg_disc, vha, 0x20fb,
4702 "%s %d %8phC post nack\n",
4703 __func__, __LINE__, sess->port_name);
4704 qla24xx_post_nack_work(vha, sess, iocb,
4712 if (le16_to_cpu(iocb->u.isp24.flags) &
4713 NOTIFY24XX_FLAGS_GLOBAL_TPRLO) {
4715 qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS);
4722 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
4723 sess = qla2x00_find_fcport_by_loopid(vha, loop_id);
4724 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
4728 sess->fw_login_state = DSC_LS_LOGO_PEND;
4729 sess->logo_ack_needed = 1;
4730 memcpy(sess->iocb, iocb, IOCB_SIZE);
4733 res = qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS_SESS);
4735 ql_dbg(ql_dbg_disc, vha, 0x20fc,
4736 "%s: logo %llx res %d sess %p ",
4737 __func__, wwn, res, sess);
4740 * cmd went upper layer, look for qlt_xmit_tm_rsp()
4741 * for LOGO_ACK & sess delete
4746 /* cmd did not go to upper layer. */
4748 qlt_schedule_sess_for_deletion_lock(sess);
4751 /* else logo will be ack */
4757 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
4758 if (tgt->link_reinit_iocb_pending) {
4759 qlt_send_notify_ack(vha, &tgt->link_reinit_iocb,
4761 tgt->link_reinit_iocb_pending = 0;
4764 sess = qla2x00_find_fcport_by_wwpn(vha,
4765 iocb->u.isp24.port_name, 1);
4767 ql_dbg(ql_dbg_disc, vha, 0x20fd,
4768 "sess %p lid %d|%d DS %d LS %d\n",
4769 sess, sess->loop_id, loop_id,
4770 sess->disc_state, sess->fw_login_state);
4773 res = 1; /* send notify ack */
4777 case ELS_FLOGI: /* should never happen */
4779 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf061,
4780 "qla_target(%d): Unsupported ELS command %x "
4781 "received\n", vha->vp_idx, iocb->u.isp24.status_subcode);
4782 res = qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS_SESS);
4790 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
4792 static void qlt_handle_imm_notify(struct scsi_qla_host *vha,
4793 struct imm_ntfy_from_isp *iocb)
4795 struct qla_hw_data *ha = vha->hw;
4796 uint32_t add_flags = 0;
4797 int send_notify_ack = 1;
4800 status = le16_to_cpu(iocb->u.isp2x.status);
4802 case IMM_NTFY_LIP_RESET:
4804 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf032,
4805 "qla_target(%d): LIP reset (loop %#x), subcode %x\n",
4806 vha->vp_idx, le16_to_cpu(iocb->u.isp24.nport_handle),
4807 iocb->u.isp24.status_subcode);
4809 if (qlt_reset(vha, iocb, QLA_TGT_ABORT_ALL) == 0)
4810 send_notify_ack = 0;
4814 case IMM_NTFY_LIP_LINK_REINIT:
4816 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
4817 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf033,
4818 "qla_target(%d): LINK REINIT (loop %#x, "
4819 "subcode %x)\n", vha->vp_idx,
4820 le16_to_cpu(iocb->u.isp24.nport_handle),
4821 iocb->u.isp24.status_subcode);
4822 if (tgt->link_reinit_iocb_pending) {
4823 qlt_send_notify_ack(vha, &tgt->link_reinit_iocb,
4826 memcpy(&tgt->link_reinit_iocb, iocb, sizeof(*iocb));
4827 tgt->link_reinit_iocb_pending = 1;
4829 * QLogic requires to wait after LINK REINIT for possible
4830 * PDISC or ADISC ELS commands
4832 send_notify_ack = 0;
4836 case IMM_NTFY_PORT_LOGOUT:
4837 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf034,
4838 "qla_target(%d): Port logout (loop "
4839 "%#x, subcode %x)\n", vha->vp_idx,
4840 le16_to_cpu(iocb->u.isp24.nport_handle),
4841 iocb->u.isp24.status_subcode);
4843 if (qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS_SESS) == 0)
4844 send_notify_ack = 0;
4845 /* The sessions will be cleared in the callback, if needed */
4848 case IMM_NTFY_GLBL_TPRLO:
4849 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf035,
4850 "qla_target(%d): Global TPRLO (%x)\n", vha->vp_idx, status);
4851 if (qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS) == 0)
4852 send_notify_ack = 0;
4853 /* The sessions will be cleared in the callback, if needed */
4856 case IMM_NTFY_PORT_CONFIG:
4857 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf036,
4858 "qla_target(%d): Port config changed (%x)\n", vha->vp_idx,
4860 if (qlt_reset(vha, iocb, QLA_TGT_ABORT_ALL) == 0)
4861 send_notify_ack = 0;
4862 /* The sessions will be cleared in the callback, if needed */
4865 case IMM_NTFY_GLBL_LOGO:
4866 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06a,
4867 "qla_target(%d): Link failure detected\n",
4869 /* I_T nexus loss */
4870 if (qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS) == 0)
4871 send_notify_ack = 0;
4874 case IMM_NTFY_IOCB_OVERFLOW:
4875 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06b,
4876 "qla_target(%d): Cannot provide requested "
4877 "capability (IOCB overflowed the immediate notify "
4878 "resource count)\n", vha->vp_idx);
4881 case IMM_NTFY_ABORT_TASK:
4882 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf037,
4883 "qla_target(%d): Abort Task (S %08x I %#x -> "
4884 "L %#x)\n", vha->vp_idx,
4885 le16_to_cpu(iocb->u.isp2x.seq_id),
4886 GET_TARGET_ID(ha, (struct atio_from_isp *)iocb),
4887 le16_to_cpu(iocb->u.isp2x.lun));
4888 if (qlt_abort_task(vha, iocb) == 0)
4889 send_notify_ack = 0;
4892 case IMM_NTFY_RESOURCE:
4893 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06c,
4894 "qla_target(%d): Out of resources, host %ld\n",
4895 vha->vp_idx, vha->host_no);
4898 case IMM_NTFY_MSG_RX:
4899 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf038,
4900 "qla_target(%d): Immediate notify task %x\n",
4901 vha->vp_idx, iocb->u.isp2x.task_flags);
4902 if (qlt_handle_task_mgmt(vha, iocb) == 0)
4903 send_notify_ack = 0;
4907 if (qlt_24xx_handle_els(vha, iocb) == 0)
4908 send_notify_ack = 0;
4911 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06d,
4912 "qla_target(%d): Received unknown immediate "
4913 "notify status %x\n", vha->vp_idx, status);
4917 if (send_notify_ack)
4918 qlt_send_notify_ack(vha, iocb, add_flags, 0, 0, 0, 0, 0);
4922 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
4923 * This function sends busy to ISP 2xxx or 24xx.
4925 static int __qlt_send_busy(struct scsi_qla_host *vha,
4926 struct atio_from_isp *atio, uint16_t status)
4928 struct ctio7_to_24xx *ctio24;
4929 struct qla_hw_data *ha = vha->hw;
4931 struct fc_port *sess = NULL;
4932 unsigned long flags;
4935 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
4936 sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha,
4937 atio->u.isp24.fcp_hdr.s_id);
4938 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
4940 qlt_send_term_exchange(vha, NULL, atio, 1, 0);
4943 /* Sending marker isn't necessary, since we called from ISR */
4945 pkt = (request_t *)qla2x00_alloc_iocbs(vha, NULL);
4947 ql_dbg(ql_dbg_io, vha, 0x3063,
4948 "qla_target(%d): %s failed: unable to allocate "
4949 "request packet", vha->vp_idx, __func__);
4953 vha->tgt_counters.num_q_full_sent++;
4954 pkt->entry_count = 1;
4955 pkt->handle = QLA_TGT_SKIP_HANDLE | CTIO_COMPLETION_HANDLE_MARK;
4957 ctio24 = (struct ctio7_to_24xx *)pkt;
4958 ctio24->entry_type = CTIO_TYPE7;
4959 ctio24->nport_handle = sess->loop_id;
4960 ctio24->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
4961 ctio24->vp_index = vha->vp_idx;
4962 ctio24->initiator_id[0] = atio->u.isp24.fcp_hdr.s_id[2];
4963 ctio24->initiator_id[1] = atio->u.isp24.fcp_hdr.s_id[1];
4964 ctio24->initiator_id[2] = atio->u.isp24.fcp_hdr.s_id[0];
4965 ctio24->exchange_addr = atio->u.isp24.exchange_addr;
4966 temp = (atio->u.isp24.attr << 9) |
4967 CTIO7_FLAGS_STATUS_MODE_1 | CTIO7_FLAGS_SEND_STATUS |
4968 CTIO7_FLAGS_DONT_RET_CTIO;
4969 ctio24->u.status1.flags = cpu_to_le16(temp);
4971 * CTIO from fw w/o se_cmd doesn't provide enough info to retry it,
4972 * if the explicit conformation is used.
4974 ctio24->u.status1.ox_id = swab16(atio->u.isp24.fcp_hdr.ox_id);
4975 ctio24->u.status1.scsi_status = cpu_to_le16(status);
4976 /* Memory Barrier */
4978 qla2x00_start_iocbs(vha, vha->req);
4983 * This routine is used to allocate a command for either a QFull condition
4984 * (ie reply SAM_STAT_BUSY) or to terminate an exchange that did not go
4988 qlt_alloc_qfull_cmd(struct scsi_qla_host *vha,
4989 struct atio_from_isp *atio, uint16_t status, int qfull)
4991 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
4992 struct qla_hw_data *ha = vha->hw;
4993 struct fc_port *sess;
4994 struct se_session *se_sess;
4995 struct qla_tgt_cmd *cmd;
4998 if (unlikely(tgt->tgt_stop)) {
4999 ql_dbg(ql_dbg_io, vha, 0x300a,
5000 "New command while device %p is shutting down\n", tgt);
5004 if ((vha->hw->tgt.num_qfull_cmds_alloc + 1) > MAX_QFULL_CMDS_ALLOC) {
5005 vha->hw->tgt.num_qfull_cmds_dropped++;
5006 if (vha->hw->tgt.num_qfull_cmds_dropped >
5007 vha->qla_stats.stat_max_qfull_cmds_dropped)
5008 vha->qla_stats.stat_max_qfull_cmds_dropped =
5009 vha->hw->tgt.num_qfull_cmds_dropped;
5011 ql_dbg(ql_dbg_io, vha, 0x3068,
5012 "qla_target(%d): %s: QFull CMD dropped[%d]\n",
5013 vha->vp_idx, __func__,
5014 vha->hw->tgt.num_qfull_cmds_dropped);
5016 qlt_chk_exch_leak_thresh_hold(vha);
5020 sess = ha->tgt.tgt_ops->find_sess_by_s_id
5021 (vha, atio->u.isp24.fcp_hdr.s_id);
5025 se_sess = sess->se_sess;
5027 tag = percpu_ida_alloc(&se_sess->sess_tag_pool, TASK_RUNNING);
5031 cmd = &((struct qla_tgt_cmd *)se_sess->sess_cmd_map)[tag];
5033 ql_dbg(ql_dbg_io, vha, 0x3009,
5034 "qla_target(%d): %s: Allocation of cmd failed\n",
5035 vha->vp_idx, __func__);
5037 vha->hw->tgt.num_qfull_cmds_dropped++;
5038 if (vha->hw->tgt.num_qfull_cmds_dropped >
5039 vha->qla_stats.stat_max_qfull_cmds_dropped)
5040 vha->qla_stats.stat_max_qfull_cmds_dropped =
5041 vha->hw->tgt.num_qfull_cmds_dropped;
5043 qlt_chk_exch_leak_thresh_hold(vha);
5047 memset(cmd, 0, sizeof(struct qla_tgt_cmd));
5049 qlt_incr_num_pend_cmds(vha);
5050 INIT_LIST_HEAD(&cmd->cmd_list);
5051 memcpy(&cmd->atio, atio, sizeof(*atio));
5053 cmd->tgt = vha->vha_tgt.qla_tgt;
5055 cmd->reset_count = vha->hw->chip_reset;
5060 /* NOTE: borrowing the state field to carry the status */
5061 cmd->state = status;
5063 cmd->term_exchg = 1;
5065 list_add_tail(&cmd->cmd_list, &vha->hw->tgt.q_full_list);
5067 vha->hw->tgt.num_qfull_cmds_alloc++;
5068 if (vha->hw->tgt.num_qfull_cmds_alloc >
5069 vha->qla_stats.stat_max_qfull_cmds_alloc)
5070 vha->qla_stats.stat_max_qfull_cmds_alloc =
5071 vha->hw->tgt.num_qfull_cmds_alloc;
5075 qlt_free_qfull_cmds(struct scsi_qla_host *vha)
5077 struct qla_hw_data *ha = vha->hw;
5078 unsigned long flags;
5079 struct qla_tgt_cmd *cmd, *tcmd;
5080 struct list_head free_list;
5083 if (list_empty(&ha->tgt.q_full_list))
5086 INIT_LIST_HEAD(&free_list);
5088 spin_lock_irqsave(&vha->hw->hardware_lock, flags);
5090 if (list_empty(&ha->tgt.q_full_list)) {
5091 spin_unlock_irqrestore(&vha->hw->hardware_lock, flags);
5095 list_for_each_entry_safe(cmd, tcmd, &ha->tgt.q_full_list, cmd_list) {
5097 /* cmd->state is a borrowed field to hold status */
5098 rc = __qlt_send_busy(vha, &cmd->atio, cmd->state);
5099 else if (cmd->term_exchg)
5100 rc = __qlt_send_term_exchange(vha, NULL, &cmd->atio);
5106 ql_dbg(ql_dbg_io, vha, 0x3006,
5107 "%s: busy sent for ox_id[%04x]\n", __func__,
5108 be16_to_cpu(cmd->atio.u.isp24.fcp_hdr.ox_id));
5109 else if (cmd->term_exchg)
5110 ql_dbg(ql_dbg_io, vha, 0x3007,
5111 "%s: Term exchg sent for ox_id[%04x]\n", __func__,
5112 be16_to_cpu(cmd->atio.u.isp24.fcp_hdr.ox_id));
5114 ql_dbg(ql_dbg_io, vha, 0x3008,
5115 "%s: Unexpected cmd in QFull list %p\n", __func__,
5118 list_del(&cmd->cmd_list);
5119 list_add_tail(&cmd->cmd_list, &free_list);
5121 /* piggy back on hardware_lock for protection */
5122 vha->hw->tgt.num_qfull_cmds_alloc--;
5124 spin_unlock_irqrestore(&vha->hw->hardware_lock, flags);
5128 list_for_each_entry_safe(cmd, tcmd, &free_list, cmd_list) {
5129 list_del(&cmd->cmd_list);
5130 /* This cmd was never sent to TCM. There is no need
5131 * to schedule free or call free_cmd
5139 qlt_send_busy(struct scsi_qla_host *vha,
5140 struct atio_from_isp *atio, uint16_t status)
5144 rc = __qlt_send_busy(vha, atio, status);
5146 qlt_alloc_qfull_cmd(vha, atio, status, 1);
5150 qlt_chk_qfull_thresh_hold(struct scsi_qla_host *vha,
5151 struct atio_from_isp *atio, bool ha_locked)
5153 struct qla_hw_data *ha = vha->hw;
5155 unsigned long flags;
5157 if (ha->tgt.num_pend_cmds < Q_FULL_THRESH_HOLD(ha))
5161 spin_lock_irqsave(&ha->hardware_lock, flags);
5162 status = temp_sam_status;
5163 qlt_send_busy(vha, atio, status);
5165 spin_unlock_irqrestore(&ha->hardware_lock, flags);
5170 /* ha->hardware_lock supposed to be held on entry */
5171 /* called via callback from qla2xxx */
5172 static void qlt_24xx_atio_pkt(struct scsi_qla_host *vha,
5173 struct atio_from_isp *atio, uint8_t ha_locked)
5175 struct qla_hw_data *ha = vha->hw;
5176 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
5178 unsigned long flags;
5180 if (unlikely(tgt == NULL)) {
5181 ql_dbg(ql_dbg_tgt, vha, 0x3064,
5182 "ATIO pkt, but no tgt (ha %p)", ha);
5186 * In tgt_stop mode we also should allow all requests to pass.
5187 * Otherwise, some commands can stuck.
5190 tgt->atio_irq_cmd_count++;
5192 switch (atio->u.raw.entry_type) {
5194 if (unlikely(atio->u.isp24.exchange_addr ==
5195 ATIO_EXCHANGE_ADDRESS_UNKNOWN)) {
5196 ql_dbg(ql_dbg_io, vha, 0x3065,
5197 "qla_target(%d): ATIO_TYPE7 "
5198 "received with UNKNOWN exchange address, "
5199 "sending QUEUE_FULL\n", vha->vp_idx);
5201 spin_lock_irqsave(&ha->hardware_lock, flags);
5202 qlt_send_busy(vha, atio, SAM_STAT_TASK_SET_FULL);
5204 spin_unlock_irqrestore(&ha->hardware_lock, flags);
5210 if (likely(atio->u.isp24.fcp_cmnd.task_mgmt_flags == 0)) {
5211 rc = qlt_chk_qfull_thresh_hold(vha, atio, ha_locked);
5213 tgt->atio_irq_cmd_count--;
5216 rc = qlt_handle_cmd_for_atio(vha, atio);
5218 rc = qlt_handle_task_mgmt(vha, atio);
5220 if (unlikely(rc != 0)) {
5224 (&ha->hardware_lock, flags);
5226 #if 1 /* With TERM EXCHANGE some FC cards refuse to boot */
5227 qlt_send_busy(vha, atio, SAM_STAT_BUSY);
5229 qlt_send_term_exchange(vha, NULL, atio, 1, 0);
5233 spin_unlock_irqrestore
5234 (&ha->hardware_lock, flags);
5237 if (tgt->tgt_stop) {
5238 ql_dbg(ql_dbg_tgt, vha, 0xe059,
5239 "qla_target: Unable to send "
5240 "command to target for req, "
5243 ql_dbg(ql_dbg_tgt, vha, 0xe05a,
5244 "qla_target(%d): Unable to send "
5245 "command to target, sending BUSY "
5246 "status.\n", vha->vp_idx);
5249 &ha->hardware_lock, flags);
5250 qlt_send_busy(vha, atio, SAM_STAT_BUSY);
5252 spin_unlock_irqrestore(
5253 &ha->hardware_lock, flags);
5259 case IMMED_NOTIFY_TYPE:
5261 if (unlikely(atio->u.isp2x.entry_status != 0)) {
5262 ql_dbg(ql_dbg_tgt, vha, 0xe05b,
5263 "qla_target(%d): Received ATIO packet %x "
5264 "with error status %x\n", vha->vp_idx,
5265 atio->u.raw.entry_type,
5266 atio->u.isp2x.entry_status);
5269 ql_dbg(ql_dbg_tgt, vha, 0xe02e, "%s", "IMMED_NOTIFY ATIO");
5272 spin_lock_irqsave(&ha->hardware_lock, flags);
5273 qlt_handle_imm_notify(vha, (struct imm_ntfy_from_isp *)atio);
5275 spin_unlock_irqrestore(&ha->hardware_lock, flags);
5280 ql_dbg(ql_dbg_tgt, vha, 0xe05c,
5281 "qla_target(%d): Received unknown ATIO atio "
5282 "type %x\n", vha->vp_idx, atio->u.raw.entry_type);
5286 tgt->atio_irq_cmd_count--;
5289 /* ha->hardware_lock supposed to be held on entry */
5290 /* called via callback from qla2xxx */
5291 static void qlt_response_pkt(struct scsi_qla_host *vha, response_t *pkt)
5293 struct qla_hw_data *ha = vha->hw;
5294 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
5296 if (unlikely(tgt == NULL)) {
5297 ql_dbg(ql_dbg_tgt, vha, 0xe05d,
5298 "qla_target(%d): Response pkt %x received, but no "
5299 "tgt (ha %p)\n", vha->vp_idx, pkt->entry_type, ha);
5304 * In tgt_stop mode we also should allow all requests to pass.
5305 * Otherwise, some commands can stuck.
5308 switch (pkt->entry_type) {
5312 struct ctio7_from_24xx *entry = (struct ctio7_from_24xx *)pkt;
5313 qlt_do_ctio_completion(vha, entry->handle,
5314 le16_to_cpu(entry->status)|(pkt->entry_status << 16),
5319 case ACCEPT_TGT_IO_TYPE:
5321 struct atio_from_isp *atio = (struct atio_from_isp *)pkt;
5323 if (atio->u.isp2x.status !=
5324 cpu_to_le16(ATIO_CDB_VALID)) {
5325 ql_dbg(ql_dbg_tgt, vha, 0xe05e,
5326 "qla_target(%d): ATIO with error "
5327 "status %x received\n", vha->vp_idx,
5328 le16_to_cpu(atio->u.isp2x.status));
5332 rc = qlt_chk_qfull_thresh_hold(vha, atio, true);
5336 rc = qlt_handle_cmd_for_atio(vha, atio);
5337 if (unlikely(rc != 0)) {
5339 #if 1 /* With TERM EXCHANGE some FC cards refuse to boot */
5340 qlt_send_busy(vha, atio, 0);
5342 qlt_send_term_exchange(vha, NULL, atio, 1, 0);
5345 if (tgt->tgt_stop) {
5346 ql_dbg(ql_dbg_tgt, vha, 0xe05f,
5347 "qla_target: Unable to send "
5348 "command to target, sending TERM "
5349 "EXCHANGE for rsp\n");
5350 qlt_send_term_exchange(vha, NULL,
5353 ql_dbg(ql_dbg_tgt, vha, 0xe060,
5354 "qla_target(%d): Unable to send "
5355 "command to target, sending BUSY "
5356 "status\n", vha->vp_idx);
5357 qlt_send_busy(vha, atio, 0);
5364 case CONTINUE_TGT_IO_TYPE:
5366 struct ctio_to_2xxx *entry = (struct ctio_to_2xxx *)pkt;
5367 qlt_do_ctio_completion(vha, entry->handle,
5368 le16_to_cpu(entry->status)|(pkt->entry_status << 16),
5375 struct ctio_to_2xxx *entry = (struct ctio_to_2xxx *)pkt;
5376 qlt_do_ctio_completion(vha, entry->handle,
5377 le16_to_cpu(entry->status)|(pkt->entry_status << 16),
5382 case IMMED_NOTIFY_TYPE:
5383 ql_dbg(ql_dbg_tgt, vha, 0xe035, "%s", "IMMED_NOTIFY\n");
5384 qlt_handle_imm_notify(vha, (struct imm_ntfy_from_isp *)pkt);
5387 case NOTIFY_ACK_TYPE:
5388 if (tgt->notify_ack_expected > 0) {
5389 struct nack_to_isp *entry = (struct nack_to_isp *)pkt;
5390 ql_dbg(ql_dbg_tgt, vha, 0xe036,
5391 "NOTIFY_ACK seq %08x status %x\n",
5392 le16_to_cpu(entry->u.isp2x.seq_id),
5393 le16_to_cpu(entry->u.isp2x.status));
5394 tgt->notify_ack_expected--;
5395 if (entry->u.isp2x.status !=
5396 cpu_to_le16(NOTIFY_ACK_SUCCESS)) {
5397 ql_dbg(ql_dbg_tgt, vha, 0xe061,
5398 "qla_target(%d): NOTIFY_ACK "
5399 "failed %x\n", vha->vp_idx,
5400 le16_to_cpu(entry->u.isp2x.status));
5403 ql_dbg(ql_dbg_tgt, vha, 0xe062,
5404 "qla_target(%d): Unexpected NOTIFY_ACK received\n",
5409 case ABTS_RECV_24XX:
5410 ql_dbg(ql_dbg_tgt, vha, 0xe037,
5411 "ABTS_RECV_24XX: instance %d\n", vha->vp_idx);
5412 qlt_24xx_handle_abts(vha, (struct abts_recv_from_24xx *)pkt);
5415 case ABTS_RESP_24XX:
5416 if (tgt->abts_resp_expected > 0) {
5417 struct abts_resp_from_24xx_fw *entry =
5418 (struct abts_resp_from_24xx_fw *)pkt;
5419 ql_dbg(ql_dbg_tgt, vha, 0xe038,
5420 "ABTS_RESP_24XX: compl_status %x\n",
5421 entry->compl_status);
5422 tgt->abts_resp_expected--;
5423 if (le16_to_cpu(entry->compl_status) !=
5424 ABTS_RESP_COMPL_SUCCESS) {
5425 if ((entry->error_subcode1 == 0x1E) &&
5426 (entry->error_subcode2 == 0)) {
5428 * We've got a race here: aborted
5429 * exchange not terminated, i.e.
5430 * response for the aborted command was
5431 * sent between the abort request was
5432 * received and processed.
5433 * Unfortunately, the firmware has a
5434 * silly requirement that all aborted
5435 * exchanges must be explicitely
5436 * terminated, otherwise it refuses to
5437 * send responses for the abort
5438 * requests. So, we have to
5439 * (re)terminate the exchange and retry
5440 * the abort response.
5442 qlt_24xx_retry_term_exchange(vha,
5445 ql_dbg(ql_dbg_tgt, vha, 0xe063,
5446 "qla_target(%d): ABTS_RESP_24XX "
5447 "failed %x (subcode %x:%x)",
5448 vha->vp_idx, entry->compl_status,
5449 entry->error_subcode1,
5450 entry->error_subcode2);
5453 ql_dbg(ql_dbg_tgt, vha, 0xe064,
5454 "qla_target(%d): Unexpected ABTS_RESP_24XX "
5455 "received\n", vha->vp_idx);
5460 ql_dbg(ql_dbg_tgt, vha, 0xe065,
5461 "qla_target(%d): Received unknown response pkt "
5462 "type %x\n", vha->vp_idx, pkt->entry_type);
5469 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
5471 void qlt_async_event(uint16_t code, struct scsi_qla_host *vha,
5474 struct qla_hw_data *ha = vha->hw;
5475 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
5478 if (!tgt || tgt->tgt_stop || tgt->tgt_stopped)
5481 if (((code == MBA_POINT_TO_POINT) || (code == MBA_CHG_IN_CONNECTION)) &&
5485 * In tgt_stop mode we also should allow all requests to pass.
5486 * Otherwise, some commands can stuck.
5491 case MBA_RESET: /* Reset */
5492 case MBA_SYSTEM_ERR: /* System Error */
5493 case MBA_REQ_TRANSFER_ERR: /* Request Transfer Error */
5494 case MBA_RSP_TRANSFER_ERR: /* Response Transfer Error */
5495 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03a,
5496 "qla_target(%d): System error async event %#x "
5497 "occurred", vha->vp_idx, code);
5499 case MBA_WAKEUP_THRES: /* Request Queue Wake-up. */
5500 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
5505 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03b,
5506 "qla_target(%d): Async LOOP_UP occurred "
5507 "(m[0]=%x, m[1]=%x, m[2]=%x, m[3]=%x)", vha->vp_idx,
5508 le16_to_cpu(mailbox[0]), le16_to_cpu(mailbox[1]),
5509 le16_to_cpu(mailbox[2]), le16_to_cpu(mailbox[3]));
5510 if (tgt->link_reinit_iocb_pending) {
5511 qlt_send_notify_ack(vha, (void *)&tgt->link_reinit_iocb,
5513 tgt->link_reinit_iocb_pending = 0;
5518 case MBA_LIP_OCCURRED:
5521 case MBA_RSCN_UPDATE:
5522 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03c,
5523 "qla_target(%d): Async event %#x occurred "
5524 "(m[0]=%x, m[1]=%x, m[2]=%x, m[3]=%x)", vha->vp_idx, code,
5525 le16_to_cpu(mailbox[0]), le16_to_cpu(mailbox[1]),
5526 le16_to_cpu(mailbox[2]), le16_to_cpu(mailbox[3]));
5529 case MBA_REJECTED_FCP_CMD:
5530 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf017,
5531 "qla_target(%d): Async event LS_REJECT occurred (m[0]=%x, m[1]=%x, m[2]=%x, m[3]=%x)",
5533 le16_to_cpu(mailbox[0]), le16_to_cpu(mailbox[1]),
5534 le16_to_cpu(mailbox[2]), le16_to_cpu(mailbox[3]));
5536 if (le16_to_cpu(mailbox[3]) == 1) {
5537 /* exchange starvation. */
5538 vha->hw->exch_starvation++;
5539 if (vha->hw->exch_starvation > 5) {
5540 ql_log(ql_log_warn, vha, 0xd03a,
5541 "Exchange starvation-. Resetting RISC\n");
5543 vha->hw->exch_starvation = 0;
5544 if (IS_P3P_TYPE(vha->hw))
5545 set_bit(FCOE_CTX_RESET_NEEDED,
5548 set_bit(ISP_ABORT_NEEDED,
5550 qla2xxx_wake_dpc(vha);
5555 case MBA_PORT_UPDATE:
5556 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03d,
5557 "qla_target(%d): Port update async event %#x "
5558 "occurred: updating the ports database (m[0]=%x, m[1]=%x, "
5559 "m[2]=%x, m[3]=%x)", vha->vp_idx, code,
5560 le16_to_cpu(mailbox[0]), le16_to_cpu(mailbox[1]),
5561 le16_to_cpu(mailbox[2]), le16_to_cpu(mailbox[3]));
5563 login_code = le16_to_cpu(mailbox[2]);
5564 if (login_code == 0x4) {
5565 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03e,
5566 "Async MB 2: Got PLOGI Complete\n");
5567 vha->hw->exch_starvation = 0;
5568 } else if (login_code == 0x7)
5569 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03f,
5570 "Async MB 2: Port Logged Out\n");
5578 static fc_port_t *qlt_get_port_database(struct scsi_qla_host *vha,
5581 fc_port_t *fcport, *tfcp, *del;
5583 unsigned long flags;
5586 fcport = kzalloc(sizeof(*fcport), GFP_KERNEL);
5588 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06f,
5589 "qla_target(%d): Allocation of tmp FC port failed",
5594 fcport->loop_id = loop_id;
5596 rc = qla24xx_gpdb_wait(vha, fcport, 0);
5597 if (rc != QLA_SUCCESS) {
5598 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf070,
5599 "qla_target(%d): Failed to retrieve fcport "
5600 "information -- get_port_database() returned %x "
5601 "(loop_id=0x%04x)", vha->vp_idx, rc, loop_id);
5607 spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
5608 tfcp = qla2x00_find_fcport_by_wwpn(vha, fcport->port_name, 1);
5611 tfcp->d_id = fcport->d_id;
5612 tfcp->port_type = fcport->port_type;
5613 tfcp->supported_classes = fcport->supported_classes;
5614 tfcp->flags |= fcport->flags;
5619 if (vha->hw->current_topology == ISP_CFG_F)
5620 fcport->flags |= FCF_FABRIC_DEVICE;
5622 list_add_tail(&fcport->list, &vha->vp_fcports);
5623 if (!IS_SW_RESV_ADDR(fcport->d_id))
5624 vha->fcport_count++;
5625 fcport->login_gen++;
5626 fcport->disc_state = DSC_LOGIN_COMPLETE;
5627 fcport->login_succ = 1;
5631 fcport->deleted = 0;
5632 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
5634 switch (vha->host->active_mode) {
5635 case MODE_INITIATOR:
5638 if (!IS_IIDMA_CAPABLE(vha->hw) || !vha->hw->flags.gpsc_supported) {
5639 ql_dbg(ql_dbg_disc, vha, 0x20fe,
5640 "%s %d %8phC post upd_fcport fcp_cnt %d\n",
5641 __func__, __LINE__, fcport->port_name, vha->fcport_count);
5642 qla24xx_post_upd_fcport_work(vha, fcport);
5644 ql_dbg(ql_dbg_disc, vha, 0x20ff,
5645 "%s %d %8phC post gpsc fcp_cnt %d\n",
5646 __func__, __LINE__, fcport->port_name, vha->fcport_count);
5647 qla24xx_post_gpsc_work(vha, fcport);
5657 qla2x00_free_fcport(del);
5662 /* Must be called under tgt_mutex */
5663 static struct fc_port *qlt_make_local_sess(struct scsi_qla_host *vha,
5666 struct fc_port *sess = NULL;
5667 fc_port_t *fcport = NULL;
5668 int rc, global_resets;
5669 uint16_t loop_id = 0;
5671 if ((s_id[0] == 0xFF) && (s_id[1] == 0xFC)) {
5673 * This is Domain Controller, so it should be
5674 * OK to drop SCSI commands from it.
5676 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf042,
5677 "Unable to find initiator with S_ID %x:%x:%x",
5678 s_id[0], s_id[1], s_id[2]);
5682 mutex_lock(&vha->vha_tgt.tgt_mutex);
5686 atomic_read(&vha->vha_tgt.qla_tgt->tgt_global_resets_count);
5688 rc = qla24xx_get_loop_id(vha, s_id, &loop_id);
5690 mutex_unlock(&vha->vha_tgt.tgt_mutex);
5692 ql_log(ql_log_info, vha, 0xf071,
5693 "qla_target(%d): Unable to find "
5694 "initiator with S_ID %x:%x:%x",
5695 vha->vp_idx, s_id[0], s_id[1],
5698 if (rc == -ENOENT) {
5699 qlt_port_logo_t logo;
5700 sid_to_portid(s_id, &logo.id);
5702 qlt_send_first_logo(vha, &logo);
5708 fcport = qlt_get_port_database(vha, loop_id);
5710 mutex_unlock(&vha->vha_tgt.tgt_mutex);
5714 if (global_resets !=
5715 atomic_read(&vha->vha_tgt.qla_tgt->tgt_global_resets_count)) {
5716 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf043,
5717 "qla_target(%d): global reset during session discovery "
5718 "(counter was %d, new %d), retrying", vha->vp_idx,
5720 atomic_read(&vha->vha_tgt.
5721 qla_tgt->tgt_global_resets_count));
5725 sess = qlt_create_sess(vha, fcport, true);
5727 mutex_unlock(&vha->vha_tgt.tgt_mutex);
5732 static void qlt_abort_work(struct qla_tgt *tgt,
5733 struct qla_tgt_sess_work_param *prm)
5735 struct scsi_qla_host *vha = tgt->vha;
5736 struct qla_hw_data *ha = vha->hw;
5737 struct fc_port *sess = NULL;
5738 unsigned long flags = 0, flags2 = 0;
5743 spin_lock_irqsave(&ha->tgt.sess_lock, flags2);
5748 s_id[0] = prm->abts.fcp_hdr_le.s_id[2];
5749 s_id[1] = prm->abts.fcp_hdr_le.s_id[1];
5750 s_id[2] = prm->abts.fcp_hdr_le.s_id[0];
5752 sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha,
5753 (unsigned char *)&be_s_id);
5755 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags2);
5757 sess = qlt_make_local_sess(vha, s_id);
5758 /* sess has got an extra creation ref */
5760 spin_lock_irqsave(&ha->tgt.sess_lock, flags2);
5764 if (sess->deleted) {
5769 if (!kref_get_unless_zero(&sess->sess_kref)) {
5770 ql_dbg(ql_dbg_tgt_tmr, vha, 0xf01c,
5771 "%s: kref_get fail %8phC \n",
5772 __func__, sess->port_name);
5778 rc = __qlt_24xx_handle_abts(vha, &prm->abts, sess);
5779 ha->tgt.tgt_ops->put_sess(sess);
5780 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags2);
5788 ha->tgt.tgt_ops->put_sess(sess);
5789 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags2);
5792 spin_lock_irqsave(&ha->hardware_lock, flags);
5793 qlt_24xx_send_abts_resp(vha, &prm->abts, FCP_TMF_REJECTED, false);
5794 spin_unlock_irqrestore(&ha->hardware_lock, flags);
5797 static void qlt_tmr_work(struct qla_tgt *tgt,
5798 struct qla_tgt_sess_work_param *prm)
5800 struct atio_from_isp *a = &prm->tm_iocb2;
5801 struct scsi_qla_host *vha = tgt->vha;
5802 struct qla_hw_data *ha = vha->hw;
5803 struct fc_port *sess = NULL;
5804 unsigned long flags;
5805 uint8_t *s_id = NULL; /* to hide compiler warnings */
5811 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
5816 s_id = prm->tm_iocb2.u.isp24.fcp_hdr.s_id;
5817 sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha, s_id);
5819 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
5821 sess = qlt_make_local_sess(vha, s_id);
5822 /* sess has got an extra creation ref */
5824 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
5828 if (sess->deleted) {
5833 if (!kref_get_unless_zero(&sess->sess_kref)) {
5834 ql_dbg(ql_dbg_tgt_tmr, vha, 0xf020,
5835 "%s: kref_get fail %8phC\n",
5836 __func__, sess->port_name);
5843 fn = a->u.isp24.fcp_cmnd.task_mgmt_flags;
5845 scsilun_to_int((struct scsi_lun *)&a->u.isp24.fcp_cmnd.lun);
5847 rc = qlt_issue_task_mgmt(sess, unpacked_lun, fn, iocb, 0);
5848 ha->tgt.tgt_ops->put_sess(sess);
5849 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
5857 ha->tgt.tgt_ops->put_sess(sess);
5858 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
5860 qlt_send_term_exchange(vha, NULL, &prm->tm_iocb2, 1, 0);
5863 static void qlt_sess_work_fn(struct work_struct *work)
5865 struct qla_tgt *tgt = container_of(work, struct qla_tgt, sess_work);
5866 struct scsi_qla_host *vha = tgt->vha;
5867 unsigned long flags;
5869 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf000, "Sess work (tgt %p)", tgt);
5871 spin_lock_irqsave(&tgt->sess_work_lock, flags);
5872 while (!list_empty(&tgt->sess_works_list)) {
5873 struct qla_tgt_sess_work_param *prm = list_entry(
5874 tgt->sess_works_list.next, typeof(*prm),
5875 sess_works_list_entry);
5878 * This work can be scheduled on several CPUs at time, so we
5879 * must delete the entry to eliminate double processing
5881 list_del(&prm->sess_works_list_entry);
5883 spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
5885 switch (prm->type) {
5886 case QLA_TGT_SESS_WORK_ABORT:
5887 qlt_abort_work(tgt, prm);
5889 case QLA_TGT_SESS_WORK_TM:
5890 qlt_tmr_work(tgt, prm);
5897 spin_lock_irqsave(&tgt->sess_work_lock, flags);
5901 spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
5904 /* Must be called under tgt_host_action_mutex */
5905 int qlt_add_target(struct qla_hw_data *ha, struct scsi_qla_host *base_vha)
5907 struct qla_tgt *tgt;
5909 if (!QLA_TGT_MODE_ENABLED())
5912 if (!IS_TGT_MODE_CAPABLE(ha)) {
5913 ql_log(ql_log_warn, base_vha, 0xe070,
5914 "This adapter does not support target mode.\n");
5918 ql_dbg(ql_dbg_tgt, base_vha, 0xe03b,
5919 "Registering target for host %ld(%p).\n", base_vha->host_no, ha);
5921 BUG_ON(base_vha->vha_tgt.qla_tgt != NULL);
5923 tgt = kzalloc(sizeof(struct qla_tgt), GFP_KERNEL);
5925 ql_dbg(ql_dbg_tgt, base_vha, 0xe066,
5926 "Unable to allocate struct qla_tgt\n");
5930 if (!(base_vha->host->hostt->supported_mode & MODE_TARGET))
5931 base_vha->host->hostt->supported_mode |= MODE_TARGET;
5934 tgt->vha = base_vha;
5935 init_waitqueue_head(&tgt->waitQ);
5936 INIT_LIST_HEAD(&tgt->del_sess_list);
5937 spin_lock_init(&tgt->sess_work_lock);
5938 INIT_WORK(&tgt->sess_work, qlt_sess_work_fn);
5939 INIT_LIST_HEAD(&tgt->sess_works_list);
5940 atomic_set(&tgt->tgt_global_resets_count, 0);
5942 base_vha->vha_tgt.qla_tgt = tgt;
5944 ql_dbg(ql_dbg_tgt, base_vha, 0xe067,
5945 "qla_target(%d): using 64 Bit PCI addressing",
5947 tgt->tgt_enable_64bit_addr = 1;
5949 tgt->sg_tablesize = QLA_TGT_MAX_SG_24XX(base_vha->req->length - 3);
5950 tgt->datasegs_per_cmd = QLA_TGT_DATASEGS_PER_CMD_24XX;
5951 tgt->datasegs_per_cont = QLA_TGT_DATASEGS_PER_CONT_24XX;
5953 mutex_lock(&qla_tgt_mutex);
5954 list_add_tail(&tgt->tgt_list_entry, &qla_tgt_glist);
5955 mutex_unlock(&qla_tgt_mutex);
5957 if (ha->tgt.tgt_ops && ha->tgt.tgt_ops->add_target)
5958 ha->tgt.tgt_ops->add_target(base_vha);
5963 /* Must be called under tgt_host_action_mutex */
5964 int qlt_remove_target(struct qla_hw_data *ha, struct scsi_qla_host *vha)
5966 if (!vha->vha_tgt.qla_tgt)
5969 if (vha->fc_vport) {
5970 qlt_release(vha->vha_tgt.qla_tgt);
5974 /* free left over qfull cmds */
5975 qlt_init_term_exchange(vha);
5977 mutex_lock(&qla_tgt_mutex);
5978 list_del(&vha->vha_tgt.qla_tgt->tgt_list_entry);
5979 mutex_unlock(&qla_tgt_mutex);
5981 ql_dbg(ql_dbg_tgt, vha, 0xe03c, "Unregistering target for host %ld(%p)",
5983 qlt_release(vha->vha_tgt.qla_tgt);
5988 void qlt_remove_target_resources(struct qla_hw_data *ha)
5990 struct scsi_qla_host *node;
5993 btree_for_each_safe32(&ha->tgt.host_map, key, node)
5994 btree_remove32(&ha->tgt.host_map, key);
5996 btree_destroy32(&ha->tgt.host_map);
5999 static void qlt_lport_dump(struct scsi_qla_host *vha, u64 wwpn,
6004 pr_debug("qla2xxx HW vha->node_name: ");
6005 for (i = 0; i < WWN_SIZE; i++)
6006 pr_debug("%02x ", vha->node_name[i]);
6008 pr_debug("qla2xxx HW vha->port_name: ");
6009 for (i = 0; i < WWN_SIZE; i++)
6010 pr_debug("%02x ", vha->port_name[i]);
6013 pr_debug("qla2xxx passed configfs WWPN: ");
6014 put_unaligned_be64(wwpn, b);
6015 for (i = 0; i < WWN_SIZE; i++)
6016 pr_debug("%02x ", b[i]);
6021 * qla_tgt_lport_register - register lport with external module
6023 * @qla_tgt_ops: Pointer for tcm_qla2xxx qla_tgt_ops
6024 * @wwpn: Passwd FC target WWPN
6025 * @callback: lport initialization callback for tcm_qla2xxx code
6026 * @target_lport_ptr: pointer for tcm_qla2xxx specific lport data
6028 int qlt_lport_register(void *target_lport_ptr, u64 phys_wwpn,
6029 u64 npiv_wwpn, u64 npiv_wwnn,
6030 int (*callback)(struct scsi_qla_host *, void *, u64, u64))
6032 struct qla_tgt *tgt;
6033 struct scsi_qla_host *vha;
6034 struct qla_hw_data *ha;
6035 struct Scsi_Host *host;
6036 unsigned long flags;
6040 mutex_lock(&qla_tgt_mutex);
6041 list_for_each_entry(tgt, &qla_tgt_glist, tgt_list_entry) {
6049 if (!(host->hostt->supported_mode & MODE_TARGET))
6052 spin_lock_irqsave(&ha->hardware_lock, flags);
6053 if ((!npiv_wwpn || !npiv_wwnn) && host->active_mode & MODE_TARGET) {
6054 pr_debug("MODE_TARGET already active on qla2xxx(%d)\n",
6056 spin_unlock_irqrestore(&ha->hardware_lock, flags);
6059 if (tgt->tgt_stop) {
6060 pr_debug("MODE_TARGET in shutdown on qla2xxx(%d)\n",
6062 spin_unlock_irqrestore(&ha->hardware_lock, flags);
6065 spin_unlock_irqrestore(&ha->hardware_lock, flags);
6067 if (!scsi_host_get(host)) {
6068 ql_dbg(ql_dbg_tgt, vha, 0xe068,
6069 "Unable to scsi_host_get() for"
6070 " qla2xxx scsi_host\n");
6073 qlt_lport_dump(vha, phys_wwpn, b);
6075 if (memcmp(vha->port_name, b, WWN_SIZE)) {
6076 scsi_host_put(host);
6079 rc = (*callback)(vha, target_lport_ptr, npiv_wwpn, npiv_wwnn);
6081 scsi_host_put(host);
6083 mutex_unlock(&qla_tgt_mutex);
6086 mutex_unlock(&qla_tgt_mutex);
6090 EXPORT_SYMBOL(qlt_lport_register);
6093 * qla_tgt_lport_deregister - Degister lport
6095 * @vha: Registered scsi_qla_host pointer
6097 void qlt_lport_deregister(struct scsi_qla_host *vha)
6099 struct qla_hw_data *ha = vha->hw;
6100 struct Scsi_Host *sh = vha->host;
6102 * Clear the target_lport_ptr qla_target_template pointer in qla_hw_data
6104 vha->vha_tgt.target_lport_ptr = NULL;
6105 ha->tgt.tgt_ops = NULL;
6107 * Release the Scsi_Host reference for the underlying qla2xxx host
6111 EXPORT_SYMBOL(qlt_lport_deregister);
6113 /* Must be called under HW lock */
6114 static void qlt_set_mode(struct scsi_qla_host *vha)
6116 switch (ql2x_ini_mode) {
6117 case QLA2XXX_INI_MODE_DISABLED:
6118 case QLA2XXX_INI_MODE_EXCLUSIVE:
6119 vha->host->active_mode = MODE_TARGET;
6121 case QLA2XXX_INI_MODE_ENABLED:
6122 vha->host->active_mode = MODE_UNKNOWN;
6124 case QLA2XXX_INI_MODE_DUAL:
6125 vha->host->active_mode = MODE_DUAL;
6132 /* Must be called under HW lock */
6133 static void qlt_clear_mode(struct scsi_qla_host *vha)
6135 switch (ql2x_ini_mode) {
6136 case QLA2XXX_INI_MODE_DISABLED:
6137 vha->host->active_mode = MODE_UNKNOWN;
6139 case QLA2XXX_INI_MODE_EXCLUSIVE:
6140 vha->host->active_mode = MODE_INITIATOR;
6142 case QLA2XXX_INI_MODE_ENABLED:
6143 case QLA2XXX_INI_MODE_DUAL:
6144 vha->host->active_mode = MODE_INITIATOR;
6152 * qla_tgt_enable_vha - NO LOCK HELD
6154 * host_reset, bring up w/ Target Mode Enabled
6157 qlt_enable_vha(struct scsi_qla_host *vha)
6159 struct qla_hw_data *ha = vha->hw;
6160 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
6161 unsigned long flags;
6162 scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
6163 int rspq_ent = QLA83XX_RSPQ_MSIX_ENTRY_NUMBER;
6166 ql_dbg(ql_dbg_tgt, vha, 0xe069,
6167 "Unable to locate qla_tgt pointer from"
6168 " struct qla_hw_data\n");
6173 spin_lock_irqsave(&ha->hardware_lock, flags);
6174 tgt->tgt_stopped = 0;
6176 spin_unlock_irqrestore(&ha->hardware_lock, flags);
6179 qla24xx_disable_vp(vha);
6180 qla24xx_enable_vp(vha);
6182 if (ha->msix_entries) {
6183 ql_dbg(ql_dbg_tgt, vha, 0xe081,
6184 "%s: host%ld : vector %d cpu %d\n",
6185 __func__, vha->host_no,
6186 ha->msix_entries[rspq_ent].vector,
6187 ha->msix_entries[rspq_ent].cpuid);
6189 ha->tgt.rspq_vector_cpuid =
6190 ha->msix_entries[rspq_ent].cpuid;
6193 set_bit(ISP_ABORT_NEEDED, &base_vha->dpc_flags);
6194 qla2xxx_wake_dpc(base_vha);
6195 qla2x00_wait_for_hba_online(base_vha);
6198 EXPORT_SYMBOL(qlt_enable_vha);
6201 * qla_tgt_disable_vha - NO LOCK HELD
6203 * Disable Target Mode and reset the adapter
6205 static void qlt_disable_vha(struct scsi_qla_host *vha)
6207 struct qla_hw_data *ha = vha->hw;
6208 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
6209 unsigned long flags;
6212 ql_dbg(ql_dbg_tgt, vha, 0xe06a,
6213 "Unable to locate qla_tgt pointer from"
6214 " struct qla_hw_data\n");
6219 spin_lock_irqsave(&ha->hardware_lock, flags);
6220 qlt_clear_mode(vha);
6221 spin_unlock_irqrestore(&ha->hardware_lock, flags);
6223 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
6224 qla2xxx_wake_dpc(vha);
6225 qla2x00_wait_for_hba_online(vha);
6229 * Called from qla_init.c:qla24xx_vport_create() contex to setup
6230 * the target mode specific struct scsi_qla_host and struct qla_hw_data
6234 qlt_vport_create(struct scsi_qla_host *vha, struct qla_hw_data *ha)
6236 vha->vha_tgt.qla_tgt = NULL;
6238 mutex_init(&vha->vha_tgt.tgt_mutex);
6239 mutex_init(&vha->vha_tgt.tgt_host_action_mutex);
6241 qlt_clear_mode(vha);
6244 * NOTE: Currently the value is kept the same for <24xx and
6245 * >=24xx ISPs. If it is necessary to change it,
6246 * the check should be added for specific ISPs,
6247 * assigning the value appropriately.
6249 ha->tgt.atio_q_length = ATIO_ENTRY_CNT_24XX;
6251 qlt_add_target(ha, vha);
6255 qlt_rff_id(struct scsi_qla_host *vha, struct ct_sns_req *ct_req)
6258 * FC-4 Feature bit 0 indicates target functionality to the name server.
6260 if (qla_tgt_mode_enabled(vha)) {
6261 ct_req->req.rff_id.fc4_feature = BIT_0;
6262 } else if (qla_ini_mode_enabled(vha)) {
6263 ct_req->req.rff_id.fc4_feature = BIT_1;
6264 } else if (qla_dual_mode_enabled(vha))
6265 ct_req->req.rff_id.fc4_feature = BIT_0 | BIT_1;
6269 * qlt_init_atio_q_entries() - Initializes ATIO queue entries.
6272 * Beginning of ATIO ring has initialization control block already built
6273 * by nvram config routine.
6275 * Returns 0 on success.
6278 qlt_init_atio_q_entries(struct scsi_qla_host *vha)
6280 struct qla_hw_data *ha = vha->hw;
6282 struct atio_from_isp *pkt = (struct atio_from_isp *)ha->tgt.atio_ring;
6284 if (qla_ini_mode_enabled(vha))
6287 for (cnt = 0; cnt < ha->tgt.atio_q_length; cnt++) {
6288 pkt->u.raw.signature = ATIO_PROCESSED;
6295 * qlt_24xx_process_atio_queue() - Process ATIO queue entries.
6296 * @ha: SCSI driver HA context
6299 qlt_24xx_process_atio_queue(struct scsi_qla_host *vha, uint8_t ha_locked)
6301 struct qla_hw_data *ha = vha->hw;
6302 struct atio_from_isp *pkt;
6305 if (!ha->flags.fw_started)
6308 while ((ha->tgt.atio_ring_ptr->signature != ATIO_PROCESSED) ||
6309 fcpcmd_is_corrupted(ha->tgt.atio_ring_ptr)) {
6310 pkt = (struct atio_from_isp *)ha->tgt.atio_ring_ptr;
6311 cnt = pkt->u.raw.entry_count;
6313 if (unlikely(fcpcmd_is_corrupted(ha->tgt.atio_ring_ptr))) {
6315 * This packet is corrupted. The header + payload
6316 * can not be trusted. There is no point in passing
6319 ql_log(ql_log_warn, vha, 0xd03c,
6320 "corrupted fcp frame SID[%3phN] OXID[%04x] EXCG[%x] %64phN\n",
6321 pkt->u.isp24.fcp_hdr.s_id,
6322 be16_to_cpu(pkt->u.isp24.fcp_hdr.ox_id),
6323 le32_to_cpu(pkt->u.isp24.exchange_addr), pkt);
6325 adjust_corrupted_atio(pkt);
6326 qlt_send_term_exchange(vha, NULL, pkt, ha_locked, 0);
6328 qlt_24xx_atio_pkt_all_vps(vha,
6329 (struct atio_from_isp *)pkt, ha_locked);
6332 for (i = 0; i < cnt; i++) {
6333 ha->tgt.atio_ring_index++;
6334 if (ha->tgt.atio_ring_index == ha->tgt.atio_q_length) {
6335 ha->tgt.atio_ring_index = 0;
6336 ha->tgt.atio_ring_ptr = ha->tgt.atio_ring;
6338 ha->tgt.atio_ring_ptr++;
6340 pkt->u.raw.signature = ATIO_PROCESSED;
6341 pkt = (struct atio_from_isp *)ha->tgt.atio_ring_ptr;
6346 /* Adjust ring index */
6347 WRT_REG_DWORD(ISP_ATIO_Q_OUT(vha), ha->tgt.atio_ring_index);
6348 RD_REG_DWORD_RELAXED(ISP_ATIO_Q_OUT(vha));
6352 qlt_24xx_config_rings(struct scsi_qla_host *vha)
6354 struct qla_hw_data *ha = vha->hw;
6355 if (!QLA_TGT_MODE_ENABLED())
6358 WRT_REG_DWORD(ISP_ATIO_Q_IN(vha), 0);
6359 WRT_REG_DWORD(ISP_ATIO_Q_OUT(vha), 0);
6360 RD_REG_DWORD(ISP_ATIO_Q_OUT(vha));
6362 if (IS_ATIO_MSIX_CAPABLE(ha)) {
6363 struct qla_msix_entry *msix = &ha->msix_entries[2];
6364 struct init_cb_24xx *icb = (struct init_cb_24xx *)ha->init_cb;
6366 icb->msix_atio = cpu_to_le16(msix->entry);
6367 ql_dbg(ql_dbg_init, vha, 0xf072,
6368 "Registering ICB vector 0x%x for atio que.\n",
6374 qlt_24xx_config_nvram_stage1(struct scsi_qla_host *vha, struct nvram_24xx *nv)
6376 struct qla_hw_data *ha = vha->hw;
6378 if (!QLA_TGT_MODE_ENABLED())
6381 if (qla_tgt_mode_enabled(vha) || qla_dual_mode_enabled(vha)) {
6382 if (!ha->tgt.saved_set) {
6383 /* We save only once */
6384 ha->tgt.saved_exchange_count = nv->exchange_count;
6385 ha->tgt.saved_firmware_options_1 =
6386 nv->firmware_options_1;
6387 ha->tgt.saved_firmware_options_2 =
6388 nv->firmware_options_2;
6389 ha->tgt.saved_firmware_options_3 =
6390 nv->firmware_options_3;
6391 ha->tgt.saved_set = 1;
6394 if (qla_tgt_mode_enabled(vha))
6395 nv->exchange_count = cpu_to_le16(0xFFFF);
6397 nv->exchange_count = cpu_to_le16(ql2xexchoffld);
6399 /* Enable target mode */
6400 nv->firmware_options_1 |= cpu_to_le32(BIT_4);
6402 /* Disable ini mode, if requested */
6403 if (qla_tgt_mode_enabled(vha))
6404 nv->firmware_options_1 |= cpu_to_le32(BIT_5);
6406 /* Disable Full Login after LIP */
6407 nv->firmware_options_1 &= cpu_to_le32(~BIT_13);
6408 /* Enable initial LIP */
6409 nv->firmware_options_1 &= cpu_to_le32(~BIT_9);
6410 if (ql2xtgt_tape_enable)
6411 /* Enable FC Tape support */
6412 nv->firmware_options_2 |= cpu_to_le32(BIT_12);
6414 /* Disable FC Tape support */
6415 nv->firmware_options_2 &= cpu_to_le32(~BIT_12);
6417 /* Disable Full Login after LIP */
6418 nv->host_p &= cpu_to_le32(~BIT_10);
6421 * clear BIT 15 explicitly as we have seen at least
6422 * a couple of instances where this was set and this
6423 * was causing the firmware to not be initialized.
6425 nv->firmware_options_1 &= cpu_to_le32(~BIT_15);
6426 /* Enable target PRLI control */
6427 nv->firmware_options_2 |= cpu_to_le32(BIT_14);
6429 if (ha->tgt.saved_set) {
6430 nv->exchange_count = ha->tgt.saved_exchange_count;
6431 nv->firmware_options_1 =
6432 ha->tgt.saved_firmware_options_1;
6433 nv->firmware_options_2 =
6434 ha->tgt.saved_firmware_options_2;
6435 nv->firmware_options_3 =
6436 ha->tgt.saved_firmware_options_3;
6441 if (ha->tgt.enable_class_2) {
6442 if (vha->flags.init_done)
6443 fc_host_supported_classes(vha->host) =
6444 FC_COS_CLASS2 | FC_COS_CLASS3;
6446 nv->firmware_options_2 |= cpu_to_le32(BIT_8);
6448 if (vha->flags.init_done)
6449 fc_host_supported_classes(vha->host) = FC_COS_CLASS3;
6451 nv->firmware_options_2 &= ~cpu_to_le32(BIT_8);
6456 qlt_24xx_config_nvram_stage2(struct scsi_qla_host *vha,
6457 struct init_cb_24xx *icb)
6459 struct qla_hw_data *ha = vha->hw;
6461 if (!QLA_TGT_MODE_ENABLED())
6464 if (ha->tgt.node_name_set) {
6465 memcpy(icb->node_name, ha->tgt.tgt_node_name, WWN_SIZE);
6466 icb->firmware_options_1 |= cpu_to_le32(BIT_14);
6469 /* disable ZIO at start time. */
6470 if (!vha->flags.init_done) {
6472 tmp = le32_to_cpu(icb->firmware_options_2);
6473 tmp &= ~(BIT_3 | BIT_2 | BIT_1 | BIT_0);
6474 icb->firmware_options_2 = cpu_to_le32(tmp);
6479 qlt_81xx_config_nvram_stage1(struct scsi_qla_host *vha, struct nvram_81xx *nv)
6481 struct qla_hw_data *ha = vha->hw;
6483 if (!QLA_TGT_MODE_ENABLED())
6486 if (qla_tgt_mode_enabled(vha) || qla_dual_mode_enabled(vha)) {
6487 if (!ha->tgt.saved_set) {
6488 /* We save only once */
6489 ha->tgt.saved_exchange_count = nv->exchange_count;
6490 ha->tgt.saved_firmware_options_1 =
6491 nv->firmware_options_1;
6492 ha->tgt.saved_firmware_options_2 =
6493 nv->firmware_options_2;
6494 ha->tgt.saved_firmware_options_3 =
6495 nv->firmware_options_3;
6496 ha->tgt.saved_set = 1;
6499 if (qla_tgt_mode_enabled(vha))
6500 nv->exchange_count = cpu_to_le16(0xFFFF);
6502 nv->exchange_count = cpu_to_le16(ql2xexchoffld);
6504 /* Enable target mode */
6505 nv->firmware_options_1 |= cpu_to_le32(BIT_4);
6507 /* Disable ini mode, if requested */
6508 if (qla_tgt_mode_enabled(vha))
6509 nv->firmware_options_1 |= cpu_to_le32(BIT_5);
6510 /* Disable Full Login after LIP */
6511 nv->firmware_options_1 &= cpu_to_le32(~BIT_13);
6512 /* Enable initial LIP */
6513 nv->firmware_options_1 &= cpu_to_le32(~BIT_9);
6515 * clear BIT 15 explicitly as we have seen at
6516 * least a couple of instances where this was set
6517 * and this was causing the firmware to not be
6520 nv->firmware_options_1 &= cpu_to_le32(~BIT_15);
6521 if (ql2xtgt_tape_enable)
6522 /* Enable FC tape support */
6523 nv->firmware_options_2 |= cpu_to_le32(BIT_12);
6525 /* Disable FC tape support */
6526 nv->firmware_options_2 &= cpu_to_le32(~BIT_12);
6528 /* Disable Full Login after LIP */
6529 nv->host_p &= cpu_to_le32(~BIT_10);
6530 /* Enable target PRLI control */
6531 nv->firmware_options_2 |= cpu_to_le32(BIT_14);
6533 if (ha->tgt.saved_set) {
6534 nv->exchange_count = ha->tgt.saved_exchange_count;
6535 nv->firmware_options_1 =
6536 ha->tgt.saved_firmware_options_1;
6537 nv->firmware_options_2 =
6538 ha->tgt.saved_firmware_options_2;
6539 nv->firmware_options_3 =
6540 ha->tgt.saved_firmware_options_3;
6545 if (ha->tgt.enable_class_2) {
6546 if (vha->flags.init_done)
6547 fc_host_supported_classes(vha->host) =
6548 FC_COS_CLASS2 | FC_COS_CLASS3;
6550 nv->firmware_options_2 |= cpu_to_le32(BIT_8);
6552 if (vha->flags.init_done)
6553 fc_host_supported_classes(vha->host) = FC_COS_CLASS3;
6555 nv->firmware_options_2 &= ~cpu_to_le32(BIT_8);
6560 qlt_81xx_config_nvram_stage2(struct scsi_qla_host *vha,
6561 struct init_cb_81xx *icb)
6563 struct qla_hw_data *ha = vha->hw;
6565 if (!QLA_TGT_MODE_ENABLED())
6568 if (ha->tgt.node_name_set) {
6569 memcpy(icb->node_name, ha->tgt.tgt_node_name, WWN_SIZE);
6570 icb->firmware_options_1 |= cpu_to_le32(BIT_14);
6573 /* disable ZIO at start time. */
6574 if (!vha->flags.init_done) {
6576 tmp = le32_to_cpu(icb->firmware_options_2);
6577 tmp &= ~(BIT_3 | BIT_2 | BIT_1 | BIT_0);
6578 icb->firmware_options_2 = cpu_to_le32(tmp);
6584 qlt_83xx_iospace_config(struct qla_hw_data *ha)
6586 if (!QLA_TGT_MODE_ENABLED())
6589 ha->msix_count += 1; /* For ATIO Q */
6594 qlt_modify_vp_config(struct scsi_qla_host *vha,
6595 struct vp_config_entry_24xx *vpmod)
6597 /* enable target mode. Bit5 = 1 => disable */
6598 if (qla_tgt_mode_enabled(vha) || qla_dual_mode_enabled(vha))
6599 vpmod->options_idx1 &= ~BIT_5;
6601 /* Disable ini mode, if requested. bit4 = 1 => disable */
6602 if (qla_tgt_mode_enabled(vha))
6603 vpmod->options_idx1 &= ~BIT_4;
6607 qlt_probe_one_stage1(struct scsi_qla_host *base_vha, struct qla_hw_data *ha)
6611 if (!QLA_TGT_MODE_ENABLED())
6614 if (ha->mqenable || IS_QLA83XX(ha) || IS_QLA27XX(ha)) {
6615 ISP_ATIO_Q_IN(base_vha) = &ha->mqiobase->isp25mq.atio_q_in;
6616 ISP_ATIO_Q_OUT(base_vha) = &ha->mqiobase->isp25mq.atio_q_out;
6618 ISP_ATIO_Q_IN(base_vha) = &ha->iobase->isp24.atio_q_in;
6619 ISP_ATIO_Q_OUT(base_vha) = &ha->iobase->isp24.atio_q_out;
6622 mutex_init(&base_vha->vha_tgt.tgt_mutex);
6623 mutex_init(&base_vha->vha_tgt.tgt_host_action_mutex);
6625 INIT_LIST_HEAD(&base_vha->unknown_atio_list);
6626 INIT_DELAYED_WORK(&base_vha->unknown_atio_work,
6627 qlt_unknown_atio_work_fn);
6629 qlt_clear_mode(base_vha);
6631 rc = btree_init32(&ha->tgt.host_map);
6633 ql_log(ql_log_info, base_vha, 0xd03d,
6634 "Unable to initialize ha->host_map btree\n");
6636 qlt_update_vp_map(base_vha, SET_VP_IDX);
6640 qla83xx_msix_atio_q(int irq, void *dev_id)
6642 struct rsp_que *rsp;
6643 scsi_qla_host_t *vha;
6644 struct qla_hw_data *ha;
6645 unsigned long flags;
6647 rsp = (struct rsp_que *) dev_id;
6649 vha = pci_get_drvdata(ha->pdev);
6651 spin_lock_irqsave(&ha->tgt.atio_lock, flags);
6653 qlt_24xx_process_atio_queue(vha, 0);
6655 spin_unlock_irqrestore(&ha->tgt.atio_lock, flags);
6661 qlt_handle_abts_recv_work(struct work_struct *work)
6663 struct qla_tgt_sess_op *op = container_of(work,
6664 struct qla_tgt_sess_op, work);
6665 scsi_qla_host_t *vha = op->vha;
6666 struct qla_hw_data *ha = vha->hw;
6667 unsigned long flags;
6669 if (qla2x00_reset_active(vha) || (op->chip_reset != ha->chip_reset))
6672 spin_lock_irqsave(&ha->tgt.atio_lock, flags);
6673 qlt_24xx_process_atio_queue(vha, 0);
6674 spin_unlock_irqrestore(&ha->tgt.atio_lock, flags);
6676 spin_lock_irqsave(&ha->hardware_lock, flags);
6677 qlt_response_pkt_all_vps(vha, (response_t *)&op->atio);
6678 spin_unlock_irqrestore(&ha->hardware_lock, flags);
6684 qlt_handle_abts_recv(struct scsi_qla_host *vha, response_t *pkt)
6686 struct qla_tgt_sess_op *op;
6688 op = kzalloc(sizeof(*op), GFP_ATOMIC);
6691 /* do not reach for ATIO queue here. This is best effort err
6692 * recovery at this point.
6694 qlt_response_pkt_all_vps(vha, pkt);
6698 memcpy(&op->atio, pkt, sizeof(*pkt));
6700 op->chip_reset = vha->hw->chip_reset;
6701 INIT_WORK(&op->work, qlt_handle_abts_recv_work);
6702 queue_work(qla_tgt_wq, &op->work);
6707 qlt_mem_alloc(struct qla_hw_data *ha)
6709 if (!QLA_TGT_MODE_ENABLED())
6712 ha->tgt.tgt_vp_map = kzalloc(sizeof(struct qla_tgt_vp_map) *
6713 MAX_MULTI_ID_FABRIC, GFP_KERNEL);
6714 if (!ha->tgt.tgt_vp_map)
6717 ha->tgt.atio_ring = dma_alloc_coherent(&ha->pdev->dev,
6718 (ha->tgt.atio_q_length + 1) * sizeof(struct atio_from_isp),
6719 &ha->tgt.atio_dma, GFP_KERNEL);
6720 if (!ha->tgt.atio_ring) {
6721 kfree(ha->tgt.tgt_vp_map);
6728 qlt_mem_free(struct qla_hw_data *ha)
6730 if (!QLA_TGT_MODE_ENABLED())
6733 if (ha->tgt.atio_ring) {
6734 dma_free_coherent(&ha->pdev->dev, (ha->tgt.atio_q_length + 1) *
6735 sizeof(struct atio_from_isp), ha->tgt.atio_ring,
6738 kfree(ha->tgt.tgt_vp_map);
6741 /* vport_slock to be held by the caller */
6743 qlt_update_vp_map(struct scsi_qla_host *vha, int cmd)
6749 if (!QLA_TGT_MODE_ENABLED())
6752 key = vha->d_id.b24;
6756 vha->hw->tgt.tgt_vp_map[vha->vp_idx].vha = vha;
6759 slot = btree_lookup32(&vha->hw->tgt.host_map, key);
6761 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf018,
6762 "Save vha in host_map %p %06x\n", vha, key);
6763 rc = btree_insert32(&vha->hw->tgt.host_map,
6764 key, vha, GFP_ATOMIC);
6766 ql_log(ql_log_info, vha, 0xd03e,
6767 "Unable to insert s_id into host_map: %06x\n",
6771 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf019,
6772 "replace existing vha in host_map %p %06x\n", vha, key);
6773 btree_update32(&vha->hw->tgt.host_map, key, vha);
6776 vha->hw->tgt.tgt_vp_map[vha->vp_idx].vha = NULL;
6779 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01a,
6780 "clear vha in host_map %p %06x\n", vha, key);
6781 slot = btree_lookup32(&vha->hw->tgt.host_map, key);
6783 btree_remove32(&vha->hw->tgt.host_map, key);
6789 void qlt_update_host_map(struct scsi_qla_host *vha, port_id_t id)
6791 unsigned long flags;
6792 struct qla_hw_data *ha = vha->hw;
6794 if (!vha->d_id.b24) {
6795 spin_lock_irqsave(&ha->vport_slock, flags);
6797 qlt_update_vp_map(vha, SET_AL_PA);
6798 spin_unlock_irqrestore(&ha->vport_slock, flags);
6799 } else if (vha->d_id.b24 != id.b24) {
6800 spin_lock_irqsave(&ha->vport_slock, flags);
6801 qlt_update_vp_map(vha, RESET_AL_PA);
6803 qlt_update_vp_map(vha, SET_AL_PA);
6804 spin_unlock_irqrestore(&ha->vport_slock, flags);
6808 static int __init qlt_parse_ini_mode(void)
6810 if (strcasecmp(qlini_mode, QLA2XXX_INI_MODE_STR_EXCLUSIVE) == 0)
6811 ql2x_ini_mode = QLA2XXX_INI_MODE_EXCLUSIVE;
6812 else if (strcasecmp(qlini_mode, QLA2XXX_INI_MODE_STR_DISABLED) == 0)
6813 ql2x_ini_mode = QLA2XXX_INI_MODE_DISABLED;
6814 else if (strcasecmp(qlini_mode, QLA2XXX_INI_MODE_STR_ENABLED) == 0)
6815 ql2x_ini_mode = QLA2XXX_INI_MODE_ENABLED;
6816 else if (strcasecmp(qlini_mode, QLA2XXX_INI_MODE_STR_DUAL) == 0)
6817 ql2x_ini_mode = QLA2XXX_INI_MODE_DUAL;
6824 int __init qlt_init(void)
6828 if (!qlt_parse_ini_mode()) {
6829 ql_log(ql_log_fatal, NULL, 0xe06b,
6830 "qlt_parse_ini_mode() failed\n");
6834 if (!QLA_TGT_MODE_ENABLED())
6837 qla_tgt_mgmt_cmd_cachep = kmem_cache_create("qla_tgt_mgmt_cmd_cachep",
6838 sizeof(struct qla_tgt_mgmt_cmd), __alignof__(struct
6839 qla_tgt_mgmt_cmd), 0, NULL);
6840 if (!qla_tgt_mgmt_cmd_cachep) {
6841 ql_log(ql_log_fatal, NULL, 0xd04b,
6842 "kmem_cache_create for qla_tgt_mgmt_cmd_cachep failed\n");
6846 qla_tgt_plogi_cachep = kmem_cache_create("qla_tgt_plogi_cachep",
6847 sizeof(struct qlt_plogi_ack_t), __alignof__(struct qlt_plogi_ack_t),
6850 if (!qla_tgt_plogi_cachep) {
6851 ql_log(ql_log_fatal, NULL, 0xe06d,
6852 "kmem_cache_create for qla_tgt_plogi_cachep failed\n");
6854 goto out_mgmt_cmd_cachep;
6857 qla_tgt_mgmt_cmd_mempool = mempool_create(25, mempool_alloc_slab,
6858 mempool_free_slab, qla_tgt_mgmt_cmd_cachep);
6859 if (!qla_tgt_mgmt_cmd_mempool) {
6860 ql_log(ql_log_fatal, NULL, 0xe06e,
6861 "mempool_create for qla_tgt_mgmt_cmd_mempool failed\n");
6863 goto out_plogi_cachep;
6866 qla_tgt_wq = alloc_workqueue("qla_tgt_wq", 0, 0);
6868 ql_log(ql_log_fatal, NULL, 0xe06f,
6869 "alloc_workqueue for qla_tgt_wq failed\n");
6871 goto out_cmd_mempool;
6874 * Return 1 to signal that initiator-mode is being disabled
6876 return (ql2x_ini_mode == QLA2XXX_INI_MODE_DISABLED) ? 1 : 0;
6879 mempool_destroy(qla_tgt_mgmt_cmd_mempool);
6881 kmem_cache_destroy(qla_tgt_plogi_cachep);
6882 out_mgmt_cmd_cachep:
6883 kmem_cache_destroy(qla_tgt_mgmt_cmd_cachep);
6889 if (!QLA_TGT_MODE_ENABLED())
6892 destroy_workqueue(qla_tgt_wq);
6893 mempool_destroy(qla_tgt_mgmt_cmd_mempool);
6894 kmem_cache_destroy(qla_tgt_plogi_cachep);
6895 kmem_cache_destroy(qla_tgt_mgmt_cmd_cachep);