1 // SPDX-License-Identifier: GPL-2.0-only
3 * qla_target.c SCSI LLD infrastructure for QLogic 22xx/23xx/24xx/25xx
5 * based on qla2x00t.c code:
7 * Copyright (C) 2004 - 2010 Vladislav Bolkhovitin <vst@vlnb.net>
8 * Copyright (C) 2004 - 2005 Leonid Stoljar
9 * Copyright (C) 2006 Nathaniel Clark <nate@misrule.us>
10 * Copyright (C) 2006 - 2010 ID7 Ltd.
12 * Forward port and refactoring to modern qla2xxx and target/configfs
14 * Copyright (C) 2010-2013 Nicholas A. Bellinger <nab@kernel.org>
17 #include <linux/module.h>
18 #include <linux/init.h>
19 #include <linux/types.h>
20 #include <linux/blkdev.h>
21 #include <linux/interrupt.h>
22 #include <linux/pci.h>
23 #include <linux/delay.h>
24 #include <linux/list.h>
25 #include <linux/workqueue.h>
26 #include <asm/unaligned.h>
27 #include <scsi/scsi.h>
28 #include <scsi/scsi_host.h>
29 #include <scsi/scsi_tcq.h>
32 #include "qla_target.h"
34 static int ql2xtgt_tape_enable;
35 module_param(ql2xtgt_tape_enable, int, S_IRUGO|S_IWUSR);
36 MODULE_PARM_DESC(ql2xtgt_tape_enable,
37 "Enables Sequence level error recovery (aka FC Tape). Default is 0 - no SLER. 1 - Enable SLER.");
39 static char *qlini_mode = QLA2XXX_INI_MODE_STR_ENABLED;
40 module_param(qlini_mode, charp, S_IRUGO);
41 MODULE_PARM_DESC(qlini_mode,
42 "Determines when initiator mode will be enabled. Possible values: "
43 "\"exclusive\" - initiator mode will be enabled on load, "
44 "disabled on enabling target mode and then on disabling target mode "
46 "\"disabled\" - initiator mode will never be enabled; "
47 "\"dual\" - Initiator Modes will be enabled. Target Mode can be activated "
49 "\"enabled\" (default) - initiator mode will always stay enabled.");
51 static int ql_dm_tgt_ex_pct = 0;
52 module_param(ql_dm_tgt_ex_pct, int, S_IRUGO|S_IWUSR);
53 MODULE_PARM_DESC(ql_dm_tgt_ex_pct,
54 "For Dual Mode (qlini_mode=dual), this parameter determines "
55 "the percentage of exchanges/cmds FW will allocate resources "
59 module_param(ql2xuctrlirq, int, 0644);
60 MODULE_PARM_DESC(ql2xuctrlirq,
61 "User to control IRQ placement via smp_affinity."
62 "Valid with qlini_mode=disabled."
63 "1(default): enable");
65 int ql2x_ini_mode = QLA2XXX_INI_MODE_EXCLUSIVE;
67 static int qla_sam_status = SAM_STAT_BUSY;
68 static int tc_sam_status = SAM_STAT_TASK_SET_FULL; /* target core */
71 * From scsi/fc/fc_fcp.h
73 enum fcp_resp_rsp_codes {
75 FCP_DATA_LEN_INVALID = 1,
76 FCP_CMND_FIELDS_INVALID = 2,
77 FCP_DATA_PARAM_MISMATCH = 3,
80 FCP_TMF_INVALID_LUN = 9,
84 * fc_pri_ta from scsi/fc/fc_fcp.h
86 #define FCP_PTA_SIMPLE 0 /* simple task attribute */
87 #define FCP_PTA_HEADQ 1 /* head of queue task attribute */
88 #define FCP_PTA_ORDERED 2 /* ordered task attribute */
89 #define FCP_PTA_ACA 4 /* auto. contingent allegiance */
90 #define FCP_PTA_MASK 7 /* mask for task attribute field */
91 #define FCP_PRI_SHIFT 3 /* priority field starts in bit 3 */
92 #define FCP_PRI_RESVD_MASK 0x80 /* reserved bits in priority field */
95 * This driver calls qla2x00_alloc_iocbs() and qla2x00_issue_marker(), which
96 * must be called under HW lock and could unlock/lock it inside.
97 * It isn't an issue, since in the current implementation on the time when
98 * those functions are called:
100 * - Either context is IRQ and only IRQ handler can modify HW data,
101 * including rings related fields,
103 * - Or access to target mode variables from struct qla_tgt doesn't
104 * cross those functions boundaries, except tgt_stop, which
105 * additionally protected by irq_cmd_count.
107 /* Predefs for callbacks handed to qla2xxx LLD */
108 static void qlt_24xx_atio_pkt(struct scsi_qla_host *ha,
109 struct atio_from_isp *pkt, uint8_t);
110 static void qlt_response_pkt(struct scsi_qla_host *ha, struct rsp_que *rsp,
112 static int qlt_issue_task_mgmt(struct fc_port *sess, u64 lun,
113 int fn, void *iocb, int flags);
114 static void qlt_send_term_exchange(struct qla_qpair *, struct qla_tgt_cmd
115 *cmd, struct atio_from_isp *atio, int ha_locked, int ul_abort);
116 static void qlt_alloc_qfull_cmd(struct scsi_qla_host *vha,
117 struct atio_from_isp *atio, uint16_t status, int qfull);
118 static void qlt_disable_vha(struct scsi_qla_host *vha);
119 static void qlt_clear_tgt_db(struct qla_tgt *tgt);
120 static void qlt_send_notify_ack(struct qla_qpair *qpair,
121 struct imm_ntfy_from_isp *ntfy,
122 uint32_t add_flags, uint16_t resp_code, int resp_code_valid,
123 uint16_t srr_flags, uint16_t srr_reject_code, uint8_t srr_explan);
124 static void qlt_send_term_imm_notif(struct scsi_qla_host *vha,
125 struct imm_ntfy_from_isp *imm, int ha_locked);
126 static struct fc_port *qlt_create_sess(struct scsi_qla_host *vha,
127 fc_port_t *fcport, bool local);
128 void qlt_unreg_sess(struct fc_port *sess);
129 static void qlt_24xx_handle_abts(struct scsi_qla_host *,
130 struct abts_recv_from_24xx *);
131 static void qlt_send_busy(struct qla_qpair *, struct atio_from_isp *,
133 static int qlt_check_reserve_free_req(struct qla_qpair *qpair, uint32_t);
134 static inline uint32_t qlt_make_handle(struct qla_qpair *);
139 static struct kmem_cache *qla_tgt_mgmt_cmd_cachep;
140 struct kmem_cache *qla_tgt_plogi_cachep;
141 static mempool_t *qla_tgt_mgmt_cmd_mempool;
142 static struct workqueue_struct *qla_tgt_wq;
143 static DEFINE_MUTEX(qla_tgt_mutex);
144 static LIST_HEAD(qla_tgt_glist);
146 static const char *prot_op_str(u32 prot_op)
149 case TARGET_PROT_NORMAL: return "NORMAL";
150 case TARGET_PROT_DIN_INSERT: return "DIN_INSERT";
151 case TARGET_PROT_DOUT_INSERT: return "DOUT_INSERT";
152 case TARGET_PROT_DIN_STRIP: return "DIN_STRIP";
153 case TARGET_PROT_DOUT_STRIP: return "DOUT_STRIP";
154 case TARGET_PROT_DIN_PASS: return "DIN_PASS";
155 case TARGET_PROT_DOUT_PASS: return "DOUT_PASS";
156 default: return "UNKNOWN";
160 /* This API intentionally takes dest as a parameter, rather than returning
161 * int value to avoid caller forgetting to issue wmb() after the store */
162 void qlt_do_generation_tick(struct scsi_qla_host *vha, int *dest)
164 scsi_qla_host_t *base_vha = pci_get_drvdata(vha->hw->pdev);
165 *dest = atomic_inc_return(&base_vha->generation_tick);
170 /* Might release hw lock, then reaquire!! */
171 static inline int qlt_issue_marker(struct scsi_qla_host *vha, int vha_locked)
173 /* Send marker if required */
174 if (unlikely(vha->marker_needed != 0)) {
175 int rc = qla2x00_issue_marker(vha, vha_locked);
177 if (rc != QLA_SUCCESS) {
178 ql_dbg(ql_dbg_tgt, vha, 0xe03d,
179 "qla_target(%d): issue_marker() failed\n",
187 struct scsi_qla_host *qla_find_host_by_d_id(struct scsi_qla_host *vha,
190 struct scsi_qla_host *host;
193 if (vha->d_id.b.area == d_id.area &&
194 vha->d_id.b.domain == d_id.domain &&
195 vha->d_id.b.al_pa == d_id.al_pa)
198 key = be_to_port_id(d_id).b24;
200 host = btree_lookup32(&vha->hw->host_map, key);
202 ql_dbg(ql_dbg_tgt_mgt + ql_dbg_verbose, vha, 0xf005,
203 "Unable to find host %06x\n", key);
209 struct scsi_qla_host *qlt_find_host_by_vp_idx(struct scsi_qla_host *vha,
212 struct qla_hw_data *ha = vha->hw;
214 if (vha->vp_idx == vp_idx)
217 BUG_ON(ha->tgt.tgt_vp_map == NULL);
218 if (likely(test_bit(vp_idx, ha->vp_idx_map)))
219 return ha->tgt.tgt_vp_map[vp_idx].vha;
224 static inline void qlt_incr_num_pend_cmds(struct scsi_qla_host *vha)
228 spin_lock_irqsave(&vha->hw->tgt.q_full_lock, flags);
230 vha->hw->tgt.num_pend_cmds++;
231 if (vha->hw->tgt.num_pend_cmds > vha->qla_stats.stat_max_pend_cmds)
232 vha->qla_stats.stat_max_pend_cmds =
233 vha->hw->tgt.num_pend_cmds;
234 spin_unlock_irqrestore(&vha->hw->tgt.q_full_lock, flags);
236 static inline void qlt_decr_num_pend_cmds(struct scsi_qla_host *vha)
240 spin_lock_irqsave(&vha->hw->tgt.q_full_lock, flags);
241 vha->hw->tgt.num_pend_cmds--;
242 spin_unlock_irqrestore(&vha->hw->tgt.q_full_lock, flags);
246 static void qlt_queue_unknown_atio(scsi_qla_host_t *vha,
247 struct atio_from_isp *atio, uint8_t ha_locked)
249 struct qla_tgt_sess_op *u;
250 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
254 ql_dbg(ql_dbg_async, vha, 0x502c,
255 "qla_target(%d): dropping unknown ATIO_TYPE7, because tgt is being stopped",
260 u = kzalloc(sizeof(*u), GFP_ATOMIC);
265 memcpy(&u->atio, atio, sizeof(*atio));
266 INIT_LIST_HEAD(&u->cmd_list);
268 spin_lock_irqsave(&vha->cmd_list_lock, flags);
269 list_add_tail(&u->cmd_list, &vha->unknown_atio_list);
270 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
272 schedule_delayed_work(&vha->unknown_atio_work, 1);
278 qlt_send_term_exchange(vha->hw->base_qpair, NULL, atio, ha_locked, 0);
282 static void qlt_try_to_dequeue_unknown_atios(struct scsi_qla_host *vha,
285 struct qla_tgt_sess_op *u, *t;
286 scsi_qla_host_t *host;
287 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
291 list_for_each_entry_safe(u, t, &vha->unknown_atio_list, cmd_list) {
293 ql_dbg(ql_dbg_async, vha, 0x502e,
294 "Freeing unknown %s %p, because of Abort\n",
296 qlt_send_term_exchange(vha->hw->base_qpair, NULL,
297 &u->atio, ha_locked, 0);
301 host = qla_find_host_by_d_id(vha, u->atio.u.isp24.fcp_hdr.d_id);
303 ql_dbg(ql_dbg_async + ql_dbg_verbose, vha, 0x502f,
304 "Requeuing unknown ATIO_TYPE7 %p\n", u);
305 qlt_24xx_atio_pkt(host, &u->atio, ha_locked);
306 } else if (tgt->tgt_stop) {
307 ql_dbg(ql_dbg_async + ql_dbg_verbose, vha, 0x503a,
308 "Freeing unknown %s %p, because tgt is being stopped\n",
310 qlt_send_term_exchange(vha->hw->base_qpair, NULL,
311 &u->atio, ha_locked, 0);
313 ql_dbg(ql_dbg_async + ql_dbg_verbose, vha, 0x503d,
314 "Reschedule u %p, vha %p, host %p\n", u, vha, host);
317 schedule_delayed_work(&vha->unknown_atio_work,
324 spin_lock_irqsave(&vha->cmd_list_lock, flags);
325 list_del(&u->cmd_list);
326 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
331 void qlt_unknown_atio_work_fn(struct work_struct *work)
333 struct scsi_qla_host *vha = container_of(to_delayed_work(work),
334 struct scsi_qla_host, unknown_atio_work);
336 qlt_try_to_dequeue_unknown_atios(vha, 0);
339 static bool qlt_24xx_atio_pkt_all_vps(struct scsi_qla_host *vha,
340 struct atio_from_isp *atio, uint8_t ha_locked)
342 ql_dbg(ql_dbg_tgt, vha, 0xe072,
343 "%s: qla_target(%d): type %x ox_id %04x\n",
344 __func__, vha->vp_idx, atio->u.raw.entry_type,
345 be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id));
347 switch (atio->u.raw.entry_type) {
350 struct scsi_qla_host *host = qla_find_host_by_d_id(vha,
351 atio->u.isp24.fcp_hdr.d_id);
352 if (unlikely(NULL == host)) {
353 ql_dbg(ql_dbg_tgt, vha, 0xe03e,
354 "qla_target(%d): Received ATIO_TYPE7 "
355 "with unknown d_id %x:%x:%x\n", vha->vp_idx,
356 atio->u.isp24.fcp_hdr.d_id.domain,
357 atio->u.isp24.fcp_hdr.d_id.area,
358 atio->u.isp24.fcp_hdr.d_id.al_pa);
361 qlt_queue_unknown_atio(vha, atio, ha_locked);
364 if (unlikely(!list_empty(&vha->unknown_atio_list)))
365 qlt_try_to_dequeue_unknown_atios(vha, ha_locked);
367 qlt_24xx_atio_pkt(host, atio, ha_locked);
371 case IMMED_NOTIFY_TYPE:
373 struct scsi_qla_host *host = vha;
374 struct imm_ntfy_from_isp *entry =
375 (struct imm_ntfy_from_isp *)atio;
377 qlt_issue_marker(vha, ha_locked);
379 if ((entry->u.isp24.vp_index != 0xFF) &&
380 (entry->u.isp24.nport_handle != cpu_to_le16(0xFFFF))) {
381 host = qlt_find_host_by_vp_idx(vha,
382 entry->u.isp24.vp_index);
383 if (unlikely(!host)) {
384 ql_dbg(ql_dbg_tgt, vha, 0xe03f,
385 "qla_target(%d): Received "
386 "ATIO (IMMED_NOTIFY_TYPE) "
387 "with unknown vp_index %d\n",
388 vha->vp_idx, entry->u.isp24.vp_index);
392 qlt_24xx_atio_pkt(host, atio, ha_locked);
396 case VP_RPT_ID_IOCB_TYPE:
397 qla24xx_report_id_acquisition(vha,
398 (struct vp_rpt_id_entry_24xx *)atio);
403 struct abts_recv_from_24xx *entry =
404 (struct abts_recv_from_24xx *)atio;
405 struct scsi_qla_host *host = qlt_find_host_by_vp_idx(vha,
409 if (unlikely(!host)) {
410 ql_dbg(ql_dbg_tgt, vha, 0xe00a,
411 "qla_target(%d): Response pkt (ABTS_RECV_24XX) "
412 "received, with unknown vp_index %d\n",
413 vha->vp_idx, entry->vp_index);
417 spin_lock_irqsave(&host->hw->hardware_lock, flags);
418 qlt_24xx_handle_abts(host, (struct abts_recv_from_24xx *)atio);
420 spin_unlock_irqrestore(&host->hw->hardware_lock, flags);
424 /* case PUREX_IOCB_TYPE: ql2xmvasynctoatio */
427 ql_dbg(ql_dbg_tgt, vha, 0xe040,
428 "qla_target(%d): Received unknown ATIO atio "
429 "type %x\n", vha->vp_idx, atio->u.raw.entry_type);
436 void qlt_response_pkt_all_vps(struct scsi_qla_host *vha,
437 struct rsp_que *rsp, response_t *pkt)
439 switch (pkt->entry_type) {
441 ql_dbg(ql_dbg_tgt, vha, 0xe073,
442 "qla_target(%d):%s: CRC2 Response pkt\n",
443 vha->vp_idx, __func__);
447 struct ctio7_from_24xx *entry = (struct ctio7_from_24xx *)pkt;
448 struct scsi_qla_host *host = qlt_find_host_by_vp_idx(vha,
450 if (unlikely(!host)) {
451 ql_dbg(ql_dbg_tgt, vha, 0xe041,
452 "qla_target(%d): Response pkt (CTIO_TYPE7) "
453 "received, with unknown vp_index %d\n",
454 vha->vp_idx, entry->vp_index);
457 qlt_response_pkt(host, rsp, pkt);
461 case IMMED_NOTIFY_TYPE:
463 struct scsi_qla_host *host;
464 struct imm_ntfy_from_isp *entry =
465 (struct imm_ntfy_from_isp *)pkt;
467 host = qlt_find_host_by_vp_idx(vha, entry->u.isp24.vp_index);
468 if (unlikely(!host)) {
469 ql_dbg(ql_dbg_tgt, vha, 0xe042,
470 "qla_target(%d): Response pkt (IMMED_NOTIFY_TYPE) "
471 "received, with unknown vp_index %d\n",
472 vha->vp_idx, entry->u.isp24.vp_index);
475 qlt_response_pkt(host, rsp, pkt);
479 case NOTIFY_ACK_TYPE:
481 struct scsi_qla_host *host = vha;
482 struct nack_to_isp *entry = (struct nack_to_isp *)pkt;
484 if (0xFF != entry->u.isp24.vp_index) {
485 host = qlt_find_host_by_vp_idx(vha,
486 entry->u.isp24.vp_index);
487 if (unlikely(!host)) {
488 ql_dbg(ql_dbg_tgt, vha, 0xe043,
489 "qla_target(%d): Response "
490 "pkt (NOTIFY_ACK_TYPE) "
491 "received, with unknown "
492 "vp_index %d\n", vha->vp_idx,
493 entry->u.isp24.vp_index);
497 qlt_response_pkt(host, rsp, pkt);
503 struct abts_recv_from_24xx *entry =
504 (struct abts_recv_from_24xx *)pkt;
505 struct scsi_qla_host *host = qlt_find_host_by_vp_idx(vha,
507 if (unlikely(!host)) {
508 ql_dbg(ql_dbg_tgt, vha, 0xe044,
509 "qla_target(%d): Response pkt "
510 "(ABTS_RECV_24XX) received, with unknown "
511 "vp_index %d\n", vha->vp_idx, entry->vp_index);
514 qlt_response_pkt(host, rsp, pkt);
520 struct abts_resp_to_24xx *entry =
521 (struct abts_resp_to_24xx *)pkt;
522 struct scsi_qla_host *host = qlt_find_host_by_vp_idx(vha,
524 if (unlikely(!host)) {
525 ql_dbg(ql_dbg_tgt, vha, 0xe045,
526 "qla_target(%d): Response pkt "
527 "(ABTS_RECV_24XX) received, with unknown "
528 "vp_index %d\n", vha->vp_idx, entry->vp_index);
531 qlt_response_pkt(host, rsp, pkt);
535 qlt_response_pkt(vha, rsp, pkt);
542 * All qlt_plogi_ack_t operations are protected by hardware_lock
544 static int qla24xx_post_nack_work(struct scsi_qla_host *vha, fc_port_t *fcport,
545 struct imm_ntfy_from_isp *ntfy, int type)
547 struct qla_work_evt *e;
549 e = qla2x00_alloc_work(vha, QLA_EVT_NACK);
551 return QLA_FUNCTION_FAILED;
553 e->u.nack.fcport = fcport;
554 e->u.nack.type = type;
555 memcpy(e->u.nack.iocb, ntfy, sizeof(struct imm_ntfy_from_isp));
556 return qla2x00_post_work(vha, e);
559 static void qla2x00_async_nack_sp_done(srb_t *sp, int res)
561 struct scsi_qla_host *vha = sp->vha;
564 ql_dbg(ql_dbg_disc, vha, 0x20f2,
565 "Async done-%s res %x %8phC type %d\n",
566 sp->name, res, sp->fcport->port_name, sp->type);
568 spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
569 sp->fcport->flags &= ~FCF_ASYNC_SENT;
570 sp->fcport->chip_reset = vha->hw->base_qpair->chip_reset;
574 sp->fcport->login_gen++;
575 sp->fcport->fw_login_state = DSC_LS_PLOGI_COMP;
576 sp->fcport->logout_on_delete = 1;
577 sp->fcport->plogi_nack_done_deadline = jiffies + HZ;
578 sp->fcport->send_els_logo = 0;
580 if (sp->fcport->flags & FCF_FCSP_DEVICE) {
581 ql_dbg(ql_dbg_edif, vha, 0x20ef,
582 "%s %8phC edif: PLOGI- AUTH WAIT\n", __func__,
583 sp->fcport->port_name);
584 qla2x00_set_fcport_disc_state(sp->fcport,
585 DSC_LOGIN_AUTH_PEND);
586 qla2x00_post_aen_work(vha, FCH_EVT_PORT_ONLINE,
587 sp->fcport->d_id.b24);
588 qla_edb_eventcreate(vha, VND_CMD_AUTH_STATE_NEEDED, sp->fcport->d_id.b24,
594 sp->fcport->fw_login_state = DSC_LS_PRLI_COMP;
595 sp->fcport->deleted = 0;
596 sp->fcport->send_els_logo = 0;
598 if (!sp->fcport->login_succ &&
599 !IS_SW_RESV_ADDR(sp->fcport->d_id)) {
600 sp->fcport->login_succ = 1;
603 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
604 qla24xx_sched_upd_fcport(sp->fcport);
605 spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
607 sp->fcport->login_retry = 0;
608 qla2x00_set_fcport_disc_state(sp->fcport,
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;
638 if (vha->hw->flags.edif_enabled &&
639 (le16_to_cpu(ntfy->u.isp24.flags) & NOTIFY24XX_FLAGS_FCSP))
640 fcport->flags |= FCF_FCSP_DEVICE;
643 fcport->fw_login_state = DSC_LS_PRLI_PEND;
648 fcport->fw_login_state = DSC_LS_LOGO_PEND;
653 sp = qla2x00_get_sp(vha, fcport, GFP_ATOMIC);
660 sp->u.iocb_cmd.timeout = qla2x00_async_iocb_timeout;
661 qla2x00_init_timer(sp, qla2x00_get_async_timeout(vha)+2);
663 sp->u.iocb_cmd.u.nack.ntfy = ntfy;
664 sp->done = qla2x00_async_nack_sp_done;
666 ql_dbg(ql_dbg_disc, vha, 0x20f4,
667 "Async-%s %8phC hndl %x %s\n",
668 sp->name, fcport->port_name, sp->handle, c);
670 rval = qla2x00_start_sp(sp);
671 if (rval != QLA_SUCCESS)
679 fcport->flags &= ~FCF_ASYNC_SENT;
683 void qla24xx_do_nack_work(struct scsi_qla_host *vha, struct qla_work_evt *e)
687 switch (e->u.nack.type) {
689 t = e->u.nack.fcport;
690 flush_work(&t->del_work);
691 flush_work(&t->free_work);
692 mutex_lock(&vha->vha_tgt.tgt_mutex);
693 t = qlt_create_sess(vha, e->u.nack.fcport, 0);
694 mutex_unlock(&vha->vha_tgt.tgt_mutex);
696 ql_log(ql_log_info, vha, 0xd034,
697 "%s create sess success %p", __func__, t);
698 /* create sess has an extra kref */
699 vha->hw->tgt.tgt_ops->put_sess(e->u.nack.fcport);
703 qla24xx_async_notify_ack(vha, e->u.nack.fcport,
704 (struct imm_ntfy_from_isp *)e->u.nack.iocb, e->u.nack.type);
707 void qla24xx_delete_sess_fn(struct work_struct *work)
709 fc_port_t *fcport = container_of(work, struct fc_port, del_work);
710 struct qla_hw_data *ha = NULL;
712 if (!fcport || !fcport->vha || !fcport->vha->hw)
715 ha = fcport->vha->hw;
717 if (fcport->se_sess) {
718 ha->tgt.tgt_ops->shutdown_sess(fcport);
719 ha->tgt.tgt_ops->put_sess(fcport);
721 qlt_unreg_sess(fcport);
726 * Called from qla2x00_reg_remote_port()
728 void qlt_fc_port_added(struct scsi_qla_host *vha, fc_port_t *fcport)
730 struct qla_hw_data *ha = vha->hw;
731 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
732 struct fc_port *sess = fcport;
735 if (!vha->hw->tgt.tgt_ops)
738 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
740 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
744 if (fcport->disc_state == DSC_DELETE_PEND) {
745 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
749 if (!sess->se_sess) {
750 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
752 mutex_lock(&vha->vha_tgt.tgt_mutex);
753 sess = qlt_create_sess(vha, fcport, false);
754 mutex_unlock(&vha->vha_tgt.tgt_mutex);
756 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
758 if (fcport->fw_login_state == DSC_LS_PRLI_COMP) {
759 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
763 if (!kref_get_unless_zero(&sess->sess_kref)) {
764 ql_dbg(ql_dbg_disc, vha, 0x2107,
765 "%s: kref_get fail sess %8phC \n",
766 __func__, sess->port_name);
767 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
771 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04c,
772 "qla_target(%u): %ssession for port %8phC "
773 "(loop ID %d) reappeared\n", vha->vp_idx,
774 sess->local ? "local " : "", sess->port_name, sess->loop_id);
776 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf007,
777 "Reappeared sess %p\n", sess);
779 ha->tgt.tgt_ops->update_sess(sess, fcport->d_id,
781 (fcport->flags & FCF_CONF_COMP_SUPPORTED));
784 if (sess && sess->local) {
785 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04d,
786 "qla_target(%u): local session for "
787 "port %8phC (loop ID %d) became global\n", vha->vp_idx,
788 fcport->port_name, sess->loop_id);
791 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
793 ha->tgt.tgt_ops->put_sess(sess);
797 * This is a zero-base ref-counting solution, since hardware_lock
798 * guarantees that ref_count is not modified concurrently.
799 * Upon successful return content of iocb is undefined
801 static struct qlt_plogi_ack_t *
802 qlt_plogi_ack_find_add(struct scsi_qla_host *vha, port_id_t *id,
803 struct imm_ntfy_from_isp *iocb)
805 struct qlt_plogi_ack_t *pla;
807 lockdep_assert_held(&vha->hw->hardware_lock);
809 list_for_each_entry(pla, &vha->plogi_ack_list, list) {
810 if (pla->id.b24 == id->b24) {
811 ql_dbg(ql_dbg_disc + ql_dbg_verbose, vha, 0x210d,
812 "%s %d %8phC Term INOT due to new INOT",
814 pla->iocb.u.isp24.port_name);
815 qlt_send_term_imm_notif(vha, &pla->iocb, 1);
816 memcpy(&pla->iocb, iocb, sizeof(pla->iocb));
821 pla = kmem_cache_zalloc(qla_tgt_plogi_cachep, GFP_ATOMIC);
823 ql_dbg(ql_dbg_async, vha, 0x5088,
824 "qla_target(%d): Allocation of plogi_ack failed\n",
829 memcpy(&pla->iocb, iocb, sizeof(pla->iocb));
831 list_add_tail(&pla->list, &vha->plogi_ack_list);
836 void qlt_plogi_ack_unref(struct scsi_qla_host *vha,
837 struct qlt_plogi_ack_t *pla)
839 struct imm_ntfy_from_isp *iocb = &pla->iocb;
842 fc_port_t *fcport = pla->fcport;
844 BUG_ON(!pla->ref_count);
850 ql_dbg(ql_dbg_disc, vha, 0x5089,
851 "Sending PLOGI ACK to wwn %8phC s_id %02x:%02x:%02x loop_id %#04x"
852 " exch %#x ox_id %#x\n", iocb->u.isp24.port_name,
853 iocb->u.isp24.port_id[2], iocb->u.isp24.port_id[1],
854 iocb->u.isp24.port_id[0],
855 le16_to_cpu(iocb->u.isp24.nport_handle),
856 iocb->u.isp24.exchange_address, iocb->ox_id);
858 port_id.b.domain = iocb->u.isp24.port_id[2];
859 port_id.b.area = iocb->u.isp24.port_id[1];
860 port_id.b.al_pa = iocb->u.isp24.port_id[0];
861 port_id.b.rsvd_1 = 0;
863 loop_id = le16_to_cpu(iocb->u.isp24.nport_handle);
865 fcport->loop_id = loop_id;
866 fcport->d_id = port_id;
867 if (iocb->u.isp24.status_subcode == ELS_PLOGI)
868 qla24xx_post_nack_work(vha, fcport, iocb, SRB_NACK_PLOGI);
870 qla24xx_post_nack_work(vha, fcport, iocb, SRB_NACK_PRLI);
872 list_for_each_entry(fcport, &vha->vp_fcports, list) {
873 if (fcport->plogi_link[QLT_PLOGI_LINK_SAME_WWN] == pla)
874 fcport->plogi_link[QLT_PLOGI_LINK_SAME_WWN] = NULL;
875 if (fcport->plogi_link[QLT_PLOGI_LINK_CONFLICT] == pla)
876 fcport->plogi_link[QLT_PLOGI_LINK_CONFLICT] = NULL;
879 list_del(&pla->list);
880 kmem_cache_free(qla_tgt_plogi_cachep, pla);
884 qlt_plogi_ack_link(struct scsi_qla_host *vha, struct qlt_plogi_ack_t *pla,
885 struct fc_port *sess, enum qlt_plogi_link_t link)
887 struct imm_ntfy_from_isp *iocb = &pla->iocb;
888 /* Inc ref_count first because link might already be pointing at pla */
891 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf097,
892 "Linking sess %p [%d] wwn %8phC with PLOGI ACK to wwn %8phC"
893 " s_id %02x:%02x:%02x, ref=%d pla %p link %d\n",
894 sess, link, sess->port_name,
895 iocb->u.isp24.port_name, iocb->u.isp24.port_id[2],
896 iocb->u.isp24.port_id[1], iocb->u.isp24.port_id[0],
897 pla->ref_count, pla, link);
899 if (link == QLT_PLOGI_LINK_CONFLICT) {
900 switch (sess->disc_state) {
902 case DSC_DELETE_PEND:
910 if (sess->plogi_link[link])
911 qlt_plogi_ack_unref(vha, sess->plogi_link[link]);
913 if (link == QLT_PLOGI_LINK_SAME_WWN)
916 sess->plogi_link[link] = pla;
920 /* These fields must be initialized by the caller */
923 * number of cmds dropped while we were waiting for
924 * initiator to ack LOGO initialize to 1 if LOGO is
925 * triggered by a command, otherwise, to 0
929 /* These fields are used by callee */
930 struct list_head list;
934 qlt_send_first_logo(struct scsi_qla_host *vha, qlt_port_logo_t *logo)
936 qlt_port_logo_t *tmp;
939 if (test_bit(PFLG_DRIVER_REMOVING, &vha->pci_flags)) {
944 mutex_lock(&vha->vha_tgt.tgt_mutex);
946 list_for_each_entry(tmp, &vha->logo_list, list) {
947 if (tmp->id.b24 == logo->id.b24) {
948 tmp->cmd_count += logo->cmd_count;
949 mutex_unlock(&vha->vha_tgt.tgt_mutex);
954 list_add_tail(&logo->list, &vha->logo_list);
956 mutex_unlock(&vha->vha_tgt.tgt_mutex);
958 res = qla24xx_els_dcmd_iocb(vha, ELS_DCMD_LOGO, logo->id);
960 mutex_lock(&vha->vha_tgt.tgt_mutex);
961 list_del(&logo->list);
962 mutex_unlock(&vha->vha_tgt.tgt_mutex);
965 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf098,
966 "Finished LOGO to %02x:%02x:%02x, dropped %d cmds, res = %#x\n",
967 logo->id.b.domain, logo->id.b.area, logo->id.b.al_pa,
968 logo->cmd_count, res);
971 void qlt_free_session_done(struct work_struct *work)
973 struct fc_port *sess = container_of(work, struct fc_port,
975 struct qla_tgt *tgt = sess->tgt;
976 struct scsi_qla_host *vha = sess->vha;
977 struct qla_hw_data *ha = vha->hw;
979 bool logout_started = false;
980 scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
981 struct qlt_plogi_ack_t *own =
982 sess->plogi_link[QLT_PLOGI_LINK_SAME_WWN];
984 ql_dbg(ql_dbg_disc, vha, 0xf084,
985 "%s: se_sess %p / sess %p from port %8phC loop_id %#04x"
986 " s_id %02x:%02x:%02x logout %d keep %d els_logo %d\n",
987 __func__, sess->se_sess, sess, sess->port_name, sess->loop_id,
988 sess->d_id.b.domain, sess->d_id.b.area, sess->d_id.b.al_pa,
989 sess->logout_on_delete, sess->keep_nport_handle,
990 sess->send_els_logo);
992 if (!IS_SW_RESV_ADDR(sess->d_id)) {
993 if (ha->flags.edif_enabled &&
994 (!own || own->iocb.u.isp24.status_subcode == ELS_PLOGI)) {
995 sess->edif.authok = 0;
996 if (!ha->flags.host_shutting_down) {
997 ql_dbg(ql_dbg_edif, vha, 0x911e,
998 "%s wwpn %8phC calling qla2x00_release_all_sadb\n",
999 __func__, sess->port_name);
1000 qla2x00_release_all_sadb(vha, sess);
1002 ql_dbg(ql_dbg_edif, vha, 0x911e,
1003 "%s bypassing release_all_sadb\n",
1006 qla_edif_sess_down(vha, sess);
1008 qla2x00_mark_device_lost(vha, sess, 0);
1010 if (sess->send_els_logo) {
1011 qlt_port_logo_t logo;
1013 logo.id = sess->d_id;
1015 INIT_LIST_HEAD(&logo.list);
1017 qlt_send_first_logo(vha, &logo);
1018 sess->send_els_logo = 0;
1021 if (sess->logout_on_delete && sess->loop_id != FC_NO_LOOP_ID) {
1025 (own->iocb.u.isp24.status_subcode == ELS_PLOGI)) {
1026 sess->logout_completed = 0;
1027 rc = qla2x00_post_async_logout_work(vha, sess,
1029 if (rc != QLA_SUCCESS)
1030 ql_log(ql_log_warn, vha, 0xf085,
1031 "Schedule logo failed sess %p rc %d\n",
1034 logout_started = true;
1035 } else if (own && (own->iocb.u.isp24.status_subcode ==
1036 ELS_PRLI) && ha->flags.rida_fmt2) {
1037 rc = qla2x00_post_async_prlo_work(vha, sess,
1039 if (rc != QLA_SUCCESS)
1040 ql_log(ql_log_warn, vha, 0xf085,
1041 "Schedule PRLO failed sess %p rc %d\n",
1044 logout_started = true;
1046 } /* if sess->logout_on_delete */
1048 if (sess->nvme_flag & NVME_FLAG_REGISTERED &&
1049 !(sess->nvme_flag & NVME_FLAG_DELETING)) {
1050 sess->nvme_flag |= NVME_FLAG_DELETING;
1051 qla_nvme_unregister_remote_port(sess);
1056 * Release the target session for FC Nexus from fabric module code.
1058 if (sess->se_sess != NULL)
1059 ha->tgt.tgt_ops->free_session(sess);
1061 if (logout_started) {
1062 bool traced = false;
1065 while (!READ_ONCE(sess->logout_completed)) {
1067 ql_dbg(ql_dbg_disc, vha, 0xf086,
1068 "%s: waiting for sess %p logout\n",
1075 * Driver timeout is set to 22 Sec, update count value to loop
1076 * long enough for log-out to complete before advancing. Otherwise,
1077 * straddling logout can interfere with re-login attempt.
1083 ql_dbg(ql_dbg_disc, vha, 0xf087,
1084 "%s: sess %p logout completed\n", __func__, sess);
1087 if (sess->logo_ack_needed) {
1088 sess->logo_ack_needed = 0;
1089 qla24xx_async_notify_ack(vha, sess,
1090 (struct imm_ntfy_from_isp *)sess->iocb, SRB_NACK_LOGO);
1093 spin_lock_irqsave(&vha->work_lock, flags);
1094 sess->flags &= ~FCF_ASYNC_SENT;
1095 spin_unlock_irqrestore(&vha->work_lock, flags);
1097 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
1098 if (sess->se_sess) {
1099 sess->se_sess = NULL;
1100 if (tgt && !IS_SW_RESV_ADDR(sess->d_id))
1104 qla2x00_set_fcport_disc_state(sess, DSC_DELETED);
1105 sess->fw_login_state = DSC_LS_PORT_UNAVAIL;
1106 sess->deleted = QLA_SESS_DELETED;
1108 if (sess->login_succ && !IS_SW_RESV_ADDR(sess->d_id)) {
1109 vha->fcport_count--;
1110 sess->login_succ = 0;
1113 qla2x00_clear_loop_id(sess);
1115 if (sess->conflict) {
1116 sess->conflict->login_pause = 0;
1117 sess->conflict = NULL;
1118 if (!test_bit(UNLOADING, &vha->dpc_flags))
1119 set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
1123 struct qlt_plogi_ack_t *con =
1124 sess->plogi_link[QLT_PLOGI_LINK_CONFLICT];
1125 struct imm_ntfy_from_isp *iocb;
1127 own = sess->plogi_link[QLT_PLOGI_LINK_SAME_WWN];
1131 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf099,
1132 "se_sess %p / sess %p port %8phC is gone,"
1133 " %s (ref=%d), releasing PLOGI for %8phC (ref=%d)\n",
1134 sess->se_sess, sess, sess->port_name,
1135 own ? "releasing own PLOGI" : "no own PLOGI pending",
1136 own ? own->ref_count : -1,
1137 iocb->u.isp24.port_name, con->ref_count);
1138 qlt_plogi_ack_unref(vha, con);
1139 sess->plogi_link[QLT_PLOGI_LINK_CONFLICT] = NULL;
1141 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf09a,
1142 "se_sess %p / sess %p port %8phC is gone, %s (ref=%d)\n",
1143 sess->se_sess, sess, sess->port_name,
1144 own ? "releasing own PLOGI" :
1145 "no own PLOGI pending",
1146 own ? own->ref_count : -1);
1150 sess->fw_login_state = DSC_LS_PLOGI_PEND;
1151 qlt_plogi_ack_unref(vha, own);
1152 sess->plogi_link[QLT_PLOGI_LINK_SAME_WWN] = NULL;
1156 sess->explicit_logout = 0;
1157 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
1158 sess->free_pending = 0;
1160 qla2x00_dfs_remove_rport(vha, sess);
1162 ql_dbg(ql_dbg_disc, vha, 0xf001,
1163 "Unregistration of sess %p %8phC finished fcp_cnt %d\n",
1164 sess, sess->port_name, vha->fcport_count);
1166 if (tgt && (tgt->sess_count == 0))
1167 wake_up_all(&tgt->waitQ);
1169 if (!test_bit(PFLG_DRIVER_REMOVING, &base_vha->pci_flags) &&
1170 !(vha->vp_idx && test_bit(VPORT_DELETE, &vha->dpc_flags)) &&
1171 (!tgt || !tgt->tgt_stop) && !LOOP_TRANSITION(vha)) {
1172 switch (vha->host->active_mode) {
1173 case MODE_INITIATOR:
1175 set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
1176 qla2xxx_wake_dpc(vha);
1185 if (vha->fcport_count == 0)
1186 wake_up_all(&vha->fcport_waitQ);
1189 /* ha->tgt.sess_lock supposed to be held on entry */
1190 void qlt_unreg_sess(struct fc_port *sess)
1192 struct scsi_qla_host *vha = sess->vha;
1193 unsigned long flags;
1195 ql_dbg(ql_dbg_disc, sess->vha, 0x210a,
1196 "%s sess %p for deletion %8phC\n",
1197 __func__, sess, sess->port_name);
1199 spin_lock_irqsave(&sess->vha->work_lock, flags);
1200 if (sess->free_pending) {
1201 spin_unlock_irqrestore(&sess->vha->work_lock, flags);
1204 sess->free_pending = 1;
1206 * Use FCF_ASYNC_SENT flag to block other cmds used in sess
1207 * management from being sent.
1209 sess->flags |= FCF_ASYNC_SENT;
1210 spin_unlock_irqrestore(&sess->vha->work_lock, flags);
1213 vha->hw->tgt.tgt_ops->clear_nacl_from_fcport_map(sess);
1215 sess->deleted = QLA_SESS_DELETION_IN_PROGRESS;
1216 qla2x00_set_fcport_disc_state(sess, DSC_DELETE_PEND);
1217 sess->last_rscn_gen = sess->rscn_gen;
1218 sess->last_login_gen = sess->login_gen;
1220 queue_work(sess->vha->hw->wq, &sess->free_work);
1222 EXPORT_SYMBOL(qlt_unreg_sess);
1224 static int qlt_reset(struct scsi_qla_host *vha, void *iocb, int mcmd)
1226 struct qla_hw_data *ha = vha->hw;
1227 struct fc_port *sess = NULL;
1230 struct imm_ntfy_from_isp *n = (struct imm_ntfy_from_isp *)iocb;
1231 unsigned long flags;
1233 loop_id = le16_to_cpu(n->u.isp24.nport_handle);
1234 if (loop_id == 0xFFFF) {
1236 atomic_inc(&vha->vha_tgt.qla_tgt->tgt_global_resets_count);
1237 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
1238 qlt_clear_tgt_db(vha->vha_tgt.qla_tgt);
1239 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
1241 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
1242 sess = ha->tgt.tgt_ops->find_sess_by_loop_id(vha, loop_id);
1243 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
1246 ql_dbg(ql_dbg_tgt, vha, 0xe000,
1247 "Using sess for qla_tgt_reset: %p\n", sess);
1253 ql_dbg(ql_dbg_tgt, vha, 0xe047,
1254 "scsi(%ld): resetting (session %p from port %8phC mcmd %x, "
1255 "loop_id %d)\n", vha->host_no, sess, sess->port_name,
1258 return qlt_issue_task_mgmt(sess, 0, mcmd, iocb, QLA24XX_MGMT_SEND_NACK);
1261 static void qla24xx_chk_fcp_state(struct fc_port *sess)
1263 if (sess->chip_reset != sess->vha->hw->base_qpair->chip_reset) {
1264 sess->logout_on_delete = 0;
1265 sess->logo_ack_needed = 0;
1266 sess->fw_login_state = DSC_LS_PORT_UNAVAIL;
1270 void qlt_schedule_sess_for_deletion(struct fc_port *sess)
1272 struct qla_tgt *tgt = sess->tgt;
1273 unsigned long flags;
1276 switch (sess->disc_state) {
1277 case DSC_DELETE_PEND:
1280 if (!sess->plogi_link[QLT_PLOGI_LINK_SAME_WWN] &&
1281 !sess->plogi_link[QLT_PLOGI_LINK_CONFLICT]) {
1282 if (tgt && tgt->tgt_stop && tgt->sess_count == 0)
1283 wake_up_all(&tgt->waitQ);
1285 if (sess->vha->fcport_count == 0)
1286 wake_up_all(&sess->vha->fcport_waitQ);
1290 case DSC_UPD_FCPORT:
1292 * This port is not done reporting to upper layer.
1295 sess->next_disc_state = DSC_DELETE_PEND;
1296 sec = jiffies_to_msecs(jiffies -
1297 sess->jiffies_at_registration)/1000;
1298 if (sess->sec_since_registration < sec && sec && !(sec % 5)) {
1299 sess->sec_since_registration = sec;
1300 ql_dbg(ql_dbg_disc, sess->vha, 0xffff,
1301 "%s %8phC : Slow Rport registration(%d Sec)\n",
1302 __func__, sess->port_name, sec);
1309 spin_lock_irqsave(&sess->vha->work_lock, flags);
1310 if (sess->deleted == QLA_SESS_DELETION_IN_PROGRESS) {
1311 spin_unlock_irqrestore(&sess->vha->work_lock, flags);
1314 sess->deleted = QLA_SESS_DELETION_IN_PROGRESS;
1315 spin_unlock_irqrestore(&sess->vha->work_lock, flags);
1317 sess->prli_pend_timer = 0;
1318 qla2x00_set_fcport_disc_state(sess, DSC_DELETE_PEND);
1320 qla24xx_chk_fcp_state(sess);
1322 ql_dbg(ql_log_warn, sess->vha, 0xe001,
1323 "Scheduling sess %p for deletion %8phC fc4_type %x\n",
1324 sess, sess->port_name, sess->fc4_type);
1326 WARN_ON(!queue_work(sess->vha->hw->wq, &sess->del_work));
1329 static void qlt_clear_tgt_db(struct qla_tgt *tgt)
1331 struct fc_port *sess;
1332 scsi_qla_host_t *vha = tgt->vha;
1334 list_for_each_entry(sess, &vha->vp_fcports, list) {
1336 qlt_schedule_sess_for_deletion(sess);
1339 /* At this point tgt could be already dead */
1342 static int qla24xx_get_loop_id(struct scsi_qla_host *vha, be_id_t s_id,
1345 struct qla_hw_data *ha = vha->hw;
1346 dma_addr_t gid_list_dma;
1347 struct gid_list_info *gid_list, *gid;
1351 gid_list = dma_alloc_coherent(&ha->pdev->dev, qla2x00_gid_list_size(ha),
1352 &gid_list_dma, GFP_KERNEL);
1354 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf044,
1355 "qla_target(%d): DMA Alloc failed of %u\n",
1356 vha->vp_idx, qla2x00_gid_list_size(ha));
1360 /* Get list of logged in devices */
1361 rc = qla24xx_gidlist_wait(vha, gid_list, gid_list_dma, &entries);
1362 if (rc != QLA_SUCCESS) {
1363 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf045,
1364 "qla_target(%d): get_id_list() failed: %x\n",
1367 goto out_free_id_list;
1372 for (i = 0; i < entries; i++) {
1373 if (gid->al_pa == s_id.al_pa &&
1374 gid->area == s_id.area &&
1375 gid->domain == s_id.domain) {
1376 *loop_id = le16_to_cpu(gid->loop_id);
1380 gid = (void *)gid + ha->gid_list_info_size;
1384 dma_free_coherent(&ha->pdev->dev, qla2x00_gid_list_size(ha),
1385 gid_list, gid_list_dma);
1390 * Adds an extra ref to allow to drop hw lock after adding sess to the list.
1391 * Caller must put it.
1393 static struct fc_port *qlt_create_sess(
1394 struct scsi_qla_host *vha,
1398 struct qla_hw_data *ha = vha->hw;
1399 struct fc_port *sess = fcport;
1400 unsigned long flags;
1402 if (vha->vha_tgt.qla_tgt->tgt_stop)
1405 if (fcport->se_sess) {
1406 if (!kref_get_unless_zero(&sess->sess_kref)) {
1407 ql_dbg(ql_dbg_disc, vha, 0x20f6,
1408 "%s: kref_get_unless_zero failed for %8phC\n",
1409 __func__, sess->port_name);
1414 sess->tgt = vha->vha_tgt.qla_tgt;
1415 sess->local = local;
1418 * Under normal circumstances we want to logout from firmware when
1419 * session eventually ends and release corresponding nport handle.
1420 * In the exception cases (e.g. when new PLOGI is waiting) corresponding
1421 * code will adjust these flags as necessary.
1423 sess->logout_on_delete = 1;
1424 sess->keep_nport_handle = 0;
1425 sess->logout_completed = 0;
1427 if (ha->tgt.tgt_ops->check_initiator_node_acl(vha,
1428 &fcport->port_name[0], sess) < 0) {
1429 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf015,
1430 "(%d) %8phC check_initiator_node_acl failed\n",
1431 vha->vp_idx, fcport->port_name);
1434 kref_init(&fcport->sess_kref);
1436 * Take an extra reference to ->sess_kref here to handle
1437 * fc_port access across ->tgt.sess_lock reaquire.
1439 if (!kref_get_unless_zero(&sess->sess_kref)) {
1440 ql_dbg(ql_dbg_disc, vha, 0x20f7,
1441 "%s: kref_get_unless_zero failed for %8phC\n",
1442 __func__, sess->port_name);
1446 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
1447 if (!IS_SW_RESV_ADDR(sess->d_id))
1448 vha->vha_tgt.qla_tgt->sess_count++;
1450 qlt_do_generation_tick(vha, &sess->generation);
1451 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
1454 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf006,
1455 "Adding sess %p se_sess %p to tgt %p sess_count %d\n",
1456 sess, sess->se_sess, vha->vha_tgt.qla_tgt,
1457 vha->vha_tgt.qla_tgt->sess_count);
1459 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04b,
1460 "qla_target(%d): %ssession for wwn %8phC (loop_id %d, "
1461 "s_id %x:%x:%x, confirmed completion %ssupported) added\n",
1462 vha->vp_idx, local ? "local " : "", fcport->port_name,
1463 fcport->loop_id, sess->d_id.b.domain, sess->d_id.b.area,
1464 sess->d_id.b.al_pa, sess->conf_compl_supported ? "" : "not ");
1470 * max_gen - specifies maximum session generation
1471 * at which this deletion requestion is still valid
1474 qlt_fc_port_deleted(struct scsi_qla_host *vha, fc_port_t *fcport, int max_gen)
1476 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
1477 struct fc_port *sess = fcport;
1478 unsigned long flags;
1480 if (!vha->hw->tgt.tgt_ops)
1486 spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
1487 if (tgt->tgt_stop) {
1488 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
1491 if (!sess->se_sess) {
1492 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
1496 if (max_gen - sess->generation < 0) {
1497 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
1498 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf092,
1499 "Ignoring stale deletion request for se_sess %p / sess %p"
1500 " for port %8phC, req_gen %d, sess_gen %d\n",
1501 sess->se_sess, sess, sess->port_name, max_gen,
1506 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf008, "qla_tgt_fc_port_deleted %p", sess);
1509 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
1510 qlt_schedule_sess_for_deletion(sess);
1513 static inline int test_tgt_sess_count(struct qla_tgt *tgt)
1515 struct qla_hw_data *ha = tgt->ha;
1516 unsigned long flags;
1519 * We need to protect against race, when tgt is freed before or
1522 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
1523 ql_dbg(ql_dbg_tgt, tgt->vha, 0xe002,
1524 "tgt %p, sess_count=%d\n",
1525 tgt, tgt->sess_count);
1526 res = (tgt->sess_count == 0);
1527 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
1532 /* Called by tcm_qla2xxx configfs code */
1533 int qlt_stop_phase1(struct qla_tgt *tgt)
1535 struct scsi_qla_host *vha = tgt->vha;
1536 struct qla_hw_data *ha = tgt->ha;
1537 unsigned long flags;
1539 mutex_lock(&ha->optrom_mutex);
1540 mutex_lock(&qla_tgt_mutex);
1542 if (tgt->tgt_stop || tgt->tgt_stopped) {
1543 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04e,
1544 "Already in tgt->tgt_stop or tgt_stopped state\n");
1545 mutex_unlock(&qla_tgt_mutex);
1546 mutex_unlock(&ha->optrom_mutex);
1550 ql_dbg(ql_dbg_tgt_mgt, vha, 0xe003, "Stopping target for host %ld(%p)\n",
1553 * Mutex needed to sync with qla_tgt_fc_port_[added,deleted].
1554 * Lock is needed, because we still can get an incoming packet.
1556 mutex_lock(&vha->vha_tgt.tgt_mutex);
1558 qlt_clear_tgt_db(tgt);
1559 mutex_unlock(&vha->vha_tgt.tgt_mutex);
1560 mutex_unlock(&qla_tgt_mutex);
1562 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf009,
1563 "Waiting for sess works (tgt %p)", tgt);
1564 spin_lock_irqsave(&tgt->sess_work_lock, flags);
1565 while (!list_empty(&tgt->sess_works_list)) {
1566 spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
1567 flush_scheduled_work();
1568 spin_lock_irqsave(&tgt->sess_work_lock, flags);
1570 spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
1572 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00a,
1573 "Waiting for tgt %p: sess_count=%d\n", tgt, tgt->sess_count);
1575 wait_event_timeout(tgt->waitQ, test_tgt_sess_count(tgt), 10*HZ);
1578 if (!ha->flags.host_shutting_down &&
1579 (qla_tgt_mode_enabled(vha) || qla_dual_mode_enabled(vha)))
1580 qlt_disable_vha(vha);
1582 /* Wait for sessions to clear out (just in case) */
1583 wait_event_timeout(tgt->waitQ, test_tgt_sess_count(tgt), 10*HZ);
1584 mutex_unlock(&ha->optrom_mutex);
1588 EXPORT_SYMBOL(qlt_stop_phase1);
1590 /* Called by tcm_qla2xxx configfs code */
1591 void qlt_stop_phase2(struct qla_tgt *tgt)
1593 scsi_qla_host_t *vha = tgt->vha;
1595 if (tgt->tgt_stopped) {
1596 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04f,
1597 "Already in tgt->tgt_stopped state\n");
1601 if (!tgt->tgt_stop) {
1602 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00b,
1603 "%s: phase1 stop is not completed\n", __func__);
1608 mutex_lock(&tgt->ha->optrom_mutex);
1609 mutex_lock(&vha->vha_tgt.tgt_mutex);
1611 tgt->tgt_stopped = 1;
1612 mutex_unlock(&vha->vha_tgt.tgt_mutex);
1613 mutex_unlock(&tgt->ha->optrom_mutex);
1615 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00c, "Stop of tgt %p finished\n",
1618 switch (vha->qlini_mode) {
1619 case QLA2XXX_INI_MODE_EXCLUSIVE:
1620 vha->flags.online = 1;
1621 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
1627 EXPORT_SYMBOL(qlt_stop_phase2);
1629 /* Called from qlt_remove_target() -> qla2x00_remove_one() */
1630 static void qlt_release(struct qla_tgt *tgt)
1632 scsi_qla_host_t *vha = tgt->vha;
1636 struct qla_qpair_hint *h;
1637 struct qla_hw_data *ha = vha->hw;
1639 if (!tgt->tgt_stop && !tgt->tgt_stopped)
1640 qlt_stop_phase1(tgt);
1642 if (!tgt->tgt_stopped)
1643 qlt_stop_phase2(tgt);
1645 for (i = 0; i < vha->hw->max_qpairs + 1; i++) {
1646 unsigned long flags;
1648 h = &tgt->qphints[i];
1650 spin_lock_irqsave(h->qpair->qp_lock_ptr, flags);
1651 list_del(&h->hint_elem);
1652 spin_unlock_irqrestore(h->qpair->qp_lock_ptr, flags);
1656 kfree(tgt->qphints);
1657 mutex_lock(&qla_tgt_mutex);
1658 list_del(&vha->vha_tgt.qla_tgt->tgt_list_entry);
1659 mutex_unlock(&qla_tgt_mutex);
1661 btree_for_each_safe64(&tgt->lun_qpair_map, key, node)
1662 btree_remove64(&tgt->lun_qpair_map, key);
1664 btree_destroy64(&tgt->lun_qpair_map);
1667 if (ha->tgt.tgt_ops &&
1668 ha->tgt.tgt_ops->remove_target &&
1669 vha->vha_tgt.target_lport_ptr)
1670 ha->tgt.tgt_ops->remove_target(vha);
1672 vha->vha_tgt.qla_tgt = NULL;
1674 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00d,
1675 "Release of tgt %p finished\n", tgt);
1680 /* ha->hardware_lock supposed to be held on entry */
1681 static int qlt_sched_sess_work(struct qla_tgt *tgt, int type,
1682 const void *param, unsigned int param_size)
1684 struct qla_tgt_sess_work_param *prm;
1685 unsigned long flags;
1687 prm = kzalloc(sizeof(*prm), GFP_ATOMIC);
1689 ql_dbg(ql_dbg_tgt_mgt, tgt->vha, 0xf050,
1690 "qla_target(%d): Unable to create session "
1691 "work, command will be refused", 0);
1695 ql_dbg(ql_dbg_tgt_mgt, tgt->vha, 0xf00e,
1696 "Scheduling work (type %d, prm %p)"
1697 " to find session for param %p (size %d, tgt %p)\n",
1698 type, prm, param, param_size, tgt);
1701 memcpy(&prm->tm_iocb, param, param_size);
1703 spin_lock_irqsave(&tgt->sess_work_lock, flags);
1704 list_add_tail(&prm->sess_works_list_entry, &tgt->sess_works_list);
1705 spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
1707 schedule_work(&tgt->sess_work);
1713 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
1715 static void qlt_send_notify_ack(struct qla_qpair *qpair,
1716 struct imm_ntfy_from_isp *ntfy,
1717 uint32_t add_flags, uint16_t resp_code, int resp_code_valid,
1718 uint16_t srr_flags, uint16_t srr_reject_code, uint8_t srr_explan)
1720 struct scsi_qla_host *vha = qpair->vha;
1721 struct qla_hw_data *ha = vha->hw;
1723 struct nack_to_isp *nack;
1725 if (!ha->flags.fw_started)
1728 ql_dbg(ql_dbg_tgt, vha, 0xe004, "Sending NOTIFY_ACK (ha=%p)\n", ha);
1730 pkt = (request_t *)__qla2x00_alloc_iocbs(qpair, NULL);
1732 ql_dbg(ql_dbg_tgt, vha, 0xe049,
1733 "qla_target(%d): %s failed: unable to allocate "
1734 "request packet\n", vha->vp_idx, __func__);
1738 if (vha->vha_tgt.qla_tgt != NULL)
1739 vha->vha_tgt.qla_tgt->notify_ack_expected++;
1741 pkt->entry_type = NOTIFY_ACK_TYPE;
1742 pkt->entry_count = 1;
1744 nack = (struct nack_to_isp *)pkt;
1745 nack->ox_id = ntfy->ox_id;
1747 nack->u.isp24.handle = QLA_TGT_SKIP_HANDLE;
1748 nack->u.isp24.nport_handle = ntfy->u.isp24.nport_handle;
1749 if (le16_to_cpu(ntfy->u.isp24.status) == IMM_NTFY_ELS) {
1750 nack->u.isp24.flags = ntfy->u.isp24.flags &
1751 cpu_to_le16(NOTIFY24XX_FLAGS_PUREX_IOCB);
1753 nack->u.isp24.srr_rx_id = ntfy->u.isp24.srr_rx_id;
1754 nack->u.isp24.status = ntfy->u.isp24.status;
1755 nack->u.isp24.status_subcode = ntfy->u.isp24.status_subcode;
1756 nack->u.isp24.fw_handle = ntfy->u.isp24.fw_handle;
1757 nack->u.isp24.exchange_address = ntfy->u.isp24.exchange_address;
1758 nack->u.isp24.srr_rel_offs = ntfy->u.isp24.srr_rel_offs;
1759 nack->u.isp24.srr_ui = ntfy->u.isp24.srr_ui;
1760 nack->u.isp24.srr_flags = cpu_to_le16(srr_flags);
1761 nack->u.isp24.srr_reject_code = srr_reject_code;
1762 nack->u.isp24.srr_reject_code_expl = srr_explan;
1763 nack->u.isp24.vp_index = ntfy->u.isp24.vp_index;
1765 /* TODO qualify this with EDIF enable */
1766 if (ntfy->u.isp24.status_subcode == ELS_PLOGI &&
1767 (le16_to_cpu(ntfy->u.isp24.flags) & NOTIFY24XX_FLAGS_FCSP)) {
1768 nack->u.isp24.flags |= cpu_to_le16(NOTIFY_ACK_FLAGS_FCSP);
1771 ql_dbg(ql_dbg_tgt, vha, 0xe005,
1772 "qla_target(%d): Sending 24xx Notify Ack %d\n",
1773 vha->vp_idx, nack->u.isp24.status);
1775 /* Memory Barrier */
1777 qla2x00_start_iocbs(vha, qpair->req);
1780 static int qlt_build_abts_resp_iocb(struct qla_tgt_mgmt_cmd *mcmd)
1782 struct scsi_qla_host *vha = mcmd->vha;
1783 struct qla_hw_data *ha = vha->hw;
1784 struct abts_resp_to_24xx *resp;
1789 struct abts_recv_from_24xx *abts = &mcmd->orig_iocb.abts;
1790 struct qla_qpair *qpair = mcmd->qpair;
1792 ql_dbg(ql_dbg_tgt, vha, 0xe006,
1793 "Sending task mgmt ABTS response (ha=%p, status=%x)\n",
1794 ha, mcmd->fc_tm_rsp);
1796 rc = qlt_check_reserve_free_req(qpair, 1);
1798 ql_dbg(ql_dbg_tgt, vha, 0xe04a,
1799 "qla_target(%d): %s failed: unable to allocate request packet\n",
1800 vha->vp_idx, __func__);
1804 resp = (struct abts_resp_to_24xx *)qpair->req->ring_ptr;
1805 memset(resp, 0, sizeof(*resp));
1807 h = qlt_make_handle(qpair);
1808 if (unlikely(h == QLA_TGT_NULL_HANDLE)) {
1810 * CTIO type 7 from the firmware doesn't provide a way to
1811 * know the initiator's LOOP ID, hence we can't find
1812 * the session and, so, the command.
1816 qpair->req->outstanding_cmds[h] = (srb_t *)mcmd;
1819 resp->handle = make_handle(qpair->req->id, h);
1820 resp->entry_type = ABTS_RESP_24XX;
1821 resp->entry_count = 1;
1822 resp->nport_handle = abts->nport_handle;
1823 resp->vp_index = vha->vp_idx;
1824 resp->sof_type = abts->sof_type;
1825 resp->exchange_address = abts->exchange_address;
1826 resp->fcp_hdr_le = abts->fcp_hdr_le;
1827 f_ctl = cpu_to_le32(F_CTL_EXCH_CONTEXT_RESP |
1828 F_CTL_LAST_SEQ | F_CTL_END_SEQ |
1829 F_CTL_SEQ_INITIATIVE);
1830 p = (uint8_t *)&f_ctl;
1831 resp->fcp_hdr_le.f_ctl[0] = *p++;
1832 resp->fcp_hdr_le.f_ctl[1] = *p++;
1833 resp->fcp_hdr_le.f_ctl[2] = *p;
1835 resp->fcp_hdr_le.d_id = abts->fcp_hdr_le.s_id;
1836 resp->fcp_hdr_le.s_id = abts->fcp_hdr_le.d_id;
1838 resp->exchange_addr_to_abort = abts->exchange_addr_to_abort;
1839 if (mcmd->fc_tm_rsp == FCP_TMF_CMPL) {
1840 resp->fcp_hdr_le.r_ctl = R_CTL_BASIC_LINK_SERV | R_CTL_B_ACC;
1841 resp->payload.ba_acct.seq_id_valid = SEQ_ID_INVALID;
1842 resp->payload.ba_acct.low_seq_cnt = 0x0000;
1843 resp->payload.ba_acct.high_seq_cnt = cpu_to_le16(0xFFFF);
1844 resp->payload.ba_acct.ox_id = abts->fcp_hdr_le.ox_id;
1845 resp->payload.ba_acct.rx_id = abts->fcp_hdr_le.rx_id;
1847 resp->fcp_hdr_le.r_ctl = R_CTL_BASIC_LINK_SERV | R_CTL_B_RJT;
1848 resp->payload.ba_rjt.reason_code =
1849 BA_RJT_REASON_CODE_UNABLE_TO_PERFORM;
1850 /* Other bytes are zero */
1853 vha->vha_tgt.qla_tgt->abts_resp_expected++;
1855 /* Memory Barrier */
1857 if (qpair->reqq_start_iocbs)
1858 qpair->reqq_start_iocbs(qpair);
1860 qla2x00_start_iocbs(vha, qpair->req);
1866 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
1868 static void qlt_24xx_send_abts_resp(struct qla_qpair *qpair,
1869 struct abts_recv_from_24xx *abts, uint32_t status,
1872 struct scsi_qla_host *vha = qpair->vha;
1873 struct qla_hw_data *ha = vha->hw;
1874 struct abts_resp_to_24xx *resp;
1878 ql_dbg(ql_dbg_tgt, vha, 0xe006,
1879 "Sending task mgmt ABTS response (ha=%p, atio=%p, status=%x\n",
1882 resp = (struct abts_resp_to_24xx *)qla2x00_alloc_iocbs_ready(qpair,
1885 ql_dbg(ql_dbg_tgt, vha, 0xe04a,
1886 "qla_target(%d): %s failed: unable to allocate "
1887 "request packet", vha->vp_idx, __func__);
1891 resp->entry_type = ABTS_RESP_24XX;
1892 resp->handle = QLA_TGT_SKIP_HANDLE;
1893 resp->entry_count = 1;
1894 resp->nport_handle = abts->nport_handle;
1895 resp->vp_index = vha->vp_idx;
1896 resp->sof_type = abts->sof_type;
1897 resp->exchange_address = abts->exchange_address;
1898 resp->fcp_hdr_le = abts->fcp_hdr_le;
1899 f_ctl = cpu_to_le32(F_CTL_EXCH_CONTEXT_RESP |
1900 F_CTL_LAST_SEQ | F_CTL_END_SEQ |
1901 F_CTL_SEQ_INITIATIVE);
1902 p = (uint8_t *)&f_ctl;
1903 resp->fcp_hdr_le.f_ctl[0] = *p++;
1904 resp->fcp_hdr_le.f_ctl[1] = *p++;
1905 resp->fcp_hdr_le.f_ctl[2] = *p;
1907 resp->fcp_hdr_le.d_id = abts->fcp_hdr_le.d_id;
1908 resp->fcp_hdr_le.s_id = abts->fcp_hdr_le.s_id;
1910 resp->fcp_hdr_le.d_id = abts->fcp_hdr_le.s_id;
1911 resp->fcp_hdr_le.s_id = abts->fcp_hdr_le.d_id;
1913 resp->exchange_addr_to_abort = abts->exchange_addr_to_abort;
1914 if (status == FCP_TMF_CMPL) {
1915 resp->fcp_hdr_le.r_ctl = R_CTL_BASIC_LINK_SERV | R_CTL_B_ACC;
1916 resp->payload.ba_acct.seq_id_valid = SEQ_ID_INVALID;
1917 resp->payload.ba_acct.low_seq_cnt = 0x0000;
1918 resp->payload.ba_acct.high_seq_cnt = cpu_to_le16(0xFFFF);
1919 resp->payload.ba_acct.ox_id = abts->fcp_hdr_le.ox_id;
1920 resp->payload.ba_acct.rx_id = abts->fcp_hdr_le.rx_id;
1922 resp->fcp_hdr_le.r_ctl = R_CTL_BASIC_LINK_SERV | R_CTL_B_RJT;
1923 resp->payload.ba_rjt.reason_code =
1924 BA_RJT_REASON_CODE_UNABLE_TO_PERFORM;
1925 /* Other bytes are zero */
1928 vha->vha_tgt.qla_tgt->abts_resp_expected++;
1930 /* Memory Barrier */
1932 if (qpair->reqq_start_iocbs)
1933 qpair->reqq_start_iocbs(qpair);
1935 qla2x00_start_iocbs(vha, qpair->req);
1939 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
1941 static void qlt_24xx_retry_term_exchange(struct scsi_qla_host *vha,
1942 struct qla_qpair *qpair, response_t *pkt, struct qla_tgt_mgmt_cmd *mcmd)
1944 struct ctio7_to_24xx *ctio;
1946 struct abts_recv_from_24xx *entry;
1948 ctio = (struct ctio7_to_24xx *)qla2x00_alloc_iocbs_ready(qpair, NULL);
1950 ql_dbg(ql_dbg_tgt, vha, 0xe04b,
1951 "qla_target(%d): %s failed: unable to allocate "
1952 "request packet\n", vha->vp_idx, __func__);
1957 /* abts from remote port */
1958 entry = &mcmd->orig_iocb.abts;
1960 /* abts from this driver. */
1961 entry = (struct abts_recv_from_24xx *)pkt;
1964 * We've got on entrance firmware's response on by us generated
1965 * ABTS response. So, in it ID fields are reversed.
1968 ctio->entry_type = CTIO_TYPE7;
1969 ctio->entry_count = 1;
1970 ctio->nport_handle = entry->nport_handle;
1971 ctio->handle = QLA_TGT_SKIP_HANDLE | CTIO_COMPLETION_HANDLE_MARK;
1972 ctio->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
1973 ctio->vp_index = vha->vp_idx;
1974 ctio->exchange_addr = entry->exchange_addr_to_abort;
1975 tmp = (CTIO7_FLAGS_STATUS_MODE_1 | CTIO7_FLAGS_TERMINATE);
1978 ctio->initiator_id = entry->fcp_hdr_le.s_id;
1980 if (mcmd->flags & QLA24XX_MGMT_ABORT_IO_ATTR_VALID)
1981 tmp |= (mcmd->abort_io_attr << 9);
1982 else if (qpair->retry_term_cnt & 1)
1985 ctio->initiator_id = entry->fcp_hdr_le.d_id;
1987 if (qpair->retry_term_cnt & 1)
1990 ctio->u.status1.flags = cpu_to_le16(tmp);
1991 ctio->u.status1.ox_id = entry->fcp_hdr_le.ox_id;
1993 ql_dbg(ql_dbg_tgt, vha, 0xe007,
1994 "Sending retry TERM EXCH CTIO7 flags %04xh oxid %04xh attr valid %x\n",
1995 le16_to_cpu(ctio->u.status1.flags),
1996 le16_to_cpu(ctio->u.status1.ox_id),
1997 (mcmd && mcmd->flags & QLA24XX_MGMT_ABORT_IO_ATTR_VALID) ? 1 : 0);
1999 /* Memory Barrier */
2001 if (qpair->reqq_start_iocbs)
2002 qpair->reqq_start_iocbs(qpair);
2004 qla2x00_start_iocbs(vha, qpair->req);
2007 qlt_build_abts_resp_iocb(mcmd);
2009 qlt_24xx_send_abts_resp(qpair,
2010 (struct abts_recv_from_24xx *)entry, FCP_TMF_CMPL, true);
2014 /* drop cmds for the given lun
2015 * XXX only looks for cmds on the port through which lun reset was recieved
2016 * XXX does not go through the list of other port (which may have cmds
2019 static void abort_cmds_for_lun(struct scsi_qla_host *vha, u64 lun, be_id_t s_id)
2021 struct qla_tgt_sess_op *op;
2022 struct qla_tgt_cmd *cmd;
2024 unsigned long flags;
2026 key = sid_to_key(s_id);
2027 spin_lock_irqsave(&vha->cmd_list_lock, flags);
2028 list_for_each_entry(op, &vha->qla_sess_op_cmd_list, cmd_list) {
2032 op_key = sid_to_key(op->atio.u.isp24.fcp_hdr.s_id);
2033 op_lun = scsilun_to_int(
2034 (struct scsi_lun *)&op->atio.u.isp24.fcp_cmnd.lun);
2035 if (op_key == key && op_lun == lun)
2039 list_for_each_entry(op, &vha->unknown_atio_list, cmd_list) {
2043 op_key = sid_to_key(op->atio.u.isp24.fcp_hdr.s_id);
2044 op_lun = scsilun_to_int(
2045 (struct scsi_lun *)&op->atio.u.isp24.fcp_cmnd.lun);
2046 if (op_key == key && op_lun == lun)
2050 list_for_each_entry(cmd, &vha->qla_cmd_list, cmd_list) {
2054 cmd_key = sid_to_key(cmd->atio.u.isp24.fcp_hdr.s_id);
2055 cmd_lun = scsilun_to_int(
2056 (struct scsi_lun *)&cmd->atio.u.isp24.fcp_cmnd.lun);
2057 if (cmd_key == key && cmd_lun == lun)
2060 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
2063 static struct qla_qpair_hint *qlt_find_qphint(struct scsi_qla_host *vha,
2064 uint64_t unpacked_lun)
2066 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
2067 struct qla_qpair_hint *h = NULL;
2069 if (vha->flags.qpairs_available) {
2070 h = btree_lookup64(&tgt->lun_qpair_map, unpacked_lun);
2072 h = &tgt->qphints[0];
2074 h = &tgt->qphints[0];
2080 static void qlt_do_tmr_work(struct work_struct *work)
2082 struct qla_tgt_mgmt_cmd *mcmd =
2083 container_of(work, struct qla_tgt_mgmt_cmd, work);
2084 struct qla_hw_data *ha = mcmd->vha->hw;
2087 unsigned long flags;
2089 switch (mcmd->tmr_func) {
2091 tag = le32_to_cpu(mcmd->orig_iocb.abts.exchange_addr_to_abort);
2098 rc = ha->tgt.tgt_ops->handle_tmr(mcmd, mcmd->unpacked_lun,
2099 mcmd->tmr_func, tag);
2102 spin_lock_irqsave(mcmd->qpair->qp_lock_ptr, flags);
2103 switch (mcmd->tmr_func) {
2105 mcmd->fc_tm_rsp = FCP_TMF_REJECTED;
2106 qlt_build_abts_resp_iocb(mcmd);
2108 case QLA_TGT_LUN_RESET:
2109 case QLA_TGT_CLEAR_TS:
2110 case QLA_TGT_ABORT_TS:
2111 case QLA_TGT_CLEAR_ACA:
2112 case QLA_TGT_TARGET_RESET:
2113 qlt_send_busy(mcmd->qpair, &mcmd->orig_iocb.atio,
2117 case QLA_TGT_ABORT_ALL:
2118 case QLA_TGT_NEXUS_LOSS_SESS:
2119 case QLA_TGT_NEXUS_LOSS:
2120 qlt_send_notify_ack(mcmd->qpair,
2121 &mcmd->orig_iocb.imm_ntfy, 0, 0, 0, 0, 0, 0);
2124 spin_unlock_irqrestore(mcmd->qpair->qp_lock_ptr, flags);
2126 ql_dbg(ql_dbg_tgt_mgt, mcmd->vha, 0xf052,
2127 "qla_target(%d): tgt_ops->handle_tmr() failed: %d\n",
2128 mcmd->vha->vp_idx, rc);
2129 mempool_free(mcmd, qla_tgt_mgmt_cmd_mempool);
2133 /* ha->hardware_lock supposed to be held on entry */
2134 static int __qlt_24xx_handle_abts(struct scsi_qla_host *vha,
2135 struct abts_recv_from_24xx *abts, struct fc_port *sess)
2137 struct qla_hw_data *ha = vha->hw;
2138 struct qla_tgt_mgmt_cmd *mcmd;
2139 struct qla_qpair_hint *h = &vha->vha_tgt.qla_tgt->qphints[0];
2140 struct qla_tgt_cmd *abort_cmd;
2142 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00f,
2143 "qla_target(%d): task abort (tag=%d)\n",
2144 vha->vp_idx, abts->exchange_addr_to_abort);
2146 mcmd = mempool_alloc(qla_tgt_mgmt_cmd_mempool, GFP_ATOMIC);
2148 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf051,
2149 "qla_target(%d): %s: Allocation of ABORT cmd failed",
2150 vha->vp_idx, __func__);
2153 memset(mcmd, 0, sizeof(*mcmd));
2154 mcmd->cmd_type = TYPE_TGT_TMCMD;
2156 memcpy(&mcmd->orig_iocb.abts, abts, sizeof(mcmd->orig_iocb.abts));
2157 mcmd->reset_count = ha->base_qpair->chip_reset;
2158 mcmd->tmr_func = QLA_TGT_ABTS;
2159 mcmd->qpair = h->qpair;
2163 * LUN is looked up by target-core internally based on the passed
2164 * abts->exchange_addr_to_abort tag.
2166 mcmd->se_cmd.cpuid = h->cpuid;
2168 abort_cmd = ha->tgt.tgt_ops->find_cmd_by_tag(sess,
2169 le32_to_cpu(abts->exchange_addr_to_abort));
2172 mcmd->unpacked_lun = abort_cmd->se_cmd.orig_fe_lun;
2174 if (abort_cmd->qpair) {
2175 mcmd->qpair = abort_cmd->qpair;
2176 mcmd->se_cmd.cpuid = abort_cmd->se_cmd.cpuid;
2177 mcmd->abort_io_attr = abort_cmd->atio.u.isp24.attr;
2178 mcmd->flags = QLA24XX_MGMT_ABORT_IO_ATTR_VALID;
2181 INIT_WORK(&mcmd->work, qlt_do_tmr_work);
2182 queue_work_on(mcmd->se_cmd.cpuid, qla_tgt_wq, &mcmd->work);
2188 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
2190 static void qlt_24xx_handle_abts(struct scsi_qla_host *vha,
2191 struct abts_recv_from_24xx *abts)
2193 struct qla_hw_data *ha = vha->hw;
2194 struct fc_port *sess;
2195 uint32_t tag = le32_to_cpu(abts->exchange_addr_to_abort);
2198 unsigned long flags;
2200 if (le32_to_cpu(abts->fcp_hdr_le.parameter) & ABTS_PARAM_ABORT_SEQ) {
2201 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf053,
2202 "qla_target(%d): ABTS: Abort Sequence not "
2203 "supported\n", vha->vp_idx);
2204 qlt_24xx_send_abts_resp(ha->base_qpair, abts, FCP_TMF_REJECTED,
2209 if (tag == ATIO_EXCHANGE_ADDRESS_UNKNOWN) {
2210 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf010,
2211 "qla_target(%d): ABTS: Unknown Exchange "
2212 "Address received\n", vha->vp_idx);
2213 qlt_24xx_send_abts_resp(ha->base_qpair, abts, FCP_TMF_REJECTED,
2218 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf011,
2219 "qla_target(%d): task abort (s_id=%x:%x:%x, "
2220 "tag=%d, param=%x)\n", vha->vp_idx, abts->fcp_hdr_le.s_id.domain,
2221 abts->fcp_hdr_le.s_id.area, abts->fcp_hdr_le.s_id.al_pa, tag,
2222 le32_to_cpu(abts->fcp_hdr_le.parameter));
2224 s_id = le_id_to_be(abts->fcp_hdr_le.s_id);
2226 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
2227 sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha, s_id);
2229 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf012,
2230 "qla_target(%d): task abort for non-existent session\n",
2232 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
2234 qlt_24xx_send_abts_resp(ha->base_qpair, abts, FCP_TMF_REJECTED,
2238 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
2241 if (sess->deleted) {
2242 qlt_24xx_send_abts_resp(ha->base_qpair, abts, FCP_TMF_REJECTED,
2247 rc = __qlt_24xx_handle_abts(vha, abts, sess);
2249 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf054,
2250 "qla_target(%d): __qlt_24xx_handle_abts() failed: %d\n",
2252 qlt_24xx_send_abts_resp(ha->base_qpair, abts, FCP_TMF_REJECTED,
2259 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
2261 static void qlt_24xx_send_task_mgmt_ctio(struct qla_qpair *qpair,
2262 struct qla_tgt_mgmt_cmd *mcmd, uint32_t resp_code)
2264 struct scsi_qla_host *ha = mcmd->vha;
2265 struct atio_from_isp *atio = &mcmd->orig_iocb.atio;
2266 struct ctio7_to_24xx *ctio;
2269 ql_dbg(ql_dbg_tgt, ha, 0xe008,
2270 "Sending task mgmt CTIO7 (ha=%p, atio=%p, resp_code=%x\n",
2271 ha, atio, resp_code);
2274 ctio = (struct ctio7_to_24xx *)__qla2x00_alloc_iocbs(qpair, NULL);
2276 ql_dbg(ql_dbg_tgt, ha, 0xe04c,
2277 "qla_target(%d): %s failed: unable to allocate "
2278 "request packet\n", ha->vp_idx, __func__);
2282 ctio->entry_type = CTIO_TYPE7;
2283 ctio->entry_count = 1;
2284 ctio->handle = QLA_TGT_SKIP_HANDLE | CTIO_COMPLETION_HANDLE_MARK;
2285 ctio->nport_handle = cpu_to_le16(mcmd->sess->loop_id);
2286 ctio->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
2287 ctio->vp_index = ha->vp_idx;
2288 ctio->initiator_id = be_id_to_le(atio->u.isp24.fcp_hdr.s_id);
2289 ctio->exchange_addr = atio->u.isp24.exchange_addr;
2290 temp = (atio->u.isp24.attr << 9)|
2291 CTIO7_FLAGS_STATUS_MODE_1 | CTIO7_FLAGS_SEND_STATUS;
2292 ctio->u.status1.flags = cpu_to_le16(temp);
2293 temp = be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id);
2294 ctio->u.status1.ox_id = cpu_to_le16(temp);
2295 ctio->u.status1.scsi_status =
2296 cpu_to_le16(SS_RESPONSE_INFO_LEN_VALID);
2297 ctio->u.status1.response_len = cpu_to_le16(8);
2298 ctio->u.status1.sense_data[0] = resp_code;
2300 /* Memory Barrier */
2302 if (qpair->reqq_start_iocbs)
2303 qpair->reqq_start_iocbs(qpair);
2305 qla2x00_start_iocbs(ha, qpair->req);
2308 void qlt_free_mcmd(struct qla_tgt_mgmt_cmd *mcmd)
2310 mempool_free(mcmd, qla_tgt_mgmt_cmd_mempool);
2312 EXPORT_SYMBOL(qlt_free_mcmd);
2315 * ha->hardware_lock supposed to be held on entry. Might drop it, then
2318 void qlt_send_resp_ctio(struct qla_qpair *qpair, struct qla_tgt_cmd *cmd,
2319 uint8_t scsi_status, uint8_t sense_key, uint8_t asc, uint8_t ascq)
2321 struct atio_from_isp *atio = &cmd->atio;
2322 struct ctio7_to_24xx *ctio;
2324 struct scsi_qla_host *vha = cmd->vha;
2326 ql_dbg(ql_dbg_tgt_dif, vha, 0x3066,
2327 "Sending response CTIO7 (vha=%p, atio=%p, scsi_status=%02x, "
2328 "sense_key=%02x, asc=%02x, ascq=%02x",
2329 vha, atio, scsi_status, sense_key, asc, ascq);
2331 ctio = (struct ctio7_to_24xx *)qla2x00_alloc_iocbs(vha, NULL);
2333 ql_dbg(ql_dbg_async, vha, 0x3067,
2334 "qla2x00t(%ld): %s failed: unable to allocate request packet",
2335 vha->host_no, __func__);
2339 ctio->entry_type = CTIO_TYPE7;
2340 ctio->entry_count = 1;
2341 ctio->handle = QLA_TGT_SKIP_HANDLE;
2342 ctio->nport_handle = cpu_to_le16(cmd->sess->loop_id);
2343 ctio->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
2344 ctio->vp_index = vha->vp_idx;
2345 ctio->initiator_id = be_id_to_le(atio->u.isp24.fcp_hdr.s_id);
2346 ctio->exchange_addr = atio->u.isp24.exchange_addr;
2347 temp = (atio->u.isp24.attr << 9) |
2348 CTIO7_FLAGS_STATUS_MODE_1 | CTIO7_FLAGS_SEND_STATUS;
2349 ctio->u.status1.flags = cpu_to_le16(temp);
2350 temp = be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id);
2351 ctio->u.status1.ox_id = cpu_to_le16(temp);
2352 ctio->u.status1.scsi_status =
2353 cpu_to_le16(SS_RESPONSE_INFO_LEN_VALID | scsi_status);
2354 ctio->u.status1.response_len = cpu_to_le16(18);
2355 ctio->u.status1.residual = cpu_to_le32(get_datalen_for_atio(atio));
2357 if (ctio->u.status1.residual != 0)
2358 ctio->u.status1.scsi_status |=
2359 cpu_to_le16(SS_RESIDUAL_UNDER);
2361 /* Fixed format sense data. */
2362 ctio->u.status1.sense_data[0] = 0x70;
2363 ctio->u.status1.sense_data[2] = sense_key;
2364 /* Additional sense length */
2365 ctio->u.status1.sense_data[7] = 0xa;
2367 ctio->u.status1.sense_data[12] = asc;
2368 ctio->u.status1.sense_data[13] = ascq;
2370 /* Memory Barrier */
2373 if (qpair->reqq_start_iocbs)
2374 qpair->reqq_start_iocbs(qpair);
2376 qla2x00_start_iocbs(vha, qpair->req);
2382 /* callback from target fabric module code */
2383 void qlt_xmit_tm_rsp(struct qla_tgt_mgmt_cmd *mcmd)
2385 struct scsi_qla_host *vha = mcmd->sess->vha;
2386 struct qla_hw_data *ha = vha->hw;
2387 unsigned long flags;
2388 struct qla_qpair *qpair = mcmd->qpair;
2389 bool free_mcmd = true;
2391 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf013,
2392 "TM response mcmd (%p) status %#x state %#x",
2393 mcmd, mcmd->fc_tm_rsp, mcmd->flags);
2395 spin_lock_irqsave(qpair->qp_lock_ptr, flags);
2397 if (!vha->flags.online || mcmd->reset_count != qpair->chip_reset) {
2399 * Either the port is not online or this request was from
2400 * previous life, just abort the processing.
2402 ql_dbg(ql_dbg_async, vha, 0xe100,
2403 "RESET-TMR online/active/old-count/new-count = %d/%d/%d/%d.\n",
2404 vha->flags.online, qla2x00_reset_active(vha),
2405 mcmd->reset_count, qpair->chip_reset);
2406 ha->tgt.tgt_ops->free_mcmd(mcmd);
2407 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
2411 if (mcmd->flags == QLA24XX_MGMT_SEND_NACK) {
2412 switch (mcmd->orig_iocb.imm_ntfy.u.isp24.status_subcode) {
2416 ql_dbg(ql_dbg_disc, vha, 0x2106,
2417 "TM response logo %8phC status %#x state %#x",
2418 mcmd->sess->port_name, mcmd->fc_tm_rsp,
2420 qlt_schedule_sess_for_deletion(mcmd->sess);
2423 qlt_send_notify_ack(vha->hw->base_qpair,
2424 &mcmd->orig_iocb.imm_ntfy, 0, 0, 0, 0, 0, 0);
2428 if (mcmd->orig_iocb.atio.u.raw.entry_type == ABTS_RECV_24XX) {
2429 qlt_build_abts_resp_iocb(mcmd);
2432 qlt_24xx_send_task_mgmt_ctio(qpair, mcmd,
2436 * Make the callback for ->free_mcmd() to queue_work() and invoke
2437 * target_put_sess_cmd() to drop cmd_kref to 1. The final
2438 * target_put_sess_cmd() call will be made from TFO->check_stop_free()
2439 * -> tcm_qla2xxx_check_stop_free() to release the TMR associated se_cmd
2440 * descriptor after TFO->queue_tm_rsp() -> tcm_qla2xxx_queue_tm_rsp() ->
2441 * qlt_xmit_tm_rsp() returns here..
2444 ha->tgt.tgt_ops->free_mcmd(mcmd);
2446 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
2448 EXPORT_SYMBOL(qlt_xmit_tm_rsp);
2451 static int qlt_pci_map_calc_cnt(struct qla_tgt_prm *prm)
2453 struct qla_tgt_cmd *cmd = prm->cmd;
2455 BUG_ON(cmd->sg_cnt == 0);
2457 prm->sg = (struct scatterlist *)cmd->sg;
2458 prm->seg_cnt = dma_map_sg(&cmd->qpair->pdev->dev, cmd->sg,
2459 cmd->sg_cnt, cmd->dma_data_direction);
2460 if (unlikely(prm->seg_cnt == 0))
2463 prm->cmd->sg_mapped = 1;
2465 if (cmd->se_cmd.prot_op == TARGET_PROT_NORMAL) {
2467 * If greater than four sg entries then we need to allocate
2468 * the continuation entries
2470 if (prm->seg_cnt > QLA_TGT_DATASEGS_PER_CMD_24XX)
2471 prm->req_cnt += DIV_ROUND_UP(prm->seg_cnt -
2472 QLA_TGT_DATASEGS_PER_CMD_24XX,
2473 QLA_TGT_DATASEGS_PER_CONT_24XX);
2476 if ((cmd->se_cmd.prot_op == TARGET_PROT_DIN_INSERT) ||
2477 (cmd->se_cmd.prot_op == TARGET_PROT_DOUT_STRIP)) {
2478 prm->seg_cnt = DIV_ROUND_UP(cmd->bufflen, cmd->blk_sz);
2479 prm->tot_dsds = prm->seg_cnt;
2481 prm->tot_dsds = prm->seg_cnt;
2483 if (cmd->prot_sg_cnt) {
2484 prm->prot_sg = cmd->prot_sg;
2485 prm->prot_seg_cnt = dma_map_sg(&cmd->qpair->pdev->dev,
2486 cmd->prot_sg, cmd->prot_sg_cnt,
2487 cmd->dma_data_direction);
2488 if (unlikely(prm->prot_seg_cnt == 0))
2491 if ((cmd->se_cmd.prot_op == TARGET_PROT_DIN_INSERT) ||
2492 (cmd->se_cmd.prot_op == TARGET_PROT_DOUT_STRIP)) {
2493 /* Dif Bundling not support here */
2494 prm->prot_seg_cnt = DIV_ROUND_UP(cmd->bufflen,
2496 prm->tot_dsds += prm->prot_seg_cnt;
2498 prm->tot_dsds += prm->prot_seg_cnt;
2505 ql_dbg_qp(ql_dbg_tgt, prm->cmd->qpair, 0xe04d,
2506 "qla_target(%d): PCI mapping failed: sg_cnt=%d",
2507 0, prm->cmd->sg_cnt);
2511 static void qlt_unmap_sg(struct scsi_qla_host *vha, struct qla_tgt_cmd *cmd)
2513 struct qla_hw_data *ha;
2514 struct qla_qpair *qpair;
2516 if (!cmd->sg_mapped)
2521 dma_unmap_sg(&qpair->pdev->dev, cmd->sg, cmd->sg_cnt,
2522 cmd->dma_data_direction);
2525 if (cmd->prot_sg_cnt)
2526 dma_unmap_sg(&qpair->pdev->dev, cmd->prot_sg, cmd->prot_sg_cnt,
2527 cmd->dma_data_direction);
2532 if (cmd->ctx_dsd_alloced)
2533 qla2x00_clean_dsd_pool(ha, cmd->ctx);
2535 dma_pool_free(ha->dl_dma_pool, cmd->ctx, cmd->ctx->crc_ctx_dma);
2538 static int qlt_check_reserve_free_req(struct qla_qpair *qpair,
2542 struct req_que *req = qpair->req;
2544 if (req->cnt < (req_cnt + 2)) {
2545 cnt = (uint16_t)(qpair->use_shadow_reg ? *req->out_ptr :
2546 rd_reg_dword_relaxed(req->req_q_out));
2548 if (req->ring_index < cnt)
2549 req->cnt = cnt - req->ring_index;
2551 req->cnt = req->length - (req->ring_index - cnt);
2553 if (unlikely(req->cnt < (req_cnt + 2)))
2557 req->cnt -= req_cnt;
2563 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
2565 static inline void *qlt_get_req_pkt(struct req_que *req)
2567 /* Adjust ring index. */
2569 if (req->ring_index == req->length) {
2570 req->ring_index = 0;
2571 req->ring_ptr = req->ring;
2575 return (cont_entry_t *)req->ring_ptr;
2578 /* ha->hardware_lock supposed to be held on entry */
2579 static inline uint32_t qlt_make_handle(struct qla_qpair *qpair)
2584 struct req_que *req = qpair->req;
2586 h = req->current_outstanding_cmd;
2588 for (index = 1; index < req->num_outstanding_cmds; index++) {
2590 if (h == req->num_outstanding_cmds)
2593 if (h == QLA_TGT_SKIP_HANDLE)
2596 if (!req->outstanding_cmds[h]) {
2603 req->current_outstanding_cmd = h;
2605 ql_dbg(ql_dbg_io, qpair->vha, 0x305b,
2606 "qla_target(%d): Ran out of empty cmd slots\n",
2607 qpair->vha->vp_idx);
2608 h = QLA_TGT_NULL_HANDLE;
2614 /* ha->hardware_lock supposed to be held on entry */
2615 static int qlt_24xx_build_ctio_pkt(struct qla_qpair *qpair,
2616 struct qla_tgt_prm *prm)
2619 struct ctio7_to_24xx *pkt;
2620 struct atio_from_isp *atio = &prm->cmd->atio;
2622 struct qla_tgt_cmd *cmd = prm->cmd;
2624 pkt = (struct ctio7_to_24xx *)qpair->req->ring_ptr;
2626 memset(pkt, 0, sizeof(*pkt));
2628 pkt->entry_type = CTIO_TYPE7;
2629 pkt->entry_count = (uint8_t)prm->req_cnt;
2630 pkt->vp_index = prm->cmd->vp_idx;
2632 h = qlt_make_handle(qpair);
2633 if (unlikely(h == QLA_TGT_NULL_HANDLE)) {
2635 * CTIO type 7 from the firmware doesn't provide a way to
2636 * know the initiator's LOOP ID, hence we can't find
2637 * the session and, so, the command.
2641 qpair->req->outstanding_cmds[h] = (srb_t *)prm->cmd;
2643 pkt->handle = make_handle(qpair->req->id, h);
2644 pkt->handle |= CTIO_COMPLETION_HANDLE_MARK;
2645 pkt->nport_handle = cpu_to_le16(prm->cmd->loop_id);
2646 pkt->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
2647 pkt->initiator_id = be_id_to_le(atio->u.isp24.fcp_hdr.s_id);
2648 pkt->exchange_addr = atio->u.isp24.exchange_addr;
2649 temp = atio->u.isp24.attr << 9;
2650 pkt->u.status0.flags |= cpu_to_le16(temp);
2651 temp = be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id);
2652 pkt->u.status0.ox_id = cpu_to_le16(temp);
2653 pkt->u.status0.relative_offset = cpu_to_le32(prm->cmd->offset);
2656 if (cmd->dma_data_direction == DMA_TO_DEVICE)
2657 prm->cmd->sess->edif.rx_bytes += cmd->bufflen;
2658 if (cmd->dma_data_direction == DMA_FROM_DEVICE)
2659 prm->cmd->sess->edif.tx_bytes += cmd->bufflen;
2661 pkt->u.status0.edif_flags |= EF_EN_EDIF;
2668 * ha->hardware_lock supposed to be held on entry. We have already made sure
2669 * that there is sufficient amount of request entries to not drop it.
2671 static void qlt_load_cont_data_segments(struct qla_tgt_prm *prm)
2674 struct dsd64 *cur_dsd;
2676 /* Build continuation packets */
2677 while (prm->seg_cnt > 0) {
2678 cont_a64_entry_t *cont_pkt64 =
2679 (cont_a64_entry_t *)qlt_get_req_pkt(
2680 prm->cmd->qpair->req);
2683 * Make sure that from cont_pkt64 none of
2684 * 64-bit specific fields used for 32-bit
2685 * addressing. Cast to (cont_entry_t *) for
2689 memset(cont_pkt64, 0, sizeof(*cont_pkt64));
2691 cont_pkt64->entry_count = 1;
2692 cont_pkt64->sys_define = 0;
2694 cont_pkt64->entry_type = CONTINUE_A64_TYPE;
2695 cur_dsd = cont_pkt64->dsd;
2697 /* Load continuation entry data segments */
2699 cnt < QLA_TGT_DATASEGS_PER_CONT_24XX && prm->seg_cnt;
2700 cnt++, prm->seg_cnt--) {
2701 append_dsd64(&cur_dsd, prm->sg);
2702 prm->sg = sg_next(prm->sg);
2708 * ha->hardware_lock supposed to be held on entry. We have already made sure
2709 * that there is sufficient amount of request entries to not drop it.
2711 static void qlt_load_data_segments(struct qla_tgt_prm *prm)
2714 struct dsd64 *cur_dsd;
2715 struct ctio7_to_24xx *pkt24 = (struct ctio7_to_24xx *)prm->pkt;
2717 pkt24->u.status0.transfer_length = cpu_to_le32(prm->cmd->bufflen);
2719 /* Setup packet address segment pointer */
2720 cur_dsd = &pkt24->u.status0.dsd;
2722 /* Set total data segment count */
2724 pkt24->dseg_count = cpu_to_le16(prm->seg_cnt);
2726 if (prm->seg_cnt == 0) {
2727 /* No data transfer */
2728 cur_dsd->address = 0;
2729 cur_dsd->length = 0;
2733 /* If scatter gather */
2735 /* Load command entry data segments */
2737 (cnt < QLA_TGT_DATASEGS_PER_CMD_24XX) && prm->seg_cnt;
2738 cnt++, prm->seg_cnt--) {
2739 append_dsd64(&cur_dsd, prm->sg);
2740 prm->sg = sg_next(prm->sg);
2743 qlt_load_cont_data_segments(prm);
2746 static inline int qlt_has_data(struct qla_tgt_cmd *cmd)
2748 return cmd->bufflen > 0;
2751 static void qlt_print_dif_err(struct qla_tgt_prm *prm)
2753 struct qla_tgt_cmd *cmd;
2754 struct scsi_qla_host *vha;
2756 /* asc 0x10=dif error */
2757 if (prm->sense_buffer && (prm->sense_buffer[12] == 0x10)) {
2761 switch (prm->sense_buffer[13]) {
2763 ql_dbg(ql_dbg_tgt_dif, vha, 0xe00b,
2764 "BE detected Guard TAG ERR: lba[0x%llx|%lld] len[0x%x] "
2765 "se_cmd=%p tag[%x]",
2766 cmd->lba, cmd->lba, cmd->num_blks, &cmd->se_cmd,
2767 cmd->atio.u.isp24.exchange_addr);
2770 ql_dbg(ql_dbg_tgt_dif, vha, 0xe00c,
2771 "BE detected APP TAG ERR: lba[0x%llx|%lld] len[0x%x] "
2772 "se_cmd=%p tag[%x]",
2773 cmd->lba, cmd->lba, cmd->num_blks, &cmd->se_cmd,
2774 cmd->atio.u.isp24.exchange_addr);
2777 ql_dbg(ql_dbg_tgt_dif, vha, 0xe00f,
2778 "BE detected REF TAG ERR: lba[0x%llx|%lld] len[0x%x] "
2779 "se_cmd=%p tag[%x]",
2780 cmd->lba, cmd->lba, cmd->num_blks, &cmd->se_cmd,
2781 cmd->atio.u.isp24.exchange_addr);
2784 ql_dbg(ql_dbg_tgt_dif, vha, 0xe010,
2785 "BE detected Dif ERR: lba[%llx|%lld] len[%x] "
2786 "se_cmd=%p tag[%x]",
2787 cmd->lba, cmd->lba, cmd->num_blks, &cmd->se_cmd,
2788 cmd->atio.u.isp24.exchange_addr);
2791 ql_dump_buffer(ql_dbg_tgt_dif, vha, 0xe011, cmd->cdb, 16);
2796 * Called without ha->hardware_lock held
2798 static int qlt_pre_xmit_response(struct qla_tgt_cmd *cmd,
2799 struct qla_tgt_prm *prm, int xmit_type, uint8_t scsi_status,
2800 uint32_t *full_req_cnt)
2802 struct se_cmd *se_cmd = &cmd->se_cmd;
2803 struct qla_qpair *qpair = cmd->qpair;
2806 prm->tgt = cmd->tgt;
2808 prm->rq_result = scsi_status;
2809 prm->sense_buffer = &cmd->sense_buffer[0];
2810 prm->sense_buffer_len = TRANSPORT_SENSE_BUFFER;
2815 prm->add_status_pkt = 0;
2816 prm->prot_sg = NULL;
2817 prm->prot_seg_cnt = 0;
2820 if ((xmit_type & QLA_TGT_XMIT_DATA) && qlt_has_data(cmd)) {
2821 if (qlt_pci_map_calc_cnt(prm) != 0)
2825 *full_req_cnt = prm->req_cnt;
2827 if (se_cmd->se_cmd_flags & SCF_UNDERFLOW_BIT) {
2828 prm->residual = se_cmd->residual_count;
2829 ql_dbg_qp(ql_dbg_io + ql_dbg_verbose, qpair, 0x305c,
2830 "Residual underflow: %d (tag %lld, op %x, bufflen %d, rq_result %x)\n",
2831 prm->residual, se_cmd->tag,
2832 se_cmd->t_task_cdb ? se_cmd->t_task_cdb[0] : 0,
2833 cmd->bufflen, prm->rq_result);
2834 prm->rq_result |= SS_RESIDUAL_UNDER;
2835 } else if (se_cmd->se_cmd_flags & SCF_OVERFLOW_BIT) {
2836 prm->residual = se_cmd->residual_count;
2837 ql_dbg_qp(ql_dbg_io, qpair, 0x305d,
2838 "Residual overflow: %d (tag %lld, op %x, bufflen %d, rq_result %x)\n",
2839 prm->residual, se_cmd->tag, se_cmd->t_task_cdb ?
2840 se_cmd->t_task_cdb[0] : 0, cmd->bufflen, prm->rq_result);
2841 prm->rq_result |= SS_RESIDUAL_OVER;
2844 if (xmit_type & QLA_TGT_XMIT_STATUS) {
2846 * If QLA_TGT_XMIT_DATA is not set, add_status_pkt will be
2847 * ignored in *xmit_response() below
2849 if (qlt_has_data(cmd)) {
2850 if (QLA_TGT_SENSE_VALID(prm->sense_buffer) ||
2851 (IS_FWI2_CAPABLE(cmd->vha->hw) &&
2852 (prm->rq_result != 0))) {
2853 prm->add_status_pkt = 1;
2862 static inline int qlt_need_explicit_conf(struct qla_tgt_cmd *cmd,
2865 if (cmd->qpair->enable_class_2)
2869 return cmd->conf_compl_supported;
2871 return cmd->qpair->enable_explicit_conf &&
2872 cmd->conf_compl_supported;
2875 static void qlt_24xx_init_ctio_to_isp(struct ctio7_to_24xx *ctio,
2876 struct qla_tgt_prm *prm)
2878 prm->sense_buffer_len = min_t(uint32_t, prm->sense_buffer_len,
2879 (uint32_t)sizeof(ctio->u.status1.sense_data));
2880 ctio->u.status0.flags |= cpu_to_le16(CTIO7_FLAGS_SEND_STATUS);
2881 if (qlt_need_explicit_conf(prm->cmd, 0)) {
2882 ctio->u.status0.flags |= cpu_to_le16(
2883 CTIO7_FLAGS_EXPLICIT_CONFORM |
2884 CTIO7_FLAGS_CONFORM_REQ);
2886 ctio->u.status0.residual = cpu_to_le32(prm->residual);
2887 ctio->u.status0.scsi_status = cpu_to_le16(prm->rq_result);
2888 if (QLA_TGT_SENSE_VALID(prm->sense_buffer)) {
2891 if (qlt_need_explicit_conf(prm->cmd, 1)) {
2892 if ((prm->rq_result & SS_SCSI_STATUS_BYTE) != 0) {
2893 ql_dbg_qp(ql_dbg_tgt, prm->cmd->qpair, 0xe017,
2894 "Skipping EXPLICIT_CONFORM and "
2895 "CTIO7_FLAGS_CONFORM_REQ for FCP READ w/ "
2896 "non GOOD status\n");
2897 goto skip_explict_conf;
2899 ctio->u.status1.flags |= cpu_to_le16(
2900 CTIO7_FLAGS_EXPLICIT_CONFORM |
2901 CTIO7_FLAGS_CONFORM_REQ);
2904 ctio->u.status1.flags &=
2905 ~cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_0);
2906 ctio->u.status1.flags |=
2907 cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_1);
2908 ctio->u.status1.scsi_status |=
2909 cpu_to_le16(SS_SENSE_LEN_VALID);
2910 ctio->u.status1.sense_length =
2911 cpu_to_le16(prm->sense_buffer_len);
2912 for (i = 0; i < prm->sense_buffer_len/4; i++) {
2915 v = get_unaligned_be32(
2916 &((uint32_t *)prm->sense_buffer)[i]);
2917 put_unaligned_le32(v,
2918 &((uint32_t *)ctio->u.status1.sense_data)[i]);
2920 qlt_print_dif_err(prm);
2923 ctio->u.status1.flags &=
2924 ~cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_0);
2925 ctio->u.status1.flags |=
2926 cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_1);
2927 ctio->u.status1.sense_length = 0;
2928 memset(ctio->u.status1.sense_data, 0,
2929 sizeof(ctio->u.status1.sense_data));
2932 /* Sense with len > 24, is it possible ??? */
2936 qlt_hba_err_chk_enabled(struct se_cmd *se_cmd)
2938 switch (se_cmd->prot_op) {
2939 case TARGET_PROT_DOUT_INSERT:
2940 case TARGET_PROT_DIN_STRIP:
2941 if (ql2xenablehba_err_chk >= 1)
2944 case TARGET_PROT_DOUT_PASS:
2945 case TARGET_PROT_DIN_PASS:
2946 if (ql2xenablehba_err_chk >= 2)
2949 case TARGET_PROT_DIN_INSERT:
2950 case TARGET_PROT_DOUT_STRIP:
2959 qla_tgt_ref_mask_check(struct se_cmd *se_cmd)
2961 switch (se_cmd->prot_op) {
2962 case TARGET_PROT_DIN_INSERT:
2963 case TARGET_PROT_DOUT_INSERT:
2964 case TARGET_PROT_DIN_STRIP:
2965 case TARGET_PROT_DOUT_STRIP:
2966 case TARGET_PROT_DIN_PASS:
2967 case TARGET_PROT_DOUT_PASS:
2976 * qla_tgt_set_dif_tags - Extract Ref and App tags from SCSI command
2979 qla_tgt_set_dif_tags(struct qla_tgt_cmd *cmd, struct crc_context *ctx,
2980 uint16_t *pfw_prot_opts)
2982 struct se_cmd *se_cmd = &cmd->se_cmd;
2983 uint32_t lba = 0xffffffff & se_cmd->t_task_lba;
2984 scsi_qla_host_t *vha = cmd->tgt->vha;
2985 struct qla_hw_data *ha = vha->hw;
2989 * wait till Mode Sense/Select cmd, modepage Ah, subpage 2
2990 * have been immplemented by TCM, before AppTag is avail.
2991 * Look for modesense_handlers[]
2994 ctx->app_tag_mask[0] = 0x0;
2995 ctx->app_tag_mask[1] = 0x0;
2997 if (IS_PI_UNINIT_CAPABLE(ha)) {
2998 if ((se_cmd->prot_type == TARGET_DIF_TYPE1_PROT) ||
2999 (se_cmd->prot_type == TARGET_DIF_TYPE2_PROT))
3000 *pfw_prot_opts |= PO_DIS_VALD_APP_ESC;
3001 else if (se_cmd->prot_type == TARGET_DIF_TYPE3_PROT)
3002 *pfw_prot_opts |= PO_DIS_VALD_APP_REF_ESC;
3005 t32 = ha->tgt.tgt_ops->get_dif_tags(cmd, pfw_prot_opts);
3007 switch (se_cmd->prot_type) {
3008 case TARGET_DIF_TYPE0_PROT:
3010 * No check for ql2xenablehba_err_chk, as it
3011 * would be an I/O error if hba tag generation
3014 ctx->ref_tag = cpu_to_le32(lba);
3015 /* enable ALL bytes of the ref tag */
3016 ctx->ref_tag_mask[0] = 0xff;
3017 ctx->ref_tag_mask[1] = 0xff;
3018 ctx->ref_tag_mask[2] = 0xff;
3019 ctx->ref_tag_mask[3] = 0xff;
3021 case TARGET_DIF_TYPE1_PROT:
3023 * For TYPE 1 protection: 16 bit GUARD tag, 32 bit
3024 * REF tag, and 16 bit app tag.
3026 ctx->ref_tag = cpu_to_le32(lba);
3027 if (!qla_tgt_ref_mask_check(se_cmd) ||
3028 !(ha->tgt.tgt_ops->chk_dif_tags(t32))) {
3029 *pfw_prot_opts |= PO_DIS_REF_TAG_VALD;
3032 /* enable ALL bytes of the ref tag */
3033 ctx->ref_tag_mask[0] = 0xff;
3034 ctx->ref_tag_mask[1] = 0xff;
3035 ctx->ref_tag_mask[2] = 0xff;
3036 ctx->ref_tag_mask[3] = 0xff;
3038 case TARGET_DIF_TYPE2_PROT:
3040 * For TYPE 2 protection: 16 bit GUARD + 32 bit REF
3041 * tag has to match LBA in CDB + N
3043 ctx->ref_tag = cpu_to_le32(lba);
3044 if (!qla_tgt_ref_mask_check(se_cmd) ||
3045 !(ha->tgt.tgt_ops->chk_dif_tags(t32))) {
3046 *pfw_prot_opts |= PO_DIS_REF_TAG_VALD;
3049 /* enable ALL bytes of the ref tag */
3050 ctx->ref_tag_mask[0] = 0xff;
3051 ctx->ref_tag_mask[1] = 0xff;
3052 ctx->ref_tag_mask[2] = 0xff;
3053 ctx->ref_tag_mask[3] = 0xff;
3055 case TARGET_DIF_TYPE3_PROT:
3056 /* For TYPE 3 protection: 16 bit GUARD only */
3057 *pfw_prot_opts |= PO_DIS_REF_TAG_VALD;
3058 ctx->ref_tag_mask[0] = ctx->ref_tag_mask[1] =
3059 ctx->ref_tag_mask[2] = ctx->ref_tag_mask[3] = 0x00;
3065 qlt_build_ctio_crc2_pkt(struct qla_qpair *qpair, struct qla_tgt_prm *prm)
3067 struct dsd64 *cur_dsd;
3068 uint32_t transfer_length = 0;
3069 uint32_t data_bytes;
3071 uint8_t bundling = 1;
3072 struct crc_context *crc_ctx_pkt = NULL;
3073 struct qla_hw_data *ha;
3074 struct ctio_crc2_to_fw *pkt;
3075 dma_addr_t crc_ctx_dma;
3076 uint16_t fw_prot_opts = 0;
3077 struct qla_tgt_cmd *cmd = prm->cmd;
3078 struct se_cmd *se_cmd = &cmd->se_cmd;
3080 struct atio_from_isp *atio = &prm->cmd->atio;
3081 struct qla_tc_param tc;
3083 scsi_qla_host_t *vha = cmd->vha;
3087 pkt = (struct ctio_crc2_to_fw *)qpair->req->ring_ptr;
3089 memset(pkt, 0, sizeof(*pkt));
3091 ql_dbg_qp(ql_dbg_tgt, cmd->qpair, 0xe071,
3092 "qla_target(%d):%s: se_cmd[%p] CRC2 prot_op[0x%x] cmd prot sg:cnt[%p:%x] lba[%llu]\n",
3093 cmd->vp_idx, __func__, se_cmd, se_cmd->prot_op,
3094 prm->prot_sg, prm->prot_seg_cnt, se_cmd->t_task_lba);
3096 if ((se_cmd->prot_op == TARGET_PROT_DIN_INSERT) ||
3097 (se_cmd->prot_op == TARGET_PROT_DOUT_STRIP))
3100 /* Compute dif len and adjust data len to incude protection */
3101 data_bytes = cmd->bufflen;
3102 dif_bytes = (data_bytes / cmd->blk_sz) * 8;
3104 switch (se_cmd->prot_op) {
3105 case TARGET_PROT_DIN_INSERT:
3106 case TARGET_PROT_DOUT_STRIP:
3107 transfer_length = data_bytes;
3108 if (cmd->prot_sg_cnt)
3109 data_bytes += dif_bytes;
3111 case TARGET_PROT_DIN_STRIP:
3112 case TARGET_PROT_DOUT_INSERT:
3113 case TARGET_PROT_DIN_PASS:
3114 case TARGET_PROT_DOUT_PASS:
3115 transfer_length = data_bytes + dif_bytes;
3122 if (!qlt_hba_err_chk_enabled(se_cmd))
3123 fw_prot_opts |= 0x10; /* Disable Guard tag checking */
3124 /* HBA error checking enabled */
3125 else if (IS_PI_UNINIT_CAPABLE(ha)) {
3126 if ((se_cmd->prot_type == TARGET_DIF_TYPE1_PROT) ||
3127 (se_cmd->prot_type == TARGET_DIF_TYPE2_PROT))
3128 fw_prot_opts |= PO_DIS_VALD_APP_ESC;
3129 else if (se_cmd->prot_type == TARGET_DIF_TYPE3_PROT)
3130 fw_prot_opts |= PO_DIS_VALD_APP_REF_ESC;
3133 switch (se_cmd->prot_op) {
3134 case TARGET_PROT_DIN_INSERT:
3135 case TARGET_PROT_DOUT_INSERT:
3136 fw_prot_opts |= PO_MODE_DIF_INSERT;
3138 case TARGET_PROT_DIN_STRIP:
3139 case TARGET_PROT_DOUT_STRIP:
3140 fw_prot_opts |= PO_MODE_DIF_REMOVE;
3142 case TARGET_PROT_DIN_PASS:
3143 case TARGET_PROT_DOUT_PASS:
3144 fw_prot_opts |= PO_MODE_DIF_PASS;
3145 /* FUTURE: does tcm require T10CRC<->IPCKSUM conversion? */
3147 default:/* Normal Request */
3148 fw_prot_opts |= PO_MODE_DIF_PASS;
3153 /* Update entry type to indicate Command Type CRC_2 IOCB */
3154 pkt->entry_type = CTIO_CRC2;
3155 pkt->entry_count = 1;
3156 pkt->vp_index = cmd->vp_idx;
3158 h = qlt_make_handle(qpair);
3159 if (unlikely(h == QLA_TGT_NULL_HANDLE)) {
3161 * CTIO type 7 from the firmware doesn't provide a way to
3162 * know the initiator's LOOP ID, hence we can't find
3163 * the session and, so, the command.
3167 qpair->req->outstanding_cmds[h] = (srb_t *)prm->cmd;
3169 pkt->handle = make_handle(qpair->req->id, h);
3170 pkt->handle |= CTIO_COMPLETION_HANDLE_MARK;
3171 pkt->nport_handle = cpu_to_le16(prm->cmd->loop_id);
3172 pkt->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
3173 pkt->initiator_id = be_id_to_le(atio->u.isp24.fcp_hdr.s_id);
3174 pkt->exchange_addr = atio->u.isp24.exchange_addr;
3176 /* silence compile warning */
3177 t16 = be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id);
3178 pkt->ox_id = cpu_to_le16(t16);
3180 t16 = (atio->u.isp24.attr << 9);
3181 pkt->flags |= cpu_to_le16(t16);
3182 pkt->relative_offset = cpu_to_le32(prm->cmd->offset);
3184 /* Set transfer direction */
3185 if (cmd->dma_data_direction == DMA_TO_DEVICE)
3186 pkt->flags = cpu_to_le16(CTIO7_FLAGS_DATA_IN);
3187 else if (cmd->dma_data_direction == DMA_FROM_DEVICE)
3188 pkt->flags = cpu_to_le16(CTIO7_FLAGS_DATA_OUT);
3190 pkt->dseg_count = cpu_to_le16(prm->tot_dsds);
3191 /* Fibre channel byte count */
3192 pkt->transfer_length = cpu_to_le32(transfer_length);
3194 /* ----- CRC context -------- */
3196 /* Allocate CRC context from global pool */
3197 crc_ctx_pkt = cmd->ctx =
3198 dma_pool_zalloc(ha->dl_dma_pool, GFP_ATOMIC, &crc_ctx_dma);
3201 goto crc_queuing_error;
3203 crc_ctx_pkt->crc_ctx_dma = crc_ctx_dma;
3204 INIT_LIST_HEAD(&crc_ctx_pkt->dsd_list);
3207 crc_ctx_pkt->handle = pkt->handle;
3209 qla_tgt_set_dif_tags(cmd, crc_ctx_pkt, &fw_prot_opts);
3211 put_unaligned_le64(crc_ctx_dma, &pkt->crc_context_address);
3212 pkt->crc_context_len = cpu_to_le16(CRC_CONTEXT_LEN_FW);
3215 cur_dsd = &crc_ctx_pkt->u.nobundling.data_dsd[0];
3218 * Configure Bundling if we need to fetch interlaving
3219 * protection PCI accesses
3221 fw_prot_opts |= PO_ENABLE_DIF_BUNDLING;
3222 crc_ctx_pkt->u.bundling.dif_byte_count = cpu_to_le32(dif_bytes);
3223 crc_ctx_pkt->u.bundling.dseg_count =
3224 cpu_to_le16(prm->tot_dsds - prm->prot_seg_cnt);
3225 cur_dsd = &crc_ctx_pkt->u.bundling.data_dsd[0];
3228 /* Finish the common fields of CRC pkt */
3229 crc_ctx_pkt->blk_size = cpu_to_le16(cmd->blk_sz);
3230 crc_ctx_pkt->prot_opts = cpu_to_le16(fw_prot_opts);
3231 crc_ctx_pkt->byte_count = cpu_to_le32(data_bytes);
3232 crc_ctx_pkt->guard_seed = cpu_to_le16(0);
3234 memset((uint8_t *)&tc, 0 , sizeof(tc));
3236 tc.blk_sz = cmd->blk_sz;
3237 tc.bufflen = cmd->bufflen;
3239 tc.prot_sg = cmd->prot_sg;
3240 tc.ctx = crc_ctx_pkt;
3241 tc.ctx_dsd_alloced = &cmd->ctx_dsd_alloced;
3243 /* Walks data segments */
3244 pkt->flags |= cpu_to_le16(CTIO7_FLAGS_DSD_PTR);
3246 if (!bundling && prm->prot_seg_cnt) {
3247 if (qla24xx_walk_and_build_sglist_no_difb(ha, NULL, cur_dsd,
3248 prm->tot_dsds, &tc))
3249 goto crc_queuing_error;
3250 } else if (qla24xx_walk_and_build_sglist(ha, NULL, cur_dsd,
3251 (prm->tot_dsds - prm->prot_seg_cnt), &tc))
3252 goto crc_queuing_error;
3254 if (bundling && prm->prot_seg_cnt) {
3255 /* Walks dif segments */
3256 pkt->add_flags |= CTIO_CRC2_AF_DIF_DSD_ENA;
3258 cur_dsd = &crc_ctx_pkt->u.bundling.dif_dsd;
3259 if (qla24xx_walk_and_build_prot_sglist(ha, NULL, cur_dsd,
3260 prm->prot_seg_cnt, cmd))
3261 goto crc_queuing_error;
3266 /* Cleanup will be performed by the caller */
3267 qpair->req->outstanding_cmds[h] = NULL;
3269 return QLA_FUNCTION_FAILED;
3273 * Callback to setup response of xmit_type of QLA_TGT_XMIT_DATA and *
3274 * QLA_TGT_XMIT_STATUS for >= 24xx silicon
3276 int qlt_xmit_response(struct qla_tgt_cmd *cmd, int xmit_type,
3277 uint8_t scsi_status)
3279 struct scsi_qla_host *vha = cmd->vha;
3280 struct qla_qpair *qpair = cmd->qpair;
3281 struct ctio7_to_24xx *pkt;
3282 struct qla_tgt_prm prm;
3283 uint32_t full_req_cnt = 0;
3284 unsigned long flags = 0;
3287 if (!qpair->fw_started || (cmd->reset_count != qpair->chip_reset) ||
3288 (cmd->sess && cmd->sess->deleted)) {
3289 cmd->state = QLA_TGT_STATE_PROCESSED;
3293 ql_dbg_qp(ql_dbg_tgt, qpair, 0xe018,
3294 "is_send_status=%d, cmd->bufflen=%d, cmd->sg_cnt=%d, cmd->dma_data_direction=%d se_cmd[%p] qp %d\n",
3295 (xmit_type & QLA_TGT_XMIT_STATUS) ?
3296 1 : 0, cmd->bufflen, cmd->sg_cnt, cmd->dma_data_direction,
3297 &cmd->se_cmd, qpair->id);
3299 res = qlt_pre_xmit_response(cmd, &prm, xmit_type, scsi_status,
3301 if (unlikely(res != 0)) {
3305 spin_lock_irqsave(qpair->qp_lock_ptr, flags);
3307 if (xmit_type == QLA_TGT_XMIT_STATUS)
3308 qpair->tgt_counters.core_qla_snd_status++;
3310 qpair->tgt_counters.core_qla_que_buf++;
3312 if (!qpair->fw_started || cmd->reset_count != qpair->chip_reset) {
3314 * Either the port is not online or this request was from
3315 * previous life, just abort the processing.
3317 cmd->state = QLA_TGT_STATE_PROCESSED;
3318 ql_dbg_qp(ql_dbg_async, qpair, 0xe101,
3319 "RESET-RSP online/active/old-count/new-count = %d/%d/%d/%d.\n",
3320 vha->flags.online, qla2x00_reset_active(vha),
3321 cmd->reset_count, qpair->chip_reset);
3322 goto out_unmap_unlock;
3325 /* Does F/W have an IOCBs for this request */
3326 res = qlt_check_reserve_free_req(qpair, full_req_cnt);
3328 goto out_unmap_unlock;
3330 if (cmd->se_cmd.prot_op && (xmit_type & QLA_TGT_XMIT_DATA))
3331 res = qlt_build_ctio_crc2_pkt(qpair, &prm);
3333 res = qlt_24xx_build_ctio_pkt(qpair, &prm);
3334 if (unlikely(res != 0)) {
3335 qpair->req->cnt += full_req_cnt;
3336 goto out_unmap_unlock;
3339 pkt = (struct ctio7_to_24xx *)prm.pkt;
3341 if (qlt_has_data(cmd) && (xmit_type & QLA_TGT_XMIT_DATA)) {
3342 pkt->u.status0.flags |=
3343 cpu_to_le16(CTIO7_FLAGS_DATA_IN |
3344 CTIO7_FLAGS_STATUS_MODE_0);
3346 if (cmd->se_cmd.prot_op == TARGET_PROT_NORMAL)
3347 qlt_load_data_segments(&prm);
3349 if (prm.add_status_pkt == 0) {
3350 if (xmit_type & QLA_TGT_XMIT_STATUS) {
3351 pkt->u.status0.scsi_status =
3352 cpu_to_le16(prm.rq_result);
3354 pkt->u.status0.residual =
3355 cpu_to_le32(prm.residual);
3357 pkt->u.status0.flags |= cpu_to_le16(
3358 CTIO7_FLAGS_SEND_STATUS);
3359 if (qlt_need_explicit_conf(cmd, 0)) {
3360 pkt->u.status0.flags |=
3362 CTIO7_FLAGS_EXPLICIT_CONFORM |
3363 CTIO7_FLAGS_CONFORM_REQ);
3369 * We have already made sure that there is sufficient
3370 * amount of request entries to not drop HW lock in
3373 struct ctio7_to_24xx *ctio =
3374 (struct ctio7_to_24xx *)qlt_get_req_pkt(
3377 ql_dbg_qp(ql_dbg_tgt, qpair, 0x305e,
3378 "Building additional status packet 0x%p.\n",
3382 * T10Dif: ctio_crc2_to_fw overlay ontop of
3385 memcpy(ctio, pkt, sizeof(*ctio));
3386 /* reset back to CTIO7 */
3387 ctio->entry_count = 1;
3388 ctio->entry_type = CTIO_TYPE7;
3389 ctio->dseg_count = 0;
3390 ctio->u.status1.flags &= ~cpu_to_le16(
3391 CTIO7_FLAGS_DATA_IN);
3393 /* Real finish is ctio_m1's finish */
3394 pkt->handle |= CTIO_INTERMEDIATE_HANDLE_MARK;
3395 pkt->u.status0.flags |= cpu_to_le16(
3396 CTIO7_FLAGS_DONT_RET_CTIO);
3398 /* qlt_24xx_init_ctio_to_isp will correct
3399 * all neccessary fields that's part of CTIO7.
3400 * There should be no residual of CTIO-CRC2 data.
3402 qlt_24xx_init_ctio_to_isp((struct ctio7_to_24xx *)ctio,
3406 qlt_24xx_init_ctio_to_isp(pkt, &prm);
3409 cmd->state = QLA_TGT_STATE_PROCESSED; /* Mid-level is done processing */
3410 cmd->cmd_sent_to_fw = 1;
3411 cmd->ctio_flags = le16_to_cpu(pkt->u.status0.flags);
3413 /* Memory Barrier */
3415 if (qpair->reqq_start_iocbs)
3416 qpair->reqq_start_iocbs(qpair);
3418 qla2x00_start_iocbs(vha, qpair->req);
3419 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
3424 qlt_unmap_sg(vha, cmd);
3425 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
3429 EXPORT_SYMBOL(qlt_xmit_response);
3431 int qlt_rdy_to_xfer(struct qla_tgt_cmd *cmd)
3433 struct ctio7_to_24xx *pkt;
3434 struct scsi_qla_host *vha = cmd->vha;
3435 struct qla_tgt *tgt = cmd->tgt;
3436 struct qla_tgt_prm prm;
3437 unsigned long flags = 0;
3439 struct qla_qpair *qpair = cmd->qpair;
3441 memset(&prm, 0, sizeof(prm));
3447 if (!qpair->fw_started || (cmd->reset_count != qpair->chip_reset) ||
3448 (cmd->sess && cmd->sess->deleted)) {
3450 * Either the port is not online or this request was from
3451 * previous life, just abort the processing.
3454 cmd->write_data_transferred = 0;
3455 cmd->state = QLA_TGT_STATE_DATA_IN;
3456 vha->hw->tgt.tgt_ops->handle_data(cmd);
3457 ql_dbg_qp(ql_dbg_async, qpair, 0xe102,
3458 "RESET-XFR online/active/old-count/new-count = %d/%d/%d/%d.\n",
3459 vha->flags.online, qla2x00_reset_active(vha),
3460 cmd->reset_count, qpair->chip_reset);
3464 /* Calculate number of entries and segments required */
3465 if (qlt_pci_map_calc_cnt(&prm) != 0)
3468 spin_lock_irqsave(qpair->qp_lock_ptr, flags);
3469 /* Does F/W have an IOCBs for this request */
3470 res = qlt_check_reserve_free_req(qpair, prm.req_cnt);
3472 goto out_unlock_free_unmap;
3473 if (cmd->se_cmd.prot_op)
3474 res = qlt_build_ctio_crc2_pkt(qpair, &prm);
3476 res = qlt_24xx_build_ctio_pkt(qpair, &prm);
3478 if (unlikely(res != 0)) {
3479 qpair->req->cnt += prm.req_cnt;
3480 goto out_unlock_free_unmap;
3483 pkt = (struct ctio7_to_24xx *)prm.pkt;
3484 pkt->u.status0.flags |= cpu_to_le16(CTIO7_FLAGS_DATA_OUT |
3485 CTIO7_FLAGS_STATUS_MODE_0);
3487 if (cmd->se_cmd.prot_op == TARGET_PROT_NORMAL)
3488 qlt_load_data_segments(&prm);
3490 cmd->state = QLA_TGT_STATE_NEED_DATA;
3491 cmd->cmd_sent_to_fw = 1;
3492 cmd->ctio_flags = le16_to_cpu(pkt->u.status0.flags);
3494 /* Memory Barrier */
3496 if (qpair->reqq_start_iocbs)
3497 qpair->reqq_start_iocbs(qpair);
3499 qla2x00_start_iocbs(vha, qpair->req);
3500 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
3504 out_unlock_free_unmap:
3505 qlt_unmap_sg(vha, cmd);
3506 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
3510 EXPORT_SYMBOL(qlt_rdy_to_xfer);
3514 * it is assumed either hardware_lock or qpair lock is held.
3517 qlt_handle_dif_error(struct qla_qpair *qpair, struct qla_tgt_cmd *cmd,
3518 struct ctio_crc_from_fw *sts)
3520 uint8_t *ap = &sts->actual_dif[0];
3521 uint8_t *ep = &sts->expected_dif[0];
3522 uint64_t lba = cmd->se_cmd.t_task_lba;
3523 uint8_t scsi_status, sense_key, asc, ascq;
3524 unsigned long flags;
3525 struct scsi_qla_host *vha = cmd->vha;
3527 cmd->trc_flags |= TRC_DIF_ERR;
3529 cmd->a_guard = get_unaligned_be16(ap + 0);
3530 cmd->a_app_tag = get_unaligned_be16(ap + 2);
3531 cmd->a_ref_tag = get_unaligned_be32(ap + 4);
3533 cmd->e_guard = get_unaligned_be16(ep + 0);
3534 cmd->e_app_tag = get_unaligned_be16(ep + 2);
3535 cmd->e_ref_tag = get_unaligned_be32(ep + 4);
3537 ql_dbg(ql_dbg_tgt_dif, vha, 0xf075,
3538 "%s: aborted %d state %d\n", __func__, cmd->aborted, cmd->state);
3540 scsi_status = sense_key = asc = ascq = 0;
3542 /* check appl tag */
3543 if (cmd->e_app_tag != cmd->a_app_tag) {
3544 ql_dbg(ql_dbg_tgt_dif, vha, 0xe00d,
3545 "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]",
3546 cmd->cdb[0], lba, (lba+cmd->num_blks), cmd->num_blks,
3547 cmd->a_ref_tag, cmd->e_ref_tag, cmd->a_app_tag,
3548 cmd->e_app_tag, cmd->a_guard, cmd->e_guard, cmd,
3549 cmd->atio.u.isp24.fcp_hdr.ox_id);
3551 cmd->dif_err_code = DIF_ERR_APP;
3552 scsi_status = SAM_STAT_CHECK_CONDITION;
3553 sense_key = ABORTED_COMMAND;
3559 if (cmd->e_ref_tag != cmd->a_ref_tag) {
3560 ql_dbg(ql_dbg_tgt_dif, vha, 0xe00e,
3561 "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] ",
3562 cmd->cdb[0], lba, (lba+cmd->num_blks), cmd->num_blks,
3563 cmd->a_ref_tag, cmd->e_ref_tag, cmd->a_app_tag,
3564 cmd->e_app_tag, cmd->a_guard, cmd->e_guard, cmd,
3565 cmd->atio.u.isp24.fcp_hdr.ox_id);
3567 cmd->dif_err_code = DIF_ERR_REF;
3568 scsi_status = SAM_STAT_CHECK_CONDITION;
3569 sense_key = ABORTED_COMMAND;
3576 if (cmd->e_guard != cmd->a_guard) {
3577 ql_dbg(ql_dbg_tgt_dif, vha, 0xe012,
3578 "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]",
3579 cmd->cdb[0], lba, (lba+cmd->num_blks), cmd->num_blks,
3580 cmd->a_ref_tag, cmd->e_ref_tag, cmd->a_app_tag,
3581 cmd->e_app_tag, cmd->a_guard, cmd->e_guard, cmd,
3582 cmd->atio.u.isp24.fcp_hdr.ox_id);
3584 cmd->dif_err_code = DIF_ERR_GRD;
3585 scsi_status = SAM_STAT_CHECK_CONDITION;
3586 sense_key = ABORTED_COMMAND;
3591 switch (cmd->state) {
3592 case QLA_TGT_STATE_NEED_DATA:
3593 /* handle_data will load DIF error code */
3594 cmd->state = QLA_TGT_STATE_DATA_IN;
3595 vha->hw->tgt.tgt_ops->handle_data(cmd);
3598 spin_lock_irqsave(&cmd->cmd_lock, flags);
3600 spin_unlock_irqrestore(&cmd->cmd_lock, flags);
3601 vha->hw->tgt.tgt_ops->free_cmd(cmd);
3604 spin_unlock_irqrestore(&cmd->cmd_lock, flags);
3606 qlt_send_resp_ctio(qpair, cmd, scsi_status, sense_key, asc,
3608 /* assume scsi status gets out on the wire.
3609 * Will not wait for completion.
3611 vha->hw->tgt.tgt_ops->free_cmd(cmd);
3616 /* If hardware_lock held on entry, might drop it, then reaquire */
3617 /* This function sends the appropriate CTIO to ISP 2xxx or 24xx */
3618 static int __qlt_send_term_imm_notif(struct scsi_qla_host *vha,
3619 struct imm_ntfy_from_isp *ntfy)
3621 struct nack_to_isp *nack;
3622 struct qla_hw_data *ha = vha->hw;
3626 ql_dbg(ql_dbg_tgt_tmr, vha, 0xe01c,
3627 "Sending TERM ELS CTIO (ha=%p)\n", ha);
3629 pkt = (request_t *)qla2x00_alloc_iocbs(vha, NULL);
3631 ql_dbg(ql_dbg_tgt, vha, 0xe080,
3632 "qla_target(%d): %s failed: unable to allocate "
3633 "request packet\n", vha->vp_idx, __func__);
3637 pkt->entry_type = NOTIFY_ACK_TYPE;
3638 pkt->entry_count = 1;
3639 pkt->handle = QLA_TGT_SKIP_HANDLE;
3641 nack = (struct nack_to_isp *)pkt;
3642 nack->ox_id = ntfy->ox_id;
3644 nack->u.isp24.nport_handle = ntfy->u.isp24.nport_handle;
3645 if (le16_to_cpu(ntfy->u.isp24.status) == IMM_NTFY_ELS) {
3646 nack->u.isp24.flags = ntfy->u.isp24.flags &
3647 cpu_to_le16(NOTIFY24XX_FLAGS_PUREX_IOCB);
3651 nack->u.isp24.flags |=
3652 __constant_cpu_to_le16(NOTIFY_ACK_FLAGS_TERMINATE);
3654 nack->u.isp24.srr_rx_id = ntfy->u.isp24.srr_rx_id;
3655 nack->u.isp24.status = ntfy->u.isp24.status;
3656 nack->u.isp24.status_subcode = ntfy->u.isp24.status_subcode;
3657 nack->u.isp24.fw_handle = ntfy->u.isp24.fw_handle;
3658 nack->u.isp24.exchange_address = ntfy->u.isp24.exchange_address;
3659 nack->u.isp24.srr_rel_offs = ntfy->u.isp24.srr_rel_offs;
3660 nack->u.isp24.srr_ui = ntfy->u.isp24.srr_ui;
3661 nack->u.isp24.vp_index = ntfy->u.isp24.vp_index;
3663 qla2x00_start_iocbs(vha, vha->req);
3667 static void qlt_send_term_imm_notif(struct scsi_qla_host *vha,
3668 struct imm_ntfy_from_isp *imm, int ha_locked)
3672 WARN_ON_ONCE(!ha_locked);
3673 rc = __qlt_send_term_imm_notif(vha, imm);
3674 pr_debug("rc = %d\n", rc);
3678 * If hardware_lock held on entry, might drop it, then reaquire
3679 * This function sends the appropriate CTIO to ISP 2xxx or 24xx
3681 static int __qlt_send_term_exchange(struct qla_qpair *qpair,
3682 struct qla_tgt_cmd *cmd,
3683 struct atio_from_isp *atio)
3685 struct scsi_qla_host *vha = qpair->vha;
3686 struct ctio7_to_24xx *ctio24;
3687 struct qla_hw_data *ha = vha->hw;
3692 ql_dbg(ql_dbg_tgt, vha, 0xe009, "Sending TERM EXCH CTIO (ha=%p)\n", ha);
3697 pkt = (request_t *)qla2x00_alloc_iocbs_ready(qpair, NULL);
3699 ql_dbg(ql_dbg_tgt, vha, 0xe050,
3700 "qla_target(%d): %s failed: unable to allocate "
3701 "request packet\n", vha->vp_idx, __func__);
3706 if (cmd->state < QLA_TGT_STATE_PROCESSED) {
3707 ql_dbg(ql_dbg_tgt, vha, 0xe051,
3708 "qla_target(%d): Terminating cmd %p with "
3709 "incorrect state %d\n", vha->vp_idx, cmd,
3715 qpair->tgt_counters.num_term_xchg_sent++;
3716 pkt->entry_count = 1;
3717 pkt->handle = QLA_TGT_SKIP_HANDLE | CTIO_COMPLETION_HANDLE_MARK;
3719 ctio24 = (struct ctio7_to_24xx *)pkt;
3720 ctio24->entry_type = CTIO_TYPE7;
3721 ctio24->nport_handle = cpu_to_le16(CTIO7_NHANDLE_UNRECOGNIZED);
3722 ctio24->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
3723 ctio24->vp_index = vha->vp_idx;
3724 ctio24->initiator_id = be_id_to_le(atio->u.isp24.fcp_hdr.s_id);
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_decr_num_pend_cmds(cmd->vha);
3875 BUG_ON(cmd->sg_mapped);
3876 cmd->jiffies_at_free = get_jiffies_64();
3877 if (unlikely(cmd->free_sg))
3880 if (!sess || !sess->se_sess) {
3884 cmd->jiffies_at_free = get_jiffies_64();
3885 cmd->vha->hw->tgt.tgt_ops->rel_cmd(cmd);
3887 EXPORT_SYMBOL(qlt_free_cmd);
3890 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
3892 static int qlt_term_ctio_exchange(struct qla_qpair *qpair, void *ctio,
3893 struct qla_tgt_cmd *cmd, uint32_t status)
3896 struct scsi_qla_host *vha = qpair->vha;
3898 if (cmd->se_cmd.prot_op)
3899 ql_dbg(ql_dbg_tgt_dif, vha, 0xe013,
3900 "Term DIF cmd: lba[0x%llx|%lld] len[0x%x] "
3901 "se_cmd=%p tag[%x] op %#x/%s",
3903 cmd->num_blks, &cmd->se_cmd,
3904 cmd->atio.u.isp24.exchange_addr,
3905 cmd->se_cmd.prot_op,
3906 prot_op_str(cmd->se_cmd.prot_op));
3909 struct ctio7_from_24xx *c = (struct ctio7_from_24xx *)ctio;
3912 cpu_to_le16(OF_TERM_EXCH));
3917 qlt_send_term_exchange(qpair, cmd, &cmd->atio, 1, 0);
3923 /* ha->hardware_lock supposed to be held on entry */
3924 static void *qlt_ctio_to_cmd(struct scsi_qla_host *vha,
3925 struct rsp_que *rsp, uint32_t handle, void *ctio)
3928 struct req_que *req;
3929 int qid = GET_QID(handle);
3930 uint32_t h = handle & ~QLA_TGT_HANDLE_MASK;
3932 if (unlikely(h == QLA_TGT_SKIP_HANDLE))
3935 if (qid == rsp->req->id) {
3937 } else if (vha->hw->req_q_map[qid]) {
3938 ql_dbg(ql_dbg_tgt_mgt, vha, 0x1000a,
3939 "qla_target(%d): CTIO completion with different QID %d handle %x\n",
3940 vha->vp_idx, rsp->id, handle);
3941 req = vha->hw->req_q_map[qid];
3946 h &= QLA_CMD_HANDLE_MASK;
3948 if (h != QLA_TGT_NULL_HANDLE) {
3949 if (unlikely(h >= req->num_outstanding_cmds)) {
3950 ql_dbg(ql_dbg_tgt, vha, 0xe052,
3951 "qla_target(%d): Wrong handle %x received\n",
3952 vha->vp_idx, handle);
3956 cmd = req->outstanding_cmds[h];
3957 if (unlikely(cmd == NULL)) {
3958 ql_dbg(ql_dbg_async, vha, 0xe053,
3959 "qla_target(%d): Suspicious: unable to find the command with handle %x req->id %d rsp->id %d\n",
3960 vha->vp_idx, handle, req->id, rsp->id);
3963 req->outstanding_cmds[h] = NULL;
3964 } else if (ctio != NULL) {
3965 /* We can't get loop ID from CTIO7 */
3966 ql_dbg(ql_dbg_tgt, vha, 0xe054,
3967 "qla_target(%d): Wrong CTIO received: QLA24xx doesn't "
3968 "support NULL handles\n", vha->vp_idx);
3976 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
3978 static void qlt_do_ctio_completion(struct scsi_qla_host *vha,
3979 struct rsp_que *rsp, uint32_t handle, uint32_t status, void *ctio)
3981 struct qla_hw_data *ha = vha->hw;
3982 struct se_cmd *se_cmd;
3983 struct qla_tgt_cmd *cmd;
3984 struct qla_qpair *qpair = rsp->qpair;
3986 if (handle & CTIO_INTERMEDIATE_HANDLE_MARK) {
3987 /* That could happen only in case of an error/reset/abort */
3988 if (status != CTIO_SUCCESS) {
3989 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01d,
3990 "Intermediate CTIO received"
3991 " (status %x)\n", status);
3996 cmd = qlt_ctio_to_cmd(vha, rsp, handle, ctio);
4000 if ((le16_to_cpu(((struct ctio7_from_24xx *)ctio)->flags) & CTIO7_FLAGS_DATA_OUT) &&
4002 qlt_chk_edif_rx_sa_delete_pending(vha, cmd->sess,
4003 (struct ctio7_from_24xx *)ctio);
4006 se_cmd = &cmd->se_cmd;
4007 cmd->cmd_sent_to_fw = 0;
4009 qlt_unmap_sg(vha, cmd);
4011 if (unlikely(status != CTIO_SUCCESS)) {
4012 switch (status & 0xFFFF) {
4013 case CTIO_INVALID_RX_ID:
4014 if (printk_ratelimit())
4015 dev_info(&vha->hw->pdev->dev,
4016 "qla_target(%d): CTIO with INVALID_RX_ID ATIO attr %x CTIO Flags %x|%x\n",
4017 vha->vp_idx, cmd->atio.u.isp24.attr,
4018 ((cmd->ctio_flags >> 9) & 0xf),
4022 case CTIO_LIP_RESET:
4023 case CTIO_TARGET_RESET:
4025 /* driver request abort via Terminate exchange */
4028 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf058,
4029 "qla_target(%d): CTIO with "
4030 "status %#x received, state %x, se_cmd %p, "
4031 "(LIP_RESET=e, ABORTED=2, TARGET_RESET=17, "
4032 "TIMEOUT=b, INVALID_RX_ID=8)\n", vha->vp_idx,
4033 status, cmd->state, se_cmd);
4036 case CTIO_PORT_LOGGED_OUT:
4037 case CTIO_PORT_UNAVAILABLE:
4040 (status & 0xFFFF) == CTIO_PORT_LOGGED_OUT;
4042 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf059,
4043 "qla_target(%d): CTIO with %s status %x "
4044 "received (state %x, se_cmd %p)\n", vha->vp_idx,
4045 logged_out ? "PORT LOGGED OUT" : "PORT UNAVAILABLE",
4046 status, cmd->state, se_cmd);
4048 if (logged_out && cmd->sess) {
4050 * Session is already logged out, but we need
4051 * to notify initiator, who's not aware of this
4053 cmd->sess->send_els_logo = 1;
4054 ql_dbg(ql_dbg_disc, vha, 0x20f8,
4055 "%s %d %8phC post del sess\n",
4056 __func__, __LINE__, cmd->sess->port_name);
4058 qlt_schedule_sess_for_deletion(cmd->sess);
4062 case CTIO_DIF_ERROR: {
4063 struct ctio_crc_from_fw *crc =
4064 (struct ctio_crc_from_fw *)ctio;
4065 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf073,
4066 "qla_target(%d): CTIO with DIF_ERROR status %x "
4067 "received (state %x, ulp_cmd %p) actual_dif[0x%llx] "
4068 "expect_dif[0x%llx]\n",
4069 vha->vp_idx, status, cmd->state, se_cmd,
4070 *((u64 *)&crc->actual_dif[0]),
4071 *((u64 *)&crc->expected_dif[0]));
4073 qlt_handle_dif_error(qpair, cmd, ctio);
4077 case CTIO_FAST_AUTH_ERR:
4078 case CTIO_FAST_INCOMP_PAD_LEN:
4079 case CTIO_FAST_INVALID_REQ:
4080 case CTIO_FAST_SPI_ERR:
4081 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05b,
4082 "qla_target(%d): CTIO with EDIF error status 0x%x received (state %x, se_cmd %p\n",
4083 vha->vp_idx, status, cmd->state, se_cmd);
4087 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05b,
4088 "qla_target(%d): CTIO with error status 0x%x received (state %x, se_cmd %p\n",
4089 vha->vp_idx, status, cmd->state, se_cmd);
4094 /* "cmd->aborted" means
4095 * cmd is already aborted/terminated, we don't
4096 * need to terminate again. The exchange is already
4097 * cleaned up/freed at FW level. Just cleanup at driver
4100 if ((cmd->state != QLA_TGT_STATE_NEED_DATA) &&
4102 cmd->trc_flags |= TRC_CTIO_ERR;
4103 if (qlt_term_ctio_exchange(qpair, ctio, cmd, status))
4108 if (cmd->state == QLA_TGT_STATE_PROCESSED) {
4109 cmd->trc_flags |= TRC_CTIO_DONE;
4110 } else if (cmd->state == QLA_TGT_STATE_NEED_DATA) {
4111 cmd->state = QLA_TGT_STATE_DATA_IN;
4113 if (status == CTIO_SUCCESS)
4114 cmd->write_data_transferred = 1;
4116 ha->tgt.tgt_ops->handle_data(cmd);
4118 } else if (cmd->aborted) {
4119 cmd->trc_flags |= TRC_CTIO_ABORTED;
4120 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01e,
4121 "Aborted command %p (tag %lld) finished\n", cmd, se_cmd->tag);
4123 cmd->trc_flags |= TRC_CTIO_STRANGE;
4124 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05c,
4125 "qla_target(%d): A command in state (%d) should "
4126 "not return a CTIO complete\n", vha->vp_idx, cmd->state);
4129 if (unlikely(status != CTIO_SUCCESS) &&
4131 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01f, "Finishing failed CTIO\n");
4135 ha->tgt.tgt_ops->free_cmd(cmd);
4138 static inline int qlt_get_fcp_task_attr(struct scsi_qla_host *vha,
4143 switch (task_codes) {
4144 case ATIO_SIMPLE_QUEUE:
4145 fcp_task_attr = TCM_SIMPLE_TAG;
4147 case ATIO_HEAD_OF_QUEUE:
4148 fcp_task_attr = TCM_HEAD_TAG;
4150 case ATIO_ORDERED_QUEUE:
4151 fcp_task_attr = TCM_ORDERED_TAG;
4153 case ATIO_ACA_QUEUE:
4154 fcp_task_attr = TCM_ACA_TAG;
4157 fcp_task_attr = TCM_SIMPLE_TAG;
4160 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05d,
4161 "qla_target: unknown task code %x, use ORDERED instead\n",
4163 fcp_task_attr = TCM_ORDERED_TAG;
4167 return fcp_task_attr;
4171 * Process context for I/O path into tcm_qla2xxx code
4173 static void __qlt_do_work(struct qla_tgt_cmd *cmd)
4175 scsi_qla_host_t *vha = cmd->vha;
4176 struct qla_hw_data *ha = vha->hw;
4177 struct fc_port *sess = cmd->sess;
4178 struct atio_from_isp *atio = &cmd->atio;
4180 unsigned long flags;
4181 uint32_t data_length;
4182 int ret, fcp_task_attr, data_dir, bidi = 0;
4183 struct qla_qpair *qpair = cmd->qpair;
4186 cmd->trc_flags |= TRC_DO_WORK;
4189 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf082,
4190 "cmd with tag %u is aborted\n",
4191 cmd->atio.u.isp24.exchange_addr);
4195 spin_lock_init(&cmd->cmd_lock);
4196 cdb = &atio->u.isp24.fcp_cmnd.cdb[0];
4197 cmd->se_cmd.tag = le32_to_cpu(atio->u.isp24.exchange_addr);
4199 if (atio->u.isp24.fcp_cmnd.rddata &&
4200 atio->u.isp24.fcp_cmnd.wrdata) {
4202 data_dir = DMA_TO_DEVICE;
4203 } else if (atio->u.isp24.fcp_cmnd.rddata)
4204 data_dir = DMA_FROM_DEVICE;
4205 else if (atio->u.isp24.fcp_cmnd.wrdata)
4206 data_dir = DMA_TO_DEVICE;
4208 data_dir = DMA_NONE;
4210 fcp_task_attr = qlt_get_fcp_task_attr(vha,
4211 atio->u.isp24.fcp_cmnd.task_attr);
4212 data_length = get_datalen_for_atio(atio);
4214 ret = ha->tgt.tgt_ops->handle_cmd(vha, cmd, cdb, data_length,
4215 fcp_task_attr, data_dir, bidi);
4219 * Drop extra session reference from qlt_handle_cmd_for_atio().
4221 ha->tgt.tgt_ops->put_sess(sess);
4225 ql_dbg(ql_dbg_io, vha, 0x3060, "Terminating work cmd %p", cmd);
4227 * cmd has not sent to target yet, so pass NULL as the second
4228 * argument to qlt_send_term_exchange() and free the memory here.
4230 cmd->trc_flags |= TRC_DO_WORK_ERR;
4231 spin_lock_irqsave(qpair->qp_lock_ptr, flags);
4232 qlt_send_term_exchange(qpair, NULL, &cmd->atio, 1, 0);
4234 qlt_decr_num_pend_cmds(vha);
4235 cmd->vha->hw->tgt.tgt_ops->rel_cmd(cmd);
4236 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
4238 ha->tgt.tgt_ops->put_sess(sess);
4241 static void qlt_do_work(struct work_struct *work)
4243 struct qla_tgt_cmd *cmd = container_of(work, struct qla_tgt_cmd, work);
4244 scsi_qla_host_t *vha = cmd->vha;
4245 unsigned long flags;
4247 spin_lock_irqsave(&vha->cmd_list_lock, flags);
4248 list_del(&cmd->cmd_list);
4249 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
4254 void qlt_clr_qp_table(struct scsi_qla_host *vha)
4256 unsigned long flags;
4257 struct qla_hw_data *ha = vha->hw;
4258 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
4262 ql_log(ql_log_info, vha, 0x706c,
4263 "User update Number of Active Qpairs %d\n",
4264 ha->tgt.num_act_qpairs);
4266 spin_lock_irqsave(&ha->tgt.atio_lock, flags);
4268 btree_for_each_safe64(&tgt->lun_qpair_map, key, node)
4269 btree_remove64(&tgt->lun_qpair_map, key);
4271 ha->base_qpair->lun_cnt = 0;
4272 for (key = 0; key < ha->max_qpairs; key++)
4273 if (ha->queue_pair_map[key])
4274 ha->queue_pair_map[key]->lun_cnt = 0;
4276 spin_unlock_irqrestore(&ha->tgt.atio_lock, flags);
4279 static void qlt_assign_qpair(struct scsi_qla_host *vha,
4280 struct qla_tgt_cmd *cmd)
4282 struct qla_qpair *qpair, *qp;
4283 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
4284 struct qla_qpair_hint *h;
4286 if (vha->flags.qpairs_available) {
4287 h = btree_lookup64(&tgt->lun_qpair_map, cmd->unpacked_lun);
4289 /* spread lun to qpair ratio evently */
4291 struct scsi_qla_host *base_vha =
4292 pci_get_drvdata(vha->hw->pdev);
4294 qpair = vha->hw->base_qpair;
4295 if (qpair->lun_cnt == 0) {
4297 h = qla_qpair_to_hint(tgt, qpair);
4299 rc = btree_insert64(&tgt->lun_qpair_map,
4300 cmd->unpacked_lun, h, GFP_ATOMIC);
4303 ql_log(ql_log_info, vha, 0xd037,
4304 "Unable to insert lun %llx into lun_qpair_map\n",
4309 lcnt = qpair->lun_cnt;
4313 list_for_each_entry(qp, &base_vha->qp_list,
4315 if (qp->lun_cnt == 0) {
4317 h = qla_qpair_to_hint(tgt, qp);
4319 rc = btree_insert64(&tgt->lun_qpair_map,
4320 cmd->unpacked_lun, h, GFP_ATOMIC);
4323 ql_log(ql_log_info, vha, 0xd038,
4324 "Unable to insert lun %llx into lun_qpair_map\n",
4330 if (qp->lun_cnt < lcnt) {
4339 h = qla_qpair_to_hint(tgt, qpair);
4341 rc = btree_insert64(&tgt->lun_qpair_map,
4342 cmd->unpacked_lun, h, GFP_ATOMIC);
4345 ql_log(ql_log_info, vha, 0xd039,
4346 "Unable to insert lun %llx into lun_qpair_map\n",
4351 h = &tgt->qphints[0];
4354 cmd->qpair = h->qpair;
4355 cmd->se_cmd.cpuid = h->cpuid;
4358 static struct qla_tgt_cmd *qlt_get_tag(scsi_qla_host_t *vha,
4359 struct fc_port *sess,
4360 struct atio_from_isp *atio)
4362 struct qla_tgt_cmd *cmd;
4364 cmd = vha->hw->tgt.tgt_ops->get_cmd(sess);
4368 cmd->cmd_type = TYPE_TGT_CMD;
4369 memcpy(&cmd->atio, atio, sizeof(*atio));
4370 INIT_LIST_HEAD(&cmd->sess_cmd_list);
4371 cmd->state = QLA_TGT_STATE_NEW;
4372 cmd->tgt = vha->vha_tgt.qla_tgt;
4373 qlt_incr_num_pend_cmds(vha);
4376 cmd->loop_id = sess->loop_id;
4377 cmd->conf_compl_supported = sess->conf_compl_supported;
4380 cmd->jiffies_at_alloc = get_jiffies_64();
4382 cmd->unpacked_lun = scsilun_to_int(
4383 (struct scsi_lun *)&atio->u.isp24.fcp_cmnd.lun);
4384 qlt_assign_qpair(vha, cmd);
4385 cmd->reset_count = vha->hw->base_qpair->chip_reset;
4386 cmd->vp_idx = vha->vp_idx;
4387 cmd->edif = sess->edif.enable;
4392 /* ha->hardware_lock supposed to be held on entry */
4393 static int qlt_handle_cmd_for_atio(struct scsi_qla_host *vha,
4394 struct atio_from_isp *atio)
4396 struct qla_hw_data *ha = vha->hw;
4397 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
4398 struct fc_port *sess;
4399 struct qla_tgt_cmd *cmd;
4400 unsigned long flags;
4403 if (unlikely(tgt->tgt_stop)) {
4404 ql_dbg(ql_dbg_io, vha, 0x3061,
4405 "New command while device %p is shutting down\n", tgt);
4409 id = be_to_port_id(atio->u.isp24.fcp_hdr.s_id);
4410 if (IS_SW_RESV_ADDR(id))
4413 sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha, atio->u.isp24.fcp_hdr.s_id);
4414 if (unlikely(!sess))
4417 /* Another WWN used to have our s_id. Our PLOGI scheduled its
4418 * session deletion, but it's still in sess_del_work wq */
4419 if (sess->deleted) {
4420 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf002,
4421 "New command while old session %p is being deleted\n",
4427 * Do kref_get() before returning + dropping qla_hw_data->hardware_lock.
4429 if (!kref_get_unless_zero(&sess->sess_kref)) {
4430 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf004,
4431 "%s: kref_get fail, %8phC oxid %x \n",
4432 __func__, sess->port_name,
4433 be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id));
4437 cmd = qlt_get_tag(vha, sess, atio);
4439 ql_dbg(ql_dbg_io, vha, 0x3062,
4440 "qla_target(%d): Allocation of cmd failed\n", vha->vp_idx);
4441 ha->tgt.tgt_ops->put_sess(sess);
4446 cmd->trc_flags |= TRC_NEW_CMD;
4448 spin_lock_irqsave(&vha->cmd_list_lock, flags);
4449 list_add_tail(&cmd->cmd_list, &vha->qla_cmd_list);
4450 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
4452 INIT_WORK(&cmd->work, qlt_do_work);
4453 if (vha->flags.qpairs_available) {
4454 queue_work_on(cmd->se_cmd.cpuid, qla_tgt_wq, &cmd->work);
4455 } else if (ha->msix_count) {
4456 if (cmd->atio.u.isp24.fcp_cmnd.rddata)
4457 queue_work_on(smp_processor_id(), qla_tgt_wq,
4460 queue_work_on(cmd->se_cmd.cpuid, qla_tgt_wq,
4463 queue_work(qla_tgt_wq, &cmd->work);
4469 /* ha->hardware_lock supposed to be held on entry */
4470 static int qlt_issue_task_mgmt(struct fc_port *sess, u64 lun,
4471 int fn, void *iocb, int flags)
4473 struct scsi_qla_host *vha = sess->vha;
4474 struct qla_hw_data *ha = vha->hw;
4475 struct qla_tgt_mgmt_cmd *mcmd;
4476 struct atio_from_isp *a = (struct atio_from_isp *)iocb;
4477 struct qla_qpair_hint *h = &vha->vha_tgt.qla_tgt->qphints[0];
4479 mcmd = mempool_alloc(qla_tgt_mgmt_cmd_mempool, GFP_ATOMIC);
4481 ql_dbg(ql_dbg_tgt_tmr, vha, 0x10009,
4482 "qla_target(%d): Allocation of management "
4483 "command failed, some commands and their data could "
4484 "leak\n", vha->vp_idx);
4487 memset(mcmd, 0, sizeof(*mcmd));
4491 memcpy(&mcmd->orig_iocb.imm_ntfy, iocb,
4492 sizeof(mcmd->orig_iocb.imm_ntfy));
4494 mcmd->tmr_func = fn;
4495 mcmd->flags = flags;
4496 mcmd->reset_count = ha->base_qpair->chip_reset;
4497 mcmd->qpair = h->qpair;
4499 mcmd->se_cmd.cpuid = h->cpuid;
4500 mcmd->unpacked_lun = lun;
4503 case QLA_TGT_LUN_RESET:
4504 case QLA_TGT_CLEAR_TS:
4505 case QLA_TGT_ABORT_TS:
4506 abort_cmds_for_lun(vha, lun, a->u.isp24.fcp_hdr.s_id);
4508 case QLA_TGT_CLEAR_ACA:
4509 h = qlt_find_qphint(vha, mcmd->unpacked_lun);
4510 mcmd->qpair = h->qpair;
4511 mcmd->se_cmd.cpuid = h->cpuid;
4514 case QLA_TGT_TARGET_RESET:
4515 case QLA_TGT_NEXUS_LOSS_SESS:
4516 case QLA_TGT_NEXUS_LOSS:
4517 case QLA_TGT_ABORT_ALL:
4523 INIT_WORK(&mcmd->work, qlt_do_tmr_work);
4524 queue_work_on(mcmd->se_cmd.cpuid, qla_tgt_wq,
4530 /* ha->hardware_lock supposed to be held on entry */
4531 static int qlt_handle_task_mgmt(struct scsi_qla_host *vha, void *iocb)
4533 struct atio_from_isp *a = (struct atio_from_isp *)iocb;
4534 struct qla_hw_data *ha = vha->hw;
4535 struct fc_port *sess;
4538 unsigned long flags;
4540 fn = a->u.isp24.fcp_cmnd.task_mgmt_flags;
4542 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
4543 sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha,
4544 a->u.isp24.fcp_hdr.s_id);
4545 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
4548 scsilun_to_int((struct scsi_lun *)&a->u.isp24.fcp_cmnd.lun);
4550 if (sess == NULL || sess->deleted)
4553 return qlt_issue_task_mgmt(sess, unpacked_lun, fn, iocb, 0);
4556 /* ha->hardware_lock supposed to be held on entry */
4557 static int __qlt_abort_task(struct scsi_qla_host *vha,
4558 struct imm_ntfy_from_isp *iocb, struct fc_port *sess)
4560 struct atio_from_isp *a = (struct atio_from_isp *)iocb;
4561 struct qla_hw_data *ha = vha->hw;
4562 struct qla_tgt_mgmt_cmd *mcmd;
4566 mcmd = mempool_alloc(qla_tgt_mgmt_cmd_mempool, GFP_ATOMIC);
4568 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05f,
4569 "qla_target(%d): %s: Allocation of ABORT cmd failed\n",
4570 vha->vp_idx, __func__);
4573 memset(mcmd, 0, sizeof(*mcmd));
4576 memcpy(&mcmd->orig_iocb.imm_ntfy, iocb,
4577 sizeof(mcmd->orig_iocb.imm_ntfy));
4580 scsilun_to_int((struct scsi_lun *)&a->u.isp24.fcp_cmnd.lun);
4581 mcmd->reset_count = ha->base_qpair->chip_reset;
4582 mcmd->tmr_func = QLA_TGT_2G_ABORT_TASK;
4583 mcmd->qpair = ha->base_qpair;
4585 rc = ha->tgt.tgt_ops->handle_tmr(mcmd, unpacked_lun, mcmd->tmr_func,
4586 le16_to_cpu(iocb->u.isp2x.seq_id));
4588 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf060,
4589 "qla_target(%d): tgt_ops->handle_tmr() failed: %d\n",
4591 mempool_free(mcmd, qla_tgt_mgmt_cmd_mempool);
4598 /* ha->hardware_lock supposed to be held on entry */
4599 static int qlt_abort_task(struct scsi_qla_host *vha,
4600 struct imm_ntfy_from_isp *iocb)
4602 struct qla_hw_data *ha = vha->hw;
4603 struct fc_port *sess;
4605 unsigned long flags;
4607 loop_id = GET_TARGET_ID(ha, (struct atio_from_isp *)iocb);
4609 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
4610 sess = ha->tgt.tgt_ops->find_sess_by_loop_id(vha, loop_id);
4611 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
4614 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf025,
4615 "qla_target(%d): task abort for unexisting "
4616 "session\n", vha->vp_idx);
4617 return qlt_sched_sess_work(vha->vha_tgt.qla_tgt,
4618 QLA_TGT_SESS_WORK_ABORT, iocb, sizeof(*iocb));
4621 return __qlt_abort_task(vha, iocb, sess);
4624 void qlt_logo_completion_handler(fc_port_t *fcport, int rc)
4626 if (rc != MBS_COMMAND_COMPLETE) {
4627 ql_dbg(ql_dbg_tgt_mgt, fcport->vha, 0xf093,
4628 "%s: se_sess %p / sess %p from"
4629 " port %8phC loop_id %#04x s_id %02x:%02x:%02x"
4630 " LOGO failed: %#x\n",
4634 fcport->port_name, fcport->loop_id,
4635 fcport->d_id.b.domain, fcport->d_id.b.area,
4636 fcport->d_id.b.al_pa, rc);
4639 fcport->logout_completed = 1;
4643 * ha->hardware_lock supposed to be held on entry (to protect tgt->sess_list)
4645 * Schedules sessions with matching port_id/loop_id but different wwn for
4646 * deletion. Returns existing session with matching wwn if present.
4650 qlt_find_sess_invalidate_other(scsi_qla_host_t *vha, uint64_t wwn,
4651 port_id_t port_id, uint16_t loop_id, struct fc_port **conflict_sess)
4653 struct fc_port *sess = NULL, *other_sess;
4656 *conflict_sess = NULL;
4658 list_for_each_entry(other_sess, &vha->vp_fcports, list) {
4660 other_wwn = wwn_to_u64(other_sess->port_name);
4662 if (wwn == other_wwn) {
4668 /* find other sess with nport_id collision */
4669 if (port_id.b24 == other_sess->d_id.b24) {
4670 if (loop_id != other_sess->loop_id) {
4671 ql_dbg(ql_dbg_disc, vha, 0x1000c,
4672 "Invalidating sess %p loop_id %d wwn %llx.\n",
4673 other_sess, other_sess->loop_id, other_wwn);
4676 * logout_on_delete is set by default, but another
4677 * session that has the same s_id/loop_id combo
4678 * might have cleared it when requested this session
4679 * deletion, so don't touch it
4681 qlt_schedule_sess_for_deletion(other_sess);
4684 * Another wwn used to have our s_id/loop_id
4685 * kill the session, but don't free the loop_id
4687 ql_dbg(ql_dbg_disc, vha, 0xf01b,
4688 "Invalidating sess %p loop_id %d wwn %llx.\n",
4689 other_sess, other_sess->loop_id, other_wwn);
4691 other_sess->keep_nport_handle = 1;
4692 if (other_sess->disc_state != DSC_DELETED)
4693 *conflict_sess = other_sess;
4694 qlt_schedule_sess_for_deletion(other_sess);
4699 /* find other sess with nport handle collision */
4700 if ((loop_id == other_sess->loop_id) &&
4701 (loop_id != FC_NO_LOOP_ID)) {
4702 ql_dbg(ql_dbg_disc, vha, 0x1000d,
4703 "Invalidating sess %p loop_id %d wwn %llx.\n",
4704 other_sess, other_sess->loop_id, other_wwn);
4706 /* Same loop_id but different s_id
4707 * Ok to kill and logout */
4708 qlt_schedule_sess_for_deletion(other_sess);
4715 /* Abort any commands for this s_id waiting on qla_tgt_wq workqueue */
4716 static int abort_cmds_for_s_id(struct scsi_qla_host *vha, port_id_t *s_id)
4718 struct qla_tgt_sess_op *op;
4719 struct qla_tgt_cmd *cmd;
4722 unsigned long flags;
4724 key = (((u32)s_id->b.domain << 16) |
4725 ((u32)s_id->b.area << 8) |
4726 ((u32)s_id->b.al_pa));
4728 spin_lock_irqsave(&vha->cmd_list_lock, flags);
4729 list_for_each_entry(op, &vha->qla_sess_op_cmd_list, cmd_list) {
4730 uint32_t op_key = sid_to_key(op->atio.u.isp24.fcp_hdr.s_id);
4732 if (op_key == key) {
4738 list_for_each_entry(op, &vha->unknown_atio_list, cmd_list) {
4739 uint32_t op_key = sid_to_key(op->atio.u.isp24.fcp_hdr.s_id);
4741 if (op_key == key) {
4747 list_for_each_entry(cmd, &vha->qla_cmd_list, cmd_list) {
4748 uint32_t cmd_key = sid_to_key(cmd->atio.u.isp24.fcp_hdr.s_id);
4750 if (cmd_key == key) {
4755 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
4760 static int qlt_handle_login(struct scsi_qla_host *vha,
4761 struct imm_ntfy_from_isp *iocb)
4763 struct fc_port *sess = NULL, *conflict_sess = NULL;
4766 uint16_t loop_id, wd3_lo;
4768 struct qlt_plogi_ack_t *pla;
4769 unsigned long flags;
4771 lockdep_assert_held(&vha->hw->hardware_lock);
4773 wwn = wwn_to_u64(iocb->u.isp24.port_name);
4775 port_id.b.domain = iocb->u.isp24.port_id[2];
4776 port_id.b.area = iocb->u.isp24.port_id[1];
4777 port_id.b.al_pa = iocb->u.isp24.port_id[0];
4778 port_id.b.rsvd_1 = 0;
4780 loop_id = le16_to_cpu(iocb->u.isp24.nport_handle);
4782 /* Mark all stale commands sitting in qla_tgt_wq for deletion */
4783 abort_cmds_for_s_id(vha, &port_id);
4786 spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
4787 sess = qlt_find_sess_invalidate_other(vha, wwn,
4788 port_id, loop_id, &conflict_sess);
4789 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
4791 ql_dbg(ql_dbg_disc, vha, 0xffff,
4792 "%s %d Term INOT due to WWN=0 lid=%d, NportID %06X ",
4793 __func__, __LINE__, loop_id, port_id.b24);
4794 qlt_send_term_imm_notif(vha, iocb, 1);
4798 if (IS_SW_RESV_ADDR(port_id)) {
4803 if (vha->hw->flags.edif_enabled &&
4804 !(vha->e_dbell.db_flags & EDB_ACTIVE) &&
4805 iocb->u.isp24.status_subcode == ELS_PLOGI &&
4806 !(le16_to_cpu(iocb->u.isp24.flags) & NOTIFY24XX_FLAGS_FCSP)) {
4807 ql_dbg(ql_dbg_disc, vha, 0xffff,
4808 "%s %d Term INOT due to app not available lid=%d, NportID %06X ",
4809 __func__, __LINE__, loop_id, port_id.b24);
4810 qlt_send_term_imm_notif(vha, iocb, 1);
4814 if (vha->hw->flags.edif_enabled) {
4815 if (!(vha->e_dbell.db_flags & EDB_ACTIVE)) {
4816 ql_dbg(ql_dbg_disc, vha, 0xffff,
4817 "%s %d Term INOT due to app not started lid=%d, NportID %06X ",
4818 __func__, __LINE__, loop_id, port_id.b24);
4819 qlt_send_term_imm_notif(vha, iocb, 1);
4821 } else if (iocb->u.isp24.status_subcode == ELS_PLOGI &&
4822 !(le16_to_cpu(iocb->u.isp24.flags) & NOTIFY24XX_FLAGS_FCSP)) {
4823 ql_dbg(ql_dbg_disc, vha, 0xffff,
4824 "%s %d Term INOT due to unsecure lid=%d, NportID %06X ",
4825 __func__, __LINE__, loop_id, port_id.b24);
4826 qlt_send_term_imm_notif(vha, iocb, 1);
4831 pla = qlt_plogi_ack_find_add(vha, &port_id, iocb);
4833 ql_dbg(ql_dbg_disc + ql_dbg_verbose, vha, 0xffff,
4834 "%s %d %8phC Term INOT due to mem alloc fail",
4836 iocb->u.isp24.port_name);
4837 qlt_send_term_imm_notif(vha, iocb, 1);
4841 if (conflict_sess) {
4842 conflict_sess->login_gen++;
4843 qlt_plogi_ack_link(vha, pla, conflict_sess,
4844 QLT_PLOGI_LINK_CONFLICT);
4849 ql_dbg(ql_dbg_disc, vha, 0xffff,
4850 "%s %d %8phC post new sess\n",
4851 __func__, __LINE__, iocb->u.isp24.port_name);
4852 if (iocb->u.isp24.status_subcode == ELS_PLOGI)
4853 qla24xx_post_newsess_work(vha, &port_id,
4854 iocb->u.isp24.port_name,
4855 iocb->u.isp24.u.plogi.node_name,
4858 qla24xx_post_newsess_work(vha, &port_id,
4859 iocb->u.isp24.port_name, NULL,
4865 if (sess->disc_state == DSC_UPD_FCPORT) {
4869 * Remote port registration is still going on from
4870 * previous login. Allow it to finish before we
4871 * accept the new login.
4873 sess->next_disc_state = DSC_DELETE_PEND;
4874 sec = jiffies_to_msecs(jiffies -
4875 sess->jiffies_at_registration) / 1000;
4876 if (sess->sec_since_registration < sec && sec &&
4878 sess->sec_since_registration = sec;
4879 ql_dbg(ql_dbg_disc, vha, 0xffff,
4880 "%s %8phC - Slow Rport registration (%d Sec)\n",
4881 __func__, sess->port_name, sec);
4884 if (!conflict_sess) {
4885 list_del(&pla->list);
4886 kmem_cache_free(qla_tgt_plogi_cachep, pla);
4889 qlt_send_term_imm_notif(vha, iocb, 1);
4893 qlt_plogi_ack_link(vha, pla, sess, QLT_PLOGI_LINK_SAME_WWN);
4894 sess->d_id = port_id;
4896 sess->loop_id = loop_id;
4898 if (iocb->u.isp24.status_subcode == ELS_PLOGI) {
4899 /* remote port has assigned Port ID */
4900 if (N2N_TOPO(vha->hw) && fcport_is_bigger(sess))
4901 vha->d_id = sess->d_id;
4903 ql_dbg(ql_dbg_disc, vha, 0xffff,
4904 "%s %8phC - send port online\n",
4905 __func__, sess->port_name);
4907 qla2x00_post_aen_work(vha, FCH_EVT_PORT_ONLINE,
4911 if (iocb->u.isp24.status_subcode == ELS_PRLI) {
4912 sess->fw_login_state = DSC_LS_PRLI_PEND;
4914 sess->loop_id = loop_id;
4915 sess->d_id = port_id;
4916 sess->fw_login_state = DSC_LS_PRLI_PEND;
4917 wd3_lo = le16_to_cpu(iocb->u.isp24.u.prli.wd3_lo);
4920 sess->conf_compl_supported = 1;
4922 if ((wd3_lo & BIT_4) == 0)
4923 sess->port_type = FCT_INITIATOR;
4925 sess->port_type = FCT_TARGET;
4928 sess->fw_login_state = DSC_LS_PLOGI_PEND;
4931 ql_dbg(ql_dbg_disc, vha, 0x20f9,
4932 "%s %d %8phC DS %d\n",
4933 __func__, __LINE__, sess->port_name, sess->disc_state);
4935 switch (sess->disc_state) {
4937 case DSC_LOGIN_PEND:
4938 qlt_plogi_ack_unref(vha, pla);
4943 * Under normal circumstances we want to release nport handle
4944 * during LOGO process to avoid nport handle leaks inside FW.
4945 * The exception is when LOGO is done while another PLOGI with
4946 * the same nport handle is waiting as might be the case here.
4947 * Note: there is always a possibily of a race where session
4948 * deletion has already started for other reasons (e.g. ACL
4949 * removal) and now PLOGI arrives:
4950 * 1. if PLOGI arrived in FW after nport handle has been freed,
4951 * FW must have assigned this PLOGI a new/same handle and we
4952 * can proceed ACK'ing it as usual when session deletion
4954 * 2. if PLOGI arrived in FW before LOGO with LCF_FREE_NPORT
4955 * bit reached it, the handle has now been released. We'll
4956 * get an error when we ACK this PLOGI. Nothing will be sent
4957 * back to initiator. Initiator should eventually retry
4958 * PLOGI and situation will correct itself.
4960 sess->keep_nport_handle = ((sess->loop_id == loop_id) &&
4961 (sess->d_id.b24 == port_id.b24));
4963 ql_dbg(ql_dbg_disc, vha, 0x20f9,
4964 "%s %d %8phC post del sess\n",
4965 __func__, __LINE__, sess->port_name);
4968 qlt_schedule_sess_for_deletion(sess);
4976 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
4978 static int qlt_24xx_handle_els(struct scsi_qla_host *vha,
4979 struct imm_ntfy_from_isp *iocb)
4981 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
4982 struct qla_hw_data *ha = vha->hw;
4983 struct fc_port *sess = NULL, *conflict_sess = NULL;
4989 unsigned long flags;
4991 lockdep_assert_held(&ha->hardware_lock);
4993 wwn = wwn_to_u64(iocb->u.isp24.port_name);
4995 port_id.b.domain = iocb->u.isp24.port_id[2];
4996 port_id.b.area = iocb->u.isp24.port_id[1];
4997 port_id.b.al_pa = iocb->u.isp24.port_id[0];
4998 port_id.b.rsvd_1 = 0;
5000 loop_id = le16_to_cpu(iocb->u.isp24.nport_handle);
5002 ql_dbg(ql_dbg_disc, vha, 0xf026,
5003 "qla_target(%d): Port ID: %02x:%02x:%02x ELS opcode: 0x%02x lid %d %8phC\n",
5004 vha->vp_idx, iocb->u.isp24.port_id[2],
5005 iocb->u.isp24.port_id[1], iocb->u.isp24.port_id[0],
5006 iocb->u.isp24.status_subcode, loop_id,
5007 iocb->u.isp24.port_name);
5009 /* res = 1 means ack at the end of thread
5010 * res = 0 means ack async/later.
5012 switch (iocb->u.isp24.status_subcode) {
5014 res = qlt_handle_login(vha, iocb);
5019 sess = qla2x00_find_fcport_by_wwpn(vha,
5020 iocb->u.isp24.port_name, 1);
5022 if (vha->hw->flags.edif_enabled && sess &&
5023 (!(sess->flags & FCF_FCSP_DEVICE) ||
5024 !sess->edif.authok)) {
5025 ql_dbg(ql_dbg_disc, vha, 0xffff,
5026 "%s %d %8phC Term PRLI due to unauthorize PRLI\n",
5027 __func__, __LINE__, iocb->u.isp24.port_name);
5028 qlt_send_term_imm_notif(vha, iocb, 1);
5032 if (sess && sess->plogi_link[QLT_PLOGI_LINK_SAME_WWN]) {
5033 ql_dbg(ql_dbg_disc, vha, 0xffff,
5034 "%s %d %8phC Term PRLI due to PLOGI ACK not completed\n",
5036 iocb->u.isp24.port_name);
5037 qlt_send_term_imm_notif(vha, iocb, 1);
5041 res = qlt_handle_login(vha, iocb);
5045 if (IS_SW_RESV_ADDR(port_id)) {
5050 wd3_lo = le16_to_cpu(iocb->u.isp24.u.prli.wd3_lo);
5053 spin_lock_irqsave(&tgt->ha->tgt.sess_lock, flags);
5054 sess = qlt_find_sess_invalidate_other(vha, wwn, port_id,
5055 loop_id, &conflict_sess);
5056 spin_unlock_irqrestore(&tgt->ha->tgt.sess_lock, flags);
5059 if (conflict_sess) {
5060 switch (conflict_sess->disc_state) {
5062 case DSC_DELETE_PEND:
5065 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf09b,
5066 "PRLI with conflicting sess %p port %8phC\n",
5067 conflict_sess, conflict_sess->port_name);
5068 conflict_sess->fw_login_state =
5069 DSC_LS_PORT_UNAVAIL;
5070 qlt_send_term_imm_notif(vha, iocb, 1);
5077 bool delete = false;
5080 if (vha->hw->flags.edif_enabled && sess &&
5081 (!(sess->flags & FCF_FCSP_DEVICE) ||
5082 !sess->edif.authok)) {
5083 ql_dbg(ql_dbg_disc, vha, 0xffff,
5084 "%s %d %8phC Term PRLI due to unauthorize prli\n",
5085 __func__, __LINE__, iocb->u.isp24.port_name);
5086 qlt_send_term_imm_notif(vha, iocb, 1);
5090 spin_lock_irqsave(&tgt->ha->tgt.sess_lock, flags);
5091 switch (sess->fw_login_state) {
5092 case DSC_LS_PLOGI_PEND:
5093 case DSC_LS_PLOGI_COMP:
5094 case DSC_LS_PRLI_COMP:
5101 switch (sess->disc_state) {
5102 case DSC_UPD_FCPORT:
5103 spin_unlock_irqrestore(&tgt->ha->tgt.sess_lock,
5106 sec = jiffies_to_msecs(jiffies -
5107 sess->jiffies_at_registration)/1000;
5108 if (sess->sec_since_registration < sec && sec &&
5110 sess->sec_since_registration = sec;
5111 ql_dbg(ql_dbg_disc, sess->vha, 0xffff,
5112 "%s %8phC : Slow Rport registration(%d Sec)\n",
5113 __func__, sess->port_name, sec);
5115 qlt_send_term_imm_notif(vha, iocb, 1);
5118 case DSC_LOGIN_PEND:
5120 case DSC_LOGIN_COMPLETE:
5129 spin_unlock_irqrestore(&tgt->ha->tgt.sess_lock,
5132 * Impatient initiator sent PRLI before last
5133 * PLOGI could finish. Will force him to re-try,
5134 * while last one finishes.
5136 ql_log(ql_log_warn, sess->vha, 0xf095,
5137 "sess %p PRLI received, before plogi ack.\n",
5139 qlt_send_term_imm_notif(vha, iocb, 1);
5145 * This shouldn't happen under normal circumstances,
5146 * since we have deleted the old session during PLOGI
5148 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf096,
5149 "PRLI (loop_id %#04x) for existing sess %p (loop_id %#04x)\n",
5150 sess->loop_id, sess, iocb->u.isp24.nport_handle);
5153 sess->loop_id = loop_id;
5154 sess->d_id = port_id;
5155 sess->fw_login_state = DSC_LS_PRLI_PEND;
5158 sess->conf_compl_supported = 1;
5160 if ((wd3_lo & BIT_4) == 0)
5161 sess->port_type = FCT_INITIATOR;
5163 sess->port_type = FCT_TARGET;
5165 spin_unlock_irqrestore(&tgt->ha->tgt.sess_lock, flags);
5167 res = 1; /* send notify ack */
5169 /* Make session global (not used in fabric mode) */
5170 if (ha->current_topology != ISP_CFG_F) {
5172 ql_dbg(ql_dbg_disc, vha, 0x20fa,
5173 "%s %d %8phC post nack\n",
5174 __func__, __LINE__, sess->port_name);
5175 qla24xx_post_nack_work(vha, sess, iocb,
5179 set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
5180 set_bit(LOCAL_LOOP_UPDATE, &vha->dpc_flags);
5181 qla2xxx_wake_dpc(vha);
5185 ql_dbg(ql_dbg_disc, vha, 0x20fb,
5186 "%s %d %8phC post nack\n",
5187 __func__, __LINE__, sess->port_name);
5188 qla24xx_post_nack_work(vha, sess, iocb,
5196 if (le16_to_cpu(iocb->u.isp24.flags) &
5197 NOTIFY24XX_FLAGS_GLOBAL_TPRLO) {
5199 qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS);
5206 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
5207 sess = qla2x00_find_fcport_by_loopid(vha, loop_id);
5208 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
5212 sess->fw_login_state = DSC_LS_LOGO_PEND;
5213 sess->logo_ack_needed = 1;
5214 memcpy(sess->iocb, iocb, IOCB_SIZE);
5217 res = qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS_SESS);
5219 ql_dbg(ql_dbg_disc, vha, 0x20fc,
5220 "%s: logo %llx res %d sess %p ",
5221 __func__, wwn, res, sess);
5224 * cmd went upper layer, look for qlt_xmit_tm_rsp()
5225 * for LOGO_ACK & sess delete
5230 /* cmd did not go to upper layer. */
5232 qlt_schedule_sess_for_deletion(sess);
5235 /* else logo will be ack */
5241 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
5243 if (tgt->link_reinit_iocb_pending) {
5244 qlt_send_notify_ack(ha->base_qpair,
5245 &tgt->link_reinit_iocb, 0, 0, 0, 0, 0, 0);
5246 tgt->link_reinit_iocb_pending = 0;
5249 sess = qla2x00_find_fcport_by_wwpn(vha,
5250 iocb->u.isp24.port_name, 1);
5252 ql_dbg(ql_dbg_disc, vha, 0x20fd,
5253 "sess %p lid %d|%d DS %d LS %d\n",
5254 sess, sess->loop_id, loop_id,
5255 sess->disc_state, sess->fw_login_state);
5258 res = 1; /* send notify ack */
5262 case ELS_FLOGI: /* should never happen */
5264 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf061,
5265 "qla_target(%d): Unsupported ELS command %x "
5266 "received\n", vha->vp_idx, iocb->u.isp24.status_subcode);
5267 res = qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS_SESS);
5271 ql_dbg(ql_dbg_disc, vha, 0xf026,
5272 "qla_target(%d): Exit ELS opcode: 0x%02x res %d\n",
5273 vha->vp_idx, iocb->u.isp24.status_subcode, res);
5279 * ha->hardware_lock supposed to be held on entry.
5280 * Might drop it, then reacquire.
5282 static void qlt_handle_imm_notify(struct scsi_qla_host *vha,
5283 struct imm_ntfy_from_isp *iocb)
5285 struct qla_hw_data *ha = vha->hw;
5286 uint32_t add_flags = 0;
5287 int send_notify_ack = 1;
5290 lockdep_assert_held(&ha->hardware_lock);
5292 status = le16_to_cpu(iocb->u.isp2x.status);
5294 case IMM_NTFY_LIP_RESET:
5296 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf032,
5297 "qla_target(%d): LIP reset (loop %#x), subcode %x\n",
5298 vha->vp_idx, le16_to_cpu(iocb->u.isp24.nport_handle),
5299 iocb->u.isp24.status_subcode);
5301 if (qlt_reset(vha, iocb, QLA_TGT_ABORT_ALL) == 0)
5302 send_notify_ack = 0;
5306 case IMM_NTFY_LIP_LINK_REINIT:
5308 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
5310 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf033,
5311 "qla_target(%d): LINK REINIT (loop %#x, "
5312 "subcode %x)\n", vha->vp_idx,
5313 le16_to_cpu(iocb->u.isp24.nport_handle),
5314 iocb->u.isp24.status_subcode);
5315 if (tgt->link_reinit_iocb_pending) {
5316 qlt_send_notify_ack(ha->base_qpair,
5317 &tgt->link_reinit_iocb, 0, 0, 0, 0, 0, 0);
5319 memcpy(&tgt->link_reinit_iocb, iocb, sizeof(*iocb));
5320 tgt->link_reinit_iocb_pending = 1;
5322 * QLogic requires to wait after LINK REINIT for possible
5323 * PDISC or ADISC ELS commands
5325 send_notify_ack = 0;
5329 case IMM_NTFY_PORT_LOGOUT:
5330 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf034,
5331 "qla_target(%d): Port logout (loop "
5332 "%#x, subcode %x)\n", vha->vp_idx,
5333 le16_to_cpu(iocb->u.isp24.nport_handle),
5334 iocb->u.isp24.status_subcode);
5336 if (qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS_SESS) == 0)
5337 send_notify_ack = 0;
5338 /* The sessions will be cleared in the callback, if needed */
5341 case IMM_NTFY_GLBL_TPRLO:
5342 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf035,
5343 "qla_target(%d): Global TPRLO (%x)\n", vha->vp_idx, status);
5344 if (qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS) == 0)
5345 send_notify_ack = 0;
5346 /* The sessions will be cleared in the callback, if needed */
5349 case IMM_NTFY_PORT_CONFIG:
5350 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf036,
5351 "qla_target(%d): Port config changed (%x)\n", vha->vp_idx,
5353 if (qlt_reset(vha, iocb, QLA_TGT_ABORT_ALL) == 0)
5354 send_notify_ack = 0;
5355 /* The sessions will be cleared in the callback, if needed */
5358 case IMM_NTFY_GLBL_LOGO:
5359 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06a,
5360 "qla_target(%d): Link failure detected\n",
5362 /* I_T nexus loss */
5363 if (qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS) == 0)
5364 send_notify_ack = 0;
5367 case IMM_NTFY_IOCB_OVERFLOW:
5368 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06b,
5369 "qla_target(%d): Cannot provide requested "
5370 "capability (IOCB overflowed the immediate notify "
5371 "resource count)\n", vha->vp_idx);
5374 case IMM_NTFY_ABORT_TASK:
5375 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf037,
5376 "qla_target(%d): Abort Task (S %08x I %#x -> "
5377 "L %#x)\n", vha->vp_idx,
5378 le16_to_cpu(iocb->u.isp2x.seq_id),
5379 GET_TARGET_ID(ha, (struct atio_from_isp *)iocb),
5380 le16_to_cpu(iocb->u.isp2x.lun));
5381 if (qlt_abort_task(vha, iocb) == 0)
5382 send_notify_ack = 0;
5385 case IMM_NTFY_RESOURCE:
5386 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06c,
5387 "qla_target(%d): Out of resources, host %ld\n",
5388 vha->vp_idx, vha->host_no);
5391 case IMM_NTFY_MSG_RX:
5392 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf038,
5393 "qla_target(%d): Immediate notify task %x\n",
5394 vha->vp_idx, iocb->u.isp2x.task_flags);
5398 if (qlt_24xx_handle_els(vha, iocb) == 0)
5399 send_notify_ack = 0;
5402 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06d,
5403 "qla_target(%d): Received unknown immediate "
5404 "notify status %x\n", vha->vp_idx, status);
5408 if (send_notify_ack)
5409 qlt_send_notify_ack(ha->base_qpair, iocb, add_flags, 0, 0, 0,
5414 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
5415 * This function sends busy to ISP 2xxx or 24xx.
5417 static int __qlt_send_busy(struct qla_qpair *qpair,
5418 struct atio_from_isp *atio, uint16_t status)
5420 struct scsi_qla_host *vha = qpair->vha;
5421 struct ctio7_to_24xx *ctio24;
5422 struct qla_hw_data *ha = vha->hw;
5424 struct fc_port *sess = NULL;
5425 unsigned long flags;
5429 id = be_to_port_id(atio->u.isp24.fcp_hdr.s_id);
5431 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
5432 sess = qla2x00_find_fcport_by_nportid(vha, &id, 1);
5433 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
5435 qlt_send_term_exchange(qpair, NULL, atio, 1, 0);
5438 /* Sending marker isn't necessary, since we called from ISR */
5440 pkt = (request_t *)__qla2x00_alloc_iocbs(qpair, NULL);
5442 ql_dbg(ql_dbg_io, vha, 0x3063,
5443 "qla_target(%d): %s failed: unable to allocate "
5444 "request packet", vha->vp_idx, __func__);
5448 qpair->tgt_counters.num_q_full_sent++;
5449 pkt->entry_count = 1;
5450 pkt->handle = QLA_TGT_SKIP_HANDLE | CTIO_COMPLETION_HANDLE_MARK;
5452 ctio24 = (struct ctio7_to_24xx *)pkt;
5453 ctio24->entry_type = CTIO_TYPE7;
5454 ctio24->nport_handle = cpu_to_le16(sess->loop_id);
5455 ctio24->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
5456 ctio24->vp_index = vha->vp_idx;
5457 ctio24->initiator_id = be_id_to_le(atio->u.isp24.fcp_hdr.s_id);
5458 ctio24->exchange_addr = atio->u.isp24.exchange_addr;
5459 temp = (atio->u.isp24.attr << 9) |
5460 CTIO7_FLAGS_STATUS_MODE_1 | CTIO7_FLAGS_SEND_STATUS |
5461 CTIO7_FLAGS_DONT_RET_CTIO;
5462 ctio24->u.status1.flags = cpu_to_le16(temp);
5464 * CTIO from fw w/o se_cmd doesn't provide enough info to retry it,
5465 * if the explicit conformation is used.
5467 ctio24->u.status1.ox_id =
5468 cpu_to_le16(be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id));
5469 ctio24->u.status1.scsi_status = cpu_to_le16(status);
5471 ctio24->u.status1.residual = cpu_to_le32(get_datalen_for_atio(atio));
5473 if (ctio24->u.status1.residual != 0)
5474 ctio24->u.status1.scsi_status |= cpu_to_le16(SS_RESIDUAL_UNDER);
5476 /* Memory Barrier */
5478 if (qpair->reqq_start_iocbs)
5479 qpair->reqq_start_iocbs(qpair);
5481 qla2x00_start_iocbs(vha, qpair->req);
5486 * This routine is used to allocate a command for either a QFull condition
5487 * (ie reply SAM_STAT_BUSY) or to terminate an exchange that did not go
5491 qlt_alloc_qfull_cmd(struct scsi_qla_host *vha,
5492 struct atio_from_isp *atio, uint16_t status, int qfull)
5494 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
5495 struct qla_hw_data *ha = vha->hw;
5496 struct fc_port *sess;
5497 struct qla_tgt_cmd *cmd;
5498 unsigned long flags;
5500 if (unlikely(tgt->tgt_stop)) {
5501 ql_dbg(ql_dbg_io, vha, 0x300a,
5502 "New command while device %p is shutting down\n", tgt);
5506 if ((vha->hw->tgt.num_qfull_cmds_alloc + 1) > MAX_QFULL_CMDS_ALLOC) {
5507 vha->hw->tgt.num_qfull_cmds_dropped++;
5508 if (vha->hw->tgt.num_qfull_cmds_dropped >
5509 vha->qla_stats.stat_max_qfull_cmds_dropped)
5510 vha->qla_stats.stat_max_qfull_cmds_dropped =
5511 vha->hw->tgt.num_qfull_cmds_dropped;
5513 ql_dbg(ql_dbg_io, vha, 0x3068,
5514 "qla_target(%d): %s: QFull CMD dropped[%d]\n",
5515 vha->vp_idx, __func__,
5516 vha->hw->tgt.num_qfull_cmds_dropped);
5518 qlt_chk_exch_leak_thresh_hold(vha);
5522 sess = ha->tgt.tgt_ops->find_sess_by_s_id
5523 (vha, atio->u.isp24.fcp_hdr.s_id);
5527 cmd = ha->tgt.tgt_ops->get_cmd(sess);
5529 ql_dbg(ql_dbg_io, vha, 0x3009,
5530 "qla_target(%d): %s: Allocation of cmd failed\n",
5531 vha->vp_idx, __func__);
5533 vha->hw->tgt.num_qfull_cmds_dropped++;
5534 if (vha->hw->tgt.num_qfull_cmds_dropped >
5535 vha->qla_stats.stat_max_qfull_cmds_dropped)
5536 vha->qla_stats.stat_max_qfull_cmds_dropped =
5537 vha->hw->tgt.num_qfull_cmds_dropped;
5539 qlt_chk_exch_leak_thresh_hold(vha);
5543 qlt_incr_num_pend_cmds(vha);
5544 INIT_LIST_HEAD(&cmd->cmd_list);
5545 memcpy(&cmd->atio, atio, sizeof(*atio));
5547 cmd->tgt = vha->vha_tgt.qla_tgt;
5549 cmd->reset_count = ha->base_qpair->chip_reset;
5551 cmd->qpair = ha->base_qpair;
5555 /* NOTE: borrowing the state field to carry the status */
5556 cmd->state = status;
5558 cmd->term_exchg = 1;
5560 spin_lock_irqsave(&vha->hw->tgt.q_full_lock, flags);
5561 list_add_tail(&cmd->cmd_list, &vha->hw->tgt.q_full_list);
5563 vha->hw->tgt.num_qfull_cmds_alloc++;
5564 if (vha->hw->tgt.num_qfull_cmds_alloc >
5565 vha->qla_stats.stat_max_qfull_cmds_alloc)
5566 vha->qla_stats.stat_max_qfull_cmds_alloc =
5567 vha->hw->tgt.num_qfull_cmds_alloc;
5568 spin_unlock_irqrestore(&vha->hw->tgt.q_full_lock, flags);
5572 qlt_free_qfull_cmds(struct qla_qpair *qpair)
5574 struct scsi_qla_host *vha = qpair->vha;
5575 struct qla_hw_data *ha = vha->hw;
5576 unsigned long flags;
5577 struct qla_tgt_cmd *cmd, *tcmd;
5578 struct list_head free_list, q_full_list;
5581 if (list_empty(&ha->tgt.q_full_list))
5584 INIT_LIST_HEAD(&free_list);
5585 INIT_LIST_HEAD(&q_full_list);
5587 spin_lock_irqsave(&vha->hw->tgt.q_full_lock, flags);
5588 if (list_empty(&ha->tgt.q_full_list)) {
5589 spin_unlock_irqrestore(&vha->hw->tgt.q_full_lock, flags);
5593 list_splice_init(&vha->hw->tgt.q_full_list, &q_full_list);
5594 spin_unlock_irqrestore(&vha->hw->tgt.q_full_lock, flags);
5596 spin_lock_irqsave(qpair->qp_lock_ptr, flags);
5597 list_for_each_entry_safe(cmd, tcmd, &q_full_list, cmd_list) {
5599 /* cmd->state is a borrowed field to hold status */
5600 rc = __qlt_send_busy(qpair, &cmd->atio, cmd->state);
5601 else if (cmd->term_exchg)
5602 rc = __qlt_send_term_exchange(qpair, NULL, &cmd->atio);
5608 ql_dbg(ql_dbg_io, vha, 0x3006,
5609 "%s: busy sent for ox_id[%04x]\n", __func__,
5610 be16_to_cpu(cmd->atio.u.isp24.fcp_hdr.ox_id));
5611 else if (cmd->term_exchg)
5612 ql_dbg(ql_dbg_io, vha, 0x3007,
5613 "%s: Term exchg sent for ox_id[%04x]\n", __func__,
5614 be16_to_cpu(cmd->atio.u.isp24.fcp_hdr.ox_id));
5616 ql_dbg(ql_dbg_io, vha, 0x3008,
5617 "%s: Unexpected cmd in QFull list %p\n", __func__,
5620 list_move_tail(&cmd->cmd_list, &free_list);
5622 /* piggy back on hardware_lock for protection */
5623 vha->hw->tgt.num_qfull_cmds_alloc--;
5625 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
5629 list_for_each_entry_safe(cmd, tcmd, &free_list, cmd_list) {
5630 list_del(&cmd->cmd_list);
5631 /* This cmd was never sent to TCM. There is no need
5632 * to schedule free or call free_cmd
5637 if (!list_empty(&q_full_list)) {
5638 spin_lock_irqsave(&vha->hw->tgt.q_full_lock, flags);
5639 list_splice(&q_full_list, &vha->hw->tgt.q_full_list);
5640 spin_unlock_irqrestore(&vha->hw->tgt.q_full_lock, flags);
5647 qlt_send_busy(struct qla_qpair *qpair, struct atio_from_isp *atio,
5651 struct scsi_qla_host *vha = qpair->vha;
5653 rc = __qlt_send_busy(qpair, atio, status);
5655 qlt_alloc_qfull_cmd(vha, atio, status, 1);
5659 qlt_chk_qfull_thresh_hold(struct scsi_qla_host *vha, struct qla_qpair *qpair,
5660 struct atio_from_isp *atio, uint8_t ha_locked)
5662 struct qla_hw_data *ha = vha->hw;
5663 unsigned long flags;
5665 if (ha->tgt.num_pend_cmds < Q_FULL_THRESH_HOLD(ha))
5669 spin_lock_irqsave(&ha->hardware_lock, flags);
5670 qlt_send_busy(qpair, atio, qla_sam_status);
5672 spin_unlock_irqrestore(&ha->hardware_lock, flags);
5677 /* ha->hardware_lock supposed to be held on entry */
5678 /* called via callback from qla2xxx */
5679 static void qlt_24xx_atio_pkt(struct scsi_qla_host *vha,
5680 struct atio_from_isp *atio, uint8_t ha_locked)
5682 struct qla_hw_data *ha = vha->hw;
5683 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
5685 unsigned long flags = 0;
5687 if (unlikely(tgt == NULL)) {
5688 ql_dbg(ql_dbg_tgt, vha, 0x3064,
5689 "ATIO pkt, but no tgt (ha %p)", ha);
5693 * In tgt_stop mode we also should allow all requests to pass.
5694 * Otherwise, some commands can stuck.
5697 tgt->atio_irq_cmd_count++;
5699 switch (atio->u.raw.entry_type) {
5701 if (unlikely(atio->u.isp24.exchange_addr ==
5702 cpu_to_le32(ATIO_EXCHANGE_ADDRESS_UNKNOWN))) {
5703 ql_dbg(ql_dbg_io, vha, 0x3065,
5704 "qla_target(%d): ATIO_TYPE7 "
5705 "received with UNKNOWN exchange address, "
5706 "sending QUEUE_FULL\n", vha->vp_idx);
5708 spin_lock_irqsave(&ha->hardware_lock, flags);
5709 qlt_send_busy(ha->base_qpair, atio, qla_sam_status);
5711 spin_unlock_irqrestore(&ha->hardware_lock,
5716 if (likely(atio->u.isp24.fcp_cmnd.task_mgmt_flags == 0)) {
5717 rc = qlt_chk_qfull_thresh_hold(vha, ha->base_qpair,
5720 tgt->atio_irq_cmd_count--;
5723 rc = qlt_handle_cmd_for_atio(vha, atio);
5725 rc = qlt_handle_task_mgmt(vha, atio);
5727 if (unlikely(rc != 0)) {
5729 spin_lock_irqsave(&ha->hardware_lock, flags);
5732 ql_dbg(ql_dbg_tgt, vha, 0xe05f,
5733 "qla_target: Unable to send command to target\n");
5736 ql_dbg(ql_dbg_tgt, vha, 0xe05f,
5737 "qla_target: Unable to send command to target, sending TERM EXCHANGE for rsp\n");
5738 qlt_send_term_exchange(ha->base_qpair, NULL,
5742 ql_dbg(ql_dbg_tgt, vha, 0xe060,
5743 "qla_target(%d): Unable to send command to target, sending BUSY status\n",
5745 qlt_send_busy(ha->base_qpair, atio,
5749 ql_dbg(ql_dbg_tgt, vha, 0xe060,
5750 "qla_target(%d): Unable to send command to target, sending BUSY status\n",
5752 qlt_send_busy(ha->base_qpair, atio,
5757 spin_unlock_irqrestore(&ha->hardware_lock,
5762 case IMMED_NOTIFY_TYPE:
5764 if (unlikely(atio->u.isp2x.entry_status != 0)) {
5765 ql_dbg(ql_dbg_tgt, vha, 0xe05b,
5766 "qla_target(%d): Received ATIO packet %x "
5767 "with error status %x\n", vha->vp_idx,
5768 atio->u.raw.entry_type,
5769 atio->u.isp2x.entry_status);
5772 ql_dbg(ql_dbg_tgt, vha, 0xe02e, "%s", "IMMED_NOTIFY ATIO");
5775 spin_lock_irqsave(&ha->hardware_lock, flags);
5776 qlt_handle_imm_notify(vha, (struct imm_ntfy_from_isp *)atio);
5778 spin_unlock_irqrestore(&ha->hardware_lock, flags);
5783 ql_dbg(ql_dbg_tgt, vha, 0xe05c,
5784 "qla_target(%d): Received unknown ATIO atio "
5785 "type %x\n", vha->vp_idx, atio->u.raw.entry_type);
5789 tgt->atio_irq_cmd_count--;
5793 * qpair lock is assume to be held
5794 * rc = 0 : send terminate & abts respond
5795 * rc != 0: do not send term & abts respond
5797 static int qlt_chk_unresolv_exchg(struct scsi_qla_host *vha,
5798 struct qla_qpair *qpair, struct abts_resp_from_24xx_fw *entry)
5800 struct qla_hw_data *ha = vha->hw;
5804 * Detect unresolved exchange. If the same ABTS is unable
5805 * to terminate an existing command and the same ABTS loops
5806 * between FW & Driver, then force FW dump. Under 1 jiff,
5807 * we should see multiple loops.
5809 if (qpair->retry_term_exchg_addr == entry->exchange_addr_to_abort &&
5810 qpair->retry_term_jiff == jiffies) {
5811 /* found existing exchange */
5812 qpair->retry_term_cnt++;
5813 if (qpair->retry_term_cnt >= 5) {
5815 qpair->retry_term_cnt = 0;
5816 ql_log(ql_log_warn, vha, 0xffff,
5817 "Unable to send ABTS Respond. Dumping firmware.\n");
5818 ql_dump_buffer(ql_dbg_tgt_mgt + ql_dbg_buffer,
5819 vha, 0xffff, (uint8_t *)entry, sizeof(*entry));
5821 if (qpair == ha->base_qpair)
5822 ha->isp_ops->fw_dump(vha);
5824 qla2xxx_dump_fw(vha);
5826 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
5827 qla2xxx_wake_dpc(vha);
5829 } else if (qpair->retry_term_jiff != jiffies) {
5830 qpair->retry_term_exchg_addr = entry->exchange_addr_to_abort;
5831 qpair->retry_term_cnt = 0;
5832 qpair->retry_term_jiff = jiffies;
5839 static void qlt_handle_abts_completion(struct scsi_qla_host *vha,
5840 struct rsp_que *rsp, response_t *pkt)
5842 struct abts_resp_from_24xx_fw *entry =
5843 (struct abts_resp_from_24xx_fw *)pkt;
5844 u32 h = pkt->handle & ~QLA_TGT_HANDLE_MASK;
5845 struct qla_tgt_mgmt_cmd *mcmd;
5846 struct qla_hw_data *ha = vha->hw;
5848 mcmd = qlt_ctio_to_cmd(vha, rsp, pkt->handle, pkt);
5849 if (mcmd == NULL && h != QLA_TGT_SKIP_HANDLE) {
5850 ql_dbg(ql_dbg_async, vha, 0xe064,
5851 "qla_target(%d): ABTS Comp without mcmd\n",
5858 vha->vha_tgt.qla_tgt->abts_resp_expected--;
5860 ql_dbg(ql_dbg_tgt, vha, 0xe038,
5861 "ABTS_RESP_24XX: compl_status %x\n",
5862 entry->compl_status);
5864 if (le16_to_cpu(entry->compl_status) != ABTS_RESP_COMPL_SUCCESS) {
5865 if (le32_to_cpu(entry->error_subcode1) == 0x1E &&
5866 le32_to_cpu(entry->error_subcode2) == 0) {
5867 if (qlt_chk_unresolv_exchg(vha, rsp->qpair, entry)) {
5868 ha->tgt.tgt_ops->free_mcmd(mcmd);
5871 qlt_24xx_retry_term_exchange(vha, rsp->qpair,
5874 ql_dbg(ql_dbg_tgt, vha, 0xe063,
5875 "qla_target(%d): ABTS_RESP_24XX failed %x (subcode %x:%x)",
5876 vha->vp_idx, entry->compl_status,
5877 entry->error_subcode1,
5878 entry->error_subcode2);
5879 ha->tgt.tgt_ops->free_mcmd(mcmd);
5882 ha->tgt.tgt_ops->free_mcmd(mcmd);
5886 /* ha->hardware_lock supposed to be held on entry */
5887 /* called via callback from qla2xxx */
5888 static void qlt_response_pkt(struct scsi_qla_host *vha,
5889 struct rsp_que *rsp, response_t *pkt)
5891 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
5893 if (unlikely(tgt == NULL)) {
5894 ql_dbg(ql_dbg_tgt, vha, 0xe05d,
5895 "qla_target(%d): Response pkt %x received, but no tgt (ha %p)\n",
5896 vha->vp_idx, pkt->entry_type, vha->hw);
5901 * In tgt_stop mode we also should allow all requests to pass.
5902 * Otherwise, some commands can stuck.
5905 switch (pkt->entry_type) {
5909 struct ctio7_from_24xx *entry = (struct ctio7_from_24xx *)pkt;
5911 qlt_do_ctio_completion(vha, rsp, entry->handle,
5912 le16_to_cpu(entry->status)|(pkt->entry_status << 16),
5917 case ACCEPT_TGT_IO_TYPE:
5919 struct atio_from_isp *atio = (struct atio_from_isp *)pkt;
5922 if (atio->u.isp2x.status !=
5923 cpu_to_le16(ATIO_CDB_VALID)) {
5924 ql_dbg(ql_dbg_tgt, vha, 0xe05e,
5925 "qla_target(%d): ATIO with error "
5926 "status %x received\n", vha->vp_idx,
5927 le16_to_cpu(atio->u.isp2x.status));
5931 rc = qlt_chk_qfull_thresh_hold(vha, rsp->qpair, atio, 1);
5935 rc = qlt_handle_cmd_for_atio(vha, atio);
5936 if (unlikely(rc != 0)) {
5939 ql_dbg(ql_dbg_tgt, vha, 0xe05f,
5940 "qla_target: Unable to send command to target\n");
5943 ql_dbg(ql_dbg_tgt, vha, 0xe05f,
5944 "qla_target: Unable to send command to target, sending TERM EXCHANGE for rsp\n");
5945 qlt_send_term_exchange(rsp->qpair, NULL,
5949 ql_dbg(ql_dbg_tgt, vha, 0xe060,
5950 "qla_target(%d): Unable to send command to target, sending BUSY status\n",
5952 qlt_send_busy(rsp->qpair, atio,
5956 ql_dbg(ql_dbg_tgt, vha, 0xe060,
5957 "qla_target(%d): Unable to send command to target, sending BUSY status\n",
5959 qlt_send_busy(rsp->qpair, atio,
5967 case CONTINUE_TGT_IO_TYPE:
5969 struct ctio_to_2xxx *entry = (struct ctio_to_2xxx *)pkt;
5971 qlt_do_ctio_completion(vha, rsp, entry->handle,
5972 le16_to_cpu(entry->status)|(pkt->entry_status << 16),
5979 struct ctio_to_2xxx *entry = (struct ctio_to_2xxx *)pkt;
5981 qlt_do_ctio_completion(vha, rsp, entry->handle,
5982 le16_to_cpu(entry->status)|(pkt->entry_status << 16),
5987 case IMMED_NOTIFY_TYPE:
5988 ql_dbg(ql_dbg_tgt, vha, 0xe035, "%s", "IMMED_NOTIFY\n");
5989 qlt_handle_imm_notify(vha, (struct imm_ntfy_from_isp *)pkt);
5992 case NOTIFY_ACK_TYPE:
5993 if (tgt->notify_ack_expected > 0) {
5994 struct nack_to_isp *entry = (struct nack_to_isp *)pkt;
5996 ql_dbg(ql_dbg_tgt, vha, 0xe036,
5997 "NOTIFY_ACK seq %08x status %x\n",
5998 le16_to_cpu(entry->u.isp2x.seq_id),
5999 le16_to_cpu(entry->u.isp2x.status));
6000 tgt->notify_ack_expected--;
6001 if (entry->u.isp2x.status !=
6002 cpu_to_le16(NOTIFY_ACK_SUCCESS)) {
6003 ql_dbg(ql_dbg_tgt, vha, 0xe061,
6004 "qla_target(%d): NOTIFY_ACK "
6005 "failed %x\n", vha->vp_idx,
6006 le16_to_cpu(entry->u.isp2x.status));
6009 ql_dbg(ql_dbg_tgt, vha, 0xe062,
6010 "qla_target(%d): Unexpected NOTIFY_ACK received\n",
6015 case ABTS_RECV_24XX:
6016 ql_dbg(ql_dbg_tgt, vha, 0xe037,
6017 "ABTS_RECV_24XX: instance %d\n", vha->vp_idx);
6018 qlt_24xx_handle_abts(vha, (struct abts_recv_from_24xx *)pkt);
6021 case ABTS_RESP_24XX:
6022 if (tgt->abts_resp_expected > 0) {
6023 qlt_handle_abts_completion(vha, rsp, pkt);
6025 ql_dbg(ql_dbg_tgt, vha, 0xe064,
6026 "qla_target(%d): Unexpected ABTS_RESP_24XX "
6027 "received\n", vha->vp_idx);
6032 ql_dbg(ql_dbg_tgt, vha, 0xe065,
6033 "qla_target(%d): Received unknown response pkt "
6034 "type %x\n", vha->vp_idx, pkt->entry_type);
6041 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
6043 void qlt_async_event(uint16_t code, struct scsi_qla_host *vha,
6046 struct qla_hw_data *ha = vha->hw;
6047 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
6050 if (!tgt || tgt->tgt_stop || tgt->tgt_stopped)
6053 if (((code == MBA_POINT_TO_POINT) || (code == MBA_CHG_IN_CONNECTION)) &&
6057 * In tgt_stop mode we also should allow all requests to pass.
6058 * Otherwise, some commands can stuck.
6063 case MBA_RESET: /* Reset */
6064 case MBA_SYSTEM_ERR: /* System Error */
6065 case MBA_REQ_TRANSFER_ERR: /* Request Transfer Error */
6066 case MBA_RSP_TRANSFER_ERR: /* Response Transfer Error */
6067 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03a,
6068 "qla_target(%d): System error async event %#x "
6069 "occurred", vha->vp_idx, code);
6071 case MBA_WAKEUP_THRES: /* Request Queue Wake-up. */
6072 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
6077 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03b,
6078 "qla_target(%d): Async LOOP_UP occurred "
6079 "(m[0]=%x, m[1]=%x, m[2]=%x, m[3]=%x)", vha->vp_idx,
6080 mailbox[0], mailbox[1], mailbox[2], mailbox[3]);
6081 if (tgt->link_reinit_iocb_pending) {
6082 qlt_send_notify_ack(ha->base_qpair,
6083 &tgt->link_reinit_iocb,
6085 tgt->link_reinit_iocb_pending = 0;
6090 case MBA_LIP_OCCURRED:
6093 case MBA_RSCN_UPDATE:
6094 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03c,
6095 "qla_target(%d): Async event %#x occurred "
6096 "(m[0]=%x, m[1]=%x, m[2]=%x, m[3]=%x)", vha->vp_idx, code,
6097 mailbox[0], mailbox[1], mailbox[2], mailbox[3]);
6100 case MBA_REJECTED_FCP_CMD:
6101 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf017,
6102 "qla_target(%d): Async event LS_REJECT occurred (m[0]=%x, m[1]=%x, m[2]=%x, m[3]=%x)",
6104 mailbox[0], mailbox[1], mailbox[2], mailbox[3]);
6106 if (mailbox[3] == 1) {
6107 /* exchange starvation. */
6108 vha->hw->exch_starvation++;
6109 if (vha->hw->exch_starvation > 5) {
6110 ql_log(ql_log_warn, vha, 0xd03a,
6111 "Exchange starvation-. Resetting RISC\n");
6113 vha->hw->exch_starvation = 0;
6114 if (IS_P3P_TYPE(vha->hw))
6115 set_bit(FCOE_CTX_RESET_NEEDED,
6118 set_bit(ISP_ABORT_NEEDED,
6120 qla2xxx_wake_dpc(vha);
6125 case MBA_PORT_UPDATE:
6126 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03d,
6127 "qla_target(%d): Port update async event %#x "
6128 "occurred: updating the ports database (m[0]=%x, m[1]=%x, "
6129 "m[2]=%x, m[3]=%x)", vha->vp_idx, code,
6130 mailbox[0], mailbox[1], mailbox[2], mailbox[3]);
6132 login_code = mailbox[2];
6133 if (login_code == 0x4) {
6134 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03e,
6135 "Async MB 2: Got PLOGI Complete\n");
6136 vha->hw->exch_starvation = 0;
6137 } else if (login_code == 0x7)
6138 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03f,
6139 "Async MB 2: Port Logged Out\n");
6147 static fc_port_t *qlt_get_port_database(struct scsi_qla_host *vha,
6150 fc_port_t *fcport, *tfcp, *del;
6152 unsigned long flags;
6155 fcport = qla2x00_alloc_fcport(vha, GFP_KERNEL);
6157 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06f,
6158 "qla_target(%d): Allocation of tmp FC port failed",
6163 fcport->loop_id = loop_id;
6165 rc = qla24xx_gpdb_wait(vha, fcport, 0);
6166 if (rc != QLA_SUCCESS) {
6167 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf070,
6168 "qla_target(%d): Failed to retrieve fcport "
6169 "information -- get_port_database() returned %x "
6170 "(loop_id=0x%04x)", vha->vp_idx, rc, loop_id);
6176 spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
6177 tfcp = qla2x00_find_fcport_by_wwpn(vha, fcport->port_name, 1);
6180 tfcp->d_id = fcport->d_id;
6181 tfcp->port_type = fcport->port_type;
6182 tfcp->supported_classes = fcport->supported_classes;
6183 tfcp->flags |= fcport->flags;
6184 tfcp->scan_state = QLA_FCPORT_FOUND;
6189 if (vha->hw->current_topology == ISP_CFG_F)
6190 fcport->flags |= FCF_FABRIC_DEVICE;
6192 list_add_tail(&fcport->list, &vha->vp_fcports);
6193 if (!IS_SW_RESV_ADDR(fcport->d_id))
6194 vha->fcport_count++;
6195 fcport->login_gen++;
6196 qla2x00_set_fcport_disc_state(fcport, DSC_LOGIN_COMPLETE);
6197 fcport->login_succ = 1;
6201 fcport->deleted = 0;
6202 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
6204 switch (vha->host->active_mode) {
6205 case MODE_INITIATOR:
6208 if (!IS_IIDMA_CAPABLE(vha->hw) || !vha->hw->flags.gpsc_supported) {
6209 qla24xx_sched_upd_fcport(fcport);
6211 ql_dbg(ql_dbg_disc, vha, 0x20ff,
6212 "%s %d %8phC post gpsc fcp_cnt %d\n",
6213 __func__, __LINE__, fcport->port_name, vha->fcport_count);
6214 qla24xx_post_gpsc_work(vha, fcport);
6224 qla2x00_free_fcport(del);
6229 /* Must be called under tgt_mutex */
6230 static struct fc_port *qlt_make_local_sess(struct scsi_qla_host *vha,
6233 struct fc_port *sess = NULL;
6234 fc_port_t *fcport = NULL;
6235 int rc, global_resets;
6236 uint16_t loop_id = 0;
6238 if (s_id.domain == 0xFF && s_id.area == 0xFC) {
6240 * This is Domain Controller, so it should be
6241 * OK to drop SCSI commands from it.
6243 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf042,
6244 "Unable to find initiator with S_ID %x:%x:%x",
6245 s_id.domain, s_id.area, s_id.al_pa);
6249 mutex_lock(&vha->vha_tgt.tgt_mutex);
6253 atomic_read(&vha->vha_tgt.qla_tgt->tgt_global_resets_count);
6255 rc = qla24xx_get_loop_id(vha, s_id, &loop_id);
6257 mutex_unlock(&vha->vha_tgt.tgt_mutex);
6259 ql_log(ql_log_info, vha, 0xf071,
6260 "qla_target(%d): Unable to find "
6261 "initiator with S_ID %x:%x:%x",
6262 vha->vp_idx, s_id.domain, s_id.area, s_id.al_pa);
6264 if (rc == -ENOENT) {
6265 qlt_port_logo_t logo;
6267 logo.id = be_to_port_id(s_id);
6269 qlt_send_first_logo(vha, &logo);
6275 fcport = qlt_get_port_database(vha, loop_id);
6277 mutex_unlock(&vha->vha_tgt.tgt_mutex);
6281 if (global_resets !=
6282 atomic_read(&vha->vha_tgt.qla_tgt->tgt_global_resets_count)) {
6283 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf043,
6284 "qla_target(%d): global reset during session discovery "
6285 "(counter was %d, new %d), retrying", vha->vp_idx,
6287 atomic_read(&vha->vha_tgt.
6288 qla_tgt->tgt_global_resets_count));
6292 sess = qlt_create_sess(vha, fcport, true);
6294 mutex_unlock(&vha->vha_tgt.tgt_mutex);
6299 static void qlt_abort_work(struct qla_tgt *tgt,
6300 struct qla_tgt_sess_work_param *prm)
6302 struct scsi_qla_host *vha = tgt->vha;
6303 struct qla_hw_data *ha = vha->hw;
6304 struct fc_port *sess = NULL;
6305 unsigned long flags = 0, flags2 = 0;
6309 spin_lock_irqsave(&ha->tgt.sess_lock, flags2);
6314 s_id = le_id_to_be(prm->abts.fcp_hdr_le.s_id);
6316 sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha, s_id);
6318 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags2);
6320 sess = qlt_make_local_sess(vha, s_id);
6321 /* sess has got an extra creation ref */
6323 spin_lock_irqsave(&ha->tgt.sess_lock, flags2);
6327 if (sess->deleted) {
6332 if (!kref_get_unless_zero(&sess->sess_kref)) {
6333 ql_dbg(ql_dbg_tgt_tmr, vha, 0xf01c,
6334 "%s: kref_get fail %8phC \n",
6335 __func__, sess->port_name);
6341 rc = __qlt_24xx_handle_abts(vha, &prm->abts, sess);
6342 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags2);
6344 ha->tgt.tgt_ops->put_sess(sess);
6351 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags2);
6354 spin_lock_irqsave(&ha->hardware_lock, flags);
6355 qlt_24xx_send_abts_resp(ha->base_qpair, &prm->abts,
6356 FCP_TMF_REJECTED, false);
6357 spin_unlock_irqrestore(&ha->hardware_lock, flags);
6360 static void qlt_tmr_work(struct qla_tgt *tgt,
6361 struct qla_tgt_sess_work_param *prm)
6363 struct atio_from_isp *a = &prm->tm_iocb2;
6364 struct scsi_qla_host *vha = tgt->vha;
6365 struct qla_hw_data *ha = vha->hw;
6366 struct fc_port *sess;
6367 unsigned long flags;
6374 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
6379 s_id = prm->tm_iocb2.u.isp24.fcp_hdr.s_id;
6380 sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha, s_id);
6382 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
6384 sess = qlt_make_local_sess(vha, s_id);
6385 /* sess has got an extra creation ref */
6387 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
6391 if (sess->deleted) {
6395 if (!kref_get_unless_zero(&sess->sess_kref)) {
6396 ql_dbg(ql_dbg_tgt_tmr, vha, 0xf020,
6397 "%s: kref_get fail %8phC\n",
6398 __func__, sess->port_name);
6404 fn = a->u.isp24.fcp_cmnd.task_mgmt_flags;
6406 scsilun_to_int((struct scsi_lun *)&a->u.isp24.fcp_cmnd.lun);
6408 rc = qlt_issue_task_mgmt(sess, unpacked_lun, fn, iocb, 0);
6409 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
6411 ha->tgt.tgt_ops->put_sess(sess);
6418 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
6420 qlt_send_term_exchange(ha->base_qpair, NULL, &prm->tm_iocb2, 1, 0);
6423 static void qlt_sess_work_fn(struct work_struct *work)
6425 struct qla_tgt *tgt = container_of(work, struct qla_tgt, sess_work);
6426 struct scsi_qla_host *vha = tgt->vha;
6427 unsigned long flags;
6429 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf000, "Sess work (tgt %p)", tgt);
6431 spin_lock_irqsave(&tgt->sess_work_lock, flags);
6432 while (!list_empty(&tgt->sess_works_list)) {
6433 struct qla_tgt_sess_work_param *prm = list_entry(
6434 tgt->sess_works_list.next, typeof(*prm),
6435 sess_works_list_entry);
6438 * This work can be scheduled on several CPUs at time, so we
6439 * must delete the entry to eliminate double processing
6441 list_del(&prm->sess_works_list_entry);
6443 spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
6445 switch (prm->type) {
6446 case QLA_TGT_SESS_WORK_ABORT:
6447 qlt_abort_work(tgt, prm);
6449 case QLA_TGT_SESS_WORK_TM:
6450 qlt_tmr_work(tgt, prm);
6457 spin_lock_irqsave(&tgt->sess_work_lock, flags);
6461 spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
6464 /* Must be called under tgt_host_action_mutex */
6465 int qlt_add_target(struct qla_hw_data *ha, struct scsi_qla_host *base_vha)
6467 struct qla_tgt *tgt;
6469 struct qla_qpair_hint *h;
6471 if (!QLA_TGT_MODE_ENABLED())
6474 if (!IS_TGT_MODE_CAPABLE(ha)) {
6475 ql_log(ql_log_warn, base_vha, 0xe070,
6476 "This adapter does not support target mode.\n");
6480 ql_dbg(ql_dbg_tgt, base_vha, 0xe03b,
6481 "Registering target for host %ld(%p).\n", base_vha->host_no, ha);
6483 BUG_ON(base_vha->vha_tgt.qla_tgt != NULL);
6485 tgt = kzalloc(sizeof(struct qla_tgt), GFP_KERNEL);
6487 ql_dbg(ql_dbg_tgt, base_vha, 0xe066,
6488 "Unable to allocate struct qla_tgt\n");
6492 tgt->qphints = kcalloc(ha->max_qpairs + 1,
6493 sizeof(struct qla_qpair_hint),
6495 if (!tgt->qphints) {
6497 ql_log(ql_log_warn, base_vha, 0x0197,
6498 "Unable to allocate qpair hints.\n");
6502 if (!(base_vha->host->hostt->supported_mode & MODE_TARGET))
6503 base_vha->host->hostt->supported_mode |= MODE_TARGET;
6505 rc = btree_init64(&tgt->lun_qpair_map);
6507 kfree(tgt->qphints);
6509 ql_log(ql_log_info, base_vha, 0x0198,
6510 "Unable to initialize lun_qpair_map btree\n");
6513 h = &tgt->qphints[0];
6514 h->qpair = ha->base_qpair;
6515 INIT_LIST_HEAD(&h->hint_elem);
6516 h->cpuid = ha->base_qpair->cpuid;
6517 list_add_tail(&h->hint_elem, &ha->base_qpair->hints_list);
6519 for (i = 0; i < ha->max_qpairs; i++) {
6520 unsigned long flags;
6522 struct qla_qpair *qpair = ha->queue_pair_map[i];
6524 h = &tgt->qphints[i + 1];
6525 INIT_LIST_HEAD(&h->hint_elem);
6528 spin_lock_irqsave(qpair->qp_lock_ptr, flags);
6529 list_add_tail(&h->hint_elem, &qpair->hints_list);
6530 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
6531 h->cpuid = qpair->cpuid;
6536 tgt->vha = base_vha;
6537 init_waitqueue_head(&tgt->waitQ);
6538 INIT_LIST_HEAD(&tgt->del_sess_list);
6539 spin_lock_init(&tgt->sess_work_lock);
6540 INIT_WORK(&tgt->sess_work, qlt_sess_work_fn);
6541 INIT_LIST_HEAD(&tgt->sess_works_list);
6542 atomic_set(&tgt->tgt_global_resets_count, 0);
6544 base_vha->vha_tgt.qla_tgt = tgt;
6546 ql_dbg(ql_dbg_tgt, base_vha, 0xe067,
6547 "qla_target(%d): using 64 Bit PCI addressing",
6550 tgt->sg_tablesize = QLA_TGT_MAX_SG_24XX(base_vha->req->length - 3);
6552 mutex_lock(&qla_tgt_mutex);
6553 list_add_tail(&tgt->tgt_list_entry, &qla_tgt_glist);
6554 mutex_unlock(&qla_tgt_mutex);
6556 if (ha->tgt.tgt_ops && ha->tgt.tgt_ops->add_target)
6557 ha->tgt.tgt_ops->add_target(base_vha);
6562 /* Must be called under tgt_host_action_mutex */
6563 int qlt_remove_target(struct qla_hw_data *ha, struct scsi_qla_host *vha)
6565 if (!vha->vha_tgt.qla_tgt)
6568 if (vha->fc_vport) {
6569 qlt_release(vha->vha_tgt.qla_tgt);
6573 /* free left over qfull cmds */
6574 qlt_init_term_exchange(vha);
6576 ql_dbg(ql_dbg_tgt, vha, 0xe03c, "Unregistering target for host %ld(%p)",
6578 qlt_release(vha->vha_tgt.qla_tgt);
6583 void qla_remove_hostmap(struct qla_hw_data *ha)
6585 struct scsi_qla_host *node;
6588 btree_for_each_safe32(&ha->host_map, key, node)
6589 btree_remove32(&ha->host_map, key);
6591 btree_destroy32(&ha->host_map);
6594 static void qlt_lport_dump(struct scsi_qla_host *vha, u64 wwpn,
6597 pr_debug("qla2xxx HW vha->node_name: %8phC\n", vha->node_name);
6598 pr_debug("qla2xxx HW vha->port_name: %8phC\n", vha->port_name);
6599 put_unaligned_be64(wwpn, b);
6600 pr_debug("qla2xxx passed configfs WWPN: %8phC\n", b);
6604 * qlt_lport_register - register lport with external module
6606 * @target_lport_ptr: pointer for tcm_qla2xxx specific lport data
6607 * @phys_wwpn: physical port WWPN
6608 * @npiv_wwpn: NPIV WWPN
6609 * @npiv_wwnn: NPIV WWNN
6610 * @callback: lport initialization callback for tcm_qla2xxx code
6612 int qlt_lport_register(void *target_lport_ptr, u64 phys_wwpn,
6613 u64 npiv_wwpn, u64 npiv_wwnn,
6614 int (*callback)(struct scsi_qla_host *, void *, u64, u64))
6616 struct qla_tgt *tgt;
6617 struct scsi_qla_host *vha;
6618 struct qla_hw_data *ha;
6619 struct Scsi_Host *host;
6620 unsigned long flags;
6624 mutex_lock(&qla_tgt_mutex);
6625 list_for_each_entry(tgt, &qla_tgt_glist, tgt_list_entry) {
6633 if (!(host->hostt->supported_mode & MODE_TARGET))
6636 if (vha->qlini_mode == QLA2XXX_INI_MODE_ENABLED)
6639 spin_lock_irqsave(&ha->hardware_lock, flags);
6640 if ((!npiv_wwpn || !npiv_wwnn) && host->active_mode & MODE_TARGET) {
6641 pr_debug("MODE_TARGET already active on qla2xxx(%d)\n",
6643 spin_unlock_irqrestore(&ha->hardware_lock, flags);
6646 if (tgt->tgt_stop) {
6647 pr_debug("MODE_TARGET in shutdown on qla2xxx(%d)\n",
6649 spin_unlock_irqrestore(&ha->hardware_lock, flags);
6652 spin_unlock_irqrestore(&ha->hardware_lock, flags);
6654 if (!scsi_host_get(host)) {
6655 ql_dbg(ql_dbg_tgt, vha, 0xe068,
6656 "Unable to scsi_host_get() for"
6657 " qla2xxx scsi_host\n");
6660 qlt_lport_dump(vha, phys_wwpn, b);
6662 if (memcmp(vha->port_name, b, WWN_SIZE)) {
6663 scsi_host_put(host);
6666 rc = (*callback)(vha, target_lport_ptr, npiv_wwpn, npiv_wwnn);
6668 scsi_host_put(host);
6670 mutex_unlock(&qla_tgt_mutex);
6673 mutex_unlock(&qla_tgt_mutex);
6677 EXPORT_SYMBOL(qlt_lport_register);
6680 * qlt_lport_deregister - Degister lport
6682 * @vha: Registered scsi_qla_host pointer
6684 void qlt_lport_deregister(struct scsi_qla_host *vha)
6686 struct qla_hw_data *ha = vha->hw;
6687 struct Scsi_Host *sh = vha->host;
6689 * Clear the target_lport_ptr qla_target_template pointer in qla_hw_data
6691 vha->vha_tgt.target_lport_ptr = NULL;
6692 ha->tgt.tgt_ops = NULL;
6694 * Release the Scsi_Host reference for the underlying qla2xxx host
6698 EXPORT_SYMBOL(qlt_lport_deregister);
6700 /* Must be called under HW lock */
6701 void qlt_set_mode(struct scsi_qla_host *vha)
6703 switch (vha->qlini_mode) {
6704 case QLA2XXX_INI_MODE_DISABLED:
6705 case QLA2XXX_INI_MODE_EXCLUSIVE:
6706 vha->host->active_mode = MODE_TARGET;
6708 case QLA2XXX_INI_MODE_ENABLED:
6709 vha->host->active_mode = MODE_INITIATOR;
6711 case QLA2XXX_INI_MODE_DUAL:
6712 vha->host->active_mode = MODE_DUAL;
6719 /* Must be called under HW lock */
6720 static void qlt_clear_mode(struct scsi_qla_host *vha)
6722 switch (vha->qlini_mode) {
6723 case QLA2XXX_INI_MODE_DISABLED:
6724 vha->host->active_mode = MODE_UNKNOWN;
6726 case QLA2XXX_INI_MODE_EXCLUSIVE:
6727 vha->host->active_mode = MODE_INITIATOR;
6729 case QLA2XXX_INI_MODE_ENABLED:
6730 case QLA2XXX_INI_MODE_DUAL:
6731 vha->host->active_mode = MODE_INITIATOR;
6739 * qla_tgt_enable_vha - NO LOCK HELD
6741 * host_reset, bring up w/ Target Mode Enabled
6744 qlt_enable_vha(struct scsi_qla_host *vha)
6746 struct qla_hw_data *ha = vha->hw;
6747 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
6748 unsigned long flags;
6749 scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
6752 ql_dbg(ql_dbg_tgt, vha, 0xe069,
6753 "Unable to locate qla_tgt pointer from"
6754 " struct qla_hw_data\n");
6758 if (vha->qlini_mode == QLA2XXX_INI_MODE_ENABLED)
6761 if (ha->tgt.num_act_qpairs > ha->max_qpairs)
6762 ha->tgt.num_act_qpairs = ha->max_qpairs;
6763 spin_lock_irqsave(&ha->hardware_lock, flags);
6764 tgt->tgt_stopped = 0;
6766 spin_unlock_irqrestore(&ha->hardware_lock, flags);
6768 mutex_lock(&ha->optrom_mutex);
6769 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf021,
6772 qla24xx_disable_vp(vha);
6773 qla24xx_enable_vp(vha);
6775 set_bit(ISP_ABORT_NEEDED, &base_vha->dpc_flags);
6776 qla2xxx_wake_dpc(base_vha);
6777 WARN_ON_ONCE(qla2x00_wait_for_hba_online(base_vha) !=
6780 mutex_unlock(&ha->optrom_mutex);
6782 EXPORT_SYMBOL(qlt_enable_vha);
6785 * qla_tgt_disable_vha - NO LOCK HELD
6787 * Disable Target Mode and reset the adapter
6789 static void qlt_disable_vha(struct scsi_qla_host *vha)
6791 struct qla_hw_data *ha = vha->hw;
6792 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
6793 unsigned long flags;
6796 ql_dbg(ql_dbg_tgt, vha, 0xe06a,
6797 "Unable to locate qla_tgt pointer from"
6798 " struct qla_hw_data\n");
6803 spin_lock_irqsave(&ha->hardware_lock, flags);
6804 qlt_clear_mode(vha);
6805 spin_unlock_irqrestore(&ha->hardware_lock, flags);
6807 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
6808 qla2xxx_wake_dpc(vha);
6811 * We are expecting the offline state.
6812 * QLA_FUNCTION_FAILED means that adapter is offline.
6814 if (qla2x00_wait_for_hba_online(vha) != QLA_SUCCESS)
6815 ql_dbg(ql_dbg_tgt, vha, 0xe081,
6816 "adapter is offline\n");
6820 * Called from qla_init.c:qla24xx_vport_create() contex to setup
6821 * the target mode specific struct scsi_qla_host and struct qla_hw_data
6825 qlt_vport_create(struct scsi_qla_host *vha, struct qla_hw_data *ha)
6827 vha->vha_tgt.qla_tgt = NULL;
6829 mutex_init(&vha->vha_tgt.tgt_mutex);
6830 mutex_init(&vha->vha_tgt.tgt_host_action_mutex);
6832 qlt_clear_mode(vha);
6835 * NOTE: Currently the value is kept the same for <24xx and
6836 * >=24xx ISPs. If it is necessary to change it,
6837 * the check should be added for specific ISPs,
6838 * assigning the value appropriately.
6840 ha->tgt.atio_q_length = ATIO_ENTRY_CNT_24XX;
6842 qlt_add_target(ha, vha);
6846 qlt_rff_id(struct scsi_qla_host *vha)
6850 * FC-4 Feature bit 0 indicates target functionality to the name server.
6852 if (qla_tgt_mode_enabled(vha)) {
6853 fc4_feature = BIT_0;
6854 } else if (qla_ini_mode_enabled(vha)) {
6855 fc4_feature = BIT_1;
6856 } else if (qla_dual_mode_enabled(vha))
6857 fc4_feature = BIT_0 | BIT_1;
6863 * qlt_init_atio_q_entries() - Initializes ATIO queue entries.
6866 * Beginning of ATIO ring has initialization control block already built
6867 * by nvram config routine.
6869 * Returns 0 on success.
6872 qlt_init_atio_q_entries(struct scsi_qla_host *vha)
6874 struct qla_hw_data *ha = vha->hw;
6876 struct atio_from_isp *pkt = (struct atio_from_isp *)ha->tgt.atio_ring;
6878 if (qla_ini_mode_enabled(vha))
6881 for (cnt = 0; cnt < ha->tgt.atio_q_length; cnt++) {
6882 pkt->u.raw.signature = cpu_to_le32(ATIO_PROCESSED);
6889 * qlt_24xx_process_atio_queue() - Process ATIO queue entries.
6890 * @ha: SCSI driver HA context
6893 qlt_24xx_process_atio_queue(struct scsi_qla_host *vha, uint8_t ha_locked)
6895 struct qla_hw_data *ha = vha->hw;
6896 struct atio_from_isp *pkt;
6899 if (!ha->flags.fw_started)
6902 while ((ha->tgt.atio_ring_ptr->signature != ATIO_PROCESSED) ||
6903 fcpcmd_is_corrupted(ha->tgt.atio_ring_ptr)) {
6904 pkt = (struct atio_from_isp *)ha->tgt.atio_ring_ptr;
6905 cnt = pkt->u.raw.entry_count;
6907 if (unlikely(fcpcmd_is_corrupted(ha->tgt.atio_ring_ptr))) {
6909 * This packet is corrupted. The header + payload
6910 * can not be trusted. There is no point in passing
6913 ql_log(ql_log_warn, vha, 0xd03c,
6914 "corrupted fcp frame SID[%3phN] OXID[%04x] EXCG[%x] %64phN\n",
6915 &pkt->u.isp24.fcp_hdr.s_id,
6916 be16_to_cpu(pkt->u.isp24.fcp_hdr.ox_id),
6917 pkt->u.isp24.exchange_addr, pkt);
6919 adjust_corrupted_atio(pkt);
6920 qlt_send_term_exchange(ha->base_qpair, NULL, pkt,
6923 qlt_24xx_atio_pkt_all_vps(vha,
6924 (struct atio_from_isp *)pkt, ha_locked);
6927 for (i = 0; i < cnt; i++) {
6928 ha->tgt.atio_ring_index++;
6929 if (ha->tgt.atio_ring_index == ha->tgt.atio_q_length) {
6930 ha->tgt.atio_ring_index = 0;
6931 ha->tgt.atio_ring_ptr = ha->tgt.atio_ring;
6933 ha->tgt.atio_ring_ptr++;
6935 pkt->u.raw.signature = cpu_to_le32(ATIO_PROCESSED);
6936 pkt = (struct atio_from_isp *)ha->tgt.atio_ring_ptr;
6941 /* Adjust ring index */
6942 wrt_reg_dword(ISP_ATIO_Q_OUT(vha), ha->tgt.atio_ring_index);
6946 qlt_24xx_config_rings(struct scsi_qla_host *vha)
6948 struct qla_hw_data *ha = vha->hw;
6949 struct qla_msix_entry *msix = &ha->msix_entries[2];
6950 struct init_cb_24xx *icb = (struct init_cb_24xx *)ha->init_cb;
6952 if (!QLA_TGT_MODE_ENABLED())
6955 wrt_reg_dword(ISP_ATIO_Q_IN(vha), 0);
6956 wrt_reg_dword(ISP_ATIO_Q_OUT(vha), 0);
6957 rd_reg_dword(ISP_ATIO_Q_OUT(vha));
6959 if (ha->flags.msix_enabled) {
6960 if (IS_QLA83XX(ha) || IS_QLA27XX(ha) || IS_QLA28XX(ha)) {
6961 if (IS_QLA2071(ha)) {
6962 /* 4 ports Baker: Enable Interrupt Handshake */
6964 icb->firmware_options_2 |= cpu_to_le32(BIT_26);
6966 icb->msix_atio = cpu_to_le16(msix->entry);
6967 icb->firmware_options_2 &= cpu_to_le32(~BIT_26);
6969 ql_dbg(ql_dbg_init, vha, 0xf072,
6970 "Registering ICB vector 0x%x for atio que.\n",
6975 if (IS_QLA83XX(ha) || IS_QLA27XX(ha) || IS_QLA28XX(ha)) {
6977 icb->firmware_options_2 |= cpu_to_le32(BIT_26);
6978 ql_dbg(ql_dbg_init, vha, 0xf072,
6979 "%s: Use INTx for ATIOQ.\n", __func__);
6985 qlt_24xx_config_nvram_stage1(struct scsi_qla_host *vha, struct nvram_24xx *nv)
6987 struct qla_hw_data *ha = vha->hw;
6990 if (!QLA_TGT_MODE_ENABLED())
6993 if (qla_tgt_mode_enabled(vha) || qla_dual_mode_enabled(vha)) {
6994 if (!ha->tgt.saved_set) {
6995 /* We save only once */
6996 ha->tgt.saved_exchange_count = nv->exchange_count;
6997 ha->tgt.saved_firmware_options_1 =
6998 nv->firmware_options_1;
6999 ha->tgt.saved_firmware_options_2 =
7000 nv->firmware_options_2;
7001 ha->tgt.saved_firmware_options_3 =
7002 nv->firmware_options_3;
7003 ha->tgt.saved_set = 1;
7006 if (qla_tgt_mode_enabled(vha))
7007 nv->exchange_count = cpu_to_le16(0xFFFF);
7009 nv->exchange_count = cpu_to_le16(vha->ql2xexchoffld);
7011 /* Enable target mode */
7012 nv->firmware_options_1 |= cpu_to_le32(BIT_4);
7014 /* Disable ini mode, if requested */
7015 if (qla_tgt_mode_enabled(vha))
7016 nv->firmware_options_1 |= cpu_to_le32(BIT_5);
7018 /* Disable Full Login after LIP */
7019 nv->firmware_options_1 &= cpu_to_le32(~BIT_13);
7020 /* Enable initial LIP */
7021 nv->firmware_options_1 &= cpu_to_le32(~BIT_9);
7022 if (ql2xtgt_tape_enable)
7023 /* Enable FC Tape support */
7024 nv->firmware_options_2 |= cpu_to_le32(BIT_12);
7026 /* Disable FC Tape support */
7027 nv->firmware_options_2 &= cpu_to_le32(~BIT_12);
7029 /* Disable Full Login after LIP */
7030 nv->host_p &= cpu_to_le32(~BIT_10);
7033 * clear BIT 15 explicitly as we have seen at least
7034 * a couple of instances where this was set and this
7035 * was causing the firmware to not be initialized.
7037 nv->firmware_options_1 &= cpu_to_le32(~BIT_15);
7038 /* Enable target PRLI control */
7039 nv->firmware_options_2 |= cpu_to_le32(BIT_14);
7041 if (IS_QLA25XX(ha)) {
7042 /* Change Loop-prefer to Pt-Pt */
7043 tmp = ~(BIT_4|BIT_5|BIT_6);
7044 nv->firmware_options_2 &= cpu_to_le32(tmp);
7046 nv->firmware_options_2 |= cpu_to_le32(tmp);
7049 if (ha->tgt.saved_set) {
7050 nv->exchange_count = ha->tgt.saved_exchange_count;
7051 nv->firmware_options_1 =
7052 ha->tgt.saved_firmware_options_1;
7053 nv->firmware_options_2 =
7054 ha->tgt.saved_firmware_options_2;
7055 nv->firmware_options_3 =
7056 ha->tgt.saved_firmware_options_3;
7061 if (ha->base_qpair->enable_class_2) {
7062 if (vha->flags.init_done)
7063 fc_host_supported_classes(vha->host) =
7064 FC_COS_CLASS2 | FC_COS_CLASS3;
7066 nv->firmware_options_2 |= cpu_to_le32(BIT_8);
7068 if (vha->flags.init_done)
7069 fc_host_supported_classes(vha->host) = FC_COS_CLASS3;
7071 nv->firmware_options_2 &= ~cpu_to_le32(BIT_8);
7076 qlt_24xx_config_nvram_stage2(struct scsi_qla_host *vha,
7077 struct init_cb_24xx *icb)
7079 struct qla_hw_data *ha = vha->hw;
7081 if (!QLA_TGT_MODE_ENABLED())
7084 if (ha->tgt.node_name_set) {
7085 memcpy(icb->node_name, ha->tgt.tgt_node_name, WWN_SIZE);
7086 icb->firmware_options_1 |= cpu_to_le32(BIT_14);
7091 qlt_81xx_config_nvram_stage1(struct scsi_qla_host *vha, struct nvram_81xx *nv)
7093 struct qla_hw_data *ha = vha->hw;
7096 if (!QLA_TGT_MODE_ENABLED())
7099 if (qla_tgt_mode_enabled(vha) || qla_dual_mode_enabled(vha)) {
7100 if (!ha->tgt.saved_set) {
7101 /* We save only once */
7102 ha->tgt.saved_exchange_count = nv->exchange_count;
7103 ha->tgt.saved_firmware_options_1 =
7104 nv->firmware_options_1;
7105 ha->tgt.saved_firmware_options_2 =
7106 nv->firmware_options_2;
7107 ha->tgt.saved_firmware_options_3 =
7108 nv->firmware_options_3;
7109 ha->tgt.saved_set = 1;
7112 if (qla_tgt_mode_enabled(vha))
7113 nv->exchange_count = cpu_to_le16(0xFFFF);
7115 nv->exchange_count = cpu_to_le16(vha->ql2xexchoffld);
7117 /* Enable target mode */
7118 nv->firmware_options_1 |= cpu_to_le32(BIT_4);
7120 /* Disable ini mode, if requested */
7121 if (qla_tgt_mode_enabled(vha))
7122 nv->firmware_options_1 |= cpu_to_le32(BIT_5);
7123 /* Disable Full Login after LIP */
7124 nv->firmware_options_1 &= cpu_to_le32(~BIT_13);
7125 /* Enable initial LIP */
7126 nv->firmware_options_1 &= cpu_to_le32(~BIT_9);
7128 * clear BIT 15 explicitly as we have seen at
7129 * least a couple of instances where this was set
7130 * and this was causing the firmware to not be
7133 nv->firmware_options_1 &= cpu_to_le32(~BIT_15);
7134 if (ql2xtgt_tape_enable)
7135 /* Enable FC tape support */
7136 nv->firmware_options_2 |= cpu_to_le32(BIT_12);
7138 /* Disable FC tape support */
7139 nv->firmware_options_2 &= cpu_to_le32(~BIT_12);
7141 /* Disable Full Login after LIP */
7142 nv->host_p &= cpu_to_le32(~BIT_10);
7143 /* Enable target PRLI control */
7144 nv->firmware_options_2 |= cpu_to_le32(BIT_14);
7146 /* Change Loop-prefer to Pt-Pt */
7147 tmp = ~(BIT_4|BIT_5|BIT_6);
7148 nv->firmware_options_2 &= cpu_to_le32(tmp);
7150 nv->firmware_options_2 |= cpu_to_le32(tmp);
7152 if (ha->tgt.saved_set) {
7153 nv->exchange_count = ha->tgt.saved_exchange_count;
7154 nv->firmware_options_1 =
7155 ha->tgt.saved_firmware_options_1;
7156 nv->firmware_options_2 =
7157 ha->tgt.saved_firmware_options_2;
7158 nv->firmware_options_3 =
7159 ha->tgt.saved_firmware_options_3;
7164 if (ha->base_qpair->enable_class_2) {
7165 if (vha->flags.init_done)
7166 fc_host_supported_classes(vha->host) =
7167 FC_COS_CLASS2 | FC_COS_CLASS3;
7169 nv->firmware_options_2 |= cpu_to_le32(BIT_8);
7171 if (vha->flags.init_done)
7172 fc_host_supported_classes(vha->host) = FC_COS_CLASS3;
7174 nv->firmware_options_2 &= ~cpu_to_le32(BIT_8);
7179 qlt_81xx_config_nvram_stage2(struct scsi_qla_host *vha,
7180 struct init_cb_81xx *icb)
7182 struct qla_hw_data *ha = vha->hw;
7184 if (!QLA_TGT_MODE_ENABLED())
7187 if (ha->tgt.node_name_set) {
7188 memcpy(icb->node_name, ha->tgt.tgt_node_name, WWN_SIZE);
7189 icb->firmware_options_1 |= cpu_to_le32(BIT_14);
7194 qlt_83xx_iospace_config(struct qla_hw_data *ha)
7196 if (!QLA_TGT_MODE_ENABLED())
7199 ha->msix_count += 1; /* For ATIO Q */
7204 qlt_modify_vp_config(struct scsi_qla_host *vha,
7205 struct vp_config_entry_24xx *vpmod)
7207 /* enable target mode. Bit5 = 1 => disable */
7208 if (qla_tgt_mode_enabled(vha) || qla_dual_mode_enabled(vha))
7209 vpmod->options_idx1 &= ~BIT_5;
7211 /* Disable ini mode, if requested. bit4 = 1 => disable */
7212 if (qla_tgt_mode_enabled(vha))
7213 vpmod->options_idx1 &= ~BIT_4;
7217 qlt_probe_one_stage1(struct scsi_qla_host *base_vha, struct qla_hw_data *ha)
7219 mutex_init(&base_vha->vha_tgt.tgt_mutex);
7220 if (!QLA_TGT_MODE_ENABLED())
7223 if ((ql2xenablemsix == 0) || IS_QLA83XX(ha) || IS_QLA27XX(ha) ||
7225 ISP_ATIO_Q_IN(base_vha) = &ha->mqiobase->isp25mq.atio_q_in;
7226 ISP_ATIO_Q_OUT(base_vha) = &ha->mqiobase->isp25mq.atio_q_out;
7228 ISP_ATIO_Q_IN(base_vha) = &ha->iobase->isp24.atio_q_in;
7229 ISP_ATIO_Q_OUT(base_vha) = &ha->iobase->isp24.atio_q_out;
7232 mutex_init(&base_vha->vha_tgt.tgt_host_action_mutex);
7234 INIT_LIST_HEAD(&base_vha->unknown_atio_list);
7235 INIT_DELAYED_WORK(&base_vha->unknown_atio_work,
7236 qlt_unknown_atio_work_fn);
7238 qlt_clear_mode(base_vha);
7240 qlt_update_vp_map(base_vha, SET_VP_IDX);
7244 qla83xx_msix_atio_q(int irq, void *dev_id)
7246 struct rsp_que *rsp;
7247 scsi_qla_host_t *vha;
7248 struct qla_hw_data *ha;
7249 unsigned long flags;
7251 rsp = (struct rsp_que *) dev_id;
7253 vha = pci_get_drvdata(ha->pdev);
7255 spin_lock_irqsave(&ha->tgt.atio_lock, flags);
7257 qlt_24xx_process_atio_queue(vha, 0);
7259 spin_unlock_irqrestore(&ha->tgt.atio_lock, flags);
7265 qlt_handle_abts_recv_work(struct work_struct *work)
7267 struct qla_tgt_sess_op *op = container_of(work,
7268 struct qla_tgt_sess_op, work);
7269 scsi_qla_host_t *vha = op->vha;
7270 struct qla_hw_data *ha = vha->hw;
7271 unsigned long flags;
7273 if (qla2x00_reset_active(vha) ||
7274 (op->chip_reset != ha->base_qpair->chip_reset))
7277 spin_lock_irqsave(&ha->tgt.atio_lock, flags);
7278 qlt_24xx_process_atio_queue(vha, 0);
7279 spin_unlock_irqrestore(&ha->tgt.atio_lock, flags);
7281 spin_lock_irqsave(&ha->hardware_lock, flags);
7282 qlt_response_pkt_all_vps(vha, op->rsp, (response_t *)&op->atio);
7283 spin_unlock_irqrestore(&ha->hardware_lock, flags);
7289 qlt_handle_abts_recv(struct scsi_qla_host *vha, struct rsp_que *rsp,
7292 struct qla_tgt_sess_op *op;
7294 op = kzalloc(sizeof(*op), GFP_ATOMIC);
7297 /* do not reach for ATIO queue here. This is best effort err
7298 * recovery at this point.
7300 qlt_response_pkt_all_vps(vha, rsp, pkt);
7304 memcpy(&op->atio, pkt, sizeof(*pkt));
7306 op->chip_reset = vha->hw->base_qpair->chip_reset;
7308 INIT_WORK(&op->work, qlt_handle_abts_recv_work);
7309 queue_work(qla_tgt_wq, &op->work);
7314 qlt_mem_alloc(struct qla_hw_data *ha)
7316 if (!QLA_TGT_MODE_ENABLED())
7319 ha->tgt.tgt_vp_map = kcalloc(MAX_MULTI_ID_FABRIC,
7320 sizeof(struct qla_tgt_vp_map),
7322 if (!ha->tgt.tgt_vp_map)
7325 ha->tgt.atio_ring = dma_alloc_coherent(&ha->pdev->dev,
7326 (ha->tgt.atio_q_length + 1) * sizeof(struct atio_from_isp),
7327 &ha->tgt.atio_dma, GFP_KERNEL);
7328 if (!ha->tgt.atio_ring) {
7329 kfree(ha->tgt.tgt_vp_map);
7336 qlt_mem_free(struct qla_hw_data *ha)
7338 if (!QLA_TGT_MODE_ENABLED())
7341 if (ha->tgt.atio_ring) {
7342 dma_free_coherent(&ha->pdev->dev, (ha->tgt.atio_q_length + 1) *
7343 sizeof(struct atio_from_isp), ha->tgt.atio_ring,
7346 ha->tgt.atio_ring = NULL;
7347 ha->tgt.atio_dma = 0;
7348 kfree(ha->tgt.tgt_vp_map);
7349 ha->tgt.tgt_vp_map = NULL;
7352 /* vport_slock to be held by the caller */
7354 qlt_update_vp_map(struct scsi_qla_host *vha, int cmd)
7360 key = vha->d_id.b24;
7364 if (!QLA_TGT_MODE_ENABLED())
7366 vha->hw->tgt.tgt_vp_map[vha->vp_idx].vha = vha;
7369 slot = btree_lookup32(&vha->hw->host_map, key);
7371 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf018,
7372 "Save vha in host_map %p %06x\n", vha, key);
7373 rc = btree_insert32(&vha->hw->host_map,
7374 key, vha, GFP_ATOMIC);
7376 ql_log(ql_log_info, vha, 0xd03e,
7377 "Unable to insert s_id into host_map: %06x\n",
7381 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf019,
7382 "replace existing vha in host_map %p %06x\n", vha, key);
7383 btree_update32(&vha->hw->host_map, key, vha);
7386 if (!QLA_TGT_MODE_ENABLED())
7388 vha->hw->tgt.tgt_vp_map[vha->vp_idx].vha = NULL;
7391 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01a,
7392 "clear vha in host_map %p %06x\n", vha, key);
7393 slot = btree_lookup32(&vha->hw->host_map, key);
7395 btree_remove32(&vha->hw->host_map, key);
7401 void qlt_update_host_map(struct scsi_qla_host *vha, port_id_t id)
7404 if (!vha->d_id.b24) {
7406 qlt_update_vp_map(vha, SET_AL_PA);
7407 } else if (vha->d_id.b24 != id.b24) {
7408 qlt_update_vp_map(vha, RESET_AL_PA);
7410 qlt_update_vp_map(vha, SET_AL_PA);
7414 static int __init qlt_parse_ini_mode(void)
7416 if (strcasecmp(qlini_mode, QLA2XXX_INI_MODE_STR_EXCLUSIVE) == 0)
7417 ql2x_ini_mode = QLA2XXX_INI_MODE_EXCLUSIVE;
7418 else if (strcasecmp(qlini_mode, QLA2XXX_INI_MODE_STR_DISABLED) == 0)
7419 ql2x_ini_mode = QLA2XXX_INI_MODE_DISABLED;
7420 else if (strcasecmp(qlini_mode, QLA2XXX_INI_MODE_STR_ENABLED) == 0)
7421 ql2x_ini_mode = QLA2XXX_INI_MODE_ENABLED;
7422 else if (strcasecmp(qlini_mode, QLA2XXX_INI_MODE_STR_DUAL) == 0)
7423 ql2x_ini_mode = QLA2XXX_INI_MODE_DUAL;
7430 int __init qlt_init(void)
7434 BUILD_BUG_ON(sizeof(struct ctio7_to_24xx) != 64);
7435 BUILD_BUG_ON(sizeof(struct ctio_to_2xxx) != 64);
7437 if (!qlt_parse_ini_mode()) {
7438 ql_log(ql_log_fatal, NULL, 0xe06b,
7439 "qlt_parse_ini_mode() failed\n");
7443 if (!QLA_TGT_MODE_ENABLED())
7446 qla_tgt_mgmt_cmd_cachep = kmem_cache_create("qla_tgt_mgmt_cmd_cachep",
7447 sizeof(struct qla_tgt_mgmt_cmd), __alignof__(struct
7448 qla_tgt_mgmt_cmd), 0, NULL);
7449 if (!qla_tgt_mgmt_cmd_cachep) {
7450 ql_log(ql_log_fatal, NULL, 0xd04b,
7451 "kmem_cache_create for qla_tgt_mgmt_cmd_cachep failed\n");
7455 qla_tgt_plogi_cachep = kmem_cache_create("qla_tgt_plogi_cachep",
7456 sizeof(struct qlt_plogi_ack_t), __alignof__(struct qlt_plogi_ack_t),
7459 if (!qla_tgt_plogi_cachep) {
7460 ql_log(ql_log_fatal, NULL, 0xe06d,
7461 "kmem_cache_create for qla_tgt_plogi_cachep failed\n");
7463 goto out_mgmt_cmd_cachep;
7466 qla_tgt_mgmt_cmd_mempool = mempool_create(25, mempool_alloc_slab,
7467 mempool_free_slab, qla_tgt_mgmt_cmd_cachep);
7468 if (!qla_tgt_mgmt_cmd_mempool) {
7469 ql_log(ql_log_fatal, NULL, 0xe06e,
7470 "mempool_create for qla_tgt_mgmt_cmd_mempool failed\n");
7472 goto out_plogi_cachep;
7475 qla_tgt_wq = alloc_workqueue("qla_tgt_wq", 0, 0);
7477 ql_log(ql_log_fatal, NULL, 0xe06f,
7478 "alloc_workqueue for qla_tgt_wq failed\n");
7480 goto out_cmd_mempool;
7483 * Return 1 to signal that initiator-mode is being disabled
7485 return (ql2x_ini_mode == QLA2XXX_INI_MODE_DISABLED) ? 1 : 0;
7488 mempool_destroy(qla_tgt_mgmt_cmd_mempool);
7490 kmem_cache_destroy(qla_tgt_plogi_cachep);
7491 out_mgmt_cmd_cachep:
7492 kmem_cache_destroy(qla_tgt_mgmt_cmd_cachep);
7498 if (!QLA_TGT_MODE_ENABLED())
7501 destroy_workqueue(qla_tgt_wq);
7502 mempool_destroy(qla_tgt_mgmt_cmd_mempool);
7503 kmem_cache_destroy(qla_tgt_plogi_cachep);
7504 kmem_cache_destroy(qla_tgt_mgmt_cmd_cachep);