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 qla2x00_mark_device_lost(vha, sess, 0);
986 if (sess->send_els_logo) {
987 qlt_port_logo_t logo;
989 logo.id = sess->d_id;
991 INIT_LIST_HEAD(&logo.list);
993 qlt_send_first_logo(vha, &logo);
994 sess->send_els_logo = 0;
997 if (sess->logout_on_delete && sess->loop_id != FC_NO_LOOP_ID) {
1001 (own->iocb.u.isp24.status_subcode == ELS_PLOGI)) {
1002 sess->logout_completed = 0;
1003 rc = qla2x00_post_async_logout_work(vha, sess,
1005 if (rc != QLA_SUCCESS)
1006 ql_log(ql_log_warn, vha, 0xf085,
1007 "Schedule logo failed sess %p rc %d\n",
1010 logout_started = true;
1011 } else if (own && (own->iocb.u.isp24.status_subcode ==
1012 ELS_PRLI) && ha->flags.rida_fmt2) {
1013 rc = qla2x00_post_async_prlo_work(vha, sess,
1015 if (rc != QLA_SUCCESS)
1016 ql_log(ql_log_warn, vha, 0xf085,
1017 "Schedule PRLO failed sess %p rc %d\n",
1020 logout_started = true;
1022 } /* if sess->logout_on_delete */
1024 if (sess->nvme_flag & NVME_FLAG_REGISTERED &&
1025 !(sess->nvme_flag & NVME_FLAG_DELETING)) {
1026 sess->nvme_flag |= NVME_FLAG_DELETING;
1027 qla_nvme_unregister_remote_port(sess);
1030 if (ha->flags.edif_enabled &&
1032 own->iocb.u.isp24.status_subcode == ELS_PLOGI))) {
1033 sess->edif.authok = 0;
1034 if (!ha->flags.host_shutting_down) {
1035 ql_dbg(ql_dbg_edif, vha, 0x911e,
1036 "%s wwpn %8phC calling qla2x00_release_all_sadb\n",
1037 __func__, sess->port_name);
1038 qla2x00_release_all_sadb(vha, sess);
1040 ql_dbg(ql_dbg_edif, vha, 0x911e,
1041 "%s bypassing release_all_sadb\n",
1045 qla_edif_clear_appdata(vha, sess);
1046 qla_edif_sess_down(vha, sess);
1051 * Release the target session for FC Nexus from fabric module code.
1053 if (sess->se_sess != NULL)
1054 ha->tgt.tgt_ops->free_session(sess);
1056 if (logout_started) {
1057 bool traced = false;
1060 while (!READ_ONCE(sess->logout_completed)) {
1062 ql_dbg(ql_dbg_disc, vha, 0xf086,
1063 "%s: waiting for sess %p logout\n",
1070 * Driver timeout is set to 22 Sec, update count value to loop
1071 * long enough for log-out to complete before advancing. Otherwise,
1072 * straddling logout can interfere with re-login attempt.
1078 ql_dbg(ql_dbg_disc, vha, 0xf087,
1079 "%s: sess %p logout completed\n", __func__, sess);
1082 if (sess->logo_ack_needed) {
1083 sess->logo_ack_needed = 0;
1084 qla24xx_async_notify_ack(vha, sess,
1085 (struct imm_ntfy_from_isp *)sess->iocb, SRB_NACK_LOGO);
1088 spin_lock_irqsave(&vha->work_lock, flags);
1089 sess->flags &= ~FCF_ASYNC_SENT;
1090 spin_unlock_irqrestore(&vha->work_lock, flags);
1092 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
1093 if (sess->se_sess) {
1094 sess->se_sess = NULL;
1095 if (tgt && !IS_SW_RESV_ADDR(sess->d_id))
1099 qla2x00_set_fcport_disc_state(sess, DSC_DELETED);
1100 sess->fw_login_state = DSC_LS_PORT_UNAVAIL;
1101 sess->deleted = QLA_SESS_DELETED;
1103 if (sess->login_succ && !IS_SW_RESV_ADDR(sess->d_id)) {
1104 vha->fcport_count--;
1105 sess->login_succ = 0;
1108 qla2x00_clear_loop_id(sess);
1110 if (sess->conflict) {
1111 sess->conflict->login_pause = 0;
1112 sess->conflict = NULL;
1113 if (!test_bit(UNLOADING, &vha->dpc_flags))
1114 set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
1118 struct qlt_plogi_ack_t *con =
1119 sess->plogi_link[QLT_PLOGI_LINK_CONFLICT];
1120 struct imm_ntfy_from_isp *iocb;
1122 own = sess->plogi_link[QLT_PLOGI_LINK_SAME_WWN];
1126 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf099,
1127 "se_sess %p / sess %p port %8phC is gone,"
1128 " %s (ref=%d), releasing PLOGI for %8phC (ref=%d)\n",
1129 sess->se_sess, sess, sess->port_name,
1130 own ? "releasing own PLOGI" : "no own PLOGI pending",
1131 own ? own->ref_count : -1,
1132 iocb->u.isp24.port_name, con->ref_count);
1133 qlt_plogi_ack_unref(vha, con);
1134 sess->plogi_link[QLT_PLOGI_LINK_CONFLICT] = NULL;
1136 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf09a,
1137 "se_sess %p / sess %p port %8phC is gone, %s (ref=%d)\n",
1138 sess->se_sess, sess, sess->port_name,
1139 own ? "releasing own PLOGI" :
1140 "no own PLOGI pending",
1141 own ? own->ref_count : -1);
1145 sess->fw_login_state = DSC_LS_PLOGI_PEND;
1146 qlt_plogi_ack_unref(vha, own);
1147 sess->plogi_link[QLT_PLOGI_LINK_SAME_WWN] = NULL;
1151 sess->explicit_logout = 0;
1152 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
1153 sess->free_pending = 0;
1155 qla2x00_dfs_remove_rport(vha, sess);
1157 ql_dbg(ql_dbg_disc, vha, 0xf001,
1158 "Unregistration of sess %p %8phC finished fcp_cnt %d\n",
1159 sess, sess->port_name, vha->fcport_count);
1161 if (tgt && (tgt->sess_count == 0))
1162 wake_up_all(&tgt->waitQ);
1164 if (!test_bit(PFLG_DRIVER_REMOVING, &base_vha->pci_flags) &&
1165 !(vha->vp_idx && test_bit(VPORT_DELETE, &vha->dpc_flags)) &&
1166 (!tgt || !tgt->tgt_stop) && !LOOP_TRANSITION(vha)) {
1167 switch (vha->host->active_mode) {
1168 case MODE_INITIATOR:
1170 set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
1171 qla2xxx_wake_dpc(vha);
1180 if (vha->fcport_count == 0)
1181 wake_up_all(&vha->fcport_waitQ);
1184 /* ha->tgt.sess_lock supposed to be held on entry */
1185 void qlt_unreg_sess(struct fc_port *sess)
1187 struct scsi_qla_host *vha = sess->vha;
1188 unsigned long flags;
1190 ql_dbg(ql_dbg_disc, sess->vha, 0x210a,
1191 "%s sess %p for deletion %8phC\n",
1192 __func__, sess, sess->port_name);
1194 spin_lock_irqsave(&sess->vha->work_lock, flags);
1195 if (sess->free_pending) {
1196 spin_unlock_irqrestore(&sess->vha->work_lock, flags);
1199 sess->free_pending = 1;
1201 * Use FCF_ASYNC_SENT flag to block other cmds used in sess
1202 * management from being sent.
1204 sess->flags |= FCF_ASYNC_SENT;
1205 spin_unlock_irqrestore(&sess->vha->work_lock, flags);
1208 vha->hw->tgt.tgt_ops->clear_nacl_from_fcport_map(sess);
1210 sess->deleted = QLA_SESS_DELETION_IN_PROGRESS;
1211 qla2x00_set_fcport_disc_state(sess, DSC_DELETE_PEND);
1212 sess->last_rscn_gen = sess->rscn_gen;
1213 sess->last_login_gen = sess->login_gen;
1215 queue_work(sess->vha->hw->wq, &sess->free_work);
1217 EXPORT_SYMBOL(qlt_unreg_sess);
1219 static int qlt_reset(struct scsi_qla_host *vha, void *iocb, int mcmd)
1221 struct qla_hw_data *ha = vha->hw;
1222 struct fc_port *sess = NULL;
1225 struct imm_ntfy_from_isp *n = (struct imm_ntfy_from_isp *)iocb;
1226 unsigned long flags;
1228 loop_id = le16_to_cpu(n->u.isp24.nport_handle);
1229 if (loop_id == 0xFFFF) {
1231 atomic_inc(&vha->vha_tgt.qla_tgt->tgt_global_resets_count);
1232 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
1233 qlt_clear_tgt_db(vha->vha_tgt.qla_tgt);
1234 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
1236 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
1237 sess = ha->tgt.tgt_ops->find_sess_by_loop_id(vha, loop_id);
1238 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
1241 ql_dbg(ql_dbg_tgt, vha, 0xe000,
1242 "Using sess for qla_tgt_reset: %p\n", sess);
1248 ql_dbg(ql_dbg_tgt, vha, 0xe047,
1249 "scsi(%ld): resetting (session %p from port %8phC mcmd %x, "
1250 "loop_id %d)\n", vha->host_no, sess, sess->port_name,
1253 return qlt_issue_task_mgmt(sess, 0, mcmd, iocb, QLA24XX_MGMT_SEND_NACK);
1256 static void qla24xx_chk_fcp_state(struct fc_port *sess)
1258 if (sess->chip_reset != sess->vha->hw->base_qpair->chip_reset) {
1259 sess->logout_on_delete = 0;
1260 sess->logo_ack_needed = 0;
1261 sess->fw_login_state = DSC_LS_PORT_UNAVAIL;
1265 void qlt_schedule_sess_for_deletion(struct fc_port *sess)
1267 struct qla_tgt *tgt = sess->tgt;
1268 unsigned long flags;
1271 switch (sess->disc_state) {
1272 case DSC_DELETE_PEND:
1275 if (!sess->plogi_link[QLT_PLOGI_LINK_SAME_WWN] &&
1276 !sess->plogi_link[QLT_PLOGI_LINK_CONFLICT]) {
1277 if (tgt && tgt->tgt_stop && tgt->sess_count == 0)
1278 wake_up_all(&tgt->waitQ);
1280 if (sess->vha->fcport_count == 0)
1281 wake_up_all(&sess->vha->fcport_waitQ);
1285 case DSC_UPD_FCPORT:
1287 * This port is not done reporting to upper layer.
1290 sess->next_disc_state = DSC_DELETE_PEND;
1291 sec = jiffies_to_msecs(jiffies -
1292 sess->jiffies_at_registration)/1000;
1293 if (sess->sec_since_registration < sec && sec && !(sec % 5)) {
1294 sess->sec_since_registration = sec;
1295 ql_dbg(ql_dbg_disc, sess->vha, 0xffff,
1296 "%s %8phC : Slow Rport registration(%d Sec)\n",
1297 __func__, sess->port_name, sec);
1304 spin_lock_irqsave(&sess->vha->work_lock, flags);
1305 if (sess->deleted == QLA_SESS_DELETION_IN_PROGRESS) {
1306 spin_unlock_irqrestore(&sess->vha->work_lock, flags);
1309 sess->deleted = QLA_SESS_DELETION_IN_PROGRESS;
1310 spin_unlock_irqrestore(&sess->vha->work_lock, flags);
1312 sess->prli_pend_timer = 0;
1313 qla2x00_set_fcport_disc_state(sess, DSC_DELETE_PEND);
1315 qla24xx_chk_fcp_state(sess);
1317 ql_dbg(ql_log_warn, sess->vha, 0xe001,
1318 "Scheduling sess %p for deletion %8phC fc4_type %x\n",
1319 sess, sess->port_name, sess->fc4_type);
1321 WARN_ON(!queue_work(sess->vha->hw->wq, &sess->del_work));
1324 static void qlt_clear_tgt_db(struct qla_tgt *tgt)
1326 struct fc_port *sess;
1327 scsi_qla_host_t *vha = tgt->vha;
1329 list_for_each_entry(sess, &vha->vp_fcports, list) {
1331 qlt_schedule_sess_for_deletion(sess);
1334 /* At this point tgt could be already dead */
1337 static int qla24xx_get_loop_id(struct scsi_qla_host *vha, be_id_t s_id,
1340 struct qla_hw_data *ha = vha->hw;
1341 dma_addr_t gid_list_dma;
1342 struct gid_list_info *gid_list, *gid;
1346 gid_list = dma_alloc_coherent(&ha->pdev->dev, qla2x00_gid_list_size(ha),
1347 &gid_list_dma, GFP_KERNEL);
1349 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf044,
1350 "qla_target(%d): DMA Alloc failed of %u\n",
1351 vha->vp_idx, qla2x00_gid_list_size(ha));
1355 /* Get list of logged in devices */
1356 rc = qla24xx_gidlist_wait(vha, gid_list, gid_list_dma, &entries);
1357 if (rc != QLA_SUCCESS) {
1358 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf045,
1359 "qla_target(%d): get_id_list() failed: %x\n",
1362 goto out_free_id_list;
1367 for (i = 0; i < entries; i++) {
1368 if (gid->al_pa == s_id.al_pa &&
1369 gid->area == s_id.area &&
1370 gid->domain == s_id.domain) {
1371 *loop_id = le16_to_cpu(gid->loop_id);
1375 gid = (void *)gid + ha->gid_list_info_size;
1379 dma_free_coherent(&ha->pdev->dev, qla2x00_gid_list_size(ha),
1380 gid_list, gid_list_dma);
1385 * Adds an extra ref to allow to drop hw lock after adding sess to the list.
1386 * Caller must put it.
1388 static struct fc_port *qlt_create_sess(
1389 struct scsi_qla_host *vha,
1393 struct qla_hw_data *ha = vha->hw;
1394 struct fc_port *sess = fcport;
1395 unsigned long flags;
1397 if (vha->vha_tgt.qla_tgt->tgt_stop)
1400 if (fcport->se_sess) {
1401 if (!kref_get_unless_zero(&sess->sess_kref)) {
1402 ql_dbg(ql_dbg_disc, vha, 0x20f6,
1403 "%s: kref_get_unless_zero failed for %8phC\n",
1404 __func__, sess->port_name);
1409 sess->tgt = vha->vha_tgt.qla_tgt;
1410 sess->local = local;
1413 * Under normal circumstances we want to logout from firmware when
1414 * session eventually ends and release corresponding nport handle.
1415 * In the exception cases (e.g. when new PLOGI is waiting) corresponding
1416 * code will adjust these flags as necessary.
1418 sess->logout_on_delete = 1;
1419 sess->keep_nport_handle = 0;
1420 sess->logout_completed = 0;
1422 if (ha->tgt.tgt_ops->check_initiator_node_acl(vha,
1423 &fcport->port_name[0], sess) < 0) {
1424 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf015,
1425 "(%d) %8phC check_initiator_node_acl failed\n",
1426 vha->vp_idx, fcport->port_name);
1429 kref_init(&fcport->sess_kref);
1431 * Take an extra reference to ->sess_kref here to handle
1432 * fc_port access across ->tgt.sess_lock reaquire.
1434 if (!kref_get_unless_zero(&sess->sess_kref)) {
1435 ql_dbg(ql_dbg_disc, vha, 0x20f7,
1436 "%s: kref_get_unless_zero failed for %8phC\n",
1437 __func__, sess->port_name);
1441 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
1442 if (!IS_SW_RESV_ADDR(sess->d_id))
1443 vha->vha_tgt.qla_tgt->sess_count++;
1445 qlt_do_generation_tick(vha, &sess->generation);
1446 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
1449 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf006,
1450 "Adding sess %p se_sess %p to tgt %p sess_count %d\n",
1451 sess, sess->se_sess, vha->vha_tgt.qla_tgt,
1452 vha->vha_tgt.qla_tgt->sess_count);
1454 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04b,
1455 "qla_target(%d): %ssession for wwn %8phC (loop_id %d, "
1456 "s_id %x:%x:%x, confirmed completion %ssupported) added\n",
1457 vha->vp_idx, local ? "local " : "", fcport->port_name,
1458 fcport->loop_id, sess->d_id.b.domain, sess->d_id.b.area,
1459 sess->d_id.b.al_pa, sess->conf_compl_supported ? "" : "not ");
1465 * max_gen - specifies maximum session generation
1466 * at which this deletion requestion is still valid
1469 qlt_fc_port_deleted(struct scsi_qla_host *vha, fc_port_t *fcport, int max_gen)
1471 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
1472 struct fc_port *sess = fcport;
1473 unsigned long flags;
1475 if (!vha->hw->tgt.tgt_ops)
1481 spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
1482 if (tgt->tgt_stop) {
1483 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
1486 if (!sess->se_sess) {
1487 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
1491 if (max_gen - sess->generation < 0) {
1492 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
1493 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf092,
1494 "Ignoring stale deletion request for se_sess %p / sess %p"
1495 " for port %8phC, req_gen %d, sess_gen %d\n",
1496 sess->se_sess, sess, sess->port_name, max_gen,
1501 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf008, "qla_tgt_fc_port_deleted %p", sess);
1504 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
1505 qlt_schedule_sess_for_deletion(sess);
1508 static inline int test_tgt_sess_count(struct qla_tgt *tgt)
1510 struct qla_hw_data *ha = tgt->ha;
1511 unsigned long flags;
1514 * We need to protect against race, when tgt is freed before or
1517 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
1518 ql_dbg(ql_dbg_tgt, tgt->vha, 0xe002,
1519 "tgt %p, sess_count=%d\n",
1520 tgt, tgt->sess_count);
1521 res = (tgt->sess_count == 0);
1522 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
1527 /* Called by tcm_qla2xxx configfs code */
1528 int qlt_stop_phase1(struct qla_tgt *tgt)
1530 struct scsi_qla_host *vha = tgt->vha;
1531 struct qla_hw_data *ha = tgt->ha;
1532 unsigned long flags;
1534 mutex_lock(&ha->optrom_mutex);
1535 mutex_lock(&qla_tgt_mutex);
1537 if (tgt->tgt_stop || tgt->tgt_stopped) {
1538 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04e,
1539 "Already in tgt->tgt_stop or tgt_stopped state\n");
1540 mutex_unlock(&qla_tgt_mutex);
1541 mutex_unlock(&ha->optrom_mutex);
1545 ql_dbg(ql_dbg_tgt_mgt, vha, 0xe003, "Stopping target for host %ld(%p)\n",
1548 * Mutex needed to sync with qla_tgt_fc_port_[added,deleted].
1549 * Lock is needed, because we still can get an incoming packet.
1551 mutex_lock(&vha->vha_tgt.tgt_mutex);
1553 qlt_clear_tgt_db(tgt);
1554 mutex_unlock(&vha->vha_tgt.tgt_mutex);
1555 mutex_unlock(&qla_tgt_mutex);
1557 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf009,
1558 "Waiting for sess works (tgt %p)", tgt);
1559 spin_lock_irqsave(&tgt->sess_work_lock, flags);
1560 while (!list_empty(&tgt->sess_works_list)) {
1561 spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
1562 flush_scheduled_work();
1563 spin_lock_irqsave(&tgt->sess_work_lock, flags);
1565 spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
1567 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00a,
1568 "Waiting for tgt %p: sess_count=%d\n", tgt, tgt->sess_count);
1570 wait_event_timeout(tgt->waitQ, test_tgt_sess_count(tgt), 10*HZ);
1573 if (!ha->flags.host_shutting_down &&
1574 (qla_tgt_mode_enabled(vha) || qla_dual_mode_enabled(vha)))
1575 qlt_disable_vha(vha);
1577 /* Wait for sessions to clear out (just in case) */
1578 wait_event_timeout(tgt->waitQ, test_tgt_sess_count(tgt), 10*HZ);
1579 mutex_unlock(&ha->optrom_mutex);
1583 EXPORT_SYMBOL(qlt_stop_phase1);
1585 /* Called by tcm_qla2xxx configfs code */
1586 void qlt_stop_phase2(struct qla_tgt *tgt)
1588 scsi_qla_host_t *vha = tgt->vha;
1590 if (tgt->tgt_stopped) {
1591 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04f,
1592 "Already in tgt->tgt_stopped state\n");
1596 if (!tgt->tgt_stop) {
1597 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00b,
1598 "%s: phase1 stop is not completed\n", __func__);
1603 mutex_lock(&tgt->ha->optrom_mutex);
1604 mutex_lock(&vha->vha_tgt.tgt_mutex);
1606 tgt->tgt_stopped = 1;
1607 mutex_unlock(&vha->vha_tgt.tgt_mutex);
1608 mutex_unlock(&tgt->ha->optrom_mutex);
1610 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00c, "Stop of tgt %p finished\n",
1613 switch (vha->qlini_mode) {
1614 case QLA2XXX_INI_MODE_EXCLUSIVE:
1615 vha->flags.online = 1;
1616 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
1622 EXPORT_SYMBOL(qlt_stop_phase2);
1624 /* Called from qlt_remove_target() -> qla2x00_remove_one() */
1625 static void qlt_release(struct qla_tgt *tgt)
1627 scsi_qla_host_t *vha = tgt->vha;
1631 struct qla_qpair_hint *h;
1632 struct qla_hw_data *ha = vha->hw;
1634 if (!tgt->tgt_stop && !tgt->tgt_stopped)
1635 qlt_stop_phase1(tgt);
1637 if (!tgt->tgt_stopped)
1638 qlt_stop_phase2(tgt);
1640 for (i = 0; i < vha->hw->max_qpairs + 1; i++) {
1641 unsigned long flags;
1643 h = &tgt->qphints[i];
1645 spin_lock_irqsave(h->qpair->qp_lock_ptr, flags);
1646 list_del(&h->hint_elem);
1647 spin_unlock_irqrestore(h->qpair->qp_lock_ptr, flags);
1651 kfree(tgt->qphints);
1652 mutex_lock(&qla_tgt_mutex);
1653 list_del(&vha->vha_tgt.qla_tgt->tgt_list_entry);
1654 mutex_unlock(&qla_tgt_mutex);
1656 btree_for_each_safe64(&tgt->lun_qpair_map, key, node)
1657 btree_remove64(&tgt->lun_qpair_map, key);
1659 btree_destroy64(&tgt->lun_qpair_map);
1662 if (ha->tgt.tgt_ops &&
1663 ha->tgt.tgt_ops->remove_target &&
1664 vha->vha_tgt.target_lport_ptr)
1665 ha->tgt.tgt_ops->remove_target(vha);
1667 vha->vha_tgt.qla_tgt = NULL;
1669 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00d,
1670 "Release of tgt %p finished\n", tgt);
1675 /* ha->hardware_lock supposed to be held on entry */
1676 static int qlt_sched_sess_work(struct qla_tgt *tgt, int type,
1677 const void *param, unsigned int param_size)
1679 struct qla_tgt_sess_work_param *prm;
1680 unsigned long flags;
1682 prm = kzalloc(sizeof(*prm), GFP_ATOMIC);
1684 ql_dbg(ql_dbg_tgt_mgt, tgt->vha, 0xf050,
1685 "qla_target(%d): Unable to create session "
1686 "work, command will be refused", 0);
1690 ql_dbg(ql_dbg_tgt_mgt, tgt->vha, 0xf00e,
1691 "Scheduling work (type %d, prm %p)"
1692 " to find session for param %p (size %d, tgt %p)\n",
1693 type, prm, param, param_size, tgt);
1696 memcpy(&prm->tm_iocb, param, param_size);
1698 spin_lock_irqsave(&tgt->sess_work_lock, flags);
1699 list_add_tail(&prm->sess_works_list_entry, &tgt->sess_works_list);
1700 spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
1702 schedule_work(&tgt->sess_work);
1708 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
1710 static void qlt_send_notify_ack(struct qla_qpair *qpair,
1711 struct imm_ntfy_from_isp *ntfy,
1712 uint32_t add_flags, uint16_t resp_code, int resp_code_valid,
1713 uint16_t srr_flags, uint16_t srr_reject_code, uint8_t srr_explan)
1715 struct scsi_qla_host *vha = qpair->vha;
1716 struct qla_hw_data *ha = vha->hw;
1718 struct nack_to_isp *nack;
1720 if (!ha->flags.fw_started)
1723 ql_dbg(ql_dbg_tgt, vha, 0xe004, "Sending NOTIFY_ACK (ha=%p)\n", ha);
1725 pkt = (request_t *)__qla2x00_alloc_iocbs(qpair, NULL);
1727 ql_dbg(ql_dbg_tgt, vha, 0xe049,
1728 "qla_target(%d): %s failed: unable to allocate "
1729 "request packet\n", vha->vp_idx, __func__);
1733 if (vha->vha_tgt.qla_tgt != NULL)
1734 vha->vha_tgt.qla_tgt->notify_ack_expected++;
1736 pkt->entry_type = NOTIFY_ACK_TYPE;
1737 pkt->entry_count = 1;
1739 nack = (struct nack_to_isp *)pkt;
1740 nack->ox_id = ntfy->ox_id;
1742 nack->u.isp24.handle = QLA_TGT_SKIP_HANDLE;
1743 nack->u.isp24.nport_handle = ntfy->u.isp24.nport_handle;
1744 if (le16_to_cpu(ntfy->u.isp24.status) == IMM_NTFY_ELS) {
1745 nack->u.isp24.flags = ntfy->u.isp24.flags &
1746 cpu_to_le16(NOTIFY24XX_FLAGS_PUREX_IOCB);
1748 nack->u.isp24.srr_rx_id = ntfy->u.isp24.srr_rx_id;
1749 nack->u.isp24.status = ntfy->u.isp24.status;
1750 nack->u.isp24.status_subcode = ntfy->u.isp24.status_subcode;
1751 nack->u.isp24.fw_handle = ntfy->u.isp24.fw_handle;
1752 nack->u.isp24.exchange_address = ntfy->u.isp24.exchange_address;
1753 nack->u.isp24.srr_rel_offs = ntfy->u.isp24.srr_rel_offs;
1754 nack->u.isp24.srr_ui = ntfy->u.isp24.srr_ui;
1755 nack->u.isp24.srr_flags = cpu_to_le16(srr_flags);
1756 nack->u.isp24.srr_reject_code = srr_reject_code;
1757 nack->u.isp24.srr_reject_code_expl = srr_explan;
1758 nack->u.isp24.vp_index = ntfy->u.isp24.vp_index;
1760 /* TODO qualify this with EDIF enable */
1761 if (ntfy->u.isp24.status_subcode == ELS_PLOGI &&
1762 (le16_to_cpu(ntfy->u.isp24.flags) & NOTIFY24XX_FLAGS_FCSP)) {
1763 nack->u.isp24.flags |= cpu_to_le16(NOTIFY_ACK_FLAGS_FCSP);
1766 ql_dbg(ql_dbg_tgt, vha, 0xe005,
1767 "qla_target(%d): Sending 24xx Notify Ack %d\n",
1768 vha->vp_idx, nack->u.isp24.status);
1770 /* Memory Barrier */
1772 qla2x00_start_iocbs(vha, qpair->req);
1775 static int qlt_build_abts_resp_iocb(struct qla_tgt_mgmt_cmd *mcmd)
1777 struct scsi_qla_host *vha = mcmd->vha;
1778 struct qla_hw_data *ha = vha->hw;
1779 struct abts_resp_to_24xx *resp;
1784 struct abts_recv_from_24xx *abts = &mcmd->orig_iocb.abts;
1785 struct qla_qpair *qpair = mcmd->qpair;
1787 ql_dbg(ql_dbg_tgt, vha, 0xe006,
1788 "Sending task mgmt ABTS response (ha=%p, status=%x)\n",
1789 ha, mcmd->fc_tm_rsp);
1791 rc = qlt_check_reserve_free_req(qpair, 1);
1793 ql_dbg(ql_dbg_tgt, vha, 0xe04a,
1794 "qla_target(%d): %s failed: unable to allocate request packet\n",
1795 vha->vp_idx, __func__);
1799 resp = (struct abts_resp_to_24xx *)qpair->req->ring_ptr;
1800 memset(resp, 0, sizeof(*resp));
1802 h = qlt_make_handle(qpair);
1803 if (unlikely(h == QLA_TGT_NULL_HANDLE)) {
1805 * CTIO type 7 from the firmware doesn't provide a way to
1806 * know the initiator's LOOP ID, hence we can't find
1807 * the session and, so, the command.
1811 qpair->req->outstanding_cmds[h] = (srb_t *)mcmd;
1814 resp->handle = make_handle(qpair->req->id, h);
1815 resp->entry_type = ABTS_RESP_24XX;
1816 resp->entry_count = 1;
1817 resp->nport_handle = abts->nport_handle;
1818 resp->vp_index = vha->vp_idx;
1819 resp->sof_type = abts->sof_type;
1820 resp->exchange_address = abts->exchange_address;
1821 resp->fcp_hdr_le = abts->fcp_hdr_le;
1822 f_ctl = cpu_to_le32(F_CTL_EXCH_CONTEXT_RESP |
1823 F_CTL_LAST_SEQ | F_CTL_END_SEQ |
1824 F_CTL_SEQ_INITIATIVE);
1825 p = (uint8_t *)&f_ctl;
1826 resp->fcp_hdr_le.f_ctl[0] = *p++;
1827 resp->fcp_hdr_le.f_ctl[1] = *p++;
1828 resp->fcp_hdr_le.f_ctl[2] = *p;
1830 resp->fcp_hdr_le.d_id = abts->fcp_hdr_le.s_id;
1831 resp->fcp_hdr_le.s_id = abts->fcp_hdr_le.d_id;
1833 resp->exchange_addr_to_abort = abts->exchange_addr_to_abort;
1834 if (mcmd->fc_tm_rsp == FCP_TMF_CMPL) {
1835 resp->fcp_hdr_le.r_ctl = R_CTL_BASIC_LINK_SERV | R_CTL_B_ACC;
1836 resp->payload.ba_acct.seq_id_valid = SEQ_ID_INVALID;
1837 resp->payload.ba_acct.low_seq_cnt = 0x0000;
1838 resp->payload.ba_acct.high_seq_cnt = cpu_to_le16(0xFFFF);
1839 resp->payload.ba_acct.ox_id = abts->fcp_hdr_le.ox_id;
1840 resp->payload.ba_acct.rx_id = abts->fcp_hdr_le.rx_id;
1842 resp->fcp_hdr_le.r_ctl = R_CTL_BASIC_LINK_SERV | R_CTL_B_RJT;
1843 resp->payload.ba_rjt.reason_code =
1844 BA_RJT_REASON_CODE_UNABLE_TO_PERFORM;
1845 /* Other bytes are zero */
1848 vha->vha_tgt.qla_tgt->abts_resp_expected++;
1850 /* Memory Barrier */
1852 if (qpair->reqq_start_iocbs)
1853 qpair->reqq_start_iocbs(qpair);
1855 qla2x00_start_iocbs(vha, qpair->req);
1861 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
1863 static void qlt_24xx_send_abts_resp(struct qla_qpair *qpair,
1864 struct abts_recv_from_24xx *abts, uint32_t status,
1867 struct scsi_qla_host *vha = qpair->vha;
1868 struct qla_hw_data *ha = vha->hw;
1869 struct abts_resp_to_24xx *resp;
1873 ql_dbg(ql_dbg_tgt, vha, 0xe006,
1874 "Sending task mgmt ABTS response (ha=%p, atio=%p, status=%x\n",
1877 resp = (struct abts_resp_to_24xx *)qla2x00_alloc_iocbs_ready(qpair,
1880 ql_dbg(ql_dbg_tgt, vha, 0xe04a,
1881 "qla_target(%d): %s failed: unable to allocate "
1882 "request packet", vha->vp_idx, __func__);
1886 resp->entry_type = ABTS_RESP_24XX;
1887 resp->handle = QLA_TGT_SKIP_HANDLE;
1888 resp->entry_count = 1;
1889 resp->nport_handle = abts->nport_handle;
1890 resp->vp_index = vha->vp_idx;
1891 resp->sof_type = abts->sof_type;
1892 resp->exchange_address = abts->exchange_address;
1893 resp->fcp_hdr_le = abts->fcp_hdr_le;
1894 f_ctl = cpu_to_le32(F_CTL_EXCH_CONTEXT_RESP |
1895 F_CTL_LAST_SEQ | F_CTL_END_SEQ |
1896 F_CTL_SEQ_INITIATIVE);
1897 p = (uint8_t *)&f_ctl;
1898 resp->fcp_hdr_le.f_ctl[0] = *p++;
1899 resp->fcp_hdr_le.f_ctl[1] = *p++;
1900 resp->fcp_hdr_le.f_ctl[2] = *p;
1902 resp->fcp_hdr_le.d_id = abts->fcp_hdr_le.d_id;
1903 resp->fcp_hdr_le.s_id = abts->fcp_hdr_le.s_id;
1905 resp->fcp_hdr_le.d_id = abts->fcp_hdr_le.s_id;
1906 resp->fcp_hdr_le.s_id = abts->fcp_hdr_le.d_id;
1908 resp->exchange_addr_to_abort = abts->exchange_addr_to_abort;
1909 if (status == FCP_TMF_CMPL) {
1910 resp->fcp_hdr_le.r_ctl = R_CTL_BASIC_LINK_SERV | R_CTL_B_ACC;
1911 resp->payload.ba_acct.seq_id_valid = SEQ_ID_INVALID;
1912 resp->payload.ba_acct.low_seq_cnt = 0x0000;
1913 resp->payload.ba_acct.high_seq_cnt = cpu_to_le16(0xFFFF);
1914 resp->payload.ba_acct.ox_id = abts->fcp_hdr_le.ox_id;
1915 resp->payload.ba_acct.rx_id = abts->fcp_hdr_le.rx_id;
1917 resp->fcp_hdr_le.r_ctl = R_CTL_BASIC_LINK_SERV | R_CTL_B_RJT;
1918 resp->payload.ba_rjt.reason_code =
1919 BA_RJT_REASON_CODE_UNABLE_TO_PERFORM;
1920 /* Other bytes are zero */
1923 vha->vha_tgt.qla_tgt->abts_resp_expected++;
1925 /* Memory Barrier */
1927 if (qpair->reqq_start_iocbs)
1928 qpair->reqq_start_iocbs(qpair);
1930 qla2x00_start_iocbs(vha, qpair->req);
1934 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
1936 static void qlt_24xx_retry_term_exchange(struct scsi_qla_host *vha,
1937 struct qla_qpair *qpair, response_t *pkt, struct qla_tgt_mgmt_cmd *mcmd)
1939 struct ctio7_to_24xx *ctio;
1941 struct abts_recv_from_24xx *entry;
1943 ctio = (struct ctio7_to_24xx *)qla2x00_alloc_iocbs_ready(qpair, NULL);
1945 ql_dbg(ql_dbg_tgt, vha, 0xe04b,
1946 "qla_target(%d): %s failed: unable to allocate "
1947 "request packet\n", vha->vp_idx, __func__);
1952 /* abts from remote port */
1953 entry = &mcmd->orig_iocb.abts;
1955 /* abts from this driver. */
1956 entry = (struct abts_recv_from_24xx *)pkt;
1959 * We've got on entrance firmware's response on by us generated
1960 * ABTS response. So, in it ID fields are reversed.
1963 ctio->entry_type = CTIO_TYPE7;
1964 ctio->entry_count = 1;
1965 ctio->nport_handle = entry->nport_handle;
1966 ctio->handle = QLA_TGT_SKIP_HANDLE | CTIO_COMPLETION_HANDLE_MARK;
1967 ctio->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
1968 ctio->vp_index = vha->vp_idx;
1969 ctio->exchange_addr = entry->exchange_addr_to_abort;
1970 tmp = (CTIO7_FLAGS_STATUS_MODE_1 | CTIO7_FLAGS_TERMINATE);
1973 ctio->initiator_id = entry->fcp_hdr_le.s_id;
1975 if (mcmd->flags & QLA24XX_MGMT_ABORT_IO_ATTR_VALID)
1976 tmp |= (mcmd->abort_io_attr << 9);
1977 else if (qpair->retry_term_cnt & 1)
1980 ctio->initiator_id = entry->fcp_hdr_le.d_id;
1982 if (qpair->retry_term_cnt & 1)
1985 ctio->u.status1.flags = cpu_to_le16(tmp);
1986 ctio->u.status1.ox_id = entry->fcp_hdr_le.ox_id;
1988 ql_dbg(ql_dbg_tgt, vha, 0xe007,
1989 "Sending retry TERM EXCH CTIO7 flags %04xh oxid %04xh attr valid %x\n",
1990 le16_to_cpu(ctio->u.status1.flags),
1991 le16_to_cpu(ctio->u.status1.ox_id),
1992 (mcmd && mcmd->flags & QLA24XX_MGMT_ABORT_IO_ATTR_VALID) ? 1 : 0);
1994 /* Memory Barrier */
1996 if (qpair->reqq_start_iocbs)
1997 qpair->reqq_start_iocbs(qpair);
1999 qla2x00_start_iocbs(vha, qpair->req);
2002 qlt_build_abts_resp_iocb(mcmd);
2004 qlt_24xx_send_abts_resp(qpair,
2005 (struct abts_recv_from_24xx *)entry, FCP_TMF_CMPL, true);
2009 /* drop cmds for the given lun
2010 * XXX only looks for cmds on the port through which lun reset was recieved
2011 * XXX does not go through the list of other port (which may have cmds
2014 static void abort_cmds_for_lun(struct scsi_qla_host *vha, u64 lun, be_id_t s_id)
2016 struct qla_tgt_sess_op *op;
2017 struct qla_tgt_cmd *cmd;
2019 unsigned long flags;
2021 key = sid_to_key(s_id);
2022 spin_lock_irqsave(&vha->cmd_list_lock, flags);
2023 list_for_each_entry(op, &vha->unknown_atio_list, cmd_list) {
2027 op_key = sid_to_key(op->atio.u.isp24.fcp_hdr.s_id);
2028 op_lun = scsilun_to_int(
2029 (struct scsi_lun *)&op->atio.u.isp24.fcp_cmnd.lun);
2030 if (op_key == key && op_lun == lun)
2034 list_for_each_entry(cmd, &vha->qla_cmd_list, cmd_list) {
2038 cmd_key = sid_to_key(cmd->atio.u.isp24.fcp_hdr.s_id);
2039 cmd_lun = scsilun_to_int(
2040 (struct scsi_lun *)&cmd->atio.u.isp24.fcp_cmnd.lun);
2041 if (cmd_key == key && cmd_lun == lun)
2044 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
2047 static struct qla_qpair_hint *qlt_find_qphint(struct scsi_qla_host *vha,
2048 uint64_t unpacked_lun)
2050 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
2051 struct qla_qpair_hint *h = NULL;
2053 if (vha->flags.qpairs_available) {
2054 h = btree_lookup64(&tgt->lun_qpair_map, unpacked_lun);
2056 h = &tgt->qphints[0];
2058 h = &tgt->qphints[0];
2064 static void qlt_do_tmr_work(struct work_struct *work)
2066 struct qla_tgt_mgmt_cmd *mcmd =
2067 container_of(work, struct qla_tgt_mgmt_cmd, work);
2068 struct qla_hw_data *ha = mcmd->vha->hw;
2071 unsigned long flags;
2073 switch (mcmd->tmr_func) {
2075 tag = le32_to_cpu(mcmd->orig_iocb.abts.exchange_addr_to_abort);
2082 rc = ha->tgt.tgt_ops->handle_tmr(mcmd, mcmd->unpacked_lun,
2083 mcmd->tmr_func, tag);
2086 spin_lock_irqsave(mcmd->qpair->qp_lock_ptr, flags);
2087 switch (mcmd->tmr_func) {
2089 mcmd->fc_tm_rsp = FCP_TMF_REJECTED;
2090 qlt_build_abts_resp_iocb(mcmd);
2092 case QLA_TGT_LUN_RESET:
2093 case QLA_TGT_CLEAR_TS:
2094 case QLA_TGT_ABORT_TS:
2095 case QLA_TGT_CLEAR_ACA:
2096 case QLA_TGT_TARGET_RESET:
2097 qlt_send_busy(mcmd->qpair, &mcmd->orig_iocb.atio,
2101 case QLA_TGT_ABORT_ALL:
2102 case QLA_TGT_NEXUS_LOSS_SESS:
2103 case QLA_TGT_NEXUS_LOSS:
2104 qlt_send_notify_ack(mcmd->qpair,
2105 &mcmd->orig_iocb.imm_ntfy, 0, 0, 0, 0, 0, 0);
2108 spin_unlock_irqrestore(mcmd->qpair->qp_lock_ptr, flags);
2110 ql_dbg(ql_dbg_tgt_mgt, mcmd->vha, 0xf052,
2111 "qla_target(%d): tgt_ops->handle_tmr() failed: %d\n",
2112 mcmd->vha->vp_idx, rc);
2113 mempool_free(mcmd, qla_tgt_mgmt_cmd_mempool);
2117 /* ha->hardware_lock supposed to be held on entry */
2118 static int __qlt_24xx_handle_abts(struct scsi_qla_host *vha,
2119 struct abts_recv_from_24xx *abts, struct fc_port *sess)
2121 struct qla_hw_data *ha = vha->hw;
2122 struct qla_tgt_mgmt_cmd *mcmd;
2123 struct qla_qpair_hint *h = &vha->vha_tgt.qla_tgt->qphints[0];
2124 struct qla_tgt_cmd *abort_cmd;
2126 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00f,
2127 "qla_target(%d): task abort (tag=%d)\n",
2128 vha->vp_idx, abts->exchange_addr_to_abort);
2130 mcmd = mempool_alloc(qla_tgt_mgmt_cmd_mempool, GFP_ATOMIC);
2132 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf051,
2133 "qla_target(%d): %s: Allocation of ABORT cmd failed",
2134 vha->vp_idx, __func__);
2137 memset(mcmd, 0, sizeof(*mcmd));
2138 mcmd->cmd_type = TYPE_TGT_TMCMD;
2140 memcpy(&mcmd->orig_iocb.abts, abts, sizeof(mcmd->orig_iocb.abts));
2141 mcmd->reset_count = ha->base_qpair->chip_reset;
2142 mcmd->tmr_func = QLA_TGT_ABTS;
2143 mcmd->qpair = h->qpair;
2147 * LUN is looked up by target-core internally based on the passed
2148 * abts->exchange_addr_to_abort tag.
2150 mcmd->se_cmd.cpuid = h->cpuid;
2152 abort_cmd = ha->tgt.tgt_ops->find_cmd_by_tag(sess,
2153 le32_to_cpu(abts->exchange_addr_to_abort));
2156 mcmd->unpacked_lun = abort_cmd->se_cmd.orig_fe_lun;
2158 if (abort_cmd->qpair) {
2159 mcmd->qpair = abort_cmd->qpair;
2160 mcmd->se_cmd.cpuid = abort_cmd->se_cmd.cpuid;
2161 mcmd->abort_io_attr = abort_cmd->atio.u.isp24.attr;
2162 mcmd->flags = QLA24XX_MGMT_ABORT_IO_ATTR_VALID;
2165 INIT_WORK(&mcmd->work, qlt_do_tmr_work);
2166 queue_work_on(mcmd->se_cmd.cpuid, qla_tgt_wq, &mcmd->work);
2172 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
2174 static void qlt_24xx_handle_abts(struct scsi_qla_host *vha,
2175 struct abts_recv_from_24xx *abts)
2177 struct qla_hw_data *ha = vha->hw;
2178 struct fc_port *sess;
2179 uint32_t tag = le32_to_cpu(abts->exchange_addr_to_abort);
2182 unsigned long flags;
2184 if (le32_to_cpu(abts->fcp_hdr_le.parameter) & ABTS_PARAM_ABORT_SEQ) {
2185 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf053,
2186 "qla_target(%d): ABTS: Abort Sequence not "
2187 "supported\n", vha->vp_idx);
2188 qlt_24xx_send_abts_resp(ha->base_qpair, abts, FCP_TMF_REJECTED,
2193 if (tag == ATIO_EXCHANGE_ADDRESS_UNKNOWN) {
2194 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf010,
2195 "qla_target(%d): ABTS: Unknown Exchange "
2196 "Address received\n", vha->vp_idx);
2197 qlt_24xx_send_abts_resp(ha->base_qpair, abts, FCP_TMF_REJECTED,
2202 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf011,
2203 "qla_target(%d): task abort (s_id=%x:%x:%x, "
2204 "tag=%d, param=%x)\n", vha->vp_idx, abts->fcp_hdr_le.s_id.domain,
2205 abts->fcp_hdr_le.s_id.area, abts->fcp_hdr_le.s_id.al_pa, tag,
2206 le32_to_cpu(abts->fcp_hdr_le.parameter));
2208 s_id = le_id_to_be(abts->fcp_hdr_le.s_id);
2210 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
2211 sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha, s_id);
2213 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf012,
2214 "qla_target(%d): task abort for non-existent session\n",
2216 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
2218 qlt_24xx_send_abts_resp(ha->base_qpair, abts, FCP_TMF_REJECTED,
2222 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
2225 if (sess->deleted) {
2226 qlt_24xx_send_abts_resp(ha->base_qpair, abts, FCP_TMF_REJECTED,
2231 rc = __qlt_24xx_handle_abts(vha, abts, sess);
2233 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf054,
2234 "qla_target(%d): __qlt_24xx_handle_abts() failed: %d\n",
2236 qlt_24xx_send_abts_resp(ha->base_qpair, abts, FCP_TMF_REJECTED,
2243 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
2245 static void qlt_24xx_send_task_mgmt_ctio(struct qla_qpair *qpair,
2246 struct qla_tgt_mgmt_cmd *mcmd, uint32_t resp_code)
2248 struct scsi_qla_host *ha = mcmd->vha;
2249 struct atio_from_isp *atio = &mcmd->orig_iocb.atio;
2250 struct ctio7_to_24xx *ctio;
2253 ql_dbg(ql_dbg_tgt, ha, 0xe008,
2254 "Sending task mgmt CTIO7 (ha=%p, atio=%p, resp_code=%x\n",
2255 ha, atio, resp_code);
2258 ctio = (struct ctio7_to_24xx *)__qla2x00_alloc_iocbs(qpair, NULL);
2260 ql_dbg(ql_dbg_tgt, ha, 0xe04c,
2261 "qla_target(%d): %s failed: unable to allocate "
2262 "request packet\n", ha->vp_idx, __func__);
2266 ctio->entry_type = CTIO_TYPE7;
2267 ctio->entry_count = 1;
2268 ctio->handle = QLA_TGT_SKIP_HANDLE | CTIO_COMPLETION_HANDLE_MARK;
2269 ctio->nport_handle = cpu_to_le16(mcmd->sess->loop_id);
2270 ctio->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
2271 ctio->vp_index = ha->vp_idx;
2272 ctio->initiator_id = be_id_to_le(atio->u.isp24.fcp_hdr.s_id);
2273 ctio->exchange_addr = atio->u.isp24.exchange_addr;
2274 temp = (atio->u.isp24.attr << 9)|
2275 CTIO7_FLAGS_STATUS_MODE_1 | CTIO7_FLAGS_SEND_STATUS;
2276 ctio->u.status1.flags = cpu_to_le16(temp);
2277 temp = be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id);
2278 ctio->u.status1.ox_id = cpu_to_le16(temp);
2279 ctio->u.status1.scsi_status =
2280 cpu_to_le16(SS_RESPONSE_INFO_LEN_VALID);
2281 ctio->u.status1.response_len = cpu_to_le16(8);
2282 ctio->u.status1.sense_data[0] = resp_code;
2284 /* Memory Barrier */
2286 if (qpair->reqq_start_iocbs)
2287 qpair->reqq_start_iocbs(qpair);
2289 qla2x00_start_iocbs(ha, qpair->req);
2292 void qlt_free_mcmd(struct qla_tgt_mgmt_cmd *mcmd)
2294 mempool_free(mcmd, qla_tgt_mgmt_cmd_mempool);
2296 EXPORT_SYMBOL(qlt_free_mcmd);
2299 * ha->hardware_lock supposed to be held on entry. Might drop it, then
2302 void qlt_send_resp_ctio(struct qla_qpair *qpair, struct qla_tgt_cmd *cmd,
2303 uint8_t scsi_status, uint8_t sense_key, uint8_t asc, uint8_t ascq)
2305 struct atio_from_isp *atio = &cmd->atio;
2306 struct ctio7_to_24xx *ctio;
2308 struct scsi_qla_host *vha = cmd->vha;
2310 ql_dbg(ql_dbg_tgt_dif, vha, 0x3066,
2311 "Sending response CTIO7 (vha=%p, atio=%p, scsi_status=%02x, "
2312 "sense_key=%02x, asc=%02x, ascq=%02x",
2313 vha, atio, scsi_status, sense_key, asc, ascq);
2315 ctio = (struct ctio7_to_24xx *)qla2x00_alloc_iocbs(vha, NULL);
2317 ql_dbg(ql_dbg_async, vha, 0x3067,
2318 "qla2x00t(%ld): %s failed: unable to allocate request packet",
2319 vha->host_no, __func__);
2323 ctio->entry_type = CTIO_TYPE7;
2324 ctio->entry_count = 1;
2325 ctio->handle = QLA_TGT_SKIP_HANDLE;
2326 ctio->nport_handle = cpu_to_le16(cmd->sess->loop_id);
2327 ctio->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
2328 ctio->vp_index = vha->vp_idx;
2329 ctio->initiator_id = be_id_to_le(atio->u.isp24.fcp_hdr.s_id);
2330 ctio->exchange_addr = atio->u.isp24.exchange_addr;
2331 temp = (atio->u.isp24.attr << 9) |
2332 CTIO7_FLAGS_STATUS_MODE_1 | CTIO7_FLAGS_SEND_STATUS;
2333 ctio->u.status1.flags = cpu_to_le16(temp);
2334 temp = be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id);
2335 ctio->u.status1.ox_id = cpu_to_le16(temp);
2336 ctio->u.status1.scsi_status =
2337 cpu_to_le16(SS_RESPONSE_INFO_LEN_VALID | scsi_status);
2338 ctio->u.status1.response_len = cpu_to_le16(18);
2339 ctio->u.status1.residual = cpu_to_le32(get_datalen_for_atio(atio));
2341 if (ctio->u.status1.residual != 0)
2342 ctio->u.status1.scsi_status |=
2343 cpu_to_le16(SS_RESIDUAL_UNDER);
2345 /* Fixed format sense data. */
2346 ctio->u.status1.sense_data[0] = 0x70;
2347 ctio->u.status1.sense_data[2] = sense_key;
2348 /* Additional sense length */
2349 ctio->u.status1.sense_data[7] = 0xa;
2351 ctio->u.status1.sense_data[12] = asc;
2352 ctio->u.status1.sense_data[13] = ascq;
2354 /* Memory Barrier */
2357 if (qpair->reqq_start_iocbs)
2358 qpair->reqq_start_iocbs(qpair);
2360 qla2x00_start_iocbs(vha, qpair->req);
2366 /* callback from target fabric module code */
2367 void qlt_xmit_tm_rsp(struct qla_tgt_mgmt_cmd *mcmd)
2369 struct scsi_qla_host *vha = mcmd->sess->vha;
2370 struct qla_hw_data *ha = vha->hw;
2371 unsigned long flags;
2372 struct qla_qpair *qpair = mcmd->qpair;
2373 bool free_mcmd = true;
2375 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf013,
2376 "TM response mcmd (%p) status %#x state %#x",
2377 mcmd, mcmd->fc_tm_rsp, mcmd->flags);
2379 spin_lock_irqsave(qpair->qp_lock_ptr, flags);
2381 if (!vha->flags.online || mcmd->reset_count != qpair->chip_reset) {
2383 * Either the port is not online or this request was from
2384 * previous life, just abort the processing.
2386 ql_dbg(ql_dbg_async, vha, 0xe100,
2387 "RESET-TMR online/active/old-count/new-count = %d/%d/%d/%d.\n",
2388 vha->flags.online, qla2x00_reset_active(vha),
2389 mcmd->reset_count, qpair->chip_reset);
2390 ha->tgt.tgt_ops->free_mcmd(mcmd);
2391 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
2395 if (mcmd->flags == QLA24XX_MGMT_SEND_NACK) {
2396 switch (mcmd->orig_iocb.imm_ntfy.u.isp24.status_subcode) {
2400 ql_dbg(ql_dbg_disc, vha, 0x2106,
2401 "TM response logo %8phC status %#x state %#x",
2402 mcmd->sess->port_name, mcmd->fc_tm_rsp,
2404 qlt_schedule_sess_for_deletion(mcmd->sess);
2407 qlt_send_notify_ack(vha->hw->base_qpair,
2408 &mcmd->orig_iocb.imm_ntfy, 0, 0, 0, 0, 0, 0);
2412 if (mcmd->orig_iocb.atio.u.raw.entry_type == ABTS_RECV_24XX) {
2413 qlt_build_abts_resp_iocb(mcmd);
2416 qlt_24xx_send_task_mgmt_ctio(qpair, mcmd,
2420 * Make the callback for ->free_mcmd() to queue_work() and invoke
2421 * target_put_sess_cmd() to drop cmd_kref to 1. The final
2422 * target_put_sess_cmd() call will be made from TFO->check_stop_free()
2423 * -> tcm_qla2xxx_check_stop_free() to release the TMR associated se_cmd
2424 * descriptor after TFO->queue_tm_rsp() -> tcm_qla2xxx_queue_tm_rsp() ->
2425 * qlt_xmit_tm_rsp() returns here..
2428 ha->tgt.tgt_ops->free_mcmd(mcmd);
2430 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
2432 EXPORT_SYMBOL(qlt_xmit_tm_rsp);
2435 static int qlt_pci_map_calc_cnt(struct qla_tgt_prm *prm)
2437 struct qla_tgt_cmd *cmd = prm->cmd;
2439 BUG_ON(cmd->sg_cnt == 0);
2441 prm->sg = (struct scatterlist *)cmd->sg;
2442 prm->seg_cnt = dma_map_sg(&cmd->qpair->pdev->dev, cmd->sg,
2443 cmd->sg_cnt, cmd->dma_data_direction);
2444 if (unlikely(prm->seg_cnt == 0))
2447 prm->cmd->sg_mapped = 1;
2449 if (cmd->se_cmd.prot_op == TARGET_PROT_NORMAL) {
2451 * If greater than four sg entries then we need to allocate
2452 * the continuation entries
2454 if (prm->seg_cnt > QLA_TGT_DATASEGS_PER_CMD_24XX)
2455 prm->req_cnt += DIV_ROUND_UP(prm->seg_cnt -
2456 QLA_TGT_DATASEGS_PER_CMD_24XX,
2457 QLA_TGT_DATASEGS_PER_CONT_24XX);
2460 if ((cmd->se_cmd.prot_op == TARGET_PROT_DIN_INSERT) ||
2461 (cmd->se_cmd.prot_op == TARGET_PROT_DOUT_STRIP)) {
2462 prm->seg_cnt = DIV_ROUND_UP(cmd->bufflen, cmd->blk_sz);
2463 prm->tot_dsds = prm->seg_cnt;
2465 prm->tot_dsds = prm->seg_cnt;
2467 if (cmd->prot_sg_cnt) {
2468 prm->prot_sg = cmd->prot_sg;
2469 prm->prot_seg_cnt = dma_map_sg(&cmd->qpair->pdev->dev,
2470 cmd->prot_sg, cmd->prot_sg_cnt,
2471 cmd->dma_data_direction);
2472 if (unlikely(prm->prot_seg_cnt == 0))
2475 if ((cmd->se_cmd.prot_op == TARGET_PROT_DIN_INSERT) ||
2476 (cmd->se_cmd.prot_op == TARGET_PROT_DOUT_STRIP)) {
2477 /* Dif Bundling not support here */
2478 prm->prot_seg_cnt = DIV_ROUND_UP(cmd->bufflen,
2480 prm->tot_dsds += prm->prot_seg_cnt;
2482 prm->tot_dsds += prm->prot_seg_cnt;
2489 ql_dbg_qp(ql_dbg_tgt, prm->cmd->qpair, 0xe04d,
2490 "qla_target(%d): PCI mapping failed: sg_cnt=%d",
2491 0, prm->cmd->sg_cnt);
2495 static void qlt_unmap_sg(struct scsi_qla_host *vha, struct qla_tgt_cmd *cmd)
2497 struct qla_hw_data *ha;
2498 struct qla_qpair *qpair;
2500 if (!cmd->sg_mapped)
2505 dma_unmap_sg(&qpair->pdev->dev, cmd->sg, cmd->sg_cnt,
2506 cmd->dma_data_direction);
2509 if (cmd->prot_sg_cnt)
2510 dma_unmap_sg(&qpair->pdev->dev, cmd->prot_sg, cmd->prot_sg_cnt,
2511 cmd->dma_data_direction);
2516 if (cmd->ctx_dsd_alloced)
2517 qla2x00_clean_dsd_pool(ha, cmd->ctx);
2519 dma_pool_free(ha->dl_dma_pool, cmd->ctx, cmd->ctx->crc_ctx_dma);
2522 static int qlt_check_reserve_free_req(struct qla_qpair *qpair,
2526 struct req_que *req = qpair->req;
2528 if (req->cnt < (req_cnt + 2)) {
2529 cnt = (uint16_t)(qpair->use_shadow_reg ? *req->out_ptr :
2530 rd_reg_dword_relaxed(req->req_q_out));
2532 if (req->ring_index < cnt)
2533 req->cnt = cnt - req->ring_index;
2535 req->cnt = req->length - (req->ring_index - cnt);
2537 if (unlikely(req->cnt < (req_cnt + 2)))
2541 req->cnt -= req_cnt;
2547 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
2549 static inline void *qlt_get_req_pkt(struct req_que *req)
2551 /* Adjust ring index. */
2553 if (req->ring_index == req->length) {
2554 req->ring_index = 0;
2555 req->ring_ptr = req->ring;
2559 return (cont_entry_t *)req->ring_ptr;
2562 /* ha->hardware_lock supposed to be held on entry */
2563 static inline uint32_t qlt_make_handle(struct qla_qpair *qpair)
2568 struct req_que *req = qpair->req;
2570 h = req->current_outstanding_cmd;
2572 for (index = 1; index < req->num_outstanding_cmds; index++) {
2574 if (h == req->num_outstanding_cmds)
2577 if (h == QLA_TGT_SKIP_HANDLE)
2580 if (!req->outstanding_cmds[h]) {
2587 req->current_outstanding_cmd = h;
2589 ql_dbg(ql_dbg_io, qpair->vha, 0x305b,
2590 "qla_target(%d): Ran out of empty cmd slots\n",
2591 qpair->vha->vp_idx);
2592 h = QLA_TGT_NULL_HANDLE;
2598 /* ha->hardware_lock supposed to be held on entry */
2599 static int qlt_24xx_build_ctio_pkt(struct qla_qpair *qpair,
2600 struct qla_tgt_prm *prm)
2603 struct ctio7_to_24xx *pkt;
2604 struct atio_from_isp *atio = &prm->cmd->atio;
2606 struct qla_tgt_cmd *cmd = prm->cmd;
2608 pkt = (struct ctio7_to_24xx *)qpair->req->ring_ptr;
2610 memset(pkt, 0, sizeof(*pkt));
2612 pkt->entry_type = CTIO_TYPE7;
2613 pkt->entry_count = (uint8_t)prm->req_cnt;
2614 pkt->vp_index = prm->cmd->vp_idx;
2616 h = qlt_make_handle(qpair);
2617 if (unlikely(h == QLA_TGT_NULL_HANDLE)) {
2619 * CTIO type 7 from the firmware doesn't provide a way to
2620 * know the initiator's LOOP ID, hence we can't find
2621 * the session and, so, the command.
2625 qpair->req->outstanding_cmds[h] = (srb_t *)prm->cmd;
2627 pkt->handle = make_handle(qpair->req->id, h);
2628 pkt->handle |= CTIO_COMPLETION_HANDLE_MARK;
2629 pkt->nport_handle = cpu_to_le16(prm->cmd->loop_id);
2630 pkt->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
2631 pkt->initiator_id = be_id_to_le(atio->u.isp24.fcp_hdr.s_id);
2632 pkt->exchange_addr = atio->u.isp24.exchange_addr;
2633 temp = atio->u.isp24.attr << 9;
2634 pkt->u.status0.flags |= cpu_to_le16(temp);
2635 temp = be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id);
2636 pkt->u.status0.ox_id = cpu_to_le16(temp);
2637 pkt->u.status0.relative_offset = cpu_to_le32(prm->cmd->offset);
2640 if (cmd->dma_data_direction == DMA_TO_DEVICE)
2641 prm->cmd->sess->edif.rx_bytes += cmd->bufflen;
2642 if (cmd->dma_data_direction == DMA_FROM_DEVICE)
2643 prm->cmd->sess->edif.tx_bytes += cmd->bufflen;
2645 pkt->u.status0.edif_flags |= EF_EN_EDIF;
2652 * ha->hardware_lock supposed to be held on entry. We have already made sure
2653 * that there is sufficient amount of request entries to not drop it.
2655 static void qlt_load_cont_data_segments(struct qla_tgt_prm *prm)
2658 struct dsd64 *cur_dsd;
2660 /* Build continuation packets */
2661 while (prm->seg_cnt > 0) {
2662 cont_a64_entry_t *cont_pkt64 =
2663 (cont_a64_entry_t *)qlt_get_req_pkt(
2664 prm->cmd->qpair->req);
2667 * Make sure that from cont_pkt64 none of
2668 * 64-bit specific fields used for 32-bit
2669 * addressing. Cast to (cont_entry_t *) for
2673 memset(cont_pkt64, 0, sizeof(*cont_pkt64));
2675 cont_pkt64->entry_count = 1;
2676 cont_pkt64->sys_define = 0;
2678 cont_pkt64->entry_type = CONTINUE_A64_TYPE;
2679 cur_dsd = cont_pkt64->dsd;
2681 /* Load continuation entry data segments */
2683 cnt < QLA_TGT_DATASEGS_PER_CONT_24XX && prm->seg_cnt;
2684 cnt++, prm->seg_cnt--) {
2685 append_dsd64(&cur_dsd, prm->sg);
2686 prm->sg = sg_next(prm->sg);
2692 * ha->hardware_lock supposed to be held on entry. We have already made sure
2693 * that there is sufficient amount of request entries to not drop it.
2695 static void qlt_load_data_segments(struct qla_tgt_prm *prm)
2698 struct dsd64 *cur_dsd;
2699 struct ctio7_to_24xx *pkt24 = (struct ctio7_to_24xx *)prm->pkt;
2701 pkt24->u.status0.transfer_length = cpu_to_le32(prm->cmd->bufflen);
2703 /* Setup packet address segment pointer */
2704 cur_dsd = &pkt24->u.status0.dsd;
2706 /* Set total data segment count */
2708 pkt24->dseg_count = cpu_to_le16(prm->seg_cnt);
2710 if (prm->seg_cnt == 0) {
2711 /* No data transfer */
2712 cur_dsd->address = 0;
2713 cur_dsd->length = 0;
2717 /* If scatter gather */
2719 /* Load command entry data segments */
2721 (cnt < QLA_TGT_DATASEGS_PER_CMD_24XX) && prm->seg_cnt;
2722 cnt++, prm->seg_cnt--) {
2723 append_dsd64(&cur_dsd, prm->sg);
2724 prm->sg = sg_next(prm->sg);
2727 qlt_load_cont_data_segments(prm);
2730 static inline int qlt_has_data(struct qla_tgt_cmd *cmd)
2732 return cmd->bufflen > 0;
2735 static void qlt_print_dif_err(struct qla_tgt_prm *prm)
2737 struct qla_tgt_cmd *cmd;
2738 struct scsi_qla_host *vha;
2740 /* asc 0x10=dif error */
2741 if (prm->sense_buffer && (prm->sense_buffer[12] == 0x10)) {
2745 switch (prm->sense_buffer[13]) {
2747 ql_dbg(ql_dbg_tgt_dif, vha, 0xe00b,
2748 "BE detected Guard TAG ERR: lba[0x%llx|%lld] len[0x%x] "
2749 "se_cmd=%p tag[%x]",
2750 cmd->lba, cmd->lba, cmd->num_blks, &cmd->se_cmd,
2751 cmd->atio.u.isp24.exchange_addr);
2754 ql_dbg(ql_dbg_tgt_dif, vha, 0xe00c,
2755 "BE detected APP TAG ERR: lba[0x%llx|%lld] len[0x%x] "
2756 "se_cmd=%p tag[%x]",
2757 cmd->lba, cmd->lba, cmd->num_blks, &cmd->se_cmd,
2758 cmd->atio.u.isp24.exchange_addr);
2761 ql_dbg(ql_dbg_tgt_dif, vha, 0xe00f,
2762 "BE detected REF TAG ERR: lba[0x%llx|%lld] len[0x%x] "
2763 "se_cmd=%p tag[%x]",
2764 cmd->lba, cmd->lba, cmd->num_blks, &cmd->se_cmd,
2765 cmd->atio.u.isp24.exchange_addr);
2768 ql_dbg(ql_dbg_tgt_dif, vha, 0xe010,
2769 "BE detected Dif ERR: lba[%llx|%lld] len[%x] "
2770 "se_cmd=%p tag[%x]",
2771 cmd->lba, cmd->lba, cmd->num_blks, &cmd->se_cmd,
2772 cmd->atio.u.isp24.exchange_addr);
2775 ql_dump_buffer(ql_dbg_tgt_dif, vha, 0xe011, cmd->cdb, 16);
2780 * Called without ha->hardware_lock held
2782 static int qlt_pre_xmit_response(struct qla_tgt_cmd *cmd,
2783 struct qla_tgt_prm *prm, int xmit_type, uint8_t scsi_status,
2784 uint32_t *full_req_cnt)
2786 struct se_cmd *se_cmd = &cmd->se_cmd;
2787 struct qla_qpair *qpair = cmd->qpair;
2790 prm->tgt = cmd->tgt;
2792 prm->rq_result = scsi_status;
2793 prm->sense_buffer = &cmd->sense_buffer[0];
2794 prm->sense_buffer_len = TRANSPORT_SENSE_BUFFER;
2799 prm->add_status_pkt = 0;
2800 prm->prot_sg = NULL;
2801 prm->prot_seg_cnt = 0;
2804 if ((xmit_type & QLA_TGT_XMIT_DATA) && qlt_has_data(cmd)) {
2805 if (qlt_pci_map_calc_cnt(prm) != 0)
2809 *full_req_cnt = prm->req_cnt;
2811 if (se_cmd->se_cmd_flags & SCF_UNDERFLOW_BIT) {
2812 prm->residual = se_cmd->residual_count;
2813 ql_dbg_qp(ql_dbg_io + ql_dbg_verbose, qpair, 0x305c,
2814 "Residual underflow: %d (tag %lld, op %x, bufflen %d, rq_result %x)\n",
2815 prm->residual, se_cmd->tag,
2816 se_cmd->t_task_cdb ? se_cmd->t_task_cdb[0] : 0,
2817 cmd->bufflen, prm->rq_result);
2818 prm->rq_result |= SS_RESIDUAL_UNDER;
2819 } else if (se_cmd->se_cmd_flags & SCF_OVERFLOW_BIT) {
2820 prm->residual = se_cmd->residual_count;
2821 ql_dbg_qp(ql_dbg_io, qpair, 0x305d,
2822 "Residual overflow: %d (tag %lld, op %x, bufflen %d, rq_result %x)\n",
2823 prm->residual, se_cmd->tag, se_cmd->t_task_cdb ?
2824 se_cmd->t_task_cdb[0] : 0, cmd->bufflen, prm->rq_result);
2825 prm->rq_result |= SS_RESIDUAL_OVER;
2828 if (xmit_type & QLA_TGT_XMIT_STATUS) {
2830 * If QLA_TGT_XMIT_DATA is not set, add_status_pkt will be
2831 * ignored in *xmit_response() below
2833 if (qlt_has_data(cmd)) {
2834 if (QLA_TGT_SENSE_VALID(prm->sense_buffer) ||
2835 (IS_FWI2_CAPABLE(cmd->vha->hw) &&
2836 (prm->rq_result != 0))) {
2837 prm->add_status_pkt = 1;
2846 static inline int qlt_need_explicit_conf(struct qla_tgt_cmd *cmd,
2849 if (cmd->qpair->enable_class_2)
2853 return cmd->conf_compl_supported;
2855 return cmd->qpair->enable_explicit_conf &&
2856 cmd->conf_compl_supported;
2859 static void qlt_24xx_init_ctio_to_isp(struct ctio7_to_24xx *ctio,
2860 struct qla_tgt_prm *prm)
2862 prm->sense_buffer_len = min_t(uint32_t, prm->sense_buffer_len,
2863 (uint32_t)sizeof(ctio->u.status1.sense_data));
2864 ctio->u.status0.flags |= cpu_to_le16(CTIO7_FLAGS_SEND_STATUS);
2865 if (qlt_need_explicit_conf(prm->cmd, 0)) {
2866 ctio->u.status0.flags |= cpu_to_le16(
2867 CTIO7_FLAGS_EXPLICIT_CONFORM |
2868 CTIO7_FLAGS_CONFORM_REQ);
2870 ctio->u.status0.residual = cpu_to_le32(prm->residual);
2871 ctio->u.status0.scsi_status = cpu_to_le16(prm->rq_result);
2872 if (QLA_TGT_SENSE_VALID(prm->sense_buffer)) {
2875 if (qlt_need_explicit_conf(prm->cmd, 1)) {
2876 if ((prm->rq_result & SS_SCSI_STATUS_BYTE) != 0) {
2877 ql_dbg_qp(ql_dbg_tgt, prm->cmd->qpair, 0xe017,
2878 "Skipping EXPLICIT_CONFORM and "
2879 "CTIO7_FLAGS_CONFORM_REQ for FCP READ w/ "
2880 "non GOOD status\n");
2881 goto skip_explict_conf;
2883 ctio->u.status1.flags |= cpu_to_le16(
2884 CTIO7_FLAGS_EXPLICIT_CONFORM |
2885 CTIO7_FLAGS_CONFORM_REQ);
2888 ctio->u.status1.flags &=
2889 ~cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_0);
2890 ctio->u.status1.flags |=
2891 cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_1);
2892 ctio->u.status1.scsi_status |=
2893 cpu_to_le16(SS_SENSE_LEN_VALID);
2894 ctio->u.status1.sense_length =
2895 cpu_to_le16(prm->sense_buffer_len);
2896 for (i = 0; i < prm->sense_buffer_len/4; i++) {
2899 v = get_unaligned_be32(
2900 &((uint32_t *)prm->sense_buffer)[i]);
2901 put_unaligned_le32(v,
2902 &((uint32_t *)ctio->u.status1.sense_data)[i]);
2904 qlt_print_dif_err(prm);
2907 ctio->u.status1.flags &=
2908 ~cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_0);
2909 ctio->u.status1.flags |=
2910 cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_1);
2911 ctio->u.status1.sense_length = 0;
2912 memset(ctio->u.status1.sense_data, 0,
2913 sizeof(ctio->u.status1.sense_data));
2916 /* Sense with len > 24, is it possible ??? */
2920 qlt_hba_err_chk_enabled(struct se_cmd *se_cmd)
2922 switch (se_cmd->prot_op) {
2923 case TARGET_PROT_DOUT_INSERT:
2924 case TARGET_PROT_DIN_STRIP:
2925 if (ql2xenablehba_err_chk >= 1)
2928 case TARGET_PROT_DOUT_PASS:
2929 case TARGET_PROT_DIN_PASS:
2930 if (ql2xenablehba_err_chk >= 2)
2933 case TARGET_PROT_DIN_INSERT:
2934 case TARGET_PROT_DOUT_STRIP:
2943 qla_tgt_ref_mask_check(struct se_cmd *se_cmd)
2945 switch (se_cmd->prot_op) {
2946 case TARGET_PROT_DIN_INSERT:
2947 case TARGET_PROT_DOUT_INSERT:
2948 case TARGET_PROT_DIN_STRIP:
2949 case TARGET_PROT_DOUT_STRIP:
2950 case TARGET_PROT_DIN_PASS:
2951 case TARGET_PROT_DOUT_PASS:
2960 * qla_tgt_set_dif_tags - Extract Ref and App tags from SCSI command
2963 qla_tgt_set_dif_tags(struct qla_tgt_cmd *cmd, struct crc_context *ctx,
2964 uint16_t *pfw_prot_opts)
2966 struct se_cmd *se_cmd = &cmd->se_cmd;
2967 uint32_t lba = 0xffffffff & se_cmd->t_task_lba;
2968 scsi_qla_host_t *vha = cmd->tgt->vha;
2969 struct qla_hw_data *ha = vha->hw;
2973 * wait till Mode Sense/Select cmd, modepage Ah, subpage 2
2974 * have been immplemented by TCM, before AppTag is avail.
2975 * Look for modesense_handlers[]
2978 ctx->app_tag_mask[0] = 0x0;
2979 ctx->app_tag_mask[1] = 0x0;
2981 if (IS_PI_UNINIT_CAPABLE(ha)) {
2982 if ((se_cmd->prot_type == TARGET_DIF_TYPE1_PROT) ||
2983 (se_cmd->prot_type == TARGET_DIF_TYPE2_PROT))
2984 *pfw_prot_opts |= PO_DIS_VALD_APP_ESC;
2985 else if (se_cmd->prot_type == TARGET_DIF_TYPE3_PROT)
2986 *pfw_prot_opts |= PO_DIS_VALD_APP_REF_ESC;
2989 t32 = ha->tgt.tgt_ops->get_dif_tags(cmd, pfw_prot_opts);
2991 switch (se_cmd->prot_type) {
2992 case TARGET_DIF_TYPE0_PROT:
2994 * No check for ql2xenablehba_err_chk, as it
2995 * would be an I/O error if hba tag generation
2998 ctx->ref_tag = cpu_to_le32(lba);
2999 /* enable ALL bytes of the ref tag */
3000 ctx->ref_tag_mask[0] = 0xff;
3001 ctx->ref_tag_mask[1] = 0xff;
3002 ctx->ref_tag_mask[2] = 0xff;
3003 ctx->ref_tag_mask[3] = 0xff;
3005 case TARGET_DIF_TYPE1_PROT:
3007 * For TYPE 1 protection: 16 bit GUARD tag, 32 bit
3008 * REF tag, and 16 bit app tag.
3010 ctx->ref_tag = cpu_to_le32(lba);
3011 if (!qla_tgt_ref_mask_check(se_cmd) ||
3012 !(ha->tgt.tgt_ops->chk_dif_tags(t32))) {
3013 *pfw_prot_opts |= PO_DIS_REF_TAG_VALD;
3016 /* enable ALL bytes of the ref tag */
3017 ctx->ref_tag_mask[0] = 0xff;
3018 ctx->ref_tag_mask[1] = 0xff;
3019 ctx->ref_tag_mask[2] = 0xff;
3020 ctx->ref_tag_mask[3] = 0xff;
3022 case TARGET_DIF_TYPE2_PROT:
3024 * For TYPE 2 protection: 16 bit GUARD + 32 bit REF
3025 * tag has to match LBA in CDB + N
3027 ctx->ref_tag = cpu_to_le32(lba);
3028 if (!qla_tgt_ref_mask_check(se_cmd) ||
3029 !(ha->tgt.tgt_ops->chk_dif_tags(t32))) {
3030 *pfw_prot_opts |= PO_DIS_REF_TAG_VALD;
3033 /* enable ALL bytes of the ref tag */
3034 ctx->ref_tag_mask[0] = 0xff;
3035 ctx->ref_tag_mask[1] = 0xff;
3036 ctx->ref_tag_mask[2] = 0xff;
3037 ctx->ref_tag_mask[3] = 0xff;
3039 case TARGET_DIF_TYPE3_PROT:
3040 /* For TYPE 3 protection: 16 bit GUARD only */
3041 *pfw_prot_opts |= PO_DIS_REF_TAG_VALD;
3042 ctx->ref_tag_mask[0] = ctx->ref_tag_mask[1] =
3043 ctx->ref_tag_mask[2] = ctx->ref_tag_mask[3] = 0x00;
3049 qlt_build_ctio_crc2_pkt(struct qla_qpair *qpair, struct qla_tgt_prm *prm)
3051 struct dsd64 *cur_dsd;
3052 uint32_t transfer_length = 0;
3053 uint32_t data_bytes;
3055 uint8_t bundling = 1;
3056 struct crc_context *crc_ctx_pkt = NULL;
3057 struct qla_hw_data *ha;
3058 struct ctio_crc2_to_fw *pkt;
3059 dma_addr_t crc_ctx_dma;
3060 uint16_t fw_prot_opts = 0;
3061 struct qla_tgt_cmd *cmd = prm->cmd;
3062 struct se_cmd *se_cmd = &cmd->se_cmd;
3064 struct atio_from_isp *atio = &prm->cmd->atio;
3065 struct qla_tc_param tc;
3067 scsi_qla_host_t *vha = cmd->vha;
3071 pkt = (struct ctio_crc2_to_fw *)qpair->req->ring_ptr;
3073 memset(pkt, 0, sizeof(*pkt));
3075 ql_dbg_qp(ql_dbg_tgt, cmd->qpair, 0xe071,
3076 "qla_target(%d):%s: se_cmd[%p] CRC2 prot_op[0x%x] cmd prot sg:cnt[%p:%x] lba[%llu]\n",
3077 cmd->vp_idx, __func__, se_cmd, se_cmd->prot_op,
3078 prm->prot_sg, prm->prot_seg_cnt, se_cmd->t_task_lba);
3080 if ((se_cmd->prot_op == TARGET_PROT_DIN_INSERT) ||
3081 (se_cmd->prot_op == TARGET_PROT_DOUT_STRIP))
3084 /* Compute dif len and adjust data len to incude protection */
3085 data_bytes = cmd->bufflen;
3086 dif_bytes = (data_bytes / cmd->blk_sz) * 8;
3088 switch (se_cmd->prot_op) {
3089 case TARGET_PROT_DIN_INSERT:
3090 case TARGET_PROT_DOUT_STRIP:
3091 transfer_length = data_bytes;
3092 if (cmd->prot_sg_cnt)
3093 data_bytes += dif_bytes;
3095 case TARGET_PROT_DIN_STRIP:
3096 case TARGET_PROT_DOUT_INSERT:
3097 case TARGET_PROT_DIN_PASS:
3098 case TARGET_PROT_DOUT_PASS:
3099 transfer_length = data_bytes + dif_bytes;
3106 if (!qlt_hba_err_chk_enabled(se_cmd))
3107 fw_prot_opts |= 0x10; /* Disable Guard tag checking */
3108 /* HBA error checking enabled */
3109 else if (IS_PI_UNINIT_CAPABLE(ha)) {
3110 if ((se_cmd->prot_type == TARGET_DIF_TYPE1_PROT) ||
3111 (se_cmd->prot_type == TARGET_DIF_TYPE2_PROT))
3112 fw_prot_opts |= PO_DIS_VALD_APP_ESC;
3113 else if (se_cmd->prot_type == TARGET_DIF_TYPE3_PROT)
3114 fw_prot_opts |= PO_DIS_VALD_APP_REF_ESC;
3117 switch (se_cmd->prot_op) {
3118 case TARGET_PROT_DIN_INSERT:
3119 case TARGET_PROT_DOUT_INSERT:
3120 fw_prot_opts |= PO_MODE_DIF_INSERT;
3122 case TARGET_PROT_DIN_STRIP:
3123 case TARGET_PROT_DOUT_STRIP:
3124 fw_prot_opts |= PO_MODE_DIF_REMOVE;
3126 case TARGET_PROT_DIN_PASS:
3127 case TARGET_PROT_DOUT_PASS:
3128 fw_prot_opts |= PO_MODE_DIF_PASS;
3129 /* FUTURE: does tcm require T10CRC<->IPCKSUM conversion? */
3131 default:/* Normal Request */
3132 fw_prot_opts |= PO_MODE_DIF_PASS;
3137 /* Update entry type to indicate Command Type CRC_2 IOCB */
3138 pkt->entry_type = CTIO_CRC2;
3139 pkt->entry_count = 1;
3140 pkt->vp_index = cmd->vp_idx;
3142 h = qlt_make_handle(qpair);
3143 if (unlikely(h == QLA_TGT_NULL_HANDLE)) {
3145 * CTIO type 7 from the firmware doesn't provide a way to
3146 * know the initiator's LOOP ID, hence we can't find
3147 * the session and, so, the command.
3151 qpair->req->outstanding_cmds[h] = (srb_t *)prm->cmd;
3153 pkt->handle = make_handle(qpair->req->id, h);
3154 pkt->handle |= CTIO_COMPLETION_HANDLE_MARK;
3155 pkt->nport_handle = cpu_to_le16(prm->cmd->loop_id);
3156 pkt->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
3157 pkt->initiator_id = be_id_to_le(atio->u.isp24.fcp_hdr.s_id);
3158 pkt->exchange_addr = atio->u.isp24.exchange_addr;
3160 /* silence compile warning */
3161 t16 = be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id);
3162 pkt->ox_id = cpu_to_le16(t16);
3164 t16 = (atio->u.isp24.attr << 9);
3165 pkt->flags |= cpu_to_le16(t16);
3166 pkt->relative_offset = cpu_to_le32(prm->cmd->offset);
3168 /* Set transfer direction */
3169 if (cmd->dma_data_direction == DMA_TO_DEVICE)
3170 pkt->flags = cpu_to_le16(CTIO7_FLAGS_DATA_IN);
3171 else if (cmd->dma_data_direction == DMA_FROM_DEVICE)
3172 pkt->flags = cpu_to_le16(CTIO7_FLAGS_DATA_OUT);
3174 pkt->dseg_count = cpu_to_le16(prm->tot_dsds);
3175 /* Fibre channel byte count */
3176 pkt->transfer_length = cpu_to_le32(transfer_length);
3178 /* ----- CRC context -------- */
3180 /* Allocate CRC context from global pool */
3181 crc_ctx_pkt = cmd->ctx =
3182 dma_pool_zalloc(ha->dl_dma_pool, GFP_ATOMIC, &crc_ctx_dma);
3185 goto crc_queuing_error;
3187 crc_ctx_pkt->crc_ctx_dma = crc_ctx_dma;
3188 INIT_LIST_HEAD(&crc_ctx_pkt->dsd_list);
3191 crc_ctx_pkt->handle = pkt->handle;
3193 qla_tgt_set_dif_tags(cmd, crc_ctx_pkt, &fw_prot_opts);
3195 put_unaligned_le64(crc_ctx_dma, &pkt->crc_context_address);
3196 pkt->crc_context_len = cpu_to_le16(CRC_CONTEXT_LEN_FW);
3199 cur_dsd = &crc_ctx_pkt->u.nobundling.data_dsd[0];
3202 * Configure Bundling if we need to fetch interlaving
3203 * protection PCI accesses
3205 fw_prot_opts |= PO_ENABLE_DIF_BUNDLING;
3206 crc_ctx_pkt->u.bundling.dif_byte_count = cpu_to_le32(dif_bytes);
3207 crc_ctx_pkt->u.bundling.dseg_count =
3208 cpu_to_le16(prm->tot_dsds - prm->prot_seg_cnt);
3209 cur_dsd = &crc_ctx_pkt->u.bundling.data_dsd[0];
3212 /* Finish the common fields of CRC pkt */
3213 crc_ctx_pkt->blk_size = cpu_to_le16(cmd->blk_sz);
3214 crc_ctx_pkt->prot_opts = cpu_to_le16(fw_prot_opts);
3215 crc_ctx_pkt->byte_count = cpu_to_le32(data_bytes);
3216 crc_ctx_pkt->guard_seed = cpu_to_le16(0);
3218 memset((uint8_t *)&tc, 0 , sizeof(tc));
3220 tc.blk_sz = cmd->blk_sz;
3221 tc.bufflen = cmd->bufflen;
3223 tc.prot_sg = cmd->prot_sg;
3224 tc.ctx = crc_ctx_pkt;
3225 tc.ctx_dsd_alloced = &cmd->ctx_dsd_alloced;
3227 /* Walks data segments */
3228 pkt->flags |= cpu_to_le16(CTIO7_FLAGS_DSD_PTR);
3230 if (!bundling && prm->prot_seg_cnt) {
3231 if (qla24xx_walk_and_build_sglist_no_difb(ha, NULL, cur_dsd,
3232 prm->tot_dsds, &tc))
3233 goto crc_queuing_error;
3234 } else if (qla24xx_walk_and_build_sglist(ha, NULL, cur_dsd,
3235 (prm->tot_dsds - prm->prot_seg_cnt), &tc))
3236 goto crc_queuing_error;
3238 if (bundling && prm->prot_seg_cnt) {
3239 /* Walks dif segments */
3240 pkt->add_flags |= CTIO_CRC2_AF_DIF_DSD_ENA;
3242 cur_dsd = &crc_ctx_pkt->u.bundling.dif_dsd;
3243 if (qla24xx_walk_and_build_prot_sglist(ha, NULL, cur_dsd,
3244 prm->prot_seg_cnt, cmd))
3245 goto crc_queuing_error;
3250 /* Cleanup will be performed by the caller */
3251 qpair->req->outstanding_cmds[h] = NULL;
3253 return QLA_FUNCTION_FAILED;
3257 * Callback to setup response of xmit_type of QLA_TGT_XMIT_DATA and *
3258 * QLA_TGT_XMIT_STATUS for >= 24xx silicon
3260 int qlt_xmit_response(struct qla_tgt_cmd *cmd, int xmit_type,
3261 uint8_t scsi_status)
3263 struct scsi_qla_host *vha = cmd->vha;
3264 struct qla_qpair *qpair = cmd->qpair;
3265 struct ctio7_to_24xx *pkt;
3266 struct qla_tgt_prm prm;
3267 uint32_t full_req_cnt = 0;
3268 unsigned long flags = 0;
3271 if (!qpair->fw_started || (cmd->reset_count != qpair->chip_reset) ||
3272 (cmd->sess && cmd->sess->deleted)) {
3273 cmd->state = QLA_TGT_STATE_PROCESSED;
3277 ql_dbg_qp(ql_dbg_tgt, qpair, 0xe018,
3278 "is_send_status=%d, cmd->bufflen=%d, cmd->sg_cnt=%d, cmd->dma_data_direction=%d se_cmd[%p] qp %d\n",
3279 (xmit_type & QLA_TGT_XMIT_STATUS) ?
3280 1 : 0, cmd->bufflen, cmd->sg_cnt, cmd->dma_data_direction,
3281 &cmd->se_cmd, qpair->id);
3283 res = qlt_pre_xmit_response(cmd, &prm, xmit_type, scsi_status,
3285 if (unlikely(res != 0)) {
3289 spin_lock_irqsave(qpair->qp_lock_ptr, flags);
3291 if (xmit_type == QLA_TGT_XMIT_STATUS)
3292 qpair->tgt_counters.core_qla_snd_status++;
3294 qpair->tgt_counters.core_qla_que_buf++;
3296 if (!qpair->fw_started || cmd->reset_count != qpair->chip_reset) {
3298 * Either the port is not online or this request was from
3299 * previous life, just abort the processing.
3301 cmd->state = QLA_TGT_STATE_PROCESSED;
3302 ql_dbg_qp(ql_dbg_async, qpair, 0xe101,
3303 "RESET-RSP online/active/old-count/new-count = %d/%d/%d/%d.\n",
3304 vha->flags.online, qla2x00_reset_active(vha),
3305 cmd->reset_count, qpair->chip_reset);
3307 goto out_unmap_unlock;
3310 /* Does F/W have an IOCBs for this request */
3311 res = qlt_check_reserve_free_req(qpair, full_req_cnt);
3313 goto out_unmap_unlock;
3315 if (cmd->se_cmd.prot_op && (xmit_type & QLA_TGT_XMIT_DATA))
3316 res = qlt_build_ctio_crc2_pkt(qpair, &prm);
3318 res = qlt_24xx_build_ctio_pkt(qpair, &prm);
3319 if (unlikely(res != 0)) {
3320 qpair->req->cnt += full_req_cnt;
3321 goto out_unmap_unlock;
3324 pkt = (struct ctio7_to_24xx *)prm.pkt;
3326 if (qlt_has_data(cmd) && (xmit_type & QLA_TGT_XMIT_DATA)) {
3327 pkt->u.status0.flags |=
3328 cpu_to_le16(CTIO7_FLAGS_DATA_IN |
3329 CTIO7_FLAGS_STATUS_MODE_0);
3331 if (cmd->se_cmd.prot_op == TARGET_PROT_NORMAL)
3332 qlt_load_data_segments(&prm);
3334 if (prm.add_status_pkt == 0) {
3335 if (xmit_type & QLA_TGT_XMIT_STATUS) {
3336 pkt->u.status0.scsi_status =
3337 cpu_to_le16(prm.rq_result);
3339 pkt->u.status0.residual =
3340 cpu_to_le32(prm.residual);
3342 pkt->u.status0.flags |= cpu_to_le16(
3343 CTIO7_FLAGS_SEND_STATUS);
3344 if (qlt_need_explicit_conf(cmd, 0)) {
3345 pkt->u.status0.flags |=
3347 CTIO7_FLAGS_EXPLICIT_CONFORM |
3348 CTIO7_FLAGS_CONFORM_REQ);
3354 * We have already made sure that there is sufficient
3355 * amount of request entries to not drop HW lock in
3358 struct ctio7_to_24xx *ctio =
3359 (struct ctio7_to_24xx *)qlt_get_req_pkt(
3362 ql_dbg_qp(ql_dbg_tgt, qpair, 0x305e,
3363 "Building additional status packet 0x%p.\n",
3367 * T10Dif: ctio_crc2_to_fw overlay ontop of
3370 memcpy(ctio, pkt, sizeof(*ctio));
3371 /* reset back to CTIO7 */
3372 ctio->entry_count = 1;
3373 ctio->entry_type = CTIO_TYPE7;
3374 ctio->dseg_count = 0;
3375 ctio->u.status1.flags &= ~cpu_to_le16(
3376 CTIO7_FLAGS_DATA_IN);
3378 /* Real finish is ctio_m1's finish */
3379 pkt->handle |= CTIO_INTERMEDIATE_HANDLE_MARK;
3380 pkt->u.status0.flags |= cpu_to_le16(
3381 CTIO7_FLAGS_DONT_RET_CTIO);
3383 /* qlt_24xx_init_ctio_to_isp will correct
3384 * all neccessary fields that's part of CTIO7.
3385 * There should be no residual of CTIO-CRC2 data.
3387 qlt_24xx_init_ctio_to_isp((struct ctio7_to_24xx *)ctio,
3391 qlt_24xx_init_ctio_to_isp(pkt, &prm);
3394 cmd->state = QLA_TGT_STATE_PROCESSED; /* Mid-level is done processing */
3395 cmd->cmd_sent_to_fw = 1;
3396 cmd->ctio_flags = le16_to_cpu(pkt->u.status0.flags);
3398 /* Memory Barrier */
3400 if (qpair->reqq_start_iocbs)
3401 qpair->reqq_start_iocbs(qpair);
3403 qla2x00_start_iocbs(vha, qpair->req);
3404 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
3409 qlt_unmap_sg(vha, cmd);
3410 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
3414 EXPORT_SYMBOL(qlt_xmit_response);
3416 int qlt_rdy_to_xfer(struct qla_tgt_cmd *cmd)
3418 struct ctio7_to_24xx *pkt;
3419 struct scsi_qla_host *vha = cmd->vha;
3420 struct qla_tgt *tgt = cmd->tgt;
3421 struct qla_tgt_prm prm;
3422 unsigned long flags = 0;
3424 struct qla_qpair *qpair = cmd->qpair;
3426 memset(&prm, 0, sizeof(prm));
3432 if (!qpair->fw_started || (cmd->reset_count != qpair->chip_reset) ||
3433 (cmd->sess && cmd->sess->deleted)) {
3435 * Either the port is not online or this request was from
3436 * previous life, just abort the processing.
3439 cmd->write_data_transferred = 0;
3440 cmd->state = QLA_TGT_STATE_DATA_IN;
3441 vha->hw->tgt.tgt_ops->handle_data(cmd);
3442 ql_dbg_qp(ql_dbg_async, qpair, 0xe102,
3443 "RESET-XFR online/active/old-count/new-count = %d/%d/%d/%d.\n",
3444 vha->flags.online, qla2x00_reset_active(vha),
3445 cmd->reset_count, qpair->chip_reset);
3449 /* Calculate number of entries and segments required */
3450 if (qlt_pci_map_calc_cnt(&prm) != 0)
3453 spin_lock_irqsave(qpair->qp_lock_ptr, flags);
3454 /* Does F/W have an IOCBs for this request */
3455 res = qlt_check_reserve_free_req(qpair, prm.req_cnt);
3457 goto out_unlock_free_unmap;
3458 if (cmd->se_cmd.prot_op)
3459 res = qlt_build_ctio_crc2_pkt(qpair, &prm);
3461 res = qlt_24xx_build_ctio_pkt(qpair, &prm);
3463 if (unlikely(res != 0)) {
3464 qpair->req->cnt += prm.req_cnt;
3465 goto out_unlock_free_unmap;
3468 pkt = (struct ctio7_to_24xx *)prm.pkt;
3469 pkt->u.status0.flags |= cpu_to_le16(CTIO7_FLAGS_DATA_OUT |
3470 CTIO7_FLAGS_STATUS_MODE_0);
3472 if (cmd->se_cmd.prot_op == TARGET_PROT_NORMAL)
3473 qlt_load_data_segments(&prm);
3475 cmd->state = QLA_TGT_STATE_NEED_DATA;
3476 cmd->cmd_sent_to_fw = 1;
3477 cmd->ctio_flags = le16_to_cpu(pkt->u.status0.flags);
3479 /* Memory Barrier */
3481 if (qpair->reqq_start_iocbs)
3482 qpair->reqq_start_iocbs(qpair);
3484 qla2x00_start_iocbs(vha, qpair->req);
3485 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
3489 out_unlock_free_unmap:
3490 qlt_unmap_sg(vha, cmd);
3491 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
3495 EXPORT_SYMBOL(qlt_rdy_to_xfer);
3499 * it is assumed either hardware_lock or qpair lock is held.
3502 qlt_handle_dif_error(struct qla_qpair *qpair, struct qla_tgt_cmd *cmd,
3503 struct ctio_crc_from_fw *sts)
3505 uint8_t *ap = &sts->actual_dif[0];
3506 uint8_t *ep = &sts->expected_dif[0];
3507 uint64_t lba = cmd->se_cmd.t_task_lba;
3508 uint8_t scsi_status, sense_key, asc, ascq;
3509 unsigned long flags;
3510 struct scsi_qla_host *vha = cmd->vha;
3512 cmd->trc_flags |= TRC_DIF_ERR;
3514 cmd->a_guard = get_unaligned_be16(ap + 0);
3515 cmd->a_app_tag = get_unaligned_be16(ap + 2);
3516 cmd->a_ref_tag = get_unaligned_be32(ap + 4);
3518 cmd->e_guard = get_unaligned_be16(ep + 0);
3519 cmd->e_app_tag = get_unaligned_be16(ep + 2);
3520 cmd->e_ref_tag = get_unaligned_be32(ep + 4);
3522 ql_dbg(ql_dbg_tgt_dif, vha, 0xf075,
3523 "%s: aborted %d state %d\n", __func__, cmd->aborted, cmd->state);
3525 scsi_status = sense_key = asc = ascq = 0;
3527 /* check appl tag */
3528 if (cmd->e_app_tag != cmd->a_app_tag) {
3529 ql_dbg(ql_dbg_tgt_dif, vha, 0xe00d,
3530 "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]",
3531 cmd->cdb[0], lba, (lba+cmd->num_blks), cmd->num_blks,
3532 cmd->a_ref_tag, cmd->e_ref_tag, cmd->a_app_tag,
3533 cmd->e_app_tag, cmd->a_guard, cmd->e_guard, cmd,
3534 cmd->atio.u.isp24.fcp_hdr.ox_id);
3536 cmd->dif_err_code = DIF_ERR_APP;
3537 scsi_status = SAM_STAT_CHECK_CONDITION;
3538 sense_key = ABORTED_COMMAND;
3544 if (cmd->e_ref_tag != cmd->a_ref_tag) {
3545 ql_dbg(ql_dbg_tgt_dif, vha, 0xe00e,
3546 "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] ",
3547 cmd->cdb[0], lba, (lba+cmd->num_blks), cmd->num_blks,
3548 cmd->a_ref_tag, cmd->e_ref_tag, cmd->a_app_tag,
3549 cmd->e_app_tag, cmd->a_guard, cmd->e_guard, cmd,
3550 cmd->atio.u.isp24.fcp_hdr.ox_id);
3552 cmd->dif_err_code = DIF_ERR_REF;
3553 scsi_status = SAM_STAT_CHECK_CONDITION;
3554 sense_key = ABORTED_COMMAND;
3561 if (cmd->e_guard != cmd->a_guard) {
3562 ql_dbg(ql_dbg_tgt_dif, vha, 0xe012,
3563 "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]",
3564 cmd->cdb[0], lba, (lba+cmd->num_blks), cmd->num_blks,
3565 cmd->a_ref_tag, cmd->e_ref_tag, cmd->a_app_tag,
3566 cmd->e_app_tag, cmd->a_guard, cmd->e_guard, cmd,
3567 cmd->atio.u.isp24.fcp_hdr.ox_id);
3569 cmd->dif_err_code = DIF_ERR_GRD;
3570 scsi_status = SAM_STAT_CHECK_CONDITION;
3571 sense_key = ABORTED_COMMAND;
3576 switch (cmd->state) {
3577 case QLA_TGT_STATE_NEED_DATA:
3578 /* handle_data will load DIF error code */
3579 cmd->state = QLA_TGT_STATE_DATA_IN;
3580 vha->hw->tgt.tgt_ops->handle_data(cmd);
3583 spin_lock_irqsave(&cmd->cmd_lock, flags);
3585 spin_unlock_irqrestore(&cmd->cmd_lock, flags);
3586 vha->hw->tgt.tgt_ops->free_cmd(cmd);
3589 spin_unlock_irqrestore(&cmd->cmd_lock, flags);
3591 qlt_send_resp_ctio(qpair, cmd, scsi_status, sense_key, asc,
3593 /* assume scsi status gets out on the wire.
3594 * Will not wait for completion.
3596 vha->hw->tgt.tgt_ops->free_cmd(cmd);
3601 /* If hardware_lock held on entry, might drop it, then reaquire */
3602 /* This function sends the appropriate CTIO to ISP 2xxx or 24xx */
3603 static int __qlt_send_term_imm_notif(struct scsi_qla_host *vha,
3604 struct imm_ntfy_from_isp *ntfy)
3606 struct nack_to_isp *nack;
3607 struct qla_hw_data *ha = vha->hw;
3611 ql_dbg(ql_dbg_tgt_tmr, vha, 0xe01c,
3612 "Sending TERM ELS CTIO (ha=%p)\n", ha);
3614 pkt = (request_t *)qla2x00_alloc_iocbs(vha, NULL);
3616 ql_dbg(ql_dbg_tgt, vha, 0xe080,
3617 "qla_target(%d): %s failed: unable to allocate "
3618 "request packet\n", vha->vp_idx, __func__);
3622 pkt->entry_type = NOTIFY_ACK_TYPE;
3623 pkt->entry_count = 1;
3624 pkt->handle = QLA_TGT_SKIP_HANDLE;
3626 nack = (struct nack_to_isp *)pkt;
3627 nack->ox_id = ntfy->ox_id;
3629 nack->u.isp24.nport_handle = ntfy->u.isp24.nport_handle;
3630 if (le16_to_cpu(ntfy->u.isp24.status) == IMM_NTFY_ELS) {
3631 nack->u.isp24.flags = ntfy->u.isp24.flags &
3632 cpu_to_le16(NOTIFY24XX_FLAGS_PUREX_IOCB);
3636 nack->u.isp24.flags |=
3637 __constant_cpu_to_le16(NOTIFY_ACK_FLAGS_TERMINATE);
3639 nack->u.isp24.srr_rx_id = ntfy->u.isp24.srr_rx_id;
3640 nack->u.isp24.status = ntfy->u.isp24.status;
3641 nack->u.isp24.status_subcode = ntfy->u.isp24.status_subcode;
3642 nack->u.isp24.fw_handle = ntfy->u.isp24.fw_handle;
3643 nack->u.isp24.exchange_address = ntfy->u.isp24.exchange_address;
3644 nack->u.isp24.srr_rel_offs = ntfy->u.isp24.srr_rel_offs;
3645 nack->u.isp24.srr_ui = ntfy->u.isp24.srr_ui;
3646 nack->u.isp24.vp_index = ntfy->u.isp24.vp_index;
3648 qla2x00_start_iocbs(vha, vha->req);
3652 static void qlt_send_term_imm_notif(struct scsi_qla_host *vha,
3653 struct imm_ntfy_from_isp *imm, int ha_locked)
3657 WARN_ON_ONCE(!ha_locked);
3658 rc = __qlt_send_term_imm_notif(vha, imm);
3659 pr_debug("rc = %d\n", rc);
3663 * If hardware_lock held on entry, might drop it, then reaquire
3664 * This function sends the appropriate CTIO to ISP 2xxx or 24xx
3666 static int __qlt_send_term_exchange(struct qla_qpair *qpair,
3667 struct qla_tgt_cmd *cmd,
3668 struct atio_from_isp *atio)
3670 struct scsi_qla_host *vha = qpair->vha;
3671 struct ctio7_to_24xx *ctio24;
3672 struct qla_hw_data *ha = vha->hw;
3677 ql_dbg(ql_dbg_tgt, vha, 0xe009, "Sending TERM EXCH CTIO (ha=%p)\n", ha);
3682 pkt = (request_t *)qla2x00_alloc_iocbs_ready(qpair, NULL);
3684 ql_dbg(ql_dbg_tgt, vha, 0xe050,
3685 "qla_target(%d): %s failed: unable to allocate "
3686 "request packet\n", vha->vp_idx, __func__);
3691 if (cmd->state < QLA_TGT_STATE_PROCESSED) {
3692 ql_dbg(ql_dbg_tgt, vha, 0xe051,
3693 "qla_target(%d): Terminating cmd %p with "
3694 "incorrect state %d\n", vha->vp_idx, cmd,
3700 qpair->tgt_counters.num_term_xchg_sent++;
3701 pkt->entry_count = 1;
3702 pkt->handle = QLA_TGT_SKIP_HANDLE | CTIO_COMPLETION_HANDLE_MARK;
3704 ctio24 = (struct ctio7_to_24xx *)pkt;
3705 ctio24->entry_type = CTIO_TYPE7;
3706 ctio24->nport_handle = cpu_to_le16(CTIO7_NHANDLE_UNRECOGNIZED);
3707 ctio24->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
3708 ctio24->vp_index = vha->vp_idx;
3709 ctio24->initiator_id = be_id_to_le(atio->u.isp24.fcp_hdr.s_id);
3710 ctio24->exchange_addr = atio->u.isp24.exchange_addr;
3711 temp = (atio->u.isp24.attr << 9) | CTIO7_FLAGS_STATUS_MODE_1 |
3712 CTIO7_FLAGS_TERMINATE;
3713 ctio24->u.status1.flags = cpu_to_le16(temp);
3714 temp = be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id);
3715 ctio24->u.status1.ox_id = cpu_to_le16(temp);
3717 /* Memory Barrier */
3719 if (qpair->reqq_start_iocbs)
3720 qpair->reqq_start_iocbs(qpair);
3722 qla2x00_start_iocbs(vha, qpair->req);
3726 static void qlt_send_term_exchange(struct qla_qpair *qpair,
3727 struct qla_tgt_cmd *cmd, struct atio_from_isp *atio, int ha_locked,
3730 struct scsi_qla_host *vha;
3731 unsigned long flags = 0;
3734 /* why use different vha? NPIV */
3741 rc = __qlt_send_term_exchange(qpair, cmd, atio);
3743 qlt_alloc_qfull_cmd(vha, atio, 0, 0);
3746 spin_lock_irqsave(qpair->qp_lock_ptr, flags);
3747 rc = __qlt_send_term_exchange(qpair, cmd, atio);
3749 qlt_alloc_qfull_cmd(vha, atio, 0, 0);
3752 if (cmd && !ul_abort && !cmd->aborted) {
3754 qlt_unmap_sg(vha, cmd);
3755 vha->hw->tgt.tgt_ops->free_cmd(cmd);
3759 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
3764 static void qlt_init_term_exchange(struct scsi_qla_host *vha)
3766 struct list_head free_list;
3767 struct qla_tgt_cmd *cmd, *tcmd;
3769 vha->hw->tgt.leak_exchg_thresh_hold =
3770 (vha->hw->cur_fw_xcb_count/100) * LEAK_EXCHG_THRESH_HOLD_PERCENT;
3773 if (!list_empty(&vha->hw->tgt.q_full_list)) {
3774 INIT_LIST_HEAD(&free_list);
3775 list_splice_init(&vha->hw->tgt.q_full_list, &free_list);
3777 list_for_each_entry_safe(cmd, tcmd, &free_list, cmd_list) {
3778 list_del(&cmd->cmd_list);
3779 /* This cmd was never sent to TCM. There is no need
3780 * to schedule free or call free_cmd
3783 vha->hw->tgt.num_qfull_cmds_alloc--;
3786 vha->hw->tgt.num_qfull_cmds_dropped = 0;
3789 static void qlt_chk_exch_leak_thresh_hold(struct scsi_qla_host *vha)
3791 uint32_t total_leaked;
3793 total_leaked = vha->hw->tgt.num_qfull_cmds_dropped;
3795 if (vha->hw->tgt.leak_exchg_thresh_hold &&
3796 (total_leaked > vha->hw->tgt.leak_exchg_thresh_hold)) {
3798 ql_dbg(ql_dbg_tgt, vha, 0xe079,
3799 "Chip reset due to exchange starvation: %d/%d.\n",
3800 total_leaked, vha->hw->cur_fw_xcb_count);
3802 if (IS_P3P_TYPE(vha->hw))
3803 set_bit(FCOE_CTX_RESET_NEEDED, &vha->dpc_flags);
3805 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
3806 qla2xxx_wake_dpc(vha);
3811 int qlt_abort_cmd(struct qla_tgt_cmd *cmd)
3813 struct qla_tgt *tgt = cmd->tgt;
3814 struct scsi_qla_host *vha = tgt->vha;
3815 struct se_cmd *se_cmd = &cmd->se_cmd;
3816 unsigned long flags;
3818 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf014,
3819 "qla_target(%d): terminating exchange for aborted cmd=%p "
3820 "(se_cmd=%p, tag=%llu)", vha->vp_idx, cmd, &cmd->se_cmd,
3823 spin_lock_irqsave(&cmd->cmd_lock, flags);
3826 qlt_unmap_sg(vha, cmd);
3828 spin_unlock_irqrestore(&cmd->cmd_lock, flags);
3830 * It's normal to see 2 calls in this path:
3831 * 1) XFER Rdy completion + CMD_T_ABORT
3832 * 2) TCM TMR - drain_state_list
3834 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf016,
3835 "multiple abort. %p transport_state %x, t_state %x, "
3836 "se_cmd_flags %x\n", cmd, cmd->se_cmd.transport_state,
3837 cmd->se_cmd.t_state, cmd->se_cmd.se_cmd_flags);
3841 cmd->trc_flags |= TRC_ABORT;
3842 spin_unlock_irqrestore(&cmd->cmd_lock, flags);
3844 qlt_send_term_exchange(cmd->qpair, cmd, &cmd->atio, 0, 1);
3847 EXPORT_SYMBOL(qlt_abort_cmd);
3849 void qlt_free_cmd(struct qla_tgt_cmd *cmd)
3851 struct fc_port *sess = cmd->sess;
3853 ql_dbg(ql_dbg_tgt, cmd->vha, 0xe074,
3854 "%s: se_cmd[%p] ox_id %04x\n",
3855 __func__, &cmd->se_cmd,
3856 be16_to_cpu(cmd->atio.u.isp24.fcp_hdr.ox_id));
3858 BUG_ON(cmd->cmd_in_wq);
3861 qlt_decr_num_pend_cmds(cmd->vha);
3863 BUG_ON(cmd->sg_mapped);
3864 cmd->jiffies_at_free = get_jiffies_64();
3866 if (!sess || !sess->se_sess) {
3870 cmd->jiffies_at_free = get_jiffies_64();
3871 cmd->vha->hw->tgt.tgt_ops->rel_cmd(cmd);
3873 EXPORT_SYMBOL(qlt_free_cmd);
3876 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
3878 static int qlt_term_ctio_exchange(struct qla_qpair *qpair, void *ctio,
3879 struct qla_tgt_cmd *cmd, uint32_t status)
3882 struct scsi_qla_host *vha = qpair->vha;
3884 if (cmd->se_cmd.prot_op)
3885 ql_dbg(ql_dbg_tgt_dif, vha, 0xe013,
3886 "Term DIF cmd: lba[0x%llx|%lld] len[0x%x] "
3887 "se_cmd=%p tag[%x] op %#x/%s",
3889 cmd->num_blks, &cmd->se_cmd,
3890 cmd->atio.u.isp24.exchange_addr,
3891 cmd->se_cmd.prot_op,
3892 prot_op_str(cmd->se_cmd.prot_op));
3895 struct ctio7_from_24xx *c = (struct ctio7_from_24xx *)ctio;
3898 cpu_to_le16(OF_TERM_EXCH));
3903 qlt_send_term_exchange(qpair, cmd, &cmd->atio, 1, 0);
3909 /* ha->hardware_lock supposed to be held on entry */
3910 static void *qlt_ctio_to_cmd(struct scsi_qla_host *vha,
3911 struct rsp_que *rsp, uint32_t handle, void *ctio)
3914 struct req_que *req;
3915 int qid = GET_QID(handle);
3916 uint32_t h = handle & ~QLA_TGT_HANDLE_MASK;
3918 if (unlikely(h == QLA_TGT_SKIP_HANDLE))
3921 if (qid == rsp->req->id) {
3923 } else if (vha->hw->req_q_map[qid]) {
3924 ql_dbg(ql_dbg_tgt_mgt, vha, 0x1000a,
3925 "qla_target(%d): CTIO completion with different QID %d handle %x\n",
3926 vha->vp_idx, rsp->id, handle);
3927 req = vha->hw->req_q_map[qid];
3932 h &= QLA_CMD_HANDLE_MASK;
3934 if (h != QLA_TGT_NULL_HANDLE) {
3935 if (unlikely(h >= req->num_outstanding_cmds)) {
3936 ql_dbg(ql_dbg_tgt, vha, 0xe052,
3937 "qla_target(%d): Wrong handle %x received\n",
3938 vha->vp_idx, handle);
3942 cmd = req->outstanding_cmds[h];
3943 if (unlikely(cmd == NULL)) {
3944 ql_dbg(ql_dbg_async, vha, 0xe053,
3945 "qla_target(%d): Suspicious: unable to find the command with handle %x req->id %d rsp->id %d\n",
3946 vha->vp_idx, handle, req->id, rsp->id);
3949 req->outstanding_cmds[h] = NULL;
3950 } else if (ctio != NULL) {
3951 /* We can't get loop ID from CTIO7 */
3952 ql_dbg(ql_dbg_tgt, vha, 0xe054,
3953 "qla_target(%d): Wrong CTIO received: QLA24xx doesn't "
3954 "support NULL handles\n", vha->vp_idx);
3962 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
3964 static void qlt_do_ctio_completion(struct scsi_qla_host *vha,
3965 struct rsp_que *rsp, uint32_t handle, uint32_t status, void *ctio)
3967 struct qla_hw_data *ha = vha->hw;
3968 struct se_cmd *se_cmd;
3969 struct qla_tgt_cmd *cmd;
3970 struct qla_qpair *qpair = rsp->qpair;
3972 if (handle & CTIO_INTERMEDIATE_HANDLE_MARK) {
3973 /* That could happen only in case of an error/reset/abort */
3974 if (status != CTIO_SUCCESS) {
3975 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01d,
3976 "Intermediate CTIO received"
3977 " (status %x)\n", status);
3982 cmd = qlt_ctio_to_cmd(vha, rsp, handle, ctio);
3986 if ((le16_to_cpu(((struct ctio7_from_24xx *)ctio)->flags) & CTIO7_FLAGS_DATA_OUT) &&
3988 qlt_chk_edif_rx_sa_delete_pending(vha, cmd->sess,
3989 (struct ctio7_from_24xx *)ctio);
3992 se_cmd = &cmd->se_cmd;
3993 cmd->cmd_sent_to_fw = 0;
3995 qlt_unmap_sg(vha, cmd);
3997 if (unlikely(status != CTIO_SUCCESS)) {
3998 switch (status & 0xFFFF) {
3999 case CTIO_INVALID_RX_ID:
4000 if (printk_ratelimit())
4001 dev_info(&vha->hw->pdev->dev,
4002 "qla_target(%d): CTIO with INVALID_RX_ID ATIO attr %x CTIO Flags %x|%x\n",
4003 vha->vp_idx, cmd->atio.u.isp24.attr,
4004 ((cmd->ctio_flags >> 9) & 0xf),
4008 case CTIO_LIP_RESET:
4009 case CTIO_TARGET_RESET:
4011 /* driver request abort via Terminate exchange */
4014 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf058,
4015 "qla_target(%d): CTIO with "
4016 "status %#x received, state %x, se_cmd %p, "
4017 "(LIP_RESET=e, ABORTED=2, TARGET_RESET=17, "
4018 "TIMEOUT=b, INVALID_RX_ID=8)\n", vha->vp_idx,
4019 status, cmd->state, se_cmd);
4022 case CTIO_PORT_LOGGED_OUT:
4023 case CTIO_PORT_UNAVAILABLE:
4026 (status & 0xFFFF) == CTIO_PORT_LOGGED_OUT;
4028 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf059,
4029 "qla_target(%d): CTIO with %s status %x "
4030 "received (state %x, se_cmd %p)\n", vha->vp_idx,
4031 logged_out ? "PORT LOGGED OUT" : "PORT UNAVAILABLE",
4032 status, cmd->state, se_cmd);
4034 if (logged_out && cmd->sess) {
4036 * Session is already logged out, but we need
4037 * to notify initiator, who's not aware of this
4039 cmd->sess->send_els_logo = 1;
4040 ql_dbg(ql_dbg_disc, vha, 0x20f8,
4041 "%s %d %8phC post del sess\n",
4042 __func__, __LINE__, cmd->sess->port_name);
4044 qlt_schedule_sess_for_deletion(cmd->sess);
4048 case CTIO_DIF_ERROR: {
4049 struct ctio_crc_from_fw *crc =
4050 (struct ctio_crc_from_fw *)ctio;
4051 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf073,
4052 "qla_target(%d): CTIO with DIF_ERROR status %x "
4053 "received (state %x, ulp_cmd %p) actual_dif[0x%llx] "
4054 "expect_dif[0x%llx]\n",
4055 vha->vp_idx, status, cmd->state, se_cmd,
4056 *((u64 *)&crc->actual_dif[0]),
4057 *((u64 *)&crc->expected_dif[0]));
4059 qlt_handle_dif_error(qpair, cmd, ctio);
4063 case CTIO_FAST_AUTH_ERR:
4064 case CTIO_FAST_INCOMP_PAD_LEN:
4065 case CTIO_FAST_INVALID_REQ:
4066 case CTIO_FAST_SPI_ERR:
4067 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05b,
4068 "qla_target(%d): CTIO with EDIF error status 0x%x received (state %x, se_cmd %p\n",
4069 vha->vp_idx, status, cmd->state, se_cmd);
4073 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05b,
4074 "qla_target(%d): CTIO with error status 0x%x received (state %x, se_cmd %p\n",
4075 vha->vp_idx, status, cmd->state, se_cmd);
4080 /* "cmd->aborted" means
4081 * cmd is already aborted/terminated, we don't
4082 * need to terminate again. The exchange is already
4083 * cleaned up/freed at FW level. Just cleanup at driver
4086 if ((cmd->state != QLA_TGT_STATE_NEED_DATA) &&
4088 cmd->trc_flags |= TRC_CTIO_ERR;
4089 if (qlt_term_ctio_exchange(qpair, ctio, cmd, status))
4094 if (cmd->state == QLA_TGT_STATE_PROCESSED) {
4095 cmd->trc_flags |= TRC_CTIO_DONE;
4096 } else if (cmd->state == QLA_TGT_STATE_NEED_DATA) {
4097 cmd->state = QLA_TGT_STATE_DATA_IN;
4099 if (status == CTIO_SUCCESS)
4100 cmd->write_data_transferred = 1;
4102 ha->tgt.tgt_ops->handle_data(cmd);
4104 } else if (cmd->aborted) {
4105 cmd->trc_flags |= TRC_CTIO_ABORTED;
4106 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01e,
4107 "Aborted command %p (tag %lld) finished\n", cmd, se_cmd->tag);
4109 cmd->trc_flags |= TRC_CTIO_STRANGE;
4110 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05c,
4111 "qla_target(%d): A command in state (%d) should "
4112 "not return a CTIO complete\n", vha->vp_idx, cmd->state);
4115 if (unlikely(status != CTIO_SUCCESS) &&
4117 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01f, "Finishing failed CTIO\n");
4121 ha->tgt.tgt_ops->free_cmd(cmd);
4124 static inline int qlt_get_fcp_task_attr(struct scsi_qla_host *vha,
4129 switch (task_codes) {
4130 case ATIO_SIMPLE_QUEUE:
4131 fcp_task_attr = TCM_SIMPLE_TAG;
4133 case ATIO_HEAD_OF_QUEUE:
4134 fcp_task_attr = TCM_HEAD_TAG;
4136 case ATIO_ORDERED_QUEUE:
4137 fcp_task_attr = TCM_ORDERED_TAG;
4139 case ATIO_ACA_QUEUE:
4140 fcp_task_attr = TCM_ACA_TAG;
4143 fcp_task_attr = TCM_SIMPLE_TAG;
4146 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05d,
4147 "qla_target: unknown task code %x, use ORDERED instead\n",
4149 fcp_task_attr = TCM_ORDERED_TAG;
4153 return fcp_task_attr;
4157 * Process context for I/O path into tcm_qla2xxx code
4159 static void __qlt_do_work(struct qla_tgt_cmd *cmd)
4161 scsi_qla_host_t *vha = cmd->vha;
4162 struct qla_hw_data *ha = vha->hw;
4163 struct fc_port *sess = cmd->sess;
4164 struct atio_from_isp *atio = &cmd->atio;
4166 unsigned long flags;
4167 uint32_t data_length;
4168 int ret, fcp_task_attr, data_dir, bidi = 0;
4169 struct qla_qpair *qpair = cmd->qpair;
4172 cmd->trc_flags |= TRC_DO_WORK;
4175 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf082,
4176 "cmd with tag %u is aborted\n",
4177 cmd->atio.u.isp24.exchange_addr);
4181 spin_lock_init(&cmd->cmd_lock);
4182 cdb = &atio->u.isp24.fcp_cmnd.cdb[0];
4183 cmd->se_cmd.tag = le32_to_cpu(atio->u.isp24.exchange_addr);
4185 if (atio->u.isp24.fcp_cmnd.rddata &&
4186 atio->u.isp24.fcp_cmnd.wrdata) {
4188 data_dir = DMA_TO_DEVICE;
4189 } else if (atio->u.isp24.fcp_cmnd.rddata)
4190 data_dir = DMA_FROM_DEVICE;
4191 else if (atio->u.isp24.fcp_cmnd.wrdata)
4192 data_dir = DMA_TO_DEVICE;
4194 data_dir = DMA_NONE;
4196 fcp_task_attr = qlt_get_fcp_task_attr(vha,
4197 atio->u.isp24.fcp_cmnd.task_attr);
4198 data_length = get_datalen_for_atio(atio);
4200 ret = ha->tgt.tgt_ops->handle_cmd(vha, cmd, cdb, data_length,
4201 fcp_task_attr, data_dir, bidi);
4205 * Drop extra session reference from qlt_handle_cmd_for_atio().
4207 ha->tgt.tgt_ops->put_sess(sess);
4211 ql_dbg(ql_dbg_io, vha, 0x3060, "Terminating work cmd %p", cmd);
4213 * cmd has not sent to target yet, so pass NULL as the second
4214 * argument to qlt_send_term_exchange() and free the memory here.
4216 cmd->trc_flags |= TRC_DO_WORK_ERR;
4217 spin_lock_irqsave(qpair->qp_lock_ptr, flags);
4218 qlt_send_term_exchange(qpair, NULL, &cmd->atio, 1, 0);
4220 qlt_decr_num_pend_cmds(vha);
4221 cmd->vha->hw->tgt.tgt_ops->rel_cmd(cmd);
4222 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
4224 ha->tgt.tgt_ops->put_sess(sess);
4227 static void qlt_do_work(struct work_struct *work)
4229 struct qla_tgt_cmd *cmd = container_of(work, struct qla_tgt_cmd, work);
4230 scsi_qla_host_t *vha = cmd->vha;
4231 unsigned long flags;
4233 spin_lock_irqsave(&vha->cmd_list_lock, flags);
4234 list_del(&cmd->cmd_list);
4235 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
4240 void qlt_clr_qp_table(struct scsi_qla_host *vha)
4242 unsigned long flags;
4243 struct qla_hw_data *ha = vha->hw;
4244 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
4248 ql_log(ql_log_info, vha, 0x706c,
4249 "User update Number of Active Qpairs %d\n",
4250 ha->tgt.num_act_qpairs);
4252 spin_lock_irqsave(&ha->tgt.atio_lock, flags);
4254 btree_for_each_safe64(&tgt->lun_qpair_map, key, node)
4255 btree_remove64(&tgt->lun_qpair_map, key);
4257 ha->base_qpair->lun_cnt = 0;
4258 for (key = 0; key < ha->max_qpairs; key++)
4259 if (ha->queue_pair_map[key])
4260 ha->queue_pair_map[key]->lun_cnt = 0;
4262 spin_unlock_irqrestore(&ha->tgt.atio_lock, flags);
4265 static void qlt_assign_qpair(struct scsi_qla_host *vha,
4266 struct qla_tgt_cmd *cmd)
4268 struct qla_qpair *qpair, *qp;
4269 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
4270 struct qla_qpair_hint *h;
4272 if (vha->flags.qpairs_available) {
4273 h = btree_lookup64(&tgt->lun_qpair_map, cmd->unpacked_lun);
4275 /* spread lun to qpair ratio evently */
4277 struct scsi_qla_host *base_vha =
4278 pci_get_drvdata(vha->hw->pdev);
4280 qpair = vha->hw->base_qpair;
4281 if (qpair->lun_cnt == 0) {
4283 h = qla_qpair_to_hint(tgt, qpair);
4285 rc = btree_insert64(&tgt->lun_qpair_map,
4286 cmd->unpacked_lun, h, GFP_ATOMIC);
4289 ql_log(ql_log_info, vha, 0xd037,
4290 "Unable to insert lun %llx into lun_qpair_map\n",
4295 lcnt = qpair->lun_cnt;
4299 list_for_each_entry(qp, &base_vha->qp_list,
4301 if (qp->lun_cnt == 0) {
4303 h = qla_qpair_to_hint(tgt, qp);
4305 rc = btree_insert64(&tgt->lun_qpair_map,
4306 cmd->unpacked_lun, h, GFP_ATOMIC);
4309 ql_log(ql_log_info, vha, 0xd038,
4310 "Unable to insert lun %llx into lun_qpair_map\n",
4316 if (qp->lun_cnt < lcnt) {
4325 h = qla_qpair_to_hint(tgt, qpair);
4327 rc = btree_insert64(&tgt->lun_qpair_map,
4328 cmd->unpacked_lun, h, GFP_ATOMIC);
4331 ql_log(ql_log_info, vha, 0xd039,
4332 "Unable to insert lun %llx into lun_qpair_map\n",
4337 h = &tgt->qphints[0];
4340 cmd->qpair = h->qpair;
4341 cmd->se_cmd.cpuid = h->cpuid;
4344 static struct qla_tgt_cmd *qlt_get_tag(scsi_qla_host_t *vha,
4345 struct fc_port *sess,
4346 struct atio_from_isp *atio)
4348 struct qla_tgt_cmd *cmd;
4350 cmd = vha->hw->tgt.tgt_ops->get_cmd(sess);
4354 cmd->cmd_type = TYPE_TGT_CMD;
4355 memcpy(&cmd->atio, atio, sizeof(*atio));
4356 INIT_LIST_HEAD(&cmd->sess_cmd_list);
4357 cmd->state = QLA_TGT_STATE_NEW;
4358 cmd->tgt = vha->vha_tgt.qla_tgt;
4359 qlt_incr_num_pend_cmds(vha);
4362 cmd->loop_id = sess->loop_id;
4363 cmd->conf_compl_supported = sess->conf_compl_supported;
4366 cmd->jiffies_at_alloc = get_jiffies_64();
4368 cmd->unpacked_lun = scsilun_to_int(
4369 (struct scsi_lun *)&atio->u.isp24.fcp_cmnd.lun);
4370 qlt_assign_qpair(vha, cmd);
4371 cmd->reset_count = vha->hw->base_qpair->chip_reset;
4372 cmd->vp_idx = vha->vp_idx;
4373 cmd->edif = sess->edif.enable;
4378 /* ha->hardware_lock supposed to be held on entry */
4379 static int qlt_handle_cmd_for_atio(struct scsi_qla_host *vha,
4380 struct atio_from_isp *atio)
4382 struct qla_hw_data *ha = vha->hw;
4383 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
4384 struct fc_port *sess;
4385 struct qla_tgt_cmd *cmd;
4386 unsigned long flags;
4389 if (unlikely(tgt->tgt_stop)) {
4390 ql_dbg(ql_dbg_io, vha, 0x3061,
4391 "New command while device %p is shutting down\n", tgt);
4395 id = be_to_port_id(atio->u.isp24.fcp_hdr.s_id);
4396 if (IS_SW_RESV_ADDR(id))
4399 sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha, atio->u.isp24.fcp_hdr.s_id);
4400 if (unlikely(!sess))
4403 /* Another WWN used to have our s_id. Our PLOGI scheduled its
4404 * session deletion, but it's still in sess_del_work wq */
4405 if (sess->deleted) {
4406 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf002,
4407 "New command while old session %p is being deleted\n",
4413 * Do kref_get() before returning + dropping qla_hw_data->hardware_lock.
4415 if (!kref_get_unless_zero(&sess->sess_kref)) {
4416 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf004,
4417 "%s: kref_get fail, %8phC oxid %x \n",
4418 __func__, sess->port_name,
4419 be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id));
4423 cmd = qlt_get_tag(vha, sess, atio);
4425 ql_dbg(ql_dbg_io, vha, 0x3062,
4426 "qla_target(%d): Allocation of cmd failed\n", vha->vp_idx);
4427 ha->tgt.tgt_ops->put_sess(sess);
4432 cmd->trc_flags |= TRC_NEW_CMD;
4434 spin_lock_irqsave(&vha->cmd_list_lock, flags);
4435 list_add_tail(&cmd->cmd_list, &vha->qla_cmd_list);
4436 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
4438 INIT_WORK(&cmd->work, qlt_do_work);
4439 if (vha->flags.qpairs_available) {
4440 queue_work_on(cmd->se_cmd.cpuid, qla_tgt_wq, &cmd->work);
4441 } else if (ha->msix_count) {
4442 if (cmd->atio.u.isp24.fcp_cmnd.rddata)
4443 queue_work_on(smp_processor_id(), qla_tgt_wq,
4446 queue_work_on(cmd->se_cmd.cpuid, qla_tgt_wq,
4449 queue_work(qla_tgt_wq, &cmd->work);
4455 /* ha->hardware_lock supposed to be held on entry */
4456 static int qlt_issue_task_mgmt(struct fc_port *sess, u64 lun,
4457 int fn, void *iocb, int flags)
4459 struct scsi_qla_host *vha = sess->vha;
4460 struct qla_hw_data *ha = vha->hw;
4461 struct qla_tgt_mgmt_cmd *mcmd;
4462 struct atio_from_isp *a = (struct atio_from_isp *)iocb;
4463 struct qla_qpair_hint *h = &vha->vha_tgt.qla_tgt->qphints[0];
4465 mcmd = mempool_alloc(qla_tgt_mgmt_cmd_mempool, GFP_ATOMIC);
4467 ql_dbg(ql_dbg_tgt_tmr, vha, 0x10009,
4468 "qla_target(%d): Allocation of management "
4469 "command failed, some commands and their data could "
4470 "leak\n", vha->vp_idx);
4473 memset(mcmd, 0, sizeof(*mcmd));
4477 memcpy(&mcmd->orig_iocb.imm_ntfy, iocb,
4478 sizeof(mcmd->orig_iocb.imm_ntfy));
4480 mcmd->tmr_func = fn;
4481 mcmd->flags = flags;
4482 mcmd->reset_count = ha->base_qpair->chip_reset;
4483 mcmd->qpair = h->qpair;
4485 mcmd->se_cmd.cpuid = h->cpuid;
4486 mcmd->unpacked_lun = lun;
4489 case QLA_TGT_LUN_RESET:
4490 case QLA_TGT_CLEAR_TS:
4491 case QLA_TGT_ABORT_TS:
4492 abort_cmds_for_lun(vha, lun, a->u.isp24.fcp_hdr.s_id);
4494 case QLA_TGT_CLEAR_ACA:
4495 h = qlt_find_qphint(vha, mcmd->unpacked_lun);
4496 mcmd->qpair = h->qpair;
4497 mcmd->se_cmd.cpuid = h->cpuid;
4500 case QLA_TGT_TARGET_RESET:
4501 case QLA_TGT_NEXUS_LOSS_SESS:
4502 case QLA_TGT_NEXUS_LOSS:
4503 case QLA_TGT_ABORT_ALL:
4509 INIT_WORK(&mcmd->work, qlt_do_tmr_work);
4510 queue_work_on(mcmd->se_cmd.cpuid, qla_tgt_wq,
4516 /* ha->hardware_lock supposed to be held on entry */
4517 static int qlt_handle_task_mgmt(struct scsi_qla_host *vha, void *iocb)
4519 struct atio_from_isp *a = (struct atio_from_isp *)iocb;
4520 struct qla_hw_data *ha = vha->hw;
4521 struct fc_port *sess;
4524 unsigned long flags;
4526 fn = a->u.isp24.fcp_cmnd.task_mgmt_flags;
4528 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
4529 sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha,
4530 a->u.isp24.fcp_hdr.s_id);
4531 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
4534 scsilun_to_int((struct scsi_lun *)&a->u.isp24.fcp_cmnd.lun);
4536 if (sess == NULL || sess->deleted)
4539 return qlt_issue_task_mgmt(sess, unpacked_lun, fn, iocb, 0);
4542 /* ha->hardware_lock supposed to be held on entry */
4543 static int __qlt_abort_task(struct scsi_qla_host *vha,
4544 struct imm_ntfy_from_isp *iocb, struct fc_port *sess)
4546 struct atio_from_isp *a = (struct atio_from_isp *)iocb;
4547 struct qla_hw_data *ha = vha->hw;
4548 struct qla_tgt_mgmt_cmd *mcmd;
4552 mcmd = mempool_alloc(qla_tgt_mgmt_cmd_mempool, GFP_ATOMIC);
4554 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05f,
4555 "qla_target(%d): %s: Allocation of ABORT cmd failed\n",
4556 vha->vp_idx, __func__);
4559 memset(mcmd, 0, sizeof(*mcmd));
4562 memcpy(&mcmd->orig_iocb.imm_ntfy, iocb,
4563 sizeof(mcmd->orig_iocb.imm_ntfy));
4566 scsilun_to_int((struct scsi_lun *)&a->u.isp24.fcp_cmnd.lun);
4567 mcmd->reset_count = ha->base_qpair->chip_reset;
4568 mcmd->tmr_func = QLA_TGT_2G_ABORT_TASK;
4569 mcmd->qpair = ha->base_qpair;
4571 rc = ha->tgt.tgt_ops->handle_tmr(mcmd, unpacked_lun, mcmd->tmr_func,
4572 le16_to_cpu(iocb->u.isp2x.seq_id));
4574 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf060,
4575 "qla_target(%d): tgt_ops->handle_tmr() failed: %d\n",
4577 mempool_free(mcmd, qla_tgt_mgmt_cmd_mempool);
4584 /* ha->hardware_lock supposed to be held on entry */
4585 static int qlt_abort_task(struct scsi_qla_host *vha,
4586 struct imm_ntfy_from_isp *iocb)
4588 struct qla_hw_data *ha = vha->hw;
4589 struct fc_port *sess;
4591 unsigned long flags;
4593 loop_id = GET_TARGET_ID(ha, (struct atio_from_isp *)iocb);
4595 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
4596 sess = ha->tgt.tgt_ops->find_sess_by_loop_id(vha, loop_id);
4597 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
4600 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf025,
4601 "qla_target(%d): task abort for unexisting "
4602 "session\n", vha->vp_idx);
4603 return qlt_sched_sess_work(vha->vha_tgt.qla_tgt,
4604 QLA_TGT_SESS_WORK_ABORT, iocb, sizeof(*iocb));
4607 return __qlt_abort_task(vha, iocb, sess);
4610 void qlt_logo_completion_handler(fc_port_t *fcport, int rc)
4612 if (rc != MBS_COMMAND_COMPLETE) {
4613 ql_dbg(ql_dbg_tgt_mgt, fcport->vha, 0xf093,
4614 "%s: se_sess %p / sess %p from"
4615 " port %8phC loop_id %#04x s_id %02x:%02x:%02x"
4616 " LOGO failed: %#x\n",
4620 fcport->port_name, fcport->loop_id,
4621 fcport->d_id.b.domain, fcport->d_id.b.area,
4622 fcport->d_id.b.al_pa, rc);
4625 fcport->logout_completed = 1;
4629 * ha->hardware_lock supposed to be held on entry (to protect tgt->sess_list)
4631 * Schedules sessions with matching port_id/loop_id but different wwn for
4632 * deletion. Returns existing session with matching wwn if present.
4636 qlt_find_sess_invalidate_other(scsi_qla_host_t *vha, uint64_t wwn,
4637 port_id_t port_id, uint16_t loop_id, struct fc_port **conflict_sess)
4639 struct fc_port *sess = NULL, *other_sess;
4642 *conflict_sess = NULL;
4644 list_for_each_entry(other_sess, &vha->vp_fcports, list) {
4646 other_wwn = wwn_to_u64(other_sess->port_name);
4648 if (wwn == other_wwn) {
4654 /* find other sess with nport_id collision */
4655 if (port_id.b24 == other_sess->d_id.b24) {
4656 if (loop_id != other_sess->loop_id) {
4657 ql_dbg(ql_dbg_disc, vha, 0x1000c,
4658 "Invalidating sess %p loop_id %d wwn %llx.\n",
4659 other_sess, other_sess->loop_id, other_wwn);
4662 * logout_on_delete is set by default, but another
4663 * session that has the same s_id/loop_id combo
4664 * might have cleared it when requested this session
4665 * deletion, so don't touch it
4667 qlt_schedule_sess_for_deletion(other_sess);
4670 * Another wwn used to have our s_id/loop_id
4671 * kill the session, but don't free the loop_id
4673 ql_dbg(ql_dbg_disc, vha, 0xf01b,
4674 "Invalidating sess %p loop_id %d wwn %llx.\n",
4675 other_sess, other_sess->loop_id, other_wwn);
4677 other_sess->keep_nport_handle = 1;
4678 if (other_sess->disc_state != DSC_DELETED)
4679 *conflict_sess = other_sess;
4680 qlt_schedule_sess_for_deletion(other_sess);
4685 /* find other sess with nport handle collision */
4686 if ((loop_id == other_sess->loop_id) &&
4687 (loop_id != FC_NO_LOOP_ID)) {
4688 ql_dbg(ql_dbg_disc, vha, 0x1000d,
4689 "Invalidating sess %p loop_id %d wwn %llx.\n",
4690 other_sess, other_sess->loop_id, other_wwn);
4692 /* Same loop_id but different s_id
4693 * Ok to kill and logout */
4694 qlt_schedule_sess_for_deletion(other_sess);
4701 /* Abort any commands for this s_id waiting on qla_tgt_wq workqueue */
4702 static int abort_cmds_for_s_id(struct scsi_qla_host *vha, port_id_t *s_id)
4704 struct qla_tgt_sess_op *op;
4705 struct qla_tgt_cmd *cmd;
4708 unsigned long flags;
4710 key = (((u32)s_id->b.domain << 16) |
4711 ((u32)s_id->b.area << 8) |
4712 ((u32)s_id->b.al_pa));
4714 spin_lock_irqsave(&vha->cmd_list_lock, flags);
4715 list_for_each_entry(op, &vha->unknown_atio_list, cmd_list) {
4716 uint32_t op_key = sid_to_key(op->atio.u.isp24.fcp_hdr.s_id);
4718 if (op_key == key) {
4724 list_for_each_entry(cmd, &vha->qla_cmd_list, cmd_list) {
4725 uint32_t cmd_key = sid_to_key(cmd->atio.u.isp24.fcp_hdr.s_id);
4727 if (cmd_key == key) {
4732 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
4737 static int qlt_handle_login(struct scsi_qla_host *vha,
4738 struct imm_ntfy_from_isp *iocb)
4740 struct fc_port *sess = NULL, *conflict_sess = NULL;
4743 uint16_t loop_id, wd3_lo;
4745 struct qlt_plogi_ack_t *pla;
4746 unsigned long flags;
4748 lockdep_assert_held(&vha->hw->hardware_lock);
4750 wwn = wwn_to_u64(iocb->u.isp24.port_name);
4752 port_id.b.domain = iocb->u.isp24.port_id[2];
4753 port_id.b.area = iocb->u.isp24.port_id[1];
4754 port_id.b.al_pa = iocb->u.isp24.port_id[0];
4755 port_id.b.rsvd_1 = 0;
4757 loop_id = le16_to_cpu(iocb->u.isp24.nport_handle);
4759 /* Mark all stale commands sitting in qla_tgt_wq for deletion */
4760 abort_cmds_for_s_id(vha, &port_id);
4763 spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
4764 sess = qlt_find_sess_invalidate_other(vha, wwn,
4765 port_id, loop_id, &conflict_sess);
4766 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
4768 ql_dbg(ql_dbg_disc, vha, 0xffff,
4769 "%s %d Term INOT due to WWN=0 lid=%d, NportID %06X ",
4770 __func__, __LINE__, loop_id, port_id.b24);
4771 qlt_send_term_imm_notif(vha, iocb, 1);
4775 if (IS_SW_RESV_ADDR(port_id)) {
4780 if (vha->hw->flags.edif_enabled &&
4781 !(vha->e_dbell.db_flags & EDB_ACTIVE) &&
4782 iocb->u.isp24.status_subcode == ELS_PLOGI &&
4783 !(le16_to_cpu(iocb->u.isp24.flags) & NOTIFY24XX_FLAGS_FCSP)) {
4784 ql_dbg(ql_dbg_disc, vha, 0xffff,
4785 "%s %d Term INOT due to app not available lid=%d, NportID %06X ",
4786 __func__, __LINE__, loop_id, port_id.b24);
4787 qlt_send_term_imm_notif(vha, iocb, 1);
4791 if (vha->hw->flags.edif_enabled) {
4792 if (DBELL_INACTIVE(vha)) {
4793 ql_dbg(ql_dbg_disc, vha, 0xffff,
4794 "%s %d Term INOT due to app not started lid=%d, NportID %06X ",
4795 __func__, __LINE__, loop_id, port_id.b24);
4796 qlt_send_term_imm_notif(vha, iocb, 1);
4798 } else if (iocb->u.isp24.status_subcode == ELS_PLOGI &&
4799 !(le16_to_cpu(iocb->u.isp24.flags) & NOTIFY24XX_FLAGS_FCSP)) {
4800 ql_dbg(ql_dbg_disc, vha, 0xffff,
4801 "%s %d Term INOT due to unsecure lid=%d, NportID %06X ",
4802 __func__, __LINE__, loop_id, port_id.b24);
4803 qlt_send_term_imm_notif(vha, iocb, 1);
4808 pla = qlt_plogi_ack_find_add(vha, &port_id, iocb);
4810 ql_dbg(ql_dbg_disc + ql_dbg_verbose, vha, 0xffff,
4811 "%s %d %8phC Term INOT due to mem alloc fail",
4813 iocb->u.isp24.port_name);
4814 qlt_send_term_imm_notif(vha, iocb, 1);
4818 if (conflict_sess) {
4819 conflict_sess->login_gen++;
4820 qlt_plogi_ack_link(vha, pla, conflict_sess,
4821 QLT_PLOGI_LINK_CONFLICT);
4826 ql_dbg(ql_dbg_disc, vha, 0xffff,
4827 "%s %d %8phC post new sess\n",
4828 __func__, __LINE__, iocb->u.isp24.port_name);
4829 if (iocb->u.isp24.status_subcode == ELS_PLOGI)
4830 qla24xx_post_newsess_work(vha, &port_id,
4831 iocb->u.isp24.port_name,
4832 iocb->u.isp24.u.plogi.node_name,
4835 qla24xx_post_newsess_work(vha, &port_id,
4836 iocb->u.isp24.port_name, NULL,
4842 if (sess->disc_state == DSC_UPD_FCPORT) {
4846 * Remote port registration is still going on from
4847 * previous login. Allow it to finish before we
4848 * accept the new login.
4850 sess->next_disc_state = DSC_DELETE_PEND;
4851 sec = jiffies_to_msecs(jiffies -
4852 sess->jiffies_at_registration) / 1000;
4853 if (sess->sec_since_registration < sec && sec &&
4855 sess->sec_since_registration = sec;
4856 ql_dbg(ql_dbg_disc, vha, 0xffff,
4857 "%s %8phC - Slow Rport registration (%d Sec)\n",
4858 __func__, sess->port_name, sec);
4861 if (!conflict_sess) {
4862 list_del(&pla->list);
4863 kmem_cache_free(qla_tgt_plogi_cachep, pla);
4866 qlt_send_term_imm_notif(vha, iocb, 1);
4870 qlt_plogi_ack_link(vha, pla, sess, QLT_PLOGI_LINK_SAME_WWN);
4871 sess->d_id = port_id;
4873 sess->loop_id = loop_id;
4875 if (iocb->u.isp24.status_subcode == ELS_PLOGI) {
4876 /* remote port has assigned Port ID */
4877 if (N2N_TOPO(vha->hw) && fcport_is_bigger(sess))
4878 vha->d_id = sess->d_id;
4880 ql_dbg(ql_dbg_disc, vha, 0xffff,
4881 "%s %8phC - send port online\n",
4882 __func__, sess->port_name);
4884 qla2x00_post_aen_work(vha, FCH_EVT_PORT_ONLINE,
4888 if (iocb->u.isp24.status_subcode == ELS_PRLI) {
4889 sess->fw_login_state = DSC_LS_PRLI_PEND;
4891 sess->loop_id = loop_id;
4892 sess->d_id = port_id;
4893 sess->fw_login_state = DSC_LS_PRLI_PEND;
4894 wd3_lo = le16_to_cpu(iocb->u.isp24.u.prli.wd3_lo);
4897 sess->conf_compl_supported = 1;
4899 if ((wd3_lo & BIT_4) == 0)
4900 sess->port_type = FCT_INITIATOR;
4902 sess->port_type = FCT_TARGET;
4905 sess->fw_login_state = DSC_LS_PLOGI_PEND;
4908 ql_dbg(ql_dbg_disc, vha, 0x20f9,
4909 "%s %d %8phC DS %d\n",
4910 __func__, __LINE__, sess->port_name, sess->disc_state);
4912 switch (sess->disc_state) {
4914 case DSC_LOGIN_PEND:
4915 qlt_plogi_ack_unref(vha, pla);
4920 * Under normal circumstances we want to release nport handle
4921 * during LOGO process to avoid nport handle leaks inside FW.
4922 * The exception is when LOGO is done while another PLOGI with
4923 * the same nport handle is waiting as might be the case here.
4924 * Note: there is always a possibily of a race where session
4925 * deletion has already started for other reasons (e.g. ACL
4926 * removal) and now PLOGI arrives:
4927 * 1. if PLOGI arrived in FW after nport handle has been freed,
4928 * FW must have assigned this PLOGI a new/same handle and we
4929 * can proceed ACK'ing it as usual when session deletion
4931 * 2. if PLOGI arrived in FW before LOGO with LCF_FREE_NPORT
4932 * bit reached it, the handle has now been released. We'll
4933 * get an error when we ACK this PLOGI. Nothing will be sent
4934 * back to initiator. Initiator should eventually retry
4935 * PLOGI and situation will correct itself.
4937 sess->keep_nport_handle = ((sess->loop_id == loop_id) &&
4938 (sess->d_id.b24 == port_id.b24));
4940 ql_dbg(ql_dbg_disc, vha, 0x20f9,
4941 "%s %d %8phC post del sess\n",
4942 __func__, __LINE__, sess->port_name);
4945 qlt_schedule_sess_for_deletion(sess);
4953 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
4955 static int qlt_24xx_handle_els(struct scsi_qla_host *vha,
4956 struct imm_ntfy_from_isp *iocb)
4958 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
4959 struct qla_hw_data *ha = vha->hw;
4960 struct fc_port *sess = NULL, *conflict_sess = NULL;
4966 unsigned long flags;
4968 lockdep_assert_held(&ha->hardware_lock);
4970 wwn = wwn_to_u64(iocb->u.isp24.port_name);
4972 port_id.b.domain = iocb->u.isp24.port_id[2];
4973 port_id.b.area = iocb->u.isp24.port_id[1];
4974 port_id.b.al_pa = iocb->u.isp24.port_id[0];
4975 port_id.b.rsvd_1 = 0;
4977 loop_id = le16_to_cpu(iocb->u.isp24.nport_handle);
4979 ql_dbg(ql_dbg_disc, vha, 0xf026,
4980 "qla_target(%d): Port ID: %02x:%02x:%02x ELS opcode: 0x%02x lid %d %8phC\n",
4981 vha->vp_idx, iocb->u.isp24.port_id[2],
4982 iocb->u.isp24.port_id[1], iocb->u.isp24.port_id[0],
4983 iocb->u.isp24.status_subcode, loop_id,
4984 iocb->u.isp24.port_name);
4986 /* res = 1 means ack at the end of thread
4987 * res = 0 means ack async/later.
4989 switch (iocb->u.isp24.status_subcode) {
4991 res = qlt_handle_login(vha, iocb);
4996 sess = qla2x00_find_fcport_by_wwpn(vha,
4997 iocb->u.isp24.port_name, 1);
4999 if (vha->hw->flags.edif_enabled && sess &&
5000 (!(sess->flags & FCF_FCSP_DEVICE) ||
5001 !sess->edif.authok)) {
5002 ql_dbg(ql_dbg_disc, vha, 0xffff,
5003 "%s %d %8phC Term PRLI due to unauthorize PRLI\n",
5004 __func__, __LINE__, iocb->u.isp24.port_name);
5005 qlt_send_term_imm_notif(vha, iocb, 1);
5009 if (sess && sess->plogi_link[QLT_PLOGI_LINK_SAME_WWN]) {
5010 ql_dbg(ql_dbg_disc, vha, 0xffff,
5011 "%s %d %8phC Term PRLI due to PLOGI ACK not completed\n",
5013 iocb->u.isp24.port_name);
5014 qlt_send_term_imm_notif(vha, iocb, 1);
5018 res = qlt_handle_login(vha, iocb);
5022 if (IS_SW_RESV_ADDR(port_id)) {
5027 wd3_lo = le16_to_cpu(iocb->u.isp24.u.prli.wd3_lo);
5030 spin_lock_irqsave(&tgt->ha->tgt.sess_lock, flags);
5031 sess = qlt_find_sess_invalidate_other(vha, wwn, port_id,
5032 loop_id, &conflict_sess);
5033 spin_unlock_irqrestore(&tgt->ha->tgt.sess_lock, flags);
5036 if (conflict_sess) {
5037 switch (conflict_sess->disc_state) {
5039 case DSC_DELETE_PEND:
5042 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf09b,
5043 "PRLI with conflicting sess %p port %8phC\n",
5044 conflict_sess, conflict_sess->port_name);
5045 conflict_sess->fw_login_state =
5046 DSC_LS_PORT_UNAVAIL;
5047 qlt_send_term_imm_notif(vha, iocb, 1);
5054 bool delete = false;
5057 if (vha->hw->flags.edif_enabled && sess &&
5058 (!(sess->flags & FCF_FCSP_DEVICE) ||
5059 !sess->edif.authok)) {
5060 ql_dbg(ql_dbg_disc, vha, 0xffff,
5061 "%s %d %8phC Term PRLI due to unauthorize prli\n",
5062 __func__, __LINE__, iocb->u.isp24.port_name);
5063 qlt_send_term_imm_notif(vha, iocb, 1);
5067 spin_lock_irqsave(&tgt->ha->tgt.sess_lock, flags);
5068 switch (sess->fw_login_state) {
5069 case DSC_LS_PLOGI_PEND:
5070 case DSC_LS_PLOGI_COMP:
5071 case DSC_LS_PRLI_COMP:
5078 switch (sess->disc_state) {
5079 case DSC_UPD_FCPORT:
5080 spin_unlock_irqrestore(&tgt->ha->tgt.sess_lock,
5083 sec = jiffies_to_msecs(jiffies -
5084 sess->jiffies_at_registration)/1000;
5085 if (sess->sec_since_registration < sec && sec &&
5087 sess->sec_since_registration = sec;
5088 ql_dbg(ql_dbg_disc, sess->vha, 0xffff,
5089 "%s %8phC : Slow Rport registration(%d Sec)\n",
5090 __func__, sess->port_name, sec);
5092 qlt_send_term_imm_notif(vha, iocb, 1);
5095 case DSC_LOGIN_PEND:
5097 case DSC_LOGIN_COMPLETE:
5106 spin_unlock_irqrestore(&tgt->ha->tgt.sess_lock,
5109 * Impatient initiator sent PRLI before last
5110 * PLOGI could finish. Will force him to re-try,
5111 * while last one finishes.
5113 ql_log(ql_log_warn, sess->vha, 0xf095,
5114 "sess %p PRLI received, before plogi ack.\n",
5116 qlt_send_term_imm_notif(vha, iocb, 1);
5122 * This shouldn't happen under normal circumstances,
5123 * since we have deleted the old session during PLOGI
5125 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf096,
5126 "PRLI (loop_id %#04x) for existing sess %p (loop_id %#04x)\n",
5127 sess->loop_id, sess, iocb->u.isp24.nport_handle);
5130 sess->loop_id = loop_id;
5131 sess->d_id = port_id;
5132 sess->fw_login_state = DSC_LS_PRLI_PEND;
5135 sess->conf_compl_supported = 1;
5137 if ((wd3_lo & BIT_4) == 0)
5138 sess->port_type = FCT_INITIATOR;
5140 sess->port_type = FCT_TARGET;
5142 spin_unlock_irqrestore(&tgt->ha->tgt.sess_lock, flags);
5144 res = 1; /* send notify ack */
5146 /* Make session global (not used in fabric mode) */
5147 if (ha->current_topology != ISP_CFG_F) {
5149 ql_dbg(ql_dbg_disc, vha, 0x20fa,
5150 "%s %d %8phC post nack\n",
5151 __func__, __LINE__, sess->port_name);
5152 qla24xx_post_nack_work(vha, sess, iocb,
5156 set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
5157 set_bit(LOCAL_LOOP_UPDATE, &vha->dpc_flags);
5158 qla2xxx_wake_dpc(vha);
5162 ql_dbg(ql_dbg_disc, vha, 0x20fb,
5163 "%s %d %8phC post nack\n",
5164 __func__, __LINE__, sess->port_name);
5165 qla24xx_post_nack_work(vha, sess, iocb,
5173 if (le16_to_cpu(iocb->u.isp24.flags) &
5174 NOTIFY24XX_FLAGS_GLOBAL_TPRLO) {
5176 qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS);
5183 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
5184 sess = qla2x00_find_fcport_by_loopid(vha, loop_id);
5185 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
5189 sess->fw_login_state = DSC_LS_LOGO_PEND;
5190 sess->logo_ack_needed = 1;
5191 memcpy(sess->iocb, iocb, IOCB_SIZE);
5194 res = qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS_SESS);
5196 ql_dbg(ql_dbg_disc, vha, 0x20fc,
5197 "%s: logo %llx res %d sess %p ",
5198 __func__, wwn, res, sess);
5201 * cmd went upper layer, look for qlt_xmit_tm_rsp()
5202 * for LOGO_ACK & sess delete
5207 /* cmd did not go to upper layer. */
5209 qlt_schedule_sess_for_deletion(sess);
5212 /* else logo will be ack */
5218 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
5220 if (tgt->link_reinit_iocb_pending) {
5221 qlt_send_notify_ack(ha->base_qpair,
5222 &tgt->link_reinit_iocb, 0, 0, 0, 0, 0, 0);
5223 tgt->link_reinit_iocb_pending = 0;
5226 sess = qla2x00_find_fcport_by_wwpn(vha,
5227 iocb->u.isp24.port_name, 1);
5229 ql_dbg(ql_dbg_disc, vha, 0x20fd,
5230 "sess %p lid %d|%d DS %d LS %d\n",
5231 sess, sess->loop_id, loop_id,
5232 sess->disc_state, sess->fw_login_state);
5235 res = 1; /* send notify ack */
5239 case ELS_FLOGI: /* should never happen */
5241 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf061,
5242 "qla_target(%d): Unsupported ELS command %x "
5243 "received\n", vha->vp_idx, iocb->u.isp24.status_subcode);
5244 res = qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS_SESS);
5248 ql_dbg(ql_dbg_disc, vha, 0xf026,
5249 "qla_target(%d): Exit ELS opcode: 0x%02x res %d\n",
5250 vha->vp_idx, iocb->u.isp24.status_subcode, res);
5256 * ha->hardware_lock supposed to be held on entry.
5257 * Might drop it, then reacquire.
5259 static void qlt_handle_imm_notify(struct scsi_qla_host *vha,
5260 struct imm_ntfy_from_isp *iocb)
5262 struct qla_hw_data *ha = vha->hw;
5263 uint32_t add_flags = 0;
5264 int send_notify_ack = 1;
5267 lockdep_assert_held(&ha->hardware_lock);
5269 status = le16_to_cpu(iocb->u.isp2x.status);
5271 case IMM_NTFY_LIP_RESET:
5273 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf032,
5274 "qla_target(%d): LIP reset (loop %#x), subcode %x\n",
5275 vha->vp_idx, le16_to_cpu(iocb->u.isp24.nport_handle),
5276 iocb->u.isp24.status_subcode);
5278 if (qlt_reset(vha, iocb, QLA_TGT_ABORT_ALL) == 0)
5279 send_notify_ack = 0;
5283 case IMM_NTFY_LIP_LINK_REINIT:
5285 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
5287 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf033,
5288 "qla_target(%d): LINK REINIT (loop %#x, "
5289 "subcode %x)\n", vha->vp_idx,
5290 le16_to_cpu(iocb->u.isp24.nport_handle),
5291 iocb->u.isp24.status_subcode);
5292 if (tgt->link_reinit_iocb_pending) {
5293 qlt_send_notify_ack(ha->base_qpair,
5294 &tgt->link_reinit_iocb, 0, 0, 0, 0, 0, 0);
5296 memcpy(&tgt->link_reinit_iocb, iocb, sizeof(*iocb));
5297 tgt->link_reinit_iocb_pending = 1;
5299 * QLogic requires to wait after LINK REINIT for possible
5300 * PDISC or ADISC ELS commands
5302 send_notify_ack = 0;
5306 case IMM_NTFY_PORT_LOGOUT:
5307 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf034,
5308 "qla_target(%d): Port logout (loop "
5309 "%#x, subcode %x)\n", vha->vp_idx,
5310 le16_to_cpu(iocb->u.isp24.nport_handle),
5311 iocb->u.isp24.status_subcode);
5313 if (qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS_SESS) == 0)
5314 send_notify_ack = 0;
5315 /* The sessions will be cleared in the callback, if needed */
5318 case IMM_NTFY_GLBL_TPRLO:
5319 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf035,
5320 "qla_target(%d): Global TPRLO (%x)\n", vha->vp_idx, status);
5321 if (qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS) == 0)
5322 send_notify_ack = 0;
5323 /* The sessions will be cleared in the callback, if needed */
5326 case IMM_NTFY_PORT_CONFIG:
5327 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf036,
5328 "qla_target(%d): Port config changed (%x)\n", vha->vp_idx,
5330 if (qlt_reset(vha, iocb, QLA_TGT_ABORT_ALL) == 0)
5331 send_notify_ack = 0;
5332 /* The sessions will be cleared in the callback, if needed */
5335 case IMM_NTFY_GLBL_LOGO:
5336 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06a,
5337 "qla_target(%d): Link failure detected\n",
5339 /* I_T nexus loss */
5340 if (qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS) == 0)
5341 send_notify_ack = 0;
5344 case IMM_NTFY_IOCB_OVERFLOW:
5345 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06b,
5346 "qla_target(%d): Cannot provide requested "
5347 "capability (IOCB overflowed the immediate notify "
5348 "resource count)\n", vha->vp_idx);
5351 case IMM_NTFY_ABORT_TASK:
5352 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf037,
5353 "qla_target(%d): Abort Task (S %08x I %#x -> "
5354 "L %#x)\n", vha->vp_idx,
5355 le16_to_cpu(iocb->u.isp2x.seq_id),
5356 GET_TARGET_ID(ha, (struct atio_from_isp *)iocb),
5357 le16_to_cpu(iocb->u.isp2x.lun));
5358 if (qlt_abort_task(vha, iocb) == 0)
5359 send_notify_ack = 0;
5362 case IMM_NTFY_RESOURCE:
5363 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06c,
5364 "qla_target(%d): Out of resources, host %ld\n",
5365 vha->vp_idx, vha->host_no);
5368 case IMM_NTFY_MSG_RX:
5369 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf038,
5370 "qla_target(%d): Immediate notify task %x\n",
5371 vha->vp_idx, iocb->u.isp2x.task_flags);
5375 if (qlt_24xx_handle_els(vha, iocb) == 0)
5376 send_notify_ack = 0;
5379 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06d,
5380 "qla_target(%d): Received unknown immediate "
5381 "notify status %x\n", vha->vp_idx, status);
5385 if (send_notify_ack)
5386 qlt_send_notify_ack(ha->base_qpair, iocb, add_flags, 0, 0, 0,
5391 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
5392 * This function sends busy to ISP 2xxx or 24xx.
5394 static int __qlt_send_busy(struct qla_qpair *qpair,
5395 struct atio_from_isp *atio, uint16_t status)
5397 struct scsi_qla_host *vha = qpair->vha;
5398 struct ctio7_to_24xx *ctio24;
5399 struct qla_hw_data *ha = vha->hw;
5401 struct fc_port *sess = NULL;
5402 unsigned long flags;
5406 id = be_to_port_id(atio->u.isp24.fcp_hdr.s_id);
5408 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
5409 sess = qla2x00_find_fcport_by_nportid(vha, &id, 1);
5410 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
5412 qlt_send_term_exchange(qpair, NULL, atio, 1, 0);
5415 /* Sending marker isn't necessary, since we called from ISR */
5417 pkt = (request_t *)__qla2x00_alloc_iocbs(qpair, NULL);
5419 ql_dbg(ql_dbg_io, vha, 0x3063,
5420 "qla_target(%d): %s failed: unable to allocate "
5421 "request packet", vha->vp_idx, __func__);
5425 qpair->tgt_counters.num_q_full_sent++;
5426 pkt->entry_count = 1;
5427 pkt->handle = QLA_TGT_SKIP_HANDLE | CTIO_COMPLETION_HANDLE_MARK;
5429 ctio24 = (struct ctio7_to_24xx *)pkt;
5430 ctio24->entry_type = CTIO_TYPE7;
5431 ctio24->nport_handle = cpu_to_le16(sess->loop_id);
5432 ctio24->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
5433 ctio24->vp_index = vha->vp_idx;
5434 ctio24->initiator_id = be_id_to_le(atio->u.isp24.fcp_hdr.s_id);
5435 ctio24->exchange_addr = atio->u.isp24.exchange_addr;
5436 temp = (atio->u.isp24.attr << 9) |
5437 CTIO7_FLAGS_STATUS_MODE_1 | CTIO7_FLAGS_SEND_STATUS |
5438 CTIO7_FLAGS_DONT_RET_CTIO;
5439 ctio24->u.status1.flags = cpu_to_le16(temp);
5441 * CTIO from fw w/o se_cmd doesn't provide enough info to retry it,
5442 * if the explicit conformation is used.
5444 ctio24->u.status1.ox_id =
5445 cpu_to_le16(be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id));
5446 ctio24->u.status1.scsi_status = cpu_to_le16(status);
5448 ctio24->u.status1.residual = cpu_to_le32(get_datalen_for_atio(atio));
5450 if (ctio24->u.status1.residual != 0)
5451 ctio24->u.status1.scsi_status |= cpu_to_le16(SS_RESIDUAL_UNDER);
5453 /* Memory Barrier */
5455 if (qpair->reqq_start_iocbs)
5456 qpair->reqq_start_iocbs(qpair);
5458 qla2x00_start_iocbs(vha, qpair->req);
5463 * This routine is used to allocate a command for either a QFull condition
5464 * (ie reply SAM_STAT_BUSY) or to terminate an exchange that did not go
5468 qlt_alloc_qfull_cmd(struct scsi_qla_host *vha,
5469 struct atio_from_isp *atio, uint16_t status, int qfull)
5471 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
5472 struct qla_hw_data *ha = vha->hw;
5473 struct fc_port *sess;
5474 struct qla_tgt_cmd *cmd;
5475 unsigned long flags;
5477 if (unlikely(tgt->tgt_stop)) {
5478 ql_dbg(ql_dbg_io, vha, 0x300a,
5479 "New command while device %p is shutting down\n", tgt);
5483 if ((vha->hw->tgt.num_qfull_cmds_alloc + 1) > MAX_QFULL_CMDS_ALLOC) {
5484 vha->hw->tgt.num_qfull_cmds_dropped++;
5485 if (vha->hw->tgt.num_qfull_cmds_dropped >
5486 vha->qla_stats.stat_max_qfull_cmds_dropped)
5487 vha->qla_stats.stat_max_qfull_cmds_dropped =
5488 vha->hw->tgt.num_qfull_cmds_dropped;
5490 ql_dbg(ql_dbg_io, vha, 0x3068,
5491 "qla_target(%d): %s: QFull CMD dropped[%d]\n",
5492 vha->vp_idx, __func__,
5493 vha->hw->tgt.num_qfull_cmds_dropped);
5495 qlt_chk_exch_leak_thresh_hold(vha);
5499 sess = ha->tgt.tgt_ops->find_sess_by_s_id
5500 (vha, atio->u.isp24.fcp_hdr.s_id);
5504 cmd = ha->tgt.tgt_ops->get_cmd(sess);
5506 ql_dbg(ql_dbg_io, vha, 0x3009,
5507 "qla_target(%d): %s: Allocation of cmd failed\n",
5508 vha->vp_idx, __func__);
5510 vha->hw->tgt.num_qfull_cmds_dropped++;
5511 if (vha->hw->tgt.num_qfull_cmds_dropped >
5512 vha->qla_stats.stat_max_qfull_cmds_dropped)
5513 vha->qla_stats.stat_max_qfull_cmds_dropped =
5514 vha->hw->tgt.num_qfull_cmds_dropped;
5516 qlt_chk_exch_leak_thresh_hold(vha);
5520 qlt_incr_num_pend_cmds(vha);
5521 INIT_LIST_HEAD(&cmd->cmd_list);
5522 memcpy(&cmd->atio, atio, sizeof(*atio));
5524 cmd->tgt = vha->vha_tgt.qla_tgt;
5526 cmd->reset_count = ha->base_qpair->chip_reset;
5528 cmd->qpair = ha->base_qpair;
5532 /* NOTE: borrowing the state field to carry the status */
5533 cmd->state = status;
5535 cmd->term_exchg = 1;
5537 spin_lock_irqsave(&vha->hw->tgt.q_full_lock, flags);
5538 list_add_tail(&cmd->cmd_list, &vha->hw->tgt.q_full_list);
5540 vha->hw->tgt.num_qfull_cmds_alloc++;
5541 if (vha->hw->tgt.num_qfull_cmds_alloc >
5542 vha->qla_stats.stat_max_qfull_cmds_alloc)
5543 vha->qla_stats.stat_max_qfull_cmds_alloc =
5544 vha->hw->tgt.num_qfull_cmds_alloc;
5545 spin_unlock_irqrestore(&vha->hw->tgt.q_full_lock, flags);
5549 qlt_free_qfull_cmds(struct qla_qpair *qpair)
5551 struct scsi_qla_host *vha = qpair->vha;
5552 struct qla_hw_data *ha = vha->hw;
5553 unsigned long flags;
5554 struct qla_tgt_cmd *cmd, *tcmd;
5555 struct list_head free_list, q_full_list;
5558 if (list_empty(&ha->tgt.q_full_list))
5561 INIT_LIST_HEAD(&free_list);
5562 INIT_LIST_HEAD(&q_full_list);
5564 spin_lock_irqsave(&vha->hw->tgt.q_full_lock, flags);
5565 if (list_empty(&ha->tgt.q_full_list)) {
5566 spin_unlock_irqrestore(&vha->hw->tgt.q_full_lock, flags);
5570 list_splice_init(&vha->hw->tgt.q_full_list, &q_full_list);
5571 spin_unlock_irqrestore(&vha->hw->tgt.q_full_lock, flags);
5573 spin_lock_irqsave(qpair->qp_lock_ptr, flags);
5574 list_for_each_entry_safe(cmd, tcmd, &q_full_list, cmd_list) {
5576 /* cmd->state is a borrowed field to hold status */
5577 rc = __qlt_send_busy(qpair, &cmd->atio, cmd->state);
5578 else if (cmd->term_exchg)
5579 rc = __qlt_send_term_exchange(qpair, NULL, &cmd->atio);
5585 ql_dbg(ql_dbg_io, vha, 0x3006,
5586 "%s: busy sent for ox_id[%04x]\n", __func__,
5587 be16_to_cpu(cmd->atio.u.isp24.fcp_hdr.ox_id));
5588 else if (cmd->term_exchg)
5589 ql_dbg(ql_dbg_io, vha, 0x3007,
5590 "%s: Term exchg sent for ox_id[%04x]\n", __func__,
5591 be16_to_cpu(cmd->atio.u.isp24.fcp_hdr.ox_id));
5593 ql_dbg(ql_dbg_io, vha, 0x3008,
5594 "%s: Unexpected cmd in QFull list %p\n", __func__,
5597 list_move_tail(&cmd->cmd_list, &free_list);
5599 /* piggy back on hardware_lock for protection */
5600 vha->hw->tgt.num_qfull_cmds_alloc--;
5602 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
5606 list_for_each_entry_safe(cmd, tcmd, &free_list, cmd_list) {
5607 list_del(&cmd->cmd_list);
5608 /* This cmd was never sent to TCM. There is no need
5609 * to schedule free or call free_cmd
5614 if (!list_empty(&q_full_list)) {
5615 spin_lock_irqsave(&vha->hw->tgt.q_full_lock, flags);
5616 list_splice(&q_full_list, &vha->hw->tgt.q_full_list);
5617 spin_unlock_irqrestore(&vha->hw->tgt.q_full_lock, flags);
5624 qlt_send_busy(struct qla_qpair *qpair, struct atio_from_isp *atio,
5628 struct scsi_qla_host *vha = qpair->vha;
5630 rc = __qlt_send_busy(qpair, atio, status);
5632 qlt_alloc_qfull_cmd(vha, atio, status, 1);
5636 qlt_chk_qfull_thresh_hold(struct scsi_qla_host *vha, struct qla_qpair *qpair,
5637 struct atio_from_isp *atio, uint8_t ha_locked)
5639 struct qla_hw_data *ha = vha->hw;
5640 unsigned long flags;
5642 if (ha->tgt.num_pend_cmds < Q_FULL_THRESH_HOLD(ha))
5646 spin_lock_irqsave(&ha->hardware_lock, flags);
5647 qlt_send_busy(qpair, atio, qla_sam_status);
5649 spin_unlock_irqrestore(&ha->hardware_lock, flags);
5654 /* ha->hardware_lock supposed to be held on entry */
5655 /* called via callback from qla2xxx */
5656 static void qlt_24xx_atio_pkt(struct scsi_qla_host *vha,
5657 struct atio_from_isp *atio, uint8_t ha_locked)
5659 struct qla_hw_data *ha = vha->hw;
5660 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
5662 unsigned long flags = 0;
5664 if (unlikely(tgt == NULL)) {
5665 ql_dbg(ql_dbg_tgt, vha, 0x3064,
5666 "ATIO pkt, but no tgt (ha %p)", ha);
5670 * In tgt_stop mode we also should allow all requests to pass.
5671 * Otherwise, some commands can stuck.
5674 tgt->atio_irq_cmd_count++;
5676 switch (atio->u.raw.entry_type) {
5678 if (unlikely(atio->u.isp24.exchange_addr ==
5679 cpu_to_le32(ATIO_EXCHANGE_ADDRESS_UNKNOWN))) {
5680 ql_dbg(ql_dbg_io, vha, 0x3065,
5681 "qla_target(%d): ATIO_TYPE7 "
5682 "received with UNKNOWN exchange address, "
5683 "sending QUEUE_FULL\n", vha->vp_idx);
5685 spin_lock_irqsave(&ha->hardware_lock, flags);
5686 qlt_send_busy(ha->base_qpair, atio, qla_sam_status);
5688 spin_unlock_irqrestore(&ha->hardware_lock,
5693 if (likely(atio->u.isp24.fcp_cmnd.task_mgmt_flags == 0)) {
5694 rc = qlt_chk_qfull_thresh_hold(vha, ha->base_qpair,
5697 tgt->atio_irq_cmd_count--;
5700 rc = qlt_handle_cmd_for_atio(vha, atio);
5702 rc = qlt_handle_task_mgmt(vha, atio);
5704 if (unlikely(rc != 0)) {
5706 spin_lock_irqsave(&ha->hardware_lock, flags);
5709 ql_dbg(ql_dbg_tgt, vha, 0xe05f,
5710 "qla_target: Unable to send command to target\n");
5713 ql_dbg(ql_dbg_tgt, vha, 0xe05f,
5714 "qla_target: Unable to send command to target, sending TERM EXCHANGE for rsp\n");
5715 qlt_send_term_exchange(ha->base_qpair, NULL,
5719 ql_dbg(ql_dbg_tgt, vha, 0xe060,
5720 "qla_target(%d): Unable to send command to target, sending BUSY status\n",
5722 qlt_send_busy(ha->base_qpair, atio,
5726 ql_dbg(ql_dbg_tgt, vha, 0xe060,
5727 "qla_target(%d): Unable to send command to target, sending BUSY status\n",
5729 qlt_send_busy(ha->base_qpair, atio,
5734 spin_unlock_irqrestore(&ha->hardware_lock,
5739 case IMMED_NOTIFY_TYPE:
5741 if (unlikely(atio->u.isp2x.entry_status != 0)) {
5742 ql_dbg(ql_dbg_tgt, vha, 0xe05b,
5743 "qla_target(%d): Received ATIO packet %x "
5744 "with error status %x\n", vha->vp_idx,
5745 atio->u.raw.entry_type,
5746 atio->u.isp2x.entry_status);
5749 ql_dbg(ql_dbg_tgt, vha, 0xe02e, "%s", "IMMED_NOTIFY ATIO");
5752 spin_lock_irqsave(&ha->hardware_lock, flags);
5753 qlt_handle_imm_notify(vha, (struct imm_ntfy_from_isp *)atio);
5755 spin_unlock_irqrestore(&ha->hardware_lock, flags);
5760 ql_dbg(ql_dbg_tgt, vha, 0xe05c,
5761 "qla_target(%d): Received unknown ATIO atio "
5762 "type %x\n", vha->vp_idx, atio->u.raw.entry_type);
5766 tgt->atio_irq_cmd_count--;
5770 * qpair lock is assume to be held
5771 * rc = 0 : send terminate & abts respond
5772 * rc != 0: do not send term & abts respond
5774 static int qlt_chk_unresolv_exchg(struct scsi_qla_host *vha,
5775 struct qla_qpair *qpair, struct abts_resp_from_24xx_fw *entry)
5777 struct qla_hw_data *ha = vha->hw;
5781 * Detect unresolved exchange. If the same ABTS is unable
5782 * to terminate an existing command and the same ABTS loops
5783 * between FW & Driver, then force FW dump. Under 1 jiff,
5784 * we should see multiple loops.
5786 if (qpair->retry_term_exchg_addr == entry->exchange_addr_to_abort &&
5787 qpair->retry_term_jiff == jiffies) {
5788 /* found existing exchange */
5789 qpair->retry_term_cnt++;
5790 if (qpair->retry_term_cnt >= 5) {
5792 qpair->retry_term_cnt = 0;
5793 ql_log(ql_log_warn, vha, 0xffff,
5794 "Unable to send ABTS Respond. Dumping firmware.\n");
5795 ql_dump_buffer(ql_dbg_tgt_mgt + ql_dbg_buffer,
5796 vha, 0xffff, (uint8_t *)entry, sizeof(*entry));
5798 if (qpair == ha->base_qpair)
5799 ha->isp_ops->fw_dump(vha);
5801 qla2xxx_dump_fw(vha);
5803 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
5804 qla2xxx_wake_dpc(vha);
5806 } else if (qpair->retry_term_jiff != jiffies) {
5807 qpair->retry_term_exchg_addr = entry->exchange_addr_to_abort;
5808 qpair->retry_term_cnt = 0;
5809 qpair->retry_term_jiff = jiffies;
5816 static void qlt_handle_abts_completion(struct scsi_qla_host *vha,
5817 struct rsp_que *rsp, response_t *pkt)
5819 struct abts_resp_from_24xx_fw *entry =
5820 (struct abts_resp_from_24xx_fw *)pkt;
5821 u32 h = pkt->handle & ~QLA_TGT_HANDLE_MASK;
5822 struct qla_tgt_mgmt_cmd *mcmd;
5823 struct qla_hw_data *ha = vha->hw;
5825 mcmd = qlt_ctio_to_cmd(vha, rsp, pkt->handle, pkt);
5826 if (mcmd == NULL && h != QLA_TGT_SKIP_HANDLE) {
5827 ql_dbg(ql_dbg_async, vha, 0xe064,
5828 "qla_target(%d): ABTS Comp without mcmd\n",
5835 vha->vha_tgt.qla_tgt->abts_resp_expected--;
5837 ql_dbg(ql_dbg_tgt, vha, 0xe038,
5838 "ABTS_RESP_24XX: compl_status %x\n",
5839 entry->compl_status);
5841 if (le16_to_cpu(entry->compl_status) != ABTS_RESP_COMPL_SUCCESS) {
5842 if (le32_to_cpu(entry->error_subcode1) == 0x1E &&
5843 le32_to_cpu(entry->error_subcode2) == 0) {
5844 if (qlt_chk_unresolv_exchg(vha, rsp->qpair, entry)) {
5845 ha->tgt.tgt_ops->free_mcmd(mcmd);
5848 qlt_24xx_retry_term_exchange(vha, rsp->qpair,
5851 ql_dbg(ql_dbg_tgt, vha, 0xe063,
5852 "qla_target(%d): ABTS_RESP_24XX failed %x (subcode %x:%x)",
5853 vha->vp_idx, entry->compl_status,
5854 entry->error_subcode1,
5855 entry->error_subcode2);
5856 ha->tgt.tgt_ops->free_mcmd(mcmd);
5859 ha->tgt.tgt_ops->free_mcmd(mcmd);
5863 /* ha->hardware_lock supposed to be held on entry */
5864 /* called via callback from qla2xxx */
5865 static void qlt_response_pkt(struct scsi_qla_host *vha,
5866 struct rsp_que *rsp, response_t *pkt)
5868 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
5870 if (unlikely(tgt == NULL)) {
5871 ql_dbg(ql_dbg_tgt, vha, 0xe05d,
5872 "qla_target(%d): Response pkt %x received, but no tgt (ha %p)\n",
5873 vha->vp_idx, pkt->entry_type, vha->hw);
5878 * In tgt_stop mode we also should allow all requests to pass.
5879 * Otherwise, some commands can stuck.
5882 switch (pkt->entry_type) {
5886 struct ctio7_from_24xx *entry = (struct ctio7_from_24xx *)pkt;
5888 qlt_do_ctio_completion(vha, rsp, entry->handle,
5889 le16_to_cpu(entry->status)|(pkt->entry_status << 16),
5894 case ACCEPT_TGT_IO_TYPE:
5896 struct atio_from_isp *atio = (struct atio_from_isp *)pkt;
5899 if (atio->u.isp2x.status !=
5900 cpu_to_le16(ATIO_CDB_VALID)) {
5901 ql_dbg(ql_dbg_tgt, vha, 0xe05e,
5902 "qla_target(%d): ATIO with error "
5903 "status %x received\n", vha->vp_idx,
5904 le16_to_cpu(atio->u.isp2x.status));
5908 rc = qlt_chk_qfull_thresh_hold(vha, rsp->qpair, atio, 1);
5912 rc = qlt_handle_cmd_for_atio(vha, atio);
5913 if (unlikely(rc != 0)) {
5916 ql_dbg(ql_dbg_tgt, vha, 0xe05f,
5917 "qla_target: Unable to send command to target\n");
5920 ql_dbg(ql_dbg_tgt, vha, 0xe05f,
5921 "qla_target: Unable to send command to target, sending TERM EXCHANGE for rsp\n");
5922 qlt_send_term_exchange(rsp->qpair, NULL,
5926 ql_dbg(ql_dbg_tgt, vha, 0xe060,
5927 "qla_target(%d): Unable to send command to target, sending BUSY status\n",
5929 qlt_send_busy(rsp->qpair, atio,
5933 ql_dbg(ql_dbg_tgt, vha, 0xe060,
5934 "qla_target(%d): Unable to send command to target, sending BUSY status\n",
5936 qlt_send_busy(rsp->qpair, atio,
5944 case CONTINUE_TGT_IO_TYPE:
5946 struct ctio_to_2xxx *entry = (struct ctio_to_2xxx *)pkt;
5948 qlt_do_ctio_completion(vha, rsp, entry->handle,
5949 le16_to_cpu(entry->status)|(pkt->entry_status << 16),
5956 struct ctio_to_2xxx *entry = (struct ctio_to_2xxx *)pkt;
5958 qlt_do_ctio_completion(vha, rsp, entry->handle,
5959 le16_to_cpu(entry->status)|(pkt->entry_status << 16),
5964 case IMMED_NOTIFY_TYPE:
5965 ql_dbg(ql_dbg_tgt, vha, 0xe035, "%s", "IMMED_NOTIFY\n");
5966 qlt_handle_imm_notify(vha, (struct imm_ntfy_from_isp *)pkt);
5969 case NOTIFY_ACK_TYPE:
5970 if (tgt->notify_ack_expected > 0) {
5971 struct nack_to_isp *entry = (struct nack_to_isp *)pkt;
5973 ql_dbg(ql_dbg_tgt, vha, 0xe036,
5974 "NOTIFY_ACK seq %08x status %x\n",
5975 le16_to_cpu(entry->u.isp2x.seq_id),
5976 le16_to_cpu(entry->u.isp2x.status));
5977 tgt->notify_ack_expected--;
5978 if (entry->u.isp2x.status !=
5979 cpu_to_le16(NOTIFY_ACK_SUCCESS)) {
5980 ql_dbg(ql_dbg_tgt, vha, 0xe061,
5981 "qla_target(%d): NOTIFY_ACK "
5982 "failed %x\n", vha->vp_idx,
5983 le16_to_cpu(entry->u.isp2x.status));
5986 ql_dbg(ql_dbg_tgt, vha, 0xe062,
5987 "qla_target(%d): Unexpected NOTIFY_ACK received\n",
5992 case ABTS_RECV_24XX:
5993 ql_dbg(ql_dbg_tgt, vha, 0xe037,
5994 "ABTS_RECV_24XX: instance %d\n", vha->vp_idx);
5995 qlt_24xx_handle_abts(vha, (struct abts_recv_from_24xx *)pkt);
5998 case ABTS_RESP_24XX:
5999 if (tgt->abts_resp_expected > 0) {
6000 qlt_handle_abts_completion(vha, rsp, pkt);
6002 ql_dbg(ql_dbg_tgt, vha, 0xe064,
6003 "qla_target(%d): Unexpected ABTS_RESP_24XX "
6004 "received\n", vha->vp_idx);
6009 ql_dbg(ql_dbg_tgt, vha, 0xe065,
6010 "qla_target(%d): Received unknown response pkt "
6011 "type %x\n", vha->vp_idx, pkt->entry_type);
6018 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
6020 void qlt_async_event(uint16_t code, struct scsi_qla_host *vha,
6023 struct qla_hw_data *ha = vha->hw;
6024 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
6027 if (!tgt || tgt->tgt_stop || tgt->tgt_stopped)
6030 if (((code == MBA_POINT_TO_POINT) || (code == MBA_CHG_IN_CONNECTION)) &&
6034 * In tgt_stop mode we also should allow all requests to pass.
6035 * Otherwise, some commands can stuck.
6040 case MBA_RESET: /* Reset */
6041 case MBA_SYSTEM_ERR: /* System Error */
6042 case MBA_REQ_TRANSFER_ERR: /* Request Transfer Error */
6043 case MBA_RSP_TRANSFER_ERR: /* Response Transfer Error */
6044 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03a,
6045 "qla_target(%d): System error async event %#x "
6046 "occurred", vha->vp_idx, code);
6048 case MBA_WAKEUP_THRES: /* Request Queue Wake-up. */
6049 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
6054 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03b,
6055 "qla_target(%d): Async LOOP_UP occurred "
6056 "(m[0]=%x, m[1]=%x, m[2]=%x, m[3]=%x)", vha->vp_idx,
6057 mailbox[0], mailbox[1], mailbox[2], mailbox[3]);
6058 if (tgt->link_reinit_iocb_pending) {
6059 qlt_send_notify_ack(ha->base_qpair,
6060 &tgt->link_reinit_iocb,
6062 tgt->link_reinit_iocb_pending = 0;
6067 case MBA_LIP_OCCURRED:
6070 case MBA_RSCN_UPDATE:
6071 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03c,
6072 "qla_target(%d): Async event %#x occurred "
6073 "(m[0]=%x, m[1]=%x, m[2]=%x, m[3]=%x)", vha->vp_idx, code,
6074 mailbox[0], mailbox[1], mailbox[2], mailbox[3]);
6077 case MBA_REJECTED_FCP_CMD:
6078 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf017,
6079 "qla_target(%d): Async event LS_REJECT occurred (m[0]=%x, m[1]=%x, m[2]=%x, m[3]=%x)",
6081 mailbox[0], mailbox[1], mailbox[2], mailbox[3]);
6083 if (mailbox[3] == 1) {
6084 /* exchange starvation. */
6085 vha->hw->exch_starvation++;
6086 if (vha->hw->exch_starvation > 5) {
6087 ql_log(ql_log_warn, vha, 0xd03a,
6088 "Exchange starvation-. Resetting RISC\n");
6090 vha->hw->exch_starvation = 0;
6091 if (IS_P3P_TYPE(vha->hw))
6092 set_bit(FCOE_CTX_RESET_NEEDED,
6095 set_bit(ISP_ABORT_NEEDED,
6097 qla2xxx_wake_dpc(vha);
6102 case MBA_PORT_UPDATE:
6103 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03d,
6104 "qla_target(%d): Port update async event %#x "
6105 "occurred: updating the ports database (m[0]=%x, m[1]=%x, "
6106 "m[2]=%x, m[3]=%x)", vha->vp_idx, code,
6107 mailbox[0], mailbox[1], mailbox[2], mailbox[3]);
6109 login_code = mailbox[2];
6110 if (login_code == 0x4) {
6111 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03e,
6112 "Async MB 2: Got PLOGI Complete\n");
6113 vha->hw->exch_starvation = 0;
6114 } else if (login_code == 0x7)
6115 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03f,
6116 "Async MB 2: Port Logged Out\n");
6124 static fc_port_t *qlt_get_port_database(struct scsi_qla_host *vha,
6127 fc_port_t *fcport, *tfcp, *del;
6129 unsigned long flags;
6132 fcport = qla2x00_alloc_fcport(vha, GFP_KERNEL);
6134 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06f,
6135 "qla_target(%d): Allocation of tmp FC port failed",
6140 fcport->loop_id = loop_id;
6142 rc = qla24xx_gpdb_wait(vha, fcport, 0);
6143 if (rc != QLA_SUCCESS) {
6144 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf070,
6145 "qla_target(%d): Failed to retrieve fcport "
6146 "information -- get_port_database() returned %x "
6147 "(loop_id=0x%04x)", vha->vp_idx, rc, loop_id);
6153 spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
6154 tfcp = qla2x00_find_fcport_by_wwpn(vha, fcport->port_name, 1);
6157 tfcp->d_id = fcport->d_id;
6158 tfcp->port_type = fcport->port_type;
6159 tfcp->supported_classes = fcport->supported_classes;
6160 tfcp->flags |= fcport->flags;
6161 tfcp->scan_state = QLA_FCPORT_FOUND;
6166 if (vha->hw->current_topology == ISP_CFG_F)
6167 fcport->flags |= FCF_FABRIC_DEVICE;
6169 list_add_tail(&fcport->list, &vha->vp_fcports);
6170 if (!IS_SW_RESV_ADDR(fcport->d_id))
6171 vha->fcport_count++;
6172 fcport->login_gen++;
6173 qla2x00_set_fcport_disc_state(fcport, DSC_LOGIN_COMPLETE);
6174 fcport->login_succ = 1;
6178 fcport->deleted = 0;
6179 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
6181 switch (vha->host->active_mode) {
6182 case MODE_INITIATOR:
6185 if (!IS_IIDMA_CAPABLE(vha->hw) || !vha->hw->flags.gpsc_supported) {
6186 qla24xx_sched_upd_fcport(fcport);
6188 ql_dbg(ql_dbg_disc, vha, 0x20ff,
6189 "%s %d %8phC post gpsc fcp_cnt %d\n",
6190 __func__, __LINE__, fcport->port_name, vha->fcport_count);
6191 qla24xx_post_gpsc_work(vha, fcport);
6201 qla2x00_free_fcport(del);
6206 /* Must be called under tgt_mutex */
6207 static struct fc_port *qlt_make_local_sess(struct scsi_qla_host *vha,
6210 struct fc_port *sess = NULL;
6211 fc_port_t *fcport = NULL;
6212 int rc, global_resets;
6213 uint16_t loop_id = 0;
6215 if (s_id.domain == 0xFF && s_id.area == 0xFC) {
6217 * This is Domain Controller, so it should be
6218 * OK to drop SCSI commands from it.
6220 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf042,
6221 "Unable to find initiator with S_ID %x:%x:%x",
6222 s_id.domain, s_id.area, s_id.al_pa);
6226 mutex_lock(&vha->vha_tgt.tgt_mutex);
6230 atomic_read(&vha->vha_tgt.qla_tgt->tgt_global_resets_count);
6232 rc = qla24xx_get_loop_id(vha, s_id, &loop_id);
6234 mutex_unlock(&vha->vha_tgt.tgt_mutex);
6236 ql_log(ql_log_info, vha, 0xf071,
6237 "qla_target(%d): Unable to find "
6238 "initiator with S_ID %x:%x:%x",
6239 vha->vp_idx, s_id.domain, s_id.area, s_id.al_pa);
6241 if (rc == -ENOENT) {
6242 qlt_port_logo_t logo;
6244 logo.id = be_to_port_id(s_id);
6246 qlt_send_first_logo(vha, &logo);
6252 fcport = qlt_get_port_database(vha, loop_id);
6254 mutex_unlock(&vha->vha_tgt.tgt_mutex);
6258 if (global_resets !=
6259 atomic_read(&vha->vha_tgt.qla_tgt->tgt_global_resets_count)) {
6260 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf043,
6261 "qla_target(%d): global reset during session discovery "
6262 "(counter was %d, new %d), retrying", vha->vp_idx,
6264 atomic_read(&vha->vha_tgt.
6265 qla_tgt->tgt_global_resets_count));
6269 sess = qlt_create_sess(vha, fcport, true);
6271 mutex_unlock(&vha->vha_tgt.tgt_mutex);
6276 static void qlt_abort_work(struct qla_tgt *tgt,
6277 struct qla_tgt_sess_work_param *prm)
6279 struct scsi_qla_host *vha = tgt->vha;
6280 struct qla_hw_data *ha = vha->hw;
6281 struct fc_port *sess = NULL;
6282 unsigned long flags = 0, flags2 = 0;
6286 spin_lock_irqsave(&ha->tgt.sess_lock, flags2);
6291 s_id = le_id_to_be(prm->abts.fcp_hdr_le.s_id);
6293 sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha, s_id);
6295 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags2);
6297 sess = qlt_make_local_sess(vha, s_id);
6298 /* sess has got an extra creation ref */
6300 spin_lock_irqsave(&ha->tgt.sess_lock, flags2);
6304 if (sess->deleted) {
6309 if (!kref_get_unless_zero(&sess->sess_kref)) {
6310 ql_dbg(ql_dbg_tgt_tmr, vha, 0xf01c,
6311 "%s: kref_get fail %8phC \n",
6312 __func__, sess->port_name);
6318 rc = __qlt_24xx_handle_abts(vha, &prm->abts, sess);
6319 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags2);
6321 ha->tgt.tgt_ops->put_sess(sess);
6328 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags2);
6331 spin_lock_irqsave(&ha->hardware_lock, flags);
6332 qlt_24xx_send_abts_resp(ha->base_qpair, &prm->abts,
6333 FCP_TMF_REJECTED, false);
6334 spin_unlock_irqrestore(&ha->hardware_lock, flags);
6337 static void qlt_tmr_work(struct qla_tgt *tgt,
6338 struct qla_tgt_sess_work_param *prm)
6340 struct atio_from_isp *a = &prm->tm_iocb2;
6341 struct scsi_qla_host *vha = tgt->vha;
6342 struct qla_hw_data *ha = vha->hw;
6343 struct fc_port *sess;
6344 unsigned long flags;
6351 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
6356 s_id = prm->tm_iocb2.u.isp24.fcp_hdr.s_id;
6357 sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha, s_id);
6359 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
6361 sess = qlt_make_local_sess(vha, s_id);
6362 /* sess has got an extra creation ref */
6364 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
6368 if (sess->deleted) {
6372 if (!kref_get_unless_zero(&sess->sess_kref)) {
6373 ql_dbg(ql_dbg_tgt_tmr, vha, 0xf020,
6374 "%s: kref_get fail %8phC\n",
6375 __func__, sess->port_name);
6381 fn = a->u.isp24.fcp_cmnd.task_mgmt_flags;
6383 scsilun_to_int((struct scsi_lun *)&a->u.isp24.fcp_cmnd.lun);
6385 rc = qlt_issue_task_mgmt(sess, unpacked_lun, fn, iocb, 0);
6386 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
6388 ha->tgt.tgt_ops->put_sess(sess);
6395 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
6397 qlt_send_term_exchange(ha->base_qpair, NULL, &prm->tm_iocb2, 1, 0);
6400 static void qlt_sess_work_fn(struct work_struct *work)
6402 struct qla_tgt *tgt = container_of(work, struct qla_tgt, sess_work);
6403 struct scsi_qla_host *vha = tgt->vha;
6404 unsigned long flags;
6406 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf000, "Sess work (tgt %p)", tgt);
6408 spin_lock_irqsave(&tgt->sess_work_lock, flags);
6409 while (!list_empty(&tgt->sess_works_list)) {
6410 struct qla_tgt_sess_work_param *prm = list_entry(
6411 tgt->sess_works_list.next, typeof(*prm),
6412 sess_works_list_entry);
6415 * This work can be scheduled on several CPUs at time, so we
6416 * must delete the entry to eliminate double processing
6418 list_del(&prm->sess_works_list_entry);
6420 spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
6422 switch (prm->type) {
6423 case QLA_TGT_SESS_WORK_ABORT:
6424 qlt_abort_work(tgt, prm);
6426 case QLA_TGT_SESS_WORK_TM:
6427 qlt_tmr_work(tgt, prm);
6434 spin_lock_irqsave(&tgt->sess_work_lock, flags);
6438 spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
6441 /* Must be called under tgt_host_action_mutex */
6442 int qlt_add_target(struct qla_hw_data *ha, struct scsi_qla_host *base_vha)
6444 struct qla_tgt *tgt;
6446 struct qla_qpair_hint *h;
6448 if (!QLA_TGT_MODE_ENABLED())
6451 if (!IS_TGT_MODE_CAPABLE(ha)) {
6452 ql_log(ql_log_warn, base_vha, 0xe070,
6453 "This adapter does not support target mode.\n");
6457 ql_dbg(ql_dbg_tgt, base_vha, 0xe03b,
6458 "Registering target for host %ld(%p).\n", base_vha->host_no, ha);
6460 BUG_ON(base_vha->vha_tgt.qla_tgt != NULL);
6462 tgt = kzalloc(sizeof(struct qla_tgt), GFP_KERNEL);
6464 ql_dbg(ql_dbg_tgt, base_vha, 0xe066,
6465 "Unable to allocate struct qla_tgt\n");
6469 tgt->qphints = kcalloc(ha->max_qpairs + 1,
6470 sizeof(struct qla_qpair_hint),
6472 if (!tgt->qphints) {
6474 ql_log(ql_log_warn, base_vha, 0x0197,
6475 "Unable to allocate qpair hints.\n");
6479 if (!(base_vha->host->hostt->supported_mode & MODE_TARGET))
6480 base_vha->host->hostt->supported_mode |= MODE_TARGET;
6482 rc = btree_init64(&tgt->lun_qpair_map);
6484 kfree(tgt->qphints);
6486 ql_log(ql_log_info, base_vha, 0x0198,
6487 "Unable to initialize lun_qpair_map btree\n");
6490 h = &tgt->qphints[0];
6491 h->qpair = ha->base_qpair;
6492 INIT_LIST_HEAD(&h->hint_elem);
6493 h->cpuid = ha->base_qpair->cpuid;
6494 list_add_tail(&h->hint_elem, &ha->base_qpair->hints_list);
6496 for (i = 0; i < ha->max_qpairs; i++) {
6497 unsigned long flags;
6499 struct qla_qpair *qpair = ha->queue_pair_map[i];
6501 h = &tgt->qphints[i + 1];
6502 INIT_LIST_HEAD(&h->hint_elem);
6505 spin_lock_irqsave(qpair->qp_lock_ptr, flags);
6506 list_add_tail(&h->hint_elem, &qpair->hints_list);
6507 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
6508 h->cpuid = qpair->cpuid;
6513 tgt->vha = base_vha;
6514 init_waitqueue_head(&tgt->waitQ);
6515 INIT_LIST_HEAD(&tgt->del_sess_list);
6516 spin_lock_init(&tgt->sess_work_lock);
6517 INIT_WORK(&tgt->sess_work, qlt_sess_work_fn);
6518 INIT_LIST_HEAD(&tgt->sess_works_list);
6519 atomic_set(&tgt->tgt_global_resets_count, 0);
6521 base_vha->vha_tgt.qla_tgt = tgt;
6523 ql_dbg(ql_dbg_tgt, base_vha, 0xe067,
6524 "qla_target(%d): using 64 Bit PCI addressing",
6527 tgt->sg_tablesize = QLA_TGT_MAX_SG_24XX(base_vha->req->length - 3);
6529 mutex_lock(&qla_tgt_mutex);
6530 list_add_tail(&tgt->tgt_list_entry, &qla_tgt_glist);
6531 mutex_unlock(&qla_tgt_mutex);
6533 if (ha->tgt.tgt_ops && ha->tgt.tgt_ops->add_target)
6534 ha->tgt.tgt_ops->add_target(base_vha);
6539 /* Must be called under tgt_host_action_mutex */
6540 int qlt_remove_target(struct qla_hw_data *ha, struct scsi_qla_host *vha)
6542 if (!vha->vha_tgt.qla_tgt)
6545 if (vha->fc_vport) {
6546 qlt_release(vha->vha_tgt.qla_tgt);
6550 /* free left over qfull cmds */
6551 qlt_init_term_exchange(vha);
6553 ql_dbg(ql_dbg_tgt, vha, 0xe03c, "Unregistering target for host %ld(%p)",
6555 qlt_release(vha->vha_tgt.qla_tgt);
6560 void qla_remove_hostmap(struct qla_hw_data *ha)
6562 struct scsi_qla_host *node;
6565 btree_for_each_safe32(&ha->host_map, key, node)
6566 btree_remove32(&ha->host_map, key);
6568 btree_destroy32(&ha->host_map);
6571 static void qlt_lport_dump(struct scsi_qla_host *vha, u64 wwpn,
6574 pr_debug("qla2xxx HW vha->node_name: %8phC\n", vha->node_name);
6575 pr_debug("qla2xxx HW vha->port_name: %8phC\n", vha->port_name);
6576 put_unaligned_be64(wwpn, b);
6577 pr_debug("qla2xxx passed configfs WWPN: %8phC\n", b);
6581 * qlt_lport_register - register lport with external module
6583 * @target_lport_ptr: pointer for tcm_qla2xxx specific lport data
6584 * @phys_wwpn: physical port WWPN
6585 * @npiv_wwpn: NPIV WWPN
6586 * @npiv_wwnn: NPIV WWNN
6587 * @callback: lport initialization callback for tcm_qla2xxx code
6589 int qlt_lport_register(void *target_lport_ptr, u64 phys_wwpn,
6590 u64 npiv_wwpn, u64 npiv_wwnn,
6591 int (*callback)(struct scsi_qla_host *, void *, u64, u64))
6593 struct qla_tgt *tgt;
6594 struct scsi_qla_host *vha;
6595 struct qla_hw_data *ha;
6596 struct Scsi_Host *host;
6597 unsigned long flags;
6601 mutex_lock(&qla_tgt_mutex);
6602 list_for_each_entry(tgt, &qla_tgt_glist, tgt_list_entry) {
6610 if (!(host->hostt->supported_mode & MODE_TARGET))
6613 if (vha->qlini_mode == QLA2XXX_INI_MODE_ENABLED)
6616 spin_lock_irqsave(&ha->hardware_lock, flags);
6617 if ((!npiv_wwpn || !npiv_wwnn) && host->active_mode & MODE_TARGET) {
6618 pr_debug("MODE_TARGET already active on qla2xxx(%d)\n",
6620 spin_unlock_irqrestore(&ha->hardware_lock, flags);
6623 if (tgt->tgt_stop) {
6624 pr_debug("MODE_TARGET in shutdown on qla2xxx(%d)\n",
6626 spin_unlock_irqrestore(&ha->hardware_lock, flags);
6629 spin_unlock_irqrestore(&ha->hardware_lock, flags);
6631 if (!scsi_host_get(host)) {
6632 ql_dbg(ql_dbg_tgt, vha, 0xe068,
6633 "Unable to scsi_host_get() for"
6634 " qla2xxx scsi_host\n");
6637 qlt_lport_dump(vha, phys_wwpn, b);
6639 if (memcmp(vha->port_name, b, WWN_SIZE)) {
6640 scsi_host_put(host);
6643 rc = (*callback)(vha, target_lport_ptr, npiv_wwpn, npiv_wwnn);
6645 scsi_host_put(host);
6647 mutex_unlock(&qla_tgt_mutex);
6650 mutex_unlock(&qla_tgt_mutex);
6654 EXPORT_SYMBOL(qlt_lport_register);
6657 * qlt_lport_deregister - Degister lport
6659 * @vha: Registered scsi_qla_host pointer
6661 void qlt_lport_deregister(struct scsi_qla_host *vha)
6663 struct qla_hw_data *ha = vha->hw;
6664 struct Scsi_Host *sh = vha->host;
6666 * Clear the target_lport_ptr qla_target_template pointer in qla_hw_data
6668 vha->vha_tgt.target_lport_ptr = NULL;
6669 ha->tgt.tgt_ops = NULL;
6671 * Release the Scsi_Host reference for the underlying qla2xxx host
6675 EXPORT_SYMBOL(qlt_lport_deregister);
6677 /* Must be called under HW lock */
6678 void qlt_set_mode(struct scsi_qla_host *vha)
6680 switch (vha->qlini_mode) {
6681 case QLA2XXX_INI_MODE_DISABLED:
6682 case QLA2XXX_INI_MODE_EXCLUSIVE:
6683 vha->host->active_mode = MODE_TARGET;
6685 case QLA2XXX_INI_MODE_ENABLED:
6686 vha->host->active_mode = MODE_INITIATOR;
6688 case QLA2XXX_INI_MODE_DUAL:
6689 vha->host->active_mode = MODE_DUAL;
6696 /* Must be called under HW lock */
6697 static void qlt_clear_mode(struct scsi_qla_host *vha)
6699 switch (vha->qlini_mode) {
6700 case QLA2XXX_INI_MODE_DISABLED:
6701 vha->host->active_mode = MODE_UNKNOWN;
6703 case QLA2XXX_INI_MODE_EXCLUSIVE:
6704 vha->host->active_mode = MODE_INITIATOR;
6706 case QLA2XXX_INI_MODE_ENABLED:
6707 case QLA2XXX_INI_MODE_DUAL:
6708 vha->host->active_mode = MODE_INITIATOR;
6716 * qla_tgt_enable_vha - NO LOCK HELD
6718 * host_reset, bring up w/ Target Mode Enabled
6721 qlt_enable_vha(struct scsi_qla_host *vha)
6723 struct qla_hw_data *ha = vha->hw;
6724 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
6725 unsigned long flags;
6726 scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
6729 ql_dbg(ql_dbg_tgt, vha, 0xe069,
6730 "Unable to locate qla_tgt pointer from"
6731 " struct qla_hw_data\n");
6735 if (vha->qlini_mode == QLA2XXX_INI_MODE_ENABLED)
6738 if (ha->tgt.num_act_qpairs > ha->max_qpairs)
6739 ha->tgt.num_act_qpairs = ha->max_qpairs;
6740 spin_lock_irqsave(&ha->hardware_lock, flags);
6741 tgt->tgt_stopped = 0;
6743 spin_unlock_irqrestore(&ha->hardware_lock, flags);
6745 mutex_lock(&ha->optrom_mutex);
6746 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf021,
6749 qla24xx_disable_vp(vha);
6750 qla24xx_enable_vp(vha);
6752 set_bit(ISP_ABORT_NEEDED, &base_vha->dpc_flags);
6753 qla2xxx_wake_dpc(base_vha);
6754 WARN_ON_ONCE(qla2x00_wait_for_hba_online(base_vha) !=
6757 mutex_unlock(&ha->optrom_mutex);
6759 EXPORT_SYMBOL(qlt_enable_vha);
6762 * qla_tgt_disable_vha - NO LOCK HELD
6764 * Disable Target Mode and reset the adapter
6766 static void qlt_disable_vha(struct scsi_qla_host *vha)
6768 struct qla_hw_data *ha = vha->hw;
6769 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
6770 unsigned long flags;
6773 ql_dbg(ql_dbg_tgt, vha, 0xe06a,
6774 "Unable to locate qla_tgt pointer from"
6775 " struct qla_hw_data\n");
6780 spin_lock_irqsave(&ha->hardware_lock, flags);
6781 qlt_clear_mode(vha);
6782 spin_unlock_irqrestore(&ha->hardware_lock, flags);
6784 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
6785 qla2xxx_wake_dpc(vha);
6788 * We are expecting the offline state.
6789 * QLA_FUNCTION_FAILED means that adapter is offline.
6791 if (qla2x00_wait_for_hba_online(vha) != QLA_SUCCESS)
6792 ql_dbg(ql_dbg_tgt, vha, 0xe081,
6793 "adapter is offline\n");
6797 * Called from qla_init.c:qla24xx_vport_create() contex to setup
6798 * the target mode specific struct scsi_qla_host and struct qla_hw_data
6802 qlt_vport_create(struct scsi_qla_host *vha, struct qla_hw_data *ha)
6804 vha->vha_tgt.qla_tgt = NULL;
6806 mutex_init(&vha->vha_tgt.tgt_mutex);
6807 mutex_init(&vha->vha_tgt.tgt_host_action_mutex);
6809 qlt_clear_mode(vha);
6812 * NOTE: Currently the value is kept the same for <24xx and
6813 * >=24xx ISPs. If it is necessary to change it,
6814 * the check should be added for specific ISPs,
6815 * assigning the value appropriately.
6817 ha->tgt.atio_q_length = ATIO_ENTRY_CNT_24XX;
6819 qlt_add_target(ha, vha);
6823 qlt_rff_id(struct scsi_qla_host *vha)
6827 * FC-4 Feature bit 0 indicates target functionality to the name server.
6829 if (qla_tgt_mode_enabled(vha)) {
6830 fc4_feature = BIT_0;
6831 } else if (qla_ini_mode_enabled(vha)) {
6832 fc4_feature = BIT_1;
6833 } else if (qla_dual_mode_enabled(vha))
6834 fc4_feature = BIT_0 | BIT_1;
6840 * qlt_init_atio_q_entries() - Initializes ATIO queue entries.
6843 * Beginning of ATIO ring has initialization control block already built
6844 * by nvram config routine.
6846 * Returns 0 on success.
6849 qlt_init_atio_q_entries(struct scsi_qla_host *vha)
6851 struct qla_hw_data *ha = vha->hw;
6853 struct atio_from_isp *pkt = (struct atio_from_isp *)ha->tgt.atio_ring;
6855 if (qla_ini_mode_enabled(vha))
6858 for (cnt = 0; cnt < ha->tgt.atio_q_length; cnt++) {
6859 pkt->u.raw.signature = cpu_to_le32(ATIO_PROCESSED);
6866 * qlt_24xx_process_atio_queue() - Process ATIO queue entries.
6867 * @ha: SCSI driver HA context
6870 qlt_24xx_process_atio_queue(struct scsi_qla_host *vha, uint8_t ha_locked)
6872 struct qla_hw_data *ha = vha->hw;
6873 struct atio_from_isp *pkt;
6876 if (!ha->flags.fw_started)
6879 while ((ha->tgt.atio_ring_ptr->signature != ATIO_PROCESSED) ||
6880 fcpcmd_is_corrupted(ha->tgt.atio_ring_ptr)) {
6881 pkt = (struct atio_from_isp *)ha->tgt.atio_ring_ptr;
6882 cnt = pkt->u.raw.entry_count;
6884 if (unlikely(fcpcmd_is_corrupted(ha->tgt.atio_ring_ptr))) {
6886 * This packet is corrupted. The header + payload
6887 * can not be trusted. There is no point in passing
6890 ql_log(ql_log_warn, vha, 0xd03c,
6891 "corrupted fcp frame SID[%3phN] OXID[%04x] EXCG[%x] %64phN\n",
6892 &pkt->u.isp24.fcp_hdr.s_id,
6893 be16_to_cpu(pkt->u.isp24.fcp_hdr.ox_id),
6894 pkt->u.isp24.exchange_addr, pkt);
6896 adjust_corrupted_atio(pkt);
6897 qlt_send_term_exchange(ha->base_qpair, NULL, pkt,
6900 qlt_24xx_atio_pkt_all_vps(vha,
6901 (struct atio_from_isp *)pkt, ha_locked);
6904 for (i = 0; i < cnt; i++) {
6905 ha->tgt.atio_ring_index++;
6906 if (ha->tgt.atio_ring_index == ha->tgt.atio_q_length) {
6907 ha->tgt.atio_ring_index = 0;
6908 ha->tgt.atio_ring_ptr = ha->tgt.atio_ring;
6910 ha->tgt.atio_ring_ptr++;
6912 pkt->u.raw.signature = cpu_to_le32(ATIO_PROCESSED);
6913 pkt = (struct atio_from_isp *)ha->tgt.atio_ring_ptr;
6918 /* Adjust ring index */
6919 wrt_reg_dword(ISP_ATIO_Q_OUT(vha), ha->tgt.atio_ring_index);
6923 qlt_24xx_config_rings(struct scsi_qla_host *vha)
6925 struct qla_hw_data *ha = vha->hw;
6926 struct qla_msix_entry *msix = &ha->msix_entries[2];
6927 struct init_cb_24xx *icb = (struct init_cb_24xx *)ha->init_cb;
6929 if (!QLA_TGT_MODE_ENABLED())
6932 wrt_reg_dword(ISP_ATIO_Q_IN(vha), 0);
6933 wrt_reg_dword(ISP_ATIO_Q_OUT(vha), 0);
6934 rd_reg_dword(ISP_ATIO_Q_OUT(vha));
6936 if (ha->flags.msix_enabled) {
6937 if (IS_QLA83XX(ha) || IS_QLA27XX(ha) || IS_QLA28XX(ha)) {
6938 if (IS_QLA2071(ha)) {
6939 /* 4 ports Baker: Enable Interrupt Handshake */
6941 icb->firmware_options_2 |= cpu_to_le32(BIT_26);
6943 icb->msix_atio = cpu_to_le16(msix->entry);
6944 icb->firmware_options_2 &= cpu_to_le32(~BIT_26);
6946 ql_dbg(ql_dbg_init, vha, 0xf072,
6947 "Registering ICB vector 0x%x for atio que.\n",
6952 if (IS_QLA83XX(ha) || IS_QLA27XX(ha) || IS_QLA28XX(ha)) {
6954 icb->firmware_options_2 |= cpu_to_le32(BIT_26);
6955 ql_dbg(ql_dbg_init, vha, 0xf072,
6956 "%s: Use INTx for ATIOQ.\n", __func__);
6962 qlt_24xx_config_nvram_stage1(struct scsi_qla_host *vha, struct nvram_24xx *nv)
6964 struct qla_hw_data *ha = vha->hw;
6967 if (!QLA_TGT_MODE_ENABLED())
6970 if (qla_tgt_mode_enabled(vha) || qla_dual_mode_enabled(vha)) {
6971 if (!ha->tgt.saved_set) {
6972 /* We save only once */
6973 ha->tgt.saved_exchange_count = nv->exchange_count;
6974 ha->tgt.saved_firmware_options_1 =
6975 nv->firmware_options_1;
6976 ha->tgt.saved_firmware_options_2 =
6977 nv->firmware_options_2;
6978 ha->tgt.saved_firmware_options_3 =
6979 nv->firmware_options_3;
6980 ha->tgt.saved_set = 1;
6983 if (qla_tgt_mode_enabled(vha))
6984 nv->exchange_count = cpu_to_le16(0xFFFF);
6986 nv->exchange_count = cpu_to_le16(vha->ql2xexchoffld);
6988 /* Enable target mode */
6989 nv->firmware_options_1 |= cpu_to_le32(BIT_4);
6991 /* Disable ini mode, if requested */
6992 if (qla_tgt_mode_enabled(vha))
6993 nv->firmware_options_1 |= cpu_to_le32(BIT_5);
6995 /* Disable Full Login after LIP */
6996 nv->firmware_options_1 &= cpu_to_le32(~BIT_13);
6997 /* Enable initial LIP */
6998 nv->firmware_options_1 &= cpu_to_le32(~BIT_9);
6999 if (ql2xtgt_tape_enable)
7000 /* Enable FC Tape support */
7001 nv->firmware_options_2 |= cpu_to_le32(BIT_12);
7003 /* Disable FC Tape support */
7004 nv->firmware_options_2 &= cpu_to_le32(~BIT_12);
7006 /* Disable Full Login after LIP */
7007 nv->host_p &= cpu_to_le32(~BIT_10);
7010 * clear BIT 15 explicitly as we have seen at least
7011 * a couple of instances where this was set and this
7012 * was causing the firmware to not be initialized.
7014 nv->firmware_options_1 &= cpu_to_le32(~BIT_15);
7015 /* Enable target PRLI control */
7016 nv->firmware_options_2 |= cpu_to_le32(BIT_14);
7018 if (IS_QLA25XX(ha)) {
7019 /* Change Loop-prefer to Pt-Pt */
7020 tmp = ~(BIT_4|BIT_5|BIT_6);
7021 nv->firmware_options_2 &= cpu_to_le32(tmp);
7023 nv->firmware_options_2 |= cpu_to_le32(tmp);
7026 if (ha->tgt.saved_set) {
7027 nv->exchange_count = ha->tgt.saved_exchange_count;
7028 nv->firmware_options_1 =
7029 ha->tgt.saved_firmware_options_1;
7030 nv->firmware_options_2 =
7031 ha->tgt.saved_firmware_options_2;
7032 nv->firmware_options_3 =
7033 ha->tgt.saved_firmware_options_3;
7038 if (ha->base_qpair->enable_class_2) {
7039 if (vha->flags.init_done)
7040 fc_host_supported_classes(vha->host) =
7041 FC_COS_CLASS2 | FC_COS_CLASS3;
7043 nv->firmware_options_2 |= cpu_to_le32(BIT_8);
7045 if (vha->flags.init_done)
7046 fc_host_supported_classes(vha->host) = FC_COS_CLASS3;
7048 nv->firmware_options_2 &= ~cpu_to_le32(BIT_8);
7053 qlt_24xx_config_nvram_stage2(struct scsi_qla_host *vha,
7054 struct init_cb_24xx *icb)
7056 struct qla_hw_data *ha = vha->hw;
7058 if (!QLA_TGT_MODE_ENABLED())
7061 if (ha->tgt.node_name_set) {
7062 memcpy(icb->node_name, ha->tgt.tgt_node_name, WWN_SIZE);
7063 icb->firmware_options_1 |= cpu_to_le32(BIT_14);
7068 qlt_81xx_config_nvram_stage1(struct scsi_qla_host *vha, struct nvram_81xx *nv)
7070 struct qla_hw_data *ha = vha->hw;
7073 if (!QLA_TGT_MODE_ENABLED())
7076 if (qla_tgt_mode_enabled(vha) || qla_dual_mode_enabled(vha)) {
7077 if (!ha->tgt.saved_set) {
7078 /* We save only once */
7079 ha->tgt.saved_exchange_count = nv->exchange_count;
7080 ha->tgt.saved_firmware_options_1 =
7081 nv->firmware_options_1;
7082 ha->tgt.saved_firmware_options_2 =
7083 nv->firmware_options_2;
7084 ha->tgt.saved_firmware_options_3 =
7085 nv->firmware_options_3;
7086 ha->tgt.saved_set = 1;
7089 if (qla_tgt_mode_enabled(vha))
7090 nv->exchange_count = cpu_to_le16(0xFFFF);
7092 nv->exchange_count = cpu_to_le16(vha->ql2xexchoffld);
7094 /* Enable target mode */
7095 nv->firmware_options_1 |= cpu_to_le32(BIT_4);
7097 /* Disable ini mode, if requested */
7098 if (qla_tgt_mode_enabled(vha))
7099 nv->firmware_options_1 |= cpu_to_le32(BIT_5);
7100 /* Disable Full Login after LIP */
7101 nv->firmware_options_1 &= cpu_to_le32(~BIT_13);
7102 /* Enable initial LIP */
7103 nv->firmware_options_1 &= cpu_to_le32(~BIT_9);
7105 * clear BIT 15 explicitly as we have seen at
7106 * least a couple of instances where this was set
7107 * and this was causing the firmware to not be
7110 nv->firmware_options_1 &= cpu_to_le32(~BIT_15);
7111 if (ql2xtgt_tape_enable)
7112 /* Enable FC tape support */
7113 nv->firmware_options_2 |= cpu_to_le32(BIT_12);
7115 /* Disable FC tape support */
7116 nv->firmware_options_2 &= cpu_to_le32(~BIT_12);
7118 /* Disable Full Login after LIP */
7119 nv->host_p &= cpu_to_le32(~BIT_10);
7120 /* Enable target PRLI control */
7121 nv->firmware_options_2 |= cpu_to_le32(BIT_14);
7123 /* Change Loop-prefer to Pt-Pt */
7124 tmp = ~(BIT_4|BIT_5|BIT_6);
7125 nv->firmware_options_2 &= cpu_to_le32(tmp);
7127 nv->firmware_options_2 |= cpu_to_le32(tmp);
7129 if (ha->tgt.saved_set) {
7130 nv->exchange_count = ha->tgt.saved_exchange_count;
7131 nv->firmware_options_1 =
7132 ha->tgt.saved_firmware_options_1;
7133 nv->firmware_options_2 =
7134 ha->tgt.saved_firmware_options_2;
7135 nv->firmware_options_3 =
7136 ha->tgt.saved_firmware_options_3;
7141 if (ha->base_qpair->enable_class_2) {
7142 if (vha->flags.init_done)
7143 fc_host_supported_classes(vha->host) =
7144 FC_COS_CLASS2 | FC_COS_CLASS3;
7146 nv->firmware_options_2 |= cpu_to_le32(BIT_8);
7148 if (vha->flags.init_done)
7149 fc_host_supported_classes(vha->host) = FC_COS_CLASS3;
7151 nv->firmware_options_2 &= ~cpu_to_le32(BIT_8);
7156 qlt_81xx_config_nvram_stage2(struct scsi_qla_host *vha,
7157 struct init_cb_81xx *icb)
7159 struct qla_hw_data *ha = vha->hw;
7161 if (!QLA_TGT_MODE_ENABLED())
7164 if (ha->tgt.node_name_set) {
7165 memcpy(icb->node_name, ha->tgt.tgt_node_name, WWN_SIZE);
7166 icb->firmware_options_1 |= cpu_to_le32(BIT_14);
7171 qlt_83xx_iospace_config(struct qla_hw_data *ha)
7173 if (!QLA_TGT_MODE_ENABLED())
7176 ha->msix_count += 1; /* For ATIO Q */
7181 qlt_modify_vp_config(struct scsi_qla_host *vha,
7182 struct vp_config_entry_24xx *vpmod)
7184 /* enable target mode. Bit5 = 1 => disable */
7185 if (qla_tgt_mode_enabled(vha) || qla_dual_mode_enabled(vha))
7186 vpmod->options_idx1 &= ~BIT_5;
7188 /* Disable ini mode, if requested. bit4 = 1 => disable */
7189 if (qla_tgt_mode_enabled(vha))
7190 vpmod->options_idx1 &= ~BIT_4;
7194 qlt_probe_one_stage1(struct scsi_qla_host *base_vha, struct qla_hw_data *ha)
7196 mutex_init(&base_vha->vha_tgt.tgt_mutex);
7197 if (!QLA_TGT_MODE_ENABLED())
7200 if (ha->mqenable || IS_QLA83XX(ha) || IS_QLA27XX(ha) || IS_QLA28XX(ha)) {
7201 ISP_ATIO_Q_IN(base_vha) = &ha->mqiobase->isp25mq.atio_q_in;
7202 ISP_ATIO_Q_OUT(base_vha) = &ha->mqiobase->isp25mq.atio_q_out;
7204 ISP_ATIO_Q_IN(base_vha) = &ha->iobase->isp24.atio_q_in;
7205 ISP_ATIO_Q_OUT(base_vha) = &ha->iobase->isp24.atio_q_out;
7208 mutex_init(&base_vha->vha_tgt.tgt_host_action_mutex);
7210 INIT_LIST_HEAD(&base_vha->unknown_atio_list);
7211 INIT_DELAYED_WORK(&base_vha->unknown_atio_work,
7212 qlt_unknown_atio_work_fn);
7214 qlt_clear_mode(base_vha);
7216 qlt_update_vp_map(base_vha, SET_VP_IDX);
7220 qla83xx_msix_atio_q(int irq, void *dev_id)
7222 struct rsp_que *rsp;
7223 scsi_qla_host_t *vha;
7224 struct qla_hw_data *ha;
7225 unsigned long flags;
7227 rsp = (struct rsp_que *) dev_id;
7229 vha = pci_get_drvdata(ha->pdev);
7231 spin_lock_irqsave(&ha->tgt.atio_lock, flags);
7233 qlt_24xx_process_atio_queue(vha, 0);
7235 spin_unlock_irqrestore(&ha->tgt.atio_lock, flags);
7241 qlt_handle_abts_recv_work(struct work_struct *work)
7243 struct qla_tgt_sess_op *op = container_of(work,
7244 struct qla_tgt_sess_op, work);
7245 scsi_qla_host_t *vha = op->vha;
7246 struct qla_hw_data *ha = vha->hw;
7247 unsigned long flags;
7249 if (qla2x00_reset_active(vha) ||
7250 (op->chip_reset != ha->base_qpair->chip_reset))
7253 spin_lock_irqsave(&ha->tgt.atio_lock, flags);
7254 qlt_24xx_process_atio_queue(vha, 0);
7255 spin_unlock_irqrestore(&ha->tgt.atio_lock, flags);
7257 spin_lock_irqsave(&ha->hardware_lock, flags);
7258 qlt_response_pkt_all_vps(vha, op->rsp, (response_t *)&op->atio);
7259 spin_unlock_irqrestore(&ha->hardware_lock, flags);
7265 qlt_handle_abts_recv(struct scsi_qla_host *vha, struct rsp_que *rsp,
7268 struct qla_tgt_sess_op *op;
7270 op = kzalloc(sizeof(*op), GFP_ATOMIC);
7273 /* do not reach for ATIO queue here. This is best effort err
7274 * recovery at this point.
7276 qlt_response_pkt_all_vps(vha, rsp, pkt);
7280 memcpy(&op->atio, pkt, sizeof(*pkt));
7282 op->chip_reset = vha->hw->base_qpair->chip_reset;
7284 INIT_WORK(&op->work, qlt_handle_abts_recv_work);
7285 queue_work(qla_tgt_wq, &op->work);
7290 qlt_mem_alloc(struct qla_hw_data *ha)
7292 if (!QLA_TGT_MODE_ENABLED())
7295 ha->tgt.tgt_vp_map = kcalloc(MAX_MULTI_ID_FABRIC,
7296 sizeof(struct qla_tgt_vp_map),
7298 if (!ha->tgt.tgt_vp_map)
7301 ha->tgt.atio_ring = dma_alloc_coherent(&ha->pdev->dev,
7302 (ha->tgt.atio_q_length + 1) * sizeof(struct atio_from_isp),
7303 &ha->tgt.atio_dma, GFP_KERNEL);
7304 if (!ha->tgt.atio_ring) {
7305 kfree(ha->tgt.tgt_vp_map);
7312 qlt_mem_free(struct qla_hw_data *ha)
7314 if (!QLA_TGT_MODE_ENABLED())
7317 if (ha->tgt.atio_ring) {
7318 dma_free_coherent(&ha->pdev->dev, (ha->tgt.atio_q_length + 1) *
7319 sizeof(struct atio_from_isp), ha->tgt.atio_ring,
7322 ha->tgt.atio_ring = NULL;
7323 ha->tgt.atio_dma = 0;
7324 kfree(ha->tgt.tgt_vp_map);
7325 ha->tgt.tgt_vp_map = NULL;
7328 /* vport_slock to be held by the caller */
7330 qlt_update_vp_map(struct scsi_qla_host *vha, int cmd)
7336 key = vha->d_id.b24;
7340 if (!QLA_TGT_MODE_ENABLED())
7342 vha->hw->tgt.tgt_vp_map[vha->vp_idx].vha = vha;
7345 slot = btree_lookup32(&vha->hw->host_map, key);
7347 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf018,
7348 "Save vha in host_map %p %06x\n", vha, key);
7349 rc = btree_insert32(&vha->hw->host_map,
7350 key, vha, GFP_ATOMIC);
7352 ql_log(ql_log_info, vha, 0xd03e,
7353 "Unable to insert s_id into host_map: %06x\n",
7357 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf019,
7358 "replace existing vha in host_map %p %06x\n", vha, key);
7359 btree_update32(&vha->hw->host_map, key, vha);
7362 if (!QLA_TGT_MODE_ENABLED())
7364 vha->hw->tgt.tgt_vp_map[vha->vp_idx].vha = NULL;
7367 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01a,
7368 "clear vha in host_map %p %06x\n", vha, key);
7369 slot = btree_lookup32(&vha->hw->host_map, key);
7371 btree_remove32(&vha->hw->host_map, key);
7377 void qlt_update_host_map(struct scsi_qla_host *vha, port_id_t id)
7380 if (!vha->d_id.b24) {
7382 qlt_update_vp_map(vha, SET_AL_PA);
7383 } else if (vha->d_id.b24 != id.b24) {
7384 qlt_update_vp_map(vha, RESET_AL_PA);
7386 qlt_update_vp_map(vha, SET_AL_PA);
7390 static int __init qlt_parse_ini_mode(void)
7392 if (strcasecmp(qlini_mode, QLA2XXX_INI_MODE_STR_EXCLUSIVE) == 0)
7393 ql2x_ini_mode = QLA2XXX_INI_MODE_EXCLUSIVE;
7394 else if (strcasecmp(qlini_mode, QLA2XXX_INI_MODE_STR_DISABLED) == 0)
7395 ql2x_ini_mode = QLA2XXX_INI_MODE_DISABLED;
7396 else if (strcasecmp(qlini_mode, QLA2XXX_INI_MODE_STR_ENABLED) == 0)
7397 ql2x_ini_mode = QLA2XXX_INI_MODE_ENABLED;
7398 else if (strcasecmp(qlini_mode, QLA2XXX_INI_MODE_STR_DUAL) == 0)
7399 ql2x_ini_mode = QLA2XXX_INI_MODE_DUAL;
7406 int __init qlt_init(void)
7410 BUILD_BUG_ON(sizeof(struct ctio7_to_24xx) != 64);
7411 BUILD_BUG_ON(sizeof(struct ctio_to_2xxx) != 64);
7413 if (!qlt_parse_ini_mode()) {
7414 ql_log(ql_log_fatal, NULL, 0xe06b,
7415 "qlt_parse_ini_mode() failed\n");
7419 if (!QLA_TGT_MODE_ENABLED())
7422 qla_tgt_mgmt_cmd_cachep = kmem_cache_create("qla_tgt_mgmt_cmd_cachep",
7423 sizeof(struct qla_tgt_mgmt_cmd), __alignof__(struct
7424 qla_tgt_mgmt_cmd), 0, NULL);
7425 if (!qla_tgt_mgmt_cmd_cachep) {
7426 ql_log(ql_log_fatal, NULL, 0xd04b,
7427 "kmem_cache_create for qla_tgt_mgmt_cmd_cachep failed\n");
7431 qla_tgt_plogi_cachep = kmem_cache_create("qla_tgt_plogi_cachep",
7432 sizeof(struct qlt_plogi_ack_t), __alignof__(struct qlt_plogi_ack_t),
7435 if (!qla_tgt_plogi_cachep) {
7436 ql_log(ql_log_fatal, NULL, 0xe06d,
7437 "kmem_cache_create for qla_tgt_plogi_cachep failed\n");
7439 goto out_mgmt_cmd_cachep;
7442 qla_tgt_mgmt_cmd_mempool = mempool_create(25, mempool_alloc_slab,
7443 mempool_free_slab, qla_tgt_mgmt_cmd_cachep);
7444 if (!qla_tgt_mgmt_cmd_mempool) {
7445 ql_log(ql_log_fatal, NULL, 0xe06e,
7446 "mempool_create for qla_tgt_mgmt_cmd_mempool failed\n");
7448 goto out_plogi_cachep;
7451 qla_tgt_wq = alloc_workqueue("qla_tgt_wq", 0, 0);
7453 ql_log(ql_log_fatal, NULL, 0xe06f,
7454 "alloc_workqueue for qla_tgt_wq failed\n");
7456 goto out_cmd_mempool;
7459 * Return 1 to signal that initiator-mode is being disabled
7461 return (ql2x_ini_mode == QLA2XXX_INI_MODE_DISABLED) ? 1 : 0;
7464 mempool_destroy(qla_tgt_mgmt_cmd_mempool);
7466 kmem_cache_destroy(qla_tgt_plogi_cachep);
7467 out_mgmt_cmd_cachep:
7468 kmem_cache_destroy(qla_tgt_mgmt_cmd_cachep);
7474 if (!QLA_TGT_MODE_ENABLED())
7477 destroy_workqueue(qla_tgt_wq);
7478 mempool_destroy(qla_tgt_mgmt_cmd_mempool);
7479 kmem_cache_destroy(qla_tgt_plogi_cachep);
7480 kmem_cache_destroy(qla_tgt_mgmt_cmd_cachep);