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 struct qla_hw_data *ha = sess->vha->hw;
1228 unsigned long flags;
1230 if (sess->disc_state == DSC_DELETE_PEND)
1233 if (sess->disc_state == DSC_DELETED) {
1234 if (tgt && tgt->tgt_stop && (tgt->sess_count == 0))
1235 wake_up_all(&tgt->waitQ);
1236 if (sess->vha->fcport_count == 0)
1237 wake_up_all(&sess->vha->fcport_waitQ);
1239 if (!sess->plogi_link[QLT_PLOGI_LINK_SAME_WWN] &&
1240 !sess->plogi_link[QLT_PLOGI_LINK_CONFLICT])
1244 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
1245 if (sess->deleted == QLA_SESS_DELETED)
1246 sess->logout_on_delete = 0;
1248 if (sess->deleted == QLA_SESS_DELETION_IN_PROGRESS) {
1249 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
1252 sess->deleted = QLA_SESS_DELETION_IN_PROGRESS;
1253 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
1255 sess->disc_state = DSC_DELETE_PEND;
1257 qla24xx_chk_fcp_state(sess);
1259 ql_dbg(ql_dbg_tgt, sess->vha, 0xe001,
1260 "Scheduling sess %p for deletion\n", sess);
1262 INIT_WORK(&sess->del_work, qla24xx_delete_sess_fn);
1263 WARN_ON(!queue_work(sess->vha->hw->wq, &sess->del_work));
1266 static void qlt_clear_tgt_db(struct qla_tgt *tgt)
1268 struct fc_port *sess;
1269 scsi_qla_host_t *vha = tgt->vha;
1271 list_for_each_entry(sess, &vha->vp_fcports, list) {
1273 qlt_schedule_sess_for_deletion(sess);
1276 /* At this point tgt could be already dead */
1279 static int qla24xx_get_loop_id(struct scsi_qla_host *vha, const uint8_t *s_id,
1282 struct qla_hw_data *ha = vha->hw;
1283 dma_addr_t gid_list_dma;
1284 struct gid_list_info *gid_list;
1289 gid_list = dma_alloc_coherent(&ha->pdev->dev, qla2x00_gid_list_size(ha),
1290 &gid_list_dma, GFP_KERNEL);
1292 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf044,
1293 "qla_target(%d): DMA Alloc failed of %u\n",
1294 vha->vp_idx, qla2x00_gid_list_size(ha));
1298 /* Get list of logged in devices */
1299 rc = qla24xx_gidlist_wait(vha, gid_list, gid_list_dma, &entries);
1300 if (rc != QLA_SUCCESS) {
1301 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf045,
1302 "qla_target(%d): get_id_list() failed: %x\n",
1305 goto out_free_id_list;
1308 id_iter = (char *)gid_list;
1310 for (i = 0; i < entries; i++) {
1311 struct gid_list_info *gid = (struct gid_list_info *)id_iter;
1312 if ((gid->al_pa == s_id[2]) &&
1313 (gid->area == s_id[1]) &&
1314 (gid->domain == s_id[0])) {
1315 *loop_id = le16_to_cpu(gid->loop_id);
1319 id_iter += ha->gid_list_info_size;
1323 dma_free_coherent(&ha->pdev->dev, qla2x00_gid_list_size(ha),
1324 gid_list, gid_list_dma);
1329 * Adds an extra ref to allow to drop hw lock after adding sess to the list.
1330 * Caller must put it.
1332 static struct fc_port *qlt_create_sess(
1333 struct scsi_qla_host *vha,
1337 struct qla_hw_data *ha = vha->hw;
1338 struct fc_port *sess = fcport;
1339 unsigned long flags;
1341 if (vha->vha_tgt.qla_tgt->tgt_stop)
1344 if (fcport->se_sess) {
1345 if (!kref_get_unless_zero(&sess->sess_kref)) {
1346 ql_dbg(ql_dbg_disc, vha, 0x20f6,
1347 "%s: kref_get_unless_zero failed for %8phC\n",
1348 __func__, sess->port_name);
1353 sess->tgt = vha->vha_tgt.qla_tgt;
1354 sess->local = local;
1357 * Under normal circumstances we want to logout from firmware when
1358 * session eventually ends and release corresponding nport handle.
1359 * In the exception cases (e.g. when new PLOGI is waiting) corresponding
1360 * code will adjust these flags as necessary.
1362 sess->logout_on_delete = 1;
1363 sess->keep_nport_handle = 0;
1364 sess->logout_completed = 0;
1366 if (ha->tgt.tgt_ops->check_initiator_node_acl(vha,
1367 &fcport->port_name[0], sess) < 0) {
1368 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf015,
1369 "(%d) %8phC check_initiator_node_acl failed\n",
1370 vha->vp_idx, fcport->port_name);
1373 kref_init(&fcport->sess_kref);
1375 * Take an extra reference to ->sess_kref here to handle
1376 * fc_port access across ->tgt.sess_lock reaquire.
1378 if (!kref_get_unless_zero(&sess->sess_kref)) {
1379 ql_dbg(ql_dbg_disc, vha, 0x20f7,
1380 "%s: kref_get_unless_zero failed for %8phC\n",
1381 __func__, sess->port_name);
1385 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
1386 if (!IS_SW_RESV_ADDR(sess->d_id))
1387 vha->vha_tgt.qla_tgt->sess_count++;
1389 qlt_do_generation_tick(vha, &sess->generation);
1390 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
1393 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf006,
1394 "Adding sess %p se_sess %p to tgt %p sess_count %d\n",
1395 sess, sess->se_sess, vha->vha_tgt.qla_tgt,
1396 vha->vha_tgt.qla_tgt->sess_count);
1398 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04b,
1399 "qla_target(%d): %ssession for wwn %8phC (loop_id %d, "
1400 "s_id %x:%x:%x, confirmed completion %ssupported) added\n",
1401 vha->vp_idx, local ? "local " : "", fcport->port_name,
1402 fcport->loop_id, sess->d_id.b.domain, sess->d_id.b.area,
1403 sess->d_id.b.al_pa, sess->conf_compl_supported ? "" : "not ");
1409 * max_gen - specifies maximum session generation
1410 * at which this deletion requestion is still valid
1413 qlt_fc_port_deleted(struct scsi_qla_host *vha, fc_port_t *fcport, int max_gen)
1415 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
1416 struct fc_port *sess = fcport;
1417 unsigned long flags;
1419 if (!vha->hw->tgt.tgt_ops)
1425 spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
1426 if (tgt->tgt_stop) {
1427 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
1430 if (!sess->se_sess) {
1431 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
1435 if (max_gen - sess->generation < 0) {
1436 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
1437 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf092,
1438 "Ignoring stale deletion request for se_sess %p / sess %p"
1439 " for port %8phC, req_gen %d, sess_gen %d\n",
1440 sess->se_sess, sess, sess->port_name, max_gen,
1445 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf008, "qla_tgt_fc_port_deleted %p", sess);
1448 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
1449 qlt_schedule_sess_for_deletion(sess);
1452 static inline int test_tgt_sess_count(struct qla_tgt *tgt)
1454 struct qla_hw_data *ha = tgt->ha;
1455 unsigned long flags;
1458 * We need to protect against race, when tgt is freed before or
1461 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
1462 ql_dbg(ql_dbg_tgt, tgt->vha, 0xe002,
1463 "tgt %p, sess_count=%d\n",
1464 tgt, tgt->sess_count);
1465 res = (tgt->sess_count == 0);
1466 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
1471 /* Called by tcm_qla2xxx configfs code */
1472 int qlt_stop_phase1(struct qla_tgt *tgt)
1474 struct scsi_qla_host *vha = tgt->vha;
1475 struct qla_hw_data *ha = tgt->ha;
1476 unsigned long flags;
1478 mutex_lock(&qla_tgt_mutex);
1479 if (!vha->fc_vport) {
1480 struct Scsi_Host *sh = vha->host;
1481 struct fc_host_attrs *fc_host = shost_to_fc_host(sh);
1484 spin_lock_irqsave(sh->host_lock, flags);
1485 npiv_vports = (fc_host->npiv_vports_inuse);
1486 spin_unlock_irqrestore(sh->host_lock, flags);
1489 mutex_unlock(&qla_tgt_mutex);
1490 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf021,
1491 "NPIV is in use. Can not stop target\n");
1495 if (tgt->tgt_stop || tgt->tgt_stopped) {
1496 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04e,
1497 "Already in tgt->tgt_stop or tgt_stopped state\n");
1498 mutex_unlock(&qla_tgt_mutex);
1502 ql_dbg(ql_dbg_tgt_mgt, vha, 0xe003, "Stopping target for host %ld(%p)\n",
1505 * Mutex needed to sync with qla_tgt_fc_port_[added,deleted].
1506 * Lock is needed, because we still can get an incoming packet.
1508 mutex_lock(&vha->vha_tgt.tgt_mutex);
1510 qlt_clear_tgt_db(tgt);
1511 mutex_unlock(&vha->vha_tgt.tgt_mutex);
1512 mutex_unlock(&qla_tgt_mutex);
1514 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf009,
1515 "Waiting for sess works (tgt %p)", tgt);
1516 spin_lock_irqsave(&tgt->sess_work_lock, flags);
1517 while (!list_empty(&tgt->sess_works_list)) {
1518 spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
1519 flush_scheduled_work();
1520 spin_lock_irqsave(&tgt->sess_work_lock, flags);
1522 spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
1524 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00a,
1525 "Waiting for tgt %p: sess_count=%d\n", tgt, tgt->sess_count);
1527 wait_event_timeout(tgt->waitQ, test_tgt_sess_count(tgt), 10*HZ);
1530 if (!ha->flags.host_shutting_down &&
1531 (qla_tgt_mode_enabled(vha) || qla_dual_mode_enabled(vha)))
1532 qlt_disable_vha(vha);
1534 /* Wait for sessions to clear out (just in case) */
1535 wait_event_timeout(tgt->waitQ, test_tgt_sess_count(tgt), 10*HZ);
1538 EXPORT_SYMBOL(qlt_stop_phase1);
1540 /* Called by tcm_qla2xxx configfs code */
1541 void qlt_stop_phase2(struct qla_tgt *tgt)
1543 scsi_qla_host_t *vha = tgt->vha;
1545 if (tgt->tgt_stopped) {
1546 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04f,
1547 "Already in tgt->tgt_stopped state\n");
1551 if (!tgt->tgt_stop) {
1552 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00b,
1553 "%s: phase1 stop is not completed\n", __func__);
1558 mutex_lock(&vha->vha_tgt.tgt_mutex);
1560 tgt->tgt_stopped = 1;
1561 mutex_unlock(&vha->vha_tgt.tgt_mutex);
1563 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00c, "Stop of tgt %p finished\n",
1566 EXPORT_SYMBOL(qlt_stop_phase2);
1568 /* Called from qlt_remove_target() -> qla2x00_remove_one() */
1569 static void qlt_release(struct qla_tgt *tgt)
1571 scsi_qla_host_t *vha = tgt->vha;
1575 struct qla_qpair_hint *h;
1576 struct qla_hw_data *ha = vha->hw;
1578 if ((vha->vha_tgt.qla_tgt != NULL) && !tgt->tgt_stop &&
1580 qlt_stop_phase1(tgt);
1582 if ((vha->vha_tgt.qla_tgt != NULL) && !tgt->tgt_stopped)
1583 qlt_stop_phase2(tgt);
1585 for (i = 0; i < vha->hw->max_qpairs + 1; i++) {
1586 unsigned long flags;
1588 h = &tgt->qphints[i];
1590 spin_lock_irqsave(h->qpair->qp_lock_ptr, flags);
1591 list_del(&h->hint_elem);
1592 spin_unlock_irqrestore(h->qpair->qp_lock_ptr, flags);
1596 kfree(tgt->qphints);
1597 mutex_lock(&qla_tgt_mutex);
1598 list_del(&vha->vha_tgt.qla_tgt->tgt_list_entry);
1599 mutex_unlock(&qla_tgt_mutex);
1601 btree_for_each_safe64(&tgt->lun_qpair_map, key, node)
1602 btree_remove64(&tgt->lun_qpair_map, key);
1604 btree_destroy64(&tgt->lun_qpair_map);
1607 if (ha->tgt.tgt_ops &&
1608 ha->tgt.tgt_ops->remove_target &&
1609 vha->vha_tgt.target_lport_ptr)
1610 ha->tgt.tgt_ops->remove_target(vha);
1612 vha->vha_tgt.qla_tgt = NULL;
1614 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00d,
1615 "Release of tgt %p finished\n", tgt);
1620 /* ha->hardware_lock supposed to be held on entry */
1621 static int qlt_sched_sess_work(struct qla_tgt *tgt, int type,
1622 const void *param, unsigned int param_size)
1624 struct qla_tgt_sess_work_param *prm;
1625 unsigned long flags;
1627 prm = kzalloc(sizeof(*prm), GFP_ATOMIC);
1629 ql_dbg(ql_dbg_tgt_mgt, tgt->vha, 0xf050,
1630 "qla_target(%d): Unable to create session "
1631 "work, command will be refused", 0);
1635 ql_dbg(ql_dbg_tgt_mgt, tgt->vha, 0xf00e,
1636 "Scheduling work (type %d, prm %p)"
1637 " to find session for param %p (size %d, tgt %p)\n",
1638 type, prm, param, param_size, tgt);
1641 memcpy(&prm->tm_iocb, param, param_size);
1643 spin_lock_irqsave(&tgt->sess_work_lock, flags);
1644 list_add_tail(&prm->sess_works_list_entry, &tgt->sess_works_list);
1645 spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
1647 schedule_work(&tgt->sess_work);
1653 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
1655 static void qlt_send_notify_ack(struct qla_qpair *qpair,
1656 struct imm_ntfy_from_isp *ntfy,
1657 uint32_t add_flags, uint16_t resp_code, int resp_code_valid,
1658 uint16_t srr_flags, uint16_t srr_reject_code, uint8_t srr_explan)
1660 struct scsi_qla_host *vha = qpair->vha;
1661 struct qla_hw_data *ha = vha->hw;
1663 struct nack_to_isp *nack;
1665 if (!ha->flags.fw_started)
1668 ql_dbg(ql_dbg_tgt, vha, 0xe004, "Sending NOTIFY_ACK (ha=%p)\n", ha);
1670 pkt = (request_t *)__qla2x00_alloc_iocbs(qpair, NULL);
1672 ql_dbg(ql_dbg_tgt, vha, 0xe049,
1673 "qla_target(%d): %s failed: unable to allocate "
1674 "request packet\n", vha->vp_idx, __func__);
1678 if (vha->vha_tgt.qla_tgt != NULL)
1679 vha->vha_tgt.qla_tgt->notify_ack_expected++;
1681 pkt->entry_type = NOTIFY_ACK_TYPE;
1682 pkt->entry_count = 1;
1684 nack = (struct nack_to_isp *)pkt;
1685 nack->ox_id = ntfy->ox_id;
1687 nack->u.isp24.handle = QLA_TGT_SKIP_HANDLE;
1688 nack->u.isp24.nport_handle = ntfy->u.isp24.nport_handle;
1689 if (le16_to_cpu(ntfy->u.isp24.status) == IMM_NTFY_ELS) {
1690 nack->u.isp24.flags = ntfy->u.isp24.flags &
1691 cpu_to_le32(NOTIFY24XX_FLAGS_PUREX_IOCB);
1693 nack->u.isp24.srr_rx_id = ntfy->u.isp24.srr_rx_id;
1694 nack->u.isp24.status = ntfy->u.isp24.status;
1695 nack->u.isp24.status_subcode = ntfy->u.isp24.status_subcode;
1696 nack->u.isp24.fw_handle = ntfy->u.isp24.fw_handle;
1697 nack->u.isp24.exchange_address = ntfy->u.isp24.exchange_address;
1698 nack->u.isp24.srr_rel_offs = ntfy->u.isp24.srr_rel_offs;
1699 nack->u.isp24.srr_ui = ntfy->u.isp24.srr_ui;
1700 nack->u.isp24.srr_flags = cpu_to_le16(srr_flags);
1701 nack->u.isp24.srr_reject_code = srr_reject_code;
1702 nack->u.isp24.srr_reject_code_expl = srr_explan;
1703 nack->u.isp24.vp_index = ntfy->u.isp24.vp_index;
1705 ql_dbg(ql_dbg_tgt, vha, 0xe005,
1706 "qla_target(%d): Sending 24xx Notify Ack %d\n",
1707 vha->vp_idx, nack->u.isp24.status);
1709 /* Memory Barrier */
1711 qla2x00_start_iocbs(vha, qpair->req);
1715 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
1717 static void qlt_24xx_send_abts_resp(struct qla_qpair *qpair,
1718 struct abts_recv_from_24xx *abts, uint32_t status,
1721 struct scsi_qla_host *vha = qpair->vha;
1722 struct qla_hw_data *ha = vha->hw;
1723 struct abts_resp_to_24xx *resp;
1727 ql_dbg(ql_dbg_tgt, vha, 0xe006,
1728 "Sending task mgmt ABTS response (ha=%p, atio=%p, status=%x\n",
1731 resp = (struct abts_resp_to_24xx *)qla2x00_alloc_iocbs_ready(qpair,
1734 ql_dbg(ql_dbg_tgt, vha, 0xe04a,
1735 "qla_target(%d): %s failed: unable to allocate "
1736 "request packet", vha->vp_idx, __func__);
1740 resp->entry_type = ABTS_RESP_24XX;
1741 resp->entry_count = 1;
1742 resp->nport_handle = abts->nport_handle;
1743 resp->vp_index = vha->vp_idx;
1744 resp->sof_type = abts->sof_type;
1745 resp->exchange_address = abts->exchange_address;
1746 resp->fcp_hdr_le = abts->fcp_hdr_le;
1747 f_ctl = cpu_to_le32(F_CTL_EXCH_CONTEXT_RESP |
1748 F_CTL_LAST_SEQ | F_CTL_END_SEQ |
1749 F_CTL_SEQ_INITIATIVE);
1750 p = (uint8_t *)&f_ctl;
1751 resp->fcp_hdr_le.f_ctl[0] = *p++;
1752 resp->fcp_hdr_le.f_ctl[1] = *p++;
1753 resp->fcp_hdr_le.f_ctl[2] = *p;
1755 resp->fcp_hdr_le.d_id[0] = abts->fcp_hdr_le.d_id[0];
1756 resp->fcp_hdr_le.d_id[1] = abts->fcp_hdr_le.d_id[1];
1757 resp->fcp_hdr_le.d_id[2] = abts->fcp_hdr_le.d_id[2];
1758 resp->fcp_hdr_le.s_id[0] = abts->fcp_hdr_le.s_id[0];
1759 resp->fcp_hdr_le.s_id[1] = abts->fcp_hdr_le.s_id[1];
1760 resp->fcp_hdr_le.s_id[2] = abts->fcp_hdr_le.s_id[2];
1762 resp->fcp_hdr_le.d_id[0] = abts->fcp_hdr_le.s_id[0];
1763 resp->fcp_hdr_le.d_id[1] = abts->fcp_hdr_le.s_id[1];
1764 resp->fcp_hdr_le.d_id[2] = abts->fcp_hdr_le.s_id[2];
1765 resp->fcp_hdr_le.s_id[0] = abts->fcp_hdr_le.d_id[0];
1766 resp->fcp_hdr_le.s_id[1] = abts->fcp_hdr_le.d_id[1];
1767 resp->fcp_hdr_le.s_id[2] = abts->fcp_hdr_le.d_id[2];
1769 resp->exchange_addr_to_abort = abts->exchange_addr_to_abort;
1770 if (status == FCP_TMF_CMPL) {
1771 resp->fcp_hdr_le.r_ctl = R_CTL_BASIC_LINK_SERV | R_CTL_B_ACC;
1772 resp->payload.ba_acct.seq_id_valid = SEQ_ID_INVALID;
1773 resp->payload.ba_acct.low_seq_cnt = 0x0000;
1774 resp->payload.ba_acct.high_seq_cnt = 0xFFFF;
1775 resp->payload.ba_acct.ox_id = abts->fcp_hdr_le.ox_id;
1776 resp->payload.ba_acct.rx_id = abts->fcp_hdr_le.rx_id;
1778 resp->fcp_hdr_le.r_ctl = R_CTL_BASIC_LINK_SERV | R_CTL_B_RJT;
1779 resp->payload.ba_rjt.reason_code =
1780 BA_RJT_REASON_CODE_UNABLE_TO_PERFORM;
1781 /* Other bytes are zero */
1784 vha->vha_tgt.qla_tgt->abts_resp_expected++;
1786 /* Memory Barrier */
1788 if (qpair->reqq_start_iocbs)
1789 qpair->reqq_start_iocbs(qpair);
1791 qla2x00_start_iocbs(vha, qpair->req);
1795 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
1797 static void qlt_24xx_retry_term_exchange(struct scsi_qla_host *vha,
1798 struct abts_resp_from_24xx_fw *entry)
1800 struct ctio7_to_24xx *ctio;
1802 ql_dbg(ql_dbg_tgt, vha, 0xe007,
1803 "Sending retry TERM EXCH CTIO7 (ha=%p)\n", vha->hw);
1805 ctio = (struct ctio7_to_24xx *)qla2x00_alloc_iocbs_ready(
1806 vha->hw->base_qpair, NULL);
1808 ql_dbg(ql_dbg_tgt, vha, 0xe04b,
1809 "qla_target(%d): %s failed: unable to allocate "
1810 "request packet\n", vha->vp_idx, __func__);
1815 * We've got on entrance firmware's response on by us generated
1816 * ABTS response. So, in it ID fields are reversed.
1819 ctio->entry_type = CTIO_TYPE7;
1820 ctio->entry_count = 1;
1821 ctio->nport_handle = entry->nport_handle;
1822 ctio->handle = QLA_TGT_SKIP_HANDLE | CTIO_COMPLETION_HANDLE_MARK;
1823 ctio->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
1824 ctio->vp_index = vha->vp_idx;
1825 ctio->initiator_id[0] = entry->fcp_hdr_le.d_id[0];
1826 ctio->initiator_id[1] = entry->fcp_hdr_le.d_id[1];
1827 ctio->initiator_id[2] = entry->fcp_hdr_le.d_id[2];
1828 ctio->exchange_addr = entry->exchange_addr_to_abort;
1829 ctio->u.status1.flags = cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_1 |
1830 CTIO7_FLAGS_TERMINATE);
1831 ctio->u.status1.ox_id = cpu_to_le16(entry->fcp_hdr_le.ox_id);
1833 /* Memory Barrier */
1835 qla2x00_start_iocbs(vha, vha->req);
1837 qlt_24xx_send_abts_resp(vha->hw->base_qpair,
1838 (struct abts_recv_from_24xx *)entry,
1839 FCP_TMF_CMPL, true);
1842 static int abort_cmd_for_tag(struct scsi_qla_host *vha, uint32_t tag)
1844 struct qla_tgt_sess_op *op;
1845 struct qla_tgt_cmd *cmd;
1846 unsigned long flags;
1848 spin_lock_irqsave(&vha->cmd_list_lock, flags);
1849 list_for_each_entry(op, &vha->qla_sess_op_cmd_list, cmd_list) {
1850 if (tag == op->atio.u.isp24.exchange_addr) {
1852 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
1857 list_for_each_entry(op, &vha->unknown_atio_list, cmd_list) {
1858 if (tag == op->atio.u.isp24.exchange_addr) {
1860 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
1865 list_for_each_entry(cmd, &vha->qla_cmd_list, cmd_list) {
1866 if (tag == cmd->atio.u.isp24.exchange_addr) {
1868 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
1872 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
1877 /* drop cmds for the given lun
1878 * XXX only looks for cmds on the port through which lun reset was recieved
1879 * XXX does not go through the list of other port (which may have cmds
1882 static void abort_cmds_for_lun(struct scsi_qla_host *vha,
1883 u64 lun, uint8_t *s_id)
1885 struct qla_tgt_sess_op *op;
1886 struct qla_tgt_cmd *cmd;
1888 unsigned long flags;
1890 key = sid_to_key(s_id);
1891 spin_lock_irqsave(&vha->cmd_list_lock, flags);
1892 list_for_each_entry(op, &vha->qla_sess_op_cmd_list, cmd_list) {
1896 op_key = sid_to_key(op->atio.u.isp24.fcp_hdr.s_id);
1897 op_lun = scsilun_to_int(
1898 (struct scsi_lun *)&op->atio.u.isp24.fcp_cmnd.lun);
1899 if (op_key == key && op_lun == lun)
1903 list_for_each_entry(op, &vha->unknown_atio_list, cmd_list) {
1907 op_key = sid_to_key(op->atio.u.isp24.fcp_hdr.s_id);
1908 op_lun = scsilun_to_int(
1909 (struct scsi_lun *)&op->atio.u.isp24.fcp_cmnd.lun);
1910 if (op_key == key && op_lun == lun)
1914 list_for_each_entry(cmd, &vha->qla_cmd_list, cmd_list) {
1918 cmd_key = sid_to_key(cmd->atio.u.isp24.fcp_hdr.s_id);
1919 cmd_lun = scsilun_to_int(
1920 (struct scsi_lun *)&cmd->atio.u.isp24.fcp_cmnd.lun);
1921 if (cmd_key == key && cmd_lun == lun)
1924 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
1927 static struct qla_qpair_hint *qlt_find_qphint(struct scsi_qla_host *vha,
1928 uint64_t unpacked_lun)
1930 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
1931 struct qla_qpair_hint *h = NULL;
1933 if (vha->flags.qpairs_available) {
1934 h = btree_lookup64(&tgt->lun_qpair_map, unpacked_lun);
1936 h = &tgt->qphints[0];
1938 h = &tgt->qphints[0];
1944 static void qlt_do_tmr_work(struct work_struct *work)
1946 struct qla_tgt_mgmt_cmd *mcmd =
1947 container_of(work, struct qla_tgt_mgmt_cmd, work);
1948 struct qla_hw_data *ha = mcmd->vha->hw;
1951 unsigned long flags;
1953 switch (mcmd->tmr_func) {
1955 tag = mcmd->orig_iocb.abts.exchange_addr_to_abort;
1962 rc = ha->tgt.tgt_ops->handle_tmr(mcmd, mcmd->unpacked_lun,
1963 mcmd->tmr_func, tag);
1966 spin_lock_irqsave(mcmd->qpair->qp_lock_ptr, flags);
1967 switch (mcmd->tmr_func) {
1969 qlt_24xx_send_abts_resp(mcmd->qpair,
1970 &mcmd->orig_iocb.abts,
1971 FCP_TMF_REJECTED, false);
1973 case QLA_TGT_LUN_RESET:
1974 case QLA_TGT_CLEAR_TS:
1975 case QLA_TGT_ABORT_TS:
1976 case QLA_TGT_CLEAR_ACA:
1977 case QLA_TGT_TARGET_RESET:
1978 qlt_send_busy(mcmd->qpair, &mcmd->orig_iocb.atio,
1982 case QLA_TGT_ABORT_ALL:
1983 case QLA_TGT_NEXUS_LOSS_SESS:
1984 case QLA_TGT_NEXUS_LOSS:
1985 qlt_send_notify_ack(mcmd->qpair,
1986 &mcmd->orig_iocb.imm_ntfy, 0, 0, 0, 0, 0, 0);
1989 spin_unlock_irqrestore(mcmd->qpair->qp_lock_ptr, flags);
1991 ql_dbg(ql_dbg_tgt_mgt, mcmd->vha, 0xf052,
1992 "qla_target(%d): tgt_ops->handle_tmr() failed: %d\n",
1993 mcmd->vha->vp_idx, rc);
1994 mempool_free(mcmd, qla_tgt_mgmt_cmd_mempool);
1998 /* ha->hardware_lock supposed to be held on entry */
1999 static int __qlt_24xx_handle_abts(struct scsi_qla_host *vha,
2000 struct abts_recv_from_24xx *abts, struct fc_port *sess)
2002 struct qla_hw_data *ha = vha->hw;
2003 struct qla_tgt_mgmt_cmd *mcmd;
2004 struct qla_qpair_hint *h = &vha->vha_tgt.qla_tgt->qphints[0];
2006 if (abort_cmd_for_tag(vha, abts->exchange_addr_to_abort)) {
2007 /* send TASK_ABORT response immediately */
2008 qlt_24xx_send_abts_resp(ha->base_qpair, abts, FCP_TMF_CMPL, false);
2012 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00f,
2013 "qla_target(%d): task abort (tag=%d)\n",
2014 vha->vp_idx, abts->exchange_addr_to_abort);
2016 mcmd = mempool_alloc(qla_tgt_mgmt_cmd_mempool, GFP_ATOMIC);
2018 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf051,
2019 "qla_target(%d): %s: Allocation of ABORT cmd failed",
2020 vha->vp_idx, __func__);
2023 memset(mcmd, 0, sizeof(*mcmd));
2026 memcpy(&mcmd->orig_iocb.abts, abts, sizeof(mcmd->orig_iocb.abts));
2027 mcmd->reset_count = ha->base_qpair->chip_reset;
2028 mcmd->tmr_func = QLA_TGT_ABTS;
2029 mcmd->qpair = h->qpair;
2033 * LUN is looked up by target-core internally based on the passed
2034 * abts->exchange_addr_to_abort tag.
2036 mcmd->se_cmd.cpuid = h->cpuid;
2038 if (ha->tgt.tgt_ops->find_cmd_by_tag) {
2039 struct qla_tgt_cmd *abort_cmd;
2041 abort_cmd = ha->tgt.tgt_ops->find_cmd_by_tag(sess,
2042 abts->exchange_addr_to_abort);
2043 if (abort_cmd && abort_cmd->qpair) {
2044 mcmd->qpair = abort_cmd->qpair;
2045 mcmd->se_cmd.cpuid = abort_cmd->se_cmd.cpuid;
2049 INIT_WORK(&mcmd->work, qlt_do_tmr_work);
2050 queue_work_on(mcmd->se_cmd.cpuid, qla_tgt_wq, &mcmd->work);
2056 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
2058 static void qlt_24xx_handle_abts(struct scsi_qla_host *vha,
2059 struct abts_recv_from_24xx *abts)
2061 struct qla_hw_data *ha = vha->hw;
2062 struct fc_port *sess;
2063 uint32_t tag = abts->exchange_addr_to_abort;
2066 unsigned long flags;
2068 if (le32_to_cpu(abts->fcp_hdr_le.parameter) & ABTS_PARAM_ABORT_SEQ) {
2069 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf053,
2070 "qla_target(%d): ABTS: Abort Sequence not "
2071 "supported\n", vha->vp_idx);
2072 qlt_24xx_send_abts_resp(ha->base_qpair, abts, FCP_TMF_REJECTED,
2077 if (tag == ATIO_EXCHANGE_ADDRESS_UNKNOWN) {
2078 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf010,
2079 "qla_target(%d): ABTS: Unknown Exchange "
2080 "Address received\n", vha->vp_idx);
2081 qlt_24xx_send_abts_resp(ha->base_qpair, abts, FCP_TMF_REJECTED,
2086 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf011,
2087 "qla_target(%d): task abort (s_id=%x:%x:%x, "
2088 "tag=%d, param=%x)\n", vha->vp_idx, abts->fcp_hdr_le.s_id[2],
2089 abts->fcp_hdr_le.s_id[1], abts->fcp_hdr_le.s_id[0], tag,
2090 le32_to_cpu(abts->fcp_hdr_le.parameter));
2092 s_id[0] = abts->fcp_hdr_le.s_id[2];
2093 s_id[1] = abts->fcp_hdr_le.s_id[1];
2094 s_id[2] = abts->fcp_hdr_le.s_id[0];
2096 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
2097 sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha, s_id);
2099 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf012,
2100 "qla_target(%d): task abort for non-existent session\n",
2102 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
2104 qlt_24xx_send_abts_resp(ha->base_qpair, abts, FCP_TMF_REJECTED,
2108 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
2111 if (sess->deleted) {
2112 qlt_24xx_send_abts_resp(ha->base_qpair, abts, FCP_TMF_REJECTED,
2117 rc = __qlt_24xx_handle_abts(vha, abts, sess);
2119 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf054,
2120 "qla_target(%d): __qlt_24xx_handle_abts() failed: %d\n",
2122 qlt_24xx_send_abts_resp(ha->base_qpair, abts, FCP_TMF_REJECTED,
2129 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
2131 static void qlt_24xx_send_task_mgmt_ctio(struct qla_qpair *qpair,
2132 struct qla_tgt_mgmt_cmd *mcmd, uint32_t resp_code)
2134 struct scsi_qla_host *ha = mcmd->vha;
2135 struct atio_from_isp *atio = &mcmd->orig_iocb.atio;
2136 struct ctio7_to_24xx *ctio;
2139 ql_dbg(ql_dbg_tgt, ha, 0xe008,
2140 "Sending task mgmt CTIO7 (ha=%p, atio=%p, resp_code=%x\n",
2141 ha, atio, resp_code);
2144 ctio = (struct ctio7_to_24xx *)__qla2x00_alloc_iocbs(qpair, NULL);
2146 ql_dbg(ql_dbg_tgt, ha, 0xe04c,
2147 "qla_target(%d): %s failed: unable to allocate "
2148 "request packet\n", ha->vp_idx, __func__);
2152 ctio->entry_type = CTIO_TYPE7;
2153 ctio->entry_count = 1;
2154 ctio->handle = QLA_TGT_SKIP_HANDLE | CTIO_COMPLETION_HANDLE_MARK;
2155 ctio->nport_handle = mcmd->sess->loop_id;
2156 ctio->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
2157 ctio->vp_index = ha->vp_idx;
2158 ctio->initiator_id[0] = atio->u.isp24.fcp_hdr.s_id[2];
2159 ctio->initiator_id[1] = atio->u.isp24.fcp_hdr.s_id[1];
2160 ctio->initiator_id[2] = atio->u.isp24.fcp_hdr.s_id[0];
2161 ctio->exchange_addr = atio->u.isp24.exchange_addr;
2162 temp = (atio->u.isp24.attr << 9)|
2163 CTIO7_FLAGS_STATUS_MODE_1 | CTIO7_FLAGS_SEND_STATUS;
2164 ctio->u.status1.flags = cpu_to_le16(temp);
2165 temp = be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id);
2166 ctio->u.status1.ox_id = cpu_to_le16(temp);
2167 ctio->u.status1.scsi_status =
2168 cpu_to_le16(SS_RESPONSE_INFO_LEN_VALID);
2169 ctio->u.status1.response_len = cpu_to_le16(8);
2170 ctio->u.status1.sense_data[0] = resp_code;
2172 /* Memory Barrier */
2174 if (qpair->reqq_start_iocbs)
2175 qpair->reqq_start_iocbs(qpair);
2177 qla2x00_start_iocbs(ha, qpair->req);
2180 void qlt_free_mcmd(struct qla_tgt_mgmt_cmd *mcmd)
2182 mempool_free(mcmd, qla_tgt_mgmt_cmd_mempool);
2184 EXPORT_SYMBOL(qlt_free_mcmd);
2187 * ha->hardware_lock supposed to be held on entry. Might drop it, then
2190 void qlt_send_resp_ctio(struct qla_qpair *qpair, struct qla_tgt_cmd *cmd,
2191 uint8_t scsi_status, uint8_t sense_key, uint8_t asc, uint8_t ascq)
2193 struct atio_from_isp *atio = &cmd->atio;
2194 struct ctio7_to_24xx *ctio;
2196 struct scsi_qla_host *vha = cmd->vha;
2198 ql_dbg(ql_dbg_tgt_dif, vha, 0x3066,
2199 "Sending response CTIO7 (vha=%p, atio=%p, scsi_status=%02x, "
2200 "sense_key=%02x, asc=%02x, ascq=%02x",
2201 vha, atio, scsi_status, sense_key, asc, ascq);
2203 ctio = (struct ctio7_to_24xx *)qla2x00_alloc_iocbs(vha, NULL);
2205 ql_dbg(ql_dbg_async, vha, 0x3067,
2206 "qla2x00t(%ld): %s failed: unable to allocate request packet",
2207 vha->host_no, __func__);
2211 ctio->entry_type = CTIO_TYPE7;
2212 ctio->entry_count = 1;
2213 ctio->handle = QLA_TGT_SKIP_HANDLE;
2214 ctio->nport_handle = cmd->sess->loop_id;
2215 ctio->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
2216 ctio->vp_index = vha->vp_idx;
2217 ctio->initiator_id[0] = atio->u.isp24.fcp_hdr.s_id[2];
2218 ctio->initiator_id[1] = atio->u.isp24.fcp_hdr.s_id[1];
2219 ctio->initiator_id[2] = atio->u.isp24.fcp_hdr.s_id[0];
2220 ctio->exchange_addr = atio->u.isp24.exchange_addr;
2221 temp = (atio->u.isp24.attr << 9) |
2222 CTIO7_FLAGS_STATUS_MODE_1 | CTIO7_FLAGS_SEND_STATUS;
2223 ctio->u.status1.flags = cpu_to_le16(temp);
2224 temp = be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id);
2225 ctio->u.status1.ox_id = cpu_to_le16(temp);
2226 ctio->u.status1.scsi_status =
2227 cpu_to_le16(SS_RESPONSE_INFO_LEN_VALID | scsi_status);
2228 ctio->u.status1.response_len = cpu_to_le16(18);
2229 ctio->u.status1.residual = cpu_to_le32(get_datalen_for_atio(atio));
2231 if (ctio->u.status1.residual != 0)
2232 ctio->u.status1.scsi_status |=
2233 cpu_to_le16(SS_RESIDUAL_UNDER);
2235 /* Response code and sense key */
2236 put_unaligned_le32(((0x70 << 24) | (sense_key << 8)),
2237 (&ctio->u.status1.sense_data)[0]);
2238 /* Additional sense length */
2239 put_unaligned_le32(0x0a, (&ctio->u.status1.sense_data)[1]);
2241 put_unaligned_le32(((asc << 24) | (ascq << 16)),
2242 (&ctio->u.status1.sense_data)[3]);
2244 /* Memory Barrier */
2247 if (qpair->reqq_start_iocbs)
2248 qpair->reqq_start_iocbs(qpair);
2250 qla2x00_start_iocbs(vha, qpair->req);
2256 /* callback from target fabric module code */
2257 void qlt_xmit_tm_rsp(struct qla_tgt_mgmt_cmd *mcmd)
2259 struct scsi_qla_host *vha = mcmd->sess->vha;
2260 struct qla_hw_data *ha = vha->hw;
2261 unsigned long flags;
2262 struct qla_qpair *qpair = mcmd->qpair;
2264 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf013,
2265 "TM response mcmd (%p) status %#x state %#x",
2266 mcmd, mcmd->fc_tm_rsp, mcmd->flags);
2268 spin_lock_irqsave(qpair->qp_lock_ptr, flags);
2270 if (!vha->flags.online || mcmd->reset_count != qpair->chip_reset) {
2272 * Either the port is not online or this request was from
2273 * previous life, just abort the processing.
2275 ql_dbg(ql_dbg_async, vha, 0xe100,
2276 "RESET-TMR online/active/old-count/new-count = %d/%d/%d/%d.\n",
2277 vha->flags.online, qla2x00_reset_active(vha),
2278 mcmd->reset_count, qpair->chip_reset);
2279 ha->tgt.tgt_ops->free_mcmd(mcmd);
2280 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
2284 if (mcmd->flags == QLA24XX_MGMT_SEND_NACK) {
2285 if (mcmd->orig_iocb.imm_ntfy.u.isp24.status_subcode ==
2287 mcmd->orig_iocb.imm_ntfy.u.isp24.status_subcode ==
2289 mcmd->orig_iocb.imm_ntfy.u.isp24.status_subcode ==
2291 ql_dbg(ql_dbg_disc, vha, 0x2106,
2292 "TM response logo %phC status %#x state %#x",
2293 mcmd->sess->port_name, mcmd->fc_tm_rsp,
2295 qlt_schedule_sess_for_deletion(mcmd->sess);
2297 qlt_send_notify_ack(vha->hw->base_qpair,
2298 &mcmd->orig_iocb.imm_ntfy, 0, 0, 0, 0, 0, 0);
2301 if (mcmd->orig_iocb.atio.u.raw.entry_type == ABTS_RECV_24XX)
2302 qlt_24xx_send_abts_resp(qpair, &mcmd->orig_iocb.abts,
2303 mcmd->fc_tm_rsp, false);
2305 qlt_24xx_send_task_mgmt_ctio(qpair, mcmd,
2309 * Make the callback for ->free_mcmd() to queue_work() and invoke
2310 * target_put_sess_cmd() to drop cmd_kref to 1. The final
2311 * target_put_sess_cmd() call will be made from TFO->check_stop_free()
2312 * -> tcm_qla2xxx_check_stop_free() to release the TMR associated se_cmd
2313 * descriptor after TFO->queue_tm_rsp() -> tcm_qla2xxx_queue_tm_rsp() ->
2314 * qlt_xmit_tm_rsp() returns here..
2316 ha->tgt.tgt_ops->free_mcmd(mcmd);
2317 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
2319 EXPORT_SYMBOL(qlt_xmit_tm_rsp);
2322 static int qlt_pci_map_calc_cnt(struct qla_tgt_prm *prm)
2324 struct qla_tgt_cmd *cmd = prm->cmd;
2326 BUG_ON(cmd->sg_cnt == 0);
2328 prm->sg = (struct scatterlist *)cmd->sg;
2329 prm->seg_cnt = pci_map_sg(cmd->qpair->pdev, cmd->sg,
2330 cmd->sg_cnt, cmd->dma_data_direction);
2331 if (unlikely(prm->seg_cnt == 0))
2334 prm->cmd->sg_mapped = 1;
2336 if (cmd->se_cmd.prot_op == TARGET_PROT_NORMAL) {
2338 * If greater than four sg entries then we need to allocate
2339 * the continuation entries
2341 if (prm->seg_cnt > QLA_TGT_DATASEGS_PER_CMD_24XX)
2342 prm->req_cnt += DIV_ROUND_UP(prm->seg_cnt -
2343 QLA_TGT_DATASEGS_PER_CMD_24XX,
2344 QLA_TGT_DATASEGS_PER_CONT_24XX);
2347 if ((cmd->se_cmd.prot_op == TARGET_PROT_DIN_INSERT) ||
2348 (cmd->se_cmd.prot_op == TARGET_PROT_DOUT_STRIP)) {
2349 prm->seg_cnt = DIV_ROUND_UP(cmd->bufflen, cmd->blk_sz);
2350 prm->tot_dsds = prm->seg_cnt;
2352 prm->tot_dsds = prm->seg_cnt;
2354 if (cmd->prot_sg_cnt) {
2355 prm->prot_sg = cmd->prot_sg;
2356 prm->prot_seg_cnt = pci_map_sg(cmd->qpair->pdev,
2357 cmd->prot_sg, cmd->prot_sg_cnt,
2358 cmd->dma_data_direction);
2359 if (unlikely(prm->prot_seg_cnt == 0))
2362 if ((cmd->se_cmd.prot_op == TARGET_PROT_DIN_INSERT) ||
2363 (cmd->se_cmd.prot_op == TARGET_PROT_DOUT_STRIP)) {
2364 /* Dif Bundling not support here */
2365 prm->prot_seg_cnt = DIV_ROUND_UP(cmd->bufflen,
2367 prm->tot_dsds += prm->prot_seg_cnt;
2369 prm->tot_dsds += prm->prot_seg_cnt;
2376 ql_dbg_qp(ql_dbg_tgt, prm->cmd->qpair, 0xe04d,
2377 "qla_target(%d): PCI mapping failed: sg_cnt=%d",
2378 0, prm->cmd->sg_cnt);
2382 static void qlt_unmap_sg(struct scsi_qla_host *vha, struct qla_tgt_cmd *cmd)
2384 struct qla_hw_data *ha;
2385 struct qla_qpair *qpair;
2386 if (!cmd->sg_mapped)
2391 pci_unmap_sg(qpair->pdev, cmd->sg, cmd->sg_cnt,
2392 cmd->dma_data_direction);
2395 if (cmd->prot_sg_cnt)
2396 pci_unmap_sg(qpair->pdev, cmd->prot_sg, cmd->prot_sg_cnt,
2397 cmd->dma_data_direction);
2402 if (cmd->ctx_dsd_alloced)
2403 qla2x00_clean_dsd_pool(ha, cmd->ctx);
2405 dma_pool_free(ha->dl_dma_pool, cmd->ctx, cmd->ctx->crc_ctx_dma);
2408 static int qlt_check_reserve_free_req(struct qla_qpair *qpair,
2412 struct req_que *req = qpair->req;
2414 if (req->cnt < (req_cnt + 2)) {
2415 cnt = (uint16_t)(qpair->use_shadow_reg ? *req->out_ptr :
2416 RD_REG_DWORD_RELAXED(req->req_q_out));
2418 if (req->ring_index < cnt)
2419 req->cnt = cnt - req->ring_index;
2421 req->cnt = req->length - (req->ring_index - cnt);
2423 if (unlikely(req->cnt < (req_cnt + 2)))
2427 req->cnt -= req_cnt;
2433 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
2435 static inline void *qlt_get_req_pkt(struct req_que *req)
2437 /* Adjust ring index. */
2439 if (req->ring_index == req->length) {
2440 req->ring_index = 0;
2441 req->ring_ptr = req->ring;
2445 return (cont_entry_t *)req->ring_ptr;
2448 /* ha->hardware_lock supposed to be held on entry */
2449 static inline uint32_t qlt_make_handle(struct qla_qpair *qpair)
2454 struct req_que *req = qpair->req;
2456 h = req->current_outstanding_cmd;
2458 for (index = 1; index < req->num_outstanding_cmds; index++) {
2460 if (h == req->num_outstanding_cmds)
2463 if (h == QLA_TGT_SKIP_HANDLE)
2466 if (!req->outstanding_cmds[h]) {
2473 req->current_outstanding_cmd = h;
2475 ql_dbg(ql_dbg_io, qpair->vha, 0x305b,
2476 "qla_target(%d): Ran out of empty cmd slots\n",
2477 qpair->vha->vp_idx);
2478 h = QLA_TGT_NULL_HANDLE;
2484 /* ha->hardware_lock supposed to be held on entry */
2485 static int qlt_24xx_build_ctio_pkt(struct qla_qpair *qpair,
2486 struct qla_tgt_prm *prm)
2489 struct ctio7_to_24xx *pkt;
2490 struct atio_from_isp *atio = &prm->cmd->atio;
2493 pkt = (struct ctio7_to_24xx *)qpair->req->ring_ptr;
2495 memset(pkt, 0, sizeof(*pkt));
2497 pkt->entry_type = CTIO_TYPE7;
2498 pkt->entry_count = (uint8_t)prm->req_cnt;
2499 pkt->vp_index = prm->cmd->vp_idx;
2501 h = qlt_make_handle(qpair);
2502 if (unlikely(h == QLA_TGT_NULL_HANDLE)) {
2504 * CTIO type 7 from the firmware doesn't provide a way to
2505 * know the initiator's LOOP ID, hence we can't find
2506 * the session and, so, the command.
2510 qpair->req->outstanding_cmds[h] = (srb_t *)prm->cmd;
2512 pkt->handle = MAKE_HANDLE(qpair->req->id, h);
2513 pkt->handle |= CTIO_COMPLETION_HANDLE_MARK;
2514 pkt->nport_handle = cpu_to_le16(prm->cmd->loop_id);
2515 pkt->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
2516 pkt->initiator_id[0] = atio->u.isp24.fcp_hdr.s_id[2];
2517 pkt->initiator_id[1] = atio->u.isp24.fcp_hdr.s_id[1];
2518 pkt->initiator_id[2] = atio->u.isp24.fcp_hdr.s_id[0];
2519 pkt->exchange_addr = atio->u.isp24.exchange_addr;
2520 temp = atio->u.isp24.attr << 9;
2521 pkt->u.status0.flags |= cpu_to_le16(temp);
2522 temp = be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id);
2523 pkt->u.status0.ox_id = cpu_to_le16(temp);
2524 pkt->u.status0.relative_offset = cpu_to_le32(prm->cmd->offset);
2530 * ha->hardware_lock supposed to be held on entry. We have already made sure
2531 * that there is sufficient amount of request entries to not drop it.
2533 static void qlt_load_cont_data_segments(struct qla_tgt_prm *prm)
2536 uint32_t *dword_ptr;
2538 /* Build continuation packets */
2539 while (prm->seg_cnt > 0) {
2540 cont_a64_entry_t *cont_pkt64 =
2541 (cont_a64_entry_t *)qlt_get_req_pkt(
2542 prm->cmd->qpair->req);
2545 * Make sure that from cont_pkt64 none of
2546 * 64-bit specific fields used for 32-bit
2547 * addressing. Cast to (cont_entry_t *) for
2551 memset(cont_pkt64, 0, sizeof(*cont_pkt64));
2553 cont_pkt64->entry_count = 1;
2554 cont_pkt64->sys_define = 0;
2556 cont_pkt64->entry_type = CONTINUE_A64_TYPE;
2557 dword_ptr = (uint32_t *)&cont_pkt64->dseg_0_address;
2559 /* Load continuation entry data segments */
2561 cnt < QLA_TGT_DATASEGS_PER_CONT_24XX && prm->seg_cnt;
2562 cnt++, prm->seg_cnt--) {
2564 cpu_to_le32(pci_dma_lo32
2565 (sg_dma_address(prm->sg)));
2566 *dword_ptr++ = cpu_to_le32(pci_dma_hi32
2567 (sg_dma_address(prm->sg)));
2568 *dword_ptr++ = cpu_to_le32(sg_dma_len(prm->sg));
2570 prm->sg = sg_next(prm->sg);
2576 * ha->hardware_lock supposed to be held on entry. We have already made sure
2577 * that there is sufficient amount of request entries to not drop it.
2579 static void qlt_load_data_segments(struct qla_tgt_prm *prm)
2582 uint32_t *dword_ptr;
2583 struct ctio7_to_24xx *pkt24 = (struct ctio7_to_24xx *)prm->pkt;
2585 pkt24->u.status0.transfer_length = cpu_to_le32(prm->cmd->bufflen);
2587 /* Setup packet address segment pointer */
2588 dword_ptr = pkt24->u.status0.dseg_0_address;
2590 /* Set total data segment count */
2592 pkt24->dseg_count = cpu_to_le16(prm->seg_cnt);
2594 if (prm->seg_cnt == 0) {
2595 /* No data transfer */
2601 /* If scatter gather */
2603 /* Load command entry data segments */
2605 (cnt < QLA_TGT_DATASEGS_PER_CMD_24XX) && prm->seg_cnt;
2606 cnt++, prm->seg_cnt--) {
2608 cpu_to_le32(pci_dma_lo32(sg_dma_address(prm->sg)));
2610 *dword_ptr++ = cpu_to_le32(pci_dma_hi32(
2611 sg_dma_address(prm->sg)));
2613 *dword_ptr++ = cpu_to_le32(sg_dma_len(prm->sg));
2615 prm->sg = sg_next(prm->sg);
2618 qlt_load_cont_data_segments(prm);
2621 static inline int qlt_has_data(struct qla_tgt_cmd *cmd)
2623 return cmd->bufflen > 0;
2626 static void qlt_print_dif_err(struct qla_tgt_prm *prm)
2628 struct qla_tgt_cmd *cmd;
2629 struct scsi_qla_host *vha;
2631 /* asc 0x10=dif error */
2632 if (prm->sense_buffer && (prm->sense_buffer[12] == 0x10)) {
2636 switch (prm->sense_buffer[13]) {
2638 ql_dbg(ql_dbg_tgt_dif, vha, 0xe00b,
2639 "BE detected Guard TAG ERR: lba[0x%llx|%lld] len[0x%x] "
2640 "se_cmd=%p tag[%x]",
2641 cmd->lba, cmd->lba, cmd->num_blks, &cmd->se_cmd,
2642 cmd->atio.u.isp24.exchange_addr);
2645 ql_dbg(ql_dbg_tgt_dif, vha, 0xe00c,
2646 "BE detected APP TAG ERR: lba[0x%llx|%lld] len[0x%x] "
2647 "se_cmd=%p tag[%x]",
2648 cmd->lba, cmd->lba, cmd->num_blks, &cmd->se_cmd,
2649 cmd->atio.u.isp24.exchange_addr);
2652 ql_dbg(ql_dbg_tgt_dif, vha, 0xe00f,
2653 "BE detected REF TAG ERR: lba[0x%llx|%lld] len[0x%x] "
2654 "se_cmd=%p tag[%x]",
2655 cmd->lba, cmd->lba, cmd->num_blks, &cmd->se_cmd,
2656 cmd->atio.u.isp24.exchange_addr);
2659 ql_dbg(ql_dbg_tgt_dif, vha, 0xe010,
2660 "BE detected Dif ERR: lba[%llx|%lld] len[%x] "
2661 "se_cmd=%p tag[%x]",
2662 cmd->lba, cmd->lba, cmd->num_blks, &cmd->se_cmd,
2663 cmd->atio.u.isp24.exchange_addr);
2666 ql_dump_buffer(ql_dbg_tgt_dif, vha, 0xe011, cmd->cdb, 16);
2671 * Called without ha->hardware_lock held
2673 static int qlt_pre_xmit_response(struct qla_tgt_cmd *cmd,
2674 struct qla_tgt_prm *prm, int xmit_type, uint8_t scsi_status,
2675 uint32_t *full_req_cnt)
2677 struct se_cmd *se_cmd = &cmd->se_cmd;
2678 struct qla_qpair *qpair = cmd->qpair;
2681 prm->tgt = cmd->tgt;
2683 prm->rq_result = scsi_status;
2684 prm->sense_buffer = &cmd->sense_buffer[0];
2685 prm->sense_buffer_len = TRANSPORT_SENSE_BUFFER;
2690 prm->add_status_pkt = 0;
2691 prm->prot_sg = NULL;
2692 prm->prot_seg_cnt = 0;
2695 if ((xmit_type & QLA_TGT_XMIT_DATA) && qlt_has_data(cmd)) {
2696 if (qlt_pci_map_calc_cnt(prm) != 0)
2700 *full_req_cnt = prm->req_cnt;
2702 if (se_cmd->se_cmd_flags & SCF_UNDERFLOW_BIT) {
2703 prm->residual = se_cmd->residual_count;
2704 ql_dbg_qp(ql_dbg_io + ql_dbg_verbose, qpair, 0x305c,
2705 "Residual underflow: %d (tag %lld, op %x, bufflen %d, rq_result %x)\n",
2706 prm->residual, se_cmd->tag,
2707 se_cmd->t_task_cdb ? se_cmd->t_task_cdb[0] : 0,
2708 cmd->bufflen, prm->rq_result);
2709 prm->rq_result |= SS_RESIDUAL_UNDER;
2710 } else if (se_cmd->se_cmd_flags & SCF_OVERFLOW_BIT) {
2711 prm->residual = se_cmd->residual_count;
2712 ql_dbg_qp(ql_dbg_io, qpair, 0x305d,
2713 "Residual overflow: %d (tag %lld, op %x, bufflen %d, rq_result %x)\n",
2714 prm->residual, se_cmd->tag, se_cmd->t_task_cdb ?
2715 se_cmd->t_task_cdb[0] : 0, cmd->bufflen, prm->rq_result);
2716 prm->rq_result |= SS_RESIDUAL_OVER;
2719 if (xmit_type & QLA_TGT_XMIT_STATUS) {
2721 * If QLA_TGT_XMIT_DATA is not set, add_status_pkt will be
2722 * ignored in *xmit_response() below
2724 if (qlt_has_data(cmd)) {
2725 if (QLA_TGT_SENSE_VALID(prm->sense_buffer) ||
2726 (IS_FWI2_CAPABLE(cmd->vha->hw) &&
2727 (prm->rq_result != 0))) {
2728 prm->add_status_pkt = 1;
2737 static inline int qlt_need_explicit_conf(struct qla_tgt_cmd *cmd,
2740 if (cmd->qpair->enable_class_2)
2744 return cmd->conf_compl_supported;
2746 return cmd->qpair->enable_explicit_conf &&
2747 cmd->conf_compl_supported;
2750 static void qlt_24xx_init_ctio_to_isp(struct ctio7_to_24xx *ctio,
2751 struct qla_tgt_prm *prm)
2753 prm->sense_buffer_len = min_t(uint32_t, prm->sense_buffer_len,
2754 (uint32_t)sizeof(ctio->u.status1.sense_data));
2755 ctio->u.status0.flags |= cpu_to_le16(CTIO7_FLAGS_SEND_STATUS);
2756 if (qlt_need_explicit_conf(prm->cmd, 0)) {
2757 ctio->u.status0.flags |= cpu_to_le16(
2758 CTIO7_FLAGS_EXPLICIT_CONFORM |
2759 CTIO7_FLAGS_CONFORM_REQ);
2761 ctio->u.status0.residual = cpu_to_le32(prm->residual);
2762 ctio->u.status0.scsi_status = cpu_to_le16(prm->rq_result);
2763 if (QLA_TGT_SENSE_VALID(prm->sense_buffer)) {
2766 if (qlt_need_explicit_conf(prm->cmd, 1)) {
2767 if ((prm->rq_result & SS_SCSI_STATUS_BYTE) != 0) {
2768 ql_dbg_qp(ql_dbg_tgt, prm->cmd->qpair, 0xe017,
2769 "Skipping EXPLICIT_CONFORM and "
2770 "CTIO7_FLAGS_CONFORM_REQ for FCP READ w/ "
2771 "non GOOD status\n");
2772 goto skip_explict_conf;
2774 ctio->u.status1.flags |= cpu_to_le16(
2775 CTIO7_FLAGS_EXPLICIT_CONFORM |
2776 CTIO7_FLAGS_CONFORM_REQ);
2779 ctio->u.status1.flags &=
2780 ~cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_0);
2781 ctio->u.status1.flags |=
2782 cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_1);
2783 ctio->u.status1.scsi_status |=
2784 cpu_to_le16(SS_SENSE_LEN_VALID);
2785 ctio->u.status1.sense_length =
2786 cpu_to_le16(prm->sense_buffer_len);
2787 for (i = 0; i < prm->sense_buffer_len/4; i++)
2788 ((uint32_t *)ctio->u.status1.sense_data)[i] =
2789 cpu_to_be32(((uint32_t *)prm->sense_buffer)[i]);
2791 qlt_print_dif_err(prm);
2794 ctio->u.status1.flags &=
2795 ~cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_0);
2796 ctio->u.status1.flags |=
2797 cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_1);
2798 ctio->u.status1.sense_length = 0;
2799 memset(ctio->u.status1.sense_data, 0,
2800 sizeof(ctio->u.status1.sense_data));
2803 /* Sense with len > 24, is it possible ??? */
2807 qlt_hba_err_chk_enabled(struct se_cmd *se_cmd)
2809 switch (se_cmd->prot_op) {
2810 case TARGET_PROT_DOUT_INSERT:
2811 case TARGET_PROT_DIN_STRIP:
2812 if (ql2xenablehba_err_chk >= 1)
2815 case TARGET_PROT_DOUT_PASS:
2816 case TARGET_PROT_DIN_PASS:
2817 if (ql2xenablehba_err_chk >= 2)
2820 case TARGET_PROT_DIN_INSERT:
2821 case TARGET_PROT_DOUT_STRIP:
2830 qla_tgt_ref_mask_check(struct se_cmd *se_cmd)
2832 switch (se_cmd->prot_op) {
2833 case TARGET_PROT_DIN_INSERT:
2834 case TARGET_PROT_DOUT_INSERT:
2835 case TARGET_PROT_DIN_STRIP:
2836 case TARGET_PROT_DOUT_STRIP:
2837 case TARGET_PROT_DIN_PASS:
2838 case TARGET_PROT_DOUT_PASS:
2847 * qla_tgt_set_dif_tags - Extract Ref and App tags from SCSI command
2850 qla_tgt_set_dif_tags(struct qla_tgt_cmd *cmd, struct crc_context *ctx,
2851 uint16_t *pfw_prot_opts)
2853 struct se_cmd *se_cmd = &cmd->se_cmd;
2854 uint32_t lba = 0xffffffff & se_cmd->t_task_lba;
2855 scsi_qla_host_t *vha = cmd->tgt->vha;
2856 struct qla_hw_data *ha = vha->hw;
2860 * wait till Mode Sense/Select cmd, modepage Ah, subpage 2
2861 * have been immplemented by TCM, before AppTag is avail.
2862 * Look for modesense_handlers[]
2865 ctx->app_tag_mask[0] = 0x0;
2866 ctx->app_tag_mask[1] = 0x0;
2868 if (IS_PI_UNINIT_CAPABLE(ha)) {
2869 if ((se_cmd->prot_type == TARGET_DIF_TYPE1_PROT) ||
2870 (se_cmd->prot_type == TARGET_DIF_TYPE2_PROT))
2871 *pfw_prot_opts |= PO_DIS_VALD_APP_ESC;
2872 else if (se_cmd->prot_type == TARGET_DIF_TYPE3_PROT)
2873 *pfw_prot_opts |= PO_DIS_VALD_APP_REF_ESC;
2876 t32 = ha->tgt.tgt_ops->get_dif_tags(cmd, pfw_prot_opts);
2878 switch (se_cmd->prot_type) {
2879 case TARGET_DIF_TYPE0_PROT:
2881 * No check for ql2xenablehba_err_chk, as it
2882 * would be an I/O error if hba tag generation
2885 ctx->ref_tag = cpu_to_le32(lba);
2886 /* enable ALL bytes of the ref tag */
2887 ctx->ref_tag_mask[0] = 0xff;
2888 ctx->ref_tag_mask[1] = 0xff;
2889 ctx->ref_tag_mask[2] = 0xff;
2890 ctx->ref_tag_mask[3] = 0xff;
2892 case TARGET_DIF_TYPE1_PROT:
2894 * For TYPE 1 protection: 16 bit GUARD tag, 32 bit
2895 * REF tag, and 16 bit app tag.
2897 ctx->ref_tag = cpu_to_le32(lba);
2898 if (!qla_tgt_ref_mask_check(se_cmd) ||
2899 !(ha->tgt.tgt_ops->chk_dif_tags(t32))) {
2900 *pfw_prot_opts |= PO_DIS_REF_TAG_VALD;
2903 /* enable ALL bytes of the ref tag */
2904 ctx->ref_tag_mask[0] = 0xff;
2905 ctx->ref_tag_mask[1] = 0xff;
2906 ctx->ref_tag_mask[2] = 0xff;
2907 ctx->ref_tag_mask[3] = 0xff;
2909 case TARGET_DIF_TYPE2_PROT:
2911 * For TYPE 2 protection: 16 bit GUARD + 32 bit REF
2912 * tag has to match LBA in CDB + N
2914 ctx->ref_tag = cpu_to_le32(lba);
2915 if (!qla_tgt_ref_mask_check(se_cmd) ||
2916 !(ha->tgt.tgt_ops->chk_dif_tags(t32))) {
2917 *pfw_prot_opts |= PO_DIS_REF_TAG_VALD;
2920 /* enable ALL bytes of the ref tag */
2921 ctx->ref_tag_mask[0] = 0xff;
2922 ctx->ref_tag_mask[1] = 0xff;
2923 ctx->ref_tag_mask[2] = 0xff;
2924 ctx->ref_tag_mask[3] = 0xff;
2926 case TARGET_DIF_TYPE3_PROT:
2927 /* For TYPE 3 protection: 16 bit GUARD only */
2928 *pfw_prot_opts |= PO_DIS_REF_TAG_VALD;
2929 ctx->ref_tag_mask[0] = ctx->ref_tag_mask[1] =
2930 ctx->ref_tag_mask[2] = ctx->ref_tag_mask[3] = 0x00;
2936 qlt_build_ctio_crc2_pkt(struct qla_qpair *qpair, struct qla_tgt_prm *prm)
2939 uint32_t transfer_length = 0;
2940 uint32_t data_bytes;
2942 uint8_t bundling = 1;
2943 struct crc_context *crc_ctx_pkt = NULL;
2944 struct qla_hw_data *ha;
2945 struct ctio_crc2_to_fw *pkt;
2946 dma_addr_t crc_ctx_dma;
2947 uint16_t fw_prot_opts = 0;
2948 struct qla_tgt_cmd *cmd = prm->cmd;
2949 struct se_cmd *se_cmd = &cmd->se_cmd;
2951 struct atio_from_isp *atio = &prm->cmd->atio;
2952 struct qla_tc_param tc;
2954 scsi_qla_host_t *vha = cmd->vha;
2958 pkt = (struct ctio_crc2_to_fw *)qpair->req->ring_ptr;
2960 memset(pkt, 0, sizeof(*pkt));
2962 ql_dbg_qp(ql_dbg_tgt, cmd->qpair, 0xe071,
2963 "qla_target(%d):%s: se_cmd[%p] CRC2 prot_op[0x%x] cmd prot sg:cnt[%p:%x] lba[%llu]\n",
2964 cmd->vp_idx, __func__, se_cmd, se_cmd->prot_op,
2965 prm->prot_sg, prm->prot_seg_cnt, se_cmd->t_task_lba);
2967 if ((se_cmd->prot_op == TARGET_PROT_DIN_INSERT) ||
2968 (se_cmd->prot_op == TARGET_PROT_DOUT_STRIP))
2971 /* Compute dif len and adjust data len to incude protection */
2972 data_bytes = cmd->bufflen;
2973 dif_bytes = (data_bytes / cmd->blk_sz) * 8;
2975 switch (se_cmd->prot_op) {
2976 case TARGET_PROT_DIN_INSERT:
2977 case TARGET_PROT_DOUT_STRIP:
2978 transfer_length = data_bytes;
2979 if (cmd->prot_sg_cnt)
2980 data_bytes += dif_bytes;
2982 case TARGET_PROT_DIN_STRIP:
2983 case TARGET_PROT_DOUT_INSERT:
2984 case TARGET_PROT_DIN_PASS:
2985 case TARGET_PROT_DOUT_PASS:
2986 transfer_length = data_bytes + dif_bytes;
2993 if (!qlt_hba_err_chk_enabled(se_cmd))
2994 fw_prot_opts |= 0x10; /* Disable Guard tag checking */
2995 /* HBA error checking enabled */
2996 else if (IS_PI_UNINIT_CAPABLE(ha)) {
2997 if ((se_cmd->prot_type == TARGET_DIF_TYPE1_PROT) ||
2998 (se_cmd->prot_type == TARGET_DIF_TYPE2_PROT))
2999 fw_prot_opts |= PO_DIS_VALD_APP_ESC;
3000 else if (se_cmd->prot_type == TARGET_DIF_TYPE3_PROT)
3001 fw_prot_opts |= PO_DIS_VALD_APP_REF_ESC;
3004 switch (se_cmd->prot_op) {
3005 case TARGET_PROT_DIN_INSERT:
3006 case TARGET_PROT_DOUT_INSERT:
3007 fw_prot_opts |= PO_MODE_DIF_INSERT;
3009 case TARGET_PROT_DIN_STRIP:
3010 case TARGET_PROT_DOUT_STRIP:
3011 fw_prot_opts |= PO_MODE_DIF_REMOVE;
3013 case TARGET_PROT_DIN_PASS:
3014 case TARGET_PROT_DOUT_PASS:
3015 fw_prot_opts |= PO_MODE_DIF_PASS;
3016 /* FUTURE: does tcm require T10CRC<->IPCKSUM conversion? */
3018 default:/* Normal Request */
3019 fw_prot_opts |= PO_MODE_DIF_PASS;
3024 /* Update entry type to indicate Command Type CRC_2 IOCB */
3025 pkt->entry_type = CTIO_CRC2;
3026 pkt->entry_count = 1;
3027 pkt->vp_index = cmd->vp_idx;
3029 h = qlt_make_handle(qpair);
3030 if (unlikely(h == QLA_TGT_NULL_HANDLE)) {
3032 * CTIO type 7 from the firmware doesn't provide a way to
3033 * know the initiator's LOOP ID, hence we can't find
3034 * the session and, so, the command.
3038 qpair->req->outstanding_cmds[h] = (srb_t *)prm->cmd;
3040 pkt->handle = MAKE_HANDLE(qpair->req->id, h);
3041 pkt->handle |= CTIO_COMPLETION_HANDLE_MARK;
3042 pkt->nport_handle = cpu_to_le16(prm->cmd->loop_id);
3043 pkt->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
3044 pkt->initiator_id[0] = atio->u.isp24.fcp_hdr.s_id[2];
3045 pkt->initiator_id[1] = atio->u.isp24.fcp_hdr.s_id[1];
3046 pkt->initiator_id[2] = atio->u.isp24.fcp_hdr.s_id[0];
3047 pkt->exchange_addr = atio->u.isp24.exchange_addr;
3049 /* silence compile warning */
3050 t16 = be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id);
3051 pkt->ox_id = cpu_to_le16(t16);
3053 t16 = (atio->u.isp24.attr << 9);
3054 pkt->flags |= cpu_to_le16(t16);
3055 pkt->relative_offset = cpu_to_le32(prm->cmd->offset);
3057 /* Set transfer direction */
3058 if (cmd->dma_data_direction == DMA_TO_DEVICE)
3059 pkt->flags = cpu_to_le16(CTIO7_FLAGS_DATA_IN);
3060 else if (cmd->dma_data_direction == DMA_FROM_DEVICE)
3061 pkt->flags = cpu_to_le16(CTIO7_FLAGS_DATA_OUT);
3063 pkt->dseg_count = prm->tot_dsds;
3064 /* Fibre channel byte count */
3065 pkt->transfer_length = cpu_to_le32(transfer_length);
3067 /* ----- CRC context -------- */
3069 /* Allocate CRC context from global pool */
3070 crc_ctx_pkt = cmd->ctx =
3071 dma_pool_zalloc(ha->dl_dma_pool, GFP_ATOMIC, &crc_ctx_dma);
3074 goto crc_queuing_error;
3076 crc_ctx_pkt->crc_ctx_dma = crc_ctx_dma;
3077 INIT_LIST_HEAD(&crc_ctx_pkt->dsd_list);
3080 crc_ctx_pkt->handle = pkt->handle;
3082 qla_tgt_set_dif_tags(cmd, crc_ctx_pkt, &fw_prot_opts);
3084 pkt->crc_context_address[0] = cpu_to_le32(LSD(crc_ctx_dma));
3085 pkt->crc_context_address[1] = cpu_to_le32(MSD(crc_ctx_dma));
3086 pkt->crc_context_len = CRC_CONTEXT_LEN_FW;
3089 cur_dsd = (uint32_t *) &crc_ctx_pkt->u.nobundling.data_address;
3092 * Configure Bundling if we need to fetch interlaving
3093 * protection PCI accesses
3095 fw_prot_opts |= PO_ENABLE_DIF_BUNDLING;
3096 crc_ctx_pkt->u.bundling.dif_byte_count = cpu_to_le32(dif_bytes);
3097 crc_ctx_pkt->u.bundling.dseg_count =
3098 cpu_to_le16(prm->tot_dsds - prm->prot_seg_cnt);
3099 cur_dsd = (uint32_t *) &crc_ctx_pkt->u.bundling.data_address;
3102 /* Finish the common fields of CRC pkt */
3103 crc_ctx_pkt->blk_size = cpu_to_le16(cmd->blk_sz);
3104 crc_ctx_pkt->prot_opts = cpu_to_le16(fw_prot_opts);
3105 crc_ctx_pkt->byte_count = cpu_to_le32(data_bytes);
3106 crc_ctx_pkt->guard_seed = cpu_to_le16(0);
3108 memset((uint8_t *)&tc, 0 , sizeof(tc));
3110 tc.blk_sz = cmd->blk_sz;
3111 tc.bufflen = cmd->bufflen;
3113 tc.prot_sg = cmd->prot_sg;
3114 tc.ctx = crc_ctx_pkt;
3115 tc.ctx_dsd_alloced = &cmd->ctx_dsd_alloced;
3117 /* Walks data segments */
3118 pkt->flags |= cpu_to_le16(CTIO7_FLAGS_DSD_PTR);
3120 if (!bundling && prm->prot_seg_cnt) {
3121 if (qla24xx_walk_and_build_sglist_no_difb(ha, NULL, cur_dsd,
3122 prm->tot_dsds, &tc))
3123 goto crc_queuing_error;
3124 } else if (qla24xx_walk_and_build_sglist(ha, NULL, cur_dsd,
3125 (prm->tot_dsds - prm->prot_seg_cnt), &tc))
3126 goto crc_queuing_error;
3128 if (bundling && prm->prot_seg_cnt) {
3129 /* Walks dif segments */
3130 pkt->add_flags |= CTIO_CRC2_AF_DIF_DSD_ENA;
3132 cur_dsd = (uint32_t *) &crc_ctx_pkt->u.bundling.dif_address;
3133 if (qla24xx_walk_and_build_prot_sglist(ha, NULL, cur_dsd,
3134 prm->prot_seg_cnt, &tc))
3135 goto crc_queuing_error;
3140 /* Cleanup will be performed by the caller */
3141 qpair->req->outstanding_cmds[h] = NULL;
3143 return QLA_FUNCTION_FAILED;
3147 * Callback to setup response of xmit_type of QLA_TGT_XMIT_DATA and *
3148 * QLA_TGT_XMIT_STATUS for >= 24xx silicon
3150 int qlt_xmit_response(struct qla_tgt_cmd *cmd, int xmit_type,
3151 uint8_t scsi_status)
3153 struct scsi_qla_host *vha = cmd->vha;
3154 struct qla_qpair *qpair = cmd->qpair;
3155 struct ctio7_to_24xx *pkt;
3156 struct qla_tgt_prm prm;
3157 uint32_t full_req_cnt = 0;
3158 unsigned long flags = 0;
3161 if (cmd->sess && cmd->sess->deleted) {
3162 cmd->state = QLA_TGT_STATE_PROCESSED;
3163 if (cmd->sess->logout_completed)
3164 /* no need to terminate. FW already freed exchange. */
3165 qlt_abort_cmd_on_host_reset(cmd->vha, cmd);
3167 qlt_send_term_exchange(qpair, cmd, &cmd->atio, 0, 0);
3171 ql_dbg_qp(ql_dbg_tgt, qpair, 0xe018,
3172 "is_send_status=%d, cmd->bufflen=%d, cmd->sg_cnt=%d, cmd->dma_data_direction=%d se_cmd[%p] qp %d\n",
3173 (xmit_type & QLA_TGT_XMIT_STATUS) ?
3174 1 : 0, cmd->bufflen, cmd->sg_cnt, cmd->dma_data_direction,
3175 &cmd->se_cmd, qpair->id);
3177 res = qlt_pre_xmit_response(cmd, &prm, xmit_type, scsi_status,
3179 if (unlikely(res != 0)) {
3183 spin_lock_irqsave(qpair->qp_lock_ptr, flags);
3185 if (xmit_type == QLA_TGT_XMIT_STATUS)
3186 qpair->tgt_counters.core_qla_snd_status++;
3188 qpair->tgt_counters.core_qla_que_buf++;
3190 if (!qpair->fw_started || cmd->reset_count != qpair->chip_reset) {
3192 * Either the port is not online or this request was from
3193 * previous life, just abort the processing.
3195 cmd->state = QLA_TGT_STATE_PROCESSED;
3196 qlt_abort_cmd_on_host_reset(cmd->vha, cmd);
3197 ql_dbg_qp(ql_dbg_async, qpair, 0xe101,
3198 "RESET-RSP online/active/old-count/new-count = %d/%d/%d/%d.\n",
3199 vha->flags.online, qla2x00_reset_active(vha),
3200 cmd->reset_count, qpair->chip_reset);
3201 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
3205 /* Does F/W have an IOCBs for this request */
3206 res = qlt_check_reserve_free_req(qpair, full_req_cnt);
3208 goto out_unmap_unlock;
3210 if (cmd->se_cmd.prot_op && (xmit_type & QLA_TGT_XMIT_DATA))
3211 res = qlt_build_ctio_crc2_pkt(qpair, &prm);
3213 res = qlt_24xx_build_ctio_pkt(qpair, &prm);
3214 if (unlikely(res != 0)) {
3215 qpair->req->cnt += full_req_cnt;
3216 goto out_unmap_unlock;
3219 pkt = (struct ctio7_to_24xx *)prm.pkt;
3221 if (qlt_has_data(cmd) && (xmit_type & QLA_TGT_XMIT_DATA)) {
3222 pkt->u.status0.flags |=
3223 cpu_to_le16(CTIO7_FLAGS_DATA_IN |
3224 CTIO7_FLAGS_STATUS_MODE_0);
3226 if (cmd->se_cmd.prot_op == TARGET_PROT_NORMAL)
3227 qlt_load_data_segments(&prm);
3229 if (prm.add_status_pkt == 0) {
3230 if (xmit_type & QLA_TGT_XMIT_STATUS) {
3231 pkt->u.status0.scsi_status =
3232 cpu_to_le16(prm.rq_result);
3233 pkt->u.status0.residual =
3234 cpu_to_le32(prm.residual);
3235 pkt->u.status0.flags |= cpu_to_le16(
3236 CTIO7_FLAGS_SEND_STATUS);
3237 if (qlt_need_explicit_conf(cmd, 0)) {
3238 pkt->u.status0.flags |=
3240 CTIO7_FLAGS_EXPLICIT_CONFORM |
3241 CTIO7_FLAGS_CONFORM_REQ);
3247 * We have already made sure that there is sufficient
3248 * amount of request entries to not drop HW lock in
3251 struct ctio7_to_24xx *ctio =
3252 (struct ctio7_to_24xx *)qlt_get_req_pkt(
3255 ql_dbg_qp(ql_dbg_tgt, qpair, 0x305e,
3256 "Building additional status packet 0x%p.\n",
3260 * T10Dif: ctio_crc2_to_fw overlay ontop of
3263 memcpy(ctio, pkt, sizeof(*ctio));
3264 /* reset back to CTIO7 */
3265 ctio->entry_count = 1;
3266 ctio->entry_type = CTIO_TYPE7;
3267 ctio->dseg_count = 0;
3268 ctio->u.status1.flags &= ~cpu_to_le16(
3269 CTIO7_FLAGS_DATA_IN);
3271 /* Real finish is ctio_m1's finish */
3272 pkt->handle |= CTIO_INTERMEDIATE_HANDLE_MARK;
3273 pkt->u.status0.flags |= cpu_to_le16(
3274 CTIO7_FLAGS_DONT_RET_CTIO);
3276 /* qlt_24xx_init_ctio_to_isp will correct
3277 * all neccessary fields that's part of CTIO7.
3278 * There should be no residual of CTIO-CRC2 data.
3280 qlt_24xx_init_ctio_to_isp((struct ctio7_to_24xx *)ctio,
3284 qlt_24xx_init_ctio_to_isp(pkt, &prm);
3287 cmd->state = QLA_TGT_STATE_PROCESSED; /* Mid-level is done processing */
3288 cmd->cmd_sent_to_fw = 1;
3290 /* Memory Barrier */
3292 if (qpair->reqq_start_iocbs)
3293 qpair->reqq_start_iocbs(qpair);
3295 qla2x00_start_iocbs(vha, qpair->req);
3296 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
3301 qlt_unmap_sg(vha, cmd);
3302 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
3306 EXPORT_SYMBOL(qlt_xmit_response);
3308 int qlt_rdy_to_xfer(struct qla_tgt_cmd *cmd)
3310 struct ctio7_to_24xx *pkt;
3311 struct scsi_qla_host *vha = cmd->vha;
3312 struct qla_tgt *tgt = cmd->tgt;
3313 struct qla_tgt_prm prm;
3314 unsigned long flags = 0;
3316 struct qla_qpair *qpair = cmd->qpair;
3318 memset(&prm, 0, sizeof(prm));
3324 /* Calculate number of entries and segments required */
3325 if (qlt_pci_map_calc_cnt(&prm) != 0)
3328 if (!qpair->fw_started || (cmd->reset_count != qpair->chip_reset) ||
3329 (cmd->sess && cmd->sess->deleted)) {
3331 * Either the port is not online or this request was from
3332 * previous life, just abort the processing.
3334 cmd->state = QLA_TGT_STATE_NEED_DATA;
3335 qlt_abort_cmd_on_host_reset(cmd->vha, cmd);
3336 ql_dbg_qp(ql_dbg_async, qpair, 0xe102,
3337 "RESET-XFR online/active/old-count/new-count = %d/%d/%d/%d.\n",
3338 vha->flags.online, qla2x00_reset_active(vha),
3339 cmd->reset_count, qpair->chip_reset);
3343 spin_lock_irqsave(qpair->qp_lock_ptr, flags);
3344 /* Does F/W have an IOCBs for this request */
3345 res = qlt_check_reserve_free_req(qpair, prm.req_cnt);
3347 goto out_unlock_free_unmap;
3348 if (cmd->se_cmd.prot_op)
3349 res = qlt_build_ctio_crc2_pkt(qpair, &prm);
3351 res = qlt_24xx_build_ctio_pkt(qpair, &prm);
3353 if (unlikely(res != 0)) {
3354 qpair->req->cnt += prm.req_cnt;
3355 goto out_unlock_free_unmap;
3358 pkt = (struct ctio7_to_24xx *)prm.pkt;
3359 pkt->u.status0.flags |= cpu_to_le16(CTIO7_FLAGS_DATA_OUT |
3360 CTIO7_FLAGS_STATUS_MODE_0);
3362 if (cmd->se_cmd.prot_op == TARGET_PROT_NORMAL)
3363 qlt_load_data_segments(&prm);
3365 cmd->state = QLA_TGT_STATE_NEED_DATA;
3366 cmd->cmd_sent_to_fw = 1;
3368 /* Memory Barrier */
3370 if (qpair->reqq_start_iocbs)
3371 qpair->reqq_start_iocbs(qpair);
3373 qla2x00_start_iocbs(vha, qpair->req);
3374 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
3378 out_unlock_free_unmap:
3379 qlt_unmap_sg(vha, cmd);
3380 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
3384 EXPORT_SYMBOL(qlt_rdy_to_xfer);
3388 * it is assumed either hardware_lock or qpair lock is held.
3391 qlt_handle_dif_error(struct qla_qpair *qpair, struct qla_tgt_cmd *cmd,
3392 struct ctio_crc_from_fw *sts)
3394 uint8_t *ap = &sts->actual_dif[0];
3395 uint8_t *ep = &sts->expected_dif[0];
3396 uint64_t lba = cmd->se_cmd.t_task_lba;
3397 uint8_t scsi_status, sense_key, asc, ascq;
3398 unsigned long flags;
3399 struct scsi_qla_host *vha = cmd->vha;
3401 cmd->trc_flags |= TRC_DIF_ERR;
3403 cmd->a_guard = be16_to_cpu(*(uint16_t *)(ap + 0));
3404 cmd->a_app_tag = be16_to_cpu(*(uint16_t *)(ap + 2));
3405 cmd->a_ref_tag = be32_to_cpu(*(uint32_t *)(ap + 4));
3407 cmd->e_guard = be16_to_cpu(*(uint16_t *)(ep + 0));
3408 cmd->e_app_tag = be16_to_cpu(*(uint16_t *)(ep + 2));
3409 cmd->e_ref_tag = be32_to_cpu(*(uint32_t *)(ep + 4));
3411 ql_dbg(ql_dbg_tgt_dif, vha, 0xf075,
3412 "%s: aborted %d state %d\n", __func__, cmd->aborted, cmd->state);
3414 scsi_status = sense_key = asc = ascq = 0;
3416 /* check appl tag */
3417 if (cmd->e_app_tag != cmd->a_app_tag) {
3418 ql_dbg(ql_dbg_tgt_dif, vha, 0xe00d,
3419 "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]",
3420 cmd->cdb[0], lba, (lba+cmd->num_blks), cmd->num_blks,
3421 cmd->a_ref_tag, cmd->e_ref_tag, cmd->a_app_tag,
3422 cmd->e_app_tag, cmd->a_guard, cmd->e_guard, cmd,
3423 cmd->atio.u.isp24.fcp_hdr.ox_id);
3425 cmd->dif_err_code = DIF_ERR_APP;
3426 scsi_status = SAM_STAT_CHECK_CONDITION;
3427 sense_key = ABORTED_COMMAND;
3433 if (cmd->e_ref_tag != cmd->a_ref_tag) {
3434 ql_dbg(ql_dbg_tgt_dif, vha, 0xe00e,
3435 "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] ",
3436 cmd->cdb[0], lba, (lba+cmd->num_blks), cmd->num_blks,
3437 cmd->a_ref_tag, cmd->e_ref_tag, cmd->a_app_tag,
3438 cmd->e_app_tag, cmd->a_guard, cmd->e_guard, cmd,
3439 cmd->atio.u.isp24.fcp_hdr.ox_id);
3441 cmd->dif_err_code = DIF_ERR_REF;
3442 scsi_status = SAM_STAT_CHECK_CONDITION;
3443 sense_key = ABORTED_COMMAND;
3450 if (cmd->e_guard != cmd->a_guard) {
3451 ql_dbg(ql_dbg_tgt_dif, vha, 0xe012,
3452 "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]",
3453 cmd->cdb[0], lba, (lba+cmd->num_blks), cmd->num_blks,
3454 cmd->a_ref_tag, cmd->e_ref_tag, cmd->a_app_tag,
3455 cmd->e_app_tag, cmd->a_guard, cmd->e_guard, cmd,
3456 cmd->atio.u.isp24.fcp_hdr.ox_id);
3458 cmd->dif_err_code = DIF_ERR_GRD;
3459 scsi_status = SAM_STAT_CHECK_CONDITION;
3460 sense_key = ABORTED_COMMAND;
3465 switch (cmd->state) {
3466 case QLA_TGT_STATE_NEED_DATA:
3467 /* handle_data will load DIF error code */
3468 cmd->state = QLA_TGT_STATE_DATA_IN;
3469 vha->hw->tgt.tgt_ops->handle_data(cmd);
3472 spin_lock_irqsave(&cmd->cmd_lock, flags);
3474 spin_unlock_irqrestore(&cmd->cmd_lock, flags);
3475 vha->hw->tgt.tgt_ops->free_cmd(cmd);
3478 spin_unlock_irqrestore(&cmd->cmd_lock, flags);
3480 qlt_send_resp_ctio(qpair, cmd, scsi_status, sense_key, asc,
3482 /* assume scsi status gets out on the wire.
3483 * Will not wait for completion.
3485 vha->hw->tgt.tgt_ops->free_cmd(cmd);
3490 /* If hardware_lock held on entry, might drop it, then reaquire */
3491 /* This function sends the appropriate CTIO to ISP 2xxx or 24xx */
3492 static int __qlt_send_term_imm_notif(struct scsi_qla_host *vha,
3493 struct imm_ntfy_from_isp *ntfy)
3495 struct nack_to_isp *nack;
3496 struct qla_hw_data *ha = vha->hw;
3500 ql_dbg(ql_dbg_tgt_tmr, vha, 0xe01c,
3501 "Sending TERM ELS CTIO (ha=%p)\n", ha);
3503 pkt = (request_t *)qla2x00_alloc_iocbs(vha, NULL);
3505 ql_dbg(ql_dbg_tgt, vha, 0xe080,
3506 "qla_target(%d): %s failed: unable to allocate "
3507 "request packet\n", vha->vp_idx, __func__);
3511 pkt->entry_type = NOTIFY_ACK_TYPE;
3512 pkt->entry_count = 1;
3513 pkt->handle = QLA_TGT_SKIP_HANDLE;
3515 nack = (struct nack_to_isp *)pkt;
3516 nack->ox_id = ntfy->ox_id;
3518 nack->u.isp24.nport_handle = ntfy->u.isp24.nport_handle;
3519 if (le16_to_cpu(ntfy->u.isp24.status) == IMM_NTFY_ELS) {
3520 nack->u.isp24.flags = ntfy->u.isp24.flags &
3521 __constant_cpu_to_le32(NOTIFY24XX_FLAGS_PUREX_IOCB);
3525 nack->u.isp24.flags |=
3526 __constant_cpu_to_le16(NOTIFY_ACK_FLAGS_TERMINATE);
3528 nack->u.isp24.srr_rx_id = ntfy->u.isp24.srr_rx_id;
3529 nack->u.isp24.status = ntfy->u.isp24.status;
3530 nack->u.isp24.status_subcode = ntfy->u.isp24.status_subcode;
3531 nack->u.isp24.fw_handle = ntfy->u.isp24.fw_handle;
3532 nack->u.isp24.exchange_address = ntfy->u.isp24.exchange_address;
3533 nack->u.isp24.srr_rel_offs = ntfy->u.isp24.srr_rel_offs;
3534 nack->u.isp24.srr_ui = ntfy->u.isp24.srr_ui;
3535 nack->u.isp24.vp_index = ntfy->u.isp24.vp_index;
3537 qla2x00_start_iocbs(vha, vha->req);
3541 static void qlt_send_term_imm_notif(struct scsi_qla_host *vha,
3542 struct imm_ntfy_from_isp *imm, int ha_locked)
3544 unsigned long flags = 0;
3548 rc = __qlt_send_term_imm_notif(vha, imm);
3552 qlt_alloc_qfull_cmd(vha, imm, 0, 0);
3560 spin_lock_irqsave(&vha->hw->hardware_lock, flags);
3561 rc = __qlt_send_term_imm_notif(vha, imm);
3565 qlt_alloc_qfull_cmd(vha, imm, 0, 0);
3570 spin_unlock_irqrestore(&vha->hw->hardware_lock, flags);
3574 * If hardware_lock held on entry, might drop it, then reaquire
3575 * This function sends the appropriate CTIO to ISP 2xxx or 24xx
3577 static int __qlt_send_term_exchange(struct qla_qpair *qpair,
3578 struct qla_tgt_cmd *cmd,
3579 struct atio_from_isp *atio)
3581 struct scsi_qla_host *vha = qpair->vha;
3582 struct ctio7_to_24xx *ctio24;
3583 struct qla_hw_data *ha = vha->hw;
3588 ql_dbg(ql_dbg_tgt, vha, 0xe009, "Sending TERM EXCH CTIO (ha=%p)\n", ha);
3593 pkt = (request_t *)qla2x00_alloc_iocbs_ready(qpair, NULL);
3595 ql_dbg(ql_dbg_tgt, vha, 0xe050,
3596 "qla_target(%d): %s failed: unable to allocate "
3597 "request packet\n", vha->vp_idx, __func__);
3602 if (cmd->state < QLA_TGT_STATE_PROCESSED) {
3603 ql_dbg(ql_dbg_tgt, vha, 0xe051,
3604 "qla_target(%d): Terminating cmd %p with "
3605 "incorrect state %d\n", vha->vp_idx, cmd,
3611 qpair->tgt_counters.num_term_xchg_sent++;
3612 pkt->entry_count = 1;
3613 pkt->handle = QLA_TGT_SKIP_HANDLE | CTIO_COMPLETION_HANDLE_MARK;
3615 ctio24 = (struct ctio7_to_24xx *)pkt;
3616 ctio24->entry_type = CTIO_TYPE7;
3617 ctio24->nport_handle = CTIO7_NHANDLE_UNRECOGNIZED;
3618 ctio24->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
3619 ctio24->vp_index = vha->vp_idx;
3620 ctio24->initiator_id[0] = atio->u.isp24.fcp_hdr.s_id[2];
3621 ctio24->initiator_id[1] = atio->u.isp24.fcp_hdr.s_id[1];
3622 ctio24->initiator_id[2] = atio->u.isp24.fcp_hdr.s_id[0];
3623 ctio24->exchange_addr = atio->u.isp24.exchange_addr;
3624 temp = (atio->u.isp24.attr << 9) | CTIO7_FLAGS_STATUS_MODE_1 |
3625 CTIO7_FLAGS_TERMINATE;
3626 ctio24->u.status1.flags = cpu_to_le16(temp);
3627 temp = be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id);
3628 ctio24->u.status1.ox_id = cpu_to_le16(temp);
3630 /* Memory Barrier */
3632 if (qpair->reqq_start_iocbs)
3633 qpair->reqq_start_iocbs(qpair);
3635 qla2x00_start_iocbs(vha, qpair->req);
3639 static void qlt_send_term_exchange(struct qla_qpair *qpair,
3640 struct qla_tgt_cmd *cmd, struct atio_from_isp *atio, int ha_locked,
3643 struct scsi_qla_host *vha;
3644 unsigned long flags = 0;
3647 /* why use different vha? NPIV */
3654 rc = __qlt_send_term_exchange(qpair, cmd, atio);
3656 qlt_alloc_qfull_cmd(vha, atio, 0, 0);
3659 spin_lock_irqsave(qpair->qp_lock_ptr, flags);
3660 rc = __qlt_send_term_exchange(qpair, cmd, atio);
3662 qlt_alloc_qfull_cmd(vha, atio, 0, 0);
3665 if (cmd && !ul_abort && !cmd->aborted) {
3667 qlt_unmap_sg(vha, cmd);
3668 vha->hw->tgt.tgt_ops->free_cmd(cmd);
3672 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
3677 static void qlt_init_term_exchange(struct scsi_qla_host *vha)
3679 struct list_head free_list;
3680 struct qla_tgt_cmd *cmd, *tcmd;
3682 vha->hw->tgt.leak_exchg_thresh_hold =
3683 (vha->hw->cur_fw_xcb_count/100) * LEAK_EXCHG_THRESH_HOLD_PERCENT;
3686 if (!list_empty(&vha->hw->tgt.q_full_list)) {
3687 INIT_LIST_HEAD(&free_list);
3688 list_splice_init(&vha->hw->tgt.q_full_list, &free_list);
3690 list_for_each_entry_safe(cmd, tcmd, &free_list, cmd_list) {
3691 list_del(&cmd->cmd_list);
3692 /* This cmd was never sent to TCM. There is no need
3693 * to schedule free or call free_cmd
3696 vha->hw->tgt.num_qfull_cmds_alloc--;
3699 vha->hw->tgt.num_qfull_cmds_dropped = 0;
3702 static void qlt_chk_exch_leak_thresh_hold(struct scsi_qla_host *vha)
3704 uint32_t total_leaked;
3706 total_leaked = vha->hw->tgt.num_qfull_cmds_dropped;
3708 if (vha->hw->tgt.leak_exchg_thresh_hold &&
3709 (total_leaked > vha->hw->tgt.leak_exchg_thresh_hold)) {
3711 ql_dbg(ql_dbg_tgt, vha, 0xe079,
3712 "Chip reset due to exchange starvation: %d/%d.\n",
3713 total_leaked, vha->hw->cur_fw_xcb_count);
3715 if (IS_P3P_TYPE(vha->hw))
3716 set_bit(FCOE_CTX_RESET_NEEDED, &vha->dpc_flags);
3718 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
3719 qla2xxx_wake_dpc(vha);
3724 int qlt_abort_cmd(struct qla_tgt_cmd *cmd)
3726 struct qla_tgt *tgt = cmd->tgt;
3727 struct scsi_qla_host *vha = tgt->vha;
3728 struct se_cmd *se_cmd = &cmd->se_cmd;
3729 unsigned long flags;
3731 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf014,
3732 "qla_target(%d): terminating exchange for aborted cmd=%p "
3733 "(se_cmd=%p, tag=%llu)", vha->vp_idx, cmd, &cmd->se_cmd,
3736 spin_lock_irqsave(&cmd->cmd_lock, flags);
3738 spin_unlock_irqrestore(&cmd->cmd_lock, flags);
3740 * It's normal to see 2 calls in this path:
3741 * 1) XFER Rdy completion + CMD_T_ABORT
3742 * 2) TCM TMR - drain_state_list
3744 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf016,
3745 "multiple abort. %p transport_state %x, t_state %x, "
3746 "se_cmd_flags %x\n", cmd, cmd->se_cmd.transport_state,
3747 cmd->se_cmd.t_state, cmd->se_cmd.se_cmd_flags);
3751 cmd->trc_flags |= TRC_ABORT;
3752 spin_unlock_irqrestore(&cmd->cmd_lock, flags);
3754 qlt_send_term_exchange(cmd->qpair, cmd, &cmd->atio, 0, 1);
3757 EXPORT_SYMBOL(qlt_abort_cmd);
3759 void qlt_free_cmd(struct qla_tgt_cmd *cmd)
3761 struct fc_port *sess = cmd->sess;
3763 ql_dbg(ql_dbg_tgt, cmd->vha, 0xe074,
3764 "%s: se_cmd[%p] ox_id %04x\n",
3765 __func__, &cmd->se_cmd,
3766 be16_to_cpu(cmd->atio.u.isp24.fcp_hdr.ox_id));
3768 BUG_ON(cmd->cmd_in_wq);
3771 qlt_unmap_sg(cmd->vha, cmd);
3774 qlt_decr_num_pend_cmds(cmd->vha);
3776 BUG_ON(cmd->sg_mapped);
3777 cmd->jiffies_at_free = get_jiffies_64();
3778 if (unlikely(cmd->free_sg))
3781 if (!sess || !sess->se_sess) {
3785 cmd->jiffies_at_free = get_jiffies_64();
3786 percpu_ida_free(&sess->se_sess->sess_tag_pool, cmd->se_cmd.map_tag);
3788 EXPORT_SYMBOL(qlt_free_cmd);
3791 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
3793 static int qlt_term_ctio_exchange(struct qla_qpair *qpair, void *ctio,
3794 struct qla_tgt_cmd *cmd, uint32_t status)
3797 struct scsi_qla_host *vha = qpair->vha;
3799 if (cmd->se_cmd.prot_op)
3800 ql_dbg(ql_dbg_tgt_dif, vha, 0xe013,
3801 "Term DIF cmd: lba[0x%llx|%lld] len[0x%x] "
3802 "se_cmd=%p tag[%x] op %#x/%s",
3804 cmd->num_blks, &cmd->se_cmd,
3805 cmd->atio.u.isp24.exchange_addr,
3806 cmd->se_cmd.prot_op,
3807 prot_op_str(cmd->se_cmd.prot_op));
3810 struct ctio7_from_24xx *c = (struct ctio7_from_24xx *)ctio;
3812 cpu_to_le16(OF_TERM_EXCH));
3817 qlt_send_term_exchange(qpair, cmd, &cmd->atio, 1, 0);
3823 /* ha->hardware_lock supposed to be held on entry */
3824 static struct qla_tgt_cmd *qlt_ctio_to_cmd(struct scsi_qla_host *vha,
3825 struct rsp_que *rsp, uint32_t handle, void *ctio)
3827 struct qla_tgt_cmd *cmd = NULL;
3828 struct req_que *req;
3829 int qid = GET_QID(handle);
3830 uint32_t h = handle & ~QLA_TGT_HANDLE_MASK;
3832 if (unlikely(h == QLA_TGT_SKIP_HANDLE))
3835 if (qid == rsp->req->id) {
3837 } else if (vha->hw->req_q_map[qid]) {
3838 ql_dbg(ql_dbg_tgt_mgt, vha, 0x1000a,
3839 "qla_target(%d): CTIO completion with different QID %d handle %x\n",
3840 vha->vp_idx, rsp->id, handle);
3841 req = vha->hw->req_q_map[qid];
3846 h &= QLA_CMD_HANDLE_MASK;
3848 if (h != QLA_TGT_NULL_HANDLE) {
3849 if (unlikely(h >= req->num_outstanding_cmds)) {
3850 ql_dbg(ql_dbg_tgt, vha, 0xe052,
3851 "qla_target(%d): Wrong handle %x received\n",
3852 vha->vp_idx, handle);
3856 cmd = (struct qla_tgt_cmd *)req->outstanding_cmds[h];
3857 if (unlikely(cmd == NULL)) {
3858 ql_dbg(ql_dbg_async, vha, 0xe053,
3859 "qla_target(%d): Suspicious: unable to find the command with handle %x req->id %d rsp->id %d\n",
3860 vha->vp_idx, handle, req->id, rsp->id);
3863 req->outstanding_cmds[h] = NULL;
3864 } else if (ctio != NULL) {
3865 /* We can't get loop ID from CTIO7 */
3866 ql_dbg(ql_dbg_tgt, vha, 0xe054,
3867 "qla_target(%d): Wrong CTIO received: QLA24xx doesn't "
3868 "support NULL handles\n", vha->vp_idx);
3875 /* hardware_lock should be held by caller. */
3877 qlt_abort_cmd_on_host_reset(struct scsi_qla_host *vha, struct qla_tgt_cmd *cmd)
3879 struct qla_hw_data *ha = vha->hw;
3882 qlt_unmap_sg(vha, cmd);
3884 /* TODO: fix debug message type and ids. */
3885 if (cmd->state == QLA_TGT_STATE_PROCESSED) {
3886 ql_dbg(ql_dbg_io, vha, 0xff00,
3887 "HOST-ABORT: state=PROCESSED.\n");
3888 } else if (cmd->state == QLA_TGT_STATE_NEED_DATA) {
3889 cmd->write_data_transferred = 0;
3890 cmd->state = QLA_TGT_STATE_DATA_IN;
3892 ql_dbg(ql_dbg_io, vha, 0xff01,
3893 "HOST-ABORT: state=DATA_IN.\n");
3895 ha->tgt.tgt_ops->handle_data(cmd);
3898 ql_dbg(ql_dbg_io, vha, 0xff03,
3899 "HOST-ABORT: state=BAD(%d).\n",
3904 cmd->trc_flags |= TRC_FLUSH;
3905 ha->tgt.tgt_ops->free_cmd(cmd);
3909 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
3911 static void qlt_do_ctio_completion(struct scsi_qla_host *vha,
3912 struct rsp_que *rsp, uint32_t handle, uint32_t status, void *ctio)
3914 struct qla_hw_data *ha = vha->hw;
3915 struct se_cmd *se_cmd;
3916 struct qla_tgt_cmd *cmd;
3917 struct qla_qpair *qpair = rsp->qpair;
3919 if (handle & CTIO_INTERMEDIATE_HANDLE_MARK) {
3920 /* That could happen only in case of an error/reset/abort */
3921 if (status != CTIO_SUCCESS) {
3922 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01d,
3923 "Intermediate CTIO received"
3924 " (status %x)\n", status);
3929 cmd = qlt_ctio_to_cmd(vha, rsp, handle, ctio);
3933 se_cmd = &cmd->se_cmd;
3934 cmd->cmd_sent_to_fw = 0;
3936 qlt_unmap_sg(vha, cmd);
3938 if (unlikely(status != CTIO_SUCCESS)) {
3939 switch (status & 0xFFFF) {
3940 case CTIO_LIP_RESET:
3941 case CTIO_TARGET_RESET:
3943 /* driver request abort via Terminate exchange */
3945 case CTIO_INVALID_RX_ID:
3947 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf058,
3948 "qla_target(%d): CTIO with "
3949 "status %#x received, state %x, se_cmd %p, "
3950 "(LIP_RESET=e, ABORTED=2, TARGET_RESET=17, "
3951 "TIMEOUT=b, INVALID_RX_ID=8)\n", vha->vp_idx,
3952 status, cmd->state, se_cmd);
3955 case CTIO_PORT_LOGGED_OUT:
3956 case CTIO_PORT_UNAVAILABLE:
3959 (status & 0xFFFF) == CTIO_PORT_LOGGED_OUT;
3961 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf059,
3962 "qla_target(%d): CTIO with %s status %x "
3963 "received (state %x, se_cmd %p)\n", vha->vp_idx,
3964 logged_out ? "PORT LOGGED OUT" : "PORT UNAVAILABLE",
3965 status, cmd->state, se_cmd);
3967 if (logged_out && cmd->sess) {
3969 * Session is already logged out, but we need
3970 * to notify initiator, who's not aware of this
3972 cmd->sess->logout_on_delete = 0;
3973 cmd->sess->send_els_logo = 1;
3974 ql_dbg(ql_dbg_disc, vha, 0x20f8,
3975 "%s %d %8phC post del sess\n",
3976 __func__, __LINE__, cmd->sess->port_name);
3978 qlt_schedule_sess_for_deletion(cmd->sess);
3982 case CTIO_DIF_ERROR: {
3983 struct ctio_crc_from_fw *crc =
3984 (struct ctio_crc_from_fw *)ctio;
3985 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf073,
3986 "qla_target(%d): CTIO with DIF_ERROR status %x "
3987 "received (state %x, ulp_cmd %p) actual_dif[0x%llx] "
3988 "expect_dif[0x%llx]\n",
3989 vha->vp_idx, status, cmd->state, se_cmd,
3990 *((u64 *)&crc->actual_dif[0]),
3991 *((u64 *)&crc->expected_dif[0]));
3993 qlt_handle_dif_error(qpair, cmd, ctio);
3997 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05b,
3998 "qla_target(%d): CTIO with error status 0x%x received (state %x, se_cmd %p\n",
3999 vha->vp_idx, status, cmd->state, se_cmd);
4004 /* "cmd->aborted" means
4005 * cmd is already aborted/terminated, we don't
4006 * need to terminate again. The exchange is already
4007 * cleaned up/freed at FW level. Just cleanup at driver
4010 if ((cmd->state != QLA_TGT_STATE_NEED_DATA) &&
4012 cmd->trc_flags |= TRC_CTIO_ERR;
4013 if (qlt_term_ctio_exchange(qpair, ctio, cmd, status))
4018 if (cmd->state == QLA_TGT_STATE_PROCESSED) {
4019 cmd->trc_flags |= TRC_CTIO_DONE;
4020 } else if (cmd->state == QLA_TGT_STATE_NEED_DATA) {
4021 cmd->state = QLA_TGT_STATE_DATA_IN;
4023 if (status == CTIO_SUCCESS)
4024 cmd->write_data_transferred = 1;
4026 ha->tgt.tgt_ops->handle_data(cmd);
4028 } else if (cmd->aborted) {
4029 cmd->trc_flags |= TRC_CTIO_ABORTED;
4030 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01e,
4031 "Aborted command %p (tag %lld) finished\n", cmd, se_cmd->tag);
4033 cmd->trc_flags |= TRC_CTIO_STRANGE;
4034 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05c,
4035 "qla_target(%d): A command in state (%d) should "
4036 "not return a CTIO complete\n", vha->vp_idx, cmd->state);
4039 if (unlikely(status != CTIO_SUCCESS) &&
4041 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01f, "Finishing failed CTIO\n");
4045 ha->tgt.tgt_ops->free_cmd(cmd);
4048 static inline int qlt_get_fcp_task_attr(struct scsi_qla_host *vha,
4053 switch (task_codes) {
4054 case ATIO_SIMPLE_QUEUE:
4055 fcp_task_attr = TCM_SIMPLE_TAG;
4057 case ATIO_HEAD_OF_QUEUE:
4058 fcp_task_attr = TCM_HEAD_TAG;
4060 case ATIO_ORDERED_QUEUE:
4061 fcp_task_attr = TCM_ORDERED_TAG;
4063 case ATIO_ACA_QUEUE:
4064 fcp_task_attr = TCM_ACA_TAG;
4067 fcp_task_attr = TCM_SIMPLE_TAG;
4070 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05d,
4071 "qla_target: unknown task code %x, use ORDERED instead\n",
4073 fcp_task_attr = TCM_ORDERED_TAG;
4077 return fcp_task_attr;
4080 static struct fc_port *qlt_make_local_sess(struct scsi_qla_host *,
4083 * Process context for I/O path into tcm_qla2xxx code
4085 static void __qlt_do_work(struct qla_tgt_cmd *cmd)
4087 scsi_qla_host_t *vha = cmd->vha;
4088 struct qla_hw_data *ha = vha->hw;
4089 struct fc_port *sess = cmd->sess;
4090 struct atio_from_isp *atio = &cmd->atio;
4092 unsigned long flags;
4093 uint32_t data_length;
4094 int ret, fcp_task_attr, data_dir, bidi = 0;
4095 struct qla_qpair *qpair = cmd->qpair;
4098 cmd->trc_flags |= TRC_DO_WORK;
4101 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf082,
4102 "cmd with tag %u is aborted\n",
4103 cmd->atio.u.isp24.exchange_addr);
4107 spin_lock_init(&cmd->cmd_lock);
4108 cdb = &atio->u.isp24.fcp_cmnd.cdb[0];
4109 cmd->se_cmd.tag = atio->u.isp24.exchange_addr;
4111 if (atio->u.isp24.fcp_cmnd.rddata &&
4112 atio->u.isp24.fcp_cmnd.wrdata) {
4114 data_dir = DMA_TO_DEVICE;
4115 } else if (atio->u.isp24.fcp_cmnd.rddata)
4116 data_dir = DMA_FROM_DEVICE;
4117 else if (atio->u.isp24.fcp_cmnd.wrdata)
4118 data_dir = DMA_TO_DEVICE;
4120 data_dir = DMA_NONE;
4122 fcp_task_attr = qlt_get_fcp_task_attr(vha,
4123 atio->u.isp24.fcp_cmnd.task_attr);
4124 data_length = get_datalen_for_atio(atio);
4126 ret = ha->tgt.tgt_ops->handle_cmd(vha, cmd, cdb, data_length,
4127 fcp_task_attr, data_dir, bidi);
4131 * Drop extra session reference from qla_tgt_handle_cmd_for_atio*(
4133 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
4134 ha->tgt.tgt_ops->put_sess(sess);
4135 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
4139 ql_dbg(ql_dbg_io, vha, 0x3060, "Terminating work cmd %p", cmd);
4141 * cmd has not sent to target yet, so pass NULL as the second
4142 * argument to qlt_send_term_exchange() and free the memory here.
4144 cmd->trc_flags |= TRC_DO_WORK_ERR;
4145 spin_lock_irqsave(qpair->qp_lock_ptr, flags);
4146 qlt_send_term_exchange(qpair, NULL, &cmd->atio, 1, 0);
4148 qlt_decr_num_pend_cmds(vha);
4149 percpu_ida_free(&sess->se_sess->sess_tag_pool, cmd->se_cmd.map_tag);
4150 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
4152 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
4153 ha->tgt.tgt_ops->put_sess(sess);
4154 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
4157 static void qlt_do_work(struct work_struct *work)
4159 struct qla_tgt_cmd *cmd = container_of(work, struct qla_tgt_cmd, work);
4160 scsi_qla_host_t *vha = cmd->vha;
4161 unsigned long flags;
4163 spin_lock_irqsave(&vha->cmd_list_lock, flags);
4164 list_del(&cmd->cmd_list);
4165 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
4170 void qlt_clr_qp_table(struct scsi_qla_host *vha)
4172 unsigned long flags;
4173 struct qla_hw_data *ha = vha->hw;
4174 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
4178 ql_log(ql_log_info, vha, 0x706c,
4179 "User update Number of Active Qpairs %d\n",
4180 ha->tgt.num_act_qpairs);
4182 spin_lock_irqsave(&ha->tgt.atio_lock, flags);
4184 btree_for_each_safe64(&tgt->lun_qpair_map, key, node)
4185 btree_remove64(&tgt->lun_qpair_map, key);
4187 ha->base_qpair->lun_cnt = 0;
4188 for (key = 0; key < ha->max_qpairs; key++)
4189 if (ha->queue_pair_map[key])
4190 ha->queue_pair_map[key]->lun_cnt = 0;
4192 spin_unlock_irqrestore(&ha->tgt.atio_lock, flags);
4195 static void qlt_assign_qpair(struct scsi_qla_host *vha,
4196 struct qla_tgt_cmd *cmd)
4198 struct qla_qpair *qpair, *qp;
4199 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
4200 struct qla_qpair_hint *h;
4202 if (vha->flags.qpairs_available) {
4203 h = btree_lookup64(&tgt->lun_qpair_map, cmd->unpacked_lun);
4205 /* spread lun to qpair ratio evently */
4207 struct scsi_qla_host *base_vha =
4208 pci_get_drvdata(vha->hw->pdev);
4210 qpair = vha->hw->base_qpair;
4211 if (qpair->lun_cnt == 0) {
4213 h = qla_qpair_to_hint(tgt, qpair);
4215 rc = btree_insert64(&tgt->lun_qpair_map,
4216 cmd->unpacked_lun, h, GFP_ATOMIC);
4219 ql_log(ql_log_info, vha, 0xd037,
4220 "Unable to insert lun %llx into lun_qpair_map\n",
4225 lcnt = qpair->lun_cnt;
4229 list_for_each_entry(qp, &base_vha->qp_list,
4231 if (qp->lun_cnt == 0) {
4233 h = qla_qpair_to_hint(tgt, qp);
4235 rc = btree_insert64(&tgt->lun_qpair_map,
4236 cmd->unpacked_lun, h, GFP_ATOMIC);
4239 ql_log(ql_log_info, vha, 0xd038,
4240 "Unable to insert lun %llx into lun_qpair_map\n",
4246 if (qp->lun_cnt < lcnt) {
4255 h = qla_qpair_to_hint(tgt, qpair);
4257 rc = btree_insert64(&tgt->lun_qpair_map,
4258 cmd->unpacked_lun, h, GFP_ATOMIC);
4261 ql_log(ql_log_info, vha, 0xd039,
4262 "Unable to insert lun %llx into lun_qpair_map\n",
4267 h = &tgt->qphints[0];
4270 cmd->qpair = h->qpair;
4271 cmd->se_cmd.cpuid = h->cpuid;
4274 static struct qla_tgt_cmd *qlt_get_tag(scsi_qla_host_t *vha,
4275 struct fc_port *sess,
4276 struct atio_from_isp *atio)
4278 struct se_session *se_sess = sess->se_sess;
4279 struct qla_tgt_cmd *cmd;
4282 tag = percpu_ida_alloc(&se_sess->sess_tag_pool, TASK_RUNNING);
4286 cmd = &((struct qla_tgt_cmd *)se_sess->sess_cmd_map)[tag];
4287 memset(cmd, 0, sizeof(struct qla_tgt_cmd));
4288 cmd->cmd_type = TYPE_TGT_CMD;
4289 memcpy(&cmd->atio, atio, sizeof(*atio));
4290 cmd->state = QLA_TGT_STATE_NEW;
4291 cmd->tgt = vha->vha_tgt.qla_tgt;
4292 qlt_incr_num_pend_cmds(vha);
4294 cmd->se_cmd.map_tag = tag;
4296 cmd->loop_id = sess->loop_id;
4297 cmd->conf_compl_supported = sess->conf_compl_supported;
4300 cmd->jiffies_at_alloc = get_jiffies_64();
4302 cmd->unpacked_lun = scsilun_to_int(
4303 (struct scsi_lun *)&atio->u.isp24.fcp_cmnd.lun);
4304 qlt_assign_qpair(vha, cmd);
4305 cmd->reset_count = vha->hw->base_qpair->chip_reset;
4306 cmd->vp_idx = vha->vp_idx;
4311 /* ha->hardware_lock supposed to be held on entry */
4312 static int qlt_handle_cmd_for_atio(struct scsi_qla_host *vha,
4313 struct atio_from_isp *atio)
4315 struct qla_hw_data *ha = vha->hw;
4316 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
4317 struct fc_port *sess;
4318 struct qla_tgt_cmd *cmd;
4319 unsigned long flags;
4322 if (unlikely(tgt->tgt_stop)) {
4323 ql_dbg(ql_dbg_io, vha, 0x3061,
4324 "New command while device %p is shutting down\n", tgt);
4328 id.b.al_pa = atio->u.isp24.fcp_hdr.s_id[2];
4329 id.b.area = atio->u.isp24.fcp_hdr.s_id[1];
4330 id.b.domain = atio->u.isp24.fcp_hdr.s_id[0];
4331 if (IS_SW_RESV_ADDR(id))
4334 sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha, atio->u.isp24.fcp_hdr.s_id);
4335 if (unlikely(!sess))
4338 /* Another WWN used to have our s_id. Our PLOGI scheduled its
4339 * session deletion, but it's still in sess_del_work wq */
4340 if (sess->deleted) {
4341 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf002,
4342 "New command while old session %p is being deleted\n",
4348 * Do kref_get() before returning + dropping qla_hw_data->hardware_lock.
4350 if (!kref_get_unless_zero(&sess->sess_kref)) {
4351 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf004,
4352 "%s: kref_get fail, %8phC oxid %x \n",
4353 __func__, sess->port_name,
4354 be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id));
4358 cmd = qlt_get_tag(vha, sess, atio);
4360 ql_dbg(ql_dbg_io, vha, 0x3062,
4361 "qla_target(%d): Allocation of cmd failed\n", vha->vp_idx);
4362 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
4363 ha->tgt.tgt_ops->put_sess(sess);
4364 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
4369 cmd->trc_flags |= TRC_NEW_CMD;
4371 spin_lock_irqsave(&vha->cmd_list_lock, flags);
4372 list_add_tail(&cmd->cmd_list, &vha->qla_cmd_list);
4373 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
4375 INIT_WORK(&cmd->work, qlt_do_work);
4376 if (vha->flags.qpairs_available) {
4377 queue_work_on(cmd->se_cmd.cpuid, qla_tgt_wq, &cmd->work);
4378 } else if (ha->msix_count) {
4379 if (cmd->atio.u.isp24.fcp_cmnd.rddata)
4380 queue_work_on(smp_processor_id(), qla_tgt_wq,
4383 queue_work_on(cmd->se_cmd.cpuid, qla_tgt_wq,
4386 queue_work(qla_tgt_wq, &cmd->work);
4392 /* ha->hardware_lock supposed to be held on entry */
4393 static int qlt_issue_task_mgmt(struct fc_port *sess, u64 lun,
4394 int fn, void *iocb, int flags)
4396 struct scsi_qla_host *vha = sess->vha;
4397 struct qla_hw_data *ha = vha->hw;
4398 struct qla_tgt_mgmt_cmd *mcmd;
4399 struct atio_from_isp *a = (struct atio_from_isp *)iocb;
4400 struct qla_qpair_hint *h = &vha->vha_tgt.qla_tgt->qphints[0];
4402 mcmd = mempool_alloc(qla_tgt_mgmt_cmd_mempool, GFP_ATOMIC);
4404 ql_dbg(ql_dbg_tgt_tmr, vha, 0x10009,
4405 "qla_target(%d): Allocation of management "
4406 "command failed, some commands and their data could "
4407 "leak\n", vha->vp_idx);
4410 memset(mcmd, 0, sizeof(*mcmd));
4414 memcpy(&mcmd->orig_iocb.imm_ntfy, iocb,
4415 sizeof(mcmd->orig_iocb.imm_ntfy));
4417 mcmd->tmr_func = fn;
4418 mcmd->flags = flags;
4419 mcmd->reset_count = ha->base_qpair->chip_reset;
4420 mcmd->qpair = h->qpair;
4422 mcmd->se_cmd.cpuid = h->cpuid;
4423 mcmd->unpacked_lun = lun;
4426 case QLA_TGT_LUN_RESET:
4427 case QLA_TGT_CLEAR_TS:
4428 case QLA_TGT_ABORT_TS:
4429 abort_cmds_for_lun(vha, lun, a->u.isp24.fcp_hdr.s_id);
4431 case QLA_TGT_CLEAR_ACA:
4432 h = qlt_find_qphint(vha, mcmd->unpacked_lun);
4433 mcmd->qpair = h->qpair;
4434 mcmd->se_cmd.cpuid = h->cpuid;
4437 case QLA_TGT_TARGET_RESET:
4438 case QLA_TGT_NEXUS_LOSS_SESS:
4439 case QLA_TGT_NEXUS_LOSS:
4440 case QLA_TGT_ABORT_ALL:
4446 INIT_WORK(&mcmd->work, qlt_do_tmr_work);
4447 queue_work_on(mcmd->se_cmd.cpuid, qla_tgt_wq,
4453 /* ha->hardware_lock supposed to be held on entry */
4454 static int qlt_handle_task_mgmt(struct scsi_qla_host *vha, void *iocb)
4456 struct atio_from_isp *a = (struct atio_from_isp *)iocb;
4457 struct qla_hw_data *ha = vha->hw;
4458 struct fc_port *sess;
4461 unsigned long flags;
4463 fn = a->u.isp24.fcp_cmnd.task_mgmt_flags;
4465 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
4466 sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha,
4467 a->u.isp24.fcp_hdr.s_id);
4468 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
4471 scsilun_to_int((struct scsi_lun *)&a->u.isp24.fcp_cmnd.lun);
4473 if (sess == NULL || sess->deleted)
4476 return qlt_issue_task_mgmt(sess, unpacked_lun, fn, iocb, 0);
4479 /* ha->hardware_lock supposed to be held on entry */
4480 static int __qlt_abort_task(struct scsi_qla_host *vha,
4481 struct imm_ntfy_from_isp *iocb, struct fc_port *sess)
4483 struct atio_from_isp *a = (struct atio_from_isp *)iocb;
4484 struct qla_hw_data *ha = vha->hw;
4485 struct qla_tgt_mgmt_cmd *mcmd;
4489 mcmd = mempool_alloc(qla_tgt_mgmt_cmd_mempool, GFP_ATOMIC);
4491 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05f,
4492 "qla_target(%d): %s: Allocation of ABORT cmd failed\n",
4493 vha->vp_idx, __func__);
4496 memset(mcmd, 0, sizeof(*mcmd));
4499 memcpy(&mcmd->orig_iocb.imm_ntfy, iocb,
4500 sizeof(mcmd->orig_iocb.imm_ntfy));
4503 scsilun_to_int((struct scsi_lun *)&a->u.isp24.fcp_cmnd.lun);
4504 mcmd->reset_count = ha->base_qpair->chip_reset;
4505 mcmd->tmr_func = QLA_TGT_2G_ABORT_TASK;
4506 mcmd->qpair = ha->base_qpair;
4508 rc = ha->tgt.tgt_ops->handle_tmr(mcmd, unpacked_lun, mcmd->tmr_func,
4509 le16_to_cpu(iocb->u.isp2x.seq_id));
4511 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf060,
4512 "qla_target(%d): tgt_ops->handle_tmr() failed: %d\n",
4514 mempool_free(mcmd, qla_tgt_mgmt_cmd_mempool);
4521 /* ha->hardware_lock supposed to be held on entry */
4522 static int qlt_abort_task(struct scsi_qla_host *vha,
4523 struct imm_ntfy_from_isp *iocb)
4525 struct qla_hw_data *ha = vha->hw;
4526 struct fc_port *sess;
4528 unsigned long flags;
4530 loop_id = GET_TARGET_ID(ha, (struct atio_from_isp *)iocb);
4532 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
4533 sess = ha->tgt.tgt_ops->find_sess_by_loop_id(vha, loop_id);
4534 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
4537 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf025,
4538 "qla_target(%d): task abort for unexisting "
4539 "session\n", vha->vp_idx);
4540 return qlt_sched_sess_work(vha->vha_tgt.qla_tgt,
4541 QLA_TGT_SESS_WORK_ABORT, iocb, sizeof(*iocb));
4544 return __qlt_abort_task(vha, iocb, sess);
4547 void qlt_logo_completion_handler(fc_port_t *fcport, int rc)
4549 if (rc != MBS_COMMAND_COMPLETE) {
4550 ql_dbg(ql_dbg_tgt_mgt, fcport->vha, 0xf093,
4551 "%s: se_sess %p / sess %p from"
4552 " port %8phC loop_id %#04x s_id %02x:%02x:%02x"
4553 " LOGO failed: %#x\n",
4557 fcport->port_name, fcport->loop_id,
4558 fcport->d_id.b.domain, fcport->d_id.b.area,
4559 fcport->d_id.b.al_pa, rc);
4562 fcport->logout_completed = 1;
4566 * ha->hardware_lock supposed to be held on entry (to protect tgt->sess_list)
4568 * Schedules sessions with matching port_id/loop_id but different wwn for
4569 * deletion. Returns existing session with matching wwn if present.
4573 qlt_find_sess_invalidate_other(scsi_qla_host_t *vha, uint64_t wwn,
4574 port_id_t port_id, uint16_t loop_id, struct fc_port **conflict_sess)
4576 struct fc_port *sess = NULL, *other_sess;
4579 *conflict_sess = NULL;
4581 list_for_each_entry(other_sess, &vha->vp_fcports, list) {
4583 other_wwn = wwn_to_u64(other_sess->port_name);
4585 if (wwn == other_wwn) {
4591 /* find other sess with nport_id collision */
4592 if (port_id.b24 == other_sess->d_id.b24) {
4593 if (loop_id != other_sess->loop_id) {
4594 ql_dbg(ql_dbg_tgt_tmr, vha, 0x1000c,
4595 "Invalidating sess %p loop_id %d wwn %llx.\n",
4596 other_sess, other_sess->loop_id, other_wwn);
4599 * logout_on_delete is set by default, but another
4600 * session that has the same s_id/loop_id combo
4601 * might have cleared it when requested this session
4602 * deletion, so don't touch it
4604 qlt_schedule_sess_for_deletion(other_sess);
4607 * Another wwn used to have our s_id/loop_id
4608 * kill the session, but don't free the loop_id
4610 ql_dbg(ql_dbg_tgt_tmr, vha, 0xf01b,
4611 "Invalidating sess %p loop_id %d wwn %llx.\n",
4612 other_sess, other_sess->loop_id, other_wwn);
4614 other_sess->keep_nport_handle = 1;
4615 if (other_sess->disc_state != DSC_DELETED)
4616 *conflict_sess = other_sess;
4617 qlt_schedule_sess_for_deletion(other_sess);
4622 /* find other sess with nport handle collision */
4623 if ((loop_id == other_sess->loop_id) &&
4624 (loop_id != FC_NO_LOOP_ID)) {
4625 ql_dbg(ql_dbg_tgt_tmr, vha, 0x1000d,
4626 "Invalidating sess %p loop_id %d wwn %llx.\n",
4627 other_sess, other_sess->loop_id, other_wwn);
4629 /* Same loop_id but different s_id
4630 * Ok to kill and logout */
4631 qlt_schedule_sess_for_deletion(other_sess);
4638 /* Abort any commands for this s_id waiting on qla_tgt_wq workqueue */
4639 static int abort_cmds_for_s_id(struct scsi_qla_host *vha, port_id_t *s_id)
4641 struct qla_tgt_sess_op *op;
4642 struct qla_tgt_cmd *cmd;
4645 unsigned long flags;
4647 key = (((u32)s_id->b.domain << 16) |
4648 ((u32)s_id->b.area << 8) |
4649 ((u32)s_id->b.al_pa));
4651 spin_lock_irqsave(&vha->cmd_list_lock, flags);
4652 list_for_each_entry(op, &vha->qla_sess_op_cmd_list, cmd_list) {
4653 uint32_t op_key = sid_to_key(op->atio.u.isp24.fcp_hdr.s_id);
4655 if (op_key == key) {
4661 list_for_each_entry(op, &vha->unknown_atio_list, cmd_list) {
4662 uint32_t op_key = sid_to_key(op->atio.u.isp24.fcp_hdr.s_id);
4663 if (op_key == key) {
4669 list_for_each_entry(cmd, &vha->qla_cmd_list, cmd_list) {
4670 uint32_t cmd_key = sid_to_key(cmd->atio.u.isp24.fcp_hdr.s_id);
4671 if (cmd_key == key) {
4676 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
4681 static int qlt_handle_login(struct scsi_qla_host *vha,
4682 struct imm_ntfy_from_isp *iocb)
4684 struct fc_port *sess = NULL, *conflict_sess = NULL;
4687 uint16_t loop_id, wd3_lo;
4689 struct qlt_plogi_ack_t *pla;
4690 unsigned long flags;
4692 wwn = wwn_to_u64(iocb->u.isp24.port_name);
4694 port_id.b.domain = iocb->u.isp24.port_id[2];
4695 port_id.b.area = iocb->u.isp24.port_id[1];
4696 port_id.b.al_pa = iocb->u.isp24.port_id[0];
4697 port_id.b.rsvd_1 = 0;
4699 loop_id = le16_to_cpu(iocb->u.isp24.nport_handle);
4701 /* Mark all stale commands sitting in qla_tgt_wq for deletion */
4702 abort_cmds_for_s_id(vha, &port_id);
4705 spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
4706 sess = qlt_find_sess_invalidate_other(vha, wwn,
4707 port_id, loop_id, &conflict_sess);
4708 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
4711 if (IS_SW_RESV_ADDR(port_id)) {
4716 pla = qlt_plogi_ack_find_add(vha, &port_id, iocb);
4718 qlt_send_term_imm_notif(vha, iocb, 1);
4722 if (conflict_sess) {
4723 conflict_sess->login_gen++;
4724 qlt_plogi_ack_link(vha, pla, conflict_sess,
4725 QLT_PLOGI_LINK_CONFLICT);
4730 ql_dbg(ql_dbg_disc, vha, 0xffff,
4731 "%s %d %8phC post new sess\n",
4732 __func__, __LINE__, iocb->u.isp24.port_name);
4733 if (iocb->u.isp24.status_subcode == ELS_PLOGI)
4734 qla24xx_post_newsess_work(vha, &port_id,
4735 iocb->u.isp24.port_name,
4736 iocb->u.isp24.u.plogi.node_name,
4737 pla, FC4_TYPE_UNKNOWN);
4739 qla24xx_post_newsess_work(vha, &port_id,
4740 iocb->u.isp24.port_name, NULL,
4741 pla, FC4_TYPE_UNKNOWN);
4746 qlt_plogi_ack_link(vha, pla, sess, QLT_PLOGI_LINK_SAME_WWN);
4747 sess->d_id = port_id;
4750 if (iocb->u.isp24.status_subcode == ELS_PRLI) {
4751 sess->fw_login_state = DSC_LS_PRLI_PEND;
4753 sess->loop_id = loop_id;
4754 sess->d_id = port_id;
4755 sess->fw_login_state = DSC_LS_PRLI_PEND;
4756 wd3_lo = le16_to_cpu(iocb->u.isp24.u.prli.wd3_lo);
4759 sess->conf_compl_supported = 1;
4761 if ((wd3_lo & BIT_4) == 0)
4762 sess->port_type = FCT_INITIATOR;
4764 sess->port_type = FCT_TARGET;
4767 sess->fw_login_state = DSC_LS_PLOGI_PEND;
4770 ql_dbg(ql_dbg_disc, vha, 0x20f9,
4771 "%s %d %8phC DS %d\n",
4772 __func__, __LINE__, sess->port_name, sess->disc_state);
4774 switch (sess->disc_state) {
4776 qlt_plogi_ack_unref(vha, pla);
4781 * Under normal circumstances we want to release nport handle
4782 * during LOGO process to avoid nport handle leaks inside FW.
4783 * The exception is when LOGO is done while another PLOGI with
4784 * the same nport handle is waiting as might be the case here.
4785 * Note: there is always a possibily of a race where session
4786 * deletion has already started for other reasons (e.g. ACL
4787 * removal) and now PLOGI arrives:
4788 * 1. if PLOGI arrived in FW after nport handle has been freed,
4789 * FW must have assigned this PLOGI a new/same handle and we
4790 * can proceed ACK'ing it as usual when session deletion
4792 * 2. if PLOGI arrived in FW before LOGO with LCF_FREE_NPORT
4793 * bit reached it, the handle has now been released. We'll
4794 * get an error when we ACK this PLOGI. Nothing will be sent
4795 * back to initiator. Initiator should eventually retry
4796 * PLOGI and situation will correct itself.
4798 sess->keep_nport_handle = ((sess->loop_id == loop_id) &&
4799 (sess->d_id.b24 == port_id.b24));
4801 ql_dbg(ql_dbg_disc, vha, 0x20f9,
4802 "%s %d %8phC post del sess\n",
4803 __func__, __LINE__, sess->port_name);
4806 qlt_schedule_sess_for_deletion(sess);
4814 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
4816 static int qlt_24xx_handle_els(struct scsi_qla_host *vha,
4817 struct imm_ntfy_from_isp *iocb)
4819 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
4820 struct qla_hw_data *ha = vha->hw;
4821 struct fc_port *sess = NULL, *conflict_sess = NULL;
4827 unsigned long flags;
4829 wwn = wwn_to_u64(iocb->u.isp24.port_name);
4831 port_id.b.domain = iocb->u.isp24.port_id[2];
4832 port_id.b.area = iocb->u.isp24.port_id[1];
4833 port_id.b.al_pa = iocb->u.isp24.port_id[0];
4834 port_id.b.rsvd_1 = 0;
4836 loop_id = le16_to_cpu(iocb->u.isp24.nport_handle);
4838 ql_dbg(ql_dbg_disc, vha, 0xf026,
4839 "qla_target(%d): Port ID: %02x:%02x:%02x ELS opcode: 0x%02x lid %d %8phC\n",
4840 vha->vp_idx, iocb->u.isp24.port_id[2],
4841 iocb->u.isp24.port_id[1], iocb->u.isp24.port_id[0],
4842 iocb->u.isp24.status_subcode, loop_id,
4843 iocb->u.isp24.port_name);
4845 /* res = 1 means ack at the end of thread
4846 * res = 0 means ack async/later.
4848 switch (iocb->u.isp24.status_subcode) {
4850 res = qlt_handle_login(vha, iocb);
4855 sess = qla2x00_find_fcport_by_wwpn(vha,
4856 iocb->u.isp24.port_name, 1);
4858 if (sess && sess->plogi_link[QLT_PLOGI_LINK_SAME_WWN]) {
4859 ql_dbg(ql_dbg_disc, vha, 0xffff,
4860 "%s %d %8phC Term PRLI due to PLOGI ACK not completed\n",
4862 iocb->u.isp24.port_name);
4863 qlt_send_term_imm_notif(vha, iocb, 1);
4867 res = qlt_handle_login(vha, iocb);
4871 if (IS_SW_RESV_ADDR(port_id)) {
4876 wd3_lo = le16_to_cpu(iocb->u.isp24.u.prli.wd3_lo);
4879 spin_lock_irqsave(&tgt->ha->tgt.sess_lock, flags);
4880 sess = qlt_find_sess_invalidate_other(vha, wwn, port_id,
4881 loop_id, &conflict_sess);
4882 spin_unlock_irqrestore(&tgt->ha->tgt.sess_lock, flags);
4885 if (conflict_sess) {
4886 switch (conflict_sess->disc_state) {
4888 case DSC_DELETE_PEND:
4891 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf09b,
4892 "PRLI with conflicting sess %p port %8phC\n",
4893 conflict_sess, conflict_sess->port_name);
4894 conflict_sess->fw_login_state =
4895 DSC_LS_PORT_UNAVAIL;
4896 qlt_send_term_imm_notif(vha, iocb, 1);
4903 bool delete = false;
4904 spin_lock_irqsave(&tgt->ha->tgt.sess_lock, flags);
4905 switch (sess->fw_login_state) {
4906 case DSC_LS_PLOGI_PEND:
4907 case DSC_LS_PLOGI_COMP:
4908 case DSC_LS_PRLI_COMP:
4915 switch (sess->disc_state) {
4916 case DSC_LOGIN_PEND:
4918 case DSC_UPD_FCPORT:
4919 case DSC_LOGIN_COMPLETE:
4928 spin_unlock_irqrestore(&tgt->ha->tgt.sess_lock,
4931 * Impatient initiator sent PRLI before last
4932 * PLOGI could finish. Will force him to re-try,
4933 * while last one finishes.
4935 ql_log(ql_log_warn, sess->vha, 0xf095,
4936 "sess %p PRLI received, before plogi ack.\n",
4938 qlt_send_term_imm_notif(vha, iocb, 1);
4944 * This shouldn't happen under normal circumstances,
4945 * since we have deleted the old session during PLOGI
4947 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf096,
4948 "PRLI (loop_id %#04x) for existing sess %p (loop_id %#04x)\n",
4949 sess->loop_id, sess, iocb->u.isp24.nport_handle);
4952 sess->loop_id = loop_id;
4953 sess->d_id = port_id;
4954 sess->fw_login_state = DSC_LS_PRLI_PEND;
4957 sess->conf_compl_supported = 1;
4959 if ((wd3_lo & BIT_4) == 0)
4960 sess->port_type = FCT_INITIATOR;
4962 sess->port_type = FCT_TARGET;
4964 spin_unlock_irqrestore(&tgt->ha->tgt.sess_lock, flags);
4966 res = 1; /* send notify ack */
4968 /* Make session global (not used in fabric mode) */
4969 if (ha->current_topology != ISP_CFG_F) {
4971 ql_dbg(ql_dbg_disc, vha, 0x20fa,
4972 "%s %d %8phC post nack\n",
4973 __func__, __LINE__, sess->port_name);
4974 qla24xx_post_nack_work(vha, sess, iocb,
4978 set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
4979 set_bit(LOCAL_LOOP_UPDATE, &vha->dpc_flags);
4980 qla2xxx_wake_dpc(vha);
4984 ql_dbg(ql_dbg_disc, vha, 0x20fb,
4985 "%s %d %8phC post nack\n",
4986 __func__, __LINE__, sess->port_name);
4987 qla24xx_post_nack_work(vha, sess, iocb,
4995 if (le16_to_cpu(iocb->u.isp24.flags) &
4996 NOTIFY24XX_FLAGS_GLOBAL_TPRLO) {
4998 qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS);
5005 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
5006 sess = qla2x00_find_fcport_by_loopid(vha, loop_id);
5007 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
5011 sess->fw_login_state = DSC_LS_LOGO_PEND;
5012 sess->logo_ack_needed = 1;
5013 memcpy(sess->iocb, iocb, IOCB_SIZE);
5016 res = qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS_SESS);
5018 ql_dbg(ql_dbg_disc, vha, 0x20fc,
5019 "%s: logo %llx res %d sess %p ",
5020 __func__, wwn, res, sess);
5023 * cmd went upper layer, look for qlt_xmit_tm_rsp()
5024 * for LOGO_ACK & sess delete
5029 /* cmd did not go to upper layer. */
5031 qlt_schedule_sess_for_deletion(sess);
5034 /* else logo will be ack */
5040 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
5041 if (tgt->link_reinit_iocb_pending) {
5042 qlt_send_notify_ack(ha->base_qpair,
5043 &tgt->link_reinit_iocb, 0, 0, 0, 0, 0, 0);
5044 tgt->link_reinit_iocb_pending = 0;
5047 sess = qla2x00_find_fcport_by_wwpn(vha,
5048 iocb->u.isp24.port_name, 1);
5050 ql_dbg(ql_dbg_disc, vha, 0x20fd,
5051 "sess %p lid %d|%d DS %d LS %d\n",
5052 sess, sess->loop_id, loop_id,
5053 sess->disc_state, sess->fw_login_state);
5056 res = 1; /* send notify ack */
5060 case ELS_FLOGI: /* should never happen */
5062 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf061,
5063 "qla_target(%d): Unsupported ELS command %x "
5064 "received\n", vha->vp_idx, iocb->u.isp24.status_subcode);
5065 res = qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS_SESS);
5069 ql_dbg(ql_dbg_disc, vha, 0xf026,
5070 "qla_target(%d): Exit ELS opcode: 0x%02x res %d\n",
5071 vha->vp_idx, iocb->u.isp24.status_subcode, res);
5077 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
5079 static void qlt_handle_imm_notify(struct scsi_qla_host *vha,
5080 struct imm_ntfy_from_isp *iocb)
5082 struct qla_hw_data *ha = vha->hw;
5083 uint32_t add_flags = 0;
5084 int send_notify_ack = 1;
5087 status = le16_to_cpu(iocb->u.isp2x.status);
5089 case IMM_NTFY_LIP_RESET:
5091 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf032,
5092 "qla_target(%d): LIP reset (loop %#x), subcode %x\n",
5093 vha->vp_idx, le16_to_cpu(iocb->u.isp24.nport_handle),
5094 iocb->u.isp24.status_subcode);
5096 if (qlt_reset(vha, iocb, QLA_TGT_ABORT_ALL) == 0)
5097 send_notify_ack = 0;
5101 case IMM_NTFY_LIP_LINK_REINIT:
5103 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
5104 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf033,
5105 "qla_target(%d): LINK REINIT (loop %#x, "
5106 "subcode %x)\n", vha->vp_idx,
5107 le16_to_cpu(iocb->u.isp24.nport_handle),
5108 iocb->u.isp24.status_subcode);
5109 if (tgt->link_reinit_iocb_pending) {
5110 qlt_send_notify_ack(ha->base_qpair,
5111 &tgt->link_reinit_iocb, 0, 0, 0, 0, 0, 0);
5113 memcpy(&tgt->link_reinit_iocb, iocb, sizeof(*iocb));
5114 tgt->link_reinit_iocb_pending = 1;
5116 * QLogic requires to wait after LINK REINIT for possible
5117 * PDISC or ADISC ELS commands
5119 send_notify_ack = 0;
5123 case IMM_NTFY_PORT_LOGOUT:
5124 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf034,
5125 "qla_target(%d): Port logout (loop "
5126 "%#x, subcode %x)\n", vha->vp_idx,
5127 le16_to_cpu(iocb->u.isp24.nport_handle),
5128 iocb->u.isp24.status_subcode);
5130 if (qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS_SESS) == 0)
5131 send_notify_ack = 0;
5132 /* The sessions will be cleared in the callback, if needed */
5135 case IMM_NTFY_GLBL_TPRLO:
5136 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf035,
5137 "qla_target(%d): Global TPRLO (%x)\n", vha->vp_idx, status);
5138 if (qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS) == 0)
5139 send_notify_ack = 0;
5140 /* The sessions will be cleared in the callback, if needed */
5143 case IMM_NTFY_PORT_CONFIG:
5144 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf036,
5145 "qla_target(%d): Port config changed (%x)\n", vha->vp_idx,
5147 if (qlt_reset(vha, iocb, QLA_TGT_ABORT_ALL) == 0)
5148 send_notify_ack = 0;
5149 /* The sessions will be cleared in the callback, if needed */
5152 case IMM_NTFY_GLBL_LOGO:
5153 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06a,
5154 "qla_target(%d): Link failure detected\n",
5156 /* I_T nexus loss */
5157 if (qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS) == 0)
5158 send_notify_ack = 0;
5161 case IMM_NTFY_IOCB_OVERFLOW:
5162 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06b,
5163 "qla_target(%d): Cannot provide requested "
5164 "capability (IOCB overflowed the immediate notify "
5165 "resource count)\n", vha->vp_idx);
5168 case IMM_NTFY_ABORT_TASK:
5169 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf037,
5170 "qla_target(%d): Abort Task (S %08x I %#x -> "
5171 "L %#x)\n", vha->vp_idx,
5172 le16_to_cpu(iocb->u.isp2x.seq_id),
5173 GET_TARGET_ID(ha, (struct atio_from_isp *)iocb),
5174 le16_to_cpu(iocb->u.isp2x.lun));
5175 if (qlt_abort_task(vha, iocb) == 0)
5176 send_notify_ack = 0;
5179 case IMM_NTFY_RESOURCE:
5180 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06c,
5181 "qla_target(%d): Out of resources, host %ld\n",
5182 vha->vp_idx, vha->host_no);
5185 case IMM_NTFY_MSG_RX:
5186 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf038,
5187 "qla_target(%d): Immediate notify task %x\n",
5188 vha->vp_idx, iocb->u.isp2x.task_flags);
5192 if (qlt_24xx_handle_els(vha, iocb) == 0)
5193 send_notify_ack = 0;
5196 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06d,
5197 "qla_target(%d): Received unknown immediate "
5198 "notify status %x\n", vha->vp_idx, status);
5202 if (send_notify_ack)
5203 qlt_send_notify_ack(ha->base_qpair, iocb, add_flags, 0, 0, 0,
5208 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
5209 * This function sends busy to ISP 2xxx or 24xx.
5211 static int __qlt_send_busy(struct qla_qpair *qpair,
5212 struct atio_from_isp *atio, uint16_t status)
5214 struct scsi_qla_host *vha = qpair->vha;
5215 struct ctio7_to_24xx *ctio24;
5216 struct qla_hw_data *ha = vha->hw;
5218 struct fc_port *sess = NULL;
5219 unsigned long flags;
5223 id.b.al_pa = atio->u.isp24.fcp_hdr.s_id[2];
5224 id.b.area = atio->u.isp24.fcp_hdr.s_id[1];
5225 id.b.domain = atio->u.isp24.fcp_hdr.s_id[0];
5228 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
5229 sess = qla2x00_find_fcport_by_nportid(vha, &id, 1);
5230 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
5232 qlt_send_term_exchange(qpair, NULL, atio, 1, 0);
5235 /* Sending marker isn't necessary, since we called from ISR */
5237 pkt = (request_t *)__qla2x00_alloc_iocbs(qpair, NULL);
5239 ql_dbg(ql_dbg_io, vha, 0x3063,
5240 "qla_target(%d): %s failed: unable to allocate "
5241 "request packet", vha->vp_idx, __func__);
5245 qpair->tgt_counters.num_q_full_sent++;
5246 pkt->entry_count = 1;
5247 pkt->handle = QLA_TGT_SKIP_HANDLE | CTIO_COMPLETION_HANDLE_MARK;
5249 ctio24 = (struct ctio7_to_24xx *)pkt;
5250 ctio24->entry_type = CTIO_TYPE7;
5251 ctio24->nport_handle = sess->loop_id;
5252 ctio24->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
5253 ctio24->vp_index = vha->vp_idx;
5254 ctio24->initiator_id[0] = atio->u.isp24.fcp_hdr.s_id[2];
5255 ctio24->initiator_id[1] = atio->u.isp24.fcp_hdr.s_id[1];
5256 ctio24->initiator_id[2] = atio->u.isp24.fcp_hdr.s_id[0];
5257 ctio24->exchange_addr = atio->u.isp24.exchange_addr;
5258 temp = (atio->u.isp24.attr << 9) |
5259 CTIO7_FLAGS_STATUS_MODE_1 | CTIO7_FLAGS_SEND_STATUS |
5260 CTIO7_FLAGS_DONT_RET_CTIO;
5261 ctio24->u.status1.flags = cpu_to_le16(temp);
5263 * CTIO from fw w/o se_cmd doesn't provide enough info to retry it,
5264 * if the explicit conformation is used.
5266 ctio24->u.status1.ox_id = swab16(atio->u.isp24.fcp_hdr.ox_id);
5267 ctio24->u.status1.scsi_status = cpu_to_le16(status);
5269 ctio24->u.status1.residual = get_datalen_for_atio(atio);
5271 if (ctio24->u.status1.residual != 0)
5272 ctio24->u.status1.scsi_status |= SS_RESIDUAL_UNDER;
5274 /* Memory Barrier */
5276 if (qpair->reqq_start_iocbs)
5277 qpair->reqq_start_iocbs(qpair);
5279 qla2x00_start_iocbs(vha, qpair->req);
5284 * This routine is used to allocate a command for either a QFull condition
5285 * (ie reply SAM_STAT_BUSY) or to terminate an exchange that did not go
5289 qlt_alloc_qfull_cmd(struct scsi_qla_host *vha,
5290 struct atio_from_isp *atio, uint16_t status, int qfull)
5292 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
5293 struct qla_hw_data *ha = vha->hw;
5294 struct fc_port *sess;
5295 struct se_session *se_sess;
5296 struct qla_tgt_cmd *cmd;
5298 unsigned long flags;
5300 if (unlikely(tgt->tgt_stop)) {
5301 ql_dbg(ql_dbg_io, vha, 0x300a,
5302 "New command while device %p is shutting down\n", tgt);
5306 if ((vha->hw->tgt.num_qfull_cmds_alloc + 1) > MAX_QFULL_CMDS_ALLOC) {
5307 vha->hw->tgt.num_qfull_cmds_dropped++;
5308 if (vha->hw->tgt.num_qfull_cmds_dropped >
5309 vha->qla_stats.stat_max_qfull_cmds_dropped)
5310 vha->qla_stats.stat_max_qfull_cmds_dropped =
5311 vha->hw->tgt.num_qfull_cmds_dropped;
5313 ql_dbg(ql_dbg_io, vha, 0x3068,
5314 "qla_target(%d): %s: QFull CMD dropped[%d]\n",
5315 vha->vp_idx, __func__,
5316 vha->hw->tgt.num_qfull_cmds_dropped);
5318 qlt_chk_exch_leak_thresh_hold(vha);
5322 sess = ha->tgt.tgt_ops->find_sess_by_s_id
5323 (vha, atio->u.isp24.fcp_hdr.s_id);
5327 se_sess = sess->se_sess;
5329 tag = percpu_ida_alloc(&se_sess->sess_tag_pool, TASK_RUNNING);
5333 cmd = &((struct qla_tgt_cmd *)se_sess->sess_cmd_map)[tag];
5335 ql_dbg(ql_dbg_io, vha, 0x3009,
5336 "qla_target(%d): %s: Allocation of cmd failed\n",
5337 vha->vp_idx, __func__);
5339 vha->hw->tgt.num_qfull_cmds_dropped++;
5340 if (vha->hw->tgt.num_qfull_cmds_dropped >
5341 vha->qla_stats.stat_max_qfull_cmds_dropped)
5342 vha->qla_stats.stat_max_qfull_cmds_dropped =
5343 vha->hw->tgt.num_qfull_cmds_dropped;
5345 qlt_chk_exch_leak_thresh_hold(vha);
5349 memset(cmd, 0, sizeof(struct qla_tgt_cmd));
5351 qlt_incr_num_pend_cmds(vha);
5352 INIT_LIST_HEAD(&cmd->cmd_list);
5353 memcpy(&cmd->atio, atio, sizeof(*atio));
5355 cmd->tgt = vha->vha_tgt.qla_tgt;
5357 cmd->reset_count = ha->base_qpair->chip_reset;
5359 cmd->qpair = ha->base_qpair;
5363 /* NOTE: borrowing the state field to carry the status */
5364 cmd->state = status;
5366 cmd->term_exchg = 1;
5368 spin_lock_irqsave(&vha->hw->tgt.q_full_lock, flags);
5369 list_add_tail(&cmd->cmd_list, &vha->hw->tgt.q_full_list);
5371 vha->hw->tgt.num_qfull_cmds_alloc++;
5372 if (vha->hw->tgt.num_qfull_cmds_alloc >
5373 vha->qla_stats.stat_max_qfull_cmds_alloc)
5374 vha->qla_stats.stat_max_qfull_cmds_alloc =
5375 vha->hw->tgt.num_qfull_cmds_alloc;
5376 spin_unlock_irqrestore(&vha->hw->tgt.q_full_lock, flags);
5380 qlt_free_qfull_cmds(struct qla_qpair *qpair)
5382 struct scsi_qla_host *vha = qpair->vha;
5383 struct qla_hw_data *ha = vha->hw;
5384 unsigned long flags;
5385 struct qla_tgt_cmd *cmd, *tcmd;
5386 struct list_head free_list, q_full_list;
5389 if (list_empty(&ha->tgt.q_full_list))
5392 INIT_LIST_HEAD(&free_list);
5393 INIT_LIST_HEAD(&q_full_list);
5395 spin_lock_irqsave(&vha->hw->tgt.q_full_lock, flags);
5396 if (list_empty(&ha->tgt.q_full_list)) {
5397 spin_unlock_irqrestore(&vha->hw->tgt.q_full_lock, flags);
5401 list_splice_init(&vha->hw->tgt.q_full_list, &q_full_list);
5402 spin_unlock_irqrestore(&vha->hw->tgt.q_full_lock, flags);
5404 spin_lock_irqsave(qpair->qp_lock_ptr, flags);
5405 list_for_each_entry_safe(cmd, tcmd, &q_full_list, cmd_list) {
5407 /* cmd->state is a borrowed field to hold status */
5408 rc = __qlt_send_busy(qpair, &cmd->atio, cmd->state);
5409 else if (cmd->term_exchg)
5410 rc = __qlt_send_term_exchange(qpair, NULL, &cmd->atio);
5416 ql_dbg(ql_dbg_io, vha, 0x3006,
5417 "%s: busy sent for ox_id[%04x]\n", __func__,
5418 be16_to_cpu(cmd->atio.u.isp24.fcp_hdr.ox_id));
5419 else if (cmd->term_exchg)
5420 ql_dbg(ql_dbg_io, vha, 0x3007,
5421 "%s: Term exchg sent for ox_id[%04x]\n", __func__,
5422 be16_to_cpu(cmd->atio.u.isp24.fcp_hdr.ox_id));
5424 ql_dbg(ql_dbg_io, vha, 0x3008,
5425 "%s: Unexpected cmd in QFull list %p\n", __func__,
5428 list_del(&cmd->cmd_list);
5429 list_add_tail(&cmd->cmd_list, &free_list);
5431 /* piggy back on hardware_lock for protection */
5432 vha->hw->tgt.num_qfull_cmds_alloc--;
5434 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
5438 list_for_each_entry_safe(cmd, tcmd, &free_list, cmd_list) {
5439 list_del(&cmd->cmd_list);
5440 /* This cmd was never sent to TCM. There is no need
5441 * to schedule free or call free_cmd
5446 if (!list_empty(&q_full_list)) {
5447 spin_lock_irqsave(&vha->hw->tgt.q_full_lock, flags);
5448 list_splice(&q_full_list, &vha->hw->tgt.q_full_list);
5449 spin_unlock_irqrestore(&vha->hw->tgt.q_full_lock, flags);
5456 qlt_send_busy(struct qla_qpair *qpair, struct atio_from_isp *atio,
5460 struct scsi_qla_host *vha = qpair->vha;
5462 rc = __qlt_send_busy(qpair, atio, status);
5464 qlt_alloc_qfull_cmd(vha, atio, status, 1);
5468 qlt_chk_qfull_thresh_hold(struct scsi_qla_host *vha, struct qla_qpair *qpair,
5469 struct atio_from_isp *atio, uint8_t ha_locked)
5471 struct qla_hw_data *ha = vha->hw;
5472 unsigned long flags;
5474 if (ha->tgt.num_pend_cmds < Q_FULL_THRESH_HOLD(ha))
5478 spin_lock_irqsave(&ha->hardware_lock, flags);
5479 qlt_send_busy(qpair, atio, qla_sam_status);
5481 spin_unlock_irqrestore(&ha->hardware_lock, flags);
5486 /* ha->hardware_lock supposed to be held on entry */
5487 /* called via callback from qla2xxx */
5488 static void qlt_24xx_atio_pkt(struct scsi_qla_host *vha,
5489 struct atio_from_isp *atio, uint8_t ha_locked)
5491 struct qla_hw_data *ha = vha->hw;
5492 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
5494 unsigned long flags = 0;
5496 if (unlikely(tgt == NULL)) {
5497 ql_dbg(ql_dbg_tgt, vha, 0x3064,
5498 "ATIO pkt, but no tgt (ha %p)", ha);
5502 * In tgt_stop mode we also should allow all requests to pass.
5503 * Otherwise, some commands can stuck.
5506 tgt->atio_irq_cmd_count++;
5508 switch (atio->u.raw.entry_type) {
5510 if (unlikely(atio->u.isp24.exchange_addr ==
5511 ATIO_EXCHANGE_ADDRESS_UNKNOWN)) {
5512 ql_dbg(ql_dbg_io, vha, 0x3065,
5513 "qla_target(%d): ATIO_TYPE7 "
5514 "received with UNKNOWN exchange address, "
5515 "sending QUEUE_FULL\n", vha->vp_idx);
5517 spin_lock_irqsave(&ha->hardware_lock, flags);
5518 qlt_send_busy(ha->base_qpair, atio, qla_sam_status);
5520 spin_unlock_irqrestore(&ha->hardware_lock,
5525 if (likely(atio->u.isp24.fcp_cmnd.task_mgmt_flags == 0)) {
5526 rc = qlt_chk_qfull_thresh_hold(vha, ha->base_qpair,
5529 tgt->atio_irq_cmd_count--;
5532 rc = qlt_handle_cmd_for_atio(vha, atio);
5534 rc = qlt_handle_task_mgmt(vha, atio);
5536 if (unlikely(rc != 0)) {
5538 spin_lock_irqsave(&ha->hardware_lock, flags);
5541 ql_dbg(ql_dbg_tgt, vha, 0xe05f,
5542 "qla_target: Unable to send command to target\n");
5545 ql_dbg(ql_dbg_tgt, vha, 0xe05f,
5546 "qla_target: Unable to send command to target, sending TERM EXCHANGE for rsp\n");
5547 qlt_send_term_exchange(ha->base_qpair, NULL,
5551 ql_dbg(ql_dbg_tgt, vha, 0xe060,
5552 "qla_target(%d): Unable to send command to target, sending BUSY status\n",
5554 qlt_send_busy(ha->base_qpair, atio,
5558 ql_dbg(ql_dbg_tgt, vha, 0xe060,
5559 "qla_target(%d): Unable to send command to target, sending BUSY status\n",
5561 qlt_send_busy(ha->base_qpair, atio,
5566 spin_unlock_irqrestore(&ha->hardware_lock,
5571 case IMMED_NOTIFY_TYPE:
5573 if (unlikely(atio->u.isp2x.entry_status != 0)) {
5574 ql_dbg(ql_dbg_tgt, vha, 0xe05b,
5575 "qla_target(%d): Received ATIO packet %x "
5576 "with error status %x\n", vha->vp_idx,
5577 atio->u.raw.entry_type,
5578 atio->u.isp2x.entry_status);
5581 ql_dbg(ql_dbg_tgt, vha, 0xe02e, "%s", "IMMED_NOTIFY ATIO");
5584 spin_lock_irqsave(&ha->hardware_lock, flags);
5585 qlt_handle_imm_notify(vha, (struct imm_ntfy_from_isp *)atio);
5587 spin_unlock_irqrestore(&ha->hardware_lock, flags);
5592 ql_dbg(ql_dbg_tgt, vha, 0xe05c,
5593 "qla_target(%d): Received unknown ATIO atio "
5594 "type %x\n", vha->vp_idx, atio->u.raw.entry_type);
5598 tgt->atio_irq_cmd_count--;
5601 /* ha->hardware_lock supposed to be held on entry */
5602 /* called via callback from qla2xxx */
5603 static void qlt_response_pkt(struct scsi_qla_host *vha,
5604 struct rsp_que *rsp, response_t *pkt)
5606 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
5608 if (unlikely(tgt == NULL)) {
5609 ql_dbg(ql_dbg_tgt, vha, 0xe05d,
5610 "qla_target(%d): Response pkt %x received, but no tgt (ha %p)\n",
5611 vha->vp_idx, pkt->entry_type, vha->hw);
5616 * In tgt_stop mode we also should allow all requests to pass.
5617 * Otherwise, some commands can stuck.
5620 switch (pkt->entry_type) {
5624 struct ctio7_from_24xx *entry = (struct ctio7_from_24xx *)pkt;
5625 qlt_do_ctio_completion(vha, rsp, entry->handle,
5626 le16_to_cpu(entry->status)|(pkt->entry_status << 16),
5631 case ACCEPT_TGT_IO_TYPE:
5633 struct atio_from_isp *atio = (struct atio_from_isp *)pkt;
5635 if (atio->u.isp2x.status !=
5636 cpu_to_le16(ATIO_CDB_VALID)) {
5637 ql_dbg(ql_dbg_tgt, vha, 0xe05e,
5638 "qla_target(%d): ATIO with error "
5639 "status %x received\n", vha->vp_idx,
5640 le16_to_cpu(atio->u.isp2x.status));
5644 rc = qlt_chk_qfull_thresh_hold(vha, rsp->qpair, atio, 1);
5648 rc = qlt_handle_cmd_for_atio(vha, atio);
5649 if (unlikely(rc != 0)) {
5652 ql_dbg(ql_dbg_tgt, vha, 0xe05f,
5653 "qla_target: Unable to send command to target\n");
5656 ql_dbg(ql_dbg_tgt, vha, 0xe05f,
5657 "qla_target: Unable to send command to target, sending TERM EXCHANGE for rsp\n");
5658 qlt_send_term_exchange(rsp->qpair, NULL,
5662 ql_dbg(ql_dbg_tgt, vha, 0xe060,
5663 "qla_target(%d): Unable to send command to target, sending BUSY status\n",
5665 qlt_send_busy(rsp->qpair, atio,
5669 ql_dbg(ql_dbg_tgt, vha, 0xe060,
5670 "qla_target(%d): Unable to send command to target, sending BUSY status\n",
5672 qlt_send_busy(rsp->qpair, atio,
5680 case CONTINUE_TGT_IO_TYPE:
5682 struct ctio_to_2xxx *entry = (struct ctio_to_2xxx *)pkt;
5683 qlt_do_ctio_completion(vha, rsp, entry->handle,
5684 le16_to_cpu(entry->status)|(pkt->entry_status << 16),
5691 struct ctio_to_2xxx *entry = (struct ctio_to_2xxx *)pkt;
5692 qlt_do_ctio_completion(vha, rsp, entry->handle,
5693 le16_to_cpu(entry->status)|(pkt->entry_status << 16),
5698 case IMMED_NOTIFY_TYPE:
5699 ql_dbg(ql_dbg_tgt, vha, 0xe035, "%s", "IMMED_NOTIFY\n");
5700 qlt_handle_imm_notify(vha, (struct imm_ntfy_from_isp *)pkt);
5703 case NOTIFY_ACK_TYPE:
5704 if (tgt->notify_ack_expected > 0) {
5705 struct nack_to_isp *entry = (struct nack_to_isp *)pkt;
5706 ql_dbg(ql_dbg_tgt, vha, 0xe036,
5707 "NOTIFY_ACK seq %08x status %x\n",
5708 le16_to_cpu(entry->u.isp2x.seq_id),
5709 le16_to_cpu(entry->u.isp2x.status));
5710 tgt->notify_ack_expected--;
5711 if (entry->u.isp2x.status !=
5712 cpu_to_le16(NOTIFY_ACK_SUCCESS)) {
5713 ql_dbg(ql_dbg_tgt, vha, 0xe061,
5714 "qla_target(%d): NOTIFY_ACK "
5715 "failed %x\n", vha->vp_idx,
5716 le16_to_cpu(entry->u.isp2x.status));
5719 ql_dbg(ql_dbg_tgt, vha, 0xe062,
5720 "qla_target(%d): Unexpected NOTIFY_ACK received\n",
5725 case ABTS_RECV_24XX:
5726 ql_dbg(ql_dbg_tgt, vha, 0xe037,
5727 "ABTS_RECV_24XX: instance %d\n", vha->vp_idx);
5728 qlt_24xx_handle_abts(vha, (struct abts_recv_from_24xx *)pkt);
5731 case ABTS_RESP_24XX:
5732 if (tgt->abts_resp_expected > 0) {
5733 struct abts_resp_from_24xx_fw *entry =
5734 (struct abts_resp_from_24xx_fw *)pkt;
5735 ql_dbg(ql_dbg_tgt, vha, 0xe038,
5736 "ABTS_RESP_24XX: compl_status %x\n",
5737 entry->compl_status);
5738 tgt->abts_resp_expected--;
5739 if (le16_to_cpu(entry->compl_status) !=
5740 ABTS_RESP_COMPL_SUCCESS) {
5741 if ((entry->error_subcode1 == 0x1E) &&
5742 (entry->error_subcode2 == 0)) {
5744 * We've got a race here: aborted
5745 * exchange not terminated, i.e.
5746 * response for the aborted command was
5747 * sent between the abort request was
5748 * received and processed.
5749 * Unfortunately, the firmware has a
5750 * silly requirement that all aborted
5751 * exchanges must be explicitely
5752 * terminated, otherwise it refuses to
5753 * send responses for the abort
5754 * requests. So, we have to
5755 * (re)terminate the exchange and retry
5756 * the abort response.
5758 qlt_24xx_retry_term_exchange(vha,
5761 ql_dbg(ql_dbg_tgt, vha, 0xe063,
5762 "qla_target(%d): ABTS_RESP_24XX "
5763 "failed %x (subcode %x:%x)",
5764 vha->vp_idx, entry->compl_status,
5765 entry->error_subcode1,
5766 entry->error_subcode2);
5769 ql_dbg(ql_dbg_tgt, vha, 0xe064,
5770 "qla_target(%d): Unexpected ABTS_RESP_24XX "
5771 "received\n", vha->vp_idx);
5776 ql_dbg(ql_dbg_tgt, vha, 0xe065,
5777 "qla_target(%d): Received unknown response pkt "
5778 "type %x\n", vha->vp_idx, pkt->entry_type);
5785 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
5787 void qlt_async_event(uint16_t code, struct scsi_qla_host *vha,
5790 struct qla_hw_data *ha = vha->hw;
5791 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
5794 if (!tgt || tgt->tgt_stop || tgt->tgt_stopped)
5797 if (((code == MBA_POINT_TO_POINT) || (code == MBA_CHG_IN_CONNECTION)) &&
5801 * In tgt_stop mode we also should allow all requests to pass.
5802 * Otherwise, some commands can stuck.
5807 case MBA_RESET: /* Reset */
5808 case MBA_SYSTEM_ERR: /* System Error */
5809 case MBA_REQ_TRANSFER_ERR: /* Request Transfer Error */
5810 case MBA_RSP_TRANSFER_ERR: /* Response Transfer Error */
5811 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03a,
5812 "qla_target(%d): System error async event %#x "
5813 "occurred", vha->vp_idx, code);
5815 case MBA_WAKEUP_THRES: /* Request Queue Wake-up. */
5816 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
5821 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03b,
5822 "qla_target(%d): Async LOOP_UP occurred "
5823 "(m[0]=%x, m[1]=%x, m[2]=%x, m[3]=%x)", vha->vp_idx,
5824 le16_to_cpu(mailbox[0]), le16_to_cpu(mailbox[1]),
5825 le16_to_cpu(mailbox[2]), le16_to_cpu(mailbox[3]));
5826 if (tgt->link_reinit_iocb_pending) {
5827 qlt_send_notify_ack(ha->base_qpair,
5828 (void *)&tgt->link_reinit_iocb,
5830 tgt->link_reinit_iocb_pending = 0;
5835 case MBA_LIP_OCCURRED:
5838 case MBA_RSCN_UPDATE:
5839 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03c,
5840 "qla_target(%d): Async event %#x occurred "
5841 "(m[0]=%x, m[1]=%x, m[2]=%x, m[3]=%x)", vha->vp_idx, code,
5842 le16_to_cpu(mailbox[0]), le16_to_cpu(mailbox[1]),
5843 le16_to_cpu(mailbox[2]), le16_to_cpu(mailbox[3]));
5846 case MBA_REJECTED_FCP_CMD:
5847 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf017,
5848 "qla_target(%d): Async event LS_REJECT occurred (m[0]=%x, m[1]=%x, m[2]=%x, m[3]=%x)",
5850 le16_to_cpu(mailbox[0]), le16_to_cpu(mailbox[1]),
5851 le16_to_cpu(mailbox[2]), le16_to_cpu(mailbox[3]));
5853 if (le16_to_cpu(mailbox[3]) == 1) {
5854 /* exchange starvation. */
5855 vha->hw->exch_starvation++;
5856 if (vha->hw->exch_starvation > 5) {
5857 ql_log(ql_log_warn, vha, 0xd03a,
5858 "Exchange starvation-. Resetting RISC\n");
5860 vha->hw->exch_starvation = 0;
5861 if (IS_P3P_TYPE(vha->hw))
5862 set_bit(FCOE_CTX_RESET_NEEDED,
5865 set_bit(ISP_ABORT_NEEDED,
5867 qla2xxx_wake_dpc(vha);
5872 case MBA_PORT_UPDATE:
5873 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03d,
5874 "qla_target(%d): Port update async event %#x "
5875 "occurred: updating the ports database (m[0]=%x, m[1]=%x, "
5876 "m[2]=%x, m[3]=%x)", vha->vp_idx, code,
5877 le16_to_cpu(mailbox[0]), le16_to_cpu(mailbox[1]),
5878 le16_to_cpu(mailbox[2]), le16_to_cpu(mailbox[3]));
5880 login_code = le16_to_cpu(mailbox[2]);
5881 if (login_code == 0x4) {
5882 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03e,
5883 "Async MB 2: Got PLOGI Complete\n");
5884 vha->hw->exch_starvation = 0;
5885 } else if (login_code == 0x7)
5886 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03f,
5887 "Async MB 2: Port Logged Out\n");
5895 static fc_port_t *qlt_get_port_database(struct scsi_qla_host *vha,
5898 fc_port_t *fcport, *tfcp, *del;
5900 unsigned long flags;
5903 fcport = qla2x00_alloc_fcport(vha, GFP_KERNEL);
5905 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06f,
5906 "qla_target(%d): Allocation of tmp FC port failed",
5911 fcport->loop_id = loop_id;
5913 rc = qla24xx_gpdb_wait(vha, fcport, 0);
5914 if (rc != QLA_SUCCESS) {
5915 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf070,
5916 "qla_target(%d): Failed to retrieve fcport "
5917 "information -- get_port_database() returned %x "
5918 "(loop_id=0x%04x)", vha->vp_idx, rc, loop_id);
5924 spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
5925 tfcp = qla2x00_find_fcport_by_wwpn(vha, fcport->port_name, 1);
5928 tfcp->d_id = fcport->d_id;
5929 tfcp->port_type = fcport->port_type;
5930 tfcp->supported_classes = fcport->supported_classes;
5931 tfcp->flags |= fcport->flags;
5932 tfcp->scan_state = QLA_FCPORT_FOUND;
5937 if (vha->hw->current_topology == ISP_CFG_F)
5938 fcport->flags |= FCF_FABRIC_DEVICE;
5940 list_add_tail(&fcport->list, &vha->vp_fcports);
5941 if (!IS_SW_RESV_ADDR(fcport->d_id))
5942 vha->fcport_count++;
5943 fcport->login_gen++;
5944 fcport->disc_state = DSC_LOGIN_COMPLETE;
5945 fcport->login_succ = 1;
5949 fcport->deleted = 0;
5950 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
5952 switch (vha->host->active_mode) {
5953 case MODE_INITIATOR:
5956 if (!IS_IIDMA_CAPABLE(vha->hw) || !vha->hw->flags.gpsc_supported) {
5957 ql_dbg(ql_dbg_disc, vha, 0x20fe,
5958 "%s %d %8phC post upd_fcport fcp_cnt %d\n",
5959 __func__, __LINE__, fcport->port_name, vha->fcport_count);
5960 qla24xx_post_upd_fcport_work(vha, fcport);
5962 ql_dbg(ql_dbg_disc, vha, 0x20ff,
5963 "%s %d %8phC post gpsc fcp_cnt %d\n",
5964 __func__, __LINE__, fcport->port_name, vha->fcport_count);
5965 qla24xx_post_gpsc_work(vha, fcport);
5975 qla2x00_free_fcport(del);
5980 /* Must be called under tgt_mutex */
5981 static struct fc_port *qlt_make_local_sess(struct scsi_qla_host *vha,
5984 struct fc_port *sess = NULL;
5985 fc_port_t *fcport = NULL;
5986 int rc, global_resets;
5987 uint16_t loop_id = 0;
5989 if ((s_id[0] == 0xFF) && (s_id[1] == 0xFC)) {
5991 * This is Domain Controller, so it should be
5992 * OK to drop SCSI commands from it.
5994 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf042,
5995 "Unable to find initiator with S_ID %x:%x:%x",
5996 s_id[0], s_id[1], s_id[2]);
6000 mutex_lock(&vha->vha_tgt.tgt_mutex);
6004 atomic_read(&vha->vha_tgt.qla_tgt->tgt_global_resets_count);
6006 rc = qla24xx_get_loop_id(vha, s_id, &loop_id);
6008 mutex_unlock(&vha->vha_tgt.tgt_mutex);
6010 ql_log(ql_log_info, vha, 0xf071,
6011 "qla_target(%d): Unable to find "
6012 "initiator with S_ID %x:%x:%x",
6013 vha->vp_idx, s_id[0], s_id[1],
6016 if (rc == -ENOENT) {
6017 qlt_port_logo_t logo;
6018 sid_to_portid(s_id, &logo.id);
6020 qlt_send_first_logo(vha, &logo);
6026 fcport = qlt_get_port_database(vha, loop_id);
6028 mutex_unlock(&vha->vha_tgt.tgt_mutex);
6032 if (global_resets !=
6033 atomic_read(&vha->vha_tgt.qla_tgt->tgt_global_resets_count)) {
6034 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf043,
6035 "qla_target(%d): global reset during session discovery "
6036 "(counter was %d, new %d), retrying", vha->vp_idx,
6038 atomic_read(&vha->vha_tgt.
6039 qla_tgt->tgt_global_resets_count));
6043 sess = qlt_create_sess(vha, fcport, true);
6045 mutex_unlock(&vha->vha_tgt.tgt_mutex);
6050 static void qlt_abort_work(struct qla_tgt *tgt,
6051 struct qla_tgt_sess_work_param *prm)
6053 struct scsi_qla_host *vha = tgt->vha;
6054 struct qla_hw_data *ha = vha->hw;
6055 struct fc_port *sess = NULL;
6056 unsigned long flags = 0, flags2 = 0;
6061 spin_lock_irqsave(&ha->tgt.sess_lock, flags2);
6066 s_id[0] = prm->abts.fcp_hdr_le.s_id[2];
6067 s_id[1] = prm->abts.fcp_hdr_le.s_id[1];
6068 s_id[2] = prm->abts.fcp_hdr_le.s_id[0];
6070 sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha,
6071 (unsigned char *)&be_s_id);
6073 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags2);
6075 sess = qlt_make_local_sess(vha, s_id);
6076 /* sess has got an extra creation ref */
6078 spin_lock_irqsave(&ha->tgt.sess_lock, flags2);
6082 if (sess->deleted) {
6087 if (!kref_get_unless_zero(&sess->sess_kref)) {
6088 ql_dbg(ql_dbg_tgt_tmr, vha, 0xf01c,
6089 "%s: kref_get fail %8phC \n",
6090 __func__, sess->port_name);
6096 rc = __qlt_24xx_handle_abts(vha, &prm->abts, sess);
6097 ha->tgt.tgt_ops->put_sess(sess);
6098 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags2);
6106 ha->tgt.tgt_ops->put_sess(sess);
6107 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags2);
6110 spin_lock_irqsave(&ha->hardware_lock, flags);
6111 qlt_24xx_send_abts_resp(ha->base_qpair, &prm->abts,
6112 FCP_TMF_REJECTED, false);
6113 spin_unlock_irqrestore(&ha->hardware_lock, flags);
6116 static void qlt_tmr_work(struct qla_tgt *tgt,
6117 struct qla_tgt_sess_work_param *prm)
6119 struct atio_from_isp *a = &prm->tm_iocb2;
6120 struct scsi_qla_host *vha = tgt->vha;
6121 struct qla_hw_data *ha = vha->hw;
6122 struct fc_port *sess = NULL;
6123 unsigned long flags;
6124 uint8_t *s_id = NULL; /* to hide compiler warnings */
6130 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
6135 s_id = prm->tm_iocb2.u.isp24.fcp_hdr.s_id;
6136 sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha, s_id);
6138 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
6140 sess = qlt_make_local_sess(vha, s_id);
6141 /* sess has got an extra creation ref */
6143 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
6147 if (sess->deleted) {
6152 if (!kref_get_unless_zero(&sess->sess_kref)) {
6153 ql_dbg(ql_dbg_tgt_tmr, vha, 0xf020,
6154 "%s: kref_get fail %8phC\n",
6155 __func__, sess->port_name);
6162 fn = a->u.isp24.fcp_cmnd.task_mgmt_flags;
6164 scsilun_to_int((struct scsi_lun *)&a->u.isp24.fcp_cmnd.lun);
6166 rc = qlt_issue_task_mgmt(sess, unpacked_lun, fn, iocb, 0);
6167 ha->tgt.tgt_ops->put_sess(sess);
6168 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
6176 ha->tgt.tgt_ops->put_sess(sess);
6177 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
6179 qlt_send_term_exchange(ha->base_qpair, NULL, &prm->tm_iocb2, 1, 0);
6182 static void qlt_sess_work_fn(struct work_struct *work)
6184 struct qla_tgt *tgt = container_of(work, struct qla_tgt, sess_work);
6185 struct scsi_qla_host *vha = tgt->vha;
6186 unsigned long flags;
6188 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf000, "Sess work (tgt %p)", tgt);
6190 spin_lock_irqsave(&tgt->sess_work_lock, flags);
6191 while (!list_empty(&tgt->sess_works_list)) {
6192 struct qla_tgt_sess_work_param *prm = list_entry(
6193 tgt->sess_works_list.next, typeof(*prm),
6194 sess_works_list_entry);
6197 * This work can be scheduled on several CPUs at time, so we
6198 * must delete the entry to eliminate double processing
6200 list_del(&prm->sess_works_list_entry);
6202 spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
6204 switch (prm->type) {
6205 case QLA_TGT_SESS_WORK_ABORT:
6206 qlt_abort_work(tgt, prm);
6208 case QLA_TGT_SESS_WORK_TM:
6209 qlt_tmr_work(tgt, prm);
6216 spin_lock_irqsave(&tgt->sess_work_lock, flags);
6220 spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
6223 /* Must be called under tgt_host_action_mutex */
6224 int qlt_add_target(struct qla_hw_data *ha, struct scsi_qla_host *base_vha)
6226 struct qla_tgt *tgt;
6228 struct qla_qpair_hint *h;
6230 if (!QLA_TGT_MODE_ENABLED())
6233 if (!IS_TGT_MODE_CAPABLE(ha)) {
6234 ql_log(ql_log_warn, base_vha, 0xe070,
6235 "This adapter does not support target mode.\n");
6239 ql_dbg(ql_dbg_tgt, base_vha, 0xe03b,
6240 "Registering target for host %ld(%p).\n", base_vha->host_no, ha);
6242 BUG_ON(base_vha->vha_tgt.qla_tgt != NULL);
6244 tgt = kzalloc(sizeof(struct qla_tgt), GFP_KERNEL);
6246 ql_dbg(ql_dbg_tgt, base_vha, 0xe066,
6247 "Unable to allocate struct qla_tgt\n");
6251 tgt->qphints = kzalloc((ha->max_qpairs + 1) *
6252 sizeof(struct qla_qpair_hint), GFP_KERNEL);
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 = kzalloc(sizeof(struct qla_tgt_vp_map) *
7093 MAX_MULTI_ID_FABRIC, GFP_KERNEL);
7094 if (!ha->tgt.tgt_vp_map)
7097 ha->tgt.atio_ring = dma_alloc_coherent(&ha->pdev->dev,
7098 (ha->tgt.atio_q_length + 1) * sizeof(struct atio_from_isp),
7099 &ha->tgt.atio_dma, GFP_KERNEL);
7100 if (!ha->tgt.atio_ring) {
7101 kfree(ha->tgt.tgt_vp_map);
7108 qlt_mem_free(struct qla_hw_data *ha)
7110 if (!QLA_TGT_MODE_ENABLED())
7113 if (ha->tgt.atio_ring) {
7114 dma_free_coherent(&ha->pdev->dev, (ha->tgt.atio_q_length + 1) *
7115 sizeof(struct atio_from_isp), ha->tgt.atio_ring,
7118 kfree(ha->tgt.tgt_vp_map);
7121 /* vport_slock to be held by the caller */
7123 qlt_update_vp_map(struct scsi_qla_host *vha, int cmd)
7129 if (!QLA_TGT_MODE_ENABLED())
7132 key = vha->d_id.b24;
7136 vha->hw->tgt.tgt_vp_map[vha->vp_idx].vha = vha;
7139 slot = btree_lookup32(&vha->hw->tgt.host_map, key);
7141 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf018,
7142 "Save vha in host_map %p %06x\n", vha, key);
7143 rc = btree_insert32(&vha->hw->tgt.host_map,
7144 key, vha, GFP_ATOMIC);
7146 ql_log(ql_log_info, vha, 0xd03e,
7147 "Unable to insert s_id into host_map: %06x\n",
7151 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf019,
7152 "replace existing vha in host_map %p %06x\n", vha, key);
7153 btree_update32(&vha->hw->tgt.host_map, key, vha);
7156 vha->hw->tgt.tgt_vp_map[vha->vp_idx].vha = NULL;
7159 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01a,
7160 "clear vha in host_map %p %06x\n", vha, key);
7161 slot = btree_lookup32(&vha->hw->tgt.host_map, key);
7163 btree_remove32(&vha->hw->tgt.host_map, key);
7169 void qlt_update_host_map(struct scsi_qla_host *vha, port_id_t id)
7172 if (!vha->d_id.b24) {
7174 qlt_update_vp_map(vha, SET_AL_PA);
7175 } else if (vha->d_id.b24 != id.b24) {
7176 qlt_update_vp_map(vha, RESET_AL_PA);
7178 qlt_update_vp_map(vha, SET_AL_PA);
7182 static int __init qlt_parse_ini_mode(void)
7184 if (strcasecmp(qlini_mode, QLA2XXX_INI_MODE_STR_EXCLUSIVE) == 0)
7185 ql2x_ini_mode = QLA2XXX_INI_MODE_EXCLUSIVE;
7186 else if (strcasecmp(qlini_mode, QLA2XXX_INI_MODE_STR_DISABLED) == 0)
7187 ql2x_ini_mode = QLA2XXX_INI_MODE_DISABLED;
7188 else if (strcasecmp(qlini_mode, QLA2XXX_INI_MODE_STR_ENABLED) == 0)
7189 ql2x_ini_mode = QLA2XXX_INI_MODE_ENABLED;
7190 else if (strcasecmp(qlini_mode, QLA2XXX_INI_MODE_STR_DUAL) == 0)
7191 ql2x_ini_mode = QLA2XXX_INI_MODE_DUAL;
7198 int __init qlt_init(void)
7202 if (!qlt_parse_ini_mode()) {
7203 ql_log(ql_log_fatal, NULL, 0xe06b,
7204 "qlt_parse_ini_mode() failed\n");
7208 if (!QLA_TGT_MODE_ENABLED())
7211 qla_tgt_mgmt_cmd_cachep = kmem_cache_create("qla_tgt_mgmt_cmd_cachep",
7212 sizeof(struct qla_tgt_mgmt_cmd), __alignof__(struct
7213 qla_tgt_mgmt_cmd), 0, NULL);
7214 if (!qla_tgt_mgmt_cmd_cachep) {
7215 ql_log(ql_log_fatal, NULL, 0xd04b,
7216 "kmem_cache_create for qla_tgt_mgmt_cmd_cachep failed\n");
7220 qla_tgt_plogi_cachep = kmem_cache_create("qla_tgt_plogi_cachep",
7221 sizeof(struct qlt_plogi_ack_t), __alignof__(struct qlt_plogi_ack_t),
7224 if (!qla_tgt_plogi_cachep) {
7225 ql_log(ql_log_fatal, NULL, 0xe06d,
7226 "kmem_cache_create for qla_tgt_plogi_cachep failed\n");
7228 goto out_mgmt_cmd_cachep;
7231 qla_tgt_mgmt_cmd_mempool = mempool_create(25, mempool_alloc_slab,
7232 mempool_free_slab, qla_tgt_mgmt_cmd_cachep);
7233 if (!qla_tgt_mgmt_cmd_mempool) {
7234 ql_log(ql_log_fatal, NULL, 0xe06e,
7235 "mempool_create for qla_tgt_mgmt_cmd_mempool failed\n");
7237 goto out_plogi_cachep;
7240 qla_tgt_wq = alloc_workqueue("qla_tgt_wq", 0, 0);
7242 ql_log(ql_log_fatal, NULL, 0xe06f,
7243 "alloc_workqueue for qla_tgt_wq failed\n");
7245 goto out_cmd_mempool;
7248 * Return 1 to signal that initiator-mode is being disabled
7250 return (ql2x_ini_mode == QLA2XXX_INI_MODE_DISABLED) ? 1 : 0;
7253 mempool_destroy(qla_tgt_mgmt_cmd_mempool);
7255 kmem_cache_destroy(qla_tgt_plogi_cachep);
7256 out_mgmt_cmd_cachep:
7257 kmem_cache_destroy(qla_tgt_mgmt_cmd_cachep);
7263 if (!QLA_TGT_MODE_ENABLED())
7266 destroy_workqueue(qla_tgt_wq);
7267 mempool_destroy(qla_tgt_mgmt_cmd_mempool);
7268 kmem_cache_destroy(qla_tgt_plogi_cachep);
7269 kmem_cache_destroy(qla_tgt_mgmt_cmd_cachep);