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 *,
144 static int qlt_check_reserve_free_req(struct qla_qpair *qpair, uint32_t);
145 static inline uint32_t qlt_make_handle(struct qla_qpair *);
150 static struct kmem_cache *qla_tgt_mgmt_cmd_cachep;
151 struct kmem_cache *qla_tgt_plogi_cachep;
152 static mempool_t *qla_tgt_mgmt_cmd_mempool;
153 static struct workqueue_struct *qla_tgt_wq;
154 static DEFINE_MUTEX(qla_tgt_mutex);
155 static LIST_HEAD(qla_tgt_glist);
157 static const char *prot_op_str(u32 prot_op)
160 case TARGET_PROT_NORMAL: return "NORMAL";
161 case TARGET_PROT_DIN_INSERT: return "DIN_INSERT";
162 case TARGET_PROT_DOUT_INSERT: return "DOUT_INSERT";
163 case TARGET_PROT_DIN_STRIP: return "DIN_STRIP";
164 case TARGET_PROT_DOUT_STRIP: return "DOUT_STRIP";
165 case TARGET_PROT_DIN_PASS: return "DIN_PASS";
166 case TARGET_PROT_DOUT_PASS: return "DOUT_PASS";
167 default: return "UNKNOWN";
171 /* This API intentionally takes dest as a parameter, rather than returning
172 * int value to avoid caller forgetting to issue wmb() after the store */
173 void qlt_do_generation_tick(struct scsi_qla_host *vha, int *dest)
175 scsi_qla_host_t *base_vha = pci_get_drvdata(vha->hw->pdev);
176 *dest = atomic_inc_return(&base_vha->generation_tick);
181 /* Might release hw lock, then reaquire!! */
182 static inline int qlt_issue_marker(struct scsi_qla_host *vha, int vha_locked)
184 /* Send marker if required */
185 if (unlikely(vha->marker_needed != 0)) {
186 int rc = qla2x00_issue_marker(vha, vha_locked);
187 if (rc != QLA_SUCCESS) {
188 ql_dbg(ql_dbg_tgt, vha, 0xe03d,
189 "qla_target(%d): issue_marker() failed\n",
198 struct scsi_qla_host *qlt_find_host_by_d_id(struct scsi_qla_host *vha,
201 struct scsi_qla_host *host;
204 if ((vha->d_id.b.area == d_id[1]) && (vha->d_id.b.domain == d_id[0]) &&
205 (vha->d_id.b.al_pa == d_id[2]))
208 key = (uint32_t)d_id[0] << 16;
209 key |= (uint32_t)d_id[1] << 8;
210 key |= (uint32_t)d_id[2];
212 host = btree_lookup32(&vha->hw->tgt.host_map, key);
214 ql_dbg(ql_dbg_tgt_mgt + ql_dbg_verbose, vha, 0xf005,
215 "Unable to find host %06x\n", key);
221 struct scsi_qla_host *qlt_find_host_by_vp_idx(struct scsi_qla_host *vha,
224 struct qla_hw_data *ha = vha->hw;
226 if (vha->vp_idx == vp_idx)
229 BUG_ON(ha->tgt.tgt_vp_map == NULL);
230 if (likely(test_bit(vp_idx, ha->vp_idx_map)))
231 return ha->tgt.tgt_vp_map[vp_idx].vha;
236 static inline void qlt_incr_num_pend_cmds(struct scsi_qla_host *vha)
240 spin_lock_irqsave(&vha->hw->tgt.q_full_lock, flags);
242 vha->hw->tgt.num_pend_cmds++;
243 if (vha->hw->tgt.num_pend_cmds > vha->qla_stats.stat_max_pend_cmds)
244 vha->qla_stats.stat_max_pend_cmds =
245 vha->hw->tgt.num_pend_cmds;
246 spin_unlock_irqrestore(&vha->hw->tgt.q_full_lock, flags);
248 static inline void qlt_decr_num_pend_cmds(struct scsi_qla_host *vha)
252 spin_lock_irqsave(&vha->hw->tgt.q_full_lock, flags);
253 vha->hw->tgt.num_pend_cmds--;
254 spin_unlock_irqrestore(&vha->hw->tgt.q_full_lock, flags);
258 static void qlt_queue_unknown_atio(scsi_qla_host_t *vha,
259 struct atio_from_isp *atio, uint8_t ha_locked)
261 struct qla_tgt_sess_op *u;
262 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
266 ql_dbg(ql_dbg_async, vha, 0x502c,
267 "qla_target(%d): dropping unknown ATIO_TYPE7, because tgt is being stopped",
272 u = kzalloc(sizeof(*u), GFP_ATOMIC);
277 memcpy(&u->atio, atio, sizeof(*atio));
278 INIT_LIST_HEAD(&u->cmd_list);
280 spin_lock_irqsave(&vha->cmd_list_lock, flags);
281 list_add_tail(&u->cmd_list, &vha->unknown_atio_list);
282 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
284 schedule_delayed_work(&vha->unknown_atio_work, 1);
290 qlt_send_term_exchange(vha->hw->base_qpair, NULL, atio, ha_locked, 0);
294 static void qlt_try_to_dequeue_unknown_atios(struct scsi_qla_host *vha,
297 struct qla_tgt_sess_op *u, *t;
298 scsi_qla_host_t *host;
299 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
303 list_for_each_entry_safe(u, t, &vha->unknown_atio_list, cmd_list) {
305 ql_dbg(ql_dbg_async, vha, 0x502e,
306 "Freeing unknown %s %p, because of Abort\n",
308 qlt_send_term_exchange(vha->hw->base_qpair, NULL,
309 &u->atio, ha_locked, 0);
313 host = qlt_find_host_by_d_id(vha, u->atio.u.isp24.fcp_hdr.d_id);
315 ql_dbg(ql_dbg_async + ql_dbg_verbose, vha, 0x502f,
316 "Requeuing unknown ATIO_TYPE7 %p\n", u);
317 qlt_24xx_atio_pkt(host, &u->atio, ha_locked);
318 } else if (tgt->tgt_stop) {
319 ql_dbg(ql_dbg_async + ql_dbg_verbose, vha, 0x503a,
320 "Freeing unknown %s %p, because tgt is being stopped\n",
322 qlt_send_term_exchange(vha->hw->base_qpair, NULL,
323 &u->atio, ha_locked, 0);
325 ql_dbg(ql_dbg_async + ql_dbg_verbose, vha, 0x503d,
326 "Reschedule u %p, vha %p, host %p\n", u, vha, host);
329 schedule_delayed_work(&vha->unknown_atio_work,
336 spin_lock_irqsave(&vha->cmd_list_lock, flags);
337 list_del(&u->cmd_list);
338 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
343 void qlt_unknown_atio_work_fn(struct work_struct *work)
345 struct scsi_qla_host *vha = container_of(to_delayed_work(work),
346 struct scsi_qla_host, unknown_atio_work);
348 qlt_try_to_dequeue_unknown_atios(vha, 0);
351 static bool qlt_24xx_atio_pkt_all_vps(struct scsi_qla_host *vha,
352 struct atio_from_isp *atio, uint8_t ha_locked)
354 ql_dbg(ql_dbg_tgt, vha, 0xe072,
355 "%s: qla_target(%d): type %x ox_id %04x\n",
356 __func__, vha->vp_idx, atio->u.raw.entry_type,
357 be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id));
359 switch (atio->u.raw.entry_type) {
362 struct scsi_qla_host *host = qlt_find_host_by_d_id(vha,
363 atio->u.isp24.fcp_hdr.d_id);
364 if (unlikely(NULL == host)) {
365 ql_dbg(ql_dbg_tgt, vha, 0xe03e,
366 "qla_target(%d): Received ATIO_TYPE7 "
367 "with unknown d_id %x:%x:%x\n", vha->vp_idx,
368 atio->u.isp24.fcp_hdr.d_id[0],
369 atio->u.isp24.fcp_hdr.d_id[1],
370 atio->u.isp24.fcp_hdr.d_id[2]);
373 qlt_queue_unknown_atio(vha, atio, ha_locked);
376 if (unlikely(!list_empty(&vha->unknown_atio_list)))
377 qlt_try_to_dequeue_unknown_atios(vha, ha_locked);
379 qlt_24xx_atio_pkt(host, atio, ha_locked);
383 case IMMED_NOTIFY_TYPE:
385 struct scsi_qla_host *host = vha;
386 struct imm_ntfy_from_isp *entry =
387 (struct imm_ntfy_from_isp *)atio;
389 qlt_issue_marker(vha, ha_locked);
391 if ((entry->u.isp24.vp_index != 0xFF) &&
392 (entry->u.isp24.nport_handle != 0xFFFF)) {
393 host = qlt_find_host_by_vp_idx(vha,
394 entry->u.isp24.vp_index);
395 if (unlikely(!host)) {
396 ql_dbg(ql_dbg_tgt, vha, 0xe03f,
397 "qla_target(%d): Received "
398 "ATIO (IMMED_NOTIFY_TYPE) "
399 "with unknown vp_index %d\n",
400 vha->vp_idx, entry->u.isp24.vp_index);
404 qlt_24xx_atio_pkt(host, atio, ha_locked);
408 case VP_RPT_ID_IOCB_TYPE:
409 qla24xx_report_id_acquisition(vha,
410 (struct vp_rpt_id_entry_24xx *)atio);
415 struct abts_recv_from_24xx *entry =
416 (struct abts_recv_from_24xx *)atio;
417 struct scsi_qla_host *host = qlt_find_host_by_vp_idx(vha,
421 if (unlikely(!host)) {
422 ql_dbg(ql_dbg_tgt, vha, 0xe00a,
423 "qla_target(%d): Response pkt (ABTS_RECV_24XX) "
424 "received, with unknown vp_index %d\n",
425 vha->vp_idx, entry->vp_index);
429 spin_lock_irqsave(&host->hw->hardware_lock, flags);
430 qlt_24xx_handle_abts(host, (struct abts_recv_from_24xx *)atio);
432 spin_unlock_irqrestore(&host->hw->hardware_lock, flags);
436 /* case PUREX_IOCB_TYPE: ql2xmvasynctoatio */
439 ql_dbg(ql_dbg_tgt, vha, 0xe040,
440 "qla_target(%d): Received unknown ATIO atio "
441 "type %x\n", vha->vp_idx, atio->u.raw.entry_type);
448 void qlt_response_pkt_all_vps(struct scsi_qla_host *vha,
449 struct rsp_que *rsp, response_t *pkt)
451 switch (pkt->entry_type) {
453 ql_dbg(ql_dbg_tgt, vha, 0xe073,
454 "qla_target(%d):%s: CRC2 Response pkt\n",
455 vha->vp_idx, __func__);
459 struct ctio7_from_24xx *entry = (struct ctio7_from_24xx *)pkt;
460 struct scsi_qla_host *host = qlt_find_host_by_vp_idx(vha,
462 if (unlikely(!host)) {
463 ql_dbg(ql_dbg_tgt, vha, 0xe041,
464 "qla_target(%d): Response pkt (CTIO_TYPE7) "
465 "received, with unknown vp_index %d\n",
466 vha->vp_idx, entry->vp_index);
469 qlt_response_pkt(host, rsp, pkt);
473 case IMMED_NOTIFY_TYPE:
475 struct scsi_qla_host *host = vha;
476 struct imm_ntfy_from_isp *entry =
477 (struct imm_ntfy_from_isp *)pkt;
479 host = qlt_find_host_by_vp_idx(vha, entry->u.isp24.vp_index);
480 if (unlikely(!host)) {
481 ql_dbg(ql_dbg_tgt, vha, 0xe042,
482 "qla_target(%d): Response pkt (IMMED_NOTIFY_TYPE) "
483 "received, with unknown vp_index %d\n",
484 vha->vp_idx, entry->u.isp24.vp_index);
487 qlt_response_pkt(host, rsp, pkt);
491 case NOTIFY_ACK_TYPE:
493 struct scsi_qla_host *host = vha;
494 struct nack_to_isp *entry = (struct nack_to_isp *)pkt;
496 if (0xFF != entry->u.isp24.vp_index) {
497 host = qlt_find_host_by_vp_idx(vha,
498 entry->u.isp24.vp_index);
499 if (unlikely(!host)) {
500 ql_dbg(ql_dbg_tgt, vha, 0xe043,
501 "qla_target(%d): Response "
502 "pkt (NOTIFY_ACK_TYPE) "
503 "received, with unknown "
504 "vp_index %d\n", vha->vp_idx,
505 entry->u.isp24.vp_index);
509 qlt_response_pkt(host, rsp, pkt);
515 struct abts_recv_from_24xx *entry =
516 (struct abts_recv_from_24xx *)pkt;
517 struct scsi_qla_host *host = qlt_find_host_by_vp_idx(vha,
519 if (unlikely(!host)) {
520 ql_dbg(ql_dbg_tgt, vha, 0xe044,
521 "qla_target(%d): Response pkt "
522 "(ABTS_RECV_24XX) received, with unknown "
523 "vp_index %d\n", vha->vp_idx, entry->vp_index);
526 qlt_response_pkt(host, rsp, pkt);
532 struct abts_resp_to_24xx *entry =
533 (struct abts_resp_to_24xx *)pkt;
534 struct scsi_qla_host *host = qlt_find_host_by_vp_idx(vha,
536 if (unlikely(!host)) {
537 ql_dbg(ql_dbg_tgt, vha, 0xe045,
538 "qla_target(%d): Response pkt "
539 "(ABTS_RECV_24XX) received, with unknown "
540 "vp_index %d\n", vha->vp_idx, entry->vp_index);
543 qlt_response_pkt(host, rsp, pkt);
547 qlt_response_pkt(vha, rsp, pkt);
554 * All qlt_plogi_ack_t operations are protected by hardware_lock
556 static int qla24xx_post_nack_work(struct scsi_qla_host *vha, fc_port_t *fcport,
557 struct imm_ntfy_from_isp *ntfy, int type)
559 struct qla_work_evt *e;
560 e = qla2x00_alloc_work(vha, QLA_EVT_NACK);
562 return QLA_FUNCTION_FAILED;
564 e->u.nack.fcport = fcport;
565 e->u.nack.type = type;
566 memcpy(e->u.nack.iocb, ntfy, sizeof(struct imm_ntfy_from_isp));
567 return qla2x00_post_work(vha, e);
571 void qla2x00_async_nack_sp_done(void *s, int res)
573 struct srb *sp = (struct srb *)s;
574 struct scsi_qla_host *vha = sp->vha;
577 ql_dbg(ql_dbg_disc, vha, 0x20f2,
578 "Async done-%s res %x %8phC type %d\n",
579 sp->name, res, sp->fcport->port_name, sp->type);
581 spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
582 sp->fcport->flags &= ~FCF_ASYNC_SENT;
583 sp->fcport->chip_reset = vha->hw->base_qpair->chip_reset;
587 sp->fcport->login_gen++;
588 sp->fcport->fw_login_state = DSC_LS_PLOGI_COMP;
589 sp->fcport->logout_on_delete = 1;
590 sp->fcport->plogi_nack_done_deadline = jiffies + HZ;
591 sp->fcport->send_els_logo = 0;
595 sp->fcport->fw_login_state = DSC_LS_PRLI_COMP;
596 sp->fcport->deleted = 0;
597 sp->fcport->send_els_logo = 0;
599 if (!sp->fcport->login_succ &&
600 !IS_SW_RESV_ADDR(sp->fcport->d_id)) {
601 sp->fcport->login_succ = 1;
604 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
605 qla24xx_sched_upd_fcport(sp->fcport);
606 spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
608 sp->fcport->login_retry = 0;
609 sp->fcport->disc_state = DSC_LOGIN_COMPLETE;
610 sp->fcport->deleted = 0;
611 sp->fcport->logout_on_delete = 1;
616 sp->fcport->login_gen++;
617 sp->fcport->fw_login_state = DSC_LS_PORT_UNAVAIL;
618 qlt_logo_completion_handler(sp->fcport, MBS_COMMAND_COMPLETE);
621 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
626 int qla24xx_async_notify_ack(scsi_qla_host_t *vha, fc_port_t *fcport,
627 struct imm_ntfy_from_isp *ntfy, int type)
629 int rval = QLA_FUNCTION_FAILED;
633 fcport->flags |= FCF_ASYNC_SENT;
636 fcport->fw_login_state = DSC_LS_PLOGI_PEND;
640 fcport->fw_login_state = DSC_LS_PRLI_PEND;
645 fcport->fw_login_state = DSC_LS_LOGO_PEND;
650 sp = qla2x00_get_sp(vha, fcport, GFP_ATOMIC);
657 sp->u.iocb_cmd.timeout = qla2x00_async_iocb_timeout;
658 qla2x00_init_timer(sp, qla2x00_get_async_timeout(vha)+2);
660 sp->u.iocb_cmd.u.nack.ntfy = ntfy;
661 sp->done = qla2x00_async_nack_sp_done;
663 rval = qla2x00_start_sp(sp);
664 if (rval != QLA_SUCCESS)
667 ql_dbg(ql_dbg_disc, vha, 0x20f4,
668 "Async-%s %8phC hndl %x %s\n",
669 sp->name, fcport->port_name, sp->handle, c);
676 fcport->flags &= ~FCF_ASYNC_SENT;
680 void qla24xx_do_nack_work(struct scsi_qla_host *vha, struct qla_work_evt *e)
685 switch (e->u.nack.type) {
687 mutex_lock(&vha->vha_tgt.tgt_mutex);
688 t = qlt_create_sess(vha, e->u.nack.fcport, 0);
689 mutex_unlock(&vha->vha_tgt.tgt_mutex);
691 ql_log(ql_log_info, vha, 0xd034,
692 "%s create sess success %p", __func__, t);
693 spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
694 /* create sess has an extra kref */
695 vha->hw->tgt.tgt_ops->put_sess(e->u.nack.fcport);
696 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
700 qla24xx_async_notify_ack(vha, e->u.nack.fcport,
701 (struct imm_ntfy_from_isp*)e->u.nack.iocb, e->u.nack.type);
704 void qla24xx_delete_sess_fn(struct work_struct *work)
706 fc_port_t *fcport = container_of(work, struct fc_port, del_work);
707 struct qla_hw_data *ha = fcport->vha->hw;
710 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
712 if (fcport->se_sess) {
713 ha->tgt.tgt_ops->shutdown_sess(fcport);
714 ha->tgt.tgt_ops->put_sess(fcport);
716 qlt_unreg_sess(fcport);
718 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
722 * Called from qla2x00_reg_remote_port()
724 void qlt_fc_port_added(struct scsi_qla_host *vha, fc_port_t *fcport)
726 struct qla_hw_data *ha = vha->hw;
727 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
728 struct fc_port *sess = fcport;
731 if (!vha->hw->tgt.tgt_ops)
734 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
736 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
740 if (fcport->disc_state == DSC_DELETE_PEND) {
741 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
745 if (!sess->se_sess) {
746 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
748 mutex_lock(&vha->vha_tgt.tgt_mutex);
749 sess = qlt_create_sess(vha, fcport, false);
750 mutex_unlock(&vha->vha_tgt.tgt_mutex);
752 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
754 if (fcport->fw_login_state == DSC_LS_PRLI_COMP) {
755 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
759 if (!kref_get_unless_zero(&sess->sess_kref)) {
760 ql_dbg(ql_dbg_disc, vha, 0x2107,
761 "%s: kref_get fail sess %8phC \n",
762 __func__, sess->port_name);
763 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
767 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04c,
768 "qla_target(%u): %ssession for port %8phC "
769 "(loop ID %d) reappeared\n", vha->vp_idx,
770 sess->local ? "local " : "", sess->port_name, sess->loop_id);
772 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf007,
773 "Reappeared sess %p\n", sess);
775 ha->tgt.tgt_ops->update_sess(sess, fcport->d_id,
777 (fcport->flags & FCF_CONF_COMP_SUPPORTED));
780 if (sess && sess->local) {
781 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04d,
782 "qla_target(%u): local session for "
783 "port %8phC (loop ID %d) became global\n", vha->vp_idx,
784 fcport->port_name, sess->loop_id);
787 ha->tgt.tgt_ops->put_sess(sess);
788 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
792 * This is a zero-base ref-counting solution, since hardware_lock
793 * guarantees that ref_count is not modified concurrently.
794 * Upon successful return content of iocb is undefined
796 static struct qlt_plogi_ack_t *
797 qlt_plogi_ack_find_add(struct scsi_qla_host *vha, port_id_t *id,
798 struct imm_ntfy_from_isp *iocb)
800 struct qlt_plogi_ack_t *pla;
802 list_for_each_entry(pla, &vha->plogi_ack_list, list) {
803 if (pla->id.b24 == id->b24) {
804 ql_dbg(ql_dbg_disc + ql_dbg_verbose, vha, 0x210d,
805 "%s %d %8phC Term INOT due to new INOT",
807 pla->iocb.u.isp24.port_name);
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;
986 qlt_send_first_logo(vha, &logo);
987 sess->send_els_logo = 0;
990 if (sess->logout_on_delete && sess->loop_id != FC_NO_LOOP_ID) {
995 (own->iocb.u.isp24.status_subcode == ELS_PLOGI))) {
996 rc = qla2x00_post_async_logout_work(vha, sess,
998 if (rc != QLA_SUCCESS)
999 ql_log(ql_log_warn, vha, 0xf085,
1000 "Schedule logo failed sess %p rc %d\n",
1003 logout_started = true;
1004 } else if (own && (own->iocb.u.isp24.status_subcode ==
1005 ELS_PRLI) && ha->flags.rida_fmt2) {
1006 rc = qla2x00_post_async_prlo_work(vha, sess,
1008 if (rc != QLA_SUCCESS)
1009 ql_log(ql_log_warn, vha, 0xf085,
1010 "Schedule PRLO failed sess %p rc %d\n",
1013 logout_started = true;
1019 * Release the target session for FC Nexus from fabric module code.
1021 if (sess->se_sess != NULL)
1022 ha->tgt.tgt_ops->free_session(sess);
1024 if (logout_started) {
1025 bool traced = false;
1027 while (!READ_ONCE(sess->logout_completed)) {
1029 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf086,
1030 "%s: waiting for sess %p logout\n",
1037 ql_dbg(ql_dbg_disc, vha, 0xf087,
1038 "%s: sess %p logout completed\n", __func__, sess);
1041 if (sess->logo_ack_needed) {
1042 sess->logo_ack_needed = 0;
1043 qla24xx_async_notify_ack(vha, sess,
1044 (struct imm_ntfy_from_isp *)sess->iocb, SRB_NACK_LOGO);
1047 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
1048 if (sess->se_sess) {
1049 sess->se_sess = NULL;
1050 if (tgt && !IS_SW_RESV_ADDR(sess->d_id))
1054 sess->disc_state = DSC_DELETED;
1055 sess->fw_login_state = DSC_LS_PORT_UNAVAIL;
1056 sess->deleted = QLA_SESS_DELETED;
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;
1076 own = sess->plogi_link[QLT_PLOGI_LINK_SAME_WWN];
1080 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf099,
1081 "se_sess %p / sess %p port %8phC is gone,"
1082 " %s (ref=%d), releasing PLOGI for %8phC (ref=%d)\n",
1083 sess->se_sess, sess, sess->port_name,
1084 own ? "releasing own PLOGI" : "no own PLOGI pending",
1085 own ? own->ref_count : -1,
1086 iocb->u.isp24.port_name, con->ref_count);
1087 qlt_plogi_ack_unref(vha, con);
1088 sess->plogi_link[QLT_PLOGI_LINK_CONFLICT] = NULL;
1090 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf09a,
1091 "se_sess %p / sess %p port %8phC is gone, %s (ref=%d)\n",
1092 sess->se_sess, sess, sess->port_name,
1093 own ? "releasing own PLOGI" :
1094 "no own PLOGI pending",
1095 own ? own->ref_count : -1);
1099 sess->fw_login_state = DSC_LS_PLOGI_PEND;
1100 qlt_plogi_ack_unref(vha, own);
1101 sess->plogi_link[QLT_PLOGI_LINK_SAME_WWN] = NULL;
1105 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
1107 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf001,
1108 "Unregistration of sess %p %8phC finished fcp_cnt %d\n",
1109 sess, sess->port_name, vha->fcport_count);
1111 if (tgt && (tgt->sess_count == 0))
1112 wake_up_all(&tgt->waitQ);
1114 if (vha->fcport_count == 0)
1115 wake_up_all(&vha->fcport_waitQ);
1117 base_vha = pci_get_drvdata(ha->pdev);
1119 sess->free_pending = 0;
1121 if (test_bit(PFLG_DRIVER_REMOVING, &base_vha->pci_flags))
1124 if ((!tgt || !tgt->tgt_stop) && !LOOP_TRANSITION(vha)) {
1125 switch (vha->host->active_mode) {
1126 case MODE_INITIATOR:
1128 set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
1129 qla2xxx_wake_dpc(vha);
1139 /* ha->tgt.sess_lock supposed to be held on entry */
1140 void qlt_unreg_sess(struct fc_port *sess)
1142 struct scsi_qla_host *vha = sess->vha;
1143 unsigned long flags;
1145 ql_dbg(ql_dbg_disc, sess->vha, 0x210a,
1146 "%s sess %p for deletion %8phC\n",
1147 __func__, sess, sess->port_name);
1149 spin_lock_irqsave(&sess->vha->work_lock, flags);
1150 if (sess->free_pending) {
1151 spin_unlock_irqrestore(&sess->vha->work_lock, flags);
1154 sess->free_pending = 1;
1155 spin_unlock_irqrestore(&sess->vha->work_lock, flags);
1158 vha->hw->tgt.tgt_ops->clear_nacl_from_fcport_map(sess);
1160 qla2x00_mark_device_lost(vha, sess, 0, 0);
1162 sess->deleted = QLA_SESS_DELETION_IN_PROGRESS;
1163 sess->disc_state = DSC_DELETE_PEND;
1164 sess->last_rscn_gen = sess->rscn_gen;
1165 sess->last_login_gen = sess->login_gen;
1167 if (sess->nvme_flag & NVME_FLAG_REGISTERED &&
1168 !(sess->nvme_flag & NVME_FLAG_DELETING)) {
1169 sess->nvme_flag |= NVME_FLAG_DELETING;
1170 schedule_work(&sess->nvme_del_work);
1172 INIT_WORK(&sess->free_work, qlt_free_session_done);
1173 schedule_work(&sess->free_work);
1176 EXPORT_SYMBOL(qlt_unreg_sess);
1178 static int qlt_reset(struct scsi_qla_host *vha, void *iocb, int mcmd)
1180 struct qla_hw_data *ha = vha->hw;
1181 struct fc_port *sess = NULL;
1184 struct imm_ntfy_from_isp *n = (struct imm_ntfy_from_isp *)iocb;
1185 unsigned long flags;
1187 loop_id = le16_to_cpu(n->u.isp24.nport_handle);
1188 if (loop_id == 0xFFFF) {
1190 atomic_inc(&vha->vha_tgt.qla_tgt->tgt_global_resets_count);
1191 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
1192 qlt_clear_tgt_db(vha->vha_tgt.qla_tgt);
1193 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
1195 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
1196 sess = ha->tgt.tgt_ops->find_sess_by_loop_id(vha, loop_id);
1197 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
1200 ql_dbg(ql_dbg_tgt, vha, 0xe000,
1201 "Using sess for qla_tgt_reset: %p\n", sess);
1207 ql_dbg(ql_dbg_tgt, vha, 0xe047,
1208 "scsi(%ld): resetting (session %p from port %8phC mcmd %x, "
1209 "loop_id %d)\n", vha->host_no, sess, sess->port_name,
1212 return qlt_issue_task_mgmt(sess, 0, mcmd, iocb, QLA24XX_MGMT_SEND_NACK);
1215 static void qla24xx_chk_fcp_state(struct fc_port *sess)
1217 if (sess->chip_reset != sess->vha->hw->base_qpair->chip_reset) {
1218 sess->logout_on_delete = 0;
1219 sess->logo_ack_needed = 0;
1220 sess->fw_login_state = DSC_LS_PORT_UNAVAIL;
1221 sess->scan_state = 0;
1225 void qlt_schedule_sess_for_deletion(struct fc_port *sess)
1227 struct qla_tgt *tgt = sess->tgt;
1228 unsigned long flags;
1231 switch (sess->disc_state) {
1232 case DSC_DELETE_PEND:
1235 if (tgt && tgt->tgt_stop && (tgt->sess_count == 0))
1236 wake_up_all(&tgt->waitQ);
1237 if (sess->vha->fcport_count == 0)
1238 wake_up_all(&sess->vha->fcport_waitQ);
1240 if (!sess->plogi_link[QLT_PLOGI_LINK_SAME_WWN] &&
1241 !sess->plogi_link[QLT_PLOGI_LINK_CONFLICT])
1244 case DSC_UPD_FCPORT:
1246 * This port is not done reporting to upper layer.
1249 sess->next_disc_state = DSC_DELETE_PEND;
1250 sec = jiffies_to_msecs(jiffies -
1251 sess->jiffies_at_registration)/1000;
1252 if (sess->sec_since_registration < sec && sec && !(sec % 5)) {
1253 sess->sec_since_registration = sec;
1254 ql_dbg(ql_dbg_disc, sess->vha, 0xffff,
1255 "%s %8phC : Slow Rport registration(%d Sec)\n",
1256 __func__, sess->port_name, sec);
1263 spin_lock_irqsave(&sess->vha->work_lock, flags);
1264 if (sess->deleted == QLA_SESS_DELETION_IN_PROGRESS) {
1265 spin_unlock_irqrestore(&sess->vha->work_lock, flags);
1268 sess->deleted = QLA_SESS_DELETION_IN_PROGRESS;
1269 spin_unlock_irqrestore(&sess->vha->work_lock, flags);
1271 sess->disc_state = DSC_DELETE_PEND;
1273 qla24xx_chk_fcp_state(sess);
1275 ql_dbg(ql_dbg_tgt, sess->vha, 0xe001,
1276 "Scheduling sess %p for deletion %8phC\n",
1277 sess, sess->port_name);
1279 INIT_WORK(&sess->del_work, qla24xx_delete_sess_fn);
1280 WARN_ON(!queue_work(sess->vha->hw->wq, &sess->del_work));
1283 static void qlt_clear_tgt_db(struct qla_tgt *tgt)
1285 struct fc_port *sess;
1286 scsi_qla_host_t *vha = tgt->vha;
1288 list_for_each_entry(sess, &vha->vp_fcports, list) {
1290 qlt_schedule_sess_for_deletion(sess);
1293 /* At this point tgt could be already dead */
1296 static int qla24xx_get_loop_id(struct scsi_qla_host *vha, const uint8_t *s_id,
1299 struct qla_hw_data *ha = vha->hw;
1300 dma_addr_t gid_list_dma;
1301 struct gid_list_info *gid_list;
1306 gid_list = dma_alloc_coherent(&ha->pdev->dev, qla2x00_gid_list_size(ha),
1307 &gid_list_dma, GFP_KERNEL);
1309 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf044,
1310 "qla_target(%d): DMA Alloc failed of %u\n",
1311 vha->vp_idx, qla2x00_gid_list_size(ha));
1315 /* Get list of logged in devices */
1316 rc = qla24xx_gidlist_wait(vha, gid_list, gid_list_dma, &entries);
1317 if (rc != QLA_SUCCESS) {
1318 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf045,
1319 "qla_target(%d): get_id_list() failed: %x\n",
1322 goto out_free_id_list;
1325 id_iter = (char *)gid_list;
1327 for (i = 0; i < entries; i++) {
1328 struct gid_list_info *gid = (struct gid_list_info *)id_iter;
1329 if ((gid->al_pa == s_id[2]) &&
1330 (gid->area == s_id[1]) &&
1331 (gid->domain == s_id[0])) {
1332 *loop_id = le16_to_cpu(gid->loop_id);
1336 id_iter += ha->gid_list_info_size;
1340 dma_free_coherent(&ha->pdev->dev, qla2x00_gid_list_size(ha),
1341 gid_list, gid_list_dma);
1346 * Adds an extra ref to allow to drop hw lock after adding sess to the list.
1347 * Caller must put it.
1349 static struct fc_port *qlt_create_sess(
1350 struct scsi_qla_host *vha,
1354 struct qla_hw_data *ha = vha->hw;
1355 struct fc_port *sess = fcport;
1356 unsigned long flags;
1358 if (vha->vha_tgt.qla_tgt->tgt_stop)
1361 if (fcport->se_sess) {
1362 if (!kref_get_unless_zero(&sess->sess_kref)) {
1363 ql_dbg(ql_dbg_disc, vha, 0x20f6,
1364 "%s: kref_get_unless_zero failed for %8phC\n",
1365 __func__, sess->port_name);
1370 sess->tgt = vha->vha_tgt.qla_tgt;
1371 sess->local = local;
1374 * Under normal circumstances we want to logout from firmware when
1375 * session eventually ends and release corresponding nport handle.
1376 * In the exception cases (e.g. when new PLOGI is waiting) corresponding
1377 * code will adjust these flags as necessary.
1379 sess->logout_on_delete = 1;
1380 sess->keep_nport_handle = 0;
1381 sess->logout_completed = 0;
1383 if (ha->tgt.tgt_ops->check_initiator_node_acl(vha,
1384 &fcport->port_name[0], sess) < 0) {
1385 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf015,
1386 "(%d) %8phC check_initiator_node_acl failed\n",
1387 vha->vp_idx, fcport->port_name);
1390 kref_init(&fcport->sess_kref);
1392 * Take an extra reference to ->sess_kref here to handle
1393 * fc_port access across ->tgt.sess_lock reaquire.
1395 if (!kref_get_unless_zero(&sess->sess_kref)) {
1396 ql_dbg(ql_dbg_disc, vha, 0x20f7,
1397 "%s: kref_get_unless_zero failed for %8phC\n",
1398 __func__, sess->port_name);
1402 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
1403 if (!IS_SW_RESV_ADDR(sess->d_id))
1404 vha->vha_tgt.qla_tgt->sess_count++;
1406 qlt_do_generation_tick(vha, &sess->generation);
1407 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
1410 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf006,
1411 "Adding sess %p se_sess %p to tgt %p sess_count %d\n",
1412 sess, sess->se_sess, vha->vha_tgt.qla_tgt,
1413 vha->vha_tgt.qla_tgt->sess_count);
1415 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04b,
1416 "qla_target(%d): %ssession for wwn %8phC (loop_id %d, "
1417 "s_id %x:%x:%x, confirmed completion %ssupported) added\n",
1418 vha->vp_idx, local ? "local " : "", fcport->port_name,
1419 fcport->loop_id, sess->d_id.b.domain, sess->d_id.b.area,
1420 sess->d_id.b.al_pa, sess->conf_compl_supported ? "" : "not ");
1426 * max_gen - specifies maximum session generation
1427 * at which this deletion requestion is still valid
1430 qlt_fc_port_deleted(struct scsi_qla_host *vha, fc_port_t *fcport, int max_gen)
1432 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
1433 struct fc_port *sess = fcport;
1434 unsigned long flags;
1436 if (!vha->hw->tgt.tgt_ops)
1442 spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
1443 if (tgt->tgt_stop) {
1444 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
1447 if (!sess->se_sess) {
1448 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
1452 if (max_gen - sess->generation < 0) {
1453 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
1454 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf092,
1455 "Ignoring stale deletion request for se_sess %p / sess %p"
1456 " for port %8phC, req_gen %d, sess_gen %d\n",
1457 sess->se_sess, sess, sess->port_name, max_gen,
1462 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf008, "qla_tgt_fc_port_deleted %p", sess);
1465 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
1466 qlt_schedule_sess_for_deletion(sess);
1469 static inline int test_tgt_sess_count(struct qla_tgt *tgt)
1471 struct qla_hw_data *ha = tgt->ha;
1472 unsigned long flags;
1475 * We need to protect against race, when tgt is freed before or
1478 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
1479 ql_dbg(ql_dbg_tgt, tgt->vha, 0xe002,
1480 "tgt %p, sess_count=%d\n",
1481 tgt, tgt->sess_count);
1482 res = (tgt->sess_count == 0);
1483 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
1488 /* Called by tcm_qla2xxx configfs code */
1489 int qlt_stop_phase1(struct qla_tgt *tgt)
1491 struct scsi_qla_host *vha = tgt->vha;
1492 struct qla_hw_data *ha = tgt->ha;
1493 unsigned long flags;
1495 mutex_lock(&ha->optrom_mutex);
1496 mutex_lock(&qla_tgt_mutex);
1498 if (tgt->tgt_stop || tgt->tgt_stopped) {
1499 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04e,
1500 "Already in tgt->tgt_stop or tgt_stopped state\n");
1501 mutex_unlock(&qla_tgt_mutex);
1502 mutex_unlock(&ha->optrom_mutex);
1506 ql_dbg(ql_dbg_tgt_mgt, vha, 0xe003, "Stopping target for host %ld(%p)\n",
1509 * Mutex needed to sync with qla_tgt_fc_port_[added,deleted].
1510 * Lock is needed, because we still can get an incoming packet.
1512 mutex_lock(&vha->vha_tgt.tgt_mutex);
1514 qlt_clear_tgt_db(tgt);
1515 mutex_unlock(&vha->vha_tgt.tgt_mutex);
1516 mutex_unlock(&qla_tgt_mutex);
1518 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf009,
1519 "Waiting for sess works (tgt %p)", tgt);
1520 spin_lock_irqsave(&tgt->sess_work_lock, flags);
1521 while (!list_empty(&tgt->sess_works_list)) {
1522 spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
1523 flush_scheduled_work();
1524 spin_lock_irqsave(&tgt->sess_work_lock, flags);
1526 spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
1528 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00a,
1529 "Waiting for tgt %p: sess_count=%d\n", tgt, tgt->sess_count);
1531 wait_event_timeout(tgt->waitQ, test_tgt_sess_count(tgt), 10*HZ);
1534 if (!ha->flags.host_shutting_down &&
1535 (qla_tgt_mode_enabled(vha) || qla_dual_mode_enabled(vha)))
1536 qlt_disable_vha(vha);
1538 /* Wait for sessions to clear out (just in case) */
1539 wait_event_timeout(tgt->waitQ, test_tgt_sess_count(tgt), 10*HZ);
1540 mutex_unlock(&ha->optrom_mutex);
1544 EXPORT_SYMBOL(qlt_stop_phase1);
1546 /* Called by tcm_qla2xxx configfs code */
1547 void qlt_stop_phase2(struct qla_tgt *tgt)
1549 scsi_qla_host_t *vha = tgt->vha;
1551 if (tgt->tgt_stopped) {
1552 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04f,
1553 "Already in tgt->tgt_stopped state\n");
1557 if (!tgt->tgt_stop) {
1558 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00b,
1559 "%s: phase1 stop is not completed\n", __func__);
1564 mutex_lock(&vha->vha_tgt.tgt_mutex);
1566 tgt->tgt_stopped = 1;
1567 mutex_unlock(&vha->vha_tgt.tgt_mutex);
1569 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00c, "Stop of tgt %p finished\n",
1572 switch (vha->qlini_mode) {
1573 case QLA2XXX_INI_MODE_EXCLUSIVE:
1574 vha->flags.online = 1;
1575 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
1581 EXPORT_SYMBOL(qlt_stop_phase2);
1583 /* Called from qlt_remove_target() -> qla2x00_remove_one() */
1584 static void qlt_release(struct qla_tgt *tgt)
1586 scsi_qla_host_t *vha = tgt->vha;
1590 struct qla_qpair_hint *h;
1591 struct qla_hw_data *ha = vha->hw;
1593 if ((vha->vha_tgt.qla_tgt != NULL) && !tgt->tgt_stop &&
1595 qlt_stop_phase1(tgt);
1597 if ((vha->vha_tgt.qla_tgt != NULL) && !tgt->tgt_stopped)
1598 qlt_stop_phase2(tgt);
1600 for (i = 0; i < vha->hw->max_qpairs + 1; i++) {
1601 unsigned long flags;
1603 h = &tgt->qphints[i];
1605 spin_lock_irqsave(h->qpair->qp_lock_ptr, flags);
1606 list_del(&h->hint_elem);
1607 spin_unlock_irqrestore(h->qpair->qp_lock_ptr, flags);
1611 kfree(tgt->qphints);
1612 mutex_lock(&qla_tgt_mutex);
1613 list_del(&vha->vha_tgt.qla_tgt->tgt_list_entry);
1614 mutex_unlock(&qla_tgt_mutex);
1616 btree_for_each_safe64(&tgt->lun_qpair_map, key, node)
1617 btree_remove64(&tgt->lun_qpair_map, key);
1619 btree_destroy64(&tgt->lun_qpair_map);
1622 if (ha->tgt.tgt_ops &&
1623 ha->tgt.tgt_ops->remove_target &&
1624 vha->vha_tgt.target_lport_ptr)
1625 ha->tgt.tgt_ops->remove_target(vha);
1627 vha->vha_tgt.qla_tgt = NULL;
1629 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00d,
1630 "Release of tgt %p finished\n", tgt);
1635 /* ha->hardware_lock supposed to be held on entry */
1636 static int qlt_sched_sess_work(struct qla_tgt *tgt, int type,
1637 const void *param, unsigned int param_size)
1639 struct qla_tgt_sess_work_param *prm;
1640 unsigned long flags;
1642 prm = kzalloc(sizeof(*prm), GFP_ATOMIC);
1644 ql_dbg(ql_dbg_tgt_mgt, tgt->vha, 0xf050,
1645 "qla_target(%d): Unable to create session "
1646 "work, command will be refused", 0);
1650 ql_dbg(ql_dbg_tgt_mgt, tgt->vha, 0xf00e,
1651 "Scheduling work (type %d, prm %p)"
1652 " to find session for param %p (size %d, tgt %p)\n",
1653 type, prm, param, param_size, tgt);
1656 memcpy(&prm->tm_iocb, param, param_size);
1658 spin_lock_irqsave(&tgt->sess_work_lock, flags);
1659 list_add_tail(&prm->sess_works_list_entry, &tgt->sess_works_list);
1660 spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
1662 schedule_work(&tgt->sess_work);
1668 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
1670 static void qlt_send_notify_ack(struct qla_qpair *qpair,
1671 struct imm_ntfy_from_isp *ntfy,
1672 uint32_t add_flags, uint16_t resp_code, int resp_code_valid,
1673 uint16_t srr_flags, uint16_t srr_reject_code, uint8_t srr_explan)
1675 struct scsi_qla_host *vha = qpair->vha;
1676 struct qla_hw_data *ha = vha->hw;
1678 struct nack_to_isp *nack;
1680 if (!ha->flags.fw_started)
1683 ql_dbg(ql_dbg_tgt, vha, 0xe004, "Sending NOTIFY_ACK (ha=%p)\n", ha);
1685 pkt = (request_t *)__qla2x00_alloc_iocbs(qpair, NULL);
1687 ql_dbg(ql_dbg_tgt, vha, 0xe049,
1688 "qla_target(%d): %s failed: unable to allocate "
1689 "request packet\n", vha->vp_idx, __func__);
1693 if (vha->vha_tgt.qla_tgt != NULL)
1694 vha->vha_tgt.qla_tgt->notify_ack_expected++;
1696 pkt->entry_type = NOTIFY_ACK_TYPE;
1697 pkt->entry_count = 1;
1699 nack = (struct nack_to_isp *)pkt;
1700 nack->ox_id = ntfy->ox_id;
1702 nack->u.isp24.handle = QLA_TGT_SKIP_HANDLE;
1703 nack->u.isp24.nport_handle = ntfy->u.isp24.nport_handle;
1704 if (le16_to_cpu(ntfy->u.isp24.status) == IMM_NTFY_ELS) {
1705 nack->u.isp24.flags = ntfy->u.isp24.flags &
1706 cpu_to_le32(NOTIFY24XX_FLAGS_PUREX_IOCB);
1708 nack->u.isp24.srr_rx_id = ntfy->u.isp24.srr_rx_id;
1709 nack->u.isp24.status = ntfy->u.isp24.status;
1710 nack->u.isp24.status_subcode = ntfy->u.isp24.status_subcode;
1711 nack->u.isp24.fw_handle = ntfy->u.isp24.fw_handle;
1712 nack->u.isp24.exchange_address = ntfy->u.isp24.exchange_address;
1713 nack->u.isp24.srr_rel_offs = ntfy->u.isp24.srr_rel_offs;
1714 nack->u.isp24.srr_ui = ntfy->u.isp24.srr_ui;
1715 nack->u.isp24.srr_flags = cpu_to_le16(srr_flags);
1716 nack->u.isp24.srr_reject_code = srr_reject_code;
1717 nack->u.isp24.srr_reject_code_expl = srr_explan;
1718 nack->u.isp24.vp_index = ntfy->u.isp24.vp_index;
1720 ql_dbg(ql_dbg_tgt, vha, 0xe005,
1721 "qla_target(%d): Sending 24xx Notify Ack %d\n",
1722 vha->vp_idx, nack->u.isp24.status);
1724 /* Memory Barrier */
1726 qla2x00_start_iocbs(vha, qpair->req);
1729 static int qlt_build_abts_resp_iocb(struct qla_tgt_mgmt_cmd *mcmd)
1731 struct scsi_qla_host *vha = mcmd->vha;
1732 struct qla_hw_data *ha = vha->hw;
1733 struct abts_resp_to_24xx *resp;
1737 struct abts_recv_from_24xx *abts = &mcmd->orig_iocb.abts;
1738 struct qla_qpair *qpair = mcmd->qpair;
1740 ql_dbg(ql_dbg_tgt, vha, 0xe006,
1741 "Sending task mgmt ABTS response (ha=%p, status=%x)\n",
1742 ha, mcmd->fc_tm_rsp);
1744 rc = qlt_check_reserve_free_req(qpair, 1);
1746 ql_dbg(ql_dbg_tgt, vha, 0xe04a,
1747 "qla_target(%d): %s failed: unable to allocate request packet\n",
1748 vha->vp_idx, __func__);
1752 resp = (struct abts_resp_to_24xx *)qpair->req->ring_ptr;
1753 memset(resp, 0, sizeof(*resp));
1755 h = qlt_make_handle(qpair);
1756 if (unlikely(h == QLA_TGT_NULL_HANDLE)) {
1758 * CTIO type 7 from the firmware doesn't provide a way to
1759 * know the initiator's LOOP ID, hence we can't find
1760 * the session and, so, the command.
1764 qpair->req->outstanding_cmds[h] = (srb_t *)mcmd;
1767 resp->handle = MAKE_HANDLE(qpair->req->id, h);
1768 resp->entry_type = ABTS_RESP_24XX;
1769 resp->entry_count = 1;
1770 resp->nport_handle = abts->nport_handle;
1771 resp->vp_index = vha->vp_idx;
1772 resp->sof_type = abts->sof_type;
1773 resp->exchange_address = abts->exchange_address;
1774 resp->fcp_hdr_le = abts->fcp_hdr_le;
1775 f_ctl = cpu_to_le32(F_CTL_EXCH_CONTEXT_RESP |
1776 F_CTL_LAST_SEQ | F_CTL_END_SEQ |
1777 F_CTL_SEQ_INITIATIVE);
1778 p = (uint8_t *)&f_ctl;
1779 resp->fcp_hdr_le.f_ctl[0] = *p++;
1780 resp->fcp_hdr_le.f_ctl[1] = *p++;
1781 resp->fcp_hdr_le.f_ctl[2] = *p;
1783 resp->fcp_hdr_le.d_id[0] = abts->fcp_hdr_le.s_id[0];
1784 resp->fcp_hdr_le.d_id[1] = abts->fcp_hdr_le.s_id[1];
1785 resp->fcp_hdr_le.d_id[2] = abts->fcp_hdr_le.s_id[2];
1786 resp->fcp_hdr_le.s_id[0] = abts->fcp_hdr_le.d_id[0];
1787 resp->fcp_hdr_le.s_id[1] = abts->fcp_hdr_le.d_id[1];
1788 resp->fcp_hdr_le.s_id[2] = abts->fcp_hdr_le.d_id[2];
1790 resp->exchange_addr_to_abort = abts->exchange_addr_to_abort;
1791 if (mcmd->fc_tm_rsp == FCP_TMF_CMPL) {
1792 resp->fcp_hdr_le.r_ctl = R_CTL_BASIC_LINK_SERV | R_CTL_B_ACC;
1793 resp->payload.ba_acct.seq_id_valid = SEQ_ID_INVALID;
1794 resp->payload.ba_acct.low_seq_cnt = 0x0000;
1795 resp->payload.ba_acct.high_seq_cnt = 0xFFFF;
1796 resp->payload.ba_acct.ox_id = abts->fcp_hdr_le.ox_id;
1797 resp->payload.ba_acct.rx_id = abts->fcp_hdr_le.rx_id;
1799 resp->fcp_hdr_le.r_ctl = R_CTL_BASIC_LINK_SERV | R_CTL_B_RJT;
1800 resp->payload.ba_rjt.reason_code =
1801 BA_RJT_REASON_CODE_UNABLE_TO_PERFORM;
1802 /* Other bytes are zero */
1805 vha->vha_tgt.qla_tgt->abts_resp_expected++;
1807 /* Memory Barrier */
1809 if (qpair->reqq_start_iocbs)
1810 qpair->reqq_start_iocbs(qpair);
1812 qla2x00_start_iocbs(vha, qpair->req);
1818 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
1820 static void qlt_24xx_send_abts_resp(struct qla_qpair *qpair,
1821 struct abts_recv_from_24xx *abts, uint32_t status,
1824 struct scsi_qla_host *vha = qpair->vha;
1825 struct qla_hw_data *ha = vha->hw;
1826 struct abts_resp_to_24xx *resp;
1830 ql_dbg(ql_dbg_tgt, vha, 0xe006,
1831 "Sending task mgmt ABTS response (ha=%p, atio=%p, status=%x\n",
1834 resp = (struct abts_resp_to_24xx *)qla2x00_alloc_iocbs_ready(qpair,
1837 ql_dbg(ql_dbg_tgt, vha, 0xe04a,
1838 "qla_target(%d): %s failed: unable to allocate "
1839 "request packet", vha->vp_idx, __func__);
1843 resp->entry_type = ABTS_RESP_24XX;
1844 resp->handle = QLA_TGT_SKIP_HANDLE;
1845 resp->entry_count = 1;
1846 resp->nport_handle = abts->nport_handle;
1847 resp->vp_index = vha->vp_idx;
1848 resp->sof_type = abts->sof_type;
1849 resp->exchange_address = abts->exchange_address;
1850 resp->fcp_hdr_le = abts->fcp_hdr_le;
1851 f_ctl = cpu_to_le32(F_CTL_EXCH_CONTEXT_RESP |
1852 F_CTL_LAST_SEQ | F_CTL_END_SEQ |
1853 F_CTL_SEQ_INITIATIVE);
1854 p = (uint8_t *)&f_ctl;
1855 resp->fcp_hdr_le.f_ctl[0] = *p++;
1856 resp->fcp_hdr_le.f_ctl[1] = *p++;
1857 resp->fcp_hdr_le.f_ctl[2] = *p;
1859 resp->fcp_hdr_le.d_id[0] = abts->fcp_hdr_le.d_id[0];
1860 resp->fcp_hdr_le.d_id[1] = abts->fcp_hdr_le.d_id[1];
1861 resp->fcp_hdr_le.d_id[2] = abts->fcp_hdr_le.d_id[2];
1862 resp->fcp_hdr_le.s_id[0] = abts->fcp_hdr_le.s_id[0];
1863 resp->fcp_hdr_le.s_id[1] = abts->fcp_hdr_le.s_id[1];
1864 resp->fcp_hdr_le.s_id[2] = abts->fcp_hdr_le.s_id[2];
1866 resp->fcp_hdr_le.d_id[0] = abts->fcp_hdr_le.s_id[0];
1867 resp->fcp_hdr_le.d_id[1] = abts->fcp_hdr_le.s_id[1];
1868 resp->fcp_hdr_le.d_id[2] = abts->fcp_hdr_le.s_id[2];
1869 resp->fcp_hdr_le.s_id[0] = abts->fcp_hdr_le.d_id[0];
1870 resp->fcp_hdr_le.s_id[1] = abts->fcp_hdr_le.d_id[1];
1871 resp->fcp_hdr_le.s_id[2] = abts->fcp_hdr_le.d_id[2];
1873 resp->exchange_addr_to_abort = abts->exchange_addr_to_abort;
1874 if (status == FCP_TMF_CMPL) {
1875 resp->fcp_hdr_le.r_ctl = R_CTL_BASIC_LINK_SERV | R_CTL_B_ACC;
1876 resp->payload.ba_acct.seq_id_valid = SEQ_ID_INVALID;
1877 resp->payload.ba_acct.low_seq_cnt = 0x0000;
1878 resp->payload.ba_acct.high_seq_cnt = 0xFFFF;
1879 resp->payload.ba_acct.ox_id = abts->fcp_hdr_le.ox_id;
1880 resp->payload.ba_acct.rx_id = abts->fcp_hdr_le.rx_id;
1882 resp->fcp_hdr_le.r_ctl = R_CTL_BASIC_LINK_SERV | R_CTL_B_RJT;
1883 resp->payload.ba_rjt.reason_code =
1884 BA_RJT_REASON_CODE_UNABLE_TO_PERFORM;
1885 /* Other bytes are zero */
1888 vha->vha_tgt.qla_tgt->abts_resp_expected++;
1890 /* Memory Barrier */
1892 if (qpair->reqq_start_iocbs)
1893 qpair->reqq_start_iocbs(qpair);
1895 qla2x00_start_iocbs(vha, qpair->req);
1899 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
1901 static void qlt_24xx_retry_term_exchange(struct scsi_qla_host *vha,
1902 struct qla_qpair *qpair, response_t *pkt, struct qla_tgt_mgmt_cmd *mcmd)
1904 struct ctio7_to_24xx *ctio;
1906 struct abts_recv_from_24xx *entry;
1908 ctio = (struct ctio7_to_24xx *)qla2x00_alloc_iocbs_ready(qpair, NULL);
1910 ql_dbg(ql_dbg_tgt, vha, 0xe04b,
1911 "qla_target(%d): %s failed: unable to allocate "
1912 "request packet\n", vha->vp_idx, __func__);
1917 /* abts from remote port */
1918 entry = &mcmd->orig_iocb.abts;
1920 /* abts from this driver. */
1921 entry = (struct abts_recv_from_24xx *)pkt;
1924 * We've got on entrance firmware's response on by us generated
1925 * ABTS response. So, in it ID fields are reversed.
1928 ctio->entry_type = CTIO_TYPE7;
1929 ctio->entry_count = 1;
1930 ctio->nport_handle = entry->nport_handle;
1931 ctio->handle = QLA_TGT_SKIP_HANDLE | CTIO_COMPLETION_HANDLE_MARK;
1932 ctio->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
1933 ctio->vp_index = vha->vp_idx;
1934 ctio->exchange_addr = entry->exchange_addr_to_abort;
1935 tmp = (CTIO7_FLAGS_STATUS_MODE_1 | CTIO7_FLAGS_TERMINATE);
1938 ctio->initiator_id[0] = entry->fcp_hdr_le.s_id[0];
1939 ctio->initiator_id[1] = entry->fcp_hdr_le.s_id[1];
1940 ctio->initiator_id[2] = entry->fcp_hdr_le.s_id[2];
1942 if (mcmd->flags & QLA24XX_MGMT_ABORT_IO_ATTR_VALID)
1943 tmp |= (mcmd->abort_io_attr << 9);
1944 else if (qpair->retry_term_cnt & 1)
1947 ctio->initiator_id[0] = entry->fcp_hdr_le.d_id[0];
1948 ctio->initiator_id[1] = entry->fcp_hdr_le.d_id[1];
1949 ctio->initiator_id[2] = entry->fcp_hdr_le.d_id[2];
1951 if (qpair->retry_term_cnt & 1)
1954 ctio->u.status1.flags = cpu_to_le16(tmp);
1955 ctio->u.status1.ox_id = entry->fcp_hdr_le.ox_id;
1957 ql_dbg(ql_dbg_tgt, vha, 0xe007,
1958 "Sending retry TERM EXCH CTIO7 flags %04xh oxid %04xh attr valid %x\n",
1959 le16_to_cpu(ctio->u.status1.flags),
1960 le16_to_cpu(ctio->u.status1.ox_id),
1961 (mcmd && mcmd->flags & QLA24XX_MGMT_ABORT_IO_ATTR_VALID) ? 1 : 0);
1963 /* Memory Barrier */
1965 if (qpair->reqq_start_iocbs)
1966 qpair->reqq_start_iocbs(qpair);
1968 qla2x00_start_iocbs(vha, qpair->req);
1971 qlt_build_abts_resp_iocb(mcmd);
1973 qlt_24xx_send_abts_resp(qpair,
1974 (struct abts_recv_from_24xx *)entry, FCP_TMF_CMPL, true);
1978 /* drop cmds for the given lun
1979 * XXX only looks for cmds on the port through which lun reset was recieved
1980 * XXX does not go through the list of other port (which may have cmds
1983 static void abort_cmds_for_lun(struct scsi_qla_host *vha,
1984 u64 lun, uint8_t *s_id)
1986 struct qla_tgt_sess_op *op;
1987 struct qla_tgt_cmd *cmd;
1989 unsigned long flags;
1991 key = sid_to_key(s_id);
1992 spin_lock_irqsave(&vha->cmd_list_lock, flags);
1993 list_for_each_entry(op, &vha->qla_sess_op_cmd_list, cmd_list) {
1997 op_key = sid_to_key(op->atio.u.isp24.fcp_hdr.s_id);
1998 op_lun = scsilun_to_int(
1999 (struct scsi_lun *)&op->atio.u.isp24.fcp_cmnd.lun);
2000 if (op_key == key && op_lun == lun)
2004 list_for_each_entry(op, &vha->unknown_atio_list, cmd_list) {
2008 op_key = sid_to_key(op->atio.u.isp24.fcp_hdr.s_id);
2009 op_lun = scsilun_to_int(
2010 (struct scsi_lun *)&op->atio.u.isp24.fcp_cmnd.lun);
2011 if (op_key == key && op_lun == lun)
2015 list_for_each_entry(cmd, &vha->qla_cmd_list, cmd_list) {
2019 cmd_key = sid_to_key(cmd->atio.u.isp24.fcp_hdr.s_id);
2020 cmd_lun = scsilun_to_int(
2021 (struct scsi_lun *)&cmd->atio.u.isp24.fcp_cmnd.lun);
2022 if (cmd_key == key && cmd_lun == lun)
2025 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
2028 static struct qla_qpair_hint *qlt_find_qphint(struct scsi_qla_host *vha,
2029 uint64_t unpacked_lun)
2031 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
2032 struct qla_qpair_hint *h = NULL;
2034 if (vha->flags.qpairs_available) {
2035 h = btree_lookup64(&tgt->lun_qpair_map, unpacked_lun);
2037 h = &tgt->qphints[0];
2039 h = &tgt->qphints[0];
2045 static void qlt_do_tmr_work(struct work_struct *work)
2047 struct qla_tgt_mgmt_cmd *mcmd =
2048 container_of(work, struct qla_tgt_mgmt_cmd, work);
2049 struct qla_hw_data *ha = mcmd->vha->hw;
2052 unsigned long flags;
2054 switch (mcmd->tmr_func) {
2056 tag = mcmd->orig_iocb.abts.exchange_addr_to_abort;
2063 rc = ha->tgt.tgt_ops->handle_tmr(mcmd, mcmd->unpacked_lun,
2064 mcmd->tmr_func, tag);
2067 spin_lock_irqsave(mcmd->qpair->qp_lock_ptr, flags);
2068 switch (mcmd->tmr_func) {
2070 mcmd->fc_tm_rsp = FCP_TMF_REJECTED;
2071 qlt_build_abts_resp_iocb(mcmd);
2073 case QLA_TGT_LUN_RESET:
2074 case QLA_TGT_CLEAR_TS:
2075 case QLA_TGT_ABORT_TS:
2076 case QLA_TGT_CLEAR_ACA:
2077 case QLA_TGT_TARGET_RESET:
2078 qlt_send_busy(mcmd->qpair, &mcmd->orig_iocb.atio,
2082 case QLA_TGT_ABORT_ALL:
2083 case QLA_TGT_NEXUS_LOSS_SESS:
2084 case QLA_TGT_NEXUS_LOSS:
2085 qlt_send_notify_ack(mcmd->qpair,
2086 &mcmd->orig_iocb.imm_ntfy, 0, 0, 0, 0, 0, 0);
2089 spin_unlock_irqrestore(mcmd->qpair->qp_lock_ptr, flags);
2091 ql_dbg(ql_dbg_tgt_mgt, mcmd->vha, 0xf052,
2092 "qla_target(%d): tgt_ops->handle_tmr() failed: %d\n",
2093 mcmd->vha->vp_idx, rc);
2094 mempool_free(mcmd, qla_tgt_mgmt_cmd_mempool);
2098 /* ha->hardware_lock supposed to be held on entry */
2099 static int __qlt_24xx_handle_abts(struct scsi_qla_host *vha,
2100 struct abts_recv_from_24xx *abts, struct fc_port *sess)
2102 struct qla_hw_data *ha = vha->hw;
2103 struct qla_tgt_mgmt_cmd *mcmd;
2104 struct qla_qpair_hint *h = &vha->vha_tgt.qla_tgt->qphints[0];
2106 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00f,
2107 "qla_target(%d): task abort (tag=%d)\n",
2108 vha->vp_idx, abts->exchange_addr_to_abort);
2110 mcmd = mempool_alloc(qla_tgt_mgmt_cmd_mempool, GFP_ATOMIC);
2112 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf051,
2113 "qla_target(%d): %s: Allocation of ABORT cmd failed",
2114 vha->vp_idx, __func__);
2117 memset(mcmd, 0, sizeof(*mcmd));
2118 mcmd->cmd_type = TYPE_TGT_TMCMD;
2120 memcpy(&mcmd->orig_iocb.abts, abts, sizeof(mcmd->orig_iocb.abts));
2121 mcmd->reset_count = ha->base_qpair->chip_reset;
2122 mcmd->tmr_func = QLA_TGT_ABTS;
2123 mcmd->qpair = h->qpair;
2127 * LUN is looked up by target-core internally based on the passed
2128 * abts->exchange_addr_to_abort tag.
2130 mcmd->se_cmd.cpuid = h->cpuid;
2132 if (ha->tgt.tgt_ops->find_cmd_by_tag) {
2133 struct qla_tgt_cmd *abort_cmd;
2135 abort_cmd = ha->tgt.tgt_ops->find_cmd_by_tag(sess,
2136 abts->exchange_addr_to_abort);
2137 if (abort_cmd && abort_cmd->qpair) {
2138 mcmd->qpair = abort_cmd->qpair;
2139 mcmd->se_cmd.cpuid = abort_cmd->se_cmd.cpuid;
2140 mcmd->abort_io_attr = abort_cmd->atio.u.isp24.attr;
2141 mcmd->flags = QLA24XX_MGMT_ABORT_IO_ATTR_VALID;
2145 INIT_WORK(&mcmd->work, qlt_do_tmr_work);
2146 queue_work_on(mcmd->se_cmd.cpuid, qla_tgt_wq, &mcmd->work);
2152 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
2154 static void qlt_24xx_handle_abts(struct scsi_qla_host *vha,
2155 struct abts_recv_from_24xx *abts)
2157 struct qla_hw_data *ha = vha->hw;
2158 struct fc_port *sess;
2159 uint32_t tag = abts->exchange_addr_to_abort;
2162 unsigned long flags;
2164 if (le32_to_cpu(abts->fcp_hdr_le.parameter) & ABTS_PARAM_ABORT_SEQ) {
2165 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf053,
2166 "qla_target(%d): ABTS: Abort Sequence not "
2167 "supported\n", vha->vp_idx);
2168 qlt_24xx_send_abts_resp(ha->base_qpair, abts, FCP_TMF_REJECTED,
2173 if (tag == ATIO_EXCHANGE_ADDRESS_UNKNOWN) {
2174 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf010,
2175 "qla_target(%d): ABTS: Unknown Exchange "
2176 "Address received\n", vha->vp_idx);
2177 qlt_24xx_send_abts_resp(ha->base_qpair, abts, FCP_TMF_REJECTED,
2182 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf011,
2183 "qla_target(%d): task abort (s_id=%x:%x:%x, "
2184 "tag=%d, param=%x)\n", vha->vp_idx, abts->fcp_hdr_le.s_id[2],
2185 abts->fcp_hdr_le.s_id[1], abts->fcp_hdr_le.s_id[0], tag,
2186 le32_to_cpu(abts->fcp_hdr_le.parameter));
2188 s_id[0] = abts->fcp_hdr_le.s_id[2];
2189 s_id[1] = abts->fcp_hdr_le.s_id[1];
2190 s_id[2] = abts->fcp_hdr_le.s_id[0];
2192 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
2193 sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha, s_id);
2195 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf012,
2196 "qla_target(%d): task abort for non-existent session\n",
2198 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
2200 qlt_24xx_send_abts_resp(ha->base_qpair, abts, FCP_TMF_REJECTED,
2204 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
2207 if (sess->deleted) {
2208 qlt_24xx_send_abts_resp(ha->base_qpair, abts, FCP_TMF_REJECTED,
2213 rc = __qlt_24xx_handle_abts(vha, abts, sess);
2215 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf054,
2216 "qla_target(%d): __qlt_24xx_handle_abts() failed: %d\n",
2218 qlt_24xx_send_abts_resp(ha->base_qpair, abts, FCP_TMF_REJECTED,
2225 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
2227 static void qlt_24xx_send_task_mgmt_ctio(struct qla_qpair *qpair,
2228 struct qla_tgt_mgmt_cmd *mcmd, uint32_t resp_code)
2230 struct scsi_qla_host *ha = mcmd->vha;
2231 struct atio_from_isp *atio = &mcmd->orig_iocb.atio;
2232 struct ctio7_to_24xx *ctio;
2235 ql_dbg(ql_dbg_tgt, ha, 0xe008,
2236 "Sending task mgmt CTIO7 (ha=%p, atio=%p, resp_code=%x\n",
2237 ha, atio, resp_code);
2240 ctio = (struct ctio7_to_24xx *)__qla2x00_alloc_iocbs(qpair, NULL);
2242 ql_dbg(ql_dbg_tgt, ha, 0xe04c,
2243 "qla_target(%d): %s failed: unable to allocate "
2244 "request packet\n", ha->vp_idx, __func__);
2248 ctio->entry_type = CTIO_TYPE7;
2249 ctio->entry_count = 1;
2250 ctio->handle = QLA_TGT_SKIP_HANDLE | CTIO_COMPLETION_HANDLE_MARK;
2251 ctio->nport_handle = mcmd->sess->loop_id;
2252 ctio->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
2253 ctio->vp_index = ha->vp_idx;
2254 ctio->initiator_id[0] = atio->u.isp24.fcp_hdr.s_id[2];
2255 ctio->initiator_id[1] = atio->u.isp24.fcp_hdr.s_id[1];
2256 ctio->initiator_id[2] = atio->u.isp24.fcp_hdr.s_id[0];
2257 ctio->exchange_addr = atio->u.isp24.exchange_addr;
2258 temp = (atio->u.isp24.attr << 9)|
2259 CTIO7_FLAGS_STATUS_MODE_1 | CTIO7_FLAGS_SEND_STATUS;
2260 ctio->u.status1.flags = cpu_to_le16(temp);
2261 temp = be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id);
2262 ctio->u.status1.ox_id = cpu_to_le16(temp);
2263 ctio->u.status1.scsi_status =
2264 cpu_to_le16(SS_RESPONSE_INFO_LEN_VALID);
2265 ctio->u.status1.response_len = cpu_to_le16(8);
2266 ctio->u.status1.sense_data[0] = resp_code;
2268 /* Memory Barrier */
2270 if (qpair->reqq_start_iocbs)
2271 qpair->reqq_start_iocbs(qpair);
2273 qla2x00_start_iocbs(ha, qpair->req);
2276 void qlt_free_mcmd(struct qla_tgt_mgmt_cmd *mcmd)
2278 mempool_free(mcmd, qla_tgt_mgmt_cmd_mempool);
2280 EXPORT_SYMBOL(qlt_free_mcmd);
2283 * ha->hardware_lock supposed to be held on entry. Might drop it, then
2286 void qlt_send_resp_ctio(struct qla_qpair *qpair, struct qla_tgt_cmd *cmd,
2287 uint8_t scsi_status, uint8_t sense_key, uint8_t asc, uint8_t ascq)
2289 struct atio_from_isp *atio = &cmd->atio;
2290 struct ctio7_to_24xx *ctio;
2292 struct scsi_qla_host *vha = cmd->vha;
2294 ql_dbg(ql_dbg_tgt_dif, vha, 0x3066,
2295 "Sending response CTIO7 (vha=%p, atio=%p, scsi_status=%02x, "
2296 "sense_key=%02x, asc=%02x, ascq=%02x",
2297 vha, atio, scsi_status, sense_key, asc, ascq);
2299 ctio = (struct ctio7_to_24xx *)qla2x00_alloc_iocbs(vha, NULL);
2301 ql_dbg(ql_dbg_async, vha, 0x3067,
2302 "qla2x00t(%ld): %s failed: unable to allocate request packet",
2303 vha->host_no, __func__);
2307 ctio->entry_type = CTIO_TYPE7;
2308 ctio->entry_count = 1;
2309 ctio->handle = QLA_TGT_SKIP_HANDLE;
2310 ctio->nport_handle = cmd->sess->loop_id;
2311 ctio->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
2312 ctio->vp_index = vha->vp_idx;
2313 ctio->initiator_id[0] = atio->u.isp24.fcp_hdr.s_id[2];
2314 ctio->initiator_id[1] = atio->u.isp24.fcp_hdr.s_id[1];
2315 ctio->initiator_id[2] = atio->u.isp24.fcp_hdr.s_id[0];
2316 ctio->exchange_addr = atio->u.isp24.exchange_addr;
2317 temp = (atio->u.isp24.attr << 9) |
2318 CTIO7_FLAGS_STATUS_MODE_1 | CTIO7_FLAGS_SEND_STATUS;
2319 ctio->u.status1.flags = cpu_to_le16(temp);
2320 temp = be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id);
2321 ctio->u.status1.ox_id = cpu_to_le16(temp);
2322 ctio->u.status1.scsi_status =
2323 cpu_to_le16(SS_RESPONSE_INFO_LEN_VALID | scsi_status);
2324 ctio->u.status1.response_len = cpu_to_le16(18);
2325 ctio->u.status1.residual = cpu_to_le32(get_datalen_for_atio(atio));
2327 if (ctio->u.status1.residual != 0)
2328 ctio->u.status1.scsi_status |=
2329 cpu_to_le16(SS_RESIDUAL_UNDER);
2331 /* Response code and sense key */
2332 put_unaligned_le32(((0x70 << 24) | (sense_key << 8)),
2333 (&ctio->u.status1.sense_data)[0]);
2334 /* Additional sense length */
2335 put_unaligned_le32(0x0a, (&ctio->u.status1.sense_data)[1]);
2337 put_unaligned_le32(((asc << 24) | (ascq << 16)),
2338 (&ctio->u.status1.sense_data)[3]);
2340 /* Memory Barrier */
2343 if (qpair->reqq_start_iocbs)
2344 qpair->reqq_start_iocbs(qpair);
2346 qla2x00_start_iocbs(vha, qpair->req);
2352 /* callback from target fabric module code */
2353 void qlt_xmit_tm_rsp(struct qla_tgt_mgmt_cmd *mcmd)
2355 struct scsi_qla_host *vha = mcmd->sess->vha;
2356 struct qla_hw_data *ha = vha->hw;
2357 unsigned long flags;
2358 struct qla_qpair *qpair = mcmd->qpair;
2359 bool free_mcmd = true;
2361 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf013,
2362 "TM response mcmd (%p) status %#x state %#x",
2363 mcmd, mcmd->fc_tm_rsp, mcmd->flags);
2365 spin_lock_irqsave(qpair->qp_lock_ptr, flags);
2367 if (!vha->flags.online || mcmd->reset_count != qpair->chip_reset) {
2369 * Either the port is not online or this request was from
2370 * previous life, just abort the processing.
2372 ql_dbg(ql_dbg_async, vha, 0xe100,
2373 "RESET-TMR online/active/old-count/new-count = %d/%d/%d/%d.\n",
2374 vha->flags.online, qla2x00_reset_active(vha),
2375 mcmd->reset_count, qpair->chip_reset);
2376 ha->tgt.tgt_ops->free_mcmd(mcmd);
2377 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
2381 if (mcmd->flags == QLA24XX_MGMT_SEND_NACK) {
2382 switch (mcmd->orig_iocb.imm_ntfy.u.isp24.status_subcode) {
2386 ql_dbg(ql_dbg_disc, vha, 0x2106,
2387 "TM response logo %phC status %#x state %#x",
2388 mcmd->sess->port_name, mcmd->fc_tm_rsp,
2390 qlt_schedule_sess_for_deletion(mcmd->sess);
2393 qlt_send_notify_ack(vha->hw->base_qpair,
2394 &mcmd->orig_iocb.imm_ntfy, 0, 0, 0, 0, 0, 0);
2398 if (mcmd->orig_iocb.atio.u.raw.entry_type == ABTS_RECV_24XX) {
2399 qlt_build_abts_resp_iocb(mcmd);
2402 qlt_24xx_send_task_mgmt_ctio(qpair, mcmd,
2406 * Make the callback for ->free_mcmd() to queue_work() and invoke
2407 * target_put_sess_cmd() to drop cmd_kref to 1. The final
2408 * target_put_sess_cmd() call will be made from TFO->check_stop_free()
2409 * -> tcm_qla2xxx_check_stop_free() to release the TMR associated se_cmd
2410 * descriptor after TFO->queue_tm_rsp() -> tcm_qla2xxx_queue_tm_rsp() ->
2411 * qlt_xmit_tm_rsp() returns here..
2414 ha->tgt.tgt_ops->free_mcmd(mcmd);
2416 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
2418 EXPORT_SYMBOL(qlt_xmit_tm_rsp);
2421 static int qlt_pci_map_calc_cnt(struct qla_tgt_prm *prm)
2423 struct qla_tgt_cmd *cmd = prm->cmd;
2425 BUG_ON(cmd->sg_cnt == 0);
2427 prm->sg = (struct scatterlist *)cmd->sg;
2428 prm->seg_cnt = dma_map_sg(&cmd->qpair->pdev->dev, cmd->sg,
2429 cmd->sg_cnt, cmd->dma_data_direction);
2430 if (unlikely(prm->seg_cnt == 0))
2433 prm->cmd->sg_mapped = 1;
2435 if (cmd->se_cmd.prot_op == TARGET_PROT_NORMAL) {
2437 * If greater than four sg entries then we need to allocate
2438 * the continuation entries
2440 if (prm->seg_cnt > QLA_TGT_DATASEGS_PER_CMD_24XX)
2441 prm->req_cnt += DIV_ROUND_UP(prm->seg_cnt -
2442 QLA_TGT_DATASEGS_PER_CMD_24XX,
2443 QLA_TGT_DATASEGS_PER_CONT_24XX);
2446 if ((cmd->se_cmd.prot_op == TARGET_PROT_DIN_INSERT) ||
2447 (cmd->se_cmd.prot_op == TARGET_PROT_DOUT_STRIP)) {
2448 prm->seg_cnt = DIV_ROUND_UP(cmd->bufflen, cmd->blk_sz);
2449 prm->tot_dsds = prm->seg_cnt;
2451 prm->tot_dsds = prm->seg_cnt;
2453 if (cmd->prot_sg_cnt) {
2454 prm->prot_sg = cmd->prot_sg;
2455 prm->prot_seg_cnt = dma_map_sg(&cmd->qpair->pdev->dev,
2456 cmd->prot_sg, cmd->prot_sg_cnt,
2457 cmd->dma_data_direction);
2458 if (unlikely(prm->prot_seg_cnt == 0))
2461 if ((cmd->se_cmd.prot_op == TARGET_PROT_DIN_INSERT) ||
2462 (cmd->se_cmd.prot_op == TARGET_PROT_DOUT_STRIP)) {
2463 /* Dif Bundling not support here */
2464 prm->prot_seg_cnt = DIV_ROUND_UP(cmd->bufflen,
2466 prm->tot_dsds += prm->prot_seg_cnt;
2468 prm->tot_dsds += prm->prot_seg_cnt;
2475 ql_dbg_qp(ql_dbg_tgt, prm->cmd->qpair, 0xe04d,
2476 "qla_target(%d): PCI mapping failed: sg_cnt=%d",
2477 0, prm->cmd->sg_cnt);
2481 static void qlt_unmap_sg(struct scsi_qla_host *vha, struct qla_tgt_cmd *cmd)
2483 struct qla_hw_data *ha;
2484 struct qla_qpair *qpair;
2485 if (!cmd->sg_mapped)
2490 dma_unmap_sg(&qpair->pdev->dev, cmd->sg, cmd->sg_cnt,
2491 cmd->dma_data_direction);
2494 if (cmd->prot_sg_cnt)
2495 dma_unmap_sg(&qpair->pdev->dev, cmd->prot_sg, cmd->prot_sg_cnt,
2496 cmd->dma_data_direction);
2501 if (cmd->ctx_dsd_alloced)
2502 qla2x00_clean_dsd_pool(ha, cmd->ctx);
2504 dma_pool_free(ha->dl_dma_pool, cmd->ctx, cmd->ctx->crc_ctx_dma);
2507 static int qlt_check_reserve_free_req(struct qla_qpair *qpair,
2511 struct req_que *req = qpair->req;
2513 if (req->cnt < (req_cnt + 2)) {
2514 cnt = (uint16_t)(qpair->use_shadow_reg ? *req->out_ptr :
2515 RD_REG_DWORD_RELAXED(req->req_q_out));
2517 if (req->ring_index < cnt)
2518 req->cnt = cnt - req->ring_index;
2520 req->cnt = req->length - (req->ring_index - cnt);
2522 if (unlikely(req->cnt < (req_cnt + 2)))
2526 req->cnt -= req_cnt;
2532 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
2534 static inline void *qlt_get_req_pkt(struct req_que *req)
2536 /* Adjust ring index. */
2538 if (req->ring_index == req->length) {
2539 req->ring_index = 0;
2540 req->ring_ptr = req->ring;
2544 return (cont_entry_t *)req->ring_ptr;
2547 /* ha->hardware_lock supposed to be held on entry */
2548 static inline uint32_t qlt_make_handle(struct qla_qpair *qpair)
2553 struct req_que *req = qpair->req;
2555 h = req->current_outstanding_cmd;
2557 for (index = 1; index < req->num_outstanding_cmds; index++) {
2559 if (h == req->num_outstanding_cmds)
2562 if (h == QLA_TGT_SKIP_HANDLE)
2565 if (!req->outstanding_cmds[h]) {
2572 req->current_outstanding_cmd = h;
2574 ql_dbg(ql_dbg_io, qpair->vha, 0x305b,
2575 "qla_target(%d): Ran out of empty cmd slots\n",
2576 qpair->vha->vp_idx);
2577 h = QLA_TGT_NULL_HANDLE;
2583 /* ha->hardware_lock supposed to be held on entry */
2584 static int qlt_24xx_build_ctio_pkt(struct qla_qpair *qpair,
2585 struct qla_tgt_prm *prm)
2588 struct ctio7_to_24xx *pkt;
2589 struct atio_from_isp *atio = &prm->cmd->atio;
2592 pkt = (struct ctio7_to_24xx *)qpair->req->ring_ptr;
2594 memset(pkt, 0, sizeof(*pkt));
2596 pkt->entry_type = CTIO_TYPE7;
2597 pkt->entry_count = (uint8_t)prm->req_cnt;
2598 pkt->vp_index = prm->cmd->vp_idx;
2600 h = qlt_make_handle(qpair);
2601 if (unlikely(h == QLA_TGT_NULL_HANDLE)) {
2603 * CTIO type 7 from the firmware doesn't provide a way to
2604 * know the initiator's LOOP ID, hence we can't find
2605 * the session and, so, the command.
2609 qpair->req->outstanding_cmds[h] = (srb_t *)prm->cmd;
2611 pkt->handle = MAKE_HANDLE(qpair->req->id, h);
2612 pkt->handle |= CTIO_COMPLETION_HANDLE_MARK;
2613 pkt->nport_handle = cpu_to_le16(prm->cmd->loop_id);
2614 pkt->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
2615 pkt->initiator_id[0] = atio->u.isp24.fcp_hdr.s_id[2];
2616 pkt->initiator_id[1] = atio->u.isp24.fcp_hdr.s_id[1];
2617 pkt->initiator_id[2] = atio->u.isp24.fcp_hdr.s_id[0];
2618 pkt->exchange_addr = atio->u.isp24.exchange_addr;
2619 temp = atio->u.isp24.attr << 9;
2620 pkt->u.status0.flags |= cpu_to_le16(temp);
2621 temp = be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id);
2622 pkt->u.status0.ox_id = cpu_to_le16(temp);
2623 pkt->u.status0.relative_offset = cpu_to_le32(prm->cmd->offset);
2629 * ha->hardware_lock supposed to be held on entry. We have already made sure
2630 * that there is sufficient amount of request entries to not drop it.
2632 static void qlt_load_cont_data_segments(struct qla_tgt_prm *prm)
2635 uint32_t *dword_ptr;
2637 /* Build continuation packets */
2638 while (prm->seg_cnt > 0) {
2639 cont_a64_entry_t *cont_pkt64 =
2640 (cont_a64_entry_t *)qlt_get_req_pkt(
2641 prm->cmd->qpair->req);
2644 * Make sure that from cont_pkt64 none of
2645 * 64-bit specific fields used for 32-bit
2646 * addressing. Cast to (cont_entry_t *) for
2650 memset(cont_pkt64, 0, sizeof(*cont_pkt64));
2652 cont_pkt64->entry_count = 1;
2653 cont_pkt64->sys_define = 0;
2655 cont_pkt64->entry_type = CONTINUE_A64_TYPE;
2656 dword_ptr = (uint32_t *)&cont_pkt64->dseg_0_address;
2658 /* Load continuation entry data segments */
2660 cnt < QLA_TGT_DATASEGS_PER_CONT_24XX && prm->seg_cnt;
2661 cnt++, prm->seg_cnt--) {
2663 cpu_to_le32(lower_32_bits
2664 (sg_dma_address(prm->sg)));
2665 *dword_ptr++ = cpu_to_le32(upper_32_bits
2666 (sg_dma_address(prm->sg)));
2667 *dword_ptr++ = cpu_to_le32(sg_dma_len(prm->sg));
2669 prm->sg = sg_next(prm->sg);
2675 * ha->hardware_lock supposed to be held on entry. We have already made sure
2676 * that there is sufficient amount of request entries to not drop it.
2678 static void qlt_load_data_segments(struct qla_tgt_prm *prm)
2681 uint32_t *dword_ptr;
2682 struct ctio7_to_24xx *pkt24 = (struct ctio7_to_24xx *)prm->pkt;
2684 pkt24->u.status0.transfer_length = cpu_to_le32(prm->cmd->bufflen);
2686 /* Setup packet address segment pointer */
2687 dword_ptr = pkt24->u.status0.dseg_0_address;
2689 /* Set total data segment count */
2691 pkt24->dseg_count = cpu_to_le16(prm->seg_cnt);
2693 if (prm->seg_cnt == 0) {
2694 /* No data transfer */
2700 /* If scatter gather */
2702 /* Load command entry data segments */
2704 (cnt < QLA_TGT_DATASEGS_PER_CMD_24XX) && prm->seg_cnt;
2705 cnt++, prm->seg_cnt--) {
2707 cpu_to_le32(lower_32_bits(sg_dma_address(prm->sg)));
2709 *dword_ptr++ = cpu_to_le32(upper_32_bits(
2710 sg_dma_address(prm->sg)));
2712 *dword_ptr++ = cpu_to_le32(sg_dma_len(prm->sg));
2714 prm->sg = sg_next(prm->sg);
2717 qlt_load_cont_data_segments(prm);
2720 static inline int qlt_has_data(struct qla_tgt_cmd *cmd)
2722 return cmd->bufflen > 0;
2725 static void qlt_print_dif_err(struct qla_tgt_prm *prm)
2727 struct qla_tgt_cmd *cmd;
2728 struct scsi_qla_host *vha;
2730 /* asc 0x10=dif error */
2731 if (prm->sense_buffer && (prm->sense_buffer[12] == 0x10)) {
2735 switch (prm->sense_buffer[13]) {
2737 ql_dbg(ql_dbg_tgt_dif, vha, 0xe00b,
2738 "BE detected Guard TAG ERR: lba[0x%llx|%lld] len[0x%x] "
2739 "se_cmd=%p tag[%x]",
2740 cmd->lba, cmd->lba, cmd->num_blks, &cmd->se_cmd,
2741 cmd->atio.u.isp24.exchange_addr);
2744 ql_dbg(ql_dbg_tgt_dif, vha, 0xe00c,
2745 "BE detected APP TAG ERR: lba[0x%llx|%lld] len[0x%x] "
2746 "se_cmd=%p tag[%x]",
2747 cmd->lba, cmd->lba, cmd->num_blks, &cmd->se_cmd,
2748 cmd->atio.u.isp24.exchange_addr);
2751 ql_dbg(ql_dbg_tgt_dif, vha, 0xe00f,
2752 "BE detected REF TAG ERR: lba[0x%llx|%lld] len[0x%x] "
2753 "se_cmd=%p tag[%x]",
2754 cmd->lba, cmd->lba, cmd->num_blks, &cmd->se_cmd,
2755 cmd->atio.u.isp24.exchange_addr);
2758 ql_dbg(ql_dbg_tgt_dif, vha, 0xe010,
2759 "BE detected Dif ERR: lba[%llx|%lld] len[%x] "
2760 "se_cmd=%p tag[%x]",
2761 cmd->lba, cmd->lba, cmd->num_blks, &cmd->se_cmd,
2762 cmd->atio.u.isp24.exchange_addr);
2765 ql_dump_buffer(ql_dbg_tgt_dif, vha, 0xe011, cmd->cdb, 16);
2770 * Called without ha->hardware_lock held
2772 static int qlt_pre_xmit_response(struct qla_tgt_cmd *cmd,
2773 struct qla_tgt_prm *prm, int xmit_type, uint8_t scsi_status,
2774 uint32_t *full_req_cnt)
2776 struct se_cmd *se_cmd = &cmd->se_cmd;
2777 struct qla_qpair *qpair = cmd->qpair;
2780 prm->tgt = cmd->tgt;
2782 prm->rq_result = scsi_status;
2783 prm->sense_buffer = &cmd->sense_buffer[0];
2784 prm->sense_buffer_len = TRANSPORT_SENSE_BUFFER;
2789 prm->add_status_pkt = 0;
2790 prm->prot_sg = NULL;
2791 prm->prot_seg_cnt = 0;
2794 if ((xmit_type & QLA_TGT_XMIT_DATA) && qlt_has_data(cmd)) {
2795 if (qlt_pci_map_calc_cnt(prm) != 0)
2799 *full_req_cnt = prm->req_cnt;
2801 if (se_cmd->se_cmd_flags & SCF_UNDERFLOW_BIT) {
2802 prm->residual = se_cmd->residual_count;
2803 ql_dbg_qp(ql_dbg_io + ql_dbg_verbose, qpair, 0x305c,
2804 "Residual underflow: %d (tag %lld, op %x, bufflen %d, rq_result %x)\n",
2805 prm->residual, se_cmd->tag,
2806 se_cmd->t_task_cdb ? se_cmd->t_task_cdb[0] : 0,
2807 cmd->bufflen, prm->rq_result);
2808 prm->rq_result |= SS_RESIDUAL_UNDER;
2809 } else if (se_cmd->se_cmd_flags & SCF_OVERFLOW_BIT) {
2810 prm->residual = se_cmd->residual_count;
2811 ql_dbg_qp(ql_dbg_io, qpair, 0x305d,
2812 "Residual overflow: %d (tag %lld, op %x, bufflen %d, rq_result %x)\n",
2813 prm->residual, se_cmd->tag, se_cmd->t_task_cdb ?
2814 se_cmd->t_task_cdb[0] : 0, cmd->bufflen, prm->rq_result);
2815 prm->rq_result |= SS_RESIDUAL_OVER;
2818 if (xmit_type & QLA_TGT_XMIT_STATUS) {
2820 * If QLA_TGT_XMIT_DATA is not set, add_status_pkt will be
2821 * ignored in *xmit_response() below
2823 if (qlt_has_data(cmd)) {
2824 if (QLA_TGT_SENSE_VALID(prm->sense_buffer) ||
2825 (IS_FWI2_CAPABLE(cmd->vha->hw) &&
2826 (prm->rq_result != 0))) {
2827 prm->add_status_pkt = 1;
2836 static inline int qlt_need_explicit_conf(struct qla_tgt_cmd *cmd,
2839 if (cmd->qpair->enable_class_2)
2843 return cmd->conf_compl_supported;
2845 return cmd->qpair->enable_explicit_conf &&
2846 cmd->conf_compl_supported;
2849 static void qlt_24xx_init_ctio_to_isp(struct ctio7_to_24xx *ctio,
2850 struct qla_tgt_prm *prm)
2852 prm->sense_buffer_len = min_t(uint32_t, prm->sense_buffer_len,
2853 (uint32_t)sizeof(ctio->u.status1.sense_data));
2854 ctio->u.status0.flags |= cpu_to_le16(CTIO7_FLAGS_SEND_STATUS);
2855 if (qlt_need_explicit_conf(prm->cmd, 0)) {
2856 ctio->u.status0.flags |= cpu_to_le16(
2857 CTIO7_FLAGS_EXPLICIT_CONFORM |
2858 CTIO7_FLAGS_CONFORM_REQ);
2860 ctio->u.status0.residual = cpu_to_le32(prm->residual);
2861 ctio->u.status0.scsi_status = cpu_to_le16(prm->rq_result);
2862 if (QLA_TGT_SENSE_VALID(prm->sense_buffer)) {
2865 if (qlt_need_explicit_conf(prm->cmd, 1)) {
2866 if ((prm->rq_result & SS_SCSI_STATUS_BYTE) != 0) {
2867 ql_dbg_qp(ql_dbg_tgt, prm->cmd->qpair, 0xe017,
2868 "Skipping EXPLICIT_CONFORM and "
2869 "CTIO7_FLAGS_CONFORM_REQ for FCP READ w/ "
2870 "non GOOD status\n");
2871 goto skip_explict_conf;
2873 ctio->u.status1.flags |= cpu_to_le16(
2874 CTIO7_FLAGS_EXPLICIT_CONFORM |
2875 CTIO7_FLAGS_CONFORM_REQ);
2878 ctio->u.status1.flags &=
2879 ~cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_0);
2880 ctio->u.status1.flags |=
2881 cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_1);
2882 ctio->u.status1.scsi_status |=
2883 cpu_to_le16(SS_SENSE_LEN_VALID);
2884 ctio->u.status1.sense_length =
2885 cpu_to_le16(prm->sense_buffer_len);
2886 for (i = 0; i < prm->sense_buffer_len/4; i++)
2887 ((uint32_t *)ctio->u.status1.sense_data)[i] =
2888 cpu_to_be32(((uint32_t *)prm->sense_buffer)[i]);
2890 qlt_print_dif_err(prm);
2893 ctio->u.status1.flags &=
2894 ~cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_0);
2895 ctio->u.status1.flags |=
2896 cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_1);
2897 ctio->u.status1.sense_length = 0;
2898 memset(ctio->u.status1.sense_data, 0,
2899 sizeof(ctio->u.status1.sense_data));
2902 /* Sense with len > 24, is it possible ??? */
2906 qlt_hba_err_chk_enabled(struct se_cmd *se_cmd)
2908 switch (se_cmd->prot_op) {
2909 case TARGET_PROT_DOUT_INSERT:
2910 case TARGET_PROT_DIN_STRIP:
2911 if (ql2xenablehba_err_chk >= 1)
2914 case TARGET_PROT_DOUT_PASS:
2915 case TARGET_PROT_DIN_PASS:
2916 if (ql2xenablehba_err_chk >= 2)
2919 case TARGET_PROT_DIN_INSERT:
2920 case TARGET_PROT_DOUT_STRIP:
2929 qla_tgt_ref_mask_check(struct se_cmd *se_cmd)
2931 switch (se_cmd->prot_op) {
2932 case TARGET_PROT_DIN_INSERT:
2933 case TARGET_PROT_DOUT_INSERT:
2934 case TARGET_PROT_DIN_STRIP:
2935 case TARGET_PROT_DOUT_STRIP:
2936 case TARGET_PROT_DIN_PASS:
2937 case TARGET_PROT_DOUT_PASS:
2946 * qla_tgt_set_dif_tags - Extract Ref and App tags from SCSI command
2949 qla_tgt_set_dif_tags(struct qla_tgt_cmd *cmd, struct crc_context *ctx,
2950 uint16_t *pfw_prot_opts)
2952 struct se_cmd *se_cmd = &cmd->se_cmd;
2953 uint32_t lba = 0xffffffff & se_cmd->t_task_lba;
2954 scsi_qla_host_t *vha = cmd->tgt->vha;
2955 struct qla_hw_data *ha = vha->hw;
2959 * wait till Mode Sense/Select cmd, modepage Ah, subpage 2
2960 * have been immplemented by TCM, before AppTag is avail.
2961 * Look for modesense_handlers[]
2964 ctx->app_tag_mask[0] = 0x0;
2965 ctx->app_tag_mask[1] = 0x0;
2967 if (IS_PI_UNINIT_CAPABLE(ha)) {
2968 if ((se_cmd->prot_type == TARGET_DIF_TYPE1_PROT) ||
2969 (se_cmd->prot_type == TARGET_DIF_TYPE2_PROT))
2970 *pfw_prot_opts |= PO_DIS_VALD_APP_ESC;
2971 else if (se_cmd->prot_type == TARGET_DIF_TYPE3_PROT)
2972 *pfw_prot_opts |= PO_DIS_VALD_APP_REF_ESC;
2975 t32 = ha->tgt.tgt_ops->get_dif_tags(cmd, pfw_prot_opts);
2977 switch (se_cmd->prot_type) {
2978 case TARGET_DIF_TYPE0_PROT:
2980 * No check for ql2xenablehba_err_chk, as it
2981 * would be an I/O error if hba tag generation
2984 ctx->ref_tag = cpu_to_le32(lba);
2985 /* enable ALL bytes of the ref tag */
2986 ctx->ref_tag_mask[0] = 0xff;
2987 ctx->ref_tag_mask[1] = 0xff;
2988 ctx->ref_tag_mask[2] = 0xff;
2989 ctx->ref_tag_mask[3] = 0xff;
2991 case TARGET_DIF_TYPE1_PROT:
2993 * For TYPE 1 protection: 16 bit GUARD tag, 32 bit
2994 * REF tag, and 16 bit app tag.
2996 ctx->ref_tag = cpu_to_le32(lba);
2997 if (!qla_tgt_ref_mask_check(se_cmd) ||
2998 !(ha->tgt.tgt_ops->chk_dif_tags(t32))) {
2999 *pfw_prot_opts |= PO_DIS_REF_TAG_VALD;
3002 /* enable ALL bytes of the ref tag */
3003 ctx->ref_tag_mask[0] = 0xff;
3004 ctx->ref_tag_mask[1] = 0xff;
3005 ctx->ref_tag_mask[2] = 0xff;
3006 ctx->ref_tag_mask[3] = 0xff;
3008 case TARGET_DIF_TYPE2_PROT:
3010 * For TYPE 2 protection: 16 bit GUARD + 32 bit REF
3011 * tag has to match LBA in CDB + N
3013 ctx->ref_tag = cpu_to_le32(lba);
3014 if (!qla_tgt_ref_mask_check(se_cmd) ||
3015 !(ha->tgt.tgt_ops->chk_dif_tags(t32))) {
3016 *pfw_prot_opts |= PO_DIS_REF_TAG_VALD;
3019 /* enable ALL bytes of the ref tag */
3020 ctx->ref_tag_mask[0] = 0xff;
3021 ctx->ref_tag_mask[1] = 0xff;
3022 ctx->ref_tag_mask[2] = 0xff;
3023 ctx->ref_tag_mask[3] = 0xff;
3025 case TARGET_DIF_TYPE3_PROT:
3026 /* For TYPE 3 protection: 16 bit GUARD only */
3027 *pfw_prot_opts |= PO_DIS_REF_TAG_VALD;
3028 ctx->ref_tag_mask[0] = ctx->ref_tag_mask[1] =
3029 ctx->ref_tag_mask[2] = ctx->ref_tag_mask[3] = 0x00;
3035 qlt_build_ctio_crc2_pkt(struct qla_qpair *qpair, struct qla_tgt_prm *prm)
3038 uint32_t transfer_length = 0;
3039 uint32_t data_bytes;
3041 uint8_t bundling = 1;
3042 struct crc_context *crc_ctx_pkt = NULL;
3043 struct qla_hw_data *ha;
3044 struct ctio_crc2_to_fw *pkt;
3045 dma_addr_t crc_ctx_dma;
3046 uint16_t fw_prot_opts = 0;
3047 struct qla_tgt_cmd *cmd = prm->cmd;
3048 struct se_cmd *se_cmd = &cmd->se_cmd;
3050 struct atio_from_isp *atio = &prm->cmd->atio;
3051 struct qla_tc_param tc;
3053 scsi_qla_host_t *vha = cmd->vha;
3057 pkt = (struct ctio_crc2_to_fw *)qpair->req->ring_ptr;
3059 memset(pkt, 0, sizeof(*pkt));
3061 ql_dbg_qp(ql_dbg_tgt, cmd->qpair, 0xe071,
3062 "qla_target(%d):%s: se_cmd[%p] CRC2 prot_op[0x%x] cmd prot sg:cnt[%p:%x] lba[%llu]\n",
3063 cmd->vp_idx, __func__, se_cmd, se_cmd->prot_op,
3064 prm->prot_sg, prm->prot_seg_cnt, se_cmd->t_task_lba);
3066 if ((se_cmd->prot_op == TARGET_PROT_DIN_INSERT) ||
3067 (se_cmd->prot_op == TARGET_PROT_DOUT_STRIP))
3070 /* Compute dif len and adjust data len to incude protection */
3071 data_bytes = cmd->bufflen;
3072 dif_bytes = (data_bytes / cmd->blk_sz) * 8;
3074 switch (se_cmd->prot_op) {
3075 case TARGET_PROT_DIN_INSERT:
3076 case TARGET_PROT_DOUT_STRIP:
3077 transfer_length = data_bytes;
3078 if (cmd->prot_sg_cnt)
3079 data_bytes += dif_bytes;
3081 case TARGET_PROT_DIN_STRIP:
3082 case TARGET_PROT_DOUT_INSERT:
3083 case TARGET_PROT_DIN_PASS:
3084 case TARGET_PROT_DOUT_PASS:
3085 transfer_length = data_bytes + dif_bytes;
3092 if (!qlt_hba_err_chk_enabled(se_cmd))
3093 fw_prot_opts |= 0x10; /* Disable Guard tag checking */
3094 /* HBA error checking enabled */
3095 else if (IS_PI_UNINIT_CAPABLE(ha)) {
3096 if ((se_cmd->prot_type == TARGET_DIF_TYPE1_PROT) ||
3097 (se_cmd->prot_type == TARGET_DIF_TYPE2_PROT))
3098 fw_prot_opts |= PO_DIS_VALD_APP_ESC;
3099 else if (se_cmd->prot_type == TARGET_DIF_TYPE3_PROT)
3100 fw_prot_opts |= PO_DIS_VALD_APP_REF_ESC;
3103 switch (se_cmd->prot_op) {
3104 case TARGET_PROT_DIN_INSERT:
3105 case TARGET_PROT_DOUT_INSERT:
3106 fw_prot_opts |= PO_MODE_DIF_INSERT;
3108 case TARGET_PROT_DIN_STRIP:
3109 case TARGET_PROT_DOUT_STRIP:
3110 fw_prot_opts |= PO_MODE_DIF_REMOVE;
3112 case TARGET_PROT_DIN_PASS:
3113 case TARGET_PROT_DOUT_PASS:
3114 fw_prot_opts |= PO_MODE_DIF_PASS;
3115 /* FUTURE: does tcm require T10CRC<->IPCKSUM conversion? */
3117 default:/* Normal Request */
3118 fw_prot_opts |= PO_MODE_DIF_PASS;
3123 /* Update entry type to indicate Command Type CRC_2 IOCB */
3124 pkt->entry_type = CTIO_CRC2;
3125 pkt->entry_count = 1;
3126 pkt->vp_index = cmd->vp_idx;
3128 h = qlt_make_handle(qpair);
3129 if (unlikely(h == QLA_TGT_NULL_HANDLE)) {
3131 * CTIO type 7 from the firmware doesn't provide a way to
3132 * know the initiator's LOOP ID, hence we can't find
3133 * the session and, so, the command.
3137 qpair->req->outstanding_cmds[h] = (srb_t *)prm->cmd;
3139 pkt->handle = MAKE_HANDLE(qpair->req->id, h);
3140 pkt->handle |= CTIO_COMPLETION_HANDLE_MARK;
3141 pkt->nport_handle = cpu_to_le16(prm->cmd->loop_id);
3142 pkt->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
3143 pkt->initiator_id[0] = atio->u.isp24.fcp_hdr.s_id[2];
3144 pkt->initiator_id[1] = atio->u.isp24.fcp_hdr.s_id[1];
3145 pkt->initiator_id[2] = atio->u.isp24.fcp_hdr.s_id[0];
3146 pkt->exchange_addr = atio->u.isp24.exchange_addr;
3148 /* silence compile warning */
3149 t16 = be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id);
3150 pkt->ox_id = cpu_to_le16(t16);
3152 t16 = (atio->u.isp24.attr << 9);
3153 pkt->flags |= cpu_to_le16(t16);
3154 pkt->relative_offset = cpu_to_le32(prm->cmd->offset);
3156 /* Set transfer direction */
3157 if (cmd->dma_data_direction == DMA_TO_DEVICE)
3158 pkt->flags = cpu_to_le16(CTIO7_FLAGS_DATA_IN);
3159 else if (cmd->dma_data_direction == DMA_FROM_DEVICE)
3160 pkt->flags = cpu_to_le16(CTIO7_FLAGS_DATA_OUT);
3162 pkt->dseg_count = prm->tot_dsds;
3163 /* Fibre channel byte count */
3164 pkt->transfer_length = cpu_to_le32(transfer_length);
3166 /* ----- CRC context -------- */
3168 /* Allocate CRC context from global pool */
3169 crc_ctx_pkt = cmd->ctx =
3170 dma_pool_zalloc(ha->dl_dma_pool, GFP_ATOMIC, &crc_ctx_dma);
3173 goto crc_queuing_error;
3175 crc_ctx_pkt->crc_ctx_dma = crc_ctx_dma;
3176 INIT_LIST_HEAD(&crc_ctx_pkt->dsd_list);
3179 crc_ctx_pkt->handle = pkt->handle;
3181 qla_tgt_set_dif_tags(cmd, crc_ctx_pkt, &fw_prot_opts);
3183 pkt->crc_context_address[0] = cpu_to_le32(LSD(crc_ctx_dma));
3184 pkt->crc_context_address[1] = cpu_to_le32(MSD(crc_ctx_dma));
3185 pkt->crc_context_len = CRC_CONTEXT_LEN_FW;
3188 cur_dsd = (uint32_t *) &crc_ctx_pkt->u.nobundling.data_address;
3191 * Configure Bundling if we need to fetch interlaving
3192 * protection PCI accesses
3194 fw_prot_opts |= PO_ENABLE_DIF_BUNDLING;
3195 crc_ctx_pkt->u.bundling.dif_byte_count = cpu_to_le32(dif_bytes);
3196 crc_ctx_pkt->u.bundling.dseg_count =
3197 cpu_to_le16(prm->tot_dsds - prm->prot_seg_cnt);
3198 cur_dsd = (uint32_t *) &crc_ctx_pkt->u.bundling.data_address;
3201 /* Finish the common fields of CRC pkt */
3202 crc_ctx_pkt->blk_size = cpu_to_le16(cmd->blk_sz);
3203 crc_ctx_pkt->prot_opts = cpu_to_le16(fw_prot_opts);
3204 crc_ctx_pkt->byte_count = cpu_to_le32(data_bytes);
3205 crc_ctx_pkt->guard_seed = cpu_to_le16(0);
3207 memset((uint8_t *)&tc, 0 , sizeof(tc));
3209 tc.blk_sz = cmd->blk_sz;
3210 tc.bufflen = cmd->bufflen;
3212 tc.prot_sg = cmd->prot_sg;
3213 tc.ctx = crc_ctx_pkt;
3214 tc.ctx_dsd_alloced = &cmd->ctx_dsd_alloced;
3216 /* Walks data segments */
3217 pkt->flags |= cpu_to_le16(CTIO7_FLAGS_DSD_PTR);
3219 if (!bundling && prm->prot_seg_cnt) {
3220 if (qla24xx_walk_and_build_sglist_no_difb(ha, NULL, cur_dsd,
3221 prm->tot_dsds, &tc))
3222 goto crc_queuing_error;
3223 } else if (qla24xx_walk_and_build_sglist(ha, NULL, cur_dsd,
3224 (prm->tot_dsds - prm->prot_seg_cnt), &tc))
3225 goto crc_queuing_error;
3227 if (bundling && prm->prot_seg_cnt) {
3228 /* Walks dif segments */
3229 pkt->add_flags |= CTIO_CRC2_AF_DIF_DSD_ENA;
3231 cur_dsd = (uint32_t *) &crc_ctx_pkt->u.bundling.dif_address;
3232 if (qla24xx_walk_and_build_prot_sglist(ha, NULL, cur_dsd,
3233 prm->prot_seg_cnt, cmd))
3234 goto crc_queuing_error;
3239 /* Cleanup will be performed by the caller */
3240 qpair->req->outstanding_cmds[h] = NULL;
3242 return QLA_FUNCTION_FAILED;
3246 * Callback to setup response of xmit_type of QLA_TGT_XMIT_DATA and *
3247 * QLA_TGT_XMIT_STATUS for >= 24xx silicon
3249 int qlt_xmit_response(struct qla_tgt_cmd *cmd, int xmit_type,
3250 uint8_t scsi_status)
3252 struct scsi_qla_host *vha = cmd->vha;
3253 struct qla_qpair *qpair = cmd->qpair;
3254 struct ctio7_to_24xx *pkt;
3255 struct qla_tgt_prm prm;
3256 uint32_t full_req_cnt = 0;
3257 unsigned long flags = 0;
3260 if (!qpair->fw_started || (cmd->reset_count != qpair->chip_reset) ||
3261 (cmd->sess && cmd->sess->deleted)) {
3262 cmd->state = QLA_TGT_STATE_PROCESSED;
3263 qlt_abort_cmd_on_host_reset(cmd->vha, cmd);
3267 ql_dbg_qp(ql_dbg_tgt, qpair, 0xe018,
3268 "is_send_status=%d, cmd->bufflen=%d, cmd->sg_cnt=%d, cmd->dma_data_direction=%d se_cmd[%p] qp %d\n",
3269 (xmit_type & QLA_TGT_XMIT_STATUS) ?
3270 1 : 0, cmd->bufflen, cmd->sg_cnt, cmd->dma_data_direction,
3271 &cmd->se_cmd, qpair->id);
3273 res = qlt_pre_xmit_response(cmd, &prm, xmit_type, scsi_status,
3275 if (unlikely(res != 0)) {
3279 spin_lock_irqsave(qpair->qp_lock_ptr, flags);
3281 if (xmit_type == QLA_TGT_XMIT_STATUS)
3282 qpair->tgt_counters.core_qla_snd_status++;
3284 qpair->tgt_counters.core_qla_que_buf++;
3286 if (!qpair->fw_started || cmd->reset_count != qpair->chip_reset) {
3288 * Either the port is not online or this request was from
3289 * previous life, just abort the processing.
3291 cmd->state = QLA_TGT_STATE_PROCESSED;
3292 qlt_abort_cmd_on_host_reset(cmd->vha, cmd);
3293 ql_dbg_qp(ql_dbg_async, qpair, 0xe101,
3294 "RESET-RSP online/active/old-count/new-count = %d/%d/%d/%d.\n",
3295 vha->flags.online, qla2x00_reset_active(vha),
3296 cmd->reset_count, qpair->chip_reset);
3297 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
3301 /* Does F/W have an IOCBs for this request */
3302 res = qlt_check_reserve_free_req(qpair, full_req_cnt);
3304 goto out_unmap_unlock;
3306 if (cmd->se_cmd.prot_op && (xmit_type & QLA_TGT_XMIT_DATA))
3307 res = qlt_build_ctio_crc2_pkt(qpair, &prm);
3309 res = qlt_24xx_build_ctio_pkt(qpair, &prm);
3310 if (unlikely(res != 0)) {
3311 qpair->req->cnt += full_req_cnt;
3312 goto out_unmap_unlock;
3315 pkt = (struct ctio7_to_24xx *)prm.pkt;
3317 if (qlt_has_data(cmd) && (xmit_type & QLA_TGT_XMIT_DATA)) {
3318 pkt->u.status0.flags |=
3319 cpu_to_le16(CTIO7_FLAGS_DATA_IN |
3320 CTIO7_FLAGS_STATUS_MODE_0);
3322 if (cmd->se_cmd.prot_op == TARGET_PROT_NORMAL)
3323 qlt_load_data_segments(&prm);
3325 if (prm.add_status_pkt == 0) {
3326 if (xmit_type & QLA_TGT_XMIT_STATUS) {
3327 pkt->u.status0.scsi_status =
3328 cpu_to_le16(prm.rq_result);
3329 pkt->u.status0.residual =
3330 cpu_to_le32(prm.residual);
3331 pkt->u.status0.flags |= cpu_to_le16(
3332 CTIO7_FLAGS_SEND_STATUS);
3333 if (qlt_need_explicit_conf(cmd, 0)) {
3334 pkt->u.status0.flags |=
3336 CTIO7_FLAGS_EXPLICIT_CONFORM |
3337 CTIO7_FLAGS_CONFORM_REQ);
3343 * We have already made sure that there is sufficient
3344 * amount of request entries to not drop HW lock in
3347 struct ctio7_to_24xx *ctio =
3348 (struct ctio7_to_24xx *)qlt_get_req_pkt(
3351 ql_dbg_qp(ql_dbg_tgt, qpair, 0x305e,
3352 "Building additional status packet 0x%p.\n",
3356 * T10Dif: ctio_crc2_to_fw overlay ontop of
3359 memcpy(ctio, pkt, sizeof(*ctio));
3360 /* reset back to CTIO7 */
3361 ctio->entry_count = 1;
3362 ctio->entry_type = CTIO_TYPE7;
3363 ctio->dseg_count = 0;
3364 ctio->u.status1.flags &= ~cpu_to_le16(
3365 CTIO7_FLAGS_DATA_IN);
3367 /* Real finish is ctio_m1's finish */
3368 pkt->handle |= CTIO_INTERMEDIATE_HANDLE_MARK;
3369 pkt->u.status0.flags |= cpu_to_le16(
3370 CTIO7_FLAGS_DONT_RET_CTIO);
3372 /* qlt_24xx_init_ctio_to_isp will correct
3373 * all neccessary fields that's part of CTIO7.
3374 * There should be no residual of CTIO-CRC2 data.
3376 qlt_24xx_init_ctio_to_isp((struct ctio7_to_24xx *)ctio,
3380 qlt_24xx_init_ctio_to_isp(pkt, &prm);
3383 cmd->state = QLA_TGT_STATE_PROCESSED; /* Mid-level is done processing */
3384 spin_lock(&cmd->cmd_lock);
3385 cmd->cmd_sent_to_fw = 1;
3386 spin_unlock(&cmd->cmd_lock);
3387 cmd->ctio_flags = le16_to_cpu(pkt->u.status0.flags);
3389 /* Memory Barrier */
3391 if (qpair->reqq_start_iocbs)
3392 qpair->reqq_start_iocbs(qpair);
3394 qla2x00_start_iocbs(vha, qpair->req);
3395 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
3400 qlt_unmap_sg(vha, cmd);
3401 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
3405 EXPORT_SYMBOL(qlt_xmit_response);
3407 int qlt_rdy_to_xfer(struct qla_tgt_cmd *cmd)
3409 struct ctio7_to_24xx *pkt;
3410 struct scsi_qla_host *vha = cmd->vha;
3411 struct qla_tgt *tgt = cmd->tgt;
3412 struct qla_tgt_prm prm;
3413 unsigned long flags = 0;
3415 struct qla_qpair *qpair = cmd->qpair;
3417 memset(&prm, 0, sizeof(prm));
3423 /* Calculate number of entries and segments required */
3424 if (qlt_pci_map_calc_cnt(&prm) != 0)
3427 if (!qpair->fw_started || (cmd->reset_count != qpair->chip_reset) ||
3428 (cmd->sess && cmd->sess->deleted)) {
3430 * Either the port is not online or this request was from
3431 * previous life, just abort the processing.
3433 cmd->state = QLA_TGT_STATE_NEED_DATA;
3434 qlt_abort_cmd_on_host_reset(cmd->vha, cmd);
3435 ql_dbg_qp(ql_dbg_async, qpair, 0xe102,
3436 "RESET-XFR online/active/old-count/new-count = %d/%d/%d/%d.\n",
3437 vha->flags.online, qla2x00_reset_active(vha),
3438 cmd->reset_count, qpair->chip_reset);
3442 spin_lock_irqsave(qpair->qp_lock_ptr, flags);
3443 /* Does F/W have an IOCBs for this request */
3444 res = qlt_check_reserve_free_req(qpair, prm.req_cnt);
3446 goto out_unlock_free_unmap;
3447 if (cmd->se_cmd.prot_op)
3448 res = qlt_build_ctio_crc2_pkt(qpair, &prm);
3450 res = qlt_24xx_build_ctio_pkt(qpair, &prm);
3452 if (unlikely(res != 0)) {
3453 qpair->req->cnt += prm.req_cnt;
3454 goto out_unlock_free_unmap;
3457 pkt = (struct ctio7_to_24xx *)prm.pkt;
3458 pkt->u.status0.flags |= cpu_to_le16(CTIO7_FLAGS_DATA_OUT |
3459 CTIO7_FLAGS_STATUS_MODE_0);
3461 if (cmd->se_cmd.prot_op == TARGET_PROT_NORMAL)
3462 qlt_load_data_segments(&prm);
3464 cmd->state = QLA_TGT_STATE_NEED_DATA;
3465 spin_lock(&cmd->cmd_lock);
3466 cmd->cmd_sent_to_fw = 1;
3467 spin_unlock(&cmd->cmd_lock);
3468 cmd->ctio_flags = le16_to_cpu(pkt->u.status0.flags);
3470 /* Memory Barrier */
3472 if (qpair->reqq_start_iocbs)
3473 qpair->reqq_start_iocbs(qpair);
3475 qla2x00_start_iocbs(vha, qpair->req);
3476 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
3480 out_unlock_free_unmap:
3481 qlt_unmap_sg(vha, cmd);
3482 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
3486 EXPORT_SYMBOL(qlt_rdy_to_xfer);
3490 * it is assumed either hardware_lock or qpair lock is held.
3493 qlt_handle_dif_error(struct qla_qpair *qpair, struct qla_tgt_cmd *cmd,
3494 struct ctio_crc_from_fw *sts)
3496 uint8_t *ap = &sts->actual_dif[0];
3497 uint8_t *ep = &sts->expected_dif[0];
3498 uint64_t lba = cmd->se_cmd.t_task_lba;
3499 uint8_t scsi_status, sense_key, asc, ascq;
3500 unsigned long flags;
3501 struct scsi_qla_host *vha = cmd->vha;
3503 cmd->trc_flags |= TRC_DIF_ERR;
3505 cmd->a_guard = be16_to_cpu(*(uint16_t *)(ap + 0));
3506 cmd->a_app_tag = be16_to_cpu(*(uint16_t *)(ap + 2));
3507 cmd->a_ref_tag = be32_to_cpu(*(uint32_t *)(ap + 4));
3509 cmd->e_guard = be16_to_cpu(*(uint16_t *)(ep + 0));
3510 cmd->e_app_tag = be16_to_cpu(*(uint16_t *)(ep + 2));
3511 cmd->e_ref_tag = be32_to_cpu(*(uint32_t *)(ep + 4));
3513 ql_dbg(ql_dbg_tgt_dif, vha, 0xf075,
3514 "%s: aborted %d state %d\n", __func__, cmd->aborted, cmd->state);
3516 scsi_status = sense_key = asc = ascq = 0;
3518 /* check appl tag */
3519 if (cmd->e_app_tag != cmd->a_app_tag) {
3520 ql_dbg(ql_dbg_tgt_dif, vha, 0xe00d,
3521 "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]",
3522 cmd->cdb[0], lba, (lba+cmd->num_blks), cmd->num_blks,
3523 cmd->a_ref_tag, cmd->e_ref_tag, cmd->a_app_tag,
3524 cmd->e_app_tag, cmd->a_guard, cmd->e_guard, cmd,
3525 cmd->atio.u.isp24.fcp_hdr.ox_id);
3527 cmd->dif_err_code = DIF_ERR_APP;
3528 scsi_status = SAM_STAT_CHECK_CONDITION;
3529 sense_key = ABORTED_COMMAND;
3535 if (cmd->e_ref_tag != cmd->a_ref_tag) {
3536 ql_dbg(ql_dbg_tgt_dif, vha, 0xe00e,
3537 "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] ",
3538 cmd->cdb[0], lba, (lba+cmd->num_blks), cmd->num_blks,
3539 cmd->a_ref_tag, cmd->e_ref_tag, cmd->a_app_tag,
3540 cmd->e_app_tag, cmd->a_guard, cmd->e_guard, cmd,
3541 cmd->atio.u.isp24.fcp_hdr.ox_id);
3543 cmd->dif_err_code = DIF_ERR_REF;
3544 scsi_status = SAM_STAT_CHECK_CONDITION;
3545 sense_key = ABORTED_COMMAND;
3552 if (cmd->e_guard != cmd->a_guard) {
3553 ql_dbg(ql_dbg_tgt_dif, vha, 0xe012,
3554 "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]",
3555 cmd->cdb[0], lba, (lba+cmd->num_blks), cmd->num_blks,
3556 cmd->a_ref_tag, cmd->e_ref_tag, cmd->a_app_tag,
3557 cmd->e_app_tag, cmd->a_guard, cmd->e_guard, cmd,
3558 cmd->atio.u.isp24.fcp_hdr.ox_id);
3560 cmd->dif_err_code = DIF_ERR_GRD;
3561 scsi_status = SAM_STAT_CHECK_CONDITION;
3562 sense_key = ABORTED_COMMAND;
3567 switch (cmd->state) {
3568 case QLA_TGT_STATE_NEED_DATA:
3569 /* handle_data will load DIF error code */
3570 cmd->state = QLA_TGT_STATE_DATA_IN;
3571 vha->hw->tgt.tgt_ops->handle_data(cmd);
3574 spin_lock_irqsave(&cmd->cmd_lock, flags);
3576 spin_unlock_irqrestore(&cmd->cmd_lock, flags);
3577 vha->hw->tgt.tgt_ops->free_cmd(cmd);
3580 spin_unlock_irqrestore(&cmd->cmd_lock, flags);
3582 qlt_send_resp_ctio(qpair, cmd, scsi_status, sense_key, asc,
3584 /* assume scsi status gets out on the wire.
3585 * Will not wait for completion.
3587 vha->hw->tgt.tgt_ops->free_cmd(cmd);
3592 /* If hardware_lock held on entry, might drop it, then reaquire */
3593 /* This function sends the appropriate CTIO to ISP 2xxx or 24xx */
3594 static int __qlt_send_term_imm_notif(struct scsi_qla_host *vha,
3595 struct imm_ntfy_from_isp *ntfy)
3597 struct nack_to_isp *nack;
3598 struct qla_hw_data *ha = vha->hw;
3602 ql_dbg(ql_dbg_tgt_tmr, vha, 0xe01c,
3603 "Sending TERM ELS CTIO (ha=%p)\n", ha);
3605 pkt = (request_t *)qla2x00_alloc_iocbs(vha, NULL);
3607 ql_dbg(ql_dbg_tgt, vha, 0xe080,
3608 "qla_target(%d): %s failed: unable to allocate "
3609 "request packet\n", vha->vp_idx, __func__);
3613 pkt->entry_type = NOTIFY_ACK_TYPE;
3614 pkt->entry_count = 1;
3615 pkt->handle = QLA_TGT_SKIP_HANDLE;
3617 nack = (struct nack_to_isp *)pkt;
3618 nack->ox_id = ntfy->ox_id;
3620 nack->u.isp24.nport_handle = ntfy->u.isp24.nport_handle;
3621 if (le16_to_cpu(ntfy->u.isp24.status) == IMM_NTFY_ELS) {
3622 nack->u.isp24.flags = ntfy->u.isp24.flags &
3623 __constant_cpu_to_le32(NOTIFY24XX_FLAGS_PUREX_IOCB);
3627 nack->u.isp24.flags |=
3628 __constant_cpu_to_le16(NOTIFY_ACK_FLAGS_TERMINATE);
3630 nack->u.isp24.srr_rx_id = ntfy->u.isp24.srr_rx_id;
3631 nack->u.isp24.status = ntfy->u.isp24.status;
3632 nack->u.isp24.status_subcode = ntfy->u.isp24.status_subcode;
3633 nack->u.isp24.fw_handle = ntfy->u.isp24.fw_handle;
3634 nack->u.isp24.exchange_address = ntfy->u.isp24.exchange_address;
3635 nack->u.isp24.srr_rel_offs = ntfy->u.isp24.srr_rel_offs;
3636 nack->u.isp24.srr_ui = ntfy->u.isp24.srr_ui;
3637 nack->u.isp24.vp_index = ntfy->u.isp24.vp_index;
3639 qla2x00_start_iocbs(vha, vha->req);
3643 static void qlt_send_term_imm_notif(struct scsi_qla_host *vha,
3644 struct imm_ntfy_from_isp *imm, int ha_locked)
3646 unsigned long flags = 0;
3650 rc = __qlt_send_term_imm_notif(vha, imm);
3654 qlt_alloc_qfull_cmd(vha, imm, 0, 0);
3662 spin_lock_irqsave(&vha->hw->hardware_lock, flags);
3663 rc = __qlt_send_term_imm_notif(vha, imm);
3667 qlt_alloc_qfull_cmd(vha, imm, 0, 0);
3672 spin_unlock_irqrestore(&vha->hw->hardware_lock, flags);
3676 * If hardware_lock held on entry, might drop it, then reaquire
3677 * This function sends the appropriate CTIO to ISP 2xxx or 24xx
3679 static int __qlt_send_term_exchange(struct qla_qpair *qpair,
3680 struct qla_tgt_cmd *cmd,
3681 struct atio_from_isp *atio)
3683 struct scsi_qla_host *vha = qpair->vha;
3684 struct ctio7_to_24xx *ctio24;
3685 struct qla_hw_data *ha = vha->hw;
3690 ql_dbg(ql_dbg_tgt, vha, 0xe009, "Sending TERM EXCH CTIO (ha=%p)\n", ha);
3695 pkt = (request_t *)qla2x00_alloc_iocbs_ready(qpair, NULL);
3697 ql_dbg(ql_dbg_tgt, vha, 0xe050,
3698 "qla_target(%d): %s failed: unable to allocate "
3699 "request packet\n", vha->vp_idx, __func__);
3704 if (cmd->state < QLA_TGT_STATE_PROCESSED) {
3705 ql_dbg(ql_dbg_tgt, vha, 0xe051,
3706 "qla_target(%d): Terminating cmd %p with "
3707 "incorrect state %d\n", vha->vp_idx, cmd,
3713 qpair->tgt_counters.num_term_xchg_sent++;
3714 pkt->entry_count = 1;
3715 pkt->handle = QLA_TGT_SKIP_HANDLE | CTIO_COMPLETION_HANDLE_MARK;
3717 ctio24 = (struct ctio7_to_24xx *)pkt;
3718 ctio24->entry_type = CTIO_TYPE7;
3719 ctio24->nport_handle = CTIO7_NHANDLE_UNRECOGNIZED;
3720 ctio24->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
3721 ctio24->vp_index = vha->vp_idx;
3722 ctio24->initiator_id[0] = atio->u.isp24.fcp_hdr.s_id[2];
3723 ctio24->initiator_id[1] = atio->u.isp24.fcp_hdr.s_id[1];
3724 ctio24->initiator_id[2] = atio->u.isp24.fcp_hdr.s_id[0];
3725 ctio24->exchange_addr = atio->u.isp24.exchange_addr;
3726 temp = (atio->u.isp24.attr << 9) | CTIO7_FLAGS_STATUS_MODE_1 |
3727 CTIO7_FLAGS_TERMINATE;
3728 ctio24->u.status1.flags = cpu_to_le16(temp);
3729 temp = be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id);
3730 ctio24->u.status1.ox_id = cpu_to_le16(temp);
3732 /* Memory Barrier */
3734 if (qpair->reqq_start_iocbs)
3735 qpair->reqq_start_iocbs(qpair);
3737 qla2x00_start_iocbs(vha, qpair->req);
3741 static void qlt_send_term_exchange(struct qla_qpair *qpair,
3742 struct qla_tgt_cmd *cmd, struct atio_from_isp *atio, int ha_locked,
3745 struct scsi_qla_host *vha;
3746 unsigned long flags = 0;
3749 /* why use different vha? NPIV */
3756 rc = __qlt_send_term_exchange(qpair, cmd, atio);
3758 qlt_alloc_qfull_cmd(vha, atio, 0, 0);
3761 spin_lock_irqsave(qpair->qp_lock_ptr, flags);
3762 rc = __qlt_send_term_exchange(qpair, cmd, atio);
3764 qlt_alloc_qfull_cmd(vha, atio, 0, 0);
3767 if (cmd && !ul_abort && !cmd->aborted) {
3769 qlt_unmap_sg(vha, cmd);
3770 vha->hw->tgt.tgt_ops->free_cmd(cmd);
3774 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
3779 static void qlt_init_term_exchange(struct scsi_qla_host *vha)
3781 struct list_head free_list;
3782 struct qla_tgt_cmd *cmd, *tcmd;
3784 vha->hw->tgt.leak_exchg_thresh_hold =
3785 (vha->hw->cur_fw_xcb_count/100) * LEAK_EXCHG_THRESH_HOLD_PERCENT;
3788 if (!list_empty(&vha->hw->tgt.q_full_list)) {
3789 INIT_LIST_HEAD(&free_list);
3790 list_splice_init(&vha->hw->tgt.q_full_list, &free_list);
3792 list_for_each_entry_safe(cmd, tcmd, &free_list, cmd_list) {
3793 list_del(&cmd->cmd_list);
3794 /* This cmd was never sent to TCM. There is no need
3795 * to schedule free or call free_cmd
3798 vha->hw->tgt.num_qfull_cmds_alloc--;
3801 vha->hw->tgt.num_qfull_cmds_dropped = 0;
3804 static void qlt_chk_exch_leak_thresh_hold(struct scsi_qla_host *vha)
3806 uint32_t total_leaked;
3808 total_leaked = vha->hw->tgt.num_qfull_cmds_dropped;
3810 if (vha->hw->tgt.leak_exchg_thresh_hold &&
3811 (total_leaked > vha->hw->tgt.leak_exchg_thresh_hold)) {
3813 ql_dbg(ql_dbg_tgt, vha, 0xe079,
3814 "Chip reset due to exchange starvation: %d/%d.\n",
3815 total_leaked, vha->hw->cur_fw_xcb_count);
3817 if (IS_P3P_TYPE(vha->hw))
3818 set_bit(FCOE_CTX_RESET_NEEDED, &vha->dpc_flags);
3820 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
3821 qla2xxx_wake_dpc(vha);
3826 int qlt_abort_cmd(struct qla_tgt_cmd *cmd)
3828 struct qla_tgt *tgt = cmd->tgt;
3829 struct scsi_qla_host *vha = tgt->vha;
3830 struct se_cmd *se_cmd = &cmd->se_cmd;
3831 unsigned long flags;
3833 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf014,
3834 "qla_target(%d): terminating exchange for aborted cmd=%p "
3835 "(se_cmd=%p, tag=%llu)", vha->vp_idx, cmd, &cmd->se_cmd,
3838 spin_lock_irqsave(&cmd->cmd_lock, flags);
3840 spin_unlock_irqrestore(&cmd->cmd_lock, flags);
3842 * It's normal to see 2 calls in this path:
3843 * 1) XFER Rdy completion + CMD_T_ABORT
3844 * 2) TCM TMR - drain_state_list
3846 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf016,
3847 "multiple abort. %p transport_state %x, t_state %x, "
3848 "se_cmd_flags %x\n", cmd, cmd->se_cmd.transport_state,
3849 cmd->se_cmd.t_state, cmd->se_cmd.se_cmd_flags);
3853 cmd->trc_flags |= TRC_ABORT;
3854 spin_unlock_irqrestore(&cmd->cmd_lock, flags);
3856 qlt_send_term_exchange(cmd->qpair, cmd, &cmd->atio, 0, 1);
3859 EXPORT_SYMBOL(qlt_abort_cmd);
3861 void qlt_free_cmd(struct qla_tgt_cmd *cmd)
3863 struct fc_port *sess = cmd->sess;
3865 ql_dbg(ql_dbg_tgt, cmd->vha, 0xe074,
3866 "%s: se_cmd[%p] ox_id %04x\n",
3867 __func__, &cmd->se_cmd,
3868 be16_to_cpu(cmd->atio.u.isp24.fcp_hdr.ox_id));
3870 BUG_ON(cmd->cmd_in_wq);
3873 qlt_unmap_sg(cmd->vha, cmd);
3876 qlt_decr_num_pend_cmds(cmd->vha);
3878 BUG_ON(cmd->sg_mapped);
3879 cmd->jiffies_at_free = get_jiffies_64();
3880 if (unlikely(cmd->free_sg))
3883 if (!sess || !sess->se_sess) {
3887 cmd->jiffies_at_free = get_jiffies_64();
3888 target_free_tag(sess->se_sess, &cmd->se_cmd);
3890 EXPORT_SYMBOL(qlt_free_cmd);
3893 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
3895 static int qlt_term_ctio_exchange(struct qla_qpair *qpair, void *ctio,
3896 struct qla_tgt_cmd *cmd, uint32_t status)
3899 struct scsi_qla_host *vha = qpair->vha;
3901 if (cmd->se_cmd.prot_op)
3902 ql_dbg(ql_dbg_tgt_dif, vha, 0xe013,
3903 "Term DIF cmd: lba[0x%llx|%lld] len[0x%x] "
3904 "se_cmd=%p tag[%x] op %#x/%s",
3906 cmd->num_blks, &cmd->se_cmd,
3907 cmd->atio.u.isp24.exchange_addr,
3908 cmd->se_cmd.prot_op,
3909 prot_op_str(cmd->se_cmd.prot_op));
3912 struct ctio7_from_24xx *c = (struct ctio7_from_24xx *)ctio;
3914 cpu_to_le16(OF_TERM_EXCH));
3919 qlt_send_term_exchange(qpair, cmd, &cmd->atio, 1, 0);
3925 /* ha->hardware_lock supposed to be held on entry */
3926 static void *qlt_ctio_to_cmd(struct scsi_qla_host *vha,
3927 struct rsp_que *rsp, uint32_t handle, void *ctio)
3930 struct req_que *req;
3931 int qid = GET_QID(handle);
3932 uint32_t h = handle & ~QLA_TGT_HANDLE_MASK;
3934 if (unlikely(h == QLA_TGT_SKIP_HANDLE))
3937 if (qid == rsp->req->id) {
3939 } else if (vha->hw->req_q_map[qid]) {
3940 ql_dbg(ql_dbg_tgt_mgt, vha, 0x1000a,
3941 "qla_target(%d): CTIO completion with different QID %d handle %x\n",
3942 vha->vp_idx, rsp->id, handle);
3943 req = vha->hw->req_q_map[qid];
3948 h &= QLA_CMD_HANDLE_MASK;
3950 if (h != QLA_TGT_NULL_HANDLE) {
3951 if (unlikely(h >= req->num_outstanding_cmds)) {
3952 ql_dbg(ql_dbg_tgt, vha, 0xe052,
3953 "qla_target(%d): Wrong handle %x received\n",
3954 vha->vp_idx, handle);
3958 cmd = (void *) req->outstanding_cmds[h];
3959 if (unlikely(cmd == NULL)) {
3960 ql_dbg(ql_dbg_async, vha, 0xe053,
3961 "qla_target(%d): Suspicious: unable to find the command with handle %x req->id %d rsp->id %d\n",
3962 vha->vp_idx, handle, req->id, rsp->id);
3965 req->outstanding_cmds[h] = NULL;
3966 } else if (ctio != NULL) {
3967 /* We can't get loop ID from CTIO7 */
3968 ql_dbg(ql_dbg_tgt, vha, 0xe054,
3969 "qla_target(%d): Wrong CTIO received: QLA24xx doesn't "
3970 "support NULL handles\n", vha->vp_idx);
3977 /* hardware_lock should be held by caller. */
3979 qlt_abort_cmd_on_host_reset(struct scsi_qla_host *vha, struct qla_tgt_cmd *cmd)
3981 struct qla_hw_data *ha = vha->hw;
3984 qlt_unmap_sg(vha, cmd);
3986 /* TODO: fix debug message type and ids. */
3987 if (cmd->state == QLA_TGT_STATE_PROCESSED) {
3988 ql_dbg(ql_dbg_io, vha, 0xff00,
3989 "HOST-ABORT: state=PROCESSED.\n");
3990 } else if (cmd->state == QLA_TGT_STATE_NEED_DATA) {
3991 cmd->write_data_transferred = 0;
3992 cmd->state = QLA_TGT_STATE_DATA_IN;
3994 ql_dbg(ql_dbg_io, vha, 0xff01,
3995 "HOST-ABORT: state=DATA_IN.\n");
3997 ha->tgt.tgt_ops->handle_data(cmd);
4000 ql_dbg(ql_dbg_io, vha, 0xff03,
4001 "HOST-ABORT: state=BAD(%d).\n",
4006 cmd->trc_flags |= TRC_FLUSH;
4007 ha->tgt.tgt_ops->free_cmd(cmd);
4011 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
4013 static void qlt_do_ctio_completion(struct scsi_qla_host *vha,
4014 struct rsp_que *rsp, uint32_t handle, uint32_t status, void *ctio)
4016 struct qla_hw_data *ha = vha->hw;
4017 struct se_cmd *se_cmd;
4018 struct qla_tgt_cmd *cmd;
4019 struct qla_qpair *qpair = rsp->qpair;
4021 if (handle & CTIO_INTERMEDIATE_HANDLE_MARK) {
4022 /* That could happen only in case of an error/reset/abort */
4023 if (status != CTIO_SUCCESS) {
4024 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01d,
4025 "Intermediate CTIO received"
4026 " (status %x)\n", status);
4031 cmd = (struct qla_tgt_cmd *)qlt_ctio_to_cmd(vha, rsp, handle, ctio);
4035 se_cmd = &cmd->se_cmd;
4036 cmd->cmd_sent_to_fw = 0;
4038 qlt_unmap_sg(vha, cmd);
4040 if (unlikely(status != CTIO_SUCCESS)) {
4041 switch (status & 0xFFFF) {
4042 case CTIO_INVALID_RX_ID:
4043 if (printk_ratelimit())
4044 dev_info(&vha->hw->pdev->dev,
4045 "qla_target(%d): CTIO with INVALID_RX_ID ATIO attr %x CTIO Flags %x|%x\n",
4046 vha->vp_idx, cmd->atio.u.isp24.attr,
4047 ((cmd->ctio_flags >> 9) & 0xf),
4051 case CTIO_LIP_RESET:
4052 case CTIO_TARGET_RESET:
4054 /* driver request abort via Terminate exchange */
4057 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf058,
4058 "qla_target(%d): CTIO with "
4059 "status %#x received, state %x, se_cmd %p, "
4060 "(LIP_RESET=e, ABORTED=2, TARGET_RESET=17, "
4061 "TIMEOUT=b, INVALID_RX_ID=8)\n", vha->vp_idx,
4062 status, cmd->state, se_cmd);
4065 case CTIO_PORT_LOGGED_OUT:
4066 case CTIO_PORT_UNAVAILABLE:
4069 (status & 0xFFFF) == CTIO_PORT_LOGGED_OUT;
4071 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf059,
4072 "qla_target(%d): CTIO with %s status %x "
4073 "received (state %x, se_cmd %p)\n", vha->vp_idx,
4074 logged_out ? "PORT LOGGED OUT" : "PORT UNAVAILABLE",
4075 status, cmd->state, se_cmd);
4077 if (logged_out && cmd->sess) {
4079 * Session is already logged out, but we need
4080 * to notify initiator, who's not aware of this
4082 cmd->sess->send_els_logo = 1;
4083 ql_dbg(ql_dbg_disc, vha, 0x20f8,
4084 "%s %d %8phC post del sess\n",
4085 __func__, __LINE__, cmd->sess->port_name);
4087 qlt_schedule_sess_for_deletion(cmd->sess);
4091 case CTIO_DIF_ERROR: {
4092 struct ctio_crc_from_fw *crc =
4093 (struct ctio_crc_from_fw *)ctio;
4094 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf073,
4095 "qla_target(%d): CTIO with DIF_ERROR status %x "
4096 "received (state %x, ulp_cmd %p) actual_dif[0x%llx] "
4097 "expect_dif[0x%llx]\n",
4098 vha->vp_idx, status, cmd->state, se_cmd,
4099 *((u64 *)&crc->actual_dif[0]),
4100 *((u64 *)&crc->expected_dif[0]));
4102 qlt_handle_dif_error(qpair, cmd, ctio);
4106 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05b,
4107 "qla_target(%d): CTIO with error status 0x%x received (state %x, se_cmd %p\n",
4108 vha->vp_idx, status, cmd->state, se_cmd);
4113 /* "cmd->aborted" means
4114 * cmd is already aborted/terminated, we don't
4115 * need to terminate again. The exchange is already
4116 * cleaned up/freed at FW level. Just cleanup at driver
4119 if ((cmd->state != QLA_TGT_STATE_NEED_DATA) &&
4121 cmd->trc_flags |= TRC_CTIO_ERR;
4122 if (qlt_term_ctio_exchange(qpair, ctio, cmd, status))
4127 if (cmd->state == QLA_TGT_STATE_PROCESSED) {
4128 cmd->trc_flags |= TRC_CTIO_DONE;
4129 } else if (cmd->state == QLA_TGT_STATE_NEED_DATA) {
4130 cmd->state = QLA_TGT_STATE_DATA_IN;
4132 if (status == CTIO_SUCCESS)
4133 cmd->write_data_transferred = 1;
4135 ha->tgt.tgt_ops->handle_data(cmd);
4137 } else if (cmd->aborted) {
4138 cmd->trc_flags |= TRC_CTIO_ABORTED;
4139 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01e,
4140 "Aborted command %p (tag %lld) finished\n", cmd, se_cmd->tag);
4142 cmd->trc_flags |= TRC_CTIO_STRANGE;
4143 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05c,
4144 "qla_target(%d): A command in state (%d) should "
4145 "not return a CTIO complete\n", vha->vp_idx, cmd->state);
4148 if (unlikely(status != CTIO_SUCCESS) &&
4150 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01f, "Finishing failed CTIO\n");
4154 ha->tgt.tgt_ops->free_cmd(cmd);
4157 static inline int qlt_get_fcp_task_attr(struct scsi_qla_host *vha,
4162 switch (task_codes) {
4163 case ATIO_SIMPLE_QUEUE:
4164 fcp_task_attr = TCM_SIMPLE_TAG;
4166 case ATIO_HEAD_OF_QUEUE:
4167 fcp_task_attr = TCM_HEAD_TAG;
4169 case ATIO_ORDERED_QUEUE:
4170 fcp_task_attr = TCM_ORDERED_TAG;
4172 case ATIO_ACA_QUEUE:
4173 fcp_task_attr = TCM_ACA_TAG;
4176 fcp_task_attr = TCM_SIMPLE_TAG;
4179 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05d,
4180 "qla_target: unknown task code %x, use ORDERED instead\n",
4182 fcp_task_attr = TCM_ORDERED_TAG;
4186 return fcp_task_attr;
4189 static struct fc_port *qlt_make_local_sess(struct scsi_qla_host *,
4192 * Process context for I/O path into tcm_qla2xxx code
4194 static void __qlt_do_work(struct qla_tgt_cmd *cmd)
4196 scsi_qla_host_t *vha = cmd->vha;
4197 struct qla_hw_data *ha = vha->hw;
4198 struct fc_port *sess = cmd->sess;
4199 struct atio_from_isp *atio = &cmd->atio;
4201 unsigned long flags;
4202 uint32_t data_length;
4203 int ret, fcp_task_attr, data_dir, bidi = 0;
4204 struct qla_qpair *qpair = cmd->qpair;
4207 cmd->trc_flags |= TRC_DO_WORK;
4210 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf082,
4211 "cmd with tag %u is aborted\n",
4212 cmd->atio.u.isp24.exchange_addr);
4216 spin_lock_init(&cmd->cmd_lock);
4217 cdb = &atio->u.isp24.fcp_cmnd.cdb[0];
4218 cmd->se_cmd.tag = atio->u.isp24.exchange_addr;
4220 if (atio->u.isp24.fcp_cmnd.rddata &&
4221 atio->u.isp24.fcp_cmnd.wrdata) {
4223 data_dir = DMA_TO_DEVICE;
4224 } else if (atio->u.isp24.fcp_cmnd.rddata)
4225 data_dir = DMA_FROM_DEVICE;
4226 else if (atio->u.isp24.fcp_cmnd.wrdata)
4227 data_dir = DMA_TO_DEVICE;
4229 data_dir = DMA_NONE;
4231 fcp_task_attr = qlt_get_fcp_task_attr(vha,
4232 atio->u.isp24.fcp_cmnd.task_attr);
4233 data_length = get_datalen_for_atio(atio);
4235 ret = ha->tgt.tgt_ops->handle_cmd(vha, cmd, cdb, data_length,
4236 fcp_task_attr, data_dir, bidi);
4240 * Drop extra session reference from qla_tgt_handle_cmd_for_atio*(
4242 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
4243 ha->tgt.tgt_ops->put_sess(sess);
4244 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
4248 ql_dbg(ql_dbg_io, vha, 0x3060, "Terminating work cmd %p", cmd);
4250 * cmd has not sent to target yet, so pass NULL as the second
4251 * argument to qlt_send_term_exchange() and free the memory here.
4253 cmd->trc_flags |= TRC_DO_WORK_ERR;
4254 spin_lock_irqsave(qpair->qp_lock_ptr, flags);
4255 qlt_send_term_exchange(qpair, NULL, &cmd->atio, 1, 0);
4257 qlt_decr_num_pend_cmds(vha);
4258 target_free_tag(sess->se_sess, &cmd->se_cmd);
4259 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
4261 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
4262 ha->tgt.tgt_ops->put_sess(sess);
4263 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
4266 static void qlt_do_work(struct work_struct *work)
4268 struct qla_tgt_cmd *cmd = container_of(work, struct qla_tgt_cmd, work);
4269 scsi_qla_host_t *vha = cmd->vha;
4270 unsigned long flags;
4272 spin_lock_irqsave(&vha->cmd_list_lock, flags);
4273 list_del(&cmd->cmd_list);
4274 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
4279 void qlt_clr_qp_table(struct scsi_qla_host *vha)
4281 unsigned long flags;
4282 struct qla_hw_data *ha = vha->hw;
4283 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
4287 ql_log(ql_log_info, vha, 0x706c,
4288 "User update Number of Active Qpairs %d\n",
4289 ha->tgt.num_act_qpairs);
4291 spin_lock_irqsave(&ha->tgt.atio_lock, flags);
4293 btree_for_each_safe64(&tgt->lun_qpair_map, key, node)
4294 btree_remove64(&tgt->lun_qpair_map, key);
4296 ha->base_qpair->lun_cnt = 0;
4297 for (key = 0; key < ha->max_qpairs; key++)
4298 if (ha->queue_pair_map[key])
4299 ha->queue_pair_map[key]->lun_cnt = 0;
4301 spin_unlock_irqrestore(&ha->tgt.atio_lock, flags);
4304 static void qlt_assign_qpair(struct scsi_qla_host *vha,
4305 struct qla_tgt_cmd *cmd)
4307 struct qla_qpair *qpair, *qp;
4308 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
4309 struct qla_qpair_hint *h;
4311 if (vha->flags.qpairs_available) {
4312 h = btree_lookup64(&tgt->lun_qpair_map, cmd->unpacked_lun);
4314 /* spread lun to qpair ratio evently */
4316 struct scsi_qla_host *base_vha =
4317 pci_get_drvdata(vha->hw->pdev);
4319 qpair = vha->hw->base_qpair;
4320 if (qpair->lun_cnt == 0) {
4322 h = qla_qpair_to_hint(tgt, qpair);
4324 rc = btree_insert64(&tgt->lun_qpair_map,
4325 cmd->unpacked_lun, h, GFP_ATOMIC);
4328 ql_log(ql_log_info, vha, 0xd037,
4329 "Unable to insert lun %llx into lun_qpair_map\n",
4334 lcnt = qpair->lun_cnt;
4338 list_for_each_entry(qp, &base_vha->qp_list,
4340 if (qp->lun_cnt == 0) {
4342 h = qla_qpair_to_hint(tgt, qp);
4344 rc = btree_insert64(&tgt->lun_qpair_map,
4345 cmd->unpacked_lun, h, GFP_ATOMIC);
4348 ql_log(ql_log_info, vha, 0xd038,
4349 "Unable to insert lun %llx into lun_qpair_map\n",
4355 if (qp->lun_cnt < lcnt) {
4364 h = qla_qpair_to_hint(tgt, qpair);
4366 rc = btree_insert64(&tgt->lun_qpair_map,
4367 cmd->unpacked_lun, h, GFP_ATOMIC);
4370 ql_log(ql_log_info, vha, 0xd039,
4371 "Unable to insert lun %llx into lun_qpair_map\n",
4376 h = &tgt->qphints[0];
4379 cmd->qpair = h->qpair;
4380 cmd->se_cmd.cpuid = h->cpuid;
4383 static struct qla_tgt_cmd *qlt_get_tag(scsi_qla_host_t *vha,
4384 struct fc_port *sess,
4385 struct atio_from_isp *atio)
4387 struct se_session *se_sess = sess->se_sess;
4388 struct qla_tgt_cmd *cmd;
4391 tag = sbitmap_queue_get(&se_sess->sess_tag_pool, &cpu);
4395 cmd = &((struct qla_tgt_cmd *)se_sess->sess_cmd_map)[tag];
4396 memset(cmd, 0, sizeof(struct qla_tgt_cmd));
4397 cmd->cmd_type = TYPE_TGT_CMD;
4398 memcpy(&cmd->atio, atio, sizeof(*atio));
4399 cmd->state = QLA_TGT_STATE_NEW;
4400 cmd->tgt = vha->vha_tgt.qla_tgt;
4401 qlt_incr_num_pend_cmds(vha);
4403 cmd->se_cmd.map_tag = tag;
4404 cmd->se_cmd.map_cpu = cpu;
4406 cmd->loop_id = sess->loop_id;
4407 cmd->conf_compl_supported = sess->conf_compl_supported;
4410 cmd->jiffies_at_alloc = get_jiffies_64();
4412 cmd->unpacked_lun = scsilun_to_int(
4413 (struct scsi_lun *)&atio->u.isp24.fcp_cmnd.lun);
4414 qlt_assign_qpair(vha, cmd);
4415 cmd->reset_count = vha->hw->base_qpair->chip_reset;
4416 cmd->vp_idx = vha->vp_idx;
4421 /* ha->hardware_lock supposed to be held on entry */
4422 static int qlt_handle_cmd_for_atio(struct scsi_qla_host *vha,
4423 struct atio_from_isp *atio)
4425 struct qla_hw_data *ha = vha->hw;
4426 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
4427 struct fc_port *sess;
4428 struct qla_tgt_cmd *cmd;
4429 unsigned long flags;
4432 if (unlikely(tgt->tgt_stop)) {
4433 ql_dbg(ql_dbg_io, vha, 0x3061,
4434 "New command while device %p is shutting down\n", tgt);
4438 id.b.al_pa = atio->u.isp24.fcp_hdr.s_id[2];
4439 id.b.area = atio->u.isp24.fcp_hdr.s_id[1];
4440 id.b.domain = atio->u.isp24.fcp_hdr.s_id[0];
4441 if (IS_SW_RESV_ADDR(id))
4444 sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha, atio->u.isp24.fcp_hdr.s_id);
4445 if (unlikely(!sess))
4448 /* Another WWN used to have our s_id. Our PLOGI scheduled its
4449 * session deletion, but it's still in sess_del_work wq */
4450 if (sess->deleted) {
4451 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf002,
4452 "New command while old session %p is being deleted\n",
4458 * Do kref_get() before returning + dropping qla_hw_data->hardware_lock.
4460 if (!kref_get_unless_zero(&sess->sess_kref)) {
4461 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf004,
4462 "%s: kref_get fail, %8phC oxid %x \n",
4463 __func__, sess->port_name,
4464 be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id));
4468 cmd = qlt_get_tag(vha, sess, atio);
4470 ql_dbg(ql_dbg_io, vha, 0x3062,
4471 "qla_target(%d): Allocation of cmd failed\n", vha->vp_idx);
4472 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
4473 ha->tgt.tgt_ops->put_sess(sess);
4474 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
4479 cmd->trc_flags |= TRC_NEW_CMD;
4481 spin_lock_irqsave(&vha->cmd_list_lock, flags);
4482 list_add_tail(&cmd->cmd_list, &vha->qla_cmd_list);
4483 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
4485 INIT_WORK(&cmd->work, qlt_do_work);
4486 if (vha->flags.qpairs_available) {
4487 queue_work_on(cmd->se_cmd.cpuid, qla_tgt_wq, &cmd->work);
4488 } else if (ha->msix_count) {
4489 if (cmd->atio.u.isp24.fcp_cmnd.rddata)
4490 queue_work_on(smp_processor_id(), qla_tgt_wq,
4493 queue_work_on(cmd->se_cmd.cpuid, qla_tgt_wq,
4496 queue_work(qla_tgt_wq, &cmd->work);
4502 /* ha->hardware_lock supposed to be held on entry */
4503 static int qlt_issue_task_mgmt(struct fc_port *sess, u64 lun,
4504 int fn, void *iocb, int flags)
4506 struct scsi_qla_host *vha = sess->vha;
4507 struct qla_hw_data *ha = vha->hw;
4508 struct qla_tgt_mgmt_cmd *mcmd;
4509 struct atio_from_isp *a = (struct atio_from_isp *)iocb;
4510 struct qla_qpair_hint *h = &vha->vha_tgt.qla_tgt->qphints[0];
4512 mcmd = mempool_alloc(qla_tgt_mgmt_cmd_mempool, GFP_ATOMIC);
4514 ql_dbg(ql_dbg_tgt_tmr, vha, 0x10009,
4515 "qla_target(%d): Allocation of management "
4516 "command failed, some commands and their data could "
4517 "leak\n", vha->vp_idx);
4520 memset(mcmd, 0, sizeof(*mcmd));
4524 memcpy(&mcmd->orig_iocb.imm_ntfy, iocb,
4525 sizeof(mcmd->orig_iocb.imm_ntfy));
4527 mcmd->tmr_func = fn;
4528 mcmd->flags = flags;
4529 mcmd->reset_count = ha->base_qpair->chip_reset;
4530 mcmd->qpair = h->qpair;
4532 mcmd->se_cmd.cpuid = h->cpuid;
4533 mcmd->unpacked_lun = lun;
4536 case QLA_TGT_LUN_RESET:
4537 case QLA_TGT_CLEAR_TS:
4538 case QLA_TGT_ABORT_TS:
4539 abort_cmds_for_lun(vha, lun, a->u.isp24.fcp_hdr.s_id);
4541 case QLA_TGT_CLEAR_ACA:
4542 h = qlt_find_qphint(vha, mcmd->unpacked_lun);
4543 mcmd->qpair = h->qpair;
4544 mcmd->se_cmd.cpuid = h->cpuid;
4547 case QLA_TGT_TARGET_RESET:
4548 case QLA_TGT_NEXUS_LOSS_SESS:
4549 case QLA_TGT_NEXUS_LOSS:
4550 case QLA_TGT_ABORT_ALL:
4556 INIT_WORK(&mcmd->work, qlt_do_tmr_work);
4557 queue_work_on(mcmd->se_cmd.cpuid, qla_tgt_wq,
4563 /* ha->hardware_lock supposed to be held on entry */
4564 static int qlt_handle_task_mgmt(struct scsi_qla_host *vha, void *iocb)
4566 struct atio_from_isp *a = (struct atio_from_isp *)iocb;
4567 struct qla_hw_data *ha = vha->hw;
4568 struct fc_port *sess;
4571 unsigned long flags;
4573 fn = a->u.isp24.fcp_cmnd.task_mgmt_flags;
4575 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
4576 sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha,
4577 a->u.isp24.fcp_hdr.s_id);
4578 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
4581 scsilun_to_int((struct scsi_lun *)&a->u.isp24.fcp_cmnd.lun);
4583 if (sess == NULL || sess->deleted)
4586 return qlt_issue_task_mgmt(sess, unpacked_lun, fn, iocb, 0);
4589 /* ha->hardware_lock supposed to be held on entry */
4590 static int __qlt_abort_task(struct scsi_qla_host *vha,
4591 struct imm_ntfy_from_isp *iocb, struct fc_port *sess)
4593 struct atio_from_isp *a = (struct atio_from_isp *)iocb;
4594 struct qla_hw_data *ha = vha->hw;
4595 struct qla_tgt_mgmt_cmd *mcmd;
4599 mcmd = mempool_alloc(qla_tgt_mgmt_cmd_mempool, GFP_ATOMIC);
4601 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05f,
4602 "qla_target(%d): %s: Allocation of ABORT cmd failed\n",
4603 vha->vp_idx, __func__);
4606 memset(mcmd, 0, sizeof(*mcmd));
4609 memcpy(&mcmd->orig_iocb.imm_ntfy, iocb,
4610 sizeof(mcmd->orig_iocb.imm_ntfy));
4613 scsilun_to_int((struct scsi_lun *)&a->u.isp24.fcp_cmnd.lun);
4614 mcmd->reset_count = ha->base_qpair->chip_reset;
4615 mcmd->tmr_func = QLA_TGT_2G_ABORT_TASK;
4616 mcmd->qpair = ha->base_qpair;
4618 rc = ha->tgt.tgt_ops->handle_tmr(mcmd, unpacked_lun, mcmd->tmr_func,
4619 le16_to_cpu(iocb->u.isp2x.seq_id));
4621 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf060,
4622 "qla_target(%d): tgt_ops->handle_tmr() failed: %d\n",
4624 mempool_free(mcmd, qla_tgt_mgmt_cmd_mempool);
4631 /* ha->hardware_lock supposed to be held on entry */
4632 static int qlt_abort_task(struct scsi_qla_host *vha,
4633 struct imm_ntfy_from_isp *iocb)
4635 struct qla_hw_data *ha = vha->hw;
4636 struct fc_port *sess;
4638 unsigned long flags;
4640 loop_id = GET_TARGET_ID(ha, (struct atio_from_isp *)iocb);
4642 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
4643 sess = ha->tgt.tgt_ops->find_sess_by_loop_id(vha, loop_id);
4644 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
4647 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf025,
4648 "qla_target(%d): task abort for unexisting "
4649 "session\n", vha->vp_idx);
4650 return qlt_sched_sess_work(vha->vha_tgt.qla_tgt,
4651 QLA_TGT_SESS_WORK_ABORT, iocb, sizeof(*iocb));
4654 return __qlt_abort_task(vha, iocb, sess);
4657 void qlt_logo_completion_handler(fc_port_t *fcport, int rc)
4659 if (rc != MBS_COMMAND_COMPLETE) {
4660 ql_dbg(ql_dbg_tgt_mgt, fcport->vha, 0xf093,
4661 "%s: se_sess %p / sess %p from"
4662 " port %8phC loop_id %#04x s_id %02x:%02x:%02x"
4663 " LOGO failed: %#x\n",
4667 fcport->port_name, fcport->loop_id,
4668 fcport->d_id.b.domain, fcport->d_id.b.area,
4669 fcport->d_id.b.al_pa, rc);
4672 fcport->logout_completed = 1;
4676 * ha->hardware_lock supposed to be held on entry (to protect tgt->sess_list)
4678 * Schedules sessions with matching port_id/loop_id but different wwn for
4679 * deletion. Returns existing session with matching wwn if present.
4683 qlt_find_sess_invalidate_other(scsi_qla_host_t *vha, uint64_t wwn,
4684 port_id_t port_id, uint16_t loop_id, struct fc_port **conflict_sess)
4686 struct fc_port *sess = NULL, *other_sess;
4689 *conflict_sess = NULL;
4691 list_for_each_entry(other_sess, &vha->vp_fcports, list) {
4693 other_wwn = wwn_to_u64(other_sess->port_name);
4695 if (wwn == other_wwn) {
4701 /* find other sess with nport_id collision */
4702 if (port_id.b24 == other_sess->d_id.b24) {
4703 if (loop_id != other_sess->loop_id) {
4704 ql_dbg(ql_dbg_tgt_tmr, vha, 0x1000c,
4705 "Invalidating sess %p loop_id %d wwn %llx.\n",
4706 other_sess, other_sess->loop_id, other_wwn);
4709 * logout_on_delete is set by default, but another
4710 * session that has the same s_id/loop_id combo
4711 * might have cleared it when requested this session
4712 * deletion, so don't touch it
4714 qlt_schedule_sess_for_deletion(other_sess);
4717 * Another wwn used to have our s_id/loop_id
4718 * kill the session, but don't free the loop_id
4720 ql_dbg(ql_dbg_tgt_tmr, vha, 0xf01b,
4721 "Invalidating sess %p loop_id %d wwn %llx.\n",
4722 other_sess, other_sess->loop_id, other_wwn);
4724 other_sess->keep_nport_handle = 1;
4725 if (other_sess->disc_state != DSC_DELETED)
4726 *conflict_sess = other_sess;
4727 qlt_schedule_sess_for_deletion(other_sess);
4732 /* find other sess with nport handle collision */
4733 if ((loop_id == other_sess->loop_id) &&
4734 (loop_id != FC_NO_LOOP_ID)) {
4735 ql_dbg(ql_dbg_tgt_tmr, vha, 0x1000d,
4736 "Invalidating sess %p loop_id %d wwn %llx.\n",
4737 other_sess, other_sess->loop_id, other_wwn);
4739 /* Same loop_id but different s_id
4740 * Ok to kill and logout */
4741 qlt_schedule_sess_for_deletion(other_sess);
4748 /* Abort any commands for this s_id waiting on qla_tgt_wq workqueue */
4749 static int abort_cmds_for_s_id(struct scsi_qla_host *vha, port_id_t *s_id)
4751 struct qla_tgt_sess_op *op;
4752 struct qla_tgt_cmd *cmd;
4755 unsigned long flags;
4757 key = (((u32)s_id->b.domain << 16) |
4758 ((u32)s_id->b.area << 8) |
4759 ((u32)s_id->b.al_pa));
4761 spin_lock_irqsave(&vha->cmd_list_lock, flags);
4762 list_for_each_entry(op, &vha->qla_sess_op_cmd_list, cmd_list) {
4763 uint32_t op_key = sid_to_key(op->atio.u.isp24.fcp_hdr.s_id);
4765 if (op_key == key) {
4771 list_for_each_entry(op, &vha->unknown_atio_list, cmd_list) {
4772 uint32_t op_key = sid_to_key(op->atio.u.isp24.fcp_hdr.s_id);
4773 if (op_key == key) {
4779 list_for_each_entry(cmd, &vha->qla_cmd_list, cmd_list) {
4780 uint32_t cmd_key = sid_to_key(cmd->atio.u.isp24.fcp_hdr.s_id);
4781 if (cmd_key == key) {
4786 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
4791 static int qlt_handle_login(struct scsi_qla_host *vha,
4792 struct imm_ntfy_from_isp *iocb)
4794 struct fc_port *sess = NULL, *conflict_sess = NULL;
4797 uint16_t loop_id, wd3_lo;
4799 struct qlt_plogi_ack_t *pla;
4800 unsigned long flags;
4802 wwn = wwn_to_u64(iocb->u.isp24.port_name);
4804 port_id.b.domain = iocb->u.isp24.port_id[2];
4805 port_id.b.area = iocb->u.isp24.port_id[1];
4806 port_id.b.al_pa = iocb->u.isp24.port_id[0];
4807 port_id.b.rsvd_1 = 0;
4809 loop_id = le16_to_cpu(iocb->u.isp24.nport_handle);
4811 /* Mark all stale commands sitting in qla_tgt_wq for deletion */
4812 abort_cmds_for_s_id(vha, &port_id);
4815 spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
4816 sess = qlt_find_sess_invalidate_other(vha, wwn,
4817 port_id, loop_id, &conflict_sess);
4818 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
4820 ql_dbg(ql_dbg_disc, vha, 0xffff,
4821 "%s %d Term INOT due to WWN=0 lid=%d, NportID %06X ",
4822 __func__, __LINE__, loop_id, port_id.b24);
4823 qlt_send_term_imm_notif(vha, iocb, 1);
4827 if (IS_SW_RESV_ADDR(port_id)) {
4832 pla = qlt_plogi_ack_find_add(vha, &port_id, iocb);
4834 ql_dbg(ql_dbg_disc + ql_dbg_verbose, vha, 0xffff,
4835 "%s %d %8phC Term INOT due to mem alloc fail",
4837 iocb->u.isp24.port_name);
4838 qlt_send_term_imm_notif(vha, iocb, 1);
4842 if (conflict_sess) {
4843 conflict_sess->login_gen++;
4844 qlt_plogi_ack_link(vha, pla, conflict_sess,
4845 QLT_PLOGI_LINK_CONFLICT);
4850 ql_dbg(ql_dbg_disc, vha, 0xffff,
4851 "%s %d %8phC post new sess\n",
4852 __func__, __LINE__, iocb->u.isp24.port_name);
4853 if (iocb->u.isp24.status_subcode == ELS_PLOGI)
4854 qla24xx_post_newsess_work(vha, &port_id,
4855 iocb->u.isp24.port_name,
4856 iocb->u.isp24.u.plogi.node_name,
4857 pla, FC4_TYPE_UNKNOWN);
4859 qla24xx_post_newsess_work(vha, &port_id,
4860 iocb->u.isp24.port_name, NULL,
4861 pla, FC4_TYPE_UNKNOWN);
4866 if (sess->disc_state == DSC_UPD_FCPORT) {
4870 * Remote port registration is still going on from
4871 * previous login. Allow it to finish before we
4872 * accept the new login.
4874 sess->next_disc_state = DSC_DELETE_PEND;
4875 sec = jiffies_to_msecs(jiffies -
4876 sess->jiffies_at_registration) / 1000;
4877 if (sess->sec_since_registration < sec && sec &&
4879 sess->sec_since_registration = sec;
4880 ql_dbg(ql_dbg_disc, vha, 0xffff,
4881 "%s %8phC - Slow Rport registration (%d Sec)\n",
4882 __func__, sess->port_name, sec);
4886 kmem_cache_free(qla_tgt_plogi_cachep, pla);
4888 qlt_send_term_imm_notif(vha, iocb, 1);
4892 qlt_plogi_ack_link(vha, pla, sess, QLT_PLOGI_LINK_SAME_WWN);
4893 sess->d_id = port_id;
4896 if (iocb->u.isp24.status_subcode == ELS_PRLI) {
4897 sess->fw_login_state = DSC_LS_PRLI_PEND;
4899 sess->loop_id = loop_id;
4900 sess->d_id = port_id;
4901 sess->fw_login_state = DSC_LS_PRLI_PEND;
4902 wd3_lo = le16_to_cpu(iocb->u.isp24.u.prli.wd3_lo);
4905 sess->conf_compl_supported = 1;
4907 if ((wd3_lo & BIT_4) == 0)
4908 sess->port_type = FCT_INITIATOR;
4910 sess->port_type = FCT_TARGET;
4913 sess->fw_login_state = DSC_LS_PLOGI_PEND;
4916 ql_dbg(ql_dbg_disc, vha, 0x20f9,
4917 "%s %d %8phC DS %d\n",
4918 __func__, __LINE__, sess->port_name, sess->disc_state);
4920 switch (sess->disc_state) {
4922 qlt_plogi_ack_unref(vha, pla);
4927 * Under normal circumstances we want to release nport handle
4928 * during LOGO process to avoid nport handle leaks inside FW.
4929 * The exception is when LOGO is done while another PLOGI with
4930 * the same nport handle is waiting as might be the case here.
4931 * Note: there is always a possibily of a race where session
4932 * deletion has already started for other reasons (e.g. ACL
4933 * removal) and now PLOGI arrives:
4934 * 1. if PLOGI arrived in FW after nport handle has been freed,
4935 * FW must have assigned this PLOGI a new/same handle and we
4936 * can proceed ACK'ing it as usual when session deletion
4938 * 2. if PLOGI arrived in FW before LOGO with LCF_FREE_NPORT
4939 * bit reached it, the handle has now been released. We'll
4940 * get an error when we ACK this PLOGI. Nothing will be sent
4941 * back to initiator. Initiator should eventually retry
4942 * PLOGI and situation will correct itself.
4944 sess->keep_nport_handle = ((sess->loop_id == loop_id) &&
4945 (sess->d_id.b24 == port_id.b24));
4947 ql_dbg(ql_dbg_disc, vha, 0x20f9,
4948 "%s %d %8phC post del sess\n",
4949 __func__, __LINE__, sess->port_name);
4952 qlt_schedule_sess_for_deletion(sess);
4960 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
4962 static int qlt_24xx_handle_els(struct scsi_qla_host *vha,
4963 struct imm_ntfy_from_isp *iocb)
4965 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
4966 struct qla_hw_data *ha = vha->hw;
4967 struct fc_port *sess = NULL, *conflict_sess = NULL;
4973 unsigned long flags;
4975 wwn = wwn_to_u64(iocb->u.isp24.port_name);
4977 port_id.b.domain = iocb->u.isp24.port_id[2];
4978 port_id.b.area = iocb->u.isp24.port_id[1];
4979 port_id.b.al_pa = iocb->u.isp24.port_id[0];
4980 port_id.b.rsvd_1 = 0;
4982 loop_id = le16_to_cpu(iocb->u.isp24.nport_handle);
4984 ql_dbg(ql_dbg_disc, vha, 0xf026,
4985 "qla_target(%d): Port ID: %02x:%02x:%02x ELS opcode: 0x%02x lid %d %8phC\n",
4986 vha->vp_idx, iocb->u.isp24.port_id[2],
4987 iocb->u.isp24.port_id[1], iocb->u.isp24.port_id[0],
4988 iocb->u.isp24.status_subcode, loop_id,
4989 iocb->u.isp24.port_name);
4991 /* res = 1 means ack at the end of thread
4992 * res = 0 means ack async/later.
4994 switch (iocb->u.isp24.status_subcode) {
4996 res = qlt_handle_login(vha, iocb);
5001 sess = qla2x00_find_fcport_by_wwpn(vha,
5002 iocb->u.isp24.port_name, 1);
5004 if (sess && sess->plogi_link[QLT_PLOGI_LINK_SAME_WWN]) {
5005 ql_dbg(ql_dbg_disc, vha, 0xffff,
5006 "%s %d %8phC Term PRLI due to PLOGI ACK not completed\n",
5008 iocb->u.isp24.port_name);
5009 qlt_send_term_imm_notif(vha, iocb, 1);
5013 res = qlt_handle_login(vha, iocb);
5017 if (IS_SW_RESV_ADDR(port_id)) {
5022 wd3_lo = le16_to_cpu(iocb->u.isp24.u.prli.wd3_lo);
5025 spin_lock_irqsave(&tgt->ha->tgt.sess_lock, flags);
5026 sess = qlt_find_sess_invalidate_other(vha, wwn, port_id,
5027 loop_id, &conflict_sess);
5028 spin_unlock_irqrestore(&tgt->ha->tgt.sess_lock, flags);
5031 if (conflict_sess) {
5032 switch (conflict_sess->disc_state) {
5034 case DSC_DELETE_PEND:
5037 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf09b,
5038 "PRLI with conflicting sess %p port %8phC\n",
5039 conflict_sess, conflict_sess->port_name);
5040 conflict_sess->fw_login_state =
5041 DSC_LS_PORT_UNAVAIL;
5042 qlt_send_term_imm_notif(vha, iocb, 1);
5049 bool delete = false;
5051 spin_lock_irqsave(&tgt->ha->tgt.sess_lock, flags);
5052 switch (sess->fw_login_state) {
5053 case DSC_LS_PLOGI_PEND:
5054 case DSC_LS_PLOGI_COMP:
5055 case DSC_LS_PRLI_COMP:
5062 switch (sess->disc_state) {
5063 case DSC_UPD_FCPORT:
5064 spin_unlock_irqrestore(&tgt->ha->tgt.sess_lock,
5067 sec = jiffies_to_msecs(jiffies -
5068 sess->jiffies_at_registration)/1000;
5069 if (sess->sec_since_registration < sec && sec &&
5071 sess->sec_since_registration = sec;
5072 ql_dbg(ql_dbg_disc, sess->vha, 0xffff,
5073 "%s %8phC : Slow Rport registration(%d Sec)\n",
5074 __func__, sess->port_name, sec);
5076 qlt_send_term_imm_notif(vha, iocb, 1);
5079 case DSC_LOGIN_PEND:
5081 case DSC_LOGIN_COMPLETE:
5090 spin_unlock_irqrestore(&tgt->ha->tgt.sess_lock,
5093 * Impatient initiator sent PRLI before last
5094 * PLOGI could finish. Will force him to re-try,
5095 * while last one finishes.
5097 ql_log(ql_log_warn, sess->vha, 0xf095,
5098 "sess %p PRLI received, before plogi ack.\n",
5100 qlt_send_term_imm_notif(vha, iocb, 1);
5106 * This shouldn't happen under normal circumstances,
5107 * since we have deleted the old session during PLOGI
5109 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf096,
5110 "PRLI (loop_id %#04x) for existing sess %p (loop_id %#04x)\n",
5111 sess->loop_id, sess, iocb->u.isp24.nport_handle);
5114 sess->loop_id = loop_id;
5115 sess->d_id = port_id;
5116 sess->fw_login_state = DSC_LS_PRLI_PEND;
5119 sess->conf_compl_supported = 1;
5121 if ((wd3_lo & BIT_4) == 0)
5122 sess->port_type = FCT_INITIATOR;
5124 sess->port_type = FCT_TARGET;
5126 spin_unlock_irqrestore(&tgt->ha->tgt.sess_lock, flags);
5128 res = 1; /* send notify ack */
5130 /* Make session global (not used in fabric mode) */
5131 if (ha->current_topology != ISP_CFG_F) {
5133 ql_dbg(ql_dbg_disc, vha, 0x20fa,
5134 "%s %d %8phC post nack\n",
5135 __func__, __LINE__, sess->port_name);
5136 qla24xx_post_nack_work(vha, sess, iocb,
5140 set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
5141 set_bit(LOCAL_LOOP_UPDATE, &vha->dpc_flags);
5142 qla2xxx_wake_dpc(vha);
5146 ql_dbg(ql_dbg_disc, vha, 0x20fb,
5147 "%s %d %8phC post nack\n",
5148 __func__, __LINE__, sess->port_name);
5149 qla24xx_post_nack_work(vha, sess, iocb,
5157 if (le16_to_cpu(iocb->u.isp24.flags) &
5158 NOTIFY24XX_FLAGS_GLOBAL_TPRLO) {
5160 qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS);
5167 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
5168 sess = qla2x00_find_fcport_by_loopid(vha, loop_id);
5169 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
5173 sess->fw_login_state = DSC_LS_LOGO_PEND;
5174 sess->logo_ack_needed = 1;
5175 memcpy(sess->iocb, iocb, IOCB_SIZE);
5178 res = qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS_SESS);
5180 ql_dbg(ql_dbg_disc, vha, 0x20fc,
5181 "%s: logo %llx res %d sess %p ",
5182 __func__, wwn, res, sess);
5185 * cmd went upper layer, look for qlt_xmit_tm_rsp()
5186 * for LOGO_ACK & sess delete
5191 /* cmd did not go to upper layer. */
5193 qlt_schedule_sess_for_deletion(sess);
5196 /* else logo will be ack */
5202 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
5203 if (tgt->link_reinit_iocb_pending) {
5204 qlt_send_notify_ack(ha->base_qpair,
5205 &tgt->link_reinit_iocb, 0, 0, 0, 0, 0, 0);
5206 tgt->link_reinit_iocb_pending = 0;
5209 sess = qla2x00_find_fcport_by_wwpn(vha,
5210 iocb->u.isp24.port_name, 1);
5212 ql_dbg(ql_dbg_disc, vha, 0x20fd,
5213 "sess %p lid %d|%d DS %d LS %d\n",
5214 sess, sess->loop_id, loop_id,
5215 sess->disc_state, sess->fw_login_state);
5218 res = 1; /* send notify ack */
5222 case ELS_FLOGI: /* should never happen */
5224 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf061,
5225 "qla_target(%d): Unsupported ELS command %x "
5226 "received\n", vha->vp_idx, iocb->u.isp24.status_subcode);
5227 res = qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS_SESS);
5231 ql_dbg(ql_dbg_disc, vha, 0xf026,
5232 "qla_target(%d): Exit ELS opcode: 0x%02x res %d\n",
5233 vha->vp_idx, iocb->u.isp24.status_subcode, res);
5239 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
5241 static void qlt_handle_imm_notify(struct scsi_qla_host *vha,
5242 struct imm_ntfy_from_isp *iocb)
5244 struct qla_hw_data *ha = vha->hw;
5245 uint32_t add_flags = 0;
5246 int send_notify_ack = 1;
5249 status = le16_to_cpu(iocb->u.isp2x.status);
5251 case IMM_NTFY_LIP_RESET:
5253 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf032,
5254 "qla_target(%d): LIP reset (loop %#x), subcode %x\n",
5255 vha->vp_idx, le16_to_cpu(iocb->u.isp24.nport_handle),
5256 iocb->u.isp24.status_subcode);
5258 if (qlt_reset(vha, iocb, QLA_TGT_ABORT_ALL) == 0)
5259 send_notify_ack = 0;
5263 case IMM_NTFY_LIP_LINK_REINIT:
5265 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
5266 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf033,
5267 "qla_target(%d): LINK REINIT (loop %#x, "
5268 "subcode %x)\n", vha->vp_idx,
5269 le16_to_cpu(iocb->u.isp24.nport_handle),
5270 iocb->u.isp24.status_subcode);
5271 if (tgt->link_reinit_iocb_pending) {
5272 qlt_send_notify_ack(ha->base_qpair,
5273 &tgt->link_reinit_iocb, 0, 0, 0, 0, 0, 0);
5275 memcpy(&tgt->link_reinit_iocb, iocb, sizeof(*iocb));
5276 tgt->link_reinit_iocb_pending = 1;
5278 * QLogic requires to wait after LINK REINIT for possible
5279 * PDISC or ADISC ELS commands
5281 send_notify_ack = 0;
5285 case IMM_NTFY_PORT_LOGOUT:
5286 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf034,
5287 "qla_target(%d): Port logout (loop "
5288 "%#x, subcode %x)\n", vha->vp_idx,
5289 le16_to_cpu(iocb->u.isp24.nport_handle),
5290 iocb->u.isp24.status_subcode);
5292 if (qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS_SESS) == 0)
5293 send_notify_ack = 0;
5294 /* The sessions will be cleared in the callback, if needed */
5297 case IMM_NTFY_GLBL_TPRLO:
5298 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf035,
5299 "qla_target(%d): Global TPRLO (%x)\n", vha->vp_idx, status);
5300 if (qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS) == 0)
5301 send_notify_ack = 0;
5302 /* The sessions will be cleared in the callback, if needed */
5305 case IMM_NTFY_PORT_CONFIG:
5306 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf036,
5307 "qla_target(%d): Port config changed (%x)\n", vha->vp_idx,
5309 if (qlt_reset(vha, iocb, QLA_TGT_ABORT_ALL) == 0)
5310 send_notify_ack = 0;
5311 /* The sessions will be cleared in the callback, if needed */
5314 case IMM_NTFY_GLBL_LOGO:
5315 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06a,
5316 "qla_target(%d): Link failure detected\n",
5318 /* I_T nexus loss */
5319 if (qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS) == 0)
5320 send_notify_ack = 0;
5323 case IMM_NTFY_IOCB_OVERFLOW:
5324 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06b,
5325 "qla_target(%d): Cannot provide requested "
5326 "capability (IOCB overflowed the immediate notify "
5327 "resource count)\n", vha->vp_idx);
5330 case IMM_NTFY_ABORT_TASK:
5331 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf037,
5332 "qla_target(%d): Abort Task (S %08x I %#x -> "
5333 "L %#x)\n", vha->vp_idx,
5334 le16_to_cpu(iocb->u.isp2x.seq_id),
5335 GET_TARGET_ID(ha, (struct atio_from_isp *)iocb),
5336 le16_to_cpu(iocb->u.isp2x.lun));
5337 if (qlt_abort_task(vha, iocb) == 0)
5338 send_notify_ack = 0;
5341 case IMM_NTFY_RESOURCE:
5342 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06c,
5343 "qla_target(%d): Out of resources, host %ld\n",
5344 vha->vp_idx, vha->host_no);
5347 case IMM_NTFY_MSG_RX:
5348 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf038,
5349 "qla_target(%d): Immediate notify task %x\n",
5350 vha->vp_idx, iocb->u.isp2x.task_flags);
5354 if (qlt_24xx_handle_els(vha, iocb) == 0)
5355 send_notify_ack = 0;
5358 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06d,
5359 "qla_target(%d): Received unknown immediate "
5360 "notify status %x\n", vha->vp_idx, status);
5364 if (send_notify_ack)
5365 qlt_send_notify_ack(ha->base_qpair, iocb, add_flags, 0, 0, 0,
5370 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
5371 * This function sends busy to ISP 2xxx or 24xx.
5373 static int __qlt_send_busy(struct qla_qpair *qpair,
5374 struct atio_from_isp *atio, uint16_t status)
5376 struct scsi_qla_host *vha = qpair->vha;
5377 struct ctio7_to_24xx *ctio24;
5378 struct qla_hw_data *ha = vha->hw;
5380 struct fc_port *sess = NULL;
5381 unsigned long flags;
5385 id.b.al_pa = atio->u.isp24.fcp_hdr.s_id[2];
5386 id.b.area = atio->u.isp24.fcp_hdr.s_id[1];
5387 id.b.domain = atio->u.isp24.fcp_hdr.s_id[0];
5390 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
5391 sess = qla2x00_find_fcport_by_nportid(vha, &id, 1);
5392 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
5394 qlt_send_term_exchange(qpair, NULL, atio, 1, 0);
5397 /* Sending marker isn't necessary, since we called from ISR */
5399 pkt = (request_t *)__qla2x00_alloc_iocbs(qpair, NULL);
5401 ql_dbg(ql_dbg_io, vha, 0x3063,
5402 "qla_target(%d): %s failed: unable to allocate "
5403 "request packet", vha->vp_idx, __func__);
5407 qpair->tgt_counters.num_q_full_sent++;
5408 pkt->entry_count = 1;
5409 pkt->handle = QLA_TGT_SKIP_HANDLE | CTIO_COMPLETION_HANDLE_MARK;
5411 ctio24 = (struct ctio7_to_24xx *)pkt;
5412 ctio24->entry_type = CTIO_TYPE7;
5413 ctio24->nport_handle = sess->loop_id;
5414 ctio24->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
5415 ctio24->vp_index = vha->vp_idx;
5416 ctio24->initiator_id[0] = atio->u.isp24.fcp_hdr.s_id[2];
5417 ctio24->initiator_id[1] = atio->u.isp24.fcp_hdr.s_id[1];
5418 ctio24->initiator_id[2] = atio->u.isp24.fcp_hdr.s_id[0];
5419 ctio24->exchange_addr = atio->u.isp24.exchange_addr;
5420 temp = (atio->u.isp24.attr << 9) |
5421 CTIO7_FLAGS_STATUS_MODE_1 | CTIO7_FLAGS_SEND_STATUS |
5422 CTIO7_FLAGS_DONT_RET_CTIO;
5423 ctio24->u.status1.flags = cpu_to_le16(temp);
5425 * CTIO from fw w/o se_cmd doesn't provide enough info to retry it,
5426 * if the explicit conformation is used.
5428 ctio24->u.status1.ox_id = swab16(atio->u.isp24.fcp_hdr.ox_id);
5429 ctio24->u.status1.scsi_status = cpu_to_le16(status);
5431 ctio24->u.status1.residual = get_datalen_for_atio(atio);
5433 if (ctio24->u.status1.residual != 0)
5434 ctio24->u.status1.scsi_status |= SS_RESIDUAL_UNDER;
5436 /* Memory Barrier */
5438 if (qpair->reqq_start_iocbs)
5439 qpair->reqq_start_iocbs(qpair);
5441 qla2x00_start_iocbs(vha, qpair->req);
5446 * This routine is used to allocate a command for either a QFull condition
5447 * (ie reply SAM_STAT_BUSY) or to terminate an exchange that did not go
5451 qlt_alloc_qfull_cmd(struct scsi_qla_host *vha,
5452 struct atio_from_isp *atio, uint16_t status, int qfull)
5454 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
5455 struct qla_hw_data *ha = vha->hw;
5456 struct fc_port *sess;
5457 struct se_session *se_sess;
5458 struct qla_tgt_cmd *cmd;
5460 unsigned long flags;
5462 if (unlikely(tgt->tgt_stop)) {
5463 ql_dbg(ql_dbg_io, vha, 0x300a,
5464 "New command while device %p is shutting down\n", tgt);
5468 if ((vha->hw->tgt.num_qfull_cmds_alloc + 1) > MAX_QFULL_CMDS_ALLOC) {
5469 vha->hw->tgt.num_qfull_cmds_dropped++;
5470 if (vha->hw->tgt.num_qfull_cmds_dropped >
5471 vha->qla_stats.stat_max_qfull_cmds_dropped)
5472 vha->qla_stats.stat_max_qfull_cmds_dropped =
5473 vha->hw->tgt.num_qfull_cmds_dropped;
5475 ql_dbg(ql_dbg_io, vha, 0x3068,
5476 "qla_target(%d): %s: QFull CMD dropped[%d]\n",
5477 vha->vp_idx, __func__,
5478 vha->hw->tgt.num_qfull_cmds_dropped);
5480 qlt_chk_exch_leak_thresh_hold(vha);
5484 sess = ha->tgt.tgt_ops->find_sess_by_s_id
5485 (vha, atio->u.isp24.fcp_hdr.s_id);
5489 se_sess = sess->se_sess;
5491 tag = sbitmap_queue_get(&se_sess->sess_tag_pool, &cpu);
5495 cmd = &((struct qla_tgt_cmd *)se_sess->sess_cmd_map)[tag];
5497 ql_dbg(ql_dbg_io, vha, 0x3009,
5498 "qla_target(%d): %s: Allocation of cmd failed\n",
5499 vha->vp_idx, __func__);
5501 vha->hw->tgt.num_qfull_cmds_dropped++;
5502 if (vha->hw->tgt.num_qfull_cmds_dropped >
5503 vha->qla_stats.stat_max_qfull_cmds_dropped)
5504 vha->qla_stats.stat_max_qfull_cmds_dropped =
5505 vha->hw->tgt.num_qfull_cmds_dropped;
5507 qlt_chk_exch_leak_thresh_hold(vha);
5511 memset(cmd, 0, sizeof(struct qla_tgt_cmd));
5513 qlt_incr_num_pend_cmds(vha);
5514 INIT_LIST_HEAD(&cmd->cmd_list);
5515 memcpy(&cmd->atio, atio, sizeof(*atio));
5517 cmd->tgt = vha->vha_tgt.qla_tgt;
5519 cmd->reset_count = ha->base_qpair->chip_reset;
5521 cmd->qpair = ha->base_qpair;
5522 cmd->se_cmd.map_cpu = cpu;
5526 /* NOTE: borrowing the state field to carry the status */
5527 cmd->state = status;
5529 cmd->term_exchg = 1;
5531 spin_lock_irqsave(&vha->hw->tgt.q_full_lock, flags);
5532 list_add_tail(&cmd->cmd_list, &vha->hw->tgt.q_full_list);
5534 vha->hw->tgt.num_qfull_cmds_alloc++;
5535 if (vha->hw->tgt.num_qfull_cmds_alloc >
5536 vha->qla_stats.stat_max_qfull_cmds_alloc)
5537 vha->qla_stats.stat_max_qfull_cmds_alloc =
5538 vha->hw->tgt.num_qfull_cmds_alloc;
5539 spin_unlock_irqrestore(&vha->hw->tgt.q_full_lock, flags);
5543 qlt_free_qfull_cmds(struct qla_qpair *qpair)
5545 struct scsi_qla_host *vha = qpair->vha;
5546 struct qla_hw_data *ha = vha->hw;
5547 unsigned long flags;
5548 struct qla_tgt_cmd *cmd, *tcmd;
5549 struct list_head free_list, q_full_list;
5552 if (list_empty(&ha->tgt.q_full_list))
5555 INIT_LIST_HEAD(&free_list);
5556 INIT_LIST_HEAD(&q_full_list);
5558 spin_lock_irqsave(&vha->hw->tgt.q_full_lock, flags);
5559 if (list_empty(&ha->tgt.q_full_list)) {
5560 spin_unlock_irqrestore(&vha->hw->tgt.q_full_lock, flags);
5564 list_splice_init(&vha->hw->tgt.q_full_list, &q_full_list);
5565 spin_unlock_irqrestore(&vha->hw->tgt.q_full_lock, flags);
5567 spin_lock_irqsave(qpair->qp_lock_ptr, flags);
5568 list_for_each_entry_safe(cmd, tcmd, &q_full_list, cmd_list) {
5570 /* cmd->state is a borrowed field to hold status */
5571 rc = __qlt_send_busy(qpair, &cmd->atio, cmd->state);
5572 else if (cmd->term_exchg)
5573 rc = __qlt_send_term_exchange(qpair, NULL, &cmd->atio);
5579 ql_dbg(ql_dbg_io, vha, 0x3006,
5580 "%s: busy sent for ox_id[%04x]\n", __func__,
5581 be16_to_cpu(cmd->atio.u.isp24.fcp_hdr.ox_id));
5582 else if (cmd->term_exchg)
5583 ql_dbg(ql_dbg_io, vha, 0x3007,
5584 "%s: Term exchg sent for ox_id[%04x]\n", __func__,
5585 be16_to_cpu(cmd->atio.u.isp24.fcp_hdr.ox_id));
5587 ql_dbg(ql_dbg_io, vha, 0x3008,
5588 "%s: Unexpected cmd in QFull list %p\n", __func__,
5591 list_del(&cmd->cmd_list);
5592 list_add_tail(&cmd->cmd_list, &free_list);
5594 /* piggy back on hardware_lock for protection */
5595 vha->hw->tgt.num_qfull_cmds_alloc--;
5597 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
5601 list_for_each_entry_safe(cmd, tcmd, &free_list, cmd_list) {
5602 list_del(&cmd->cmd_list);
5603 /* This cmd was never sent to TCM. There is no need
5604 * to schedule free or call free_cmd
5609 if (!list_empty(&q_full_list)) {
5610 spin_lock_irqsave(&vha->hw->tgt.q_full_lock, flags);
5611 list_splice(&q_full_list, &vha->hw->tgt.q_full_list);
5612 spin_unlock_irqrestore(&vha->hw->tgt.q_full_lock, flags);
5619 qlt_send_busy(struct qla_qpair *qpair, struct atio_from_isp *atio,
5623 struct scsi_qla_host *vha = qpair->vha;
5625 rc = __qlt_send_busy(qpair, atio, status);
5627 qlt_alloc_qfull_cmd(vha, atio, status, 1);
5631 qlt_chk_qfull_thresh_hold(struct scsi_qla_host *vha, struct qla_qpair *qpair,
5632 struct atio_from_isp *atio, uint8_t ha_locked)
5634 struct qla_hw_data *ha = vha->hw;
5635 unsigned long flags;
5637 if (ha->tgt.num_pend_cmds < Q_FULL_THRESH_HOLD(ha))
5641 spin_lock_irqsave(&ha->hardware_lock, flags);
5642 qlt_send_busy(qpair, atio, qla_sam_status);
5644 spin_unlock_irqrestore(&ha->hardware_lock, flags);
5649 /* ha->hardware_lock supposed to be held on entry */
5650 /* called via callback from qla2xxx */
5651 static void qlt_24xx_atio_pkt(struct scsi_qla_host *vha,
5652 struct atio_from_isp *atio, uint8_t ha_locked)
5654 struct qla_hw_data *ha = vha->hw;
5655 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
5657 unsigned long flags = 0;
5659 if (unlikely(tgt == NULL)) {
5660 ql_dbg(ql_dbg_tgt, vha, 0x3064,
5661 "ATIO pkt, but no tgt (ha %p)", ha);
5665 * In tgt_stop mode we also should allow all requests to pass.
5666 * Otherwise, some commands can stuck.
5669 tgt->atio_irq_cmd_count++;
5671 switch (atio->u.raw.entry_type) {
5673 if (unlikely(atio->u.isp24.exchange_addr ==
5674 ATIO_EXCHANGE_ADDRESS_UNKNOWN)) {
5675 ql_dbg(ql_dbg_io, vha, 0x3065,
5676 "qla_target(%d): ATIO_TYPE7 "
5677 "received with UNKNOWN exchange address, "
5678 "sending QUEUE_FULL\n", vha->vp_idx);
5680 spin_lock_irqsave(&ha->hardware_lock, flags);
5681 qlt_send_busy(ha->base_qpair, atio, qla_sam_status);
5683 spin_unlock_irqrestore(&ha->hardware_lock,
5688 if (likely(atio->u.isp24.fcp_cmnd.task_mgmt_flags == 0)) {
5689 rc = qlt_chk_qfull_thresh_hold(vha, ha->base_qpair,
5692 tgt->atio_irq_cmd_count--;
5695 rc = qlt_handle_cmd_for_atio(vha, atio);
5697 rc = qlt_handle_task_mgmt(vha, atio);
5699 if (unlikely(rc != 0)) {
5701 spin_lock_irqsave(&ha->hardware_lock, flags);
5704 ql_dbg(ql_dbg_tgt, vha, 0xe05f,
5705 "qla_target: Unable to send command to target\n");
5708 ql_dbg(ql_dbg_tgt, vha, 0xe05f,
5709 "qla_target: Unable to send command to target, sending TERM EXCHANGE for rsp\n");
5710 qlt_send_term_exchange(ha->base_qpair, NULL,
5714 ql_dbg(ql_dbg_tgt, vha, 0xe060,
5715 "qla_target(%d): Unable to send command to target, sending BUSY status\n",
5717 qlt_send_busy(ha->base_qpair, atio,
5721 ql_dbg(ql_dbg_tgt, vha, 0xe060,
5722 "qla_target(%d): Unable to send command to target, sending BUSY status\n",
5724 qlt_send_busy(ha->base_qpair, atio,
5729 spin_unlock_irqrestore(&ha->hardware_lock,
5734 case IMMED_NOTIFY_TYPE:
5736 if (unlikely(atio->u.isp2x.entry_status != 0)) {
5737 ql_dbg(ql_dbg_tgt, vha, 0xe05b,
5738 "qla_target(%d): Received ATIO packet %x "
5739 "with error status %x\n", vha->vp_idx,
5740 atio->u.raw.entry_type,
5741 atio->u.isp2x.entry_status);
5744 ql_dbg(ql_dbg_tgt, vha, 0xe02e, "%s", "IMMED_NOTIFY ATIO");
5747 spin_lock_irqsave(&ha->hardware_lock, flags);
5748 qlt_handle_imm_notify(vha, (struct imm_ntfy_from_isp *)atio);
5750 spin_unlock_irqrestore(&ha->hardware_lock, flags);
5755 ql_dbg(ql_dbg_tgt, vha, 0xe05c,
5756 "qla_target(%d): Received unknown ATIO atio "
5757 "type %x\n", vha->vp_idx, atio->u.raw.entry_type);
5761 tgt->atio_irq_cmd_count--;
5765 * qpair lock is assume to be held
5766 * rc = 0 : send terminate & abts respond
5767 * rc != 0: do not send term & abts respond
5769 static int qlt_chk_unresolv_exchg(struct scsi_qla_host *vha,
5770 struct qla_qpair *qpair, struct abts_resp_from_24xx_fw *entry)
5772 struct qla_hw_data *ha = vha->hw;
5776 * Detect unresolved exchange. If the same ABTS is unable
5777 * to terminate an existing command and the same ABTS loops
5778 * between FW & Driver, then force FW dump. Under 1 jiff,
5779 * we should see multiple loops.
5781 if (qpair->retry_term_exchg_addr == entry->exchange_addr_to_abort &&
5782 qpair->retry_term_jiff == jiffies) {
5783 /* found existing exchange */
5784 qpair->retry_term_cnt++;
5785 if (qpair->retry_term_cnt >= 5) {
5787 qpair->retry_term_cnt = 0;
5788 ql_log(ql_log_warn, vha, 0xffff,
5789 "Unable to send ABTS Respond. Dumping firmware.\n");
5790 ql_dump_buffer(ql_dbg_tgt_mgt + ql_dbg_buffer,
5791 vha, 0xffff, (uint8_t *)entry, sizeof(*entry));
5793 if (qpair == ha->base_qpair)
5794 ha->isp_ops->fw_dump(vha, 1);
5796 ha->isp_ops->fw_dump(vha, 0);
5798 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
5799 qla2xxx_wake_dpc(vha);
5801 } else if (qpair->retry_term_jiff != jiffies) {
5802 qpair->retry_term_exchg_addr = entry->exchange_addr_to_abort;
5803 qpair->retry_term_cnt = 0;
5804 qpair->retry_term_jiff = jiffies;
5811 static void qlt_handle_abts_completion(struct scsi_qla_host *vha,
5812 struct rsp_que *rsp, response_t *pkt)
5814 struct abts_resp_from_24xx_fw *entry =
5815 (struct abts_resp_from_24xx_fw *)pkt;
5816 u32 h = pkt->handle & ~QLA_TGT_HANDLE_MASK;
5817 struct qla_tgt_mgmt_cmd *mcmd;
5818 struct qla_hw_data *ha = vha->hw;
5820 mcmd = (struct qla_tgt_mgmt_cmd *)qlt_ctio_to_cmd(vha, rsp,
5822 if (mcmd == NULL && h != QLA_TGT_SKIP_HANDLE) {
5823 ql_dbg(ql_dbg_async, vha, 0xe064,
5824 "qla_target(%d): ABTS Comp without mcmd\n",
5831 vha->vha_tgt.qla_tgt->abts_resp_expected--;
5833 ql_dbg(ql_dbg_tgt, vha, 0xe038,
5834 "ABTS_RESP_24XX: compl_status %x\n",
5835 entry->compl_status);
5837 if (le16_to_cpu(entry->compl_status) != ABTS_RESP_COMPL_SUCCESS) {
5838 if ((entry->error_subcode1 == 0x1E) &&
5839 (entry->error_subcode2 == 0)) {
5840 if (qlt_chk_unresolv_exchg(vha, rsp->qpair, entry)) {
5841 ha->tgt.tgt_ops->free_mcmd(mcmd);
5844 qlt_24xx_retry_term_exchange(vha, rsp->qpair,
5847 ql_dbg(ql_dbg_tgt, vha, 0xe063,
5848 "qla_target(%d): ABTS_RESP_24XX failed %x (subcode %x:%x)",
5849 vha->vp_idx, entry->compl_status,
5850 entry->error_subcode1,
5851 entry->error_subcode2);
5852 ha->tgt.tgt_ops->free_mcmd(mcmd);
5855 ha->tgt.tgt_ops->free_mcmd(mcmd);
5859 /* ha->hardware_lock supposed to be held on entry */
5860 /* called via callback from qla2xxx */
5861 static void qlt_response_pkt(struct scsi_qla_host *vha,
5862 struct rsp_que *rsp, response_t *pkt)
5864 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
5866 if (unlikely(tgt == NULL)) {
5867 ql_dbg(ql_dbg_tgt, vha, 0xe05d,
5868 "qla_target(%d): Response pkt %x received, but no tgt (ha %p)\n",
5869 vha->vp_idx, pkt->entry_type, vha->hw);
5874 * In tgt_stop mode we also should allow all requests to pass.
5875 * Otherwise, some commands can stuck.
5878 switch (pkt->entry_type) {
5882 struct ctio7_from_24xx *entry = (struct ctio7_from_24xx *)pkt;
5883 qlt_do_ctio_completion(vha, rsp, entry->handle,
5884 le16_to_cpu(entry->status)|(pkt->entry_status << 16),
5889 case ACCEPT_TGT_IO_TYPE:
5891 struct atio_from_isp *atio = (struct atio_from_isp *)pkt;
5893 if (atio->u.isp2x.status !=
5894 cpu_to_le16(ATIO_CDB_VALID)) {
5895 ql_dbg(ql_dbg_tgt, vha, 0xe05e,
5896 "qla_target(%d): ATIO with error "
5897 "status %x received\n", vha->vp_idx,
5898 le16_to_cpu(atio->u.isp2x.status));
5902 rc = qlt_chk_qfull_thresh_hold(vha, rsp->qpair, atio, 1);
5906 rc = qlt_handle_cmd_for_atio(vha, atio);
5907 if (unlikely(rc != 0)) {
5910 ql_dbg(ql_dbg_tgt, vha, 0xe05f,
5911 "qla_target: Unable to send command to target\n");
5914 ql_dbg(ql_dbg_tgt, vha, 0xe05f,
5915 "qla_target: Unable to send command to target, sending TERM EXCHANGE for rsp\n");
5916 qlt_send_term_exchange(rsp->qpair, NULL,
5920 ql_dbg(ql_dbg_tgt, vha, 0xe060,
5921 "qla_target(%d): Unable to send command to target, sending BUSY status\n",
5923 qlt_send_busy(rsp->qpair, atio,
5927 ql_dbg(ql_dbg_tgt, vha, 0xe060,
5928 "qla_target(%d): Unable to send command to target, sending BUSY status\n",
5930 qlt_send_busy(rsp->qpair, atio,
5938 case CONTINUE_TGT_IO_TYPE:
5940 struct ctio_to_2xxx *entry = (struct ctio_to_2xxx *)pkt;
5941 qlt_do_ctio_completion(vha, rsp, entry->handle,
5942 le16_to_cpu(entry->status)|(pkt->entry_status << 16),
5949 struct ctio_to_2xxx *entry = (struct ctio_to_2xxx *)pkt;
5950 qlt_do_ctio_completion(vha, rsp, entry->handle,
5951 le16_to_cpu(entry->status)|(pkt->entry_status << 16),
5956 case IMMED_NOTIFY_TYPE:
5957 ql_dbg(ql_dbg_tgt, vha, 0xe035, "%s", "IMMED_NOTIFY\n");
5958 qlt_handle_imm_notify(vha, (struct imm_ntfy_from_isp *)pkt);
5961 case NOTIFY_ACK_TYPE:
5962 if (tgt->notify_ack_expected > 0) {
5963 struct nack_to_isp *entry = (struct nack_to_isp *)pkt;
5964 ql_dbg(ql_dbg_tgt, vha, 0xe036,
5965 "NOTIFY_ACK seq %08x status %x\n",
5966 le16_to_cpu(entry->u.isp2x.seq_id),
5967 le16_to_cpu(entry->u.isp2x.status));
5968 tgt->notify_ack_expected--;
5969 if (entry->u.isp2x.status !=
5970 cpu_to_le16(NOTIFY_ACK_SUCCESS)) {
5971 ql_dbg(ql_dbg_tgt, vha, 0xe061,
5972 "qla_target(%d): NOTIFY_ACK "
5973 "failed %x\n", vha->vp_idx,
5974 le16_to_cpu(entry->u.isp2x.status));
5977 ql_dbg(ql_dbg_tgt, vha, 0xe062,
5978 "qla_target(%d): Unexpected NOTIFY_ACK received\n",
5983 case ABTS_RECV_24XX:
5984 ql_dbg(ql_dbg_tgt, vha, 0xe037,
5985 "ABTS_RECV_24XX: instance %d\n", vha->vp_idx);
5986 qlt_24xx_handle_abts(vha, (struct abts_recv_from_24xx *)pkt);
5989 case ABTS_RESP_24XX:
5990 if (tgt->abts_resp_expected > 0) {
5991 qlt_handle_abts_completion(vha, rsp, pkt);
5993 ql_dbg(ql_dbg_tgt, vha, 0xe064,
5994 "qla_target(%d): Unexpected ABTS_RESP_24XX "
5995 "received\n", vha->vp_idx);
6000 ql_dbg(ql_dbg_tgt, vha, 0xe065,
6001 "qla_target(%d): Received unknown response pkt "
6002 "type %x\n", vha->vp_idx, pkt->entry_type);
6009 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
6011 void qlt_async_event(uint16_t code, struct scsi_qla_host *vha,
6014 struct qla_hw_data *ha = vha->hw;
6015 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
6018 if (!tgt || tgt->tgt_stop || tgt->tgt_stopped)
6021 if (((code == MBA_POINT_TO_POINT) || (code == MBA_CHG_IN_CONNECTION)) &&
6025 * In tgt_stop mode we also should allow all requests to pass.
6026 * Otherwise, some commands can stuck.
6031 case MBA_RESET: /* Reset */
6032 case MBA_SYSTEM_ERR: /* System Error */
6033 case MBA_REQ_TRANSFER_ERR: /* Request Transfer Error */
6034 case MBA_RSP_TRANSFER_ERR: /* Response Transfer Error */
6035 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03a,
6036 "qla_target(%d): System error async event %#x "
6037 "occurred", vha->vp_idx, code);
6039 case MBA_WAKEUP_THRES: /* Request Queue Wake-up. */
6040 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
6045 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03b,
6046 "qla_target(%d): Async LOOP_UP occurred "
6047 "(m[0]=%x, m[1]=%x, m[2]=%x, m[3]=%x)", vha->vp_idx,
6048 le16_to_cpu(mailbox[0]), le16_to_cpu(mailbox[1]),
6049 le16_to_cpu(mailbox[2]), le16_to_cpu(mailbox[3]));
6050 if (tgt->link_reinit_iocb_pending) {
6051 qlt_send_notify_ack(ha->base_qpair,
6052 (void *)&tgt->link_reinit_iocb,
6054 tgt->link_reinit_iocb_pending = 0;
6059 case MBA_LIP_OCCURRED:
6062 case MBA_RSCN_UPDATE:
6063 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03c,
6064 "qla_target(%d): Async event %#x occurred "
6065 "(m[0]=%x, m[1]=%x, m[2]=%x, m[3]=%x)", vha->vp_idx, code,
6066 le16_to_cpu(mailbox[0]), le16_to_cpu(mailbox[1]),
6067 le16_to_cpu(mailbox[2]), le16_to_cpu(mailbox[3]));
6070 case MBA_REJECTED_FCP_CMD:
6071 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf017,
6072 "qla_target(%d): Async event LS_REJECT occurred (m[0]=%x, m[1]=%x, m[2]=%x, m[3]=%x)",
6074 le16_to_cpu(mailbox[0]), le16_to_cpu(mailbox[1]),
6075 le16_to_cpu(mailbox[2]), le16_to_cpu(mailbox[3]));
6077 if (le16_to_cpu(mailbox[3]) == 1) {
6078 /* exchange starvation. */
6079 vha->hw->exch_starvation++;
6080 if (vha->hw->exch_starvation > 5) {
6081 ql_log(ql_log_warn, vha, 0xd03a,
6082 "Exchange starvation-. Resetting RISC\n");
6084 vha->hw->exch_starvation = 0;
6085 if (IS_P3P_TYPE(vha->hw))
6086 set_bit(FCOE_CTX_RESET_NEEDED,
6089 set_bit(ISP_ABORT_NEEDED,
6091 qla2xxx_wake_dpc(vha);
6096 case MBA_PORT_UPDATE:
6097 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03d,
6098 "qla_target(%d): Port update async event %#x "
6099 "occurred: updating the ports database (m[0]=%x, m[1]=%x, "
6100 "m[2]=%x, m[3]=%x)", vha->vp_idx, code,
6101 le16_to_cpu(mailbox[0]), le16_to_cpu(mailbox[1]),
6102 le16_to_cpu(mailbox[2]), le16_to_cpu(mailbox[3]));
6104 login_code = le16_to_cpu(mailbox[2]);
6105 if (login_code == 0x4) {
6106 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03e,
6107 "Async MB 2: Got PLOGI Complete\n");
6108 vha->hw->exch_starvation = 0;
6109 } else if (login_code == 0x7)
6110 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03f,
6111 "Async MB 2: Port Logged Out\n");
6119 static fc_port_t *qlt_get_port_database(struct scsi_qla_host *vha,
6122 fc_port_t *fcport, *tfcp, *del;
6124 unsigned long flags;
6127 fcport = qla2x00_alloc_fcport(vha, GFP_KERNEL);
6129 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06f,
6130 "qla_target(%d): Allocation of tmp FC port failed",
6135 fcport->loop_id = loop_id;
6137 rc = qla24xx_gpdb_wait(vha, fcport, 0);
6138 if (rc != QLA_SUCCESS) {
6139 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf070,
6140 "qla_target(%d): Failed to retrieve fcport "
6141 "information -- get_port_database() returned %x "
6142 "(loop_id=0x%04x)", vha->vp_idx, rc, loop_id);
6148 spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
6149 tfcp = qla2x00_find_fcport_by_wwpn(vha, fcport->port_name, 1);
6152 tfcp->d_id = fcport->d_id;
6153 tfcp->port_type = fcport->port_type;
6154 tfcp->supported_classes = fcport->supported_classes;
6155 tfcp->flags |= fcport->flags;
6156 tfcp->scan_state = QLA_FCPORT_FOUND;
6161 if (vha->hw->current_topology == ISP_CFG_F)
6162 fcport->flags |= FCF_FABRIC_DEVICE;
6164 list_add_tail(&fcport->list, &vha->vp_fcports);
6165 if (!IS_SW_RESV_ADDR(fcport->d_id))
6166 vha->fcport_count++;
6167 fcport->login_gen++;
6168 fcport->disc_state = DSC_LOGIN_COMPLETE;
6169 fcport->login_succ = 1;
6173 fcport->deleted = 0;
6174 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
6176 switch (vha->host->active_mode) {
6177 case MODE_INITIATOR:
6180 if (!IS_IIDMA_CAPABLE(vha->hw) || !vha->hw->flags.gpsc_supported) {
6181 qla24xx_sched_upd_fcport(fcport);
6183 ql_dbg(ql_dbg_disc, vha, 0x20ff,
6184 "%s %d %8phC post gpsc fcp_cnt %d\n",
6185 __func__, __LINE__, fcport->port_name, vha->fcport_count);
6186 qla24xx_post_gpsc_work(vha, fcport);
6196 qla2x00_free_fcport(del);
6201 /* Must be called under tgt_mutex */
6202 static struct fc_port *qlt_make_local_sess(struct scsi_qla_host *vha,
6205 struct fc_port *sess = NULL;
6206 fc_port_t *fcport = NULL;
6207 int rc, global_resets;
6208 uint16_t loop_id = 0;
6210 if ((s_id[0] == 0xFF) && (s_id[1] == 0xFC)) {
6212 * This is Domain Controller, so it should be
6213 * OK to drop SCSI commands from it.
6215 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf042,
6216 "Unable to find initiator with S_ID %x:%x:%x",
6217 s_id[0], s_id[1], s_id[2]);
6221 mutex_lock(&vha->vha_tgt.tgt_mutex);
6225 atomic_read(&vha->vha_tgt.qla_tgt->tgt_global_resets_count);
6227 rc = qla24xx_get_loop_id(vha, s_id, &loop_id);
6229 mutex_unlock(&vha->vha_tgt.tgt_mutex);
6231 ql_log(ql_log_info, vha, 0xf071,
6232 "qla_target(%d): Unable to find "
6233 "initiator with S_ID %x:%x:%x",
6234 vha->vp_idx, s_id[0], s_id[1],
6237 if (rc == -ENOENT) {
6238 qlt_port_logo_t logo;
6239 sid_to_portid(s_id, &logo.id);
6241 qlt_send_first_logo(vha, &logo);
6247 fcport = qlt_get_port_database(vha, loop_id);
6249 mutex_unlock(&vha->vha_tgt.tgt_mutex);
6253 if (global_resets !=
6254 atomic_read(&vha->vha_tgt.qla_tgt->tgt_global_resets_count)) {
6255 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf043,
6256 "qla_target(%d): global reset during session discovery "
6257 "(counter was %d, new %d), retrying", vha->vp_idx,
6259 atomic_read(&vha->vha_tgt.
6260 qla_tgt->tgt_global_resets_count));
6264 sess = qlt_create_sess(vha, fcport, true);
6266 mutex_unlock(&vha->vha_tgt.tgt_mutex);
6271 static void qlt_abort_work(struct qla_tgt *tgt,
6272 struct qla_tgt_sess_work_param *prm)
6274 struct scsi_qla_host *vha = tgt->vha;
6275 struct qla_hw_data *ha = vha->hw;
6276 struct fc_port *sess = NULL;
6277 unsigned long flags = 0, flags2 = 0;
6282 spin_lock_irqsave(&ha->tgt.sess_lock, flags2);
6287 s_id[0] = prm->abts.fcp_hdr_le.s_id[2];
6288 s_id[1] = prm->abts.fcp_hdr_le.s_id[1];
6289 s_id[2] = prm->abts.fcp_hdr_le.s_id[0];
6291 sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha,
6292 (unsigned char *)&be_s_id);
6294 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags2);
6296 sess = qlt_make_local_sess(vha, s_id);
6297 /* sess has got an extra creation ref */
6299 spin_lock_irqsave(&ha->tgt.sess_lock, flags2);
6303 if (sess->deleted) {
6308 if (!kref_get_unless_zero(&sess->sess_kref)) {
6309 ql_dbg(ql_dbg_tgt_tmr, vha, 0xf01c,
6310 "%s: kref_get fail %8phC \n",
6311 __func__, sess->port_name);
6317 rc = __qlt_24xx_handle_abts(vha, &prm->abts, sess);
6318 ha->tgt.tgt_ops->put_sess(sess);
6319 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags2);
6327 ha->tgt.tgt_ops->put_sess(sess);
6328 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags2);
6331 spin_lock_irqsave(&ha->hardware_lock, flags);
6332 qlt_24xx_send_abts_resp(ha->base_qpair, &prm->abts,
6333 FCP_TMF_REJECTED, false);
6334 spin_unlock_irqrestore(&ha->hardware_lock, flags);
6337 static void qlt_tmr_work(struct qla_tgt *tgt,
6338 struct qla_tgt_sess_work_param *prm)
6340 struct atio_from_isp *a = &prm->tm_iocb2;
6341 struct scsi_qla_host *vha = tgt->vha;
6342 struct qla_hw_data *ha = vha->hw;
6343 struct fc_port *sess = NULL;
6344 unsigned long flags;
6345 uint8_t *s_id = NULL; /* to hide compiler warnings */
6351 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
6356 s_id = prm->tm_iocb2.u.isp24.fcp_hdr.s_id;
6357 sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha, s_id);
6359 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
6361 sess = qlt_make_local_sess(vha, s_id);
6362 /* sess has got an extra creation ref */
6364 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
6368 if (sess->deleted) {
6373 if (!kref_get_unless_zero(&sess->sess_kref)) {
6374 ql_dbg(ql_dbg_tgt_tmr, vha, 0xf020,
6375 "%s: kref_get fail %8phC\n",
6376 __func__, sess->port_name);
6383 fn = a->u.isp24.fcp_cmnd.task_mgmt_flags;
6385 scsilun_to_int((struct scsi_lun *)&a->u.isp24.fcp_cmnd.lun);
6387 rc = qlt_issue_task_mgmt(sess, unpacked_lun, fn, iocb, 0);
6388 ha->tgt.tgt_ops->put_sess(sess);
6389 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
6397 ha->tgt.tgt_ops->put_sess(sess);
6398 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
6400 qlt_send_term_exchange(ha->base_qpair, NULL, &prm->tm_iocb2, 1, 0);
6403 static void qlt_sess_work_fn(struct work_struct *work)
6405 struct qla_tgt *tgt = container_of(work, struct qla_tgt, sess_work);
6406 struct scsi_qla_host *vha = tgt->vha;
6407 unsigned long flags;
6409 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf000, "Sess work (tgt %p)", tgt);
6411 spin_lock_irqsave(&tgt->sess_work_lock, flags);
6412 while (!list_empty(&tgt->sess_works_list)) {
6413 struct qla_tgt_sess_work_param *prm = list_entry(
6414 tgt->sess_works_list.next, typeof(*prm),
6415 sess_works_list_entry);
6418 * This work can be scheduled on several CPUs at time, so we
6419 * must delete the entry to eliminate double processing
6421 list_del(&prm->sess_works_list_entry);
6423 spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
6425 switch (prm->type) {
6426 case QLA_TGT_SESS_WORK_ABORT:
6427 qlt_abort_work(tgt, prm);
6429 case QLA_TGT_SESS_WORK_TM:
6430 qlt_tmr_work(tgt, prm);
6437 spin_lock_irqsave(&tgt->sess_work_lock, flags);
6441 spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
6444 /* Must be called under tgt_host_action_mutex */
6445 int qlt_add_target(struct qla_hw_data *ha, struct scsi_qla_host *base_vha)
6447 struct qla_tgt *tgt;
6449 struct qla_qpair_hint *h;
6451 if (!QLA_TGT_MODE_ENABLED())
6454 if (!IS_TGT_MODE_CAPABLE(ha)) {
6455 ql_log(ql_log_warn, base_vha, 0xe070,
6456 "This adapter does not support target mode.\n");
6460 ql_dbg(ql_dbg_tgt, base_vha, 0xe03b,
6461 "Registering target for host %ld(%p).\n", base_vha->host_no, ha);
6463 BUG_ON(base_vha->vha_tgt.qla_tgt != NULL);
6465 tgt = kzalloc(sizeof(struct qla_tgt), GFP_KERNEL);
6467 ql_dbg(ql_dbg_tgt, base_vha, 0xe066,
6468 "Unable to allocate struct qla_tgt\n");
6472 tgt->qphints = kcalloc(ha->max_qpairs + 1,
6473 sizeof(struct qla_qpair_hint),
6475 if (!tgt->qphints) {
6477 ql_log(ql_log_warn, base_vha, 0x0197,
6478 "Unable to allocate qpair hints.\n");
6482 if (!(base_vha->host->hostt->supported_mode & MODE_TARGET))
6483 base_vha->host->hostt->supported_mode |= MODE_TARGET;
6485 rc = btree_init64(&tgt->lun_qpair_map);
6487 kfree(tgt->qphints);
6489 ql_log(ql_log_info, base_vha, 0x0198,
6490 "Unable to initialize lun_qpair_map btree\n");
6493 h = &tgt->qphints[0];
6494 h->qpair = ha->base_qpair;
6495 INIT_LIST_HEAD(&h->hint_elem);
6496 h->cpuid = ha->base_qpair->cpuid;
6497 list_add_tail(&h->hint_elem, &ha->base_qpair->hints_list);
6499 for (i = 0; i < ha->max_qpairs; i++) {
6500 unsigned long flags;
6502 struct qla_qpair *qpair = ha->queue_pair_map[i];
6503 h = &tgt->qphints[i + 1];
6504 INIT_LIST_HEAD(&h->hint_elem);
6507 spin_lock_irqsave(qpair->qp_lock_ptr, flags);
6508 list_add_tail(&h->hint_elem, &qpair->hints_list);
6509 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
6510 h->cpuid = qpair->cpuid;
6515 tgt->vha = base_vha;
6516 init_waitqueue_head(&tgt->waitQ);
6517 INIT_LIST_HEAD(&tgt->del_sess_list);
6518 spin_lock_init(&tgt->sess_work_lock);
6519 INIT_WORK(&tgt->sess_work, qlt_sess_work_fn);
6520 INIT_LIST_HEAD(&tgt->sess_works_list);
6521 atomic_set(&tgt->tgt_global_resets_count, 0);
6523 base_vha->vha_tgt.qla_tgt = tgt;
6525 ql_dbg(ql_dbg_tgt, base_vha, 0xe067,
6526 "qla_target(%d): using 64 Bit PCI addressing",
6529 tgt->sg_tablesize = QLA_TGT_MAX_SG_24XX(base_vha->req->length - 3);
6531 mutex_lock(&qla_tgt_mutex);
6532 list_add_tail(&tgt->tgt_list_entry, &qla_tgt_glist);
6533 mutex_unlock(&qla_tgt_mutex);
6535 if (ha->tgt.tgt_ops && ha->tgt.tgt_ops->add_target)
6536 ha->tgt.tgt_ops->add_target(base_vha);
6541 /* Must be called under tgt_host_action_mutex */
6542 int qlt_remove_target(struct qla_hw_data *ha, struct scsi_qla_host *vha)
6544 if (!vha->vha_tgt.qla_tgt)
6547 if (vha->fc_vport) {
6548 qlt_release(vha->vha_tgt.qla_tgt);
6552 /* free left over qfull cmds */
6553 qlt_init_term_exchange(vha);
6555 ql_dbg(ql_dbg_tgt, vha, 0xe03c, "Unregistering target for host %ld(%p)",
6557 qlt_release(vha->vha_tgt.qla_tgt);
6562 void qlt_remove_target_resources(struct qla_hw_data *ha)
6564 struct scsi_qla_host *node;
6567 btree_for_each_safe32(&ha->tgt.host_map, key, node)
6568 btree_remove32(&ha->tgt.host_map, key);
6570 btree_destroy32(&ha->tgt.host_map);
6573 static void qlt_lport_dump(struct scsi_qla_host *vha, u64 wwpn,
6578 pr_debug("qla2xxx HW vha->node_name: ");
6579 for (i = 0; i < WWN_SIZE; i++)
6580 pr_debug("%02x ", vha->node_name[i]);
6582 pr_debug("qla2xxx HW vha->port_name: ");
6583 for (i = 0; i < WWN_SIZE; i++)
6584 pr_debug("%02x ", vha->port_name[i]);
6587 pr_debug("qla2xxx passed configfs WWPN: ");
6588 put_unaligned_be64(wwpn, b);
6589 for (i = 0; i < WWN_SIZE; i++)
6590 pr_debug("%02x ", b[i]);
6595 * qla_tgt_lport_register - register lport with external module
6597 * @target_lport_ptr: pointer for tcm_qla2xxx specific lport data
6598 * @phys_wwpn: physical port WWPN
6599 * @npiv_wwpn: NPIV WWPN
6600 * @npiv_wwnn: NPIV WWNN
6601 * @callback: lport initialization callback for tcm_qla2xxx code
6603 int qlt_lport_register(void *target_lport_ptr, u64 phys_wwpn,
6604 u64 npiv_wwpn, u64 npiv_wwnn,
6605 int (*callback)(struct scsi_qla_host *, void *, u64, u64))
6607 struct qla_tgt *tgt;
6608 struct scsi_qla_host *vha;
6609 struct qla_hw_data *ha;
6610 struct Scsi_Host *host;
6611 unsigned long flags;
6615 mutex_lock(&qla_tgt_mutex);
6616 list_for_each_entry(tgt, &qla_tgt_glist, tgt_list_entry) {
6624 if (!(host->hostt->supported_mode & MODE_TARGET))
6627 if (vha->qlini_mode == QLA2XXX_INI_MODE_ENABLED)
6630 spin_lock_irqsave(&ha->hardware_lock, flags);
6631 if ((!npiv_wwpn || !npiv_wwnn) && host->active_mode & MODE_TARGET) {
6632 pr_debug("MODE_TARGET already active on qla2xxx(%d)\n",
6634 spin_unlock_irqrestore(&ha->hardware_lock, flags);
6637 if (tgt->tgt_stop) {
6638 pr_debug("MODE_TARGET in shutdown on qla2xxx(%d)\n",
6640 spin_unlock_irqrestore(&ha->hardware_lock, flags);
6643 spin_unlock_irqrestore(&ha->hardware_lock, flags);
6645 if (!scsi_host_get(host)) {
6646 ql_dbg(ql_dbg_tgt, vha, 0xe068,
6647 "Unable to scsi_host_get() for"
6648 " qla2xxx scsi_host\n");
6651 qlt_lport_dump(vha, phys_wwpn, b);
6653 if (memcmp(vha->port_name, b, WWN_SIZE)) {
6654 scsi_host_put(host);
6657 rc = (*callback)(vha, target_lport_ptr, npiv_wwpn, npiv_wwnn);
6659 scsi_host_put(host);
6661 mutex_unlock(&qla_tgt_mutex);
6664 mutex_unlock(&qla_tgt_mutex);
6668 EXPORT_SYMBOL(qlt_lport_register);
6671 * qla_tgt_lport_deregister - Degister lport
6673 * @vha: Registered scsi_qla_host pointer
6675 void qlt_lport_deregister(struct scsi_qla_host *vha)
6677 struct qla_hw_data *ha = vha->hw;
6678 struct Scsi_Host *sh = vha->host;
6680 * Clear the target_lport_ptr qla_target_template pointer in qla_hw_data
6682 vha->vha_tgt.target_lport_ptr = NULL;
6683 ha->tgt.tgt_ops = NULL;
6685 * Release the Scsi_Host reference for the underlying qla2xxx host
6689 EXPORT_SYMBOL(qlt_lport_deregister);
6691 /* Must be called under HW lock */
6692 void qlt_set_mode(struct scsi_qla_host *vha)
6694 switch (vha->qlini_mode) {
6695 case QLA2XXX_INI_MODE_DISABLED:
6696 case QLA2XXX_INI_MODE_EXCLUSIVE:
6697 vha->host->active_mode = MODE_TARGET;
6699 case QLA2XXX_INI_MODE_ENABLED:
6700 vha->host->active_mode = MODE_INITIATOR;
6702 case QLA2XXX_INI_MODE_DUAL:
6703 vha->host->active_mode = MODE_DUAL;
6710 /* Must be called under HW lock */
6711 static void qlt_clear_mode(struct scsi_qla_host *vha)
6713 switch (vha->qlini_mode) {
6714 case QLA2XXX_INI_MODE_DISABLED:
6715 vha->host->active_mode = MODE_UNKNOWN;
6717 case QLA2XXX_INI_MODE_EXCLUSIVE:
6718 vha->host->active_mode = MODE_INITIATOR;
6720 case QLA2XXX_INI_MODE_ENABLED:
6721 case QLA2XXX_INI_MODE_DUAL:
6722 vha->host->active_mode = MODE_INITIATOR;
6730 * qla_tgt_enable_vha - NO LOCK HELD
6732 * host_reset, bring up w/ Target Mode Enabled
6735 qlt_enable_vha(struct scsi_qla_host *vha)
6737 struct qla_hw_data *ha = vha->hw;
6738 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
6739 unsigned long flags;
6740 scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
6743 ql_dbg(ql_dbg_tgt, vha, 0xe069,
6744 "Unable to locate qla_tgt pointer from"
6745 " struct qla_hw_data\n");
6749 if (vha->qlini_mode == QLA2XXX_INI_MODE_ENABLED)
6752 spin_lock_irqsave(&ha->hardware_lock, flags);
6753 tgt->tgt_stopped = 0;
6755 spin_unlock_irqrestore(&ha->hardware_lock, flags);
6757 mutex_lock(&ha->optrom_mutex);
6758 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf021,
6761 qla24xx_disable_vp(vha);
6762 qla24xx_enable_vp(vha);
6764 set_bit(ISP_ABORT_NEEDED, &base_vha->dpc_flags);
6765 qla2xxx_wake_dpc(base_vha);
6766 qla2x00_wait_for_hba_online(base_vha);
6768 mutex_unlock(&ha->optrom_mutex);
6770 EXPORT_SYMBOL(qlt_enable_vha);
6773 * qla_tgt_disable_vha - NO LOCK HELD
6775 * Disable Target Mode and reset the adapter
6777 static void qlt_disable_vha(struct scsi_qla_host *vha)
6779 struct qla_hw_data *ha = vha->hw;
6780 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
6781 unsigned long flags;
6784 ql_dbg(ql_dbg_tgt, vha, 0xe06a,
6785 "Unable to locate qla_tgt pointer from"
6786 " struct qla_hw_data\n");
6791 spin_lock_irqsave(&ha->hardware_lock, flags);
6792 qlt_clear_mode(vha);
6793 spin_unlock_irqrestore(&ha->hardware_lock, flags);
6795 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
6796 qla2xxx_wake_dpc(vha);
6797 qla2x00_wait_for_hba_online(vha);
6801 * Called from qla_init.c:qla24xx_vport_create() contex to setup
6802 * the target mode specific struct scsi_qla_host and struct qla_hw_data
6806 qlt_vport_create(struct scsi_qla_host *vha, struct qla_hw_data *ha)
6808 vha->vha_tgt.qla_tgt = NULL;
6810 mutex_init(&vha->vha_tgt.tgt_mutex);
6811 mutex_init(&vha->vha_tgt.tgt_host_action_mutex);
6813 qlt_clear_mode(vha);
6816 * NOTE: Currently the value is kept the same for <24xx and
6817 * >=24xx ISPs. If it is necessary to change it,
6818 * the check should be added for specific ISPs,
6819 * assigning the value appropriately.
6821 ha->tgt.atio_q_length = ATIO_ENTRY_CNT_24XX;
6823 qlt_add_target(ha, vha);
6827 qlt_rff_id(struct scsi_qla_host *vha)
6831 * FC-4 Feature bit 0 indicates target functionality to the name server.
6833 if (qla_tgt_mode_enabled(vha)) {
6834 fc4_feature = BIT_0;
6835 } else if (qla_ini_mode_enabled(vha)) {
6836 fc4_feature = BIT_1;
6837 } else if (qla_dual_mode_enabled(vha))
6838 fc4_feature = BIT_0 | BIT_1;
6844 * qlt_init_atio_q_entries() - Initializes ATIO queue entries.
6847 * Beginning of ATIO ring has initialization control block already built
6848 * by nvram config routine.
6850 * Returns 0 on success.
6853 qlt_init_atio_q_entries(struct scsi_qla_host *vha)
6855 struct qla_hw_data *ha = vha->hw;
6857 struct atio_from_isp *pkt = (struct atio_from_isp *)ha->tgt.atio_ring;
6859 if (qla_ini_mode_enabled(vha))
6862 for (cnt = 0; cnt < ha->tgt.atio_q_length; cnt++) {
6863 pkt->u.raw.signature = ATIO_PROCESSED;
6870 * qlt_24xx_process_atio_queue() - Process ATIO queue entries.
6871 * @ha: SCSI driver HA context
6874 qlt_24xx_process_atio_queue(struct scsi_qla_host *vha, uint8_t ha_locked)
6876 struct qla_hw_data *ha = vha->hw;
6877 struct atio_from_isp *pkt;
6880 if (!ha->flags.fw_started)
6883 while ((ha->tgt.atio_ring_ptr->signature != ATIO_PROCESSED) ||
6884 fcpcmd_is_corrupted(ha->tgt.atio_ring_ptr)) {
6885 pkt = (struct atio_from_isp *)ha->tgt.atio_ring_ptr;
6886 cnt = pkt->u.raw.entry_count;
6888 if (unlikely(fcpcmd_is_corrupted(ha->tgt.atio_ring_ptr))) {
6890 * This packet is corrupted. The header + payload
6891 * can not be trusted. There is no point in passing
6894 ql_log(ql_log_warn, vha, 0xd03c,
6895 "corrupted fcp frame SID[%3phN] OXID[%04x] EXCG[%x] %64phN\n",
6896 pkt->u.isp24.fcp_hdr.s_id,
6897 be16_to_cpu(pkt->u.isp24.fcp_hdr.ox_id),
6898 le32_to_cpu(pkt->u.isp24.exchange_addr), pkt);
6900 adjust_corrupted_atio(pkt);
6901 qlt_send_term_exchange(ha->base_qpair, NULL, pkt,
6904 qlt_24xx_atio_pkt_all_vps(vha,
6905 (struct atio_from_isp *)pkt, ha_locked);
6908 for (i = 0; i < cnt; i++) {
6909 ha->tgt.atio_ring_index++;
6910 if (ha->tgt.atio_ring_index == ha->tgt.atio_q_length) {
6911 ha->tgt.atio_ring_index = 0;
6912 ha->tgt.atio_ring_ptr = ha->tgt.atio_ring;
6914 ha->tgt.atio_ring_ptr++;
6916 pkt->u.raw.signature = ATIO_PROCESSED;
6917 pkt = (struct atio_from_isp *)ha->tgt.atio_ring_ptr;
6922 /* Adjust ring index */
6923 WRT_REG_DWORD(ISP_ATIO_Q_OUT(vha), ha->tgt.atio_ring_index);
6927 qlt_24xx_config_rings(struct scsi_qla_host *vha)
6929 struct qla_hw_data *ha = vha->hw;
6930 struct qla_msix_entry *msix = &ha->msix_entries[2];
6931 struct init_cb_24xx *icb = (struct init_cb_24xx *)ha->init_cb;
6933 if (!QLA_TGT_MODE_ENABLED())
6936 WRT_REG_DWORD(ISP_ATIO_Q_IN(vha), 0);
6937 WRT_REG_DWORD(ISP_ATIO_Q_OUT(vha), 0);
6938 RD_REG_DWORD(ISP_ATIO_Q_OUT(vha));
6940 if (ha->flags.msix_enabled) {
6941 if (IS_QLA83XX(ha) || IS_QLA27XX(ha)) {
6942 if (IS_QLA2071(ha)) {
6943 /* 4 ports Baker: Enable Interrupt Handshake */
6945 icb->firmware_options_2 |= BIT_26;
6947 icb->msix_atio = cpu_to_le16(msix->entry);
6948 icb->firmware_options_2 &= ~BIT_26;
6950 ql_dbg(ql_dbg_init, vha, 0xf072,
6951 "Registering ICB vector 0x%x for atio que.\n",
6956 if (IS_QLA83XX(ha) || IS_QLA27XX(ha)) {
6958 icb->firmware_options_2 |= BIT_26;
6959 ql_dbg(ql_dbg_init, vha, 0xf072,
6960 "%s: Use INTx for ATIOQ.\n", __func__);
6966 qlt_24xx_config_nvram_stage1(struct scsi_qla_host *vha, struct nvram_24xx *nv)
6968 struct qla_hw_data *ha = vha->hw;
6971 if (!QLA_TGT_MODE_ENABLED())
6974 if (qla_tgt_mode_enabled(vha) || qla_dual_mode_enabled(vha)) {
6975 if (!ha->tgt.saved_set) {
6976 /* We save only once */
6977 ha->tgt.saved_exchange_count = nv->exchange_count;
6978 ha->tgt.saved_firmware_options_1 =
6979 nv->firmware_options_1;
6980 ha->tgt.saved_firmware_options_2 =
6981 nv->firmware_options_2;
6982 ha->tgt.saved_firmware_options_3 =
6983 nv->firmware_options_3;
6984 ha->tgt.saved_set = 1;
6987 if (qla_tgt_mode_enabled(vha))
6988 nv->exchange_count = cpu_to_le16(0xFFFF);
6990 nv->exchange_count = cpu_to_le16(vha->ql2xexchoffld);
6992 /* Enable target mode */
6993 nv->firmware_options_1 |= cpu_to_le32(BIT_4);
6995 /* Disable ini mode, if requested */
6996 if (qla_tgt_mode_enabled(vha))
6997 nv->firmware_options_1 |= cpu_to_le32(BIT_5);
6999 /* Disable Full Login after LIP */
7000 nv->firmware_options_1 &= cpu_to_le32(~BIT_13);
7001 /* Enable initial LIP */
7002 nv->firmware_options_1 &= cpu_to_le32(~BIT_9);
7003 if (ql2xtgt_tape_enable)
7004 /* Enable FC Tape support */
7005 nv->firmware_options_2 |= cpu_to_le32(BIT_12);
7007 /* Disable FC Tape support */
7008 nv->firmware_options_2 &= cpu_to_le32(~BIT_12);
7010 /* Disable Full Login after LIP */
7011 nv->host_p &= cpu_to_le32(~BIT_10);
7014 * clear BIT 15 explicitly as we have seen at least
7015 * a couple of instances where this was set and this
7016 * was causing the firmware to not be initialized.
7018 nv->firmware_options_1 &= cpu_to_le32(~BIT_15);
7019 /* Enable target PRLI control */
7020 nv->firmware_options_2 |= cpu_to_le32(BIT_14);
7022 if (IS_QLA25XX(ha)) {
7023 /* Change Loop-prefer to Pt-Pt */
7024 tmp = ~(BIT_4|BIT_5|BIT_6);
7025 nv->firmware_options_2 &= cpu_to_le32(tmp);
7027 nv->firmware_options_2 |= cpu_to_le32(tmp);
7030 if (ha->tgt.saved_set) {
7031 nv->exchange_count = ha->tgt.saved_exchange_count;
7032 nv->firmware_options_1 =
7033 ha->tgt.saved_firmware_options_1;
7034 nv->firmware_options_2 =
7035 ha->tgt.saved_firmware_options_2;
7036 nv->firmware_options_3 =
7037 ha->tgt.saved_firmware_options_3;
7042 if (ha->base_qpair->enable_class_2) {
7043 if (vha->flags.init_done)
7044 fc_host_supported_classes(vha->host) =
7045 FC_COS_CLASS2 | FC_COS_CLASS3;
7047 nv->firmware_options_2 |= cpu_to_le32(BIT_8);
7049 if (vha->flags.init_done)
7050 fc_host_supported_classes(vha->host) = FC_COS_CLASS3;
7052 nv->firmware_options_2 &= ~cpu_to_le32(BIT_8);
7057 qlt_24xx_config_nvram_stage2(struct scsi_qla_host *vha,
7058 struct init_cb_24xx *icb)
7060 struct qla_hw_data *ha = vha->hw;
7062 if (!QLA_TGT_MODE_ENABLED())
7065 if (ha->tgt.node_name_set) {
7066 memcpy(icb->node_name, ha->tgt.tgt_node_name, WWN_SIZE);
7067 icb->firmware_options_1 |= cpu_to_le32(BIT_14);
7072 qlt_81xx_config_nvram_stage1(struct scsi_qla_host *vha, struct nvram_81xx *nv)
7074 struct qla_hw_data *ha = vha->hw;
7077 if (!QLA_TGT_MODE_ENABLED())
7080 if (qla_tgt_mode_enabled(vha) || qla_dual_mode_enabled(vha)) {
7081 if (!ha->tgt.saved_set) {
7082 /* We save only once */
7083 ha->tgt.saved_exchange_count = nv->exchange_count;
7084 ha->tgt.saved_firmware_options_1 =
7085 nv->firmware_options_1;
7086 ha->tgt.saved_firmware_options_2 =
7087 nv->firmware_options_2;
7088 ha->tgt.saved_firmware_options_3 =
7089 nv->firmware_options_3;
7090 ha->tgt.saved_set = 1;
7093 if (qla_tgt_mode_enabled(vha))
7094 nv->exchange_count = cpu_to_le16(0xFFFF);
7096 nv->exchange_count = cpu_to_le16(vha->ql2xexchoffld);
7098 /* Enable target mode */
7099 nv->firmware_options_1 |= cpu_to_le32(BIT_4);
7101 /* Disable ini mode, if requested */
7102 if (qla_tgt_mode_enabled(vha))
7103 nv->firmware_options_1 |= cpu_to_le32(BIT_5);
7104 /* Disable Full Login after LIP */
7105 nv->firmware_options_1 &= cpu_to_le32(~BIT_13);
7106 /* Enable initial LIP */
7107 nv->firmware_options_1 &= cpu_to_le32(~BIT_9);
7109 * clear BIT 15 explicitly as we have seen at
7110 * least a couple of instances where this was set
7111 * and this was causing the firmware to not be
7114 nv->firmware_options_1 &= cpu_to_le32(~BIT_15);
7115 if (ql2xtgt_tape_enable)
7116 /* Enable FC tape support */
7117 nv->firmware_options_2 |= cpu_to_le32(BIT_12);
7119 /* Disable FC tape support */
7120 nv->firmware_options_2 &= cpu_to_le32(~BIT_12);
7122 /* Disable Full Login after LIP */
7123 nv->host_p &= cpu_to_le32(~BIT_10);
7124 /* Enable target PRLI control */
7125 nv->firmware_options_2 |= cpu_to_le32(BIT_14);
7127 /* Change Loop-prefer to Pt-Pt */
7128 tmp = ~(BIT_4|BIT_5|BIT_6);
7129 nv->firmware_options_2 &= cpu_to_le32(tmp);
7131 nv->firmware_options_2 |= cpu_to_le32(tmp);
7133 if (ha->tgt.saved_set) {
7134 nv->exchange_count = ha->tgt.saved_exchange_count;
7135 nv->firmware_options_1 =
7136 ha->tgt.saved_firmware_options_1;
7137 nv->firmware_options_2 =
7138 ha->tgt.saved_firmware_options_2;
7139 nv->firmware_options_3 =
7140 ha->tgt.saved_firmware_options_3;
7145 if (ha->base_qpair->enable_class_2) {
7146 if (vha->flags.init_done)
7147 fc_host_supported_classes(vha->host) =
7148 FC_COS_CLASS2 | FC_COS_CLASS3;
7150 nv->firmware_options_2 |= cpu_to_le32(BIT_8);
7152 if (vha->flags.init_done)
7153 fc_host_supported_classes(vha->host) = FC_COS_CLASS3;
7155 nv->firmware_options_2 &= ~cpu_to_le32(BIT_8);
7160 qlt_81xx_config_nvram_stage2(struct scsi_qla_host *vha,
7161 struct init_cb_81xx *icb)
7163 struct qla_hw_data *ha = vha->hw;
7165 if (!QLA_TGT_MODE_ENABLED())
7168 if (ha->tgt.node_name_set) {
7169 memcpy(icb->node_name, ha->tgt.tgt_node_name, WWN_SIZE);
7170 icb->firmware_options_1 |= cpu_to_le32(BIT_14);
7175 qlt_83xx_iospace_config(struct qla_hw_data *ha)
7177 if (!QLA_TGT_MODE_ENABLED())
7180 ha->msix_count += 1; /* For ATIO Q */
7185 qlt_modify_vp_config(struct scsi_qla_host *vha,
7186 struct vp_config_entry_24xx *vpmod)
7188 /* enable target mode. Bit5 = 1 => disable */
7189 if (qla_tgt_mode_enabled(vha) || qla_dual_mode_enabled(vha))
7190 vpmod->options_idx1 &= ~BIT_5;
7192 /* Disable ini mode, if requested. bit4 = 1 => disable */
7193 if (qla_tgt_mode_enabled(vha))
7194 vpmod->options_idx1 &= ~BIT_4;
7198 qlt_probe_one_stage1(struct scsi_qla_host *base_vha, struct qla_hw_data *ha)
7202 if (!QLA_TGT_MODE_ENABLED())
7205 if ((ql2xenablemsix == 0) || IS_QLA83XX(ha) || IS_QLA27XX(ha)) {
7206 ISP_ATIO_Q_IN(base_vha) = &ha->mqiobase->isp25mq.atio_q_in;
7207 ISP_ATIO_Q_OUT(base_vha) = &ha->mqiobase->isp25mq.atio_q_out;
7209 ISP_ATIO_Q_IN(base_vha) = &ha->iobase->isp24.atio_q_in;
7210 ISP_ATIO_Q_OUT(base_vha) = &ha->iobase->isp24.atio_q_out;
7213 mutex_init(&base_vha->vha_tgt.tgt_mutex);
7214 mutex_init(&base_vha->vha_tgt.tgt_host_action_mutex);
7216 INIT_LIST_HEAD(&base_vha->unknown_atio_list);
7217 INIT_DELAYED_WORK(&base_vha->unknown_atio_work,
7218 qlt_unknown_atio_work_fn);
7220 qlt_clear_mode(base_vha);
7222 rc = btree_init32(&ha->tgt.host_map);
7224 ql_log(ql_log_info, base_vha, 0xd03d,
7225 "Unable to initialize ha->host_map btree\n");
7227 qlt_update_vp_map(base_vha, SET_VP_IDX);
7231 qla83xx_msix_atio_q(int irq, void *dev_id)
7233 struct rsp_que *rsp;
7234 scsi_qla_host_t *vha;
7235 struct qla_hw_data *ha;
7236 unsigned long flags;
7238 rsp = (struct rsp_que *) dev_id;
7240 vha = pci_get_drvdata(ha->pdev);
7242 spin_lock_irqsave(&ha->tgt.atio_lock, flags);
7244 qlt_24xx_process_atio_queue(vha, 0);
7246 spin_unlock_irqrestore(&ha->tgt.atio_lock, flags);
7252 qlt_handle_abts_recv_work(struct work_struct *work)
7254 struct qla_tgt_sess_op *op = container_of(work,
7255 struct qla_tgt_sess_op, work);
7256 scsi_qla_host_t *vha = op->vha;
7257 struct qla_hw_data *ha = vha->hw;
7258 unsigned long flags;
7260 if (qla2x00_reset_active(vha) ||
7261 (op->chip_reset != ha->base_qpair->chip_reset))
7264 spin_lock_irqsave(&ha->tgt.atio_lock, flags);
7265 qlt_24xx_process_atio_queue(vha, 0);
7266 spin_unlock_irqrestore(&ha->tgt.atio_lock, flags);
7268 spin_lock_irqsave(&ha->hardware_lock, flags);
7269 qlt_response_pkt_all_vps(vha, op->rsp, (response_t *)&op->atio);
7270 spin_unlock_irqrestore(&ha->hardware_lock, flags);
7276 qlt_handle_abts_recv(struct scsi_qla_host *vha, struct rsp_que *rsp,
7279 struct qla_tgt_sess_op *op;
7281 op = kzalloc(sizeof(*op), GFP_ATOMIC);
7284 /* do not reach for ATIO queue here. This is best effort err
7285 * recovery at this point.
7287 qlt_response_pkt_all_vps(vha, rsp, pkt);
7291 memcpy(&op->atio, pkt, sizeof(*pkt));
7293 op->chip_reset = vha->hw->base_qpair->chip_reset;
7295 INIT_WORK(&op->work, qlt_handle_abts_recv_work);
7296 queue_work(qla_tgt_wq, &op->work);
7301 qlt_mem_alloc(struct qla_hw_data *ha)
7303 if (!QLA_TGT_MODE_ENABLED())
7306 ha->tgt.tgt_vp_map = kcalloc(MAX_MULTI_ID_FABRIC,
7307 sizeof(struct qla_tgt_vp_map),
7309 if (!ha->tgt.tgt_vp_map)
7312 ha->tgt.atio_ring = dma_alloc_coherent(&ha->pdev->dev,
7313 (ha->tgt.atio_q_length + 1) * sizeof(struct atio_from_isp),
7314 &ha->tgt.atio_dma, GFP_KERNEL);
7315 if (!ha->tgt.atio_ring) {
7316 kfree(ha->tgt.tgt_vp_map);
7323 qlt_mem_free(struct qla_hw_data *ha)
7325 if (!QLA_TGT_MODE_ENABLED())
7328 if (ha->tgt.atio_ring) {
7329 dma_free_coherent(&ha->pdev->dev, (ha->tgt.atio_q_length + 1) *
7330 sizeof(struct atio_from_isp), ha->tgt.atio_ring,
7333 kfree(ha->tgt.tgt_vp_map);
7336 /* vport_slock to be held by the caller */
7338 qlt_update_vp_map(struct scsi_qla_host *vha, int cmd)
7344 if (!QLA_TGT_MODE_ENABLED())
7347 key = vha->d_id.b24;
7351 vha->hw->tgt.tgt_vp_map[vha->vp_idx].vha = vha;
7354 slot = btree_lookup32(&vha->hw->tgt.host_map, key);
7356 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf018,
7357 "Save vha in host_map %p %06x\n", vha, key);
7358 rc = btree_insert32(&vha->hw->tgt.host_map,
7359 key, vha, GFP_ATOMIC);
7361 ql_log(ql_log_info, vha, 0xd03e,
7362 "Unable to insert s_id into host_map: %06x\n",
7366 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf019,
7367 "replace existing vha in host_map %p %06x\n", vha, key);
7368 btree_update32(&vha->hw->tgt.host_map, key, vha);
7371 vha->hw->tgt.tgt_vp_map[vha->vp_idx].vha = NULL;
7374 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01a,
7375 "clear vha in host_map %p %06x\n", vha, key);
7376 slot = btree_lookup32(&vha->hw->tgt.host_map, key);
7378 btree_remove32(&vha->hw->tgt.host_map, key);
7384 void qlt_update_host_map(struct scsi_qla_host *vha, port_id_t id)
7387 if (!vha->d_id.b24) {
7389 qlt_update_vp_map(vha, SET_AL_PA);
7390 } else if (vha->d_id.b24 != id.b24) {
7391 qlt_update_vp_map(vha, RESET_AL_PA);
7393 qlt_update_vp_map(vha, SET_AL_PA);
7397 static int __init qlt_parse_ini_mode(void)
7399 if (strcasecmp(qlini_mode, QLA2XXX_INI_MODE_STR_EXCLUSIVE) == 0)
7400 ql2x_ini_mode = QLA2XXX_INI_MODE_EXCLUSIVE;
7401 else if (strcasecmp(qlini_mode, QLA2XXX_INI_MODE_STR_DISABLED) == 0)
7402 ql2x_ini_mode = QLA2XXX_INI_MODE_DISABLED;
7403 else if (strcasecmp(qlini_mode, QLA2XXX_INI_MODE_STR_ENABLED) == 0)
7404 ql2x_ini_mode = QLA2XXX_INI_MODE_ENABLED;
7405 else if (strcasecmp(qlini_mode, QLA2XXX_INI_MODE_STR_DUAL) == 0)
7406 ql2x_ini_mode = QLA2XXX_INI_MODE_DUAL;
7413 int __init qlt_init(void)
7417 if (!qlt_parse_ini_mode()) {
7418 ql_log(ql_log_fatal, NULL, 0xe06b,
7419 "qlt_parse_ini_mode() failed\n");
7423 if (!QLA_TGT_MODE_ENABLED())
7426 qla_tgt_mgmt_cmd_cachep = kmem_cache_create("qla_tgt_mgmt_cmd_cachep",
7427 sizeof(struct qla_tgt_mgmt_cmd), __alignof__(struct
7428 qla_tgt_mgmt_cmd), 0, NULL);
7429 if (!qla_tgt_mgmt_cmd_cachep) {
7430 ql_log(ql_log_fatal, NULL, 0xd04b,
7431 "kmem_cache_create for qla_tgt_mgmt_cmd_cachep failed\n");
7435 qla_tgt_plogi_cachep = kmem_cache_create("qla_tgt_plogi_cachep",
7436 sizeof(struct qlt_plogi_ack_t), __alignof__(struct qlt_plogi_ack_t),
7439 if (!qla_tgt_plogi_cachep) {
7440 ql_log(ql_log_fatal, NULL, 0xe06d,
7441 "kmem_cache_create for qla_tgt_plogi_cachep failed\n");
7443 goto out_mgmt_cmd_cachep;
7446 qla_tgt_mgmt_cmd_mempool = mempool_create(25, mempool_alloc_slab,
7447 mempool_free_slab, qla_tgt_mgmt_cmd_cachep);
7448 if (!qla_tgt_mgmt_cmd_mempool) {
7449 ql_log(ql_log_fatal, NULL, 0xe06e,
7450 "mempool_create for qla_tgt_mgmt_cmd_mempool failed\n");
7452 goto out_plogi_cachep;
7455 qla_tgt_wq = alloc_workqueue("qla_tgt_wq", 0, 0);
7457 ql_log(ql_log_fatal, NULL, 0xe06f,
7458 "alloc_workqueue for qla_tgt_wq failed\n");
7460 goto out_cmd_mempool;
7463 * Return 1 to signal that initiator-mode is being disabled
7465 return (ql2x_ini_mode == QLA2XXX_INI_MODE_DISABLED) ? 1 : 0;
7468 mempool_destroy(qla_tgt_mgmt_cmd_mempool);
7470 kmem_cache_destroy(qla_tgt_plogi_cachep);
7471 out_mgmt_cmd_cachep:
7472 kmem_cache_destroy(qla_tgt_mgmt_cmd_cachep);
7478 if (!QLA_TGT_MODE_ENABLED())
7481 destroy_workqueue(qla_tgt_wq);
7482 mempool_destroy(qla_tgt_mgmt_cmd_mempool);
7483 kmem_cache_destroy(qla_tgt_plogi_cachep);
7484 kmem_cache_destroy(qla_tgt_mgmt_cmd_cachep);