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);
623 kref_put(&sp->cmd_kref, qla2x00_sp_release);
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);
659 qla2x00_init_async_sp(sp, qla2x00_get_async_timeout(vha) + 2,
660 qla2x00_async_nack_sp_done);
662 sp->u.iocb_cmd.u.nack.ntfy = ntfy;
664 ql_dbg(ql_dbg_disc, vha, 0x20f4,
665 "Async-%s %8phC hndl %x %s\n",
666 sp->name, fcport->port_name, sp->handle, c);
668 rval = qla2x00_start_sp(sp);
669 if (rval != QLA_SUCCESS)
675 kref_put(&sp->cmd_kref, qla2x00_sp_release);
677 fcport->flags &= ~FCF_ASYNC_SENT;
681 void qla24xx_do_nack_work(struct scsi_qla_host *vha, struct qla_work_evt *e)
685 switch (e->u.nack.type) {
687 t = e->u.nack.fcport;
688 flush_work(&t->del_work);
689 flush_work(&t->free_work);
690 mutex_lock(&vha->vha_tgt.tgt_mutex);
691 t = qlt_create_sess(vha, e->u.nack.fcport, 0);
692 mutex_unlock(&vha->vha_tgt.tgt_mutex);
694 ql_log(ql_log_info, vha, 0xd034,
695 "%s create sess success %p", __func__, t);
696 /* create sess has an extra kref */
697 vha->hw->tgt.tgt_ops->put_sess(e->u.nack.fcport);
701 qla24xx_async_notify_ack(vha, e->u.nack.fcport,
702 (struct imm_ntfy_from_isp *)e->u.nack.iocb, e->u.nack.type);
705 void qla24xx_delete_sess_fn(struct work_struct *work)
707 fc_port_t *fcport = container_of(work, struct fc_port, del_work);
708 struct qla_hw_data *ha = NULL;
710 if (!fcport || !fcport->vha || !fcport->vha->hw)
713 ha = fcport->vha->hw;
715 if (fcport->se_sess) {
716 ha->tgt.tgt_ops->shutdown_sess(fcport);
717 ha->tgt.tgt_ops->put_sess(fcport);
719 qlt_unreg_sess(fcport);
724 * Called from qla2x00_reg_remote_port()
726 void qlt_fc_port_added(struct scsi_qla_host *vha, fc_port_t *fcport)
728 struct qla_hw_data *ha = vha->hw;
729 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
730 struct fc_port *sess = fcport;
733 if (!vha->hw->tgt.tgt_ops)
736 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
738 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
742 if (fcport->disc_state == DSC_DELETE_PEND) {
743 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
747 if (!sess->se_sess) {
748 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
750 mutex_lock(&vha->vha_tgt.tgt_mutex);
751 sess = qlt_create_sess(vha, fcport, false);
752 mutex_unlock(&vha->vha_tgt.tgt_mutex);
754 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
756 if (fcport->fw_login_state == DSC_LS_PRLI_COMP) {
757 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
761 if (!kref_get_unless_zero(&sess->sess_kref)) {
762 ql_dbg(ql_dbg_disc, vha, 0x2107,
763 "%s: kref_get fail sess %8phC \n",
764 __func__, sess->port_name);
765 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
769 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04c,
770 "qla_target(%u): %ssession for port %8phC "
771 "(loop ID %d) reappeared\n", vha->vp_idx,
772 sess->local ? "local " : "", sess->port_name, sess->loop_id);
774 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf007,
775 "Reappeared sess %p\n", sess);
777 ha->tgt.tgt_ops->update_sess(sess, fcport->d_id,
779 (fcport->flags & FCF_CONF_COMP_SUPPORTED));
782 if (sess && sess->local) {
783 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04d,
784 "qla_target(%u): local session for "
785 "port %8phC (loop ID %d) became global\n", vha->vp_idx,
786 fcport->port_name, sess->loop_id);
789 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
791 ha->tgt.tgt_ops->put_sess(sess);
795 * This is a zero-base ref-counting solution, since hardware_lock
796 * guarantees that ref_count is not modified concurrently.
797 * Upon successful return content of iocb is undefined
799 static struct qlt_plogi_ack_t *
800 qlt_plogi_ack_find_add(struct scsi_qla_host *vha, port_id_t *id,
801 struct imm_ntfy_from_isp *iocb)
803 struct qlt_plogi_ack_t *pla;
805 lockdep_assert_held(&vha->hw->hardware_lock);
807 list_for_each_entry(pla, &vha->plogi_ack_list, list) {
808 if (pla->id.b24 == id->b24) {
809 ql_dbg(ql_dbg_disc + ql_dbg_verbose, vha, 0x210d,
810 "%s %d %8phC Term INOT due to new INOT",
812 pla->iocb.u.isp24.port_name);
813 qlt_send_term_imm_notif(vha, &pla->iocb, 1);
814 memcpy(&pla->iocb, iocb, sizeof(pla->iocb));
819 pla = kmem_cache_zalloc(qla_tgt_plogi_cachep, GFP_ATOMIC);
821 ql_dbg(ql_dbg_async, vha, 0x5088,
822 "qla_target(%d): Allocation of plogi_ack failed\n",
827 memcpy(&pla->iocb, iocb, sizeof(pla->iocb));
829 list_add_tail(&pla->list, &vha->plogi_ack_list);
834 void qlt_plogi_ack_unref(struct scsi_qla_host *vha,
835 struct qlt_plogi_ack_t *pla)
837 struct imm_ntfy_from_isp *iocb = &pla->iocb;
840 fc_port_t *fcport = pla->fcport;
842 BUG_ON(!pla->ref_count);
848 ql_dbg(ql_dbg_disc, vha, 0x5089,
849 "Sending PLOGI ACK to wwn %8phC s_id %02x:%02x:%02x loop_id %#04x"
850 " exch %#x ox_id %#x\n", iocb->u.isp24.port_name,
851 iocb->u.isp24.port_id[2], iocb->u.isp24.port_id[1],
852 iocb->u.isp24.port_id[0],
853 le16_to_cpu(iocb->u.isp24.nport_handle),
854 iocb->u.isp24.exchange_address, iocb->ox_id);
856 port_id.b.domain = iocb->u.isp24.port_id[2];
857 port_id.b.area = iocb->u.isp24.port_id[1];
858 port_id.b.al_pa = iocb->u.isp24.port_id[0];
859 port_id.b.rsvd_1 = 0;
861 loop_id = le16_to_cpu(iocb->u.isp24.nport_handle);
863 fcport->loop_id = loop_id;
864 fcport->d_id = port_id;
865 if (iocb->u.isp24.status_subcode == ELS_PLOGI)
866 qla24xx_post_nack_work(vha, fcport, iocb, SRB_NACK_PLOGI);
868 qla24xx_post_nack_work(vha, fcport, iocb, SRB_NACK_PRLI);
870 list_for_each_entry(fcport, &vha->vp_fcports, list) {
871 if (fcport->plogi_link[QLT_PLOGI_LINK_SAME_WWN] == pla)
872 fcport->plogi_link[QLT_PLOGI_LINK_SAME_WWN] = NULL;
873 if (fcport->plogi_link[QLT_PLOGI_LINK_CONFLICT] == pla)
874 fcport->plogi_link[QLT_PLOGI_LINK_CONFLICT] = NULL;
877 list_del(&pla->list);
878 kmem_cache_free(qla_tgt_plogi_cachep, pla);
882 qlt_plogi_ack_link(struct scsi_qla_host *vha, struct qlt_plogi_ack_t *pla,
883 struct fc_port *sess, enum qlt_plogi_link_t link)
885 struct imm_ntfy_from_isp *iocb = &pla->iocb;
886 /* Inc ref_count first because link might already be pointing at pla */
889 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf097,
890 "Linking sess %p [%d] wwn %8phC with PLOGI ACK to wwn %8phC"
891 " s_id %02x:%02x:%02x, ref=%d pla %p link %d\n",
892 sess, link, sess->port_name,
893 iocb->u.isp24.port_name, iocb->u.isp24.port_id[2],
894 iocb->u.isp24.port_id[1], iocb->u.isp24.port_id[0],
895 pla->ref_count, pla, link);
897 if (link == QLT_PLOGI_LINK_CONFLICT) {
898 switch (sess->disc_state) {
900 case DSC_DELETE_PEND:
908 if (sess->plogi_link[link])
909 qlt_plogi_ack_unref(vha, sess->plogi_link[link]);
911 if (link == QLT_PLOGI_LINK_SAME_WWN)
914 sess->plogi_link[link] = pla;
918 /* These fields must be initialized by the caller */
921 * number of cmds dropped while we were waiting for
922 * initiator to ack LOGO initialize to 1 if LOGO is
923 * triggered by a command, otherwise, to 0
927 /* These fields are used by callee */
928 struct list_head list;
932 qlt_send_first_logo(struct scsi_qla_host *vha, qlt_port_logo_t *logo)
934 qlt_port_logo_t *tmp;
937 if (test_bit(PFLG_DRIVER_REMOVING, &vha->pci_flags)) {
942 mutex_lock(&vha->vha_tgt.tgt_mutex);
944 list_for_each_entry(tmp, &vha->logo_list, list) {
945 if (tmp->id.b24 == logo->id.b24) {
946 tmp->cmd_count += logo->cmd_count;
947 mutex_unlock(&vha->vha_tgt.tgt_mutex);
952 list_add_tail(&logo->list, &vha->logo_list);
954 mutex_unlock(&vha->vha_tgt.tgt_mutex);
956 res = qla24xx_els_dcmd_iocb(vha, ELS_DCMD_LOGO, logo->id);
958 mutex_lock(&vha->vha_tgt.tgt_mutex);
959 list_del(&logo->list);
960 mutex_unlock(&vha->vha_tgt.tgt_mutex);
963 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf098,
964 "Finished LOGO to %02x:%02x:%02x, dropped %d cmds, res = %#x\n",
965 logo->id.b.domain, logo->id.b.area, logo->id.b.al_pa,
966 logo->cmd_count, res);
969 void qlt_free_session_done(struct work_struct *work)
971 struct fc_port *sess = container_of(work, struct fc_port,
973 struct qla_tgt *tgt = sess->tgt;
974 struct scsi_qla_host *vha = sess->vha;
975 struct qla_hw_data *ha = vha->hw;
977 bool logout_started = false;
978 scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
979 struct qlt_plogi_ack_t *own =
980 sess->plogi_link[QLT_PLOGI_LINK_SAME_WWN];
982 ql_dbg(ql_dbg_disc, vha, 0xf084,
983 "%s: se_sess %p / sess %p from port %8phC loop_id %#04x"
984 " s_id %02x:%02x:%02x logout %d keep %d els_logo %d\n",
985 __func__, sess->se_sess, sess, sess->port_name, sess->loop_id,
986 sess->d_id.b.domain, sess->d_id.b.area, sess->d_id.b.al_pa,
987 sess->logout_on_delete, sess->keep_nport_handle,
988 sess->send_els_logo);
990 if (!IS_SW_RESV_ADDR(sess->d_id)) {
991 if (ha->flags.edif_enabled &&
992 (!own || own->iocb.u.isp24.status_subcode == ELS_PLOGI)) {
993 sess->edif.authok = 0;
994 if (!ha->flags.host_shutting_down) {
995 ql_dbg(ql_dbg_edif, vha, 0x911e,
996 "%s wwpn %8phC calling qla2x00_release_all_sadb\n",
997 __func__, sess->port_name);
998 qla2x00_release_all_sadb(vha, sess);
1000 ql_dbg(ql_dbg_edif, vha, 0x911e,
1001 "%s bypassing release_all_sadb\n",
1004 qla_edif_clear_appdata(vha, sess);
1005 qla_edif_sess_down(vha, sess);
1007 qla2x00_mark_device_lost(vha, sess, 0);
1009 if (sess->send_els_logo) {
1010 qlt_port_logo_t logo;
1012 logo.id = sess->d_id;
1014 INIT_LIST_HEAD(&logo.list);
1016 qlt_send_first_logo(vha, &logo);
1017 sess->send_els_logo = 0;
1020 if (sess->logout_on_delete && sess->loop_id != FC_NO_LOOP_ID) {
1024 (own->iocb.u.isp24.status_subcode == ELS_PLOGI)) {
1025 sess->logout_completed = 0;
1026 rc = qla2x00_post_async_logout_work(vha, sess,
1028 if (rc != QLA_SUCCESS)
1029 ql_log(ql_log_warn, vha, 0xf085,
1030 "Schedule logo failed sess %p rc %d\n",
1033 logout_started = true;
1034 } else if (own && (own->iocb.u.isp24.status_subcode ==
1035 ELS_PRLI) && ha->flags.rida_fmt2) {
1036 rc = qla2x00_post_async_prlo_work(vha, sess,
1038 if (rc != QLA_SUCCESS)
1039 ql_log(ql_log_warn, vha, 0xf085,
1040 "Schedule PRLO failed sess %p rc %d\n",
1043 logout_started = true;
1045 } /* if sess->logout_on_delete */
1047 if (sess->nvme_flag & NVME_FLAG_REGISTERED &&
1048 !(sess->nvme_flag & NVME_FLAG_DELETING)) {
1049 sess->nvme_flag |= NVME_FLAG_DELETING;
1050 qla_nvme_unregister_remote_port(sess);
1055 * Release the target session for FC Nexus from fabric module code.
1057 if (sess->se_sess != NULL)
1058 ha->tgt.tgt_ops->free_session(sess);
1060 if (logout_started) {
1061 bool traced = false;
1064 while (!READ_ONCE(sess->logout_completed)) {
1066 ql_dbg(ql_dbg_disc, vha, 0xf086,
1067 "%s: waiting for sess %p logout\n",
1074 * Driver timeout is set to 22 Sec, update count value to loop
1075 * long enough for log-out to complete before advancing. Otherwise,
1076 * straddling logout can interfere with re-login attempt.
1082 ql_dbg(ql_dbg_disc, vha, 0xf087,
1083 "%s: sess %p logout completed\n", __func__, sess);
1086 if (sess->logo_ack_needed) {
1087 sess->logo_ack_needed = 0;
1088 qla24xx_async_notify_ack(vha, sess,
1089 (struct imm_ntfy_from_isp *)sess->iocb, SRB_NACK_LOGO);
1092 spin_lock_irqsave(&vha->work_lock, flags);
1093 sess->flags &= ~FCF_ASYNC_SENT;
1094 spin_unlock_irqrestore(&vha->work_lock, flags);
1096 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
1097 if (sess->se_sess) {
1098 sess->se_sess = NULL;
1099 if (tgt && !IS_SW_RESV_ADDR(sess->d_id))
1103 qla2x00_set_fcport_disc_state(sess, DSC_DELETED);
1104 sess->fw_login_state = DSC_LS_PORT_UNAVAIL;
1105 sess->deleted = QLA_SESS_DELETED;
1107 if (sess->login_succ && !IS_SW_RESV_ADDR(sess->d_id)) {
1108 vha->fcport_count--;
1109 sess->login_succ = 0;
1112 qla2x00_clear_loop_id(sess);
1114 if (sess->conflict) {
1115 sess->conflict->login_pause = 0;
1116 sess->conflict = NULL;
1117 if (!test_bit(UNLOADING, &vha->dpc_flags))
1118 set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
1122 struct qlt_plogi_ack_t *con =
1123 sess->plogi_link[QLT_PLOGI_LINK_CONFLICT];
1124 struct imm_ntfy_from_isp *iocb;
1126 own = sess->plogi_link[QLT_PLOGI_LINK_SAME_WWN];
1130 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf099,
1131 "se_sess %p / sess %p port %8phC is gone,"
1132 " %s (ref=%d), releasing PLOGI for %8phC (ref=%d)\n",
1133 sess->se_sess, sess, sess->port_name,
1134 own ? "releasing own PLOGI" : "no own PLOGI pending",
1135 own ? own->ref_count : -1,
1136 iocb->u.isp24.port_name, con->ref_count);
1137 qlt_plogi_ack_unref(vha, con);
1138 sess->plogi_link[QLT_PLOGI_LINK_CONFLICT] = NULL;
1140 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf09a,
1141 "se_sess %p / sess %p port %8phC is gone, %s (ref=%d)\n",
1142 sess->se_sess, sess, sess->port_name,
1143 own ? "releasing own PLOGI" :
1144 "no own PLOGI pending",
1145 own ? own->ref_count : -1);
1149 sess->fw_login_state = DSC_LS_PLOGI_PEND;
1150 qlt_plogi_ack_unref(vha, own);
1151 sess->plogi_link[QLT_PLOGI_LINK_SAME_WWN] = NULL;
1155 sess->explicit_logout = 0;
1156 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
1157 sess->free_pending = 0;
1159 qla2x00_dfs_remove_rport(vha, sess);
1161 ql_dbg(ql_dbg_disc, vha, 0xf001,
1162 "Unregistration of sess %p %8phC finished fcp_cnt %d\n",
1163 sess, sess->port_name, vha->fcport_count);
1165 if (tgt && (tgt->sess_count == 0))
1166 wake_up_all(&tgt->waitQ);
1168 if (!test_bit(PFLG_DRIVER_REMOVING, &base_vha->pci_flags) &&
1169 !(vha->vp_idx && test_bit(VPORT_DELETE, &vha->dpc_flags)) &&
1170 (!tgt || !tgt->tgt_stop) && !LOOP_TRANSITION(vha)) {
1171 switch (vha->host->active_mode) {
1172 case MODE_INITIATOR:
1174 set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
1175 qla2xxx_wake_dpc(vha);
1184 if (vha->fcport_count == 0)
1185 wake_up_all(&vha->fcport_waitQ);
1188 /* ha->tgt.sess_lock supposed to be held on entry */
1189 void qlt_unreg_sess(struct fc_port *sess)
1191 struct scsi_qla_host *vha = sess->vha;
1192 unsigned long flags;
1194 ql_dbg(ql_dbg_disc, sess->vha, 0x210a,
1195 "%s sess %p for deletion %8phC\n",
1196 __func__, sess, sess->port_name);
1198 spin_lock_irqsave(&sess->vha->work_lock, flags);
1199 if (sess->free_pending) {
1200 spin_unlock_irqrestore(&sess->vha->work_lock, flags);
1203 sess->free_pending = 1;
1205 * Use FCF_ASYNC_SENT flag to block other cmds used in sess
1206 * management from being sent.
1208 sess->flags |= FCF_ASYNC_SENT;
1209 spin_unlock_irqrestore(&sess->vha->work_lock, flags);
1212 vha->hw->tgt.tgt_ops->clear_nacl_from_fcport_map(sess);
1214 sess->deleted = QLA_SESS_DELETION_IN_PROGRESS;
1215 qla2x00_set_fcport_disc_state(sess, DSC_DELETE_PEND);
1216 sess->last_rscn_gen = sess->rscn_gen;
1217 sess->last_login_gen = sess->login_gen;
1219 queue_work(sess->vha->hw->wq, &sess->free_work);
1221 EXPORT_SYMBOL(qlt_unreg_sess);
1223 static int qlt_reset(struct scsi_qla_host *vha, void *iocb, int mcmd)
1225 struct qla_hw_data *ha = vha->hw;
1226 struct fc_port *sess = NULL;
1229 struct imm_ntfy_from_isp *n = (struct imm_ntfy_from_isp *)iocb;
1230 unsigned long flags;
1232 loop_id = le16_to_cpu(n->u.isp24.nport_handle);
1233 if (loop_id == 0xFFFF) {
1235 atomic_inc(&vha->vha_tgt.qla_tgt->tgt_global_resets_count);
1236 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
1237 qlt_clear_tgt_db(vha->vha_tgt.qla_tgt);
1238 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
1240 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
1241 sess = ha->tgt.tgt_ops->find_sess_by_loop_id(vha, loop_id);
1242 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
1245 ql_dbg(ql_dbg_tgt, vha, 0xe000,
1246 "Using sess for qla_tgt_reset: %p\n", sess);
1252 ql_dbg(ql_dbg_tgt, vha, 0xe047,
1253 "scsi(%ld): resetting (session %p from port %8phC mcmd %x, "
1254 "loop_id %d)\n", vha->host_no, sess, sess->port_name,
1257 return qlt_issue_task_mgmt(sess, 0, mcmd, iocb, QLA24XX_MGMT_SEND_NACK);
1260 static void qla24xx_chk_fcp_state(struct fc_port *sess)
1262 if (sess->chip_reset != sess->vha->hw->base_qpair->chip_reset) {
1263 sess->logout_on_delete = 0;
1264 sess->logo_ack_needed = 0;
1265 sess->fw_login_state = DSC_LS_PORT_UNAVAIL;
1269 void qlt_schedule_sess_for_deletion(struct fc_port *sess)
1271 struct qla_tgt *tgt = sess->tgt;
1272 unsigned long flags;
1275 switch (sess->disc_state) {
1276 case DSC_DELETE_PEND:
1279 if (!sess->plogi_link[QLT_PLOGI_LINK_SAME_WWN] &&
1280 !sess->plogi_link[QLT_PLOGI_LINK_CONFLICT]) {
1281 if (tgt && tgt->tgt_stop && tgt->sess_count == 0)
1282 wake_up_all(&tgt->waitQ);
1284 if (sess->vha->fcport_count == 0)
1285 wake_up_all(&sess->vha->fcport_waitQ);
1289 case DSC_UPD_FCPORT:
1291 * This port is not done reporting to upper layer.
1294 sess->next_disc_state = DSC_DELETE_PEND;
1295 sec = jiffies_to_msecs(jiffies -
1296 sess->jiffies_at_registration)/1000;
1297 if (sess->sec_since_registration < sec && sec && !(sec % 5)) {
1298 sess->sec_since_registration = sec;
1299 ql_dbg(ql_dbg_disc, sess->vha, 0xffff,
1300 "%s %8phC : Slow Rport registration(%d Sec)\n",
1301 __func__, sess->port_name, sec);
1308 spin_lock_irqsave(&sess->vha->work_lock, flags);
1309 if (sess->deleted == QLA_SESS_DELETION_IN_PROGRESS) {
1310 spin_unlock_irqrestore(&sess->vha->work_lock, flags);
1313 sess->deleted = QLA_SESS_DELETION_IN_PROGRESS;
1314 spin_unlock_irqrestore(&sess->vha->work_lock, flags);
1316 sess->prli_pend_timer = 0;
1317 qla2x00_set_fcport_disc_state(sess, DSC_DELETE_PEND);
1319 qla24xx_chk_fcp_state(sess);
1321 ql_dbg(ql_log_warn, sess->vha, 0xe001,
1322 "Scheduling sess %p for deletion %8phC fc4_type %x\n",
1323 sess, sess->port_name, sess->fc4_type);
1325 WARN_ON(!queue_work(sess->vha->hw->wq, &sess->del_work));
1328 static void qlt_clear_tgt_db(struct qla_tgt *tgt)
1330 struct fc_port *sess;
1331 scsi_qla_host_t *vha = tgt->vha;
1333 list_for_each_entry(sess, &vha->vp_fcports, list) {
1335 qlt_schedule_sess_for_deletion(sess);
1338 /* At this point tgt could be already dead */
1341 static int qla24xx_get_loop_id(struct scsi_qla_host *vha, be_id_t s_id,
1344 struct qla_hw_data *ha = vha->hw;
1345 dma_addr_t gid_list_dma;
1346 struct gid_list_info *gid_list, *gid;
1350 gid_list = dma_alloc_coherent(&ha->pdev->dev, qla2x00_gid_list_size(ha),
1351 &gid_list_dma, GFP_KERNEL);
1353 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf044,
1354 "qla_target(%d): DMA Alloc failed of %u\n",
1355 vha->vp_idx, qla2x00_gid_list_size(ha));
1359 /* Get list of logged in devices */
1360 rc = qla24xx_gidlist_wait(vha, gid_list, gid_list_dma, &entries);
1361 if (rc != QLA_SUCCESS) {
1362 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf045,
1363 "qla_target(%d): get_id_list() failed: %x\n",
1366 goto out_free_id_list;
1371 for (i = 0; i < entries; i++) {
1372 if (gid->al_pa == s_id.al_pa &&
1373 gid->area == s_id.area &&
1374 gid->domain == s_id.domain) {
1375 *loop_id = le16_to_cpu(gid->loop_id);
1379 gid = (void *)gid + ha->gid_list_info_size;
1383 dma_free_coherent(&ha->pdev->dev, qla2x00_gid_list_size(ha),
1384 gid_list, gid_list_dma);
1389 * Adds an extra ref to allow to drop hw lock after adding sess to the list.
1390 * Caller must put it.
1392 static struct fc_port *qlt_create_sess(
1393 struct scsi_qla_host *vha,
1397 struct qla_hw_data *ha = vha->hw;
1398 struct fc_port *sess = fcport;
1399 unsigned long flags;
1401 if (vha->vha_tgt.qla_tgt->tgt_stop)
1404 if (fcport->se_sess) {
1405 if (!kref_get_unless_zero(&sess->sess_kref)) {
1406 ql_dbg(ql_dbg_disc, vha, 0x20f6,
1407 "%s: kref_get_unless_zero failed for %8phC\n",
1408 __func__, sess->port_name);
1413 sess->tgt = vha->vha_tgt.qla_tgt;
1414 sess->local = local;
1417 * Under normal circumstances we want to logout from firmware when
1418 * session eventually ends and release corresponding nport handle.
1419 * In the exception cases (e.g. when new PLOGI is waiting) corresponding
1420 * code will adjust these flags as necessary.
1422 sess->logout_on_delete = 1;
1423 sess->keep_nport_handle = 0;
1424 sess->logout_completed = 0;
1426 if (ha->tgt.tgt_ops->check_initiator_node_acl(vha,
1427 &fcport->port_name[0], sess) < 0) {
1428 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf015,
1429 "(%d) %8phC check_initiator_node_acl failed\n",
1430 vha->vp_idx, fcport->port_name);
1433 kref_init(&fcport->sess_kref);
1435 * Take an extra reference to ->sess_kref here to handle
1436 * fc_port access across ->tgt.sess_lock reaquire.
1438 if (!kref_get_unless_zero(&sess->sess_kref)) {
1439 ql_dbg(ql_dbg_disc, vha, 0x20f7,
1440 "%s: kref_get_unless_zero failed for %8phC\n",
1441 __func__, sess->port_name);
1445 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
1446 if (!IS_SW_RESV_ADDR(sess->d_id))
1447 vha->vha_tgt.qla_tgt->sess_count++;
1449 qlt_do_generation_tick(vha, &sess->generation);
1450 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
1453 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf006,
1454 "Adding sess %p se_sess %p to tgt %p sess_count %d\n",
1455 sess, sess->se_sess, vha->vha_tgt.qla_tgt,
1456 vha->vha_tgt.qla_tgt->sess_count);
1458 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04b,
1459 "qla_target(%d): %ssession for wwn %8phC (loop_id %d, "
1460 "s_id %x:%x:%x, confirmed completion %ssupported) added\n",
1461 vha->vp_idx, local ? "local " : "", fcport->port_name,
1462 fcport->loop_id, sess->d_id.b.domain, sess->d_id.b.area,
1463 sess->d_id.b.al_pa, sess->conf_compl_supported ? "" : "not ");
1469 * max_gen - specifies maximum session generation
1470 * at which this deletion requestion is still valid
1473 qlt_fc_port_deleted(struct scsi_qla_host *vha, fc_port_t *fcport, int max_gen)
1475 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
1476 struct fc_port *sess = fcport;
1477 unsigned long flags;
1479 if (!vha->hw->tgt.tgt_ops)
1485 spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
1486 if (tgt->tgt_stop) {
1487 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
1490 if (!sess->se_sess) {
1491 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
1495 if (max_gen - sess->generation < 0) {
1496 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
1497 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf092,
1498 "Ignoring stale deletion request for se_sess %p / sess %p"
1499 " for port %8phC, req_gen %d, sess_gen %d\n",
1500 sess->se_sess, sess, sess->port_name, max_gen,
1505 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf008, "qla_tgt_fc_port_deleted %p", sess);
1508 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
1509 qlt_schedule_sess_for_deletion(sess);
1512 static inline int test_tgt_sess_count(struct qla_tgt *tgt)
1514 struct qla_hw_data *ha = tgt->ha;
1515 unsigned long flags;
1518 * We need to protect against race, when tgt is freed before or
1521 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
1522 ql_dbg(ql_dbg_tgt, tgt->vha, 0xe002,
1523 "tgt %p, sess_count=%d\n",
1524 tgt, tgt->sess_count);
1525 res = (tgt->sess_count == 0);
1526 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
1531 /* Called by tcm_qla2xxx configfs code */
1532 int qlt_stop_phase1(struct qla_tgt *tgt)
1534 struct scsi_qla_host *vha = tgt->vha;
1535 struct qla_hw_data *ha = tgt->ha;
1536 unsigned long flags;
1538 mutex_lock(&ha->optrom_mutex);
1539 mutex_lock(&qla_tgt_mutex);
1541 if (tgt->tgt_stop || tgt->tgt_stopped) {
1542 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04e,
1543 "Already in tgt->tgt_stop or tgt_stopped state\n");
1544 mutex_unlock(&qla_tgt_mutex);
1545 mutex_unlock(&ha->optrom_mutex);
1549 ql_dbg(ql_dbg_tgt_mgt, vha, 0xe003, "Stopping target for host %ld(%p)\n",
1552 * Mutex needed to sync with qla_tgt_fc_port_[added,deleted].
1553 * Lock is needed, because we still can get an incoming packet.
1555 mutex_lock(&vha->vha_tgt.tgt_mutex);
1557 qlt_clear_tgt_db(tgt);
1558 mutex_unlock(&vha->vha_tgt.tgt_mutex);
1559 mutex_unlock(&qla_tgt_mutex);
1561 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf009,
1562 "Waiting for sess works (tgt %p)", tgt);
1563 spin_lock_irqsave(&tgt->sess_work_lock, flags);
1564 while (!list_empty(&tgt->sess_works_list)) {
1565 spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
1566 flush_scheduled_work();
1567 spin_lock_irqsave(&tgt->sess_work_lock, flags);
1569 spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
1571 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00a,
1572 "Waiting for tgt %p: sess_count=%d\n", tgt, tgt->sess_count);
1574 wait_event_timeout(tgt->waitQ, test_tgt_sess_count(tgt), 10*HZ);
1577 if (!ha->flags.host_shutting_down &&
1578 (qla_tgt_mode_enabled(vha) || qla_dual_mode_enabled(vha)))
1579 qlt_disable_vha(vha);
1581 /* Wait for sessions to clear out (just in case) */
1582 wait_event_timeout(tgt->waitQ, test_tgt_sess_count(tgt), 10*HZ);
1583 mutex_unlock(&ha->optrom_mutex);
1587 EXPORT_SYMBOL(qlt_stop_phase1);
1589 /* Called by tcm_qla2xxx configfs code */
1590 void qlt_stop_phase2(struct qla_tgt *tgt)
1592 scsi_qla_host_t *vha = tgt->vha;
1594 if (tgt->tgt_stopped) {
1595 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04f,
1596 "Already in tgt->tgt_stopped state\n");
1600 if (!tgt->tgt_stop) {
1601 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00b,
1602 "%s: phase1 stop is not completed\n", __func__);
1607 mutex_lock(&tgt->ha->optrom_mutex);
1608 mutex_lock(&vha->vha_tgt.tgt_mutex);
1610 tgt->tgt_stopped = 1;
1611 mutex_unlock(&vha->vha_tgt.tgt_mutex);
1612 mutex_unlock(&tgt->ha->optrom_mutex);
1614 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00c, "Stop of tgt %p finished\n",
1617 switch (vha->qlini_mode) {
1618 case QLA2XXX_INI_MODE_EXCLUSIVE:
1619 vha->flags.online = 1;
1620 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
1626 EXPORT_SYMBOL(qlt_stop_phase2);
1628 /* Called from qlt_remove_target() -> qla2x00_remove_one() */
1629 static void qlt_release(struct qla_tgt *tgt)
1631 scsi_qla_host_t *vha = tgt->vha;
1635 struct qla_qpair_hint *h;
1636 struct qla_hw_data *ha = vha->hw;
1638 if (!tgt->tgt_stop && !tgt->tgt_stopped)
1639 qlt_stop_phase1(tgt);
1641 if (!tgt->tgt_stopped)
1642 qlt_stop_phase2(tgt);
1644 for (i = 0; i < vha->hw->max_qpairs + 1; i++) {
1645 unsigned long flags;
1647 h = &tgt->qphints[i];
1649 spin_lock_irqsave(h->qpair->qp_lock_ptr, flags);
1650 list_del(&h->hint_elem);
1651 spin_unlock_irqrestore(h->qpair->qp_lock_ptr, flags);
1655 kfree(tgt->qphints);
1656 mutex_lock(&qla_tgt_mutex);
1657 list_del(&vha->vha_tgt.qla_tgt->tgt_list_entry);
1658 mutex_unlock(&qla_tgt_mutex);
1660 btree_for_each_safe64(&tgt->lun_qpair_map, key, node)
1661 btree_remove64(&tgt->lun_qpair_map, key);
1663 btree_destroy64(&tgt->lun_qpair_map);
1666 if (ha->tgt.tgt_ops &&
1667 ha->tgt.tgt_ops->remove_target &&
1668 vha->vha_tgt.target_lport_ptr)
1669 ha->tgt.tgt_ops->remove_target(vha);
1671 vha->vha_tgt.qla_tgt = NULL;
1673 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00d,
1674 "Release of tgt %p finished\n", tgt);
1679 /* ha->hardware_lock supposed to be held on entry */
1680 static int qlt_sched_sess_work(struct qla_tgt *tgt, int type,
1681 const void *param, unsigned int param_size)
1683 struct qla_tgt_sess_work_param *prm;
1684 unsigned long flags;
1686 prm = kzalloc(sizeof(*prm), GFP_ATOMIC);
1688 ql_dbg(ql_dbg_tgt_mgt, tgt->vha, 0xf050,
1689 "qla_target(%d): Unable to create session "
1690 "work, command will be refused", 0);
1694 ql_dbg(ql_dbg_tgt_mgt, tgt->vha, 0xf00e,
1695 "Scheduling work (type %d, prm %p)"
1696 " to find session for param %p (size %d, tgt %p)\n",
1697 type, prm, param, param_size, tgt);
1700 memcpy(&prm->tm_iocb, param, param_size);
1702 spin_lock_irqsave(&tgt->sess_work_lock, flags);
1703 list_add_tail(&prm->sess_works_list_entry, &tgt->sess_works_list);
1704 spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
1706 schedule_work(&tgt->sess_work);
1712 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
1714 static void qlt_send_notify_ack(struct qla_qpair *qpair,
1715 struct imm_ntfy_from_isp *ntfy,
1716 uint32_t add_flags, uint16_t resp_code, int resp_code_valid,
1717 uint16_t srr_flags, uint16_t srr_reject_code, uint8_t srr_explan)
1719 struct scsi_qla_host *vha = qpair->vha;
1720 struct qla_hw_data *ha = vha->hw;
1722 struct nack_to_isp *nack;
1724 if (!ha->flags.fw_started)
1727 ql_dbg(ql_dbg_tgt, vha, 0xe004, "Sending NOTIFY_ACK (ha=%p)\n", ha);
1729 pkt = (request_t *)__qla2x00_alloc_iocbs(qpair, NULL);
1731 ql_dbg(ql_dbg_tgt, vha, 0xe049,
1732 "qla_target(%d): %s failed: unable to allocate "
1733 "request packet\n", vha->vp_idx, __func__);
1737 if (vha->vha_tgt.qla_tgt != NULL)
1738 vha->vha_tgt.qla_tgt->notify_ack_expected++;
1740 pkt->entry_type = NOTIFY_ACK_TYPE;
1741 pkt->entry_count = 1;
1743 nack = (struct nack_to_isp *)pkt;
1744 nack->ox_id = ntfy->ox_id;
1746 nack->u.isp24.handle = QLA_TGT_SKIP_HANDLE;
1747 nack->u.isp24.nport_handle = ntfy->u.isp24.nport_handle;
1748 if (le16_to_cpu(ntfy->u.isp24.status) == IMM_NTFY_ELS) {
1749 nack->u.isp24.flags = ntfy->u.isp24.flags &
1750 cpu_to_le16(NOTIFY24XX_FLAGS_PUREX_IOCB);
1752 nack->u.isp24.srr_rx_id = ntfy->u.isp24.srr_rx_id;
1753 nack->u.isp24.status = ntfy->u.isp24.status;
1754 nack->u.isp24.status_subcode = ntfy->u.isp24.status_subcode;
1755 nack->u.isp24.fw_handle = ntfy->u.isp24.fw_handle;
1756 nack->u.isp24.exchange_address = ntfy->u.isp24.exchange_address;
1757 nack->u.isp24.srr_rel_offs = ntfy->u.isp24.srr_rel_offs;
1758 nack->u.isp24.srr_ui = ntfy->u.isp24.srr_ui;
1759 nack->u.isp24.srr_flags = cpu_to_le16(srr_flags);
1760 nack->u.isp24.srr_reject_code = srr_reject_code;
1761 nack->u.isp24.srr_reject_code_expl = srr_explan;
1762 nack->u.isp24.vp_index = ntfy->u.isp24.vp_index;
1764 /* TODO qualify this with EDIF enable */
1765 if (ntfy->u.isp24.status_subcode == ELS_PLOGI &&
1766 (le16_to_cpu(ntfy->u.isp24.flags) & NOTIFY24XX_FLAGS_FCSP)) {
1767 nack->u.isp24.flags |= cpu_to_le16(NOTIFY_ACK_FLAGS_FCSP);
1770 ql_dbg(ql_dbg_tgt, vha, 0xe005,
1771 "qla_target(%d): Sending 24xx Notify Ack %d\n",
1772 vha->vp_idx, nack->u.isp24.status);
1774 /* Memory Barrier */
1776 qla2x00_start_iocbs(vha, qpair->req);
1779 static int qlt_build_abts_resp_iocb(struct qla_tgt_mgmt_cmd *mcmd)
1781 struct scsi_qla_host *vha = mcmd->vha;
1782 struct qla_hw_data *ha = vha->hw;
1783 struct abts_resp_to_24xx *resp;
1788 struct abts_recv_from_24xx *abts = &mcmd->orig_iocb.abts;
1789 struct qla_qpair *qpair = mcmd->qpair;
1791 ql_dbg(ql_dbg_tgt, vha, 0xe006,
1792 "Sending task mgmt ABTS response (ha=%p, status=%x)\n",
1793 ha, mcmd->fc_tm_rsp);
1795 rc = qlt_check_reserve_free_req(qpair, 1);
1797 ql_dbg(ql_dbg_tgt, vha, 0xe04a,
1798 "qla_target(%d): %s failed: unable to allocate request packet\n",
1799 vha->vp_idx, __func__);
1803 resp = (struct abts_resp_to_24xx *)qpair->req->ring_ptr;
1804 memset(resp, 0, sizeof(*resp));
1806 h = qlt_make_handle(qpair);
1807 if (unlikely(h == QLA_TGT_NULL_HANDLE)) {
1809 * CTIO type 7 from the firmware doesn't provide a way to
1810 * know the initiator's LOOP ID, hence we can't find
1811 * the session and, so, the command.
1815 qpair->req->outstanding_cmds[h] = (srb_t *)mcmd;
1818 resp->handle = make_handle(qpair->req->id, h);
1819 resp->entry_type = ABTS_RESP_24XX;
1820 resp->entry_count = 1;
1821 resp->nport_handle = abts->nport_handle;
1822 resp->vp_index = vha->vp_idx;
1823 resp->sof_type = abts->sof_type;
1824 resp->exchange_address = abts->exchange_address;
1825 resp->fcp_hdr_le = abts->fcp_hdr_le;
1826 f_ctl = cpu_to_le32(F_CTL_EXCH_CONTEXT_RESP |
1827 F_CTL_LAST_SEQ | F_CTL_END_SEQ |
1828 F_CTL_SEQ_INITIATIVE);
1829 p = (uint8_t *)&f_ctl;
1830 resp->fcp_hdr_le.f_ctl[0] = *p++;
1831 resp->fcp_hdr_le.f_ctl[1] = *p++;
1832 resp->fcp_hdr_le.f_ctl[2] = *p;
1834 resp->fcp_hdr_le.d_id = abts->fcp_hdr_le.s_id;
1835 resp->fcp_hdr_le.s_id = abts->fcp_hdr_le.d_id;
1837 resp->exchange_addr_to_abort = abts->exchange_addr_to_abort;
1838 if (mcmd->fc_tm_rsp == FCP_TMF_CMPL) {
1839 resp->fcp_hdr_le.r_ctl = R_CTL_BASIC_LINK_SERV | R_CTL_B_ACC;
1840 resp->payload.ba_acct.seq_id_valid = SEQ_ID_INVALID;
1841 resp->payload.ba_acct.low_seq_cnt = 0x0000;
1842 resp->payload.ba_acct.high_seq_cnt = cpu_to_le16(0xFFFF);
1843 resp->payload.ba_acct.ox_id = abts->fcp_hdr_le.ox_id;
1844 resp->payload.ba_acct.rx_id = abts->fcp_hdr_le.rx_id;
1846 resp->fcp_hdr_le.r_ctl = R_CTL_BASIC_LINK_SERV | R_CTL_B_RJT;
1847 resp->payload.ba_rjt.reason_code =
1848 BA_RJT_REASON_CODE_UNABLE_TO_PERFORM;
1849 /* Other bytes are zero */
1852 vha->vha_tgt.qla_tgt->abts_resp_expected++;
1854 /* Memory Barrier */
1856 if (qpair->reqq_start_iocbs)
1857 qpair->reqq_start_iocbs(qpair);
1859 qla2x00_start_iocbs(vha, qpair->req);
1865 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
1867 static void qlt_24xx_send_abts_resp(struct qla_qpair *qpair,
1868 struct abts_recv_from_24xx *abts, uint32_t status,
1871 struct scsi_qla_host *vha = qpair->vha;
1872 struct qla_hw_data *ha = vha->hw;
1873 struct abts_resp_to_24xx *resp;
1877 ql_dbg(ql_dbg_tgt, vha, 0xe006,
1878 "Sending task mgmt ABTS response (ha=%p, atio=%p, status=%x\n",
1881 resp = (struct abts_resp_to_24xx *)qla2x00_alloc_iocbs_ready(qpair,
1884 ql_dbg(ql_dbg_tgt, vha, 0xe04a,
1885 "qla_target(%d): %s failed: unable to allocate "
1886 "request packet", vha->vp_idx, __func__);
1890 resp->entry_type = ABTS_RESP_24XX;
1891 resp->handle = QLA_TGT_SKIP_HANDLE;
1892 resp->entry_count = 1;
1893 resp->nport_handle = abts->nport_handle;
1894 resp->vp_index = vha->vp_idx;
1895 resp->sof_type = abts->sof_type;
1896 resp->exchange_address = abts->exchange_address;
1897 resp->fcp_hdr_le = abts->fcp_hdr_le;
1898 f_ctl = cpu_to_le32(F_CTL_EXCH_CONTEXT_RESP |
1899 F_CTL_LAST_SEQ | F_CTL_END_SEQ |
1900 F_CTL_SEQ_INITIATIVE);
1901 p = (uint8_t *)&f_ctl;
1902 resp->fcp_hdr_le.f_ctl[0] = *p++;
1903 resp->fcp_hdr_le.f_ctl[1] = *p++;
1904 resp->fcp_hdr_le.f_ctl[2] = *p;
1906 resp->fcp_hdr_le.d_id = abts->fcp_hdr_le.d_id;
1907 resp->fcp_hdr_le.s_id = abts->fcp_hdr_le.s_id;
1909 resp->fcp_hdr_le.d_id = abts->fcp_hdr_le.s_id;
1910 resp->fcp_hdr_le.s_id = abts->fcp_hdr_le.d_id;
1912 resp->exchange_addr_to_abort = abts->exchange_addr_to_abort;
1913 if (status == FCP_TMF_CMPL) {
1914 resp->fcp_hdr_le.r_ctl = R_CTL_BASIC_LINK_SERV | R_CTL_B_ACC;
1915 resp->payload.ba_acct.seq_id_valid = SEQ_ID_INVALID;
1916 resp->payload.ba_acct.low_seq_cnt = 0x0000;
1917 resp->payload.ba_acct.high_seq_cnt = cpu_to_le16(0xFFFF);
1918 resp->payload.ba_acct.ox_id = abts->fcp_hdr_le.ox_id;
1919 resp->payload.ba_acct.rx_id = abts->fcp_hdr_le.rx_id;
1921 resp->fcp_hdr_le.r_ctl = R_CTL_BASIC_LINK_SERV | R_CTL_B_RJT;
1922 resp->payload.ba_rjt.reason_code =
1923 BA_RJT_REASON_CODE_UNABLE_TO_PERFORM;
1924 /* Other bytes are zero */
1927 vha->vha_tgt.qla_tgt->abts_resp_expected++;
1929 /* Memory Barrier */
1931 if (qpair->reqq_start_iocbs)
1932 qpair->reqq_start_iocbs(qpair);
1934 qla2x00_start_iocbs(vha, qpair->req);
1938 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
1940 static void qlt_24xx_retry_term_exchange(struct scsi_qla_host *vha,
1941 struct qla_qpair *qpair, response_t *pkt, struct qla_tgt_mgmt_cmd *mcmd)
1943 struct ctio7_to_24xx *ctio;
1945 struct abts_recv_from_24xx *entry;
1947 ctio = (struct ctio7_to_24xx *)qla2x00_alloc_iocbs_ready(qpair, NULL);
1949 ql_dbg(ql_dbg_tgt, vha, 0xe04b,
1950 "qla_target(%d): %s failed: unable to allocate "
1951 "request packet\n", vha->vp_idx, __func__);
1956 /* abts from remote port */
1957 entry = &mcmd->orig_iocb.abts;
1959 /* abts from this driver. */
1960 entry = (struct abts_recv_from_24xx *)pkt;
1963 * We've got on entrance firmware's response on by us generated
1964 * ABTS response. So, in it ID fields are reversed.
1967 ctio->entry_type = CTIO_TYPE7;
1968 ctio->entry_count = 1;
1969 ctio->nport_handle = entry->nport_handle;
1970 ctio->handle = QLA_TGT_SKIP_HANDLE | CTIO_COMPLETION_HANDLE_MARK;
1971 ctio->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
1972 ctio->vp_index = vha->vp_idx;
1973 ctio->exchange_addr = entry->exchange_addr_to_abort;
1974 tmp = (CTIO7_FLAGS_STATUS_MODE_1 | CTIO7_FLAGS_TERMINATE);
1977 ctio->initiator_id = entry->fcp_hdr_le.s_id;
1979 if (mcmd->flags & QLA24XX_MGMT_ABORT_IO_ATTR_VALID)
1980 tmp |= (mcmd->abort_io_attr << 9);
1981 else if (qpair->retry_term_cnt & 1)
1984 ctio->initiator_id = entry->fcp_hdr_le.d_id;
1986 if (qpair->retry_term_cnt & 1)
1989 ctio->u.status1.flags = cpu_to_le16(tmp);
1990 ctio->u.status1.ox_id = entry->fcp_hdr_le.ox_id;
1992 ql_dbg(ql_dbg_tgt, vha, 0xe007,
1993 "Sending retry TERM EXCH CTIO7 flags %04xh oxid %04xh attr valid %x\n",
1994 le16_to_cpu(ctio->u.status1.flags),
1995 le16_to_cpu(ctio->u.status1.ox_id),
1996 (mcmd && mcmd->flags & QLA24XX_MGMT_ABORT_IO_ATTR_VALID) ? 1 : 0);
1998 /* Memory Barrier */
2000 if (qpair->reqq_start_iocbs)
2001 qpair->reqq_start_iocbs(qpair);
2003 qla2x00_start_iocbs(vha, qpair->req);
2006 qlt_build_abts_resp_iocb(mcmd);
2008 qlt_24xx_send_abts_resp(qpair,
2009 (struct abts_recv_from_24xx *)entry, FCP_TMF_CMPL, true);
2013 /* drop cmds for the given lun
2014 * XXX only looks for cmds on the port through which lun reset was recieved
2015 * XXX does not go through the list of other port (which may have cmds
2018 static void abort_cmds_for_lun(struct scsi_qla_host *vha, u64 lun, be_id_t s_id)
2020 struct qla_tgt_sess_op *op;
2021 struct qla_tgt_cmd *cmd;
2023 unsigned long flags;
2025 key = sid_to_key(s_id);
2026 spin_lock_irqsave(&vha->cmd_list_lock, flags);
2027 list_for_each_entry(op, &vha->qla_sess_op_cmd_list, cmd_list) {
2031 op_key = sid_to_key(op->atio.u.isp24.fcp_hdr.s_id);
2032 op_lun = scsilun_to_int(
2033 (struct scsi_lun *)&op->atio.u.isp24.fcp_cmnd.lun);
2034 if (op_key == key && op_lun == lun)
2038 list_for_each_entry(op, &vha->unknown_atio_list, cmd_list) {
2042 op_key = sid_to_key(op->atio.u.isp24.fcp_hdr.s_id);
2043 op_lun = scsilun_to_int(
2044 (struct scsi_lun *)&op->atio.u.isp24.fcp_cmnd.lun);
2045 if (op_key == key && op_lun == lun)
2049 list_for_each_entry(cmd, &vha->qla_cmd_list, cmd_list) {
2053 cmd_key = sid_to_key(cmd->atio.u.isp24.fcp_hdr.s_id);
2054 cmd_lun = scsilun_to_int(
2055 (struct scsi_lun *)&cmd->atio.u.isp24.fcp_cmnd.lun);
2056 if (cmd_key == key && cmd_lun == lun)
2059 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
2062 static struct qla_qpair_hint *qlt_find_qphint(struct scsi_qla_host *vha,
2063 uint64_t unpacked_lun)
2065 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
2066 struct qla_qpair_hint *h = NULL;
2068 if (vha->flags.qpairs_available) {
2069 h = btree_lookup64(&tgt->lun_qpair_map, unpacked_lun);
2071 h = &tgt->qphints[0];
2073 h = &tgt->qphints[0];
2079 static void qlt_do_tmr_work(struct work_struct *work)
2081 struct qla_tgt_mgmt_cmd *mcmd =
2082 container_of(work, struct qla_tgt_mgmt_cmd, work);
2083 struct qla_hw_data *ha = mcmd->vha->hw;
2086 unsigned long flags;
2088 switch (mcmd->tmr_func) {
2090 tag = le32_to_cpu(mcmd->orig_iocb.abts.exchange_addr_to_abort);
2097 rc = ha->tgt.tgt_ops->handle_tmr(mcmd, mcmd->unpacked_lun,
2098 mcmd->tmr_func, tag);
2101 spin_lock_irqsave(mcmd->qpair->qp_lock_ptr, flags);
2102 switch (mcmd->tmr_func) {
2104 mcmd->fc_tm_rsp = FCP_TMF_REJECTED;
2105 qlt_build_abts_resp_iocb(mcmd);
2107 case QLA_TGT_LUN_RESET:
2108 case QLA_TGT_CLEAR_TS:
2109 case QLA_TGT_ABORT_TS:
2110 case QLA_TGT_CLEAR_ACA:
2111 case QLA_TGT_TARGET_RESET:
2112 qlt_send_busy(mcmd->qpair, &mcmd->orig_iocb.atio,
2116 case QLA_TGT_ABORT_ALL:
2117 case QLA_TGT_NEXUS_LOSS_SESS:
2118 case QLA_TGT_NEXUS_LOSS:
2119 qlt_send_notify_ack(mcmd->qpair,
2120 &mcmd->orig_iocb.imm_ntfy, 0, 0, 0, 0, 0, 0);
2123 spin_unlock_irqrestore(mcmd->qpair->qp_lock_ptr, flags);
2125 ql_dbg(ql_dbg_tgt_mgt, mcmd->vha, 0xf052,
2126 "qla_target(%d): tgt_ops->handle_tmr() failed: %d\n",
2127 mcmd->vha->vp_idx, rc);
2128 mempool_free(mcmd, qla_tgt_mgmt_cmd_mempool);
2132 /* ha->hardware_lock supposed to be held on entry */
2133 static int __qlt_24xx_handle_abts(struct scsi_qla_host *vha,
2134 struct abts_recv_from_24xx *abts, struct fc_port *sess)
2136 struct qla_hw_data *ha = vha->hw;
2137 struct qla_tgt_mgmt_cmd *mcmd;
2138 struct qla_qpair_hint *h = &vha->vha_tgt.qla_tgt->qphints[0];
2139 struct qla_tgt_cmd *abort_cmd;
2141 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00f,
2142 "qla_target(%d): task abort (tag=%d)\n",
2143 vha->vp_idx, abts->exchange_addr_to_abort);
2145 mcmd = mempool_alloc(qla_tgt_mgmt_cmd_mempool, GFP_ATOMIC);
2147 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf051,
2148 "qla_target(%d): %s: Allocation of ABORT cmd failed",
2149 vha->vp_idx, __func__);
2152 memset(mcmd, 0, sizeof(*mcmd));
2153 mcmd->cmd_type = TYPE_TGT_TMCMD;
2155 memcpy(&mcmd->orig_iocb.abts, abts, sizeof(mcmd->orig_iocb.abts));
2156 mcmd->reset_count = ha->base_qpair->chip_reset;
2157 mcmd->tmr_func = QLA_TGT_ABTS;
2158 mcmd->qpair = h->qpair;
2162 * LUN is looked up by target-core internally based on the passed
2163 * abts->exchange_addr_to_abort tag.
2165 mcmd->se_cmd.cpuid = h->cpuid;
2167 abort_cmd = ha->tgt.tgt_ops->find_cmd_by_tag(sess,
2168 le32_to_cpu(abts->exchange_addr_to_abort));
2171 mcmd->unpacked_lun = abort_cmd->se_cmd.orig_fe_lun;
2173 if (abort_cmd->qpair) {
2174 mcmd->qpair = abort_cmd->qpair;
2175 mcmd->se_cmd.cpuid = abort_cmd->se_cmd.cpuid;
2176 mcmd->abort_io_attr = abort_cmd->atio.u.isp24.attr;
2177 mcmd->flags = QLA24XX_MGMT_ABORT_IO_ATTR_VALID;
2180 INIT_WORK(&mcmd->work, qlt_do_tmr_work);
2181 queue_work_on(mcmd->se_cmd.cpuid, qla_tgt_wq, &mcmd->work);
2187 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
2189 static void qlt_24xx_handle_abts(struct scsi_qla_host *vha,
2190 struct abts_recv_from_24xx *abts)
2192 struct qla_hw_data *ha = vha->hw;
2193 struct fc_port *sess;
2194 uint32_t tag = le32_to_cpu(abts->exchange_addr_to_abort);
2197 unsigned long flags;
2199 if (le32_to_cpu(abts->fcp_hdr_le.parameter) & ABTS_PARAM_ABORT_SEQ) {
2200 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf053,
2201 "qla_target(%d): ABTS: Abort Sequence not "
2202 "supported\n", vha->vp_idx);
2203 qlt_24xx_send_abts_resp(ha->base_qpair, abts, FCP_TMF_REJECTED,
2208 if (tag == ATIO_EXCHANGE_ADDRESS_UNKNOWN) {
2209 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf010,
2210 "qla_target(%d): ABTS: Unknown Exchange "
2211 "Address received\n", vha->vp_idx);
2212 qlt_24xx_send_abts_resp(ha->base_qpair, abts, FCP_TMF_REJECTED,
2217 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf011,
2218 "qla_target(%d): task abort (s_id=%x:%x:%x, "
2219 "tag=%d, param=%x)\n", vha->vp_idx, abts->fcp_hdr_le.s_id.domain,
2220 abts->fcp_hdr_le.s_id.area, abts->fcp_hdr_le.s_id.al_pa, tag,
2221 le32_to_cpu(abts->fcp_hdr_le.parameter));
2223 s_id = le_id_to_be(abts->fcp_hdr_le.s_id);
2225 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
2226 sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha, s_id);
2228 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf012,
2229 "qla_target(%d): task abort for non-existent session\n",
2231 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
2233 qlt_24xx_send_abts_resp(ha->base_qpair, abts, FCP_TMF_REJECTED,
2237 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
2240 if (sess->deleted) {
2241 qlt_24xx_send_abts_resp(ha->base_qpair, abts, FCP_TMF_REJECTED,
2246 rc = __qlt_24xx_handle_abts(vha, abts, sess);
2248 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf054,
2249 "qla_target(%d): __qlt_24xx_handle_abts() failed: %d\n",
2251 qlt_24xx_send_abts_resp(ha->base_qpair, abts, FCP_TMF_REJECTED,
2258 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
2260 static void qlt_24xx_send_task_mgmt_ctio(struct qla_qpair *qpair,
2261 struct qla_tgt_mgmt_cmd *mcmd, uint32_t resp_code)
2263 struct scsi_qla_host *ha = mcmd->vha;
2264 struct atio_from_isp *atio = &mcmd->orig_iocb.atio;
2265 struct ctio7_to_24xx *ctio;
2268 ql_dbg(ql_dbg_tgt, ha, 0xe008,
2269 "Sending task mgmt CTIO7 (ha=%p, atio=%p, resp_code=%x\n",
2270 ha, atio, resp_code);
2273 ctio = (struct ctio7_to_24xx *)__qla2x00_alloc_iocbs(qpair, NULL);
2275 ql_dbg(ql_dbg_tgt, ha, 0xe04c,
2276 "qla_target(%d): %s failed: unable to allocate "
2277 "request packet\n", ha->vp_idx, __func__);
2281 ctio->entry_type = CTIO_TYPE7;
2282 ctio->entry_count = 1;
2283 ctio->handle = QLA_TGT_SKIP_HANDLE | CTIO_COMPLETION_HANDLE_MARK;
2284 ctio->nport_handle = cpu_to_le16(mcmd->sess->loop_id);
2285 ctio->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
2286 ctio->vp_index = ha->vp_idx;
2287 ctio->initiator_id = be_id_to_le(atio->u.isp24.fcp_hdr.s_id);
2288 ctio->exchange_addr = atio->u.isp24.exchange_addr;
2289 temp = (atio->u.isp24.attr << 9)|
2290 CTIO7_FLAGS_STATUS_MODE_1 | CTIO7_FLAGS_SEND_STATUS;
2291 ctio->u.status1.flags = cpu_to_le16(temp);
2292 temp = be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id);
2293 ctio->u.status1.ox_id = cpu_to_le16(temp);
2294 ctio->u.status1.scsi_status =
2295 cpu_to_le16(SS_RESPONSE_INFO_LEN_VALID);
2296 ctio->u.status1.response_len = cpu_to_le16(8);
2297 ctio->u.status1.sense_data[0] = resp_code;
2299 /* Memory Barrier */
2301 if (qpair->reqq_start_iocbs)
2302 qpair->reqq_start_iocbs(qpair);
2304 qla2x00_start_iocbs(ha, qpair->req);
2307 void qlt_free_mcmd(struct qla_tgt_mgmt_cmd *mcmd)
2309 mempool_free(mcmd, qla_tgt_mgmt_cmd_mempool);
2311 EXPORT_SYMBOL(qlt_free_mcmd);
2314 * ha->hardware_lock supposed to be held on entry. Might drop it, then
2317 void qlt_send_resp_ctio(struct qla_qpair *qpair, struct qla_tgt_cmd *cmd,
2318 uint8_t scsi_status, uint8_t sense_key, uint8_t asc, uint8_t ascq)
2320 struct atio_from_isp *atio = &cmd->atio;
2321 struct ctio7_to_24xx *ctio;
2323 struct scsi_qla_host *vha = cmd->vha;
2325 ql_dbg(ql_dbg_tgt_dif, vha, 0x3066,
2326 "Sending response CTIO7 (vha=%p, atio=%p, scsi_status=%02x, "
2327 "sense_key=%02x, asc=%02x, ascq=%02x",
2328 vha, atio, scsi_status, sense_key, asc, ascq);
2330 ctio = (struct ctio7_to_24xx *)qla2x00_alloc_iocbs(vha, NULL);
2332 ql_dbg(ql_dbg_async, vha, 0x3067,
2333 "qla2x00t(%ld): %s failed: unable to allocate request packet",
2334 vha->host_no, __func__);
2338 ctio->entry_type = CTIO_TYPE7;
2339 ctio->entry_count = 1;
2340 ctio->handle = QLA_TGT_SKIP_HANDLE;
2341 ctio->nport_handle = cpu_to_le16(cmd->sess->loop_id);
2342 ctio->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
2343 ctio->vp_index = vha->vp_idx;
2344 ctio->initiator_id = be_id_to_le(atio->u.isp24.fcp_hdr.s_id);
2345 ctio->exchange_addr = atio->u.isp24.exchange_addr;
2346 temp = (atio->u.isp24.attr << 9) |
2347 CTIO7_FLAGS_STATUS_MODE_1 | CTIO7_FLAGS_SEND_STATUS;
2348 ctio->u.status1.flags = cpu_to_le16(temp);
2349 temp = be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id);
2350 ctio->u.status1.ox_id = cpu_to_le16(temp);
2351 ctio->u.status1.scsi_status =
2352 cpu_to_le16(SS_RESPONSE_INFO_LEN_VALID | scsi_status);
2353 ctio->u.status1.response_len = cpu_to_le16(18);
2354 ctio->u.status1.residual = cpu_to_le32(get_datalen_for_atio(atio));
2356 if (ctio->u.status1.residual != 0)
2357 ctio->u.status1.scsi_status |=
2358 cpu_to_le16(SS_RESIDUAL_UNDER);
2360 /* Fixed format sense data. */
2361 ctio->u.status1.sense_data[0] = 0x70;
2362 ctio->u.status1.sense_data[2] = sense_key;
2363 /* Additional sense length */
2364 ctio->u.status1.sense_data[7] = 0xa;
2366 ctio->u.status1.sense_data[12] = asc;
2367 ctio->u.status1.sense_data[13] = ascq;
2369 /* Memory Barrier */
2372 if (qpair->reqq_start_iocbs)
2373 qpair->reqq_start_iocbs(qpair);
2375 qla2x00_start_iocbs(vha, qpair->req);
2381 /* callback from target fabric module code */
2382 void qlt_xmit_tm_rsp(struct qla_tgt_mgmt_cmd *mcmd)
2384 struct scsi_qla_host *vha = mcmd->sess->vha;
2385 struct qla_hw_data *ha = vha->hw;
2386 unsigned long flags;
2387 struct qla_qpair *qpair = mcmd->qpair;
2388 bool free_mcmd = true;
2390 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf013,
2391 "TM response mcmd (%p) status %#x state %#x",
2392 mcmd, mcmd->fc_tm_rsp, mcmd->flags);
2394 spin_lock_irqsave(qpair->qp_lock_ptr, flags);
2396 if (!vha->flags.online || mcmd->reset_count != qpair->chip_reset) {
2398 * Either the port is not online or this request was from
2399 * previous life, just abort the processing.
2401 ql_dbg(ql_dbg_async, vha, 0xe100,
2402 "RESET-TMR online/active/old-count/new-count = %d/%d/%d/%d.\n",
2403 vha->flags.online, qla2x00_reset_active(vha),
2404 mcmd->reset_count, qpair->chip_reset);
2405 ha->tgt.tgt_ops->free_mcmd(mcmd);
2406 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
2410 if (mcmd->flags == QLA24XX_MGMT_SEND_NACK) {
2411 switch (mcmd->orig_iocb.imm_ntfy.u.isp24.status_subcode) {
2415 ql_dbg(ql_dbg_disc, vha, 0x2106,
2416 "TM response logo %8phC status %#x state %#x",
2417 mcmd->sess->port_name, mcmd->fc_tm_rsp,
2419 qlt_schedule_sess_for_deletion(mcmd->sess);
2422 qlt_send_notify_ack(vha->hw->base_qpair,
2423 &mcmd->orig_iocb.imm_ntfy, 0, 0, 0, 0, 0, 0);
2427 if (mcmd->orig_iocb.atio.u.raw.entry_type == ABTS_RECV_24XX) {
2428 qlt_build_abts_resp_iocb(mcmd);
2431 qlt_24xx_send_task_mgmt_ctio(qpair, mcmd,
2435 * Make the callback for ->free_mcmd() to queue_work() and invoke
2436 * target_put_sess_cmd() to drop cmd_kref to 1. The final
2437 * target_put_sess_cmd() call will be made from TFO->check_stop_free()
2438 * -> tcm_qla2xxx_check_stop_free() to release the TMR associated se_cmd
2439 * descriptor after TFO->queue_tm_rsp() -> tcm_qla2xxx_queue_tm_rsp() ->
2440 * qlt_xmit_tm_rsp() returns here..
2443 ha->tgt.tgt_ops->free_mcmd(mcmd);
2445 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
2447 EXPORT_SYMBOL(qlt_xmit_tm_rsp);
2450 static int qlt_pci_map_calc_cnt(struct qla_tgt_prm *prm)
2452 struct qla_tgt_cmd *cmd = prm->cmd;
2454 BUG_ON(cmd->sg_cnt == 0);
2456 prm->sg = (struct scatterlist *)cmd->sg;
2457 prm->seg_cnt = dma_map_sg(&cmd->qpair->pdev->dev, cmd->sg,
2458 cmd->sg_cnt, cmd->dma_data_direction);
2459 if (unlikely(prm->seg_cnt == 0))
2462 prm->cmd->sg_mapped = 1;
2464 if (cmd->se_cmd.prot_op == TARGET_PROT_NORMAL) {
2466 * If greater than four sg entries then we need to allocate
2467 * the continuation entries
2469 if (prm->seg_cnt > QLA_TGT_DATASEGS_PER_CMD_24XX)
2470 prm->req_cnt += DIV_ROUND_UP(prm->seg_cnt -
2471 QLA_TGT_DATASEGS_PER_CMD_24XX,
2472 QLA_TGT_DATASEGS_PER_CONT_24XX);
2475 if ((cmd->se_cmd.prot_op == TARGET_PROT_DIN_INSERT) ||
2476 (cmd->se_cmd.prot_op == TARGET_PROT_DOUT_STRIP)) {
2477 prm->seg_cnt = DIV_ROUND_UP(cmd->bufflen, cmd->blk_sz);
2478 prm->tot_dsds = prm->seg_cnt;
2480 prm->tot_dsds = prm->seg_cnt;
2482 if (cmd->prot_sg_cnt) {
2483 prm->prot_sg = cmd->prot_sg;
2484 prm->prot_seg_cnt = dma_map_sg(&cmd->qpair->pdev->dev,
2485 cmd->prot_sg, cmd->prot_sg_cnt,
2486 cmd->dma_data_direction);
2487 if (unlikely(prm->prot_seg_cnt == 0))
2490 if ((cmd->se_cmd.prot_op == TARGET_PROT_DIN_INSERT) ||
2491 (cmd->se_cmd.prot_op == TARGET_PROT_DOUT_STRIP)) {
2492 /* Dif Bundling not support here */
2493 prm->prot_seg_cnt = DIV_ROUND_UP(cmd->bufflen,
2495 prm->tot_dsds += prm->prot_seg_cnt;
2497 prm->tot_dsds += prm->prot_seg_cnt;
2504 ql_dbg_qp(ql_dbg_tgt, prm->cmd->qpair, 0xe04d,
2505 "qla_target(%d): PCI mapping failed: sg_cnt=%d",
2506 0, prm->cmd->sg_cnt);
2510 static void qlt_unmap_sg(struct scsi_qla_host *vha, struct qla_tgt_cmd *cmd)
2512 struct qla_hw_data *ha;
2513 struct qla_qpair *qpair;
2515 if (!cmd->sg_mapped)
2520 dma_unmap_sg(&qpair->pdev->dev, cmd->sg, cmd->sg_cnt,
2521 cmd->dma_data_direction);
2524 if (cmd->prot_sg_cnt)
2525 dma_unmap_sg(&qpair->pdev->dev, cmd->prot_sg, cmd->prot_sg_cnt,
2526 cmd->dma_data_direction);
2531 if (cmd->ctx_dsd_alloced)
2532 qla2x00_clean_dsd_pool(ha, cmd->ctx);
2534 dma_pool_free(ha->dl_dma_pool, cmd->ctx, cmd->ctx->crc_ctx_dma);
2537 static int qlt_check_reserve_free_req(struct qla_qpair *qpair,
2541 struct req_que *req = qpair->req;
2543 if (req->cnt < (req_cnt + 2)) {
2544 cnt = (uint16_t)(qpair->use_shadow_reg ? *req->out_ptr :
2545 rd_reg_dword_relaxed(req->req_q_out));
2547 if (req->ring_index < cnt)
2548 req->cnt = cnt - req->ring_index;
2550 req->cnt = req->length - (req->ring_index - cnt);
2552 if (unlikely(req->cnt < (req_cnt + 2)))
2556 req->cnt -= req_cnt;
2562 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
2564 static inline void *qlt_get_req_pkt(struct req_que *req)
2566 /* Adjust ring index. */
2568 if (req->ring_index == req->length) {
2569 req->ring_index = 0;
2570 req->ring_ptr = req->ring;
2574 return (cont_entry_t *)req->ring_ptr;
2577 /* ha->hardware_lock supposed to be held on entry */
2578 static inline uint32_t qlt_make_handle(struct qla_qpair *qpair)
2583 struct req_que *req = qpair->req;
2585 h = req->current_outstanding_cmd;
2587 for (index = 1; index < req->num_outstanding_cmds; index++) {
2589 if (h == req->num_outstanding_cmds)
2592 if (h == QLA_TGT_SKIP_HANDLE)
2595 if (!req->outstanding_cmds[h]) {
2602 req->current_outstanding_cmd = h;
2604 ql_dbg(ql_dbg_io, qpair->vha, 0x305b,
2605 "qla_target(%d): Ran out of empty cmd slots\n",
2606 qpair->vha->vp_idx);
2607 h = QLA_TGT_NULL_HANDLE;
2613 /* ha->hardware_lock supposed to be held on entry */
2614 static int qlt_24xx_build_ctio_pkt(struct qla_qpair *qpair,
2615 struct qla_tgt_prm *prm)
2618 struct ctio7_to_24xx *pkt;
2619 struct atio_from_isp *atio = &prm->cmd->atio;
2621 struct qla_tgt_cmd *cmd = prm->cmd;
2623 pkt = (struct ctio7_to_24xx *)qpair->req->ring_ptr;
2625 memset(pkt, 0, sizeof(*pkt));
2627 pkt->entry_type = CTIO_TYPE7;
2628 pkt->entry_count = (uint8_t)prm->req_cnt;
2629 pkt->vp_index = prm->cmd->vp_idx;
2631 h = qlt_make_handle(qpair);
2632 if (unlikely(h == QLA_TGT_NULL_HANDLE)) {
2634 * CTIO type 7 from the firmware doesn't provide a way to
2635 * know the initiator's LOOP ID, hence we can't find
2636 * the session and, so, the command.
2640 qpair->req->outstanding_cmds[h] = (srb_t *)prm->cmd;
2642 pkt->handle = make_handle(qpair->req->id, h);
2643 pkt->handle |= CTIO_COMPLETION_HANDLE_MARK;
2644 pkt->nport_handle = cpu_to_le16(prm->cmd->loop_id);
2645 pkt->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
2646 pkt->initiator_id = be_id_to_le(atio->u.isp24.fcp_hdr.s_id);
2647 pkt->exchange_addr = atio->u.isp24.exchange_addr;
2648 temp = atio->u.isp24.attr << 9;
2649 pkt->u.status0.flags |= cpu_to_le16(temp);
2650 temp = be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id);
2651 pkt->u.status0.ox_id = cpu_to_le16(temp);
2652 pkt->u.status0.relative_offset = cpu_to_le32(prm->cmd->offset);
2655 if (cmd->dma_data_direction == DMA_TO_DEVICE)
2656 prm->cmd->sess->edif.rx_bytes += cmd->bufflen;
2657 if (cmd->dma_data_direction == DMA_FROM_DEVICE)
2658 prm->cmd->sess->edif.tx_bytes += cmd->bufflen;
2660 pkt->u.status0.edif_flags |= EF_EN_EDIF;
2667 * ha->hardware_lock supposed to be held on entry. We have already made sure
2668 * that there is sufficient amount of request entries to not drop it.
2670 static void qlt_load_cont_data_segments(struct qla_tgt_prm *prm)
2673 struct dsd64 *cur_dsd;
2675 /* Build continuation packets */
2676 while (prm->seg_cnt > 0) {
2677 cont_a64_entry_t *cont_pkt64 =
2678 (cont_a64_entry_t *)qlt_get_req_pkt(
2679 prm->cmd->qpair->req);
2682 * Make sure that from cont_pkt64 none of
2683 * 64-bit specific fields used for 32-bit
2684 * addressing. Cast to (cont_entry_t *) for
2688 memset(cont_pkt64, 0, sizeof(*cont_pkt64));
2690 cont_pkt64->entry_count = 1;
2691 cont_pkt64->sys_define = 0;
2693 cont_pkt64->entry_type = CONTINUE_A64_TYPE;
2694 cur_dsd = cont_pkt64->dsd;
2696 /* Load continuation entry data segments */
2698 cnt < QLA_TGT_DATASEGS_PER_CONT_24XX && prm->seg_cnt;
2699 cnt++, prm->seg_cnt--) {
2700 append_dsd64(&cur_dsd, prm->sg);
2701 prm->sg = sg_next(prm->sg);
2707 * ha->hardware_lock supposed to be held on entry. We have already made sure
2708 * that there is sufficient amount of request entries to not drop it.
2710 static void qlt_load_data_segments(struct qla_tgt_prm *prm)
2713 struct dsd64 *cur_dsd;
2714 struct ctio7_to_24xx *pkt24 = (struct ctio7_to_24xx *)prm->pkt;
2716 pkt24->u.status0.transfer_length = cpu_to_le32(prm->cmd->bufflen);
2718 /* Setup packet address segment pointer */
2719 cur_dsd = &pkt24->u.status0.dsd;
2721 /* Set total data segment count */
2723 pkt24->dseg_count = cpu_to_le16(prm->seg_cnt);
2725 if (prm->seg_cnt == 0) {
2726 /* No data transfer */
2727 cur_dsd->address = 0;
2728 cur_dsd->length = 0;
2732 /* If scatter gather */
2734 /* Load command entry data segments */
2736 (cnt < QLA_TGT_DATASEGS_PER_CMD_24XX) && prm->seg_cnt;
2737 cnt++, prm->seg_cnt--) {
2738 append_dsd64(&cur_dsd, prm->sg);
2739 prm->sg = sg_next(prm->sg);
2742 qlt_load_cont_data_segments(prm);
2745 static inline int qlt_has_data(struct qla_tgt_cmd *cmd)
2747 return cmd->bufflen > 0;
2750 static void qlt_print_dif_err(struct qla_tgt_prm *prm)
2752 struct qla_tgt_cmd *cmd;
2753 struct scsi_qla_host *vha;
2755 /* asc 0x10=dif error */
2756 if (prm->sense_buffer && (prm->sense_buffer[12] == 0x10)) {
2760 switch (prm->sense_buffer[13]) {
2762 ql_dbg(ql_dbg_tgt_dif, vha, 0xe00b,
2763 "BE detected Guard TAG ERR: lba[0x%llx|%lld] len[0x%x] "
2764 "se_cmd=%p tag[%x]",
2765 cmd->lba, cmd->lba, cmd->num_blks, &cmd->se_cmd,
2766 cmd->atio.u.isp24.exchange_addr);
2769 ql_dbg(ql_dbg_tgt_dif, vha, 0xe00c,
2770 "BE detected APP TAG ERR: lba[0x%llx|%lld] len[0x%x] "
2771 "se_cmd=%p tag[%x]",
2772 cmd->lba, cmd->lba, cmd->num_blks, &cmd->se_cmd,
2773 cmd->atio.u.isp24.exchange_addr);
2776 ql_dbg(ql_dbg_tgt_dif, vha, 0xe00f,
2777 "BE detected REF TAG ERR: lba[0x%llx|%lld] len[0x%x] "
2778 "se_cmd=%p tag[%x]",
2779 cmd->lba, cmd->lba, cmd->num_blks, &cmd->se_cmd,
2780 cmd->atio.u.isp24.exchange_addr);
2783 ql_dbg(ql_dbg_tgt_dif, vha, 0xe010,
2784 "BE detected Dif ERR: lba[%llx|%lld] len[%x] "
2785 "se_cmd=%p tag[%x]",
2786 cmd->lba, cmd->lba, cmd->num_blks, &cmd->se_cmd,
2787 cmd->atio.u.isp24.exchange_addr);
2790 ql_dump_buffer(ql_dbg_tgt_dif, vha, 0xe011, cmd->cdb, 16);
2795 * Called without ha->hardware_lock held
2797 static int qlt_pre_xmit_response(struct qla_tgt_cmd *cmd,
2798 struct qla_tgt_prm *prm, int xmit_type, uint8_t scsi_status,
2799 uint32_t *full_req_cnt)
2801 struct se_cmd *se_cmd = &cmd->se_cmd;
2802 struct qla_qpair *qpair = cmd->qpair;
2805 prm->tgt = cmd->tgt;
2807 prm->rq_result = scsi_status;
2808 prm->sense_buffer = &cmd->sense_buffer[0];
2809 prm->sense_buffer_len = TRANSPORT_SENSE_BUFFER;
2814 prm->add_status_pkt = 0;
2815 prm->prot_sg = NULL;
2816 prm->prot_seg_cnt = 0;
2819 if ((xmit_type & QLA_TGT_XMIT_DATA) && qlt_has_data(cmd)) {
2820 if (qlt_pci_map_calc_cnt(prm) != 0)
2824 *full_req_cnt = prm->req_cnt;
2826 if (se_cmd->se_cmd_flags & SCF_UNDERFLOW_BIT) {
2827 prm->residual = se_cmd->residual_count;
2828 ql_dbg_qp(ql_dbg_io + ql_dbg_verbose, qpair, 0x305c,
2829 "Residual underflow: %d (tag %lld, op %x, bufflen %d, rq_result %x)\n",
2830 prm->residual, se_cmd->tag,
2831 se_cmd->t_task_cdb ? se_cmd->t_task_cdb[0] : 0,
2832 cmd->bufflen, prm->rq_result);
2833 prm->rq_result |= SS_RESIDUAL_UNDER;
2834 } else if (se_cmd->se_cmd_flags & SCF_OVERFLOW_BIT) {
2835 prm->residual = se_cmd->residual_count;
2836 ql_dbg_qp(ql_dbg_io, qpair, 0x305d,
2837 "Residual overflow: %d (tag %lld, op %x, bufflen %d, rq_result %x)\n",
2838 prm->residual, se_cmd->tag, se_cmd->t_task_cdb ?
2839 se_cmd->t_task_cdb[0] : 0, cmd->bufflen, prm->rq_result);
2840 prm->rq_result |= SS_RESIDUAL_OVER;
2843 if (xmit_type & QLA_TGT_XMIT_STATUS) {
2845 * If QLA_TGT_XMIT_DATA is not set, add_status_pkt will be
2846 * ignored in *xmit_response() below
2848 if (qlt_has_data(cmd)) {
2849 if (QLA_TGT_SENSE_VALID(prm->sense_buffer) ||
2850 (IS_FWI2_CAPABLE(cmd->vha->hw) &&
2851 (prm->rq_result != 0))) {
2852 prm->add_status_pkt = 1;
2861 static inline int qlt_need_explicit_conf(struct qla_tgt_cmd *cmd,
2864 if (cmd->qpair->enable_class_2)
2868 return cmd->conf_compl_supported;
2870 return cmd->qpair->enable_explicit_conf &&
2871 cmd->conf_compl_supported;
2874 static void qlt_24xx_init_ctio_to_isp(struct ctio7_to_24xx *ctio,
2875 struct qla_tgt_prm *prm)
2877 prm->sense_buffer_len = min_t(uint32_t, prm->sense_buffer_len,
2878 (uint32_t)sizeof(ctio->u.status1.sense_data));
2879 ctio->u.status0.flags |= cpu_to_le16(CTIO7_FLAGS_SEND_STATUS);
2880 if (qlt_need_explicit_conf(prm->cmd, 0)) {
2881 ctio->u.status0.flags |= cpu_to_le16(
2882 CTIO7_FLAGS_EXPLICIT_CONFORM |
2883 CTIO7_FLAGS_CONFORM_REQ);
2885 ctio->u.status0.residual = cpu_to_le32(prm->residual);
2886 ctio->u.status0.scsi_status = cpu_to_le16(prm->rq_result);
2887 if (QLA_TGT_SENSE_VALID(prm->sense_buffer)) {
2890 if (qlt_need_explicit_conf(prm->cmd, 1)) {
2891 if ((prm->rq_result & SS_SCSI_STATUS_BYTE) != 0) {
2892 ql_dbg_qp(ql_dbg_tgt, prm->cmd->qpair, 0xe017,
2893 "Skipping EXPLICIT_CONFORM and "
2894 "CTIO7_FLAGS_CONFORM_REQ for FCP READ w/ "
2895 "non GOOD status\n");
2896 goto skip_explict_conf;
2898 ctio->u.status1.flags |= cpu_to_le16(
2899 CTIO7_FLAGS_EXPLICIT_CONFORM |
2900 CTIO7_FLAGS_CONFORM_REQ);
2903 ctio->u.status1.flags &=
2904 ~cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_0);
2905 ctio->u.status1.flags |=
2906 cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_1);
2907 ctio->u.status1.scsi_status |=
2908 cpu_to_le16(SS_SENSE_LEN_VALID);
2909 ctio->u.status1.sense_length =
2910 cpu_to_le16(prm->sense_buffer_len);
2911 for (i = 0; i < prm->sense_buffer_len/4; i++) {
2914 v = get_unaligned_be32(
2915 &((uint32_t *)prm->sense_buffer)[i]);
2916 put_unaligned_le32(v,
2917 &((uint32_t *)ctio->u.status1.sense_data)[i]);
2919 qlt_print_dif_err(prm);
2922 ctio->u.status1.flags &=
2923 ~cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_0);
2924 ctio->u.status1.flags |=
2925 cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_1);
2926 ctio->u.status1.sense_length = 0;
2927 memset(ctio->u.status1.sense_data, 0,
2928 sizeof(ctio->u.status1.sense_data));
2931 /* Sense with len > 24, is it possible ??? */
2935 qlt_hba_err_chk_enabled(struct se_cmd *se_cmd)
2937 switch (se_cmd->prot_op) {
2938 case TARGET_PROT_DOUT_INSERT:
2939 case TARGET_PROT_DIN_STRIP:
2940 if (ql2xenablehba_err_chk >= 1)
2943 case TARGET_PROT_DOUT_PASS:
2944 case TARGET_PROT_DIN_PASS:
2945 if (ql2xenablehba_err_chk >= 2)
2948 case TARGET_PROT_DIN_INSERT:
2949 case TARGET_PROT_DOUT_STRIP:
2958 qla_tgt_ref_mask_check(struct se_cmd *se_cmd)
2960 switch (se_cmd->prot_op) {
2961 case TARGET_PROT_DIN_INSERT:
2962 case TARGET_PROT_DOUT_INSERT:
2963 case TARGET_PROT_DIN_STRIP:
2964 case TARGET_PROT_DOUT_STRIP:
2965 case TARGET_PROT_DIN_PASS:
2966 case TARGET_PROT_DOUT_PASS:
2975 * qla_tgt_set_dif_tags - Extract Ref and App tags from SCSI command
2978 qla_tgt_set_dif_tags(struct qla_tgt_cmd *cmd, struct crc_context *ctx,
2979 uint16_t *pfw_prot_opts)
2981 struct se_cmd *se_cmd = &cmd->se_cmd;
2982 uint32_t lba = 0xffffffff & se_cmd->t_task_lba;
2983 scsi_qla_host_t *vha = cmd->tgt->vha;
2984 struct qla_hw_data *ha = vha->hw;
2988 * wait till Mode Sense/Select cmd, modepage Ah, subpage 2
2989 * have been immplemented by TCM, before AppTag is avail.
2990 * Look for modesense_handlers[]
2993 ctx->app_tag_mask[0] = 0x0;
2994 ctx->app_tag_mask[1] = 0x0;
2996 if (IS_PI_UNINIT_CAPABLE(ha)) {
2997 if ((se_cmd->prot_type == TARGET_DIF_TYPE1_PROT) ||
2998 (se_cmd->prot_type == TARGET_DIF_TYPE2_PROT))
2999 *pfw_prot_opts |= PO_DIS_VALD_APP_ESC;
3000 else if (se_cmd->prot_type == TARGET_DIF_TYPE3_PROT)
3001 *pfw_prot_opts |= PO_DIS_VALD_APP_REF_ESC;
3004 t32 = ha->tgt.tgt_ops->get_dif_tags(cmd, pfw_prot_opts);
3006 switch (se_cmd->prot_type) {
3007 case TARGET_DIF_TYPE0_PROT:
3009 * No check for ql2xenablehba_err_chk, as it
3010 * would be an I/O error if hba tag generation
3013 ctx->ref_tag = cpu_to_le32(lba);
3014 /* enable ALL bytes of the ref tag */
3015 ctx->ref_tag_mask[0] = 0xff;
3016 ctx->ref_tag_mask[1] = 0xff;
3017 ctx->ref_tag_mask[2] = 0xff;
3018 ctx->ref_tag_mask[3] = 0xff;
3020 case TARGET_DIF_TYPE1_PROT:
3022 * For TYPE 1 protection: 16 bit GUARD tag, 32 bit
3023 * REF tag, and 16 bit app tag.
3025 ctx->ref_tag = cpu_to_le32(lba);
3026 if (!qla_tgt_ref_mask_check(se_cmd) ||
3027 !(ha->tgt.tgt_ops->chk_dif_tags(t32))) {
3028 *pfw_prot_opts |= PO_DIS_REF_TAG_VALD;
3031 /* enable ALL bytes of the ref tag */
3032 ctx->ref_tag_mask[0] = 0xff;
3033 ctx->ref_tag_mask[1] = 0xff;
3034 ctx->ref_tag_mask[2] = 0xff;
3035 ctx->ref_tag_mask[3] = 0xff;
3037 case TARGET_DIF_TYPE2_PROT:
3039 * For TYPE 2 protection: 16 bit GUARD + 32 bit REF
3040 * tag has to match LBA in CDB + N
3042 ctx->ref_tag = cpu_to_le32(lba);
3043 if (!qla_tgt_ref_mask_check(se_cmd) ||
3044 !(ha->tgt.tgt_ops->chk_dif_tags(t32))) {
3045 *pfw_prot_opts |= PO_DIS_REF_TAG_VALD;
3048 /* enable ALL bytes of the ref tag */
3049 ctx->ref_tag_mask[0] = 0xff;
3050 ctx->ref_tag_mask[1] = 0xff;
3051 ctx->ref_tag_mask[2] = 0xff;
3052 ctx->ref_tag_mask[3] = 0xff;
3054 case TARGET_DIF_TYPE3_PROT:
3055 /* For TYPE 3 protection: 16 bit GUARD only */
3056 *pfw_prot_opts |= PO_DIS_REF_TAG_VALD;
3057 ctx->ref_tag_mask[0] = ctx->ref_tag_mask[1] =
3058 ctx->ref_tag_mask[2] = ctx->ref_tag_mask[3] = 0x00;
3064 qlt_build_ctio_crc2_pkt(struct qla_qpair *qpair, struct qla_tgt_prm *prm)
3066 struct dsd64 *cur_dsd;
3067 uint32_t transfer_length = 0;
3068 uint32_t data_bytes;
3070 uint8_t bundling = 1;
3071 struct crc_context *crc_ctx_pkt = NULL;
3072 struct qla_hw_data *ha;
3073 struct ctio_crc2_to_fw *pkt;
3074 dma_addr_t crc_ctx_dma;
3075 uint16_t fw_prot_opts = 0;
3076 struct qla_tgt_cmd *cmd = prm->cmd;
3077 struct se_cmd *se_cmd = &cmd->se_cmd;
3079 struct atio_from_isp *atio = &prm->cmd->atio;
3080 struct qla_tc_param tc;
3082 scsi_qla_host_t *vha = cmd->vha;
3086 pkt = (struct ctio_crc2_to_fw *)qpair->req->ring_ptr;
3088 memset(pkt, 0, sizeof(*pkt));
3090 ql_dbg_qp(ql_dbg_tgt, cmd->qpair, 0xe071,
3091 "qla_target(%d):%s: se_cmd[%p] CRC2 prot_op[0x%x] cmd prot sg:cnt[%p:%x] lba[%llu]\n",
3092 cmd->vp_idx, __func__, se_cmd, se_cmd->prot_op,
3093 prm->prot_sg, prm->prot_seg_cnt, se_cmd->t_task_lba);
3095 if ((se_cmd->prot_op == TARGET_PROT_DIN_INSERT) ||
3096 (se_cmd->prot_op == TARGET_PROT_DOUT_STRIP))
3099 /* Compute dif len and adjust data len to incude protection */
3100 data_bytes = cmd->bufflen;
3101 dif_bytes = (data_bytes / cmd->blk_sz) * 8;
3103 switch (se_cmd->prot_op) {
3104 case TARGET_PROT_DIN_INSERT:
3105 case TARGET_PROT_DOUT_STRIP:
3106 transfer_length = data_bytes;
3107 if (cmd->prot_sg_cnt)
3108 data_bytes += dif_bytes;
3110 case TARGET_PROT_DIN_STRIP:
3111 case TARGET_PROT_DOUT_INSERT:
3112 case TARGET_PROT_DIN_PASS:
3113 case TARGET_PROT_DOUT_PASS:
3114 transfer_length = data_bytes + dif_bytes;
3121 if (!qlt_hba_err_chk_enabled(se_cmd))
3122 fw_prot_opts |= 0x10; /* Disable Guard tag checking */
3123 /* HBA error checking enabled */
3124 else if (IS_PI_UNINIT_CAPABLE(ha)) {
3125 if ((se_cmd->prot_type == TARGET_DIF_TYPE1_PROT) ||
3126 (se_cmd->prot_type == TARGET_DIF_TYPE2_PROT))
3127 fw_prot_opts |= PO_DIS_VALD_APP_ESC;
3128 else if (se_cmd->prot_type == TARGET_DIF_TYPE3_PROT)
3129 fw_prot_opts |= PO_DIS_VALD_APP_REF_ESC;
3132 switch (se_cmd->prot_op) {
3133 case TARGET_PROT_DIN_INSERT:
3134 case TARGET_PROT_DOUT_INSERT:
3135 fw_prot_opts |= PO_MODE_DIF_INSERT;
3137 case TARGET_PROT_DIN_STRIP:
3138 case TARGET_PROT_DOUT_STRIP:
3139 fw_prot_opts |= PO_MODE_DIF_REMOVE;
3141 case TARGET_PROT_DIN_PASS:
3142 case TARGET_PROT_DOUT_PASS:
3143 fw_prot_opts |= PO_MODE_DIF_PASS;
3144 /* FUTURE: does tcm require T10CRC<->IPCKSUM conversion? */
3146 default:/* Normal Request */
3147 fw_prot_opts |= PO_MODE_DIF_PASS;
3152 /* Update entry type to indicate Command Type CRC_2 IOCB */
3153 pkt->entry_type = CTIO_CRC2;
3154 pkt->entry_count = 1;
3155 pkt->vp_index = cmd->vp_idx;
3157 h = qlt_make_handle(qpair);
3158 if (unlikely(h == QLA_TGT_NULL_HANDLE)) {
3160 * CTIO type 7 from the firmware doesn't provide a way to
3161 * know the initiator's LOOP ID, hence we can't find
3162 * the session and, so, the command.
3166 qpair->req->outstanding_cmds[h] = (srb_t *)prm->cmd;
3168 pkt->handle = make_handle(qpair->req->id, h);
3169 pkt->handle |= CTIO_COMPLETION_HANDLE_MARK;
3170 pkt->nport_handle = cpu_to_le16(prm->cmd->loop_id);
3171 pkt->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
3172 pkt->initiator_id = be_id_to_le(atio->u.isp24.fcp_hdr.s_id);
3173 pkt->exchange_addr = atio->u.isp24.exchange_addr;
3175 /* silence compile warning */
3176 t16 = be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id);
3177 pkt->ox_id = cpu_to_le16(t16);
3179 t16 = (atio->u.isp24.attr << 9);
3180 pkt->flags |= cpu_to_le16(t16);
3181 pkt->relative_offset = cpu_to_le32(prm->cmd->offset);
3183 /* Set transfer direction */
3184 if (cmd->dma_data_direction == DMA_TO_DEVICE)
3185 pkt->flags = cpu_to_le16(CTIO7_FLAGS_DATA_IN);
3186 else if (cmd->dma_data_direction == DMA_FROM_DEVICE)
3187 pkt->flags = cpu_to_le16(CTIO7_FLAGS_DATA_OUT);
3189 pkt->dseg_count = cpu_to_le16(prm->tot_dsds);
3190 /* Fibre channel byte count */
3191 pkt->transfer_length = cpu_to_le32(transfer_length);
3193 /* ----- CRC context -------- */
3195 /* Allocate CRC context from global pool */
3196 crc_ctx_pkt = cmd->ctx =
3197 dma_pool_zalloc(ha->dl_dma_pool, GFP_ATOMIC, &crc_ctx_dma);
3200 goto crc_queuing_error;
3202 crc_ctx_pkt->crc_ctx_dma = crc_ctx_dma;
3203 INIT_LIST_HEAD(&crc_ctx_pkt->dsd_list);
3206 crc_ctx_pkt->handle = pkt->handle;
3208 qla_tgt_set_dif_tags(cmd, crc_ctx_pkt, &fw_prot_opts);
3210 put_unaligned_le64(crc_ctx_dma, &pkt->crc_context_address);
3211 pkt->crc_context_len = cpu_to_le16(CRC_CONTEXT_LEN_FW);
3214 cur_dsd = &crc_ctx_pkt->u.nobundling.data_dsd[0];
3217 * Configure Bundling if we need to fetch interlaving
3218 * protection PCI accesses
3220 fw_prot_opts |= PO_ENABLE_DIF_BUNDLING;
3221 crc_ctx_pkt->u.bundling.dif_byte_count = cpu_to_le32(dif_bytes);
3222 crc_ctx_pkt->u.bundling.dseg_count =
3223 cpu_to_le16(prm->tot_dsds - prm->prot_seg_cnt);
3224 cur_dsd = &crc_ctx_pkt->u.bundling.data_dsd[0];
3227 /* Finish the common fields of CRC pkt */
3228 crc_ctx_pkt->blk_size = cpu_to_le16(cmd->blk_sz);
3229 crc_ctx_pkt->prot_opts = cpu_to_le16(fw_prot_opts);
3230 crc_ctx_pkt->byte_count = cpu_to_le32(data_bytes);
3231 crc_ctx_pkt->guard_seed = cpu_to_le16(0);
3233 memset((uint8_t *)&tc, 0 , sizeof(tc));
3235 tc.blk_sz = cmd->blk_sz;
3236 tc.bufflen = cmd->bufflen;
3238 tc.prot_sg = cmd->prot_sg;
3239 tc.ctx = crc_ctx_pkt;
3240 tc.ctx_dsd_alloced = &cmd->ctx_dsd_alloced;
3242 /* Walks data segments */
3243 pkt->flags |= cpu_to_le16(CTIO7_FLAGS_DSD_PTR);
3245 if (!bundling && prm->prot_seg_cnt) {
3246 if (qla24xx_walk_and_build_sglist_no_difb(ha, NULL, cur_dsd,
3247 prm->tot_dsds, &tc))
3248 goto crc_queuing_error;
3249 } else if (qla24xx_walk_and_build_sglist(ha, NULL, cur_dsd,
3250 (prm->tot_dsds - prm->prot_seg_cnt), &tc))
3251 goto crc_queuing_error;
3253 if (bundling && prm->prot_seg_cnt) {
3254 /* Walks dif segments */
3255 pkt->add_flags |= CTIO_CRC2_AF_DIF_DSD_ENA;
3257 cur_dsd = &crc_ctx_pkt->u.bundling.dif_dsd;
3258 if (qla24xx_walk_and_build_prot_sglist(ha, NULL, cur_dsd,
3259 prm->prot_seg_cnt, cmd))
3260 goto crc_queuing_error;
3265 /* Cleanup will be performed by the caller */
3266 qpair->req->outstanding_cmds[h] = NULL;
3268 return QLA_FUNCTION_FAILED;
3272 * Callback to setup response of xmit_type of QLA_TGT_XMIT_DATA and *
3273 * QLA_TGT_XMIT_STATUS for >= 24xx silicon
3275 int qlt_xmit_response(struct qla_tgt_cmd *cmd, int xmit_type,
3276 uint8_t scsi_status)
3278 struct scsi_qla_host *vha = cmd->vha;
3279 struct qla_qpair *qpair = cmd->qpair;
3280 struct ctio7_to_24xx *pkt;
3281 struct qla_tgt_prm prm;
3282 uint32_t full_req_cnt = 0;
3283 unsigned long flags = 0;
3286 if (!qpair->fw_started || (cmd->reset_count != qpair->chip_reset) ||
3287 (cmd->sess && cmd->sess->deleted)) {
3288 cmd->state = QLA_TGT_STATE_PROCESSED;
3292 ql_dbg_qp(ql_dbg_tgt, qpair, 0xe018,
3293 "is_send_status=%d, cmd->bufflen=%d, cmd->sg_cnt=%d, cmd->dma_data_direction=%d se_cmd[%p] qp %d\n",
3294 (xmit_type & QLA_TGT_XMIT_STATUS) ?
3295 1 : 0, cmd->bufflen, cmd->sg_cnt, cmd->dma_data_direction,
3296 &cmd->se_cmd, qpair->id);
3298 res = qlt_pre_xmit_response(cmd, &prm, xmit_type, scsi_status,
3300 if (unlikely(res != 0)) {
3304 spin_lock_irqsave(qpair->qp_lock_ptr, flags);
3306 if (xmit_type == QLA_TGT_XMIT_STATUS)
3307 qpair->tgt_counters.core_qla_snd_status++;
3309 qpair->tgt_counters.core_qla_que_buf++;
3311 if (!qpair->fw_started || cmd->reset_count != qpair->chip_reset) {
3313 * Either the port is not online or this request was from
3314 * previous life, just abort the processing.
3316 cmd->state = QLA_TGT_STATE_PROCESSED;
3317 ql_dbg_qp(ql_dbg_async, qpair, 0xe101,
3318 "RESET-RSP online/active/old-count/new-count = %d/%d/%d/%d.\n",
3319 vha->flags.online, qla2x00_reset_active(vha),
3320 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);
3841 qlt_unmap_sg(vha, cmd);
3843 spin_unlock_irqrestore(&cmd->cmd_lock, flags);
3845 * It's normal to see 2 calls in this path:
3846 * 1) XFER Rdy completion + CMD_T_ABORT
3847 * 2) TCM TMR - drain_state_list
3849 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf016,
3850 "multiple abort. %p transport_state %x, t_state %x, "
3851 "se_cmd_flags %x\n", cmd, cmd->se_cmd.transport_state,
3852 cmd->se_cmd.t_state, cmd->se_cmd.se_cmd_flags);
3856 cmd->trc_flags |= TRC_ABORT;
3857 spin_unlock_irqrestore(&cmd->cmd_lock, flags);
3859 qlt_send_term_exchange(cmd->qpair, cmd, &cmd->atio, 0, 1);
3862 EXPORT_SYMBOL(qlt_abort_cmd);
3864 void qlt_free_cmd(struct qla_tgt_cmd *cmd)
3866 struct fc_port *sess = cmd->sess;
3868 ql_dbg(ql_dbg_tgt, cmd->vha, 0xe074,
3869 "%s: se_cmd[%p] ox_id %04x\n",
3870 __func__, &cmd->se_cmd,
3871 be16_to_cpu(cmd->atio.u.isp24.fcp_hdr.ox_id));
3873 BUG_ON(cmd->cmd_in_wq);
3876 qlt_decr_num_pend_cmds(cmd->vha);
3878 BUG_ON(cmd->sg_mapped);
3879 cmd->jiffies_at_free = get_jiffies_64();
3880 if (unlikely(cmd->free_sg))
3883 if (!sess || !sess->se_sess) {
3887 cmd->jiffies_at_free = get_jiffies_64();
3888 cmd->vha->hw->tgt.tgt_ops->rel_cmd(cmd);
3890 EXPORT_SYMBOL(qlt_free_cmd);
3893 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
3895 static int qlt_term_ctio_exchange(struct qla_qpair *qpair, void *ctio,
3896 struct qla_tgt_cmd *cmd, uint32_t status)
3899 struct scsi_qla_host *vha = qpair->vha;
3901 if (cmd->se_cmd.prot_op)
3902 ql_dbg(ql_dbg_tgt_dif, vha, 0xe013,
3903 "Term DIF cmd: lba[0x%llx|%lld] len[0x%x] "
3904 "se_cmd=%p tag[%x] op %#x/%s",
3906 cmd->num_blks, &cmd->se_cmd,
3907 cmd->atio.u.isp24.exchange_addr,
3908 cmd->se_cmd.prot_op,
3909 prot_op_str(cmd->se_cmd.prot_op));
3912 struct ctio7_from_24xx *c = (struct ctio7_from_24xx *)ctio;
3915 cpu_to_le16(OF_TERM_EXCH));
3920 qlt_send_term_exchange(qpair, cmd, &cmd->atio, 1, 0);
3926 /* ha->hardware_lock supposed to be held on entry */
3927 static void *qlt_ctio_to_cmd(struct scsi_qla_host *vha,
3928 struct rsp_que *rsp, uint32_t handle, void *ctio)
3931 struct req_que *req;
3932 int qid = GET_QID(handle);
3933 uint32_t h = handle & ~QLA_TGT_HANDLE_MASK;
3935 if (unlikely(h == QLA_TGT_SKIP_HANDLE))
3938 if (qid == rsp->req->id) {
3940 } else if (vha->hw->req_q_map[qid]) {
3941 ql_dbg(ql_dbg_tgt_mgt, vha, 0x1000a,
3942 "qla_target(%d): CTIO completion with different QID %d handle %x\n",
3943 vha->vp_idx, rsp->id, handle);
3944 req = vha->hw->req_q_map[qid];
3949 h &= QLA_CMD_HANDLE_MASK;
3951 if (h != QLA_TGT_NULL_HANDLE) {
3952 if (unlikely(h >= req->num_outstanding_cmds)) {
3953 ql_dbg(ql_dbg_tgt, vha, 0xe052,
3954 "qla_target(%d): Wrong handle %x received\n",
3955 vha->vp_idx, handle);
3959 cmd = req->outstanding_cmds[h];
3960 if (unlikely(cmd == NULL)) {
3961 ql_dbg(ql_dbg_async, vha, 0xe053,
3962 "qla_target(%d): Suspicious: unable to find the command with handle %x req->id %d rsp->id %d\n",
3963 vha->vp_idx, handle, req->id, rsp->id);
3966 req->outstanding_cmds[h] = NULL;
3967 } else if (ctio != NULL) {
3968 /* We can't get loop ID from CTIO7 */
3969 ql_dbg(ql_dbg_tgt, vha, 0xe054,
3970 "qla_target(%d): Wrong CTIO received: QLA24xx doesn't "
3971 "support NULL handles\n", vha->vp_idx);
3979 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
3981 static void qlt_do_ctio_completion(struct scsi_qla_host *vha,
3982 struct rsp_que *rsp, uint32_t handle, uint32_t status, void *ctio)
3984 struct qla_hw_data *ha = vha->hw;
3985 struct se_cmd *se_cmd;
3986 struct qla_tgt_cmd *cmd;
3987 struct qla_qpair *qpair = rsp->qpair;
3989 if (handle & CTIO_INTERMEDIATE_HANDLE_MARK) {
3990 /* That could happen only in case of an error/reset/abort */
3991 if (status != CTIO_SUCCESS) {
3992 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01d,
3993 "Intermediate CTIO received"
3994 " (status %x)\n", status);
3999 cmd = qlt_ctio_to_cmd(vha, rsp, handle, ctio);
4003 if ((le16_to_cpu(((struct ctio7_from_24xx *)ctio)->flags) & CTIO7_FLAGS_DATA_OUT) &&
4005 qlt_chk_edif_rx_sa_delete_pending(vha, cmd->sess,
4006 (struct ctio7_from_24xx *)ctio);
4009 se_cmd = &cmd->se_cmd;
4010 cmd->cmd_sent_to_fw = 0;
4012 qlt_unmap_sg(vha, cmd);
4014 if (unlikely(status != CTIO_SUCCESS)) {
4015 switch (status & 0xFFFF) {
4016 case CTIO_INVALID_RX_ID:
4017 if (printk_ratelimit())
4018 dev_info(&vha->hw->pdev->dev,
4019 "qla_target(%d): CTIO with INVALID_RX_ID ATIO attr %x CTIO Flags %x|%x\n",
4020 vha->vp_idx, cmd->atio.u.isp24.attr,
4021 ((cmd->ctio_flags >> 9) & 0xf),
4025 case CTIO_LIP_RESET:
4026 case CTIO_TARGET_RESET:
4028 /* driver request abort via Terminate exchange */
4031 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf058,
4032 "qla_target(%d): CTIO with "
4033 "status %#x received, state %x, se_cmd %p, "
4034 "(LIP_RESET=e, ABORTED=2, TARGET_RESET=17, "
4035 "TIMEOUT=b, INVALID_RX_ID=8)\n", vha->vp_idx,
4036 status, cmd->state, se_cmd);
4039 case CTIO_PORT_LOGGED_OUT:
4040 case CTIO_PORT_UNAVAILABLE:
4043 (status & 0xFFFF) == CTIO_PORT_LOGGED_OUT;
4045 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf059,
4046 "qla_target(%d): CTIO with %s status %x "
4047 "received (state %x, se_cmd %p)\n", vha->vp_idx,
4048 logged_out ? "PORT LOGGED OUT" : "PORT UNAVAILABLE",
4049 status, cmd->state, se_cmd);
4051 if (logged_out && cmd->sess) {
4053 * Session is already logged out, but we need
4054 * to notify initiator, who's not aware of this
4056 cmd->sess->send_els_logo = 1;
4057 ql_dbg(ql_dbg_disc, vha, 0x20f8,
4058 "%s %d %8phC post del sess\n",
4059 __func__, __LINE__, cmd->sess->port_name);
4061 qlt_schedule_sess_for_deletion(cmd->sess);
4065 case CTIO_DIF_ERROR: {
4066 struct ctio_crc_from_fw *crc =
4067 (struct ctio_crc_from_fw *)ctio;
4068 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf073,
4069 "qla_target(%d): CTIO with DIF_ERROR status %x "
4070 "received (state %x, ulp_cmd %p) actual_dif[0x%llx] "
4071 "expect_dif[0x%llx]\n",
4072 vha->vp_idx, status, cmd->state, se_cmd,
4073 *((u64 *)&crc->actual_dif[0]),
4074 *((u64 *)&crc->expected_dif[0]));
4076 qlt_handle_dif_error(qpair, cmd, ctio);
4080 case CTIO_FAST_AUTH_ERR:
4081 case CTIO_FAST_INCOMP_PAD_LEN:
4082 case CTIO_FAST_INVALID_REQ:
4083 case CTIO_FAST_SPI_ERR:
4084 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05b,
4085 "qla_target(%d): CTIO with EDIF error status 0x%x received (state %x, se_cmd %p\n",
4086 vha->vp_idx, status, cmd->state, se_cmd);
4090 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05b,
4091 "qla_target(%d): CTIO with error status 0x%x received (state %x, se_cmd %p\n",
4092 vha->vp_idx, status, cmd->state, se_cmd);
4097 /* "cmd->aborted" means
4098 * cmd is already aborted/terminated, we don't
4099 * need to terminate again. The exchange is already
4100 * cleaned up/freed at FW level. Just cleanup at driver
4103 if ((cmd->state != QLA_TGT_STATE_NEED_DATA) &&
4105 cmd->trc_flags |= TRC_CTIO_ERR;
4106 if (qlt_term_ctio_exchange(qpair, ctio, cmd, status))
4111 if (cmd->state == QLA_TGT_STATE_PROCESSED) {
4112 cmd->trc_flags |= TRC_CTIO_DONE;
4113 } else if (cmd->state == QLA_TGT_STATE_NEED_DATA) {
4114 cmd->state = QLA_TGT_STATE_DATA_IN;
4116 if (status == CTIO_SUCCESS)
4117 cmd->write_data_transferred = 1;
4119 ha->tgt.tgt_ops->handle_data(cmd);
4121 } else if (cmd->aborted) {
4122 cmd->trc_flags |= TRC_CTIO_ABORTED;
4123 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01e,
4124 "Aborted command %p (tag %lld) finished\n", cmd, se_cmd->tag);
4126 cmd->trc_flags |= TRC_CTIO_STRANGE;
4127 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05c,
4128 "qla_target(%d): A command in state (%d) should "
4129 "not return a CTIO complete\n", vha->vp_idx, cmd->state);
4132 if (unlikely(status != CTIO_SUCCESS) &&
4134 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01f, "Finishing failed CTIO\n");
4138 ha->tgt.tgt_ops->free_cmd(cmd);
4141 static inline int qlt_get_fcp_task_attr(struct scsi_qla_host *vha,
4146 switch (task_codes) {
4147 case ATIO_SIMPLE_QUEUE:
4148 fcp_task_attr = TCM_SIMPLE_TAG;
4150 case ATIO_HEAD_OF_QUEUE:
4151 fcp_task_attr = TCM_HEAD_TAG;
4153 case ATIO_ORDERED_QUEUE:
4154 fcp_task_attr = TCM_ORDERED_TAG;
4156 case ATIO_ACA_QUEUE:
4157 fcp_task_attr = TCM_ACA_TAG;
4160 fcp_task_attr = TCM_SIMPLE_TAG;
4163 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05d,
4164 "qla_target: unknown task code %x, use ORDERED instead\n",
4166 fcp_task_attr = TCM_ORDERED_TAG;
4170 return fcp_task_attr;
4174 * Process context for I/O path into tcm_qla2xxx code
4176 static void __qlt_do_work(struct qla_tgt_cmd *cmd)
4178 scsi_qla_host_t *vha = cmd->vha;
4179 struct qla_hw_data *ha = vha->hw;
4180 struct fc_port *sess = cmd->sess;
4181 struct atio_from_isp *atio = &cmd->atio;
4183 unsigned long flags;
4184 uint32_t data_length;
4185 int ret, fcp_task_attr, data_dir, bidi = 0;
4186 struct qla_qpair *qpair = cmd->qpair;
4189 cmd->trc_flags |= TRC_DO_WORK;
4192 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf082,
4193 "cmd with tag %u is aborted\n",
4194 cmd->atio.u.isp24.exchange_addr);
4198 spin_lock_init(&cmd->cmd_lock);
4199 cdb = &atio->u.isp24.fcp_cmnd.cdb[0];
4200 cmd->se_cmd.tag = le32_to_cpu(atio->u.isp24.exchange_addr);
4202 if (atio->u.isp24.fcp_cmnd.rddata &&
4203 atio->u.isp24.fcp_cmnd.wrdata) {
4205 data_dir = DMA_TO_DEVICE;
4206 } else if (atio->u.isp24.fcp_cmnd.rddata)
4207 data_dir = DMA_FROM_DEVICE;
4208 else if (atio->u.isp24.fcp_cmnd.wrdata)
4209 data_dir = DMA_TO_DEVICE;
4211 data_dir = DMA_NONE;
4213 fcp_task_attr = qlt_get_fcp_task_attr(vha,
4214 atio->u.isp24.fcp_cmnd.task_attr);
4215 data_length = get_datalen_for_atio(atio);
4217 ret = ha->tgt.tgt_ops->handle_cmd(vha, cmd, cdb, data_length,
4218 fcp_task_attr, data_dir, bidi);
4222 * Drop extra session reference from qlt_handle_cmd_for_atio().
4224 ha->tgt.tgt_ops->put_sess(sess);
4228 ql_dbg(ql_dbg_io, vha, 0x3060, "Terminating work cmd %p", cmd);
4230 * cmd has not sent to target yet, so pass NULL as the second
4231 * argument to qlt_send_term_exchange() and free the memory here.
4233 cmd->trc_flags |= TRC_DO_WORK_ERR;
4234 spin_lock_irqsave(qpair->qp_lock_ptr, flags);
4235 qlt_send_term_exchange(qpair, NULL, &cmd->atio, 1, 0);
4237 qlt_decr_num_pend_cmds(vha);
4238 cmd->vha->hw->tgt.tgt_ops->rel_cmd(cmd);
4239 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
4241 ha->tgt.tgt_ops->put_sess(sess);
4244 static void qlt_do_work(struct work_struct *work)
4246 struct qla_tgt_cmd *cmd = container_of(work, struct qla_tgt_cmd, work);
4247 scsi_qla_host_t *vha = cmd->vha;
4248 unsigned long flags;
4250 spin_lock_irqsave(&vha->cmd_list_lock, flags);
4251 list_del(&cmd->cmd_list);
4252 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
4257 void qlt_clr_qp_table(struct scsi_qla_host *vha)
4259 unsigned long flags;
4260 struct qla_hw_data *ha = vha->hw;
4261 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
4265 ql_log(ql_log_info, vha, 0x706c,
4266 "User update Number of Active Qpairs %d\n",
4267 ha->tgt.num_act_qpairs);
4269 spin_lock_irqsave(&ha->tgt.atio_lock, flags);
4271 btree_for_each_safe64(&tgt->lun_qpair_map, key, node)
4272 btree_remove64(&tgt->lun_qpair_map, key);
4274 ha->base_qpair->lun_cnt = 0;
4275 for (key = 0; key < ha->max_qpairs; key++)
4276 if (ha->queue_pair_map[key])
4277 ha->queue_pair_map[key]->lun_cnt = 0;
4279 spin_unlock_irqrestore(&ha->tgt.atio_lock, flags);
4282 static void qlt_assign_qpair(struct scsi_qla_host *vha,
4283 struct qla_tgt_cmd *cmd)
4285 struct qla_qpair *qpair, *qp;
4286 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
4287 struct qla_qpair_hint *h;
4289 if (vha->flags.qpairs_available) {
4290 h = btree_lookup64(&tgt->lun_qpair_map, cmd->unpacked_lun);
4292 /* spread lun to qpair ratio evently */
4294 struct scsi_qla_host *base_vha =
4295 pci_get_drvdata(vha->hw->pdev);
4297 qpair = vha->hw->base_qpair;
4298 if (qpair->lun_cnt == 0) {
4300 h = qla_qpair_to_hint(tgt, qpair);
4302 rc = btree_insert64(&tgt->lun_qpair_map,
4303 cmd->unpacked_lun, h, GFP_ATOMIC);
4306 ql_log(ql_log_info, vha, 0xd037,
4307 "Unable to insert lun %llx into lun_qpair_map\n",
4312 lcnt = qpair->lun_cnt;
4316 list_for_each_entry(qp, &base_vha->qp_list,
4318 if (qp->lun_cnt == 0) {
4320 h = qla_qpair_to_hint(tgt, qp);
4322 rc = btree_insert64(&tgt->lun_qpair_map,
4323 cmd->unpacked_lun, h, GFP_ATOMIC);
4326 ql_log(ql_log_info, vha, 0xd038,
4327 "Unable to insert lun %llx into lun_qpair_map\n",
4333 if (qp->lun_cnt < lcnt) {
4342 h = qla_qpair_to_hint(tgt, qpair);
4344 rc = btree_insert64(&tgt->lun_qpair_map,
4345 cmd->unpacked_lun, h, GFP_ATOMIC);
4348 ql_log(ql_log_info, vha, 0xd039,
4349 "Unable to insert lun %llx into lun_qpair_map\n",
4354 h = &tgt->qphints[0];
4357 cmd->qpair = h->qpair;
4358 cmd->se_cmd.cpuid = h->cpuid;
4361 static struct qla_tgt_cmd *qlt_get_tag(scsi_qla_host_t *vha,
4362 struct fc_port *sess,
4363 struct atio_from_isp *atio)
4365 struct qla_tgt_cmd *cmd;
4367 cmd = vha->hw->tgt.tgt_ops->get_cmd(sess);
4371 cmd->cmd_type = TYPE_TGT_CMD;
4372 memcpy(&cmd->atio, atio, sizeof(*atio));
4373 INIT_LIST_HEAD(&cmd->sess_cmd_list);
4374 cmd->state = QLA_TGT_STATE_NEW;
4375 cmd->tgt = vha->vha_tgt.qla_tgt;
4376 qlt_incr_num_pend_cmds(vha);
4379 cmd->loop_id = sess->loop_id;
4380 cmd->conf_compl_supported = sess->conf_compl_supported;
4383 cmd->jiffies_at_alloc = get_jiffies_64();
4385 cmd->unpacked_lun = scsilun_to_int(
4386 (struct scsi_lun *)&atio->u.isp24.fcp_cmnd.lun);
4387 qlt_assign_qpair(vha, cmd);
4388 cmd->reset_count = vha->hw->base_qpair->chip_reset;
4389 cmd->vp_idx = vha->vp_idx;
4390 cmd->edif = sess->edif.enable;
4395 /* ha->hardware_lock supposed to be held on entry */
4396 static int qlt_handle_cmd_for_atio(struct scsi_qla_host *vha,
4397 struct atio_from_isp *atio)
4399 struct qla_hw_data *ha = vha->hw;
4400 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
4401 struct fc_port *sess;
4402 struct qla_tgt_cmd *cmd;
4403 unsigned long flags;
4406 if (unlikely(tgt->tgt_stop)) {
4407 ql_dbg(ql_dbg_io, vha, 0x3061,
4408 "New command while device %p is shutting down\n", tgt);
4412 id = be_to_port_id(atio->u.isp24.fcp_hdr.s_id);
4413 if (IS_SW_RESV_ADDR(id))
4416 sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha, atio->u.isp24.fcp_hdr.s_id);
4417 if (unlikely(!sess))
4420 /* Another WWN used to have our s_id. Our PLOGI scheduled its
4421 * session deletion, but it's still in sess_del_work wq */
4422 if (sess->deleted) {
4423 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf002,
4424 "New command while old session %p is being deleted\n",
4430 * Do kref_get() before returning + dropping qla_hw_data->hardware_lock.
4432 if (!kref_get_unless_zero(&sess->sess_kref)) {
4433 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf004,
4434 "%s: kref_get fail, %8phC oxid %x \n",
4435 __func__, sess->port_name,
4436 be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id));
4440 cmd = qlt_get_tag(vha, sess, atio);
4442 ql_dbg(ql_dbg_io, vha, 0x3062,
4443 "qla_target(%d): Allocation of cmd failed\n", vha->vp_idx);
4444 ha->tgt.tgt_ops->put_sess(sess);
4449 cmd->trc_flags |= TRC_NEW_CMD;
4451 spin_lock_irqsave(&vha->cmd_list_lock, flags);
4452 list_add_tail(&cmd->cmd_list, &vha->qla_cmd_list);
4453 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
4455 INIT_WORK(&cmd->work, qlt_do_work);
4456 if (vha->flags.qpairs_available) {
4457 queue_work_on(cmd->se_cmd.cpuid, qla_tgt_wq, &cmd->work);
4458 } else if (ha->msix_count) {
4459 if (cmd->atio.u.isp24.fcp_cmnd.rddata)
4460 queue_work_on(smp_processor_id(), qla_tgt_wq,
4463 queue_work_on(cmd->se_cmd.cpuid, qla_tgt_wq,
4466 queue_work(qla_tgt_wq, &cmd->work);
4472 /* ha->hardware_lock supposed to be held on entry */
4473 static int qlt_issue_task_mgmt(struct fc_port *sess, u64 lun,
4474 int fn, void *iocb, int flags)
4476 struct scsi_qla_host *vha = sess->vha;
4477 struct qla_hw_data *ha = vha->hw;
4478 struct qla_tgt_mgmt_cmd *mcmd;
4479 struct atio_from_isp *a = (struct atio_from_isp *)iocb;
4480 struct qla_qpair_hint *h = &vha->vha_tgt.qla_tgt->qphints[0];
4482 mcmd = mempool_alloc(qla_tgt_mgmt_cmd_mempool, GFP_ATOMIC);
4484 ql_dbg(ql_dbg_tgt_tmr, vha, 0x10009,
4485 "qla_target(%d): Allocation of management "
4486 "command failed, some commands and their data could "
4487 "leak\n", vha->vp_idx);
4490 memset(mcmd, 0, sizeof(*mcmd));
4494 memcpy(&mcmd->orig_iocb.imm_ntfy, iocb,
4495 sizeof(mcmd->orig_iocb.imm_ntfy));
4497 mcmd->tmr_func = fn;
4498 mcmd->flags = flags;
4499 mcmd->reset_count = ha->base_qpair->chip_reset;
4500 mcmd->qpair = h->qpair;
4502 mcmd->se_cmd.cpuid = h->cpuid;
4503 mcmd->unpacked_lun = lun;
4506 case QLA_TGT_LUN_RESET:
4507 case QLA_TGT_CLEAR_TS:
4508 case QLA_TGT_ABORT_TS:
4509 abort_cmds_for_lun(vha, lun, a->u.isp24.fcp_hdr.s_id);
4511 case QLA_TGT_CLEAR_ACA:
4512 h = qlt_find_qphint(vha, mcmd->unpacked_lun);
4513 mcmd->qpair = h->qpair;
4514 mcmd->se_cmd.cpuid = h->cpuid;
4517 case QLA_TGT_TARGET_RESET:
4518 case QLA_TGT_NEXUS_LOSS_SESS:
4519 case QLA_TGT_NEXUS_LOSS:
4520 case QLA_TGT_ABORT_ALL:
4526 INIT_WORK(&mcmd->work, qlt_do_tmr_work);
4527 queue_work_on(mcmd->se_cmd.cpuid, qla_tgt_wq,
4533 /* ha->hardware_lock supposed to be held on entry */
4534 static int qlt_handle_task_mgmt(struct scsi_qla_host *vha, void *iocb)
4536 struct atio_from_isp *a = (struct atio_from_isp *)iocb;
4537 struct qla_hw_data *ha = vha->hw;
4538 struct fc_port *sess;
4541 unsigned long flags;
4543 fn = a->u.isp24.fcp_cmnd.task_mgmt_flags;
4545 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
4546 sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha,
4547 a->u.isp24.fcp_hdr.s_id);
4548 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
4551 scsilun_to_int((struct scsi_lun *)&a->u.isp24.fcp_cmnd.lun);
4553 if (sess == NULL || sess->deleted)
4556 return qlt_issue_task_mgmt(sess, unpacked_lun, fn, iocb, 0);
4559 /* ha->hardware_lock supposed to be held on entry */
4560 static int __qlt_abort_task(struct scsi_qla_host *vha,
4561 struct imm_ntfy_from_isp *iocb, struct fc_port *sess)
4563 struct atio_from_isp *a = (struct atio_from_isp *)iocb;
4564 struct qla_hw_data *ha = vha->hw;
4565 struct qla_tgt_mgmt_cmd *mcmd;
4569 mcmd = mempool_alloc(qla_tgt_mgmt_cmd_mempool, GFP_ATOMIC);
4571 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05f,
4572 "qla_target(%d): %s: Allocation of ABORT cmd failed\n",
4573 vha->vp_idx, __func__);
4576 memset(mcmd, 0, sizeof(*mcmd));
4579 memcpy(&mcmd->orig_iocb.imm_ntfy, iocb,
4580 sizeof(mcmd->orig_iocb.imm_ntfy));
4583 scsilun_to_int((struct scsi_lun *)&a->u.isp24.fcp_cmnd.lun);
4584 mcmd->reset_count = ha->base_qpair->chip_reset;
4585 mcmd->tmr_func = QLA_TGT_2G_ABORT_TASK;
4586 mcmd->qpair = ha->base_qpair;
4588 rc = ha->tgt.tgt_ops->handle_tmr(mcmd, unpacked_lun, mcmd->tmr_func,
4589 le16_to_cpu(iocb->u.isp2x.seq_id));
4591 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf060,
4592 "qla_target(%d): tgt_ops->handle_tmr() failed: %d\n",
4594 mempool_free(mcmd, qla_tgt_mgmt_cmd_mempool);
4601 /* ha->hardware_lock supposed to be held on entry */
4602 static int qlt_abort_task(struct scsi_qla_host *vha,
4603 struct imm_ntfy_from_isp *iocb)
4605 struct qla_hw_data *ha = vha->hw;
4606 struct fc_port *sess;
4608 unsigned long flags;
4610 loop_id = GET_TARGET_ID(ha, (struct atio_from_isp *)iocb);
4612 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
4613 sess = ha->tgt.tgt_ops->find_sess_by_loop_id(vha, loop_id);
4614 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
4617 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf025,
4618 "qla_target(%d): task abort for unexisting "
4619 "session\n", vha->vp_idx);
4620 return qlt_sched_sess_work(vha->vha_tgt.qla_tgt,
4621 QLA_TGT_SESS_WORK_ABORT, iocb, sizeof(*iocb));
4624 return __qlt_abort_task(vha, iocb, sess);
4627 void qlt_logo_completion_handler(fc_port_t *fcport, int rc)
4629 if (rc != MBS_COMMAND_COMPLETE) {
4630 ql_dbg(ql_dbg_tgt_mgt, fcport->vha, 0xf093,
4631 "%s: se_sess %p / sess %p from"
4632 " port %8phC loop_id %#04x s_id %02x:%02x:%02x"
4633 " LOGO failed: %#x\n",
4637 fcport->port_name, fcport->loop_id,
4638 fcport->d_id.b.domain, fcport->d_id.b.area,
4639 fcport->d_id.b.al_pa, rc);
4642 fcport->logout_completed = 1;
4646 * ha->hardware_lock supposed to be held on entry (to protect tgt->sess_list)
4648 * Schedules sessions with matching port_id/loop_id but different wwn for
4649 * deletion. Returns existing session with matching wwn if present.
4653 qlt_find_sess_invalidate_other(scsi_qla_host_t *vha, uint64_t wwn,
4654 port_id_t port_id, uint16_t loop_id, struct fc_port **conflict_sess)
4656 struct fc_port *sess = NULL, *other_sess;
4659 *conflict_sess = NULL;
4661 list_for_each_entry(other_sess, &vha->vp_fcports, list) {
4663 other_wwn = wwn_to_u64(other_sess->port_name);
4665 if (wwn == other_wwn) {
4671 /* find other sess with nport_id collision */
4672 if (port_id.b24 == other_sess->d_id.b24) {
4673 if (loop_id != other_sess->loop_id) {
4674 ql_dbg(ql_dbg_disc, vha, 0x1000c,
4675 "Invalidating sess %p loop_id %d wwn %llx.\n",
4676 other_sess, other_sess->loop_id, other_wwn);
4679 * logout_on_delete is set by default, but another
4680 * session that has the same s_id/loop_id combo
4681 * might have cleared it when requested this session
4682 * deletion, so don't touch it
4684 qlt_schedule_sess_for_deletion(other_sess);
4687 * Another wwn used to have our s_id/loop_id
4688 * kill the session, but don't free the loop_id
4690 ql_dbg(ql_dbg_disc, vha, 0xf01b,
4691 "Invalidating sess %p loop_id %d wwn %llx.\n",
4692 other_sess, other_sess->loop_id, other_wwn);
4694 other_sess->keep_nport_handle = 1;
4695 if (other_sess->disc_state != DSC_DELETED)
4696 *conflict_sess = other_sess;
4697 qlt_schedule_sess_for_deletion(other_sess);
4702 /* find other sess with nport handle collision */
4703 if ((loop_id == other_sess->loop_id) &&
4704 (loop_id != FC_NO_LOOP_ID)) {
4705 ql_dbg(ql_dbg_disc, vha, 0x1000d,
4706 "Invalidating sess %p loop_id %d wwn %llx.\n",
4707 other_sess, other_sess->loop_id, other_wwn);
4709 /* Same loop_id but different s_id
4710 * Ok to kill and logout */
4711 qlt_schedule_sess_for_deletion(other_sess);
4718 /* Abort any commands for this s_id waiting on qla_tgt_wq workqueue */
4719 static int abort_cmds_for_s_id(struct scsi_qla_host *vha, port_id_t *s_id)
4721 struct qla_tgt_sess_op *op;
4722 struct qla_tgt_cmd *cmd;
4725 unsigned long flags;
4727 key = (((u32)s_id->b.domain << 16) |
4728 ((u32)s_id->b.area << 8) |
4729 ((u32)s_id->b.al_pa));
4731 spin_lock_irqsave(&vha->cmd_list_lock, flags);
4732 list_for_each_entry(op, &vha->qla_sess_op_cmd_list, cmd_list) {
4733 uint32_t op_key = sid_to_key(op->atio.u.isp24.fcp_hdr.s_id);
4735 if (op_key == key) {
4741 list_for_each_entry(op, &vha->unknown_atio_list, cmd_list) {
4742 uint32_t op_key = sid_to_key(op->atio.u.isp24.fcp_hdr.s_id);
4744 if (op_key == key) {
4750 list_for_each_entry(cmd, &vha->qla_cmd_list, cmd_list) {
4751 uint32_t cmd_key = sid_to_key(cmd->atio.u.isp24.fcp_hdr.s_id);
4753 if (cmd_key == key) {
4758 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
4763 static int qlt_handle_login(struct scsi_qla_host *vha,
4764 struct imm_ntfy_from_isp *iocb)
4766 struct fc_port *sess = NULL, *conflict_sess = NULL;
4769 uint16_t loop_id, wd3_lo;
4771 struct qlt_plogi_ack_t *pla;
4772 unsigned long flags;
4774 lockdep_assert_held(&vha->hw->hardware_lock);
4776 wwn = wwn_to_u64(iocb->u.isp24.port_name);
4778 port_id.b.domain = iocb->u.isp24.port_id[2];
4779 port_id.b.area = iocb->u.isp24.port_id[1];
4780 port_id.b.al_pa = iocb->u.isp24.port_id[0];
4781 port_id.b.rsvd_1 = 0;
4783 loop_id = le16_to_cpu(iocb->u.isp24.nport_handle);
4785 /* Mark all stale commands sitting in qla_tgt_wq for deletion */
4786 abort_cmds_for_s_id(vha, &port_id);
4789 spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
4790 sess = qlt_find_sess_invalidate_other(vha, wwn,
4791 port_id, loop_id, &conflict_sess);
4792 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
4794 ql_dbg(ql_dbg_disc, vha, 0xffff,
4795 "%s %d Term INOT due to WWN=0 lid=%d, NportID %06X ",
4796 __func__, __LINE__, loop_id, port_id.b24);
4797 qlt_send_term_imm_notif(vha, iocb, 1);
4801 if (IS_SW_RESV_ADDR(port_id)) {
4806 if (vha->hw->flags.edif_enabled &&
4807 !(vha->e_dbell.db_flags & EDB_ACTIVE) &&
4808 iocb->u.isp24.status_subcode == ELS_PLOGI &&
4809 !(le16_to_cpu(iocb->u.isp24.flags) & NOTIFY24XX_FLAGS_FCSP)) {
4810 ql_dbg(ql_dbg_disc, vha, 0xffff,
4811 "%s %d Term INOT due to app not available lid=%d, NportID %06X ",
4812 __func__, __LINE__, loop_id, port_id.b24);
4813 qlt_send_term_imm_notif(vha, iocb, 1);
4817 if (vha->hw->flags.edif_enabled) {
4818 if (!(vha->e_dbell.db_flags & EDB_ACTIVE)) {
4819 ql_dbg(ql_dbg_disc, vha, 0xffff,
4820 "%s %d Term INOT due to app not started lid=%d, NportID %06X ",
4821 __func__, __LINE__, loop_id, port_id.b24);
4822 qlt_send_term_imm_notif(vha, iocb, 1);
4824 } else if (iocb->u.isp24.status_subcode == ELS_PLOGI &&
4825 !(le16_to_cpu(iocb->u.isp24.flags) & NOTIFY24XX_FLAGS_FCSP)) {
4826 ql_dbg(ql_dbg_disc, vha, 0xffff,
4827 "%s %d Term INOT due to unsecure lid=%d, NportID %06X ",
4828 __func__, __LINE__, loop_id, port_id.b24);
4829 qlt_send_term_imm_notif(vha, iocb, 1);
4834 pla = qlt_plogi_ack_find_add(vha, &port_id, iocb);
4836 ql_dbg(ql_dbg_disc + ql_dbg_verbose, vha, 0xffff,
4837 "%s %d %8phC Term INOT due to mem alloc fail",
4839 iocb->u.isp24.port_name);
4840 qlt_send_term_imm_notif(vha, iocb, 1);
4844 if (conflict_sess) {
4845 conflict_sess->login_gen++;
4846 qlt_plogi_ack_link(vha, pla, conflict_sess,
4847 QLT_PLOGI_LINK_CONFLICT);
4852 ql_dbg(ql_dbg_disc, vha, 0xffff,
4853 "%s %d %8phC post new sess\n",
4854 __func__, __LINE__, iocb->u.isp24.port_name);
4855 if (iocb->u.isp24.status_subcode == ELS_PLOGI)
4856 qla24xx_post_newsess_work(vha, &port_id,
4857 iocb->u.isp24.port_name,
4858 iocb->u.isp24.u.plogi.node_name,
4861 qla24xx_post_newsess_work(vha, &port_id,
4862 iocb->u.isp24.port_name, NULL,
4868 if (sess->disc_state == DSC_UPD_FCPORT) {
4872 * Remote port registration is still going on from
4873 * previous login. Allow it to finish before we
4874 * accept the new login.
4876 sess->next_disc_state = DSC_DELETE_PEND;
4877 sec = jiffies_to_msecs(jiffies -
4878 sess->jiffies_at_registration) / 1000;
4879 if (sess->sec_since_registration < sec && sec &&
4881 sess->sec_since_registration = sec;
4882 ql_dbg(ql_dbg_disc, vha, 0xffff,
4883 "%s %8phC - Slow Rport registration (%d Sec)\n",
4884 __func__, sess->port_name, sec);
4887 if (!conflict_sess) {
4888 list_del(&pla->list);
4889 kmem_cache_free(qla_tgt_plogi_cachep, pla);
4892 qlt_send_term_imm_notif(vha, iocb, 1);
4896 qlt_plogi_ack_link(vha, pla, sess, QLT_PLOGI_LINK_SAME_WWN);
4897 sess->d_id = port_id;
4899 sess->loop_id = loop_id;
4901 if (iocb->u.isp24.status_subcode == ELS_PLOGI) {
4902 /* remote port has assigned Port ID */
4903 if (N2N_TOPO(vha->hw) && fcport_is_bigger(sess))
4904 vha->d_id = sess->d_id;
4906 ql_dbg(ql_dbg_disc, vha, 0xffff,
4907 "%s %8phC - send port online\n",
4908 __func__, sess->port_name);
4910 qla2x00_post_aen_work(vha, FCH_EVT_PORT_ONLINE,
4914 if (iocb->u.isp24.status_subcode == ELS_PRLI) {
4915 sess->fw_login_state = DSC_LS_PRLI_PEND;
4917 sess->loop_id = loop_id;
4918 sess->d_id = port_id;
4919 sess->fw_login_state = DSC_LS_PRLI_PEND;
4920 wd3_lo = le16_to_cpu(iocb->u.isp24.u.prli.wd3_lo);
4923 sess->conf_compl_supported = 1;
4925 if ((wd3_lo & BIT_4) == 0)
4926 sess->port_type = FCT_INITIATOR;
4928 sess->port_type = FCT_TARGET;
4931 sess->fw_login_state = DSC_LS_PLOGI_PEND;
4934 ql_dbg(ql_dbg_disc, vha, 0x20f9,
4935 "%s %d %8phC DS %d\n",
4936 __func__, __LINE__, sess->port_name, sess->disc_state);
4938 switch (sess->disc_state) {
4940 case DSC_LOGIN_PEND:
4941 qlt_plogi_ack_unref(vha, pla);
4946 * Under normal circumstances we want to release nport handle
4947 * during LOGO process to avoid nport handle leaks inside FW.
4948 * The exception is when LOGO is done while another PLOGI with
4949 * the same nport handle is waiting as might be the case here.
4950 * Note: there is always a possibily of a race where session
4951 * deletion has already started for other reasons (e.g. ACL
4952 * removal) and now PLOGI arrives:
4953 * 1. if PLOGI arrived in FW after nport handle has been freed,
4954 * FW must have assigned this PLOGI a new/same handle and we
4955 * can proceed ACK'ing it as usual when session deletion
4957 * 2. if PLOGI arrived in FW before LOGO with LCF_FREE_NPORT
4958 * bit reached it, the handle has now been released. We'll
4959 * get an error when we ACK this PLOGI. Nothing will be sent
4960 * back to initiator. Initiator should eventually retry
4961 * PLOGI and situation will correct itself.
4963 sess->keep_nport_handle = ((sess->loop_id == loop_id) &&
4964 (sess->d_id.b24 == port_id.b24));
4966 ql_dbg(ql_dbg_disc, vha, 0x20f9,
4967 "%s %d %8phC post del sess\n",
4968 __func__, __LINE__, sess->port_name);
4971 qlt_schedule_sess_for_deletion(sess);
4979 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
4981 static int qlt_24xx_handle_els(struct scsi_qla_host *vha,
4982 struct imm_ntfy_from_isp *iocb)
4984 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
4985 struct qla_hw_data *ha = vha->hw;
4986 struct fc_port *sess = NULL, *conflict_sess = NULL;
4992 unsigned long flags;
4994 lockdep_assert_held(&ha->hardware_lock);
4996 wwn = wwn_to_u64(iocb->u.isp24.port_name);
4998 port_id.b.domain = iocb->u.isp24.port_id[2];
4999 port_id.b.area = iocb->u.isp24.port_id[1];
5000 port_id.b.al_pa = iocb->u.isp24.port_id[0];
5001 port_id.b.rsvd_1 = 0;
5003 loop_id = le16_to_cpu(iocb->u.isp24.nport_handle);
5005 ql_dbg(ql_dbg_disc, vha, 0xf026,
5006 "qla_target(%d): Port ID: %02x:%02x:%02x ELS opcode: 0x%02x lid %d %8phC\n",
5007 vha->vp_idx, iocb->u.isp24.port_id[2],
5008 iocb->u.isp24.port_id[1], iocb->u.isp24.port_id[0],
5009 iocb->u.isp24.status_subcode, loop_id,
5010 iocb->u.isp24.port_name);
5012 /* res = 1 means ack at the end of thread
5013 * res = 0 means ack async/later.
5015 switch (iocb->u.isp24.status_subcode) {
5017 res = qlt_handle_login(vha, iocb);
5022 sess = qla2x00_find_fcport_by_wwpn(vha,
5023 iocb->u.isp24.port_name, 1);
5025 if (vha->hw->flags.edif_enabled && sess &&
5026 (!(sess->flags & FCF_FCSP_DEVICE) ||
5027 !sess->edif.authok)) {
5028 ql_dbg(ql_dbg_disc, vha, 0xffff,
5029 "%s %d %8phC Term PRLI due to unauthorize PRLI\n",
5030 __func__, __LINE__, iocb->u.isp24.port_name);
5031 qlt_send_term_imm_notif(vha, iocb, 1);
5035 if (sess && sess->plogi_link[QLT_PLOGI_LINK_SAME_WWN]) {
5036 ql_dbg(ql_dbg_disc, vha, 0xffff,
5037 "%s %d %8phC Term PRLI due to PLOGI ACK not completed\n",
5039 iocb->u.isp24.port_name);
5040 qlt_send_term_imm_notif(vha, iocb, 1);
5044 res = qlt_handle_login(vha, iocb);
5048 if (IS_SW_RESV_ADDR(port_id)) {
5053 wd3_lo = le16_to_cpu(iocb->u.isp24.u.prli.wd3_lo);
5056 spin_lock_irqsave(&tgt->ha->tgt.sess_lock, flags);
5057 sess = qlt_find_sess_invalidate_other(vha, wwn, port_id,
5058 loop_id, &conflict_sess);
5059 spin_unlock_irqrestore(&tgt->ha->tgt.sess_lock, flags);
5062 if (conflict_sess) {
5063 switch (conflict_sess->disc_state) {
5065 case DSC_DELETE_PEND:
5068 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf09b,
5069 "PRLI with conflicting sess %p port %8phC\n",
5070 conflict_sess, conflict_sess->port_name);
5071 conflict_sess->fw_login_state =
5072 DSC_LS_PORT_UNAVAIL;
5073 qlt_send_term_imm_notif(vha, iocb, 1);
5080 bool delete = false;
5083 if (vha->hw->flags.edif_enabled && sess &&
5084 (!(sess->flags & FCF_FCSP_DEVICE) ||
5085 !sess->edif.authok)) {
5086 ql_dbg(ql_dbg_disc, vha, 0xffff,
5087 "%s %d %8phC Term PRLI due to unauthorize prli\n",
5088 __func__, __LINE__, iocb->u.isp24.port_name);
5089 qlt_send_term_imm_notif(vha, iocb, 1);
5093 spin_lock_irqsave(&tgt->ha->tgt.sess_lock, flags);
5094 switch (sess->fw_login_state) {
5095 case DSC_LS_PLOGI_PEND:
5096 case DSC_LS_PLOGI_COMP:
5097 case DSC_LS_PRLI_COMP:
5104 switch (sess->disc_state) {
5105 case DSC_UPD_FCPORT:
5106 spin_unlock_irqrestore(&tgt->ha->tgt.sess_lock,
5109 sec = jiffies_to_msecs(jiffies -
5110 sess->jiffies_at_registration)/1000;
5111 if (sess->sec_since_registration < sec && sec &&
5113 sess->sec_since_registration = sec;
5114 ql_dbg(ql_dbg_disc, sess->vha, 0xffff,
5115 "%s %8phC : Slow Rport registration(%d Sec)\n",
5116 __func__, sess->port_name, sec);
5118 qlt_send_term_imm_notif(vha, iocb, 1);
5121 case DSC_LOGIN_PEND:
5123 case DSC_LOGIN_COMPLETE:
5132 spin_unlock_irqrestore(&tgt->ha->tgt.sess_lock,
5135 * Impatient initiator sent PRLI before last
5136 * PLOGI could finish. Will force him to re-try,
5137 * while last one finishes.
5139 ql_log(ql_log_warn, sess->vha, 0xf095,
5140 "sess %p PRLI received, before plogi ack.\n",
5142 qlt_send_term_imm_notif(vha, iocb, 1);
5148 * This shouldn't happen under normal circumstances,
5149 * since we have deleted the old session during PLOGI
5151 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf096,
5152 "PRLI (loop_id %#04x) for existing sess %p (loop_id %#04x)\n",
5153 sess->loop_id, sess, iocb->u.isp24.nport_handle);
5156 sess->loop_id = loop_id;
5157 sess->d_id = port_id;
5158 sess->fw_login_state = DSC_LS_PRLI_PEND;
5161 sess->conf_compl_supported = 1;
5163 if ((wd3_lo & BIT_4) == 0)
5164 sess->port_type = FCT_INITIATOR;
5166 sess->port_type = FCT_TARGET;
5168 spin_unlock_irqrestore(&tgt->ha->tgt.sess_lock, flags);
5170 res = 1; /* send notify ack */
5172 /* Make session global (not used in fabric mode) */
5173 if (ha->current_topology != ISP_CFG_F) {
5175 ql_dbg(ql_dbg_disc, vha, 0x20fa,
5176 "%s %d %8phC post nack\n",
5177 __func__, __LINE__, sess->port_name);
5178 qla24xx_post_nack_work(vha, sess, iocb,
5182 set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
5183 set_bit(LOCAL_LOOP_UPDATE, &vha->dpc_flags);
5184 qla2xxx_wake_dpc(vha);
5188 ql_dbg(ql_dbg_disc, vha, 0x20fb,
5189 "%s %d %8phC post nack\n",
5190 __func__, __LINE__, sess->port_name);
5191 qla24xx_post_nack_work(vha, sess, iocb,
5199 if (le16_to_cpu(iocb->u.isp24.flags) &
5200 NOTIFY24XX_FLAGS_GLOBAL_TPRLO) {
5202 qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS);
5209 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
5210 sess = qla2x00_find_fcport_by_loopid(vha, loop_id);
5211 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
5215 sess->fw_login_state = DSC_LS_LOGO_PEND;
5216 sess->logo_ack_needed = 1;
5217 memcpy(sess->iocb, iocb, IOCB_SIZE);
5220 res = qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS_SESS);
5222 ql_dbg(ql_dbg_disc, vha, 0x20fc,
5223 "%s: logo %llx res %d sess %p ",
5224 __func__, wwn, res, sess);
5227 * cmd went upper layer, look for qlt_xmit_tm_rsp()
5228 * for LOGO_ACK & sess delete
5233 /* cmd did not go to upper layer. */
5235 qlt_schedule_sess_for_deletion(sess);
5238 /* else logo will be ack */
5244 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
5246 if (tgt->link_reinit_iocb_pending) {
5247 qlt_send_notify_ack(ha->base_qpair,
5248 &tgt->link_reinit_iocb, 0, 0, 0, 0, 0, 0);
5249 tgt->link_reinit_iocb_pending = 0;
5252 sess = qla2x00_find_fcport_by_wwpn(vha,
5253 iocb->u.isp24.port_name, 1);
5255 ql_dbg(ql_dbg_disc, vha, 0x20fd,
5256 "sess %p lid %d|%d DS %d LS %d\n",
5257 sess, sess->loop_id, loop_id,
5258 sess->disc_state, sess->fw_login_state);
5261 res = 1; /* send notify ack */
5265 case ELS_FLOGI: /* should never happen */
5267 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf061,
5268 "qla_target(%d): Unsupported ELS command %x "
5269 "received\n", vha->vp_idx, iocb->u.isp24.status_subcode);
5270 res = qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS_SESS);
5274 ql_dbg(ql_dbg_disc, vha, 0xf026,
5275 "qla_target(%d): Exit ELS opcode: 0x%02x res %d\n",
5276 vha->vp_idx, iocb->u.isp24.status_subcode, res);
5282 * ha->hardware_lock supposed to be held on entry.
5283 * Might drop it, then reacquire.
5285 static void qlt_handle_imm_notify(struct scsi_qla_host *vha,
5286 struct imm_ntfy_from_isp *iocb)
5288 struct qla_hw_data *ha = vha->hw;
5289 uint32_t add_flags = 0;
5290 int send_notify_ack = 1;
5293 lockdep_assert_held(&ha->hardware_lock);
5295 status = le16_to_cpu(iocb->u.isp2x.status);
5297 case IMM_NTFY_LIP_RESET:
5299 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf032,
5300 "qla_target(%d): LIP reset (loop %#x), subcode %x\n",
5301 vha->vp_idx, le16_to_cpu(iocb->u.isp24.nport_handle),
5302 iocb->u.isp24.status_subcode);
5304 if (qlt_reset(vha, iocb, QLA_TGT_ABORT_ALL) == 0)
5305 send_notify_ack = 0;
5309 case IMM_NTFY_LIP_LINK_REINIT:
5311 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
5313 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf033,
5314 "qla_target(%d): LINK REINIT (loop %#x, "
5315 "subcode %x)\n", vha->vp_idx,
5316 le16_to_cpu(iocb->u.isp24.nport_handle),
5317 iocb->u.isp24.status_subcode);
5318 if (tgt->link_reinit_iocb_pending) {
5319 qlt_send_notify_ack(ha->base_qpair,
5320 &tgt->link_reinit_iocb, 0, 0, 0, 0, 0, 0);
5322 memcpy(&tgt->link_reinit_iocb, iocb, sizeof(*iocb));
5323 tgt->link_reinit_iocb_pending = 1;
5325 * QLogic requires to wait after LINK REINIT for possible
5326 * PDISC or ADISC ELS commands
5328 send_notify_ack = 0;
5332 case IMM_NTFY_PORT_LOGOUT:
5333 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf034,
5334 "qla_target(%d): Port logout (loop "
5335 "%#x, subcode %x)\n", vha->vp_idx,
5336 le16_to_cpu(iocb->u.isp24.nport_handle),
5337 iocb->u.isp24.status_subcode);
5339 if (qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS_SESS) == 0)
5340 send_notify_ack = 0;
5341 /* The sessions will be cleared in the callback, if needed */
5344 case IMM_NTFY_GLBL_TPRLO:
5345 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf035,
5346 "qla_target(%d): Global TPRLO (%x)\n", vha->vp_idx, status);
5347 if (qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS) == 0)
5348 send_notify_ack = 0;
5349 /* The sessions will be cleared in the callback, if needed */
5352 case IMM_NTFY_PORT_CONFIG:
5353 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf036,
5354 "qla_target(%d): Port config changed (%x)\n", vha->vp_idx,
5356 if (qlt_reset(vha, iocb, QLA_TGT_ABORT_ALL) == 0)
5357 send_notify_ack = 0;
5358 /* The sessions will be cleared in the callback, if needed */
5361 case IMM_NTFY_GLBL_LOGO:
5362 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06a,
5363 "qla_target(%d): Link failure detected\n",
5365 /* I_T nexus loss */
5366 if (qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS) == 0)
5367 send_notify_ack = 0;
5370 case IMM_NTFY_IOCB_OVERFLOW:
5371 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06b,
5372 "qla_target(%d): Cannot provide requested "
5373 "capability (IOCB overflowed the immediate notify "
5374 "resource count)\n", vha->vp_idx);
5377 case IMM_NTFY_ABORT_TASK:
5378 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf037,
5379 "qla_target(%d): Abort Task (S %08x I %#x -> "
5380 "L %#x)\n", vha->vp_idx,
5381 le16_to_cpu(iocb->u.isp2x.seq_id),
5382 GET_TARGET_ID(ha, (struct atio_from_isp *)iocb),
5383 le16_to_cpu(iocb->u.isp2x.lun));
5384 if (qlt_abort_task(vha, iocb) == 0)
5385 send_notify_ack = 0;
5388 case IMM_NTFY_RESOURCE:
5389 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06c,
5390 "qla_target(%d): Out of resources, host %ld\n",
5391 vha->vp_idx, vha->host_no);
5394 case IMM_NTFY_MSG_RX:
5395 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf038,
5396 "qla_target(%d): Immediate notify task %x\n",
5397 vha->vp_idx, iocb->u.isp2x.task_flags);
5401 if (qlt_24xx_handle_els(vha, iocb) == 0)
5402 send_notify_ack = 0;
5405 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06d,
5406 "qla_target(%d): Received unknown immediate "
5407 "notify status %x\n", vha->vp_idx, status);
5411 if (send_notify_ack)
5412 qlt_send_notify_ack(ha->base_qpair, iocb, add_flags, 0, 0, 0,
5417 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
5418 * This function sends busy to ISP 2xxx or 24xx.
5420 static int __qlt_send_busy(struct qla_qpair *qpair,
5421 struct atio_from_isp *atio, uint16_t status)
5423 struct scsi_qla_host *vha = qpair->vha;
5424 struct ctio7_to_24xx *ctio24;
5425 struct qla_hw_data *ha = vha->hw;
5427 struct fc_port *sess = NULL;
5428 unsigned long flags;
5432 id = be_to_port_id(atio->u.isp24.fcp_hdr.s_id);
5434 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
5435 sess = qla2x00_find_fcport_by_nportid(vha, &id, 1);
5436 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
5438 qlt_send_term_exchange(qpair, NULL, atio, 1, 0);
5441 /* Sending marker isn't necessary, since we called from ISR */
5443 pkt = (request_t *)__qla2x00_alloc_iocbs(qpair, NULL);
5445 ql_dbg(ql_dbg_io, vha, 0x3063,
5446 "qla_target(%d): %s failed: unable to allocate "
5447 "request packet", vha->vp_idx, __func__);
5451 qpair->tgt_counters.num_q_full_sent++;
5452 pkt->entry_count = 1;
5453 pkt->handle = QLA_TGT_SKIP_HANDLE | CTIO_COMPLETION_HANDLE_MARK;
5455 ctio24 = (struct ctio7_to_24xx *)pkt;
5456 ctio24->entry_type = CTIO_TYPE7;
5457 ctio24->nport_handle = cpu_to_le16(sess->loop_id);
5458 ctio24->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
5459 ctio24->vp_index = vha->vp_idx;
5460 ctio24->initiator_id = be_id_to_le(atio->u.isp24.fcp_hdr.s_id);
5461 ctio24->exchange_addr = atio->u.isp24.exchange_addr;
5462 temp = (atio->u.isp24.attr << 9) |
5463 CTIO7_FLAGS_STATUS_MODE_1 | CTIO7_FLAGS_SEND_STATUS |
5464 CTIO7_FLAGS_DONT_RET_CTIO;
5465 ctio24->u.status1.flags = cpu_to_le16(temp);
5467 * CTIO from fw w/o se_cmd doesn't provide enough info to retry it,
5468 * if the explicit conformation is used.
5470 ctio24->u.status1.ox_id =
5471 cpu_to_le16(be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id));
5472 ctio24->u.status1.scsi_status = cpu_to_le16(status);
5474 ctio24->u.status1.residual = cpu_to_le32(get_datalen_for_atio(atio));
5476 if (ctio24->u.status1.residual != 0)
5477 ctio24->u.status1.scsi_status |= cpu_to_le16(SS_RESIDUAL_UNDER);
5479 /* Memory Barrier */
5481 if (qpair->reqq_start_iocbs)
5482 qpair->reqq_start_iocbs(qpair);
5484 qla2x00_start_iocbs(vha, qpair->req);
5489 * This routine is used to allocate a command for either a QFull condition
5490 * (ie reply SAM_STAT_BUSY) or to terminate an exchange that did not go
5494 qlt_alloc_qfull_cmd(struct scsi_qla_host *vha,
5495 struct atio_from_isp *atio, uint16_t status, int qfull)
5497 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
5498 struct qla_hw_data *ha = vha->hw;
5499 struct fc_port *sess;
5500 struct qla_tgt_cmd *cmd;
5501 unsigned long flags;
5503 if (unlikely(tgt->tgt_stop)) {
5504 ql_dbg(ql_dbg_io, vha, 0x300a,
5505 "New command while device %p is shutting down\n", tgt);
5509 if ((vha->hw->tgt.num_qfull_cmds_alloc + 1) > MAX_QFULL_CMDS_ALLOC) {
5510 vha->hw->tgt.num_qfull_cmds_dropped++;
5511 if (vha->hw->tgt.num_qfull_cmds_dropped >
5512 vha->qla_stats.stat_max_qfull_cmds_dropped)
5513 vha->qla_stats.stat_max_qfull_cmds_dropped =
5514 vha->hw->tgt.num_qfull_cmds_dropped;
5516 ql_dbg(ql_dbg_io, vha, 0x3068,
5517 "qla_target(%d): %s: QFull CMD dropped[%d]\n",
5518 vha->vp_idx, __func__,
5519 vha->hw->tgt.num_qfull_cmds_dropped);
5521 qlt_chk_exch_leak_thresh_hold(vha);
5525 sess = ha->tgt.tgt_ops->find_sess_by_s_id
5526 (vha, atio->u.isp24.fcp_hdr.s_id);
5530 cmd = ha->tgt.tgt_ops->get_cmd(sess);
5532 ql_dbg(ql_dbg_io, vha, 0x3009,
5533 "qla_target(%d): %s: Allocation of cmd failed\n",
5534 vha->vp_idx, __func__);
5536 vha->hw->tgt.num_qfull_cmds_dropped++;
5537 if (vha->hw->tgt.num_qfull_cmds_dropped >
5538 vha->qla_stats.stat_max_qfull_cmds_dropped)
5539 vha->qla_stats.stat_max_qfull_cmds_dropped =
5540 vha->hw->tgt.num_qfull_cmds_dropped;
5542 qlt_chk_exch_leak_thresh_hold(vha);
5546 qlt_incr_num_pend_cmds(vha);
5547 INIT_LIST_HEAD(&cmd->cmd_list);
5548 memcpy(&cmd->atio, atio, sizeof(*atio));
5550 cmd->tgt = vha->vha_tgt.qla_tgt;
5552 cmd->reset_count = ha->base_qpair->chip_reset;
5554 cmd->qpair = ha->base_qpair;
5558 /* NOTE: borrowing the state field to carry the status */
5559 cmd->state = status;
5561 cmd->term_exchg = 1;
5563 spin_lock_irqsave(&vha->hw->tgt.q_full_lock, flags);
5564 list_add_tail(&cmd->cmd_list, &vha->hw->tgt.q_full_list);
5566 vha->hw->tgt.num_qfull_cmds_alloc++;
5567 if (vha->hw->tgt.num_qfull_cmds_alloc >
5568 vha->qla_stats.stat_max_qfull_cmds_alloc)
5569 vha->qla_stats.stat_max_qfull_cmds_alloc =
5570 vha->hw->tgt.num_qfull_cmds_alloc;
5571 spin_unlock_irqrestore(&vha->hw->tgt.q_full_lock, flags);
5575 qlt_free_qfull_cmds(struct qla_qpair *qpair)
5577 struct scsi_qla_host *vha = qpair->vha;
5578 struct qla_hw_data *ha = vha->hw;
5579 unsigned long flags;
5580 struct qla_tgt_cmd *cmd, *tcmd;
5581 struct list_head free_list, q_full_list;
5584 if (list_empty(&ha->tgt.q_full_list))
5587 INIT_LIST_HEAD(&free_list);
5588 INIT_LIST_HEAD(&q_full_list);
5590 spin_lock_irqsave(&vha->hw->tgt.q_full_lock, flags);
5591 if (list_empty(&ha->tgt.q_full_list)) {
5592 spin_unlock_irqrestore(&vha->hw->tgt.q_full_lock, flags);
5596 list_splice_init(&vha->hw->tgt.q_full_list, &q_full_list);
5597 spin_unlock_irqrestore(&vha->hw->tgt.q_full_lock, flags);
5599 spin_lock_irqsave(qpair->qp_lock_ptr, flags);
5600 list_for_each_entry_safe(cmd, tcmd, &q_full_list, cmd_list) {
5602 /* cmd->state is a borrowed field to hold status */
5603 rc = __qlt_send_busy(qpair, &cmd->atio, cmd->state);
5604 else if (cmd->term_exchg)
5605 rc = __qlt_send_term_exchange(qpair, NULL, &cmd->atio);
5611 ql_dbg(ql_dbg_io, vha, 0x3006,
5612 "%s: busy sent for ox_id[%04x]\n", __func__,
5613 be16_to_cpu(cmd->atio.u.isp24.fcp_hdr.ox_id));
5614 else if (cmd->term_exchg)
5615 ql_dbg(ql_dbg_io, vha, 0x3007,
5616 "%s: Term exchg sent for ox_id[%04x]\n", __func__,
5617 be16_to_cpu(cmd->atio.u.isp24.fcp_hdr.ox_id));
5619 ql_dbg(ql_dbg_io, vha, 0x3008,
5620 "%s: Unexpected cmd in QFull list %p\n", __func__,
5623 list_move_tail(&cmd->cmd_list, &free_list);
5625 /* piggy back on hardware_lock for protection */
5626 vha->hw->tgt.num_qfull_cmds_alloc--;
5628 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
5632 list_for_each_entry_safe(cmd, tcmd, &free_list, cmd_list) {
5633 list_del(&cmd->cmd_list);
5634 /* This cmd was never sent to TCM. There is no need
5635 * to schedule free or call free_cmd
5640 if (!list_empty(&q_full_list)) {
5641 spin_lock_irqsave(&vha->hw->tgt.q_full_lock, flags);
5642 list_splice(&q_full_list, &vha->hw->tgt.q_full_list);
5643 spin_unlock_irqrestore(&vha->hw->tgt.q_full_lock, flags);
5650 qlt_send_busy(struct qla_qpair *qpair, struct atio_from_isp *atio,
5654 struct scsi_qla_host *vha = qpair->vha;
5656 rc = __qlt_send_busy(qpair, atio, status);
5658 qlt_alloc_qfull_cmd(vha, atio, status, 1);
5662 qlt_chk_qfull_thresh_hold(struct scsi_qla_host *vha, struct qla_qpair *qpair,
5663 struct atio_from_isp *atio, uint8_t ha_locked)
5665 struct qla_hw_data *ha = vha->hw;
5666 unsigned long flags;
5668 if (ha->tgt.num_pend_cmds < Q_FULL_THRESH_HOLD(ha))
5672 spin_lock_irqsave(&ha->hardware_lock, flags);
5673 qlt_send_busy(qpair, atio, qla_sam_status);
5675 spin_unlock_irqrestore(&ha->hardware_lock, flags);
5680 /* ha->hardware_lock supposed to be held on entry */
5681 /* called via callback from qla2xxx */
5682 static void qlt_24xx_atio_pkt(struct scsi_qla_host *vha,
5683 struct atio_from_isp *atio, uint8_t ha_locked)
5685 struct qla_hw_data *ha = vha->hw;
5686 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
5688 unsigned long flags = 0;
5690 if (unlikely(tgt == NULL)) {
5691 ql_dbg(ql_dbg_tgt, vha, 0x3064,
5692 "ATIO pkt, but no tgt (ha %p)", ha);
5696 * In tgt_stop mode we also should allow all requests to pass.
5697 * Otherwise, some commands can stuck.
5700 tgt->atio_irq_cmd_count++;
5702 switch (atio->u.raw.entry_type) {
5704 if (unlikely(atio->u.isp24.exchange_addr ==
5705 cpu_to_le32(ATIO_EXCHANGE_ADDRESS_UNKNOWN))) {
5706 ql_dbg(ql_dbg_io, vha, 0x3065,
5707 "qla_target(%d): ATIO_TYPE7 "
5708 "received with UNKNOWN exchange address, "
5709 "sending QUEUE_FULL\n", vha->vp_idx);
5711 spin_lock_irqsave(&ha->hardware_lock, flags);
5712 qlt_send_busy(ha->base_qpair, atio, qla_sam_status);
5714 spin_unlock_irqrestore(&ha->hardware_lock,
5719 if (likely(atio->u.isp24.fcp_cmnd.task_mgmt_flags == 0)) {
5720 rc = qlt_chk_qfull_thresh_hold(vha, ha->base_qpair,
5723 tgt->atio_irq_cmd_count--;
5726 rc = qlt_handle_cmd_for_atio(vha, atio);
5728 rc = qlt_handle_task_mgmt(vha, atio);
5730 if (unlikely(rc != 0)) {
5732 spin_lock_irqsave(&ha->hardware_lock, flags);
5735 ql_dbg(ql_dbg_tgt, vha, 0xe05f,
5736 "qla_target: Unable to send command to target\n");
5739 ql_dbg(ql_dbg_tgt, vha, 0xe05f,
5740 "qla_target: Unable to send command to target, sending TERM EXCHANGE for rsp\n");
5741 qlt_send_term_exchange(ha->base_qpair, NULL,
5745 ql_dbg(ql_dbg_tgt, vha, 0xe060,
5746 "qla_target(%d): Unable to send command to target, sending BUSY status\n",
5748 qlt_send_busy(ha->base_qpair, atio,
5752 ql_dbg(ql_dbg_tgt, vha, 0xe060,
5753 "qla_target(%d): Unable to send command to target, sending BUSY status\n",
5755 qlt_send_busy(ha->base_qpair, atio,
5760 spin_unlock_irqrestore(&ha->hardware_lock,
5765 case IMMED_NOTIFY_TYPE:
5767 if (unlikely(atio->u.isp2x.entry_status != 0)) {
5768 ql_dbg(ql_dbg_tgt, vha, 0xe05b,
5769 "qla_target(%d): Received ATIO packet %x "
5770 "with error status %x\n", vha->vp_idx,
5771 atio->u.raw.entry_type,
5772 atio->u.isp2x.entry_status);
5775 ql_dbg(ql_dbg_tgt, vha, 0xe02e, "%s", "IMMED_NOTIFY ATIO");
5778 spin_lock_irqsave(&ha->hardware_lock, flags);
5779 qlt_handle_imm_notify(vha, (struct imm_ntfy_from_isp *)atio);
5781 spin_unlock_irqrestore(&ha->hardware_lock, flags);
5786 ql_dbg(ql_dbg_tgt, vha, 0xe05c,
5787 "qla_target(%d): Received unknown ATIO atio "
5788 "type %x\n", vha->vp_idx, atio->u.raw.entry_type);
5792 tgt->atio_irq_cmd_count--;
5796 * qpair lock is assume to be held
5797 * rc = 0 : send terminate & abts respond
5798 * rc != 0: do not send term & abts respond
5800 static int qlt_chk_unresolv_exchg(struct scsi_qla_host *vha,
5801 struct qla_qpair *qpair, struct abts_resp_from_24xx_fw *entry)
5803 struct qla_hw_data *ha = vha->hw;
5807 * Detect unresolved exchange. If the same ABTS is unable
5808 * to terminate an existing command and the same ABTS loops
5809 * between FW & Driver, then force FW dump. Under 1 jiff,
5810 * we should see multiple loops.
5812 if (qpair->retry_term_exchg_addr == entry->exchange_addr_to_abort &&
5813 qpair->retry_term_jiff == jiffies) {
5814 /* found existing exchange */
5815 qpair->retry_term_cnt++;
5816 if (qpair->retry_term_cnt >= 5) {
5818 qpair->retry_term_cnt = 0;
5819 ql_log(ql_log_warn, vha, 0xffff,
5820 "Unable to send ABTS Respond. Dumping firmware.\n");
5821 ql_dump_buffer(ql_dbg_tgt_mgt + ql_dbg_buffer,
5822 vha, 0xffff, (uint8_t *)entry, sizeof(*entry));
5824 if (qpair == ha->base_qpair)
5825 ha->isp_ops->fw_dump(vha);
5827 qla2xxx_dump_fw(vha);
5829 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
5830 qla2xxx_wake_dpc(vha);
5832 } else if (qpair->retry_term_jiff != jiffies) {
5833 qpair->retry_term_exchg_addr = entry->exchange_addr_to_abort;
5834 qpair->retry_term_cnt = 0;
5835 qpair->retry_term_jiff = jiffies;
5842 static void qlt_handle_abts_completion(struct scsi_qla_host *vha,
5843 struct rsp_que *rsp, response_t *pkt)
5845 struct abts_resp_from_24xx_fw *entry =
5846 (struct abts_resp_from_24xx_fw *)pkt;
5847 u32 h = pkt->handle & ~QLA_TGT_HANDLE_MASK;
5848 struct qla_tgt_mgmt_cmd *mcmd;
5849 struct qla_hw_data *ha = vha->hw;
5851 mcmd = qlt_ctio_to_cmd(vha, rsp, pkt->handle, pkt);
5852 if (mcmd == NULL && h != QLA_TGT_SKIP_HANDLE) {
5853 ql_dbg(ql_dbg_async, vha, 0xe064,
5854 "qla_target(%d): ABTS Comp without mcmd\n",
5861 vha->vha_tgt.qla_tgt->abts_resp_expected--;
5863 ql_dbg(ql_dbg_tgt, vha, 0xe038,
5864 "ABTS_RESP_24XX: compl_status %x\n",
5865 entry->compl_status);
5867 if (le16_to_cpu(entry->compl_status) != ABTS_RESP_COMPL_SUCCESS) {
5868 if (le32_to_cpu(entry->error_subcode1) == 0x1E &&
5869 le32_to_cpu(entry->error_subcode2) == 0) {
5870 if (qlt_chk_unresolv_exchg(vha, rsp->qpair, entry)) {
5871 ha->tgt.tgt_ops->free_mcmd(mcmd);
5874 qlt_24xx_retry_term_exchange(vha, rsp->qpair,
5877 ql_dbg(ql_dbg_tgt, vha, 0xe063,
5878 "qla_target(%d): ABTS_RESP_24XX failed %x (subcode %x:%x)",
5879 vha->vp_idx, entry->compl_status,
5880 entry->error_subcode1,
5881 entry->error_subcode2);
5882 ha->tgt.tgt_ops->free_mcmd(mcmd);
5885 ha->tgt.tgt_ops->free_mcmd(mcmd);
5889 /* ha->hardware_lock supposed to be held on entry */
5890 /* called via callback from qla2xxx */
5891 static void qlt_response_pkt(struct scsi_qla_host *vha,
5892 struct rsp_que *rsp, response_t *pkt)
5894 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
5896 if (unlikely(tgt == NULL)) {
5897 ql_dbg(ql_dbg_tgt, vha, 0xe05d,
5898 "qla_target(%d): Response pkt %x received, but no tgt (ha %p)\n",
5899 vha->vp_idx, pkt->entry_type, vha->hw);
5904 * In tgt_stop mode we also should allow all requests to pass.
5905 * Otherwise, some commands can stuck.
5908 switch (pkt->entry_type) {
5912 struct ctio7_from_24xx *entry = (struct ctio7_from_24xx *)pkt;
5914 qlt_do_ctio_completion(vha, rsp, entry->handle,
5915 le16_to_cpu(entry->status)|(pkt->entry_status << 16),
5920 case ACCEPT_TGT_IO_TYPE:
5922 struct atio_from_isp *atio = (struct atio_from_isp *)pkt;
5925 if (atio->u.isp2x.status !=
5926 cpu_to_le16(ATIO_CDB_VALID)) {
5927 ql_dbg(ql_dbg_tgt, vha, 0xe05e,
5928 "qla_target(%d): ATIO with error "
5929 "status %x received\n", vha->vp_idx,
5930 le16_to_cpu(atio->u.isp2x.status));
5934 rc = qlt_chk_qfull_thresh_hold(vha, rsp->qpair, atio, 1);
5938 rc = qlt_handle_cmd_for_atio(vha, atio);
5939 if (unlikely(rc != 0)) {
5942 ql_dbg(ql_dbg_tgt, vha, 0xe05f,
5943 "qla_target: Unable to send command to target\n");
5946 ql_dbg(ql_dbg_tgt, vha, 0xe05f,
5947 "qla_target: Unable to send command to target, sending TERM EXCHANGE for rsp\n");
5948 qlt_send_term_exchange(rsp->qpair, NULL,
5952 ql_dbg(ql_dbg_tgt, vha, 0xe060,
5953 "qla_target(%d): Unable to send command to target, sending BUSY status\n",
5955 qlt_send_busy(rsp->qpair, atio,
5959 ql_dbg(ql_dbg_tgt, vha, 0xe060,
5960 "qla_target(%d): Unable to send command to target, sending BUSY status\n",
5962 qlt_send_busy(rsp->qpair, atio,
5970 case CONTINUE_TGT_IO_TYPE:
5972 struct ctio_to_2xxx *entry = (struct ctio_to_2xxx *)pkt;
5974 qlt_do_ctio_completion(vha, rsp, entry->handle,
5975 le16_to_cpu(entry->status)|(pkt->entry_status << 16),
5982 struct ctio_to_2xxx *entry = (struct ctio_to_2xxx *)pkt;
5984 qlt_do_ctio_completion(vha, rsp, entry->handle,
5985 le16_to_cpu(entry->status)|(pkt->entry_status << 16),
5990 case IMMED_NOTIFY_TYPE:
5991 ql_dbg(ql_dbg_tgt, vha, 0xe035, "%s", "IMMED_NOTIFY\n");
5992 qlt_handle_imm_notify(vha, (struct imm_ntfy_from_isp *)pkt);
5995 case NOTIFY_ACK_TYPE:
5996 if (tgt->notify_ack_expected > 0) {
5997 struct nack_to_isp *entry = (struct nack_to_isp *)pkt;
5999 ql_dbg(ql_dbg_tgt, vha, 0xe036,
6000 "NOTIFY_ACK seq %08x status %x\n",
6001 le16_to_cpu(entry->u.isp2x.seq_id),
6002 le16_to_cpu(entry->u.isp2x.status));
6003 tgt->notify_ack_expected--;
6004 if (entry->u.isp2x.status !=
6005 cpu_to_le16(NOTIFY_ACK_SUCCESS)) {
6006 ql_dbg(ql_dbg_tgt, vha, 0xe061,
6007 "qla_target(%d): NOTIFY_ACK "
6008 "failed %x\n", vha->vp_idx,
6009 le16_to_cpu(entry->u.isp2x.status));
6012 ql_dbg(ql_dbg_tgt, vha, 0xe062,
6013 "qla_target(%d): Unexpected NOTIFY_ACK received\n",
6018 case ABTS_RECV_24XX:
6019 ql_dbg(ql_dbg_tgt, vha, 0xe037,
6020 "ABTS_RECV_24XX: instance %d\n", vha->vp_idx);
6021 qlt_24xx_handle_abts(vha, (struct abts_recv_from_24xx *)pkt);
6024 case ABTS_RESP_24XX:
6025 if (tgt->abts_resp_expected > 0) {
6026 qlt_handle_abts_completion(vha, rsp, pkt);
6028 ql_dbg(ql_dbg_tgt, vha, 0xe064,
6029 "qla_target(%d): Unexpected ABTS_RESP_24XX "
6030 "received\n", vha->vp_idx);
6035 ql_dbg(ql_dbg_tgt, vha, 0xe065,
6036 "qla_target(%d): Received unknown response pkt "
6037 "type %x\n", vha->vp_idx, pkt->entry_type);
6044 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
6046 void qlt_async_event(uint16_t code, struct scsi_qla_host *vha,
6049 struct qla_hw_data *ha = vha->hw;
6050 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
6053 if (!tgt || tgt->tgt_stop || tgt->tgt_stopped)
6056 if (((code == MBA_POINT_TO_POINT) || (code == MBA_CHG_IN_CONNECTION)) &&
6060 * In tgt_stop mode we also should allow all requests to pass.
6061 * Otherwise, some commands can stuck.
6066 case MBA_RESET: /* Reset */
6067 case MBA_SYSTEM_ERR: /* System Error */
6068 case MBA_REQ_TRANSFER_ERR: /* Request Transfer Error */
6069 case MBA_RSP_TRANSFER_ERR: /* Response Transfer Error */
6070 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03a,
6071 "qla_target(%d): System error async event %#x "
6072 "occurred", vha->vp_idx, code);
6074 case MBA_WAKEUP_THRES: /* Request Queue Wake-up. */
6075 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
6080 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03b,
6081 "qla_target(%d): Async LOOP_UP occurred "
6082 "(m[0]=%x, m[1]=%x, m[2]=%x, m[3]=%x)", vha->vp_idx,
6083 mailbox[0], mailbox[1], mailbox[2], mailbox[3]);
6084 if (tgt->link_reinit_iocb_pending) {
6085 qlt_send_notify_ack(ha->base_qpair,
6086 &tgt->link_reinit_iocb,
6088 tgt->link_reinit_iocb_pending = 0;
6093 case MBA_LIP_OCCURRED:
6096 case MBA_RSCN_UPDATE:
6097 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03c,
6098 "qla_target(%d): Async event %#x occurred "
6099 "(m[0]=%x, m[1]=%x, m[2]=%x, m[3]=%x)", vha->vp_idx, code,
6100 mailbox[0], mailbox[1], mailbox[2], mailbox[3]);
6103 case MBA_REJECTED_FCP_CMD:
6104 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf017,
6105 "qla_target(%d): Async event LS_REJECT occurred (m[0]=%x, m[1]=%x, m[2]=%x, m[3]=%x)",
6107 mailbox[0], mailbox[1], mailbox[2], mailbox[3]);
6109 if (mailbox[3] == 1) {
6110 /* exchange starvation. */
6111 vha->hw->exch_starvation++;
6112 if (vha->hw->exch_starvation > 5) {
6113 ql_log(ql_log_warn, vha, 0xd03a,
6114 "Exchange starvation-. Resetting RISC\n");
6116 vha->hw->exch_starvation = 0;
6117 if (IS_P3P_TYPE(vha->hw))
6118 set_bit(FCOE_CTX_RESET_NEEDED,
6121 set_bit(ISP_ABORT_NEEDED,
6123 qla2xxx_wake_dpc(vha);
6128 case MBA_PORT_UPDATE:
6129 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03d,
6130 "qla_target(%d): Port update async event %#x "
6131 "occurred: updating the ports database (m[0]=%x, m[1]=%x, "
6132 "m[2]=%x, m[3]=%x)", vha->vp_idx, code,
6133 mailbox[0], mailbox[1], mailbox[2], mailbox[3]);
6135 login_code = mailbox[2];
6136 if (login_code == 0x4) {
6137 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03e,
6138 "Async MB 2: Got PLOGI Complete\n");
6139 vha->hw->exch_starvation = 0;
6140 } else if (login_code == 0x7)
6141 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03f,
6142 "Async MB 2: Port Logged Out\n");
6150 static fc_port_t *qlt_get_port_database(struct scsi_qla_host *vha,
6153 fc_port_t *fcport, *tfcp, *del;
6155 unsigned long flags;
6158 fcport = qla2x00_alloc_fcport(vha, GFP_KERNEL);
6160 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06f,
6161 "qla_target(%d): Allocation of tmp FC port failed",
6166 fcport->loop_id = loop_id;
6168 rc = qla24xx_gpdb_wait(vha, fcport, 0);
6169 if (rc != QLA_SUCCESS) {
6170 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf070,
6171 "qla_target(%d): Failed to retrieve fcport "
6172 "information -- get_port_database() returned %x "
6173 "(loop_id=0x%04x)", vha->vp_idx, rc, loop_id);
6179 spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
6180 tfcp = qla2x00_find_fcport_by_wwpn(vha, fcport->port_name, 1);
6183 tfcp->d_id = fcport->d_id;
6184 tfcp->port_type = fcport->port_type;
6185 tfcp->supported_classes = fcport->supported_classes;
6186 tfcp->flags |= fcport->flags;
6187 tfcp->scan_state = QLA_FCPORT_FOUND;
6192 if (vha->hw->current_topology == ISP_CFG_F)
6193 fcport->flags |= FCF_FABRIC_DEVICE;
6195 list_add_tail(&fcport->list, &vha->vp_fcports);
6196 if (!IS_SW_RESV_ADDR(fcport->d_id))
6197 vha->fcport_count++;
6198 fcport->login_gen++;
6199 qla2x00_set_fcport_disc_state(fcport, DSC_LOGIN_COMPLETE);
6200 fcport->login_succ = 1;
6204 fcport->deleted = 0;
6205 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
6207 switch (vha->host->active_mode) {
6208 case MODE_INITIATOR:
6211 if (!IS_IIDMA_CAPABLE(vha->hw) || !vha->hw->flags.gpsc_supported) {
6212 qla24xx_sched_upd_fcport(fcport);
6214 ql_dbg(ql_dbg_disc, vha, 0x20ff,
6215 "%s %d %8phC post gpsc fcp_cnt %d\n",
6216 __func__, __LINE__, fcport->port_name, vha->fcport_count);
6217 qla24xx_post_gpsc_work(vha, fcport);
6227 qla2x00_free_fcport(del);
6232 /* Must be called under tgt_mutex */
6233 static struct fc_port *qlt_make_local_sess(struct scsi_qla_host *vha,
6236 struct fc_port *sess = NULL;
6237 fc_port_t *fcport = NULL;
6238 int rc, global_resets;
6239 uint16_t loop_id = 0;
6241 if (s_id.domain == 0xFF && s_id.area == 0xFC) {
6243 * This is Domain Controller, so it should be
6244 * OK to drop SCSI commands from it.
6246 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf042,
6247 "Unable to find initiator with S_ID %x:%x:%x",
6248 s_id.domain, s_id.area, s_id.al_pa);
6252 mutex_lock(&vha->vha_tgt.tgt_mutex);
6256 atomic_read(&vha->vha_tgt.qla_tgt->tgt_global_resets_count);
6258 rc = qla24xx_get_loop_id(vha, s_id, &loop_id);
6260 mutex_unlock(&vha->vha_tgt.tgt_mutex);
6262 ql_log(ql_log_info, vha, 0xf071,
6263 "qla_target(%d): Unable to find "
6264 "initiator with S_ID %x:%x:%x",
6265 vha->vp_idx, s_id.domain, s_id.area, s_id.al_pa);
6267 if (rc == -ENOENT) {
6268 qlt_port_logo_t logo;
6270 logo.id = be_to_port_id(s_id);
6272 qlt_send_first_logo(vha, &logo);
6278 fcport = qlt_get_port_database(vha, loop_id);
6280 mutex_unlock(&vha->vha_tgt.tgt_mutex);
6284 if (global_resets !=
6285 atomic_read(&vha->vha_tgt.qla_tgt->tgt_global_resets_count)) {
6286 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf043,
6287 "qla_target(%d): global reset during session discovery "
6288 "(counter was %d, new %d), retrying", vha->vp_idx,
6290 atomic_read(&vha->vha_tgt.
6291 qla_tgt->tgt_global_resets_count));
6295 sess = qlt_create_sess(vha, fcport, true);
6297 mutex_unlock(&vha->vha_tgt.tgt_mutex);
6302 static void qlt_abort_work(struct qla_tgt *tgt,
6303 struct qla_tgt_sess_work_param *prm)
6305 struct scsi_qla_host *vha = tgt->vha;
6306 struct qla_hw_data *ha = vha->hw;
6307 struct fc_port *sess = NULL;
6308 unsigned long flags = 0, flags2 = 0;
6312 spin_lock_irqsave(&ha->tgt.sess_lock, flags2);
6317 s_id = le_id_to_be(prm->abts.fcp_hdr_le.s_id);
6319 sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha, s_id);
6321 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags2);
6323 sess = qlt_make_local_sess(vha, s_id);
6324 /* sess has got an extra creation ref */
6326 spin_lock_irqsave(&ha->tgt.sess_lock, flags2);
6330 if (sess->deleted) {
6335 if (!kref_get_unless_zero(&sess->sess_kref)) {
6336 ql_dbg(ql_dbg_tgt_tmr, vha, 0xf01c,
6337 "%s: kref_get fail %8phC \n",
6338 __func__, sess->port_name);
6344 rc = __qlt_24xx_handle_abts(vha, &prm->abts, sess);
6345 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags2);
6347 ha->tgt.tgt_ops->put_sess(sess);
6354 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags2);
6357 spin_lock_irqsave(&ha->hardware_lock, flags);
6358 qlt_24xx_send_abts_resp(ha->base_qpair, &prm->abts,
6359 FCP_TMF_REJECTED, false);
6360 spin_unlock_irqrestore(&ha->hardware_lock, flags);
6363 static void qlt_tmr_work(struct qla_tgt *tgt,
6364 struct qla_tgt_sess_work_param *prm)
6366 struct atio_from_isp *a = &prm->tm_iocb2;
6367 struct scsi_qla_host *vha = tgt->vha;
6368 struct qla_hw_data *ha = vha->hw;
6369 struct fc_port *sess;
6370 unsigned long flags;
6377 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
6382 s_id = prm->tm_iocb2.u.isp24.fcp_hdr.s_id;
6383 sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha, s_id);
6385 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
6387 sess = qlt_make_local_sess(vha, s_id);
6388 /* sess has got an extra creation ref */
6390 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
6394 if (sess->deleted) {
6398 if (!kref_get_unless_zero(&sess->sess_kref)) {
6399 ql_dbg(ql_dbg_tgt_tmr, vha, 0xf020,
6400 "%s: kref_get fail %8phC\n",
6401 __func__, sess->port_name);
6407 fn = a->u.isp24.fcp_cmnd.task_mgmt_flags;
6409 scsilun_to_int((struct scsi_lun *)&a->u.isp24.fcp_cmnd.lun);
6411 rc = qlt_issue_task_mgmt(sess, unpacked_lun, fn, iocb, 0);
6412 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
6414 ha->tgt.tgt_ops->put_sess(sess);
6421 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
6423 qlt_send_term_exchange(ha->base_qpair, NULL, &prm->tm_iocb2, 1, 0);
6426 static void qlt_sess_work_fn(struct work_struct *work)
6428 struct qla_tgt *tgt = container_of(work, struct qla_tgt, sess_work);
6429 struct scsi_qla_host *vha = tgt->vha;
6430 unsigned long flags;
6432 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf000, "Sess work (tgt %p)", tgt);
6434 spin_lock_irqsave(&tgt->sess_work_lock, flags);
6435 while (!list_empty(&tgt->sess_works_list)) {
6436 struct qla_tgt_sess_work_param *prm = list_entry(
6437 tgt->sess_works_list.next, typeof(*prm),
6438 sess_works_list_entry);
6441 * This work can be scheduled on several CPUs at time, so we
6442 * must delete the entry to eliminate double processing
6444 list_del(&prm->sess_works_list_entry);
6446 spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
6448 switch (prm->type) {
6449 case QLA_TGT_SESS_WORK_ABORT:
6450 qlt_abort_work(tgt, prm);
6452 case QLA_TGT_SESS_WORK_TM:
6453 qlt_tmr_work(tgt, prm);
6460 spin_lock_irqsave(&tgt->sess_work_lock, flags);
6464 spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
6467 /* Must be called under tgt_host_action_mutex */
6468 int qlt_add_target(struct qla_hw_data *ha, struct scsi_qla_host *base_vha)
6470 struct qla_tgt *tgt;
6472 struct qla_qpair_hint *h;
6474 if (!QLA_TGT_MODE_ENABLED())
6477 if (!IS_TGT_MODE_CAPABLE(ha)) {
6478 ql_log(ql_log_warn, base_vha, 0xe070,
6479 "This adapter does not support target mode.\n");
6483 ql_dbg(ql_dbg_tgt, base_vha, 0xe03b,
6484 "Registering target for host %ld(%p).\n", base_vha->host_no, ha);
6486 BUG_ON(base_vha->vha_tgt.qla_tgt != NULL);
6488 tgt = kzalloc(sizeof(struct qla_tgt), GFP_KERNEL);
6490 ql_dbg(ql_dbg_tgt, base_vha, 0xe066,
6491 "Unable to allocate struct qla_tgt\n");
6495 tgt->qphints = kcalloc(ha->max_qpairs + 1,
6496 sizeof(struct qla_qpair_hint),
6498 if (!tgt->qphints) {
6500 ql_log(ql_log_warn, base_vha, 0x0197,
6501 "Unable to allocate qpair hints.\n");
6505 if (!(base_vha->host->hostt->supported_mode & MODE_TARGET))
6506 base_vha->host->hostt->supported_mode |= MODE_TARGET;
6508 rc = btree_init64(&tgt->lun_qpair_map);
6510 kfree(tgt->qphints);
6512 ql_log(ql_log_info, base_vha, 0x0198,
6513 "Unable to initialize lun_qpair_map btree\n");
6516 h = &tgt->qphints[0];
6517 h->qpair = ha->base_qpair;
6518 INIT_LIST_HEAD(&h->hint_elem);
6519 h->cpuid = ha->base_qpair->cpuid;
6520 list_add_tail(&h->hint_elem, &ha->base_qpair->hints_list);
6522 for (i = 0; i < ha->max_qpairs; i++) {
6523 unsigned long flags;
6525 struct qla_qpair *qpair = ha->queue_pair_map[i];
6527 h = &tgt->qphints[i + 1];
6528 INIT_LIST_HEAD(&h->hint_elem);
6531 spin_lock_irqsave(qpair->qp_lock_ptr, flags);
6532 list_add_tail(&h->hint_elem, &qpair->hints_list);
6533 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
6534 h->cpuid = qpair->cpuid;
6539 tgt->vha = base_vha;
6540 init_waitqueue_head(&tgt->waitQ);
6541 INIT_LIST_HEAD(&tgt->del_sess_list);
6542 spin_lock_init(&tgt->sess_work_lock);
6543 INIT_WORK(&tgt->sess_work, qlt_sess_work_fn);
6544 INIT_LIST_HEAD(&tgt->sess_works_list);
6545 atomic_set(&tgt->tgt_global_resets_count, 0);
6547 base_vha->vha_tgt.qla_tgt = tgt;
6549 ql_dbg(ql_dbg_tgt, base_vha, 0xe067,
6550 "qla_target(%d): using 64 Bit PCI addressing",
6553 tgt->sg_tablesize = QLA_TGT_MAX_SG_24XX(base_vha->req->length - 3);
6555 mutex_lock(&qla_tgt_mutex);
6556 list_add_tail(&tgt->tgt_list_entry, &qla_tgt_glist);
6557 mutex_unlock(&qla_tgt_mutex);
6559 if (ha->tgt.tgt_ops && ha->tgt.tgt_ops->add_target)
6560 ha->tgt.tgt_ops->add_target(base_vha);
6565 /* Must be called under tgt_host_action_mutex */
6566 int qlt_remove_target(struct qla_hw_data *ha, struct scsi_qla_host *vha)
6568 if (!vha->vha_tgt.qla_tgt)
6571 if (vha->fc_vport) {
6572 qlt_release(vha->vha_tgt.qla_tgt);
6576 /* free left over qfull cmds */
6577 qlt_init_term_exchange(vha);
6579 ql_dbg(ql_dbg_tgt, vha, 0xe03c, "Unregistering target for host %ld(%p)",
6581 qlt_release(vha->vha_tgt.qla_tgt);
6586 void qla_remove_hostmap(struct qla_hw_data *ha)
6588 struct scsi_qla_host *node;
6591 btree_for_each_safe32(&ha->host_map, key, node)
6592 btree_remove32(&ha->host_map, key);
6594 btree_destroy32(&ha->host_map);
6597 static void qlt_lport_dump(struct scsi_qla_host *vha, u64 wwpn,
6600 pr_debug("qla2xxx HW vha->node_name: %8phC\n", vha->node_name);
6601 pr_debug("qla2xxx HW vha->port_name: %8phC\n", vha->port_name);
6602 put_unaligned_be64(wwpn, b);
6603 pr_debug("qla2xxx passed configfs WWPN: %8phC\n", b);
6607 * qlt_lport_register - register lport with external module
6609 * @target_lport_ptr: pointer for tcm_qla2xxx specific lport data
6610 * @phys_wwpn: physical port WWPN
6611 * @npiv_wwpn: NPIV WWPN
6612 * @npiv_wwnn: NPIV WWNN
6613 * @callback: lport initialization callback for tcm_qla2xxx code
6615 int qlt_lport_register(void *target_lport_ptr, u64 phys_wwpn,
6616 u64 npiv_wwpn, u64 npiv_wwnn,
6617 int (*callback)(struct scsi_qla_host *, void *, u64, u64))
6619 struct qla_tgt *tgt;
6620 struct scsi_qla_host *vha;
6621 struct qla_hw_data *ha;
6622 struct Scsi_Host *host;
6623 unsigned long flags;
6627 mutex_lock(&qla_tgt_mutex);
6628 list_for_each_entry(tgt, &qla_tgt_glist, tgt_list_entry) {
6636 if (!(host->hostt->supported_mode & MODE_TARGET))
6639 if (vha->qlini_mode == QLA2XXX_INI_MODE_ENABLED)
6642 spin_lock_irqsave(&ha->hardware_lock, flags);
6643 if ((!npiv_wwpn || !npiv_wwnn) && host->active_mode & MODE_TARGET) {
6644 pr_debug("MODE_TARGET already active on qla2xxx(%d)\n",
6646 spin_unlock_irqrestore(&ha->hardware_lock, flags);
6649 if (tgt->tgt_stop) {
6650 pr_debug("MODE_TARGET in shutdown on qla2xxx(%d)\n",
6652 spin_unlock_irqrestore(&ha->hardware_lock, flags);
6655 spin_unlock_irqrestore(&ha->hardware_lock, flags);
6657 if (!scsi_host_get(host)) {
6658 ql_dbg(ql_dbg_tgt, vha, 0xe068,
6659 "Unable to scsi_host_get() for"
6660 " qla2xxx scsi_host\n");
6663 qlt_lport_dump(vha, phys_wwpn, b);
6665 if (memcmp(vha->port_name, b, WWN_SIZE)) {
6666 scsi_host_put(host);
6669 rc = (*callback)(vha, target_lport_ptr, npiv_wwpn, npiv_wwnn);
6671 scsi_host_put(host);
6673 mutex_unlock(&qla_tgt_mutex);
6676 mutex_unlock(&qla_tgt_mutex);
6680 EXPORT_SYMBOL(qlt_lport_register);
6683 * qlt_lport_deregister - Degister lport
6685 * @vha: Registered scsi_qla_host pointer
6687 void qlt_lport_deregister(struct scsi_qla_host *vha)
6689 struct qla_hw_data *ha = vha->hw;
6690 struct Scsi_Host *sh = vha->host;
6692 * Clear the target_lport_ptr qla_target_template pointer in qla_hw_data
6694 vha->vha_tgt.target_lport_ptr = NULL;
6695 ha->tgt.tgt_ops = NULL;
6697 * Release the Scsi_Host reference for the underlying qla2xxx host
6701 EXPORT_SYMBOL(qlt_lport_deregister);
6703 /* Must be called under HW lock */
6704 void qlt_set_mode(struct scsi_qla_host *vha)
6706 switch (vha->qlini_mode) {
6707 case QLA2XXX_INI_MODE_DISABLED:
6708 case QLA2XXX_INI_MODE_EXCLUSIVE:
6709 vha->host->active_mode = MODE_TARGET;
6711 case QLA2XXX_INI_MODE_ENABLED:
6712 vha->host->active_mode = MODE_INITIATOR;
6714 case QLA2XXX_INI_MODE_DUAL:
6715 vha->host->active_mode = MODE_DUAL;
6722 /* Must be called under HW lock */
6723 static void qlt_clear_mode(struct scsi_qla_host *vha)
6725 switch (vha->qlini_mode) {
6726 case QLA2XXX_INI_MODE_DISABLED:
6727 vha->host->active_mode = MODE_UNKNOWN;
6729 case QLA2XXX_INI_MODE_EXCLUSIVE:
6730 vha->host->active_mode = MODE_INITIATOR;
6732 case QLA2XXX_INI_MODE_ENABLED:
6733 case QLA2XXX_INI_MODE_DUAL:
6734 vha->host->active_mode = MODE_INITIATOR;
6742 * qla_tgt_enable_vha - NO LOCK HELD
6744 * host_reset, bring up w/ Target Mode Enabled
6747 qlt_enable_vha(struct scsi_qla_host *vha)
6749 struct qla_hw_data *ha = vha->hw;
6750 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
6751 unsigned long flags;
6752 scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
6755 ql_dbg(ql_dbg_tgt, vha, 0xe069,
6756 "Unable to locate qla_tgt pointer from"
6757 " struct qla_hw_data\n");
6761 if (vha->qlini_mode == QLA2XXX_INI_MODE_ENABLED)
6764 if (ha->tgt.num_act_qpairs > ha->max_qpairs)
6765 ha->tgt.num_act_qpairs = ha->max_qpairs;
6766 spin_lock_irqsave(&ha->hardware_lock, flags);
6767 tgt->tgt_stopped = 0;
6769 spin_unlock_irqrestore(&ha->hardware_lock, flags);
6771 mutex_lock(&ha->optrom_mutex);
6772 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf021,
6775 qla24xx_disable_vp(vha);
6776 qla24xx_enable_vp(vha);
6778 set_bit(ISP_ABORT_NEEDED, &base_vha->dpc_flags);
6779 qla2xxx_wake_dpc(base_vha);
6780 WARN_ON_ONCE(qla2x00_wait_for_hba_online(base_vha) !=
6783 mutex_unlock(&ha->optrom_mutex);
6785 EXPORT_SYMBOL(qlt_enable_vha);
6788 * qla_tgt_disable_vha - NO LOCK HELD
6790 * Disable Target Mode and reset the adapter
6792 static void qlt_disable_vha(struct scsi_qla_host *vha)
6794 struct qla_hw_data *ha = vha->hw;
6795 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
6796 unsigned long flags;
6799 ql_dbg(ql_dbg_tgt, vha, 0xe06a,
6800 "Unable to locate qla_tgt pointer from"
6801 " struct qla_hw_data\n");
6806 spin_lock_irqsave(&ha->hardware_lock, flags);
6807 qlt_clear_mode(vha);
6808 spin_unlock_irqrestore(&ha->hardware_lock, flags);
6810 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
6811 qla2xxx_wake_dpc(vha);
6814 * We are expecting the offline state.
6815 * QLA_FUNCTION_FAILED means that adapter is offline.
6817 if (qla2x00_wait_for_hba_online(vha) != QLA_SUCCESS)
6818 ql_dbg(ql_dbg_tgt, vha, 0xe081,
6819 "adapter is offline\n");
6823 * Called from qla_init.c:qla24xx_vport_create() contex to setup
6824 * the target mode specific struct scsi_qla_host and struct qla_hw_data
6828 qlt_vport_create(struct scsi_qla_host *vha, struct qla_hw_data *ha)
6830 vha->vha_tgt.qla_tgt = NULL;
6832 mutex_init(&vha->vha_tgt.tgt_mutex);
6833 mutex_init(&vha->vha_tgt.tgt_host_action_mutex);
6835 qlt_clear_mode(vha);
6838 * NOTE: Currently the value is kept the same for <24xx and
6839 * >=24xx ISPs. If it is necessary to change it,
6840 * the check should be added for specific ISPs,
6841 * assigning the value appropriately.
6843 ha->tgt.atio_q_length = ATIO_ENTRY_CNT_24XX;
6845 qlt_add_target(ha, vha);
6849 qlt_rff_id(struct scsi_qla_host *vha)
6853 * FC-4 Feature bit 0 indicates target functionality to the name server.
6855 if (qla_tgt_mode_enabled(vha)) {
6856 fc4_feature = BIT_0;
6857 } else if (qla_ini_mode_enabled(vha)) {
6858 fc4_feature = BIT_1;
6859 } else if (qla_dual_mode_enabled(vha))
6860 fc4_feature = BIT_0 | BIT_1;
6866 * qlt_init_atio_q_entries() - Initializes ATIO queue entries.
6869 * Beginning of ATIO ring has initialization control block already built
6870 * by nvram config routine.
6872 * Returns 0 on success.
6875 qlt_init_atio_q_entries(struct scsi_qla_host *vha)
6877 struct qla_hw_data *ha = vha->hw;
6879 struct atio_from_isp *pkt = (struct atio_from_isp *)ha->tgt.atio_ring;
6881 if (qla_ini_mode_enabled(vha))
6884 for (cnt = 0; cnt < ha->tgt.atio_q_length; cnt++) {
6885 pkt->u.raw.signature = cpu_to_le32(ATIO_PROCESSED);
6892 * qlt_24xx_process_atio_queue() - Process ATIO queue entries.
6893 * @ha: SCSI driver HA context
6896 qlt_24xx_process_atio_queue(struct scsi_qla_host *vha, uint8_t ha_locked)
6898 struct qla_hw_data *ha = vha->hw;
6899 struct atio_from_isp *pkt;
6902 if (!ha->flags.fw_started)
6905 while ((ha->tgt.atio_ring_ptr->signature != ATIO_PROCESSED) ||
6906 fcpcmd_is_corrupted(ha->tgt.atio_ring_ptr)) {
6907 pkt = (struct atio_from_isp *)ha->tgt.atio_ring_ptr;
6908 cnt = pkt->u.raw.entry_count;
6910 if (unlikely(fcpcmd_is_corrupted(ha->tgt.atio_ring_ptr))) {
6912 * This packet is corrupted. The header + payload
6913 * can not be trusted. There is no point in passing
6916 ql_log(ql_log_warn, vha, 0xd03c,
6917 "corrupted fcp frame SID[%3phN] OXID[%04x] EXCG[%x] %64phN\n",
6918 &pkt->u.isp24.fcp_hdr.s_id,
6919 be16_to_cpu(pkt->u.isp24.fcp_hdr.ox_id),
6920 pkt->u.isp24.exchange_addr, pkt);
6922 adjust_corrupted_atio(pkt);
6923 qlt_send_term_exchange(ha->base_qpair, NULL, pkt,
6926 qlt_24xx_atio_pkt_all_vps(vha,
6927 (struct atio_from_isp *)pkt, ha_locked);
6930 for (i = 0; i < cnt; i++) {
6931 ha->tgt.atio_ring_index++;
6932 if (ha->tgt.atio_ring_index == ha->tgt.atio_q_length) {
6933 ha->tgt.atio_ring_index = 0;
6934 ha->tgt.atio_ring_ptr = ha->tgt.atio_ring;
6936 ha->tgt.atio_ring_ptr++;
6938 pkt->u.raw.signature = cpu_to_le32(ATIO_PROCESSED);
6939 pkt = (struct atio_from_isp *)ha->tgt.atio_ring_ptr;
6944 /* Adjust ring index */
6945 wrt_reg_dword(ISP_ATIO_Q_OUT(vha), ha->tgt.atio_ring_index);
6949 qlt_24xx_config_rings(struct scsi_qla_host *vha)
6951 struct qla_hw_data *ha = vha->hw;
6952 struct qla_msix_entry *msix = &ha->msix_entries[2];
6953 struct init_cb_24xx *icb = (struct init_cb_24xx *)ha->init_cb;
6955 if (!QLA_TGT_MODE_ENABLED())
6958 wrt_reg_dword(ISP_ATIO_Q_IN(vha), 0);
6959 wrt_reg_dword(ISP_ATIO_Q_OUT(vha), 0);
6960 rd_reg_dword(ISP_ATIO_Q_OUT(vha));
6962 if (ha->flags.msix_enabled) {
6963 if (IS_QLA83XX(ha) || IS_QLA27XX(ha) || IS_QLA28XX(ha)) {
6964 if (IS_QLA2071(ha)) {
6965 /* 4 ports Baker: Enable Interrupt Handshake */
6967 icb->firmware_options_2 |= cpu_to_le32(BIT_26);
6969 icb->msix_atio = cpu_to_le16(msix->entry);
6970 icb->firmware_options_2 &= cpu_to_le32(~BIT_26);
6972 ql_dbg(ql_dbg_init, vha, 0xf072,
6973 "Registering ICB vector 0x%x for atio que.\n",
6978 if (IS_QLA83XX(ha) || IS_QLA27XX(ha) || IS_QLA28XX(ha)) {
6980 icb->firmware_options_2 |= cpu_to_le32(BIT_26);
6981 ql_dbg(ql_dbg_init, vha, 0xf072,
6982 "%s: Use INTx for ATIOQ.\n", __func__);
6988 qlt_24xx_config_nvram_stage1(struct scsi_qla_host *vha, struct nvram_24xx *nv)
6990 struct qla_hw_data *ha = vha->hw;
6993 if (!QLA_TGT_MODE_ENABLED())
6996 if (qla_tgt_mode_enabled(vha) || qla_dual_mode_enabled(vha)) {
6997 if (!ha->tgt.saved_set) {
6998 /* We save only once */
6999 ha->tgt.saved_exchange_count = nv->exchange_count;
7000 ha->tgt.saved_firmware_options_1 =
7001 nv->firmware_options_1;
7002 ha->tgt.saved_firmware_options_2 =
7003 nv->firmware_options_2;
7004 ha->tgt.saved_firmware_options_3 =
7005 nv->firmware_options_3;
7006 ha->tgt.saved_set = 1;
7009 if (qla_tgt_mode_enabled(vha))
7010 nv->exchange_count = cpu_to_le16(0xFFFF);
7012 nv->exchange_count = cpu_to_le16(vha->ql2xexchoffld);
7014 /* Enable target mode */
7015 nv->firmware_options_1 |= cpu_to_le32(BIT_4);
7017 /* Disable ini mode, if requested */
7018 if (qla_tgt_mode_enabled(vha))
7019 nv->firmware_options_1 |= cpu_to_le32(BIT_5);
7021 /* Disable Full Login after LIP */
7022 nv->firmware_options_1 &= cpu_to_le32(~BIT_13);
7023 /* Enable initial LIP */
7024 nv->firmware_options_1 &= cpu_to_le32(~BIT_9);
7025 if (ql2xtgt_tape_enable)
7026 /* Enable FC Tape support */
7027 nv->firmware_options_2 |= cpu_to_le32(BIT_12);
7029 /* Disable FC Tape support */
7030 nv->firmware_options_2 &= cpu_to_le32(~BIT_12);
7032 /* Disable Full Login after LIP */
7033 nv->host_p &= cpu_to_le32(~BIT_10);
7036 * clear BIT 15 explicitly as we have seen at least
7037 * a couple of instances where this was set and this
7038 * was causing the firmware to not be initialized.
7040 nv->firmware_options_1 &= cpu_to_le32(~BIT_15);
7041 /* Enable target PRLI control */
7042 nv->firmware_options_2 |= cpu_to_le32(BIT_14);
7044 if (IS_QLA25XX(ha)) {
7045 /* Change Loop-prefer to Pt-Pt */
7046 tmp = ~(BIT_4|BIT_5|BIT_6);
7047 nv->firmware_options_2 &= cpu_to_le32(tmp);
7049 nv->firmware_options_2 |= cpu_to_le32(tmp);
7052 if (ha->tgt.saved_set) {
7053 nv->exchange_count = ha->tgt.saved_exchange_count;
7054 nv->firmware_options_1 =
7055 ha->tgt.saved_firmware_options_1;
7056 nv->firmware_options_2 =
7057 ha->tgt.saved_firmware_options_2;
7058 nv->firmware_options_3 =
7059 ha->tgt.saved_firmware_options_3;
7064 if (ha->base_qpair->enable_class_2) {
7065 if (vha->flags.init_done)
7066 fc_host_supported_classes(vha->host) =
7067 FC_COS_CLASS2 | FC_COS_CLASS3;
7069 nv->firmware_options_2 |= cpu_to_le32(BIT_8);
7071 if (vha->flags.init_done)
7072 fc_host_supported_classes(vha->host) = FC_COS_CLASS3;
7074 nv->firmware_options_2 &= ~cpu_to_le32(BIT_8);
7079 qlt_24xx_config_nvram_stage2(struct scsi_qla_host *vha,
7080 struct init_cb_24xx *icb)
7082 struct qla_hw_data *ha = vha->hw;
7084 if (!QLA_TGT_MODE_ENABLED())
7087 if (ha->tgt.node_name_set) {
7088 memcpy(icb->node_name, ha->tgt.tgt_node_name, WWN_SIZE);
7089 icb->firmware_options_1 |= cpu_to_le32(BIT_14);
7094 qlt_81xx_config_nvram_stage1(struct scsi_qla_host *vha, struct nvram_81xx *nv)
7096 struct qla_hw_data *ha = vha->hw;
7099 if (!QLA_TGT_MODE_ENABLED())
7102 if (qla_tgt_mode_enabled(vha) || qla_dual_mode_enabled(vha)) {
7103 if (!ha->tgt.saved_set) {
7104 /* We save only once */
7105 ha->tgt.saved_exchange_count = nv->exchange_count;
7106 ha->tgt.saved_firmware_options_1 =
7107 nv->firmware_options_1;
7108 ha->tgt.saved_firmware_options_2 =
7109 nv->firmware_options_2;
7110 ha->tgt.saved_firmware_options_3 =
7111 nv->firmware_options_3;
7112 ha->tgt.saved_set = 1;
7115 if (qla_tgt_mode_enabled(vha))
7116 nv->exchange_count = cpu_to_le16(0xFFFF);
7118 nv->exchange_count = cpu_to_le16(vha->ql2xexchoffld);
7120 /* Enable target mode */
7121 nv->firmware_options_1 |= cpu_to_le32(BIT_4);
7123 /* Disable ini mode, if requested */
7124 if (qla_tgt_mode_enabled(vha))
7125 nv->firmware_options_1 |= cpu_to_le32(BIT_5);
7126 /* Disable Full Login after LIP */
7127 nv->firmware_options_1 &= cpu_to_le32(~BIT_13);
7128 /* Enable initial LIP */
7129 nv->firmware_options_1 &= cpu_to_le32(~BIT_9);
7131 * clear BIT 15 explicitly as we have seen at
7132 * least a couple of instances where this was set
7133 * and this was causing the firmware to not be
7136 nv->firmware_options_1 &= cpu_to_le32(~BIT_15);
7137 if (ql2xtgt_tape_enable)
7138 /* Enable FC tape support */
7139 nv->firmware_options_2 |= cpu_to_le32(BIT_12);
7141 /* Disable FC tape support */
7142 nv->firmware_options_2 &= cpu_to_le32(~BIT_12);
7144 /* Disable Full Login after LIP */
7145 nv->host_p &= cpu_to_le32(~BIT_10);
7146 /* Enable target PRLI control */
7147 nv->firmware_options_2 |= cpu_to_le32(BIT_14);
7149 /* Change Loop-prefer to Pt-Pt */
7150 tmp = ~(BIT_4|BIT_5|BIT_6);
7151 nv->firmware_options_2 &= cpu_to_le32(tmp);
7153 nv->firmware_options_2 |= cpu_to_le32(tmp);
7155 if (ha->tgt.saved_set) {
7156 nv->exchange_count = ha->tgt.saved_exchange_count;
7157 nv->firmware_options_1 =
7158 ha->tgt.saved_firmware_options_1;
7159 nv->firmware_options_2 =
7160 ha->tgt.saved_firmware_options_2;
7161 nv->firmware_options_3 =
7162 ha->tgt.saved_firmware_options_3;
7167 if (ha->base_qpair->enable_class_2) {
7168 if (vha->flags.init_done)
7169 fc_host_supported_classes(vha->host) =
7170 FC_COS_CLASS2 | FC_COS_CLASS3;
7172 nv->firmware_options_2 |= cpu_to_le32(BIT_8);
7174 if (vha->flags.init_done)
7175 fc_host_supported_classes(vha->host) = FC_COS_CLASS3;
7177 nv->firmware_options_2 &= ~cpu_to_le32(BIT_8);
7182 qlt_81xx_config_nvram_stage2(struct scsi_qla_host *vha,
7183 struct init_cb_81xx *icb)
7185 struct qla_hw_data *ha = vha->hw;
7187 if (!QLA_TGT_MODE_ENABLED())
7190 if (ha->tgt.node_name_set) {
7191 memcpy(icb->node_name, ha->tgt.tgt_node_name, WWN_SIZE);
7192 icb->firmware_options_1 |= cpu_to_le32(BIT_14);
7197 qlt_83xx_iospace_config(struct qla_hw_data *ha)
7199 if (!QLA_TGT_MODE_ENABLED())
7202 ha->msix_count += 1; /* For ATIO Q */
7207 qlt_modify_vp_config(struct scsi_qla_host *vha,
7208 struct vp_config_entry_24xx *vpmod)
7210 /* enable target mode. Bit5 = 1 => disable */
7211 if (qla_tgt_mode_enabled(vha) || qla_dual_mode_enabled(vha))
7212 vpmod->options_idx1 &= ~BIT_5;
7214 /* Disable ini mode, if requested. bit4 = 1 => disable */
7215 if (qla_tgt_mode_enabled(vha))
7216 vpmod->options_idx1 &= ~BIT_4;
7220 qlt_probe_one_stage1(struct scsi_qla_host *base_vha, struct qla_hw_data *ha)
7222 mutex_init(&base_vha->vha_tgt.tgt_mutex);
7223 if (!QLA_TGT_MODE_ENABLED())
7226 if (ha->mqenable || IS_QLA83XX(ha) || IS_QLA27XX(ha) || IS_QLA28XX(ha)) {
7227 ISP_ATIO_Q_IN(base_vha) = &ha->mqiobase->isp25mq.atio_q_in;
7228 ISP_ATIO_Q_OUT(base_vha) = &ha->mqiobase->isp25mq.atio_q_out;
7230 ISP_ATIO_Q_IN(base_vha) = &ha->iobase->isp24.atio_q_in;
7231 ISP_ATIO_Q_OUT(base_vha) = &ha->iobase->isp24.atio_q_out;
7234 mutex_init(&base_vha->vha_tgt.tgt_host_action_mutex);
7236 INIT_LIST_HEAD(&base_vha->unknown_atio_list);
7237 INIT_DELAYED_WORK(&base_vha->unknown_atio_work,
7238 qlt_unknown_atio_work_fn);
7240 qlt_clear_mode(base_vha);
7242 qlt_update_vp_map(base_vha, SET_VP_IDX);
7246 qla83xx_msix_atio_q(int irq, void *dev_id)
7248 struct rsp_que *rsp;
7249 scsi_qla_host_t *vha;
7250 struct qla_hw_data *ha;
7251 unsigned long flags;
7253 rsp = (struct rsp_que *) dev_id;
7255 vha = pci_get_drvdata(ha->pdev);
7257 spin_lock_irqsave(&ha->tgt.atio_lock, flags);
7259 qlt_24xx_process_atio_queue(vha, 0);
7261 spin_unlock_irqrestore(&ha->tgt.atio_lock, flags);
7267 qlt_handle_abts_recv_work(struct work_struct *work)
7269 struct qla_tgt_sess_op *op = container_of(work,
7270 struct qla_tgt_sess_op, work);
7271 scsi_qla_host_t *vha = op->vha;
7272 struct qla_hw_data *ha = vha->hw;
7273 unsigned long flags;
7275 if (qla2x00_reset_active(vha) ||
7276 (op->chip_reset != ha->base_qpair->chip_reset))
7279 spin_lock_irqsave(&ha->tgt.atio_lock, flags);
7280 qlt_24xx_process_atio_queue(vha, 0);
7281 spin_unlock_irqrestore(&ha->tgt.atio_lock, flags);
7283 spin_lock_irqsave(&ha->hardware_lock, flags);
7284 qlt_response_pkt_all_vps(vha, op->rsp, (response_t *)&op->atio);
7285 spin_unlock_irqrestore(&ha->hardware_lock, flags);
7291 qlt_handle_abts_recv(struct scsi_qla_host *vha, struct rsp_que *rsp,
7294 struct qla_tgt_sess_op *op;
7296 op = kzalloc(sizeof(*op), GFP_ATOMIC);
7299 /* do not reach for ATIO queue here. This is best effort err
7300 * recovery at this point.
7302 qlt_response_pkt_all_vps(vha, rsp, pkt);
7306 memcpy(&op->atio, pkt, sizeof(*pkt));
7308 op->chip_reset = vha->hw->base_qpair->chip_reset;
7310 INIT_WORK(&op->work, qlt_handle_abts_recv_work);
7311 queue_work(qla_tgt_wq, &op->work);
7316 qlt_mem_alloc(struct qla_hw_data *ha)
7318 if (!QLA_TGT_MODE_ENABLED())
7321 ha->tgt.tgt_vp_map = kcalloc(MAX_MULTI_ID_FABRIC,
7322 sizeof(struct qla_tgt_vp_map),
7324 if (!ha->tgt.tgt_vp_map)
7327 ha->tgt.atio_ring = dma_alloc_coherent(&ha->pdev->dev,
7328 (ha->tgt.atio_q_length + 1) * sizeof(struct atio_from_isp),
7329 &ha->tgt.atio_dma, GFP_KERNEL);
7330 if (!ha->tgt.atio_ring) {
7331 kfree(ha->tgt.tgt_vp_map);
7338 qlt_mem_free(struct qla_hw_data *ha)
7340 if (!QLA_TGT_MODE_ENABLED())
7343 if (ha->tgt.atio_ring) {
7344 dma_free_coherent(&ha->pdev->dev, (ha->tgt.atio_q_length + 1) *
7345 sizeof(struct atio_from_isp), ha->tgt.atio_ring,
7348 ha->tgt.atio_ring = NULL;
7349 ha->tgt.atio_dma = 0;
7350 kfree(ha->tgt.tgt_vp_map);
7351 ha->tgt.tgt_vp_map = NULL;
7354 /* vport_slock to be held by the caller */
7356 qlt_update_vp_map(struct scsi_qla_host *vha, int cmd)
7362 key = vha->d_id.b24;
7366 if (!QLA_TGT_MODE_ENABLED())
7368 vha->hw->tgt.tgt_vp_map[vha->vp_idx].vha = vha;
7371 slot = btree_lookup32(&vha->hw->host_map, key);
7373 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf018,
7374 "Save vha in host_map %p %06x\n", vha, key);
7375 rc = btree_insert32(&vha->hw->host_map,
7376 key, vha, GFP_ATOMIC);
7378 ql_log(ql_log_info, vha, 0xd03e,
7379 "Unable to insert s_id into host_map: %06x\n",
7383 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf019,
7384 "replace existing vha in host_map %p %06x\n", vha, key);
7385 btree_update32(&vha->hw->host_map, key, vha);
7388 if (!QLA_TGT_MODE_ENABLED())
7390 vha->hw->tgt.tgt_vp_map[vha->vp_idx].vha = NULL;
7393 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01a,
7394 "clear vha in host_map %p %06x\n", vha, key);
7395 slot = btree_lookup32(&vha->hw->host_map, key);
7397 btree_remove32(&vha->hw->host_map, key);
7403 void qlt_update_host_map(struct scsi_qla_host *vha, port_id_t id)
7406 if (!vha->d_id.b24) {
7408 qlt_update_vp_map(vha, SET_AL_PA);
7409 } else if (vha->d_id.b24 != id.b24) {
7410 qlt_update_vp_map(vha, RESET_AL_PA);
7412 qlt_update_vp_map(vha, SET_AL_PA);
7416 static int __init qlt_parse_ini_mode(void)
7418 if (strcasecmp(qlini_mode, QLA2XXX_INI_MODE_STR_EXCLUSIVE) == 0)
7419 ql2x_ini_mode = QLA2XXX_INI_MODE_EXCLUSIVE;
7420 else if (strcasecmp(qlini_mode, QLA2XXX_INI_MODE_STR_DISABLED) == 0)
7421 ql2x_ini_mode = QLA2XXX_INI_MODE_DISABLED;
7422 else if (strcasecmp(qlini_mode, QLA2XXX_INI_MODE_STR_ENABLED) == 0)
7423 ql2x_ini_mode = QLA2XXX_INI_MODE_ENABLED;
7424 else if (strcasecmp(qlini_mode, QLA2XXX_INI_MODE_STR_DUAL) == 0)
7425 ql2x_ini_mode = QLA2XXX_INI_MODE_DUAL;
7432 int __init qlt_init(void)
7436 BUILD_BUG_ON(sizeof(struct ctio7_to_24xx) != 64);
7437 BUILD_BUG_ON(sizeof(struct ctio_to_2xxx) != 64);
7439 if (!qlt_parse_ini_mode()) {
7440 ql_log(ql_log_fatal, NULL, 0xe06b,
7441 "qlt_parse_ini_mode() failed\n");
7445 if (!QLA_TGT_MODE_ENABLED())
7448 qla_tgt_mgmt_cmd_cachep = kmem_cache_create("qla_tgt_mgmt_cmd_cachep",
7449 sizeof(struct qla_tgt_mgmt_cmd), __alignof__(struct
7450 qla_tgt_mgmt_cmd), 0, NULL);
7451 if (!qla_tgt_mgmt_cmd_cachep) {
7452 ql_log(ql_log_fatal, NULL, 0xd04b,
7453 "kmem_cache_create for qla_tgt_mgmt_cmd_cachep failed\n");
7457 qla_tgt_plogi_cachep = kmem_cache_create("qla_tgt_plogi_cachep",
7458 sizeof(struct qlt_plogi_ack_t), __alignof__(struct qlt_plogi_ack_t),
7461 if (!qla_tgt_plogi_cachep) {
7462 ql_log(ql_log_fatal, NULL, 0xe06d,
7463 "kmem_cache_create for qla_tgt_plogi_cachep failed\n");
7465 goto out_mgmt_cmd_cachep;
7468 qla_tgt_mgmt_cmd_mempool = mempool_create(25, mempool_alloc_slab,
7469 mempool_free_slab, qla_tgt_mgmt_cmd_cachep);
7470 if (!qla_tgt_mgmt_cmd_mempool) {
7471 ql_log(ql_log_fatal, NULL, 0xe06e,
7472 "mempool_create for qla_tgt_mgmt_cmd_mempool failed\n");
7474 goto out_plogi_cachep;
7477 qla_tgt_wq = alloc_workqueue("qla_tgt_wq", 0, 0);
7479 ql_log(ql_log_fatal, NULL, 0xe06f,
7480 "alloc_workqueue for qla_tgt_wq failed\n");
7482 goto out_cmd_mempool;
7485 * Return 1 to signal that initiator-mode is being disabled
7487 return (ql2x_ini_mode == QLA2XXX_INI_MODE_DISABLED) ? 1 : 0;
7490 mempool_destroy(qla_tgt_mgmt_cmd_mempool);
7492 kmem_cache_destroy(qla_tgt_plogi_cachep);
7493 out_mgmt_cmd_cachep:
7494 kmem_cache_destroy(qla_tgt_mgmt_cmd_cachep);
7500 if (!QLA_TGT_MODE_ENABLED())
7503 destroy_workqueue(qla_tgt_wq);
7504 mempool_destroy(qla_tgt_mgmt_cmd_mempool);
7505 kmem_cache_destroy(qla_tgt_plogi_cachep);
7506 kmem_cache_destroy(qla_tgt_mgmt_cmd_cachep);