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;
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 qlt_send_first_logo(vha, &logo);
977 if (sess->logout_on_delete) {
980 rc = qla2x00_post_async_logout_work(vha, sess, NULL);
981 if (rc != QLA_SUCCESS)
982 ql_log(ql_log_warn, vha, 0xf085,
983 "Schedule logo failed sess %p rc %d\n",
986 logout_started = true;
991 * Release the target session for FC Nexus from fabric module code.
993 if (sess->se_sess != NULL)
994 ha->tgt.tgt_ops->free_session(sess);
996 if (logout_started) {
999 while (!ACCESS_ONCE(sess->logout_completed)) {
1001 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf086,
1002 "%s: waiting for sess %p logout\n",
1009 ql_dbg(ql_dbg_disc, vha, 0xf087,
1010 "%s: sess %p logout completed\n",__func__, sess);
1013 if (sess->logo_ack_needed) {
1014 sess->logo_ack_needed = 0;
1015 qla24xx_async_notify_ack(vha, sess,
1016 (struct imm_ntfy_from_isp *)sess->iocb, SRB_NACK_LOGO);
1019 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
1020 if (sess->se_sess) {
1021 sess->se_sess = NULL;
1022 if (tgt && !IS_SW_RESV_ADDR(sess->d_id))
1026 sess->disc_state = DSC_DELETED;
1027 sess->fw_login_state = DSC_LS_PORT_UNAVAIL;
1028 sess->deleted = QLA_SESS_DELETED;
1029 sess->login_retry = vha->hw->login_retry_count;
1031 if (sess->login_succ && !IS_SW_RESV_ADDR(sess->d_id)) {
1032 vha->fcport_count--;
1033 sess->login_succ = 0;
1036 if (sess->chip_reset != ha->base_qpair->chip_reset)
1037 qla2x00_clear_loop_id(sess);
1039 if (sess->conflict) {
1040 sess->conflict->login_pause = 0;
1041 sess->conflict = NULL;
1042 if (!test_bit(UNLOADING, &vha->dpc_flags))
1043 set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
1047 struct qlt_plogi_ack_t *own =
1048 sess->plogi_link[QLT_PLOGI_LINK_SAME_WWN];
1049 struct qlt_plogi_ack_t *con =
1050 sess->plogi_link[QLT_PLOGI_LINK_CONFLICT];
1051 struct imm_ntfy_from_isp *iocb;
1055 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf099,
1056 "se_sess %p / sess %p port %8phC is gone,"
1057 " %s (ref=%d), releasing PLOGI for %8phC (ref=%d)\n",
1058 sess->se_sess, sess, sess->port_name,
1059 own ? "releasing own PLOGI" : "no own PLOGI pending",
1060 own ? own->ref_count : -1,
1061 iocb->u.isp24.port_name, con->ref_count);
1062 qlt_plogi_ack_unref(vha, con);
1063 sess->plogi_link[QLT_PLOGI_LINK_CONFLICT] = NULL;
1065 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf09a,
1066 "se_sess %p / sess %p port %8phC is gone, %s (ref=%d)\n",
1067 sess->se_sess, sess, sess->port_name,
1068 own ? "releasing own PLOGI" :
1069 "no own PLOGI pending",
1070 own ? own->ref_count : -1);
1074 sess->fw_login_state = DSC_LS_PLOGI_PEND;
1075 qlt_plogi_ack_unref(vha, own);
1076 sess->plogi_link[QLT_PLOGI_LINK_SAME_WWN] = NULL;
1079 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
1081 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf001,
1082 "Unregistration of sess %p %8phC finished fcp_cnt %d\n",
1083 sess, sess->port_name, vha->fcport_count);
1085 if (tgt && (tgt->sess_count == 0))
1086 wake_up_all(&tgt->waitQ);
1088 if (vha->fcport_count == 0)
1089 wake_up_all(&vha->fcport_waitQ);
1091 base_vha = pci_get_drvdata(ha->pdev);
1092 if (test_bit(PFLG_DRIVER_REMOVING, &base_vha->pci_flags))
1095 if (!tgt || !tgt->tgt_stop) {
1096 memset(&ea, 0, sizeof(ea));
1097 ea.event = FCME_DELETE_DONE;
1099 qla2x00_fcport_event_handler(vha, &ea);
1103 /* ha->tgt.sess_lock supposed to be held on entry */
1104 void qlt_unreg_sess(struct fc_port *sess)
1106 struct scsi_qla_host *vha = sess->vha;
1108 ql_dbg(ql_dbg_disc, sess->vha, 0x210a,
1109 "%s sess %p for deletion %8phC\n",
1110 __func__, sess, sess->port_name);
1113 vha->hw->tgt.tgt_ops->clear_nacl_from_fcport_map(sess);
1115 qla2x00_mark_device_lost(vha, sess, 1, 1);
1117 sess->deleted = QLA_SESS_DELETION_IN_PROGRESS;
1118 sess->disc_state = DSC_DELETE_PEND;
1119 sess->last_rscn_gen = sess->rscn_gen;
1120 sess->last_login_gen = sess->login_gen;
1122 if (sess->nvme_flag & NVME_FLAG_REGISTERED)
1123 schedule_work(&sess->nvme_del_work);
1125 INIT_WORK(&sess->free_work, qlt_free_session_done);
1126 schedule_work(&sess->free_work);
1128 EXPORT_SYMBOL(qlt_unreg_sess);
1130 static int qlt_reset(struct scsi_qla_host *vha, void *iocb, int mcmd)
1132 struct qla_hw_data *ha = vha->hw;
1133 struct fc_port *sess = NULL;
1136 struct imm_ntfy_from_isp *n = (struct imm_ntfy_from_isp *)iocb;
1137 unsigned long flags;
1139 loop_id = le16_to_cpu(n->u.isp24.nport_handle);
1140 if (loop_id == 0xFFFF) {
1142 atomic_inc(&vha->vha_tgt.qla_tgt->tgt_global_resets_count);
1143 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
1144 qlt_clear_tgt_db(vha->vha_tgt.qla_tgt);
1145 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
1147 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
1148 sess = ha->tgt.tgt_ops->find_sess_by_loop_id(vha, loop_id);
1149 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
1152 ql_dbg(ql_dbg_tgt, vha, 0xe000,
1153 "Using sess for qla_tgt_reset: %p\n", sess);
1159 ql_dbg(ql_dbg_tgt, vha, 0xe047,
1160 "scsi(%ld): resetting (session %p from port %8phC mcmd %x, "
1161 "loop_id %d)\n", vha->host_no, sess, sess->port_name,
1164 return qlt_issue_task_mgmt(sess, 0, mcmd, iocb, QLA24XX_MGMT_SEND_NACK);
1167 static void qla24xx_chk_fcp_state(struct fc_port *sess)
1169 if (sess->chip_reset != sess->vha->hw->base_qpair->chip_reset) {
1170 sess->logout_on_delete = 0;
1171 sess->logo_ack_needed = 0;
1172 sess->fw_login_state = DSC_LS_PORT_UNAVAIL;
1173 sess->scan_state = 0;
1177 /* ha->tgt.sess_lock supposed to be held on entry */
1178 void qlt_schedule_sess_for_deletion(struct fc_port *sess,
1181 struct qla_tgt *tgt = sess->tgt;
1183 if (sess->disc_state == DSC_DELETE_PEND)
1186 if (sess->disc_state == DSC_DELETED) {
1187 if (tgt && tgt->tgt_stop && (tgt->sess_count == 0))
1188 wake_up_all(&tgt->waitQ);
1189 if (sess->vha->fcport_count == 0)
1190 wake_up_all(&sess->vha->fcport_waitQ);
1192 if (!sess->plogi_link[QLT_PLOGI_LINK_SAME_WWN] &&
1193 !sess->plogi_link[QLT_PLOGI_LINK_CONFLICT])
1197 sess->disc_state = DSC_DELETE_PEND;
1199 if (sess->deleted == QLA_SESS_DELETED)
1200 sess->logout_on_delete = 0;
1202 sess->deleted = QLA_SESS_DELETION_IN_PROGRESS;
1203 qla24xx_chk_fcp_state(sess);
1205 ql_dbg(ql_dbg_tgt, sess->vha, 0xe001,
1206 "Scheduling sess %p for deletion\n", sess);
1208 schedule_work(&sess->del_work);
1211 void qlt_schedule_sess_for_deletion_lock(struct fc_port *sess)
1213 unsigned long flags;
1214 struct qla_hw_data *ha = sess->vha->hw;
1215 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
1216 qlt_schedule_sess_for_deletion(sess, 1);
1217 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
1220 /* ha->tgt.sess_lock supposed to be held on entry */
1221 static void qlt_clear_tgt_db(struct qla_tgt *tgt)
1223 struct fc_port *sess;
1224 scsi_qla_host_t *vha = tgt->vha;
1226 list_for_each_entry(sess, &vha->vp_fcports, list) {
1228 qlt_schedule_sess_for_deletion(sess, 1);
1231 /* At this point tgt could be already dead */
1234 static int qla24xx_get_loop_id(struct scsi_qla_host *vha, const uint8_t *s_id,
1237 struct qla_hw_data *ha = vha->hw;
1238 dma_addr_t gid_list_dma;
1239 struct gid_list_info *gid_list;
1244 gid_list = dma_alloc_coherent(&ha->pdev->dev, qla2x00_gid_list_size(ha),
1245 &gid_list_dma, GFP_KERNEL);
1247 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf044,
1248 "qla_target(%d): DMA Alloc failed of %u\n",
1249 vha->vp_idx, qla2x00_gid_list_size(ha));
1253 /* Get list of logged in devices */
1254 rc = qla24xx_gidlist_wait(vha, gid_list, gid_list_dma, &entries);
1255 if (rc != QLA_SUCCESS) {
1256 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf045,
1257 "qla_target(%d): get_id_list() failed: %x\n",
1260 goto out_free_id_list;
1263 id_iter = (char *)gid_list;
1265 for (i = 0; i < entries; i++) {
1266 struct gid_list_info *gid = (struct gid_list_info *)id_iter;
1267 if ((gid->al_pa == s_id[2]) &&
1268 (gid->area == s_id[1]) &&
1269 (gid->domain == s_id[0])) {
1270 *loop_id = le16_to_cpu(gid->loop_id);
1274 id_iter += ha->gid_list_info_size;
1278 dma_free_coherent(&ha->pdev->dev, qla2x00_gid_list_size(ha),
1279 gid_list, gid_list_dma);
1284 * Adds an extra ref to allow to drop hw lock after adding sess to the list.
1285 * Caller must put it.
1287 static struct fc_port *qlt_create_sess(
1288 struct scsi_qla_host *vha,
1292 struct qla_hw_data *ha = vha->hw;
1293 struct fc_port *sess = fcport;
1294 unsigned long flags;
1296 if (vha->vha_tgt.qla_tgt->tgt_stop)
1299 if (fcport->se_sess) {
1300 if (!kref_get_unless_zero(&sess->sess_kref)) {
1301 ql_dbg(ql_dbg_disc, vha, 0x20f6,
1302 "%s: kref_get_unless_zero failed for %8phC\n",
1303 __func__, sess->port_name);
1308 sess->tgt = vha->vha_tgt.qla_tgt;
1309 sess->local = local;
1312 * Under normal circumstances we want to logout from firmware when
1313 * session eventually ends and release corresponding nport handle.
1314 * In the exception cases (e.g. when new PLOGI is waiting) corresponding
1315 * code will adjust these flags as necessary.
1317 sess->logout_on_delete = 1;
1318 sess->keep_nport_handle = 0;
1319 sess->logout_completed = 0;
1321 if (ha->tgt.tgt_ops->check_initiator_node_acl(vha,
1322 &fcport->port_name[0], sess) < 0) {
1323 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf015,
1324 "(%d) %8phC check_initiator_node_acl failed\n",
1325 vha->vp_idx, fcport->port_name);
1328 kref_init(&fcport->sess_kref);
1330 * Take an extra reference to ->sess_kref here to handle
1331 * fc_port access across ->tgt.sess_lock reaquire.
1333 if (!kref_get_unless_zero(&sess->sess_kref)) {
1334 ql_dbg(ql_dbg_disc, vha, 0x20f7,
1335 "%s: kref_get_unless_zero failed for %8phC\n",
1336 __func__, sess->port_name);
1340 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
1341 if (!IS_SW_RESV_ADDR(sess->d_id))
1342 vha->vha_tgt.qla_tgt->sess_count++;
1344 qlt_do_generation_tick(vha, &sess->generation);
1345 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
1348 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf006,
1349 "Adding sess %p se_sess %p to tgt %p sess_count %d\n",
1350 sess, sess->se_sess, vha->vha_tgt.qla_tgt,
1351 vha->vha_tgt.qla_tgt->sess_count);
1353 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04b,
1354 "qla_target(%d): %ssession for wwn %8phC (loop_id %d, "
1355 "s_id %x:%x:%x, confirmed completion %ssupported) added\n",
1356 vha->vp_idx, local ? "local " : "", fcport->port_name,
1357 fcport->loop_id, sess->d_id.b.domain, sess->d_id.b.area,
1358 sess->d_id.b.al_pa, sess->conf_compl_supported ? "" : "not ");
1364 * max_gen - specifies maximum session generation
1365 * at which this deletion requestion is still valid
1368 qlt_fc_port_deleted(struct scsi_qla_host *vha, fc_port_t *fcport, int max_gen)
1370 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
1371 struct fc_port *sess = fcport;
1372 unsigned long flags;
1374 if (!vha->hw->tgt.tgt_ops)
1380 spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
1381 if (tgt->tgt_stop) {
1382 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
1385 if (!sess->se_sess) {
1386 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
1390 if (max_gen - sess->generation < 0) {
1391 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
1392 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf092,
1393 "Ignoring stale deletion request for se_sess %p / sess %p"
1394 " for port %8phC, req_gen %d, sess_gen %d\n",
1395 sess->se_sess, sess, sess->port_name, max_gen,
1400 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf008, "qla_tgt_fc_port_deleted %p", sess);
1403 qlt_schedule_sess_for_deletion(sess, false);
1404 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
1407 static inline int test_tgt_sess_count(struct qla_tgt *tgt)
1409 struct qla_hw_data *ha = tgt->ha;
1410 unsigned long flags;
1413 * We need to protect against race, when tgt is freed before or
1416 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
1417 ql_dbg(ql_dbg_tgt, tgt->vha, 0xe002,
1418 "tgt %p, sess_count=%d\n",
1419 tgt, tgt->sess_count);
1420 res = (tgt->sess_count == 0);
1421 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
1426 /* Called by tcm_qla2xxx configfs code */
1427 int qlt_stop_phase1(struct qla_tgt *tgt)
1429 struct scsi_qla_host *vha = tgt->vha;
1430 struct qla_hw_data *ha = tgt->ha;
1431 unsigned long flags;
1433 mutex_lock(&qla_tgt_mutex);
1434 if (!vha->fc_vport) {
1435 struct Scsi_Host *sh = vha->host;
1436 struct fc_host_attrs *fc_host = shost_to_fc_host(sh);
1439 spin_lock_irqsave(sh->host_lock, flags);
1440 npiv_vports = (fc_host->npiv_vports_inuse);
1441 spin_unlock_irqrestore(sh->host_lock, flags);
1444 mutex_unlock(&qla_tgt_mutex);
1445 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf021,
1446 "NPIV is in use. Can not stop target\n");
1450 if (tgt->tgt_stop || tgt->tgt_stopped) {
1451 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04e,
1452 "Already in tgt->tgt_stop or tgt_stopped state\n");
1453 mutex_unlock(&qla_tgt_mutex);
1457 ql_dbg(ql_dbg_tgt_mgt, vha, 0xe003, "Stopping target for host %ld(%p)\n",
1460 * Mutex needed to sync with qla_tgt_fc_port_[added,deleted].
1461 * Lock is needed, because we still can get an incoming packet.
1463 mutex_lock(&vha->vha_tgt.tgt_mutex);
1464 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
1466 qlt_clear_tgt_db(tgt);
1467 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
1468 mutex_unlock(&vha->vha_tgt.tgt_mutex);
1469 mutex_unlock(&qla_tgt_mutex);
1471 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf009,
1472 "Waiting for sess works (tgt %p)", tgt);
1473 spin_lock_irqsave(&tgt->sess_work_lock, flags);
1474 while (!list_empty(&tgt->sess_works_list)) {
1475 spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
1476 flush_scheduled_work();
1477 spin_lock_irqsave(&tgt->sess_work_lock, flags);
1479 spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
1481 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00a,
1482 "Waiting for tgt %p: sess_count=%d\n", tgt, tgt->sess_count);
1484 wait_event_timeout(tgt->waitQ, test_tgt_sess_count(tgt), 10*HZ);
1487 if (!ha->flags.host_shutting_down &&
1488 (qla_tgt_mode_enabled(vha) || qla_dual_mode_enabled(vha)))
1489 qlt_disable_vha(vha);
1491 /* Wait for sessions to clear out (just in case) */
1492 wait_event_timeout(tgt->waitQ, test_tgt_sess_count(tgt), 10*HZ);
1495 EXPORT_SYMBOL(qlt_stop_phase1);
1497 /* Called by tcm_qla2xxx configfs code */
1498 void qlt_stop_phase2(struct qla_tgt *tgt)
1500 scsi_qla_host_t *vha = tgt->vha;
1502 if (tgt->tgt_stopped) {
1503 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04f,
1504 "Already in tgt->tgt_stopped state\n");
1508 if (!tgt->tgt_stop) {
1509 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00b,
1510 "%s: phase1 stop is not completed\n", __func__);
1515 mutex_lock(&vha->vha_tgt.tgt_mutex);
1517 tgt->tgt_stopped = 1;
1518 mutex_unlock(&vha->vha_tgt.tgt_mutex);
1520 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00c, "Stop of tgt %p finished\n",
1523 EXPORT_SYMBOL(qlt_stop_phase2);
1525 /* Called from qlt_remove_target() -> qla2x00_remove_one() */
1526 static void qlt_release(struct qla_tgt *tgt)
1528 scsi_qla_host_t *vha = tgt->vha;
1532 struct qla_qpair_hint *h;
1533 struct qla_hw_data *ha = vha->hw;
1535 if ((vha->vha_tgt.qla_tgt != NULL) && !tgt->tgt_stop &&
1537 qlt_stop_phase1(tgt);
1539 if ((vha->vha_tgt.qla_tgt != NULL) && !tgt->tgt_stopped)
1540 qlt_stop_phase2(tgt);
1542 for (i = 0; i < vha->hw->max_qpairs + 1; i++) {
1543 unsigned long flags;
1545 h = &tgt->qphints[i];
1547 spin_lock_irqsave(h->qpair->qp_lock_ptr, flags);
1548 list_del(&h->hint_elem);
1549 spin_unlock_irqrestore(h->qpair->qp_lock_ptr, flags);
1553 kfree(tgt->qphints);
1554 mutex_lock(&qla_tgt_mutex);
1555 list_del(&vha->vha_tgt.qla_tgt->tgt_list_entry);
1556 mutex_unlock(&qla_tgt_mutex);
1558 btree_for_each_safe64(&tgt->lun_qpair_map, key, node)
1559 btree_remove64(&tgt->lun_qpair_map, key);
1561 btree_destroy64(&tgt->lun_qpair_map);
1563 if (ha->tgt.tgt_ops && ha->tgt.tgt_ops->remove_target)
1564 ha->tgt.tgt_ops->remove_target(vha);
1566 vha->vha_tgt.qla_tgt = NULL;
1568 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00d,
1569 "Release of tgt %p finished\n", tgt);
1574 /* ha->hardware_lock supposed to be held on entry */
1575 static int qlt_sched_sess_work(struct qla_tgt *tgt, int type,
1576 const void *param, unsigned int param_size)
1578 struct qla_tgt_sess_work_param *prm;
1579 unsigned long flags;
1581 prm = kzalloc(sizeof(*prm), GFP_ATOMIC);
1583 ql_dbg(ql_dbg_tgt_mgt, tgt->vha, 0xf050,
1584 "qla_target(%d): Unable to create session "
1585 "work, command will be refused", 0);
1589 ql_dbg(ql_dbg_tgt_mgt, tgt->vha, 0xf00e,
1590 "Scheduling work (type %d, prm %p)"
1591 " to find session for param %p (size %d, tgt %p)\n",
1592 type, prm, param, param_size, tgt);
1595 memcpy(&prm->tm_iocb, param, param_size);
1597 spin_lock_irqsave(&tgt->sess_work_lock, flags);
1598 list_add_tail(&prm->sess_works_list_entry, &tgt->sess_works_list);
1599 spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
1601 schedule_work(&tgt->sess_work);
1607 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
1609 static void qlt_send_notify_ack(struct qla_qpair *qpair,
1610 struct imm_ntfy_from_isp *ntfy,
1611 uint32_t add_flags, uint16_t resp_code, int resp_code_valid,
1612 uint16_t srr_flags, uint16_t srr_reject_code, uint8_t srr_explan)
1614 struct scsi_qla_host *vha = qpair->vha;
1615 struct qla_hw_data *ha = vha->hw;
1617 struct nack_to_isp *nack;
1619 if (!ha->flags.fw_started)
1622 ql_dbg(ql_dbg_tgt, vha, 0xe004, "Sending NOTIFY_ACK (ha=%p)\n", ha);
1624 pkt = (request_t *)__qla2x00_alloc_iocbs(qpair, NULL);
1626 ql_dbg(ql_dbg_tgt, vha, 0xe049,
1627 "qla_target(%d): %s failed: unable to allocate "
1628 "request packet\n", vha->vp_idx, __func__);
1632 if (vha->vha_tgt.qla_tgt != NULL)
1633 vha->vha_tgt.qla_tgt->notify_ack_expected++;
1635 pkt->entry_type = NOTIFY_ACK_TYPE;
1636 pkt->entry_count = 1;
1638 nack = (struct nack_to_isp *)pkt;
1639 nack->ox_id = ntfy->ox_id;
1641 nack->u.isp24.handle = QLA_TGT_SKIP_HANDLE;
1642 nack->u.isp24.nport_handle = ntfy->u.isp24.nport_handle;
1643 if (le16_to_cpu(ntfy->u.isp24.status) == IMM_NTFY_ELS) {
1644 nack->u.isp24.flags = ntfy->u.isp24.flags &
1645 cpu_to_le32(NOTIFY24XX_FLAGS_PUREX_IOCB);
1647 nack->u.isp24.srr_rx_id = ntfy->u.isp24.srr_rx_id;
1648 nack->u.isp24.status = ntfy->u.isp24.status;
1649 nack->u.isp24.status_subcode = ntfy->u.isp24.status_subcode;
1650 nack->u.isp24.fw_handle = ntfy->u.isp24.fw_handle;
1651 nack->u.isp24.exchange_address = ntfy->u.isp24.exchange_address;
1652 nack->u.isp24.srr_rel_offs = ntfy->u.isp24.srr_rel_offs;
1653 nack->u.isp24.srr_ui = ntfy->u.isp24.srr_ui;
1654 nack->u.isp24.srr_flags = cpu_to_le16(srr_flags);
1655 nack->u.isp24.srr_reject_code = srr_reject_code;
1656 nack->u.isp24.srr_reject_code_expl = srr_explan;
1657 nack->u.isp24.vp_index = ntfy->u.isp24.vp_index;
1659 ql_dbg(ql_dbg_tgt, vha, 0xe005,
1660 "qla_target(%d): Sending 24xx Notify Ack %d\n",
1661 vha->vp_idx, nack->u.isp24.status);
1663 /* Memory Barrier */
1665 qla2x00_start_iocbs(vha, qpair->req);
1669 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
1671 static void qlt_24xx_send_abts_resp(struct qla_qpair *qpair,
1672 struct abts_recv_from_24xx *abts, uint32_t status,
1675 struct scsi_qla_host *vha = qpair->vha;
1676 struct qla_hw_data *ha = vha->hw;
1677 struct abts_resp_to_24xx *resp;
1681 ql_dbg(ql_dbg_tgt, vha, 0xe006,
1682 "Sending task mgmt ABTS response (ha=%p, atio=%p, status=%x\n",
1685 resp = (struct abts_resp_to_24xx *)qla2x00_alloc_iocbs_ready(qpair,
1688 ql_dbg(ql_dbg_tgt, vha, 0xe04a,
1689 "qla_target(%d): %s failed: unable to allocate "
1690 "request packet", vha->vp_idx, __func__);
1694 resp->entry_type = ABTS_RESP_24XX;
1695 resp->entry_count = 1;
1696 resp->nport_handle = abts->nport_handle;
1697 resp->vp_index = vha->vp_idx;
1698 resp->sof_type = abts->sof_type;
1699 resp->exchange_address = abts->exchange_address;
1700 resp->fcp_hdr_le = abts->fcp_hdr_le;
1701 f_ctl = cpu_to_le32(F_CTL_EXCH_CONTEXT_RESP |
1702 F_CTL_LAST_SEQ | F_CTL_END_SEQ |
1703 F_CTL_SEQ_INITIATIVE);
1704 p = (uint8_t *)&f_ctl;
1705 resp->fcp_hdr_le.f_ctl[0] = *p++;
1706 resp->fcp_hdr_le.f_ctl[1] = *p++;
1707 resp->fcp_hdr_le.f_ctl[2] = *p;
1709 resp->fcp_hdr_le.d_id[0] = abts->fcp_hdr_le.d_id[0];
1710 resp->fcp_hdr_le.d_id[1] = abts->fcp_hdr_le.d_id[1];
1711 resp->fcp_hdr_le.d_id[2] = abts->fcp_hdr_le.d_id[2];
1712 resp->fcp_hdr_le.s_id[0] = abts->fcp_hdr_le.s_id[0];
1713 resp->fcp_hdr_le.s_id[1] = abts->fcp_hdr_le.s_id[1];
1714 resp->fcp_hdr_le.s_id[2] = abts->fcp_hdr_le.s_id[2];
1716 resp->fcp_hdr_le.d_id[0] = abts->fcp_hdr_le.s_id[0];
1717 resp->fcp_hdr_le.d_id[1] = abts->fcp_hdr_le.s_id[1];
1718 resp->fcp_hdr_le.d_id[2] = abts->fcp_hdr_le.s_id[2];
1719 resp->fcp_hdr_le.s_id[0] = abts->fcp_hdr_le.d_id[0];
1720 resp->fcp_hdr_le.s_id[1] = abts->fcp_hdr_le.d_id[1];
1721 resp->fcp_hdr_le.s_id[2] = abts->fcp_hdr_le.d_id[2];
1723 resp->exchange_addr_to_abort = abts->exchange_addr_to_abort;
1724 if (status == FCP_TMF_CMPL) {
1725 resp->fcp_hdr_le.r_ctl = R_CTL_BASIC_LINK_SERV | R_CTL_B_ACC;
1726 resp->payload.ba_acct.seq_id_valid = SEQ_ID_INVALID;
1727 resp->payload.ba_acct.low_seq_cnt = 0x0000;
1728 resp->payload.ba_acct.high_seq_cnt = 0xFFFF;
1729 resp->payload.ba_acct.ox_id = abts->fcp_hdr_le.ox_id;
1730 resp->payload.ba_acct.rx_id = abts->fcp_hdr_le.rx_id;
1732 resp->fcp_hdr_le.r_ctl = R_CTL_BASIC_LINK_SERV | R_CTL_B_RJT;
1733 resp->payload.ba_rjt.reason_code =
1734 BA_RJT_REASON_CODE_UNABLE_TO_PERFORM;
1735 /* Other bytes are zero */
1738 vha->vha_tgt.qla_tgt->abts_resp_expected++;
1740 /* Memory Barrier */
1742 if (qpair->reqq_start_iocbs)
1743 qpair->reqq_start_iocbs(qpair);
1745 qla2x00_start_iocbs(vha, qpair->req);
1749 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
1751 static void qlt_24xx_retry_term_exchange(struct scsi_qla_host *vha,
1752 struct abts_resp_from_24xx_fw *entry)
1754 struct ctio7_to_24xx *ctio;
1756 ql_dbg(ql_dbg_tgt, vha, 0xe007,
1757 "Sending retry TERM EXCH CTIO7 (ha=%p)\n", vha->hw);
1759 ctio = (struct ctio7_to_24xx *)qla2x00_alloc_iocbs_ready(
1760 vha->hw->base_qpair, NULL);
1762 ql_dbg(ql_dbg_tgt, vha, 0xe04b,
1763 "qla_target(%d): %s failed: unable to allocate "
1764 "request packet\n", vha->vp_idx, __func__);
1769 * We've got on entrance firmware's response on by us generated
1770 * ABTS response. So, in it ID fields are reversed.
1773 ctio->entry_type = CTIO_TYPE7;
1774 ctio->entry_count = 1;
1775 ctio->nport_handle = entry->nport_handle;
1776 ctio->handle = QLA_TGT_SKIP_HANDLE | CTIO_COMPLETION_HANDLE_MARK;
1777 ctio->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
1778 ctio->vp_index = vha->vp_idx;
1779 ctio->initiator_id[0] = entry->fcp_hdr_le.d_id[0];
1780 ctio->initiator_id[1] = entry->fcp_hdr_le.d_id[1];
1781 ctio->initiator_id[2] = entry->fcp_hdr_le.d_id[2];
1782 ctio->exchange_addr = entry->exchange_addr_to_abort;
1783 ctio->u.status1.flags = cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_1 |
1784 CTIO7_FLAGS_TERMINATE);
1785 ctio->u.status1.ox_id = cpu_to_le16(entry->fcp_hdr_le.ox_id);
1787 /* Memory Barrier */
1789 qla2x00_start_iocbs(vha, vha->req);
1791 qlt_24xx_send_abts_resp(vha->hw->base_qpair,
1792 (struct abts_recv_from_24xx *)entry,
1793 FCP_TMF_CMPL, true);
1796 static int abort_cmd_for_tag(struct scsi_qla_host *vha, uint32_t tag)
1798 struct qla_tgt_sess_op *op;
1799 struct qla_tgt_cmd *cmd;
1800 unsigned long flags;
1802 spin_lock_irqsave(&vha->cmd_list_lock, flags);
1803 list_for_each_entry(op, &vha->qla_sess_op_cmd_list, cmd_list) {
1804 if (tag == op->atio.u.isp24.exchange_addr) {
1806 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
1811 list_for_each_entry(op, &vha->unknown_atio_list, cmd_list) {
1812 if (tag == op->atio.u.isp24.exchange_addr) {
1814 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
1819 list_for_each_entry(cmd, &vha->qla_cmd_list, cmd_list) {
1820 if (tag == cmd->atio.u.isp24.exchange_addr) {
1822 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
1826 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
1831 /* drop cmds for the given lun
1832 * XXX only looks for cmds on the port through which lun reset was recieved
1833 * XXX does not go through the list of other port (which may have cmds
1836 static void abort_cmds_for_lun(struct scsi_qla_host *vha,
1837 u64 lun, uint8_t *s_id)
1839 struct qla_tgt_sess_op *op;
1840 struct qla_tgt_cmd *cmd;
1842 unsigned long flags;
1844 key = sid_to_key(s_id);
1845 spin_lock_irqsave(&vha->cmd_list_lock, flags);
1846 list_for_each_entry(op, &vha->qla_sess_op_cmd_list, cmd_list) {
1850 op_key = sid_to_key(op->atio.u.isp24.fcp_hdr.s_id);
1851 op_lun = scsilun_to_int(
1852 (struct scsi_lun *)&op->atio.u.isp24.fcp_cmnd.lun);
1853 if (op_key == key && op_lun == lun)
1857 list_for_each_entry(op, &vha->unknown_atio_list, cmd_list) {
1861 op_key = sid_to_key(op->atio.u.isp24.fcp_hdr.s_id);
1862 op_lun = scsilun_to_int(
1863 (struct scsi_lun *)&op->atio.u.isp24.fcp_cmnd.lun);
1864 if (op_key == key && op_lun == lun)
1868 list_for_each_entry(cmd, &vha->qla_cmd_list, cmd_list) {
1872 cmd_key = sid_to_key(cmd->atio.u.isp24.fcp_hdr.s_id);
1873 cmd_lun = scsilun_to_int(
1874 (struct scsi_lun *)&cmd->atio.u.isp24.fcp_cmnd.lun);
1875 if (cmd_key == key && cmd_lun == lun)
1878 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
1881 /* ha->hardware_lock supposed to be held on entry */
1882 static int __qlt_24xx_handle_abts(struct scsi_qla_host *vha,
1883 struct abts_recv_from_24xx *abts, struct fc_port *sess)
1885 struct qla_hw_data *ha = vha->hw;
1886 struct qla_tgt_mgmt_cmd *mcmd;
1889 if (abort_cmd_for_tag(vha, abts->exchange_addr_to_abort)) {
1890 /* send TASK_ABORT response immediately */
1891 qlt_24xx_send_abts_resp(ha->base_qpair, abts, FCP_TMF_CMPL, false);
1895 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00f,
1896 "qla_target(%d): task abort (tag=%d)\n",
1897 vha->vp_idx, abts->exchange_addr_to_abort);
1899 mcmd = mempool_alloc(qla_tgt_mgmt_cmd_mempool, GFP_ATOMIC);
1901 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf051,
1902 "qla_target(%d): %s: Allocation of ABORT cmd failed",
1903 vha->vp_idx, __func__);
1906 memset(mcmd, 0, sizeof(*mcmd));
1909 memcpy(&mcmd->orig_iocb.abts, abts, sizeof(mcmd->orig_iocb.abts));
1910 mcmd->reset_count = ha->base_qpair->chip_reset;
1911 mcmd->tmr_func = QLA_TGT_ABTS;
1912 mcmd->qpair = ha->base_qpair;
1916 * LUN is looked up by target-core internally based on the passed
1917 * abts->exchange_addr_to_abort tag.
1919 rc = ha->tgt.tgt_ops->handle_tmr(mcmd, 0, mcmd->tmr_func,
1920 abts->exchange_addr_to_abort);
1922 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf052,
1923 "qla_target(%d): tgt_ops->handle_tmr()"
1924 " failed: %d", vha->vp_idx, rc);
1925 mempool_free(mcmd, qla_tgt_mgmt_cmd_mempool);
1933 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
1935 static void qlt_24xx_handle_abts(struct scsi_qla_host *vha,
1936 struct abts_recv_from_24xx *abts)
1938 struct qla_hw_data *ha = vha->hw;
1939 struct fc_port *sess;
1940 uint32_t tag = abts->exchange_addr_to_abort;
1943 unsigned long flags;
1945 if (le32_to_cpu(abts->fcp_hdr_le.parameter) & ABTS_PARAM_ABORT_SEQ) {
1946 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf053,
1947 "qla_target(%d): ABTS: Abort Sequence not "
1948 "supported\n", vha->vp_idx);
1949 qlt_24xx_send_abts_resp(ha->base_qpair, abts, FCP_TMF_REJECTED,
1954 if (tag == ATIO_EXCHANGE_ADDRESS_UNKNOWN) {
1955 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf010,
1956 "qla_target(%d): ABTS: Unknown Exchange "
1957 "Address received\n", vha->vp_idx);
1958 qlt_24xx_send_abts_resp(ha->base_qpair, abts, FCP_TMF_REJECTED,
1963 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf011,
1964 "qla_target(%d): task abort (s_id=%x:%x:%x, "
1965 "tag=%d, param=%x)\n", vha->vp_idx, abts->fcp_hdr_le.s_id[2],
1966 abts->fcp_hdr_le.s_id[1], abts->fcp_hdr_le.s_id[0], tag,
1967 le32_to_cpu(abts->fcp_hdr_le.parameter));
1969 s_id[0] = abts->fcp_hdr_le.s_id[2];
1970 s_id[1] = abts->fcp_hdr_le.s_id[1];
1971 s_id[2] = abts->fcp_hdr_le.s_id[0];
1973 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
1974 sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha, s_id);
1976 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf012,
1977 "qla_target(%d): task abort for non-existant session\n",
1979 rc = qlt_sched_sess_work(vha->vha_tgt.qla_tgt,
1980 QLA_TGT_SESS_WORK_ABORT, abts, sizeof(*abts));
1982 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
1985 qlt_24xx_send_abts_resp(ha->base_qpair, abts,
1986 FCP_TMF_REJECTED, false);
1990 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
1993 if (sess->deleted) {
1994 qlt_24xx_send_abts_resp(ha->base_qpair, abts, FCP_TMF_REJECTED,
1999 rc = __qlt_24xx_handle_abts(vha, abts, sess);
2001 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf054,
2002 "qla_target(%d): __qlt_24xx_handle_abts() failed: %d\n",
2004 qlt_24xx_send_abts_resp(ha->base_qpair, abts, FCP_TMF_REJECTED,
2011 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
2013 static void qlt_24xx_send_task_mgmt_ctio(struct qla_qpair *qpair,
2014 struct qla_tgt_mgmt_cmd *mcmd, uint32_t resp_code)
2016 struct scsi_qla_host *ha = mcmd->vha;
2017 struct atio_from_isp *atio = &mcmd->orig_iocb.atio;
2018 struct ctio7_to_24xx *ctio;
2021 ql_dbg(ql_dbg_tgt, ha, 0xe008,
2022 "Sending task mgmt CTIO7 (ha=%p, atio=%p, resp_code=%x\n",
2023 ha, atio, resp_code);
2026 ctio = (struct ctio7_to_24xx *)__qla2x00_alloc_iocbs(qpair, NULL);
2028 ql_dbg(ql_dbg_tgt, ha, 0xe04c,
2029 "qla_target(%d): %s failed: unable to allocate "
2030 "request packet\n", ha->vp_idx, __func__);
2034 ctio->entry_type = CTIO_TYPE7;
2035 ctio->entry_count = 1;
2036 ctio->handle = QLA_TGT_SKIP_HANDLE | CTIO_COMPLETION_HANDLE_MARK;
2037 ctio->nport_handle = mcmd->sess->loop_id;
2038 ctio->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
2039 ctio->vp_index = ha->vp_idx;
2040 ctio->initiator_id[0] = atio->u.isp24.fcp_hdr.s_id[2];
2041 ctio->initiator_id[1] = atio->u.isp24.fcp_hdr.s_id[1];
2042 ctio->initiator_id[2] = atio->u.isp24.fcp_hdr.s_id[0];
2043 ctio->exchange_addr = atio->u.isp24.exchange_addr;
2044 temp = (atio->u.isp24.attr << 9)|
2045 CTIO7_FLAGS_STATUS_MODE_1 | CTIO7_FLAGS_SEND_STATUS;
2046 ctio->u.status1.flags = cpu_to_le16(temp);
2047 temp = be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id);
2048 ctio->u.status1.ox_id = cpu_to_le16(temp);
2049 ctio->u.status1.scsi_status =
2050 cpu_to_le16(SS_RESPONSE_INFO_LEN_VALID);
2051 ctio->u.status1.response_len = cpu_to_le16(8);
2052 ctio->u.status1.sense_data[0] = resp_code;
2054 /* Memory Barrier */
2056 if (qpair->reqq_start_iocbs)
2057 qpair->reqq_start_iocbs(qpair);
2059 qla2x00_start_iocbs(ha, qpair->req);
2062 void qlt_free_mcmd(struct qla_tgt_mgmt_cmd *mcmd)
2064 mempool_free(mcmd, qla_tgt_mgmt_cmd_mempool);
2066 EXPORT_SYMBOL(qlt_free_mcmd);
2069 * ha->hardware_lock supposed to be held on entry. Might drop it, then
2072 void qlt_send_resp_ctio(struct qla_qpair *qpair, struct qla_tgt_cmd *cmd,
2073 uint8_t scsi_status, uint8_t sense_key, uint8_t asc, uint8_t ascq)
2075 struct atio_from_isp *atio = &cmd->atio;
2076 struct ctio7_to_24xx *ctio;
2078 struct scsi_qla_host *vha = cmd->vha;
2080 ql_dbg(ql_dbg_tgt_dif, vha, 0x3066,
2081 "Sending response CTIO7 (vha=%p, atio=%p, scsi_status=%02x, "
2082 "sense_key=%02x, asc=%02x, ascq=%02x",
2083 vha, atio, scsi_status, sense_key, asc, ascq);
2085 ctio = (struct ctio7_to_24xx *)qla2x00_alloc_iocbs(vha, NULL);
2087 ql_dbg(ql_dbg_async, vha, 0x3067,
2088 "qla2x00t(%ld): %s failed: unable to allocate request packet",
2089 vha->host_no, __func__);
2093 ctio->entry_type = CTIO_TYPE7;
2094 ctio->entry_count = 1;
2095 ctio->handle = QLA_TGT_SKIP_HANDLE;
2096 ctio->nport_handle = cmd->sess->loop_id;
2097 ctio->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
2098 ctio->vp_index = vha->vp_idx;
2099 ctio->initiator_id[0] = atio->u.isp24.fcp_hdr.s_id[2];
2100 ctio->initiator_id[1] = atio->u.isp24.fcp_hdr.s_id[1];
2101 ctio->initiator_id[2] = atio->u.isp24.fcp_hdr.s_id[0];
2102 ctio->exchange_addr = atio->u.isp24.exchange_addr;
2103 temp = (atio->u.isp24.attr << 9) |
2104 CTIO7_FLAGS_STATUS_MODE_1 | CTIO7_FLAGS_SEND_STATUS;
2105 ctio->u.status1.flags = cpu_to_le16(temp);
2106 temp = be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id);
2107 ctio->u.status1.ox_id = cpu_to_le16(temp);
2108 ctio->u.status1.scsi_status =
2109 cpu_to_le16(SS_RESPONSE_INFO_LEN_VALID | scsi_status);
2110 ctio->u.status1.response_len = cpu_to_le16(18);
2111 ctio->u.status1.residual = cpu_to_le32(get_datalen_for_atio(atio));
2113 if (ctio->u.status1.residual != 0)
2114 ctio->u.status1.scsi_status |=
2115 cpu_to_le16(SS_RESIDUAL_UNDER);
2117 /* Response code and sense key */
2118 put_unaligned_le32(((0x70 << 24) | (sense_key << 8)),
2119 (&ctio->u.status1.sense_data)[0]);
2120 /* Additional sense length */
2121 put_unaligned_le32(0x0a, (&ctio->u.status1.sense_data)[1]);
2123 put_unaligned_le32(((asc << 24) | (ascq << 16)),
2124 (&ctio->u.status1.sense_data)[3]);
2126 /* Memory Barrier */
2129 if (qpair->reqq_start_iocbs)
2130 qpair->reqq_start_iocbs(qpair);
2132 qla2x00_start_iocbs(vha, qpair->req);
2138 /* callback from target fabric module code */
2139 void qlt_xmit_tm_rsp(struct qla_tgt_mgmt_cmd *mcmd)
2141 struct scsi_qla_host *vha = mcmd->sess->vha;
2142 struct qla_hw_data *ha = vha->hw;
2143 unsigned long flags;
2144 struct qla_qpair *qpair = mcmd->qpair;
2146 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf013,
2147 "TM response mcmd (%p) status %#x state %#x",
2148 mcmd, mcmd->fc_tm_rsp, mcmd->flags);
2150 spin_lock_irqsave(qpair->qp_lock_ptr, flags);
2152 if (!vha->flags.online || mcmd->reset_count != qpair->chip_reset) {
2154 * Either the port is not online or this request was from
2155 * previous life, just abort the processing.
2157 ql_dbg(ql_dbg_async, vha, 0xe100,
2158 "RESET-TMR online/active/old-count/new-count = %d/%d/%d/%d.\n",
2159 vha->flags.online, qla2x00_reset_active(vha),
2160 mcmd->reset_count, qpair->chip_reset);
2161 ha->tgt.tgt_ops->free_mcmd(mcmd);
2162 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
2166 if (mcmd->flags == QLA24XX_MGMT_SEND_NACK) {
2167 if (mcmd->orig_iocb.imm_ntfy.u.isp24.status_subcode ==
2169 mcmd->orig_iocb.imm_ntfy.u.isp24.status_subcode ==
2171 mcmd->orig_iocb.imm_ntfy.u.isp24.status_subcode ==
2173 ql_dbg(ql_dbg_disc, vha, 0x2106,
2174 "TM response logo %phC status %#x state %#x",
2175 mcmd->sess->port_name, mcmd->fc_tm_rsp,
2177 qlt_schedule_sess_for_deletion_lock(mcmd->sess);
2179 qlt_send_notify_ack(vha->hw->base_qpair,
2180 &mcmd->orig_iocb.imm_ntfy, 0, 0, 0, 0, 0, 0);
2183 if (mcmd->orig_iocb.atio.u.raw.entry_type == ABTS_RECV_24XX)
2184 qlt_24xx_send_abts_resp(qpair, &mcmd->orig_iocb.abts,
2185 mcmd->fc_tm_rsp, false);
2187 qlt_24xx_send_task_mgmt_ctio(qpair, mcmd,
2191 * Make the callback for ->free_mcmd() to queue_work() and invoke
2192 * target_put_sess_cmd() to drop cmd_kref to 1. The final
2193 * target_put_sess_cmd() call will be made from TFO->check_stop_free()
2194 * -> tcm_qla2xxx_check_stop_free() to release the TMR associated se_cmd
2195 * descriptor after TFO->queue_tm_rsp() -> tcm_qla2xxx_queue_tm_rsp() ->
2196 * qlt_xmit_tm_rsp() returns here..
2198 ha->tgt.tgt_ops->free_mcmd(mcmd);
2199 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
2201 EXPORT_SYMBOL(qlt_xmit_tm_rsp);
2204 static int qlt_pci_map_calc_cnt(struct qla_tgt_prm *prm)
2206 struct qla_tgt_cmd *cmd = prm->cmd;
2208 BUG_ON(cmd->sg_cnt == 0);
2210 prm->sg = (struct scatterlist *)cmd->sg;
2211 prm->seg_cnt = pci_map_sg(cmd->qpair->pdev, cmd->sg,
2212 cmd->sg_cnt, cmd->dma_data_direction);
2213 if (unlikely(prm->seg_cnt == 0))
2216 prm->cmd->sg_mapped = 1;
2218 if (cmd->se_cmd.prot_op == TARGET_PROT_NORMAL) {
2220 * If greater than four sg entries then we need to allocate
2221 * the continuation entries
2223 if (prm->seg_cnt > QLA_TGT_DATASEGS_PER_CMD_24XX)
2224 prm->req_cnt += DIV_ROUND_UP(prm->seg_cnt -
2225 QLA_TGT_DATASEGS_PER_CMD_24XX,
2226 QLA_TGT_DATASEGS_PER_CONT_24XX);
2229 if ((cmd->se_cmd.prot_op == TARGET_PROT_DIN_INSERT) ||
2230 (cmd->se_cmd.prot_op == TARGET_PROT_DOUT_STRIP)) {
2231 prm->seg_cnt = DIV_ROUND_UP(cmd->bufflen, cmd->blk_sz);
2232 prm->tot_dsds = prm->seg_cnt;
2234 prm->tot_dsds = prm->seg_cnt;
2236 if (cmd->prot_sg_cnt) {
2237 prm->prot_sg = cmd->prot_sg;
2238 prm->prot_seg_cnt = pci_map_sg(cmd->qpair->pdev,
2239 cmd->prot_sg, cmd->prot_sg_cnt,
2240 cmd->dma_data_direction);
2241 if (unlikely(prm->prot_seg_cnt == 0))
2244 if ((cmd->se_cmd.prot_op == TARGET_PROT_DIN_INSERT) ||
2245 (cmd->se_cmd.prot_op == TARGET_PROT_DOUT_STRIP)) {
2246 /* Dif Bundling not support here */
2247 prm->prot_seg_cnt = DIV_ROUND_UP(cmd->bufflen,
2249 prm->tot_dsds += prm->prot_seg_cnt;
2251 prm->tot_dsds += prm->prot_seg_cnt;
2258 ql_dbg_qp(ql_dbg_tgt, prm->cmd->qpair, 0xe04d,
2259 "qla_target(%d): PCI mapping failed: sg_cnt=%d",
2260 0, prm->cmd->sg_cnt);
2264 static void qlt_unmap_sg(struct scsi_qla_host *vha, struct qla_tgt_cmd *cmd)
2266 struct qla_hw_data *ha;
2267 struct qla_qpair *qpair;
2268 if (!cmd->sg_mapped)
2273 pci_unmap_sg(qpair->pdev, cmd->sg, cmd->sg_cnt,
2274 cmd->dma_data_direction);
2277 if (cmd->prot_sg_cnt)
2278 pci_unmap_sg(qpair->pdev, cmd->prot_sg, cmd->prot_sg_cnt,
2279 cmd->dma_data_direction);
2284 if (cmd->ctx_dsd_alloced)
2285 qla2x00_clean_dsd_pool(ha, cmd->ctx);
2287 dma_pool_free(ha->dl_dma_pool, cmd->ctx, cmd->ctx->crc_ctx_dma);
2290 static int qlt_check_reserve_free_req(struct qla_qpair *qpair,
2294 struct req_que *req = qpair->req;
2296 if (req->cnt < (req_cnt + 2)) {
2297 cnt = (uint16_t)(qpair->use_shadow_reg ? *req->out_ptr :
2298 RD_REG_DWORD_RELAXED(req->req_q_out));
2300 if (req->ring_index < cnt)
2301 req->cnt = cnt - req->ring_index;
2303 req->cnt = req->length - (req->ring_index - cnt);
2305 if (unlikely(req->cnt < (req_cnt + 2)))
2309 req->cnt -= req_cnt;
2315 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
2317 static inline void *qlt_get_req_pkt(struct req_que *req)
2319 /* Adjust ring index. */
2321 if (req->ring_index == req->length) {
2322 req->ring_index = 0;
2323 req->ring_ptr = req->ring;
2327 return (cont_entry_t *)req->ring_ptr;
2330 /* ha->hardware_lock supposed to be held on entry */
2331 static inline uint32_t qlt_make_handle(struct qla_qpair *qpair)
2336 struct req_que *req = qpair->req;
2338 h = req->current_outstanding_cmd;
2340 for (index = 1; index < req->num_outstanding_cmds; index++) {
2342 if (h == req->num_outstanding_cmds)
2345 if (h == QLA_TGT_SKIP_HANDLE)
2348 if (!req->outstanding_cmds[h]) {
2355 req->current_outstanding_cmd = h;
2357 ql_dbg(ql_dbg_io, qpair->vha, 0x305b,
2358 "qla_target(%d): Ran out of empty cmd slots\n",
2359 qpair->vha->vp_idx);
2360 h = QLA_TGT_NULL_HANDLE;
2366 /* ha->hardware_lock supposed to be held on entry */
2367 static int qlt_24xx_build_ctio_pkt(struct qla_qpair *qpair,
2368 struct qla_tgt_prm *prm)
2371 struct ctio7_to_24xx *pkt;
2372 struct atio_from_isp *atio = &prm->cmd->atio;
2375 pkt = (struct ctio7_to_24xx *)qpair->req->ring_ptr;
2377 memset(pkt, 0, sizeof(*pkt));
2379 pkt->entry_type = CTIO_TYPE7;
2380 pkt->entry_count = (uint8_t)prm->req_cnt;
2381 pkt->vp_index = prm->cmd->vp_idx;
2383 h = qlt_make_handle(qpair);
2384 if (unlikely(h == QLA_TGT_NULL_HANDLE)) {
2386 * CTIO type 7 from the firmware doesn't provide a way to
2387 * know the initiator's LOOP ID, hence we can't find
2388 * the session and, so, the command.
2392 qpair->req->outstanding_cmds[h] = (srb_t *)prm->cmd;
2394 pkt->handle = MAKE_HANDLE(qpair->req->id, h);
2395 pkt->handle |= CTIO_COMPLETION_HANDLE_MARK;
2396 pkt->nport_handle = cpu_to_le16(prm->cmd->loop_id);
2397 pkt->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
2398 pkt->initiator_id[0] = atio->u.isp24.fcp_hdr.s_id[2];
2399 pkt->initiator_id[1] = atio->u.isp24.fcp_hdr.s_id[1];
2400 pkt->initiator_id[2] = atio->u.isp24.fcp_hdr.s_id[0];
2401 pkt->exchange_addr = atio->u.isp24.exchange_addr;
2402 temp = atio->u.isp24.attr << 9;
2403 pkt->u.status0.flags |= cpu_to_le16(temp);
2404 temp = be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id);
2405 pkt->u.status0.ox_id = cpu_to_le16(temp);
2406 pkt->u.status0.relative_offset = cpu_to_le32(prm->cmd->offset);
2412 * ha->hardware_lock supposed to be held on entry. We have already made sure
2413 * that there is sufficient amount of request entries to not drop it.
2415 static void qlt_load_cont_data_segments(struct qla_tgt_prm *prm)
2418 uint32_t *dword_ptr;
2420 /* Build continuation packets */
2421 while (prm->seg_cnt > 0) {
2422 cont_a64_entry_t *cont_pkt64 =
2423 (cont_a64_entry_t *)qlt_get_req_pkt(
2424 prm->cmd->qpair->req);
2427 * Make sure that from cont_pkt64 none of
2428 * 64-bit specific fields used for 32-bit
2429 * addressing. Cast to (cont_entry_t *) for
2433 memset(cont_pkt64, 0, sizeof(*cont_pkt64));
2435 cont_pkt64->entry_count = 1;
2436 cont_pkt64->sys_define = 0;
2438 cont_pkt64->entry_type = CONTINUE_A64_TYPE;
2439 dword_ptr = (uint32_t *)&cont_pkt64->dseg_0_address;
2441 /* Load continuation entry data segments */
2443 cnt < QLA_TGT_DATASEGS_PER_CONT_24XX && prm->seg_cnt;
2444 cnt++, prm->seg_cnt--) {
2446 cpu_to_le32(pci_dma_lo32
2447 (sg_dma_address(prm->sg)));
2448 *dword_ptr++ = cpu_to_le32(pci_dma_hi32
2449 (sg_dma_address(prm->sg)));
2450 *dword_ptr++ = cpu_to_le32(sg_dma_len(prm->sg));
2452 prm->sg = sg_next(prm->sg);
2458 * ha->hardware_lock supposed to be held on entry. We have already made sure
2459 * that there is sufficient amount of request entries to not drop it.
2461 static void qlt_load_data_segments(struct qla_tgt_prm *prm)
2464 uint32_t *dword_ptr;
2465 struct ctio7_to_24xx *pkt24 = (struct ctio7_to_24xx *)prm->pkt;
2467 pkt24->u.status0.transfer_length = cpu_to_le32(prm->cmd->bufflen);
2469 /* Setup packet address segment pointer */
2470 dword_ptr = pkt24->u.status0.dseg_0_address;
2472 /* Set total data segment count */
2474 pkt24->dseg_count = cpu_to_le16(prm->seg_cnt);
2476 if (prm->seg_cnt == 0) {
2477 /* No data transfer */
2483 /* If scatter gather */
2485 /* Load command entry data segments */
2487 (cnt < QLA_TGT_DATASEGS_PER_CMD_24XX) && prm->seg_cnt;
2488 cnt++, prm->seg_cnt--) {
2490 cpu_to_le32(pci_dma_lo32(sg_dma_address(prm->sg)));
2492 *dword_ptr++ = cpu_to_le32(pci_dma_hi32(
2493 sg_dma_address(prm->sg)));
2495 *dword_ptr++ = cpu_to_le32(sg_dma_len(prm->sg));
2497 prm->sg = sg_next(prm->sg);
2500 qlt_load_cont_data_segments(prm);
2503 static inline int qlt_has_data(struct qla_tgt_cmd *cmd)
2505 return cmd->bufflen > 0;
2508 static void qlt_print_dif_err(struct qla_tgt_prm *prm)
2510 struct qla_tgt_cmd *cmd;
2511 struct scsi_qla_host *vha;
2513 /* asc 0x10=dif error */
2514 if (prm->sense_buffer && (prm->sense_buffer[12] == 0x10)) {
2518 switch (prm->sense_buffer[13]) {
2520 ql_dbg(ql_dbg_tgt_dif, vha, 0xe00b,
2521 "BE detected Guard TAG ERR: lba[0x%llx|%lld] len[0x%x] "
2522 "se_cmd=%p tag[%x]",
2523 cmd->lba, cmd->lba, cmd->num_blks, &cmd->se_cmd,
2524 cmd->atio.u.isp24.exchange_addr);
2527 ql_dbg(ql_dbg_tgt_dif, vha, 0xe00c,
2528 "BE detected APP TAG ERR: lba[0x%llx|%lld] len[0x%x] "
2529 "se_cmd=%p tag[%x]",
2530 cmd->lba, cmd->lba, cmd->num_blks, &cmd->se_cmd,
2531 cmd->atio.u.isp24.exchange_addr);
2534 ql_dbg(ql_dbg_tgt_dif, vha, 0xe00f,
2535 "BE detected REF TAG ERR: lba[0x%llx|%lld] len[0x%x] "
2536 "se_cmd=%p tag[%x]",
2537 cmd->lba, cmd->lba, cmd->num_blks, &cmd->se_cmd,
2538 cmd->atio.u.isp24.exchange_addr);
2541 ql_dbg(ql_dbg_tgt_dif, vha, 0xe010,
2542 "BE detected Dif ERR: lba[%llx|%lld] len[%x] "
2543 "se_cmd=%p tag[%x]",
2544 cmd->lba, cmd->lba, cmd->num_blks, &cmd->se_cmd,
2545 cmd->atio.u.isp24.exchange_addr);
2548 ql_dump_buffer(ql_dbg_tgt_dif, vha, 0xe011, cmd->cdb, 16);
2553 * Called without ha->hardware_lock held
2555 static int qlt_pre_xmit_response(struct qla_tgt_cmd *cmd,
2556 struct qla_tgt_prm *prm, int xmit_type, uint8_t scsi_status,
2557 uint32_t *full_req_cnt)
2559 struct se_cmd *se_cmd = &cmd->se_cmd;
2560 struct qla_qpair *qpair = cmd->qpair;
2563 prm->tgt = cmd->tgt;
2565 prm->rq_result = scsi_status;
2566 prm->sense_buffer = &cmd->sense_buffer[0];
2567 prm->sense_buffer_len = TRANSPORT_SENSE_BUFFER;
2572 prm->add_status_pkt = 0;
2573 prm->prot_sg = NULL;
2574 prm->prot_seg_cnt = 0;
2577 if ((xmit_type & QLA_TGT_XMIT_DATA) && qlt_has_data(cmd)) {
2578 if (qlt_pci_map_calc_cnt(prm) != 0)
2582 *full_req_cnt = prm->req_cnt;
2584 if (se_cmd->se_cmd_flags & SCF_UNDERFLOW_BIT) {
2585 prm->residual = se_cmd->residual_count;
2586 ql_dbg_qp(ql_dbg_io + ql_dbg_verbose, qpair, 0x305c,
2587 "Residual underflow: %d (tag %lld, op %x, bufflen %d, rq_result %x)\n",
2588 prm->residual, se_cmd->tag,
2589 se_cmd->t_task_cdb ? se_cmd->t_task_cdb[0] : 0,
2590 cmd->bufflen, prm->rq_result);
2591 prm->rq_result |= SS_RESIDUAL_UNDER;
2592 } else if (se_cmd->se_cmd_flags & SCF_OVERFLOW_BIT) {
2593 prm->residual = se_cmd->residual_count;
2594 ql_dbg_qp(ql_dbg_io, qpair, 0x305d,
2595 "Residual overflow: %d (tag %lld, op %x, bufflen %d, rq_result %x)\n",
2596 prm->residual, se_cmd->tag, se_cmd->t_task_cdb ?
2597 se_cmd->t_task_cdb[0] : 0, cmd->bufflen, prm->rq_result);
2598 prm->rq_result |= SS_RESIDUAL_OVER;
2601 if (xmit_type & QLA_TGT_XMIT_STATUS) {
2603 * If QLA_TGT_XMIT_DATA is not set, add_status_pkt will be
2604 * ignored in *xmit_response() below
2606 if (qlt_has_data(cmd)) {
2607 if (QLA_TGT_SENSE_VALID(prm->sense_buffer) ||
2608 (IS_FWI2_CAPABLE(cmd->vha->hw) &&
2609 (prm->rq_result != 0))) {
2610 prm->add_status_pkt = 1;
2619 static inline int qlt_need_explicit_conf(struct qla_tgt_cmd *cmd,
2622 if (cmd->qpair->enable_class_2)
2626 return cmd->conf_compl_supported;
2628 return cmd->qpair->enable_explicit_conf &&
2629 cmd->conf_compl_supported;
2632 static void qlt_24xx_init_ctio_to_isp(struct ctio7_to_24xx *ctio,
2633 struct qla_tgt_prm *prm)
2635 prm->sense_buffer_len = min_t(uint32_t, prm->sense_buffer_len,
2636 (uint32_t)sizeof(ctio->u.status1.sense_data));
2637 ctio->u.status0.flags |= cpu_to_le16(CTIO7_FLAGS_SEND_STATUS);
2638 if (qlt_need_explicit_conf(prm->cmd, 0)) {
2639 ctio->u.status0.flags |= cpu_to_le16(
2640 CTIO7_FLAGS_EXPLICIT_CONFORM |
2641 CTIO7_FLAGS_CONFORM_REQ);
2643 ctio->u.status0.residual = cpu_to_le32(prm->residual);
2644 ctio->u.status0.scsi_status = cpu_to_le16(prm->rq_result);
2645 if (QLA_TGT_SENSE_VALID(prm->sense_buffer)) {
2648 if (qlt_need_explicit_conf(prm->cmd, 1)) {
2649 if ((prm->rq_result & SS_SCSI_STATUS_BYTE) != 0) {
2650 ql_dbg_qp(ql_dbg_tgt, prm->cmd->qpair, 0xe017,
2651 "Skipping EXPLICIT_CONFORM and "
2652 "CTIO7_FLAGS_CONFORM_REQ for FCP READ w/ "
2653 "non GOOD status\n");
2654 goto skip_explict_conf;
2656 ctio->u.status1.flags |= cpu_to_le16(
2657 CTIO7_FLAGS_EXPLICIT_CONFORM |
2658 CTIO7_FLAGS_CONFORM_REQ);
2661 ctio->u.status1.flags &=
2662 ~cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_0);
2663 ctio->u.status1.flags |=
2664 cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_1);
2665 ctio->u.status1.scsi_status |=
2666 cpu_to_le16(SS_SENSE_LEN_VALID);
2667 ctio->u.status1.sense_length =
2668 cpu_to_le16(prm->sense_buffer_len);
2669 for (i = 0; i < prm->sense_buffer_len/4; i++)
2670 ((uint32_t *)ctio->u.status1.sense_data)[i] =
2671 cpu_to_be32(((uint32_t *)prm->sense_buffer)[i]);
2673 qlt_print_dif_err(prm);
2676 ctio->u.status1.flags &=
2677 ~cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_0);
2678 ctio->u.status1.flags |=
2679 cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_1);
2680 ctio->u.status1.sense_length = 0;
2681 memset(ctio->u.status1.sense_data, 0,
2682 sizeof(ctio->u.status1.sense_data));
2685 /* Sense with len > 24, is it possible ??? */
2689 qlt_hba_err_chk_enabled(struct se_cmd *se_cmd)
2691 switch (se_cmd->prot_op) {
2692 case TARGET_PROT_DOUT_INSERT:
2693 case TARGET_PROT_DIN_STRIP:
2694 if (ql2xenablehba_err_chk >= 1)
2697 case TARGET_PROT_DOUT_PASS:
2698 case TARGET_PROT_DIN_PASS:
2699 if (ql2xenablehba_err_chk >= 2)
2702 case TARGET_PROT_DIN_INSERT:
2703 case TARGET_PROT_DOUT_STRIP:
2712 qla_tgt_ref_mask_check(struct se_cmd *se_cmd)
2714 switch (se_cmd->prot_op) {
2715 case TARGET_PROT_DIN_INSERT:
2716 case TARGET_PROT_DOUT_INSERT:
2717 case TARGET_PROT_DIN_STRIP:
2718 case TARGET_PROT_DOUT_STRIP:
2719 case TARGET_PROT_DIN_PASS:
2720 case TARGET_PROT_DOUT_PASS:
2729 * qla_tgt_set_dif_tags - Extract Ref and App tags from SCSI command
2732 qla_tgt_set_dif_tags(struct qla_tgt_cmd *cmd, struct crc_context *ctx,
2733 uint16_t *pfw_prot_opts)
2735 struct se_cmd *se_cmd = &cmd->se_cmd;
2736 uint32_t lba = 0xffffffff & se_cmd->t_task_lba;
2737 scsi_qla_host_t *vha = cmd->tgt->vha;
2738 struct qla_hw_data *ha = vha->hw;
2742 * wait till Mode Sense/Select cmd, modepage Ah, subpage 2
2743 * have been immplemented by TCM, before AppTag is avail.
2744 * Look for modesense_handlers[]
2747 ctx->app_tag_mask[0] = 0x0;
2748 ctx->app_tag_mask[1] = 0x0;
2750 if (IS_PI_UNINIT_CAPABLE(ha)) {
2751 if ((se_cmd->prot_type == TARGET_DIF_TYPE1_PROT) ||
2752 (se_cmd->prot_type == TARGET_DIF_TYPE2_PROT))
2753 *pfw_prot_opts |= PO_DIS_VALD_APP_ESC;
2754 else if (se_cmd->prot_type == TARGET_DIF_TYPE3_PROT)
2755 *pfw_prot_opts |= PO_DIS_VALD_APP_REF_ESC;
2758 t32 = ha->tgt.tgt_ops->get_dif_tags(cmd, pfw_prot_opts);
2760 switch (se_cmd->prot_type) {
2761 case TARGET_DIF_TYPE0_PROT:
2763 * No check for ql2xenablehba_err_chk, as it
2764 * would be an I/O error if hba tag generation
2767 ctx->ref_tag = cpu_to_le32(lba);
2768 /* enable ALL bytes of the ref tag */
2769 ctx->ref_tag_mask[0] = 0xff;
2770 ctx->ref_tag_mask[1] = 0xff;
2771 ctx->ref_tag_mask[2] = 0xff;
2772 ctx->ref_tag_mask[3] = 0xff;
2774 case TARGET_DIF_TYPE1_PROT:
2776 * For TYPE 1 protection: 16 bit GUARD tag, 32 bit
2777 * REF tag, and 16 bit app tag.
2779 ctx->ref_tag = cpu_to_le32(lba);
2780 if (!qla_tgt_ref_mask_check(se_cmd) ||
2781 !(ha->tgt.tgt_ops->chk_dif_tags(t32))) {
2782 *pfw_prot_opts |= PO_DIS_REF_TAG_VALD;
2785 /* enable ALL bytes of the ref tag */
2786 ctx->ref_tag_mask[0] = 0xff;
2787 ctx->ref_tag_mask[1] = 0xff;
2788 ctx->ref_tag_mask[2] = 0xff;
2789 ctx->ref_tag_mask[3] = 0xff;
2791 case TARGET_DIF_TYPE2_PROT:
2793 * For TYPE 2 protection: 16 bit GUARD + 32 bit REF
2794 * tag has to match LBA in CDB + N
2796 ctx->ref_tag = cpu_to_le32(lba);
2797 if (!qla_tgt_ref_mask_check(se_cmd) ||
2798 !(ha->tgt.tgt_ops->chk_dif_tags(t32))) {
2799 *pfw_prot_opts |= PO_DIS_REF_TAG_VALD;
2802 /* enable ALL bytes of the ref tag */
2803 ctx->ref_tag_mask[0] = 0xff;
2804 ctx->ref_tag_mask[1] = 0xff;
2805 ctx->ref_tag_mask[2] = 0xff;
2806 ctx->ref_tag_mask[3] = 0xff;
2808 case TARGET_DIF_TYPE3_PROT:
2809 /* For TYPE 3 protection: 16 bit GUARD only */
2810 *pfw_prot_opts |= PO_DIS_REF_TAG_VALD;
2811 ctx->ref_tag_mask[0] = ctx->ref_tag_mask[1] =
2812 ctx->ref_tag_mask[2] = ctx->ref_tag_mask[3] = 0x00;
2818 qlt_build_ctio_crc2_pkt(struct qla_qpair *qpair, struct qla_tgt_prm *prm)
2821 uint32_t transfer_length = 0;
2822 uint32_t data_bytes;
2824 uint8_t bundling = 1;
2826 struct crc_context *crc_ctx_pkt = NULL;
2827 struct qla_hw_data *ha;
2828 struct ctio_crc2_to_fw *pkt;
2829 dma_addr_t crc_ctx_dma;
2830 uint16_t fw_prot_opts = 0;
2831 struct qla_tgt_cmd *cmd = prm->cmd;
2832 struct se_cmd *se_cmd = &cmd->se_cmd;
2834 struct atio_from_isp *atio = &prm->cmd->atio;
2835 struct qla_tc_param tc;
2837 scsi_qla_host_t *vha = cmd->vha;
2841 pkt = (struct ctio_crc2_to_fw *)qpair->req->ring_ptr;
2843 memset(pkt, 0, sizeof(*pkt));
2845 ql_dbg_qp(ql_dbg_tgt, cmd->qpair, 0xe071,
2846 "qla_target(%d):%s: se_cmd[%p] CRC2 prot_op[0x%x] cmd prot sg:cnt[%p:%x] lba[%llu]\n",
2847 cmd->vp_idx, __func__, se_cmd, se_cmd->prot_op,
2848 prm->prot_sg, prm->prot_seg_cnt, se_cmd->t_task_lba);
2850 if ((se_cmd->prot_op == TARGET_PROT_DIN_INSERT) ||
2851 (se_cmd->prot_op == TARGET_PROT_DOUT_STRIP))
2854 /* Compute dif len and adjust data len to incude protection */
2855 data_bytes = cmd->bufflen;
2856 dif_bytes = (data_bytes / cmd->blk_sz) * 8;
2858 switch (se_cmd->prot_op) {
2859 case TARGET_PROT_DIN_INSERT:
2860 case TARGET_PROT_DOUT_STRIP:
2861 transfer_length = data_bytes;
2862 if (cmd->prot_sg_cnt)
2863 data_bytes += dif_bytes;
2865 case TARGET_PROT_DIN_STRIP:
2866 case TARGET_PROT_DOUT_INSERT:
2867 case TARGET_PROT_DIN_PASS:
2868 case TARGET_PROT_DOUT_PASS:
2869 transfer_length = data_bytes + dif_bytes;
2876 if (!qlt_hba_err_chk_enabled(se_cmd))
2877 fw_prot_opts |= 0x10; /* Disable Guard tag checking */
2878 /* HBA error checking enabled */
2879 else if (IS_PI_UNINIT_CAPABLE(ha)) {
2880 if ((se_cmd->prot_type == TARGET_DIF_TYPE1_PROT) ||
2881 (se_cmd->prot_type == TARGET_DIF_TYPE2_PROT))
2882 fw_prot_opts |= PO_DIS_VALD_APP_ESC;
2883 else if (se_cmd->prot_type == TARGET_DIF_TYPE3_PROT)
2884 fw_prot_opts |= PO_DIS_VALD_APP_REF_ESC;
2887 switch (se_cmd->prot_op) {
2888 case TARGET_PROT_DIN_INSERT:
2889 case TARGET_PROT_DOUT_INSERT:
2890 fw_prot_opts |= PO_MODE_DIF_INSERT;
2892 case TARGET_PROT_DIN_STRIP:
2893 case TARGET_PROT_DOUT_STRIP:
2894 fw_prot_opts |= PO_MODE_DIF_REMOVE;
2896 case TARGET_PROT_DIN_PASS:
2897 case TARGET_PROT_DOUT_PASS:
2898 fw_prot_opts |= PO_MODE_DIF_PASS;
2899 /* FUTURE: does tcm require T10CRC<->IPCKSUM conversion? */
2901 default:/* Normal Request */
2902 fw_prot_opts |= PO_MODE_DIF_PASS;
2907 /* Update entry type to indicate Command Type CRC_2 IOCB */
2908 pkt->entry_type = CTIO_CRC2;
2909 pkt->entry_count = 1;
2910 pkt->vp_index = cmd->vp_idx;
2912 h = qlt_make_handle(qpair);
2913 if (unlikely(h == QLA_TGT_NULL_HANDLE)) {
2915 * CTIO type 7 from the firmware doesn't provide a way to
2916 * know the initiator's LOOP ID, hence we can't find
2917 * the session and, so, the command.
2921 qpair->req->outstanding_cmds[h] = (srb_t *)prm->cmd;
2923 pkt->handle = MAKE_HANDLE(qpair->req->id, h);
2924 pkt->handle |= CTIO_COMPLETION_HANDLE_MARK;
2925 pkt->nport_handle = cpu_to_le16(prm->cmd->loop_id);
2926 pkt->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
2927 pkt->initiator_id[0] = atio->u.isp24.fcp_hdr.s_id[2];
2928 pkt->initiator_id[1] = atio->u.isp24.fcp_hdr.s_id[1];
2929 pkt->initiator_id[2] = atio->u.isp24.fcp_hdr.s_id[0];
2930 pkt->exchange_addr = atio->u.isp24.exchange_addr;
2932 /* silence compile warning */
2933 t16 = be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id);
2934 pkt->ox_id = cpu_to_le16(t16);
2936 t16 = (atio->u.isp24.attr << 9);
2937 pkt->flags |= cpu_to_le16(t16);
2938 pkt->relative_offset = cpu_to_le32(prm->cmd->offset);
2940 /* Set transfer direction */
2941 if (cmd->dma_data_direction == DMA_TO_DEVICE)
2942 pkt->flags = cpu_to_le16(CTIO7_FLAGS_DATA_IN);
2943 else if (cmd->dma_data_direction == DMA_FROM_DEVICE)
2944 pkt->flags = cpu_to_le16(CTIO7_FLAGS_DATA_OUT);
2946 pkt->dseg_count = prm->tot_dsds;
2947 /* Fibre channel byte count */
2948 pkt->transfer_length = cpu_to_le32(transfer_length);
2950 /* ----- CRC context -------- */
2952 /* Allocate CRC context from global pool */
2953 crc_ctx_pkt = cmd->ctx =
2954 dma_pool_alloc(ha->dl_dma_pool, GFP_ATOMIC, &crc_ctx_dma);
2957 goto crc_queuing_error;
2959 /* Zero out CTX area. */
2960 clr_ptr = (uint8_t *)crc_ctx_pkt;
2961 memset(clr_ptr, 0, sizeof(*crc_ctx_pkt));
2963 crc_ctx_pkt->crc_ctx_dma = crc_ctx_dma;
2964 INIT_LIST_HEAD(&crc_ctx_pkt->dsd_list);
2967 crc_ctx_pkt->handle = pkt->handle;
2969 qla_tgt_set_dif_tags(cmd, crc_ctx_pkt, &fw_prot_opts);
2971 pkt->crc_context_address[0] = cpu_to_le32(LSD(crc_ctx_dma));
2972 pkt->crc_context_address[1] = cpu_to_le32(MSD(crc_ctx_dma));
2973 pkt->crc_context_len = CRC_CONTEXT_LEN_FW;
2976 cur_dsd = (uint32_t *) &crc_ctx_pkt->u.nobundling.data_address;
2979 * Configure Bundling if we need to fetch interlaving
2980 * protection PCI accesses
2982 fw_prot_opts |= PO_ENABLE_DIF_BUNDLING;
2983 crc_ctx_pkt->u.bundling.dif_byte_count = cpu_to_le32(dif_bytes);
2984 crc_ctx_pkt->u.bundling.dseg_count =
2985 cpu_to_le16(prm->tot_dsds - prm->prot_seg_cnt);
2986 cur_dsd = (uint32_t *) &crc_ctx_pkt->u.bundling.data_address;
2989 /* Finish the common fields of CRC pkt */
2990 crc_ctx_pkt->blk_size = cpu_to_le16(cmd->blk_sz);
2991 crc_ctx_pkt->prot_opts = cpu_to_le16(fw_prot_opts);
2992 crc_ctx_pkt->byte_count = cpu_to_le32(data_bytes);
2993 crc_ctx_pkt->guard_seed = cpu_to_le16(0);
2995 memset((uint8_t *)&tc, 0 , sizeof(tc));
2997 tc.blk_sz = cmd->blk_sz;
2998 tc.bufflen = cmd->bufflen;
3000 tc.prot_sg = cmd->prot_sg;
3001 tc.ctx = crc_ctx_pkt;
3002 tc.ctx_dsd_alloced = &cmd->ctx_dsd_alloced;
3004 /* Walks data segments */
3005 pkt->flags |= cpu_to_le16(CTIO7_FLAGS_DSD_PTR);
3007 if (!bundling && prm->prot_seg_cnt) {
3008 if (qla24xx_walk_and_build_sglist_no_difb(ha, NULL, cur_dsd,
3009 prm->tot_dsds, &tc))
3010 goto crc_queuing_error;
3011 } else if (qla24xx_walk_and_build_sglist(ha, NULL, cur_dsd,
3012 (prm->tot_dsds - prm->prot_seg_cnt), &tc))
3013 goto crc_queuing_error;
3015 if (bundling && prm->prot_seg_cnt) {
3016 /* Walks dif segments */
3017 pkt->add_flags |= CTIO_CRC2_AF_DIF_DSD_ENA;
3019 cur_dsd = (uint32_t *) &crc_ctx_pkt->u.bundling.dif_address;
3020 if (qla24xx_walk_and_build_prot_sglist(ha, NULL, cur_dsd,
3021 prm->prot_seg_cnt, &tc))
3022 goto crc_queuing_error;
3027 /* Cleanup will be performed by the caller */
3028 qpair->req->outstanding_cmds[h] = NULL;
3030 return QLA_FUNCTION_FAILED;
3034 * Callback to setup response of xmit_type of QLA_TGT_XMIT_DATA and *
3035 * QLA_TGT_XMIT_STATUS for >= 24xx silicon
3037 int qlt_xmit_response(struct qla_tgt_cmd *cmd, int xmit_type,
3038 uint8_t scsi_status)
3040 struct scsi_qla_host *vha = cmd->vha;
3041 struct qla_qpair *qpair = cmd->qpair;
3042 struct ctio7_to_24xx *pkt;
3043 struct qla_tgt_prm prm;
3044 uint32_t full_req_cnt = 0;
3045 unsigned long flags = 0;
3048 if (cmd->sess && cmd->sess->deleted) {
3049 cmd->state = QLA_TGT_STATE_PROCESSED;
3050 if (cmd->sess->logout_completed)
3051 /* no need to terminate. FW already freed exchange. */
3052 qlt_abort_cmd_on_host_reset(cmd->vha, cmd);
3054 qlt_send_term_exchange(qpair, cmd, &cmd->atio, 0, 0);
3058 ql_dbg_qp(ql_dbg_tgt, qpair, 0xe018,
3059 "is_send_status=%d, cmd->bufflen=%d, cmd->sg_cnt=%d, cmd->dma_data_direction=%d se_cmd[%p] qp %d\n",
3060 (xmit_type & QLA_TGT_XMIT_STATUS) ?
3061 1 : 0, cmd->bufflen, cmd->sg_cnt, cmd->dma_data_direction,
3062 &cmd->se_cmd, qpair->id);
3064 res = qlt_pre_xmit_response(cmd, &prm, xmit_type, scsi_status,
3066 if (unlikely(res != 0)) {
3070 spin_lock_irqsave(qpair->qp_lock_ptr, flags);
3072 if (xmit_type == QLA_TGT_XMIT_STATUS)
3073 qpair->tgt_counters.core_qla_snd_status++;
3075 qpair->tgt_counters.core_qla_que_buf++;
3077 if (!qpair->fw_started || cmd->reset_count != qpair->chip_reset) {
3079 * Either the port is not online or this request was from
3080 * previous life, just abort the processing.
3082 cmd->state = QLA_TGT_STATE_PROCESSED;
3083 qlt_abort_cmd_on_host_reset(cmd->vha, cmd);
3084 ql_dbg_qp(ql_dbg_async, qpair, 0xe101,
3085 "RESET-RSP online/active/old-count/new-count = %d/%d/%d/%d.\n",
3086 vha->flags.online, qla2x00_reset_active(vha),
3087 cmd->reset_count, qpair->chip_reset);
3088 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
3092 /* Does F/W have an IOCBs for this request */
3093 res = qlt_check_reserve_free_req(qpair, full_req_cnt);
3095 goto out_unmap_unlock;
3097 if (cmd->se_cmd.prot_op && (xmit_type & QLA_TGT_XMIT_DATA))
3098 res = qlt_build_ctio_crc2_pkt(qpair, &prm);
3100 res = qlt_24xx_build_ctio_pkt(qpair, &prm);
3101 if (unlikely(res != 0)) {
3102 qpair->req->cnt += full_req_cnt;
3103 goto out_unmap_unlock;
3106 pkt = (struct ctio7_to_24xx *)prm.pkt;
3108 if (qlt_has_data(cmd) && (xmit_type & QLA_TGT_XMIT_DATA)) {
3109 pkt->u.status0.flags |=
3110 cpu_to_le16(CTIO7_FLAGS_DATA_IN |
3111 CTIO7_FLAGS_STATUS_MODE_0);
3113 if (cmd->se_cmd.prot_op == TARGET_PROT_NORMAL)
3114 qlt_load_data_segments(&prm);
3116 if (prm.add_status_pkt == 0) {
3117 if (xmit_type & QLA_TGT_XMIT_STATUS) {
3118 pkt->u.status0.scsi_status =
3119 cpu_to_le16(prm.rq_result);
3120 pkt->u.status0.residual =
3121 cpu_to_le32(prm.residual);
3122 pkt->u.status0.flags |= cpu_to_le16(
3123 CTIO7_FLAGS_SEND_STATUS);
3124 if (qlt_need_explicit_conf(cmd, 0)) {
3125 pkt->u.status0.flags |=
3127 CTIO7_FLAGS_EXPLICIT_CONFORM |
3128 CTIO7_FLAGS_CONFORM_REQ);
3134 * We have already made sure that there is sufficient
3135 * amount of request entries to not drop HW lock in
3138 struct ctio7_to_24xx *ctio =
3139 (struct ctio7_to_24xx *)qlt_get_req_pkt(
3142 ql_dbg_qp(ql_dbg_tgt, qpair, 0x305e,
3143 "Building additional status packet 0x%p.\n",
3147 * T10Dif: ctio_crc2_to_fw overlay ontop of
3150 memcpy(ctio, pkt, sizeof(*ctio));
3151 /* reset back to CTIO7 */
3152 ctio->entry_count = 1;
3153 ctio->entry_type = CTIO_TYPE7;
3154 ctio->dseg_count = 0;
3155 ctio->u.status1.flags &= ~cpu_to_le16(
3156 CTIO7_FLAGS_DATA_IN);
3158 /* Real finish is ctio_m1's finish */
3159 pkt->handle |= CTIO_INTERMEDIATE_HANDLE_MARK;
3160 pkt->u.status0.flags |= cpu_to_le16(
3161 CTIO7_FLAGS_DONT_RET_CTIO);
3163 /* qlt_24xx_init_ctio_to_isp will correct
3164 * all neccessary fields that's part of CTIO7.
3165 * There should be no residual of CTIO-CRC2 data.
3167 qlt_24xx_init_ctio_to_isp((struct ctio7_to_24xx *)ctio,
3171 qlt_24xx_init_ctio_to_isp(pkt, &prm);
3174 cmd->state = QLA_TGT_STATE_PROCESSED; /* Mid-level is done processing */
3175 cmd->cmd_sent_to_fw = 1;
3177 /* Memory Barrier */
3179 if (qpair->reqq_start_iocbs)
3180 qpair->reqq_start_iocbs(qpair);
3182 qla2x00_start_iocbs(vha, qpair->req);
3183 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
3188 qlt_unmap_sg(vha, cmd);
3189 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
3193 EXPORT_SYMBOL(qlt_xmit_response);
3195 int qlt_rdy_to_xfer(struct qla_tgt_cmd *cmd)
3197 struct ctio7_to_24xx *pkt;
3198 struct scsi_qla_host *vha = cmd->vha;
3199 struct qla_tgt *tgt = cmd->tgt;
3200 struct qla_tgt_prm prm;
3201 unsigned long flags = 0;
3203 struct qla_qpair *qpair = cmd->qpair;
3205 memset(&prm, 0, sizeof(prm));
3211 /* Calculate number of entries and segments required */
3212 if (qlt_pci_map_calc_cnt(&prm) != 0)
3215 if (!qpair->fw_started || (cmd->reset_count != qpair->chip_reset) ||
3216 (cmd->sess && cmd->sess->deleted)) {
3218 * Either the port is not online or this request was from
3219 * previous life, just abort the processing.
3221 cmd->state = QLA_TGT_STATE_NEED_DATA;
3222 qlt_abort_cmd_on_host_reset(cmd->vha, cmd);
3223 ql_dbg_qp(ql_dbg_async, qpair, 0xe102,
3224 "RESET-XFR online/active/old-count/new-count = %d/%d/%d/%d.\n",
3225 vha->flags.online, qla2x00_reset_active(vha),
3226 cmd->reset_count, qpair->chip_reset);
3230 spin_lock_irqsave(qpair->qp_lock_ptr, flags);
3231 /* Does F/W have an IOCBs for this request */
3232 res = qlt_check_reserve_free_req(qpair, prm.req_cnt);
3234 goto out_unlock_free_unmap;
3235 if (cmd->se_cmd.prot_op)
3236 res = qlt_build_ctio_crc2_pkt(qpair, &prm);
3238 res = qlt_24xx_build_ctio_pkt(qpair, &prm);
3240 if (unlikely(res != 0)) {
3241 qpair->req->cnt += prm.req_cnt;
3242 goto out_unlock_free_unmap;
3245 pkt = (struct ctio7_to_24xx *)prm.pkt;
3246 pkt->u.status0.flags |= cpu_to_le16(CTIO7_FLAGS_DATA_OUT |
3247 CTIO7_FLAGS_STATUS_MODE_0);
3249 if (cmd->se_cmd.prot_op == TARGET_PROT_NORMAL)
3250 qlt_load_data_segments(&prm);
3252 cmd->state = QLA_TGT_STATE_NEED_DATA;
3253 cmd->cmd_sent_to_fw = 1;
3255 /* Memory Barrier */
3257 if (qpair->reqq_start_iocbs)
3258 qpair->reqq_start_iocbs(qpair);
3260 qla2x00_start_iocbs(vha, qpair->req);
3261 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
3265 out_unlock_free_unmap:
3266 qlt_unmap_sg(vha, cmd);
3267 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
3271 EXPORT_SYMBOL(qlt_rdy_to_xfer);
3275 * it is assumed either hardware_lock or qpair lock is held.
3278 qlt_handle_dif_error(struct qla_qpair *qpair, struct qla_tgt_cmd *cmd,
3279 struct ctio_crc_from_fw *sts)
3281 uint8_t *ap = &sts->actual_dif[0];
3282 uint8_t *ep = &sts->expected_dif[0];
3283 uint64_t lba = cmd->se_cmd.t_task_lba;
3284 uint8_t scsi_status, sense_key, asc, ascq;
3285 unsigned long flags;
3286 struct scsi_qla_host *vha = cmd->vha;
3288 cmd->trc_flags |= TRC_DIF_ERR;
3290 cmd->a_guard = be16_to_cpu(*(uint16_t *)(ap + 0));
3291 cmd->a_app_tag = be16_to_cpu(*(uint16_t *)(ap + 2));
3292 cmd->a_ref_tag = be32_to_cpu(*(uint32_t *)(ap + 4));
3294 cmd->e_guard = be16_to_cpu(*(uint16_t *)(ep + 0));
3295 cmd->e_app_tag = be16_to_cpu(*(uint16_t *)(ep + 2));
3296 cmd->e_ref_tag = be32_to_cpu(*(uint32_t *)(ep + 4));
3298 ql_dbg(ql_dbg_tgt_dif, vha, 0xf075,
3299 "%s: aborted %d state %d\n", __func__, cmd->aborted, cmd->state);
3301 scsi_status = sense_key = asc = ascq = 0;
3303 /* check appl tag */
3304 if (cmd->e_app_tag != cmd->a_app_tag) {
3305 ql_dbg(ql_dbg_tgt_dif, vha, 0xe00d,
3306 "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]",
3307 cmd->cdb[0], lba, (lba+cmd->num_blks), cmd->num_blks,
3308 cmd->a_ref_tag, cmd->e_ref_tag, cmd->a_app_tag,
3309 cmd->e_app_tag, cmd->a_guard, cmd->e_guard, cmd,
3310 cmd->atio.u.isp24.fcp_hdr.ox_id);
3312 cmd->dif_err_code = DIF_ERR_APP;
3313 scsi_status = SAM_STAT_CHECK_CONDITION;
3314 sense_key = ABORTED_COMMAND;
3320 if (cmd->e_ref_tag != cmd->a_ref_tag) {
3321 ql_dbg(ql_dbg_tgt_dif, vha, 0xe00e,
3322 "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] ",
3323 cmd->cdb[0], lba, (lba+cmd->num_blks), cmd->num_blks,
3324 cmd->a_ref_tag, cmd->e_ref_tag, cmd->a_app_tag,
3325 cmd->e_app_tag, cmd->a_guard, cmd->e_guard, cmd,
3326 cmd->atio.u.isp24.fcp_hdr.ox_id);
3328 cmd->dif_err_code = DIF_ERR_REF;
3329 scsi_status = SAM_STAT_CHECK_CONDITION;
3330 sense_key = ABORTED_COMMAND;
3337 if (cmd->e_guard != cmd->a_guard) {
3338 ql_dbg(ql_dbg_tgt_dif, vha, 0xe012,
3339 "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]",
3340 cmd->cdb[0], lba, (lba+cmd->num_blks), cmd->num_blks,
3341 cmd->a_ref_tag, cmd->e_ref_tag, cmd->a_app_tag,
3342 cmd->e_app_tag, cmd->a_guard, cmd->e_guard, cmd,
3343 cmd->atio.u.isp24.fcp_hdr.ox_id);
3345 cmd->dif_err_code = DIF_ERR_GRD;
3346 scsi_status = SAM_STAT_CHECK_CONDITION;
3347 sense_key = ABORTED_COMMAND;
3352 switch (cmd->state) {
3353 case QLA_TGT_STATE_NEED_DATA:
3354 /* handle_data will load DIF error code */
3355 cmd->state = QLA_TGT_STATE_DATA_IN;
3356 vha->hw->tgt.tgt_ops->handle_data(cmd);
3359 spin_lock_irqsave(&cmd->cmd_lock, flags);
3361 spin_unlock_irqrestore(&cmd->cmd_lock, flags);
3362 vha->hw->tgt.tgt_ops->free_cmd(cmd);
3365 spin_unlock_irqrestore(&cmd->cmd_lock, flags);
3367 qlt_send_resp_ctio(qpair, cmd, scsi_status, sense_key, asc,
3369 /* assume scsi status gets out on the wire.
3370 * Will not wait for completion.
3372 vha->hw->tgt.tgt_ops->free_cmd(cmd);
3377 /* If hardware_lock held on entry, might drop it, then reaquire */
3378 /* This function sends the appropriate CTIO to ISP 2xxx or 24xx */
3379 static int __qlt_send_term_imm_notif(struct scsi_qla_host *vha,
3380 struct imm_ntfy_from_isp *ntfy)
3382 struct nack_to_isp *nack;
3383 struct qla_hw_data *ha = vha->hw;
3387 ql_dbg(ql_dbg_tgt_tmr, vha, 0xe01c,
3388 "Sending TERM ELS CTIO (ha=%p)\n", ha);
3390 pkt = (request_t *)qla2x00_alloc_iocbs(vha, NULL);
3392 ql_dbg(ql_dbg_tgt, vha, 0xe080,
3393 "qla_target(%d): %s failed: unable to allocate "
3394 "request packet\n", vha->vp_idx, __func__);
3398 pkt->entry_type = NOTIFY_ACK_TYPE;
3399 pkt->entry_count = 1;
3400 pkt->handle = QLA_TGT_SKIP_HANDLE;
3402 nack = (struct nack_to_isp *)pkt;
3403 nack->ox_id = ntfy->ox_id;
3405 nack->u.isp24.nport_handle = ntfy->u.isp24.nport_handle;
3406 if (le16_to_cpu(ntfy->u.isp24.status) == IMM_NTFY_ELS) {
3407 nack->u.isp24.flags = ntfy->u.isp24.flags &
3408 __constant_cpu_to_le32(NOTIFY24XX_FLAGS_PUREX_IOCB);
3412 nack->u.isp24.flags |=
3413 __constant_cpu_to_le16(NOTIFY_ACK_FLAGS_TERMINATE);
3415 nack->u.isp24.srr_rx_id = ntfy->u.isp24.srr_rx_id;
3416 nack->u.isp24.status = ntfy->u.isp24.status;
3417 nack->u.isp24.status_subcode = ntfy->u.isp24.status_subcode;
3418 nack->u.isp24.fw_handle = ntfy->u.isp24.fw_handle;
3419 nack->u.isp24.exchange_address = ntfy->u.isp24.exchange_address;
3420 nack->u.isp24.srr_rel_offs = ntfy->u.isp24.srr_rel_offs;
3421 nack->u.isp24.srr_ui = ntfy->u.isp24.srr_ui;
3422 nack->u.isp24.vp_index = ntfy->u.isp24.vp_index;
3424 qla2x00_start_iocbs(vha, vha->req);
3428 static void qlt_send_term_imm_notif(struct scsi_qla_host *vha,
3429 struct imm_ntfy_from_isp *imm, int ha_locked)
3431 unsigned long flags = 0;
3435 rc = __qlt_send_term_imm_notif(vha, imm);
3439 qlt_alloc_qfull_cmd(vha, imm, 0, 0);
3447 spin_lock_irqsave(&vha->hw->hardware_lock, flags);
3448 rc = __qlt_send_term_imm_notif(vha, imm);
3452 qlt_alloc_qfull_cmd(vha, imm, 0, 0);
3457 spin_unlock_irqrestore(&vha->hw->hardware_lock, flags);
3461 * If hardware_lock held on entry, might drop it, then reaquire
3462 * This function sends the appropriate CTIO to ISP 2xxx or 24xx
3464 static int __qlt_send_term_exchange(struct qla_qpair *qpair,
3465 struct qla_tgt_cmd *cmd,
3466 struct atio_from_isp *atio)
3468 struct scsi_qla_host *vha = qpair->vha;
3469 struct ctio7_to_24xx *ctio24;
3470 struct qla_hw_data *ha = vha->hw;
3475 ql_dbg(ql_dbg_tgt, vha, 0xe009, "Sending TERM EXCH CTIO (ha=%p)\n", ha);
3480 pkt = (request_t *)qla2x00_alloc_iocbs_ready(qpair, NULL);
3482 ql_dbg(ql_dbg_tgt, vha, 0xe050,
3483 "qla_target(%d): %s failed: unable to allocate "
3484 "request packet\n", vha->vp_idx, __func__);
3489 if (cmd->state < QLA_TGT_STATE_PROCESSED) {
3490 ql_dbg(ql_dbg_tgt, vha, 0xe051,
3491 "qla_target(%d): Terminating cmd %p with "
3492 "incorrect state %d\n", vha->vp_idx, cmd,
3498 qpair->tgt_counters.num_term_xchg_sent++;
3499 pkt->entry_count = 1;
3500 pkt->handle = QLA_TGT_SKIP_HANDLE | CTIO_COMPLETION_HANDLE_MARK;
3502 ctio24 = (struct ctio7_to_24xx *)pkt;
3503 ctio24->entry_type = CTIO_TYPE7;
3504 ctio24->nport_handle = CTIO7_NHANDLE_UNRECOGNIZED;
3505 ctio24->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
3506 ctio24->vp_index = vha->vp_idx;
3507 ctio24->initiator_id[0] = atio->u.isp24.fcp_hdr.s_id[2];
3508 ctio24->initiator_id[1] = atio->u.isp24.fcp_hdr.s_id[1];
3509 ctio24->initiator_id[2] = atio->u.isp24.fcp_hdr.s_id[0];
3510 ctio24->exchange_addr = atio->u.isp24.exchange_addr;
3511 temp = (atio->u.isp24.attr << 9) | CTIO7_FLAGS_STATUS_MODE_1 |
3512 CTIO7_FLAGS_TERMINATE;
3513 ctio24->u.status1.flags = cpu_to_le16(temp);
3514 temp = be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id);
3515 ctio24->u.status1.ox_id = cpu_to_le16(temp);
3517 /* Most likely, it isn't needed */
3518 ctio24->u.status1.residual = get_unaligned((uint32_t *)
3519 &atio->u.isp24.fcp_cmnd.add_cdb[
3520 atio->u.isp24.fcp_cmnd.add_cdb_len]);
3521 if (ctio24->u.status1.residual != 0)
3522 ctio24->u.status1.scsi_status |= SS_RESIDUAL_UNDER;
3524 /* Memory Barrier */
3526 if (qpair->reqq_start_iocbs)
3527 qpair->reqq_start_iocbs(qpair);
3529 qla2x00_start_iocbs(vha, qpair->req);
3533 static void qlt_send_term_exchange(struct qla_qpair *qpair,
3534 struct qla_tgt_cmd *cmd, struct atio_from_isp *atio, int ha_locked,
3537 struct scsi_qla_host *vha;
3538 unsigned long flags = 0;
3541 /* why use different vha? NPIV */
3548 rc = __qlt_send_term_exchange(qpair, cmd, atio);
3550 qlt_alloc_qfull_cmd(vha, atio, 0, 0);
3553 spin_lock_irqsave(qpair->qp_lock_ptr, flags);
3554 rc = __qlt_send_term_exchange(qpair, cmd, atio);
3556 qlt_alloc_qfull_cmd(vha, atio, 0, 0);
3559 if (cmd && !ul_abort && !cmd->aborted) {
3561 qlt_unmap_sg(vha, cmd);
3562 vha->hw->tgt.tgt_ops->free_cmd(cmd);
3566 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
3571 static void qlt_init_term_exchange(struct scsi_qla_host *vha)
3573 struct list_head free_list;
3574 struct qla_tgt_cmd *cmd, *tcmd;
3576 vha->hw->tgt.leak_exchg_thresh_hold =
3577 (vha->hw->cur_fw_xcb_count/100) * LEAK_EXCHG_THRESH_HOLD_PERCENT;
3580 if (!list_empty(&vha->hw->tgt.q_full_list)) {
3581 INIT_LIST_HEAD(&free_list);
3582 list_splice_init(&vha->hw->tgt.q_full_list, &free_list);
3584 list_for_each_entry_safe(cmd, tcmd, &free_list, cmd_list) {
3585 list_del(&cmd->cmd_list);
3586 /* This cmd was never sent to TCM. There is no need
3587 * to schedule free or call free_cmd
3590 vha->hw->tgt.num_qfull_cmds_alloc--;
3593 vha->hw->tgt.num_qfull_cmds_dropped = 0;
3596 static void qlt_chk_exch_leak_thresh_hold(struct scsi_qla_host *vha)
3598 uint32_t total_leaked;
3600 total_leaked = vha->hw->tgt.num_qfull_cmds_dropped;
3602 if (vha->hw->tgt.leak_exchg_thresh_hold &&
3603 (total_leaked > vha->hw->tgt.leak_exchg_thresh_hold)) {
3605 ql_dbg(ql_dbg_tgt, vha, 0xe079,
3606 "Chip reset due to exchange starvation: %d/%d.\n",
3607 total_leaked, vha->hw->cur_fw_xcb_count);
3609 if (IS_P3P_TYPE(vha->hw))
3610 set_bit(FCOE_CTX_RESET_NEEDED, &vha->dpc_flags);
3612 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
3613 qla2xxx_wake_dpc(vha);
3618 int qlt_abort_cmd(struct qla_tgt_cmd *cmd)
3620 struct qla_tgt *tgt = cmd->tgt;
3621 struct scsi_qla_host *vha = tgt->vha;
3622 struct se_cmd *se_cmd = &cmd->se_cmd;
3623 unsigned long flags;
3625 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf014,
3626 "qla_target(%d): terminating exchange for aborted cmd=%p "
3627 "(se_cmd=%p, tag=%llu)", vha->vp_idx, cmd, &cmd->se_cmd,
3630 spin_lock_irqsave(&cmd->cmd_lock, flags);
3632 spin_unlock_irqrestore(&cmd->cmd_lock, flags);
3634 * It's normal to see 2 calls in this path:
3635 * 1) XFER Rdy completion + CMD_T_ABORT
3636 * 2) TCM TMR - drain_state_list
3638 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf016,
3639 "multiple abort. %p transport_state %x, t_state %x, "
3640 "se_cmd_flags %x\n", cmd, cmd->se_cmd.transport_state,
3641 cmd->se_cmd.t_state, cmd->se_cmd.se_cmd_flags);
3645 cmd->trc_flags |= TRC_ABORT;
3646 spin_unlock_irqrestore(&cmd->cmd_lock, flags);
3648 qlt_send_term_exchange(cmd->qpair, cmd, &cmd->atio, 0, 1);
3651 EXPORT_SYMBOL(qlt_abort_cmd);
3653 void qlt_free_cmd(struct qla_tgt_cmd *cmd)
3655 struct fc_port *sess = cmd->sess;
3657 ql_dbg(ql_dbg_tgt, cmd->vha, 0xe074,
3658 "%s: se_cmd[%p] ox_id %04x\n",
3659 __func__, &cmd->se_cmd,
3660 be16_to_cpu(cmd->atio.u.isp24.fcp_hdr.ox_id));
3662 BUG_ON(cmd->cmd_in_wq);
3665 qlt_unmap_sg(cmd->vha, cmd);
3668 qlt_decr_num_pend_cmds(cmd->vha);
3670 BUG_ON(cmd->sg_mapped);
3671 cmd->jiffies_at_free = get_jiffies_64();
3672 if (unlikely(cmd->free_sg))
3675 if (!sess || !sess->se_sess) {
3679 cmd->jiffies_at_free = get_jiffies_64();
3680 percpu_ida_free(&sess->se_sess->sess_tag_pool, cmd->se_cmd.map_tag);
3682 EXPORT_SYMBOL(qlt_free_cmd);
3685 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
3687 static int qlt_term_ctio_exchange(struct qla_qpair *qpair, void *ctio,
3688 struct qla_tgt_cmd *cmd, uint32_t status)
3691 struct scsi_qla_host *vha = qpair->vha;
3693 if (cmd->se_cmd.prot_op)
3694 ql_dbg(ql_dbg_tgt_dif, vha, 0xe013,
3695 "Term DIF cmd: lba[0x%llx|%lld] len[0x%x] "
3696 "se_cmd=%p tag[%x] op %#x/%s",
3698 cmd->num_blks, &cmd->se_cmd,
3699 cmd->atio.u.isp24.exchange_addr,
3700 cmd->se_cmd.prot_op,
3701 prot_op_str(cmd->se_cmd.prot_op));
3704 struct ctio7_from_24xx *c = (struct ctio7_from_24xx *)ctio;
3706 cpu_to_le16(OF_TERM_EXCH));
3711 qlt_term_ctio_exchange(qpair, ctio, cmd, status);
3717 /* ha->hardware_lock supposed to be held on entry */
3718 static struct qla_tgt_cmd *qlt_ctio_to_cmd(struct scsi_qla_host *vha,
3719 struct rsp_que *rsp, uint32_t handle, void *ctio)
3721 struct qla_tgt_cmd *cmd = NULL;
3722 struct req_que *req;
3723 int qid = GET_QID(handle);
3724 uint32_t h = handle & ~QLA_TGT_HANDLE_MASK;
3726 if (unlikely(h == QLA_TGT_SKIP_HANDLE))
3729 if (qid == rsp->req->id) {
3731 } else if (vha->hw->req_q_map[qid]) {
3732 ql_dbg(ql_dbg_tgt_mgt, vha, 0x1000a,
3733 "qla_target(%d): CTIO completion with different QID %d handle %x\n",
3734 vha->vp_idx, rsp->id, handle);
3735 req = vha->hw->req_q_map[qid];
3740 h &= QLA_CMD_HANDLE_MASK;
3742 if (h != QLA_TGT_NULL_HANDLE) {
3743 if (unlikely(h >= req->num_outstanding_cmds)) {
3744 ql_dbg(ql_dbg_tgt, vha, 0xe052,
3745 "qla_target(%d): Wrong handle %x received\n",
3746 vha->vp_idx, handle);
3750 cmd = (struct qla_tgt_cmd *)req->outstanding_cmds[h];
3751 if (unlikely(cmd == NULL)) {
3752 ql_dbg(ql_dbg_async, vha, 0xe053,
3753 "qla_target(%d): Suspicious: unable to find the command with handle %x req->id %d rsp->id %d\n",
3754 vha->vp_idx, handle, req->id, rsp->id);
3757 req->outstanding_cmds[h] = NULL;
3758 } else if (ctio != NULL) {
3759 /* We can't get loop ID from CTIO7 */
3760 ql_dbg(ql_dbg_tgt, vha, 0xe054,
3761 "qla_target(%d): Wrong CTIO received: QLA24xx doesn't "
3762 "support NULL handles\n", vha->vp_idx);
3769 /* hardware_lock should be held by caller. */
3771 qlt_abort_cmd_on_host_reset(struct scsi_qla_host *vha, struct qla_tgt_cmd *cmd)
3773 struct qla_hw_data *ha = vha->hw;
3776 qlt_unmap_sg(vha, cmd);
3778 /* TODO: fix debug message type and ids. */
3779 if (cmd->state == QLA_TGT_STATE_PROCESSED) {
3780 ql_dbg(ql_dbg_io, vha, 0xff00,
3781 "HOST-ABORT: state=PROCESSED.\n");
3782 } else if (cmd->state == QLA_TGT_STATE_NEED_DATA) {
3783 cmd->write_data_transferred = 0;
3784 cmd->state = QLA_TGT_STATE_DATA_IN;
3786 ql_dbg(ql_dbg_io, vha, 0xff01,
3787 "HOST-ABORT: state=DATA_IN.\n");
3789 ha->tgt.tgt_ops->handle_data(cmd);
3792 ql_dbg(ql_dbg_io, vha, 0xff03,
3793 "HOST-ABORT: state=BAD(%d).\n",
3798 cmd->trc_flags |= TRC_FLUSH;
3799 ha->tgt.tgt_ops->free_cmd(cmd);
3803 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
3805 static void qlt_do_ctio_completion(struct scsi_qla_host *vha,
3806 struct rsp_que *rsp, uint32_t handle, uint32_t status, void *ctio)
3808 struct qla_hw_data *ha = vha->hw;
3809 struct se_cmd *se_cmd;
3810 struct qla_tgt_cmd *cmd;
3811 struct qla_qpair *qpair = rsp->qpair;
3813 if (handle & CTIO_INTERMEDIATE_HANDLE_MARK) {
3814 /* That could happen only in case of an error/reset/abort */
3815 if (status != CTIO_SUCCESS) {
3816 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01d,
3817 "Intermediate CTIO received"
3818 " (status %x)\n", status);
3823 cmd = qlt_ctio_to_cmd(vha, rsp, handle, ctio);
3827 se_cmd = &cmd->se_cmd;
3828 cmd->cmd_sent_to_fw = 0;
3830 qlt_unmap_sg(vha, cmd);
3832 if (unlikely(status != CTIO_SUCCESS)) {
3833 switch (status & 0xFFFF) {
3834 case CTIO_LIP_RESET:
3835 case CTIO_TARGET_RESET:
3837 /* driver request abort via Terminate exchange */
3839 case CTIO_INVALID_RX_ID:
3841 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf058,
3842 "qla_target(%d): CTIO with "
3843 "status %#x received, state %x, se_cmd %p, "
3844 "(LIP_RESET=e, ABORTED=2, TARGET_RESET=17, "
3845 "TIMEOUT=b, INVALID_RX_ID=8)\n", vha->vp_idx,
3846 status, cmd->state, se_cmd);
3849 case CTIO_PORT_LOGGED_OUT:
3850 case CTIO_PORT_UNAVAILABLE:
3853 (status & 0xFFFF) == CTIO_PORT_LOGGED_OUT;
3855 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf059,
3856 "qla_target(%d): CTIO with %s status %x "
3857 "received (state %x, se_cmd %p)\n", vha->vp_idx,
3858 logged_out ? "PORT LOGGED OUT" : "PORT UNAVAILABLE",
3859 status, cmd->state, se_cmd);
3861 if (logged_out && cmd->sess) {
3863 * Session is already logged out, but we need
3864 * to notify initiator, who's not aware of this
3866 cmd->sess->logout_on_delete = 0;
3867 cmd->sess->send_els_logo = 1;
3868 ql_dbg(ql_dbg_disc, vha, 0x20f8,
3869 "%s %d %8phC post del sess\n",
3870 __func__, __LINE__, cmd->sess->port_name);
3872 qlt_schedule_sess_for_deletion_lock(cmd->sess);
3876 case CTIO_DIF_ERROR: {
3877 struct ctio_crc_from_fw *crc =
3878 (struct ctio_crc_from_fw *)ctio;
3879 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf073,
3880 "qla_target(%d): CTIO with DIF_ERROR status %x "
3881 "received (state %x, ulp_cmd %p) actual_dif[0x%llx] "
3882 "expect_dif[0x%llx]\n",
3883 vha->vp_idx, status, cmd->state, se_cmd,
3884 *((u64 *)&crc->actual_dif[0]),
3885 *((u64 *)&crc->expected_dif[0]));
3887 qlt_handle_dif_error(qpair, cmd, ctio);
3891 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05b,
3892 "qla_target(%d): CTIO with error status 0x%x received (state %x, se_cmd %p\n",
3893 vha->vp_idx, status, cmd->state, se_cmd);
3898 /* "cmd->aborted" means
3899 * cmd is already aborted/terminated, we don't
3900 * need to terminate again. The exchange is already
3901 * cleaned up/freed at FW level. Just cleanup at driver
3904 if ((cmd->state != QLA_TGT_STATE_NEED_DATA) &&
3906 cmd->trc_flags |= TRC_CTIO_ERR;
3907 if (qlt_term_ctio_exchange(qpair, ctio, cmd, status))
3912 if (cmd->state == QLA_TGT_STATE_PROCESSED) {
3913 cmd->trc_flags |= TRC_CTIO_DONE;
3914 } else if (cmd->state == QLA_TGT_STATE_NEED_DATA) {
3915 cmd->state = QLA_TGT_STATE_DATA_IN;
3917 if (status == CTIO_SUCCESS)
3918 cmd->write_data_transferred = 1;
3920 ha->tgt.tgt_ops->handle_data(cmd);
3922 } else if (cmd->aborted) {
3923 cmd->trc_flags |= TRC_CTIO_ABORTED;
3924 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01e,
3925 "Aborted command %p (tag %lld) finished\n", cmd, se_cmd->tag);
3927 cmd->trc_flags |= TRC_CTIO_STRANGE;
3928 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05c,
3929 "qla_target(%d): A command in state (%d) should "
3930 "not return a CTIO complete\n", vha->vp_idx, cmd->state);
3933 if (unlikely(status != CTIO_SUCCESS) &&
3935 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01f, "Finishing failed CTIO\n");
3939 ha->tgt.tgt_ops->free_cmd(cmd);
3942 static inline int qlt_get_fcp_task_attr(struct scsi_qla_host *vha,
3947 switch (task_codes) {
3948 case ATIO_SIMPLE_QUEUE:
3949 fcp_task_attr = TCM_SIMPLE_TAG;
3951 case ATIO_HEAD_OF_QUEUE:
3952 fcp_task_attr = TCM_HEAD_TAG;
3954 case ATIO_ORDERED_QUEUE:
3955 fcp_task_attr = TCM_ORDERED_TAG;
3957 case ATIO_ACA_QUEUE:
3958 fcp_task_attr = TCM_ACA_TAG;
3961 fcp_task_attr = TCM_SIMPLE_TAG;
3964 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05d,
3965 "qla_target: unknown task code %x, use ORDERED instead\n",
3967 fcp_task_attr = TCM_ORDERED_TAG;
3971 return fcp_task_attr;
3974 static struct fc_port *qlt_make_local_sess(struct scsi_qla_host *,
3977 * Process context for I/O path into tcm_qla2xxx code
3979 static void __qlt_do_work(struct qla_tgt_cmd *cmd)
3981 scsi_qla_host_t *vha = cmd->vha;
3982 struct qla_hw_data *ha = vha->hw;
3983 struct fc_port *sess = cmd->sess;
3984 struct atio_from_isp *atio = &cmd->atio;
3986 unsigned long flags;
3987 uint32_t data_length;
3988 int ret, fcp_task_attr, data_dir, bidi = 0;
3989 struct qla_qpair *qpair = cmd->qpair;
3992 cmd->trc_flags |= TRC_DO_WORK;
3995 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf082,
3996 "cmd with tag %u is aborted\n",
3997 cmd->atio.u.isp24.exchange_addr);
4001 spin_lock_init(&cmd->cmd_lock);
4002 cdb = &atio->u.isp24.fcp_cmnd.cdb[0];
4003 cmd->se_cmd.tag = atio->u.isp24.exchange_addr;
4005 if (atio->u.isp24.fcp_cmnd.rddata &&
4006 atio->u.isp24.fcp_cmnd.wrdata) {
4008 data_dir = DMA_TO_DEVICE;
4009 } else if (atio->u.isp24.fcp_cmnd.rddata)
4010 data_dir = DMA_FROM_DEVICE;
4011 else if (atio->u.isp24.fcp_cmnd.wrdata)
4012 data_dir = DMA_TO_DEVICE;
4014 data_dir = DMA_NONE;
4016 fcp_task_attr = qlt_get_fcp_task_attr(vha,
4017 atio->u.isp24.fcp_cmnd.task_attr);
4018 data_length = be32_to_cpu(get_unaligned((uint32_t *)
4019 &atio->u.isp24.fcp_cmnd.add_cdb[
4020 atio->u.isp24.fcp_cmnd.add_cdb_len]));
4022 ret = ha->tgt.tgt_ops->handle_cmd(vha, cmd, cdb, data_length,
4023 fcp_task_attr, data_dir, bidi);
4027 * Drop extra session reference from qla_tgt_handle_cmd_for_atio*(
4029 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
4030 ha->tgt.tgt_ops->put_sess(sess);
4031 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
4035 ql_dbg(ql_dbg_io, vha, 0x3060, "Terminating work cmd %p", cmd);
4037 * cmd has not sent to target yet, so pass NULL as the second
4038 * argument to qlt_send_term_exchange() and free the memory here.
4040 cmd->trc_flags |= TRC_DO_WORK_ERR;
4041 spin_lock_irqsave(qpair->qp_lock_ptr, flags);
4042 qlt_send_term_exchange(qpair, NULL, &cmd->atio, 1, 0);
4044 qlt_decr_num_pend_cmds(vha);
4045 percpu_ida_free(&sess->se_sess->sess_tag_pool, cmd->se_cmd.map_tag);
4046 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
4048 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
4049 ha->tgt.tgt_ops->put_sess(sess);
4050 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
4053 static void qlt_do_work(struct work_struct *work)
4055 struct qla_tgt_cmd *cmd = container_of(work, struct qla_tgt_cmd, work);
4056 scsi_qla_host_t *vha = cmd->vha;
4057 unsigned long flags;
4059 spin_lock_irqsave(&vha->cmd_list_lock, flags);
4060 list_del(&cmd->cmd_list);
4061 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
4066 void qlt_clr_qp_table(struct scsi_qla_host *vha)
4068 unsigned long flags;
4069 struct qla_hw_data *ha = vha->hw;
4070 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
4074 ql_log(ql_log_info, vha, 0x706c,
4075 "User update Number of Active Qpairs %d\n",
4076 ha->tgt.num_act_qpairs);
4078 spin_lock_irqsave(&ha->tgt.atio_lock, flags);
4080 btree_for_each_safe64(&tgt->lun_qpair_map, key, node)
4081 btree_remove64(&tgt->lun_qpair_map, key);
4083 ha->base_qpair->lun_cnt = 0;
4084 for (key = 0; key < ha->max_qpairs; key++)
4085 if (ha->queue_pair_map[key])
4086 ha->queue_pair_map[key]->lun_cnt = 0;
4088 spin_unlock_irqrestore(&ha->tgt.atio_lock, flags);
4091 static void qlt_assign_qpair(struct scsi_qla_host *vha,
4092 struct qla_tgt_cmd *cmd)
4094 struct qla_qpair *qpair, *qp;
4095 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
4096 struct qla_qpair_hint *h;
4098 if (vha->flags.qpairs_available) {
4099 h = btree_lookup64(&tgt->lun_qpair_map, cmd->unpacked_lun);
4101 /* spread lun to qpair ratio evently */
4103 struct scsi_qla_host *base_vha =
4104 pci_get_drvdata(vha->hw->pdev);
4106 qpair = vha->hw->base_qpair;
4107 if (qpair->lun_cnt == 0) {
4109 h = qla_qpair_to_hint(tgt, qpair);
4111 rc = btree_insert64(&tgt->lun_qpair_map,
4112 cmd->unpacked_lun, h, GFP_ATOMIC);
4115 ql_log(ql_log_info, vha, 0xd037,
4116 "Unable to insert lun %llx into lun_qpair_map\n",
4121 lcnt = qpair->lun_cnt;
4125 list_for_each_entry(qp, &base_vha->qp_list,
4127 if (qp->lun_cnt == 0) {
4129 h = qla_qpair_to_hint(tgt, qp);
4131 rc = btree_insert64(&tgt->lun_qpair_map,
4132 cmd->unpacked_lun, h, GFP_ATOMIC);
4135 ql_log(ql_log_info, vha, 0xd038,
4136 "Unable to insert lun %llx into lun_qpair_map\n",
4142 if (qp->lun_cnt < lcnt) {
4151 h = qla_qpair_to_hint(tgt, qpair);
4153 rc = btree_insert64(&tgt->lun_qpair_map,
4154 cmd->unpacked_lun, h, GFP_ATOMIC);
4157 ql_log(ql_log_info, vha, 0xd039,
4158 "Unable to insert lun %llx into lun_qpair_map\n",
4163 h = &tgt->qphints[0];
4166 cmd->qpair = h->qpair;
4167 cmd->se_cmd.cpuid = h->cpuid;
4170 static struct qla_tgt_cmd *qlt_get_tag(scsi_qla_host_t *vha,
4171 struct fc_port *sess,
4172 struct atio_from_isp *atio)
4174 struct se_session *se_sess = sess->se_sess;
4175 struct qla_tgt_cmd *cmd;
4178 tag = percpu_ida_alloc(&se_sess->sess_tag_pool, TASK_RUNNING);
4182 cmd = &((struct qla_tgt_cmd *)se_sess->sess_cmd_map)[tag];
4183 memset(cmd, 0, sizeof(struct qla_tgt_cmd));
4184 cmd->cmd_type = TYPE_TGT_CMD;
4185 memcpy(&cmd->atio, atio, sizeof(*atio));
4186 cmd->state = QLA_TGT_STATE_NEW;
4187 cmd->tgt = vha->vha_tgt.qla_tgt;
4188 qlt_incr_num_pend_cmds(vha);
4190 cmd->se_cmd.map_tag = tag;
4192 cmd->loop_id = sess->loop_id;
4193 cmd->conf_compl_supported = sess->conf_compl_supported;
4196 cmd->jiffies_at_alloc = get_jiffies_64();
4198 cmd->unpacked_lun = scsilun_to_int(
4199 (struct scsi_lun *)&atio->u.isp24.fcp_cmnd.lun);
4200 qlt_assign_qpair(vha, cmd);
4201 cmd->reset_count = vha->hw->base_qpair->chip_reset;
4202 cmd->vp_idx = vha->vp_idx;
4207 static void qlt_create_sess_from_atio(struct work_struct *work)
4209 struct qla_tgt_sess_op *op = container_of(work,
4210 struct qla_tgt_sess_op, work);
4211 scsi_qla_host_t *vha = op->vha;
4212 struct qla_hw_data *ha = vha->hw;
4213 struct fc_port *sess;
4214 struct qla_tgt_cmd *cmd;
4215 unsigned long flags;
4216 uint8_t *s_id = op->atio.u.isp24.fcp_hdr.s_id;
4218 spin_lock_irqsave(&vha->cmd_list_lock, flags);
4219 list_del(&op->cmd_list);
4220 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
4223 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf083,
4224 "sess_op with tag %u is aborted\n",
4225 op->atio.u.isp24.exchange_addr);
4229 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf022,
4230 "qla_target(%d): Unable to find wwn login"
4231 " (s_id %x:%x:%x), trying to create it manually\n",
4232 vha->vp_idx, s_id[0], s_id[1], s_id[2]);
4234 if (op->atio.u.raw.entry_count > 1) {
4235 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf023,
4236 "Dropping multy entry atio %p\n", &op->atio);
4240 sess = qlt_make_local_sess(vha, s_id);
4241 /* sess has an extra creation ref. */
4246 * Now obtain a pre-allocated session tag using the original op->atio
4247 * packet header, and dispatch into __qlt_do_work() using the existing
4250 cmd = qlt_get_tag(vha, sess, &op->atio);
4252 struct qla_qpair *qpair = ha->base_qpair;
4254 spin_lock_irqsave(qpair->qp_lock_ptr, flags);
4255 qlt_send_busy(qpair, &op->atio, SAM_STAT_BUSY);
4256 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
4258 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
4259 ha->tgt.tgt_ops->put_sess(sess);
4260 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
4266 * __qlt_do_work() will call qlt_put_sess() to release
4267 * the extra reference taken above by qlt_make_local_sess()
4273 qlt_send_term_exchange(vha->hw->base_qpair, NULL, &op->atio, 0, 0);
4277 /* ha->hardware_lock supposed to be held on entry */
4278 static int qlt_handle_cmd_for_atio(struct scsi_qla_host *vha,
4279 struct atio_from_isp *atio)
4281 struct qla_hw_data *ha = vha->hw;
4282 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
4283 struct fc_port *sess;
4284 struct qla_tgt_cmd *cmd;
4285 unsigned long flags;
4287 if (unlikely(tgt->tgt_stop)) {
4288 ql_dbg(ql_dbg_io, vha, 0x3061,
4289 "New command while device %p is shutting down\n", tgt);
4293 sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha, atio->u.isp24.fcp_hdr.s_id);
4294 if (unlikely(!sess)) {
4295 struct qla_tgt_sess_op *op = kzalloc(sizeof(struct qla_tgt_sess_op),
4300 memcpy(&op->atio, atio, sizeof(*atio));
4303 spin_lock_irqsave(&vha->cmd_list_lock, flags);
4304 list_add_tail(&op->cmd_list, &vha->qla_sess_op_cmd_list);
4305 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
4307 INIT_WORK(&op->work, qlt_create_sess_from_atio);
4308 queue_work(qla_tgt_wq, &op->work);
4312 /* Another WWN used to have our s_id. Our PLOGI scheduled its
4313 * session deletion, but it's still in sess_del_work wq */
4314 if (sess->deleted) {
4315 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf002,
4316 "New command while old session %p is being deleted\n",
4322 * Do kref_get() before returning + dropping qla_hw_data->hardware_lock.
4324 if (!kref_get_unless_zero(&sess->sess_kref)) {
4325 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf004,
4326 "%s: kref_get fail, %8phC oxid %x \n",
4327 __func__, sess->port_name,
4328 be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id));
4332 cmd = qlt_get_tag(vha, sess, atio);
4334 ql_dbg(ql_dbg_io, vha, 0x3062,
4335 "qla_target(%d): Allocation of cmd failed\n", vha->vp_idx);
4336 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
4337 ha->tgt.tgt_ops->put_sess(sess);
4338 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
4343 cmd->trc_flags |= TRC_NEW_CMD;
4345 spin_lock_irqsave(&vha->cmd_list_lock, flags);
4346 list_add_tail(&cmd->cmd_list, &vha->qla_cmd_list);
4347 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
4349 INIT_WORK(&cmd->work, qlt_do_work);
4350 if (vha->flags.qpairs_available) {
4351 queue_work_on(cmd->se_cmd.cpuid, qla_tgt_wq, &cmd->work);
4352 } else if (ha->msix_count) {
4353 if (cmd->atio.u.isp24.fcp_cmnd.rddata)
4354 queue_work_on(smp_processor_id(), qla_tgt_wq,
4357 queue_work_on(cmd->se_cmd.cpuid, qla_tgt_wq,
4360 queue_work(qla_tgt_wq, &cmd->work);
4366 /* ha->hardware_lock supposed to be held on entry */
4367 static int qlt_issue_task_mgmt(struct fc_port *sess, u64 lun,
4368 int fn, void *iocb, int flags)
4370 struct scsi_qla_host *vha = sess->vha;
4371 struct qla_hw_data *ha = vha->hw;
4372 struct qla_tgt_mgmt_cmd *mcmd;
4373 struct atio_from_isp *a = (struct atio_from_isp *)iocb;
4376 mcmd = mempool_alloc(qla_tgt_mgmt_cmd_mempool, GFP_ATOMIC);
4378 ql_dbg(ql_dbg_tgt_tmr, vha, 0x10009,
4379 "qla_target(%d): Allocation of management "
4380 "command failed, some commands and their data could "
4381 "leak\n", vha->vp_idx);
4384 memset(mcmd, 0, sizeof(*mcmd));
4388 memcpy(&mcmd->orig_iocb.imm_ntfy, iocb,
4389 sizeof(mcmd->orig_iocb.imm_ntfy));
4391 mcmd->tmr_func = fn;
4392 mcmd->flags = flags;
4393 mcmd->reset_count = ha->base_qpair->chip_reset;
4394 mcmd->qpair = ha->base_qpair;
4398 case QLA_TGT_LUN_RESET:
4399 abort_cmds_for_lun(vha, lun, a->u.isp24.fcp_hdr.s_id);
4403 res = ha->tgt.tgt_ops->handle_tmr(mcmd, lun, mcmd->tmr_func, 0);
4405 ql_dbg(ql_dbg_tgt_tmr, vha, 0x1000b,
4406 "qla_target(%d): tgt.tgt_ops->handle_tmr() failed: %d\n",
4407 sess->vha->vp_idx, res);
4408 mempool_free(mcmd, qla_tgt_mgmt_cmd_mempool);
4415 /* ha->hardware_lock supposed to be held on entry */
4416 static int qlt_handle_task_mgmt(struct scsi_qla_host *vha, void *iocb)
4418 struct atio_from_isp *a = (struct atio_from_isp *)iocb;
4419 struct qla_hw_data *ha = vha->hw;
4420 struct qla_tgt *tgt;
4421 struct fc_port *sess;
4424 unsigned long flags;
4426 tgt = vha->vha_tgt.qla_tgt;
4428 fn = a->u.isp24.fcp_cmnd.task_mgmt_flags;
4430 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
4431 sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha,
4432 a->u.isp24.fcp_hdr.s_id);
4433 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
4436 scsilun_to_int((struct scsi_lun *)&a->u.isp24.fcp_cmnd.lun);
4439 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf024,
4440 "qla_target(%d): task mgmt fn 0x%x for "
4441 "non-existant session\n", vha->vp_idx, fn);
4442 return qlt_sched_sess_work(tgt, QLA_TGT_SESS_WORK_TM, iocb,
4443 sizeof(struct atio_from_isp));
4449 return qlt_issue_task_mgmt(sess, unpacked_lun, fn, iocb, 0);
4452 /* ha->hardware_lock supposed to be held on entry */
4453 static int __qlt_abort_task(struct scsi_qla_host *vha,
4454 struct imm_ntfy_from_isp *iocb, struct fc_port *sess)
4456 struct atio_from_isp *a = (struct atio_from_isp *)iocb;
4457 struct qla_hw_data *ha = vha->hw;
4458 struct qla_tgt_mgmt_cmd *mcmd;
4462 mcmd = mempool_alloc(qla_tgt_mgmt_cmd_mempool, GFP_ATOMIC);
4464 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05f,
4465 "qla_target(%d): %s: Allocation of ABORT cmd failed\n",
4466 vha->vp_idx, __func__);
4469 memset(mcmd, 0, sizeof(*mcmd));
4472 memcpy(&mcmd->orig_iocb.imm_ntfy, iocb,
4473 sizeof(mcmd->orig_iocb.imm_ntfy));
4476 scsilun_to_int((struct scsi_lun *)&a->u.isp24.fcp_cmnd.lun);
4477 mcmd->reset_count = ha->base_qpair->chip_reset;
4478 mcmd->tmr_func = QLA_TGT_2G_ABORT_TASK;
4479 mcmd->qpair = ha->base_qpair;
4481 rc = ha->tgt.tgt_ops->handle_tmr(mcmd, unpacked_lun, mcmd->tmr_func,
4482 le16_to_cpu(iocb->u.isp2x.seq_id));
4484 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf060,
4485 "qla_target(%d): tgt_ops->handle_tmr() failed: %d\n",
4487 mempool_free(mcmd, qla_tgt_mgmt_cmd_mempool);
4494 /* ha->hardware_lock supposed to be held on entry */
4495 static int qlt_abort_task(struct scsi_qla_host *vha,
4496 struct imm_ntfy_from_isp *iocb)
4498 struct qla_hw_data *ha = vha->hw;
4499 struct fc_port *sess;
4501 unsigned long flags;
4503 loop_id = GET_TARGET_ID(ha, (struct atio_from_isp *)iocb);
4505 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
4506 sess = ha->tgt.tgt_ops->find_sess_by_loop_id(vha, loop_id);
4507 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
4510 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf025,
4511 "qla_target(%d): task abort for unexisting "
4512 "session\n", vha->vp_idx);
4513 return qlt_sched_sess_work(vha->vha_tgt.qla_tgt,
4514 QLA_TGT_SESS_WORK_ABORT, iocb, sizeof(*iocb));
4517 return __qlt_abort_task(vha, iocb, sess);
4520 void qlt_logo_completion_handler(fc_port_t *fcport, int rc)
4522 if (rc != MBS_COMMAND_COMPLETE) {
4523 ql_dbg(ql_dbg_tgt_mgt, fcport->vha, 0xf093,
4524 "%s: se_sess %p / sess %p from"
4525 " port %8phC loop_id %#04x s_id %02x:%02x:%02x"
4526 " LOGO failed: %#x\n",
4530 fcport->port_name, fcport->loop_id,
4531 fcport->d_id.b.domain, fcport->d_id.b.area,
4532 fcport->d_id.b.al_pa, rc);
4535 fcport->logout_completed = 1;
4539 * ha->hardware_lock supposed to be held on entry (to protect tgt->sess_list)
4541 * Schedules sessions with matching port_id/loop_id but different wwn for
4542 * deletion. Returns existing session with matching wwn if present.
4546 qlt_find_sess_invalidate_other(scsi_qla_host_t *vha, uint64_t wwn,
4547 port_id_t port_id, uint16_t loop_id, struct fc_port **conflict_sess)
4549 struct fc_port *sess = NULL, *other_sess;
4552 *conflict_sess = NULL;
4554 list_for_each_entry(other_sess, &vha->vp_fcports, list) {
4556 other_wwn = wwn_to_u64(other_sess->port_name);
4558 if (wwn == other_wwn) {
4564 /* find other sess with nport_id collision */
4565 if (port_id.b24 == other_sess->d_id.b24) {
4566 if (loop_id != other_sess->loop_id) {
4567 ql_dbg(ql_dbg_tgt_tmr, vha, 0x1000c,
4568 "Invalidating sess %p loop_id %d wwn %llx.\n",
4569 other_sess, other_sess->loop_id, other_wwn);
4572 * logout_on_delete is set by default, but another
4573 * session that has the same s_id/loop_id combo
4574 * might have cleared it when requested this session
4575 * deletion, so don't touch it
4577 qlt_schedule_sess_for_deletion(other_sess, true);
4580 * Another wwn used to have our s_id/loop_id
4581 * kill the session, but don't free the loop_id
4583 ql_dbg(ql_dbg_tgt_tmr, vha, 0xf01b,
4584 "Invalidating sess %p loop_id %d wwn %llx.\n",
4585 other_sess, other_sess->loop_id, other_wwn);
4588 other_sess->keep_nport_handle = 1;
4589 *conflict_sess = other_sess;
4590 qlt_schedule_sess_for_deletion(other_sess,
4596 /* find other sess with nport handle collision */
4597 if ((loop_id == other_sess->loop_id) &&
4598 (loop_id != FC_NO_LOOP_ID)) {
4599 ql_dbg(ql_dbg_tgt_tmr, vha, 0x1000d,
4600 "Invalidating sess %p loop_id %d wwn %llx.\n",
4601 other_sess, other_sess->loop_id, other_wwn);
4603 /* Same loop_id but different s_id
4604 * Ok to kill and logout */
4605 qlt_schedule_sess_for_deletion(other_sess, true);
4612 /* Abort any commands for this s_id waiting on qla_tgt_wq workqueue */
4613 static int abort_cmds_for_s_id(struct scsi_qla_host *vha, port_id_t *s_id)
4615 struct qla_tgt_sess_op *op;
4616 struct qla_tgt_cmd *cmd;
4619 unsigned long flags;
4621 key = (((u32)s_id->b.domain << 16) |
4622 ((u32)s_id->b.area << 8) |
4623 ((u32)s_id->b.al_pa));
4625 spin_lock_irqsave(&vha->cmd_list_lock, flags);
4626 list_for_each_entry(op, &vha->qla_sess_op_cmd_list, cmd_list) {
4627 uint32_t op_key = sid_to_key(op->atio.u.isp24.fcp_hdr.s_id);
4629 if (op_key == key) {
4635 list_for_each_entry(op, &vha->unknown_atio_list, cmd_list) {
4636 uint32_t op_key = sid_to_key(op->atio.u.isp24.fcp_hdr.s_id);
4637 if (op_key == key) {
4643 list_for_each_entry(cmd, &vha->qla_cmd_list, cmd_list) {
4644 uint32_t cmd_key = sid_to_key(cmd->atio.u.isp24.fcp_hdr.s_id);
4645 if (cmd_key == key) {
4650 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
4656 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
4658 static int qlt_24xx_handle_els(struct scsi_qla_host *vha,
4659 struct imm_ntfy_from_isp *iocb)
4661 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
4662 struct qla_hw_data *ha = vha->hw;
4663 struct fc_port *sess = NULL, *conflict_sess = NULL;
4669 struct qlt_plogi_ack_t *pla;
4670 unsigned long flags;
4672 wwn = wwn_to_u64(iocb->u.isp24.port_name);
4674 port_id.b.domain = iocb->u.isp24.port_id[2];
4675 port_id.b.area = iocb->u.isp24.port_id[1];
4676 port_id.b.al_pa = iocb->u.isp24.port_id[0];
4677 port_id.b.rsvd_1 = 0;
4679 loop_id = le16_to_cpu(iocb->u.isp24.nport_handle);
4681 ql_dbg(ql_dbg_disc, vha, 0xf026,
4682 "qla_target(%d): Port ID: %02x:%02x:%02x ELS opcode: 0x%02x lid %d %8phC\n",
4683 vha->vp_idx, iocb->u.isp24.port_id[2],
4684 iocb->u.isp24.port_id[1], iocb->u.isp24.port_id[0],
4685 iocb->u.isp24.status_subcode, loop_id,
4686 iocb->u.isp24.port_name);
4688 /* res = 1 means ack at the end of thread
4689 * res = 0 means ack async/later.
4691 switch (iocb->u.isp24.status_subcode) {
4694 /* Mark all stale commands in qla_tgt_wq for deletion */
4695 abort_cmds_for_s_id(vha, &port_id);
4698 spin_lock_irqsave(&tgt->ha->tgt.sess_lock, flags);
4699 sess = qlt_find_sess_invalidate_other(vha, wwn,
4700 port_id, loop_id, &conflict_sess);
4701 spin_unlock_irqrestore(&tgt->ha->tgt.sess_lock, flags);
4704 if (IS_SW_RESV_ADDR(port_id)) {
4709 pla = qlt_plogi_ack_find_add(vha, &port_id, iocb);
4711 qlt_send_term_imm_notif(vha, iocb, 1);
4717 if (conflict_sess) {
4718 conflict_sess->login_gen++;
4719 qlt_plogi_ack_link(vha, pla, conflict_sess,
4720 QLT_PLOGI_LINK_CONFLICT);
4725 qla24xx_post_newsess_work(vha, &port_id,
4726 iocb->u.isp24.port_name, pla);
4731 qlt_plogi_ack_link(vha, pla, sess, QLT_PLOGI_LINK_SAME_WWN);
4732 sess->fw_login_state = DSC_LS_PLOGI_PEND;
4733 sess->d_id = port_id;
4736 switch (sess->disc_state) {
4738 qlt_plogi_ack_unref(vha, pla);
4743 * Under normal circumstances we want to release nport handle
4744 * during LOGO process to avoid nport handle leaks inside FW.
4745 * The exception is when LOGO is done while another PLOGI with
4746 * the same nport handle is waiting as might be the case here.
4747 * Note: there is always a possibily of a race where session
4748 * deletion has already started for other reasons (e.g. ACL
4749 * removal) and now PLOGI arrives:
4750 * 1. if PLOGI arrived in FW after nport handle has been freed,
4751 * FW must have assigned this PLOGI a new/same handle and we
4752 * can proceed ACK'ing it as usual when session deletion
4754 * 2. if PLOGI arrived in FW before LOGO with LCF_FREE_NPORT
4755 * bit reached it, the handle has now been released. We'll
4756 * get an error when we ACK this PLOGI. Nothing will be sent
4757 * back to initiator. Initiator should eventually retry
4758 * PLOGI and situation will correct itself.
4760 sess->keep_nport_handle = ((sess->loop_id == loop_id) &&
4761 (sess->d_id.b24 == port_id.b24));
4763 ql_dbg(ql_dbg_disc, vha, 0x20f9,
4764 "%s %d %8phC post del sess\n",
4765 __func__, __LINE__, sess->port_name);
4768 qlt_schedule_sess_for_deletion_lock(sess);
4775 wd3_lo = le16_to_cpu(iocb->u.isp24.u.prli.wd3_lo);
4778 spin_lock_irqsave(&tgt->ha->tgt.sess_lock, flags);
4779 sess = qlt_find_sess_invalidate_other(vha, wwn, port_id,
4780 loop_id, &conflict_sess);
4781 spin_unlock_irqrestore(&tgt->ha->tgt.sess_lock, flags);
4784 if (conflict_sess) {
4785 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf09b,
4786 "PRLI with conflicting sess %p port %8phC\n",
4787 conflict_sess, conflict_sess->port_name);
4788 qlt_send_term_imm_notif(vha, iocb, 1);
4794 if (sess->fw_login_state != DSC_LS_PLOGI_PEND &&
4795 sess->fw_login_state != DSC_LS_PLOGI_COMP) {
4797 * Impatient initiator sent PRLI before last
4798 * PLOGI could finish. Will force him to re-try,
4799 * while last one finishes.
4801 ql_log(ql_log_warn, sess->vha, 0xf095,
4802 "sess %p PRLI received, before plogi ack.\n",
4804 qlt_send_term_imm_notif(vha, iocb, 1);
4810 * This shouldn't happen under normal circumstances,
4811 * since we have deleted the old session during PLOGI
4813 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf096,
4814 "PRLI (loop_id %#04x) for existing sess %p (loop_id %#04x)\n",
4815 sess->loop_id, sess, iocb->u.isp24.nport_handle);
4818 sess->loop_id = loop_id;
4819 sess->d_id = port_id;
4820 sess->fw_login_state = DSC_LS_PRLI_PEND;
4823 sess->conf_compl_supported = 1;
4825 if ((wd3_lo & BIT_4) == 0)
4826 sess->port_type = FCT_INITIATOR;
4828 sess->port_type = FCT_TARGET;
4830 res = 1; /* send notify ack */
4832 /* Make session global (not used in fabric mode) */
4833 if (ha->current_topology != ISP_CFG_F) {
4835 ql_dbg(ql_dbg_disc, vha, 0x20fa,
4836 "%s %d %8phC post nack\n",
4837 __func__, __LINE__, sess->port_name);
4838 qla24xx_post_nack_work(vha, sess, iocb,
4842 set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
4843 set_bit(LOCAL_LOOP_UPDATE, &vha->dpc_flags);
4844 qla2xxx_wake_dpc(vha);
4848 ql_dbg(ql_dbg_disc, vha, 0x20fb,
4849 "%s %d %8phC post nack\n",
4850 __func__, __LINE__, sess->port_name);
4851 qla24xx_post_nack_work(vha, sess, iocb,
4859 if (le16_to_cpu(iocb->u.isp24.flags) &
4860 NOTIFY24XX_FLAGS_GLOBAL_TPRLO) {
4862 qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS);
4869 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
4870 sess = qla2x00_find_fcport_by_loopid(vha, loop_id);
4871 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
4875 sess->fw_login_state = DSC_LS_LOGO_PEND;
4876 sess->logo_ack_needed = 1;
4877 memcpy(sess->iocb, iocb, IOCB_SIZE);
4880 res = qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS_SESS);
4882 ql_dbg(ql_dbg_disc, vha, 0x20fc,
4883 "%s: logo %llx res %d sess %p ",
4884 __func__, wwn, res, sess);
4887 * cmd went upper layer, look for qlt_xmit_tm_rsp()
4888 * for LOGO_ACK & sess delete
4893 /* cmd did not go to upper layer. */
4895 qlt_schedule_sess_for_deletion_lock(sess);
4898 /* else logo will be ack */
4904 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
4905 if (tgt->link_reinit_iocb_pending) {
4906 qlt_send_notify_ack(ha->base_qpair,
4907 &tgt->link_reinit_iocb, 0, 0, 0, 0, 0, 0);
4908 tgt->link_reinit_iocb_pending = 0;
4911 sess = qla2x00_find_fcport_by_wwpn(vha,
4912 iocb->u.isp24.port_name, 1);
4914 ql_dbg(ql_dbg_disc, vha, 0x20fd,
4915 "sess %p lid %d|%d DS %d LS %d\n",
4916 sess, sess->loop_id, loop_id,
4917 sess->disc_state, sess->fw_login_state);
4920 res = 1; /* send notify ack */
4924 case ELS_FLOGI: /* should never happen */
4926 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf061,
4927 "qla_target(%d): Unsupported ELS command %x "
4928 "received\n", vha->vp_idx, iocb->u.isp24.status_subcode);
4929 res = qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS_SESS);
4937 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
4939 static void qlt_handle_imm_notify(struct scsi_qla_host *vha,
4940 struct imm_ntfy_from_isp *iocb)
4942 struct qla_hw_data *ha = vha->hw;
4943 uint32_t add_flags = 0;
4944 int send_notify_ack = 1;
4947 status = le16_to_cpu(iocb->u.isp2x.status);
4949 case IMM_NTFY_LIP_RESET:
4951 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf032,
4952 "qla_target(%d): LIP reset (loop %#x), subcode %x\n",
4953 vha->vp_idx, le16_to_cpu(iocb->u.isp24.nport_handle),
4954 iocb->u.isp24.status_subcode);
4956 if (qlt_reset(vha, iocb, QLA_TGT_ABORT_ALL) == 0)
4957 send_notify_ack = 0;
4961 case IMM_NTFY_LIP_LINK_REINIT:
4963 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
4964 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf033,
4965 "qla_target(%d): LINK REINIT (loop %#x, "
4966 "subcode %x)\n", vha->vp_idx,
4967 le16_to_cpu(iocb->u.isp24.nport_handle),
4968 iocb->u.isp24.status_subcode);
4969 if (tgt->link_reinit_iocb_pending) {
4970 qlt_send_notify_ack(ha->base_qpair,
4971 &tgt->link_reinit_iocb, 0, 0, 0, 0, 0, 0);
4973 memcpy(&tgt->link_reinit_iocb, iocb, sizeof(*iocb));
4974 tgt->link_reinit_iocb_pending = 1;
4976 * QLogic requires to wait after LINK REINIT for possible
4977 * PDISC or ADISC ELS commands
4979 send_notify_ack = 0;
4983 case IMM_NTFY_PORT_LOGOUT:
4984 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf034,
4985 "qla_target(%d): Port logout (loop "
4986 "%#x, subcode %x)\n", vha->vp_idx,
4987 le16_to_cpu(iocb->u.isp24.nport_handle),
4988 iocb->u.isp24.status_subcode);
4990 if (qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS_SESS) == 0)
4991 send_notify_ack = 0;
4992 /* The sessions will be cleared in the callback, if needed */
4995 case IMM_NTFY_GLBL_TPRLO:
4996 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf035,
4997 "qla_target(%d): Global TPRLO (%x)\n", vha->vp_idx, status);
4998 if (qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS) == 0)
4999 send_notify_ack = 0;
5000 /* The sessions will be cleared in the callback, if needed */
5003 case IMM_NTFY_PORT_CONFIG:
5004 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf036,
5005 "qla_target(%d): Port config changed (%x)\n", vha->vp_idx,
5007 if (qlt_reset(vha, iocb, QLA_TGT_ABORT_ALL) == 0)
5008 send_notify_ack = 0;
5009 /* The sessions will be cleared in the callback, if needed */
5012 case IMM_NTFY_GLBL_LOGO:
5013 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06a,
5014 "qla_target(%d): Link failure detected\n",
5016 /* I_T nexus loss */
5017 if (qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS) == 0)
5018 send_notify_ack = 0;
5021 case IMM_NTFY_IOCB_OVERFLOW:
5022 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06b,
5023 "qla_target(%d): Cannot provide requested "
5024 "capability (IOCB overflowed the immediate notify "
5025 "resource count)\n", vha->vp_idx);
5028 case IMM_NTFY_ABORT_TASK:
5029 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf037,
5030 "qla_target(%d): Abort Task (S %08x I %#x -> "
5031 "L %#x)\n", vha->vp_idx,
5032 le16_to_cpu(iocb->u.isp2x.seq_id),
5033 GET_TARGET_ID(ha, (struct atio_from_isp *)iocb),
5034 le16_to_cpu(iocb->u.isp2x.lun));
5035 if (qlt_abort_task(vha, iocb) == 0)
5036 send_notify_ack = 0;
5039 case IMM_NTFY_RESOURCE:
5040 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06c,
5041 "qla_target(%d): Out of resources, host %ld\n",
5042 vha->vp_idx, vha->host_no);
5045 case IMM_NTFY_MSG_RX:
5046 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf038,
5047 "qla_target(%d): Immediate notify task %x\n",
5048 vha->vp_idx, iocb->u.isp2x.task_flags);
5049 if (qlt_handle_task_mgmt(vha, iocb) == 0)
5050 send_notify_ack = 0;
5054 if (qlt_24xx_handle_els(vha, iocb) == 0)
5055 send_notify_ack = 0;
5058 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06d,
5059 "qla_target(%d): Received unknown immediate "
5060 "notify status %x\n", vha->vp_idx, status);
5064 if (send_notify_ack)
5065 qlt_send_notify_ack(ha->base_qpair, iocb, add_flags, 0, 0, 0,
5070 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
5071 * This function sends busy to ISP 2xxx or 24xx.
5073 static int __qlt_send_busy(struct qla_qpair *qpair,
5074 struct atio_from_isp *atio, uint16_t status)
5076 struct scsi_qla_host *vha = qpair->vha;
5077 struct ctio7_to_24xx *ctio24;
5078 struct qla_hw_data *ha = vha->hw;
5080 struct fc_port *sess = NULL;
5081 unsigned long flags;
5084 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
5085 sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha,
5086 atio->u.isp24.fcp_hdr.s_id);
5087 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
5089 qlt_send_term_exchange(qpair, NULL, atio, 1, 0);
5092 /* Sending marker isn't necessary, since we called from ISR */
5094 pkt = (request_t *)__qla2x00_alloc_iocbs(qpair, NULL);
5096 ql_dbg(ql_dbg_io, vha, 0x3063,
5097 "qla_target(%d): %s failed: unable to allocate "
5098 "request packet", vha->vp_idx, __func__);
5102 qpair->tgt_counters.num_q_full_sent++;
5103 pkt->entry_count = 1;
5104 pkt->handle = QLA_TGT_SKIP_HANDLE | CTIO_COMPLETION_HANDLE_MARK;
5106 ctio24 = (struct ctio7_to_24xx *)pkt;
5107 ctio24->entry_type = CTIO_TYPE7;
5108 ctio24->nport_handle = sess->loop_id;
5109 ctio24->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
5110 ctio24->vp_index = vha->vp_idx;
5111 ctio24->initiator_id[0] = atio->u.isp24.fcp_hdr.s_id[2];
5112 ctio24->initiator_id[1] = atio->u.isp24.fcp_hdr.s_id[1];
5113 ctio24->initiator_id[2] = atio->u.isp24.fcp_hdr.s_id[0];
5114 ctio24->exchange_addr = atio->u.isp24.exchange_addr;
5115 temp = (atio->u.isp24.attr << 9) |
5116 CTIO7_FLAGS_STATUS_MODE_1 | CTIO7_FLAGS_SEND_STATUS |
5117 CTIO7_FLAGS_DONT_RET_CTIO;
5118 ctio24->u.status1.flags = cpu_to_le16(temp);
5120 * CTIO from fw w/o se_cmd doesn't provide enough info to retry it,
5121 * if the explicit conformation is used.
5123 ctio24->u.status1.ox_id = swab16(atio->u.isp24.fcp_hdr.ox_id);
5124 ctio24->u.status1.scsi_status = cpu_to_le16(status);
5125 /* Memory Barrier */
5127 if (qpair->reqq_start_iocbs)
5128 qpair->reqq_start_iocbs(qpair);
5130 qla2x00_start_iocbs(vha, qpair->req);
5135 * This routine is used to allocate a command for either a QFull condition
5136 * (ie reply SAM_STAT_BUSY) or to terminate an exchange that did not go
5140 qlt_alloc_qfull_cmd(struct scsi_qla_host *vha,
5141 struct atio_from_isp *atio, uint16_t status, int qfull)
5143 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
5144 struct qla_hw_data *ha = vha->hw;
5145 struct fc_port *sess;
5146 struct se_session *se_sess;
5147 struct qla_tgt_cmd *cmd;
5149 unsigned long flags;
5151 if (unlikely(tgt->tgt_stop)) {
5152 ql_dbg(ql_dbg_io, vha, 0x300a,
5153 "New command while device %p is shutting down\n", tgt);
5157 if ((vha->hw->tgt.num_qfull_cmds_alloc + 1) > MAX_QFULL_CMDS_ALLOC) {
5158 vha->hw->tgt.num_qfull_cmds_dropped++;
5159 if (vha->hw->tgt.num_qfull_cmds_dropped >
5160 vha->qla_stats.stat_max_qfull_cmds_dropped)
5161 vha->qla_stats.stat_max_qfull_cmds_dropped =
5162 vha->hw->tgt.num_qfull_cmds_dropped;
5164 ql_dbg(ql_dbg_io, vha, 0x3068,
5165 "qla_target(%d): %s: QFull CMD dropped[%d]\n",
5166 vha->vp_idx, __func__,
5167 vha->hw->tgt.num_qfull_cmds_dropped);
5169 qlt_chk_exch_leak_thresh_hold(vha);
5173 sess = ha->tgt.tgt_ops->find_sess_by_s_id
5174 (vha, atio->u.isp24.fcp_hdr.s_id);
5178 se_sess = sess->se_sess;
5180 tag = percpu_ida_alloc(&se_sess->sess_tag_pool, TASK_RUNNING);
5184 cmd = &((struct qla_tgt_cmd *)se_sess->sess_cmd_map)[tag];
5186 ql_dbg(ql_dbg_io, vha, 0x3009,
5187 "qla_target(%d): %s: Allocation of cmd failed\n",
5188 vha->vp_idx, __func__);
5190 vha->hw->tgt.num_qfull_cmds_dropped++;
5191 if (vha->hw->tgt.num_qfull_cmds_dropped >
5192 vha->qla_stats.stat_max_qfull_cmds_dropped)
5193 vha->qla_stats.stat_max_qfull_cmds_dropped =
5194 vha->hw->tgt.num_qfull_cmds_dropped;
5196 qlt_chk_exch_leak_thresh_hold(vha);
5200 memset(cmd, 0, sizeof(struct qla_tgt_cmd));
5202 qlt_incr_num_pend_cmds(vha);
5203 INIT_LIST_HEAD(&cmd->cmd_list);
5204 memcpy(&cmd->atio, atio, sizeof(*atio));
5206 cmd->tgt = vha->vha_tgt.qla_tgt;
5208 cmd->reset_count = ha->base_qpair->chip_reset;
5210 cmd->qpair = ha->base_qpair;
5214 /* NOTE: borrowing the state field to carry the status */
5215 cmd->state = status;
5217 cmd->term_exchg = 1;
5219 spin_lock_irqsave(&vha->hw->tgt.q_full_lock, flags);
5220 list_add_tail(&cmd->cmd_list, &vha->hw->tgt.q_full_list);
5222 vha->hw->tgt.num_qfull_cmds_alloc++;
5223 if (vha->hw->tgt.num_qfull_cmds_alloc >
5224 vha->qla_stats.stat_max_qfull_cmds_alloc)
5225 vha->qla_stats.stat_max_qfull_cmds_alloc =
5226 vha->hw->tgt.num_qfull_cmds_alloc;
5227 spin_unlock_irqrestore(&vha->hw->tgt.q_full_lock, flags);
5231 qlt_free_qfull_cmds(struct qla_qpair *qpair)
5233 struct scsi_qla_host *vha = qpair->vha;
5234 struct qla_hw_data *ha = vha->hw;
5235 unsigned long flags;
5236 struct qla_tgt_cmd *cmd, *tcmd;
5237 struct list_head free_list, q_full_list;
5240 if (list_empty(&ha->tgt.q_full_list))
5243 INIT_LIST_HEAD(&free_list);
5244 INIT_LIST_HEAD(&q_full_list);
5246 spin_lock_irqsave(&vha->hw->tgt.q_full_lock, flags);
5247 if (list_empty(&ha->tgt.q_full_list)) {
5248 spin_unlock_irqrestore(&vha->hw->tgt.q_full_lock, flags);
5252 list_splice_init(&vha->hw->tgt.q_full_list, &q_full_list);
5253 spin_unlock_irqrestore(&vha->hw->tgt.q_full_lock, flags);
5255 spin_lock_irqsave(qpair->qp_lock_ptr, flags);
5256 list_for_each_entry_safe(cmd, tcmd, &q_full_list, cmd_list) {
5258 /* cmd->state is a borrowed field to hold status */
5259 rc = __qlt_send_busy(qpair, &cmd->atio, cmd->state);
5260 else if (cmd->term_exchg)
5261 rc = __qlt_send_term_exchange(qpair, NULL, &cmd->atio);
5267 ql_dbg(ql_dbg_io, vha, 0x3006,
5268 "%s: busy sent for ox_id[%04x]\n", __func__,
5269 be16_to_cpu(cmd->atio.u.isp24.fcp_hdr.ox_id));
5270 else if (cmd->term_exchg)
5271 ql_dbg(ql_dbg_io, vha, 0x3007,
5272 "%s: Term exchg sent for ox_id[%04x]\n", __func__,
5273 be16_to_cpu(cmd->atio.u.isp24.fcp_hdr.ox_id));
5275 ql_dbg(ql_dbg_io, vha, 0x3008,
5276 "%s: Unexpected cmd in QFull list %p\n", __func__,
5279 list_del(&cmd->cmd_list);
5280 list_add_tail(&cmd->cmd_list, &free_list);
5282 /* piggy back on hardware_lock for protection */
5283 vha->hw->tgt.num_qfull_cmds_alloc--;
5285 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
5289 list_for_each_entry_safe(cmd, tcmd, &free_list, cmd_list) {
5290 list_del(&cmd->cmd_list);
5291 /* This cmd was never sent to TCM. There is no need
5292 * to schedule free or call free_cmd
5297 if (!list_empty(&q_full_list)) {
5298 spin_lock_irqsave(&vha->hw->tgt.q_full_lock, flags);
5299 list_splice(&q_full_list, &vha->hw->tgt.q_full_list);
5300 spin_unlock_irqrestore(&vha->hw->tgt.q_full_lock, flags);
5307 qlt_send_busy(struct qla_qpair *qpair, struct atio_from_isp *atio,
5311 struct scsi_qla_host *vha = qpair->vha;
5313 rc = __qlt_send_busy(qpair, atio, status);
5315 qlt_alloc_qfull_cmd(vha, atio, status, 1);
5319 qlt_chk_qfull_thresh_hold(struct scsi_qla_host *vha, struct qla_qpair *qpair,
5320 struct atio_from_isp *atio, uint8_t ha_locked)
5322 struct qla_hw_data *ha = vha->hw;
5324 unsigned long flags;
5326 if (ha->tgt.num_pend_cmds < Q_FULL_THRESH_HOLD(ha))
5330 spin_lock_irqsave(&ha->hardware_lock, flags);
5331 status = temp_sam_status;
5332 qlt_send_busy(qpair, atio, status);
5334 spin_unlock_irqrestore(&ha->hardware_lock, flags);
5339 /* ha->hardware_lock supposed to be held on entry */
5340 /* called via callback from qla2xxx */
5341 static void qlt_24xx_atio_pkt(struct scsi_qla_host *vha,
5342 struct atio_from_isp *atio, uint8_t ha_locked)
5344 struct qla_hw_data *ha = vha->hw;
5345 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
5347 unsigned long flags;
5349 if (unlikely(tgt == NULL)) {
5350 ql_dbg(ql_dbg_tgt, vha, 0x3064,
5351 "ATIO pkt, but no tgt (ha %p)", ha);
5355 * In tgt_stop mode we also should allow all requests to pass.
5356 * Otherwise, some commands can stuck.
5359 tgt->atio_irq_cmd_count++;
5361 switch (atio->u.raw.entry_type) {
5363 if (unlikely(atio->u.isp24.exchange_addr ==
5364 ATIO_EXCHANGE_ADDRESS_UNKNOWN)) {
5365 ql_dbg(ql_dbg_io, vha, 0x3065,
5366 "qla_target(%d): ATIO_TYPE7 "
5367 "received with UNKNOWN exchange address, "
5368 "sending QUEUE_FULL\n", vha->vp_idx);
5370 spin_lock_irqsave(&ha->hardware_lock, flags);
5371 qlt_send_busy(ha->base_qpair, atio,
5372 SAM_STAT_TASK_SET_FULL);
5374 spin_unlock_irqrestore(&ha->hardware_lock,
5379 if (likely(atio->u.isp24.fcp_cmnd.task_mgmt_flags == 0)) {
5380 rc = qlt_chk_qfull_thresh_hold(vha, ha->base_qpair,
5383 tgt->atio_irq_cmd_count--;
5386 rc = qlt_handle_cmd_for_atio(vha, atio);
5388 rc = qlt_handle_task_mgmt(vha, atio);
5390 if (unlikely(rc != 0)) {
5393 spin_lock_irqsave(&ha->hardware_lock,
5396 #if 1 /* With TERM EXCHANGE some FC cards refuse to boot */
5397 qlt_send_busy(ha->base_qpair, atio,
5400 qlt_send_term_exchange(ha->base_qpair, NULL,
5404 spin_unlock_irqrestore(
5405 &ha->hardware_lock, flags);
5407 if (tgt->tgt_stop) {
5408 ql_dbg(ql_dbg_tgt, vha, 0xe059,
5409 "qla_target: Unable to send "
5410 "command to target for req, "
5413 ql_dbg(ql_dbg_tgt, vha, 0xe05a,
5414 "qla_target(%d): Unable to send "
5415 "command to target, sending BUSY "
5416 "status.\n", vha->vp_idx);
5419 &ha->hardware_lock, flags);
5420 qlt_send_busy(ha->base_qpair,
5421 atio, SAM_STAT_BUSY);
5423 spin_unlock_irqrestore(
5424 &ha->hardware_lock, flags);
5430 case IMMED_NOTIFY_TYPE:
5432 if (unlikely(atio->u.isp2x.entry_status != 0)) {
5433 ql_dbg(ql_dbg_tgt, vha, 0xe05b,
5434 "qla_target(%d): Received ATIO packet %x "
5435 "with error status %x\n", vha->vp_idx,
5436 atio->u.raw.entry_type,
5437 atio->u.isp2x.entry_status);
5440 ql_dbg(ql_dbg_tgt, vha, 0xe02e, "%s", "IMMED_NOTIFY ATIO");
5443 spin_lock_irqsave(&ha->hardware_lock, flags);
5444 qlt_handle_imm_notify(vha, (struct imm_ntfy_from_isp *)atio);
5446 spin_unlock_irqrestore(&ha->hardware_lock, flags);
5451 ql_dbg(ql_dbg_tgt, vha, 0xe05c,
5452 "qla_target(%d): Received unknown ATIO atio "
5453 "type %x\n", vha->vp_idx, atio->u.raw.entry_type);
5457 tgt->atio_irq_cmd_count--;
5460 /* ha->hardware_lock supposed to be held on entry */
5461 /* called via callback from qla2xxx */
5462 static void qlt_response_pkt(struct scsi_qla_host *vha,
5463 struct rsp_que *rsp, response_t *pkt)
5465 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
5467 if (unlikely(tgt == NULL)) {
5468 ql_dbg(ql_dbg_tgt, vha, 0xe05d,
5469 "qla_target(%d): Response pkt %x received, but no tgt (ha %p)\n",
5470 vha->vp_idx, pkt->entry_type, vha->hw);
5475 * In tgt_stop mode we also should allow all requests to pass.
5476 * Otherwise, some commands can stuck.
5479 switch (pkt->entry_type) {
5483 struct ctio7_from_24xx *entry = (struct ctio7_from_24xx *)pkt;
5484 qlt_do_ctio_completion(vha, rsp, entry->handle,
5485 le16_to_cpu(entry->status)|(pkt->entry_status << 16),
5490 case ACCEPT_TGT_IO_TYPE:
5492 struct atio_from_isp *atio = (struct atio_from_isp *)pkt;
5494 if (atio->u.isp2x.status !=
5495 cpu_to_le16(ATIO_CDB_VALID)) {
5496 ql_dbg(ql_dbg_tgt, vha, 0xe05e,
5497 "qla_target(%d): ATIO with error "
5498 "status %x received\n", vha->vp_idx,
5499 le16_to_cpu(atio->u.isp2x.status));
5503 rc = qlt_chk_qfull_thresh_hold(vha, rsp->qpair, atio, 1);
5507 rc = qlt_handle_cmd_for_atio(vha, atio);
5508 if (unlikely(rc != 0)) {
5510 #if 1 /* With TERM EXCHANGE some FC cards refuse to boot */
5511 qlt_send_busy(rsp->qpair, atio, 0);
5513 qlt_send_term_exchange(rsp->qpair, NULL, atio, 1, 0);
5516 if (tgt->tgt_stop) {
5517 ql_dbg(ql_dbg_tgt, vha, 0xe05f,
5518 "qla_target: Unable to send "
5519 "command to target, sending TERM "
5520 "EXCHANGE for rsp\n");
5521 qlt_send_term_exchange(rsp->qpair, NULL,
5524 ql_dbg(ql_dbg_tgt, vha, 0xe060,
5525 "qla_target(%d): Unable to send "
5526 "command to target, sending BUSY "
5527 "status\n", vha->vp_idx);
5528 qlt_send_busy(rsp->qpair, atio, 0);
5535 case CONTINUE_TGT_IO_TYPE:
5537 struct ctio_to_2xxx *entry = (struct ctio_to_2xxx *)pkt;
5538 qlt_do_ctio_completion(vha, rsp, entry->handle,
5539 le16_to_cpu(entry->status)|(pkt->entry_status << 16),
5546 struct ctio_to_2xxx *entry = (struct ctio_to_2xxx *)pkt;
5547 qlt_do_ctio_completion(vha, rsp, entry->handle,
5548 le16_to_cpu(entry->status)|(pkt->entry_status << 16),
5553 case IMMED_NOTIFY_TYPE:
5554 ql_dbg(ql_dbg_tgt, vha, 0xe035, "%s", "IMMED_NOTIFY\n");
5555 qlt_handle_imm_notify(vha, (struct imm_ntfy_from_isp *)pkt);
5558 case NOTIFY_ACK_TYPE:
5559 if (tgt->notify_ack_expected > 0) {
5560 struct nack_to_isp *entry = (struct nack_to_isp *)pkt;
5561 ql_dbg(ql_dbg_tgt, vha, 0xe036,
5562 "NOTIFY_ACK seq %08x status %x\n",
5563 le16_to_cpu(entry->u.isp2x.seq_id),
5564 le16_to_cpu(entry->u.isp2x.status));
5565 tgt->notify_ack_expected--;
5566 if (entry->u.isp2x.status !=
5567 cpu_to_le16(NOTIFY_ACK_SUCCESS)) {
5568 ql_dbg(ql_dbg_tgt, vha, 0xe061,
5569 "qla_target(%d): NOTIFY_ACK "
5570 "failed %x\n", vha->vp_idx,
5571 le16_to_cpu(entry->u.isp2x.status));
5574 ql_dbg(ql_dbg_tgt, vha, 0xe062,
5575 "qla_target(%d): Unexpected NOTIFY_ACK received\n",
5580 case ABTS_RECV_24XX:
5581 ql_dbg(ql_dbg_tgt, vha, 0xe037,
5582 "ABTS_RECV_24XX: instance %d\n", vha->vp_idx);
5583 qlt_24xx_handle_abts(vha, (struct abts_recv_from_24xx *)pkt);
5586 case ABTS_RESP_24XX:
5587 if (tgt->abts_resp_expected > 0) {
5588 struct abts_resp_from_24xx_fw *entry =
5589 (struct abts_resp_from_24xx_fw *)pkt;
5590 ql_dbg(ql_dbg_tgt, vha, 0xe038,
5591 "ABTS_RESP_24XX: compl_status %x\n",
5592 entry->compl_status);
5593 tgt->abts_resp_expected--;
5594 if (le16_to_cpu(entry->compl_status) !=
5595 ABTS_RESP_COMPL_SUCCESS) {
5596 if ((entry->error_subcode1 == 0x1E) &&
5597 (entry->error_subcode2 == 0)) {
5599 * We've got a race here: aborted
5600 * exchange not terminated, i.e.
5601 * response for the aborted command was
5602 * sent between the abort request was
5603 * received and processed.
5604 * Unfortunately, the firmware has a
5605 * silly requirement that all aborted
5606 * exchanges must be explicitely
5607 * terminated, otherwise it refuses to
5608 * send responses for the abort
5609 * requests. So, we have to
5610 * (re)terminate the exchange and retry
5611 * the abort response.
5613 qlt_24xx_retry_term_exchange(vha,
5616 ql_dbg(ql_dbg_tgt, vha, 0xe063,
5617 "qla_target(%d): ABTS_RESP_24XX "
5618 "failed %x (subcode %x:%x)",
5619 vha->vp_idx, entry->compl_status,
5620 entry->error_subcode1,
5621 entry->error_subcode2);
5624 ql_dbg(ql_dbg_tgt, vha, 0xe064,
5625 "qla_target(%d): Unexpected ABTS_RESP_24XX "
5626 "received\n", vha->vp_idx);
5631 ql_dbg(ql_dbg_tgt, vha, 0xe065,
5632 "qla_target(%d): Received unknown response pkt "
5633 "type %x\n", vha->vp_idx, pkt->entry_type);
5640 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
5642 void qlt_async_event(uint16_t code, struct scsi_qla_host *vha,
5645 struct qla_hw_data *ha = vha->hw;
5646 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
5649 if (!tgt || tgt->tgt_stop || tgt->tgt_stopped)
5652 if (((code == MBA_POINT_TO_POINT) || (code == MBA_CHG_IN_CONNECTION)) &&
5656 * In tgt_stop mode we also should allow all requests to pass.
5657 * Otherwise, some commands can stuck.
5662 case MBA_RESET: /* Reset */
5663 case MBA_SYSTEM_ERR: /* System Error */
5664 case MBA_REQ_TRANSFER_ERR: /* Request Transfer Error */
5665 case MBA_RSP_TRANSFER_ERR: /* Response Transfer Error */
5666 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03a,
5667 "qla_target(%d): System error async event %#x "
5668 "occurred", vha->vp_idx, code);
5670 case MBA_WAKEUP_THRES: /* Request Queue Wake-up. */
5671 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
5676 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03b,
5677 "qla_target(%d): Async LOOP_UP occurred "
5678 "(m[0]=%x, m[1]=%x, m[2]=%x, m[3]=%x)", vha->vp_idx,
5679 le16_to_cpu(mailbox[0]), le16_to_cpu(mailbox[1]),
5680 le16_to_cpu(mailbox[2]), le16_to_cpu(mailbox[3]));
5681 if (tgt->link_reinit_iocb_pending) {
5682 qlt_send_notify_ack(ha->base_qpair,
5683 (void *)&tgt->link_reinit_iocb,
5685 tgt->link_reinit_iocb_pending = 0;
5690 case MBA_LIP_OCCURRED:
5693 case MBA_RSCN_UPDATE:
5694 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03c,
5695 "qla_target(%d): Async event %#x occurred "
5696 "(m[0]=%x, m[1]=%x, m[2]=%x, m[3]=%x)", vha->vp_idx, code,
5697 le16_to_cpu(mailbox[0]), le16_to_cpu(mailbox[1]),
5698 le16_to_cpu(mailbox[2]), le16_to_cpu(mailbox[3]));
5701 case MBA_REJECTED_FCP_CMD:
5702 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf017,
5703 "qla_target(%d): Async event LS_REJECT occurred (m[0]=%x, m[1]=%x, m[2]=%x, m[3]=%x)",
5705 le16_to_cpu(mailbox[0]), le16_to_cpu(mailbox[1]),
5706 le16_to_cpu(mailbox[2]), le16_to_cpu(mailbox[3]));
5708 if (le16_to_cpu(mailbox[3]) == 1) {
5709 /* exchange starvation. */
5710 vha->hw->exch_starvation++;
5711 if (vha->hw->exch_starvation > 5) {
5712 ql_log(ql_log_warn, vha, 0xd03a,
5713 "Exchange starvation-. Resetting RISC\n");
5715 vha->hw->exch_starvation = 0;
5716 if (IS_P3P_TYPE(vha->hw))
5717 set_bit(FCOE_CTX_RESET_NEEDED,
5720 set_bit(ISP_ABORT_NEEDED,
5722 qla2xxx_wake_dpc(vha);
5727 case MBA_PORT_UPDATE:
5728 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03d,
5729 "qla_target(%d): Port update async event %#x "
5730 "occurred: updating the ports database (m[0]=%x, m[1]=%x, "
5731 "m[2]=%x, m[3]=%x)", vha->vp_idx, code,
5732 le16_to_cpu(mailbox[0]), le16_to_cpu(mailbox[1]),
5733 le16_to_cpu(mailbox[2]), le16_to_cpu(mailbox[3]));
5735 login_code = le16_to_cpu(mailbox[2]);
5736 if (login_code == 0x4) {
5737 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03e,
5738 "Async MB 2: Got PLOGI Complete\n");
5739 vha->hw->exch_starvation = 0;
5740 } else if (login_code == 0x7)
5741 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03f,
5742 "Async MB 2: Port Logged Out\n");
5750 static fc_port_t *qlt_get_port_database(struct scsi_qla_host *vha,
5753 fc_port_t *fcport, *tfcp, *del;
5755 unsigned long flags;
5758 fcport = kzalloc(sizeof(*fcport), GFP_KERNEL);
5760 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06f,
5761 "qla_target(%d): Allocation of tmp FC port failed",
5766 fcport->loop_id = loop_id;
5768 rc = qla24xx_gpdb_wait(vha, fcport, 0);
5769 if (rc != QLA_SUCCESS) {
5770 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf070,
5771 "qla_target(%d): Failed to retrieve fcport "
5772 "information -- get_port_database() returned %x "
5773 "(loop_id=0x%04x)", vha->vp_idx, rc, loop_id);
5779 spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
5780 tfcp = qla2x00_find_fcport_by_wwpn(vha, fcport->port_name, 1);
5783 tfcp->d_id = fcport->d_id;
5784 tfcp->port_type = fcport->port_type;
5785 tfcp->supported_classes = fcport->supported_classes;
5786 tfcp->flags |= fcport->flags;
5791 if (vha->hw->current_topology == ISP_CFG_F)
5792 fcport->flags |= FCF_FABRIC_DEVICE;
5794 list_add_tail(&fcport->list, &vha->vp_fcports);
5795 if (!IS_SW_RESV_ADDR(fcport->d_id))
5796 vha->fcport_count++;
5797 fcport->login_gen++;
5798 fcport->disc_state = DSC_LOGIN_COMPLETE;
5799 fcport->login_succ = 1;
5803 fcport->deleted = 0;
5804 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
5806 switch (vha->host->active_mode) {
5807 case MODE_INITIATOR:
5810 if (!IS_IIDMA_CAPABLE(vha->hw) || !vha->hw->flags.gpsc_supported) {
5811 ql_dbg(ql_dbg_disc, vha, 0x20fe,
5812 "%s %d %8phC post upd_fcport fcp_cnt %d\n",
5813 __func__, __LINE__, fcport->port_name, vha->fcport_count);
5814 qla24xx_post_upd_fcport_work(vha, fcport);
5816 ql_dbg(ql_dbg_disc, vha, 0x20ff,
5817 "%s %d %8phC post gpsc fcp_cnt %d\n",
5818 __func__, __LINE__, fcport->port_name, vha->fcport_count);
5819 qla24xx_post_gpsc_work(vha, fcport);
5829 qla2x00_free_fcport(del);
5834 /* Must be called under tgt_mutex */
5835 static struct fc_port *qlt_make_local_sess(struct scsi_qla_host *vha,
5838 struct fc_port *sess = NULL;
5839 fc_port_t *fcport = NULL;
5840 int rc, global_resets;
5841 uint16_t loop_id = 0;
5843 if ((s_id[0] == 0xFF) && (s_id[1] == 0xFC)) {
5845 * This is Domain Controller, so it should be
5846 * OK to drop SCSI commands from it.
5848 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf042,
5849 "Unable to find initiator with S_ID %x:%x:%x",
5850 s_id[0], s_id[1], s_id[2]);
5854 mutex_lock(&vha->vha_tgt.tgt_mutex);
5858 atomic_read(&vha->vha_tgt.qla_tgt->tgt_global_resets_count);
5860 rc = qla24xx_get_loop_id(vha, s_id, &loop_id);
5862 mutex_unlock(&vha->vha_tgt.tgt_mutex);
5864 ql_log(ql_log_info, vha, 0xf071,
5865 "qla_target(%d): Unable to find "
5866 "initiator with S_ID %x:%x:%x",
5867 vha->vp_idx, s_id[0], s_id[1],
5870 if (rc == -ENOENT) {
5871 qlt_port_logo_t logo;
5872 sid_to_portid(s_id, &logo.id);
5874 qlt_send_first_logo(vha, &logo);
5880 fcport = qlt_get_port_database(vha, loop_id);
5882 mutex_unlock(&vha->vha_tgt.tgt_mutex);
5886 if (global_resets !=
5887 atomic_read(&vha->vha_tgt.qla_tgt->tgt_global_resets_count)) {
5888 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf043,
5889 "qla_target(%d): global reset during session discovery "
5890 "(counter was %d, new %d), retrying", vha->vp_idx,
5892 atomic_read(&vha->vha_tgt.
5893 qla_tgt->tgt_global_resets_count));
5897 sess = qlt_create_sess(vha, fcport, true);
5899 mutex_unlock(&vha->vha_tgt.tgt_mutex);
5904 static void qlt_abort_work(struct qla_tgt *tgt,
5905 struct qla_tgt_sess_work_param *prm)
5907 struct scsi_qla_host *vha = tgt->vha;
5908 struct qla_hw_data *ha = vha->hw;
5909 struct fc_port *sess = NULL;
5910 unsigned long flags = 0, flags2 = 0;
5915 spin_lock_irqsave(&ha->tgt.sess_lock, flags2);
5920 s_id[0] = prm->abts.fcp_hdr_le.s_id[2];
5921 s_id[1] = prm->abts.fcp_hdr_le.s_id[1];
5922 s_id[2] = prm->abts.fcp_hdr_le.s_id[0];
5924 sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha,
5925 (unsigned char *)&be_s_id);
5927 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags2);
5929 sess = qlt_make_local_sess(vha, s_id);
5930 /* sess has got an extra creation ref */
5932 spin_lock_irqsave(&ha->tgt.sess_lock, flags2);
5936 if (sess->deleted) {
5941 if (!kref_get_unless_zero(&sess->sess_kref)) {
5942 ql_dbg(ql_dbg_tgt_tmr, vha, 0xf01c,
5943 "%s: kref_get fail %8phC \n",
5944 __func__, sess->port_name);
5950 rc = __qlt_24xx_handle_abts(vha, &prm->abts, sess);
5951 ha->tgt.tgt_ops->put_sess(sess);
5952 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags2);
5960 ha->tgt.tgt_ops->put_sess(sess);
5961 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags2);
5964 spin_lock_irqsave(&ha->hardware_lock, flags);
5965 qlt_24xx_send_abts_resp(ha->base_qpair, &prm->abts,
5966 FCP_TMF_REJECTED, false);
5967 spin_unlock_irqrestore(&ha->hardware_lock, flags);
5970 static void qlt_tmr_work(struct qla_tgt *tgt,
5971 struct qla_tgt_sess_work_param *prm)
5973 struct atio_from_isp *a = &prm->tm_iocb2;
5974 struct scsi_qla_host *vha = tgt->vha;
5975 struct qla_hw_data *ha = vha->hw;
5976 struct fc_port *sess = NULL;
5977 unsigned long flags;
5978 uint8_t *s_id = NULL; /* to hide compiler warnings */
5984 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
5989 s_id = prm->tm_iocb2.u.isp24.fcp_hdr.s_id;
5990 sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha, s_id);
5992 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
5994 sess = qlt_make_local_sess(vha, s_id);
5995 /* sess has got an extra creation ref */
5997 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
6001 if (sess->deleted) {
6006 if (!kref_get_unless_zero(&sess->sess_kref)) {
6007 ql_dbg(ql_dbg_tgt_tmr, vha, 0xf020,
6008 "%s: kref_get fail %8phC\n",
6009 __func__, sess->port_name);
6016 fn = a->u.isp24.fcp_cmnd.task_mgmt_flags;
6018 scsilun_to_int((struct scsi_lun *)&a->u.isp24.fcp_cmnd.lun);
6020 rc = qlt_issue_task_mgmt(sess, unpacked_lun, fn, iocb, 0);
6021 ha->tgt.tgt_ops->put_sess(sess);
6022 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
6030 ha->tgt.tgt_ops->put_sess(sess);
6031 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
6033 qlt_send_term_exchange(ha->base_qpair, NULL, &prm->tm_iocb2, 1, 0);
6036 static void qlt_sess_work_fn(struct work_struct *work)
6038 struct qla_tgt *tgt = container_of(work, struct qla_tgt, sess_work);
6039 struct scsi_qla_host *vha = tgt->vha;
6040 unsigned long flags;
6042 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf000, "Sess work (tgt %p)", tgt);
6044 spin_lock_irqsave(&tgt->sess_work_lock, flags);
6045 while (!list_empty(&tgt->sess_works_list)) {
6046 struct qla_tgt_sess_work_param *prm = list_entry(
6047 tgt->sess_works_list.next, typeof(*prm),
6048 sess_works_list_entry);
6051 * This work can be scheduled on several CPUs at time, so we
6052 * must delete the entry to eliminate double processing
6054 list_del(&prm->sess_works_list_entry);
6056 spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
6058 switch (prm->type) {
6059 case QLA_TGT_SESS_WORK_ABORT:
6060 qlt_abort_work(tgt, prm);
6062 case QLA_TGT_SESS_WORK_TM:
6063 qlt_tmr_work(tgt, prm);
6070 spin_lock_irqsave(&tgt->sess_work_lock, flags);
6074 spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
6077 /* Must be called under tgt_host_action_mutex */
6078 int qlt_add_target(struct qla_hw_data *ha, struct scsi_qla_host *base_vha)
6080 struct qla_tgt *tgt;
6082 struct qla_qpair_hint *h;
6084 if (!QLA_TGT_MODE_ENABLED())
6087 if (!IS_TGT_MODE_CAPABLE(ha)) {
6088 ql_log(ql_log_warn, base_vha, 0xe070,
6089 "This adapter does not support target mode.\n");
6093 ql_dbg(ql_dbg_tgt, base_vha, 0xe03b,
6094 "Registering target for host %ld(%p).\n", base_vha->host_no, ha);
6096 BUG_ON(base_vha->vha_tgt.qla_tgt != NULL);
6098 tgt = kzalloc(sizeof(struct qla_tgt), GFP_KERNEL);
6100 ql_dbg(ql_dbg_tgt, base_vha, 0xe066,
6101 "Unable to allocate struct qla_tgt\n");
6105 tgt->qphints = kzalloc((ha->max_qpairs + 1) *
6106 sizeof(struct qla_qpair_hint), GFP_KERNEL);
6107 if (!tgt->qphints) {
6109 ql_log(ql_log_warn, base_vha, 0x0197,
6110 "Unable to allocate qpair hints.\n");
6114 if (!(base_vha->host->hostt->supported_mode & MODE_TARGET))
6115 base_vha->host->hostt->supported_mode |= MODE_TARGET;
6117 rc = btree_init64(&tgt->lun_qpair_map);
6119 kfree(tgt->qphints);
6121 ql_log(ql_log_info, base_vha, 0x0198,
6122 "Unable to initialize lun_qpair_map btree\n");
6125 h = &tgt->qphints[0];
6126 h->qpair = ha->base_qpair;
6127 INIT_LIST_HEAD(&h->hint_elem);
6128 h->cpuid = ha->base_qpair->cpuid;
6129 list_add_tail(&h->hint_elem, &ha->base_qpair->hints_list);
6131 for (i = 0; i < ha->max_qpairs; i++) {
6132 unsigned long flags;
6134 struct qla_qpair *qpair = ha->queue_pair_map[i];
6135 h = &tgt->qphints[i + 1];
6136 INIT_LIST_HEAD(&h->hint_elem);
6139 spin_lock_irqsave(qpair->qp_lock_ptr, flags);
6140 list_add_tail(&h->hint_elem, &qpair->hints_list);
6141 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
6142 h->cpuid = qpair->cpuid;
6147 tgt->vha = base_vha;
6148 init_waitqueue_head(&tgt->waitQ);
6149 INIT_LIST_HEAD(&tgt->del_sess_list);
6150 spin_lock_init(&tgt->sess_work_lock);
6151 INIT_WORK(&tgt->sess_work, qlt_sess_work_fn);
6152 INIT_LIST_HEAD(&tgt->sess_works_list);
6153 atomic_set(&tgt->tgt_global_resets_count, 0);
6155 base_vha->vha_tgt.qla_tgt = tgt;
6157 ql_dbg(ql_dbg_tgt, base_vha, 0xe067,
6158 "qla_target(%d): using 64 Bit PCI addressing",
6161 tgt->sg_tablesize = QLA_TGT_MAX_SG_24XX(base_vha->req->length - 3);
6163 mutex_lock(&qla_tgt_mutex);
6164 list_add_tail(&tgt->tgt_list_entry, &qla_tgt_glist);
6165 mutex_unlock(&qla_tgt_mutex);
6167 if (ha->tgt.tgt_ops && ha->tgt.tgt_ops->add_target)
6168 ha->tgt.tgt_ops->add_target(base_vha);
6173 /* Must be called under tgt_host_action_mutex */
6174 int qlt_remove_target(struct qla_hw_data *ha, struct scsi_qla_host *vha)
6176 if (!vha->vha_tgt.qla_tgt)
6179 if (vha->fc_vport) {
6180 qlt_release(vha->vha_tgt.qla_tgt);
6184 /* free left over qfull cmds */
6185 qlt_init_term_exchange(vha);
6187 ql_dbg(ql_dbg_tgt, vha, 0xe03c, "Unregistering target for host %ld(%p)",
6189 qlt_release(vha->vha_tgt.qla_tgt);
6194 void qlt_remove_target_resources(struct qla_hw_data *ha)
6196 struct scsi_qla_host *node;
6199 btree_for_each_safe32(&ha->tgt.host_map, key, node)
6200 btree_remove32(&ha->tgt.host_map, key);
6202 btree_destroy32(&ha->tgt.host_map);
6205 static void qlt_lport_dump(struct scsi_qla_host *vha, u64 wwpn,
6210 pr_debug("qla2xxx HW vha->node_name: ");
6211 for (i = 0; i < WWN_SIZE; i++)
6212 pr_debug("%02x ", vha->node_name[i]);
6214 pr_debug("qla2xxx HW vha->port_name: ");
6215 for (i = 0; i < WWN_SIZE; i++)
6216 pr_debug("%02x ", vha->port_name[i]);
6219 pr_debug("qla2xxx passed configfs WWPN: ");
6220 put_unaligned_be64(wwpn, b);
6221 for (i = 0; i < WWN_SIZE; i++)
6222 pr_debug("%02x ", b[i]);
6227 * qla_tgt_lport_register - register lport with external module
6229 * @qla_tgt_ops: Pointer for tcm_qla2xxx qla_tgt_ops
6230 * @wwpn: Passwd FC target WWPN
6231 * @callback: lport initialization callback for tcm_qla2xxx code
6232 * @target_lport_ptr: pointer for tcm_qla2xxx specific lport data
6234 int qlt_lport_register(void *target_lport_ptr, u64 phys_wwpn,
6235 u64 npiv_wwpn, u64 npiv_wwnn,
6236 int (*callback)(struct scsi_qla_host *, void *, u64, u64))
6238 struct qla_tgt *tgt;
6239 struct scsi_qla_host *vha;
6240 struct qla_hw_data *ha;
6241 struct Scsi_Host *host;
6242 unsigned long flags;
6246 mutex_lock(&qla_tgt_mutex);
6247 list_for_each_entry(tgt, &qla_tgt_glist, tgt_list_entry) {
6255 if (!(host->hostt->supported_mode & MODE_TARGET))
6258 spin_lock_irqsave(&ha->hardware_lock, flags);
6259 if ((!npiv_wwpn || !npiv_wwnn) && host->active_mode & MODE_TARGET) {
6260 pr_debug("MODE_TARGET already active on qla2xxx(%d)\n",
6262 spin_unlock_irqrestore(&ha->hardware_lock, flags);
6265 if (tgt->tgt_stop) {
6266 pr_debug("MODE_TARGET in shutdown on qla2xxx(%d)\n",
6268 spin_unlock_irqrestore(&ha->hardware_lock, flags);
6271 spin_unlock_irqrestore(&ha->hardware_lock, flags);
6273 if (!scsi_host_get(host)) {
6274 ql_dbg(ql_dbg_tgt, vha, 0xe068,
6275 "Unable to scsi_host_get() for"
6276 " qla2xxx scsi_host\n");
6279 qlt_lport_dump(vha, phys_wwpn, b);
6281 if (memcmp(vha->port_name, b, WWN_SIZE)) {
6282 scsi_host_put(host);
6285 rc = (*callback)(vha, target_lport_ptr, npiv_wwpn, npiv_wwnn);
6287 scsi_host_put(host);
6289 mutex_unlock(&qla_tgt_mutex);
6292 mutex_unlock(&qla_tgt_mutex);
6296 EXPORT_SYMBOL(qlt_lport_register);
6299 * qla_tgt_lport_deregister - Degister lport
6301 * @vha: Registered scsi_qla_host pointer
6303 void qlt_lport_deregister(struct scsi_qla_host *vha)
6305 struct qla_hw_data *ha = vha->hw;
6306 struct Scsi_Host *sh = vha->host;
6308 * Clear the target_lport_ptr qla_target_template pointer in qla_hw_data
6310 vha->vha_tgt.target_lport_ptr = NULL;
6311 ha->tgt.tgt_ops = NULL;
6313 * Release the Scsi_Host reference for the underlying qla2xxx host
6317 EXPORT_SYMBOL(qlt_lport_deregister);
6319 /* Must be called under HW lock */
6320 static void qlt_set_mode(struct scsi_qla_host *vha)
6322 switch (ql2x_ini_mode) {
6323 case QLA2XXX_INI_MODE_DISABLED:
6324 case QLA2XXX_INI_MODE_EXCLUSIVE:
6325 vha->host->active_mode = MODE_TARGET;
6327 case QLA2XXX_INI_MODE_ENABLED:
6328 vha->host->active_mode = MODE_UNKNOWN;
6330 case QLA2XXX_INI_MODE_DUAL:
6331 vha->host->active_mode = MODE_DUAL;
6338 /* Must be called under HW lock */
6339 static void qlt_clear_mode(struct scsi_qla_host *vha)
6341 switch (ql2x_ini_mode) {
6342 case QLA2XXX_INI_MODE_DISABLED:
6343 vha->host->active_mode = MODE_UNKNOWN;
6345 case QLA2XXX_INI_MODE_EXCLUSIVE:
6346 vha->host->active_mode = MODE_INITIATOR;
6348 case QLA2XXX_INI_MODE_ENABLED:
6349 case QLA2XXX_INI_MODE_DUAL:
6350 vha->host->active_mode = MODE_INITIATOR;
6358 * qla_tgt_enable_vha - NO LOCK HELD
6360 * host_reset, bring up w/ Target Mode Enabled
6363 qlt_enable_vha(struct scsi_qla_host *vha)
6365 struct qla_hw_data *ha = vha->hw;
6366 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
6367 unsigned long flags;
6368 scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
6371 ql_dbg(ql_dbg_tgt, vha, 0xe069,
6372 "Unable to locate qla_tgt pointer from"
6373 " struct qla_hw_data\n");
6378 spin_lock_irqsave(&ha->hardware_lock, flags);
6379 tgt->tgt_stopped = 0;
6381 spin_unlock_irqrestore(&ha->hardware_lock, flags);
6384 qla24xx_disable_vp(vha);
6385 qla24xx_enable_vp(vha);
6387 set_bit(ISP_ABORT_NEEDED, &base_vha->dpc_flags);
6388 qla2xxx_wake_dpc(base_vha);
6389 qla2x00_wait_for_hba_online(base_vha);
6392 EXPORT_SYMBOL(qlt_enable_vha);
6395 * qla_tgt_disable_vha - NO LOCK HELD
6397 * Disable Target Mode and reset the adapter
6399 static void qlt_disable_vha(struct scsi_qla_host *vha)
6401 struct qla_hw_data *ha = vha->hw;
6402 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
6403 unsigned long flags;
6406 ql_dbg(ql_dbg_tgt, vha, 0xe06a,
6407 "Unable to locate qla_tgt pointer from"
6408 " struct qla_hw_data\n");
6413 spin_lock_irqsave(&ha->hardware_lock, flags);
6414 qlt_clear_mode(vha);
6415 spin_unlock_irqrestore(&ha->hardware_lock, flags);
6417 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
6418 qla2xxx_wake_dpc(vha);
6419 qla2x00_wait_for_hba_online(vha);
6423 * Called from qla_init.c:qla24xx_vport_create() contex to setup
6424 * the target mode specific struct scsi_qla_host and struct qla_hw_data
6428 qlt_vport_create(struct scsi_qla_host *vha, struct qla_hw_data *ha)
6430 vha->vha_tgt.qla_tgt = NULL;
6432 mutex_init(&vha->vha_tgt.tgt_mutex);
6433 mutex_init(&vha->vha_tgt.tgt_host_action_mutex);
6435 qlt_clear_mode(vha);
6438 * NOTE: Currently the value is kept the same for <24xx and
6439 * >=24xx ISPs. If it is necessary to change it,
6440 * the check should be added for specific ISPs,
6441 * assigning the value appropriately.
6443 ha->tgt.atio_q_length = ATIO_ENTRY_CNT_24XX;
6445 qlt_add_target(ha, vha);
6449 qlt_rff_id(struct scsi_qla_host *vha, struct ct_sns_req *ct_req)
6452 * FC-4 Feature bit 0 indicates target functionality to the name server.
6454 if (qla_tgt_mode_enabled(vha)) {
6455 ct_req->req.rff_id.fc4_feature = BIT_0;
6456 } else if (qla_ini_mode_enabled(vha)) {
6457 ct_req->req.rff_id.fc4_feature = BIT_1;
6458 } else if (qla_dual_mode_enabled(vha))
6459 ct_req->req.rff_id.fc4_feature = BIT_0 | BIT_1;
6463 * qlt_init_atio_q_entries() - Initializes ATIO queue entries.
6466 * Beginning of ATIO ring has initialization control block already built
6467 * by nvram config routine.
6469 * Returns 0 on success.
6472 qlt_init_atio_q_entries(struct scsi_qla_host *vha)
6474 struct qla_hw_data *ha = vha->hw;
6476 struct atio_from_isp *pkt = (struct atio_from_isp *)ha->tgt.atio_ring;
6478 if (qla_ini_mode_enabled(vha))
6481 for (cnt = 0; cnt < ha->tgt.atio_q_length; cnt++) {
6482 pkt->u.raw.signature = ATIO_PROCESSED;
6489 * qlt_24xx_process_atio_queue() - Process ATIO queue entries.
6490 * @ha: SCSI driver HA context
6493 qlt_24xx_process_atio_queue(struct scsi_qla_host *vha, uint8_t ha_locked)
6495 struct qla_hw_data *ha = vha->hw;
6496 struct atio_from_isp *pkt;
6499 if (!ha->flags.fw_started)
6502 while ((ha->tgt.atio_ring_ptr->signature != ATIO_PROCESSED) ||
6503 fcpcmd_is_corrupted(ha->tgt.atio_ring_ptr)) {
6504 pkt = (struct atio_from_isp *)ha->tgt.atio_ring_ptr;
6505 cnt = pkt->u.raw.entry_count;
6507 if (unlikely(fcpcmd_is_corrupted(ha->tgt.atio_ring_ptr))) {
6509 * This packet is corrupted. The header + payload
6510 * can not be trusted. There is no point in passing
6513 ql_log(ql_log_warn, vha, 0xd03c,
6514 "corrupted fcp frame SID[%3phN] OXID[%04x] EXCG[%x] %64phN\n",
6515 pkt->u.isp24.fcp_hdr.s_id,
6516 be16_to_cpu(pkt->u.isp24.fcp_hdr.ox_id),
6517 le32_to_cpu(pkt->u.isp24.exchange_addr), pkt);
6519 adjust_corrupted_atio(pkt);
6520 qlt_send_term_exchange(ha->base_qpair, NULL, pkt,
6523 qlt_24xx_atio_pkt_all_vps(vha,
6524 (struct atio_from_isp *)pkt, ha_locked);
6527 for (i = 0; i < cnt; i++) {
6528 ha->tgt.atio_ring_index++;
6529 if (ha->tgt.atio_ring_index == ha->tgt.atio_q_length) {
6530 ha->tgt.atio_ring_index = 0;
6531 ha->tgt.atio_ring_ptr = ha->tgt.atio_ring;
6533 ha->tgt.atio_ring_ptr++;
6535 pkt->u.raw.signature = ATIO_PROCESSED;
6536 pkt = (struct atio_from_isp *)ha->tgt.atio_ring_ptr;
6541 /* Adjust ring index */
6542 WRT_REG_DWORD(ISP_ATIO_Q_OUT(vha), ha->tgt.atio_ring_index);
6546 qlt_24xx_config_rings(struct scsi_qla_host *vha)
6548 struct qla_hw_data *ha = vha->hw;
6549 if (!QLA_TGT_MODE_ENABLED())
6552 WRT_REG_DWORD(ISP_ATIO_Q_IN(vha), 0);
6553 WRT_REG_DWORD(ISP_ATIO_Q_OUT(vha), 0);
6554 RD_REG_DWORD(ISP_ATIO_Q_OUT(vha));
6556 if (IS_ATIO_MSIX_CAPABLE(ha)) {
6557 struct qla_msix_entry *msix = &ha->msix_entries[2];
6558 struct init_cb_24xx *icb = (struct init_cb_24xx *)ha->init_cb;
6560 icb->msix_atio = cpu_to_le16(msix->entry);
6561 ql_dbg(ql_dbg_init, vha, 0xf072,
6562 "Registering ICB vector 0x%x for atio que.\n",
6568 qlt_24xx_config_nvram_stage1(struct scsi_qla_host *vha, struct nvram_24xx *nv)
6570 struct qla_hw_data *ha = vha->hw;
6572 if (!QLA_TGT_MODE_ENABLED())
6575 if (qla_tgt_mode_enabled(vha) || qla_dual_mode_enabled(vha)) {
6576 if (!ha->tgt.saved_set) {
6577 /* We save only once */
6578 ha->tgt.saved_exchange_count = nv->exchange_count;
6579 ha->tgt.saved_firmware_options_1 =
6580 nv->firmware_options_1;
6581 ha->tgt.saved_firmware_options_2 =
6582 nv->firmware_options_2;
6583 ha->tgt.saved_firmware_options_3 =
6584 nv->firmware_options_3;
6585 ha->tgt.saved_set = 1;
6588 if (qla_tgt_mode_enabled(vha))
6589 nv->exchange_count = cpu_to_le16(0xFFFF);
6591 nv->exchange_count = cpu_to_le16(ql2xexchoffld);
6593 /* Enable target mode */
6594 nv->firmware_options_1 |= cpu_to_le32(BIT_4);
6596 /* Disable ini mode, if requested */
6597 if (qla_tgt_mode_enabled(vha))
6598 nv->firmware_options_1 |= cpu_to_le32(BIT_5);
6600 /* Disable Full Login after LIP */
6601 nv->firmware_options_1 &= cpu_to_le32(~BIT_13);
6602 /* Enable initial LIP */
6603 nv->firmware_options_1 &= cpu_to_le32(~BIT_9);
6604 if (ql2xtgt_tape_enable)
6605 /* Enable FC Tape support */
6606 nv->firmware_options_2 |= cpu_to_le32(BIT_12);
6608 /* Disable FC Tape support */
6609 nv->firmware_options_2 &= cpu_to_le32(~BIT_12);
6611 /* Disable Full Login after LIP */
6612 nv->host_p &= cpu_to_le32(~BIT_10);
6615 * clear BIT 15 explicitly as we have seen at least
6616 * a couple of instances where this was set and this
6617 * was causing the firmware to not be initialized.
6619 nv->firmware_options_1 &= cpu_to_le32(~BIT_15);
6620 /* Enable target PRLI control */
6621 nv->firmware_options_2 |= cpu_to_le32(BIT_14);
6623 if (ha->tgt.saved_set) {
6624 nv->exchange_count = ha->tgt.saved_exchange_count;
6625 nv->firmware_options_1 =
6626 ha->tgt.saved_firmware_options_1;
6627 nv->firmware_options_2 =
6628 ha->tgt.saved_firmware_options_2;
6629 nv->firmware_options_3 =
6630 ha->tgt.saved_firmware_options_3;
6635 if (ha->base_qpair->enable_class_2) {
6636 if (vha->flags.init_done)
6637 fc_host_supported_classes(vha->host) =
6638 FC_COS_CLASS2 | FC_COS_CLASS3;
6640 nv->firmware_options_2 |= cpu_to_le32(BIT_8);
6642 if (vha->flags.init_done)
6643 fc_host_supported_classes(vha->host) = FC_COS_CLASS3;
6645 nv->firmware_options_2 &= ~cpu_to_le32(BIT_8);
6650 qlt_24xx_config_nvram_stage2(struct scsi_qla_host *vha,
6651 struct init_cb_24xx *icb)
6653 struct qla_hw_data *ha = vha->hw;
6655 if (!QLA_TGT_MODE_ENABLED())
6658 if (ha->tgt.node_name_set) {
6659 memcpy(icb->node_name, ha->tgt.tgt_node_name, WWN_SIZE);
6660 icb->firmware_options_1 |= cpu_to_le32(BIT_14);
6663 /* disable ZIO at start time. */
6664 if (!vha->flags.init_done) {
6666 tmp = le32_to_cpu(icb->firmware_options_2);
6667 tmp &= ~(BIT_3 | BIT_2 | BIT_1 | BIT_0);
6668 icb->firmware_options_2 = cpu_to_le32(tmp);
6673 qlt_81xx_config_nvram_stage1(struct scsi_qla_host *vha, struct nvram_81xx *nv)
6675 struct qla_hw_data *ha = vha->hw;
6677 if (!QLA_TGT_MODE_ENABLED())
6680 if (qla_tgt_mode_enabled(vha) || qla_dual_mode_enabled(vha)) {
6681 if (!ha->tgt.saved_set) {
6682 /* We save only once */
6683 ha->tgt.saved_exchange_count = nv->exchange_count;
6684 ha->tgt.saved_firmware_options_1 =
6685 nv->firmware_options_1;
6686 ha->tgt.saved_firmware_options_2 =
6687 nv->firmware_options_2;
6688 ha->tgt.saved_firmware_options_3 =
6689 nv->firmware_options_3;
6690 ha->tgt.saved_set = 1;
6693 if (qla_tgt_mode_enabled(vha))
6694 nv->exchange_count = cpu_to_le16(0xFFFF);
6696 nv->exchange_count = cpu_to_le16(ql2xexchoffld);
6698 /* Enable target mode */
6699 nv->firmware_options_1 |= cpu_to_le32(BIT_4);
6701 /* Disable ini mode, if requested */
6702 if (qla_tgt_mode_enabled(vha))
6703 nv->firmware_options_1 |= cpu_to_le32(BIT_5);
6704 /* Disable Full Login after LIP */
6705 nv->firmware_options_1 &= cpu_to_le32(~BIT_13);
6706 /* Enable initial LIP */
6707 nv->firmware_options_1 &= cpu_to_le32(~BIT_9);
6709 * clear BIT 15 explicitly as we have seen at
6710 * least a couple of instances where this was set
6711 * and this was causing the firmware to not be
6714 nv->firmware_options_1 &= cpu_to_le32(~BIT_15);
6715 if (ql2xtgt_tape_enable)
6716 /* Enable FC tape support */
6717 nv->firmware_options_2 |= cpu_to_le32(BIT_12);
6719 /* Disable FC tape support */
6720 nv->firmware_options_2 &= cpu_to_le32(~BIT_12);
6722 /* Disable Full Login after LIP */
6723 nv->host_p &= cpu_to_le32(~BIT_10);
6724 /* Enable target PRLI control */
6725 nv->firmware_options_2 |= cpu_to_le32(BIT_14);
6727 if (ha->tgt.saved_set) {
6728 nv->exchange_count = ha->tgt.saved_exchange_count;
6729 nv->firmware_options_1 =
6730 ha->tgt.saved_firmware_options_1;
6731 nv->firmware_options_2 =
6732 ha->tgt.saved_firmware_options_2;
6733 nv->firmware_options_3 =
6734 ha->tgt.saved_firmware_options_3;
6739 if (ha->base_qpair->enable_class_2) {
6740 if (vha->flags.init_done)
6741 fc_host_supported_classes(vha->host) =
6742 FC_COS_CLASS2 | FC_COS_CLASS3;
6744 nv->firmware_options_2 |= cpu_to_le32(BIT_8);
6746 if (vha->flags.init_done)
6747 fc_host_supported_classes(vha->host) = FC_COS_CLASS3;
6749 nv->firmware_options_2 &= ~cpu_to_le32(BIT_8);
6754 qlt_81xx_config_nvram_stage2(struct scsi_qla_host *vha,
6755 struct init_cb_81xx *icb)
6757 struct qla_hw_data *ha = vha->hw;
6759 if (!QLA_TGT_MODE_ENABLED())
6762 if (ha->tgt.node_name_set) {
6763 memcpy(icb->node_name, ha->tgt.tgt_node_name, WWN_SIZE);
6764 icb->firmware_options_1 |= cpu_to_le32(BIT_14);
6767 /* disable ZIO at start time. */
6768 if (!vha->flags.init_done) {
6770 tmp = le32_to_cpu(icb->firmware_options_2);
6771 tmp &= ~(BIT_3 | BIT_2 | BIT_1 | BIT_0);
6772 icb->firmware_options_2 = cpu_to_le32(tmp);
6778 qlt_83xx_iospace_config(struct qla_hw_data *ha)
6780 if (!QLA_TGT_MODE_ENABLED())
6783 ha->msix_count += 1; /* For ATIO Q */
6788 qlt_modify_vp_config(struct scsi_qla_host *vha,
6789 struct vp_config_entry_24xx *vpmod)
6791 /* enable target mode. Bit5 = 1 => disable */
6792 if (qla_tgt_mode_enabled(vha) || qla_dual_mode_enabled(vha))
6793 vpmod->options_idx1 &= ~BIT_5;
6795 /* Disable ini mode, if requested. bit4 = 1 => disable */
6796 if (qla_tgt_mode_enabled(vha))
6797 vpmod->options_idx1 &= ~BIT_4;
6801 qlt_probe_one_stage1(struct scsi_qla_host *base_vha, struct qla_hw_data *ha)
6805 if (!QLA_TGT_MODE_ENABLED())
6808 if (IS_QLA83XX(ha) || IS_QLA27XX(ha)) {
6809 ISP_ATIO_Q_IN(base_vha) = &ha->mqiobase->isp25mq.atio_q_in;
6810 ISP_ATIO_Q_OUT(base_vha) = &ha->mqiobase->isp25mq.atio_q_out;
6812 ISP_ATIO_Q_IN(base_vha) = &ha->iobase->isp24.atio_q_in;
6813 ISP_ATIO_Q_OUT(base_vha) = &ha->iobase->isp24.atio_q_out;
6816 mutex_init(&base_vha->vha_tgt.tgt_mutex);
6817 mutex_init(&base_vha->vha_tgt.tgt_host_action_mutex);
6819 INIT_LIST_HEAD(&base_vha->unknown_atio_list);
6820 INIT_DELAYED_WORK(&base_vha->unknown_atio_work,
6821 qlt_unknown_atio_work_fn);
6823 qlt_clear_mode(base_vha);
6825 rc = btree_init32(&ha->tgt.host_map);
6827 ql_log(ql_log_info, base_vha, 0xd03d,
6828 "Unable to initialize ha->host_map btree\n");
6830 qlt_update_vp_map(base_vha, SET_VP_IDX);
6834 qla83xx_msix_atio_q(int irq, void *dev_id)
6836 struct rsp_que *rsp;
6837 scsi_qla_host_t *vha;
6838 struct qla_hw_data *ha;
6839 unsigned long flags;
6841 rsp = (struct rsp_que *) dev_id;
6843 vha = pci_get_drvdata(ha->pdev);
6845 spin_lock_irqsave(&ha->tgt.atio_lock, flags);
6847 qlt_24xx_process_atio_queue(vha, 0);
6849 spin_unlock_irqrestore(&ha->tgt.atio_lock, flags);
6855 qlt_handle_abts_recv_work(struct work_struct *work)
6857 struct qla_tgt_sess_op *op = container_of(work,
6858 struct qla_tgt_sess_op, work);
6859 scsi_qla_host_t *vha = op->vha;
6860 struct qla_hw_data *ha = vha->hw;
6861 unsigned long flags;
6863 if (qla2x00_reset_active(vha) ||
6864 (op->chip_reset != ha->base_qpair->chip_reset))
6867 spin_lock_irqsave(&ha->tgt.atio_lock, flags);
6868 qlt_24xx_process_atio_queue(vha, 0);
6869 spin_unlock_irqrestore(&ha->tgt.atio_lock, flags);
6871 spin_lock_irqsave(&ha->hardware_lock, flags);
6872 qlt_response_pkt_all_vps(vha, op->rsp, (response_t *)&op->atio);
6873 spin_unlock_irqrestore(&ha->hardware_lock, flags);
6879 qlt_handle_abts_recv(struct scsi_qla_host *vha, struct rsp_que *rsp,
6882 struct qla_tgt_sess_op *op;
6884 op = kzalloc(sizeof(*op), GFP_ATOMIC);
6887 /* do not reach for ATIO queue here. This is best effort err
6888 * recovery at this point.
6890 qlt_response_pkt_all_vps(vha, rsp, pkt);
6894 memcpy(&op->atio, pkt, sizeof(*pkt));
6896 op->chip_reset = vha->hw->base_qpair->chip_reset;
6898 INIT_WORK(&op->work, qlt_handle_abts_recv_work);
6899 queue_work(qla_tgt_wq, &op->work);
6904 qlt_mem_alloc(struct qla_hw_data *ha)
6906 if (!QLA_TGT_MODE_ENABLED())
6909 ha->tgt.tgt_vp_map = kzalloc(sizeof(struct qla_tgt_vp_map) *
6910 MAX_MULTI_ID_FABRIC, GFP_KERNEL);
6911 if (!ha->tgt.tgt_vp_map)
6914 ha->tgt.atio_ring = dma_alloc_coherent(&ha->pdev->dev,
6915 (ha->tgt.atio_q_length + 1) * sizeof(struct atio_from_isp),
6916 &ha->tgt.atio_dma, GFP_KERNEL);
6917 if (!ha->tgt.atio_ring) {
6918 kfree(ha->tgt.tgt_vp_map);
6925 qlt_mem_free(struct qla_hw_data *ha)
6927 if (!QLA_TGT_MODE_ENABLED())
6930 if (ha->tgt.atio_ring) {
6931 dma_free_coherent(&ha->pdev->dev, (ha->tgt.atio_q_length + 1) *
6932 sizeof(struct atio_from_isp), ha->tgt.atio_ring,
6935 kfree(ha->tgt.tgt_vp_map);
6938 /* vport_slock to be held by the caller */
6940 qlt_update_vp_map(struct scsi_qla_host *vha, int cmd)
6946 if (!QLA_TGT_MODE_ENABLED())
6949 key = vha->d_id.b24;
6953 vha->hw->tgt.tgt_vp_map[vha->vp_idx].vha = vha;
6956 slot = btree_lookup32(&vha->hw->tgt.host_map, key);
6958 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf018,
6959 "Save vha in host_map %p %06x\n", vha, key);
6960 rc = btree_insert32(&vha->hw->tgt.host_map,
6961 key, vha, GFP_ATOMIC);
6963 ql_log(ql_log_info, vha, 0xd03e,
6964 "Unable to insert s_id into host_map: %06x\n",
6968 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf019,
6969 "replace existing vha in host_map %p %06x\n", vha, key);
6970 btree_update32(&vha->hw->tgt.host_map, key, vha);
6973 vha->hw->tgt.tgt_vp_map[vha->vp_idx].vha = NULL;
6976 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01a,
6977 "clear vha in host_map %p %06x\n", vha, key);
6978 slot = btree_lookup32(&vha->hw->tgt.host_map, key);
6980 btree_remove32(&vha->hw->tgt.host_map, key);
6986 void qlt_update_host_map(struct scsi_qla_host *vha, port_id_t id)
6988 unsigned long flags;
6989 struct qla_hw_data *ha = vha->hw;
6991 if (!vha->d_id.b24) {
6992 spin_lock_irqsave(&ha->vport_slock, flags);
6994 qlt_update_vp_map(vha, SET_AL_PA);
6995 spin_unlock_irqrestore(&ha->vport_slock, flags);
6996 } else if (vha->d_id.b24 != id.b24) {
6997 spin_lock_irqsave(&ha->vport_slock, flags);
6998 qlt_update_vp_map(vha, RESET_AL_PA);
7000 qlt_update_vp_map(vha, SET_AL_PA);
7001 spin_unlock_irqrestore(&ha->vport_slock, flags);
7005 static int __init qlt_parse_ini_mode(void)
7007 if (strcasecmp(qlini_mode, QLA2XXX_INI_MODE_STR_EXCLUSIVE) == 0)
7008 ql2x_ini_mode = QLA2XXX_INI_MODE_EXCLUSIVE;
7009 else if (strcasecmp(qlini_mode, QLA2XXX_INI_MODE_STR_DISABLED) == 0)
7010 ql2x_ini_mode = QLA2XXX_INI_MODE_DISABLED;
7011 else if (strcasecmp(qlini_mode, QLA2XXX_INI_MODE_STR_ENABLED) == 0)
7012 ql2x_ini_mode = QLA2XXX_INI_MODE_ENABLED;
7013 else if (strcasecmp(qlini_mode, QLA2XXX_INI_MODE_STR_DUAL) == 0)
7014 ql2x_ini_mode = QLA2XXX_INI_MODE_DUAL;
7021 int __init qlt_init(void)
7025 if (!qlt_parse_ini_mode()) {
7026 ql_log(ql_log_fatal, NULL, 0xe06b,
7027 "qlt_parse_ini_mode() failed\n");
7031 if (!QLA_TGT_MODE_ENABLED())
7034 qla_tgt_mgmt_cmd_cachep = kmem_cache_create("qla_tgt_mgmt_cmd_cachep",
7035 sizeof(struct qla_tgt_mgmt_cmd), __alignof__(struct
7036 qla_tgt_mgmt_cmd), 0, NULL);
7037 if (!qla_tgt_mgmt_cmd_cachep) {
7038 ql_log(ql_log_fatal, NULL, 0xd04b,
7039 "kmem_cache_create for qla_tgt_mgmt_cmd_cachep failed\n");
7043 qla_tgt_plogi_cachep = kmem_cache_create("qla_tgt_plogi_cachep",
7044 sizeof(struct qlt_plogi_ack_t), __alignof__(struct qlt_plogi_ack_t),
7047 if (!qla_tgt_plogi_cachep) {
7048 ql_log(ql_log_fatal, NULL, 0xe06d,
7049 "kmem_cache_create for qla_tgt_plogi_cachep failed\n");
7051 goto out_mgmt_cmd_cachep;
7054 qla_tgt_mgmt_cmd_mempool = mempool_create(25, mempool_alloc_slab,
7055 mempool_free_slab, qla_tgt_mgmt_cmd_cachep);
7056 if (!qla_tgt_mgmt_cmd_mempool) {
7057 ql_log(ql_log_fatal, NULL, 0xe06e,
7058 "mempool_create for qla_tgt_mgmt_cmd_mempool failed\n");
7060 goto out_plogi_cachep;
7063 qla_tgt_wq = alloc_workqueue("qla_tgt_wq", 0, 0);
7065 ql_log(ql_log_fatal, NULL, 0xe06f,
7066 "alloc_workqueue for qla_tgt_wq failed\n");
7068 goto out_cmd_mempool;
7071 * Return 1 to signal that initiator-mode is being disabled
7073 return (ql2x_ini_mode == QLA2XXX_INI_MODE_DISABLED) ? 1 : 0;
7076 mempool_destroy(qla_tgt_mgmt_cmd_mempool);
7078 kmem_cache_destroy(qla_tgt_plogi_cachep);
7079 out_mgmt_cmd_cachep:
7080 kmem_cache_destroy(qla_tgt_mgmt_cmd_cachep);
7086 if (!QLA_TGT_MODE_ENABLED())
7089 destroy_workqueue(qla_tgt_wq);
7090 mempool_destroy(qla_tgt_mgmt_cmd_mempool);
7091 kmem_cache_destroy(qla_tgt_plogi_cachep);
7092 kmem_cache_destroy(qla_tgt_mgmt_cmd_cachep);