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 = 50;
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_abort_cmd_on_host_reset(struct scsi_qla_host *vha,
119 struct qla_tgt_cmd *cmd);
120 static void qlt_alloc_qfull_cmd(struct scsi_qla_host *vha,
121 struct atio_from_isp *atio, uint16_t status, int qfull);
122 static void qlt_disable_vha(struct scsi_qla_host *vha);
123 static void qlt_clear_tgt_db(struct qla_tgt *tgt);
124 static void qlt_send_notify_ack(struct scsi_qla_host *vha,
125 struct imm_ntfy_from_isp *ntfy,
126 uint32_t add_flags, uint16_t resp_code, int resp_code_valid,
127 uint16_t srr_flags, uint16_t srr_reject_code, uint8_t srr_explan);
128 static void qlt_send_term_imm_notif(struct scsi_qla_host *vha,
129 struct imm_ntfy_from_isp *imm, int ha_locked);
130 static struct fc_port *qlt_create_sess(struct scsi_qla_host *vha,
131 fc_port_t *fcport, bool local);
132 void qlt_unreg_sess(struct fc_port *sess);
136 static struct kmem_cache *qla_tgt_mgmt_cmd_cachep;
137 static struct kmem_cache *qla_tgt_plogi_cachep;
138 static mempool_t *qla_tgt_mgmt_cmd_mempool;
139 static struct workqueue_struct *qla_tgt_wq;
140 static DEFINE_MUTEX(qla_tgt_mutex);
141 static LIST_HEAD(qla_tgt_glist);
143 /* This API intentionally takes dest as a parameter, rather than returning
144 * int value to avoid caller forgetting to issue wmb() after the store */
145 void qlt_do_generation_tick(struct scsi_qla_host *vha, int *dest)
147 scsi_qla_host_t *base_vha = pci_get_drvdata(vha->hw->pdev);
148 *dest = atomic_inc_return(&base_vha->generation_tick);
153 /* Might release hw lock, then reaquire!! */
154 static inline int qlt_issue_marker(struct scsi_qla_host *vha, int vha_locked)
156 /* Send marker if required */
157 if (unlikely(vha->marker_needed != 0)) {
158 int rc = qla2x00_issue_marker(vha, vha_locked);
159 if (rc != QLA_SUCCESS) {
160 ql_dbg(ql_dbg_tgt, vha, 0xe03d,
161 "qla_target(%d): issue_marker() failed\n",
170 struct scsi_qla_host *qlt_find_host_by_d_id(struct scsi_qla_host *vha,
173 struct qla_hw_data *ha = vha->hw;
176 if ((vha->d_id.b.area != d_id[1]) || (vha->d_id.b.domain != d_id[0]))
179 if (vha->d_id.b.al_pa == d_id[2])
182 BUG_ON(ha->tgt.tgt_vp_map == NULL);
183 vp_idx = ha->tgt.tgt_vp_map[d_id[2]].idx;
184 if (likely(test_bit(vp_idx, ha->vp_idx_map)))
185 return ha->tgt.tgt_vp_map[vp_idx].vha;
191 struct scsi_qla_host *qlt_find_host_by_vp_idx(struct scsi_qla_host *vha,
194 struct qla_hw_data *ha = vha->hw;
196 if (vha->vp_idx == vp_idx)
199 BUG_ON(ha->tgt.tgt_vp_map == NULL);
200 if (likely(test_bit(vp_idx, ha->vp_idx_map)))
201 return ha->tgt.tgt_vp_map[vp_idx].vha;
206 static inline void qlt_incr_num_pend_cmds(struct scsi_qla_host *vha)
210 spin_lock_irqsave(&vha->hw->tgt.q_full_lock, flags);
212 vha->hw->tgt.num_pend_cmds++;
213 if (vha->hw->tgt.num_pend_cmds > vha->qla_stats.stat_max_pend_cmds)
214 vha->qla_stats.stat_max_pend_cmds =
215 vha->hw->tgt.num_pend_cmds;
216 spin_unlock_irqrestore(&vha->hw->tgt.q_full_lock, flags);
218 static inline void qlt_decr_num_pend_cmds(struct scsi_qla_host *vha)
222 spin_lock_irqsave(&vha->hw->tgt.q_full_lock, flags);
223 vha->hw->tgt.num_pend_cmds--;
224 spin_unlock_irqrestore(&vha->hw->tgt.q_full_lock, flags);
227 static bool qlt_24xx_atio_pkt_all_vps(struct scsi_qla_host *vha,
228 struct atio_from_isp *atio, uint8_t ha_locked)
230 ql_dbg(ql_dbg_tgt, vha, 0xe072,
231 "%s: qla_target(%d): type %x ox_id %04x\n",
232 __func__, vha->vp_idx, atio->u.raw.entry_type,
233 be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id));
235 switch (atio->u.raw.entry_type) {
238 struct scsi_qla_host *host = qlt_find_host_by_d_id(vha,
239 atio->u.isp24.fcp_hdr.d_id);
240 if (unlikely(NULL == host)) {
241 ql_dbg(ql_dbg_tgt, vha, 0xe03e,
242 "qla_target(%d): Received ATIO_TYPE7 "
243 "with unknown d_id %x:%x:%x\n", vha->vp_idx,
244 atio->u.isp24.fcp_hdr.d_id[0],
245 atio->u.isp24.fcp_hdr.d_id[1],
246 atio->u.isp24.fcp_hdr.d_id[2]);
249 qlt_24xx_atio_pkt(host, atio, ha_locked);
253 case IMMED_NOTIFY_TYPE:
255 struct scsi_qla_host *host = vha;
256 struct imm_ntfy_from_isp *entry =
257 (struct imm_ntfy_from_isp *)atio;
259 if ((entry->u.isp24.vp_index != 0xFF) &&
260 (entry->u.isp24.nport_handle != 0xFFFF)) {
261 host = qlt_find_host_by_vp_idx(vha,
262 entry->u.isp24.vp_index);
263 if (unlikely(!host)) {
264 ql_dbg(ql_dbg_tgt, vha, 0xe03f,
265 "qla_target(%d): Received "
266 "ATIO (IMMED_NOTIFY_TYPE) "
267 "with unknown vp_index %d\n",
268 vha->vp_idx, entry->u.isp24.vp_index);
272 qlt_24xx_atio_pkt(host, atio, ha_locked);
277 ql_dbg(ql_dbg_tgt, vha, 0xe040,
278 "qla_target(%d): Received unknown ATIO atio "
279 "type %x\n", vha->vp_idx, atio->u.raw.entry_type);
286 void qlt_response_pkt_all_vps(struct scsi_qla_host *vha, response_t *pkt)
288 switch (pkt->entry_type) {
290 ql_dbg(ql_dbg_tgt, vha, 0xe073,
291 "qla_target(%d):%s: CRC2 Response pkt\n",
292 vha->vp_idx, __func__);
295 struct ctio7_from_24xx *entry = (struct ctio7_from_24xx *)pkt;
296 struct scsi_qla_host *host = qlt_find_host_by_vp_idx(vha,
298 if (unlikely(!host)) {
299 ql_dbg(ql_dbg_tgt, vha, 0xe041,
300 "qla_target(%d): Response pkt (CTIO_TYPE7) "
301 "received, with unknown vp_index %d\n",
302 vha->vp_idx, entry->vp_index);
305 qlt_response_pkt(host, pkt);
309 case IMMED_NOTIFY_TYPE:
311 struct scsi_qla_host *host = vha;
312 struct imm_ntfy_from_isp *entry =
313 (struct imm_ntfy_from_isp *)pkt;
315 host = qlt_find_host_by_vp_idx(vha, entry->u.isp24.vp_index);
316 if (unlikely(!host)) {
317 ql_dbg(ql_dbg_tgt, vha, 0xe042,
318 "qla_target(%d): Response pkt (IMMED_NOTIFY_TYPE) "
319 "received, with unknown vp_index %d\n",
320 vha->vp_idx, entry->u.isp24.vp_index);
323 qlt_response_pkt(host, pkt);
327 case NOTIFY_ACK_TYPE:
329 struct scsi_qla_host *host = vha;
330 struct nack_to_isp *entry = (struct nack_to_isp *)pkt;
332 if (0xFF != entry->u.isp24.vp_index) {
333 host = qlt_find_host_by_vp_idx(vha,
334 entry->u.isp24.vp_index);
335 if (unlikely(!host)) {
336 ql_dbg(ql_dbg_tgt, vha, 0xe043,
337 "qla_target(%d): Response "
338 "pkt (NOTIFY_ACK_TYPE) "
339 "received, with unknown "
340 "vp_index %d\n", vha->vp_idx,
341 entry->u.isp24.vp_index);
345 qlt_response_pkt(host, pkt);
351 struct abts_recv_from_24xx *entry =
352 (struct abts_recv_from_24xx *)pkt;
353 struct scsi_qla_host *host = qlt_find_host_by_vp_idx(vha,
355 if (unlikely(!host)) {
356 ql_dbg(ql_dbg_tgt, vha, 0xe044,
357 "qla_target(%d): Response pkt "
358 "(ABTS_RECV_24XX) received, with unknown "
359 "vp_index %d\n", vha->vp_idx, entry->vp_index);
362 qlt_response_pkt(host, pkt);
368 struct abts_resp_to_24xx *entry =
369 (struct abts_resp_to_24xx *)pkt;
370 struct scsi_qla_host *host = qlt_find_host_by_vp_idx(vha,
372 if (unlikely(!host)) {
373 ql_dbg(ql_dbg_tgt, vha, 0xe045,
374 "qla_target(%d): Response pkt "
375 "(ABTS_RECV_24XX) received, with unknown "
376 "vp_index %d\n", vha->vp_idx, entry->vp_index);
379 qlt_response_pkt(host, pkt);
384 qlt_response_pkt(vha, pkt);
391 * All qlt_plogi_ack_t operations are protected by hardware_lock
393 static int qla24xx_post_nack_work(struct scsi_qla_host *vha, fc_port_t *fcport,
394 struct imm_ntfy_from_isp *ntfy, int type)
396 struct qla_work_evt *e;
397 e = qla2x00_alloc_work(vha, QLA_EVT_NACK);
399 return QLA_FUNCTION_FAILED;
401 e->u.nack.fcport = fcport;
402 e->u.nack.type = type;
403 memcpy(e->u.nack.iocb, ntfy, sizeof(struct imm_ntfy_from_isp));
404 return qla2x00_post_work(vha, e);
408 void qla2x00_async_nack_sp_done(void *v, void *s, int res)
410 struct scsi_qla_host *vha = (struct scsi_qla_host *)v;
411 struct srb *sp = (struct srb *)s;
414 ql_dbg(ql_dbg_disc, vha, 0xffff,
415 "Async done-%s res %x %8phC type %d\n",
416 sp->name, res, sp->fcport->port_name, sp->type);
418 spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
419 sp->fcport->flags &= ~FCF_ASYNC_SENT;
420 sp->fcport->chip_reset = vha->hw->chip_reset;
424 sp->fcport->login_gen++;
425 sp->fcport->fw_login_state = DSC_LS_PLOGI_COMP;
426 sp->fcport->logout_on_delete = 1;
430 sp->fcport->fw_login_state = DSC_LS_PRLI_COMP;
431 sp->fcport->deleted = 0;
433 if (!sp->fcport->login_succ &&
434 !IS_SW_RESV_ADDR(sp->fcport->d_id)) {
435 sp->fcport->login_succ = 1;
439 if (!IS_IIDMA_CAPABLE(vha->hw) ||
440 !vha->hw->flags.gpsc_supported) {
441 ql_dbg(ql_dbg_disc, vha, 0xffff,
442 "%s %d %8phC post upd_fcport fcp_cnt %d\n",
444 sp->fcport->port_name,
447 qla24xx_post_upd_fcport_work(vha, sp->fcport);
449 ql_dbg(ql_dbg_disc, vha, 0xffff,
450 "%s %d %8phC post gpsc fcp_cnt %d\n",
452 sp->fcport->port_name,
455 qla24xx_post_gpsc_work(vha, sp->fcport);
461 sp->fcport->login_gen++;
462 sp->fcport->fw_login_state = DSC_LS_PORT_UNAVAIL;
463 qlt_logo_completion_handler(sp->fcport, MBS_COMMAND_COMPLETE);
466 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
471 int qla24xx_async_notify_ack(scsi_qla_host_t *vha, fc_port_t *fcport,
472 struct imm_ntfy_from_isp *ntfy, int type)
474 int rval = QLA_FUNCTION_FAILED;
478 fcport->flags |= FCF_ASYNC_SENT;
481 fcport->fw_login_state = DSC_LS_PLOGI_PEND;
485 fcport->fw_login_state = DSC_LS_PRLI_PEND;
489 fcport->fw_login_state = DSC_LS_LOGO_PEND;
494 sp = qla2x00_get_sp(vha, fcport, GFP_ATOMIC);
501 qla2x00_init_timer(sp, qla2x00_get_async_timeout(vha)+2);
503 sp->u.iocb_cmd.u.nack.ntfy = ntfy;
505 sp->done = qla2x00_async_nack_sp_done;
507 rval = qla2x00_start_sp(sp);
508 if (rval != QLA_SUCCESS)
511 ql_dbg(ql_dbg_disc, vha, 0xffff,
512 "Async-%s %8phC hndl %x %s\n",
513 sp->name, fcport->port_name, sp->handle, c);
520 fcport->flags &= ~FCF_ASYNC_SENT;
524 void qla24xx_do_nack_work(struct scsi_qla_host *vha, struct qla_work_evt *e)
529 switch (e->u.nack.type) {
531 mutex_lock(&vha->vha_tgt.tgt_mutex);
532 t = qlt_create_sess(vha, e->u.nack.fcport, 0);
533 mutex_unlock(&vha->vha_tgt.tgt_mutex);
535 ql_log(ql_log_info, vha, 0xffff,
536 "%s create sess success %p", __func__, t);
537 spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
538 /* create sess has an extra kref */
539 vha->hw->tgt.tgt_ops->put_sess(e->u.nack.fcport);
540 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
544 qla24xx_async_notify_ack(vha, e->u.nack.fcport,
545 (struct imm_ntfy_from_isp*)e->u.nack.iocb, e->u.nack.type);
548 void qla24xx_delete_sess_fn(struct work_struct *work)
550 fc_port_t *fcport = container_of(work, struct fc_port, del_work);
551 struct qla_hw_data *ha = fcport->vha->hw;
554 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
556 if (fcport->se_sess) {
557 ha->tgt.tgt_ops->shutdown_sess(fcport);
558 ha->tgt.tgt_ops->put_sess(fcport);
560 qlt_unreg_sess(fcport);
562 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
566 * Called from qla2x00_reg_remote_port()
568 void qlt_fc_port_added(struct scsi_qla_host *vha, fc_port_t *fcport)
570 struct qla_hw_data *ha = vha->hw;
571 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
572 struct fc_port *sess = fcport;
575 if (!vha->hw->tgt.tgt_ops)
578 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
580 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
584 if (fcport->disc_state == DSC_DELETE_PEND) {
585 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
589 if (!sess->se_sess) {
590 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
592 mutex_lock(&vha->vha_tgt.tgt_mutex);
593 sess = qlt_create_sess(vha, fcport, false);
594 mutex_unlock(&vha->vha_tgt.tgt_mutex);
596 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
598 if (fcport->fw_login_state == DSC_LS_PRLI_COMP) {
599 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
603 if (!kref_get_unless_zero(&sess->sess_kref)) {
604 ql_dbg(ql_dbg_disc, vha, 0xffff,
605 "%s: kref_get fail sess %8phC \n",
606 __func__, sess->port_name);
607 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
611 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04c,
612 "qla_target(%u): %ssession for port %8phC "
613 "(loop ID %d) reappeared\n", vha->vp_idx,
614 sess->local ? "local " : "", sess->port_name, sess->loop_id);
616 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf007,
617 "Reappeared sess %p\n", sess);
619 ha->tgt.tgt_ops->update_sess(sess, fcport->d_id,
621 (fcport->flags & FCF_CONF_COMP_SUPPORTED));
624 if (sess && sess->local) {
625 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04d,
626 "qla_target(%u): local session for "
627 "port %8phC (loop ID %d) became global\n", vha->vp_idx,
628 fcport->port_name, sess->loop_id);
631 ha->tgt.tgt_ops->put_sess(sess);
632 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
636 * This is a zero-base ref-counting solution, since hardware_lock
637 * guarantees that ref_count is not modified concurrently.
638 * Upon successful return content of iocb is undefined
640 static struct qlt_plogi_ack_t *
641 qlt_plogi_ack_find_add(struct scsi_qla_host *vha, port_id_t *id,
642 struct imm_ntfy_from_isp *iocb)
644 struct qlt_plogi_ack_t *pla;
646 list_for_each_entry(pla, &vha->plogi_ack_list, list) {
647 if (pla->id.b24 == id->b24) {
648 qlt_send_term_imm_notif(vha, &pla->iocb, 1);
649 memcpy(&pla->iocb, iocb, sizeof(pla->iocb));
654 pla = kmem_cache_zalloc(qla_tgt_plogi_cachep, GFP_ATOMIC);
656 ql_dbg(ql_dbg_async, vha, 0x5088,
657 "qla_target(%d): Allocation of plogi_ack failed\n",
662 memcpy(&pla->iocb, iocb, sizeof(pla->iocb));
664 list_add_tail(&pla->list, &vha->plogi_ack_list);
669 void qlt_plogi_ack_unref(struct scsi_qla_host *vha,
670 struct qlt_plogi_ack_t *pla)
672 struct imm_ntfy_from_isp *iocb = &pla->iocb;
675 fc_port_t *fcport = pla->fcport;
677 BUG_ON(!pla->ref_count);
683 ql_dbg(ql_dbg_disc, vha, 0x5089,
684 "Sending PLOGI ACK to wwn %8phC s_id %02x:%02x:%02x loop_id %#04x"
685 " exch %#x ox_id %#x\n", iocb->u.isp24.port_name,
686 iocb->u.isp24.port_id[2], iocb->u.isp24.port_id[1],
687 iocb->u.isp24.port_id[0],
688 le16_to_cpu(iocb->u.isp24.nport_handle),
689 iocb->u.isp24.exchange_address, iocb->ox_id);
691 port_id.b.domain = iocb->u.isp24.port_id[2];
692 port_id.b.area = iocb->u.isp24.port_id[1];
693 port_id.b.al_pa = iocb->u.isp24.port_id[0];
694 port_id.b.rsvd_1 = 0;
696 loop_id = le16_to_cpu(iocb->u.isp24.nport_handle);
698 fcport->loop_id = loop_id;
699 fcport->d_id = port_id;
700 qla24xx_post_nack_work(vha, fcport, iocb, SRB_NACK_PLOGI);
702 list_for_each_entry(fcport, &vha->vp_fcports, list) {
703 if (fcport->plogi_link[QLT_PLOGI_LINK_SAME_WWN] == pla)
704 fcport->plogi_link[QLT_PLOGI_LINK_SAME_WWN] = NULL;
705 if (fcport->plogi_link[QLT_PLOGI_LINK_CONFLICT] == pla)
706 fcport->plogi_link[QLT_PLOGI_LINK_CONFLICT] = NULL;
709 list_del(&pla->list);
710 kmem_cache_free(qla_tgt_plogi_cachep, pla);
714 qlt_plogi_ack_link(struct scsi_qla_host *vha, struct qlt_plogi_ack_t *pla,
715 struct fc_port *sess, enum qlt_plogi_link_t link)
717 struct imm_ntfy_from_isp *iocb = &pla->iocb;
718 /* Inc ref_count first because link might already be pointing at pla */
721 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf097,
722 "Linking sess %p [%d] wwn %8phC with PLOGI ACK to wwn %8phC"
723 " s_id %02x:%02x:%02x, ref=%d pla %p link %d\n",
724 sess, link, sess->port_name,
725 iocb->u.isp24.port_name, iocb->u.isp24.port_id[2],
726 iocb->u.isp24.port_id[1], iocb->u.isp24.port_id[0],
727 pla->ref_count, pla, link);
729 if (sess->plogi_link[link])
730 qlt_plogi_ack_unref(vha, sess->plogi_link[link]);
732 if (link == QLT_PLOGI_LINK_SAME_WWN)
735 sess->plogi_link[link] = pla;
739 /* These fields must be initialized by the caller */
742 * number of cmds dropped while we were waiting for
743 * initiator to ack LOGO initialize to 1 if LOGO is
744 * triggered by a command, otherwise, to 0
748 /* These fields are used by callee */
749 struct list_head list;
753 qlt_send_first_logo(struct scsi_qla_host *vha, qlt_port_logo_t *logo)
755 qlt_port_logo_t *tmp;
758 mutex_lock(&vha->vha_tgt.tgt_mutex);
760 list_for_each_entry(tmp, &vha->logo_list, list) {
761 if (tmp->id.b24 == logo->id.b24) {
762 tmp->cmd_count += logo->cmd_count;
763 mutex_unlock(&vha->vha_tgt.tgt_mutex);
768 list_add_tail(&logo->list, &vha->logo_list);
770 mutex_unlock(&vha->vha_tgt.tgt_mutex);
772 res = qla24xx_els_dcmd_iocb(vha, ELS_DCMD_LOGO, logo->id);
774 mutex_lock(&vha->vha_tgt.tgt_mutex);
775 list_del(&logo->list);
776 mutex_unlock(&vha->vha_tgt.tgt_mutex);
778 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf098,
779 "Finished LOGO to %02x:%02x:%02x, dropped %d cmds, res = %#x\n",
780 logo->id.b.domain, logo->id.b.area, logo->id.b.al_pa,
781 logo->cmd_count, res);
784 static void qlt_free_session_done(struct work_struct *work)
786 struct fc_port *sess = container_of(work, struct fc_port,
788 struct qla_tgt *tgt = sess->tgt;
789 struct scsi_qla_host *vha = sess->vha;
790 struct qla_hw_data *ha = vha->hw;
792 bool logout_started = false;
795 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf084,
796 "%s: se_sess %p / sess %p from port %8phC loop_id %#04x"
797 " s_id %02x:%02x:%02x logout %d keep %d els_logo %d\n",
798 __func__, sess->se_sess, sess, sess->port_name, sess->loop_id,
799 sess->d_id.b.domain, sess->d_id.b.area, sess->d_id.b.al_pa,
800 sess->logout_on_delete, sess->keep_nport_handle,
801 sess->send_els_logo);
804 if (!IS_SW_RESV_ADDR(sess->d_id)) {
805 if (sess->send_els_logo) {
806 qlt_port_logo_t logo;
807 logo.id = sess->d_id;
809 qlt_send_first_logo(vha, &logo);
812 if (sess->logout_on_delete) {
814 rc = qla2x00_post_async_logout_work(vha, sess, NULL);
815 if (rc != QLA_SUCCESS)
816 ql_log(ql_log_warn, vha, 0xf085,
817 "Schedule logo failed sess %p rc %d\n",
820 logout_started = true;
825 * Release the target session for FC Nexus from fabric module code.
827 if (sess->se_sess != NULL)
828 ha->tgt.tgt_ops->free_session(sess);
830 if (logout_started) {
833 while (!ACCESS_ONCE(sess->logout_completed)) {
835 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf086,
836 "%s: waiting for sess %p logout\n",
843 ql_dbg(ql_dbg_disc, vha, 0xf087,
844 "%s: sess %p logout completed\n",
848 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
850 sess->se_sess = NULL;
851 if (tgt && !IS_SW_RESV_ADDR(sess->d_id))
855 sess->disc_state = DSC_DELETED;
856 sess->fw_login_state = DSC_LS_PORT_UNAVAIL;
857 sess->deleted = QLA_SESS_DELETED;
858 sess->login_retry = vha->hw->login_retry_count;
860 if (sess->login_succ && !IS_SW_RESV_ADDR(sess->d_id)) {
862 sess->login_succ = 0;
865 if (sess->chip_reset != sess->vha->hw->chip_reset)
866 qla2x00_clear_loop_id(sess);
868 if (sess->conflict) {
869 sess->conflict->login_pause = 0;
870 sess->conflict = NULL;
871 if (!test_bit(UNLOADING, &vha->dpc_flags))
872 set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
876 struct qlt_plogi_ack_t *own =
877 sess->plogi_link[QLT_PLOGI_LINK_SAME_WWN];
878 struct qlt_plogi_ack_t *con =
879 sess->plogi_link[QLT_PLOGI_LINK_CONFLICT];
880 struct imm_ntfy_from_isp *iocb;
884 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf099,
885 "se_sess %p / sess %p port %8phC is gone,"
886 " %s (ref=%d), releasing PLOGI for %8phC (ref=%d)\n",
887 sess->se_sess, sess, sess->port_name,
888 own ? "releasing own PLOGI" : "no own PLOGI pending",
889 own ? own->ref_count : -1,
890 iocb->u.isp24.port_name, con->ref_count);
891 qlt_plogi_ack_unref(vha, con);
892 sess->plogi_link[QLT_PLOGI_LINK_CONFLICT] = NULL;
894 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf09a,
895 "se_sess %p / sess %p port %8phC is gone, %s (ref=%d)\n",
896 sess->se_sess, sess, sess->port_name,
897 own ? "releasing own PLOGI" :
898 "no own PLOGI pending",
899 own ? own->ref_count : -1);
903 sess->fw_login_state = DSC_LS_PLOGI_PEND;
904 qlt_plogi_ack_unref(vha, own);
905 sess->plogi_link[QLT_PLOGI_LINK_SAME_WWN] = NULL;
908 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
910 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf001,
911 "Unregistration of sess %p %8phC finished fcp_cnt %d\n",
912 sess, sess->port_name, vha->fcport_count);
914 if (tgt && (tgt->sess_count == 0))
915 wake_up_all(&tgt->waitQ);
917 if (vha->fcport_count == 0)
918 wake_up_all(&vha->fcport_waitQ);
920 if (!tgt || !tgt->tgt_stop) {
921 memset(&ea, 0, sizeof(ea));
922 ea.event = FCME_DELETE_DONE;
924 qla2x00_fcport_event_handler(vha, &ea);
928 /* ha->tgt.sess_lock supposed to be held on entry */
929 void qlt_unreg_sess(struct fc_port *sess)
931 struct scsi_qla_host *vha = sess->vha;
933 ql_dbg(ql_dbg_disc, sess->vha, 0xffff,
934 "%s sess %p for deletion %8phC\n",
935 __func__, sess, sess->port_name);
938 vha->hw->tgt.tgt_ops->clear_nacl_from_fcport_map(sess);
940 qla2x00_mark_device_lost(vha, sess, 1, 1);
942 sess->deleted = QLA_SESS_DELETION_IN_PROGRESS;
943 sess->disc_state = DSC_DELETE_PEND;
944 sess->last_rscn_gen = sess->rscn_gen;
945 sess->last_login_gen = sess->login_gen;
947 INIT_WORK(&sess->free_work, qlt_free_session_done);
948 schedule_work(&sess->free_work);
950 EXPORT_SYMBOL(qlt_unreg_sess);
952 static int qlt_reset(struct scsi_qla_host *vha, void *iocb, int mcmd)
954 struct qla_hw_data *ha = vha->hw;
955 struct fc_port *sess = NULL;
958 struct imm_ntfy_from_isp *n = (struct imm_ntfy_from_isp *)iocb;
961 loop_id = le16_to_cpu(n->u.isp24.nport_handle);
962 if (loop_id == 0xFFFF) {
964 atomic_inc(&vha->vha_tgt.qla_tgt->tgt_global_resets_count);
965 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
966 qlt_clear_tgt_db(vha->vha_tgt.qla_tgt);
967 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
969 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
970 sess = ha->tgt.tgt_ops->find_sess_by_loop_id(vha, loop_id);
971 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
974 ql_dbg(ql_dbg_tgt, vha, 0xe000,
975 "Using sess for qla_tgt_reset: %p\n", sess);
981 ql_dbg(ql_dbg_tgt, vha, 0xe047,
982 "scsi(%ld): resetting (session %p from port %8phC mcmd %x, "
983 "loop_id %d)\n", vha->host_no, sess, sess->port_name,
986 return qlt_issue_task_mgmt(sess, 0, mcmd, iocb, QLA24XX_MGMT_SEND_NACK);
989 static void qla24xx_chk_fcp_state(struct fc_port *sess)
991 if (sess->chip_reset != sess->vha->hw->chip_reset) {
992 sess->logout_on_delete = 0;
993 sess->logo_ack_needed = 0;
994 sess->fw_login_state = DSC_LS_PORT_UNAVAIL;
995 sess->scan_state = 0;
999 /* ha->tgt.sess_lock supposed to be held on entry */
1000 void qlt_schedule_sess_for_deletion(struct fc_port *sess,
1003 struct qla_tgt *tgt = sess->tgt;
1005 if (sess->disc_state == DSC_DELETE_PEND)
1008 if (sess->disc_state == DSC_DELETED) {
1009 if (tgt && tgt->tgt_stop && (tgt->sess_count == 0))
1010 wake_up_all(&tgt->waitQ);
1011 if (sess->vha->fcport_count == 0)
1012 wake_up_all(&sess->vha->fcport_waitQ);
1014 if (!sess->plogi_link[QLT_PLOGI_LINK_SAME_WWN] &&
1015 !sess->plogi_link[QLT_PLOGI_LINK_CONFLICT])
1019 sess->disc_state = DSC_DELETE_PEND;
1021 if (sess->deleted == QLA_SESS_DELETED)
1022 sess->logout_on_delete = 0;
1024 sess->deleted = QLA_SESS_DELETION_IN_PROGRESS;
1025 qla24xx_chk_fcp_state(sess);
1027 ql_dbg(ql_dbg_tgt, sess->vha, 0xe001,
1028 "Scheduling sess %p for deletion\n", sess);
1030 schedule_work(&sess->del_work);
1033 void qlt_schedule_sess_for_deletion_lock(struct fc_port *sess)
1035 unsigned long flags;
1036 struct qla_hw_data *ha = sess->vha->hw;
1037 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
1038 qlt_schedule_sess_for_deletion(sess, 1);
1039 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
1042 /* ha->tgt.sess_lock supposed to be held on entry */
1043 static void qlt_clear_tgt_db(struct qla_tgt *tgt)
1045 struct fc_port *sess;
1046 scsi_qla_host_t *vha = tgt->vha;
1048 list_for_each_entry(sess, &vha->vp_fcports, list) {
1050 qlt_schedule_sess_for_deletion(sess, 1);
1053 /* At this point tgt could be already dead */
1056 static int qla24xx_get_loop_id(struct scsi_qla_host *vha, const uint8_t *s_id,
1059 struct qla_hw_data *ha = vha->hw;
1060 dma_addr_t gid_list_dma;
1061 struct gid_list_info *gid_list;
1066 gid_list = dma_alloc_coherent(&ha->pdev->dev, qla2x00_gid_list_size(ha),
1067 &gid_list_dma, GFP_KERNEL);
1069 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf044,
1070 "qla_target(%d): DMA Alloc failed of %u\n",
1071 vha->vp_idx, qla2x00_gid_list_size(ha));
1075 /* Get list of logged in devices */
1076 rc = qla2x00_get_id_list(vha, gid_list, gid_list_dma, &entries);
1077 if (rc != QLA_SUCCESS) {
1078 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf045,
1079 "qla_target(%d): get_id_list() failed: %x\n",
1082 goto out_free_id_list;
1085 id_iter = (char *)gid_list;
1087 for (i = 0; i < entries; i++) {
1088 struct gid_list_info *gid = (struct gid_list_info *)id_iter;
1089 if ((gid->al_pa == s_id[2]) &&
1090 (gid->area == s_id[1]) &&
1091 (gid->domain == s_id[0])) {
1092 *loop_id = le16_to_cpu(gid->loop_id);
1096 id_iter += ha->gid_list_info_size;
1100 dma_free_coherent(&ha->pdev->dev, qla2x00_gid_list_size(ha),
1101 gid_list, gid_list_dma);
1106 * Adds an extra ref to allow to drop hw lock after adding sess to the list.
1107 * Caller must put it.
1109 static struct fc_port *qlt_create_sess(
1110 struct scsi_qla_host *vha,
1114 struct qla_hw_data *ha = vha->hw;
1115 struct fc_port *sess = fcport;
1116 unsigned long flags;
1118 if (vha->vha_tgt.qla_tgt->tgt_stop)
1121 if (fcport->se_sess) {
1122 if (!kref_get_unless_zero(&sess->sess_kref)) {
1123 ql_dbg(ql_dbg_disc, vha, 0xffff,
1124 "%s: kref_get_unless_zero failed for %8phC\n",
1125 __func__, sess->port_name);
1130 sess->tgt = vha->vha_tgt.qla_tgt;
1131 sess->local = local;
1134 * Under normal circumstances we want to logout from firmware when
1135 * session eventually ends and release corresponding nport handle.
1136 * In the exception cases (e.g. when new PLOGI is waiting) corresponding
1137 * code will adjust these flags as necessary.
1139 sess->logout_on_delete = 1;
1140 sess->keep_nport_handle = 0;
1141 sess->logout_completed = 0;
1143 if (ha->tgt.tgt_ops->check_initiator_node_acl(vha,
1144 &fcport->port_name[0], sess) < 0) {
1145 ql_dbg(ql_dbg_tgt_mgt, vha, 0xffff,
1146 "(%d) %8phC check_initiator_node_acl failed\n",
1147 vha->vp_idx, fcport->port_name);
1150 kref_init(&fcport->sess_kref);
1152 * Take an extra reference to ->sess_kref here to handle
1153 * fc_port access across ->tgt.sess_lock reaquire.
1155 if (!kref_get_unless_zero(&sess->sess_kref)) {
1156 ql_dbg(ql_dbg_disc, vha, 0xffff,
1157 "%s: kref_get_unless_zero failed for %8phC\n",
1158 __func__, sess->port_name);
1162 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
1163 if (!IS_SW_RESV_ADDR(sess->d_id))
1164 vha->vha_tgt.qla_tgt->sess_count++;
1166 qlt_do_generation_tick(vha, &sess->generation);
1167 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
1170 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf006,
1171 "Adding sess %p se_sess %p to tgt %p sess_count %d\n",
1172 sess, sess->se_sess, vha->vha_tgt.qla_tgt,
1173 vha->vha_tgt.qla_tgt->sess_count);
1175 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04b,
1176 "qla_target(%d): %ssession for wwn %8phC (loop_id %d, "
1177 "s_id %x:%x:%x, confirmed completion %ssupported) added\n",
1178 vha->vp_idx, local ? "local " : "", fcport->port_name,
1179 fcport->loop_id, sess->d_id.b.domain, sess->d_id.b.area,
1180 sess->d_id.b.al_pa, sess->conf_compl_supported ? "" : "not ");
1186 * max_gen - specifies maximum session generation
1187 * at which this deletion requestion is still valid
1190 qlt_fc_port_deleted(struct scsi_qla_host *vha, fc_port_t *fcport, int max_gen)
1192 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
1193 struct fc_port *sess = fcport;
1194 unsigned long flags;
1196 if (!vha->hw->tgt.tgt_ops)
1202 spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
1203 if (tgt->tgt_stop) {
1204 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
1207 if (!sess->se_sess) {
1208 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
1212 if (max_gen - sess->generation < 0) {
1213 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
1214 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf092,
1215 "Ignoring stale deletion request for se_sess %p / sess %p"
1216 " for port %8phC, req_gen %d, sess_gen %d\n",
1217 sess->se_sess, sess, sess->port_name, max_gen,
1222 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf008, "qla_tgt_fc_port_deleted %p", sess);
1225 qlt_schedule_sess_for_deletion(sess, false);
1226 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
1229 static inline int test_tgt_sess_count(struct qla_tgt *tgt)
1231 struct qla_hw_data *ha = tgt->ha;
1232 unsigned long flags;
1235 * We need to protect against race, when tgt is freed before or
1238 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
1239 ql_dbg(ql_dbg_tgt, tgt->vha, 0xe002,
1240 "tgt %p, sess_count=%d\n",
1241 tgt, tgt->sess_count);
1242 res = (tgt->sess_count == 0);
1243 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
1248 /* Called by tcm_qla2xxx configfs code */
1249 int qlt_stop_phase1(struct qla_tgt *tgt)
1251 struct scsi_qla_host *vha = tgt->vha;
1252 struct qla_hw_data *ha = tgt->ha;
1253 unsigned long flags;
1255 mutex_lock(&qla_tgt_mutex);
1256 if (!vha->fc_vport) {
1257 struct Scsi_Host *sh = vha->host;
1258 struct fc_host_attrs *fc_host = shost_to_fc_host(sh);
1261 spin_lock_irqsave(sh->host_lock, flags);
1262 npiv_vports = (fc_host->npiv_vports_inuse);
1263 spin_unlock_irqrestore(sh->host_lock, flags);
1266 mutex_unlock(&qla_tgt_mutex);
1270 if (tgt->tgt_stop || tgt->tgt_stopped) {
1271 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04e,
1272 "Already in tgt->tgt_stop or tgt_stopped state\n");
1273 mutex_unlock(&qla_tgt_mutex);
1277 ql_dbg(ql_dbg_tgt, vha, 0xe003, "Stopping target for host %ld(%p)\n",
1280 * Mutex needed to sync with qla_tgt_fc_port_[added,deleted].
1281 * Lock is needed, because we still can get an incoming packet.
1283 mutex_lock(&vha->vha_tgt.tgt_mutex);
1284 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
1286 qlt_clear_tgt_db(tgt);
1287 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
1288 mutex_unlock(&vha->vha_tgt.tgt_mutex);
1289 mutex_unlock(&qla_tgt_mutex);
1291 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf009,
1292 "Waiting for sess works (tgt %p)", tgt);
1293 spin_lock_irqsave(&tgt->sess_work_lock, flags);
1294 while (!list_empty(&tgt->sess_works_list)) {
1295 spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
1296 flush_scheduled_work();
1297 spin_lock_irqsave(&tgt->sess_work_lock, flags);
1299 spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
1301 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00a,
1302 "Waiting for tgt %p: sess_count=%d\n", tgt, tgt->sess_count);
1304 wait_event(tgt->waitQ, test_tgt_sess_count(tgt));
1307 if (!ha->flags.host_shutting_down &&
1308 (qla_tgt_mode_enabled(vha) || qla_dual_mode_enabled(vha)))
1309 qlt_disable_vha(vha);
1311 /* Wait for sessions to clear out (just in case) */
1312 wait_event(tgt->waitQ, test_tgt_sess_count(tgt));
1315 EXPORT_SYMBOL(qlt_stop_phase1);
1317 /* Called by tcm_qla2xxx configfs code */
1318 void qlt_stop_phase2(struct qla_tgt *tgt)
1320 struct qla_hw_data *ha = tgt->ha;
1321 scsi_qla_host_t *vha = pci_get_drvdata(ha->pdev);
1322 unsigned long flags;
1324 if (tgt->tgt_stopped) {
1325 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04f,
1326 "Already in tgt->tgt_stopped state\n");
1331 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00b,
1332 "Waiting for %d IRQ commands to complete (tgt %p)",
1333 tgt->irq_cmd_count, tgt);
1335 mutex_lock(&vha->vha_tgt.tgt_mutex);
1336 spin_lock_irqsave(&ha->hardware_lock, flags);
1337 while ((tgt->irq_cmd_count != 0) || (tgt->atio_irq_cmd_count != 0)) {
1338 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1340 spin_lock_irqsave(&ha->hardware_lock, flags);
1343 tgt->tgt_stopped = 1;
1344 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1345 mutex_unlock(&vha->vha_tgt.tgt_mutex);
1347 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00c, "Stop of tgt %p finished",
1350 EXPORT_SYMBOL(qlt_stop_phase2);
1352 /* Called from qlt_remove_target() -> qla2x00_remove_one() */
1353 static void qlt_release(struct qla_tgt *tgt)
1355 scsi_qla_host_t *vha = tgt->vha;
1357 if ((vha->vha_tgt.qla_tgt != NULL) && !tgt->tgt_stopped)
1358 qlt_stop_phase2(tgt);
1360 vha->vha_tgt.qla_tgt = NULL;
1362 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00d,
1363 "Release of tgt %p finished\n", tgt);
1368 /* ha->hardware_lock supposed to be held on entry */
1369 static int qlt_sched_sess_work(struct qla_tgt *tgt, int type,
1370 const void *param, unsigned int param_size)
1372 struct qla_tgt_sess_work_param *prm;
1373 unsigned long flags;
1375 prm = kzalloc(sizeof(*prm), GFP_ATOMIC);
1377 ql_dbg(ql_dbg_tgt_mgt, tgt->vha, 0xf050,
1378 "qla_target(%d): Unable to create session "
1379 "work, command will be refused", 0);
1383 ql_dbg(ql_dbg_tgt_mgt, tgt->vha, 0xf00e,
1384 "Scheduling work (type %d, prm %p)"
1385 " to find session for param %p (size %d, tgt %p)\n",
1386 type, prm, param, param_size, tgt);
1389 memcpy(&prm->tm_iocb, param, param_size);
1391 spin_lock_irqsave(&tgt->sess_work_lock, flags);
1392 list_add_tail(&prm->sess_works_list_entry, &tgt->sess_works_list);
1393 spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
1395 schedule_work(&tgt->sess_work);
1401 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
1403 static void qlt_send_notify_ack(struct scsi_qla_host *vha,
1404 struct imm_ntfy_from_isp *ntfy,
1405 uint32_t add_flags, uint16_t resp_code, int resp_code_valid,
1406 uint16_t srr_flags, uint16_t srr_reject_code, uint8_t srr_explan)
1408 struct qla_hw_data *ha = vha->hw;
1410 struct nack_to_isp *nack;
1412 ql_dbg(ql_dbg_tgt, vha, 0xe004, "Sending NOTIFY_ACK (ha=%p)\n", ha);
1414 /* Send marker if required */
1415 if (qlt_issue_marker(vha, 1) != QLA_SUCCESS)
1418 pkt = (request_t *)qla2x00_alloc_iocbs(vha, NULL);
1420 ql_dbg(ql_dbg_tgt, vha, 0xe049,
1421 "qla_target(%d): %s failed: unable to allocate "
1422 "request packet\n", vha->vp_idx, __func__);
1426 if (vha->vha_tgt.qla_tgt != NULL)
1427 vha->vha_tgt.qla_tgt->notify_ack_expected++;
1429 pkt->entry_type = NOTIFY_ACK_TYPE;
1430 pkt->entry_count = 1;
1432 nack = (struct nack_to_isp *)pkt;
1433 nack->ox_id = ntfy->ox_id;
1435 nack->u.isp24.handle = QLA_TGT_SKIP_HANDLE;
1436 nack->u.isp24.nport_handle = ntfy->u.isp24.nport_handle;
1437 if (le16_to_cpu(ntfy->u.isp24.status) == IMM_NTFY_ELS) {
1438 nack->u.isp24.flags = ntfy->u.isp24.flags &
1439 cpu_to_le32(NOTIFY24XX_FLAGS_PUREX_IOCB);
1441 nack->u.isp24.srr_rx_id = ntfy->u.isp24.srr_rx_id;
1442 nack->u.isp24.status = ntfy->u.isp24.status;
1443 nack->u.isp24.status_subcode = ntfy->u.isp24.status_subcode;
1444 nack->u.isp24.fw_handle = ntfy->u.isp24.fw_handle;
1445 nack->u.isp24.exchange_address = ntfy->u.isp24.exchange_address;
1446 nack->u.isp24.srr_rel_offs = ntfy->u.isp24.srr_rel_offs;
1447 nack->u.isp24.srr_ui = ntfy->u.isp24.srr_ui;
1448 nack->u.isp24.srr_flags = cpu_to_le16(srr_flags);
1449 nack->u.isp24.srr_reject_code = srr_reject_code;
1450 nack->u.isp24.srr_reject_code_expl = srr_explan;
1451 nack->u.isp24.vp_index = ntfy->u.isp24.vp_index;
1453 ql_dbg(ql_dbg_tgt, vha, 0xe005,
1454 "qla_target(%d): Sending 24xx Notify Ack %d\n",
1455 vha->vp_idx, nack->u.isp24.status);
1457 /* Memory Barrier */
1459 qla2x00_start_iocbs(vha, vha->req);
1463 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
1465 static void qlt_24xx_send_abts_resp(struct scsi_qla_host *vha,
1466 struct abts_recv_from_24xx *abts, uint32_t status,
1469 struct qla_hw_data *ha = vha->hw;
1470 struct abts_resp_to_24xx *resp;
1474 ql_dbg(ql_dbg_tgt, vha, 0xe006,
1475 "Sending task mgmt ABTS response (ha=%p, atio=%p, status=%x\n",
1478 /* Send marker if required */
1479 if (qlt_issue_marker(vha, 1) != QLA_SUCCESS)
1482 resp = (struct abts_resp_to_24xx *)qla2x00_alloc_iocbs_ready(vha, NULL);
1484 ql_dbg(ql_dbg_tgt, vha, 0xe04a,
1485 "qla_target(%d): %s failed: unable to allocate "
1486 "request packet", vha->vp_idx, __func__);
1490 resp->entry_type = ABTS_RESP_24XX;
1491 resp->entry_count = 1;
1492 resp->nport_handle = abts->nport_handle;
1493 resp->vp_index = vha->vp_idx;
1494 resp->sof_type = abts->sof_type;
1495 resp->exchange_address = abts->exchange_address;
1496 resp->fcp_hdr_le = abts->fcp_hdr_le;
1497 f_ctl = cpu_to_le32(F_CTL_EXCH_CONTEXT_RESP |
1498 F_CTL_LAST_SEQ | F_CTL_END_SEQ |
1499 F_CTL_SEQ_INITIATIVE);
1500 p = (uint8_t *)&f_ctl;
1501 resp->fcp_hdr_le.f_ctl[0] = *p++;
1502 resp->fcp_hdr_le.f_ctl[1] = *p++;
1503 resp->fcp_hdr_le.f_ctl[2] = *p;
1505 resp->fcp_hdr_le.d_id[0] = abts->fcp_hdr_le.d_id[0];
1506 resp->fcp_hdr_le.d_id[1] = abts->fcp_hdr_le.d_id[1];
1507 resp->fcp_hdr_le.d_id[2] = abts->fcp_hdr_le.d_id[2];
1508 resp->fcp_hdr_le.s_id[0] = abts->fcp_hdr_le.s_id[0];
1509 resp->fcp_hdr_le.s_id[1] = abts->fcp_hdr_le.s_id[1];
1510 resp->fcp_hdr_le.s_id[2] = abts->fcp_hdr_le.s_id[2];
1512 resp->fcp_hdr_le.d_id[0] = abts->fcp_hdr_le.s_id[0];
1513 resp->fcp_hdr_le.d_id[1] = abts->fcp_hdr_le.s_id[1];
1514 resp->fcp_hdr_le.d_id[2] = abts->fcp_hdr_le.s_id[2];
1515 resp->fcp_hdr_le.s_id[0] = abts->fcp_hdr_le.d_id[0];
1516 resp->fcp_hdr_le.s_id[1] = abts->fcp_hdr_le.d_id[1];
1517 resp->fcp_hdr_le.s_id[2] = abts->fcp_hdr_le.d_id[2];
1519 resp->exchange_addr_to_abort = abts->exchange_addr_to_abort;
1520 if (status == FCP_TMF_CMPL) {
1521 resp->fcp_hdr_le.r_ctl = R_CTL_BASIC_LINK_SERV | R_CTL_B_ACC;
1522 resp->payload.ba_acct.seq_id_valid = SEQ_ID_INVALID;
1523 resp->payload.ba_acct.low_seq_cnt = 0x0000;
1524 resp->payload.ba_acct.high_seq_cnt = 0xFFFF;
1525 resp->payload.ba_acct.ox_id = abts->fcp_hdr_le.ox_id;
1526 resp->payload.ba_acct.rx_id = abts->fcp_hdr_le.rx_id;
1528 resp->fcp_hdr_le.r_ctl = R_CTL_BASIC_LINK_SERV | R_CTL_B_RJT;
1529 resp->payload.ba_rjt.reason_code =
1530 BA_RJT_REASON_CODE_UNABLE_TO_PERFORM;
1531 /* Other bytes are zero */
1534 vha->vha_tgt.qla_tgt->abts_resp_expected++;
1536 /* Memory Barrier */
1538 qla2x00_start_iocbs(vha, vha->req);
1542 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
1544 static void qlt_24xx_retry_term_exchange(struct scsi_qla_host *vha,
1545 struct abts_resp_from_24xx_fw *entry)
1547 struct ctio7_to_24xx *ctio;
1549 ql_dbg(ql_dbg_tgt, vha, 0xe007,
1550 "Sending retry TERM EXCH CTIO7 (ha=%p)\n", vha->hw);
1551 /* Send marker if required */
1552 if (qlt_issue_marker(vha, 1) != QLA_SUCCESS)
1555 ctio = (struct ctio7_to_24xx *)qla2x00_alloc_iocbs_ready(vha, NULL);
1557 ql_dbg(ql_dbg_tgt, vha, 0xe04b,
1558 "qla_target(%d): %s failed: unable to allocate "
1559 "request packet\n", vha->vp_idx, __func__);
1564 * We've got on entrance firmware's response on by us generated
1565 * ABTS response. So, in it ID fields are reversed.
1568 ctio->entry_type = CTIO_TYPE7;
1569 ctio->entry_count = 1;
1570 ctio->nport_handle = entry->nport_handle;
1571 ctio->handle = QLA_TGT_SKIP_HANDLE | CTIO_COMPLETION_HANDLE_MARK;
1572 ctio->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
1573 ctio->vp_index = vha->vp_idx;
1574 ctio->initiator_id[0] = entry->fcp_hdr_le.d_id[0];
1575 ctio->initiator_id[1] = entry->fcp_hdr_le.d_id[1];
1576 ctio->initiator_id[2] = entry->fcp_hdr_le.d_id[2];
1577 ctio->exchange_addr = entry->exchange_addr_to_abort;
1578 ctio->u.status1.flags = cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_1 |
1579 CTIO7_FLAGS_TERMINATE);
1580 ctio->u.status1.ox_id = cpu_to_le16(entry->fcp_hdr_le.ox_id);
1582 /* Memory Barrier */
1584 qla2x00_start_iocbs(vha, vha->req);
1586 qlt_24xx_send_abts_resp(vha, (struct abts_recv_from_24xx *)entry,
1587 FCP_TMF_CMPL, true);
1590 static int abort_cmd_for_tag(struct scsi_qla_host *vha, uint32_t tag)
1592 struct qla_tgt_sess_op *op;
1593 struct qla_tgt_cmd *cmd;
1595 spin_lock(&vha->cmd_list_lock);
1597 list_for_each_entry(op, &vha->qla_sess_op_cmd_list, cmd_list) {
1598 if (tag == op->atio.u.isp24.exchange_addr) {
1600 spin_unlock(&vha->cmd_list_lock);
1605 list_for_each_entry(cmd, &vha->qla_cmd_list, cmd_list) {
1606 if (tag == cmd->atio.u.isp24.exchange_addr) {
1608 spin_unlock(&vha->cmd_list_lock);
1613 spin_unlock(&vha->cmd_list_lock);
1617 /* drop cmds for the given lun
1618 * XXX only looks for cmds on the port through which lun reset was recieved
1619 * XXX does not go through the list of other port (which may have cmds
1622 static void abort_cmds_for_lun(struct scsi_qla_host *vha,
1623 uint32_t lun, uint8_t *s_id)
1625 struct qla_tgt_sess_op *op;
1626 struct qla_tgt_cmd *cmd;
1629 key = sid_to_key(s_id);
1630 spin_lock(&vha->cmd_list_lock);
1631 list_for_each_entry(op, &vha->qla_sess_op_cmd_list, cmd_list) {
1635 op_key = sid_to_key(op->atio.u.isp24.fcp_hdr.s_id);
1636 op_lun = scsilun_to_int(
1637 (struct scsi_lun *)&op->atio.u.isp24.fcp_cmnd.lun);
1638 if (op_key == key && op_lun == lun)
1641 list_for_each_entry(cmd, &vha->qla_cmd_list, cmd_list) {
1645 cmd_key = sid_to_key(cmd->atio.u.isp24.fcp_hdr.s_id);
1646 cmd_lun = scsilun_to_int(
1647 (struct scsi_lun *)&cmd->atio.u.isp24.fcp_cmnd.lun);
1648 if (cmd_key == key && cmd_lun == lun)
1651 spin_unlock(&vha->cmd_list_lock);
1654 /* ha->hardware_lock supposed to be held on entry */
1655 static int __qlt_24xx_handle_abts(struct scsi_qla_host *vha,
1656 struct abts_recv_from_24xx *abts, struct fc_port *sess)
1658 struct qla_hw_data *ha = vha->hw;
1659 struct se_session *se_sess = sess->se_sess;
1660 struct qla_tgt_mgmt_cmd *mcmd;
1661 struct se_cmd *se_cmd;
1664 bool found_lun = false;
1665 unsigned long flags;
1667 spin_lock_irqsave(&se_sess->sess_cmd_lock, flags);
1668 list_for_each_entry(se_cmd, &se_sess->sess_cmd_list, se_cmd_list) {
1669 struct qla_tgt_cmd *cmd =
1670 container_of(se_cmd, struct qla_tgt_cmd, se_cmd);
1671 if (se_cmd->tag == abts->exchange_addr_to_abort) {
1672 lun = cmd->unpacked_lun;
1677 spin_unlock_irqrestore(&se_sess->sess_cmd_lock, flags);
1679 /* cmd not in LIO lists, look in qla list */
1681 if (abort_cmd_for_tag(vha, abts->exchange_addr_to_abort)) {
1682 /* send TASK_ABORT response immediately */
1683 qlt_24xx_send_abts_resp(vha, abts, FCP_TMF_CMPL, false);
1686 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf081,
1687 "unable to find cmd in driver or LIO for tag 0x%x\n",
1688 abts->exchange_addr_to_abort);
1693 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00f,
1694 "qla_target(%d): task abort (tag=%d)\n",
1695 vha->vp_idx, abts->exchange_addr_to_abort);
1697 mcmd = mempool_alloc(qla_tgt_mgmt_cmd_mempool, GFP_ATOMIC);
1699 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf051,
1700 "qla_target(%d): %s: Allocation of ABORT cmd failed",
1701 vha->vp_idx, __func__);
1704 memset(mcmd, 0, sizeof(*mcmd));
1707 memcpy(&mcmd->orig_iocb.abts, abts, sizeof(mcmd->orig_iocb.abts));
1708 mcmd->reset_count = vha->hw->chip_reset;
1709 mcmd->tmr_func = QLA_TGT_ABTS;
1711 rc = ha->tgt.tgt_ops->handle_tmr(mcmd, lun, mcmd->tmr_func,
1712 abts->exchange_addr_to_abort);
1714 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf052,
1715 "qla_target(%d): tgt_ops->handle_tmr()"
1716 " failed: %d", vha->vp_idx, rc);
1717 mempool_free(mcmd, qla_tgt_mgmt_cmd_mempool);
1725 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
1727 static void qlt_24xx_handle_abts(struct scsi_qla_host *vha,
1728 struct abts_recv_from_24xx *abts)
1730 struct qla_hw_data *ha = vha->hw;
1731 struct fc_port *sess;
1732 uint32_t tag = abts->exchange_addr_to_abort;
1735 unsigned long flags;
1737 if (le32_to_cpu(abts->fcp_hdr_le.parameter) & ABTS_PARAM_ABORT_SEQ) {
1738 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf053,
1739 "qla_target(%d): ABTS: Abort Sequence not "
1740 "supported\n", vha->vp_idx);
1741 qlt_24xx_send_abts_resp(vha, abts, FCP_TMF_REJECTED, false);
1745 if (tag == ATIO_EXCHANGE_ADDRESS_UNKNOWN) {
1746 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf010,
1747 "qla_target(%d): ABTS: Unknown Exchange "
1748 "Address received\n", vha->vp_idx);
1749 qlt_24xx_send_abts_resp(vha, abts, FCP_TMF_REJECTED, false);
1753 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf011,
1754 "qla_target(%d): task abort (s_id=%x:%x:%x, "
1755 "tag=%d, param=%x)\n", vha->vp_idx, abts->fcp_hdr_le.s_id[2],
1756 abts->fcp_hdr_le.s_id[1], abts->fcp_hdr_le.s_id[0], tag,
1757 le32_to_cpu(abts->fcp_hdr_le.parameter));
1759 s_id[0] = abts->fcp_hdr_le.s_id[2];
1760 s_id[1] = abts->fcp_hdr_le.s_id[1];
1761 s_id[2] = abts->fcp_hdr_le.s_id[0];
1763 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
1764 sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha, s_id);
1766 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf012,
1767 "qla_target(%d): task abort for non-existant session\n",
1769 rc = qlt_sched_sess_work(vha->vha_tgt.qla_tgt,
1770 QLA_TGT_SESS_WORK_ABORT, abts, sizeof(*abts));
1772 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
1775 qlt_24xx_send_abts_resp(vha, abts, FCP_TMF_REJECTED,
1780 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
1783 if (sess->deleted) {
1784 qlt_24xx_send_abts_resp(vha, abts, FCP_TMF_REJECTED, false);
1788 rc = __qlt_24xx_handle_abts(vha, abts, sess);
1790 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf054,
1791 "qla_target(%d): __qlt_24xx_handle_abts() failed: %d\n",
1793 qlt_24xx_send_abts_resp(vha, abts, FCP_TMF_REJECTED, false);
1799 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
1801 static void qlt_24xx_send_task_mgmt_ctio(struct scsi_qla_host *ha,
1802 struct qla_tgt_mgmt_cmd *mcmd, uint32_t resp_code)
1804 struct atio_from_isp *atio = &mcmd->orig_iocb.atio;
1805 struct ctio7_to_24xx *ctio;
1808 ql_dbg(ql_dbg_tgt, ha, 0xe008,
1809 "Sending task mgmt CTIO7 (ha=%p, atio=%p, resp_code=%x\n",
1810 ha, atio, resp_code);
1812 /* Send marker if required */
1813 if (qlt_issue_marker(ha, 1) != QLA_SUCCESS)
1816 ctio = (struct ctio7_to_24xx *)qla2x00_alloc_iocbs(ha, NULL);
1818 ql_dbg(ql_dbg_tgt, ha, 0xe04c,
1819 "qla_target(%d): %s failed: unable to allocate "
1820 "request packet\n", ha->vp_idx, __func__);
1824 ctio->entry_type = CTIO_TYPE7;
1825 ctio->entry_count = 1;
1826 ctio->handle = QLA_TGT_SKIP_HANDLE | CTIO_COMPLETION_HANDLE_MARK;
1827 ctio->nport_handle = mcmd->sess->loop_id;
1828 ctio->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
1829 ctio->vp_index = ha->vp_idx;
1830 ctio->initiator_id[0] = atio->u.isp24.fcp_hdr.s_id[2];
1831 ctio->initiator_id[1] = atio->u.isp24.fcp_hdr.s_id[1];
1832 ctio->initiator_id[2] = atio->u.isp24.fcp_hdr.s_id[0];
1833 ctio->exchange_addr = atio->u.isp24.exchange_addr;
1834 ctio->u.status1.flags = (atio->u.isp24.attr << 9) |
1835 cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_1 | CTIO7_FLAGS_SEND_STATUS);
1836 temp = be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id);
1837 ctio->u.status1.ox_id = cpu_to_le16(temp);
1838 ctio->u.status1.scsi_status =
1839 cpu_to_le16(SS_RESPONSE_INFO_LEN_VALID);
1840 ctio->u.status1.response_len = cpu_to_le16(8);
1841 ctio->u.status1.sense_data[0] = resp_code;
1843 /* Memory Barrier */
1845 qla2x00_start_iocbs(ha, ha->req);
1848 void qlt_free_mcmd(struct qla_tgt_mgmt_cmd *mcmd)
1850 mempool_free(mcmd, qla_tgt_mgmt_cmd_mempool);
1852 EXPORT_SYMBOL(qlt_free_mcmd);
1854 /* callback from target fabric module code */
1855 void qlt_xmit_tm_rsp(struct qla_tgt_mgmt_cmd *mcmd)
1857 struct scsi_qla_host *vha = mcmd->sess->vha;
1858 struct qla_hw_data *ha = vha->hw;
1859 unsigned long flags;
1861 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf013,
1862 "TM response mcmd (%p) status %#x state %#x",
1863 mcmd, mcmd->fc_tm_rsp, mcmd->flags);
1865 spin_lock_irqsave(&ha->hardware_lock, flags);
1867 if (!vha->flags.online || mcmd->reset_count != ha->chip_reset) {
1869 * Either the port is not online or this request was from
1870 * previous life, just abort the processing.
1872 ql_dbg(ql_dbg_async, vha, 0xe100,
1873 "RESET-TMR online/active/old-count/new-count = %d/%d/%d/%d.\n",
1874 vha->flags.online, qla2x00_reset_active(vha),
1875 mcmd->reset_count, ha->chip_reset);
1876 ha->tgt.tgt_ops->free_mcmd(mcmd);
1877 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1881 if (mcmd->flags == QLA24XX_MGMT_SEND_NACK) {
1882 if (mcmd->orig_iocb.imm_ntfy.u.isp24.status_subcode ==
1884 ql_dbg(ql_dbg_disc, vha, 0xffff,
1885 "TM response logo %phC status %#x state %#x",
1886 mcmd->sess->port_name, mcmd->fc_tm_rsp,
1888 qlt_schedule_sess_for_deletion_lock(mcmd->sess);
1890 qlt_send_notify_ack(vha, &mcmd->orig_iocb.imm_ntfy,
1894 if (mcmd->orig_iocb.atio.u.raw.entry_type == ABTS_RECV_24XX)
1895 qlt_24xx_send_abts_resp(vha, &mcmd->orig_iocb.abts,
1896 mcmd->fc_tm_rsp, false);
1898 qlt_24xx_send_task_mgmt_ctio(vha, mcmd,
1902 * Make the callback for ->free_mcmd() to queue_work() and invoke
1903 * target_put_sess_cmd() to drop cmd_kref to 1. The final
1904 * target_put_sess_cmd() call will be made from TFO->check_stop_free()
1905 * -> tcm_qla2xxx_check_stop_free() to release the TMR associated se_cmd
1906 * descriptor after TFO->queue_tm_rsp() -> tcm_qla2xxx_queue_tm_rsp() ->
1907 * qlt_xmit_tm_rsp() returns here..
1909 ha->tgt.tgt_ops->free_mcmd(mcmd);
1910 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1912 EXPORT_SYMBOL(qlt_xmit_tm_rsp);
1915 static int qlt_pci_map_calc_cnt(struct qla_tgt_prm *prm)
1917 struct qla_tgt_cmd *cmd = prm->cmd;
1919 BUG_ON(cmd->sg_cnt == 0);
1921 prm->sg = (struct scatterlist *)cmd->sg;
1922 prm->seg_cnt = pci_map_sg(prm->tgt->ha->pdev, cmd->sg,
1923 cmd->sg_cnt, cmd->dma_data_direction);
1924 if (unlikely(prm->seg_cnt == 0))
1927 prm->cmd->sg_mapped = 1;
1929 if (cmd->se_cmd.prot_op == TARGET_PROT_NORMAL) {
1931 * If greater than four sg entries then we need to allocate
1932 * the continuation entries
1934 if (prm->seg_cnt > prm->tgt->datasegs_per_cmd)
1935 prm->req_cnt += DIV_ROUND_UP(prm->seg_cnt -
1936 prm->tgt->datasegs_per_cmd,
1937 prm->tgt->datasegs_per_cont);
1940 if ((cmd->se_cmd.prot_op == TARGET_PROT_DIN_INSERT) ||
1941 (cmd->se_cmd.prot_op == TARGET_PROT_DOUT_STRIP)) {
1942 prm->seg_cnt = DIV_ROUND_UP(cmd->bufflen, cmd->blk_sz);
1943 prm->tot_dsds = prm->seg_cnt;
1945 prm->tot_dsds = prm->seg_cnt;
1947 if (cmd->prot_sg_cnt) {
1948 prm->prot_sg = cmd->prot_sg;
1949 prm->prot_seg_cnt = pci_map_sg(prm->tgt->ha->pdev,
1950 cmd->prot_sg, cmd->prot_sg_cnt,
1951 cmd->dma_data_direction);
1952 if (unlikely(prm->prot_seg_cnt == 0))
1955 if ((cmd->se_cmd.prot_op == TARGET_PROT_DIN_INSERT) ||
1956 (cmd->se_cmd.prot_op == TARGET_PROT_DOUT_STRIP)) {
1957 /* Dif Bundling not support here */
1958 prm->prot_seg_cnt = DIV_ROUND_UP(cmd->bufflen,
1960 prm->tot_dsds += prm->prot_seg_cnt;
1962 prm->tot_dsds += prm->prot_seg_cnt;
1969 ql_dbg(ql_dbg_tgt, prm->cmd->vha, 0xe04d,
1970 "qla_target(%d): PCI mapping failed: sg_cnt=%d",
1971 0, prm->cmd->sg_cnt);
1975 static void qlt_unmap_sg(struct scsi_qla_host *vha, struct qla_tgt_cmd *cmd)
1977 struct qla_hw_data *ha = vha->hw;
1979 if (!cmd->sg_mapped)
1982 pci_unmap_sg(ha->pdev, cmd->sg, cmd->sg_cnt, cmd->dma_data_direction);
1985 if (cmd->prot_sg_cnt)
1986 pci_unmap_sg(ha->pdev, cmd->prot_sg, cmd->prot_sg_cnt,
1987 cmd->dma_data_direction);
1989 if (cmd->ctx_dsd_alloced)
1990 qla2x00_clean_dsd_pool(ha, NULL, cmd);
1993 dma_pool_free(ha->dl_dma_pool, cmd->ctx, cmd->ctx->crc_ctx_dma);
1996 static int qlt_check_reserve_free_req(struct scsi_qla_host *vha,
1999 uint32_t cnt, cnt_in;
2001 if (vha->req->cnt < (req_cnt + 2)) {
2002 cnt = (uint16_t)RD_REG_DWORD(vha->req->req_q_out);
2003 cnt_in = (uint16_t)RD_REG_DWORD(vha->req->req_q_in);
2005 if (vha->req->ring_index < cnt)
2006 vha->req->cnt = cnt - vha->req->ring_index;
2008 vha->req->cnt = vha->req->length -
2009 (vha->req->ring_index - cnt);
2011 if (unlikely(vha->req->cnt < (req_cnt + 2))) {
2012 ql_dbg(ql_dbg_io, vha, 0x305a,
2013 "qla_target(%d): There is no room in the request ring: vha->req->ring_index=%d, vha->req->cnt=%d, req_cnt=%d Req-out=%d Req-in=%d Req-Length=%d\n",
2014 vha->vp_idx, vha->req->ring_index,
2015 vha->req->cnt, req_cnt, cnt, cnt_in,
2021 vha->req->cnt -= req_cnt;
2027 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
2029 static inline void *qlt_get_req_pkt(struct scsi_qla_host *vha)
2031 /* Adjust ring index. */
2032 vha->req->ring_index++;
2033 if (vha->req->ring_index == vha->req->length) {
2034 vha->req->ring_index = 0;
2035 vha->req->ring_ptr = vha->req->ring;
2037 vha->req->ring_ptr++;
2039 return (cont_entry_t *)vha->req->ring_ptr;
2042 /* ha->hardware_lock supposed to be held on entry */
2043 static inline uint32_t qlt_make_handle(struct scsi_qla_host *vha)
2045 struct qla_hw_data *ha = vha->hw;
2048 h = ha->tgt.current_handle;
2049 /* always increment cmd handle */
2052 if (h > DEFAULT_OUTSTANDING_COMMANDS)
2053 h = 1; /* 0 is QLA_TGT_NULL_HANDLE */
2054 if (h == ha->tgt.current_handle) {
2055 ql_dbg(ql_dbg_io, vha, 0x305b,
2056 "qla_target(%d): Ran out of "
2057 "empty cmd slots in ha %p\n", vha->vp_idx, ha);
2058 h = QLA_TGT_NULL_HANDLE;
2061 } while ((h == QLA_TGT_NULL_HANDLE) ||
2062 (h == QLA_TGT_SKIP_HANDLE) ||
2063 (ha->tgt.cmds[h-1] != NULL));
2065 if (h != QLA_TGT_NULL_HANDLE)
2066 ha->tgt.current_handle = h;
2071 /* ha->hardware_lock supposed to be held on entry */
2072 static int qlt_24xx_build_ctio_pkt(struct qla_tgt_prm *prm,
2073 struct scsi_qla_host *vha)
2076 struct ctio7_to_24xx *pkt;
2077 struct qla_hw_data *ha = vha->hw;
2078 struct atio_from_isp *atio = &prm->cmd->atio;
2081 pkt = (struct ctio7_to_24xx *)vha->req->ring_ptr;
2083 memset(pkt, 0, sizeof(*pkt));
2085 pkt->entry_type = CTIO_TYPE7;
2086 pkt->entry_count = (uint8_t)prm->req_cnt;
2087 pkt->vp_index = vha->vp_idx;
2089 h = qlt_make_handle(vha);
2090 if (unlikely(h == QLA_TGT_NULL_HANDLE)) {
2092 * CTIO type 7 from the firmware doesn't provide a way to
2093 * know the initiator's LOOP ID, hence we can't find
2094 * the session and, so, the command.
2098 ha->tgt.cmds[h-1] = prm->cmd;
2100 pkt->handle = h | CTIO_COMPLETION_HANDLE_MARK;
2101 pkt->nport_handle = prm->cmd->loop_id;
2102 pkt->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
2103 pkt->initiator_id[0] = atio->u.isp24.fcp_hdr.s_id[2];
2104 pkt->initiator_id[1] = atio->u.isp24.fcp_hdr.s_id[1];
2105 pkt->initiator_id[2] = atio->u.isp24.fcp_hdr.s_id[0];
2106 pkt->exchange_addr = atio->u.isp24.exchange_addr;
2107 pkt->u.status0.flags |= (atio->u.isp24.attr << 9);
2108 temp = be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id);
2109 pkt->u.status0.ox_id = cpu_to_le16(temp);
2110 pkt->u.status0.relative_offset = cpu_to_le32(prm->cmd->offset);
2116 * ha->hardware_lock supposed to be held on entry. We have already made sure
2117 * that there is sufficient amount of request entries to not drop it.
2119 static void qlt_load_cont_data_segments(struct qla_tgt_prm *prm,
2120 struct scsi_qla_host *vha)
2123 uint32_t *dword_ptr;
2124 int enable_64bit_addressing = prm->tgt->tgt_enable_64bit_addr;
2126 /* Build continuation packets */
2127 while (prm->seg_cnt > 0) {
2128 cont_a64_entry_t *cont_pkt64 =
2129 (cont_a64_entry_t *)qlt_get_req_pkt(vha);
2132 * Make sure that from cont_pkt64 none of
2133 * 64-bit specific fields used for 32-bit
2134 * addressing. Cast to (cont_entry_t *) for
2138 memset(cont_pkt64, 0, sizeof(*cont_pkt64));
2140 cont_pkt64->entry_count = 1;
2141 cont_pkt64->sys_define = 0;
2143 if (enable_64bit_addressing) {
2144 cont_pkt64->entry_type = CONTINUE_A64_TYPE;
2146 (uint32_t *)&cont_pkt64->dseg_0_address;
2148 cont_pkt64->entry_type = CONTINUE_TYPE;
2150 (uint32_t *)&((cont_entry_t *)
2151 cont_pkt64)->dseg_0_address;
2154 /* Load continuation entry data segments */
2156 cnt < prm->tgt->datasegs_per_cont && prm->seg_cnt;
2157 cnt++, prm->seg_cnt--) {
2159 cpu_to_le32(pci_dma_lo32
2160 (sg_dma_address(prm->sg)));
2161 if (enable_64bit_addressing) {
2163 cpu_to_le32(pci_dma_hi32
2167 *dword_ptr++ = cpu_to_le32(sg_dma_len(prm->sg));
2169 prm->sg = sg_next(prm->sg);
2175 * ha->hardware_lock supposed to be held on entry. We have already made sure
2176 * that there is sufficient amount of request entries to not drop it.
2178 static void qlt_load_data_segments(struct qla_tgt_prm *prm,
2179 struct scsi_qla_host *vha)
2182 uint32_t *dword_ptr;
2183 int enable_64bit_addressing = prm->tgt->tgt_enable_64bit_addr;
2184 struct ctio7_to_24xx *pkt24 = (struct ctio7_to_24xx *)prm->pkt;
2186 pkt24->u.status0.transfer_length = cpu_to_le32(prm->cmd->bufflen);
2188 /* Setup packet address segment pointer */
2189 dword_ptr = pkt24->u.status0.dseg_0_address;
2191 /* Set total data segment count */
2193 pkt24->dseg_count = cpu_to_le16(prm->seg_cnt);
2195 if (prm->seg_cnt == 0) {
2196 /* No data transfer */
2202 /* If scatter gather */
2204 /* Load command entry data segments */
2206 (cnt < prm->tgt->datasegs_per_cmd) && prm->seg_cnt;
2207 cnt++, prm->seg_cnt--) {
2209 cpu_to_le32(pci_dma_lo32(sg_dma_address(prm->sg)));
2210 if (enable_64bit_addressing) {
2212 cpu_to_le32(pci_dma_hi32(
2213 sg_dma_address(prm->sg)));
2215 *dword_ptr++ = cpu_to_le32(sg_dma_len(prm->sg));
2217 prm->sg = sg_next(prm->sg);
2220 qlt_load_cont_data_segments(prm, vha);
2223 static inline int qlt_has_data(struct qla_tgt_cmd *cmd)
2225 return cmd->bufflen > 0;
2229 * Called without ha->hardware_lock held
2231 static int qlt_pre_xmit_response(struct qla_tgt_cmd *cmd,
2232 struct qla_tgt_prm *prm, int xmit_type, uint8_t scsi_status,
2233 uint32_t *full_req_cnt)
2235 struct qla_tgt *tgt = cmd->tgt;
2236 struct scsi_qla_host *vha = tgt->vha;
2237 struct qla_hw_data *ha = vha->hw;
2238 struct se_cmd *se_cmd = &cmd->se_cmd;
2242 prm->rq_result = scsi_status;
2243 prm->sense_buffer = &cmd->sense_buffer[0];
2244 prm->sense_buffer_len = TRANSPORT_SENSE_BUFFER;
2248 prm->add_status_pkt = 0;
2250 /* Send marker if required */
2251 if (qlt_issue_marker(vha, 0) != QLA_SUCCESS)
2254 if ((xmit_type & QLA_TGT_XMIT_DATA) && qlt_has_data(cmd)) {
2255 if (qlt_pci_map_calc_cnt(prm) != 0)
2259 *full_req_cnt = prm->req_cnt;
2261 if (se_cmd->se_cmd_flags & SCF_UNDERFLOW_BIT) {
2262 prm->residual = se_cmd->residual_count;
2263 ql_dbg(ql_dbg_io + ql_dbg_verbose, vha, 0x305c,
2264 "Residual underflow: %d (tag %lld, op %x, bufflen %d, rq_result %x)\n",
2265 prm->residual, se_cmd->tag,
2266 se_cmd->t_task_cdb ? se_cmd->t_task_cdb[0] : 0,
2267 cmd->bufflen, prm->rq_result);
2268 prm->rq_result |= SS_RESIDUAL_UNDER;
2269 } else if (se_cmd->se_cmd_flags & SCF_OVERFLOW_BIT) {
2270 prm->residual = se_cmd->residual_count;
2271 ql_dbg(ql_dbg_io, vha, 0x305d,
2272 "Residual overflow: %d (tag %lld, op %x, bufflen %d, rq_result %x)\n",
2273 prm->residual, se_cmd->tag, se_cmd->t_task_cdb ?
2274 se_cmd->t_task_cdb[0] : 0, cmd->bufflen, prm->rq_result);
2275 prm->rq_result |= SS_RESIDUAL_OVER;
2278 if (xmit_type & QLA_TGT_XMIT_STATUS) {
2280 * If QLA_TGT_XMIT_DATA is not set, add_status_pkt will be
2281 * ignored in *xmit_response() below
2283 if (qlt_has_data(cmd)) {
2284 if (QLA_TGT_SENSE_VALID(prm->sense_buffer) ||
2285 (IS_FWI2_CAPABLE(ha) &&
2286 (prm->rq_result != 0))) {
2287 prm->add_status_pkt = 1;
2296 static inline int qlt_need_explicit_conf(struct qla_hw_data *ha,
2297 struct qla_tgt_cmd *cmd, int sending_sense)
2299 if (ha->tgt.enable_class_2)
2303 return cmd->conf_compl_supported;
2305 return ha->tgt.enable_explicit_conf &&
2306 cmd->conf_compl_supported;
2309 static void qlt_24xx_init_ctio_to_isp(struct ctio7_to_24xx *ctio,
2310 struct qla_tgt_prm *prm)
2312 prm->sense_buffer_len = min_t(uint32_t, prm->sense_buffer_len,
2313 (uint32_t)sizeof(ctio->u.status1.sense_data));
2314 ctio->u.status0.flags |= cpu_to_le16(CTIO7_FLAGS_SEND_STATUS);
2315 if (qlt_need_explicit_conf(prm->tgt->ha, prm->cmd, 0)) {
2316 ctio->u.status0.flags |= cpu_to_le16(
2317 CTIO7_FLAGS_EXPLICIT_CONFORM |
2318 CTIO7_FLAGS_CONFORM_REQ);
2320 ctio->u.status0.residual = cpu_to_le32(prm->residual);
2321 ctio->u.status0.scsi_status = cpu_to_le16(prm->rq_result);
2322 if (QLA_TGT_SENSE_VALID(prm->sense_buffer)) {
2325 if (qlt_need_explicit_conf(prm->tgt->ha, prm->cmd, 1)) {
2326 if ((prm->rq_result & SS_SCSI_STATUS_BYTE) != 0) {
2327 ql_dbg(ql_dbg_tgt, prm->cmd->vha, 0xe017,
2328 "Skipping EXPLICIT_CONFORM and "
2329 "CTIO7_FLAGS_CONFORM_REQ for FCP READ w/ "
2330 "non GOOD status\n");
2331 goto skip_explict_conf;
2333 ctio->u.status1.flags |= cpu_to_le16(
2334 CTIO7_FLAGS_EXPLICIT_CONFORM |
2335 CTIO7_FLAGS_CONFORM_REQ);
2338 ctio->u.status1.flags &=
2339 ~cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_0);
2340 ctio->u.status1.flags |=
2341 cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_1);
2342 ctio->u.status1.scsi_status |=
2343 cpu_to_le16(SS_SENSE_LEN_VALID);
2344 ctio->u.status1.sense_length =
2345 cpu_to_le16(prm->sense_buffer_len);
2346 for (i = 0; i < prm->sense_buffer_len/4; i++)
2347 ((uint32_t *)ctio->u.status1.sense_data)[i] =
2348 cpu_to_be32(((uint32_t *)prm->sense_buffer)[i]);
2350 if (unlikely((prm->sense_buffer_len % 4) != 0)) {
2353 ql_dbg(ql_dbg_tgt, vha, 0xe04f,
2354 "qla_target(%d): %d bytes of sense "
2355 "lost", prm->tgt->ha->vp_idx,
2356 prm->sense_buffer_len % 4);
2362 ctio->u.status1.flags &=
2363 ~cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_0);
2364 ctio->u.status1.flags |=
2365 cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_1);
2366 ctio->u.status1.sense_length = 0;
2367 memset(ctio->u.status1.sense_data, 0,
2368 sizeof(ctio->u.status1.sense_data));
2371 /* Sense with len > 24, is it possible ??? */
2378 qlt_hba_err_chk_enabled(struct se_cmd *se_cmd)
2381 * Uncomment when corresponding SCSI changes are done.
2383 if (!sp->cmd->prot_chk)
2387 switch (se_cmd->prot_op) {
2388 case TARGET_PROT_DOUT_INSERT:
2389 case TARGET_PROT_DIN_STRIP:
2390 if (ql2xenablehba_err_chk >= 1)
2393 case TARGET_PROT_DOUT_PASS:
2394 case TARGET_PROT_DIN_PASS:
2395 if (ql2xenablehba_err_chk >= 2)
2398 case TARGET_PROT_DIN_INSERT:
2399 case TARGET_PROT_DOUT_STRIP:
2408 * qla24xx_set_t10dif_tags_from_cmd - Extract Ref and App tags from SCSI command
2412 qlt_set_t10dif_tags(struct se_cmd *se_cmd, struct crc_context *ctx)
2414 uint32_t lba = 0xffffffff & se_cmd->t_task_lba;
2416 /* wait til Mode Sense/Select cmd, modepage Ah, subpage 2
2417 * have been immplemented by TCM, before AppTag is avail.
2418 * Look for modesense_handlers[]
2421 ctx->app_tag_mask[0] = 0x0;
2422 ctx->app_tag_mask[1] = 0x0;
2424 switch (se_cmd->prot_type) {
2425 case TARGET_DIF_TYPE0_PROT:
2427 * No check for ql2xenablehba_err_chk, as it would be an
2428 * I/O error if hba tag generation is not done.
2430 ctx->ref_tag = cpu_to_le32(lba);
2432 if (!qlt_hba_err_chk_enabled(se_cmd))
2435 /* enable ALL bytes of the ref tag */
2436 ctx->ref_tag_mask[0] = 0xff;
2437 ctx->ref_tag_mask[1] = 0xff;
2438 ctx->ref_tag_mask[2] = 0xff;
2439 ctx->ref_tag_mask[3] = 0xff;
2442 * For TYpe 1 protection: 16 bit GUARD tag, 32 bit REF tag, and
2445 case TARGET_DIF_TYPE1_PROT:
2446 ctx->ref_tag = cpu_to_le32(lba);
2448 if (!qlt_hba_err_chk_enabled(se_cmd))
2451 /* enable ALL bytes of the ref tag */
2452 ctx->ref_tag_mask[0] = 0xff;
2453 ctx->ref_tag_mask[1] = 0xff;
2454 ctx->ref_tag_mask[2] = 0xff;
2455 ctx->ref_tag_mask[3] = 0xff;
2458 * For TYPE 2 protection: 16 bit GUARD + 32 bit REF tag has to
2459 * match LBA in CDB + N
2461 case TARGET_DIF_TYPE2_PROT:
2462 ctx->ref_tag = cpu_to_le32(lba);
2464 if (!qlt_hba_err_chk_enabled(se_cmd))
2467 /* enable ALL bytes of the ref tag */
2468 ctx->ref_tag_mask[0] = 0xff;
2469 ctx->ref_tag_mask[1] = 0xff;
2470 ctx->ref_tag_mask[2] = 0xff;
2471 ctx->ref_tag_mask[3] = 0xff;
2474 /* For Type 3 protection: 16 bit GUARD only */
2475 case TARGET_DIF_TYPE3_PROT:
2476 ctx->ref_tag_mask[0] = ctx->ref_tag_mask[1] =
2477 ctx->ref_tag_mask[2] = ctx->ref_tag_mask[3] = 0x00;
2484 qlt_build_ctio_crc2_pkt(struct qla_tgt_prm *prm, scsi_qla_host_t *vha)
2487 uint32_t transfer_length = 0;
2488 uint32_t data_bytes;
2490 uint8_t bundling = 1;
2492 struct crc_context *crc_ctx_pkt = NULL;
2493 struct qla_hw_data *ha;
2494 struct ctio_crc2_to_fw *pkt;
2495 dma_addr_t crc_ctx_dma;
2496 uint16_t fw_prot_opts = 0;
2497 struct qla_tgt_cmd *cmd = prm->cmd;
2498 struct se_cmd *se_cmd = &cmd->se_cmd;
2500 struct atio_from_isp *atio = &prm->cmd->atio;
2505 pkt = (struct ctio_crc2_to_fw *)vha->req->ring_ptr;
2507 memset(pkt, 0, sizeof(*pkt));
2509 ql_dbg(ql_dbg_tgt, vha, 0xe071,
2510 "qla_target(%d):%s: se_cmd[%p] CRC2 prot_op[0x%x] cmd prot sg:cnt[%p:%x] lba[%llu]\n",
2511 vha->vp_idx, __func__, se_cmd, se_cmd->prot_op,
2512 prm->prot_sg, prm->prot_seg_cnt, se_cmd->t_task_lba);
2514 if ((se_cmd->prot_op == TARGET_PROT_DIN_INSERT) ||
2515 (se_cmd->prot_op == TARGET_PROT_DOUT_STRIP))
2518 /* Compute dif len and adjust data len to incude protection */
2519 data_bytes = cmd->bufflen;
2520 dif_bytes = (data_bytes / cmd->blk_sz) * 8;
2522 switch (se_cmd->prot_op) {
2523 case TARGET_PROT_DIN_INSERT:
2524 case TARGET_PROT_DOUT_STRIP:
2525 transfer_length = data_bytes;
2526 data_bytes += dif_bytes;
2529 case TARGET_PROT_DIN_STRIP:
2530 case TARGET_PROT_DOUT_INSERT:
2531 case TARGET_PROT_DIN_PASS:
2532 case TARGET_PROT_DOUT_PASS:
2533 transfer_length = data_bytes + dif_bytes;
2541 if (!qlt_hba_err_chk_enabled(se_cmd))
2542 fw_prot_opts |= 0x10; /* Disable Guard tag checking */
2543 /* HBA error checking enabled */
2544 else if (IS_PI_UNINIT_CAPABLE(ha)) {
2545 if ((se_cmd->prot_type == TARGET_DIF_TYPE1_PROT) ||
2546 (se_cmd->prot_type == TARGET_DIF_TYPE2_PROT))
2547 fw_prot_opts |= PO_DIS_VALD_APP_ESC;
2548 else if (se_cmd->prot_type == TARGET_DIF_TYPE3_PROT)
2549 fw_prot_opts |= PO_DIS_VALD_APP_REF_ESC;
2552 switch (se_cmd->prot_op) {
2553 case TARGET_PROT_DIN_INSERT:
2554 case TARGET_PROT_DOUT_INSERT:
2555 fw_prot_opts |= PO_MODE_DIF_INSERT;
2557 case TARGET_PROT_DIN_STRIP:
2558 case TARGET_PROT_DOUT_STRIP:
2559 fw_prot_opts |= PO_MODE_DIF_REMOVE;
2561 case TARGET_PROT_DIN_PASS:
2562 case TARGET_PROT_DOUT_PASS:
2563 fw_prot_opts |= PO_MODE_DIF_PASS;
2564 /* FUTURE: does tcm require T10CRC<->IPCKSUM conversion? */
2566 default:/* Normal Request */
2567 fw_prot_opts |= PO_MODE_DIF_PASS;
2573 /* Update entry type to indicate Command Type CRC_2 IOCB */
2574 pkt->entry_type = CTIO_CRC2;
2575 pkt->entry_count = 1;
2576 pkt->vp_index = vha->vp_idx;
2578 h = qlt_make_handle(vha);
2579 if (unlikely(h == QLA_TGT_NULL_HANDLE)) {
2581 * CTIO type 7 from the firmware doesn't provide a way to
2582 * know the initiator's LOOP ID, hence we can't find
2583 * the session and, so, the command.
2587 ha->tgt.cmds[h-1] = prm->cmd;
2590 pkt->handle = h | CTIO_COMPLETION_HANDLE_MARK;
2591 pkt->nport_handle = prm->cmd->loop_id;
2592 pkt->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
2593 pkt->initiator_id[0] = atio->u.isp24.fcp_hdr.s_id[2];
2594 pkt->initiator_id[1] = atio->u.isp24.fcp_hdr.s_id[1];
2595 pkt->initiator_id[2] = atio->u.isp24.fcp_hdr.s_id[0];
2596 pkt->exchange_addr = atio->u.isp24.exchange_addr;
2598 /* silence compile warning */
2599 t16 = be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id);
2600 pkt->ox_id = cpu_to_le16(t16);
2602 t16 = (atio->u.isp24.attr << 9);
2603 pkt->flags |= cpu_to_le16(t16);
2604 pkt->relative_offset = cpu_to_le32(prm->cmd->offset);
2606 /* Set transfer direction */
2607 if (cmd->dma_data_direction == DMA_TO_DEVICE)
2608 pkt->flags = cpu_to_le16(CTIO7_FLAGS_DATA_IN);
2609 else if (cmd->dma_data_direction == DMA_FROM_DEVICE)
2610 pkt->flags = cpu_to_le16(CTIO7_FLAGS_DATA_OUT);
2613 pkt->dseg_count = prm->tot_dsds;
2614 /* Fibre channel byte count */
2615 pkt->transfer_length = cpu_to_le32(transfer_length);
2618 /* ----- CRC context -------- */
2620 /* Allocate CRC context from global pool */
2621 crc_ctx_pkt = cmd->ctx =
2622 dma_pool_alloc(ha->dl_dma_pool, GFP_ATOMIC, &crc_ctx_dma);
2625 goto crc_queuing_error;
2627 /* Zero out CTX area. */
2628 clr_ptr = (uint8_t *)crc_ctx_pkt;
2629 memset(clr_ptr, 0, sizeof(*crc_ctx_pkt));
2631 crc_ctx_pkt->crc_ctx_dma = crc_ctx_dma;
2632 INIT_LIST_HEAD(&crc_ctx_pkt->dsd_list);
2635 crc_ctx_pkt->handle = pkt->handle;
2637 qlt_set_t10dif_tags(se_cmd, crc_ctx_pkt);
2639 pkt->crc_context_address[0] = cpu_to_le32(LSD(crc_ctx_dma));
2640 pkt->crc_context_address[1] = cpu_to_le32(MSD(crc_ctx_dma));
2641 pkt->crc_context_len = CRC_CONTEXT_LEN_FW;
2645 cur_dsd = (uint32_t *) &crc_ctx_pkt->u.nobundling.data_address;
2648 * Configure Bundling if we need to fetch interlaving
2649 * protection PCI accesses
2651 fw_prot_opts |= PO_ENABLE_DIF_BUNDLING;
2652 crc_ctx_pkt->u.bundling.dif_byte_count = cpu_to_le32(dif_bytes);
2653 crc_ctx_pkt->u.bundling.dseg_count =
2654 cpu_to_le16(prm->tot_dsds - prm->prot_seg_cnt);
2655 cur_dsd = (uint32_t *) &crc_ctx_pkt->u.bundling.data_address;
2658 /* Finish the common fields of CRC pkt */
2659 crc_ctx_pkt->blk_size = cpu_to_le16(cmd->blk_sz);
2660 crc_ctx_pkt->prot_opts = cpu_to_le16(fw_prot_opts);
2661 crc_ctx_pkt->byte_count = cpu_to_le32(data_bytes);
2662 crc_ctx_pkt->guard_seed = cpu_to_le16(0);
2665 /* Walks data segments */
2666 pkt->flags |= cpu_to_le16(CTIO7_FLAGS_DSD_PTR);
2668 if (!bundling && prm->prot_seg_cnt) {
2669 if (qla24xx_walk_and_build_sglist_no_difb(ha, NULL, cur_dsd,
2670 prm->tot_dsds, cmd))
2671 goto crc_queuing_error;
2672 } else if (qla24xx_walk_and_build_sglist(ha, NULL, cur_dsd,
2673 (prm->tot_dsds - prm->prot_seg_cnt), cmd))
2674 goto crc_queuing_error;
2676 if (bundling && prm->prot_seg_cnt) {
2677 /* Walks dif segments */
2678 pkt->add_flags |= CTIO_CRC2_AF_DIF_DSD_ENA;
2680 cur_dsd = (uint32_t *) &crc_ctx_pkt->u.bundling.dif_address;
2681 if (qla24xx_walk_and_build_prot_sglist(ha, NULL, cur_dsd,
2682 prm->prot_seg_cnt, cmd))
2683 goto crc_queuing_error;
2688 /* Cleanup will be performed by the caller */
2690 return QLA_FUNCTION_FAILED;
2695 * Callback to setup response of xmit_type of QLA_TGT_XMIT_DATA and *
2696 * QLA_TGT_XMIT_STATUS for >= 24xx silicon
2698 int qlt_xmit_response(struct qla_tgt_cmd *cmd, int xmit_type,
2699 uint8_t scsi_status)
2701 struct scsi_qla_host *vha = cmd->vha;
2702 struct qla_hw_data *ha = vha->hw;
2703 struct ctio7_to_24xx *pkt;
2704 struct qla_tgt_prm prm;
2705 uint32_t full_req_cnt = 0;
2706 unsigned long flags = 0;
2709 spin_lock_irqsave(&ha->hardware_lock, flags);
2710 if (cmd->sess && cmd->sess->deleted) {
2711 cmd->state = QLA_TGT_STATE_PROCESSED;
2712 if (cmd->sess->logout_completed)
2713 /* no need to terminate. FW already freed exchange. */
2714 qlt_abort_cmd_on_host_reset(cmd->vha, cmd);
2716 qlt_send_term_exchange(vha, cmd, &cmd->atio, 1, 0);
2717 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2720 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2722 memset(&prm, 0, sizeof(prm));
2724 ql_dbg(ql_dbg_tgt, cmd->vha, 0xe018,
2725 "is_send_status=%d, cmd->bufflen=%d, cmd->sg_cnt=%d, cmd->dma_data_direction=%d se_cmd[%p]\n",
2726 (xmit_type & QLA_TGT_XMIT_STATUS) ?
2727 1 : 0, cmd->bufflen, cmd->sg_cnt, cmd->dma_data_direction,
2730 res = qlt_pre_xmit_response(cmd, &prm, xmit_type, scsi_status,
2732 if (unlikely(res != 0)) {
2736 spin_lock_irqsave(&ha->hardware_lock, flags);
2738 if (xmit_type == QLA_TGT_XMIT_STATUS)
2739 vha->tgt_counters.core_qla_snd_status++;
2741 vha->tgt_counters.core_qla_que_buf++;
2743 if (!vha->flags.online || cmd->reset_count != ha->chip_reset) {
2745 * Either the port is not online or this request was from
2746 * previous life, just abort the processing.
2748 cmd->state = QLA_TGT_STATE_PROCESSED;
2749 qlt_abort_cmd_on_host_reset(cmd->vha, cmd);
2750 ql_dbg(ql_dbg_async, vha, 0xe101,
2751 "RESET-RSP online/active/old-count/new-count = %d/%d/%d/%d.\n",
2752 vha->flags.online, qla2x00_reset_active(vha),
2753 cmd->reset_count, ha->chip_reset);
2754 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2758 /* Does F/W have an IOCBs for this request */
2759 res = qlt_check_reserve_free_req(vha, full_req_cnt);
2761 goto out_unmap_unlock;
2763 if (cmd->se_cmd.prot_op && (xmit_type & QLA_TGT_XMIT_DATA))
2764 res = qlt_build_ctio_crc2_pkt(&prm, vha);
2766 res = qlt_24xx_build_ctio_pkt(&prm, vha);
2767 if (unlikely(res != 0)) {
2768 vha->req->cnt += full_req_cnt;
2769 goto out_unmap_unlock;
2772 pkt = (struct ctio7_to_24xx *)prm.pkt;
2774 if (qlt_has_data(cmd) && (xmit_type & QLA_TGT_XMIT_DATA)) {
2775 pkt->u.status0.flags |=
2776 cpu_to_le16(CTIO7_FLAGS_DATA_IN |
2777 CTIO7_FLAGS_STATUS_MODE_0);
2779 if (cmd->se_cmd.prot_op == TARGET_PROT_NORMAL)
2780 qlt_load_data_segments(&prm, vha);
2782 if (prm.add_status_pkt == 0) {
2783 if (xmit_type & QLA_TGT_XMIT_STATUS) {
2784 pkt->u.status0.scsi_status =
2785 cpu_to_le16(prm.rq_result);
2786 pkt->u.status0.residual =
2787 cpu_to_le32(prm.residual);
2788 pkt->u.status0.flags |= cpu_to_le16(
2789 CTIO7_FLAGS_SEND_STATUS);
2790 if (qlt_need_explicit_conf(ha, cmd, 0)) {
2791 pkt->u.status0.flags |=
2793 CTIO7_FLAGS_EXPLICIT_CONFORM |
2794 CTIO7_FLAGS_CONFORM_REQ);
2800 * We have already made sure that there is sufficient
2801 * amount of request entries to not drop HW lock in
2804 struct ctio7_to_24xx *ctio =
2805 (struct ctio7_to_24xx *)qlt_get_req_pkt(vha);
2807 ql_dbg(ql_dbg_io, vha, 0x305e,
2808 "Building additional status packet 0x%p.\n",
2812 * T10Dif: ctio_crc2_to_fw overlay ontop of
2815 memcpy(ctio, pkt, sizeof(*ctio));
2816 /* reset back to CTIO7 */
2817 ctio->entry_count = 1;
2818 ctio->entry_type = CTIO_TYPE7;
2819 ctio->dseg_count = 0;
2820 ctio->u.status1.flags &= ~cpu_to_le16(
2821 CTIO7_FLAGS_DATA_IN);
2823 /* Real finish is ctio_m1's finish */
2824 pkt->handle |= CTIO_INTERMEDIATE_HANDLE_MARK;
2825 pkt->u.status0.flags |= cpu_to_le16(
2826 CTIO7_FLAGS_DONT_RET_CTIO);
2828 /* qlt_24xx_init_ctio_to_isp will correct
2829 * all neccessary fields that's part of CTIO7.
2830 * There should be no residual of CTIO-CRC2 data.
2832 qlt_24xx_init_ctio_to_isp((struct ctio7_to_24xx *)ctio,
2834 pr_debug("Status CTIO7: %p\n", ctio);
2837 qlt_24xx_init_ctio_to_isp(pkt, &prm);
2840 cmd->state = QLA_TGT_STATE_PROCESSED; /* Mid-level is done processing */
2841 cmd->cmd_sent_to_fw = 1;
2843 /* Memory Barrier */
2845 qla2x00_start_iocbs(vha, vha->req);
2846 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2851 qlt_unmap_sg(vha, cmd);
2852 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2856 EXPORT_SYMBOL(qlt_xmit_response);
2858 int qlt_rdy_to_xfer(struct qla_tgt_cmd *cmd)
2860 struct ctio7_to_24xx *pkt;
2861 struct scsi_qla_host *vha = cmd->vha;
2862 struct qla_hw_data *ha = vha->hw;
2863 struct qla_tgt *tgt = cmd->tgt;
2864 struct qla_tgt_prm prm;
2865 unsigned long flags;
2868 memset(&prm, 0, sizeof(prm));
2874 /* Send marker if required */
2875 if (qlt_issue_marker(vha, 0) != QLA_SUCCESS)
2878 /* Calculate number of entries and segments required */
2879 if (qlt_pci_map_calc_cnt(&prm) != 0)
2882 spin_lock_irqsave(&ha->hardware_lock, flags);
2884 if (!vha->flags.online || (cmd->reset_count != ha->chip_reset) ||
2885 (cmd->sess && cmd->sess->deleted)) {
2887 * Either the port is not online or this request was from
2888 * previous life, just abort the processing.
2890 cmd->state = QLA_TGT_STATE_NEED_DATA;
2891 qlt_abort_cmd_on_host_reset(cmd->vha, cmd);
2892 ql_dbg(ql_dbg_async, vha, 0xe102,
2893 "RESET-XFR online/active/old-count/new-count = %d/%d/%d/%d.\n",
2894 vha->flags.online, qla2x00_reset_active(vha),
2895 cmd->reset_count, ha->chip_reset);
2896 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2900 /* Does F/W have an IOCBs for this request */
2901 res = qlt_check_reserve_free_req(vha, prm.req_cnt);
2903 goto out_unlock_free_unmap;
2904 if (cmd->se_cmd.prot_op)
2905 res = qlt_build_ctio_crc2_pkt(&prm, vha);
2907 res = qlt_24xx_build_ctio_pkt(&prm, vha);
2909 if (unlikely(res != 0)) {
2910 vha->req->cnt += prm.req_cnt;
2911 goto out_unlock_free_unmap;
2914 pkt = (struct ctio7_to_24xx *)prm.pkt;
2915 pkt->u.status0.flags |= cpu_to_le16(CTIO7_FLAGS_DATA_OUT |
2916 CTIO7_FLAGS_STATUS_MODE_0);
2918 if (cmd->se_cmd.prot_op == TARGET_PROT_NORMAL)
2919 qlt_load_data_segments(&prm, vha);
2921 cmd->state = QLA_TGT_STATE_NEED_DATA;
2922 cmd->cmd_sent_to_fw = 1;
2924 /* Memory Barrier */
2926 qla2x00_start_iocbs(vha, vha->req);
2927 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2931 out_unlock_free_unmap:
2932 qlt_unmap_sg(vha, cmd);
2933 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2937 EXPORT_SYMBOL(qlt_rdy_to_xfer);
2941 * Checks the guard or meta-data for the type of error
2942 * detected by the HBA.
2945 qlt_handle_dif_error(struct scsi_qla_host *vha, struct qla_tgt_cmd *cmd,
2946 struct ctio_crc_from_fw *sts)
2948 uint8_t *ap = &sts->actual_dif[0];
2949 uint8_t *ep = &sts->expected_dif[0];
2950 uint32_t e_ref_tag, a_ref_tag;
2951 uint16_t e_app_tag, a_app_tag;
2952 uint16_t e_guard, a_guard;
2953 uint64_t lba = cmd->se_cmd.t_task_lba;
2955 a_guard = be16_to_cpu(*(uint16_t *)(ap + 0));
2956 a_app_tag = be16_to_cpu(*(uint16_t *)(ap + 2));
2957 a_ref_tag = be32_to_cpu(*(uint32_t *)(ap + 4));
2959 e_guard = be16_to_cpu(*(uint16_t *)(ep + 0));
2960 e_app_tag = be16_to_cpu(*(uint16_t *)(ep + 2));
2961 e_ref_tag = be32_to_cpu(*(uint32_t *)(ep + 4));
2963 ql_dbg(ql_dbg_tgt, vha, 0xe075,
2964 "iocb(s) %p Returned STATUS.\n", sts);
2966 ql_dbg(ql_dbg_tgt, vha, 0xf075,
2967 "dif check TGT cdb 0x%x lba 0x%llx: [Actual|Expected] Ref Tag[0x%x|0x%x], App Tag [0x%x|0x%x], Guard [0x%x|0x%x]\n",
2968 cmd->atio.u.isp24.fcp_cmnd.cdb[0], lba,
2969 a_ref_tag, e_ref_tag, a_app_tag, e_app_tag, a_guard, e_guard);
2973 * For type 3: ref & app tag is all 'f's
2974 * For type 0,1,2: app tag is all 'f's
2976 if ((a_app_tag == 0xffff) &&
2977 ((cmd->se_cmd.prot_type != TARGET_DIF_TYPE3_PROT) ||
2978 (a_ref_tag == 0xffffffff))) {
2979 uint32_t blocks_done;
2981 /* 2TB boundary case covered automatically with this */
2982 blocks_done = e_ref_tag - (uint32_t)lba + 1;
2983 cmd->se_cmd.bad_sector = e_ref_tag;
2984 cmd->se_cmd.pi_err = 0;
2985 ql_dbg(ql_dbg_tgt, vha, 0xf074,
2986 "need to return scsi good\n");
2988 /* Update protection tag */
2989 if (cmd->prot_sg_cnt) {
2990 uint32_t i, k = 0, num_ent;
2991 struct scatterlist *sg, *sgl;
2996 /* Patch the corresponding protection tags */
2997 for_each_sg(sgl, sg, cmd->prot_sg_cnt, i) {
2998 num_ent = sg_dma_len(sg) / 8;
2999 if (k + num_ent < blocks_done) {
3007 if (k != blocks_done) {
3008 ql_log(ql_log_warn, vha, 0xf076,
3009 "unexpected tag values tag:lba=%u:%llu)\n",
3010 e_ref_tag, (unsigned long long)lba);
3015 struct sd_dif_tuple *spt;
3017 * This section came from initiator. Is it valid here?
3018 * should ulp be override with actual val???
3020 spt = page_address(sg_page(sg)) + sg->offset;
3023 spt->app_tag = 0xffff;
3024 if (cmd->se_cmd.prot_type == SCSI_PROT_DIF_TYPE3)
3025 spt->ref_tag = 0xffffffff;
3033 if (e_guard != a_guard) {
3034 cmd->se_cmd.pi_err = TCM_LOGICAL_BLOCK_GUARD_CHECK_FAILED;
3035 cmd->se_cmd.bad_sector = cmd->se_cmd.t_task_lba;
3037 ql_log(ql_log_warn, vha, 0xe076,
3038 "Guard ERR: cdb 0x%x lba 0x%llx: [Actual|Expected] Ref Tag[0x%x|0x%x], App Tag [0x%x|0x%x], Guard [0x%x|0x%x] cmd=%p\n",
3039 cmd->atio.u.isp24.fcp_cmnd.cdb[0], lba,
3040 a_ref_tag, e_ref_tag, a_app_tag, e_app_tag,
3041 a_guard, e_guard, cmd);
3046 if (e_ref_tag != a_ref_tag) {
3047 cmd->se_cmd.pi_err = TCM_LOGICAL_BLOCK_REF_TAG_CHECK_FAILED;
3048 cmd->se_cmd.bad_sector = e_ref_tag;
3050 ql_log(ql_log_warn, vha, 0xe077,
3051 "Ref Tag ERR: cdb 0x%x lba 0x%llx: [Actual|Expected] Ref Tag[0x%x|0x%x], App Tag [0x%x|0x%x], Guard [0x%x|0x%x] cmd=%p\n",
3052 cmd->atio.u.isp24.fcp_cmnd.cdb[0], lba,
3053 a_ref_tag, e_ref_tag, a_app_tag, e_app_tag,
3054 a_guard, e_guard, cmd);
3058 /* check appl tag */
3059 if (e_app_tag != a_app_tag) {
3060 cmd->se_cmd.pi_err = TCM_LOGICAL_BLOCK_APP_TAG_CHECK_FAILED;
3061 cmd->se_cmd.bad_sector = cmd->se_cmd.t_task_lba;
3063 ql_log(ql_log_warn, vha, 0xe078,
3064 "App Tag ERR: cdb 0x%x lba 0x%llx: [Actual|Expected] Ref Tag[0x%x|0x%x], App Tag [0x%x|0x%x], Guard [0x%x|0x%x] cmd=%p\n",
3065 cmd->atio.u.isp24.fcp_cmnd.cdb[0], lba,
3066 a_ref_tag, e_ref_tag, a_app_tag, e_app_tag,
3067 a_guard, e_guard, cmd);
3075 /* If hardware_lock held on entry, might drop it, then reaquire */
3076 /* This function sends the appropriate CTIO to ISP 2xxx or 24xx */
3077 static int __qlt_send_term_imm_notif(struct scsi_qla_host *vha,
3078 struct imm_ntfy_from_isp *ntfy)
3080 struct nack_to_isp *nack;
3081 struct qla_hw_data *ha = vha->hw;
3085 ql_dbg(ql_dbg_tgt_tmr, vha, 0xe01c,
3086 "Sending TERM ELS CTIO (ha=%p)\n", ha);
3088 pkt = (request_t *)qla2x00_alloc_iocbs_ready(vha, NULL);
3090 ql_dbg(ql_dbg_tgt, vha, 0xe080,
3091 "qla_target(%d): %s failed: unable to allocate "
3092 "request packet\n", vha->vp_idx, __func__);
3096 pkt->entry_type = NOTIFY_ACK_TYPE;
3097 pkt->entry_count = 1;
3098 pkt->handle = QLA_TGT_SKIP_HANDLE;
3100 nack = (struct nack_to_isp *)pkt;
3101 nack->ox_id = ntfy->ox_id;
3103 nack->u.isp24.nport_handle = ntfy->u.isp24.nport_handle;
3104 if (le16_to_cpu(ntfy->u.isp24.status) == IMM_NTFY_ELS) {
3105 nack->u.isp24.flags = ntfy->u.isp24.flags &
3106 __constant_cpu_to_le32(NOTIFY24XX_FLAGS_PUREX_IOCB);
3110 nack->u.isp24.flags |=
3111 __constant_cpu_to_le16(NOTIFY_ACK_FLAGS_TERMINATE);
3113 nack->u.isp24.srr_rx_id = ntfy->u.isp24.srr_rx_id;
3114 nack->u.isp24.status = ntfy->u.isp24.status;
3115 nack->u.isp24.status_subcode = ntfy->u.isp24.status_subcode;
3116 nack->u.isp24.fw_handle = ntfy->u.isp24.fw_handle;
3117 nack->u.isp24.exchange_address = ntfy->u.isp24.exchange_address;
3118 nack->u.isp24.srr_rel_offs = ntfy->u.isp24.srr_rel_offs;
3119 nack->u.isp24.srr_ui = ntfy->u.isp24.srr_ui;
3120 nack->u.isp24.vp_index = ntfy->u.isp24.vp_index;
3122 qla2x00_start_iocbs(vha, vha->req);
3126 static void qlt_send_term_imm_notif(struct scsi_qla_host *vha,
3127 struct imm_ntfy_from_isp *imm, int ha_locked)
3129 unsigned long flags = 0;
3132 if (qlt_issue_marker(vha, ha_locked) < 0)
3136 rc = __qlt_send_term_imm_notif(vha, imm);
3140 qlt_alloc_qfull_cmd(vha, imm, 0, 0);
3148 spin_lock_irqsave(&vha->hw->hardware_lock, flags);
3149 rc = __qlt_send_term_imm_notif(vha, imm);
3153 qlt_alloc_qfull_cmd(vha, imm, 0, 0);
3158 spin_unlock_irqrestore(&vha->hw->hardware_lock, flags);
3161 /* If hardware_lock held on entry, might drop it, then reaquire */
3162 /* This function sends the appropriate CTIO to ISP 2xxx or 24xx */
3163 static int __qlt_send_term_exchange(struct scsi_qla_host *vha,
3164 struct qla_tgt_cmd *cmd,
3165 struct atio_from_isp *atio)
3167 struct ctio7_to_24xx *ctio24;
3168 struct qla_hw_data *ha = vha->hw;
3173 ql_dbg(ql_dbg_tgt, vha, 0xe01c, "Sending TERM EXCH CTIO (ha=%p)\n", ha);
3175 pkt = (request_t *)qla2x00_alloc_iocbs_ready(vha, NULL);
3177 ql_dbg(ql_dbg_tgt, vha, 0xe050,
3178 "qla_target(%d): %s failed: unable to allocate "
3179 "request packet\n", vha->vp_idx, __func__);
3184 if (cmd->state < QLA_TGT_STATE_PROCESSED) {
3185 ql_dbg(ql_dbg_tgt, vha, 0xe051,
3186 "qla_target(%d): Terminating cmd %p with "
3187 "incorrect state %d\n", vha->vp_idx, cmd,
3193 vha->tgt_counters.num_term_xchg_sent++;
3194 pkt->entry_count = 1;
3195 pkt->handle = QLA_TGT_SKIP_HANDLE | CTIO_COMPLETION_HANDLE_MARK;
3197 ctio24 = (struct ctio7_to_24xx *)pkt;
3198 ctio24->entry_type = CTIO_TYPE7;
3199 ctio24->nport_handle = CTIO7_NHANDLE_UNRECOGNIZED;
3200 ctio24->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
3201 ctio24->vp_index = vha->vp_idx;
3202 ctio24->initiator_id[0] = atio->u.isp24.fcp_hdr.s_id[2];
3203 ctio24->initiator_id[1] = atio->u.isp24.fcp_hdr.s_id[1];
3204 ctio24->initiator_id[2] = atio->u.isp24.fcp_hdr.s_id[0];
3205 ctio24->exchange_addr = atio->u.isp24.exchange_addr;
3206 ctio24->u.status1.flags = (atio->u.isp24.attr << 9) |
3207 cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_1 |
3208 CTIO7_FLAGS_TERMINATE);
3209 temp = be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id);
3210 ctio24->u.status1.ox_id = cpu_to_le16(temp);
3212 /* Most likely, it isn't needed */
3213 ctio24->u.status1.residual = get_unaligned((uint32_t *)
3214 &atio->u.isp24.fcp_cmnd.add_cdb[
3215 atio->u.isp24.fcp_cmnd.add_cdb_len]);
3216 if (ctio24->u.status1.residual != 0)
3217 ctio24->u.status1.scsi_status |= SS_RESIDUAL_UNDER;
3219 /* Memory Barrier */
3221 qla2x00_start_iocbs(vha, vha->req);
3225 static void qlt_send_term_exchange(struct scsi_qla_host *vha,
3226 struct qla_tgt_cmd *cmd, struct atio_from_isp *atio, int ha_locked,
3229 unsigned long flags = 0;
3232 if (qlt_issue_marker(vha, ha_locked) < 0)
3236 rc = __qlt_send_term_exchange(vha, cmd, atio);
3238 qlt_alloc_qfull_cmd(vha, atio, 0, 0);
3241 spin_lock_irqsave(&vha->hw->hardware_lock, flags);
3242 rc = __qlt_send_term_exchange(vha, cmd, atio);
3244 qlt_alloc_qfull_cmd(vha, atio, 0, 0);
3247 if (cmd && !ul_abort && !cmd->aborted) {
3249 qlt_unmap_sg(vha, cmd);
3250 vha->hw->tgt.tgt_ops->free_cmd(cmd);
3254 spin_unlock_irqrestore(&vha->hw->hardware_lock, flags);
3259 static void qlt_init_term_exchange(struct scsi_qla_host *vha)
3261 struct list_head free_list;
3262 struct qla_tgt_cmd *cmd, *tcmd;
3264 vha->hw->tgt.leak_exchg_thresh_hold =
3265 (vha->hw->cur_fw_xcb_count/100) * LEAK_EXCHG_THRESH_HOLD_PERCENT;
3268 if (!list_empty(&vha->hw->tgt.q_full_list)) {
3269 INIT_LIST_HEAD(&free_list);
3270 list_splice_init(&vha->hw->tgt.q_full_list, &free_list);
3272 list_for_each_entry_safe(cmd, tcmd, &free_list, cmd_list) {
3273 list_del(&cmd->cmd_list);
3274 /* This cmd was never sent to TCM. There is no need
3275 * to schedule free or call free_cmd
3278 vha->hw->tgt.num_qfull_cmds_alloc--;
3281 vha->hw->tgt.num_qfull_cmds_dropped = 0;
3284 static void qlt_chk_exch_leak_thresh_hold(struct scsi_qla_host *vha)
3286 uint32_t total_leaked;
3288 total_leaked = vha->hw->tgt.num_qfull_cmds_dropped;
3290 if (vha->hw->tgt.leak_exchg_thresh_hold &&
3291 (total_leaked > vha->hw->tgt.leak_exchg_thresh_hold)) {
3293 ql_dbg(ql_dbg_tgt, vha, 0xe079,
3294 "Chip reset due to exchange starvation: %d/%d.\n",
3295 total_leaked, vha->hw->cur_fw_xcb_count);
3297 if (IS_P3P_TYPE(vha->hw))
3298 set_bit(FCOE_CTX_RESET_NEEDED, &vha->dpc_flags);
3300 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
3301 qla2xxx_wake_dpc(vha);
3306 int qlt_abort_cmd(struct qla_tgt_cmd *cmd)
3308 struct qla_tgt *tgt = cmd->tgt;
3309 struct scsi_qla_host *vha = tgt->vha;
3310 struct se_cmd *se_cmd = &cmd->se_cmd;
3311 unsigned long flags;
3313 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf014,
3314 "qla_target(%d): terminating exchange for aborted cmd=%p "
3315 "(se_cmd=%p, tag=%llu)", vha->vp_idx, cmd, &cmd->se_cmd,
3318 spin_lock_irqsave(&cmd->cmd_lock, flags);
3320 spin_unlock_irqrestore(&cmd->cmd_lock, flags);
3322 * It's normal to see 2 calls in this path:
3323 * 1) XFER Rdy completion + CMD_T_ABORT
3324 * 2) TCM TMR - drain_state_list
3326 ql_dbg(ql_dbg_tgt_mgt, vha, 0xffff,
3327 "multiple abort. %p transport_state %x, t_state %x,"
3328 " se_cmd_flags %x \n", cmd, cmd->se_cmd.transport_state,
3329 cmd->se_cmd.t_state,cmd->se_cmd.se_cmd_flags);
3333 cmd->trc_flags |= TRC_ABORT;
3334 spin_unlock_irqrestore(&cmd->cmd_lock, flags);
3336 qlt_send_term_exchange(vha, cmd, &cmd->atio, 0, 1);
3339 EXPORT_SYMBOL(qlt_abort_cmd);
3341 void qlt_free_cmd(struct qla_tgt_cmd *cmd)
3343 struct fc_port *sess = cmd->sess;
3345 ql_dbg(ql_dbg_tgt, cmd->vha, 0xe074,
3346 "%s: se_cmd[%p] ox_id %04x\n",
3347 __func__, &cmd->se_cmd,
3348 be16_to_cpu(cmd->atio.u.isp24.fcp_hdr.ox_id));
3350 BUG_ON(cmd->cmd_in_wq);
3353 qlt_unmap_sg(cmd->vha, cmd);
3356 qlt_decr_num_pend_cmds(cmd->vha);
3358 BUG_ON(cmd->sg_mapped);
3359 cmd->jiffies_at_free = get_jiffies_64();
3360 if (unlikely(cmd->free_sg))
3363 if (!sess || !sess->se_sess) {
3367 cmd->jiffies_at_free = get_jiffies_64();
3368 percpu_ida_free(&sess->se_sess->sess_tag_pool, cmd->se_cmd.map_tag);
3370 EXPORT_SYMBOL(qlt_free_cmd);
3373 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
3375 static int qlt_term_ctio_exchange(struct scsi_qla_host *vha, void *ctio,
3376 struct qla_tgt_cmd *cmd, uint32_t status)
3381 struct ctio7_from_24xx *c = (struct ctio7_from_24xx *)ctio;
3383 cpu_to_le16(OF_TERM_EXCH));
3388 qlt_send_term_exchange(vha, cmd, &cmd->atio, 1, 0);
3393 /* ha->hardware_lock supposed to be held on entry */
3394 static inline struct qla_tgt_cmd *qlt_get_cmd(struct scsi_qla_host *vha,
3397 struct qla_hw_data *ha = vha->hw;
3400 if (ha->tgt.cmds[handle] != NULL) {
3401 struct qla_tgt_cmd *cmd = ha->tgt.cmds[handle];
3402 ha->tgt.cmds[handle] = NULL;
3408 /* ha->hardware_lock supposed to be held on entry */
3409 static struct qla_tgt_cmd *qlt_ctio_to_cmd(struct scsi_qla_host *vha,
3410 uint32_t handle, void *ctio)
3412 struct qla_tgt_cmd *cmd = NULL;
3414 /* Clear out internal marks */
3415 handle &= ~(CTIO_COMPLETION_HANDLE_MARK |
3416 CTIO_INTERMEDIATE_HANDLE_MARK);
3418 if (handle != QLA_TGT_NULL_HANDLE) {
3419 if (unlikely(handle == QLA_TGT_SKIP_HANDLE))
3422 /* handle-1 is actually used */
3423 if (unlikely(handle > DEFAULT_OUTSTANDING_COMMANDS)) {
3424 ql_dbg(ql_dbg_tgt, vha, 0xe052,
3425 "qla_target(%d): Wrong handle %x received\n",
3426 vha->vp_idx, handle);
3429 cmd = qlt_get_cmd(vha, handle);
3430 if (unlikely(cmd == NULL)) {
3431 ql_dbg(ql_dbg_tgt, vha, 0xe053,
3432 "qla_target(%d): Suspicious: unable to "
3433 "find the command with handle %x\n", vha->vp_idx,
3437 } else if (ctio != NULL) {
3438 /* We can't get loop ID from CTIO7 */
3439 ql_dbg(ql_dbg_tgt, vha, 0xe054,
3440 "qla_target(%d): Wrong CTIO received: QLA24xx doesn't "
3441 "support NULL handles\n", vha->vp_idx);
3448 /* hardware_lock should be held by caller. */
3450 qlt_abort_cmd_on_host_reset(struct scsi_qla_host *vha, struct qla_tgt_cmd *cmd)
3452 struct qla_hw_data *ha = vha->hw;
3456 qlt_unmap_sg(vha, cmd);
3458 handle = qlt_make_handle(vha);
3460 /* TODO: fix debug message type and ids. */
3461 if (cmd->state == QLA_TGT_STATE_PROCESSED) {
3462 ql_dbg(ql_dbg_io, vha, 0xff00,
3463 "HOST-ABORT: handle=%d, state=PROCESSED.\n", handle);
3464 } else if (cmd->state == QLA_TGT_STATE_NEED_DATA) {
3465 cmd->write_data_transferred = 0;
3466 cmd->state = QLA_TGT_STATE_DATA_IN;
3468 ql_dbg(ql_dbg_io, vha, 0xff01,
3469 "HOST-ABORT: handle=%d, state=DATA_IN.\n", handle);
3471 ha->tgt.tgt_ops->handle_data(cmd);
3474 ql_dbg(ql_dbg_io, vha, 0xff03,
3475 "HOST-ABORT: handle=%d, state=BAD(%d).\n", handle,
3480 cmd->trc_flags |= TRC_FLUSH;
3481 ha->tgt.tgt_ops->free_cmd(cmd);
3485 qlt_host_reset_handler(struct qla_hw_data *ha)
3487 struct qla_tgt_cmd *cmd;
3488 unsigned long flags;
3489 scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
3490 scsi_qla_host_t *vha = NULL;
3491 struct qla_tgt *tgt = base_vha->vha_tgt.qla_tgt;
3494 if (!base_vha->hw->tgt.tgt_ops)
3497 if (!tgt || qla_ini_mode_enabled(base_vha)) {
3498 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf003,
3499 "Target mode disabled\n");
3503 ql_dbg(ql_dbg_tgt_mgt, vha, 0xff10,
3504 "HOST-ABORT-HNDLR: base_vha->dpc_flags=%lx.\n",
3505 base_vha->dpc_flags);
3507 spin_lock_irqsave(&ha->hardware_lock, flags);
3508 for (i = 1; i < DEFAULT_OUTSTANDING_COMMANDS + 1; i++) {
3509 cmd = qlt_get_cmd(base_vha, i);
3512 /* ha->tgt.cmds entry is cleared by qlt_get_cmd. */
3514 qlt_abort_cmd_on_host_reset(vha, cmd);
3516 spin_unlock_irqrestore(&ha->hardware_lock, flags);
3521 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
3523 static void qlt_do_ctio_completion(struct scsi_qla_host *vha, uint32_t handle,
3524 uint32_t status, void *ctio)
3526 struct qla_hw_data *ha = vha->hw;
3527 struct se_cmd *se_cmd;
3528 struct qla_tgt_cmd *cmd;
3530 if (handle & CTIO_INTERMEDIATE_HANDLE_MARK) {
3531 /* That could happen only in case of an error/reset/abort */
3532 if (status != CTIO_SUCCESS) {
3533 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01d,
3534 "Intermediate CTIO received"
3535 " (status %x)\n", status);
3540 cmd = qlt_ctio_to_cmd(vha, handle, ctio);
3544 se_cmd = &cmd->se_cmd;
3545 cmd->cmd_sent_to_fw = 0;
3547 qlt_unmap_sg(vha, cmd);
3549 if (unlikely(status != CTIO_SUCCESS)) {
3550 switch (status & 0xFFFF) {
3551 case CTIO_LIP_RESET:
3552 case CTIO_TARGET_RESET:
3554 /* driver request abort via Terminate exchange */
3556 case CTIO_INVALID_RX_ID:
3558 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf058,
3559 "qla_target(%d): CTIO with "
3560 "status %#x received, state %x, se_cmd %p, "
3561 "(LIP_RESET=e, ABORTED=2, TARGET_RESET=17, "
3562 "TIMEOUT=b, INVALID_RX_ID=8)\n", vha->vp_idx,
3563 status, cmd->state, se_cmd);
3566 case CTIO_PORT_LOGGED_OUT:
3567 case CTIO_PORT_UNAVAILABLE:
3570 (status & 0xFFFF) == CTIO_PORT_LOGGED_OUT;
3572 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf059,
3573 "qla_target(%d): CTIO with %s status %x "
3574 "received (state %x, se_cmd %p)\n", vha->vp_idx,
3575 logged_out ? "PORT LOGGED OUT" : "PORT UNAVAILABLE",
3576 status, cmd->state, se_cmd);
3578 if (logged_out && cmd->sess) {
3580 * Session is already logged out, but we need
3581 * to notify initiator, who's not aware of this
3583 cmd->sess->logout_on_delete = 0;
3584 cmd->sess->send_els_logo = 1;
3585 ql_dbg(ql_dbg_disc, vha, 0xffff,
3586 "%s %d %8phC post del sess\n",
3587 __func__, __LINE__, cmd->sess->port_name);
3589 qlt_schedule_sess_for_deletion_lock(cmd->sess);
3593 case CTIO_DIF_ERROR: {
3594 struct ctio_crc_from_fw *crc =
3595 (struct ctio_crc_from_fw *)ctio;
3596 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf073,
3597 "qla_target(%d): CTIO with DIF_ERROR status %x received (state %x, se_cmd %p) actual_dif[0x%llx] expect_dif[0x%llx]\n",
3598 vha->vp_idx, status, cmd->state, se_cmd,
3599 *((u64 *)&crc->actual_dif[0]),
3600 *((u64 *)&crc->expected_dif[0]));
3602 if (qlt_handle_dif_error(vha, cmd, ctio)) {
3603 if (cmd->state == QLA_TGT_STATE_NEED_DATA) {
3604 /* scsi Write/xfer rdy complete */
3607 /* scsi read/xmit respond complete
3608 * call handle dif to send scsi status
3609 * rather than terminate exchange.
3611 cmd->state = QLA_TGT_STATE_PROCESSED;
3612 ha->tgt.tgt_ops->handle_dif_err(cmd);
3616 /* Need to generate a SCSI good completion.
3617 * because FW did not send scsi status.
3625 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05b,
3626 "qla_target(%d): CTIO with error status 0x%x received (state %x, se_cmd %p\n",
3627 vha->vp_idx, status, cmd->state, se_cmd);
3632 /* "cmd->aborted" means
3633 * cmd is already aborted/terminated, we don't
3634 * need to terminate again. The exchange is already
3635 * cleaned up/freed at FW level. Just cleanup at driver
3638 if ((cmd->state != QLA_TGT_STATE_NEED_DATA) &&
3640 cmd->trc_flags |= TRC_CTIO_ERR;
3641 if (qlt_term_ctio_exchange(vha, ctio, cmd, status))
3647 if (cmd->state == QLA_TGT_STATE_PROCESSED) {
3648 cmd->trc_flags |= TRC_CTIO_DONE;
3649 } else if (cmd->state == QLA_TGT_STATE_NEED_DATA) {
3650 cmd->state = QLA_TGT_STATE_DATA_IN;
3652 if (status == CTIO_SUCCESS)
3653 cmd->write_data_transferred = 1;
3655 ha->tgt.tgt_ops->handle_data(cmd);
3657 } else if (cmd->aborted) {
3658 cmd->trc_flags |= TRC_CTIO_ABORTED;
3659 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01e,
3660 "Aborted command %p (tag %lld) finished\n", cmd, se_cmd->tag);
3662 cmd->trc_flags |= TRC_CTIO_STRANGE;
3663 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05c,
3664 "qla_target(%d): A command in state (%d) should "
3665 "not return a CTIO complete\n", vha->vp_idx, cmd->state);
3668 if (unlikely(status != CTIO_SUCCESS) &&
3670 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01f, "Finishing failed CTIO\n");
3674 ha->tgt.tgt_ops->free_cmd(cmd);
3677 static inline int qlt_get_fcp_task_attr(struct scsi_qla_host *vha,
3682 switch (task_codes) {
3683 case ATIO_SIMPLE_QUEUE:
3684 fcp_task_attr = TCM_SIMPLE_TAG;
3686 case ATIO_HEAD_OF_QUEUE:
3687 fcp_task_attr = TCM_HEAD_TAG;
3689 case ATIO_ORDERED_QUEUE:
3690 fcp_task_attr = TCM_ORDERED_TAG;
3692 case ATIO_ACA_QUEUE:
3693 fcp_task_attr = TCM_ACA_TAG;
3696 fcp_task_attr = TCM_SIMPLE_TAG;
3699 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05d,
3700 "qla_target: unknown task code %x, use ORDERED instead\n",
3702 fcp_task_attr = TCM_ORDERED_TAG;
3706 return fcp_task_attr;
3709 static struct fc_port *qlt_make_local_sess(struct scsi_qla_host *,
3712 * Process context for I/O path into tcm_qla2xxx code
3714 static void __qlt_do_work(struct qla_tgt_cmd *cmd)
3716 scsi_qla_host_t *vha = cmd->vha;
3717 struct qla_hw_data *ha = vha->hw;
3718 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
3719 struct fc_port *sess = cmd->sess;
3720 struct atio_from_isp *atio = &cmd->atio;
3722 unsigned long flags;
3723 uint32_t data_length;
3724 int ret, fcp_task_attr, data_dir, bidi = 0;
3727 cmd->trc_flags |= TRC_DO_WORK;
3732 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf082,
3733 "cmd with tag %u is aborted\n",
3734 cmd->atio.u.isp24.exchange_addr);
3738 spin_lock_init(&cmd->cmd_lock);
3739 cdb = &atio->u.isp24.fcp_cmnd.cdb[0];
3740 cmd->se_cmd.tag = atio->u.isp24.exchange_addr;
3741 cmd->unpacked_lun = scsilun_to_int(
3742 (struct scsi_lun *)&atio->u.isp24.fcp_cmnd.lun);
3744 if (atio->u.isp24.fcp_cmnd.rddata &&
3745 atio->u.isp24.fcp_cmnd.wrdata) {
3747 data_dir = DMA_TO_DEVICE;
3748 } else if (atio->u.isp24.fcp_cmnd.rddata)
3749 data_dir = DMA_FROM_DEVICE;
3750 else if (atio->u.isp24.fcp_cmnd.wrdata)
3751 data_dir = DMA_TO_DEVICE;
3753 data_dir = DMA_NONE;
3755 fcp_task_attr = qlt_get_fcp_task_attr(vha,
3756 atio->u.isp24.fcp_cmnd.task_attr);
3757 data_length = be32_to_cpu(get_unaligned((uint32_t *)
3758 &atio->u.isp24.fcp_cmnd.add_cdb[
3759 atio->u.isp24.fcp_cmnd.add_cdb_len]));
3761 ret = ha->tgt.tgt_ops->handle_cmd(vha, cmd, cdb, data_length,
3762 fcp_task_attr, data_dir, bidi);
3766 * Drop extra session reference from qla_tgt_handle_cmd_for_atio*(
3768 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
3769 ha->tgt.tgt_ops->put_sess(sess);
3770 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
3774 ql_dbg(ql_dbg_io, vha, 0x3060, "Terminating work cmd %p", cmd);
3776 * cmd has not sent to target yet, so pass NULL as the second
3777 * argument to qlt_send_term_exchange() and free the memory here.
3779 cmd->trc_flags |= TRC_DO_WORK_ERR;
3780 spin_lock_irqsave(&ha->hardware_lock, flags);
3781 qlt_send_term_exchange(vha, NULL, &cmd->atio, 1, 0);
3783 qlt_decr_num_pend_cmds(vha);
3784 percpu_ida_free(&sess->se_sess->sess_tag_pool, cmd->se_cmd.map_tag);
3785 spin_unlock_irqrestore(&ha->hardware_lock, flags);
3787 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
3788 ha->tgt.tgt_ops->put_sess(sess);
3789 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
3792 static void qlt_do_work(struct work_struct *work)
3794 struct qla_tgt_cmd *cmd = container_of(work, struct qla_tgt_cmd, work);
3795 scsi_qla_host_t *vha = cmd->vha;
3796 unsigned long flags;
3798 spin_lock_irqsave(&vha->cmd_list_lock, flags);
3799 list_del(&cmd->cmd_list);
3800 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
3805 static struct qla_tgt_cmd *qlt_get_tag(scsi_qla_host_t *vha,
3806 struct fc_port *sess,
3807 struct atio_from_isp *atio)
3809 struct se_session *se_sess = sess->se_sess;
3810 struct qla_tgt_cmd *cmd;
3813 tag = percpu_ida_alloc(&se_sess->sess_tag_pool, TASK_RUNNING);
3817 cmd = &((struct qla_tgt_cmd *)se_sess->sess_cmd_map)[tag];
3818 memset(cmd, 0, sizeof(struct qla_tgt_cmd));
3820 memcpy(&cmd->atio, atio, sizeof(*atio));
3821 cmd->state = QLA_TGT_STATE_NEW;
3822 cmd->tgt = vha->vha_tgt.qla_tgt;
3823 qlt_incr_num_pend_cmds(vha);
3825 cmd->se_cmd.map_tag = tag;
3827 cmd->loop_id = sess->loop_id;
3828 cmd->conf_compl_supported = sess->conf_compl_supported;
3831 cmd->jiffies_at_alloc = get_jiffies_64();
3833 cmd->reset_count = vha->hw->chip_reset;
3838 static void qlt_send_busy(struct scsi_qla_host *, struct atio_from_isp *,
3841 static void qlt_create_sess_from_atio(struct work_struct *work)
3843 struct qla_tgt_sess_op *op = container_of(work,
3844 struct qla_tgt_sess_op, work);
3845 scsi_qla_host_t *vha = op->vha;
3846 struct qla_hw_data *ha = vha->hw;
3847 struct fc_port *sess;
3848 struct qla_tgt_cmd *cmd;
3849 unsigned long flags;
3850 uint8_t *s_id = op->atio.u.isp24.fcp_hdr.s_id;
3852 spin_lock_irqsave(&vha->cmd_list_lock, flags);
3853 list_del(&op->cmd_list);
3854 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
3857 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf083,
3858 "sess_op with tag %u is aborted\n",
3859 op->atio.u.isp24.exchange_addr);
3863 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf022,
3864 "qla_target(%d): Unable to find wwn login"
3865 " (s_id %x:%x:%x), trying to create it manually\n",
3866 vha->vp_idx, s_id[0], s_id[1], s_id[2]);
3868 if (op->atio.u.raw.entry_count > 1) {
3869 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf023,
3870 "Dropping multy entry atio %p\n", &op->atio);
3874 sess = qlt_make_local_sess(vha, s_id);
3875 /* sess has an extra creation ref. */
3880 * Now obtain a pre-allocated session tag using the original op->atio
3881 * packet header, and dispatch into __qlt_do_work() using the existing
3884 cmd = qlt_get_tag(vha, sess, &op->atio);
3886 spin_lock_irqsave(&ha->hardware_lock, flags);
3887 qlt_send_busy(vha, &op->atio, SAM_STAT_BUSY);
3888 ha->tgt.tgt_ops->put_sess(sess);
3889 spin_unlock_irqrestore(&ha->hardware_lock, flags);
3895 * __qlt_do_work() will call qlt_put_sess() to release
3896 * the extra reference taken above by qlt_make_local_sess()
3902 spin_lock_irqsave(&ha->hardware_lock, flags);
3903 qlt_send_term_exchange(vha, NULL, &op->atio, 1, 0);
3904 spin_unlock_irqrestore(&ha->hardware_lock, flags);
3908 /* ha->hardware_lock supposed to be held on entry */
3909 static int qlt_handle_cmd_for_atio(struct scsi_qla_host *vha,
3910 struct atio_from_isp *atio)
3912 struct qla_hw_data *ha = vha->hw;
3913 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
3914 struct fc_port *sess;
3915 struct qla_tgt_cmd *cmd;
3916 unsigned long flags;
3918 if (unlikely(tgt->tgt_stop)) {
3919 ql_dbg(ql_dbg_io, vha, 0x3061,
3920 "New command while device %p is shutting down\n", tgt);
3924 sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha, atio->u.isp24.fcp_hdr.s_id);
3925 if (unlikely(!sess)) {
3926 struct qla_tgt_sess_op *op = kzalloc(sizeof(struct qla_tgt_sess_op),
3931 memcpy(&op->atio, atio, sizeof(*atio));
3934 spin_lock(&vha->cmd_list_lock);
3935 list_add_tail(&op->cmd_list, &vha->qla_sess_op_cmd_list);
3936 spin_unlock(&vha->cmd_list_lock);
3938 INIT_WORK(&op->work, qlt_create_sess_from_atio);
3939 queue_work(qla_tgt_wq, &op->work);
3943 /* Another WWN used to have our s_id. Our PLOGI scheduled its
3944 * session deletion, but it's still in sess_del_work wq */
3945 if (sess->deleted) {
3946 ql_dbg(ql_dbg_io, vha, 0x3061,
3947 "New command while old session %p is being deleted\n",
3953 * Do kref_get() before returning + dropping qla_hw_data->hardware_lock.
3955 if (!kref_get_unless_zero(&sess->sess_kref)) {
3956 ql_dbg(ql_dbg_tgt, vha, 0xffff,
3957 "%s: kref_get fail, %8phC oxid %x \n",
3958 __func__, sess->port_name,
3959 be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id));
3963 cmd = qlt_get_tag(vha, sess, atio);
3965 ql_dbg(ql_dbg_io, vha, 0x3062,
3966 "qla_target(%d): Allocation of cmd failed\n", vha->vp_idx);
3967 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
3968 ha->tgt.tgt_ops->put_sess(sess);
3969 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
3974 cmd->trc_flags |= TRC_NEW_CMD;
3975 cmd->se_cmd.cpuid = ha->msix_count ?
3976 ha->tgt.rspq_vector_cpuid : WORK_CPU_UNBOUND;
3978 spin_lock_irqsave(&vha->cmd_list_lock, flags);
3979 list_add_tail(&cmd->cmd_list, &vha->qla_cmd_list);
3980 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
3982 INIT_WORK(&cmd->work, qlt_do_work);
3983 if (ha->msix_count) {
3984 if (cmd->atio.u.isp24.fcp_cmnd.rddata)
3985 queue_work_on(smp_processor_id(), qla_tgt_wq,
3988 queue_work_on(cmd->se_cmd.cpuid, qla_tgt_wq,
3991 queue_work(qla_tgt_wq, &cmd->work);
3997 /* ha->hardware_lock supposed to be held on entry */
3998 static int qlt_issue_task_mgmt(struct fc_port *sess, u64 lun,
3999 int fn, void *iocb, int flags)
4001 struct scsi_qla_host *vha = sess->vha;
4002 struct qla_hw_data *ha = vha->hw;
4003 struct qla_tgt_mgmt_cmd *mcmd;
4004 struct atio_from_isp *a = (struct atio_from_isp *)iocb;
4007 mcmd = mempool_alloc(qla_tgt_mgmt_cmd_mempool, GFP_ATOMIC);
4009 ql_dbg(ql_dbg_tgt_tmr, vha, 0x10009,
4010 "qla_target(%d): Allocation of management "
4011 "command failed, some commands and their data could "
4012 "leak\n", vha->vp_idx);
4015 memset(mcmd, 0, sizeof(*mcmd));
4019 memcpy(&mcmd->orig_iocb.imm_ntfy, iocb,
4020 sizeof(mcmd->orig_iocb.imm_ntfy));
4022 mcmd->tmr_func = fn;
4023 mcmd->flags = flags;
4024 mcmd->reset_count = vha->hw->chip_reset;
4027 case QLA_TGT_LUN_RESET:
4028 abort_cmds_for_lun(vha, lun, a->u.isp24.fcp_hdr.s_id);
4032 res = ha->tgt.tgt_ops->handle_tmr(mcmd, lun, mcmd->tmr_func, 0);
4034 ql_dbg(ql_dbg_tgt_tmr, vha, 0x1000b,
4035 "qla_target(%d): tgt.tgt_ops->handle_tmr() failed: %d\n",
4036 sess->vha->vp_idx, res);
4037 mempool_free(mcmd, qla_tgt_mgmt_cmd_mempool);
4044 /* ha->hardware_lock supposed to be held on entry */
4045 static int qlt_handle_task_mgmt(struct scsi_qla_host *vha, void *iocb)
4047 struct atio_from_isp *a = (struct atio_from_isp *)iocb;
4048 struct qla_hw_data *ha = vha->hw;
4049 struct qla_tgt *tgt;
4050 struct fc_port *sess;
4051 uint32_t lun, unpacked_lun;
4053 unsigned long flags;
4055 tgt = vha->vha_tgt.qla_tgt;
4057 lun = a->u.isp24.fcp_cmnd.lun;
4058 fn = a->u.isp24.fcp_cmnd.task_mgmt_flags;
4060 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
4061 sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha,
4062 a->u.isp24.fcp_hdr.s_id);
4063 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
4065 unpacked_lun = scsilun_to_int((struct scsi_lun *)&lun);
4068 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf024,
4069 "qla_target(%d): task mgmt fn 0x%x for "
4070 "non-existant session\n", vha->vp_idx, fn);
4071 return qlt_sched_sess_work(tgt, QLA_TGT_SESS_WORK_TM, iocb,
4072 sizeof(struct atio_from_isp));
4078 return qlt_issue_task_mgmt(sess, unpacked_lun, fn, iocb, 0);
4081 /* ha->hardware_lock supposed to be held on entry */
4082 static int __qlt_abort_task(struct scsi_qla_host *vha,
4083 struct imm_ntfy_from_isp *iocb, struct fc_port *sess)
4085 struct atio_from_isp *a = (struct atio_from_isp *)iocb;
4086 struct qla_hw_data *ha = vha->hw;
4087 struct qla_tgt_mgmt_cmd *mcmd;
4088 uint32_t lun, unpacked_lun;
4091 mcmd = mempool_alloc(qla_tgt_mgmt_cmd_mempool, GFP_ATOMIC);
4093 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05f,
4094 "qla_target(%d): %s: Allocation of ABORT cmd failed\n",
4095 vha->vp_idx, __func__);
4098 memset(mcmd, 0, sizeof(*mcmd));
4101 memcpy(&mcmd->orig_iocb.imm_ntfy, iocb,
4102 sizeof(mcmd->orig_iocb.imm_ntfy));
4104 lun = a->u.isp24.fcp_cmnd.lun;
4105 unpacked_lun = scsilun_to_int((struct scsi_lun *)&lun);
4106 mcmd->reset_count = vha->hw->chip_reset;
4107 mcmd->tmr_func = QLA_TGT_2G_ABORT_TASK;
4109 rc = ha->tgt.tgt_ops->handle_tmr(mcmd, unpacked_lun, mcmd->tmr_func,
4110 le16_to_cpu(iocb->u.isp2x.seq_id));
4112 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf060,
4113 "qla_target(%d): tgt_ops->handle_tmr() failed: %d\n",
4115 mempool_free(mcmd, qla_tgt_mgmt_cmd_mempool);
4122 /* ha->hardware_lock supposed to be held on entry */
4123 static int qlt_abort_task(struct scsi_qla_host *vha,
4124 struct imm_ntfy_from_isp *iocb)
4126 struct qla_hw_data *ha = vha->hw;
4127 struct fc_port *sess;
4129 unsigned long flags;
4131 loop_id = GET_TARGET_ID(ha, (struct atio_from_isp *)iocb);
4133 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
4134 sess = ha->tgt.tgt_ops->find_sess_by_loop_id(vha, loop_id);
4135 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
4138 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf025,
4139 "qla_target(%d): task abort for unexisting "
4140 "session\n", vha->vp_idx);
4141 return qlt_sched_sess_work(vha->vha_tgt.qla_tgt,
4142 QLA_TGT_SESS_WORK_ABORT, iocb, sizeof(*iocb));
4145 return __qlt_abort_task(vha, iocb, sess);
4148 void qlt_logo_completion_handler(fc_port_t *fcport, int rc)
4150 if (rc != MBS_COMMAND_COMPLETE) {
4151 ql_dbg(ql_dbg_tgt_mgt, fcport->vha, 0xf093,
4152 "%s: se_sess %p / sess %p from"
4153 " port %8phC loop_id %#04x s_id %02x:%02x:%02x"
4154 " LOGO failed: %#x\n",
4158 fcport->port_name, fcport->loop_id,
4159 fcport->d_id.b.domain, fcport->d_id.b.area,
4160 fcport->d_id.b.al_pa, rc);
4163 fcport->logout_completed = 1;
4167 * ha->hardware_lock supposed to be held on entry (to protect tgt->sess_list)
4169 * Schedules sessions with matching port_id/loop_id but different wwn for
4170 * deletion. Returns existing session with matching wwn if present.
4174 qlt_find_sess_invalidate_other(scsi_qla_host_t *vha, uint64_t wwn,
4175 port_id_t port_id, uint16_t loop_id, struct fc_port **conflict_sess)
4177 struct fc_port *sess = NULL, *other_sess;
4180 *conflict_sess = NULL;
4182 list_for_each_entry(other_sess, &vha->vp_fcports, list) {
4184 other_wwn = wwn_to_u64(other_sess->port_name);
4186 if (wwn == other_wwn) {
4192 /* find other sess with nport_id collision */
4193 if (port_id.b24 == other_sess->d_id.b24) {
4194 if (loop_id != other_sess->loop_id) {
4195 ql_dbg(ql_dbg_tgt_tmr, vha, 0x1000c,
4196 "Invalidating sess %p loop_id %d wwn %llx.\n",
4197 other_sess, other_sess->loop_id, other_wwn);
4200 * logout_on_delete is set by default, but another
4201 * session that has the same s_id/loop_id combo
4202 * might have cleared it when requested this session
4203 * deletion, so don't touch it
4205 qlt_schedule_sess_for_deletion(other_sess, true);
4208 * Another wwn used to have our s_id/loop_id
4209 * kill the session, but don't free the loop_id
4211 ql_dbg(ql_dbg_tgt_tmr, vha, 0xffff,
4212 "Invalidating sess %p loop_id %d wwn %llx.\n",
4213 other_sess, other_sess->loop_id, other_wwn);
4216 other_sess->keep_nport_handle = 1;
4217 *conflict_sess = other_sess;
4218 qlt_schedule_sess_for_deletion(other_sess,
4224 /* find other sess with nport handle collision */
4225 if ((loop_id == other_sess->loop_id) &&
4226 (loop_id != FC_NO_LOOP_ID)) {
4227 ql_dbg(ql_dbg_tgt_tmr, vha, 0x1000d,
4228 "Invalidating sess %p loop_id %d wwn %llx.\n",
4229 other_sess, other_sess->loop_id, other_wwn);
4231 /* Same loop_id but different s_id
4232 * Ok to kill and logout */
4233 qlt_schedule_sess_for_deletion(other_sess, true);
4240 /* Abort any commands for this s_id waiting on qla_tgt_wq workqueue */
4241 static int abort_cmds_for_s_id(struct scsi_qla_host *vha, port_id_t *s_id)
4243 struct qla_tgt_sess_op *op;
4244 struct qla_tgt_cmd *cmd;
4248 key = (((u32)s_id->b.domain << 16) |
4249 ((u32)s_id->b.area << 8) |
4250 ((u32)s_id->b.al_pa));
4252 spin_lock(&vha->cmd_list_lock);
4253 list_for_each_entry(op, &vha->qla_sess_op_cmd_list, cmd_list) {
4254 uint32_t op_key = sid_to_key(op->atio.u.isp24.fcp_hdr.s_id);
4255 if (op_key == key) {
4260 list_for_each_entry(cmd, &vha->qla_cmd_list, cmd_list) {
4261 uint32_t cmd_key = sid_to_key(cmd->atio.u.isp24.fcp_hdr.s_id);
4262 if (cmd_key == key) {
4267 spin_unlock(&vha->cmd_list_lock);
4273 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
4275 static int qlt_24xx_handle_els(struct scsi_qla_host *vha,
4276 struct imm_ntfy_from_isp *iocb)
4278 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
4279 struct qla_hw_data *ha = vha->hw;
4280 struct fc_port *sess = NULL, *conflict_sess = NULL;
4286 struct qlt_plogi_ack_t *pla;
4287 unsigned long flags;
4289 wwn = wwn_to_u64(iocb->u.isp24.port_name);
4291 port_id.b.domain = iocb->u.isp24.port_id[2];
4292 port_id.b.area = iocb->u.isp24.port_id[1];
4293 port_id.b.al_pa = iocb->u.isp24.port_id[0];
4294 port_id.b.rsvd_1 = 0;
4296 loop_id = le16_to_cpu(iocb->u.isp24.nport_handle);
4298 ql_dbg(ql_dbg_disc, vha, 0xf026,
4299 "qla_target(%d): Port ID: %02x:%02x:%02x ELS opcode: 0x%02x lid %d %8phC\n",
4300 vha->vp_idx, iocb->u.isp24.port_id[2],
4301 iocb->u.isp24.port_id[1], iocb->u.isp24.port_id[0],
4302 iocb->u.isp24.status_subcode, loop_id,
4303 iocb->u.isp24.port_name);
4305 /* res = 1 means ack at the end of thread
4306 * res = 0 means ack async/later.
4308 switch (iocb->u.isp24.status_subcode) {
4311 /* Mark all stale commands in qla_tgt_wq for deletion */
4312 abort_cmds_for_s_id(vha, &port_id);
4315 spin_lock_irqsave(&tgt->ha->tgt.sess_lock, flags);
4316 sess = qlt_find_sess_invalidate_other(vha, wwn,
4317 port_id, loop_id, &conflict_sess);
4318 spin_unlock_irqrestore(&tgt->ha->tgt.sess_lock, flags);
4321 if (IS_SW_RESV_ADDR(port_id)) {
4326 pla = qlt_plogi_ack_find_add(vha, &port_id, iocb);
4328 qlt_send_term_imm_notif(vha, iocb, 1);
4334 if (conflict_sess) {
4335 conflict_sess->login_gen++;
4336 qlt_plogi_ack_link(vha, pla, conflict_sess,
4337 QLT_PLOGI_LINK_CONFLICT);
4342 qla24xx_post_newsess_work(vha, &port_id,
4343 iocb->u.isp24.port_name, pla);
4348 qlt_plogi_ack_link(vha, pla, sess, QLT_PLOGI_LINK_SAME_WWN);
4349 sess->fw_login_state = DSC_LS_PLOGI_PEND;
4350 sess->d_id = port_id;
4353 switch (sess->disc_state) {
4355 qlt_plogi_ack_unref(vha, pla);
4360 * Under normal circumstances we want to release nport handle
4361 * during LOGO process to avoid nport handle leaks inside FW.
4362 * The exception is when LOGO is done while another PLOGI with
4363 * the same nport handle is waiting as might be the case here.
4364 * Note: there is always a possibily of a race where session
4365 * deletion has already started for other reasons (e.g. ACL
4366 * removal) and now PLOGI arrives:
4367 * 1. if PLOGI arrived in FW after nport handle has been freed,
4368 * FW must have assigned this PLOGI a new/same handle and we
4369 * can proceed ACK'ing it as usual when session deletion
4371 * 2. if PLOGI arrived in FW before LOGO with LCF_FREE_NPORT
4372 * bit reached it, the handle has now been released. We'll
4373 * get an error when we ACK this PLOGI. Nothing will be sent
4374 * back to initiator. Initiator should eventually retry
4375 * PLOGI and situation will correct itself.
4377 sess->keep_nport_handle = ((sess->loop_id == loop_id) &&
4378 (sess->d_id.b24 == port_id.b24));
4380 ql_dbg(ql_dbg_disc, vha, 0xffff,
4381 "%s %d %8phC post del sess\n",
4382 __func__, __LINE__, sess->port_name);
4385 qlt_schedule_sess_for_deletion_lock(sess);
4392 wd3_lo = le16_to_cpu(iocb->u.isp24.u.prli.wd3_lo);
4395 spin_lock_irqsave(&tgt->ha->tgt.sess_lock, flags);
4396 sess = qlt_find_sess_invalidate_other(vha, wwn, port_id,
4397 loop_id, &conflict_sess);
4398 spin_unlock_irqrestore(&tgt->ha->tgt.sess_lock, flags);
4401 if (conflict_sess) {
4402 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf09b,
4403 "PRLI with conflicting sess %p port %8phC\n",
4404 conflict_sess, conflict_sess->port_name);
4405 qlt_send_term_imm_notif(vha, iocb, 1);
4411 if (sess->fw_login_state == DSC_LS_PLOGI_PEND) {
4413 * Impatient initiator sent PRLI before last
4414 * PLOGI could finish. Will force him to re-try,
4415 * while last one finishes.
4417 ql_log(ql_log_warn, sess->vha, 0xf095,
4418 "sess %p PRLI received, before plogi ack.\n",
4420 qlt_send_term_imm_notif(vha, iocb, 1);
4426 * This shouldn't happen under normal circumstances,
4427 * since we have deleted the old session during PLOGI
4429 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf096,
4430 "PRLI (loop_id %#04x) for existing sess %p (loop_id %#04x)\n",
4431 sess->loop_id, sess, iocb->u.isp24.nport_handle);
4434 sess->loop_id = loop_id;
4435 sess->d_id = port_id;
4436 sess->fw_login_state = DSC_LS_PRLI_PEND;
4439 sess->conf_compl_supported = 1;
4441 if ((wd3_lo & BIT_4) == 0)
4442 sess->port_type = FCT_INITIATOR;
4444 sess->port_type = FCT_TARGET;
4446 res = 1; /* send notify ack */
4448 /* Make session global (not used in fabric mode) */
4449 if (ha->current_topology != ISP_CFG_F) {
4450 set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
4451 set_bit(LOCAL_LOOP_UPDATE, &vha->dpc_flags);
4452 qla2xxx_wake_dpc(vha);
4455 ql_dbg(ql_dbg_disc, vha, 0xffff,
4456 "%s %d %8phC post nack\n",
4457 __func__, __LINE__, sess->port_name);
4459 qla24xx_post_nack_work(vha, sess, iocb,
4468 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
4469 sess = qla2x00_find_fcport_by_loopid(vha, loop_id);
4470 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
4474 sess->fw_login_state = DSC_LS_LOGO_PEND;
4475 sess->logout_on_delete = 0;
4476 sess->logo_ack_needed = 1;
4477 memcpy(sess->iocb, iocb, IOCB_SIZE);
4480 res = qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS_SESS);
4482 ql_dbg(ql_dbg_disc, vha, 0xffff,
4483 "%s: logo %llx res %d sess %p ",
4484 __func__, wwn, res, sess);
4486 /* cmd went up to ULP. look for qlt_xmit_tm_rsp()
4491 /* cmd did not go upstair. */
4493 qlt_schedule_sess_for_deletion_lock(sess);
4496 /* else logo will be ack */
4502 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
4503 if (tgt->link_reinit_iocb_pending) {
4504 qlt_send_notify_ack(vha, &tgt->link_reinit_iocb,
4506 tgt->link_reinit_iocb_pending = 0;
4509 sess = qla2x00_find_fcport_by_wwpn(vha,
4510 iocb->u.isp24.port_name, 1);
4512 ql_dbg(ql_dbg_disc, vha, 0xffff,
4513 "sess %p lid %d|%d DS %d LS %d\n",
4514 sess, sess->loop_id, loop_id,
4515 sess->disc_state, sess->fw_login_state);
4518 res = 1; /* send notify ack */
4522 case ELS_FLOGI: /* should never happen */
4524 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf061,
4525 "qla_target(%d): Unsupported ELS command %x "
4526 "received\n", vha->vp_idx, iocb->u.isp24.status_subcode);
4527 res = qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS_SESS);
4535 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
4537 static void qlt_handle_imm_notify(struct scsi_qla_host *vha,
4538 struct imm_ntfy_from_isp *iocb)
4540 struct qla_hw_data *ha = vha->hw;
4541 uint32_t add_flags = 0;
4542 int send_notify_ack = 1;
4545 status = le16_to_cpu(iocb->u.isp2x.status);
4547 case IMM_NTFY_LIP_RESET:
4549 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf032,
4550 "qla_target(%d): LIP reset (loop %#x), subcode %x\n",
4551 vha->vp_idx, le16_to_cpu(iocb->u.isp24.nport_handle),
4552 iocb->u.isp24.status_subcode);
4554 if (qlt_reset(vha, iocb, QLA_TGT_ABORT_ALL) == 0)
4555 send_notify_ack = 0;
4559 case IMM_NTFY_LIP_LINK_REINIT:
4561 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
4562 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf033,
4563 "qla_target(%d): LINK REINIT (loop %#x, "
4564 "subcode %x)\n", vha->vp_idx,
4565 le16_to_cpu(iocb->u.isp24.nport_handle),
4566 iocb->u.isp24.status_subcode);
4567 if (tgt->link_reinit_iocb_pending) {
4568 qlt_send_notify_ack(vha, &tgt->link_reinit_iocb,
4571 memcpy(&tgt->link_reinit_iocb, iocb, sizeof(*iocb));
4572 tgt->link_reinit_iocb_pending = 1;
4574 * QLogic requires to wait after LINK REINIT for possible
4575 * PDISC or ADISC ELS commands
4577 send_notify_ack = 0;
4581 case IMM_NTFY_PORT_LOGOUT:
4582 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf034,
4583 "qla_target(%d): Port logout (loop "
4584 "%#x, subcode %x)\n", vha->vp_idx,
4585 le16_to_cpu(iocb->u.isp24.nport_handle),
4586 iocb->u.isp24.status_subcode);
4588 if (qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS_SESS) == 0)
4589 send_notify_ack = 0;
4590 /* The sessions will be cleared in the callback, if needed */
4593 case IMM_NTFY_GLBL_TPRLO:
4594 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf035,
4595 "qla_target(%d): Global TPRLO (%x)\n", vha->vp_idx, status);
4596 if (qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS) == 0)
4597 send_notify_ack = 0;
4598 /* The sessions will be cleared in the callback, if needed */
4601 case IMM_NTFY_PORT_CONFIG:
4602 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf036,
4603 "qla_target(%d): Port config changed (%x)\n", vha->vp_idx,
4605 if (qlt_reset(vha, iocb, QLA_TGT_ABORT_ALL) == 0)
4606 send_notify_ack = 0;
4607 /* The sessions will be cleared in the callback, if needed */
4610 case IMM_NTFY_GLBL_LOGO:
4611 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06a,
4612 "qla_target(%d): Link failure detected\n",
4614 /* I_T nexus loss */
4615 if (qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS) == 0)
4616 send_notify_ack = 0;
4619 case IMM_NTFY_IOCB_OVERFLOW:
4620 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06b,
4621 "qla_target(%d): Cannot provide requested "
4622 "capability (IOCB overflowed the immediate notify "
4623 "resource count)\n", vha->vp_idx);
4626 case IMM_NTFY_ABORT_TASK:
4627 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf037,
4628 "qla_target(%d): Abort Task (S %08x I %#x -> "
4629 "L %#x)\n", vha->vp_idx,
4630 le16_to_cpu(iocb->u.isp2x.seq_id),
4631 GET_TARGET_ID(ha, (struct atio_from_isp *)iocb),
4632 le16_to_cpu(iocb->u.isp2x.lun));
4633 if (qlt_abort_task(vha, iocb) == 0)
4634 send_notify_ack = 0;
4637 case IMM_NTFY_RESOURCE:
4638 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06c,
4639 "qla_target(%d): Out of resources, host %ld\n",
4640 vha->vp_idx, vha->host_no);
4643 case IMM_NTFY_MSG_RX:
4644 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf038,
4645 "qla_target(%d): Immediate notify task %x\n",
4646 vha->vp_idx, iocb->u.isp2x.task_flags);
4647 if (qlt_handle_task_mgmt(vha, iocb) == 0)
4648 send_notify_ack = 0;
4652 if (qlt_24xx_handle_els(vha, iocb) == 0)
4653 send_notify_ack = 0;
4656 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06d,
4657 "qla_target(%d): Received unknown immediate "
4658 "notify status %x\n", vha->vp_idx, status);
4662 if (send_notify_ack)
4663 qlt_send_notify_ack(vha, iocb, add_flags, 0, 0, 0, 0, 0);
4667 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
4668 * This function sends busy to ISP 2xxx or 24xx.
4670 static int __qlt_send_busy(struct scsi_qla_host *vha,
4671 struct atio_from_isp *atio, uint16_t status)
4673 struct ctio7_to_24xx *ctio24;
4674 struct qla_hw_data *ha = vha->hw;
4676 struct fc_port *sess = NULL;
4677 unsigned long flags;
4679 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
4680 sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha,
4681 atio->u.isp24.fcp_hdr.s_id);
4682 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
4684 qlt_send_term_exchange(vha, NULL, atio, 1, 0);
4687 /* Sending marker isn't necessary, since we called from ISR */
4689 pkt = (request_t *)qla2x00_alloc_iocbs(vha, NULL);
4691 ql_dbg(ql_dbg_io, vha, 0x3063,
4692 "qla_target(%d): %s failed: unable to allocate "
4693 "request packet", vha->vp_idx, __func__);
4697 vha->tgt_counters.num_q_full_sent++;
4698 pkt->entry_count = 1;
4699 pkt->handle = QLA_TGT_SKIP_HANDLE | CTIO_COMPLETION_HANDLE_MARK;
4701 ctio24 = (struct ctio7_to_24xx *)pkt;
4702 ctio24->entry_type = CTIO_TYPE7;
4703 ctio24->nport_handle = sess->loop_id;
4704 ctio24->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
4705 ctio24->vp_index = vha->vp_idx;
4706 ctio24->initiator_id[0] = atio->u.isp24.fcp_hdr.s_id[2];
4707 ctio24->initiator_id[1] = atio->u.isp24.fcp_hdr.s_id[1];
4708 ctio24->initiator_id[2] = atio->u.isp24.fcp_hdr.s_id[0];
4709 ctio24->exchange_addr = atio->u.isp24.exchange_addr;
4710 ctio24->u.status1.flags = (atio->u.isp24.attr << 9) |
4712 CTIO7_FLAGS_STATUS_MODE_1 | CTIO7_FLAGS_SEND_STATUS |
4713 CTIO7_FLAGS_DONT_RET_CTIO);
4715 * CTIO from fw w/o se_cmd doesn't provide enough info to retry it,
4716 * if the explicit conformation is used.
4718 ctio24->u.status1.ox_id = swab16(atio->u.isp24.fcp_hdr.ox_id);
4719 ctio24->u.status1.scsi_status = cpu_to_le16(status);
4720 /* Memory Barrier */
4722 qla2x00_start_iocbs(vha, vha->req);
4727 * This routine is used to allocate a command for either a QFull condition
4728 * (ie reply SAM_STAT_BUSY) or to terminate an exchange that did not go
4732 qlt_alloc_qfull_cmd(struct scsi_qla_host *vha,
4733 struct atio_from_isp *atio, uint16_t status, int qfull)
4735 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
4736 struct qla_hw_data *ha = vha->hw;
4737 struct fc_port *sess;
4738 struct se_session *se_sess;
4739 struct qla_tgt_cmd *cmd;
4742 if (unlikely(tgt->tgt_stop)) {
4743 ql_dbg(ql_dbg_io, vha, 0x300a,
4744 "New command while device %p is shutting down\n", tgt);
4748 if ((vha->hw->tgt.num_qfull_cmds_alloc + 1) > MAX_QFULL_CMDS_ALLOC) {
4749 vha->hw->tgt.num_qfull_cmds_dropped++;
4750 if (vha->hw->tgt.num_qfull_cmds_dropped >
4751 vha->qla_stats.stat_max_qfull_cmds_dropped)
4752 vha->qla_stats.stat_max_qfull_cmds_dropped =
4753 vha->hw->tgt.num_qfull_cmds_dropped;
4755 ql_dbg(ql_dbg_io, vha, 0x3068,
4756 "qla_target(%d): %s: QFull CMD dropped[%d]\n",
4757 vha->vp_idx, __func__,
4758 vha->hw->tgt.num_qfull_cmds_dropped);
4760 qlt_chk_exch_leak_thresh_hold(vha);
4764 sess = ha->tgt.tgt_ops->find_sess_by_s_id
4765 (vha, atio->u.isp24.fcp_hdr.s_id);
4769 se_sess = sess->se_sess;
4771 tag = percpu_ida_alloc(&se_sess->sess_tag_pool, TASK_RUNNING);
4775 cmd = &((struct qla_tgt_cmd *)se_sess->sess_cmd_map)[tag];
4777 ql_dbg(ql_dbg_io, vha, 0x3009,
4778 "qla_target(%d): %s: Allocation of cmd failed\n",
4779 vha->vp_idx, __func__);
4781 vha->hw->tgt.num_qfull_cmds_dropped++;
4782 if (vha->hw->tgt.num_qfull_cmds_dropped >
4783 vha->qla_stats.stat_max_qfull_cmds_dropped)
4784 vha->qla_stats.stat_max_qfull_cmds_dropped =
4785 vha->hw->tgt.num_qfull_cmds_dropped;
4787 qlt_chk_exch_leak_thresh_hold(vha);
4791 memset(cmd, 0, sizeof(struct qla_tgt_cmd));
4793 qlt_incr_num_pend_cmds(vha);
4794 INIT_LIST_HEAD(&cmd->cmd_list);
4795 memcpy(&cmd->atio, atio, sizeof(*atio));
4797 cmd->tgt = vha->vha_tgt.qla_tgt;
4799 cmd->reset_count = vha->hw->chip_reset;
4804 /* NOTE: borrowing the state field to carry the status */
4805 cmd->state = status;
4807 cmd->term_exchg = 1;
4809 list_add_tail(&cmd->cmd_list, &vha->hw->tgt.q_full_list);
4811 vha->hw->tgt.num_qfull_cmds_alloc++;
4812 if (vha->hw->tgt.num_qfull_cmds_alloc >
4813 vha->qla_stats.stat_max_qfull_cmds_alloc)
4814 vha->qla_stats.stat_max_qfull_cmds_alloc =
4815 vha->hw->tgt.num_qfull_cmds_alloc;
4819 qlt_free_qfull_cmds(struct scsi_qla_host *vha)
4821 struct qla_hw_data *ha = vha->hw;
4822 unsigned long flags;
4823 struct qla_tgt_cmd *cmd, *tcmd;
4824 struct list_head free_list;
4827 if (list_empty(&ha->tgt.q_full_list))
4830 INIT_LIST_HEAD(&free_list);
4832 spin_lock_irqsave(&vha->hw->hardware_lock, flags);
4834 if (list_empty(&ha->tgt.q_full_list)) {
4835 spin_unlock_irqrestore(&vha->hw->hardware_lock, flags);
4839 list_for_each_entry_safe(cmd, tcmd, &ha->tgt.q_full_list, cmd_list) {
4841 /* cmd->state is a borrowed field to hold status */
4842 rc = __qlt_send_busy(vha, &cmd->atio, cmd->state);
4843 else if (cmd->term_exchg)
4844 rc = __qlt_send_term_exchange(vha, NULL, &cmd->atio);
4850 ql_dbg(ql_dbg_io, vha, 0x3006,
4851 "%s: busy sent for ox_id[%04x]\n", __func__,
4852 be16_to_cpu(cmd->atio.u.isp24.fcp_hdr.ox_id));
4853 else if (cmd->term_exchg)
4854 ql_dbg(ql_dbg_io, vha, 0x3007,
4855 "%s: Term exchg sent for ox_id[%04x]\n", __func__,
4856 be16_to_cpu(cmd->atio.u.isp24.fcp_hdr.ox_id));
4858 ql_dbg(ql_dbg_io, vha, 0x3008,
4859 "%s: Unexpected cmd in QFull list %p\n", __func__,
4862 list_del(&cmd->cmd_list);
4863 list_add_tail(&cmd->cmd_list, &free_list);
4865 /* piggy back on hardware_lock for protection */
4866 vha->hw->tgt.num_qfull_cmds_alloc--;
4868 spin_unlock_irqrestore(&vha->hw->hardware_lock, flags);
4872 list_for_each_entry_safe(cmd, tcmd, &free_list, cmd_list) {
4873 list_del(&cmd->cmd_list);
4874 /* This cmd was never sent to TCM. There is no need
4875 * to schedule free or call free_cmd
4883 qlt_send_busy(struct scsi_qla_host *vha,
4884 struct atio_from_isp *atio, uint16_t status)
4888 rc = __qlt_send_busy(vha, atio, status);
4890 qlt_alloc_qfull_cmd(vha, atio, status, 1);
4894 qlt_chk_qfull_thresh_hold(struct scsi_qla_host *vha,
4895 struct atio_from_isp *atio)
4897 struct qla_hw_data *ha = vha->hw;
4900 if (ha->tgt.num_pend_cmds < Q_FULL_THRESH_HOLD(ha))
4903 status = temp_sam_status;
4904 qlt_send_busy(vha, atio, status);
4908 /* ha->hardware_lock supposed to be held on entry */
4909 /* called via callback from qla2xxx */
4910 static void qlt_24xx_atio_pkt(struct scsi_qla_host *vha,
4911 struct atio_from_isp *atio, uint8_t ha_locked)
4913 struct qla_hw_data *ha = vha->hw;
4914 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
4916 unsigned long flags;
4918 if (unlikely(tgt == NULL)) {
4919 ql_dbg(ql_dbg_io, vha, 0x3064,
4920 "ATIO pkt, but no tgt (ha %p)", ha);
4924 * In tgt_stop mode we also should allow all requests to pass.
4925 * Otherwise, some commands can stuck.
4928 tgt->atio_irq_cmd_count++;
4930 switch (atio->u.raw.entry_type) {
4932 if (unlikely(atio->u.isp24.exchange_addr ==
4933 ATIO_EXCHANGE_ADDRESS_UNKNOWN)) {
4934 ql_dbg(ql_dbg_io, vha, 0x3065,
4935 "qla_target(%d): ATIO_TYPE7 "
4936 "received with UNKNOWN exchange address, "
4937 "sending QUEUE_FULL\n", vha->vp_idx);
4939 spin_lock_irqsave(&ha->hardware_lock, flags);
4940 qlt_send_busy(vha, atio, SAM_STAT_TASK_SET_FULL);
4942 spin_unlock_irqrestore(&ha->hardware_lock, flags);
4948 if (likely(atio->u.isp24.fcp_cmnd.task_mgmt_flags == 0)) {
4949 rc = qlt_chk_qfull_thresh_hold(vha, atio);
4951 tgt->atio_irq_cmd_count--;
4954 rc = qlt_handle_cmd_for_atio(vha, atio);
4956 rc = qlt_handle_task_mgmt(vha, atio);
4958 if (unlikely(rc != 0)) {
4962 (&ha->hardware_lock, flags);
4964 #if 1 /* With TERM EXCHANGE some FC cards refuse to boot */
4965 qlt_send_busy(vha, atio, SAM_STAT_BUSY);
4967 qlt_send_term_exchange(vha, NULL, atio, 1, 0);
4971 spin_unlock_irqrestore
4972 (&ha->hardware_lock, flags);
4975 if (tgt->tgt_stop) {
4976 ql_dbg(ql_dbg_tgt, vha, 0xe059,
4977 "qla_target: Unable to send "
4978 "command to target for req, "
4981 ql_dbg(ql_dbg_tgt, vha, 0xe05a,
4982 "qla_target(%d): Unable to send "
4983 "command to target, sending BUSY "
4984 "status.\n", vha->vp_idx);
4987 &ha->hardware_lock, flags);
4988 qlt_send_busy(vha, atio, SAM_STAT_BUSY);
4990 spin_unlock_irqrestore(
4991 &ha->hardware_lock, flags);
4997 case IMMED_NOTIFY_TYPE:
4999 if (unlikely(atio->u.isp2x.entry_status != 0)) {
5000 ql_dbg(ql_dbg_tgt, vha, 0xe05b,
5001 "qla_target(%d): Received ATIO packet %x "
5002 "with error status %x\n", vha->vp_idx,
5003 atio->u.raw.entry_type,
5004 atio->u.isp2x.entry_status);
5007 ql_dbg(ql_dbg_tgt, vha, 0xe02e, "%s", "IMMED_NOTIFY ATIO");
5010 spin_lock_irqsave(&ha->hardware_lock, flags);
5011 qlt_handle_imm_notify(vha, (struct imm_ntfy_from_isp *)atio);
5013 spin_unlock_irqrestore(&ha->hardware_lock, flags);
5018 ql_dbg(ql_dbg_tgt, vha, 0xe05c,
5019 "qla_target(%d): Received unknown ATIO atio "
5020 "type %x\n", vha->vp_idx, atio->u.raw.entry_type);
5024 tgt->atio_irq_cmd_count--;
5027 /* ha->hardware_lock supposed to be held on entry */
5028 /* called via callback from qla2xxx */
5029 static void qlt_response_pkt(struct scsi_qla_host *vha, response_t *pkt)
5031 struct qla_hw_data *ha = vha->hw;
5032 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
5034 if (unlikely(tgt == NULL)) {
5035 ql_dbg(ql_dbg_tgt, vha, 0xe05d,
5036 "qla_target(%d): Response pkt %x received, but no "
5037 "tgt (ha %p)\n", vha->vp_idx, pkt->entry_type, ha);
5042 * In tgt_stop mode we also should allow all requests to pass.
5043 * Otherwise, some commands can stuck.
5046 tgt->irq_cmd_count++;
5048 switch (pkt->entry_type) {
5052 struct ctio7_from_24xx *entry = (struct ctio7_from_24xx *)pkt;
5053 qlt_do_ctio_completion(vha, entry->handle,
5054 le16_to_cpu(entry->status)|(pkt->entry_status << 16),
5059 case ACCEPT_TGT_IO_TYPE:
5061 struct atio_from_isp *atio = (struct atio_from_isp *)pkt;
5063 if (atio->u.isp2x.status !=
5064 cpu_to_le16(ATIO_CDB_VALID)) {
5065 ql_dbg(ql_dbg_tgt, vha, 0xe05e,
5066 "qla_target(%d): ATIO with error "
5067 "status %x received\n", vha->vp_idx,
5068 le16_to_cpu(atio->u.isp2x.status));
5072 rc = qlt_chk_qfull_thresh_hold(vha, atio);
5074 tgt->irq_cmd_count--;
5078 rc = qlt_handle_cmd_for_atio(vha, atio);
5079 if (unlikely(rc != 0)) {
5081 #if 1 /* With TERM EXCHANGE some FC cards refuse to boot */
5082 qlt_send_busy(vha, atio, 0);
5084 qlt_send_term_exchange(vha, NULL, atio, 1, 0);
5087 if (tgt->tgt_stop) {
5088 ql_dbg(ql_dbg_tgt, vha, 0xe05f,
5089 "qla_target: Unable to send "
5090 "command to target, sending TERM "
5091 "EXCHANGE for rsp\n");
5092 qlt_send_term_exchange(vha, NULL,
5095 ql_dbg(ql_dbg_tgt, vha, 0xe060,
5096 "qla_target(%d): Unable to send "
5097 "command to target, sending BUSY "
5098 "status\n", vha->vp_idx);
5099 qlt_send_busy(vha, atio, 0);
5106 case CONTINUE_TGT_IO_TYPE:
5108 struct ctio_to_2xxx *entry = (struct ctio_to_2xxx *)pkt;
5109 qlt_do_ctio_completion(vha, entry->handle,
5110 le16_to_cpu(entry->status)|(pkt->entry_status << 16),
5117 struct ctio_to_2xxx *entry = (struct ctio_to_2xxx *)pkt;
5118 qlt_do_ctio_completion(vha, entry->handle,
5119 le16_to_cpu(entry->status)|(pkt->entry_status << 16),
5124 case IMMED_NOTIFY_TYPE:
5125 ql_dbg(ql_dbg_tgt, vha, 0xe035, "%s", "IMMED_NOTIFY\n");
5126 qlt_handle_imm_notify(vha, (struct imm_ntfy_from_isp *)pkt);
5129 case NOTIFY_ACK_TYPE:
5130 if (tgt->notify_ack_expected > 0) {
5131 struct nack_to_isp *entry = (struct nack_to_isp *)pkt;
5132 ql_dbg(ql_dbg_tgt, vha, 0xe036,
5133 "NOTIFY_ACK seq %08x status %x\n",
5134 le16_to_cpu(entry->u.isp2x.seq_id),
5135 le16_to_cpu(entry->u.isp2x.status));
5136 tgt->notify_ack_expected--;
5137 if (entry->u.isp2x.status !=
5138 cpu_to_le16(NOTIFY_ACK_SUCCESS)) {
5139 ql_dbg(ql_dbg_tgt, vha, 0xe061,
5140 "qla_target(%d): NOTIFY_ACK "
5141 "failed %x\n", vha->vp_idx,
5142 le16_to_cpu(entry->u.isp2x.status));
5145 ql_dbg(ql_dbg_tgt, vha, 0xe062,
5146 "qla_target(%d): Unexpected NOTIFY_ACK received\n",
5151 case ABTS_RECV_24XX:
5152 ql_dbg(ql_dbg_tgt, vha, 0xe037,
5153 "ABTS_RECV_24XX: instance %d\n", vha->vp_idx);
5154 qlt_24xx_handle_abts(vha, (struct abts_recv_from_24xx *)pkt);
5157 case ABTS_RESP_24XX:
5158 if (tgt->abts_resp_expected > 0) {
5159 struct abts_resp_from_24xx_fw *entry =
5160 (struct abts_resp_from_24xx_fw *)pkt;
5161 ql_dbg(ql_dbg_tgt, vha, 0xe038,
5162 "ABTS_RESP_24XX: compl_status %x\n",
5163 entry->compl_status);
5164 tgt->abts_resp_expected--;
5165 if (le16_to_cpu(entry->compl_status) !=
5166 ABTS_RESP_COMPL_SUCCESS) {
5167 if ((entry->error_subcode1 == 0x1E) &&
5168 (entry->error_subcode2 == 0)) {
5170 * We've got a race here: aborted
5171 * exchange not terminated, i.e.
5172 * response for the aborted command was
5173 * sent between the abort request was
5174 * received and processed.
5175 * Unfortunately, the firmware has a
5176 * silly requirement that all aborted
5177 * exchanges must be explicitely
5178 * terminated, otherwise it refuses to
5179 * send responses for the abort
5180 * requests. So, we have to
5181 * (re)terminate the exchange and retry
5182 * the abort response.
5184 qlt_24xx_retry_term_exchange(vha,
5187 ql_dbg(ql_dbg_tgt, vha, 0xe063,
5188 "qla_target(%d): ABTS_RESP_24XX "
5189 "failed %x (subcode %x:%x)",
5190 vha->vp_idx, entry->compl_status,
5191 entry->error_subcode1,
5192 entry->error_subcode2);
5195 ql_dbg(ql_dbg_tgt, vha, 0xe064,
5196 "qla_target(%d): Unexpected ABTS_RESP_24XX "
5197 "received\n", vha->vp_idx);
5202 ql_dbg(ql_dbg_tgt, vha, 0xe065,
5203 "qla_target(%d): Received unknown response pkt "
5204 "type %x\n", vha->vp_idx, pkt->entry_type);
5208 tgt->irq_cmd_count--;
5212 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
5214 void qlt_async_event(uint16_t code, struct scsi_qla_host *vha,
5217 struct qla_hw_data *ha = vha->hw;
5218 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
5221 if (!ha->tgt.tgt_ops)
5224 if (unlikely(tgt == NULL)) {
5225 ql_dbg(ql_dbg_tgt, vha, 0xe03a,
5226 "ASYNC EVENT %#x, but no tgt (ha %p)\n", code, ha);
5230 if (((code == MBA_POINT_TO_POINT) || (code == MBA_CHG_IN_CONNECTION)) &&
5234 * In tgt_stop mode we also should allow all requests to pass.
5235 * Otherwise, some commands can stuck.
5238 tgt->irq_cmd_count++;
5241 case MBA_RESET: /* Reset */
5242 case MBA_SYSTEM_ERR: /* System Error */
5243 case MBA_REQ_TRANSFER_ERR: /* Request Transfer Error */
5244 case MBA_RSP_TRANSFER_ERR: /* Response Transfer Error */
5245 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03a,
5246 "qla_target(%d): System error async event %#x "
5247 "occurred", vha->vp_idx, code);
5249 case MBA_WAKEUP_THRES: /* Request Queue Wake-up. */
5250 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
5255 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03b,
5256 "qla_target(%d): Async LOOP_UP occurred "
5257 "(m[0]=%x, m[1]=%x, m[2]=%x, m[3]=%x)", vha->vp_idx,
5258 le16_to_cpu(mailbox[0]), le16_to_cpu(mailbox[1]),
5259 le16_to_cpu(mailbox[2]), le16_to_cpu(mailbox[3]));
5260 if (tgt->link_reinit_iocb_pending) {
5261 qlt_send_notify_ack(vha, (void *)&tgt->link_reinit_iocb,
5263 tgt->link_reinit_iocb_pending = 0;
5268 case MBA_LIP_OCCURRED:
5271 case MBA_RSCN_UPDATE:
5272 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03c,
5273 "qla_target(%d): Async event %#x occurred "
5274 "(m[0]=%x, m[1]=%x, m[2]=%x, m[3]=%x)", vha->vp_idx, code,
5275 le16_to_cpu(mailbox[0]), le16_to_cpu(mailbox[1]),
5276 le16_to_cpu(mailbox[2]), le16_to_cpu(mailbox[3]));
5279 case MBA_REJECTED_FCP_CMD:
5280 ql_dbg(ql_dbg_tgt_mgt, vha, 0xffff,
5281 "qla_target(%d): Async event LS_REJECT occurred "
5282 "(m[0]=%x, m[1]=%x, m[2]=%x, m[3]=%x)", vha->vp_idx,
5283 le16_to_cpu(mailbox[0]), le16_to_cpu(mailbox[1]),
5284 le16_to_cpu(mailbox[2]), le16_to_cpu(mailbox[3]));
5286 if (le16_to_cpu(mailbox[3]) == 1) {
5287 /* exchange starvation. */
5288 vha->hw->exch_starvation++;
5289 if (vha->hw->exch_starvation > 5) {
5290 ql_log(ql_log_warn, vha, 0xffff,
5291 "Exchange starvation-. Resetting RISC\n");
5293 vha->hw->exch_starvation = 0;
5294 if (IS_P3P_TYPE(vha->hw))
5295 set_bit(FCOE_CTX_RESET_NEEDED,
5298 set_bit(ISP_ABORT_NEEDED,
5300 qla2xxx_wake_dpc(vha);
5305 case MBA_PORT_UPDATE:
5306 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03d,
5307 "qla_target(%d): Port update async event %#x "
5308 "occurred: updating the ports database (m[0]=%x, m[1]=%x, "
5309 "m[2]=%x, m[3]=%x)", vha->vp_idx, code,
5310 le16_to_cpu(mailbox[0]), le16_to_cpu(mailbox[1]),
5311 le16_to_cpu(mailbox[2]), le16_to_cpu(mailbox[3]));
5313 login_code = le16_to_cpu(mailbox[2]);
5314 if (login_code == 0x4) {
5315 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03e,
5316 "Async MB 2: Got PLOGI Complete\n");
5317 vha->hw->exch_starvation = 0;
5318 } else if (login_code == 0x7)
5319 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03f,
5320 "Async MB 2: Port Logged Out\n");
5326 tgt->irq_cmd_count--;
5329 static fc_port_t *qlt_get_port_database(struct scsi_qla_host *vha,
5332 fc_port_t *fcport, *tfcp, *del;
5334 unsigned long flags;
5337 fcport = kzalloc(sizeof(*fcport), GFP_KERNEL);
5339 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06f,
5340 "qla_target(%d): Allocation of tmp FC port failed",
5345 fcport->loop_id = loop_id;
5347 rc = qla2x00_get_port_database(vha, fcport, 0);
5348 if (rc != QLA_SUCCESS) {
5349 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf070,
5350 "qla_target(%d): Failed to retrieve fcport "
5351 "information -- get_port_database() returned %x "
5352 "(loop_id=0x%04x)", vha->vp_idx, rc, loop_id);
5358 spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
5359 tfcp = qla2x00_find_fcport_by_wwpn(vha, fcport->port_name, 1);
5362 tfcp->d_id = fcport->d_id;
5363 tfcp->port_type = fcport->port_type;
5364 tfcp->supported_classes = fcport->supported_classes;
5365 tfcp->flags |= fcport->flags;
5370 if (vha->hw->current_topology == ISP_CFG_F)
5371 fcport->flags |= FCF_FABRIC_DEVICE;
5373 list_add_tail(&fcport->list, &vha->vp_fcports);
5374 if (!IS_SW_RESV_ADDR(fcport->d_id))
5375 vha->fcport_count++;
5376 fcport->login_gen++;
5377 fcport->disc_state = DSC_LOGIN_COMPLETE;
5378 fcport->login_succ = 1;
5382 fcport->deleted = 0;
5383 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
5385 switch (vha->host->active_mode) {
5386 case MODE_INITIATOR:
5389 if (!IS_IIDMA_CAPABLE(vha->hw) || !vha->hw->flags.gpsc_supported) {
5390 ql_dbg(ql_dbg_disc, vha, 0xffff,
5391 "%s %d %8phC post upd_fcport fcp_cnt %d\n",
5392 __func__, __LINE__, fcport->port_name, vha->fcport_count);
5393 qla24xx_post_upd_fcport_work(vha, fcport);
5395 ql_dbg(ql_dbg_disc, vha, 0xffff,
5396 "%s %d %8phC post gpsc fcp_cnt %d\n",
5397 __func__, __LINE__, fcport->port_name, vha->fcport_count);
5398 qla24xx_post_gpsc_work(vha, fcport);
5408 qla2x00_free_fcport(del);
5413 /* Must be called under tgt_mutex */
5414 static struct fc_port *qlt_make_local_sess(struct scsi_qla_host *vha,
5417 struct fc_port *sess = NULL;
5418 fc_port_t *fcport = NULL;
5419 int rc, global_resets;
5420 uint16_t loop_id = 0;
5422 if ((s_id[0] == 0xFF) && (s_id[1] == 0xFC)) {
5424 * This is Domain Controller, so it should be
5425 * OK to drop SCSI commands from it.
5427 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf042,
5428 "Unable to find initiator with S_ID %x:%x:%x",
5429 s_id[0], s_id[1], s_id[2]);
5433 mutex_lock(&vha->vha_tgt.tgt_mutex);
5437 atomic_read(&vha->vha_tgt.qla_tgt->tgt_global_resets_count);
5439 rc = qla24xx_get_loop_id(vha, s_id, &loop_id);
5441 mutex_unlock(&vha->vha_tgt.tgt_mutex);
5443 ql_log(ql_log_info, vha, 0xf071,
5444 "qla_target(%d): Unable to find "
5445 "initiator with S_ID %x:%x:%x",
5446 vha->vp_idx, s_id[0], s_id[1],
5449 if (rc == -ENOENT) {
5450 qlt_port_logo_t logo;
5451 sid_to_portid(s_id, &logo.id);
5453 qlt_send_first_logo(vha, &logo);
5459 fcport = qlt_get_port_database(vha, loop_id);
5461 mutex_unlock(&vha->vha_tgt.tgt_mutex);
5465 if (global_resets !=
5466 atomic_read(&vha->vha_tgt.qla_tgt->tgt_global_resets_count)) {
5467 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf043,
5468 "qla_target(%d): global reset during session discovery "
5469 "(counter was %d, new %d), retrying", vha->vp_idx,
5471 atomic_read(&vha->vha_tgt.
5472 qla_tgt->tgt_global_resets_count));
5476 sess = qlt_create_sess(vha, fcport, true);
5478 mutex_unlock(&vha->vha_tgt.tgt_mutex);
5483 static void qlt_abort_work(struct qla_tgt *tgt,
5484 struct qla_tgt_sess_work_param *prm)
5486 struct scsi_qla_host *vha = tgt->vha;
5487 struct qla_hw_data *ha = vha->hw;
5488 struct fc_port *sess = NULL;
5489 unsigned long flags = 0, flags2 = 0;
5494 spin_lock_irqsave(&ha->tgt.sess_lock, flags2);
5499 s_id[0] = prm->abts.fcp_hdr_le.s_id[2];
5500 s_id[1] = prm->abts.fcp_hdr_le.s_id[1];
5501 s_id[2] = prm->abts.fcp_hdr_le.s_id[0];
5503 sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha,
5504 (unsigned char *)&be_s_id);
5506 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags2);
5508 sess = qlt_make_local_sess(vha, s_id);
5509 /* sess has got an extra creation ref */
5511 spin_lock_irqsave(&ha->tgt.sess_lock, flags2);
5515 if (sess->deleted) {
5520 if (!kref_get_unless_zero(&sess->sess_kref)) {
5521 ql_dbg(ql_dbg_tgt_tmr, vha, 0xffff,
5522 "%s: kref_get fail %8phC \n",
5523 __func__, sess->port_name);
5529 spin_lock_irqsave(&ha->hardware_lock, flags);
5534 rc = __qlt_24xx_handle_abts(vha, &prm->abts, sess);
5537 spin_unlock_irqrestore(&ha->hardware_lock, flags);
5539 ha->tgt.tgt_ops->put_sess(sess);
5540 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags2);
5544 spin_lock_irqsave(&ha->hardware_lock, flags);
5547 qlt_24xx_send_abts_resp(vha, &prm->abts, FCP_TMF_REJECTED, false);
5548 spin_unlock_irqrestore(&ha->hardware_lock, flags);
5551 ha->tgt.tgt_ops->put_sess(sess);
5552 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags2);
5555 static void qlt_tmr_work(struct qla_tgt *tgt,
5556 struct qla_tgt_sess_work_param *prm)
5558 struct atio_from_isp *a = &prm->tm_iocb2;
5559 struct scsi_qla_host *vha = tgt->vha;
5560 struct qla_hw_data *ha = vha->hw;
5561 struct fc_port *sess = NULL;
5562 unsigned long flags;
5563 uint8_t *s_id = NULL; /* to hide compiler warnings */
5565 uint32_t lun, unpacked_lun;
5569 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
5574 s_id = prm->tm_iocb2.u.isp24.fcp_hdr.s_id;
5575 sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha, s_id);
5577 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
5579 sess = qlt_make_local_sess(vha, s_id);
5580 /* sess has got an extra creation ref */
5582 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
5586 if (sess->deleted) {
5591 if (!kref_get_unless_zero(&sess->sess_kref)) {
5592 ql_dbg(ql_dbg_tgt_tmr, vha, 0xffff,
5593 "%s: kref_get fail %8phC\n",
5594 __func__, sess->port_name);
5601 lun = a->u.isp24.fcp_cmnd.lun;
5602 fn = a->u.isp24.fcp_cmnd.task_mgmt_flags;
5603 unpacked_lun = scsilun_to_int((struct scsi_lun *)&lun);
5605 rc = qlt_issue_task_mgmt(sess, unpacked_lun, fn, iocb, 0);
5609 ha->tgt.tgt_ops->put_sess(sess);
5610 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
5614 qlt_send_term_exchange(vha, NULL, &prm->tm_iocb2, 1, 0);
5615 ha->tgt.tgt_ops->put_sess(sess);
5616 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
5619 static void qlt_sess_work_fn(struct work_struct *work)
5621 struct qla_tgt *tgt = container_of(work, struct qla_tgt, sess_work);
5622 struct scsi_qla_host *vha = tgt->vha;
5623 unsigned long flags;
5625 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf000, "Sess work (tgt %p)", tgt);
5627 spin_lock_irqsave(&tgt->sess_work_lock, flags);
5628 while (!list_empty(&tgt->sess_works_list)) {
5629 struct qla_tgt_sess_work_param *prm = list_entry(
5630 tgt->sess_works_list.next, typeof(*prm),
5631 sess_works_list_entry);
5634 * This work can be scheduled on several CPUs at time, so we
5635 * must delete the entry to eliminate double processing
5637 list_del(&prm->sess_works_list_entry);
5639 spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
5641 switch (prm->type) {
5642 case QLA_TGT_SESS_WORK_ABORT:
5643 qlt_abort_work(tgt, prm);
5645 case QLA_TGT_SESS_WORK_TM:
5646 qlt_tmr_work(tgt, prm);
5653 spin_lock_irqsave(&tgt->sess_work_lock, flags);
5657 spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
5660 /* Must be called under tgt_host_action_mutex */
5661 int qlt_add_target(struct qla_hw_data *ha, struct scsi_qla_host *base_vha)
5663 struct qla_tgt *tgt;
5665 if (!QLA_TGT_MODE_ENABLED())
5668 if (!IS_TGT_MODE_CAPABLE(ha)) {
5669 ql_log(ql_log_warn, base_vha, 0xe070,
5670 "This adapter does not support target mode.\n");
5674 ql_dbg(ql_dbg_tgt, base_vha, 0xe03b,
5675 "Registering target for host %ld(%p).\n", base_vha->host_no, ha);
5677 BUG_ON(base_vha->vha_tgt.qla_tgt != NULL);
5679 tgt = kzalloc(sizeof(struct qla_tgt), GFP_KERNEL);
5681 ql_dbg(ql_dbg_tgt, base_vha, 0xe066,
5682 "Unable to allocate struct qla_tgt\n");
5686 if (!(base_vha->host->hostt->supported_mode & MODE_TARGET))
5687 base_vha->host->hostt->supported_mode |= MODE_TARGET;
5690 tgt->vha = base_vha;
5691 init_waitqueue_head(&tgt->waitQ);
5692 INIT_LIST_HEAD(&tgt->del_sess_list);
5693 spin_lock_init(&tgt->sess_work_lock);
5694 INIT_WORK(&tgt->sess_work, qlt_sess_work_fn);
5695 INIT_LIST_HEAD(&tgt->sess_works_list);
5696 atomic_set(&tgt->tgt_global_resets_count, 0);
5698 base_vha->vha_tgt.qla_tgt = tgt;
5700 ql_dbg(ql_dbg_tgt, base_vha, 0xe067,
5701 "qla_target(%d): using 64 Bit PCI addressing",
5703 tgt->tgt_enable_64bit_addr = 1;
5705 tgt->sg_tablesize = QLA_TGT_MAX_SG_24XX(base_vha->req->length - 3);
5706 tgt->datasegs_per_cmd = QLA_TGT_DATASEGS_PER_CMD_24XX;
5707 tgt->datasegs_per_cont = QLA_TGT_DATASEGS_PER_CONT_24XX;
5709 if (base_vha->fc_vport)
5712 mutex_lock(&qla_tgt_mutex);
5713 list_add_tail(&tgt->tgt_list_entry, &qla_tgt_glist);
5714 mutex_unlock(&qla_tgt_mutex);
5719 /* Must be called under tgt_host_action_mutex */
5720 int qlt_remove_target(struct qla_hw_data *ha, struct scsi_qla_host *vha)
5722 if (!vha->vha_tgt.qla_tgt)
5725 if (vha->fc_vport) {
5726 qlt_release(vha->vha_tgt.qla_tgt);
5730 /* free left over qfull cmds */
5731 qlt_init_term_exchange(vha);
5733 mutex_lock(&qla_tgt_mutex);
5734 list_del(&vha->vha_tgt.qla_tgt->tgt_list_entry);
5735 mutex_unlock(&qla_tgt_mutex);
5737 ql_dbg(ql_dbg_tgt, vha, 0xe03c, "Unregistering target for host %ld(%p)",
5739 qlt_release(vha->vha_tgt.qla_tgt);
5744 static void qlt_lport_dump(struct scsi_qla_host *vha, u64 wwpn,
5749 pr_debug("qla2xxx HW vha->node_name: ");
5750 for (i = 0; i < WWN_SIZE; i++)
5751 pr_debug("%02x ", vha->node_name[i]);
5753 pr_debug("qla2xxx HW vha->port_name: ");
5754 for (i = 0; i < WWN_SIZE; i++)
5755 pr_debug("%02x ", vha->port_name[i]);
5758 pr_debug("qla2xxx passed configfs WWPN: ");
5759 put_unaligned_be64(wwpn, b);
5760 for (i = 0; i < WWN_SIZE; i++)
5761 pr_debug("%02x ", b[i]);
5766 * qla_tgt_lport_register - register lport with external module
5768 * @qla_tgt_ops: Pointer for tcm_qla2xxx qla_tgt_ops
5769 * @wwpn: Passwd FC target WWPN
5770 * @callback: lport initialization callback for tcm_qla2xxx code
5771 * @target_lport_ptr: pointer for tcm_qla2xxx specific lport data
5773 int qlt_lport_register(void *target_lport_ptr, u64 phys_wwpn,
5774 u64 npiv_wwpn, u64 npiv_wwnn,
5775 int (*callback)(struct scsi_qla_host *, void *, u64, u64))
5777 struct qla_tgt *tgt;
5778 struct scsi_qla_host *vha;
5779 struct qla_hw_data *ha;
5780 struct Scsi_Host *host;
5781 unsigned long flags;
5785 mutex_lock(&qla_tgt_mutex);
5786 list_for_each_entry(tgt, &qla_tgt_glist, tgt_list_entry) {
5794 if (!(host->hostt->supported_mode & MODE_TARGET))
5797 spin_lock_irqsave(&ha->hardware_lock, flags);
5798 if ((!npiv_wwpn || !npiv_wwnn) && host->active_mode & MODE_TARGET) {
5799 pr_debug("MODE_TARGET already active on qla2xxx(%d)\n",
5801 spin_unlock_irqrestore(&ha->hardware_lock, flags);
5804 if (tgt->tgt_stop) {
5805 pr_debug("MODE_TARGET in shutdown on qla2xxx(%d)\n",
5807 spin_unlock_irqrestore(&ha->hardware_lock, flags);
5810 spin_unlock_irqrestore(&ha->hardware_lock, flags);
5812 if (!scsi_host_get(host)) {
5813 ql_dbg(ql_dbg_tgt, vha, 0xe068,
5814 "Unable to scsi_host_get() for"
5815 " qla2xxx scsi_host\n");
5818 qlt_lport_dump(vha, phys_wwpn, b);
5820 if (memcmp(vha->port_name, b, WWN_SIZE)) {
5821 scsi_host_put(host);
5824 rc = (*callback)(vha, target_lport_ptr, npiv_wwpn, npiv_wwnn);
5826 scsi_host_put(host);
5828 mutex_unlock(&qla_tgt_mutex);
5831 mutex_unlock(&qla_tgt_mutex);
5835 EXPORT_SYMBOL(qlt_lport_register);
5838 * qla_tgt_lport_deregister - Degister lport
5840 * @vha: Registered scsi_qla_host pointer
5842 void qlt_lport_deregister(struct scsi_qla_host *vha)
5844 struct qla_hw_data *ha = vha->hw;
5845 struct Scsi_Host *sh = vha->host;
5847 * Clear the target_lport_ptr qla_target_template pointer in qla_hw_data
5849 vha->vha_tgt.target_lport_ptr = NULL;
5850 ha->tgt.tgt_ops = NULL;
5852 * Release the Scsi_Host reference for the underlying qla2xxx host
5856 EXPORT_SYMBOL(qlt_lport_deregister);
5858 /* Must be called under HW lock */
5859 static void qlt_set_mode(struct scsi_qla_host *vha)
5861 switch (ql2x_ini_mode) {
5862 case QLA2XXX_INI_MODE_DISABLED:
5863 case QLA2XXX_INI_MODE_EXCLUSIVE:
5864 vha->host->active_mode = MODE_TARGET;
5866 case QLA2XXX_INI_MODE_ENABLED:
5867 vha->host->active_mode = MODE_UNKNOWN;
5869 case QLA2XXX_INI_MODE_DUAL:
5870 vha->host->active_mode = MODE_DUAL;
5877 /* Must be called under HW lock */
5878 static void qlt_clear_mode(struct scsi_qla_host *vha)
5880 switch (ql2x_ini_mode) {
5881 case QLA2XXX_INI_MODE_DISABLED:
5882 vha->host->active_mode = MODE_UNKNOWN;
5884 case QLA2XXX_INI_MODE_EXCLUSIVE:
5885 vha->host->active_mode = MODE_INITIATOR;
5887 case QLA2XXX_INI_MODE_ENABLED:
5888 case QLA2XXX_INI_MODE_DUAL:
5889 vha->host->active_mode = MODE_INITIATOR;
5897 * qla_tgt_enable_vha - NO LOCK HELD
5899 * host_reset, bring up w/ Target Mode Enabled
5902 qlt_enable_vha(struct scsi_qla_host *vha)
5904 struct qla_hw_data *ha = vha->hw;
5905 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
5906 unsigned long flags;
5907 scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
5908 int rspq_ent = QLA83XX_RSPQ_MSIX_ENTRY_NUMBER;
5911 ql_dbg(ql_dbg_tgt, vha, 0xe069,
5912 "Unable to locate qla_tgt pointer from"
5913 " struct qla_hw_data\n");
5918 spin_lock_irqsave(&ha->hardware_lock, flags);
5919 tgt->tgt_stopped = 0;
5921 spin_unlock_irqrestore(&ha->hardware_lock, flags);
5924 qla24xx_disable_vp(vha);
5925 qla24xx_enable_vp(vha);
5927 if (ha->msix_entries) {
5928 ql_dbg(ql_dbg_tgt, vha, 0xffff,
5929 "%s: host%ld : vector %d cpu %d\n",
5930 __func__, vha->host_no,
5931 ha->msix_entries[rspq_ent].vector,
5932 ha->msix_entries[rspq_ent].cpuid);
5934 ha->tgt.rspq_vector_cpuid =
5935 ha->msix_entries[rspq_ent].cpuid;
5938 set_bit(ISP_ABORT_NEEDED, &base_vha->dpc_flags);
5939 qla2xxx_wake_dpc(base_vha);
5940 qla2x00_wait_for_hba_online(base_vha);
5943 EXPORT_SYMBOL(qlt_enable_vha);
5946 * qla_tgt_disable_vha - NO LOCK HELD
5948 * Disable Target Mode and reset the adapter
5950 static void qlt_disable_vha(struct scsi_qla_host *vha)
5952 struct qla_hw_data *ha = vha->hw;
5953 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
5954 unsigned long flags;
5957 ql_dbg(ql_dbg_tgt, vha, 0xe06a,
5958 "Unable to locate qla_tgt pointer from"
5959 " struct qla_hw_data\n");
5964 spin_lock_irqsave(&ha->hardware_lock, flags);
5965 qlt_clear_mode(vha);
5966 spin_unlock_irqrestore(&ha->hardware_lock, flags);
5968 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
5969 qla2xxx_wake_dpc(vha);
5970 qla2x00_wait_for_hba_online(vha);
5974 * Called from qla_init.c:qla24xx_vport_create() contex to setup
5975 * the target mode specific struct scsi_qla_host and struct qla_hw_data
5979 qlt_vport_create(struct scsi_qla_host *vha, struct qla_hw_data *ha)
5981 vha->vha_tgt.qla_tgt = NULL;
5983 mutex_init(&vha->vha_tgt.tgt_mutex);
5984 mutex_init(&vha->vha_tgt.tgt_host_action_mutex);
5986 qlt_clear_mode(vha);
5989 * NOTE: Currently the value is kept the same for <24xx and
5990 * >=24xx ISPs. If it is necessary to change it,
5991 * the check should be added for specific ISPs,
5992 * assigning the value appropriately.
5994 ha->tgt.atio_q_length = ATIO_ENTRY_CNT_24XX;
5996 qlt_add_target(ha, vha);
6000 qlt_rff_id(struct scsi_qla_host *vha, struct ct_sns_req *ct_req)
6003 * FC-4 Feature bit 0 indicates target functionality to the name server.
6005 if (qla_tgt_mode_enabled(vha)) {
6006 ct_req->req.rff_id.fc4_feature = BIT_0;
6007 } else if (qla_ini_mode_enabled(vha)) {
6008 ct_req->req.rff_id.fc4_feature = BIT_1;
6009 } else if (qla_dual_mode_enabled(vha))
6010 ct_req->req.rff_id.fc4_feature = BIT_0 | BIT_1;
6014 * qlt_init_atio_q_entries() - Initializes ATIO queue entries.
6017 * Beginning of ATIO ring has initialization control block already built
6018 * by nvram config routine.
6020 * Returns 0 on success.
6023 qlt_init_atio_q_entries(struct scsi_qla_host *vha)
6025 struct qla_hw_data *ha = vha->hw;
6027 struct atio_from_isp *pkt = (struct atio_from_isp *)ha->tgt.atio_ring;
6029 if (qla_ini_mode_enabled(vha))
6032 for (cnt = 0; cnt < ha->tgt.atio_q_length; cnt++) {
6033 pkt->u.raw.signature = ATIO_PROCESSED;
6040 * qlt_24xx_process_atio_queue() - Process ATIO queue entries.
6041 * @ha: SCSI driver HA context
6044 qlt_24xx_process_atio_queue(struct scsi_qla_host *vha, uint8_t ha_locked)
6046 struct qla_hw_data *ha = vha->hw;
6047 struct atio_from_isp *pkt;
6050 if (!vha->flags.online)
6053 while ((ha->tgt.atio_ring_ptr->signature != ATIO_PROCESSED) ||
6054 fcpcmd_is_corrupted(ha->tgt.atio_ring_ptr)) {
6055 pkt = (struct atio_from_isp *)ha->tgt.atio_ring_ptr;
6056 cnt = pkt->u.raw.entry_count;
6058 if (unlikely(fcpcmd_is_corrupted(ha->tgt.atio_ring_ptr))) {
6060 * This packet is corrupted. The header + payload
6061 * can not be trusted. There is no point in passing
6064 ql_log(ql_log_warn, vha, 0xffff,
6065 "corrupted fcp frame SID[%3phN] OXID[%04x] EXCG[%x] %64phN\n",
6066 pkt->u.isp24.fcp_hdr.s_id,
6067 be16_to_cpu(pkt->u.isp24.fcp_hdr.ox_id),
6068 le32_to_cpu(pkt->u.isp24.exchange_addr), pkt);
6070 adjust_corrupted_atio(pkt);
6071 qlt_send_term_exchange(vha, NULL, pkt, ha_locked, 0);
6073 qlt_24xx_atio_pkt_all_vps(vha,
6074 (struct atio_from_isp *)pkt, ha_locked);
6077 for (i = 0; i < cnt; i++) {
6078 ha->tgt.atio_ring_index++;
6079 if (ha->tgt.atio_ring_index == ha->tgt.atio_q_length) {
6080 ha->tgt.atio_ring_index = 0;
6081 ha->tgt.atio_ring_ptr = ha->tgt.atio_ring;
6083 ha->tgt.atio_ring_ptr++;
6085 pkt->u.raw.signature = ATIO_PROCESSED;
6086 pkt = (struct atio_from_isp *)ha->tgt.atio_ring_ptr;
6091 /* Adjust ring index */
6092 WRT_REG_DWORD(ISP_ATIO_Q_OUT(vha), ha->tgt.atio_ring_index);
6093 RD_REG_DWORD_RELAXED(ISP_ATIO_Q_OUT(vha));
6097 qlt_24xx_config_rings(struct scsi_qla_host *vha)
6099 struct qla_hw_data *ha = vha->hw;
6100 if (!QLA_TGT_MODE_ENABLED())
6103 WRT_REG_DWORD(ISP_ATIO_Q_IN(vha), 0);
6104 WRT_REG_DWORD(ISP_ATIO_Q_OUT(vha), 0);
6105 RD_REG_DWORD(ISP_ATIO_Q_OUT(vha));
6107 if (IS_ATIO_MSIX_CAPABLE(ha)) {
6108 struct qla_msix_entry *msix = &ha->msix_entries[2];
6109 struct init_cb_24xx *icb = (struct init_cb_24xx *)ha->init_cb;
6111 icb->msix_atio = cpu_to_le16(msix->entry);
6112 ql_dbg(ql_dbg_init, vha, 0xf072,
6113 "Registering ICB vector 0x%x for atio que.\n",
6119 qlt_24xx_config_nvram_stage1(struct scsi_qla_host *vha, struct nvram_24xx *nv)
6121 struct qla_hw_data *ha = vha->hw;
6125 if (qla_tgt_mode_enabled(vha) || qla_dual_mode_enabled(vha)) {
6126 if (!ha->tgt.saved_set) {
6127 /* We save only once */
6128 ha->tgt.saved_exchange_count = nv->exchange_count;
6129 ha->tgt.saved_firmware_options_1 =
6130 nv->firmware_options_1;
6131 ha->tgt.saved_firmware_options_2 =
6132 nv->firmware_options_2;
6133 ha->tgt.saved_firmware_options_3 =
6134 nv->firmware_options_3;
6135 ha->tgt.saved_set = 1;
6138 if (qla_tgt_mode_enabled(vha)) {
6139 nv->exchange_count = cpu_to_le16(0xFFFF);
6141 if (ql_dm_tgt_ex_pct > 100) {
6142 ql_dm_tgt_ex_pct = 50;
6143 } else if (ql_dm_tgt_ex_pct == 100) {
6144 /* leave some for FW */
6145 ql_dm_tgt_ex_pct = 95;
6148 tmp = ha->orig_fw_xcb_count * ql_dm_tgt_ex_pct;
6154 nv->exchange_count = cpu_to_le16(t);
6157 /* Enable target mode */
6158 nv->firmware_options_1 |= cpu_to_le32(BIT_4);
6160 /* Disable ini mode, if requested */
6161 if (qla_tgt_mode_enabled(vha))
6162 nv->firmware_options_1 |= cpu_to_le32(BIT_5);
6164 /* Disable Full Login after LIP */
6165 nv->firmware_options_1 &= cpu_to_le32(~BIT_13);
6166 /* Enable initial LIP */
6167 nv->firmware_options_1 &= cpu_to_le32(~BIT_9);
6168 if (ql2xtgt_tape_enable)
6169 /* Enable FC Tape support */
6170 nv->firmware_options_2 |= cpu_to_le32(BIT_12);
6172 /* Disable FC Tape support */
6173 nv->firmware_options_2 &= cpu_to_le32(~BIT_12);
6175 /* Disable Full Login after LIP */
6176 nv->host_p &= cpu_to_le32(~BIT_10);
6179 * clear BIT 15 explicitly as we have seen at least
6180 * a couple of instances where this was set and this
6181 * was causing the firmware to not be initialized.
6183 nv->firmware_options_1 &= cpu_to_le32(~BIT_15);
6184 /* Enable target PRLI control */
6185 nv->firmware_options_2 |= cpu_to_le32(BIT_14);
6187 if (ha->tgt.saved_set) {
6188 nv->exchange_count = ha->tgt.saved_exchange_count;
6189 nv->firmware_options_1 =
6190 ha->tgt.saved_firmware_options_1;
6191 nv->firmware_options_2 =
6192 ha->tgt.saved_firmware_options_2;
6193 nv->firmware_options_3 =
6194 ha->tgt.saved_firmware_options_3;
6199 if (ha->tgt.enable_class_2) {
6200 if (vha->flags.init_done)
6201 fc_host_supported_classes(vha->host) =
6202 FC_COS_CLASS2 | FC_COS_CLASS3;
6204 nv->firmware_options_2 |= cpu_to_le32(BIT_8);
6206 if (vha->flags.init_done)
6207 fc_host_supported_classes(vha->host) = FC_COS_CLASS3;
6209 nv->firmware_options_2 &= ~cpu_to_le32(BIT_8);
6214 qlt_24xx_config_nvram_stage2(struct scsi_qla_host *vha,
6215 struct init_cb_24xx *icb)
6217 struct qla_hw_data *ha = vha->hw;
6219 if (!QLA_TGT_MODE_ENABLED())
6222 if (ha->tgt.node_name_set) {
6223 memcpy(icb->node_name, ha->tgt.tgt_node_name, WWN_SIZE);
6224 icb->firmware_options_1 |= cpu_to_le32(BIT_14);
6227 /* disable ZIO at start time. */
6228 if (!vha->flags.init_done) {
6230 tmp = le32_to_cpu(icb->firmware_options_2);
6231 tmp &= ~(BIT_3 | BIT_2 | BIT_1 | BIT_0);
6232 icb->firmware_options_2 = cpu_to_le32(tmp);
6237 qlt_81xx_config_nvram_stage1(struct scsi_qla_host *vha, struct nvram_81xx *nv)
6239 struct qla_hw_data *ha = vha->hw;
6243 if (!QLA_TGT_MODE_ENABLED())
6246 if (qla_tgt_mode_enabled(vha) || qla_dual_mode_enabled(vha)) {
6247 if (!ha->tgt.saved_set) {
6248 /* We save only once */
6249 ha->tgt.saved_exchange_count = nv->exchange_count;
6250 ha->tgt.saved_firmware_options_1 =
6251 nv->firmware_options_1;
6252 ha->tgt.saved_firmware_options_2 =
6253 nv->firmware_options_2;
6254 ha->tgt.saved_firmware_options_3 =
6255 nv->firmware_options_3;
6256 ha->tgt.saved_set = 1;
6259 if (qla_tgt_mode_enabled(vha)) {
6260 nv->exchange_count = cpu_to_le16(0xFFFF);
6262 if (ql_dm_tgt_ex_pct > 100) {
6263 ql_dm_tgt_ex_pct = 50;
6264 } else if (ql_dm_tgt_ex_pct == 100) {
6265 /* leave some for FW */
6266 ql_dm_tgt_ex_pct = 95;
6269 tmp = ha->orig_fw_xcb_count * ql_dm_tgt_ex_pct;
6274 nv->exchange_count = cpu_to_le16(t);
6277 /* Enable target mode */
6278 nv->firmware_options_1 |= cpu_to_le32(BIT_4);
6280 /* Disable ini mode, if requested */
6281 if (qla_tgt_mode_enabled(vha))
6282 nv->firmware_options_1 |= cpu_to_le32(BIT_5);
6283 /* Disable Full Login after LIP */
6284 nv->firmware_options_1 &= cpu_to_le32(~BIT_13);
6285 /* Enable initial LIP */
6286 nv->firmware_options_1 &= cpu_to_le32(~BIT_9);
6288 * clear BIT 15 explicitly as we have seen at
6289 * least a couple of instances where this was set
6290 * and this was causing the firmware to not be
6293 nv->firmware_options_1 &= cpu_to_le32(~BIT_15);
6294 if (ql2xtgt_tape_enable)
6295 /* Enable FC tape support */
6296 nv->firmware_options_2 |= cpu_to_le32(BIT_12);
6298 /* Disable FC tape support */
6299 nv->firmware_options_2 &= cpu_to_le32(~BIT_12);
6301 /* Disable Full Login after LIP */
6302 nv->host_p &= cpu_to_le32(~BIT_10);
6303 /* Enable target PRLI control */
6304 nv->firmware_options_2 |= cpu_to_le32(BIT_14);
6306 if (ha->tgt.saved_set) {
6307 nv->exchange_count = ha->tgt.saved_exchange_count;
6308 nv->firmware_options_1 =
6309 ha->tgt.saved_firmware_options_1;
6310 nv->firmware_options_2 =
6311 ha->tgt.saved_firmware_options_2;
6312 nv->firmware_options_3 =
6313 ha->tgt.saved_firmware_options_3;
6318 if (ha->tgt.enable_class_2) {
6319 if (vha->flags.init_done)
6320 fc_host_supported_classes(vha->host) =
6321 FC_COS_CLASS2 | FC_COS_CLASS3;
6323 nv->firmware_options_2 |= cpu_to_le32(BIT_8);
6325 if (vha->flags.init_done)
6326 fc_host_supported_classes(vha->host) = FC_COS_CLASS3;
6328 nv->firmware_options_2 &= ~cpu_to_le32(BIT_8);
6333 qlt_81xx_config_nvram_stage2(struct scsi_qla_host *vha,
6334 struct init_cb_81xx *icb)
6336 struct qla_hw_data *ha = vha->hw;
6338 if (!QLA_TGT_MODE_ENABLED())
6341 if (ha->tgt.node_name_set) {
6342 memcpy(icb->node_name, ha->tgt.tgt_node_name, WWN_SIZE);
6343 icb->firmware_options_1 |= cpu_to_le32(BIT_14);
6346 /* disable ZIO at start time. */
6347 if (!vha->flags.init_done) {
6349 tmp = le32_to_cpu(icb->firmware_options_2);
6350 tmp &= ~(BIT_3 | BIT_2 | BIT_1 | BIT_0);
6351 icb->firmware_options_2 = cpu_to_le32(tmp);
6357 qlt_83xx_iospace_config(struct qla_hw_data *ha)
6359 if (!QLA_TGT_MODE_ENABLED())
6362 ha->msix_count += 1; /* For ATIO Q */
6366 qlt_24xx_process_response_error(struct scsi_qla_host *vha,
6367 struct sts_entry_24xx *pkt)
6369 switch (pkt->entry_type) {
6370 case ABTS_RECV_24XX:
6371 case ABTS_RESP_24XX:
6373 case NOTIFY_ACK_TYPE:
6382 qlt_modify_vp_config(struct scsi_qla_host *vha,
6383 struct vp_config_entry_24xx *vpmod)
6385 /* enable target mode. Bit5 = 1 => disable */
6386 if (qla_tgt_mode_enabled(vha) || qla_dual_mode_enabled(vha))
6387 vpmod->options_idx1 &= ~BIT_5;
6389 /* Disable ini mode, if requested. bit4 = 1 => disable */
6390 if (qla_tgt_mode_enabled(vha))
6391 vpmod->options_idx1 &= ~BIT_4;
6395 qlt_probe_one_stage1(struct scsi_qla_host *base_vha, struct qla_hw_data *ha)
6397 if (!QLA_TGT_MODE_ENABLED())
6400 if (ha->mqenable || IS_QLA83XX(ha) || IS_QLA27XX(ha)) {
6401 ISP_ATIO_Q_IN(base_vha) = &ha->mqiobase->isp25mq.atio_q_in;
6402 ISP_ATIO_Q_OUT(base_vha) = &ha->mqiobase->isp25mq.atio_q_out;
6404 ISP_ATIO_Q_IN(base_vha) = &ha->iobase->isp24.atio_q_in;
6405 ISP_ATIO_Q_OUT(base_vha) = &ha->iobase->isp24.atio_q_out;
6408 mutex_init(&base_vha->vha_tgt.tgt_mutex);
6409 mutex_init(&base_vha->vha_tgt.tgt_host_action_mutex);
6410 qlt_clear_mode(base_vha);
6414 qla83xx_msix_atio_q(int irq, void *dev_id)
6416 struct rsp_que *rsp;
6417 scsi_qla_host_t *vha;
6418 struct qla_hw_data *ha;
6419 unsigned long flags;
6421 rsp = (struct rsp_que *) dev_id;
6423 vha = pci_get_drvdata(ha->pdev);
6425 spin_lock_irqsave(&ha->tgt.atio_lock, flags);
6427 qlt_24xx_process_atio_queue(vha, 0);
6429 spin_unlock_irqrestore(&ha->tgt.atio_lock, flags);
6435 qlt_handle_abts_recv_work(struct work_struct *work)
6437 struct qla_tgt_sess_op *op = container_of(work,
6438 struct qla_tgt_sess_op, work);
6439 scsi_qla_host_t *vha = op->vha;
6440 struct qla_hw_data *ha = vha->hw;
6441 unsigned long flags;
6443 if (qla2x00_reset_active(vha) || (op->chip_reset != ha->chip_reset))
6446 spin_lock_irqsave(&ha->tgt.atio_lock, flags);
6447 qlt_24xx_process_atio_queue(vha, 0);
6448 spin_unlock_irqrestore(&ha->tgt.atio_lock, flags);
6450 spin_lock_irqsave(&ha->hardware_lock, flags);
6451 qlt_response_pkt_all_vps(vha, (response_t *)&op->atio);
6452 spin_unlock_irqrestore(&ha->hardware_lock, flags);
6456 qlt_handle_abts_recv(struct scsi_qla_host *vha, response_t *pkt)
6458 struct qla_tgt_sess_op *op;
6460 op = kzalloc(sizeof(*op), GFP_ATOMIC);
6463 /* do not reach for ATIO queue here. This is best effort err
6464 * recovery at this point.
6466 qlt_response_pkt_all_vps(vha, pkt);
6470 memcpy(&op->atio, pkt, sizeof(*pkt));
6472 op->chip_reset = vha->hw->chip_reset;
6473 INIT_WORK(&op->work, qlt_handle_abts_recv_work);
6474 queue_work(qla_tgt_wq, &op->work);
6479 qlt_mem_alloc(struct qla_hw_data *ha)
6481 if (!QLA_TGT_MODE_ENABLED())
6484 ha->tgt.tgt_vp_map = kzalloc(sizeof(struct qla_tgt_vp_map) *
6485 MAX_MULTI_ID_FABRIC, GFP_KERNEL);
6486 if (!ha->tgt.tgt_vp_map)
6489 ha->tgt.atio_ring = dma_alloc_coherent(&ha->pdev->dev,
6490 (ha->tgt.atio_q_length + 1) * sizeof(struct atio_from_isp),
6491 &ha->tgt.atio_dma, GFP_KERNEL);
6492 if (!ha->tgt.atio_ring) {
6493 kfree(ha->tgt.tgt_vp_map);
6500 qlt_mem_free(struct qla_hw_data *ha)
6502 if (!QLA_TGT_MODE_ENABLED())
6505 if (ha->tgt.atio_ring) {
6506 dma_free_coherent(&ha->pdev->dev, (ha->tgt.atio_q_length + 1) *
6507 sizeof(struct atio_from_isp), ha->tgt.atio_ring,
6510 kfree(ha->tgt.tgt_vp_map);
6513 /* vport_slock to be held by the caller */
6515 qlt_update_vp_map(struct scsi_qla_host *vha, int cmd)
6517 if (!QLA_TGT_MODE_ENABLED())
6522 vha->hw->tgt.tgt_vp_map[vha->vp_idx].vha = vha;
6525 vha->hw->tgt.tgt_vp_map[vha->d_id.b.al_pa].idx = vha->vp_idx;
6528 vha->hw->tgt.tgt_vp_map[vha->vp_idx].vha = NULL;
6531 vha->hw->tgt.tgt_vp_map[vha->d_id.b.al_pa].idx = 0;
6536 static int __init qlt_parse_ini_mode(void)
6538 if (strcasecmp(qlini_mode, QLA2XXX_INI_MODE_STR_EXCLUSIVE) == 0)
6539 ql2x_ini_mode = QLA2XXX_INI_MODE_EXCLUSIVE;
6540 else if (strcasecmp(qlini_mode, QLA2XXX_INI_MODE_STR_DISABLED) == 0)
6541 ql2x_ini_mode = QLA2XXX_INI_MODE_DISABLED;
6542 else if (strcasecmp(qlini_mode, QLA2XXX_INI_MODE_STR_ENABLED) == 0)
6543 ql2x_ini_mode = QLA2XXX_INI_MODE_ENABLED;
6544 else if (strcasecmp(qlini_mode, QLA2XXX_INI_MODE_STR_DUAL) == 0)
6545 ql2x_ini_mode = QLA2XXX_INI_MODE_DUAL;
6552 int __init qlt_init(void)
6556 if (!qlt_parse_ini_mode()) {
6557 ql_log(ql_log_fatal, NULL, 0xe06b,
6558 "qlt_parse_ini_mode() failed\n");
6562 if (!QLA_TGT_MODE_ENABLED())
6565 qla_tgt_mgmt_cmd_cachep = kmem_cache_create("qla_tgt_mgmt_cmd_cachep",
6566 sizeof(struct qla_tgt_mgmt_cmd), __alignof__(struct
6567 qla_tgt_mgmt_cmd), 0, NULL);
6568 if (!qla_tgt_mgmt_cmd_cachep) {
6569 ql_log(ql_log_fatal, NULL, 0xe06d,
6570 "kmem_cache_create for qla_tgt_mgmt_cmd_cachep failed\n");
6574 qla_tgt_plogi_cachep = kmem_cache_create("qla_tgt_plogi_cachep",
6575 sizeof(struct qlt_plogi_ack_t), __alignof__(struct qlt_plogi_ack_t),
6578 if (!qla_tgt_plogi_cachep) {
6579 ql_log(ql_log_fatal, NULL, 0xe06d,
6580 "kmem_cache_create for qla_tgt_plogi_cachep failed\n");
6582 goto out_mgmt_cmd_cachep;
6585 qla_tgt_mgmt_cmd_mempool = mempool_create(25, mempool_alloc_slab,
6586 mempool_free_slab, qla_tgt_mgmt_cmd_cachep);
6587 if (!qla_tgt_mgmt_cmd_mempool) {
6588 ql_log(ql_log_fatal, NULL, 0xe06e,
6589 "mempool_create for qla_tgt_mgmt_cmd_mempool failed\n");
6591 goto out_plogi_cachep;
6594 qla_tgt_wq = alloc_workqueue("qla_tgt_wq", 0, 0);
6596 ql_log(ql_log_fatal, NULL, 0xe06f,
6597 "alloc_workqueue for qla_tgt_wq failed\n");
6599 goto out_cmd_mempool;
6602 * Return 1 to signal that initiator-mode is being disabled
6604 return (ql2x_ini_mode == QLA2XXX_INI_MODE_DISABLED) ? 1 : 0;
6607 mempool_destroy(qla_tgt_mgmt_cmd_mempool);
6609 kmem_cache_destroy(qla_tgt_plogi_cachep);
6610 out_mgmt_cmd_cachep:
6611 kmem_cache_destroy(qla_tgt_mgmt_cmd_cachep);
6617 if (!QLA_TGT_MODE_ENABLED())
6620 destroy_workqueue(qla_tgt_wq);
6621 mempool_destroy(qla_tgt_mgmt_cmd_mempool);
6622 kmem_cache_destroy(qla_tgt_plogi_cachep);
6623 kmem_cache_destroy(qla_tgt_mgmt_cmd_cachep);