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 qla_sam_status = SAM_STAT_BUSY;
79 static int tc_sam_status = SAM_STAT_TASK_SET_FULL; /* target core */
82 * From scsi/fc/fc_fcp.h
84 enum fcp_resp_rsp_codes {
86 FCP_DATA_LEN_INVALID = 1,
87 FCP_CMND_FIELDS_INVALID = 2,
88 FCP_DATA_PARAM_MISMATCH = 3,
91 FCP_TMF_INVALID_LUN = 9,
95 * fc_pri_ta from scsi/fc/fc_fcp.h
97 #define FCP_PTA_SIMPLE 0 /* simple task attribute */
98 #define FCP_PTA_HEADQ 1 /* head of queue task attribute */
99 #define FCP_PTA_ORDERED 2 /* ordered task attribute */
100 #define FCP_PTA_ACA 4 /* auto. contingent allegiance */
101 #define FCP_PTA_MASK 7 /* mask for task attribute field */
102 #define FCP_PRI_SHIFT 3 /* priority field starts in bit 3 */
103 #define FCP_PRI_RESVD_MASK 0x80 /* reserved bits in priority field */
106 * This driver calls qla2x00_alloc_iocbs() and qla2x00_issue_marker(), which
107 * must be called under HW lock and could unlock/lock it inside.
108 * It isn't an issue, since in the current implementation on the time when
109 * those functions are called:
111 * - Either context is IRQ and only IRQ handler can modify HW data,
112 * including rings related fields,
114 * - Or access to target mode variables from struct qla_tgt doesn't
115 * cross those functions boundaries, except tgt_stop, which
116 * additionally protected by irq_cmd_count.
118 /* Predefs for callbacks handed to qla2xxx LLD */
119 static void qlt_24xx_atio_pkt(struct scsi_qla_host *ha,
120 struct atio_from_isp *pkt, uint8_t);
121 static void qlt_response_pkt(struct scsi_qla_host *ha, struct rsp_que *rsp,
123 static int qlt_issue_task_mgmt(struct fc_port *sess, u64 lun,
124 int fn, void *iocb, int flags);
125 static void qlt_send_term_exchange(struct qla_qpair *, struct qla_tgt_cmd
126 *cmd, struct atio_from_isp *atio, int ha_locked, int ul_abort);
127 static void qlt_alloc_qfull_cmd(struct scsi_qla_host *vha,
128 struct atio_from_isp *atio, uint16_t status, int qfull);
129 static void qlt_disable_vha(struct scsi_qla_host *vha);
130 static void qlt_clear_tgt_db(struct qla_tgt *tgt);
131 static void qlt_send_notify_ack(struct qla_qpair *qpair,
132 struct imm_ntfy_from_isp *ntfy,
133 uint32_t add_flags, uint16_t resp_code, int resp_code_valid,
134 uint16_t srr_flags, uint16_t srr_reject_code, uint8_t srr_explan);
135 static void qlt_send_term_imm_notif(struct scsi_qla_host *vha,
136 struct imm_ntfy_from_isp *imm, int ha_locked);
137 static struct fc_port *qlt_create_sess(struct scsi_qla_host *vha,
138 fc_port_t *fcport, bool local);
139 void qlt_unreg_sess(struct fc_port *sess);
140 static void qlt_24xx_handle_abts(struct scsi_qla_host *,
141 struct abts_recv_from_24xx *);
142 static void qlt_send_busy(struct qla_qpair *, struct atio_from_isp *,
148 static struct kmem_cache *qla_tgt_mgmt_cmd_cachep;
149 struct kmem_cache *qla_tgt_plogi_cachep;
150 static mempool_t *qla_tgt_mgmt_cmd_mempool;
151 static struct workqueue_struct *qla_tgt_wq;
152 static DEFINE_MUTEX(qla_tgt_mutex);
153 static LIST_HEAD(qla_tgt_glist);
155 static const char *prot_op_str(u32 prot_op)
158 case TARGET_PROT_NORMAL: return "NORMAL";
159 case TARGET_PROT_DIN_INSERT: return "DIN_INSERT";
160 case TARGET_PROT_DOUT_INSERT: return "DOUT_INSERT";
161 case TARGET_PROT_DIN_STRIP: return "DIN_STRIP";
162 case TARGET_PROT_DOUT_STRIP: return "DOUT_STRIP";
163 case TARGET_PROT_DIN_PASS: return "DIN_PASS";
164 case TARGET_PROT_DOUT_PASS: return "DOUT_PASS";
165 default: return "UNKNOWN";
169 /* This API intentionally takes dest as a parameter, rather than returning
170 * int value to avoid caller forgetting to issue wmb() after the store */
171 void qlt_do_generation_tick(struct scsi_qla_host *vha, int *dest)
173 scsi_qla_host_t *base_vha = pci_get_drvdata(vha->hw->pdev);
174 *dest = atomic_inc_return(&base_vha->generation_tick);
179 /* Might release hw lock, then reaquire!! */
180 static inline int qlt_issue_marker(struct scsi_qla_host *vha, int vha_locked)
182 /* Send marker if required */
183 if (unlikely(vha->marker_needed != 0)) {
184 int rc = qla2x00_issue_marker(vha, vha_locked);
185 if (rc != QLA_SUCCESS) {
186 ql_dbg(ql_dbg_tgt, vha, 0xe03d,
187 "qla_target(%d): issue_marker() failed\n",
196 struct scsi_qla_host *qlt_find_host_by_d_id(struct scsi_qla_host *vha,
199 struct scsi_qla_host *host;
202 if ((vha->d_id.b.area == d_id[1]) && (vha->d_id.b.domain == d_id[0]) &&
203 (vha->d_id.b.al_pa == d_id[2]))
206 key = (uint32_t)d_id[0] << 16;
207 key |= (uint32_t)d_id[1] << 8;
208 key |= (uint32_t)d_id[2];
210 host = btree_lookup32(&vha->hw->tgt.host_map, key);
212 ql_dbg(ql_dbg_tgt_mgt + ql_dbg_verbose, vha, 0xf005,
213 "Unable to find host %06x\n", key);
219 struct scsi_qla_host *qlt_find_host_by_vp_idx(struct scsi_qla_host *vha,
222 struct qla_hw_data *ha = vha->hw;
224 if (vha->vp_idx == vp_idx)
227 BUG_ON(ha->tgt.tgt_vp_map == NULL);
228 if (likely(test_bit(vp_idx, ha->vp_idx_map)))
229 return ha->tgt.tgt_vp_map[vp_idx].vha;
234 static inline void qlt_incr_num_pend_cmds(struct scsi_qla_host *vha)
238 spin_lock_irqsave(&vha->hw->tgt.q_full_lock, flags);
240 vha->hw->tgt.num_pend_cmds++;
241 if (vha->hw->tgt.num_pend_cmds > vha->qla_stats.stat_max_pend_cmds)
242 vha->qla_stats.stat_max_pend_cmds =
243 vha->hw->tgt.num_pend_cmds;
244 spin_unlock_irqrestore(&vha->hw->tgt.q_full_lock, flags);
246 static inline void qlt_decr_num_pend_cmds(struct scsi_qla_host *vha)
250 spin_lock_irqsave(&vha->hw->tgt.q_full_lock, flags);
251 vha->hw->tgt.num_pend_cmds--;
252 spin_unlock_irqrestore(&vha->hw->tgt.q_full_lock, flags);
256 static void qlt_queue_unknown_atio(scsi_qla_host_t *vha,
257 struct atio_from_isp *atio, uint8_t ha_locked)
259 struct qla_tgt_sess_op *u;
260 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
264 ql_dbg(ql_dbg_async, vha, 0x502c,
265 "qla_target(%d): dropping unknown ATIO_TYPE7, because tgt is being stopped",
270 u = kzalloc(sizeof(*u), GFP_ATOMIC);
275 memcpy(&u->atio, atio, sizeof(*atio));
276 INIT_LIST_HEAD(&u->cmd_list);
278 spin_lock_irqsave(&vha->cmd_list_lock, flags);
279 list_add_tail(&u->cmd_list, &vha->unknown_atio_list);
280 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
282 schedule_delayed_work(&vha->unknown_atio_work, 1);
288 qlt_send_term_exchange(vha->hw->base_qpair, NULL, atio, ha_locked, 0);
292 static void qlt_try_to_dequeue_unknown_atios(struct scsi_qla_host *vha,
295 struct qla_tgt_sess_op *u, *t;
296 scsi_qla_host_t *host;
297 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
301 list_for_each_entry_safe(u, t, &vha->unknown_atio_list, cmd_list) {
303 ql_dbg(ql_dbg_async, vha, 0x502e,
304 "Freeing unknown %s %p, because of Abort\n",
306 qlt_send_term_exchange(vha->hw->base_qpair, NULL,
307 &u->atio, ha_locked, 0);
311 host = qlt_find_host_by_d_id(vha, u->atio.u.isp24.fcp_hdr.d_id);
313 ql_dbg(ql_dbg_async + ql_dbg_verbose, vha, 0x502f,
314 "Requeuing unknown ATIO_TYPE7 %p\n", u);
315 qlt_24xx_atio_pkt(host, &u->atio, ha_locked);
316 } else if (tgt->tgt_stop) {
317 ql_dbg(ql_dbg_async + ql_dbg_verbose, vha, 0x503a,
318 "Freeing unknown %s %p, because tgt is being stopped\n",
320 qlt_send_term_exchange(vha->hw->base_qpair, NULL,
321 &u->atio, ha_locked, 0);
323 ql_dbg(ql_dbg_async + ql_dbg_verbose, vha, 0x503d,
324 "Reschedule u %p, vha %p, host %p\n", u, vha, host);
327 schedule_delayed_work(&vha->unknown_atio_work,
334 spin_lock_irqsave(&vha->cmd_list_lock, flags);
335 list_del(&u->cmd_list);
336 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
341 void qlt_unknown_atio_work_fn(struct work_struct *work)
343 struct scsi_qla_host *vha = container_of(to_delayed_work(work),
344 struct scsi_qla_host, unknown_atio_work);
346 qlt_try_to_dequeue_unknown_atios(vha, 0);
349 static bool qlt_24xx_atio_pkt_all_vps(struct scsi_qla_host *vha,
350 struct atio_from_isp *atio, uint8_t ha_locked)
352 ql_dbg(ql_dbg_tgt, vha, 0xe072,
353 "%s: qla_target(%d): type %x ox_id %04x\n",
354 __func__, vha->vp_idx, atio->u.raw.entry_type,
355 be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id));
357 switch (atio->u.raw.entry_type) {
360 struct scsi_qla_host *host = qlt_find_host_by_d_id(vha,
361 atio->u.isp24.fcp_hdr.d_id);
362 if (unlikely(NULL == host)) {
363 ql_dbg(ql_dbg_tgt, vha, 0xe03e,
364 "qla_target(%d): Received ATIO_TYPE7 "
365 "with unknown d_id %x:%x:%x\n", vha->vp_idx,
366 atio->u.isp24.fcp_hdr.d_id[0],
367 atio->u.isp24.fcp_hdr.d_id[1],
368 atio->u.isp24.fcp_hdr.d_id[2]);
371 qlt_queue_unknown_atio(vha, atio, ha_locked);
374 if (unlikely(!list_empty(&vha->unknown_atio_list)))
375 qlt_try_to_dequeue_unknown_atios(vha, ha_locked);
377 qlt_24xx_atio_pkt(host, atio, ha_locked);
381 case IMMED_NOTIFY_TYPE:
383 struct scsi_qla_host *host = vha;
384 struct imm_ntfy_from_isp *entry =
385 (struct imm_ntfy_from_isp *)atio;
387 qlt_issue_marker(vha, ha_locked);
389 if ((entry->u.isp24.vp_index != 0xFF) &&
390 (entry->u.isp24.nport_handle != 0xFFFF)) {
391 host = qlt_find_host_by_vp_idx(vha,
392 entry->u.isp24.vp_index);
393 if (unlikely(!host)) {
394 ql_dbg(ql_dbg_tgt, vha, 0xe03f,
395 "qla_target(%d): Received "
396 "ATIO (IMMED_NOTIFY_TYPE) "
397 "with unknown vp_index %d\n",
398 vha->vp_idx, entry->u.isp24.vp_index);
402 qlt_24xx_atio_pkt(host, atio, ha_locked);
406 case VP_RPT_ID_IOCB_TYPE:
407 qla24xx_report_id_acquisition(vha,
408 (struct vp_rpt_id_entry_24xx *)atio);
413 struct abts_recv_from_24xx *entry =
414 (struct abts_recv_from_24xx *)atio;
415 struct scsi_qla_host *host = qlt_find_host_by_vp_idx(vha,
419 if (unlikely(!host)) {
420 ql_dbg(ql_dbg_tgt, vha, 0xe00a,
421 "qla_target(%d): Response pkt (ABTS_RECV_24XX) "
422 "received, with unknown vp_index %d\n",
423 vha->vp_idx, entry->vp_index);
427 spin_lock_irqsave(&host->hw->hardware_lock, flags);
428 qlt_24xx_handle_abts(host, (struct abts_recv_from_24xx *)atio);
430 spin_unlock_irqrestore(&host->hw->hardware_lock, flags);
434 /* case PUREX_IOCB_TYPE: ql2xmvasynctoatio */
437 ql_dbg(ql_dbg_tgt, vha, 0xe040,
438 "qla_target(%d): Received unknown ATIO atio "
439 "type %x\n", vha->vp_idx, atio->u.raw.entry_type);
446 void qlt_response_pkt_all_vps(struct scsi_qla_host *vha,
447 struct rsp_que *rsp, response_t *pkt)
449 switch (pkt->entry_type) {
451 ql_dbg(ql_dbg_tgt, vha, 0xe073,
452 "qla_target(%d):%s: CRC2 Response pkt\n",
453 vha->vp_idx, __func__);
457 struct ctio7_from_24xx *entry = (struct ctio7_from_24xx *)pkt;
458 struct scsi_qla_host *host = qlt_find_host_by_vp_idx(vha,
460 if (unlikely(!host)) {
461 ql_dbg(ql_dbg_tgt, vha, 0xe041,
462 "qla_target(%d): Response pkt (CTIO_TYPE7) "
463 "received, with unknown vp_index %d\n",
464 vha->vp_idx, entry->vp_index);
467 qlt_response_pkt(host, rsp, pkt);
471 case IMMED_NOTIFY_TYPE:
473 struct scsi_qla_host *host = vha;
474 struct imm_ntfy_from_isp *entry =
475 (struct imm_ntfy_from_isp *)pkt;
477 host = qlt_find_host_by_vp_idx(vha, entry->u.isp24.vp_index);
478 if (unlikely(!host)) {
479 ql_dbg(ql_dbg_tgt, vha, 0xe042,
480 "qla_target(%d): Response pkt (IMMED_NOTIFY_TYPE) "
481 "received, with unknown vp_index %d\n",
482 vha->vp_idx, entry->u.isp24.vp_index);
485 qlt_response_pkt(host, rsp, pkt);
489 case NOTIFY_ACK_TYPE:
491 struct scsi_qla_host *host = vha;
492 struct nack_to_isp *entry = (struct nack_to_isp *)pkt;
494 if (0xFF != entry->u.isp24.vp_index) {
495 host = qlt_find_host_by_vp_idx(vha,
496 entry->u.isp24.vp_index);
497 if (unlikely(!host)) {
498 ql_dbg(ql_dbg_tgt, vha, 0xe043,
499 "qla_target(%d): Response "
500 "pkt (NOTIFY_ACK_TYPE) "
501 "received, with unknown "
502 "vp_index %d\n", vha->vp_idx,
503 entry->u.isp24.vp_index);
507 qlt_response_pkt(host, rsp, pkt);
513 struct abts_recv_from_24xx *entry =
514 (struct abts_recv_from_24xx *)pkt;
515 struct scsi_qla_host *host = qlt_find_host_by_vp_idx(vha,
517 if (unlikely(!host)) {
518 ql_dbg(ql_dbg_tgt, vha, 0xe044,
519 "qla_target(%d): Response pkt "
520 "(ABTS_RECV_24XX) received, with unknown "
521 "vp_index %d\n", vha->vp_idx, entry->vp_index);
524 qlt_response_pkt(host, rsp, pkt);
530 struct abts_resp_to_24xx *entry =
531 (struct abts_resp_to_24xx *)pkt;
532 struct scsi_qla_host *host = qlt_find_host_by_vp_idx(vha,
534 if (unlikely(!host)) {
535 ql_dbg(ql_dbg_tgt, vha, 0xe045,
536 "qla_target(%d): Response pkt "
537 "(ABTS_RECV_24XX) received, with unknown "
538 "vp_index %d\n", vha->vp_idx, entry->vp_index);
541 qlt_response_pkt(host, rsp, pkt);
546 qlt_response_pkt(vha, rsp, pkt);
553 * All qlt_plogi_ack_t operations are protected by hardware_lock
555 static int qla24xx_post_nack_work(struct scsi_qla_host *vha, fc_port_t *fcport,
556 struct imm_ntfy_from_isp *ntfy, int type)
558 struct qla_work_evt *e;
559 e = qla2x00_alloc_work(vha, QLA_EVT_NACK);
561 return QLA_FUNCTION_FAILED;
563 e->u.nack.fcport = fcport;
564 e->u.nack.type = type;
565 memcpy(e->u.nack.iocb, ntfy, sizeof(struct imm_ntfy_from_isp));
566 return qla2x00_post_work(vha, e);
570 void qla2x00_async_nack_sp_done(void *s, int res)
572 struct srb *sp = (struct srb *)s;
573 struct scsi_qla_host *vha = sp->vha;
576 ql_dbg(ql_dbg_disc, vha, 0x20f2,
577 "Async done-%s res %x %8phC type %d\n",
578 sp->name, res, sp->fcport->port_name, sp->type);
580 spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
581 sp->fcport->flags &= ~FCF_ASYNC_SENT;
582 sp->fcport->chip_reset = vha->hw->base_qpair->chip_reset;
586 sp->fcport->login_gen++;
587 sp->fcport->fw_login_state = DSC_LS_PLOGI_COMP;
588 sp->fcport->logout_on_delete = 1;
589 sp->fcport->plogi_nack_done_deadline = jiffies + HZ;
590 sp->fcport->send_els_logo = 0;
594 sp->fcport->fw_login_state = DSC_LS_PRLI_COMP;
595 sp->fcport->deleted = 0;
596 sp->fcport->send_els_logo = 0;
598 if (!sp->fcport->login_succ &&
599 !IS_SW_RESV_ADDR(sp->fcport->d_id)) {
600 sp->fcport->login_succ = 1;
604 ql_dbg(ql_dbg_disc, vha, 0x20f3,
605 "%s %d %8phC post upd_fcport fcp_cnt %d\n",
607 sp->fcport->port_name,
609 sp->fcport->disc_state = DSC_UPD_FCPORT;
610 qla24xx_post_upd_fcport_work(vha, sp->fcport);
612 sp->fcport->login_retry = 0;
613 sp->fcport->disc_state = DSC_LOGIN_COMPLETE;
614 sp->fcport->deleted = 0;
615 sp->fcport->logout_on_delete = 1;
620 sp->fcport->login_gen++;
621 sp->fcport->fw_login_state = DSC_LS_PORT_UNAVAIL;
622 qlt_logo_completion_handler(sp->fcport, MBS_COMMAND_COMPLETE);
625 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
630 int qla24xx_async_notify_ack(scsi_qla_host_t *vha, fc_port_t *fcport,
631 struct imm_ntfy_from_isp *ntfy, int type)
633 int rval = QLA_FUNCTION_FAILED;
637 fcport->flags |= FCF_ASYNC_SENT;
640 fcport->fw_login_state = DSC_LS_PLOGI_PEND;
644 fcport->fw_login_state = DSC_LS_PRLI_PEND;
649 fcport->fw_login_state = DSC_LS_LOGO_PEND;
654 sp = qla2x00_get_sp(vha, fcport, GFP_ATOMIC);
661 sp->u.iocb_cmd.timeout = qla2x00_async_iocb_timeout;
662 qla2x00_init_timer(sp, qla2x00_get_async_timeout(vha)+2);
664 sp->u.iocb_cmd.u.nack.ntfy = ntfy;
665 sp->done = qla2x00_async_nack_sp_done;
667 rval = qla2x00_start_sp(sp);
668 if (rval != QLA_SUCCESS)
671 ql_dbg(ql_dbg_disc, vha, 0x20f4,
672 "Async-%s %8phC hndl %x %s\n",
673 sp->name, fcport->port_name, sp->handle, c);
680 fcport->flags &= ~FCF_ASYNC_SENT;
684 void qla24xx_do_nack_work(struct scsi_qla_host *vha, struct qla_work_evt *e)
689 switch (e->u.nack.type) {
691 mutex_lock(&vha->vha_tgt.tgt_mutex);
692 t = qlt_create_sess(vha, e->u.nack.fcport, 0);
693 mutex_unlock(&vha->vha_tgt.tgt_mutex);
695 ql_log(ql_log_info, vha, 0xd034,
696 "%s create sess success %p", __func__, t);
697 spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
698 /* create sess has an extra kref */
699 vha->hw->tgt.tgt_ops->put_sess(e->u.nack.fcport);
700 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
704 qla24xx_async_notify_ack(vha, e->u.nack.fcport,
705 (struct imm_ntfy_from_isp*)e->u.nack.iocb, e->u.nack.type);
708 void qla24xx_delete_sess_fn(struct work_struct *work)
710 fc_port_t *fcport = container_of(work, struct fc_port, del_work);
711 struct qla_hw_data *ha = fcport->vha->hw;
714 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
716 if (fcport->se_sess) {
717 ha->tgt.tgt_ops->shutdown_sess(fcport);
718 ha->tgt.tgt_ops->put_sess(fcport);
720 qlt_unreg_sess(fcport);
722 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
726 * Called from qla2x00_reg_remote_port()
728 void qlt_fc_port_added(struct scsi_qla_host *vha, fc_port_t *fcport)
730 struct qla_hw_data *ha = vha->hw;
731 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
732 struct fc_port *sess = fcport;
735 if (!vha->hw->tgt.tgt_ops)
738 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
740 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
744 if (fcport->disc_state == DSC_DELETE_PEND) {
745 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
749 if (!sess->se_sess) {
750 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
752 mutex_lock(&vha->vha_tgt.tgt_mutex);
753 sess = qlt_create_sess(vha, fcport, false);
754 mutex_unlock(&vha->vha_tgt.tgt_mutex);
756 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
758 if (fcport->fw_login_state == DSC_LS_PRLI_COMP) {
759 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
763 if (!kref_get_unless_zero(&sess->sess_kref)) {
764 ql_dbg(ql_dbg_disc, vha, 0x2107,
765 "%s: kref_get fail sess %8phC \n",
766 __func__, sess->port_name);
767 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
771 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04c,
772 "qla_target(%u): %ssession for port %8phC "
773 "(loop ID %d) reappeared\n", vha->vp_idx,
774 sess->local ? "local " : "", sess->port_name, sess->loop_id);
776 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf007,
777 "Reappeared sess %p\n", sess);
779 ha->tgt.tgt_ops->update_sess(sess, fcport->d_id,
781 (fcport->flags & FCF_CONF_COMP_SUPPORTED));
784 if (sess && sess->local) {
785 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04d,
786 "qla_target(%u): local session for "
787 "port %8phC (loop ID %d) became global\n", vha->vp_idx,
788 fcport->port_name, sess->loop_id);
791 ha->tgt.tgt_ops->put_sess(sess);
792 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
796 * This is a zero-base ref-counting solution, since hardware_lock
797 * guarantees that ref_count is not modified concurrently.
798 * Upon successful return content of iocb is undefined
800 static struct qlt_plogi_ack_t *
801 qlt_plogi_ack_find_add(struct scsi_qla_host *vha, port_id_t *id,
802 struct imm_ntfy_from_isp *iocb)
804 struct qlt_plogi_ack_t *pla;
806 list_for_each_entry(pla, &vha->plogi_ack_list, list) {
807 if (pla->id.b24 == id->b24) {
808 qlt_send_term_imm_notif(vha, &pla->iocb, 1);
809 memcpy(&pla->iocb, iocb, sizeof(pla->iocb));
814 pla = kmem_cache_zalloc(qla_tgt_plogi_cachep, GFP_ATOMIC);
816 ql_dbg(ql_dbg_async, vha, 0x5088,
817 "qla_target(%d): Allocation of plogi_ack failed\n",
822 memcpy(&pla->iocb, iocb, sizeof(pla->iocb));
824 list_add_tail(&pla->list, &vha->plogi_ack_list);
829 void qlt_plogi_ack_unref(struct scsi_qla_host *vha,
830 struct qlt_plogi_ack_t *pla)
832 struct imm_ntfy_from_isp *iocb = &pla->iocb;
835 fc_port_t *fcport = pla->fcport;
837 BUG_ON(!pla->ref_count);
843 ql_dbg(ql_dbg_disc, vha, 0x5089,
844 "Sending PLOGI ACK to wwn %8phC s_id %02x:%02x:%02x loop_id %#04x"
845 " exch %#x ox_id %#x\n", iocb->u.isp24.port_name,
846 iocb->u.isp24.port_id[2], iocb->u.isp24.port_id[1],
847 iocb->u.isp24.port_id[0],
848 le16_to_cpu(iocb->u.isp24.nport_handle),
849 iocb->u.isp24.exchange_address, iocb->ox_id);
851 port_id.b.domain = iocb->u.isp24.port_id[2];
852 port_id.b.area = iocb->u.isp24.port_id[1];
853 port_id.b.al_pa = iocb->u.isp24.port_id[0];
854 port_id.b.rsvd_1 = 0;
856 loop_id = le16_to_cpu(iocb->u.isp24.nport_handle);
858 fcport->loop_id = loop_id;
859 fcport->d_id = port_id;
860 if (iocb->u.isp24.status_subcode == ELS_PLOGI)
861 qla24xx_post_nack_work(vha, fcport, iocb, SRB_NACK_PLOGI);
863 qla24xx_post_nack_work(vha, fcport, iocb, SRB_NACK_PRLI);
865 list_for_each_entry(fcport, &vha->vp_fcports, list) {
866 if (fcport->plogi_link[QLT_PLOGI_LINK_SAME_WWN] == pla)
867 fcport->plogi_link[QLT_PLOGI_LINK_SAME_WWN] = NULL;
868 if (fcport->plogi_link[QLT_PLOGI_LINK_CONFLICT] == pla)
869 fcport->plogi_link[QLT_PLOGI_LINK_CONFLICT] = NULL;
872 list_del(&pla->list);
873 kmem_cache_free(qla_tgt_plogi_cachep, pla);
877 qlt_plogi_ack_link(struct scsi_qla_host *vha, struct qlt_plogi_ack_t *pla,
878 struct fc_port *sess, enum qlt_plogi_link_t link)
880 struct imm_ntfy_from_isp *iocb = &pla->iocb;
881 /* Inc ref_count first because link might already be pointing at pla */
884 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf097,
885 "Linking sess %p [%d] wwn %8phC with PLOGI ACK to wwn %8phC"
886 " s_id %02x:%02x:%02x, ref=%d pla %p link %d\n",
887 sess, link, sess->port_name,
888 iocb->u.isp24.port_name, iocb->u.isp24.port_id[2],
889 iocb->u.isp24.port_id[1], iocb->u.isp24.port_id[0],
890 pla->ref_count, pla, link);
892 if (link == QLT_PLOGI_LINK_CONFLICT) {
893 switch (sess->disc_state) {
895 case DSC_DELETE_PEND:
903 if (sess->plogi_link[link])
904 qlt_plogi_ack_unref(vha, sess->plogi_link[link]);
906 if (link == QLT_PLOGI_LINK_SAME_WWN)
909 sess->plogi_link[link] = pla;
913 /* These fields must be initialized by the caller */
916 * number of cmds dropped while we were waiting for
917 * initiator to ack LOGO initialize to 1 if LOGO is
918 * triggered by a command, otherwise, to 0
922 /* These fields are used by callee */
923 struct list_head list;
927 qlt_send_first_logo(struct scsi_qla_host *vha, qlt_port_logo_t *logo)
929 qlt_port_logo_t *tmp;
932 mutex_lock(&vha->vha_tgt.tgt_mutex);
934 list_for_each_entry(tmp, &vha->logo_list, list) {
935 if (tmp->id.b24 == logo->id.b24) {
936 tmp->cmd_count += logo->cmd_count;
937 mutex_unlock(&vha->vha_tgt.tgt_mutex);
942 list_add_tail(&logo->list, &vha->logo_list);
944 mutex_unlock(&vha->vha_tgt.tgt_mutex);
946 res = qla24xx_els_dcmd_iocb(vha, ELS_DCMD_LOGO, logo->id);
948 mutex_lock(&vha->vha_tgt.tgt_mutex);
949 list_del(&logo->list);
950 mutex_unlock(&vha->vha_tgt.tgt_mutex);
952 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf098,
953 "Finished LOGO to %02x:%02x:%02x, dropped %d cmds, res = %#x\n",
954 logo->id.b.domain, logo->id.b.area, logo->id.b.al_pa,
955 logo->cmd_count, res);
958 void qlt_free_session_done(struct work_struct *work)
960 struct fc_port *sess = container_of(work, struct fc_port,
962 struct qla_tgt *tgt = sess->tgt;
963 struct scsi_qla_host *vha = sess->vha;
964 struct qla_hw_data *ha = vha->hw;
966 bool logout_started = false;
967 scsi_qla_host_t *base_vha;
968 struct qlt_plogi_ack_t *own =
969 sess->plogi_link[QLT_PLOGI_LINK_SAME_WWN];
971 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf084,
972 "%s: se_sess %p / sess %p from port %8phC loop_id %#04x"
973 " s_id %02x:%02x:%02x logout %d keep %d els_logo %d\n",
974 __func__, sess->se_sess, sess, sess->port_name, sess->loop_id,
975 sess->d_id.b.domain, sess->d_id.b.area, sess->d_id.b.al_pa,
976 sess->logout_on_delete, sess->keep_nport_handle,
977 sess->send_els_logo);
979 if (!IS_SW_RESV_ADDR(sess->d_id)) {
980 if (sess->send_els_logo) {
981 qlt_port_logo_t logo;
983 logo.id = sess->d_id;
985 sess->send_els_logo = 0;
986 qlt_send_first_logo(vha, &logo);
989 if (sess->logout_on_delete && sess->loop_id != FC_NO_LOOP_ID) {
994 (own->iocb.u.isp24.status_subcode == ELS_PLOGI))) {
995 rc = qla2x00_post_async_logout_work(vha, sess,
997 if (rc != QLA_SUCCESS)
998 ql_log(ql_log_warn, vha, 0xf085,
999 "Schedule logo failed sess %p rc %d\n",
1002 logout_started = true;
1003 } else if (own && (own->iocb.u.isp24.status_subcode ==
1004 ELS_PRLI) && ha->flags.rida_fmt2) {
1005 rc = qla2x00_post_async_prlo_work(vha, sess,
1007 if (rc != QLA_SUCCESS)
1008 ql_log(ql_log_warn, vha, 0xf085,
1009 "Schedule PRLO failed sess %p rc %d\n",
1012 logout_started = true;
1018 * Release the target session for FC Nexus from fabric module code.
1020 if (sess->se_sess != NULL)
1021 ha->tgt.tgt_ops->free_session(sess);
1023 if (logout_started) {
1024 bool traced = false;
1026 while (!READ_ONCE(sess->logout_completed)) {
1028 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf086,
1029 "%s: waiting for sess %p logout\n",
1036 ql_dbg(ql_dbg_disc, vha, 0xf087,
1037 "%s: sess %p logout completed\n", __func__, sess);
1040 if (sess->logo_ack_needed) {
1041 sess->logo_ack_needed = 0;
1042 qla24xx_async_notify_ack(vha, sess,
1043 (struct imm_ntfy_from_isp *)sess->iocb, SRB_NACK_LOGO);
1046 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
1047 if (sess->se_sess) {
1048 sess->se_sess = NULL;
1049 if (tgt && !IS_SW_RESV_ADDR(sess->d_id))
1053 sess->disc_state = DSC_DELETED;
1054 sess->fw_login_state = DSC_LS_PORT_UNAVAIL;
1055 sess->deleted = QLA_SESS_DELETED;
1056 sess->login_retry = vha->hw->login_retry_count;
1058 if (sess->login_succ && !IS_SW_RESV_ADDR(sess->d_id)) {
1059 vha->fcport_count--;
1060 sess->login_succ = 0;
1063 qla2x00_clear_loop_id(sess);
1065 if (sess->conflict) {
1066 sess->conflict->login_pause = 0;
1067 sess->conflict = NULL;
1068 if (!test_bit(UNLOADING, &vha->dpc_flags))
1069 set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
1073 struct qlt_plogi_ack_t *con =
1074 sess->plogi_link[QLT_PLOGI_LINK_CONFLICT];
1075 struct imm_ntfy_from_isp *iocb;
1079 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf099,
1080 "se_sess %p / sess %p port %8phC is gone,"
1081 " %s (ref=%d), releasing PLOGI for %8phC (ref=%d)\n",
1082 sess->se_sess, sess, sess->port_name,
1083 own ? "releasing own PLOGI" : "no own PLOGI pending",
1084 own ? own->ref_count : -1,
1085 iocb->u.isp24.port_name, con->ref_count);
1086 qlt_plogi_ack_unref(vha, con);
1087 sess->plogi_link[QLT_PLOGI_LINK_CONFLICT] = NULL;
1089 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf09a,
1090 "se_sess %p / sess %p port %8phC is gone, %s (ref=%d)\n",
1091 sess->se_sess, sess, sess->port_name,
1092 own ? "releasing own PLOGI" :
1093 "no own PLOGI pending",
1094 own ? own->ref_count : -1);
1098 sess->fw_login_state = DSC_LS_PLOGI_PEND;
1099 qlt_plogi_ack_unref(vha, own);
1100 sess->plogi_link[QLT_PLOGI_LINK_SAME_WWN] = NULL;
1104 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
1106 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf001,
1107 "Unregistration of sess %p %8phC finished fcp_cnt %d\n",
1108 sess, sess->port_name, vha->fcport_count);
1110 if (tgt && (tgt->sess_count == 0))
1111 wake_up_all(&tgt->waitQ);
1113 if (vha->fcport_count == 0)
1114 wake_up_all(&vha->fcport_waitQ);
1116 base_vha = pci_get_drvdata(ha->pdev);
1118 sess->free_pending = 0;
1120 if (test_bit(PFLG_DRIVER_REMOVING, &base_vha->pci_flags))
1123 if ((!tgt || !tgt->tgt_stop) && !LOOP_TRANSITION(vha)) {
1124 switch (vha->host->active_mode) {
1125 case MODE_INITIATOR:
1127 set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
1128 qla2xxx_wake_dpc(vha);
1138 /* ha->tgt.sess_lock supposed to be held on entry */
1139 void qlt_unreg_sess(struct fc_port *sess)
1141 struct scsi_qla_host *vha = sess->vha;
1142 unsigned long flags;
1144 ql_dbg(ql_dbg_disc, sess->vha, 0x210a,
1145 "%s sess %p for deletion %8phC\n",
1146 __func__, sess, sess->port_name);
1148 spin_lock_irqsave(&sess->vha->work_lock, flags);
1149 if (sess->free_pending) {
1150 spin_unlock_irqrestore(&sess->vha->work_lock, flags);
1153 sess->free_pending = 1;
1154 spin_unlock_irqrestore(&sess->vha->work_lock, flags);
1157 vha->hw->tgt.tgt_ops->clear_nacl_from_fcport_map(sess);
1159 qla2x00_mark_device_lost(vha, sess, 1, 1);
1161 sess->deleted = QLA_SESS_DELETION_IN_PROGRESS;
1162 sess->disc_state = DSC_DELETE_PEND;
1163 sess->last_rscn_gen = sess->rscn_gen;
1164 sess->last_login_gen = sess->login_gen;
1166 if (sess->nvme_flag & NVME_FLAG_REGISTERED &&
1167 !(sess->nvme_flag & NVME_FLAG_DELETING)) {
1168 sess->nvme_flag |= NVME_FLAG_DELETING;
1169 schedule_work(&sess->nvme_del_work);
1171 INIT_WORK(&sess->free_work, qlt_free_session_done);
1172 schedule_work(&sess->free_work);
1175 EXPORT_SYMBOL(qlt_unreg_sess);
1177 static int qlt_reset(struct scsi_qla_host *vha, void *iocb, int mcmd)
1179 struct qla_hw_data *ha = vha->hw;
1180 struct fc_port *sess = NULL;
1183 struct imm_ntfy_from_isp *n = (struct imm_ntfy_from_isp *)iocb;
1184 unsigned long flags;
1186 loop_id = le16_to_cpu(n->u.isp24.nport_handle);
1187 if (loop_id == 0xFFFF) {
1189 atomic_inc(&vha->vha_tgt.qla_tgt->tgt_global_resets_count);
1190 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
1191 qlt_clear_tgt_db(vha->vha_tgt.qla_tgt);
1192 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
1194 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
1195 sess = ha->tgt.tgt_ops->find_sess_by_loop_id(vha, loop_id);
1196 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
1199 ql_dbg(ql_dbg_tgt, vha, 0xe000,
1200 "Using sess for qla_tgt_reset: %p\n", sess);
1206 ql_dbg(ql_dbg_tgt, vha, 0xe047,
1207 "scsi(%ld): resetting (session %p from port %8phC mcmd %x, "
1208 "loop_id %d)\n", vha->host_no, sess, sess->port_name,
1211 return qlt_issue_task_mgmt(sess, 0, mcmd, iocb, QLA24XX_MGMT_SEND_NACK);
1214 static void qla24xx_chk_fcp_state(struct fc_port *sess)
1216 if (sess->chip_reset != sess->vha->hw->base_qpair->chip_reset) {
1217 sess->logout_on_delete = 0;
1218 sess->logo_ack_needed = 0;
1219 sess->fw_login_state = DSC_LS_PORT_UNAVAIL;
1220 sess->scan_state = 0;
1224 void qlt_schedule_sess_for_deletion(struct fc_port *sess)
1226 struct qla_tgt *tgt = sess->tgt;
1227 unsigned long flags;
1229 if (sess->disc_state == DSC_DELETE_PEND)
1232 if (sess->disc_state == DSC_DELETED) {
1233 if (tgt && tgt->tgt_stop && (tgt->sess_count == 0))
1234 wake_up_all(&tgt->waitQ);
1235 if (sess->vha->fcport_count == 0)
1236 wake_up_all(&sess->vha->fcport_waitQ);
1238 if (!sess->plogi_link[QLT_PLOGI_LINK_SAME_WWN] &&
1239 !sess->plogi_link[QLT_PLOGI_LINK_CONFLICT])
1243 if (sess->deleted == QLA_SESS_DELETED)
1244 sess->logout_on_delete = 0;
1246 spin_lock_irqsave(&sess->vha->work_lock, flags);
1247 if (sess->deleted == QLA_SESS_DELETION_IN_PROGRESS) {
1248 spin_unlock_irqrestore(&sess->vha->work_lock, flags);
1251 sess->deleted = QLA_SESS_DELETION_IN_PROGRESS;
1252 spin_unlock_irqrestore(&sess->vha->work_lock, flags);
1254 sess->disc_state = DSC_DELETE_PEND;
1256 qla24xx_chk_fcp_state(sess);
1258 ql_dbg(ql_dbg_tgt, sess->vha, 0xe001,
1259 "Scheduling sess %p for deletion\n", sess);
1261 INIT_WORK(&sess->del_work, qla24xx_delete_sess_fn);
1262 WARN_ON(!queue_work(sess->vha->hw->wq, &sess->del_work));
1265 static void qlt_clear_tgt_db(struct qla_tgt *tgt)
1267 struct fc_port *sess;
1268 scsi_qla_host_t *vha = tgt->vha;
1270 list_for_each_entry(sess, &vha->vp_fcports, list) {
1272 qlt_schedule_sess_for_deletion(sess);
1275 /* At this point tgt could be already dead */
1278 static int qla24xx_get_loop_id(struct scsi_qla_host *vha, const uint8_t *s_id,
1281 struct qla_hw_data *ha = vha->hw;
1282 dma_addr_t gid_list_dma;
1283 struct gid_list_info *gid_list;
1288 gid_list = dma_alloc_coherent(&ha->pdev->dev, qla2x00_gid_list_size(ha),
1289 &gid_list_dma, GFP_KERNEL);
1291 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf044,
1292 "qla_target(%d): DMA Alloc failed of %u\n",
1293 vha->vp_idx, qla2x00_gid_list_size(ha));
1297 /* Get list of logged in devices */
1298 rc = qla24xx_gidlist_wait(vha, gid_list, gid_list_dma, &entries);
1299 if (rc != QLA_SUCCESS) {
1300 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf045,
1301 "qla_target(%d): get_id_list() failed: %x\n",
1304 goto out_free_id_list;
1307 id_iter = (char *)gid_list;
1309 for (i = 0; i < entries; i++) {
1310 struct gid_list_info *gid = (struct gid_list_info *)id_iter;
1311 if ((gid->al_pa == s_id[2]) &&
1312 (gid->area == s_id[1]) &&
1313 (gid->domain == s_id[0])) {
1314 *loop_id = le16_to_cpu(gid->loop_id);
1318 id_iter += ha->gid_list_info_size;
1322 dma_free_coherent(&ha->pdev->dev, qla2x00_gid_list_size(ha),
1323 gid_list, gid_list_dma);
1328 * Adds an extra ref to allow to drop hw lock after adding sess to the list.
1329 * Caller must put it.
1331 static struct fc_port *qlt_create_sess(
1332 struct scsi_qla_host *vha,
1336 struct qla_hw_data *ha = vha->hw;
1337 struct fc_port *sess = fcport;
1338 unsigned long flags;
1340 if (vha->vha_tgt.qla_tgt->tgt_stop)
1343 if (fcport->se_sess) {
1344 if (!kref_get_unless_zero(&sess->sess_kref)) {
1345 ql_dbg(ql_dbg_disc, vha, 0x20f6,
1346 "%s: kref_get_unless_zero failed for %8phC\n",
1347 __func__, sess->port_name);
1352 sess->tgt = vha->vha_tgt.qla_tgt;
1353 sess->local = local;
1356 * Under normal circumstances we want to logout from firmware when
1357 * session eventually ends and release corresponding nport handle.
1358 * In the exception cases (e.g. when new PLOGI is waiting) corresponding
1359 * code will adjust these flags as necessary.
1361 sess->logout_on_delete = 1;
1362 sess->keep_nport_handle = 0;
1363 sess->logout_completed = 0;
1365 if (ha->tgt.tgt_ops->check_initiator_node_acl(vha,
1366 &fcport->port_name[0], sess) < 0) {
1367 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf015,
1368 "(%d) %8phC check_initiator_node_acl failed\n",
1369 vha->vp_idx, fcport->port_name);
1372 kref_init(&fcport->sess_kref);
1374 * Take an extra reference to ->sess_kref here to handle
1375 * fc_port access across ->tgt.sess_lock reaquire.
1377 if (!kref_get_unless_zero(&sess->sess_kref)) {
1378 ql_dbg(ql_dbg_disc, vha, 0x20f7,
1379 "%s: kref_get_unless_zero failed for %8phC\n",
1380 __func__, sess->port_name);
1384 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
1385 if (!IS_SW_RESV_ADDR(sess->d_id))
1386 vha->vha_tgt.qla_tgt->sess_count++;
1388 qlt_do_generation_tick(vha, &sess->generation);
1389 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
1392 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf006,
1393 "Adding sess %p se_sess %p to tgt %p sess_count %d\n",
1394 sess, sess->se_sess, vha->vha_tgt.qla_tgt,
1395 vha->vha_tgt.qla_tgt->sess_count);
1397 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04b,
1398 "qla_target(%d): %ssession for wwn %8phC (loop_id %d, "
1399 "s_id %x:%x:%x, confirmed completion %ssupported) added\n",
1400 vha->vp_idx, local ? "local " : "", fcport->port_name,
1401 fcport->loop_id, sess->d_id.b.domain, sess->d_id.b.area,
1402 sess->d_id.b.al_pa, sess->conf_compl_supported ? "" : "not ");
1408 * max_gen - specifies maximum session generation
1409 * at which this deletion requestion is still valid
1412 qlt_fc_port_deleted(struct scsi_qla_host *vha, fc_port_t *fcport, int max_gen)
1414 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
1415 struct fc_port *sess = fcport;
1416 unsigned long flags;
1418 if (!vha->hw->tgt.tgt_ops)
1424 spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
1425 if (tgt->tgt_stop) {
1426 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
1429 if (!sess->se_sess) {
1430 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
1434 if (max_gen - sess->generation < 0) {
1435 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
1436 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf092,
1437 "Ignoring stale deletion request for se_sess %p / sess %p"
1438 " for port %8phC, req_gen %d, sess_gen %d\n",
1439 sess->se_sess, sess, sess->port_name, max_gen,
1444 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf008, "qla_tgt_fc_port_deleted %p", sess);
1447 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
1448 qlt_schedule_sess_for_deletion(sess);
1451 static inline int test_tgt_sess_count(struct qla_tgt *tgt)
1453 struct qla_hw_data *ha = tgt->ha;
1454 unsigned long flags;
1457 * We need to protect against race, when tgt is freed before or
1460 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
1461 ql_dbg(ql_dbg_tgt, tgt->vha, 0xe002,
1462 "tgt %p, sess_count=%d\n",
1463 tgt, tgt->sess_count);
1464 res = (tgt->sess_count == 0);
1465 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
1470 /* Called by tcm_qla2xxx configfs code */
1471 int qlt_stop_phase1(struct qla_tgt *tgt)
1473 struct scsi_qla_host *vha = tgt->vha;
1474 struct qla_hw_data *ha = tgt->ha;
1475 unsigned long flags;
1477 mutex_lock(&qla_tgt_mutex);
1478 if (!vha->fc_vport) {
1479 struct Scsi_Host *sh = vha->host;
1480 struct fc_host_attrs *fc_host = shost_to_fc_host(sh);
1483 spin_lock_irqsave(sh->host_lock, flags);
1484 npiv_vports = (fc_host->npiv_vports_inuse);
1485 spin_unlock_irqrestore(sh->host_lock, flags);
1488 mutex_unlock(&qla_tgt_mutex);
1489 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf021,
1490 "NPIV is in use. Can not stop target\n");
1494 if (tgt->tgt_stop || tgt->tgt_stopped) {
1495 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04e,
1496 "Already in tgt->tgt_stop or tgt_stopped state\n");
1497 mutex_unlock(&qla_tgt_mutex);
1501 ql_dbg(ql_dbg_tgt_mgt, vha, 0xe003, "Stopping target for host %ld(%p)\n",
1504 * Mutex needed to sync with qla_tgt_fc_port_[added,deleted].
1505 * Lock is needed, because we still can get an incoming packet.
1507 mutex_lock(&vha->vha_tgt.tgt_mutex);
1509 qlt_clear_tgt_db(tgt);
1510 mutex_unlock(&vha->vha_tgt.tgt_mutex);
1511 mutex_unlock(&qla_tgt_mutex);
1513 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf009,
1514 "Waiting for sess works (tgt %p)", tgt);
1515 spin_lock_irqsave(&tgt->sess_work_lock, flags);
1516 while (!list_empty(&tgt->sess_works_list)) {
1517 spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
1518 flush_scheduled_work();
1519 spin_lock_irqsave(&tgt->sess_work_lock, flags);
1521 spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
1523 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00a,
1524 "Waiting for tgt %p: sess_count=%d\n", tgt, tgt->sess_count);
1526 wait_event_timeout(tgt->waitQ, test_tgt_sess_count(tgt), 10*HZ);
1529 if (!ha->flags.host_shutting_down &&
1530 (qla_tgt_mode_enabled(vha) || qla_dual_mode_enabled(vha)))
1531 qlt_disable_vha(vha);
1533 /* Wait for sessions to clear out (just in case) */
1534 wait_event_timeout(tgt->waitQ, test_tgt_sess_count(tgt), 10*HZ);
1537 EXPORT_SYMBOL(qlt_stop_phase1);
1539 /* Called by tcm_qla2xxx configfs code */
1540 void qlt_stop_phase2(struct qla_tgt *tgt)
1542 scsi_qla_host_t *vha = tgt->vha;
1544 if (tgt->tgt_stopped) {
1545 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04f,
1546 "Already in tgt->tgt_stopped state\n");
1550 if (!tgt->tgt_stop) {
1551 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00b,
1552 "%s: phase1 stop is not completed\n", __func__);
1557 mutex_lock(&vha->vha_tgt.tgt_mutex);
1559 tgt->tgt_stopped = 1;
1560 mutex_unlock(&vha->vha_tgt.tgt_mutex);
1562 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00c, "Stop of tgt %p finished\n",
1565 EXPORT_SYMBOL(qlt_stop_phase2);
1567 /* Called from qlt_remove_target() -> qla2x00_remove_one() */
1568 static void qlt_release(struct qla_tgt *tgt)
1570 scsi_qla_host_t *vha = tgt->vha;
1574 struct qla_qpair_hint *h;
1575 struct qla_hw_data *ha = vha->hw;
1577 if ((vha->vha_tgt.qla_tgt != NULL) && !tgt->tgt_stop &&
1579 qlt_stop_phase1(tgt);
1581 if ((vha->vha_tgt.qla_tgt != NULL) && !tgt->tgt_stopped)
1582 qlt_stop_phase2(tgt);
1584 for (i = 0; i < vha->hw->max_qpairs + 1; i++) {
1585 unsigned long flags;
1587 h = &tgt->qphints[i];
1589 spin_lock_irqsave(h->qpair->qp_lock_ptr, flags);
1590 list_del(&h->hint_elem);
1591 spin_unlock_irqrestore(h->qpair->qp_lock_ptr, flags);
1595 kfree(tgt->qphints);
1596 mutex_lock(&qla_tgt_mutex);
1597 list_del(&vha->vha_tgt.qla_tgt->tgt_list_entry);
1598 mutex_unlock(&qla_tgt_mutex);
1600 btree_for_each_safe64(&tgt->lun_qpair_map, key, node)
1601 btree_remove64(&tgt->lun_qpair_map, key);
1603 btree_destroy64(&tgt->lun_qpair_map);
1606 if (ha->tgt.tgt_ops &&
1607 ha->tgt.tgt_ops->remove_target &&
1608 vha->vha_tgt.target_lport_ptr)
1609 ha->tgt.tgt_ops->remove_target(vha);
1611 vha->vha_tgt.qla_tgt = NULL;
1613 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00d,
1614 "Release of tgt %p finished\n", tgt);
1619 /* ha->hardware_lock supposed to be held on entry */
1620 static int qlt_sched_sess_work(struct qla_tgt *tgt, int type,
1621 const void *param, unsigned int param_size)
1623 struct qla_tgt_sess_work_param *prm;
1624 unsigned long flags;
1626 prm = kzalloc(sizeof(*prm), GFP_ATOMIC);
1628 ql_dbg(ql_dbg_tgt_mgt, tgt->vha, 0xf050,
1629 "qla_target(%d): Unable to create session "
1630 "work, command will be refused", 0);
1634 ql_dbg(ql_dbg_tgt_mgt, tgt->vha, 0xf00e,
1635 "Scheduling work (type %d, prm %p)"
1636 " to find session for param %p (size %d, tgt %p)\n",
1637 type, prm, param, param_size, tgt);
1640 memcpy(&prm->tm_iocb, param, param_size);
1642 spin_lock_irqsave(&tgt->sess_work_lock, flags);
1643 list_add_tail(&prm->sess_works_list_entry, &tgt->sess_works_list);
1644 spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
1646 schedule_work(&tgt->sess_work);
1652 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
1654 static void qlt_send_notify_ack(struct qla_qpair *qpair,
1655 struct imm_ntfy_from_isp *ntfy,
1656 uint32_t add_flags, uint16_t resp_code, int resp_code_valid,
1657 uint16_t srr_flags, uint16_t srr_reject_code, uint8_t srr_explan)
1659 struct scsi_qla_host *vha = qpair->vha;
1660 struct qla_hw_data *ha = vha->hw;
1662 struct nack_to_isp *nack;
1664 if (!ha->flags.fw_started)
1667 ql_dbg(ql_dbg_tgt, vha, 0xe004, "Sending NOTIFY_ACK (ha=%p)\n", ha);
1669 pkt = (request_t *)__qla2x00_alloc_iocbs(qpair, NULL);
1671 ql_dbg(ql_dbg_tgt, vha, 0xe049,
1672 "qla_target(%d): %s failed: unable to allocate "
1673 "request packet\n", vha->vp_idx, __func__);
1677 if (vha->vha_tgt.qla_tgt != NULL)
1678 vha->vha_tgt.qla_tgt->notify_ack_expected++;
1680 pkt->entry_type = NOTIFY_ACK_TYPE;
1681 pkt->entry_count = 1;
1683 nack = (struct nack_to_isp *)pkt;
1684 nack->ox_id = ntfy->ox_id;
1686 nack->u.isp24.handle = QLA_TGT_SKIP_HANDLE;
1687 nack->u.isp24.nport_handle = ntfy->u.isp24.nport_handle;
1688 if (le16_to_cpu(ntfy->u.isp24.status) == IMM_NTFY_ELS) {
1689 nack->u.isp24.flags = ntfy->u.isp24.flags &
1690 cpu_to_le32(NOTIFY24XX_FLAGS_PUREX_IOCB);
1692 nack->u.isp24.srr_rx_id = ntfy->u.isp24.srr_rx_id;
1693 nack->u.isp24.status = ntfy->u.isp24.status;
1694 nack->u.isp24.status_subcode = ntfy->u.isp24.status_subcode;
1695 nack->u.isp24.fw_handle = ntfy->u.isp24.fw_handle;
1696 nack->u.isp24.exchange_address = ntfy->u.isp24.exchange_address;
1697 nack->u.isp24.srr_rel_offs = ntfy->u.isp24.srr_rel_offs;
1698 nack->u.isp24.srr_ui = ntfy->u.isp24.srr_ui;
1699 nack->u.isp24.srr_flags = cpu_to_le16(srr_flags);
1700 nack->u.isp24.srr_reject_code = srr_reject_code;
1701 nack->u.isp24.srr_reject_code_expl = srr_explan;
1702 nack->u.isp24.vp_index = ntfy->u.isp24.vp_index;
1704 ql_dbg(ql_dbg_tgt, vha, 0xe005,
1705 "qla_target(%d): Sending 24xx Notify Ack %d\n",
1706 vha->vp_idx, nack->u.isp24.status);
1708 /* Memory Barrier */
1710 qla2x00_start_iocbs(vha, qpair->req);
1714 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
1716 static void qlt_24xx_send_abts_resp(struct qla_qpair *qpair,
1717 struct abts_recv_from_24xx *abts, uint32_t status,
1720 struct scsi_qla_host *vha = qpair->vha;
1721 struct qla_hw_data *ha = vha->hw;
1722 struct abts_resp_to_24xx *resp;
1726 ql_dbg(ql_dbg_tgt, vha, 0xe006,
1727 "Sending task mgmt ABTS response (ha=%p, atio=%p, status=%x\n",
1730 resp = (struct abts_resp_to_24xx *)qla2x00_alloc_iocbs_ready(qpair,
1733 ql_dbg(ql_dbg_tgt, vha, 0xe04a,
1734 "qla_target(%d): %s failed: unable to allocate "
1735 "request packet", vha->vp_idx, __func__);
1739 resp->entry_type = ABTS_RESP_24XX;
1740 resp->entry_count = 1;
1741 resp->nport_handle = abts->nport_handle;
1742 resp->vp_index = vha->vp_idx;
1743 resp->sof_type = abts->sof_type;
1744 resp->exchange_address = abts->exchange_address;
1745 resp->fcp_hdr_le = abts->fcp_hdr_le;
1746 f_ctl = cpu_to_le32(F_CTL_EXCH_CONTEXT_RESP |
1747 F_CTL_LAST_SEQ | F_CTL_END_SEQ |
1748 F_CTL_SEQ_INITIATIVE);
1749 p = (uint8_t *)&f_ctl;
1750 resp->fcp_hdr_le.f_ctl[0] = *p++;
1751 resp->fcp_hdr_le.f_ctl[1] = *p++;
1752 resp->fcp_hdr_le.f_ctl[2] = *p;
1754 resp->fcp_hdr_le.d_id[0] = abts->fcp_hdr_le.d_id[0];
1755 resp->fcp_hdr_le.d_id[1] = abts->fcp_hdr_le.d_id[1];
1756 resp->fcp_hdr_le.d_id[2] = abts->fcp_hdr_le.d_id[2];
1757 resp->fcp_hdr_le.s_id[0] = abts->fcp_hdr_le.s_id[0];
1758 resp->fcp_hdr_le.s_id[1] = abts->fcp_hdr_le.s_id[1];
1759 resp->fcp_hdr_le.s_id[2] = abts->fcp_hdr_le.s_id[2];
1761 resp->fcp_hdr_le.d_id[0] = abts->fcp_hdr_le.s_id[0];
1762 resp->fcp_hdr_le.d_id[1] = abts->fcp_hdr_le.s_id[1];
1763 resp->fcp_hdr_le.d_id[2] = abts->fcp_hdr_le.s_id[2];
1764 resp->fcp_hdr_le.s_id[0] = abts->fcp_hdr_le.d_id[0];
1765 resp->fcp_hdr_le.s_id[1] = abts->fcp_hdr_le.d_id[1];
1766 resp->fcp_hdr_le.s_id[2] = abts->fcp_hdr_le.d_id[2];
1768 resp->exchange_addr_to_abort = abts->exchange_addr_to_abort;
1769 if (status == FCP_TMF_CMPL) {
1770 resp->fcp_hdr_le.r_ctl = R_CTL_BASIC_LINK_SERV | R_CTL_B_ACC;
1771 resp->payload.ba_acct.seq_id_valid = SEQ_ID_INVALID;
1772 resp->payload.ba_acct.low_seq_cnt = 0x0000;
1773 resp->payload.ba_acct.high_seq_cnt = 0xFFFF;
1774 resp->payload.ba_acct.ox_id = abts->fcp_hdr_le.ox_id;
1775 resp->payload.ba_acct.rx_id = abts->fcp_hdr_le.rx_id;
1777 resp->fcp_hdr_le.r_ctl = R_CTL_BASIC_LINK_SERV | R_CTL_B_RJT;
1778 resp->payload.ba_rjt.reason_code =
1779 BA_RJT_REASON_CODE_UNABLE_TO_PERFORM;
1780 /* Other bytes are zero */
1783 vha->vha_tgt.qla_tgt->abts_resp_expected++;
1785 /* Memory Barrier */
1787 if (qpair->reqq_start_iocbs)
1788 qpair->reqq_start_iocbs(qpair);
1790 qla2x00_start_iocbs(vha, qpair->req);
1794 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
1796 static void qlt_24xx_retry_term_exchange(struct scsi_qla_host *vha,
1797 struct abts_resp_from_24xx_fw *entry)
1799 struct ctio7_to_24xx *ctio;
1801 ql_dbg(ql_dbg_tgt, vha, 0xe007,
1802 "Sending retry TERM EXCH CTIO7 (ha=%p)\n", vha->hw);
1804 ctio = (struct ctio7_to_24xx *)qla2x00_alloc_iocbs_ready(
1805 vha->hw->base_qpair, NULL);
1807 ql_dbg(ql_dbg_tgt, vha, 0xe04b,
1808 "qla_target(%d): %s failed: unable to allocate "
1809 "request packet\n", vha->vp_idx, __func__);
1814 * We've got on entrance firmware's response on by us generated
1815 * ABTS response. So, in it ID fields are reversed.
1818 ctio->entry_type = CTIO_TYPE7;
1819 ctio->entry_count = 1;
1820 ctio->nport_handle = entry->nport_handle;
1821 ctio->handle = QLA_TGT_SKIP_HANDLE | CTIO_COMPLETION_HANDLE_MARK;
1822 ctio->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
1823 ctio->vp_index = vha->vp_idx;
1824 ctio->initiator_id[0] = entry->fcp_hdr_le.d_id[0];
1825 ctio->initiator_id[1] = entry->fcp_hdr_le.d_id[1];
1826 ctio->initiator_id[2] = entry->fcp_hdr_le.d_id[2];
1827 ctio->exchange_addr = entry->exchange_addr_to_abort;
1828 ctio->u.status1.flags = cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_1 |
1829 CTIO7_FLAGS_TERMINATE);
1830 ctio->u.status1.ox_id = cpu_to_le16(entry->fcp_hdr_le.ox_id);
1832 /* Memory Barrier */
1834 qla2x00_start_iocbs(vha, vha->req);
1836 qlt_24xx_send_abts_resp(vha->hw->base_qpair,
1837 (struct abts_recv_from_24xx *)entry,
1838 FCP_TMF_CMPL, true);
1841 static int abort_cmd_for_tag(struct scsi_qla_host *vha, uint32_t tag)
1843 struct qla_tgt_sess_op *op;
1844 struct qla_tgt_cmd *cmd;
1845 unsigned long flags;
1847 spin_lock_irqsave(&vha->cmd_list_lock, flags);
1848 list_for_each_entry(op, &vha->qla_sess_op_cmd_list, cmd_list) {
1849 if (tag == op->atio.u.isp24.exchange_addr) {
1851 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
1856 list_for_each_entry(op, &vha->unknown_atio_list, cmd_list) {
1857 if (tag == op->atio.u.isp24.exchange_addr) {
1859 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
1864 list_for_each_entry(cmd, &vha->qla_cmd_list, cmd_list) {
1865 if (tag == cmd->atio.u.isp24.exchange_addr) {
1867 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
1871 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
1876 /* drop cmds for the given lun
1877 * XXX only looks for cmds on the port through which lun reset was recieved
1878 * XXX does not go through the list of other port (which may have cmds
1881 static void abort_cmds_for_lun(struct scsi_qla_host *vha,
1882 u64 lun, uint8_t *s_id)
1884 struct qla_tgt_sess_op *op;
1885 struct qla_tgt_cmd *cmd;
1887 unsigned long flags;
1889 key = sid_to_key(s_id);
1890 spin_lock_irqsave(&vha->cmd_list_lock, flags);
1891 list_for_each_entry(op, &vha->qla_sess_op_cmd_list, cmd_list) {
1895 op_key = sid_to_key(op->atio.u.isp24.fcp_hdr.s_id);
1896 op_lun = scsilun_to_int(
1897 (struct scsi_lun *)&op->atio.u.isp24.fcp_cmnd.lun);
1898 if (op_key == key && op_lun == lun)
1902 list_for_each_entry(op, &vha->unknown_atio_list, cmd_list) {
1906 op_key = sid_to_key(op->atio.u.isp24.fcp_hdr.s_id);
1907 op_lun = scsilun_to_int(
1908 (struct scsi_lun *)&op->atio.u.isp24.fcp_cmnd.lun);
1909 if (op_key == key && op_lun == lun)
1913 list_for_each_entry(cmd, &vha->qla_cmd_list, cmd_list) {
1917 cmd_key = sid_to_key(cmd->atio.u.isp24.fcp_hdr.s_id);
1918 cmd_lun = scsilun_to_int(
1919 (struct scsi_lun *)&cmd->atio.u.isp24.fcp_cmnd.lun);
1920 if (cmd_key == key && cmd_lun == lun)
1923 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
1926 static struct qla_qpair_hint *qlt_find_qphint(struct scsi_qla_host *vha,
1927 uint64_t unpacked_lun)
1929 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
1930 struct qla_qpair_hint *h = NULL;
1932 if (vha->flags.qpairs_available) {
1933 h = btree_lookup64(&tgt->lun_qpair_map, unpacked_lun);
1935 h = &tgt->qphints[0];
1937 h = &tgt->qphints[0];
1943 static void qlt_do_tmr_work(struct work_struct *work)
1945 struct qla_tgt_mgmt_cmd *mcmd =
1946 container_of(work, struct qla_tgt_mgmt_cmd, work);
1947 struct qla_hw_data *ha = mcmd->vha->hw;
1950 unsigned long flags;
1952 switch (mcmd->tmr_func) {
1954 tag = mcmd->orig_iocb.abts.exchange_addr_to_abort;
1961 rc = ha->tgt.tgt_ops->handle_tmr(mcmd, mcmd->unpacked_lun,
1962 mcmd->tmr_func, tag);
1965 spin_lock_irqsave(mcmd->qpair->qp_lock_ptr, flags);
1966 switch (mcmd->tmr_func) {
1968 qlt_24xx_send_abts_resp(mcmd->qpair,
1969 &mcmd->orig_iocb.abts,
1970 FCP_TMF_REJECTED, false);
1972 case QLA_TGT_LUN_RESET:
1973 case QLA_TGT_CLEAR_TS:
1974 case QLA_TGT_ABORT_TS:
1975 case QLA_TGT_CLEAR_ACA:
1976 case QLA_TGT_TARGET_RESET:
1977 qlt_send_busy(mcmd->qpair, &mcmd->orig_iocb.atio,
1981 case QLA_TGT_ABORT_ALL:
1982 case QLA_TGT_NEXUS_LOSS_SESS:
1983 case QLA_TGT_NEXUS_LOSS:
1984 qlt_send_notify_ack(mcmd->qpair,
1985 &mcmd->orig_iocb.imm_ntfy, 0, 0, 0, 0, 0, 0);
1988 spin_unlock_irqrestore(mcmd->qpair->qp_lock_ptr, flags);
1990 ql_dbg(ql_dbg_tgt_mgt, mcmd->vha, 0xf052,
1991 "qla_target(%d): tgt_ops->handle_tmr() failed: %d\n",
1992 mcmd->vha->vp_idx, rc);
1993 mempool_free(mcmd, qla_tgt_mgmt_cmd_mempool);
1997 /* ha->hardware_lock supposed to be held on entry */
1998 static int __qlt_24xx_handle_abts(struct scsi_qla_host *vha,
1999 struct abts_recv_from_24xx *abts, struct fc_port *sess)
2001 struct qla_hw_data *ha = vha->hw;
2002 struct qla_tgt_mgmt_cmd *mcmd;
2003 struct qla_qpair_hint *h = &vha->vha_tgt.qla_tgt->qphints[0];
2005 if (abort_cmd_for_tag(vha, abts->exchange_addr_to_abort)) {
2006 /* send TASK_ABORT response immediately */
2007 qlt_24xx_send_abts_resp(ha->base_qpair, abts, FCP_TMF_CMPL, false);
2011 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00f,
2012 "qla_target(%d): task abort (tag=%d)\n",
2013 vha->vp_idx, abts->exchange_addr_to_abort);
2015 mcmd = mempool_alloc(qla_tgt_mgmt_cmd_mempool, GFP_ATOMIC);
2017 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf051,
2018 "qla_target(%d): %s: Allocation of ABORT cmd failed",
2019 vha->vp_idx, __func__);
2022 memset(mcmd, 0, sizeof(*mcmd));
2025 memcpy(&mcmd->orig_iocb.abts, abts, sizeof(mcmd->orig_iocb.abts));
2026 mcmd->reset_count = ha->base_qpair->chip_reset;
2027 mcmd->tmr_func = QLA_TGT_ABTS;
2028 mcmd->qpair = h->qpair;
2032 * LUN is looked up by target-core internally based on the passed
2033 * abts->exchange_addr_to_abort tag.
2035 mcmd->se_cmd.cpuid = h->cpuid;
2037 if (ha->tgt.tgt_ops->find_cmd_by_tag) {
2038 struct qla_tgt_cmd *abort_cmd;
2040 abort_cmd = ha->tgt.tgt_ops->find_cmd_by_tag(sess,
2041 abts->exchange_addr_to_abort);
2042 if (abort_cmd && abort_cmd->qpair) {
2043 mcmd->qpair = abort_cmd->qpair;
2044 mcmd->se_cmd.cpuid = abort_cmd->se_cmd.cpuid;
2048 INIT_WORK(&mcmd->work, qlt_do_tmr_work);
2049 queue_work_on(mcmd->se_cmd.cpuid, qla_tgt_wq, &mcmd->work);
2055 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
2057 static void qlt_24xx_handle_abts(struct scsi_qla_host *vha,
2058 struct abts_recv_from_24xx *abts)
2060 struct qla_hw_data *ha = vha->hw;
2061 struct fc_port *sess;
2062 uint32_t tag = abts->exchange_addr_to_abort;
2065 unsigned long flags;
2067 if (le32_to_cpu(abts->fcp_hdr_le.parameter) & ABTS_PARAM_ABORT_SEQ) {
2068 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf053,
2069 "qla_target(%d): ABTS: Abort Sequence not "
2070 "supported\n", vha->vp_idx);
2071 qlt_24xx_send_abts_resp(ha->base_qpair, abts, FCP_TMF_REJECTED,
2076 if (tag == ATIO_EXCHANGE_ADDRESS_UNKNOWN) {
2077 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf010,
2078 "qla_target(%d): ABTS: Unknown Exchange "
2079 "Address received\n", vha->vp_idx);
2080 qlt_24xx_send_abts_resp(ha->base_qpair, abts, FCP_TMF_REJECTED,
2085 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf011,
2086 "qla_target(%d): task abort (s_id=%x:%x:%x, "
2087 "tag=%d, param=%x)\n", vha->vp_idx, abts->fcp_hdr_le.s_id[2],
2088 abts->fcp_hdr_le.s_id[1], abts->fcp_hdr_le.s_id[0], tag,
2089 le32_to_cpu(abts->fcp_hdr_le.parameter));
2091 s_id[0] = abts->fcp_hdr_le.s_id[2];
2092 s_id[1] = abts->fcp_hdr_le.s_id[1];
2093 s_id[2] = abts->fcp_hdr_le.s_id[0];
2095 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
2096 sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha, s_id);
2098 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf012,
2099 "qla_target(%d): task abort for non-existent session\n",
2101 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
2103 qlt_24xx_send_abts_resp(ha->base_qpair, abts, FCP_TMF_REJECTED,
2107 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
2110 if (sess->deleted) {
2111 qlt_24xx_send_abts_resp(ha->base_qpair, abts, FCP_TMF_REJECTED,
2116 rc = __qlt_24xx_handle_abts(vha, abts, sess);
2118 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf054,
2119 "qla_target(%d): __qlt_24xx_handle_abts() failed: %d\n",
2121 qlt_24xx_send_abts_resp(ha->base_qpair, abts, FCP_TMF_REJECTED,
2128 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
2130 static void qlt_24xx_send_task_mgmt_ctio(struct qla_qpair *qpair,
2131 struct qla_tgt_mgmt_cmd *mcmd, uint32_t resp_code)
2133 struct scsi_qla_host *ha = mcmd->vha;
2134 struct atio_from_isp *atio = &mcmd->orig_iocb.atio;
2135 struct ctio7_to_24xx *ctio;
2138 ql_dbg(ql_dbg_tgt, ha, 0xe008,
2139 "Sending task mgmt CTIO7 (ha=%p, atio=%p, resp_code=%x\n",
2140 ha, atio, resp_code);
2143 ctio = (struct ctio7_to_24xx *)__qla2x00_alloc_iocbs(qpair, NULL);
2145 ql_dbg(ql_dbg_tgt, ha, 0xe04c,
2146 "qla_target(%d): %s failed: unable to allocate "
2147 "request packet\n", ha->vp_idx, __func__);
2151 ctio->entry_type = CTIO_TYPE7;
2152 ctio->entry_count = 1;
2153 ctio->handle = QLA_TGT_SKIP_HANDLE | CTIO_COMPLETION_HANDLE_MARK;
2154 ctio->nport_handle = mcmd->sess->loop_id;
2155 ctio->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
2156 ctio->vp_index = ha->vp_idx;
2157 ctio->initiator_id[0] = atio->u.isp24.fcp_hdr.s_id[2];
2158 ctio->initiator_id[1] = atio->u.isp24.fcp_hdr.s_id[1];
2159 ctio->initiator_id[2] = atio->u.isp24.fcp_hdr.s_id[0];
2160 ctio->exchange_addr = atio->u.isp24.exchange_addr;
2161 temp = (atio->u.isp24.attr << 9)|
2162 CTIO7_FLAGS_STATUS_MODE_1 | CTIO7_FLAGS_SEND_STATUS;
2163 ctio->u.status1.flags = cpu_to_le16(temp);
2164 temp = be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id);
2165 ctio->u.status1.ox_id = cpu_to_le16(temp);
2166 ctio->u.status1.scsi_status =
2167 cpu_to_le16(SS_RESPONSE_INFO_LEN_VALID);
2168 ctio->u.status1.response_len = cpu_to_le16(8);
2169 ctio->u.status1.sense_data[0] = resp_code;
2171 /* Memory Barrier */
2173 if (qpair->reqq_start_iocbs)
2174 qpair->reqq_start_iocbs(qpair);
2176 qla2x00_start_iocbs(ha, qpair->req);
2179 void qlt_free_mcmd(struct qla_tgt_mgmt_cmd *mcmd)
2181 mempool_free(mcmd, qla_tgt_mgmt_cmd_mempool);
2183 EXPORT_SYMBOL(qlt_free_mcmd);
2186 * ha->hardware_lock supposed to be held on entry. Might drop it, then
2189 void qlt_send_resp_ctio(struct qla_qpair *qpair, struct qla_tgt_cmd *cmd,
2190 uint8_t scsi_status, uint8_t sense_key, uint8_t asc, uint8_t ascq)
2192 struct atio_from_isp *atio = &cmd->atio;
2193 struct ctio7_to_24xx *ctio;
2195 struct scsi_qla_host *vha = cmd->vha;
2197 ql_dbg(ql_dbg_tgt_dif, vha, 0x3066,
2198 "Sending response CTIO7 (vha=%p, atio=%p, scsi_status=%02x, "
2199 "sense_key=%02x, asc=%02x, ascq=%02x",
2200 vha, atio, scsi_status, sense_key, asc, ascq);
2202 ctio = (struct ctio7_to_24xx *)qla2x00_alloc_iocbs(vha, NULL);
2204 ql_dbg(ql_dbg_async, vha, 0x3067,
2205 "qla2x00t(%ld): %s failed: unable to allocate request packet",
2206 vha->host_no, __func__);
2210 ctio->entry_type = CTIO_TYPE7;
2211 ctio->entry_count = 1;
2212 ctio->handle = QLA_TGT_SKIP_HANDLE;
2213 ctio->nport_handle = cmd->sess->loop_id;
2214 ctio->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
2215 ctio->vp_index = vha->vp_idx;
2216 ctio->initiator_id[0] = atio->u.isp24.fcp_hdr.s_id[2];
2217 ctio->initiator_id[1] = atio->u.isp24.fcp_hdr.s_id[1];
2218 ctio->initiator_id[2] = atio->u.isp24.fcp_hdr.s_id[0];
2219 ctio->exchange_addr = atio->u.isp24.exchange_addr;
2220 temp = (atio->u.isp24.attr << 9) |
2221 CTIO7_FLAGS_STATUS_MODE_1 | CTIO7_FLAGS_SEND_STATUS;
2222 ctio->u.status1.flags = cpu_to_le16(temp);
2223 temp = be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id);
2224 ctio->u.status1.ox_id = cpu_to_le16(temp);
2225 ctio->u.status1.scsi_status =
2226 cpu_to_le16(SS_RESPONSE_INFO_LEN_VALID | scsi_status);
2227 ctio->u.status1.response_len = cpu_to_le16(18);
2228 ctio->u.status1.residual = cpu_to_le32(get_datalen_for_atio(atio));
2230 if (ctio->u.status1.residual != 0)
2231 ctio->u.status1.scsi_status |=
2232 cpu_to_le16(SS_RESIDUAL_UNDER);
2234 /* Response code and sense key */
2235 put_unaligned_le32(((0x70 << 24) | (sense_key << 8)),
2236 (&ctio->u.status1.sense_data)[0]);
2237 /* Additional sense length */
2238 put_unaligned_le32(0x0a, (&ctio->u.status1.sense_data)[1]);
2240 put_unaligned_le32(((asc << 24) | (ascq << 16)),
2241 (&ctio->u.status1.sense_data)[3]);
2243 /* Memory Barrier */
2246 if (qpair->reqq_start_iocbs)
2247 qpair->reqq_start_iocbs(qpair);
2249 qla2x00_start_iocbs(vha, qpair->req);
2255 /* callback from target fabric module code */
2256 void qlt_xmit_tm_rsp(struct qla_tgt_mgmt_cmd *mcmd)
2258 struct scsi_qla_host *vha = mcmd->sess->vha;
2259 struct qla_hw_data *ha = vha->hw;
2260 unsigned long flags;
2261 struct qla_qpair *qpair = mcmd->qpair;
2263 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf013,
2264 "TM response mcmd (%p) status %#x state %#x",
2265 mcmd, mcmd->fc_tm_rsp, mcmd->flags);
2267 spin_lock_irqsave(qpair->qp_lock_ptr, flags);
2269 if (!vha->flags.online || mcmd->reset_count != qpair->chip_reset) {
2271 * Either the port is not online or this request was from
2272 * previous life, just abort the processing.
2274 ql_dbg(ql_dbg_async, vha, 0xe100,
2275 "RESET-TMR online/active/old-count/new-count = %d/%d/%d/%d.\n",
2276 vha->flags.online, qla2x00_reset_active(vha),
2277 mcmd->reset_count, qpair->chip_reset);
2278 ha->tgt.tgt_ops->free_mcmd(mcmd);
2279 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
2283 if (mcmd->flags == QLA24XX_MGMT_SEND_NACK) {
2284 if (mcmd->orig_iocb.imm_ntfy.u.isp24.status_subcode ==
2286 mcmd->orig_iocb.imm_ntfy.u.isp24.status_subcode ==
2288 mcmd->orig_iocb.imm_ntfy.u.isp24.status_subcode ==
2290 ql_dbg(ql_dbg_disc, vha, 0x2106,
2291 "TM response logo %phC status %#x state %#x",
2292 mcmd->sess->port_name, mcmd->fc_tm_rsp,
2294 qlt_schedule_sess_for_deletion(mcmd->sess);
2296 qlt_send_notify_ack(vha->hw->base_qpair,
2297 &mcmd->orig_iocb.imm_ntfy, 0, 0, 0, 0, 0, 0);
2300 if (mcmd->orig_iocb.atio.u.raw.entry_type == ABTS_RECV_24XX)
2301 qlt_24xx_send_abts_resp(qpair, &mcmd->orig_iocb.abts,
2302 mcmd->fc_tm_rsp, false);
2304 qlt_24xx_send_task_mgmt_ctio(qpair, mcmd,
2308 * Make the callback for ->free_mcmd() to queue_work() and invoke
2309 * target_put_sess_cmd() to drop cmd_kref to 1. The final
2310 * target_put_sess_cmd() call will be made from TFO->check_stop_free()
2311 * -> tcm_qla2xxx_check_stop_free() to release the TMR associated se_cmd
2312 * descriptor after TFO->queue_tm_rsp() -> tcm_qla2xxx_queue_tm_rsp() ->
2313 * qlt_xmit_tm_rsp() returns here..
2315 ha->tgt.tgt_ops->free_mcmd(mcmd);
2316 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
2318 EXPORT_SYMBOL(qlt_xmit_tm_rsp);
2321 static int qlt_pci_map_calc_cnt(struct qla_tgt_prm *prm)
2323 struct qla_tgt_cmd *cmd = prm->cmd;
2325 BUG_ON(cmd->sg_cnt == 0);
2327 prm->sg = (struct scatterlist *)cmd->sg;
2328 prm->seg_cnt = pci_map_sg(cmd->qpair->pdev, cmd->sg,
2329 cmd->sg_cnt, cmd->dma_data_direction);
2330 if (unlikely(prm->seg_cnt == 0))
2333 prm->cmd->sg_mapped = 1;
2335 if (cmd->se_cmd.prot_op == TARGET_PROT_NORMAL) {
2337 * If greater than four sg entries then we need to allocate
2338 * the continuation entries
2340 if (prm->seg_cnt > QLA_TGT_DATASEGS_PER_CMD_24XX)
2341 prm->req_cnt += DIV_ROUND_UP(prm->seg_cnt -
2342 QLA_TGT_DATASEGS_PER_CMD_24XX,
2343 QLA_TGT_DATASEGS_PER_CONT_24XX);
2346 if ((cmd->se_cmd.prot_op == TARGET_PROT_DIN_INSERT) ||
2347 (cmd->se_cmd.prot_op == TARGET_PROT_DOUT_STRIP)) {
2348 prm->seg_cnt = DIV_ROUND_UP(cmd->bufflen, cmd->blk_sz);
2349 prm->tot_dsds = prm->seg_cnt;
2351 prm->tot_dsds = prm->seg_cnt;
2353 if (cmd->prot_sg_cnt) {
2354 prm->prot_sg = cmd->prot_sg;
2355 prm->prot_seg_cnt = pci_map_sg(cmd->qpair->pdev,
2356 cmd->prot_sg, cmd->prot_sg_cnt,
2357 cmd->dma_data_direction);
2358 if (unlikely(prm->prot_seg_cnt == 0))
2361 if ((cmd->se_cmd.prot_op == TARGET_PROT_DIN_INSERT) ||
2362 (cmd->se_cmd.prot_op == TARGET_PROT_DOUT_STRIP)) {
2363 /* Dif Bundling not support here */
2364 prm->prot_seg_cnt = DIV_ROUND_UP(cmd->bufflen,
2366 prm->tot_dsds += prm->prot_seg_cnt;
2368 prm->tot_dsds += prm->prot_seg_cnt;
2375 ql_dbg_qp(ql_dbg_tgt, prm->cmd->qpair, 0xe04d,
2376 "qla_target(%d): PCI mapping failed: sg_cnt=%d",
2377 0, prm->cmd->sg_cnt);
2381 static void qlt_unmap_sg(struct scsi_qla_host *vha, struct qla_tgt_cmd *cmd)
2383 struct qla_hw_data *ha;
2384 struct qla_qpair *qpair;
2385 if (!cmd->sg_mapped)
2390 pci_unmap_sg(qpair->pdev, cmd->sg, cmd->sg_cnt,
2391 cmd->dma_data_direction);
2394 if (cmd->prot_sg_cnt)
2395 pci_unmap_sg(qpair->pdev, cmd->prot_sg, cmd->prot_sg_cnt,
2396 cmd->dma_data_direction);
2401 if (cmd->ctx_dsd_alloced)
2402 qla2x00_clean_dsd_pool(ha, cmd->ctx);
2404 dma_pool_free(ha->dl_dma_pool, cmd->ctx, cmd->ctx->crc_ctx_dma);
2407 static int qlt_check_reserve_free_req(struct qla_qpair *qpair,
2411 struct req_que *req = qpair->req;
2413 if (req->cnt < (req_cnt + 2)) {
2414 cnt = (uint16_t)(qpair->use_shadow_reg ? *req->out_ptr :
2415 RD_REG_DWORD_RELAXED(req->req_q_out));
2417 if (req->ring_index < cnt)
2418 req->cnt = cnt - req->ring_index;
2420 req->cnt = req->length - (req->ring_index - cnt);
2422 if (unlikely(req->cnt < (req_cnt + 2)))
2426 req->cnt -= req_cnt;
2432 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
2434 static inline void *qlt_get_req_pkt(struct req_que *req)
2436 /* Adjust ring index. */
2438 if (req->ring_index == req->length) {
2439 req->ring_index = 0;
2440 req->ring_ptr = req->ring;
2444 return (cont_entry_t *)req->ring_ptr;
2447 /* ha->hardware_lock supposed to be held on entry */
2448 static inline uint32_t qlt_make_handle(struct qla_qpair *qpair)
2453 struct req_que *req = qpair->req;
2455 h = req->current_outstanding_cmd;
2457 for (index = 1; index < req->num_outstanding_cmds; index++) {
2459 if (h == req->num_outstanding_cmds)
2462 if (h == QLA_TGT_SKIP_HANDLE)
2465 if (!req->outstanding_cmds[h]) {
2472 req->current_outstanding_cmd = h;
2474 ql_dbg(ql_dbg_io, qpair->vha, 0x305b,
2475 "qla_target(%d): Ran out of empty cmd slots\n",
2476 qpair->vha->vp_idx);
2477 h = QLA_TGT_NULL_HANDLE;
2483 /* ha->hardware_lock supposed to be held on entry */
2484 static int qlt_24xx_build_ctio_pkt(struct qla_qpair *qpair,
2485 struct qla_tgt_prm *prm)
2488 struct ctio7_to_24xx *pkt;
2489 struct atio_from_isp *atio = &prm->cmd->atio;
2492 pkt = (struct ctio7_to_24xx *)qpair->req->ring_ptr;
2494 memset(pkt, 0, sizeof(*pkt));
2496 pkt->entry_type = CTIO_TYPE7;
2497 pkt->entry_count = (uint8_t)prm->req_cnt;
2498 pkt->vp_index = prm->cmd->vp_idx;
2500 h = qlt_make_handle(qpair);
2501 if (unlikely(h == QLA_TGT_NULL_HANDLE)) {
2503 * CTIO type 7 from the firmware doesn't provide a way to
2504 * know the initiator's LOOP ID, hence we can't find
2505 * the session and, so, the command.
2509 qpair->req->outstanding_cmds[h] = (srb_t *)prm->cmd;
2511 pkt->handle = MAKE_HANDLE(qpair->req->id, h);
2512 pkt->handle |= CTIO_COMPLETION_HANDLE_MARK;
2513 pkt->nport_handle = cpu_to_le16(prm->cmd->loop_id);
2514 pkt->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
2515 pkt->initiator_id[0] = atio->u.isp24.fcp_hdr.s_id[2];
2516 pkt->initiator_id[1] = atio->u.isp24.fcp_hdr.s_id[1];
2517 pkt->initiator_id[2] = atio->u.isp24.fcp_hdr.s_id[0];
2518 pkt->exchange_addr = atio->u.isp24.exchange_addr;
2519 temp = atio->u.isp24.attr << 9;
2520 pkt->u.status0.flags |= cpu_to_le16(temp);
2521 temp = be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id);
2522 pkt->u.status0.ox_id = cpu_to_le16(temp);
2523 pkt->u.status0.relative_offset = cpu_to_le32(prm->cmd->offset);
2529 * ha->hardware_lock supposed to be held on entry. We have already made sure
2530 * that there is sufficient amount of request entries to not drop it.
2532 static void qlt_load_cont_data_segments(struct qla_tgt_prm *prm)
2535 uint32_t *dword_ptr;
2537 /* Build continuation packets */
2538 while (prm->seg_cnt > 0) {
2539 cont_a64_entry_t *cont_pkt64 =
2540 (cont_a64_entry_t *)qlt_get_req_pkt(
2541 prm->cmd->qpair->req);
2544 * Make sure that from cont_pkt64 none of
2545 * 64-bit specific fields used for 32-bit
2546 * addressing. Cast to (cont_entry_t *) for
2550 memset(cont_pkt64, 0, sizeof(*cont_pkt64));
2552 cont_pkt64->entry_count = 1;
2553 cont_pkt64->sys_define = 0;
2555 cont_pkt64->entry_type = CONTINUE_A64_TYPE;
2556 dword_ptr = (uint32_t *)&cont_pkt64->dseg_0_address;
2558 /* Load continuation entry data segments */
2560 cnt < QLA_TGT_DATASEGS_PER_CONT_24XX && prm->seg_cnt;
2561 cnt++, prm->seg_cnt--) {
2563 cpu_to_le32(pci_dma_lo32
2564 (sg_dma_address(prm->sg)));
2565 *dword_ptr++ = cpu_to_le32(pci_dma_hi32
2566 (sg_dma_address(prm->sg)));
2567 *dword_ptr++ = cpu_to_le32(sg_dma_len(prm->sg));
2569 prm->sg = sg_next(prm->sg);
2575 * ha->hardware_lock supposed to be held on entry. We have already made sure
2576 * that there is sufficient amount of request entries to not drop it.
2578 static void qlt_load_data_segments(struct qla_tgt_prm *prm)
2581 uint32_t *dword_ptr;
2582 struct ctio7_to_24xx *pkt24 = (struct ctio7_to_24xx *)prm->pkt;
2584 pkt24->u.status0.transfer_length = cpu_to_le32(prm->cmd->bufflen);
2586 /* Setup packet address segment pointer */
2587 dword_ptr = pkt24->u.status0.dseg_0_address;
2589 /* Set total data segment count */
2591 pkt24->dseg_count = cpu_to_le16(prm->seg_cnt);
2593 if (prm->seg_cnt == 0) {
2594 /* No data transfer */
2600 /* If scatter gather */
2602 /* Load command entry data segments */
2604 (cnt < QLA_TGT_DATASEGS_PER_CMD_24XX) && prm->seg_cnt;
2605 cnt++, prm->seg_cnt--) {
2607 cpu_to_le32(pci_dma_lo32(sg_dma_address(prm->sg)));
2609 *dword_ptr++ = cpu_to_le32(pci_dma_hi32(
2610 sg_dma_address(prm->sg)));
2612 *dword_ptr++ = cpu_to_le32(sg_dma_len(prm->sg));
2614 prm->sg = sg_next(prm->sg);
2617 qlt_load_cont_data_segments(prm);
2620 static inline int qlt_has_data(struct qla_tgt_cmd *cmd)
2622 return cmd->bufflen > 0;
2625 static void qlt_print_dif_err(struct qla_tgt_prm *prm)
2627 struct qla_tgt_cmd *cmd;
2628 struct scsi_qla_host *vha;
2630 /* asc 0x10=dif error */
2631 if (prm->sense_buffer && (prm->sense_buffer[12] == 0x10)) {
2635 switch (prm->sense_buffer[13]) {
2637 ql_dbg(ql_dbg_tgt_dif, vha, 0xe00b,
2638 "BE detected Guard TAG ERR: lba[0x%llx|%lld] len[0x%x] "
2639 "se_cmd=%p tag[%x]",
2640 cmd->lba, cmd->lba, cmd->num_blks, &cmd->se_cmd,
2641 cmd->atio.u.isp24.exchange_addr);
2644 ql_dbg(ql_dbg_tgt_dif, vha, 0xe00c,
2645 "BE detected APP TAG ERR: lba[0x%llx|%lld] len[0x%x] "
2646 "se_cmd=%p tag[%x]",
2647 cmd->lba, cmd->lba, cmd->num_blks, &cmd->se_cmd,
2648 cmd->atio.u.isp24.exchange_addr);
2651 ql_dbg(ql_dbg_tgt_dif, vha, 0xe00f,
2652 "BE detected REF TAG ERR: lba[0x%llx|%lld] len[0x%x] "
2653 "se_cmd=%p tag[%x]",
2654 cmd->lba, cmd->lba, cmd->num_blks, &cmd->se_cmd,
2655 cmd->atio.u.isp24.exchange_addr);
2658 ql_dbg(ql_dbg_tgt_dif, vha, 0xe010,
2659 "BE detected Dif ERR: lba[%llx|%lld] len[%x] "
2660 "se_cmd=%p tag[%x]",
2661 cmd->lba, cmd->lba, cmd->num_blks, &cmd->se_cmd,
2662 cmd->atio.u.isp24.exchange_addr);
2665 ql_dump_buffer(ql_dbg_tgt_dif, vha, 0xe011, cmd->cdb, 16);
2670 * Called without ha->hardware_lock held
2672 static int qlt_pre_xmit_response(struct qla_tgt_cmd *cmd,
2673 struct qla_tgt_prm *prm, int xmit_type, uint8_t scsi_status,
2674 uint32_t *full_req_cnt)
2676 struct se_cmd *se_cmd = &cmd->se_cmd;
2677 struct qla_qpair *qpair = cmd->qpair;
2680 prm->tgt = cmd->tgt;
2682 prm->rq_result = scsi_status;
2683 prm->sense_buffer = &cmd->sense_buffer[0];
2684 prm->sense_buffer_len = TRANSPORT_SENSE_BUFFER;
2689 prm->add_status_pkt = 0;
2690 prm->prot_sg = NULL;
2691 prm->prot_seg_cnt = 0;
2694 if ((xmit_type & QLA_TGT_XMIT_DATA) && qlt_has_data(cmd)) {
2695 if (qlt_pci_map_calc_cnt(prm) != 0)
2699 *full_req_cnt = prm->req_cnt;
2701 if (se_cmd->se_cmd_flags & SCF_UNDERFLOW_BIT) {
2702 prm->residual = se_cmd->residual_count;
2703 ql_dbg_qp(ql_dbg_io + ql_dbg_verbose, qpair, 0x305c,
2704 "Residual underflow: %d (tag %lld, op %x, bufflen %d, rq_result %x)\n",
2705 prm->residual, se_cmd->tag,
2706 se_cmd->t_task_cdb ? se_cmd->t_task_cdb[0] : 0,
2707 cmd->bufflen, prm->rq_result);
2708 prm->rq_result |= SS_RESIDUAL_UNDER;
2709 } else if (se_cmd->se_cmd_flags & SCF_OVERFLOW_BIT) {
2710 prm->residual = se_cmd->residual_count;
2711 ql_dbg_qp(ql_dbg_io, qpair, 0x305d,
2712 "Residual overflow: %d (tag %lld, op %x, bufflen %d, rq_result %x)\n",
2713 prm->residual, se_cmd->tag, se_cmd->t_task_cdb ?
2714 se_cmd->t_task_cdb[0] : 0, cmd->bufflen, prm->rq_result);
2715 prm->rq_result |= SS_RESIDUAL_OVER;
2718 if (xmit_type & QLA_TGT_XMIT_STATUS) {
2720 * If QLA_TGT_XMIT_DATA is not set, add_status_pkt will be
2721 * ignored in *xmit_response() below
2723 if (qlt_has_data(cmd)) {
2724 if (QLA_TGT_SENSE_VALID(prm->sense_buffer) ||
2725 (IS_FWI2_CAPABLE(cmd->vha->hw) &&
2726 (prm->rq_result != 0))) {
2727 prm->add_status_pkt = 1;
2736 static inline int qlt_need_explicit_conf(struct qla_tgt_cmd *cmd,
2739 if (cmd->qpair->enable_class_2)
2743 return cmd->conf_compl_supported;
2745 return cmd->qpair->enable_explicit_conf &&
2746 cmd->conf_compl_supported;
2749 static void qlt_24xx_init_ctio_to_isp(struct ctio7_to_24xx *ctio,
2750 struct qla_tgt_prm *prm)
2752 prm->sense_buffer_len = min_t(uint32_t, prm->sense_buffer_len,
2753 (uint32_t)sizeof(ctio->u.status1.sense_data));
2754 ctio->u.status0.flags |= cpu_to_le16(CTIO7_FLAGS_SEND_STATUS);
2755 if (qlt_need_explicit_conf(prm->cmd, 0)) {
2756 ctio->u.status0.flags |= cpu_to_le16(
2757 CTIO7_FLAGS_EXPLICIT_CONFORM |
2758 CTIO7_FLAGS_CONFORM_REQ);
2760 ctio->u.status0.residual = cpu_to_le32(prm->residual);
2761 ctio->u.status0.scsi_status = cpu_to_le16(prm->rq_result);
2762 if (QLA_TGT_SENSE_VALID(prm->sense_buffer)) {
2765 if (qlt_need_explicit_conf(prm->cmd, 1)) {
2766 if ((prm->rq_result & SS_SCSI_STATUS_BYTE) != 0) {
2767 ql_dbg_qp(ql_dbg_tgt, prm->cmd->qpair, 0xe017,
2768 "Skipping EXPLICIT_CONFORM and "
2769 "CTIO7_FLAGS_CONFORM_REQ for FCP READ w/ "
2770 "non GOOD status\n");
2771 goto skip_explict_conf;
2773 ctio->u.status1.flags |= cpu_to_le16(
2774 CTIO7_FLAGS_EXPLICIT_CONFORM |
2775 CTIO7_FLAGS_CONFORM_REQ);
2778 ctio->u.status1.flags &=
2779 ~cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_0);
2780 ctio->u.status1.flags |=
2781 cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_1);
2782 ctio->u.status1.scsi_status |=
2783 cpu_to_le16(SS_SENSE_LEN_VALID);
2784 ctio->u.status1.sense_length =
2785 cpu_to_le16(prm->sense_buffer_len);
2786 for (i = 0; i < prm->sense_buffer_len/4; i++)
2787 ((uint32_t *)ctio->u.status1.sense_data)[i] =
2788 cpu_to_be32(((uint32_t *)prm->sense_buffer)[i]);
2790 qlt_print_dif_err(prm);
2793 ctio->u.status1.flags &=
2794 ~cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_0);
2795 ctio->u.status1.flags |=
2796 cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_1);
2797 ctio->u.status1.sense_length = 0;
2798 memset(ctio->u.status1.sense_data, 0,
2799 sizeof(ctio->u.status1.sense_data));
2802 /* Sense with len > 24, is it possible ??? */
2806 qlt_hba_err_chk_enabled(struct se_cmd *se_cmd)
2808 switch (se_cmd->prot_op) {
2809 case TARGET_PROT_DOUT_INSERT:
2810 case TARGET_PROT_DIN_STRIP:
2811 if (ql2xenablehba_err_chk >= 1)
2814 case TARGET_PROT_DOUT_PASS:
2815 case TARGET_PROT_DIN_PASS:
2816 if (ql2xenablehba_err_chk >= 2)
2819 case TARGET_PROT_DIN_INSERT:
2820 case TARGET_PROT_DOUT_STRIP:
2829 qla_tgt_ref_mask_check(struct se_cmd *se_cmd)
2831 switch (se_cmd->prot_op) {
2832 case TARGET_PROT_DIN_INSERT:
2833 case TARGET_PROT_DOUT_INSERT:
2834 case TARGET_PROT_DIN_STRIP:
2835 case TARGET_PROT_DOUT_STRIP:
2836 case TARGET_PROT_DIN_PASS:
2837 case TARGET_PROT_DOUT_PASS:
2846 * qla_tgt_set_dif_tags - Extract Ref and App tags from SCSI command
2849 qla_tgt_set_dif_tags(struct qla_tgt_cmd *cmd, struct crc_context *ctx,
2850 uint16_t *pfw_prot_opts)
2852 struct se_cmd *se_cmd = &cmd->se_cmd;
2853 uint32_t lba = 0xffffffff & se_cmd->t_task_lba;
2854 scsi_qla_host_t *vha = cmd->tgt->vha;
2855 struct qla_hw_data *ha = vha->hw;
2859 * wait till Mode Sense/Select cmd, modepage Ah, subpage 2
2860 * have been immplemented by TCM, before AppTag is avail.
2861 * Look for modesense_handlers[]
2864 ctx->app_tag_mask[0] = 0x0;
2865 ctx->app_tag_mask[1] = 0x0;
2867 if (IS_PI_UNINIT_CAPABLE(ha)) {
2868 if ((se_cmd->prot_type == TARGET_DIF_TYPE1_PROT) ||
2869 (se_cmd->prot_type == TARGET_DIF_TYPE2_PROT))
2870 *pfw_prot_opts |= PO_DIS_VALD_APP_ESC;
2871 else if (se_cmd->prot_type == TARGET_DIF_TYPE3_PROT)
2872 *pfw_prot_opts |= PO_DIS_VALD_APP_REF_ESC;
2875 t32 = ha->tgt.tgt_ops->get_dif_tags(cmd, pfw_prot_opts);
2877 switch (se_cmd->prot_type) {
2878 case TARGET_DIF_TYPE0_PROT:
2880 * No check for ql2xenablehba_err_chk, as it
2881 * would be an I/O error if hba tag generation
2884 ctx->ref_tag = cpu_to_le32(lba);
2885 /* enable ALL bytes of the ref tag */
2886 ctx->ref_tag_mask[0] = 0xff;
2887 ctx->ref_tag_mask[1] = 0xff;
2888 ctx->ref_tag_mask[2] = 0xff;
2889 ctx->ref_tag_mask[3] = 0xff;
2891 case TARGET_DIF_TYPE1_PROT:
2893 * For TYPE 1 protection: 16 bit GUARD tag, 32 bit
2894 * REF tag, and 16 bit app tag.
2896 ctx->ref_tag = cpu_to_le32(lba);
2897 if (!qla_tgt_ref_mask_check(se_cmd) ||
2898 !(ha->tgt.tgt_ops->chk_dif_tags(t32))) {
2899 *pfw_prot_opts |= PO_DIS_REF_TAG_VALD;
2902 /* enable ALL bytes of the ref tag */
2903 ctx->ref_tag_mask[0] = 0xff;
2904 ctx->ref_tag_mask[1] = 0xff;
2905 ctx->ref_tag_mask[2] = 0xff;
2906 ctx->ref_tag_mask[3] = 0xff;
2908 case TARGET_DIF_TYPE2_PROT:
2910 * For TYPE 2 protection: 16 bit GUARD + 32 bit REF
2911 * tag has to match LBA in CDB + N
2913 ctx->ref_tag = cpu_to_le32(lba);
2914 if (!qla_tgt_ref_mask_check(se_cmd) ||
2915 !(ha->tgt.tgt_ops->chk_dif_tags(t32))) {
2916 *pfw_prot_opts |= PO_DIS_REF_TAG_VALD;
2919 /* enable ALL bytes of the ref tag */
2920 ctx->ref_tag_mask[0] = 0xff;
2921 ctx->ref_tag_mask[1] = 0xff;
2922 ctx->ref_tag_mask[2] = 0xff;
2923 ctx->ref_tag_mask[3] = 0xff;
2925 case TARGET_DIF_TYPE3_PROT:
2926 /* For TYPE 3 protection: 16 bit GUARD only */
2927 *pfw_prot_opts |= PO_DIS_REF_TAG_VALD;
2928 ctx->ref_tag_mask[0] = ctx->ref_tag_mask[1] =
2929 ctx->ref_tag_mask[2] = ctx->ref_tag_mask[3] = 0x00;
2935 qlt_build_ctio_crc2_pkt(struct qla_qpair *qpair, struct qla_tgt_prm *prm)
2938 uint32_t transfer_length = 0;
2939 uint32_t data_bytes;
2941 uint8_t bundling = 1;
2942 struct crc_context *crc_ctx_pkt = NULL;
2943 struct qla_hw_data *ha;
2944 struct ctio_crc2_to_fw *pkt;
2945 dma_addr_t crc_ctx_dma;
2946 uint16_t fw_prot_opts = 0;
2947 struct qla_tgt_cmd *cmd = prm->cmd;
2948 struct se_cmd *se_cmd = &cmd->se_cmd;
2950 struct atio_from_isp *atio = &prm->cmd->atio;
2951 struct qla_tc_param tc;
2953 scsi_qla_host_t *vha = cmd->vha;
2957 pkt = (struct ctio_crc2_to_fw *)qpair->req->ring_ptr;
2959 memset(pkt, 0, sizeof(*pkt));
2961 ql_dbg_qp(ql_dbg_tgt, cmd->qpair, 0xe071,
2962 "qla_target(%d):%s: se_cmd[%p] CRC2 prot_op[0x%x] cmd prot sg:cnt[%p:%x] lba[%llu]\n",
2963 cmd->vp_idx, __func__, se_cmd, se_cmd->prot_op,
2964 prm->prot_sg, prm->prot_seg_cnt, se_cmd->t_task_lba);
2966 if ((se_cmd->prot_op == TARGET_PROT_DIN_INSERT) ||
2967 (se_cmd->prot_op == TARGET_PROT_DOUT_STRIP))
2970 /* Compute dif len and adjust data len to incude protection */
2971 data_bytes = cmd->bufflen;
2972 dif_bytes = (data_bytes / cmd->blk_sz) * 8;
2974 switch (se_cmd->prot_op) {
2975 case TARGET_PROT_DIN_INSERT:
2976 case TARGET_PROT_DOUT_STRIP:
2977 transfer_length = data_bytes;
2978 if (cmd->prot_sg_cnt)
2979 data_bytes += dif_bytes;
2981 case TARGET_PROT_DIN_STRIP:
2982 case TARGET_PROT_DOUT_INSERT:
2983 case TARGET_PROT_DIN_PASS:
2984 case TARGET_PROT_DOUT_PASS:
2985 transfer_length = data_bytes + dif_bytes;
2992 if (!qlt_hba_err_chk_enabled(se_cmd))
2993 fw_prot_opts |= 0x10; /* Disable Guard tag checking */
2994 /* HBA error checking enabled */
2995 else if (IS_PI_UNINIT_CAPABLE(ha)) {
2996 if ((se_cmd->prot_type == TARGET_DIF_TYPE1_PROT) ||
2997 (se_cmd->prot_type == TARGET_DIF_TYPE2_PROT))
2998 fw_prot_opts |= PO_DIS_VALD_APP_ESC;
2999 else if (se_cmd->prot_type == TARGET_DIF_TYPE3_PROT)
3000 fw_prot_opts |= PO_DIS_VALD_APP_REF_ESC;
3003 switch (se_cmd->prot_op) {
3004 case TARGET_PROT_DIN_INSERT:
3005 case TARGET_PROT_DOUT_INSERT:
3006 fw_prot_opts |= PO_MODE_DIF_INSERT;
3008 case TARGET_PROT_DIN_STRIP:
3009 case TARGET_PROT_DOUT_STRIP:
3010 fw_prot_opts |= PO_MODE_DIF_REMOVE;
3012 case TARGET_PROT_DIN_PASS:
3013 case TARGET_PROT_DOUT_PASS:
3014 fw_prot_opts |= PO_MODE_DIF_PASS;
3015 /* FUTURE: does tcm require T10CRC<->IPCKSUM conversion? */
3017 default:/* Normal Request */
3018 fw_prot_opts |= PO_MODE_DIF_PASS;
3023 /* Update entry type to indicate Command Type CRC_2 IOCB */
3024 pkt->entry_type = CTIO_CRC2;
3025 pkt->entry_count = 1;
3026 pkt->vp_index = cmd->vp_idx;
3028 h = qlt_make_handle(qpair);
3029 if (unlikely(h == QLA_TGT_NULL_HANDLE)) {
3031 * CTIO type 7 from the firmware doesn't provide a way to
3032 * know the initiator's LOOP ID, hence we can't find
3033 * the session and, so, the command.
3037 qpair->req->outstanding_cmds[h] = (srb_t *)prm->cmd;
3039 pkt->handle = MAKE_HANDLE(qpair->req->id, h);
3040 pkt->handle |= CTIO_COMPLETION_HANDLE_MARK;
3041 pkt->nport_handle = cpu_to_le16(prm->cmd->loop_id);
3042 pkt->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
3043 pkt->initiator_id[0] = atio->u.isp24.fcp_hdr.s_id[2];
3044 pkt->initiator_id[1] = atio->u.isp24.fcp_hdr.s_id[1];
3045 pkt->initiator_id[2] = atio->u.isp24.fcp_hdr.s_id[0];
3046 pkt->exchange_addr = atio->u.isp24.exchange_addr;
3048 /* silence compile warning */
3049 t16 = be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id);
3050 pkt->ox_id = cpu_to_le16(t16);
3052 t16 = (atio->u.isp24.attr << 9);
3053 pkt->flags |= cpu_to_le16(t16);
3054 pkt->relative_offset = cpu_to_le32(prm->cmd->offset);
3056 /* Set transfer direction */
3057 if (cmd->dma_data_direction == DMA_TO_DEVICE)
3058 pkt->flags = cpu_to_le16(CTIO7_FLAGS_DATA_IN);
3059 else if (cmd->dma_data_direction == DMA_FROM_DEVICE)
3060 pkt->flags = cpu_to_le16(CTIO7_FLAGS_DATA_OUT);
3062 pkt->dseg_count = prm->tot_dsds;
3063 /* Fibre channel byte count */
3064 pkt->transfer_length = cpu_to_le32(transfer_length);
3066 /* ----- CRC context -------- */
3068 /* Allocate CRC context from global pool */
3069 crc_ctx_pkt = cmd->ctx =
3070 dma_pool_zalloc(ha->dl_dma_pool, GFP_ATOMIC, &crc_ctx_dma);
3073 goto crc_queuing_error;
3075 crc_ctx_pkt->crc_ctx_dma = crc_ctx_dma;
3076 INIT_LIST_HEAD(&crc_ctx_pkt->dsd_list);
3079 crc_ctx_pkt->handle = pkt->handle;
3081 qla_tgt_set_dif_tags(cmd, crc_ctx_pkt, &fw_prot_opts);
3083 pkt->crc_context_address[0] = cpu_to_le32(LSD(crc_ctx_dma));
3084 pkt->crc_context_address[1] = cpu_to_le32(MSD(crc_ctx_dma));
3085 pkt->crc_context_len = CRC_CONTEXT_LEN_FW;
3088 cur_dsd = (uint32_t *) &crc_ctx_pkt->u.nobundling.data_address;
3091 * Configure Bundling if we need to fetch interlaving
3092 * protection PCI accesses
3094 fw_prot_opts |= PO_ENABLE_DIF_BUNDLING;
3095 crc_ctx_pkt->u.bundling.dif_byte_count = cpu_to_le32(dif_bytes);
3096 crc_ctx_pkt->u.bundling.dseg_count =
3097 cpu_to_le16(prm->tot_dsds - prm->prot_seg_cnt);
3098 cur_dsd = (uint32_t *) &crc_ctx_pkt->u.bundling.data_address;
3101 /* Finish the common fields of CRC pkt */
3102 crc_ctx_pkt->blk_size = cpu_to_le16(cmd->blk_sz);
3103 crc_ctx_pkt->prot_opts = cpu_to_le16(fw_prot_opts);
3104 crc_ctx_pkt->byte_count = cpu_to_le32(data_bytes);
3105 crc_ctx_pkt->guard_seed = cpu_to_le16(0);
3107 memset((uint8_t *)&tc, 0 , sizeof(tc));
3109 tc.blk_sz = cmd->blk_sz;
3110 tc.bufflen = cmd->bufflen;
3112 tc.prot_sg = cmd->prot_sg;
3113 tc.ctx = crc_ctx_pkt;
3114 tc.ctx_dsd_alloced = &cmd->ctx_dsd_alloced;
3116 /* Walks data segments */
3117 pkt->flags |= cpu_to_le16(CTIO7_FLAGS_DSD_PTR);
3119 if (!bundling && prm->prot_seg_cnt) {
3120 if (qla24xx_walk_and_build_sglist_no_difb(ha, NULL, cur_dsd,
3121 prm->tot_dsds, &tc))
3122 goto crc_queuing_error;
3123 } else if (qla24xx_walk_and_build_sglist(ha, NULL, cur_dsd,
3124 (prm->tot_dsds - prm->prot_seg_cnt), &tc))
3125 goto crc_queuing_error;
3127 if (bundling && prm->prot_seg_cnt) {
3128 /* Walks dif segments */
3129 pkt->add_flags |= CTIO_CRC2_AF_DIF_DSD_ENA;
3131 cur_dsd = (uint32_t *) &crc_ctx_pkt->u.bundling.dif_address;
3132 if (qla24xx_walk_and_build_prot_sglist(ha, NULL, cur_dsd,
3133 prm->prot_seg_cnt, &tc))
3134 goto crc_queuing_error;
3139 /* Cleanup will be performed by the caller */
3140 qpair->req->outstanding_cmds[h] = NULL;
3142 return QLA_FUNCTION_FAILED;
3146 * Callback to setup response of xmit_type of QLA_TGT_XMIT_DATA and *
3147 * QLA_TGT_XMIT_STATUS for >= 24xx silicon
3149 int qlt_xmit_response(struct qla_tgt_cmd *cmd, int xmit_type,
3150 uint8_t scsi_status)
3152 struct scsi_qla_host *vha = cmd->vha;
3153 struct qla_qpair *qpair = cmd->qpair;
3154 struct ctio7_to_24xx *pkt;
3155 struct qla_tgt_prm prm;
3156 uint32_t full_req_cnt = 0;
3157 unsigned long flags = 0;
3160 if (cmd->sess && cmd->sess->deleted) {
3161 cmd->state = QLA_TGT_STATE_PROCESSED;
3162 if (cmd->sess->logout_completed)
3163 /* no need to terminate. FW already freed exchange. */
3164 qlt_abort_cmd_on_host_reset(cmd->vha, cmd);
3166 qlt_send_term_exchange(qpair, cmd, &cmd->atio, 0, 0);
3170 ql_dbg_qp(ql_dbg_tgt, qpair, 0xe018,
3171 "is_send_status=%d, cmd->bufflen=%d, cmd->sg_cnt=%d, cmd->dma_data_direction=%d se_cmd[%p] qp %d\n",
3172 (xmit_type & QLA_TGT_XMIT_STATUS) ?
3173 1 : 0, cmd->bufflen, cmd->sg_cnt, cmd->dma_data_direction,
3174 &cmd->se_cmd, qpair->id);
3176 res = qlt_pre_xmit_response(cmd, &prm, xmit_type, scsi_status,
3178 if (unlikely(res != 0)) {
3182 spin_lock_irqsave(qpair->qp_lock_ptr, flags);
3184 if (xmit_type == QLA_TGT_XMIT_STATUS)
3185 qpair->tgt_counters.core_qla_snd_status++;
3187 qpair->tgt_counters.core_qla_que_buf++;
3189 if (!qpair->fw_started || cmd->reset_count != qpair->chip_reset) {
3191 * Either the port is not online or this request was from
3192 * previous life, just abort the processing.
3194 cmd->state = QLA_TGT_STATE_PROCESSED;
3195 qlt_abort_cmd_on_host_reset(cmd->vha, cmd);
3196 ql_dbg_qp(ql_dbg_async, qpair, 0xe101,
3197 "RESET-RSP online/active/old-count/new-count = %d/%d/%d/%d.\n",
3198 vha->flags.online, qla2x00_reset_active(vha),
3199 cmd->reset_count, qpair->chip_reset);
3200 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
3204 /* Does F/W have an IOCBs for this request */
3205 res = qlt_check_reserve_free_req(qpair, full_req_cnt);
3207 goto out_unmap_unlock;
3209 if (cmd->se_cmd.prot_op && (xmit_type & QLA_TGT_XMIT_DATA))
3210 res = qlt_build_ctio_crc2_pkt(qpair, &prm);
3212 res = qlt_24xx_build_ctio_pkt(qpair, &prm);
3213 if (unlikely(res != 0)) {
3214 qpair->req->cnt += full_req_cnt;
3215 goto out_unmap_unlock;
3218 pkt = (struct ctio7_to_24xx *)prm.pkt;
3220 if (qlt_has_data(cmd) && (xmit_type & QLA_TGT_XMIT_DATA)) {
3221 pkt->u.status0.flags |=
3222 cpu_to_le16(CTIO7_FLAGS_DATA_IN |
3223 CTIO7_FLAGS_STATUS_MODE_0);
3225 if (cmd->se_cmd.prot_op == TARGET_PROT_NORMAL)
3226 qlt_load_data_segments(&prm);
3228 if (prm.add_status_pkt == 0) {
3229 if (xmit_type & QLA_TGT_XMIT_STATUS) {
3230 pkt->u.status0.scsi_status =
3231 cpu_to_le16(prm.rq_result);
3232 pkt->u.status0.residual =
3233 cpu_to_le32(prm.residual);
3234 pkt->u.status0.flags |= cpu_to_le16(
3235 CTIO7_FLAGS_SEND_STATUS);
3236 if (qlt_need_explicit_conf(cmd, 0)) {
3237 pkt->u.status0.flags |=
3239 CTIO7_FLAGS_EXPLICIT_CONFORM |
3240 CTIO7_FLAGS_CONFORM_REQ);
3246 * We have already made sure that there is sufficient
3247 * amount of request entries to not drop HW lock in
3250 struct ctio7_to_24xx *ctio =
3251 (struct ctio7_to_24xx *)qlt_get_req_pkt(
3254 ql_dbg_qp(ql_dbg_tgt, qpair, 0x305e,
3255 "Building additional status packet 0x%p.\n",
3259 * T10Dif: ctio_crc2_to_fw overlay ontop of
3262 memcpy(ctio, pkt, sizeof(*ctio));
3263 /* reset back to CTIO7 */
3264 ctio->entry_count = 1;
3265 ctio->entry_type = CTIO_TYPE7;
3266 ctio->dseg_count = 0;
3267 ctio->u.status1.flags &= ~cpu_to_le16(
3268 CTIO7_FLAGS_DATA_IN);
3270 /* Real finish is ctio_m1's finish */
3271 pkt->handle |= CTIO_INTERMEDIATE_HANDLE_MARK;
3272 pkt->u.status0.flags |= cpu_to_le16(
3273 CTIO7_FLAGS_DONT_RET_CTIO);
3275 /* qlt_24xx_init_ctio_to_isp will correct
3276 * all neccessary fields that's part of CTIO7.
3277 * There should be no residual of CTIO-CRC2 data.
3279 qlt_24xx_init_ctio_to_isp((struct ctio7_to_24xx *)ctio,
3283 qlt_24xx_init_ctio_to_isp(pkt, &prm);
3286 cmd->state = QLA_TGT_STATE_PROCESSED; /* Mid-level is done processing */
3287 cmd->cmd_sent_to_fw = 1;
3289 /* Memory Barrier */
3291 if (qpair->reqq_start_iocbs)
3292 qpair->reqq_start_iocbs(qpair);
3294 qla2x00_start_iocbs(vha, qpair->req);
3295 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
3300 qlt_unmap_sg(vha, cmd);
3301 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
3305 EXPORT_SYMBOL(qlt_xmit_response);
3307 int qlt_rdy_to_xfer(struct qla_tgt_cmd *cmd)
3309 struct ctio7_to_24xx *pkt;
3310 struct scsi_qla_host *vha = cmd->vha;
3311 struct qla_tgt *tgt = cmd->tgt;
3312 struct qla_tgt_prm prm;
3313 unsigned long flags = 0;
3315 struct qla_qpair *qpair = cmd->qpair;
3317 memset(&prm, 0, sizeof(prm));
3323 /* Calculate number of entries and segments required */
3324 if (qlt_pci_map_calc_cnt(&prm) != 0)
3327 if (!qpair->fw_started || (cmd->reset_count != qpair->chip_reset) ||
3328 (cmd->sess && cmd->sess->deleted)) {
3330 * Either the port is not online or this request was from
3331 * previous life, just abort the processing.
3333 cmd->state = QLA_TGT_STATE_NEED_DATA;
3334 qlt_abort_cmd_on_host_reset(cmd->vha, cmd);
3335 ql_dbg_qp(ql_dbg_async, qpair, 0xe102,
3336 "RESET-XFR online/active/old-count/new-count = %d/%d/%d/%d.\n",
3337 vha->flags.online, qla2x00_reset_active(vha),
3338 cmd->reset_count, qpair->chip_reset);
3342 spin_lock_irqsave(qpair->qp_lock_ptr, flags);
3343 /* Does F/W have an IOCBs for this request */
3344 res = qlt_check_reserve_free_req(qpair, prm.req_cnt);
3346 goto out_unlock_free_unmap;
3347 if (cmd->se_cmd.prot_op)
3348 res = qlt_build_ctio_crc2_pkt(qpair, &prm);
3350 res = qlt_24xx_build_ctio_pkt(qpair, &prm);
3352 if (unlikely(res != 0)) {
3353 qpair->req->cnt += prm.req_cnt;
3354 goto out_unlock_free_unmap;
3357 pkt = (struct ctio7_to_24xx *)prm.pkt;
3358 pkt->u.status0.flags |= cpu_to_le16(CTIO7_FLAGS_DATA_OUT |
3359 CTIO7_FLAGS_STATUS_MODE_0);
3361 if (cmd->se_cmd.prot_op == TARGET_PROT_NORMAL)
3362 qlt_load_data_segments(&prm);
3364 cmd->state = QLA_TGT_STATE_NEED_DATA;
3365 cmd->cmd_sent_to_fw = 1;
3367 /* Memory Barrier */
3369 if (qpair->reqq_start_iocbs)
3370 qpair->reqq_start_iocbs(qpair);
3372 qla2x00_start_iocbs(vha, qpair->req);
3373 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
3377 out_unlock_free_unmap:
3378 qlt_unmap_sg(vha, cmd);
3379 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
3383 EXPORT_SYMBOL(qlt_rdy_to_xfer);
3387 * it is assumed either hardware_lock or qpair lock is held.
3390 qlt_handle_dif_error(struct qla_qpair *qpair, struct qla_tgt_cmd *cmd,
3391 struct ctio_crc_from_fw *sts)
3393 uint8_t *ap = &sts->actual_dif[0];
3394 uint8_t *ep = &sts->expected_dif[0];
3395 uint64_t lba = cmd->se_cmd.t_task_lba;
3396 uint8_t scsi_status, sense_key, asc, ascq;
3397 unsigned long flags;
3398 struct scsi_qla_host *vha = cmd->vha;
3400 cmd->trc_flags |= TRC_DIF_ERR;
3402 cmd->a_guard = be16_to_cpu(*(uint16_t *)(ap + 0));
3403 cmd->a_app_tag = be16_to_cpu(*(uint16_t *)(ap + 2));
3404 cmd->a_ref_tag = be32_to_cpu(*(uint32_t *)(ap + 4));
3406 cmd->e_guard = be16_to_cpu(*(uint16_t *)(ep + 0));
3407 cmd->e_app_tag = be16_to_cpu(*(uint16_t *)(ep + 2));
3408 cmd->e_ref_tag = be32_to_cpu(*(uint32_t *)(ep + 4));
3410 ql_dbg(ql_dbg_tgt_dif, vha, 0xf075,
3411 "%s: aborted %d state %d\n", __func__, cmd->aborted, cmd->state);
3413 scsi_status = sense_key = asc = ascq = 0;
3415 /* check appl tag */
3416 if (cmd->e_app_tag != cmd->a_app_tag) {
3417 ql_dbg(ql_dbg_tgt_dif, vha, 0xe00d,
3418 "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]",
3419 cmd->cdb[0], lba, (lba+cmd->num_blks), cmd->num_blks,
3420 cmd->a_ref_tag, cmd->e_ref_tag, cmd->a_app_tag,
3421 cmd->e_app_tag, cmd->a_guard, cmd->e_guard, cmd,
3422 cmd->atio.u.isp24.fcp_hdr.ox_id);
3424 cmd->dif_err_code = DIF_ERR_APP;
3425 scsi_status = SAM_STAT_CHECK_CONDITION;
3426 sense_key = ABORTED_COMMAND;
3432 if (cmd->e_ref_tag != cmd->a_ref_tag) {
3433 ql_dbg(ql_dbg_tgt_dif, vha, 0xe00e,
3434 "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] ",
3435 cmd->cdb[0], lba, (lba+cmd->num_blks), cmd->num_blks,
3436 cmd->a_ref_tag, cmd->e_ref_tag, cmd->a_app_tag,
3437 cmd->e_app_tag, cmd->a_guard, cmd->e_guard, cmd,
3438 cmd->atio.u.isp24.fcp_hdr.ox_id);
3440 cmd->dif_err_code = DIF_ERR_REF;
3441 scsi_status = SAM_STAT_CHECK_CONDITION;
3442 sense_key = ABORTED_COMMAND;
3449 if (cmd->e_guard != cmd->a_guard) {
3450 ql_dbg(ql_dbg_tgt_dif, vha, 0xe012,
3451 "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]",
3452 cmd->cdb[0], lba, (lba+cmd->num_blks), cmd->num_blks,
3453 cmd->a_ref_tag, cmd->e_ref_tag, cmd->a_app_tag,
3454 cmd->e_app_tag, cmd->a_guard, cmd->e_guard, cmd,
3455 cmd->atio.u.isp24.fcp_hdr.ox_id);
3457 cmd->dif_err_code = DIF_ERR_GRD;
3458 scsi_status = SAM_STAT_CHECK_CONDITION;
3459 sense_key = ABORTED_COMMAND;
3464 switch (cmd->state) {
3465 case QLA_TGT_STATE_NEED_DATA:
3466 /* handle_data will load DIF error code */
3467 cmd->state = QLA_TGT_STATE_DATA_IN;
3468 vha->hw->tgt.tgt_ops->handle_data(cmd);
3471 spin_lock_irqsave(&cmd->cmd_lock, flags);
3473 spin_unlock_irqrestore(&cmd->cmd_lock, flags);
3474 vha->hw->tgt.tgt_ops->free_cmd(cmd);
3477 spin_unlock_irqrestore(&cmd->cmd_lock, flags);
3479 qlt_send_resp_ctio(qpair, cmd, scsi_status, sense_key, asc,
3481 /* assume scsi status gets out on the wire.
3482 * Will not wait for completion.
3484 vha->hw->tgt.tgt_ops->free_cmd(cmd);
3489 /* If hardware_lock held on entry, might drop it, then reaquire */
3490 /* This function sends the appropriate CTIO to ISP 2xxx or 24xx */
3491 static int __qlt_send_term_imm_notif(struct scsi_qla_host *vha,
3492 struct imm_ntfy_from_isp *ntfy)
3494 struct nack_to_isp *nack;
3495 struct qla_hw_data *ha = vha->hw;
3499 ql_dbg(ql_dbg_tgt_tmr, vha, 0xe01c,
3500 "Sending TERM ELS CTIO (ha=%p)\n", ha);
3502 pkt = (request_t *)qla2x00_alloc_iocbs(vha, NULL);
3504 ql_dbg(ql_dbg_tgt, vha, 0xe080,
3505 "qla_target(%d): %s failed: unable to allocate "
3506 "request packet\n", vha->vp_idx, __func__);
3510 pkt->entry_type = NOTIFY_ACK_TYPE;
3511 pkt->entry_count = 1;
3512 pkt->handle = QLA_TGT_SKIP_HANDLE;
3514 nack = (struct nack_to_isp *)pkt;
3515 nack->ox_id = ntfy->ox_id;
3517 nack->u.isp24.nport_handle = ntfy->u.isp24.nport_handle;
3518 if (le16_to_cpu(ntfy->u.isp24.status) == IMM_NTFY_ELS) {
3519 nack->u.isp24.flags = ntfy->u.isp24.flags &
3520 __constant_cpu_to_le32(NOTIFY24XX_FLAGS_PUREX_IOCB);
3524 nack->u.isp24.flags |=
3525 __constant_cpu_to_le16(NOTIFY_ACK_FLAGS_TERMINATE);
3527 nack->u.isp24.srr_rx_id = ntfy->u.isp24.srr_rx_id;
3528 nack->u.isp24.status = ntfy->u.isp24.status;
3529 nack->u.isp24.status_subcode = ntfy->u.isp24.status_subcode;
3530 nack->u.isp24.fw_handle = ntfy->u.isp24.fw_handle;
3531 nack->u.isp24.exchange_address = ntfy->u.isp24.exchange_address;
3532 nack->u.isp24.srr_rel_offs = ntfy->u.isp24.srr_rel_offs;
3533 nack->u.isp24.srr_ui = ntfy->u.isp24.srr_ui;
3534 nack->u.isp24.vp_index = ntfy->u.isp24.vp_index;
3536 qla2x00_start_iocbs(vha, vha->req);
3540 static void qlt_send_term_imm_notif(struct scsi_qla_host *vha,
3541 struct imm_ntfy_from_isp *imm, int ha_locked)
3543 unsigned long flags = 0;
3547 rc = __qlt_send_term_imm_notif(vha, imm);
3551 qlt_alloc_qfull_cmd(vha, imm, 0, 0);
3559 spin_lock_irqsave(&vha->hw->hardware_lock, flags);
3560 rc = __qlt_send_term_imm_notif(vha, imm);
3564 qlt_alloc_qfull_cmd(vha, imm, 0, 0);
3569 spin_unlock_irqrestore(&vha->hw->hardware_lock, flags);
3573 * If hardware_lock held on entry, might drop it, then reaquire
3574 * This function sends the appropriate CTIO to ISP 2xxx or 24xx
3576 static int __qlt_send_term_exchange(struct qla_qpair *qpair,
3577 struct qla_tgt_cmd *cmd,
3578 struct atio_from_isp *atio)
3580 struct scsi_qla_host *vha = qpair->vha;
3581 struct ctio7_to_24xx *ctio24;
3582 struct qla_hw_data *ha = vha->hw;
3587 ql_dbg(ql_dbg_tgt, vha, 0xe009, "Sending TERM EXCH CTIO (ha=%p)\n", ha);
3592 pkt = (request_t *)qla2x00_alloc_iocbs_ready(qpair, NULL);
3594 ql_dbg(ql_dbg_tgt, vha, 0xe050,
3595 "qla_target(%d): %s failed: unable to allocate "
3596 "request packet\n", vha->vp_idx, __func__);
3601 if (cmd->state < QLA_TGT_STATE_PROCESSED) {
3602 ql_dbg(ql_dbg_tgt, vha, 0xe051,
3603 "qla_target(%d): Terminating cmd %p with "
3604 "incorrect state %d\n", vha->vp_idx, cmd,
3610 qpair->tgt_counters.num_term_xchg_sent++;
3611 pkt->entry_count = 1;
3612 pkt->handle = QLA_TGT_SKIP_HANDLE | CTIO_COMPLETION_HANDLE_MARK;
3614 ctio24 = (struct ctio7_to_24xx *)pkt;
3615 ctio24->entry_type = CTIO_TYPE7;
3616 ctio24->nport_handle = CTIO7_NHANDLE_UNRECOGNIZED;
3617 ctio24->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
3618 ctio24->vp_index = vha->vp_idx;
3619 ctio24->initiator_id[0] = atio->u.isp24.fcp_hdr.s_id[2];
3620 ctio24->initiator_id[1] = atio->u.isp24.fcp_hdr.s_id[1];
3621 ctio24->initiator_id[2] = atio->u.isp24.fcp_hdr.s_id[0];
3622 ctio24->exchange_addr = atio->u.isp24.exchange_addr;
3623 temp = (atio->u.isp24.attr << 9) | CTIO7_FLAGS_STATUS_MODE_1 |
3624 CTIO7_FLAGS_TERMINATE;
3625 ctio24->u.status1.flags = cpu_to_le16(temp);
3626 temp = be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id);
3627 ctio24->u.status1.ox_id = cpu_to_le16(temp);
3629 /* Memory Barrier */
3631 if (qpair->reqq_start_iocbs)
3632 qpair->reqq_start_iocbs(qpair);
3634 qla2x00_start_iocbs(vha, qpair->req);
3638 static void qlt_send_term_exchange(struct qla_qpair *qpair,
3639 struct qla_tgt_cmd *cmd, struct atio_from_isp *atio, int ha_locked,
3642 struct scsi_qla_host *vha;
3643 unsigned long flags = 0;
3646 /* why use different vha? NPIV */
3653 rc = __qlt_send_term_exchange(qpair, cmd, atio);
3655 qlt_alloc_qfull_cmd(vha, atio, 0, 0);
3658 spin_lock_irqsave(qpair->qp_lock_ptr, flags);
3659 rc = __qlt_send_term_exchange(qpair, cmd, atio);
3661 qlt_alloc_qfull_cmd(vha, atio, 0, 0);
3664 if (cmd && !ul_abort && !cmd->aborted) {
3666 qlt_unmap_sg(vha, cmd);
3667 vha->hw->tgt.tgt_ops->free_cmd(cmd);
3671 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
3676 static void qlt_init_term_exchange(struct scsi_qla_host *vha)
3678 struct list_head free_list;
3679 struct qla_tgt_cmd *cmd, *tcmd;
3681 vha->hw->tgt.leak_exchg_thresh_hold =
3682 (vha->hw->cur_fw_xcb_count/100) * LEAK_EXCHG_THRESH_HOLD_PERCENT;
3685 if (!list_empty(&vha->hw->tgt.q_full_list)) {
3686 INIT_LIST_HEAD(&free_list);
3687 list_splice_init(&vha->hw->tgt.q_full_list, &free_list);
3689 list_for_each_entry_safe(cmd, tcmd, &free_list, cmd_list) {
3690 list_del(&cmd->cmd_list);
3691 /* This cmd was never sent to TCM. There is no need
3692 * to schedule free or call free_cmd
3695 vha->hw->tgt.num_qfull_cmds_alloc--;
3698 vha->hw->tgt.num_qfull_cmds_dropped = 0;
3701 static void qlt_chk_exch_leak_thresh_hold(struct scsi_qla_host *vha)
3703 uint32_t total_leaked;
3705 total_leaked = vha->hw->tgt.num_qfull_cmds_dropped;
3707 if (vha->hw->tgt.leak_exchg_thresh_hold &&
3708 (total_leaked > vha->hw->tgt.leak_exchg_thresh_hold)) {
3710 ql_dbg(ql_dbg_tgt, vha, 0xe079,
3711 "Chip reset due to exchange starvation: %d/%d.\n",
3712 total_leaked, vha->hw->cur_fw_xcb_count);
3714 if (IS_P3P_TYPE(vha->hw))
3715 set_bit(FCOE_CTX_RESET_NEEDED, &vha->dpc_flags);
3717 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
3718 qla2xxx_wake_dpc(vha);
3723 int qlt_abort_cmd(struct qla_tgt_cmd *cmd)
3725 struct qla_tgt *tgt = cmd->tgt;
3726 struct scsi_qla_host *vha = tgt->vha;
3727 struct se_cmd *se_cmd = &cmd->se_cmd;
3728 unsigned long flags;
3730 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf014,
3731 "qla_target(%d): terminating exchange for aborted cmd=%p "
3732 "(se_cmd=%p, tag=%llu)", vha->vp_idx, cmd, &cmd->se_cmd,
3735 spin_lock_irqsave(&cmd->cmd_lock, flags);
3737 spin_unlock_irqrestore(&cmd->cmd_lock, flags);
3739 * It's normal to see 2 calls in this path:
3740 * 1) XFER Rdy completion + CMD_T_ABORT
3741 * 2) TCM TMR - drain_state_list
3743 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf016,
3744 "multiple abort. %p transport_state %x, t_state %x, "
3745 "se_cmd_flags %x\n", cmd, cmd->se_cmd.transport_state,
3746 cmd->se_cmd.t_state, cmd->se_cmd.se_cmd_flags);
3750 cmd->trc_flags |= TRC_ABORT;
3751 spin_unlock_irqrestore(&cmd->cmd_lock, flags);
3753 qlt_send_term_exchange(cmd->qpair, cmd, &cmd->atio, 0, 1);
3756 EXPORT_SYMBOL(qlt_abort_cmd);
3758 void qlt_free_cmd(struct qla_tgt_cmd *cmd)
3760 struct fc_port *sess = cmd->sess;
3762 ql_dbg(ql_dbg_tgt, cmd->vha, 0xe074,
3763 "%s: se_cmd[%p] ox_id %04x\n",
3764 __func__, &cmd->se_cmd,
3765 be16_to_cpu(cmd->atio.u.isp24.fcp_hdr.ox_id));
3767 BUG_ON(cmd->cmd_in_wq);
3770 qlt_unmap_sg(cmd->vha, cmd);
3773 qlt_decr_num_pend_cmds(cmd->vha);
3775 BUG_ON(cmd->sg_mapped);
3776 cmd->jiffies_at_free = get_jiffies_64();
3777 if (unlikely(cmd->free_sg))
3780 if (!sess || !sess->se_sess) {
3784 cmd->jiffies_at_free = get_jiffies_64();
3785 percpu_ida_free(&sess->se_sess->sess_tag_pool, cmd->se_cmd.map_tag);
3787 EXPORT_SYMBOL(qlt_free_cmd);
3790 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
3792 static int qlt_term_ctio_exchange(struct qla_qpair *qpair, void *ctio,
3793 struct qla_tgt_cmd *cmd, uint32_t status)
3796 struct scsi_qla_host *vha = qpair->vha;
3798 if (cmd->se_cmd.prot_op)
3799 ql_dbg(ql_dbg_tgt_dif, vha, 0xe013,
3800 "Term DIF cmd: lba[0x%llx|%lld] len[0x%x] "
3801 "se_cmd=%p tag[%x] op %#x/%s",
3803 cmd->num_blks, &cmd->se_cmd,
3804 cmd->atio.u.isp24.exchange_addr,
3805 cmd->se_cmd.prot_op,
3806 prot_op_str(cmd->se_cmd.prot_op));
3809 struct ctio7_from_24xx *c = (struct ctio7_from_24xx *)ctio;
3811 cpu_to_le16(OF_TERM_EXCH));
3816 qlt_send_term_exchange(qpair, cmd, &cmd->atio, 1, 0);
3822 /* ha->hardware_lock supposed to be held on entry */
3823 static struct qla_tgt_cmd *qlt_ctio_to_cmd(struct scsi_qla_host *vha,
3824 struct rsp_que *rsp, uint32_t handle, void *ctio)
3826 struct qla_tgt_cmd *cmd = NULL;
3827 struct req_que *req;
3828 int qid = GET_QID(handle);
3829 uint32_t h = handle & ~QLA_TGT_HANDLE_MASK;
3831 if (unlikely(h == QLA_TGT_SKIP_HANDLE))
3834 if (qid == rsp->req->id) {
3836 } else if (vha->hw->req_q_map[qid]) {
3837 ql_dbg(ql_dbg_tgt_mgt, vha, 0x1000a,
3838 "qla_target(%d): CTIO completion with different QID %d handle %x\n",
3839 vha->vp_idx, rsp->id, handle);
3840 req = vha->hw->req_q_map[qid];
3845 h &= QLA_CMD_HANDLE_MASK;
3847 if (h != QLA_TGT_NULL_HANDLE) {
3848 if (unlikely(h >= req->num_outstanding_cmds)) {
3849 ql_dbg(ql_dbg_tgt, vha, 0xe052,
3850 "qla_target(%d): Wrong handle %x received\n",
3851 vha->vp_idx, handle);
3855 cmd = (struct qla_tgt_cmd *)req->outstanding_cmds[h];
3856 if (unlikely(cmd == NULL)) {
3857 ql_dbg(ql_dbg_async, vha, 0xe053,
3858 "qla_target(%d): Suspicious: unable to find the command with handle %x req->id %d rsp->id %d\n",
3859 vha->vp_idx, handle, req->id, rsp->id);
3862 req->outstanding_cmds[h] = NULL;
3863 } else if (ctio != NULL) {
3864 /* We can't get loop ID from CTIO7 */
3865 ql_dbg(ql_dbg_tgt, vha, 0xe054,
3866 "qla_target(%d): Wrong CTIO received: QLA24xx doesn't "
3867 "support NULL handles\n", vha->vp_idx);
3874 /* hardware_lock should be held by caller. */
3876 qlt_abort_cmd_on_host_reset(struct scsi_qla_host *vha, struct qla_tgt_cmd *cmd)
3878 struct qla_hw_data *ha = vha->hw;
3881 qlt_unmap_sg(vha, cmd);
3883 /* TODO: fix debug message type and ids. */
3884 if (cmd->state == QLA_TGT_STATE_PROCESSED) {
3885 ql_dbg(ql_dbg_io, vha, 0xff00,
3886 "HOST-ABORT: state=PROCESSED.\n");
3887 } else if (cmd->state == QLA_TGT_STATE_NEED_DATA) {
3888 cmd->write_data_transferred = 0;
3889 cmd->state = QLA_TGT_STATE_DATA_IN;
3891 ql_dbg(ql_dbg_io, vha, 0xff01,
3892 "HOST-ABORT: state=DATA_IN.\n");
3894 ha->tgt.tgt_ops->handle_data(cmd);
3897 ql_dbg(ql_dbg_io, vha, 0xff03,
3898 "HOST-ABORT: state=BAD(%d).\n",
3903 cmd->trc_flags |= TRC_FLUSH;
3904 ha->tgt.tgt_ops->free_cmd(cmd);
3908 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
3910 static void qlt_do_ctio_completion(struct scsi_qla_host *vha,
3911 struct rsp_que *rsp, uint32_t handle, uint32_t status, void *ctio)
3913 struct qla_hw_data *ha = vha->hw;
3914 struct se_cmd *se_cmd;
3915 struct qla_tgt_cmd *cmd;
3916 struct qla_qpair *qpair = rsp->qpair;
3918 if (handle & CTIO_INTERMEDIATE_HANDLE_MARK) {
3919 /* That could happen only in case of an error/reset/abort */
3920 if (status != CTIO_SUCCESS) {
3921 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01d,
3922 "Intermediate CTIO received"
3923 " (status %x)\n", status);
3928 cmd = qlt_ctio_to_cmd(vha, rsp, handle, ctio);
3932 se_cmd = &cmd->se_cmd;
3933 cmd->cmd_sent_to_fw = 0;
3935 qlt_unmap_sg(vha, cmd);
3937 if (unlikely(status != CTIO_SUCCESS)) {
3938 switch (status & 0xFFFF) {
3939 case CTIO_LIP_RESET:
3940 case CTIO_TARGET_RESET:
3942 /* driver request abort via Terminate exchange */
3944 case CTIO_INVALID_RX_ID:
3946 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf058,
3947 "qla_target(%d): CTIO with "
3948 "status %#x received, state %x, se_cmd %p, "
3949 "(LIP_RESET=e, ABORTED=2, TARGET_RESET=17, "
3950 "TIMEOUT=b, INVALID_RX_ID=8)\n", vha->vp_idx,
3951 status, cmd->state, se_cmd);
3954 case CTIO_PORT_LOGGED_OUT:
3955 case CTIO_PORT_UNAVAILABLE:
3958 (status & 0xFFFF) == CTIO_PORT_LOGGED_OUT;
3960 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf059,
3961 "qla_target(%d): CTIO with %s status %x "
3962 "received (state %x, se_cmd %p)\n", vha->vp_idx,
3963 logged_out ? "PORT LOGGED OUT" : "PORT UNAVAILABLE",
3964 status, cmd->state, se_cmd);
3966 if (logged_out && cmd->sess) {
3968 * Session is already logged out, but we need
3969 * to notify initiator, who's not aware of this
3971 cmd->sess->logout_on_delete = 0;
3972 cmd->sess->send_els_logo = 1;
3973 ql_dbg(ql_dbg_disc, vha, 0x20f8,
3974 "%s %d %8phC post del sess\n",
3975 __func__, __LINE__, cmd->sess->port_name);
3977 qlt_schedule_sess_for_deletion(cmd->sess);
3981 case CTIO_DIF_ERROR: {
3982 struct ctio_crc_from_fw *crc =
3983 (struct ctio_crc_from_fw *)ctio;
3984 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf073,
3985 "qla_target(%d): CTIO with DIF_ERROR status %x "
3986 "received (state %x, ulp_cmd %p) actual_dif[0x%llx] "
3987 "expect_dif[0x%llx]\n",
3988 vha->vp_idx, status, cmd->state, se_cmd,
3989 *((u64 *)&crc->actual_dif[0]),
3990 *((u64 *)&crc->expected_dif[0]));
3992 qlt_handle_dif_error(qpair, cmd, ctio);
3996 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05b,
3997 "qla_target(%d): CTIO with error status 0x%x received (state %x, se_cmd %p\n",
3998 vha->vp_idx, status, cmd->state, se_cmd);
4003 /* "cmd->aborted" means
4004 * cmd is already aborted/terminated, we don't
4005 * need to terminate again. The exchange is already
4006 * cleaned up/freed at FW level. Just cleanup at driver
4009 if ((cmd->state != QLA_TGT_STATE_NEED_DATA) &&
4011 cmd->trc_flags |= TRC_CTIO_ERR;
4012 if (qlt_term_ctio_exchange(qpair, ctio, cmd, status))
4017 if (cmd->state == QLA_TGT_STATE_PROCESSED) {
4018 cmd->trc_flags |= TRC_CTIO_DONE;
4019 } else if (cmd->state == QLA_TGT_STATE_NEED_DATA) {
4020 cmd->state = QLA_TGT_STATE_DATA_IN;
4022 if (status == CTIO_SUCCESS)
4023 cmd->write_data_transferred = 1;
4025 ha->tgt.tgt_ops->handle_data(cmd);
4027 } else if (cmd->aborted) {
4028 cmd->trc_flags |= TRC_CTIO_ABORTED;
4029 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01e,
4030 "Aborted command %p (tag %lld) finished\n", cmd, se_cmd->tag);
4032 cmd->trc_flags |= TRC_CTIO_STRANGE;
4033 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05c,
4034 "qla_target(%d): A command in state (%d) should "
4035 "not return a CTIO complete\n", vha->vp_idx, cmd->state);
4038 if (unlikely(status != CTIO_SUCCESS) &&
4040 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01f, "Finishing failed CTIO\n");
4044 ha->tgt.tgt_ops->free_cmd(cmd);
4047 static inline int qlt_get_fcp_task_attr(struct scsi_qla_host *vha,
4052 switch (task_codes) {
4053 case ATIO_SIMPLE_QUEUE:
4054 fcp_task_attr = TCM_SIMPLE_TAG;
4056 case ATIO_HEAD_OF_QUEUE:
4057 fcp_task_attr = TCM_HEAD_TAG;
4059 case ATIO_ORDERED_QUEUE:
4060 fcp_task_attr = TCM_ORDERED_TAG;
4062 case ATIO_ACA_QUEUE:
4063 fcp_task_attr = TCM_ACA_TAG;
4066 fcp_task_attr = TCM_SIMPLE_TAG;
4069 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05d,
4070 "qla_target: unknown task code %x, use ORDERED instead\n",
4072 fcp_task_attr = TCM_ORDERED_TAG;
4076 return fcp_task_attr;
4079 static struct fc_port *qlt_make_local_sess(struct scsi_qla_host *,
4082 * Process context for I/O path into tcm_qla2xxx code
4084 static void __qlt_do_work(struct qla_tgt_cmd *cmd)
4086 scsi_qla_host_t *vha = cmd->vha;
4087 struct qla_hw_data *ha = vha->hw;
4088 struct fc_port *sess = cmd->sess;
4089 struct atio_from_isp *atio = &cmd->atio;
4091 unsigned long flags;
4092 uint32_t data_length;
4093 int ret, fcp_task_attr, data_dir, bidi = 0;
4094 struct qla_qpair *qpair = cmd->qpair;
4097 cmd->trc_flags |= TRC_DO_WORK;
4100 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf082,
4101 "cmd with tag %u is aborted\n",
4102 cmd->atio.u.isp24.exchange_addr);
4106 spin_lock_init(&cmd->cmd_lock);
4107 cdb = &atio->u.isp24.fcp_cmnd.cdb[0];
4108 cmd->se_cmd.tag = atio->u.isp24.exchange_addr;
4110 if (atio->u.isp24.fcp_cmnd.rddata &&
4111 atio->u.isp24.fcp_cmnd.wrdata) {
4113 data_dir = DMA_TO_DEVICE;
4114 } else if (atio->u.isp24.fcp_cmnd.rddata)
4115 data_dir = DMA_FROM_DEVICE;
4116 else if (atio->u.isp24.fcp_cmnd.wrdata)
4117 data_dir = DMA_TO_DEVICE;
4119 data_dir = DMA_NONE;
4121 fcp_task_attr = qlt_get_fcp_task_attr(vha,
4122 atio->u.isp24.fcp_cmnd.task_attr);
4123 data_length = get_datalen_for_atio(atio);
4125 ret = ha->tgt.tgt_ops->handle_cmd(vha, cmd, cdb, data_length,
4126 fcp_task_attr, data_dir, bidi);
4130 * Drop extra session reference from qla_tgt_handle_cmd_for_atio*(
4132 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
4133 ha->tgt.tgt_ops->put_sess(sess);
4134 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
4138 ql_dbg(ql_dbg_io, vha, 0x3060, "Terminating work cmd %p", cmd);
4140 * cmd has not sent to target yet, so pass NULL as the second
4141 * argument to qlt_send_term_exchange() and free the memory here.
4143 cmd->trc_flags |= TRC_DO_WORK_ERR;
4144 spin_lock_irqsave(qpair->qp_lock_ptr, flags);
4145 qlt_send_term_exchange(qpair, NULL, &cmd->atio, 1, 0);
4147 qlt_decr_num_pend_cmds(vha);
4148 percpu_ida_free(&sess->se_sess->sess_tag_pool, cmd->se_cmd.map_tag);
4149 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
4151 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
4152 ha->tgt.tgt_ops->put_sess(sess);
4153 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
4156 static void qlt_do_work(struct work_struct *work)
4158 struct qla_tgt_cmd *cmd = container_of(work, struct qla_tgt_cmd, work);
4159 scsi_qla_host_t *vha = cmd->vha;
4160 unsigned long flags;
4162 spin_lock_irqsave(&vha->cmd_list_lock, flags);
4163 list_del(&cmd->cmd_list);
4164 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
4169 void qlt_clr_qp_table(struct scsi_qla_host *vha)
4171 unsigned long flags;
4172 struct qla_hw_data *ha = vha->hw;
4173 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
4177 ql_log(ql_log_info, vha, 0x706c,
4178 "User update Number of Active Qpairs %d\n",
4179 ha->tgt.num_act_qpairs);
4181 spin_lock_irqsave(&ha->tgt.atio_lock, flags);
4183 btree_for_each_safe64(&tgt->lun_qpair_map, key, node)
4184 btree_remove64(&tgt->lun_qpair_map, key);
4186 ha->base_qpair->lun_cnt = 0;
4187 for (key = 0; key < ha->max_qpairs; key++)
4188 if (ha->queue_pair_map[key])
4189 ha->queue_pair_map[key]->lun_cnt = 0;
4191 spin_unlock_irqrestore(&ha->tgt.atio_lock, flags);
4194 static void qlt_assign_qpair(struct scsi_qla_host *vha,
4195 struct qla_tgt_cmd *cmd)
4197 struct qla_qpair *qpair, *qp;
4198 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
4199 struct qla_qpair_hint *h;
4201 if (vha->flags.qpairs_available) {
4202 h = btree_lookup64(&tgt->lun_qpair_map, cmd->unpacked_lun);
4204 /* spread lun to qpair ratio evently */
4206 struct scsi_qla_host *base_vha =
4207 pci_get_drvdata(vha->hw->pdev);
4209 qpair = vha->hw->base_qpair;
4210 if (qpair->lun_cnt == 0) {
4212 h = qla_qpair_to_hint(tgt, qpair);
4214 rc = btree_insert64(&tgt->lun_qpair_map,
4215 cmd->unpacked_lun, h, GFP_ATOMIC);
4218 ql_log(ql_log_info, vha, 0xd037,
4219 "Unable to insert lun %llx into lun_qpair_map\n",
4224 lcnt = qpair->lun_cnt;
4228 list_for_each_entry(qp, &base_vha->qp_list,
4230 if (qp->lun_cnt == 0) {
4232 h = qla_qpair_to_hint(tgt, qp);
4234 rc = btree_insert64(&tgt->lun_qpair_map,
4235 cmd->unpacked_lun, h, GFP_ATOMIC);
4238 ql_log(ql_log_info, vha, 0xd038,
4239 "Unable to insert lun %llx into lun_qpair_map\n",
4245 if (qp->lun_cnt < lcnt) {
4254 h = qla_qpair_to_hint(tgt, qpair);
4256 rc = btree_insert64(&tgt->lun_qpair_map,
4257 cmd->unpacked_lun, h, GFP_ATOMIC);
4260 ql_log(ql_log_info, vha, 0xd039,
4261 "Unable to insert lun %llx into lun_qpair_map\n",
4266 h = &tgt->qphints[0];
4269 cmd->qpair = h->qpair;
4270 cmd->se_cmd.cpuid = h->cpuid;
4273 static struct qla_tgt_cmd *qlt_get_tag(scsi_qla_host_t *vha,
4274 struct fc_port *sess,
4275 struct atio_from_isp *atio)
4277 struct se_session *se_sess = sess->se_sess;
4278 struct qla_tgt_cmd *cmd;
4281 tag = percpu_ida_alloc(&se_sess->sess_tag_pool, TASK_RUNNING);
4285 cmd = &((struct qla_tgt_cmd *)se_sess->sess_cmd_map)[tag];
4286 memset(cmd, 0, sizeof(struct qla_tgt_cmd));
4287 cmd->cmd_type = TYPE_TGT_CMD;
4288 memcpy(&cmd->atio, atio, sizeof(*atio));
4289 cmd->state = QLA_TGT_STATE_NEW;
4290 cmd->tgt = vha->vha_tgt.qla_tgt;
4291 qlt_incr_num_pend_cmds(vha);
4293 cmd->se_cmd.map_tag = tag;
4295 cmd->loop_id = sess->loop_id;
4296 cmd->conf_compl_supported = sess->conf_compl_supported;
4299 cmd->jiffies_at_alloc = get_jiffies_64();
4301 cmd->unpacked_lun = scsilun_to_int(
4302 (struct scsi_lun *)&atio->u.isp24.fcp_cmnd.lun);
4303 qlt_assign_qpair(vha, cmd);
4304 cmd->reset_count = vha->hw->base_qpair->chip_reset;
4305 cmd->vp_idx = vha->vp_idx;
4310 /* ha->hardware_lock supposed to be held on entry */
4311 static int qlt_handle_cmd_for_atio(struct scsi_qla_host *vha,
4312 struct atio_from_isp *atio)
4314 struct qla_hw_data *ha = vha->hw;
4315 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
4316 struct fc_port *sess;
4317 struct qla_tgt_cmd *cmd;
4318 unsigned long flags;
4321 if (unlikely(tgt->tgt_stop)) {
4322 ql_dbg(ql_dbg_io, vha, 0x3061,
4323 "New command while device %p is shutting down\n", tgt);
4327 id.b.al_pa = atio->u.isp24.fcp_hdr.s_id[2];
4328 id.b.area = atio->u.isp24.fcp_hdr.s_id[1];
4329 id.b.domain = atio->u.isp24.fcp_hdr.s_id[0];
4330 if (IS_SW_RESV_ADDR(id))
4333 sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha, atio->u.isp24.fcp_hdr.s_id);
4334 if (unlikely(!sess))
4337 /* Another WWN used to have our s_id. Our PLOGI scheduled its
4338 * session deletion, but it's still in sess_del_work wq */
4339 if (sess->deleted) {
4340 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf002,
4341 "New command while old session %p is being deleted\n",
4347 * Do kref_get() before returning + dropping qla_hw_data->hardware_lock.
4349 if (!kref_get_unless_zero(&sess->sess_kref)) {
4350 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf004,
4351 "%s: kref_get fail, %8phC oxid %x \n",
4352 __func__, sess->port_name,
4353 be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id));
4357 cmd = qlt_get_tag(vha, sess, atio);
4359 ql_dbg(ql_dbg_io, vha, 0x3062,
4360 "qla_target(%d): Allocation of cmd failed\n", vha->vp_idx);
4361 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
4362 ha->tgt.tgt_ops->put_sess(sess);
4363 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
4368 cmd->trc_flags |= TRC_NEW_CMD;
4370 spin_lock_irqsave(&vha->cmd_list_lock, flags);
4371 list_add_tail(&cmd->cmd_list, &vha->qla_cmd_list);
4372 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
4374 INIT_WORK(&cmd->work, qlt_do_work);
4375 if (vha->flags.qpairs_available) {
4376 queue_work_on(cmd->se_cmd.cpuid, qla_tgt_wq, &cmd->work);
4377 } else if (ha->msix_count) {
4378 if (cmd->atio.u.isp24.fcp_cmnd.rddata)
4379 queue_work_on(smp_processor_id(), qla_tgt_wq,
4382 queue_work_on(cmd->se_cmd.cpuid, qla_tgt_wq,
4385 queue_work(qla_tgt_wq, &cmd->work);
4391 /* ha->hardware_lock supposed to be held on entry */
4392 static int qlt_issue_task_mgmt(struct fc_port *sess, u64 lun,
4393 int fn, void *iocb, int flags)
4395 struct scsi_qla_host *vha = sess->vha;
4396 struct qla_hw_data *ha = vha->hw;
4397 struct qla_tgt_mgmt_cmd *mcmd;
4398 struct atio_from_isp *a = (struct atio_from_isp *)iocb;
4399 struct qla_qpair_hint *h = &vha->vha_tgt.qla_tgt->qphints[0];
4401 mcmd = mempool_alloc(qla_tgt_mgmt_cmd_mempool, GFP_ATOMIC);
4403 ql_dbg(ql_dbg_tgt_tmr, vha, 0x10009,
4404 "qla_target(%d): Allocation of management "
4405 "command failed, some commands and their data could "
4406 "leak\n", vha->vp_idx);
4409 memset(mcmd, 0, sizeof(*mcmd));
4413 memcpy(&mcmd->orig_iocb.imm_ntfy, iocb,
4414 sizeof(mcmd->orig_iocb.imm_ntfy));
4416 mcmd->tmr_func = fn;
4417 mcmd->flags = flags;
4418 mcmd->reset_count = ha->base_qpair->chip_reset;
4419 mcmd->qpair = h->qpair;
4421 mcmd->se_cmd.cpuid = h->cpuid;
4422 mcmd->unpacked_lun = lun;
4425 case QLA_TGT_LUN_RESET:
4426 case QLA_TGT_CLEAR_TS:
4427 case QLA_TGT_ABORT_TS:
4428 abort_cmds_for_lun(vha, lun, a->u.isp24.fcp_hdr.s_id);
4430 case QLA_TGT_CLEAR_ACA:
4431 h = qlt_find_qphint(vha, mcmd->unpacked_lun);
4432 mcmd->qpair = h->qpair;
4433 mcmd->se_cmd.cpuid = h->cpuid;
4436 case QLA_TGT_TARGET_RESET:
4437 case QLA_TGT_NEXUS_LOSS_SESS:
4438 case QLA_TGT_NEXUS_LOSS:
4439 case QLA_TGT_ABORT_ALL:
4445 INIT_WORK(&mcmd->work, qlt_do_tmr_work);
4446 queue_work_on(mcmd->se_cmd.cpuid, qla_tgt_wq,
4452 /* ha->hardware_lock supposed to be held on entry */
4453 static int qlt_handle_task_mgmt(struct scsi_qla_host *vha, void *iocb)
4455 struct atio_from_isp *a = (struct atio_from_isp *)iocb;
4456 struct qla_hw_data *ha = vha->hw;
4457 struct fc_port *sess;
4460 unsigned long flags;
4462 fn = a->u.isp24.fcp_cmnd.task_mgmt_flags;
4464 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
4465 sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha,
4466 a->u.isp24.fcp_hdr.s_id);
4467 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
4470 scsilun_to_int((struct scsi_lun *)&a->u.isp24.fcp_cmnd.lun);
4472 if (sess == NULL || sess->deleted)
4475 return qlt_issue_task_mgmt(sess, unpacked_lun, fn, iocb, 0);
4478 /* ha->hardware_lock supposed to be held on entry */
4479 static int __qlt_abort_task(struct scsi_qla_host *vha,
4480 struct imm_ntfy_from_isp *iocb, struct fc_port *sess)
4482 struct atio_from_isp *a = (struct atio_from_isp *)iocb;
4483 struct qla_hw_data *ha = vha->hw;
4484 struct qla_tgt_mgmt_cmd *mcmd;
4488 mcmd = mempool_alloc(qla_tgt_mgmt_cmd_mempool, GFP_ATOMIC);
4490 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05f,
4491 "qla_target(%d): %s: Allocation of ABORT cmd failed\n",
4492 vha->vp_idx, __func__);
4495 memset(mcmd, 0, sizeof(*mcmd));
4498 memcpy(&mcmd->orig_iocb.imm_ntfy, iocb,
4499 sizeof(mcmd->orig_iocb.imm_ntfy));
4502 scsilun_to_int((struct scsi_lun *)&a->u.isp24.fcp_cmnd.lun);
4503 mcmd->reset_count = ha->base_qpair->chip_reset;
4504 mcmd->tmr_func = QLA_TGT_2G_ABORT_TASK;
4505 mcmd->qpair = ha->base_qpair;
4507 rc = ha->tgt.tgt_ops->handle_tmr(mcmd, unpacked_lun, mcmd->tmr_func,
4508 le16_to_cpu(iocb->u.isp2x.seq_id));
4510 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf060,
4511 "qla_target(%d): tgt_ops->handle_tmr() failed: %d\n",
4513 mempool_free(mcmd, qla_tgt_mgmt_cmd_mempool);
4520 /* ha->hardware_lock supposed to be held on entry */
4521 static int qlt_abort_task(struct scsi_qla_host *vha,
4522 struct imm_ntfy_from_isp *iocb)
4524 struct qla_hw_data *ha = vha->hw;
4525 struct fc_port *sess;
4527 unsigned long flags;
4529 loop_id = GET_TARGET_ID(ha, (struct atio_from_isp *)iocb);
4531 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
4532 sess = ha->tgt.tgt_ops->find_sess_by_loop_id(vha, loop_id);
4533 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
4536 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf025,
4537 "qla_target(%d): task abort for unexisting "
4538 "session\n", vha->vp_idx);
4539 return qlt_sched_sess_work(vha->vha_tgt.qla_tgt,
4540 QLA_TGT_SESS_WORK_ABORT, iocb, sizeof(*iocb));
4543 return __qlt_abort_task(vha, iocb, sess);
4546 void qlt_logo_completion_handler(fc_port_t *fcport, int rc)
4548 if (rc != MBS_COMMAND_COMPLETE) {
4549 ql_dbg(ql_dbg_tgt_mgt, fcport->vha, 0xf093,
4550 "%s: se_sess %p / sess %p from"
4551 " port %8phC loop_id %#04x s_id %02x:%02x:%02x"
4552 " LOGO failed: %#x\n",
4556 fcport->port_name, fcport->loop_id,
4557 fcport->d_id.b.domain, fcport->d_id.b.area,
4558 fcport->d_id.b.al_pa, rc);
4561 fcport->logout_completed = 1;
4565 * ha->hardware_lock supposed to be held on entry (to protect tgt->sess_list)
4567 * Schedules sessions with matching port_id/loop_id but different wwn for
4568 * deletion. Returns existing session with matching wwn if present.
4572 qlt_find_sess_invalidate_other(scsi_qla_host_t *vha, uint64_t wwn,
4573 port_id_t port_id, uint16_t loop_id, struct fc_port **conflict_sess)
4575 struct fc_port *sess = NULL, *other_sess;
4578 *conflict_sess = NULL;
4580 list_for_each_entry(other_sess, &vha->vp_fcports, list) {
4582 other_wwn = wwn_to_u64(other_sess->port_name);
4584 if (wwn == other_wwn) {
4590 /* find other sess with nport_id collision */
4591 if (port_id.b24 == other_sess->d_id.b24) {
4592 if (loop_id != other_sess->loop_id) {
4593 ql_dbg(ql_dbg_tgt_tmr, vha, 0x1000c,
4594 "Invalidating sess %p loop_id %d wwn %llx.\n",
4595 other_sess, other_sess->loop_id, other_wwn);
4598 * logout_on_delete is set by default, but another
4599 * session that has the same s_id/loop_id combo
4600 * might have cleared it when requested this session
4601 * deletion, so don't touch it
4603 qlt_schedule_sess_for_deletion(other_sess);
4606 * Another wwn used to have our s_id/loop_id
4607 * kill the session, but don't free the loop_id
4609 ql_dbg(ql_dbg_tgt_tmr, vha, 0xf01b,
4610 "Invalidating sess %p loop_id %d wwn %llx.\n",
4611 other_sess, other_sess->loop_id, other_wwn);
4613 other_sess->keep_nport_handle = 1;
4614 if (other_sess->disc_state != DSC_DELETED)
4615 *conflict_sess = other_sess;
4616 qlt_schedule_sess_for_deletion(other_sess);
4621 /* find other sess with nport handle collision */
4622 if ((loop_id == other_sess->loop_id) &&
4623 (loop_id != FC_NO_LOOP_ID)) {
4624 ql_dbg(ql_dbg_tgt_tmr, vha, 0x1000d,
4625 "Invalidating sess %p loop_id %d wwn %llx.\n",
4626 other_sess, other_sess->loop_id, other_wwn);
4628 /* Same loop_id but different s_id
4629 * Ok to kill and logout */
4630 qlt_schedule_sess_for_deletion(other_sess);
4637 /* Abort any commands for this s_id waiting on qla_tgt_wq workqueue */
4638 static int abort_cmds_for_s_id(struct scsi_qla_host *vha, port_id_t *s_id)
4640 struct qla_tgt_sess_op *op;
4641 struct qla_tgt_cmd *cmd;
4644 unsigned long flags;
4646 key = (((u32)s_id->b.domain << 16) |
4647 ((u32)s_id->b.area << 8) |
4648 ((u32)s_id->b.al_pa));
4650 spin_lock_irqsave(&vha->cmd_list_lock, flags);
4651 list_for_each_entry(op, &vha->qla_sess_op_cmd_list, cmd_list) {
4652 uint32_t op_key = sid_to_key(op->atio.u.isp24.fcp_hdr.s_id);
4654 if (op_key == key) {
4660 list_for_each_entry(op, &vha->unknown_atio_list, cmd_list) {
4661 uint32_t op_key = sid_to_key(op->atio.u.isp24.fcp_hdr.s_id);
4662 if (op_key == key) {
4668 list_for_each_entry(cmd, &vha->qla_cmd_list, cmd_list) {
4669 uint32_t cmd_key = sid_to_key(cmd->atio.u.isp24.fcp_hdr.s_id);
4670 if (cmd_key == key) {
4675 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
4680 static int qlt_handle_login(struct scsi_qla_host *vha,
4681 struct imm_ntfy_from_isp *iocb)
4683 struct fc_port *sess = NULL, *conflict_sess = NULL;
4686 uint16_t loop_id, wd3_lo;
4688 struct qlt_plogi_ack_t *pla;
4689 unsigned long flags;
4691 wwn = wwn_to_u64(iocb->u.isp24.port_name);
4693 port_id.b.domain = iocb->u.isp24.port_id[2];
4694 port_id.b.area = iocb->u.isp24.port_id[1];
4695 port_id.b.al_pa = iocb->u.isp24.port_id[0];
4696 port_id.b.rsvd_1 = 0;
4698 loop_id = le16_to_cpu(iocb->u.isp24.nport_handle);
4700 /* Mark all stale commands sitting in qla_tgt_wq for deletion */
4701 abort_cmds_for_s_id(vha, &port_id);
4704 spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
4705 sess = qlt_find_sess_invalidate_other(vha, wwn,
4706 port_id, loop_id, &conflict_sess);
4707 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
4710 if (IS_SW_RESV_ADDR(port_id)) {
4715 pla = qlt_plogi_ack_find_add(vha, &port_id, iocb);
4717 qlt_send_term_imm_notif(vha, iocb, 1);
4721 if (conflict_sess) {
4722 conflict_sess->login_gen++;
4723 qlt_plogi_ack_link(vha, pla, conflict_sess,
4724 QLT_PLOGI_LINK_CONFLICT);
4729 ql_dbg(ql_dbg_disc, vha, 0xffff,
4730 "%s %d %8phC post new sess\n",
4731 __func__, __LINE__, iocb->u.isp24.port_name);
4732 if (iocb->u.isp24.status_subcode == ELS_PLOGI)
4733 qla24xx_post_newsess_work(vha, &port_id,
4734 iocb->u.isp24.port_name,
4735 iocb->u.isp24.u.plogi.node_name,
4736 pla, FC4_TYPE_UNKNOWN);
4738 qla24xx_post_newsess_work(vha, &port_id,
4739 iocb->u.isp24.port_name, NULL,
4740 pla, FC4_TYPE_UNKNOWN);
4745 qlt_plogi_ack_link(vha, pla, sess, QLT_PLOGI_LINK_SAME_WWN);
4746 sess->d_id = port_id;
4749 if (iocb->u.isp24.status_subcode == ELS_PRLI) {
4750 sess->fw_login_state = DSC_LS_PRLI_PEND;
4752 sess->loop_id = loop_id;
4753 sess->d_id = port_id;
4754 sess->fw_login_state = DSC_LS_PRLI_PEND;
4755 wd3_lo = le16_to_cpu(iocb->u.isp24.u.prli.wd3_lo);
4758 sess->conf_compl_supported = 1;
4760 if ((wd3_lo & BIT_4) == 0)
4761 sess->port_type = FCT_INITIATOR;
4763 sess->port_type = FCT_TARGET;
4766 sess->fw_login_state = DSC_LS_PLOGI_PEND;
4769 ql_dbg(ql_dbg_disc, vha, 0x20f9,
4770 "%s %d %8phC DS %d\n",
4771 __func__, __LINE__, sess->port_name, sess->disc_state);
4773 switch (sess->disc_state) {
4775 qlt_plogi_ack_unref(vha, pla);
4780 * Under normal circumstances we want to release nport handle
4781 * during LOGO process to avoid nport handle leaks inside FW.
4782 * The exception is when LOGO is done while another PLOGI with
4783 * the same nport handle is waiting as might be the case here.
4784 * Note: there is always a possibily of a race where session
4785 * deletion has already started for other reasons (e.g. ACL
4786 * removal) and now PLOGI arrives:
4787 * 1. if PLOGI arrived in FW after nport handle has been freed,
4788 * FW must have assigned this PLOGI a new/same handle and we
4789 * can proceed ACK'ing it as usual when session deletion
4791 * 2. if PLOGI arrived in FW before LOGO with LCF_FREE_NPORT
4792 * bit reached it, the handle has now been released. We'll
4793 * get an error when we ACK this PLOGI. Nothing will be sent
4794 * back to initiator. Initiator should eventually retry
4795 * PLOGI and situation will correct itself.
4797 sess->keep_nport_handle = ((sess->loop_id == loop_id) &&
4798 (sess->d_id.b24 == port_id.b24));
4800 ql_dbg(ql_dbg_disc, vha, 0x20f9,
4801 "%s %d %8phC post del sess\n",
4802 __func__, __LINE__, sess->port_name);
4805 qlt_schedule_sess_for_deletion(sess);
4813 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
4815 static int qlt_24xx_handle_els(struct scsi_qla_host *vha,
4816 struct imm_ntfy_from_isp *iocb)
4818 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
4819 struct qla_hw_data *ha = vha->hw;
4820 struct fc_port *sess = NULL, *conflict_sess = NULL;
4826 unsigned long flags;
4828 wwn = wwn_to_u64(iocb->u.isp24.port_name);
4830 port_id.b.domain = iocb->u.isp24.port_id[2];
4831 port_id.b.area = iocb->u.isp24.port_id[1];
4832 port_id.b.al_pa = iocb->u.isp24.port_id[0];
4833 port_id.b.rsvd_1 = 0;
4835 loop_id = le16_to_cpu(iocb->u.isp24.nport_handle);
4837 ql_dbg(ql_dbg_disc, vha, 0xf026,
4838 "qla_target(%d): Port ID: %02x:%02x:%02x ELS opcode: 0x%02x lid %d %8phC\n",
4839 vha->vp_idx, iocb->u.isp24.port_id[2],
4840 iocb->u.isp24.port_id[1], iocb->u.isp24.port_id[0],
4841 iocb->u.isp24.status_subcode, loop_id,
4842 iocb->u.isp24.port_name);
4844 /* res = 1 means ack at the end of thread
4845 * res = 0 means ack async/later.
4847 switch (iocb->u.isp24.status_subcode) {
4849 res = qlt_handle_login(vha, iocb);
4854 sess = qla2x00_find_fcport_by_wwpn(vha,
4855 iocb->u.isp24.port_name, 1);
4857 if (sess && sess->plogi_link[QLT_PLOGI_LINK_SAME_WWN]) {
4858 ql_dbg(ql_dbg_disc, vha, 0xffff,
4859 "%s %d %8phC Term PRLI due to PLOGI ACK not completed\n",
4861 iocb->u.isp24.port_name);
4862 qlt_send_term_imm_notif(vha, iocb, 1);
4866 res = qlt_handle_login(vha, iocb);
4870 if (IS_SW_RESV_ADDR(port_id)) {
4875 wd3_lo = le16_to_cpu(iocb->u.isp24.u.prli.wd3_lo);
4878 spin_lock_irqsave(&tgt->ha->tgt.sess_lock, flags);
4879 sess = qlt_find_sess_invalidate_other(vha, wwn, port_id,
4880 loop_id, &conflict_sess);
4881 spin_unlock_irqrestore(&tgt->ha->tgt.sess_lock, flags);
4884 if (conflict_sess) {
4885 switch (conflict_sess->disc_state) {
4887 case DSC_DELETE_PEND:
4890 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf09b,
4891 "PRLI with conflicting sess %p port %8phC\n",
4892 conflict_sess, conflict_sess->port_name);
4893 conflict_sess->fw_login_state =
4894 DSC_LS_PORT_UNAVAIL;
4895 qlt_send_term_imm_notif(vha, iocb, 1);
4902 bool delete = false;
4903 spin_lock_irqsave(&tgt->ha->tgt.sess_lock, flags);
4904 switch (sess->fw_login_state) {
4905 case DSC_LS_PLOGI_PEND:
4906 case DSC_LS_PLOGI_COMP:
4907 case DSC_LS_PRLI_COMP:
4914 switch (sess->disc_state) {
4915 case DSC_LOGIN_PEND:
4917 case DSC_UPD_FCPORT:
4918 case DSC_LOGIN_COMPLETE:
4927 spin_unlock_irqrestore(&tgt->ha->tgt.sess_lock,
4930 * Impatient initiator sent PRLI before last
4931 * PLOGI could finish. Will force him to re-try,
4932 * while last one finishes.
4934 ql_log(ql_log_warn, sess->vha, 0xf095,
4935 "sess %p PRLI received, before plogi ack.\n",
4937 qlt_send_term_imm_notif(vha, iocb, 1);
4943 * This shouldn't happen under normal circumstances,
4944 * since we have deleted the old session during PLOGI
4946 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf096,
4947 "PRLI (loop_id %#04x) for existing sess %p (loop_id %#04x)\n",
4948 sess->loop_id, sess, iocb->u.isp24.nport_handle);
4951 sess->loop_id = loop_id;
4952 sess->d_id = port_id;
4953 sess->fw_login_state = DSC_LS_PRLI_PEND;
4956 sess->conf_compl_supported = 1;
4958 if ((wd3_lo & BIT_4) == 0)
4959 sess->port_type = FCT_INITIATOR;
4961 sess->port_type = FCT_TARGET;
4963 spin_unlock_irqrestore(&tgt->ha->tgt.sess_lock, flags);
4965 res = 1; /* send notify ack */
4967 /* Make session global (not used in fabric mode) */
4968 if (ha->current_topology != ISP_CFG_F) {
4970 ql_dbg(ql_dbg_disc, vha, 0x20fa,
4971 "%s %d %8phC post nack\n",
4972 __func__, __LINE__, sess->port_name);
4973 qla24xx_post_nack_work(vha, sess, iocb,
4977 set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
4978 set_bit(LOCAL_LOOP_UPDATE, &vha->dpc_flags);
4979 qla2xxx_wake_dpc(vha);
4983 ql_dbg(ql_dbg_disc, vha, 0x20fb,
4984 "%s %d %8phC post nack\n",
4985 __func__, __LINE__, sess->port_name);
4986 qla24xx_post_nack_work(vha, sess, iocb,
4994 if (le16_to_cpu(iocb->u.isp24.flags) &
4995 NOTIFY24XX_FLAGS_GLOBAL_TPRLO) {
4997 qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS);
5004 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
5005 sess = qla2x00_find_fcport_by_loopid(vha, loop_id);
5006 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
5010 sess->fw_login_state = DSC_LS_LOGO_PEND;
5011 sess->logo_ack_needed = 1;
5012 memcpy(sess->iocb, iocb, IOCB_SIZE);
5015 res = qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS_SESS);
5017 ql_dbg(ql_dbg_disc, vha, 0x20fc,
5018 "%s: logo %llx res %d sess %p ",
5019 __func__, wwn, res, sess);
5022 * cmd went upper layer, look for qlt_xmit_tm_rsp()
5023 * for LOGO_ACK & sess delete
5028 /* cmd did not go to upper layer. */
5030 qlt_schedule_sess_for_deletion(sess);
5033 /* else logo will be ack */
5039 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
5040 if (tgt->link_reinit_iocb_pending) {
5041 qlt_send_notify_ack(ha->base_qpair,
5042 &tgt->link_reinit_iocb, 0, 0, 0, 0, 0, 0);
5043 tgt->link_reinit_iocb_pending = 0;
5046 sess = qla2x00_find_fcport_by_wwpn(vha,
5047 iocb->u.isp24.port_name, 1);
5049 ql_dbg(ql_dbg_disc, vha, 0x20fd,
5050 "sess %p lid %d|%d DS %d LS %d\n",
5051 sess, sess->loop_id, loop_id,
5052 sess->disc_state, sess->fw_login_state);
5055 res = 1; /* send notify ack */
5059 case ELS_FLOGI: /* should never happen */
5061 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf061,
5062 "qla_target(%d): Unsupported ELS command %x "
5063 "received\n", vha->vp_idx, iocb->u.isp24.status_subcode);
5064 res = qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS_SESS);
5068 ql_dbg(ql_dbg_disc, vha, 0xf026,
5069 "qla_target(%d): Exit ELS opcode: 0x%02x res %d\n",
5070 vha->vp_idx, iocb->u.isp24.status_subcode, res);
5076 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
5078 static void qlt_handle_imm_notify(struct scsi_qla_host *vha,
5079 struct imm_ntfy_from_isp *iocb)
5081 struct qla_hw_data *ha = vha->hw;
5082 uint32_t add_flags = 0;
5083 int send_notify_ack = 1;
5086 status = le16_to_cpu(iocb->u.isp2x.status);
5088 case IMM_NTFY_LIP_RESET:
5090 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf032,
5091 "qla_target(%d): LIP reset (loop %#x), subcode %x\n",
5092 vha->vp_idx, le16_to_cpu(iocb->u.isp24.nport_handle),
5093 iocb->u.isp24.status_subcode);
5095 if (qlt_reset(vha, iocb, QLA_TGT_ABORT_ALL) == 0)
5096 send_notify_ack = 0;
5100 case IMM_NTFY_LIP_LINK_REINIT:
5102 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
5103 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf033,
5104 "qla_target(%d): LINK REINIT (loop %#x, "
5105 "subcode %x)\n", vha->vp_idx,
5106 le16_to_cpu(iocb->u.isp24.nport_handle),
5107 iocb->u.isp24.status_subcode);
5108 if (tgt->link_reinit_iocb_pending) {
5109 qlt_send_notify_ack(ha->base_qpair,
5110 &tgt->link_reinit_iocb, 0, 0, 0, 0, 0, 0);
5112 memcpy(&tgt->link_reinit_iocb, iocb, sizeof(*iocb));
5113 tgt->link_reinit_iocb_pending = 1;
5115 * QLogic requires to wait after LINK REINIT for possible
5116 * PDISC or ADISC ELS commands
5118 send_notify_ack = 0;
5122 case IMM_NTFY_PORT_LOGOUT:
5123 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf034,
5124 "qla_target(%d): Port logout (loop "
5125 "%#x, subcode %x)\n", vha->vp_idx,
5126 le16_to_cpu(iocb->u.isp24.nport_handle),
5127 iocb->u.isp24.status_subcode);
5129 if (qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS_SESS) == 0)
5130 send_notify_ack = 0;
5131 /* The sessions will be cleared in the callback, if needed */
5134 case IMM_NTFY_GLBL_TPRLO:
5135 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf035,
5136 "qla_target(%d): Global TPRLO (%x)\n", vha->vp_idx, status);
5137 if (qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS) == 0)
5138 send_notify_ack = 0;
5139 /* The sessions will be cleared in the callback, if needed */
5142 case IMM_NTFY_PORT_CONFIG:
5143 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf036,
5144 "qla_target(%d): Port config changed (%x)\n", vha->vp_idx,
5146 if (qlt_reset(vha, iocb, QLA_TGT_ABORT_ALL) == 0)
5147 send_notify_ack = 0;
5148 /* The sessions will be cleared in the callback, if needed */
5151 case IMM_NTFY_GLBL_LOGO:
5152 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06a,
5153 "qla_target(%d): Link failure detected\n",
5155 /* I_T nexus loss */
5156 if (qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS) == 0)
5157 send_notify_ack = 0;
5160 case IMM_NTFY_IOCB_OVERFLOW:
5161 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06b,
5162 "qla_target(%d): Cannot provide requested "
5163 "capability (IOCB overflowed the immediate notify "
5164 "resource count)\n", vha->vp_idx);
5167 case IMM_NTFY_ABORT_TASK:
5168 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf037,
5169 "qla_target(%d): Abort Task (S %08x I %#x -> "
5170 "L %#x)\n", vha->vp_idx,
5171 le16_to_cpu(iocb->u.isp2x.seq_id),
5172 GET_TARGET_ID(ha, (struct atio_from_isp *)iocb),
5173 le16_to_cpu(iocb->u.isp2x.lun));
5174 if (qlt_abort_task(vha, iocb) == 0)
5175 send_notify_ack = 0;
5178 case IMM_NTFY_RESOURCE:
5179 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06c,
5180 "qla_target(%d): Out of resources, host %ld\n",
5181 vha->vp_idx, vha->host_no);
5184 case IMM_NTFY_MSG_RX:
5185 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf038,
5186 "qla_target(%d): Immediate notify task %x\n",
5187 vha->vp_idx, iocb->u.isp2x.task_flags);
5191 if (qlt_24xx_handle_els(vha, iocb) == 0)
5192 send_notify_ack = 0;
5195 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06d,
5196 "qla_target(%d): Received unknown immediate "
5197 "notify status %x\n", vha->vp_idx, status);
5201 if (send_notify_ack)
5202 qlt_send_notify_ack(ha->base_qpair, iocb, add_flags, 0, 0, 0,
5207 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
5208 * This function sends busy to ISP 2xxx or 24xx.
5210 static int __qlt_send_busy(struct qla_qpair *qpair,
5211 struct atio_from_isp *atio, uint16_t status)
5213 struct scsi_qla_host *vha = qpair->vha;
5214 struct ctio7_to_24xx *ctio24;
5215 struct qla_hw_data *ha = vha->hw;
5217 struct fc_port *sess = NULL;
5218 unsigned long flags;
5222 id.b.al_pa = atio->u.isp24.fcp_hdr.s_id[2];
5223 id.b.area = atio->u.isp24.fcp_hdr.s_id[1];
5224 id.b.domain = atio->u.isp24.fcp_hdr.s_id[0];
5227 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
5228 sess = qla2x00_find_fcport_by_nportid(vha, &id, 1);
5229 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
5231 qlt_send_term_exchange(qpair, NULL, atio, 1, 0);
5234 /* Sending marker isn't necessary, since we called from ISR */
5236 pkt = (request_t *)__qla2x00_alloc_iocbs(qpair, NULL);
5238 ql_dbg(ql_dbg_io, vha, 0x3063,
5239 "qla_target(%d): %s failed: unable to allocate "
5240 "request packet", vha->vp_idx, __func__);
5244 qpair->tgt_counters.num_q_full_sent++;
5245 pkt->entry_count = 1;
5246 pkt->handle = QLA_TGT_SKIP_HANDLE | CTIO_COMPLETION_HANDLE_MARK;
5248 ctio24 = (struct ctio7_to_24xx *)pkt;
5249 ctio24->entry_type = CTIO_TYPE7;
5250 ctio24->nport_handle = sess->loop_id;
5251 ctio24->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
5252 ctio24->vp_index = vha->vp_idx;
5253 ctio24->initiator_id[0] = atio->u.isp24.fcp_hdr.s_id[2];
5254 ctio24->initiator_id[1] = atio->u.isp24.fcp_hdr.s_id[1];
5255 ctio24->initiator_id[2] = atio->u.isp24.fcp_hdr.s_id[0];
5256 ctio24->exchange_addr = atio->u.isp24.exchange_addr;
5257 temp = (atio->u.isp24.attr << 9) |
5258 CTIO7_FLAGS_STATUS_MODE_1 | CTIO7_FLAGS_SEND_STATUS |
5259 CTIO7_FLAGS_DONT_RET_CTIO;
5260 ctio24->u.status1.flags = cpu_to_le16(temp);
5262 * CTIO from fw w/o se_cmd doesn't provide enough info to retry it,
5263 * if the explicit conformation is used.
5265 ctio24->u.status1.ox_id = swab16(atio->u.isp24.fcp_hdr.ox_id);
5266 ctio24->u.status1.scsi_status = cpu_to_le16(status);
5268 ctio24->u.status1.residual = get_datalen_for_atio(atio);
5270 if (ctio24->u.status1.residual != 0)
5271 ctio24->u.status1.scsi_status |= SS_RESIDUAL_UNDER;
5273 /* Memory Barrier */
5275 if (qpair->reqq_start_iocbs)
5276 qpair->reqq_start_iocbs(qpair);
5278 qla2x00_start_iocbs(vha, qpair->req);
5283 * This routine is used to allocate a command for either a QFull condition
5284 * (ie reply SAM_STAT_BUSY) or to terminate an exchange that did not go
5288 qlt_alloc_qfull_cmd(struct scsi_qla_host *vha,
5289 struct atio_from_isp *atio, uint16_t status, int qfull)
5291 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
5292 struct qla_hw_data *ha = vha->hw;
5293 struct fc_port *sess;
5294 struct se_session *se_sess;
5295 struct qla_tgt_cmd *cmd;
5297 unsigned long flags;
5299 if (unlikely(tgt->tgt_stop)) {
5300 ql_dbg(ql_dbg_io, vha, 0x300a,
5301 "New command while device %p is shutting down\n", tgt);
5305 if ((vha->hw->tgt.num_qfull_cmds_alloc + 1) > MAX_QFULL_CMDS_ALLOC) {
5306 vha->hw->tgt.num_qfull_cmds_dropped++;
5307 if (vha->hw->tgt.num_qfull_cmds_dropped >
5308 vha->qla_stats.stat_max_qfull_cmds_dropped)
5309 vha->qla_stats.stat_max_qfull_cmds_dropped =
5310 vha->hw->tgt.num_qfull_cmds_dropped;
5312 ql_dbg(ql_dbg_io, vha, 0x3068,
5313 "qla_target(%d): %s: QFull CMD dropped[%d]\n",
5314 vha->vp_idx, __func__,
5315 vha->hw->tgt.num_qfull_cmds_dropped);
5317 qlt_chk_exch_leak_thresh_hold(vha);
5321 sess = ha->tgt.tgt_ops->find_sess_by_s_id
5322 (vha, atio->u.isp24.fcp_hdr.s_id);
5326 se_sess = sess->se_sess;
5328 tag = percpu_ida_alloc(&se_sess->sess_tag_pool, TASK_RUNNING);
5332 cmd = &((struct qla_tgt_cmd *)se_sess->sess_cmd_map)[tag];
5334 ql_dbg(ql_dbg_io, vha, 0x3009,
5335 "qla_target(%d): %s: Allocation of cmd failed\n",
5336 vha->vp_idx, __func__);
5338 vha->hw->tgt.num_qfull_cmds_dropped++;
5339 if (vha->hw->tgt.num_qfull_cmds_dropped >
5340 vha->qla_stats.stat_max_qfull_cmds_dropped)
5341 vha->qla_stats.stat_max_qfull_cmds_dropped =
5342 vha->hw->tgt.num_qfull_cmds_dropped;
5344 qlt_chk_exch_leak_thresh_hold(vha);
5348 memset(cmd, 0, sizeof(struct qla_tgt_cmd));
5350 qlt_incr_num_pend_cmds(vha);
5351 INIT_LIST_HEAD(&cmd->cmd_list);
5352 memcpy(&cmd->atio, atio, sizeof(*atio));
5354 cmd->tgt = vha->vha_tgt.qla_tgt;
5356 cmd->reset_count = ha->base_qpair->chip_reset;
5358 cmd->qpair = ha->base_qpair;
5362 /* NOTE: borrowing the state field to carry the status */
5363 cmd->state = status;
5365 cmd->term_exchg = 1;
5367 spin_lock_irqsave(&vha->hw->tgt.q_full_lock, flags);
5368 list_add_tail(&cmd->cmd_list, &vha->hw->tgt.q_full_list);
5370 vha->hw->tgt.num_qfull_cmds_alloc++;
5371 if (vha->hw->tgt.num_qfull_cmds_alloc >
5372 vha->qla_stats.stat_max_qfull_cmds_alloc)
5373 vha->qla_stats.stat_max_qfull_cmds_alloc =
5374 vha->hw->tgt.num_qfull_cmds_alloc;
5375 spin_unlock_irqrestore(&vha->hw->tgt.q_full_lock, flags);
5379 qlt_free_qfull_cmds(struct qla_qpair *qpair)
5381 struct scsi_qla_host *vha = qpair->vha;
5382 struct qla_hw_data *ha = vha->hw;
5383 unsigned long flags;
5384 struct qla_tgt_cmd *cmd, *tcmd;
5385 struct list_head free_list, q_full_list;
5388 if (list_empty(&ha->tgt.q_full_list))
5391 INIT_LIST_HEAD(&free_list);
5392 INIT_LIST_HEAD(&q_full_list);
5394 spin_lock_irqsave(&vha->hw->tgt.q_full_lock, flags);
5395 if (list_empty(&ha->tgt.q_full_list)) {
5396 spin_unlock_irqrestore(&vha->hw->tgt.q_full_lock, flags);
5400 list_splice_init(&vha->hw->tgt.q_full_list, &q_full_list);
5401 spin_unlock_irqrestore(&vha->hw->tgt.q_full_lock, flags);
5403 spin_lock_irqsave(qpair->qp_lock_ptr, flags);
5404 list_for_each_entry_safe(cmd, tcmd, &q_full_list, cmd_list) {
5406 /* cmd->state is a borrowed field to hold status */
5407 rc = __qlt_send_busy(qpair, &cmd->atio, cmd->state);
5408 else if (cmd->term_exchg)
5409 rc = __qlt_send_term_exchange(qpair, NULL, &cmd->atio);
5415 ql_dbg(ql_dbg_io, vha, 0x3006,
5416 "%s: busy sent for ox_id[%04x]\n", __func__,
5417 be16_to_cpu(cmd->atio.u.isp24.fcp_hdr.ox_id));
5418 else if (cmd->term_exchg)
5419 ql_dbg(ql_dbg_io, vha, 0x3007,
5420 "%s: Term exchg sent for ox_id[%04x]\n", __func__,
5421 be16_to_cpu(cmd->atio.u.isp24.fcp_hdr.ox_id));
5423 ql_dbg(ql_dbg_io, vha, 0x3008,
5424 "%s: Unexpected cmd in QFull list %p\n", __func__,
5427 list_del(&cmd->cmd_list);
5428 list_add_tail(&cmd->cmd_list, &free_list);
5430 /* piggy back on hardware_lock for protection */
5431 vha->hw->tgt.num_qfull_cmds_alloc--;
5433 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
5437 list_for_each_entry_safe(cmd, tcmd, &free_list, cmd_list) {
5438 list_del(&cmd->cmd_list);
5439 /* This cmd was never sent to TCM. There is no need
5440 * to schedule free or call free_cmd
5445 if (!list_empty(&q_full_list)) {
5446 spin_lock_irqsave(&vha->hw->tgt.q_full_lock, flags);
5447 list_splice(&q_full_list, &vha->hw->tgt.q_full_list);
5448 spin_unlock_irqrestore(&vha->hw->tgt.q_full_lock, flags);
5455 qlt_send_busy(struct qla_qpair *qpair, struct atio_from_isp *atio,
5459 struct scsi_qla_host *vha = qpair->vha;
5461 rc = __qlt_send_busy(qpair, atio, status);
5463 qlt_alloc_qfull_cmd(vha, atio, status, 1);
5467 qlt_chk_qfull_thresh_hold(struct scsi_qla_host *vha, struct qla_qpair *qpair,
5468 struct atio_from_isp *atio, uint8_t ha_locked)
5470 struct qla_hw_data *ha = vha->hw;
5471 unsigned long flags;
5473 if (ha->tgt.num_pend_cmds < Q_FULL_THRESH_HOLD(ha))
5477 spin_lock_irqsave(&ha->hardware_lock, flags);
5478 qlt_send_busy(qpair, atio, qla_sam_status);
5480 spin_unlock_irqrestore(&ha->hardware_lock, flags);
5485 /* ha->hardware_lock supposed to be held on entry */
5486 /* called via callback from qla2xxx */
5487 static void qlt_24xx_atio_pkt(struct scsi_qla_host *vha,
5488 struct atio_from_isp *atio, uint8_t ha_locked)
5490 struct qla_hw_data *ha = vha->hw;
5491 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
5493 unsigned long flags = 0;
5495 if (unlikely(tgt == NULL)) {
5496 ql_dbg(ql_dbg_tgt, vha, 0x3064,
5497 "ATIO pkt, but no tgt (ha %p)", ha);
5501 * In tgt_stop mode we also should allow all requests to pass.
5502 * Otherwise, some commands can stuck.
5505 tgt->atio_irq_cmd_count++;
5507 switch (atio->u.raw.entry_type) {
5509 if (unlikely(atio->u.isp24.exchange_addr ==
5510 ATIO_EXCHANGE_ADDRESS_UNKNOWN)) {
5511 ql_dbg(ql_dbg_io, vha, 0x3065,
5512 "qla_target(%d): ATIO_TYPE7 "
5513 "received with UNKNOWN exchange address, "
5514 "sending QUEUE_FULL\n", vha->vp_idx);
5516 spin_lock_irqsave(&ha->hardware_lock, flags);
5517 qlt_send_busy(ha->base_qpair, atio, qla_sam_status);
5519 spin_unlock_irqrestore(&ha->hardware_lock,
5524 if (likely(atio->u.isp24.fcp_cmnd.task_mgmt_flags == 0)) {
5525 rc = qlt_chk_qfull_thresh_hold(vha, ha->base_qpair,
5528 tgt->atio_irq_cmd_count--;
5531 rc = qlt_handle_cmd_for_atio(vha, atio);
5533 rc = qlt_handle_task_mgmt(vha, atio);
5535 if (unlikely(rc != 0)) {
5537 spin_lock_irqsave(&ha->hardware_lock, flags);
5540 ql_dbg(ql_dbg_tgt, vha, 0xe05f,
5541 "qla_target: Unable to send command to target\n");
5544 ql_dbg(ql_dbg_tgt, vha, 0xe05f,
5545 "qla_target: Unable to send command to target, sending TERM EXCHANGE for rsp\n");
5546 qlt_send_term_exchange(ha->base_qpair, NULL,
5550 ql_dbg(ql_dbg_tgt, vha, 0xe060,
5551 "qla_target(%d): Unable to send command to target, sending BUSY status\n",
5553 qlt_send_busy(ha->base_qpair, atio,
5557 ql_dbg(ql_dbg_tgt, vha, 0xe060,
5558 "qla_target(%d): Unable to send command to target, sending BUSY status\n",
5560 qlt_send_busy(ha->base_qpair, atio,
5565 spin_unlock_irqrestore(&ha->hardware_lock,
5570 case IMMED_NOTIFY_TYPE:
5572 if (unlikely(atio->u.isp2x.entry_status != 0)) {
5573 ql_dbg(ql_dbg_tgt, vha, 0xe05b,
5574 "qla_target(%d): Received ATIO packet %x "
5575 "with error status %x\n", vha->vp_idx,
5576 atio->u.raw.entry_type,
5577 atio->u.isp2x.entry_status);
5580 ql_dbg(ql_dbg_tgt, vha, 0xe02e, "%s", "IMMED_NOTIFY ATIO");
5583 spin_lock_irqsave(&ha->hardware_lock, flags);
5584 qlt_handle_imm_notify(vha, (struct imm_ntfy_from_isp *)atio);
5586 spin_unlock_irqrestore(&ha->hardware_lock, flags);
5591 ql_dbg(ql_dbg_tgt, vha, 0xe05c,
5592 "qla_target(%d): Received unknown ATIO atio "
5593 "type %x\n", vha->vp_idx, atio->u.raw.entry_type);
5597 tgt->atio_irq_cmd_count--;
5600 /* ha->hardware_lock supposed to be held on entry */
5601 /* called via callback from qla2xxx */
5602 static void qlt_response_pkt(struct scsi_qla_host *vha,
5603 struct rsp_que *rsp, response_t *pkt)
5605 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
5607 if (unlikely(tgt == NULL)) {
5608 ql_dbg(ql_dbg_tgt, vha, 0xe05d,
5609 "qla_target(%d): Response pkt %x received, but no tgt (ha %p)\n",
5610 vha->vp_idx, pkt->entry_type, vha->hw);
5615 * In tgt_stop mode we also should allow all requests to pass.
5616 * Otherwise, some commands can stuck.
5619 switch (pkt->entry_type) {
5623 struct ctio7_from_24xx *entry = (struct ctio7_from_24xx *)pkt;
5624 qlt_do_ctio_completion(vha, rsp, entry->handle,
5625 le16_to_cpu(entry->status)|(pkt->entry_status << 16),
5630 case ACCEPT_TGT_IO_TYPE:
5632 struct atio_from_isp *atio = (struct atio_from_isp *)pkt;
5634 if (atio->u.isp2x.status !=
5635 cpu_to_le16(ATIO_CDB_VALID)) {
5636 ql_dbg(ql_dbg_tgt, vha, 0xe05e,
5637 "qla_target(%d): ATIO with error "
5638 "status %x received\n", vha->vp_idx,
5639 le16_to_cpu(atio->u.isp2x.status));
5643 rc = qlt_chk_qfull_thresh_hold(vha, rsp->qpair, atio, 1);
5647 rc = qlt_handle_cmd_for_atio(vha, atio);
5648 if (unlikely(rc != 0)) {
5651 ql_dbg(ql_dbg_tgt, vha, 0xe05f,
5652 "qla_target: Unable to send command to target\n");
5655 ql_dbg(ql_dbg_tgt, vha, 0xe05f,
5656 "qla_target: Unable to send command to target, sending TERM EXCHANGE for rsp\n");
5657 qlt_send_term_exchange(rsp->qpair, NULL,
5661 ql_dbg(ql_dbg_tgt, vha, 0xe060,
5662 "qla_target(%d): Unable to send command to target, sending BUSY status\n",
5664 qlt_send_busy(rsp->qpair, atio,
5668 ql_dbg(ql_dbg_tgt, vha, 0xe060,
5669 "qla_target(%d): Unable to send command to target, sending BUSY status\n",
5671 qlt_send_busy(rsp->qpair, atio,
5679 case CONTINUE_TGT_IO_TYPE:
5681 struct ctio_to_2xxx *entry = (struct ctio_to_2xxx *)pkt;
5682 qlt_do_ctio_completion(vha, rsp, entry->handle,
5683 le16_to_cpu(entry->status)|(pkt->entry_status << 16),
5690 struct ctio_to_2xxx *entry = (struct ctio_to_2xxx *)pkt;
5691 qlt_do_ctio_completion(vha, rsp, entry->handle,
5692 le16_to_cpu(entry->status)|(pkt->entry_status << 16),
5697 case IMMED_NOTIFY_TYPE:
5698 ql_dbg(ql_dbg_tgt, vha, 0xe035, "%s", "IMMED_NOTIFY\n");
5699 qlt_handle_imm_notify(vha, (struct imm_ntfy_from_isp *)pkt);
5702 case NOTIFY_ACK_TYPE:
5703 if (tgt->notify_ack_expected > 0) {
5704 struct nack_to_isp *entry = (struct nack_to_isp *)pkt;
5705 ql_dbg(ql_dbg_tgt, vha, 0xe036,
5706 "NOTIFY_ACK seq %08x status %x\n",
5707 le16_to_cpu(entry->u.isp2x.seq_id),
5708 le16_to_cpu(entry->u.isp2x.status));
5709 tgt->notify_ack_expected--;
5710 if (entry->u.isp2x.status !=
5711 cpu_to_le16(NOTIFY_ACK_SUCCESS)) {
5712 ql_dbg(ql_dbg_tgt, vha, 0xe061,
5713 "qla_target(%d): NOTIFY_ACK "
5714 "failed %x\n", vha->vp_idx,
5715 le16_to_cpu(entry->u.isp2x.status));
5718 ql_dbg(ql_dbg_tgt, vha, 0xe062,
5719 "qla_target(%d): Unexpected NOTIFY_ACK received\n",
5724 case ABTS_RECV_24XX:
5725 ql_dbg(ql_dbg_tgt, vha, 0xe037,
5726 "ABTS_RECV_24XX: instance %d\n", vha->vp_idx);
5727 qlt_24xx_handle_abts(vha, (struct abts_recv_from_24xx *)pkt);
5730 case ABTS_RESP_24XX:
5731 if (tgt->abts_resp_expected > 0) {
5732 struct abts_resp_from_24xx_fw *entry =
5733 (struct abts_resp_from_24xx_fw *)pkt;
5734 ql_dbg(ql_dbg_tgt, vha, 0xe038,
5735 "ABTS_RESP_24XX: compl_status %x\n",
5736 entry->compl_status);
5737 tgt->abts_resp_expected--;
5738 if (le16_to_cpu(entry->compl_status) !=
5739 ABTS_RESP_COMPL_SUCCESS) {
5740 if ((entry->error_subcode1 == 0x1E) &&
5741 (entry->error_subcode2 == 0)) {
5743 * We've got a race here: aborted
5744 * exchange not terminated, i.e.
5745 * response for the aborted command was
5746 * sent between the abort request was
5747 * received and processed.
5748 * Unfortunately, the firmware has a
5749 * silly requirement that all aborted
5750 * exchanges must be explicitely
5751 * terminated, otherwise it refuses to
5752 * send responses for the abort
5753 * requests. So, we have to
5754 * (re)terminate the exchange and retry
5755 * the abort response.
5757 qlt_24xx_retry_term_exchange(vha,
5760 ql_dbg(ql_dbg_tgt, vha, 0xe063,
5761 "qla_target(%d): ABTS_RESP_24XX "
5762 "failed %x (subcode %x:%x)",
5763 vha->vp_idx, entry->compl_status,
5764 entry->error_subcode1,
5765 entry->error_subcode2);
5768 ql_dbg(ql_dbg_tgt, vha, 0xe064,
5769 "qla_target(%d): Unexpected ABTS_RESP_24XX "
5770 "received\n", vha->vp_idx);
5775 ql_dbg(ql_dbg_tgt, vha, 0xe065,
5776 "qla_target(%d): Received unknown response pkt "
5777 "type %x\n", vha->vp_idx, pkt->entry_type);
5784 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
5786 void qlt_async_event(uint16_t code, struct scsi_qla_host *vha,
5789 struct qla_hw_data *ha = vha->hw;
5790 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
5793 if (!tgt || tgt->tgt_stop || tgt->tgt_stopped)
5796 if (((code == MBA_POINT_TO_POINT) || (code == MBA_CHG_IN_CONNECTION)) &&
5800 * In tgt_stop mode we also should allow all requests to pass.
5801 * Otherwise, some commands can stuck.
5806 case MBA_RESET: /* Reset */
5807 case MBA_SYSTEM_ERR: /* System Error */
5808 case MBA_REQ_TRANSFER_ERR: /* Request Transfer Error */
5809 case MBA_RSP_TRANSFER_ERR: /* Response Transfer Error */
5810 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03a,
5811 "qla_target(%d): System error async event %#x "
5812 "occurred", vha->vp_idx, code);
5814 case MBA_WAKEUP_THRES: /* Request Queue Wake-up. */
5815 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
5820 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03b,
5821 "qla_target(%d): Async LOOP_UP occurred "
5822 "(m[0]=%x, m[1]=%x, m[2]=%x, m[3]=%x)", vha->vp_idx,
5823 le16_to_cpu(mailbox[0]), le16_to_cpu(mailbox[1]),
5824 le16_to_cpu(mailbox[2]), le16_to_cpu(mailbox[3]));
5825 if (tgt->link_reinit_iocb_pending) {
5826 qlt_send_notify_ack(ha->base_qpair,
5827 (void *)&tgt->link_reinit_iocb,
5829 tgt->link_reinit_iocb_pending = 0;
5834 case MBA_LIP_OCCURRED:
5837 case MBA_RSCN_UPDATE:
5838 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03c,
5839 "qla_target(%d): Async event %#x occurred "
5840 "(m[0]=%x, m[1]=%x, m[2]=%x, m[3]=%x)", vha->vp_idx, code,
5841 le16_to_cpu(mailbox[0]), le16_to_cpu(mailbox[1]),
5842 le16_to_cpu(mailbox[2]), le16_to_cpu(mailbox[3]));
5845 case MBA_REJECTED_FCP_CMD:
5846 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf017,
5847 "qla_target(%d): Async event LS_REJECT occurred (m[0]=%x, m[1]=%x, m[2]=%x, m[3]=%x)",
5849 le16_to_cpu(mailbox[0]), le16_to_cpu(mailbox[1]),
5850 le16_to_cpu(mailbox[2]), le16_to_cpu(mailbox[3]));
5852 if (le16_to_cpu(mailbox[3]) == 1) {
5853 /* exchange starvation. */
5854 vha->hw->exch_starvation++;
5855 if (vha->hw->exch_starvation > 5) {
5856 ql_log(ql_log_warn, vha, 0xd03a,
5857 "Exchange starvation-. Resetting RISC\n");
5859 vha->hw->exch_starvation = 0;
5860 if (IS_P3P_TYPE(vha->hw))
5861 set_bit(FCOE_CTX_RESET_NEEDED,
5864 set_bit(ISP_ABORT_NEEDED,
5866 qla2xxx_wake_dpc(vha);
5871 case MBA_PORT_UPDATE:
5872 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03d,
5873 "qla_target(%d): Port update async event %#x "
5874 "occurred: updating the ports database (m[0]=%x, m[1]=%x, "
5875 "m[2]=%x, m[3]=%x)", vha->vp_idx, code,
5876 le16_to_cpu(mailbox[0]), le16_to_cpu(mailbox[1]),
5877 le16_to_cpu(mailbox[2]), le16_to_cpu(mailbox[3]));
5879 login_code = le16_to_cpu(mailbox[2]);
5880 if (login_code == 0x4) {
5881 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03e,
5882 "Async MB 2: Got PLOGI Complete\n");
5883 vha->hw->exch_starvation = 0;
5884 } else if (login_code == 0x7)
5885 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03f,
5886 "Async MB 2: Port Logged Out\n");
5894 static fc_port_t *qlt_get_port_database(struct scsi_qla_host *vha,
5897 fc_port_t *fcport, *tfcp, *del;
5899 unsigned long flags;
5902 fcport = qla2x00_alloc_fcport(vha, GFP_KERNEL);
5904 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06f,
5905 "qla_target(%d): Allocation of tmp FC port failed",
5910 fcport->loop_id = loop_id;
5912 rc = qla24xx_gpdb_wait(vha, fcport, 0);
5913 if (rc != QLA_SUCCESS) {
5914 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf070,
5915 "qla_target(%d): Failed to retrieve fcport "
5916 "information -- get_port_database() returned %x "
5917 "(loop_id=0x%04x)", vha->vp_idx, rc, loop_id);
5923 spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
5924 tfcp = qla2x00_find_fcport_by_wwpn(vha, fcport->port_name, 1);
5927 tfcp->d_id = fcport->d_id;
5928 tfcp->port_type = fcport->port_type;
5929 tfcp->supported_classes = fcport->supported_classes;
5930 tfcp->flags |= fcport->flags;
5931 tfcp->scan_state = QLA_FCPORT_FOUND;
5936 if (vha->hw->current_topology == ISP_CFG_F)
5937 fcport->flags |= FCF_FABRIC_DEVICE;
5939 list_add_tail(&fcport->list, &vha->vp_fcports);
5940 if (!IS_SW_RESV_ADDR(fcport->d_id))
5941 vha->fcport_count++;
5942 fcport->login_gen++;
5943 fcport->disc_state = DSC_LOGIN_COMPLETE;
5944 fcport->login_succ = 1;
5948 fcport->deleted = 0;
5949 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
5951 switch (vha->host->active_mode) {
5952 case MODE_INITIATOR:
5955 if (!IS_IIDMA_CAPABLE(vha->hw) || !vha->hw->flags.gpsc_supported) {
5956 ql_dbg(ql_dbg_disc, vha, 0x20fe,
5957 "%s %d %8phC post upd_fcport fcp_cnt %d\n",
5958 __func__, __LINE__, fcport->port_name, vha->fcport_count);
5959 qla24xx_post_upd_fcport_work(vha, fcport);
5961 ql_dbg(ql_dbg_disc, vha, 0x20ff,
5962 "%s %d %8phC post gpsc fcp_cnt %d\n",
5963 __func__, __LINE__, fcport->port_name, vha->fcport_count);
5964 qla24xx_post_gpsc_work(vha, fcport);
5974 qla2x00_free_fcport(del);
5979 /* Must be called under tgt_mutex */
5980 static struct fc_port *qlt_make_local_sess(struct scsi_qla_host *vha,
5983 struct fc_port *sess = NULL;
5984 fc_port_t *fcport = NULL;
5985 int rc, global_resets;
5986 uint16_t loop_id = 0;
5988 if ((s_id[0] == 0xFF) && (s_id[1] == 0xFC)) {
5990 * This is Domain Controller, so it should be
5991 * OK to drop SCSI commands from it.
5993 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf042,
5994 "Unable to find initiator with S_ID %x:%x:%x",
5995 s_id[0], s_id[1], s_id[2]);
5999 mutex_lock(&vha->vha_tgt.tgt_mutex);
6003 atomic_read(&vha->vha_tgt.qla_tgt->tgt_global_resets_count);
6005 rc = qla24xx_get_loop_id(vha, s_id, &loop_id);
6007 mutex_unlock(&vha->vha_tgt.tgt_mutex);
6009 ql_log(ql_log_info, vha, 0xf071,
6010 "qla_target(%d): Unable to find "
6011 "initiator with S_ID %x:%x:%x",
6012 vha->vp_idx, s_id[0], s_id[1],
6015 if (rc == -ENOENT) {
6016 qlt_port_logo_t logo;
6017 sid_to_portid(s_id, &logo.id);
6019 qlt_send_first_logo(vha, &logo);
6025 fcport = qlt_get_port_database(vha, loop_id);
6027 mutex_unlock(&vha->vha_tgt.tgt_mutex);
6031 if (global_resets !=
6032 atomic_read(&vha->vha_tgt.qla_tgt->tgt_global_resets_count)) {
6033 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf043,
6034 "qla_target(%d): global reset during session discovery "
6035 "(counter was %d, new %d), retrying", vha->vp_idx,
6037 atomic_read(&vha->vha_tgt.
6038 qla_tgt->tgt_global_resets_count));
6042 sess = qlt_create_sess(vha, fcport, true);
6044 mutex_unlock(&vha->vha_tgt.tgt_mutex);
6049 static void qlt_abort_work(struct qla_tgt *tgt,
6050 struct qla_tgt_sess_work_param *prm)
6052 struct scsi_qla_host *vha = tgt->vha;
6053 struct qla_hw_data *ha = vha->hw;
6054 struct fc_port *sess = NULL;
6055 unsigned long flags = 0, flags2 = 0;
6060 spin_lock_irqsave(&ha->tgt.sess_lock, flags2);
6065 s_id[0] = prm->abts.fcp_hdr_le.s_id[2];
6066 s_id[1] = prm->abts.fcp_hdr_le.s_id[1];
6067 s_id[2] = prm->abts.fcp_hdr_le.s_id[0];
6069 sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha,
6070 (unsigned char *)&be_s_id);
6072 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags2);
6074 sess = qlt_make_local_sess(vha, s_id);
6075 /* sess has got an extra creation ref */
6077 spin_lock_irqsave(&ha->tgt.sess_lock, flags2);
6081 if (sess->deleted) {
6086 if (!kref_get_unless_zero(&sess->sess_kref)) {
6087 ql_dbg(ql_dbg_tgt_tmr, vha, 0xf01c,
6088 "%s: kref_get fail %8phC \n",
6089 __func__, sess->port_name);
6095 rc = __qlt_24xx_handle_abts(vha, &prm->abts, sess);
6096 ha->tgt.tgt_ops->put_sess(sess);
6097 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags2);
6105 ha->tgt.tgt_ops->put_sess(sess);
6106 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags2);
6109 spin_lock_irqsave(&ha->hardware_lock, flags);
6110 qlt_24xx_send_abts_resp(ha->base_qpair, &prm->abts,
6111 FCP_TMF_REJECTED, false);
6112 spin_unlock_irqrestore(&ha->hardware_lock, flags);
6115 static void qlt_tmr_work(struct qla_tgt *tgt,
6116 struct qla_tgt_sess_work_param *prm)
6118 struct atio_from_isp *a = &prm->tm_iocb2;
6119 struct scsi_qla_host *vha = tgt->vha;
6120 struct qla_hw_data *ha = vha->hw;
6121 struct fc_port *sess = NULL;
6122 unsigned long flags;
6123 uint8_t *s_id = NULL; /* to hide compiler warnings */
6129 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
6134 s_id = prm->tm_iocb2.u.isp24.fcp_hdr.s_id;
6135 sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha, s_id);
6137 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
6139 sess = qlt_make_local_sess(vha, s_id);
6140 /* sess has got an extra creation ref */
6142 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
6146 if (sess->deleted) {
6151 if (!kref_get_unless_zero(&sess->sess_kref)) {
6152 ql_dbg(ql_dbg_tgt_tmr, vha, 0xf020,
6153 "%s: kref_get fail %8phC\n",
6154 __func__, sess->port_name);
6161 fn = a->u.isp24.fcp_cmnd.task_mgmt_flags;
6163 scsilun_to_int((struct scsi_lun *)&a->u.isp24.fcp_cmnd.lun);
6165 rc = qlt_issue_task_mgmt(sess, unpacked_lun, fn, iocb, 0);
6166 ha->tgt.tgt_ops->put_sess(sess);
6167 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
6175 ha->tgt.tgt_ops->put_sess(sess);
6176 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
6178 qlt_send_term_exchange(ha->base_qpair, NULL, &prm->tm_iocb2, 1, 0);
6181 static void qlt_sess_work_fn(struct work_struct *work)
6183 struct qla_tgt *tgt = container_of(work, struct qla_tgt, sess_work);
6184 struct scsi_qla_host *vha = tgt->vha;
6185 unsigned long flags;
6187 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf000, "Sess work (tgt %p)", tgt);
6189 spin_lock_irqsave(&tgt->sess_work_lock, flags);
6190 while (!list_empty(&tgt->sess_works_list)) {
6191 struct qla_tgt_sess_work_param *prm = list_entry(
6192 tgt->sess_works_list.next, typeof(*prm),
6193 sess_works_list_entry);
6196 * This work can be scheduled on several CPUs at time, so we
6197 * must delete the entry to eliminate double processing
6199 list_del(&prm->sess_works_list_entry);
6201 spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
6203 switch (prm->type) {
6204 case QLA_TGT_SESS_WORK_ABORT:
6205 qlt_abort_work(tgt, prm);
6207 case QLA_TGT_SESS_WORK_TM:
6208 qlt_tmr_work(tgt, prm);
6215 spin_lock_irqsave(&tgt->sess_work_lock, flags);
6219 spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
6222 /* Must be called under tgt_host_action_mutex */
6223 int qlt_add_target(struct qla_hw_data *ha, struct scsi_qla_host *base_vha)
6225 struct qla_tgt *tgt;
6227 struct qla_qpair_hint *h;
6229 if (!QLA_TGT_MODE_ENABLED())
6232 if (!IS_TGT_MODE_CAPABLE(ha)) {
6233 ql_log(ql_log_warn, base_vha, 0xe070,
6234 "This adapter does not support target mode.\n");
6238 ql_dbg(ql_dbg_tgt, base_vha, 0xe03b,
6239 "Registering target for host %ld(%p).\n", base_vha->host_no, ha);
6241 BUG_ON(base_vha->vha_tgt.qla_tgt != NULL);
6243 tgt = kzalloc(sizeof(struct qla_tgt), GFP_KERNEL);
6245 ql_dbg(ql_dbg_tgt, base_vha, 0xe066,
6246 "Unable to allocate struct qla_tgt\n");
6250 tgt->qphints = kcalloc(ha->max_qpairs + 1,
6251 sizeof(struct qla_qpair_hint),
6253 if (!tgt->qphints) {
6255 ql_log(ql_log_warn, base_vha, 0x0197,
6256 "Unable to allocate qpair hints.\n");
6260 if (!(base_vha->host->hostt->supported_mode & MODE_TARGET))
6261 base_vha->host->hostt->supported_mode |= MODE_TARGET;
6263 rc = btree_init64(&tgt->lun_qpair_map);
6265 kfree(tgt->qphints);
6267 ql_log(ql_log_info, base_vha, 0x0198,
6268 "Unable to initialize lun_qpair_map btree\n");
6271 h = &tgt->qphints[0];
6272 h->qpair = ha->base_qpair;
6273 INIT_LIST_HEAD(&h->hint_elem);
6274 h->cpuid = ha->base_qpair->cpuid;
6275 list_add_tail(&h->hint_elem, &ha->base_qpair->hints_list);
6277 for (i = 0; i < ha->max_qpairs; i++) {
6278 unsigned long flags;
6280 struct qla_qpair *qpair = ha->queue_pair_map[i];
6281 h = &tgt->qphints[i + 1];
6282 INIT_LIST_HEAD(&h->hint_elem);
6285 spin_lock_irqsave(qpair->qp_lock_ptr, flags);
6286 list_add_tail(&h->hint_elem, &qpair->hints_list);
6287 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
6288 h->cpuid = qpair->cpuid;
6293 tgt->vha = base_vha;
6294 init_waitqueue_head(&tgt->waitQ);
6295 INIT_LIST_HEAD(&tgt->del_sess_list);
6296 spin_lock_init(&tgt->sess_work_lock);
6297 INIT_WORK(&tgt->sess_work, qlt_sess_work_fn);
6298 INIT_LIST_HEAD(&tgt->sess_works_list);
6299 atomic_set(&tgt->tgt_global_resets_count, 0);
6301 base_vha->vha_tgt.qla_tgt = tgt;
6303 ql_dbg(ql_dbg_tgt, base_vha, 0xe067,
6304 "qla_target(%d): using 64 Bit PCI addressing",
6307 tgt->sg_tablesize = QLA_TGT_MAX_SG_24XX(base_vha->req->length - 3);
6309 mutex_lock(&qla_tgt_mutex);
6310 list_add_tail(&tgt->tgt_list_entry, &qla_tgt_glist);
6311 mutex_unlock(&qla_tgt_mutex);
6313 if (ha->tgt.tgt_ops && ha->tgt.tgt_ops->add_target)
6314 ha->tgt.tgt_ops->add_target(base_vha);
6319 /* Must be called under tgt_host_action_mutex */
6320 int qlt_remove_target(struct qla_hw_data *ha, struct scsi_qla_host *vha)
6322 if (!vha->vha_tgt.qla_tgt)
6325 if (vha->fc_vport) {
6326 qlt_release(vha->vha_tgt.qla_tgt);
6330 /* free left over qfull cmds */
6331 qlt_init_term_exchange(vha);
6333 ql_dbg(ql_dbg_tgt, vha, 0xe03c, "Unregistering target for host %ld(%p)",
6335 qlt_release(vha->vha_tgt.qla_tgt);
6340 void qlt_remove_target_resources(struct qla_hw_data *ha)
6342 struct scsi_qla_host *node;
6345 btree_for_each_safe32(&ha->tgt.host_map, key, node)
6346 btree_remove32(&ha->tgt.host_map, key);
6348 btree_destroy32(&ha->tgt.host_map);
6351 static void qlt_lport_dump(struct scsi_qla_host *vha, u64 wwpn,
6356 pr_debug("qla2xxx HW vha->node_name: ");
6357 for (i = 0; i < WWN_SIZE; i++)
6358 pr_debug("%02x ", vha->node_name[i]);
6360 pr_debug("qla2xxx HW vha->port_name: ");
6361 for (i = 0; i < WWN_SIZE; i++)
6362 pr_debug("%02x ", vha->port_name[i]);
6365 pr_debug("qla2xxx passed configfs WWPN: ");
6366 put_unaligned_be64(wwpn, b);
6367 for (i = 0; i < WWN_SIZE; i++)
6368 pr_debug("%02x ", b[i]);
6373 * qla_tgt_lport_register - register lport with external module
6375 * @target_lport_ptr: pointer for tcm_qla2xxx specific lport data
6379 * @callback: lport initialization callback for tcm_qla2xxx code
6381 int qlt_lport_register(void *target_lport_ptr, u64 phys_wwpn,
6382 u64 npiv_wwpn, u64 npiv_wwnn,
6383 int (*callback)(struct scsi_qla_host *, void *, u64, u64))
6385 struct qla_tgt *tgt;
6386 struct scsi_qla_host *vha;
6387 struct qla_hw_data *ha;
6388 struct Scsi_Host *host;
6389 unsigned long flags;
6393 mutex_lock(&qla_tgt_mutex);
6394 list_for_each_entry(tgt, &qla_tgt_glist, tgt_list_entry) {
6402 if (!(host->hostt->supported_mode & MODE_TARGET))
6405 spin_lock_irqsave(&ha->hardware_lock, flags);
6406 if ((!npiv_wwpn || !npiv_wwnn) && host->active_mode & MODE_TARGET) {
6407 pr_debug("MODE_TARGET already active on qla2xxx(%d)\n",
6409 spin_unlock_irqrestore(&ha->hardware_lock, flags);
6412 if (tgt->tgt_stop) {
6413 pr_debug("MODE_TARGET in shutdown on qla2xxx(%d)\n",
6415 spin_unlock_irqrestore(&ha->hardware_lock, flags);
6418 spin_unlock_irqrestore(&ha->hardware_lock, flags);
6420 if (!scsi_host_get(host)) {
6421 ql_dbg(ql_dbg_tgt, vha, 0xe068,
6422 "Unable to scsi_host_get() for"
6423 " qla2xxx scsi_host\n");
6426 qlt_lport_dump(vha, phys_wwpn, b);
6428 if (memcmp(vha->port_name, b, WWN_SIZE)) {
6429 scsi_host_put(host);
6432 rc = (*callback)(vha, target_lport_ptr, npiv_wwpn, npiv_wwnn);
6434 scsi_host_put(host);
6436 mutex_unlock(&qla_tgt_mutex);
6439 mutex_unlock(&qla_tgt_mutex);
6443 EXPORT_SYMBOL(qlt_lport_register);
6446 * qla_tgt_lport_deregister - Degister lport
6448 * @vha: Registered scsi_qla_host pointer
6450 void qlt_lport_deregister(struct scsi_qla_host *vha)
6452 struct qla_hw_data *ha = vha->hw;
6453 struct Scsi_Host *sh = vha->host;
6455 * Clear the target_lport_ptr qla_target_template pointer in qla_hw_data
6457 vha->vha_tgt.target_lport_ptr = NULL;
6458 ha->tgt.tgt_ops = NULL;
6460 * Release the Scsi_Host reference for the underlying qla2xxx host
6464 EXPORT_SYMBOL(qlt_lport_deregister);
6466 /* Must be called under HW lock */
6467 static void qlt_set_mode(struct scsi_qla_host *vha)
6469 switch (ql2x_ini_mode) {
6470 case QLA2XXX_INI_MODE_DISABLED:
6471 case QLA2XXX_INI_MODE_EXCLUSIVE:
6472 vha->host->active_mode = MODE_TARGET;
6474 case QLA2XXX_INI_MODE_ENABLED:
6475 vha->host->active_mode = MODE_UNKNOWN;
6477 case QLA2XXX_INI_MODE_DUAL:
6478 vha->host->active_mode = MODE_DUAL;
6485 /* Must be called under HW lock */
6486 static void qlt_clear_mode(struct scsi_qla_host *vha)
6488 switch (ql2x_ini_mode) {
6489 case QLA2XXX_INI_MODE_DISABLED:
6490 vha->host->active_mode = MODE_UNKNOWN;
6492 case QLA2XXX_INI_MODE_EXCLUSIVE:
6493 vha->host->active_mode = MODE_INITIATOR;
6495 case QLA2XXX_INI_MODE_ENABLED:
6496 case QLA2XXX_INI_MODE_DUAL:
6497 vha->host->active_mode = MODE_INITIATOR;
6505 * qla_tgt_enable_vha - NO LOCK HELD
6507 * host_reset, bring up w/ Target Mode Enabled
6510 qlt_enable_vha(struct scsi_qla_host *vha)
6512 struct qla_hw_data *ha = vha->hw;
6513 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
6514 unsigned long flags;
6515 scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
6518 ql_dbg(ql_dbg_tgt, vha, 0xe069,
6519 "Unable to locate qla_tgt pointer from"
6520 " struct qla_hw_data\n");
6525 spin_lock_irqsave(&ha->hardware_lock, flags);
6526 tgt->tgt_stopped = 0;
6528 spin_unlock_irqrestore(&ha->hardware_lock, flags);
6531 qla24xx_disable_vp(vha);
6532 qla24xx_enable_vp(vha);
6534 set_bit(ISP_ABORT_NEEDED, &base_vha->dpc_flags);
6535 qla2xxx_wake_dpc(base_vha);
6536 qla2x00_wait_for_hba_online(base_vha);
6539 EXPORT_SYMBOL(qlt_enable_vha);
6542 * qla_tgt_disable_vha - NO LOCK HELD
6544 * Disable Target Mode and reset the adapter
6546 static void qlt_disable_vha(struct scsi_qla_host *vha)
6548 struct qla_hw_data *ha = vha->hw;
6549 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
6550 unsigned long flags;
6553 ql_dbg(ql_dbg_tgt, vha, 0xe06a,
6554 "Unable to locate qla_tgt pointer from"
6555 " struct qla_hw_data\n");
6560 spin_lock_irqsave(&ha->hardware_lock, flags);
6561 qlt_clear_mode(vha);
6562 spin_unlock_irqrestore(&ha->hardware_lock, flags);
6564 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
6565 qla2xxx_wake_dpc(vha);
6566 qla2x00_wait_for_hba_online(vha);
6570 * Called from qla_init.c:qla24xx_vport_create() contex to setup
6571 * the target mode specific struct scsi_qla_host and struct qla_hw_data
6575 qlt_vport_create(struct scsi_qla_host *vha, struct qla_hw_data *ha)
6577 vha->vha_tgt.qla_tgt = NULL;
6579 mutex_init(&vha->vha_tgt.tgt_mutex);
6580 mutex_init(&vha->vha_tgt.tgt_host_action_mutex);
6582 qlt_clear_mode(vha);
6585 * NOTE: Currently the value is kept the same for <24xx and
6586 * >=24xx ISPs. If it is necessary to change it,
6587 * the check should be added for specific ISPs,
6588 * assigning the value appropriately.
6590 ha->tgt.atio_q_length = ATIO_ENTRY_CNT_24XX;
6592 qlt_add_target(ha, vha);
6596 qlt_rff_id(struct scsi_qla_host *vha)
6600 * FC-4 Feature bit 0 indicates target functionality to the name server.
6602 if (qla_tgt_mode_enabled(vha)) {
6603 fc4_feature = BIT_0;
6604 } else if (qla_ini_mode_enabled(vha)) {
6605 fc4_feature = BIT_1;
6606 } else if (qla_dual_mode_enabled(vha))
6607 fc4_feature = BIT_0 | BIT_1;
6613 * qlt_init_atio_q_entries() - Initializes ATIO queue entries.
6616 * Beginning of ATIO ring has initialization control block already built
6617 * by nvram config routine.
6619 * Returns 0 on success.
6622 qlt_init_atio_q_entries(struct scsi_qla_host *vha)
6624 struct qla_hw_data *ha = vha->hw;
6626 struct atio_from_isp *pkt = (struct atio_from_isp *)ha->tgt.atio_ring;
6628 if (qla_ini_mode_enabled(vha))
6631 for (cnt = 0; cnt < ha->tgt.atio_q_length; cnt++) {
6632 pkt->u.raw.signature = ATIO_PROCESSED;
6639 * qlt_24xx_process_atio_queue() - Process ATIO queue entries.
6640 * @ha: SCSI driver HA context
6643 qlt_24xx_process_atio_queue(struct scsi_qla_host *vha, uint8_t ha_locked)
6645 struct qla_hw_data *ha = vha->hw;
6646 struct atio_from_isp *pkt;
6649 if (!ha->flags.fw_started)
6652 while ((ha->tgt.atio_ring_ptr->signature != ATIO_PROCESSED) ||
6653 fcpcmd_is_corrupted(ha->tgt.atio_ring_ptr)) {
6654 pkt = (struct atio_from_isp *)ha->tgt.atio_ring_ptr;
6655 cnt = pkt->u.raw.entry_count;
6657 if (unlikely(fcpcmd_is_corrupted(ha->tgt.atio_ring_ptr))) {
6659 * This packet is corrupted. The header + payload
6660 * can not be trusted. There is no point in passing
6663 ql_log(ql_log_warn, vha, 0xd03c,
6664 "corrupted fcp frame SID[%3phN] OXID[%04x] EXCG[%x] %64phN\n",
6665 pkt->u.isp24.fcp_hdr.s_id,
6666 be16_to_cpu(pkt->u.isp24.fcp_hdr.ox_id),
6667 le32_to_cpu(pkt->u.isp24.exchange_addr), pkt);
6669 adjust_corrupted_atio(pkt);
6670 qlt_send_term_exchange(ha->base_qpair, NULL, pkt,
6673 qlt_24xx_atio_pkt_all_vps(vha,
6674 (struct atio_from_isp *)pkt, ha_locked);
6677 for (i = 0; i < cnt; i++) {
6678 ha->tgt.atio_ring_index++;
6679 if (ha->tgt.atio_ring_index == ha->tgt.atio_q_length) {
6680 ha->tgt.atio_ring_index = 0;
6681 ha->tgt.atio_ring_ptr = ha->tgt.atio_ring;
6683 ha->tgt.atio_ring_ptr++;
6685 pkt->u.raw.signature = ATIO_PROCESSED;
6686 pkt = (struct atio_from_isp *)ha->tgt.atio_ring_ptr;
6691 /* Adjust ring index */
6692 WRT_REG_DWORD(ISP_ATIO_Q_OUT(vha), ha->tgt.atio_ring_index);
6696 qlt_24xx_config_rings(struct scsi_qla_host *vha)
6698 struct qla_hw_data *ha = vha->hw;
6699 struct qla_msix_entry *msix = &ha->msix_entries[2];
6700 struct init_cb_24xx *icb = (struct init_cb_24xx *)ha->init_cb;
6702 if (!QLA_TGT_MODE_ENABLED())
6705 WRT_REG_DWORD(ISP_ATIO_Q_IN(vha), 0);
6706 WRT_REG_DWORD(ISP_ATIO_Q_OUT(vha), 0);
6707 RD_REG_DWORD(ISP_ATIO_Q_OUT(vha));
6709 if (ha->flags.msix_enabled) {
6710 if (IS_QLA83XX(ha) || IS_QLA27XX(ha)) {
6711 if (IS_QLA2071(ha)) {
6712 /* 4 ports Baker: Enable Interrupt Handshake */
6714 icb->firmware_options_2 |= BIT_26;
6716 icb->msix_atio = cpu_to_le16(msix->entry);
6717 icb->firmware_options_2 &= ~BIT_26;
6719 ql_dbg(ql_dbg_init, vha, 0xf072,
6720 "Registering ICB vector 0x%x for atio que.\n",
6725 if (IS_QLA83XX(ha) || IS_QLA27XX(ha)) {
6727 icb->firmware_options_2 |= BIT_26;
6728 ql_dbg(ql_dbg_init, vha, 0xf072,
6729 "%s: Use INTx for ATIOQ.\n", __func__);
6735 qlt_24xx_config_nvram_stage1(struct scsi_qla_host *vha, struct nvram_24xx *nv)
6737 struct qla_hw_data *ha = vha->hw;
6740 if (!QLA_TGT_MODE_ENABLED())
6743 if (qla_tgt_mode_enabled(vha) || qla_dual_mode_enabled(vha)) {
6744 if (!ha->tgt.saved_set) {
6745 /* We save only once */
6746 ha->tgt.saved_exchange_count = nv->exchange_count;
6747 ha->tgt.saved_firmware_options_1 =
6748 nv->firmware_options_1;
6749 ha->tgt.saved_firmware_options_2 =
6750 nv->firmware_options_2;
6751 ha->tgt.saved_firmware_options_3 =
6752 nv->firmware_options_3;
6753 ha->tgt.saved_set = 1;
6756 if (qla_tgt_mode_enabled(vha))
6757 nv->exchange_count = cpu_to_le16(0xFFFF);
6759 nv->exchange_count = cpu_to_le16(ql2xexchoffld);
6761 /* Enable target mode */
6762 nv->firmware_options_1 |= cpu_to_le32(BIT_4);
6764 /* Disable ini mode, if requested */
6765 if (qla_tgt_mode_enabled(vha))
6766 nv->firmware_options_1 |= cpu_to_le32(BIT_5);
6768 /* Disable Full Login after LIP */
6769 nv->firmware_options_1 &= cpu_to_le32(~BIT_13);
6770 /* Enable initial LIP */
6771 nv->firmware_options_1 &= cpu_to_le32(~BIT_9);
6772 if (ql2xtgt_tape_enable)
6773 /* Enable FC Tape support */
6774 nv->firmware_options_2 |= cpu_to_le32(BIT_12);
6776 /* Disable FC Tape support */
6777 nv->firmware_options_2 &= cpu_to_le32(~BIT_12);
6779 /* Disable Full Login after LIP */
6780 nv->host_p &= cpu_to_le32(~BIT_10);
6783 * clear BIT 15 explicitly as we have seen at least
6784 * a couple of instances where this was set and this
6785 * was causing the firmware to not be initialized.
6787 nv->firmware_options_1 &= cpu_to_le32(~BIT_15);
6788 /* Enable target PRLI control */
6789 nv->firmware_options_2 |= cpu_to_le32(BIT_14);
6791 if (IS_QLA25XX(ha)) {
6792 /* Change Loop-prefer to Pt-Pt */
6793 tmp = ~(BIT_4|BIT_5|BIT_6);
6794 nv->firmware_options_2 &= cpu_to_le32(tmp);
6796 nv->firmware_options_2 |= cpu_to_le32(tmp);
6799 if (ha->tgt.saved_set) {
6800 nv->exchange_count = ha->tgt.saved_exchange_count;
6801 nv->firmware_options_1 =
6802 ha->tgt.saved_firmware_options_1;
6803 nv->firmware_options_2 =
6804 ha->tgt.saved_firmware_options_2;
6805 nv->firmware_options_3 =
6806 ha->tgt.saved_firmware_options_3;
6811 if (ha->base_qpair->enable_class_2) {
6812 if (vha->flags.init_done)
6813 fc_host_supported_classes(vha->host) =
6814 FC_COS_CLASS2 | FC_COS_CLASS3;
6816 nv->firmware_options_2 |= cpu_to_le32(BIT_8);
6818 if (vha->flags.init_done)
6819 fc_host_supported_classes(vha->host) = FC_COS_CLASS3;
6821 nv->firmware_options_2 &= ~cpu_to_le32(BIT_8);
6826 qlt_24xx_config_nvram_stage2(struct scsi_qla_host *vha,
6827 struct init_cb_24xx *icb)
6829 struct qla_hw_data *ha = vha->hw;
6831 if (!QLA_TGT_MODE_ENABLED())
6834 if (ha->tgt.node_name_set) {
6835 memcpy(icb->node_name, ha->tgt.tgt_node_name, WWN_SIZE);
6836 icb->firmware_options_1 |= cpu_to_le32(BIT_14);
6839 /* disable ZIO at start time. */
6840 if (!vha->flags.init_done) {
6842 tmp = le32_to_cpu(icb->firmware_options_2);
6843 tmp &= ~(BIT_3 | BIT_2 | BIT_1 | BIT_0);
6844 icb->firmware_options_2 = cpu_to_le32(tmp);
6849 qlt_81xx_config_nvram_stage1(struct scsi_qla_host *vha, struct nvram_81xx *nv)
6851 struct qla_hw_data *ha = vha->hw;
6854 if (!QLA_TGT_MODE_ENABLED())
6857 if (qla_tgt_mode_enabled(vha) || qla_dual_mode_enabled(vha)) {
6858 if (!ha->tgt.saved_set) {
6859 /* We save only once */
6860 ha->tgt.saved_exchange_count = nv->exchange_count;
6861 ha->tgt.saved_firmware_options_1 =
6862 nv->firmware_options_1;
6863 ha->tgt.saved_firmware_options_2 =
6864 nv->firmware_options_2;
6865 ha->tgt.saved_firmware_options_3 =
6866 nv->firmware_options_3;
6867 ha->tgt.saved_set = 1;
6870 if (qla_tgt_mode_enabled(vha))
6871 nv->exchange_count = cpu_to_le16(0xFFFF);
6873 nv->exchange_count = cpu_to_le16(ql2xexchoffld);
6875 /* Enable target mode */
6876 nv->firmware_options_1 |= cpu_to_le32(BIT_4);
6878 /* Disable ini mode, if requested */
6879 if (qla_tgt_mode_enabled(vha))
6880 nv->firmware_options_1 |= cpu_to_le32(BIT_5);
6881 /* Disable Full Login after LIP */
6882 nv->firmware_options_1 &= cpu_to_le32(~BIT_13);
6883 /* Enable initial LIP */
6884 nv->firmware_options_1 &= cpu_to_le32(~BIT_9);
6886 * clear BIT 15 explicitly as we have seen at
6887 * least a couple of instances where this was set
6888 * and this was causing the firmware to not be
6891 nv->firmware_options_1 &= cpu_to_le32(~BIT_15);
6892 if (ql2xtgt_tape_enable)
6893 /* Enable FC tape support */
6894 nv->firmware_options_2 |= cpu_to_le32(BIT_12);
6896 /* Disable FC tape support */
6897 nv->firmware_options_2 &= cpu_to_le32(~BIT_12);
6899 /* Disable Full Login after LIP */
6900 nv->host_p &= cpu_to_le32(~BIT_10);
6901 /* Enable target PRLI control */
6902 nv->firmware_options_2 |= cpu_to_le32(BIT_14);
6904 /* Change Loop-prefer to Pt-Pt */
6905 tmp = ~(BIT_4|BIT_5|BIT_6);
6906 nv->firmware_options_2 &= cpu_to_le32(tmp);
6908 nv->firmware_options_2 |= cpu_to_le32(tmp);
6910 if (ha->tgt.saved_set) {
6911 nv->exchange_count = ha->tgt.saved_exchange_count;
6912 nv->firmware_options_1 =
6913 ha->tgt.saved_firmware_options_1;
6914 nv->firmware_options_2 =
6915 ha->tgt.saved_firmware_options_2;
6916 nv->firmware_options_3 =
6917 ha->tgt.saved_firmware_options_3;
6922 if (ha->base_qpair->enable_class_2) {
6923 if (vha->flags.init_done)
6924 fc_host_supported_classes(vha->host) =
6925 FC_COS_CLASS2 | FC_COS_CLASS3;
6927 nv->firmware_options_2 |= cpu_to_le32(BIT_8);
6929 if (vha->flags.init_done)
6930 fc_host_supported_classes(vha->host) = FC_COS_CLASS3;
6932 nv->firmware_options_2 &= ~cpu_to_le32(BIT_8);
6937 qlt_81xx_config_nvram_stage2(struct scsi_qla_host *vha,
6938 struct init_cb_81xx *icb)
6940 struct qla_hw_data *ha = vha->hw;
6942 if (!QLA_TGT_MODE_ENABLED())
6945 if (ha->tgt.node_name_set) {
6946 memcpy(icb->node_name, ha->tgt.tgt_node_name, WWN_SIZE);
6947 icb->firmware_options_1 |= cpu_to_le32(BIT_14);
6950 /* disable ZIO at start time. */
6951 if (!vha->flags.init_done) {
6953 tmp = le32_to_cpu(icb->firmware_options_2);
6954 tmp &= ~(BIT_3 | BIT_2 | BIT_1 | BIT_0);
6955 icb->firmware_options_2 = cpu_to_le32(tmp);
6961 qlt_83xx_iospace_config(struct qla_hw_data *ha)
6963 if (!QLA_TGT_MODE_ENABLED())
6966 ha->msix_count += 1; /* For ATIO Q */
6971 qlt_modify_vp_config(struct scsi_qla_host *vha,
6972 struct vp_config_entry_24xx *vpmod)
6974 /* enable target mode. Bit5 = 1 => disable */
6975 if (qla_tgt_mode_enabled(vha) || qla_dual_mode_enabled(vha))
6976 vpmod->options_idx1 &= ~BIT_5;
6978 /* Disable ini mode, if requested. bit4 = 1 => disable */
6979 if (qla_tgt_mode_enabled(vha))
6980 vpmod->options_idx1 &= ~BIT_4;
6984 qlt_probe_one_stage1(struct scsi_qla_host *base_vha, struct qla_hw_data *ha)
6988 if (!QLA_TGT_MODE_ENABLED())
6991 if ((ql2xenablemsix == 0) || IS_QLA83XX(ha) || IS_QLA27XX(ha)) {
6992 ISP_ATIO_Q_IN(base_vha) = &ha->mqiobase->isp25mq.atio_q_in;
6993 ISP_ATIO_Q_OUT(base_vha) = &ha->mqiobase->isp25mq.atio_q_out;
6995 ISP_ATIO_Q_IN(base_vha) = &ha->iobase->isp24.atio_q_in;
6996 ISP_ATIO_Q_OUT(base_vha) = &ha->iobase->isp24.atio_q_out;
6999 mutex_init(&base_vha->vha_tgt.tgt_mutex);
7000 mutex_init(&base_vha->vha_tgt.tgt_host_action_mutex);
7002 INIT_LIST_HEAD(&base_vha->unknown_atio_list);
7003 INIT_DELAYED_WORK(&base_vha->unknown_atio_work,
7004 qlt_unknown_atio_work_fn);
7006 qlt_clear_mode(base_vha);
7008 rc = btree_init32(&ha->tgt.host_map);
7010 ql_log(ql_log_info, base_vha, 0xd03d,
7011 "Unable to initialize ha->host_map btree\n");
7013 qlt_update_vp_map(base_vha, SET_VP_IDX);
7017 qla83xx_msix_atio_q(int irq, void *dev_id)
7019 struct rsp_que *rsp;
7020 scsi_qla_host_t *vha;
7021 struct qla_hw_data *ha;
7022 unsigned long flags;
7024 rsp = (struct rsp_que *) dev_id;
7026 vha = pci_get_drvdata(ha->pdev);
7028 spin_lock_irqsave(&ha->tgt.atio_lock, flags);
7030 qlt_24xx_process_atio_queue(vha, 0);
7032 spin_unlock_irqrestore(&ha->tgt.atio_lock, flags);
7038 qlt_handle_abts_recv_work(struct work_struct *work)
7040 struct qla_tgt_sess_op *op = container_of(work,
7041 struct qla_tgt_sess_op, work);
7042 scsi_qla_host_t *vha = op->vha;
7043 struct qla_hw_data *ha = vha->hw;
7044 unsigned long flags;
7046 if (qla2x00_reset_active(vha) ||
7047 (op->chip_reset != ha->base_qpair->chip_reset))
7050 spin_lock_irqsave(&ha->tgt.atio_lock, flags);
7051 qlt_24xx_process_atio_queue(vha, 0);
7052 spin_unlock_irqrestore(&ha->tgt.atio_lock, flags);
7054 spin_lock_irqsave(&ha->hardware_lock, flags);
7055 qlt_response_pkt_all_vps(vha, op->rsp, (response_t *)&op->atio);
7056 spin_unlock_irqrestore(&ha->hardware_lock, flags);
7062 qlt_handle_abts_recv(struct scsi_qla_host *vha, struct rsp_que *rsp,
7065 struct qla_tgt_sess_op *op;
7067 op = kzalloc(sizeof(*op), GFP_ATOMIC);
7070 /* do not reach for ATIO queue here. This is best effort err
7071 * recovery at this point.
7073 qlt_response_pkt_all_vps(vha, rsp, pkt);
7077 memcpy(&op->atio, pkt, sizeof(*pkt));
7079 op->chip_reset = vha->hw->base_qpair->chip_reset;
7081 INIT_WORK(&op->work, qlt_handle_abts_recv_work);
7082 queue_work(qla_tgt_wq, &op->work);
7087 qlt_mem_alloc(struct qla_hw_data *ha)
7089 if (!QLA_TGT_MODE_ENABLED())
7092 ha->tgt.tgt_vp_map = kcalloc(MAX_MULTI_ID_FABRIC,
7093 sizeof(struct qla_tgt_vp_map),
7095 if (!ha->tgt.tgt_vp_map)
7098 ha->tgt.atio_ring = dma_alloc_coherent(&ha->pdev->dev,
7099 (ha->tgt.atio_q_length + 1) * sizeof(struct atio_from_isp),
7100 &ha->tgt.atio_dma, GFP_KERNEL);
7101 if (!ha->tgt.atio_ring) {
7102 kfree(ha->tgt.tgt_vp_map);
7109 qlt_mem_free(struct qla_hw_data *ha)
7111 if (!QLA_TGT_MODE_ENABLED())
7114 if (ha->tgt.atio_ring) {
7115 dma_free_coherent(&ha->pdev->dev, (ha->tgt.atio_q_length + 1) *
7116 sizeof(struct atio_from_isp), ha->tgt.atio_ring,
7119 kfree(ha->tgt.tgt_vp_map);
7122 /* vport_slock to be held by the caller */
7124 qlt_update_vp_map(struct scsi_qla_host *vha, int cmd)
7130 if (!QLA_TGT_MODE_ENABLED())
7133 key = vha->d_id.b24;
7137 vha->hw->tgt.tgt_vp_map[vha->vp_idx].vha = vha;
7140 slot = btree_lookup32(&vha->hw->tgt.host_map, key);
7142 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf018,
7143 "Save vha in host_map %p %06x\n", vha, key);
7144 rc = btree_insert32(&vha->hw->tgt.host_map,
7145 key, vha, GFP_ATOMIC);
7147 ql_log(ql_log_info, vha, 0xd03e,
7148 "Unable to insert s_id into host_map: %06x\n",
7152 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf019,
7153 "replace existing vha in host_map %p %06x\n", vha, key);
7154 btree_update32(&vha->hw->tgt.host_map, key, vha);
7157 vha->hw->tgt.tgt_vp_map[vha->vp_idx].vha = NULL;
7160 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01a,
7161 "clear vha in host_map %p %06x\n", vha, key);
7162 slot = btree_lookup32(&vha->hw->tgt.host_map, key);
7164 btree_remove32(&vha->hw->tgt.host_map, key);
7170 void qlt_update_host_map(struct scsi_qla_host *vha, port_id_t id)
7173 if (!vha->d_id.b24) {
7175 qlt_update_vp_map(vha, SET_AL_PA);
7176 } else if (vha->d_id.b24 != id.b24) {
7177 qlt_update_vp_map(vha, RESET_AL_PA);
7179 qlt_update_vp_map(vha, SET_AL_PA);
7183 static int __init qlt_parse_ini_mode(void)
7185 if (strcasecmp(qlini_mode, QLA2XXX_INI_MODE_STR_EXCLUSIVE) == 0)
7186 ql2x_ini_mode = QLA2XXX_INI_MODE_EXCLUSIVE;
7187 else if (strcasecmp(qlini_mode, QLA2XXX_INI_MODE_STR_DISABLED) == 0)
7188 ql2x_ini_mode = QLA2XXX_INI_MODE_DISABLED;
7189 else if (strcasecmp(qlini_mode, QLA2XXX_INI_MODE_STR_ENABLED) == 0)
7190 ql2x_ini_mode = QLA2XXX_INI_MODE_ENABLED;
7191 else if (strcasecmp(qlini_mode, QLA2XXX_INI_MODE_STR_DUAL) == 0)
7192 ql2x_ini_mode = QLA2XXX_INI_MODE_DUAL;
7199 int __init qlt_init(void)
7203 if (!qlt_parse_ini_mode()) {
7204 ql_log(ql_log_fatal, NULL, 0xe06b,
7205 "qlt_parse_ini_mode() failed\n");
7209 if (!QLA_TGT_MODE_ENABLED())
7212 qla_tgt_mgmt_cmd_cachep = kmem_cache_create("qla_tgt_mgmt_cmd_cachep",
7213 sizeof(struct qla_tgt_mgmt_cmd), __alignof__(struct
7214 qla_tgt_mgmt_cmd), 0, NULL);
7215 if (!qla_tgt_mgmt_cmd_cachep) {
7216 ql_log(ql_log_fatal, NULL, 0xd04b,
7217 "kmem_cache_create for qla_tgt_mgmt_cmd_cachep failed\n");
7221 qla_tgt_plogi_cachep = kmem_cache_create("qla_tgt_plogi_cachep",
7222 sizeof(struct qlt_plogi_ack_t), __alignof__(struct qlt_plogi_ack_t),
7225 if (!qla_tgt_plogi_cachep) {
7226 ql_log(ql_log_fatal, NULL, 0xe06d,
7227 "kmem_cache_create for qla_tgt_plogi_cachep failed\n");
7229 goto out_mgmt_cmd_cachep;
7232 qla_tgt_mgmt_cmd_mempool = mempool_create(25, mempool_alloc_slab,
7233 mempool_free_slab, qla_tgt_mgmt_cmd_cachep);
7234 if (!qla_tgt_mgmt_cmd_mempool) {
7235 ql_log(ql_log_fatal, NULL, 0xe06e,
7236 "mempool_create for qla_tgt_mgmt_cmd_mempool failed\n");
7238 goto out_plogi_cachep;
7241 qla_tgt_wq = alloc_workqueue("qla_tgt_wq", 0, 0);
7243 ql_log(ql_log_fatal, NULL, 0xe06f,
7244 "alloc_workqueue for qla_tgt_wq failed\n");
7246 goto out_cmd_mempool;
7249 * Return 1 to signal that initiator-mode is being disabled
7251 return (ql2x_ini_mode == QLA2XXX_INI_MODE_DISABLED) ? 1 : 0;
7254 mempool_destroy(qla_tgt_mgmt_cmd_mempool);
7256 kmem_cache_destroy(qla_tgt_plogi_cachep);
7257 out_mgmt_cmd_cachep:
7258 kmem_cache_destroy(qla_tgt_mgmt_cmd_cachep);
7264 if (!QLA_TGT_MODE_ENABLED())
7267 destroy_workqueue(qla_tgt_wq);
7268 mempool_destroy(qla_tgt_mgmt_cmd_mempool);
7269 kmem_cache_destroy(qla_tgt_plogi_cachep);
7270 kmem_cache_destroy(qla_tgt_mgmt_cmd_cachep);