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.");
52 module_param(ql2xuctrlirq, int, 0644);
53 MODULE_PARM_DESC(ql2xuctrlirq,
54 "User to control IRQ placement via smp_affinity."
55 "Valid with qlini_mode=disabled."
56 "1(default): enable");
58 int ql2x_ini_mode = QLA2XXX_INI_MODE_EXCLUSIVE;
60 static int qla_sam_status = SAM_STAT_BUSY;
61 static int tc_sam_status = SAM_STAT_TASK_SET_FULL; /* target core */
64 * From scsi/fc/fc_fcp.h
66 enum fcp_resp_rsp_codes {
68 FCP_DATA_LEN_INVALID = 1,
69 FCP_CMND_FIELDS_INVALID = 2,
70 FCP_DATA_PARAM_MISMATCH = 3,
73 FCP_TMF_INVALID_LUN = 9,
77 * fc_pri_ta from scsi/fc/fc_fcp.h
79 #define FCP_PTA_SIMPLE 0 /* simple task attribute */
80 #define FCP_PTA_HEADQ 1 /* head of queue task attribute */
81 #define FCP_PTA_ORDERED 2 /* ordered task attribute */
82 #define FCP_PTA_ACA 4 /* auto. contingent allegiance */
83 #define FCP_PTA_MASK 7 /* mask for task attribute field */
84 #define FCP_PRI_SHIFT 3 /* priority field starts in bit 3 */
85 #define FCP_PRI_RESVD_MASK 0x80 /* reserved bits in priority field */
88 * This driver calls qla2x00_alloc_iocbs() and qla2x00_issue_marker(), which
89 * must be called under HW lock and could unlock/lock it inside.
90 * It isn't an issue, since in the current implementation on the time when
91 * those functions are called:
93 * - Either context is IRQ and only IRQ handler can modify HW data,
94 * including rings related fields,
96 * - Or access to target mode variables from struct qla_tgt doesn't
97 * cross those functions boundaries, except tgt_stop, which
98 * additionally protected by irq_cmd_count.
100 /* Predefs for callbacks handed to qla2xxx LLD */
101 static void qlt_24xx_atio_pkt(struct scsi_qla_host *ha,
102 struct atio_from_isp *pkt, uint8_t);
103 static void qlt_response_pkt(struct scsi_qla_host *ha, struct rsp_que *rsp,
105 static int qlt_issue_task_mgmt(struct fc_port *sess, u64 lun,
106 int fn, void *iocb, int flags);
107 static void qlt_send_term_exchange(struct qla_qpair *, struct qla_tgt_cmd
108 *cmd, struct atio_from_isp *atio, int ha_locked, int ul_abort);
109 static void qlt_alloc_qfull_cmd(struct scsi_qla_host *vha,
110 struct atio_from_isp *atio, uint16_t status, int qfull);
111 static void qlt_disable_vha(struct scsi_qla_host *vha);
112 static void qlt_clear_tgt_db(struct qla_tgt *tgt);
113 static void qlt_send_notify_ack(struct qla_qpair *qpair,
114 struct imm_ntfy_from_isp *ntfy,
115 uint32_t add_flags, uint16_t resp_code, int resp_code_valid,
116 uint16_t srr_flags, uint16_t srr_reject_code, uint8_t srr_explan);
117 static void qlt_send_term_imm_notif(struct scsi_qla_host *vha,
118 struct imm_ntfy_from_isp *imm, int ha_locked);
119 static struct fc_port *qlt_create_sess(struct scsi_qla_host *vha,
120 fc_port_t *fcport, bool local);
121 void qlt_unreg_sess(struct fc_port *sess);
122 static void qlt_24xx_handle_abts(struct scsi_qla_host *,
123 struct abts_recv_from_24xx *);
124 static void qlt_send_busy(struct qla_qpair *, struct atio_from_isp *,
126 static int qlt_check_reserve_free_req(struct qla_qpair *qpair, uint32_t);
127 static inline uint32_t qlt_make_handle(struct qla_qpair *);
132 static struct kmem_cache *qla_tgt_mgmt_cmd_cachep;
133 struct kmem_cache *qla_tgt_plogi_cachep;
134 static mempool_t *qla_tgt_mgmt_cmd_mempool;
135 static struct workqueue_struct *qla_tgt_wq;
136 static DEFINE_MUTEX(qla_tgt_mutex);
137 static LIST_HEAD(qla_tgt_glist);
139 static const char *prot_op_str(u32 prot_op)
142 case TARGET_PROT_NORMAL: return "NORMAL";
143 case TARGET_PROT_DIN_INSERT: return "DIN_INSERT";
144 case TARGET_PROT_DOUT_INSERT: return "DOUT_INSERT";
145 case TARGET_PROT_DIN_STRIP: return "DIN_STRIP";
146 case TARGET_PROT_DOUT_STRIP: return "DOUT_STRIP";
147 case TARGET_PROT_DIN_PASS: return "DIN_PASS";
148 case TARGET_PROT_DOUT_PASS: return "DOUT_PASS";
149 default: return "UNKNOWN";
153 /* This API intentionally takes dest as a parameter, rather than returning
154 * int value to avoid caller forgetting to issue wmb() after the store */
155 void qlt_do_generation_tick(struct scsi_qla_host *vha, int *dest)
157 scsi_qla_host_t *base_vha = pci_get_drvdata(vha->hw->pdev);
158 *dest = atomic_inc_return(&base_vha->generation_tick);
163 /* Might release hw lock, then reaquire!! */
164 static inline int qlt_issue_marker(struct scsi_qla_host *vha, int vha_locked)
166 /* Send marker if required */
167 if (unlikely(vha->marker_needed != 0)) {
168 int rc = qla2x00_issue_marker(vha, vha_locked);
170 if (rc != QLA_SUCCESS) {
171 ql_dbg(ql_dbg_tgt, vha, 0xe03d,
172 "qla_target(%d): issue_marker() failed\n",
180 struct scsi_qla_host *qla_find_host_by_d_id(struct scsi_qla_host *vha,
183 struct scsi_qla_host *host;
186 if (vha->d_id.b.area == d_id.area &&
187 vha->d_id.b.domain == d_id.domain &&
188 vha->d_id.b.al_pa == d_id.al_pa)
191 key = be_to_port_id(d_id).b24;
193 host = btree_lookup32(&vha->hw->host_map, key);
195 ql_dbg(ql_dbg_tgt_mgt + ql_dbg_verbose, vha, 0xf005,
196 "Unable to find host %06x\n", key);
202 struct scsi_qla_host *qlt_find_host_by_vp_idx(struct scsi_qla_host *vha,
205 struct qla_hw_data *ha = vha->hw;
207 if (vha->vp_idx == vp_idx)
210 BUG_ON(ha->tgt.tgt_vp_map == NULL);
211 if (likely(test_bit(vp_idx, ha->vp_idx_map)))
212 return ha->tgt.tgt_vp_map[vp_idx].vha;
217 static inline void qlt_incr_num_pend_cmds(struct scsi_qla_host *vha)
221 spin_lock_irqsave(&vha->hw->tgt.q_full_lock, flags);
223 vha->hw->tgt.num_pend_cmds++;
224 if (vha->hw->tgt.num_pend_cmds > vha->qla_stats.stat_max_pend_cmds)
225 vha->qla_stats.stat_max_pend_cmds =
226 vha->hw->tgt.num_pend_cmds;
227 spin_unlock_irqrestore(&vha->hw->tgt.q_full_lock, flags);
229 static inline void qlt_decr_num_pend_cmds(struct scsi_qla_host *vha)
233 spin_lock_irqsave(&vha->hw->tgt.q_full_lock, flags);
234 vha->hw->tgt.num_pend_cmds--;
235 spin_unlock_irqrestore(&vha->hw->tgt.q_full_lock, flags);
239 static void qlt_queue_unknown_atio(scsi_qla_host_t *vha,
240 struct atio_from_isp *atio, uint8_t ha_locked)
242 struct qla_tgt_sess_op *u;
243 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
247 ql_dbg(ql_dbg_async, vha, 0x502c,
248 "qla_target(%d): dropping unknown ATIO_TYPE7, because tgt is being stopped",
253 u = kzalloc(sizeof(*u), GFP_ATOMIC);
258 memcpy(&u->atio, atio, sizeof(*atio));
259 INIT_LIST_HEAD(&u->cmd_list);
261 spin_lock_irqsave(&vha->cmd_list_lock, flags);
262 list_add_tail(&u->cmd_list, &vha->unknown_atio_list);
263 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
265 schedule_delayed_work(&vha->unknown_atio_work, 1);
271 qlt_send_term_exchange(vha->hw->base_qpair, NULL, atio, ha_locked, 0);
275 static void qlt_try_to_dequeue_unknown_atios(struct scsi_qla_host *vha,
278 struct qla_tgt_sess_op *u, *t;
279 scsi_qla_host_t *host;
280 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
284 list_for_each_entry_safe(u, t, &vha->unknown_atio_list, cmd_list) {
286 ql_dbg(ql_dbg_async, vha, 0x502e,
287 "Freeing unknown %s %p, because of Abort\n",
289 qlt_send_term_exchange(vha->hw->base_qpair, NULL,
290 &u->atio, ha_locked, 0);
294 host = qla_find_host_by_d_id(vha, u->atio.u.isp24.fcp_hdr.d_id);
296 ql_dbg(ql_dbg_async + ql_dbg_verbose, vha, 0x502f,
297 "Requeuing unknown ATIO_TYPE7 %p\n", u);
298 qlt_24xx_atio_pkt(host, &u->atio, ha_locked);
299 } else if (tgt->tgt_stop) {
300 ql_dbg(ql_dbg_async + ql_dbg_verbose, vha, 0x503a,
301 "Freeing unknown %s %p, because tgt is being stopped\n",
303 qlt_send_term_exchange(vha->hw->base_qpair, NULL,
304 &u->atio, ha_locked, 0);
306 ql_dbg(ql_dbg_async + ql_dbg_verbose, vha, 0x503d,
307 "Reschedule u %p, vha %p, host %p\n", u, vha, host);
310 schedule_delayed_work(&vha->unknown_atio_work,
317 spin_lock_irqsave(&vha->cmd_list_lock, flags);
318 list_del(&u->cmd_list);
319 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
324 void qlt_unknown_atio_work_fn(struct work_struct *work)
326 struct scsi_qla_host *vha = container_of(to_delayed_work(work),
327 struct scsi_qla_host, unknown_atio_work);
329 qlt_try_to_dequeue_unknown_atios(vha, 0);
332 static bool qlt_24xx_atio_pkt_all_vps(struct scsi_qla_host *vha,
333 struct atio_from_isp *atio, uint8_t ha_locked)
335 ql_dbg(ql_dbg_tgt, vha, 0xe072,
336 "%s: qla_target(%d): type %x ox_id %04x\n",
337 __func__, vha->vp_idx, atio->u.raw.entry_type,
338 be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id));
340 switch (atio->u.raw.entry_type) {
343 struct scsi_qla_host *host = qla_find_host_by_d_id(vha,
344 atio->u.isp24.fcp_hdr.d_id);
345 if (unlikely(NULL == host)) {
346 ql_dbg(ql_dbg_tgt, vha, 0xe03e,
347 "qla_target(%d): Received ATIO_TYPE7 "
348 "with unknown d_id %x:%x:%x\n", vha->vp_idx,
349 atio->u.isp24.fcp_hdr.d_id.domain,
350 atio->u.isp24.fcp_hdr.d_id.area,
351 atio->u.isp24.fcp_hdr.d_id.al_pa);
354 qlt_queue_unknown_atio(vha, atio, ha_locked);
357 if (unlikely(!list_empty(&vha->unknown_atio_list)))
358 qlt_try_to_dequeue_unknown_atios(vha, ha_locked);
360 qlt_24xx_atio_pkt(host, atio, ha_locked);
364 case IMMED_NOTIFY_TYPE:
366 struct scsi_qla_host *host = vha;
367 struct imm_ntfy_from_isp *entry =
368 (struct imm_ntfy_from_isp *)atio;
370 qlt_issue_marker(vha, ha_locked);
372 if ((entry->u.isp24.vp_index != 0xFF) &&
373 (entry->u.isp24.nport_handle != cpu_to_le16(0xFFFF))) {
374 host = qlt_find_host_by_vp_idx(vha,
375 entry->u.isp24.vp_index);
376 if (unlikely(!host)) {
377 ql_dbg(ql_dbg_tgt, vha, 0xe03f,
378 "qla_target(%d): Received "
379 "ATIO (IMMED_NOTIFY_TYPE) "
380 "with unknown vp_index %d\n",
381 vha->vp_idx, entry->u.isp24.vp_index);
385 qlt_24xx_atio_pkt(host, atio, ha_locked);
389 case VP_RPT_ID_IOCB_TYPE:
390 qla24xx_report_id_acquisition(vha,
391 (struct vp_rpt_id_entry_24xx *)atio);
396 struct abts_recv_from_24xx *entry =
397 (struct abts_recv_from_24xx *)atio;
398 struct scsi_qla_host *host = qlt_find_host_by_vp_idx(vha,
402 if (unlikely(!host)) {
403 ql_dbg(ql_dbg_tgt, vha, 0xe00a,
404 "qla_target(%d): Response pkt (ABTS_RECV_24XX) "
405 "received, with unknown vp_index %d\n",
406 vha->vp_idx, entry->vp_index);
410 spin_lock_irqsave(&host->hw->hardware_lock, flags);
411 qlt_24xx_handle_abts(host, (struct abts_recv_from_24xx *)atio);
413 spin_unlock_irqrestore(&host->hw->hardware_lock, flags);
417 /* case PUREX_IOCB_TYPE: ql2xmvasynctoatio */
420 ql_dbg(ql_dbg_tgt, vha, 0xe040,
421 "qla_target(%d): Received unknown ATIO atio "
422 "type %x\n", vha->vp_idx, atio->u.raw.entry_type);
429 void qlt_response_pkt_all_vps(struct scsi_qla_host *vha,
430 struct rsp_que *rsp, response_t *pkt)
432 switch (pkt->entry_type) {
434 ql_dbg(ql_dbg_tgt, vha, 0xe073,
435 "qla_target(%d):%s: CRC2 Response pkt\n",
436 vha->vp_idx, __func__);
440 struct ctio7_from_24xx *entry = (struct ctio7_from_24xx *)pkt;
441 struct scsi_qla_host *host = qlt_find_host_by_vp_idx(vha,
443 if (unlikely(!host)) {
444 ql_dbg(ql_dbg_tgt, vha, 0xe041,
445 "qla_target(%d): Response pkt (CTIO_TYPE7) "
446 "received, with unknown vp_index %d\n",
447 vha->vp_idx, entry->vp_index);
450 qlt_response_pkt(host, rsp, pkt);
454 case IMMED_NOTIFY_TYPE:
456 struct scsi_qla_host *host;
457 struct imm_ntfy_from_isp *entry =
458 (struct imm_ntfy_from_isp *)pkt;
460 host = qlt_find_host_by_vp_idx(vha, entry->u.isp24.vp_index);
461 if (unlikely(!host)) {
462 ql_dbg(ql_dbg_tgt, vha, 0xe042,
463 "qla_target(%d): Response pkt (IMMED_NOTIFY_TYPE) "
464 "received, with unknown vp_index %d\n",
465 vha->vp_idx, entry->u.isp24.vp_index);
468 qlt_response_pkt(host, rsp, pkt);
472 case NOTIFY_ACK_TYPE:
474 struct scsi_qla_host *host = vha;
475 struct nack_to_isp *entry = (struct nack_to_isp *)pkt;
477 if (0xFF != entry->u.isp24.vp_index) {
478 host = qlt_find_host_by_vp_idx(vha,
479 entry->u.isp24.vp_index);
480 if (unlikely(!host)) {
481 ql_dbg(ql_dbg_tgt, vha, 0xe043,
482 "qla_target(%d): Response "
483 "pkt (NOTIFY_ACK_TYPE) "
484 "received, with unknown "
485 "vp_index %d\n", vha->vp_idx,
486 entry->u.isp24.vp_index);
490 qlt_response_pkt(host, rsp, pkt);
496 struct abts_recv_from_24xx *entry =
497 (struct abts_recv_from_24xx *)pkt;
498 struct scsi_qla_host *host = qlt_find_host_by_vp_idx(vha,
500 if (unlikely(!host)) {
501 ql_dbg(ql_dbg_tgt, vha, 0xe044,
502 "qla_target(%d): Response pkt "
503 "(ABTS_RECV_24XX) received, with unknown "
504 "vp_index %d\n", vha->vp_idx, entry->vp_index);
507 qlt_response_pkt(host, rsp, pkt);
513 struct abts_resp_to_24xx *entry =
514 (struct abts_resp_to_24xx *)pkt;
515 struct scsi_qla_host *host = qlt_find_host_by_vp_idx(vha,
517 if (unlikely(!host)) {
518 ql_dbg(ql_dbg_tgt, vha, 0xe045,
519 "qla_target(%d): Response pkt "
520 "(ABTS_RECV_24XX) received, with unknown "
521 "vp_index %d\n", vha->vp_idx, entry->vp_index);
524 qlt_response_pkt(host, rsp, pkt);
528 qlt_response_pkt(vha, rsp, pkt);
535 * All qlt_plogi_ack_t operations are protected by hardware_lock
537 static int qla24xx_post_nack_work(struct scsi_qla_host *vha, fc_port_t *fcport,
538 struct imm_ntfy_from_isp *ntfy, int type)
540 struct qla_work_evt *e;
542 e = qla2x00_alloc_work(vha, QLA_EVT_NACK);
544 return QLA_FUNCTION_FAILED;
546 e->u.nack.fcport = fcport;
547 e->u.nack.type = type;
548 memcpy(e->u.nack.iocb, ntfy, sizeof(struct imm_ntfy_from_isp));
549 return qla2x00_post_work(vha, e);
552 static void qla2x00_async_nack_sp_done(srb_t *sp, int res)
554 struct scsi_qla_host *vha = sp->vha;
557 ql_dbg(ql_dbg_disc, vha, 0x20f2,
558 "Async done-%s res %x %8phC type %d\n",
559 sp->name, res, sp->fcport->port_name, sp->type);
561 spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
562 sp->fcport->flags &= ~FCF_ASYNC_SENT;
563 sp->fcport->chip_reset = vha->hw->base_qpair->chip_reset;
567 sp->fcport->login_gen++;
568 sp->fcport->fw_login_state = DSC_LS_PLOGI_COMP;
569 sp->fcport->logout_on_delete = 1;
570 sp->fcport->plogi_nack_done_deadline = jiffies + HZ;
571 sp->fcport->send_els_logo = 0;
573 if (sp->fcport->flags & FCF_FCSP_DEVICE) {
574 ql_dbg(ql_dbg_edif, vha, 0x20ef,
575 "%s %8phC edif: PLOGI- AUTH WAIT\n", __func__,
576 sp->fcport->port_name);
577 qla2x00_set_fcport_disc_state(sp->fcport,
578 DSC_LOGIN_AUTH_PEND);
579 qla2x00_post_aen_work(vha, FCH_EVT_PORT_ONLINE,
580 sp->fcport->d_id.b24);
581 qla_edb_eventcreate(vha, VND_CMD_AUTH_STATE_NEEDED, sp->fcport->d_id.b24,
587 sp->fcport->fw_login_state = DSC_LS_PRLI_COMP;
588 sp->fcport->deleted = 0;
589 sp->fcport->send_els_logo = 0;
591 if (!sp->fcport->login_succ &&
592 !IS_SW_RESV_ADDR(sp->fcport->d_id)) {
593 sp->fcport->login_succ = 1;
596 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
597 qla24xx_sched_upd_fcport(sp->fcport);
598 spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
600 sp->fcport->login_retry = 0;
601 qla2x00_set_fcport_disc_state(sp->fcport,
603 sp->fcport->deleted = 0;
604 sp->fcport->logout_on_delete = 1;
609 sp->fcport->login_gen++;
610 sp->fcport->fw_login_state = DSC_LS_PORT_UNAVAIL;
611 qlt_logo_completion_handler(sp->fcport, MBS_COMMAND_COMPLETE);
614 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
616 kref_put(&sp->cmd_kref, qla2x00_sp_release);
619 int qla24xx_async_notify_ack(scsi_qla_host_t *vha, fc_port_t *fcport,
620 struct imm_ntfy_from_isp *ntfy, int type)
622 int rval = QLA_FUNCTION_FAILED;
626 fcport->flags |= FCF_ASYNC_SENT;
629 fcport->fw_login_state = DSC_LS_PLOGI_PEND;
631 if (vha->hw->flags.edif_enabled &&
632 (le16_to_cpu(ntfy->u.isp24.flags) & NOTIFY24XX_FLAGS_FCSP))
633 fcport->flags |= FCF_FCSP_DEVICE;
636 fcport->fw_login_state = DSC_LS_PRLI_PEND;
641 fcport->fw_login_state = DSC_LS_LOGO_PEND;
646 sp = qla2x00_get_sp(vha, fcport, GFP_ATOMIC);
652 qla2x00_init_async_sp(sp, qla2x00_get_async_timeout(vha) + 2,
653 qla2x00_async_nack_sp_done);
655 sp->u.iocb_cmd.u.nack.ntfy = ntfy;
657 ql_dbg(ql_dbg_disc, vha, 0x20f4,
658 "Async-%s %8phC hndl %x %s\n",
659 sp->name, fcport->port_name, sp->handle, c);
661 rval = qla2x00_start_sp(sp);
662 if (rval != QLA_SUCCESS)
668 kref_put(&sp->cmd_kref, qla2x00_sp_release);
670 fcport->flags &= ~FCF_ASYNC_SENT;
674 void qla24xx_do_nack_work(struct scsi_qla_host *vha, struct qla_work_evt *e)
678 switch (e->u.nack.type) {
680 t = e->u.nack.fcport;
681 flush_work(&t->del_work);
682 flush_work(&t->free_work);
683 mutex_lock(&vha->vha_tgt.tgt_mutex);
684 t = qlt_create_sess(vha, e->u.nack.fcport, 0);
685 mutex_unlock(&vha->vha_tgt.tgt_mutex);
687 ql_log(ql_log_info, vha, 0xd034,
688 "%s create sess success %p", __func__, t);
689 /* create sess has an extra kref */
690 vha->hw->tgt.tgt_ops->put_sess(e->u.nack.fcport);
694 qla24xx_async_notify_ack(vha, e->u.nack.fcport,
695 (struct imm_ntfy_from_isp *)e->u.nack.iocb, e->u.nack.type);
698 void qla24xx_delete_sess_fn(struct work_struct *work)
700 fc_port_t *fcport = container_of(work, struct fc_port, del_work);
701 struct qla_hw_data *ha = NULL;
703 if (!fcport || !fcport->vha || !fcport->vha->hw)
706 ha = fcport->vha->hw;
708 if (fcport->se_sess) {
709 ha->tgt.tgt_ops->shutdown_sess(fcport);
710 ha->tgt.tgt_ops->put_sess(fcport);
712 qlt_unreg_sess(fcport);
717 * Called from qla2x00_reg_remote_port()
719 void qlt_fc_port_added(struct scsi_qla_host *vha, fc_port_t *fcport)
721 struct qla_hw_data *ha = vha->hw;
722 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
723 struct fc_port *sess = fcport;
726 if (!vha->hw->tgt.tgt_ops)
729 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
731 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
735 if (fcport->disc_state == DSC_DELETE_PEND) {
736 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
740 if (!sess->se_sess) {
741 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
743 mutex_lock(&vha->vha_tgt.tgt_mutex);
744 sess = qlt_create_sess(vha, fcport, false);
745 mutex_unlock(&vha->vha_tgt.tgt_mutex);
747 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
749 if (fcport->fw_login_state == DSC_LS_PRLI_COMP) {
750 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
754 if (!kref_get_unless_zero(&sess->sess_kref)) {
755 ql_dbg(ql_dbg_disc, vha, 0x2107,
756 "%s: kref_get fail sess %8phC \n",
757 __func__, sess->port_name);
758 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
762 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04c,
763 "qla_target(%u): %ssession for port %8phC "
764 "(loop ID %d) reappeared\n", vha->vp_idx,
765 sess->local ? "local " : "", sess->port_name, sess->loop_id);
767 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf007,
768 "Reappeared sess %p\n", sess);
770 ha->tgt.tgt_ops->update_sess(sess, fcport->d_id,
772 (fcport->flags & FCF_CONF_COMP_SUPPORTED));
775 if (sess && sess->local) {
776 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04d,
777 "qla_target(%u): local session for "
778 "port %8phC (loop ID %d) became global\n", vha->vp_idx,
779 fcport->port_name, sess->loop_id);
782 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
784 ha->tgt.tgt_ops->put_sess(sess);
788 * This is a zero-base ref-counting solution, since hardware_lock
789 * guarantees that ref_count is not modified concurrently.
790 * Upon successful return content of iocb is undefined
792 static struct qlt_plogi_ack_t *
793 qlt_plogi_ack_find_add(struct scsi_qla_host *vha, port_id_t *id,
794 struct imm_ntfy_from_isp *iocb)
796 struct qlt_plogi_ack_t *pla;
798 lockdep_assert_held(&vha->hw->hardware_lock);
800 list_for_each_entry(pla, &vha->plogi_ack_list, list) {
801 if (pla->id.b24 == id->b24) {
802 ql_dbg(ql_dbg_disc + ql_dbg_verbose, vha, 0x210d,
803 "%s %d %8phC Term INOT due to new INOT",
805 pla->iocb.u.isp24.port_name);
806 qlt_send_term_imm_notif(vha, &pla->iocb, 1);
807 memcpy(&pla->iocb, iocb, sizeof(pla->iocb));
812 pla = kmem_cache_zalloc(qla_tgt_plogi_cachep, GFP_ATOMIC);
814 ql_dbg(ql_dbg_async, vha, 0x5088,
815 "qla_target(%d): Allocation of plogi_ack failed\n",
820 memcpy(&pla->iocb, iocb, sizeof(pla->iocb));
822 list_add_tail(&pla->list, &vha->plogi_ack_list);
827 void qlt_plogi_ack_unref(struct scsi_qla_host *vha,
828 struct qlt_plogi_ack_t *pla)
830 struct imm_ntfy_from_isp *iocb = &pla->iocb;
833 fc_port_t *fcport = pla->fcport;
835 BUG_ON(!pla->ref_count);
841 ql_dbg(ql_dbg_disc, vha, 0x5089,
842 "Sending PLOGI ACK to wwn %8phC s_id %02x:%02x:%02x loop_id %#04x"
843 " exch %#x ox_id %#x\n", iocb->u.isp24.port_name,
844 iocb->u.isp24.port_id[2], iocb->u.isp24.port_id[1],
845 iocb->u.isp24.port_id[0],
846 le16_to_cpu(iocb->u.isp24.nport_handle),
847 iocb->u.isp24.exchange_address, iocb->ox_id);
849 port_id.b.domain = iocb->u.isp24.port_id[2];
850 port_id.b.area = iocb->u.isp24.port_id[1];
851 port_id.b.al_pa = iocb->u.isp24.port_id[0];
852 port_id.b.rsvd_1 = 0;
854 loop_id = le16_to_cpu(iocb->u.isp24.nport_handle);
856 fcport->loop_id = loop_id;
857 fcport->d_id = port_id;
858 if (iocb->u.isp24.status_subcode == ELS_PLOGI)
859 qla24xx_post_nack_work(vha, fcport, iocb, SRB_NACK_PLOGI);
861 qla24xx_post_nack_work(vha, fcport, iocb, SRB_NACK_PRLI);
863 list_for_each_entry(fcport, &vha->vp_fcports, list) {
864 if (fcport->plogi_link[QLT_PLOGI_LINK_SAME_WWN] == pla)
865 fcport->plogi_link[QLT_PLOGI_LINK_SAME_WWN] = NULL;
866 if (fcport->plogi_link[QLT_PLOGI_LINK_CONFLICT] == pla)
867 fcport->plogi_link[QLT_PLOGI_LINK_CONFLICT] = NULL;
870 list_del(&pla->list);
871 kmem_cache_free(qla_tgt_plogi_cachep, pla);
875 qlt_plogi_ack_link(struct scsi_qla_host *vha, struct qlt_plogi_ack_t *pla,
876 struct fc_port *sess, enum qlt_plogi_link_t link)
878 struct imm_ntfy_from_isp *iocb = &pla->iocb;
879 /* Inc ref_count first because link might already be pointing at pla */
882 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf097,
883 "Linking sess %p [%d] wwn %8phC with PLOGI ACK to wwn %8phC"
884 " s_id %02x:%02x:%02x, ref=%d pla %p link %d\n",
885 sess, link, sess->port_name,
886 iocb->u.isp24.port_name, iocb->u.isp24.port_id[2],
887 iocb->u.isp24.port_id[1], iocb->u.isp24.port_id[0],
888 pla->ref_count, pla, link);
890 if (link == QLT_PLOGI_LINK_CONFLICT) {
891 switch (sess->disc_state) {
893 case DSC_DELETE_PEND:
901 if (sess->plogi_link[link])
902 qlt_plogi_ack_unref(vha, sess->plogi_link[link]);
904 if (link == QLT_PLOGI_LINK_SAME_WWN)
907 sess->plogi_link[link] = pla;
911 /* These fields must be initialized by the caller */
914 * number of cmds dropped while we were waiting for
915 * initiator to ack LOGO initialize to 1 if LOGO is
916 * triggered by a command, otherwise, to 0
920 /* These fields are used by callee */
921 struct list_head list;
925 qlt_send_first_logo(struct scsi_qla_host *vha, qlt_port_logo_t *logo)
927 qlt_port_logo_t *tmp;
930 if (test_bit(PFLG_DRIVER_REMOVING, &vha->pci_flags)) {
935 mutex_lock(&vha->vha_tgt.tgt_mutex);
937 list_for_each_entry(tmp, &vha->logo_list, list) {
938 if (tmp->id.b24 == logo->id.b24) {
939 tmp->cmd_count += logo->cmd_count;
940 mutex_unlock(&vha->vha_tgt.tgt_mutex);
945 list_add_tail(&logo->list, &vha->logo_list);
947 mutex_unlock(&vha->vha_tgt.tgt_mutex);
949 res = qla24xx_els_dcmd_iocb(vha, ELS_DCMD_LOGO, logo->id);
951 mutex_lock(&vha->vha_tgt.tgt_mutex);
952 list_del(&logo->list);
953 mutex_unlock(&vha->vha_tgt.tgt_mutex);
956 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf098,
957 "Finished LOGO to %02x:%02x:%02x, dropped %d cmds, res = %#x\n",
958 logo->id.b.domain, logo->id.b.area, logo->id.b.al_pa,
959 logo->cmd_count, res);
962 void qlt_free_session_done(struct work_struct *work)
964 struct fc_port *sess = container_of(work, struct fc_port,
966 struct qla_tgt *tgt = sess->tgt;
967 struct scsi_qla_host *vha = sess->vha;
968 struct qla_hw_data *ha = vha->hw;
970 bool logout_started = false;
971 scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
972 struct qlt_plogi_ack_t *own =
973 sess->plogi_link[QLT_PLOGI_LINK_SAME_WWN];
975 ql_dbg(ql_dbg_disc, vha, 0xf084,
976 "%s: se_sess %p / sess %p from port %8phC loop_id %#04x"
977 " s_id %02x:%02x:%02x logout %d keep %d els_logo %d\n",
978 __func__, sess->se_sess, sess, sess->port_name, sess->loop_id,
979 sess->d_id.b.domain, sess->d_id.b.area, sess->d_id.b.al_pa,
980 sess->logout_on_delete, sess->keep_nport_handle,
981 sess->send_els_logo);
983 if (!IS_SW_RESV_ADDR(sess->d_id)) {
984 if (ha->flags.edif_enabled &&
985 (!own || own->iocb.u.isp24.status_subcode == ELS_PLOGI)) {
986 sess->edif.authok = 0;
987 if (!ha->flags.host_shutting_down) {
988 ql_dbg(ql_dbg_edif, vha, 0x911e,
989 "%s wwpn %8phC calling qla2x00_release_all_sadb\n",
990 __func__, sess->port_name);
991 qla2x00_release_all_sadb(vha, sess);
993 ql_dbg(ql_dbg_edif, vha, 0x911e,
994 "%s bypassing release_all_sadb\n",
997 qla_edif_clear_appdata(vha, sess);
998 qla_edif_sess_down(vha, sess);
1000 qla2x00_mark_device_lost(vha, sess, 0);
1002 if (sess->send_els_logo) {
1003 qlt_port_logo_t logo;
1005 logo.id = sess->d_id;
1007 INIT_LIST_HEAD(&logo.list);
1009 qlt_send_first_logo(vha, &logo);
1010 sess->send_els_logo = 0;
1013 if (sess->logout_on_delete && sess->loop_id != FC_NO_LOOP_ID) {
1017 (own->iocb.u.isp24.status_subcode == ELS_PLOGI)) {
1018 sess->logout_completed = 0;
1019 rc = qla2x00_post_async_logout_work(vha, sess,
1021 if (rc != QLA_SUCCESS)
1022 ql_log(ql_log_warn, vha, 0xf085,
1023 "Schedule logo failed sess %p rc %d\n",
1026 logout_started = true;
1027 } else if (own && (own->iocb.u.isp24.status_subcode ==
1028 ELS_PRLI) && ha->flags.rida_fmt2) {
1029 rc = qla2x00_post_async_prlo_work(vha, sess,
1031 if (rc != QLA_SUCCESS)
1032 ql_log(ql_log_warn, vha, 0xf085,
1033 "Schedule PRLO failed sess %p rc %d\n",
1036 logout_started = true;
1038 } /* if sess->logout_on_delete */
1040 if (sess->nvme_flag & NVME_FLAG_REGISTERED &&
1041 !(sess->nvme_flag & NVME_FLAG_DELETING)) {
1042 sess->nvme_flag |= NVME_FLAG_DELETING;
1043 qla_nvme_unregister_remote_port(sess);
1048 * Release the target session for FC Nexus from fabric module code.
1050 if (sess->se_sess != NULL)
1051 ha->tgt.tgt_ops->free_session(sess);
1053 if (logout_started) {
1054 bool traced = false;
1057 while (!READ_ONCE(sess->logout_completed)) {
1059 ql_dbg(ql_dbg_disc, vha, 0xf086,
1060 "%s: waiting for sess %p logout\n",
1067 * Driver timeout is set to 22 Sec, update count value to loop
1068 * long enough for log-out to complete before advancing. Otherwise,
1069 * straddling logout can interfere with re-login attempt.
1075 ql_dbg(ql_dbg_disc, vha, 0xf087,
1076 "%s: sess %p logout completed\n", __func__, sess);
1079 if (sess->logo_ack_needed) {
1080 sess->logo_ack_needed = 0;
1081 qla24xx_async_notify_ack(vha, sess,
1082 (struct imm_ntfy_from_isp *)sess->iocb, SRB_NACK_LOGO);
1085 spin_lock_irqsave(&vha->work_lock, flags);
1086 sess->flags &= ~FCF_ASYNC_SENT;
1087 spin_unlock_irqrestore(&vha->work_lock, flags);
1089 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
1090 if (sess->se_sess) {
1091 sess->se_sess = NULL;
1092 if (tgt && !IS_SW_RESV_ADDR(sess->d_id))
1096 qla2x00_set_fcport_disc_state(sess, DSC_DELETED);
1097 sess->fw_login_state = DSC_LS_PORT_UNAVAIL;
1098 sess->deleted = QLA_SESS_DELETED;
1100 if (sess->login_succ && !IS_SW_RESV_ADDR(sess->d_id)) {
1101 vha->fcport_count--;
1102 sess->login_succ = 0;
1105 qla2x00_clear_loop_id(sess);
1107 if (sess->conflict) {
1108 sess->conflict->login_pause = 0;
1109 sess->conflict = NULL;
1110 if (!test_bit(UNLOADING, &vha->dpc_flags))
1111 set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
1115 struct qlt_plogi_ack_t *con =
1116 sess->plogi_link[QLT_PLOGI_LINK_CONFLICT];
1117 struct imm_ntfy_from_isp *iocb;
1119 own = sess->plogi_link[QLT_PLOGI_LINK_SAME_WWN];
1123 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf099,
1124 "se_sess %p / sess %p port %8phC is gone,"
1125 " %s (ref=%d), releasing PLOGI for %8phC (ref=%d)\n",
1126 sess->se_sess, sess, sess->port_name,
1127 own ? "releasing own PLOGI" : "no own PLOGI pending",
1128 own ? own->ref_count : -1,
1129 iocb->u.isp24.port_name, con->ref_count);
1130 qlt_plogi_ack_unref(vha, con);
1131 sess->plogi_link[QLT_PLOGI_LINK_CONFLICT] = NULL;
1133 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf09a,
1134 "se_sess %p / sess %p port %8phC is gone, %s (ref=%d)\n",
1135 sess->se_sess, sess, sess->port_name,
1136 own ? "releasing own PLOGI" :
1137 "no own PLOGI pending",
1138 own ? own->ref_count : -1);
1142 sess->fw_login_state = DSC_LS_PLOGI_PEND;
1143 qlt_plogi_ack_unref(vha, own);
1144 sess->plogi_link[QLT_PLOGI_LINK_SAME_WWN] = NULL;
1148 sess->explicit_logout = 0;
1149 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
1150 sess->free_pending = 0;
1152 qla2x00_dfs_remove_rport(vha, sess);
1154 ql_dbg(ql_dbg_disc, vha, 0xf001,
1155 "Unregistration of sess %p %8phC finished fcp_cnt %d\n",
1156 sess, sess->port_name, vha->fcport_count);
1158 if (tgt && (tgt->sess_count == 0))
1159 wake_up_all(&tgt->waitQ);
1161 if (!test_bit(PFLG_DRIVER_REMOVING, &base_vha->pci_flags) &&
1162 !(vha->vp_idx && test_bit(VPORT_DELETE, &vha->dpc_flags)) &&
1163 (!tgt || !tgt->tgt_stop) && !LOOP_TRANSITION(vha)) {
1164 switch (vha->host->active_mode) {
1165 case MODE_INITIATOR:
1167 set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
1168 qla2xxx_wake_dpc(vha);
1177 if (vha->fcport_count == 0)
1178 wake_up_all(&vha->fcport_waitQ);
1181 /* ha->tgt.sess_lock supposed to be held on entry */
1182 void qlt_unreg_sess(struct fc_port *sess)
1184 struct scsi_qla_host *vha = sess->vha;
1185 unsigned long flags;
1187 ql_dbg(ql_dbg_disc, sess->vha, 0x210a,
1188 "%s sess %p for deletion %8phC\n",
1189 __func__, sess, sess->port_name);
1191 spin_lock_irqsave(&sess->vha->work_lock, flags);
1192 if (sess->free_pending) {
1193 spin_unlock_irqrestore(&sess->vha->work_lock, flags);
1196 sess->free_pending = 1;
1198 * Use FCF_ASYNC_SENT flag to block other cmds used in sess
1199 * management from being sent.
1201 sess->flags |= FCF_ASYNC_SENT;
1202 spin_unlock_irqrestore(&sess->vha->work_lock, flags);
1205 vha->hw->tgt.tgt_ops->clear_nacl_from_fcport_map(sess);
1207 sess->deleted = QLA_SESS_DELETION_IN_PROGRESS;
1208 qla2x00_set_fcport_disc_state(sess, DSC_DELETE_PEND);
1209 sess->last_rscn_gen = sess->rscn_gen;
1210 sess->last_login_gen = sess->login_gen;
1212 queue_work(sess->vha->hw->wq, &sess->free_work);
1214 EXPORT_SYMBOL(qlt_unreg_sess);
1216 static int qlt_reset(struct scsi_qla_host *vha, void *iocb, int mcmd)
1218 struct qla_hw_data *ha = vha->hw;
1219 struct fc_port *sess = NULL;
1222 struct imm_ntfy_from_isp *n = (struct imm_ntfy_from_isp *)iocb;
1223 unsigned long flags;
1225 loop_id = le16_to_cpu(n->u.isp24.nport_handle);
1226 if (loop_id == 0xFFFF) {
1228 atomic_inc(&vha->vha_tgt.qla_tgt->tgt_global_resets_count);
1229 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
1230 qlt_clear_tgt_db(vha->vha_tgt.qla_tgt);
1231 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
1233 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
1234 sess = ha->tgt.tgt_ops->find_sess_by_loop_id(vha, loop_id);
1235 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
1238 ql_dbg(ql_dbg_tgt, vha, 0xe000,
1239 "Using sess for qla_tgt_reset: %p\n", sess);
1245 ql_dbg(ql_dbg_tgt, vha, 0xe047,
1246 "scsi(%ld): resetting (session %p from port %8phC mcmd %x, "
1247 "loop_id %d)\n", vha->host_no, sess, sess->port_name,
1250 return qlt_issue_task_mgmt(sess, 0, mcmd, iocb, QLA24XX_MGMT_SEND_NACK);
1253 static void qla24xx_chk_fcp_state(struct fc_port *sess)
1255 if (sess->chip_reset != sess->vha->hw->base_qpair->chip_reset) {
1256 sess->logout_on_delete = 0;
1257 sess->logo_ack_needed = 0;
1258 sess->fw_login_state = DSC_LS_PORT_UNAVAIL;
1262 void qlt_schedule_sess_for_deletion(struct fc_port *sess)
1264 struct qla_tgt *tgt = sess->tgt;
1265 unsigned long flags;
1268 switch (sess->disc_state) {
1269 case DSC_DELETE_PEND:
1272 if (!sess->plogi_link[QLT_PLOGI_LINK_SAME_WWN] &&
1273 !sess->plogi_link[QLT_PLOGI_LINK_CONFLICT]) {
1274 if (tgt && tgt->tgt_stop && tgt->sess_count == 0)
1275 wake_up_all(&tgt->waitQ);
1277 if (sess->vha->fcport_count == 0)
1278 wake_up_all(&sess->vha->fcport_waitQ);
1282 case DSC_UPD_FCPORT:
1284 * This port is not done reporting to upper layer.
1287 sess->next_disc_state = DSC_DELETE_PEND;
1288 sec = jiffies_to_msecs(jiffies -
1289 sess->jiffies_at_registration)/1000;
1290 if (sess->sec_since_registration < sec && sec && !(sec % 5)) {
1291 sess->sec_since_registration = sec;
1292 ql_dbg(ql_dbg_disc, sess->vha, 0xffff,
1293 "%s %8phC : Slow Rport registration(%d Sec)\n",
1294 __func__, sess->port_name, sec);
1301 spin_lock_irqsave(&sess->vha->work_lock, flags);
1302 if (sess->deleted == QLA_SESS_DELETION_IN_PROGRESS) {
1303 spin_unlock_irqrestore(&sess->vha->work_lock, flags);
1306 sess->deleted = QLA_SESS_DELETION_IN_PROGRESS;
1307 spin_unlock_irqrestore(&sess->vha->work_lock, flags);
1309 sess->prli_pend_timer = 0;
1310 qla2x00_set_fcport_disc_state(sess, DSC_DELETE_PEND);
1312 qla24xx_chk_fcp_state(sess);
1314 ql_dbg(ql_log_warn, sess->vha, 0xe001,
1315 "Scheduling sess %p for deletion %8phC fc4_type %x\n",
1316 sess, sess->port_name, sess->fc4_type);
1318 WARN_ON(!queue_work(sess->vha->hw->wq, &sess->del_work));
1321 static void qlt_clear_tgt_db(struct qla_tgt *tgt)
1323 struct fc_port *sess;
1324 scsi_qla_host_t *vha = tgt->vha;
1326 list_for_each_entry(sess, &vha->vp_fcports, list) {
1328 qlt_schedule_sess_for_deletion(sess);
1331 /* At this point tgt could be already dead */
1334 static int qla24xx_get_loop_id(struct scsi_qla_host *vha, be_id_t s_id,
1337 struct qla_hw_data *ha = vha->hw;
1338 dma_addr_t gid_list_dma;
1339 struct gid_list_info *gid_list, *gid;
1343 gid_list = dma_alloc_coherent(&ha->pdev->dev, qla2x00_gid_list_size(ha),
1344 &gid_list_dma, GFP_KERNEL);
1346 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf044,
1347 "qla_target(%d): DMA Alloc failed of %u\n",
1348 vha->vp_idx, qla2x00_gid_list_size(ha));
1352 /* Get list of logged in devices */
1353 rc = qla24xx_gidlist_wait(vha, gid_list, gid_list_dma, &entries);
1354 if (rc != QLA_SUCCESS) {
1355 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf045,
1356 "qla_target(%d): get_id_list() failed: %x\n",
1359 goto out_free_id_list;
1364 for (i = 0; i < entries; i++) {
1365 if (gid->al_pa == s_id.al_pa &&
1366 gid->area == s_id.area &&
1367 gid->domain == s_id.domain) {
1368 *loop_id = le16_to_cpu(gid->loop_id);
1372 gid = (void *)gid + ha->gid_list_info_size;
1376 dma_free_coherent(&ha->pdev->dev, qla2x00_gid_list_size(ha),
1377 gid_list, gid_list_dma);
1382 * Adds an extra ref to allow to drop hw lock after adding sess to the list.
1383 * Caller must put it.
1385 static struct fc_port *qlt_create_sess(
1386 struct scsi_qla_host *vha,
1390 struct qla_hw_data *ha = vha->hw;
1391 struct fc_port *sess = fcport;
1392 unsigned long flags;
1394 if (vha->vha_tgt.qla_tgt->tgt_stop)
1397 if (fcport->se_sess) {
1398 if (!kref_get_unless_zero(&sess->sess_kref)) {
1399 ql_dbg(ql_dbg_disc, vha, 0x20f6,
1400 "%s: kref_get_unless_zero failed for %8phC\n",
1401 __func__, sess->port_name);
1406 sess->tgt = vha->vha_tgt.qla_tgt;
1407 sess->local = local;
1410 * Under normal circumstances we want to logout from firmware when
1411 * session eventually ends and release corresponding nport handle.
1412 * In the exception cases (e.g. when new PLOGI is waiting) corresponding
1413 * code will adjust these flags as necessary.
1415 sess->logout_on_delete = 1;
1416 sess->keep_nport_handle = 0;
1417 sess->logout_completed = 0;
1419 if (ha->tgt.tgt_ops->check_initiator_node_acl(vha,
1420 &fcport->port_name[0], sess) < 0) {
1421 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf015,
1422 "(%d) %8phC check_initiator_node_acl failed\n",
1423 vha->vp_idx, fcport->port_name);
1426 kref_init(&fcport->sess_kref);
1428 * Take an extra reference to ->sess_kref here to handle
1429 * fc_port access across ->tgt.sess_lock reaquire.
1431 if (!kref_get_unless_zero(&sess->sess_kref)) {
1432 ql_dbg(ql_dbg_disc, vha, 0x20f7,
1433 "%s: kref_get_unless_zero failed for %8phC\n",
1434 __func__, sess->port_name);
1438 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
1439 if (!IS_SW_RESV_ADDR(sess->d_id))
1440 vha->vha_tgt.qla_tgt->sess_count++;
1442 qlt_do_generation_tick(vha, &sess->generation);
1443 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
1446 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf006,
1447 "Adding sess %p se_sess %p to tgt %p sess_count %d\n",
1448 sess, sess->se_sess, vha->vha_tgt.qla_tgt,
1449 vha->vha_tgt.qla_tgt->sess_count);
1451 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04b,
1452 "qla_target(%d): %ssession for wwn %8phC (loop_id %d, "
1453 "s_id %x:%x:%x, confirmed completion %ssupported) added\n",
1454 vha->vp_idx, local ? "local " : "", fcport->port_name,
1455 fcport->loop_id, sess->d_id.b.domain, sess->d_id.b.area,
1456 sess->d_id.b.al_pa, sess->conf_compl_supported ? "" : "not ");
1462 * max_gen - specifies maximum session generation
1463 * at which this deletion requestion is still valid
1466 qlt_fc_port_deleted(struct scsi_qla_host *vha, fc_port_t *fcport, int max_gen)
1468 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
1469 struct fc_port *sess = fcport;
1470 unsigned long flags;
1472 if (!vha->hw->tgt.tgt_ops)
1478 spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
1479 if (tgt->tgt_stop) {
1480 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
1483 if (!sess->se_sess) {
1484 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
1488 if (max_gen - sess->generation < 0) {
1489 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
1490 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf092,
1491 "Ignoring stale deletion request for se_sess %p / sess %p"
1492 " for port %8phC, req_gen %d, sess_gen %d\n",
1493 sess->se_sess, sess, sess->port_name, max_gen,
1498 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf008, "qla_tgt_fc_port_deleted %p", sess);
1501 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
1502 qlt_schedule_sess_for_deletion(sess);
1505 static inline int test_tgt_sess_count(struct qla_tgt *tgt)
1507 struct qla_hw_data *ha = tgt->ha;
1508 unsigned long flags;
1511 * We need to protect against race, when tgt is freed before or
1514 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
1515 ql_dbg(ql_dbg_tgt, tgt->vha, 0xe002,
1516 "tgt %p, sess_count=%d\n",
1517 tgt, tgt->sess_count);
1518 res = (tgt->sess_count == 0);
1519 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
1524 /* Called by tcm_qla2xxx configfs code */
1525 int qlt_stop_phase1(struct qla_tgt *tgt)
1527 struct scsi_qla_host *vha = tgt->vha;
1528 struct qla_hw_data *ha = tgt->ha;
1529 unsigned long flags;
1531 mutex_lock(&ha->optrom_mutex);
1532 mutex_lock(&qla_tgt_mutex);
1534 if (tgt->tgt_stop || tgt->tgt_stopped) {
1535 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04e,
1536 "Already in tgt->tgt_stop or tgt_stopped state\n");
1537 mutex_unlock(&qla_tgt_mutex);
1538 mutex_unlock(&ha->optrom_mutex);
1542 ql_dbg(ql_dbg_tgt_mgt, vha, 0xe003, "Stopping target for host %ld(%p)\n",
1545 * Mutex needed to sync with qla_tgt_fc_port_[added,deleted].
1546 * Lock is needed, because we still can get an incoming packet.
1548 mutex_lock(&vha->vha_tgt.tgt_mutex);
1550 qlt_clear_tgt_db(tgt);
1551 mutex_unlock(&vha->vha_tgt.tgt_mutex);
1552 mutex_unlock(&qla_tgt_mutex);
1554 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf009,
1555 "Waiting for sess works (tgt %p)", tgt);
1556 spin_lock_irqsave(&tgt->sess_work_lock, flags);
1557 while (!list_empty(&tgt->sess_works_list)) {
1558 spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
1559 flush_scheduled_work();
1560 spin_lock_irqsave(&tgt->sess_work_lock, flags);
1562 spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
1564 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00a,
1565 "Waiting for tgt %p: sess_count=%d\n", tgt, tgt->sess_count);
1567 wait_event_timeout(tgt->waitQ, test_tgt_sess_count(tgt), 10*HZ);
1570 if (!ha->flags.host_shutting_down &&
1571 (qla_tgt_mode_enabled(vha) || qla_dual_mode_enabled(vha)))
1572 qlt_disable_vha(vha);
1574 /* Wait for sessions to clear out (just in case) */
1575 wait_event_timeout(tgt->waitQ, test_tgt_sess_count(tgt), 10*HZ);
1576 mutex_unlock(&ha->optrom_mutex);
1580 EXPORT_SYMBOL(qlt_stop_phase1);
1582 /* Called by tcm_qla2xxx configfs code */
1583 void qlt_stop_phase2(struct qla_tgt *tgt)
1585 scsi_qla_host_t *vha = tgt->vha;
1587 if (tgt->tgt_stopped) {
1588 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04f,
1589 "Already in tgt->tgt_stopped state\n");
1593 if (!tgt->tgt_stop) {
1594 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00b,
1595 "%s: phase1 stop is not completed\n", __func__);
1600 mutex_lock(&tgt->ha->optrom_mutex);
1601 mutex_lock(&vha->vha_tgt.tgt_mutex);
1603 tgt->tgt_stopped = 1;
1604 mutex_unlock(&vha->vha_tgt.tgt_mutex);
1605 mutex_unlock(&tgt->ha->optrom_mutex);
1607 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00c, "Stop of tgt %p finished\n",
1610 switch (vha->qlini_mode) {
1611 case QLA2XXX_INI_MODE_EXCLUSIVE:
1612 vha->flags.online = 1;
1613 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
1619 EXPORT_SYMBOL(qlt_stop_phase2);
1621 /* Called from qlt_remove_target() -> qla2x00_remove_one() */
1622 static void qlt_release(struct qla_tgt *tgt)
1624 scsi_qla_host_t *vha = tgt->vha;
1628 struct qla_qpair_hint *h;
1629 struct qla_hw_data *ha = vha->hw;
1631 if (!tgt->tgt_stop && !tgt->tgt_stopped)
1632 qlt_stop_phase1(tgt);
1634 if (!tgt->tgt_stopped)
1635 qlt_stop_phase2(tgt);
1637 for (i = 0; i < vha->hw->max_qpairs + 1; i++) {
1638 unsigned long flags;
1640 h = &tgt->qphints[i];
1642 spin_lock_irqsave(h->qpair->qp_lock_ptr, flags);
1643 list_del(&h->hint_elem);
1644 spin_unlock_irqrestore(h->qpair->qp_lock_ptr, flags);
1648 kfree(tgt->qphints);
1649 mutex_lock(&qla_tgt_mutex);
1650 list_del(&vha->vha_tgt.qla_tgt->tgt_list_entry);
1651 mutex_unlock(&qla_tgt_mutex);
1653 btree_for_each_safe64(&tgt->lun_qpair_map, key, node)
1654 btree_remove64(&tgt->lun_qpair_map, key);
1656 btree_destroy64(&tgt->lun_qpair_map);
1659 if (ha->tgt.tgt_ops &&
1660 ha->tgt.tgt_ops->remove_target &&
1661 vha->vha_tgt.target_lport_ptr)
1662 ha->tgt.tgt_ops->remove_target(vha);
1664 vha->vha_tgt.qla_tgt = NULL;
1666 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00d,
1667 "Release of tgt %p finished\n", tgt);
1672 /* ha->hardware_lock supposed to be held on entry */
1673 static int qlt_sched_sess_work(struct qla_tgt *tgt, int type,
1674 const void *param, unsigned int param_size)
1676 struct qla_tgt_sess_work_param *prm;
1677 unsigned long flags;
1679 prm = kzalloc(sizeof(*prm), GFP_ATOMIC);
1681 ql_dbg(ql_dbg_tgt_mgt, tgt->vha, 0xf050,
1682 "qla_target(%d): Unable to create session "
1683 "work, command will be refused", 0);
1687 ql_dbg(ql_dbg_tgt_mgt, tgt->vha, 0xf00e,
1688 "Scheduling work (type %d, prm %p)"
1689 " to find session for param %p (size %d, tgt %p)\n",
1690 type, prm, param, param_size, tgt);
1693 memcpy(&prm->tm_iocb, param, param_size);
1695 spin_lock_irqsave(&tgt->sess_work_lock, flags);
1696 list_add_tail(&prm->sess_works_list_entry, &tgt->sess_works_list);
1697 spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
1699 schedule_work(&tgt->sess_work);
1705 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
1707 static void qlt_send_notify_ack(struct qla_qpair *qpair,
1708 struct imm_ntfy_from_isp *ntfy,
1709 uint32_t add_flags, uint16_t resp_code, int resp_code_valid,
1710 uint16_t srr_flags, uint16_t srr_reject_code, uint8_t srr_explan)
1712 struct scsi_qla_host *vha = qpair->vha;
1713 struct qla_hw_data *ha = vha->hw;
1715 struct nack_to_isp *nack;
1717 if (!ha->flags.fw_started)
1720 ql_dbg(ql_dbg_tgt, vha, 0xe004, "Sending NOTIFY_ACK (ha=%p)\n", ha);
1722 pkt = (request_t *)__qla2x00_alloc_iocbs(qpair, NULL);
1724 ql_dbg(ql_dbg_tgt, vha, 0xe049,
1725 "qla_target(%d): %s failed: unable to allocate "
1726 "request packet\n", vha->vp_idx, __func__);
1730 if (vha->vha_tgt.qla_tgt != NULL)
1731 vha->vha_tgt.qla_tgt->notify_ack_expected++;
1733 pkt->entry_type = NOTIFY_ACK_TYPE;
1734 pkt->entry_count = 1;
1736 nack = (struct nack_to_isp *)pkt;
1737 nack->ox_id = ntfy->ox_id;
1739 nack->u.isp24.handle = QLA_TGT_SKIP_HANDLE;
1740 nack->u.isp24.nport_handle = ntfy->u.isp24.nport_handle;
1741 if (le16_to_cpu(ntfy->u.isp24.status) == IMM_NTFY_ELS) {
1742 nack->u.isp24.flags = ntfy->u.isp24.flags &
1743 cpu_to_le16(NOTIFY24XX_FLAGS_PUREX_IOCB);
1745 nack->u.isp24.srr_rx_id = ntfy->u.isp24.srr_rx_id;
1746 nack->u.isp24.status = ntfy->u.isp24.status;
1747 nack->u.isp24.status_subcode = ntfy->u.isp24.status_subcode;
1748 nack->u.isp24.fw_handle = ntfy->u.isp24.fw_handle;
1749 nack->u.isp24.exchange_address = ntfy->u.isp24.exchange_address;
1750 nack->u.isp24.srr_rel_offs = ntfy->u.isp24.srr_rel_offs;
1751 nack->u.isp24.srr_ui = ntfy->u.isp24.srr_ui;
1752 nack->u.isp24.srr_flags = cpu_to_le16(srr_flags);
1753 nack->u.isp24.srr_reject_code = srr_reject_code;
1754 nack->u.isp24.srr_reject_code_expl = srr_explan;
1755 nack->u.isp24.vp_index = ntfy->u.isp24.vp_index;
1757 /* TODO qualify this with EDIF enable */
1758 if (ntfy->u.isp24.status_subcode == ELS_PLOGI &&
1759 (le16_to_cpu(ntfy->u.isp24.flags) & NOTIFY24XX_FLAGS_FCSP)) {
1760 nack->u.isp24.flags |= cpu_to_le16(NOTIFY_ACK_FLAGS_FCSP);
1763 ql_dbg(ql_dbg_tgt, vha, 0xe005,
1764 "qla_target(%d): Sending 24xx Notify Ack %d\n",
1765 vha->vp_idx, nack->u.isp24.status);
1767 /* Memory Barrier */
1769 qla2x00_start_iocbs(vha, qpair->req);
1772 static int qlt_build_abts_resp_iocb(struct qla_tgt_mgmt_cmd *mcmd)
1774 struct scsi_qla_host *vha = mcmd->vha;
1775 struct qla_hw_data *ha = vha->hw;
1776 struct abts_resp_to_24xx *resp;
1781 struct abts_recv_from_24xx *abts = &mcmd->orig_iocb.abts;
1782 struct qla_qpair *qpair = mcmd->qpair;
1784 ql_dbg(ql_dbg_tgt, vha, 0xe006,
1785 "Sending task mgmt ABTS response (ha=%p, status=%x)\n",
1786 ha, mcmd->fc_tm_rsp);
1788 rc = qlt_check_reserve_free_req(qpair, 1);
1790 ql_dbg(ql_dbg_tgt, vha, 0xe04a,
1791 "qla_target(%d): %s failed: unable to allocate request packet\n",
1792 vha->vp_idx, __func__);
1796 resp = (struct abts_resp_to_24xx *)qpair->req->ring_ptr;
1797 memset(resp, 0, sizeof(*resp));
1799 h = qlt_make_handle(qpair);
1800 if (unlikely(h == QLA_TGT_NULL_HANDLE)) {
1802 * CTIO type 7 from the firmware doesn't provide a way to
1803 * know the initiator's LOOP ID, hence we can't find
1804 * the session and, so, the command.
1808 qpair->req->outstanding_cmds[h] = (srb_t *)mcmd;
1811 resp->handle = make_handle(qpair->req->id, h);
1812 resp->entry_type = ABTS_RESP_24XX;
1813 resp->entry_count = 1;
1814 resp->nport_handle = abts->nport_handle;
1815 resp->vp_index = vha->vp_idx;
1816 resp->sof_type = abts->sof_type;
1817 resp->exchange_address = abts->exchange_address;
1818 resp->fcp_hdr_le = abts->fcp_hdr_le;
1819 f_ctl = cpu_to_le32(F_CTL_EXCH_CONTEXT_RESP |
1820 F_CTL_LAST_SEQ | F_CTL_END_SEQ |
1821 F_CTL_SEQ_INITIATIVE);
1822 p = (uint8_t *)&f_ctl;
1823 resp->fcp_hdr_le.f_ctl[0] = *p++;
1824 resp->fcp_hdr_le.f_ctl[1] = *p++;
1825 resp->fcp_hdr_le.f_ctl[2] = *p;
1827 resp->fcp_hdr_le.d_id = abts->fcp_hdr_le.s_id;
1828 resp->fcp_hdr_le.s_id = abts->fcp_hdr_le.d_id;
1830 resp->exchange_addr_to_abort = abts->exchange_addr_to_abort;
1831 if (mcmd->fc_tm_rsp == FCP_TMF_CMPL) {
1832 resp->fcp_hdr_le.r_ctl = R_CTL_BASIC_LINK_SERV | R_CTL_B_ACC;
1833 resp->payload.ba_acct.seq_id_valid = SEQ_ID_INVALID;
1834 resp->payload.ba_acct.low_seq_cnt = 0x0000;
1835 resp->payload.ba_acct.high_seq_cnt = cpu_to_le16(0xFFFF);
1836 resp->payload.ba_acct.ox_id = abts->fcp_hdr_le.ox_id;
1837 resp->payload.ba_acct.rx_id = abts->fcp_hdr_le.rx_id;
1839 resp->fcp_hdr_le.r_ctl = R_CTL_BASIC_LINK_SERV | R_CTL_B_RJT;
1840 resp->payload.ba_rjt.reason_code =
1841 BA_RJT_REASON_CODE_UNABLE_TO_PERFORM;
1842 /* Other bytes are zero */
1845 vha->vha_tgt.qla_tgt->abts_resp_expected++;
1847 /* Memory Barrier */
1849 if (qpair->reqq_start_iocbs)
1850 qpair->reqq_start_iocbs(qpair);
1852 qla2x00_start_iocbs(vha, qpair->req);
1858 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
1860 static void qlt_24xx_send_abts_resp(struct qla_qpair *qpair,
1861 struct abts_recv_from_24xx *abts, uint32_t status,
1864 struct scsi_qla_host *vha = qpair->vha;
1865 struct qla_hw_data *ha = vha->hw;
1866 struct abts_resp_to_24xx *resp;
1870 ql_dbg(ql_dbg_tgt, vha, 0xe006,
1871 "Sending task mgmt ABTS response (ha=%p, atio=%p, status=%x\n",
1874 resp = (struct abts_resp_to_24xx *)qla2x00_alloc_iocbs_ready(qpair,
1877 ql_dbg(ql_dbg_tgt, vha, 0xe04a,
1878 "qla_target(%d): %s failed: unable to allocate "
1879 "request packet", vha->vp_idx, __func__);
1883 resp->entry_type = ABTS_RESP_24XX;
1884 resp->handle = QLA_TGT_SKIP_HANDLE;
1885 resp->entry_count = 1;
1886 resp->nport_handle = abts->nport_handle;
1887 resp->vp_index = vha->vp_idx;
1888 resp->sof_type = abts->sof_type;
1889 resp->exchange_address = abts->exchange_address;
1890 resp->fcp_hdr_le = abts->fcp_hdr_le;
1891 f_ctl = cpu_to_le32(F_CTL_EXCH_CONTEXT_RESP |
1892 F_CTL_LAST_SEQ | F_CTL_END_SEQ |
1893 F_CTL_SEQ_INITIATIVE);
1894 p = (uint8_t *)&f_ctl;
1895 resp->fcp_hdr_le.f_ctl[0] = *p++;
1896 resp->fcp_hdr_le.f_ctl[1] = *p++;
1897 resp->fcp_hdr_le.f_ctl[2] = *p;
1899 resp->fcp_hdr_le.d_id = abts->fcp_hdr_le.d_id;
1900 resp->fcp_hdr_le.s_id = abts->fcp_hdr_le.s_id;
1902 resp->fcp_hdr_le.d_id = abts->fcp_hdr_le.s_id;
1903 resp->fcp_hdr_le.s_id = abts->fcp_hdr_le.d_id;
1905 resp->exchange_addr_to_abort = abts->exchange_addr_to_abort;
1906 if (status == FCP_TMF_CMPL) {
1907 resp->fcp_hdr_le.r_ctl = R_CTL_BASIC_LINK_SERV | R_CTL_B_ACC;
1908 resp->payload.ba_acct.seq_id_valid = SEQ_ID_INVALID;
1909 resp->payload.ba_acct.low_seq_cnt = 0x0000;
1910 resp->payload.ba_acct.high_seq_cnt = cpu_to_le16(0xFFFF);
1911 resp->payload.ba_acct.ox_id = abts->fcp_hdr_le.ox_id;
1912 resp->payload.ba_acct.rx_id = abts->fcp_hdr_le.rx_id;
1914 resp->fcp_hdr_le.r_ctl = R_CTL_BASIC_LINK_SERV | R_CTL_B_RJT;
1915 resp->payload.ba_rjt.reason_code =
1916 BA_RJT_REASON_CODE_UNABLE_TO_PERFORM;
1917 /* Other bytes are zero */
1920 vha->vha_tgt.qla_tgt->abts_resp_expected++;
1922 /* Memory Barrier */
1924 if (qpair->reqq_start_iocbs)
1925 qpair->reqq_start_iocbs(qpair);
1927 qla2x00_start_iocbs(vha, qpair->req);
1931 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
1933 static void qlt_24xx_retry_term_exchange(struct scsi_qla_host *vha,
1934 struct qla_qpair *qpair, response_t *pkt, struct qla_tgt_mgmt_cmd *mcmd)
1936 struct ctio7_to_24xx *ctio;
1938 struct abts_recv_from_24xx *entry;
1940 ctio = (struct ctio7_to_24xx *)qla2x00_alloc_iocbs_ready(qpair, NULL);
1942 ql_dbg(ql_dbg_tgt, vha, 0xe04b,
1943 "qla_target(%d): %s failed: unable to allocate "
1944 "request packet\n", vha->vp_idx, __func__);
1949 /* abts from remote port */
1950 entry = &mcmd->orig_iocb.abts;
1952 /* abts from this driver. */
1953 entry = (struct abts_recv_from_24xx *)pkt;
1956 * We've got on entrance firmware's response on by us generated
1957 * ABTS response. So, in it ID fields are reversed.
1960 ctio->entry_type = CTIO_TYPE7;
1961 ctio->entry_count = 1;
1962 ctio->nport_handle = entry->nport_handle;
1963 ctio->handle = QLA_TGT_SKIP_HANDLE | CTIO_COMPLETION_HANDLE_MARK;
1964 ctio->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
1965 ctio->vp_index = vha->vp_idx;
1966 ctio->exchange_addr = entry->exchange_addr_to_abort;
1967 tmp = (CTIO7_FLAGS_STATUS_MODE_1 | CTIO7_FLAGS_TERMINATE);
1970 ctio->initiator_id = entry->fcp_hdr_le.s_id;
1972 if (mcmd->flags & QLA24XX_MGMT_ABORT_IO_ATTR_VALID)
1973 tmp |= (mcmd->abort_io_attr << 9);
1974 else if (qpair->retry_term_cnt & 1)
1977 ctio->initiator_id = entry->fcp_hdr_le.d_id;
1979 if (qpair->retry_term_cnt & 1)
1982 ctio->u.status1.flags = cpu_to_le16(tmp);
1983 ctio->u.status1.ox_id = entry->fcp_hdr_le.ox_id;
1985 ql_dbg(ql_dbg_tgt, vha, 0xe007,
1986 "Sending retry TERM EXCH CTIO7 flags %04xh oxid %04xh attr valid %x\n",
1987 le16_to_cpu(ctio->u.status1.flags),
1988 le16_to_cpu(ctio->u.status1.ox_id),
1989 (mcmd && mcmd->flags & QLA24XX_MGMT_ABORT_IO_ATTR_VALID) ? 1 : 0);
1991 /* Memory Barrier */
1993 if (qpair->reqq_start_iocbs)
1994 qpair->reqq_start_iocbs(qpair);
1996 qla2x00_start_iocbs(vha, qpair->req);
1999 qlt_build_abts_resp_iocb(mcmd);
2001 qlt_24xx_send_abts_resp(qpair,
2002 (struct abts_recv_from_24xx *)entry, FCP_TMF_CMPL, true);
2006 /* drop cmds for the given lun
2007 * XXX only looks for cmds on the port through which lun reset was recieved
2008 * XXX does not go through the list of other port (which may have cmds
2011 static void abort_cmds_for_lun(struct scsi_qla_host *vha, u64 lun, be_id_t s_id)
2013 struct qla_tgt_sess_op *op;
2014 struct qla_tgt_cmd *cmd;
2016 unsigned long flags;
2018 key = sid_to_key(s_id);
2019 spin_lock_irqsave(&vha->cmd_list_lock, flags);
2020 list_for_each_entry(op, &vha->unknown_atio_list, cmd_list) {
2024 op_key = sid_to_key(op->atio.u.isp24.fcp_hdr.s_id);
2025 op_lun = scsilun_to_int(
2026 (struct scsi_lun *)&op->atio.u.isp24.fcp_cmnd.lun);
2027 if (op_key == key && op_lun == lun)
2031 list_for_each_entry(cmd, &vha->qla_cmd_list, cmd_list) {
2035 cmd_key = sid_to_key(cmd->atio.u.isp24.fcp_hdr.s_id);
2036 cmd_lun = scsilun_to_int(
2037 (struct scsi_lun *)&cmd->atio.u.isp24.fcp_cmnd.lun);
2038 if (cmd_key == key && cmd_lun == lun)
2041 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
2044 static struct qla_qpair_hint *qlt_find_qphint(struct scsi_qla_host *vha,
2045 uint64_t unpacked_lun)
2047 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
2048 struct qla_qpair_hint *h = NULL;
2050 if (vha->flags.qpairs_available) {
2051 h = btree_lookup64(&tgt->lun_qpair_map, unpacked_lun);
2053 h = &tgt->qphints[0];
2055 h = &tgt->qphints[0];
2061 static void qlt_do_tmr_work(struct work_struct *work)
2063 struct qla_tgt_mgmt_cmd *mcmd =
2064 container_of(work, struct qla_tgt_mgmt_cmd, work);
2065 struct qla_hw_data *ha = mcmd->vha->hw;
2068 unsigned long flags;
2070 switch (mcmd->tmr_func) {
2072 tag = le32_to_cpu(mcmd->orig_iocb.abts.exchange_addr_to_abort);
2079 rc = ha->tgt.tgt_ops->handle_tmr(mcmd, mcmd->unpacked_lun,
2080 mcmd->tmr_func, tag);
2083 spin_lock_irqsave(mcmd->qpair->qp_lock_ptr, flags);
2084 switch (mcmd->tmr_func) {
2086 mcmd->fc_tm_rsp = FCP_TMF_REJECTED;
2087 qlt_build_abts_resp_iocb(mcmd);
2089 case QLA_TGT_LUN_RESET:
2090 case QLA_TGT_CLEAR_TS:
2091 case QLA_TGT_ABORT_TS:
2092 case QLA_TGT_CLEAR_ACA:
2093 case QLA_TGT_TARGET_RESET:
2094 qlt_send_busy(mcmd->qpair, &mcmd->orig_iocb.atio,
2098 case QLA_TGT_ABORT_ALL:
2099 case QLA_TGT_NEXUS_LOSS_SESS:
2100 case QLA_TGT_NEXUS_LOSS:
2101 qlt_send_notify_ack(mcmd->qpair,
2102 &mcmd->orig_iocb.imm_ntfy, 0, 0, 0, 0, 0, 0);
2105 spin_unlock_irqrestore(mcmd->qpair->qp_lock_ptr, flags);
2107 ql_dbg(ql_dbg_tgt_mgt, mcmd->vha, 0xf052,
2108 "qla_target(%d): tgt_ops->handle_tmr() failed: %d\n",
2109 mcmd->vha->vp_idx, rc);
2110 mempool_free(mcmd, qla_tgt_mgmt_cmd_mempool);
2114 /* ha->hardware_lock supposed to be held on entry */
2115 static int __qlt_24xx_handle_abts(struct scsi_qla_host *vha,
2116 struct abts_recv_from_24xx *abts, struct fc_port *sess)
2118 struct qla_hw_data *ha = vha->hw;
2119 struct qla_tgt_mgmt_cmd *mcmd;
2120 struct qla_qpair_hint *h = &vha->vha_tgt.qla_tgt->qphints[0];
2121 struct qla_tgt_cmd *abort_cmd;
2123 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00f,
2124 "qla_target(%d): task abort (tag=%d)\n",
2125 vha->vp_idx, abts->exchange_addr_to_abort);
2127 mcmd = mempool_alloc(qla_tgt_mgmt_cmd_mempool, GFP_ATOMIC);
2129 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf051,
2130 "qla_target(%d): %s: Allocation of ABORT cmd failed",
2131 vha->vp_idx, __func__);
2134 memset(mcmd, 0, sizeof(*mcmd));
2135 mcmd->cmd_type = TYPE_TGT_TMCMD;
2137 memcpy(&mcmd->orig_iocb.abts, abts, sizeof(mcmd->orig_iocb.abts));
2138 mcmd->reset_count = ha->base_qpair->chip_reset;
2139 mcmd->tmr_func = QLA_TGT_ABTS;
2140 mcmd->qpair = h->qpair;
2144 * LUN is looked up by target-core internally based on the passed
2145 * abts->exchange_addr_to_abort tag.
2147 mcmd->se_cmd.cpuid = h->cpuid;
2149 abort_cmd = ha->tgt.tgt_ops->find_cmd_by_tag(sess,
2150 le32_to_cpu(abts->exchange_addr_to_abort));
2153 mcmd->unpacked_lun = abort_cmd->se_cmd.orig_fe_lun;
2155 if (abort_cmd->qpair) {
2156 mcmd->qpair = abort_cmd->qpair;
2157 mcmd->se_cmd.cpuid = abort_cmd->se_cmd.cpuid;
2158 mcmd->abort_io_attr = abort_cmd->atio.u.isp24.attr;
2159 mcmd->flags = QLA24XX_MGMT_ABORT_IO_ATTR_VALID;
2162 INIT_WORK(&mcmd->work, qlt_do_tmr_work);
2163 queue_work_on(mcmd->se_cmd.cpuid, qla_tgt_wq, &mcmd->work);
2169 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
2171 static void qlt_24xx_handle_abts(struct scsi_qla_host *vha,
2172 struct abts_recv_from_24xx *abts)
2174 struct qla_hw_data *ha = vha->hw;
2175 struct fc_port *sess;
2176 uint32_t tag = le32_to_cpu(abts->exchange_addr_to_abort);
2179 unsigned long flags;
2181 if (le32_to_cpu(abts->fcp_hdr_le.parameter) & ABTS_PARAM_ABORT_SEQ) {
2182 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf053,
2183 "qla_target(%d): ABTS: Abort Sequence not "
2184 "supported\n", vha->vp_idx);
2185 qlt_24xx_send_abts_resp(ha->base_qpair, abts, FCP_TMF_REJECTED,
2190 if (tag == ATIO_EXCHANGE_ADDRESS_UNKNOWN) {
2191 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf010,
2192 "qla_target(%d): ABTS: Unknown Exchange "
2193 "Address received\n", vha->vp_idx);
2194 qlt_24xx_send_abts_resp(ha->base_qpair, abts, FCP_TMF_REJECTED,
2199 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf011,
2200 "qla_target(%d): task abort (s_id=%x:%x:%x, "
2201 "tag=%d, param=%x)\n", vha->vp_idx, abts->fcp_hdr_le.s_id.domain,
2202 abts->fcp_hdr_le.s_id.area, abts->fcp_hdr_le.s_id.al_pa, tag,
2203 le32_to_cpu(abts->fcp_hdr_le.parameter));
2205 s_id = le_id_to_be(abts->fcp_hdr_le.s_id);
2207 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
2208 sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha, s_id);
2210 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf012,
2211 "qla_target(%d): task abort for non-existent session\n",
2213 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
2215 qlt_24xx_send_abts_resp(ha->base_qpair, abts, FCP_TMF_REJECTED,
2219 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
2222 if (sess->deleted) {
2223 qlt_24xx_send_abts_resp(ha->base_qpair, abts, FCP_TMF_REJECTED,
2228 rc = __qlt_24xx_handle_abts(vha, abts, sess);
2230 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf054,
2231 "qla_target(%d): __qlt_24xx_handle_abts() failed: %d\n",
2233 qlt_24xx_send_abts_resp(ha->base_qpair, abts, FCP_TMF_REJECTED,
2240 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
2242 static void qlt_24xx_send_task_mgmt_ctio(struct qla_qpair *qpair,
2243 struct qla_tgt_mgmt_cmd *mcmd, uint32_t resp_code)
2245 struct scsi_qla_host *ha = mcmd->vha;
2246 struct atio_from_isp *atio = &mcmd->orig_iocb.atio;
2247 struct ctio7_to_24xx *ctio;
2250 ql_dbg(ql_dbg_tgt, ha, 0xe008,
2251 "Sending task mgmt CTIO7 (ha=%p, atio=%p, resp_code=%x\n",
2252 ha, atio, resp_code);
2255 ctio = (struct ctio7_to_24xx *)__qla2x00_alloc_iocbs(qpair, NULL);
2257 ql_dbg(ql_dbg_tgt, ha, 0xe04c,
2258 "qla_target(%d): %s failed: unable to allocate "
2259 "request packet\n", ha->vp_idx, __func__);
2263 ctio->entry_type = CTIO_TYPE7;
2264 ctio->entry_count = 1;
2265 ctio->handle = QLA_TGT_SKIP_HANDLE | CTIO_COMPLETION_HANDLE_MARK;
2266 ctio->nport_handle = cpu_to_le16(mcmd->sess->loop_id);
2267 ctio->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
2268 ctio->vp_index = ha->vp_idx;
2269 ctio->initiator_id = be_id_to_le(atio->u.isp24.fcp_hdr.s_id);
2270 ctio->exchange_addr = atio->u.isp24.exchange_addr;
2271 temp = (atio->u.isp24.attr << 9)|
2272 CTIO7_FLAGS_STATUS_MODE_1 | CTIO7_FLAGS_SEND_STATUS;
2273 ctio->u.status1.flags = cpu_to_le16(temp);
2274 temp = be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id);
2275 ctio->u.status1.ox_id = cpu_to_le16(temp);
2276 ctio->u.status1.scsi_status =
2277 cpu_to_le16(SS_RESPONSE_INFO_LEN_VALID);
2278 ctio->u.status1.response_len = cpu_to_le16(8);
2279 ctio->u.status1.sense_data[0] = resp_code;
2281 /* Memory Barrier */
2283 if (qpair->reqq_start_iocbs)
2284 qpair->reqq_start_iocbs(qpair);
2286 qla2x00_start_iocbs(ha, qpair->req);
2289 void qlt_free_mcmd(struct qla_tgt_mgmt_cmd *mcmd)
2291 mempool_free(mcmd, qla_tgt_mgmt_cmd_mempool);
2293 EXPORT_SYMBOL(qlt_free_mcmd);
2296 * ha->hardware_lock supposed to be held on entry. Might drop it, then
2299 void qlt_send_resp_ctio(struct qla_qpair *qpair, struct qla_tgt_cmd *cmd,
2300 uint8_t scsi_status, uint8_t sense_key, uint8_t asc, uint8_t ascq)
2302 struct atio_from_isp *atio = &cmd->atio;
2303 struct ctio7_to_24xx *ctio;
2305 struct scsi_qla_host *vha = cmd->vha;
2307 ql_dbg(ql_dbg_tgt_dif, vha, 0x3066,
2308 "Sending response CTIO7 (vha=%p, atio=%p, scsi_status=%02x, "
2309 "sense_key=%02x, asc=%02x, ascq=%02x",
2310 vha, atio, scsi_status, sense_key, asc, ascq);
2312 ctio = (struct ctio7_to_24xx *)qla2x00_alloc_iocbs(vha, NULL);
2314 ql_dbg(ql_dbg_async, vha, 0x3067,
2315 "qla2x00t(%ld): %s failed: unable to allocate request packet",
2316 vha->host_no, __func__);
2320 ctio->entry_type = CTIO_TYPE7;
2321 ctio->entry_count = 1;
2322 ctio->handle = QLA_TGT_SKIP_HANDLE;
2323 ctio->nport_handle = cpu_to_le16(cmd->sess->loop_id);
2324 ctio->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
2325 ctio->vp_index = vha->vp_idx;
2326 ctio->initiator_id = be_id_to_le(atio->u.isp24.fcp_hdr.s_id);
2327 ctio->exchange_addr = atio->u.isp24.exchange_addr;
2328 temp = (atio->u.isp24.attr << 9) |
2329 CTIO7_FLAGS_STATUS_MODE_1 | CTIO7_FLAGS_SEND_STATUS;
2330 ctio->u.status1.flags = cpu_to_le16(temp);
2331 temp = be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id);
2332 ctio->u.status1.ox_id = cpu_to_le16(temp);
2333 ctio->u.status1.scsi_status =
2334 cpu_to_le16(SS_RESPONSE_INFO_LEN_VALID | scsi_status);
2335 ctio->u.status1.response_len = cpu_to_le16(18);
2336 ctio->u.status1.residual = cpu_to_le32(get_datalen_for_atio(atio));
2338 if (ctio->u.status1.residual != 0)
2339 ctio->u.status1.scsi_status |=
2340 cpu_to_le16(SS_RESIDUAL_UNDER);
2342 /* Fixed format sense data. */
2343 ctio->u.status1.sense_data[0] = 0x70;
2344 ctio->u.status1.sense_data[2] = sense_key;
2345 /* Additional sense length */
2346 ctio->u.status1.sense_data[7] = 0xa;
2348 ctio->u.status1.sense_data[12] = asc;
2349 ctio->u.status1.sense_data[13] = ascq;
2351 /* Memory Barrier */
2354 if (qpair->reqq_start_iocbs)
2355 qpair->reqq_start_iocbs(qpair);
2357 qla2x00_start_iocbs(vha, qpair->req);
2363 /* callback from target fabric module code */
2364 void qlt_xmit_tm_rsp(struct qla_tgt_mgmt_cmd *mcmd)
2366 struct scsi_qla_host *vha = mcmd->sess->vha;
2367 struct qla_hw_data *ha = vha->hw;
2368 unsigned long flags;
2369 struct qla_qpair *qpair = mcmd->qpair;
2370 bool free_mcmd = true;
2372 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf013,
2373 "TM response mcmd (%p) status %#x state %#x",
2374 mcmd, mcmd->fc_tm_rsp, mcmd->flags);
2376 spin_lock_irqsave(qpair->qp_lock_ptr, flags);
2378 if (!vha->flags.online || mcmd->reset_count != qpair->chip_reset) {
2380 * Either the port is not online or this request was from
2381 * previous life, just abort the processing.
2383 ql_dbg(ql_dbg_async, vha, 0xe100,
2384 "RESET-TMR online/active/old-count/new-count = %d/%d/%d/%d.\n",
2385 vha->flags.online, qla2x00_reset_active(vha),
2386 mcmd->reset_count, qpair->chip_reset);
2387 ha->tgt.tgt_ops->free_mcmd(mcmd);
2388 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
2392 if (mcmd->flags == QLA24XX_MGMT_SEND_NACK) {
2393 switch (mcmd->orig_iocb.imm_ntfy.u.isp24.status_subcode) {
2397 ql_dbg(ql_dbg_disc, vha, 0x2106,
2398 "TM response logo %8phC status %#x state %#x",
2399 mcmd->sess->port_name, mcmd->fc_tm_rsp,
2401 qlt_schedule_sess_for_deletion(mcmd->sess);
2404 qlt_send_notify_ack(vha->hw->base_qpair,
2405 &mcmd->orig_iocb.imm_ntfy, 0, 0, 0, 0, 0, 0);
2409 if (mcmd->orig_iocb.atio.u.raw.entry_type == ABTS_RECV_24XX) {
2410 qlt_build_abts_resp_iocb(mcmd);
2413 qlt_24xx_send_task_mgmt_ctio(qpair, mcmd,
2417 * Make the callback for ->free_mcmd() to queue_work() and invoke
2418 * target_put_sess_cmd() to drop cmd_kref to 1. The final
2419 * target_put_sess_cmd() call will be made from TFO->check_stop_free()
2420 * -> tcm_qla2xxx_check_stop_free() to release the TMR associated se_cmd
2421 * descriptor after TFO->queue_tm_rsp() -> tcm_qla2xxx_queue_tm_rsp() ->
2422 * qlt_xmit_tm_rsp() returns here..
2425 ha->tgt.tgt_ops->free_mcmd(mcmd);
2427 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
2429 EXPORT_SYMBOL(qlt_xmit_tm_rsp);
2432 static int qlt_pci_map_calc_cnt(struct qla_tgt_prm *prm)
2434 struct qla_tgt_cmd *cmd = prm->cmd;
2436 BUG_ON(cmd->sg_cnt == 0);
2438 prm->sg = (struct scatterlist *)cmd->sg;
2439 prm->seg_cnt = dma_map_sg(&cmd->qpair->pdev->dev, cmd->sg,
2440 cmd->sg_cnt, cmd->dma_data_direction);
2441 if (unlikely(prm->seg_cnt == 0))
2444 prm->cmd->sg_mapped = 1;
2446 if (cmd->se_cmd.prot_op == TARGET_PROT_NORMAL) {
2448 * If greater than four sg entries then we need to allocate
2449 * the continuation entries
2451 if (prm->seg_cnt > QLA_TGT_DATASEGS_PER_CMD_24XX)
2452 prm->req_cnt += DIV_ROUND_UP(prm->seg_cnt -
2453 QLA_TGT_DATASEGS_PER_CMD_24XX,
2454 QLA_TGT_DATASEGS_PER_CONT_24XX);
2457 if ((cmd->se_cmd.prot_op == TARGET_PROT_DIN_INSERT) ||
2458 (cmd->se_cmd.prot_op == TARGET_PROT_DOUT_STRIP)) {
2459 prm->seg_cnt = DIV_ROUND_UP(cmd->bufflen, cmd->blk_sz);
2460 prm->tot_dsds = prm->seg_cnt;
2462 prm->tot_dsds = prm->seg_cnt;
2464 if (cmd->prot_sg_cnt) {
2465 prm->prot_sg = cmd->prot_sg;
2466 prm->prot_seg_cnt = dma_map_sg(&cmd->qpair->pdev->dev,
2467 cmd->prot_sg, cmd->prot_sg_cnt,
2468 cmd->dma_data_direction);
2469 if (unlikely(prm->prot_seg_cnt == 0))
2472 if ((cmd->se_cmd.prot_op == TARGET_PROT_DIN_INSERT) ||
2473 (cmd->se_cmd.prot_op == TARGET_PROT_DOUT_STRIP)) {
2474 /* Dif Bundling not support here */
2475 prm->prot_seg_cnt = DIV_ROUND_UP(cmd->bufflen,
2477 prm->tot_dsds += prm->prot_seg_cnt;
2479 prm->tot_dsds += prm->prot_seg_cnt;
2486 ql_dbg_qp(ql_dbg_tgt, prm->cmd->qpair, 0xe04d,
2487 "qla_target(%d): PCI mapping failed: sg_cnt=%d",
2488 0, prm->cmd->sg_cnt);
2492 static void qlt_unmap_sg(struct scsi_qla_host *vha, struct qla_tgt_cmd *cmd)
2494 struct qla_hw_data *ha;
2495 struct qla_qpair *qpair;
2497 if (!cmd->sg_mapped)
2502 dma_unmap_sg(&qpair->pdev->dev, cmd->sg, cmd->sg_cnt,
2503 cmd->dma_data_direction);
2506 if (cmd->prot_sg_cnt)
2507 dma_unmap_sg(&qpair->pdev->dev, cmd->prot_sg, cmd->prot_sg_cnt,
2508 cmd->dma_data_direction);
2513 if (cmd->ctx_dsd_alloced)
2514 qla2x00_clean_dsd_pool(ha, cmd->ctx);
2516 dma_pool_free(ha->dl_dma_pool, cmd->ctx, cmd->ctx->crc_ctx_dma);
2519 static int qlt_check_reserve_free_req(struct qla_qpair *qpair,
2523 struct req_que *req = qpair->req;
2525 if (req->cnt < (req_cnt + 2)) {
2526 cnt = (uint16_t)(qpair->use_shadow_reg ? *req->out_ptr :
2527 rd_reg_dword_relaxed(req->req_q_out));
2529 if (req->ring_index < cnt)
2530 req->cnt = cnt - req->ring_index;
2532 req->cnt = req->length - (req->ring_index - cnt);
2534 if (unlikely(req->cnt < (req_cnt + 2)))
2538 req->cnt -= req_cnt;
2544 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
2546 static inline void *qlt_get_req_pkt(struct req_que *req)
2548 /* Adjust ring index. */
2550 if (req->ring_index == req->length) {
2551 req->ring_index = 0;
2552 req->ring_ptr = req->ring;
2556 return (cont_entry_t *)req->ring_ptr;
2559 /* ha->hardware_lock supposed to be held on entry */
2560 static inline uint32_t qlt_make_handle(struct qla_qpair *qpair)
2565 struct req_que *req = qpair->req;
2567 h = req->current_outstanding_cmd;
2569 for (index = 1; index < req->num_outstanding_cmds; index++) {
2571 if (h == req->num_outstanding_cmds)
2574 if (h == QLA_TGT_SKIP_HANDLE)
2577 if (!req->outstanding_cmds[h]) {
2584 req->current_outstanding_cmd = h;
2586 ql_dbg(ql_dbg_io, qpair->vha, 0x305b,
2587 "qla_target(%d): Ran out of empty cmd slots\n",
2588 qpair->vha->vp_idx);
2589 h = QLA_TGT_NULL_HANDLE;
2595 /* ha->hardware_lock supposed to be held on entry */
2596 static int qlt_24xx_build_ctio_pkt(struct qla_qpair *qpair,
2597 struct qla_tgt_prm *prm)
2600 struct ctio7_to_24xx *pkt;
2601 struct atio_from_isp *atio = &prm->cmd->atio;
2603 struct qla_tgt_cmd *cmd = prm->cmd;
2605 pkt = (struct ctio7_to_24xx *)qpair->req->ring_ptr;
2607 memset(pkt, 0, sizeof(*pkt));
2609 pkt->entry_type = CTIO_TYPE7;
2610 pkt->entry_count = (uint8_t)prm->req_cnt;
2611 pkt->vp_index = prm->cmd->vp_idx;
2613 h = qlt_make_handle(qpair);
2614 if (unlikely(h == QLA_TGT_NULL_HANDLE)) {
2616 * CTIO type 7 from the firmware doesn't provide a way to
2617 * know the initiator's LOOP ID, hence we can't find
2618 * the session and, so, the command.
2622 qpair->req->outstanding_cmds[h] = (srb_t *)prm->cmd;
2624 pkt->handle = make_handle(qpair->req->id, h);
2625 pkt->handle |= CTIO_COMPLETION_HANDLE_MARK;
2626 pkt->nport_handle = cpu_to_le16(prm->cmd->loop_id);
2627 pkt->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
2628 pkt->initiator_id = be_id_to_le(atio->u.isp24.fcp_hdr.s_id);
2629 pkt->exchange_addr = atio->u.isp24.exchange_addr;
2630 temp = atio->u.isp24.attr << 9;
2631 pkt->u.status0.flags |= cpu_to_le16(temp);
2632 temp = be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id);
2633 pkt->u.status0.ox_id = cpu_to_le16(temp);
2634 pkt->u.status0.relative_offset = cpu_to_le32(prm->cmd->offset);
2637 if (cmd->dma_data_direction == DMA_TO_DEVICE)
2638 prm->cmd->sess->edif.rx_bytes += cmd->bufflen;
2639 if (cmd->dma_data_direction == DMA_FROM_DEVICE)
2640 prm->cmd->sess->edif.tx_bytes += cmd->bufflen;
2642 pkt->u.status0.edif_flags |= EF_EN_EDIF;
2649 * ha->hardware_lock supposed to be held on entry. We have already made sure
2650 * that there is sufficient amount of request entries to not drop it.
2652 static void qlt_load_cont_data_segments(struct qla_tgt_prm *prm)
2655 struct dsd64 *cur_dsd;
2657 /* Build continuation packets */
2658 while (prm->seg_cnt > 0) {
2659 cont_a64_entry_t *cont_pkt64 =
2660 (cont_a64_entry_t *)qlt_get_req_pkt(
2661 prm->cmd->qpair->req);
2664 * Make sure that from cont_pkt64 none of
2665 * 64-bit specific fields used for 32-bit
2666 * addressing. Cast to (cont_entry_t *) for
2670 memset(cont_pkt64, 0, sizeof(*cont_pkt64));
2672 cont_pkt64->entry_count = 1;
2673 cont_pkt64->sys_define = 0;
2675 cont_pkt64->entry_type = CONTINUE_A64_TYPE;
2676 cur_dsd = cont_pkt64->dsd;
2678 /* Load continuation entry data segments */
2680 cnt < QLA_TGT_DATASEGS_PER_CONT_24XX && prm->seg_cnt;
2681 cnt++, prm->seg_cnt--) {
2682 append_dsd64(&cur_dsd, prm->sg);
2683 prm->sg = sg_next(prm->sg);
2689 * ha->hardware_lock supposed to be held on entry. We have already made sure
2690 * that there is sufficient amount of request entries to not drop it.
2692 static void qlt_load_data_segments(struct qla_tgt_prm *prm)
2695 struct dsd64 *cur_dsd;
2696 struct ctio7_to_24xx *pkt24 = (struct ctio7_to_24xx *)prm->pkt;
2698 pkt24->u.status0.transfer_length = cpu_to_le32(prm->cmd->bufflen);
2700 /* Setup packet address segment pointer */
2701 cur_dsd = &pkt24->u.status0.dsd;
2703 /* Set total data segment count */
2705 pkt24->dseg_count = cpu_to_le16(prm->seg_cnt);
2707 if (prm->seg_cnt == 0) {
2708 /* No data transfer */
2709 cur_dsd->address = 0;
2710 cur_dsd->length = 0;
2714 /* If scatter gather */
2716 /* Load command entry data segments */
2718 (cnt < QLA_TGT_DATASEGS_PER_CMD_24XX) && prm->seg_cnt;
2719 cnt++, prm->seg_cnt--) {
2720 append_dsd64(&cur_dsd, prm->sg);
2721 prm->sg = sg_next(prm->sg);
2724 qlt_load_cont_data_segments(prm);
2727 static inline int qlt_has_data(struct qla_tgt_cmd *cmd)
2729 return cmd->bufflen > 0;
2732 static void qlt_print_dif_err(struct qla_tgt_prm *prm)
2734 struct qla_tgt_cmd *cmd;
2735 struct scsi_qla_host *vha;
2737 /* asc 0x10=dif error */
2738 if (prm->sense_buffer && (prm->sense_buffer[12] == 0x10)) {
2742 switch (prm->sense_buffer[13]) {
2744 ql_dbg(ql_dbg_tgt_dif, vha, 0xe00b,
2745 "BE detected Guard TAG ERR: lba[0x%llx|%lld] len[0x%x] "
2746 "se_cmd=%p tag[%x]",
2747 cmd->lba, cmd->lba, cmd->num_blks, &cmd->se_cmd,
2748 cmd->atio.u.isp24.exchange_addr);
2751 ql_dbg(ql_dbg_tgt_dif, vha, 0xe00c,
2752 "BE detected APP TAG ERR: lba[0x%llx|%lld] len[0x%x] "
2753 "se_cmd=%p tag[%x]",
2754 cmd->lba, cmd->lba, cmd->num_blks, &cmd->se_cmd,
2755 cmd->atio.u.isp24.exchange_addr);
2758 ql_dbg(ql_dbg_tgt_dif, vha, 0xe00f,
2759 "BE detected REF TAG ERR: lba[0x%llx|%lld] len[0x%x] "
2760 "se_cmd=%p tag[%x]",
2761 cmd->lba, cmd->lba, cmd->num_blks, &cmd->se_cmd,
2762 cmd->atio.u.isp24.exchange_addr);
2765 ql_dbg(ql_dbg_tgt_dif, vha, 0xe010,
2766 "BE detected Dif ERR: lba[%llx|%lld] len[%x] "
2767 "se_cmd=%p tag[%x]",
2768 cmd->lba, cmd->lba, cmd->num_blks, &cmd->se_cmd,
2769 cmd->atio.u.isp24.exchange_addr);
2772 ql_dump_buffer(ql_dbg_tgt_dif, vha, 0xe011, cmd->cdb, 16);
2777 * Called without ha->hardware_lock held
2779 static int qlt_pre_xmit_response(struct qla_tgt_cmd *cmd,
2780 struct qla_tgt_prm *prm, int xmit_type, uint8_t scsi_status,
2781 uint32_t *full_req_cnt)
2783 struct se_cmd *se_cmd = &cmd->se_cmd;
2784 struct qla_qpair *qpair = cmd->qpair;
2787 prm->tgt = cmd->tgt;
2789 prm->rq_result = scsi_status;
2790 prm->sense_buffer = &cmd->sense_buffer[0];
2791 prm->sense_buffer_len = TRANSPORT_SENSE_BUFFER;
2796 prm->add_status_pkt = 0;
2797 prm->prot_sg = NULL;
2798 prm->prot_seg_cnt = 0;
2801 if ((xmit_type & QLA_TGT_XMIT_DATA) && qlt_has_data(cmd)) {
2802 if (qlt_pci_map_calc_cnt(prm) != 0)
2806 *full_req_cnt = prm->req_cnt;
2808 if (se_cmd->se_cmd_flags & SCF_UNDERFLOW_BIT) {
2809 prm->residual = se_cmd->residual_count;
2810 ql_dbg_qp(ql_dbg_io + ql_dbg_verbose, qpair, 0x305c,
2811 "Residual underflow: %d (tag %lld, op %x, bufflen %d, rq_result %x)\n",
2812 prm->residual, se_cmd->tag,
2813 se_cmd->t_task_cdb ? se_cmd->t_task_cdb[0] : 0,
2814 cmd->bufflen, prm->rq_result);
2815 prm->rq_result |= SS_RESIDUAL_UNDER;
2816 } else if (se_cmd->se_cmd_flags & SCF_OVERFLOW_BIT) {
2817 prm->residual = se_cmd->residual_count;
2818 ql_dbg_qp(ql_dbg_io, qpair, 0x305d,
2819 "Residual overflow: %d (tag %lld, op %x, bufflen %d, rq_result %x)\n",
2820 prm->residual, se_cmd->tag, se_cmd->t_task_cdb ?
2821 se_cmd->t_task_cdb[0] : 0, cmd->bufflen, prm->rq_result);
2822 prm->rq_result |= SS_RESIDUAL_OVER;
2825 if (xmit_type & QLA_TGT_XMIT_STATUS) {
2827 * If QLA_TGT_XMIT_DATA is not set, add_status_pkt will be
2828 * ignored in *xmit_response() below
2830 if (qlt_has_data(cmd)) {
2831 if (QLA_TGT_SENSE_VALID(prm->sense_buffer) ||
2832 (IS_FWI2_CAPABLE(cmd->vha->hw) &&
2833 (prm->rq_result != 0))) {
2834 prm->add_status_pkt = 1;
2843 static inline int qlt_need_explicit_conf(struct qla_tgt_cmd *cmd,
2846 if (cmd->qpair->enable_class_2)
2850 return cmd->conf_compl_supported;
2852 return cmd->qpair->enable_explicit_conf &&
2853 cmd->conf_compl_supported;
2856 static void qlt_24xx_init_ctio_to_isp(struct ctio7_to_24xx *ctio,
2857 struct qla_tgt_prm *prm)
2859 prm->sense_buffer_len = min_t(uint32_t, prm->sense_buffer_len,
2860 (uint32_t)sizeof(ctio->u.status1.sense_data));
2861 ctio->u.status0.flags |= cpu_to_le16(CTIO7_FLAGS_SEND_STATUS);
2862 if (qlt_need_explicit_conf(prm->cmd, 0)) {
2863 ctio->u.status0.flags |= cpu_to_le16(
2864 CTIO7_FLAGS_EXPLICIT_CONFORM |
2865 CTIO7_FLAGS_CONFORM_REQ);
2867 ctio->u.status0.residual = cpu_to_le32(prm->residual);
2868 ctio->u.status0.scsi_status = cpu_to_le16(prm->rq_result);
2869 if (QLA_TGT_SENSE_VALID(prm->sense_buffer)) {
2872 if (qlt_need_explicit_conf(prm->cmd, 1)) {
2873 if ((prm->rq_result & SS_SCSI_STATUS_BYTE) != 0) {
2874 ql_dbg_qp(ql_dbg_tgt, prm->cmd->qpair, 0xe017,
2875 "Skipping EXPLICIT_CONFORM and "
2876 "CTIO7_FLAGS_CONFORM_REQ for FCP READ w/ "
2877 "non GOOD status\n");
2878 goto skip_explict_conf;
2880 ctio->u.status1.flags |= cpu_to_le16(
2881 CTIO7_FLAGS_EXPLICIT_CONFORM |
2882 CTIO7_FLAGS_CONFORM_REQ);
2885 ctio->u.status1.flags &=
2886 ~cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_0);
2887 ctio->u.status1.flags |=
2888 cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_1);
2889 ctio->u.status1.scsi_status |=
2890 cpu_to_le16(SS_SENSE_LEN_VALID);
2891 ctio->u.status1.sense_length =
2892 cpu_to_le16(prm->sense_buffer_len);
2893 for (i = 0; i < prm->sense_buffer_len/4; i++) {
2896 v = get_unaligned_be32(
2897 &((uint32_t *)prm->sense_buffer)[i]);
2898 put_unaligned_le32(v,
2899 &((uint32_t *)ctio->u.status1.sense_data)[i]);
2901 qlt_print_dif_err(prm);
2904 ctio->u.status1.flags &=
2905 ~cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_0);
2906 ctio->u.status1.flags |=
2907 cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_1);
2908 ctio->u.status1.sense_length = 0;
2909 memset(ctio->u.status1.sense_data, 0,
2910 sizeof(ctio->u.status1.sense_data));
2913 /* Sense with len > 24, is it possible ??? */
2917 qlt_hba_err_chk_enabled(struct se_cmd *se_cmd)
2919 switch (se_cmd->prot_op) {
2920 case TARGET_PROT_DOUT_INSERT:
2921 case TARGET_PROT_DIN_STRIP:
2922 if (ql2xenablehba_err_chk >= 1)
2925 case TARGET_PROT_DOUT_PASS:
2926 case TARGET_PROT_DIN_PASS:
2927 if (ql2xenablehba_err_chk >= 2)
2930 case TARGET_PROT_DIN_INSERT:
2931 case TARGET_PROT_DOUT_STRIP:
2940 qla_tgt_ref_mask_check(struct se_cmd *se_cmd)
2942 switch (se_cmd->prot_op) {
2943 case TARGET_PROT_DIN_INSERT:
2944 case TARGET_PROT_DOUT_INSERT:
2945 case TARGET_PROT_DIN_STRIP:
2946 case TARGET_PROT_DOUT_STRIP:
2947 case TARGET_PROT_DIN_PASS:
2948 case TARGET_PROT_DOUT_PASS:
2957 * qla_tgt_set_dif_tags - Extract Ref and App tags from SCSI command
2960 qla_tgt_set_dif_tags(struct qla_tgt_cmd *cmd, struct crc_context *ctx,
2961 uint16_t *pfw_prot_opts)
2963 struct se_cmd *se_cmd = &cmd->se_cmd;
2964 uint32_t lba = 0xffffffff & se_cmd->t_task_lba;
2965 scsi_qla_host_t *vha = cmd->tgt->vha;
2966 struct qla_hw_data *ha = vha->hw;
2970 * wait till Mode Sense/Select cmd, modepage Ah, subpage 2
2971 * have been immplemented by TCM, before AppTag is avail.
2972 * Look for modesense_handlers[]
2975 ctx->app_tag_mask[0] = 0x0;
2976 ctx->app_tag_mask[1] = 0x0;
2978 if (IS_PI_UNINIT_CAPABLE(ha)) {
2979 if ((se_cmd->prot_type == TARGET_DIF_TYPE1_PROT) ||
2980 (se_cmd->prot_type == TARGET_DIF_TYPE2_PROT))
2981 *pfw_prot_opts |= PO_DIS_VALD_APP_ESC;
2982 else if (se_cmd->prot_type == TARGET_DIF_TYPE3_PROT)
2983 *pfw_prot_opts |= PO_DIS_VALD_APP_REF_ESC;
2986 t32 = ha->tgt.tgt_ops->get_dif_tags(cmd, pfw_prot_opts);
2988 switch (se_cmd->prot_type) {
2989 case TARGET_DIF_TYPE0_PROT:
2991 * No check for ql2xenablehba_err_chk, as it
2992 * would be an I/O error if hba tag generation
2995 ctx->ref_tag = cpu_to_le32(lba);
2996 /* enable ALL bytes of the ref tag */
2997 ctx->ref_tag_mask[0] = 0xff;
2998 ctx->ref_tag_mask[1] = 0xff;
2999 ctx->ref_tag_mask[2] = 0xff;
3000 ctx->ref_tag_mask[3] = 0xff;
3002 case TARGET_DIF_TYPE1_PROT:
3004 * For TYPE 1 protection: 16 bit GUARD tag, 32 bit
3005 * REF tag, and 16 bit app tag.
3007 ctx->ref_tag = cpu_to_le32(lba);
3008 if (!qla_tgt_ref_mask_check(se_cmd) ||
3009 !(ha->tgt.tgt_ops->chk_dif_tags(t32))) {
3010 *pfw_prot_opts |= PO_DIS_REF_TAG_VALD;
3013 /* enable ALL bytes of the ref tag */
3014 ctx->ref_tag_mask[0] = 0xff;
3015 ctx->ref_tag_mask[1] = 0xff;
3016 ctx->ref_tag_mask[2] = 0xff;
3017 ctx->ref_tag_mask[3] = 0xff;
3019 case TARGET_DIF_TYPE2_PROT:
3021 * For TYPE 2 protection: 16 bit GUARD + 32 bit REF
3022 * tag has to match LBA in CDB + N
3024 ctx->ref_tag = cpu_to_le32(lba);
3025 if (!qla_tgt_ref_mask_check(se_cmd) ||
3026 !(ha->tgt.tgt_ops->chk_dif_tags(t32))) {
3027 *pfw_prot_opts |= PO_DIS_REF_TAG_VALD;
3030 /* enable ALL bytes of the ref tag */
3031 ctx->ref_tag_mask[0] = 0xff;
3032 ctx->ref_tag_mask[1] = 0xff;
3033 ctx->ref_tag_mask[2] = 0xff;
3034 ctx->ref_tag_mask[3] = 0xff;
3036 case TARGET_DIF_TYPE3_PROT:
3037 /* For TYPE 3 protection: 16 bit GUARD only */
3038 *pfw_prot_opts |= PO_DIS_REF_TAG_VALD;
3039 ctx->ref_tag_mask[0] = ctx->ref_tag_mask[1] =
3040 ctx->ref_tag_mask[2] = ctx->ref_tag_mask[3] = 0x00;
3046 qlt_build_ctio_crc2_pkt(struct qla_qpair *qpair, struct qla_tgt_prm *prm)
3048 struct dsd64 *cur_dsd;
3049 uint32_t transfer_length = 0;
3050 uint32_t data_bytes;
3052 uint8_t bundling = 1;
3053 struct crc_context *crc_ctx_pkt = NULL;
3054 struct qla_hw_data *ha;
3055 struct ctio_crc2_to_fw *pkt;
3056 dma_addr_t crc_ctx_dma;
3057 uint16_t fw_prot_opts = 0;
3058 struct qla_tgt_cmd *cmd = prm->cmd;
3059 struct se_cmd *se_cmd = &cmd->se_cmd;
3061 struct atio_from_isp *atio = &prm->cmd->atio;
3062 struct qla_tc_param tc;
3064 scsi_qla_host_t *vha = cmd->vha;
3068 pkt = (struct ctio_crc2_to_fw *)qpair->req->ring_ptr;
3070 memset(pkt, 0, sizeof(*pkt));
3072 ql_dbg_qp(ql_dbg_tgt, cmd->qpair, 0xe071,
3073 "qla_target(%d):%s: se_cmd[%p] CRC2 prot_op[0x%x] cmd prot sg:cnt[%p:%x] lba[%llu]\n",
3074 cmd->vp_idx, __func__, se_cmd, se_cmd->prot_op,
3075 prm->prot_sg, prm->prot_seg_cnt, se_cmd->t_task_lba);
3077 if ((se_cmd->prot_op == TARGET_PROT_DIN_INSERT) ||
3078 (se_cmd->prot_op == TARGET_PROT_DOUT_STRIP))
3081 /* Compute dif len and adjust data len to incude protection */
3082 data_bytes = cmd->bufflen;
3083 dif_bytes = (data_bytes / cmd->blk_sz) * 8;
3085 switch (se_cmd->prot_op) {
3086 case TARGET_PROT_DIN_INSERT:
3087 case TARGET_PROT_DOUT_STRIP:
3088 transfer_length = data_bytes;
3089 if (cmd->prot_sg_cnt)
3090 data_bytes += dif_bytes;
3092 case TARGET_PROT_DIN_STRIP:
3093 case TARGET_PROT_DOUT_INSERT:
3094 case TARGET_PROT_DIN_PASS:
3095 case TARGET_PROT_DOUT_PASS:
3096 transfer_length = data_bytes + dif_bytes;
3103 if (!qlt_hba_err_chk_enabled(se_cmd))
3104 fw_prot_opts |= 0x10; /* Disable Guard tag checking */
3105 /* HBA error checking enabled */
3106 else if (IS_PI_UNINIT_CAPABLE(ha)) {
3107 if ((se_cmd->prot_type == TARGET_DIF_TYPE1_PROT) ||
3108 (se_cmd->prot_type == TARGET_DIF_TYPE2_PROT))
3109 fw_prot_opts |= PO_DIS_VALD_APP_ESC;
3110 else if (se_cmd->prot_type == TARGET_DIF_TYPE3_PROT)
3111 fw_prot_opts |= PO_DIS_VALD_APP_REF_ESC;
3114 switch (se_cmd->prot_op) {
3115 case TARGET_PROT_DIN_INSERT:
3116 case TARGET_PROT_DOUT_INSERT:
3117 fw_prot_opts |= PO_MODE_DIF_INSERT;
3119 case TARGET_PROT_DIN_STRIP:
3120 case TARGET_PROT_DOUT_STRIP:
3121 fw_prot_opts |= PO_MODE_DIF_REMOVE;
3123 case TARGET_PROT_DIN_PASS:
3124 case TARGET_PROT_DOUT_PASS:
3125 fw_prot_opts |= PO_MODE_DIF_PASS;
3126 /* FUTURE: does tcm require T10CRC<->IPCKSUM conversion? */
3128 default:/* Normal Request */
3129 fw_prot_opts |= PO_MODE_DIF_PASS;
3134 /* Update entry type to indicate Command Type CRC_2 IOCB */
3135 pkt->entry_type = CTIO_CRC2;
3136 pkt->entry_count = 1;
3137 pkt->vp_index = cmd->vp_idx;
3139 h = qlt_make_handle(qpair);
3140 if (unlikely(h == QLA_TGT_NULL_HANDLE)) {
3142 * CTIO type 7 from the firmware doesn't provide a way to
3143 * know the initiator's LOOP ID, hence we can't find
3144 * the session and, so, the command.
3148 qpair->req->outstanding_cmds[h] = (srb_t *)prm->cmd;
3150 pkt->handle = make_handle(qpair->req->id, h);
3151 pkt->handle |= CTIO_COMPLETION_HANDLE_MARK;
3152 pkt->nport_handle = cpu_to_le16(prm->cmd->loop_id);
3153 pkt->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
3154 pkt->initiator_id = be_id_to_le(atio->u.isp24.fcp_hdr.s_id);
3155 pkt->exchange_addr = atio->u.isp24.exchange_addr;
3157 /* silence compile warning */
3158 t16 = be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id);
3159 pkt->ox_id = cpu_to_le16(t16);
3161 t16 = (atio->u.isp24.attr << 9);
3162 pkt->flags |= cpu_to_le16(t16);
3163 pkt->relative_offset = cpu_to_le32(prm->cmd->offset);
3165 /* Set transfer direction */
3166 if (cmd->dma_data_direction == DMA_TO_DEVICE)
3167 pkt->flags = cpu_to_le16(CTIO7_FLAGS_DATA_IN);
3168 else if (cmd->dma_data_direction == DMA_FROM_DEVICE)
3169 pkt->flags = cpu_to_le16(CTIO7_FLAGS_DATA_OUT);
3171 pkt->dseg_count = cpu_to_le16(prm->tot_dsds);
3172 /* Fibre channel byte count */
3173 pkt->transfer_length = cpu_to_le32(transfer_length);
3175 /* ----- CRC context -------- */
3177 /* Allocate CRC context from global pool */
3178 crc_ctx_pkt = cmd->ctx =
3179 dma_pool_zalloc(ha->dl_dma_pool, GFP_ATOMIC, &crc_ctx_dma);
3182 goto crc_queuing_error;
3184 crc_ctx_pkt->crc_ctx_dma = crc_ctx_dma;
3185 INIT_LIST_HEAD(&crc_ctx_pkt->dsd_list);
3188 crc_ctx_pkt->handle = pkt->handle;
3190 qla_tgt_set_dif_tags(cmd, crc_ctx_pkt, &fw_prot_opts);
3192 put_unaligned_le64(crc_ctx_dma, &pkt->crc_context_address);
3193 pkt->crc_context_len = cpu_to_le16(CRC_CONTEXT_LEN_FW);
3196 cur_dsd = &crc_ctx_pkt->u.nobundling.data_dsd[0];
3199 * Configure Bundling if we need to fetch interlaving
3200 * protection PCI accesses
3202 fw_prot_opts |= PO_ENABLE_DIF_BUNDLING;
3203 crc_ctx_pkt->u.bundling.dif_byte_count = cpu_to_le32(dif_bytes);
3204 crc_ctx_pkt->u.bundling.dseg_count =
3205 cpu_to_le16(prm->tot_dsds - prm->prot_seg_cnt);
3206 cur_dsd = &crc_ctx_pkt->u.bundling.data_dsd[0];
3209 /* Finish the common fields of CRC pkt */
3210 crc_ctx_pkt->blk_size = cpu_to_le16(cmd->blk_sz);
3211 crc_ctx_pkt->prot_opts = cpu_to_le16(fw_prot_opts);
3212 crc_ctx_pkt->byte_count = cpu_to_le32(data_bytes);
3213 crc_ctx_pkt->guard_seed = cpu_to_le16(0);
3215 memset((uint8_t *)&tc, 0 , sizeof(tc));
3217 tc.blk_sz = cmd->blk_sz;
3218 tc.bufflen = cmd->bufflen;
3220 tc.prot_sg = cmd->prot_sg;
3221 tc.ctx = crc_ctx_pkt;
3222 tc.ctx_dsd_alloced = &cmd->ctx_dsd_alloced;
3224 /* Walks data segments */
3225 pkt->flags |= cpu_to_le16(CTIO7_FLAGS_DSD_PTR);
3227 if (!bundling && prm->prot_seg_cnt) {
3228 if (qla24xx_walk_and_build_sglist_no_difb(ha, NULL, cur_dsd,
3229 prm->tot_dsds, &tc))
3230 goto crc_queuing_error;
3231 } else if (qla24xx_walk_and_build_sglist(ha, NULL, cur_dsd,
3232 (prm->tot_dsds - prm->prot_seg_cnt), &tc))
3233 goto crc_queuing_error;
3235 if (bundling && prm->prot_seg_cnt) {
3236 /* Walks dif segments */
3237 pkt->add_flags |= CTIO_CRC2_AF_DIF_DSD_ENA;
3239 cur_dsd = &crc_ctx_pkt->u.bundling.dif_dsd;
3240 if (qla24xx_walk_and_build_prot_sglist(ha, NULL, cur_dsd,
3241 prm->prot_seg_cnt, cmd))
3242 goto crc_queuing_error;
3247 /* Cleanup will be performed by the caller */
3248 qpair->req->outstanding_cmds[h] = NULL;
3250 return QLA_FUNCTION_FAILED;
3254 * Callback to setup response of xmit_type of QLA_TGT_XMIT_DATA and *
3255 * QLA_TGT_XMIT_STATUS for >= 24xx silicon
3257 int qlt_xmit_response(struct qla_tgt_cmd *cmd, int xmit_type,
3258 uint8_t scsi_status)
3260 struct scsi_qla_host *vha = cmd->vha;
3261 struct qla_qpair *qpair = cmd->qpair;
3262 struct ctio7_to_24xx *pkt;
3263 struct qla_tgt_prm prm;
3264 uint32_t full_req_cnt = 0;
3265 unsigned long flags = 0;
3268 if (!qpair->fw_started || (cmd->reset_count != qpair->chip_reset) ||
3269 (cmd->sess && cmd->sess->deleted)) {
3270 cmd->state = QLA_TGT_STATE_PROCESSED;
3274 ql_dbg_qp(ql_dbg_tgt, qpair, 0xe018,
3275 "is_send_status=%d, cmd->bufflen=%d, cmd->sg_cnt=%d, cmd->dma_data_direction=%d se_cmd[%p] qp %d\n",
3276 (xmit_type & QLA_TGT_XMIT_STATUS) ?
3277 1 : 0, cmd->bufflen, cmd->sg_cnt, cmd->dma_data_direction,
3278 &cmd->se_cmd, qpair->id);
3280 res = qlt_pre_xmit_response(cmd, &prm, xmit_type, scsi_status,
3282 if (unlikely(res != 0)) {
3286 spin_lock_irqsave(qpair->qp_lock_ptr, flags);
3288 if (xmit_type == QLA_TGT_XMIT_STATUS)
3289 qpair->tgt_counters.core_qla_snd_status++;
3291 qpair->tgt_counters.core_qla_que_buf++;
3293 if (!qpair->fw_started || cmd->reset_count != qpair->chip_reset) {
3295 * Either the port is not online or this request was from
3296 * previous life, just abort the processing.
3298 cmd->state = QLA_TGT_STATE_PROCESSED;
3299 ql_dbg_qp(ql_dbg_async, qpair, 0xe101,
3300 "RESET-RSP online/active/old-count/new-count = %d/%d/%d/%d.\n",
3301 vha->flags.online, qla2x00_reset_active(vha),
3302 cmd->reset_count, qpair->chip_reset);
3304 goto out_unmap_unlock;
3307 /* Does F/W have an IOCBs for this request */
3308 res = qlt_check_reserve_free_req(qpair, full_req_cnt);
3310 goto out_unmap_unlock;
3312 if (cmd->se_cmd.prot_op && (xmit_type & QLA_TGT_XMIT_DATA))
3313 res = qlt_build_ctio_crc2_pkt(qpair, &prm);
3315 res = qlt_24xx_build_ctio_pkt(qpair, &prm);
3316 if (unlikely(res != 0)) {
3317 qpair->req->cnt += full_req_cnt;
3318 goto out_unmap_unlock;
3321 pkt = (struct ctio7_to_24xx *)prm.pkt;
3323 if (qlt_has_data(cmd) && (xmit_type & QLA_TGT_XMIT_DATA)) {
3324 pkt->u.status0.flags |=
3325 cpu_to_le16(CTIO7_FLAGS_DATA_IN |
3326 CTIO7_FLAGS_STATUS_MODE_0);
3328 if (cmd->se_cmd.prot_op == TARGET_PROT_NORMAL)
3329 qlt_load_data_segments(&prm);
3331 if (prm.add_status_pkt == 0) {
3332 if (xmit_type & QLA_TGT_XMIT_STATUS) {
3333 pkt->u.status0.scsi_status =
3334 cpu_to_le16(prm.rq_result);
3336 pkt->u.status0.residual =
3337 cpu_to_le32(prm.residual);
3339 pkt->u.status0.flags |= cpu_to_le16(
3340 CTIO7_FLAGS_SEND_STATUS);
3341 if (qlt_need_explicit_conf(cmd, 0)) {
3342 pkt->u.status0.flags |=
3344 CTIO7_FLAGS_EXPLICIT_CONFORM |
3345 CTIO7_FLAGS_CONFORM_REQ);
3351 * We have already made sure that there is sufficient
3352 * amount of request entries to not drop HW lock in
3355 struct ctio7_to_24xx *ctio =
3356 (struct ctio7_to_24xx *)qlt_get_req_pkt(
3359 ql_dbg_qp(ql_dbg_tgt, qpair, 0x305e,
3360 "Building additional status packet 0x%p.\n",
3364 * T10Dif: ctio_crc2_to_fw overlay ontop of
3367 memcpy(ctio, pkt, sizeof(*ctio));
3368 /* reset back to CTIO7 */
3369 ctio->entry_count = 1;
3370 ctio->entry_type = CTIO_TYPE7;
3371 ctio->dseg_count = 0;
3372 ctio->u.status1.flags &= ~cpu_to_le16(
3373 CTIO7_FLAGS_DATA_IN);
3375 /* Real finish is ctio_m1's finish */
3376 pkt->handle |= CTIO_INTERMEDIATE_HANDLE_MARK;
3377 pkt->u.status0.flags |= cpu_to_le16(
3378 CTIO7_FLAGS_DONT_RET_CTIO);
3380 /* qlt_24xx_init_ctio_to_isp will correct
3381 * all neccessary fields that's part of CTIO7.
3382 * There should be no residual of CTIO-CRC2 data.
3384 qlt_24xx_init_ctio_to_isp((struct ctio7_to_24xx *)ctio,
3388 qlt_24xx_init_ctio_to_isp(pkt, &prm);
3391 cmd->state = QLA_TGT_STATE_PROCESSED; /* Mid-level is done processing */
3392 cmd->cmd_sent_to_fw = 1;
3393 cmd->ctio_flags = le16_to_cpu(pkt->u.status0.flags);
3395 /* Memory Barrier */
3397 if (qpair->reqq_start_iocbs)
3398 qpair->reqq_start_iocbs(qpair);
3400 qla2x00_start_iocbs(vha, qpair->req);
3401 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
3406 qlt_unmap_sg(vha, cmd);
3407 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
3411 EXPORT_SYMBOL(qlt_xmit_response);
3413 int qlt_rdy_to_xfer(struct qla_tgt_cmd *cmd)
3415 struct ctio7_to_24xx *pkt;
3416 struct scsi_qla_host *vha = cmd->vha;
3417 struct qla_tgt *tgt = cmd->tgt;
3418 struct qla_tgt_prm prm;
3419 unsigned long flags = 0;
3421 struct qla_qpair *qpair = cmd->qpair;
3423 memset(&prm, 0, sizeof(prm));
3429 if (!qpair->fw_started || (cmd->reset_count != qpair->chip_reset) ||
3430 (cmd->sess && cmd->sess->deleted)) {
3432 * Either the port is not online or this request was from
3433 * previous life, just abort the processing.
3436 cmd->write_data_transferred = 0;
3437 cmd->state = QLA_TGT_STATE_DATA_IN;
3438 vha->hw->tgt.tgt_ops->handle_data(cmd);
3439 ql_dbg_qp(ql_dbg_async, qpair, 0xe102,
3440 "RESET-XFR online/active/old-count/new-count = %d/%d/%d/%d.\n",
3441 vha->flags.online, qla2x00_reset_active(vha),
3442 cmd->reset_count, qpair->chip_reset);
3446 /* Calculate number of entries and segments required */
3447 if (qlt_pci_map_calc_cnt(&prm) != 0)
3450 spin_lock_irqsave(qpair->qp_lock_ptr, flags);
3451 /* Does F/W have an IOCBs for this request */
3452 res = qlt_check_reserve_free_req(qpair, prm.req_cnt);
3454 goto out_unlock_free_unmap;
3455 if (cmd->se_cmd.prot_op)
3456 res = qlt_build_ctio_crc2_pkt(qpair, &prm);
3458 res = qlt_24xx_build_ctio_pkt(qpair, &prm);
3460 if (unlikely(res != 0)) {
3461 qpair->req->cnt += prm.req_cnt;
3462 goto out_unlock_free_unmap;
3465 pkt = (struct ctio7_to_24xx *)prm.pkt;
3466 pkt->u.status0.flags |= cpu_to_le16(CTIO7_FLAGS_DATA_OUT |
3467 CTIO7_FLAGS_STATUS_MODE_0);
3469 if (cmd->se_cmd.prot_op == TARGET_PROT_NORMAL)
3470 qlt_load_data_segments(&prm);
3472 cmd->state = QLA_TGT_STATE_NEED_DATA;
3473 cmd->cmd_sent_to_fw = 1;
3474 cmd->ctio_flags = le16_to_cpu(pkt->u.status0.flags);
3476 /* Memory Barrier */
3478 if (qpair->reqq_start_iocbs)
3479 qpair->reqq_start_iocbs(qpair);
3481 qla2x00_start_iocbs(vha, qpair->req);
3482 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
3486 out_unlock_free_unmap:
3487 qlt_unmap_sg(vha, cmd);
3488 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
3492 EXPORT_SYMBOL(qlt_rdy_to_xfer);
3496 * it is assumed either hardware_lock or qpair lock is held.
3499 qlt_handle_dif_error(struct qla_qpair *qpair, struct qla_tgt_cmd *cmd,
3500 struct ctio_crc_from_fw *sts)
3502 uint8_t *ap = &sts->actual_dif[0];
3503 uint8_t *ep = &sts->expected_dif[0];
3504 uint64_t lba = cmd->se_cmd.t_task_lba;
3505 uint8_t scsi_status, sense_key, asc, ascq;
3506 unsigned long flags;
3507 struct scsi_qla_host *vha = cmd->vha;
3509 cmd->trc_flags |= TRC_DIF_ERR;
3511 cmd->a_guard = get_unaligned_be16(ap + 0);
3512 cmd->a_app_tag = get_unaligned_be16(ap + 2);
3513 cmd->a_ref_tag = get_unaligned_be32(ap + 4);
3515 cmd->e_guard = get_unaligned_be16(ep + 0);
3516 cmd->e_app_tag = get_unaligned_be16(ep + 2);
3517 cmd->e_ref_tag = get_unaligned_be32(ep + 4);
3519 ql_dbg(ql_dbg_tgt_dif, vha, 0xf075,
3520 "%s: aborted %d state %d\n", __func__, cmd->aborted, cmd->state);
3522 scsi_status = sense_key = asc = ascq = 0;
3524 /* check appl tag */
3525 if (cmd->e_app_tag != cmd->a_app_tag) {
3526 ql_dbg(ql_dbg_tgt_dif, vha, 0xe00d,
3527 "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]",
3528 cmd->cdb[0], lba, (lba+cmd->num_blks), cmd->num_blks,
3529 cmd->a_ref_tag, cmd->e_ref_tag, cmd->a_app_tag,
3530 cmd->e_app_tag, cmd->a_guard, cmd->e_guard, cmd,
3531 cmd->atio.u.isp24.fcp_hdr.ox_id);
3533 cmd->dif_err_code = DIF_ERR_APP;
3534 scsi_status = SAM_STAT_CHECK_CONDITION;
3535 sense_key = ABORTED_COMMAND;
3541 if (cmd->e_ref_tag != cmd->a_ref_tag) {
3542 ql_dbg(ql_dbg_tgt_dif, vha, 0xe00e,
3543 "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] ",
3544 cmd->cdb[0], lba, (lba+cmd->num_blks), cmd->num_blks,
3545 cmd->a_ref_tag, cmd->e_ref_tag, cmd->a_app_tag,
3546 cmd->e_app_tag, cmd->a_guard, cmd->e_guard, cmd,
3547 cmd->atio.u.isp24.fcp_hdr.ox_id);
3549 cmd->dif_err_code = DIF_ERR_REF;
3550 scsi_status = SAM_STAT_CHECK_CONDITION;
3551 sense_key = ABORTED_COMMAND;
3558 if (cmd->e_guard != cmd->a_guard) {
3559 ql_dbg(ql_dbg_tgt_dif, vha, 0xe012,
3560 "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]",
3561 cmd->cdb[0], lba, (lba+cmd->num_blks), cmd->num_blks,
3562 cmd->a_ref_tag, cmd->e_ref_tag, cmd->a_app_tag,
3563 cmd->e_app_tag, cmd->a_guard, cmd->e_guard, cmd,
3564 cmd->atio.u.isp24.fcp_hdr.ox_id);
3566 cmd->dif_err_code = DIF_ERR_GRD;
3567 scsi_status = SAM_STAT_CHECK_CONDITION;
3568 sense_key = ABORTED_COMMAND;
3573 switch (cmd->state) {
3574 case QLA_TGT_STATE_NEED_DATA:
3575 /* handle_data will load DIF error code */
3576 cmd->state = QLA_TGT_STATE_DATA_IN;
3577 vha->hw->tgt.tgt_ops->handle_data(cmd);
3580 spin_lock_irqsave(&cmd->cmd_lock, flags);
3582 spin_unlock_irqrestore(&cmd->cmd_lock, flags);
3583 vha->hw->tgt.tgt_ops->free_cmd(cmd);
3586 spin_unlock_irqrestore(&cmd->cmd_lock, flags);
3588 qlt_send_resp_ctio(qpair, cmd, scsi_status, sense_key, asc,
3590 /* assume scsi status gets out on the wire.
3591 * Will not wait for completion.
3593 vha->hw->tgt.tgt_ops->free_cmd(cmd);
3598 /* If hardware_lock held on entry, might drop it, then reaquire */
3599 /* This function sends the appropriate CTIO to ISP 2xxx or 24xx */
3600 static int __qlt_send_term_imm_notif(struct scsi_qla_host *vha,
3601 struct imm_ntfy_from_isp *ntfy)
3603 struct nack_to_isp *nack;
3604 struct qla_hw_data *ha = vha->hw;
3608 ql_dbg(ql_dbg_tgt_tmr, vha, 0xe01c,
3609 "Sending TERM ELS CTIO (ha=%p)\n", ha);
3611 pkt = (request_t *)qla2x00_alloc_iocbs(vha, NULL);
3613 ql_dbg(ql_dbg_tgt, vha, 0xe080,
3614 "qla_target(%d): %s failed: unable to allocate "
3615 "request packet\n", vha->vp_idx, __func__);
3619 pkt->entry_type = NOTIFY_ACK_TYPE;
3620 pkt->entry_count = 1;
3621 pkt->handle = QLA_TGT_SKIP_HANDLE;
3623 nack = (struct nack_to_isp *)pkt;
3624 nack->ox_id = ntfy->ox_id;
3626 nack->u.isp24.nport_handle = ntfy->u.isp24.nport_handle;
3627 if (le16_to_cpu(ntfy->u.isp24.status) == IMM_NTFY_ELS) {
3628 nack->u.isp24.flags = ntfy->u.isp24.flags &
3629 cpu_to_le16(NOTIFY24XX_FLAGS_PUREX_IOCB);
3633 nack->u.isp24.flags |=
3634 __constant_cpu_to_le16(NOTIFY_ACK_FLAGS_TERMINATE);
3636 nack->u.isp24.srr_rx_id = ntfy->u.isp24.srr_rx_id;
3637 nack->u.isp24.status = ntfy->u.isp24.status;
3638 nack->u.isp24.status_subcode = ntfy->u.isp24.status_subcode;
3639 nack->u.isp24.fw_handle = ntfy->u.isp24.fw_handle;
3640 nack->u.isp24.exchange_address = ntfy->u.isp24.exchange_address;
3641 nack->u.isp24.srr_rel_offs = ntfy->u.isp24.srr_rel_offs;
3642 nack->u.isp24.srr_ui = ntfy->u.isp24.srr_ui;
3643 nack->u.isp24.vp_index = ntfy->u.isp24.vp_index;
3645 qla2x00_start_iocbs(vha, vha->req);
3649 static void qlt_send_term_imm_notif(struct scsi_qla_host *vha,
3650 struct imm_ntfy_from_isp *imm, int ha_locked)
3654 WARN_ON_ONCE(!ha_locked);
3655 rc = __qlt_send_term_imm_notif(vha, imm);
3656 pr_debug("rc = %d\n", rc);
3660 * If hardware_lock held on entry, might drop it, then reaquire
3661 * This function sends the appropriate CTIO to ISP 2xxx or 24xx
3663 static int __qlt_send_term_exchange(struct qla_qpair *qpair,
3664 struct qla_tgt_cmd *cmd,
3665 struct atio_from_isp *atio)
3667 struct scsi_qla_host *vha = qpair->vha;
3668 struct ctio7_to_24xx *ctio24;
3669 struct qla_hw_data *ha = vha->hw;
3674 ql_dbg(ql_dbg_tgt, vha, 0xe009, "Sending TERM EXCH CTIO (ha=%p)\n", ha);
3679 pkt = (request_t *)qla2x00_alloc_iocbs_ready(qpair, NULL);
3681 ql_dbg(ql_dbg_tgt, vha, 0xe050,
3682 "qla_target(%d): %s failed: unable to allocate "
3683 "request packet\n", vha->vp_idx, __func__);
3688 if (cmd->state < QLA_TGT_STATE_PROCESSED) {
3689 ql_dbg(ql_dbg_tgt, vha, 0xe051,
3690 "qla_target(%d): Terminating cmd %p with "
3691 "incorrect state %d\n", vha->vp_idx, cmd,
3697 qpair->tgt_counters.num_term_xchg_sent++;
3698 pkt->entry_count = 1;
3699 pkt->handle = QLA_TGT_SKIP_HANDLE | CTIO_COMPLETION_HANDLE_MARK;
3701 ctio24 = (struct ctio7_to_24xx *)pkt;
3702 ctio24->entry_type = CTIO_TYPE7;
3703 ctio24->nport_handle = cpu_to_le16(CTIO7_NHANDLE_UNRECOGNIZED);
3704 ctio24->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
3705 ctio24->vp_index = vha->vp_idx;
3706 ctio24->initiator_id = be_id_to_le(atio->u.isp24.fcp_hdr.s_id);
3707 ctio24->exchange_addr = atio->u.isp24.exchange_addr;
3708 temp = (atio->u.isp24.attr << 9) | CTIO7_FLAGS_STATUS_MODE_1 |
3709 CTIO7_FLAGS_TERMINATE;
3710 ctio24->u.status1.flags = cpu_to_le16(temp);
3711 temp = be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id);
3712 ctio24->u.status1.ox_id = cpu_to_le16(temp);
3714 /* Memory Barrier */
3716 if (qpair->reqq_start_iocbs)
3717 qpair->reqq_start_iocbs(qpair);
3719 qla2x00_start_iocbs(vha, qpair->req);
3723 static void qlt_send_term_exchange(struct qla_qpair *qpair,
3724 struct qla_tgt_cmd *cmd, struct atio_from_isp *atio, int ha_locked,
3727 struct scsi_qla_host *vha;
3728 unsigned long flags = 0;
3731 /* why use different vha? NPIV */
3738 rc = __qlt_send_term_exchange(qpair, cmd, atio);
3740 qlt_alloc_qfull_cmd(vha, atio, 0, 0);
3743 spin_lock_irqsave(qpair->qp_lock_ptr, flags);
3744 rc = __qlt_send_term_exchange(qpair, cmd, atio);
3746 qlt_alloc_qfull_cmd(vha, atio, 0, 0);
3749 if (cmd && !ul_abort && !cmd->aborted) {
3751 qlt_unmap_sg(vha, cmd);
3752 vha->hw->tgt.tgt_ops->free_cmd(cmd);
3756 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
3761 static void qlt_init_term_exchange(struct scsi_qla_host *vha)
3763 struct list_head free_list;
3764 struct qla_tgt_cmd *cmd, *tcmd;
3766 vha->hw->tgt.leak_exchg_thresh_hold =
3767 (vha->hw->cur_fw_xcb_count/100) * LEAK_EXCHG_THRESH_HOLD_PERCENT;
3770 if (!list_empty(&vha->hw->tgt.q_full_list)) {
3771 INIT_LIST_HEAD(&free_list);
3772 list_splice_init(&vha->hw->tgt.q_full_list, &free_list);
3774 list_for_each_entry_safe(cmd, tcmd, &free_list, cmd_list) {
3775 list_del(&cmd->cmd_list);
3776 /* This cmd was never sent to TCM. There is no need
3777 * to schedule free or call free_cmd
3780 vha->hw->tgt.num_qfull_cmds_alloc--;
3783 vha->hw->tgt.num_qfull_cmds_dropped = 0;
3786 static void qlt_chk_exch_leak_thresh_hold(struct scsi_qla_host *vha)
3788 uint32_t total_leaked;
3790 total_leaked = vha->hw->tgt.num_qfull_cmds_dropped;
3792 if (vha->hw->tgt.leak_exchg_thresh_hold &&
3793 (total_leaked > vha->hw->tgt.leak_exchg_thresh_hold)) {
3795 ql_dbg(ql_dbg_tgt, vha, 0xe079,
3796 "Chip reset due to exchange starvation: %d/%d.\n",
3797 total_leaked, vha->hw->cur_fw_xcb_count);
3799 if (IS_P3P_TYPE(vha->hw))
3800 set_bit(FCOE_CTX_RESET_NEEDED, &vha->dpc_flags);
3802 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
3803 qla2xxx_wake_dpc(vha);
3808 int qlt_abort_cmd(struct qla_tgt_cmd *cmd)
3810 struct qla_tgt *tgt = cmd->tgt;
3811 struct scsi_qla_host *vha = tgt->vha;
3812 struct se_cmd *se_cmd = &cmd->se_cmd;
3813 unsigned long flags;
3815 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf014,
3816 "qla_target(%d): terminating exchange for aborted cmd=%p "
3817 "(se_cmd=%p, tag=%llu)", vha->vp_idx, cmd, &cmd->se_cmd,
3820 spin_lock_irqsave(&cmd->cmd_lock, flags);
3823 qlt_unmap_sg(vha, cmd);
3825 spin_unlock_irqrestore(&cmd->cmd_lock, flags);
3827 * It's normal to see 2 calls in this path:
3828 * 1) XFER Rdy completion + CMD_T_ABORT
3829 * 2) TCM TMR - drain_state_list
3831 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf016,
3832 "multiple abort. %p transport_state %x, t_state %x, "
3833 "se_cmd_flags %x\n", cmd, cmd->se_cmd.transport_state,
3834 cmd->se_cmd.t_state, cmd->se_cmd.se_cmd_flags);
3838 cmd->trc_flags |= TRC_ABORT;
3839 spin_unlock_irqrestore(&cmd->cmd_lock, flags);
3841 qlt_send_term_exchange(cmd->qpair, cmd, &cmd->atio, 0, 1);
3844 EXPORT_SYMBOL(qlt_abort_cmd);
3846 void qlt_free_cmd(struct qla_tgt_cmd *cmd)
3848 struct fc_port *sess = cmd->sess;
3850 ql_dbg(ql_dbg_tgt, cmd->vha, 0xe074,
3851 "%s: se_cmd[%p] ox_id %04x\n",
3852 __func__, &cmd->se_cmd,
3853 be16_to_cpu(cmd->atio.u.isp24.fcp_hdr.ox_id));
3855 BUG_ON(cmd->cmd_in_wq);
3858 qlt_decr_num_pend_cmds(cmd->vha);
3860 BUG_ON(cmd->sg_mapped);
3861 cmd->jiffies_at_free = get_jiffies_64();
3863 if (!sess || !sess->se_sess) {
3867 cmd->jiffies_at_free = get_jiffies_64();
3868 cmd->vha->hw->tgt.tgt_ops->rel_cmd(cmd);
3870 EXPORT_SYMBOL(qlt_free_cmd);
3873 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
3875 static int qlt_term_ctio_exchange(struct qla_qpair *qpair, void *ctio,
3876 struct qla_tgt_cmd *cmd, uint32_t status)
3879 struct scsi_qla_host *vha = qpair->vha;
3881 if (cmd->se_cmd.prot_op)
3882 ql_dbg(ql_dbg_tgt_dif, vha, 0xe013,
3883 "Term DIF cmd: lba[0x%llx|%lld] len[0x%x] "
3884 "se_cmd=%p tag[%x] op %#x/%s",
3886 cmd->num_blks, &cmd->se_cmd,
3887 cmd->atio.u.isp24.exchange_addr,
3888 cmd->se_cmd.prot_op,
3889 prot_op_str(cmd->se_cmd.prot_op));
3892 struct ctio7_from_24xx *c = (struct ctio7_from_24xx *)ctio;
3895 cpu_to_le16(OF_TERM_EXCH));
3900 qlt_send_term_exchange(qpair, cmd, &cmd->atio, 1, 0);
3906 /* ha->hardware_lock supposed to be held on entry */
3907 static void *qlt_ctio_to_cmd(struct scsi_qla_host *vha,
3908 struct rsp_que *rsp, uint32_t handle, void *ctio)
3911 struct req_que *req;
3912 int qid = GET_QID(handle);
3913 uint32_t h = handle & ~QLA_TGT_HANDLE_MASK;
3915 if (unlikely(h == QLA_TGT_SKIP_HANDLE))
3918 if (qid == rsp->req->id) {
3920 } else if (vha->hw->req_q_map[qid]) {
3921 ql_dbg(ql_dbg_tgt_mgt, vha, 0x1000a,
3922 "qla_target(%d): CTIO completion with different QID %d handle %x\n",
3923 vha->vp_idx, rsp->id, handle);
3924 req = vha->hw->req_q_map[qid];
3929 h &= QLA_CMD_HANDLE_MASK;
3931 if (h != QLA_TGT_NULL_HANDLE) {
3932 if (unlikely(h >= req->num_outstanding_cmds)) {
3933 ql_dbg(ql_dbg_tgt, vha, 0xe052,
3934 "qla_target(%d): Wrong handle %x received\n",
3935 vha->vp_idx, handle);
3939 cmd = req->outstanding_cmds[h];
3940 if (unlikely(cmd == NULL)) {
3941 ql_dbg(ql_dbg_async, vha, 0xe053,
3942 "qla_target(%d): Suspicious: unable to find the command with handle %x req->id %d rsp->id %d\n",
3943 vha->vp_idx, handle, req->id, rsp->id);
3946 req->outstanding_cmds[h] = NULL;
3947 } else if (ctio != NULL) {
3948 /* We can't get loop ID from CTIO7 */
3949 ql_dbg(ql_dbg_tgt, vha, 0xe054,
3950 "qla_target(%d): Wrong CTIO received: QLA24xx doesn't "
3951 "support NULL handles\n", vha->vp_idx);
3959 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
3961 static void qlt_do_ctio_completion(struct scsi_qla_host *vha,
3962 struct rsp_que *rsp, uint32_t handle, uint32_t status, void *ctio)
3964 struct qla_hw_data *ha = vha->hw;
3965 struct se_cmd *se_cmd;
3966 struct qla_tgt_cmd *cmd;
3967 struct qla_qpair *qpair = rsp->qpair;
3969 if (handle & CTIO_INTERMEDIATE_HANDLE_MARK) {
3970 /* That could happen only in case of an error/reset/abort */
3971 if (status != CTIO_SUCCESS) {
3972 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01d,
3973 "Intermediate CTIO received"
3974 " (status %x)\n", status);
3979 cmd = qlt_ctio_to_cmd(vha, rsp, handle, ctio);
3983 if ((le16_to_cpu(((struct ctio7_from_24xx *)ctio)->flags) & CTIO7_FLAGS_DATA_OUT) &&
3985 qlt_chk_edif_rx_sa_delete_pending(vha, cmd->sess,
3986 (struct ctio7_from_24xx *)ctio);
3989 se_cmd = &cmd->se_cmd;
3990 cmd->cmd_sent_to_fw = 0;
3992 qlt_unmap_sg(vha, cmd);
3994 if (unlikely(status != CTIO_SUCCESS)) {
3995 switch (status & 0xFFFF) {
3996 case CTIO_INVALID_RX_ID:
3997 if (printk_ratelimit())
3998 dev_info(&vha->hw->pdev->dev,
3999 "qla_target(%d): CTIO with INVALID_RX_ID ATIO attr %x CTIO Flags %x|%x\n",
4000 vha->vp_idx, cmd->atio.u.isp24.attr,
4001 ((cmd->ctio_flags >> 9) & 0xf),
4005 case CTIO_LIP_RESET:
4006 case CTIO_TARGET_RESET:
4008 /* driver request abort via Terminate exchange */
4011 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf058,
4012 "qla_target(%d): CTIO with "
4013 "status %#x received, state %x, se_cmd %p, "
4014 "(LIP_RESET=e, ABORTED=2, TARGET_RESET=17, "
4015 "TIMEOUT=b, INVALID_RX_ID=8)\n", vha->vp_idx,
4016 status, cmd->state, se_cmd);
4019 case CTIO_PORT_LOGGED_OUT:
4020 case CTIO_PORT_UNAVAILABLE:
4023 (status & 0xFFFF) == CTIO_PORT_LOGGED_OUT;
4025 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf059,
4026 "qla_target(%d): CTIO with %s status %x "
4027 "received (state %x, se_cmd %p)\n", vha->vp_idx,
4028 logged_out ? "PORT LOGGED OUT" : "PORT UNAVAILABLE",
4029 status, cmd->state, se_cmd);
4031 if (logged_out && cmd->sess) {
4033 * Session is already logged out, but we need
4034 * to notify initiator, who's not aware of this
4036 cmd->sess->send_els_logo = 1;
4037 ql_dbg(ql_dbg_disc, vha, 0x20f8,
4038 "%s %d %8phC post del sess\n",
4039 __func__, __LINE__, cmd->sess->port_name);
4041 qlt_schedule_sess_for_deletion(cmd->sess);
4045 case CTIO_DIF_ERROR: {
4046 struct ctio_crc_from_fw *crc =
4047 (struct ctio_crc_from_fw *)ctio;
4048 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf073,
4049 "qla_target(%d): CTIO with DIF_ERROR status %x "
4050 "received (state %x, ulp_cmd %p) actual_dif[0x%llx] "
4051 "expect_dif[0x%llx]\n",
4052 vha->vp_idx, status, cmd->state, se_cmd,
4053 *((u64 *)&crc->actual_dif[0]),
4054 *((u64 *)&crc->expected_dif[0]));
4056 qlt_handle_dif_error(qpair, cmd, ctio);
4060 case CTIO_FAST_AUTH_ERR:
4061 case CTIO_FAST_INCOMP_PAD_LEN:
4062 case CTIO_FAST_INVALID_REQ:
4063 case CTIO_FAST_SPI_ERR:
4064 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05b,
4065 "qla_target(%d): CTIO with EDIF error status 0x%x received (state %x, se_cmd %p\n",
4066 vha->vp_idx, status, cmd->state, se_cmd);
4070 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05b,
4071 "qla_target(%d): CTIO with error status 0x%x received (state %x, se_cmd %p\n",
4072 vha->vp_idx, status, cmd->state, se_cmd);
4077 /* "cmd->aborted" means
4078 * cmd is already aborted/terminated, we don't
4079 * need to terminate again. The exchange is already
4080 * cleaned up/freed at FW level. Just cleanup at driver
4083 if ((cmd->state != QLA_TGT_STATE_NEED_DATA) &&
4085 cmd->trc_flags |= TRC_CTIO_ERR;
4086 if (qlt_term_ctio_exchange(qpair, ctio, cmd, status))
4091 if (cmd->state == QLA_TGT_STATE_PROCESSED) {
4092 cmd->trc_flags |= TRC_CTIO_DONE;
4093 } else if (cmd->state == QLA_TGT_STATE_NEED_DATA) {
4094 cmd->state = QLA_TGT_STATE_DATA_IN;
4096 if (status == CTIO_SUCCESS)
4097 cmd->write_data_transferred = 1;
4099 ha->tgt.tgt_ops->handle_data(cmd);
4101 } else if (cmd->aborted) {
4102 cmd->trc_flags |= TRC_CTIO_ABORTED;
4103 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01e,
4104 "Aborted command %p (tag %lld) finished\n", cmd, se_cmd->tag);
4106 cmd->trc_flags |= TRC_CTIO_STRANGE;
4107 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05c,
4108 "qla_target(%d): A command in state (%d) should "
4109 "not return a CTIO complete\n", vha->vp_idx, cmd->state);
4112 if (unlikely(status != CTIO_SUCCESS) &&
4114 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01f, "Finishing failed CTIO\n");
4118 ha->tgt.tgt_ops->free_cmd(cmd);
4121 static inline int qlt_get_fcp_task_attr(struct scsi_qla_host *vha,
4126 switch (task_codes) {
4127 case ATIO_SIMPLE_QUEUE:
4128 fcp_task_attr = TCM_SIMPLE_TAG;
4130 case ATIO_HEAD_OF_QUEUE:
4131 fcp_task_attr = TCM_HEAD_TAG;
4133 case ATIO_ORDERED_QUEUE:
4134 fcp_task_attr = TCM_ORDERED_TAG;
4136 case ATIO_ACA_QUEUE:
4137 fcp_task_attr = TCM_ACA_TAG;
4140 fcp_task_attr = TCM_SIMPLE_TAG;
4143 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05d,
4144 "qla_target: unknown task code %x, use ORDERED instead\n",
4146 fcp_task_attr = TCM_ORDERED_TAG;
4150 return fcp_task_attr;
4154 * Process context for I/O path into tcm_qla2xxx code
4156 static void __qlt_do_work(struct qla_tgt_cmd *cmd)
4158 scsi_qla_host_t *vha = cmd->vha;
4159 struct qla_hw_data *ha = vha->hw;
4160 struct fc_port *sess = cmd->sess;
4161 struct atio_from_isp *atio = &cmd->atio;
4163 unsigned long flags;
4164 uint32_t data_length;
4165 int ret, fcp_task_attr, data_dir, bidi = 0;
4166 struct qla_qpair *qpair = cmd->qpair;
4169 cmd->trc_flags |= TRC_DO_WORK;
4172 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf082,
4173 "cmd with tag %u is aborted\n",
4174 cmd->atio.u.isp24.exchange_addr);
4178 spin_lock_init(&cmd->cmd_lock);
4179 cdb = &atio->u.isp24.fcp_cmnd.cdb[0];
4180 cmd->se_cmd.tag = le32_to_cpu(atio->u.isp24.exchange_addr);
4182 if (atio->u.isp24.fcp_cmnd.rddata &&
4183 atio->u.isp24.fcp_cmnd.wrdata) {
4185 data_dir = DMA_TO_DEVICE;
4186 } else if (atio->u.isp24.fcp_cmnd.rddata)
4187 data_dir = DMA_FROM_DEVICE;
4188 else if (atio->u.isp24.fcp_cmnd.wrdata)
4189 data_dir = DMA_TO_DEVICE;
4191 data_dir = DMA_NONE;
4193 fcp_task_attr = qlt_get_fcp_task_attr(vha,
4194 atio->u.isp24.fcp_cmnd.task_attr);
4195 data_length = get_datalen_for_atio(atio);
4197 ret = ha->tgt.tgt_ops->handle_cmd(vha, cmd, cdb, data_length,
4198 fcp_task_attr, data_dir, bidi);
4202 * Drop extra session reference from qlt_handle_cmd_for_atio().
4204 ha->tgt.tgt_ops->put_sess(sess);
4208 ql_dbg(ql_dbg_io, vha, 0x3060, "Terminating work cmd %p", cmd);
4210 * cmd has not sent to target yet, so pass NULL as the second
4211 * argument to qlt_send_term_exchange() and free the memory here.
4213 cmd->trc_flags |= TRC_DO_WORK_ERR;
4214 spin_lock_irqsave(qpair->qp_lock_ptr, flags);
4215 qlt_send_term_exchange(qpair, NULL, &cmd->atio, 1, 0);
4217 qlt_decr_num_pend_cmds(vha);
4218 cmd->vha->hw->tgt.tgt_ops->rel_cmd(cmd);
4219 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
4221 ha->tgt.tgt_ops->put_sess(sess);
4224 static void qlt_do_work(struct work_struct *work)
4226 struct qla_tgt_cmd *cmd = container_of(work, struct qla_tgt_cmd, work);
4227 scsi_qla_host_t *vha = cmd->vha;
4228 unsigned long flags;
4230 spin_lock_irqsave(&vha->cmd_list_lock, flags);
4231 list_del(&cmd->cmd_list);
4232 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
4237 void qlt_clr_qp_table(struct scsi_qla_host *vha)
4239 unsigned long flags;
4240 struct qla_hw_data *ha = vha->hw;
4241 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
4245 ql_log(ql_log_info, vha, 0x706c,
4246 "User update Number of Active Qpairs %d\n",
4247 ha->tgt.num_act_qpairs);
4249 spin_lock_irqsave(&ha->tgt.atio_lock, flags);
4251 btree_for_each_safe64(&tgt->lun_qpair_map, key, node)
4252 btree_remove64(&tgt->lun_qpair_map, key);
4254 ha->base_qpair->lun_cnt = 0;
4255 for (key = 0; key < ha->max_qpairs; key++)
4256 if (ha->queue_pair_map[key])
4257 ha->queue_pair_map[key]->lun_cnt = 0;
4259 spin_unlock_irqrestore(&ha->tgt.atio_lock, flags);
4262 static void qlt_assign_qpair(struct scsi_qla_host *vha,
4263 struct qla_tgt_cmd *cmd)
4265 struct qla_qpair *qpair, *qp;
4266 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
4267 struct qla_qpair_hint *h;
4269 if (vha->flags.qpairs_available) {
4270 h = btree_lookup64(&tgt->lun_qpair_map, cmd->unpacked_lun);
4272 /* spread lun to qpair ratio evently */
4274 struct scsi_qla_host *base_vha =
4275 pci_get_drvdata(vha->hw->pdev);
4277 qpair = vha->hw->base_qpair;
4278 if (qpair->lun_cnt == 0) {
4280 h = qla_qpair_to_hint(tgt, qpair);
4282 rc = btree_insert64(&tgt->lun_qpair_map,
4283 cmd->unpacked_lun, h, GFP_ATOMIC);
4286 ql_log(ql_log_info, vha, 0xd037,
4287 "Unable to insert lun %llx into lun_qpair_map\n",
4292 lcnt = qpair->lun_cnt;
4296 list_for_each_entry(qp, &base_vha->qp_list,
4298 if (qp->lun_cnt == 0) {
4300 h = qla_qpair_to_hint(tgt, qp);
4302 rc = btree_insert64(&tgt->lun_qpair_map,
4303 cmd->unpacked_lun, h, GFP_ATOMIC);
4306 ql_log(ql_log_info, vha, 0xd038,
4307 "Unable to insert lun %llx into lun_qpair_map\n",
4313 if (qp->lun_cnt < lcnt) {
4322 h = qla_qpair_to_hint(tgt, qpair);
4324 rc = btree_insert64(&tgt->lun_qpair_map,
4325 cmd->unpacked_lun, h, GFP_ATOMIC);
4328 ql_log(ql_log_info, vha, 0xd039,
4329 "Unable to insert lun %llx into lun_qpair_map\n",
4334 h = &tgt->qphints[0];
4337 cmd->qpair = h->qpair;
4338 cmd->se_cmd.cpuid = h->cpuid;
4341 static struct qla_tgt_cmd *qlt_get_tag(scsi_qla_host_t *vha,
4342 struct fc_port *sess,
4343 struct atio_from_isp *atio)
4345 struct qla_tgt_cmd *cmd;
4347 cmd = vha->hw->tgt.tgt_ops->get_cmd(sess);
4351 cmd->cmd_type = TYPE_TGT_CMD;
4352 memcpy(&cmd->atio, atio, sizeof(*atio));
4353 INIT_LIST_HEAD(&cmd->sess_cmd_list);
4354 cmd->state = QLA_TGT_STATE_NEW;
4355 cmd->tgt = vha->vha_tgt.qla_tgt;
4356 qlt_incr_num_pend_cmds(vha);
4359 cmd->loop_id = sess->loop_id;
4360 cmd->conf_compl_supported = sess->conf_compl_supported;
4363 cmd->jiffies_at_alloc = get_jiffies_64();
4365 cmd->unpacked_lun = scsilun_to_int(
4366 (struct scsi_lun *)&atio->u.isp24.fcp_cmnd.lun);
4367 qlt_assign_qpair(vha, cmd);
4368 cmd->reset_count = vha->hw->base_qpair->chip_reset;
4369 cmd->vp_idx = vha->vp_idx;
4370 cmd->edif = sess->edif.enable;
4375 /* ha->hardware_lock supposed to be held on entry */
4376 static int qlt_handle_cmd_for_atio(struct scsi_qla_host *vha,
4377 struct atio_from_isp *atio)
4379 struct qla_hw_data *ha = vha->hw;
4380 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
4381 struct fc_port *sess;
4382 struct qla_tgt_cmd *cmd;
4383 unsigned long flags;
4386 if (unlikely(tgt->tgt_stop)) {
4387 ql_dbg(ql_dbg_io, vha, 0x3061,
4388 "New command while device %p is shutting down\n", tgt);
4392 id = be_to_port_id(atio->u.isp24.fcp_hdr.s_id);
4393 if (IS_SW_RESV_ADDR(id))
4396 sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha, atio->u.isp24.fcp_hdr.s_id);
4397 if (unlikely(!sess))
4400 /* Another WWN used to have our s_id. Our PLOGI scheduled its
4401 * session deletion, but it's still in sess_del_work wq */
4402 if (sess->deleted) {
4403 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf002,
4404 "New command while old session %p is being deleted\n",
4410 * Do kref_get() before returning + dropping qla_hw_data->hardware_lock.
4412 if (!kref_get_unless_zero(&sess->sess_kref)) {
4413 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf004,
4414 "%s: kref_get fail, %8phC oxid %x \n",
4415 __func__, sess->port_name,
4416 be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id));
4420 cmd = qlt_get_tag(vha, sess, atio);
4422 ql_dbg(ql_dbg_io, vha, 0x3062,
4423 "qla_target(%d): Allocation of cmd failed\n", vha->vp_idx);
4424 ha->tgt.tgt_ops->put_sess(sess);
4429 cmd->trc_flags |= TRC_NEW_CMD;
4431 spin_lock_irqsave(&vha->cmd_list_lock, flags);
4432 list_add_tail(&cmd->cmd_list, &vha->qla_cmd_list);
4433 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
4435 INIT_WORK(&cmd->work, qlt_do_work);
4436 if (vha->flags.qpairs_available) {
4437 queue_work_on(cmd->se_cmd.cpuid, qla_tgt_wq, &cmd->work);
4438 } else if (ha->msix_count) {
4439 if (cmd->atio.u.isp24.fcp_cmnd.rddata)
4440 queue_work_on(smp_processor_id(), qla_tgt_wq,
4443 queue_work_on(cmd->se_cmd.cpuid, qla_tgt_wq,
4446 queue_work(qla_tgt_wq, &cmd->work);
4452 /* ha->hardware_lock supposed to be held on entry */
4453 static int qlt_issue_task_mgmt(struct fc_port *sess, u64 lun,
4454 int fn, void *iocb, int flags)
4456 struct scsi_qla_host *vha = sess->vha;
4457 struct qla_hw_data *ha = vha->hw;
4458 struct qla_tgt_mgmt_cmd *mcmd;
4459 struct atio_from_isp *a = (struct atio_from_isp *)iocb;
4460 struct qla_qpair_hint *h = &vha->vha_tgt.qla_tgt->qphints[0];
4462 mcmd = mempool_alloc(qla_tgt_mgmt_cmd_mempool, GFP_ATOMIC);
4464 ql_dbg(ql_dbg_tgt_tmr, vha, 0x10009,
4465 "qla_target(%d): Allocation of management "
4466 "command failed, some commands and their data could "
4467 "leak\n", vha->vp_idx);
4470 memset(mcmd, 0, sizeof(*mcmd));
4474 memcpy(&mcmd->orig_iocb.imm_ntfy, iocb,
4475 sizeof(mcmd->orig_iocb.imm_ntfy));
4477 mcmd->tmr_func = fn;
4478 mcmd->flags = flags;
4479 mcmd->reset_count = ha->base_qpair->chip_reset;
4480 mcmd->qpair = h->qpair;
4482 mcmd->se_cmd.cpuid = h->cpuid;
4483 mcmd->unpacked_lun = lun;
4486 case QLA_TGT_LUN_RESET:
4487 case QLA_TGT_CLEAR_TS:
4488 case QLA_TGT_ABORT_TS:
4489 abort_cmds_for_lun(vha, lun, a->u.isp24.fcp_hdr.s_id);
4491 case QLA_TGT_CLEAR_ACA:
4492 h = qlt_find_qphint(vha, mcmd->unpacked_lun);
4493 mcmd->qpair = h->qpair;
4494 mcmd->se_cmd.cpuid = h->cpuid;
4497 case QLA_TGT_TARGET_RESET:
4498 case QLA_TGT_NEXUS_LOSS_SESS:
4499 case QLA_TGT_NEXUS_LOSS:
4500 case QLA_TGT_ABORT_ALL:
4506 INIT_WORK(&mcmd->work, qlt_do_tmr_work);
4507 queue_work_on(mcmd->se_cmd.cpuid, qla_tgt_wq,
4513 /* ha->hardware_lock supposed to be held on entry */
4514 static int qlt_handle_task_mgmt(struct scsi_qla_host *vha, void *iocb)
4516 struct atio_from_isp *a = (struct atio_from_isp *)iocb;
4517 struct qla_hw_data *ha = vha->hw;
4518 struct fc_port *sess;
4521 unsigned long flags;
4523 fn = a->u.isp24.fcp_cmnd.task_mgmt_flags;
4525 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
4526 sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha,
4527 a->u.isp24.fcp_hdr.s_id);
4528 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
4531 scsilun_to_int((struct scsi_lun *)&a->u.isp24.fcp_cmnd.lun);
4533 if (sess == NULL || sess->deleted)
4536 return qlt_issue_task_mgmt(sess, unpacked_lun, fn, iocb, 0);
4539 /* ha->hardware_lock supposed to be held on entry */
4540 static int __qlt_abort_task(struct scsi_qla_host *vha,
4541 struct imm_ntfy_from_isp *iocb, struct fc_port *sess)
4543 struct atio_from_isp *a = (struct atio_from_isp *)iocb;
4544 struct qla_hw_data *ha = vha->hw;
4545 struct qla_tgt_mgmt_cmd *mcmd;
4549 mcmd = mempool_alloc(qla_tgt_mgmt_cmd_mempool, GFP_ATOMIC);
4551 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05f,
4552 "qla_target(%d): %s: Allocation of ABORT cmd failed\n",
4553 vha->vp_idx, __func__);
4556 memset(mcmd, 0, sizeof(*mcmd));
4559 memcpy(&mcmd->orig_iocb.imm_ntfy, iocb,
4560 sizeof(mcmd->orig_iocb.imm_ntfy));
4563 scsilun_to_int((struct scsi_lun *)&a->u.isp24.fcp_cmnd.lun);
4564 mcmd->reset_count = ha->base_qpair->chip_reset;
4565 mcmd->tmr_func = QLA_TGT_2G_ABORT_TASK;
4566 mcmd->qpair = ha->base_qpair;
4568 rc = ha->tgt.tgt_ops->handle_tmr(mcmd, unpacked_lun, mcmd->tmr_func,
4569 le16_to_cpu(iocb->u.isp2x.seq_id));
4571 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf060,
4572 "qla_target(%d): tgt_ops->handle_tmr() failed: %d\n",
4574 mempool_free(mcmd, qla_tgt_mgmt_cmd_mempool);
4581 /* ha->hardware_lock supposed to be held on entry */
4582 static int qlt_abort_task(struct scsi_qla_host *vha,
4583 struct imm_ntfy_from_isp *iocb)
4585 struct qla_hw_data *ha = vha->hw;
4586 struct fc_port *sess;
4588 unsigned long flags;
4590 loop_id = GET_TARGET_ID(ha, (struct atio_from_isp *)iocb);
4592 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
4593 sess = ha->tgt.tgt_ops->find_sess_by_loop_id(vha, loop_id);
4594 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
4597 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf025,
4598 "qla_target(%d): task abort for unexisting "
4599 "session\n", vha->vp_idx);
4600 return qlt_sched_sess_work(vha->vha_tgt.qla_tgt,
4601 QLA_TGT_SESS_WORK_ABORT, iocb, sizeof(*iocb));
4604 return __qlt_abort_task(vha, iocb, sess);
4607 void qlt_logo_completion_handler(fc_port_t *fcport, int rc)
4609 if (rc != MBS_COMMAND_COMPLETE) {
4610 ql_dbg(ql_dbg_tgt_mgt, fcport->vha, 0xf093,
4611 "%s: se_sess %p / sess %p from"
4612 " port %8phC loop_id %#04x s_id %02x:%02x:%02x"
4613 " LOGO failed: %#x\n",
4617 fcport->port_name, fcport->loop_id,
4618 fcport->d_id.b.domain, fcport->d_id.b.area,
4619 fcport->d_id.b.al_pa, rc);
4622 fcport->logout_completed = 1;
4626 * ha->hardware_lock supposed to be held on entry (to protect tgt->sess_list)
4628 * Schedules sessions with matching port_id/loop_id but different wwn for
4629 * deletion. Returns existing session with matching wwn if present.
4633 qlt_find_sess_invalidate_other(scsi_qla_host_t *vha, uint64_t wwn,
4634 port_id_t port_id, uint16_t loop_id, struct fc_port **conflict_sess)
4636 struct fc_port *sess = NULL, *other_sess;
4639 *conflict_sess = NULL;
4641 list_for_each_entry(other_sess, &vha->vp_fcports, list) {
4643 other_wwn = wwn_to_u64(other_sess->port_name);
4645 if (wwn == other_wwn) {
4651 /* find other sess with nport_id collision */
4652 if (port_id.b24 == other_sess->d_id.b24) {
4653 if (loop_id != other_sess->loop_id) {
4654 ql_dbg(ql_dbg_disc, vha, 0x1000c,
4655 "Invalidating sess %p loop_id %d wwn %llx.\n",
4656 other_sess, other_sess->loop_id, other_wwn);
4659 * logout_on_delete is set by default, but another
4660 * session that has the same s_id/loop_id combo
4661 * might have cleared it when requested this session
4662 * deletion, so don't touch it
4664 qlt_schedule_sess_for_deletion(other_sess);
4667 * Another wwn used to have our s_id/loop_id
4668 * kill the session, but don't free the loop_id
4670 ql_dbg(ql_dbg_disc, vha, 0xf01b,
4671 "Invalidating sess %p loop_id %d wwn %llx.\n",
4672 other_sess, other_sess->loop_id, other_wwn);
4674 other_sess->keep_nport_handle = 1;
4675 if (other_sess->disc_state != DSC_DELETED)
4676 *conflict_sess = other_sess;
4677 qlt_schedule_sess_for_deletion(other_sess);
4682 /* find other sess with nport handle collision */
4683 if ((loop_id == other_sess->loop_id) &&
4684 (loop_id != FC_NO_LOOP_ID)) {
4685 ql_dbg(ql_dbg_disc, vha, 0x1000d,
4686 "Invalidating sess %p loop_id %d wwn %llx.\n",
4687 other_sess, other_sess->loop_id, other_wwn);
4689 /* Same loop_id but different s_id
4690 * Ok to kill and logout */
4691 qlt_schedule_sess_for_deletion(other_sess);
4698 /* Abort any commands for this s_id waiting on qla_tgt_wq workqueue */
4699 static int abort_cmds_for_s_id(struct scsi_qla_host *vha, port_id_t *s_id)
4701 struct qla_tgt_sess_op *op;
4702 struct qla_tgt_cmd *cmd;
4705 unsigned long flags;
4707 key = (((u32)s_id->b.domain << 16) |
4708 ((u32)s_id->b.area << 8) |
4709 ((u32)s_id->b.al_pa));
4711 spin_lock_irqsave(&vha->cmd_list_lock, flags);
4712 list_for_each_entry(op, &vha->unknown_atio_list, cmd_list) {
4713 uint32_t op_key = sid_to_key(op->atio.u.isp24.fcp_hdr.s_id);
4715 if (op_key == key) {
4721 list_for_each_entry(cmd, &vha->qla_cmd_list, cmd_list) {
4722 uint32_t cmd_key = sid_to_key(cmd->atio.u.isp24.fcp_hdr.s_id);
4724 if (cmd_key == key) {
4729 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
4734 static int qlt_handle_login(struct scsi_qla_host *vha,
4735 struct imm_ntfy_from_isp *iocb)
4737 struct fc_port *sess = NULL, *conflict_sess = NULL;
4740 uint16_t loop_id, wd3_lo;
4742 struct qlt_plogi_ack_t *pla;
4743 unsigned long flags;
4745 lockdep_assert_held(&vha->hw->hardware_lock);
4747 wwn = wwn_to_u64(iocb->u.isp24.port_name);
4749 port_id.b.domain = iocb->u.isp24.port_id[2];
4750 port_id.b.area = iocb->u.isp24.port_id[1];
4751 port_id.b.al_pa = iocb->u.isp24.port_id[0];
4752 port_id.b.rsvd_1 = 0;
4754 loop_id = le16_to_cpu(iocb->u.isp24.nport_handle);
4756 /* Mark all stale commands sitting in qla_tgt_wq for deletion */
4757 abort_cmds_for_s_id(vha, &port_id);
4760 spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
4761 sess = qlt_find_sess_invalidate_other(vha, wwn,
4762 port_id, loop_id, &conflict_sess);
4763 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
4765 ql_dbg(ql_dbg_disc, vha, 0xffff,
4766 "%s %d Term INOT due to WWN=0 lid=%d, NportID %06X ",
4767 __func__, __LINE__, loop_id, port_id.b24);
4768 qlt_send_term_imm_notif(vha, iocb, 1);
4772 if (IS_SW_RESV_ADDR(port_id)) {
4777 if (vha->hw->flags.edif_enabled &&
4778 !(vha->e_dbell.db_flags & EDB_ACTIVE) &&
4779 iocb->u.isp24.status_subcode == ELS_PLOGI &&
4780 !(le16_to_cpu(iocb->u.isp24.flags) & NOTIFY24XX_FLAGS_FCSP)) {
4781 ql_dbg(ql_dbg_disc, vha, 0xffff,
4782 "%s %d Term INOT due to app not available lid=%d, NportID %06X ",
4783 __func__, __LINE__, loop_id, port_id.b24);
4784 qlt_send_term_imm_notif(vha, iocb, 1);
4788 if (vha->hw->flags.edif_enabled) {
4789 if (DBELL_INACTIVE(vha)) {
4790 ql_dbg(ql_dbg_disc, vha, 0xffff,
4791 "%s %d Term INOT due to app not started lid=%d, NportID %06X ",
4792 __func__, __LINE__, loop_id, port_id.b24);
4793 qlt_send_term_imm_notif(vha, iocb, 1);
4795 } else if (iocb->u.isp24.status_subcode == ELS_PLOGI &&
4796 !(le16_to_cpu(iocb->u.isp24.flags) & NOTIFY24XX_FLAGS_FCSP)) {
4797 ql_dbg(ql_dbg_disc, vha, 0xffff,
4798 "%s %d Term INOT due to unsecure lid=%d, NportID %06X ",
4799 __func__, __LINE__, loop_id, port_id.b24);
4800 qlt_send_term_imm_notif(vha, iocb, 1);
4805 pla = qlt_plogi_ack_find_add(vha, &port_id, iocb);
4807 ql_dbg(ql_dbg_disc + ql_dbg_verbose, vha, 0xffff,
4808 "%s %d %8phC Term INOT due to mem alloc fail",
4810 iocb->u.isp24.port_name);
4811 qlt_send_term_imm_notif(vha, iocb, 1);
4815 if (conflict_sess) {
4816 conflict_sess->login_gen++;
4817 qlt_plogi_ack_link(vha, pla, conflict_sess,
4818 QLT_PLOGI_LINK_CONFLICT);
4823 ql_dbg(ql_dbg_disc, vha, 0xffff,
4824 "%s %d %8phC post new sess\n",
4825 __func__, __LINE__, iocb->u.isp24.port_name);
4826 if (iocb->u.isp24.status_subcode == ELS_PLOGI)
4827 qla24xx_post_newsess_work(vha, &port_id,
4828 iocb->u.isp24.port_name,
4829 iocb->u.isp24.u.plogi.node_name,
4832 qla24xx_post_newsess_work(vha, &port_id,
4833 iocb->u.isp24.port_name, NULL,
4839 if (sess->disc_state == DSC_UPD_FCPORT) {
4843 * Remote port registration is still going on from
4844 * previous login. Allow it to finish before we
4845 * accept the new login.
4847 sess->next_disc_state = DSC_DELETE_PEND;
4848 sec = jiffies_to_msecs(jiffies -
4849 sess->jiffies_at_registration) / 1000;
4850 if (sess->sec_since_registration < sec && sec &&
4852 sess->sec_since_registration = sec;
4853 ql_dbg(ql_dbg_disc, vha, 0xffff,
4854 "%s %8phC - Slow Rport registration (%d Sec)\n",
4855 __func__, sess->port_name, sec);
4858 if (!conflict_sess) {
4859 list_del(&pla->list);
4860 kmem_cache_free(qla_tgt_plogi_cachep, pla);
4863 qlt_send_term_imm_notif(vha, iocb, 1);
4867 qlt_plogi_ack_link(vha, pla, sess, QLT_PLOGI_LINK_SAME_WWN);
4868 sess->d_id = port_id;
4870 sess->loop_id = loop_id;
4872 if (iocb->u.isp24.status_subcode == ELS_PLOGI) {
4873 /* remote port has assigned Port ID */
4874 if (N2N_TOPO(vha->hw) && fcport_is_bigger(sess))
4875 vha->d_id = sess->d_id;
4877 ql_dbg(ql_dbg_disc, vha, 0xffff,
4878 "%s %8phC - send port online\n",
4879 __func__, sess->port_name);
4881 qla2x00_post_aen_work(vha, FCH_EVT_PORT_ONLINE,
4885 if (iocb->u.isp24.status_subcode == ELS_PRLI) {
4886 sess->fw_login_state = DSC_LS_PRLI_PEND;
4888 sess->loop_id = loop_id;
4889 sess->d_id = port_id;
4890 sess->fw_login_state = DSC_LS_PRLI_PEND;
4891 wd3_lo = le16_to_cpu(iocb->u.isp24.u.prli.wd3_lo);
4894 sess->conf_compl_supported = 1;
4896 if ((wd3_lo & BIT_4) == 0)
4897 sess->port_type = FCT_INITIATOR;
4899 sess->port_type = FCT_TARGET;
4902 sess->fw_login_state = DSC_LS_PLOGI_PEND;
4905 ql_dbg(ql_dbg_disc, vha, 0x20f9,
4906 "%s %d %8phC DS %d\n",
4907 __func__, __LINE__, sess->port_name, sess->disc_state);
4909 switch (sess->disc_state) {
4911 case DSC_LOGIN_PEND:
4912 qlt_plogi_ack_unref(vha, pla);
4917 * Under normal circumstances we want to release nport handle
4918 * during LOGO process to avoid nport handle leaks inside FW.
4919 * The exception is when LOGO is done while another PLOGI with
4920 * the same nport handle is waiting as might be the case here.
4921 * Note: there is always a possibily of a race where session
4922 * deletion has already started for other reasons (e.g. ACL
4923 * removal) and now PLOGI arrives:
4924 * 1. if PLOGI arrived in FW after nport handle has been freed,
4925 * FW must have assigned this PLOGI a new/same handle and we
4926 * can proceed ACK'ing it as usual when session deletion
4928 * 2. if PLOGI arrived in FW before LOGO with LCF_FREE_NPORT
4929 * bit reached it, the handle has now been released. We'll
4930 * get an error when we ACK this PLOGI. Nothing will be sent
4931 * back to initiator. Initiator should eventually retry
4932 * PLOGI and situation will correct itself.
4934 sess->keep_nport_handle = ((sess->loop_id == loop_id) &&
4935 (sess->d_id.b24 == port_id.b24));
4937 ql_dbg(ql_dbg_disc, vha, 0x20f9,
4938 "%s %d %8phC post del sess\n",
4939 __func__, __LINE__, sess->port_name);
4942 qlt_schedule_sess_for_deletion(sess);
4950 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
4952 static int qlt_24xx_handle_els(struct scsi_qla_host *vha,
4953 struct imm_ntfy_from_isp *iocb)
4955 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
4956 struct qla_hw_data *ha = vha->hw;
4957 struct fc_port *sess = NULL, *conflict_sess = NULL;
4963 unsigned long flags;
4965 lockdep_assert_held(&ha->hardware_lock);
4967 wwn = wwn_to_u64(iocb->u.isp24.port_name);
4969 port_id.b.domain = iocb->u.isp24.port_id[2];
4970 port_id.b.area = iocb->u.isp24.port_id[1];
4971 port_id.b.al_pa = iocb->u.isp24.port_id[0];
4972 port_id.b.rsvd_1 = 0;
4974 loop_id = le16_to_cpu(iocb->u.isp24.nport_handle);
4976 ql_dbg(ql_dbg_disc, vha, 0xf026,
4977 "qla_target(%d): Port ID: %02x:%02x:%02x ELS opcode: 0x%02x lid %d %8phC\n",
4978 vha->vp_idx, iocb->u.isp24.port_id[2],
4979 iocb->u.isp24.port_id[1], iocb->u.isp24.port_id[0],
4980 iocb->u.isp24.status_subcode, loop_id,
4981 iocb->u.isp24.port_name);
4983 /* res = 1 means ack at the end of thread
4984 * res = 0 means ack async/later.
4986 switch (iocb->u.isp24.status_subcode) {
4988 res = qlt_handle_login(vha, iocb);
4993 sess = qla2x00_find_fcport_by_wwpn(vha,
4994 iocb->u.isp24.port_name, 1);
4996 if (vha->hw->flags.edif_enabled && sess &&
4997 (!(sess->flags & FCF_FCSP_DEVICE) ||
4998 !sess->edif.authok)) {
4999 ql_dbg(ql_dbg_disc, vha, 0xffff,
5000 "%s %d %8phC Term PRLI due to unauthorize PRLI\n",
5001 __func__, __LINE__, iocb->u.isp24.port_name);
5002 qlt_send_term_imm_notif(vha, iocb, 1);
5006 if (sess && sess->plogi_link[QLT_PLOGI_LINK_SAME_WWN]) {
5007 ql_dbg(ql_dbg_disc, vha, 0xffff,
5008 "%s %d %8phC Term PRLI due to PLOGI ACK not completed\n",
5010 iocb->u.isp24.port_name);
5011 qlt_send_term_imm_notif(vha, iocb, 1);
5015 res = qlt_handle_login(vha, iocb);
5019 if (IS_SW_RESV_ADDR(port_id)) {
5024 wd3_lo = le16_to_cpu(iocb->u.isp24.u.prli.wd3_lo);
5027 spin_lock_irqsave(&tgt->ha->tgt.sess_lock, flags);
5028 sess = qlt_find_sess_invalidate_other(vha, wwn, port_id,
5029 loop_id, &conflict_sess);
5030 spin_unlock_irqrestore(&tgt->ha->tgt.sess_lock, flags);
5033 if (conflict_sess) {
5034 switch (conflict_sess->disc_state) {
5036 case DSC_DELETE_PEND:
5039 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf09b,
5040 "PRLI with conflicting sess %p port %8phC\n",
5041 conflict_sess, conflict_sess->port_name);
5042 conflict_sess->fw_login_state =
5043 DSC_LS_PORT_UNAVAIL;
5044 qlt_send_term_imm_notif(vha, iocb, 1);
5051 bool delete = false;
5054 if (vha->hw->flags.edif_enabled && sess &&
5055 (!(sess->flags & FCF_FCSP_DEVICE) ||
5056 !sess->edif.authok)) {
5057 ql_dbg(ql_dbg_disc, vha, 0xffff,
5058 "%s %d %8phC Term PRLI due to unauthorize prli\n",
5059 __func__, __LINE__, iocb->u.isp24.port_name);
5060 qlt_send_term_imm_notif(vha, iocb, 1);
5064 spin_lock_irqsave(&tgt->ha->tgt.sess_lock, flags);
5065 switch (sess->fw_login_state) {
5066 case DSC_LS_PLOGI_PEND:
5067 case DSC_LS_PLOGI_COMP:
5068 case DSC_LS_PRLI_COMP:
5075 switch (sess->disc_state) {
5076 case DSC_UPD_FCPORT:
5077 spin_unlock_irqrestore(&tgt->ha->tgt.sess_lock,
5080 sec = jiffies_to_msecs(jiffies -
5081 sess->jiffies_at_registration)/1000;
5082 if (sess->sec_since_registration < sec && sec &&
5084 sess->sec_since_registration = sec;
5085 ql_dbg(ql_dbg_disc, sess->vha, 0xffff,
5086 "%s %8phC : Slow Rport registration(%d Sec)\n",
5087 __func__, sess->port_name, sec);
5089 qlt_send_term_imm_notif(vha, iocb, 1);
5092 case DSC_LOGIN_PEND:
5094 case DSC_LOGIN_COMPLETE:
5103 spin_unlock_irqrestore(&tgt->ha->tgt.sess_lock,
5106 * Impatient initiator sent PRLI before last
5107 * PLOGI could finish. Will force him to re-try,
5108 * while last one finishes.
5110 ql_log(ql_log_warn, sess->vha, 0xf095,
5111 "sess %p PRLI received, before plogi ack.\n",
5113 qlt_send_term_imm_notif(vha, iocb, 1);
5119 * This shouldn't happen under normal circumstances,
5120 * since we have deleted the old session during PLOGI
5122 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf096,
5123 "PRLI (loop_id %#04x) for existing sess %p (loop_id %#04x)\n",
5124 sess->loop_id, sess, iocb->u.isp24.nport_handle);
5127 sess->loop_id = loop_id;
5128 sess->d_id = port_id;
5129 sess->fw_login_state = DSC_LS_PRLI_PEND;
5132 sess->conf_compl_supported = 1;
5134 if ((wd3_lo & BIT_4) == 0)
5135 sess->port_type = FCT_INITIATOR;
5137 sess->port_type = FCT_TARGET;
5139 spin_unlock_irqrestore(&tgt->ha->tgt.sess_lock, flags);
5141 res = 1; /* send notify ack */
5143 /* Make session global (not used in fabric mode) */
5144 if (ha->current_topology != ISP_CFG_F) {
5146 ql_dbg(ql_dbg_disc, vha, 0x20fa,
5147 "%s %d %8phC post nack\n",
5148 __func__, __LINE__, sess->port_name);
5149 qla24xx_post_nack_work(vha, sess, iocb,
5153 set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
5154 set_bit(LOCAL_LOOP_UPDATE, &vha->dpc_flags);
5155 qla2xxx_wake_dpc(vha);
5159 ql_dbg(ql_dbg_disc, vha, 0x20fb,
5160 "%s %d %8phC post nack\n",
5161 __func__, __LINE__, sess->port_name);
5162 qla24xx_post_nack_work(vha, sess, iocb,
5170 if (le16_to_cpu(iocb->u.isp24.flags) &
5171 NOTIFY24XX_FLAGS_GLOBAL_TPRLO) {
5173 qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS);
5180 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
5181 sess = qla2x00_find_fcport_by_loopid(vha, loop_id);
5182 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
5186 sess->fw_login_state = DSC_LS_LOGO_PEND;
5187 sess->logo_ack_needed = 1;
5188 memcpy(sess->iocb, iocb, IOCB_SIZE);
5191 res = qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS_SESS);
5193 ql_dbg(ql_dbg_disc, vha, 0x20fc,
5194 "%s: logo %llx res %d sess %p ",
5195 __func__, wwn, res, sess);
5198 * cmd went upper layer, look for qlt_xmit_tm_rsp()
5199 * for LOGO_ACK & sess delete
5204 /* cmd did not go to upper layer. */
5206 qlt_schedule_sess_for_deletion(sess);
5209 /* else logo will be ack */
5215 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
5217 if (tgt->link_reinit_iocb_pending) {
5218 qlt_send_notify_ack(ha->base_qpair,
5219 &tgt->link_reinit_iocb, 0, 0, 0, 0, 0, 0);
5220 tgt->link_reinit_iocb_pending = 0;
5223 sess = qla2x00_find_fcport_by_wwpn(vha,
5224 iocb->u.isp24.port_name, 1);
5226 ql_dbg(ql_dbg_disc, vha, 0x20fd,
5227 "sess %p lid %d|%d DS %d LS %d\n",
5228 sess, sess->loop_id, loop_id,
5229 sess->disc_state, sess->fw_login_state);
5232 res = 1; /* send notify ack */
5236 case ELS_FLOGI: /* should never happen */
5238 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf061,
5239 "qla_target(%d): Unsupported ELS command %x "
5240 "received\n", vha->vp_idx, iocb->u.isp24.status_subcode);
5241 res = qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS_SESS);
5245 ql_dbg(ql_dbg_disc, vha, 0xf026,
5246 "qla_target(%d): Exit ELS opcode: 0x%02x res %d\n",
5247 vha->vp_idx, iocb->u.isp24.status_subcode, res);
5253 * ha->hardware_lock supposed to be held on entry.
5254 * Might drop it, then reacquire.
5256 static void qlt_handle_imm_notify(struct scsi_qla_host *vha,
5257 struct imm_ntfy_from_isp *iocb)
5259 struct qla_hw_data *ha = vha->hw;
5260 uint32_t add_flags = 0;
5261 int send_notify_ack = 1;
5264 lockdep_assert_held(&ha->hardware_lock);
5266 status = le16_to_cpu(iocb->u.isp2x.status);
5268 case IMM_NTFY_LIP_RESET:
5270 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf032,
5271 "qla_target(%d): LIP reset (loop %#x), subcode %x\n",
5272 vha->vp_idx, le16_to_cpu(iocb->u.isp24.nport_handle),
5273 iocb->u.isp24.status_subcode);
5275 if (qlt_reset(vha, iocb, QLA_TGT_ABORT_ALL) == 0)
5276 send_notify_ack = 0;
5280 case IMM_NTFY_LIP_LINK_REINIT:
5282 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
5284 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf033,
5285 "qla_target(%d): LINK REINIT (loop %#x, "
5286 "subcode %x)\n", vha->vp_idx,
5287 le16_to_cpu(iocb->u.isp24.nport_handle),
5288 iocb->u.isp24.status_subcode);
5289 if (tgt->link_reinit_iocb_pending) {
5290 qlt_send_notify_ack(ha->base_qpair,
5291 &tgt->link_reinit_iocb, 0, 0, 0, 0, 0, 0);
5293 memcpy(&tgt->link_reinit_iocb, iocb, sizeof(*iocb));
5294 tgt->link_reinit_iocb_pending = 1;
5296 * QLogic requires to wait after LINK REINIT for possible
5297 * PDISC or ADISC ELS commands
5299 send_notify_ack = 0;
5303 case IMM_NTFY_PORT_LOGOUT:
5304 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf034,
5305 "qla_target(%d): Port logout (loop "
5306 "%#x, subcode %x)\n", vha->vp_idx,
5307 le16_to_cpu(iocb->u.isp24.nport_handle),
5308 iocb->u.isp24.status_subcode);
5310 if (qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS_SESS) == 0)
5311 send_notify_ack = 0;
5312 /* The sessions will be cleared in the callback, if needed */
5315 case IMM_NTFY_GLBL_TPRLO:
5316 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf035,
5317 "qla_target(%d): Global TPRLO (%x)\n", vha->vp_idx, status);
5318 if (qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS) == 0)
5319 send_notify_ack = 0;
5320 /* The sessions will be cleared in the callback, if needed */
5323 case IMM_NTFY_PORT_CONFIG:
5324 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf036,
5325 "qla_target(%d): Port config changed (%x)\n", vha->vp_idx,
5327 if (qlt_reset(vha, iocb, QLA_TGT_ABORT_ALL) == 0)
5328 send_notify_ack = 0;
5329 /* The sessions will be cleared in the callback, if needed */
5332 case IMM_NTFY_GLBL_LOGO:
5333 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06a,
5334 "qla_target(%d): Link failure detected\n",
5336 /* I_T nexus loss */
5337 if (qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS) == 0)
5338 send_notify_ack = 0;
5341 case IMM_NTFY_IOCB_OVERFLOW:
5342 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06b,
5343 "qla_target(%d): Cannot provide requested "
5344 "capability (IOCB overflowed the immediate notify "
5345 "resource count)\n", vha->vp_idx);
5348 case IMM_NTFY_ABORT_TASK:
5349 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf037,
5350 "qla_target(%d): Abort Task (S %08x I %#x -> "
5351 "L %#x)\n", vha->vp_idx,
5352 le16_to_cpu(iocb->u.isp2x.seq_id),
5353 GET_TARGET_ID(ha, (struct atio_from_isp *)iocb),
5354 le16_to_cpu(iocb->u.isp2x.lun));
5355 if (qlt_abort_task(vha, iocb) == 0)
5356 send_notify_ack = 0;
5359 case IMM_NTFY_RESOURCE:
5360 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06c,
5361 "qla_target(%d): Out of resources, host %ld\n",
5362 vha->vp_idx, vha->host_no);
5365 case IMM_NTFY_MSG_RX:
5366 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf038,
5367 "qla_target(%d): Immediate notify task %x\n",
5368 vha->vp_idx, iocb->u.isp2x.task_flags);
5372 if (qlt_24xx_handle_els(vha, iocb) == 0)
5373 send_notify_ack = 0;
5376 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06d,
5377 "qla_target(%d): Received unknown immediate "
5378 "notify status %x\n", vha->vp_idx, status);
5382 if (send_notify_ack)
5383 qlt_send_notify_ack(ha->base_qpair, iocb, add_flags, 0, 0, 0,
5388 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
5389 * This function sends busy to ISP 2xxx or 24xx.
5391 static int __qlt_send_busy(struct qla_qpair *qpair,
5392 struct atio_from_isp *atio, uint16_t status)
5394 struct scsi_qla_host *vha = qpair->vha;
5395 struct ctio7_to_24xx *ctio24;
5396 struct qla_hw_data *ha = vha->hw;
5398 struct fc_port *sess = NULL;
5399 unsigned long flags;
5403 id = be_to_port_id(atio->u.isp24.fcp_hdr.s_id);
5405 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
5406 sess = qla2x00_find_fcport_by_nportid(vha, &id, 1);
5407 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
5409 qlt_send_term_exchange(qpair, NULL, atio, 1, 0);
5412 /* Sending marker isn't necessary, since we called from ISR */
5414 pkt = (request_t *)__qla2x00_alloc_iocbs(qpair, NULL);
5416 ql_dbg(ql_dbg_io, vha, 0x3063,
5417 "qla_target(%d): %s failed: unable to allocate "
5418 "request packet", vha->vp_idx, __func__);
5422 qpair->tgt_counters.num_q_full_sent++;
5423 pkt->entry_count = 1;
5424 pkt->handle = QLA_TGT_SKIP_HANDLE | CTIO_COMPLETION_HANDLE_MARK;
5426 ctio24 = (struct ctio7_to_24xx *)pkt;
5427 ctio24->entry_type = CTIO_TYPE7;
5428 ctio24->nport_handle = cpu_to_le16(sess->loop_id);
5429 ctio24->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
5430 ctio24->vp_index = vha->vp_idx;
5431 ctio24->initiator_id = be_id_to_le(atio->u.isp24.fcp_hdr.s_id);
5432 ctio24->exchange_addr = atio->u.isp24.exchange_addr;
5433 temp = (atio->u.isp24.attr << 9) |
5434 CTIO7_FLAGS_STATUS_MODE_1 | CTIO7_FLAGS_SEND_STATUS |
5435 CTIO7_FLAGS_DONT_RET_CTIO;
5436 ctio24->u.status1.flags = cpu_to_le16(temp);
5438 * CTIO from fw w/o se_cmd doesn't provide enough info to retry it,
5439 * if the explicit conformation is used.
5441 ctio24->u.status1.ox_id =
5442 cpu_to_le16(be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id));
5443 ctio24->u.status1.scsi_status = cpu_to_le16(status);
5445 ctio24->u.status1.residual = cpu_to_le32(get_datalen_for_atio(atio));
5447 if (ctio24->u.status1.residual != 0)
5448 ctio24->u.status1.scsi_status |= cpu_to_le16(SS_RESIDUAL_UNDER);
5450 /* Memory Barrier */
5452 if (qpair->reqq_start_iocbs)
5453 qpair->reqq_start_iocbs(qpair);
5455 qla2x00_start_iocbs(vha, qpair->req);
5460 * This routine is used to allocate a command for either a QFull condition
5461 * (ie reply SAM_STAT_BUSY) or to terminate an exchange that did not go
5465 qlt_alloc_qfull_cmd(struct scsi_qla_host *vha,
5466 struct atio_from_isp *atio, uint16_t status, int qfull)
5468 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
5469 struct qla_hw_data *ha = vha->hw;
5470 struct fc_port *sess;
5471 struct qla_tgt_cmd *cmd;
5472 unsigned long flags;
5474 if (unlikely(tgt->tgt_stop)) {
5475 ql_dbg(ql_dbg_io, vha, 0x300a,
5476 "New command while device %p is shutting down\n", tgt);
5480 if ((vha->hw->tgt.num_qfull_cmds_alloc + 1) > MAX_QFULL_CMDS_ALLOC) {
5481 vha->hw->tgt.num_qfull_cmds_dropped++;
5482 if (vha->hw->tgt.num_qfull_cmds_dropped >
5483 vha->qla_stats.stat_max_qfull_cmds_dropped)
5484 vha->qla_stats.stat_max_qfull_cmds_dropped =
5485 vha->hw->tgt.num_qfull_cmds_dropped;
5487 ql_dbg(ql_dbg_io, vha, 0x3068,
5488 "qla_target(%d): %s: QFull CMD dropped[%d]\n",
5489 vha->vp_idx, __func__,
5490 vha->hw->tgt.num_qfull_cmds_dropped);
5492 qlt_chk_exch_leak_thresh_hold(vha);
5496 sess = ha->tgt.tgt_ops->find_sess_by_s_id
5497 (vha, atio->u.isp24.fcp_hdr.s_id);
5501 cmd = ha->tgt.tgt_ops->get_cmd(sess);
5503 ql_dbg(ql_dbg_io, vha, 0x3009,
5504 "qla_target(%d): %s: Allocation of cmd failed\n",
5505 vha->vp_idx, __func__);
5507 vha->hw->tgt.num_qfull_cmds_dropped++;
5508 if (vha->hw->tgt.num_qfull_cmds_dropped >
5509 vha->qla_stats.stat_max_qfull_cmds_dropped)
5510 vha->qla_stats.stat_max_qfull_cmds_dropped =
5511 vha->hw->tgt.num_qfull_cmds_dropped;
5513 qlt_chk_exch_leak_thresh_hold(vha);
5517 qlt_incr_num_pend_cmds(vha);
5518 INIT_LIST_HEAD(&cmd->cmd_list);
5519 memcpy(&cmd->atio, atio, sizeof(*atio));
5521 cmd->tgt = vha->vha_tgt.qla_tgt;
5523 cmd->reset_count = ha->base_qpair->chip_reset;
5525 cmd->qpair = ha->base_qpair;
5529 /* NOTE: borrowing the state field to carry the status */
5530 cmd->state = status;
5532 cmd->term_exchg = 1;
5534 spin_lock_irqsave(&vha->hw->tgt.q_full_lock, flags);
5535 list_add_tail(&cmd->cmd_list, &vha->hw->tgt.q_full_list);
5537 vha->hw->tgt.num_qfull_cmds_alloc++;
5538 if (vha->hw->tgt.num_qfull_cmds_alloc >
5539 vha->qla_stats.stat_max_qfull_cmds_alloc)
5540 vha->qla_stats.stat_max_qfull_cmds_alloc =
5541 vha->hw->tgt.num_qfull_cmds_alloc;
5542 spin_unlock_irqrestore(&vha->hw->tgt.q_full_lock, flags);
5546 qlt_free_qfull_cmds(struct qla_qpair *qpair)
5548 struct scsi_qla_host *vha = qpair->vha;
5549 struct qla_hw_data *ha = vha->hw;
5550 unsigned long flags;
5551 struct qla_tgt_cmd *cmd, *tcmd;
5552 struct list_head free_list, q_full_list;
5555 if (list_empty(&ha->tgt.q_full_list))
5558 INIT_LIST_HEAD(&free_list);
5559 INIT_LIST_HEAD(&q_full_list);
5561 spin_lock_irqsave(&vha->hw->tgt.q_full_lock, flags);
5562 if (list_empty(&ha->tgt.q_full_list)) {
5563 spin_unlock_irqrestore(&vha->hw->tgt.q_full_lock, flags);
5567 list_splice_init(&vha->hw->tgt.q_full_list, &q_full_list);
5568 spin_unlock_irqrestore(&vha->hw->tgt.q_full_lock, flags);
5570 spin_lock_irqsave(qpair->qp_lock_ptr, flags);
5571 list_for_each_entry_safe(cmd, tcmd, &q_full_list, cmd_list) {
5573 /* cmd->state is a borrowed field to hold status */
5574 rc = __qlt_send_busy(qpair, &cmd->atio, cmd->state);
5575 else if (cmd->term_exchg)
5576 rc = __qlt_send_term_exchange(qpair, NULL, &cmd->atio);
5582 ql_dbg(ql_dbg_io, vha, 0x3006,
5583 "%s: busy sent for ox_id[%04x]\n", __func__,
5584 be16_to_cpu(cmd->atio.u.isp24.fcp_hdr.ox_id));
5585 else if (cmd->term_exchg)
5586 ql_dbg(ql_dbg_io, vha, 0x3007,
5587 "%s: Term exchg sent for ox_id[%04x]\n", __func__,
5588 be16_to_cpu(cmd->atio.u.isp24.fcp_hdr.ox_id));
5590 ql_dbg(ql_dbg_io, vha, 0x3008,
5591 "%s: Unexpected cmd in QFull list %p\n", __func__,
5594 list_move_tail(&cmd->cmd_list, &free_list);
5596 /* piggy back on hardware_lock for protection */
5597 vha->hw->tgt.num_qfull_cmds_alloc--;
5599 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
5603 list_for_each_entry_safe(cmd, tcmd, &free_list, cmd_list) {
5604 list_del(&cmd->cmd_list);
5605 /* This cmd was never sent to TCM. There is no need
5606 * to schedule free or call free_cmd
5611 if (!list_empty(&q_full_list)) {
5612 spin_lock_irqsave(&vha->hw->tgt.q_full_lock, flags);
5613 list_splice(&q_full_list, &vha->hw->tgt.q_full_list);
5614 spin_unlock_irqrestore(&vha->hw->tgt.q_full_lock, flags);
5621 qlt_send_busy(struct qla_qpair *qpair, struct atio_from_isp *atio,
5625 struct scsi_qla_host *vha = qpair->vha;
5627 rc = __qlt_send_busy(qpair, atio, status);
5629 qlt_alloc_qfull_cmd(vha, atio, status, 1);
5633 qlt_chk_qfull_thresh_hold(struct scsi_qla_host *vha, struct qla_qpair *qpair,
5634 struct atio_from_isp *atio, uint8_t ha_locked)
5636 struct qla_hw_data *ha = vha->hw;
5637 unsigned long flags;
5639 if (ha->tgt.num_pend_cmds < Q_FULL_THRESH_HOLD(ha))
5643 spin_lock_irqsave(&ha->hardware_lock, flags);
5644 qlt_send_busy(qpair, atio, qla_sam_status);
5646 spin_unlock_irqrestore(&ha->hardware_lock, flags);
5651 /* ha->hardware_lock supposed to be held on entry */
5652 /* called via callback from qla2xxx */
5653 static void qlt_24xx_atio_pkt(struct scsi_qla_host *vha,
5654 struct atio_from_isp *atio, uint8_t ha_locked)
5656 struct qla_hw_data *ha = vha->hw;
5657 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
5659 unsigned long flags = 0;
5661 if (unlikely(tgt == NULL)) {
5662 ql_dbg(ql_dbg_tgt, vha, 0x3064,
5663 "ATIO pkt, but no tgt (ha %p)", ha);
5667 * In tgt_stop mode we also should allow all requests to pass.
5668 * Otherwise, some commands can stuck.
5671 tgt->atio_irq_cmd_count++;
5673 switch (atio->u.raw.entry_type) {
5675 if (unlikely(atio->u.isp24.exchange_addr ==
5676 cpu_to_le32(ATIO_EXCHANGE_ADDRESS_UNKNOWN))) {
5677 ql_dbg(ql_dbg_io, vha, 0x3065,
5678 "qla_target(%d): ATIO_TYPE7 "
5679 "received with UNKNOWN exchange address, "
5680 "sending QUEUE_FULL\n", vha->vp_idx);
5682 spin_lock_irqsave(&ha->hardware_lock, flags);
5683 qlt_send_busy(ha->base_qpair, atio, qla_sam_status);
5685 spin_unlock_irqrestore(&ha->hardware_lock,
5690 if (likely(atio->u.isp24.fcp_cmnd.task_mgmt_flags == 0)) {
5691 rc = qlt_chk_qfull_thresh_hold(vha, ha->base_qpair,
5694 tgt->atio_irq_cmd_count--;
5697 rc = qlt_handle_cmd_for_atio(vha, atio);
5699 rc = qlt_handle_task_mgmt(vha, atio);
5701 if (unlikely(rc != 0)) {
5703 spin_lock_irqsave(&ha->hardware_lock, flags);
5706 ql_dbg(ql_dbg_tgt, vha, 0xe05f,
5707 "qla_target: Unable to send command to target\n");
5710 ql_dbg(ql_dbg_tgt, vha, 0xe05f,
5711 "qla_target: Unable to send command to target, sending TERM EXCHANGE for rsp\n");
5712 qlt_send_term_exchange(ha->base_qpair, NULL,
5716 ql_dbg(ql_dbg_tgt, vha, 0xe060,
5717 "qla_target(%d): Unable to send command to target, sending BUSY status\n",
5719 qlt_send_busy(ha->base_qpair, atio,
5723 ql_dbg(ql_dbg_tgt, vha, 0xe060,
5724 "qla_target(%d): Unable to send command to target, sending BUSY status\n",
5726 qlt_send_busy(ha->base_qpair, atio,
5731 spin_unlock_irqrestore(&ha->hardware_lock,
5736 case IMMED_NOTIFY_TYPE:
5738 if (unlikely(atio->u.isp2x.entry_status != 0)) {
5739 ql_dbg(ql_dbg_tgt, vha, 0xe05b,
5740 "qla_target(%d): Received ATIO packet %x "
5741 "with error status %x\n", vha->vp_idx,
5742 atio->u.raw.entry_type,
5743 atio->u.isp2x.entry_status);
5746 ql_dbg(ql_dbg_tgt, vha, 0xe02e, "%s", "IMMED_NOTIFY ATIO");
5749 spin_lock_irqsave(&ha->hardware_lock, flags);
5750 qlt_handle_imm_notify(vha, (struct imm_ntfy_from_isp *)atio);
5752 spin_unlock_irqrestore(&ha->hardware_lock, flags);
5757 ql_dbg(ql_dbg_tgt, vha, 0xe05c,
5758 "qla_target(%d): Received unknown ATIO atio "
5759 "type %x\n", vha->vp_idx, atio->u.raw.entry_type);
5763 tgt->atio_irq_cmd_count--;
5767 * qpair lock is assume to be held
5768 * rc = 0 : send terminate & abts respond
5769 * rc != 0: do not send term & abts respond
5771 static int qlt_chk_unresolv_exchg(struct scsi_qla_host *vha,
5772 struct qla_qpair *qpair, struct abts_resp_from_24xx_fw *entry)
5774 struct qla_hw_data *ha = vha->hw;
5778 * Detect unresolved exchange. If the same ABTS is unable
5779 * to terminate an existing command and the same ABTS loops
5780 * between FW & Driver, then force FW dump. Under 1 jiff,
5781 * we should see multiple loops.
5783 if (qpair->retry_term_exchg_addr == entry->exchange_addr_to_abort &&
5784 qpair->retry_term_jiff == jiffies) {
5785 /* found existing exchange */
5786 qpair->retry_term_cnt++;
5787 if (qpair->retry_term_cnt >= 5) {
5789 qpair->retry_term_cnt = 0;
5790 ql_log(ql_log_warn, vha, 0xffff,
5791 "Unable to send ABTS Respond. Dumping firmware.\n");
5792 ql_dump_buffer(ql_dbg_tgt_mgt + ql_dbg_buffer,
5793 vha, 0xffff, (uint8_t *)entry, sizeof(*entry));
5795 if (qpair == ha->base_qpair)
5796 ha->isp_ops->fw_dump(vha);
5798 qla2xxx_dump_fw(vha);
5800 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
5801 qla2xxx_wake_dpc(vha);
5803 } else if (qpair->retry_term_jiff != jiffies) {
5804 qpair->retry_term_exchg_addr = entry->exchange_addr_to_abort;
5805 qpair->retry_term_cnt = 0;
5806 qpair->retry_term_jiff = jiffies;
5813 static void qlt_handle_abts_completion(struct scsi_qla_host *vha,
5814 struct rsp_que *rsp, response_t *pkt)
5816 struct abts_resp_from_24xx_fw *entry =
5817 (struct abts_resp_from_24xx_fw *)pkt;
5818 u32 h = pkt->handle & ~QLA_TGT_HANDLE_MASK;
5819 struct qla_tgt_mgmt_cmd *mcmd;
5820 struct qla_hw_data *ha = vha->hw;
5822 mcmd = qlt_ctio_to_cmd(vha, rsp, pkt->handle, pkt);
5823 if (mcmd == NULL && h != QLA_TGT_SKIP_HANDLE) {
5824 ql_dbg(ql_dbg_async, vha, 0xe064,
5825 "qla_target(%d): ABTS Comp without mcmd\n",
5832 vha->vha_tgt.qla_tgt->abts_resp_expected--;
5834 ql_dbg(ql_dbg_tgt, vha, 0xe038,
5835 "ABTS_RESP_24XX: compl_status %x\n",
5836 entry->compl_status);
5838 if (le16_to_cpu(entry->compl_status) != ABTS_RESP_COMPL_SUCCESS) {
5839 if (le32_to_cpu(entry->error_subcode1) == 0x1E &&
5840 le32_to_cpu(entry->error_subcode2) == 0) {
5841 if (qlt_chk_unresolv_exchg(vha, rsp->qpair, entry)) {
5842 ha->tgt.tgt_ops->free_mcmd(mcmd);
5845 qlt_24xx_retry_term_exchange(vha, rsp->qpair,
5848 ql_dbg(ql_dbg_tgt, vha, 0xe063,
5849 "qla_target(%d): ABTS_RESP_24XX failed %x (subcode %x:%x)",
5850 vha->vp_idx, entry->compl_status,
5851 entry->error_subcode1,
5852 entry->error_subcode2);
5853 ha->tgt.tgt_ops->free_mcmd(mcmd);
5856 ha->tgt.tgt_ops->free_mcmd(mcmd);
5860 /* ha->hardware_lock supposed to be held on entry */
5861 /* called via callback from qla2xxx */
5862 static void qlt_response_pkt(struct scsi_qla_host *vha,
5863 struct rsp_que *rsp, response_t *pkt)
5865 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
5867 if (unlikely(tgt == NULL)) {
5868 ql_dbg(ql_dbg_tgt, vha, 0xe05d,
5869 "qla_target(%d): Response pkt %x received, but no tgt (ha %p)\n",
5870 vha->vp_idx, pkt->entry_type, vha->hw);
5875 * In tgt_stop mode we also should allow all requests to pass.
5876 * Otherwise, some commands can stuck.
5879 switch (pkt->entry_type) {
5883 struct ctio7_from_24xx *entry = (struct ctio7_from_24xx *)pkt;
5885 qlt_do_ctio_completion(vha, rsp, entry->handle,
5886 le16_to_cpu(entry->status)|(pkt->entry_status << 16),
5891 case ACCEPT_TGT_IO_TYPE:
5893 struct atio_from_isp *atio = (struct atio_from_isp *)pkt;
5896 if (atio->u.isp2x.status !=
5897 cpu_to_le16(ATIO_CDB_VALID)) {
5898 ql_dbg(ql_dbg_tgt, vha, 0xe05e,
5899 "qla_target(%d): ATIO with error "
5900 "status %x received\n", vha->vp_idx,
5901 le16_to_cpu(atio->u.isp2x.status));
5905 rc = qlt_chk_qfull_thresh_hold(vha, rsp->qpair, atio, 1);
5909 rc = qlt_handle_cmd_for_atio(vha, atio);
5910 if (unlikely(rc != 0)) {
5913 ql_dbg(ql_dbg_tgt, vha, 0xe05f,
5914 "qla_target: Unable to send command to target\n");
5917 ql_dbg(ql_dbg_tgt, vha, 0xe05f,
5918 "qla_target: Unable to send command to target, sending TERM EXCHANGE for rsp\n");
5919 qlt_send_term_exchange(rsp->qpair, NULL,
5923 ql_dbg(ql_dbg_tgt, vha, 0xe060,
5924 "qla_target(%d): Unable to send command to target, sending BUSY status\n",
5926 qlt_send_busy(rsp->qpair, atio,
5930 ql_dbg(ql_dbg_tgt, vha, 0xe060,
5931 "qla_target(%d): Unable to send command to target, sending BUSY status\n",
5933 qlt_send_busy(rsp->qpair, atio,
5941 case CONTINUE_TGT_IO_TYPE:
5943 struct ctio_to_2xxx *entry = (struct ctio_to_2xxx *)pkt;
5945 qlt_do_ctio_completion(vha, rsp, entry->handle,
5946 le16_to_cpu(entry->status)|(pkt->entry_status << 16),
5953 struct ctio_to_2xxx *entry = (struct ctio_to_2xxx *)pkt;
5955 qlt_do_ctio_completion(vha, rsp, entry->handle,
5956 le16_to_cpu(entry->status)|(pkt->entry_status << 16),
5961 case IMMED_NOTIFY_TYPE:
5962 ql_dbg(ql_dbg_tgt, vha, 0xe035, "%s", "IMMED_NOTIFY\n");
5963 qlt_handle_imm_notify(vha, (struct imm_ntfy_from_isp *)pkt);
5966 case NOTIFY_ACK_TYPE:
5967 if (tgt->notify_ack_expected > 0) {
5968 struct nack_to_isp *entry = (struct nack_to_isp *)pkt;
5970 ql_dbg(ql_dbg_tgt, vha, 0xe036,
5971 "NOTIFY_ACK seq %08x status %x\n",
5972 le16_to_cpu(entry->u.isp2x.seq_id),
5973 le16_to_cpu(entry->u.isp2x.status));
5974 tgt->notify_ack_expected--;
5975 if (entry->u.isp2x.status !=
5976 cpu_to_le16(NOTIFY_ACK_SUCCESS)) {
5977 ql_dbg(ql_dbg_tgt, vha, 0xe061,
5978 "qla_target(%d): NOTIFY_ACK "
5979 "failed %x\n", vha->vp_idx,
5980 le16_to_cpu(entry->u.isp2x.status));
5983 ql_dbg(ql_dbg_tgt, vha, 0xe062,
5984 "qla_target(%d): Unexpected NOTIFY_ACK received\n",
5989 case ABTS_RECV_24XX:
5990 ql_dbg(ql_dbg_tgt, vha, 0xe037,
5991 "ABTS_RECV_24XX: instance %d\n", vha->vp_idx);
5992 qlt_24xx_handle_abts(vha, (struct abts_recv_from_24xx *)pkt);
5995 case ABTS_RESP_24XX:
5996 if (tgt->abts_resp_expected > 0) {
5997 qlt_handle_abts_completion(vha, rsp, pkt);
5999 ql_dbg(ql_dbg_tgt, vha, 0xe064,
6000 "qla_target(%d): Unexpected ABTS_RESP_24XX "
6001 "received\n", vha->vp_idx);
6006 ql_dbg(ql_dbg_tgt, vha, 0xe065,
6007 "qla_target(%d): Received unknown response pkt "
6008 "type %x\n", vha->vp_idx, pkt->entry_type);
6015 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
6017 void qlt_async_event(uint16_t code, struct scsi_qla_host *vha,
6020 struct qla_hw_data *ha = vha->hw;
6021 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
6024 if (!tgt || tgt->tgt_stop || tgt->tgt_stopped)
6027 if (((code == MBA_POINT_TO_POINT) || (code == MBA_CHG_IN_CONNECTION)) &&
6031 * In tgt_stop mode we also should allow all requests to pass.
6032 * Otherwise, some commands can stuck.
6037 case MBA_RESET: /* Reset */
6038 case MBA_SYSTEM_ERR: /* System Error */
6039 case MBA_REQ_TRANSFER_ERR: /* Request Transfer Error */
6040 case MBA_RSP_TRANSFER_ERR: /* Response Transfer Error */
6041 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03a,
6042 "qla_target(%d): System error async event %#x "
6043 "occurred", vha->vp_idx, code);
6045 case MBA_WAKEUP_THRES: /* Request Queue Wake-up. */
6046 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
6051 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03b,
6052 "qla_target(%d): Async LOOP_UP occurred "
6053 "(m[0]=%x, m[1]=%x, m[2]=%x, m[3]=%x)", vha->vp_idx,
6054 mailbox[0], mailbox[1], mailbox[2], mailbox[3]);
6055 if (tgt->link_reinit_iocb_pending) {
6056 qlt_send_notify_ack(ha->base_qpair,
6057 &tgt->link_reinit_iocb,
6059 tgt->link_reinit_iocb_pending = 0;
6064 case MBA_LIP_OCCURRED:
6067 case MBA_RSCN_UPDATE:
6068 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03c,
6069 "qla_target(%d): Async event %#x occurred "
6070 "(m[0]=%x, m[1]=%x, m[2]=%x, m[3]=%x)", vha->vp_idx, code,
6071 mailbox[0], mailbox[1], mailbox[2], mailbox[3]);
6074 case MBA_REJECTED_FCP_CMD:
6075 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf017,
6076 "qla_target(%d): Async event LS_REJECT occurred (m[0]=%x, m[1]=%x, m[2]=%x, m[3]=%x)",
6078 mailbox[0], mailbox[1], mailbox[2], mailbox[3]);
6080 if (mailbox[3] == 1) {
6081 /* exchange starvation. */
6082 vha->hw->exch_starvation++;
6083 if (vha->hw->exch_starvation > 5) {
6084 ql_log(ql_log_warn, vha, 0xd03a,
6085 "Exchange starvation-. Resetting RISC\n");
6087 vha->hw->exch_starvation = 0;
6088 if (IS_P3P_TYPE(vha->hw))
6089 set_bit(FCOE_CTX_RESET_NEEDED,
6092 set_bit(ISP_ABORT_NEEDED,
6094 qla2xxx_wake_dpc(vha);
6099 case MBA_PORT_UPDATE:
6100 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03d,
6101 "qla_target(%d): Port update async event %#x "
6102 "occurred: updating the ports database (m[0]=%x, m[1]=%x, "
6103 "m[2]=%x, m[3]=%x)", vha->vp_idx, code,
6104 mailbox[0], mailbox[1], mailbox[2], mailbox[3]);
6106 login_code = mailbox[2];
6107 if (login_code == 0x4) {
6108 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03e,
6109 "Async MB 2: Got PLOGI Complete\n");
6110 vha->hw->exch_starvation = 0;
6111 } else if (login_code == 0x7)
6112 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03f,
6113 "Async MB 2: Port Logged Out\n");
6121 static fc_port_t *qlt_get_port_database(struct scsi_qla_host *vha,
6124 fc_port_t *fcport, *tfcp, *del;
6126 unsigned long flags;
6129 fcport = qla2x00_alloc_fcport(vha, GFP_KERNEL);
6131 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06f,
6132 "qla_target(%d): Allocation of tmp FC port failed",
6137 fcport->loop_id = loop_id;
6139 rc = qla24xx_gpdb_wait(vha, fcport, 0);
6140 if (rc != QLA_SUCCESS) {
6141 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf070,
6142 "qla_target(%d): Failed to retrieve fcport "
6143 "information -- get_port_database() returned %x "
6144 "(loop_id=0x%04x)", vha->vp_idx, rc, loop_id);
6150 spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
6151 tfcp = qla2x00_find_fcport_by_wwpn(vha, fcport->port_name, 1);
6154 tfcp->d_id = fcport->d_id;
6155 tfcp->port_type = fcport->port_type;
6156 tfcp->supported_classes = fcport->supported_classes;
6157 tfcp->flags |= fcport->flags;
6158 tfcp->scan_state = QLA_FCPORT_FOUND;
6163 if (vha->hw->current_topology == ISP_CFG_F)
6164 fcport->flags |= FCF_FABRIC_DEVICE;
6166 list_add_tail(&fcport->list, &vha->vp_fcports);
6167 if (!IS_SW_RESV_ADDR(fcport->d_id))
6168 vha->fcport_count++;
6169 fcport->login_gen++;
6170 qla2x00_set_fcport_disc_state(fcport, DSC_LOGIN_COMPLETE);
6171 fcport->login_succ = 1;
6175 fcport->deleted = 0;
6176 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
6178 switch (vha->host->active_mode) {
6179 case MODE_INITIATOR:
6182 if (!IS_IIDMA_CAPABLE(vha->hw) || !vha->hw->flags.gpsc_supported) {
6183 qla24xx_sched_upd_fcport(fcport);
6185 ql_dbg(ql_dbg_disc, vha, 0x20ff,
6186 "%s %d %8phC post gpsc fcp_cnt %d\n",
6187 __func__, __LINE__, fcport->port_name, vha->fcport_count);
6188 qla24xx_post_gpsc_work(vha, fcport);
6198 qla2x00_free_fcport(del);
6203 /* Must be called under tgt_mutex */
6204 static struct fc_port *qlt_make_local_sess(struct scsi_qla_host *vha,
6207 struct fc_port *sess = NULL;
6208 fc_port_t *fcport = NULL;
6209 int rc, global_resets;
6210 uint16_t loop_id = 0;
6212 if (s_id.domain == 0xFF && s_id.area == 0xFC) {
6214 * This is Domain Controller, so it should be
6215 * OK to drop SCSI commands from it.
6217 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf042,
6218 "Unable to find initiator with S_ID %x:%x:%x",
6219 s_id.domain, s_id.area, s_id.al_pa);
6223 mutex_lock(&vha->vha_tgt.tgt_mutex);
6227 atomic_read(&vha->vha_tgt.qla_tgt->tgt_global_resets_count);
6229 rc = qla24xx_get_loop_id(vha, s_id, &loop_id);
6231 mutex_unlock(&vha->vha_tgt.tgt_mutex);
6233 ql_log(ql_log_info, vha, 0xf071,
6234 "qla_target(%d): Unable to find "
6235 "initiator with S_ID %x:%x:%x",
6236 vha->vp_idx, s_id.domain, s_id.area, s_id.al_pa);
6238 if (rc == -ENOENT) {
6239 qlt_port_logo_t logo;
6241 logo.id = be_to_port_id(s_id);
6243 qlt_send_first_logo(vha, &logo);
6249 fcport = qlt_get_port_database(vha, loop_id);
6251 mutex_unlock(&vha->vha_tgt.tgt_mutex);
6255 if (global_resets !=
6256 atomic_read(&vha->vha_tgt.qla_tgt->tgt_global_resets_count)) {
6257 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf043,
6258 "qla_target(%d): global reset during session discovery "
6259 "(counter was %d, new %d), retrying", vha->vp_idx,
6261 atomic_read(&vha->vha_tgt.
6262 qla_tgt->tgt_global_resets_count));
6266 sess = qlt_create_sess(vha, fcport, true);
6268 mutex_unlock(&vha->vha_tgt.tgt_mutex);
6273 static void qlt_abort_work(struct qla_tgt *tgt,
6274 struct qla_tgt_sess_work_param *prm)
6276 struct scsi_qla_host *vha = tgt->vha;
6277 struct qla_hw_data *ha = vha->hw;
6278 struct fc_port *sess = NULL;
6279 unsigned long flags = 0, flags2 = 0;
6283 spin_lock_irqsave(&ha->tgt.sess_lock, flags2);
6288 s_id = le_id_to_be(prm->abts.fcp_hdr_le.s_id);
6290 sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha, s_id);
6292 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags2);
6294 sess = qlt_make_local_sess(vha, s_id);
6295 /* sess has got an extra creation ref */
6297 spin_lock_irqsave(&ha->tgt.sess_lock, flags2);
6301 if (sess->deleted) {
6306 if (!kref_get_unless_zero(&sess->sess_kref)) {
6307 ql_dbg(ql_dbg_tgt_tmr, vha, 0xf01c,
6308 "%s: kref_get fail %8phC \n",
6309 __func__, sess->port_name);
6315 rc = __qlt_24xx_handle_abts(vha, &prm->abts, sess);
6316 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags2);
6318 ha->tgt.tgt_ops->put_sess(sess);
6325 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags2);
6328 spin_lock_irqsave(&ha->hardware_lock, flags);
6329 qlt_24xx_send_abts_resp(ha->base_qpair, &prm->abts,
6330 FCP_TMF_REJECTED, false);
6331 spin_unlock_irqrestore(&ha->hardware_lock, flags);
6334 static void qlt_tmr_work(struct qla_tgt *tgt,
6335 struct qla_tgt_sess_work_param *prm)
6337 struct atio_from_isp *a = &prm->tm_iocb2;
6338 struct scsi_qla_host *vha = tgt->vha;
6339 struct qla_hw_data *ha = vha->hw;
6340 struct fc_port *sess;
6341 unsigned long flags;
6348 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
6353 s_id = prm->tm_iocb2.u.isp24.fcp_hdr.s_id;
6354 sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha, s_id);
6356 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
6358 sess = qlt_make_local_sess(vha, s_id);
6359 /* sess has got an extra creation ref */
6361 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
6365 if (sess->deleted) {
6369 if (!kref_get_unless_zero(&sess->sess_kref)) {
6370 ql_dbg(ql_dbg_tgt_tmr, vha, 0xf020,
6371 "%s: kref_get fail %8phC\n",
6372 __func__, sess->port_name);
6378 fn = a->u.isp24.fcp_cmnd.task_mgmt_flags;
6380 scsilun_to_int((struct scsi_lun *)&a->u.isp24.fcp_cmnd.lun);
6382 rc = qlt_issue_task_mgmt(sess, unpacked_lun, fn, iocb, 0);
6383 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
6385 ha->tgt.tgt_ops->put_sess(sess);
6392 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
6394 qlt_send_term_exchange(ha->base_qpair, NULL, &prm->tm_iocb2, 1, 0);
6397 static void qlt_sess_work_fn(struct work_struct *work)
6399 struct qla_tgt *tgt = container_of(work, struct qla_tgt, sess_work);
6400 struct scsi_qla_host *vha = tgt->vha;
6401 unsigned long flags;
6403 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf000, "Sess work (tgt %p)", tgt);
6405 spin_lock_irqsave(&tgt->sess_work_lock, flags);
6406 while (!list_empty(&tgt->sess_works_list)) {
6407 struct qla_tgt_sess_work_param *prm = list_entry(
6408 tgt->sess_works_list.next, typeof(*prm),
6409 sess_works_list_entry);
6412 * This work can be scheduled on several CPUs at time, so we
6413 * must delete the entry to eliminate double processing
6415 list_del(&prm->sess_works_list_entry);
6417 spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
6419 switch (prm->type) {
6420 case QLA_TGT_SESS_WORK_ABORT:
6421 qlt_abort_work(tgt, prm);
6423 case QLA_TGT_SESS_WORK_TM:
6424 qlt_tmr_work(tgt, prm);
6431 spin_lock_irqsave(&tgt->sess_work_lock, flags);
6435 spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
6438 /* Must be called under tgt_host_action_mutex */
6439 int qlt_add_target(struct qla_hw_data *ha, struct scsi_qla_host *base_vha)
6441 struct qla_tgt *tgt;
6443 struct qla_qpair_hint *h;
6445 if (!QLA_TGT_MODE_ENABLED())
6448 if (!IS_TGT_MODE_CAPABLE(ha)) {
6449 ql_log(ql_log_warn, base_vha, 0xe070,
6450 "This adapter does not support target mode.\n");
6454 ql_dbg(ql_dbg_tgt, base_vha, 0xe03b,
6455 "Registering target for host %ld(%p).\n", base_vha->host_no, ha);
6457 BUG_ON(base_vha->vha_tgt.qla_tgt != NULL);
6459 tgt = kzalloc(sizeof(struct qla_tgt), GFP_KERNEL);
6461 ql_dbg(ql_dbg_tgt, base_vha, 0xe066,
6462 "Unable to allocate struct qla_tgt\n");
6466 tgt->qphints = kcalloc(ha->max_qpairs + 1,
6467 sizeof(struct qla_qpair_hint),
6469 if (!tgt->qphints) {
6471 ql_log(ql_log_warn, base_vha, 0x0197,
6472 "Unable to allocate qpair hints.\n");
6476 if (!(base_vha->host->hostt->supported_mode & MODE_TARGET))
6477 base_vha->host->hostt->supported_mode |= MODE_TARGET;
6479 rc = btree_init64(&tgt->lun_qpair_map);
6481 kfree(tgt->qphints);
6483 ql_log(ql_log_info, base_vha, 0x0198,
6484 "Unable to initialize lun_qpair_map btree\n");
6487 h = &tgt->qphints[0];
6488 h->qpair = ha->base_qpair;
6489 INIT_LIST_HEAD(&h->hint_elem);
6490 h->cpuid = ha->base_qpair->cpuid;
6491 list_add_tail(&h->hint_elem, &ha->base_qpair->hints_list);
6493 for (i = 0; i < ha->max_qpairs; i++) {
6494 unsigned long flags;
6496 struct qla_qpair *qpair = ha->queue_pair_map[i];
6498 h = &tgt->qphints[i + 1];
6499 INIT_LIST_HEAD(&h->hint_elem);
6502 spin_lock_irqsave(qpair->qp_lock_ptr, flags);
6503 list_add_tail(&h->hint_elem, &qpair->hints_list);
6504 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
6505 h->cpuid = qpair->cpuid;
6510 tgt->vha = base_vha;
6511 init_waitqueue_head(&tgt->waitQ);
6512 INIT_LIST_HEAD(&tgt->del_sess_list);
6513 spin_lock_init(&tgt->sess_work_lock);
6514 INIT_WORK(&tgt->sess_work, qlt_sess_work_fn);
6515 INIT_LIST_HEAD(&tgt->sess_works_list);
6516 atomic_set(&tgt->tgt_global_resets_count, 0);
6518 base_vha->vha_tgt.qla_tgt = tgt;
6520 ql_dbg(ql_dbg_tgt, base_vha, 0xe067,
6521 "qla_target(%d): using 64 Bit PCI addressing",
6524 tgt->sg_tablesize = QLA_TGT_MAX_SG_24XX(base_vha->req->length - 3);
6526 mutex_lock(&qla_tgt_mutex);
6527 list_add_tail(&tgt->tgt_list_entry, &qla_tgt_glist);
6528 mutex_unlock(&qla_tgt_mutex);
6530 if (ha->tgt.tgt_ops && ha->tgt.tgt_ops->add_target)
6531 ha->tgt.tgt_ops->add_target(base_vha);
6536 /* Must be called under tgt_host_action_mutex */
6537 int qlt_remove_target(struct qla_hw_data *ha, struct scsi_qla_host *vha)
6539 if (!vha->vha_tgt.qla_tgt)
6542 if (vha->fc_vport) {
6543 qlt_release(vha->vha_tgt.qla_tgt);
6547 /* free left over qfull cmds */
6548 qlt_init_term_exchange(vha);
6550 ql_dbg(ql_dbg_tgt, vha, 0xe03c, "Unregistering target for host %ld(%p)",
6552 qlt_release(vha->vha_tgt.qla_tgt);
6557 void qla_remove_hostmap(struct qla_hw_data *ha)
6559 struct scsi_qla_host *node;
6562 btree_for_each_safe32(&ha->host_map, key, node)
6563 btree_remove32(&ha->host_map, key);
6565 btree_destroy32(&ha->host_map);
6568 static void qlt_lport_dump(struct scsi_qla_host *vha, u64 wwpn,
6571 pr_debug("qla2xxx HW vha->node_name: %8phC\n", vha->node_name);
6572 pr_debug("qla2xxx HW vha->port_name: %8phC\n", vha->port_name);
6573 put_unaligned_be64(wwpn, b);
6574 pr_debug("qla2xxx passed configfs WWPN: %8phC\n", b);
6578 * qlt_lport_register - register lport with external module
6580 * @target_lport_ptr: pointer for tcm_qla2xxx specific lport data
6581 * @phys_wwpn: physical port WWPN
6582 * @npiv_wwpn: NPIV WWPN
6583 * @npiv_wwnn: NPIV WWNN
6584 * @callback: lport initialization callback for tcm_qla2xxx code
6586 int qlt_lport_register(void *target_lport_ptr, u64 phys_wwpn,
6587 u64 npiv_wwpn, u64 npiv_wwnn,
6588 int (*callback)(struct scsi_qla_host *, void *, u64, u64))
6590 struct qla_tgt *tgt;
6591 struct scsi_qla_host *vha;
6592 struct qla_hw_data *ha;
6593 struct Scsi_Host *host;
6594 unsigned long flags;
6598 mutex_lock(&qla_tgt_mutex);
6599 list_for_each_entry(tgt, &qla_tgt_glist, tgt_list_entry) {
6607 if (!(host->hostt->supported_mode & MODE_TARGET))
6610 if (vha->qlini_mode == QLA2XXX_INI_MODE_ENABLED)
6613 spin_lock_irqsave(&ha->hardware_lock, flags);
6614 if ((!npiv_wwpn || !npiv_wwnn) && host->active_mode & MODE_TARGET) {
6615 pr_debug("MODE_TARGET already active on qla2xxx(%d)\n",
6617 spin_unlock_irqrestore(&ha->hardware_lock, flags);
6620 if (tgt->tgt_stop) {
6621 pr_debug("MODE_TARGET in shutdown on qla2xxx(%d)\n",
6623 spin_unlock_irqrestore(&ha->hardware_lock, flags);
6626 spin_unlock_irqrestore(&ha->hardware_lock, flags);
6628 if (!scsi_host_get(host)) {
6629 ql_dbg(ql_dbg_tgt, vha, 0xe068,
6630 "Unable to scsi_host_get() for"
6631 " qla2xxx scsi_host\n");
6634 qlt_lport_dump(vha, phys_wwpn, b);
6636 if (memcmp(vha->port_name, b, WWN_SIZE)) {
6637 scsi_host_put(host);
6640 rc = (*callback)(vha, target_lport_ptr, npiv_wwpn, npiv_wwnn);
6642 scsi_host_put(host);
6644 mutex_unlock(&qla_tgt_mutex);
6647 mutex_unlock(&qla_tgt_mutex);
6651 EXPORT_SYMBOL(qlt_lport_register);
6654 * qlt_lport_deregister - Degister lport
6656 * @vha: Registered scsi_qla_host pointer
6658 void qlt_lport_deregister(struct scsi_qla_host *vha)
6660 struct qla_hw_data *ha = vha->hw;
6661 struct Scsi_Host *sh = vha->host;
6663 * Clear the target_lport_ptr qla_target_template pointer in qla_hw_data
6665 vha->vha_tgt.target_lport_ptr = NULL;
6666 ha->tgt.tgt_ops = NULL;
6668 * Release the Scsi_Host reference for the underlying qla2xxx host
6672 EXPORT_SYMBOL(qlt_lport_deregister);
6674 /* Must be called under HW lock */
6675 void qlt_set_mode(struct scsi_qla_host *vha)
6677 switch (vha->qlini_mode) {
6678 case QLA2XXX_INI_MODE_DISABLED:
6679 case QLA2XXX_INI_MODE_EXCLUSIVE:
6680 vha->host->active_mode = MODE_TARGET;
6682 case QLA2XXX_INI_MODE_ENABLED:
6683 vha->host->active_mode = MODE_INITIATOR;
6685 case QLA2XXX_INI_MODE_DUAL:
6686 vha->host->active_mode = MODE_DUAL;
6693 /* Must be called under HW lock */
6694 static void qlt_clear_mode(struct scsi_qla_host *vha)
6696 switch (vha->qlini_mode) {
6697 case QLA2XXX_INI_MODE_DISABLED:
6698 vha->host->active_mode = MODE_UNKNOWN;
6700 case QLA2XXX_INI_MODE_EXCLUSIVE:
6701 vha->host->active_mode = MODE_INITIATOR;
6703 case QLA2XXX_INI_MODE_ENABLED:
6704 case QLA2XXX_INI_MODE_DUAL:
6705 vha->host->active_mode = MODE_INITIATOR;
6713 * qla_tgt_enable_vha - NO LOCK HELD
6715 * host_reset, bring up w/ Target Mode Enabled
6718 qlt_enable_vha(struct scsi_qla_host *vha)
6720 struct qla_hw_data *ha = vha->hw;
6721 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
6722 unsigned long flags;
6723 scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
6726 ql_dbg(ql_dbg_tgt, vha, 0xe069,
6727 "Unable to locate qla_tgt pointer from"
6728 " struct qla_hw_data\n");
6732 if (vha->qlini_mode == QLA2XXX_INI_MODE_ENABLED)
6735 if (ha->tgt.num_act_qpairs > ha->max_qpairs)
6736 ha->tgt.num_act_qpairs = ha->max_qpairs;
6737 spin_lock_irqsave(&ha->hardware_lock, flags);
6738 tgt->tgt_stopped = 0;
6740 spin_unlock_irqrestore(&ha->hardware_lock, flags);
6742 mutex_lock(&ha->optrom_mutex);
6743 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf021,
6746 qla24xx_disable_vp(vha);
6747 qla24xx_enable_vp(vha);
6749 set_bit(ISP_ABORT_NEEDED, &base_vha->dpc_flags);
6750 qla2xxx_wake_dpc(base_vha);
6751 WARN_ON_ONCE(qla2x00_wait_for_hba_online(base_vha) !=
6754 mutex_unlock(&ha->optrom_mutex);
6756 EXPORT_SYMBOL(qlt_enable_vha);
6759 * qla_tgt_disable_vha - NO LOCK HELD
6761 * Disable Target Mode and reset the adapter
6763 static void qlt_disable_vha(struct scsi_qla_host *vha)
6765 struct qla_hw_data *ha = vha->hw;
6766 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
6767 unsigned long flags;
6770 ql_dbg(ql_dbg_tgt, vha, 0xe06a,
6771 "Unable to locate qla_tgt pointer from"
6772 " struct qla_hw_data\n");
6777 spin_lock_irqsave(&ha->hardware_lock, flags);
6778 qlt_clear_mode(vha);
6779 spin_unlock_irqrestore(&ha->hardware_lock, flags);
6781 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
6782 qla2xxx_wake_dpc(vha);
6785 * We are expecting the offline state.
6786 * QLA_FUNCTION_FAILED means that adapter is offline.
6788 if (qla2x00_wait_for_hba_online(vha) != QLA_SUCCESS)
6789 ql_dbg(ql_dbg_tgt, vha, 0xe081,
6790 "adapter is offline\n");
6794 * Called from qla_init.c:qla24xx_vport_create() contex to setup
6795 * the target mode specific struct scsi_qla_host and struct qla_hw_data
6799 qlt_vport_create(struct scsi_qla_host *vha, struct qla_hw_data *ha)
6801 vha->vha_tgt.qla_tgt = NULL;
6803 mutex_init(&vha->vha_tgt.tgt_mutex);
6804 mutex_init(&vha->vha_tgt.tgt_host_action_mutex);
6806 qlt_clear_mode(vha);
6809 * NOTE: Currently the value is kept the same for <24xx and
6810 * >=24xx ISPs. If it is necessary to change it,
6811 * the check should be added for specific ISPs,
6812 * assigning the value appropriately.
6814 ha->tgt.atio_q_length = ATIO_ENTRY_CNT_24XX;
6816 qlt_add_target(ha, vha);
6820 qlt_rff_id(struct scsi_qla_host *vha)
6824 * FC-4 Feature bit 0 indicates target functionality to the name server.
6826 if (qla_tgt_mode_enabled(vha)) {
6827 fc4_feature = BIT_0;
6828 } else if (qla_ini_mode_enabled(vha)) {
6829 fc4_feature = BIT_1;
6830 } else if (qla_dual_mode_enabled(vha))
6831 fc4_feature = BIT_0 | BIT_1;
6837 * qlt_init_atio_q_entries() - Initializes ATIO queue entries.
6840 * Beginning of ATIO ring has initialization control block already built
6841 * by nvram config routine.
6843 * Returns 0 on success.
6846 qlt_init_atio_q_entries(struct scsi_qla_host *vha)
6848 struct qla_hw_data *ha = vha->hw;
6850 struct atio_from_isp *pkt = (struct atio_from_isp *)ha->tgt.atio_ring;
6852 if (qla_ini_mode_enabled(vha))
6855 for (cnt = 0; cnt < ha->tgt.atio_q_length; cnt++) {
6856 pkt->u.raw.signature = cpu_to_le32(ATIO_PROCESSED);
6863 * qlt_24xx_process_atio_queue() - Process ATIO queue entries.
6864 * @ha: SCSI driver HA context
6867 qlt_24xx_process_atio_queue(struct scsi_qla_host *vha, uint8_t ha_locked)
6869 struct qla_hw_data *ha = vha->hw;
6870 struct atio_from_isp *pkt;
6873 if (!ha->flags.fw_started)
6876 while ((ha->tgt.atio_ring_ptr->signature != ATIO_PROCESSED) ||
6877 fcpcmd_is_corrupted(ha->tgt.atio_ring_ptr)) {
6878 pkt = (struct atio_from_isp *)ha->tgt.atio_ring_ptr;
6879 cnt = pkt->u.raw.entry_count;
6881 if (unlikely(fcpcmd_is_corrupted(ha->tgt.atio_ring_ptr))) {
6883 * This packet is corrupted. The header + payload
6884 * can not be trusted. There is no point in passing
6887 ql_log(ql_log_warn, vha, 0xd03c,
6888 "corrupted fcp frame SID[%3phN] OXID[%04x] EXCG[%x] %64phN\n",
6889 &pkt->u.isp24.fcp_hdr.s_id,
6890 be16_to_cpu(pkt->u.isp24.fcp_hdr.ox_id),
6891 pkt->u.isp24.exchange_addr, pkt);
6893 adjust_corrupted_atio(pkt);
6894 qlt_send_term_exchange(ha->base_qpair, NULL, pkt,
6897 qlt_24xx_atio_pkt_all_vps(vha,
6898 (struct atio_from_isp *)pkt, ha_locked);
6901 for (i = 0; i < cnt; i++) {
6902 ha->tgt.atio_ring_index++;
6903 if (ha->tgt.atio_ring_index == ha->tgt.atio_q_length) {
6904 ha->tgt.atio_ring_index = 0;
6905 ha->tgt.atio_ring_ptr = ha->tgt.atio_ring;
6907 ha->tgt.atio_ring_ptr++;
6909 pkt->u.raw.signature = cpu_to_le32(ATIO_PROCESSED);
6910 pkt = (struct atio_from_isp *)ha->tgt.atio_ring_ptr;
6915 /* Adjust ring index */
6916 wrt_reg_dword(ISP_ATIO_Q_OUT(vha), ha->tgt.atio_ring_index);
6920 qlt_24xx_config_rings(struct scsi_qla_host *vha)
6922 struct qla_hw_data *ha = vha->hw;
6923 struct qla_msix_entry *msix = &ha->msix_entries[2];
6924 struct init_cb_24xx *icb = (struct init_cb_24xx *)ha->init_cb;
6926 if (!QLA_TGT_MODE_ENABLED())
6929 wrt_reg_dword(ISP_ATIO_Q_IN(vha), 0);
6930 wrt_reg_dword(ISP_ATIO_Q_OUT(vha), 0);
6931 rd_reg_dword(ISP_ATIO_Q_OUT(vha));
6933 if (ha->flags.msix_enabled) {
6934 if (IS_QLA83XX(ha) || IS_QLA27XX(ha) || IS_QLA28XX(ha)) {
6935 if (IS_QLA2071(ha)) {
6936 /* 4 ports Baker: Enable Interrupt Handshake */
6938 icb->firmware_options_2 |= cpu_to_le32(BIT_26);
6940 icb->msix_atio = cpu_to_le16(msix->entry);
6941 icb->firmware_options_2 &= cpu_to_le32(~BIT_26);
6943 ql_dbg(ql_dbg_init, vha, 0xf072,
6944 "Registering ICB vector 0x%x for atio que.\n",
6949 if (IS_QLA83XX(ha) || IS_QLA27XX(ha) || IS_QLA28XX(ha)) {
6951 icb->firmware_options_2 |= cpu_to_le32(BIT_26);
6952 ql_dbg(ql_dbg_init, vha, 0xf072,
6953 "%s: Use INTx for ATIOQ.\n", __func__);
6959 qlt_24xx_config_nvram_stage1(struct scsi_qla_host *vha, struct nvram_24xx *nv)
6961 struct qla_hw_data *ha = vha->hw;
6964 if (!QLA_TGT_MODE_ENABLED())
6967 if (qla_tgt_mode_enabled(vha) || qla_dual_mode_enabled(vha)) {
6968 if (!ha->tgt.saved_set) {
6969 /* We save only once */
6970 ha->tgt.saved_exchange_count = nv->exchange_count;
6971 ha->tgt.saved_firmware_options_1 =
6972 nv->firmware_options_1;
6973 ha->tgt.saved_firmware_options_2 =
6974 nv->firmware_options_2;
6975 ha->tgt.saved_firmware_options_3 =
6976 nv->firmware_options_3;
6977 ha->tgt.saved_set = 1;
6980 if (qla_tgt_mode_enabled(vha))
6981 nv->exchange_count = cpu_to_le16(0xFFFF);
6983 nv->exchange_count = cpu_to_le16(vha->ql2xexchoffld);
6985 /* Enable target mode */
6986 nv->firmware_options_1 |= cpu_to_le32(BIT_4);
6988 /* Disable ini mode, if requested */
6989 if (qla_tgt_mode_enabled(vha))
6990 nv->firmware_options_1 |= cpu_to_le32(BIT_5);
6992 /* Disable Full Login after LIP */
6993 nv->firmware_options_1 &= cpu_to_le32(~BIT_13);
6994 /* Enable initial LIP */
6995 nv->firmware_options_1 &= cpu_to_le32(~BIT_9);
6996 if (ql2xtgt_tape_enable)
6997 /* Enable FC Tape support */
6998 nv->firmware_options_2 |= cpu_to_le32(BIT_12);
7000 /* Disable FC Tape support */
7001 nv->firmware_options_2 &= cpu_to_le32(~BIT_12);
7003 /* Disable Full Login after LIP */
7004 nv->host_p &= cpu_to_le32(~BIT_10);
7007 * clear BIT 15 explicitly as we have seen at least
7008 * a couple of instances where this was set and this
7009 * was causing the firmware to not be initialized.
7011 nv->firmware_options_1 &= cpu_to_le32(~BIT_15);
7012 /* Enable target PRLI control */
7013 nv->firmware_options_2 |= cpu_to_le32(BIT_14);
7015 if (IS_QLA25XX(ha)) {
7016 /* Change Loop-prefer to Pt-Pt */
7017 tmp = ~(BIT_4|BIT_5|BIT_6);
7018 nv->firmware_options_2 &= cpu_to_le32(tmp);
7020 nv->firmware_options_2 |= cpu_to_le32(tmp);
7023 if (ha->tgt.saved_set) {
7024 nv->exchange_count = ha->tgt.saved_exchange_count;
7025 nv->firmware_options_1 =
7026 ha->tgt.saved_firmware_options_1;
7027 nv->firmware_options_2 =
7028 ha->tgt.saved_firmware_options_2;
7029 nv->firmware_options_3 =
7030 ha->tgt.saved_firmware_options_3;
7035 if (ha->base_qpair->enable_class_2) {
7036 if (vha->flags.init_done)
7037 fc_host_supported_classes(vha->host) =
7038 FC_COS_CLASS2 | FC_COS_CLASS3;
7040 nv->firmware_options_2 |= cpu_to_le32(BIT_8);
7042 if (vha->flags.init_done)
7043 fc_host_supported_classes(vha->host) = FC_COS_CLASS3;
7045 nv->firmware_options_2 &= ~cpu_to_le32(BIT_8);
7050 qlt_24xx_config_nvram_stage2(struct scsi_qla_host *vha,
7051 struct init_cb_24xx *icb)
7053 struct qla_hw_data *ha = vha->hw;
7055 if (!QLA_TGT_MODE_ENABLED())
7058 if (ha->tgt.node_name_set) {
7059 memcpy(icb->node_name, ha->tgt.tgt_node_name, WWN_SIZE);
7060 icb->firmware_options_1 |= cpu_to_le32(BIT_14);
7065 qlt_81xx_config_nvram_stage1(struct scsi_qla_host *vha, struct nvram_81xx *nv)
7067 struct qla_hw_data *ha = vha->hw;
7070 if (!QLA_TGT_MODE_ENABLED())
7073 if (qla_tgt_mode_enabled(vha) || qla_dual_mode_enabled(vha)) {
7074 if (!ha->tgt.saved_set) {
7075 /* We save only once */
7076 ha->tgt.saved_exchange_count = nv->exchange_count;
7077 ha->tgt.saved_firmware_options_1 =
7078 nv->firmware_options_1;
7079 ha->tgt.saved_firmware_options_2 =
7080 nv->firmware_options_2;
7081 ha->tgt.saved_firmware_options_3 =
7082 nv->firmware_options_3;
7083 ha->tgt.saved_set = 1;
7086 if (qla_tgt_mode_enabled(vha))
7087 nv->exchange_count = cpu_to_le16(0xFFFF);
7089 nv->exchange_count = cpu_to_le16(vha->ql2xexchoffld);
7091 /* Enable target mode */
7092 nv->firmware_options_1 |= cpu_to_le32(BIT_4);
7094 /* Disable ini mode, if requested */
7095 if (qla_tgt_mode_enabled(vha))
7096 nv->firmware_options_1 |= cpu_to_le32(BIT_5);
7097 /* Disable Full Login after LIP */
7098 nv->firmware_options_1 &= cpu_to_le32(~BIT_13);
7099 /* Enable initial LIP */
7100 nv->firmware_options_1 &= cpu_to_le32(~BIT_9);
7102 * clear BIT 15 explicitly as we have seen at
7103 * least a couple of instances where this was set
7104 * and this was causing the firmware to not be
7107 nv->firmware_options_1 &= cpu_to_le32(~BIT_15);
7108 if (ql2xtgt_tape_enable)
7109 /* Enable FC tape support */
7110 nv->firmware_options_2 |= cpu_to_le32(BIT_12);
7112 /* Disable FC tape support */
7113 nv->firmware_options_2 &= cpu_to_le32(~BIT_12);
7115 /* Disable Full Login after LIP */
7116 nv->host_p &= cpu_to_le32(~BIT_10);
7117 /* Enable target PRLI control */
7118 nv->firmware_options_2 |= cpu_to_le32(BIT_14);
7120 /* Change Loop-prefer to Pt-Pt */
7121 tmp = ~(BIT_4|BIT_5|BIT_6);
7122 nv->firmware_options_2 &= cpu_to_le32(tmp);
7124 nv->firmware_options_2 |= cpu_to_le32(tmp);
7126 if (ha->tgt.saved_set) {
7127 nv->exchange_count = ha->tgt.saved_exchange_count;
7128 nv->firmware_options_1 =
7129 ha->tgt.saved_firmware_options_1;
7130 nv->firmware_options_2 =
7131 ha->tgt.saved_firmware_options_2;
7132 nv->firmware_options_3 =
7133 ha->tgt.saved_firmware_options_3;
7138 if (ha->base_qpair->enable_class_2) {
7139 if (vha->flags.init_done)
7140 fc_host_supported_classes(vha->host) =
7141 FC_COS_CLASS2 | FC_COS_CLASS3;
7143 nv->firmware_options_2 |= cpu_to_le32(BIT_8);
7145 if (vha->flags.init_done)
7146 fc_host_supported_classes(vha->host) = FC_COS_CLASS3;
7148 nv->firmware_options_2 &= ~cpu_to_le32(BIT_8);
7153 qlt_81xx_config_nvram_stage2(struct scsi_qla_host *vha,
7154 struct init_cb_81xx *icb)
7156 struct qla_hw_data *ha = vha->hw;
7158 if (!QLA_TGT_MODE_ENABLED())
7161 if (ha->tgt.node_name_set) {
7162 memcpy(icb->node_name, ha->tgt.tgt_node_name, WWN_SIZE);
7163 icb->firmware_options_1 |= cpu_to_le32(BIT_14);
7168 qlt_83xx_iospace_config(struct qla_hw_data *ha)
7170 if (!QLA_TGT_MODE_ENABLED())
7173 ha->msix_count += 1; /* For ATIO Q */
7178 qlt_modify_vp_config(struct scsi_qla_host *vha,
7179 struct vp_config_entry_24xx *vpmod)
7181 /* enable target mode. Bit5 = 1 => disable */
7182 if (qla_tgt_mode_enabled(vha) || qla_dual_mode_enabled(vha))
7183 vpmod->options_idx1 &= ~BIT_5;
7185 /* Disable ini mode, if requested. bit4 = 1 => disable */
7186 if (qla_tgt_mode_enabled(vha))
7187 vpmod->options_idx1 &= ~BIT_4;
7191 qlt_probe_one_stage1(struct scsi_qla_host *base_vha, struct qla_hw_data *ha)
7193 mutex_init(&base_vha->vha_tgt.tgt_mutex);
7194 if (!QLA_TGT_MODE_ENABLED())
7197 if (ha->mqenable || IS_QLA83XX(ha) || IS_QLA27XX(ha) || IS_QLA28XX(ha)) {
7198 ISP_ATIO_Q_IN(base_vha) = &ha->mqiobase->isp25mq.atio_q_in;
7199 ISP_ATIO_Q_OUT(base_vha) = &ha->mqiobase->isp25mq.atio_q_out;
7201 ISP_ATIO_Q_IN(base_vha) = &ha->iobase->isp24.atio_q_in;
7202 ISP_ATIO_Q_OUT(base_vha) = &ha->iobase->isp24.atio_q_out;
7205 mutex_init(&base_vha->vha_tgt.tgt_host_action_mutex);
7207 INIT_LIST_HEAD(&base_vha->unknown_atio_list);
7208 INIT_DELAYED_WORK(&base_vha->unknown_atio_work,
7209 qlt_unknown_atio_work_fn);
7211 qlt_clear_mode(base_vha);
7213 qlt_update_vp_map(base_vha, SET_VP_IDX);
7217 qla83xx_msix_atio_q(int irq, void *dev_id)
7219 struct rsp_que *rsp;
7220 scsi_qla_host_t *vha;
7221 struct qla_hw_data *ha;
7222 unsigned long flags;
7224 rsp = (struct rsp_que *) dev_id;
7226 vha = pci_get_drvdata(ha->pdev);
7228 spin_lock_irqsave(&ha->tgt.atio_lock, flags);
7230 qlt_24xx_process_atio_queue(vha, 0);
7232 spin_unlock_irqrestore(&ha->tgt.atio_lock, flags);
7238 qlt_handle_abts_recv_work(struct work_struct *work)
7240 struct qla_tgt_sess_op *op = container_of(work,
7241 struct qla_tgt_sess_op, work);
7242 scsi_qla_host_t *vha = op->vha;
7243 struct qla_hw_data *ha = vha->hw;
7244 unsigned long flags;
7246 if (qla2x00_reset_active(vha) ||
7247 (op->chip_reset != ha->base_qpair->chip_reset))
7250 spin_lock_irqsave(&ha->tgt.atio_lock, flags);
7251 qlt_24xx_process_atio_queue(vha, 0);
7252 spin_unlock_irqrestore(&ha->tgt.atio_lock, flags);
7254 spin_lock_irqsave(&ha->hardware_lock, flags);
7255 qlt_response_pkt_all_vps(vha, op->rsp, (response_t *)&op->atio);
7256 spin_unlock_irqrestore(&ha->hardware_lock, flags);
7262 qlt_handle_abts_recv(struct scsi_qla_host *vha, struct rsp_que *rsp,
7265 struct qla_tgt_sess_op *op;
7267 op = kzalloc(sizeof(*op), GFP_ATOMIC);
7270 /* do not reach for ATIO queue here. This is best effort err
7271 * recovery at this point.
7273 qlt_response_pkt_all_vps(vha, rsp, pkt);
7277 memcpy(&op->atio, pkt, sizeof(*pkt));
7279 op->chip_reset = vha->hw->base_qpair->chip_reset;
7281 INIT_WORK(&op->work, qlt_handle_abts_recv_work);
7282 queue_work(qla_tgt_wq, &op->work);
7287 qlt_mem_alloc(struct qla_hw_data *ha)
7289 if (!QLA_TGT_MODE_ENABLED())
7292 ha->tgt.tgt_vp_map = kcalloc(MAX_MULTI_ID_FABRIC,
7293 sizeof(struct qla_tgt_vp_map),
7295 if (!ha->tgt.tgt_vp_map)
7298 ha->tgt.atio_ring = dma_alloc_coherent(&ha->pdev->dev,
7299 (ha->tgt.atio_q_length + 1) * sizeof(struct atio_from_isp),
7300 &ha->tgt.atio_dma, GFP_KERNEL);
7301 if (!ha->tgt.atio_ring) {
7302 kfree(ha->tgt.tgt_vp_map);
7309 qlt_mem_free(struct qla_hw_data *ha)
7311 if (!QLA_TGT_MODE_ENABLED())
7314 if (ha->tgt.atio_ring) {
7315 dma_free_coherent(&ha->pdev->dev, (ha->tgt.atio_q_length + 1) *
7316 sizeof(struct atio_from_isp), ha->tgt.atio_ring,
7319 ha->tgt.atio_ring = NULL;
7320 ha->tgt.atio_dma = 0;
7321 kfree(ha->tgt.tgt_vp_map);
7322 ha->tgt.tgt_vp_map = NULL;
7325 /* vport_slock to be held by the caller */
7327 qlt_update_vp_map(struct scsi_qla_host *vha, int cmd)
7333 key = vha->d_id.b24;
7337 if (!QLA_TGT_MODE_ENABLED())
7339 vha->hw->tgt.tgt_vp_map[vha->vp_idx].vha = vha;
7342 slot = btree_lookup32(&vha->hw->host_map, key);
7344 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf018,
7345 "Save vha in host_map %p %06x\n", vha, key);
7346 rc = btree_insert32(&vha->hw->host_map,
7347 key, vha, GFP_ATOMIC);
7349 ql_log(ql_log_info, vha, 0xd03e,
7350 "Unable to insert s_id into host_map: %06x\n",
7354 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf019,
7355 "replace existing vha in host_map %p %06x\n", vha, key);
7356 btree_update32(&vha->hw->host_map, key, vha);
7359 if (!QLA_TGT_MODE_ENABLED())
7361 vha->hw->tgt.tgt_vp_map[vha->vp_idx].vha = NULL;
7364 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01a,
7365 "clear vha in host_map %p %06x\n", vha, key);
7366 slot = btree_lookup32(&vha->hw->host_map, key);
7368 btree_remove32(&vha->hw->host_map, key);
7374 void qlt_update_host_map(struct scsi_qla_host *vha, port_id_t id)
7377 if (!vha->d_id.b24) {
7379 qlt_update_vp_map(vha, SET_AL_PA);
7380 } else if (vha->d_id.b24 != id.b24) {
7381 qlt_update_vp_map(vha, RESET_AL_PA);
7383 qlt_update_vp_map(vha, SET_AL_PA);
7387 static int __init qlt_parse_ini_mode(void)
7389 if (strcasecmp(qlini_mode, QLA2XXX_INI_MODE_STR_EXCLUSIVE) == 0)
7390 ql2x_ini_mode = QLA2XXX_INI_MODE_EXCLUSIVE;
7391 else if (strcasecmp(qlini_mode, QLA2XXX_INI_MODE_STR_DISABLED) == 0)
7392 ql2x_ini_mode = QLA2XXX_INI_MODE_DISABLED;
7393 else if (strcasecmp(qlini_mode, QLA2XXX_INI_MODE_STR_ENABLED) == 0)
7394 ql2x_ini_mode = QLA2XXX_INI_MODE_ENABLED;
7395 else if (strcasecmp(qlini_mode, QLA2XXX_INI_MODE_STR_DUAL) == 0)
7396 ql2x_ini_mode = QLA2XXX_INI_MODE_DUAL;
7403 int __init qlt_init(void)
7407 BUILD_BUG_ON(sizeof(struct ctio7_to_24xx) != 64);
7408 BUILD_BUG_ON(sizeof(struct ctio_to_2xxx) != 64);
7410 if (!qlt_parse_ini_mode()) {
7411 ql_log(ql_log_fatal, NULL, 0xe06b,
7412 "qlt_parse_ini_mode() failed\n");
7416 if (!QLA_TGT_MODE_ENABLED())
7419 qla_tgt_mgmt_cmd_cachep = kmem_cache_create("qla_tgt_mgmt_cmd_cachep",
7420 sizeof(struct qla_tgt_mgmt_cmd), __alignof__(struct
7421 qla_tgt_mgmt_cmd), 0, NULL);
7422 if (!qla_tgt_mgmt_cmd_cachep) {
7423 ql_log(ql_log_fatal, NULL, 0xd04b,
7424 "kmem_cache_create for qla_tgt_mgmt_cmd_cachep failed\n");
7428 qla_tgt_plogi_cachep = kmem_cache_create("qla_tgt_plogi_cachep",
7429 sizeof(struct qlt_plogi_ack_t), __alignof__(struct qlt_plogi_ack_t),
7432 if (!qla_tgt_plogi_cachep) {
7433 ql_log(ql_log_fatal, NULL, 0xe06d,
7434 "kmem_cache_create for qla_tgt_plogi_cachep failed\n");
7436 goto out_mgmt_cmd_cachep;
7439 qla_tgt_mgmt_cmd_mempool = mempool_create(25, mempool_alloc_slab,
7440 mempool_free_slab, qla_tgt_mgmt_cmd_cachep);
7441 if (!qla_tgt_mgmt_cmd_mempool) {
7442 ql_log(ql_log_fatal, NULL, 0xe06e,
7443 "mempool_create for qla_tgt_mgmt_cmd_mempool failed\n");
7445 goto out_plogi_cachep;
7448 qla_tgt_wq = alloc_workqueue("qla_tgt_wq", 0, 0);
7450 ql_log(ql_log_fatal, NULL, 0xe06f,
7451 "alloc_workqueue for qla_tgt_wq failed\n");
7453 goto out_cmd_mempool;
7456 * Return 1 to signal that initiator-mode is being disabled
7458 return (ql2x_ini_mode == QLA2XXX_INI_MODE_DISABLED) ? 1 : 0;
7461 mempool_destroy(qla_tgt_mgmt_cmd_mempool);
7463 kmem_cache_destroy(qla_tgt_plogi_cachep);
7464 out_mgmt_cmd_cachep:
7465 kmem_cache_destroy(qla_tgt_mgmt_cmd_cachep);
7471 if (!QLA_TGT_MODE_ENABLED())
7474 destroy_workqueue(qla_tgt_wq);
7475 mempool_destroy(qla_tgt_mgmt_cmd_mempool);
7476 kmem_cache_destroy(qla_tgt_plogi_cachep);
7477 kmem_cache_destroy(qla_tgt_mgmt_cmd_cachep);