2 * qla_target.c SCSI LLD infrastructure for QLogic 22xx/23xx/24xx/25xx
4 * based on qla2x00t.c code:
6 * Copyright (C) 2004 - 2010 Vladislav Bolkhovitin <vst@vlnb.net>
7 * Copyright (C) 2004 - 2005 Leonid Stoljar
8 * Copyright (C) 2006 Nathaniel Clark <nate@misrule.us>
9 * Copyright (C) 2006 - 2010 ID7 Ltd.
11 * Forward port and refactoring to modern qla2xxx and target/configfs
13 * Copyright (C) 2010-2013 Nicholas A. Bellinger <nab@kernel.org>
15 * This program is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public License
17 * as published by the Free Software Foundation, version 2
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
26 #include <linux/module.h>
27 #include <linux/init.h>
28 #include <linux/types.h>
29 #include <linux/blkdev.h>
30 #include <linux/interrupt.h>
31 #include <linux/pci.h>
32 #include <linux/delay.h>
33 #include <linux/list.h>
34 #include <linux/workqueue.h>
35 #include <asm/unaligned.h>
36 #include <scsi/scsi.h>
37 #include <scsi/scsi_host.h>
38 #include <scsi/scsi_tcq.h>
39 #include <target/target_core_base.h>
40 #include <target/target_core_fabric.h>
43 #include "qla_target.h"
45 static int ql2xtgt_tape_enable;
46 module_param(ql2xtgt_tape_enable, int, S_IRUGO|S_IWUSR);
47 MODULE_PARM_DESC(ql2xtgt_tape_enable,
48 "Enables Sequence level error recovery (aka FC Tape). Default is 0 - no SLER. 1 - Enable SLER.");
50 static char *qlini_mode = QLA2XXX_INI_MODE_STR_ENABLED;
51 module_param(qlini_mode, charp, S_IRUGO);
52 MODULE_PARM_DESC(qlini_mode,
53 "Determines when initiator mode will be enabled. Possible values: "
54 "\"exclusive\" - initiator mode will be enabled on load, "
55 "disabled on enabling target mode and then on disabling target mode "
57 "\"disabled\" - initiator mode will never be enabled; "
58 "\"dual\" - Initiator Modes will be enabled. Target Mode can be activated "
60 "\"enabled\" (default) - initiator mode will always stay enabled.");
62 static int ql_dm_tgt_ex_pct = 0;
63 module_param(ql_dm_tgt_ex_pct, int, S_IRUGO|S_IWUSR);
64 MODULE_PARM_DESC(ql_dm_tgt_ex_pct,
65 "For Dual Mode (qlini_mode=dual), this parameter determines "
66 "the percentage of exchanges/cmds FW will allocate resources "
69 int ql2x_ini_mode = QLA2XXX_INI_MODE_EXCLUSIVE;
71 static int temp_sam_status = SAM_STAT_BUSY;
74 * From scsi/fc/fc_fcp.h
76 enum fcp_resp_rsp_codes {
78 FCP_DATA_LEN_INVALID = 1,
79 FCP_CMND_FIELDS_INVALID = 2,
80 FCP_DATA_PARAM_MISMATCH = 3,
83 FCP_TMF_INVALID_LUN = 9,
87 * fc_pri_ta from scsi/fc/fc_fcp.h
89 #define FCP_PTA_SIMPLE 0 /* simple task attribute */
90 #define FCP_PTA_HEADQ 1 /* head of queue task attribute */
91 #define FCP_PTA_ORDERED 2 /* ordered task attribute */
92 #define FCP_PTA_ACA 4 /* auto. contingent allegiance */
93 #define FCP_PTA_MASK 7 /* mask for task attribute field */
94 #define FCP_PRI_SHIFT 3 /* priority field starts in bit 3 */
95 #define FCP_PRI_RESVD_MASK 0x80 /* reserved bits in priority field */
98 * This driver calls qla2x00_alloc_iocbs() and qla2x00_issue_marker(), which
99 * must be called under HW lock and could unlock/lock it inside.
100 * It isn't an issue, since in the current implementation on the time when
101 * those functions are called:
103 * - Either context is IRQ and only IRQ handler can modify HW data,
104 * including rings related fields,
106 * - Or access to target mode variables from struct qla_tgt doesn't
107 * cross those functions boundaries, except tgt_stop, which
108 * additionally protected by irq_cmd_count.
110 /* Predefs for callbacks handed to qla2xxx LLD */
111 static void qlt_24xx_atio_pkt(struct scsi_qla_host *ha,
112 struct atio_from_isp *pkt, uint8_t);
113 static void qlt_response_pkt(struct scsi_qla_host *ha, struct rsp_que *rsp,
115 static int qlt_issue_task_mgmt(struct fc_port *sess, u64 lun,
116 int fn, void *iocb, int flags);
117 static void qlt_send_term_exchange(struct qla_qpair *, struct qla_tgt_cmd
118 *cmd, struct atio_from_isp *atio, int ha_locked, int ul_abort);
119 static void qlt_alloc_qfull_cmd(struct scsi_qla_host *vha,
120 struct atio_from_isp *atio, uint16_t status, int qfull);
121 static void qlt_disable_vha(struct scsi_qla_host *vha);
122 static void qlt_clear_tgt_db(struct qla_tgt *tgt);
123 static void qlt_send_notify_ack(struct qla_qpair *qpair,
124 struct imm_ntfy_from_isp *ntfy,
125 uint32_t add_flags, uint16_t resp_code, int resp_code_valid,
126 uint16_t srr_flags, uint16_t srr_reject_code, uint8_t srr_explan);
127 static void qlt_send_term_imm_notif(struct scsi_qla_host *vha,
128 struct imm_ntfy_from_isp *imm, int ha_locked);
129 static struct fc_port *qlt_create_sess(struct scsi_qla_host *vha,
130 fc_port_t *fcport, bool local);
131 void qlt_unreg_sess(struct fc_port *sess);
132 static void qlt_24xx_handle_abts(struct scsi_qla_host *,
133 struct abts_recv_from_24xx *);
134 static void qlt_send_busy(struct qla_qpair *, struct atio_from_isp *,
140 static struct kmem_cache *qla_tgt_mgmt_cmd_cachep;
141 static struct kmem_cache *qla_tgt_plogi_cachep;
142 static mempool_t *qla_tgt_mgmt_cmd_mempool;
143 static struct workqueue_struct *qla_tgt_wq;
144 static DEFINE_MUTEX(qla_tgt_mutex);
145 static LIST_HEAD(qla_tgt_glist);
147 static const char *prot_op_str(u32 prot_op)
150 case TARGET_PROT_NORMAL: return "NORMAL";
151 case TARGET_PROT_DIN_INSERT: return "DIN_INSERT";
152 case TARGET_PROT_DOUT_INSERT: return "DOUT_INSERT";
153 case TARGET_PROT_DIN_STRIP: return "DIN_STRIP";
154 case TARGET_PROT_DOUT_STRIP: return "DOUT_STRIP";
155 case TARGET_PROT_DIN_PASS: return "DIN_PASS";
156 case TARGET_PROT_DOUT_PASS: return "DOUT_PASS";
157 default: return "UNKNOWN";
161 /* This API intentionally takes dest as a parameter, rather than returning
162 * int value to avoid caller forgetting to issue wmb() after the store */
163 void qlt_do_generation_tick(struct scsi_qla_host *vha, int *dest)
165 scsi_qla_host_t *base_vha = pci_get_drvdata(vha->hw->pdev);
166 *dest = atomic_inc_return(&base_vha->generation_tick);
171 /* Might release hw lock, then reaquire!! */
172 static inline int qlt_issue_marker(struct scsi_qla_host *vha, int vha_locked)
174 /* Send marker if required */
175 if (unlikely(vha->marker_needed != 0)) {
176 int rc = qla2x00_issue_marker(vha, vha_locked);
177 if (rc != QLA_SUCCESS) {
178 ql_dbg(ql_dbg_tgt, vha, 0xe03d,
179 "qla_target(%d): issue_marker() failed\n",
188 struct scsi_qla_host *qlt_find_host_by_d_id(struct scsi_qla_host *vha,
191 struct scsi_qla_host *host;
194 if ((vha->d_id.b.area == d_id[1]) && (vha->d_id.b.domain == d_id[0]) &&
195 (vha->d_id.b.al_pa == d_id[2]))
198 key = (uint32_t)d_id[0] << 16;
199 key |= (uint32_t)d_id[1] << 8;
200 key |= (uint32_t)d_id[2];
202 host = btree_lookup32(&vha->hw->tgt.host_map, key);
204 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf005,
205 "Unable to find host %06x\n", key);
211 struct scsi_qla_host *qlt_find_host_by_vp_idx(struct scsi_qla_host *vha,
214 struct qla_hw_data *ha = vha->hw;
216 if (vha->vp_idx == vp_idx)
219 BUG_ON(ha->tgt.tgt_vp_map == NULL);
220 if (likely(test_bit(vp_idx, ha->vp_idx_map)))
221 return ha->tgt.tgt_vp_map[vp_idx].vha;
226 static inline void qlt_incr_num_pend_cmds(struct scsi_qla_host *vha)
230 spin_lock_irqsave(&vha->hw->tgt.q_full_lock, flags);
232 vha->hw->tgt.num_pend_cmds++;
233 if (vha->hw->tgt.num_pend_cmds > vha->qla_stats.stat_max_pend_cmds)
234 vha->qla_stats.stat_max_pend_cmds =
235 vha->hw->tgt.num_pend_cmds;
236 spin_unlock_irqrestore(&vha->hw->tgt.q_full_lock, flags);
238 static inline void qlt_decr_num_pend_cmds(struct scsi_qla_host *vha)
242 spin_lock_irqsave(&vha->hw->tgt.q_full_lock, flags);
243 vha->hw->tgt.num_pend_cmds--;
244 spin_unlock_irqrestore(&vha->hw->tgt.q_full_lock, flags);
248 static void qlt_queue_unknown_atio(scsi_qla_host_t *vha,
249 struct atio_from_isp *atio, uint8_t ha_locked)
251 struct qla_tgt_sess_op *u;
252 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
256 ql_dbg(ql_dbg_async, vha, 0x502c,
257 "qla_target(%d): dropping unknown ATIO_TYPE7, because tgt is being stopped",
262 u = kzalloc(sizeof(*u), GFP_ATOMIC);
267 memcpy(&u->atio, atio, sizeof(*atio));
268 INIT_LIST_HEAD(&u->cmd_list);
270 spin_lock_irqsave(&vha->cmd_list_lock, flags);
271 list_add_tail(&u->cmd_list, &vha->unknown_atio_list);
272 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
274 schedule_delayed_work(&vha->unknown_atio_work, 1);
280 qlt_send_term_exchange(vha->hw->base_qpair, NULL, atio, ha_locked, 0);
284 static void qlt_try_to_dequeue_unknown_atios(struct scsi_qla_host *vha,
287 struct qla_tgt_sess_op *u, *t;
288 scsi_qla_host_t *host;
289 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
293 list_for_each_entry_safe(u, t, &vha->unknown_atio_list, cmd_list) {
295 ql_dbg(ql_dbg_async, vha, 0x502e,
296 "Freeing unknown %s %p, because of Abort\n",
298 qlt_send_term_exchange(vha->hw->base_qpair, NULL,
299 &u->atio, ha_locked, 0);
303 host = qlt_find_host_by_d_id(vha, u->atio.u.isp24.fcp_hdr.d_id);
305 ql_dbg(ql_dbg_async, vha, 0x502f,
306 "Requeuing unknown ATIO_TYPE7 %p\n", u);
307 qlt_24xx_atio_pkt(host, &u->atio, ha_locked);
308 } else if (tgt->tgt_stop) {
309 ql_dbg(ql_dbg_async, vha, 0x503a,
310 "Freeing unknown %s %p, because tgt is being stopped\n",
312 qlt_send_term_exchange(vha->hw->base_qpair, NULL,
313 &u->atio, ha_locked, 0);
315 ql_dbg(ql_dbg_async, vha, 0x503d,
316 "Reschedule u %p, vha %p, host %p\n", u, vha, host);
319 schedule_delayed_work(&vha->unknown_atio_work,
326 spin_lock_irqsave(&vha->cmd_list_lock, flags);
327 list_del(&u->cmd_list);
328 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
333 void qlt_unknown_atio_work_fn(struct work_struct *work)
335 struct scsi_qla_host *vha = container_of(to_delayed_work(work),
336 struct scsi_qla_host, unknown_atio_work);
338 qlt_try_to_dequeue_unknown_atios(vha, 0);
341 static bool qlt_24xx_atio_pkt_all_vps(struct scsi_qla_host *vha,
342 struct atio_from_isp *atio, uint8_t ha_locked)
344 ql_dbg(ql_dbg_tgt, vha, 0xe072,
345 "%s: qla_target(%d): type %x ox_id %04x\n",
346 __func__, vha->vp_idx, atio->u.raw.entry_type,
347 be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id));
349 switch (atio->u.raw.entry_type) {
352 struct scsi_qla_host *host = qlt_find_host_by_d_id(vha,
353 atio->u.isp24.fcp_hdr.d_id);
354 if (unlikely(NULL == host)) {
355 ql_dbg(ql_dbg_tgt, vha, 0xe03e,
356 "qla_target(%d): Received ATIO_TYPE7 "
357 "with unknown d_id %x:%x:%x\n", vha->vp_idx,
358 atio->u.isp24.fcp_hdr.d_id[0],
359 atio->u.isp24.fcp_hdr.d_id[1],
360 atio->u.isp24.fcp_hdr.d_id[2]);
363 qlt_queue_unknown_atio(vha, atio, ha_locked);
366 if (unlikely(!list_empty(&vha->unknown_atio_list)))
367 qlt_try_to_dequeue_unknown_atios(vha, ha_locked);
369 qlt_24xx_atio_pkt(host, atio, ha_locked);
373 case IMMED_NOTIFY_TYPE:
375 struct scsi_qla_host *host = vha;
376 struct imm_ntfy_from_isp *entry =
377 (struct imm_ntfy_from_isp *)atio;
379 qlt_issue_marker(vha, ha_locked);
381 if ((entry->u.isp24.vp_index != 0xFF) &&
382 (entry->u.isp24.nport_handle != 0xFFFF)) {
383 host = qlt_find_host_by_vp_idx(vha,
384 entry->u.isp24.vp_index);
385 if (unlikely(!host)) {
386 ql_dbg(ql_dbg_tgt, vha, 0xe03f,
387 "qla_target(%d): Received "
388 "ATIO (IMMED_NOTIFY_TYPE) "
389 "with unknown vp_index %d\n",
390 vha->vp_idx, entry->u.isp24.vp_index);
394 qlt_24xx_atio_pkt(host, atio, ha_locked);
398 case VP_RPT_ID_IOCB_TYPE:
399 qla24xx_report_id_acquisition(vha,
400 (struct vp_rpt_id_entry_24xx *)atio);
405 struct abts_recv_from_24xx *entry =
406 (struct abts_recv_from_24xx *)atio;
407 struct scsi_qla_host *host = qlt_find_host_by_vp_idx(vha,
411 if (unlikely(!host)) {
412 ql_dbg(ql_dbg_tgt, vha, 0xe00a,
413 "qla_target(%d): Response pkt (ABTS_RECV_24XX) "
414 "received, with unknown vp_index %d\n",
415 vha->vp_idx, entry->vp_index);
419 spin_lock_irqsave(&host->hw->hardware_lock, flags);
420 qlt_24xx_handle_abts(host, (struct abts_recv_from_24xx *)atio);
422 spin_unlock_irqrestore(&host->hw->hardware_lock, flags);
426 /* case PUREX_IOCB_TYPE: ql2xmvasynctoatio */
429 ql_dbg(ql_dbg_tgt, vha, 0xe040,
430 "qla_target(%d): Received unknown ATIO atio "
431 "type %x\n", vha->vp_idx, atio->u.raw.entry_type);
438 void qlt_response_pkt_all_vps(struct scsi_qla_host *vha,
439 struct rsp_que *rsp, response_t *pkt)
441 switch (pkt->entry_type) {
443 ql_dbg(ql_dbg_tgt, vha, 0xe073,
444 "qla_target(%d):%s: CRC2 Response pkt\n",
445 vha->vp_idx, __func__);
448 struct ctio7_from_24xx *entry = (struct ctio7_from_24xx *)pkt;
449 struct scsi_qla_host *host = qlt_find_host_by_vp_idx(vha,
451 if (unlikely(!host)) {
452 ql_dbg(ql_dbg_tgt, vha, 0xe041,
453 "qla_target(%d): Response pkt (CTIO_TYPE7) "
454 "received, with unknown vp_index %d\n",
455 vha->vp_idx, entry->vp_index);
458 qlt_response_pkt(host, rsp, pkt);
462 case IMMED_NOTIFY_TYPE:
464 struct scsi_qla_host *host = vha;
465 struct imm_ntfy_from_isp *entry =
466 (struct imm_ntfy_from_isp *)pkt;
468 host = qlt_find_host_by_vp_idx(vha, entry->u.isp24.vp_index);
469 if (unlikely(!host)) {
470 ql_dbg(ql_dbg_tgt, vha, 0xe042,
471 "qla_target(%d): Response pkt (IMMED_NOTIFY_TYPE) "
472 "received, with unknown vp_index %d\n",
473 vha->vp_idx, entry->u.isp24.vp_index);
476 qlt_response_pkt(host, rsp, pkt);
480 case NOTIFY_ACK_TYPE:
482 struct scsi_qla_host *host = vha;
483 struct nack_to_isp *entry = (struct nack_to_isp *)pkt;
485 if (0xFF != entry->u.isp24.vp_index) {
486 host = qlt_find_host_by_vp_idx(vha,
487 entry->u.isp24.vp_index);
488 if (unlikely(!host)) {
489 ql_dbg(ql_dbg_tgt, vha, 0xe043,
490 "qla_target(%d): Response "
491 "pkt (NOTIFY_ACK_TYPE) "
492 "received, with unknown "
493 "vp_index %d\n", vha->vp_idx,
494 entry->u.isp24.vp_index);
498 qlt_response_pkt(host, rsp, pkt);
504 struct abts_recv_from_24xx *entry =
505 (struct abts_recv_from_24xx *)pkt;
506 struct scsi_qla_host *host = qlt_find_host_by_vp_idx(vha,
508 if (unlikely(!host)) {
509 ql_dbg(ql_dbg_tgt, vha, 0xe044,
510 "qla_target(%d): Response pkt "
511 "(ABTS_RECV_24XX) received, with unknown "
512 "vp_index %d\n", vha->vp_idx, entry->vp_index);
515 qlt_response_pkt(host, rsp, pkt);
521 struct abts_resp_to_24xx *entry =
522 (struct abts_resp_to_24xx *)pkt;
523 struct scsi_qla_host *host = qlt_find_host_by_vp_idx(vha,
525 if (unlikely(!host)) {
526 ql_dbg(ql_dbg_tgt, vha, 0xe045,
527 "qla_target(%d): Response pkt "
528 "(ABTS_RECV_24XX) received, with unknown "
529 "vp_index %d\n", vha->vp_idx, entry->vp_index);
532 qlt_response_pkt(host, rsp, pkt);
537 qlt_response_pkt(vha, rsp, pkt);
544 * All qlt_plogi_ack_t operations are protected by hardware_lock
546 static int qla24xx_post_nack_work(struct scsi_qla_host *vha, fc_port_t *fcport,
547 struct imm_ntfy_from_isp *ntfy, int type)
549 struct qla_work_evt *e;
550 e = qla2x00_alloc_work(vha, QLA_EVT_NACK);
552 return QLA_FUNCTION_FAILED;
554 e->u.nack.fcport = fcport;
555 e->u.nack.type = type;
556 memcpy(e->u.nack.iocb, ntfy, sizeof(struct imm_ntfy_from_isp));
557 return qla2x00_post_work(vha, e);
561 void qla2x00_async_nack_sp_done(void *s, int res)
563 struct srb *sp = (struct srb *)s;
564 struct scsi_qla_host *vha = sp->vha;
567 ql_dbg(ql_dbg_disc, vha, 0x20f2,
568 "Async done-%s res %x %8phC type %d\n",
569 sp->name, res, sp->fcport->port_name, sp->type);
571 spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
572 sp->fcport->flags &= ~FCF_ASYNC_SENT;
573 sp->fcport->chip_reset = vha->hw->chip_reset;
577 sp->fcport->login_gen++;
578 sp->fcport->fw_login_state = DSC_LS_PLOGI_COMP;
579 sp->fcport->logout_on_delete = 1;
580 sp->fcport->plogi_nack_done_deadline = jiffies + HZ;
584 sp->fcport->fw_login_state = DSC_LS_PRLI_COMP;
585 sp->fcport->deleted = 0;
587 if (!sp->fcport->login_succ &&
588 !IS_SW_RESV_ADDR(sp->fcport->d_id)) {
589 sp->fcport->login_succ = 1;
593 if (!IS_IIDMA_CAPABLE(vha->hw) ||
594 !vha->hw->flags.gpsc_supported) {
595 ql_dbg(ql_dbg_disc, vha, 0x20f3,
596 "%s %d %8phC post upd_fcport fcp_cnt %d\n",
598 sp->fcport->port_name,
601 qla24xx_post_upd_fcport_work(vha, sp->fcport);
603 ql_dbg(ql_dbg_disc, vha, 0x20f5,
604 "%s %d %8phC post gpsc fcp_cnt %d\n",
606 sp->fcport->port_name,
609 qla24xx_post_gpsc_work(vha, sp->fcport);
615 sp->fcport->login_gen++;
616 sp->fcport->fw_login_state = DSC_LS_PORT_UNAVAIL;
617 qlt_logo_completion_handler(sp->fcport, MBS_COMMAND_COMPLETE);
620 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
625 int qla24xx_async_notify_ack(scsi_qla_host_t *vha, fc_port_t *fcport,
626 struct imm_ntfy_from_isp *ntfy, int type)
628 int rval = QLA_FUNCTION_FAILED;
632 fcport->flags |= FCF_ASYNC_SENT;
635 fcport->fw_login_state = DSC_LS_PLOGI_PEND;
639 fcport->fw_login_state = DSC_LS_PRLI_PEND;
644 fcport->fw_login_state = DSC_LS_LOGO_PEND;
649 sp = qla2x00_get_sp(vha, fcport, GFP_ATOMIC);
656 qla2x00_init_timer(sp, qla2x00_get_async_timeout(vha)+2);
658 sp->u.iocb_cmd.u.nack.ntfy = ntfy;
660 sp->done = qla2x00_async_nack_sp_done;
662 rval = qla2x00_start_sp(sp);
663 if (rval != QLA_SUCCESS)
666 ql_dbg(ql_dbg_disc, vha, 0x20f4,
667 "Async-%s %8phC hndl %x %s\n",
668 sp->name, fcport->port_name, sp->handle, c);
675 fcport->flags &= ~FCF_ASYNC_SENT;
679 void qla24xx_do_nack_work(struct scsi_qla_host *vha, struct qla_work_evt *e)
684 switch (e->u.nack.type) {
686 mutex_lock(&vha->vha_tgt.tgt_mutex);
687 t = qlt_create_sess(vha, e->u.nack.fcport, 0);
688 mutex_unlock(&vha->vha_tgt.tgt_mutex);
690 ql_log(ql_log_info, vha, 0xd034,
691 "%s create sess success %p", __func__, t);
692 spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
693 /* create sess has an extra kref */
694 vha->hw->tgt.tgt_ops->put_sess(e->u.nack.fcport);
695 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
699 qla24xx_async_notify_ack(vha, e->u.nack.fcport,
700 (struct imm_ntfy_from_isp*)e->u.nack.iocb, e->u.nack.type);
703 void qla24xx_delete_sess_fn(struct work_struct *work)
705 fc_port_t *fcport = container_of(work, struct fc_port, del_work);
706 struct qla_hw_data *ha = fcport->vha->hw;
709 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
711 if (fcport->se_sess) {
712 ha->tgt.tgt_ops->shutdown_sess(fcport);
713 ha->tgt.tgt_ops->put_sess(fcport);
715 qlt_unreg_sess(fcport);
717 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
721 * Called from qla2x00_reg_remote_port()
723 void qlt_fc_port_added(struct scsi_qla_host *vha, fc_port_t *fcport)
725 struct qla_hw_data *ha = vha->hw;
726 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
727 struct fc_port *sess = fcport;
730 if (!vha->hw->tgt.tgt_ops)
733 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
735 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
739 if (fcport->disc_state == DSC_DELETE_PEND) {
740 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
744 if (!sess->se_sess) {
745 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
747 mutex_lock(&vha->vha_tgt.tgt_mutex);
748 sess = qlt_create_sess(vha, fcport, false);
749 mutex_unlock(&vha->vha_tgt.tgt_mutex);
751 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
753 if (fcport->fw_login_state == DSC_LS_PRLI_COMP) {
754 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
758 if (!kref_get_unless_zero(&sess->sess_kref)) {
759 ql_dbg(ql_dbg_disc, vha, 0x2107,
760 "%s: kref_get fail sess %8phC \n",
761 __func__, sess->port_name);
762 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
766 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04c,
767 "qla_target(%u): %ssession for port %8phC "
768 "(loop ID %d) reappeared\n", vha->vp_idx,
769 sess->local ? "local " : "", sess->port_name, sess->loop_id);
771 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf007,
772 "Reappeared sess %p\n", sess);
774 ha->tgt.tgt_ops->update_sess(sess, fcport->d_id,
776 (fcport->flags & FCF_CONF_COMP_SUPPORTED));
779 if (sess && sess->local) {
780 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04d,
781 "qla_target(%u): local session for "
782 "port %8phC (loop ID %d) became global\n", vha->vp_idx,
783 fcport->port_name, sess->loop_id);
786 ha->tgt.tgt_ops->put_sess(sess);
787 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
791 * This is a zero-base ref-counting solution, since hardware_lock
792 * guarantees that ref_count is not modified concurrently.
793 * Upon successful return content of iocb is undefined
795 static struct qlt_plogi_ack_t *
796 qlt_plogi_ack_find_add(struct scsi_qla_host *vha, port_id_t *id,
797 struct imm_ntfy_from_isp *iocb)
799 struct qlt_plogi_ack_t *pla;
801 list_for_each_entry(pla, &vha->plogi_ack_list, list) {
802 if (pla->id.b24 == id->b24) {
803 qlt_send_term_imm_notif(vha, &pla->iocb, 1);
804 memcpy(&pla->iocb, iocb, sizeof(pla->iocb));
809 pla = kmem_cache_zalloc(qla_tgt_plogi_cachep, GFP_ATOMIC);
811 ql_dbg(ql_dbg_async, vha, 0x5088,
812 "qla_target(%d): Allocation of plogi_ack failed\n",
817 memcpy(&pla->iocb, iocb, sizeof(pla->iocb));
819 list_add_tail(&pla->list, &vha->plogi_ack_list);
824 void qlt_plogi_ack_unref(struct scsi_qla_host *vha,
825 struct qlt_plogi_ack_t *pla)
827 struct imm_ntfy_from_isp *iocb = &pla->iocb;
830 fc_port_t *fcport = pla->fcport;
832 BUG_ON(!pla->ref_count);
838 ql_dbg(ql_dbg_disc, vha, 0x5089,
839 "Sending PLOGI ACK to wwn %8phC s_id %02x:%02x:%02x loop_id %#04x"
840 " exch %#x ox_id %#x\n", iocb->u.isp24.port_name,
841 iocb->u.isp24.port_id[2], iocb->u.isp24.port_id[1],
842 iocb->u.isp24.port_id[0],
843 le16_to_cpu(iocb->u.isp24.nport_handle),
844 iocb->u.isp24.exchange_address, iocb->ox_id);
846 port_id.b.domain = iocb->u.isp24.port_id[2];
847 port_id.b.area = iocb->u.isp24.port_id[1];
848 port_id.b.al_pa = iocb->u.isp24.port_id[0];
849 port_id.b.rsvd_1 = 0;
851 loop_id = le16_to_cpu(iocb->u.isp24.nport_handle);
853 fcport->loop_id = loop_id;
854 fcport->d_id = port_id;
855 qla24xx_post_nack_work(vha, fcport, iocb, SRB_NACK_PLOGI);
857 list_for_each_entry(fcport, &vha->vp_fcports, list) {
858 if (fcport->plogi_link[QLT_PLOGI_LINK_SAME_WWN] == pla)
859 fcport->plogi_link[QLT_PLOGI_LINK_SAME_WWN] = NULL;
860 if (fcport->plogi_link[QLT_PLOGI_LINK_CONFLICT] == pla)
861 fcport->plogi_link[QLT_PLOGI_LINK_CONFLICT] = NULL;
864 list_del(&pla->list);
865 kmem_cache_free(qla_tgt_plogi_cachep, pla);
869 qlt_plogi_ack_link(struct scsi_qla_host *vha, struct qlt_plogi_ack_t *pla,
870 struct fc_port *sess, enum qlt_plogi_link_t link)
872 struct imm_ntfy_from_isp *iocb = &pla->iocb;
873 /* Inc ref_count first because link might already be pointing at pla */
876 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf097,
877 "Linking sess %p [%d] wwn %8phC with PLOGI ACK to wwn %8phC"
878 " s_id %02x:%02x:%02x, ref=%d pla %p link %d\n",
879 sess, link, sess->port_name,
880 iocb->u.isp24.port_name, iocb->u.isp24.port_id[2],
881 iocb->u.isp24.port_id[1], iocb->u.isp24.port_id[0],
882 pla->ref_count, pla, link);
884 if (sess->plogi_link[link])
885 qlt_plogi_ack_unref(vha, sess->plogi_link[link]);
887 if (link == QLT_PLOGI_LINK_SAME_WWN)
890 sess->plogi_link[link] = pla;
894 /* These fields must be initialized by the caller */
897 * number of cmds dropped while we were waiting for
898 * initiator to ack LOGO initialize to 1 if LOGO is
899 * triggered by a command, otherwise, to 0
903 /* These fields are used by callee */
904 struct list_head list;
908 qlt_send_first_logo(struct scsi_qla_host *vha, qlt_port_logo_t *logo)
910 qlt_port_logo_t *tmp;
913 mutex_lock(&vha->vha_tgt.tgt_mutex);
915 list_for_each_entry(tmp, &vha->logo_list, list) {
916 if (tmp->id.b24 == logo->id.b24) {
917 tmp->cmd_count += logo->cmd_count;
918 mutex_unlock(&vha->vha_tgt.tgt_mutex);
923 list_add_tail(&logo->list, &vha->logo_list);
925 mutex_unlock(&vha->vha_tgt.tgt_mutex);
927 res = qla24xx_els_dcmd_iocb(vha, ELS_DCMD_LOGO, logo->id);
929 mutex_lock(&vha->vha_tgt.tgt_mutex);
930 list_del(&logo->list);
931 mutex_unlock(&vha->vha_tgt.tgt_mutex);
933 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf098,
934 "Finished LOGO to %02x:%02x:%02x, dropped %d cmds, res = %#x\n",
935 logo->id.b.domain, logo->id.b.area, logo->id.b.al_pa,
936 logo->cmd_count, res);
939 static void qlt_free_session_done(struct work_struct *work)
941 struct fc_port *sess = container_of(work, struct fc_port,
943 struct qla_tgt *tgt = sess->tgt;
944 struct scsi_qla_host *vha = sess->vha;
945 struct qla_hw_data *ha = vha->hw;
947 bool logout_started = false;
949 scsi_qla_host_t *base_vha;
951 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf084,
952 "%s: se_sess %p / sess %p from port %8phC loop_id %#04x"
953 " s_id %02x:%02x:%02x logout %d keep %d els_logo %d\n",
954 __func__, sess->se_sess, sess, sess->port_name, sess->loop_id,
955 sess->d_id.b.domain, sess->d_id.b.area, sess->d_id.b.al_pa,
956 sess->logout_on_delete, sess->keep_nport_handle,
957 sess->send_els_logo);
960 if (!IS_SW_RESV_ADDR(sess->d_id)) {
961 if (sess->send_els_logo) {
962 qlt_port_logo_t logo;
964 logo.id = sess->d_id;
966 qlt_send_first_logo(vha, &logo);
969 if (sess->logout_on_delete) {
972 rc = qla2x00_post_async_logout_work(vha, sess, NULL);
973 if (rc != QLA_SUCCESS)
974 ql_log(ql_log_warn, vha, 0xf085,
975 "Schedule logo failed sess %p rc %d\n",
978 logout_started = true;
983 * Release the target session for FC Nexus from fabric module code.
985 if (sess->se_sess != NULL)
986 ha->tgt.tgt_ops->free_session(sess);
988 if (logout_started) {
991 while (!ACCESS_ONCE(sess->logout_completed)) {
993 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf086,
994 "%s: waiting for sess %p logout\n",
1001 ql_dbg(ql_dbg_disc, vha, 0xf087,
1002 "%s: sess %p logout completed\n",__func__, sess);
1005 if (sess->logo_ack_needed) {
1006 sess->logo_ack_needed = 0;
1007 qla24xx_async_notify_ack(vha, sess,
1008 (struct imm_ntfy_from_isp *)sess->iocb, SRB_NACK_LOGO);
1011 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
1012 if (sess->se_sess) {
1013 sess->se_sess = NULL;
1014 if (tgt && !IS_SW_RESV_ADDR(sess->d_id))
1018 sess->disc_state = DSC_DELETED;
1019 sess->fw_login_state = DSC_LS_PORT_UNAVAIL;
1020 sess->deleted = QLA_SESS_DELETED;
1021 sess->login_retry = vha->hw->login_retry_count;
1023 if (sess->login_succ && !IS_SW_RESV_ADDR(sess->d_id)) {
1024 vha->fcport_count--;
1025 sess->login_succ = 0;
1028 if (sess->chip_reset != sess->vha->hw->chip_reset)
1029 qla2x00_clear_loop_id(sess);
1031 if (sess->conflict) {
1032 sess->conflict->login_pause = 0;
1033 sess->conflict = NULL;
1034 if (!test_bit(UNLOADING, &vha->dpc_flags))
1035 set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
1039 struct qlt_plogi_ack_t *own =
1040 sess->plogi_link[QLT_PLOGI_LINK_SAME_WWN];
1041 struct qlt_plogi_ack_t *con =
1042 sess->plogi_link[QLT_PLOGI_LINK_CONFLICT];
1043 struct imm_ntfy_from_isp *iocb;
1047 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf099,
1048 "se_sess %p / sess %p port %8phC is gone,"
1049 " %s (ref=%d), releasing PLOGI for %8phC (ref=%d)\n",
1050 sess->se_sess, sess, sess->port_name,
1051 own ? "releasing own PLOGI" : "no own PLOGI pending",
1052 own ? own->ref_count : -1,
1053 iocb->u.isp24.port_name, con->ref_count);
1054 qlt_plogi_ack_unref(vha, con);
1055 sess->plogi_link[QLT_PLOGI_LINK_CONFLICT] = NULL;
1057 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf09a,
1058 "se_sess %p / sess %p port %8phC is gone, %s (ref=%d)\n",
1059 sess->se_sess, sess, sess->port_name,
1060 own ? "releasing own PLOGI" :
1061 "no own PLOGI pending",
1062 own ? own->ref_count : -1);
1066 sess->fw_login_state = DSC_LS_PLOGI_PEND;
1067 qlt_plogi_ack_unref(vha, own);
1068 sess->plogi_link[QLT_PLOGI_LINK_SAME_WWN] = NULL;
1071 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
1073 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf001,
1074 "Unregistration of sess %p %8phC finished fcp_cnt %d\n",
1075 sess, sess->port_name, vha->fcport_count);
1077 if (tgt && (tgt->sess_count == 0))
1078 wake_up_all(&tgt->waitQ);
1080 if (vha->fcport_count == 0)
1081 wake_up_all(&vha->fcport_waitQ);
1083 base_vha = pci_get_drvdata(ha->pdev);
1084 if (test_bit(PFLG_DRIVER_REMOVING, &base_vha->pci_flags))
1087 if (!tgt || !tgt->tgt_stop) {
1088 memset(&ea, 0, sizeof(ea));
1089 ea.event = FCME_DELETE_DONE;
1091 qla2x00_fcport_event_handler(vha, &ea);
1095 /* ha->tgt.sess_lock supposed to be held on entry */
1096 void qlt_unreg_sess(struct fc_port *sess)
1098 struct scsi_qla_host *vha = sess->vha;
1100 ql_dbg(ql_dbg_disc, sess->vha, 0x210a,
1101 "%s sess %p for deletion %8phC\n",
1102 __func__, sess, sess->port_name);
1105 vha->hw->tgt.tgt_ops->clear_nacl_from_fcport_map(sess);
1107 qla2x00_mark_device_lost(vha, sess, 1, 1);
1109 sess->deleted = QLA_SESS_DELETION_IN_PROGRESS;
1110 sess->disc_state = DSC_DELETE_PEND;
1111 sess->last_rscn_gen = sess->rscn_gen;
1112 sess->last_login_gen = sess->login_gen;
1114 INIT_WORK(&sess->free_work, qlt_free_session_done);
1115 schedule_work(&sess->free_work);
1117 EXPORT_SYMBOL(qlt_unreg_sess);
1119 static int qlt_reset(struct scsi_qla_host *vha, void *iocb, int mcmd)
1121 struct qla_hw_data *ha = vha->hw;
1122 struct fc_port *sess = NULL;
1125 struct imm_ntfy_from_isp *n = (struct imm_ntfy_from_isp *)iocb;
1126 unsigned long flags;
1128 loop_id = le16_to_cpu(n->u.isp24.nport_handle);
1129 if (loop_id == 0xFFFF) {
1131 atomic_inc(&vha->vha_tgt.qla_tgt->tgt_global_resets_count);
1132 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
1133 qlt_clear_tgt_db(vha->vha_tgt.qla_tgt);
1134 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
1136 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
1137 sess = ha->tgt.tgt_ops->find_sess_by_loop_id(vha, loop_id);
1138 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
1141 ql_dbg(ql_dbg_tgt, vha, 0xe000,
1142 "Using sess for qla_tgt_reset: %p\n", sess);
1148 ql_dbg(ql_dbg_tgt, vha, 0xe047,
1149 "scsi(%ld): resetting (session %p from port %8phC mcmd %x, "
1150 "loop_id %d)\n", vha->host_no, sess, sess->port_name,
1153 return qlt_issue_task_mgmt(sess, 0, mcmd, iocb, QLA24XX_MGMT_SEND_NACK);
1156 static void qla24xx_chk_fcp_state(struct fc_port *sess)
1158 if (sess->chip_reset != sess->vha->hw->chip_reset) {
1159 sess->logout_on_delete = 0;
1160 sess->logo_ack_needed = 0;
1161 sess->fw_login_state = DSC_LS_PORT_UNAVAIL;
1162 sess->scan_state = 0;
1166 /* ha->tgt.sess_lock supposed to be held on entry */
1167 void qlt_schedule_sess_for_deletion(struct fc_port *sess,
1170 struct qla_tgt *tgt = sess->tgt;
1172 if (sess->disc_state == DSC_DELETE_PEND)
1175 if (sess->disc_state == DSC_DELETED) {
1176 if (tgt && tgt->tgt_stop && (tgt->sess_count == 0))
1177 wake_up_all(&tgt->waitQ);
1178 if (sess->vha->fcport_count == 0)
1179 wake_up_all(&sess->vha->fcport_waitQ);
1181 if (!sess->plogi_link[QLT_PLOGI_LINK_SAME_WWN] &&
1182 !sess->plogi_link[QLT_PLOGI_LINK_CONFLICT])
1186 sess->disc_state = DSC_DELETE_PEND;
1188 if (sess->deleted == QLA_SESS_DELETED)
1189 sess->logout_on_delete = 0;
1191 sess->deleted = QLA_SESS_DELETION_IN_PROGRESS;
1192 qla24xx_chk_fcp_state(sess);
1194 ql_dbg(ql_dbg_tgt, sess->vha, 0xe001,
1195 "Scheduling sess %p for deletion\n", sess);
1197 schedule_work(&sess->del_work);
1200 void qlt_schedule_sess_for_deletion_lock(struct fc_port *sess)
1202 unsigned long flags;
1203 struct qla_hw_data *ha = sess->vha->hw;
1204 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
1205 qlt_schedule_sess_for_deletion(sess, 1);
1206 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
1209 /* ha->tgt.sess_lock supposed to be held on entry */
1210 static void qlt_clear_tgt_db(struct qla_tgt *tgt)
1212 struct fc_port *sess;
1213 scsi_qla_host_t *vha = tgt->vha;
1215 list_for_each_entry(sess, &vha->vp_fcports, list) {
1217 qlt_schedule_sess_for_deletion(sess, 1);
1220 /* At this point tgt could be already dead */
1223 static int qla24xx_get_loop_id(struct scsi_qla_host *vha, const uint8_t *s_id,
1226 struct qla_hw_data *ha = vha->hw;
1227 dma_addr_t gid_list_dma;
1228 struct gid_list_info *gid_list;
1233 gid_list = dma_alloc_coherent(&ha->pdev->dev, qla2x00_gid_list_size(ha),
1234 &gid_list_dma, GFP_KERNEL);
1236 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf044,
1237 "qla_target(%d): DMA Alloc failed of %u\n",
1238 vha->vp_idx, qla2x00_gid_list_size(ha));
1242 /* Get list of logged in devices */
1243 rc = qla24xx_gidlist_wait(vha, gid_list, gid_list_dma, &entries);
1244 if (rc != QLA_SUCCESS) {
1245 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf045,
1246 "qla_target(%d): get_id_list() failed: %x\n",
1249 goto out_free_id_list;
1252 id_iter = (char *)gid_list;
1254 for (i = 0; i < entries; i++) {
1255 struct gid_list_info *gid = (struct gid_list_info *)id_iter;
1256 if ((gid->al_pa == s_id[2]) &&
1257 (gid->area == s_id[1]) &&
1258 (gid->domain == s_id[0])) {
1259 *loop_id = le16_to_cpu(gid->loop_id);
1263 id_iter += ha->gid_list_info_size;
1267 dma_free_coherent(&ha->pdev->dev, qla2x00_gid_list_size(ha),
1268 gid_list, gid_list_dma);
1273 * Adds an extra ref to allow to drop hw lock after adding sess to the list.
1274 * Caller must put it.
1276 static struct fc_port *qlt_create_sess(
1277 struct scsi_qla_host *vha,
1281 struct qla_hw_data *ha = vha->hw;
1282 struct fc_port *sess = fcport;
1283 unsigned long flags;
1285 if (vha->vha_tgt.qla_tgt->tgt_stop)
1288 if (fcport->se_sess) {
1289 if (!kref_get_unless_zero(&sess->sess_kref)) {
1290 ql_dbg(ql_dbg_disc, vha, 0x20f6,
1291 "%s: kref_get_unless_zero failed for %8phC\n",
1292 __func__, sess->port_name);
1297 sess->tgt = vha->vha_tgt.qla_tgt;
1298 sess->local = local;
1301 * Under normal circumstances we want to logout from firmware when
1302 * session eventually ends and release corresponding nport handle.
1303 * In the exception cases (e.g. when new PLOGI is waiting) corresponding
1304 * code will adjust these flags as necessary.
1306 sess->logout_on_delete = 1;
1307 sess->keep_nport_handle = 0;
1308 sess->logout_completed = 0;
1310 if (ha->tgt.tgt_ops->check_initiator_node_acl(vha,
1311 &fcport->port_name[0], sess) < 0) {
1312 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf015,
1313 "(%d) %8phC check_initiator_node_acl failed\n",
1314 vha->vp_idx, fcport->port_name);
1317 kref_init(&fcport->sess_kref);
1319 * Take an extra reference to ->sess_kref here to handle
1320 * fc_port access across ->tgt.sess_lock reaquire.
1322 if (!kref_get_unless_zero(&sess->sess_kref)) {
1323 ql_dbg(ql_dbg_disc, vha, 0x20f7,
1324 "%s: kref_get_unless_zero failed for %8phC\n",
1325 __func__, sess->port_name);
1329 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
1330 if (!IS_SW_RESV_ADDR(sess->d_id))
1331 vha->vha_tgt.qla_tgt->sess_count++;
1333 qlt_do_generation_tick(vha, &sess->generation);
1334 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
1337 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf006,
1338 "Adding sess %p se_sess %p to tgt %p sess_count %d\n",
1339 sess, sess->se_sess, vha->vha_tgt.qla_tgt,
1340 vha->vha_tgt.qla_tgt->sess_count);
1342 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04b,
1343 "qla_target(%d): %ssession for wwn %8phC (loop_id %d, "
1344 "s_id %x:%x:%x, confirmed completion %ssupported) added\n",
1345 vha->vp_idx, local ? "local " : "", fcport->port_name,
1346 fcport->loop_id, sess->d_id.b.domain, sess->d_id.b.area,
1347 sess->d_id.b.al_pa, sess->conf_compl_supported ? "" : "not ");
1353 * max_gen - specifies maximum session generation
1354 * at which this deletion requestion is still valid
1357 qlt_fc_port_deleted(struct scsi_qla_host *vha, fc_port_t *fcport, int max_gen)
1359 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
1360 struct fc_port *sess = fcport;
1361 unsigned long flags;
1363 if (!vha->hw->tgt.tgt_ops)
1369 spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
1370 if (tgt->tgt_stop) {
1371 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
1374 if (!sess->se_sess) {
1375 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
1379 if (max_gen - sess->generation < 0) {
1380 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
1381 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf092,
1382 "Ignoring stale deletion request for se_sess %p / sess %p"
1383 " for port %8phC, req_gen %d, sess_gen %d\n",
1384 sess->se_sess, sess, sess->port_name, max_gen,
1389 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf008, "qla_tgt_fc_port_deleted %p", sess);
1392 qlt_schedule_sess_for_deletion(sess, false);
1393 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
1396 static inline int test_tgt_sess_count(struct qla_tgt *tgt)
1398 struct qla_hw_data *ha = tgt->ha;
1399 unsigned long flags;
1402 * We need to protect against race, when tgt is freed before or
1405 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
1406 ql_dbg(ql_dbg_tgt, tgt->vha, 0xe002,
1407 "tgt %p, sess_count=%d\n",
1408 tgt, tgt->sess_count);
1409 res = (tgt->sess_count == 0);
1410 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
1415 /* Called by tcm_qla2xxx configfs code */
1416 int qlt_stop_phase1(struct qla_tgt *tgt)
1418 struct scsi_qla_host *vha = tgt->vha;
1419 struct qla_hw_data *ha = tgt->ha;
1420 unsigned long flags;
1422 mutex_lock(&qla_tgt_mutex);
1423 if (!vha->fc_vport) {
1424 struct Scsi_Host *sh = vha->host;
1425 struct fc_host_attrs *fc_host = shost_to_fc_host(sh);
1428 spin_lock_irqsave(sh->host_lock, flags);
1429 npiv_vports = (fc_host->npiv_vports_inuse);
1430 spin_unlock_irqrestore(sh->host_lock, flags);
1433 mutex_unlock(&qla_tgt_mutex);
1434 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf021,
1435 "NPIV is in use. Can not stop target\n");
1439 if (tgt->tgt_stop || tgt->tgt_stopped) {
1440 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04e,
1441 "Already in tgt->tgt_stop or tgt_stopped state\n");
1442 mutex_unlock(&qla_tgt_mutex);
1446 ql_dbg(ql_dbg_tgt_mgt, vha, 0xe003, "Stopping target for host %ld(%p)\n",
1449 * Mutex needed to sync with qla_tgt_fc_port_[added,deleted].
1450 * Lock is needed, because we still can get an incoming packet.
1452 mutex_lock(&vha->vha_tgt.tgt_mutex);
1453 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
1455 qlt_clear_tgt_db(tgt);
1456 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
1457 mutex_unlock(&vha->vha_tgt.tgt_mutex);
1458 mutex_unlock(&qla_tgt_mutex);
1460 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf009,
1461 "Waiting for sess works (tgt %p)", tgt);
1462 spin_lock_irqsave(&tgt->sess_work_lock, flags);
1463 while (!list_empty(&tgt->sess_works_list)) {
1464 spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
1465 flush_scheduled_work();
1466 spin_lock_irqsave(&tgt->sess_work_lock, flags);
1468 spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
1470 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00a,
1471 "Waiting for tgt %p: sess_count=%d\n", tgt, tgt->sess_count);
1473 wait_event(tgt->waitQ, test_tgt_sess_count(tgt));
1476 if (!ha->flags.host_shutting_down &&
1477 (qla_tgt_mode_enabled(vha) || qla_dual_mode_enabled(vha)))
1478 qlt_disable_vha(vha);
1480 /* Wait for sessions to clear out (just in case) */
1481 wait_event(tgt->waitQ, test_tgt_sess_count(tgt));
1484 EXPORT_SYMBOL(qlt_stop_phase1);
1486 /* Called by tcm_qla2xxx configfs code */
1487 void qlt_stop_phase2(struct qla_tgt *tgt)
1489 scsi_qla_host_t *vha = tgt->vha;
1491 if (tgt->tgt_stopped) {
1492 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04f,
1493 "Already in tgt->tgt_stopped state\n");
1497 if (!tgt->tgt_stop) {
1498 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00b,
1499 "%s: phase1 stop is not completed\n", __func__);
1504 mutex_lock(&vha->vha_tgt.tgt_mutex);
1506 tgt->tgt_stopped = 1;
1507 mutex_unlock(&vha->vha_tgt.tgt_mutex);
1509 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00c, "Stop of tgt %p finished\n",
1512 EXPORT_SYMBOL(qlt_stop_phase2);
1514 /* Called from qlt_remove_target() -> qla2x00_remove_one() */
1515 static void qlt_release(struct qla_tgt *tgt)
1517 scsi_qla_host_t *vha = tgt->vha;
1519 if ((vha->vha_tgt.qla_tgt != NULL) && !tgt->tgt_stop &&
1521 qlt_stop_phase1(tgt);
1523 if ((vha->vha_tgt.qla_tgt != NULL) && !tgt->tgt_stopped)
1524 qlt_stop_phase2(tgt);
1526 vha->vha_tgt.qla_tgt = NULL;
1528 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00d,
1529 "Release of tgt %p finished\n", tgt);
1534 /* ha->hardware_lock supposed to be held on entry */
1535 static int qlt_sched_sess_work(struct qla_tgt *tgt, int type,
1536 const void *param, unsigned int param_size)
1538 struct qla_tgt_sess_work_param *prm;
1539 unsigned long flags;
1541 prm = kzalloc(sizeof(*prm), GFP_ATOMIC);
1543 ql_dbg(ql_dbg_tgt_mgt, tgt->vha, 0xf050,
1544 "qla_target(%d): Unable to create session "
1545 "work, command will be refused", 0);
1549 ql_dbg(ql_dbg_tgt_mgt, tgt->vha, 0xf00e,
1550 "Scheduling work (type %d, prm %p)"
1551 " to find session for param %p (size %d, tgt %p)\n",
1552 type, prm, param, param_size, tgt);
1555 memcpy(&prm->tm_iocb, param, param_size);
1557 spin_lock_irqsave(&tgt->sess_work_lock, flags);
1558 list_add_tail(&prm->sess_works_list_entry, &tgt->sess_works_list);
1559 spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
1561 schedule_work(&tgt->sess_work);
1567 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
1569 static void qlt_send_notify_ack(struct qla_qpair *qpair,
1570 struct imm_ntfy_from_isp *ntfy,
1571 uint32_t add_flags, uint16_t resp_code, int resp_code_valid,
1572 uint16_t srr_flags, uint16_t srr_reject_code, uint8_t srr_explan)
1574 struct scsi_qla_host *vha = qpair->vha;
1575 struct qla_hw_data *ha = vha->hw;
1577 struct nack_to_isp *nack;
1579 if (!ha->flags.fw_started)
1582 ql_dbg(ql_dbg_tgt, vha, 0xe004, "Sending NOTIFY_ACK (ha=%p)\n", ha);
1584 pkt = (request_t *)__qla2x00_alloc_iocbs(qpair, NULL);
1586 ql_dbg(ql_dbg_tgt, vha, 0xe049,
1587 "qla_target(%d): %s failed: unable to allocate "
1588 "request packet\n", vha->vp_idx, __func__);
1592 if (vha->vha_tgt.qla_tgt != NULL)
1593 vha->vha_tgt.qla_tgt->notify_ack_expected++;
1595 pkt->entry_type = NOTIFY_ACK_TYPE;
1596 pkt->entry_count = 1;
1598 nack = (struct nack_to_isp *)pkt;
1599 nack->ox_id = ntfy->ox_id;
1601 nack->u.isp24.handle = QLA_TGT_SKIP_HANDLE;
1602 nack->u.isp24.nport_handle = ntfy->u.isp24.nport_handle;
1603 if (le16_to_cpu(ntfy->u.isp24.status) == IMM_NTFY_ELS) {
1604 nack->u.isp24.flags = ntfy->u.isp24.flags &
1605 cpu_to_le32(NOTIFY24XX_FLAGS_PUREX_IOCB);
1607 nack->u.isp24.srr_rx_id = ntfy->u.isp24.srr_rx_id;
1608 nack->u.isp24.status = ntfy->u.isp24.status;
1609 nack->u.isp24.status_subcode = ntfy->u.isp24.status_subcode;
1610 nack->u.isp24.fw_handle = ntfy->u.isp24.fw_handle;
1611 nack->u.isp24.exchange_address = ntfy->u.isp24.exchange_address;
1612 nack->u.isp24.srr_rel_offs = ntfy->u.isp24.srr_rel_offs;
1613 nack->u.isp24.srr_ui = ntfy->u.isp24.srr_ui;
1614 nack->u.isp24.srr_flags = cpu_to_le16(srr_flags);
1615 nack->u.isp24.srr_reject_code = srr_reject_code;
1616 nack->u.isp24.srr_reject_code_expl = srr_explan;
1617 nack->u.isp24.vp_index = ntfy->u.isp24.vp_index;
1619 ql_dbg(ql_dbg_tgt, vha, 0xe005,
1620 "qla_target(%d): Sending 24xx Notify Ack %d\n",
1621 vha->vp_idx, nack->u.isp24.status);
1623 /* Memory Barrier */
1625 qla2x00_start_iocbs(vha, qpair->req);
1629 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
1631 static void qlt_24xx_send_abts_resp(struct qla_qpair *qpair,
1632 struct abts_recv_from_24xx *abts, uint32_t status,
1635 struct scsi_qla_host *vha = qpair->vha;
1636 struct qla_hw_data *ha = vha->hw;
1637 struct abts_resp_to_24xx *resp;
1641 ql_dbg(ql_dbg_tgt, vha, 0xe006,
1642 "Sending task mgmt ABTS response (ha=%p, atio=%p, status=%x\n",
1645 resp = (struct abts_resp_to_24xx *)qla2x00_alloc_iocbs_ready(qpair,
1648 ql_dbg(ql_dbg_tgt, vha, 0xe04a,
1649 "qla_target(%d): %s failed: unable to allocate "
1650 "request packet", vha->vp_idx, __func__);
1654 resp->entry_type = ABTS_RESP_24XX;
1655 resp->entry_count = 1;
1656 resp->nport_handle = abts->nport_handle;
1657 resp->vp_index = vha->vp_idx;
1658 resp->sof_type = abts->sof_type;
1659 resp->exchange_address = abts->exchange_address;
1660 resp->fcp_hdr_le = abts->fcp_hdr_le;
1661 f_ctl = cpu_to_le32(F_CTL_EXCH_CONTEXT_RESP |
1662 F_CTL_LAST_SEQ | F_CTL_END_SEQ |
1663 F_CTL_SEQ_INITIATIVE);
1664 p = (uint8_t *)&f_ctl;
1665 resp->fcp_hdr_le.f_ctl[0] = *p++;
1666 resp->fcp_hdr_le.f_ctl[1] = *p++;
1667 resp->fcp_hdr_le.f_ctl[2] = *p;
1669 resp->fcp_hdr_le.d_id[0] = abts->fcp_hdr_le.d_id[0];
1670 resp->fcp_hdr_le.d_id[1] = abts->fcp_hdr_le.d_id[1];
1671 resp->fcp_hdr_le.d_id[2] = abts->fcp_hdr_le.d_id[2];
1672 resp->fcp_hdr_le.s_id[0] = abts->fcp_hdr_le.s_id[0];
1673 resp->fcp_hdr_le.s_id[1] = abts->fcp_hdr_le.s_id[1];
1674 resp->fcp_hdr_le.s_id[2] = abts->fcp_hdr_le.s_id[2];
1676 resp->fcp_hdr_le.d_id[0] = abts->fcp_hdr_le.s_id[0];
1677 resp->fcp_hdr_le.d_id[1] = abts->fcp_hdr_le.s_id[1];
1678 resp->fcp_hdr_le.d_id[2] = abts->fcp_hdr_le.s_id[2];
1679 resp->fcp_hdr_le.s_id[0] = abts->fcp_hdr_le.d_id[0];
1680 resp->fcp_hdr_le.s_id[1] = abts->fcp_hdr_le.d_id[1];
1681 resp->fcp_hdr_le.s_id[2] = abts->fcp_hdr_le.d_id[2];
1683 resp->exchange_addr_to_abort = abts->exchange_addr_to_abort;
1684 if (status == FCP_TMF_CMPL) {
1685 resp->fcp_hdr_le.r_ctl = R_CTL_BASIC_LINK_SERV | R_CTL_B_ACC;
1686 resp->payload.ba_acct.seq_id_valid = SEQ_ID_INVALID;
1687 resp->payload.ba_acct.low_seq_cnt = 0x0000;
1688 resp->payload.ba_acct.high_seq_cnt = 0xFFFF;
1689 resp->payload.ba_acct.ox_id = abts->fcp_hdr_le.ox_id;
1690 resp->payload.ba_acct.rx_id = abts->fcp_hdr_le.rx_id;
1692 resp->fcp_hdr_le.r_ctl = R_CTL_BASIC_LINK_SERV | R_CTL_B_RJT;
1693 resp->payload.ba_rjt.reason_code =
1694 BA_RJT_REASON_CODE_UNABLE_TO_PERFORM;
1695 /* Other bytes are zero */
1698 vha->vha_tgt.qla_tgt->abts_resp_expected++;
1700 /* Memory Barrier */
1702 qla2x00_start_iocbs(vha, qpair->req);
1706 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
1708 static void qlt_24xx_retry_term_exchange(struct scsi_qla_host *vha,
1709 struct abts_resp_from_24xx_fw *entry)
1711 struct ctio7_to_24xx *ctio;
1713 ql_dbg(ql_dbg_tgt, vha, 0xe007,
1714 "Sending retry TERM EXCH CTIO7 (ha=%p)\n", vha->hw);
1716 ctio = (struct ctio7_to_24xx *)qla2x00_alloc_iocbs_ready(
1717 vha->hw->base_qpair, NULL);
1719 ql_dbg(ql_dbg_tgt, vha, 0xe04b,
1720 "qla_target(%d): %s failed: unable to allocate "
1721 "request packet\n", vha->vp_idx, __func__);
1726 * We've got on entrance firmware's response on by us generated
1727 * ABTS response. So, in it ID fields are reversed.
1730 ctio->entry_type = CTIO_TYPE7;
1731 ctio->entry_count = 1;
1732 ctio->nport_handle = entry->nport_handle;
1733 ctio->handle = QLA_TGT_SKIP_HANDLE | CTIO_COMPLETION_HANDLE_MARK;
1734 ctio->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
1735 ctio->vp_index = vha->vp_idx;
1736 ctio->initiator_id[0] = entry->fcp_hdr_le.d_id[0];
1737 ctio->initiator_id[1] = entry->fcp_hdr_le.d_id[1];
1738 ctio->initiator_id[2] = entry->fcp_hdr_le.d_id[2];
1739 ctio->exchange_addr = entry->exchange_addr_to_abort;
1740 ctio->u.status1.flags = cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_1 |
1741 CTIO7_FLAGS_TERMINATE);
1742 ctio->u.status1.ox_id = cpu_to_le16(entry->fcp_hdr_le.ox_id);
1744 /* Memory Barrier */
1746 qla2x00_start_iocbs(vha, vha->req);
1748 qlt_24xx_send_abts_resp(vha->hw->base_qpair,
1749 (struct abts_recv_from_24xx *)entry,
1750 FCP_TMF_CMPL, true);
1753 static int abort_cmd_for_tag(struct scsi_qla_host *vha, uint32_t tag)
1755 struct qla_tgt_sess_op *op;
1756 struct qla_tgt_cmd *cmd;
1757 unsigned long flags;
1759 spin_lock_irqsave(&vha->cmd_list_lock, flags);
1760 list_for_each_entry(op, &vha->qla_sess_op_cmd_list, cmd_list) {
1761 if (tag == op->atio.u.isp24.exchange_addr) {
1763 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
1768 list_for_each_entry(op, &vha->unknown_atio_list, cmd_list) {
1769 if (tag == op->atio.u.isp24.exchange_addr) {
1771 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
1776 list_for_each_entry(cmd, &vha->qla_cmd_list, cmd_list) {
1777 if (tag == cmd->atio.u.isp24.exchange_addr) {
1779 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
1783 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
1788 /* drop cmds for the given lun
1789 * XXX only looks for cmds on the port through which lun reset was recieved
1790 * XXX does not go through the list of other port (which may have cmds
1793 static void abort_cmds_for_lun(struct scsi_qla_host *vha,
1794 u64 lun, uint8_t *s_id)
1796 struct qla_tgt_sess_op *op;
1797 struct qla_tgt_cmd *cmd;
1799 unsigned long flags;
1801 key = sid_to_key(s_id);
1802 spin_lock_irqsave(&vha->cmd_list_lock, flags);
1803 list_for_each_entry(op, &vha->qla_sess_op_cmd_list, cmd_list) {
1807 op_key = sid_to_key(op->atio.u.isp24.fcp_hdr.s_id);
1808 op_lun = scsilun_to_int(
1809 (struct scsi_lun *)&op->atio.u.isp24.fcp_cmnd.lun);
1810 if (op_key == key && op_lun == lun)
1814 list_for_each_entry(op, &vha->unknown_atio_list, cmd_list) {
1818 op_key = sid_to_key(op->atio.u.isp24.fcp_hdr.s_id);
1819 op_lun = scsilun_to_int(
1820 (struct scsi_lun *)&op->atio.u.isp24.fcp_cmnd.lun);
1821 if (op_key == key && op_lun == lun)
1825 list_for_each_entry(cmd, &vha->qla_cmd_list, cmd_list) {
1829 cmd_key = sid_to_key(cmd->atio.u.isp24.fcp_hdr.s_id);
1830 cmd_lun = scsilun_to_int(
1831 (struct scsi_lun *)&cmd->atio.u.isp24.fcp_cmnd.lun);
1832 if (cmd_key == key && cmd_lun == lun)
1835 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
1838 /* ha->hardware_lock supposed to be held on entry */
1839 static int __qlt_24xx_handle_abts(struct scsi_qla_host *vha,
1840 struct abts_recv_from_24xx *abts, struct fc_port *sess)
1842 struct qla_hw_data *ha = vha->hw;
1843 struct se_session *se_sess = sess->se_sess;
1844 struct qla_tgt_mgmt_cmd *mcmd;
1845 struct qla_tgt_cmd *cmd;
1846 struct se_cmd *se_cmd;
1848 bool found_lun = false;
1849 unsigned long flags;
1851 spin_lock_irqsave(&se_sess->sess_cmd_lock, flags);
1852 list_for_each_entry(se_cmd, &se_sess->sess_cmd_list, se_cmd_list) {
1853 if (se_cmd->tag == abts->exchange_addr_to_abort) {
1858 spin_unlock_irqrestore(&se_sess->sess_cmd_lock, flags);
1860 /* cmd not in LIO lists, look in qla list */
1862 if (abort_cmd_for_tag(vha, abts->exchange_addr_to_abort)) {
1863 /* send TASK_ABORT response immediately */
1864 qlt_24xx_send_abts_resp(ha->base_qpair, abts,
1865 FCP_TMF_CMPL, false);
1868 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf081,
1869 "unable to find cmd in driver or LIO for tag 0x%x\n",
1870 abts->exchange_addr_to_abort);
1875 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00f,
1876 "qla_target(%d): task abort (tag=%d)\n",
1877 vha->vp_idx, abts->exchange_addr_to_abort);
1879 mcmd = mempool_alloc(qla_tgt_mgmt_cmd_mempool, GFP_ATOMIC);
1881 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf051,
1882 "qla_target(%d): %s: Allocation of ABORT cmd failed",
1883 vha->vp_idx, __func__);
1886 memset(mcmd, 0, sizeof(*mcmd));
1888 cmd = container_of(se_cmd, struct qla_tgt_cmd, se_cmd);
1890 memcpy(&mcmd->orig_iocb.abts, abts, sizeof(mcmd->orig_iocb.abts));
1891 mcmd->reset_count = vha->hw->chip_reset;
1892 mcmd->tmr_func = QLA_TGT_ABTS;
1893 mcmd->qpair = ha->base_qpair;
1895 rc = ha->tgt.tgt_ops->handle_tmr(mcmd, cmd->unpacked_lun, mcmd->tmr_func,
1896 abts->exchange_addr_to_abort);
1898 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf052,
1899 "qla_target(%d): tgt_ops->handle_tmr()"
1900 " failed: %d", vha->vp_idx, rc);
1901 mempool_free(mcmd, qla_tgt_mgmt_cmd_mempool);
1909 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
1911 static void qlt_24xx_handle_abts(struct scsi_qla_host *vha,
1912 struct abts_recv_from_24xx *abts)
1914 struct qla_hw_data *ha = vha->hw;
1915 struct fc_port *sess;
1916 uint32_t tag = abts->exchange_addr_to_abort;
1919 unsigned long flags;
1921 if (le32_to_cpu(abts->fcp_hdr_le.parameter) & ABTS_PARAM_ABORT_SEQ) {
1922 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf053,
1923 "qla_target(%d): ABTS: Abort Sequence not "
1924 "supported\n", vha->vp_idx);
1925 qlt_24xx_send_abts_resp(ha->base_qpair, abts, FCP_TMF_REJECTED,
1930 if (tag == ATIO_EXCHANGE_ADDRESS_UNKNOWN) {
1931 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf010,
1932 "qla_target(%d): ABTS: Unknown Exchange "
1933 "Address received\n", vha->vp_idx);
1934 qlt_24xx_send_abts_resp(ha->base_qpair, abts, FCP_TMF_REJECTED,
1939 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf011,
1940 "qla_target(%d): task abort (s_id=%x:%x:%x, "
1941 "tag=%d, param=%x)\n", vha->vp_idx, abts->fcp_hdr_le.s_id[2],
1942 abts->fcp_hdr_le.s_id[1], abts->fcp_hdr_le.s_id[0], tag,
1943 le32_to_cpu(abts->fcp_hdr_le.parameter));
1945 s_id[0] = abts->fcp_hdr_le.s_id[2];
1946 s_id[1] = abts->fcp_hdr_le.s_id[1];
1947 s_id[2] = abts->fcp_hdr_le.s_id[0];
1949 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
1950 sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha, s_id);
1952 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf012,
1953 "qla_target(%d): task abort for non-existant session\n",
1955 rc = qlt_sched_sess_work(vha->vha_tgt.qla_tgt,
1956 QLA_TGT_SESS_WORK_ABORT, abts, sizeof(*abts));
1958 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
1961 qlt_24xx_send_abts_resp(ha->base_qpair, abts,
1962 FCP_TMF_REJECTED, false);
1966 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
1969 if (sess->deleted) {
1970 qlt_24xx_send_abts_resp(ha->base_qpair, abts, FCP_TMF_REJECTED,
1975 rc = __qlt_24xx_handle_abts(vha, abts, sess);
1977 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf054,
1978 "qla_target(%d): __qlt_24xx_handle_abts() failed: %d\n",
1980 qlt_24xx_send_abts_resp(ha->base_qpair, abts, FCP_TMF_REJECTED,
1987 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
1989 static void qlt_24xx_send_task_mgmt_ctio(struct qla_qpair *qpair,
1990 struct qla_tgt_mgmt_cmd *mcmd, uint32_t resp_code)
1992 struct scsi_qla_host *ha = qpair->vha;
1993 struct atio_from_isp *atio = &mcmd->orig_iocb.atio;
1994 struct ctio7_to_24xx *ctio;
1997 ql_dbg(ql_dbg_tgt, ha, 0xe008,
1998 "Sending task mgmt CTIO7 (ha=%p, atio=%p, resp_code=%x\n",
1999 ha, atio, resp_code);
2002 ctio = (struct ctio7_to_24xx *)__qla2x00_alloc_iocbs(qpair, NULL);
2004 ql_dbg(ql_dbg_tgt, ha, 0xe04c,
2005 "qla_target(%d): %s failed: unable to allocate "
2006 "request packet\n", ha->vp_idx, __func__);
2010 ctio->entry_type = CTIO_TYPE7;
2011 ctio->entry_count = 1;
2012 ctio->handle = QLA_TGT_SKIP_HANDLE | CTIO_COMPLETION_HANDLE_MARK;
2013 ctio->nport_handle = mcmd->sess->loop_id;
2014 ctio->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
2015 ctio->vp_index = ha->vp_idx;
2016 ctio->initiator_id[0] = atio->u.isp24.fcp_hdr.s_id[2];
2017 ctio->initiator_id[1] = atio->u.isp24.fcp_hdr.s_id[1];
2018 ctio->initiator_id[2] = atio->u.isp24.fcp_hdr.s_id[0];
2019 ctio->exchange_addr = atio->u.isp24.exchange_addr;
2020 temp = (atio->u.isp24.attr << 9)|
2021 CTIO7_FLAGS_STATUS_MODE_1 | CTIO7_FLAGS_SEND_STATUS;
2022 ctio->u.status1.flags = cpu_to_le16(temp);
2023 temp = be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id);
2024 ctio->u.status1.ox_id = cpu_to_le16(temp);
2025 ctio->u.status1.scsi_status =
2026 cpu_to_le16(SS_RESPONSE_INFO_LEN_VALID);
2027 ctio->u.status1.response_len = cpu_to_le16(8);
2028 ctio->u.status1.sense_data[0] = resp_code;
2030 /* Memory Barrier */
2032 qla2x00_start_iocbs(ha, qpair->req);
2035 void qlt_free_mcmd(struct qla_tgt_mgmt_cmd *mcmd)
2037 mempool_free(mcmd, qla_tgt_mgmt_cmd_mempool);
2039 EXPORT_SYMBOL(qlt_free_mcmd);
2042 * ha->hardware_lock supposed to be held on entry. Might drop it, then
2045 void qlt_send_resp_ctio(scsi_qla_host_t *vha, struct qla_tgt_cmd *cmd,
2046 uint8_t scsi_status, uint8_t sense_key, uint8_t asc, uint8_t ascq)
2048 struct atio_from_isp *atio = &cmd->atio;
2049 struct ctio7_to_24xx *ctio;
2052 ql_dbg(ql_dbg_tgt_dif, vha, 0x3066,
2053 "Sending response CTIO7 (vha=%p, atio=%p, scsi_status=%02x, "
2054 "sense_key=%02x, asc=%02x, ascq=%02x",
2055 vha, atio, scsi_status, sense_key, asc, ascq);
2057 ctio = (struct ctio7_to_24xx *)qla2x00_alloc_iocbs(vha, NULL);
2059 ql_dbg(ql_dbg_async, vha, 0x3067,
2060 "qla2x00t(%ld): %s failed: unable to allocate request packet",
2061 vha->host_no, __func__);
2065 ctio->entry_type = CTIO_TYPE7;
2066 ctio->entry_count = 1;
2067 ctio->handle = QLA_TGT_SKIP_HANDLE;
2068 ctio->nport_handle = cmd->sess->loop_id;
2069 ctio->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
2070 ctio->vp_index = vha->vp_idx;
2071 ctio->initiator_id[0] = atio->u.isp24.fcp_hdr.s_id[2];
2072 ctio->initiator_id[1] = atio->u.isp24.fcp_hdr.s_id[1];
2073 ctio->initiator_id[2] = atio->u.isp24.fcp_hdr.s_id[0];
2074 ctio->exchange_addr = atio->u.isp24.exchange_addr;
2075 temp = (atio->u.isp24.attr << 9) |
2076 CTIO7_FLAGS_STATUS_MODE_1 | CTIO7_FLAGS_SEND_STATUS;
2077 ctio->u.status1.flags = cpu_to_le16(temp);
2078 temp = be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id);
2079 ctio->u.status1.ox_id = cpu_to_le16(temp);
2080 ctio->u.status1.scsi_status =
2081 cpu_to_le16(SS_RESPONSE_INFO_LEN_VALID | scsi_status);
2082 ctio->u.status1.response_len = cpu_to_le16(18);
2083 ctio->u.status1.residual = cpu_to_le32(get_datalen_for_atio(atio));
2085 if (ctio->u.status1.residual != 0)
2086 ctio->u.status1.scsi_status |=
2087 cpu_to_le16(SS_RESIDUAL_UNDER);
2089 /* Response code and sense key */
2090 put_unaligned_le32(((0x70 << 24) | (sense_key << 8)),
2091 (&ctio->u.status1.sense_data)[0]);
2092 /* Additional sense length */
2093 put_unaligned_le32(0x0a, (&ctio->u.status1.sense_data)[1]);
2095 put_unaligned_le32(((asc << 24) | (ascq << 16)),
2096 (&ctio->u.status1.sense_data)[3]);
2098 /* Memory Barrier */
2101 qla2x00_start_iocbs(vha, vha->req);
2106 /* callback from target fabric module code */
2107 void qlt_xmit_tm_rsp(struct qla_tgt_mgmt_cmd *mcmd)
2109 struct scsi_qla_host *vha = mcmd->sess->vha;
2110 struct qla_hw_data *ha = vha->hw;
2111 unsigned long flags;
2112 struct qla_qpair *qpair = mcmd->qpair;
2114 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf013,
2115 "TM response mcmd (%p) status %#x state %#x",
2116 mcmd, mcmd->fc_tm_rsp, mcmd->flags);
2118 spin_lock_irqsave(qpair->qp_lock_ptr, flags);
2120 if (!vha->flags.online || mcmd->reset_count != ha->chip_reset) {
2122 * Either the port is not online or this request was from
2123 * previous life, just abort the processing.
2125 ql_dbg(ql_dbg_async, vha, 0xe100,
2126 "RESET-TMR online/active/old-count/new-count = %d/%d/%d/%d.\n",
2127 vha->flags.online, qla2x00_reset_active(vha),
2128 mcmd->reset_count, ha->chip_reset);
2129 ha->tgt.tgt_ops->free_mcmd(mcmd);
2130 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
2134 if (mcmd->flags == QLA24XX_MGMT_SEND_NACK) {
2135 if (mcmd->orig_iocb.imm_ntfy.u.isp24.status_subcode ==
2137 mcmd->orig_iocb.imm_ntfy.u.isp24.status_subcode ==
2139 mcmd->orig_iocb.imm_ntfy.u.isp24.status_subcode ==
2141 ql_dbg(ql_dbg_disc, vha, 0x2106,
2142 "TM response logo %phC status %#x state %#x",
2143 mcmd->sess->port_name, mcmd->fc_tm_rsp,
2145 qlt_schedule_sess_for_deletion_lock(mcmd->sess);
2147 qlt_send_notify_ack(vha->hw->base_qpair,
2148 &mcmd->orig_iocb.imm_ntfy, 0, 0, 0, 0, 0, 0);
2151 if (mcmd->orig_iocb.atio.u.raw.entry_type == ABTS_RECV_24XX)
2152 qlt_24xx_send_abts_resp(qpair, &mcmd->orig_iocb.abts,
2153 mcmd->fc_tm_rsp, false);
2155 qlt_24xx_send_task_mgmt_ctio(qpair, mcmd,
2159 * Make the callback for ->free_mcmd() to queue_work() and invoke
2160 * target_put_sess_cmd() to drop cmd_kref to 1. The final
2161 * target_put_sess_cmd() call will be made from TFO->check_stop_free()
2162 * -> tcm_qla2xxx_check_stop_free() to release the TMR associated se_cmd
2163 * descriptor after TFO->queue_tm_rsp() -> tcm_qla2xxx_queue_tm_rsp() ->
2164 * qlt_xmit_tm_rsp() returns here..
2166 ha->tgt.tgt_ops->free_mcmd(mcmd);
2167 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
2169 EXPORT_SYMBOL(qlt_xmit_tm_rsp);
2172 static int qlt_pci_map_calc_cnt(struct qla_tgt_prm *prm)
2174 struct qla_tgt_cmd *cmd = prm->cmd;
2176 BUG_ON(cmd->sg_cnt == 0);
2178 prm->sg = (struct scatterlist *)cmd->sg;
2179 prm->seg_cnt = pci_map_sg(prm->tgt->ha->pdev, cmd->sg,
2180 cmd->sg_cnt, cmd->dma_data_direction);
2181 if (unlikely(prm->seg_cnt == 0))
2184 prm->cmd->sg_mapped = 1;
2186 if (cmd->se_cmd.prot_op == TARGET_PROT_NORMAL) {
2188 * If greater than four sg entries then we need to allocate
2189 * the continuation entries
2191 if (prm->seg_cnt > prm->tgt->datasegs_per_cmd)
2192 prm->req_cnt += DIV_ROUND_UP(prm->seg_cnt -
2193 prm->tgt->datasegs_per_cmd,
2194 prm->tgt->datasegs_per_cont);
2197 if ((cmd->se_cmd.prot_op == TARGET_PROT_DIN_INSERT) ||
2198 (cmd->se_cmd.prot_op == TARGET_PROT_DOUT_STRIP)) {
2199 prm->seg_cnt = DIV_ROUND_UP(cmd->bufflen, cmd->blk_sz);
2200 prm->tot_dsds = prm->seg_cnt;
2202 prm->tot_dsds = prm->seg_cnt;
2204 if (cmd->prot_sg_cnt) {
2205 prm->prot_sg = cmd->prot_sg;
2206 prm->prot_seg_cnt = pci_map_sg(prm->tgt->ha->pdev,
2207 cmd->prot_sg, cmd->prot_sg_cnt,
2208 cmd->dma_data_direction);
2209 if (unlikely(prm->prot_seg_cnt == 0))
2212 if ((cmd->se_cmd.prot_op == TARGET_PROT_DIN_INSERT) ||
2213 (cmd->se_cmd.prot_op == TARGET_PROT_DOUT_STRIP)) {
2214 /* Dif Bundling not support here */
2215 prm->prot_seg_cnt = DIV_ROUND_UP(cmd->bufflen,
2217 prm->tot_dsds += prm->prot_seg_cnt;
2219 prm->tot_dsds += prm->prot_seg_cnt;
2226 ql_dbg(ql_dbg_tgt, prm->cmd->vha, 0xe04d,
2227 "qla_target(%d): PCI mapping failed: sg_cnt=%d",
2228 0, prm->cmd->sg_cnt);
2232 static void qlt_unmap_sg(struct scsi_qla_host *vha, struct qla_tgt_cmd *cmd)
2234 struct qla_hw_data *ha = vha->hw;
2236 if (!cmd->sg_mapped)
2239 pci_unmap_sg(ha->pdev, cmd->sg, cmd->sg_cnt, cmd->dma_data_direction);
2242 if (cmd->prot_sg_cnt)
2243 pci_unmap_sg(ha->pdev, cmd->prot_sg, cmd->prot_sg_cnt,
2244 cmd->dma_data_direction);
2249 if (cmd->ctx_dsd_alloced)
2250 qla2x00_clean_dsd_pool(ha, cmd->ctx);
2252 dma_pool_free(ha->dl_dma_pool, cmd->ctx, cmd->ctx->crc_ctx_dma);
2255 static int qlt_check_reserve_free_req(struct qla_qpair *qpair,
2259 struct req_que *req = qpair->req;
2261 if (req->cnt < (req_cnt + 2)) {
2262 cnt = (uint16_t)RD_REG_DWORD(req->req_q_out);
2264 if (req->ring_index < cnt)
2265 req->cnt = cnt - req->ring_index;
2267 req->cnt = req->length - (req->ring_index - cnt);
2269 if (unlikely(req->cnt < (req_cnt + 2)))
2273 req->cnt -= req_cnt;
2279 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
2281 static inline void *qlt_get_req_pkt(struct req_que *req)
2283 /* Adjust ring index. */
2285 if (req->ring_index == req->length) {
2286 req->ring_index = 0;
2287 req->ring_ptr = req->ring;
2291 return (cont_entry_t *)req->ring_ptr;
2294 /* ha->hardware_lock supposed to be held on entry */
2295 static inline uint32_t qlt_make_handle(struct qla_qpair *qpair)
2297 struct scsi_qla_host *vha = qpair->vha;
2301 struct req_que *req = qpair->req;
2303 h = req->current_outstanding_cmd;
2305 for (index = 1; index < req->num_outstanding_cmds; index++) {
2307 if (h == req->num_outstanding_cmds)
2310 if (h == QLA_TGT_SKIP_HANDLE)
2313 if (!req->outstanding_cmds[h]) {
2320 req->current_outstanding_cmd = h;
2322 ql_dbg(ql_dbg_io, vha, 0x305b,
2323 "qla_target(%d): Ran out of empty cmd slots\n",
2325 h = QLA_TGT_NULL_HANDLE;
2331 /* ha->hardware_lock supposed to be held on entry */
2332 static int qlt_24xx_build_ctio_pkt(struct qla_qpair *qpair,
2333 struct qla_tgt_prm *prm)
2336 struct ctio7_to_24xx *pkt;
2337 struct atio_from_isp *atio = &prm->cmd->atio;
2339 struct scsi_qla_host *vha = prm->cmd->vha;
2341 pkt = (struct ctio7_to_24xx *)qpair->req->ring_ptr;
2343 memset(pkt, 0, sizeof(*pkt));
2345 pkt->entry_type = CTIO_TYPE7;
2346 pkt->entry_count = (uint8_t)prm->req_cnt;
2347 pkt->vp_index = vha->vp_idx;
2349 h = qlt_make_handle(qpair);
2350 if (unlikely(h == QLA_TGT_NULL_HANDLE)) {
2352 * CTIO type 7 from the firmware doesn't provide a way to
2353 * know the initiator's LOOP ID, hence we can't find
2354 * the session and, so, the command.
2358 vha->req->outstanding_cmds[h] = (srb_t *)prm->cmd;
2361 pkt->handle = MAKE_HANDLE(qpair->req->id, h);
2362 pkt->handle |= CTIO_COMPLETION_HANDLE_MARK;
2363 pkt->nport_handle = cpu_to_le16(prm->cmd->loop_id);
2364 pkt->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
2365 pkt->initiator_id[0] = atio->u.isp24.fcp_hdr.s_id[2];
2366 pkt->initiator_id[1] = atio->u.isp24.fcp_hdr.s_id[1];
2367 pkt->initiator_id[2] = atio->u.isp24.fcp_hdr.s_id[0];
2368 pkt->exchange_addr = atio->u.isp24.exchange_addr;
2369 temp = atio->u.isp24.attr << 9;
2370 pkt->u.status0.flags |= cpu_to_le16(temp);
2371 temp = be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id);
2372 pkt->u.status0.ox_id = cpu_to_le16(temp);
2373 pkt->u.status0.relative_offset = cpu_to_le32(prm->cmd->offset);
2379 * ha->hardware_lock supposed to be held on entry. We have already made sure
2380 * that there is sufficient amount of request entries to not drop it.
2382 static void qlt_load_cont_data_segments(struct qla_tgt_prm *prm,
2383 struct scsi_qla_host *vha)
2386 uint32_t *dword_ptr;
2387 int enable_64bit_addressing = prm->tgt->tgt_enable_64bit_addr;
2389 /* Build continuation packets */
2390 while (prm->seg_cnt > 0) {
2391 cont_a64_entry_t *cont_pkt64 =
2392 (cont_a64_entry_t *)qlt_get_req_pkt(
2393 prm->cmd->qpair->req);
2396 * Make sure that from cont_pkt64 none of
2397 * 64-bit specific fields used for 32-bit
2398 * addressing. Cast to (cont_entry_t *) for
2402 memset(cont_pkt64, 0, sizeof(*cont_pkt64));
2404 cont_pkt64->entry_count = 1;
2405 cont_pkt64->sys_define = 0;
2407 if (enable_64bit_addressing) {
2408 cont_pkt64->entry_type = CONTINUE_A64_TYPE;
2410 (uint32_t *)&cont_pkt64->dseg_0_address;
2412 cont_pkt64->entry_type = CONTINUE_TYPE;
2414 (uint32_t *)&((cont_entry_t *)
2415 cont_pkt64)->dseg_0_address;
2418 /* Load continuation entry data segments */
2420 cnt < prm->tgt->datasegs_per_cont && prm->seg_cnt;
2421 cnt++, prm->seg_cnt--) {
2423 cpu_to_le32(pci_dma_lo32
2424 (sg_dma_address(prm->sg)));
2425 if (enable_64bit_addressing) {
2427 cpu_to_le32(pci_dma_hi32
2431 *dword_ptr++ = cpu_to_le32(sg_dma_len(prm->sg));
2433 prm->sg = sg_next(prm->sg);
2439 * ha->hardware_lock supposed to be held on entry. We have already made sure
2440 * that there is sufficient amount of request entries to not drop it.
2442 static void qlt_load_data_segments(struct qla_tgt_prm *prm,
2443 struct scsi_qla_host *vha)
2446 uint32_t *dword_ptr;
2447 int enable_64bit_addressing = prm->tgt->tgt_enable_64bit_addr;
2448 struct ctio7_to_24xx *pkt24 = (struct ctio7_to_24xx *)prm->pkt;
2450 pkt24->u.status0.transfer_length = cpu_to_le32(prm->cmd->bufflen);
2452 /* Setup packet address segment pointer */
2453 dword_ptr = pkt24->u.status0.dseg_0_address;
2455 /* Set total data segment count */
2457 pkt24->dseg_count = cpu_to_le16(prm->seg_cnt);
2459 if (prm->seg_cnt == 0) {
2460 /* No data transfer */
2466 /* If scatter gather */
2468 /* Load command entry data segments */
2470 (cnt < prm->tgt->datasegs_per_cmd) && prm->seg_cnt;
2471 cnt++, prm->seg_cnt--) {
2473 cpu_to_le32(pci_dma_lo32(sg_dma_address(prm->sg)));
2474 if (enable_64bit_addressing) {
2476 cpu_to_le32(pci_dma_hi32(
2477 sg_dma_address(prm->sg)));
2479 *dword_ptr++ = cpu_to_le32(sg_dma_len(prm->sg));
2481 prm->sg = sg_next(prm->sg);
2484 qlt_load_cont_data_segments(prm, vha);
2487 static inline int qlt_has_data(struct qla_tgt_cmd *cmd)
2489 return cmd->bufflen > 0;
2492 static void qlt_print_dif_err(struct qla_tgt_prm *prm)
2494 struct qla_tgt_cmd *cmd;
2495 struct scsi_qla_host *vha;
2497 /* asc 0x10=dif error */
2498 if (prm->sense_buffer && (prm->sense_buffer[12] == 0x10)) {
2502 switch (prm->sense_buffer[13]) {
2504 ql_dbg(ql_dbg_tgt_dif, vha, 0xe00b,
2505 "BE detected Guard TAG ERR: lba[0x%llx|%lld] len[0x%x] "
2506 "se_cmd=%p tag[%x]",
2507 cmd->lba, cmd->lba, cmd->num_blks, &cmd->se_cmd,
2508 cmd->atio.u.isp24.exchange_addr);
2511 ql_dbg(ql_dbg_tgt_dif, vha, 0xe00c,
2512 "BE detected APP TAG ERR: lba[0x%llx|%lld] len[0x%x] "
2513 "se_cmd=%p tag[%x]",
2514 cmd->lba, cmd->lba, cmd->num_blks, &cmd->se_cmd,
2515 cmd->atio.u.isp24.exchange_addr);
2518 ql_dbg(ql_dbg_tgt_dif, vha, 0xe00f,
2519 "BE detected REF TAG ERR: lba[0x%llx|%lld] len[0x%x] "
2520 "se_cmd=%p tag[%x]",
2521 cmd->lba, cmd->lba, cmd->num_blks, &cmd->se_cmd,
2522 cmd->atio.u.isp24.exchange_addr);
2525 ql_dbg(ql_dbg_tgt_dif, vha, 0xe010,
2526 "BE detected Dif ERR: lba[%llx|%lld] len[%x] "
2527 "se_cmd=%p tag[%x]",
2528 cmd->lba, cmd->lba, cmd->num_blks, &cmd->se_cmd,
2529 cmd->atio.u.isp24.exchange_addr);
2532 ql_dump_buffer(ql_dbg_tgt_dif, vha, 0xe011, cmd->cdb, 16);
2537 * Called without ha->hardware_lock held
2539 static int qlt_pre_xmit_response(struct qla_tgt_cmd *cmd,
2540 struct qla_tgt_prm *prm, int xmit_type, uint8_t scsi_status,
2541 uint32_t *full_req_cnt)
2543 struct qla_tgt *tgt = cmd->tgt;
2544 struct scsi_qla_host *vha = tgt->vha;
2545 struct qla_hw_data *ha = vha->hw;
2546 struct se_cmd *se_cmd = &cmd->se_cmd;
2550 prm->rq_result = scsi_status;
2551 prm->sense_buffer = &cmd->sense_buffer[0];
2552 prm->sense_buffer_len = TRANSPORT_SENSE_BUFFER;
2556 prm->add_status_pkt = 0;
2558 if ((xmit_type & QLA_TGT_XMIT_DATA) && qlt_has_data(cmd)) {
2559 if (qlt_pci_map_calc_cnt(prm) != 0)
2563 *full_req_cnt = prm->req_cnt;
2565 if (se_cmd->se_cmd_flags & SCF_UNDERFLOW_BIT) {
2566 prm->residual = se_cmd->residual_count;
2567 ql_dbg(ql_dbg_io + ql_dbg_verbose, vha, 0x305c,
2568 "Residual underflow: %d (tag %lld, op %x, bufflen %d, rq_result %x)\n",
2569 prm->residual, se_cmd->tag,
2570 se_cmd->t_task_cdb ? se_cmd->t_task_cdb[0] : 0,
2571 cmd->bufflen, prm->rq_result);
2572 prm->rq_result |= SS_RESIDUAL_UNDER;
2573 } else if (se_cmd->se_cmd_flags & SCF_OVERFLOW_BIT) {
2574 prm->residual = se_cmd->residual_count;
2575 ql_dbg(ql_dbg_io, vha, 0x305d,
2576 "Residual overflow: %d (tag %lld, op %x, bufflen %d, rq_result %x)\n",
2577 prm->residual, se_cmd->tag, se_cmd->t_task_cdb ?
2578 se_cmd->t_task_cdb[0] : 0, cmd->bufflen, prm->rq_result);
2579 prm->rq_result |= SS_RESIDUAL_OVER;
2582 if (xmit_type & QLA_TGT_XMIT_STATUS) {
2584 * If QLA_TGT_XMIT_DATA is not set, add_status_pkt will be
2585 * ignored in *xmit_response() below
2587 if (qlt_has_data(cmd)) {
2588 if (QLA_TGT_SENSE_VALID(prm->sense_buffer) ||
2589 (IS_FWI2_CAPABLE(ha) &&
2590 (prm->rq_result != 0))) {
2591 prm->add_status_pkt = 1;
2600 static inline int qlt_need_explicit_conf(struct qla_hw_data *ha,
2601 struct qla_tgt_cmd *cmd, int sending_sense)
2603 if (ha->tgt.enable_class_2)
2607 return cmd->conf_compl_supported;
2609 return ha->tgt.enable_explicit_conf &&
2610 cmd->conf_compl_supported;
2613 static void qlt_24xx_init_ctio_to_isp(struct ctio7_to_24xx *ctio,
2614 struct qla_tgt_prm *prm)
2616 prm->sense_buffer_len = min_t(uint32_t, prm->sense_buffer_len,
2617 (uint32_t)sizeof(ctio->u.status1.sense_data));
2618 ctio->u.status0.flags |= cpu_to_le16(CTIO7_FLAGS_SEND_STATUS);
2619 if (qlt_need_explicit_conf(prm->tgt->ha, prm->cmd, 0)) {
2620 ctio->u.status0.flags |= cpu_to_le16(
2621 CTIO7_FLAGS_EXPLICIT_CONFORM |
2622 CTIO7_FLAGS_CONFORM_REQ);
2624 ctio->u.status0.residual = cpu_to_le32(prm->residual);
2625 ctio->u.status0.scsi_status = cpu_to_le16(prm->rq_result);
2626 if (QLA_TGT_SENSE_VALID(prm->sense_buffer)) {
2629 if (qlt_need_explicit_conf(prm->tgt->ha, prm->cmd, 1)) {
2630 if ((prm->rq_result & SS_SCSI_STATUS_BYTE) != 0) {
2631 ql_dbg(ql_dbg_tgt, prm->cmd->vha, 0xe017,
2632 "Skipping EXPLICIT_CONFORM and "
2633 "CTIO7_FLAGS_CONFORM_REQ for FCP READ w/ "
2634 "non GOOD status\n");
2635 goto skip_explict_conf;
2637 ctio->u.status1.flags |= cpu_to_le16(
2638 CTIO7_FLAGS_EXPLICIT_CONFORM |
2639 CTIO7_FLAGS_CONFORM_REQ);
2642 ctio->u.status1.flags &=
2643 ~cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_0);
2644 ctio->u.status1.flags |=
2645 cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_1);
2646 ctio->u.status1.scsi_status |=
2647 cpu_to_le16(SS_SENSE_LEN_VALID);
2648 ctio->u.status1.sense_length =
2649 cpu_to_le16(prm->sense_buffer_len);
2650 for (i = 0; i < prm->sense_buffer_len/4; i++)
2651 ((uint32_t *)ctio->u.status1.sense_data)[i] =
2652 cpu_to_be32(((uint32_t *)prm->sense_buffer)[i]);
2654 qlt_print_dif_err(prm);
2657 ctio->u.status1.flags &=
2658 ~cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_0);
2659 ctio->u.status1.flags |=
2660 cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_1);
2661 ctio->u.status1.sense_length = 0;
2662 memset(ctio->u.status1.sense_data, 0,
2663 sizeof(ctio->u.status1.sense_data));
2666 /* Sense with len > 24, is it possible ??? */
2670 qlt_hba_err_chk_enabled(struct se_cmd *se_cmd)
2672 switch (se_cmd->prot_op) {
2673 case TARGET_PROT_DOUT_INSERT:
2674 case TARGET_PROT_DIN_STRIP:
2675 if (ql2xenablehba_err_chk >= 1)
2678 case TARGET_PROT_DOUT_PASS:
2679 case TARGET_PROT_DIN_PASS:
2680 if (ql2xenablehba_err_chk >= 2)
2683 case TARGET_PROT_DIN_INSERT:
2684 case TARGET_PROT_DOUT_STRIP:
2693 qla_tgt_ref_mask_check(struct se_cmd *se_cmd)
2695 switch (se_cmd->prot_op) {
2696 case TARGET_PROT_DIN_INSERT:
2697 case TARGET_PROT_DOUT_INSERT:
2698 case TARGET_PROT_DIN_STRIP:
2699 case TARGET_PROT_DOUT_STRIP:
2700 case TARGET_PROT_DIN_PASS:
2701 case TARGET_PROT_DOUT_PASS:
2710 * qla_tgt_set_dif_tags - Extract Ref and App tags from SCSI command
2713 qla_tgt_set_dif_tags(struct qla_tgt_cmd *cmd, struct crc_context *ctx,
2714 uint16_t *pfw_prot_opts)
2716 struct se_cmd *se_cmd = &cmd->se_cmd;
2717 uint32_t lba = 0xffffffff & se_cmd->t_task_lba;
2718 scsi_qla_host_t *vha = cmd->tgt->vha;
2719 struct qla_hw_data *ha = vha->hw;
2723 * wait till Mode Sense/Select cmd, modepage Ah, subpage 2
2724 * have been immplemented by TCM, before AppTag is avail.
2725 * Look for modesense_handlers[]
2728 ctx->app_tag_mask[0] = 0x0;
2729 ctx->app_tag_mask[1] = 0x0;
2731 if (IS_PI_UNINIT_CAPABLE(ha)) {
2732 if ((se_cmd->prot_type == TARGET_DIF_TYPE1_PROT) ||
2733 (se_cmd->prot_type == TARGET_DIF_TYPE2_PROT))
2734 *pfw_prot_opts |= PO_DIS_VALD_APP_ESC;
2735 else if (se_cmd->prot_type == TARGET_DIF_TYPE3_PROT)
2736 *pfw_prot_opts |= PO_DIS_VALD_APP_REF_ESC;
2739 t32 = ha->tgt.tgt_ops->get_dif_tags(cmd, pfw_prot_opts);
2741 switch (se_cmd->prot_type) {
2742 case TARGET_DIF_TYPE0_PROT:
2744 * No check for ql2xenablehba_err_chk, as it
2745 * would be an I/O error if hba tag generation
2748 ctx->ref_tag = cpu_to_le32(lba);
2749 /* enable ALL bytes of the ref tag */
2750 ctx->ref_tag_mask[0] = 0xff;
2751 ctx->ref_tag_mask[1] = 0xff;
2752 ctx->ref_tag_mask[2] = 0xff;
2753 ctx->ref_tag_mask[3] = 0xff;
2755 case TARGET_DIF_TYPE1_PROT:
2757 * For TYPE 1 protection: 16 bit GUARD tag, 32 bit
2758 * REF tag, and 16 bit app tag.
2760 ctx->ref_tag = cpu_to_le32(lba);
2761 if (!qla_tgt_ref_mask_check(se_cmd) ||
2762 !(ha->tgt.tgt_ops->chk_dif_tags(t32))) {
2763 *pfw_prot_opts |= PO_DIS_REF_TAG_VALD;
2766 /* enable ALL bytes of the ref tag */
2767 ctx->ref_tag_mask[0] = 0xff;
2768 ctx->ref_tag_mask[1] = 0xff;
2769 ctx->ref_tag_mask[2] = 0xff;
2770 ctx->ref_tag_mask[3] = 0xff;
2772 case TARGET_DIF_TYPE2_PROT:
2774 * For TYPE 2 protection: 16 bit GUARD + 32 bit REF
2775 * tag has to match LBA in CDB + N
2777 ctx->ref_tag = cpu_to_le32(lba);
2778 if (!qla_tgt_ref_mask_check(se_cmd) ||
2779 !(ha->tgt.tgt_ops->chk_dif_tags(t32))) {
2780 *pfw_prot_opts |= PO_DIS_REF_TAG_VALD;
2783 /* enable ALL bytes of the ref tag */
2784 ctx->ref_tag_mask[0] = 0xff;
2785 ctx->ref_tag_mask[1] = 0xff;
2786 ctx->ref_tag_mask[2] = 0xff;
2787 ctx->ref_tag_mask[3] = 0xff;
2789 case TARGET_DIF_TYPE3_PROT:
2790 /* For TYPE 3 protection: 16 bit GUARD only */
2791 *pfw_prot_opts |= PO_DIS_REF_TAG_VALD;
2792 ctx->ref_tag_mask[0] = ctx->ref_tag_mask[1] =
2793 ctx->ref_tag_mask[2] = ctx->ref_tag_mask[3] = 0x00;
2799 qlt_build_ctio_crc2_pkt(struct qla_qpair *qpair, struct qla_tgt_prm *prm)
2802 uint32_t transfer_length = 0;
2803 uint32_t data_bytes;
2805 uint8_t bundling = 1;
2807 struct crc_context *crc_ctx_pkt = NULL;
2808 struct qla_hw_data *ha;
2809 struct ctio_crc2_to_fw *pkt;
2810 dma_addr_t crc_ctx_dma;
2811 uint16_t fw_prot_opts = 0;
2812 struct qla_tgt_cmd *cmd = prm->cmd;
2813 struct se_cmd *se_cmd = &cmd->se_cmd;
2815 struct atio_from_isp *atio = &prm->cmd->atio;
2816 struct qla_tc_param tc;
2818 scsi_qla_host_t *vha = cmd->vha;
2822 pkt = (struct ctio_crc2_to_fw *)qpair->req->ring_ptr;
2824 memset(pkt, 0, sizeof(*pkt));
2826 ql_dbg(ql_dbg_tgt, vha, 0xe071,
2827 "qla_target(%d):%s: se_cmd[%p] CRC2 prot_op[0x%x] cmd prot sg:cnt[%p:%x] lba[%llu]\n",
2828 vha->vp_idx, __func__, se_cmd, se_cmd->prot_op,
2829 prm->prot_sg, prm->prot_seg_cnt, se_cmd->t_task_lba);
2831 if ((se_cmd->prot_op == TARGET_PROT_DIN_INSERT) ||
2832 (se_cmd->prot_op == TARGET_PROT_DOUT_STRIP))
2835 /* Compute dif len and adjust data len to incude protection */
2836 data_bytes = cmd->bufflen;
2837 dif_bytes = (data_bytes / cmd->blk_sz) * 8;
2839 switch (se_cmd->prot_op) {
2840 case TARGET_PROT_DIN_INSERT:
2841 case TARGET_PROT_DOUT_STRIP:
2842 transfer_length = data_bytes;
2843 if (cmd->prot_sg_cnt)
2844 data_bytes += dif_bytes;
2846 case TARGET_PROT_DIN_STRIP:
2847 case TARGET_PROT_DOUT_INSERT:
2848 case TARGET_PROT_DIN_PASS:
2849 case TARGET_PROT_DOUT_PASS:
2850 transfer_length = data_bytes + dif_bytes;
2857 if (!qlt_hba_err_chk_enabled(se_cmd))
2858 fw_prot_opts |= 0x10; /* Disable Guard tag checking */
2859 /* HBA error checking enabled */
2860 else if (IS_PI_UNINIT_CAPABLE(ha)) {
2861 if ((se_cmd->prot_type == TARGET_DIF_TYPE1_PROT) ||
2862 (se_cmd->prot_type == TARGET_DIF_TYPE2_PROT))
2863 fw_prot_opts |= PO_DIS_VALD_APP_ESC;
2864 else if (se_cmd->prot_type == TARGET_DIF_TYPE3_PROT)
2865 fw_prot_opts |= PO_DIS_VALD_APP_REF_ESC;
2868 switch (se_cmd->prot_op) {
2869 case TARGET_PROT_DIN_INSERT:
2870 case TARGET_PROT_DOUT_INSERT:
2871 fw_prot_opts |= PO_MODE_DIF_INSERT;
2873 case TARGET_PROT_DIN_STRIP:
2874 case TARGET_PROT_DOUT_STRIP:
2875 fw_prot_opts |= PO_MODE_DIF_REMOVE;
2877 case TARGET_PROT_DIN_PASS:
2878 case TARGET_PROT_DOUT_PASS:
2879 fw_prot_opts |= PO_MODE_DIF_PASS;
2880 /* FUTURE: does tcm require T10CRC<->IPCKSUM conversion? */
2882 default:/* Normal Request */
2883 fw_prot_opts |= PO_MODE_DIF_PASS;
2888 /* Update entry type to indicate Command Type CRC_2 IOCB */
2889 pkt->entry_type = CTIO_CRC2;
2890 pkt->entry_count = 1;
2891 pkt->vp_index = vha->vp_idx;
2893 h = qlt_make_handle(qpair);
2894 if (unlikely(h == QLA_TGT_NULL_HANDLE)) {
2896 * CTIO type 7 from the firmware doesn't provide a way to
2897 * know the initiator's LOOP ID, hence we can't find
2898 * the session and, so, the command.
2902 qpair->req->outstanding_cmds[h] = (srb_t *)prm->cmd;
2904 pkt->handle = MAKE_HANDLE(qpair->req->id, h);
2905 pkt->handle |= CTIO_COMPLETION_HANDLE_MARK;
2906 pkt->nport_handle = cpu_to_le16(prm->cmd->loop_id);
2907 pkt->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
2908 pkt->initiator_id[0] = atio->u.isp24.fcp_hdr.s_id[2];
2909 pkt->initiator_id[1] = atio->u.isp24.fcp_hdr.s_id[1];
2910 pkt->initiator_id[2] = atio->u.isp24.fcp_hdr.s_id[0];
2911 pkt->exchange_addr = atio->u.isp24.exchange_addr;
2913 /* silence compile warning */
2914 t16 = be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id);
2915 pkt->ox_id = cpu_to_le16(t16);
2917 t16 = (atio->u.isp24.attr << 9);
2918 pkt->flags |= cpu_to_le16(t16);
2919 pkt->relative_offset = cpu_to_le32(prm->cmd->offset);
2921 /* Set transfer direction */
2922 if (cmd->dma_data_direction == DMA_TO_DEVICE)
2923 pkt->flags = cpu_to_le16(CTIO7_FLAGS_DATA_IN);
2924 else if (cmd->dma_data_direction == DMA_FROM_DEVICE)
2925 pkt->flags = cpu_to_le16(CTIO7_FLAGS_DATA_OUT);
2927 pkt->dseg_count = prm->tot_dsds;
2928 /* Fibre channel byte count */
2929 pkt->transfer_length = cpu_to_le32(transfer_length);
2931 /* ----- CRC context -------- */
2933 /* Allocate CRC context from global pool */
2934 crc_ctx_pkt = cmd->ctx =
2935 dma_pool_alloc(ha->dl_dma_pool, GFP_ATOMIC, &crc_ctx_dma);
2938 goto crc_queuing_error;
2940 /* Zero out CTX area. */
2941 clr_ptr = (uint8_t *)crc_ctx_pkt;
2942 memset(clr_ptr, 0, sizeof(*crc_ctx_pkt));
2944 crc_ctx_pkt->crc_ctx_dma = crc_ctx_dma;
2945 INIT_LIST_HEAD(&crc_ctx_pkt->dsd_list);
2948 crc_ctx_pkt->handle = pkt->handle;
2950 qla_tgt_set_dif_tags(cmd, crc_ctx_pkt, &fw_prot_opts);
2952 pkt->crc_context_address[0] = cpu_to_le32(LSD(crc_ctx_dma));
2953 pkt->crc_context_address[1] = cpu_to_le32(MSD(crc_ctx_dma));
2954 pkt->crc_context_len = CRC_CONTEXT_LEN_FW;
2957 cur_dsd = (uint32_t *) &crc_ctx_pkt->u.nobundling.data_address;
2960 * Configure Bundling if we need to fetch interlaving
2961 * protection PCI accesses
2963 fw_prot_opts |= PO_ENABLE_DIF_BUNDLING;
2964 crc_ctx_pkt->u.bundling.dif_byte_count = cpu_to_le32(dif_bytes);
2965 crc_ctx_pkt->u.bundling.dseg_count =
2966 cpu_to_le16(prm->tot_dsds - prm->prot_seg_cnt);
2967 cur_dsd = (uint32_t *) &crc_ctx_pkt->u.bundling.data_address;
2970 /* Finish the common fields of CRC pkt */
2971 crc_ctx_pkt->blk_size = cpu_to_le16(cmd->blk_sz);
2972 crc_ctx_pkt->prot_opts = cpu_to_le16(fw_prot_opts);
2973 crc_ctx_pkt->byte_count = cpu_to_le32(data_bytes);
2974 crc_ctx_pkt->guard_seed = cpu_to_le16(0);
2976 memset((uint8_t *)&tc, 0 , sizeof(tc));
2978 tc.blk_sz = cmd->blk_sz;
2979 tc.bufflen = cmd->bufflen;
2981 tc.prot_sg = cmd->prot_sg;
2982 tc.ctx = crc_ctx_pkt;
2983 tc.ctx_dsd_alloced = &cmd->ctx_dsd_alloced;
2985 /* Walks data segments */
2986 pkt->flags |= cpu_to_le16(CTIO7_FLAGS_DSD_PTR);
2988 if (!bundling && prm->prot_seg_cnt) {
2989 if (qla24xx_walk_and_build_sglist_no_difb(ha, NULL, cur_dsd,
2990 prm->tot_dsds, &tc))
2991 goto crc_queuing_error;
2992 } else if (qla24xx_walk_and_build_sglist(ha, NULL, cur_dsd,
2993 (prm->tot_dsds - prm->prot_seg_cnt), &tc))
2994 goto crc_queuing_error;
2996 if (bundling && prm->prot_seg_cnt) {
2997 /* Walks dif segments */
2998 pkt->add_flags |= CTIO_CRC2_AF_DIF_DSD_ENA;
3000 cur_dsd = (uint32_t *) &crc_ctx_pkt->u.bundling.dif_address;
3001 if (qla24xx_walk_and_build_prot_sglist(ha, NULL, cur_dsd,
3002 prm->prot_seg_cnt, &tc))
3003 goto crc_queuing_error;
3008 /* Cleanup will be performed by the caller */
3009 qpair->req->outstanding_cmds[h] = NULL;
3011 return QLA_FUNCTION_FAILED;
3015 * Callback to setup response of xmit_type of QLA_TGT_XMIT_DATA and *
3016 * QLA_TGT_XMIT_STATUS for >= 24xx silicon
3018 int qlt_xmit_response(struct qla_tgt_cmd *cmd, int xmit_type,
3019 uint8_t scsi_status)
3021 struct scsi_qla_host *vha = cmd->vha;
3022 struct qla_hw_data *ha = vha->hw;
3023 struct qla_qpair *qpair = cmd->qpair;
3024 struct ctio7_to_24xx *pkt;
3025 struct qla_tgt_prm prm;
3026 uint32_t full_req_cnt = 0;
3027 unsigned long flags = 0;
3030 if (cmd->sess && cmd->sess->deleted) {
3031 cmd->state = QLA_TGT_STATE_PROCESSED;
3032 if (cmd->sess->logout_completed)
3033 /* no need to terminate. FW already freed exchange. */
3034 qlt_abort_cmd_on_host_reset(cmd->vha, cmd);
3036 qlt_send_term_exchange(qpair, cmd, &cmd->atio, 0, 0);
3040 memset(&prm, 0, sizeof(prm));
3042 ql_dbg(ql_dbg_tgt, cmd->vha, 0xe018,
3043 "is_send_status=%d, cmd->bufflen=%d, cmd->sg_cnt=%d, cmd->dma_data_direction=%d se_cmd[%p] qp %d\n",
3044 (xmit_type & QLA_TGT_XMIT_STATUS) ?
3045 1 : 0, cmd->bufflen, cmd->sg_cnt, cmd->dma_data_direction,
3046 &cmd->se_cmd, qpair->id);
3048 res = qlt_pre_xmit_response(cmd, &prm, xmit_type, scsi_status,
3050 if (unlikely(res != 0)) {
3054 spin_lock_irqsave(qpair->qp_lock_ptr, flags);
3056 if (xmit_type == QLA_TGT_XMIT_STATUS)
3057 vha->tgt_counters.core_qla_snd_status++;
3059 vha->tgt_counters.core_qla_que_buf++;
3061 if (!ha->flags.fw_started || cmd->reset_count != ha->chip_reset) {
3063 * Either the port is not online or this request was from
3064 * previous life, just abort the processing.
3066 cmd->state = QLA_TGT_STATE_PROCESSED;
3067 qlt_abort_cmd_on_host_reset(cmd->vha, cmd);
3068 ql_dbg(ql_dbg_async, vha, 0xe101,
3069 "RESET-RSP online/active/old-count/new-count = %d/%d/%d/%d.\n",
3070 vha->flags.online, qla2x00_reset_active(vha),
3071 cmd->reset_count, ha->chip_reset);
3072 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
3076 /* Does F/W have an IOCBs for this request */
3077 res = qlt_check_reserve_free_req(qpair, full_req_cnt);
3079 goto out_unmap_unlock;
3081 if (cmd->se_cmd.prot_op && (xmit_type & QLA_TGT_XMIT_DATA))
3082 res = qlt_build_ctio_crc2_pkt(qpair, &prm);
3084 res = qlt_24xx_build_ctio_pkt(qpair, &prm);
3085 if (unlikely(res != 0)) {
3086 qpair->req->cnt += full_req_cnt;
3087 goto out_unmap_unlock;
3090 pkt = (struct ctio7_to_24xx *)prm.pkt;
3092 if (qlt_has_data(cmd) && (xmit_type & QLA_TGT_XMIT_DATA)) {
3093 pkt->u.status0.flags |=
3094 cpu_to_le16(CTIO7_FLAGS_DATA_IN |
3095 CTIO7_FLAGS_STATUS_MODE_0);
3097 if (cmd->se_cmd.prot_op == TARGET_PROT_NORMAL)
3098 qlt_load_data_segments(&prm, vha);
3100 if (prm.add_status_pkt == 0) {
3101 if (xmit_type & QLA_TGT_XMIT_STATUS) {
3102 pkt->u.status0.scsi_status =
3103 cpu_to_le16(prm.rq_result);
3104 pkt->u.status0.residual =
3105 cpu_to_le32(prm.residual);
3106 pkt->u.status0.flags |= cpu_to_le16(
3107 CTIO7_FLAGS_SEND_STATUS);
3108 if (qlt_need_explicit_conf(ha, cmd, 0)) {
3109 pkt->u.status0.flags |=
3111 CTIO7_FLAGS_EXPLICIT_CONFORM |
3112 CTIO7_FLAGS_CONFORM_REQ);
3118 * We have already made sure that there is sufficient
3119 * amount of request entries to not drop HW lock in
3122 struct ctio7_to_24xx *ctio =
3123 (struct ctio7_to_24xx *)qlt_get_req_pkt(
3126 ql_dbg(ql_dbg_tgt, vha, 0x305e,
3127 "Building additional status packet 0x%p.\n",
3131 * T10Dif: ctio_crc2_to_fw overlay ontop of
3134 memcpy(ctio, pkt, sizeof(*ctio));
3135 /* reset back to CTIO7 */
3136 ctio->entry_count = 1;
3137 ctio->entry_type = CTIO_TYPE7;
3138 ctio->dseg_count = 0;
3139 ctio->u.status1.flags &= ~cpu_to_le16(
3140 CTIO7_FLAGS_DATA_IN);
3142 /* Real finish is ctio_m1's finish */
3143 pkt->handle |= CTIO_INTERMEDIATE_HANDLE_MARK;
3144 pkt->u.status0.flags |= cpu_to_le16(
3145 CTIO7_FLAGS_DONT_RET_CTIO);
3147 /* qlt_24xx_init_ctio_to_isp will correct
3148 * all neccessary fields that's part of CTIO7.
3149 * There should be no residual of CTIO-CRC2 data.
3151 qlt_24xx_init_ctio_to_isp((struct ctio7_to_24xx *)ctio,
3153 pr_debug("Status CTIO7: %p\n", ctio);
3156 qlt_24xx_init_ctio_to_isp(pkt, &prm);
3159 cmd->state = QLA_TGT_STATE_PROCESSED; /* Mid-level is done processing */
3160 cmd->cmd_sent_to_fw = 1;
3162 /* Memory Barrier */
3164 qla2x00_start_iocbs(vha, qpair->req);
3165 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
3170 qlt_unmap_sg(vha, cmd);
3171 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
3175 EXPORT_SYMBOL(qlt_xmit_response);
3177 int qlt_rdy_to_xfer(struct qla_tgt_cmd *cmd)
3179 struct ctio7_to_24xx *pkt;
3180 struct scsi_qla_host *vha = cmd->vha;
3181 struct qla_hw_data *ha = vha->hw;
3182 struct qla_tgt *tgt = cmd->tgt;
3183 struct qla_tgt_prm prm;
3184 unsigned long flags = 0;
3186 struct qla_qpair *qpair = cmd->qpair;
3188 memset(&prm, 0, sizeof(prm));
3194 /* Calculate number of entries and segments required */
3195 if (qlt_pci_map_calc_cnt(&prm) != 0)
3198 if (!ha->flags.fw_started || (cmd->reset_count != ha->chip_reset) ||
3199 (cmd->sess && cmd->sess->deleted)) {
3201 * Either the port is not online or this request was from
3202 * previous life, just abort the processing.
3204 cmd->state = QLA_TGT_STATE_NEED_DATA;
3205 qlt_abort_cmd_on_host_reset(cmd->vha, cmd);
3206 ql_dbg(ql_dbg_async, vha, 0xe102,
3207 "RESET-XFR online/active/old-count/new-count = %d/%d/%d/%d.\n",
3208 vha->flags.online, qla2x00_reset_active(vha),
3209 cmd->reset_count, ha->chip_reset);
3213 spin_lock_irqsave(qpair->qp_lock_ptr, flags);
3214 /* Does F/W have an IOCBs for this request */
3215 res = qlt_check_reserve_free_req(qpair, prm.req_cnt);
3217 goto out_unlock_free_unmap;
3218 if (cmd->se_cmd.prot_op)
3219 res = qlt_build_ctio_crc2_pkt(qpair, &prm);
3221 res = qlt_24xx_build_ctio_pkt(qpair, &prm);
3223 if (unlikely(res != 0)) {
3224 qpair->req->cnt += prm.req_cnt;
3225 goto out_unlock_free_unmap;
3228 pkt = (struct ctio7_to_24xx *)prm.pkt;
3229 pkt->u.status0.flags |= cpu_to_le16(CTIO7_FLAGS_DATA_OUT |
3230 CTIO7_FLAGS_STATUS_MODE_0);
3232 if (cmd->se_cmd.prot_op == TARGET_PROT_NORMAL)
3233 qlt_load_data_segments(&prm, vha);
3235 cmd->state = QLA_TGT_STATE_NEED_DATA;
3236 cmd->cmd_sent_to_fw = 1;
3238 /* Memory Barrier */
3240 qla2x00_start_iocbs(vha, qpair->req);
3241 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
3245 out_unlock_free_unmap:
3246 qlt_unmap_sg(vha, cmd);
3247 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
3251 EXPORT_SYMBOL(qlt_rdy_to_xfer);
3255 * it is assumed either hardware_lock or qpair lock is held.
3258 qlt_handle_dif_error(struct scsi_qla_host *vha, struct qla_tgt_cmd *cmd,
3259 struct ctio_crc_from_fw *sts)
3261 uint8_t *ap = &sts->actual_dif[0];
3262 uint8_t *ep = &sts->expected_dif[0];
3263 uint64_t lba = cmd->se_cmd.t_task_lba;
3264 uint8_t scsi_status, sense_key, asc, ascq;
3265 unsigned long flags;
3267 cmd->trc_flags |= TRC_DIF_ERR;
3269 cmd->a_guard = be16_to_cpu(*(uint16_t *)(ap + 0));
3270 cmd->a_app_tag = be16_to_cpu(*(uint16_t *)(ap + 2));
3271 cmd->a_ref_tag = be32_to_cpu(*(uint32_t *)(ap + 4));
3273 cmd->e_guard = be16_to_cpu(*(uint16_t *)(ep + 0));
3274 cmd->e_app_tag = be16_to_cpu(*(uint16_t *)(ep + 2));
3275 cmd->e_ref_tag = be32_to_cpu(*(uint32_t *)(ep + 4));
3277 ql_dbg(ql_dbg_tgt_dif, vha, 0xf075,
3278 "%s: aborted %d state %d\n", __func__, cmd->aborted, cmd->state);
3280 scsi_status = sense_key = asc = ascq = 0;
3282 /* check appl tag */
3283 if (cmd->e_app_tag != cmd->a_app_tag) {
3284 ql_dbg(ql_dbg_tgt_dif, vha, 0xe00d,
3285 "App Tag ERR: cdb[%x] lba[%llx %llx] blks[%x] [Actual|Expected] Ref[%x|%x], App[%x|%x], Guard [%x|%x] cmd=%p ox_id[%04x]",
3286 cmd->cdb[0], lba, (lba+cmd->num_blks), cmd->num_blks,
3287 cmd->a_ref_tag, cmd->e_ref_tag, cmd->a_app_tag,
3288 cmd->e_app_tag, cmd->a_guard, cmd->e_guard, cmd,
3289 cmd->atio.u.isp24.fcp_hdr.ox_id);
3291 cmd->dif_err_code = DIF_ERR_APP;
3292 scsi_status = SAM_STAT_CHECK_CONDITION;
3293 sense_key = ABORTED_COMMAND;
3299 if (cmd->e_ref_tag != cmd->a_ref_tag) {
3300 ql_dbg(ql_dbg_tgt_dif, vha, 0xe00e,
3301 "Ref Tag ERR: cdb[%x] lba[%llx %llx] blks[%x] [Actual|Expected] Ref[%x|%x], App[%x|%x], Guard[%x|%x] cmd=%p ox_id[%04x] ",
3302 cmd->cdb[0], lba, (lba+cmd->num_blks), cmd->num_blks,
3303 cmd->a_ref_tag, cmd->e_ref_tag, cmd->a_app_tag,
3304 cmd->e_app_tag, cmd->a_guard, cmd->e_guard, cmd,
3305 cmd->atio.u.isp24.fcp_hdr.ox_id);
3307 cmd->dif_err_code = DIF_ERR_REF;
3308 scsi_status = SAM_STAT_CHECK_CONDITION;
3309 sense_key = ABORTED_COMMAND;
3316 if (cmd->e_guard != cmd->a_guard) {
3317 ql_dbg(ql_dbg_tgt_dif, vha, 0xe012,
3318 "Guard ERR: cdb[%x] lba[%llx %llx] blks[%x] [Actual|Expected] Ref[%x|%x], App[%x|%x], Guard [%x|%x] cmd=%p ox_id[%04x]",
3319 cmd->cdb[0], lba, (lba+cmd->num_blks), cmd->num_blks,
3320 cmd->a_ref_tag, cmd->e_ref_tag, cmd->a_app_tag,
3321 cmd->e_app_tag, cmd->a_guard, cmd->e_guard, cmd,
3322 cmd->atio.u.isp24.fcp_hdr.ox_id);
3324 cmd->dif_err_code = DIF_ERR_GRD;
3325 scsi_status = SAM_STAT_CHECK_CONDITION;
3326 sense_key = ABORTED_COMMAND;
3331 switch (cmd->state) {
3332 case QLA_TGT_STATE_NEED_DATA:
3333 /* handle_data will load DIF error code */
3334 cmd->state = QLA_TGT_STATE_DATA_IN;
3335 vha->hw->tgt.tgt_ops->handle_data(cmd);
3338 spin_lock_irqsave(&cmd->cmd_lock, flags);
3340 spin_unlock_irqrestore(&cmd->cmd_lock, flags);
3341 vha->hw->tgt.tgt_ops->free_cmd(cmd);
3344 spin_unlock_irqrestore(&cmd->cmd_lock, flags);
3346 qlt_send_resp_ctio(vha, cmd, scsi_status, sense_key, asc, ascq);
3347 /* assume scsi status gets out on the wire.
3348 * Will not wait for completion.
3350 vha->hw->tgt.tgt_ops->free_cmd(cmd);
3355 /* If hardware_lock held on entry, might drop it, then reaquire */
3356 /* This function sends the appropriate CTIO to ISP 2xxx or 24xx */
3357 static int __qlt_send_term_imm_notif(struct scsi_qla_host *vha,
3358 struct imm_ntfy_from_isp *ntfy)
3360 struct nack_to_isp *nack;
3361 struct qla_hw_data *ha = vha->hw;
3365 ql_dbg(ql_dbg_tgt_tmr, vha, 0xe01c,
3366 "Sending TERM ELS CTIO (ha=%p)\n", ha);
3368 pkt = (request_t *)qla2x00_alloc_iocbs(vha, NULL);
3370 ql_dbg(ql_dbg_tgt, vha, 0xe080,
3371 "qla_target(%d): %s failed: unable to allocate "
3372 "request packet\n", vha->vp_idx, __func__);
3376 pkt->entry_type = NOTIFY_ACK_TYPE;
3377 pkt->entry_count = 1;
3378 pkt->handle = QLA_TGT_SKIP_HANDLE;
3380 nack = (struct nack_to_isp *)pkt;
3381 nack->ox_id = ntfy->ox_id;
3383 nack->u.isp24.nport_handle = ntfy->u.isp24.nport_handle;
3384 if (le16_to_cpu(ntfy->u.isp24.status) == IMM_NTFY_ELS) {
3385 nack->u.isp24.flags = ntfy->u.isp24.flags &
3386 __constant_cpu_to_le32(NOTIFY24XX_FLAGS_PUREX_IOCB);
3390 nack->u.isp24.flags |=
3391 __constant_cpu_to_le16(NOTIFY_ACK_FLAGS_TERMINATE);
3393 nack->u.isp24.srr_rx_id = ntfy->u.isp24.srr_rx_id;
3394 nack->u.isp24.status = ntfy->u.isp24.status;
3395 nack->u.isp24.status_subcode = ntfy->u.isp24.status_subcode;
3396 nack->u.isp24.fw_handle = ntfy->u.isp24.fw_handle;
3397 nack->u.isp24.exchange_address = ntfy->u.isp24.exchange_address;
3398 nack->u.isp24.srr_rel_offs = ntfy->u.isp24.srr_rel_offs;
3399 nack->u.isp24.srr_ui = ntfy->u.isp24.srr_ui;
3400 nack->u.isp24.vp_index = ntfy->u.isp24.vp_index;
3402 qla2x00_start_iocbs(vha, vha->req);
3406 static void qlt_send_term_imm_notif(struct scsi_qla_host *vha,
3407 struct imm_ntfy_from_isp *imm, int ha_locked)
3409 unsigned long flags = 0;
3413 rc = __qlt_send_term_imm_notif(vha, imm);
3417 qlt_alloc_qfull_cmd(vha, imm, 0, 0);
3425 spin_lock_irqsave(&vha->hw->hardware_lock, flags);
3426 rc = __qlt_send_term_imm_notif(vha, imm);
3430 qlt_alloc_qfull_cmd(vha, imm, 0, 0);
3435 spin_unlock_irqrestore(&vha->hw->hardware_lock, flags);
3439 * If hardware_lock held on entry, might drop it, then reaquire
3440 * This function sends the appropriate CTIO to ISP 2xxx or 24xx
3442 static int __qlt_send_term_exchange(struct qla_qpair *qpair,
3443 struct qla_tgt_cmd *cmd,
3444 struct atio_from_isp *atio)
3446 struct scsi_qla_host *vha = qpair->vha;
3447 struct ctio7_to_24xx *ctio24;
3448 struct qla_hw_data *ha = vha->hw;
3453 ql_dbg(ql_dbg_tgt, vha, 0xe009, "Sending TERM EXCH CTIO (ha=%p)\n", ha);
3455 pkt = (request_t *)qla2x00_alloc_iocbs_ready(qpair, NULL);
3457 ql_dbg(ql_dbg_tgt, vha, 0xe050,
3458 "qla_target(%d): %s failed: unable to allocate "
3459 "request packet\n", vha->vp_idx, __func__);
3464 if (cmd->state < QLA_TGT_STATE_PROCESSED) {
3465 ql_dbg(ql_dbg_tgt, vha, 0xe051,
3466 "qla_target(%d): Terminating cmd %p with "
3467 "incorrect state %d\n", vha->vp_idx, cmd,
3473 vha->tgt_counters.num_term_xchg_sent++;
3474 pkt->entry_count = 1;
3475 pkt->handle = QLA_TGT_SKIP_HANDLE | CTIO_COMPLETION_HANDLE_MARK;
3477 ctio24 = (struct ctio7_to_24xx *)pkt;
3478 ctio24->entry_type = CTIO_TYPE7;
3479 ctio24->nport_handle = CTIO7_NHANDLE_UNRECOGNIZED;
3480 ctio24->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
3481 ctio24->vp_index = vha->vp_idx;
3482 ctio24->initiator_id[0] = atio->u.isp24.fcp_hdr.s_id[2];
3483 ctio24->initiator_id[1] = atio->u.isp24.fcp_hdr.s_id[1];
3484 ctio24->initiator_id[2] = atio->u.isp24.fcp_hdr.s_id[0];
3485 ctio24->exchange_addr = atio->u.isp24.exchange_addr;
3486 temp = (atio->u.isp24.attr << 9) | CTIO7_FLAGS_STATUS_MODE_1 |
3487 CTIO7_FLAGS_TERMINATE;
3488 ctio24->u.status1.flags = cpu_to_le16(temp);
3489 temp = be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id);
3490 ctio24->u.status1.ox_id = cpu_to_le16(temp);
3492 /* Most likely, it isn't needed */
3493 ctio24->u.status1.residual = get_unaligned((uint32_t *)
3494 &atio->u.isp24.fcp_cmnd.add_cdb[
3495 atio->u.isp24.fcp_cmnd.add_cdb_len]);
3496 if (ctio24->u.status1.residual != 0)
3497 ctio24->u.status1.scsi_status |= SS_RESIDUAL_UNDER;
3499 /* Memory Barrier */
3501 qla2x00_start_iocbs(vha, qpair->req);
3505 static void qlt_send_term_exchange(struct qla_qpair *qpair,
3506 struct qla_tgt_cmd *cmd, struct atio_from_isp *atio, int ha_locked,
3509 struct scsi_qla_host *vha;
3510 unsigned long flags = 0;
3513 /* why use different vha? NPIV */
3520 rc = __qlt_send_term_exchange(qpair, cmd, atio);
3522 qlt_alloc_qfull_cmd(vha, atio, 0, 0);
3525 spin_lock_irqsave(qpair->qp_lock_ptr, flags);
3526 rc = __qlt_send_term_exchange(qpair, cmd, atio);
3528 qlt_alloc_qfull_cmd(vha, atio, 0, 0);
3531 if (cmd && !ul_abort && !cmd->aborted) {
3533 qlt_unmap_sg(vha, cmd);
3534 vha->hw->tgt.tgt_ops->free_cmd(cmd);
3538 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
3543 static void qlt_init_term_exchange(struct scsi_qla_host *vha)
3545 struct list_head free_list;
3546 struct qla_tgt_cmd *cmd, *tcmd;
3548 vha->hw->tgt.leak_exchg_thresh_hold =
3549 (vha->hw->cur_fw_xcb_count/100) * LEAK_EXCHG_THRESH_HOLD_PERCENT;
3552 if (!list_empty(&vha->hw->tgt.q_full_list)) {
3553 INIT_LIST_HEAD(&free_list);
3554 list_splice_init(&vha->hw->tgt.q_full_list, &free_list);
3556 list_for_each_entry_safe(cmd, tcmd, &free_list, cmd_list) {
3557 list_del(&cmd->cmd_list);
3558 /* This cmd was never sent to TCM. There is no need
3559 * to schedule free or call free_cmd
3562 vha->hw->tgt.num_qfull_cmds_alloc--;
3565 vha->hw->tgt.num_qfull_cmds_dropped = 0;
3568 static void qlt_chk_exch_leak_thresh_hold(struct scsi_qla_host *vha)
3570 uint32_t total_leaked;
3572 total_leaked = vha->hw->tgt.num_qfull_cmds_dropped;
3574 if (vha->hw->tgt.leak_exchg_thresh_hold &&
3575 (total_leaked > vha->hw->tgt.leak_exchg_thresh_hold)) {
3577 ql_dbg(ql_dbg_tgt, vha, 0xe079,
3578 "Chip reset due to exchange starvation: %d/%d.\n",
3579 total_leaked, vha->hw->cur_fw_xcb_count);
3581 if (IS_P3P_TYPE(vha->hw))
3582 set_bit(FCOE_CTX_RESET_NEEDED, &vha->dpc_flags);
3584 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
3585 qla2xxx_wake_dpc(vha);
3590 int qlt_abort_cmd(struct qla_tgt_cmd *cmd)
3592 struct qla_tgt *tgt = cmd->tgt;
3593 struct scsi_qla_host *vha = tgt->vha;
3594 struct se_cmd *se_cmd = &cmd->se_cmd;
3595 unsigned long flags;
3597 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf014,
3598 "qla_target(%d): terminating exchange for aborted cmd=%p "
3599 "(se_cmd=%p, tag=%llu)", vha->vp_idx, cmd, &cmd->se_cmd,
3602 spin_lock_irqsave(&cmd->cmd_lock, flags);
3604 spin_unlock_irqrestore(&cmd->cmd_lock, flags);
3606 * It's normal to see 2 calls in this path:
3607 * 1) XFER Rdy completion + CMD_T_ABORT
3608 * 2) TCM TMR - drain_state_list
3610 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf016,
3611 "multiple abort. %p transport_state %x, t_state %x, "
3612 "se_cmd_flags %x\n", cmd, cmd->se_cmd.transport_state,
3613 cmd->se_cmd.t_state, cmd->se_cmd.se_cmd_flags);
3617 cmd->trc_flags |= TRC_ABORT;
3618 spin_unlock_irqrestore(&cmd->cmd_lock, flags);
3620 qlt_send_term_exchange(cmd->qpair, cmd, &cmd->atio, 0, 1);
3623 EXPORT_SYMBOL(qlt_abort_cmd);
3625 void qlt_free_cmd(struct qla_tgt_cmd *cmd)
3627 struct fc_port *sess = cmd->sess;
3629 ql_dbg(ql_dbg_tgt, cmd->vha, 0xe074,
3630 "%s: se_cmd[%p] ox_id %04x\n",
3631 __func__, &cmd->se_cmd,
3632 be16_to_cpu(cmd->atio.u.isp24.fcp_hdr.ox_id));
3634 BUG_ON(cmd->cmd_in_wq);
3637 qlt_unmap_sg(cmd->vha, cmd);
3640 qlt_decr_num_pend_cmds(cmd->vha);
3642 BUG_ON(cmd->sg_mapped);
3643 cmd->jiffies_at_free = get_jiffies_64();
3644 if (unlikely(cmd->free_sg))
3647 if (!sess || !sess->se_sess) {
3651 cmd->jiffies_at_free = get_jiffies_64();
3652 percpu_ida_free(&sess->se_sess->sess_tag_pool, cmd->se_cmd.map_tag);
3654 EXPORT_SYMBOL(qlt_free_cmd);
3657 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
3659 static int qlt_term_ctio_exchange(struct qla_qpair *qpair, void *ctio,
3660 struct qla_tgt_cmd *cmd, uint32_t status)
3663 struct scsi_qla_host *vha = qpair->vha;
3665 if (cmd->se_cmd.prot_op)
3666 ql_dbg(ql_dbg_tgt_dif, vha, 0xe013,
3667 "Term DIF cmd: lba[0x%llx|%lld] len[0x%x] "
3668 "se_cmd=%p tag[%x] op %#x/%s",
3670 cmd->num_blks, &cmd->se_cmd,
3671 cmd->atio.u.isp24.exchange_addr,
3672 cmd->se_cmd.prot_op,
3673 prot_op_str(cmd->se_cmd.prot_op));
3676 struct ctio7_from_24xx *c = (struct ctio7_from_24xx *)ctio;
3678 cpu_to_le16(OF_TERM_EXCH));
3683 qlt_term_ctio_exchange(qpair, ctio, cmd, status);
3689 /* ha->hardware_lock supposed to be held on entry */
3690 static struct qla_tgt_cmd *qlt_ctio_to_cmd(struct scsi_qla_host *vha,
3691 struct rsp_que *rsp, uint32_t handle, void *ctio)
3693 struct qla_tgt_cmd *cmd = NULL;
3694 struct req_que *req;
3695 int qid = GET_QID(handle);
3696 uint32_t h = handle & ~QLA_TGT_HANDLE_MASK;
3698 if (unlikely(h == QLA_TGT_SKIP_HANDLE))
3701 if (qid == rsp->req->id) {
3703 } else if (vha->hw->req_q_map[qid]) {
3704 ql_dbg(ql_dbg_tgt_mgt, vha, 0x1000a,
3705 "qla_target(%d): CTIO completion with different QID %d handle %x\n",
3706 vha->vp_idx, rsp->id, handle);
3707 req = vha->hw->req_q_map[qid];
3712 h &= QLA_CMD_HANDLE_MASK;
3714 if (h != QLA_TGT_NULL_HANDLE) {
3715 if (unlikely(h > req->num_outstanding_cmds)) {
3716 ql_dbg(ql_dbg_tgt, vha, 0xe052,
3717 "qla_target(%d): Wrong handle %x received\n",
3718 vha->vp_idx, handle);
3722 cmd = (struct qla_tgt_cmd *)req->outstanding_cmds[h];
3723 if (unlikely(cmd == NULL)) {
3724 ql_dbg(ql_dbg_async, vha, 0xe053,
3725 "qla_target(%d): Suspicious: unable to find the command with handle %x req->id %d rsp->id %d\n",
3726 vha->vp_idx, handle, req->id, rsp->id);
3729 req->outstanding_cmds[h] = NULL;
3730 } else if (ctio != NULL) {
3731 /* We can't get loop ID from CTIO7 */
3732 ql_dbg(ql_dbg_tgt, vha, 0xe054,
3733 "qla_target(%d): Wrong CTIO received: QLA24xx doesn't "
3734 "support NULL handles\n", vha->vp_idx);
3741 /* hardware_lock should be held by caller. */
3743 qlt_abort_cmd_on_host_reset(struct scsi_qla_host *vha, struct qla_tgt_cmd *cmd)
3745 struct qla_hw_data *ha = vha->hw;
3748 qlt_unmap_sg(vha, cmd);
3750 /* TODO: fix debug message type and ids. */
3751 if (cmd->state == QLA_TGT_STATE_PROCESSED) {
3752 ql_dbg(ql_dbg_io, vha, 0xff00,
3753 "HOST-ABORT: state=PROCESSED.\n");
3754 } else if (cmd->state == QLA_TGT_STATE_NEED_DATA) {
3755 cmd->write_data_transferred = 0;
3756 cmd->state = QLA_TGT_STATE_DATA_IN;
3758 ql_dbg(ql_dbg_io, vha, 0xff01,
3759 "HOST-ABORT: state=DATA_IN.\n");
3761 ha->tgt.tgt_ops->handle_data(cmd);
3764 ql_dbg(ql_dbg_io, vha, 0xff03,
3765 "HOST-ABORT: state=BAD(%d).\n",
3770 cmd->trc_flags |= TRC_FLUSH;
3771 ha->tgt.tgt_ops->free_cmd(cmd);
3775 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
3777 static void qlt_do_ctio_completion(struct scsi_qla_host *vha,
3778 struct rsp_que *rsp, uint32_t handle, uint32_t status, void *ctio)
3780 struct qla_hw_data *ha = vha->hw;
3781 struct se_cmd *se_cmd;
3782 struct qla_tgt_cmd *cmd;
3783 struct qla_qpair *qpair = rsp->qpair;
3785 if (handle & CTIO_INTERMEDIATE_HANDLE_MARK) {
3786 /* That could happen only in case of an error/reset/abort */
3787 if (status != CTIO_SUCCESS) {
3788 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01d,
3789 "Intermediate CTIO received"
3790 " (status %x)\n", status);
3795 cmd = qlt_ctio_to_cmd(vha, rsp, handle, ctio);
3799 se_cmd = &cmd->se_cmd;
3800 cmd->cmd_sent_to_fw = 0;
3802 qlt_unmap_sg(vha, cmd);
3804 if (unlikely(status != CTIO_SUCCESS)) {
3805 switch (status & 0xFFFF) {
3806 case CTIO_LIP_RESET:
3807 case CTIO_TARGET_RESET:
3809 /* driver request abort via Terminate exchange */
3811 case CTIO_INVALID_RX_ID:
3813 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf058,
3814 "qla_target(%d): CTIO with "
3815 "status %#x received, state %x, se_cmd %p, "
3816 "(LIP_RESET=e, ABORTED=2, TARGET_RESET=17, "
3817 "TIMEOUT=b, INVALID_RX_ID=8)\n", vha->vp_idx,
3818 status, cmd->state, se_cmd);
3821 case CTIO_PORT_LOGGED_OUT:
3822 case CTIO_PORT_UNAVAILABLE:
3825 (status & 0xFFFF) == CTIO_PORT_LOGGED_OUT;
3827 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf059,
3828 "qla_target(%d): CTIO with %s status %x "
3829 "received (state %x, se_cmd %p)\n", vha->vp_idx,
3830 logged_out ? "PORT LOGGED OUT" : "PORT UNAVAILABLE",
3831 status, cmd->state, se_cmd);
3833 if (logged_out && cmd->sess) {
3835 * Session is already logged out, but we need
3836 * to notify initiator, who's not aware of this
3838 cmd->sess->logout_on_delete = 0;
3839 cmd->sess->send_els_logo = 1;
3840 ql_dbg(ql_dbg_disc, vha, 0x20f8,
3841 "%s %d %8phC post del sess\n",
3842 __func__, __LINE__, cmd->sess->port_name);
3844 qlt_schedule_sess_for_deletion_lock(cmd->sess);
3848 case CTIO_DIF_ERROR: {
3849 struct ctio_crc_from_fw *crc =
3850 (struct ctio_crc_from_fw *)ctio;
3851 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf073,
3852 "qla_target(%d): CTIO with DIF_ERROR status %x "
3853 "received (state %x, ulp_cmd %p) actual_dif[0x%llx] "
3854 "expect_dif[0x%llx]\n",
3855 vha->vp_idx, status, cmd->state, se_cmd,
3856 *((u64 *)&crc->actual_dif[0]),
3857 *((u64 *)&crc->expected_dif[0]));
3859 qlt_handle_dif_error(vha, cmd, ctio);
3863 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05b,
3864 "qla_target(%d): CTIO with error status 0x%x received (state %x, se_cmd %p\n",
3865 vha->vp_idx, status, cmd->state, se_cmd);
3870 /* "cmd->aborted" means
3871 * cmd is already aborted/terminated, we don't
3872 * need to terminate again. The exchange is already
3873 * cleaned up/freed at FW level. Just cleanup at driver
3876 if ((cmd->state != QLA_TGT_STATE_NEED_DATA) &&
3878 cmd->trc_flags |= TRC_CTIO_ERR;
3879 if (qlt_term_ctio_exchange(qpair, ctio, cmd, status))
3884 if (cmd->state == QLA_TGT_STATE_PROCESSED) {
3885 cmd->trc_flags |= TRC_CTIO_DONE;
3886 } else if (cmd->state == QLA_TGT_STATE_NEED_DATA) {
3887 cmd->state = QLA_TGT_STATE_DATA_IN;
3889 if (status == CTIO_SUCCESS)
3890 cmd->write_data_transferred = 1;
3892 ha->tgt.tgt_ops->handle_data(cmd);
3894 } else if (cmd->aborted) {
3895 cmd->trc_flags |= TRC_CTIO_ABORTED;
3896 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01e,
3897 "Aborted command %p (tag %lld) finished\n", cmd, se_cmd->tag);
3899 cmd->trc_flags |= TRC_CTIO_STRANGE;
3900 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05c,
3901 "qla_target(%d): A command in state (%d) should "
3902 "not return a CTIO complete\n", vha->vp_idx, cmd->state);
3905 if (unlikely(status != CTIO_SUCCESS) &&
3907 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01f, "Finishing failed CTIO\n");
3911 ha->tgt.tgt_ops->free_cmd(cmd);
3914 static inline int qlt_get_fcp_task_attr(struct scsi_qla_host *vha,
3919 switch (task_codes) {
3920 case ATIO_SIMPLE_QUEUE:
3921 fcp_task_attr = TCM_SIMPLE_TAG;
3923 case ATIO_HEAD_OF_QUEUE:
3924 fcp_task_attr = TCM_HEAD_TAG;
3926 case ATIO_ORDERED_QUEUE:
3927 fcp_task_attr = TCM_ORDERED_TAG;
3929 case ATIO_ACA_QUEUE:
3930 fcp_task_attr = TCM_ACA_TAG;
3933 fcp_task_attr = TCM_SIMPLE_TAG;
3936 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05d,
3937 "qla_target: unknown task code %x, use ORDERED instead\n",
3939 fcp_task_attr = TCM_ORDERED_TAG;
3943 return fcp_task_attr;
3946 static struct fc_port *qlt_make_local_sess(struct scsi_qla_host *,
3949 * Process context for I/O path into tcm_qla2xxx code
3951 static void __qlt_do_work(struct qla_tgt_cmd *cmd)
3953 scsi_qla_host_t *vha = cmd->vha;
3954 struct qla_hw_data *ha = vha->hw;
3955 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
3956 struct fc_port *sess = cmd->sess;
3957 struct atio_from_isp *atio = &cmd->atio;
3959 unsigned long flags;
3960 uint32_t data_length;
3961 int ret, fcp_task_attr, data_dir, bidi = 0;
3962 struct qla_qpair *qpair = cmd->qpair;
3965 cmd->trc_flags |= TRC_DO_WORK;
3970 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf082,
3971 "cmd with tag %u is aborted\n",
3972 cmd->atio.u.isp24.exchange_addr);
3976 spin_lock_init(&cmd->cmd_lock);
3977 cdb = &atio->u.isp24.fcp_cmnd.cdb[0];
3978 cmd->se_cmd.tag = atio->u.isp24.exchange_addr;
3979 cmd->unpacked_lun = scsilun_to_int(
3980 (struct scsi_lun *)&atio->u.isp24.fcp_cmnd.lun);
3982 if (atio->u.isp24.fcp_cmnd.rddata &&
3983 atio->u.isp24.fcp_cmnd.wrdata) {
3985 data_dir = DMA_TO_DEVICE;
3986 } else if (atio->u.isp24.fcp_cmnd.rddata)
3987 data_dir = DMA_FROM_DEVICE;
3988 else if (atio->u.isp24.fcp_cmnd.wrdata)
3989 data_dir = DMA_TO_DEVICE;
3991 data_dir = DMA_NONE;
3993 fcp_task_attr = qlt_get_fcp_task_attr(vha,
3994 atio->u.isp24.fcp_cmnd.task_attr);
3995 data_length = be32_to_cpu(get_unaligned((uint32_t *)
3996 &atio->u.isp24.fcp_cmnd.add_cdb[
3997 atio->u.isp24.fcp_cmnd.add_cdb_len]));
3999 ret = ha->tgt.tgt_ops->handle_cmd(vha, cmd, cdb, data_length,
4000 fcp_task_attr, data_dir, bidi);
4004 * Drop extra session reference from qla_tgt_handle_cmd_for_atio*(
4006 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
4007 ha->tgt.tgt_ops->put_sess(sess);
4008 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
4012 ql_dbg(ql_dbg_io, vha, 0x3060, "Terminating work cmd %p", cmd);
4014 * cmd has not sent to target yet, so pass NULL as the second
4015 * argument to qlt_send_term_exchange() and free the memory here.
4017 cmd->trc_flags |= TRC_DO_WORK_ERR;
4018 spin_lock_irqsave(qpair->qp_lock_ptr, flags);
4019 qlt_send_term_exchange(qpair, NULL, &cmd->atio, 1, 0);
4021 qlt_decr_num_pend_cmds(vha);
4022 percpu_ida_free(&sess->se_sess->sess_tag_pool, cmd->se_cmd.map_tag);
4023 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
4025 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
4026 ha->tgt.tgt_ops->put_sess(sess);
4027 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
4030 static void qlt_do_work(struct work_struct *work)
4032 struct qla_tgt_cmd *cmd = container_of(work, struct qla_tgt_cmd, work);
4033 scsi_qla_host_t *vha = cmd->vha;
4034 unsigned long flags;
4036 spin_lock_irqsave(&vha->cmd_list_lock, flags);
4037 list_del(&cmd->cmd_list);
4038 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
4043 static struct qla_tgt_cmd *qlt_get_tag(scsi_qla_host_t *vha,
4044 struct fc_port *sess,
4045 struct atio_from_isp *atio)
4047 struct se_session *se_sess = sess->se_sess;
4048 struct qla_tgt_cmd *cmd;
4051 tag = percpu_ida_alloc(&se_sess->sess_tag_pool, TASK_RUNNING);
4055 cmd = &((struct qla_tgt_cmd *)se_sess->sess_cmd_map)[tag];
4056 memset(cmd, 0, sizeof(struct qla_tgt_cmd));
4057 cmd->cmd_type = TYPE_TGT_CMD;
4058 memcpy(&cmd->atio, atio, sizeof(*atio));
4059 cmd->state = QLA_TGT_STATE_NEW;
4060 cmd->tgt = vha->vha_tgt.qla_tgt;
4061 qlt_incr_num_pend_cmds(vha);
4063 cmd->se_cmd.map_tag = tag;
4065 cmd->loop_id = sess->loop_id;
4066 cmd->conf_compl_supported = sess->conf_compl_supported;
4069 cmd->jiffies_at_alloc = get_jiffies_64();
4071 cmd->reset_count = vha->hw->chip_reset;
4072 cmd->qpair = vha->hw->base_qpair;
4073 cmd->se_cmd.cpuid = cmd->qpair->cpuid;
4078 static void qlt_create_sess_from_atio(struct work_struct *work)
4080 struct qla_tgt_sess_op *op = container_of(work,
4081 struct qla_tgt_sess_op, work);
4082 scsi_qla_host_t *vha = op->vha;
4083 struct qla_hw_data *ha = vha->hw;
4084 struct fc_port *sess;
4085 struct qla_tgt_cmd *cmd;
4086 unsigned long flags;
4087 uint8_t *s_id = op->atio.u.isp24.fcp_hdr.s_id;
4089 spin_lock_irqsave(&vha->cmd_list_lock, flags);
4090 list_del(&op->cmd_list);
4091 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
4094 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf083,
4095 "sess_op with tag %u is aborted\n",
4096 op->atio.u.isp24.exchange_addr);
4100 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf022,
4101 "qla_target(%d): Unable to find wwn login"
4102 " (s_id %x:%x:%x), trying to create it manually\n",
4103 vha->vp_idx, s_id[0], s_id[1], s_id[2]);
4105 if (op->atio.u.raw.entry_count > 1) {
4106 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf023,
4107 "Dropping multy entry atio %p\n", &op->atio);
4111 sess = qlt_make_local_sess(vha, s_id);
4112 /* sess has an extra creation ref. */
4117 * Now obtain a pre-allocated session tag using the original op->atio
4118 * packet header, and dispatch into __qlt_do_work() using the existing
4121 cmd = qlt_get_tag(vha, sess, &op->atio);
4123 struct qla_qpair *qpair = ha->base_qpair;
4125 spin_lock_irqsave(qpair->qp_lock_ptr, flags);
4126 qlt_send_busy(qpair, &op->atio, SAM_STAT_BUSY);
4127 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
4129 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
4130 ha->tgt.tgt_ops->put_sess(sess);
4131 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
4137 * __qlt_do_work() will call qlt_put_sess() to release
4138 * the extra reference taken above by qlt_make_local_sess()
4144 qlt_send_term_exchange(vha->hw->base_qpair, NULL, &op->atio, 0, 0);
4148 /* ha->hardware_lock supposed to be held on entry */
4149 static int qlt_handle_cmd_for_atio(struct scsi_qla_host *vha,
4150 struct atio_from_isp *atio)
4152 struct qla_hw_data *ha = vha->hw;
4153 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
4154 struct fc_port *sess;
4155 struct qla_tgt_cmd *cmd;
4156 unsigned long flags;
4158 if (unlikely(tgt->tgt_stop)) {
4159 ql_dbg(ql_dbg_io, vha, 0x3061,
4160 "New command while device %p is shutting down\n", tgt);
4164 sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha, atio->u.isp24.fcp_hdr.s_id);
4165 if (unlikely(!sess)) {
4166 struct qla_tgt_sess_op *op = kzalloc(sizeof(struct qla_tgt_sess_op),
4171 memcpy(&op->atio, atio, sizeof(*atio));
4174 spin_lock_irqsave(&vha->cmd_list_lock, flags);
4175 list_add_tail(&op->cmd_list, &vha->qla_sess_op_cmd_list);
4176 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
4178 INIT_WORK(&op->work, qlt_create_sess_from_atio);
4179 queue_work(qla_tgt_wq, &op->work);
4183 /* Another WWN used to have our s_id. Our PLOGI scheduled its
4184 * session deletion, but it's still in sess_del_work wq */
4185 if (sess->deleted) {
4186 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf002,
4187 "New command while old session %p is being deleted\n",
4193 * Do kref_get() before returning + dropping qla_hw_data->hardware_lock.
4195 if (!kref_get_unless_zero(&sess->sess_kref)) {
4196 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf004,
4197 "%s: kref_get fail, %8phC oxid %x \n",
4198 __func__, sess->port_name,
4199 be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id));
4203 cmd = qlt_get_tag(vha, sess, atio);
4205 ql_dbg(ql_dbg_io, vha, 0x3062,
4206 "qla_target(%d): Allocation of cmd failed\n", vha->vp_idx);
4207 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
4208 ha->tgt.tgt_ops->put_sess(sess);
4209 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
4214 cmd->trc_flags |= TRC_NEW_CMD;
4216 spin_lock_irqsave(&vha->cmd_list_lock, flags);
4217 list_add_tail(&cmd->cmd_list, &vha->qla_cmd_list);
4218 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
4220 INIT_WORK(&cmd->work, qlt_do_work);
4221 if (ha->msix_count) {
4222 if (cmd->atio.u.isp24.fcp_cmnd.rddata)
4223 queue_work_on(smp_processor_id(), qla_tgt_wq,
4226 queue_work_on(cmd->se_cmd.cpuid, qla_tgt_wq,
4229 queue_work(qla_tgt_wq, &cmd->work);
4235 /* ha->hardware_lock supposed to be held on entry */
4236 static int qlt_issue_task_mgmt(struct fc_port *sess, u64 lun,
4237 int fn, void *iocb, int flags)
4239 struct scsi_qla_host *vha = sess->vha;
4240 struct qla_hw_data *ha = vha->hw;
4241 struct qla_tgt_mgmt_cmd *mcmd;
4242 struct atio_from_isp *a = (struct atio_from_isp *)iocb;
4245 mcmd = mempool_alloc(qla_tgt_mgmt_cmd_mempool, GFP_ATOMIC);
4247 ql_dbg(ql_dbg_tgt_tmr, vha, 0x10009,
4248 "qla_target(%d): Allocation of management "
4249 "command failed, some commands and their data could "
4250 "leak\n", vha->vp_idx);
4253 memset(mcmd, 0, sizeof(*mcmd));
4257 memcpy(&mcmd->orig_iocb.imm_ntfy, iocb,
4258 sizeof(mcmd->orig_iocb.imm_ntfy));
4260 mcmd->tmr_func = fn;
4261 mcmd->flags = flags;
4262 mcmd->reset_count = vha->hw->chip_reset;
4263 mcmd->qpair = ha->base_qpair;
4266 case QLA_TGT_LUN_RESET:
4267 abort_cmds_for_lun(vha, lun, a->u.isp24.fcp_hdr.s_id);
4271 res = ha->tgt.tgt_ops->handle_tmr(mcmd, lun, mcmd->tmr_func, 0);
4273 ql_dbg(ql_dbg_tgt_tmr, vha, 0x1000b,
4274 "qla_target(%d): tgt.tgt_ops->handle_tmr() failed: %d\n",
4275 sess->vha->vp_idx, res);
4276 mempool_free(mcmd, qla_tgt_mgmt_cmd_mempool);
4283 /* ha->hardware_lock supposed to be held on entry */
4284 static int qlt_handle_task_mgmt(struct scsi_qla_host *vha, void *iocb)
4286 struct atio_from_isp *a = (struct atio_from_isp *)iocb;
4287 struct qla_hw_data *ha = vha->hw;
4288 struct qla_tgt *tgt;
4289 struct fc_port *sess;
4292 unsigned long flags;
4294 tgt = vha->vha_tgt.qla_tgt;
4296 fn = a->u.isp24.fcp_cmnd.task_mgmt_flags;
4298 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
4299 sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha,
4300 a->u.isp24.fcp_hdr.s_id);
4301 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
4304 scsilun_to_int((struct scsi_lun *)&a->u.isp24.fcp_cmnd.lun);
4307 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf024,
4308 "qla_target(%d): task mgmt fn 0x%x for "
4309 "non-existant session\n", vha->vp_idx, fn);
4310 return qlt_sched_sess_work(tgt, QLA_TGT_SESS_WORK_TM, iocb,
4311 sizeof(struct atio_from_isp));
4317 return qlt_issue_task_mgmt(sess, unpacked_lun, fn, iocb, 0);
4320 /* ha->hardware_lock supposed to be held on entry */
4321 static int __qlt_abort_task(struct scsi_qla_host *vha,
4322 struct imm_ntfy_from_isp *iocb, struct fc_port *sess)
4324 struct atio_from_isp *a = (struct atio_from_isp *)iocb;
4325 struct qla_hw_data *ha = vha->hw;
4326 struct qla_tgt_mgmt_cmd *mcmd;
4330 mcmd = mempool_alloc(qla_tgt_mgmt_cmd_mempool, GFP_ATOMIC);
4332 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05f,
4333 "qla_target(%d): %s: Allocation of ABORT cmd failed\n",
4334 vha->vp_idx, __func__);
4337 memset(mcmd, 0, sizeof(*mcmd));
4340 memcpy(&mcmd->orig_iocb.imm_ntfy, iocb,
4341 sizeof(mcmd->orig_iocb.imm_ntfy));
4344 scsilun_to_int((struct scsi_lun *)&a->u.isp24.fcp_cmnd.lun);
4345 mcmd->reset_count = vha->hw->chip_reset;
4346 mcmd->tmr_func = QLA_TGT_2G_ABORT_TASK;
4347 mcmd->qpair = ha->base_qpair;
4349 rc = ha->tgt.tgt_ops->handle_tmr(mcmd, unpacked_lun, mcmd->tmr_func,
4350 le16_to_cpu(iocb->u.isp2x.seq_id));
4352 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf060,
4353 "qla_target(%d): tgt_ops->handle_tmr() failed: %d\n",
4355 mempool_free(mcmd, qla_tgt_mgmt_cmd_mempool);
4362 /* ha->hardware_lock supposed to be held on entry */
4363 static int qlt_abort_task(struct scsi_qla_host *vha,
4364 struct imm_ntfy_from_isp *iocb)
4366 struct qla_hw_data *ha = vha->hw;
4367 struct fc_port *sess;
4369 unsigned long flags;
4371 loop_id = GET_TARGET_ID(ha, (struct atio_from_isp *)iocb);
4373 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
4374 sess = ha->tgt.tgt_ops->find_sess_by_loop_id(vha, loop_id);
4375 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
4378 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf025,
4379 "qla_target(%d): task abort for unexisting "
4380 "session\n", vha->vp_idx);
4381 return qlt_sched_sess_work(vha->vha_tgt.qla_tgt,
4382 QLA_TGT_SESS_WORK_ABORT, iocb, sizeof(*iocb));
4385 return __qlt_abort_task(vha, iocb, sess);
4388 void qlt_logo_completion_handler(fc_port_t *fcport, int rc)
4390 if (rc != MBS_COMMAND_COMPLETE) {
4391 ql_dbg(ql_dbg_tgt_mgt, fcport->vha, 0xf093,
4392 "%s: se_sess %p / sess %p from"
4393 " port %8phC loop_id %#04x s_id %02x:%02x:%02x"
4394 " LOGO failed: %#x\n",
4398 fcport->port_name, fcport->loop_id,
4399 fcport->d_id.b.domain, fcport->d_id.b.area,
4400 fcport->d_id.b.al_pa, rc);
4403 fcport->logout_completed = 1;
4407 * ha->hardware_lock supposed to be held on entry (to protect tgt->sess_list)
4409 * Schedules sessions with matching port_id/loop_id but different wwn for
4410 * deletion. Returns existing session with matching wwn if present.
4414 qlt_find_sess_invalidate_other(scsi_qla_host_t *vha, uint64_t wwn,
4415 port_id_t port_id, uint16_t loop_id, struct fc_port **conflict_sess)
4417 struct fc_port *sess = NULL, *other_sess;
4420 *conflict_sess = NULL;
4422 list_for_each_entry(other_sess, &vha->vp_fcports, list) {
4424 other_wwn = wwn_to_u64(other_sess->port_name);
4426 if (wwn == other_wwn) {
4432 /* find other sess with nport_id collision */
4433 if (port_id.b24 == other_sess->d_id.b24) {
4434 if (loop_id != other_sess->loop_id) {
4435 ql_dbg(ql_dbg_tgt_tmr, vha, 0x1000c,
4436 "Invalidating sess %p loop_id %d wwn %llx.\n",
4437 other_sess, other_sess->loop_id, other_wwn);
4440 * logout_on_delete is set by default, but another
4441 * session that has the same s_id/loop_id combo
4442 * might have cleared it when requested this session
4443 * deletion, so don't touch it
4445 qlt_schedule_sess_for_deletion(other_sess, true);
4448 * Another wwn used to have our s_id/loop_id
4449 * kill the session, but don't free the loop_id
4451 ql_dbg(ql_dbg_tgt_tmr, vha, 0xf01b,
4452 "Invalidating sess %p loop_id %d wwn %llx.\n",
4453 other_sess, other_sess->loop_id, other_wwn);
4456 other_sess->keep_nport_handle = 1;
4457 *conflict_sess = other_sess;
4458 qlt_schedule_sess_for_deletion(other_sess,
4464 /* find other sess with nport handle collision */
4465 if ((loop_id == other_sess->loop_id) &&
4466 (loop_id != FC_NO_LOOP_ID)) {
4467 ql_dbg(ql_dbg_tgt_tmr, vha, 0x1000d,
4468 "Invalidating sess %p loop_id %d wwn %llx.\n",
4469 other_sess, other_sess->loop_id, other_wwn);
4471 /* Same loop_id but different s_id
4472 * Ok to kill and logout */
4473 qlt_schedule_sess_for_deletion(other_sess, true);
4480 /* Abort any commands for this s_id waiting on qla_tgt_wq workqueue */
4481 static int abort_cmds_for_s_id(struct scsi_qla_host *vha, port_id_t *s_id)
4483 struct qla_tgt_sess_op *op;
4484 struct qla_tgt_cmd *cmd;
4487 unsigned long flags;
4489 key = (((u32)s_id->b.domain << 16) |
4490 ((u32)s_id->b.area << 8) |
4491 ((u32)s_id->b.al_pa));
4493 spin_lock_irqsave(&vha->cmd_list_lock, flags);
4494 list_for_each_entry(op, &vha->qla_sess_op_cmd_list, cmd_list) {
4495 uint32_t op_key = sid_to_key(op->atio.u.isp24.fcp_hdr.s_id);
4497 if (op_key == key) {
4503 list_for_each_entry(op, &vha->unknown_atio_list, cmd_list) {
4504 uint32_t op_key = sid_to_key(op->atio.u.isp24.fcp_hdr.s_id);
4505 if (op_key == key) {
4511 list_for_each_entry(cmd, &vha->qla_cmd_list, cmd_list) {
4512 uint32_t cmd_key = sid_to_key(cmd->atio.u.isp24.fcp_hdr.s_id);
4513 if (cmd_key == key) {
4518 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
4524 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
4526 static int qlt_24xx_handle_els(struct scsi_qla_host *vha,
4527 struct imm_ntfy_from_isp *iocb)
4529 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
4530 struct qla_hw_data *ha = vha->hw;
4531 struct fc_port *sess = NULL, *conflict_sess = NULL;
4537 struct qlt_plogi_ack_t *pla;
4538 unsigned long flags;
4540 wwn = wwn_to_u64(iocb->u.isp24.port_name);
4542 port_id.b.domain = iocb->u.isp24.port_id[2];
4543 port_id.b.area = iocb->u.isp24.port_id[1];
4544 port_id.b.al_pa = iocb->u.isp24.port_id[0];
4545 port_id.b.rsvd_1 = 0;
4547 loop_id = le16_to_cpu(iocb->u.isp24.nport_handle);
4549 ql_dbg(ql_dbg_disc, vha, 0xf026,
4550 "qla_target(%d): Port ID: %02x:%02x:%02x ELS opcode: 0x%02x lid %d %8phC\n",
4551 vha->vp_idx, iocb->u.isp24.port_id[2],
4552 iocb->u.isp24.port_id[1], iocb->u.isp24.port_id[0],
4553 iocb->u.isp24.status_subcode, loop_id,
4554 iocb->u.isp24.port_name);
4556 /* res = 1 means ack at the end of thread
4557 * res = 0 means ack async/later.
4559 switch (iocb->u.isp24.status_subcode) {
4562 /* Mark all stale commands in qla_tgt_wq for deletion */
4563 abort_cmds_for_s_id(vha, &port_id);
4566 spin_lock_irqsave(&tgt->ha->tgt.sess_lock, flags);
4567 sess = qlt_find_sess_invalidate_other(vha, wwn,
4568 port_id, loop_id, &conflict_sess);
4569 spin_unlock_irqrestore(&tgt->ha->tgt.sess_lock, flags);
4572 if (IS_SW_RESV_ADDR(port_id)) {
4577 pla = qlt_plogi_ack_find_add(vha, &port_id, iocb);
4579 qlt_send_term_imm_notif(vha, iocb, 1);
4585 if (conflict_sess) {
4586 conflict_sess->login_gen++;
4587 qlt_plogi_ack_link(vha, pla, conflict_sess,
4588 QLT_PLOGI_LINK_CONFLICT);
4593 qla24xx_post_newsess_work(vha, &port_id,
4594 iocb->u.isp24.port_name, pla);
4599 qlt_plogi_ack_link(vha, pla, sess, QLT_PLOGI_LINK_SAME_WWN);
4600 sess->fw_login_state = DSC_LS_PLOGI_PEND;
4601 sess->d_id = port_id;
4604 switch (sess->disc_state) {
4606 qlt_plogi_ack_unref(vha, pla);
4611 * Under normal circumstances we want to release nport handle
4612 * during LOGO process to avoid nport handle leaks inside FW.
4613 * The exception is when LOGO is done while another PLOGI with
4614 * the same nport handle is waiting as might be the case here.
4615 * Note: there is always a possibily of a race where session
4616 * deletion has already started for other reasons (e.g. ACL
4617 * removal) and now PLOGI arrives:
4618 * 1. if PLOGI arrived in FW after nport handle has been freed,
4619 * FW must have assigned this PLOGI a new/same handle and we
4620 * can proceed ACK'ing it as usual when session deletion
4622 * 2. if PLOGI arrived in FW before LOGO with LCF_FREE_NPORT
4623 * bit reached it, the handle has now been released. We'll
4624 * get an error when we ACK this PLOGI. Nothing will be sent
4625 * back to initiator. Initiator should eventually retry
4626 * PLOGI and situation will correct itself.
4628 sess->keep_nport_handle = ((sess->loop_id == loop_id) &&
4629 (sess->d_id.b24 == port_id.b24));
4631 ql_dbg(ql_dbg_disc, vha, 0x20f9,
4632 "%s %d %8phC post del sess\n",
4633 __func__, __LINE__, sess->port_name);
4636 qlt_schedule_sess_for_deletion_lock(sess);
4643 wd3_lo = le16_to_cpu(iocb->u.isp24.u.prli.wd3_lo);
4646 spin_lock_irqsave(&tgt->ha->tgt.sess_lock, flags);
4647 sess = qlt_find_sess_invalidate_other(vha, wwn, port_id,
4648 loop_id, &conflict_sess);
4649 spin_unlock_irqrestore(&tgt->ha->tgt.sess_lock, flags);
4652 if (conflict_sess) {
4653 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf09b,
4654 "PRLI with conflicting sess %p port %8phC\n",
4655 conflict_sess, conflict_sess->port_name);
4656 qlt_send_term_imm_notif(vha, iocb, 1);
4662 if (sess->fw_login_state != DSC_LS_PLOGI_PEND &&
4663 sess->fw_login_state != DSC_LS_PLOGI_COMP) {
4665 * Impatient initiator sent PRLI before last
4666 * PLOGI could finish. Will force him to re-try,
4667 * while last one finishes.
4669 ql_log(ql_log_warn, sess->vha, 0xf095,
4670 "sess %p PRLI received, before plogi ack.\n",
4672 qlt_send_term_imm_notif(vha, iocb, 1);
4678 * This shouldn't happen under normal circumstances,
4679 * since we have deleted the old session during PLOGI
4681 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf096,
4682 "PRLI (loop_id %#04x) for existing sess %p (loop_id %#04x)\n",
4683 sess->loop_id, sess, iocb->u.isp24.nport_handle);
4686 sess->loop_id = loop_id;
4687 sess->d_id = port_id;
4688 sess->fw_login_state = DSC_LS_PRLI_PEND;
4691 sess->conf_compl_supported = 1;
4693 if ((wd3_lo & BIT_4) == 0)
4694 sess->port_type = FCT_INITIATOR;
4696 sess->port_type = FCT_TARGET;
4698 res = 1; /* send notify ack */
4700 /* Make session global (not used in fabric mode) */
4701 if (ha->current_topology != ISP_CFG_F) {
4703 ql_dbg(ql_dbg_disc, vha, 0x20fa,
4704 "%s %d %8phC post nack\n",
4705 __func__, __LINE__, sess->port_name);
4706 qla24xx_post_nack_work(vha, sess, iocb,
4710 set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
4711 set_bit(LOCAL_LOOP_UPDATE, &vha->dpc_flags);
4712 qla2xxx_wake_dpc(vha);
4716 ql_dbg(ql_dbg_disc, vha, 0x20fb,
4717 "%s %d %8phC post nack\n",
4718 __func__, __LINE__, sess->port_name);
4719 qla24xx_post_nack_work(vha, sess, iocb,
4727 if (le16_to_cpu(iocb->u.isp24.flags) &
4728 NOTIFY24XX_FLAGS_GLOBAL_TPRLO) {
4730 qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS);
4737 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
4738 sess = qla2x00_find_fcport_by_loopid(vha, loop_id);
4739 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
4743 sess->fw_login_state = DSC_LS_LOGO_PEND;
4744 sess->logo_ack_needed = 1;
4745 memcpy(sess->iocb, iocb, IOCB_SIZE);
4748 res = qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS_SESS);
4750 ql_dbg(ql_dbg_disc, vha, 0x20fc,
4751 "%s: logo %llx res %d sess %p ",
4752 __func__, wwn, res, sess);
4755 * cmd went upper layer, look for qlt_xmit_tm_rsp()
4756 * for LOGO_ACK & sess delete
4761 /* cmd did not go to upper layer. */
4763 qlt_schedule_sess_for_deletion_lock(sess);
4766 /* else logo will be ack */
4772 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
4773 if (tgt->link_reinit_iocb_pending) {
4774 qlt_send_notify_ack(ha->base_qpair,
4775 &tgt->link_reinit_iocb, 0, 0, 0, 0, 0, 0);
4776 tgt->link_reinit_iocb_pending = 0;
4779 sess = qla2x00_find_fcport_by_wwpn(vha,
4780 iocb->u.isp24.port_name, 1);
4782 ql_dbg(ql_dbg_disc, vha, 0x20fd,
4783 "sess %p lid %d|%d DS %d LS %d\n",
4784 sess, sess->loop_id, loop_id,
4785 sess->disc_state, sess->fw_login_state);
4788 res = 1; /* send notify ack */
4792 case ELS_FLOGI: /* should never happen */
4794 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf061,
4795 "qla_target(%d): Unsupported ELS command %x "
4796 "received\n", vha->vp_idx, iocb->u.isp24.status_subcode);
4797 res = qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS_SESS);
4805 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
4807 static void qlt_handle_imm_notify(struct scsi_qla_host *vha,
4808 struct imm_ntfy_from_isp *iocb)
4810 struct qla_hw_data *ha = vha->hw;
4811 uint32_t add_flags = 0;
4812 int send_notify_ack = 1;
4815 status = le16_to_cpu(iocb->u.isp2x.status);
4817 case IMM_NTFY_LIP_RESET:
4819 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf032,
4820 "qla_target(%d): LIP reset (loop %#x), subcode %x\n",
4821 vha->vp_idx, le16_to_cpu(iocb->u.isp24.nport_handle),
4822 iocb->u.isp24.status_subcode);
4824 if (qlt_reset(vha, iocb, QLA_TGT_ABORT_ALL) == 0)
4825 send_notify_ack = 0;
4829 case IMM_NTFY_LIP_LINK_REINIT:
4831 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
4832 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf033,
4833 "qla_target(%d): LINK REINIT (loop %#x, "
4834 "subcode %x)\n", vha->vp_idx,
4835 le16_to_cpu(iocb->u.isp24.nport_handle),
4836 iocb->u.isp24.status_subcode);
4837 if (tgt->link_reinit_iocb_pending) {
4838 qlt_send_notify_ack(ha->base_qpair,
4839 &tgt->link_reinit_iocb, 0, 0, 0, 0, 0, 0);
4841 memcpy(&tgt->link_reinit_iocb, iocb, sizeof(*iocb));
4842 tgt->link_reinit_iocb_pending = 1;
4844 * QLogic requires to wait after LINK REINIT for possible
4845 * PDISC or ADISC ELS commands
4847 send_notify_ack = 0;
4851 case IMM_NTFY_PORT_LOGOUT:
4852 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf034,
4853 "qla_target(%d): Port logout (loop "
4854 "%#x, subcode %x)\n", vha->vp_idx,
4855 le16_to_cpu(iocb->u.isp24.nport_handle),
4856 iocb->u.isp24.status_subcode);
4858 if (qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS_SESS) == 0)
4859 send_notify_ack = 0;
4860 /* The sessions will be cleared in the callback, if needed */
4863 case IMM_NTFY_GLBL_TPRLO:
4864 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf035,
4865 "qla_target(%d): Global TPRLO (%x)\n", vha->vp_idx, status);
4866 if (qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS) == 0)
4867 send_notify_ack = 0;
4868 /* The sessions will be cleared in the callback, if needed */
4871 case IMM_NTFY_PORT_CONFIG:
4872 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf036,
4873 "qla_target(%d): Port config changed (%x)\n", vha->vp_idx,
4875 if (qlt_reset(vha, iocb, QLA_TGT_ABORT_ALL) == 0)
4876 send_notify_ack = 0;
4877 /* The sessions will be cleared in the callback, if needed */
4880 case IMM_NTFY_GLBL_LOGO:
4881 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06a,
4882 "qla_target(%d): Link failure detected\n",
4884 /* I_T nexus loss */
4885 if (qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS) == 0)
4886 send_notify_ack = 0;
4889 case IMM_NTFY_IOCB_OVERFLOW:
4890 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06b,
4891 "qla_target(%d): Cannot provide requested "
4892 "capability (IOCB overflowed the immediate notify "
4893 "resource count)\n", vha->vp_idx);
4896 case IMM_NTFY_ABORT_TASK:
4897 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf037,
4898 "qla_target(%d): Abort Task (S %08x I %#x -> "
4899 "L %#x)\n", vha->vp_idx,
4900 le16_to_cpu(iocb->u.isp2x.seq_id),
4901 GET_TARGET_ID(ha, (struct atio_from_isp *)iocb),
4902 le16_to_cpu(iocb->u.isp2x.lun));
4903 if (qlt_abort_task(vha, iocb) == 0)
4904 send_notify_ack = 0;
4907 case IMM_NTFY_RESOURCE:
4908 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06c,
4909 "qla_target(%d): Out of resources, host %ld\n",
4910 vha->vp_idx, vha->host_no);
4913 case IMM_NTFY_MSG_RX:
4914 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf038,
4915 "qla_target(%d): Immediate notify task %x\n",
4916 vha->vp_idx, iocb->u.isp2x.task_flags);
4917 if (qlt_handle_task_mgmt(vha, iocb) == 0)
4918 send_notify_ack = 0;
4922 if (qlt_24xx_handle_els(vha, iocb) == 0)
4923 send_notify_ack = 0;
4926 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06d,
4927 "qla_target(%d): Received unknown immediate "
4928 "notify status %x\n", vha->vp_idx, status);
4932 if (send_notify_ack)
4933 qlt_send_notify_ack(ha->base_qpair, iocb, add_flags, 0, 0, 0,
4938 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
4939 * This function sends busy to ISP 2xxx or 24xx.
4941 static int __qlt_send_busy(struct qla_qpair *qpair,
4942 struct atio_from_isp *atio, uint16_t status)
4944 struct scsi_qla_host *vha = qpair->vha;
4945 struct ctio7_to_24xx *ctio24;
4946 struct qla_hw_data *ha = vha->hw;
4948 struct fc_port *sess = NULL;
4949 unsigned long flags;
4952 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
4953 sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha,
4954 atio->u.isp24.fcp_hdr.s_id);
4955 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
4957 qlt_send_term_exchange(qpair, NULL, atio, 1, 0);
4960 /* Sending marker isn't necessary, since we called from ISR */
4962 pkt = (request_t *)__qla2x00_alloc_iocbs(qpair, NULL);
4964 ql_dbg(ql_dbg_io, vha, 0x3063,
4965 "qla_target(%d): %s failed: unable to allocate "
4966 "request packet", vha->vp_idx, __func__);
4970 vha->tgt_counters.num_q_full_sent++;
4971 pkt->entry_count = 1;
4972 pkt->handle = QLA_TGT_SKIP_HANDLE | CTIO_COMPLETION_HANDLE_MARK;
4974 ctio24 = (struct ctio7_to_24xx *)pkt;
4975 ctio24->entry_type = CTIO_TYPE7;
4976 ctio24->nport_handle = sess->loop_id;
4977 ctio24->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
4978 ctio24->vp_index = vha->vp_idx;
4979 ctio24->initiator_id[0] = atio->u.isp24.fcp_hdr.s_id[2];
4980 ctio24->initiator_id[1] = atio->u.isp24.fcp_hdr.s_id[1];
4981 ctio24->initiator_id[2] = atio->u.isp24.fcp_hdr.s_id[0];
4982 ctio24->exchange_addr = atio->u.isp24.exchange_addr;
4983 temp = (atio->u.isp24.attr << 9) |
4984 CTIO7_FLAGS_STATUS_MODE_1 | CTIO7_FLAGS_SEND_STATUS |
4985 CTIO7_FLAGS_DONT_RET_CTIO;
4986 ctio24->u.status1.flags = cpu_to_le16(temp);
4988 * CTIO from fw w/o se_cmd doesn't provide enough info to retry it,
4989 * if the explicit conformation is used.
4991 ctio24->u.status1.ox_id = swab16(atio->u.isp24.fcp_hdr.ox_id);
4992 ctio24->u.status1.scsi_status = cpu_to_le16(status);
4993 /* Memory Barrier */
4995 qla2x00_start_iocbs(vha, qpair->req);
5000 * This routine is used to allocate a command for either a QFull condition
5001 * (ie reply SAM_STAT_BUSY) or to terminate an exchange that did not go
5005 qlt_alloc_qfull_cmd(struct scsi_qla_host *vha,
5006 struct atio_from_isp *atio, uint16_t status, int qfull)
5008 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
5009 struct qla_hw_data *ha = vha->hw;
5010 struct fc_port *sess;
5011 struct se_session *se_sess;
5012 struct qla_tgt_cmd *cmd;
5014 unsigned long flags;
5016 if (unlikely(tgt->tgt_stop)) {
5017 ql_dbg(ql_dbg_io, vha, 0x300a,
5018 "New command while device %p is shutting down\n", tgt);
5022 if ((vha->hw->tgt.num_qfull_cmds_alloc + 1) > MAX_QFULL_CMDS_ALLOC) {
5023 vha->hw->tgt.num_qfull_cmds_dropped++;
5024 if (vha->hw->tgt.num_qfull_cmds_dropped >
5025 vha->qla_stats.stat_max_qfull_cmds_dropped)
5026 vha->qla_stats.stat_max_qfull_cmds_dropped =
5027 vha->hw->tgt.num_qfull_cmds_dropped;
5029 ql_dbg(ql_dbg_io, vha, 0x3068,
5030 "qla_target(%d): %s: QFull CMD dropped[%d]\n",
5031 vha->vp_idx, __func__,
5032 vha->hw->tgt.num_qfull_cmds_dropped);
5034 qlt_chk_exch_leak_thresh_hold(vha);
5038 sess = ha->tgt.tgt_ops->find_sess_by_s_id
5039 (vha, atio->u.isp24.fcp_hdr.s_id);
5043 se_sess = sess->se_sess;
5045 tag = percpu_ida_alloc(&se_sess->sess_tag_pool, TASK_RUNNING);
5049 cmd = &((struct qla_tgt_cmd *)se_sess->sess_cmd_map)[tag];
5051 ql_dbg(ql_dbg_io, vha, 0x3009,
5052 "qla_target(%d): %s: Allocation of cmd failed\n",
5053 vha->vp_idx, __func__);
5055 vha->hw->tgt.num_qfull_cmds_dropped++;
5056 if (vha->hw->tgt.num_qfull_cmds_dropped >
5057 vha->qla_stats.stat_max_qfull_cmds_dropped)
5058 vha->qla_stats.stat_max_qfull_cmds_dropped =
5059 vha->hw->tgt.num_qfull_cmds_dropped;
5061 qlt_chk_exch_leak_thresh_hold(vha);
5065 memset(cmd, 0, sizeof(struct qla_tgt_cmd));
5067 qlt_incr_num_pend_cmds(vha);
5068 INIT_LIST_HEAD(&cmd->cmd_list);
5069 memcpy(&cmd->atio, atio, sizeof(*atio));
5071 cmd->tgt = vha->vha_tgt.qla_tgt;
5073 cmd->reset_count = vha->hw->chip_reset;
5075 cmd->qpair = ha->base_qpair;
5079 /* NOTE: borrowing the state field to carry the status */
5080 cmd->state = status;
5082 cmd->term_exchg = 1;
5084 spin_lock_irqsave(&vha->hw->tgt.q_full_lock, flags);
5085 list_add_tail(&cmd->cmd_list, &vha->hw->tgt.q_full_list);
5087 vha->hw->tgt.num_qfull_cmds_alloc++;
5088 if (vha->hw->tgt.num_qfull_cmds_alloc >
5089 vha->qla_stats.stat_max_qfull_cmds_alloc)
5090 vha->qla_stats.stat_max_qfull_cmds_alloc =
5091 vha->hw->tgt.num_qfull_cmds_alloc;
5092 spin_unlock_irqrestore(&vha->hw->tgt.q_full_lock, flags);
5096 qlt_free_qfull_cmds(struct qla_qpair *qpair)
5098 struct scsi_qla_host *vha = qpair->vha;
5099 struct qla_hw_data *ha = vha->hw;
5100 unsigned long flags;
5101 struct qla_tgt_cmd *cmd, *tcmd;
5102 struct list_head free_list, q_full_list;
5105 if (list_empty(&ha->tgt.q_full_list))
5108 INIT_LIST_HEAD(&free_list);
5109 INIT_LIST_HEAD(&q_full_list);
5111 spin_lock_irqsave(&vha->hw->tgt.q_full_lock, flags);
5112 if (list_empty(&ha->tgt.q_full_list)) {
5113 spin_unlock_irqrestore(&vha->hw->tgt.q_full_lock, flags);
5117 list_splice_init(&vha->hw->tgt.q_full_list, &q_full_list);
5118 spin_unlock_irqrestore(&vha->hw->tgt.q_full_lock, flags);
5120 spin_lock_irqsave(qpair->qp_lock_ptr, flags);
5121 list_for_each_entry_safe(cmd, tcmd, &q_full_list, cmd_list) {
5123 /* cmd->state is a borrowed field to hold status */
5124 rc = __qlt_send_busy(qpair, &cmd->atio, cmd->state);
5125 else if (cmd->term_exchg)
5126 rc = __qlt_send_term_exchange(qpair, NULL, &cmd->atio);
5132 ql_dbg(ql_dbg_io, vha, 0x3006,
5133 "%s: busy sent for ox_id[%04x]\n", __func__,
5134 be16_to_cpu(cmd->atio.u.isp24.fcp_hdr.ox_id));
5135 else if (cmd->term_exchg)
5136 ql_dbg(ql_dbg_io, vha, 0x3007,
5137 "%s: Term exchg sent for ox_id[%04x]\n", __func__,
5138 be16_to_cpu(cmd->atio.u.isp24.fcp_hdr.ox_id));
5140 ql_dbg(ql_dbg_io, vha, 0x3008,
5141 "%s: Unexpected cmd in QFull list %p\n", __func__,
5144 list_del(&cmd->cmd_list);
5145 list_add_tail(&cmd->cmd_list, &free_list);
5147 /* piggy back on hardware_lock for protection */
5148 vha->hw->tgt.num_qfull_cmds_alloc--;
5150 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
5154 list_for_each_entry_safe(cmd, tcmd, &free_list, cmd_list) {
5155 list_del(&cmd->cmd_list);
5156 /* This cmd was never sent to TCM. There is no need
5157 * to schedule free or call free_cmd
5162 if (!list_empty(&q_full_list)) {
5163 spin_lock_irqsave(&vha->hw->tgt.q_full_lock, flags);
5164 list_splice(&q_full_list, &vha->hw->tgt.q_full_list);
5165 spin_unlock_irqrestore(&vha->hw->tgt.q_full_lock, flags);
5172 qlt_send_busy(struct qla_qpair *qpair, struct atio_from_isp *atio,
5176 struct scsi_qla_host *vha = qpair->vha;
5178 rc = __qlt_send_busy(qpair, atio, status);
5180 qlt_alloc_qfull_cmd(vha, atio, status, 1);
5184 qlt_chk_qfull_thresh_hold(struct scsi_qla_host *vha, struct qla_qpair *qpair,
5185 struct atio_from_isp *atio, uint8_t ha_locked)
5187 struct qla_hw_data *ha = vha->hw;
5189 unsigned long flags;
5191 if (ha->tgt.num_pend_cmds < Q_FULL_THRESH_HOLD(ha))
5195 spin_lock_irqsave(&ha->hardware_lock, flags);
5196 status = temp_sam_status;
5197 qlt_send_busy(qpair, atio, status);
5199 spin_unlock_irqrestore(&ha->hardware_lock, flags);
5204 /* ha->hardware_lock supposed to be held on entry */
5205 /* called via callback from qla2xxx */
5206 static void qlt_24xx_atio_pkt(struct scsi_qla_host *vha,
5207 struct atio_from_isp *atio, uint8_t ha_locked)
5209 struct qla_hw_data *ha = vha->hw;
5210 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
5212 unsigned long flags;
5214 if (unlikely(tgt == NULL)) {
5215 ql_dbg(ql_dbg_tgt, vha, 0x3064,
5216 "ATIO pkt, but no tgt (ha %p)", ha);
5220 * In tgt_stop mode we also should allow all requests to pass.
5221 * Otherwise, some commands can stuck.
5224 tgt->atio_irq_cmd_count++;
5226 switch (atio->u.raw.entry_type) {
5228 if (unlikely(atio->u.isp24.exchange_addr ==
5229 ATIO_EXCHANGE_ADDRESS_UNKNOWN)) {
5230 ql_dbg(ql_dbg_io, vha, 0x3065,
5231 "qla_target(%d): ATIO_TYPE7 "
5232 "received with UNKNOWN exchange address, "
5233 "sending QUEUE_FULL\n", vha->vp_idx);
5235 spin_lock_irqsave(&ha->hardware_lock, flags);
5236 qlt_send_busy(ha->base_qpair, atio,
5237 SAM_STAT_TASK_SET_FULL);
5239 spin_unlock_irqrestore(&ha->hardware_lock,
5244 if (likely(atio->u.isp24.fcp_cmnd.task_mgmt_flags == 0)) {
5245 rc = qlt_chk_qfull_thresh_hold(vha, ha->base_qpair,
5248 tgt->atio_irq_cmd_count--;
5251 rc = qlt_handle_cmd_for_atio(vha, atio);
5253 rc = qlt_handle_task_mgmt(vha, atio);
5255 if (unlikely(rc != 0)) {
5258 spin_lock_irqsave(&ha->hardware_lock,
5261 #if 1 /* With TERM EXCHANGE some FC cards refuse to boot */
5262 qlt_send_busy(ha->base_qpair, atio,
5265 qlt_send_term_exchange(ha->base_qpair, NULL,
5269 spin_unlock_irqrestore(
5270 &ha->hardware_lock, flags);
5272 if (tgt->tgt_stop) {
5273 ql_dbg(ql_dbg_tgt, vha, 0xe059,
5274 "qla_target: Unable to send "
5275 "command to target for req, "
5278 ql_dbg(ql_dbg_tgt, vha, 0xe05a,
5279 "qla_target(%d): Unable to send "
5280 "command to target, sending BUSY "
5281 "status.\n", vha->vp_idx);
5284 &ha->hardware_lock, flags);
5285 qlt_send_busy(ha->base_qpair,
5286 atio, SAM_STAT_BUSY);
5288 spin_unlock_irqrestore(
5289 &ha->hardware_lock, flags);
5295 case IMMED_NOTIFY_TYPE:
5297 if (unlikely(atio->u.isp2x.entry_status != 0)) {
5298 ql_dbg(ql_dbg_tgt, vha, 0xe05b,
5299 "qla_target(%d): Received ATIO packet %x "
5300 "with error status %x\n", vha->vp_idx,
5301 atio->u.raw.entry_type,
5302 atio->u.isp2x.entry_status);
5305 ql_dbg(ql_dbg_tgt, vha, 0xe02e, "%s", "IMMED_NOTIFY ATIO");
5308 spin_lock_irqsave(&ha->hardware_lock, flags);
5309 qlt_handle_imm_notify(vha, (struct imm_ntfy_from_isp *)atio);
5311 spin_unlock_irqrestore(&ha->hardware_lock, flags);
5316 ql_dbg(ql_dbg_tgt, vha, 0xe05c,
5317 "qla_target(%d): Received unknown ATIO atio "
5318 "type %x\n", vha->vp_idx, atio->u.raw.entry_type);
5322 tgt->atio_irq_cmd_count--;
5325 /* ha->hardware_lock supposed to be held on entry */
5326 /* called via callback from qla2xxx */
5327 static void qlt_response_pkt(struct scsi_qla_host *vha,
5328 struct rsp_que *rsp, response_t *pkt)
5330 struct qla_hw_data *ha = vha->hw;
5331 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
5333 if (unlikely(tgt == NULL)) {
5334 ql_dbg(ql_dbg_tgt, vha, 0xe05d,
5335 "qla_target(%d): Response pkt %x received, but no "
5336 "tgt (ha %p)\n", vha->vp_idx, pkt->entry_type, ha);
5341 * In tgt_stop mode we also should allow all requests to pass.
5342 * Otherwise, some commands can stuck.
5345 switch (pkt->entry_type) {
5349 struct ctio7_from_24xx *entry = (struct ctio7_from_24xx *)pkt;
5350 qlt_do_ctio_completion(vha, rsp, entry->handle,
5351 le16_to_cpu(entry->status)|(pkt->entry_status << 16),
5356 case ACCEPT_TGT_IO_TYPE:
5358 struct atio_from_isp *atio = (struct atio_from_isp *)pkt;
5360 if (atio->u.isp2x.status !=
5361 cpu_to_le16(ATIO_CDB_VALID)) {
5362 ql_dbg(ql_dbg_tgt, vha, 0xe05e,
5363 "qla_target(%d): ATIO with error "
5364 "status %x received\n", vha->vp_idx,
5365 le16_to_cpu(atio->u.isp2x.status));
5369 rc = qlt_chk_qfull_thresh_hold(vha, rsp->qpair, atio, 1);
5373 rc = qlt_handle_cmd_for_atio(vha, atio);
5374 if (unlikely(rc != 0)) {
5376 #if 1 /* With TERM EXCHANGE some FC cards refuse to boot */
5377 qlt_send_busy(rsp->qpair, atio, 0);
5379 qlt_send_term_exchange(rsp->qpair, NULL, atio, 1, 0);
5382 if (tgt->tgt_stop) {
5383 ql_dbg(ql_dbg_tgt, vha, 0xe05f,
5384 "qla_target: Unable to send "
5385 "command to target, sending TERM "
5386 "EXCHANGE for rsp\n");
5387 qlt_send_term_exchange(rsp->qpair, NULL,
5390 ql_dbg(ql_dbg_tgt, vha, 0xe060,
5391 "qla_target(%d): Unable to send "
5392 "command to target, sending BUSY "
5393 "status\n", vha->vp_idx);
5394 qlt_send_busy(rsp->qpair, atio, 0);
5401 case CONTINUE_TGT_IO_TYPE:
5403 struct ctio_to_2xxx *entry = (struct ctio_to_2xxx *)pkt;
5404 qlt_do_ctio_completion(vha, rsp, entry->handle,
5405 le16_to_cpu(entry->status)|(pkt->entry_status << 16),
5412 struct ctio_to_2xxx *entry = (struct ctio_to_2xxx *)pkt;
5413 qlt_do_ctio_completion(vha, rsp, entry->handle,
5414 le16_to_cpu(entry->status)|(pkt->entry_status << 16),
5419 case IMMED_NOTIFY_TYPE:
5420 ql_dbg(ql_dbg_tgt, vha, 0xe035, "%s", "IMMED_NOTIFY\n");
5421 qlt_handle_imm_notify(vha, (struct imm_ntfy_from_isp *)pkt);
5424 case NOTIFY_ACK_TYPE:
5425 if (tgt->notify_ack_expected > 0) {
5426 struct nack_to_isp *entry = (struct nack_to_isp *)pkt;
5427 ql_dbg(ql_dbg_tgt, vha, 0xe036,
5428 "NOTIFY_ACK seq %08x status %x\n",
5429 le16_to_cpu(entry->u.isp2x.seq_id),
5430 le16_to_cpu(entry->u.isp2x.status));
5431 tgt->notify_ack_expected--;
5432 if (entry->u.isp2x.status !=
5433 cpu_to_le16(NOTIFY_ACK_SUCCESS)) {
5434 ql_dbg(ql_dbg_tgt, vha, 0xe061,
5435 "qla_target(%d): NOTIFY_ACK "
5436 "failed %x\n", vha->vp_idx,
5437 le16_to_cpu(entry->u.isp2x.status));
5440 ql_dbg(ql_dbg_tgt, vha, 0xe062,
5441 "qla_target(%d): Unexpected NOTIFY_ACK received\n",
5446 case ABTS_RECV_24XX:
5447 ql_dbg(ql_dbg_tgt, vha, 0xe037,
5448 "ABTS_RECV_24XX: instance %d\n", vha->vp_idx);
5449 qlt_24xx_handle_abts(vha, (struct abts_recv_from_24xx *)pkt);
5452 case ABTS_RESP_24XX:
5453 if (tgt->abts_resp_expected > 0) {
5454 struct abts_resp_from_24xx_fw *entry =
5455 (struct abts_resp_from_24xx_fw *)pkt;
5456 ql_dbg(ql_dbg_tgt, vha, 0xe038,
5457 "ABTS_RESP_24XX: compl_status %x\n",
5458 entry->compl_status);
5459 tgt->abts_resp_expected--;
5460 if (le16_to_cpu(entry->compl_status) !=
5461 ABTS_RESP_COMPL_SUCCESS) {
5462 if ((entry->error_subcode1 == 0x1E) &&
5463 (entry->error_subcode2 == 0)) {
5465 * We've got a race here: aborted
5466 * exchange not terminated, i.e.
5467 * response for the aborted command was
5468 * sent between the abort request was
5469 * received and processed.
5470 * Unfortunately, the firmware has a
5471 * silly requirement that all aborted
5472 * exchanges must be explicitely
5473 * terminated, otherwise it refuses to
5474 * send responses for the abort
5475 * requests. So, we have to
5476 * (re)terminate the exchange and retry
5477 * the abort response.
5479 qlt_24xx_retry_term_exchange(vha,
5482 ql_dbg(ql_dbg_tgt, vha, 0xe063,
5483 "qla_target(%d): ABTS_RESP_24XX "
5484 "failed %x (subcode %x:%x)",
5485 vha->vp_idx, entry->compl_status,
5486 entry->error_subcode1,
5487 entry->error_subcode2);
5490 ql_dbg(ql_dbg_tgt, vha, 0xe064,
5491 "qla_target(%d): Unexpected ABTS_RESP_24XX "
5492 "received\n", vha->vp_idx);
5497 ql_dbg(ql_dbg_tgt, vha, 0xe065,
5498 "qla_target(%d): Received unknown response pkt "
5499 "type %x\n", vha->vp_idx, pkt->entry_type);
5506 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
5508 void qlt_async_event(uint16_t code, struct scsi_qla_host *vha,
5511 struct qla_hw_data *ha = vha->hw;
5512 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
5515 if (!tgt || tgt->tgt_stop || tgt->tgt_stopped)
5518 if (((code == MBA_POINT_TO_POINT) || (code == MBA_CHG_IN_CONNECTION)) &&
5522 * In tgt_stop mode we also should allow all requests to pass.
5523 * Otherwise, some commands can stuck.
5528 case MBA_RESET: /* Reset */
5529 case MBA_SYSTEM_ERR: /* System Error */
5530 case MBA_REQ_TRANSFER_ERR: /* Request Transfer Error */
5531 case MBA_RSP_TRANSFER_ERR: /* Response Transfer Error */
5532 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03a,
5533 "qla_target(%d): System error async event %#x "
5534 "occurred", vha->vp_idx, code);
5536 case MBA_WAKEUP_THRES: /* Request Queue Wake-up. */
5537 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
5542 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03b,
5543 "qla_target(%d): Async LOOP_UP occurred "
5544 "(m[0]=%x, m[1]=%x, m[2]=%x, m[3]=%x)", vha->vp_idx,
5545 le16_to_cpu(mailbox[0]), le16_to_cpu(mailbox[1]),
5546 le16_to_cpu(mailbox[2]), le16_to_cpu(mailbox[3]));
5547 if (tgt->link_reinit_iocb_pending) {
5548 qlt_send_notify_ack(ha->base_qpair,
5549 (void *)&tgt->link_reinit_iocb,
5551 tgt->link_reinit_iocb_pending = 0;
5556 case MBA_LIP_OCCURRED:
5559 case MBA_RSCN_UPDATE:
5560 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03c,
5561 "qla_target(%d): Async event %#x occurred "
5562 "(m[0]=%x, m[1]=%x, m[2]=%x, m[3]=%x)", vha->vp_idx, code,
5563 le16_to_cpu(mailbox[0]), le16_to_cpu(mailbox[1]),
5564 le16_to_cpu(mailbox[2]), le16_to_cpu(mailbox[3]));
5567 case MBA_REJECTED_FCP_CMD:
5568 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf017,
5569 "qla_target(%d): Async event LS_REJECT occurred (m[0]=%x, m[1]=%x, m[2]=%x, m[3]=%x)",
5571 le16_to_cpu(mailbox[0]), le16_to_cpu(mailbox[1]),
5572 le16_to_cpu(mailbox[2]), le16_to_cpu(mailbox[3]));
5574 if (le16_to_cpu(mailbox[3]) == 1) {
5575 /* exchange starvation. */
5576 vha->hw->exch_starvation++;
5577 if (vha->hw->exch_starvation > 5) {
5578 ql_log(ql_log_warn, vha, 0xd03a,
5579 "Exchange starvation-. Resetting RISC\n");
5581 vha->hw->exch_starvation = 0;
5582 if (IS_P3P_TYPE(vha->hw))
5583 set_bit(FCOE_CTX_RESET_NEEDED,
5586 set_bit(ISP_ABORT_NEEDED,
5588 qla2xxx_wake_dpc(vha);
5593 case MBA_PORT_UPDATE:
5594 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03d,
5595 "qla_target(%d): Port update async event %#x "
5596 "occurred: updating the ports database (m[0]=%x, m[1]=%x, "
5597 "m[2]=%x, m[3]=%x)", vha->vp_idx, code,
5598 le16_to_cpu(mailbox[0]), le16_to_cpu(mailbox[1]),
5599 le16_to_cpu(mailbox[2]), le16_to_cpu(mailbox[3]));
5601 login_code = le16_to_cpu(mailbox[2]);
5602 if (login_code == 0x4) {
5603 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03e,
5604 "Async MB 2: Got PLOGI Complete\n");
5605 vha->hw->exch_starvation = 0;
5606 } else if (login_code == 0x7)
5607 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03f,
5608 "Async MB 2: Port Logged Out\n");
5616 static fc_port_t *qlt_get_port_database(struct scsi_qla_host *vha,
5619 fc_port_t *fcport, *tfcp, *del;
5621 unsigned long flags;
5624 fcport = kzalloc(sizeof(*fcport), GFP_KERNEL);
5626 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06f,
5627 "qla_target(%d): Allocation of tmp FC port failed",
5632 fcport->loop_id = loop_id;
5634 rc = qla24xx_gpdb_wait(vha, fcport, 0);
5635 if (rc != QLA_SUCCESS) {
5636 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf070,
5637 "qla_target(%d): Failed to retrieve fcport "
5638 "information -- get_port_database() returned %x "
5639 "(loop_id=0x%04x)", vha->vp_idx, rc, loop_id);
5645 spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
5646 tfcp = qla2x00_find_fcport_by_wwpn(vha, fcport->port_name, 1);
5649 tfcp->d_id = fcport->d_id;
5650 tfcp->port_type = fcport->port_type;
5651 tfcp->supported_classes = fcport->supported_classes;
5652 tfcp->flags |= fcport->flags;
5657 if (vha->hw->current_topology == ISP_CFG_F)
5658 fcport->flags |= FCF_FABRIC_DEVICE;
5660 list_add_tail(&fcport->list, &vha->vp_fcports);
5661 if (!IS_SW_RESV_ADDR(fcport->d_id))
5662 vha->fcport_count++;
5663 fcport->login_gen++;
5664 fcport->disc_state = DSC_LOGIN_COMPLETE;
5665 fcport->login_succ = 1;
5669 fcport->deleted = 0;
5670 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
5672 switch (vha->host->active_mode) {
5673 case MODE_INITIATOR:
5676 if (!IS_IIDMA_CAPABLE(vha->hw) || !vha->hw->flags.gpsc_supported) {
5677 ql_dbg(ql_dbg_disc, vha, 0x20fe,
5678 "%s %d %8phC post upd_fcport fcp_cnt %d\n",
5679 __func__, __LINE__, fcport->port_name, vha->fcport_count);
5680 qla24xx_post_upd_fcport_work(vha, fcport);
5682 ql_dbg(ql_dbg_disc, vha, 0x20ff,
5683 "%s %d %8phC post gpsc fcp_cnt %d\n",
5684 __func__, __LINE__, fcport->port_name, vha->fcport_count);
5685 qla24xx_post_gpsc_work(vha, fcport);
5695 qla2x00_free_fcport(del);
5700 /* Must be called under tgt_mutex */
5701 static struct fc_port *qlt_make_local_sess(struct scsi_qla_host *vha,
5704 struct fc_port *sess = NULL;
5705 fc_port_t *fcport = NULL;
5706 int rc, global_resets;
5707 uint16_t loop_id = 0;
5709 if ((s_id[0] == 0xFF) && (s_id[1] == 0xFC)) {
5711 * This is Domain Controller, so it should be
5712 * OK to drop SCSI commands from it.
5714 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf042,
5715 "Unable to find initiator with S_ID %x:%x:%x",
5716 s_id[0], s_id[1], s_id[2]);
5720 mutex_lock(&vha->vha_tgt.tgt_mutex);
5724 atomic_read(&vha->vha_tgt.qla_tgt->tgt_global_resets_count);
5726 rc = qla24xx_get_loop_id(vha, s_id, &loop_id);
5728 mutex_unlock(&vha->vha_tgt.tgt_mutex);
5730 ql_log(ql_log_info, vha, 0xf071,
5731 "qla_target(%d): Unable to find "
5732 "initiator with S_ID %x:%x:%x",
5733 vha->vp_idx, s_id[0], s_id[1],
5736 if (rc == -ENOENT) {
5737 qlt_port_logo_t logo;
5738 sid_to_portid(s_id, &logo.id);
5740 qlt_send_first_logo(vha, &logo);
5746 fcport = qlt_get_port_database(vha, loop_id);
5748 mutex_unlock(&vha->vha_tgt.tgt_mutex);
5752 if (global_resets !=
5753 atomic_read(&vha->vha_tgt.qla_tgt->tgt_global_resets_count)) {
5754 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf043,
5755 "qla_target(%d): global reset during session discovery "
5756 "(counter was %d, new %d), retrying", vha->vp_idx,
5758 atomic_read(&vha->vha_tgt.
5759 qla_tgt->tgt_global_resets_count));
5763 sess = qlt_create_sess(vha, fcport, true);
5765 mutex_unlock(&vha->vha_tgt.tgt_mutex);
5770 static void qlt_abort_work(struct qla_tgt *tgt,
5771 struct qla_tgt_sess_work_param *prm)
5773 struct scsi_qla_host *vha = tgt->vha;
5774 struct qla_hw_data *ha = vha->hw;
5775 struct fc_port *sess = NULL;
5776 unsigned long flags = 0, flags2 = 0;
5781 spin_lock_irqsave(&ha->tgt.sess_lock, flags2);
5786 s_id[0] = prm->abts.fcp_hdr_le.s_id[2];
5787 s_id[1] = prm->abts.fcp_hdr_le.s_id[1];
5788 s_id[2] = prm->abts.fcp_hdr_le.s_id[0];
5790 sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha,
5791 (unsigned char *)&be_s_id);
5793 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags2);
5795 sess = qlt_make_local_sess(vha, s_id);
5796 /* sess has got an extra creation ref */
5798 spin_lock_irqsave(&ha->tgt.sess_lock, flags2);
5802 if (sess->deleted) {
5807 if (!kref_get_unless_zero(&sess->sess_kref)) {
5808 ql_dbg(ql_dbg_tgt_tmr, vha, 0xf01c,
5809 "%s: kref_get fail %8phC \n",
5810 __func__, sess->port_name);
5816 rc = __qlt_24xx_handle_abts(vha, &prm->abts, sess);
5817 ha->tgt.tgt_ops->put_sess(sess);
5818 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags2);
5826 ha->tgt.tgt_ops->put_sess(sess);
5827 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags2);
5830 spin_lock_irqsave(&ha->hardware_lock, flags);
5831 qlt_24xx_send_abts_resp(ha->base_qpair, &prm->abts,
5832 FCP_TMF_REJECTED, false);
5833 spin_unlock_irqrestore(&ha->hardware_lock, flags);
5836 static void qlt_tmr_work(struct qla_tgt *tgt,
5837 struct qla_tgt_sess_work_param *prm)
5839 struct atio_from_isp *a = &prm->tm_iocb2;
5840 struct scsi_qla_host *vha = tgt->vha;
5841 struct qla_hw_data *ha = vha->hw;
5842 struct fc_port *sess = NULL;
5843 unsigned long flags;
5844 uint8_t *s_id = NULL; /* to hide compiler warnings */
5850 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
5855 s_id = prm->tm_iocb2.u.isp24.fcp_hdr.s_id;
5856 sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha, s_id);
5858 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
5860 sess = qlt_make_local_sess(vha, s_id);
5861 /* sess has got an extra creation ref */
5863 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
5867 if (sess->deleted) {
5872 if (!kref_get_unless_zero(&sess->sess_kref)) {
5873 ql_dbg(ql_dbg_tgt_tmr, vha, 0xf020,
5874 "%s: kref_get fail %8phC\n",
5875 __func__, sess->port_name);
5882 fn = a->u.isp24.fcp_cmnd.task_mgmt_flags;
5884 scsilun_to_int((struct scsi_lun *)&a->u.isp24.fcp_cmnd.lun);
5886 rc = qlt_issue_task_mgmt(sess, unpacked_lun, fn, iocb, 0);
5887 ha->tgt.tgt_ops->put_sess(sess);
5888 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
5896 ha->tgt.tgt_ops->put_sess(sess);
5897 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
5899 qlt_send_term_exchange(ha->base_qpair, NULL, &prm->tm_iocb2, 1, 0);
5902 static void qlt_sess_work_fn(struct work_struct *work)
5904 struct qla_tgt *tgt = container_of(work, struct qla_tgt, sess_work);
5905 struct scsi_qla_host *vha = tgt->vha;
5906 unsigned long flags;
5908 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf000, "Sess work (tgt %p)", tgt);
5910 spin_lock_irqsave(&tgt->sess_work_lock, flags);
5911 while (!list_empty(&tgt->sess_works_list)) {
5912 struct qla_tgt_sess_work_param *prm = list_entry(
5913 tgt->sess_works_list.next, typeof(*prm),
5914 sess_works_list_entry);
5917 * This work can be scheduled on several CPUs at time, so we
5918 * must delete the entry to eliminate double processing
5920 list_del(&prm->sess_works_list_entry);
5922 spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
5924 switch (prm->type) {
5925 case QLA_TGT_SESS_WORK_ABORT:
5926 qlt_abort_work(tgt, prm);
5928 case QLA_TGT_SESS_WORK_TM:
5929 qlt_tmr_work(tgt, prm);
5936 spin_lock_irqsave(&tgt->sess_work_lock, flags);
5940 spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
5943 /* Must be called under tgt_host_action_mutex */
5944 int qlt_add_target(struct qla_hw_data *ha, struct scsi_qla_host *base_vha)
5946 struct qla_tgt *tgt;
5948 if (!QLA_TGT_MODE_ENABLED())
5951 if (!IS_TGT_MODE_CAPABLE(ha)) {
5952 ql_log(ql_log_warn, base_vha, 0xe070,
5953 "This adapter does not support target mode.\n");
5957 ql_dbg(ql_dbg_tgt, base_vha, 0xe03b,
5958 "Registering target for host %ld(%p).\n", base_vha->host_no, ha);
5960 BUG_ON(base_vha->vha_tgt.qla_tgt != NULL);
5962 tgt = kzalloc(sizeof(struct qla_tgt), GFP_KERNEL);
5964 ql_dbg(ql_dbg_tgt, base_vha, 0xe066,
5965 "Unable to allocate struct qla_tgt\n");
5969 if (!(base_vha->host->hostt->supported_mode & MODE_TARGET))
5970 base_vha->host->hostt->supported_mode |= MODE_TARGET;
5973 tgt->vha = base_vha;
5974 init_waitqueue_head(&tgt->waitQ);
5975 INIT_LIST_HEAD(&tgt->del_sess_list);
5976 spin_lock_init(&tgt->sess_work_lock);
5977 INIT_WORK(&tgt->sess_work, qlt_sess_work_fn);
5978 INIT_LIST_HEAD(&tgt->sess_works_list);
5979 atomic_set(&tgt->tgt_global_resets_count, 0);
5981 base_vha->vha_tgt.qla_tgt = tgt;
5983 ql_dbg(ql_dbg_tgt, base_vha, 0xe067,
5984 "qla_target(%d): using 64 Bit PCI addressing",
5986 tgt->tgt_enable_64bit_addr = 1;
5988 tgt->sg_tablesize = QLA_TGT_MAX_SG_24XX(base_vha->req->length - 3);
5989 tgt->datasegs_per_cmd = QLA_TGT_DATASEGS_PER_CMD_24XX;
5990 tgt->datasegs_per_cont = QLA_TGT_DATASEGS_PER_CONT_24XX;
5992 mutex_lock(&qla_tgt_mutex);
5993 list_add_tail(&tgt->tgt_list_entry, &qla_tgt_glist);
5994 mutex_unlock(&qla_tgt_mutex);
5996 if (ha->tgt.tgt_ops && ha->tgt.tgt_ops->add_target)
5997 ha->tgt.tgt_ops->add_target(base_vha);
6002 /* Must be called under tgt_host_action_mutex */
6003 int qlt_remove_target(struct qla_hw_data *ha, struct scsi_qla_host *vha)
6005 if (!vha->vha_tgt.qla_tgt)
6008 if (vha->fc_vport) {
6009 qlt_release(vha->vha_tgt.qla_tgt);
6013 /* free left over qfull cmds */
6014 qlt_init_term_exchange(vha);
6016 mutex_lock(&qla_tgt_mutex);
6017 list_del(&vha->vha_tgt.qla_tgt->tgt_list_entry);
6018 mutex_unlock(&qla_tgt_mutex);
6020 ql_dbg(ql_dbg_tgt, vha, 0xe03c, "Unregistering target for host %ld(%p)",
6022 qlt_release(vha->vha_tgt.qla_tgt);
6027 void qlt_remove_target_resources(struct qla_hw_data *ha)
6029 struct scsi_qla_host *node;
6032 btree_for_each_safe32(&ha->tgt.host_map, key, node)
6033 btree_remove32(&ha->tgt.host_map, key);
6035 btree_destroy32(&ha->tgt.host_map);
6038 static void qlt_lport_dump(struct scsi_qla_host *vha, u64 wwpn,
6043 pr_debug("qla2xxx HW vha->node_name: ");
6044 for (i = 0; i < WWN_SIZE; i++)
6045 pr_debug("%02x ", vha->node_name[i]);
6047 pr_debug("qla2xxx HW vha->port_name: ");
6048 for (i = 0; i < WWN_SIZE; i++)
6049 pr_debug("%02x ", vha->port_name[i]);
6052 pr_debug("qla2xxx passed configfs WWPN: ");
6053 put_unaligned_be64(wwpn, b);
6054 for (i = 0; i < WWN_SIZE; i++)
6055 pr_debug("%02x ", b[i]);
6060 * qla_tgt_lport_register - register lport with external module
6062 * @qla_tgt_ops: Pointer for tcm_qla2xxx qla_tgt_ops
6063 * @wwpn: Passwd FC target WWPN
6064 * @callback: lport initialization callback for tcm_qla2xxx code
6065 * @target_lport_ptr: pointer for tcm_qla2xxx specific lport data
6067 int qlt_lport_register(void *target_lport_ptr, u64 phys_wwpn,
6068 u64 npiv_wwpn, u64 npiv_wwnn,
6069 int (*callback)(struct scsi_qla_host *, void *, u64, u64))
6071 struct qla_tgt *tgt;
6072 struct scsi_qla_host *vha;
6073 struct qla_hw_data *ha;
6074 struct Scsi_Host *host;
6075 unsigned long flags;
6079 mutex_lock(&qla_tgt_mutex);
6080 list_for_each_entry(tgt, &qla_tgt_glist, tgt_list_entry) {
6088 if (!(host->hostt->supported_mode & MODE_TARGET))
6091 spin_lock_irqsave(&ha->hardware_lock, flags);
6092 if ((!npiv_wwpn || !npiv_wwnn) && host->active_mode & MODE_TARGET) {
6093 pr_debug("MODE_TARGET already active on qla2xxx(%d)\n",
6095 spin_unlock_irqrestore(&ha->hardware_lock, flags);
6098 if (tgt->tgt_stop) {
6099 pr_debug("MODE_TARGET in shutdown on qla2xxx(%d)\n",
6101 spin_unlock_irqrestore(&ha->hardware_lock, flags);
6104 spin_unlock_irqrestore(&ha->hardware_lock, flags);
6106 if (!scsi_host_get(host)) {
6107 ql_dbg(ql_dbg_tgt, vha, 0xe068,
6108 "Unable to scsi_host_get() for"
6109 " qla2xxx scsi_host\n");
6112 qlt_lport_dump(vha, phys_wwpn, b);
6114 if (memcmp(vha->port_name, b, WWN_SIZE)) {
6115 scsi_host_put(host);
6118 rc = (*callback)(vha, target_lport_ptr, npiv_wwpn, npiv_wwnn);
6120 scsi_host_put(host);
6122 mutex_unlock(&qla_tgt_mutex);
6125 mutex_unlock(&qla_tgt_mutex);
6129 EXPORT_SYMBOL(qlt_lport_register);
6132 * qla_tgt_lport_deregister - Degister lport
6134 * @vha: Registered scsi_qla_host pointer
6136 void qlt_lport_deregister(struct scsi_qla_host *vha)
6138 struct qla_hw_data *ha = vha->hw;
6139 struct Scsi_Host *sh = vha->host;
6141 * Clear the target_lport_ptr qla_target_template pointer in qla_hw_data
6143 vha->vha_tgt.target_lport_ptr = NULL;
6144 ha->tgt.tgt_ops = NULL;
6146 * Release the Scsi_Host reference for the underlying qla2xxx host
6150 EXPORT_SYMBOL(qlt_lport_deregister);
6152 /* Must be called under HW lock */
6153 static void qlt_set_mode(struct scsi_qla_host *vha)
6155 switch (ql2x_ini_mode) {
6156 case QLA2XXX_INI_MODE_DISABLED:
6157 case QLA2XXX_INI_MODE_EXCLUSIVE:
6158 vha->host->active_mode = MODE_TARGET;
6160 case QLA2XXX_INI_MODE_ENABLED:
6161 vha->host->active_mode = MODE_UNKNOWN;
6163 case QLA2XXX_INI_MODE_DUAL:
6164 vha->host->active_mode = MODE_DUAL;
6171 /* Must be called under HW lock */
6172 static void qlt_clear_mode(struct scsi_qla_host *vha)
6174 switch (ql2x_ini_mode) {
6175 case QLA2XXX_INI_MODE_DISABLED:
6176 vha->host->active_mode = MODE_UNKNOWN;
6178 case QLA2XXX_INI_MODE_EXCLUSIVE:
6179 vha->host->active_mode = MODE_INITIATOR;
6181 case QLA2XXX_INI_MODE_ENABLED:
6182 case QLA2XXX_INI_MODE_DUAL:
6183 vha->host->active_mode = MODE_INITIATOR;
6191 * qla_tgt_enable_vha - NO LOCK HELD
6193 * host_reset, bring up w/ Target Mode Enabled
6196 qlt_enable_vha(struct scsi_qla_host *vha)
6198 struct qla_hw_data *ha = vha->hw;
6199 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
6200 unsigned long flags;
6201 scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
6204 ql_dbg(ql_dbg_tgt, vha, 0xe069,
6205 "Unable to locate qla_tgt pointer from"
6206 " struct qla_hw_data\n");
6211 spin_lock_irqsave(&ha->hardware_lock, flags);
6212 tgt->tgt_stopped = 0;
6214 spin_unlock_irqrestore(&ha->hardware_lock, flags);
6217 qla24xx_disable_vp(vha);
6218 qla24xx_enable_vp(vha);
6220 set_bit(ISP_ABORT_NEEDED, &base_vha->dpc_flags);
6221 qla2xxx_wake_dpc(base_vha);
6222 qla2x00_wait_for_hba_online(base_vha);
6225 EXPORT_SYMBOL(qlt_enable_vha);
6228 * qla_tgt_disable_vha - NO LOCK HELD
6230 * Disable Target Mode and reset the adapter
6232 static void qlt_disable_vha(struct scsi_qla_host *vha)
6234 struct qla_hw_data *ha = vha->hw;
6235 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
6236 unsigned long flags;
6239 ql_dbg(ql_dbg_tgt, vha, 0xe06a,
6240 "Unable to locate qla_tgt pointer from"
6241 " struct qla_hw_data\n");
6246 spin_lock_irqsave(&ha->hardware_lock, flags);
6247 qlt_clear_mode(vha);
6248 spin_unlock_irqrestore(&ha->hardware_lock, flags);
6250 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
6251 qla2xxx_wake_dpc(vha);
6252 qla2x00_wait_for_hba_online(vha);
6256 * Called from qla_init.c:qla24xx_vport_create() contex to setup
6257 * the target mode specific struct scsi_qla_host and struct qla_hw_data
6261 qlt_vport_create(struct scsi_qla_host *vha, struct qla_hw_data *ha)
6263 vha->vha_tgt.qla_tgt = NULL;
6265 mutex_init(&vha->vha_tgt.tgt_mutex);
6266 mutex_init(&vha->vha_tgt.tgt_host_action_mutex);
6268 qlt_clear_mode(vha);
6271 * NOTE: Currently the value is kept the same for <24xx and
6272 * >=24xx ISPs. If it is necessary to change it,
6273 * the check should be added for specific ISPs,
6274 * assigning the value appropriately.
6276 ha->tgt.atio_q_length = ATIO_ENTRY_CNT_24XX;
6278 qlt_add_target(ha, vha);
6282 qlt_rff_id(struct scsi_qla_host *vha, struct ct_sns_req *ct_req)
6285 * FC-4 Feature bit 0 indicates target functionality to the name server.
6287 if (qla_tgt_mode_enabled(vha)) {
6288 ct_req->req.rff_id.fc4_feature = BIT_0;
6289 } else if (qla_ini_mode_enabled(vha)) {
6290 ct_req->req.rff_id.fc4_feature = BIT_1;
6291 } else if (qla_dual_mode_enabled(vha))
6292 ct_req->req.rff_id.fc4_feature = BIT_0 | BIT_1;
6296 * qlt_init_atio_q_entries() - Initializes ATIO queue entries.
6299 * Beginning of ATIO ring has initialization control block already built
6300 * by nvram config routine.
6302 * Returns 0 on success.
6305 qlt_init_atio_q_entries(struct scsi_qla_host *vha)
6307 struct qla_hw_data *ha = vha->hw;
6309 struct atio_from_isp *pkt = (struct atio_from_isp *)ha->tgt.atio_ring;
6311 if (qla_ini_mode_enabled(vha))
6314 for (cnt = 0; cnt < ha->tgt.atio_q_length; cnt++) {
6315 pkt->u.raw.signature = ATIO_PROCESSED;
6322 * qlt_24xx_process_atio_queue() - Process ATIO queue entries.
6323 * @ha: SCSI driver HA context
6326 qlt_24xx_process_atio_queue(struct scsi_qla_host *vha, uint8_t ha_locked)
6328 struct qla_hw_data *ha = vha->hw;
6329 struct atio_from_isp *pkt;
6332 if (!ha->flags.fw_started)
6335 while ((ha->tgt.atio_ring_ptr->signature != ATIO_PROCESSED) ||
6336 fcpcmd_is_corrupted(ha->tgt.atio_ring_ptr)) {
6337 pkt = (struct atio_from_isp *)ha->tgt.atio_ring_ptr;
6338 cnt = pkt->u.raw.entry_count;
6340 if (unlikely(fcpcmd_is_corrupted(ha->tgt.atio_ring_ptr))) {
6342 * This packet is corrupted. The header + payload
6343 * can not be trusted. There is no point in passing
6346 ql_log(ql_log_warn, vha, 0xd03c,
6347 "corrupted fcp frame SID[%3phN] OXID[%04x] EXCG[%x] %64phN\n",
6348 pkt->u.isp24.fcp_hdr.s_id,
6349 be16_to_cpu(pkt->u.isp24.fcp_hdr.ox_id),
6350 le32_to_cpu(pkt->u.isp24.exchange_addr), pkt);
6352 adjust_corrupted_atio(pkt);
6353 qlt_send_term_exchange(ha->base_qpair, NULL, pkt,
6356 qlt_24xx_atio_pkt_all_vps(vha,
6357 (struct atio_from_isp *)pkt, ha_locked);
6360 for (i = 0; i < cnt; i++) {
6361 ha->tgt.atio_ring_index++;
6362 if (ha->tgt.atio_ring_index == ha->tgt.atio_q_length) {
6363 ha->tgt.atio_ring_index = 0;
6364 ha->tgt.atio_ring_ptr = ha->tgt.atio_ring;
6366 ha->tgt.atio_ring_ptr++;
6368 pkt->u.raw.signature = ATIO_PROCESSED;
6369 pkt = (struct atio_from_isp *)ha->tgt.atio_ring_ptr;
6374 /* Adjust ring index */
6375 WRT_REG_DWORD(ISP_ATIO_Q_OUT(vha), ha->tgt.atio_ring_index);
6376 RD_REG_DWORD_RELAXED(ISP_ATIO_Q_OUT(vha));
6380 qlt_24xx_config_rings(struct scsi_qla_host *vha)
6382 struct qla_hw_data *ha = vha->hw;
6383 if (!QLA_TGT_MODE_ENABLED())
6386 WRT_REG_DWORD(ISP_ATIO_Q_IN(vha), 0);
6387 WRT_REG_DWORD(ISP_ATIO_Q_OUT(vha), 0);
6388 RD_REG_DWORD(ISP_ATIO_Q_OUT(vha));
6390 if (IS_ATIO_MSIX_CAPABLE(ha)) {
6391 struct qla_msix_entry *msix = &ha->msix_entries[2];
6392 struct init_cb_24xx *icb = (struct init_cb_24xx *)ha->init_cb;
6394 icb->msix_atio = cpu_to_le16(msix->entry);
6395 ql_dbg(ql_dbg_init, vha, 0xf072,
6396 "Registering ICB vector 0x%x for atio que.\n",
6402 qlt_24xx_config_nvram_stage1(struct scsi_qla_host *vha, struct nvram_24xx *nv)
6404 struct qla_hw_data *ha = vha->hw;
6406 if (!QLA_TGT_MODE_ENABLED())
6409 if (qla_tgt_mode_enabled(vha) || qla_dual_mode_enabled(vha)) {
6410 if (!ha->tgt.saved_set) {
6411 /* We save only once */
6412 ha->tgt.saved_exchange_count = nv->exchange_count;
6413 ha->tgt.saved_firmware_options_1 =
6414 nv->firmware_options_1;
6415 ha->tgt.saved_firmware_options_2 =
6416 nv->firmware_options_2;
6417 ha->tgt.saved_firmware_options_3 =
6418 nv->firmware_options_3;
6419 ha->tgt.saved_set = 1;
6422 if (qla_tgt_mode_enabled(vha))
6423 nv->exchange_count = cpu_to_le16(0xFFFF);
6425 nv->exchange_count = cpu_to_le16(ql2xexchoffld);
6427 /* Enable target mode */
6428 nv->firmware_options_1 |= cpu_to_le32(BIT_4);
6430 /* Disable ini mode, if requested */
6431 if (qla_tgt_mode_enabled(vha))
6432 nv->firmware_options_1 |= cpu_to_le32(BIT_5);
6434 /* Disable Full Login after LIP */
6435 nv->firmware_options_1 &= cpu_to_le32(~BIT_13);
6436 /* Enable initial LIP */
6437 nv->firmware_options_1 &= cpu_to_le32(~BIT_9);
6438 if (ql2xtgt_tape_enable)
6439 /* Enable FC Tape support */
6440 nv->firmware_options_2 |= cpu_to_le32(BIT_12);
6442 /* Disable FC Tape support */
6443 nv->firmware_options_2 &= cpu_to_le32(~BIT_12);
6445 /* Disable Full Login after LIP */
6446 nv->host_p &= cpu_to_le32(~BIT_10);
6449 * clear BIT 15 explicitly as we have seen at least
6450 * a couple of instances where this was set and this
6451 * was causing the firmware to not be initialized.
6453 nv->firmware_options_1 &= cpu_to_le32(~BIT_15);
6454 /* Enable target PRLI control */
6455 nv->firmware_options_2 |= cpu_to_le32(BIT_14);
6457 if (ha->tgt.saved_set) {
6458 nv->exchange_count = ha->tgt.saved_exchange_count;
6459 nv->firmware_options_1 =
6460 ha->tgt.saved_firmware_options_1;
6461 nv->firmware_options_2 =
6462 ha->tgt.saved_firmware_options_2;
6463 nv->firmware_options_3 =
6464 ha->tgt.saved_firmware_options_3;
6469 if (ha->tgt.enable_class_2) {
6470 if (vha->flags.init_done)
6471 fc_host_supported_classes(vha->host) =
6472 FC_COS_CLASS2 | FC_COS_CLASS3;
6474 nv->firmware_options_2 |= cpu_to_le32(BIT_8);
6476 if (vha->flags.init_done)
6477 fc_host_supported_classes(vha->host) = FC_COS_CLASS3;
6479 nv->firmware_options_2 &= ~cpu_to_le32(BIT_8);
6484 qlt_24xx_config_nvram_stage2(struct scsi_qla_host *vha,
6485 struct init_cb_24xx *icb)
6487 struct qla_hw_data *ha = vha->hw;
6489 if (!QLA_TGT_MODE_ENABLED())
6492 if (ha->tgt.node_name_set) {
6493 memcpy(icb->node_name, ha->tgt.tgt_node_name, WWN_SIZE);
6494 icb->firmware_options_1 |= cpu_to_le32(BIT_14);
6497 /* disable ZIO at start time. */
6498 if (!vha->flags.init_done) {
6500 tmp = le32_to_cpu(icb->firmware_options_2);
6501 tmp &= ~(BIT_3 | BIT_2 | BIT_1 | BIT_0);
6502 icb->firmware_options_2 = cpu_to_le32(tmp);
6507 qlt_81xx_config_nvram_stage1(struct scsi_qla_host *vha, struct nvram_81xx *nv)
6509 struct qla_hw_data *ha = vha->hw;
6511 if (!QLA_TGT_MODE_ENABLED())
6514 if (qla_tgt_mode_enabled(vha) || qla_dual_mode_enabled(vha)) {
6515 if (!ha->tgt.saved_set) {
6516 /* We save only once */
6517 ha->tgt.saved_exchange_count = nv->exchange_count;
6518 ha->tgt.saved_firmware_options_1 =
6519 nv->firmware_options_1;
6520 ha->tgt.saved_firmware_options_2 =
6521 nv->firmware_options_2;
6522 ha->tgt.saved_firmware_options_3 =
6523 nv->firmware_options_3;
6524 ha->tgt.saved_set = 1;
6527 if (qla_tgt_mode_enabled(vha))
6528 nv->exchange_count = cpu_to_le16(0xFFFF);
6530 nv->exchange_count = cpu_to_le16(ql2xexchoffld);
6532 /* Enable target mode */
6533 nv->firmware_options_1 |= cpu_to_le32(BIT_4);
6535 /* Disable ini mode, if requested */
6536 if (qla_tgt_mode_enabled(vha))
6537 nv->firmware_options_1 |= cpu_to_le32(BIT_5);
6538 /* Disable Full Login after LIP */
6539 nv->firmware_options_1 &= cpu_to_le32(~BIT_13);
6540 /* Enable initial LIP */
6541 nv->firmware_options_1 &= cpu_to_le32(~BIT_9);
6543 * clear BIT 15 explicitly as we have seen at
6544 * least a couple of instances where this was set
6545 * and this was causing the firmware to not be
6548 nv->firmware_options_1 &= cpu_to_le32(~BIT_15);
6549 if (ql2xtgt_tape_enable)
6550 /* Enable FC tape support */
6551 nv->firmware_options_2 |= cpu_to_le32(BIT_12);
6553 /* Disable FC tape support */
6554 nv->firmware_options_2 &= cpu_to_le32(~BIT_12);
6556 /* Disable Full Login after LIP */
6557 nv->host_p &= cpu_to_le32(~BIT_10);
6558 /* Enable target PRLI control */
6559 nv->firmware_options_2 |= cpu_to_le32(BIT_14);
6561 if (ha->tgt.saved_set) {
6562 nv->exchange_count = ha->tgt.saved_exchange_count;
6563 nv->firmware_options_1 =
6564 ha->tgt.saved_firmware_options_1;
6565 nv->firmware_options_2 =
6566 ha->tgt.saved_firmware_options_2;
6567 nv->firmware_options_3 =
6568 ha->tgt.saved_firmware_options_3;
6573 if (ha->tgt.enable_class_2) {
6574 if (vha->flags.init_done)
6575 fc_host_supported_classes(vha->host) =
6576 FC_COS_CLASS2 | FC_COS_CLASS3;
6578 nv->firmware_options_2 |= cpu_to_le32(BIT_8);
6580 if (vha->flags.init_done)
6581 fc_host_supported_classes(vha->host) = FC_COS_CLASS3;
6583 nv->firmware_options_2 &= ~cpu_to_le32(BIT_8);
6588 qlt_81xx_config_nvram_stage2(struct scsi_qla_host *vha,
6589 struct init_cb_81xx *icb)
6591 struct qla_hw_data *ha = vha->hw;
6593 if (!QLA_TGT_MODE_ENABLED())
6596 if (ha->tgt.node_name_set) {
6597 memcpy(icb->node_name, ha->tgt.tgt_node_name, WWN_SIZE);
6598 icb->firmware_options_1 |= cpu_to_le32(BIT_14);
6601 /* disable ZIO at start time. */
6602 if (!vha->flags.init_done) {
6604 tmp = le32_to_cpu(icb->firmware_options_2);
6605 tmp &= ~(BIT_3 | BIT_2 | BIT_1 | BIT_0);
6606 icb->firmware_options_2 = cpu_to_le32(tmp);
6612 qlt_83xx_iospace_config(struct qla_hw_data *ha)
6614 if (!QLA_TGT_MODE_ENABLED())
6617 ha->msix_count += 1; /* For ATIO Q */
6622 qlt_modify_vp_config(struct scsi_qla_host *vha,
6623 struct vp_config_entry_24xx *vpmod)
6625 /* enable target mode. Bit5 = 1 => disable */
6626 if (qla_tgt_mode_enabled(vha) || qla_dual_mode_enabled(vha))
6627 vpmod->options_idx1 &= ~BIT_5;
6629 /* Disable ini mode, if requested. bit4 = 1 => disable */
6630 if (qla_tgt_mode_enabled(vha))
6631 vpmod->options_idx1 &= ~BIT_4;
6635 qlt_probe_one_stage1(struct scsi_qla_host *base_vha, struct qla_hw_data *ha)
6639 if (!QLA_TGT_MODE_ENABLED())
6642 if (ha->mqenable || IS_QLA83XX(ha) || IS_QLA27XX(ha)) {
6643 ISP_ATIO_Q_IN(base_vha) = &ha->mqiobase->isp25mq.atio_q_in;
6644 ISP_ATIO_Q_OUT(base_vha) = &ha->mqiobase->isp25mq.atio_q_out;
6646 ISP_ATIO_Q_IN(base_vha) = &ha->iobase->isp24.atio_q_in;
6647 ISP_ATIO_Q_OUT(base_vha) = &ha->iobase->isp24.atio_q_out;
6650 mutex_init(&base_vha->vha_tgt.tgt_mutex);
6651 mutex_init(&base_vha->vha_tgt.tgt_host_action_mutex);
6653 INIT_LIST_HEAD(&base_vha->unknown_atio_list);
6654 INIT_DELAYED_WORK(&base_vha->unknown_atio_work,
6655 qlt_unknown_atio_work_fn);
6657 qlt_clear_mode(base_vha);
6659 rc = btree_init32(&ha->tgt.host_map);
6661 ql_log(ql_log_info, base_vha, 0xd03d,
6662 "Unable to initialize ha->host_map btree\n");
6664 qlt_update_vp_map(base_vha, SET_VP_IDX);
6668 qla83xx_msix_atio_q(int irq, void *dev_id)
6670 struct rsp_que *rsp;
6671 scsi_qla_host_t *vha;
6672 struct qla_hw_data *ha;
6673 unsigned long flags;
6675 rsp = (struct rsp_que *) dev_id;
6677 vha = pci_get_drvdata(ha->pdev);
6679 spin_lock_irqsave(&ha->tgt.atio_lock, flags);
6681 qlt_24xx_process_atio_queue(vha, 0);
6683 spin_unlock_irqrestore(&ha->tgt.atio_lock, flags);
6689 qlt_handle_abts_recv_work(struct work_struct *work)
6691 struct qla_tgt_sess_op *op = container_of(work,
6692 struct qla_tgt_sess_op, work);
6693 scsi_qla_host_t *vha = op->vha;
6694 struct qla_hw_data *ha = vha->hw;
6695 unsigned long flags;
6697 if (qla2x00_reset_active(vha) || (op->chip_reset != ha->chip_reset))
6700 spin_lock_irqsave(&ha->tgt.atio_lock, flags);
6701 qlt_24xx_process_atio_queue(vha, 0);
6702 spin_unlock_irqrestore(&ha->tgt.atio_lock, flags);
6704 spin_lock_irqsave(&ha->hardware_lock, flags);
6705 qlt_response_pkt_all_vps(vha, op->rsp, (response_t *)&op->atio);
6706 spin_unlock_irqrestore(&ha->hardware_lock, flags);
6712 qlt_handle_abts_recv(struct scsi_qla_host *vha, struct rsp_que *rsp,
6715 struct qla_tgt_sess_op *op;
6717 op = kzalloc(sizeof(*op), GFP_ATOMIC);
6720 /* do not reach for ATIO queue here. This is best effort err
6721 * recovery at this point.
6723 qlt_response_pkt_all_vps(vha, rsp, pkt);
6727 memcpy(&op->atio, pkt, sizeof(*pkt));
6729 op->chip_reset = vha->hw->chip_reset;
6731 INIT_WORK(&op->work, qlt_handle_abts_recv_work);
6732 queue_work(qla_tgt_wq, &op->work);
6737 qlt_mem_alloc(struct qla_hw_data *ha)
6739 if (!QLA_TGT_MODE_ENABLED())
6742 ha->tgt.tgt_vp_map = kzalloc(sizeof(struct qla_tgt_vp_map) *
6743 MAX_MULTI_ID_FABRIC, GFP_KERNEL);
6744 if (!ha->tgt.tgt_vp_map)
6747 ha->tgt.atio_ring = dma_alloc_coherent(&ha->pdev->dev,
6748 (ha->tgt.atio_q_length + 1) * sizeof(struct atio_from_isp),
6749 &ha->tgt.atio_dma, GFP_KERNEL);
6750 if (!ha->tgt.atio_ring) {
6751 kfree(ha->tgt.tgt_vp_map);
6758 qlt_mem_free(struct qla_hw_data *ha)
6760 if (!QLA_TGT_MODE_ENABLED())
6763 if (ha->tgt.atio_ring) {
6764 dma_free_coherent(&ha->pdev->dev, (ha->tgt.atio_q_length + 1) *
6765 sizeof(struct atio_from_isp), ha->tgt.atio_ring,
6768 kfree(ha->tgt.tgt_vp_map);
6771 /* vport_slock to be held by the caller */
6773 qlt_update_vp_map(struct scsi_qla_host *vha, int cmd)
6779 if (!QLA_TGT_MODE_ENABLED())
6782 key = vha->d_id.b24;
6786 vha->hw->tgt.tgt_vp_map[vha->vp_idx].vha = vha;
6789 slot = btree_lookup32(&vha->hw->tgt.host_map, key);
6791 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf018,
6792 "Save vha in host_map %p %06x\n", vha, key);
6793 rc = btree_insert32(&vha->hw->tgt.host_map,
6794 key, vha, GFP_ATOMIC);
6796 ql_log(ql_log_info, vha, 0xd03e,
6797 "Unable to insert s_id into host_map: %06x\n",
6801 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf019,
6802 "replace existing vha in host_map %p %06x\n", vha, key);
6803 btree_update32(&vha->hw->tgt.host_map, key, vha);
6806 vha->hw->tgt.tgt_vp_map[vha->vp_idx].vha = NULL;
6809 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01a,
6810 "clear vha in host_map %p %06x\n", vha, key);
6811 slot = btree_lookup32(&vha->hw->tgt.host_map, key);
6813 btree_remove32(&vha->hw->tgt.host_map, key);
6819 void qlt_update_host_map(struct scsi_qla_host *vha, port_id_t id)
6821 unsigned long flags;
6822 struct qla_hw_data *ha = vha->hw;
6824 if (!vha->d_id.b24) {
6825 spin_lock_irqsave(&ha->vport_slock, flags);
6827 qlt_update_vp_map(vha, SET_AL_PA);
6828 spin_unlock_irqrestore(&ha->vport_slock, flags);
6829 } else if (vha->d_id.b24 != id.b24) {
6830 spin_lock_irqsave(&ha->vport_slock, flags);
6831 qlt_update_vp_map(vha, RESET_AL_PA);
6833 qlt_update_vp_map(vha, SET_AL_PA);
6834 spin_unlock_irqrestore(&ha->vport_slock, flags);
6838 static int __init qlt_parse_ini_mode(void)
6840 if (strcasecmp(qlini_mode, QLA2XXX_INI_MODE_STR_EXCLUSIVE) == 0)
6841 ql2x_ini_mode = QLA2XXX_INI_MODE_EXCLUSIVE;
6842 else if (strcasecmp(qlini_mode, QLA2XXX_INI_MODE_STR_DISABLED) == 0)
6843 ql2x_ini_mode = QLA2XXX_INI_MODE_DISABLED;
6844 else if (strcasecmp(qlini_mode, QLA2XXX_INI_MODE_STR_ENABLED) == 0)
6845 ql2x_ini_mode = QLA2XXX_INI_MODE_ENABLED;
6846 else if (strcasecmp(qlini_mode, QLA2XXX_INI_MODE_STR_DUAL) == 0)
6847 ql2x_ini_mode = QLA2XXX_INI_MODE_DUAL;
6854 int __init qlt_init(void)
6858 if (!qlt_parse_ini_mode()) {
6859 ql_log(ql_log_fatal, NULL, 0xe06b,
6860 "qlt_parse_ini_mode() failed\n");
6864 if (!QLA_TGT_MODE_ENABLED())
6867 qla_tgt_mgmt_cmd_cachep = kmem_cache_create("qla_tgt_mgmt_cmd_cachep",
6868 sizeof(struct qla_tgt_mgmt_cmd), __alignof__(struct
6869 qla_tgt_mgmt_cmd), 0, NULL);
6870 if (!qla_tgt_mgmt_cmd_cachep) {
6871 ql_log(ql_log_fatal, NULL, 0xd04b,
6872 "kmem_cache_create for qla_tgt_mgmt_cmd_cachep failed\n");
6876 qla_tgt_plogi_cachep = kmem_cache_create("qla_tgt_plogi_cachep",
6877 sizeof(struct qlt_plogi_ack_t), __alignof__(struct qlt_plogi_ack_t),
6880 if (!qla_tgt_plogi_cachep) {
6881 ql_log(ql_log_fatal, NULL, 0xe06d,
6882 "kmem_cache_create for qla_tgt_plogi_cachep failed\n");
6884 goto out_mgmt_cmd_cachep;
6887 qla_tgt_mgmt_cmd_mempool = mempool_create(25, mempool_alloc_slab,
6888 mempool_free_slab, qla_tgt_mgmt_cmd_cachep);
6889 if (!qla_tgt_mgmt_cmd_mempool) {
6890 ql_log(ql_log_fatal, NULL, 0xe06e,
6891 "mempool_create for qla_tgt_mgmt_cmd_mempool failed\n");
6893 goto out_plogi_cachep;
6896 qla_tgt_wq = alloc_workqueue("qla_tgt_wq", 0, 0);
6898 ql_log(ql_log_fatal, NULL, 0xe06f,
6899 "alloc_workqueue for qla_tgt_wq failed\n");
6901 goto out_cmd_mempool;
6904 * Return 1 to signal that initiator-mode is being disabled
6906 return (ql2x_ini_mode == QLA2XXX_INI_MODE_DISABLED) ? 1 : 0;
6909 mempool_destroy(qla_tgt_mgmt_cmd_mempool);
6911 kmem_cache_destroy(qla_tgt_plogi_cachep);
6912 out_mgmt_cmd_cachep:
6913 kmem_cache_destroy(qla_tgt_mgmt_cmd_cachep);
6919 if (!QLA_TGT_MODE_ENABLED())
6922 destroy_workqueue(qla_tgt_wq);
6923 mempool_destroy(qla_tgt_mgmt_cmd_mempool);
6924 kmem_cache_destroy(qla_tgt_plogi_cachep);
6925 kmem_cache_destroy(qla_tgt_mgmt_cmd_cachep);