x86/xen: resume timer irqs early
[linux/fpc-iii.git] / drivers / scsi / qla2xxx / qla_target.c
blobe4fa6fb7e72a6b3a6fcc372762e14887f3e77665
1 /*
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
18 * of the License.
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>
42 #include "qla_def.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 "
51 "enabled back; "
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 {
61 FCP_TMF_CMPL = 0,
62 FCP_DATA_LEN_INVALID = 1,
63 FCP_CMND_FIELDS_INVALID = 2,
64 FCP_DATA_PARAM_MISMATCH = 3,
65 FCP_TMF_REJECTED = 4,
66 FCP_TMF_FAILED = 5,
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);
105 * Global Variables
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(
116 struct qla_tgt *tgt,
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))
123 return sess;
126 return NULL;
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",
138 vha->vp_idx);
140 return rc;
142 return QLA_SUCCESS;
145 static inline
146 struct scsi_qla_host *qlt_find_host_by_d_id(struct scsi_qla_host *vha,
147 uint8_t *d_id)
149 struct qla_hw_data *ha = vha->hw;
150 uint8_t vp_idx;
152 if ((vha->d_id.b.area != d_id[1]) || (vha->d_id.b.domain != d_id[0]))
153 return NULL;
155 if (vha->d_id.b.al_pa == d_id[2])
156 return vha;
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;
163 return NULL;
166 static inline
167 struct scsi_qla_host *qlt_find_host_by_vp_idx(struct scsi_qla_host *vha,
168 uint16_t vp_idx)
170 struct qla_hw_data *ha = vha->hw;
172 if (vha->vp_idx == vp_idx)
173 return vha;
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;
179 return NULL;
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) {
186 case ATIO_TYPE7:
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]);
197 break;
199 qlt_24xx_atio_pkt(host, atio);
200 break;
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);
219 break;
222 qlt_24xx_atio_pkt(host, atio);
223 break;
226 default:
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);
230 break;
233 return;
236 void qlt_response_pkt_all_vps(struct scsi_qla_host *vha, response_t *pkt)
238 switch (pkt->entry_type) {
239 case CTIO_TYPE7:
241 struct ctio7_from_24xx *entry = (struct ctio7_from_24xx *)pkt;
242 struct scsi_qla_host *host = qlt_find_host_by_vp_idx(vha,
243 entry->vp_index);
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);
249 break;
251 qlt_response_pkt(host, pkt);
252 break;
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);
267 break;
269 qlt_response_pkt(host, pkt);
270 break;
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);
288 break;
291 qlt_response_pkt(host, pkt);
292 break;
295 case ABTS_RECV_24XX:
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,
300 entry->vp_index);
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);
306 break;
308 qlt_response_pkt(host, pkt);
309 break;
312 case ABTS_RESP_24XX:
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,
317 entry->vp_index);
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);
323 break;
325 qlt_response_pkt(host, pkt);
326 break;
329 default:
330 qlt_response_pkt(vha, pkt);
331 break;
336 static void qlt_free_session_done(struct work_struct *work)
338 struct qla_tgt_sess *sess = container_of(work, struct qla_tgt_sess,
339 free_work);
340 struct qla_tgt *tgt = sess->tgt;
341 struct scsi_qla_host *vha = sess->vha;
342 struct qla_hw_data *ha = vha->hw;
344 BUG_ON(!tgt);
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);
354 kfree(sess);
356 * We need to protect against race, when tgt is freed before or
357 * inside wake_up()
359 tgt->sess_count--;
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);
372 if (sess->deleted)
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;
386 uint16_t loop_id;
387 int res = 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.. */
394 /* Global event */
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);
400 switch (mcmd) {
401 case QLA_TGT_NEXUS_LOSS_SESS:
402 mcmd = QLA_TGT_NEXUS_LOSS;
403 break;
404 case QLA_TGT_ABORT_ALL_SESS:
405 mcmd = QLA_TGT_ABORT_ALL;
406 break;
407 case QLA_TGT_NEXUS_LOSS:
408 case QLA_TGT_ABORT_ALL:
409 break;
410 default:
411 ql_dbg(ql_dbg_tgt, vha, 0xe046,
412 "qla_target(%d): Not allowed "
413 "command %x in %s", vha->vp_idx,
414 mcmd, __func__);
415 sess = NULL;
416 break;
418 } else
419 sess = NULL;
420 #endif
421 } else {
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);
427 if (!sess) {
428 res = -ESRCH;
429 return res;
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,
435 mcmd, loop_id);
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,
446 bool immediate)
448 struct qla_tgt *tgt = sess->tgt;
449 uint32_t dev_loss_tmo = tgt->ha->port_down_retry_count + 5;
451 if (sess->deleted)
452 return;
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);
457 sess->deleted = 1;
459 if (immediate)
460 dev_loss_tmo = 0;
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);
470 if (immediate)
471 schedule_delayed_work(&tgt->sess_del_work, 0);
472 else
473 schedule_delayed_work(&tgt->sess_del_work,
474 sess->expires - jiffies);
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,
489 uint16_t *loop_id)
491 struct qla_hw_data *ha = vha->hw;
492 dma_addr_t gid_list_dma;
493 struct gid_list_info *gid_list;
494 char *id_iter;
495 int res, rc, i;
496 uint16_t entries;
498 gid_list = dma_alloc_coherent(&ha->pdev->dev, qla2x00_gid_list_size(ha),
499 &gid_list_dma, GFP_KERNEL);
500 if (!gid_list) {
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));
504 return -ENOMEM;
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",
512 vha->vp_idx, rc);
513 res = -1;
514 goto out_free_id_list;
517 id_iter = (char *)gid_list;
518 res = -1;
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);
525 res = 0;
526 break;
528 id_iter += ha->gid_list_info_size;
531 out_free_id_list:
532 dma_free_coherent(&ha->pdev->dev, qla2x00_gid_list_size(ha),
533 gid_list, gid_list_dma);
534 return res;
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);
543 sess->deleted = 0;
546 static void qlt_del_sess_work_fn(struct delayed_work *work)
548 struct qla_tgt *tgt = container_of(work, struct qla_tgt,
549 sess_del_work);
550 struct scsi_qla_host *vha = tgt->vha;
551 struct qla_hw_data *ha = vha->hw;
552 struct qla_tgt_sess *sess;
553 unsigned long flags, elapsed;
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),
558 del_list_entry);
559 elapsed = jiffies;
560 if (time_after_eq(elapsed, sess->expires)) {
561 qlt_undelete_sess(sess);
563 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf004,
564 "Timeout: sess %p about to be deleted\n",
565 sess);
566 ha->tgt.tgt_ops->shutdown_sess(sess);
567 ha->tgt.tgt_ops->put_sess(sess);
568 } else {
569 schedule_delayed_work(&tgt->sess_del_work,
570 sess->expires - elapsed);
571 break;
574 spin_unlock_irqrestore(&ha->hardware_lock, flags);
578 * Adds an extra ref to allow to drop hw lock after adding sess to the list.
579 * Caller must put it.
581 static struct qla_tgt_sess *qlt_create_sess(
582 struct scsi_qla_host *vha,
583 fc_port_t *fcport,
584 bool local)
586 struct qla_hw_data *ha = vha->hw;
587 struct qla_tgt_sess *sess;
588 unsigned long flags;
589 unsigned char be_sid[3];
591 /* Check to avoid double sessions */
592 spin_lock_irqsave(&ha->hardware_lock, flags);
593 list_for_each_entry(sess, &ha->tgt.qla_tgt->sess_list,
594 sess_list_entry) {
595 if (!memcmp(sess->port_name, fcport->port_name, WWN_SIZE)) {
596 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf005,
597 "Double sess %p found (s_id %x:%x:%x, "
598 "loop_id %d), updating to d_id %x:%x:%x, "
599 "loop_id %d", sess, sess->s_id.b.domain,
600 sess->s_id.b.al_pa, sess->s_id.b.area,
601 sess->loop_id, fcport->d_id.b.domain,
602 fcport->d_id.b.al_pa, fcport->d_id.b.area,
603 fcport->loop_id);
605 if (sess->deleted)
606 qlt_undelete_sess(sess);
608 kref_get(&sess->se_sess->sess_kref);
609 ha->tgt.tgt_ops->update_sess(sess, fcport->d_id, fcport->loop_id,
610 (fcport->flags & FCF_CONF_COMP_SUPPORTED));
612 if (sess->local && !local)
613 sess->local = 0;
614 spin_unlock_irqrestore(&ha->hardware_lock, flags);
616 return sess;
619 spin_unlock_irqrestore(&ha->hardware_lock, flags);
621 sess = kzalloc(sizeof(*sess), GFP_KERNEL);
622 if (!sess) {
623 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04a,
624 "qla_target(%u): session allocation failed, all commands "
625 "from port %8phC will be refused", vha->vp_idx,
626 fcport->port_name);
628 return NULL;
630 sess->tgt = ha->tgt.qla_tgt;
631 sess->vha = vha;
632 sess->s_id = fcport->d_id;
633 sess->loop_id = fcport->loop_id;
634 sess->local = local;
636 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf006,
637 "Adding sess %p to tgt %p via ->check_initiator_node_acl()\n",
638 sess, ha->tgt.qla_tgt);
640 be_sid[0] = sess->s_id.b.domain;
641 be_sid[1] = sess->s_id.b.area;
642 be_sid[2] = sess->s_id.b.al_pa;
644 * Determine if this fc_port->port_name is allowed to access
645 * target mode using explict NodeACLs+MappedLUNs, or using
646 * TPG demo mode. If this is successful a target mode FC nexus
647 * is created.
649 if (ha->tgt.tgt_ops->check_initiator_node_acl(vha,
650 &fcport->port_name[0], sess, &be_sid[0], fcport->loop_id) < 0) {
651 kfree(sess);
652 return NULL;
655 * Take an extra reference to ->sess_kref here to handle qla_tgt_sess
656 * access across ->hardware_lock reaquire.
658 kref_get(&sess->se_sess->sess_kref);
660 sess->conf_compl_supported = (fcport->flags & FCF_CONF_COMP_SUPPORTED);
661 BUILD_BUG_ON(sizeof(sess->port_name) != sizeof(fcport->port_name));
662 memcpy(sess->port_name, fcport->port_name, sizeof(sess->port_name));
664 spin_lock_irqsave(&ha->hardware_lock, flags);
665 list_add_tail(&sess->sess_list_entry, &ha->tgt.qla_tgt->sess_list);
666 ha->tgt.qla_tgt->sess_count++;
667 spin_unlock_irqrestore(&ha->hardware_lock, flags);
669 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04b,
670 "qla_target(%d): %ssession for wwn %8phC (loop_id %d, "
671 "s_id %x:%x:%x, confirmed completion %ssupported) added\n",
672 vha->vp_idx, local ? "local " : "", fcport->port_name,
673 fcport->loop_id, sess->s_id.b.domain, sess->s_id.b.area,
674 sess->s_id.b.al_pa, sess->conf_compl_supported ? "" : "not ");
676 return sess;
680 * Called from drivers/scsi/qla2xxx/qla_init.c:qla2x00_reg_remote_port()
682 void qlt_fc_port_added(struct scsi_qla_host *vha, fc_port_t *fcport)
684 struct qla_hw_data *ha = vha->hw;
685 struct qla_tgt *tgt = ha->tgt.qla_tgt;
686 struct qla_tgt_sess *sess;
687 unsigned long flags;
689 if (!vha->hw->tgt.tgt_ops)
690 return;
692 if (!tgt || (fcport->port_type != FCT_INITIATOR))
693 return;
695 spin_lock_irqsave(&ha->hardware_lock, flags);
696 if (tgt->tgt_stop) {
697 spin_unlock_irqrestore(&ha->hardware_lock, flags);
698 return;
700 sess = qlt_find_sess_by_port_name(tgt, fcport->port_name);
701 if (!sess) {
702 spin_unlock_irqrestore(&ha->hardware_lock, flags);
704 mutex_lock(&ha->tgt.tgt_mutex);
705 sess = qlt_create_sess(vha, fcport, false);
706 mutex_unlock(&ha->tgt.tgt_mutex);
708 spin_lock_irqsave(&ha->hardware_lock, flags);
709 } else {
710 kref_get(&sess->se_sess->sess_kref);
712 if (sess->deleted) {
713 qlt_undelete_sess(sess);
715 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04c,
716 "qla_target(%u): %ssession for port %8phC "
717 "(loop ID %d) reappeared\n", vha->vp_idx,
718 sess->local ? "local " : "", sess->port_name,
719 sess->loop_id);
721 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf007,
722 "Reappeared sess %p\n", sess);
724 ha->tgt.tgt_ops->update_sess(sess, fcport->d_id, fcport->loop_id,
725 (fcport->flags & FCF_CONF_COMP_SUPPORTED));
728 if (sess && sess->local) {
729 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04d,
730 "qla_target(%u): local session for "
731 "port %8phC (loop ID %d) became global\n", vha->vp_idx,
732 fcport->port_name, sess->loop_id);
733 sess->local = 0;
735 ha->tgt.tgt_ops->put_sess(sess);
736 spin_unlock_irqrestore(&ha->hardware_lock, flags);
739 void qlt_fc_port_deleted(struct scsi_qla_host *vha, fc_port_t *fcport)
741 struct qla_hw_data *ha = vha->hw;
742 struct qla_tgt *tgt = ha->tgt.qla_tgt;
743 struct qla_tgt_sess *sess;
744 unsigned long flags;
746 if (!vha->hw->tgt.tgt_ops)
747 return;
749 if (!tgt || (fcport->port_type != FCT_INITIATOR))
750 return;
752 spin_lock_irqsave(&ha->hardware_lock, flags);
753 if (tgt->tgt_stop) {
754 spin_unlock_irqrestore(&ha->hardware_lock, flags);
755 return;
757 sess = qlt_find_sess_by_port_name(tgt, fcport->port_name);
758 if (!sess) {
759 spin_unlock_irqrestore(&ha->hardware_lock, flags);
760 return;
763 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf008, "qla_tgt_fc_port_deleted %p", sess);
765 sess->local = 1;
766 qlt_schedule_sess_for_deletion(sess, false);
767 spin_unlock_irqrestore(&ha->hardware_lock, flags);
770 static inline int test_tgt_sess_count(struct qla_tgt *tgt)
772 struct qla_hw_data *ha = tgt->ha;
773 unsigned long flags;
774 int res;
776 * We need to protect against race, when tgt is freed before or
777 * inside wake_up()
779 spin_lock_irqsave(&ha->hardware_lock, flags);
780 ql_dbg(ql_dbg_tgt, tgt->vha, 0xe002,
781 "tgt %p, empty(sess_list)=%d sess_count=%d\n",
782 tgt, list_empty(&tgt->sess_list), tgt->sess_count);
783 res = (tgt->sess_count == 0);
784 spin_unlock_irqrestore(&ha->hardware_lock, flags);
786 return res;
789 /* Called by tcm_qla2xxx configfs code */
790 void qlt_stop_phase1(struct qla_tgt *tgt)
792 struct scsi_qla_host *vha = tgt->vha;
793 struct qla_hw_data *ha = tgt->ha;
794 unsigned long flags;
796 if (tgt->tgt_stop || tgt->tgt_stopped) {
797 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04e,
798 "Already in tgt->tgt_stop or tgt_stopped state\n");
799 dump_stack();
800 return;
803 ql_dbg(ql_dbg_tgt, vha, 0xe003, "Stopping target for host %ld(%p)\n",
804 vha->host_no, vha);
806 * Mutex needed to sync with qla_tgt_fc_port_[added,deleted].
807 * Lock is needed, because we still can get an incoming packet.
809 mutex_lock(&ha->tgt.tgt_mutex);
810 spin_lock_irqsave(&ha->hardware_lock, flags);
811 tgt->tgt_stop = 1;
812 qlt_clear_tgt_db(tgt, true);
813 spin_unlock_irqrestore(&ha->hardware_lock, flags);
814 mutex_unlock(&ha->tgt.tgt_mutex);
816 flush_delayed_work(&tgt->sess_del_work);
818 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf009,
819 "Waiting for sess works (tgt %p)", tgt);
820 spin_lock_irqsave(&tgt->sess_work_lock, flags);
821 while (!list_empty(&tgt->sess_works_list)) {
822 spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
823 flush_scheduled_work();
824 spin_lock_irqsave(&tgt->sess_work_lock, flags);
826 spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
828 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00a,
829 "Waiting for tgt %p: list_empty(sess_list)=%d "
830 "sess_count=%d\n", tgt, list_empty(&tgt->sess_list),
831 tgt->sess_count);
833 wait_event(tgt->waitQ, test_tgt_sess_count(tgt));
835 /* Big hammer */
836 if (!ha->flags.host_shutting_down && qla_tgt_mode_enabled(vha))
837 qlt_disable_vha(vha);
839 /* Wait for sessions to clear out (just in case) */
840 wait_event(tgt->waitQ, test_tgt_sess_count(tgt));
842 EXPORT_SYMBOL(qlt_stop_phase1);
844 /* Called by tcm_qla2xxx configfs code */
845 void qlt_stop_phase2(struct qla_tgt *tgt)
847 struct qla_hw_data *ha = tgt->ha;
848 unsigned long flags;
850 if (tgt->tgt_stopped) {
851 ql_dbg(ql_dbg_tgt_mgt, tgt->vha, 0xf04f,
852 "Already in tgt->tgt_stopped state\n");
853 dump_stack();
854 return;
857 ql_dbg(ql_dbg_tgt_mgt, tgt->vha, 0xf00b,
858 "Waiting for %d IRQ commands to complete (tgt %p)",
859 tgt->irq_cmd_count, tgt);
861 mutex_lock(&ha->tgt.tgt_mutex);
862 spin_lock_irqsave(&ha->hardware_lock, flags);
863 while (tgt->irq_cmd_count != 0) {
864 spin_unlock_irqrestore(&ha->hardware_lock, flags);
865 udelay(2);
866 spin_lock_irqsave(&ha->hardware_lock, flags);
868 tgt->tgt_stop = 0;
869 tgt->tgt_stopped = 1;
870 spin_unlock_irqrestore(&ha->hardware_lock, flags);
871 mutex_unlock(&ha->tgt.tgt_mutex);
873 ql_dbg(ql_dbg_tgt_mgt, tgt->vha, 0xf00c, "Stop of tgt %p finished",
874 tgt);
876 EXPORT_SYMBOL(qlt_stop_phase2);
878 /* Called from qlt_remove_target() -> qla2x00_remove_one() */
879 static void qlt_release(struct qla_tgt *tgt)
881 struct qla_hw_data *ha = tgt->ha;
883 if ((ha->tgt.qla_tgt != NULL) && !tgt->tgt_stopped)
884 qlt_stop_phase2(tgt);
886 ha->tgt.qla_tgt = NULL;
888 ql_dbg(ql_dbg_tgt_mgt, tgt->vha, 0xf00d,
889 "Release of tgt %p finished\n", tgt);
891 kfree(tgt);
894 /* ha->hardware_lock supposed to be held on entry */
895 static int qlt_sched_sess_work(struct qla_tgt *tgt, int type,
896 const void *param, unsigned int param_size)
898 struct qla_tgt_sess_work_param *prm;
899 unsigned long flags;
901 prm = kzalloc(sizeof(*prm), GFP_ATOMIC);
902 if (!prm) {
903 ql_dbg(ql_dbg_tgt_mgt, tgt->vha, 0xf050,
904 "qla_target(%d): Unable to create session "
905 "work, command will be refused", 0);
906 return -ENOMEM;
909 ql_dbg(ql_dbg_tgt_mgt, tgt->vha, 0xf00e,
910 "Scheduling work (type %d, prm %p)"
911 " to find session for param %p (size %d, tgt %p)\n",
912 type, prm, param, param_size, tgt);
914 prm->type = type;
915 memcpy(&prm->tm_iocb, param, param_size);
917 spin_lock_irqsave(&tgt->sess_work_lock, flags);
918 list_add_tail(&prm->sess_works_list_entry, &tgt->sess_works_list);
919 spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
921 schedule_work(&tgt->sess_work);
923 return 0;
927 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
929 static void qlt_send_notify_ack(struct scsi_qla_host *vha,
930 struct imm_ntfy_from_isp *ntfy,
931 uint32_t add_flags, uint16_t resp_code, int resp_code_valid,
932 uint16_t srr_flags, uint16_t srr_reject_code, uint8_t srr_explan)
934 struct qla_hw_data *ha = vha->hw;
935 request_t *pkt;
936 struct nack_to_isp *nack;
938 ql_dbg(ql_dbg_tgt, vha, 0xe004, "Sending NOTIFY_ACK (ha=%p)\n", ha);
940 /* Send marker if required */
941 if (qlt_issue_marker(vha, 1) != QLA_SUCCESS)
942 return;
944 pkt = (request_t *)qla2x00_alloc_iocbs(vha, NULL);
945 if (!pkt) {
946 ql_dbg(ql_dbg_tgt, vha, 0xe049,
947 "qla_target(%d): %s failed: unable to allocate "
948 "request packet\n", vha->vp_idx, __func__);
949 return;
952 if (ha->tgt.qla_tgt != NULL)
953 ha->tgt.qla_tgt->notify_ack_expected++;
955 pkt->entry_type = NOTIFY_ACK_TYPE;
956 pkt->entry_count = 1;
958 nack = (struct nack_to_isp *)pkt;
959 nack->ox_id = ntfy->ox_id;
961 nack->u.isp24.nport_handle = ntfy->u.isp24.nport_handle;
962 if (le16_to_cpu(ntfy->u.isp24.status) == IMM_NTFY_ELS) {
963 nack->u.isp24.flags = ntfy->u.isp24.flags &
964 __constant_cpu_to_le32(NOTIFY24XX_FLAGS_PUREX_IOCB);
966 nack->u.isp24.srr_rx_id = ntfy->u.isp24.srr_rx_id;
967 nack->u.isp24.status = ntfy->u.isp24.status;
968 nack->u.isp24.status_subcode = ntfy->u.isp24.status_subcode;
969 nack->u.isp24.fw_handle = ntfy->u.isp24.fw_handle;
970 nack->u.isp24.exchange_address = ntfy->u.isp24.exchange_address;
971 nack->u.isp24.srr_rel_offs = ntfy->u.isp24.srr_rel_offs;
972 nack->u.isp24.srr_ui = ntfy->u.isp24.srr_ui;
973 nack->u.isp24.srr_flags = cpu_to_le16(srr_flags);
974 nack->u.isp24.srr_reject_code = srr_reject_code;
975 nack->u.isp24.srr_reject_code_expl = srr_explan;
976 nack->u.isp24.vp_index = ntfy->u.isp24.vp_index;
978 ql_dbg(ql_dbg_tgt, vha, 0xe005,
979 "qla_target(%d): Sending 24xx Notify Ack %d\n",
980 vha->vp_idx, nack->u.isp24.status);
982 qla2x00_start_iocbs(vha, vha->req);
986 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
988 static void qlt_24xx_send_abts_resp(struct scsi_qla_host *vha,
989 struct abts_recv_from_24xx *abts, uint32_t status,
990 bool ids_reversed)
992 struct qla_hw_data *ha = vha->hw;
993 struct abts_resp_to_24xx *resp;
994 uint32_t f_ctl;
995 uint8_t *p;
997 ql_dbg(ql_dbg_tgt, vha, 0xe006,
998 "Sending task mgmt ABTS response (ha=%p, atio=%p, status=%x\n",
999 ha, abts, status);
1001 /* Send marker if required */
1002 if (qlt_issue_marker(vha, 1) != QLA_SUCCESS)
1003 return;
1005 resp = (struct abts_resp_to_24xx *)qla2x00_alloc_iocbs(vha, NULL);
1006 if (!resp) {
1007 ql_dbg(ql_dbg_tgt, vha, 0xe04a,
1008 "qla_target(%d): %s failed: unable to allocate "
1009 "request packet", vha->vp_idx, __func__);
1010 return;
1013 resp->entry_type = ABTS_RESP_24XX;
1014 resp->entry_count = 1;
1015 resp->nport_handle = abts->nport_handle;
1016 resp->vp_index = vha->vp_idx;
1017 resp->sof_type = abts->sof_type;
1018 resp->exchange_address = abts->exchange_address;
1019 resp->fcp_hdr_le = abts->fcp_hdr_le;
1020 f_ctl = __constant_cpu_to_le32(F_CTL_EXCH_CONTEXT_RESP |
1021 F_CTL_LAST_SEQ | F_CTL_END_SEQ |
1022 F_CTL_SEQ_INITIATIVE);
1023 p = (uint8_t *)&f_ctl;
1024 resp->fcp_hdr_le.f_ctl[0] = *p++;
1025 resp->fcp_hdr_le.f_ctl[1] = *p++;
1026 resp->fcp_hdr_le.f_ctl[2] = *p;
1027 if (ids_reversed) {
1028 resp->fcp_hdr_le.d_id[0] = abts->fcp_hdr_le.d_id[0];
1029 resp->fcp_hdr_le.d_id[1] = abts->fcp_hdr_le.d_id[1];
1030 resp->fcp_hdr_le.d_id[2] = abts->fcp_hdr_le.d_id[2];
1031 resp->fcp_hdr_le.s_id[0] = abts->fcp_hdr_le.s_id[0];
1032 resp->fcp_hdr_le.s_id[1] = abts->fcp_hdr_le.s_id[1];
1033 resp->fcp_hdr_le.s_id[2] = abts->fcp_hdr_le.s_id[2];
1034 } else {
1035 resp->fcp_hdr_le.d_id[0] = abts->fcp_hdr_le.s_id[0];
1036 resp->fcp_hdr_le.d_id[1] = abts->fcp_hdr_le.s_id[1];
1037 resp->fcp_hdr_le.d_id[2] = abts->fcp_hdr_le.s_id[2];
1038 resp->fcp_hdr_le.s_id[0] = abts->fcp_hdr_le.d_id[0];
1039 resp->fcp_hdr_le.s_id[1] = abts->fcp_hdr_le.d_id[1];
1040 resp->fcp_hdr_le.s_id[2] = abts->fcp_hdr_le.d_id[2];
1042 resp->exchange_addr_to_abort = abts->exchange_addr_to_abort;
1043 if (status == FCP_TMF_CMPL) {
1044 resp->fcp_hdr_le.r_ctl = R_CTL_BASIC_LINK_SERV | R_CTL_B_ACC;
1045 resp->payload.ba_acct.seq_id_valid = SEQ_ID_INVALID;
1046 resp->payload.ba_acct.low_seq_cnt = 0x0000;
1047 resp->payload.ba_acct.high_seq_cnt = 0xFFFF;
1048 resp->payload.ba_acct.ox_id = abts->fcp_hdr_le.ox_id;
1049 resp->payload.ba_acct.rx_id = abts->fcp_hdr_le.rx_id;
1050 } else {
1051 resp->fcp_hdr_le.r_ctl = R_CTL_BASIC_LINK_SERV | R_CTL_B_RJT;
1052 resp->payload.ba_rjt.reason_code =
1053 BA_RJT_REASON_CODE_UNABLE_TO_PERFORM;
1054 /* Other bytes are zero */
1057 ha->tgt.qla_tgt->abts_resp_expected++;
1059 qla2x00_start_iocbs(vha, vha->req);
1063 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
1065 static void qlt_24xx_retry_term_exchange(struct scsi_qla_host *vha,
1066 struct abts_resp_from_24xx_fw *entry)
1068 struct ctio7_to_24xx *ctio;
1070 ql_dbg(ql_dbg_tgt, vha, 0xe007,
1071 "Sending retry TERM EXCH CTIO7 (ha=%p)\n", vha->hw);
1072 /* Send marker if required */
1073 if (qlt_issue_marker(vha, 1) != QLA_SUCCESS)
1074 return;
1076 ctio = (struct ctio7_to_24xx *)qla2x00_alloc_iocbs(vha, NULL);
1077 if (ctio == NULL) {
1078 ql_dbg(ql_dbg_tgt, vha, 0xe04b,
1079 "qla_target(%d): %s failed: unable to allocate "
1080 "request packet\n", vha->vp_idx, __func__);
1081 return;
1085 * We've got on entrance firmware's response on by us generated
1086 * ABTS response. So, in it ID fields are reversed.
1089 ctio->entry_type = CTIO_TYPE7;
1090 ctio->entry_count = 1;
1091 ctio->nport_handle = entry->nport_handle;
1092 ctio->handle = QLA_TGT_SKIP_HANDLE | CTIO_COMPLETION_HANDLE_MARK;
1093 ctio->timeout = __constant_cpu_to_le16(QLA_TGT_TIMEOUT);
1094 ctio->vp_index = vha->vp_idx;
1095 ctio->initiator_id[0] = entry->fcp_hdr_le.d_id[0];
1096 ctio->initiator_id[1] = entry->fcp_hdr_le.d_id[1];
1097 ctio->initiator_id[2] = entry->fcp_hdr_le.d_id[2];
1098 ctio->exchange_addr = entry->exchange_addr_to_abort;
1099 ctio->u.status1.flags =
1100 __constant_cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_1 |
1101 CTIO7_FLAGS_TERMINATE);
1102 ctio->u.status1.ox_id = entry->fcp_hdr_le.ox_id;
1104 qla2x00_start_iocbs(vha, vha->req);
1106 qlt_24xx_send_abts_resp(vha, (struct abts_recv_from_24xx *)entry,
1107 FCP_TMF_CMPL, true);
1110 /* ha->hardware_lock supposed to be held on entry */
1111 static int __qlt_24xx_handle_abts(struct scsi_qla_host *vha,
1112 struct abts_recv_from_24xx *abts, struct qla_tgt_sess *sess)
1114 struct qla_hw_data *ha = vha->hw;
1115 struct se_session *se_sess = sess->se_sess;
1116 struct qla_tgt_mgmt_cmd *mcmd;
1117 struct se_cmd *se_cmd;
1118 u32 lun = 0;
1119 int rc;
1120 bool found_lun = false;
1122 spin_lock(&se_sess->sess_cmd_lock);
1123 list_for_each_entry(se_cmd, &se_sess->sess_cmd_list, se_cmd_list) {
1124 struct qla_tgt_cmd *cmd =
1125 container_of(se_cmd, struct qla_tgt_cmd, se_cmd);
1126 if (cmd->tag == abts->exchange_addr_to_abort) {
1127 lun = cmd->unpacked_lun;
1128 found_lun = true;
1129 break;
1132 spin_unlock(&se_sess->sess_cmd_lock);
1134 if (!found_lun)
1135 return -ENOENT;
1137 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00f,
1138 "qla_target(%d): task abort (tag=%d)\n",
1139 vha->vp_idx, abts->exchange_addr_to_abort);
1141 mcmd = mempool_alloc(qla_tgt_mgmt_cmd_mempool, GFP_ATOMIC);
1142 if (mcmd == NULL) {
1143 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf051,
1144 "qla_target(%d): %s: Allocation of ABORT cmd failed",
1145 vha->vp_idx, __func__);
1146 return -ENOMEM;
1148 memset(mcmd, 0, sizeof(*mcmd));
1150 mcmd->sess = sess;
1151 memcpy(&mcmd->orig_iocb.abts, abts, sizeof(mcmd->orig_iocb.abts));
1153 rc = ha->tgt.tgt_ops->handle_tmr(mcmd, lun, TMR_ABORT_TASK,
1154 abts->exchange_addr_to_abort);
1155 if (rc != 0) {
1156 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf052,
1157 "qla_target(%d): tgt_ops->handle_tmr()"
1158 " failed: %d", vha->vp_idx, rc);
1159 mempool_free(mcmd, qla_tgt_mgmt_cmd_mempool);
1160 return -EFAULT;
1163 return 0;
1167 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
1169 static void qlt_24xx_handle_abts(struct scsi_qla_host *vha,
1170 struct abts_recv_from_24xx *abts)
1172 struct qla_hw_data *ha = vha->hw;
1173 struct qla_tgt_sess *sess;
1174 uint32_t tag = abts->exchange_addr_to_abort;
1175 uint8_t s_id[3];
1176 int rc;
1178 if (le32_to_cpu(abts->fcp_hdr_le.parameter) & ABTS_PARAM_ABORT_SEQ) {
1179 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf053,
1180 "qla_target(%d): ABTS: Abort Sequence not "
1181 "supported\n", vha->vp_idx);
1182 qlt_24xx_send_abts_resp(vha, abts, FCP_TMF_REJECTED, false);
1183 return;
1186 if (tag == ATIO_EXCHANGE_ADDRESS_UNKNOWN) {
1187 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf010,
1188 "qla_target(%d): ABTS: Unknown Exchange "
1189 "Address received\n", vha->vp_idx);
1190 qlt_24xx_send_abts_resp(vha, abts, FCP_TMF_REJECTED, false);
1191 return;
1194 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf011,
1195 "qla_target(%d): task abort (s_id=%x:%x:%x, "
1196 "tag=%d, param=%x)\n", vha->vp_idx, abts->fcp_hdr_le.s_id[2],
1197 abts->fcp_hdr_le.s_id[1], abts->fcp_hdr_le.s_id[0], tag,
1198 le32_to_cpu(abts->fcp_hdr_le.parameter));
1200 s_id[0] = abts->fcp_hdr_le.s_id[2];
1201 s_id[1] = abts->fcp_hdr_le.s_id[1];
1202 s_id[2] = abts->fcp_hdr_le.s_id[0];
1204 sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha, s_id);
1205 if (!sess) {
1206 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf012,
1207 "qla_target(%d): task abort for non-existant session\n",
1208 vha->vp_idx);
1209 rc = qlt_sched_sess_work(ha->tgt.qla_tgt,
1210 QLA_TGT_SESS_WORK_ABORT, abts, sizeof(*abts));
1211 if (rc != 0) {
1212 qlt_24xx_send_abts_resp(vha, abts, FCP_TMF_REJECTED,
1213 false);
1215 return;
1218 rc = __qlt_24xx_handle_abts(vha, abts, sess);
1219 if (rc != 0) {
1220 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf054,
1221 "qla_target(%d): __qlt_24xx_handle_abts() failed: %d\n",
1222 vha->vp_idx, rc);
1223 qlt_24xx_send_abts_resp(vha, abts, FCP_TMF_REJECTED, false);
1224 return;
1229 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
1231 static void qlt_24xx_send_task_mgmt_ctio(struct scsi_qla_host *ha,
1232 struct qla_tgt_mgmt_cmd *mcmd, uint32_t resp_code)
1234 struct atio_from_isp *atio = &mcmd->orig_iocb.atio;
1235 struct ctio7_to_24xx *ctio;
1237 ql_dbg(ql_dbg_tgt, ha, 0xe008,
1238 "Sending task mgmt CTIO7 (ha=%p, atio=%p, resp_code=%x\n",
1239 ha, atio, resp_code);
1241 /* Send marker if required */
1242 if (qlt_issue_marker(ha, 1) != QLA_SUCCESS)
1243 return;
1245 ctio = (struct ctio7_to_24xx *)qla2x00_alloc_iocbs(ha, NULL);
1246 if (ctio == NULL) {
1247 ql_dbg(ql_dbg_tgt, ha, 0xe04c,
1248 "qla_target(%d): %s failed: unable to allocate "
1249 "request packet\n", ha->vp_idx, __func__);
1250 return;
1253 ctio->entry_type = CTIO_TYPE7;
1254 ctio->entry_count = 1;
1255 ctio->handle = QLA_TGT_SKIP_HANDLE | CTIO_COMPLETION_HANDLE_MARK;
1256 ctio->nport_handle = mcmd->sess->loop_id;
1257 ctio->timeout = __constant_cpu_to_le16(QLA_TGT_TIMEOUT);
1258 ctio->vp_index = ha->vp_idx;
1259 ctio->initiator_id[0] = atio->u.isp24.fcp_hdr.s_id[2];
1260 ctio->initiator_id[1] = atio->u.isp24.fcp_hdr.s_id[1];
1261 ctio->initiator_id[2] = atio->u.isp24.fcp_hdr.s_id[0];
1262 ctio->exchange_addr = atio->u.isp24.exchange_addr;
1263 ctio->u.status1.flags = (atio->u.isp24.attr << 9) |
1264 __constant_cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_1 |
1265 CTIO7_FLAGS_SEND_STATUS);
1266 ctio->u.status1.ox_id = swab16(atio->u.isp24.fcp_hdr.ox_id);
1267 ctio->u.status1.scsi_status =
1268 __constant_cpu_to_le16(SS_RESPONSE_INFO_LEN_VALID);
1269 ctio->u.status1.response_len = __constant_cpu_to_le16(8);
1270 ctio->u.status1.sense_data[0] = resp_code;
1272 qla2x00_start_iocbs(ha, ha->req);
1275 void qlt_free_mcmd(struct qla_tgt_mgmt_cmd *mcmd)
1277 mempool_free(mcmd, qla_tgt_mgmt_cmd_mempool);
1279 EXPORT_SYMBOL(qlt_free_mcmd);
1281 /* callback from target fabric module code */
1282 void qlt_xmit_tm_rsp(struct qla_tgt_mgmt_cmd *mcmd)
1284 struct scsi_qla_host *vha = mcmd->sess->vha;
1285 struct qla_hw_data *ha = vha->hw;
1286 unsigned long flags;
1288 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf013,
1289 "TM response mcmd (%p) status %#x state %#x",
1290 mcmd, mcmd->fc_tm_rsp, mcmd->flags);
1292 spin_lock_irqsave(&ha->hardware_lock, flags);
1293 if (mcmd->flags == QLA24XX_MGMT_SEND_NACK)
1294 qlt_send_notify_ack(vha, &mcmd->orig_iocb.imm_ntfy,
1295 0, 0, 0, 0, 0, 0);
1296 else {
1297 if (mcmd->se_cmd.se_tmr_req->function == TMR_ABORT_TASK)
1298 qlt_24xx_send_abts_resp(vha, &mcmd->orig_iocb.abts,
1299 mcmd->fc_tm_rsp, false);
1300 else
1301 qlt_24xx_send_task_mgmt_ctio(vha, mcmd,
1302 mcmd->fc_tm_rsp);
1305 * Make the callback for ->free_mcmd() to queue_work() and invoke
1306 * target_put_sess_cmd() to drop cmd_kref to 1. The final
1307 * target_put_sess_cmd() call will be made from TFO->check_stop_free()
1308 * -> tcm_qla2xxx_check_stop_free() to release the TMR associated se_cmd
1309 * descriptor after TFO->queue_tm_rsp() -> tcm_qla2xxx_queue_tm_rsp() ->
1310 * qlt_xmit_tm_rsp() returns here..
1312 ha->tgt.tgt_ops->free_mcmd(mcmd);
1313 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1315 EXPORT_SYMBOL(qlt_xmit_tm_rsp);
1317 /* No locks */
1318 static int qlt_pci_map_calc_cnt(struct qla_tgt_prm *prm)
1320 struct qla_tgt_cmd *cmd = prm->cmd;
1322 BUG_ON(cmd->sg_cnt == 0);
1324 prm->sg = (struct scatterlist *)cmd->sg;
1325 prm->seg_cnt = pci_map_sg(prm->tgt->ha->pdev, cmd->sg,
1326 cmd->sg_cnt, cmd->dma_data_direction);
1327 if (unlikely(prm->seg_cnt == 0))
1328 goto out_err;
1330 prm->cmd->sg_mapped = 1;
1333 * If greater than four sg entries then we need to allocate
1334 * the continuation entries
1336 if (prm->seg_cnt > prm->tgt->datasegs_per_cmd)
1337 prm->req_cnt += DIV_ROUND_UP(prm->seg_cnt -
1338 prm->tgt->datasegs_per_cmd, prm->tgt->datasegs_per_cont);
1340 ql_dbg(ql_dbg_tgt, prm->cmd->vha, 0xe009, "seg_cnt=%d, req_cnt=%d\n",
1341 prm->seg_cnt, prm->req_cnt);
1342 return 0;
1344 out_err:
1345 ql_dbg(ql_dbg_tgt, prm->cmd->vha, 0xe04d,
1346 "qla_target(%d): PCI mapping failed: sg_cnt=%d",
1347 0, prm->cmd->sg_cnt);
1348 return -1;
1351 static inline void qlt_unmap_sg(struct scsi_qla_host *vha,
1352 struct qla_tgt_cmd *cmd)
1354 struct qla_hw_data *ha = vha->hw;
1356 BUG_ON(!cmd->sg_mapped);
1357 pci_unmap_sg(ha->pdev, cmd->sg, cmd->sg_cnt, cmd->dma_data_direction);
1358 cmd->sg_mapped = 0;
1361 static int qlt_check_reserve_free_req(struct scsi_qla_host *vha,
1362 uint32_t req_cnt)
1364 struct qla_hw_data *ha = vha->hw;
1365 device_reg_t __iomem *reg = ha->iobase;
1366 uint32_t cnt;
1368 if (vha->req->cnt < (req_cnt + 2)) {
1369 cnt = (uint16_t)RD_REG_DWORD(&reg->isp24.req_q_out);
1371 ql_dbg(ql_dbg_tgt, vha, 0xe00a,
1372 "Request ring circled: cnt=%d, vha->->ring_index=%d, "
1373 "vha->req->cnt=%d, req_cnt=%d\n", cnt,
1374 vha->req->ring_index, vha->req->cnt, req_cnt);
1375 if (vha->req->ring_index < cnt)
1376 vha->req->cnt = cnt - vha->req->ring_index;
1377 else
1378 vha->req->cnt = vha->req->length -
1379 (vha->req->ring_index - cnt);
1382 if (unlikely(vha->req->cnt < (req_cnt + 2))) {
1383 ql_dbg(ql_dbg_tgt, vha, 0xe00b,
1384 "qla_target(%d): There is no room in the "
1385 "request ring: vha->req->ring_index=%d, vha->req->cnt=%d, "
1386 "req_cnt=%d\n", vha->vp_idx, vha->req->ring_index,
1387 vha->req->cnt, req_cnt);
1388 return -EAGAIN;
1390 vha->req->cnt -= req_cnt;
1392 return 0;
1396 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
1398 static inline void *qlt_get_req_pkt(struct scsi_qla_host *vha)
1400 /* Adjust ring index. */
1401 vha->req->ring_index++;
1402 if (vha->req->ring_index == vha->req->length) {
1403 vha->req->ring_index = 0;
1404 vha->req->ring_ptr = vha->req->ring;
1405 } else {
1406 vha->req->ring_ptr++;
1408 return (cont_entry_t *)vha->req->ring_ptr;
1411 /* ha->hardware_lock supposed to be held on entry */
1412 static inline uint32_t qlt_make_handle(struct scsi_qla_host *vha)
1414 struct qla_hw_data *ha = vha->hw;
1415 uint32_t h;
1417 h = ha->tgt.current_handle;
1418 /* always increment cmd handle */
1419 do {
1420 ++h;
1421 if (h > DEFAULT_OUTSTANDING_COMMANDS)
1422 h = 1; /* 0 is QLA_TGT_NULL_HANDLE */
1423 if (h == ha->tgt.current_handle) {
1424 ql_dbg(ql_dbg_tgt, vha, 0xe04e,
1425 "qla_target(%d): Ran out of "
1426 "empty cmd slots in ha %p\n", vha->vp_idx, ha);
1427 h = QLA_TGT_NULL_HANDLE;
1428 break;
1430 } while ((h == QLA_TGT_NULL_HANDLE) ||
1431 (h == QLA_TGT_SKIP_HANDLE) ||
1432 (ha->tgt.cmds[h-1] != NULL));
1434 if (h != QLA_TGT_NULL_HANDLE)
1435 ha->tgt.current_handle = h;
1437 return h;
1440 /* ha->hardware_lock supposed to be held on entry */
1441 static int qlt_24xx_build_ctio_pkt(struct qla_tgt_prm *prm,
1442 struct scsi_qla_host *vha)
1444 uint32_t h;
1445 struct ctio7_to_24xx *pkt;
1446 struct qla_hw_data *ha = vha->hw;
1447 struct atio_from_isp *atio = &prm->cmd->atio;
1449 pkt = (struct ctio7_to_24xx *)vha->req->ring_ptr;
1450 prm->pkt = pkt;
1451 memset(pkt, 0, sizeof(*pkt));
1453 pkt->entry_type = CTIO_TYPE7;
1454 pkt->entry_count = (uint8_t)prm->req_cnt;
1455 pkt->vp_index = vha->vp_idx;
1457 h = qlt_make_handle(vha);
1458 if (unlikely(h == QLA_TGT_NULL_HANDLE)) {
1460 * CTIO type 7 from the firmware doesn't provide a way to
1461 * know the initiator's LOOP ID, hence we can't find
1462 * the session and, so, the command.
1464 return -EAGAIN;
1465 } else
1466 ha->tgt.cmds[h-1] = prm->cmd;
1468 pkt->handle = h | CTIO_COMPLETION_HANDLE_MARK;
1469 pkt->nport_handle = prm->cmd->loop_id;
1470 pkt->timeout = __constant_cpu_to_le16(QLA_TGT_TIMEOUT);
1471 pkt->initiator_id[0] = atio->u.isp24.fcp_hdr.s_id[2];
1472 pkt->initiator_id[1] = atio->u.isp24.fcp_hdr.s_id[1];
1473 pkt->initiator_id[2] = atio->u.isp24.fcp_hdr.s_id[0];
1474 pkt->exchange_addr = atio->u.isp24.exchange_addr;
1475 pkt->u.status0.flags |= (atio->u.isp24.attr << 9);
1476 pkt->u.status0.ox_id = swab16(atio->u.isp24.fcp_hdr.ox_id);
1477 pkt->u.status0.relative_offset = cpu_to_le32(prm->cmd->offset);
1479 ql_dbg(ql_dbg_tgt, vha, 0xe00c,
1480 "qla_target(%d): handle(cmd) -> %08x, timeout %d, ox_id %#x\n",
1481 vha->vp_idx, pkt->handle, QLA_TGT_TIMEOUT,
1482 le16_to_cpu(pkt->u.status0.ox_id));
1483 return 0;
1487 * ha->hardware_lock supposed to be held on entry. We have already made sure
1488 * that there is sufficient amount of request entries to not drop it.
1490 static void qlt_load_cont_data_segments(struct qla_tgt_prm *prm,
1491 struct scsi_qla_host *vha)
1493 int cnt;
1494 uint32_t *dword_ptr;
1495 int enable_64bit_addressing = prm->tgt->tgt_enable_64bit_addr;
1497 /* Build continuation packets */
1498 while (prm->seg_cnt > 0) {
1499 cont_a64_entry_t *cont_pkt64 =
1500 (cont_a64_entry_t *)qlt_get_req_pkt(vha);
1503 * Make sure that from cont_pkt64 none of
1504 * 64-bit specific fields used for 32-bit
1505 * addressing. Cast to (cont_entry_t *) for
1506 * that.
1509 memset(cont_pkt64, 0, sizeof(*cont_pkt64));
1511 cont_pkt64->entry_count = 1;
1512 cont_pkt64->sys_define = 0;
1514 if (enable_64bit_addressing) {
1515 cont_pkt64->entry_type = CONTINUE_A64_TYPE;
1516 dword_ptr =
1517 (uint32_t *)&cont_pkt64->dseg_0_address;
1518 } else {
1519 cont_pkt64->entry_type = CONTINUE_TYPE;
1520 dword_ptr =
1521 (uint32_t *)&((cont_entry_t *)
1522 cont_pkt64)->dseg_0_address;
1525 /* Load continuation entry data segments */
1526 for (cnt = 0;
1527 cnt < prm->tgt->datasegs_per_cont && prm->seg_cnt;
1528 cnt++, prm->seg_cnt--) {
1529 *dword_ptr++ =
1530 cpu_to_le32(pci_dma_lo32
1531 (sg_dma_address(prm->sg)));
1532 if (enable_64bit_addressing) {
1533 *dword_ptr++ =
1534 cpu_to_le32(pci_dma_hi32
1535 (sg_dma_address
1536 (prm->sg)));
1538 *dword_ptr++ = cpu_to_le32(sg_dma_len(prm->sg));
1540 ql_dbg(ql_dbg_tgt, vha, 0xe00d,
1541 "S/G Segment Cont. phys_addr=%llx:%llx, len=%d\n",
1542 (long long unsigned int)
1543 pci_dma_hi32(sg_dma_address(prm->sg)),
1544 (long long unsigned int)
1545 pci_dma_lo32(sg_dma_address(prm->sg)),
1546 (int)sg_dma_len(prm->sg));
1548 prm->sg = sg_next(prm->sg);
1554 * ha->hardware_lock supposed to be held on entry. We have already made sure
1555 * that there is sufficient amount of request entries to not drop it.
1557 static void qlt_load_data_segments(struct qla_tgt_prm *prm,
1558 struct scsi_qla_host *vha)
1560 int cnt;
1561 uint32_t *dword_ptr;
1562 int enable_64bit_addressing = prm->tgt->tgt_enable_64bit_addr;
1563 struct ctio7_to_24xx *pkt24 = (struct ctio7_to_24xx *)prm->pkt;
1565 ql_dbg(ql_dbg_tgt, vha, 0xe00e,
1566 "iocb->scsi_status=%x, iocb->flags=%x\n",
1567 le16_to_cpu(pkt24->u.status0.scsi_status),
1568 le16_to_cpu(pkt24->u.status0.flags));
1570 pkt24->u.status0.transfer_length = cpu_to_le32(prm->cmd->bufflen);
1572 /* Setup packet address segment pointer */
1573 dword_ptr = pkt24->u.status0.dseg_0_address;
1575 /* Set total data segment count */
1576 if (prm->seg_cnt)
1577 pkt24->dseg_count = cpu_to_le16(prm->seg_cnt);
1579 if (prm->seg_cnt == 0) {
1580 /* No data transfer */
1581 *dword_ptr++ = 0;
1582 *dword_ptr = 0;
1583 return;
1586 /* If scatter gather */
1587 ql_dbg(ql_dbg_tgt, vha, 0xe00f, "%s", "Building S/G data segments...");
1589 /* Load command entry data segments */
1590 for (cnt = 0;
1591 (cnt < prm->tgt->datasegs_per_cmd) && prm->seg_cnt;
1592 cnt++, prm->seg_cnt--) {
1593 *dword_ptr++ =
1594 cpu_to_le32(pci_dma_lo32(sg_dma_address(prm->sg)));
1595 if (enable_64bit_addressing) {
1596 *dword_ptr++ =
1597 cpu_to_le32(pci_dma_hi32(
1598 sg_dma_address(prm->sg)));
1600 *dword_ptr++ = cpu_to_le32(sg_dma_len(prm->sg));
1602 ql_dbg(ql_dbg_tgt, vha, 0xe010,
1603 "S/G Segment phys_addr=%llx:%llx, len=%d\n",
1604 (long long unsigned int)pci_dma_hi32(sg_dma_address(
1605 prm->sg)),
1606 (long long unsigned int)pci_dma_lo32(sg_dma_address(
1607 prm->sg)),
1608 (int)sg_dma_len(prm->sg));
1610 prm->sg = sg_next(prm->sg);
1613 qlt_load_cont_data_segments(prm, vha);
1616 static inline int qlt_has_data(struct qla_tgt_cmd *cmd)
1618 return cmd->bufflen > 0;
1622 * Called without ha->hardware_lock held
1624 static int qlt_pre_xmit_response(struct qla_tgt_cmd *cmd,
1625 struct qla_tgt_prm *prm, int xmit_type, uint8_t scsi_status,
1626 uint32_t *full_req_cnt)
1628 struct qla_tgt *tgt = cmd->tgt;
1629 struct scsi_qla_host *vha = tgt->vha;
1630 struct qla_hw_data *ha = vha->hw;
1631 struct se_cmd *se_cmd = &cmd->se_cmd;
1633 if (unlikely(cmd->aborted)) {
1634 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf014,
1635 "qla_target(%d): terminating exchange "
1636 "for aborted cmd=%p (se_cmd=%p, tag=%d)", vha->vp_idx, cmd,
1637 se_cmd, cmd->tag);
1639 cmd->state = QLA_TGT_STATE_ABORTED;
1641 qlt_send_term_exchange(vha, cmd, &cmd->atio, 0);
1643 /* !! At this point cmd could be already freed !! */
1644 return QLA_TGT_PRE_XMIT_RESP_CMD_ABORTED;
1647 ql_dbg(ql_dbg_tgt, vha, 0xe011, "qla_target(%d): tag=%u\n",
1648 vha->vp_idx, cmd->tag);
1650 prm->cmd = cmd;
1651 prm->tgt = tgt;
1652 prm->rq_result = scsi_status;
1653 prm->sense_buffer = &cmd->sense_buffer[0];
1654 prm->sense_buffer_len = TRANSPORT_SENSE_BUFFER;
1655 prm->sg = NULL;
1656 prm->seg_cnt = -1;
1657 prm->req_cnt = 1;
1658 prm->add_status_pkt = 0;
1660 ql_dbg(ql_dbg_tgt, vha, 0xe012, "rq_result=%x, xmit_type=%x\n",
1661 prm->rq_result, xmit_type);
1663 /* Send marker if required */
1664 if (qlt_issue_marker(vha, 0) != QLA_SUCCESS)
1665 return -EFAULT;
1667 ql_dbg(ql_dbg_tgt, vha, 0xe013, "CTIO start: vha(%d)\n", vha->vp_idx);
1669 if ((xmit_type & QLA_TGT_XMIT_DATA) && qlt_has_data(cmd)) {
1670 if (qlt_pci_map_calc_cnt(prm) != 0)
1671 return -EAGAIN;
1674 *full_req_cnt = prm->req_cnt;
1676 if (se_cmd->se_cmd_flags & SCF_UNDERFLOW_BIT) {
1677 prm->residual = se_cmd->residual_count;
1678 ql_dbg(ql_dbg_tgt, vha, 0xe014,
1679 "Residual underflow: %d (tag %d, "
1680 "op %x, bufflen %d, rq_result %x)\n", prm->residual,
1681 cmd->tag, se_cmd->t_task_cdb ? se_cmd->t_task_cdb[0] : 0,
1682 cmd->bufflen, prm->rq_result);
1683 prm->rq_result |= SS_RESIDUAL_UNDER;
1684 } else if (se_cmd->se_cmd_flags & SCF_OVERFLOW_BIT) {
1685 prm->residual = se_cmd->residual_count;
1686 ql_dbg(ql_dbg_tgt, vha, 0xe015,
1687 "Residual overflow: %d (tag %d, "
1688 "op %x, bufflen %d, rq_result %x)\n", prm->residual,
1689 cmd->tag, se_cmd->t_task_cdb ? se_cmd->t_task_cdb[0] : 0,
1690 cmd->bufflen, prm->rq_result);
1691 prm->rq_result |= SS_RESIDUAL_OVER;
1694 if (xmit_type & QLA_TGT_XMIT_STATUS) {
1696 * If QLA_TGT_XMIT_DATA is not set, add_status_pkt will be
1697 * ignored in *xmit_response() below
1699 if (qlt_has_data(cmd)) {
1700 if (QLA_TGT_SENSE_VALID(prm->sense_buffer) ||
1701 (IS_FWI2_CAPABLE(ha) &&
1702 (prm->rq_result != 0))) {
1703 prm->add_status_pkt = 1;
1704 (*full_req_cnt)++;
1709 ql_dbg(ql_dbg_tgt, vha, 0xe016,
1710 "req_cnt=%d, full_req_cnt=%d, add_status_pkt=%d\n",
1711 prm->req_cnt, *full_req_cnt, prm->add_status_pkt);
1713 return 0;
1716 static inline int qlt_need_explicit_conf(struct qla_hw_data *ha,
1717 struct qla_tgt_cmd *cmd, int sending_sense)
1719 if (ha->tgt.enable_class_2)
1720 return 0;
1722 if (sending_sense)
1723 return cmd->conf_compl_supported;
1724 else
1725 return ha->tgt.enable_explicit_conf &&
1726 cmd->conf_compl_supported;
1729 #ifdef CONFIG_QLA_TGT_DEBUG_SRR
1731 * Original taken from the XFS code
1733 static unsigned long qlt_srr_random(void)
1735 static int Inited;
1736 static unsigned long RandomValue;
1737 static DEFINE_SPINLOCK(lock);
1738 /* cycles pseudo-randomly through all values between 1 and 2^31 - 2 */
1739 register long rv;
1740 register long lo;
1741 register long hi;
1742 unsigned long flags;
1744 spin_lock_irqsave(&lock, flags);
1745 if (!Inited) {
1746 RandomValue = jiffies;
1747 Inited = 1;
1749 rv = RandomValue;
1750 hi = rv / 127773;
1751 lo = rv % 127773;
1752 rv = 16807 * lo - 2836 * hi;
1753 if (rv <= 0)
1754 rv += 2147483647;
1755 RandomValue = rv;
1756 spin_unlock_irqrestore(&lock, flags);
1757 return rv;
1760 static void qlt_check_srr_debug(struct qla_tgt_cmd *cmd, int *xmit_type)
1762 #if 0 /* This is not a real status packets lost, so it won't lead to SRR */
1763 if ((*xmit_type & QLA_TGT_XMIT_STATUS) && (qlt_srr_random() % 200)
1764 == 50) {
1765 *xmit_type &= ~QLA_TGT_XMIT_STATUS;
1766 ql_dbg(ql_dbg_tgt_mgt, cmd->vha, 0xf015,
1767 "Dropping cmd %p (tag %d) status", cmd, cmd->tag);
1769 #endif
1771 * It's currently not possible to simulate SRRs for FCP_WRITE without
1772 * a physical link layer failure, so don't even try here..
1774 if (cmd->dma_data_direction != DMA_FROM_DEVICE)
1775 return;
1777 if (qlt_has_data(cmd) && (cmd->sg_cnt > 1) &&
1778 ((qlt_srr_random() % 100) == 20)) {
1779 int i, leave = 0;
1780 unsigned int tot_len = 0;
1782 while (leave == 0)
1783 leave = qlt_srr_random() % cmd->sg_cnt;
1785 for (i = 0; i < leave; i++)
1786 tot_len += cmd->sg[i].length;
1788 ql_dbg(ql_dbg_tgt_mgt, cmd->vha, 0xf016,
1789 "Cutting cmd %p (tag %d) buffer"
1790 " tail to len %d, sg_cnt %d (cmd->bufflen %d,"
1791 " cmd->sg_cnt %d)", cmd, cmd->tag, tot_len, leave,
1792 cmd->bufflen, cmd->sg_cnt);
1794 cmd->bufflen = tot_len;
1795 cmd->sg_cnt = leave;
1798 if (qlt_has_data(cmd) && ((qlt_srr_random() % 100) == 70)) {
1799 unsigned int offset = qlt_srr_random() % cmd->bufflen;
1801 ql_dbg(ql_dbg_tgt_mgt, cmd->vha, 0xf017,
1802 "Cutting cmd %p (tag %d) buffer head "
1803 "to offset %d (cmd->bufflen %d)", cmd, cmd->tag, offset,
1804 cmd->bufflen);
1805 if (offset == 0)
1806 *xmit_type &= ~QLA_TGT_XMIT_DATA;
1807 else if (qlt_set_data_offset(cmd, offset)) {
1808 ql_dbg(ql_dbg_tgt_mgt, cmd->vha, 0xf018,
1809 "qlt_set_data_offset() failed (tag %d)", cmd->tag);
1813 #else
1814 static inline void qlt_check_srr_debug(struct qla_tgt_cmd *cmd, int *xmit_type)
1816 #endif
1818 static void qlt_24xx_init_ctio_to_isp(struct ctio7_to_24xx *ctio,
1819 struct qla_tgt_prm *prm)
1821 prm->sense_buffer_len = min_t(uint32_t, prm->sense_buffer_len,
1822 (uint32_t)sizeof(ctio->u.status1.sense_data));
1823 ctio->u.status0.flags |=
1824 __constant_cpu_to_le16(CTIO7_FLAGS_SEND_STATUS);
1825 if (qlt_need_explicit_conf(prm->tgt->ha, prm->cmd, 0)) {
1826 ctio->u.status0.flags |= __constant_cpu_to_le16(
1827 CTIO7_FLAGS_EXPLICIT_CONFORM |
1828 CTIO7_FLAGS_CONFORM_REQ);
1830 ctio->u.status0.residual = cpu_to_le32(prm->residual);
1831 ctio->u.status0.scsi_status = cpu_to_le16(prm->rq_result);
1832 if (QLA_TGT_SENSE_VALID(prm->sense_buffer)) {
1833 int i;
1835 if (qlt_need_explicit_conf(prm->tgt->ha, prm->cmd, 1)) {
1836 if (prm->cmd->se_cmd.scsi_status != 0) {
1837 ql_dbg(ql_dbg_tgt, prm->cmd->vha, 0xe017,
1838 "Skipping EXPLICIT_CONFORM and "
1839 "CTIO7_FLAGS_CONFORM_REQ for FCP READ w/ "
1840 "non GOOD status\n");
1841 goto skip_explict_conf;
1843 ctio->u.status1.flags |= __constant_cpu_to_le16(
1844 CTIO7_FLAGS_EXPLICIT_CONFORM |
1845 CTIO7_FLAGS_CONFORM_REQ);
1847 skip_explict_conf:
1848 ctio->u.status1.flags &=
1849 ~__constant_cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_0);
1850 ctio->u.status1.flags |=
1851 __constant_cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_1);
1852 ctio->u.status1.scsi_status |=
1853 __constant_cpu_to_le16(SS_SENSE_LEN_VALID);
1854 ctio->u.status1.sense_length =
1855 cpu_to_le16(prm->sense_buffer_len);
1856 for (i = 0; i < prm->sense_buffer_len/4; i++)
1857 ((uint32_t *)ctio->u.status1.sense_data)[i] =
1858 cpu_to_be32(((uint32_t *)prm->sense_buffer)[i]);
1859 #if 0
1860 if (unlikely((prm->sense_buffer_len % 4) != 0)) {
1861 static int q;
1862 if (q < 10) {
1863 ql_dbg(ql_dbg_tgt, vha, 0xe04f,
1864 "qla_target(%d): %d bytes of sense "
1865 "lost", prm->tgt->ha->vp_idx,
1866 prm->sense_buffer_len % 4);
1867 q++;
1870 #endif
1871 } else {
1872 ctio->u.status1.flags &=
1873 ~__constant_cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_0);
1874 ctio->u.status1.flags |=
1875 __constant_cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_1);
1876 ctio->u.status1.sense_length = 0;
1877 memset(ctio->u.status1.sense_data, 0,
1878 sizeof(ctio->u.status1.sense_data));
1881 /* Sense with len > 24, is it possible ??? */
1885 * Callback to setup response of xmit_type of QLA_TGT_XMIT_DATA and *
1886 * QLA_TGT_XMIT_STATUS for >= 24xx silicon
1888 int qlt_xmit_response(struct qla_tgt_cmd *cmd, int xmit_type,
1889 uint8_t scsi_status)
1891 struct scsi_qla_host *vha = cmd->vha;
1892 struct qla_hw_data *ha = vha->hw;
1893 struct ctio7_to_24xx *pkt;
1894 struct qla_tgt_prm prm;
1895 uint32_t full_req_cnt = 0;
1896 unsigned long flags = 0;
1897 int res;
1899 memset(&prm, 0, sizeof(prm));
1900 qlt_check_srr_debug(cmd, &xmit_type);
1902 ql_dbg(ql_dbg_tgt, cmd->vha, 0xe018,
1903 "is_send_status=%d, cmd->bufflen=%d, cmd->sg_cnt=%d, "
1904 "cmd->dma_data_direction=%d\n", (xmit_type & QLA_TGT_XMIT_STATUS) ?
1905 1 : 0, cmd->bufflen, cmd->sg_cnt, cmd->dma_data_direction);
1907 res = qlt_pre_xmit_response(cmd, &prm, xmit_type, scsi_status,
1908 &full_req_cnt);
1909 if (unlikely(res != 0)) {
1910 if (res == QLA_TGT_PRE_XMIT_RESP_CMD_ABORTED)
1911 return 0;
1913 return res;
1916 spin_lock_irqsave(&ha->hardware_lock, flags);
1918 /* Does F/W have an IOCBs for this request */
1919 res = qlt_check_reserve_free_req(vha, full_req_cnt);
1920 if (unlikely(res))
1921 goto out_unmap_unlock;
1923 res = qlt_24xx_build_ctio_pkt(&prm, vha);
1924 if (unlikely(res != 0))
1925 goto out_unmap_unlock;
1928 pkt = (struct ctio7_to_24xx *)prm.pkt;
1930 if (qlt_has_data(cmd) && (xmit_type & QLA_TGT_XMIT_DATA)) {
1931 pkt->u.status0.flags |=
1932 __constant_cpu_to_le16(CTIO7_FLAGS_DATA_IN |
1933 CTIO7_FLAGS_STATUS_MODE_0);
1935 qlt_load_data_segments(&prm, vha);
1937 if (prm.add_status_pkt == 0) {
1938 if (xmit_type & QLA_TGT_XMIT_STATUS) {
1939 pkt->u.status0.scsi_status =
1940 cpu_to_le16(prm.rq_result);
1941 pkt->u.status0.residual =
1942 cpu_to_le32(prm.residual);
1943 pkt->u.status0.flags |= __constant_cpu_to_le16(
1944 CTIO7_FLAGS_SEND_STATUS);
1945 if (qlt_need_explicit_conf(ha, cmd, 0)) {
1946 pkt->u.status0.flags |=
1947 __constant_cpu_to_le16(
1948 CTIO7_FLAGS_EXPLICIT_CONFORM |
1949 CTIO7_FLAGS_CONFORM_REQ);
1953 } else {
1955 * We have already made sure that there is sufficient
1956 * amount of request entries to not drop HW lock in
1957 * req_pkt().
1959 struct ctio7_to_24xx *ctio =
1960 (struct ctio7_to_24xx *)qlt_get_req_pkt(vha);
1962 ql_dbg(ql_dbg_tgt, vha, 0xe019,
1963 "Building additional status packet\n");
1965 memcpy(ctio, pkt, sizeof(*ctio));
1966 ctio->entry_count = 1;
1967 ctio->dseg_count = 0;
1968 ctio->u.status1.flags &= ~__constant_cpu_to_le16(
1969 CTIO7_FLAGS_DATA_IN);
1971 /* Real finish is ctio_m1's finish */
1972 pkt->handle |= CTIO_INTERMEDIATE_HANDLE_MARK;
1973 pkt->u.status0.flags |= __constant_cpu_to_le16(
1974 CTIO7_FLAGS_DONT_RET_CTIO);
1975 qlt_24xx_init_ctio_to_isp((struct ctio7_to_24xx *)ctio,
1976 &prm);
1977 pr_debug("Status CTIO7: %p\n", ctio);
1979 } else
1980 qlt_24xx_init_ctio_to_isp(pkt, &prm);
1983 cmd->state = QLA_TGT_STATE_PROCESSED; /* Mid-level is done processing */
1985 ql_dbg(ql_dbg_tgt, vha, 0xe01a,
1986 "Xmitting CTIO7 response pkt for 24xx: %p scsi_status: 0x%02x\n",
1987 pkt, scsi_status);
1989 qla2x00_start_iocbs(vha, vha->req);
1990 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1992 return 0;
1994 out_unmap_unlock:
1995 if (cmd->sg_mapped)
1996 qlt_unmap_sg(vha, cmd);
1997 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1999 return res;
2001 EXPORT_SYMBOL(qlt_xmit_response);
2003 int qlt_rdy_to_xfer(struct qla_tgt_cmd *cmd)
2005 struct ctio7_to_24xx *pkt;
2006 struct scsi_qla_host *vha = cmd->vha;
2007 struct qla_hw_data *ha = vha->hw;
2008 struct qla_tgt *tgt = cmd->tgt;
2009 struct qla_tgt_prm prm;
2010 unsigned long flags;
2011 int res = 0;
2013 memset(&prm, 0, sizeof(prm));
2014 prm.cmd = cmd;
2015 prm.tgt = tgt;
2016 prm.sg = NULL;
2017 prm.req_cnt = 1;
2019 /* Send marker if required */
2020 if (qlt_issue_marker(vha, 0) != QLA_SUCCESS)
2021 return -EIO;
2023 ql_dbg(ql_dbg_tgt, vha, 0xe01b, "CTIO_start: vha(%d)",
2024 (int)vha->vp_idx);
2026 /* Calculate number of entries and segments required */
2027 if (qlt_pci_map_calc_cnt(&prm) != 0)
2028 return -EAGAIN;
2030 spin_lock_irqsave(&ha->hardware_lock, flags);
2032 /* Does F/W have an IOCBs for this request */
2033 res = qlt_check_reserve_free_req(vha, prm.req_cnt);
2034 if (res != 0)
2035 goto out_unlock_free_unmap;
2037 res = qlt_24xx_build_ctio_pkt(&prm, vha);
2038 if (unlikely(res != 0))
2039 goto out_unlock_free_unmap;
2040 pkt = (struct ctio7_to_24xx *)prm.pkt;
2041 pkt->u.status0.flags |= __constant_cpu_to_le16(CTIO7_FLAGS_DATA_OUT |
2042 CTIO7_FLAGS_STATUS_MODE_0);
2043 qlt_load_data_segments(&prm, vha);
2045 cmd->state = QLA_TGT_STATE_NEED_DATA;
2047 qla2x00_start_iocbs(vha, vha->req);
2048 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2050 return res;
2052 out_unlock_free_unmap:
2053 if (cmd->sg_mapped)
2054 qlt_unmap_sg(vha, cmd);
2055 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2057 return res;
2059 EXPORT_SYMBOL(qlt_rdy_to_xfer);
2061 /* If hardware_lock held on entry, might drop it, then reaquire */
2062 /* This function sends the appropriate CTIO to ISP 2xxx or 24xx */
2063 static int __qlt_send_term_exchange(struct scsi_qla_host *vha,
2064 struct qla_tgt_cmd *cmd,
2065 struct atio_from_isp *atio)
2067 struct ctio7_to_24xx *ctio24;
2068 struct qla_hw_data *ha = vha->hw;
2069 request_t *pkt;
2070 int ret = 0;
2072 ql_dbg(ql_dbg_tgt, vha, 0xe01c, "Sending TERM EXCH CTIO (ha=%p)\n", ha);
2074 pkt = (request_t *)qla2x00_alloc_iocbs(vha, NULL);
2075 if (pkt == NULL) {
2076 ql_dbg(ql_dbg_tgt, vha, 0xe050,
2077 "qla_target(%d): %s failed: unable to allocate "
2078 "request packet\n", vha->vp_idx, __func__);
2079 return -ENOMEM;
2082 if (cmd != NULL) {
2083 if (cmd->state < QLA_TGT_STATE_PROCESSED) {
2084 ql_dbg(ql_dbg_tgt, vha, 0xe051,
2085 "qla_target(%d): Terminating cmd %p with "
2086 "incorrect state %d\n", vha->vp_idx, cmd,
2087 cmd->state);
2088 } else
2089 ret = 1;
2092 pkt->entry_count = 1;
2093 pkt->handle = QLA_TGT_SKIP_HANDLE | CTIO_COMPLETION_HANDLE_MARK;
2095 ctio24 = (struct ctio7_to_24xx *)pkt;
2096 ctio24->entry_type = CTIO_TYPE7;
2097 ctio24->nport_handle = cmd ? cmd->loop_id : CTIO7_NHANDLE_UNRECOGNIZED;
2098 ctio24->timeout = __constant_cpu_to_le16(QLA_TGT_TIMEOUT);
2099 ctio24->vp_index = vha->vp_idx;
2100 ctio24->initiator_id[0] = atio->u.isp24.fcp_hdr.s_id[2];
2101 ctio24->initiator_id[1] = atio->u.isp24.fcp_hdr.s_id[1];
2102 ctio24->initiator_id[2] = atio->u.isp24.fcp_hdr.s_id[0];
2103 ctio24->exchange_addr = atio->u.isp24.exchange_addr;
2104 ctio24->u.status1.flags = (atio->u.isp24.attr << 9) |
2105 __constant_cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_1 |
2106 CTIO7_FLAGS_TERMINATE);
2107 ctio24->u.status1.ox_id = swab16(atio->u.isp24.fcp_hdr.ox_id);
2109 /* Most likely, it isn't needed */
2110 ctio24->u.status1.residual = get_unaligned((uint32_t *)
2111 &atio->u.isp24.fcp_cmnd.add_cdb[
2112 atio->u.isp24.fcp_cmnd.add_cdb_len]);
2113 if (ctio24->u.status1.residual != 0)
2114 ctio24->u.status1.scsi_status |= SS_RESIDUAL_UNDER;
2116 qla2x00_start_iocbs(vha, vha->req);
2117 return ret;
2120 static void qlt_send_term_exchange(struct scsi_qla_host *vha,
2121 struct qla_tgt_cmd *cmd, struct atio_from_isp *atio, int ha_locked)
2123 unsigned long flags;
2124 int rc;
2126 if (qlt_issue_marker(vha, ha_locked) < 0)
2127 return;
2129 if (ha_locked) {
2130 rc = __qlt_send_term_exchange(vha, cmd, atio);
2131 goto done;
2133 spin_lock_irqsave(&vha->hw->hardware_lock, flags);
2134 rc = __qlt_send_term_exchange(vha, cmd, atio);
2135 spin_unlock_irqrestore(&vha->hw->hardware_lock, flags);
2136 done:
2137 if (rc == 1) {
2138 if (!ha_locked && !in_interrupt())
2139 msleep(250); /* just in case */
2141 vha->hw->tgt.tgt_ops->free_cmd(cmd);
2145 void qlt_free_cmd(struct qla_tgt_cmd *cmd)
2147 BUG_ON(cmd->sg_mapped);
2149 if (unlikely(cmd->free_sg))
2150 kfree(cmd->sg);
2151 kmem_cache_free(qla_tgt_cmd_cachep, cmd);
2153 EXPORT_SYMBOL(qlt_free_cmd);
2155 /* ha->hardware_lock supposed to be held on entry */
2156 static int qlt_prepare_srr_ctio(struct scsi_qla_host *vha,
2157 struct qla_tgt_cmd *cmd, void *ctio)
2159 struct qla_tgt_srr_ctio *sc;
2160 struct qla_hw_data *ha = vha->hw;
2161 struct qla_tgt *tgt = ha->tgt.qla_tgt;
2162 struct qla_tgt_srr_imm *imm;
2164 tgt->ctio_srr_id++;
2166 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf019,
2167 "qla_target(%d): CTIO with SRR status received\n", vha->vp_idx);
2169 if (!ctio) {
2170 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf055,
2171 "qla_target(%d): SRR CTIO, but ctio is NULL\n",
2172 vha->vp_idx);
2173 return -EINVAL;
2176 sc = kzalloc(sizeof(*sc), GFP_ATOMIC);
2177 if (sc != NULL) {
2178 sc->cmd = cmd;
2179 /* IRQ is already OFF */
2180 spin_lock(&tgt->srr_lock);
2181 sc->srr_id = tgt->ctio_srr_id;
2182 list_add_tail(&sc->srr_list_entry,
2183 &tgt->srr_ctio_list);
2184 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01a,
2185 "CTIO SRR %p added (id %d)\n", sc, sc->srr_id);
2186 if (tgt->imm_srr_id == tgt->ctio_srr_id) {
2187 int found = 0;
2188 list_for_each_entry(imm, &tgt->srr_imm_list,
2189 srr_list_entry) {
2190 if (imm->srr_id == sc->srr_id) {
2191 found = 1;
2192 break;
2195 if (found) {
2196 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01b,
2197 "Scheduling srr work\n");
2198 schedule_work(&tgt->srr_work);
2199 } else {
2200 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf056,
2201 "qla_target(%d): imm_srr_id "
2202 "== ctio_srr_id (%d), but there is no "
2203 "corresponding SRR IMM, deleting CTIO "
2204 "SRR %p\n", vha->vp_idx,
2205 tgt->ctio_srr_id, sc);
2206 list_del(&sc->srr_list_entry);
2207 spin_unlock(&tgt->srr_lock);
2209 kfree(sc);
2210 return -EINVAL;
2213 spin_unlock(&tgt->srr_lock);
2214 } else {
2215 struct qla_tgt_srr_imm *ti;
2217 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf057,
2218 "qla_target(%d): Unable to allocate SRR CTIO entry\n",
2219 vha->vp_idx);
2220 spin_lock(&tgt->srr_lock);
2221 list_for_each_entry_safe(imm, ti, &tgt->srr_imm_list,
2222 srr_list_entry) {
2223 if (imm->srr_id == tgt->ctio_srr_id) {
2224 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01c,
2225 "IMM SRR %p deleted (id %d)\n",
2226 imm, imm->srr_id);
2227 list_del(&imm->srr_list_entry);
2228 qlt_reject_free_srr_imm(vha, imm, 1);
2231 spin_unlock(&tgt->srr_lock);
2233 return -ENOMEM;
2236 return 0;
2240 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
2242 static int qlt_term_ctio_exchange(struct scsi_qla_host *vha, void *ctio,
2243 struct qla_tgt_cmd *cmd, uint32_t status)
2245 int term = 0;
2247 if (ctio != NULL) {
2248 struct ctio7_from_24xx *c = (struct ctio7_from_24xx *)ctio;
2249 term = !(c->flags &
2250 __constant_cpu_to_le16(OF_TERM_EXCH));
2251 } else
2252 term = 1;
2254 if (term)
2255 qlt_send_term_exchange(vha, cmd, &cmd->atio, 1);
2257 return term;
2260 /* ha->hardware_lock supposed to be held on entry */
2261 static inline struct qla_tgt_cmd *qlt_get_cmd(struct scsi_qla_host *vha,
2262 uint32_t handle)
2264 struct qla_hw_data *ha = vha->hw;
2266 handle--;
2267 if (ha->tgt.cmds[handle] != NULL) {
2268 struct qla_tgt_cmd *cmd = ha->tgt.cmds[handle];
2269 ha->tgt.cmds[handle] = NULL;
2270 return cmd;
2271 } else
2272 return NULL;
2275 /* ha->hardware_lock supposed to be held on entry */
2276 static struct qla_tgt_cmd *qlt_ctio_to_cmd(struct scsi_qla_host *vha,
2277 uint32_t handle, void *ctio)
2279 struct qla_tgt_cmd *cmd = NULL;
2281 /* Clear out internal marks */
2282 handle &= ~(CTIO_COMPLETION_HANDLE_MARK |
2283 CTIO_INTERMEDIATE_HANDLE_MARK);
2285 if (handle != QLA_TGT_NULL_HANDLE) {
2286 if (unlikely(handle == QLA_TGT_SKIP_HANDLE)) {
2287 ql_dbg(ql_dbg_tgt, vha, 0xe01d, "%s",
2288 "SKIP_HANDLE CTIO\n");
2289 return NULL;
2291 /* handle-1 is actually used */
2292 if (unlikely(handle > DEFAULT_OUTSTANDING_COMMANDS)) {
2293 ql_dbg(ql_dbg_tgt, vha, 0xe052,
2294 "qla_target(%d): Wrong handle %x received\n",
2295 vha->vp_idx, handle);
2296 return NULL;
2298 cmd = qlt_get_cmd(vha, handle);
2299 if (unlikely(cmd == NULL)) {
2300 ql_dbg(ql_dbg_tgt, vha, 0xe053,
2301 "qla_target(%d): Suspicious: unable to "
2302 "find the command with handle %x\n", vha->vp_idx,
2303 handle);
2304 return NULL;
2306 } else if (ctio != NULL) {
2307 /* We can't get loop ID from CTIO7 */
2308 ql_dbg(ql_dbg_tgt, vha, 0xe054,
2309 "qla_target(%d): Wrong CTIO received: QLA24xx doesn't "
2310 "support NULL handles\n", vha->vp_idx);
2311 return NULL;
2314 return cmd;
2318 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
2320 static void qlt_do_ctio_completion(struct scsi_qla_host *vha, uint32_t handle,
2321 uint32_t status, void *ctio)
2323 struct qla_hw_data *ha = vha->hw;
2324 struct se_cmd *se_cmd;
2325 struct target_core_fabric_ops *tfo;
2326 struct qla_tgt_cmd *cmd;
2328 ql_dbg(ql_dbg_tgt, vha, 0xe01e,
2329 "qla_target(%d): handle(ctio %p status %#x) <- %08x\n",
2330 vha->vp_idx, ctio, status, handle);
2332 if (handle & CTIO_INTERMEDIATE_HANDLE_MARK) {
2333 /* That could happen only in case of an error/reset/abort */
2334 if (status != CTIO_SUCCESS) {
2335 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01d,
2336 "Intermediate CTIO received"
2337 " (status %x)\n", status);
2339 return;
2342 cmd = qlt_ctio_to_cmd(vha, handle, ctio);
2343 if (cmd == NULL)
2344 return;
2346 se_cmd = &cmd->se_cmd;
2347 tfo = se_cmd->se_tfo;
2349 if (cmd->sg_mapped)
2350 qlt_unmap_sg(vha, cmd);
2352 if (unlikely(status != CTIO_SUCCESS)) {
2353 switch (status & 0xFFFF) {
2354 case CTIO_LIP_RESET:
2355 case CTIO_TARGET_RESET:
2356 case CTIO_ABORTED:
2357 case CTIO_TIMEOUT:
2358 case CTIO_INVALID_RX_ID:
2359 /* They are OK */
2360 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf058,
2361 "qla_target(%d): CTIO with "
2362 "status %#x received, state %x, se_cmd %p, "
2363 "(LIP_RESET=e, ABORTED=2, TARGET_RESET=17, "
2364 "TIMEOUT=b, INVALID_RX_ID=8)\n", vha->vp_idx,
2365 status, cmd->state, se_cmd);
2366 break;
2368 case CTIO_PORT_LOGGED_OUT:
2369 case CTIO_PORT_UNAVAILABLE:
2370 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf059,
2371 "qla_target(%d): CTIO with PORT LOGGED "
2372 "OUT (29) or PORT UNAVAILABLE (28) status %x "
2373 "received (state %x, se_cmd %p)\n", vha->vp_idx,
2374 status, cmd->state, se_cmd);
2375 break;
2377 case CTIO_SRR_RECEIVED:
2378 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05a,
2379 "qla_target(%d): CTIO with SRR_RECEIVED"
2380 " status %x received (state %x, se_cmd %p)\n",
2381 vha->vp_idx, status, cmd->state, se_cmd);
2382 if (qlt_prepare_srr_ctio(vha, cmd, ctio) != 0)
2383 break;
2384 else
2385 return;
2387 default:
2388 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05b,
2389 "qla_target(%d): CTIO with error status "
2390 "0x%x received (state %x, se_cmd %p\n",
2391 vha->vp_idx, status, cmd->state, se_cmd);
2392 break;
2395 if (cmd->state != QLA_TGT_STATE_NEED_DATA)
2396 if (qlt_term_ctio_exchange(vha, ctio, cmd, status))
2397 return;
2400 if (cmd->state == QLA_TGT_STATE_PROCESSED) {
2401 ql_dbg(ql_dbg_tgt, vha, 0xe01f, "Command %p finished\n", cmd);
2402 } else if (cmd->state == QLA_TGT_STATE_NEED_DATA) {
2403 int rx_status = 0;
2405 cmd->state = QLA_TGT_STATE_DATA_IN;
2407 if (unlikely(status != CTIO_SUCCESS))
2408 rx_status = -EIO;
2409 else
2410 cmd->write_data_transferred = 1;
2412 ql_dbg(ql_dbg_tgt, vha, 0xe020,
2413 "Data received, context %x, rx_status %d\n",
2414 0x0, rx_status);
2416 ha->tgt.tgt_ops->handle_data(cmd);
2417 return;
2418 } else if (cmd->state == QLA_TGT_STATE_ABORTED) {
2419 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01e,
2420 "Aborted command %p (tag %d) finished\n", cmd, cmd->tag);
2421 } else {
2422 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05c,
2423 "qla_target(%d): A command in state (%d) should "
2424 "not return a CTIO complete\n", vha->vp_idx, cmd->state);
2427 if (unlikely(status != CTIO_SUCCESS)) {
2428 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01f, "Finishing failed CTIO\n");
2429 dump_stack();
2432 ha->tgt.tgt_ops->free_cmd(cmd);
2435 static inline int qlt_get_fcp_task_attr(struct scsi_qla_host *vha,
2436 uint8_t task_codes)
2438 int fcp_task_attr;
2440 switch (task_codes) {
2441 case ATIO_SIMPLE_QUEUE:
2442 fcp_task_attr = MSG_SIMPLE_TAG;
2443 break;
2444 case ATIO_HEAD_OF_QUEUE:
2445 fcp_task_attr = MSG_HEAD_TAG;
2446 break;
2447 case ATIO_ORDERED_QUEUE:
2448 fcp_task_attr = MSG_ORDERED_TAG;
2449 break;
2450 case ATIO_ACA_QUEUE:
2451 fcp_task_attr = MSG_ACA_TAG;
2452 break;
2453 case ATIO_UNTAGGED:
2454 fcp_task_attr = MSG_SIMPLE_TAG;
2455 break;
2456 default:
2457 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05d,
2458 "qla_target: unknown task code %x, use ORDERED instead\n",
2459 task_codes);
2460 fcp_task_attr = MSG_ORDERED_TAG;
2461 break;
2464 return fcp_task_attr;
2467 static struct qla_tgt_sess *qlt_make_local_sess(struct scsi_qla_host *,
2468 uint8_t *);
2470 * Process context for I/O path into tcm_qla2xxx code
2472 static void qlt_do_work(struct work_struct *work)
2474 struct qla_tgt_cmd *cmd = container_of(work, struct qla_tgt_cmd, work);
2475 scsi_qla_host_t *vha = cmd->vha;
2476 struct qla_hw_data *ha = vha->hw;
2477 struct qla_tgt *tgt = ha->tgt.qla_tgt;
2478 struct qla_tgt_sess *sess = NULL;
2479 struct atio_from_isp *atio = &cmd->atio;
2480 unsigned char *cdb;
2481 unsigned long flags;
2482 uint32_t data_length;
2483 int ret, fcp_task_attr, data_dir, bidi = 0;
2485 if (tgt->tgt_stop)
2486 goto out_term;
2488 spin_lock_irqsave(&ha->hardware_lock, flags);
2489 sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha,
2490 atio->u.isp24.fcp_hdr.s_id);
2491 /* Do kref_get() before dropping qla_hw_data->hardware_lock. */
2492 if (sess)
2493 kref_get(&sess->se_sess->sess_kref);
2494 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2496 if (unlikely(!sess)) {
2497 uint8_t *s_id = atio->u.isp24.fcp_hdr.s_id;
2499 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf022,
2500 "qla_target(%d): Unable to find wwn login"
2501 " (s_id %x:%x:%x), trying to create it manually\n",
2502 vha->vp_idx, s_id[0], s_id[1], s_id[2]);
2504 if (atio->u.raw.entry_count > 1) {
2505 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf023,
2506 "Dropping multy entry cmd %p\n", cmd);
2507 goto out_term;
2510 mutex_lock(&ha->tgt.tgt_mutex);
2511 sess = qlt_make_local_sess(vha, s_id);
2512 /* sess has an extra creation ref. */
2513 mutex_unlock(&ha->tgt.tgt_mutex);
2515 if (!sess)
2516 goto out_term;
2519 cmd->sess = sess;
2520 cmd->loop_id = sess->loop_id;
2521 cmd->conf_compl_supported = sess->conf_compl_supported;
2523 cdb = &atio->u.isp24.fcp_cmnd.cdb[0];
2524 cmd->tag = atio->u.isp24.exchange_addr;
2525 cmd->unpacked_lun = scsilun_to_int(
2526 (struct scsi_lun *)&atio->u.isp24.fcp_cmnd.lun);
2528 if (atio->u.isp24.fcp_cmnd.rddata &&
2529 atio->u.isp24.fcp_cmnd.wrdata) {
2530 bidi = 1;
2531 data_dir = DMA_TO_DEVICE;
2532 } else if (atio->u.isp24.fcp_cmnd.rddata)
2533 data_dir = DMA_FROM_DEVICE;
2534 else if (atio->u.isp24.fcp_cmnd.wrdata)
2535 data_dir = DMA_TO_DEVICE;
2536 else
2537 data_dir = DMA_NONE;
2539 fcp_task_attr = qlt_get_fcp_task_attr(vha,
2540 atio->u.isp24.fcp_cmnd.task_attr);
2541 data_length = be32_to_cpu(get_unaligned((uint32_t *)
2542 &atio->u.isp24.fcp_cmnd.add_cdb[
2543 atio->u.isp24.fcp_cmnd.add_cdb_len]));
2545 ql_dbg(ql_dbg_tgt, vha, 0xe022,
2546 "qla_target: START qla command: %p lun: 0x%04x (tag %d)\n",
2547 cmd, cmd->unpacked_lun, cmd->tag);
2549 ret = vha->hw->tgt.tgt_ops->handle_cmd(vha, cmd, cdb, data_length,
2550 fcp_task_attr, data_dir, bidi);
2551 if (ret != 0)
2552 goto out_term;
2554 * Drop extra session reference from qla_tgt_handle_cmd_for_atio*(
2556 spin_lock_irqsave(&ha->hardware_lock, flags);
2557 ha->tgt.tgt_ops->put_sess(sess);
2558 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2559 return;
2561 out_term:
2562 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf020, "Terminating work cmd %p", cmd);
2564 * cmd has not sent to target yet, so pass NULL as the second
2565 * argument to qlt_send_term_exchange() and free the memory here.
2567 spin_lock_irqsave(&ha->hardware_lock, flags);
2568 qlt_send_term_exchange(vha, NULL, &cmd->atio, 1);
2569 kmem_cache_free(qla_tgt_cmd_cachep, cmd);
2570 if (sess)
2571 ha->tgt.tgt_ops->put_sess(sess);
2572 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2575 /* ha->hardware_lock supposed to be held on entry */
2576 static int qlt_handle_cmd_for_atio(struct scsi_qla_host *vha,
2577 struct atio_from_isp *atio)
2579 struct qla_hw_data *ha = vha->hw;
2580 struct qla_tgt *tgt = ha->tgt.qla_tgt;
2581 struct qla_tgt_cmd *cmd;
2583 if (unlikely(tgt->tgt_stop)) {
2584 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf021,
2585 "New command while device %p is shutting down\n", tgt);
2586 return -EFAULT;
2589 cmd = kmem_cache_zalloc(qla_tgt_cmd_cachep, GFP_ATOMIC);
2590 if (!cmd) {
2591 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05e,
2592 "qla_target(%d): Allocation of cmd failed\n", vha->vp_idx);
2593 return -ENOMEM;
2596 INIT_LIST_HEAD(&cmd->cmd_list);
2598 memcpy(&cmd->atio, atio, sizeof(*atio));
2599 cmd->state = QLA_TGT_STATE_NEW;
2600 cmd->tgt = ha->tgt.qla_tgt;
2601 cmd->vha = vha;
2603 INIT_WORK(&cmd->work, qlt_do_work);
2604 queue_work(qla_tgt_wq, &cmd->work);
2605 return 0;
2609 /* ha->hardware_lock supposed to be held on entry */
2610 static int qlt_issue_task_mgmt(struct qla_tgt_sess *sess, uint32_t lun,
2611 int fn, void *iocb, int flags)
2613 struct scsi_qla_host *vha = sess->vha;
2614 struct qla_hw_data *ha = vha->hw;
2615 struct qla_tgt_mgmt_cmd *mcmd;
2616 int res;
2617 uint8_t tmr_func;
2619 mcmd = mempool_alloc(qla_tgt_mgmt_cmd_mempool, GFP_ATOMIC);
2620 if (!mcmd) {
2621 ql_dbg(ql_dbg_tgt_tmr, vha, 0x10009,
2622 "qla_target(%d): Allocation of management "
2623 "command failed, some commands and their data could "
2624 "leak\n", vha->vp_idx);
2625 return -ENOMEM;
2627 memset(mcmd, 0, sizeof(*mcmd));
2628 mcmd->sess = sess;
2630 if (iocb) {
2631 memcpy(&mcmd->orig_iocb.imm_ntfy, iocb,
2632 sizeof(mcmd->orig_iocb.imm_ntfy));
2634 mcmd->tmr_func = fn;
2635 mcmd->flags = flags;
2637 switch (fn) {
2638 case QLA_TGT_CLEAR_ACA:
2639 ql_dbg(ql_dbg_tgt_tmr, vha, 0x10000,
2640 "qla_target(%d): CLEAR_ACA received\n", sess->vha->vp_idx);
2641 tmr_func = TMR_CLEAR_ACA;
2642 break;
2644 case QLA_TGT_TARGET_RESET:
2645 ql_dbg(ql_dbg_tgt_tmr, vha, 0x10001,
2646 "qla_target(%d): TARGET_RESET received\n",
2647 sess->vha->vp_idx);
2648 tmr_func = TMR_TARGET_WARM_RESET;
2649 break;
2651 case QLA_TGT_LUN_RESET:
2652 ql_dbg(ql_dbg_tgt_tmr, vha, 0x10002,
2653 "qla_target(%d): LUN_RESET received\n", sess->vha->vp_idx);
2654 tmr_func = TMR_LUN_RESET;
2655 break;
2657 case QLA_TGT_CLEAR_TS:
2658 ql_dbg(ql_dbg_tgt_tmr, vha, 0x10003,
2659 "qla_target(%d): CLEAR_TS received\n", sess->vha->vp_idx);
2660 tmr_func = TMR_CLEAR_TASK_SET;
2661 break;
2663 case QLA_TGT_ABORT_TS:
2664 ql_dbg(ql_dbg_tgt_tmr, vha, 0x10004,
2665 "qla_target(%d): ABORT_TS received\n", sess->vha->vp_idx);
2666 tmr_func = TMR_ABORT_TASK_SET;
2667 break;
2668 #if 0
2669 case QLA_TGT_ABORT_ALL:
2670 ql_dbg(ql_dbg_tgt_tmr, vha, 0x10005,
2671 "qla_target(%d): Doing ABORT_ALL_TASKS\n",
2672 sess->vha->vp_idx);
2673 tmr_func = 0;
2674 break;
2676 case QLA_TGT_ABORT_ALL_SESS:
2677 ql_dbg(ql_dbg_tgt_tmr, vha, 0x10006,
2678 "qla_target(%d): Doing ABORT_ALL_TASKS_SESS\n",
2679 sess->vha->vp_idx);
2680 tmr_func = 0;
2681 break;
2683 case QLA_TGT_NEXUS_LOSS_SESS:
2684 ql_dbg(ql_dbg_tgt_tmr, vha, 0x10007,
2685 "qla_target(%d): Doing NEXUS_LOSS_SESS\n",
2686 sess->vha->vp_idx);
2687 tmr_func = 0;
2688 break;
2690 case QLA_TGT_NEXUS_LOSS:
2691 ql_dbg(ql_dbg_tgt_tmr, vha, 0x10008,
2692 "qla_target(%d): Doing NEXUS_LOSS\n", sess->vha->vp_idx);
2693 tmr_func = 0;
2694 break;
2695 #endif
2696 default:
2697 ql_dbg(ql_dbg_tgt_tmr, vha, 0x1000a,
2698 "qla_target(%d): Unknown task mgmt fn 0x%x\n",
2699 sess->vha->vp_idx, fn);
2700 mempool_free(mcmd, qla_tgt_mgmt_cmd_mempool);
2701 return -ENOSYS;
2704 res = ha->tgt.tgt_ops->handle_tmr(mcmd, lun, tmr_func, 0);
2705 if (res != 0) {
2706 ql_dbg(ql_dbg_tgt_tmr, vha, 0x1000b,
2707 "qla_target(%d): tgt.tgt_ops->handle_tmr() failed: %d\n",
2708 sess->vha->vp_idx, res);
2709 mempool_free(mcmd, qla_tgt_mgmt_cmd_mempool);
2710 return -EFAULT;
2713 return 0;
2716 /* ha->hardware_lock supposed to be held on entry */
2717 static int qlt_handle_task_mgmt(struct scsi_qla_host *vha, void *iocb)
2719 struct atio_from_isp *a = (struct atio_from_isp *)iocb;
2720 struct qla_hw_data *ha = vha->hw;
2721 struct qla_tgt *tgt;
2722 struct qla_tgt_sess *sess;
2723 uint32_t lun, unpacked_lun;
2724 int lun_size, fn;
2726 tgt = ha->tgt.qla_tgt;
2728 lun = a->u.isp24.fcp_cmnd.lun;
2729 lun_size = sizeof(a->u.isp24.fcp_cmnd.lun);
2730 fn = a->u.isp24.fcp_cmnd.task_mgmt_flags;
2731 sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha,
2732 a->u.isp24.fcp_hdr.s_id);
2733 unpacked_lun = scsilun_to_int((struct scsi_lun *)&lun);
2735 if (!sess) {
2736 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf024,
2737 "qla_target(%d): task mgmt fn 0x%x for "
2738 "non-existant session\n", vha->vp_idx, fn);
2739 return qlt_sched_sess_work(tgt, QLA_TGT_SESS_WORK_TM, iocb,
2740 sizeof(struct atio_from_isp));
2743 return qlt_issue_task_mgmt(sess, unpacked_lun, fn, iocb, 0);
2746 /* ha->hardware_lock supposed to be held on entry */
2747 static int __qlt_abort_task(struct scsi_qla_host *vha,
2748 struct imm_ntfy_from_isp *iocb, struct qla_tgt_sess *sess)
2750 struct atio_from_isp *a = (struct atio_from_isp *)iocb;
2751 struct qla_hw_data *ha = vha->hw;
2752 struct qla_tgt_mgmt_cmd *mcmd;
2753 uint32_t lun, unpacked_lun;
2754 int rc;
2756 mcmd = mempool_alloc(qla_tgt_mgmt_cmd_mempool, GFP_ATOMIC);
2757 if (mcmd == NULL) {
2758 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05f,
2759 "qla_target(%d): %s: Allocation of ABORT cmd failed\n",
2760 vha->vp_idx, __func__);
2761 return -ENOMEM;
2763 memset(mcmd, 0, sizeof(*mcmd));
2765 mcmd->sess = sess;
2766 memcpy(&mcmd->orig_iocb.imm_ntfy, iocb,
2767 sizeof(mcmd->orig_iocb.imm_ntfy));
2769 lun = a->u.isp24.fcp_cmnd.lun;
2770 unpacked_lun = scsilun_to_int((struct scsi_lun *)&lun);
2772 rc = ha->tgt.tgt_ops->handle_tmr(mcmd, unpacked_lun, TMR_ABORT_TASK,
2773 le16_to_cpu(iocb->u.isp2x.seq_id));
2774 if (rc != 0) {
2775 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf060,
2776 "qla_target(%d): tgt_ops->handle_tmr() failed: %d\n",
2777 vha->vp_idx, rc);
2778 mempool_free(mcmd, qla_tgt_mgmt_cmd_mempool);
2779 return -EFAULT;
2782 return 0;
2785 /* ha->hardware_lock supposed to be held on entry */
2786 static int qlt_abort_task(struct scsi_qla_host *vha,
2787 struct imm_ntfy_from_isp *iocb)
2789 struct qla_hw_data *ha = vha->hw;
2790 struct qla_tgt_sess *sess;
2791 int loop_id;
2793 loop_id = GET_TARGET_ID(ha, (struct atio_from_isp *)iocb);
2795 sess = ha->tgt.tgt_ops->find_sess_by_loop_id(vha, loop_id);
2796 if (sess == NULL) {
2797 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf025,
2798 "qla_target(%d): task abort for unexisting "
2799 "session\n", vha->vp_idx);
2800 return qlt_sched_sess_work(ha->tgt.qla_tgt,
2801 QLA_TGT_SESS_WORK_ABORT, iocb, sizeof(*iocb));
2804 return __qlt_abort_task(vha, iocb, sess);
2808 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
2810 static int qlt_24xx_handle_els(struct scsi_qla_host *vha,
2811 struct imm_ntfy_from_isp *iocb)
2813 struct qla_hw_data *ha = vha->hw;
2814 int res = 0;
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) {
2821 case ELS_PLOGI:
2822 case ELS_FLOGI:
2823 case ELS_PRLI:
2824 case ELS_LOGO:
2825 case ELS_PRLO:
2826 res = qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS_SESS);
2827 break;
2828 case ELS_PDISC:
2829 case ELS_ADISC:
2831 struct qla_tgt *tgt = ha->tgt.qla_tgt;
2832 if (tgt->link_reinit_iocb_pending) {
2833 qlt_send_notify_ack(vha, &tgt->link_reinit_iocb,
2834 0, 0, 0, 0, 0, 0);
2835 tgt->link_reinit_iocb_pending = 0;
2837 res = 1; /* send notify ack */
2838 break;
2841 default:
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);
2846 break;
2849 return res;
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);
2868 return -1;
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");
2874 return -EINVAL;
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;
2886 sg_srr_start = sg;
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,
2890 first_offset);
2891 break;
2893 tmp += sg->length;
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);
2900 return -EINVAL;
2902 sg_srr_cnt = (cmd->sg_cnt - i);
2904 sg_srr = kzalloc(sizeof(struct scatterlist) * sg_srr_cnt, GFP_KERNEL);
2905 if (!sg_srr) {
2906 ql_dbg(ql_dbg_tgt, cmd->vha, 0xe057,
2907 "Unable to allocate sgp\n");
2908 return -ENOMEM;
2910 sg_init_table(sg_srr, sg_srr_cnt);
2911 sgp = &sg_srr[0];
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) {
2917 if (first_offset) {
2918 sg_set_page(sgp, sg_page(sg),
2919 (sg->length - first_offset), first_offset);
2920 first_offset = 0;
2921 } else {
2922 sg_set_page(sgp, sg_page(sg), sg->length, 0);
2924 bufflen += sgp->length;
2926 sgp = sg_next(sgp);
2927 if (!sgp)
2928 break;
2931 cmd->sg = sg_srr;
2932 cmd->sg_cnt = sg_srr_cnt;
2933 cmd->bufflen = bufflen;
2934 cmd->offset += offset;
2935 cmd->free_sg = 1;
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",
2939 cmd->sg_cnt);
2940 ql_dbg(ql_dbg_tgt, cmd->vha, 0xe028, "New cmd->bufflen: %u\n",
2941 cmd->bufflen);
2942 ql_dbg(ql_dbg_tgt, cmd->vha, 0xe029, "New cmd->offset: %u\n",
2943 cmd->offset);
2945 if (cmd->sg_cnt < 0)
2946 BUG();
2948 if (cmd->bufflen < 0)
2949 BUG();
2951 return 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;
2965 if (rel_offs < 0) {
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);
2969 res = -1;
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);
2975 return res;
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;
2989 uint32_t offset;
2990 uint16_t srr_ui;
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",
2996 cmd, srr_ui);
2998 switch (srr_ui) {
2999 case SRR_IU_STATUS:
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;
3005 resp = 1;
3006 break;
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);
3012 dump_stack();
3013 goto out_reject;
3015 if (se_cmd->scsi_status != 0) {
3016 ql_dbg(ql_dbg_tgt, vha, 0xe02a,
3017 "Rejecting SRR_IU_DATA_IN with non GOOD "
3018 "scsi_status\n");
3019 goto out_reject;
3021 cmd->bufflen = se_cmd->data_length;
3023 if (qlt_has_data(cmd)) {
3024 if (qlt_srr_adjust_data(cmd, offset, &xmit_type) != 0)
3025 goto out_reject;
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);
3030 resp = 1;
3031 } else {
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);
3037 goto out_reject;
3039 break;
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");
3045 dump_stack();
3046 goto out_reject;
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");
3052 goto out_reject;
3054 cmd->bufflen = se_cmd->data_length;
3056 if (qlt_has_data(cmd)) {
3057 if (qlt_srr_adjust_data(cmd, offset, &xmit_type) != 0)
3058 goto out_reject;
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);
3065 } else {
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);
3071 goto out_reject;
3073 break;
3074 default:
3075 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf067,
3076 "qla_target(%d): Unknown srr_ui value %x",
3077 vha->vp_idx, srr_ui);
3078 goto out_reject;
3081 /* Transmit response in case of status and data-in cases */
3082 if (resp)
3083 qlt_xmit_response(cmd, xmit_type, se_cmd->scsi_status);
3085 return;
3087 out_reject:
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;
3095 dump_stack();
3096 } else
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;
3107 if (!ha_locked)
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);
3115 if (!ha_locked)
3116 spin_unlock_irqrestore(&ha->hardware_lock, flags);
3118 kfree(imm);
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",
3129 tgt);
3131 restart:
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;
3138 imm = NULL;
3139 list_for_each_entry_safe(i, ti, &tgt->srr_imm_list,
3140 srr_list_entry) {
3141 if (i->srr_id == sctio->srr_id) {
3142 list_del(&i->srr_list_entry);
3143 if (imm) {
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);
3150 } else
3151 imm = i;
3155 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf02a,
3156 "IMM SRR %p, CTIO SRR %p (id %d)\n", imm, sctio,
3157 sctio->srr_id);
3159 if (imm == NULL) {
3160 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf02b,
3161 "Not found matching IMM for SRR CTIO (id %d)\n",
3162 sctio->srr_id);
3163 continue;
3164 } else
3165 list_del(&sctio->srr_list_entry);
3167 spin_unlock_irqrestore(&tgt->srr_lock, flags);
3169 cmd = sctio->cmd;
3171 * Reset qla_tgt_cmd SRR values and SGL pointer+count to follow
3172 * tcm_qla2xxx_write_pending() and tcm_qla2xxx_queue_data_in()
3173 * logic..
3175 cmd->offset = 0;
3176 if (cmd->free_sg) {
3177 kfree(cmd->sg);
3178 cmd->sg = NULL;
3179 cmd->free_sg = 0;
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 ? se_cmd->t_task_cdb[0] : 0,
3190 cmd->sg_cnt, cmd->offset);
3192 qlt_handle_srr(vha, sctio, imm);
3194 kfree(imm);
3195 kfree(sctio);
3196 goto restart;
3198 spin_unlock_irqrestore(&tgt->srr_lock, flags);
3201 /* ha->hardware_lock supposed to be held on entry */
3202 static void qlt_prepare_srr_imm(struct scsi_qla_host *vha,
3203 struct imm_ntfy_from_isp *iocb)
3205 struct qla_tgt_srr_imm *imm;
3206 struct qla_hw_data *ha = vha->hw;
3207 struct qla_tgt *tgt = ha->tgt.qla_tgt;
3208 struct qla_tgt_srr_ctio *sctio;
3210 tgt->imm_srr_id++;
3212 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf02d, "qla_target(%d): SRR received\n",
3213 vha->vp_idx);
3215 imm = kzalloc(sizeof(*imm), GFP_ATOMIC);
3216 if (imm != NULL) {
3217 memcpy(&imm->imm_ntfy, iocb, sizeof(imm->imm_ntfy));
3219 /* IRQ is already OFF */
3220 spin_lock(&tgt->srr_lock);
3221 imm->srr_id = tgt->imm_srr_id;
3222 list_add_tail(&imm->srr_list_entry,
3223 &tgt->srr_imm_list);
3224 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf02e,
3225 "IMM NTFY SRR %p added (id %d, ui %x)\n",
3226 imm, imm->srr_id, iocb->u.isp24.srr_ui);
3227 if (tgt->imm_srr_id == tgt->ctio_srr_id) {
3228 int found = 0;
3229 list_for_each_entry(sctio, &tgt->srr_ctio_list,
3230 srr_list_entry) {
3231 if (sctio->srr_id == imm->srr_id) {
3232 found = 1;
3233 break;
3236 if (found) {
3237 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf02f, "%s",
3238 "Scheduling srr work\n");
3239 schedule_work(&tgt->srr_work);
3240 } else {
3241 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf030,
3242 "qla_target(%d): imm_srr_id "
3243 "== ctio_srr_id (%d), but there is no "
3244 "corresponding SRR CTIO, deleting IMM "
3245 "SRR %p\n", vha->vp_idx, tgt->ctio_srr_id,
3246 imm);
3247 list_del(&imm->srr_list_entry);
3249 kfree(imm);
3251 spin_unlock(&tgt->srr_lock);
3252 goto out_reject;
3255 spin_unlock(&tgt->srr_lock);
3256 } else {
3257 struct qla_tgt_srr_ctio *ts;
3259 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf069,
3260 "qla_target(%d): Unable to allocate SRR IMM "
3261 "entry, SRR request will be rejected\n", vha->vp_idx);
3263 /* IRQ is already OFF */
3264 spin_lock(&tgt->srr_lock);
3265 list_for_each_entry_safe(sctio, ts, &tgt->srr_ctio_list,
3266 srr_list_entry) {
3267 if (sctio->srr_id == tgt->imm_srr_id) {
3268 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf031,
3269 "CTIO SRR %p deleted (id %d)\n",
3270 sctio, sctio->srr_id);
3271 list_del(&sctio->srr_list_entry);
3272 qlt_send_term_exchange(vha, sctio->cmd,
3273 &sctio->cmd->atio, 1);
3274 kfree(sctio);
3277 spin_unlock(&tgt->srr_lock);
3278 goto out_reject;
3281 return;
3283 out_reject:
3284 qlt_send_notify_ack(vha, iocb, 0, 0, 0,
3285 NOTIFY_ACK_SRR_FLAGS_REJECT,
3286 NOTIFY_ACK_SRR_REJECT_REASON_UNABLE_TO_PERFORM,
3287 NOTIFY_ACK_SRR_FLAGS_REJECT_EXPL_NO_EXPL);
3291 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
3293 static void qlt_handle_imm_notify(struct scsi_qla_host *vha,
3294 struct imm_ntfy_from_isp *iocb)
3296 struct qla_hw_data *ha = vha->hw;
3297 uint32_t add_flags = 0;
3298 int send_notify_ack = 1;
3299 uint16_t status;
3301 status = le16_to_cpu(iocb->u.isp2x.status);
3302 switch (status) {
3303 case IMM_NTFY_LIP_RESET:
3305 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf032,
3306 "qla_target(%d): LIP reset (loop %#x), subcode %x\n",
3307 vha->vp_idx, le16_to_cpu(iocb->u.isp24.nport_handle),
3308 iocb->u.isp24.status_subcode);
3310 if (qlt_reset(vha, iocb, QLA_TGT_ABORT_ALL) == 0)
3311 send_notify_ack = 0;
3312 break;
3315 case IMM_NTFY_LIP_LINK_REINIT:
3317 struct qla_tgt *tgt = ha->tgt.qla_tgt;
3318 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf033,
3319 "qla_target(%d): LINK REINIT (loop %#x, "
3320 "subcode %x)\n", vha->vp_idx,
3321 le16_to_cpu(iocb->u.isp24.nport_handle),
3322 iocb->u.isp24.status_subcode);
3323 if (tgt->link_reinit_iocb_pending) {
3324 qlt_send_notify_ack(vha, &tgt->link_reinit_iocb,
3325 0, 0, 0, 0, 0, 0);
3327 memcpy(&tgt->link_reinit_iocb, iocb, sizeof(*iocb));
3328 tgt->link_reinit_iocb_pending = 1;
3330 * QLogic requires to wait after LINK REINIT for possible
3331 * PDISC or ADISC ELS commands
3333 send_notify_ack = 0;
3334 break;
3337 case IMM_NTFY_PORT_LOGOUT:
3338 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf034,
3339 "qla_target(%d): Port logout (loop "
3340 "%#x, subcode %x)\n", vha->vp_idx,
3341 le16_to_cpu(iocb->u.isp24.nport_handle),
3342 iocb->u.isp24.status_subcode);
3344 if (qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS_SESS) == 0)
3345 send_notify_ack = 0;
3346 /* The sessions will be cleared in the callback, if needed */
3347 break;
3349 case IMM_NTFY_GLBL_TPRLO:
3350 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf035,
3351 "qla_target(%d): Global TPRLO (%x)\n", vha->vp_idx, status);
3352 if (qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS) == 0)
3353 send_notify_ack = 0;
3354 /* The sessions will be cleared in the callback, if needed */
3355 break;
3357 case IMM_NTFY_PORT_CONFIG:
3358 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf036,
3359 "qla_target(%d): Port config changed (%x)\n", vha->vp_idx,
3360 status);
3361 if (qlt_reset(vha, iocb, QLA_TGT_ABORT_ALL) == 0)
3362 send_notify_ack = 0;
3363 /* The sessions will be cleared in the callback, if needed */
3364 break;
3366 case IMM_NTFY_GLBL_LOGO:
3367 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06a,
3368 "qla_target(%d): Link failure detected\n",
3369 vha->vp_idx);
3370 /* I_T nexus loss */
3371 if (qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS) == 0)
3372 send_notify_ack = 0;
3373 break;
3375 case IMM_NTFY_IOCB_OVERFLOW:
3376 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06b,
3377 "qla_target(%d): Cannot provide requested "
3378 "capability (IOCB overflowed the immediate notify "
3379 "resource count)\n", vha->vp_idx);
3380 break;
3382 case IMM_NTFY_ABORT_TASK:
3383 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf037,
3384 "qla_target(%d): Abort Task (S %08x I %#x -> "
3385 "L %#x)\n", vha->vp_idx,
3386 le16_to_cpu(iocb->u.isp2x.seq_id),
3387 GET_TARGET_ID(ha, (struct atio_from_isp *)iocb),
3388 le16_to_cpu(iocb->u.isp2x.lun));
3389 if (qlt_abort_task(vha, iocb) == 0)
3390 send_notify_ack = 0;
3391 break;
3393 case IMM_NTFY_RESOURCE:
3394 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06c,
3395 "qla_target(%d): Out of resources, host %ld\n",
3396 vha->vp_idx, vha->host_no);
3397 break;
3399 case IMM_NTFY_MSG_RX:
3400 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf038,
3401 "qla_target(%d): Immediate notify task %x\n",
3402 vha->vp_idx, iocb->u.isp2x.task_flags);
3403 if (qlt_handle_task_mgmt(vha, iocb) == 0)
3404 send_notify_ack = 0;
3405 break;
3407 case IMM_NTFY_ELS:
3408 if (qlt_24xx_handle_els(vha, iocb) == 0)
3409 send_notify_ack = 0;
3410 break;
3412 case IMM_NTFY_SRR:
3413 qlt_prepare_srr_imm(vha, iocb);
3414 send_notify_ack = 0;
3415 break;
3417 default:
3418 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06d,
3419 "qla_target(%d): Received unknown immediate "
3420 "notify status %x\n", vha->vp_idx, status);
3421 break;
3424 if (send_notify_ack)
3425 qlt_send_notify_ack(vha, iocb, add_flags, 0, 0, 0, 0, 0);
3429 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
3430 * This function sends busy to ISP 2xxx or 24xx.
3432 static void qlt_send_busy(struct scsi_qla_host *vha,
3433 struct atio_from_isp *atio, uint16_t status)
3435 struct ctio7_to_24xx *ctio24;
3436 struct qla_hw_data *ha = vha->hw;
3437 request_t *pkt;
3438 struct qla_tgt_sess *sess = NULL;
3440 sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha,
3441 atio->u.isp24.fcp_hdr.s_id);
3442 if (!sess) {
3443 qlt_send_term_exchange(vha, NULL, atio, 1);
3444 return;
3446 /* Sending marker isn't necessary, since we called from ISR */
3448 pkt = (request_t *)qla2x00_alloc_iocbs(vha, NULL);
3449 if (!pkt) {
3450 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06e,
3451 "qla_target(%d): %s failed: unable to allocate "
3452 "request packet", vha->vp_idx, __func__);
3453 return;
3456 pkt->entry_count = 1;
3457 pkt->handle = QLA_TGT_SKIP_HANDLE | CTIO_COMPLETION_HANDLE_MARK;
3459 ctio24 = (struct ctio7_to_24xx *)pkt;
3460 ctio24->entry_type = CTIO_TYPE7;
3461 ctio24->nport_handle = sess->loop_id;
3462 ctio24->timeout = __constant_cpu_to_le16(QLA_TGT_TIMEOUT);
3463 ctio24->vp_index = vha->vp_idx;
3464 ctio24->initiator_id[0] = atio->u.isp24.fcp_hdr.s_id[2];
3465 ctio24->initiator_id[1] = atio->u.isp24.fcp_hdr.s_id[1];
3466 ctio24->initiator_id[2] = atio->u.isp24.fcp_hdr.s_id[0];
3467 ctio24->exchange_addr = atio->u.isp24.exchange_addr;
3468 ctio24->u.status1.flags = (atio->u.isp24.attr << 9) |
3469 __constant_cpu_to_le16(
3470 CTIO7_FLAGS_STATUS_MODE_1 | CTIO7_FLAGS_SEND_STATUS |
3471 CTIO7_FLAGS_DONT_RET_CTIO);
3473 * CTIO from fw w/o se_cmd doesn't provide enough info to retry it,
3474 * if the explicit conformation is used.
3476 ctio24->u.status1.ox_id = swab16(atio->u.isp24.fcp_hdr.ox_id);
3477 ctio24->u.status1.scsi_status = cpu_to_le16(status);
3478 ctio24->u.status1.residual = get_unaligned((uint32_t *)
3479 &atio->u.isp24.fcp_cmnd.add_cdb[
3480 atio->u.isp24.fcp_cmnd.add_cdb_len]);
3481 if (ctio24->u.status1.residual != 0)
3482 ctio24->u.status1.scsi_status |= SS_RESIDUAL_UNDER;
3484 qla2x00_start_iocbs(vha, vha->req);
3487 /* ha->hardware_lock supposed to be held on entry */
3488 /* called via callback from qla2xxx */
3489 static void qlt_24xx_atio_pkt(struct scsi_qla_host *vha,
3490 struct atio_from_isp *atio)
3492 struct qla_hw_data *ha = vha->hw;
3493 struct qla_tgt *tgt = ha->tgt.qla_tgt;
3494 int rc;
3496 if (unlikely(tgt == NULL)) {
3497 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf039,
3498 "ATIO pkt, but no tgt (ha %p)", ha);
3499 return;
3501 ql_dbg(ql_dbg_tgt, vha, 0xe02c,
3502 "qla_target(%d): ATIO pkt %p: type %02x count %02x",
3503 vha->vp_idx, atio, atio->u.raw.entry_type,
3504 atio->u.raw.entry_count);
3506 * In tgt_stop mode we also should allow all requests to pass.
3507 * Otherwise, some commands can stuck.
3510 tgt->irq_cmd_count++;
3512 switch (atio->u.raw.entry_type) {
3513 case ATIO_TYPE7:
3514 ql_dbg(ql_dbg_tgt, vha, 0xe02d,
3515 "ATIO_TYPE7 instance %d, lun %Lx, read/write %d/%d, "
3516 "add_cdb_len %d, data_length %04x, s_id %x:%x:%x\n",
3517 vha->vp_idx, atio->u.isp24.fcp_cmnd.lun,
3518 atio->u.isp24.fcp_cmnd.rddata,
3519 atio->u.isp24.fcp_cmnd.wrdata,
3520 atio->u.isp24.fcp_cmnd.add_cdb_len,
3521 be32_to_cpu(get_unaligned((uint32_t *)
3522 &atio->u.isp24.fcp_cmnd.add_cdb[
3523 atio->u.isp24.fcp_cmnd.add_cdb_len])),
3524 atio->u.isp24.fcp_hdr.s_id[0],
3525 atio->u.isp24.fcp_hdr.s_id[1],
3526 atio->u.isp24.fcp_hdr.s_id[2]);
3528 if (unlikely(atio->u.isp24.exchange_addr ==
3529 ATIO_EXCHANGE_ADDRESS_UNKNOWN)) {
3530 ql_dbg(ql_dbg_tgt, vha, 0xe058,
3531 "qla_target(%d): ATIO_TYPE7 "
3532 "received with UNKNOWN exchange address, "
3533 "sending QUEUE_FULL\n", vha->vp_idx);
3534 qlt_send_busy(vha, atio, SAM_STAT_TASK_SET_FULL);
3535 break;
3537 if (likely(atio->u.isp24.fcp_cmnd.task_mgmt_flags == 0))
3538 rc = qlt_handle_cmd_for_atio(vha, atio);
3539 else
3540 rc = qlt_handle_task_mgmt(vha, atio);
3541 if (unlikely(rc != 0)) {
3542 if (rc == -ESRCH) {
3543 #if 1 /* With TERM EXCHANGE some FC cards refuse to boot */
3544 qlt_send_busy(vha, atio, SAM_STAT_BUSY);
3545 #else
3546 qlt_send_term_exchange(vha, NULL, atio, 1);
3547 #endif
3548 } else {
3549 if (tgt->tgt_stop) {
3550 ql_dbg(ql_dbg_tgt, vha, 0xe059,
3551 "qla_target: Unable to send "
3552 "command to target for req, "
3553 "ignoring.\n");
3554 } else {
3555 ql_dbg(ql_dbg_tgt, vha, 0xe05a,
3556 "qla_target(%d): Unable to send "
3557 "command to target, sending BUSY "
3558 "status.\n", vha->vp_idx);
3559 qlt_send_busy(vha, atio, SAM_STAT_BUSY);
3563 break;
3565 case IMMED_NOTIFY_TYPE:
3567 if (unlikely(atio->u.isp2x.entry_status != 0)) {
3568 ql_dbg(ql_dbg_tgt, vha, 0xe05b,
3569 "qla_target(%d): Received ATIO packet %x "
3570 "with error status %x\n", vha->vp_idx,
3571 atio->u.raw.entry_type,
3572 atio->u.isp2x.entry_status);
3573 break;
3575 ql_dbg(ql_dbg_tgt, vha, 0xe02e, "%s", "IMMED_NOTIFY ATIO");
3576 qlt_handle_imm_notify(vha, (struct imm_ntfy_from_isp *)atio);
3577 break;
3580 default:
3581 ql_dbg(ql_dbg_tgt, vha, 0xe05c,
3582 "qla_target(%d): Received unknown ATIO atio "
3583 "type %x\n", vha->vp_idx, atio->u.raw.entry_type);
3584 break;
3587 tgt->irq_cmd_count--;
3590 /* ha->hardware_lock supposed to be held on entry */
3591 /* called via callback from qla2xxx */
3592 static void qlt_response_pkt(struct scsi_qla_host *vha, response_t *pkt)
3594 struct qla_hw_data *ha = vha->hw;
3595 struct qla_tgt *tgt = ha->tgt.qla_tgt;
3597 if (unlikely(tgt == NULL)) {
3598 ql_dbg(ql_dbg_tgt, vha, 0xe05d,
3599 "qla_target(%d): Response pkt %x received, but no "
3600 "tgt (ha %p)\n", vha->vp_idx, pkt->entry_type, ha);
3601 return;
3604 ql_dbg(ql_dbg_tgt, vha, 0xe02f,
3605 "qla_target(%d): response pkt %p: T %02x C %02x S %02x "
3606 "handle %#x\n", vha->vp_idx, pkt, pkt->entry_type,
3607 pkt->entry_count, pkt->entry_status, pkt->handle);
3610 * In tgt_stop mode we also should allow all requests to pass.
3611 * Otherwise, some commands can stuck.
3614 tgt->irq_cmd_count++;
3616 switch (pkt->entry_type) {
3617 case CTIO_TYPE7:
3619 struct ctio7_from_24xx *entry = (struct ctio7_from_24xx *)pkt;
3620 ql_dbg(ql_dbg_tgt, vha, 0xe030, "CTIO_TYPE7: instance %d\n",
3621 vha->vp_idx);
3622 qlt_do_ctio_completion(vha, entry->handle,
3623 le16_to_cpu(entry->status)|(pkt->entry_status << 16),
3624 entry);
3625 break;
3628 case ACCEPT_TGT_IO_TYPE:
3630 struct atio_from_isp *atio = (struct atio_from_isp *)pkt;
3631 int rc;
3632 ql_dbg(ql_dbg_tgt, vha, 0xe031,
3633 "ACCEPT_TGT_IO instance %d status %04x "
3634 "lun %04x read/write %d data_length %04x "
3635 "target_id %02x rx_id %04x\n ", vha->vp_idx,
3636 le16_to_cpu(atio->u.isp2x.status),
3637 le16_to_cpu(atio->u.isp2x.lun),
3638 atio->u.isp2x.execution_codes,
3639 le32_to_cpu(atio->u.isp2x.data_length), GET_TARGET_ID(ha,
3640 atio), atio->u.isp2x.rx_id);
3641 if (atio->u.isp2x.status !=
3642 __constant_cpu_to_le16(ATIO_CDB_VALID)) {
3643 ql_dbg(ql_dbg_tgt, vha, 0xe05e,
3644 "qla_target(%d): ATIO with error "
3645 "status %x received\n", vha->vp_idx,
3646 le16_to_cpu(atio->u.isp2x.status));
3647 break;
3649 ql_dbg(ql_dbg_tgt, vha, 0xe032,
3650 "FCP CDB: 0x%02x, sizeof(cdb): %lu",
3651 atio->u.isp2x.cdb[0], (unsigned long
3652 int)sizeof(atio->u.isp2x.cdb));
3654 rc = qlt_handle_cmd_for_atio(vha, atio);
3655 if (unlikely(rc != 0)) {
3656 if (rc == -ESRCH) {
3657 #if 1 /* With TERM EXCHANGE some FC cards refuse to boot */
3658 qlt_send_busy(vha, atio, 0);
3659 #else
3660 qlt_send_term_exchange(vha, NULL, atio, 1);
3661 #endif
3662 } else {
3663 if (tgt->tgt_stop) {
3664 ql_dbg(ql_dbg_tgt, vha, 0xe05f,
3665 "qla_target: Unable to send "
3666 "command to target, sending TERM "
3667 "EXCHANGE for rsp\n");
3668 qlt_send_term_exchange(vha, NULL,
3669 atio, 1);
3670 } else {
3671 ql_dbg(ql_dbg_tgt, vha, 0xe060,
3672 "qla_target(%d): Unable to send "
3673 "command to target, sending BUSY "
3674 "status\n", vha->vp_idx);
3675 qlt_send_busy(vha, atio, 0);
3680 break;
3682 case CONTINUE_TGT_IO_TYPE:
3684 struct ctio_to_2xxx *entry = (struct ctio_to_2xxx *)pkt;
3685 ql_dbg(ql_dbg_tgt, vha, 0xe033,
3686 "CONTINUE_TGT_IO: instance %d\n", vha->vp_idx);
3687 qlt_do_ctio_completion(vha, entry->handle,
3688 le16_to_cpu(entry->status)|(pkt->entry_status << 16),
3689 entry);
3690 break;
3693 case CTIO_A64_TYPE:
3695 struct ctio_to_2xxx *entry = (struct ctio_to_2xxx *)pkt;
3696 ql_dbg(ql_dbg_tgt, vha, 0xe034, "CTIO_A64: instance %d\n",
3697 vha->vp_idx);
3698 qlt_do_ctio_completion(vha, entry->handle,
3699 le16_to_cpu(entry->status)|(pkt->entry_status << 16),
3700 entry);
3701 break;
3704 case IMMED_NOTIFY_TYPE:
3705 ql_dbg(ql_dbg_tgt, vha, 0xe035, "%s", "IMMED_NOTIFY\n");
3706 qlt_handle_imm_notify(vha, (struct imm_ntfy_from_isp *)pkt);
3707 break;
3709 case NOTIFY_ACK_TYPE:
3710 if (tgt->notify_ack_expected > 0) {
3711 struct nack_to_isp *entry = (struct nack_to_isp *)pkt;
3712 ql_dbg(ql_dbg_tgt, vha, 0xe036,
3713 "NOTIFY_ACK seq %08x status %x\n",
3714 le16_to_cpu(entry->u.isp2x.seq_id),
3715 le16_to_cpu(entry->u.isp2x.status));
3716 tgt->notify_ack_expected--;
3717 if (entry->u.isp2x.status !=
3718 __constant_cpu_to_le16(NOTIFY_ACK_SUCCESS)) {
3719 ql_dbg(ql_dbg_tgt, vha, 0xe061,
3720 "qla_target(%d): NOTIFY_ACK "
3721 "failed %x\n", vha->vp_idx,
3722 le16_to_cpu(entry->u.isp2x.status));
3724 } else {
3725 ql_dbg(ql_dbg_tgt, vha, 0xe062,
3726 "qla_target(%d): Unexpected NOTIFY_ACK received\n",
3727 vha->vp_idx);
3729 break;
3731 case ABTS_RECV_24XX:
3732 ql_dbg(ql_dbg_tgt, vha, 0xe037,
3733 "ABTS_RECV_24XX: instance %d\n", vha->vp_idx);
3734 qlt_24xx_handle_abts(vha, (struct abts_recv_from_24xx *)pkt);
3735 break;
3737 case ABTS_RESP_24XX:
3738 if (tgt->abts_resp_expected > 0) {
3739 struct abts_resp_from_24xx_fw *entry =
3740 (struct abts_resp_from_24xx_fw *)pkt;
3741 ql_dbg(ql_dbg_tgt, vha, 0xe038,
3742 "ABTS_RESP_24XX: compl_status %x\n",
3743 entry->compl_status);
3744 tgt->abts_resp_expected--;
3745 if (le16_to_cpu(entry->compl_status) !=
3746 ABTS_RESP_COMPL_SUCCESS) {
3747 if ((entry->error_subcode1 == 0x1E) &&
3748 (entry->error_subcode2 == 0)) {
3750 * We've got a race here: aborted
3751 * exchange not terminated, i.e.
3752 * response for the aborted command was
3753 * sent between the abort request was
3754 * received and processed.
3755 * Unfortunately, the firmware has a
3756 * silly requirement that all aborted
3757 * exchanges must be explicitely
3758 * terminated, otherwise it refuses to
3759 * send responses for the abort
3760 * requests. So, we have to
3761 * (re)terminate the exchange and retry
3762 * the abort response.
3764 qlt_24xx_retry_term_exchange(vha,
3765 entry);
3766 } else
3767 ql_dbg(ql_dbg_tgt, vha, 0xe063,
3768 "qla_target(%d): ABTS_RESP_24XX "
3769 "failed %x (subcode %x:%x)",
3770 vha->vp_idx, entry->compl_status,
3771 entry->error_subcode1,
3772 entry->error_subcode2);
3774 } else {
3775 ql_dbg(ql_dbg_tgt, vha, 0xe064,
3776 "qla_target(%d): Unexpected ABTS_RESP_24XX "
3777 "received\n", vha->vp_idx);
3779 break;
3781 default:
3782 ql_dbg(ql_dbg_tgt, vha, 0xe065,
3783 "qla_target(%d): Received unknown response pkt "
3784 "type %x\n", vha->vp_idx, pkt->entry_type);
3785 break;
3788 tgt->irq_cmd_count--;
3792 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
3794 void qlt_async_event(uint16_t code, struct scsi_qla_host *vha,
3795 uint16_t *mailbox)
3797 struct qla_hw_data *ha = vha->hw;
3798 struct qla_tgt *tgt = ha->tgt.qla_tgt;
3799 int login_code;
3801 ql_dbg(ql_dbg_tgt, vha, 0xe039,
3802 "scsi(%ld): ha state %d init_done %d oper_mode %d topo %d\n",
3803 vha->host_no, atomic_read(&vha->loop_state), vha->flags.init_done,
3804 ha->operating_mode, ha->current_topology);
3806 if (!ha->tgt.tgt_ops)
3807 return;
3809 if (unlikely(tgt == NULL)) {
3810 ql_dbg(ql_dbg_tgt, vha, 0xe03a,
3811 "ASYNC EVENT %#x, but no tgt (ha %p)\n", code, ha);
3812 return;
3815 if (((code == MBA_POINT_TO_POINT) || (code == MBA_CHG_IN_CONNECTION)) &&
3816 IS_QLA2100(ha))
3817 return;
3819 * In tgt_stop mode we also should allow all requests to pass.
3820 * Otherwise, some commands can stuck.
3823 tgt->irq_cmd_count++;
3825 switch (code) {
3826 case MBA_RESET: /* Reset */
3827 case MBA_SYSTEM_ERR: /* System Error */
3828 case MBA_REQ_TRANSFER_ERR: /* Request Transfer Error */
3829 case MBA_RSP_TRANSFER_ERR: /* Response Transfer Error */
3830 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03a,
3831 "qla_target(%d): System error async event %#x "
3832 "occurred", vha->vp_idx, code);
3833 break;
3834 case MBA_WAKEUP_THRES: /* Request Queue Wake-up. */
3835 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
3836 break;
3838 case MBA_LOOP_UP:
3840 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03b,
3841 "qla_target(%d): Async LOOP_UP occurred "
3842 "(m[0]=%x, m[1]=%x, m[2]=%x, m[3]=%x)", vha->vp_idx,
3843 le16_to_cpu(mailbox[0]), le16_to_cpu(mailbox[1]),
3844 le16_to_cpu(mailbox[2]), le16_to_cpu(mailbox[3]));
3845 if (tgt->link_reinit_iocb_pending) {
3846 qlt_send_notify_ack(vha, (void *)&tgt->link_reinit_iocb,
3847 0, 0, 0, 0, 0, 0);
3848 tgt->link_reinit_iocb_pending = 0;
3850 break;
3853 case MBA_LIP_OCCURRED:
3854 case MBA_LOOP_DOWN:
3855 case MBA_LIP_RESET:
3856 case MBA_RSCN_UPDATE:
3857 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03c,
3858 "qla_target(%d): Async event %#x occurred "
3859 "(m[0]=%x, m[1]=%x, m[2]=%x, m[3]=%x)", vha->vp_idx, code,
3860 le16_to_cpu(mailbox[0]), le16_to_cpu(mailbox[1]),
3861 le16_to_cpu(mailbox[2]), le16_to_cpu(mailbox[3]));
3862 break;
3864 case MBA_PORT_UPDATE:
3865 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03d,
3866 "qla_target(%d): Port update async event %#x "
3867 "occurred: updating the ports database (m[0]=%x, m[1]=%x, "
3868 "m[2]=%x, m[3]=%x)", vha->vp_idx, code,
3869 le16_to_cpu(mailbox[0]), le16_to_cpu(mailbox[1]),
3870 le16_to_cpu(mailbox[2]), le16_to_cpu(mailbox[3]));
3872 login_code = le16_to_cpu(mailbox[2]);
3873 if (login_code == 0x4)
3874 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03e,
3875 "Async MB 2: Got PLOGI Complete\n");
3876 else if (login_code == 0x7)
3877 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03f,
3878 "Async MB 2: Port Logged Out\n");
3879 break;
3881 default:
3882 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf040,
3883 "qla_target(%d): Async event %#x occurred: "
3884 "ignore (m[0]=%x, m[1]=%x, m[2]=%x, m[3]=%x)", vha->vp_idx,
3885 code, le16_to_cpu(mailbox[0]), le16_to_cpu(mailbox[1]),
3886 le16_to_cpu(mailbox[2]), le16_to_cpu(mailbox[3]));
3887 break;
3890 tgt->irq_cmd_count--;
3893 static fc_port_t *qlt_get_port_database(struct scsi_qla_host *vha,
3894 uint16_t loop_id)
3896 fc_port_t *fcport;
3897 int rc;
3899 fcport = kzalloc(sizeof(*fcport), GFP_KERNEL);
3900 if (!fcport) {
3901 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06f,
3902 "qla_target(%d): Allocation of tmp FC port failed",
3903 vha->vp_idx);
3904 return NULL;
3907 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf041, "loop_id %d", loop_id);
3909 fcport->loop_id = loop_id;
3911 rc = qla2x00_get_port_database(vha, fcport, 0);
3912 if (rc != QLA_SUCCESS) {
3913 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf070,
3914 "qla_target(%d): Failed to retrieve fcport "
3915 "information -- get_port_database() returned %x "
3916 "(loop_id=0x%04x)", vha->vp_idx, rc, loop_id);
3917 kfree(fcport);
3918 return NULL;
3921 return fcport;
3924 /* Must be called under tgt_mutex */
3925 static struct qla_tgt_sess *qlt_make_local_sess(struct scsi_qla_host *vha,
3926 uint8_t *s_id)
3928 struct qla_hw_data *ha = vha->hw;
3929 struct qla_tgt_sess *sess = NULL;
3930 fc_port_t *fcport = NULL;
3931 int rc, global_resets;
3932 uint16_t loop_id = 0;
3934 retry:
3935 global_resets = atomic_read(&ha->tgt.qla_tgt->tgt_global_resets_count);
3937 rc = qla24xx_get_loop_id(vha, s_id, &loop_id);
3938 if (rc != 0) {
3939 if ((s_id[0] == 0xFF) &&
3940 (s_id[1] == 0xFC)) {
3942 * This is Domain Controller, so it should be
3943 * OK to drop SCSI commands from it.
3945 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf042,
3946 "Unable to find initiator with S_ID %x:%x:%x",
3947 s_id[0], s_id[1], s_id[2]);
3948 } else
3949 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf071,
3950 "qla_target(%d): Unable to find "
3951 "initiator with S_ID %x:%x:%x",
3952 vha->vp_idx, s_id[0], s_id[1],
3953 s_id[2]);
3954 return NULL;
3957 fcport = qlt_get_port_database(vha, loop_id);
3958 if (!fcport)
3959 return NULL;
3961 if (global_resets !=
3962 atomic_read(&ha->tgt.qla_tgt->tgt_global_resets_count)) {
3963 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf043,
3964 "qla_target(%d): global reset during session discovery "
3965 "(counter was %d, new %d), retrying", vha->vp_idx,
3966 global_resets,
3967 atomic_read(&ha->tgt.qla_tgt->tgt_global_resets_count));
3968 goto retry;
3971 sess = qlt_create_sess(vha, fcport, true);
3973 kfree(fcport);
3974 return sess;
3977 static void qlt_abort_work(struct qla_tgt *tgt,
3978 struct qla_tgt_sess_work_param *prm)
3980 struct scsi_qla_host *vha = tgt->vha;
3981 struct qla_hw_data *ha = vha->hw;
3982 struct qla_tgt_sess *sess = NULL;
3983 unsigned long flags;
3984 uint32_t be_s_id;
3985 uint8_t s_id[3];
3986 int rc;
3988 spin_lock_irqsave(&ha->hardware_lock, flags);
3990 if (tgt->tgt_stop)
3991 goto out_term;
3993 s_id[0] = prm->abts.fcp_hdr_le.s_id[2];
3994 s_id[1] = prm->abts.fcp_hdr_le.s_id[1];
3995 s_id[2] = prm->abts.fcp_hdr_le.s_id[0];
3997 sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha,
3998 (unsigned char *)&be_s_id);
3999 if (!sess) {
4000 spin_unlock_irqrestore(&ha->hardware_lock, flags);
4002 mutex_lock(&ha->tgt.tgt_mutex);
4003 sess = qlt_make_local_sess(vha, s_id);
4004 /* sess has got an extra creation ref */
4005 mutex_unlock(&ha->tgt.tgt_mutex);
4007 spin_lock_irqsave(&ha->hardware_lock, flags);
4008 if (!sess)
4009 goto out_term;
4010 } else {
4011 kref_get(&sess->se_sess->sess_kref);
4014 if (tgt->tgt_stop)
4015 goto out_term;
4017 rc = __qlt_24xx_handle_abts(vha, &prm->abts, sess);
4018 if (rc != 0)
4019 goto out_term;
4021 ha->tgt.tgt_ops->put_sess(sess);
4022 spin_unlock_irqrestore(&ha->hardware_lock, flags);
4023 return;
4025 out_term:
4026 qlt_24xx_send_abts_resp(vha, &prm->abts, FCP_TMF_REJECTED, false);
4027 if (sess)
4028 ha->tgt.tgt_ops->put_sess(sess);
4029 spin_unlock_irqrestore(&ha->hardware_lock, flags);
4032 static void qlt_tmr_work(struct qla_tgt *tgt,
4033 struct qla_tgt_sess_work_param *prm)
4035 struct atio_from_isp *a = &prm->tm_iocb2;
4036 struct scsi_qla_host *vha = tgt->vha;
4037 struct qla_hw_data *ha = vha->hw;
4038 struct qla_tgt_sess *sess = NULL;
4039 unsigned long flags;
4040 uint8_t *s_id = NULL; /* to hide compiler warnings */
4041 int rc;
4042 uint32_t lun, unpacked_lun;
4043 int lun_size, fn;
4044 void *iocb;
4046 spin_lock_irqsave(&ha->hardware_lock, flags);
4048 if (tgt->tgt_stop)
4049 goto out_term;
4051 s_id = prm->tm_iocb2.u.isp24.fcp_hdr.s_id;
4052 sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha, s_id);
4053 if (!sess) {
4054 spin_unlock_irqrestore(&ha->hardware_lock, flags);
4056 mutex_lock(&ha->tgt.tgt_mutex);
4057 sess = qlt_make_local_sess(vha, s_id);
4058 /* sess has got an extra creation ref */
4059 mutex_unlock(&ha->tgt.tgt_mutex);
4061 spin_lock_irqsave(&ha->hardware_lock, flags);
4062 if (!sess)
4063 goto out_term;
4064 } else {
4065 kref_get(&sess->se_sess->sess_kref);
4068 iocb = a;
4069 lun = a->u.isp24.fcp_cmnd.lun;
4070 lun_size = sizeof(lun);
4071 fn = a->u.isp24.fcp_cmnd.task_mgmt_flags;
4072 unpacked_lun = scsilun_to_int((struct scsi_lun *)&lun);
4074 rc = qlt_issue_task_mgmt(sess, unpacked_lun, fn, iocb, 0);
4075 if (rc != 0)
4076 goto out_term;
4078 ha->tgt.tgt_ops->put_sess(sess);
4079 spin_unlock_irqrestore(&ha->hardware_lock, flags);
4080 return;
4082 out_term:
4083 qlt_send_term_exchange(vha, NULL, &prm->tm_iocb2, 1);
4084 if (sess)
4085 ha->tgt.tgt_ops->put_sess(sess);
4086 spin_unlock_irqrestore(&ha->hardware_lock, flags);
4089 static void qlt_sess_work_fn(struct work_struct *work)
4091 struct qla_tgt *tgt = container_of(work, struct qla_tgt, sess_work);
4092 struct scsi_qla_host *vha = tgt->vha;
4093 unsigned long flags;
4095 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf000, "Sess work (tgt %p)", tgt);
4097 spin_lock_irqsave(&tgt->sess_work_lock, flags);
4098 while (!list_empty(&tgt->sess_works_list)) {
4099 struct qla_tgt_sess_work_param *prm = list_entry(
4100 tgt->sess_works_list.next, typeof(*prm),
4101 sess_works_list_entry);
4104 * This work can be scheduled on several CPUs at time, so we
4105 * must delete the entry to eliminate double processing
4107 list_del(&prm->sess_works_list_entry);
4109 spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
4111 switch (prm->type) {
4112 case QLA_TGT_SESS_WORK_ABORT:
4113 qlt_abort_work(tgt, prm);
4114 break;
4115 case QLA_TGT_SESS_WORK_TM:
4116 qlt_tmr_work(tgt, prm);
4117 break;
4118 default:
4119 BUG_ON(1);
4120 break;
4123 spin_lock_irqsave(&tgt->sess_work_lock, flags);
4125 kfree(prm);
4127 spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
4130 /* Must be called under tgt_host_action_mutex */
4131 int qlt_add_target(struct qla_hw_data *ha, struct scsi_qla_host *base_vha)
4133 struct qla_tgt *tgt;
4135 if (!QLA_TGT_MODE_ENABLED())
4136 return 0;
4138 if (!IS_TGT_MODE_CAPABLE(ha)) {
4139 ql_log(ql_log_warn, base_vha, 0xe070,
4140 "This adapter does not support target mode.\n");
4141 return 0;
4144 ql_dbg(ql_dbg_tgt, base_vha, 0xe03b,
4145 "Registering target for host %ld(%p)", base_vha->host_no, ha);
4147 BUG_ON((ha->tgt.qla_tgt != NULL) || (ha->tgt.tgt_ops != NULL));
4149 tgt = kzalloc(sizeof(struct qla_tgt), GFP_KERNEL);
4150 if (!tgt) {
4151 ql_dbg(ql_dbg_tgt, base_vha, 0xe066,
4152 "Unable to allocate struct qla_tgt\n");
4153 return -ENOMEM;
4156 if (!(base_vha->host->hostt->supported_mode & MODE_TARGET))
4157 base_vha->host->hostt->supported_mode |= MODE_TARGET;
4159 tgt->ha = ha;
4160 tgt->vha = base_vha;
4161 init_waitqueue_head(&tgt->waitQ);
4162 INIT_LIST_HEAD(&tgt->sess_list);
4163 INIT_LIST_HEAD(&tgt->del_sess_list);
4164 INIT_DELAYED_WORK(&tgt->sess_del_work,
4165 (void (*)(struct work_struct *))qlt_del_sess_work_fn);
4166 spin_lock_init(&tgt->sess_work_lock);
4167 INIT_WORK(&tgt->sess_work, qlt_sess_work_fn);
4168 INIT_LIST_HEAD(&tgt->sess_works_list);
4169 spin_lock_init(&tgt->srr_lock);
4170 INIT_LIST_HEAD(&tgt->srr_ctio_list);
4171 INIT_LIST_HEAD(&tgt->srr_imm_list);
4172 INIT_WORK(&tgt->srr_work, qlt_handle_srr_work);
4173 atomic_set(&tgt->tgt_global_resets_count, 0);
4175 ha->tgt.qla_tgt = tgt;
4177 ql_dbg(ql_dbg_tgt, base_vha, 0xe067,
4178 "qla_target(%d): using 64 Bit PCI addressing",
4179 base_vha->vp_idx);
4180 tgt->tgt_enable_64bit_addr = 1;
4181 /* 3 is reserved */
4182 tgt->sg_tablesize = QLA_TGT_MAX_SG_24XX(base_vha->req->length - 3);
4183 tgt->datasegs_per_cmd = QLA_TGT_DATASEGS_PER_CMD_24XX;
4184 tgt->datasegs_per_cont = QLA_TGT_DATASEGS_PER_CONT_24XX;
4186 mutex_lock(&qla_tgt_mutex);
4187 list_add_tail(&tgt->tgt_list_entry, &qla_tgt_glist);
4188 mutex_unlock(&qla_tgt_mutex);
4190 return 0;
4193 /* Must be called under tgt_host_action_mutex */
4194 int qlt_remove_target(struct qla_hw_data *ha, struct scsi_qla_host *vha)
4196 if (!ha->tgt.qla_tgt)
4197 return 0;
4199 mutex_lock(&qla_tgt_mutex);
4200 list_del(&ha->tgt.qla_tgt->tgt_list_entry);
4201 mutex_unlock(&qla_tgt_mutex);
4203 ql_dbg(ql_dbg_tgt, vha, 0xe03c, "Unregistering target for host %ld(%p)",
4204 vha->host_no, ha);
4205 qlt_release(ha->tgt.qla_tgt);
4207 return 0;
4210 static void qlt_lport_dump(struct scsi_qla_host *vha, u64 wwpn,
4211 unsigned char *b)
4213 int i;
4215 pr_debug("qla2xxx HW vha->node_name: ");
4216 for (i = 0; i < WWN_SIZE; i++)
4217 pr_debug("%02x ", vha->node_name[i]);
4218 pr_debug("\n");
4219 pr_debug("qla2xxx HW vha->port_name: ");
4220 for (i = 0; i < WWN_SIZE; i++)
4221 pr_debug("%02x ", vha->port_name[i]);
4222 pr_debug("\n");
4224 pr_debug("qla2xxx passed configfs WWPN: ");
4225 put_unaligned_be64(wwpn, b);
4226 for (i = 0; i < WWN_SIZE; i++)
4227 pr_debug("%02x ", b[i]);
4228 pr_debug("\n");
4232 * qla_tgt_lport_register - register lport with external module
4234 * @qla_tgt_ops: Pointer for tcm_qla2xxx qla_tgt_ops
4235 * @wwpn: Passwd FC target WWPN
4236 * @callback: lport initialization callback for tcm_qla2xxx code
4237 * @target_lport_ptr: pointer for tcm_qla2xxx specific lport data
4239 int qlt_lport_register(struct qla_tgt_func_tmpl *qla_tgt_ops, u64 wwpn,
4240 int (*callback)(struct scsi_qla_host *), void *target_lport_ptr)
4242 struct qla_tgt *tgt;
4243 struct scsi_qla_host *vha;
4244 struct qla_hw_data *ha;
4245 struct Scsi_Host *host;
4246 unsigned long flags;
4247 int rc;
4248 u8 b[WWN_SIZE];
4250 mutex_lock(&qla_tgt_mutex);
4251 list_for_each_entry(tgt, &qla_tgt_glist, tgt_list_entry) {
4252 vha = tgt->vha;
4253 ha = vha->hw;
4255 host = vha->host;
4256 if (!host)
4257 continue;
4259 if (ha->tgt.tgt_ops != NULL)
4260 continue;
4262 if (!(host->hostt->supported_mode & MODE_TARGET))
4263 continue;
4265 spin_lock_irqsave(&ha->hardware_lock, flags);
4266 if (host->active_mode & MODE_TARGET) {
4267 pr_debug("MODE_TARGET already active on qla2xxx(%d)\n",
4268 host->host_no);
4269 spin_unlock_irqrestore(&ha->hardware_lock, flags);
4270 continue;
4272 spin_unlock_irqrestore(&ha->hardware_lock, flags);
4274 if (!scsi_host_get(host)) {
4275 ql_dbg(ql_dbg_tgt, vha, 0xe068,
4276 "Unable to scsi_host_get() for"
4277 " qla2xxx scsi_host\n");
4278 continue;
4280 qlt_lport_dump(vha, wwpn, b);
4282 if (memcmp(vha->port_name, b, WWN_SIZE)) {
4283 scsi_host_put(host);
4284 continue;
4287 * Setup passed parameters ahead of invoking callback
4289 ha->tgt.tgt_ops = qla_tgt_ops;
4290 ha->tgt.target_lport_ptr = target_lport_ptr;
4291 rc = (*callback)(vha);
4292 if (rc != 0) {
4293 ha->tgt.tgt_ops = NULL;
4294 ha->tgt.target_lport_ptr = NULL;
4296 mutex_unlock(&qla_tgt_mutex);
4297 return rc;
4299 mutex_unlock(&qla_tgt_mutex);
4301 return -ENODEV;
4303 EXPORT_SYMBOL(qlt_lport_register);
4306 * qla_tgt_lport_deregister - Degister lport
4308 * @vha: Registered scsi_qla_host pointer
4310 void qlt_lport_deregister(struct scsi_qla_host *vha)
4312 struct qla_hw_data *ha = vha->hw;
4313 struct Scsi_Host *sh = vha->host;
4315 * Clear the target_lport_ptr qla_target_template pointer in qla_hw_data
4317 ha->tgt.target_lport_ptr = NULL;
4318 ha->tgt.tgt_ops = NULL;
4320 * Release the Scsi_Host reference for the underlying qla2xxx host
4322 scsi_host_put(sh);
4324 EXPORT_SYMBOL(qlt_lport_deregister);
4326 /* Must be called under HW lock */
4327 void qlt_set_mode(struct scsi_qla_host *vha)
4329 struct qla_hw_data *ha = vha->hw;
4331 switch (ql2x_ini_mode) {
4332 case QLA2XXX_INI_MODE_DISABLED:
4333 case QLA2XXX_INI_MODE_EXCLUSIVE:
4334 vha->host->active_mode = MODE_TARGET;
4335 break;
4336 case QLA2XXX_INI_MODE_ENABLED:
4337 vha->host->active_mode |= MODE_TARGET;
4338 break;
4339 default:
4340 break;
4343 if (ha->tgt.ini_mode_force_reverse)
4344 qla_reverse_ini_mode(vha);
4347 /* Must be called under HW lock */
4348 void qlt_clear_mode(struct scsi_qla_host *vha)
4350 struct qla_hw_data *ha = vha->hw;
4352 switch (ql2x_ini_mode) {
4353 case QLA2XXX_INI_MODE_DISABLED:
4354 vha->host->active_mode = MODE_UNKNOWN;
4355 break;
4356 case QLA2XXX_INI_MODE_EXCLUSIVE:
4357 vha->host->active_mode = MODE_INITIATOR;
4358 break;
4359 case QLA2XXX_INI_MODE_ENABLED:
4360 vha->host->active_mode &= ~MODE_TARGET;
4361 break;
4362 default:
4363 break;
4366 if (ha->tgt.ini_mode_force_reverse)
4367 qla_reverse_ini_mode(vha);
4371 * qla_tgt_enable_vha - NO LOCK HELD
4373 * host_reset, bring up w/ Target Mode Enabled
4375 void
4376 qlt_enable_vha(struct scsi_qla_host *vha)
4378 struct qla_hw_data *ha = vha->hw;
4379 struct qla_tgt *tgt = ha->tgt.qla_tgt;
4380 unsigned long flags;
4382 if (!tgt) {
4383 ql_dbg(ql_dbg_tgt, vha, 0xe069,
4384 "Unable to locate qla_tgt pointer from"
4385 " struct qla_hw_data\n");
4386 dump_stack();
4387 return;
4390 spin_lock_irqsave(&ha->hardware_lock, flags);
4391 tgt->tgt_stopped = 0;
4392 qlt_set_mode(vha);
4393 spin_unlock_irqrestore(&ha->hardware_lock, flags);
4395 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
4396 qla2xxx_wake_dpc(vha);
4397 qla2x00_wait_for_hba_online(vha);
4399 EXPORT_SYMBOL(qlt_enable_vha);
4402 * qla_tgt_disable_vha - NO LOCK HELD
4404 * Disable Target Mode and reset the adapter
4406 void
4407 qlt_disable_vha(struct scsi_qla_host *vha)
4409 struct qla_hw_data *ha = vha->hw;
4410 struct qla_tgt *tgt = ha->tgt.qla_tgt;
4411 unsigned long flags;
4413 if (!tgt) {
4414 ql_dbg(ql_dbg_tgt, vha, 0xe06a,
4415 "Unable to locate qla_tgt pointer from"
4416 " struct qla_hw_data\n");
4417 dump_stack();
4418 return;
4421 spin_lock_irqsave(&ha->hardware_lock, flags);
4422 qlt_clear_mode(vha);
4423 spin_unlock_irqrestore(&ha->hardware_lock, flags);
4425 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
4426 qla2xxx_wake_dpc(vha);
4427 qla2x00_wait_for_hba_online(vha);
4431 * Called from qla_init.c:qla24xx_vport_create() contex to setup
4432 * the target mode specific struct scsi_qla_host and struct qla_hw_data
4433 * members.
4435 void
4436 qlt_vport_create(struct scsi_qla_host *vha, struct qla_hw_data *ha)
4438 if (!qla_tgt_mode_enabled(vha))
4439 return;
4441 mutex_init(&ha->tgt.tgt_mutex);
4442 mutex_init(&ha->tgt.tgt_host_action_mutex);
4444 qlt_clear_mode(vha);
4447 * NOTE: Currently the value is kept the same for <24xx and
4448 * >=24xx ISPs. If it is necessary to change it,
4449 * the check should be added for specific ISPs,
4450 * assigning the value appropriately.
4452 ha->tgt.atio_q_length = ATIO_ENTRY_CNT_24XX;
4455 void
4456 qlt_rff_id(struct scsi_qla_host *vha, struct ct_sns_req *ct_req)
4459 * FC-4 Feature bit 0 indicates target functionality to the name server.
4461 if (qla_tgt_mode_enabled(vha)) {
4462 if (qla_ini_mode_enabled(vha))
4463 ct_req->req.rff_id.fc4_feature = BIT_0 | BIT_1;
4464 else
4465 ct_req->req.rff_id.fc4_feature = BIT_0;
4466 } else if (qla_ini_mode_enabled(vha)) {
4467 ct_req->req.rff_id.fc4_feature = BIT_1;
4472 * qlt_init_atio_q_entries() - Initializes ATIO queue entries.
4473 * @ha: HA context
4475 * Beginning of ATIO ring has initialization control block already built
4476 * by nvram config routine.
4478 * Returns 0 on success.
4480 void
4481 qlt_init_atio_q_entries(struct scsi_qla_host *vha)
4483 struct qla_hw_data *ha = vha->hw;
4484 uint16_t cnt;
4485 struct atio_from_isp *pkt = (struct atio_from_isp *)ha->tgt.atio_ring;
4487 if (!qla_tgt_mode_enabled(vha))
4488 return;
4490 for (cnt = 0; cnt < ha->tgt.atio_q_length; cnt++) {
4491 pkt->u.raw.signature = ATIO_PROCESSED;
4492 pkt++;
4498 * qlt_24xx_process_atio_queue() - Process ATIO queue entries.
4499 * @ha: SCSI driver HA context
4501 void
4502 qlt_24xx_process_atio_queue(struct scsi_qla_host *vha)
4504 struct qla_hw_data *ha = vha->hw;
4505 struct atio_from_isp *pkt;
4506 int cnt, i;
4508 if (!vha->flags.online)
4509 return;
4511 while (ha->tgt.atio_ring_ptr->signature != ATIO_PROCESSED) {
4512 pkt = (struct atio_from_isp *)ha->tgt.atio_ring_ptr;
4513 cnt = pkt->u.raw.entry_count;
4515 qlt_24xx_atio_pkt_all_vps(vha, (struct atio_from_isp *)pkt);
4517 for (i = 0; i < cnt; i++) {
4518 ha->tgt.atio_ring_index++;
4519 if (ha->tgt.atio_ring_index == ha->tgt.atio_q_length) {
4520 ha->tgt.atio_ring_index = 0;
4521 ha->tgt.atio_ring_ptr = ha->tgt.atio_ring;
4522 } else
4523 ha->tgt.atio_ring_ptr++;
4525 pkt->u.raw.signature = ATIO_PROCESSED;
4526 pkt = (struct atio_from_isp *)ha->tgt.atio_ring_ptr;
4528 wmb();
4531 /* Adjust ring index */
4532 WRT_REG_DWORD(ISP_ATIO_Q_OUT(vha), ha->tgt.atio_ring_index);
4535 void
4536 qlt_24xx_config_rings(struct scsi_qla_host *vha)
4538 struct qla_hw_data *ha = vha->hw;
4539 if (!QLA_TGT_MODE_ENABLED())
4540 return;
4542 WRT_REG_DWORD(ISP_ATIO_Q_IN(vha), 0);
4543 WRT_REG_DWORD(ISP_ATIO_Q_OUT(vha), 0);
4544 RD_REG_DWORD(ISP_ATIO_Q_OUT(vha));
4546 if (IS_ATIO_MSIX_CAPABLE(ha)) {
4547 struct qla_msix_entry *msix = &ha->msix_entries[2];
4548 struct init_cb_24xx *icb = (struct init_cb_24xx *)ha->init_cb;
4550 icb->msix_atio = cpu_to_le16(msix->entry);
4551 ql_dbg(ql_dbg_init, vha, 0xf072,
4552 "Registering ICB vector 0x%x for atio que.\n",
4553 msix->entry);
4557 void
4558 qlt_24xx_config_nvram_stage1(struct scsi_qla_host *vha, struct nvram_24xx *nv)
4560 struct qla_hw_data *ha = vha->hw;
4562 if (qla_tgt_mode_enabled(vha)) {
4563 if (!ha->tgt.saved_set) {
4564 /* We save only once */
4565 ha->tgt.saved_exchange_count = nv->exchange_count;
4566 ha->tgt.saved_firmware_options_1 =
4567 nv->firmware_options_1;
4568 ha->tgt.saved_firmware_options_2 =
4569 nv->firmware_options_2;
4570 ha->tgt.saved_firmware_options_3 =
4571 nv->firmware_options_3;
4572 ha->tgt.saved_set = 1;
4575 nv->exchange_count = __constant_cpu_to_le16(0xFFFF);
4577 /* Enable target mode */
4578 nv->firmware_options_1 |= __constant_cpu_to_le32(BIT_4);
4580 /* Disable ini mode, if requested */
4581 if (!qla_ini_mode_enabled(vha))
4582 nv->firmware_options_1 |= __constant_cpu_to_le32(BIT_5);
4584 /* Disable Full Login after LIP */
4585 nv->firmware_options_1 &= __constant_cpu_to_le32(~BIT_13);
4586 /* Enable initial LIP */
4587 nv->firmware_options_1 &= __constant_cpu_to_le32(~BIT_9);
4588 /* Enable FC tapes support */
4589 nv->firmware_options_2 |= __constant_cpu_to_le32(BIT_12);
4590 /* Disable Full Login after LIP */
4591 nv->host_p &= __constant_cpu_to_le32(~BIT_10);
4592 /* Enable target PRLI control */
4593 nv->firmware_options_2 |= __constant_cpu_to_le32(BIT_14);
4594 } else {
4595 if (ha->tgt.saved_set) {
4596 nv->exchange_count = ha->tgt.saved_exchange_count;
4597 nv->firmware_options_1 =
4598 ha->tgt.saved_firmware_options_1;
4599 nv->firmware_options_2 =
4600 ha->tgt.saved_firmware_options_2;
4601 nv->firmware_options_3 =
4602 ha->tgt.saved_firmware_options_3;
4604 return;
4607 /* out-of-order frames reassembly */
4608 nv->firmware_options_3 |= BIT_6|BIT_9;
4610 if (ha->tgt.enable_class_2) {
4611 if (vha->flags.init_done)
4612 fc_host_supported_classes(vha->host) =
4613 FC_COS_CLASS2 | FC_COS_CLASS3;
4615 nv->firmware_options_2 |= __constant_cpu_to_le32(BIT_8);
4616 } else {
4617 if (vha->flags.init_done)
4618 fc_host_supported_classes(vha->host) = FC_COS_CLASS3;
4620 nv->firmware_options_2 &= ~__constant_cpu_to_le32(BIT_8);
4624 void
4625 qlt_24xx_config_nvram_stage2(struct scsi_qla_host *vha,
4626 struct init_cb_24xx *icb)
4628 struct qla_hw_data *ha = vha->hw;
4630 if (ha->tgt.node_name_set) {
4631 memcpy(icb->node_name, ha->tgt.tgt_node_name, WWN_SIZE);
4632 icb->firmware_options_1 |= __constant_cpu_to_le32(BIT_14);
4636 void
4637 qlt_81xx_config_nvram_stage1(struct scsi_qla_host *vha, struct nvram_81xx *nv)
4639 struct qla_hw_data *ha = vha->hw;
4641 if (!QLA_TGT_MODE_ENABLED())
4642 return;
4644 if (qla_tgt_mode_enabled(vha)) {
4645 if (!ha->tgt.saved_set) {
4646 /* We save only once */
4647 ha->tgt.saved_exchange_count = nv->exchange_count;
4648 ha->tgt.saved_firmware_options_1 =
4649 nv->firmware_options_1;
4650 ha->tgt.saved_firmware_options_2 =
4651 nv->firmware_options_2;
4652 ha->tgt.saved_firmware_options_3 =
4653 nv->firmware_options_3;
4654 ha->tgt.saved_set = 1;
4657 nv->exchange_count = __constant_cpu_to_le16(0xFFFF);
4659 /* Enable target mode */
4660 nv->firmware_options_1 |= __constant_cpu_to_le32(BIT_4);
4662 /* Disable ini mode, if requested */
4663 if (!qla_ini_mode_enabled(vha))
4664 nv->firmware_options_1 |=
4665 __constant_cpu_to_le32(BIT_5);
4667 /* Disable Full Login after LIP */
4668 nv->firmware_options_1 &= __constant_cpu_to_le32(~BIT_13);
4669 /* Enable initial LIP */
4670 nv->firmware_options_1 &= __constant_cpu_to_le32(~BIT_9);
4671 /* Enable FC tapes support */
4672 nv->firmware_options_2 |= __constant_cpu_to_le32(BIT_12);
4673 /* Disable Full Login after LIP */
4674 nv->host_p &= __constant_cpu_to_le32(~BIT_10);
4675 /* Enable target PRLI control */
4676 nv->firmware_options_2 |= __constant_cpu_to_le32(BIT_14);
4677 } else {
4678 if (ha->tgt.saved_set) {
4679 nv->exchange_count = ha->tgt.saved_exchange_count;
4680 nv->firmware_options_1 =
4681 ha->tgt.saved_firmware_options_1;
4682 nv->firmware_options_2 =
4683 ha->tgt.saved_firmware_options_2;
4684 nv->firmware_options_3 =
4685 ha->tgt.saved_firmware_options_3;
4687 return;
4690 /* out-of-order frames reassembly */
4691 nv->firmware_options_3 |= BIT_6|BIT_9;
4693 if (ha->tgt.enable_class_2) {
4694 if (vha->flags.init_done)
4695 fc_host_supported_classes(vha->host) =
4696 FC_COS_CLASS2 | FC_COS_CLASS3;
4698 nv->firmware_options_2 |= __constant_cpu_to_le32(BIT_8);
4699 } else {
4700 if (vha->flags.init_done)
4701 fc_host_supported_classes(vha->host) = FC_COS_CLASS3;
4703 nv->firmware_options_2 &= ~__constant_cpu_to_le32(BIT_8);
4707 void
4708 qlt_81xx_config_nvram_stage2(struct scsi_qla_host *vha,
4709 struct init_cb_81xx *icb)
4711 struct qla_hw_data *ha = vha->hw;
4713 if (!QLA_TGT_MODE_ENABLED())
4714 return;
4716 if (ha->tgt.node_name_set) {
4717 memcpy(icb->node_name, ha->tgt.tgt_node_name, WWN_SIZE);
4718 icb->firmware_options_1 |= __constant_cpu_to_le32(BIT_14);
4722 void
4723 qlt_83xx_iospace_config(struct qla_hw_data *ha)
4725 if (!QLA_TGT_MODE_ENABLED())
4726 return;
4728 ha->msix_count += 1; /* For ATIO Q */
4732 qlt_24xx_process_response_error(struct scsi_qla_host *vha,
4733 struct sts_entry_24xx *pkt)
4735 switch (pkt->entry_type) {
4736 case ABTS_RECV_24XX:
4737 case ABTS_RESP_24XX:
4738 case CTIO_TYPE7:
4739 case NOTIFY_ACK_TYPE:
4740 return 1;
4741 default:
4742 return 0;
4746 void
4747 qlt_modify_vp_config(struct scsi_qla_host *vha,
4748 struct vp_config_entry_24xx *vpmod)
4750 if (qla_tgt_mode_enabled(vha))
4751 vpmod->options_idx1 &= ~BIT_5;
4752 /* Disable ini mode, if requested */
4753 if (!qla_ini_mode_enabled(vha))
4754 vpmod->options_idx1 &= ~BIT_4;
4757 void
4758 qlt_probe_one_stage1(struct scsi_qla_host *base_vha, struct qla_hw_data *ha)
4760 if (!QLA_TGT_MODE_ENABLED())
4761 return;
4763 if (ha->mqenable || IS_QLA83XX(ha)) {
4764 ISP_ATIO_Q_IN(base_vha) = &ha->mqiobase->isp25mq.atio_q_in;
4765 ISP_ATIO_Q_OUT(base_vha) = &ha->mqiobase->isp25mq.atio_q_out;
4766 } else {
4767 ISP_ATIO_Q_IN(base_vha) = &ha->iobase->isp24.atio_q_in;
4768 ISP_ATIO_Q_OUT(base_vha) = &ha->iobase->isp24.atio_q_out;
4771 mutex_init(&ha->tgt.tgt_mutex);
4772 mutex_init(&ha->tgt.tgt_host_action_mutex);
4773 qlt_clear_mode(base_vha);
4776 irqreturn_t
4777 qla83xx_msix_atio_q(int irq, void *dev_id)
4779 struct rsp_que *rsp;
4780 scsi_qla_host_t *vha;
4781 struct qla_hw_data *ha;
4782 unsigned long flags;
4784 rsp = (struct rsp_que *) dev_id;
4785 ha = rsp->hw;
4786 vha = pci_get_drvdata(ha->pdev);
4788 spin_lock_irqsave(&ha->hardware_lock, flags);
4790 qlt_24xx_process_atio_queue(vha);
4791 qla24xx_process_response_queue(vha, rsp);
4793 spin_unlock_irqrestore(&ha->hardware_lock, flags);
4795 return IRQ_HANDLED;
4799 qlt_mem_alloc(struct qla_hw_data *ha)
4801 if (!QLA_TGT_MODE_ENABLED())
4802 return 0;
4804 ha->tgt.tgt_vp_map = kzalloc(sizeof(struct qla_tgt_vp_map) *
4805 MAX_MULTI_ID_FABRIC, GFP_KERNEL);
4806 if (!ha->tgt.tgt_vp_map)
4807 return -ENOMEM;
4809 ha->tgt.atio_ring = dma_alloc_coherent(&ha->pdev->dev,
4810 (ha->tgt.atio_q_length + 1) * sizeof(struct atio_from_isp),
4811 &ha->tgt.atio_dma, GFP_KERNEL);
4812 if (!ha->tgt.atio_ring) {
4813 kfree(ha->tgt.tgt_vp_map);
4814 return -ENOMEM;
4816 return 0;
4819 void
4820 qlt_mem_free(struct qla_hw_data *ha)
4822 if (!QLA_TGT_MODE_ENABLED())
4823 return;
4825 if (ha->tgt.atio_ring) {
4826 dma_free_coherent(&ha->pdev->dev, (ha->tgt.atio_q_length + 1) *
4827 sizeof(struct atio_from_isp), ha->tgt.atio_ring,
4828 ha->tgt.atio_dma);
4830 kfree(ha->tgt.tgt_vp_map);
4833 /* vport_slock to be held by the caller */
4834 void
4835 qlt_update_vp_map(struct scsi_qla_host *vha, int cmd)
4837 if (!QLA_TGT_MODE_ENABLED())
4838 return;
4840 switch (cmd) {
4841 case SET_VP_IDX:
4842 vha->hw->tgt.tgt_vp_map[vha->vp_idx].vha = vha;
4843 break;
4844 case SET_AL_PA:
4845 vha->hw->tgt.tgt_vp_map[vha->d_id.b.al_pa].idx = vha->vp_idx;
4846 break;
4847 case RESET_VP_IDX:
4848 vha->hw->tgt.tgt_vp_map[vha->vp_idx].vha = NULL;
4849 break;
4850 case RESET_AL_PA:
4851 vha->hw->tgt.tgt_vp_map[vha->d_id.b.al_pa].idx = 0;
4852 break;
4856 static int __init qlt_parse_ini_mode(void)
4858 if (strcasecmp(qlini_mode, QLA2XXX_INI_MODE_STR_EXCLUSIVE) == 0)
4859 ql2x_ini_mode = QLA2XXX_INI_MODE_EXCLUSIVE;
4860 else if (strcasecmp(qlini_mode, QLA2XXX_INI_MODE_STR_DISABLED) == 0)
4861 ql2x_ini_mode = QLA2XXX_INI_MODE_DISABLED;
4862 else if (strcasecmp(qlini_mode, QLA2XXX_INI_MODE_STR_ENABLED) == 0)
4863 ql2x_ini_mode = QLA2XXX_INI_MODE_ENABLED;
4864 else
4865 return false;
4867 return true;
4870 int __init qlt_init(void)
4872 int ret;
4874 if (!qlt_parse_ini_mode()) {
4875 ql_log(ql_log_fatal, NULL, 0xe06b,
4876 "qlt_parse_ini_mode() failed\n");
4877 return -EINVAL;
4880 if (!QLA_TGT_MODE_ENABLED())
4881 return 0;
4883 qla_tgt_cmd_cachep = kmem_cache_create("qla_tgt_cmd_cachep",
4884 sizeof(struct qla_tgt_cmd), __alignof__(struct qla_tgt_cmd), 0,
4885 NULL);
4886 if (!qla_tgt_cmd_cachep) {
4887 ql_log(ql_log_fatal, NULL, 0xe06c,
4888 "kmem_cache_create for qla_tgt_cmd_cachep failed\n");
4889 return -ENOMEM;
4892 qla_tgt_mgmt_cmd_cachep = kmem_cache_create("qla_tgt_mgmt_cmd_cachep",
4893 sizeof(struct qla_tgt_mgmt_cmd), __alignof__(struct
4894 qla_tgt_mgmt_cmd), 0, NULL);
4895 if (!qla_tgt_mgmt_cmd_cachep) {
4896 ql_log(ql_log_fatal, NULL, 0xe06d,
4897 "kmem_cache_create for qla_tgt_mgmt_cmd_cachep failed\n");
4898 ret = -ENOMEM;
4899 goto out;
4902 qla_tgt_mgmt_cmd_mempool = mempool_create(25, mempool_alloc_slab,
4903 mempool_free_slab, qla_tgt_mgmt_cmd_cachep);
4904 if (!qla_tgt_mgmt_cmd_mempool) {
4905 ql_log(ql_log_fatal, NULL, 0xe06e,
4906 "mempool_create for qla_tgt_mgmt_cmd_mempool failed\n");
4907 ret = -ENOMEM;
4908 goto out_mgmt_cmd_cachep;
4911 qla_tgt_wq = alloc_workqueue("qla_tgt_wq", 0, 0);
4912 if (!qla_tgt_wq) {
4913 ql_log(ql_log_fatal, NULL, 0xe06f,
4914 "alloc_workqueue for qla_tgt_wq failed\n");
4915 ret = -ENOMEM;
4916 goto out_cmd_mempool;
4919 * Return 1 to signal that initiator-mode is being disabled
4921 return (ql2x_ini_mode == QLA2XXX_INI_MODE_DISABLED) ? 1 : 0;
4923 out_cmd_mempool:
4924 mempool_destroy(qla_tgt_mgmt_cmd_mempool);
4925 out_mgmt_cmd_cachep:
4926 kmem_cache_destroy(qla_tgt_mgmt_cmd_cachep);
4927 out:
4928 kmem_cache_destroy(qla_tgt_cmd_cachep);
4929 return ret;
4932 void qlt_exit(void)
4934 if (!QLA_TGT_MODE_ENABLED())
4935 return;
4937 destroy_workqueue(qla_tgt_wq);
4938 mempool_destroy(qla_tgt_mgmt_cmd_mempool);
4939 kmem_cache_destroy(qla_tgt_mgmt_cmd_cachep);
4940 kmem_cache_destroy(qla_tgt_cmd_cachep);