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 "
70 module_param(ql2xuctrlirq, int, 0644);
71 MODULE_PARM_DESC(ql2xuctrlirq,
72 "User to control IRQ placement via smp_affinity."
73 "Valid with qlini_mode=disabled."
74 "1(default): enable");
76 int ql2x_ini_mode = QLA2XXX_INI_MODE_EXCLUSIVE;
78 static int temp_sam_status = SAM_STAT_BUSY;
81 * From scsi/fc/fc_fcp.h
83 enum fcp_resp_rsp_codes {
85 FCP_DATA_LEN_INVALID = 1,
86 FCP_CMND_FIELDS_INVALID = 2,
87 FCP_DATA_PARAM_MISMATCH = 3,
90 FCP_TMF_INVALID_LUN = 9,
94 * fc_pri_ta from scsi/fc/fc_fcp.h
96 #define FCP_PTA_SIMPLE 0 /* simple task attribute */
97 #define FCP_PTA_HEADQ 1 /* head of queue task attribute */
98 #define FCP_PTA_ORDERED 2 /* ordered task attribute */
99 #define FCP_PTA_ACA 4 /* auto. contingent allegiance */
100 #define FCP_PTA_MASK 7 /* mask for task attribute field */
101 #define FCP_PRI_SHIFT 3 /* priority field starts in bit 3 */
102 #define FCP_PRI_RESVD_MASK 0x80 /* reserved bits in priority field */
105 * This driver calls qla2x00_alloc_iocbs() and qla2x00_issue_marker(), which
106 * must be called under HW lock and could unlock/lock it inside.
107 * It isn't an issue, since in the current implementation on the time when
108 * those functions are called:
110 * - Either context is IRQ and only IRQ handler can modify HW data,
111 * including rings related fields,
113 * - Or access to target mode variables from struct qla_tgt doesn't
114 * cross those functions boundaries, except tgt_stop, which
115 * additionally protected by irq_cmd_count.
117 /* Predefs for callbacks handed to qla2xxx LLD */
118 static void qlt_24xx_atio_pkt(struct scsi_qla_host *ha,
119 struct atio_from_isp *pkt, uint8_t);
120 static void qlt_response_pkt(struct scsi_qla_host *ha, struct rsp_que *rsp,
122 static int qlt_issue_task_mgmt(struct fc_port *sess, u64 lun,
123 int fn, void *iocb, int flags);
124 static void qlt_send_term_exchange(struct qla_qpair *, struct qla_tgt_cmd
125 *cmd, struct atio_from_isp *atio, int ha_locked, int ul_abort);
126 static void qlt_alloc_qfull_cmd(struct scsi_qla_host *vha,
127 struct atio_from_isp *atio, uint16_t status, int qfull);
128 static void qlt_disable_vha(struct scsi_qla_host *vha);
129 static void qlt_clear_tgt_db(struct qla_tgt *tgt);
130 static void qlt_send_notify_ack(struct qla_qpair *qpair,
131 struct imm_ntfy_from_isp *ntfy,
132 uint32_t add_flags, uint16_t resp_code, int resp_code_valid,
133 uint16_t srr_flags, uint16_t srr_reject_code, uint8_t srr_explan);
134 static void qlt_send_term_imm_notif(struct scsi_qla_host *vha,
135 struct imm_ntfy_from_isp *imm, int ha_locked);
136 static struct fc_port *qlt_create_sess(struct scsi_qla_host *vha,
137 fc_port_t *fcport, bool local);
138 void qlt_unreg_sess(struct fc_port *sess);
139 static void qlt_24xx_handle_abts(struct scsi_qla_host *,
140 struct abts_recv_from_24xx *);
141 static void qlt_send_busy(struct qla_qpair *, struct atio_from_isp *,
147 static struct kmem_cache *qla_tgt_mgmt_cmd_cachep;
148 struct kmem_cache *qla_tgt_plogi_cachep;
149 static mempool_t *qla_tgt_mgmt_cmd_mempool;
150 static struct workqueue_struct *qla_tgt_wq;
151 static DEFINE_MUTEX(qla_tgt_mutex);
152 static LIST_HEAD(qla_tgt_glist);
154 static const char *prot_op_str(u32 prot_op)
157 case TARGET_PROT_NORMAL: return "NORMAL";
158 case TARGET_PROT_DIN_INSERT: return "DIN_INSERT";
159 case TARGET_PROT_DOUT_INSERT: return "DOUT_INSERT";
160 case TARGET_PROT_DIN_STRIP: return "DIN_STRIP";
161 case TARGET_PROT_DOUT_STRIP: return "DOUT_STRIP";
162 case TARGET_PROT_DIN_PASS: return "DIN_PASS";
163 case TARGET_PROT_DOUT_PASS: return "DOUT_PASS";
164 default: return "UNKNOWN";
168 /* This API intentionally takes dest as a parameter, rather than returning
169 * int value to avoid caller forgetting to issue wmb() after the store */
170 void qlt_do_generation_tick(struct scsi_qla_host *vha, int *dest)
172 scsi_qla_host_t *base_vha = pci_get_drvdata(vha->hw->pdev);
173 *dest = atomic_inc_return(&base_vha->generation_tick);
178 /* Might release hw lock, then reaquire!! */
179 static inline int qlt_issue_marker(struct scsi_qla_host *vha, int vha_locked)
181 /* Send marker if required */
182 if (unlikely(vha->marker_needed != 0)) {
183 int rc = qla2x00_issue_marker(vha, vha_locked);
184 if (rc != QLA_SUCCESS) {
185 ql_dbg(ql_dbg_tgt, vha, 0xe03d,
186 "qla_target(%d): issue_marker() failed\n",
195 struct scsi_qla_host *qlt_find_host_by_d_id(struct scsi_qla_host *vha,
198 struct scsi_qla_host *host;
201 if ((vha->d_id.b.area == d_id[1]) && (vha->d_id.b.domain == d_id[0]) &&
202 (vha->d_id.b.al_pa == d_id[2]))
205 key = (uint32_t)d_id[0] << 16;
206 key |= (uint32_t)d_id[1] << 8;
207 key |= (uint32_t)d_id[2];
209 host = btree_lookup32(&vha->hw->tgt.host_map, key);
211 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf005,
212 "Unable to find host %06x\n", key);
218 struct scsi_qla_host *qlt_find_host_by_vp_idx(struct scsi_qla_host *vha,
221 struct qla_hw_data *ha = vha->hw;
223 if (vha->vp_idx == vp_idx)
226 BUG_ON(ha->tgt.tgt_vp_map == NULL);
227 if (likely(test_bit(vp_idx, ha->vp_idx_map)))
228 return ha->tgt.tgt_vp_map[vp_idx].vha;
233 static inline void qlt_incr_num_pend_cmds(struct scsi_qla_host *vha)
237 spin_lock_irqsave(&vha->hw->tgt.q_full_lock, flags);
239 vha->hw->tgt.num_pend_cmds++;
240 if (vha->hw->tgt.num_pend_cmds > vha->qla_stats.stat_max_pend_cmds)
241 vha->qla_stats.stat_max_pend_cmds =
242 vha->hw->tgt.num_pend_cmds;
243 spin_unlock_irqrestore(&vha->hw->tgt.q_full_lock, flags);
245 static inline void qlt_decr_num_pend_cmds(struct scsi_qla_host *vha)
249 spin_lock_irqsave(&vha->hw->tgt.q_full_lock, flags);
250 vha->hw->tgt.num_pend_cmds--;
251 spin_unlock_irqrestore(&vha->hw->tgt.q_full_lock, flags);
255 static void qlt_queue_unknown_atio(scsi_qla_host_t *vha,
256 struct atio_from_isp *atio, uint8_t ha_locked)
258 struct qla_tgt_sess_op *u;
259 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
263 ql_dbg(ql_dbg_async, vha, 0x502c,
264 "qla_target(%d): dropping unknown ATIO_TYPE7, because tgt is being stopped",
269 u = kzalloc(sizeof(*u), GFP_ATOMIC);
274 memcpy(&u->atio, atio, sizeof(*atio));
275 INIT_LIST_HEAD(&u->cmd_list);
277 spin_lock_irqsave(&vha->cmd_list_lock, flags);
278 list_add_tail(&u->cmd_list, &vha->unknown_atio_list);
279 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
281 schedule_delayed_work(&vha->unknown_atio_work, 1);
287 qlt_send_term_exchange(vha->hw->base_qpair, NULL, atio, ha_locked, 0);
291 static void qlt_try_to_dequeue_unknown_atios(struct scsi_qla_host *vha,
294 struct qla_tgt_sess_op *u, *t;
295 scsi_qla_host_t *host;
296 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
300 list_for_each_entry_safe(u, t, &vha->unknown_atio_list, cmd_list) {
302 ql_dbg(ql_dbg_async, vha, 0x502e,
303 "Freeing unknown %s %p, because of Abort\n",
305 qlt_send_term_exchange(vha->hw->base_qpair, NULL,
306 &u->atio, ha_locked, 0);
310 host = qlt_find_host_by_d_id(vha, u->atio.u.isp24.fcp_hdr.d_id);
312 ql_dbg(ql_dbg_async, vha, 0x502f,
313 "Requeuing unknown ATIO_TYPE7 %p\n", u);
314 qlt_24xx_atio_pkt(host, &u->atio, ha_locked);
315 } else if (tgt->tgt_stop) {
316 ql_dbg(ql_dbg_async, vha, 0x503a,
317 "Freeing unknown %s %p, because tgt is being stopped\n",
319 qlt_send_term_exchange(vha->hw->base_qpair, NULL,
320 &u->atio, ha_locked, 0);
322 ql_dbg(ql_dbg_async, vha, 0x503d,
323 "Reschedule u %p, vha %p, host %p\n", u, vha, host);
326 schedule_delayed_work(&vha->unknown_atio_work,
333 spin_lock_irqsave(&vha->cmd_list_lock, flags);
334 list_del(&u->cmd_list);
335 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
340 void qlt_unknown_atio_work_fn(struct work_struct *work)
342 struct scsi_qla_host *vha = container_of(to_delayed_work(work),
343 struct scsi_qla_host, unknown_atio_work);
345 qlt_try_to_dequeue_unknown_atios(vha, 0);
348 static bool qlt_24xx_atio_pkt_all_vps(struct scsi_qla_host *vha,
349 struct atio_from_isp *atio, uint8_t ha_locked)
351 ql_dbg(ql_dbg_tgt, vha, 0xe072,
352 "%s: qla_target(%d): type %x ox_id %04x\n",
353 __func__, vha->vp_idx, atio->u.raw.entry_type,
354 be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id));
356 switch (atio->u.raw.entry_type) {
359 struct scsi_qla_host *host = qlt_find_host_by_d_id(vha,
360 atio->u.isp24.fcp_hdr.d_id);
361 if (unlikely(NULL == host)) {
362 ql_dbg(ql_dbg_tgt, vha, 0xe03e,
363 "qla_target(%d): Received ATIO_TYPE7 "
364 "with unknown d_id %x:%x:%x\n", vha->vp_idx,
365 atio->u.isp24.fcp_hdr.d_id[0],
366 atio->u.isp24.fcp_hdr.d_id[1],
367 atio->u.isp24.fcp_hdr.d_id[2]);
370 qlt_queue_unknown_atio(vha, atio, ha_locked);
373 if (unlikely(!list_empty(&vha->unknown_atio_list)))
374 qlt_try_to_dequeue_unknown_atios(vha, ha_locked);
376 qlt_24xx_atio_pkt(host, atio, ha_locked);
380 case IMMED_NOTIFY_TYPE:
382 struct scsi_qla_host *host = vha;
383 struct imm_ntfy_from_isp *entry =
384 (struct imm_ntfy_from_isp *)atio;
386 qlt_issue_marker(vha, ha_locked);
388 if ((entry->u.isp24.vp_index != 0xFF) &&
389 (entry->u.isp24.nport_handle != 0xFFFF)) {
390 host = qlt_find_host_by_vp_idx(vha,
391 entry->u.isp24.vp_index);
392 if (unlikely(!host)) {
393 ql_dbg(ql_dbg_tgt, vha, 0xe03f,
394 "qla_target(%d): Received "
395 "ATIO (IMMED_NOTIFY_TYPE) "
396 "with unknown vp_index %d\n",
397 vha->vp_idx, entry->u.isp24.vp_index);
401 qlt_24xx_atio_pkt(host, atio, ha_locked);
405 case VP_RPT_ID_IOCB_TYPE:
406 qla24xx_report_id_acquisition(vha,
407 (struct vp_rpt_id_entry_24xx *)atio);
412 struct abts_recv_from_24xx *entry =
413 (struct abts_recv_from_24xx *)atio;
414 struct scsi_qla_host *host = qlt_find_host_by_vp_idx(vha,
418 if (unlikely(!host)) {
419 ql_dbg(ql_dbg_tgt, vha, 0xe00a,
420 "qla_target(%d): Response pkt (ABTS_RECV_24XX) "
421 "received, with unknown vp_index %d\n",
422 vha->vp_idx, entry->vp_index);
426 spin_lock_irqsave(&host->hw->hardware_lock, flags);
427 qlt_24xx_handle_abts(host, (struct abts_recv_from_24xx *)atio);
429 spin_unlock_irqrestore(&host->hw->hardware_lock, flags);
433 /* case PUREX_IOCB_TYPE: ql2xmvasynctoatio */
436 ql_dbg(ql_dbg_tgt, vha, 0xe040,
437 "qla_target(%d): Received unknown ATIO atio "
438 "type %x\n", vha->vp_idx, atio->u.raw.entry_type);
445 void qlt_response_pkt_all_vps(struct scsi_qla_host *vha,
446 struct rsp_que *rsp, response_t *pkt)
448 switch (pkt->entry_type) {
450 ql_dbg(ql_dbg_tgt, vha, 0xe073,
451 "qla_target(%d):%s: CRC2 Response pkt\n",
452 vha->vp_idx, __func__);
455 struct ctio7_from_24xx *entry = (struct ctio7_from_24xx *)pkt;
456 struct scsi_qla_host *host = qlt_find_host_by_vp_idx(vha,
458 if (unlikely(!host)) {
459 ql_dbg(ql_dbg_tgt, vha, 0xe041,
460 "qla_target(%d): Response pkt (CTIO_TYPE7) "
461 "received, with unknown vp_index %d\n",
462 vha->vp_idx, entry->vp_index);
465 qlt_response_pkt(host, rsp, pkt);
469 case IMMED_NOTIFY_TYPE:
471 struct scsi_qla_host *host = vha;
472 struct imm_ntfy_from_isp *entry =
473 (struct imm_ntfy_from_isp *)pkt;
475 host = qlt_find_host_by_vp_idx(vha, entry->u.isp24.vp_index);
476 if (unlikely(!host)) {
477 ql_dbg(ql_dbg_tgt, vha, 0xe042,
478 "qla_target(%d): Response pkt (IMMED_NOTIFY_TYPE) "
479 "received, with unknown vp_index %d\n",
480 vha->vp_idx, entry->u.isp24.vp_index);
483 qlt_response_pkt(host, rsp, pkt);
487 case NOTIFY_ACK_TYPE:
489 struct scsi_qla_host *host = vha;
490 struct nack_to_isp *entry = (struct nack_to_isp *)pkt;
492 if (0xFF != entry->u.isp24.vp_index) {
493 host = qlt_find_host_by_vp_idx(vha,
494 entry->u.isp24.vp_index);
495 if (unlikely(!host)) {
496 ql_dbg(ql_dbg_tgt, vha, 0xe043,
497 "qla_target(%d): Response "
498 "pkt (NOTIFY_ACK_TYPE) "
499 "received, with unknown "
500 "vp_index %d\n", vha->vp_idx,
501 entry->u.isp24.vp_index);
505 qlt_response_pkt(host, rsp, pkt);
511 struct abts_recv_from_24xx *entry =
512 (struct abts_recv_from_24xx *)pkt;
513 struct scsi_qla_host *host = qlt_find_host_by_vp_idx(vha,
515 if (unlikely(!host)) {
516 ql_dbg(ql_dbg_tgt, vha, 0xe044,
517 "qla_target(%d): Response pkt "
518 "(ABTS_RECV_24XX) received, with unknown "
519 "vp_index %d\n", vha->vp_idx, entry->vp_index);
522 qlt_response_pkt(host, rsp, pkt);
528 struct abts_resp_to_24xx *entry =
529 (struct abts_resp_to_24xx *)pkt;
530 struct scsi_qla_host *host = qlt_find_host_by_vp_idx(vha,
532 if (unlikely(!host)) {
533 ql_dbg(ql_dbg_tgt, vha, 0xe045,
534 "qla_target(%d): Response pkt "
535 "(ABTS_RECV_24XX) received, with unknown "
536 "vp_index %d\n", vha->vp_idx, entry->vp_index);
539 qlt_response_pkt(host, rsp, pkt);
544 qlt_response_pkt(vha, rsp, pkt);
551 * All qlt_plogi_ack_t operations are protected by hardware_lock
553 static int qla24xx_post_nack_work(struct scsi_qla_host *vha, fc_port_t *fcport,
554 struct imm_ntfy_from_isp *ntfy, int type)
556 struct qla_work_evt *e;
557 e = qla2x00_alloc_work(vha, QLA_EVT_NACK);
559 return QLA_FUNCTION_FAILED;
561 e->u.nack.fcport = fcport;
562 e->u.nack.type = type;
563 memcpy(e->u.nack.iocb, ntfy, sizeof(struct imm_ntfy_from_isp));
564 return qla2x00_post_work(vha, e);
568 void qla2x00_async_nack_sp_done(void *s, int res)
570 struct srb *sp = (struct srb *)s;
571 struct scsi_qla_host *vha = sp->vha;
574 ql_dbg(ql_dbg_disc, vha, 0x20f2,
575 "Async done-%s res %x %8phC type %d\n",
576 sp->name, res, sp->fcport->port_name, sp->type);
578 spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
579 sp->fcport->flags &= ~FCF_ASYNC_SENT;
580 sp->fcport->chip_reset = vha->hw->base_qpair->chip_reset;
584 sp->fcport->login_gen++;
585 sp->fcport->fw_login_state = DSC_LS_PLOGI_COMP;
586 sp->fcport->logout_on_delete = 1;
587 sp->fcport->plogi_nack_done_deadline = jiffies + HZ;
588 sp->fcport->send_els_logo = 0;
592 sp->fcport->fw_login_state = DSC_LS_PRLI_COMP;
593 sp->fcport->deleted = 0;
594 sp->fcport->send_els_logo = 0;
596 if (!sp->fcport->login_succ &&
597 !IS_SW_RESV_ADDR(sp->fcport->d_id)) {
598 sp->fcport->login_succ = 1;
602 if (!IS_IIDMA_CAPABLE(vha->hw) ||
603 !vha->hw->flags.gpsc_supported) {
604 ql_dbg(ql_dbg_disc, vha, 0x20f3,
605 "%s %d %8phC post upd_fcport fcp_cnt %d\n",
607 sp->fcport->port_name,
610 qla24xx_post_upd_fcport_work(vha, sp->fcport);
612 ql_dbg(ql_dbg_disc, vha, 0x20f5,
613 "%s %d %8phC post gpsc fcp_cnt %d\n",
615 sp->fcport->port_name,
618 qla24xx_post_gpsc_work(vha, sp->fcport);
624 sp->fcport->login_gen++;
625 sp->fcport->fw_login_state = DSC_LS_PORT_UNAVAIL;
626 qlt_logo_completion_handler(sp->fcport, MBS_COMMAND_COMPLETE);
629 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
634 int qla24xx_async_notify_ack(scsi_qla_host_t *vha, fc_port_t *fcport,
635 struct imm_ntfy_from_isp *ntfy, int type)
637 int rval = QLA_FUNCTION_FAILED;
641 fcport->flags |= FCF_ASYNC_SENT;
644 fcport->fw_login_state = DSC_LS_PLOGI_PEND;
648 fcport->fw_login_state = DSC_LS_PRLI_PEND;
653 fcport->fw_login_state = DSC_LS_LOGO_PEND;
658 sp = qla2x00_get_sp(vha, fcport, GFP_ATOMIC);
665 qla2x00_init_timer(sp, qla2x00_get_async_timeout(vha)+2);
667 sp->u.iocb_cmd.u.nack.ntfy = ntfy;
668 sp->u.iocb_cmd.timeout = qla2x00_async_iocb_timeout;
669 sp->done = qla2x00_async_nack_sp_done;
671 rval = qla2x00_start_sp(sp);
672 if (rval != QLA_SUCCESS)
675 ql_dbg(ql_dbg_disc, vha, 0x20f4,
676 "Async-%s %8phC hndl %x %s\n",
677 sp->name, fcport->port_name, sp->handle, c);
684 fcport->flags &= ~FCF_ASYNC_SENT;
688 void qla24xx_do_nack_work(struct scsi_qla_host *vha, struct qla_work_evt *e)
693 switch (e->u.nack.type) {
695 mutex_lock(&vha->vha_tgt.tgt_mutex);
696 t = qlt_create_sess(vha, e->u.nack.fcport, 0);
697 mutex_unlock(&vha->vha_tgt.tgt_mutex);
699 ql_log(ql_log_info, vha, 0xd034,
700 "%s create sess success %p", __func__, t);
701 spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
702 /* create sess has an extra kref */
703 vha->hw->tgt.tgt_ops->put_sess(e->u.nack.fcport);
704 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
708 qla24xx_async_notify_ack(vha, e->u.nack.fcport,
709 (struct imm_ntfy_from_isp*)e->u.nack.iocb, e->u.nack.type);
712 void qla24xx_delete_sess_fn(struct work_struct *work)
714 fc_port_t *fcport = container_of(work, struct fc_port, del_work);
715 struct qla_hw_data *ha = fcport->vha->hw;
718 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
720 if (fcport->se_sess) {
721 ha->tgt.tgt_ops->shutdown_sess(fcport);
722 ha->tgt.tgt_ops->put_sess(fcport);
724 qlt_unreg_sess(fcport);
726 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
730 * Called from qla2x00_reg_remote_port()
732 void qlt_fc_port_added(struct scsi_qla_host *vha, fc_port_t *fcport)
734 struct qla_hw_data *ha = vha->hw;
735 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
736 struct fc_port *sess = fcport;
739 if (!vha->hw->tgt.tgt_ops)
742 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
744 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
748 if (fcport->disc_state == DSC_DELETE_PEND) {
749 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
753 if (!sess->se_sess) {
754 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
756 mutex_lock(&vha->vha_tgt.tgt_mutex);
757 sess = qlt_create_sess(vha, fcport, false);
758 mutex_unlock(&vha->vha_tgt.tgt_mutex);
760 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
762 if (fcport->fw_login_state == DSC_LS_PRLI_COMP) {
763 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
767 if (!kref_get_unless_zero(&sess->sess_kref)) {
768 ql_dbg(ql_dbg_disc, vha, 0x2107,
769 "%s: kref_get fail sess %8phC \n",
770 __func__, sess->port_name);
771 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
775 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04c,
776 "qla_target(%u): %ssession for port %8phC "
777 "(loop ID %d) reappeared\n", vha->vp_idx,
778 sess->local ? "local " : "", sess->port_name, sess->loop_id);
780 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf007,
781 "Reappeared sess %p\n", sess);
783 ha->tgt.tgt_ops->update_sess(sess, fcport->d_id,
785 (fcport->flags & FCF_CONF_COMP_SUPPORTED));
788 if (sess && sess->local) {
789 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04d,
790 "qla_target(%u): local session for "
791 "port %8phC (loop ID %d) became global\n", vha->vp_idx,
792 fcport->port_name, sess->loop_id);
795 ha->tgt.tgt_ops->put_sess(sess);
796 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
800 * This is a zero-base ref-counting solution, since hardware_lock
801 * guarantees that ref_count is not modified concurrently.
802 * Upon successful return content of iocb is undefined
804 static struct qlt_plogi_ack_t *
805 qlt_plogi_ack_find_add(struct scsi_qla_host *vha, port_id_t *id,
806 struct imm_ntfy_from_isp *iocb)
808 struct qlt_plogi_ack_t *pla;
810 list_for_each_entry(pla, &vha->plogi_ack_list, list) {
811 if (pla->id.b24 == id->b24) {
812 qlt_send_term_imm_notif(vha, &pla->iocb, 1);
813 memcpy(&pla->iocb, iocb, sizeof(pla->iocb));
818 pla = kmem_cache_zalloc(qla_tgt_plogi_cachep, GFP_ATOMIC);
820 ql_dbg(ql_dbg_async, vha, 0x5088,
821 "qla_target(%d): Allocation of plogi_ack failed\n",
826 memcpy(&pla->iocb, iocb, sizeof(pla->iocb));
828 list_add_tail(&pla->list, &vha->plogi_ack_list);
833 void qlt_plogi_ack_unref(struct scsi_qla_host *vha,
834 struct qlt_plogi_ack_t *pla)
836 struct imm_ntfy_from_isp *iocb = &pla->iocb;
839 fc_port_t *fcport = pla->fcport;
841 BUG_ON(!pla->ref_count);
847 ql_dbg(ql_dbg_disc, vha, 0x5089,
848 "Sending PLOGI ACK to wwn %8phC s_id %02x:%02x:%02x loop_id %#04x"
849 " exch %#x ox_id %#x\n", iocb->u.isp24.port_name,
850 iocb->u.isp24.port_id[2], iocb->u.isp24.port_id[1],
851 iocb->u.isp24.port_id[0],
852 le16_to_cpu(iocb->u.isp24.nport_handle),
853 iocb->u.isp24.exchange_address, iocb->ox_id);
855 port_id.b.domain = iocb->u.isp24.port_id[2];
856 port_id.b.area = iocb->u.isp24.port_id[1];
857 port_id.b.al_pa = iocb->u.isp24.port_id[0];
858 port_id.b.rsvd_1 = 0;
860 loop_id = le16_to_cpu(iocb->u.isp24.nport_handle);
862 fcport->loop_id = loop_id;
863 fcport->d_id = port_id;
864 qla24xx_post_nack_work(vha, fcport, iocb, SRB_NACK_PLOGI);
866 list_for_each_entry(fcport, &vha->vp_fcports, list) {
867 if (fcport->plogi_link[QLT_PLOGI_LINK_SAME_WWN] == pla)
868 fcport->plogi_link[QLT_PLOGI_LINK_SAME_WWN] = NULL;
869 if (fcport->plogi_link[QLT_PLOGI_LINK_CONFLICT] == pla)
870 fcport->plogi_link[QLT_PLOGI_LINK_CONFLICT] = NULL;
873 list_del(&pla->list);
874 kmem_cache_free(qla_tgt_plogi_cachep, pla);
878 qlt_plogi_ack_link(struct scsi_qla_host *vha, struct qlt_plogi_ack_t *pla,
879 struct fc_port *sess, enum qlt_plogi_link_t link)
881 struct imm_ntfy_from_isp *iocb = &pla->iocb;
882 /* Inc ref_count first because link might already be pointing at pla */
885 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf097,
886 "Linking sess %p [%d] wwn %8phC with PLOGI ACK to wwn %8phC"
887 " s_id %02x:%02x:%02x, ref=%d pla %p link %d\n",
888 sess, link, sess->port_name,
889 iocb->u.isp24.port_name, iocb->u.isp24.port_id[2],
890 iocb->u.isp24.port_id[1], iocb->u.isp24.port_id[0],
891 pla->ref_count, pla, link);
893 if (sess->plogi_link[link])
894 qlt_plogi_ack_unref(vha, sess->plogi_link[link]);
896 if (link == QLT_PLOGI_LINK_SAME_WWN)
899 sess->plogi_link[link] = pla;
903 /* These fields must be initialized by the caller */
906 * number of cmds dropped while we were waiting for
907 * initiator to ack LOGO initialize to 1 if LOGO is
908 * triggered by a command, otherwise, to 0
912 /* These fields are used by callee */
913 struct list_head list;
917 qlt_send_first_logo(struct scsi_qla_host *vha, qlt_port_logo_t *logo)
919 qlt_port_logo_t *tmp;
922 mutex_lock(&vha->vha_tgt.tgt_mutex);
924 list_for_each_entry(tmp, &vha->logo_list, list) {
925 if (tmp->id.b24 == logo->id.b24) {
926 tmp->cmd_count += logo->cmd_count;
927 mutex_unlock(&vha->vha_tgt.tgt_mutex);
932 list_add_tail(&logo->list, &vha->logo_list);
934 mutex_unlock(&vha->vha_tgt.tgt_mutex);
936 res = qla24xx_els_dcmd_iocb(vha, ELS_DCMD_LOGO, logo->id);
938 mutex_lock(&vha->vha_tgt.tgt_mutex);
939 list_del(&logo->list);
940 mutex_unlock(&vha->vha_tgt.tgt_mutex);
942 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf098,
943 "Finished LOGO to %02x:%02x:%02x, dropped %d cmds, res = %#x\n",
944 logo->id.b.domain, logo->id.b.area, logo->id.b.al_pa,
945 logo->cmd_count, res);
948 static void qlt_free_session_done(struct work_struct *work)
950 struct fc_port *sess = container_of(work, struct fc_port,
952 struct qla_tgt *tgt = sess->tgt;
953 struct scsi_qla_host *vha = sess->vha;
954 struct qla_hw_data *ha = vha->hw;
956 bool logout_started = false;
958 scsi_qla_host_t *base_vha;
960 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf084,
961 "%s: se_sess %p / sess %p from port %8phC loop_id %#04x"
962 " s_id %02x:%02x:%02x logout %d keep %d els_logo %d\n",
963 __func__, sess->se_sess, sess, sess->port_name, sess->loop_id,
964 sess->d_id.b.domain, sess->d_id.b.area, sess->d_id.b.al_pa,
965 sess->logout_on_delete, sess->keep_nport_handle,
966 sess->send_els_logo);
968 if (!IS_SW_RESV_ADDR(sess->d_id)) {
969 if (sess->send_els_logo) {
970 qlt_port_logo_t logo;
972 logo.id = sess->d_id;
974 sess->send_els_logo = 0;
975 qlt_send_first_logo(vha, &logo);
978 if (sess->logout_on_delete && sess->loop_id != FC_NO_LOOP_ID) {
981 rc = qla2x00_post_async_logout_work(vha, sess, NULL);
982 if (rc != QLA_SUCCESS)
983 ql_log(ql_log_warn, vha, 0xf085,
984 "Schedule logo failed sess %p rc %d\n",
987 logout_started = true;
992 * Release the target session for FC Nexus from fabric module code.
994 if (sess->se_sess != NULL)
995 ha->tgt.tgt_ops->free_session(sess);
997 if (logout_started) {
1001 while (!ACCESS_ONCE(sess->logout_completed)) {
1003 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf086,
1004 "%s: waiting for sess %p logout\n",
1014 ql_dbg(ql_dbg_disc, vha, 0xf087,
1015 "%s: sess %p logout completed\n",__func__, sess);
1018 if (sess->logo_ack_needed) {
1019 sess->logo_ack_needed = 0;
1020 qla24xx_async_notify_ack(vha, sess,
1021 (struct imm_ntfy_from_isp *)sess->iocb, SRB_NACK_LOGO);
1024 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
1025 if (sess->se_sess) {
1026 sess->se_sess = NULL;
1027 if (tgt && !IS_SW_RESV_ADDR(sess->d_id))
1031 sess->disc_state = DSC_DELETED;
1032 sess->fw_login_state = DSC_LS_PORT_UNAVAIL;
1033 sess->deleted = QLA_SESS_DELETED;
1034 sess->login_retry = vha->hw->login_retry_count;
1036 if (sess->login_succ && !IS_SW_RESV_ADDR(sess->d_id)) {
1037 vha->fcport_count--;
1038 sess->login_succ = 0;
1041 qla2x00_clear_loop_id(sess);
1043 if (sess->conflict) {
1044 sess->conflict->login_pause = 0;
1045 sess->conflict = NULL;
1046 if (!test_bit(UNLOADING, &vha->dpc_flags))
1047 set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
1051 struct qlt_plogi_ack_t *own =
1052 sess->plogi_link[QLT_PLOGI_LINK_SAME_WWN];
1053 struct qlt_plogi_ack_t *con =
1054 sess->plogi_link[QLT_PLOGI_LINK_CONFLICT];
1055 struct imm_ntfy_from_isp *iocb;
1059 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf099,
1060 "se_sess %p / sess %p port %8phC is gone,"
1061 " %s (ref=%d), releasing PLOGI for %8phC (ref=%d)\n",
1062 sess->se_sess, sess, sess->port_name,
1063 own ? "releasing own PLOGI" : "no own PLOGI pending",
1064 own ? own->ref_count : -1,
1065 iocb->u.isp24.port_name, con->ref_count);
1066 qlt_plogi_ack_unref(vha, con);
1067 sess->plogi_link[QLT_PLOGI_LINK_CONFLICT] = NULL;
1069 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf09a,
1070 "se_sess %p / sess %p port %8phC is gone, %s (ref=%d)\n",
1071 sess->se_sess, sess, sess->port_name,
1072 own ? "releasing own PLOGI" :
1073 "no own PLOGI pending",
1074 own ? own->ref_count : -1);
1078 sess->fw_login_state = DSC_LS_PLOGI_PEND;
1079 qlt_plogi_ack_unref(vha, own);
1080 sess->plogi_link[QLT_PLOGI_LINK_SAME_WWN] = NULL;
1083 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
1085 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf001,
1086 "Unregistration of sess %p %8phC finished fcp_cnt %d\n",
1087 sess, sess->port_name, vha->fcport_count);
1089 if (tgt && (tgt->sess_count == 0))
1090 wake_up_all(&tgt->waitQ);
1092 if (vha->fcport_count == 0)
1093 wake_up_all(&vha->fcport_waitQ);
1095 base_vha = pci_get_drvdata(ha->pdev);
1096 if (test_bit(PFLG_DRIVER_REMOVING, &base_vha->pci_flags))
1099 if (!tgt || !tgt->tgt_stop) {
1100 memset(&ea, 0, sizeof(ea));
1101 ea.event = FCME_DELETE_DONE;
1103 qla2x00_fcport_event_handler(vha, &ea);
1107 /* ha->tgt.sess_lock supposed to be held on entry */
1108 void qlt_unreg_sess(struct fc_port *sess)
1110 struct scsi_qla_host *vha = sess->vha;
1112 ql_dbg(ql_dbg_disc, sess->vha, 0x210a,
1113 "%s sess %p for deletion %8phC\n",
1114 __func__, sess, sess->port_name);
1117 vha->hw->tgt.tgt_ops->clear_nacl_from_fcport_map(sess);
1119 qla2x00_mark_device_lost(vha, sess, 1, 1);
1121 sess->deleted = QLA_SESS_DELETION_IN_PROGRESS;
1122 sess->disc_state = DSC_DELETE_PEND;
1123 sess->last_rscn_gen = sess->rscn_gen;
1124 sess->last_login_gen = sess->login_gen;
1126 if (sess->nvme_flag & NVME_FLAG_REGISTERED)
1127 schedule_work(&sess->nvme_del_work);
1129 INIT_WORK(&sess->free_work, qlt_free_session_done);
1130 schedule_work(&sess->free_work);
1132 EXPORT_SYMBOL(qlt_unreg_sess);
1134 static int qlt_reset(struct scsi_qla_host *vha, void *iocb, int mcmd)
1136 struct qla_hw_data *ha = vha->hw;
1137 struct fc_port *sess = NULL;
1140 struct imm_ntfy_from_isp *n = (struct imm_ntfy_from_isp *)iocb;
1141 unsigned long flags;
1143 loop_id = le16_to_cpu(n->u.isp24.nport_handle);
1144 if (loop_id == 0xFFFF) {
1146 atomic_inc(&vha->vha_tgt.qla_tgt->tgt_global_resets_count);
1147 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
1148 qlt_clear_tgt_db(vha->vha_tgt.qla_tgt);
1149 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
1151 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
1152 sess = ha->tgt.tgt_ops->find_sess_by_loop_id(vha, loop_id);
1153 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
1156 ql_dbg(ql_dbg_tgt, vha, 0xe000,
1157 "Using sess for qla_tgt_reset: %p\n", sess);
1163 ql_dbg(ql_dbg_tgt, vha, 0xe047,
1164 "scsi(%ld): resetting (session %p from port %8phC mcmd %x, "
1165 "loop_id %d)\n", vha->host_no, sess, sess->port_name,
1168 return qlt_issue_task_mgmt(sess, 0, mcmd, iocb, QLA24XX_MGMT_SEND_NACK);
1171 static void qla24xx_chk_fcp_state(struct fc_port *sess)
1173 if (sess->chip_reset != sess->vha->hw->base_qpair->chip_reset) {
1174 sess->logout_on_delete = 0;
1175 sess->logo_ack_needed = 0;
1176 sess->fw_login_state = DSC_LS_PORT_UNAVAIL;
1177 sess->scan_state = 0;
1181 /* ha->tgt.sess_lock supposed to be held on entry */
1182 void qlt_schedule_sess_for_deletion(struct fc_port *sess,
1185 struct qla_tgt *tgt = sess->tgt;
1187 if (sess->disc_state == DSC_DELETE_PEND)
1190 if (sess->disc_state == DSC_DELETED) {
1191 if (tgt && tgt->tgt_stop && (tgt->sess_count == 0))
1192 wake_up_all(&tgt->waitQ);
1193 if (sess->vha->fcport_count == 0)
1194 wake_up_all(&sess->vha->fcport_waitQ);
1196 if (!sess->plogi_link[QLT_PLOGI_LINK_SAME_WWN] &&
1197 !sess->plogi_link[QLT_PLOGI_LINK_CONFLICT])
1201 sess->disc_state = DSC_DELETE_PEND;
1203 if (sess->deleted == QLA_SESS_DELETED)
1204 sess->logout_on_delete = 0;
1206 sess->deleted = QLA_SESS_DELETION_IN_PROGRESS;
1207 qla24xx_chk_fcp_state(sess);
1209 ql_dbg(ql_dbg_tgt, sess->vha, 0xe001,
1210 "Scheduling sess %p for deletion %8phC\n",
1211 sess, sess->port_name);
1213 queue_work(sess->vha->hw->wq, &sess->del_work);
1216 void qlt_schedule_sess_for_deletion_lock(struct fc_port *sess)
1218 unsigned long flags;
1219 struct qla_hw_data *ha = sess->vha->hw;
1220 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
1221 qlt_schedule_sess_for_deletion(sess, 1);
1222 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
1225 /* ha->tgt.sess_lock supposed to be held on entry */
1226 static void qlt_clear_tgt_db(struct qla_tgt *tgt)
1228 struct fc_port *sess;
1229 scsi_qla_host_t *vha = tgt->vha;
1231 list_for_each_entry(sess, &vha->vp_fcports, list) {
1233 qlt_schedule_sess_for_deletion(sess, 1);
1236 /* At this point tgt could be already dead */
1239 static int qla24xx_get_loop_id(struct scsi_qla_host *vha, const uint8_t *s_id,
1242 struct qla_hw_data *ha = vha->hw;
1243 dma_addr_t gid_list_dma;
1244 struct gid_list_info *gid_list;
1249 gid_list = dma_alloc_coherent(&ha->pdev->dev, qla2x00_gid_list_size(ha),
1250 &gid_list_dma, GFP_KERNEL);
1252 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf044,
1253 "qla_target(%d): DMA Alloc failed of %u\n",
1254 vha->vp_idx, qla2x00_gid_list_size(ha));
1258 /* Get list of logged in devices */
1259 rc = qla24xx_gidlist_wait(vha, gid_list, gid_list_dma, &entries);
1260 if (rc != QLA_SUCCESS) {
1261 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf045,
1262 "qla_target(%d): get_id_list() failed: %x\n",
1265 goto out_free_id_list;
1268 id_iter = (char *)gid_list;
1270 for (i = 0; i < entries; i++) {
1271 struct gid_list_info *gid = (struct gid_list_info *)id_iter;
1272 if ((gid->al_pa == s_id[2]) &&
1273 (gid->area == s_id[1]) &&
1274 (gid->domain == s_id[0])) {
1275 *loop_id = le16_to_cpu(gid->loop_id);
1279 id_iter += ha->gid_list_info_size;
1283 dma_free_coherent(&ha->pdev->dev, qla2x00_gid_list_size(ha),
1284 gid_list, gid_list_dma);
1289 * Adds an extra ref to allow to drop hw lock after adding sess to the list.
1290 * Caller must put it.
1292 static struct fc_port *qlt_create_sess(
1293 struct scsi_qla_host *vha,
1297 struct qla_hw_data *ha = vha->hw;
1298 struct fc_port *sess = fcport;
1299 unsigned long flags;
1301 if (vha->vha_tgt.qla_tgt->tgt_stop)
1304 if (fcport->se_sess) {
1305 if (!kref_get_unless_zero(&sess->sess_kref)) {
1306 ql_dbg(ql_dbg_disc, vha, 0x20f6,
1307 "%s: kref_get_unless_zero failed for %8phC\n",
1308 __func__, sess->port_name);
1313 sess->tgt = vha->vha_tgt.qla_tgt;
1314 sess->local = local;
1317 * Under normal circumstances we want to logout from firmware when
1318 * session eventually ends and release corresponding nport handle.
1319 * In the exception cases (e.g. when new PLOGI is waiting) corresponding
1320 * code will adjust these flags as necessary.
1322 sess->logout_on_delete = 1;
1323 sess->keep_nport_handle = 0;
1324 sess->logout_completed = 0;
1326 if (ha->tgt.tgt_ops->check_initiator_node_acl(vha,
1327 &fcport->port_name[0], sess) < 0) {
1328 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf015,
1329 "(%d) %8phC check_initiator_node_acl failed\n",
1330 vha->vp_idx, fcport->port_name);
1333 kref_init(&fcport->sess_kref);
1335 * Take an extra reference to ->sess_kref here to handle
1336 * fc_port access across ->tgt.sess_lock reaquire.
1338 if (!kref_get_unless_zero(&sess->sess_kref)) {
1339 ql_dbg(ql_dbg_disc, vha, 0x20f7,
1340 "%s: kref_get_unless_zero failed for %8phC\n",
1341 __func__, sess->port_name);
1345 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
1346 if (!IS_SW_RESV_ADDR(sess->d_id))
1347 vha->vha_tgt.qla_tgt->sess_count++;
1349 qlt_do_generation_tick(vha, &sess->generation);
1350 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
1353 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf006,
1354 "Adding sess %p se_sess %p to tgt %p sess_count %d\n",
1355 sess, sess->se_sess, vha->vha_tgt.qla_tgt,
1356 vha->vha_tgt.qla_tgt->sess_count);
1358 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04b,
1359 "qla_target(%d): %ssession for wwn %8phC (loop_id %d, "
1360 "s_id %x:%x:%x, confirmed completion %ssupported) added\n",
1361 vha->vp_idx, local ? "local " : "", fcport->port_name,
1362 fcport->loop_id, sess->d_id.b.domain, sess->d_id.b.area,
1363 sess->d_id.b.al_pa, sess->conf_compl_supported ? "" : "not ");
1369 * max_gen - specifies maximum session generation
1370 * at which this deletion requestion is still valid
1373 qlt_fc_port_deleted(struct scsi_qla_host *vha, fc_port_t *fcport, int max_gen)
1375 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
1376 struct fc_port *sess = fcport;
1377 unsigned long flags;
1379 if (!vha->hw->tgt.tgt_ops)
1385 spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
1386 if (tgt->tgt_stop) {
1387 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
1390 if (!sess->se_sess) {
1391 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
1395 if (max_gen - sess->generation < 0) {
1396 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
1397 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf092,
1398 "Ignoring stale deletion request for se_sess %p / sess %p"
1399 " for port %8phC, req_gen %d, sess_gen %d\n",
1400 sess->se_sess, sess, sess->port_name, max_gen,
1405 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf008, "qla_tgt_fc_port_deleted %p", sess);
1408 qlt_schedule_sess_for_deletion(sess, false);
1409 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
1412 static inline int test_tgt_sess_count(struct qla_tgt *tgt)
1414 struct qla_hw_data *ha = tgt->ha;
1415 unsigned long flags;
1418 * We need to protect against race, when tgt is freed before or
1421 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
1422 ql_dbg(ql_dbg_tgt, tgt->vha, 0xe002,
1423 "tgt %p, sess_count=%d\n",
1424 tgt, tgt->sess_count);
1425 res = (tgt->sess_count == 0);
1426 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
1431 /* Called by tcm_qla2xxx configfs code */
1432 int qlt_stop_phase1(struct qla_tgt *tgt)
1434 struct scsi_qla_host *vha = tgt->vha;
1435 struct qla_hw_data *ha = tgt->ha;
1436 unsigned long flags;
1438 mutex_lock(&qla_tgt_mutex);
1439 if (!vha->fc_vport) {
1440 struct Scsi_Host *sh = vha->host;
1441 struct fc_host_attrs *fc_host = shost_to_fc_host(sh);
1444 spin_lock_irqsave(sh->host_lock, flags);
1445 npiv_vports = (fc_host->npiv_vports_inuse);
1446 spin_unlock_irqrestore(sh->host_lock, flags);
1449 mutex_unlock(&qla_tgt_mutex);
1450 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf021,
1451 "NPIV is in use. Can not stop target\n");
1455 if (tgt->tgt_stop || tgt->tgt_stopped) {
1456 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04e,
1457 "Already in tgt->tgt_stop or tgt_stopped state\n");
1458 mutex_unlock(&qla_tgt_mutex);
1462 ql_dbg(ql_dbg_tgt_mgt, vha, 0xe003, "Stopping target for host %ld(%p)\n",
1465 * Mutex needed to sync with qla_tgt_fc_port_[added,deleted].
1466 * Lock is needed, because we still can get an incoming packet.
1468 mutex_lock(&vha->vha_tgt.tgt_mutex);
1469 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
1471 qlt_clear_tgt_db(tgt);
1472 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
1473 mutex_unlock(&vha->vha_tgt.tgt_mutex);
1474 mutex_unlock(&qla_tgt_mutex);
1476 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf009,
1477 "Waiting for sess works (tgt %p)", tgt);
1478 spin_lock_irqsave(&tgt->sess_work_lock, flags);
1479 while (!list_empty(&tgt->sess_works_list)) {
1480 spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
1481 flush_scheduled_work();
1482 spin_lock_irqsave(&tgt->sess_work_lock, flags);
1484 spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
1486 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00a,
1487 "Waiting for tgt %p: sess_count=%d\n", tgt, tgt->sess_count);
1489 wait_event_timeout(tgt->waitQ, test_tgt_sess_count(tgt), 10*HZ);
1492 if (!ha->flags.host_shutting_down &&
1493 (qla_tgt_mode_enabled(vha) || qla_dual_mode_enabled(vha)))
1494 qlt_disable_vha(vha);
1496 /* Wait for sessions to clear out (just in case) */
1497 wait_event_timeout(tgt->waitQ, test_tgt_sess_count(tgt), 10*HZ);
1500 EXPORT_SYMBOL(qlt_stop_phase1);
1502 /* Called by tcm_qla2xxx configfs code */
1503 void qlt_stop_phase2(struct qla_tgt *tgt)
1505 scsi_qla_host_t *vha = tgt->vha;
1507 if (tgt->tgt_stopped) {
1508 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04f,
1509 "Already in tgt->tgt_stopped state\n");
1513 if (!tgt->tgt_stop) {
1514 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00b,
1515 "%s: phase1 stop is not completed\n", __func__);
1520 mutex_lock(&tgt->ha->optrom_mutex);
1521 mutex_lock(&vha->vha_tgt.tgt_mutex);
1523 tgt->tgt_stopped = 1;
1524 mutex_unlock(&vha->vha_tgt.tgt_mutex);
1525 mutex_unlock(&tgt->ha->optrom_mutex);
1527 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00c, "Stop of tgt %p finished\n",
1530 EXPORT_SYMBOL(qlt_stop_phase2);
1532 /* Called from qlt_remove_target() -> qla2x00_remove_one() */
1533 static void qlt_release(struct qla_tgt *tgt)
1535 scsi_qla_host_t *vha = tgt->vha;
1539 struct qla_qpair_hint *h;
1540 struct qla_hw_data *ha = vha->hw;
1542 if ((vha->vha_tgt.qla_tgt != NULL) && !tgt->tgt_stop &&
1544 qlt_stop_phase1(tgt);
1546 if ((vha->vha_tgt.qla_tgt != NULL) && !tgt->tgt_stopped)
1547 qlt_stop_phase2(tgt);
1549 for (i = 0; i < vha->hw->max_qpairs + 1; i++) {
1550 unsigned long flags;
1552 h = &tgt->qphints[i];
1554 spin_lock_irqsave(h->qpair->qp_lock_ptr, flags);
1555 list_del(&h->hint_elem);
1556 spin_unlock_irqrestore(h->qpair->qp_lock_ptr, flags);
1560 kfree(tgt->qphints);
1561 mutex_lock(&qla_tgt_mutex);
1562 list_del(&vha->vha_tgt.qla_tgt->tgt_list_entry);
1563 mutex_unlock(&qla_tgt_mutex);
1565 btree_for_each_safe64(&tgt->lun_qpair_map, key, node)
1566 btree_remove64(&tgt->lun_qpair_map, key);
1568 btree_destroy64(&tgt->lun_qpair_map);
1571 if (ha->tgt.tgt_ops &&
1572 ha->tgt.tgt_ops->remove_target &&
1573 vha->vha_tgt.target_lport_ptr)
1574 ha->tgt.tgt_ops->remove_target(vha);
1576 vha->vha_tgt.qla_tgt = NULL;
1578 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00d,
1579 "Release of tgt %p finished\n", tgt);
1584 /* ha->hardware_lock supposed to be held on entry */
1585 static int qlt_sched_sess_work(struct qla_tgt *tgt, int type,
1586 const void *param, unsigned int param_size)
1588 struct qla_tgt_sess_work_param *prm;
1589 unsigned long flags;
1591 prm = kzalloc(sizeof(*prm), GFP_ATOMIC);
1593 ql_dbg(ql_dbg_tgt_mgt, tgt->vha, 0xf050,
1594 "qla_target(%d): Unable to create session "
1595 "work, command will be refused", 0);
1599 ql_dbg(ql_dbg_tgt_mgt, tgt->vha, 0xf00e,
1600 "Scheduling work (type %d, prm %p)"
1601 " to find session for param %p (size %d, tgt %p)\n",
1602 type, prm, param, param_size, tgt);
1605 memcpy(&prm->tm_iocb, param, param_size);
1607 spin_lock_irqsave(&tgt->sess_work_lock, flags);
1608 list_add_tail(&prm->sess_works_list_entry, &tgt->sess_works_list);
1609 spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
1611 schedule_work(&tgt->sess_work);
1617 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
1619 static void qlt_send_notify_ack(struct qla_qpair *qpair,
1620 struct imm_ntfy_from_isp *ntfy,
1621 uint32_t add_flags, uint16_t resp_code, int resp_code_valid,
1622 uint16_t srr_flags, uint16_t srr_reject_code, uint8_t srr_explan)
1624 struct scsi_qla_host *vha = qpair->vha;
1625 struct qla_hw_data *ha = vha->hw;
1627 struct nack_to_isp *nack;
1629 if (!ha->flags.fw_started)
1632 ql_dbg(ql_dbg_tgt, vha, 0xe004, "Sending NOTIFY_ACK (ha=%p)\n", ha);
1634 pkt = (request_t *)__qla2x00_alloc_iocbs(qpair, NULL);
1636 ql_dbg(ql_dbg_tgt, vha, 0xe049,
1637 "qla_target(%d): %s failed: unable to allocate "
1638 "request packet\n", vha->vp_idx, __func__);
1642 if (vha->vha_tgt.qla_tgt != NULL)
1643 vha->vha_tgt.qla_tgt->notify_ack_expected++;
1645 pkt->entry_type = NOTIFY_ACK_TYPE;
1646 pkt->entry_count = 1;
1648 nack = (struct nack_to_isp *)pkt;
1649 nack->ox_id = ntfy->ox_id;
1651 nack->u.isp24.handle = QLA_TGT_SKIP_HANDLE;
1652 nack->u.isp24.nport_handle = ntfy->u.isp24.nport_handle;
1653 if (le16_to_cpu(ntfy->u.isp24.status) == IMM_NTFY_ELS) {
1654 nack->u.isp24.flags = ntfy->u.isp24.flags &
1655 cpu_to_le32(NOTIFY24XX_FLAGS_PUREX_IOCB);
1657 nack->u.isp24.srr_rx_id = ntfy->u.isp24.srr_rx_id;
1658 nack->u.isp24.status = ntfy->u.isp24.status;
1659 nack->u.isp24.status_subcode = ntfy->u.isp24.status_subcode;
1660 nack->u.isp24.fw_handle = ntfy->u.isp24.fw_handle;
1661 nack->u.isp24.exchange_address = ntfy->u.isp24.exchange_address;
1662 nack->u.isp24.srr_rel_offs = ntfy->u.isp24.srr_rel_offs;
1663 nack->u.isp24.srr_ui = ntfy->u.isp24.srr_ui;
1664 nack->u.isp24.srr_flags = cpu_to_le16(srr_flags);
1665 nack->u.isp24.srr_reject_code = srr_reject_code;
1666 nack->u.isp24.srr_reject_code_expl = srr_explan;
1667 nack->u.isp24.vp_index = ntfy->u.isp24.vp_index;
1669 ql_dbg(ql_dbg_tgt, vha, 0xe005,
1670 "qla_target(%d): Sending 24xx Notify Ack %d\n",
1671 vha->vp_idx, nack->u.isp24.status);
1673 /* Memory Barrier */
1675 qla2x00_start_iocbs(vha, qpair->req);
1679 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
1681 static void qlt_24xx_send_abts_resp(struct qla_qpair *qpair,
1682 struct abts_recv_from_24xx *abts, uint32_t status,
1685 struct scsi_qla_host *vha = qpair->vha;
1686 struct qla_hw_data *ha = vha->hw;
1687 struct abts_resp_to_24xx *resp;
1691 ql_dbg(ql_dbg_tgt, vha, 0xe006,
1692 "Sending task mgmt ABTS response (ha=%p, atio=%p, status=%x\n",
1695 resp = (struct abts_resp_to_24xx *)qla2x00_alloc_iocbs_ready(qpair,
1698 ql_dbg(ql_dbg_tgt, vha, 0xe04a,
1699 "qla_target(%d): %s failed: unable to allocate "
1700 "request packet", vha->vp_idx, __func__);
1704 resp->entry_type = ABTS_RESP_24XX;
1705 resp->entry_count = 1;
1706 resp->nport_handle = abts->nport_handle;
1707 resp->vp_index = vha->vp_idx;
1708 resp->sof_type = abts->sof_type;
1709 resp->exchange_address = abts->exchange_address;
1710 resp->fcp_hdr_le = abts->fcp_hdr_le;
1711 f_ctl = cpu_to_le32(F_CTL_EXCH_CONTEXT_RESP |
1712 F_CTL_LAST_SEQ | F_CTL_END_SEQ |
1713 F_CTL_SEQ_INITIATIVE);
1714 p = (uint8_t *)&f_ctl;
1715 resp->fcp_hdr_le.f_ctl[0] = *p++;
1716 resp->fcp_hdr_le.f_ctl[1] = *p++;
1717 resp->fcp_hdr_le.f_ctl[2] = *p;
1719 resp->fcp_hdr_le.d_id[0] = abts->fcp_hdr_le.d_id[0];
1720 resp->fcp_hdr_le.d_id[1] = abts->fcp_hdr_le.d_id[1];
1721 resp->fcp_hdr_le.d_id[2] = abts->fcp_hdr_le.d_id[2];
1722 resp->fcp_hdr_le.s_id[0] = abts->fcp_hdr_le.s_id[0];
1723 resp->fcp_hdr_le.s_id[1] = abts->fcp_hdr_le.s_id[1];
1724 resp->fcp_hdr_le.s_id[2] = abts->fcp_hdr_le.s_id[2];
1726 resp->fcp_hdr_le.d_id[0] = abts->fcp_hdr_le.s_id[0];
1727 resp->fcp_hdr_le.d_id[1] = abts->fcp_hdr_le.s_id[1];
1728 resp->fcp_hdr_le.d_id[2] = abts->fcp_hdr_le.s_id[2];
1729 resp->fcp_hdr_le.s_id[0] = abts->fcp_hdr_le.d_id[0];
1730 resp->fcp_hdr_le.s_id[1] = abts->fcp_hdr_le.d_id[1];
1731 resp->fcp_hdr_le.s_id[2] = abts->fcp_hdr_le.d_id[2];
1733 resp->exchange_addr_to_abort = abts->exchange_addr_to_abort;
1734 if (status == FCP_TMF_CMPL) {
1735 resp->fcp_hdr_le.r_ctl = R_CTL_BASIC_LINK_SERV | R_CTL_B_ACC;
1736 resp->payload.ba_acct.seq_id_valid = SEQ_ID_INVALID;
1737 resp->payload.ba_acct.low_seq_cnt = 0x0000;
1738 resp->payload.ba_acct.high_seq_cnt = 0xFFFF;
1739 resp->payload.ba_acct.ox_id = abts->fcp_hdr_le.ox_id;
1740 resp->payload.ba_acct.rx_id = abts->fcp_hdr_le.rx_id;
1742 resp->fcp_hdr_le.r_ctl = R_CTL_BASIC_LINK_SERV | R_CTL_B_RJT;
1743 resp->payload.ba_rjt.reason_code =
1744 BA_RJT_REASON_CODE_UNABLE_TO_PERFORM;
1745 /* Other bytes are zero */
1748 vha->vha_tgt.qla_tgt->abts_resp_expected++;
1750 /* Memory Barrier */
1752 if (qpair->reqq_start_iocbs)
1753 qpair->reqq_start_iocbs(qpair);
1755 qla2x00_start_iocbs(vha, qpair->req);
1759 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
1761 static void qlt_24xx_retry_term_exchange(struct scsi_qla_host *vha,
1762 struct abts_resp_from_24xx_fw *entry)
1764 struct ctio7_to_24xx *ctio;
1766 ql_dbg(ql_dbg_tgt, vha, 0xe007,
1767 "Sending retry TERM EXCH CTIO7 (ha=%p)\n", vha->hw);
1769 ctio = (struct ctio7_to_24xx *)qla2x00_alloc_iocbs_ready(
1770 vha->hw->base_qpair, NULL);
1772 ql_dbg(ql_dbg_tgt, vha, 0xe04b,
1773 "qla_target(%d): %s failed: unable to allocate "
1774 "request packet\n", vha->vp_idx, __func__);
1779 * We've got on entrance firmware's response on by us generated
1780 * ABTS response. So, in it ID fields are reversed.
1783 ctio->entry_type = CTIO_TYPE7;
1784 ctio->entry_count = 1;
1785 ctio->nport_handle = entry->nport_handle;
1786 ctio->handle = QLA_TGT_SKIP_HANDLE | CTIO_COMPLETION_HANDLE_MARK;
1787 ctio->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
1788 ctio->vp_index = vha->vp_idx;
1789 ctio->initiator_id[0] = entry->fcp_hdr_le.d_id[0];
1790 ctio->initiator_id[1] = entry->fcp_hdr_le.d_id[1];
1791 ctio->initiator_id[2] = entry->fcp_hdr_le.d_id[2];
1792 ctio->exchange_addr = entry->exchange_addr_to_abort;
1793 ctio->u.status1.flags = cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_1 |
1794 CTIO7_FLAGS_TERMINATE);
1795 ctio->u.status1.ox_id = cpu_to_le16(entry->fcp_hdr_le.ox_id);
1797 /* Memory Barrier */
1799 qla2x00_start_iocbs(vha, vha->req);
1801 qlt_24xx_send_abts_resp(vha->hw->base_qpair,
1802 (struct abts_recv_from_24xx *)entry,
1803 FCP_TMF_CMPL, true);
1806 static int abort_cmd_for_tag(struct scsi_qla_host *vha, uint32_t tag)
1808 struct qla_tgt_sess_op *op;
1809 struct qla_tgt_cmd *cmd;
1810 unsigned long flags;
1812 spin_lock_irqsave(&vha->cmd_list_lock, flags);
1813 list_for_each_entry(op, &vha->qla_sess_op_cmd_list, cmd_list) {
1814 if (tag == op->atio.u.isp24.exchange_addr) {
1816 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
1821 list_for_each_entry(op, &vha->unknown_atio_list, cmd_list) {
1822 if (tag == op->atio.u.isp24.exchange_addr) {
1824 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
1829 list_for_each_entry(cmd, &vha->qla_cmd_list, cmd_list) {
1830 if (tag == cmd->atio.u.isp24.exchange_addr) {
1832 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
1836 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
1841 /* drop cmds for the given lun
1842 * XXX only looks for cmds on the port through which lun reset was recieved
1843 * XXX does not go through the list of other port (which may have cmds
1846 static void abort_cmds_for_lun(struct scsi_qla_host *vha,
1847 u64 lun, uint8_t *s_id)
1849 struct qla_tgt_sess_op *op;
1850 struct qla_tgt_cmd *cmd;
1852 unsigned long flags;
1854 key = sid_to_key(s_id);
1855 spin_lock_irqsave(&vha->cmd_list_lock, flags);
1856 list_for_each_entry(op, &vha->qla_sess_op_cmd_list, cmd_list) {
1860 op_key = sid_to_key(op->atio.u.isp24.fcp_hdr.s_id);
1861 op_lun = scsilun_to_int(
1862 (struct scsi_lun *)&op->atio.u.isp24.fcp_cmnd.lun);
1863 if (op_key == key && op_lun == lun)
1867 list_for_each_entry(op, &vha->unknown_atio_list, cmd_list) {
1871 op_key = sid_to_key(op->atio.u.isp24.fcp_hdr.s_id);
1872 op_lun = scsilun_to_int(
1873 (struct scsi_lun *)&op->atio.u.isp24.fcp_cmnd.lun);
1874 if (op_key == key && op_lun == lun)
1878 list_for_each_entry(cmd, &vha->qla_cmd_list, cmd_list) {
1882 cmd_key = sid_to_key(cmd->atio.u.isp24.fcp_hdr.s_id);
1883 cmd_lun = scsilun_to_int(
1884 (struct scsi_lun *)&cmd->atio.u.isp24.fcp_cmnd.lun);
1885 if (cmd_key == key && cmd_lun == lun)
1888 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
1891 /* ha->hardware_lock supposed to be held on entry */
1892 static int __qlt_24xx_handle_abts(struct scsi_qla_host *vha,
1893 struct abts_recv_from_24xx *abts, struct fc_port *sess)
1895 struct qla_hw_data *ha = vha->hw;
1896 struct qla_tgt_mgmt_cmd *mcmd;
1899 if (abort_cmd_for_tag(vha, abts->exchange_addr_to_abort)) {
1900 /* send TASK_ABORT response immediately */
1901 qlt_24xx_send_abts_resp(ha->base_qpair, abts, FCP_TMF_CMPL, false);
1905 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00f,
1906 "qla_target(%d): task abort (tag=%d)\n",
1907 vha->vp_idx, abts->exchange_addr_to_abort);
1909 mcmd = mempool_alloc(qla_tgt_mgmt_cmd_mempool, GFP_ATOMIC);
1911 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf051,
1912 "qla_target(%d): %s: Allocation of ABORT cmd failed",
1913 vha->vp_idx, __func__);
1916 memset(mcmd, 0, sizeof(*mcmd));
1919 memcpy(&mcmd->orig_iocb.abts, abts, sizeof(mcmd->orig_iocb.abts));
1920 mcmd->reset_count = ha->base_qpair->chip_reset;
1921 mcmd->tmr_func = QLA_TGT_ABTS;
1922 mcmd->qpair = ha->base_qpair;
1926 * LUN is looked up by target-core internally based on the passed
1927 * abts->exchange_addr_to_abort tag.
1929 rc = ha->tgt.tgt_ops->handle_tmr(mcmd, 0, mcmd->tmr_func,
1930 abts->exchange_addr_to_abort);
1932 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf052,
1933 "qla_target(%d): tgt_ops->handle_tmr()"
1934 " failed: %d", vha->vp_idx, rc);
1935 mempool_free(mcmd, qla_tgt_mgmt_cmd_mempool);
1943 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
1945 static void qlt_24xx_handle_abts(struct scsi_qla_host *vha,
1946 struct abts_recv_from_24xx *abts)
1948 struct qla_hw_data *ha = vha->hw;
1949 struct fc_port *sess;
1950 uint32_t tag = abts->exchange_addr_to_abort;
1953 unsigned long flags;
1955 if (le32_to_cpu(abts->fcp_hdr_le.parameter) & ABTS_PARAM_ABORT_SEQ) {
1956 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf053,
1957 "qla_target(%d): ABTS: Abort Sequence not "
1958 "supported\n", vha->vp_idx);
1959 qlt_24xx_send_abts_resp(ha->base_qpair, abts, FCP_TMF_REJECTED,
1964 if (tag == ATIO_EXCHANGE_ADDRESS_UNKNOWN) {
1965 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf010,
1966 "qla_target(%d): ABTS: Unknown Exchange "
1967 "Address received\n", vha->vp_idx);
1968 qlt_24xx_send_abts_resp(ha->base_qpair, abts, FCP_TMF_REJECTED,
1973 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf011,
1974 "qla_target(%d): task abort (s_id=%x:%x:%x, "
1975 "tag=%d, param=%x)\n", vha->vp_idx, abts->fcp_hdr_le.s_id[2],
1976 abts->fcp_hdr_le.s_id[1], abts->fcp_hdr_le.s_id[0], tag,
1977 le32_to_cpu(abts->fcp_hdr_le.parameter));
1979 s_id[0] = abts->fcp_hdr_le.s_id[2];
1980 s_id[1] = abts->fcp_hdr_le.s_id[1];
1981 s_id[2] = abts->fcp_hdr_le.s_id[0];
1983 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
1984 sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha, s_id);
1986 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf012,
1987 "qla_target(%d): task abort for non-existant session\n",
1989 rc = qlt_sched_sess_work(vha->vha_tgt.qla_tgt,
1990 QLA_TGT_SESS_WORK_ABORT, abts, sizeof(*abts));
1992 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
1995 qlt_24xx_send_abts_resp(ha->base_qpair, abts,
1996 FCP_TMF_REJECTED, false);
2000 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
2003 if (sess->deleted) {
2004 qlt_24xx_send_abts_resp(ha->base_qpair, abts, FCP_TMF_REJECTED,
2009 rc = __qlt_24xx_handle_abts(vha, abts, sess);
2011 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf054,
2012 "qla_target(%d): __qlt_24xx_handle_abts() failed: %d\n",
2014 qlt_24xx_send_abts_resp(ha->base_qpair, abts, FCP_TMF_REJECTED,
2021 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
2023 static void qlt_24xx_send_task_mgmt_ctio(struct qla_qpair *qpair,
2024 struct qla_tgt_mgmt_cmd *mcmd, uint32_t resp_code)
2026 struct scsi_qla_host *ha = mcmd->vha;
2027 struct atio_from_isp *atio = &mcmd->orig_iocb.atio;
2028 struct ctio7_to_24xx *ctio;
2031 ql_dbg(ql_dbg_tgt, ha, 0xe008,
2032 "Sending task mgmt CTIO7 (ha=%p, atio=%p, resp_code=%x\n",
2033 ha, atio, resp_code);
2036 ctio = (struct ctio7_to_24xx *)__qla2x00_alloc_iocbs(qpair, NULL);
2038 ql_dbg(ql_dbg_tgt, ha, 0xe04c,
2039 "qla_target(%d): %s failed: unable to allocate "
2040 "request packet\n", ha->vp_idx, __func__);
2044 ctio->entry_type = CTIO_TYPE7;
2045 ctio->entry_count = 1;
2046 ctio->handle = QLA_TGT_SKIP_HANDLE | CTIO_COMPLETION_HANDLE_MARK;
2047 ctio->nport_handle = mcmd->sess->loop_id;
2048 ctio->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
2049 ctio->vp_index = ha->vp_idx;
2050 ctio->initiator_id[0] = atio->u.isp24.fcp_hdr.s_id[2];
2051 ctio->initiator_id[1] = atio->u.isp24.fcp_hdr.s_id[1];
2052 ctio->initiator_id[2] = atio->u.isp24.fcp_hdr.s_id[0];
2053 ctio->exchange_addr = atio->u.isp24.exchange_addr;
2054 temp = (atio->u.isp24.attr << 9)|
2055 CTIO7_FLAGS_STATUS_MODE_1 | CTIO7_FLAGS_SEND_STATUS;
2056 ctio->u.status1.flags = cpu_to_le16(temp);
2057 temp = be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id);
2058 ctio->u.status1.ox_id = cpu_to_le16(temp);
2059 ctio->u.status1.scsi_status =
2060 cpu_to_le16(SS_RESPONSE_INFO_LEN_VALID);
2061 ctio->u.status1.response_len = cpu_to_le16(8);
2062 ctio->u.status1.sense_data[0] = resp_code;
2064 /* Memory Barrier */
2066 if (qpair->reqq_start_iocbs)
2067 qpair->reqq_start_iocbs(qpair);
2069 qla2x00_start_iocbs(ha, qpair->req);
2072 void qlt_free_mcmd(struct qla_tgt_mgmt_cmd *mcmd)
2074 mempool_free(mcmd, qla_tgt_mgmt_cmd_mempool);
2076 EXPORT_SYMBOL(qlt_free_mcmd);
2079 * ha->hardware_lock supposed to be held on entry. Might drop it, then
2082 void qlt_send_resp_ctio(struct qla_qpair *qpair, struct qla_tgt_cmd *cmd,
2083 uint8_t scsi_status, uint8_t sense_key, uint8_t asc, uint8_t ascq)
2085 struct atio_from_isp *atio = &cmd->atio;
2086 struct ctio7_to_24xx *ctio;
2088 struct scsi_qla_host *vha = cmd->vha;
2090 ql_dbg(ql_dbg_tgt_dif, vha, 0x3066,
2091 "Sending response CTIO7 (vha=%p, atio=%p, scsi_status=%02x, "
2092 "sense_key=%02x, asc=%02x, ascq=%02x",
2093 vha, atio, scsi_status, sense_key, asc, ascq);
2095 ctio = (struct ctio7_to_24xx *)qla2x00_alloc_iocbs(vha, NULL);
2097 ql_dbg(ql_dbg_async, vha, 0x3067,
2098 "qla2x00t(%ld): %s failed: unable to allocate request packet",
2099 vha->host_no, __func__);
2103 ctio->entry_type = CTIO_TYPE7;
2104 ctio->entry_count = 1;
2105 ctio->handle = QLA_TGT_SKIP_HANDLE;
2106 ctio->nport_handle = cmd->sess->loop_id;
2107 ctio->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
2108 ctio->vp_index = vha->vp_idx;
2109 ctio->initiator_id[0] = atio->u.isp24.fcp_hdr.s_id[2];
2110 ctio->initiator_id[1] = atio->u.isp24.fcp_hdr.s_id[1];
2111 ctio->initiator_id[2] = atio->u.isp24.fcp_hdr.s_id[0];
2112 ctio->exchange_addr = atio->u.isp24.exchange_addr;
2113 temp = (atio->u.isp24.attr << 9) |
2114 CTIO7_FLAGS_STATUS_MODE_1 | CTIO7_FLAGS_SEND_STATUS;
2115 ctio->u.status1.flags = cpu_to_le16(temp);
2116 temp = be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id);
2117 ctio->u.status1.ox_id = cpu_to_le16(temp);
2118 ctio->u.status1.scsi_status =
2119 cpu_to_le16(SS_RESPONSE_INFO_LEN_VALID | scsi_status);
2120 ctio->u.status1.response_len = cpu_to_le16(18);
2121 ctio->u.status1.residual = cpu_to_le32(get_datalen_for_atio(atio));
2123 if (ctio->u.status1.residual != 0)
2124 ctio->u.status1.scsi_status |=
2125 cpu_to_le16(SS_RESIDUAL_UNDER);
2127 /* Fixed format sense data. */
2128 ctio->u.status1.sense_data[0] = 0x70;
2129 ctio->u.status1.sense_data[2] = sense_key;
2130 /* Additional sense length */
2131 ctio->u.status1.sense_data[7] = 0xa;
2133 ctio->u.status1.sense_data[12] = asc;
2134 ctio->u.status1.sense_data[13] = ascq;
2136 /* Memory Barrier */
2139 if (qpair->reqq_start_iocbs)
2140 qpair->reqq_start_iocbs(qpair);
2142 qla2x00_start_iocbs(vha, qpair->req);
2148 /* callback from target fabric module code */
2149 void qlt_xmit_tm_rsp(struct qla_tgt_mgmt_cmd *mcmd)
2151 struct scsi_qla_host *vha = mcmd->sess->vha;
2152 struct qla_hw_data *ha = vha->hw;
2153 unsigned long flags;
2154 struct qla_qpair *qpair = mcmd->qpair;
2156 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf013,
2157 "TM response mcmd (%p) status %#x state %#x",
2158 mcmd, mcmd->fc_tm_rsp, mcmd->flags);
2160 spin_lock_irqsave(qpair->qp_lock_ptr, flags);
2162 if (!vha->flags.online || mcmd->reset_count != qpair->chip_reset) {
2164 * Either the port is not online or this request was from
2165 * previous life, just abort the processing.
2167 ql_dbg(ql_dbg_async, vha, 0xe100,
2168 "RESET-TMR online/active/old-count/new-count = %d/%d/%d/%d.\n",
2169 vha->flags.online, qla2x00_reset_active(vha),
2170 mcmd->reset_count, qpair->chip_reset);
2171 ha->tgt.tgt_ops->free_mcmd(mcmd);
2172 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
2176 if (mcmd->flags == QLA24XX_MGMT_SEND_NACK) {
2177 if (mcmd->orig_iocb.imm_ntfy.u.isp24.status_subcode ==
2179 mcmd->orig_iocb.imm_ntfy.u.isp24.status_subcode ==
2181 mcmd->orig_iocb.imm_ntfy.u.isp24.status_subcode ==
2183 ql_dbg(ql_dbg_disc, vha, 0x2106,
2184 "TM response logo %8phC status %#x state %#x",
2185 mcmd->sess->port_name, mcmd->fc_tm_rsp,
2187 qlt_schedule_sess_for_deletion_lock(mcmd->sess);
2189 qlt_send_notify_ack(vha->hw->base_qpair,
2190 &mcmd->orig_iocb.imm_ntfy, 0, 0, 0, 0, 0, 0);
2193 if (mcmd->orig_iocb.atio.u.raw.entry_type == ABTS_RECV_24XX)
2194 qlt_24xx_send_abts_resp(qpair, &mcmd->orig_iocb.abts,
2195 mcmd->fc_tm_rsp, false);
2197 qlt_24xx_send_task_mgmt_ctio(qpair, mcmd,
2201 * Make the callback for ->free_mcmd() to queue_work() and invoke
2202 * target_put_sess_cmd() to drop cmd_kref to 1. The final
2203 * target_put_sess_cmd() call will be made from TFO->check_stop_free()
2204 * -> tcm_qla2xxx_check_stop_free() to release the TMR associated se_cmd
2205 * descriptor after TFO->queue_tm_rsp() -> tcm_qla2xxx_queue_tm_rsp() ->
2206 * qlt_xmit_tm_rsp() returns here..
2208 ha->tgt.tgt_ops->free_mcmd(mcmd);
2209 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
2211 EXPORT_SYMBOL(qlt_xmit_tm_rsp);
2214 static int qlt_pci_map_calc_cnt(struct qla_tgt_prm *prm)
2216 struct qla_tgt_cmd *cmd = prm->cmd;
2218 BUG_ON(cmd->sg_cnt == 0);
2220 prm->sg = (struct scatterlist *)cmd->sg;
2221 prm->seg_cnt = pci_map_sg(cmd->qpair->pdev, cmd->sg,
2222 cmd->sg_cnt, cmd->dma_data_direction);
2223 if (unlikely(prm->seg_cnt == 0))
2226 prm->cmd->sg_mapped = 1;
2228 if (cmd->se_cmd.prot_op == TARGET_PROT_NORMAL) {
2230 * If greater than four sg entries then we need to allocate
2231 * the continuation entries
2233 if (prm->seg_cnt > QLA_TGT_DATASEGS_PER_CMD_24XX)
2234 prm->req_cnt += DIV_ROUND_UP(prm->seg_cnt -
2235 QLA_TGT_DATASEGS_PER_CMD_24XX,
2236 QLA_TGT_DATASEGS_PER_CONT_24XX);
2239 if ((cmd->se_cmd.prot_op == TARGET_PROT_DIN_INSERT) ||
2240 (cmd->se_cmd.prot_op == TARGET_PROT_DOUT_STRIP)) {
2241 prm->seg_cnt = DIV_ROUND_UP(cmd->bufflen, cmd->blk_sz);
2242 prm->tot_dsds = prm->seg_cnt;
2244 prm->tot_dsds = prm->seg_cnt;
2246 if (cmd->prot_sg_cnt) {
2247 prm->prot_sg = cmd->prot_sg;
2248 prm->prot_seg_cnt = pci_map_sg(cmd->qpair->pdev,
2249 cmd->prot_sg, cmd->prot_sg_cnt,
2250 cmd->dma_data_direction);
2251 if (unlikely(prm->prot_seg_cnt == 0))
2254 if ((cmd->se_cmd.prot_op == TARGET_PROT_DIN_INSERT) ||
2255 (cmd->se_cmd.prot_op == TARGET_PROT_DOUT_STRIP)) {
2256 /* Dif Bundling not support here */
2257 prm->prot_seg_cnt = DIV_ROUND_UP(cmd->bufflen,
2259 prm->tot_dsds += prm->prot_seg_cnt;
2261 prm->tot_dsds += prm->prot_seg_cnt;
2268 ql_dbg_qp(ql_dbg_tgt, prm->cmd->qpair, 0xe04d,
2269 "qla_target(%d): PCI mapping failed: sg_cnt=%d",
2270 0, prm->cmd->sg_cnt);
2274 static void qlt_unmap_sg(struct scsi_qla_host *vha, struct qla_tgt_cmd *cmd)
2276 struct qla_hw_data *ha;
2277 struct qla_qpair *qpair;
2278 if (!cmd->sg_mapped)
2283 pci_unmap_sg(qpair->pdev, cmd->sg, cmd->sg_cnt,
2284 cmd->dma_data_direction);
2287 if (cmd->prot_sg_cnt)
2288 pci_unmap_sg(qpair->pdev, cmd->prot_sg, cmd->prot_sg_cnt,
2289 cmd->dma_data_direction);
2294 if (cmd->ctx_dsd_alloced)
2295 qla2x00_clean_dsd_pool(ha, cmd->ctx);
2297 dma_pool_free(ha->dl_dma_pool, cmd->ctx, cmd->ctx->crc_ctx_dma);
2300 static int qlt_check_reserve_free_req(struct qla_qpair *qpair,
2304 struct req_que *req = qpair->req;
2306 if (req->cnt < (req_cnt + 2)) {
2307 cnt = (uint16_t)(qpair->use_shadow_reg ? *req->out_ptr :
2308 RD_REG_DWORD_RELAXED(req->req_q_out));
2310 if (req->ring_index < cnt)
2311 req->cnt = cnt - req->ring_index;
2313 req->cnt = req->length - (req->ring_index - cnt);
2315 if (unlikely(req->cnt < (req_cnt + 2)))
2319 req->cnt -= req_cnt;
2325 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
2327 static inline void *qlt_get_req_pkt(struct req_que *req)
2329 /* Adjust ring index. */
2331 if (req->ring_index == req->length) {
2332 req->ring_index = 0;
2333 req->ring_ptr = req->ring;
2337 return (cont_entry_t *)req->ring_ptr;
2340 /* ha->hardware_lock supposed to be held on entry */
2341 static inline uint32_t qlt_make_handle(struct qla_qpair *qpair)
2346 struct req_que *req = qpair->req;
2348 h = req->current_outstanding_cmd;
2350 for (index = 1; index < req->num_outstanding_cmds; index++) {
2352 if (h == req->num_outstanding_cmds)
2355 if (h == QLA_TGT_SKIP_HANDLE)
2358 if (!req->outstanding_cmds[h]) {
2365 req->current_outstanding_cmd = h;
2367 ql_dbg(ql_dbg_io, qpair->vha, 0x305b,
2368 "qla_target(%d): Ran out of empty cmd slots\n",
2369 qpair->vha->vp_idx);
2370 h = QLA_TGT_NULL_HANDLE;
2376 /* ha->hardware_lock supposed to be held on entry */
2377 static int qlt_24xx_build_ctio_pkt(struct qla_qpair *qpair,
2378 struct qla_tgt_prm *prm)
2381 struct ctio7_to_24xx *pkt;
2382 struct atio_from_isp *atio = &prm->cmd->atio;
2385 pkt = (struct ctio7_to_24xx *)qpair->req->ring_ptr;
2387 memset(pkt, 0, sizeof(*pkt));
2389 pkt->entry_type = CTIO_TYPE7;
2390 pkt->entry_count = (uint8_t)prm->req_cnt;
2391 pkt->vp_index = prm->cmd->vp_idx;
2393 h = qlt_make_handle(qpair);
2394 if (unlikely(h == QLA_TGT_NULL_HANDLE)) {
2396 * CTIO type 7 from the firmware doesn't provide a way to
2397 * know the initiator's LOOP ID, hence we can't find
2398 * the session and, so, the command.
2402 qpair->req->outstanding_cmds[h] = (srb_t *)prm->cmd;
2404 pkt->handle = MAKE_HANDLE(qpair->req->id, h);
2405 pkt->handle |= CTIO_COMPLETION_HANDLE_MARK;
2406 pkt->nport_handle = cpu_to_le16(prm->cmd->loop_id);
2407 pkt->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
2408 pkt->initiator_id[0] = atio->u.isp24.fcp_hdr.s_id[2];
2409 pkt->initiator_id[1] = atio->u.isp24.fcp_hdr.s_id[1];
2410 pkt->initiator_id[2] = atio->u.isp24.fcp_hdr.s_id[0];
2411 pkt->exchange_addr = atio->u.isp24.exchange_addr;
2412 temp = atio->u.isp24.attr << 9;
2413 pkt->u.status0.flags |= cpu_to_le16(temp);
2414 temp = be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id);
2415 pkt->u.status0.ox_id = cpu_to_le16(temp);
2416 pkt->u.status0.relative_offset = cpu_to_le32(prm->cmd->offset);
2422 * ha->hardware_lock supposed to be held on entry. We have already made sure
2423 * that there is sufficient amount of request entries to not drop it.
2425 static void qlt_load_cont_data_segments(struct qla_tgt_prm *prm)
2428 uint32_t *dword_ptr;
2430 /* Build continuation packets */
2431 while (prm->seg_cnt > 0) {
2432 cont_a64_entry_t *cont_pkt64 =
2433 (cont_a64_entry_t *)qlt_get_req_pkt(
2434 prm->cmd->qpair->req);
2437 * Make sure that from cont_pkt64 none of
2438 * 64-bit specific fields used for 32-bit
2439 * addressing. Cast to (cont_entry_t *) for
2443 memset(cont_pkt64, 0, sizeof(*cont_pkt64));
2445 cont_pkt64->entry_count = 1;
2446 cont_pkt64->sys_define = 0;
2448 cont_pkt64->entry_type = CONTINUE_A64_TYPE;
2449 dword_ptr = (uint32_t *)&cont_pkt64->dseg_0_address;
2451 /* Load continuation entry data segments */
2453 cnt < QLA_TGT_DATASEGS_PER_CONT_24XX && prm->seg_cnt;
2454 cnt++, prm->seg_cnt--) {
2456 cpu_to_le32(pci_dma_lo32
2457 (sg_dma_address(prm->sg)));
2458 *dword_ptr++ = cpu_to_le32(pci_dma_hi32
2459 (sg_dma_address(prm->sg)));
2460 *dword_ptr++ = cpu_to_le32(sg_dma_len(prm->sg));
2462 prm->sg = sg_next(prm->sg);
2468 * ha->hardware_lock supposed to be held on entry. We have already made sure
2469 * that there is sufficient amount of request entries to not drop it.
2471 static void qlt_load_data_segments(struct qla_tgt_prm *prm)
2474 uint32_t *dword_ptr;
2475 struct ctio7_to_24xx *pkt24 = (struct ctio7_to_24xx *)prm->pkt;
2477 pkt24->u.status0.transfer_length = cpu_to_le32(prm->cmd->bufflen);
2479 /* Setup packet address segment pointer */
2480 dword_ptr = pkt24->u.status0.dseg_0_address;
2482 /* Set total data segment count */
2484 pkt24->dseg_count = cpu_to_le16(prm->seg_cnt);
2486 if (prm->seg_cnt == 0) {
2487 /* No data transfer */
2493 /* If scatter gather */
2495 /* Load command entry data segments */
2497 (cnt < QLA_TGT_DATASEGS_PER_CMD_24XX) && prm->seg_cnt;
2498 cnt++, prm->seg_cnt--) {
2500 cpu_to_le32(pci_dma_lo32(sg_dma_address(prm->sg)));
2502 *dword_ptr++ = cpu_to_le32(pci_dma_hi32(
2503 sg_dma_address(prm->sg)));
2505 *dword_ptr++ = cpu_to_le32(sg_dma_len(prm->sg));
2507 prm->sg = sg_next(prm->sg);
2510 qlt_load_cont_data_segments(prm);
2513 static inline int qlt_has_data(struct qla_tgt_cmd *cmd)
2515 return cmd->bufflen > 0;
2518 static void qlt_print_dif_err(struct qla_tgt_prm *prm)
2520 struct qla_tgt_cmd *cmd;
2521 struct scsi_qla_host *vha;
2523 /* asc 0x10=dif error */
2524 if (prm->sense_buffer && (prm->sense_buffer[12] == 0x10)) {
2528 switch (prm->sense_buffer[13]) {
2530 ql_dbg(ql_dbg_tgt_dif, vha, 0xe00b,
2531 "BE detected Guard TAG ERR: lba[0x%llx|%lld] len[0x%x] "
2532 "se_cmd=%p tag[%x]",
2533 cmd->lba, cmd->lba, cmd->num_blks, &cmd->se_cmd,
2534 cmd->atio.u.isp24.exchange_addr);
2537 ql_dbg(ql_dbg_tgt_dif, vha, 0xe00c,
2538 "BE detected APP TAG ERR: lba[0x%llx|%lld] len[0x%x] "
2539 "se_cmd=%p tag[%x]",
2540 cmd->lba, cmd->lba, cmd->num_blks, &cmd->se_cmd,
2541 cmd->atio.u.isp24.exchange_addr);
2544 ql_dbg(ql_dbg_tgt_dif, vha, 0xe00f,
2545 "BE detected REF TAG ERR: lba[0x%llx|%lld] len[0x%x] "
2546 "se_cmd=%p tag[%x]",
2547 cmd->lba, cmd->lba, cmd->num_blks, &cmd->se_cmd,
2548 cmd->atio.u.isp24.exchange_addr);
2551 ql_dbg(ql_dbg_tgt_dif, vha, 0xe010,
2552 "BE detected Dif ERR: lba[%llx|%lld] len[%x] "
2553 "se_cmd=%p tag[%x]",
2554 cmd->lba, cmd->lba, cmd->num_blks, &cmd->se_cmd,
2555 cmd->atio.u.isp24.exchange_addr);
2558 ql_dump_buffer(ql_dbg_tgt_dif, vha, 0xe011, cmd->cdb, 16);
2563 * Called without ha->hardware_lock held
2565 static int qlt_pre_xmit_response(struct qla_tgt_cmd *cmd,
2566 struct qla_tgt_prm *prm, int xmit_type, uint8_t scsi_status,
2567 uint32_t *full_req_cnt)
2569 struct se_cmd *se_cmd = &cmd->se_cmd;
2570 struct qla_qpair *qpair = cmd->qpair;
2573 prm->tgt = cmd->tgt;
2575 prm->rq_result = scsi_status;
2576 prm->sense_buffer = &cmd->sense_buffer[0];
2577 prm->sense_buffer_len = TRANSPORT_SENSE_BUFFER;
2582 prm->add_status_pkt = 0;
2583 prm->prot_sg = NULL;
2584 prm->prot_seg_cnt = 0;
2587 if ((xmit_type & QLA_TGT_XMIT_DATA) && qlt_has_data(cmd)) {
2588 if (qlt_pci_map_calc_cnt(prm) != 0)
2592 *full_req_cnt = prm->req_cnt;
2594 if (se_cmd->se_cmd_flags & SCF_UNDERFLOW_BIT) {
2595 prm->residual = se_cmd->residual_count;
2596 ql_dbg_qp(ql_dbg_io + ql_dbg_verbose, qpair, 0x305c,
2597 "Residual underflow: %d (tag %lld, op %x, bufflen %d, rq_result %x)\n",
2598 prm->residual, se_cmd->tag,
2599 se_cmd->t_task_cdb ? se_cmd->t_task_cdb[0] : 0,
2600 cmd->bufflen, prm->rq_result);
2601 prm->rq_result |= SS_RESIDUAL_UNDER;
2602 } else if (se_cmd->se_cmd_flags & SCF_OVERFLOW_BIT) {
2603 prm->residual = se_cmd->residual_count;
2604 ql_dbg_qp(ql_dbg_io, qpair, 0x305d,
2605 "Residual overflow: %d (tag %lld, op %x, bufflen %d, rq_result %x)\n",
2606 prm->residual, se_cmd->tag, se_cmd->t_task_cdb ?
2607 se_cmd->t_task_cdb[0] : 0, cmd->bufflen, prm->rq_result);
2608 prm->rq_result |= SS_RESIDUAL_OVER;
2611 if (xmit_type & QLA_TGT_XMIT_STATUS) {
2613 * If QLA_TGT_XMIT_DATA is not set, add_status_pkt will be
2614 * ignored in *xmit_response() below
2616 if (qlt_has_data(cmd)) {
2617 if (QLA_TGT_SENSE_VALID(prm->sense_buffer) ||
2618 (IS_FWI2_CAPABLE(cmd->vha->hw) &&
2619 (prm->rq_result != 0))) {
2620 prm->add_status_pkt = 1;
2629 static inline int qlt_need_explicit_conf(struct qla_tgt_cmd *cmd,
2632 if (cmd->qpair->enable_class_2)
2636 return cmd->conf_compl_supported;
2638 return cmd->qpair->enable_explicit_conf &&
2639 cmd->conf_compl_supported;
2642 static void qlt_24xx_init_ctio_to_isp(struct ctio7_to_24xx *ctio,
2643 struct qla_tgt_prm *prm)
2645 prm->sense_buffer_len = min_t(uint32_t, prm->sense_buffer_len,
2646 (uint32_t)sizeof(ctio->u.status1.sense_data));
2647 ctio->u.status0.flags |= cpu_to_le16(CTIO7_FLAGS_SEND_STATUS);
2648 if (qlt_need_explicit_conf(prm->cmd, 0)) {
2649 ctio->u.status0.flags |= cpu_to_le16(
2650 CTIO7_FLAGS_EXPLICIT_CONFORM |
2651 CTIO7_FLAGS_CONFORM_REQ);
2653 ctio->u.status0.residual = cpu_to_le32(prm->residual);
2654 ctio->u.status0.scsi_status = cpu_to_le16(prm->rq_result);
2655 if (QLA_TGT_SENSE_VALID(prm->sense_buffer)) {
2658 if (qlt_need_explicit_conf(prm->cmd, 1)) {
2659 if ((prm->rq_result & SS_SCSI_STATUS_BYTE) != 0) {
2660 ql_dbg_qp(ql_dbg_tgt, prm->cmd->qpair, 0xe017,
2661 "Skipping EXPLICIT_CONFORM and "
2662 "CTIO7_FLAGS_CONFORM_REQ for FCP READ w/ "
2663 "non GOOD status\n");
2664 goto skip_explict_conf;
2666 ctio->u.status1.flags |= cpu_to_le16(
2667 CTIO7_FLAGS_EXPLICIT_CONFORM |
2668 CTIO7_FLAGS_CONFORM_REQ);
2671 ctio->u.status1.flags &=
2672 ~cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_0);
2673 ctio->u.status1.flags |=
2674 cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_1);
2675 ctio->u.status1.scsi_status |=
2676 cpu_to_le16(SS_SENSE_LEN_VALID);
2677 ctio->u.status1.sense_length =
2678 cpu_to_le16(prm->sense_buffer_len);
2679 for (i = 0; i < prm->sense_buffer_len/4; i++)
2680 ((uint32_t *)ctio->u.status1.sense_data)[i] =
2681 cpu_to_be32(((uint32_t *)prm->sense_buffer)[i]);
2683 qlt_print_dif_err(prm);
2686 ctio->u.status1.flags &=
2687 ~cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_0);
2688 ctio->u.status1.flags |=
2689 cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_1);
2690 ctio->u.status1.sense_length = 0;
2691 memset(ctio->u.status1.sense_data, 0,
2692 sizeof(ctio->u.status1.sense_data));
2695 /* Sense with len > 24, is it possible ??? */
2699 qlt_hba_err_chk_enabled(struct se_cmd *se_cmd)
2701 switch (se_cmd->prot_op) {
2702 case TARGET_PROT_DOUT_INSERT:
2703 case TARGET_PROT_DIN_STRIP:
2704 if (ql2xenablehba_err_chk >= 1)
2707 case TARGET_PROT_DOUT_PASS:
2708 case TARGET_PROT_DIN_PASS:
2709 if (ql2xenablehba_err_chk >= 2)
2712 case TARGET_PROT_DIN_INSERT:
2713 case TARGET_PROT_DOUT_STRIP:
2722 qla_tgt_ref_mask_check(struct se_cmd *se_cmd)
2724 switch (se_cmd->prot_op) {
2725 case TARGET_PROT_DIN_INSERT:
2726 case TARGET_PROT_DOUT_INSERT:
2727 case TARGET_PROT_DIN_STRIP:
2728 case TARGET_PROT_DOUT_STRIP:
2729 case TARGET_PROT_DIN_PASS:
2730 case TARGET_PROT_DOUT_PASS:
2739 * qla_tgt_set_dif_tags - Extract Ref and App tags from SCSI command
2742 qla_tgt_set_dif_tags(struct qla_tgt_cmd *cmd, struct crc_context *ctx,
2743 uint16_t *pfw_prot_opts)
2745 struct se_cmd *se_cmd = &cmd->se_cmd;
2746 uint32_t lba = 0xffffffff & se_cmd->t_task_lba;
2747 scsi_qla_host_t *vha = cmd->tgt->vha;
2748 struct qla_hw_data *ha = vha->hw;
2752 * wait till Mode Sense/Select cmd, modepage Ah, subpage 2
2753 * have been immplemented by TCM, before AppTag is avail.
2754 * Look for modesense_handlers[]
2757 ctx->app_tag_mask[0] = 0x0;
2758 ctx->app_tag_mask[1] = 0x0;
2760 if (IS_PI_UNINIT_CAPABLE(ha)) {
2761 if ((se_cmd->prot_type == TARGET_DIF_TYPE1_PROT) ||
2762 (se_cmd->prot_type == TARGET_DIF_TYPE2_PROT))
2763 *pfw_prot_opts |= PO_DIS_VALD_APP_ESC;
2764 else if (se_cmd->prot_type == TARGET_DIF_TYPE3_PROT)
2765 *pfw_prot_opts |= PO_DIS_VALD_APP_REF_ESC;
2768 t32 = ha->tgt.tgt_ops->get_dif_tags(cmd, pfw_prot_opts);
2770 switch (se_cmd->prot_type) {
2771 case TARGET_DIF_TYPE0_PROT:
2773 * No check for ql2xenablehba_err_chk, as it
2774 * would be an I/O error if hba tag generation
2777 ctx->ref_tag = cpu_to_le32(lba);
2778 /* enable ALL bytes of the ref tag */
2779 ctx->ref_tag_mask[0] = 0xff;
2780 ctx->ref_tag_mask[1] = 0xff;
2781 ctx->ref_tag_mask[2] = 0xff;
2782 ctx->ref_tag_mask[3] = 0xff;
2784 case TARGET_DIF_TYPE1_PROT:
2786 * For TYPE 1 protection: 16 bit GUARD tag, 32 bit
2787 * REF tag, and 16 bit app tag.
2789 ctx->ref_tag = cpu_to_le32(lba);
2790 if (!qla_tgt_ref_mask_check(se_cmd) ||
2791 !(ha->tgt.tgt_ops->chk_dif_tags(t32))) {
2792 *pfw_prot_opts |= PO_DIS_REF_TAG_VALD;
2795 /* enable ALL bytes of the ref tag */
2796 ctx->ref_tag_mask[0] = 0xff;
2797 ctx->ref_tag_mask[1] = 0xff;
2798 ctx->ref_tag_mask[2] = 0xff;
2799 ctx->ref_tag_mask[3] = 0xff;
2801 case TARGET_DIF_TYPE2_PROT:
2803 * For TYPE 2 protection: 16 bit GUARD + 32 bit REF
2804 * tag has to match LBA in CDB + N
2806 ctx->ref_tag = cpu_to_le32(lba);
2807 if (!qla_tgt_ref_mask_check(se_cmd) ||
2808 !(ha->tgt.tgt_ops->chk_dif_tags(t32))) {
2809 *pfw_prot_opts |= PO_DIS_REF_TAG_VALD;
2812 /* enable ALL bytes of the ref tag */
2813 ctx->ref_tag_mask[0] = 0xff;
2814 ctx->ref_tag_mask[1] = 0xff;
2815 ctx->ref_tag_mask[2] = 0xff;
2816 ctx->ref_tag_mask[3] = 0xff;
2818 case TARGET_DIF_TYPE3_PROT:
2819 /* For TYPE 3 protection: 16 bit GUARD only */
2820 *pfw_prot_opts |= PO_DIS_REF_TAG_VALD;
2821 ctx->ref_tag_mask[0] = ctx->ref_tag_mask[1] =
2822 ctx->ref_tag_mask[2] = ctx->ref_tag_mask[3] = 0x00;
2828 qlt_build_ctio_crc2_pkt(struct qla_qpair *qpair, struct qla_tgt_prm *prm)
2831 uint32_t transfer_length = 0;
2832 uint32_t data_bytes;
2834 uint8_t bundling = 1;
2836 struct crc_context *crc_ctx_pkt = NULL;
2837 struct qla_hw_data *ha;
2838 struct ctio_crc2_to_fw *pkt;
2839 dma_addr_t crc_ctx_dma;
2840 uint16_t fw_prot_opts = 0;
2841 struct qla_tgt_cmd *cmd = prm->cmd;
2842 struct se_cmd *se_cmd = &cmd->se_cmd;
2844 struct atio_from_isp *atio = &prm->cmd->atio;
2845 struct qla_tc_param tc;
2847 scsi_qla_host_t *vha = cmd->vha;
2851 pkt = (struct ctio_crc2_to_fw *)qpair->req->ring_ptr;
2853 memset(pkt, 0, sizeof(*pkt));
2855 ql_dbg_qp(ql_dbg_tgt, cmd->qpair, 0xe071,
2856 "qla_target(%d):%s: se_cmd[%p] CRC2 prot_op[0x%x] cmd prot sg:cnt[%p:%x] lba[%llu]\n",
2857 cmd->vp_idx, __func__, se_cmd, se_cmd->prot_op,
2858 prm->prot_sg, prm->prot_seg_cnt, se_cmd->t_task_lba);
2860 if ((se_cmd->prot_op == TARGET_PROT_DIN_INSERT) ||
2861 (se_cmd->prot_op == TARGET_PROT_DOUT_STRIP))
2864 /* Compute dif len and adjust data len to incude protection */
2865 data_bytes = cmd->bufflen;
2866 dif_bytes = (data_bytes / cmd->blk_sz) * 8;
2868 switch (se_cmd->prot_op) {
2869 case TARGET_PROT_DIN_INSERT:
2870 case TARGET_PROT_DOUT_STRIP:
2871 transfer_length = data_bytes;
2872 if (cmd->prot_sg_cnt)
2873 data_bytes += dif_bytes;
2875 case TARGET_PROT_DIN_STRIP:
2876 case TARGET_PROT_DOUT_INSERT:
2877 case TARGET_PROT_DIN_PASS:
2878 case TARGET_PROT_DOUT_PASS:
2879 transfer_length = data_bytes + dif_bytes;
2886 if (!qlt_hba_err_chk_enabled(se_cmd))
2887 fw_prot_opts |= 0x10; /* Disable Guard tag checking */
2888 /* HBA error checking enabled */
2889 else if (IS_PI_UNINIT_CAPABLE(ha)) {
2890 if ((se_cmd->prot_type == TARGET_DIF_TYPE1_PROT) ||
2891 (se_cmd->prot_type == TARGET_DIF_TYPE2_PROT))
2892 fw_prot_opts |= PO_DIS_VALD_APP_ESC;
2893 else if (se_cmd->prot_type == TARGET_DIF_TYPE3_PROT)
2894 fw_prot_opts |= PO_DIS_VALD_APP_REF_ESC;
2897 switch (se_cmd->prot_op) {
2898 case TARGET_PROT_DIN_INSERT:
2899 case TARGET_PROT_DOUT_INSERT:
2900 fw_prot_opts |= PO_MODE_DIF_INSERT;
2902 case TARGET_PROT_DIN_STRIP:
2903 case TARGET_PROT_DOUT_STRIP:
2904 fw_prot_opts |= PO_MODE_DIF_REMOVE;
2906 case TARGET_PROT_DIN_PASS:
2907 case TARGET_PROT_DOUT_PASS:
2908 fw_prot_opts |= PO_MODE_DIF_PASS;
2909 /* FUTURE: does tcm require T10CRC<->IPCKSUM conversion? */
2911 default:/* Normal Request */
2912 fw_prot_opts |= PO_MODE_DIF_PASS;
2917 /* Update entry type to indicate Command Type CRC_2 IOCB */
2918 pkt->entry_type = CTIO_CRC2;
2919 pkt->entry_count = 1;
2920 pkt->vp_index = cmd->vp_idx;
2922 h = qlt_make_handle(qpair);
2923 if (unlikely(h == QLA_TGT_NULL_HANDLE)) {
2925 * CTIO type 7 from the firmware doesn't provide a way to
2926 * know the initiator's LOOP ID, hence we can't find
2927 * the session and, so, the command.
2931 qpair->req->outstanding_cmds[h] = (srb_t *)prm->cmd;
2933 pkt->handle = MAKE_HANDLE(qpair->req->id, h);
2934 pkt->handle |= CTIO_COMPLETION_HANDLE_MARK;
2935 pkt->nport_handle = cpu_to_le16(prm->cmd->loop_id);
2936 pkt->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
2937 pkt->initiator_id[0] = atio->u.isp24.fcp_hdr.s_id[2];
2938 pkt->initiator_id[1] = atio->u.isp24.fcp_hdr.s_id[1];
2939 pkt->initiator_id[2] = atio->u.isp24.fcp_hdr.s_id[0];
2940 pkt->exchange_addr = atio->u.isp24.exchange_addr;
2942 /* silence compile warning */
2943 t16 = be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id);
2944 pkt->ox_id = cpu_to_le16(t16);
2946 t16 = (atio->u.isp24.attr << 9);
2947 pkt->flags |= cpu_to_le16(t16);
2948 pkt->relative_offset = cpu_to_le32(prm->cmd->offset);
2950 /* Set transfer direction */
2951 if (cmd->dma_data_direction == DMA_TO_DEVICE)
2952 pkt->flags = cpu_to_le16(CTIO7_FLAGS_DATA_IN);
2953 else if (cmd->dma_data_direction == DMA_FROM_DEVICE)
2954 pkt->flags = cpu_to_le16(CTIO7_FLAGS_DATA_OUT);
2956 pkt->dseg_count = prm->tot_dsds;
2957 /* Fibre channel byte count */
2958 pkt->transfer_length = cpu_to_le32(transfer_length);
2960 /* ----- CRC context -------- */
2962 /* Allocate CRC context from global pool */
2963 crc_ctx_pkt = cmd->ctx =
2964 dma_pool_alloc(ha->dl_dma_pool, GFP_ATOMIC, &crc_ctx_dma);
2967 goto crc_queuing_error;
2969 /* Zero out CTX area. */
2970 clr_ptr = (uint8_t *)crc_ctx_pkt;
2971 memset(clr_ptr, 0, sizeof(*crc_ctx_pkt));
2973 crc_ctx_pkt->crc_ctx_dma = crc_ctx_dma;
2974 INIT_LIST_HEAD(&crc_ctx_pkt->dsd_list);
2977 crc_ctx_pkt->handle = pkt->handle;
2979 qla_tgt_set_dif_tags(cmd, crc_ctx_pkt, &fw_prot_opts);
2981 pkt->crc_context_address[0] = cpu_to_le32(LSD(crc_ctx_dma));
2982 pkt->crc_context_address[1] = cpu_to_le32(MSD(crc_ctx_dma));
2983 pkt->crc_context_len = CRC_CONTEXT_LEN_FW;
2986 cur_dsd = (uint32_t *) &crc_ctx_pkt->u.nobundling.data_address;
2989 * Configure Bundling if we need to fetch interlaving
2990 * protection PCI accesses
2992 fw_prot_opts |= PO_ENABLE_DIF_BUNDLING;
2993 crc_ctx_pkt->u.bundling.dif_byte_count = cpu_to_le32(dif_bytes);
2994 crc_ctx_pkt->u.bundling.dseg_count =
2995 cpu_to_le16(prm->tot_dsds - prm->prot_seg_cnt);
2996 cur_dsd = (uint32_t *) &crc_ctx_pkt->u.bundling.data_address;
2999 /* Finish the common fields of CRC pkt */
3000 crc_ctx_pkt->blk_size = cpu_to_le16(cmd->blk_sz);
3001 crc_ctx_pkt->prot_opts = cpu_to_le16(fw_prot_opts);
3002 crc_ctx_pkt->byte_count = cpu_to_le32(data_bytes);
3003 crc_ctx_pkt->guard_seed = cpu_to_le16(0);
3005 memset((uint8_t *)&tc, 0 , sizeof(tc));
3007 tc.blk_sz = cmd->blk_sz;
3008 tc.bufflen = cmd->bufflen;
3010 tc.prot_sg = cmd->prot_sg;
3011 tc.ctx = crc_ctx_pkt;
3012 tc.ctx_dsd_alloced = &cmd->ctx_dsd_alloced;
3014 /* Walks data segments */
3015 pkt->flags |= cpu_to_le16(CTIO7_FLAGS_DSD_PTR);
3017 if (!bundling && prm->prot_seg_cnt) {
3018 if (qla24xx_walk_and_build_sglist_no_difb(ha, NULL, cur_dsd,
3019 prm->tot_dsds, &tc))
3020 goto crc_queuing_error;
3021 } else if (qla24xx_walk_and_build_sglist(ha, NULL, cur_dsd,
3022 (prm->tot_dsds - prm->prot_seg_cnt), &tc))
3023 goto crc_queuing_error;
3025 if (bundling && prm->prot_seg_cnt) {
3026 /* Walks dif segments */
3027 pkt->add_flags |= CTIO_CRC2_AF_DIF_DSD_ENA;
3029 cur_dsd = (uint32_t *) &crc_ctx_pkt->u.bundling.dif_address;
3030 if (qla24xx_walk_and_build_prot_sglist(ha, NULL, cur_dsd,
3031 prm->prot_seg_cnt, &tc))
3032 goto crc_queuing_error;
3037 /* Cleanup will be performed by the caller */
3038 qpair->req->outstanding_cmds[h] = NULL;
3040 return QLA_FUNCTION_FAILED;
3044 * Callback to setup response of xmit_type of QLA_TGT_XMIT_DATA and *
3045 * QLA_TGT_XMIT_STATUS for >= 24xx silicon
3047 int qlt_xmit_response(struct qla_tgt_cmd *cmd, int xmit_type,
3048 uint8_t scsi_status)
3050 struct scsi_qla_host *vha = cmd->vha;
3051 struct qla_qpair *qpair = cmd->qpair;
3052 struct ctio7_to_24xx *pkt;
3053 struct qla_tgt_prm prm;
3054 uint32_t full_req_cnt = 0;
3055 unsigned long flags = 0;
3058 if (cmd->sess && cmd->sess->deleted) {
3059 cmd->state = QLA_TGT_STATE_PROCESSED;
3060 if (cmd->sess->logout_completed)
3061 /* no need to terminate. FW already freed exchange. */
3062 qlt_abort_cmd_on_host_reset(cmd->vha, cmd);
3064 qlt_send_term_exchange(qpair, cmd, &cmd->atio, 0, 0);
3068 ql_dbg_qp(ql_dbg_tgt, qpair, 0xe018,
3069 "is_send_status=%d, cmd->bufflen=%d, cmd->sg_cnt=%d, cmd->dma_data_direction=%d se_cmd[%p] qp %d\n",
3070 (xmit_type & QLA_TGT_XMIT_STATUS) ?
3071 1 : 0, cmd->bufflen, cmd->sg_cnt, cmd->dma_data_direction,
3072 &cmd->se_cmd, qpair->id);
3074 res = qlt_pre_xmit_response(cmd, &prm, xmit_type, scsi_status,
3076 if (unlikely(res != 0)) {
3080 spin_lock_irqsave(qpair->qp_lock_ptr, flags);
3082 if (xmit_type == QLA_TGT_XMIT_STATUS)
3083 qpair->tgt_counters.core_qla_snd_status++;
3085 qpair->tgt_counters.core_qla_que_buf++;
3087 if (!qpair->fw_started || cmd->reset_count != qpair->chip_reset) {
3089 * Either the port is not online or this request was from
3090 * previous life, just abort the processing.
3092 cmd->state = QLA_TGT_STATE_PROCESSED;
3093 qlt_abort_cmd_on_host_reset(cmd->vha, cmd);
3094 ql_dbg_qp(ql_dbg_async, qpair, 0xe101,
3095 "RESET-RSP online/active/old-count/new-count = %d/%d/%d/%d.\n",
3096 vha->flags.online, qla2x00_reset_active(vha),
3097 cmd->reset_count, qpair->chip_reset);
3098 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
3102 /* Does F/W have an IOCBs for this request */
3103 res = qlt_check_reserve_free_req(qpair, full_req_cnt);
3105 goto out_unmap_unlock;
3107 if (cmd->se_cmd.prot_op && (xmit_type & QLA_TGT_XMIT_DATA))
3108 res = qlt_build_ctio_crc2_pkt(qpair, &prm);
3110 res = qlt_24xx_build_ctio_pkt(qpair, &prm);
3111 if (unlikely(res != 0)) {
3112 qpair->req->cnt += full_req_cnt;
3113 goto out_unmap_unlock;
3116 pkt = (struct ctio7_to_24xx *)prm.pkt;
3118 if (qlt_has_data(cmd) && (xmit_type & QLA_TGT_XMIT_DATA)) {
3119 pkt->u.status0.flags |=
3120 cpu_to_le16(CTIO7_FLAGS_DATA_IN |
3121 CTIO7_FLAGS_STATUS_MODE_0);
3123 if (cmd->se_cmd.prot_op == TARGET_PROT_NORMAL)
3124 qlt_load_data_segments(&prm);
3126 if (prm.add_status_pkt == 0) {
3127 if (xmit_type & QLA_TGT_XMIT_STATUS) {
3128 pkt->u.status0.scsi_status =
3129 cpu_to_le16(prm.rq_result);
3130 pkt->u.status0.residual =
3131 cpu_to_le32(prm.residual);
3132 pkt->u.status0.flags |= cpu_to_le16(
3133 CTIO7_FLAGS_SEND_STATUS);
3134 if (qlt_need_explicit_conf(cmd, 0)) {
3135 pkt->u.status0.flags |=
3137 CTIO7_FLAGS_EXPLICIT_CONFORM |
3138 CTIO7_FLAGS_CONFORM_REQ);
3144 * We have already made sure that there is sufficient
3145 * amount of request entries to not drop HW lock in
3148 struct ctio7_to_24xx *ctio =
3149 (struct ctio7_to_24xx *)qlt_get_req_pkt(
3152 ql_dbg_qp(ql_dbg_tgt, qpair, 0x305e,
3153 "Building additional status packet 0x%p.\n",
3157 * T10Dif: ctio_crc2_to_fw overlay ontop of
3160 memcpy(ctio, pkt, sizeof(*ctio));
3161 /* reset back to CTIO7 */
3162 ctio->entry_count = 1;
3163 ctio->entry_type = CTIO_TYPE7;
3164 ctio->dseg_count = 0;
3165 ctio->u.status1.flags &= ~cpu_to_le16(
3166 CTIO7_FLAGS_DATA_IN);
3168 /* Real finish is ctio_m1's finish */
3169 pkt->handle |= CTIO_INTERMEDIATE_HANDLE_MARK;
3170 pkt->u.status0.flags |= cpu_to_le16(
3171 CTIO7_FLAGS_DONT_RET_CTIO);
3173 /* qlt_24xx_init_ctio_to_isp will correct
3174 * all neccessary fields that's part of CTIO7.
3175 * There should be no residual of CTIO-CRC2 data.
3177 qlt_24xx_init_ctio_to_isp((struct ctio7_to_24xx *)ctio,
3181 qlt_24xx_init_ctio_to_isp(pkt, &prm);
3184 cmd->state = QLA_TGT_STATE_PROCESSED; /* Mid-level is done processing */
3185 cmd->cmd_sent_to_fw = 1;
3187 /* Memory Barrier */
3189 if (qpair->reqq_start_iocbs)
3190 qpair->reqq_start_iocbs(qpair);
3192 qla2x00_start_iocbs(vha, qpair->req);
3193 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
3198 qlt_unmap_sg(vha, cmd);
3199 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
3203 EXPORT_SYMBOL(qlt_xmit_response);
3205 int qlt_rdy_to_xfer(struct qla_tgt_cmd *cmd)
3207 struct ctio7_to_24xx *pkt;
3208 struct scsi_qla_host *vha = cmd->vha;
3209 struct qla_tgt *tgt = cmd->tgt;
3210 struct qla_tgt_prm prm;
3211 unsigned long flags = 0;
3213 struct qla_qpair *qpair = cmd->qpair;
3215 memset(&prm, 0, sizeof(prm));
3221 /* Calculate number of entries and segments required */
3222 if (qlt_pci_map_calc_cnt(&prm) != 0)
3225 if (!qpair->fw_started || (cmd->reset_count != qpair->chip_reset) ||
3226 (cmd->sess && cmd->sess->deleted)) {
3228 * Either the port is not online or this request was from
3229 * previous life, just abort the processing.
3231 cmd->state = QLA_TGT_STATE_NEED_DATA;
3232 qlt_abort_cmd_on_host_reset(cmd->vha, cmd);
3233 ql_dbg_qp(ql_dbg_async, qpair, 0xe102,
3234 "RESET-XFR online/active/old-count/new-count = %d/%d/%d/%d.\n",
3235 vha->flags.online, qla2x00_reset_active(vha),
3236 cmd->reset_count, qpair->chip_reset);
3240 spin_lock_irqsave(qpair->qp_lock_ptr, flags);
3241 /* Does F/W have an IOCBs for this request */
3242 res = qlt_check_reserve_free_req(qpair, prm.req_cnt);
3244 goto out_unlock_free_unmap;
3245 if (cmd->se_cmd.prot_op)
3246 res = qlt_build_ctio_crc2_pkt(qpair, &prm);
3248 res = qlt_24xx_build_ctio_pkt(qpair, &prm);
3250 if (unlikely(res != 0)) {
3251 qpair->req->cnt += prm.req_cnt;
3252 goto out_unlock_free_unmap;
3255 pkt = (struct ctio7_to_24xx *)prm.pkt;
3256 pkt->u.status0.flags |= cpu_to_le16(CTIO7_FLAGS_DATA_OUT |
3257 CTIO7_FLAGS_STATUS_MODE_0);
3259 if (cmd->se_cmd.prot_op == TARGET_PROT_NORMAL)
3260 qlt_load_data_segments(&prm);
3262 cmd->state = QLA_TGT_STATE_NEED_DATA;
3263 cmd->cmd_sent_to_fw = 1;
3265 /* Memory Barrier */
3267 if (qpair->reqq_start_iocbs)
3268 qpair->reqq_start_iocbs(qpair);
3270 qla2x00_start_iocbs(vha, qpair->req);
3271 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
3275 out_unlock_free_unmap:
3276 qlt_unmap_sg(vha, cmd);
3277 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
3281 EXPORT_SYMBOL(qlt_rdy_to_xfer);
3285 * it is assumed either hardware_lock or qpair lock is held.
3288 qlt_handle_dif_error(struct qla_qpair *qpair, struct qla_tgt_cmd *cmd,
3289 struct ctio_crc_from_fw *sts)
3291 uint8_t *ap = &sts->actual_dif[0];
3292 uint8_t *ep = &sts->expected_dif[0];
3293 uint64_t lba = cmd->se_cmd.t_task_lba;
3294 uint8_t scsi_status, sense_key, asc, ascq;
3295 unsigned long flags;
3296 struct scsi_qla_host *vha = cmd->vha;
3298 cmd->trc_flags |= TRC_DIF_ERR;
3300 cmd->a_guard = be16_to_cpu(*(uint16_t *)(ap + 0));
3301 cmd->a_app_tag = be16_to_cpu(*(uint16_t *)(ap + 2));
3302 cmd->a_ref_tag = be32_to_cpu(*(uint32_t *)(ap + 4));
3304 cmd->e_guard = be16_to_cpu(*(uint16_t *)(ep + 0));
3305 cmd->e_app_tag = be16_to_cpu(*(uint16_t *)(ep + 2));
3306 cmd->e_ref_tag = be32_to_cpu(*(uint32_t *)(ep + 4));
3308 ql_dbg(ql_dbg_tgt_dif, vha, 0xf075,
3309 "%s: aborted %d state %d\n", __func__, cmd->aborted, cmd->state);
3311 scsi_status = sense_key = asc = ascq = 0;
3313 /* check appl tag */
3314 if (cmd->e_app_tag != cmd->a_app_tag) {
3315 ql_dbg(ql_dbg_tgt_dif, vha, 0xe00d,
3316 "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]",
3317 cmd->cdb[0], lba, (lba+cmd->num_blks), cmd->num_blks,
3318 cmd->a_ref_tag, cmd->e_ref_tag, cmd->a_app_tag,
3319 cmd->e_app_tag, cmd->a_guard, cmd->e_guard, cmd,
3320 cmd->atio.u.isp24.fcp_hdr.ox_id);
3322 cmd->dif_err_code = DIF_ERR_APP;
3323 scsi_status = SAM_STAT_CHECK_CONDITION;
3324 sense_key = ABORTED_COMMAND;
3330 if (cmd->e_ref_tag != cmd->a_ref_tag) {
3331 ql_dbg(ql_dbg_tgt_dif, vha, 0xe00e,
3332 "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] ",
3333 cmd->cdb[0], lba, (lba+cmd->num_blks), cmd->num_blks,
3334 cmd->a_ref_tag, cmd->e_ref_tag, cmd->a_app_tag,
3335 cmd->e_app_tag, cmd->a_guard, cmd->e_guard, cmd,
3336 cmd->atio.u.isp24.fcp_hdr.ox_id);
3338 cmd->dif_err_code = DIF_ERR_REF;
3339 scsi_status = SAM_STAT_CHECK_CONDITION;
3340 sense_key = ABORTED_COMMAND;
3347 if (cmd->e_guard != cmd->a_guard) {
3348 ql_dbg(ql_dbg_tgt_dif, vha, 0xe012,
3349 "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]",
3350 cmd->cdb[0], lba, (lba+cmd->num_blks), cmd->num_blks,
3351 cmd->a_ref_tag, cmd->e_ref_tag, cmd->a_app_tag,
3352 cmd->e_app_tag, cmd->a_guard, cmd->e_guard, cmd,
3353 cmd->atio.u.isp24.fcp_hdr.ox_id);
3355 cmd->dif_err_code = DIF_ERR_GRD;
3356 scsi_status = SAM_STAT_CHECK_CONDITION;
3357 sense_key = ABORTED_COMMAND;
3362 switch (cmd->state) {
3363 case QLA_TGT_STATE_NEED_DATA:
3364 /* handle_data will load DIF error code */
3365 cmd->state = QLA_TGT_STATE_DATA_IN;
3366 vha->hw->tgt.tgt_ops->handle_data(cmd);
3369 spin_lock_irqsave(&cmd->cmd_lock, flags);
3371 spin_unlock_irqrestore(&cmd->cmd_lock, flags);
3372 vha->hw->tgt.tgt_ops->free_cmd(cmd);
3375 spin_unlock_irqrestore(&cmd->cmd_lock, flags);
3377 qlt_send_resp_ctio(qpair, cmd, scsi_status, sense_key, asc,
3379 /* assume scsi status gets out on the wire.
3380 * Will not wait for completion.
3382 vha->hw->tgt.tgt_ops->free_cmd(cmd);
3387 /* If hardware_lock held on entry, might drop it, then reaquire */
3388 /* This function sends the appropriate CTIO to ISP 2xxx or 24xx */
3389 static int __qlt_send_term_imm_notif(struct scsi_qla_host *vha,
3390 struct imm_ntfy_from_isp *ntfy)
3392 struct nack_to_isp *nack;
3393 struct qla_hw_data *ha = vha->hw;
3397 ql_dbg(ql_dbg_tgt_tmr, vha, 0xe01c,
3398 "Sending TERM ELS CTIO (ha=%p)\n", ha);
3400 pkt = (request_t *)qla2x00_alloc_iocbs(vha, NULL);
3402 ql_dbg(ql_dbg_tgt, vha, 0xe080,
3403 "qla_target(%d): %s failed: unable to allocate "
3404 "request packet\n", vha->vp_idx, __func__);
3408 pkt->entry_type = NOTIFY_ACK_TYPE;
3409 pkt->entry_count = 1;
3410 pkt->handle = QLA_TGT_SKIP_HANDLE;
3412 nack = (struct nack_to_isp *)pkt;
3413 nack->ox_id = ntfy->ox_id;
3415 nack->u.isp24.nport_handle = ntfy->u.isp24.nport_handle;
3416 if (le16_to_cpu(ntfy->u.isp24.status) == IMM_NTFY_ELS) {
3417 nack->u.isp24.flags = ntfy->u.isp24.flags &
3418 __constant_cpu_to_le32(NOTIFY24XX_FLAGS_PUREX_IOCB);
3422 nack->u.isp24.flags |=
3423 __constant_cpu_to_le16(NOTIFY_ACK_FLAGS_TERMINATE);
3425 nack->u.isp24.srr_rx_id = ntfy->u.isp24.srr_rx_id;
3426 nack->u.isp24.status = ntfy->u.isp24.status;
3427 nack->u.isp24.status_subcode = ntfy->u.isp24.status_subcode;
3428 nack->u.isp24.fw_handle = ntfy->u.isp24.fw_handle;
3429 nack->u.isp24.exchange_address = ntfy->u.isp24.exchange_address;
3430 nack->u.isp24.srr_rel_offs = ntfy->u.isp24.srr_rel_offs;
3431 nack->u.isp24.srr_ui = ntfy->u.isp24.srr_ui;
3432 nack->u.isp24.vp_index = ntfy->u.isp24.vp_index;
3434 qla2x00_start_iocbs(vha, vha->req);
3438 static void qlt_send_term_imm_notif(struct scsi_qla_host *vha,
3439 struct imm_ntfy_from_isp *imm, int ha_locked)
3441 unsigned long flags = 0;
3445 rc = __qlt_send_term_imm_notif(vha, imm);
3449 qlt_alloc_qfull_cmd(vha, imm, 0, 0);
3457 spin_lock_irqsave(&vha->hw->hardware_lock, flags);
3458 rc = __qlt_send_term_imm_notif(vha, imm);
3462 qlt_alloc_qfull_cmd(vha, imm, 0, 0);
3467 spin_unlock_irqrestore(&vha->hw->hardware_lock, flags);
3471 * If hardware_lock held on entry, might drop it, then reaquire
3472 * This function sends the appropriate CTIO to ISP 2xxx or 24xx
3474 static int __qlt_send_term_exchange(struct qla_qpair *qpair,
3475 struct qla_tgt_cmd *cmd,
3476 struct atio_from_isp *atio)
3478 struct scsi_qla_host *vha = qpair->vha;
3479 struct ctio7_to_24xx *ctio24;
3480 struct qla_hw_data *ha = vha->hw;
3485 ql_dbg(ql_dbg_tgt, vha, 0xe009, "Sending TERM EXCH CTIO (ha=%p)\n", ha);
3490 pkt = (request_t *)qla2x00_alloc_iocbs_ready(qpair, NULL);
3492 ql_dbg(ql_dbg_tgt, vha, 0xe050,
3493 "qla_target(%d): %s failed: unable to allocate "
3494 "request packet\n", vha->vp_idx, __func__);
3499 if (cmd->state < QLA_TGT_STATE_PROCESSED) {
3500 ql_dbg(ql_dbg_tgt, vha, 0xe051,
3501 "qla_target(%d): Terminating cmd %p with "
3502 "incorrect state %d\n", vha->vp_idx, cmd,
3508 qpair->tgt_counters.num_term_xchg_sent++;
3509 pkt->entry_count = 1;
3510 pkt->handle = QLA_TGT_SKIP_HANDLE | CTIO_COMPLETION_HANDLE_MARK;
3512 ctio24 = (struct ctio7_to_24xx *)pkt;
3513 ctio24->entry_type = CTIO_TYPE7;
3514 ctio24->nport_handle = CTIO7_NHANDLE_UNRECOGNIZED;
3515 ctio24->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
3516 ctio24->vp_index = vha->vp_idx;
3517 ctio24->initiator_id[0] = atio->u.isp24.fcp_hdr.s_id[2];
3518 ctio24->initiator_id[1] = atio->u.isp24.fcp_hdr.s_id[1];
3519 ctio24->initiator_id[2] = atio->u.isp24.fcp_hdr.s_id[0];
3520 ctio24->exchange_addr = atio->u.isp24.exchange_addr;
3521 temp = (atio->u.isp24.attr << 9) | CTIO7_FLAGS_STATUS_MODE_1 |
3522 CTIO7_FLAGS_TERMINATE;
3523 ctio24->u.status1.flags = cpu_to_le16(temp);
3524 temp = be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id);
3525 ctio24->u.status1.ox_id = cpu_to_le16(temp);
3527 /* Most likely, it isn't needed */
3528 ctio24->u.status1.residual = get_unaligned((uint32_t *)
3529 &atio->u.isp24.fcp_cmnd.add_cdb[
3530 atio->u.isp24.fcp_cmnd.add_cdb_len]);
3531 if (ctio24->u.status1.residual != 0)
3532 ctio24->u.status1.scsi_status |= SS_RESIDUAL_UNDER;
3534 /* Memory Barrier */
3536 if (qpair->reqq_start_iocbs)
3537 qpair->reqq_start_iocbs(qpair);
3539 qla2x00_start_iocbs(vha, qpair->req);
3543 static void qlt_send_term_exchange(struct qla_qpair *qpair,
3544 struct qla_tgt_cmd *cmd, struct atio_from_isp *atio, int ha_locked,
3547 struct scsi_qla_host *vha;
3548 unsigned long flags = 0;
3551 /* why use different vha? NPIV */
3558 rc = __qlt_send_term_exchange(qpair, cmd, atio);
3560 qlt_alloc_qfull_cmd(vha, atio, 0, 0);
3563 spin_lock_irqsave(qpair->qp_lock_ptr, flags);
3564 rc = __qlt_send_term_exchange(qpair, cmd, atio);
3566 qlt_alloc_qfull_cmd(vha, atio, 0, 0);
3569 if (cmd && !ul_abort && !cmd->aborted) {
3571 qlt_unmap_sg(vha, cmd);
3572 vha->hw->tgt.tgt_ops->free_cmd(cmd);
3576 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
3581 static void qlt_init_term_exchange(struct scsi_qla_host *vha)
3583 struct list_head free_list;
3584 struct qla_tgt_cmd *cmd, *tcmd;
3586 vha->hw->tgt.leak_exchg_thresh_hold =
3587 (vha->hw->cur_fw_xcb_count/100) * LEAK_EXCHG_THRESH_HOLD_PERCENT;
3590 if (!list_empty(&vha->hw->tgt.q_full_list)) {
3591 INIT_LIST_HEAD(&free_list);
3592 list_splice_init(&vha->hw->tgt.q_full_list, &free_list);
3594 list_for_each_entry_safe(cmd, tcmd, &free_list, cmd_list) {
3595 list_del(&cmd->cmd_list);
3596 /* This cmd was never sent to TCM. There is no need
3597 * to schedule free or call free_cmd
3600 vha->hw->tgt.num_qfull_cmds_alloc--;
3603 vha->hw->tgt.num_qfull_cmds_dropped = 0;
3606 static void qlt_chk_exch_leak_thresh_hold(struct scsi_qla_host *vha)
3608 uint32_t total_leaked;
3610 total_leaked = vha->hw->tgt.num_qfull_cmds_dropped;
3612 if (vha->hw->tgt.leak_exchg_thresh_hold &&
3613 (total_leaked > vha->hw->tgt.leak_exchg_thresh_hold)) {
3615 ql_dbg(ql_dbg_tgt, vha, 0xe079,
3616 "Chip reset due to exchange starvation: %d/%d.\n",
3617 total_leaked, vha->hw->cur_fw_xcb_count);
3619 if (IS_P3P_TYPE(vha->hw))
3620 set_bit(FCOE_CTX_RESET_NEEDED, &vha->dpc_flags);
3622 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
3623 qla2xxx_wake_dpc(vha);
3628 int qlt_abort_cmd(struct qla_tgt_cmd *cmd)
3630 struct qla_tgt *tgt = cmd->tgt;
3631 struct scsi_qla_host *vha = tgt->vha;
3632 struct se_cmd *se_cmd = &cmd->se_cmd;
3633 unsigned long flags;
3635 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf014,
3636 "qla_target(%d): terminating exchange for aborted cmd=%p "
3637 "(se_cmd=%p, tag=%llu)", vha->vp_idx, cmd, &cmd->se_cmd,
3640 spin_lock_irqsave(&cmd->cmd_lock, flags);
3642 spin_unlock_irqrestore(&cmd->cmd_lock, flags);
3644 * It's normal to see 2 calls in this path:
3645 * 1) XFER Rdy completion + CMD_T_ABORT
3646 * 2) TCM TMR - drain_state_list
3648 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf016,
3649 "multiple abort. %p transport_state %x, t_state %x, "
3650 "se_cmd_flags %x\n", cmd, cmd->se_cmd.transport_state,
3651 cmd->se_cmd.t_state, cmd->se_cmd.se_cmd_flags);
3655 cmd->trc_flags |= TRC_ABORT;
3656 spin_unlock_irqrestore(&cmd->cmd_lock, flags);
3658 qlt_send_term_exchange(cmd->qpair, cmd, &cmd->atio, 0, 1);
3661 EXPORT_SYMBOL(qlt_abort_cmd);
3663 void qlt_free_cmd(struct qla_tgt_cmd *cmd)
3665 struct fc_port *sess = cmd->sess;
3667 ql_dbg(ql_dbg_tgt, cmd->vha, 0xe074,
3668 "%s: se_cmd[%p] ox_id %04x\n",
3669 __func__, &cmd->se_cmd,
3670 be16_to_cpu(cmd->atio.u.isp24.fcp_hdr.ox_id));
3672 BUG_ON(cmd->cmd_in_wq);
3675 qlt_unmap_sg(cmd->vha, cmd);
3678 qlt_decr_num_pend_cmds(cmd->vha);
3680 BUG_ON(cmd->sg_mapped);
3681 cmd->jiffies_at_free = get_jiffies_64();
3682 if (unlikely(cmd->free_sg))
3685 if (!sess || !sess->se_sess) {
3689 cmd->jiffies_at_free = get_jiffies_64();
3690 percpu_ida_free(&sess->se_sess->sess_tag_pool, cmd->se_cmd.map_tag);
3692 EXPORT_SYMBOL(qlt_free_cmd);
3695 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
3697 static int qlt_term_ctio_exchange(struct qla_qpair *qpair, void *ctio,
3698 struct qla_tgt_cmd *cmd, uint32_t status)
3701 struct scsi_qla_host *vha = qpair->vha;
3703 if (cmd->se_cmd.prot_op)
3704 ql_dbg(ql_dbg_tgt_dif, vha, 0xe013,
3705 "Term DIF cmd: lba[0x%llx|%lld] len[0x%x] "
3706 "se_cmd=%p tag[%x] op %#x/%s",
3708 cmd->num_blks, &cmd->se_cmd,
3709 cmd->atio.u.isp24.exchange_addr,
3710 cmd->se_cmd.prot_op,
3711 prot_op_str(cmd->se_cmd.prot_op));
3714 struct ctio7_from_24xx *c = (struct ctio7_from_24xx *)ctio;
3716 cpu_to_le16(OF_TERM_EXCH));
3721 qlt_send_term_exchange(qpair, cmd, &cmd->atio, 1, 0);
3727 /* ha->hardware_lock supposed to be held on entry */
3728 static struct qla_tgt_cmd *qlt_ctio_to_cmd(struct scsi_qla_host *vha,
3729 struct rsp_que *rsp, uint32_t handle, void *ctio)
3731 struct qla_tgt_cmd *cmd = NULL;
3732 struct req_que *req;
3733 int qid = GET_QID(handle);
3734 uint32_t h = handle & ~QLA_TGT_HANDLE_MASK;
3736 if (unlikely(h == QLA_TGT_SKIP_HANDLE))
3739 if (qid == rsp->req->id) {
3741 } else if (vha->hw->req_q_map[qid]) {
3742 ql_dbg(ql_dbg_tgt_mgt, vha, 0x1000a,
3743 "qla_target(%d): CTIO completion with different QID %d handle %x\n",
3744 vha->vp_idx, rsp->id, handle);
3745 req = vha->hw->req_q_map[qid];
3750 h &= QLA_CMD_HANDLE_MASK;
3752 if (h != QLA_TGT_NULL_HANDLE) {
3753 if (unlikely(h >= req->num_outstanding_cmds)) {
3754 ql_dbg(ql_dbg_tgt, vha, 0xe052,
3755 "qla_target(%d): Wrong handle %x received\n",
3756 vha->vp_idx, handle);
3760 cmd = (struct qla_tgt_cmd *)req->outstanding_cmds[h];
3761 if (unlikely(cmd == NULL)) {
3762 ql_dbg(ql_dbg_async, vha, 0xe053,
3763 "qla_target(%d): Suspicious: unable to find the command with handle %x req->id %d rsp->id %d\n",
3764 vha->vp_idx, handle, req->id, rsp->id);
3767 req->outstanding_cmds[h] = NULL;
3768 } else if (ctio != NULL) {
3769 /* We can't get loop ID from CTIO7 */
3770 ql_dbg(ql_dbg_tgt, vha, 0xe054,
3771 "qla_target(%d): Wrong CTIO received: QLA24xx doesn't "
3772 "support NULL handles\n", vha->vp_idx);
3779 /* hardware_lock should be held by caller. */
3781 qlt_abort_cmd_on_host_reset(struct scsi_qla_host *vha, struct qla_tgt_cmd *cmd)
3783 struct qla_hw_data *ha = vha->hw;
3786 qlt_unmap_sg(vha, cmd);
3788 /* TODO: fix debug message type and ids. */
3789 if (cmd->state == QLA_TGT_STATE_PROCESSED) {
3790 ql_dbg(ql_dbg_io, vha, 0xff00,
3791 "HOST-ABORT: state=PROCESSED.\n");
3792 } else if (cmd->state == QLA_TGT_STATE_NEED_DATA) {
3793 cmd->write_data_transferred = 0;
3794 cmd->state = QLA_TGT_STATE_DATA_IN;
3796 ql_dbg(ql_dbg_io, vha, 0xff01,
3797 "HOST-ABORT: state=DATA_IN.\n");
3799 ha->tgt.tgt_ops->handle_data(cmd);
3802 ql_dbg(ql_dbg_io, vha, 0xff03,
3803 "HOST-ABORT: state=BAD(%d).\n",
3808 cmd->trc_flags |= TRC_FLUSH;
3809 ha->tgt.tgt_ops->free_cmd(cmd);
3813 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
3815 static void qlt_do_ctio_completion(struct scsi_qla_host *vha,
3816 struct rsp_que *rsp, uint32_t handle, uint32_t status, void *ctio)
3818 struct qla_hw_data *ha = vha->hw;
3819 struct se_cmd *se_cmd;
3820 struct qla_tgt_cmd *cmd;
3821 struct qla_qpair *qpair = rsp->qpair;
3823 if (handle & CTIO_INTERMEDIATE_HANDLE_MARK) {
3824 /* That could happen only in case of an error/reset/abort */
3825 if (status != CTIO_SUCCESS) {
3826 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01d,
3827 "Intermediate CTIO received"
3828 " (status %x)\n", status);
3833 cmd = qlt_ctio_to_cmd(vha, rsp, handle, ctio);
3837 se_cmd = &cmd->se_cmd;
3838 cmd->cmd_sent_to_fw = 0;
3840 qlt_unmap_sg(vha, cmd);
3842 if (unlikely(status != CTIO_SUCCESS)) {
3843 switch (status & 0xFFFF) {
3844 case CTIO_LIP_RESET:
3845 case CTIO_TARGET_RESET:
3847 /* driver request abort via Terminate exchange */
3849 case CTIO_INVALID_RX_ID:
3851 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf058,
3852 "qla_target(%d): CTIO with "
3853 "status %#x received, state %x, se_cmd %p, "
3854 "(LIP_RESET=e, ABORTED=2, TARGET_RESET=17, "
3855 "TIMEOUT=b, INVALID_RX_ID=8)\n", vha->vp_idx,
3856 status, cmd->state, se_cmd);
3859 case CTIO_PORT_LOGGED_OUT:
3860 case CTIO_PORT_UNAVAILABLE:
3863 (status & 0xFFFF) == CTIO_PORT_LOGGED_OUT;
3865 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf059,
3866 "qla_target(%d): CTIO with %s status %x "
3867 "received (state %x, se_cmd %p)\n", vha->vp_idx,
3868 logged_out ? "PORT LOGGED OUT" : "PORT UNAVAILABLE",
3869 status, cmd->state, se_cmd);
3871 if (logged_out && cmd->sess) {
3873 * Session is already logged out, but we need
3874 * to notify initiator, who's not aware of this
3876 cmd->sess->logout_on_delete = 0;
3877 cmd->sess->send_els_logo = 1;
3878 ql_dbg(ql_dbg_disc, vha, 0x20f8,
3879 "%s %d %8phC post del sess\n",
3880 __func__, __LINE__, cmd->sess->port_name);
3882 qlt_schedule_sess_for_deletion_lock(cmd->sess);
3886 case CTIO_DIF_ERROR: {
3887 struct ctio_crc_from_fw *crc =
3888 (struct ctio_crc_from_fw *)ctio;
3889 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf073,
3890 "qla_target(%d): CTIO with DIF_ERROR status %x "
3891 "received (state %x, ulp_cmd %p) actual_dif[0x%llx] "
3892 "expect_dif[0x%llx]\n",
3893 vha->vp_idx, status, cmd->state, se_cmd,
3894 *((u64 *)&crc->actual_dif[0]),
3895 *((u64 *)&crc->expected_dif[0]));
3897 qlt_handle_dif_error(qpair, cmd, ctio);
3901 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05b,
3902 "qla_target(%d): CTIO with error status 0x%x received (state %x, se_cmd %p\n",
3903 vha->vp_idx, status, cmd->state, se_cmd);
3908 /* "cmd->aborted" means
3909 * cmd is already aborted/terminated, we don't
3910 * need to terminate again. The exchange is already
3911 * cleaned up/freed at FW level. Just cleanup at driver
3914 if ((cmd->state != QLA_TGT_STATE_NEED_DATA) &&
3916 cmd->trc_flags |= TRC_CTIO_ERR;
3917 if (qlt_term_ctio_exchange(qpair, ctio, cmd, status))
3922 if (cmd->state == QLA_TGT_STATE_PROCESSED) {
3923 cmd->trc_flags |= TRC_CTIO_DONE;
3924 } else if (cmd->state == QLA_TGT_STATE_NEED_DATA) {
3925 cmd->state = QLA_TGT_STATE_DATA_IN;
3927 if (status == CTIO_SUCCESS)
3928 cmd->write_data_transferred = 1;
3930 ha->tgt.tgt_ops->handle_data(cmd);
3932 } else if (cmd->aborted) {
3933 cmd->trc_flags |= TRC_CTIO_ABORTED;
3934 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01e,
3935 "Aborted command %p (tag %lld) finished\n", cmd, se_cmd->tag);
3937 cmd->trc_flags |= TRC_CTIO_STRANGE;
3938 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05c,
3939 "qla_target(%d): A command in state (%d) should "
3940 "not return a CTIO complete\n", vha->vp_idx, cmd->state);
3943 if (unlikely(status != CTIO_SUCCESS) &&
3945 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01f, "Finishing failed CTIO\n");
3949 ha->tgt.tgt_ops->free_cmd(cmd);
3952 static inline int qlt_get_fcp_task_attr(struct scsi_qla_host *vha,
3957 switch (task_codes) {
3958 case ATIO_SIMPLE_QUEUE:
3959 fcp_task_attr = TCM_SIMPLE_TAG;
3961 case ATIO_HEAD_OF_QUEUE:
3962 fcp_task_attr = TCM_HEAD_TAG;
3964 case ATIO_ORDERED_QUEUE:
3965 fcp_task_attr = TCM_ORDERED_TAG;
3967 case ATIO_ACA_QUEUE:
3968 fcp_task_attr = TCM_ACA_TAG;
3971 fcp_task_attr = TCM_SIMPLE_TAG;
3974 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05d,
3975 "qla_target: unknown task code %x, use ORDERED instead\n",
3977 fcp_task_attr = TCM_ORDERED_TAG;
3981 return fcp_task_attr;
3984 static struct fc_port *qlt_make_local_sess(struct scsi_qla_host *,
3987 * Process context for I/O path into tcm_qla2xxx code
3989 static void __qlt_do_work(struct qla_tgt_cmd *cmd)
3991 scsi_qla_host_t *vha = cmd->vha;
3992 struct qla_hw_data *ha = vha->hw;
3993 struct fc_port *sess = cmd->sess;
3994 struct atio_from_isp *atio = &cmd->atio;
3996 unsigned long flags;
3997 uint32_t data_length;
3998 int ret, fcp_task_attr, data_dir, bidi = 0;
3999 struct qla_qpair *qpair = cmd->qpair;
4002 cmd->trc_flags |= TRC_DO_WORK;
4005 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf082,
4006 "cmd with tag %u is aborted\n",
4007 cmd->atio.u.isp24.exchange_addr);
4011 spin_lock_init(&cmd->cmd_lock);
4012 cdb = &atio->u.isp24.fcp_cmnd.cdb[0];
4013 cmd->se_cmd.tag = atio->u.isp24.exchange_addr;
4015 if (atio->u.isp24.fcp_cmnd.rddata &&
4016 atio->u.isp24.fcp_cmnd.wrdata) {
4018 data_dir = DMA_TO_DEVICE;
4019 } else if (atio->u.isp24.fcp_cmnd.rddata)
4020 data_dir = DMA_FROM_DEVICE;
4021 else if (atio->u.isp24.fcp_cmnd.wrdata)
4022 data_dir = DMA_TO_DEVICE;
4024 data_dir = DMA_NONE;
4026 fcp_task_attr = qlt_get_fcp_task_attr(vha,
4027 atio->u.isp24.fcp_cmnd.task_attr);
4028 data_length = be32_to_cpu(get_unaligned((uint32_t *)
4029 &atio->u.isp24.fcp_cmnd.add_cdb[
4030 atio->u.isp24.fcp_cmnd.add_cdb_len]));
4032 ret = ha->tgt.tgt_ops->handle_cmd(vha, cmd, cdb, data_length,
4033 fcp_task_attr, data_dir, bidi);
4037 * Drop extra session reference from qla_tgt_handle_cmd_for_atio*(
4039 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
4040 ha->tgt.tgt_ops->put_sess(sess);
4041 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
4045 ql_dbg(ql_dbg_io, vha, 0x3060, "Terminating work cmd %p", cmd);
4047 * cmd has not sent to target yet, so pass NULL as the second
4048 * argument to qlt_send_term_exchange() and free the memory here.
4050 cmd->trc_flags |= TRC_DO_WORK_ERR;
4051 spin_lock_irqsave(qpair->qp_lock_ptr, flags);
4052 qlt_send_term_exchange(qpair, NULL, &cmd->atio, 1, 0);
4054 qlt_decr_num_pend_cmds(vha);
4055 percpu_ida_free(&sess->se_sess->sess_tag_pool, cmd->se_cmd.map_tag);
4056 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
4058 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
4059 ha->tgt.tgt_ops->put_sess(sess);
4060 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
4063 static void qlt_do_work(struct work_struct *work)
4065 struct qla_tgt_cmd *cmd = container_of(work, struct qla_tgt_cmd, work);
4066 scsi_qla_host_t *vha = cmd->vha;
4067 unsigned long flags;
4069 spin_lock_irqsave(&vha->cmd_list_lock, flags);
4070 list_del(&cmd->cmd_list);
4071 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
4076 void qlt_clr_qp_table(struct scsi_qla_host *vha)
4078 unsigned long flags;
4079 struct qla_hw_data *ha = vha->hw;
4080 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
4084 ql_log(ql_log_info, vha, 0x706c,
4085 "User update Number of Active Qpairs %d\n",
4086 ha->tgt.num_act_qpairs);
4088 spin_lock_irqsave(&ha->tgt.atio_lock, flags);
4090 btree_for_each_safe64(&tgt->lun_qpair_map, key, node)
4091 btree_remove64(&tgt->lun_qpair_map, key);
4093 ha->base_qpair->lun_cnt = 0;
4094 for (key = 0; key < ha->max_qpairs; key++)
4095 if (ha->queue_pair_map[key])
4096 ha->queue_pair_map[key]->lun_cnt = 0;
4098 spin_unlock_irqrestore(&ha->tgt.atio_lock, flags);
4101 static void qlt_assign_qpair(struct scsi_qla_host *vha,
4102 struct qla_tgt_cmd *cmd)
4104 struct qla_qpair *qpair, *qp;
4105 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
4106 struct qla_qpair_hint *h;
4108 if (vha->flags.qpairs_available) {
4109 h = btree_lookup64(&tgt->lun_qpair_map, cmd->unpacked_lun);
4111 /* spread lun to qpair ratio evently */
4113 struct scsi_qla_host *base_vha =
4114 pci_get_drvdata(vha->hw->pdev);
4116 qpair = vha->hw->base_qpair;
4117 if (qpair->lun_cnt == 0) {
4119 h = qla_qpair_to_hint(tgt, qpair);
4121 rc = btree_insert64(&tgt->lun_qpair_map,
4122 cmd->unpacked_lun, h, GFP_ATOMIC);
4125 ql_log(ql_log_info, vha, 0xd037,
4126 "Unable to insert lun %llx into lun_qpair_map\n",
4131 lcnt = qpair->lun_cnt;
4135 list_for_each_entry(qp, &base_vha->qp_list,
4137 if (qp->lun_cnt == 0) {
4139 h = qla_qpair_to_hint(tgt, qp);
4141 rc = btree_insert64(&tgt->lun_qpair_map,
4142 cmd->unpacked_lun, h, GFP_ATOMIC);
4145 ql_log(ql_log_info, vha, 0xd038,
4146 "Unable to insert lun %llx into lun_qpair_map\n",
4152 if (qp->lun_cnt < lcnt) {
4161 h = qla_qpair_to_hint(tgt, qpair);
4163 rc = btree_insert64(&tgt->lun_qpair_map,
4164 cmd->unpacked_lun, h, GFP_ATOMIC);
4167 ql_log(ql_log_info, vha, 0xd039,
4168 "Unable to insert lun %llx into lun_qpair_map\n",
4173 h = &tgt->qphints[0];
4176 cmd->qpair = h->qpair;
4177 cmd->se_cmd.cpuid = h->cpuid;
4180 static struct qla_tgt_cmd *qlt_get_tag(scsi_qla_host_t *vha,
4181 struct fc_port *sess,
4182 struct atio_from_isp *atio)
4184 struct se_session *se_sess = sess->se_sess;
4185 struct qla_tgt_cmd *cmd;
4188 tag = percpu_ida_alloc(&se_sess->sess_tag_pool, TASK_RUNNING);
4192 cmd = &((struct qla_tgt_cmd *)se_sess->sess_cmd_map)[tag];
4193 memset(cmd, 0, sizeof(struct qla_tgt_cmd));
4194 cmd->cmd_type = TYPE_TGT_CMD;
4195 memcpy(&cmd->atio, atio, sizeof(*atio));
4196 cmd->state = QLA_TGT_STATE_NEW;
4197 cmd->tgt = vha->vha_tgt.qla_tgt;
4198 qlt_incr_num_pend_cmds(vha);
4200 cmd->se_cmd.map_tag = tag;
4202 cmd->loop_id = sess->loop_id;
4203 cmd->conf_compl_supported = sess->conf_compl_supported;
4206 cmd->jiffies_at_alloc = get_jiffies_64();
4208 cmd->unpacked_lun = scsilun_to_int(
4209 (struct scsi_lun *)&atio->u.isp24.fcp_cmnd.lun);
4210 qlt_assign_qpair(vha, cmd);
4211 cmd->reset_count = vha->hw->base_qpair->chip_reset;
4212 cmd->vp_idx = vha->vp_idx;
4217 static void qlt_create_sess_from_atio(struct work_struct *work)
4219 struct qla_tgt_sess_op *op = container_of(work,
4220 struct qla_tgt_sess_op, work);
4221 scsi_qla_host_t *vha = op->vha;
4222 struct qla_hw_data *ha = vha->hw;
4223 struct fc_port *sess;
4224 struct qla_tgt_cmd *cmd;
4225 unsigned long flags;
4226 uint8_t *s_id = op->atio.u.isp24.fcp_hdr.s_id;
4228 spin_lock_irqsave(&vha->cmd_list_lock, flags);
4229 list_del(&op->cmd_list);
4230 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
4233 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf083,
4234 "sess_op with tag %u is aborted\n",
4235 op->atio.u.isp24.exchange_addr);
4239 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf022,
4240 "qla_target(%d): Unable to find wwn login"
4241 " (s_id %x:%x:%x), trying to create it manually\n",
4242 vha->vp_idx, s_id[0], s_id[1], s_id[2]);
4244 if (op->atio.u.raw.entry_count > 1) {
4245 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf023,
4246 "Dropping multy entry atio %p\n", &op->atio);
4250 sess = qlt_make_local_sess(vha, s_id);
4251 /* sess has an extra creation ref. */
4256 * Now obtain a pre-allocated session tag using the original op->atio
4257 * packet header, and dispatch into __qlt_do_work() using the existing
4260 cmd = qlt_get_tag(vha, sess, &op->atio);
4262 struct qla_qpair *qpair = ha->base_qpair;
4264 spin_lock_irqsave(qpair->qp_lock_ptr, flags);
4265 qlt_send_busy(qpair, &op->atio, SAM_STAT_BUSY);
4266 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
4268 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
4269 ha->tgt.tgt_ops->put_sess(sess);
4270 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
4276 * __qlt_do_work() will call qlt_put_sess() to release
4277 * the extra reference taken above by qlt_make_local_sess()
4283 qlt_send_term_exchange(vha->hw->base_qpair, NULL, &op->atio, 0, 0);
4287 /* ha->hardware_lock supposed to be held on entry */
4288 static int qlt_handle_cmd_for_atio(struct scsi_qla_host *vha,
4289 struct atio_from_isp *atio)
4291 struct qla_hw_data *ha = vha->hw;
4292 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
4293 struct fc_port *sess;
4294 struct qla_tgt_cmd *cmd;
4295 unsigned long flags;
4297 if (unlikely(tgt->tgt_stop)) {
4298 ql_dbg(ql_dbg_io, vha, 0x3061,
4299 "New command while device %p is shutting down\n", tgt);
4303 sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha, atio->u.isp24.fcp_hdr.s_id);
4304 if (unlikely(!sess)) {
4305 struct qla_tgt_sess_op *op = kzalloc(sizeof(struct qla_tgt_sess_op),
4310 memcpy(&op->atio, atio, sizeof(*atio));
4313 spin_lock_irqsave(&vha->cmd_list_lock, flags);
4314 list_add_tail(&op->cmd_list, &vha->qla_sess_op_cmd_list);
4315 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
4317 INIT_WORK(&op->work, qlt_create_sess_from_atio);
4318 queue_work(qla_tgt_wq, &op->work);
4322 /* Another WWN used to have our s_id. Our PLOGI scheduled its
4323 * session deletion, but it's still in sess_del_work wq */
4324 if (sess->deleted) {
4325 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf002,
4326 "New command while old session %p is being deleted\n",
4332 * Do kref_get() before returning + dropping qla_hw_data->hardware_lock.
4334 if (!kref_get_unless_zero(&sess->sess_kref)) {
4335 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf004,
4336 "%s: kref_get fail, %8phC oxid %x \n",
4337 __func__, sess->port_name,
4338 be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id));
4342 cmd = qlt_get_tag(vha, sess, atio);
4344 ql_dbg(ql_dbg_io, vha, 0x3062,
4345 "qla_target(%d): Allocation of cmd failed\n", vha->vp_idx);
4346 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
4347 ha->tgt.tgt_ops->put_sess(sess);
4348 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
4353 cmd->trc_flags |= TRC_NEW_CMD;
4355 spin_lock_irqsave(&vha->cmd_list_lock, flags);
4356 list_add_tail(&cmd->cmd_list, &vha->qla_cmd_list);
4357 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
4359 INIT_WORK(&cmd->work, qlt_do_work);
4360 if (vha->flags.qpairs_available) {
4361 queue_work_on(cmd->se_cmd.cpuid, qla_tgt_wq, &cmd->work);
4362 } else if (ha->msix_count) {
4363 if (cmd->atio.u.isp24.fcp_cmnd.rddata)
4364 queue_work_on(smp_processor_id(), qla_tgt_wq,
4367 queue_work_on(cmd->se_cmd.cpuid, qla_tgt_wq,
4370 queue_work(qla_tgt_wq, &cmd->work);
4376 /* ha->hardware_lock supposed to be held on entry */
4377 static int qlt_issue_task_mgmt(struct fc_port *sess, u64 lun,
4378 int fn, void *iocb, int flags)
4380 struct scsi_qla_host *vha = sess->vha;
4381 struct qla_hw_data *ha = vha->hw;
4382 struct qla_tgt_mgmt_cmd *mcmd;
4383 struct atio_from_isp *a = (struct atio_from_isp *)iocb;
4386 mcmd = mempool_alloc(qla_tgt_mgmt_cmd_mempool, GFP_ATOMIC);
4388 ql_dbg(ql_dbg_tgt_tmr, vha, 0x10009,
4389 "qla_target(%d): Allocation of management "
4390 "command failed, some commands and their data could "
4391 "leak\n", vha->vp_idx);
4394 memset(mcmd, 0, sizeof(*mcmd));
4398 memcpy(&mcmd->orig_iocb.imm_ntfy, iocb,
4399 sizeof(mcmd->orig_iocb.imm_ntfy));
4401 mcmd->tmr_func = fn;
4402 mcmd->flags = flags;
4403 mcmd->reset_count = ha->base_qpair->chip_reset;
4404 mcmd->qpair = ha->base_qpair;
4408 case QLA_TGT_LUN_RESET:
4409 abort_cmds_for_lun(vha, lun, a->u.isp24.fcp_hdr.s_id);
4413 res = ha->tgt.tgt_ops->handle_tmr(mcmd, lun, mcmd->tmr_func, 0);
4415 ql_dbg(ql_dbg_tgt_tmr, vha, 0x1000b,
4416 "qla_target(%d): tgt.tgt_ops->handle_tmr() failed: %d\n",
4417 sess->vha->vp_idx, res);
4418 mempool_free(mcmd, qla_tgt_mgmt_cmd_mempool);
4425 /* ha->hardware_lock supposed to be held on entry */
4426 static int qlt_handle_task_mgmt(struct scsi_qla_host *vha, void *iocb)
4428 struct atio_from_isp *a = (struct atio_from_isp *)iocb;
4429 struct qla_hw_data *ha = vha->hw;
4430 struct qla_tgt *tgt;
4431 struct fc_port *sess;
4434 unsigned long flags;
4436 tgt = vha->vha_tgt.qla_tgt;
4438 fn = a->u.isp24.fcp_cmnd.task_mgmt_flags;
4440 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
4441 sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha,
4442 a->u.isp24.fcp_hdr.s_id);
4443 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
4446 scsilun_to_int((struct scsi_lun *)&a->u.isp24.fcp_cmnd.lun);
4449 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf024,
4450 "qla_target(%d): task mgmt fn 0x%x for "
4451 "non-existant session\n", vha->vp_idx, fn);
4452 return qlt_sched_sess_work(tgt, QLA_TGT_SESS_WORK_TM, iocb,
4453 sizeof(struct atio_from_isp));
4459 return qlt_issue_task_mgmt(sess, unpacked_lun, fn, iocb, 0);
4462 /* ha->hardware_lock supposed to be held on entry */
4463 static int __qlt_abort_task(struct scsi_qla_host *vha,
4464 struct imm_ntfy_from_isp *iocb, struct fc_port *sess)
4466 struct atio_from_isp *a = (struct atio_from_isp *)iocb;
4467 struct qla_hw_data *ha = vha->hw;
4468 struct qla_tgt_mgmt_cmd *mcmd;
4472 mcmd = mempool_alloc(qla_tgt_mgmt_cmd_mempool, GFP_ATOMIC);
4474 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05f,
4475 "qla_target(%d): %s: Allocation of ABORT cmd failed\n",
4476 vha->vp_idx, __func__);
4479 memset(mcmd, 0, sizeof(*mcmd));
4482 memcpy(&mcmd->orig_iocb.imm_ntfy, iocb,
4483 sizeof(mcmd->orig_iocb.imm_ntfy));
4486 scsilun_to_int((struct scsi_lun *)&a->u.isp24.fcp_cmnd.lun);
4487 mcmd->reset_count = ha->base_qpair->chip_reset;
4488 mcmd->tmr_func = QLA_TGT_2G_ABORT_TASK;
4489 mcmd->qpair = ha->base_qpair;
4491 rc = ha->tgt.tgt_ops->handle_tmr(mcmd, unpacked_lun, mcmd->tmr_func,
4492 le16_to_cpu(iocb->u.isp2x.seq_id));
4494 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf060,
4495 "qla_target(%d): tgt_ops->handle_tmr() failed: %d\n",
4497 mempool_free(mcmd, qla_tgt_mgmt_cmd_mempool);
4504 /* ha->hardware_lock supposed to be held on entry */
4505 static int qlt_abort_task(struct scsi_qla_host *vha,
4506 struct imm_ntfy_from_isp *iocb)
4508 struct qla_hw_data *ha = vha->hw;
4509 struct fc_port *sess;
4511 unsigned long flags;
4513 loop_id = GET_TARGET_ID(ha, (struct atio_from_isp *)iocb);
4515 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
4516 sess = ha->tgt.tgt_ops->find_sess_by_loop_id(vha, loop_id);
4517 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
4520 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf025,
4521 "qla_target(%d): task abort for unexisting "
4522 "session\n", vha->vp_idx);
4523 return qlt_sched_sess_work(vha->vha_tgt.qla_tgt,
4524 QLA_TGT_SESS_WORK_ABORT, iocb, sizeof(*iocb));
4527 return __qlt_abort_task(vha, iocb, sess);
4530 void qlt_logo_completion_handler(fc_port_t *fcport, int rc)
4532 if (rc != MBS_COMMAND_COMPLETE) {
4533 ql_dbg(ql_dbg_tgt_mgt, fcport->vha, 0xf093,
4534 "%s: se_sess %p / sess %p from"
4535 " port %8phC loop_id %#04x s_id %02x:%02x:%02x"
4536 " LOGO failed: %#x\n",
4540 fcport->port_name, fcport->loop_id,
4541 fcport->d_id.b.domain, fcport->d_id.b.area,
4542 fcport->d_id.b.al_pa, rc);
4545 fcport->logout_completed = 1;
4549 * ha->hardware_lock supposed to be held on entry (to protect tgt->sess_list)
4551 * Schedules sessions with matching port_id/loop_id but different wwn for
4552 * deletion. Returns existing session with matching wwn if present.
4556 qlt_find_sess_invalidate_other(scsi_qla_host_t *vha, uint64_t wwn,
4557 port_id_t port_id, uint16_t loop_id, struct fc_port **conflict_sess)
4559 struct fc_port *sess = NULL, *other_sess;
4562 *conflict_sess = NULL;
4564 list_for_each_entry(other_sess, &vha->vp_fcports, list) {
4566 other_wwn = wwn_to_u64(other_sess->port_name);
4568 if (wwn == other_wwn) {
4574 /* find other sess with nport_id collision */
4575 if (port_id.b24 == other_sess->d_id.b24) {
4576 if (loop_id != other_sess->loop_id) {
4577 ql_dbg(ql_dbg_tgt_tmr, vha, 0x1000c,
4578 "Invalidating sess %p loop_id %d wwn %llx.\n",
4579 other_sess, other_sess->loop_id, other_wwn);
4582 * logout_on_delete is set by default, but another
4583 * session that has the same s_id/loop_id combo
4584 * might have cleared it when requested this session
4585 * deletion, so don't touch it
4587 qlt_schedule_sess_for_deletion(other_sess, true);
4590 * Another wwn used to have our s_id/loop_id
4591 * kill the session, but don't free the loop_id
4593 ql_dbg(ql_dbg_tgt_tmr, vha, 0xf01b,
4594 "Invalidating sess %p loop_id %d wwn %llx.\n",
4595 other_sess, other_sess->loop_id, other_wwn);
4597 other_sess->keep_nport_handle = 1;
4598 if (other_sess->disc_state != DSC_DELETED)
4599 *conflict_sess = other_sess;
4600 qlt_schedule_sess_for_deletion(other_sess,
4606 /* find other sess with nport handle collision */
4607 if ((loop_id == other_sess->loop_id) &&
4608 (loop_id != FC_NO_LOOP_ID)) {
4609 ql_dbg(ql_dbg_tgt_tmr, vha, 0x1000d,
4610 "Invalidating sess %p loop_id %d wwn %llx.\n",
4611 other_sess, other_sess->loop_id, other_wwn);
4613 /* Same loop_id but different s_id
4614 * Ok to kill and logout */
4615 qlt_schedule_sess_for_deletion(other_sess, true);
4622 /* Abort any commands for this s_id waiting on qla_tgt_wq workqueue */
4623 static int abort_cmds_for_s_id(struct scsi_qla_host *vha, port_id_t *s_id)
4625 struct qla_tgt_sess_op *op;
4626 struct qla_tgt_cmd *cmd;
4629 unsigned long flags;
4631 key = (((u32)s_id->b.domain << 16) |
4632 ((u32)s_id->b.area << 8) |
4633 ((u32)s_id->b.al_pa));
4635 spin_lock_irqsave(&vha->cmd_list_lock, flags);
4636 list_for_each_entry(op, &vha->qla_sess_op_cmd_list, cmd_list) {
4637 uint32_t op_key = sid_to_key(op->atio.u.isp24.fcp_hdr.s_id);
4639 if (op_key == key) {
4645 list_for_each_entry(op, &vha->unknown_atio_list, cmd_list) {
4646 uint32_t op_key = sid_to_key(op->atio.u.isp24.fcp_hdr.s_id);
4647 if (op_key == key) {
4653 list_for_each_entry(cmd, &vha->qla_cmd_list, cmd_list) {
4654 uint32_t cmd_key = sid_to_key(cmd->atio.u.isp24.fcp_hdr.s_id);
4655 if (cmd_key == key) {
4660 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
4666 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
4668 static int qlt_24xx_handle_els(struct scsi_qla_host *vha,
4669 struct imm_ntfy_from_isp *iocb)
4671 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
4672 struct qla_hw_data *ha = vha->hw;
4673 struct fc_port *sess = NULL, *conflict_sess = NULL;
4679 struct qlt_plogi_ack_t *pla;
4680 unsigned long flags;
4682 wwn = wwn_to_u64(iocb->u.isp24.port_name);
4684 port_id.b.domain = iocb->u.isp24.port_id[2];
4685 port_id.b.area = iocb->u.isp24.port_id[1];
4686 port_id.b.al_pa = iocb->u.isp24.port_id[0];
4687 port_id.b.rsvd_1 = 0;
4689 loop_id = le16_to_cpu(iocb->u.isp24.nport_handle);
4691 ql_dbg(ql_dbg_disc, vha, 0xf026,
4692 "qla_target(%d): Port ID: %02x:%02x:%02x ELS opcode: 0x%02x lid %d %8phC\n",
4693 vha->vp_idx, iocb->u.isp24.port_id[2],
4694 iocb->u.isp24.port_id[1], iocb->u.isp24.port_id[0],
4695 iocb->u.isp24.status_subcode, loop_id,
4696 iocb->u.isp24.port_name);
4698 /* res = 1 means ack at the end of thread
4699 * res = 0 means ack async/later.
4701 switch (iocb->u.isp24.status_subcode) {
4704 /* Mark all stale commands in qla_tgt_wq for deletion */
4705 abort_cmds_for_s_id(vha, &port_id);
4708 spin_lock_irqsave(&tgt->ha->tgt.sess_lock, flags);
4709 sess = qlt_find_sess_invalidate_other(vha, wwn,
4710 port_id, loop_id, &conflict_sess);
4711 spin_unlock_irqrestore(&tgt->ha->tgt.sess_lock, flags);
4714 if (IS_SW_RESV_ADDR(port_id)) {
4719 pla = qlt_plogi_ack_find_add(vha, &port_id, iocb);
4721 qlt_send_term_imm_notif(vha, iocb, 1);
4727 if (conflict_sess) {
4728 conflict_sess->login_gen++;
4729 qlt_plogi_ack_link(vha, pla, conflict_sess,
4730 QLT_PLOGI_LINK_CONFLICT);
4735 qla24xx_post_newsess_work(vha, &port_id,
4736 iocb->u.isp24.port_name, pla);
4741 qlt_plogi_ack_link(vha, pla, sess, QLT_PLOGI_LINK_SAME_WWN);
4742 sess->fw_login_state = DSC_LS_PLOGI_PEND;
4743 sess->d_id = port_id;
4746 switch (sess->disc_state) {
4748 qlt_plogi_ack_unref(vha, pla);
4753 * Under normal circumstances we want to release nport handle
4754 * during LOGO process to avoid nport handle leaks inside FW.
4755 * The exception is when LOGO is done while another PLOGI with
4756 * the same nport handle is waiting as might be the case here.
4757 * Note: there is always a possibily of a race where session
4758 * deletion has already started for other reasons (e.g. ACL
4759 * removal) and now PLOGI arrives:
4760 * 1. if PLOGI arrived in FW after nport handle has been freed,
4761 * FW must have assigned this PLOGI a new/same handle and we
4762 * can proceed ACK'ing it as usual when session deletion
4764 * 2. if PLOGI arrived in FW before LOGO with LCF_FREE_NPORT
4765 * bit reached it, the handle has now been released. We'll
4766 * get an error when we ACK this PLOGI. Nothing will be sent
4767 * back to initiator. Initiator should eventually retry
4768 * PLOGI and situation will correct itself.
4770 sess->keep_nport_handle = ((sess->loop_id == loop_id) &&
4771 (sess->d_id.b24 == port_id.b24));
4773 ql_dbg(ql_dbg_disc, vha, 0x20f9,
4774 "%s %d %8phC post del sess\n",
4775 __func__, __LINE__, sess->port_name);
4778 qlt_schedule_sess_for_deletion_lock(sess);
4785 wd3_lo = le16_to_cpu(iocb->u.isp24.u.prli.wd3_lo);
4788 spin_lock_irqsave(&tgt->ha->tgt.sess_lock, flags);
4789 sess = qlt_find_sess_invalidate_other(vha, wwn, port_id,
4790 loop_id, &conflict_sess);
4791 spin_unlock_irqrestore(&tgt->ha->tgt.sess_lock, flags);
4794 if (conflict_sess) {
4795 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf09b,
4796 "PRLI with conflicting sess %p port %8phC\n",
4797 conflict_sess, conflict_sess->port_name);
4798 qlt_send_term_imm_notif(vha, iocb, 1);
4804 if (sess->fw_login_state != DSC_LS_PLOGI_PEND &&
4805 sess->fw_login_state != DSC_LS_PLOGI_COMP) {
4807 * Impatient initiator sent PRLI before last
4808 * PLOGI could finish. Will force him to re-try,
4809 * while last one finishes.
4811 ql_log(ql_log_warn, sess->vha, 0xf095,
4812 "sess %p PRLI received, before plogi ack.\n",
4814 qlt_send_term_imm_notif(vha, iocb, 1);
4820 * This shouldn't happen under normal circumstances,
4821 * since we have deleted the old session during PLOGI
4823 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf096,
4824 "PRLI (loop_id %#04x) for existing sess %p (loop_id %#04x)\n",
4825 sess->loop_id, sess, iocb->u.isp24.nport_handle);
4828 sess->loop_id = loop_id;
4829 sess->d_id = port_id;
4830 sess->fw_login_state = DSC_LS_PRLI_PEND;
4833 sess->conf_compl_supported = 1;
4835 if ((wd3_lo & BIT_4) == 0)
4836 sess->port_type = FCT_INITIATOR;
4838 sess->port_type = FCT_TARGET;
4840 res = 1; /* send notify ack */
4842 /* Make session global (not used in fabric mode) */
4843 if (ha->current_topology != ISP_CFG_F) {
4845 ql_dbg(ql_dbg_disc, vha, 0x20fa,
4846 "%s %d %8phC post nack\n",
4847 __func__, __LINE__, sess->port_name);
4848 qla24xx_post_nack_work(vha, sess, iocb,
4852 set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
4853 set_bit(LOCAL_LOOP_UPDATE, &vha->dpc_flags);
4854 qla2xxx_wake_dpc(vha);
4858 ql_dbg(ql_dbg_disc, vha, 0x20fb,
4859 "%s %d %8phC post nack\n",
4860 __func__, __LINE__, sess->port_name);
4861 qla24xx_post_nack_work(vha, sess, iocb,
4869 if (le16_to_cpu(iocb->u.isp24.flags) &
4870 NOTIFY24XX_FLAGS_GLOBAL_TPRLO) {
4872 qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS);
4879 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
4880 sess = qla2x00_find_fcport_by_loopid(vha, loop_id);
4881 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
4885 sess->fw_login_state = DSC_LS_LOGO_PEND;
4886 sess->logo_ack_needed = 1;
4887 memcpy(sess->iocb, iocb, IOCB_SIZE);
4890 res = qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS_SESS);
4892 ql_dbg(ql_dbg_disc, vha, 0x20fc,
4893 "%s: logo %llx res %d sess %p ",
4894 __func__, wwn, res, sess);
4897 * cmd went upper layer, look for qlt_xmit_tm_rsp()
4898 * for LOGO_ACK & sess delete
4903 /* cmd did not go to upper layer. */
4905 qlt_schedule_sess_for_deletion_lock(sess);
4908 /* else logo will be ack */
4914 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
4915 if (tgt->link_reinit_iocb_pending) {
4916 qlt_send_notify_ack(ha->base_qpair,
4917 &tgt->link_reinit_iocb, 0, 0, 0, 0, 0, 0);
4918 tgt->link_reinit_iocb_pending = 0;
4921 sess = qla2x00_find_fcport_by_wwpn(vha,
4922 iocb->u.isp24.port_name, 1);
4924 ql_dbg(ql_dbg_disc, vha, 0x20fd,
4925 "sess %p lid %d|%d DS %d LS %d\n",
4926 sess, sess->loop_id, loop_id,
4927 sess->disc_state, sess->fw_login_state);
4930 res = 1; /* send notify ack */
4934 case ELS_FLOGI: /* should never happen */
4936 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf061,
4937 "qla_target(%d): Unsupported ELS command %x "
4938 "received\n", vha->vp_idx, iocb->u.isp24.status_subcode);
4939 res = qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS_SESS);
4947 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
4949 static void qlt_handle_imm_notify(struct scsi_qla_host *vha,
4950 struct imm_ntfy_from_isp *iocb)
4952 struct qla_hw_data *ha = vha->hw;
4953 uint32_t add_flags = 0;
4954 int send_notify_ack = 1;
4957 status = le16_to_cpu(iocb->u.isp2x.status);
4959 case IMM_NTFY_LIP_RESET:
4961 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf032,
4962 "qla_target(%d): LIP reset (loop %#x), subcode %x\n",
4963 vha->vp_idx, le16_to_cpu(iocb->u.isp24.nport_handle),
4964 iocb->u.isp24.status_subcode);
4966 if (qlt_reset(vha, iocb, QLA_TGT_ABORT_ALL) == 0)
4967 send_notify_ack = 0;
4971 case IMM_NTFY_LIP_LINK_REINIT:
4973 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
4974 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf033,
4975 "qla_target(%d): LINK REINIT (loop %#x, "
4976 "subcode %x)\n", vha->vp_idx,
4977 le16_to_cpu(iocb->u.isp24.nport_handle),
4978 iocb->u.isp24.status_subcode);
4979 if (tgt->link_reinit_iocb_pending) {
4980 qlt_send_notify_ack(ha->base_qpair,
4981 &tgt->link_reinit_iocb, 0, 0, 0, 0, 0, 0);
4983 memcpy(&tgt->link_reinit_iocb, iocb, sizeof(*iocb));
4984 tgt->link_reinit_iocb_pending = 1;
4986 * QLogic requires to wait after LINK REINIT for possible
4987 * PDISC or ADISC ELS commands
4989 send_notify_ack = 0;
4993 case IMM_NTFY_PORT_LOGOUT:
4994 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf034,
4995 "qla_target(%d): Port logout (loop "
4996 "%#x, subcode %x)\n", vha->vp_idx,
4997 le16_to_cpu(iocb->u.isp24.nport_handle),
4998 iocb->u.isp24.status_subcode);
5000 if (qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS_SESS) == 0)
5001 send_notify_ack = 0;
5002 /* The sessions will be cleared in the callback, if needed */
5005 case IMM_NTFY_GLBL_TPRLO:
5006 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf035,
5007 "qla_target(%d): Global TPRLO (%x)\n", vha->vp_idx, status);
5008 if (qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS) == 0)
5009 send_notify_ack = 0;
5010 /* The sessions will be cleared in the callback, if needed */
5013 case IMM_NTFY_PORT_CONFIG:
5014 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf036,
5015 "qla_target(%d): Port config changed (%x)\n", vha->vp_idx,
5017 if (qlt_reset(vha, iocb, QLA_TGT_ABORT_ALL) == 0)
5018 send_notify_ack = 0;
5019 /* The sessions will be cleared in the callback, if needed */
5022 case IMM_NTFY_GLBL_LOGO:
5023 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06a,
5024 "qla_target(%d): Link failure detected\n",
5026 /* I_T nexus loss */
5027 if (qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS) == 0)
5028 send_notify_ack = 0;
5031 case IMM_NTFY_IOCB_OVERFLOW:
5032 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06b,
5033 "qla_target(%d): Cannot provide requested "
5034 "capability (IOCB overflowed the immediate notify "
5035 "resource count)\n", vha->vp_idx);
5038 case IMM_NTFY_ABORT_TASK:
5039 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf037,
5040 "qla_target(%d): Abort Task (S %08x I %#x -> "
5041 "L %#x)\n", vha->vp_idx,
5042 le16_to_cpu(iocb->u.isp2x.seq_id),
5043 GET_TARGET_ID(ha, (struct atio_from_isp *)iocb),
5044 le16_to_cpu(iocb->u.isp2x.lun));
5045 if (qlt_abort_task(vha, iocb) == 0)
5046 send_notify_ack = 0;
5049 case IMM_NTFY_RESOURCE:
5050 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06c,
5051 "qla_target(%d): Out of resources, host %ld\n",
5052 vha->vp_idx, vha->host_no);
5055 case IMM_NTFY_MSG_RX:
5056 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf038,
5057 "qla_target(%d): Immediate notify task %x\n",
5058 vha->vp_idx, iocb->u.isp2x.task_flags);
5059 if (qlt_handle_task_mgmt(vha, iocb) == 0)
5060 send_notify_ack = 0;
5064 if (qlt_24xx_handle_els(vha, iocb) == 0)
5065 send_notify_ack = 0;
5068 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06d,
5069 "qla_target(%d): Received unknown immediate "
5070 "notify status %x\n", vha->vp_idx, status);
5074 if (send_notify_ack)
5075 qlt_send_notify_ack(ha->base_qpair, iocb, add_flags, 0, 0, 0,
5080 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
5081 * This function sends busy to ISP 2xxx or 24xx.
5083 static int __qlt_send_busy(struct qla_qpair *qpair,
5084 struct atio_from_isp *atio, uint16_t status)
5086 struct scsi_qla_host *vha = qpair->vha;
5087 struct ctio7_to_24xx *ctio24;
5088 struct qla_hw_data *ha = vha->hw;
5090 struct fc_port *sess = NULL;
5091 unsigned long flags;
5094 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
5095 sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha,
5096 atio->u.isp24.fcp_hdr.s_id);
5097 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
5099 qlt_send_term_exchange(qpair, NULL, atio, 1, 0);
5102 /* Sending marker isn't necessary, since we called from ISR */
5104 pkt = (request_t *)__qla2x00_alloc_iocbs(qpair, NULL);
5106 ql_dbg(ql_dbg_io, vha, 0x3063,
5107 "qla_target(%d): %s failed: unable to allocate "
5108 "request packet", vha->vp_idx, __func__);
5112 qpair->tgt_counters.num_q_full_sent++;
5113 pkt->entry_count = 1;
5114 pkt->handle = QLA_TGT_SKIP_HANDLE | CTIO_COMPLETION_HANDLE_MARK;
5116 ctio24 = (struct ctio7_to_24xx *)pkt;
5117 ctio24->entry_type = CTIO_TYPE7;
5118 ctio24->nport_handle = sess->loop_id;
5119 ctio24->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
5120 ctio24->vp_index = vha->vp_idx;
5121 ctio24->initiator_id[0] = atio->u.isp24.fcp_hdr.s_id[2];
5122 ctio24->initiator_id[1] = atio->u.isp24.fcp_hdr.s_id[1];
5123 ctio24->initiator_id[2] = atio->u.isp24.fcp_hdr.s_id[0];
5124 ctio24->exchange_addr = atio->u.isp24.exchange_addr;
5125 temp = (atio->u.isp24.attr << 9) |
5126 CTIO7_FLAGS_STATUS_MODE_1 | CTIO7_FLAGS_SEND_STATUS |
5127 CTIO7_FLAGS_DONT_RET_CTIO;
5128 ctio24->u.status1.flags = cpu_to_le16(temp);
5130 * CTIO from fw w/o se_cmd doesn't provide enough info to retry it,
5131 * if the explicit conformation is used.
5133 ctio24->u.status1.ox_id = swab16(atio->u.isp24.fcp_hdr.ox_id);
5134 ctio24->u.status1.scsi_status = cpu_to_le16(status);
5135 /* Memory Barrier */
5137 if (qpair->reqq_start_iocbs)
5138 qpair->reqq_start_iocbs(qpair);
5140 qla2x00_start_iocbs(vha, qpair->req);
5145 * This routine is used to allocate a command for either a QFull condition
5146 * (ie reply SAM_STAT_BUSY) or to terminate an exchange that did not go
5150 qlt_alloc_qfull_cmd(struct scsi_qla_host *vha,
5151 struct atio_from_isp *atio, uint16_t status, int qfull)
5153 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
5154 struct qla_hw_data *ha = vha->hw;
5155 struct fc_port *sess;
5156 struct se_session *se_sess;
5157 struct qla_tgt_cmd *cmd;
5159 unsigned long flags;
5161 if (unlikely(tgt->tgt_stop)) {
5162 ql_dbg(ql_dbg_io, vha, 0x300a,
5163 "New command while device %p is shutting down\n", tgt);
5167 if ((vha->hw->tgt.num_qfull_cmds_alloc + 1) > MAX_QFULL_CMDS_ALLOC) {
5168 vha->hw->tgt.num_qfull_cmds_dropped++;
5169 if (vha->hw->tgt.num_qfull_cmds_dropped >
5170 vha->qla_stats.stat_max_qfull_cmds_dropped)
5171 vha->qla_stats.stat_max_qfull_cmds_dropped =
5172 vha->hw->tgt.num_qfull_cmds_dropped;
5174 ql_dbg(ql_dbg_io, vha, 0x3068,
5175 "qla_target(%d): %s: QFull CMD dropped[%d]\n",
5176 vha->vp_idx, __func__,
5177 vha->hw->tgt.num_qfull_cmds_dropped);
5179 qlt_chk_exch_leak_thresh_hold(vha);
5183 sess = ha->tgt.tgt_ops->find_sess_by_s_id
5184 (vha, atio->u.isp24.fcp_hdr.s_id);
5188 se_sess = sess->se_sess;
5190 tag = percpu_ida_alloc(&se_sess->sess_tag_pool, TASK_RUNNING);
5194 cmd = &((struct qla_tgt_cmd *)se_sess->sess_cmd_map)[tag];
5196 ql_dbg(ql_dbg_io, vha, 0x3009,
5197 "qla_target(%d): %s: Allocation of cmd failed\n",
5198 vha->vp_idx, __func__);
5200 vha->hw->tgt.num_qfull_cmds_dropped++;
5201 if (vha->hw->tgt.num_qfull_cmds_dropped >
5202 vha->qla_stats.stat_max_qfull_cmds_dropped)
5203 vha->qla_stats.stat_max_qfull_cmds_dropped =
5204 vha->hw->tgt.num_qfull_cmds_dropped;
5206 qlt_chk_exch_leak_thresh_hold(vha);
5210 memset(cmd, 0, sizeof(struct qla_tgt_cmd));
5212 qlt_incr_num_pend_cmds(vha);
5213 INIT_LIST_HEAD(&cmd->cmd_list);
5214 memcpy(&cmd->atio, atio, sizeof(*atio));
5216 cmd->tgt = vha->vha_tgt.qla_tgt;
5218 cmd->reset_count = ha->base_qpair->chip_reset;
5220 cmd->qpair = ha->base_qpair;
5224 /* NOTE: borrowing the state field to carry the status */
5225 cmd->state = status;
5227 cmd->term_exchg = 1;
5229 spin_lock_irqsave(&vha->hw->tgt.q_full_lock, flags);
5230 list_add_tail(&cmd->cmd_list, &vha->hw->tgt.q_full_list);
5232 vha->hw->tgt.num_qfull_cmds_alloc++;
5233 if (vha->hw->tgt.num_qfull_cmds_alloc >
5234 vha->qla_stats.stat_max_qfull_cmds_alloc)
5235 vha->qla_stats.stat_max_qfull_cmds_alloc =
5236 vha->hw->tgt.num_qfull_cmds_alloc;
5237 spin_unlock_irqrestore(&vha->hw->tgt.q_full_lock, flags);
5241 qlt_free_qfull_cmds(struct qla_qpair *qpair)
5243 struct scsi_qla_host *vha = qpair->vha;
5244 struct qla_hw_data *ha = vha->hw;
5245 unsigned long flags;
5246 struct qla_tgt_cmd *cmd, *tcmd;
5247 struct list_head free_list, q_full_list;
5250 if (list_empty(&ha->tgt.q_full_list))
5253 INIT_LIST_HEAD(&free_list);
5254 INIT_LIST_HEAD(&q_full_list);
5256 spin_lock_irqsave(&vha->hw->tgt.q_full_lock, flags);
5257 if (list_empty(&ha->tgt.q_full_list)) {
5258 spin_unlock_irqrestore(&vha->hw->tgt.q_full_lock, flags);
5262 list_splice_init(&vha->hw->tgt.q_full_list, &q_full_list);
5263 spin_unlock_irqrestore(&vha->hw->tgt.q_full_lock, flags);
5265 spin_lock_irqsave(qpair->qp_lock_ptr, flags);
5266 list_for_each_entry_safe(cmd, tcmd, &q_full_list, cmd_list) {
5268 /* cmd->state is a borrowed field to hold status */
5269 rc = __qlt_send_busy(qpair, &cmd->atio, cmd->state);
5270 else if (cmd->term_exchg)
5271 rc = __qlt_send_term_exchange(qpair, NULL, &cmd->atio);
5277 ql_dbg(ql_dbg_io, vha, 0x3006,
5278 "%s: busy sent for ox_id[%04x]\n", __func__,
5279 be16_to_cpu(cmd->atio.u.isp24.fcp_hdr.ox_id));
5280 else if (cmd->term_exchg)
5281 ql_dbg(ql_dbg_io, vha, 0x3007,
5282 "%s: Term exchg sent for ox_id[%04x]\n", __func__,
5283 be16_to_cpu(cmd->atio.u.isp24.fcp_hdr.ox_id));
5285 ql_dbg(ql_dbg_io, vha, 0x3008,
5286 "%s: Unexpected cmd in QFull list %p\n", __func__,
5289 list_del(&cmd->cmd_list);
5290 list_add_tail(&cmd->cmd_list, &free_list);
5292 /* piggy back on hardware_lock for protection */
5293 vha->hw->tgt.num_qfull_cmds_alloc--;
5295 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
5299 list_for_each_entry_safe(cmd, tcmd, &free_list, cmd_list) {
5300 list_del(&cmd->cmd_list);
5301 /* This cmd was never sent to TCM. There is no need
5302 * to schedule free or call free_cmd
5307 if (!list_empty(&q_full_list)) {
5308 spin_lock_irqsave(&vha->hw->tgt.q_full_lock, flags);
5309 list_splice(&q_full_list, &vha->hw->tgt.q_full_list);
5310 spin_unlock_irqrestore(&vha->hw->tgt.q_full_lock, flags);
5317 qlt_send_busy(struct qla_qpair *qpair, struct atio_from_isp *atio,
5321 struct scsi_qla_host *vha = qpair->vha;
5323 rc = __qlt_send_busy(qpair, atio, status);
5325 qlt_alloc_qfull_cmd(vha, atio, status, 1);
5329 qlt_chk_qfull_thresh_hold(struct scsi_qla_host *vha, struct qla_qpair *qpair,
5330 struct atio_from_isp *atio, uint8_t ha_locked)
5332 struct qla_hw_data *ha = vha->hw;
5334 unsigned long flags;
5336 if (ha->tgt.num_pend_cmds < Q_FULL_THRESH_HOLD(ha))
5340 spin_lock_irqsave(&ha->hardware_lock, flags);
5341 status = temp_sam_status;
5342 qlt_send_busy(qpair, atio, status);
5344 spin_unlock_irqrestore(&ha->hardware_lock, flags);
5349 /* ha->hardware_lock supposed to be held on entry */
5350 /* called via callback from qla2xxx */
5351 static void qlt_24xx_atio_pkt(struct scsi_qla_host *vha,
5352 struct atio_from_isp *atio, uint8_t ha_locked)
5354 struct qla_hw_data *ha = vha->hw;
5355 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
5357 unsigned long flags;
5359 if (unlikely(tgt == NULL)) {
5360 ql_dbg(ql_dbg_tgt, vha, 0x3064,
5361 "ATIO pkt, but no tgt (ha %p)", ha);
5365 * In tgt_stop mode we also should allow all requests to pass.
5366 * Otherwise, some commands can stuck.
5369 tgt->atio_irq_cmd_count++;
5371 switch (atio->u.raw.entry_type) {
5373 if (unlikely(atio->u.isp24.exchange_addr ==
5374 ATIO_EXCHANGE_ADDRESS_UNKNOWN)) {
5375 ql_dbg(ql_dbg_io, vha, 0x3065,
5376 "qla_target(%d): ATIO_TYPE7 "
5377 "received with UNKNOWN exchange address, "
5378 "sending QUEUE_FULL\n", vha->vp_idx);
5380 spin_lock_irqsave(&ha->hardware_lock, flags);
5381 qlt_send_busy(ha->base_qpair, atio,
5382 SAM_STAT_TASK_SET_FULL);
5384 spin_unlock_irqrestore(&ha->hardware_lock,
5389 if (likely(atio->u.isp24.fcp_cmnd.task_mgmt_flags == 0)) {
5390 rc = qlt_chk_qfull_thresh_hold(vha, ha->base_qpair,
5393 tgt->atio_irq_cmd_count--;
5396 rc = qlt_handle_cmd_for_atio(vha, atio);
5398 rc = qlt_handle_task_mgmt(vha, atio);
5400 if (unlikely(rc != 0)) {
5403 spin_lock_irqsave(&ha->hardware_lock,
5406 #if 1 /* With TERM EXCHANGE some FC cards refuse to boot */
5407 qlt_send_busy(ha->base_qpair, atio,
5410 qlt_send_term_exchange(ha->base_qpair, NULL,
5414 spin_unlock_irqrestore(
5415 &ha->hardware_lock, flags);
5417 if (tgt->tgt_stop) {
5418 ql_dbg(ql_dbg_tgt, vha, 0xe059,
5419 "qla_target: Unable to send "
5420 "command to target for req, "
5423 ql_dbg(ql_dbg_tgt, vha, 0xe05a,
5424 "qla_target(%d): Unable to send "
5425 "command to target, sending BUSY "
5426 "status.\n", vha->vp_idx);
5429 &ha->hardware_lock, flags);
5430 qlt_send_busy(ha->base_qpair,
5431 atio, SAM_STAT_BUSY);
5433 spin_unlock_irqrestore(
5434 &ha->hardware_lock, flags);
5440 case IMMED_NOTIFY_TYPE:
5442 if (unlikely(atio->u.isp2x.entry_status != 0)) {
5443 ql_dbg(ql_dbg_tgt, vha, 0xe05b,
5444 "qla_target(%d): Received ATIO packet %x "
5445 "with error status %x\n", vha->vp_idx,
5446 atio->u.raw.entry_type,
5447 atio->u.isp2x.entry_status);
5450 ql_dbg(ql_dbg_tgt, vha, 0xe02e, "%s", "IMMED_NOTIFY ATIO");
5453 spin_lock_irqsave(&ha->hardware_lock, flags);
5454 qlt_handle_imm_notify(vha, (struct imm_ntfy_from_isp *)atio);
5456 spin_unlock_irqrestore(&ha->hardware_lock, flags);
5461 ql_dbg(ql_dbg_tgt, vha, 0xe05c,
5462 "qla_target(%d): Received unknown ATIO atio "
5463 "type %x\n", vha->vp_idx, atio->u.raw.entry_type);
5467 tgt->atio_irq_cmd_count--;
5470 /* ha->hardware_lock supposed to be held on entry */
5471 /* called via callback from qla2xxx */
5472 static void qlt_response_pkt(struct scsi_qla_host *vha,
5473 struct rsp_que *rsp, response_t *pkt)
5475 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
5477 if (unlikely(tgt == NULL)) {
5478 ql_dbg(ql_dbg_tgt, vha, 0xe05d,
5479 "qla_target(%d): Response pkt %x received, but no tgt (ha %p)\n",
5480 vha->vp_idx, pkt->entry_type, vha->hw);
5485 * In tgt_stop mode we also should allow all requests to pass.
5486 * Otherwise, some commands can stuck.
5489 switch (pkt->entry_type) {
5493 struct ctio7_from_24xx *entry = (struct ctio7_from_24xx *)pkt;
5494 qlt_do_ctio_completion(vha, rsp, entry->handle,
5495 le16_to_cpu(entry->status)|(pkt->entry_status << 16),
5500 case ACCEPT_TGT_IO_TYPE:
5502 struct atio_from_isp *atio = (struct atio_from_isp *)pkt;
5504 if (atio->u.isp2x.status !=
5505 cpu_to_le16(ATIO_CDB_VALID)) {
5506 ql_dbg(ql_dbg_tgt, vha, 0xe05e,
5507 "qla_target(%d): ATIO with error "
5508 "status %x received\n", vha->vp_idx,
5509 le16_to_cpu(atio->u.isp2x.status));
5513 rc = qlt_chk_qfull_thresh_hold(vha, rsp->qpair, atio, 1);
5517 rc = qlt_handle_cmd_for_atio(vha, atio);
5518 if (unlikely(rc != 0)) {
5520 #if 1 /* With TERM EXCHANGE some FC cards refuse to boot */
5521 qlt_send_busy(rsp->qpair, atio, 0);
5523 qlt_send_term_exchange(rsp->qpair, NULL, atio, 1, 0);
5526 if (tgt->tgt_stop) {
5527 ql_dbg(ql_dbg_tgt, vha, 0xe05f,
5528 "qla_target: Unable to send "
5529 "command to target, sending TERM "
5530 "EXCHANGE for rsp\n");
5531 qlt_send_term_exchange(rsp->qpair, NULL,
5534 ql_dbg(ql_dbg_tgt, vha, 0xe060,
5535 "qla_target(%d): Unable to send "
5536 "command to target, sending BUSY "
5537 "status\n", vha->vp_idx);
5538 qlt_send_busy(rsp->qpair, atio, 0);
5545 case CONTINUE_TGT_IO_TYPE:
5547 struct ctio_to_2xxx *entry = (struct ctio_to_2xxx *)pkt;
5548 qlt_do_ctio_completion(vha, rsp, entry->handle,
5549 le16_to_cpu(entry->status)|(pkt->entry_status << 16),
5556 struct ctio_to_2xxx *entry = (struct ctio_to_2xxx *)pkt;
5557 qlt_do_ctio_completion(vha, rsp, entry->handle,
5558 le16_to_cpu(entry->status)|(pkt->entry_status << 16),
5563 case IMMED_NOTIFY_TYPE:
5564 ql_dbg(ql_dbg_tgt, vha, 0xe035, "%s", "IMMED_NOTIFY\n");
5565 qlt_handle_imm_notify(vha, (struct imm_ntfy_from_isp *)pkt);
5568 case NOTIFY_ACK_TYPE:
5569 if (tgt->notify_ack_expected > 0) {
5570 struct nack_to_isp *entry = (struct nack_to_isp *)pkt;
5571 ql_dbg(ql_dbg_tgt, vha, 0xe036,
5572 "NOTIFY_ACK seq %08x status %x\n",
5573 le16_to_cpu(entry->u.isp2x.seq_id),
5574 le16_to_cpu(entry->u.isp2x.status));
5575 tgt->notify_ack_expected--;
5576 if (entry->u.isp2x.status !=
5577 cpu_to_le16(NOTIFY_ACK_SUCCESS)) {
5578 ql_dbg(ql_dbg_tgt, vha, 0xe061,
5579 "qla_target(%d): NOTIFY_ACK "
5580 "failed %x\n", vha->vp_idx,
5581 le16_to_cpu(entry->u.isp2x.status));
5584 ql_dbg(ql_dbg_tgt, vha, 0xe062,
5585 "qla_target(%d): Unexpected NOTIFY_ACK received\n",
5590 case ABTS_RECV_24XX:
5591 ql_dbg(ql_dbg_tgt, vha, 0xe037,
5592 "ABTS_RECV_24XX: instance %d\n", vha->vp_idx);
5593 qlt_24xx_handle_abts(vha, (struct abts_recv_from_24xx *)pkt);
5596 case ABTS_RESP_24XX:
5597 if (tgt->abts_resp_expected > 0) {
5598 struct abts_resp_from_24xx_fw *entry =
5599 (struct abts_resp_from_24xx_fw *)pkt;
5600 ql_dbg(ql_dbg_tgt, vha, 0xe038,
5601 "ABTS_RESP_24XX: compl_status %x\n",
5602 entry->compl_status);
5603 tgt->abts_resp_expected--;
5604 if (le16_to_cpu(entry->compl_status) !=
5605 ABTS_RESP_COMPL_SUCCESS) {
5606 if ((entry->error_subcode1 == 0x1E) &&
5607 (entry->error_subcode2 == 0)) {
5609 * We've got a race here: aborted
5610 * exchange not terminated, i.e.
5611 * response for the aborted command was
5612 * sent between the abort request was
5613 * received and processed.
5614 * Unfortunately, the firmware has a
5615 * silly requirement that all aborted
5616 * exchanges must be explicitely
5617 * terminated, otherwise it refuses to
5618 * send responses for the abort
5619 * requests. So, we have to
5620 * (re)terminate the exchange and retry
5621 * the abort response.
5623 qlt_24xx_retry_term_exchange(vha,
5626 ql_dbg(ql_dbg_tgt, vha, 0xe063,
5627 "qla_target(%d): ABTS_RESP_24XX "
5628 "failed %x (subcode %x:%x)",
5629 vha->vp_idx, entry->compl_status,
5630 entry->error_subcode1,
5631 entry->error_subcode2);
5634 ql_dbg(ql_dbg_tgt, vha, 0xe064,
5635 "qla_target(%d): Unexpected ABTS_RESP_24XX "
5636 "received\n", vha->vp_idx);
5641 ql_dbg(ql_dbg_tgt, vha, 0xe065,
5642 "qla_target(%d): Received unknown response pkt "
5643 "type %x\n", vha->vp_idx, pkt->entry_type);
5650 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
5652 void qlt_async_event(uint16_t code, struct scsi_qla_host *vha,
5655 struct qla_hw_data *ha = vha->hw;
5656 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
5659 if (!tgt || tgt->tgt_stop || tgt->tgt_stopped)
5662 if (((code == MBA_POINT_TO_POINT) || (code == MBA_CHG_IN_CONNECTION)) &&
5666 * In tgt_stop mode we also should allow all requests to pass.
5667 * Otherwise, some commands can stuck.
5672 case MBA_RESET: /* Reset */
5673 case MBA_SYSTEM_ERR: /* System Error */
5674 case MBA_REQ_TRANSFER_ERR: /* Request Transfer Error */
5675 case MBA_RSP_TRANSFER_ERR: /* Response Transfer Error */
5676 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03a,
5677 "qla_target(%d): System error async event %#x "
5678 "occurred", vha->vp_idx, code);
5680 case MBA_WAKEUP_THRES: /* Request Queue Wake-up. */
5681 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
5686 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03b,
5687 "qla_target(%d): Async LOOP_UP occurred "
5688 "(m[0]=%x, m[1]=%x, m[2]=%x, m[3]=%x)", vha->vp_idx,
5689 le16_to_cpu(mailbox[0]), le16_to_cpu(mailbox[1]),
5690 le16_to_cpu(mailbox[2]), le16_to_cpu(mailbox[3]));
5691 if (tgt->link_reinit_iocb_pending) {
5692 qlt_send_notify_ack(ha->base_qpair,
5693 (void *)&tgt->link_reinit_iocb,
5695 tgt->link_reinit_iocb_pending = 0;
5700 case MBA_LIP_OCCURRED:
5703 case MBA_RSCN_UPDATE:
5704 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03c,
5705 "qla_target(%d): Async event %#x occurred "
5706 "(m[0]=%x, m[1]=%x, m[2]=%x, m[3]=%x)", vha->vp_idx, code,
5707 le16_to_cpu(mailbox[0]), le16_to_cpu(mailbox[1]),
5708 le16_to_cpu(mailbox[2]), le16_to_cpu(mailbox[3]));
5711 case MBA_REJECTED_FCP_CMD:
5712 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf017,
5713 "qla_target(%d): Async event LS_REJECT occurred (m[0]=%x, m[1]=%x, m[2]=%x, m[3]=%x)",
5715 le16_to_cpu(mailbox[0]), le16_to_cpu(mailbox[1]),
5716 le16_to_cpu(mailbox[2]), le16_to_cpu(mailbox[3]));
5718 if (le16_to_cpu(mailbox[3]) == 1) {
5719 /* exchange starvation. */
5720 vha->hw->exch_starvation++;
5721 if (vha->hw->exch_starvation > 5) {
5722 ql_log(ql_log_warn, vha, 0xd03a,
5723 "Exchange starvation-. Resetting RISC\n");
5725 vha->hw->exch_starvation = 0;
5726 if (IS_P3P_TYPE(vha->hw))
5727 set_bit(FCOE_CTX_RESET_NEEDED,
5730 set_bit(ISP_ABORT_NEEDED,
5732 qla2xxx_wake_dpc(vha);
5737 case MBA_PORT_UPDATE:
5738 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03d,
5739 "qla_target(%d): Port update async event %#x "
5740 "occurred: updating the ports database (m[0]=%x, m[1]=%x, "
5741 "m[2]=%x, m[3]=%x)", vha->vp_idx, code,
5742 le16_to_cpu(mailbox[0]), le16_to_cpu(mailbox[1]),
5743 le16_to_cpu(mailbox[2]), le16_to_cpu(mailbox[3]));
5745 login_code = le16_to_cpu(mailbox[2]);
5746 if (login_code == 0x4) {
5747 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03e,
5748 "Async MB 2: Got PLOGI Complete\n");
5749 vha->hw->exch_starvation = 0;
5750 } else if (login_code == 0x7)
5751 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03f,
5752 "Async MB 2: Port Logged Out\n");
5760 static fc_port_t *qlt_get_port_database(struct scsi_qla_host *vha,
5763 fc_port_t *fcport, *tfcp, *del;
5765 unsigned long flags;
5768 fcport = qla2x00_alloc_fcport(vha, GFP_KERNEL);
5770 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06f,
5771 "qla_target(%d): Allocation of tmp FC port failed",
5776 fcport->loop_id = loop_id;
5778 rc = qla24xx_gpdb_wait(vha, fcport, 0);
5779 if (rc != QLA_SUCCESS) {
5780 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf070,
5781 "qla_target(%d): Failed to retrieve fcport "
5782 "information -- get_port_database() returned %x "
5783 "(loop_id=0x%04x)", vha->vp_idx, rc, loop_id);
5789 spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
5790 tfcp = qla2x00_find_fcport_by_wwpn(vha, fcport->port_name, 1);
5793 tfcp->d_id = fcport->d_id;
5794 tfcp->port_type = fcport->port_type;
5795 tfcp->supported_classes = fcport->supported_classes;
5796 tfcp->flags |= fcport->flags;
5797 tfcp->scan_state = QLA_FCPORT_FOUND;
5802 if (vha->hw->current_topology == ISP_CFG_F)
5803 fcport->flags |= FCF_FABRIC_DEVICE;
5805 list_add_tail(&fcport->list, &vha->vp_fcports);
5806 if (!IS_SW_RESV_ADDR(fcport->d_id))
5807 vha->fcport_count++;
5808 fcport->login_gen++;
5809 fcport->disc_state = DSC_LOGIN_COMPLETE;
5810 fcport->login_succ = 1;
5814 fcport->deleted = 0;
5815 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
5817 switch (vha->host->active_mode) {
5818 case MODE_INITIATOR:
5821 if (!IS_IIDMA_CAPABLE(vha->hw) || !vha->hw->flags.gpsc_supported) {
5822 ql_dbg(ql_dbg_disc, vha, 0x20fe,
5823 "%s %d %8phC post upd_fcport fcp_cnt %d\n",
5824 __func__, __LINE__, fcport->port_name, vha->fcport_count);
5825 qla24xx_post_upd_fcport_work(vha, fcport);
5827 ql_dbg(ql_dbg_disc, vha, 0x20ff,
5828 "%s %d %8phC post gpsc fcp_cnt %d\n",
5829 __func__, __LINE__, fcport->port_name, vha->fcport_count);
5830 qla24xx_post_gpsc_work(vha, fcport);
5840 qla2x00_free_fcport(del);
5845 /* Must be called under tgt_mutex */
5846 static struct fc_port *qlt_make_local_sess(struct scsi_qla_host *vha,
5849 struct fc_port *sess = NULL;
5850 fc_port_t *fcport = NULL;
5851 int rc, global_resets;
5852 uint16_t loop_id = 0;
5854 if ((s_id[0] == 0xFF) && (s_id[1] == 0xFC)) {
5856 * This is Domain Controller, so it should be
5857 * OK to drop SCSI commands from it.
5859 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf042,
5860 "Unable to find initiator with S_ID %x:%x:%x",
5861 s_id[0], s_id[1], s_id[2]);
5865 mutex_lock(&vha->vha_tgt.tgt_mutex);
5869 atomic_read(&vha->vha_tgt.qla_tgt->tgt_global_resets_count);
5871 rc = qla24xx_get_loop_id(vha, s_id, &loop_id);
5873 mutex_unlock(&vha->vha_tgt.tgt_mutex);
5875 ql_log(ql_log_info, vha, 0xf071,
5876 "qla_target(%d): Unable to find "
5877 "initiator with S_ID %x:%x:%x",
5878 vha->vp_idx, s_id[0], s_id[1],
5881 if (rc == -ENOENT) {
5882 qlt_port_logo_t logo;
5883 sid_to_portid(s_id, &logo.id);
5885 qlt_send_first_logo(vha, &logo);
5891 fcport = qlt_get_port_database(vha, loop_id);
5893 mutex_unlock(&vha->vha_tgt.tgt_mutex);
5897 if (global_resets !=
5898 atomic_read(&vha->vha_tgt.qla_tgt->tgt_global_resets_count)) {
5899 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf043,
5900 "qla_target(%d): global reset during session discovery "
5901 "(counter was %d, new %d), retrying", vha->vp_idx,
5903 atomic_read(&vha->vha_tgt.
5904 qla_tgt->tgt_global_resets_count));
5908 sess = qlt_create_sess(vha, fcport, true);
5910 mutex_unlock(&vha->vha_tgt.tgt_mutex);
5915 static void qlt_abort_work(struct qla_tgt *tgt,
5916 struct qla_tgt_sess_work_param *prm)
5918 struct scsi_qla_host *vha = tgt->vha;
5919 struct qla_hw_data *ha = vha->hw;
5920 struct fc_port *sess = NULL;
5921 unsigned long flags = 0, flags2 = 0;
5925 spin_lock_irqsave(&ha->tgt.sess_lock, flags2);
5930 s_id[0] = prm->abts.fcp_hdr_le.s_id[2];
5931 s_id[1] = prm->abts.fcp_hdr_le.s_id[1];
5932 s_id[2] = prm->abts.fcp_hdr_le.s_id[0];
5934 sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha, s_id);
5936 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags2);
5938 sess = qlt_make_local_sess(vha, s_id);
5939 /* sess has got an extra creation ref */
5941 spin_lock_irqsave(&ha->tgt.sess_lock, flags2);
5945 if (sess->deleted) {
5950 if (!kref_get_unless_zero(&sess->sess_kref)) {
5951 ql_dbg(ql_dbg_tgt_tmr, vha, 0xf01c,
5952 "%s: kref_get fail %8phC \n",
5953 __func__, sess->port_name);
5959 rc = __qlt_24xx_handle_abts(vha, &prm->abts, sess);
5960 ha->tgt.tgt_ops->put_sess(sess);
5961 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags2);
5969 ha->tgt.tgt_ops->put_sess(sess);
5970 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags2);
5973 spin_lock_irqsave(&ha->hardware_lock, flags);
5974 qlt_24xx_send_abts_resp(ha->base_qpair, &prm->abts,
5975 FCP_TMF_REJECTED, false);
5976 spin_unlock_irqrestore(&ha->hardware_lock, flags);
5979 static void qlt_tmr_work(struct qla_tgt *tgt,
5980 struct qla_tgt_sess_work_param *prm)
5982 struct atio_from_isp *a = &prm->tm_iocb2;
5983 struct scsi_qla_host *vha = tgt->vha;
5984 struct qla_hw_data *ha = vha->hw;
5985 struct fc_port *sess = NULL;
5986 unsigned long flags;
5987 uint8_t *s_id = NULL; /* to hide compiler warnings */
5993 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
5998 s_id = prm->tm_iocb2.u.isp24.fcp_hdr.s_id;
5999 sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha, s_id);
6001 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
6003 sess = qlt_make_local_sess(vha, s_id);
6004 /* sess has got an extra creation ref */
6006 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
6010 if (sess->deleted) {
6015 if (!kref_get_unless_zero(&sess->sess_kref)) {
6016 ql_dbg(ql_dbg_tgt_tmr, vha, 0xf020,
6017 "%s: kref_get fail %8phC\n",
6018 __func__, sess->port_name);
6025 fn = a->u.isp24.fcp_cmnd.task_mgmt_flags;
6027 scsilun_to_int((struct scsi_lun *)&a->u.isp24.fcp_cmnd.lun);
6029 rc = qlt_issue_task_mgmt(sess, unpacked_lun, fn, iocb, 0);
6030 ha->tgt.tgt_ops->put_sess(sess);
6031 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
6039 ha->tgt.tgt_ops->put_sess(sess);
6040 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
6042 qlt_send_term_exchange(ha->base_qpair, NULL, &prm->tm_iocb2, 1, 0);
6045 static void qlt_sess_work_fn(struct work_struct *work)
6047 struct qla_tgt *tgt = container_of(work, struct qla_tgt, sess_work);
6048 struct scsi_qla_host *vha = tgt->vha;
6049 unsigned long flags;
6051 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf000, "Sess work (tgt %p)", tgt);
6053 spin_lock_irqsave(&tgt->sess_work_lock, flags);
6054 while (!list_empty(&tgt->sess_works_list)) {
6055 struct qla_tgt_sess_work_param *prm = list_entry(
6056 tgt->sess_works_list.next, typeof(*prm),
6057 sess_works_list_entry);
6060 * This work can be scheduled on several CPUs at time, so we
6061 * must delete the entry to eliminate double processing
6063 list_del(&prm->sess_works_list_entry);
6065 spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
6067 switch (prm->type) {
6068 case QLA_TGT_SESS_WORK_ABORT:
6069 qlt_abort_work(tgt, prm);
6071 case QLA_TGT_SESS_WORK_TM:
6072 qlt_tmr_work(tgt, prm);
6079 spin_lock_irqsave(&tgt->sess_work_lock, flags);
6083 spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
6086 /* Must be called under tgt_host_action_mutex */
6087 int qlt_add_target(struct qla_hw_data *ha, struct scsi_qla_host *base_vha)
6089 struct qla_tgt *tgt;
6091 struct qla_qpair_hint *h;
6093 if (!QLA_TGT_MODE_ENABLED())
6096 if (!IS_TGT_MODE_CAPABLE(ha)) {
6097 ql_log(ql_log_warn, base_vha, 0xe070,
6098 "This adapter does not support target mode.\n");
6102 ql_dbg(ql_dbg_tgt, base_vha, 0xe03b,
6103 "Registering target for host %ld(%p).\n", base_vha->host_no, ha);
6105 BUG_ON(base_vha->vha_tgt.qla_tgt != NULL);
6107 tgt = kzalloc(sizeof(struct qla_tgt), GFP_KERNEL);
6109 ql_dbg(ql_dbg_tgt, base_vha, 0xe066,
6110 "Unable to allocate struct qla_tgt\n");
6114 tgt->qphints = kzalloc((ha->max_qpairs + 1) *
6115 sizeof(struct qla_qpair_hint), GFP_KERNEL);
6116 if (!tgt->qphints) {
6118 ql_log(ql_log_warn, base_vha, 0x0197,
6119 "Unable to allocate qpair hints.\n");
6123 if (!(base_vha->host->hostt->supported_mode & MODE_TARGET))
6124 base_vha->host->hostt->supported_mode |= MODE_TARGET;
6126 rc = btree_init64(&tgt->lun_qpair_map);
6128 kfree(tgt->qphints);
6130 ql_log(ql_log_info, base_vha, 0x0198,
6131 "Unable to initialize lun_qpair_map btree\n");
6134 h = &tgt->qphints[0];
6135 h->qpair = ha->base_qpair;
6136 INIT_LIST_HEAD(&h->hint_elem);
6137 h->cpuid = ha->base_qpair->cpuid;
6138 list_add_tail(&h->hint_elem, &ha->base_qpair->hints_list);
6140 for (i = 0; i < ha->max_qpairs; i++) {
6141 unsigned long flags;
6143 struct qla_qpair *qpair = ha->queue_pair_map[i];
6144 h = &tgt->qphints[i + 1];
6145 INIT_LIST_HEAD(&h->hint_elem);
6148 spin_lock_irqsave(qpair->qp_lock_ptr, flags);
6149 list_add_tail(&h->hint_elem, &qpair->hints_list);
6150 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
6151 h->cpuid = qpair->cpuid;
6156 tgt->vha = base_vha;
6157 init_waitqueue_head(&tgt->waitQ);
6158 INIT_LIST_HEAD(&tgt->del_sess_list);
6159 spin_lock_init(&tgt->sess_work_lock);
6160 INIT_WORK(&tgt->sess_work, qlt_sess_work_fn);
6161 INIT_LIST_HEAD(&tgt->sess_works_list);
6162 atomic_set(&tgt->tgt_global_resets_count, 0);
6164 base_vha->vha_tgt.qla_tgt = tgt;
6166 ql_dbg(ql_dbg_tgt, base_vha, 0xe067,
6167 "qla_target(%d): using 64 Bit PCI addressing",
6170 tgt->sg_tablesize = QLA_TGT_MAX_SG_24XX(base_vha->req->length - 3);
6172 mutex_lock(&qla_tgt_mutex);
6173 list_add_tail(&tgt->tgt_list_entry, &qla_tgt_glist);
6174 mutex_unlock(&qla_tgt_mutex);
6176 if (ha->tgt.tgt_ops && ha->tgt.tgt_ops->add_target)
6177 ha->tgt.tgt_ops->add_target(base_vha);
6182 /* Must be called under tgt_host_action_mutex */
6183 int qlt_remove_target(struct qla_hw_data *ha, struct scsi_qla_host *vha)
6185 if (!vha->vha_tgt.qla_tgt)
6188 if (vha->fc_vport) {
6189 qlt_release(vha->vha_tgt.qla_tgt);
6193 /* free left over qfull cmds */
6194 qlt_init_term_exchange(vha);
6196 ql_dbg(ql_dbg_tgt, vha, 0xe03c, "Unregistering target for host %ld(%p)",
6198 qlt_release(vha->vha_tgt.qla_tgt);
6203 void qlt_remove_target_resources(struct qla_hw_data *ha)
6205 struct scsi_qla_host *node;
6208 btree_for_each_safe32(&ha->tgt.host_map, key, node)
6209 btree_remove32(&ha->tgt.host_map, key);
6211 btree_destroy32(&ha->tgt.host_map);
6214 static void qlt_lport_dump(struct scsi_qla_host *vha, u64 wwpn,
6219 pr_debug("qla2xxx HW vha->node_name: ");
6220 for (i = 0; i < WWN_SIZE; i++)
6221 pr_debug("%02x ", vha->node_name[i]);
6223 pr_debug("qla2xxx HW vha->port_name: ");
6224 for (i = 0; i < WWN_SIZE; i++)
6225 pr_debug("%02x ", vha->port_name[i]);
6228 pr_debug("qla2xxx passed configfs WWPN: ");
6229 put_unaligned_be64(wwpn, b);
6230 for (i = 0; i < WWN_SIZE; i++)
6231 pr_debug("%02x ", b[i]);
6236 * qla_tgt_lport_register - register lport with external module
6238 * @qla_tgt_ops: Pointer for tcm_qla2xxx qla_tgt_ops
6239 * @wwpn: Passwd FC target WWPN
6240 * @callback: lport initialization callback for tcm_qla2xxx code
6241 * @target_lport_ptr: pointer for tcm_qla2xxx specific lport data
6243 int qlt_lport_register(void *target_lport_ptr, u64 phys_wwpn,
6244 u64 npiv_wwpn, u64 npiv_wwnn,
6245 int (*callback)(struct scsi_qla_host *, void *, u64, u64))
6247 struct qla_tgt *tgt;
6248 struct scsi_qla_host *vha;
6249 struct qla_hw_data *ha;
6250 struct Scsi_Host *host;
6251 unsigned long flags;
6255 mutex_lock(&qla_tgt_mutex);
6256 list_for_each_entry(tgt, &qla_tgt_glist, tgt_list_entry) {
6264 if (!(host->hostt->supported_mode & MODE_TARGET))
6267 spin_lock_irqsave(&ha->hardware_lock, flags);
6268 if ((!npiv_wwpn || !npiv_wwnn) && host->active_mode & MODE_TARGET) {
6269 pr_debug("MODE_TARGET already active on qla2xxx(%d)\n",
6271 spin_unlock_irqrestore(&ha->hardware_lock, flags);
6274 if (tgt->tgt_stop) {
6275 pr_debug("MODE_TARGET in shutdown on qla2xxx(%d)\n",
6277 spin_unlock_irqrestore(&ha->hardware_lock, flags);
6280 spin_unlock_irqrestore(&ha->hardware_lock, flags);
6282 if (!scsi_host_get(host)) {
6283 ql_dbg(ql_dbg_tgt, vha, 0xe068,
6284 "Unable to scsi_host_get() for"
6285 " qla2xxx scsi_host\n");
6288 qlt_lport_dump(vha, phys_wwpn, b);
6290 if (memcmp(vha->port_name, b, WWN_SIZE)) {
6291 scsi_host_put(host);
6294 rc = (*callback)(vha, target_lport_ptr, npiv_wwpn, npiv_wwnn);
6296 scsi_host_put(host);
6298 mutex_unlock(&qla_tgt_mutex);
6301 mutex_unlock(&qla_tgt_mutex);
6305 EXPORT_SYMBOL(qlt_lport_register);
6308 * qla_tgt_lport_deregister - Degister lport
6310 * @vha: Registered scsi_qla_host pointer
6312 void qlt_lport_deregister(struct scsi_qla_host *vha)
6314 struct qla_hw_data *ha = vha->hw;
6315 struct Scsi_Host *sh = vha->host;
6317 * Clear the target_lport_ptr qla_target_template pointer in qla_hw_data
6319 vha->vha_tgt.target_lport_ptr = NULL;
6320 ha->tgt.tgt_ops = NULL;
6322 * Release the Scsi_Host reference for the underlying qla2xxx host
6326 EXPORT_SYMBOL(qlt_lport_deregister);
6328 /* Must be called under HW lock */
6329 static void qlt_set_mode(struct scsi_qla_host *vha)
6331 switch (ql2x_ini_mode) {
6332 case QLA2XXX_INI_MODE_DISABLED:
6333 case QLA2XXX_INI_MODE_EXCLUSIVE:
6334 vha->host->active_mode = MODE_TARGET;
6336 case QLA2XXX_INI_MODE_ENABLED:
6337 vha->host->active_mode = MODE_UNKNOWN;
6339 case QLA2XXX_INI_MODE_DUAL:
6340 vha->host->active_mode = MODE_DUAL;
6347 /* Must be called under HW lock */
6348 static void qlt_clear_mode(struct scsi_qla_host *vha)
6350 switch (ql2x_ini_mode) {
6351 case QLA2XXX_INI_MODE_DISABLED:
6352 vha->host->active_mode = MODE_UNKNOWN;
6354 case QLA2XXX_INI_MODE_EXCLUSIVE:
6355 vha->host->active_mode = MODE_INITIATOR;
6357 case QLA2XXX_INI_MODE_ENABLED:
6358 case QLA2XXX_INI_MODE_DUAL:
6359 vha->host->active_mode = MODE_INITIATOR;
6367 * qla_tgt_enable_vha - NO LOCK HELD
6369 * host_reset, bring up w/ Target Mode Enabled
6372 qlt_enable_vha(struct scsi_qla_host *vha)
6374 struct qla_hw_data *ha = vha->hw;
6375 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
6376 unsigned long flags;
6377 scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
6380 ql_dbg(ql_dbg_tgt, vha, 0xe069,
6381 "Unable to locate qla_tgt pointer from"
6382 " struct qla_hw_data\n");
6387 spin_lock_irqsave(&ha->hardware_lock, flags);
6388 tgt->tgt_stopped = 0;
6390 spin_unlock_irqrestore(&ha->hardware_lock, flags);
6393 qla24xx_disable_vp(vha);
6394 qla24xx_enable_vp(vha);
6396 set_bit(ISP_ABORT_NEEDED, &base_vha->dpc_flags);
6397 qla2xxx_wake_dpc(base_vha);
6398 WARN_ON_ONCE(qla2x00_wait_for_hba_online(base_vha) !=
6402 EXPORT_SYMBOL(qlt_enable_vha);
6405 * qla_tgt_disable_vha - NO LOCK HELD
6407 * Disable Target Mode and reset the adapter
6409 static void qlt_disable_vha(struct scsi_qla_host *vha)
6411 struct qla_hw_data *ha = vha->hw;
6412 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
6413 unsigned long flags;
6416 ql_dbg(ql_dbg_tgt, vha, 0xe06a,
6417 "Unable to locate qla_tgt pointer from"
6418 " struct qla_hw_data\n");
6423 spin_lock_irqsave(&ha->hardware_lock, flags);
6424 qlt_clear_mode(vha);
6425 spin_unlock_irqrestore(&ha->hardware_lock, flags);
6427 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
6428 qla2xxx_wake_dpc(vha);
6429 if (qla2x00_wait_for_hba_online(vha) != QLA_SUCCESS)
6430 ql_dbg(ql_dbg_tgt, vha, 0xe081,
6431 "qla2x00_wait_for_hba_online() failed\n");
6435 * Called from qla_init.c:qla24xx_vport_create() contex to setup
6436 * the target mode specific struct scsi_qla_host and struct qla_hw_data
6440 qlt_vport_create(struct scsi_qla_host *vha, struct qla_hw_data *ha)
6442 vha->vha_tgt.qla_tgt = NULL;
6444 mutex_init(&vha->vha_tgt.tgt_mutex);
6445 mutex_init(&vha->vha_tgt.tgt_host_action_mutex);
6447 qlt_clear_mode(vha);
6450 * NOTE: Currently the value is kept the same for <24xx and
6451 * >=24xx ISPs. If it is necessary to change it,
6452 * the check should be added for specific ISPs,
6453 * assigning the value appropriately.
6455 ha->tgt.atio_q_length = ATIO_ENTRY_CNT_24XX;
6457 qlt_add_target(ha, vha);
6461 qlt_rff_id(struct scsi_qla_host *vha, struct ct_sns_req *ct_req)
6464 * FC-4 Feature bit 0 indicates target functionality to the name server.
6466 if (qla_tgt_mode_enabled(vha)) {
6467 ct_req->req.rff_id.fc4_feature = BIT_0;
6468 } else if (qla_ini_mode_enabled(vha)) {
6469 ct_req->req.rff_id.fc4_feature = BIT_1;
6470 } else if (qla_dual_mode_enabled(vha))
6471 ct_req->req.rff_id.fc4_feature = BIT_0 | BIT_1;
6475 * qlt_init_atio_q_entries() - Initializes ATIO queue entries.
6478 * Beginning of ATIO ring has initialization control block already built
6479 * by nvram config routine.
6481 * Returns 0 on success.
6484 qlt_init_atio_q_entries(struct scsi_qla_host *vha)
6486 struct qla_hw_data *ha = vha->hw;
6488 struct atio_from_isp *pkt = (struct atio_from_isp *)ha->tgt.atio_ring;
6490 if (qla_ini_mode_enabled(vha))
6493 for (cnt = 0; cnt < ha->tgt.atio_q_length; cnt++) {
6494 pkt->u.raw.signature = ATIO_PROCESSED;
6501 * qlt_24xx_process_atio_queue() - Process ATIO queue entries.
6502 * @ha: SCSI driver HA context
6505 qlt_24xx_process_atio_queue(struct scsi_qla_host *vha, uint8_t ha_locked)
6507 struct qla_hw_data *ha = vha->hw;
6508 struct atio_from_isp *pkt;
6511 if (!ha->flags.fw_started)
6514 while ((ha->tgt.atio_ring_ptr->signature != ATIO_PROCESSED) ||
6515 fcpcmd_is_corrupted(ha->tgt.atio_ring_ptr)) {
6516 pkt = (struct atio_from_isp *)ha->tgt.atio_ring_ptr;
6517 cnt = pkt->u.raw.entry_count;
6519 if (unlikely(fcpcmd_is_corrupted(ha->tgt.atio_ring_ptr))) {
6521 * This packet is corrupted. The header + payload
6522 * can not be trusted. There is no point in passing
6525 ql_log(ql_log_warn, vha, 0xd03c,
6526 "corrupted fcp frame SID[%3phN] OXID[%04x] EXCG[%x] %64phN\n",
6527 pkt->u.isp24.fcp_hdr.s_id,
6528 be16_to_cpu(pkt->u.isp24.fcp_hdr.ox_id),
6529 le32_to_cpu(pkt->u.isp24.exchange_addr), pkt);
6531 adjust_corrupted_atio(pkt);
6532 qlt_send_term_exchange(ha->base_qpair, NULL, pkt,
6535 qlt_24xx_atio_pkt_all_vps(vha,
6536 (struct atio_from_isp *)pkt, ha_locked);
6539 for (i = 0; i < cnt; i++) {
6540 ha->tgt.atio_ring_index++;
6541 if (ha->tgt.atio_ring_index == ha->tgt.atio_q_length) {
6542 ha->tgt.atio_ring_index = 0;
6543 ha->tgt.atio_ring_ptr = ha->tgt.atio_ring;
6545 ha->tgt.atio_ring_ptr++;
6547 pkt->u.raw.signature = ATIO_PROCESSED;
6548 pkt = (struct atio_from_isp *)ha->tgt.atio_ring_ptr;
6553 /* Adjust ring index */
6554 WRT_REG_DWORD(ISP_ATIO_Q_OUT(vha), ha->tgt.atio_ring_index);
6558 qlt_24xx_config_rings(struct scsi_qla_host *vha)
6560 struct qla_hw_data *ha = vha->hw;
6561 if (!QLA_TGT_MODE_ENABLED())
6564 WRT_REG_DWORD(ISP_ATIO_Q_IN(vha), 0);
6565 WRT_REG_DWORD(ISP_ATIO_Q_OUT(vha), 0);
6566 RD_REG_DWORD(ISP_ATIO_Q_OUT(vha));
6568 if (IS_ATIO_MSIX_CAPABLE(ha)) {
6569 struct qla_msix_entry *msix = &ha->msix_entries[2];
6570 struct init_cb_24xx *icb = (struct init_cb_24xx *)ha->init_cb;
6572 icb->msix_atio = cpu_to_le16(msix->entry);
6573 ql_dbg(ql_dbg_init, vha, 0xf072,
6574 "Registering ICB vector 0x%x for atio que.\n",
6580 qlt_24xx_config_nvram_stage1(struct scsi_qla_host *vha, struct nvram_24xx *nv)
6582 struct qla_hw_data *ha = vha->hw;
6584 if (!QLA_TGT_MODE_ENABLED())
6587 if (qla_tgt_mode_enabled(vha) || qla_dual_mode_enabled(vha)) {
6588 if (!ha->tgt.saved_set) {
6589 /* We save only once */
6590 ha->tgt.saved_exchange_count = nv->exchange_count;
6591 ha->tgt.saved_firmware_options_1 =
6592 nv->firmware_options_1;
6593 ha->tgt.saved_firmware_options_2 =
6594 nv->firmware_options_2;
6595 ha->tgt.saved_firmware_options_3 =
6596 nv->firmware_options_3;
6597 ha->tgt.saved_set = 1;
6600 if (qla_tgt_mode_enabled(vha))
6601 nv->exchange_count = cpu_to_le16(0xFFFF);
6603 nv->exchange_count = cpu_to_le16(ql2xexchoffld);
6605 /* Enable target mode */
6606 nv->firmware_options_1 |= cpu_to_le32(BIT_4);
6608 /* Disable ini mode, if requested */
6609 if (qla_tgt_mode_enabled(vha))
6610 nv->firmware_options_1 |= cpu_to_le32(BIT_5);
6612 /* Disable Full Login after LIP */
6613 nv->firmware_options_1 &= cpu_to_le32(~BIT_13);
6614 /* Enable initial LIP */
6615 nv->firmware_options_1 &= cpu_to_le32(~BIT_9);
6616 if (ql2xtgt_tape_enable)
6617 /* Enable FC Tape support */
6618 nv->firmware_options_2 |= cpu_to_le32(BIT_12);
6620 /* Disable FC Tape support */
6621 nv->firmware_options_2 &= cpu_to_le32(~BIT_12);
6623 /* Disable Full Login after LIP */
6624 nv->host_p &= cpu_to_le32(~BIT_10);
6627 * clear BIT 15 explicitly as we have seen at least
6628 * a couple of instances where this was set and this
6629 * was causing the firmware to not be initialized.
6631 nv->firmware_options_1 &= cpu_to_le32(~BIT_15);
6632 /* Enable target PRLI control */
6633 nv->firmware_options_2 |= cpu_to_le32(BIT_14);
6635 if (ha->tgt.saved_set) {
6636 nv->exchange_count = ha->tgt.saved_exchange_count;
6637 nv->firmware_options_1 =
6638 ha->tgt.saved_firmware_options_1;
6639 nv->firmware_options_2 =
6640 ha->tgt.saved_firmware_options_2;
6641 nv->firmware_options_3 =
6642 ha->tgt.saved_firmware_options_3;
6647 if (ha->base_qpair->enable_class_2) {
6648 if (vha->flags.init_done)
6649 fc_host_supported_classes(vha->host) =
6650 FC_COS_CLASS2 | FC_COS_CLASS3;
6652 nv->firmware_options_2 |= cpu_to_le32(BIT_8);
6654 if (vha->flags.init_done)
6655 fc_host_supported_classes(vha->host) = FC_COS_CLASS3;
6657 nv->firmware_options_2 &= ~cpu_to_le32(BIT_8);
6662 qlt_24xx_config_nvram_stage2(struct scsi_qla_host *vha,
6663 struct init_cb_24xx *icb)
6665 struct qla_hw_data *ha = vha->hw;
6667 if (!QLA_TGT_MODE_ENABLED())
6670 if (ha->tgt.node_name_set) {
6671 memcpy(icb->node_name, ha->tgt.tgt_node_name, WWN_SIZE);
6672 icb->firmware_options_1 |= cpu_to_le32(BIT_14);
6675 /* disable ZIO at start time. */
6676 if (!vha->flags.init_done) {
6678 tmp = le32_to_cpu(icb->firmware_options_2);
6679 tmp &= ~(BIT_3 | BIT_2 | BIT_1 | BIT_0);
6680 icb->firmware_options_2 = cpu_to_le32(tmp);
6685 qlt_81xx_config_nvram_stage1(struct scsi_qla_host *vha, struct nvram_81xx *nv)
6687 struct qla_hw_data *ha = vha->hw;
6689 if (!QLA_TGT_MODE_ENABLED())
6692 if (qla_tgt_mode_enabled(vha) || qla_dual_mode_enabled(vha)) {
6693 if (!ha->tgt.saved_set) {
6694 /* We save only once */
6695 ha->tgt.saved_exchange_count = nv->exchange_count;
6696 ha->tgt.saved_firmware_options_1 =
6697 nv->firmware_options_1;
6698 ha->tgt.saved_firmware_options_2 =
6699 nv->firmware_options_2;
6700 ha->tgt.saved_firmware_options_3 =
6701 nv->firmware_options_3;
6702 ha->tgt.saved_set = 1;
6705 if (qla_tgt_mode_enabled(vha))
6706 nv->exchange_count = cpu_to_le16(0xFFFF);
6708 nv->exchange_count = cpu_to_le16(ql2xexchoffld);
6710 /* Enable target mode */
6711 nv->firmware_options_1 |= cpu_to_le32(BIT_4);
6713 /* Disable ini mode, if requested */
6714 if (qla_tgt_mode_enabled(vha))
6715 nv->firmware_options_1 |= cpu_to_le32(BIT_5);
6716 /* Disable Full Login after LIP */
6717 nv->firmware_options_1 &= cpu_to_le32(~BIT_13);
6718 /* Enable initial LIP */
6719 nv->firmware_options_1 &= cpu_to_le32(~BIT_9);
6721 * clear BIT 15 explicitly as we have seen at
6722 * least a couple of instances where this was set
6723 * and this was causing the firmware to not be
6726 nv->firmware_options_1 &= cpu_to_le32(~BIT_15);
6727 if (ql2xtgt_tape_enable)
6728 /* Enable FC tape support */
6729 nv->firmware_options_2 |= cpu_to_le32(BIT_12);
6731 /* Disable FC tape support */
6732 nv->firmware_options_2 &= cpu_to_le32(~BIT_12);
6734 /* Disable Full Login after LIP */
6735 nv->host_p &= cpu_to_le32(~BIT_10);
6736 /* Enable target PRLI control */
6737 nv->firmware_options_2 |= cpu_to_le32(BIT_14);
6739 if (ha->tgt.saved_set) {
6740 nv->exchange_count = ha->tgt.saved_exchange_count;
6741 nv->firmware_options_1 =
6742 ha->tgt.saved_firmware_options_1;
6743 nv->firmware_options_2 =
6744 ha->tgt.saved_firmware_options_2;
6745 nv->firmware_options_3 =
6746 ha->tgt.saved_firmware_options_3;
6751 if (ha->base_qpair->enable_class_2) {
6752 if (vha->flags.init_done)
6753 fc_host_supported_classes(vha->host) =
6754 FC_COS_CLASS2 | FC_COS_CLASS3;
6756 nv->firmware_options_2 |= cpu_to_le32(BIT_8);
6758 if (vha->flags.init_done)
6759 fc_host_supported_classes(vha->host) = FC_COS_CLASS3;
6761 nv->firmware_options_2 &= ~cpu_to_le32(BIT_8);
6766 qlt_81xx_config_nvram_stage2(struct scsi_qla_host *vha,
6767 struct init_cb_81xx *icb)
6769 struct qla_hw_data *ha = vha->hw;
6771 if (!QLA_TGT_MODE_ENABLED())
6774 if (ha->tgt.node_name_set) {
6775 memcpy(icb->node_name, ha->tgt.tgt_node_name, WWN_SIZE);
6776 icb->firmware_options_1 |= cpu_to_le32(BIT_14);
6779 /* disable ZIO at start time. */
6780 if (!vha->flags.init_done) {
6782 tmp = le32_to_cpu(icb->firmware_options_2);
6783 tmp &= ~(BIT_3 | BIT_2 | BIT_1 | BIT_0);
6784 icb->firmware_options_2 = cpu_to_le32(tmp);
6790 qlt_83xx_iospace_config(struct qla_hw_data *ha)
6792 if (!QLA_TGT_MODE_ENABLED())
6795 ha->msix_count += 1; /* For ATIO Q */
6800 qlt_modify_vp_config(struct scsi_qla_host *vha,
6801 struct vp_config_entry_24xx *vpmod)
6803 /* enable target mode. Bit5 = 1 => disable */
6804 if (qla_tgt_mode_enabled(vha) || qla_dual_mode_enabled(vha))
6805 vpmod->options_idx1 &= ~BIT_5;
6807 /* Disable ini mode, if requested. bit4 = 1 => disable */
6808 if (qla_tgt_mode_enabled(vha))
6809 vpmod->options_idx1 &= ~BIT_4;
6813 qlt_probe_one_stage1(struct scsi_qla_host *base_vha, struct qla_hw_data *ha)
6817 if (!QLA_TGT_MODE_ENABLED())
6820 if (IS_QLA83XX(ha) || IS_QLA27XX(ha)) {
6821 ISP_ATIO_Q_IN(base_vha) = &ha->mqiobase->isp25mq.atio_q_in;
6822 ISP_ATIO_Q_OUT(base_vha) = &ha->mqiobase->isp25mq.atio_q_out;
6824 ISP_ATIO_Q_IN(base_vha) = &ha->iobase->isp24.atio_q_in;
6825 ISP_ATIO_Q_OUT(base_vha) = &ha->iobase->isp24.atio_q_out;
6828 mutex_init(&base_vha->vha_tgt.tgt_mutex);
6829 mutex_init(&base_vha->vha_tgt.tgt_host_action_mutex);
6831 INIT_LIST_HEAD(&base_vha->unknown_atio_list);
6832 INIT_DELAYED_WORK(&base_vha->unknown_atio_work,
6833 qlt_unknown_atio_work_fn);
6835 qlt_clear_mode(base_vha);
6837 rc = btree_init32(&ha->tgt.host_map);
6839 ql_log(ql_log_info, base_vha, 0xd03d,
6840 "Unable to initialize ha->host_map btree\n");
6842 qlt_update_vp_map(base_vha, SET_VP_IDX);
6846 qla83xx_msix_atio_q(int irq, void *dev_id)
6848 struct rsp_que *rsp;
6849 scsi_qla_host_t *vha;
6850 struct qla_hw_data *ha;
6851 unsigned long flags;
6853 rsp = (struct rsp_que *) dev_id;
6855 vha = pci_get_drvdata(ha->pdev);
6857 spin_lock_irqsave(&ha->tgt.atio_lock, flags);
6859 qlt_24xx_process_atio_queue(vha, 0);
6861 spin_unlock_irqrestore(&ha->tgt.atio_lock, flags);
6867 qlt_handle_abts_recv_work(struct work_struct *work)
6869 struct qla_tgt_sess_op *op = container_of(work,
6870 struct qla_tgt_sess_op, work);
6871 scsi_qla_host_t *vha = op->vha;
6872 struct qla_hw_data *ha = vha->hw;
6873 unsigned long flags;
6875 if (qla2x00_reset_active(vha) ||
6876 (op->chip_reset != ha->base_qpair->chip_reset))
6879 spin_lock_irqsave(&ha->tgt.atio_lock, flags);
6880 qlt_24xx_process_atio_queue(vha, 0);
6881 spin_unlock_irqrestore(&ha->tgt.atio_lock, flags);
6883 spin_lock_irqsave(&ha->hardware_lock, flags);
6884 qlt_response_pkt_all_vps(vha, op->rsp, (response_t *)&op->atio);
6885 spin_unlock_irqrestore(&ha->hardware_lock, flags);
6891 qlt_handle_abts_recv(struct scsi_qla_host *vha, struct rsp_que *rsp,
6894 struct qla_tgt_sess_op *op;
6896 op = kzalloc(sizeof(*op), GFP_ATOMIC);
6899 /* do not reach for ATIO queue here. This is best effort err
6900 * recovery at this point.
6902 qlt_response_pkt_all_vps(vha, rsp, pkt);
6906 memcpy(&op->atio, pkt, sizeof(*pkt));
6908 op->chip_reset = vha->hw->base_qpair->chip_reset;
6910 INIT_WORK(&op->work, qlt_handle_abts_recv_work);
6911 queue_work(qla_tgt_wq, &op->work);
6916 qlt_mem_alloc(struct qla_hw_data *ha)
6918 if (!QLA_TGT_MODE_ENABLED())
6921 ha->tgt.tgt_vp_map = kzalloc(sizeof(struct qla_tgt_vp_map) *
6922 MAX_MULTI_ID_FABRIC, GFP_KERNEL);
6923 if (!ha->tgt.tgt_vp_map)
6926 ha->tgt.atio_ring = dma_alloc_coherent(&ha->pdev->dev,
6927 (ha->tgt.atio_q_length + 1) * sizeof(struct atio_from_isp),
6928 &ha->tgt.atio_dma, GFP_KERNEL);
6929 if (!ha->tgt.atio_ring) {
6930 kfree(ha->tgt.tgt_vp_map);
6937 qlt_mem_free(struct qla_hw_data *ha)
6939 if (!QLA_TGT_MODE_ENABLED())
6942 if (ha->tgt.atio_ring) {
6943 dma_free_coherent(&ha->pdev->dev, (ha->tgt.atio_q_length + 1) *
6944 sizeof(struct atio_from_isp), ha->tgt.atio_ring,
6947 kfree(ha->tgt.tgt_vp_map);
6950 /* vport_slock to be held by the caller */
6952 qlt_update_vp_map(struct scsi_qla_host *vha, int cmd)
6958 if (!QLA_TGT_MODE_ENABLED())
6961 key = vha->d_id.b24;
6965 vha->hw->tgt.tgt_vp_map[vha->vp_idx].vha = vha;
6968 slot = btree_lookup32(&vha->hw->tgt.host_map, key);
6970 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf018,
6971 "Save vha in host_map %p %06x\n", vha, key);
6972 rc = btree_insert32(&vha->hw->tgt.host_map,
6973 key, vha, GFP_ATOMIC);
6975 ql_log(ql_log_info, vha, 0xd03e,
6976 "Unable to insert s_id into host_map: %06x\n",
6980 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf019,
6981 "replace existing vha in host_map %p %06x\n", vha, key);
6982 btree_update32(&vha->hw->tgt.host_map, key, vha);
6985 vha->hw->tgt.tgt_vp_map[vha->vp_idx].vha = NULL;
6988 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01a,
6989 "clear vha in host_map %p %06x\n", vha, key);
6990 slot = btree_lookup32(&vha->hw->tgt.host_map, key);
6992 btree_remove32(&vha->hw->tgt.host_map, key);
6998 void qlt_update_host_map(struct scsi_qla_host *vha, port_id_t id)
7000 unsigned long flags;
7001 struct qla_hw_data *ha = vha->hw;
7003 if (!vha->d_id.b24) {
7004 spin_lock_irqsave(&ha->vport_slock, flags);
7006 qlt_update_vp_map(vha, SET_AL_PA);
7007 spin_unlock_irqrestore(&ha->vport_slock, flags);
7008 } else if (vha->d_id.b24 != id.b24) {
7009 spin_lock_irqsave(&ha->vport_slock, flags);
7010 qlt_update_vp_map(vha, RESET_AL_PA);
7012 qlt_update_vp_map(vha, SET_AL_PA);
7013 spin_unlock_irqrestore(&ha->vport_slock, flags);
7017 static int __init qlt_parse_ini_mode(void)
7019 if (strcasecmp(qlini_mode, QLA2XXX_INI_MODE_STR_EXCLUSIVE) == 0)
7020 ql2x_ini_mode = QLA2XXX_INI_MODE_EXCLUSIVE;
7021 else if (strcasecmp(qlini_mode, QLA2XXX_INI_MODE_STR_DISABLED) == 0)
7022 ql2x_ini_mode = QLA2XXX_INI_MODE_DISABLED;
7023 else if (strcasecmp(qlini_mode, QLA2XXX_INI_MODE_STR_ENABLED) == 0)
7024 ql2x_ini_mode = QLA2XXX_INI_MODE_ENABLED;
7025 else if (strcasecmp(qlini_mode, QLA2XXX_INI_MODE_STR_DUAL) == 0)
7026 ql2x_ini_mode = QLA2XXX_INI_MODE_DUAL;
7033 int __init qlt_init(void)
7037 if (!qlt_parse_ini_mode()) {
7038 ql_log(ql_log_fatal, NULL, 0xe06b,
7039 "qlt_parse_ini_mode() failed\n");
7043 if (!QLA_TGT_MODE_ENABLED())
7046 qla_tgt_mgmt_cmd_cachep = kmem_cache_create("qla_tgt_mgmt_cmd_cachep",
7047 sizeof(struct qla_tgt_mgmt_cmd), __alignof__(struct
7048 qla_tgt_mgmt_cmd), 0, NULL);
7049 if (!qla_tgt_mgmt_cmd_cachep) {
7050 ql_log(ql_log_fatal, NULL, 0xd04b,
7051 "kmem_cache_create for qla_tgt_mgmt_cmd_cachep failed\n");
7055 qla_tgt_plogi_cachep = kmem_cache_create("qla_tgt_plogi_cachep",
7056 sizeof(struct qlt_plogi_ack_t), __alignof__(struct qlt_plogi_ack_t),
7059 if (!qla_tgt_plogi_cachep) {
7060 ql_log(ql_log_fatal, NULL, 0xe06d,
7061 "kmem_cache_create for qla_tgt_plogi_cachep failed\n");
7063 goto out_mgmt_cmd_cachep;
7066 qla_tgt_mgmt_cmd_mempool = mempool_create(25, mempool_alloc_slab,
7067 mempool_free_slab, qla_tgt_mgmt_cmd_cachep);
7068 if (!qla_tgt_mgmt_cmd_mempool) {
7069 ql_log(ql_log_fatal, NULL, 0xe06e,
7070 "mempool_create for qla_tgt_mgmt_cmd_mempool failed\n");
7072 goto out_plogi_cachep;
7075 qla_tgt_wq = alloc_workqueue("qla_tgt_wq", 0, 0);
7077 ql_log(ql_log_fatal, NULL, 0xe06f,
7078 "alloc_workqueue for qla_tgt_wq failed\n");
7080 goto out_cmd_mempool;
7083 * Return 1 to signal that initiator-mode is being disabled
7085 return (ql2x_ini_mode == QLA2XXX_INI_MODE_DISABLED) ? 1 : 0;
7088 mempool_destroy(qla_tgt_mgmt_cmd_mempool);
7090 kmem_cache_destroy(qla_tgt_plogi_cachep);
7091 out_mgmt_cmd_cachep:
7092 kmem_cache_destroy(qla_tgt_mgmt_cmd_cachep);
7098 if (!QLA_TGT_MODE_ENABLED())
7101 destroy_workqueue(qla_tgt_wq);
7102 mempool_destroy(qla_tgt_mgmt_cmd_mempool);
7103 kmem_cache_destroy(qla_tgt_plogi_cachep);
7104 kmem_cache_destroy(qla_tgt_mgmt_cmd_cachep);