ARM: EXYNOS: Make combiner_init function static
[linux-2.6/libata-dev.git] / drivers / scsi / qla2xxx / qla_target.c
blob6986552b47e6f6af591681fa4f14dbe3c0b11a6f
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-2011 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 static 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. contigent 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 "
434 "%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x, "
435 "mcmd %x, loop_id %d)\n", vha->host_no, sess,
436 sess->port_name[0], sess->port_name[1],
437 sess->port_name[2], sess->port_name[3],
438 sess->port_name[4], sess->port_name[5],
439 sess->port_name[6], sess->port_name[7],
440 mcmd, loop_id);
442 lun = a->u.isp24.fcp_cmnd.lun;
443 unpacked_lun = scsilun_to_int((struct scsi_lun *)&lun);
445 return qlt_issue_task_mgmt(sess, unpacked_lun, mcmd,
446 iocb, QLA24XX_MGMT_SEND_NACK);
449 /* ha->hardware_lock supposed to be held on entry */
450 static void qlt_schedule_sess_for_deletion(struct qla_tgt_sess *sess,
451 bool immediate)
453 struct qla_tgt *tgt = sess->tgt;
454 uint32_t dev_loss_tmo = tgt->ha->port_down_retry_count + 5;
456 if (sess->deleted)
457 return;
459 ql_dbg(ql_dbg_tgt, sess->vha, 0xe001,
460 "Scheduling sess %p for deletion\n", sess);
461 list_add_tail(&sess->del_list_entry, &tgt->del_sess_list);
462 sess->deleted = 1;
464 if (immediate)
465 dev_loss_tmo = 0;
467 sess->expires = jiffies + dev_loss_tmo * HZ;
469 ql_dbg(ql_dbg_tgt, sess->vha, 0xe048,
470 "qla_target(%d): session for port %02x:%02x:%02x:"
471 "%02x:%02x:%02x:%02x:%02x (loop ID %d) scheduled for "
472 "deletion in %u secs (expires: %lu) immed: %d\n",
473 sess->vha->vp_idx,
474 sess->port_name[0], sess->port_name[1],
475 sess->port_name[2], sess->port_name[3],
476 sess->port_name[4], sess->port_name[5],
477 sess->port_name[6], sess->port_name[7],
478 sess->loop_id, dev_loss_tmo, sess->expires, immediate);
480 if (immediate)
481 schedule_delayed_work(&tgt->sess_del_work, 0);
482 else
483 schedule_delayed_work(&tgt->sess_del_work,
484 jiffies - sess->expires);
487 /* ha->hardware_lock supposed to be held on entry */
488 static void qlt_clear_tgt_db(struct qla_tgt *tgt, bool local_only)
490 struct qla_tgt_sess *sess;
492 list_for_each_entry(sess, &tgt->sess_list, sess_list_entry)
493 qlt_schedule_sess_for_deletion(sess, true);
495 /* At this point tgt could be already dead */
498 static int qla24xx_get_loop_id(struct scsi_qla_host *vha, const uint8_t *s_id,
499 uint16_t *loop_id)
501 struct qla_hw_data *ha = vha->hw;
502 dma_addr_t gid_list_dma;
503 struct gid_list_info *gid_list;
504 char *id_iter;
505 int res, rc, i;
506 uint16_t entries;
508 gid_list = dma_alloc_coherent(&ha->pdev->dev, qla2x00_gid_list_size(ha),
509 &gid_list_dma, GFP_KERNEL);
510 if (!gid_list) {
511 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf044,
512 "qla_target(%d): DMA Alloc failed of %u\n",
513 vha->vp_idx, qla2x00_gid_list_size(ha));
514 return -ENOMEM;
517 /* Get list of logged in devices */
518 rc = qla2x00_get_id_list(vha, gid_list, gid_list_dma, &entries);
519 if (rc != QLA_SUCCESS) {
520 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf045,
521 "qla_target(%d): get_id_list() failed: %x\n",
522 vha->vp_idx, rc);
523 res = -1;
524 goto out_free_id_list;
527 id_iter = (char *)gid_list;
528 res = -1;
529 for (i = 0; i < entries; i++) {
530 struct gid_list_info *gid = (struct gid_list_info *)id_iter;
531 if ((gid->al_pa == s_id[2]) &&
532 (gid->area == s_id[1]) &&
533 (gid->domain == s_id[0])) {
534 *loop_id = le16_to_cpu(gid->loop_id);
535 res = 0;
536 break;
538 id_iter += ha->gid_list_info_size;
541 out_free_id_list:
542 dma_free_coherent(&ha->pdev->dev, qla2x00_gid_list_size(ha),
543 gid_list, gid_list_dma);
544 return res;
547 static bool qlt_check_fcport_exist(struct scsi_qla_host *vha,
548 struct qla_tgt_sess *sess)
550 struct qla_hw_data *ha = vha->hw;
551 struct qla_port_24xx_data *pmap24;
552 bool res, found = false;
553 int rc, i;
554 uint16_t loop_id = 0xFFFF; /* to eliminate compiler's warning */
555 uint16_t entries;
556 void *pmap;
557 int pmap_len;
558 fc_port_t *fcport;
559 int global_resets;
561 retry:
562 global_resets = atomic_read(&ha->tgt.qla_tgt->tgt_global_resets_count);
564 rc = qla2x00_get_node_name_list(vha, &pmap, &pmap_len);
565 if (rc != QLA_SUCCESS) {
566 res = false;
567 goto out;
570 pmap24 = pmap;
571 entries = pmap_len/sizeof(*pmap24);
573 for (i = 0; i < entries; ++i) {
574 if (!memcmp(sess->port_name, pmap24[i].port_name, WWN_SIZE)) {
575 loop_id = le16_to_cpu(pmap24[i].loop_id);
576 found = true;
577 break;
581 kfree(pmap);
583 if (!found) {
584 res = false;
585 goto out;
588 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf046,
589 "qlt_check_fcport_exist(): loop_id %d", loop_id);
591 fcport = kzalloc(sizeof(*fcport), GFP_KERNEL);
592 if (fcport == NULL) {
593 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf047,
594 "qla_target(%d): Allocation of tmp FC port failed",
595 vha->vp_idx);
596 res = false;
597 goto out;
600 fcport->loop_id = loop_id;
602 rc = qla2x00_get_port_database(vha, fcport, 0);
603 if (rc != QLA_SUCCESS) {
604 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf048,
605 "qla_target(%d): Failed to retrieve fcport "
606 "information -- get_port_database() returned %x "
607 "(loop_id=0x%04x)", vha->vp_idx, rc, loop_id);
608 res = false;
609 goto out_free_fcport;
612 if (global_resets !=
613 atomic_read(&ha->tgt.qla_tgt->tgt_global_resets_count)) {
614 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf002,
615 "qla_target(%d): global reset during session discovery"
616 " (counter was %d, new %d), retrying",
617 vha->vp_idx, global_resets,
618 atomic_read(&ha->tgt.qla_tgt->tgt_global_resets_count));
619 goto retry;
622 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf003,
623 "Updating sess %p s_id %x:%x:%x, loop_id %d) to d_id %x:%x:%x, "
624 "loop_id %d", sess, sess->s_id.b.domain, sess->s_id.b.al_pa,
625 sess->s_id.b.area, sess->loop_id, fcport->d_id.b.domain,
626 fcport->d_id.b.al_pa, fcport->d_id.b.area, fcport->loop_id);
628 sess->s_id = fcport->d_id;
629 sess->loop_id = fcport->loop_id;
630 sess->conf_compl_supported = !!(fcport->flags &
631 FCF_CONF_COMP_SUPPORTED);
633 res = true;
635 out_free_fcport:
636 kfree(fcport);
638 out:
639 return res;
642 /* ha->hardware_lock supposed to be held on entry */
643 static void qlt_undelete_sess(struct qla_tgt_sess *sess)
645 BUG_ON(!sess->deleted);
647 list_del(&sess->del_list_entry);
648 sess->deleted = 0;
651 static void qlt_del_sess_work_fn(struct delayed_work *work)
653 struct qla_tgt *tgt = container_of(work, struct qla_tgt,
654 sess_del_work);
655 struct scsi_qla_host *vha = tgt->vha;
656 struct qla_hw_data *ha = vha->hw;
657 struct qla_tgt_sess *sess;
658 unsigned long flags;
660 spin_lock_irqsave(&ha->hardware_lock, flags);
661 while (!list_empty(&tgt->del_sess_list)) {
662 sess = list_entry(tgt->del_sess_list.next, typeof(*sess),
663 del_list_entry);
664 if (time_after_eq(jiffies, sess->expires)) {
665 bool cancel;
667 qlt_undelete_sess(sess);
669 spin_unlock_irqrestore(&ha->hardware_lock, flags);
670 cancel = qlt_check_fcport_exist(vha, sess);
672 if (cancel) {
673 if (sess->deleted) {
675 * sess was again deleted while we were
676 * discovering it
678 spin_lock_irqsave(&ha->hardware_lock,
679 flags);
680 continue;
683 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf049,
684 "qla_target(%d): cancel deletion of "
685 "session for port %02x:%02x:%02x:%02x:%02x:"
686 "%02x:%02x:%02x (loop ID %d), because "
687 " it isn't deleted by firmware",
688 vha->vp_idx, sess->port_name[0],
689 sess->port_name[1], sess->port_name[2],
690 sess->port_name[3], sess->port_name[4],
691 sess->port_name[5], sess->port_name[6],
692 sess->port_name[7], sess->loop_id);
693 } else {
694 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf004,
695 "Timeout: sess %p about to be deleted\n",
696 sess);
697 ha->tgt.tgt_ops->shutdown_sess(sess);
698 ha->tgt.tgt_ops->put_sess(sess);
701 spin_lock_irqsave(&ha->hardware_lock, flags);
702 } else {
703 schedule_delayed_work(&tgt->sess_del_work,
704 jiffies - sess->expires);
705 break;
708 spin_unlock_irqrestore(&ha->hardware_lock, flags);
712 * Adds an extra ref to allow to drop hw lock after adding sess to the list.
713 * Caller must put it.
715 static struct qla_tgt_sess *qlt_create_sess(
716 struct scsi_qla_host *vha,
717 fc_port_t *fcport,
718 bool local)
720 struct qla_hw_data *ha = vha->hw;
721 struct qla_tgt_sess *sess;
722 unsigned long flags;
723 unsigned char be_sid[3];
725 /* Check to avoid double sessions */
726 spin_lock_irqsave(&ha->hardware_lock, flags);
727 list_for_each_entry(sess, &ha->tgt.qla_tgt->sess_list,
728 sess_list_entry) {
729 if (!memcmp(sess->port_name, fcport->port_name, WWN_SIZE)) {
730 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf005,
731 "Double sess %p found (s_id %x:%x:%x, "
732 "loop_id %d), updating to d_id %x:%x:%x, "
733 "loop_id %d", sess, sess->s_id.b.domain,
734 sess->s_id.b.al_pa, sess->s_id.b.area,
735 sess->loop_id, fcport->d_id.b.domain,
736 fcport->d_id.b.al_pa, fcport->d_id.b.area,
737 fcport->loop_id);
739 if (sess->deleted)
740 qlt_undelete_sess(sess);
742 kref_get(&sess->se_sess->sess_kref);
743 sess->s_id = fcport->d_id;
744 sess->loop_id = fcport->loop_id;
745 sess->conf_compl_supported = !!(fcport->flags &
746 FCF_CONF_COMP_SUPPORTED);
747 if (sess->local && !local)
748 sess->local = 0;
749 spin_unlock_irqrestore(&ha->hardware_lock, flags);
751 return sess;
754 spin_unlock_irqrestore(&ha->hardware_lock, flags);
756 sess = kzalloc(sizeof(*sess), GFP_KERNEL);
757 if (!sess) {
758 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04a,
759 "qla_target(%u): session allocation failed, "
760 "all commands from port %02x:%02x:%02x:%02x:"
761 "%02x:%02x:%02x:%02x will be refused", vha->vp_idx,
762 fcport->port_name[0], fcport->port_name[1],
763 fcport->port_name[2], fcport->port_name[3],
764 fcport->port_name[4], fcport->port_name[5],
765 fcport->port_name[6], fcport->port_name[7]);
767 return NULL;
769 sess->tgt = ha->tgt.qla_tgt;
770 sess->vha = vha;
771 sess->s_id = fcport->d_id;
772 sess->loop_id = fcport->loop_id;
773 sess->local = local;
775 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf006,
776 "Adding sess %p to tgt %p via ->check_initiator_node_acl()\n",
777 sess, ha->tgt.qla_tgt);
779 be_sid[0] = sess->s_id.b.domain;
780 be_sid[1] = sess->s_id.b.area;
781 be_sid[2] = sess->s_id.b.al_pa;
783 * Determine if this fc_port->port_name is allowed to access
784 * target mode using explict NodeACLs+MappedLUNs, or using
785 * TPG demo mode. If this is successful a target mode FC nexus
786 * is created.
788 if (ha->tgt.tgt_ops->check_initiator_node_acl(vha,
789 &fcport->port_name[0], sess, &be_sid[0], fcport->loop_id) < 0) {
790 kfree(sess);
791 return NULL;
794 * Take an extra reference to ->sess_kref here to handle qla_tgt_sess
795 * access across ->hardware_lock reaquire.
797 kref_get(&sess->se_sess->sess_kref);
799 sess->conf_compl_supported = !!(fcport->flags &
800 FCF_CONF_COMP_SUPPORTED);
801 BUILD_BUG_ON(sizeof(sess->port_name) != sizeof(fcport->port_name));
802 memcpy(sess->port_name, fcport->port_name, sizeof(sess->port_name));
804 spin_lock_irqsave(&ha->hardware_lock, flags);
805 list_add_tail(&sess->sess_list_entry, &ha->tgt.qla_tgt->sess_list);
806 ha->tgt.qla_tgt->sess_count++;
807 spin_unlock_irqrestore(&ha->hardware_lock, flags);
809 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04b,
810 "qla_target(%d): %ssession for wwn %02x:%02x:%02x:%02x:"
811 "%02x:%02x:%02x:%02x (loop_id %d, s_id %x:%x:%x, confirmed"
812 " completion %ssupported) added\n",
813 vha->vp_idx, local ? "local " : "", fcport->port_name[0],
814 fcport->port_name[1], fcport->port_name[2], fcport->port_name[3],
815 fcport->port_name[4], fcport->port_name[5], fcport->port_name[6],
816 fcport->port_name[7], fcport->loop_id, sess->s_id.b.domain,
817 sess->s_id.b.area, sess->s_id.b.al_pa, sess->conf_compl_supported ?
818 "" : "not ");
820 return sess;
824 * Called from drivers/scsi/qla2xxx/qla_init.c:qla2x00_reg_remote_port()
826 void qlt_fc_port_added(struct scsi_qla_host *vha, fc_port_t *fcport)
828 struct qla_hw_data *ha = vha->hw;
829 struct qla_tgt *tgt = ha->tgt.qla_tgt;
830 struct qla_tgt_sess *sess;
831 unsigned long flags;
833 if (!vha->hw->tgt.tgt_ops)
834 return;
836 if (!tgt || (fcport->port_type != FCT_INITIATOR))
837 return;
839 spin_lock_irqsave(&ha->hardware_lock, flags);
840 if (tgt->tgt_stop) {
841 spin_unlock_irqrestore(&ha->hardware_lock, flags);
842 return;
844 sess = qlt_find_sess_by_port_name(tgt, fcport->port_name);
845 if (!sess) {
846 spin_unlock_irqrestore(&ha->hardware_lock, flags);
848 mutex_lock(&ha->tgt.tgt_mutex);
849 sess = qlt_create_sess(vha, fcport, false);
850 mutex_unlock(&ha->tgt.tgt_mutex);
852 spin_lock_irqsave(&ha->hardware_lock, flags);
853 } else {
854 kref_get(&sess->se_sess->sess_kref);
856 if (sess->deleted) {
857 qlt_undelete_sess(sess);
859 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04c,
860 "qla_target(%u): %ssession for port %02x:"
861 "%02x:%02x:%02x:%02x:%02x:%02x:%02x (loop ID %d) "
862 "reappeared\n", vha->vp_idx, sess->local ? "local "
863 : "", sess->port_name[0], sess->port_name[1],
864 sess->port_name[2], sess->port_name[3],
865 sess->port_name[4], sess->port_name[5],
866 sess->port_name[6], sess->port_name[7],
867 sess->loop_id);
869 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf007,
870 "Reappeared sess %p\n", sess);
872 sess->s_id = fcport->d_id;
873 sess->loop_id = fcport->loop_id;
874 sess->conf_compl_supported = !!(fcport->flags &
875 FCF_CONF_COMP_SUPPORTED);
878 if (sess && sess->local) {
879 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04d,
880 "qla_target(%u): local session for "
881 "port %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x "
882 "(loop ID %d) became global\n", vha->vp_idx,
883 fcport->port_name[0], fcport->port_name[1],
884 fcport->port_name[2], fcport->port_name[3],
885 fcport->port_name[4], fcport->port_name[5],
886 fcport->port_name[6], fcport->port_name[7],
887 sess->loop_id);
888 sess->local = 0;
890 spin_unlock_irqrestore(&ha->hardware_lock, flags);
892 ha->tgt.tgt_ops->put_sess(sess);
895 void qlt_fc_port_deleted(struct scsi_qla_host *vha, fc_port_t *fcport)
897 struct qla_hw_data *ha = vha->hw;
898 struct qla_tgt *tgt = ha->tgt.qla_tgt;
899 struct qla_tgt_sess *sess;
900 unsigned long flags;
902 if (!vha->hw->tgt.tgt_ops)
903 return;
905 if (!tgt || (fcport->port_type != FCT_INITIATOR))
906 return;
908 spin_lock_irqsave(&ha->hardware_lock, flags);
909 if (tgt->tgt_stop) {
910 spin_unlock_irqrestore(&ha->hardware_lock, flags);
911 return;
913 sess = qlt_find_sess_by_port_name(tgt, fcport->port_name);
914 if (!sess) {
915 spin_unlock_irqrestore(&ha->hardware_lock, flags);
916 return;
919 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf008, "qla_tgt_fc_port_deleted %p", sess);
921 sess->local = 1;
922 qlt_schedule_sess_for_deletion(sess, false);
923 spin_unlock_irqrestore(&ha->hardware_lock, flags);
926 static inline int test_tgt_sess_count(struct qla_tgt *tgt)
928 struct qla_hw_data *ha = tgt->ha;
929 unsigned long flags;
930 int res;
932 * We need to protect against race, when tgt is freed before or
933 * inside wake_up()
935 spin_lock_irqsave(&ha->hardware_lock, flags);
936 ql_dbg(ql_dbg_tgt, tgt->vha, 0xe002,
937 "tgt %p, empty(sess_list)=%d sess_count=%d\n",
938 tgt, list_empty(&tgt->sess_list), tgt->sess_count);
939 res = (tgt->sess_count == 0);
940 spin_unlock_irqrestore(&ha->hardware_lock, flags);
942 return res;
945 /* Called by tcm_qla2xxx configfs code */
946 void qlt_stop_phase1(struct qla_tgt *tgt)
948 struct scsi_qla_host *vha = tgt->vha;
949 struct qla_hw_data *ha = tgt->ha;
950 unsigned long flags;
952 if (tgt->tgt_stop || tgt->tgt_stopped) {
953 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04e,
954 "Already in tgt->tgt_stop or tgt_stopped state\n");
955 dump_stack();
956 return;
959 ql_dbg(ql_dbg_tgt, vha, 0xe003, "Stopping target for host %ld(%p)\n",
960 vha->host_no, vha);
962 * Mutex needed to sync with qla_tgt_fc_port_[added,deleted].
963 * Lock is needed, because we still can get an incoming packet.
965 mutex_lock(&ha->tgt.tgt_mutex);
966 spin_lock_irqsave(&ha->hardware_lock, flags);
967 tgt->tgt_stop = 1;
968 qlt_clear_tgt_db(tgt, true);
969 spin_unlock_irqrestore(&ha->hardware_lock, flags);
970 mutex_unlock(&ha->tgt.tgt_mutex);
972 flush_delayed_work_sync(&tgt->sess_del_work);
974 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf009,
975 "Waiting for sess works (tgt %p)", tgt);
976 spin_lock_irqsave(&tgt->sess_work_lock, flags);
977 while (!list_empty(&tgt->sess_works_list)) {
978 spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
979 flush_scheduled_work();
980 spin_lock_irqsave(&tgt->sess_work_lock, flags);
982 spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
984 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00a,
985 "Waiting for tgt %p: list_empty(sess_list)=%d "
986 "sess_count=%d\n", tgt, list_empty(&tgt->sess_list),
987 tgt->sess_count);
989 wait_event(tgt->waitQ, test_tgt_sess_count(tgt));
991 /* Big hammer */
992 if (!ha->flags.host_shutting_down && qla_tgt_mode_enabled(vha))
993 qlt_disable_vha(vha);
995 /* Wait for sessions to clear out (just in case) */
996 wait_event(tgt->waitQ, test_tgt_sess_count(tgt));
998 EXPORT_SYMBOL(qlt_stop_phase1);
1000 /* Called by tcm_qla2xxx configfs code */
1001 void qlt_stop_phase2(struct qla_tgt *tgt)
1003 struct qla_hw_data *ha = tgt->ha;
1004 unsigned long flags;
1006 if (tgt->tgt_stopped) {
1007 ql_dbg(ql_dbg_tgt_mgt, tgt->vha, 0xf04f,
1008 "Already in tgt->tgt_stopped state\n");
1009 dump_stack();
1010 return;
1013 ql_dbg(ql_dbg_tgt_mgt, tgt->vha, 0xf00b,
1014 "Waiting for %d IRQ commands to complete (tgt %p)",
1015 tgt->irq_cmd_count, tgt);
1017 mutex_lock(&ha->tgt.tgt_mutex);
1018 spin_lock_irqsave(&ha->hardware_lock, flags);
1019 while (tgt->irq_cmd_count != 0) {
1020 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1021 udelay(2);
1022 spin_lock_irqsave(&ha->hardware_lock, flags);
1024 tgt->tgt_stop = 0;
1025 tgt->tgt_stopped = 1;
1026 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1027 mutex_unlock(&ha->tgt.tgt_mutex);
1029 ql_dbg(ql_dbg_tgt_mgt, tgt->vha, 0xf00c, "Stop of tgt %p finished",
1030 tgt);
1032 EXPORT_SYMBOL(qlt_stop_phase2);
1034 /* Called from qlt_remove_target() -> qla2x00_remove_one() */
1035 void qlt_release(struct qla_tgt *tgt)
1037 struct qla_hw_data *ha = tgt->ha;
1039 if ((ha->tgt.qla_tgt != NULL) && !tgt->tgt_stopped)
1040 qlt_stop_phase2(tgt);
1042 ha->tgt.qla_tgt = NULL;
1044 ql_dbg(ql_dbg_tgt_mgt, tgt->vha, 0xf00d,
1045 "Release of tgt %p finished\n", tgt);
1047 kfree(tgt);
1050 /* ha->hardware_lock supposed to be held on entry */
1051 static int qlt_sched_sess_work(struct qla_tgt *tgt, int type,
1052 const void *param, unsigned int param_size)
1054 struct qla_tgt_sess_work_param *prm;
1055 unsigned long flags;
1057 prm = kzalloc(sizeof(*prm), GFP_ATOMIC);
1058 if (!prm) {
1059 ql_dbg(ql_dbg_tgt_mgt, tgt->vha, 0xf050,
1060 "qla_target(%d): Unable to create session "
1061 "work, command will be refused", 0);
1062 return -ENOMEM;
1065 ql_dbg(ql_dbg_tgt_mgt, tgt->vha, 0xf00e,
1066 "Scheduling work (type %d, prm %p)"
1067 " to find session for param %p (size %d, tgt %p)\n",
1068 type, prm, param, param_size, tgt);
1070 prm->type = type;
1071 memcpy(&prm->tm_iocb, param, param_size);
1073 spin_lock_irqsave(&tgt->sess_work_lock, flags);
1074 list_add_tail(&prm->sess_works_list_entry, &tgt->sess_works_list);
1075 spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
1077 schedule_work(&tgt->sess_work);
1079 return 0;
1083 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
1085 static void qlt_send_notify_ack(struct scsi_qla_host *vha,
1086 struct imm_ntfy_from_isp *ntfy,
1087 uint32_t add_flags, uint16_t resp_code, int resp_code_valid,
1088 uint16_t srr_flags, uint16_t srr_reject_code, uint8_t srr_explan)
1090 struct qla_hw_data *ha = vha->hw;
1091 request_t *pkt;
1092 struct nack_to_isp *nack;
1094 ql_dbg(ql_dbg_tgt, vha, 0xe004, "Sending NOTIFY_ACK (ha=%p)\n", ha);
1096 /* Send marker if required */
1097 if (qlt_issue_marker(vha, 1) != QLA_SUCCESS)
1098 return;
1100 pkt = (request_t *)qla2x00_alloc_iocbs(vha, NULL);
1101 if (!pkt) {
1102 ql_dbg(ql_dbg_tgt, vha, 0xe049,
1103 "qla_target(%d): %s failed: unable to allocate "
1104 "request packet\n", vha->vp_idx, __func__);
1105 return;
1108 if (ha->tgt.qla_tgt != NULL)
1109 ha->tgt.qla_tgt->notify_ack_expected++;
1111 pkt->entry_type = NOTIFY_ACK_TYPE;
1112 pkt->entry_count = 1;
1114 nack = (struct nack_to_isp *)pkt;
1115 nack->ox_id = ntfy->ox_id;
1117 nack->u.isp24.nport_handle = ntfy->u.isp24.nport_handle;
1118 if (le16_to_cpu(ntfy->u.isp24.status) == IMM_NTFY_ELS) {
1119 nack->u.isp24.flags = ntfy->u.isp24.flags &
1120 __constant_cpu_to_le32(NOTIFY24XX_FLAGS_PUREX_IOCB);
1122 nack->u.isp24.srr_rx_id = ntfy->u.isp24.srr_rx_id;
1123 nack->u.isp24.status = ntfy->u.isp24.status;
1124 nack->u.isp24.status_subcode = ntfy->u.isp24.status_subcode;
1125 nack->u.isp24.exchange_address = ntfy->u.isp24.exchange_address;
1126 nack->u.isp24.srr_rel_offs = ntfy->u.isp24.srr_rel_offs;
1127 nack->u.isp24.srr_ui = ntfy->u.isp24.srr_ui;
1128 nack->u.isp24.srr_flags = cpu_to_le16(srr_flags);
1129 nack->u.isp24.srr_reject_code = srr_reject_code;
1130 nack->u.isp24.srr_reject_code_expl = srr_explan;
1131 nack->u.isp24.vp_index = ntfy->u.isp24.vp_index;
1133 ql_dbg(ql_dbg_tgt, vha, 0xe005,
1134 "qla_target(%d): Sending 24xx Notify Ack %d\n",
1135 vha->vp_idx, nack->u.isp24.status);
1137 qla2x00_start_iocbs(vha, vha->req);
1141 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
1143 static void qlt_24xx_send_abts_resp(struct scsi_qla_host *vha,
1144 struct abts_recv_from_24xx *abts, uint32_t status,
1145 bool ids_reversed)
1147 struct qla_hw_data *ha = vha->hw;
1148 struct abts_resp_to_24xx *resp;
1149 uint32_t f_ctl;
1150 uint8_t *p;
1152 ql_dbg(ql_dbg_tgt, vha, 0xe006,
1153 "Sending task mgmt ABTS response (ha=%p, atio=%p, status=%x\n",
1154 ha, abts, status);
1156 /* Send marker if required */
1157 if (qlt_issue_marker(vha, 1) != QLA_SUCCESS)
1158 return;
1160 resp = (struct abts_resp_to_24xx *)qla2x00_alloc_iocbs(vha, NULL);
1161 if (!resp) {
1162 ql_dbg(ql_dbg_tgt, vha, 0xe04a,
1163 "qla_target(%d): %s failed: unable to allocate "
1164 "request packet", vha->vp_idx, __func__);
1165 return;
1168 resp->entry_type = ABTS_RESP_24XX;
1169 resp->entry_count = 1;
1170 resp->nport_handle = abts->nport_handle;
1171 resp->vp_index = vha->vp_idx;
1172 resp->sof_type = abts->sof_type;
1173 resp->exchange_address = abts->exchange_address;
1174 resp->fcp_hdr_le = abts->fcp_hdr_le;
1175 f_ctl = __constant_cpu_to_le32(F_CTL_EXCH_CONTEXT_RESP |
1176 F_CTL_LAST_SEQ | F_CTL_END_SEQ |
1177 F_CTL_SEQ_INITIATIVE);
1178 p = (uint8_t *)&f_ctl;
1179 resp->fcp_hdr_le.f_ctl[0] = *p++;
1180 resp->fcp_hdr_le.f_ctl[1] = *p++;
1181 resp->fcp_hdr_le.f_ctl[2] = *p;
1182 if (ids_reversed) {
1183 resp->fcp_hdr_le.d_id[0] = abts->fcp_hdr_le.d_id[0];
1184 resp->fcp_hdr_le.d_id[1] = abts->fcp_hdr_le.d_id[1];
1185 resp->fcp_hdr_le.d_id[2] = abts->fcp_hdr_le.d_id[2];
1186 resp->fcp_hdr_le.s_id[0] = abts->fcp_hdr_le.s_id[0];
1187 resp->fcp_hdr_le.s_id[1] = abts->fcp_hdr_le.s_id[1];
1188 resp->fcp_hdr_le.s_id[2] = abts->fcp_hdr_le.s_id[2];
1189 } else {
1190 resp->fcp_hdr_le.d_id[0] = abts->fcp_hdr_le.s_id[0];
1191 resp->fcp_hdr_le.d_id[1] = abts->fcp_hdr_le.s_id[1];
1192 resp->fcp_hdr_le.d_id[2] = abts->fcp_hdr_le.s_id[2];
1193 resp->fcp_hdr_le.s_id[0] = abts->fcp_hdr_le.d_id[0];
1194 resp->fcp_hdr_le.s_id[1] = abts->fcp_hdr_le.d_id[1];
1195 resp->fcp_hdr_le.s_id[2] = abts->fcp_hdr_le.d_id[2];
1197 resp->exchange_addr_to_abort = abts->exchange_addr_to_abort;
1198 if (status == FCP_TMF_CMPL) {
1199 resp->fcp_hdr_le.r_ctl = R_CTL_BASIC_LINK_SERV | R_CTL_B_ACC;
1200 resp->payload.ba_acct.seq_id_valid = SEQ_ID_INVALID;
1201 resp->payload.ba_acct.low_seq_cnt = 0x0000;
1202 resp->payload.ba_acct.high_seq_cnt = 0xFFFF;
1203 resp->payload.ba_acct.ox_id = abts->fcp_hdr_le.ox_id;
1204 resp->payload.ba_acct.rx_id = abts->fcp_hdr_le.rx_id;
1205 } else {
1206 resp->fcp_hdr_le.r_ctl = R_CTL_BASIC_LINK_SERV | R_CTL_B_RJT;
1207 resp->payload.ba_rjt.reason_code =
1208 BA_RJT_REASON_CODE_UNABLE_TO_PERFORM;
1209 /* Other bytes are zero */
1212 ha->tgt.qla_tgt->abts_resp_expected++;
1214 qla2x00_start_iocbs(vha, vha->req);
1218 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
1220 static void qlt_24xx_retry_term_exchange(struct scsi_qla_host *vha,
1221 struct abts_resp_from_24xx_fw *entry)
1223 struct ctio7_to_24xx *ctio;
1225 ql_dbg(ql_dbg_tgt, vha, 0xe007,
1226 "Sending retry TERM EXCH CTIO7 (ha=%p)\n", vha->hw);
1227 /* Send marker if required */
1228 if (qlt_issue_marker(vha, 1) != QLA_SUCCESS)
1229 return;
1231 ctio = (struct ctio7_to_24xx *)qla2x00_alloc_iocbs(vha, NULL);
1232 if (ctio == NULL) {
1233 ql_dbg(ql_dbg_tgt, vha, 0xe04b,
1234 "qla_target(%d): %s failed: unable to allocate "
1235 "request packet\n", vha->vp_idx, __func__);
1236 return;
1240 * We've got on entrance firmware's response on by us generated
1241 * ABTS response. So, in it ID fields are reversed.
1244 ctio->entry_type = CTIO_TYPE7;
1245 ctio->entry_count = 1;
1246 ctio->nport_handle = entry->nport_handle;
1247 ctio->handle = QLA_TGT_SKIP_HANDLE | CTIO_COMPLETION_HANDLE_MARK;
1248 ctio->timeout = __constant_cpu_to_le16(QLA_TGT_TIMEOUT);
1249 ctio->vp_index = vha->vp_idx;
1250 ctio->initiator_id[0] = entry->fcp_hdr_le.d_id[0];
1251 ctio->initiator_id[1] = entry->fcp_hdr_le.d_id[1];
1252 ctio->initiator_id[2] = entry->fcp_hdr_le.d_id[2];
1253 ctio->exchange_addr = entry->exchange_addr_to_abort;
1254 ctio->u.status1.flags =
1255 __constant_cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_1 |
1256 CTIO7_FLAGS_TERMINATE);
1257 ctio->u.status1.ox_id = entry->fcp_hdr_le.ox_id;
1259 qla2x00_start_iocbs(vha, vha->req);
1261 qlt_24xx_send_abts_resp(vha, (struct abts_recv_from_24xx *)entry,
1262 FCP_TMF_CMPL, true);
1265 /* ha->hardware_lock supposed to be held on entry */
1266 static int __qlt_24xx_handle_abts(struct scsi_qla_host *vha,
1267 struct abts_recv_from_24xx *abts, struct qla_tgt_sess *sess)
1269 struct qla_hw_data *ha = vha->hw;
1270 struct qla_tgt_mgmt_cmd *mcmd;
1271 int rc;
1273 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00f,
1274 "qla_target(%d): task abort (tag=%d)\n",
1275 vha->vp_idx, abts->exchange_addr_to_abort);
1277 mcmd = mempool_alloc(qla_tgt_mgmt_cmd_mempool, GFP_ATOMIC);
1278 if (mcmd == NULL) {
1279 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf051,
1280 "qla_target(%d): %s: Allocation of ABORT cmd failed",
1281 vha->vp_idx, __func__);
1282 return -ENOMEM;
1284 memset(mcmd, 0, sizeof(*mcmd));
1286 mcmd->sess = sess;
1287 memcpy(&mcmd->orig_iocb.abts, abts, sizeof(mcmd->orig_iocb.abts));
1289 rc = ha->tgt.tgt_ops->handle_tmr(mcmd, 0, TMR_ABORT_TASK,
1290 abts->exchange_addr_to_abort);
1291 if (rc != 0) {
1292 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf052,
1293 "qla_target(%d): tgt_ops->handle_tmr()"
1294 " failed: %d", vha->vp_idx, rc);
1295 mempool_free(mcmd, qla_tgt_mgmt_cmd_mempool);
1296 return -EFAULT;
1299 return 0;
1303 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
1305 static void qlt_24xx_handle_abts(struct scsi_qla_host *vha,
1306 struct abts_recv_from_24xx *abts)
1308 struct qla_hw_data *ha = vha->hw;
1309 struct qla_tgt_sess *sess;
1310 uint32_t tag = abts->exchange_addr_to_abort;
1311 uint8_t s_id[3];
1312 int rc;
1314 if (le32_to_cpu(abts->fcp_hdr_le.parameter) & ABTS_PARAM_ABORT_SEQ) {
1315 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf053,
1316 "qla_target(%d): ABTS: Abort Sequence not "
1317 "supported\n", vha->vp_idx);
1318 qlt_24xx_send_abts_resp(vha, abts, FCP_TMF_REJECTED, false);
1319 return;
1322 if (tag == ATIO_EXCHANGE_ADDRESS_UNKNOWN) {
1323 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf010,
1324 "qla_target(%d): ABTS: Unknown Exchange "
1325 "Address received\n", vha->vp_idx);
1326 qlt_24xx_send_abts_resp(vha, abts, FCP_TMF_REJECTED, false);
1327 return;
1330 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf011,
1331 "qla_target(%d): task abort (s_id=%x:%x:%x, "
1332 "tag=%d, param=%x)\n", vha->vp_idx, abts->fcp_hdr_le.s_id[2],
1333 abts->fcp_hdr_le.s_id[1], abts->fcp_hdr_le.s_id[0], tag,
1334 le32_to_cpu(abts->fcp_hdr_le.parameter));
1336 s_id[0] = abts->fcp_hdr_le.s_id[2];
1337 s_id[1] = abts->fcp_hdr_le.s_id[1];
1338 s_id[2] = abts->fcp_hdr_le.s_id[0];
1340 sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha, s_id);
1341 if (!sess) {
1342 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf012,
1343 "qla_target(%d): task abort for non-existant session\n",
1344 vha->vp_idx);
1345 rc = qlt_sched_sess_work(ha->tgt.qla_tgt,
1346 QLA_TGT_SESS_WORK_ABORT, abts, sizeof(*abts));
1347 if (rc != 0) {
1348 qlt_24xx_send_abts_resp(vha, abts, FCP_TMF_REJECTED,
1349 false);
1351 return;
1354 rc = __qlt_24xx_handle_abts(vha, abts, sess);
1355 if (rc != 0) {
1356 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf054,
1357 "qla_target(%d): __qlt_24xx_handle_abts() failed: %d\n",
1358 vha->vp_idx, rc);
1359 qlt_24xx_send_abts_resp(vha, abts, FCP_TMF_REJECTED, false);
1360 return;
1365 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
1367 static void qlt_24xx_send_task_mgmt_ctio(struct scsi_qla_host *ha,
1368 struct qla_tgt_mgmt_cmd *mcmd, uint32_t resp_code)
1370 struct atio_from_isp *atio = &mcmd->orig_iocb.atio;
1371 struct ctio7_to_24xx *ctio;
1373 ql_dbg(ql_dbg_tgt, ha, 0xe008,
1374 "Sending task mgmt CTIO7 (ha=%p, atio=%p, resp_code=%x\n",
1375 ha, atio, resp_code);
1377 /* Send marker if required */
1378 if (qlt_issue_marker(ha, 1) != QLA_SUCCESS)
1379 return;
1381 ctio = (struct ctio7_to_24xx *)qla2x00_alloc_iocbs(ha, NULL);
1382 if (ctio == NULL) {
1383 ql_dbg(ql_dbg_tgt, ha, 0xe04c,
1384 "qla_target(%d): %s failed: unable to allocate "
1385 "request packet\n", ha->vp_idx, __func__);
1386 return;
1389 ctio->entry_type = CTIO_TYPE7;
1390 ctio->entry_count = 1;
1391 ctio->handle = QLA_TGT_SKIP_HANDLE | CTIO_COMPLETION_HANDLE_MARK;
1392 ctio->nport_handle = mcmd->sess->loop_id;
1393 ctio->timeout = __constant_cpu_to_le16(QLA_TGT_TIMEOUT);
1394 ctio->vp_index = ha->vp_idx;
1395 ctio->initiator_id[0] = atio->u.isp24.fcp_hdr.s_id[2];
1396 ctio->initiator_id[1] = atio->u.isp24.fcp_hdr.s_id[1];
1397 ctio->initiator_id[2] = atio->u.isp24.fcp_hdr.s_id[0];
1398 ctio->exchange_addr = atio->u.isp24.exchange_addr;
1399 ctio->u.status1.flags = (atio->u.isp24.attr << 9) |
1400 __constant_cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_1 |
1401 CTIO7_FLAGS_SEND_STATUS);
1402 ctio->u.status1.ox_id = swab16(atio->u.isp24.fcp_hdr.ox_id);
1403 ctio->u.status1.scsi_status =
1404 __constant_cpu_to_le16(SS_RESPONSE_INFO_LEN_VALID);
1405 ctio->u.status1.response_len = __constant_cpu_to_le16(8);
1406 ((uint32_t *)ctio->u.status1.sense_data)[0] = cpu_to_be32(resp_code);
1408 qla2x00_start_iocbs(ha, ha->req);
1411 void qlt_free_mcmd(struct qla_tgt_mgmt_cmd *mcmd)
1413 mempool_free(mcmd, qla_tgt_mgmt_cmd_mempool);
1415 EXPORT_SYMBOL(qlt_free_mcmd);
1417 /* callback from target fabric module code */
1418 void qlt_xmit_tm_rsp(struct qla_tgt_mgmt_cmd *mcmd)
1420 struct scsi_qla_host *vha = mcmd->sess->vha;
1421 struct qla_hw_data *ha = vha->hw;
1422 unsigned long flags;
1424 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf013,
1425 "TM response mcmd (%p) status %#x state %#x",
1426 mcmd, mcmd->fc_tm_rsp, mcmd->flags);
1428 spin_lock_irqsave(&ha->hardware_lock, flags);
1429 if (mcmd->flags == QLA24XX_MGMT_SEND_NACK)
1430 qlt_send_notify_ack(vha, &mcmd->orig_iocb.imm_ntfy,
1431 0, 0, 0, 0, 0, 0);
1432 else {
1433 if (mcmd->se_cmd.se_tmr_req->function == TMR_ABORT_TASK)
1434 qlt_24xx_send_abts_resp(vha, &mcmd->orig_iocb.abts,
1435 mcmd->fc_tm_rsp, false);
1436 else
1437 qlt_24xx_send_task_mgmt_ctio(vha, mcmd,
1438 mcmd->fc_tm_rsp);
1441 * Make the callback for ->free_mcmd() to queue_work() and invoke
1442 * target_put_sess_cmd() to drop cmd_kref to 1. The final
1443 * target_put_sess_cmd() call will be made from TFO->check_stop_free()
1444 * -> tcm_qla2xxx_check_stop_free() to release the TMR associated se_cmd
1445 * descriptor after TFO->queue_tm_rsp() -> tcm_qla2xxx_queue_tm_rsp() ->
1446 * qlt_xmit_tm_rsp() returns here..
1448 ha->tgt.tgt_ops->free_mcmd(mcmd);
1449 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1451 EXPORT_SYMBOL(qlt_xmit_tm_rsp);
1453 /* No locks */
1454 static int qlt_pci_map_calc_cnt(struct qla_tgt_prm *prm)
1456 struct qla_tgt_cmd *cmd = prm->cmd;
1458 BUG_ON(cmd->sg_cnt == 0);
1460 prm->sg = (struct scatterlist *)cmd->sg;
1461 prm->seg_cnt = pci_map_sg(prm->tgt->ha->pdev, cmd->sg,
1462 cmd->sg_cnt, cmd->dma_data_direction);
1463 if (unlikely(prm->seg_cnt == 0))
1464 goto out_err;
1466 prm->cmd->sg_mapped = 1;
1469 * If greater than four sg entries then we need to allocate
1470 * the continuation entries
1472 if (prm->seg_cnt > prm->tgt->datasegs_per_cmd)
1473 prm->req_cnt += DIV_ROUND_UP(prm->seg_cnt -
1474 prm->tgt->datasegs_per_cmd, prm->tgt->datasegs_per_cont);
1476 ql_dbg(ql_dbg_tgt, prm->cmd->vha, 0xe009, "seg_cnt=%d, req_cnt=%d\n",
1477 prm->seg_cnt, prm->req_cnt);
1478 return 0;
1480 out_err:
1481 ql_dbg(ql_dbg_tgt, prm->cmd->vha, 0xe04d,
1482 "qla_target(%d): PCI mapping failed: sg_cnt=%d",
1483 0, prm->cmd->sg_cnt);
1484 return -1;
1487 static inline void qlt_unmap_sg(struct scsi_qla_host *vha,
1488 struct qla_tgt_cmd *cmd)
1490 struct qla_hw_data *ha = vha->hw;
1492 BUG_ON(!cmd->sg_mapped);
1493 pci_unmap_sg(ha->pdev, cmd->sg, cmd->sg_cnt, cmd->dma_data_direction);
1494 cmd->sg_mapped = 0;
1497 static int qlt_check_reserve_free_req(struct scsi_qla_host *vha,
1498 uint32_t req_cnt)
1500 struct qla_hw_data *ha = vha->hw;
1501 device_reg_t __iomem *reg = ha->iobase;
1502 uint32_t cnt;
1504 if (vha->req->cnt < (req_cnt + 2)) {
1505 cnt = (uint16_t)RD_REG_DWORD(&reg->isp24.req_q_out);
1507 ql_dbg(ql_dbg_tgt, vha, 0xe00a,
1508 "Request ring circled: cnt=%d, vha->->ring_index=%d, "
1509 "vha->req->cnt=%d, req_cnt=%d\n", cnt,
1510 vha->req->ring_index, vha->req->cnt, req_cnt);
1511 if (vha->req->ring_index < cnt)
1512 vha->req->cnt = cnt - vha->req->ring_index;
1513 else
1514 vha->req->cnt = vha->req->length -
1515 (vha->req->ring_index - cnt);
1518 if (unlikely(vha->req->cnt < (req_cnt + 2))) {
1519 ql_dbg(ql_dbg_tgt, vha, 0xe00b,
1520 "qla_target(%d): There is no room in the "
1521 "request ring: vha->req->ring_index=%d, vha->req->cnt=%d, "
1522 "req_cnt=%d\n", vha->vp_idx, vha->req->ring_index,
1523 vha->req->cnt, req_cnt);
1524 return -EAGAIN;
1526 vha->req->cnt -= req_cnt;
1528 return 0;
1532 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
1534 static inline void *qlt_get_req_pkt(struct scsi_qla_host *vha)
1536 /* Adjust ring index. */
1537 vha->req->ring_index++;
1538 if (vha->req->ring_index == vha->req->length) {
1539 vha->req->ring_index = 0;
1540 vha->req->ring_ptr = vha->req->ring;
1541 } else {
1542 vha->req->ring_ptr++;
1544 return (cont_entry_t *)vha->req->ring_ptr;
1547 /* ha->hardware_lock supposed to be held on entry */
1548 static inline uint32_t qlt_make_handle(struct scsi_qla_host *vha)
1550 struct qla_hw_data *ha = vha->hw;
1551 uint32_t h;
1553 h = ha->tgt.current_handle;
1554 /* always increment cmd handle */
1555 do {
1556 ++h;
1557 if (h > MAX_OUTSTANDING_COMMANDS)
1558 h = 1; /* 0 is QLA_TGT_NULL_HANDLE */
1559 if (h == ha->tgt.current_handle) {
1560 ql_dbg(ql_dbg_tgt, vha, 0xe04e,
1561 "qla_target(%d): Ran out of "
1562 "empty cmd slots in ha %p\n", vha->vp_idx, ha);
1563 h = QLA_TGT_NULL_HANDLE;
1564 break;
1566 } while ((h == QLA_TGT_NULL_HANDLE) ||
1567 (h == QLA_TGT_SKIP_HANDLE) ||
1568 (ha->tgt.cmds[h-1] != NULL));
1570 if (h != QLA_TGT_NULL_HANDLE)
1571 ha->tgt.current_handle = h;
1573 return h;
1576 /* ha->hardware_lock supposed to be held on entry */
1577 static int qlt_24xx_build_ctio_pkt(struct qla_tgt_prm *prm,
1578 struct scsi_qla_host *vha)
1580 uint32_t h;
1581 struct ctio7_to_24xx *pkt;
1582 struct qla_hw_data *ha = vha->hw;
1583 struct atio_from_isp *atio = &prm->cmd->atio;
1585 pkt = (struct ctio7_to_24xx *)vha->req->ring_ptr;
1586 prm->pkt = pkt;
1587 memset(pkt, 0, sizeof(*pkt));
1589 pkt->entry_type = CTIO_TYPE7;
1590 pkt->entry_count = (uint8_t)prm->req_cnt;
1591 pkt->vp_index = vha->vp_idx;
1593 h = qlt_make_handle(vha);
1594 if (unlikely(h == QLA_TGT_NULL_HANDLE)) {
1596 * CTIO type 7 from the firmware doesn't provide a way to
1597 * know the initiator's LOOP ID, hence we can't find
1598 * the session and, so, the command.
1600 return -EAGAIN;
1601 } else
1602 ha->tgt.cmds[h-1] = prm->cmd;
1604 pkt->handle = h | CTIO_COMPLETION_HANDLE_MARK;
1605 pkt->nport_handle = prm->cmd->loop_id;
1606 pkt->timeout = __constant_cpu_to_le16(QLA_TGT_TIMEOUT);
1607 pkt->initiator_id[0] = atio->u.isp24.fcp_hdr.s_id[2];
1608 pkt->initiator_id[1] = atio->u.isp24.fcp_hdr.s_id[1];
1609 pkt->initiator_id[2] = atio->u.isp24.fcp_hdr.s_id[0];
1610 pkt->exchange_addr = atio->u.isp24.exchange_addr;
1611 pkt->u.status0.flags |= (atio->u.isp24.attr << 9);
1612 pkt->u.status0.ox_id = swab16(atio->u.isp24.fcp_hdr.ox_id);
1613 pkt->u.status0.relative_offset = cpu_to_le32(prm->cmd->offset);
1615 ql_dbg(ql_dbg_tgt, vha, 0xe00c,
1616 "qla_target(%d): handle(cmd) -> %08x, timeout %d, ox_id %#x\n",
1617 vha->vp_idx, pkt->handle, QLA_TGT_TIMEOUT,
1618 le16_to_cpu(pkt->u.status0.ox_id));
1619 return 0;
1623 * ha->hardware_lock supposed to be held on entry. We have already made sure
1624 * that there is sufficient amount of request entries to not drop it.
1626 static void qlt_load_cont_data_segments(struct qla_tgt_prm *prm,
1627 struct scsi_qla_host *vha)
1629 int cnt;
1630 uint32_t *dword_ptr;
1631 int enable_64bit_addressing = prm->tgt->tgt_enable_64bit_addr;
1633 /* Build continuation packets */
1634 while (prm->seg_cnt > 0) {
1635 cont_a64_entry_t *cont_pkt64 =
1636 (cont_a64_entry_t *)qlt_get_req_pkt(vha);
1639 * Make sure that from cont_pkt64 none of
1640 * 64-bit specific fields used for 32-bit
1641 * addressing. Cast to (cont_entry_t *) for
1642 * that.
1645 memset(cont_pkt64, 0, sizeof(*cont_pkt64));
1647 cont_pkt64->entry_count = 1;
1648 cont_pkt64->sys_define = 0;
1650 if (enable_64bit_addressing) {
1651 cont_pkt64->entry_type = CONTINUE_A64_TYPE;
1652 dword_ptr =
1653 (uint32_t *)&cont_pkt64->dseg_0_address;
1654 } else {
1655 cont_pkt64->entry_type = CONTINUE_TYPE;
1656 dword_ptr =
1657 (uint32_t *)&((cont_entry_t *)
1658 cont_pkt64)->dseg_0_address;
1661 /* Load continuation entry data segments */
1662 for (cnt = 0;
1663 cnt < prm->tgt->datasegs_per_cont && prm->seg_cnt;
1664 cnt++, prm->seg_cnt--) {
1665 *dword_ptr++ =
1666 cpu_to_le32(pci_dma_lo32
1667 (sg_dma_address(prm->sg)));
1668 if (enable_64bit_addressing) {
1669 *dword_ptr++ =
1670 cpu_to_le32(pci_dma_hi32
1671 (sg_dma_address
1672 (prm->sg)));
1674 *dword_ptr++ = cpu_to_le32(sg_dma_len(prm->sg));
1676 ql_dbg(ql_dbg_tgt, vha, 0xe00d,
1677 "S/G Segment Cont. phys_addr=%llx:%llx, len=%d\n",
1678 (long long unsigned int)
1679 pci_dma_hi32(sg_dma_address(prm->sg)),
1680 (long long unsigned int)
1681 pci_dma_lo32(sg_dma_address(prm->sg)),
1682 (int)sg_dma_len(prm->sg));
1684 prm->sg = sg_next(prm->sg);
1690 * ha->hardware_lock supposed to be held on entry. We have already made sure
1691 * that there is sufficient amount of request entries to not drop it.
1693 static void qlt_load_data_segments(struct qla_tgt_prm *prm,
1694 struct scsi_qla_host *vha)
1696 int cnt;
1697 uint32_t *dword_ptr;
1698 int enable_64bit_addressing = prm->tgt->tgt_enable_64bit_addr;
1699 struct ctio7_to_24xx *pkt24 = (struct ctio7_to_24xx *)prm->pkt;
1701 ql_dbg(ql_dbg_tgt, vha, 0xe00e,
1702 "iocb->scsi_status=%x, iocb->flags=%x\n",
1703 le16_to_cpu(pkt24->u.status0.scsi_status),
1704 le16_to_cpu(pkt24->u.status0.flags));
1706 pkt24->u.status0.transfer_length = cpu_to_le32(prm->cmd->bufflen);
1708 /* Setup packet address segment pointer */
1709 dword_ptr = pkt24->u.status0.dseg_0_address;
1711 /* Set total data segment count */
1712 if (prm->seg_cnt)
1713 pkt24->dseg_count = cpu_to_le16(prm->seg_cnt);
1715 if (prm->seg_cnt == 0) {
1716 /* No data transfer */
1717 *dword_ptr++ = 0;
1718 *dword_ptr = 0;
1719 return;
1722 /* If scatter gather */
1723 ql_dbg(ql_dbg_tgt, vha, 0xe00f, "%s", "Building S/G data segments...");
1725 /* Load command entry data segments */
1726 for (cnt = 0;
1727 (cnt < prm->tgt->datasegs_per_cmd) && prm->seg_cnt;
1728 cnt++, prm->seg_cnt--) {
1729 *dword_ptr++ =
1730 cpu_to_le32(pci_dma_lo32(sg_dma_address(prm->sg)));
1731 if (enable_64bit_addressing) {
1732 *dword_ptr++ =
1733 cpu_to_le32(pci_dma_hi32(
1734 sg_dma_address(prm->sg)));
1736 *dword_ptr++ = cpu_to_le32(sg_dma_len(prm->sg));
1738 ql_dbg(ql_dbg_tgt, vha, 0xe010,
1739 "S/G Segment phys_addr=%llx:%llx, len=%d\n",
1740 (long long unsigned int)pci_dma_hi32(sg_dma_address(
1741 prm->sg)),
1742 (long long unsigned int)pci_dma_lo32(sg_dma_address(
1743 prm->sg)),
1744 (int)sg_dma_len(prm->sg));
1746 prm->sg = sg_next(prm->sg);
1749 qlt_load_cont_data_segments(prm, vha);
1752 static inline int qlt_has_data(struct qla_tgt_cmd *cmd)
1754 return cmd->bufflen > 0;
1758 * Called without ha->hardware_lock held
1760 static int qlt_pre_xmit_response(struct qla_tgt_cmd *cmd,
1761 struct qla_tgt_prm *prm, int xmit_type, uint8_t scsi_status,
1762 uint32_t *full_req_cnt)
1764 struct qla_tgt *tgt = cmd->tgt;
1765 struct scsi_qla_host *vha = tgt->vha;
1766 struct qla_hw_data *ha = vha->hw;
1767 struct se_cmd *se_cmd = &cmd->se_cmd;
1769 if (unlikely(cmd->aborted)) {
1770 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf014,
1771 "qla_target(%d): terminating exchange "
1772 "for aborted cmd=%p (se_cmd=%p, tag=%d)", vha->vp_idx, cmd,
1773 se_cmd, cmd->tag);
1775 cmd->state = QLA_TGT_STATE_ABORTED;
1777 qlt_send_term_exchange(vha, cmd, &cmd->atio, 0);
1779 /* !! At this point cmd could be already freed !! */
1780 return QLA_TGT_PRE_XMIT_RESP_CMD_ABORTED;
1783 ql_dbg(ql_dbg_tgt, vha, 0xe011, "qla_target(%d): tag=%u\n",
1784 vha->vp_idx, cmd->tag);
1786 prm->cmd = cmd;
1787 prm->tgt = tgt;
1788 prm->rq_result = scsi_status;
1789 prm->sense_buffer = &cmd->sense_buffer[0];
1790 prm->sense_buffer_len = TRANSPORT_SENSE_BUFFER;
1791 prm->sg = NULL;
1792 prm->seg_cnt = -1;
1793 prm->req_cnt = 1;
1794 prm->add_status_pkt = 0;
1796 ql_dbg(ql_dbg_tgt, vha, 0xe012, "rq_result=%x, xmit_type=%x\n",
1797 prm->rq_result, xmit_type);
1799 /* Send marker if required */
1800 if (qlt_issue_marker(vha, 0) != QLA_SUCCESS)
1801 return -EFAULT;
1803 ql_dbg(ql_dbg_tgt, vha, 0xe013, "CTIO start: vha(%d)\n", vha->vp_idx);
1805 if ((xmit_type & QLA_TGT_XMIT_DATA) && qlt_has_data(cmd)) {
1806 if (qlt_pci_map_calc_cnt(prm) != 0)
1807 return -EAGAIN;
1810 *full_req_cnt = prm->req_cnt;
1812 if (se_cmd->se_cmd_flags & SCF_UNDERFLOW_BIT) {
1813 prm->residual = se_cmd->residual_count;
1814 ql_dbg(ql_dbg_tgt, vha, 0xe014,
1815 "Residual underflow: %d (tag %d, "
1816 "op %x, bufflen %d, rq_result %x)\n", prm->residual,
1817 cmd->tag, se_cmd->t_task_cdb ? se_cmd->t_task_cdb[0] : 0,
1818 cmd->bufflen, prm->rq_result);
1819 prm->rq_result |= SS_RESIDUAL_UNDER;
1820 } else if (se_cmd->se_cmd_flags & SCF_OVERFLOW_BIT) {
1821 prm->residual = se_cmd->residual_count;
1822 ql_dbg(ql_dbg_tgt, vha, 0xe015,
1823 "Residual overflow: %d (tag %d, "
1824 "op %x, bufflen %d, rq_result %x)\n", prm->residual,
1825 cmd->tag, se_cmd->t_task_cdb ? se_cmd->t_task_cdb[0] : 0,
1826 cmd->bufflen, prm->rq_result);
1827 prm->rq_result |= SS_RESIDUAL_OVER;
1830 if (xmit_type & QLA_TGT_XMIT_STATUS) {
1832 * If QLA_TGT_XMIT_DATA is not set, add_status_pkt will be
1833 * ignored in *xmit_response() below
1835 if (qlt_has_data(cmd)) {
1836 if (QLA_TGT_SENSE_VALID(prm->sense_buffer) ||
1837 (IS_FWI2_CAPABLE(ha) &&
1838 (prm->rq_result != 0))) {
1839 prm->add_status_pkt = 1;
1840 (*full_req_cnt)++;
1845 ql_dbg(ql_dbg_tgt, vha, 0xe016,
1846 "req_cnt=%d, full_req_cnt=%d, add_status_pkt=%d\n",
1847 prm->req_cnt, *full_req_cnt, prm->add_status_pkt);
1849 return 0;
1852 static inline int qlt_need_explicit_conf(struct qla_hw_data *ha,
1853 struct qla_tgt_cmd *cmd, int sending_sense)
1855 if (ha->tgt.enable_class_2)
1856 return 0;
1858 if (sending_sense)
1859 return cmd->conf_compl_supported;
1860 else
1861 return ha->tgt.enable_explicit_conf &&
1862 cmd->conf_compl_supported;
1865 #ifdef CONFIG_QLA_TGT_DEBUG_SRR
1867 * Original taken from the XFS code
1869 static unsigned long qlt_srr_random(void)
1871 static int Inited;
1872 static unsigned long RandomValue;
1873 static DEFINE_SPINLOCK(lock);
1874 /* cycles pseudo-randomly through all values between 1 and 2^31 - 2 */
1875 register long rv;
1876 register long lo;
1877 register long hi;
1878 unsigned long flags;
1880 spin_lock_irqsave(&lock, flags);
1881 if (!Inited) {
1882 RandomValue = jiffies;
1883 Inited = 1;
1885 rv = RandomValue;
1886 hi = rv / 127773;
1887 lo = rv % 127773;
1888 rv = 16807 * lo - 2836 * hi;
1889 if (rv <= 0)
1890 rv += 2147483647;
1891 RandomValue = rv;
1892 spin_unlock_irqrestore(&lock, flags);
1893 return rv;
1896 static void qlt_check_srr_debug(struct qla_tgt_cmd *cmd, int *xmit_type)
1898 #if 0 /* This is not a real status packets lost, so it won't lead to SRR */
1899 if ((*xmit_type & QLA_TGT_XMIT_STATUS) && (qlt_srr_random() % 200)
1900 == 50) {
1901 *xmit_type &= ~QLA_TGT_XMIT_STATUS;
1902 ql_dbg(ql_dbg_tgt_mgt, cmd->vha, 0xf015,
1903 "Dropping cmd %p (tag %d) status", cmd, cmd->tag);
1905 #endif
1907 * It's currently not possible to simulate SRRs for FCP_WRITE without
1908 * a physical link layer failure, so don't even try here..
1910 if (cmd->dma_data_direction != DMA_FROM_DEVICE)
1911 return;
1913 if (qlt_has_data(cmd) && (cmd->sg_cnt > 1) &&
1914 ((qlt_srr_random() % 100) == 20)) {
1915 int i, leave = 0;
1916 unsigned int tot_len = 0;
1918 while (leave == 0)
1919 leave = qlt_srr_random() % cmd->sg_cnt;
1921 for (i = 0; i < leave; i++)
1922 tot_len += cmd->sg[i].length;
1924 ql_dbg(ql_dbg_tgt_mgt, cmd->vha, 0xf016,
1925 "Cutting cmd %p (tag %d) buffer"
1926 " tail to len %d, sg_cnt %d (cmd->bufflen %d,"
1927 " cmd->sg_cnt %d)", cmd, cmd->tag, tot_len, leave,
1928 cmd->bufflen, cmd->sg_cnt);
1930 cmd->bufflen = tot_len;
1931 cmd->sg_cnt = leave;
1934 if (qlt_has_data(cmd) && ((qlt_srr_random() % 100) == 70)) {
1935 unsigned int offset = qlt_srr_random() % cmd->bufflen;
1937 ql_dbg(ql_dbg_tgt_mgt, cmd->vha, 0xf017,
1938 "Cutting cmd %p (tag %d) buffer head "
1939 "to offset %d (cmd->bufflen %d)", cmd, cmd->tag, offset,
1940 cmd->bufflen);
1941 if (offset == 0)
1942 *xmit_type &= ~QLA_TGT_XMIT_DATA;
1943 else if (qlt_set_data_offset(cmd, offset)) {
1944 ql_dbg(ql_dbg_tgt_mgt, cmd->vha, 0xf018,
1945 "qlt_set_data_offset() failed (tag %d)", cmd->tag);
1949 #else
1950 static inline void qlt_check_srr_debug(struct qla_tgt_cmd *cmd, int *xmit_type)
1952 #endif
1954 static void qlt_24xx_init_ctio_to_isp(struct ctio7_to_24xx *ctio,
1955 struct qla_tgt_prm *prm)
1957 prm->sense_buffer_len = min_t(uint32_t, prm->sense_buffer_len,
1958 (uint32_t)sizeof(ctio->u.status1.sense_data));
1959 ctio->u.status0.flags |=
1960 __constant_cpu_to_le16(CTIO7_FLAGS_SEND_STATUS);
1961 if (qlt_need_explicit_conf(prm->tgt->ha, prm->cmd, 0)) {
1962 ctio->u.status0.flags |= __constant_cpu_to_le16(
1963 CTIO7_FLAGS_EXPLICIT_CONFORM |
1964 CTIO7_FLAGS_CONFORM_REQ);
1966 ctio->u.status0.residual = cpu_to_le32(prm->residual);
1967 ctio->u.status0.scsi_status = cpu_to_le16(prm->rq_result);
1968 if (QLA_TGT_SENSE_VALID(prm->sense_buffer)) {
1969 int i;
1971 if (qlt_need_explicit_conf(prm->tgt->ha, prm->cmd, 1)) {
1972 if (prm->cmd->se_cmd.scsi_status != 0) {
1973 ql_dbg(ql_dbg_tgt, prm->cmd->vha, 0xe017,
1974 "Skipping EXPLICIT_CONFORM and "
1975 "CTIO7_FLAGS_CONFORM_REQ for FCP READ w/ "
1976 "non GOOD status\n");
1977 goto skip_explict_conf;
1979 ctio->u.status1.flags |= __constant_cpu_to_le16(
1980 CTIO7_FLAGS_EXPLICIT_CONFORM |
1981 CTIO7_FLAGS_CONFORM_REQ);
1983 skip_explict_conf:
1984 ctio->u.status1.flags &=
1985 ~__constant_cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_0);
1986 ctio->u.status1.flags |=
1987 __constant_cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_1);
1988 ctio->u.status1.scsi_status |=
1989 __constant_cpu_to_le16(SS_SENSE_LEN_VALID);
1990 ctio->u.status1.sense_length =
1991 cpu_to_le16(prm->sense_buffer_len);
1992 for (i = 0; i < prm->sense_buffer_len/4; i++)
1993 ((uint32_t *)ctio->u.status1.sense_data)[i] =
1994 cpu_to_be32(((uint32_t *)prm->sense_buffer)[i]);
1995 #if 0
1996 if (unlikely((prm->sense_buffer_len % 4) != 0)) {
1997 static int q;
1998 if (q < 10) {
1999 ql_dbg(ql_dbg_tgt, vha, 0xe04f,
2000 "qla_target(%d): %d bytes of sense "
2001 "lost", prm->tgt->ha->vp_idx,
2002 prm->sense_buffer_len % 4);
2003 q++;
2006 #endif
2007 } else {
2008 ctio->u.status1.flags &=
2009 ~__constant_cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_0);
2010 ctio->u.status1.flags |=
2011 __constant_cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_1);
2012 ctio->u.status1.sense_length = 0;
2013 memset(ctio->u.status1.sense_data, 0,
2014 sizeof(ctio->u.status1.sense_data));
2017 /* Sense with len > 24, is it possible ??? */
2021 * Callback to setup response of xmit_type of QLA_TGT_XMIT_DATA and *
2022 * QLA_TGT_XMIT_STATUS for >= 24xx silicon
2024 int qlt_xmit_response(struct qla_tgt_cmd *cmd, int xmit_type,
2025 uint8_t scsi_status)
2027 struct scsi_qla_host *vha = cmd->vha;
2028 struct qla_hw_data *ha = vha->hw;
2029 struct ctio7_to_24xx *pkt;
2030 struct qla_tgt_prm prm;
2031 uint32_t full_req_cnt = 0;
2032 unsigned long flags = 0;
2033 int res;
2035 memset(&prm, 0, sizeof(prm));
2036 qlt_check_srr_debug(cmd, &xmit_type);
2038 ql_dbg(ql_dbg_tgt, cmd->vha, 0xe018,
2039 "is_send_status=%d, cmd->bufflen=%d, cmd->sg_cnt=%d, "
2040 "cmd->dma_data_direction=%d\n", (xmit_type & QLA_TGT_XMIT_STATUS) ?
2041 1 : 0, cmd->bufflen, cmd->sg_cnt, cmd->dma_data_direction);
2043 res = qlt_pre_xmit_response(cmd, &prm, xmit_type, scsi_status,
2044 &full_req_cnt);
2045 if (unlikely(res != 0)) {
2046 if (res == QLA_TGT_PRE_XMIT_RESP_CMD_ABORTED)
2047 return 0;
2049 return res;
2052 spin_lock_irqsave(&ha->hardware_lock, flags);
2054 /* Does F/W have an IOCBs for this request */
2055 res = qlt_check_reserve_free_req(vha, full_req_cnt);
2056 if (unlikely(res))
2057 goto out_unmap_unlock;
2059 res = qlt_24xx_build_ctio_pkt(&prm, vha);
2060 if (unlikely(res != 0))
2061 goto out_unmap_unlock;
2064 pkt = (struct ctio7_to_24xx *)prm.pkt;
2066 if (qlt_has_data(cmd) && (xmit_type & QLA_TGT_XMIT_DATA)) {
2067 pkt->u.status0.flags |=
2068 __constant_cpu_to_le16(CTIO7_FLAGS_DATA_IN |
2069 CTIO7_FLAGS_STATUS_MODE_0);
2071 qlt_load_data_segments(&prm, vha);
2073 if (prm.add_status_pkt == 0) {
2074 if (xmit_type & QLA_TGT_XMIT_STATUS) {
2075 pkt->u.status0.scsi_status =
2076 cpu_to_le16(prm.rq_result);
2077 pkt->u.status0.residual =
2078 cpu_to_le32(prm.residual);
2079 pkt->u.status0.flags |= __constant_cpu_to_le16(
2080 CTIO7_FLAGS_SEND_STATUS);
2081 if (qlt_need_explicit_conf(ha, cmd, 0)) {
2082 pkt->u.status0.flags |=
2083 __constant_cpu_to_le16(
2084 CTIO7_FLAGS_EXPLICIT_CONFORM |
2085 CTIO7_FLAGS_CONFORM_REQ);
2089 } else {
2091 * We have already made sure that there is sufficient
2092 * amount of request entries to not drop HW lock in
2093 * req_pkt().
2095 struct ctio7_to_24xx *ctio =
2096 (struct ctio7_to_24xx *)qlt_get_req_pkt(vha);
2098 ql_dbg(ql_dbg_tgt, vha, 0xe019,
2099 "Building additional status packet\n");
2101 memcpy(ctio, pkt, sizeof(*ctio));
2102 ctio->entry_count = 1;
2103 ctio->dseg_count = 0;
2104 ctio->u.status1.flags &= ~__constant_cpu_to_le16(
2105 CTIO7_FLAGS_DATA_IN);
2107 /* Real finish is ctio_m1's finish */
2108 pkt->handle |= CTIO_INTERMEDIATE_HANDLE_MARK;
2109 pkt->u.status0.flags |= __constant_cpu_to_le16(
2110 CTIO7_FLAGS_DONT_RET_CTIO);
2111 qlt_24xx_init_ctio_to_isp((struct ctio7_to_24xx *)ctio,
2112 &prm);
2113 pr_debug("Status CTIO7: %p\n", ctio);
2115 } else
2116 qlt_24xx_init_ctio_to_isp(pkt, &prm);
2119 cmd->state = QLA_TGT_STATE_PROCESSED; /* Mid-level is done processing */
2121 ql_dbg(ql_dbg_tgt, vha, 0xe01a,
2122 "Xmitting CTIO7 response pkt for 24xx: %p scsi_status: 0x%02x\n",
2123 pkt, scsi_status);
2125 qla2x00_start_iocbs(vha, vha->req);
2126 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2128 return 0;
2130 out_unmap_unlock:
2131 if (cmd->sg_mapped)
2132 qlt_unmap_sg(vha, cmd);
2133 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2135 return res;
2137 EXPORT_SYMBOL(qlt_xmit_response);
2139 int qlt_rdy_to_xfer(struct qla_tgt_cmd *cmd)
2141 struct ctio7_to_24xx *pkt;
2142 struct scsi_qla_host *vha = cmd->vha;
2143 struct qla_hw_data *ha = vha->hw;
2144 struct qla_tgt *tgt = cmd->tgt;
2145 struct qla_tgt_prm prm;
2146 unsigned long flags;
2147 int res = 0;
2149 memset(&prm, 0, sizeof(prm));
2150 prm.cmd = cmd;
2151 prm.tgt = tgt;
2152 prm.sg = NULL;
2153 prm.req_cnt = 1;
2155 /* Send marker if required */
2156 if (qlt_issue_marker(vha, 0) != QLA_SUCCESS)
2157 return -EIO;
2159 ql_dbg(ql_dbg_tgt, vha, 0xe01b, "CTIO_start: vha(%d)",
2160 (int)vha->vp_idx);
2162 /* Calculate number of entries and segments required */
2163 if (qlt_pci_map_calc_cnt(&prm) != 0)
2164 return -EAGAIN;
2166 spin_lock_irqsave(&ha->hardware_lock, flags);
2168 /* Does F/W have an IOCBs for this request */
2169 res = qlt_check_reserve_free_req(vha, prm.req_cnt);
2170 if (res != 0)
2171 goto out_unlock_free_unmap;
2173 res = qlt_24xx_build_ctio_pkt(&prm, vha);
2174 if (unlikely(res != 0))
2175 goto out_unlock_free_unmap;
2176 pkt = (struct ctio7_to_24xx *)prm.pkt;
2177 pkt->u.status0.flags |= __constant_cpu_to_le16(CTIO7_FLAGS_DATA_OUT |
2178 CTIO7_FLAGS_STATUS_MODE_0);
2179 qlt_load_data_segments(&prm, vha);
2181 cmd->state = QLA_TGT_STATE_NEED_DATA;
2183 qla2x00_start_iocbs(vha, vha->req);
2184 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2186 return res;
2188 out_unlock_free_unmap:
2189 if (cmd->sg_mapped)
2190 qlt_unmap_sg(vha, cmd);
2191 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2193 return res;
2195 EXPORT_SYMBOL(qlt_rdy_to_xfer);
2197 /* If hardware_lock held on entry, might drop it, then reaquire */
2198 /* This function sends the appropriate CTIO to ISP 2xxx or 24xx */
2199 static int __qlt_send_term_exchange(struct scsi_qla_host *vha,
2200 struct qla_tgt_cmd *cmd,
2201 struct atio_from_isp *atio)
2203 struct ctio7_to_24xx *ctio24;
2204 struct qla_hw_data *ha = vha->hw;
2205 request_t *pkt;
2206 int ret = 0;
2208 ql_dbg(ql_dbg_tgt, vha, 0xe01c, "Sending TERM EXCH CTIO (ha=%p)\n", ha);
2210 pkt = (request_t *)qla2x00_alloc_iocbs(vha, NULL);
2211 if (pkt == NULL) {
2212 ql_dbg(ql_dbg_tgt, vha, 0xe050,
2213 "qla_target(%d): %s failed: unable to allocate "
2214 "request packet\n", vha->vp_idx, __func__);
2215 return -ENOMEM;
2218 if (cmd != NULL) {
2219 if (cmd->state < QLA_TGT_STATE_PROCESSED) {
2220 ql_dbg(ql_dbg_tgt, vha, 0xe051,
2221 "qla_target(%d): Terminating cmd %p with "
2222 "incorrect state %d\n", vha->vp_idx, cmd,
2223 cmd->state);
2224 } else
2225 ret = 1;
2228 pkt->entry_count = 1;
2229 pkt->handle = QLA_TGT_SKIP_HANDLE | CTIO_COMPLETION_HANDLE_MARK;
2231 ctio24 = (struct ctio7_to_24xx *)pkt;
2232 ctio24->entry_type = CTIO_TYPE7;
2233 ctio24->nport_handle = cmd ? cmd->loop_id : CTIO7_NHANDLE_UNRECOGNIZED;
2234 ctio24->timeout = __constant_cpu_to_le16(QLA_TGT_TIMEOUT);
2235 ctio24->vp_index = vha->vp_idx;
2236 ctio24->initiator_id[0] = atio->u.isp24.fcp_hdr.s_id[2];
2237 ctio24->initiator_id[1] = atio->u.isp24.fcp_hdr.s_id[1];
2238 ctio24->initiator_id[2] = atio->u.isp24.fcp_hdr.s_id[0];
2239 ctio24->exchange_addr = atio->u.isp24.exchange_addr;
2240 ctio24->u.status1.flags = (atio->u.isp24.attr << 9) |
2241 __constant_cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_1 |
2242 CTIO7_FLAGS_TERMINATE);
2243 ctio24->u.status1.ox_id = swab16(atio->u.isp24.fcp_hdr.ox_id);
2245 /* Most likely, it isn't needed */
2246 ctio24->u.status1.residual = get_unaligned((uint32_t *)
2247 &atio->u.isp24.fcp_cmnd.add_cdb[
2248 atio->u.isp24.fcp_cmnd.add_cdb_len]);
2249 if (ctio24->u.status1.residual != 0)
2250 ctio24->u.status1.scsi_status |= SS_RESIDUAL_UNDER;
2252 qla2x00_start_iocbs(vha, vha->req);
2253 return ret;
2256 static void qlt_send_term_exchange(struct scsi_qla_host *vha,
2257 struct qla_tgt_cmd *cmd, struct atio_from_isp *atio, int ha_locked)
2259 unsigned long flags;
2260 int rc;
2262 if (qlt_issue_marker(vha, ha_locked) < 0)
2263 return;
2265 if (ha_locked) {
2266 rc = __qlt_send_term_exchange(vha, cmd, atio);
2267 goto done;
2269 spin_lock_irqsave(&vha->hw->hardware_lock, flags);
2270 rc = __qlt_send_term_exchange(vha, cmd, atio);
2271 spin_unlock_irqrestore(&vha->hw->hardware_lock, flags);
2272 done:
2273 if (rc == 1) {
2274 if (!ha_locked && !in_interrupt())
2275 msleep(250); /* just in case */
2277 vha->hw->tgt.tgt_ops->free_cmd(cmd);
2281 void qlt_free_cmd(struct qla_tgt_cmd *cmd)
2283 BUG_ON(cmd->sg_mapped);
2285 if (unlikely(cmd->free_sg))
2286 kfree(cmd->sg);
2287 kmem_cache_free(qla_tgt_cmd_cachep, cmd);
2289 EXPORT_SYMBOL(qlt_free_cmd);
2291 /* ha->hardware_lock supposed to be held on entry */
2292 static int qlt_prepare_srr_ctio(struct scsi_qla_host *vha,
2293 struct qla_tgt_cmd *cmd, void *ctio)
2295 struct qla_tgt_srr_ctio *sc;
2296 struct qla_hw_data *ha = vha->hw;
2297 struct qla_tgt *tgt = ha->tgt.qla_tgt;
2298 struct qla_tgt_srr_imm *imm;
2300 tgt->ctio_srr_id++;
2302 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf019,
2303 "qla_target(%d): CTIO with SRR status received\n", vha->vp_idx);
2305 if (!ctio) {
2306 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf055,
2307 "qla_target(%d): SRR CTIO, but ctio is NULL\n",
2308 vha->vp_idx);
2309 return -EINVAL;
2312 sc = kzalloc(sizeof(*sc), GFP_ATOMIC);
2313 if (sc != NULL) {
2314 sc->cmd = cmd;
2315 /* IRQ is already OFF */
2316 spin_lock(&tgt->srr_lock);
2317 sc->srr_id = tgt->ctio_srr_id;
2318 list_add_tail(&sc->srr_list_entry,
2319 &tgt->srr_ctio_list);
2320 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01a,
2321 "CTIO SRR %p added (id %d)\n", sc, sc->srr_id);
2322 if (tgt->imm_srr_id == tgt->ctio_srr_id) {
2323 int found = 0;
2324 list_for_each_entry(imm, &tgt->srr_imm_list,
2325 srr_list_entry) {
2326 if (imm->srr_id == sc->srr_id) {
2327 found = 1;
2328 break;
2331 if (found) {
2332 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01b,
2333 "Scheduling srr work\n");
2334 schedule_work(&tgt->srr_work);
2335 } else {
2336 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf056,
2337 "qla_target(%d): imm_srr_id "
2338 "== ctio_srr_id (%d), but there is no "
2339 "corresponding SRR IMM, deleting CTIO "
2340 "SRR %p\n", vha->vp_idx,
2341 tgt->ctio_srr_id, sc);
2342 list_del(&sc->srr_list_entry);
2343 spin_unlock(&tgt->srr_lock);
2345 kfree(sc);
2346 return -EINVAL;
2349 spin_unlock(&tgt->srr_lock);
2350 } else {
2351 struct qla_tgt_srr_imm *ti;
2353 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf057,
2354 "qla_target(%d): Unable to allocate SRR CTIO entry\n",
2355 vha->vp_idx);
2356 spin_lock(&tgt->srr_lock);
2357 list_for_each_entry_safe(imm, ti, &tgt->srr_imm_list,
2358 srr_list_entry) {
2359 if (imm->srr_id == tgt->ctio_srr_id) {
2360 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01c,
2361 "IMM SRR %p deleted (id %d)\n",
2362 imm, imm->srr_id);
2363 list_del(&imm->srr_list_entry);
2364 qlt_reject_free_srr_imm(vha, imm, 1);
2367 spin_unlock(&tgt->srr_lock);
2369 return -ENOMEM;
2372 return 0;
2376 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
2378 static int qlt_term_ctio_exchange(struct scsi_qla_host *vha, void *ctio,
2379 struct qla_tgt_cmd *cmd, uint32_t status)
2381 int term = 0;
2383 if (ctio != NULL) {
2384 struct ctio7_from_24xx *c = (struct ctio7_from_24xx *)ctio;
2385 term = !(c->flags &
2386 __constant_cpu_to_le16(OF_TERM_EXCH));
2387 } else
2388 term = 1;
2390 if (term)
2391 qlt_send_term_exchange(vha, cmd, &cmd->atio, 1);
2393 return term;
2396 /* ha->hardware_lock supposed to be held on entry */
2397 static inline struct qla_tgt_cmd *qlt_get_cmd(struct scsi_qla_host *vha,
2398 uint32_t handle)
2400 struct qla_hw_data *ha = vha->hw;
2402 handle--;
2403 if (ha->tgt.cmds[handle] != NULL) {
2404 struct qla_tgt_cmd *cmd = ha->tgt.cmds[handle];
2405 ha->tgt.cmds[handle] = NULL;
2406 return cmd;
2407 } else
2408 return NULL;
2411 /* ha->hardware_lock supposed to be held on entry */
2412 static struct qla_tgt_cmd *qlt_ctio_to_cmd(struct scsi_qla_host *vha,
2413 uint32_t handle, void *ctio)
2415 struct qla_tgt_cmd *cmd = NULL;
2417 /* Clear out internal marks */
2418 handle &= ~(CTIO_COMPLETION_HANDLE_MARK |
2419 CTIO_INTERMEDIATE_HANDLE_MARK);
2421 if (handle != QLA_TGT_NULL_HANDLE) {
2422 if (unlikely(handle == QLA_TGT_SKIP_HANDLE)) {
2423 ql_dbg(ql_dbg_tgt, vha, 0xe01d, "%s",
2424 "SKIP_HANDLE CTIO\n");
2425 return NULL;
2427 /* handle-1 is actually used */
2428 if (unlikely(handle > MAX_OUTSTANDING_COMMANDS)) {
2429 ql_dbg(ql_dbg_tgt, vha, 0xe052,
2430 "qla_target(%d): Wrong handle %x received\n",
2431 vha->vp_idx, handle);
2432 return NULL;
2434 cmd = qlt_get_cmd(vha, handle);
2435 if (unlikely(cmd == NULL)) {
2436 ql_dbg(ql_dbg_tgt, vha, 0xe053,
2437 "qla_target(%d): Suspicious: unable to "
2438 "find the command with handle %x\n", vha->vp_idx,
2439 handle);
2440 return NULL;
2442 } else if (ctio != NULL) {
2443 /* We can't get loop ID from CTIO7 */
2444 ql_dbg(ql_dbg_tgt, vha, 0xe054,
2445 "qla_target(%d): Wrong CTIO received: QLA24xx doesn't "
2446 "support NULL handles\n", vha->vp_idx);
2447 return NULL;
2450 return cmd;
2454 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
2456 static void qlt_do_ctio_completion(struct scsi_qla_host *vha, uint32_t handle,
2457 uint32_t status, void *ctio)
2459 struct qla_hw_data *ha = vha->hw;
2460 struct se_cmd *se_cmd;
2461 struct target_core_fabric_ops *tfo;
2462 struct qla_tgt_cmd *cmd;
2464 ql_dbg(ql_dbg_tgt, vha, 0xe01e,
2465 "qla_target(%d): handle(ctio %p status %#x) <- %08x\n",
2466 vha->vp_idx, ctio, status, handle);
2468 if (handle & CTIO_INTERMEDIATE_HANDLE_MARK) {
2469 /* That could happen only in case of an error/reset/abort */
2470 if (status != CTIO_SUCCESS) {
2471 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01d,
2472 "Intermediate CTIO received"
2473 " (status %x)\n", status);
2475 return;
2478 cmd = qlt_ctio_to_cmd(vha, handle, ctio);
2479 if (cmd == NULL)
2480 return;
2482 se_cmd = &cmd->se_cmd;
2483 tfo = se_cmd->se_tfo;
2485 if (cmd->sg_mapped)
2486 qlt_unmap_sg(vha, cmd);
2488 if (unlikely(status != CTIO_SUCCESS)) {
2489 switch (status & 0xFFFF) {
2490 case CTIO_LIP_RESET:
2491 case CTIO_TARGET_RESET:
2492 case CTIO_ABORTED:
2493 case CTIO_TIMEOUT:
2494 case CTIO_INVALID_RX_ID:
2495 /* They are OK */
2496 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf058,
2497 "qla_target(%d): CTIO with "
2498 "status %#x received, state %x, se_cmd %p, "
2499 "(LIP_RESET=e, ABORTED=2, TARGET_RESET=17, "
2500 "TIMEOUT=b, INVALID_RX_ID=8)\n", vha->vp_idx,
2501 status, cmd->state, se_cmd);
2502 break;
2504 case CTIO_PORT_LOGGED_OUT:
2505 case CTIO_PORT_UNAVAILABLE:
2506 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf059,
2507 "qla_target(%d): CTIO with PORT LOGGED "
2508 "OUT (29) or PORT UNAVAILABLE (28) status %x "
2509 "received (state %x, se_cmd %p)\n", vha->vp_idx,
2510 status, cmd->state, se_cmd);
2511 break;
2513 case CTIO_SRR_RECEIVED:
2514 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05a,
2515 "qla_target(%d): CTIO with SRR_RECEIVED"
2516 " status %x received (state %x, se_cmd %p)\n",
2517 vha->vp_idx, status, cmd->state, se_cmd);
2518 if (qlt_prepare_srr_ctio(vha, cmd, ctio) != 0)
2519 break;
2520 else
2521 return;
2523 default:
2524 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05b,
2525 "qla_target(%d): CTIO with error status "
2526 "0x%x received (state %x, se_cmd %p\n",
2527 vha->vp_idx, status, cmd->state, se_cmd);
2528 break;
2531 if (cmd->state != QLA_TGT_STATE_NEED_DATA)
2532 if (qlt_term_ctio_exchange(vha, ctio, cmd, status))
2533 return;
2536 if (cmd->state == QLA_TGT_STATE_PROCESSED) {
2537 ql_dbg(ql_dbg_tgt, vha, 0xe01f, "Command %p finished\n", cmd);
2538 } else if (cmd->state == QLA_TGT_STATE_NEED_DATA) {
2539 int rx_status = 0;
2541 cmd->state = QLA_TGT_STATE_DATA_IN;
2543 if (unlikely(status != CTIO_SUCCESS))
2544 rx_status = -EIO;
2545 else
2546 cmd->write_data_transferred = 1;
2548 ql_dbg(ql_dbg_tgt, vha, 0xe020,
2549 "Data received, context %x, rx_status %d\n",
2550 0x0, rx_status);
2552 ha->tgt.tgt_ops->handle_data(cmd);
2553 return;
2554 } else if (cmd->state == QLA_TGT_STATE_ABORTED) {
2555 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01e,
2556 "Aborted command %p (tag %d) finished\n", cmd, cmd->tag);
2557 } else {
2558 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05c,
2559 "qla_target(%d): A command in state (%d) should "
2560 "not return a CTIO complete\n", vha->vp_idx, cmd->state);
2563 if (unlikely(status != CTIO_SUCCESS)) {
2564 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01f, "Finishing failed CTIO\n");
2565 dump_stack();
2568 ha->tgt.tgt_ops->free_cmd(cmd);
2571 /* ha->hardware_lock supposed to be held on entry */
2572 /* called via callback from qla2xxx */
2573 void qlt_ctio_completion(struct scsi_qla_host *vha, uint32_t handle)
2575 struct qla_hw_data *ha = vha->hw;
2576 struct qla_tgt *tgt = ha->tgt.qla_tgt;
2578 if (likely(tgt == NULL)) {
2579 ql_dbg(ql_dbg_tgt, vha, 0xe021,
2580 "CTIO, but target mode not enabled"
2581 " (ha %d %p handle %#x)", vha->vp_idx, ha, handle);
2582 return;
2585 tgt->irq_cmd_count++;
2586 qlt_do_ctio_completion(vha, handle, CTIO_SUCCESS, NULL);
2587 tgt->irq_cmd_count--;
2590 static inline int qlt_get_fcp_task_attr(struct scsi_qla_host *vha,
2591 uint8_t task_codes)
2593 int fcp_task_attr;
2595 switch (task_codes) {
2596 case ATIO_SIMPLE_QUEUE:
2597 fcp_task_attr = MSG_SIMPLE_TAG;
2598 break;
2599 case ATIO_HEAD_OF_QUEUE:
2600 fcp_task_attr = MSG_HEAD_TAG;
2601 break;
2602 case ATIO_ORDERED_QUEUE:
2603 fcp_task_attr = MSG_ORDERED_TAG;
2604 break;
2605 case ATIO_ACA_QUEUE:
2606 fcp_task_attr = MSG_ACA_TAG;
2607 break;
2608 case ATIO_UNTAGGED:
2609 fcp_task_attr = MSG_SIMPLE_TAG;
2610 break;
2611 default:
2612 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05d,
2613 "qla_target: unknown task code %x, use ORDERED instead\n",
2614 task_codes);
2615 fcp_task_attr = MSG_ORDERED_TAG;
2616 break;
2619 return fcp_task_attr;
2622 static struct qla_tgt_sess *qlt_make_local_sess(struct scsi_qla_host *,
2623 uint8_t *);
2625 * Process context for I/O path into tcm_qla2xxx code
2627 static void qlt_do_work(struct work_struct *work)
2629 struct qla_tgt_cmd *cmd = container_of(work, struct qla_tgt_cmd, work);
2630 scsi_qla_host_t *vha = cmd->vha;
2631 struct qla_hw_data *ha = vha->hw;
2632 struct qla_tgt *tgt = ha->tgt.qla_tgt;
2633 struct qla_tgt_sess *sess = NULL;
2634 struct atio_from_isp *atio = &cmd->atio;
2635 unsigned char *cdb;
2636 unsigned long flags;
2637 uint32_t data_length;
2638 int ret, fcp_task_attr, data_dir, bidi = 0;
2640 if (tgt->tgt_stop)
2641 goto out_term;
2643 spin_lock_irqsave(&ha->hardware_lock, flags);
2644 sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha,
2645 atio->u.isp24.fcp_hdr.s_id);
2646 if (sess) {
2647 if (unlikely(sess->tearing_down)) {
2648 sess = NULL;
2649 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2650 goto out_term;
2651 } else {
2653 * Do the extra kref_get() before dropping
2654 * qla_hw_data->hardware_lock.
2656 kref_get(&sess->se_sess->sess_kref);
2659 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2661 if (unlikely(!sess)) {
2662 uint8_t *s_id = atio->u.isp24.fcp_hdr.s_id;
2664 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf022,
2665 "qla_target(%d): Unable to find wwn login"
2666 " (s_id %x:%x:%x), trying to create it manually\n",
2667 vha->vp_idx, s_id[0], s_id[1], s_id[2]);
2669 if (atio->u.raw.entry_count > 1) {
2670 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf023,
2671 "Dropping multy entry cmd %p\n", cmd);
2672 goto out_term;
2675 mutex_lock(&ha->tgt.tgt_mutex);
2676 sess = qlt_make_local_sess(vha, s_id);
2677 /* sess has an extra creation ref. */
2678 mutex_unlock(&ha->tgt.tgt_mutex);
2680 if (!sess)
2681 goto out_term;
2684 cmd->sess = sess;
2685 cmd->loop_id = sess->loop_id;
2686 cmd->conf_compl_supported = sess->conf_compl_supported;
2688 cdb = &atio->u.isp24.fcp_cmnd.cdb[0];
2689 cmd->tag = atio->u.isp24.exchange_addr;
2690 cmd->unpacked_lun = scsilun_to_int(
2691 (struct scsi_lun *)&atio->u.isp24.fcp_cmnd.lun);
2693 if (atio->u.isp24.fcp_cmnd.rddata &&
2694 atio->u.isp24.fcp_cmnd.wrdata) {
2695 bidi = 1;
2696 data_dir = DMA_TO_DEVICE;
2697 } else if (atio->u.isp24.fcp_cmnd.rddata)
2698 data_dir = DMA_FROM_DEVICE;
2699 else if (atio->u.isp24.fcp_cmnd.wrdata)
2700 data_dir = DMA_TO_DEVICE;
2701 else
2702 data_dir = DMA_NONE;
2704 fcp_task_attr = qlt_get_fcp_task_attr(vha,
2705 atio->u.isp24.fcp_cmnd.task_attr);
2706 data_length = be32_to_cpu(get_unaligned((uint32_t *)
2707 &atio->u.isp24.fcp_cmnd.add_cdb[
2708 atio->u.isp24.fcp_cmnd.add_cdb_len]));
2710 ql_dbg(ql_dbg_tgt, vha, 0xe022,
2711 "qla_target: START qla command: %p lun: 0x%04x (tag %d)\n",
2712 cmd, cmd->unpacked_lun, cmd->tag);
2714 ret = vha->hw->tgt.tgt_ops->handle_cmd(vha, cmd, cdb, data_length,
2715 fcp_task_attr, data_dir, bidi);
2716 if (ret != 0)
2717 goto out_term;
2719 * Drop extra session reference from qla_tgt_handle_cmd_for_atio*(
2721 ha->tgt.tgt_ops->put_sess(sess);
2722 return;
2724 out_term:
2725 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf020, "Terminating work cmd %p", cmd);
2727 * cmd has not sent to target yet, so pass NULL as the second
2728 * argument to qlt_send_term_exchange() and free the memory here.
2730 spin_lock_irqsave(&ha->hardware_lock, flags);
2731 qlt_send_term_exchange(vha, NULL, &cmd->atio, 1);
2732 kmem_cache_free(qla_tgt_cmd_cachep, cmd);
2733 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2734 if (sess)
2735 ha->tgt.tgt_ops->put_sess(sess);
2738 /* ha->hardware_lock supposed to be held on entry */
2739 static int qlt_handle_cmd_for_atio(struct scsi_qla_host *vha,
2740 struct atio_from_isp *atio)
2742 struct qla_hw_data *ha = vha->hw;
2743 struct qla_tgt *tgt = ha->tgt.qla_tgt;
2744 struct qla_tgt_cmd *cmd;
2746 if (unlikely(tgt->tgt_stop)) {
2747 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf021,
2748 "New command while device %p is shutting down\n", tgt);
2749 return -EFAULT;
2752 cmd = kmem_cache_zalloc(qla_tgt_cmd_cachep, GFP_ATOMIC);
2753 if (!cmd) {
2754 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05e,
2755 "qla_target(%d): Allocation of cmd failed\n", vha->vp_idx);
2756 return -ENOMEM;
2759 INIT_LIST_HEAD(&cmd->cmd_list);
2761 memcpy(&cmd->atio, atio, sizeof(*atio));
2762 cmd->state = QLA_TGT_STATE_NEW;
2763 cmd->tgt = ha->tgt.qla_tgt;
2764 cmd->vha = vha;
2766 INIT_WORK(&cmd->work, qlt_do_work);
2767 queue_work(qla_tgt_wq, &cmd->work);
2768 return 0;
2772 /* ha->hardware_lock supposed to be held on entry */
2773 static int qlt_issue_task_mgmt(struct qla_tgt_sess *sess, uint32_t lun,
2774 int fn, void *iocb, int flags)
2776 struct scsi_qla_host *vha = sess->vha;
2777 struct qla_hw_data *ha = vha->hw;
2778 struct qla_tgt_mgmt_cmd *mcmd;
2779 int res;
2780 uint8_t tmr_func;
2782 mcmd = mempool_alloc(qla_tgt_mgmt_cmd_mempool, GFP_ATOMIC);
2783 if (!mcmd) {
2784 ql_dbg(ql_dbg_tgt_tmr, vha, 0x10009,
2785 "qla_target(%d): Allocation of management "
2786 "command failed, some commands and their data could "
2787 "leak\n", vha->vp_idx);
2788 return -ENOMEM;
2790 memset(mcmd, 0, sizeof(*mcmd));
2791 mcmd->sess = sess;
2793 if (iocb) {
2794 memcpy(&mcmd->orig_iocb.imm_ntfy, iocb,
2795 sizeof(mcmd->orig_iocb.imm_ntfy));
2797 mcmd->tmr_func = fn;
2798 mcmd->flags = flags;
2800 switch (fn) {
2801 case QLA_TGT_CLEAR_ACA:
2802 ql_dbg(ql_dbg_tgt_tmr, vha, 0x10000,
2803 "qla_target(%d): CLEAR_ACA received\n", sess->vha->vp_idx);
2804 tmr_func = TMR_CLEAR_ACA;
2805 break;
2807 case QLA_TGT_TARGET_RESET:
2808 ql_dbg(ql_dbg_tgt_tmr, vha, 0x10001,
2809 "qla_target(%d): TARGET_RESET received\n",
2810 sess->vha->vp_idx);
2811 tmr_func = TMR_TARGET_WARM_RESET;
2812 break;
2814 case QLA_TGT_LUN_RESET:
2815 ql_dbg(ql_dbg_tgt_tmr, vha, 0x10002,
2816 "qla_target(%d): LUN_RESET received\n", sess->vha->vp_idx);
2817 tmr_func = TMR_LUN_RESET;
2818 break;
2820 case QLA_TGT_CLEAR_TS:
2821 ql_dbg(ql_dbg_tgt_tmr, vha, 0x10003,
2822 "qla_target(%d): CLEAR_TS received\n", sess->vha->vp_idx);
2823 tmr_func = TMR_CLEAR_TASK_SET;
2824 break;
2826 case QLA_TGT_ABORT_TS:
2827 ql_dbg(ql_dbg_tgt_tmr, vha, 0x10004,
2828 "qla_target(%d): ABORT_TS received\n", sess->vha->vp_idx);
2829 tmr_func = TMR_ABORT_TASK_SET;
2830 break;
2831 #if 0
2832 case QLA_TGT_ABORT_ALL:
2833 ql_dbg(ql_dbg_tgt_tmr, vha, 0x10005,
2834 "qla_target(%d): Doing ABORT_ALL_TASKS\n",
2835 sess->vha->vp_idx);
2836 tmr_func = 0;
2837 break;
2839 case QLA_TGT_ABORT_ALL_SESS:
2840 ql_dbg(ql_dbg_tgt_tmr, vha, 0x10006,
2841 "qla_target(%d): Doing ABORT_ALL_TASKS_SESS\n",
2842 sess->vha->vp_idx);
2843 tmr_func = 0;
2844 break;
2846 case QLA_TGT_NEXUS_LOSS_SESS:
2847 ql_dbg(ql_dbg_tgt_tmr, vha, 0x10007,
2848 "qla_target(%d): Doing NEXUS_LOSS_SESS\n",
2849 sess->vha->vp_idx);
2850 tmr_func = 0;
2851 break;
2853 case QLA_TGT_NEXUS_LOSS:
2854 ql_dbg(ql_dbg_tgt_tmr, vha, 0x10008,
2855 "qla_target(%d): Doing NEXUS_LOSS\n", sess->vha->vp_idx);
2856 tmr_func = 0;
2857 break;
2858 #endif
2859 default:
2860 ql_dbg(ql_dbg_tgt_tmr, vha, 0x1000a,
2861 "qla_target(%d): Unknown task mgmt fn 0x%x\n",
2862 sess->vha->vp_idx, fn);
2863 mempool_free(mcmd, qla_tgt_mgmt_cmd_mempool);
2864 return -ENOSYS;
2867 res = ha->tgt.tgt_ops->handle_tmr(mcmd, lun, tmr_func, 0);
2868 if (res != 0) {
2869 ql_dbg(ql_dbg_tgt_tmr, vha, 0x1000b,
2870 "qla_target(%d): tgt.tgt_ops->handle_tmr() failed: %d\n",
2871 sess->vha->vp_idx, res);
2872 mempool_free(mcmd, qla_tgt_mgmt_cmd_mempool);
2873 return -EFAULT;
2876 return 0;
2879 /* ha->hardware_lock supposed to be held on entry */
2880 static int qlt_handle_task_mgmt(struct scsi_qla_host *vha, void *iocb)
2882 struct atio_from_isp *a = (struct atio_from_isp *)iocb;
2883 struct qla_hw_data *ha = vha->hw;
2884 struct qla_tgt *tgt;
2885 struct qla_tgt_sess *sess;
2886 uint32_t lun, unpacked_lun;
2887 int lun_size, fn;
2889 tgt = ha->tgt.qla_tgt;
2891 lun = a->u.isp24.fcp_cmnd.lun;
2892 lun_size = sizeof(a->u.isp24.fcp_cmnd.lun);
2893 fn = a->u.isp24.fcp_cmnd.task_mgmt_flags;
2894 sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha,
2895 a->u.isp24.fcp_hdr.s_id);
2896 unpacked_lun = scsilun_to_int((struct scsi_lun *)&lun);
2898 if (!sess) {
2899 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf024,
2900 "qla_target(%d): task mgmt fn 0x%x for "
2901 "non-existant session\n", vha->vp_idx, fn);
2902 return qlt_sched_sess_work(tgt, QLA_TGT_SESS_WORK_TM, iocb,
2903 sizeof(struct atio_from_isp));
2906 return qlt_issue_task_mgmt(sess, unpacked_lun, fn, iocb, 0);
2909 /* ha->hardware_lock supposed to be held on entry */
2910 static int __qlt_abort_task(struct scsi_qla_host *vha,
2911 struct imm_ntfy_from_isp *iocb, struct qla_tgt_sess *sess)
2913 struct atio_from_isp *a = (struct atio_from_isp *)iocb;
2914 struct qla_hw_data *ha = vha->hw;
2915 struct qla_tgt_mgmt_cmd *mcmd;
2916 uint32_t lun, unpacked_lun;
2917 int rc;
2919 mcmd = mempool_alloc(qla_tgt_mgmt_cmd_mempool, GFP_ATOMIC);
2920 if (mcmd == NULL) {
2921 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05f,
2922 "qla_target(%d): %s: Allocation of ABORT cmd failed\n",
2923 vha->vp_idx, __func__);
2924 return -ENOMEM;
2926 memset(mcmd, 0, sizeof(*mcmd));
2928 mcmd->sess = sess;
2929 memcpy(&mcmd->orig_iocb.imm_ntfy, iocb,
2930 sizeof(mcmd->orig_iocb.imm_ntfy));
2932 lun = a->u.isp24.fcp_cmnd.lun;
2933 unpacked_lun = scsilun_to_int((struct scsi_lun *)&lun);
2935 rc = ha->tgt.tgt_ops->handle_tmr(mcmd, unpacked_lun, TMR_ABORT_TASK,
2936 le16_to_cpu(iocb->u.isp2x.seq_id));
2937 if (rc != 0) {
2938 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf060,
2939 "qla_target(%d): tgt_ops->handle_tmr() failed: %d\n",
2940 vha->vp_idx, rc);
2941 mempool_free(mcmd, qla_tgt_mgmt_cmd_mempool);
2942 return -EFAULT;
2945 return 0;
2948 /* ha->hardware_lock supposed to be held on entry */
2949 static int qlt_abort_task(struct scsi_qla_host *vha,
2950 struct imm_ntfy_from_isp *iocb)
2952 struct qla_hw_data *ha = vha->hw;
2953 struct qla_tgt_sess *sess;
2954 int loop_id;
2956 loop_id = GET_TARGET_ID(ha, (struct atio_from_isp *)iocb);
2958 sess = ha->tgt.tgt_ops->find_sess_by_loop_id(vha, loop_id);
2959 if (sess == NULL) {
2960 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf025,
2961 "qla_target(%d): task abort for unexisting "
2962 "session\n", vha->vp_idx);
2963 return qlt_sched_sess_work(ha->tgt.qla_tgt,
2964 QLA_TGT_SESS_WORK_ABORT, iocb, sizeof(*iocb));
2967 return __qlt_abort_task(vha, iocb, sess);
2971 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
2973 static int qlt_24xx_handle_els(struct scsi_qla_host *vha,
2974 struct imm_ntfy_from_isp *iocb)
2976 struct qla_hw_data *ha = vha->hw;
2977 int res = 0;
2979 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf026,
2980 "qla_target(%d): Port ID: 0x%02x:%02x:%02x"
2981 " ELS opcode: 0x%02x\n", vha->vp_idx, iocb->u.isp24.port_id[0],
2982 iocb->u.isp24.port_id[1], iocb->u.isp24.port_id[2],
2983 iocb->u.isp24.status_subcode);
2985 switch (iocb->u.isp24.status_subcode) {
2986 case ELS_PLOGI:
2987 case ELS_FLOGI:
2988 case ELS_PRLI:
2989 case ELS_LOGO:
2990 case ELS_PRLO:
2991 res = qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS_SESS);
2992 break;
2993 case ELS_PDISC:
2994 case ELS_ADISC:
2996 struct qla_tgt *tgt = ha->tgt.qla_tgt;
2997 if (tgt->link_reinit_iocb_pending) {
2998 qlt_send_notify_ack(vha, &tgt->link_reinit_iocb,
2999 0, 0, 0, 0, 0, 0);
3000 tgt->link_reinit_iocb_pending = 0;
3002 res = 1; /* send notify ack */
3003 break;
3006 default:
3007 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf061,
3008 "qla_target(%d): Unsupported ELS command %x "
3009 "received\n", vha->vp_idx, iocb->u.isp24.status_subcode);
3010 res = qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS_SESS);
3011 break;
3014 return res;
3017 static int qlt_set_data_offset(struct qla_tgt_cmd *cmd, uint32_t offset)
3019 struct scatterlist *sg, *sgp, *sg_srr, *sg_srr_start = NULL;
3020 size_t first_offset = 0, rem_offset = offset, tmp = 0;
3021 int i, sg_srr_cnt, bufflen = 0;
3023 ql_dbg(ql_dbg_tgt, cmd->vha, 0xe023,
3024 "Entering qla_tgt_set_data_offset: cmd: %p, cmd->sg: %p, "
3025 "cmd->sg_cnt: %u, direction: %d\n",
3026 cmd, cmd->sg, cmd->sg_cnt, cmd->dma_data_direction);
3029 * FIXME: Reject non zero SRR relative offset until we can test
3030 * this code properly.
3032 pr_debug("Rejecting non zero SRR rel_offs: %u\n", offset);
3033 return -1;
3035 if (!cmd->sg || !cmd->sg_cnt) {
3036 ql_dbg(ql_dbg_tgt, cmd->vha, 0xe055,
3037 "Missing cmd->sg or zero cmd->sg_cnt in"
3038 " qla_tgt_set_data_offset\n");
3039 return -EINVAL;
3042 * Walk the current cmd->sg list until we locate the new sg_srr_start
3044 for_each_sg(cmd->sg, sg, cmd->sg_cnt, i) {
3045 ql_dbg(ql_dbg_tgt, cmd->vha, 0xe024,
3046 "sg[%d]: %p page: %p, length: %d, offset: %d\n",
3047 i, sg, sg_page(sg), sg->length, sg->offset);
3049 if ((sg->length + tmp) > offset) {
3050 first_offset = rem_offset;
3051 sg_srr_start = sg;
3052 ql_dbg(ql_dbg_tgt, cmd->vha, 0xe025,
3053 "Found matching sg[%d], using %p as sg_srr_start, "
3054 "and using first_offset: %zu\n", i, sg,
3055 first_offset);
3056 break;
3058 tmp += sg->length;
3059 rem_offset -= sg->length;
3062 if (!sg_srr_start) {
3063 ql_dbg(ql_dbg_tgt, cmd->vha, 0xe056,
3064 "Unable to locate sg_srr_start for offset: %u\n", offset);
3065 return -EINVAL;
3067 sg_srr_cnt = (cmd->sg_cnt - i);
3069 sg_srr = kzalloc(sizeof(struct scatterlist) * sg_srr_cnt, GFP_KERNEL);
3070 if (!sg_srr) {
3071 ql_dbg(ql_dbg_tgt, cmd->vha, 0xe057,
3072 "Unable to allocate sgp\n");
3073 return -ENOMEM;
3075 sg_init_table(sg_srr, sg_srr_cnt);
3076 sgp = &sg_srr[0];
3078 * Walk the remaining list for sg_srr_start, mapping to the newly
3079 * allocated sg_srr taking first_offset into account.
3081 for_each_sg(sg_srr_start, sg, sg_srr_cnt, i) {
3082 if (first_offset) {
3083 sg_set_page(sgp, sg_page(sg),
3084 (sg->length - first_offset), first_offset);
3085 first_offset = 0;
3086 } else {
3087 sg_set_page(sgp, sg_page(sg), sg->length, 0);
3089 bufflen += sgp->length;
3091 sgp = sg_next(sgp);
3092 if (!sgp)
3093 break;
3096 cmd->sg = sg_srr;
3097 cmd->sg_cnt = sg_srr_cnt;
3098 cmd->bufflen = bufflen;
3099 cmd->offset += offset;
3100 cmd->free_sg = 1;
3102 ql_dbg(ql_dbg_tgt, cmd->vha, 0xe026, "New cmd->sg: %p\n", cmd->sg);
3103 ql_dbg(ql_dbg_tgt, cmd->vha, 0xe027, "New cmd->sg_cnt: %u\n",
3104 cmd->sg_cnt);
3105 ql_dbg(ql_dbg_tgt, cmd->vha, 0xe028, "New cmd->bufflen: %u\n",
3106 cmd->bufflen);
3107 ql_dbg(ql_dbg_tgt, cmd->vha, 0xe029, "New cmd->offset: %u\n",
3108 cmd->offset);
3110 if (cmd->sg_cnt < 0)
3111 BUG();
3113 if (cmd->bufflen < 0)
3114 BUG();
3116 return 0;
3119 static inline int qlt_srr_adjust_data(struct qla_tgt_cmd *cmd,
3120 uint32_t srr_rel_offs, int *xmit_type)
3122 int res = 0, rel_offs;
3124 rel_offs = srr_rel_offs - cmd->offset;
3125 ql_dbg(ql_dbg_tgt_mgt, cmd->vha, 0xf027, "srr_rel_offs=%d, rel_offs=%d",
3126 srr_rel_offs, rel_offs);
3128 *xmit_type = QLA_TGT_XMIT_ALL;
3130 if (rel_offs < 0) {
3131 ql_dbg(ql_dbg_tgt_mgt, cmd->vha, 0xf062,
3132 "qla_target(%d): SRR rel_offs (%d) < 0",
3133 cmd->vha->vp_idx, rel_offs);
3134 res = -1;
3135 } else if (rel_offs == cmd->bufflen)
3136 *xmit_type = QLA_TGT_XMIT_STATUS;
3137 else if (rel_offs > 0)
3138 res = qlt_set_data_offset(cmd, rel_offs);
3140 return res;
3143 /* No locks, thread context */
3144 static void qlt_handle_srr(struct scsi_qla_host *vha,
3145 struct qla_tgt_srr_ctio *sctio, struct qla_tgt_srr_imm *imm)
3147 struct imm_ntfy_from_isp *ntfy =
3148 (struct imm_ntfy_from_isp *)&imm->imm_ntfy;
3149 struct qla_hw_data *ha = vha->hw;
3150 struct qla_tgt_cmd *cmd = sctio->cmd;
3151 struct se_cmd *se_cmd = &cmd->se_cmd;
3152 unsigned long flags;
3153 int xmit_type = 0, resp = 0;
3154 uint32_t offset;
3155 uint16_t srr_ui;
3157 offset = le32_to_cpu(ntfy->u.isp24.srr_rel_offs);
3158 srr_ui = ntfy->u.isp24.srr_ui;
3160 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf028, "SRR cmd %p, srr_ui %x\n",
3161 cmd, srr_ui);
3163 switch (srr_ui) {
3164 case SRR_IU_STATUS:
3165 spin_lock_irqsave(&ha->hardware_lock, flags);
3166 qlt_send_notify_ack(vha, ntfy,
3167 0, 0, 0, NOTIFY_ACK_SRR_FLAGS_ACCEPT, 0, 0);
3168 spin_unlock_irqrestore(&ha->hardware_lock, flags);
3169 xmit_type = QLA_TGT_XMIT_STATUS;
3170 resp = 1;
3171 break;
3172 case SRR_IU_DATA_IN:
3173 if (!cmd->sg || !cmd->sg_cnt) {
3174 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf063,
3175 "Unable to process SRR_IU_DATA_IN due to"
3176 " missing cmd->sg, state: %d\n", cmd->state);
3177 dump_stack();
3178 goto out_reject;
3180 if (se_cmd->scsi_status != 0) {
3181 ql_dbg(ql_dbg_tgt, vha, 0xe02a,
3182 "Rejecting SRR_IU_DATA_IN with non GOOD "
3183 "scsi_status\n");
3184 goto out_reject;
3186 cmd->bufflen = se_cmd->data_length;
3188 if (qlt_has_data(cmd)) {
3189 if (qlt_srr_adjust_data(cmd, offset, &xmit_type) != 0)
3190 goto out_reject;
3191 spin_lock_irqsave(&ha->hardware_lock, flags);
3192 qlt_send_notify_ack(vha, ntfy,
3193 0, 0, 0, NOTIFY_ACK_SRR_FLAGS_ACCEPT, 0, 0);
3194 spin_unlock_irqrestore(&ha->hardware_lock, flags);
3195 resp = 1;
3196 } else {
3197 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf064,
3198 "qla_target(%d): SRR for in data for cmd "
3199 "without them (tag %d, SCSI status %d), "
3200 "reject", vha->vp_idx, cmd->tag,
3201 cmd->se_cmd.scsi_status);
3202 goto out_reject;
3204 break;
3205 case SRR_IU_DATA_OUT:
3206 if (!cmd->sg || !cmd->sg_cnt) {
3207 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf065,
3208 "Unable to process SRR_IU_DATA_OUT due to"
3209 " missing cmd->sg\n");
3210 dump_stack();
3211 goto out_reject;
3213 if (se_cmd->scsi_status != 0) {
3214 ql_dbg(ql_dbg_tgt, vha, 0xe02b,
3215 "Rejecting SRR_IU_DATA_OUT"
3216 " with non GOOD scsi_status\n");
3217 goto out_reject;
3219 cmd->bufflen = se_cmd->data_length;
3221 if (qlt_has_data(cmd)) {
3222 if (qlt_srr_adjust_data(cmd, offset, &xmit_type) != 0)
3223 goto out_reject;
3224 spin_lock_irqsave(&ha->hardware_lock, flags);
3225 qlt_send_notify_ack(vha, ntfy,
3226 0, 0, 0, NOTIFY_ACK_SRR_FLAGS_ACCEPT, 0, 0);
3227 spin_unlock_irqrestore(&ha->hardware_lock, flags);
3228 if (xmit_type & QLA_TGT_XMIT_DATA)
3229 qlt_rdy_to_xfer(cmd);
3230 } else {
3231 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf066,
3232 "qla_target(%d): SRR for out data for cmd "
3233 "without them (tag %d, SCSI status %d), "
3234 "reject", vha->vp_idx, cmd->tag,
3235 cmd->se_cmd.scsi_status);
3236 goto out_reject;
3238 break;
3239 default:
3240 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf067,
3241 "qla_target(%d): Unknown srr_ui value %x",
3242 vha->vp_idx, srr_ui);
3243 goto out_reject;
3246 /* Transmit response in case of status and data-in cases */
3247 if (resp)
3248 qlt_xmit_response(cmd, xmit_type, se_cmd->scsi_status);
3250 return;
3252 out_reject:
3253 spin_lock_irqsave(&ha->hardware_lock, flags);
3254 qlt_send_notify_ack(vha, ntfy, 0, 0, 0,
3255 NOTIFY_ACK_SRR_FLAGS_REJECT,
3256 NOTIFY_ACK_SRR_REJECT_REASON_UNABLE_TO_PERFORM,
3257 NOTIFY_ACK_SRR_FLAGS_REJECT_EXPL_NO_EXPL);
3258 if (cmd->state == QLA_TGT_STATE_NEED_DATA) {
3259 cmd->state = QLA_TGT_STATE_DATA_IN;
3260 dump_stack();
3261 } else
3262 qlt_send_term_exchange(vha, cmd, &cmd->atio, 1);
3263 spin_unlock_irqrestore(&ha->hardware_lock, flags);
3266 static void qlt_reject_free_srr_imm(struct scsi_qla_host *vha,
3267 struct qla_tgt_srr_imm *imm, int ha_locked)
3269 struct qla_hw_data *ha = vha->hw;
3270 unsigned long flags = 0;
3272 if (!ha_locked)
3273 spin_lock_irqsave(&ha->hardware_lock, flags);
3275 qlt_send_notify_ack(vha, (void *)&imm->imm_ntfy, 0, 0, 0,
3276 NOTIFY_ACK_SRR_FLAGS_REJECT,
3277 NOTIFY_ACK_SRR_REJECT_REASON_UNABLE_TO_PERFORM,
3278 NOTIFY_ACK_SRR_FLAGS_REJECT_EXPL_NO_EXPL);
3280 if (!ha_locked)
3281 spin_unlock_irqrestore(&ha->hardware_lock, flags);
3283 kfree(imm);
3286 static void qlt_handle_srr_work(struct work_struct *work)
3288 struct qla_tgt *tgt = container_of(work, struct qla_tgt, srr_work);
3289 struct scsi_qla_host *vha = tgt->vha;
3290 struct qla_tgt_srr_ctio *sctio;
3291 unsigned long flags;
3293 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf029, "Entering SRR work (tgt %p)\n",
3294 tgt);
3296 restart:
3297 spin_lock_irqsave(&tgt->srr_lock, flags);
3298 list_for_each_entry(sctio, &tgt->srr_ctio_list, srr_list_entry) {
3299 struct qla_tgt_srr_imm *imm, *i, *ti;
3300 struct qla_tgt_cmd *cmd;
3301 struct se_cmd *se_cmd;
3303 imm = NULL;
3304 list_for_each_entry_safe(i, ti, &tgt->srr_imm_list,
3305 srr_list_entry) {
3306 if (i->srr_id == sctio->srr_id) {
3307 list_del(&i->srr_list_entry);
3308 if (imm) {
3309 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf068,
3310 "qla_target(%d): There must be "
3311 "only one IMM SRR per CTIO SRR "
3312 "(IMM SRR %p, id %d, CTIO %p\n",
3313 vha->vp_idx, i, i->srr_id, sctio);
3314 qlt_reject_free_srr_imm(tgt->vha, i, 0);
3315 } else
3316 imm = i;
3320 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf02a,
3321 "IMM SRR %p, CTIO SRR %p (id %d)\n", imm, sctio,
3322 sctio->srr_id);
3324 if (imm == NULL) {
3325 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf02b,
3326 "Not found matching IMM for SRR CTIO (id %d)\n",
3327 sctio->srr_id);
3328 continue;
3329 } else
3330 list_del(&sctio->srr_list_entry);
3332 spin_unlock_irqrestore(&tgt->srr_lock, flags);
3334 cmd = sctio->cmd;
3336 * Reset qla_tgt_cmd SRR values and SGL pointer+count to follow
3337 * tcm_qla2xxx_write_pending() and tcm_qla2xxx_queue_data_in()
3338 * logic..
3340 cmd->offset = 0;
3341 if (cmd->free_sg) {
3342 kfree(cmd->sg);
3343 cmd->sg = NULL;
3344 cmd->free_sg = 0;
3346 se_cmd = &cmd->se_cmd;
3348 cmd->sg_cnt = se_cmd->t_data_nents;
3349 cmd->sg = se_cmd->t_data_sg;
3351 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf02c,
3352 "SRR cmd %p (se_cmd %p, tag %d, op %x), "
3353 "sg_cnt=%d, offset=%d", cmd, &cmd->se_cmd, cmd->tag,
3354 se_cmd->t_task_cdb[0], cmd->sg_cnt, cmd->offset);
3356 qlt_handle_srr(vha, sctio, imm);
3358 kfree(imm);
3359 kfree(sctio);
3360 goto restart;
3362 spin_unlock_irqrestore(&tgt->srr_lock, flags);
3365 /* ha->hardware_lock supposed to be held on entry */
3366 static void qlt_prepare_srr_imm(struct scsi_qla_host *vha,
3367 struct imm_ntfy_from_isp *iocb)
3369 struct qla_tgt_srr_imm *imm;
3370 struct qla_hw_data *ha = vha->hw;
3371 struct qla_tgt *tgt = ha->tgt.qla_tgt;
3372 struct qla_tgt_srr_ctio *sctio;
3374 tgt->imm_srr_id++;
3376 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf02d, "qla_target(%d): SRR received\n",
3377 vha->vp_idx);
3379 imm = kzalloc(sizeof(*imm), GFP_ATOMIC);
3380 if (imm != NULL) {
3381 memcpy(&imm->imm_ntfy, iocb, sizeof(imm->imm_ntfy));
3383 /* IRQ is already OFF */
3384 spin_lock(&tgt->srr_lock);
3385 imm->srr_id = tgt->imm_srr_id;
3386 list_add_tail(&imm->srr_list_entry,
3387 &tgt->srr_imm_list);
3388 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf02e,
3389 "IMM NTFY SRR %p added (id %d, ui %x)\n",
3390 imm, imm->srr_id, iocb->u.isp24.srr_ui);
3391 if (tgt->imm_srr_id == tgt->ctio_srr_id) {
3392 int found = 0;
3393 list_for_each_entry(sctio, &tgt->srr_ctio_list,
3394 srr_list_entry) {
3395 if (sctio->srr_id == imm->srr_id) {
3396 found = 1;
3397 break;
3400 if (found) {
3401 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf02f, "%s",
3402 "Scheduling srr work\n");
3403 schedule_work(&tgt->srr_work);
3404 } else {
3405 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf030,
3406 "qla_target(%d): imm_srr_id "
3407 "== ctio_srr_id (%d), but there is no "
3408 "corresponding SRR CTIO, deleting IMM "
3409 "SRR %p\n", vha->vp_idx, tgt->ctio_srr_id,
3410 imm);
3411 list_del(&imm->srr_list_entry);
3413 kfree(imm);
3415 spin_unlock(&tgt->srr_lock);
3416 goto out_reject;
3419 spin_unlock(&tgt->srr_lock);
3420 } else {
3421 struct qla_tgt_srr_ctio *ts;
3423 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf069,
3424 "qla_target(%d): Unable to allocate SRR IMM "
3425 "entry, SRR request will be rejected\n", vha->vp_idx);
3427 /* IRQ is already OFF */
3428 spin_lock(&tgt->srr_lock);
3429 list_for_each_entry_safe(sctio, ts, &tgt->srr_ctio_list,
3430 srr_list_entry) {
3431 if (sctio->srr_id == tgt->imm_srr_id) {
3432 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf031,
3433 "CTIO SRR %p deleted (id %d)\n",
3434 sctio, sctio->srr_id);
3435 list_del(&sctio->srr_list_entry);
3436 qlt_send_term_exchange(vha, sctio->cmd,
3437 &sctio->cmd->atio, 1);
3438 kfree(sctio);
3441 spin_unlock(&tgt->srr_lock);
3442 goto out_reject;
3445 return;
3447 out_reject:
3448 qlt_send_notify_ack(vha, iocb, 0, 0, 0,
3449 NOTIFY_ACK_SRR_FLAGS_REJECT,
3450 NOTIFY_ACK_SRR_REJECT_REASON_UNABLE_TO_PERFORM,
3451 NOTIFY_ACK_SRR_FLAGS_REJECT_EXPL_NO_EXPL);
3455 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
3457 static void qlt_handle_imm_notify(struct scsi_qla_host *vha,
3458 struct imm_ntfy_from_isp *iocb)
3460 struct qla_hw_data *ha = vha->hw;
3461 uint32_t add_flags = 0;
3462 int send_notify_ack = 1;
3463 uint16_t status;
3465 status = le16_to_cpu(iocb->u.isp2x.status);
3466 switch (status) {
3467 case IMM_NTFY_LIP_RESET:
3469 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf032,
3470 "qla_target(%d): LIP reset (loop %#x), subcode %x\n",
3471 vha->vp_idx, le16_to_cpu(iocb->u.isp24.nport_handle),
3472 iocb->u.isp24.status_subcode);
3474 if (qlt_reset(vha, iocb, QLA_TGT_ABORT_ALL) == 0)
3475 send_notify_ack = 0;
3476 break;
3479 case IMM_NTFY_LIP_LINK_REINIT:
3481 struct qla_tgt *tgt = ha->tgt.qla_tgt;
3482 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf033,
3483 "qla_target(%d): LINK REINIT (loop %#x, "
3484 "subcode %x)\n", vha->vp_idx,
3485 le16_to_cpu(iocb->u.isp24.nport_handle),
3486 iocb->u.isp24.status_subcode);
3487 if (tgt->link_reinit_iocb_pending) {
3488 qlt_send_notify_ack(vha, &tgt->link_reinit_iocb,
3489 0, 0, 0, 0, 0, 0);
3491 memcpy(&tgt->link_reinit_iocb, iocb, sizeof(*iocb));
3492 tgt->link_reinit_iocb_pending = 1;
3494 * QLogic requires to wait after LINK REINIT for possible
3495 * PDISC or ADISC ELS commands
3497 send_notify_ack = 0;
3498 break;
3501 case IMM_NTFY_PORT_LOGOUT:
3502 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf034,
3503 "qla_target(%d): Port logout (loop "
3504 "%#x, subcode %x)\n", vha->vp_idx,
3505 le16_to_cpu(iocb->u.isp24.nport_handle),
3506 iocb->u.isp24.status_subcode);
3508 if (qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS_SESS) == 0)
3509 send_notify_ack = 0;
3510 /* The sessions will be cleared in the callback, if needed */
3511 break;
3513 case IMM_NTFY_GLBL_TPRLO:
3514 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf035,
3515 "qla_target(%d): Global TPRLO (%x)\n", vha->vp_idx, status);
3516 if (qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS) == 0)
3517 send_notify_ack = 0;
3518 /* The sessions will be cleared in the callback, if needed */
3519 break;
3521 case IMM_NTFY_PORT_CONFIG:
3522 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf036,
3523 "qla_target(%d): Port config changed (%x)\n", vha->vp_idx,
3524 status);
3525 if (qlt_reset(vha, iocb, QLA_TGT_ABORT_ALL) == 0)
3526 send_notify_ack = 0;
3527 /* The sessions will be cleared in the callback, if needed */
3528 break;
3530 case IMM_NTFY_GLBL_LOGO:
3531 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06a,
3532 "qla_target(%d): Link failure detected\n",
3533 vha->vp_idx);
3534 /* I_T nexus loss */
3535 if (qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS) == 0)
3536 send_notify_ack = 0;
3537 break;
3539 case IMM_NTFY_IOCB_OVERFLOW:
3540 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06b,
3541 "qla_target(%d): Cannot provide requested "
3542 "capability (IOCB overflowed the immediate notify "
3543 "resource count)\n", vha->vp_idx);
3544 break;
3546 case IMM_NTFY_ABORT_TASK:
3547 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf037,
3548 "qla_target(%d): Abort Task (S %08x I %#x -> "
3549 "L %#x)\n", vha->vp_idx,
3550 le16_to_cpu(iocb->u.isp2x.seq_id),
3551 GET_TARGET_ID(ha, (struct atio_from_isp *)iocb),
3552 le16_to_cpu(iocb->u.isp2x.lun));
3553 if (qlt_abort_task(vha, iocb) == 0)
3554 send_notify_ack = 0;
3555 break;
3557 case IMM_NTFY_RESOURCE:
3558 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06c,
3559 "qla_target(%d): Out of resources, host %ld\n",
3560 vha->vp_idx, vha->host_no);
3561 break;
3563 case IMM_NTFY_MSG_RX:
3564 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf038,
3565 "qla_target(%d): Immediate notify task %x\n",
3566 vha->vp_idx, iocb->u.isp2x.task_flags);
3567 if (qlt_handle_task_mgmt(vha, iocb) == 0)
3568 send_notify_ack = 0;
3569 break;
3571 case IMM_NTFY_ELS:
3572 if (qlt_24xx_handle_els(vha, iocb) == 0)
3573 send_notify_ack = 0;
3574 break;
3576 case IMM_NTFY_SRR:
3577 qlt_prepare_srr_imm(vha, iocb);
3578 send_notify_ack = 0;
3579 break;
3581 default:
3582 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06d,
3583 "qla_target(%d): Received unknown immediate "
3584 "notify status %x\n", vha->vp_idx, status);
3585 break;
3588 if (send_notify_ack)
3589 qlt_send_notify_ack(vha, iocb, add_flags, 0, 0, 0, 0, 0);
3593 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
3594 * This function sends busy to ISP 2xxx or 24xx.
3596 static void qlt_send_busy(struct scsi_qla_host *vha,
3597 struct atio_from_isp *atio, uint16_t status)
3599 struct ctio7_to_24xx *ctio24;
3600 struct qla_hw_data *ha = vha->hw;
3601 request_t *pkt;
3602 struct qla_tgt_sess *sess = NULL;
3604 sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha,
3605 atio->u.isp24.fcp_hdr.s_id);
3606 if (!sess) {
3607 qlt_send_term_exchange(vha, NULL, atio, 1);
3608 return;
3610 /* Sending marker isn't necessary, since we called from ISR */
3612 pkt = (request_t *)qla2x00_alloc_iocbs(vha, NULL);
3613 if (!pkt) {
3614 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06e,
3615 "qla_target(%d): %s failed: unable to allocate "
3616 "request packet", vha->vp_idx, __func__);
3617 return;
3620 pkt->entry_count = 1;
3621 pkt->handle = QLA_TGT_SKIP_HANDLE | CTIO_COMPLETION_HANDLE_MARK;
3623 ctio24 = (struct ctio7_to_24xx *)pkt;
3624 ctio24->entry_type = CTIO_TYPE7;
3625 ctio24->nport_handle = sess->loop_id;
3626 ctio24->timeout = __constant_cpu_to_le16(QLA_TGT_TIMEOUT);
3627 ctio24->vp_index = vha->vp_idx;
3628 ctio24->initiator_id[0] = atio->u.isp24.fcp_hdr.s_id[2];
3629 ctio24->initiator_id[1] = atio->u.isp24.fcp_hdr.s_id[1];
3630 ctio24->initiator_id[2] = atio->u.isp24.fcp_hdr.s_id[0];
3631 ctio24->exchange_addr = atio->u.isp24.exchange_addr;
3632 ctio24->u.status1.flags = (atio->u.isp24.attr << 9) |
3633 __constant_cpu_to_le16(
3634 CTIO7_FLAGS_STATUS_MODE_1 | CTIO7_FLAGS_SEND_STATUS |
3635 CTIO7_FLAGS_DONT_RET_CTIO);
3637 * CTIO from fw w/o se_cmd doesn't provide enough info to retry it,
3638 * if the explicit conformation is used.
3640 ctio24->u.status1.ox_id = swab16(atio->u.isp24.fcp_hdr.ox_id);
3641 ctio24->u.status1.scsi_status = cpu_to_le16(status);
3642 ctio24->u.status1.residual = get_unaligned((uint32_t *)
3643 &atio->u.isp24.fcp_cmnd.add_cdb[
3644 atio->u.isp24.fcp_cmnd.add_cdb_len]);
3645 if (ctio24->u.status1.residual != 0)
3646 ctio24->u.status1.scsi_status |= SS_RESIDUAL_UNDER;
3648 qla2x00_start_iocbs(vha, vha->req);
3651 /* ha->hardware_lock supposed to be held on entry */
3652 /* called via callback from qla2xxx */
3653 static void qlt_24xx_atio_pkt(struct scsi_qla_host *vha,
3654 struct atio_from_isp *atio)
3656 struct qla_hw_data *ha = vha->hw;
3657 struct qla_tgt *tgt = ha->tgt.qla_tgt;
3658 int rc;
3660 if (unlikely(tgt == NULL)) {
3661 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf039,
3662 "ATIO pkt, but no tgt (ha %p)", ha);
3663 return;
3665 ql_dbg(ql_dbg_tgt, vha, 0xe02c,
3666 "qla_target(%d): ATIO pkt %p: type %02x count %02x",
3667 vha->vp_idx, atio, atio->u.raw.entry_type,
3668 atio->u.raw.entry_count);
3670 * In tgt_stop mode we also should allow all requests to pass.
3671 * Otherwise, some commands can stuck.
3674 tgt->irq_cmd_count++;
3676 switch (atio->u.raw.entry_type) {
3677 case ATIO_TYPE7:
3678 ql_dbg(ql_dbg_tgt, vha, 0xe02d,
3679 "ATIO_TYPE7 instance %d, lun %Lx, read/write %d/%d, "
3680 "add_cdb_len %d, data_length %04x, s_id %x:%x:%x\n",
3681 vha->vp_idx, atio->u.isp24.fcp_cmnd.lun,
3682 atio->u.isp24.fcp_cmnd.rddata,
3683 atio->u.isp24.fcp_cmnd.wrdata,
3684 atio->u.isp24.fcp_cmnd.add_cdb_len,
3685 be32_to_cpu(get_unaligned((uint32_t *)
3686 &atio->u.isp24.fcp_cmnd.add_cdb[
3687 atio->u.isp24.fcp_cmnd.add_cdb_len])),
3688 atio->u.isp24.fcp_hdr.s_id[0],
3689 atio->u.isp24.fcp_hdr.s_id[1],
3690 atio->u.isp24.fcp_hdr.s_id[2]);
3692 if (unlikely(atio->u.isp24.exchange_addr ==
3693 ATIO_EXCHANGE_ADDRESS_UNKNOWN)) {
3694 ql_dbg(ql_dbg_tgt, vha, 0xe058,
3695 "qla_target(%d): ATIO_TYPE7 "
3696 "received with UNKNOWN exchange address, "
3697 "sending QUEUE_FULL\n", vha->vp_idx);
3698 qlt_send_busy(vha, atio, SAM_STAT_TASK_SET_FULL);
3699 break;
3701 if (likely(atio->u.isp24.fcp_cmnd.task_mgmt_flags == 0))
3702 rc = qlt_handle_cmd_for_atio(vha, atio);
3703 else
3704 rc = qlt_handle_task_mgmt(vha, atio);
3705 if (unlikely(rc != 0)) {
3706 if (rc == -ESRCH) {
3707 #if 1 /* With TERM EXCHANGE some FC cards refuse to boot */
3708 qlt_send_busy(vha, atio, SAM_STAT_BUSY);
3709 #else
3710 qlt_send_term_exchange(vha, NULL, atio, 1);
3711 #endif
3712 } else {
3713 if (tgt->tgt_stop) {
3714 ql_dbg(ql_dbg_tgt, vha, 0xe059,
3715 "qla_target: Unable to send "
3716 "command to target for req, "
3717 "ignoring.\n");
3718 } else {
3719 ql_dbg(ql_dbg_tgt, vha, 0xe05a,
3720 "qla_target(%d): Unable to send "
3721 "command to target, sending BUSY "
3722 "status.\n", vha->vp_idx);
3723 qlt_send_busy(vha, atio, SAM_STAT_BUSY);
3727 break;
3729 case IMMED_NOTIFY_TYPE:
3731 if (unlikely(atio->u.isp2x.entry_status != 0)) {
3732 ql_dbg(ql_dbg_tgt, vha, 0xe05b,
3733 "qla_target(%d): Received ATIO packet %x "
3734 "with error status %x\n", vha->vp_idx,
3735 atio->u.raw.entry_type,
3736 atio->u.isp2x.entry_status);
3737 break;
3739 ql_dbg(ql_dbg_tgt, vha, 0xe02e, "%s", "IMMED_NOTIFY ATIO");
3740 qlt_handle_imm_notify(vha, (struct imm_ntfy_from_isp *)atio);
3741 break;
3744 default:
3745 ql_dbg(ql_dbg_tgt, vha, 0xe05c,
3746 "qla_target(%d): Received unknown ATIO atio "
3747 "type %x\n", vha->vp_idx, atio->u.raw.entry_type);
3748 break;
3751 tgt->irq_cmd_count--;
3754 /* ha->hardware_lock supposed to be held on entry */
3755 /* called via callback from qla2xxx */
3756 static void qlt_response_pkt(struct scsi_qla_host *vha, response_t *pkt)
3758 struct qla_hw_data *ha = vha->hw;
3759 struct qla_tgt *tgt = ha->tgt.qla_tgt;
3761 if (unlikely(tgt == NULL)) {
3762 ql_dbg(ql_dbg_tgt, vha, 0xe05d,
3763 "qla_target(%d): Response pkt %x received, but no "
3764 "tgt (ha %p)\n", vha->vp_idx, pkt->entry_type, ha);
3765 return;
3768 ql_dbg(ql_dbg_tgt, vha, 0xe02f,
3769 "qla_target(%d): response pkt %p: T %02x C %02x S %02x "
3770 "handle %#x\n", vha->vp_idx, pkt, pkt->entry_type,
3771 pkt->entry_count, pkt->entry_status, pkt->handle);
3774 * In tgt_stop mode we also should allow all requests to pass.
3775 * Otherwise, some commands can stuck.
3778 tgt->irq_cmd_count++;
3780 switch (pkt->entry_type) {
3781 case CTIO_TYPE7:
3783 struct ctio7_from_24xx *entry = (struct ctio7_from_24xx *)pkt;
3784 ql_dbg(ql_dbg_tgt, vha, 0xe030, "CTIO_TYPE7: instance %d\n",
3785 vha->vp_idx);
3786 qlt_do_ctio_completion(vha, entry->handle,
3787 le16_to_cpu(entry->status)|(pkt->entry_status << 16),
3788 entry);
3789 break;
3792 case ACCEPT_TGT_IO_TYPE:
3794 struct atio_from_isp *atio = (struct atio_from_isp *)pkt;
3795 int rc;
3796 ql_dbg(ql_dbg_tgt, vha, 0xe031,
3797 "ACCEPT_TGT_IO instance %d status %04x "
3798 "lun %04x read/write %d data_length %04x "
3799 "target_id %02x rx_id %04x\n ", vha->vp_idx,
3800 le16_to_cpu(atio->u.isp2x.status),
3801 le16_to_cpu(atio->u.isp2x.lun),
3802 atio->u.isp2x.execution_codes,
3803 le32_to_cpu(atio->u.isp2x.data_length), GET_TARGET_ID(ha,
3804 atio), atio->u.isp2x.rx_id);
3805 if (atio->u.isp2x.status !=
3806 __constant_cpu_to_le16(ATIO_CDB_VALID)) {
3807 ql_dbg(ql_dbg_tgt, vha, 0xe05e,
3808 "qla_target(%d): ATIO with error "
3809 "status %x received\n", vha->vp_idx,
3810 le16_to_cpu(atio->u.isp2x.status));
3811 break;
3813 ql_dbg(ql_dbg_tgt, vha, 0xe032,
3814 "FCP CDB: 0x%02x, sizeof(cdb): %lu",
3815 atio->u.isp2x.cdb[0], (unsigned long
3816 int)sizeof(atio->u.isp2x.cdb));
3818 rc = qlt_handle_cmd_for_atio(vha, atio);
3819 if (unlikely(rc != 0)) {
3820 if (rc == -ESRCH) {
3821 #if 1 /* With TERM EXCHANGE some FC cards refuse to boot */
3822 qlt_send_busy(vha, atio, 0);
3823 #else
3824 qlt_send_term_exchange(vha, NULL, atio, 1);
3825 #endif
3826 } else {
3827 if (tgt->tgt_stop) {
3828 ql_dbg(ql_dbg_tgt, vha, 0xe05f,
3829 "qla_target: Unable to send "
3830 "command to target, sending TERM "
3831 "EXCHANGE for rsp\n");
3832 qlt_send_term_exchange(vha, NULL,
3833 atio, 1);
3834 } else {
3835 ql_dbg(ql_dbg_tgt, vha, 0xe060,
3836 "qla_target(%d): Unable to send "
3837 "command to target, sending BUSY "
3838 "status\n", vha->vp_idx);
3839 qlt_send_busy(vha, atio, 0);
3844 break;
3846 case CONTINUE_TGT_IO_TYPE:
3848 struct ctio_to_2xxx *entry = (struct ctio_to_2xxx *)pkt;
3849 ql_dbg(ql_dbg_tgt, vha, 0xe033,
3850 "CONTINUE_TGT_IO: instance %d\n", vha->vp_idx);
3851 qlt_do_ctio_completion(vha, entry->handle,
3852 le16_to_cpu(entry->status)|(pkt->entry_status << 16),
3853 entry);
3854 break;
3857 case CTIO_A64_TYPE:
3859 struct ctio_to_2xxx *entry = (struct ctio_to_2xxx *)pkt;
3860 ql_dbg(ql_dbg_tgt, vha, 0xe034, "CTIO_A64: instance %d\n",
3861 vha->vp_idx);
3862 qlt_do_ctio_completion(vha, entry->handle,
3863 le16_to_cpu(entry->status)|(pkt->entry_status << 16),
3864 entry);
3865 break;
3868 case IMMED_NOTIFY_TYPE:
3869 ql_dbg(ql_dbg_tgt, vha, 0xe035, "%s", "IMMED_NOTIFY\n");
3870 qlt_handle_imm_notify(vha, (struct imm_ntfy_from_isp *)pkt);
3871 break;
3873 case NOTIFY_ACK_TYPE:
3874 if (tgt->notify_ack_expected > 0) {
3875 struct nack_to_isp *entry = (struct nack_to_isp *)pkt;
3876 ql_dbg(ql_dbg_tgt, vha, 0xe036,
3877 "NOTIFY_ACK seq %08x status %x\n",
3878 le16_to_cpu(entry->u.isp2x.seq_id),
3879 le16_to_cpu(entry->u.isp2x.status));
3880 tgt->notify_ack_expected--;
3881 if (entry->u.isp2x.status !=
3882 __constant_cpu_to_le16(NOTIFY_ACK_SUCCESS)) {
3883 ql_dbg(ql_dbg_tgt, vha, 0xe061,
3884 "qla_target(%d): NOTIFY_ACK "
3885 "failed %x\n", vha->vp_idx,
3886 le16_to_cpu(entry->u.isp2x.status));
3888 } else {
3889 ql_dbg(ql_dbg_tgt, vha, 0xe062,
3890 "qla_target(%d): Unexpected NOTIFY_ACK received\n",
3891 vha->vp_idx);
3893 break;
3895 case ABTS_RECV_24XX:
3896 ql_dbg(ql_dbg_tgt, vha, 0xe037,
3897 "ABTS_RECV_24XX: instance %d\n", vha->vp_idx);
3898 qlt_24xx_handle_abts(vha, (struct abts_recv_from_24xx *)pkt);
3899 break;
3901 case ABTS_RESP_24XX:
3902 if (tgt->abts_resp_expected > 0) {
3903 struct abts_resp_from_24xx_fw *entry =
3904 (struct abts_resp_from_24xx_fw *)pkt;
3905 ql_dbg(ql_dbg_tgt, vha, 0xe038,
3906 "ABTS_RESP_24XX: compl_status %x\n",
3907 entry->compl_status);
3908 tgt->abts_resp_expected--;
3909 if (le16_to_cpu(entry->compl_status) !=
3910 ABTS_RESP_COMPL_SUCCESS) {
3911 if ((entry->error_subcode1 == 0x1E) &&
3912 (entry->error_subcode2 == 0)) {
3914 * We've got a race here: aborted
3915 * exchange not terminated, i.e.
3916 * response for the aborted command was
3917 * sent between the abort request was
3918 * received and processed.
3919 * Unfortunately, the firmware has a
3920 * silly requirement that all aborted
3921 * exchanges must be explicitely
3922 * terminated, otherwise it refuses to
3923 * send responses for the abort
3924 * requests. So, we have to
3925 * (re)terminate the exchange and retry
3926 * the abort response.
3928 qlt_24xx_retry_term_exchange(vha,
3929 entry);
3930 } else
3931 ql_dbg(ql_dbg_tgt, vha, 0xe063,
3932 "qla_target(%d): ABTS_RESP_24XX "
3933 "failed %x (subcode %x:%x)",
3934 vha->vp_idx, entry->compl_status,
3935 entry->error_subcode1,
3936 entry->error_subcode2);
3938 } else {
3939 ql_dbg(ql_dbg_tgt, vha, 0xe064,
3940 "qla_target(%d): Unexpected ABTS_RESP_24XX "
3941 "received\n", vha->vp_idx);
3943 break;
3945 default:
3946 ql_dbg(ql_dbg_tgt, vha, 0xe065,
3947 "qla_target(%d): Received unknown response pkt "
3948 "type %x\n", vha->vp_idx, pkt->entry_type);
3949 break;
3952 tgt->irq_cmd_count--;
3956 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
3958 void qlt_async_event(uint16_t code, struct scsi_qla_host *vha,
3959 uint16_t *mailbox)
3961 struct qla_hw_data *ha = vha->hw;
3962 struct qla_tgt *tgt = ha->tgt.qla_tgt;
3963 int reason_code;
3965 ql_dbg(ql_dbg_tgt, vha, 0xe039,
3966 "scsi(%ld): ha state %d init_done %d oper_mode %d topo %d\n",
3967 vha->host_no, atomic_read(&vha->loop_state), vha->flags.init_done,
3968 ha->operating_mode, ha->current_topology);
3970 if (!ha->tgt.tgt_ops)
3971 return;
3973 if (unlikely(tgt == NULL)) {
3974 ql_dbg(ql_dbg_tgt, vha, 0xe03a,
3975 "ASYNC EVENT %#x, but no tgt (ha %p)\n", code, ha);
3976 return;
3979 if (((code == MBA_POINT_TO_POINT) || (code == MBA_CHG_IN_CONNECTION)) &&
3980 IS_QLA2100(ha))
3981 return;
3983 * In tgt_stop mode we also should allow all requests to pass.
3984 * Otherwise, some commands can stuck.
3987 tgt->irq_cmd_count++;
3989 switch (code) {
3990 case MBA_RESET: /* Reset */
3991 case MBA_SYSTEM_ERR: /* System Error */
3992 case MBA_REQ_TRANSFER_ERR: /* Request Transfer Error */
3993 case MBA_RSP_TRANSFER_ERR: /* Response Transfer Error */
3994 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03a,
3995 "qla_target(%d): System error async event %#x "
3996 "occured", vha->vp_idx, code);
3997 break;
3998 case MBA_WAKEUP_THRES: /* Request Queue Wake-up. */
3999 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
4000 break;
4002 case MBA_LOOP_UP:
4004 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03b,
4005 "qla_target(%d): Async LOOP_UP occured "
4006 "(m[1]=%x, m[2]=%x, m[3]=%x, m[4]=%x)", vha->vp_idx,
4007 le16_to_cpu(mailbox[1]), le16_to_cpu(mailbox[2]),
4008 le16_to_cpu(mailbox[3]), le16_to_cpu(mailbox[4]));
4009 if (tgt->link_reinit_iocb_pending) {
4010 qlt_send_notify_ack(vha, (void *)&tgt->link_reinit_iocb,
4011 0, 0, 0, 0, 0, 0);
4012 tgt->link_reinit_iocb_pending = 0;
4014 break;
4017 case MBA_LIP_OCCURRED:
4018 case MBA_LOOP_DOWN:
4019 case MBA_LIP_RESET:
4020 case MBA_RSCN_UPDATE:
4021 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03c,
4022 "qla_target(%d): Async event %#x occured "
4023 "(m[1]=%x, m[2]=%x, m[3]=%x, m[4]=%x)", vha->vp_idx, code,
4024 le16_to_cpu(mailbox[1]), le16_to_cpu(mailbox[2]),
4025 le16_to_cpu(mailbox[3]), le16_to_cpu(mailbox[4]));
4026 break;
4028 case MBA_PORT_UPDATE:
4029 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03d,
4030 "qla_target(%d): Port update async event %#x "
4031 "occured: updating the ports database (m[1]=%x, m[2]=%x, "
4032 "m[3]=%x, m[4]=%x)", vha->vp_idx, code,
4033 le16_to_cpu(mailbox[1]), le16_to_cpu(mailbox[2]),
4034 le16_to_cpu(mailbox[3]), le16_to_cpu(mailbox[4]));
4035 reason_code = le16_to_cpu(mailbox[2]);
4036 if (reason_code == 0x4)
4037 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03e,
4038 "Async MB 2: Got PLOGI Complete\n");
4039 else if (reason_code == 0x7)
4040 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03f,
4041 "Async MB 2: Port Logged Out\n");
4042 break;
4044 default:
4045 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf040,
4046 "qla_target(%d): Async event %#x occured: "
4047 "ignore (m[1]=%x, m[2]=%x, m[3]=%x, m[4]=%x)", vha->vp_idx,
4048 code, le16_to_cpu(mailbox[1]), le16_to_cpu(mailbox[2]),
4049 le16_to_cpu(mailbox[3]), le16_to_cpu(mailbox[4]));
4050 break;
4053 tgt->irq_cmd_count--;
4056 static fc_port_t *qlt_get_port_database(struct scsi_qla_host *vha,
4057 uint16_t loop_id)
4059 fc_port_t *fcport;
4060 int rc;
4062 fcport = kzalloc(sizeof(*fcport), GFP_KERNEL);
4063 if (!fcport) {
4064 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06f,
4065 "qla_target(%d): Allocation of tmp FC port failed",
4066 vha->vp_idx);
4067 return NULL;
4070 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf041, "loop_id %d", loop_id);
4072 fcport->loop_id = loop_id;
4074 rc = qla2x00_get_port_database(vha, fcport, 0);
4075 if (rc != QLA_SUCCESS) {
4076 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf070,
4077 "qla_target(%d): Failed to retrieve fcport "
4078 "information -- get_port_database() returned %x "
4079 "(loop_id=0x%04x)", vha->vp_idx, rc, loop_id);
4080 kfree(fcport);
4081 return NULL;
4084 return fcport;
4087 /* Must be called under tgt_mutex */
4088 static struct qla_tgt_sess *qlt_make_local_sess(struct scsi_qla_host *vha,
4089 uint8_t *s_id)
4091 struct qla_hw_data *ha = vha->hw;
4092 struct qla_tgt_sess *sess = NULL;
4093 fc_port_t *fcport = NULL;
4094 int rc, global_resets;
4095 uint16_t loop_id = 0;
4097 retry:
4098 global_resets = atomic_read(&ha->tgt.qla_tgt->tgt_global_resets_count);
4100 rc = qla24xx_get_loop_id(vha, s_id, &loop_id);
4101 if (rc != 0) {
4102 if ((s_id[0] == 0xFF) &&
4103 (s_id[1] == 0xFC)) {
4105 * This is Domain Controller, so it should be
4106 * OK to drop SCSI commands from it.
4108 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf042,
4109 "Unable to find initiator with S_ID %x:%x:%x",
4110 s_id[0], s_id[1], s_id[2]);
4111 } else
4112 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf071,
4113 "qla_target(%d): Unable to find "
4114 "initiator with S_ID %x:%x:%x",
4115 vha->vp_idx, s_id[0], s_id[1],
4116 s_id[2]);
4117 return NULL;
4120 fcport = qlt_get_port_database(vha, loop_id);
4121 if (!fcport)
4122 return NULL;
4124 if (global_resets !=
4125 atomic_read(&ha->tgt.qla_tgt->tgt_global_resets_count)) {
4126 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf043,
4127 "qla_target(%d): global reset during session discovery "
4128 "(counter was %d, new %d), retrying", vha->vp_idx,
4129 global_resets,
4130 atomic_read(&ha->tgt.qla_tgt->tgt_global_resets_count));
4131 goto retry;
4134 sess = qlt_create_sess(vha, fcport, true);
4136 kfree(fcport);
4137 return sess;
4140 static void qlt_abort_work(struct qla_tgt *tgt,
4141 struct qla_tgt_sess_work_param *prm)
4143 struct scsi_qla_host *vha = tgt->vha;
4144 struct qla_hw_data *ha = vha->hw;
4145 struct qla_tgt_sess *sess = NULL;
4146 unsigned long flags;
4147 uint32_t be_s_id;
4148 uint8_t s_id[3];
4149 int rc;
4151 spin_lock_irqsave(&ha->hardware_lock, flags);
4153 if (tgt->tgt_stop)
4154 goto out_term;
4156 s_id[0] = prm->abts.fcp_hdr_le.s_id[2];
4157 s_id[1] = prm->abts.fcp_hdr_le.s_id[1];
4158 s_id[2] = prm->abts.fcp_hdr_le.s_id[0];
4160 sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha,
4161 (unsigned char *)&be_s_id);
4162 if (!sess) {
4163 spin_unlock_irqrestore(&ha->hardware_lock, flags);
4165 mutex_lock(&ha->tgt.tgt_mutex);
4166 sess = qlt_make_local_sess(vha, s_id);
4167 /* sess has got an extra creation ref */
4168 mutex_unlock(&ha->tgt.tgt_mutex);
4170 spin_lock_irqsave(&ha->hardware_lock, flags);
4171 if (!sess)
4172 goto out_term;
4173 } else {
4174 kref_get(&sess->se_sess->sess_kref);
4177 if (tgt->tgt_stop)
4178 goto out_term;
4180 rc = __qlt_24xx_handle_abts(vha, &prm->abts, sess);
4181 if (rc != 0)
4182 goto out_term;
4183 spin_unlock_irqrestore(&ha->hardware_lock, flags);
4185 ha->tgt.tgt_ops->put_sess(sess);
4186 return;
4188 out_term:
4189 qlt_24xx_send_abts_resp(vha, &prm->abts, FCP_TMF_REJECTED, false);
4190 spin_unlock_irqrestore(&ha->hardware_lock, flags);
4191 if (sess)
4192 ha->tgt.tgt_ops->put_sess(sess);
4195 static void qlt_tmr_work(struct qla_tgt *tgt,
4196 struct qla_tgt_sess_work_param *prm)
4198 struct atio_from_isp *a = &prm->tm_iocb2;
4199 struct scsi_qla_host *vha = tgt->vha;
4200 struct qla_hw_data *ha = vha->hw;
4201 struct qla_tgt_sess *sess = NULL;
4202 unsigned long flags;
4203 uint8_t *s_id = NULL; /* to hide compiler warnings */
4204 int rc;
4205 uint32_t lun, unpacked_lun;
4206 int lun_size, fn;
4207 void *iocb;
4209 spin_lock_irqsave(&ha->hardware_lock, flags);
4211 if (tgt->tgt_stop)
4212 goto out_term;
4214 s_id = prm->tm_iocb2.u.isp24.fcp_hdr.s_id;
4215 sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha, s_id);
4216 if (!sess) {
4217 spin_unlock_irqrestore(&ha->hardware_lock, flags);
4219 mutex_lock(&ha->tgt.tgt_mutex);
4220 sess = qlt_make_local_sess(vha, s_id);
4221 /* sess has got an extra creation ref */
4222 mutex_unlock(&ha->tgt.tgt_mutex);
4224 spin_lock_irqsave(&ha->hardware_lock, flags);
4225 if (!sess)
4226 goto out_term;
4227 } else {
4228 kref_get(&sess->se_sess->sess_kref);
4231 iocb = a;
4232 lun = a->u.isp24.fcp_cmnd.lun;
4233 lun_size = sizeof(lun);
4234 fn = a->u.isp24.fcp_cmnd.task_mgmt_flags;
4235 unpacked_lun = scsilun_to_int((struct scsi_lun *)&lun);
4237 rc = qlt_issue_task_mgmt(sess, unpacked_lun, fn, iocb, 0);
4238 if (rc != 0)
4239 goto out_term;
4240 spin_unlock_irqrestore(&ha->hardware_lock, flags);
4242 ha->tgt.tgt_ops->put_sess(sess);
4243 return;
4245 out_term:
4246 qlt_send_term_exchange(vha, NULL, &prm->tm_iocb2, 1);
4247 spin_unlock_irqrestore(&ha->hardware_lock, flags);
4248 if (sess)
4249 ha->tgt.tgt_ops->put_sess(sess);
4252 static void qlt_sess_work_fn(struct work_struct *work)
4254 struct qla_tgt *tgt = container_of(work, struct qla_tgt, sess_work);
4255 struct scsi_qla_host *vha = tgt->vha;
4256 unsigned long flags;
4258 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf000, "Sess work (tgt %p)", tgt);
4260 spin_lock_irqsave(&tgt->sess_work_lock, flags);
4261 while (!list_empty(&tgt->sess_works_list)) {
4262 struct qla_tgt_sess_work_param *prm = list_entry(
4263 tgt->sess_works_list.next, typeof(*prm),
4264 sess_works_list_entry);
4267 * This work can be scheduled on several CPUs at time, so we
4268 * must delete the entry to eliminate double processing
4270 list_del(&prm->sess_works_list_entry);
4272 spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
4274 switch (prm->type) {
4275 case QLA_TGT_SESS_WORK_ABORT:
4276 qlt_abort_work(tgt, prm);
4277 break;
4278 case QLA_TGT_SESS_WORK_TM:
4279 qlt_tmr_work(tgt, prm);
4280 break;
4281 default:
4282 BUG_ON(1);
4283 break;
4286 spin_lock_irqsave(&tgt->sess_work_lock, flags);
4288 kfree(prm);
4290 spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
4293 /* Must be called under tgt_host_action_mutex */
4294 int qlt_add_target(struct qla_hw_data *ha, struct scsi_qla_host *base_vha)
4296 struct qla_tgt *tgt;
4298 if (!QLA_TGT_MODE_ENABLED())
4299 return 0;
4301 ql_dbg(ql_dbg_tgt, base_vha, 0xe03b,
4302 "Registering target for host %ld(%p)", base_vha->host_no, ha);
4304 BUG_ON((ha->tgt.qla_tgt != NULL) || (ha->tgt.tgt_ops != NULL));
4306 tgt = kzalloc(sizeof(struct qla_tgt), GFP_KERNEL);
4307 if (!tgt) {
4308 ql_dbg(ql_dbg_tgt, base_vha, 0xe066,
4309 "Unable to allocate struct qla_tgt\n");
4310 return -ENOMEM;
4313 if (!(base_vha->host->hostt->supported_mode & MODE_TARGET))
4314 base_vha->host->hostt->supported_mode |= MODE_TARGET;
4316 tgt->ha = ha;
4317 tgt->vha = base_vha;
4318 init_waitqueue_head(&tgt->waitQ);
4319 INIT_LIST_HEAD(&tgt->sess_list);
4320 INIT_LIST_HEAD(&tgt->del_sess_list);
4321 INIT_DELAYED_WORK(&tgt->sess_del_work,
4322 (void (*)(struct work_struct *))qlt_del_sess_work_fn);
4323 spin_lock_init(&tgt->sess_work_lock);
4324 INIT_WORK(&tgt->sess_work, qlt_sess_work_fn);
4325 INIT_LIST_HEAD(&tgt->sess_works_list);
4326 spin_lock_init(&tgt->srr_lock);
4327 INIT_LIST_HEAD(&tgt->srr_ctio_list);
4328 INIT_LIST_HEAD(&tgt->srr_imm_list);
4329 INIT_WORK(&tgt->srr_work, qlt_handle_srr_work);
4330 atomic_set(&tgt->tgt_global_resets_count, 0);
4332 ha->tgt.qla_tgt = tgt;
4334 ql_dbg(ql_dbg_tgt, base_vha, 0xe067,
4335 "qla_target(%d): using 64 Bit PCI addressing",
4336 base_vha->vp_idx);
4337 tgt->tgt_enable_64bit_addr = 1;
4338 /* 3 is reserved */
4339 tgt->sg_tablesize = QLA_TGT_MAX_SG_24XX(base_vha->req->length - 3);
4340 tgt->datasegs_per_cmd = QLA_TGT_DATASEGS_PER_CMD_24XX;
4341 tgt->datasegs_per_cont = QLA_TGT_DATASEGS_PER_CONT_24XX;
4343 mutex_lock(&qla_tgt_mutex);
4344 list_add_tail(&tgt->tgt_list_entry, &qla_tgt_glist);
4345 mutex_unlock(&qla_tgt_mutex);
4347 return 0;
4350 /* Must be called under tgt_host_action_mutex */
4351 int qlt_remove_target(struct qla_hw_data *ha, struct scsi_qla_host *vha)
4353 if (!ha->tgt.qla_tgt)
4354 return 0;
4356 mutex_lock(&qla_tgt_mutex);
4357 list_del(&ha->tgt.qla_tgt->tgt_list_entry);
4358 mutex_unlock(&qla_tgt_mutex);
4360 ql_dbg(ql_dbg_tgt, vha, 0xe03c, "Unregistering target for host %ld(%p)",
4361 vha->host_no, ha);
4362 qlt_release(ha->tgt.qla_tgt);
4364 return 0;
4367 static void qlt_lport_dump(struct scsi_qla_host *vha, u64 wwpn,
4368 unsigned char *b)
4370 int i;
4372 pr_debug("qla2xxx HW vha->node_name: ");
4373 for (i = 0; i < WWN_SIZE; i++)
4374 pr_debug("%02x ", vha->node_name[i]);
4375 pr_debug("\n");
4376 pr_debug("qla2xxx HW vha->port_name: ");
4377 for (i = 0; i < WWN_SIZE; i++)
4378 pr_debug("%02x ", vha->port_name[i]);
4379 pr_debug("\n");
4381 pr_debug("qla2xxx passed configfs WWPN: ");
4382 put_unaligned_be64(wwpn, b);
4383 for (i = 0; i < WWN_SIZE; i++)
4384 pr_debug("%02x ", b[i]);
4385 pr_debug("\n");
4389 * qla_tgt_lport_register - register lport with external module
4391 * @qla_tgt_ops: Pointer for tcm_qla2xxx qla_tgt_ops
4392 * @wwpn: Passwd FC target WWPN
4393 * @callback: lport initialization callback for tcm_qla2xxx code
4394 * @target_lport_ptr: pointer for tcm_qla2xxx specific lport data
4396 int qlt_lport_register(struct qla_tgt_func_tmpl *qla_tgt_ops, u64 wwpn,
4397 int (*callback)(struct scsi_qla_host *), void *target_lport_ptr)
4399 struct qla_tgt *tgt;
4400 struct scsi_qla_host *vha;
4401 struct qla_hw_data *ha;
4402 struct Scsi_Host *host;
4403 unsigned long flags;
4404 int rc;
4405 u8 b[WWN_SIZE];
4407 mutex_lock(&qla_tgt_mutex);
4408 list_for_each_entry(tgt, &qla_tgt_glist, tgt_list_entry) {
4409 vha = tgt->vha;
4410 ha = vha->hw;
4412 host = vha->host;
4413 if (!host)
4414 continue;
4416 if (ha->tgt.tgt_ops != NULL)
4417 continue;
4419 if (!(host->hostt->supported_mode & MODE_TARGET))
4420 continue;
4422 spin_lock_irqsave(&ha->hardware_lock, flags);
4423 if (host->active_mode & MODE_TARGET) {
4424 pr_debug("MODE_TARGET already active on qla2xxx(%d)\n",
4425 host->host_no);
4426 spin_unlock_irqrestore(&ha->hardware_lock, flags);
4427 continue;
4429 spin_unlock_irqrestore(&ha->hardware_lock, flags);
4431 if (!scsi_host_get(host)) {
4432 ql_dbg(ql_dbg_tgt, vha, 0xe068,
4433 "Unable to scsi_host_get() for"
4434 " qla2xxx scsi_host\n");
4435 continue;
4437 qlt_lport_dump(vha, wwpn, b);
4439 if (memcmp(vha->port_name, b, WWN_SIZE)) {
4440 scsi_host_put(host);
4441 continue;
4444 * Setup passed parameters ahead of invoking callback
4446 ha->tgt.tgt_ops = qla_tgt_ops;
4447 ha->tgt.target_lport_ptr = target_lport_ptr;
4448 rc = (*callback)(vha);
4449 if (rc != 0) {
4450 ha->tgt.tgt_ops = NULL;
4451 ha->tgt.target_lport_ptr = NULL;
4453 mutex_unlock(&qla_tgt_mutex);
4454 return rc;
4456 mutex_unlock(&qla_tgt_mutex);
4458 return -ENODEV;
4460 EXPORT_SYMBOL(qlt_lport_register);
4463 * qla_tgt_lport_deregister - Degister lport
4465 * @vha: Registered scsi_qla_host pointer
4467 void qlt_lport_deregister(struct scsi_qla_host *vha)
4469 struct qla_hw_data *ha = vha->hw;
4470 struct Scsi_Host *sh = vha->host;
4472 * Clear the target_lport_ptr qla_target_template pointer in qla_hw_data
4474 ha->tgt.target_lport_ptr = NULL;
4475 ha->tgt.tgt_ops = NULL;
4477 * Release the Scsi_Host reference for the underlying qla2xxx host
4479 scsi_host_put(sh);
4481 EXPORT_SYMBOL(qlt_lport_deregister);
4483 /* Must be called under HW lock */
4484 void qlt_set_mode(struct scsi_qla_host *vha)
4486 struct qla_hw_data *ha = vha->hw;
4488 switch (ql2x_ini_mode) {
4489 case QLA2XXX_INI_MODE_DISABLED:
4490 case QLA2XXX_INI_MODE_EXCLUSIVE:
4491 vha->host->active_mode = MODE_TARGET;
4492 break;
4493 case QLA2XXX_INI_MODE_ENABLED:
4494 vha->host->active_mode |= MODE_TARGET;
4495 break;
4496 default:
4497 break;
4500 if (ha->tgt.ini_mode_force_reverse)
4501 qla_reverse_ini_mode(vha);
4504 /* Must be called under HW lock */
4505 void qlt_clear_mode(struct scsi_qla_host *vha)
4507 struct qla_hw_data *ha = vha->hw;
4509 switch (ql2x_ini_mode) {
4510 case QLA2XXX_INI_MODE_DISABLED:
4511 vha->host->active_mode = MODE_UNKNOWN;
4512 break;
4513 case QLA2XXX_INI_MODE_EXCLUSIVE:
4514 vha->host->active_mode = MODE_INITIATOR;
4515 break;
4516 case QLA2XXX_INI_MODE_ENABLED:
4517 vha->host->active_mode &= ~MODE_TARGET;
4518 break;
4519 default:
4520 break;
4523 if (ha->tgt.ini_mode_force_reverse)
4524 qla_reverse_ini_mode(vha);
4528 * qla_tgt_enable_vha - NO LOCK HELD
4530 * host_reset, bring up w/ Target Mode Enabled
4532 void
4533 qlt_enable_vha(struct scsi_qla_host *vha)
4535 struct qla_hw_data *ha = vha->hw;
4536 struct qla_tgt *tgt = ha->tgt.qla_tgt;
4537 unsigned long flags;
4539 if (!tgt) {
4540 ql_dbg(ql_dbg_tgt, vha, 0xe069,
4541 "Unable to locate qla_tgt pointer from"
4542 " struct qla_hw_data\n");
4543 dump_stack();
4544 return;
4547 spin_lock_irqsave(&ha->hardware_lock, flags);
4548 tgt->tgt_stopped = 0;
4549 qlt_set_mode(vha);
4550 spin_unlock_irqrestore(&ha->hardware_lock, flags);
4552 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
4553 qla2xxx_wake_dpc(vha);
4554 qla2x00_wait_for_hba_online(vha);
4556 EXPORT_SYMBOL(qlt_enable_vha);
4559 * qla_tgt_disable_vha - NO LOCK HELD
4561 * Disable Target Mode and reset the adapter
4563 void
4564 qlt_disable_vha(struct scsi_qla_host *vha)
4566 struct qla_hw_data *ha = vha->hw;
4567 struct qla_tgt *tgt = ha->tgt.qla_tgt;
4568 unsigned long flags;
4570 if (!tgt) {
4571 ql_dbg(ql_dbg_tgt, vha, 0xe06a,
4572 "Unable to locate qla_tgt pointer from"
4573 " struct qla_hw_data\n");
4574 dump_stack();
4575 return;
4578 spin_lock_irqsave(&ha->hardware_lock, flags);
4579 qlt_clear_mode(vha);
4580 spin_unlock_irqrestore(&ha->hardware_lock, flags);
4582 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
4583 qla2xxx_wake_dpc(vha);
4584 qla2x00_wait_for_hba_online(vha);
4588 * Called from qla_init.c:qla24xx_vport_create() contex to setup
4589 * the target mode specific struct scsi_qla_host and struct qla_hw_data
4590 * members.
4592 void
4593 qlt_vport_create(struct scsi_qla_host *vha, struct qla_hw_data *ha)
4595 if (!qla_tgt_mode_enabled(vha))
4596 return;
4598 mutex_init(&ha->tgt.tgt_mutex);
4599 mutex_init(&ha->tgt.tgt_host_action_mutex);
4601 qlt_clear_mode(vha);
4604 * NOTE: Currently the value is kept the same for <24xx and
4605 * >=24xx ISPs. If it is necessary to change it,
4606 * the check should be added for specific ISPs,
4607 * assigning the value appropriately.
4609 ha->tgt.atio_q_length = ATIO_ENTRY_CNT_24XX;
4612 void
4613 qlt_rff_id(struct scsi_qla_host *vha, struct ct_sns_req *ct_req)
4616 * FC-4 Feature bit 0 indicates target functionality to the name server.
4618 if (qla_tgt_mode_enabled(vha)) {
4619 if (qla_ini_mode_enabled(vha))
4620 ct_req->req.rff_id.fc4_feature = BIT_0 | BIT_1;
4621 else
4622 ct_req->req.rff_id.fc4_feature = BIT_0;
4623 } else if (qla_ini_mode_enabled(vha)) {
4624 ct_req->req.rff_id.fc4_feature = BIT_1;
4629 * qlt_init_atio_q_entries() - Initializes ATIO queue entries.
4630 * @ha: HA context
4632 * Beginning of ATIO ring has initialization control block already built
4633 * by nvram config routine.
4635 * Returns 0 on success.
4637 void
4638 qlt_init_atio_q_entries(struct scsi_qla_host *vha)
4640 struct qla_hw_data *ha = vha->hw;
4641 uint16_t cnt;
4642 struct atio_from_isp *pkt = (struct atio_from_isp *)ha->tgt.atio_ring;
4644 if (!qla_tgt_mode_enabled(vha))
4645 return;
4647 for (cnt = 0; cnt < ha->tgt.atio_q_length; cnt++) {
4648 pkt->u.raw.signature = ATIO_PROCESSED;
4649 pkt++;
4655 * qlt_24xx_process_atio_queue() - Process ATIO queue entries.
4656 * @ha: SCSI driver HA context
4658 void
4659 qlt_24xx_process_atio_queue(struct scsi_qla_host *vha)
4661 struct qla_hw_data *ha = vha->hw;
4662 struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
4663 struct atio_from_isp *pkt;
4664 int cnt, i;
4666 if (!vha->flags.online)
4667 return;
4669 while (ha->tgt.atio_ring_ptr->signature != ATIO_PROCESSED) {
4670 pkt = (struct atio_from_isp *)ha->tgt.atio_ring_ptr;
4671 cnt = pkt->u.raw.entry_count;
4673 qlt_24xx_atio_pkt_all_vps(vha, (struct atio_from_isp *)pkt);
4675 for (i = 0; i < cnt; i++) {
4676 ha->tgt.atio_ring_index++;
4677 if (ha->tgt.atio_ring_index == ha->tgt.atio_q_length) {
4678 ha->tgt.atio_ring_index = 0;
4679 ha->tgt.atio_ring_ptr = ha->tgt.atio_ring;
4680 } else
4681 ha->tgt.atio_ring_ptr++;
4683 pkt->u.raw.signature = ATIO_PROCESSED;
4684 pkt = (struct atio_from_isp *)ha->tgt.atio_ring_ptr;
4686 wmb();
4689 /* Adjust ring index */
4690 WRT_REG_DWORD(&reg->atio_q_out, ha->tgt.atio_ring_index);
4693 void
4694 qlt_24xx_config_rings(struct scsi_qla_host *vha, device_reg_t __iomem *reg)
4696 struct qla_hw_data *ha = vha->hw;
4698 /* FIXME: atio_q in/out for ha->mqenable=1..? */
4699 if (ha->mqenable) {
4700 #if 0
4701 WRT_REG_DWORD(&reg->isp25mq.atio_q_in, 0);
4702 WRT_REG_DWORD(&reg->isp25mq.atio_q_out, 0);
4703 RD_REG_DWORD(&reg->isp25mq.atio_q_out);
4704 #endif
4705 } else {
4706 /* Setup APTIO registers for target mode */
4707 WRT_REG_DWORD(&reg->isp24.atio_q_in, 0);
4708 WRT_REG_DWORD(&reg->isp24.atio_q_out, 0);
4709 RD_REG_DWORD(&reg->isp24.atio_q_out);
4713 void
4714 qlt_24xx_config_nvram_stage1(struct scsi_qla_host *vha, struct nvram_24xx *nv)
4716 struct qla_hw_data *ha = vha->hw;
4718 if (qla_tgt_mode_enabled(vha)) {
4719 if (!ha->tgt.saved_set) {
4720 /* We save only once */
4721 ha->tgt.saved_exchange_count = nv->exchange_count;
4722 ha->tgt.saved_firmware_options_1 =
4723 nv->firmware_options_1;
4724 ha->tgt.saved_firmware_options_2 =
4725 nv->firmware_options_2;
4726 ha->tgt.saved_firmware_options_3 =
4727 nv->firmware_options_3;
4728 ha->tgt.saved_set = 1;
4731 nv->exchange_count = __constant_cpu_to_le16(0xFFFF);
4733 /* Enable target mode */
4734 nv->firmware_options_1 |= __constant_cpu_to_le32(BIT_4);
4736 /* Disable ini mode, if requested */
4737 if (!qla_ini_mode_enabled(vha))
4738 nv->firmware_options_1 |= __constant_cpu_to_le32(BIT_5);
4740 /* Disable Full Login after LIP */
4741 nv->firmware_options_1 &= __constant_cpu_to_le32(~BIT_13);
4742 /* Enable initial LIP */
4743 nv->firmware_options_1 &= __constant_cpu_to_le32(~BIT_9);
4744 /* Enable FC tapes support */
4745 nv->firmware_options_2 |= __constant_cpu_to_le32(BIT_12);
4746 /* Disable Full Login after LIP */
4747 nv->host_p &= __constant_cpu_to_le32(~BIT_10);
4748 /* Enable target PRLI control */
4749 nv->firmware_options_2 |= __constant_cpu_to_le32(BIT_14);
4750 } else {
4751 if (ha->tgt.saved_set) {
4752 nv->exchange_count = ha->tgt.saved_exchange_count;
4753 nv->firmware_options_1 =
4754 ha->tgt.saved_firmware_options_1;
4755 nv->firmware_options_2 =
4756 ha->tgt.saved_firmware_options_2;
4757 nv->firmware_options_3 =
4758 ha->tgt.saved_firmware_options_3;
4760 return;
4763 /* out-of-order frames reassembly */
4764 nv->firmware_options_3 |= BIT_6|BIT_9;
4766 if (ha->tgt.enable_class_2) {
4767 if (vha->flags.init_done)
4768 fc_host_supported_classes(vha->host) =
4769 FC_COS_CLASS2 | FC_COS_CLASS3;
4771 nv->firmware_options_2 |= __constant_cpu_to_le32(BIT_8);
4772 } else {
4773 if (vha->flags.init_done)
4774 fc_host_supported_classes(vha->host) = FC_COS_CLASS3;
4776 nv->firmware_options_2 &= ~__constant_cpu_to_le32(BIT_8);
4780 void
4781 qlt_24xx_config_nvram_stage2(struct scsi_qla_host *vha,
4782 struct init_cb_24xx *icb)
4784 struct qla_hw_data *ha = vha->hw;
4786 if (ha->tgt.node_name_set) {
4787 memcpy(icb->node_name, ha->tgt.tgt_node_name, WWN_SIZE);
4788 icb->firmware_options_1 |= __constant_cpu_to_le32(BIT_14);
4793 qlt_24xx_process_response_error(struct scsi_qla_host *vha,
4794 struct sts_entry_24xx *pkt)
4796 switch (pkt->entry_type) {
4797 case ABTS_RECV_24XX:
4798 case ABTS_RESP_24XX:
4799 case CTIO_TYPE7:
4800 case NOTIFY_ACK_TYPE:
4801 return 1;
4802 default:
4803 return 0;
4807 void
4808 qlt_modify_vp_config(struct scsi_qla_host *vha,
4809 struct vp_config_entry_24xx *vpmod)
4811 if (qla_tgt_mode_enabled(vha))
4812 vpmod->options_idx1 &= ~BIT_5;
4813 /* Disable ini mode, if requested */
4814 if (!qla_ini_mode_enabled(vha))
4815 vpmod->options_idx1 &= ~BIT_4;
4818 void
4819 qlt_probe_one_stage1(struct scsi_qla_host *base_vha, struct qla_hw_data *ha)
4821 if (!QLA_TGT_MODE_ENABLED())
4822 return;
4824 mutex_init(&ha->tgt.tgt_mutex);
4825 mutex_init(&ha->tgt.tgt_host_action_mutex);
4826 qlt_clear_mode(base_vha);
4830 qlt_mem_alloc(struct qla_hw_data *ha)
4832 if (!QLA_TGT_MODE_ENABLED())
4833 return 0;
4835 ha->tgt.tgt_vp_map = kzalloc(sizeof(struct qla_tgt_vp_map) *
4836 MAX_MULTI_ID_FABRIC, GFP_KERNEL);
4837 if (!ha->tgt.tgt_vp_map)
4838 return -ENOMEM;
4840 ha->tgt.atio_ring = dma_alloc_coherent(&ha->pdev->dev,
4841 (ha->tgt.atio_q_length + 1) * sizeof(struct atio_from_isp),
4842 &ha->tgt.atio_dma, GFP_KERNEL);
4843 if (!ha->tgt.atio_ring) {
4844 kfree(ha->tgt.tgt_vp_map);
4845 return -ENOMEM;
4847 return 0;
4850 void
4851 qlt_mem_free(struct qla_hw_data *ha)
4853 if (!QLA_TGT_MODE_ENABLED())
4854 return;
4856 if (ha->tgt.atio_ring) {
4857 dma_free_coherent(&ha->pdev->dev, (ha->tgt.atio_q_length + 1) *
4858 sizeof(struct atio_from_isp), ha->tgt.atio_ring,
4859 ha->tgt.atio_dma);
4861 kfree(ha->tgt.tgt_vp_map);
4864 /* vport_slock to be held by the caller */
4865 void
4866 qlt_update_vp_map(struct scsi_qla_host *vha, int cmd)
4868 if (!QLA_TGT_MODE_ENABLED())
4869 return;
4871 switch (cmd) {
4872 case SET_VP_IDX:
4873 vha->hw->tgt.tgt_vp_map[vha->vp_idx].vha = vha;
4874 break;
4875 case SET_AL_PA:
4876 vha->hw->tgt.tgt_vp_map[vha->d_id.b.al_pa].idx = vha->vp_idx;
4877 break;
4878 case RESET_VP_IDX:
4879 vha->hw->tgt.tgt_vp_map[vha->vp_idx].vha = NULL;
4880 break;
4881 case RESET_AL_PA:
4882 vha->hw->tgt.tgt_vp_map[vha->d_id.b.al_pa].idx = 0;
4883 break;
4887 static int __init qlt_parse_ini_mode(void)
4889 if (strcasecmp(qlini_mode, QLA2XXX_INI_MODE_STR_EXCLUSIVE) == 0)
4890 ql2x_ini_mode = QLA2XXX_INI_MODE_EXCLUSIVE;
4891 else if (strcasecmp(qlini_mode, QLA2XXX_INI_MODE_STR_DISABLED) == 0)
4892 ql2x_ini_mode = QLA2XXX_INI_MODE_DISABLED;
4893 else if (strcasecmp(qlini_mode, QLA2XXX_INI_MODE_STR_ENABLED) == 0)
4894 ql2x_ini_mode = QLA2XXX_INI_MODE_ENABLED;
4895 else
4896 return false;
4898 return true;
4901 int __init qlt_init(void)
4903 int ret;
4905 if (!qlt_parse_ini_mode()) {
4906 ql_log(ql_log_fatal, NULL, 0xe06b,
4907 "qlt_parse_ini_mode() failed\n");
4908 return -EINVAL;
4911 if (!QLA_TGT_MODE_ENABLED())
4912 return 0;
4914 qla_tgt_cmd_cachep = kmem_cache_create("qla_tgt_cmd_cachep",
4915 sizeof(struct qla_tgt_cmd), __alignof__(struct qla_tgt_cmd), 0,
4916 NULL);
4917 if (!qla_tgt_cmd_cachep) {
4918 ql_log(ql_log_fatal, NULL, 0xe06c,
4919 "kmem_cache_create for qla_tgt_cmd_cachep failed\n");
4920 return -ENOMEM;
4923 qla_tgt_mgmt_cmd_cachep = kmem_cache_create("qla_tgt_mgmt_cmd_cachep",
4924 sizeof(struct qla_tgt_mgmt_cmd), __alignof__(struct
4925 qla_tgt_mgmt_cmd), 0, NULL);
4926 if (!qla_tgt_mgmt_cmd_cachep) {
4927 ql_log(ql_log_fatal, NULL, 0xe06d,
4928 "kmem_cache_create for qla_tgt_mgmt_cmd_cachep failed\n");
4929 ret = -ENOMEM;
4930 goto out;
4933 qla_tgt_mgmt_cmd_mempool = mempool_create(25, mempool_alloc_slab,
4934 mempool_free_slab, qla_tgt_mgmt_cmd_cachep);
4935 if (!qla_tgt_mgmt_cmd_mempool) {
4936 ql_log(ql_log_fatal, NULL, 0xe06e,
4937 "mempool_create for qla_tgt_mgmt_cmd_mempool failed\n");
4938 ret = -ENOMEM;
4939 goto out_mgmt_cmd_cachep;
4942 qla_tgt_wq = alloc_workqueue("qla_tgt_wq", 0, 0);
4943 if (!qla_tgt_wq) {
4944 ql_log(ql_log_fatal, NULL, 0xe06f,
4945 "alloc_workqueue for qla_tgt_wq failed\n");
4946 ret = -ENOMEM;
4947 goto out_cmd_mempool;
4950 * Return 1 to signal that initiator-mode is being disabled
4952 return (ql2x_ini_mode == QLA2XXX_INI_MODE_DISABLED) ? 1 : 0;
4954 out_cmd_mempool:
4955 mempool_destroy(qla_tgt_mgmt_cmd_mempool);
4956 out_mgmt_cmd_cachep:
4957 kmem_cache_destroy(qla_tgt_mgmt_cmd_cachep);
4958 out:
4959 kmem_cache_destroy(qla_tgt_cmd_cachep);
4960 return ret;
4963 void qlt_exit(void)
4965 if (!QLA_TGT_MODE_ENABLED())
4966 return;
4968 destroy_workqueue(qla_tgt_wq);
4969 mempool_destroy(qla_tgt_mgmt_cmd_mempool);
4970 kmem_cache_destroy(qla_tgt_mgmt_cmd_cachep);
4971 kmem_cache_destroy(qla_tgt_cmd_cachep);