2 * qla_target.c SCSI LLD infrastructure for QLogic 22xx/23xx/24xx/25xx
4 * based on qla2x00t.c code:
6 * Copyright (C) 2004 - 2010 Vladislav Bolkhovitin <vst@vlnb.net>
7 * Copyright (C) 2004 - 2005 Leonid Stoljar
8 * Copyright (C) 2006 Nathaniel Clark <nate@misrule.us>
9 * Copyright (C) 2006 - 2010 ID7 Ltd.
11 * Forward port and refactoring to modern qla2xxx and target/configfs
13 * Copyright (C) 2010-2013 Nicholas A. Bellinger <nab@kernel.org>
15 * This program is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public License
17 * as published by the Free Software Foundation, version 2
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
26 #include <linux/module.h>
27 #include <linux/init.h>
28 #include <linux/types.h>
29 #include <linux/blkdev.h>
30 #include <linux/interrupt.h>
31 #include <linux/pci.h>
32 #include <linux/delay.h>
33 #include <linux/list.h>
34 #include <linux/workqueue.h>
35 #include <asm/unaligned.h>
36 #include <scsi/scsi.h>
37 #include <scsi/scsi_host.h>
38 #include <scsi/scsi_tcq.h>
39 #include <target/target_core_base.h>
40 #include <target/target_core_fabric.h>
43 #include "qla_target.h"
45 static char *qlini_mode = QLA2XXX_INI_MODE_STR_ENABLED;
46 module_param(qlini_mode, charp, S_IRUGO);
47 MODULE_PARM_DESC(qlini_mode,
48 "Determines when initiator mode will be enabled. Possible values: "
49 "\"exclusive\" - initiator mode will be enabled on load, "
50 "disabled on enabling target mode and then on disabling target mode "
52 "\"disabled\" - initiator mode will never be enabled; "
53 "\"enabled\" (default) - initiator mode will always stay enabled.");
55 int ql2x_ini_mode = QLA2XXX_INI_MODE_EXCLUSIVE;
58 * From scsi/fc/fc_fcp.h
60 enum fcp_resp_rsp_codes {
62 FCP_DATA_LEN_INVALID = 1,
63 FCP_CMND_FIELDS_INVALID = 2,
64 FCP_DATA_PARAM_MISMATCH = 3,
67 FCP_TMF_INVALID_LUN = 9,
71 * fc_pri_ta from scsi/fc/fc_fcp.h
73 #define FCP_PTA_SIMPLE 0 /* simple task attribute */
74 #define FCP_PTA_HEADQ 1 /* head of queue task attribute */
75 #define FCP_PTA_ORDERED 2 /* ordered task attribute */
76 #define FCP_PTA_ACA 4 /* auto. contingent allegiance */
77 #define FCP_PTA_MASK 7 /* mask for task attribute field */
78 #define FCP_PRI_SHIFT 3 /* priority field starts in bit 3 */
79 #define FCP_PRI_RESVD_MASK 0x80 /* reserved bits in priority field */
82 * This driver calls qla2x00_alloc_iocbs() and qla2x00_issue_marker(), which
83 * must be called under HW lock and could unlock/lock it inside.
84 * It isn't an issue, since in the current implementation on the time when
85 * those functions are called:
87 * - Either context is IRQ and only IRQ handler can modify HW data,
88 * including rings related fields,
90 * - Or access to target mode variables from struct qla_tgt doesn't
91 * cross those functions boundaries, except tgt_stop, which
92 * additionally protected by irq_cmd_count.
94 /* Predefs for callbacks handed to qla2xxx LLD */
95 static void qlt_24xx_atio_pkt(struct scsi_qla_host *ha,
96 struct atio_from_isp *pkt);
97 static void qlt_response_pkt(struct scsi_qla_host *ha, response_t *pkt);
98 static int qlt_issue_task_mgmt(struct qla_tgt_sess *sess, uint32_t lun,
99 int fn, void *iocb, int flags);
100 static void qlt_send_term_exchange(struct scsi_qla_host *ha, struct qla_tgt_cmd
101 *cmd, struct atio_from_isp *atio, int ha_locked);
102 static void qlt_reject_free_srr_imm(struct scsi_qla_host *ha,
103 struct qla_tgt_srr_imm *imm, int ha_lock);
107 static struct kmem_cache *qla_tgt_cmd_cachep;
108 static struct kmem_cache *qla_tgt_mgmt_cmd_cachep;
109 static mempool_t *qla_tgt_mgmt_cmd_mempool;
110 static struct workqueue_struct *qla_tgt_wq;
111 static DEFINE_MUTEX(qla_tgt_mutex);
112 static LIST_HEAD(qla_tgt_glist);
114 /* ha->hardware_lock supposed to be held on entry (to protect tgt->sess_list) */
115 static struct qla_tgt_sess *qlt_find_sess_by_port_name(
117 const uint8_t *port_name)
119 struct qla_tgt_sess *sess;
121 list_for_each_entry(sess, &tgt->sess_list, sess_list_entry) {
122 if (!memcmp(sess->port_name, port_name, WWN_SIZE))
129 /* Might release hw lock, then reaquire!! */
130 static inline int qlt_issue_marker(struct scsi_qla_host *vha, int vha_locked)
132 /* Send marker if required */
133 if (unlikely(vha->marker_needed != 0)) {
134 int rc = qla2x00_issue_marker(vha, vha_locked);
135 if (rc != QLA_SUCCESS) {
136 ql_dbg(ql_dbg_tgt, vha, 0xe03d,
137 "qla_target(%d): issue_marker() failed\n",
146 struct scsi_qla_host *qlt_find_host_by_d_id(struct scsi_qla_host *vha,
149 struct qla_hw_data *ha = vha->hw;
152 if ((vha->d_id.b.area != d_id[1]) || (vha->d_id.b.domain != d_id[0]))
155 if (vha->d_id.b.al_pa == d_id[2])
158 BUG_ON(ha->tgt.tgt_vp_map == NULL);
159 vp_idx = ha->tgt.tgt_vp_map[d_id[2]].idx;
160 if (likely(test_bit(vp_idx, ha->vp_idx_map)))
161 return ha->tgt.tgt_vp_map[vp_idx].vha;
167 struct scsi_qla_host *qlt_find_host_by_vp_idx(struct scsi_qla_host *vha,
170 struct qla_hw_data *ha = vha->hw;
172 if (vha->vp_idx == vp_idx)
175 BUG_ON(ha->tgt.tgt_vp_map == NULL);
176 if (likely(test_bit(vp_idx, ha->vp_idx_map)))
177 return ha->tgt.tgt_vp_map[vp_idx].vha;
182 void qlt_24xx_atio_pkt_all_vps(struct scsi_qla_host *vha,
183 struct atio_from_isp *atio)
185 switch (atio->u.raw.entry_type) {
188 struct scsi_qla_host *host = qlt_find_host_by_d_id(vha,
189 atio->u.isp24.fcp_hdr.d_id);
190 if (unlikely(NULL == host)) {
191 ql_dbg(ql_dbg_tgt, vha, 0xe03e,
192 "qla_target(%d): Received ATIO_TYPE7 "
193 "with unknown d_id %x:%x:%x\n", vha->vp_idx,
194 atio->u.isp24.fcp_hdr.d_id[0],
195 atio->u.isp24.fcp_hdr.d_id[1],
196 atio->u.isp24.fcp_hdr.d_id[2]);
199 qlt_24xx_atio_pkt(host, atio);
203 case IMMED_NOTIFY_TYPE:
205 struct scsi_qla_host *host = vha;
206 struct imm_ntfy_from_isp *entry =
207 (struct imm_ntfy_from_isp *)atio;
209 if ((entry->u.isp24.vp_index != 0xFF) &&
210 (entry->u.isp24.nport_handle != 0xFFFF)) {
211 host = qlt_find_host_by_vp_idx(vha,
212 entry->u.isp24.vp_index);
213 if (unlikely(!host)) {
214 ql_dbg(ql_dbg_tgt, vha, 0xe03f,
215 "qla_target(%d): Received "
216 "ATIO (IMMED_NOTIFY_TYPE) "
217 "with unknown vp_index %d\n",
218 vha->vp_idx, entry->u.isp24.vp_index);
222 qlt_24xx_atio_pkt(host, atio);
227 ql_dbg(ql_dbg_tgt, vha, 0xe040,
228 "qla_target(%d): Received unknown ATIO atio "
229 "type %x\n", vha->vp_idx, atio->u.raw.entry_type);
236 void qlt_response_pkt_all_vps(struct scsi_qla_host *vha, response_t *pkt)
238 switch (pkt->entry_type) {
241 struct ctio7_from_24xx *entry = (struct ctio7_from_24xx *)pkt;
242 struct scsi_qla_host *host = qlt_find_host_by_vp_idx(vha,
244 if (unlikely(!host)) {
245 ql_dbg(ql_dbg_tgt, vha, 0xe041,
246 "qla_target(%d): Response pkt (CTIO_TYPE7) "
247 "received, with unknown vp_index %d\n",
248 vha->vp_idx, entry->vp_index);
251 qlt_response_pkt(host, pkt);
255 case IMMED_NOTIFY_TYPE:
257 struct scsi_qla_host *host = vha;
258 struct imm_ntfy_from_isp *entry =
259 (struct imm_ntfy_from_isp *)pkt;
261 host = qlt_find_host_by_vp_idx(vha, entry->u.isp24.vp_index);
262 if (unlikely(!host)) {
263 ql_dbg(ql_dbg_tgt, vha, 0xe042,
264 "qla_target(%d): Response pkt (IMMED_NOTIFY_TYPE) "
265 "received, with unknown vp_index %d\n",
266 vha->vp_idx, entry->u.isp24.vp_index);
269 qlt_response_pkt(host, pkt);
273 case NOTIFY_ACK_TYPE:
275 struct scsi_qla_host *host = vha;
276 struct nack_to_isp *entry = (struct nack_to_isp *)pkt;
278 if (0xFF != entry->u.isp24.vp_index) {
279 host = qlt_find_host_by_vp_idx(vha,
280 entry->u.isp24.vp_index);
281 if (unlikely(!host)) {
282 ql_dbg(ql_dbg_tgt, vha, 0xe043,
283 "qla_target(%d): Response "
284 "pkt (NOTIFY_ACK_TYPE) "
285 "received, with unknown "
286 "vp_index %d\n", vha->vp_idx,
287 entry->u.isp24.vp_index);
291 qlt_response_pkt(host, pkt);
297 struct abts_recv_from_24xx *entry =
298 (struct abts_recv_from_24xx *)pkt;
299 struct scsi_qla_host *host = qlt_find_host_by_vp_idx(vha,
301 if (unlikely(!host)) {
302 ql_dbg(ql_dbg_tgt, vha, 0xe044,
303 "qla_target(%d): Response pkt "
304 "(ABTS_RECV_24XX) received, with unknown "
305 "vp_index %d\n", vha->vp_idx, entry->vp_index);
308 qlt_response_pkt(host, pkt);
314 struct abts_resp_to_24xx *entry =
315 (struct abts_resp_to_24xx *)pkt;
316 struct scsi_qla_host *host = qlt_find_host_by_vp_idx(vha,
318 if (unlikely(!host)) {
319 ql_dbg(ql_dbg_tgt, vha, 0xe045,
320 "qla_target(%d): Response pkt "
321 "(ABTS_RECV_24XX) received, with unknown "
322 "vp_index %d\n", vha->vp_idx, entry->vp_index);
325 qlt_response_pkt(host, pkt);
330 qlt_response_pkt(vha, pkt);
336 static void qlt_free_session_done(struct work_struct *work)
338 struct qla_tgt_sess *sess = container_of(work, struct qla_tgt_sess,
340 struct qla_tgt *tgt = sess->tgt;
341 struct scsi_qla_host *vha = sess->vha;
342 struct qla_hw_data *ha = vha->hw;
346 * Release the target session for FC Nexus from fabric module code.
348 if (sess->se_sess != NULL)
349 ha->tgt.tgt_ops->free_session(sess);
351 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf001,
352 "Unregistration of sess %p finished\n", sess);
356 * We need to protect against race, when tgt is freed before or
360 if (tgt->sess_count == 0)
361 wake_up_all(&tgt->waitQ);
364 /* ha->hardware_lock supposed to be held on entry */
365 void qlt_unreg_sess(struct qla_tgt_sess *sess)
367 struct scsi_qla_host *vha = sess->vha;
369 vha->hw->tgt.tgt_ops->clear_nacl_from_fcport_map(sess);
371 list_del(&sess->sess_list_entry);
373 list_del(&sess->del_list_entry);
375 INIT_WORK(&sess->free_work, qlt_free_session_done);
376 schedule_work(&sess->free_work);
378 EXPORT_SYMBOL(qlt_unreg_sess);
380 /* ha->hardware_lock supposed to be held on entry */
381 static int qlt_reset(struct scsi_qla_host *vha, void *iocb, int mcmd)
383 struct qla_hw_data *ha = vha->hw;
384 struct qla_tgt_sess *sess = NULL;
385 uint32_t unpacked_lun, lun = 0;
388 struct imm_ntfy_from_isp *n = (struct imm_ntfy_from_isp *)iocb;
389 struct atio_from_isp *a = (struct atio_from_isp *)iocb;
391 loop_id = le16_to_cpu(n->u.isp24.nport_handle);
392 if (loop_id == 0xFFFF) {
393 #if 0 /* FIXME: Re-enable Global event handling.. */
395 atomic_inc(&ha->tgt.qla_tgt->tgt_global_resets_count);
396 qlt_clear_tgt_db(ha->tgt.qla_tgt, 1);
397 if (!list_empty(&ha->tgt.qla_tgt->sess_list)) {
398 sess = list_entry(ha->tgt.qla_tgt->sess_list.next,
399 typeof(*sess), sess_list_entry);
401 case QLA_TGT_NEXUS_LOSS_SESS:
402 mcmd = QLA_TGT_NEXUS_LOSS;
404 case QLA_TGT_ABORT_ALL_SESS:
405 mcmd = QLA_TGT_ABORT_ALL;
407 case QLA_TGT_NEXUS_LOSS:
408 case QLA_TGT_ABORT_ALL:
411 ql_dbg(ql_dbg_tgt, vha, 0xe046,
412 "qla_target(%d): Not allowed "
413 "command %x in %s", vha->vp_idx,
422 sess = ha->tgt.tgt_ops->find_sess_by_loop_id(vha, loop_id);
425 ql_dbg(ql_dbg_tgt, vha, 0xe000,
426 "Using sess for qla_tgt_reset: %p\n", sess);
432 ql_dbg(ql_dbg_tgt, vha, 0xe047,
433 "scsi(%ld): resetting (session %p from port %8phC mcmd %x, "
434 "loop_id %d)\n", vha->host_no, sess, sess->port_name,
437 lun = a->u.isp24.fcp_cmnd.lun;
438 unpacked_lun = scsilun_to_int((struct scsi_lun *)&lun);
440 return qlt_issue_task_mgmt(sess, unpacked_lun, mcmd,
441 iocb, QLA24XX_MGMT_SEND_NACK);
444 /* ha->hardware_lock supposed to be held on entry */
445 static void qlt_schedule_sess_for_deletion(struct qla_tgt_sess *sess,
448 struct qla_tgt *tgt = sess->tgt;
449 uint32_t dev_loss_tmo = tgt->ha->port_down_retry_count + 5;
454 ql_dbg(ql_dbg_tgt, sess->vha, 0xe001,
455 "Scheduling sess %p for deletion\n", sess);
456 list_add_tail(&sess->del_list_entry, &tgt->del_sess_list);
462 sess->expires = jiffies + dev_loss_tmo * HZ;
464 ql_dbg(ql_dbg_tgt, sess->vha, 0xe048,
465 "qla_target(%d): session for port %8phC (loop ID %d) scheduled for "
466 "deletion in %u secs (expires: %lu) immed: %d\n",
467 sess->vha->vp_idx, sess->port_name, sess->loop_id, dev_loss_tmo,
468 sess->expires, immediate);
471 schedule_delayed_work(&tgt->sess_del_work, 0);
473 schedule_delayed_work(&tgt->sess_del_work,
474 jiffies - sess->expires);
477 /* ha->hardware_lock supposed to be held on entry */
478 static void qlt_clear_tgt_db(struct qla_tgt *tgt, bool local_only)
480 struct qla_tgt_sess *sess;
482 list_for_each_entry(sess, &tgt->sess_list, sess_list_entry)
483 qlt_schedule_sess_for_deletion(sess, true);
485 /* At this point tgt could be already dead */
488 static int qla24xx_get_loop_id(struct scsi_qla_host *vha, const uint8_t *s_id,
491 struct qla_hw_data *ha = vha->hw;
492 dma_addr_t gid_list_dma;
493 struct gid_list_info *gid_list;
498 gid_list = dma_alloc_coherent(&ha->pdev->dev, qla2x00_gid_list_size(ha),
499 &gid_list_dma, GFP_KERNEL);
501 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf044,
502 "qla_target(%d): DMA Alloc failed of %u\n",
503 vha->vp_idx, qla2x00_gid_list_size(ha));
507 /* Get list of logged in devices */
508 rc = qla2x00_get_id_list(vha, gid_list, gid_list_dma, &entries);
509 if (rc != QLA_SUCCESS) {
510 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf045,
511 "qla_target(%d): get_id_list() failed: %x\n",
514 goto out_free_id_list;
517 id_iter = (char *)gid_list;
519 for (i = 0; i < entries; i++) {
520 struct gid_list_info *gid = (struct gid_list_info *)id_iter;
521 if ((gid->al_pa == s_id[2]) &&
522 (gid->area == s_id[1]) &&
523 (gid->domain == s_id[0])) {
524 *loop_id = le16_to_cpu(gid->loop_id);
528 id_iter += ha->gid_list_info_size;
532 dma_free_coherent(&ha->pdev->dev, qla2x00_gid_list_size(ha),
533 gid_list, gid_list_dma);
537 /* ha->hardware_lock supposed to be held on entry */
538 static void qlt_undelete_sess(struct qla_tgt_sess *sess)
540 BUG_ON(!sess->deleted);
542 list_del(&sess->del_list_entry);
546 static void qlt_del_sess_work_fn(struct delayed_work *work)
548 struct qla_tgt *tgt = container_of(work, struct qla_tgt,
550 struct scsi_qla_host *vha = tgt->vha;
551 struct qla_hw_data *ha = vha->hw;
552 struct qla_tgt_sess *sess;
555 spin_lock_irqsave(&ha->hardware_lock, flags);
556 while (!list_empty(&tgt->del_sess_list)) {
557 sess = list_entry(tgt->del_sess_list.next, typeof(*sess),
559 if (time_after_eq(jiffies, sess->expires)) {
560 qlt_undelete_sess(sess);
562 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf004,
563 "Timeout: sess %p about to be deleted\n",
565 ha->tgt.tgt_ops->shutdown_sess(sess);
566 ha->tgt.tgt_ops->put_sess(sess);
568 schedule_delayed_work(&tgt->sess_del_work,
569 jiffies - sess->expires);
573 spin_unlock_irqrestore(&ha->hardware_lock, flags);
577 * Adds an extra ref to allow to drop hw lock after adding sess to the list.
578 * Caller must put it.
580 static struct qla_tgt_sess *qlt_create_sess(
581 struct scsi_qla_host *vha,
585 struct qla_hw_data *ha = vha->hw;
586 struct qla_tgt_sess *sess;
588 unsigned char be_sid[3];
590 /* Check to avoid double sessions */
591 spin_lock_irqsave(&ha->hardware_lock, flags);
592 list_for_each_entry(sess, &vha->vha_tgt.qla_tgt->sess_list,
594 if (!memcmp(sess->port_name, fcport->port_name, WWN_SIZE)) {
595 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf005,
596 "Double sess %p found (s_id %x:%x:%x, "
597 "loop_id %d), updating to d_id %x:%x:%x, "
598 "loop_id %d", sess, sess->s_id.b.domain,
599 sess->s_id.b.al_pa, sess->s_id.b.area,
600 sess->loop_id, fcport->d_id.b.domain,
601 fcport->d_id.b.al_pa, fcport->d_id.b.area,
605 qlt_undelete_sess(sess);
607 kref_get(&sess->se_sess->sess_kref);
608 ha->tgt.tgt_ops->update_sess(sess, fcport->d_id, fcport->loop_id,
609 (fcport->flags & FCF_CONF_COMP_SUPPORTED));
611 if (sess->local && !local)
613 spin_unlock_irqrestore(&ha->hardware_lock, flags);
618 spin_unlock_irqrestore(&ha->hardware_lock, flags);
620 sess = kzalloc(sizeof(*sess), GFP_KERNEL);
622 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04a,
623 "qla_target(%u): session allocation failed, all commands "
624 "from port %8phC will be refused", vha->vp_idx,
629 sess->tgt = vha->vha_tgt.qla_tgt;
631 sess->s_id = fcport->d_id;
632 sess->loop_id = fcport->loop_id;
635 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf006,
636 "Adding sess %p to tgt %p via ->check_initiator_node_acl()\n",
637 sess, vha->vha_tgt.qla_tgt);
639 be_sid[0] = sess->s_id.b.domain;
640 be_sid[1] = sess->s_id.b.area;
641 be_sid[2] = sess->s_id.b.al_pa;
643 * Determine if this fc_port->port_name is allowed to access
644 * target mode using explict NodeACLs+MappedLUNs, or using
645 * TPG demo mode. If this is successful a target mode FC nexus
648 if (ha->tgt.tgt_ops->check_initiator_node_acl(vha,
649 &fcport->port_name[0], sess, &be_sid[0], fcport->loop_id) < 0) {
654 * Take an extra reference to ->sess_kref here to handle qla_tgt_sess
655 * access across ->hardware_lock reaquire.
657 kref_get(&sess->se_sess->sess_kref);
659 sess->conf_compl_supported = (fcport->flags & FCF_CONF_COMP_SUPPORTED);
660 BUILD_BUG_ON(sizeof(sess->port_name) != sizeof(fcport->port_name));
661 memcpy(sess->port_name, fcport->port_name, sizeof(sess->port_name));
663 spin_lock_irqsave(&ha->hardware_lock, flags);
664 list_add_tail(&sess->sess_list_entry, &vha->vha_tgt.qla_tgt->sess_list);
665 vha->vha_tgt.qla_tgt->sess_count++;
666 spin_unlock_irqrestore(&ha->hardware_lock, flags);
668 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04b,
669 "qla_target(%d): %ssession for wwn %8phC (loop_id %d, "
670 "s_id %x:%x:%x, confirmed completion %ssupported) added\n",
671 vha->vp_idx, local ? "local " : "", fcport->port_name,
672 fcport->loop_id, sess->s_id.b.domain, sess->s_id.b.area,
673 sess->s_id.b.al_pa, sess->conf_compl_supported ? "" : "not ");
679 * Called from drivers/scsi/qla2xxx/qla_init.c:qla2x00_reg_remote_port()
681 void qlt_fc_port_added(struct scsi_qla_host *vha, fc_port_t *fcport)
683 struct qla_hw_data *ha = vha->hw;
684 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
685 struct qla_tgt_sess *sess;
688 if (!vha->hw->tgt.tgt_ops)
691 if (!tgt || (fcport->port_type != FCT_INITIATOR))
694 if (qla_ini_mode_enabled(vha))
697 spin_lock_irqsave(&ha->hardware_lock, flags);
699 spin_unlock_irqrestore(&ha->hardware_lock, flags);
702 sess = qlt_find_sess_by_port_name(tgt, fcport->port_name);
704 spin_unlock_irqrestore(&ha->hardware_lock, flags);
706 mutex_lock(&vha->vha_tgt.tgt_mutex);
707 sess = qlt_create_sess(vha, fcport, false);
708 mutex_unlock(&vha->vha_tgt.tgt_mutex);
710 spin_lock_irqsave(&ha->hardware_lock, flags);
712 kref_get(&sess->se_sess->sess_kref);
715 qlt_undelete_sess(sess);
717 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04c,
718 "qla_target(%u): %ssession for port %8phC "
719 "(loop ID %d) reappeared\n", vha->vp_idx,
720 sess->local ? "local " : "", sess->port_name,
723 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf007,
724 "Reappeared sess %p\n", sess);
726 ha->tgt.tgt_ops->update_sess(sess, fcport->d_id, fcport->loop_id,
727 (fcport->flags & FCF_CONF_COMP_SUPPORTED));
730 if (sess && sess->local) {
731 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04d,
732 "qla_target(%u): local session for "
733 "port %8phC (loop ID %d) became global\n", vha->vp_idx,
734 fcport->port_name, sess->loop_id);
737 ha->tgt.tgt_ops->put_sess(sess);
738 spin_unlock_irqrestore(&ha->hardware_lock, flags);
741 void qlt_fc_port_deleted(struct scsi_qla_host *vha, fc_port_t *fcport)
743 struct qla_hw_data *ha = vha->hw;
744 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
745 struct qla_tgt_sess *sess;
748 if (!vha->hw->tgt.tgt_ops)
751 if (!tgt || (fcport->port_type != FCT_INITIATOR))
754 spin_lock_irqsave(&ha->hardware_lock, flags);
756 spin_unlock_irqrestore(&ha->hardware_lock, flags);
759 sess = qlt_find_sess_by_port_name(tgt, fcport->port_name);
761 spin_unlock_irqrestore(&ha->hardware_lock, flags);
765 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf008, "qla_tgt_fc_port_deleted %p", sess);
768 qlt_schedule_sess_for_deletion(sess, false);
769 spin_unlock_irqrestore(&ha->hardware_lock, flags);
772 static inline int test_tgt_sess_count(struct qla_tgt *tgt)
774 struct qla_hw_data *ha = tgt->ha;
778 * We need to protect against race, when tgt is freed before or
781 spin_lock_irqsave(&ha->hardware_lock, flags);
782 ql_dbg(ql_dbg_tgt, tgt->vha, 0xe002,
783 "tgt %p, empty(sess_list)=%d sess_count=%d\n",
784 tgt, list_empty(&tgt->sess_list), tgt->sess_count);
785 res = (tgt->sess_count == 0);
786 spin_unlock_irqrestore(&ha->hardware_lock, flags);
791 /* Called by tcm_qla2xxx configfs code */
792 void qlt_stop_phase1(struct qla_tgt *tgt)
794 struct scsi_qla_host *vha = tgt->vha;
795 struct qla_hw_data *ha = tgt->ha;
798 if (tgt->tgt_stop || tgt->tgt_stopped) {
799 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04e,
800 "Already in tgt->tgt_stop or tgt_stopped state\n");
805 ql_dbg(ql_dbg_tgt, vha, 0xe003, "Stopping target for host %ld(%p)\n",
808 * Mutex needed to sync with qla_tgt_fc_port_[added,deleted].
809 * Lock is needed, because we still can get an incoming packet.
811 mutex_lock(&vha->vha_tgt.tgt_mutex);
812 spin_lock_irqsave(&ha->hardware_lock, flags);
814 qlt_clear_tgt_db(tgt, true);
815 spin_unlock_irqrestore(&ha->hardware_lock, flags);
816 mutex_unlock(&vha->vha_tgt.tgt_mutex);
818 flush_delayed_work(&tgt->sess_del_work);
820 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf009,
821 "Waiting for sess works (tgt %p)", tgt);
822 spin_lock_irqsave(&tgt->sess_work_lock, flags);
823 while (!list_empty(&tgt->sess_works_list)) {
824 spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
825 flush_scheduled_work();
826 spin_lock_irqsave(&tgt->sess_work_lock, flags);
828 spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
830 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00a,
831 "Waiting for tgt %p: list_empty(sess_list)=%d "
832 "sess_count=%d\n", tgt, list_empty(&tgt->sess_list),
835 wait_event(tgt->waitQ, test_tgt_sess_count(tgt));
838 if (!ha->flags.host_shutting_down && qla_tgt_mode_enabled(vha))
839 qlt_disable_vha(vha);
841 /* Wait for sessions to clear out (just in case) */
842 wait_event(tgt->waitQ, test_tgt_sess_count(tgt));
844 EXPORT_SYMBOL(qlt_stop_phase1);
846 /* Called by tcm_qla2xxx configfs code */
847 void qlt_stop_phase2(struct qla_tgt *tgt)
849 struct qla_hw_data *ha = tgt->ha;
850 scsi_qla_host_t *vha = pci_get_drvdata(ha->pdev);
853 if (tgt->tgt_stopped) {
854 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04f,
855 "Already in tgt->tgt_stopped state\n");
860 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00b,
861 "Waiting for %d IRQ commands to complete (tgt %p)",
862 tgt->irq_cmd_count, tgt);
864 mutex_lock(&vha->vha_tgt.tgt_mutex);
865 spin_lock_irqsave(&ha->hardware_lock, flags);
866 while (tgt->irq_cmd_count != 0) {
867 spin_unlock_irqrestore(&ha->hardware_lock, flags);
869 spin_lock_irqsave(&ha->hardware_lock, flags);
872 tgt->tgt_stopped = 1;
873 spin_unlock_irqrestore(&ha->hardware_lock, flags);
874 mutex_unlock(&vha->vha_tgt.tgt_mutex);
876 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00c, "Stop of tgt %p finished",
879 EXPORT_SYMBOL(qlt_stop_phase2);
881 /* Called from qlt_remove_target() -> qla2x00_remove_one() */
882 static void qlt_release(struct qla_tgt *tgt)
884 scsi_qla_host_t *vha = tgt->vha;
886 if ((vha->vha_tgt.qla_tgt != NULL) && !tgt->tgt_stopped)
887 qlt_stop_phase2(tgt);
889 vha->vha_tgt.qla_tgt = NULL;
891 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00d,
892 "Release of tgt %p finished\n", tgt);
897 /* ha->hardware_lock supposed to be held on entry */
898 static int qlt_sched_sess_work(struct qla_tgt *tgt, int type,
899 const void *param, unsigned int param_size)
901 struct qla_tgt_sess_work_param *prm;
904 prm = kzalloc(sizeof(*prm), GFP_ATOMIC);
906 ql_dbg(ql_dbg_tgt_mgt, tgt->vha, 0xf050,
907 "qla_target(%d): Unable to create session "
908 "work, command will be refused", 0);
912 ql_dbg(ql_dbg_tgt_mgt, tgt->vha, 0xf00e,
913 "Scheduling work (type %d, prm %p)"
914 " to find session for param %p (size %d, tgt %p)\n",
915 type, prm, param, param_size, tgt);
918 memcpy(&prm->tm_iocb, param, param_size);
920 spin_lock_irqsave(&tgt->sess_work_lock, flags);
921 list_add_tail(&prm->sess_works_list_entry, &tgt->sess_works_list);
922 spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
924 schedule_work(&tgt->sess_work);
930 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
932 static void qlt_send_notify_ack(struct scsi_qla_host *vha,
933 struct imm_ntfy_from_isp *ntfy,
934 uint32_t add_flags, uint16_t resp_code, int resp_code_valid,
935 uint16_t srr_flags, uint16_t srr_reject_code, uint8_t srr_explan)
937 struct qla_hw_data *ha = vha->hw;
939 struct nack_to_isp *nack;
941 ql_dbg(ql_dbg_tgt, vha, 0xe004, "Sending NOTIFY_ACK (ha=%p)\n", ha);
943 /* Send marker if required */
944 if (qlt_issue_marker(vha, 1) != QLA_SUCCESS)
947 pkt = (request_t *)qla2x00_alloc_iocbs(vha, NULL);
949 ql_dbg(ql_dbg_tgt, vha, 0xe049,
950 "qla_target(%d): %s failed: unable to allocate "
951 "request packet\n", vha->vp_idx, __func__);
955 if (vha->vha_tgt.qla_tgt != NULL)
956 vha->vha_tgt.qla_tgt->notify_ack_expected++;
958 pkt->entry_type = NOTIFY_ACK_TYPE;
959 pkt->entry_count = 1;
961 nack = (struct nack_to_isp *)pkt;
962 nack->ox_id = ntfy->ox_id;
964 nack->u.isp24.nport_handle = ntfy->u.isp24.nport_handle;
965 if (le16_to_cpu(ntfy->u.isp24.status) == IMM_NTFY_ELS) {
966 nack->u.isp24.flags = ntfy->u.isp24.flags &
967 __constant_cpu_to_le32(NOTIFY24XX_FLAGS_PUREX_IOCB);
969 nack->u.isp24.srr_rx_id = ntfy->u.isp24.srr_rx_id;
970 nack->u.isp24.status = ntfy->u.isp24.status;
971 nack->u.isp24.status_subcode = ntfy->u.isp24.status_subcode;
972 nack->u.isp24.fw_handle = ntfy->u.isp24.fw_handle;
973 nack->u.isp24.exchange_address = ntfy->u.isp24.exchange_address;
974 nack->u.isp24.srr_rel_offs = ntfy->u.isp24.srr_rel_offs;
975 nack->u.isp24.srr_ui = ntfy->u.isp24.srr_ui;
976 nack->u.isp24.srr_flags = cpu_to_le16(srr_flags);
977 nack->u.isp24.srr_reject_code = srr_reject_code;
978 nack->u.isp24.srr_reject_code_expl = srr_explan;
979 nack->u.isp24.vp_index = ntfy->u.isp24.vp_index;
981 ql_dbg(ql_dbg_tgt, vha, 0xe005,
982 "qla_target(%d): Sending 24xx Notify Ack %d\n",
983 vha->vp_idx, nack->u.isp24.status);
985 qla2x00_start_iocbs(vha, vha->req);
989 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
991 static void qlt_24xx_send_abts_resp(struct scsi_qla_host *vha,
992 struct abts_recv_from_24xx *abts, uint32_t status,
995 struct qla_hw_data *ha = vha->hw;
996 struct abts_resp_to_24xx *resp;
1000 ql_dbg(ql_dbg_tgt, vha, 0xe006,
1001 "Sending task mgmt ABTS response (ha=%p, atio=%p, status=%x\n",
1004 /* Send marker if required */
1005 if (qlt_issue_marker(vha, 1) != QLA_SUCCESS)
1008 resp = (struct abts_resp_to_24xx *)qla2x00_alloc_iocbs(vha, NULL);
1010 ql_dbg(ql_dbg_tgt, vha, 0xe04a,
1011 "qla_target(%d): %s failed: unable to allocate "
1012 "request packet", vha->vp_idx, __func__);
1016 resp->entry_type = ABTS_RESP_24XX;
1017 resp->entry_count = 1;
1018 resp->nport_handle = abts->nport_handle;
1019 resp->vp_index = vha->vp_idx;
1020 resp->sof_type = abts->sof_type;
1021 resp->exchange_address = abts->exchange_address;
1022 resp->fcp_hdr_le = abts->fcp_hdr_le;
1023 f_ctl = __constant_cpu_to_le32(F_CTL_EXCH_CONTEXT_RESP |
1024 F_CTL_LAST_SEQ | F_CTL_END_SEQ |
1025 F_CTL_SEQ_INITIATIVE);
1026 p = (uint8_t *)&f_ctl;
1027 resp->fcp_hdr_le.f_ctl[0] = *p++;
1028 resp->fcp_hdr_le.f_ctl[1] = *p++;
1029 resp->fcp_hdr_le.f_ctl[2] = *p;
1031 resp->fcp_hdr_le.d_id[0] = abts->fcp_hdr_le.d_id[0];
1032 resp->fcp_hdr_le.d_id[1] = abts->fcp_hdr_le.d_id[1];
1033 resp->fcp_hdr_le.d_id[2] = abts->fcp_hdr_le.d_id[2];
1034 resp->fcp_hdr_le.s_id[0] = abts->fcp_hdr_le.s_id[0];
1035 resp->fcp_hdr_le.s_id[1] = abts->fcp_hdr_le.s_id[1];
1036 resp->fcp_hdr_le.s_id[2] = abts->fcp_hdr_le.s_id[2];
1038 resp->fcp_hdr_le.d_id[0] = abts->fcp_hdr_le.s_id[0];
1039 resp->fcp_hdr_le.d_id[1] = abts->fcp_hdr_le.s_id[1];
1040 resp->fcp_hdr_le.d_id[2] = abts->fcp_hdr_le.s_id[2];
1041 resp->fcp_hdr_le.s_id[0] = abts->fcp_hdr_le.d_id[0];
1042 resp->fcp_hdr_le.s_id[1] = abts->fcp_hdr_le.d_id[1];
1043 resp->fcp_hdr_le.s_id[2] = abts->fcp_hdr_le.d_id[2];
1045 resp->exchange_addr_to_abort = abts->exchange_addr_to_abort;
1046 if (status == FCP_TMF_CMPL) {
1047 resp->fcp_hdr_le.r_ctl = R_CTL_BASIC_LINK_SERV | R_CTL_B_ACC;
1048 resp->payload.ba_acct.seq_id_valid = SEQ_ID_INVALID;
1049 resp->payload.ba_acct.low_seq_cnt = 0x0000;
1050 resp->payload.ba_acct.high_seq_cnt = 0xFFFF;
1051 resp->payload.ba_acct.ox_id = abts->fcp_hdr_le.ox_id;
1052 resp->payload.ba_acct.rx_id = abts->fcp_hdr_le.rx_id;
1054 resp->fcp_hdr_le.r_ctl = R_CTL_BASIC_LINK_SERV | R_CTL_B_RJT;
1055 resp->payload.ba_rjt.reason_code =
1056 BA_RJT_REASON_CODE_UNABLE_TO_PERFORM;
1057 /* Other bytes are zero */
1060 vha->vha_tgt.qla_tgt->abts_resp_expected++;
1062 qla2x00_start_iocbs(vha, vha->req);
1066 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
1068 static void qlt_24xx_retry_term_exchange(struct scsi_qla_host *vha,
1069 struct abts_resp_from_24xx_fw *entry)
1071 struct ctio7_to_24xx *ctio;
1073 ql_dbg(ql_dbg_tgt, vha, 0xe007,
1074 "Sending retry TERM EXCH CTIO7 (ha=%p)\n", vha->hw);
1075 /* Send marker if required */
1076 if (qlt_issue_marker(vha, 1) != QLA_SUCCESS)
1079 ctio = (struct ctio7_to_24xx *)qla2x00_alloc_iocbs(vha, NULL);
1081 ql_dbg(ql_dbg_tgt, vha, 0xe04b,
1082 "qla_target(%d): %s failed: unable to allocate "
1083 "request packet\n", vha->vp_idx, __func__);
1088 * We've got on entrance firmware's response on by us generated
1089 * ABTS response. So, in it ID fields are reversed.
1092 ctio->entry_type = CTIO_TYPE7;
1093 ctio->entry_count = 1;
1094 ctio->nport_handle = entry->nport_handle;
1095 ctio->handle = QLA_TGT_SKIP_HANDLE | CTIO_COMPLETION_HANDLE_MARK;
1096 ctio->timeout = __constant_cpu_to_le16(QLA_TGT_TIMEOUT);
1097 ctio->vp_index = vha->vp_idx;
1098 ctio->initiator_id[0] = entry->fcp_hdr_le.d_id[0];
1099 ctio->initiator_id[1] = entry->fcp_hdr_le.d_id[1];
1100 ctio->initiator_id[2] = entry->fcp_hdr_le.d_id[2];
1101 ctio->exchange_addr = entry->exchange_addr_to_abort;
1102 ctio->u.status1.flags =
1103 __constant_cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_1 |
1104 CTIO7_FLAGS_TERMINATE);
1105 ctio->u.status1.ox_id = entry->fcp_hdr_le.ox_id;
1107 qla2x00_start_iocbs(vha, vha->req);
1109 qlt_24xx_send_abts_resp(vha, (struct abts_recv_from_24xx *)entry,
1110 FCP_TMF_CMPL, true);
1113 /* ha->hardware_lock supposed to be held on entry */
1114 static int __qlt_24xx_handle_abts(struct scsi_qla_host *vha,
1115 struct abts_recv_from_24xx *abts, struct qla_tgt_sess *sess)
1117 struct qla_hw_data *ha = vha->hw;
1118 struct se_session *se_sess = sess->se_sess;
1119 struct qla_tgt_mgmt_cmd *mcmd;
1120 struct se_cmd *se_cmd;
1123 bool found_lun = false;
1125 spin_lock(&se_sess->sess_cmd_lock);
1126 list_for_each_entry(se_cmd, &se_sess->sess_cmd_list, se_cmd_list) {
1127 struct qla_tgt_cmd *cmd =
1128 container_of(se_cmd, struct qla_tgt_cmd, se_cmd);
1129 if (cmd->tag == abts->exchange_addr_to_abort) {
1130 lun = cmd->unpacked_lun;
1135 spin_unlock(&se_sess->sess_cmd_lock);
1140 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00f,
1141 "qla_target(%d): task abort (tag=%d)\n",
1142 vha->vp_idx, abts->exchange_addr_to_abort);
1144 mcmd = mempool_alloc(qla_tgt_mgmt_cmd_mempool, GFP_ATOMIC);
1146 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf051,
1147 "qla_target(%d): %s: Allocation of ABORT cmd failed",
1148 vha->vp_idx, __func__);
1151 memset(mcmd, 0, sizeof(*mcmd));
1154 memcpy(&mcmd->orig_iocb.abts, abts, sizeof(mcmd->orig_iocb.abts));
1156 rc = ha->tgt.tgt_ops->handle_tmr(mcmd, lun, TMR_ABORT_TASK,
1157 abts->exchange_addr_to_abort);
1159 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf052,
1160 "qla_target(%d): tgt_ops->handle_tmr()"
1161 " failed: %d", vha->vp_idx, rc);
1162 mempool_free(mcmd, qla_tgt_mgmt_cmd_mempool);
1170 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
1172 static void qlt_24xx_handle_abts(struct scsi_qla_host *vha,
1173 struct abts_recv_from_24xx *abts)
1175 struct qla_hw_data *ha = vha->hw;
1176 struct qla_tgt_sess *sess;
1177 uint32_t tag = abts->exchange_addr_to_abort;
1181 if (le32_to_cpu(abts->fcp_hdr_le.parameter) & ABTS_PARAM_ABORT_SEQ) {
1182 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf053,
1183 "qla_target(%d): ABTS: Abort Sequence not "
1184 "supported\n", vha->vp_idx);
1185 qlt_24xx_send_abts_resp(vha, abts, FCP_TMF_REJECTED, false);
1189 if (tag == ATIO_EXCHANGE_ADDRESS_UNKNOWN) {
1190 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf010,
1191 "qla_target(%d): ABTS: Unknown Exchange "
1192 "Address received\n", vha->vp_idx);
1193 qlt_24xx_send_abts_resp(vha, abts, FCP_TMF_REJECTED, false);
1197 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf011,
1198 "qla_target(%d): task abort (s_id=%x:%x:%x, "
1199 "tag=%d, param=%x)\n", vha->vp_idx, abts->fcp_hdr_le.s_id[2],
1200 abts->fcp_hdr_le.s_id[1], abts->fcp_hdr_le.s_id[0], tag,
1201 le32_to_cpu(abts->fcp_hdr_le.parameter));
1203 s_id[0] = abts->fcp_hdr_le.s_id[2];
1204 s_id[1] = abts->fcp_hdr_le.s_id[1];
1205 s_id[2] = abts->fcp_hdr_le.s_id[0];
1207 sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha, s_id);
1209 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf012,
1210 "qla_target(%d): task abort for non-existant session\n",
1212 rc = qlt_sched_sess_work(vha->vha_tgt.qla_tgt,
1213 QLA_TGT_SESS_WORK_ABORT, abts, sizeof(*abts));
1215 qlt_24xx_send_abts_resp(vha, abts, FCP_TMF_REJECTED,
1221 rc = __qlt_24xx_handle_abts(vha, abts, sess);
1223 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf054,
1224 "qla_target(%d): __qlt_24xx_handle_abts() failed: %d\n",
1226 qlt_24xx_send_abts_resp(vha, abts, FCP_TMF_REJECTED, false);
1232 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
1234 static void qlt_24xx_send_task_mgmt_ctio(struct scsi_qla_host *ha,
1235 struct qla_tgt_mgmt_cmd *mcmd, uint32_t resp_code)
1237 struct atio_from_isp *atio = &mcmd->orig_iocb.atio;
1238 struct ctio7_to_24xx *ctio;
1240 ql_dbg(ql_dbg_tgt, ha, 0xe008,
1241 "Sending task mgmt CTIO7 (ha=%p, atio=%p, resp_code=%x\n",
1242 ha, atio, resp_code);
1244 /* Send marker if required */
1245 if (qlt_issue_marker(ha, 1) != QLA_SUCCESS)
1248 ctio = (struct ctio7_to_24xx *)qla2x00_alloc_iocbs(ha, NULL);
1250 ql_dbg(ql_dbg_tgt, ha, 0xe04c,
1251 "qla_target(%d): %s failed: unable to allocate "
1252 "request packet\n", ha->vp_idx, __func__);
1256 ctio->entry_type = CTIO_TYPE7;
1257 ctio->entry_count = 1;
1258 ctio->handle = QLA_TGT_SKIP_HANDLE | CTIO_COMPLETION_HANDLE_MARK;
1259 ctio->nport_handle = mcmd->sess->loop_id;
1260 ctio->timeout = __constant_cpu_to_le16(QLA_TGT_TIMEOUT);
1261 ctio->vp_index = ha->vp_idx;
1262 ctio->initiator_id[0] = atio->u.isp24.fcp_hdr.s_id[2];
1263 ctio->initiator_id[1] = atio->u.isp24.fcp_hdr.s_id[1];
1264 ctio->initiator_id[2] = atio->u.isp24.fcp_hdr.s_id[0];
1265 ctio->exchange_addr = atio->u.isp24.exchange_addr;
1266 ctio->u.status1.flags = (atio->u.isp24.attr << 9) |
1267 __constant_cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_1 |
1268 CTIO7_FLAGS_SEND_STATUS);
1269 ctio->u.status1.ox_id = swab16(atio->u.isp24.fcp_hdr.ox_id);
1270 ctio->u.status1.scsi_status =
1271 __constant_cpu_to_le16(SS_RESPONSE_INFO_LEN_VALID);
1272 ctio->u.status1.response_len = __constant_cpu_to_le16(8);
1273 ctio->u.status1.sense_data[0] = resp_code;
1275 qla2x00_start_iocbs(ha, ha->req);
1278 void qlt_free_mcmd(struct qla_tgt_mgmt_cmd *mcmd)
1280 mempool_free(mcmd, qla_tgt_mgmt_cmd_mempool);
1282 EXPORT_SYMBOL(qlt_free_mcmd);
1284 /* callback from target fabric module code */
1285 void qlt_xmit_tm_rsp(struct qla_tgt_mgmt_cmd *mcmd)
1287 struct scsi_qla_host *vha = mcmd->sess->vha;
1288 struct qla_hw_data *ha = vha->hw;
1289 unsigned long flags;
1291 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf013,
1292 "TM response mcmd (%p) status %#x state %#x",
1293 mcmd, mcmd->fc_tm_rsp, mcmd->flags);
1295 spin_lock_irqsave(&ha->hardware_lock, flags);
1296 if (mcmd->flags == QLA24XX_MGMT_SEND_NACK)
1297 qlt_send_notify_ack(vha, &mcmd->orig_iocb.imm_ntfy,
1300 if (mcmd->se_cmd.se_tmr_req->function == TMR_ABORT_TASK)
1301 qlt_24xx_send_abts_resp(vha, &mcmd->orig_iocb.abts,
1302 mcmd->fc_tm_rsp, false);
1304 qlt_24xx_send_task_mgmt_ctio(vha, mcmd,
1308 * Make the callback for ->free_mcmd() to queue_work() and invoke
1309 * target_put_sess_cmd() to drop cmd_kref to 1. The final
1310 * target_put_sess_cmd() call will be made from TFO->check_stop_free()
1311 * -> tcm_qla2xxx_check_stop_free() to release the TMR associated se_cmd
1312 * descriptor after TFO->queue_tm_rsp() -> tcm_qla2xxx_queue_tm_rsp() ->
1313 * qlt_xmit_tm_rsp() returns here..
1315 ha->tgt.tgt_ops->free_mcmd(mcmd);
1316 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1318 EXPORT_SYMBOL(qlt_xmit_tm_rsp);
1321 static int qlt_pci_map_calc_cnt(struct qla_tgt_prm *prm)
1323 struct qla_tgt_cmd *cmd = prm->cmd;
1325 BUG_ON(cmd->sg_cnt == 0);
1327 prm->sg = (struct scatterlist *)cmd->sg;
1328 prm->seg_cnt = pci_map_sg(prm->tgt->ha->pdev, cmd->sg,
1329 cmd->sg_cnt, cmd->dma_data_direction);
1330 if (unlikely(prm->seg_cnt == 0))
1333 prm->cmd->sg_mapped = 1;
1336 * If greater than four sg entries then we need to allocate
1337 * the continuation entries
1339 if (prm->seg_cnt > prm->tgt->datasegs_per_cmd)
1340 prm->req_cnt += DIV_ROUND_UP(prm->seg_cnt -
1341 prm->tgt->datasegs_per_cmd, prm->tgt->datasegs_per_cont);
1343 ql_dbg(ql_dbg_tgt, prm->cmd->vha, 0xe009, "seg_cnt=%d, req_cnt=%d\n",
1344 prm->seg_cnt, prm->req_cnt);
1348 ql_dbg(ql_dbg_tgt, prm->cmd->vha, 0xe04d,
1349 "qla_target(%d): PCI mapping failed: sg_cnt=%d",
1350 0, prm->cmd->sg_cnt);
1354 static inline void qlt_unmap_sg(struct scsi_qla_host *vha,
1355 struct qla_tgt_cmd *cmd)
1357 struct qla_hw_data *ha = vha->hw;
1359 BUG_ON(!cmd->sg_mapped);
1360 pci_unmap_sg(ha->pdev, cmd->sg, cmd->sg_cnt, cmd->dma_data_direction);
1364 static int qlt_check_reserve_free_req(struct scsi_qla_host *vha,
1367 struct qla_hw_data *ha = vha->hw;
1368 device_reg_t __iomem *reg = ha->iobase;
1371 if (vha->req->cnt < (req_cnt + 2)) {
1372 cnt = (uint16_t)RD_REG_DWORD(®->isp24.req_q_out);
1374 ql_dbg(ql_dbg_tgt, vha, 0xe00a,
1375 "Request ring circled: cnt=%d, vha->->ring_index=%d, "
1376 "vha->req->cnt=%d, req_cnt=%d\n", cnt,
1377 vha->req->ring_index, vha->req->cnt, req_cnt);
1378 if (vha->req->ring_index < cnt)
1379 vha->req->cnt = cnt - vha->req->ring_index;
1381 vha->req->cnt = vha->req->length -
1382 (vha->req->ring_index - cnt);
1385 if (unlikely(vha->req->cnt < (req_cnt + 2))) {
1386 ql_dbg(ql_dbg_tgt, vha, 0xe00b,
1387 "qla_target(%d): There is no room in the "
1388 "request ring: vha->req->ring_index=%d, vha->req->cnt=%d, "
1389 "req_cnt=%d\n", vha->vp_idx, vha->req->ring_index,
1390 vha->req->cnt, req_cnt);
1393 vha->req->cnt -= req_cnt;
1399 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
1401 static inline void *qlt_get_req_pkt(struct scsi_qla_host *vha)
1403 /* Adjust ring index. */
1404 vha->req->ring_index++;
1405 if (vha->req->ring_index == vha->req->length) {
1406 vha->req->ring_index = 0;
1407 vha->req->ring_ptr = vha->req->ring;
1409 vha->req->ring_ptr++;
1411 return (cont_entry_t *)vha->req->ring_ptr;
1414 /* ha->hardware_lock supposed to be held on entry */
1415 static inline uint32_t qlt_make_handle(struct scsi_qla_host *vha)
1417 struct qla_hw_data *ha = vha->hw;
1420 h = ha->tgt.current_handle;
1421 /* always increment cmd handle */
1424 if (h > DEFAULT_OUTSTANDING_COMMANDS)
1425 h = 1; /* 0 is QLA_TGT_NULL_HANDLE */
1426 if (h == ha->tgt.current_handle) {
1427 ql_dbg(ql_dbg_tgt, vha, 0xe04e,
1428 "qla_target(%d): Ran out of "
1429 "empty cmd slots in ha %p\n", vha->vp_idx, ha);
1430 h = QLA_TGT_NULL_HANDLE;
1433 } while ((h == QLA_TGT_NULL_HANDLE) ||
1434 (h == QLA_TGT_SKIP_HANDLE) ||
1435 (ha->tgt.cmds[h-1] != NULL));
1437 if (h != QLA_TGT_NULL_HANDLE)
1438 ha->tgt.current_handle = h;
1443 /* ha->hardware_lock supposed to be held on entry */
1444 static int qlt_24xx_build_ctio_pkt(struct qla_tgt_prm *prm,
1445 struct scsi_qla_host *vha)
1448 struct ctio7_to_24xx *pkt;
1449 struct qla_hw_data *ha = vha->hw;
1450 struct atio_from_isp *atio = &prm->cmd->atio;
1452 pkt = (struct ctio7_to_24xx *)vha->req->ring_ptr;
1454 memset(pkt, 0, sizeof(*pkt));
1456 pkt->entry_type = CTIO_TYPE7;
1457 pkt->entry_count = (uint8_t)prm->req_cnt;
1458 pkt->vp_index = vha->vp_idx;
1460 h = qlt_make_handle(vha);
1461 if (unlikely(h == QLA_TGT_NULL_HANDLE)) {
1463 * CTIO type 7 from the firmware doesn't provide a way to
1464 * know the initiator's LOOP ID, hence we can't find
1465 * the session and, so, the command.
1469 ha->tgt.cmds[h-1] = prm->cmd;
1471 pkt->handle = h | CTIO_COMPLETION_HANDLE_MARK;
1472 pkt->nport_handle = prm->cmd->loop_id;
1473 pkt->timeout = __constant_cpu_to_le16(QLA_TGT_TIMEOUT);
1474 pkt->initiator_id[0] = atio->u.isp24.fcp_hdr.s_id[2];
1475 pkt->initiator_id[1] = atio->u.isp24.fcp_hdr.s_id[1];
1476 pkt->initiator_id[2] = atio->u.isp24.fcp_hdr.s_id[0];
1477 pkt->exchange_addr = atio->u.isp24.exchange_addr;
1478 pkt->u.status0.flags |= (atio->u.isp24.attr << 9);
1479 pkt->u.status0.ox_id = swab16(atio->u.isp24.fcp_hdr.ox_id);
1480 pkt->u.status0.relative_offset = cpu_to_le32(prm->cmd->offset);
1482 ql_dbg(ql_dbg_tgt, vha, 0xe00c,
1483 "qla_target(%d): handle(cmd) -> %08x, timeout %d, ox_id %#x\n",
1484 vha->vp_idx, pkt->handle, QLA_TGT_TIMEOUT,
1485 le16_to_cpu(pkt->u.status0.ox_id));
1490 * ha->hardware_lock supposed to be held on entry. We have already made sure
1491 * that there is sufficient amount of request entries to not drop it.
1493 static void qlt_load_cont_data_segments(struct qla_tgt_prm *prm,
1494 struct scsi_qla_host *vha)
1497 uint32_t *dword_ptr;
1498 int enable_64bit_addressing = prm->tgt->tgt_enable_64bit_addr;
1500 /* Build continuation packets */
1501 while (prm->seg_cnt > 0) {
1502 cont_a64_entry_t *cont_pkt64 =
1503 (cont_a64_entry_t *)qlt_get_req_pkt(vha);
1506 * Make sure that from cont_pkt64 none of
1507 * 64-bit specific fields used for 32-bit
1508 * addressing. Cast to (cont_entry_t *) for
1512 memset(cont_pkt64, 0, sizeof(*cont_pkt64));
1514 cont_pkt64->entry_count = 1;
1515 cont_pkt64->sys_define = 0;
1517 if (enable_64bit_addressing) {
1518 cont_pkt64->entry_type = CONTINUE_A64_TYPE;
1520 (uint32_t *)&cont_pkt64->dseg_0_address;
1522 cont_pkt64->entry_type = CONTINUE_TYPE;
1524 (uint32_t *)&((cont_entry_t *)
1525 cont_pkt64)->dseg_0_address;
1528 /* Load continuation entry data segments */
1530 cnt < prm->tgt->datasegs_per_cont && prm->seg_cnt;
1531 cnt++, prm->seg_cnt--) {
1533 cpu_to_le32(pci_dma_lo32
1534 (sg_dma_address(prm->sg)));
1535 if (enable_64bit_addressing) {
1537 cpu_to_le32(pci_dma_hi32
1541 *dword_ptr++ = cpu_to_le32(sg_dma_len(prm->sg));
1543 ql_dbg(ql_dbg_tgt, vha, 0xe00d,
1544 "S/G Segment Cont. phys_addr=%llx:%llx, len=%d\n",
1545 (long long unsigned int)
1546 pci_dma_hi32(sg_dma_address(prm->sg)),
1547 (long long unsigned int)
1548 pci_dma_lo32(sg_dma_address(prm->sg)),
1549 (int)sg_dma_len(prm->sg));
1551 prm->sg = sg_next(prm->sg);
1557 * ha->hardware_lock supposed to be held on entry. We have already made sure
1558 * that there is sufficient amount of request entries to not drop it.
1560 static void qlt_load_data_segments(struct qla_tgt_prm *prm,
1561 struct scsi_qla_host *vha)
1564 uint32_t *dword_ptr;
1565 int enable_64bit_addressing = prm->tgt->tgt_enable_64bit_addr;
1566 struct ctio7_to_24xx *pkt24 = (struct ctio7_to_24xx *)prm->pkt;
1568 ql_dbg(ql_dbg_tgt, vha, 0xe00e,
1569 "iocb->scsi_status=%x, iocb->flags=%x\n",
1570 le16_to_cpu(pkt24->u.status0.scsi_status),
1571 le16_to_cpu(pkt24->u.status0.flags));
1573 pkt24->u.status0.transfer_length = cpu_to_le32(prm->cmd->bufflen);
1575 /* Setup packet address segment pointer */
1576 dword_ptr = pkt24->u.status0.dseg_0_address;
1578 /* Set total data segment count */
1580 pkt24->dseg_count = cpu_to_le16(prm->seg_cnt);
1582 if (prm->seg_cnt == 0) {
1583 /* No data transfer */
1589 /* If scatter gather */
1590 ql_dbg(ql_dbg_tgt, vha, 0xe00f, "%s", "Building S/G data segments...");
1592 /* Load command entry data segments */
1594 (cnt < prm->tgt->datasegs_per_cmd) && prm->seg_cnt;
1595 cnt++, prm->seg_cnt--) {
1597 cpu_to_le32(pci_dma_lo32(sg_dma_address(prm->sg)));
1598 if (enable_64bit_addressing) {
1600 cpu_to_le32(pci_dma_hi32(
1601 sg_dma_address(prm->sg)));
1603 *dword_ptr++ = cpu_to_le32(sg_dma_len(prm->sg));
1605 ql_dbg(ql_dbg_tgt, vha, 0xe010,
1606 "S/G Segment phys_addr=%llx:%llx, len=%d\n",
1607 (long long unsigned int)pci_dma_hi32(sg_dma_address(
1609 (long long unsigned int)pci_dma_lo32(sg_dma_address(
1611 (int)sg_dma_len(prm->sg));
1613 prm->sg = sg_next(prm->sg);
1616 qlt_load_cont_data_segments(prm, vha);
1619 static inline int qlt_has_data(struct qla_tgt_cmd *cmd)
1621 return cmd->bufflen > 0;
1625 * Called without ha->hardware_lock held
1627 static int qlt_pre_xmit_response(struct qla_tgt_cmd *cmd,
1628 struct qla_tgt_prm *prm, int xmit_type, uint8_t scsi_status,
1629 uint32_t *full_req_cnt)
1631 struct qla_tgt *tgt = cmd->tgt;
1632 struct scsi_qla_host *vha = tgt->vha;
1633 struct qla_hw_data *ha = vha->hw;
1634 struct se_cmd *se_cmd = &cmd->se_cmd;
1636 if (unlikely(cmd->aborted)) {
1637 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf014,
1638 "qla_target(%d): terminating exchange "
1639 "for aborted cmd=%p (se_cmd=%p, tag=%d)", vha->vp_idx, cmd,
1642 cmd->state = QLA_TGT_STATE_ABORTED;
1644 qlt_send_term_exchange(vha, cmd, &cmd->atio, 0);
1646 /* !! At this point cmd could be already freed !! */
1647 return QLA_TGT_PRE_XMIT_RESP_CMD_ABORTED;
1650 ql_dbg(ql_dbg_tgt, vha, 0xe011, "qla_target(%d): tag=%u\n",
1651 vha->vp_idx, cmd->tag);
1655 prm->rq_result = scsi_status;
1656 prm->sense_buffer = &cmd->sense_buffer[0];
1657 prm->sense_buffer_len = TRANSPORT_SENSE_BUFFER;
1661 prm->add_status_pkt = 0;
1663 ql_dbg(ql_dbg_tgt, vha, 0xe012, "rq_result=%x, xmit_type=%x\n",
1664 prm->rq_result, xmit_type);
1666 /* Send marker if required */
1667 if (qlt_issue_marker(vha, 0) != QLA_SUCCESS)
1670 ql_dbg(ql_dbg_tgt, vha, 0xe013, "CTIO start: vha(%d)\n", vha->vp_idx);
1672 if ((xmit_type & QLA_TGT_XMIT_DATA) && qlt_has_data(cmd)) {
1673 if (qlt_pci_map_calc_cnt(prm) != 0)
1677 *full_req_cnt = prm->req_cnt;
1679 if (se_cmd->se_cmd_flags & SCF_UNDERFLOW_BIT) {
1680 prm->residual = se_cmd->residual_count;
1681 ql_dbg(ql_dbg_tgt, vha, 0xe014,
1682 "Residual underflow: %d (tag %d, "
1683 "op %x, bufflen %d, rq_result %x)\n", prm->residual,
1684 cmd->tag, se_cmd->t_task_cdb ? se_cmd->t_task_cdb[0] : 0,
1685 cmd->bufflen, prm->rq_result);
1686 prm->rq_result |= SS_RESIDUAL_UNDER;
1687 } else if (se_cmd->se_cmd_flags & SCF_OVERFLOW_BIT) {
1688 prm->residual = se_cmd->residual_count;
1689 ql_dbg(ql_dbg_tgt, vha, 0xe015,
1690 "Residual overflow: %d (tag %d, "
1691 "op %x, bufflen %d, rq_result %x)\n", prm->residual,
1692 cmd->tag, se_cmd->t_task_cdb ? se_cmd->t_task_cdb[0] : 0,
1693 cmd->bufflen, prm->rq_result);
1694 prm->rq_result |= SS_RESIDUAL_OVER;
1697 if (xmit_type & QLA_TGT_XMIT_STATUS) {
1699 * If QLA_TGT_XMIT_DATA is not set, add_status_pkt will be
1700 * ignored in *xmit_response() below
1702 if (qlt_has_data(cmd)) {
1703 if (QLA_TGT_SENSE_VALID(prm->sense_buffer) ||
1704 (IS_FWI2_CAPABLE(ha) &&
1705 (prm->rq_result != 0))) {
1706 prm->add_status_pkt = 1;
1712 ql_dbg(ql_dbg_tgt, vha, 0xe016,
1713 "req_cnt=%d, full_req_cnt=%d, add_status_pkt=%d\n",
1714 prm->req_cnt, *full_req_cnt, prm->add_status_pkt);
1719 static inline int qlt_need_explicit_conf(struct qla_hw_data *ha,
1720 struct qla_tgt_cmd *cmd, int sending_sense)
1722 if (ha->tgt.enable_class_2)
1726 return cmd->conf_compl_supported;
1728 return ha->tgt.enable_explicit_conf &&
1729 cmd->conf_compl_supported;
1732 #ifdef CONFIG_QLA_TGT_DEBUG_SRR
1734 * Original taken from the XFS code
1736 static unsigned long qlt_srr_random(void)
1739 static unsigned long RandomValue;
1740 static DEFINE_SPINLOCK(lock);
1741 /* cycles pseudo-randomly through all values between 1 and 2^31 - 2 */
1745 unsigned long flags;
1747 spin_lock_irqsave(&lock, flags);
1749 RandomValue = jiffies;
1755 rv = 16807 * lo - 2836 * hi;
1759 spin_unlock_irqrestore(&lock, flags);
1763 static void qlt_check_srr_debug(struct qla_tgt_cmd *cmd, int *xmit_type)
1765 #if 0 /* This is not a real status packets lost, so it won't lead to SRR */
1766 if ((*xmit_type & QLA_TGT_XMIT_STATUS) && (qlt_srr_random() % 200)
1768 *xmit_type &= ~QLA_TGT_XMIT_STATUS;
1769 ql_dbg(ql_dbg_tgt_mgt, cmd->vha, 0xf015,
1770 "Dropping cmd %p (tag %d) status", cmd, cmd->tag);
1774 * It's currently not possible to simulate SRRs for FCP_WRITE without
1775 * a physical link layer failure, so don't even try here..
1777 if (cmd->dma_data_direction != DMA_FROM_DEVICE)
1780 if (qlt_has_data(cmd) && (cmd->sg_cnt > 1) &&
1781 ((qlt_srr_random() % 100) == 20)) {
1783 unsigned int tot_len = 0;
1786 leave = qlt_srr_random() % cmd->sg_cnt;
1788 for (i = 0; i < leave; i++)
1789 tot_len += cmd->sg[i].length;
1791 ql_dbg(ql_dbg_tgt_mgt, cmd->vha, 0xf016,
1792 "Cutting cmd %p (tag %d) buffer"
1793 " tail to len %d, sg_cnt %d (cmd->bufflen %d,"
1794 " cmd->sg_cnt %d)", cmd, cmd->tag, tot_len, leave,
1795 cmd->bufflen, cmd->sg_cnt);
1797 cmd->bufflen = tot_len;
1798 cmd->sg_cnt = leave;
1801 if (qlt_has_data(cmd) && ((qlt_srr_random() % 100) == 70)) {
1802 unsigned int offset = qlt_srr_random() % cmd->bufflen;
1804 ql_dbg(ql_dbg_tgt_mgt, cmd->vha, 0xf017,
1805 "Cutting cmd %p (tag %d) buffer head "
1806 "to offset %d (cmd->bufflen %d)", cmd, cmd->tag, offset,
1809 *xmit_type &= ~QLA_TGT_XMIT_DATA;
1810 else if (qlt_set_data_offset(cmd, offset)) {
1811 ql_dbg(ql_dbg_tgt_mgt, cmd->vha, 0xf018,
1812 "qlt_set_data_offset() failed (tag %d)", cmd->tag);
1817 static inline void qlt_check_srr_debug(struct qla_tgt_cmd *cmd, int *xmit_type)
1821 static void qlt_24xx_init_ctio_to_isp(struct ctio7_to_24xx *ctio,
1822 struct qla_tgt_prm *prm)
1824 prm->sense_buffer_len = min_t(uint32_t, prm->sense_buffer_len,
1825 (uint32_t)sizeof(ctio->u.status1.sense_data));
1826 ctio->u.status0.flags |=
1827 __constant_cpu_to_le16(CTIO7_FLAGS_SEND_STATUS);
1828 if (qlt_need_explicit_conf(prm->tgt->ha, prm->cmd, 0)) {
1829 ctio->u.status0.flags |= __constant_cpu_to_le16(
1830 CTIO7_FLAGS_EXPLICIT_CONFORM |
1831 CTIO7_FLAGS_CONFORM_REQ);
1833 ctio->u.status0.residual = cpu_to_le32(prm->residual);
1834 ctio->u.status0.scsi_status = cpu_to_le16(prm->rq_result);
1835 if (QLA_TGT_SENSE_VALID(prm->sense_buffer)) {
1838 if (qlt_need_explicit_conf(prm->tgt->ha, prm->cmd, 1)) {
1839 if (prm->cmd->se_cmd.scsi_status != 0) {
1840 ql_dbg(ql_dbg_tgt, prm->cmd->vha, 0xe017,
1841 "Skipping EXPLICIT_CONFORM and "
1842 "CTIO7_FLAGS_CONFORM_REQ for FCP READ w/ "
1843 "non GOOD status\n");
1844 goto skip_explict_conf;
1846 ctio->u.status1.flags |= __constant_cpu_to_le16(
1847 CTIO7_FLAGS_EXPLICIT_CONFORM |
1848 CTIO7_FLAGS_CONFORM_REQ);
1851 ctio->u.status1.flags &=
1852 ~__constant_cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_0);
1853 ctio->u.status1.flags |=
1854 __constant_cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_1);
1855 ctio->u.status1.scsi_status |=
1856 __constant_cpu_to_le16(SS_SENSE_LEN_VALID);
1857 ctio->u.status1.sense_length =
1858 cpu_to_le16(prm->sense_buffer_len);
1859 for (i = 0; i < prm->sense_buffer_len/4; i++)
1860 ((uint32_t *)ctio->u.status1.sense_data)[i] =
1861 cpu_to_be32(((uint32_t *)prm->sense_buffer)[i]);
1863 if (unlikely((prm->sense_buffer_len % 4) != 0)) {
1866 ql_dbg(ql_dbg_tgt, vha, 0xe04f,
1867 "qla_target(%d): %d bytes of sense "
1868 "lost", prm->tgt->ha->vp_idx,
1869 prm->sense_buffer_len % 4);
1875 ctio->u.status1.flags &=
1876 ~__constant_cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_0);
1877 ctio->u.status1.flags |=
1878 __constant_cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_1);
1879 ctio->u.status1.sense_length = 0;
1880 memset(ctio->u.status1.sense_data, 0,
1881 sizeof(ctio->u.status1.sense_data));
1884 /* Sense with len > 24, is it possible ??? */
1888 * Callback to setup response of xmit_type of QLA_TGT_XMIT_DATA and *
1889 * QLA_TGT_XMIT_STATUS for >= 24xx silicon
1891 int qlt_xmit_response(struct qla_tgt_cmd *cmd, int xmit_type,
1892 uint8_t scsi_status)
1894 struct scsi_qla_host *vha = cmd->vha;
1895 struct qla_hw_data *ha = vha->hw;
1896 struct ctio7_to_24xx *pkt;
1897 struct qla_tgt_prm prm;
1898 uint32_t full_req_cnt = 0;
1899 unsigned long flags = 0;
1902 memset(&prm, 0, sizeof(prm));
1903 qlt_check_srr_debug(cmd, &xmit_type);
1905 ql_dbg(ql_dbg_tgt, cmd->vha, 0xe018,
1906 "is_send_status=%d, cmd->bufflen=%d, cmd->sg_cnt=%d, "
1907 "cmd->dma_data_direction=%d\n", (xmit_type & QLA_TGT_XMIT_STATUS) ?
1908 1 : 0, cmd->bufflen, cmd->sg_cnt, cmd->dma_data_direction);
1910 res = qlt_pre_xmit_response(cmd, &prm, xmit_type, scsi_status,
1912 if (unlikely(res != 0)) {
1913 if (res == QLA_TGT_PRE_XMIT_RESP_CMD_ABORTED)
1919 spin_lock_irqsave(&ha->hardware_lock, flags);
1921 /* Does F/W have an IOCBs for this request */
1922 res = qlt_check_reserve_free_req(vha, full_req_cnt);
1924 goto out_unmap_unlock;
1926 res = qlt_24xx_build_ctio_pkt(&prm, vha);
1927 if (unlikely(res != 0))
1928 goto out_unmap_unlock;
1931 pkt = (struct ctio7_to_24xx *)prm.pkt;
1933 if (qlt_has_data(cmd) && (xmit_type & QLA_TGT_XMIT_DATA)) {
1934 pkt->u.status0.flags |=
1935 __constant_cpu_to_le16(CTIO7_FLAGS_DATA_IN |
1936 CTIO7_FLAGS_STATUS_MODE_0);
1938 qlt_load_data_segments(&prm, vha);
1940 if (prm.add_status_pkt == 0) {
1941 if (xmit_type & QLA_TGT_XMIT_STATUS) {
1942 pkt->u.status0.scsi_status =
1943 cpu_to_le16(prm.rq_result);
1944 pkt->u.status0.residual =
1945 cpu_to_le32(prm.residual);
1946 pkt->u.status0.flags |= __constant_cpu_to_le16(
1947 CTIO7_FLAGS_SEND_STATUS);
1948 if (qlt_need_explicit_conf(ha, cmd, 0)) {
1949 pkt->u.status0.flags |=
1950 __constant_cpu_to_le16(
1951 CTIO7_FLAGS_EXPLICIT_CONFORM |
1952 CTIO7_FLAGS_CONFORM_REQ);
1958 * We have already made sure that there is sufficient
1959 * amount of request entries to not drop HW lock in
1962 struct ctio7_to_24xx *ctio =
1963 (struct ctio7_to_24xx *)qlt_get_req_pkt(vha);
1965 ql_dbg(ql_dbg_tgt, vha, 0xe019,
1966 "Building additional status packet\n");
1968 memcpy(ctio, pkt, sizeof(*ctio));
1969 ctio->entry_count = 1;
1970 ctio->dseg_count = 0;
1971 ctio->u.status1.flags &= ~__constant_cpu_to_le16(
1972 CTIO7_FLAGS_DATA_IN);
1974 /* Real finish is ctio_m1's finish */
1975 pkt->handle |= CTIO_INTERMEDIATE_HANDLE_MARK;
1976 pkt->u.status0.flags |= __constant_cpu_to_le16(
1977 CTIO7_FLAGS_DONT_RET_CTIO);
1978 qlt_24xx_init_ctio_to_isp((struct ctio7_to_24xx *)ctio,
1980 pr_debug("Status CTIO7: %p\n", ctio);
1983 qlt_24xx_init_ctio_to_isp(pkt, &prm);
1986 cmd->state = QLA_TGT_STATE_PROCESSED; /* Mid-level is done processing */
1988 ql_dbg(ql_dbg_tgt, vha, 0xe01a,
1989 "Xmitting CTIO7 response pkt for 24xx: %p scsi_status: 0x%02x\n",
1992 qla2x00_start_iocbs(vha, vha->req);
1993 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1999 qlt_unmap_sg(vha, cmd);
2000 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2004 EXPORT_SYMBOL(qlt_xmit_response);
2006 int qlt_rdy_to_xfer(struct qla_tgt_cmd *cmd)
2008 struct ctio7_to_24xx *pkt;
2009 struct scsi_qla_host *vha = cmd->vha;
2010 struct qla_hw_data *ha = vha->hw;
2011 struct qla_tgt *tgt = cmd->tgt;
2012 struct qla_tgt_prm prm;
2013 unsigned long flags;
2016 memset(&prm, 0, sizeof(prm));
2022 /* Send marker if required */
2023 if (qlt_issue_marker(vha, 0) != QLA_SUCCESS)
2026 ql_dbg(ql_dbg_tgt, vha, 0xe01b, "CTIO_start: vha(%d)",
2029 /* Calculate number of entries and segments required */
2030 if (qlt_pci_map_calc_cnt(&prm) != 0)
2033 spin_lock_irqsave(&ha->hardware_lock, flags);
2035 /* Does F/W have an IOCBs for this request */
2036 res = qlt_check_reserve_free_req(vha, prm.req_cnt);
2038 goto out_unlock_free_unmap;
2040 res = qlt_24xx_build_ctio_pkt(&prm, vha);
2041 if (unlikely(res != 0))
2042 goto out_unlock_free_unmap;
2043 pkt = (struct ctio7_to_24xx *)prm.pkt;
2044 pkt->u.status0.flags |= __constant_cpu_to_le16(CTIO7_FLAGS_DATA_OUT |
2045 CTIO7_FLAGS_STATUS_MODE_0);
2046 qlt_load_data_segments(&prm, vha);
2048 cmd->state = QLA_TGT_STATE_NEED_DATA;
2050 qla2x00_start_iocbs(vha, vha->req);
2051 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2055 out_unlock_free_unmap:
2057 qlt_unmap_sg(vha, cmd);
2058 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2062 EXPORT_SYMBOL(qlt_rdy_to_xfer);
2064 /* If hardware_lock held on entry, might drop it, then reaquire */
2065 /* This function sends the appropriate CTIO to ISP 2xxx or 24xx */
2066 static int __qlt_send_term_exchange(struct scsi_qla_host *vha,
2067 struct qla_tgt_cmd *cmd,
2068 struct atio_from_isp *atio)
2070 struct ctio7_to_24xx *ctio24;
2071 struct qla_hw_data *ha = vha->hw;
2075 ql_dbg(ql_dbg_tgt, vha, 0xe01c, "Sending TERM EXCH CTIO (ha=%p)\n", ha);
2077 pkt = (request_t *)qla2x00_alloc_iocbs(vha, NULL);
2079 ql_dbg(ql_dbg_tgt, vha, 0xe050,
2080 "qla_target(%d): %s failed: unable to allocate "
2081 "request packet\n", vha->vp_idx, __func__);
2086 if (cmd->state < QLA_TGT_STATE_PROCESSED) {
2087 ql_dbg(ql_dbg_tgt, vha, 0xe051,
2088 "qla_target(%d): Terminating cmd %p with "
2089 "incorrect state %d\n", vha->vp_idx, cmd,
2095 pkt->entry_count = 1;
2096 pkt->handle = QLA_TGT_SKIP_HANDLE | CTIO_COMPLETION_HANDLE_MARK;
2098 ctio24 = (struct ctio7_to_24xx *)pkt;
2099 ctio24->entry_type = CTIO_TYPE7;
2100 ctio24->nport_handle = cmd ? cmd->loop_id : CTIO7_NHANDLE_UNRECOGNIZED;
2101 ctio24->timeout = __constant_cpu_to_le16(QLA_TGT_TIMEOUT);
2102 ctio24->vp_index = vha->vp_idx;
2103 ctio24->initiator_id[0] = atio->u.isp24.fcp_hdr.s_id[2];
2104 ctio24->initiator_id[1] = atio->u.isp24.fcp_hdr.s_id[1];
2105 ctio24->initiator_id[2] = atio->u.isp24.fcp_hdr.s_id[0];
2106 ctio24->exchange_addr = atio->u.isp24.exchange_addr;
2107 ctio24->u.status1.flags = (atio->u.isp24.attr << 9) |
2108 __constant_cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_1 |
2109 CTIO7_FLAGS_TERMINATE);
2110 ctio24->u.status1.ox_id = swab16(atio->u.isp24.fcp_hdr.ox_id);
2112 /* Most likely, it isn't needed */
2113 ctio24->u.status1.residual = get_unaligned((uint32_t *)
2114 &atio->u.isp24.fcp_cmnd.add_cdb[
2115 atio->u.isp24.fcp_cmnd.add_cdb_len]);
2116 if (ctio24->u.status1.residual != 0)
2117 ctio24->u.status1.scsi_status |= SS_RESIDUAL_UNDER;
2119 qla2x00_start_iocbs(vha, vha->req);
2123 static void qlt_send_term_exchange(struct scsi_qla_host *vha,
2124 struct qla_tgt_cmd *cmd, struct atio_from_isp *atio, int ha_locked)
2126 unsigned long flags;
2129 if (qlt_issue_marker(vha, ha_locked) < 0)
2133 rc = __qlt_send_term_exchange(vha, cmd, atio);
2136 spin_lock_irqsave(&vha->hw->hardware_lock, flags);
2137 rc = __qlt_send_term_exchange(vha, cmd, atio);
2138 spin_unlock_irqrestore(&vha->hw->hardware_lock, flags);
2141 if (!ha_locked && !in_interrupt())
2142 msleep(250); /* just in case */
2144 vha->hw->tgt.tgt_ops->free_cmd(cmd);
2148 void qlt_free_cmd(struct qla_tgt_cmd *cmd)
2150 BUG_ON(cmd->sg_mapped);
2152 if (unlikely(cmd->free_sg))
2154 kmem_cache_free(qla_tgt_cmd_cachep, cmd);
2156 EXPORT_SYMBOL(qlt_free_cmd);
2158 /* ha->hardware_lock supposed to be held on entry */
2159 static int qlt_prepare_srr_ctio(struct scsi_qla_host *vha,
2160 struct qla_tgt_cmd *cmd, void *ctio)
2162 struct qla_tgt_srr_ctio *sc;
2163 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
2164 struct qla_tgt_srr_imm *imm;
2168 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf019,
2169 "qla_target(%d): CTIO with SRR status received\n", vha->vp_idx);
2172 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf055,
2173 "qla_target(%d): SRR CTIO, but ctio is NULL\n",
2178 sc = kzalloc(sizeof(*sc), GFP_ATOMIC);
2181 /* IRQ is already OFF */
2182 spin_lock(&tgt->srr_lock);
2183 sc->srr_id = tgt->ctio_srr_id;
2184 list_add_tail(&sc->srr_list_entry,
2185 &tgt->srr_ctio_list);
2186 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01a,
2187 "CTIO SRR %p added (id %d)\n", sc, sc->srr_id);
2188 if (tgt->imm_srr_id == tgt->ctio_srr_id) {
2190 list_for_each_entry(imm, &tgt->srr_imm_list,
2192 if (imm->srr_id == sc->srr_id) {
2198 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01b,
2199 "Scheduling srr work\n");
2200 schedule_work(&tgt->srr_work);
2202 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf056,
2203 "qla_target(%d): imm_srr_id "
2204 "== ctio_srr_id (%d), but there is no "
2205 "corresponding SRR IMM, deleting CTIO "
2206 "SRR %p\n", vha->vp_idx,
2207 tgt->ctio_srr_id, sc);
2208 list_del(&sc->srr_list_entry);
2209 spin_unlock(&tgt->srr_lock);
2215 spin_unlock(&tgt->srr_lock);
2217 struct qla_tgt_srr_imm *ti;
2219 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf057,
2220 "qla_target(%d): Unable to allocate SRR CTIO entry\n",
2222 spin_lock(&tgt->srr_lock);
2223 list_for_each_entry_safe(imm, ti, &tgt->srr_imm_list,
2225 if (imm->srr_id == tgt->ctio_srr_id) {
2226 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01c,
2227 "IMM SRR %p deleted (id %d)\n",
2229 list_del(&imm->srr_list_entry);
2230 qlt_reject_free_srr_imm(vha, imm, 1);
2233 spin_unlock(&tgt->srr_lock);
2242 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
2244 static int qlt_term_ctio_exchange(struct scsi_qla_host *vha, void *ctio,
2245 struct qla_tgt_cmd *cmd, uint32_t status)
2250 struct ctio7_from_24xx *c = (struct ctio7_from_24xx *)ctio;
2252 __constant_cpu_to_le16(OF_TERM_EXCH));
2257 qlt_send_term_exchange(vha, cmd, &cmd->atio, 1);
2262 /* ha->hardware_lock supposed to be held on entry */
2263 static inline struct qla_tgt_cmd *qlt_get_cmd(struct scsi_qla_host *vha,
2266 struct qla_hw_data *ha = vha->hw;
2269 if (ha->tgt.cmds[handle] != NULL) {
2270 struct qla_tgt_cmd *cmd = ha->tgt.cmds[handle];
2271 ha->tgt.cmds[handle] = NULL;
2277 /* ha->hardware_lock supposed to be held on entry */
2278 static struct qla_tgt_cmd *qlt_ctio_to_cmd(struct scsi_qla_host *vha,
2279 uint32_t handle, void *ctio)
2281 struct qla_tgt_cmd *cmd = NULL;
2283 /* Clear out internal marks */
2284 handle &= ~(CTIO_COMPLETION_HANDLE_MARK |
2285 CTIO_INTERMEDIATE_HANDLE_MARK);
2287 if (handle != QLA_TGT_NULL_HANDLE) {
2288 if (unlikely(handle == QLA_TGT_SKIP_HANDLE)) {
2289 ql_dbg(ql_dbg_tgt, vha, 0xe01d, "%s",
2290 "SKIP_HANDLE CTIO\n");
2293 /* handle-1 is actually used */
2294 if (unlikely(handle > DEFAULT_OUTSTANDING_COMMANDS)) {
2295 ql_dbg(ql_dbg_tgt, vha, 0xe052,
2296 "qla_target(%d): Wrong handle %x received\n",
2297 vha->vp_idx, handle);
2300 cmd = qlt_get_cmd(vha, handle);
2301 if (unlikely(cmd == NULL)) {
2302 ql_dbg(ql_dbg_tgt, vha, 0xe053,
2303 "qla_target(%d): Suspicious: unable to "
2304 "find the command with handle %x\n", vha->vp_idx,
2308 } else if (ctio != NULL) {
2309 /* We can't get loop ID from CTIO7 */
2310 ql_dbg(ql_dbg_tgt, vha, 0xe054,
2311 "qla_target(%d): Wrong CTIO received: QLA24xx doesn't "
2312 "support NULL handles\n", vha->vp_idx);
2320 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
2322 static void qlt_do_ctio_completion(struct scsi_qla_host *vha, uint32_t handle,
2323 uint32_t status, void *ctio)
2325 struct qla_hw_data *ha = vha->hw;
2326 struct se_cmd *se_cmd;
2327 struct target_core_fabric_ops *tfo;
2328 struct qla_tgt_cmd *cmd;
2330 ql_dbg(ql_dbg_tgt, vha, 0xe01e,
2331 "qla_target(%d): handle(ctio %p status %#x) <- %08x\n",
2332 vha->vp_idx, ctio, status, handle);
2334 if (handle & CTIO_INTERMEDIATE_HANDLE_MARK) {
2335 /* That could happen only in case of an error/reset/abort */
2336 if (status != CTIO_SUCCESS) {
2337 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01d,
2338 "Intermediate CTIO received"
2339 " (status %x)\n", status);
2344 cmd = qlt_ctio_to_cmd(vha, handle, ctio);
2348 se_cmd = &cmd->se_cmd;
2349 tfo = se_cmd->se_tfo;
2352 qlt_unmap_sg(vha, cmd);
2354 if (unlikely(status != CTIO_SUCCESS)) {
2355 switch (status & 0xFFFF) {
2356 case CTIO_LIP_RESET:
2357 case CTIO_TARGET_RESET:
2360 case CTIO_INVALID_RX_ID:
2362 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf058,
2363 "qla_target(%d): CTIO with "
2364 "status %#x received, state %x, se_cmd %p, "
2365 "(LIP_RESET=e, ABORTED=2, TARGET_RESET=17, "
2366 "TIMEOUT=b, INVALID_RX_ID=8)\n", vha->vp_idx,
2367 status, cmd->state, se_cmd);
2370 case CTIO_PORT_LOGGED_OUT:
2371 case CTIO_PORT_UNAVAILABLE:
2372 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf059,
2373 "qla_target(%d): CTIO with PORT LOGGED "
2374 "OUT (29) or PORT UNAVAILABLE (28) status %x "
2375 "received (state %x, se_cmd %p)\n", vha->vp_idx,
2376 status, cmd->state, se_cmd);
2379 case CTIO_SRR_RECEIVED:
2380 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05a,
2381 "qla_target(%d): CTIO with SRR_RECEIVED"
2382 " status %x received (state %x, se_cmd %p)\n",
2383 vha->vp_idx, status, cmd->state, se_cmd);
2384 if (qlt_prepare_srr_ctio(vha, cmd, ctio) != 0)
2390 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05b,
2391 "qla_target(%d): CTIO with error status "
2392 "0x%x received (state %x, se_cmd %p\n",
2393 vha->vp_idx, status, cmd->state, se_cmd);
2397 if (cmd->state != QLA_TGT_STATE_NEED_DATA)
2398 if (qlt_term_ctio_exchange(vha, ctio, cmd, status))
2402 if (cmd->state == QLA_TGT_STATE_PROCESSED) {
2403 ql_dbg(ql_dbg_tgt, vha, 0xe01f, "Command %p finished\n", cmd);
2404 } else if (cmd->state == QLA_TGT_STATE_NEED_DATA) {
2407 cmd->state = QLA_TGT_STATE_DATA_IN;
2409 if (unlikely(status != CTIO_SUCCESS))
2412 cmd->write_data_transferred = 1;
2414 ql_dbg(ql_dbg_tgt, vha, 0xe020,
2415 "Data received, context %x, rx_status %d\n",
2418 ha->tgt.tgt_ops->handle_data(cmd);
2420 } else if (cmd->state == QLA_TGT_STATE_ABORTED) {
2421 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01e,
2422 "Aborted command %p (tag %d) finished\n", cmd, cmd->tag);
2424 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05c,
2425 "qla_target(%d): A command in state (%d) should "
2426 "not return a CTIO complete\n", vha->vp_idx, cmd->state);
2429 if (unlikely(status != CTIO_SUCCESS)) {
2430 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01f, "Finishing failed CTIO\n");
2434 ha->tgt.tgt_ops->free_cmd(cmd);
2437 static inline int qlt_get_fcp_task_attr(struct scsi_qla_host *vha,
2442 switch (task_codes) {
2443 case ATIO_SIMPLE_QUEUE:
2444 fcp_task_attr = MSG_SIMPLE_TAG;
2446 case ATIO_HEAD_OF_QUEUE:
2447 fcp_task_attr = MSG_HEAD_TAG;
2449 case ATIO_ORDERED_QUEUE:
2450 fcp_task_attr = MSG_ORDERED_TAG;
2452 case ATIO_ACA_QUEUE:
2453 fcp_task_attr = MSG_ACA_TAG;
2456 fcp_task_attr = MSG_SIMPLE_TAG;
2459 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05d,
2460 "qla_target: unknown task code %x, use ORDERED instead\n",
2462 fcp_task_attr = MSG_ORDERED_TAG;
2466 return fcp_task_attr;
2469 static struct qla_tgt_sess *qlt_make_local_sess(struct scsi_qla_host *,
2472 * Process context for I/O path into tcm_qla2xxx code
2474 static void qlt_do_work(struct work_struct *work)
2476 struct qla_tgt_cmd *cmd = container_of(work, struct qla_tgt_cmd, work);
2477 scsi_qla_host_t *vha = cmd->vha;
2478 struct qla_hw_data *ha = vha->hw;
2479 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
2480 struct qla_tgt_sess *sess = NULL;
2481 struct atio_from_isp *atio = &cmd->atio;
2483 unsigned long flags;
2484 uint32_t data_length;
2485 int ret, fcp_task_attr, data_dir, bidi = 0;
2490 spin_lock_irqsave(&ha->hardware_lock, flags);
2491 sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha,
2492 atio->u.isp24.fcp_hdr.s_id);
2493 /* Do kref_get() before dropping qla_hw_data->hardware_lock. */
2495 kref_get(&sess->se_sess->sess_kref);
2496 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2498 if (unlikely(!sess)) {
2499 uint8_t *s_id = atio->u.isp24.fcp_hdr.s_id;
2501 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf022,
2502 "qla_target(%d): Unable to find wwn login"
2503 " (s_id %x:%x:%x), trying to create it manually\n",
2504 vha->vp_idx, s_id[0], s_id[1], s_id[2]);
2506 if (atio->u.raw.entry_count > 1) {
2507 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf023,
2508 "Dropping multy entry cmd %p\n", cmd);
2512 mutex_lock(&vha->vha_tgt.tgt_mutex);
2513 sess = qlt_make_local_sess(vha, s_id);
2514 /* sess has an extra creation ref. */
2515 mutex_unlock(&vha->vha_tgt.tgt_mutex);
2522 cmd->loop_id = sess->loop_id;
2523 cmd->conf_compl_supported = sess->conf_compl_supported;
2525 cdb = &atio->u.isp24.fcp_cmnd.cdb[0];
2526 cmd->tag = atio->u.isp24.exchange_addr;
2527 cmd->unpacked_lun = scsilun_to_int(
2528 (struct scsi_lun *)&atio->u.isp24.fcp_cmnd.lun);
2530 if (atio->u.isp24.fcp_cmnd.rddata &&
2531 atio->u.isp24.fcp_cmnd.wrdata) {
2533 data_dir = DMA_TO_DEVICE;
2534 } else if (atio->u.isp24.fcp_cmnd.rddata)
2535 data_dir = DMA_FROM_DEVICE;
2536 else if (atio->u.isp24.fcp_cmnd.wrdata)
2537 data_dir = DMA_TO_DEVICE;
2539 data_dir = DMA_NONE;
2541 fcp_task_attr = qlt_get_fcp_task_attr(vha,
2542 atio->u.isp24.fcp_cmnd.task_attr);
2543 data_length = be32_to_cpu(get_unaligned((uint32_t *)
2544 &atio->u.isp24.fcp_cmnd.add_cdb[
2545 atio->u.isp24.fcp_cmnd.add_cdb_len]));
2547 ql_dbg(ql_dbg_tgt, vha, 0xe022,
2548 "qla_target: START qla command: %p lun: 0x%04x (tag %d)\n",
2549 cmd, cmd->unpacked_lun, cmd->tag);
2551 ret = vha->hw->tgt.tgt_ops->handle_cmd(vha, cmd, cdb, data_length,
2552 fcp_task_attr, data_dir, bidi);
2556 * Drop extra session reference from qla_tgt_handle_cmd_for_atio*(
2558 spin_lock_irqsave(&ha->hardware_lock, flags);
2559 ha->tgt.tgt_ops->put_sess(sess);
2560 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2564 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf020, "Terminating work cmd %p", cmd);
2566 * cmd has not sent to target yet, so pass NULL as the second
2567 * argument to qlt_send_term_exchange() and free the memory here.
2569 spin_lock_irqsave(&ha->hardware_lock, flags);
2570 qlt_send_term_exchange(vha, NULL, &cmd->atio, 1);
2571 kmem_cache_free(qla_tgt_cmd_cachep, cmd);
2573 ha->tgt.tgt_ops->put_sess(sess);
2574 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2577 /* ha->hardware_lock supposed to be held on entry */
2578 static int qlt_handle_cmd_for_atio(struct scsi_qla_host *vha,
2579 struct atio_from_isp *atio)
2581 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
2582 struct qla_tgt_cmd *cmd;
2584 if (unlikely(tgt->tgt_stop)) {
2585 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf021,
2586 "New command while device %p is shutting down\n", tgt);
2590 cmd = kmem_cache_zalloc(qla_tgt_cmd_cachep, GFP_ATOMIC);
2592 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05e,
2593 "qla_target(%d): Allocation of cmd failed\n", vha->vp_idx);
2597 INIT_LIST_HEAD(&cmd->cmd_list);
2599 memcpy(&cmd->atio, atio, sizeof(*atio));
2600 cmd->state = QLA_TGT_STATE_NEW;
2601 cmd->tgt = vha->vha_tgt.qla_tgt;
2604 INIT_WORK(&cmd->work, qlt_do_work);
2605 queue_work(qla_tgt_wq, &cmd->work);
2610 /* ha->hardware_lock supposed to be held on entry */
2611 static int qlt_issue_task_mgmt(struct qla_tgt_sess *sess, uint32_t lun,
2612 int fn, void *iocb, int flags)
2614 struct scsi_qla_host *vha = sess->vha;
2615 struct qla_hw_data *ha = vha->hw;
2616 struct qla_tgt_mgmt_cmd *mcmd;
2620 mcmd = mempool_alloc(qla_tgt_mgmt_cmd_mempool, GFP_ATOMIC);
2622 ql_dbg(ql_dbg_tgt_tmr, vha, 0x10009,
2623 "qla_target(%d): Allocation of management "
2624 "command failed, some commands and their data could "
2625 "leak\n", vha->vp_idx);
2628 memset(mcmd, 0, sizeof(*mcmd));
2632 memcpy(&mcmd->orig_iocb.imm_ntfy, iocb,
2633 sizeof(mcmd->orig_iocb.imm_ntfy));
2635 mcmd->tmr_func = fn;
2636 mcmd->flags = flags;
2639 case QLA_TGT_CLEAR_ACA:
2640 ql_dbg(ql_dbg_tgt_tmr, vha, 0x10000,
2641 "qla_target(%d): CLEAR_ACA received\n", sess->vha->vp_idx);
2642 tmr_func = TMR_CLEAR_ACA;
2645 case QLA_TGT_TARGET_RESET:
2646 ql_dbg(ql_dbg_tgt_tmr, vha, 0x10001,
2647 "qla_target(%d): TARGET_RESET received\n",
2649 tmr_func = TMR_TARGET_WARM_RESET;
2652 case QLA_TGT_LUN_RESET:
2653 ql_dbg(ql_dbg_tgt_tmr, vha, 0x10002,
2654 "qla_target(%d): LUN_RESET received\n", sess->vha->vp_idx);
2655 tmr_func = TMR_LUN_RESET;
2658 case QLA_TGT_CLEAR_TS:
2659 ql_dbg(ql_dbg_tgt_tmr, vha, 0x10003,
2660 "qla_target(%d): CLEAR_TS received\n", sess->vha->vp_idx);
2661 tmr_func = TMR_CLEAR_TASK_SET;
2664 case QLA_TGT_ABORT_TS:
2665 ql_dbg(ql_dbg_tgt_tmr, vha, 0x10004,
2666 "qla_target(%d): ABORT_TS received\n", sess->vha->vp_idx);
2667 tmr_func = TMR_ABORT_TASK_SET;
2670 case QLA_TGT_ABORT_ALL:
2671 ql_dbg(ql_dbg_tgt_tmr, vha, 0x10005,
2672 "qla_target(%d): Doing ABORT_ALL_TASKS\n",
2677 case QLA_TGT_ABORT_ALL_SESS:
2678 ql_dbg(ql_dbg_tgt_tmr, vha, 0x10006,
2679 "qla_target(%d): Doing ABORT_ALL_TASKS_SESS\n",
2684 case QLA_TGT_NEXUS_LOSS_SESS:
2685 ql_dbg(ql_dbg_tgt_tmr, vha, 0x10007,
2686 "qla_target(%d): Doing NEXUS_LOSS_SESS\n",
2691 case QLA_TGT_NEXUS_LOSS:
2692 ql_dbg(ql_dbg_tgt_tmr, vha, 0x10008,
2693 "qla_target(%d): Doing NEXUS_LOSS\n", sess->vha->vp_idx);
2698 ql_dbg(ql_dbg_tgt_tmr, vha, 0x1000a,
2699 "qla_target(%d): Unknown task mgmt fn 0x%x\n",
2700 sess->vha->vp_idx, fn);
2701 mempool_free(mcmd, qla_tgt_mgmt_cmd_mempool);
2705 res = ha->tgt.tgt_ops->handle_tmr(mcmd, lun, tmr_func, 0);
2707 ql_dbg(ql_dbg_tgt_tmr, vha, 0x1000b,
2708 "qla_target(%d): tgt.tgt_ops->handle_tmr() failed: %d\n",
2709 sess->vha->vp_idx, res);
2710 mempool_free(mcmd, qla_tgt_mgmt_cmd_mempool);
2717 /* ha->hardware_lock supposed to be held on entry */
2718 static int qlt_handle_task_mgmt(struct scsi_qla_host *vha, void *iocb)
2720 struct atio_from_isp *a = (struct atio_from_isp *)iocb;
2721 struct qla_hw_data *ha = vha->hw;
2722 struct qla_tgt *tgt;
2723 struct qla_tgt_sess *sess;
2724 uint32_t lun, unpacked_lun;
2727 tgt = vha->vha_tgt.qla_tgt;
2729 lun = a->u.isp24.fcp_cmnd.lun;
2730 lun_size = sizeof(a->u.isp24.fcp_cmnd.lun);
2731 fn = a->u.isp24.fcp_cmnd.task_mgmt_flags;
2732 sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha,
2733 a->u.isp24.fcp_hdr.s_id);
2734 unpacked_lun = scsilun_to_int((struct scsi_lun *)&lun);
2737 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf024,
2738 "qla_target(%d): task mgmt fn 0x%x for "
2739 "non-existant session\n", vha->vp_idx, fn);
2740 return qlt_sched_sess_work(tgt, QLA_TGT_SESS_WORK_TM, iocb,
2741 sizeof(struct atio_from_isp));
2744 return qlt_issue_task_mgmt(sess, unpacked_lun, fn, iocb, 0);
2747 /* ha->hardware_lock supposed to be held on entry */
2748 static int __qlt_abort_task(struct scsi_qla_host *vha,
2749 struct imm_ntfy_from_isp *iocb, struct qla_tgt_sess *sess)
2751 struct atio_from_isp *a = (struct atio_from_isp *)iocb;
2752 struct qla_hw_data *ha = vha->hw;
2753 struct qla_tgt_mgmt_cmd *mcmd;
2754 uint32_t lun, unpacked_lun;
2757 mcmd = mempool_alloc(qla_tgt_mgmt_cmd_mempool, GFP_ATOMIC);
2759 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05f,
2760 "qla_target(%d): %s: Allocation of ABORT cmd failed\n",
2761 vha->vp_idx, __func__);
2764 memset(mcmd, 0, sizeof(*mcmd));
2767 memcpy(&mcmd->orig_iocb.imm_ntfy, iocb,
2768 sizeof(mcmd->orig_iocb.imm_ntfy));
2770 lun = a->u.isp24.fcp_cmnd.lun;
2771 unpacked_lun = scsilun_to_int((struct scsi_lun *)&lun);
2773 rc = ha->tgt.tgt_ops->handle_tmr(mcmd, unpacked_lun, TMR_ABORT_TASK,
2774 le16_to_cpu(iocb->u.isp2x.seq_id));
2776 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf060,
2777 "qla_target(%d): tgt_ops->handle_tmr() failed: %d\n",
2779 mempool_free(mcmd, qla_tgt_mgmt_cmd_mempool);
2786 /* ha->hardware_lock supposed to be held on entry */
2787 static int qlt_abort_task(struct scsi_qla_host *vha,
2788 struct imm_ntfy_from_isp *iocb)
2790 struct qla_hw_data *ha = vha->hw;
2791 struct qla_tgt_sess *sess;
2794 loop_id = GET_TARGET_ID(ha, (struct atio_from_isp *)iocb);
2796 sess = ha->tgt.tgt_ops->find_sess_by_loop_id(vha, loop_id);
2798 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf025,
2799 "qla_target(%d): task abort for unexisting "
2800 "session\n", vha->vp_idx);
2801 return qlt_sched_sess_work(vha->vha_tgt.qla_tgt,
2802 QLA_TGT_SESS_WORK_ABORT, iocb, sizeof(*iocb));
2805 return __qlt_abort_task(vha, iocb, sess);
2809 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
2811 static int qlt_24xx_handle_els(struct scsi_qla_host *vha,
2812 struct imm_ntfy_from_isp *iocb)
2816 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf026,
2817 "qla_target(%d): Port ID: 0x%3phC ELS opcode: 0x%02x\n",
2818 vha->vp_idx, iocb->u.isp24.port_id, iocb->u.isp24.status_subcode);
2820 switch (iocb->u.isp24.status_subcode) {
2826 res = qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS_SESS);
2831 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
2832 if (tgt->link_reinit_iocb_pending) {
2833 qlt_send_notify_ack(vha, &tgt->link_reinit_iocb,
2835 tgt->link_reinit_iocb_pending = 0;
2837 res = 1; /* send notify ack */
2842 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf061,
2843 "qla_target(%d): Unsupported ELS command %x "
2844 "received\n", vha->vp_idx, iocb->u.isp24.status_subcode);
2845 res = qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS_SESS);
2852 static int qlt_set_data_offset(struct qla_tgt_cmd *cmd, uint32_t offset)
2854 struct scatterlist *sg, *sgp, *sg_srr, *sg_srr_start = NULL;
2855 size_t first_offset = 0, rem_offset = offset, tmp = 0;
2856 int i, sg_srr_cnt, bufflen = 0;
2858 ql_dbg(ql_dbg_tgt, cmd->vha, 0xe023,
2859 "Entering qla_tgt_set_data_offset: cmd: %p, cmd->sg: %p, "
2860 "cmd->sg_cnt: %u, direction: %d\n",
2861 cmd, cmd->sg, cmd->sg_cnt, cmd->dma_data_direction);
2864 * FIXME: Reject non zero SRR relative offset until we can test
2865 * this code properly.
2867 pr_debug("Rejecting non zero SRR rel_offs: %u\n", offset);
2870 if (!cmd->sg || !cmd->sg_cnt) {
2871 ql_dbg(ql_dbg_tgt, cmd->vha, 0xe055,
2872 "Missing cmd->sg or zero cmd->sg_cnt in"
2873 " qla_tgt_set_data_offset\n");
2877 * Walk the current cmd->sg list until we locate the new sg_srr_start
2879 for_each_sg(cmd->sg, sg, cmd->sg_cnt, i) {
2880 ql_dbg(ql_dbg_tgt, cmd->vha, 0xe024,
2881 "sg[%d]: %p page: %p, length: %d, offset: %d\n",
2882 i, sg, sg_page(sg), sg->length, sg->offset);
2884 if ((sg->length + tmp) > offset) {
2885 first_offset = rem_offset;
2887 ql_dbg(ql_dbg_tgt, cmd->vha, 0xe025,
2888 "Found matching sg[%d], using %p as sg_srr_start, "
2889 "and using first_offset: %zu\n", i, sg,
2894 rem_offset -= sg->length;
2897 if (!sg_srr_start) {
2898 ql_dbg(ql_dbg_tgt, cmd->vha, 0xe056,
2899 "Unable to locate sg_srr_start for offset: %u\n", offset);
2902 sg_srr_cnt = (cmd->sg_cnt - i);
2904 sg_srr = kzalloc(sizeof(struct scatterlist) * sg_srr_cnt, GFP_KERNEL);
2906 ql_dbg(ql_dbg_tgt, cmd->vha, 0xe057,
2907 "Unable to allocate sgp\n");
2910 sg_init_table(sg_srr, sg_srr_cnt);
2913 * Walk the remaining list for sg_srr_start, mapping to the newly
2914 * allocated sg_srr taking first_offset into account.
2916 for_each_sg(sg_srr_start, sg, sg_srr_cnt, i) {
2918 sg_set_page(sgp, sg_page(sg),
2919 (sg->length - first_offset), first_offset);
2922 sg_set_page(sgp, sg_page(sg), sg->length, 0);
2924 bufflen += sgp->length;
2932 cmd->sg_cnt = sg_srr_cnt;
2933 cmd->bufflen = bufflen;
2934 cmd->offset += offset;
2937 ql_dbg(ql_dbg_tgt, cmd->vha, 0xe026, "New cmd->sg: %p\n", cmd->sg);
2938 ql_dbg(ql_dbg_tgt, cmd->vha, 0xe027, "New cmd->sg_cnt: %u\n",
2940 ql_dbg(ql_dbg_tgt, cmd->vha, 0xe028, "New cmd->bufflen: %u\n",
2942 ql_dbg(ql_dbg_tgt, cmd->vha, 0xe029, "New cmd->offset: %u\n",
2945 if (cmd->sg_cnt < 0)
2948 if (cmd->bufflen < 0)
2954 static inline int qlt_srr_adjust_data(struct qla_tgt_cmd *cmd,
2955 uint32_t srr_rel_offs, int *xmit_type)
2957 int res = 0, rel_offs;
2959 rel_offs = srr_rel_offs - cmd->offset;
2960 ql_dbg(ql_dbg_tgt_mgt, cmd->vha, 0xf027, "srr_rel_offs=%d, rel_offs=%d",
2961 srr_rel_offs, rel_offs);
2963 *xmit_type = QLA_TGT_XMIT_ALL;
2966 ql_dbg(ql_dbg_tgt_mgt, cmd->vha, 0xf062,
2967 "qla_target(%d): SRR rel_offs (%d) < 0",
2968 cmd->vha->vp_idx, rel_offs);
2970 } else if (rel_offs == cmd->bufflen)
2971 *xmit_type = QLA_TGT_XMIT_STATUS;
2972 else if (rel_offs > 0)
2973 res = qlt_set_data_offset(cmd, rel_offs);
2978 /* No locks, thread context */
2979 static void qlt_handle_srr(struct scsi_qla_host *vha,
2980 struct qla_tgt_srr_ctio *sctio, struct qla_tgt_srr_imm *imm)
2982 struct imm_ntfy_from_isp *ntfy =
2983 (struct imm_ntfy_from_isp *)&imm->imm_ntfy;
2984 struct qla_hw_data *ha = vha->hw;
2985 struct qla_tgt_cmd *cmd = sctio->cmd;
2986 struct se_cmd *se_cmd = &cmd->se_cmd;
2987 unsigned long flags;
2988 int xmit_type = 0, resp = 0;
2992 offset = le32_to_cpu(ntfy->u.isp24.srr_rel_offs);
2993 srr_ui = ntfy->u.isp24.srr_ui;
2995 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf028, "SRR cmd %p, srr_ui %x\n",
3000 spin_lock_irqsave(&ha->hardware_lock, flags);
3001 qlt_send_notify_ack(vha, ntfy,
3002 0, 0, 0, NOTIFY_ACK_SRR_FLAGS_ACCEPT, 0, 0);
3003 spin_unlock_irqrestore(&ha->hardware_lock, flags);
3004 xmit_type = QLA_TGT_XMIT_STATUS;
3007 case SRR_IU_DATA_IN:
3008 if (!cmd->sg || !cmd->sg_cnt) {
3009 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf063,
3010 "Unable to process SRR_IU_DATA_IN due to"
3011 " missing cmd->sg, state: %d\n", cmd->state);
3015 if (se_cmd->scsi_status != 0) {
3016 ql_dbg(ql_dbg_tgt, vha, 0xe02a,
3017 "Rejecting SRR_IU_DATA_IN with non GOOD "
3021 cmd->bufflen = se_cmd->data_length;
3023 if (qlt_has_data(cmd)) {
3024 if (qlt_srr_adjust_data(cmd, offset, &xmit_type) != 0)
3026 spin_lock_irqsave(&ha->hardware_lock, flags);
3027 qlt_send_notify_ack(vha, ntfy,
3028 0, 0, 0, NOTIFY_ACK_SRR_FLAGS_ACCEPT, 0, 0);
3029 spin_unlock_irqrestore(&ha->hardware_lock, flags);
3032 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf064,
3033 "qla_target(%d): SRR for in data for cmd "
3034 "without them (tag %d, SCSI status %d), "
3035 "reject", vha->vp_idx, cmd->tag,
3036 cmd->se_cmd.scsi_status);
3040 case SRR_IU_DATA_OUT:
3041 if (!cmd->sg || !cmd->sg_cnt) {
3042 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf065,
3043 "Unable to process SRR_IU_DATA_OUT due to"
3044 " missing cmd->sg\n");
3048 if (se_cmd->scsi_status != 0) {
3049 ql_dbg(ql_dbg_tgt, vha, 0xe02b,
3050 "Rejecting SRR_IU_DATA_OUT"
3051 " with non GOOD scsi_status\n");
3054 cmd->bufflen = se_cmd->data_length;
3056 if (qlt_has_data(cmd)) {
3057 if (qlt_srr_adjust_data(cmd, offset, &xmit_type) != 0)
3059 spin_lock_irqsave(&ha->hardware_lock, flags);
3060 qlt_send_notify_ack(vha, ntfy,
3061 0, 0, 0, NOTIFY_ACK_SRR_FLAGS_ACCEPT, 0, 0);
3062 spin_unlock_irqrestore(&ha->hardware_lock, flags);
3063 if (xmit_type & QLA_TGT_XMIT_DATA)
3064 qlt_rdy_to_xfer(cmd);
3066 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf066,
3067 "qla_target(%d): SRR for out data for cmd "
3068 "without them (tag %d, SCSI status %d), "
3069 "reject", vha->vp_idx, cmd->tag,
3070 cmd->se_cmd.scsi_status);
3075 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf067,
3076 "qla_target(%d): Unknown srr_ui value %x",
3077 vha->vp_idx, srr_ui);
3081 /* Transmit response in case of status and data-in cases */
3083 qlt_xmit_response(cmd, xmit_type, se_cmd->scsi_status);
3088 spin_lock_irqsave(&ha->hardware_lock, flags);
3089 qlt_send_notify_ack(vha, ntfy, 0, 0, 0,
3090 NOTIFY_ACK_SRR_FLAGS_REJECT,
3091 NOTIFY_ACK_SRR_REJECT_REASON_UNABLE_TO_PERFORM,
3092 NOTIFY_ACK_SRR_FLAGS_REJECT_EXPL_NO_EXPL);
3093 if (cmd->state == QLA_TGT_STATE_NEED_DATA) {
3094 cmd->state = QLA_TGT_STATE_DATA_IN;
3097 qlt_send_term_exchange(vha, cmd, &cmd->atio, 1);
3098 spin_unlock_irqrestore(&ha->hardware_lock, flags);
3101 static void qlt_reject_free_srr_imm(struct scsi_qla_host *vha,
3102 struct qla_tgt_srr_imm *imm, int ha_locked)
3104 struct qla_hw_data *ha = vha->hw;
3105 unsigned long flags = 0;
3108 spin_lock_irqsave(&ha->hardware_lock, flags);
3110 qlt_send_notify_ack(vha, (void *)&imm->imm_ntfy, 0, 0, 0,
3111 NOTIFY_ACK_SRR_FLAGS_REJECT,
3112 NOTIFY_ACK_SRR_REJECT_REASON_UNABLE_TO_PERFORM,
3113 NOTIFY_ACK_SRR_FLAGS_REJECT_EXPL_NO_EXPL);
3116 spin_unlock_irqrestore(&ha->hardware_lock, flags);
3121 static void qlt_handle_srr_work(struct work_struct *work)
3123 struct qla_tgt *tgt = container_of(work, struct qla_tgt, srr_work);
3124 struct scsi_qla_host *vha = tgt->vha;
3125 struct qla_tgt_srr_ctio *sctio;
3126 unsigned long flags;
3128 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf029, "Entering SRR work (tgt %p)\n",
3132 spin_lock_irqsave(&tgt->srr_lock, flags);
3133 list_for_each_entry(sctio, &tgt->srr_ctio_list, srr_list_entry) {
3134 struct qla_tgt_srr_imm *imm, *i, *ti;
3135 struct qla_tgt_cmd *cmd;
3136 struct se_cmd *se_cmd;
3139 list_for_each_entry_safe(i, ti, &tgt->srr_imm_list,
3141 if (i->srr_id == sctio->srr_id) {
3142 list_del(&i->srr_list_entry);
3144 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf068,
3145 "qla_target(%d): There must be "
3146 "only one IMM SRR per CTIO SRR "
3147 "(IMM SRR %p, id %d, CTIO %p\n",
3148 vha->vp_idx, i, i->srr_id, sctio);
3149 qlt_reject_free_srr_imm(tgt->vha, i, 0);
3155 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf02a,
3156 "IMM SRR %p, CTIO SRR %p (id %d)\n", imm, sctio,
3160 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf02b,
3161 "Not found matching IMM for SRR CTIO (id %d)\n",
3165 list_del(&sctio->srr_list_entry);
3167 spin_unlock_irqrestore(&tgt->srr_lock, flags);
3171 * Reset qla_tgt_cmd SRR values and SGL pointer+count to follow
3172 * tcm_qla2xxx_write_pending() and tcm_qla2xxx_queue_data_in()
3181 se_cmd = &cmd->se_cmd;
3183 cmd->sg_cnt = se_cmd->t_data_nents;
3184 cmd->sg = se_cmd->t_data_sg;
3186 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf02c,
3187 "SRR cmd %p (se_cmd %p, tag %d, op %x), "
3188 "sg_cnt=%d, offset=%d", cmd, &cmd->se_cmd, cmd->tag,
3189 se_cmd->t_task_cdb[0], cmd->sg_cnt, cmd->offset);
3191 qlt_handle_srr(vha, sctio, imm);
3197 spin_unlock_irqrestore(&tgt->srr_lock, flags);
3200 /* ha->hardware_lock supposed to be held on entry */
3201 static void qlt_prepare_srr_imm(struct scsi_qla_host *vha,
3202 struct imm_ntfy_from_isp *iocb)
3204 struct qla_tgt_srr_imm *imm;
3205 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
3206 struct qla_tgt_srr_ctio *sctio;
3210 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf02d, "qla_target(%d): SRR received\n",
3213 imm = kzalloc(sizeof(*imm), GFP_ATOMIC);
3215 memcpy(&imm->imm_ntfy, iocb, sizeof(imm->imm_ntfy));
3217 /* IRQ is already OFF */
3218 spin_lock(&tgt->srr_lock);
3219 imm->srr_id = tgt->imm_srr_id;
3220 list_add_tail(&imm->srr_list_entry,
3221 &tgt->srr_imm_list);
3222 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf02e,
3223 "IMM NTFY SRR %p added (id %d, ui %x)\n",
3224 imm, imm->srr_id, iocb->u.isp24.srr_ui);
3225 if (tgt->imm_srr_id == tgt->ctio_srr_id) {
3227 list_for_each_entry(sctio, &tgt->srr_ctio_list,
3229 if (sctio->srr_id == imm->srr_id) {
3235 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf02f, "%s",
3236 "Scheduling srr work\n");
3237 schedule_work(&tgt->srr_work);
3239 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf030,
3240 "qla_target(%d): imm_srr_id "
3241 "== ctio_srr_id (%d), but there is no "
3242 "corresponding SRR CTIO, deleting IMM "
3243 "SRR %p\n", vha->vp_idx, tgt->ctio_srr_id,
3245 list_del(&imm->srr_list_entry);
3249 spin_unlock(&tgt->srr_lock);
3253 spin_unlock(&tgt->srr_lock);
3255 struct qla_tgt_srr_ctio *ts;
3257 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf069,
3258 "qla_target(%d): Unable to allocate SRR IMM "
3259 "entry, SRR request will be rejected\n", vha->vp_idx);
3261 /* IRQ is already OFF */
3262 spin_lock(&tgt->srr_lock);
3263 list_for_each_entry_safe(sctio, ts, &tgt->srr_ctio_list,
3265 if (sctio->srr_id == tgt->imm_srr_id) {
3266 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf031,
3267 "CTIO SRR %p deleted (id %d)\n",
3268 sctio, sctio->srr_id);
3269 list_del(&sctio->srr_list_entry);
3270 qlt_send_term_exchange(vha, sctio->cmd,
3271 &sctio->cmd->atio, 1);
3275 spin_unlock(&tgt->srr_lock);
3282 qlt_send_notify_ack(vha, iocb, 0, 0, 0,
3283 NOTIFY_ACK_SRR_FLAGS_REJECT,
3284 NOTIFY_ACK_SRR_REJECT_REASON_UNABLE_TO_PERFORM,
3285 NOTIFY_ACK_SRR_FLAGS_REJECT_EXPL_NO_EXPL);
3289 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
3291 static void qlt_handle_imm_notify(struct scsi_qla_host *vha,
3292 struct imm_ntfy_from_isp *iocb)
3294 struct qla_hw_data *ha = vha->hw;
3295 uint32_t add_flags = 0;
3296 int send_notify_ack = 1;
3299 status = le16_to_cpu(iocb->u.isp2x.status);
3301 case IMM_NTFY_LIP_RESET:
3303 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf032,
3304 "qla_target(%d): LIP reset (loop %#x), subcode %x\n",
3305 vha->vp_idx, le16_to_cpu(iocb->u.isp24.nport_handle),
3306 iocb->u.isp24.status_subcode);
3308 if (qlt_reset(vha, iocb, QLA_TGT_ABORT_ALL) == 0)
3309 send_notify_ack = 0;
3313 case IMM_NTFY_LIP_LINK_REINIT:
3315 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
3316 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf033,
3317 "qla_target(%d): LINK REINIT (loop %#x, "
3318 "subcode %x)\n", vha->vp_idx,
3319 le16_to_cpu(iocb->u.isp24.nport_handle),
3320 iocb->u.isp24.status_subcode);
3321 if (tgt->link_reinit_iocb_pending) {
3322 qlt_send_notify_ack(vha, &tgt->link_reinit_iocb,
3325 memcpy(&tgt->link_reinit_iocb, iocb, sizeof(*iocb));
3326 tgt->link_reinit_iocb_pending = 1;
3328 * QLogic requires to wait after LINK REINIT for possible
3329 * PDISC or ADISC ELS commands
3331 send_notify_ack = 0;
3335 case IMM_NTFY_PORT_LOGOUT:
3336 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf034,
3337 "qla_target(%d): Port logout (loop "
3338 "%#x, subcode %x)\n", vha->vp_idx,
3339 le16_to_cpu(iocb->u.isp24.nport_handle),
3340 iocb->u.isp24.status_subcode);
3342 if (qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS_SESS) == 0)
3343 send_notify_ack = 0;
3344 /* The sessions will be cleared in the callback, if needed */
3347 case IMM_NTFY_GLBL_TPRLO:
3348 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf035,
3349 "qla_target(%d): Global TPRLO (%x)\n", vha->vp_idx, status);
3350 if (qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS) == 0)
3351 send_notify_ack = 0;
3352 /* The sessions will be cleared in the callback, if needed */
3355 case IMM_NTFY_PORT_CONFIG:
3356 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf036,
3357 "qla_target(%d): Port config changed (%x)\n", vha->vp_idx,
3359 if (qlt_reset(vha, iocb, QLA_TGT_ABORT_ALL) == 0)
3360 send_notify_ack = 0;
3361 /* The sessions will be cleared in the callback, if needed */
3364 case IMM_NTFY_GLBL_LOGO:
3365 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06a,
3366 "qla_target(%d): Link failure detected\n",
3368 /* I_T nexus loss */
3369 if (qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS) == 0)
3370 send_notify_ack = 0;
3373 case IMM_NTFY_IOCB_OVERFLOW:
3374 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06b,
3375 "qla_target(%d): Cannot provide requested "
3376 "capability (IOCB overflowed the immediate notify "
3377 "resource count)\n", vha->vp_idx);
3380 case IMM_NTFY_ABORT_TASK:
3381 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf037,
3382 "qla_target(%d): Abort Task (S %08x I %#x -> "
3383 "L %#x)\n", vha->vp_idx,
3384 le16_to_cpu(iocb->u.isp2x.seq_id),
3385 GET_TARGET_ID(ha, (struct atio_from_isp *)iocb),
3386 le16_to_cpu(iocb->u.isp2x.lun));
3387 if (qlt_abort_task(vha, iocb) == 0)
3388 send_notify_ack = 0;
3391 case IMM_NTFY_RESOURCE:
3392 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06c,
3393 "qla_target(%d): Out of resources, host %ld\n",
3394 vha->vp_idx, vha->host_no);
3397 case IMM_NTFY_MSG_RX:
3398 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf038,
3399 "qla_target(%d): Immediate notify task %x\n",
3400 vha->vp_idx, iocb->u.isp2x.task_flags);
3401 if (qlt_handle_task_mgmt(vha, iocb) == 0)
3402 send_notify_ack = 0;
3406 if (qlt_24xx_handle_els(vha, iocb) == 0)
3407 send_notify_ack = 0;
3411 qlt_prepare_srr_imm(vha, iocb);
3412 send_notify_ack = 0;
3416 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06d,
3417 "qla_target(%d): Received unknown immediate "
3418 "notify status %x\n", vha->vp_idx, status);
3422 if (send_notify_ack)
3423 qlt_send_notify_ack(vha, iocb, add_flags, 0, 0, 0, 0, 0);
3427 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
3428 * This function sends busy to ISP 2xxx or 24xx.
3430 static void qlt_send_busy(struct scsi_qla_host *vha,
3431 struct atio_from_isp *atio, uint16_t status)
3433 struct ctio7_to_24xx *ctio24;
3434 struct qla_hw_data *ha = vha->hw;
3436 struct qla_tgt_sess *sess = NULL;
3438 sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha,
3439 atio->u.isp24.fcp_hdr.s_id);
3441 qlt_send_term_exchange(vha, NULL, atio, 1);
3444 /* Sending marker isn't necessary, since we called from ISR */
3446 pkt = (request_t *)qla2x00_alloc_iocbs(vha, NULL);
3448 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06e,
3449 "qla_target(%d): %s failed: unable to allocate "
3450 "request packet", vha->vp_idx, __func__);
3454 pkt->entry_count = 1;
3455 pkt->handle = QLA_TGT_SKIP_HANDLE | CTIO_COMPLETION_HANDLE_MARK;
3457 ctio24 = (struct ctio7_to_24xx *)pkt;
3458 ctio24->entry_type = CTIO_TYPE7;
3459 ctio24->nport_handle = sess->loop_id;
3460 ctio24->timeout = __constant_cpu_to_le16(QLA_TGT_TIMEOUT);
3461 ctio24->vp_index = vha->vp_idx;
3462 ctio24->initiator_id[0] = atio->u.isp24.fcp_hdr.s_id[2];
3463 ctio24->initiator_id[1] = atio->u.isp24.fcp_hdr.s_id[1];
3464 ctio24->initiator_id[2] = atio->u.isp24.fcp_hdr.s_id[0];
3465 ctio24->exchange_addr = atio->u.isp24.exchange_addr;
3466 ctio24->u.status1.flags = (atio->u.isp24.attr << 9) |
3467 __constant_cpu_to_le16(
3468 CTIO7_FLAGS_STATUS_MODE_1 | CTIO7_FLAGS_SEND_STATUS |
3469 CTIO7_FLAGS_DONT_RET_CTIO);
3471 * CTIO from fw w/o se_cmd doesn't provide enough info to retry it,
3472 * if the explicit conformation is used.
3474 ctio24->u.status1.ox_id = swab16(atio->u.isp24.fcp_hdr.ox_id);
3475 ctio24->u.status1.scsi_status = cpu_to_le16(status);
3476 ctio24->u.status1.residual = get_unaligned((uint32_t *)
3477 &atio->u.isp24.fcp_cmnd.add_cdb[
3478 atio->u.isp24.fcp_cmnd.add_cdb_len]);
3479 if (ctio24->u.status1.residual != 0)
3480 ctio24->u.status1.scsi_status |= SS_RESIDUAL_UNDER;
3482 qla2x00_start_iocbs(vha, vha->req);
3485 /* ha->hardware_lock supposed to be held on entry */
3486 /* called via callback from qla2xxx */
3487 static void qlt_24xx_atio_pkt(struct scsi_qla_host *vha,
3488 struct atio_from_isp *atio)
3490 struct qla_hw_data *ha = vha->hw;
3491 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
3494 if (unlikely(tgt == NULL)) {
3495 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf039,
3496 "ATIO pkt, but no tgt (ha %p)", ha);
3499 ql_dbg(ql_dbg_tgt, vha, 0xe02c,
3500 "qla_target(%d): ATIO pkt %p: type %02x count %02x",
3501 vha->vp_idx, atio, atio->u.raw.entry_type,
3502 atio->u.raw.entry_count);
3504 * In tgt_stop mode we also should allow all requests to pass.
3505 * Otherwise, some commands can stuck.
3508 tgt->irq_cmd_count++;
3510 switch (atio->u.raw.entry_type) {
3512 ql_dbg(ql_dbg_tgt, vha, 0xe02d,
3513 "ATIO_TYPE7 instance %d, lun %Lx, read/write %d/%d, "
3514 "add_cdb_len %d, data_length %04x, s_id %x:%x:%x\n",
3515 vha->vp_idx, atio->u.isp24.fcp_cmnd.lun,
3516 atio->u.isp24.fcp_cmnd.rddata,
3517 atio->u.isp24.fcp_cmnd.wrdata,
3518 atio->u.isp24.fcp_cmnd.add_cdb_len,
3519 be32_to_cpu(get_unaligned((uint32_t *)
3520 &atio->u.isp24.fcp_cmnd.add_cdb[
3521 atio->u.isp24.fcp_cmnd.add_cdb_len])),
3522 atio->u.isp24.fcp_hdr.s_id[0],
3523 atio->u.isp24.fcp_hdr.s_id[1],
3524 atio->u.isp24.fcp_hdr.s_id[2]);
3526 if (unlikely(atio->u.isp24.exchange_addr ==
3527 ATIO_EXCHANGE_ADDRESS_UNKNOWN)) {
3528 ql_dbg(ql_dbg_tgt, vha, 0xe058,
3529 "qla_target(%d): ATIO_TYPE7 "
3530 "received with UNKNOWN exchange address, "
3531 "sending QUEUE_FULL\n", vha->vp_idx);
3532 qlt_send_busy(vha, atio, SAM_STAT_TASK_SET_FULL);
3535 if (likely(atio->u.isp24.fcp_cmnd.task_mgmt_flags == 0))
3536 rc = qlt_handle_cmd_for_atio(vha, atio);
3538 rc = qlt_handle_task_mgmt(vha, atio);
3539 if (unlikely(rc != 0)) {
3541 #if 1 /* With TERM EXCHANGE some FC cards refuse to boot */
3542 qlt_send_busy(vha, atio, SAM_STAT_BUSY);
3544 qlt_send_term_exchange(vha, NULL, atio, 1);
3547 if (tgt->tgt_stop) {
3548 ql_dbg(ql_dbg_tgt, vha, 0xe059,
3549 "qla_target: Unable to send "
3550 "command to target for req, "
3553 ql_dbg(ql_dbg_tgt, vha, 0xe05a,
3554 "qla_target(%d): Unable to send "
3555 "command to target, sending BUSY "
3556 "status.\n", vha->vp_idx);
3557 qlt_send_busy(vha, atio, SAM_STAT_BUSY);
3563 case IMMED_NOTIFY_TYPE:
3565 if (unlikely(atio->u.isp2x.entry_status != 0)) {
3566 ql_dbg(ql_dbg_tgt, vha, 0xe05b,
3567 "qla_target(%d): Received ATIO packet %x "
3568 "with error status %x\n", vha->vp_idx,
3569 atio->u.raw.entry_type,
3570 atio->u.isp2x.entry_status);
3573 ql_dbg(ql_dbg_tgt, vha, 0xe02e, "%s", "IMMED_NOTIFY ATIO");
3574 qlt_handle_imm_notify(vha, (struct imm_ntfy_from_isp *)atio);
3579 ql_dbg(ql_dbg_tgt, vha, 0xe05c,
3580 "qla_target(%d): Received unknown ATIO atio "
3581 "type %x\n", vha->vp_idx, atio->u.raw.entry_type);
3585 tgt->irq_cmd_count--;
3588 /* ha->hardware_lock supposed to be held on entry */
3589 /* called via callback from qla2xxx */
3590 static void qlt_response_pkt(struct scsi_qla_host *vha, response_t *pkt)
3592 struct qla_hw_data *ha = vha->hw;
3593 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
3595 if (unlikely(tgt == NULL)) {
3596 ql_dbg(ql_dbg_tgt, vha, 0xe05d,
3597 "qla_target(%d): Response pkt %x received, but no "
3598 "tgt (ha %p)\n", vha->vp_idx, pkt->entry_type, ha);
3602 ql_dbg(ql_dbg_tgt, vha, 0xe02f,
3603 "qla_target(%d): response pkt %p: T %02x C %02x S %02x "
3604 "handle %#x\n", vha->vp_idx, pkt, pkt->entry_type,
3605 pkt->entry_count, pkt->entry_status, pkt->handle);
3608 * In tgt_stop mode we also should allow all requests to pass.
3609 * Otherwise, some commands can stuck.
3612 tgt->irq_cmd_count++;
3614 switch (pkt->entry_type) {
3617 struct ctio7_from_24xx *entry = (struct ctio7_from_24xx *)pkt;
3618 ql_dbg(ql_dbg_tgt, vha, 0xe030, "CTIO_TYPE7: instance %d\n",
3620 qlt_do_ctio_completion(vha, entry->handle,
3621 le16_to_cpu(entry->status)|(pkt->entry_status << 16),
3626 case ACCEPT_TGT_IO_TYPE:
3628 struct atio_from_isp *atio = (struct atio_from_isp *)pkt;
3630 ql_dbg(ql_dbg_tgt, vha, 0xe031,
3631 "ACCEPT_TGT_IO instance %d status %04x "
3632 "lun %04x read/write %d data_length %04x "
3633 "target_id %02x rx_id %04x\n ", vha->vp_idx,
3634 le16_to_cpu(atio->u.isp2x.status),
3635 le16_to_cpu(atio->u.isp2x.lun),
3636 atio->u.isp2x.execution_codes,
3637 le32_to_cpu(atio->u.isp2x.data_length), GET_TARGET_ID(ha,
3638 atio), atio->u.isp2x.rx_id);
3639 if (atio->u.isp2x.status !=
3640 __constant_cpu_to_le16(ATIO_CDB_VALID)) {
3641 ql_dbg(ql_dbg_tgt, vha, 0xe05e,
3642 "qla_target(%d): ATIO with error "
3643 "status %x received\n", vha->vp_idx,
3644 le16_to_cpu(atio->u.isp2x.status));
3647 ql_dbg(ql_dbg_tgt, vha, 0xe032,
3648 "FCP CDB: 0x%02x, sizeof(cdb): %lu",
3649 atio->u.isp2x.cdb[0], (unsigned long
3650 int)sizeof(atio->u.isp2x.cdb));
3652 rc = qlt_handle_cmd_for_atio(vha, atio);
3653 if (unlikely(rc != 0)) {
3655 #if 1 /* With TERM EXCHANGE some FC cards refuse to boot */
3656 qlt_send_busy(vha, atio, 0);
3658 qlt_send_term_exchange(vha, NULL, atio, 1);
3661 if (tgt->tgt_stop) {
3662 ql_dbg(ql_dbg_tgt, vha, 0xe05f,
3663 "qla_target: Unable to send "
3664 "command to target, sending TERM "
3665 "EXCHANGE for rsp\n");
3666 qlt_send_term_exchange(vha, NULL,
3669 ql_dbg(ql_dbg_tgt, vha, 0xe060,
3670 "qla_target(%d): Unable to send "
3671 "command to target, sending BUSY "
3672 "status\n", vha->vp_idx);
3673 qlt_send_busy(vha, atio, 0);
3680 case CONTINUE_TGT_IO_TYPE:
3682 struct ctio_to_2xxx *entry = (struct ctio_to_2xxx *)pkt;
3683 ql_dbg(ql_dbg_tgt, vha, 0xe033,
3684 "CONTINUE_TGT_IO: instance %d\n", vha->vp_idx);
3685 qlt_do_ctio_completion(vha, entry->handle,
3686 le16_to_cpu(entry->status)|(pkt->entry_status << 16),
3693 struct ctio_to_2xxx *entry = (struct ctio_to_2xxx *)pkt;
3694 ql_dbg(ql_dbg_tgt, vha, 0xe034, "CTIO_A64: instance %d\n",
3696 qlt_do_ctio_completion(vha, entry->handle,
3697 le16_to_cpu(entry->status)|(pkt->entry_status << 16),
3702 case IMMED_NOTIFY_TYPE:
3703 ql_dbg(ql_dbg_tgt, vha, 0xe035, "%s", "IMMED_NOTIFY\n");
3704 qlt_handle_imm_notify(vha, (struct imm_ntfy_from_isp *)pkt);
3707 case NOTIFY_ACK_TYPE:
3708 if (tgt->notify_ack_expected > 0) {
3709 struct nack_to_isp *entry = (struct nack_to_isp *)pkt;
3710 ql_dbg(ql_dbg_tgt, vha, 0xe036,
3711 "NOTIFY_ACK seq %08x status %x\n",
3712 le16_to_cpu(entry->u.isp2x.seq_id),
3713 le16_to_cpu(entry->u.isp2x.status));
3714 tgt->notify_ack_expected--;
3715 if (entry->u.isp2x.status !=
3716 __constant_cpu_to_le16(NOTIFY_ACK_SUCCESS)) {
3717 ql_dbg(ql_dbg_tgt, vha, 0xe061,
3718 "qla_target(%d): NOTIFY_ACK "
3719 "failed %x\n", vha->vp_idx,
3720 le16_to_cpu(entry->u.isp2x.status));
3723 ql_dbg(ql_dbg_tgt, vha, 0xe062,
3724 "qla_target(%d): Unexpected NOTIFY_ACK received\n",
3729 case ABTS_RECV_24XX:
3730 ql_dbg(ql_dbg_tgt, vha, 0xe037,
3731 "ABTS_RECV_24XX: instance %d\n", vha->vp_idx);
3732 qlt_24xx_handle_abts(vha, (struct abts_recv_from_24xx *)pkt);
3735 case ABTS_RESP_24XX:
3736 if (tgt->abts_resp_expected > 0) {
3737 struct abts_resp_from_24xx_fw *entry =
3738 (struct abts_resp_from_24xx_fw *)pkt;
3739 ql_dbg(ql_dbg_tgt, vha, 0xe038,
3740 "ABTS_RESP_24XX: compl_status %x\n",
3741 entry->compl_status);
3742 tgt->abts_resp_expected--;
3743 if (le16_to_cpu(entry->compl_status) !=
3744 ABTS_RESP_COMPL_SUCCESS) {
3745 if ((entry->error_subcode1 == 0x1E) &&
3746 (entry->error_subcode2 == 0)) {
3748 * We've got a race here: aborted
3749 * exchange not terminated, i.e.
3750 * response for the aborted command was
3751 * sent between the abort request was
3752 * received and processed.
3753 * Unfortunately, the firmware has a
3754 * silly requirement that all aborted
3755 * exchanges must be explicitely
3756 * terminated, otherwise it refuses to
3757 * send responses for the abort
3758 * requests. So, we have to
3759 * (re)terminate the exchange and retry
3760 * the abort response.
3762 qlt_24xx_retry_term_exchange(vha,
3765 ql_dbg(ql_dbg_tgt, vha, 0xe063,
3766 "qla_target(%d): ABTS_RESP_24XX "
3767 "failed %x (subcode %x:%x)",
3768 vha->vp_idx, entry->compl_status,
3769 entry->error_subcode1,
3770 entry->error_subcode2);
3773 ql_dbg(ql_dbg_tgt, vha, 0xe064,
3774 "qla_target(%d): Unexpected ABTS_RESP_24XX "
3775 "received\n", vha->vp_idx);
3780 ql_dbg(ql_dbg_tgt, vha, 0xe065,
3781 "qla_target(%d): Received unknown response pkt "
3782 "type %x\n", vha->vp_idx, pkt->entry_type);
3786 tgt->irq_cmd_count--;
3790 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
3792 void qlt_async_event(uint16_t code, struct scsi_qla_host *vha,
3795 struct qla_hw_data *ha = vha->hw;
3796 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
3799 ql_dbg(ql_dbg_tgt, vha, 0xe039,
3800 "scsi(%ld): ha state %d init_done %d oper_mode %d topo %d\n",
3801 vha->host_no, atomic_read(&vha->loop_state), vha->flags.init_done,
3802 ha->operating_mode, ha->current_topology);
3804 if (!ha->tgt.tgt_ops)
3807 if (unlikely(tgt == NULL)) {
3808 ql_dbg(ql_dbg_tgt, vha, 0xe03a,
3809 "ASYNC EVENT %#x, but no tgt (ha %p)\n", code, ha);
3813 if (((code == MBA_POINT_TO_POINT) || (code == MBA_CHG_IN_CONNECTION)) &&
3817 * In tgt_stop mode we also should allow all requests to pass.
3818 * Otherwise, some commands can stuck.
3821 tgt->irq_cmd_count++;
3824 case MBA_RESET: /* Reset */
3825 case MBA_SYSTEM_ERR: /* System Error */
3826 case MBA_REQ_TRANSFER_ERR: /* Request Transfer Error */
3827 case MBA_RSP_TRANSFER_ERR: /* Response Transfer Error */
3828 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03a,
3829 "qla_target(%d): System error async event %#x "
3830 "occurred", vha->vp_idx, code);
3832 case MBA_WAKEUP_THRES: /* Request Queue Wake-up. */
3833 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
3838 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03b,
3839 "qla_target(%d): Async LOOP_UP occurred "
3840 "(m[0]=%x, m[1]=%x, m[2]=%x, m[3]=%x)", vha->vp_idx,
3841 le16_to_cpu(mailbox[0]), le16_to_cpu(mailbox[1]),
3842 le16_to_cpu(mailbox[2]), le16_to_cpu(mailbox[3]));
3843 if (tgt->link_reinit_iocb_pending) {
3844 qlt_send_notify_ack(vha, (void *)&tgt->link_reinit_iocb,
3846 tgt->link_reinit_iocb_pending = 0;
3851 case MBA_LIP_OCCURRED:
3854 case MBA_RSCN_UPDATE:
3855 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03c,
3856 "qla_target(%d): Async event %#x occurred "
3857 "(m[0]=%x, m[1]=%x, m[2]=%x, m[3]=%x)", vha->vp_idx, code,
3858 le16_to_cpu(mailbox[0]), le16_to_cpu(mailbox[1]),
3859 le16_to_cpu(mailbox[2]), le16_to_cpu(mailbox[3]));
3862 case MBA_PORT_UPDATE:
3863 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03d,
3864 "qla_target(%d): Port update async event %#x "
3865 "occurred: updating the ports database (m[0]=%x, m[1]=%x, "
3866 "m[2]=%x, m[3]=%x)", vha->vp_idx, code,
3867 le16_to_cpu(mailbox[0]), le16_to_cpu(mailbox[1]),
3868 le16_to_cpu(mailbox[2]), le16_to_cpu(mailbox[3]));
3870 login_code = le16_to_cpu(mailbox[2]);
3871 if (login_code == 0x4)
3872 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03e,
3873 "Async MB 2: Got PLOGI Complete\n");
3874 else if (login_code == 0x7)
3875 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03f,
3876 "Async MB 2: Port Logged Out\n");
3880 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf040,
3881 "qla_target(%d): Async event %#x occurred: "
3882 "ignore (m[0]=%x, m[1]=%x, m[2]=%x, m[3]=%x)", vha->vp_idx,
3883 code, le16_to_cpu(mailbox[0]), le16_to_cpu(mailbox[1]),
3884 le16_to_cpu(mailbox[2]), le16_to_cpu(mailbox[3]));
3888 tgt->irq_cmd_count--;
3891 static fc_port_t *qlt_get_port_database(struct scsi_qla_host *vha,
3897 fcport = kzalloc(sizeof(*fcport), GFP_KERNEL);
3899 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06f,
3900 "qla_target(%d): Allocation of tmp FC port failed",
3905 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf041, "loop_id %d", loop_id);
3907 fcport->loop_id = loop_id;
3909 rc = qla2x00_get_port_database(vha, fcport, 0);
3910 if (rc != QLA_SUCCESS) {
3911 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf070,
3912 "qla_target(%d): Failed to retrieve fcport "
3913 "information -- get_port_database() returned %x "
3914 "(loop_id=0x%04x)", vha->vp_idx, rc, loop_id);
3922 /* Must be called under tgt_mutex */
3923 static struct qla_tgt_sess *qlt_make_local_sess(struct scsi_qla_host *vha,
3926 struct qla_tgt_sess *sess = NULL;
3927 fc_port_t *fcport = NULL;
3928 int rc, global_resets;
3929 uint16_t loop_id = 0;
3933 atomic_read(&vha->vha_tgt.qla_tgt->tgt_global_resets_count);
3935 rc = qla24xx_get_loop_id(vha, s_id, &loop_id);
3937 if ((s_id[0] == 0xFF) &&
3938 (s_id[1] == 0xFC)) {
3940 * This is Domain Controller, so it should be
3941 * OK to drop SCSI commands from it.
3943 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf042,
3944 "Unable to find initiator with S_ID %x:%x:%x",
3945 s_id[0], s_id[1], s_id[2]);
3947 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf071,
3948 "qla_target(%d): Unable to find "
3949 "initiator with S_ID %x:%x:%x",
3950 vha->vp_idx, s_id[0], s_id[1],
3955 fcport = qlt_get_port_database(vha, loop_id);
3959 if (global_resets !=
3960 atomic_read(&vha->vha_tgt.qla_tgt->tgt_global_resets_count)) {
3961 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf043,
3962 "qla_target(%d): global reset during session discovery "
3963 "(counter was %d, new %d), retrying", vha->vp_idx,
3965 atomic_read(&vha->vha_tgt.
3966 qla_tgt->tgt_global_resets_count));
3970 sess = qlt_create_sess(vha, fcport, true);
3976 static void qlt_abort_work(struct qla_tgt *tgt,
3977 struct qla_tgt_sess_work_param *prm)
3979 struct scsi_qla_host *vha = tgt->vha;
3980 struct qla_hw_data *ha = vha->hw;
3981 struct qla_tgt_sess *sess = NULL;
3982 unsigned long flags;
3987 spin_lock_irqsave(&ha->hardware_lock, flags);
3992 s_id[0] = prm->abts.fcp_hdr_le.s_id[2];
3993 s_id[1] = prm->abts.fcp_hdr_le.s_id[1];
3994 s_id[2] = prm->abts.fcp_hdr_le.s_id[0];
3996 sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha,
3997 (unsigned char *)&be_s_id);
3999 spin_unlock_irqrestore(&ha->hardware_lock, flags);
4001 mutex_lock(&vha->vha_tgt.tgt_mutex);
4002 sess = qlt_make_local_sess(vha, s_id);
4003 /* sess has got an extra creation ref */
4004 mutex_unlock(&vha->vha_tgt.tgt_mutex);
4006 spin_lock_irqsave(&ha->hardware_lock, flags);
4010 kref_get(&sess->se_sess->sess_kref);
4016 rc = __qlt_24xx_handle_abts(vha, &prm->abts, sess);
4020 ha->tgt.tgt_ops->put_sess(sess);
4021 spin_unlock_irqrestore(&ha->hardware_lock, flags);
4025 qlt_24xx_send_abts_resp(vha, &prm->abts, FCP_TMF_REJECTED, false);
4027 ha->tgt.tgt_ops->put_sess(sess);
4028 spin_unlock_irqrestore(&ha->hardware_lock, flags);
4031 static void qlt_tmr_work(struct qla_tgt *tgt,
4032 struct qla_tgt_sess_work_param *prm)
4034 struct atio_from_isp *a = &prm->tm_iocb2;
4035 struct scsi_qla_host *vha = tgt->vha;
4036 struct qla_hw_data *ha = vha->hw;
4037 struct qla_tgt_sess *sess = NULL;
4038 unsigned long flags;
4039 uint8_t *s_id = NULL; /* to hide compiler warnings */
4041 uint32_t lun, unpacked_lun;
4045 spin_lock_irqsave(&ha->hardware_lock, flags);
4050 s_id = prm->tm_iocb2.u.isp24.fcp_hdr.s_id;
4051 sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha, s_id);
4053 spin_unlock_irqrestore(&ha->hardware_lock, flags);
4055 mutex_lock(&vha->vha_tgt.tgt_mutex);
4056 sess = qlt_make_local_sess(vha, s_id);
4057 /* sess has got an extra creation ref */
4058 mutex_unlock(&vha->vha_tgt.tgt_mutex);
4060 spin_lock_irqsave(&ha->hardware_lock, flags);
4064 kref_get(&sess->se_sess->sess_kref);
4068 lun = a->u.isp24.fcp_cmnd.lun;
4069 lun_size = sizeof(lun);
4070 fn = a->u.isp24.fcp_cmnd.task_mgmt_flags;
4071 unpacked_lun = scsilun_to_int((struct scsi_lun *)&lun);
4073 rc = qlt_issue_task_mgmt(sess, unpacked_lun, fn, iocb, 0);
4077 ha->tgt.tgt_ops->put_sess(sess);
4078 spin_unlock_irqrestore(&ha->hardware_lock, flags);
4082 qlt_send_term_exchange(vha, NULL, &prm->tm_iocb2, 1);
4084 ha->tgt.tgt_ops->put_sess(sess);
4085 spin_unlock_irqrestore(&ha->hardware_lock, flags);
4088 static void qlt_sess_work_fn(struct work_struct *work)
4090 struct qla_tgt *tgt = container_of(work, struct qla_tgt, sess_work);
4091 struct scsi_qla_host *vha = tgt->vha;
4092 unsigned long flags;
4094 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf000, "Sess work (tgt %p)", tgt);
4096 spin_lock_irqsave(&tgt->sess_work_lock, flags);
4097 while (!list_empty(&tgt->sess_works_list)) {
4098 struct qla_tgt_sess_work_param *prm = list_entry(
4099 tgt->sess_works_list.next, typeof(*prm),
4100 sess_works_list_entry);
4103 * This work can be scheduled on several CPUs at time, so we
4104 * must delete the entry to eliminate double processing
4106 list_del(&prm->sess_works_list_entry);
4108 spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
4110 switch (prm->type) {
4111 case QLA_TGT_SESS_WORK_ABORT:
4112 qlt_abort_work(tgt, prm);
4114 case QLA_TGT_SESS_WORK_TM:
4115 qlt_tmr_work(tgt, prm);
4122 spin_lock_irqsave(&tgt->sess_work_lock, flags);
4126 spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
4129 /* Must be called under tgt_host_action_mutex */
4130 int qlt_add_target(struct qla_hw_data *ha, struct scsi_qla_host *base_vha)
4132 struct qla_tgt *tgt;
4134 if (!QLA_TGT_MODE_ENABLED())
4137 if (!IS_TGT_MODE_CAPABLE(ha)) {
4138 ql_log(ql_log_warn, base_vha, 0xe070,
4139 "This adapter does not support target mode.\n");
4143 ql_dbg(ql_dbg_tgt, base_vha, 0xe03b,
4144 "Registering target for host %ld(%p).\n", base_vha->host_no, ha);
4146 BUG_ON(base_vha->vha_tgt.qla_tgt != NULL);
4148 tgt = kzalloc(sizeof(struct qla_tgt), GFP_KERNEL);
4150 ql_dbg(ql_dbg_tgt, base_vha, 0xe066,
4151 "Unable to allocate struct qla_tgt\n");
4155 if (!(base_vha->host->hostt->supported_mode & MODE_TARGET))
4156 base_vha->host->hostt->supported_mode |= MODE_TARGET;
4159 tgt->vha = base_vha;
4160 init_waitqueue_head(&tgt->waitQ);
4161 INIT_LIST_HEAD(&tgt->sess_list);
4162 INIT_LIST_HEAD(&tgt->del_sess_list);
4163 INIT_DELAYED_WORK(&tgt->sess_del_work,
4164 (void (*)(struct work_struct *))qlt_del_sess_work_fn);
4165 spin_lock_init(&tgt->sess_work_lock);
4166 INIT_WORK(&tgt->sess_work, qlt_sess_work_fn);
4167 INIT_LIST_HEAD(&tgt->sess_works_list);
4168 spin_lock_init(&tgt->srr_lock);
4169 INIT_LIST_HEAD(&tgt->srr_ctio_list);
4170 INIT_LIST_HEAD(&tgt->srr_imm_list);
4171 INIT_WORK(&tgt->srr_work, qlt_handle_srr_work);
4172 atomic_set(&tgt->tgt_global_resets_count, 0);
4174 base_vha->vha_tgt.qla_tgt = tgt;
4176 ql_dbg(ql_dbg_tgt, base_vha, 0xe067,
4177 "qla_target(%d): using 64 Bit PCI addressing",
4179 tgt->tgt_enable_64bit_addr = 1;
4181 tgt->sg_tablesize = QLA_TGT_MAX_SG_24XX(base_vha->req->length - 3);
4182 tgt->datasegs_per_cmd = QLA_TGT_DATASEGS_PER_CMD_24XX;
4183 tgt->datasegs_per_cont = QLA_TGT_DATASEGS_PER_CONT_24XX;
4185 mutex_lock(&qla_tgt_mutex);
4186 list_add_tail(&tgt->tgt_list_entry, &qla_tgt_glist);
4187 mutex_unlock(&qla_tgt_mutex);
4192 /* Must be called under tgt_host_action_mutex */
4193 int qlt_remove_target(struct qla_hw_data *ha, struct scsi_qla_host *vha)
4195 if (!vha->vha_tgt.qla_tgt)
4198 mutex_lock(&qla_tgt_mutex);
4199 list_del(&vha->vha_tgt.qla_tgt->tgt_list_entry);
4200 mutex_unlock(&qla_tgt_mutex);
4202 ql_dbg(ql_dbg_tgt, vha, 0xe03c, "Unregistering target for host %ld(%p)",
4204 qlt_release(vha->vha_tgt.qla_tgt);
4209 static void qlt_lport_dump(struct scsi_qla_host *vha, u64 wwpn,
4214 pr_debug("qla2xxx HW vha->node_name: ");
4215 for (i = 0; i < WWN_SIZE; i++)
4216 pr_debug("%02x ", vha->node_name[i]);
4218 pr_debug("qla2xxx HW vha->port_name: ");
4219 for (i = 0; i < WWN_SIZE; i++)
4220 pr_debug("%02x ", vha->port_name[i]);
4223 pr_debug("qla2xxx passed configfs WWPN: ");
4224 put_unaligned_be64(wwpn, b);
4225 for (i = 0; i < WWN_SIZE; i++)
4226 pr_debug("%02x ", b[i]);
4231 * qla_tgt_lport_register - register lport with external module
4233 * @qla_tgt_ops: Pointer for tcm_qla2xxx qla_tgt_ops
4234 * @wwpn: Passwd FC target WWPN
4235 * @callback: lport initialization callback for tcm_qla2xxx code
4236 * @target_lport_ptr: pointer for tcm_qla2xxx specific lport data
4238 int qlt_lport_register(struct qla_tgt_func_tmpl *qla_tgt_ops, u64 wwpn,
4239 int (*callback)(struct scsi_qla_host *), void *target_lport_ptr)
4241 struct qla_tgt *tgt;
4242 struct scsi_qla_host *vha;
4243 struct qla_hw_data *ha;
4244 struct Scsi_Host *host;
4245 unsigned long flags;
4249 mutex_lock(&qla_tgt_mutex);
4250 list_for_each_entry(tgt, &qla_tgt_glist, tgt_list_entry) {
4258 if (!(host->hostt->supported_mode & MODE_TARGET))
4261 spin_lock_irqsave(&ha->hardware_lock, flags);
4262 if (host->active_mode & MODE_TARGET) {
4263 pr_debug("MODE_TARGET already active on qla2xxx(%d)\n",
4265 spin_unlock_irqrestore(&ha->hardware_lock, flags);
4268 spin_unlock_irqrestore(&ha->hardware_lock, flags);
4270 if (!scsi_host_get(host)) {
4271 ql_dbg(ql_dbg_tgt, vha, 0xe068,
4272 "Unable to scsi_host_get() for"
4273 " qla2xxx scsi_host\n");
4276 qlt_lport_dump(vha, wwpn, b);
4278 if (memcmp(vha->port_name, b, WWN_SIZE)) {
4279 scsi_host_put(host);
4283 * Setup passed parameters ahead of invoking callback
4285 ha->tgt.tgt_ops = qla_tgt_ops;
4286 vha->vha_tgt.target_lport_ptr = target_lport_ptr;
4287 rc = (*callback)(vha);
4289 ha->tgt.tgt_ops = NULL;
4290 vha->vha_tgt.target_lport_ptr = NULL;
4291 scsi_host_put(host);
4293 mutex_unlock(&qla_tgt_mutex);
4296 mutex_unlock(&qla_tgt_mutex);
4300 EXPORT_SYMBOL(qlt_lport_register);
4303 * qla_tgt_lport_deregister - Degister lport
4305 * @vha: Registered scsi_qla_host pointer
4307 void qlt_lport_deregister(struct scsi_qla_host *vha)
4309 struct qla_hw_data *ha = vha->hw;
4310 struct Scsi_Host *sh = vha->host;
4312 * Clear the target_lport_ptr qla_target_template pointer in qla_hw_data
4314 vha->vha_tgt.target_lport_ptr = NULL;
4315 ha->tgt.tgt_ops = NULL;
4317 * Release the Scsi_Host reference for the underlying qla2xxx host
4321 EXPORT_SYMBOL(qlt_lport_deregister);
4323 /* Must be called under HW lock */
4324 void qlt_set_mode(struct scsi_qla_host *vha)
4326 struct qla_hw_data *ha = vha->hw;
4328 switch (ql2x_ini_mode) {
4329 case QLA2XXX_INI_MODE_DISABLED:
4330 case QLA2XXX_INI_MODE_EXCLUSIVE:
4331 vha->host->active_mode = MODE_TARGET;
4333 case QLA2XXX_INI_MODE_ENABLED:
4334 vha->host->active_mode |= MODE_TARGET;
4340 if (ha->tgt.ini_mode_force_reverse)
4341 qla_reverse_ini_mode(vha);
4344 /* Must be called under HW lock */
4345 void qlt_clear_mode(struct scsi_qla_host *vha)
4347 struct qla_hw_data *ha = vha->hw;
4349 switch (ql2x_ini_mode) {
4350 case QLA2XXX_INI_MODE_DISABLED:
4351 vha->host->active_mode = MODE_UNKNOWN;
4353 case QLA2XXX_INI_MODE_EXCLUSIVE:
4354 vha->host->active_mode = MODE_INITIATOR;
4356 case QLA2XXX_INI_MODE_ENABLED:
4357 vha->host->active_mode &= ~MODE_TARGET;
4363 if (ha->tgt.ini_mode_force_reverse)
4364 qla_reverse_ini_mode(vha);
4368 * qla_tgt_enable_vha - NO LOCK HELD
4370 * host_reset, bring up w/ Target Mode Enabled
4373 qlt_enable_vha(struct scsi_qla_host *vha)
4375 struct qla_hw_data *ha = vha->hw;
4376 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
4377 unsigned long flags;
4378 scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
4381 ql_dbg(ql_dbg_tgt, vha, 0xe069,
4382 "Unable to locate qla_tgt pointer from"
4383 " struct qla_hw_data\n");
4388 spin_lock_irqsave(&ha->hardware_lock, flags);
4389 tgt->tgt_stopped = 0;
4391 spin_unlock_irqrestore(&ha->hardware_lock, flags);
4394 qla24xx_disable_vp(vha);
4395 qla24xx_enable_vp(vha);
4397 set_bit(ISP_ABORT_NEEDED, &base_vha->dpc_flags);
4398 qla2xxx_wake_dpc(base_vha);
4399 qla2x00_wait_for_hba_online(base_vha);
4402 EXPORT_SYMBOL(qlt_enable_vha);
4405 * qla_tgt_disable_vha - NO LOCK HELD
4407 * Disable Target Mode and reset the adapter
4410 qlt_disable_vha(struct scsi_qla_host *vha)
4412 struct qla_hw_data *ha = vha->hw;
4413 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
4414 unsigned long flags;
4417 ql_dbg(ql_dbg_tgt, vha, 0xe06a,
4418 "Unable to locate qla_tgt pointer from"
4419 " struct qla_hw_data\n");
4424 spin_lock_irqsave(&ha->hardware_lock, flags);
4425 qlt_clear_mode(vha);
4426 spin_unlock_irqrestore(&ha->hardware_lock, flags);
4428 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
4429 qla2xxx_wake_dpc(vha);
4430 qla2x00_wait_for_hba_online(vha);
4434 * Called from qla_init.c:qla24xx_vport_create() contex to setup
4435 * the target mode specific struct scsi_qla_host and struct qla_hw_data
4439 qlt_vport_create(struct scsi_qla_host *vha, struct qla_hw_data *ha)
4441 if (!qla_tgt_mode_enabled(vha))
4444 vha->vha_tgt.qla_tgt = NULL;
4446 mutex_init(&vha->vha_tgt.tgt_mutex);
4447 mutex_init(&vha->vha_tgt.tgt_host_action_mutex);
4449 qlt_clear_mode(vha);
4452 * NOTE: Currently the value is kept the same for <24xx and
4453 * >=24xx ISPs. If it is necessary to change it,
4454 * the check should be added for specific ISPs,
4455 * assigning the value appropriately.
4457 ha->tgt.atio_q_length = ATIO_ENTRY_CNT_24XX;
4459 qlt_add_target(ha, vha);
4463 qlt_rff_id(struct scsi_qla_host *vha, struct ct_sns_req *ct_req)
4466 * FC-4 Feature bit 0 indicates target functionality to the name server.
4468 if (qla_tgt_mode_enabled(vha)) {
4469 if (qla_ini_mode_enabled(vha))
4470 ct_req->req.rff_id.fc4_feature = BIT_0 | BIT_1;
4472 ct_req->req.rff_id.fc4_feature = BIT_0;
4473 } else if (qla_ini_mode_enabled(vha)) {
4474 ct_req->req.rff_id.fc4_feature = BIT_1;
4479 * qlt_init_atio_q_entries() - Initializes ATIO queue entries.
4482 * Beginning of ATIO ring has initialization control block already built
4483 * by nvram config routine.
4485 * Returns 0 on success.
4488 qlt_init_atio_q_entries(struct scsi_qla_host *vha)
4490 struct qla_hw_data *ha = vha->hw;
4492 struct atio_from_isp *pkt = (struct atio_from_isp *)ha->tgt.atio_ring;
4494 if (!qla_tgt_mode_enabled(vha))
4497 for (cnt = 0; cnt < ha->tgt.atio_q_length; cnt++) {
4498 pkt->u.raw.signature = ATIO_PROCESSED;
4505 * qlt_24xx_process_atio_queue() - Process ATIO queue entries.
4506 * @ha: SCSI driver HA context
4509 qlt_24xx_process_atio_queue(struct scsi_qla_host *vha)
4511 struct qla_hw_data *ha = vha->hw;
4512 struct atio_from_isp *pkt;
4515 if (!vha->flags.online)
4518 while (ha->tgt.atio_ring_ptr->signature != ATIO_PROCESSED) {
4519 pkt = (struct atio_from_isp *)ha->tgt.atio_ring_ptr;
4520 cnt = pkt->u.raw.entry_count;
4522 qlt_24xx_atio_pkt_all_vps(vha, (struct atio_from_isp *)pkt);
4524 for (i = 0; i < cnt; i++) {
4525 ha->tgt.atio_ring_index++;
4526 if (ha->tgt.atio_ring_index == ha->tgt.atio_q_length) {
4527 ha->tgt.atio_ring_index = 0;
4528 ha->tgt.atio_ring_ptr = ha->tgt.atio_ring;
4530 ha->tgt.atio_ring_ptr++;
4532 pkt->u.raw.signature = ATIO_PROCESSED;
4533 pkt = (struct atio_from_isp *)ha->tgt.atio_ring_ptr;
4538 /* Adjust ring index */
4539 WRT_REG_DWORD(ISP_ATIO_Q_OUT(vha), ha->tgt.atio_ring_index);
4543 qlt_24xx_config_rings(struct scsi_qla_host *vha)
4545 struct qla_hw_data *ha = vha->hw;
4546 if (!QLA_TGT_MODE_ENABLED())
4549 WRT_REG_DWORD(ISP_ATIO_Q_IN(vha), 0);
4550 WRT_REG_DWORD(ISP_ATIO_Q_OUT(vha), 0);
4551 RD_REG_DWORD(ISP_ATIO_Q_OUT(vha));
4553 if (IS_ATIO_MSIX_CAPABLE(ha)) {
4554 struct qla_msix_entry *msix = &ha->msix_entries[2];
4555 struct init_cb_24xx *icb = (struct init_cb_24xx *)ha->init_cb;
4557 icb->msix_atio = cpu_to_le16(msix->entry);
4558 ql_dbg(ql_dbg_init, vha, 0xf072,
4559 "Registering ICB vector 0x%x for atio que.\n",
4565 qlt_24xx_config_nvram_stage1(struct scsi_qla_host *vha, struct nvram_24xx *nv)
4567 struct qla_hw_data *ha = vha->hw;
4569 if (qla_tgt_mode_enabled(vha)) {
4570 if (!ha->tgt.saved_set) {
4571 /* We save only once */
4572 ha->tgt.saved_exchange_count = nv->exchange_count;
4573 ha->tgt.saved_firmware_options_1 =
4574 nv->firmware_options_1;
4575 ha->tgt.saved_firmware_options_2 =
4576 nv->firmware_options_2;
4577 ha->tgt.saved_firmware_options_3 =
4578 nv->firmware_options_3;
4579 ha->tgt.saved_set = 1;
4582 nv->exchange_count = __constant_cpu_to_le16(0xFFFF);
4584 /* Enable target mode */
4585 nv->firmware_options_1 |= __constant_cpu_to_le32(BIT_4);
4587 /* Disable ini mode, if requested */
4588 if (!qla_ini_mode_enabled(vha))
4589 nv->firmware_options_1 |= __constant_cpu_to_le32(BIT_5);
4591 /* Disable Full Login after LIP */
4592 nv->firmware_options_1 &= __constant_cpu_to_le32(~BIT_13);
4593 /* Enable initial LIP */
4594 nv->firmware_options_1 &= __constant_cpu_to_le32(~BIT_9);
4595 /* Enable FC tapes support */
4596 nv->firmware_options_2 |= __constant_cpu_to_le32(BIT_12);
4597 /* Disable Full Login after LIP */
4598 nv->host_p &= __constant_cpu_to_le32(~BIT_10);
4599 /* Enable target PRLI control */
4600 nv->firmware_options_2 |= __constant_cpu_to_le32(BIT_14);
4602 if (ha->tgt.saved_set) {
4603 nv->exchange_count = ha->tgt.saved_exchange_count;
4604 nv->firmware_options_1 =
4605 ha->tgt.saved_firmware_options_1;
4606 nv->firmware_options_2 =
4607 ha->tgt.saved_firmware_options_2;
4608 nv->firmware_options_3 =
4609 ha->tgt.saved_firmware_options_3;
4614 /* out-of-order frames reassembly */
4615 nv->firmware_options_3 |= BIT_6|BIT_9;
4617 if (ha->tgt.enable_class_2) {
4618 if (vha->flags.init_done)
4619 fc_host_supported_classes(vha->host) =
4620 FC_COS_CLASS2 | FC_COS_CLASS3;
4622 nv->firmware_options_2 |= __constant_cpu_to_le32(BIT_8);
4624 if (vha->flags.init_done)
4625 fc_host_supported_classes(vha->host) = FC_COS_CLASS3;
4627 nv->firmware_options_2 &= ~__constant_cpu_to_le32(BIT_8);
4632 qlt_24xx_config_nvram_stage2(struct scsi_qla_host *vha,
4633 struct init_cb_24xx *icb)
4635 struct qla_hw_data *ha = vha->hw;
4637 if (ha->tgt.node_name_set) {
4638 memcpy(icb->node_name, ha->tgt.tgt_node_name, WWN_SIZE);
4639 icb->firmware_options_1 |= __constant_cpu_to_le32(BIT_14);
4644 qlt_81xx_config_nvram_stage1(struct scsi_qla_host *vha, struct nvram_81xx *nv)
4646 struct qla_hw_data *ha = vha->hw;
4648 if (!QLA_TGT_MODE_ENABLED())
4651 if (qla_tgt_mode_enabled(vha)) {
4652 if (!ha->tgt.saved_set) {
4653 /* We save only once */
4654 ha->tgt.saved_exchange_count = nv->exchange_count;
4655 ha->tgt.saved_firmware_options_1 =
4656 nv->firmware_options_1;
4657 ha->tgt.saved_firmware_options_2 =
4658 nv->firmware_options_2;
4659 ha->tgt.saved_firmware_options_3 =
4660 nv->firmware_options_3;
4661 ha->tgt.saved_set = 1;
4664 nv->exchange_count = __constant_cpu_to_le16(0xFFFF);
4666 /* Enable target mode */
4667 nv->firmware_options_1 |= __constant_cpu_to_le32(BIT_4);
4669 /* Disable ini mode, if requested */
4670 if (!qla_ini_mode_enabled(vha))
4671 nv->firmware_options_1 |=
4672 __constant_cpu_to_le32(BIT_5);
4674 /* Disable Full Login after LIP */
4675 nv->firmware_options_1 &= __constant_cpu_to_le32(~BIT_13);
4676 /* Enable initial LIP */
4677 nv->firmware_options_1 &= __constant_cpu_to_le32(~BIT_9);
4678 /* Enable FC tapes support */
4679 nv->firmware_options_2 |= __constant_cpu_to_le32(BIT_12);
4680 /* Disable Full Login after LIP */
4681 nv->host_p &= __constant_cpu_to_le32(~BIT_10);
4682 /* Enable target PRLI control */
4683 nv->firmware_options_2 |= __constant_cpu_to_le32(BIT_14);
4685 if (ha->tgt.saved_set) {
4686 nv->exchange_count = ha->tgt.saved_exchange_count;
4687 nv->firmware_options_1 =
4688 ha->tgt.saved_firmware_options_1;
4689 nv->firmware_options_2 =
4690 ha->tgt.saved_firmware_options_2;
4691 nv->firmware_options_3 =
4692 ha->tgt.saved_firmware_options_3;
4697 /* out-of-order frames reassembly */
4698 nv->firmware_options_3 |= BIT_6|BIT_9;
4700 if (ha->tgt.enable_class_2) {
4701 if (vha->flags.init_done)
4702 fc_host_supported_classes(vha->host) =
4703 FC_COS_CLASS2 | FC_COS_CLASS3;
4705 nv->firmware_options_2 |= __constant_cpu_to_le32(BIT_8);
4707 if (vha->flags.init_done)
4708 fc_host_supported_classes(vha->host) = FC_COS_CLASS3;
4710 nv->firmware_options_2 &= ~__constant_cpu_to_le32(BIT_8);
4715 qlt_81xx_config_nvram_stage2(struct scsi_qla_host *vha,
4716 struct init_cb_81xx *icb)
4718 struct qla_hw_data *ha = vha->hw;
4720 if (!QLA_TGT_MODE_ENABLED())
4723 if (ha->tgt.node_name_set) {
4724 memcpy(icb->node_name, ha->tgt.tgt_node_name, WWN_SIZE);
4725 icb->firmware_options_1 |= __constant_cpu_to_le32(BIT_14);
4730 qlt_83xx_iospace_config(struct qla_hw_data *ha)
4732 if (!QLA_TGT_MODE_ENABLED())
4735 ha->msix_count += 1; /* For ATIO Q */
4739 qlt_24xx_process_response_error(struct scsi_qla_host *vha,
4740 struct sts_entry_24xx *pkt)
4742 switch (pkt->entry_type) {
4743 case ABTS_RECV_24XX:
4744 case ABTS_RESP_24XX:
4746 case NOTIFY_ACK_TYPE:
4754 qlt_modify_vp_config(struct scsi_qla_host *vha,
4755 struct vp_config_entry_24xx *vpmod)
4757 if (qla_tgt_mode_enabled(vha))
4758 vpmod->options_idx1 &= ~BIT_5;
4759 /* Disable ini mode, if requested */
4760 if (!qla_ini_mode_enabled(vha))
4761 vpmod->options_idx1 &= ~BIT_4;
4765 qlt_probe_one_stage1(struct scsi_qla_host *base_vha, struct qla_hw_data *ha)
4767 if (!QLA_TGT_MODE_ENABLED())
4770 if (ha->mqenable || IS_QLA83XX(ha)) {
4771 ISP_ATIO_Q_IN(base_vha) = &ha->mqiobase->isp25mq.atio_q_in;
4772 ISP_ATIO_Q_OUT(base_vha) = &ha->mqiobase->isp25mq.atio_q_out;
4774 ISP_ATIO_Q_IN(base_vha) = &ha->iobase->isp24.atio_q_in;
4775 ISP_ATIO_Q_OUT(base_vha) = &ha->iobase->isp24.atio_q_out;
4778 mutex_init(&base_vha->vha_tgt.tgt_mutex);
4779 mutex_init(&base_vha->vha_tgt.tgt_host_action_mutex);
4780 qlt_clear_mode(base_vha);
4784 qla83xx_msix_atio_q(int irq, void *dev_id)
4786 struct rsp_que *rsp;
4787 scsi_qla_host_t *vha;
4788 struct qla_hw_data *ha;
4789 unsigned long flags;
4791 rsp = (struct rsp_que *) dev_id;
4793 vha = pci_get_drvdata(ha->pdev);
4795 spin_lock_irqsave(&ha->hardware_lock, flags);
4797 qlt_24xx_process_atio_queue(vha);
4798 qla24xx_process_response_queue(vha, rsp);
4800 spin_unlock_irqrestore(&ha->hardware_lock, flags);
4806 qlt_mem_alloc(struct qla_hw_data *ha)
4808 if (!QLA_TGT_MODE_ENABLED())
4811 ha->tgt.tgt_vp_map = kzalloc(sizeof(struct qla_tgt_vp_map) *
4812 MAX_MULTI_ID_FABRIC, GFP_KERNEL);
4813 if (!ha->tgt.tgt_vp_map)
4816 ha->tgt.atio_ring = dma_alloc_coherent(&ha->pdev->dev,
4817 (ha->tgt.atio_q_length + 1) * sizeof(struct atio_from_isp),
4818 &ha->tgt.atio_dma, GFP_KERNEL);
4819 if (!ha->tgt.atio_ring) {
4820 kfree(ha->tgt.tgt_vp_map);
4827 qlt_mem_free(struct qla_hw_data *ha)
4829 if (!QLA_TGT_MODE_ENABLED())
4832 if (ha->tgt.atio_ring) {
4833 dma_free_coherent(&ha->pdev->dev, (ha->tgt.atio_q_length + 1) *
4834 sizeof(struct atio_from_isp), ha->tgt.atio_ring,
4837 kfree(ha->tgt.tgt_vp_map);
4840 /* vport_slock to be held by the caller */
4842 qlt_update_vp_map(struct scsi_qla_host *vha, int cmd)
4844 if (!QLA_TGT_MODE_ENABLED())
4849 vha->hw->tgt.tgt_vp_map[vha->vp_idx].vha = vha;
4852 vha->hw->tgt.tgt_vp_map[vha->d_id.b.al_pa].idx = vha->vp_idx;
4855 vha->hw->tgt.tgt_vp_map[vha->vp_idx].vha = NULL;
4858 vha->hw->tgt.tgt_vp_map[vha->d_id.b.al_pa].idx = 0;
4863 static int __init qlt_parse_ini_mode(void)
4865 if (strcasecmp(qlini_mode, QLA2XXX_INI_MODE_STR_EXCLUSIVE) == 0)
4866 ql2x_ini_mode = QLA2XXX_INI_MODE_EXCLUSIVE;
4867 else if (strcasecmp(qlini_mode, QLA2XXX_INI_MODE_STR_DISABLED) == 0)
4868 ql2x_ini_mode = QLA2XXX_INI_MODE_DISABLED;
4869 else if (strcasecmp(qlini_mode, QLA2XXX_INI_MODE_STR_ENABLED) == 0)
4870 ql2x_ini_mode = QLA2XXX_INI_MODE_ENABLED;
4877 int __init qlt_init(void)
4881 if (!qlt_parse_ini_mode()) {
4882 ql_log(ql_log_fatal, NULL, 0xe06b,
4883 "qlt_parse_ini_mode() failed\n");
4887 if (!QLA_TGT_MODE_ENABLED())
4890 qla_tgt_cmd_cachep = kmem_cache_create("qla_tgt_cmd_cachep",
4891 sizeof(struct qla_tgt_cmd), __alignof__(struct qla_tgt_cmd), 0,
4893 if (!qla_tgt_cmd_cachep) {
4894 ql_log(ql_log_fatal, NULL, 0xe06c,
4895 "kmem_cache_create for qla_tgt_cmd_cachep failed\n");
4899 qla_tgt_mgmt_cmd_cachep = kmem_cache_create("qla_tgt_mgmt_cmd_cachep",
4900 sizeof(struct qla_tgt_mgmt_cmd), __alignof__(struct
4901 qla_tgt_mgmt_cmd), 0, NULL);
4902 if (!qla_tgt_mgmt_cmd_cachep) {
4903 ql_log(ql_log_fatal, NULL, 0xe06d,
4904 "kmem_cache_create for qla_tgt_mgmt_cmd_cachep failed\n");
4909 qla_tgt_mgmt_cmd_mempool = mempool_create(25, mempool_alloc_slab,
4910 mempool_free_slab, qla_tgt_mgmt_cmd_cachep);
4911 if (!qla_tgt_mgmt_cmd_mempool) {
4912 ql_log(ql_log_fatal, NULL, 0xe06e,
4913 "mempool_create for qla_tgt_mgmt_cmd_mempool failed\n");
4915 goto out_mgmt_cmd_cachep;
4918 qla_tgt_wq = alloc_workqueue("qla_tgt_wq", 0, 0);
4920 ql_log(ql_log_fatal, NULL, 0xe06f,
4921 "alloc_workqueue for qla_tgt_wq failed\n");
4923 goto out_cmd_mempool;
4926 * Return 1 to signal that initiator-mode is being disabled
4928 return (ql2x_ini_mode == QLA2XXX_INI_MODE_DISABLED) ? 1 : 0;
4931 mempool_destroy(qla_tgt_mgmt_cmd_mempool);
4932 out_mgmt_cmd_cachep:
4933 kmem_cache_destroy(qla_tgt_mgmt_cmd_cachep);
4935 kmem_cache_destroy(qla_tgt_cmd_cachep);
4941 if (!QLA_TGT_MODE_ENABLED())
4944 destroy_workqueue(qla_tgt_wq);
4945 mempool_destroy(qla_tgt_mgmt_cmd_mempool);
4946 kmem_cache_destroy(qla_tgt_mgmt_cmd_cachep);
4947 kmem_cache_destroy(qla_tgt_cmd_cachep);