x86/mm/pat: Don't report PAT on CPUs that don't support it
[linux/fpc-iii.git] / drivers / scsi / qla2xxx / qla_target.c
blob0e03ca2ab3e52358c817cdd2cdc667ba2bfb1ba3
1 /*
2 * qla_target.c SCSI LLD infrastructure for QLogic 22xx/23xx/24xx/25xx
4 * based on qla2x00t.c code:
6 * Copyright (C) 2004 - 2010 Vladislav Bolkhovitin <vst@vlnb.net>
7 * Copyright (C) 2004 - 2005 Leonid Stoljar
8 * Copyright (C) 2006 Nathaniel Clark <nate@misrule.us>
9 * Copyright (C) 2006 - 2010 ID7 Ltd.
11 * Forward port and refactoring to modern qla2xxx and target/configfs
13 * Copyright (C) 2010-2013 Nicholas A. Bellinger <nab@kernel.org>
15 * This program is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public License
17 * as published by the Free Software Foundation, version 2
18 * of the License.
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
26 #include <linux/module.h>
27 #include <linux/init.h>
28 #include <linux/types.h>
29 #include <linux/blkdev.h>
30 #include <linux/interrupt.h>
31 #include <linux/pci.h>
32 #include <linux/delay.h>
33 #include <linux/list.h>
34 #include <linux/workqueue.h>
35 #include <asm/unaligned.h>
36 #include <scsi/scsi.h>
37 #include <scsi/scsi_host.h>
38 #include <scsi/scsi_tcq.h>
39 #include <target/target_core_base.h>
40 #include <target/target_core_fabric.h>
42 #include "qla_def.h"
43 #include "qla_target.h"
45 static int ql2xtgt_tape_enable;
46 module_param(ql2xtgt_tape_enable, int, S_IRUGO|S_IWUSR);
47 MODULE_PARM_DESC(ql2xtgt_tape_enable,
48 "Enables Sequence level error recovery (aka FC Tape). Default is 0 - no SLER. 1 - Enable SLER.");
50 static char *qlini_mode = QLA2XXX_INI_MODE_STR_ENABLED;
51 module_param(qlini_mode, charp, S_IRUGO);
52 MODULE_PARM_DESC(qlini_mode,
53 "Determines when initiator mode will be enabled. Possible values: "
54 "\"exclusive\" - initiator mode will be enabled on load, "
55 "disabled on enabling target mode and then on disabling target mode "
56 "enabled back; "
57 "\"disabled\" - initiator mode will never be enabled; "
58 "\"dual\" - Initiator Modes will be enabled. Target Mode can be activated "
59 "when ready "
60 "\"enabled\" (default) - initiator mode will always stay enabled.");
62 static int ql_dm_tgt_ex_pct = 50;
63 module_param(ql_dm_tgt_ex_pct, int, S_IRUGO|S_IWUSR);
64 MODULE_PARM_DESC(ql_dm_tgt_ex_pct,
65 "For Dual Mode (qlini_mode=dual), this parameter determines "
66 "the percentage of exchanges/cmds FW will allocate resources "
67 "for Target mode.");
69 int ql2x_ini_mode = QLA2XXX_INI_MODE_EXCLUSIVE;
71 static int temp_sam_status = SAM_STAT_BUSY;
74 * From scsi/fc/fc_fcp.h
76 enum fcp_resp_rsp_codes {
77 FCP_TMF_CMPL = 0,
78 FCP_DATA_LEN_INVALID = 1,
79 FCP_CMND_FIELDS_INVALID = 2,
80 FCP_DATA_PARAM_MISMATCH = 3,
81 FCP_TMF_REJECTED = 4,
82 FCP_TMF_FAILED = 5,
83 FCP_TMF_INVALID_LUN = 9,
87 * fc_pri_ta from scsi/fc/fc_fcp.h
89 #define FCP_PTA_SIMPLE 0 /* simple task attribute */
90 #define FCP_PTA_HEADQ 1 /* head of queue task attribute */
91 #define FCP_PTA_ORDERED 2 /* ordered task attribute */
92 #define FCP_PTA_ACA 4 /* auto. contingent allegiance */
93 #define FCP_PTA_MASK 7 /* mask for task attribute field */
94 #define FCP_PRI_SHIFT 3 /* priority field starts in bit 3 */
95 #define FCP_PRI_RESVD_MASK 0x80 /* reserved bits in priority field */
98 * This driver calls qla2x00_alloc_iocbs() and qla2x00_issue_marker(), which
99 * must be called under HW lock and could unlock/lock it inside.
100 * It isn't an issue, since in the current implementation on the time when
101 * those functions are called:
103 * - Either context is IRQ and only IRQ handler can modify HW data,
104 * including rings related fields,
106 * - Or access to target mode variables from struct qla_tgt doesn't
107 * cross those functions boundaries, except tgt_stop, which
108 * additionally protected by irq_cmd_count.
110 /* Predefs for callbacks handed to qla2xxx LLD */
111 static void qlt_24xx_atio_pkt(struct scsi_qla_host *ha,
112 struct atio_from_isp *pkt, uint8_t);
113 static void qlt_response_pkt(struct scsi_qla_host *ha, response_t *pkt);
114 static int qlt_issue_task_mgmt(struct fc_port *sess, u64 lun,
115 int fn, void *iocb, int flags);
116 static void qlt_send_term_exchange(struct scsi_qla_host *ha, struct qla_tgt_cmd
117 *cmd, struct atio_from_isp *atio, int ha_locked, int ul_abort);
118 static void qlt_abort_cmd_on_host_reset(struct scsi_qla_host *vha,
119 struct qla_tgt_cmd *cmd);
120 static void qlt_alloc_qfull_cmd(struct scsi_qla_host *vha,
121 struct atio_from_isp *atio, uint16_t status, int qfull);
122 static void qlt_disable_vha(struct scsi_qla_host *vha);
123 static void qlt_clear_tgt_db(struct qla_tgt *tgt);
124 static void qlt_send_notify_ack(struct scsi_qla_host *vha,
125 struct imm_ntfy_from_isp *ntfy,
126 uint32_t add_flags, uint16_t resp_code, int resp_code_valid,
127 uint16_t srr_flags, uint16_t srr_reject_code, uint8_t srr_explan);
128 static void qlt_send_term_imm_notif(struct scsi_qla_host *vha,
129 struct imm_ntfy_from_isp *imm, int ha_locked);
130 static struct fc_port *qlt_create_sess(struct scsi_qla_host *vha,
131 fc_port_t *fcport, bool local);
132 void qlt_unreg_sess(struct fc_port *sess);
133 static void qlt_24xx_handle_abts(struct scsi_qla_host *,
134 struct abts_recv_from_24xx *);
137 * Global Variables
139 static struct kmem_cache *qla_tgt_mgmt_cmd_cachep;
140 static struct kmem_cache *qla_tgt_plogi_cachep;
141 static mempool_t *qla_tgt_mgmt_cmd_mempool;
142 static struct workqueue_struct *qla_tgt_wq;
143 static DEFINE_MUTEX(qla_tgt_mutex);
144 static LIST_HEAD(qla_tgt_glist);
146 static const char *prot_op_str(u32 prot_op)
148 switch (prot_op) {
149 case TARGET_PROT_NORMAL: return "NORMAL";
150 case TARGET_PROT_DIN_INSERT: return "DIN_INSERT";
151 case TARGET_PROT_DOUT_INSERT: return "DOUT_INSERT";
152 case TARGET_PROT_DIN_STRIP: return "DIN_STRIP";
153 case TARGET_PROT_DOUT_STRIP: return "DOUT_STRIP";
154 case TARGET_PROT_DIN_PASS: return "DIN_PASS";
155 case TARGET_PROT_DOUT_PASS: return "DOUT_PASS";
156 default: return "UNKNOWN";
160 /* This API intentionally takes dest as a parameter, rather than returning
161 * int value to avoid caller forgetting to issue wmb() after the store */
162 void qlt_do_generation_tick(struct scsi_qla_host *vha, int *dest)
164 scsi_qla_host_t *base_vha = pci_get_drvdata(vha->hw->pdev);
165 *dest = atomic_inc_return(&base_vha->generation_tick);
166 /* memory barrier */
167 wmb();
170 /* Might release hw lock, then reaquire!! */
171 static inline int qlt_issue_marker(struct scsi_qla_host *vha, int vha_locked)
173 /* Send marker if required */
174 if (unlikely(vha->marker_needed != 0)) {
175 int rc = qla2x00_issue_marker(vha, vha_locked);
176 if (rc != QLA_SUCCESS) {
177 ql_dbg(ql_dbg_tgt, vha, 0xe03d,
178 "qla_target(%d): issue_marker() failed\n",
179 vha->vp_idx);
181 return rc;
183 return QLA_SUCCESS;
186 static inline
187 struct scsi_qla_host *qlt_find_host_by_d_id(struct scsi_qla_host *vha,
188 uint8_t *d_id)
190 struct scsi_qla_host *host;
191 uint32_t key = 0;
193 if ((vha->d_id.b.area == d_id[1]) && (vha->d_id.b.domain == d_id[0]) &&
194 (vha->d_id.b.al_pa == d_id[2]))
195 return vha;
197 key = (uint32_t)d_id[0] << 16;
198 key |= (uint32_t)d_id[1] << 8;
199 key |= (uint32_t)d_id[2];
201 host = btree_lookup32(&vha->hw->tgt.host_map, key);
202 if (!host)
203 ql_dbg(ql_dbg_tgt_mgt, vha, 0xffff,
204 "Unable to find host %06x\n", key);
206 return host;
209 static inline
210 struct scsi_qla_host *qlt_find_host_by_vp_idx(struct scsi_qla_host *vha,
211 uint16_t vp_idx)
213 struct qla_hw_data *ha = vha->hw;
215 if (vha->vp_idx == vp_idx)
216 return vha;
218 BUG_ON(ha->tgt.tgt_vp_map == NULL);
219 if (likely(test_bit(vp_idx, ha->vp_idx_map)))
220 return ha->tgt.tgt_vp_map[vp_idx].vha;
222 return NULL;
225 static inline void qlt_incr_num_pend_cmds(struct scsi_qla_host *vha)
227 unsigned long flags;
229 spin_lock_irqsave(&vha->hw->tgt.q_full_lock, flags);
231 vha->hw->tgt.num_pend_cmds++;
232 if (vha->hw->tgt.num_pend_cmds > vha->qla_stats.stat_max_pend_cmds)
233 vha->qla_stats.stat_max_pend_cmds =
234 vha->hw->tgt.num_pend_cmds;
235 spin_unlock_irqrestore(&vha->hw->tgt.q_full_lock, flags);
237 static inline void qlt_decr_num_pend_cmds(struct scsi_qla_host *vha)
239 unsigned long flags;
241 spin_lock_irqsave(&vha->hw->tgt.q_full_lock, flags);
242 vha->hw->tgt.num_pend_cmds--;
243 spin_unlock_irqrestore(&vha->hw->tgt.q_full_lock, flags);
247 static void qlt_queue_unknown_atio(scsi_qla_host_t *vha,
248 struct atio_from_isp *atio, uint8_t ha_locked)
250 struct qla_tgt_sess_op *u;
251 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
252 unsigned long flags;
254 if (tgt->tgt_stop) {
255 ql_dbg(ql_dbg_async, vha, 0xffff,
256 "qla_target(%d): dropping unknown ATIO_TYPE7, "
257 "because tgt is being stopped", vha->vp_idx);
258 goto out_term;
261 u = kzalloc(sizeof(*u), GFP_ATOMIC);
262 if (u == NULL) {
263 ql_dbg(ql_dbg_async, vha, 0xffff,
264 "Alloc of struct unknown_atio (size %zd) failed", sizeof(*u));
265 /* It should be harmless and on the next retry should work well */
266 goto out_term;
269 u->vha = vha;
270 memcpy(&u->atio, atio, sizeof(*atio));
271 INIT_LIST_HEAD(&u->cmd_list);
273 spin_lock_irqsave(&vha->cmd_list_lock, flags);
274 list_add_tail(&u->cmd_list, &vha->unknown_atio_list);
275 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
277 schedule_delayed_work(&vha->unknown_atio_work, 1);
279 out:
280 return;
282 out_term:
283 qlt_send_term_exchange(vha, NULL, atio, ha_locked, 0);
284 goto out;
287 static void qlt_try_to_dequeue_unknown_atios(struct scsi_qla_host *vha,
288 uint8_t ha_locked)
290 struct qla_tgt_sess_op *u, *t;
291 scsi_qla_host_t *host;
292 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
293 unsigned long flags;
294 uint8_t queued = 0;
296 list_for_each_entry_safe(u, t, &vha->unknown_atio_list, cmd_list) {
297 if (u->aborted) {
298 ql_dbg(ql_dbg_async, vha, 0xffff,
299 "Freeing unknown %s %p, because of Abort",
300 "ATIO_TYPE7", u);
301 qlt_send_term_exchange(vha, NULL, &u->atio,
302 ha_locked, 0);
303 goto abort;
306 host = qlt_find_host_by_d_id(vha, u->atio.u.isp24.fcp_hdr.d_id);
307 if (host != NULL) {
308 ql_dbg(ql_dbg_async, vha, 0xffff,
309 "Requeuing unknown ATIO_TYPE7 %p", u);
310 qlt_24xx_atio_pkt(host, &u->atio, ha_locked);
311 } else if (tgt->tgt_stop) {
312 ql_dbg(ql_dbg_async, vha, 0xffff,
313 "Freeing unknown %s %p, because tgt is being stopped",
314 "ATIO_TYPE7", u);
315 qlt_send_term_exchange(vha, NULL, &u->atio,
316 ha_locked, 0);
317 } else {
318 ql_dbg(ql_dbg_async, vha, 0xffff,
319 "u %p, vha %p, host %p, sched again..", u,
320 vha, host);
321 if (!queued) {
322 queued = 1;
323 schedule_delayed_work(&vha->unknown_atio_work,
326 continue;
329 abort:
330 spin_lock_irqsave(&vha->cmd_list_lock, flags);
331 list_del(&u->cmd_list);
332 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
333 kfree(u);
337 void qlt_unknown_atio_work_fn(struct work_struct *work)
339 struct scsi_qla_host *vha = container_of(to_delayed_work(work),
340 struct scsi_qla_host, unknown_atio_work);
342 qlt_try_to_dequeue_unknown_atios(vha, 0);
345 static bool qlt_24xx_atio_pkt_all_vps(struct scsi_qla_host *vha,
346 struct atio_from_isp *atio, uint8_t ha_locked)
348 ql_dbg(ql_dbg_tgt, vha, 0xe072,
349 "%s: qla_target(%d): type %x ox_id %04x\n",
350 __func__, vha->vp_idx, atio->u.raw.entry_type,
351 be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id));
353 switch (atio->u.raw.entry_type) {
354 case ATIO_TYPE7:
356 struct scsi_qla_host *host = qlt_find_host_by_d_id(vha,
357 atio->u.isp24.fcp_hdr.d_id);
358 if (unlikely(NULL == host)) {
359 ql_dbg(ql_dbg_tgt, vha, 0xe03e,
360 "qla_target(%d): Received ATIO_TYPE7 "
361 "with unknown d_id %x:%x:%x\n", vha->vp_idx,
362 atio->u.isp24.fcp_hdr.d_id[0],
363 atio->u.isp24.fcp_hdr.d_id[1],
364 atio->u.isp24.fcp_hdr.d_id[2]);
367 qlt_queue_unknown_atio(vha, atio, ha_locked);
368 break;
370 if (unlikely(!list_empty(&vha->unknown_atio_list)))
371 qlt_try_to_dequeue_unknown_atios(vha, ha_locked);
373 qlt_24xx_atio_pkt(host, atio, ha_locked);
374 break;
377 case IMMED_NOTIFY_TYPE:
379 struct scsi_qla_host *host = vha;
380 struct imm_ntfy_from_isp *entry =
381 (struct imm_ntfy_from_isp *)atio;
383 if ((entry->u.isp24.vp_index != 0xFF) &&
384 (entry->u.isp24.nport_handle != 0xFFFF)) {
385 host = qlt_find_host_by_vp_idx(vha,
386 entry->u.isp24.vp_index);
387 if (unlikely(!host)) {
388 ql_dbg(ql_dbg_tgt, vha, 0xe03f,
389 "qla_target(%d): Received "
390 "ATIO (IMMED_NOTIFY_TYPE) "
391 "with unknown vp_index %d\n",
392 vha->vp_idx, entry->u.isp24.vp_index);
393 break;
396 qlt_24xx_atio_pkt(host, atio, ha_locked);
397 break;
400 case VP_RPT_ID_IOCB_TYPE:
401 qla24xx_report_id_acquisition(vha,
402 (struct vp_rpt_id_entry_24xx *)atio);
403 break;
405 case ABTS_RECV_24XX:
407 struct abts_recv_from_24xx *entry =
408 (struct abts_recv_from_24xx *)atio;
409 struct scsi_qla_host *host = qlt_find_host_by_vp_idx(vha,
410 entry->vp_index);
411 unsigned long flags;
413 if (unlikely(!host)) {
414 ql_dbg(ql_dbg_tgt, vha, 0xffff,
415 "qla_target(%d): Response pkt (ABTS_RECV_24XX) "
416 "received, with unknown vp_index %d\n",
417 vha->vp_idx, entry->vp_index);
418 break;
420 if (!ha_locked)
421 spin_lock_irqsave(&host->hw->hardware_lock, flags);
422 qlt_24xx_handle_abts(host, (struct abts_recv_from_24xx *)atio);
423 if (!ha_locked)
424 spin_unlock_irqrestore(&host->hw->hardware_lock, flags);
425 break;
428 /* case PUREX_IOCB_TYPE: ql2xmvasynctoatio */
430 default:
431 ql_dbg(ql_dbg_tgt, vha, 0xe040,
432 "qla_target(%d): Received unknown ATIO atio "
433 "type %x\n", vha->vp_idx, atio->u.raw.entry_type);
434 break;
437 return false;
440 void qlt_response_pkt_all_vps(struct scsi_qla_host *vha, response_t *pkt)
442 switch (pkt->entry_type) {
443 case CTIO_CRC2:
444 ql_dbg(ql_dbg_tgt, vha, 0xe073,
445 "qla_target(%d):%s: CRC2 Response pkt\n",
446 vha->vp_idx, __func__);
447 case CTIO_TYPE7:
449 struct ctio7_from_24xx *entry = (struct ctio7_from_24xx *)pkt;
450 struct scsi_qla_host *host = qlt_find_host_by_vp_idx(vha,
451 entry->vp_index);
452 if (unlikely(!host)) {
453 ql_dbg(ql_dbg_tgt, vha, 0xe041,
454 "qla_target(%d): Response pkt (CTIO_TYPE7) "
455 "received, with unknown vp_index %d\n",
456 vha->vp_idx, entry->vp_index);
457 break;
459 qlt_response_pkt(host, pkt);
460 break;
463 case IMMED_NOTIFY_TYPE:
465 struct scsi_qla_host *host = vha;
466 struct imm_ntfy_from_isp *entry =
467 (struct imm_ntfy_from_isp *)pkt;
469 host = qlt_find_host_by_vp_idx(vha, entry->u.isp24.vp_index);
470 if (unlikely(!host)) {
471 ql_dbg(ql_dbg_tgt, vha, 0xe042,
472 "qla_target(%d): Response pkt (IMMED_NOTIFY_TYPE) "
473 "received, with unknown vp_index %d\n",
474 vha->vp_idx, entry->u.isp24.vp_index);
475 break;
477 qlt_response_pkt(host, pkt);
478 break;
481 case NOTIFY_ACK_TYPE:
483 struct scsi_qla_host *host = vha;
484 struct nack_to_isp *entry = (struct nack_to_isp *)pkt;
486 if (0xFF != entry->u.isp24.vp_index) {
487 host = qlt_find_host_by_vp_idx(vha,
488 entry->u.isp24.vp_index);
489 if (unlikely(!host)) {
490 ql_dbg(ql_dbg_tgt, vha, 0xe043,
491 "qla_target(%d): Response "
492 "pkt (NOTIFY_ACK_TYPE) "
493 "received, with unknown "
494 "vp_index %d\n", vha->vp_idx,
495 entry->u.isp24.vp_index);
496 break;
499 qlt_response_pkt(host, pkt);
500 break;
503 case ABTS_RECV_24XX:
505 struct abts_recv_from_24xx *entry =
506 (struct abts_recv_from_24xx *)pkt;
507 struct scsi_qla_host *host = qlt_find_host_by_vp_idx(vha,
508 entry->vp_index);
509 if (unlikely(!host)) {
510 ql_dbg(ql_dbg_tgt, vha, 0xe044,
511 "qla_target(%d): Response pkt "
512 "(ABTS_RECV_24XX) received, with unknown "
513 "vp_index %d\n", vha->vp_idx, entry->vp_index);
514 break;
516 qlt_response_pkt(host, pkt);
517 break;
520 case ABTS_RESP_24XX:
522 struct abts_resp_to_24xx *entry =
523 (struct abts_resp_to_24xx *)pkt;
524 struct scsi_qla_host *host = qlt_find_host_by_vp_idx(vha,
525 entry->vp_index);
526 if (unlikely(!host)) {
527 ql_dbg(ql_dbg_tgt, vha, 0xe045,
528 "qla_target(%d): Response pkt "
529 "(ABTS_RECV_24XX) received, with unknown "
530 "vp_index %d\n", vha->vp_idx, entry->vp_index);
531 break;
533 qlt_response_pkt(host, pkt);
534 break;
537 default:
538 qlt_response_pkt(vha, pkt);
539 break;
545 * All qlt_plogi_ack_t operations are protected by hardware_lock
547 static int qla24xx_post_nack_work(struct scsi_qla_host *vha, fc_port_t *fcport,
548 struct imm_ntfy_from_isp *ntfy, int type)
550 struct qla_work_evt *e;
551 e = qla2x00_alloc_work(vha, QLA_EVT_NACK);
552 if (!e)
553 return QLA_FUNCTION_FAILED;
555 e->u.nack.fcport = fcport;
556 e->u.nack.type = type;
557 memcpy(e->u.nack.iocb, ntfy, sizeof(struct imm_ntfy_from_isp));
558 return qla2x00_post_work(vha, e);
561 static
562 void qla2x00_async_nack_sp_done(void *s, int res)
564 struct srb *sp = (struct srb *)s;
565 struct scsi_qla_host *vha = sp->vha;
566 unsigned long flags;
568 ql_dbg(ql_dbg_disc, vha, 0xffff,
569 "Async done-%s res %x %8phC type %d\n",
570 sp->name, res, sp->fcport->port_name, sp->type);
572 spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
573 sp->fcport->flags &= ~FCF_ASYNC_SENT;
574 sp->fcport->chip_reset = vha->hw->chip_reset;
576 switch (sp->type) {
577 case SRB_NACK_PLOGI:
578 sp->fcport->login_gen++;
579 sp->fcport->fw_login_state = DSC_LS_PLOGI_COMP;
580 sp->fcport->logout_on_delete = 1;
581 sp->fcport->plogi_nack_done_deadline = jiffies + HZ;
582 break;
584 case SRB_NACK_PRLI:
585 sp->fcport->fw_login_state = DSC_LS_PRLI_COMP;
586 sp->fcport->deleted = 0;
588 if (!sp->fcport->login_succ &&
589 !IS_SW_RESV_ADDR(sp->fcport->d_id)) {
590 sp->fcport->login_succ = 1;
592 vha->fcport_count++;
594 if (!IS_IIDMA_CAPABLE(vha->hw) ||
595 !vha->hw->flags.gpsc_supported) {
596 ql_dbg(ql_dbg_disc, vha, 0xffff,
597 "%s %d %8phC post upd_fcport fcp_cnt %d\n",
598 __func__, __LINE__,
599 sp->fcport->port_name,
600 vha->fcport_count);
602 qla24xx_post_upd_fcport_work(vha, sp->fcport);
603 } else {
604 ql_dbg(ql_dbg_disc, vha, 0xffff,
605 "%s %d %8phC post gpsc fcp_cnt %d\n",
606 __func__, __LINE__,
607 sp->fcport->port_name,
608 vha->fcport_count);
610 qla24xx_post_gpsc_work(vha, sp->fcport);
613 break;
615 case SRB_NACK_LOGO:
616 sp->fcport->login_gen++;
617 sp->fcport->fw_login_state = DSC_LS_PORT_UNAVAIL;
618 qlt_logo_completion_handler(sp->fcport, MBS_COMMAND_COMPLETE);
619 break;
621 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
623 sp->free(sp);
626 int qla24xx_async_notify_ack(scsi_qla_host_t *vha, fc_port_t *fcport,
627 struct imm_ntfy_from_isp *ntfy, int type)
629 int rval = QLA_FUNCTION_FAILED;
630 srb_t *sp;
631 char *c = NULL;
633 fcport->flags |= FCF_ASYNC_SENT;
634 switch (type) {
635 case SRB_NACK_PLOGI:
636 fcport->fw_login_state = DSC_LS_PLOGI_PEND;
637 c = "PLOGI";
638 break;
639 case SRB_NACK_PRLI:
640 fcport->fw_login_state = DSC_LS_PRLI_PEND;
641 fcport->deleted = 0;
642 c = "PRLI";
643 break;
644 case SRB_NACK_LOGO:
645 fcport->fw_login_state = DSC_LS_LOGO_PEND;
646 c = "LOGO";
647 break;
650 sp = qla2x00_get_sp(vha, fcport, GFP_ATOMIC);
651 if (!sp)
652 goto done;
654 sp->type = type;
655 sp->name = "nack";
657 qla2x00_init_timer(sp, qla2x00_get_async_timeout(vha)+2);
659 sp->u.iocb_cmd.u.nack.ntfy = ntfy;
661 sp->done = qla2x00_async_nack_sp_done;
663 rval = qla2x00_start_sp(sp);
664 if (rval != QLA_SUCCESS)
665 goto done_free_sp;
667 ql_dbg(ql_dbg_disc, vha, 0xffff,
668 "Async-%s %8phC hndl %x %s\n",
669 sp->name, fcport->port_name, sp->handle, c);
671 return rval;
673 done_free_sp:
674 sp->free(sp);
675 done:
676 fcport->flags &= ~FCF_ASYNC_SENT;
677 return rval;
680 void qla24xx_do_nack_work(struct scsi_qla_host *vha, struct qla_work_evt *e)
682 fc_port_t *t;
683 unsigned long flags;
685 switch (e->u.nack.type) {
686 case SRB_NACK_PRLI:
687 mutex_lock(&vha->vha_tgt.tgt_mutex);
688 t = qlt_create_sess(vha, e->u.nack.fcport, 0);
689 mutex_unlock(&vha->vha_tgt.tgt_mutex);
690 if (t) {
691 ql_log(ql_log_info, vha, 0xffff,
692 "%s create sess success %p", __func__, t);
693 spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
694 /* create sess has an extra kref */
695 vha->hw->tgt.tgt_ops->put_sess(e->u.nack.fcport);
696 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
698 break;
700 qla24xx_async_notify_ack(vha, e->u.nack.fcport,
701 (struct imm_ntfy_from_isp*)e->u.nack.iocb, e->u.nack.type);
704 void qla24xx_delete_sess_fn(struct work_struct *work)
706 fc_port_t *fcport = container_of(work, struct fc_port, del_work);
707 struct qla_hw_data *ha = fcport->vha->hw;
708 unsigned long flags;
710 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
712 if (fcport->se_sess) {
713 ha->tgt.tgt_ops->shutdown_sess(fcport);
714 ha->tgt.tgt_ops->put_sess(fcport);
715 } else {
716 qlt_unreg_sess(fcport);
718 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
722 * Called from qla2x00_reg_remote_port()
724 void qlt_fc_port_added(struct scsi_qla_host *vha, fc_port_t *fcport)
726 struct qla_hw_data *ha = vha->hw;
727 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
728 struct fc_port *sess = fcport;
729 unsigned long flags;
731 if (!vha->hw->tgt.tgt_ops)
732 return;
734 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
735 if (tgt->tgt_stop) {
736 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
737 return;
740 if (fcport->disc_state == DSC_DELETE_PEND) {
741 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
742 return;
745 if (!sess->se_sess) {
746 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
748 mutex_lock(&vha->vha_tgt.tgt_mutex);
749 sess = qlt_create_sess(vha, fcport, false);
750 mutex_unlock(&vha->vha_tgt.tgt_mutex);
752 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
753 } else {
754 if (fcport->fw_login_state == DSC_LS_PRLI_COMP) {
755 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
756 return;
759 if (!kref_get_unless_zero(&sess->sess_kref)) {
760 ql_dbg(ql_dbg_disc, vha, 0xffff,
761 "%s: kref_get fail sess %8phC \n",
762 __func__, sess->port_name);
763 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
764 return;
767 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04c,
768 "qla_target(%u): %ssession for port %8phC "
769 "(loop ID %d) reappeared\n", vha->vp_idx,
770 sess->local ? "local " : "", sess->port_name, sess->loop_id);
772 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf007,
773 "Reappeared sess %p\n", sess);
775 ha->tgt.tgt_ops->update_sess(sess, fcport->d_id,
776 fcport->loop_id,
777 (fcport->flags & FCF_CONF_COMP_SUPPORTED));
780 if (sess && sess->local) {
781 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04d,
782 "qla_target(%u): local session for "
783 "port %8phC (loop ID %d) became global\n", vha->vp_idx,
784 fcport->port_name, sess->loop_id);
785 sess->local = 0;
787 ha->tgt.tgt_ops->put_sess(sess);
788 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
792 * This is a zero-base ref-counting solution, since hardware_lock
793 * guarantees that ref_count is not modified concurrently.
794 * Upon successful return content of iocb is undefined
796 static struct qlt_plogi_ack_t *
797 qlt_plogi_ack_find_add(struct scsi_qla_host *vha, port_id_t *id,
798 struct imm_ntfy_from_isp *iocb)
800 struct qlt_plogi_ack_t *pla;
802 list_for_each_entry(pla, &vha->plogi_ack_list, list) {
803 if (pla->id.b24 == id->b24) {
804 qlt_send_term_imm_notif(vha, &pla->iocb, 1);
805 memcpy(&pla->iocb, iocb, sizeof(pla->iocb));
806 return pla;
810 pla = kmem_cache_zalloc(qla_tgt_plogi_cachep, GFP_ATOMIC);
811 if (!pla) {
812 ql_dbg(ql_dbg_async, vha, 0x5088,
813 "qla_target(%d): Allocation of plogi_ack failed\n",
814 vha->vp_idx);
815 return NULL;
818 memcpy(&pla->iocb, iocb, sizeof(pla->iocb));
819 pla->id = *id;
820 list_add_tail(&pla->list, &vha->plogi_ack_list);
822 return pla;
825 void qlt_plogi_ack_unref(struct scsi_qla_host *vha,
826 struct qlt_plogi_ack_t *pla)
828 struct imm_ntfy_from_isp *iocb = &pla->iocb;
829 port_id_t port_id;
830 uint16_t loop_id;
831 fc_port_t *fcport = pla->fcport;
833 BUG_ON(!pla->ref_count);
834 pla->ref_count--;
836 if (pla->ref_count)
837 return;
839 ql_dbg(ql_dbg_disc, vha, 0x5089,
840 "Sending PLOGI ACK to wwn %8phC s_id %02x:%02x:%02x loop_id %#04x"
841 " exch %#x ox_id %#x\n", iocb->u.isp24.port_name,
842 iocb->u.isp24.port_id[2], iocb->u.isp24.port_id[1],
843 iocb->u.isp24.port_id[0],
844 le16_to_cpu(iocb->u.isp24.nport_handle),
845 iocb->u.isp24.exchange_address, iocb->ox_id);
847 port_id.b.domain = iocb->u.isp24.port_id[2];
848 port_id.b.area = iocb->u.isp24.port_id[1];
849 port_id.b.al_pa = iocb->u.isp24.port_id[0];
850 port_id.b.rsvd_1 = 0;
852 loop_id = le16_to_cpu(iocb->u.isp24.nport_handle);
854 fcport->loop_id = loop_id;
855 fcport->d_id = port_id;
856 qla24xx_post_nack_work(vha, fcport, iocb, SRB_NACK_PLOGI);
858 list_for_each_entry(fcport, &vha->vp_fcports, list) {
859 if (fcport->plogi_link[QLT_PLOGI_LINK_SAME_WWN] == pla)
860 fcport->plogi_link[QLT_PLOGI_LINK_SAME_WWN] = NULL;
861 if (fcport->plogi_link[QLT_PLOGI_LINK_CONFLICT] == pla)
862 fcport->plogi_link[QLT_PLOGI_LINK_CONFLICT] = NULL;
865 list_del(&pla->list);
866 kmem_cache_free(qla_tgt_plogi_cachep, pla);
869 void
870 qlt_plogi_ack_link(struct scsi_qla_host *vha, struct qlt_plogi_ack_t *pla,
871 struct fc_port *sess, enum qlt_plogi_link_t link)
873 struct imm_ntfy_from_isp *iocb = &pla->iocb;
874 /* Inc ref_count first because link might already be pointing at pla */
875 pla->ref_count++;
877 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf097,
878 "Linking sess %p [%d] wwn %8phC with PLOGI ACK to wwn %8phC"
879 " s_id %02x:%02x:%02x, ref=%d pla %p link %d\n",
880 sess, link, sess->port_name,
881 iocb->u.isp24.port_name, iocb->u.isp24.port_id[2],
882 iocb->u.isp24.port_id[1], iocb->u.isp24.port_id[0],
883 pla->ref_count, pla, link);
885 if (sess->plogi_link[link])
886 qlt_plogi_ack_unref(vha, sess->plogi_link[link]);
888 if (link == QLT_PLOGI_LINK_SAME_WWN)
889 pla->fcport = sess;
891 sess->plogi_link[link] = pla;
894 typedef struct {
895 /* These fields must be initialized by the caller */
896 port_id_t id;
898 * number of cmds dropped while we were waiting for
899 * initiator to ack LOGO initialize to 1 if LOGO is
900 * triggered by a command, otherwise, to 0
902 int cmd_count;
904 /* These fields are used by callee */
905 struct list_head list;
906 } qlt_port_logo_t;
908 static void
909 qlt_send_first_logo(struct scsi_qla_host *vha, qlt_port_logo_t *logo)
911 qlt_port_logo_t *tmp;
912 int res;
914 mutex_lock(&vha->vha_tgt.tgt_mutex);
916 list_for_each_entry(tmp, &vha->logo_list, list) {
917 if (tmp->id.b24 == logo->id.b24) {
918 tmp->cmd_count += logo->cmd_count;
919 mutex_unlock(&vha->vha_tgt.tgt_mutex);
920 return;
924 list_add_tail(&logo->list, &vha->logo_list);
926 mutex_unlock(&vha->vha_tgt.tgt_mutex);
928 res = qla24xx_els_dcmd_iocb(vha, ELS_DCMD_LOGO, logo->id);
930 mutex_lock(&vha->vha_tgt.tgt_mutex);
931 list_del(&logo->list);
932 mutex_unlock(&vha->vha_tgt.tgt_mutex);
934 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf098,
935 "Finished LOGO to %02x:%02x:%02x, dropped %d cmds, res = %#x\n",
936 logo->id.b.domain, logo->id.b.area, logo->id.b.al_pa,
937 logo->cmd_count, res);
940 static void qlt_free_session_done(struct work_struct *work)
942 struct fc_port *sess = container_of(work, struct fc_port,
943 free_work);
944 struct qla_tgt *tgt = sess->tgt;
945 struct scsi_qla_host *vha = sess->vha;
946 struct qla_hw_data *ha = vha->hw;
947 unsigned long flags;
948 bool logout_started = false;
949 struct event_arg ea;
950 scsi_qla_host_t *base_vha;
952 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf084,
953 "%s: se_sess %p / sess %p from port %8phC loop_id %#04x"
954 " s_id %02x:%02x:%02x logout %d keep %d els_logo %d\n",
955 __func__, sess->se_sess, sess, sess->port_name, sess->loop_id,
956 sess->d_id.b.domain, sess->d_id.b.area, sess->d_id.b.al_pa,
957 sess->logout_on_delete, sess->keep_nport_handle,
958 sess->send_els_logo);
961 if (!IS_SW_RESV_ADDR(sess->d_id)) {
962 if (sess->send_els_logo) {
963 qlt_port_logo_t logo;
965 logo.id = sess->d_id;
966 logo.cmd_count = 0;
967 qlt_send_first_logo(vha, &logo);
970 if (sess->logout_on_delete) {
971 int rc;
973 rc = qla2x00_post_async_logout_work(vha, sess, NULL);
974 if (rc != QLA_SUCCESS)
975 ql_log(ql_log_warn, vha, 0xf085,
976 "Schedule logo failed sess %p rc %d\n",
977 sess, rc);
978 else
979 logout_started = true;
984 * Release the target session for FC Nexus from fabric module code.
986 if (sess->se_sess != NULL)
987 ha->tgt.tgt_ops->free_session(sess);
989 if (logout_started) {
990 bool traced = false;
992 while (!ACCESS_ONCE(sess->logout_completed)) {
993 if (!traced) {
994 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf086,
995 "%s: waiting for sess %p logout\n",
996 __func__, sess);
997 traced = true;
999 msleep(100);
1002 ql_dbg(ql_dbg_disc, vha, 0xf087,
1003 "%s: sess %p logout completed\n",__func__, sess);
1006 if (sess->logo_ack_needed) {
1007 sess->logo_ack_needed = 0;
1008 qla24xx_async_notify_ack(vha, sess,
1009 (struct imm_ntfy_from_isp *)sess->iocb, SRB_NACK_LOGO);
1012 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
1013 if (sess->se_sess) {
1014 sess->se_sess = NULL;
1015 if (tgt && !IS_SW_RESV_ADDR(sess->d_id))
1016 tgt->sess_count--;
1019 sess->disc_state = DSC_DELETED;
1020 sess->fw_login_state = DSC_LS_PORT_UNAVAIL;
1021 sess->deleted = QLA_SESS_DELETED;
1022 sess->login_retry = vha->hw->login_retry_count;
1024 if (sess->login_succ && !IS_SW_RESV_ADDR(sess->d_id)) {
1025 vha->fcport_count--;
1026 sess->login_succ = 0;
1029 if (sess->chip_reset != sess->vha->hw->chip_reset)
1030 qla2x00_clear_loop_id(sess);
1032 if (sess->conflict) {
1033 sess->conflict->login_pause = 0;
1034 sess->conflict = NULL;
1035 if (!test_bit(UNLOADING, &vha->dpc_flags))
1036 set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
1040 struct qlt_plogi_ack_t *own =
1041 sess->plogi_link[QLT_PLOGI_LINK_SAME_WWN];
1042 struct qlt_plogi_ack_t *con =
1043 sess->plogi_link[QLT_PLOGI_LINK_CONFLICT];
1044 struct imm_ntfy_from_isp *iocb;
1046 if (con) {
1047 iocb = &con->iocb;
1048 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf099,
1049 "se_sess %p / sess %p port %8phC is gone,"
1050 " %s (ref=%d), releasing PLOGI for %8phC (ref=%d)\n",
1051 sess->se_sess, sess, sess->port_name,
1052 own ? "releasing own PLOGI" : "no own PLOGI pending",
1053 own ? own->ref_count : -1,
1054 iocb->u.isp24.port_name, con->ref_count);
1055 qlt_plogi_ack_unref(vha, con);
1056 sess->plogi_link[QLT_PLOGI_LINK_CONFLICT] = NULL;
1057 } else {
1058 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf09a,
1059 "se_sess %p / sess %p port %8phC is gone, %s (ref=%d)\n",
1060 sess->se_sess, sess, sess->port_name,
1061 own ? "releasing own PLOGI" :
1062 "no own PLOGI pending",
1063 own ? own->ref_count : -1);
1066 if (own) {
1067 sess->fw_login_state = DSC_LS_PLOGI_PEND;
1068 qlt_plogi_ack_unref(vha, own);
1069 sess->plogi_link[QLT_PLOGI_LINK_SAME_WWN] = NULL;
1072 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
1074 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf001,
1075 "Unregistration of sess %p %8phC finished fcp_cnt %d\n",
1076 sess, sess->port_name, vha->fcport_count);
1078 if (tgt && (tgt->sess_count == 0))
1079 wake_up_all(&tgt->waitQ);
1081 if (vha->fcport_count == 0)
1082 wake_up_all(&vha->fcport_waitQ);
1084 base_vha = pci_get_drvdata(ha->pdev);
1085 if (test_bit(PFLG_DRIVER_REMOVING, &base_vha->pci_flags))
1086 return;
1088 if (!tgt || !tgt->tgt_stop) {
1089 memset(&ea, 0, sizeof(ea));
1090 ea.event = FCME_DELETE_DONE;
1091 ea.fcport = sess;
1092 qla2x00_fcport_event_handler(vha, &ea);
1096 /* ha->tgt.sess_lock supposed to be held on entry */
1097 void qlt_unreg_sess(struct fc_port *sess)
1099 struct scsi_qla_host *vha = sess->vha;
1101 ql_dbg(ql_dbg_disc, sess->vha, 0xffff,
1102 "%s sess %p for deletion %8phC\n",
1103 __func__, sess, sess->port_name);
1105 if (sess->se_sess)
1106 vha->hw->tgt.tgt_ops->clear_nacl_from_fcport_map(sess);
1108 qla2x00_mark_device_lost(vha, sess, 1, 1);
1110 sess->deleted = QLA_SESS_DELETION_IN_PROGRESS;
1111 sess->disc_state = DSC_DELETE_PEND;
1112 sess->last_rscn_gen = sess->rscn_gen;
1113 sess->last_login_gen = sess->login_gen;
1115 INIT_WORK(&sess->free_work, qlt_free_session_done);
1116 schedule_work(&sess->free_work);
1118 EXPORT_SYMBOL(qlt_unreg_sess);
1120 static int qlt_reset(struct scsi_qla_host *vha, void *iocb, int mcmd)
1122 struct qla_hw_data *ha = vha->hw;
1123 struct fc_port *sess = NULL;
1124 uint16_t loop_id;
1125 int res = 0;
1126 struct imm_ntfy_from_isp *n = (struct imm_ntfy_from_isp *)iocb;
1127 unsigned long flags;
1129 loop_id = le16_to_cpu(n->u.isp24.nport_handle);
1130 if (loop_id == 0xFFFF) {
1131 /* Global event */
1132 atomic_inc(&vha->vha_tgt.qla_tgt->tgt_global_resets_count);
1133 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
1134 qlt_clear_tgt_db(vha->vha_tgt.qla_tgt);
1135 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
1136 } else {
1137 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
1138 sess = ha->tgt.tgt_ops->find_sess_by_loop_id(vha, loop_id);
1139 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
1142 ql_dbg(ql_dbg_tgt, vha, 0xe000,
1143 "Using sess for qla_tgt_reset: %p\n", sess);
1144 if (!sess) {
1145 res = -ESRCH;
1146 return res;
1149 ql_dbg(ql_dbg_tgt, vha, 0xe047,
1150 "scsi(%ld): resetting (session %p from port %8phC mcmd %x, "
1151 "loop_id %d)\n", vha->host_no, sess, sess->port_name,
1152 mcmd, loop_id);
1154 return qlt_issue_task_mgmt(sess, 0, mcmd, iocb, QLA24XX_MGMT_SEND_NACK);
1157 static void qla24xx_chk_fcp_state(struct fc_port *sess)
1159 if (sess->chip_reset != sess->vha->hw->chip_reset) {
1160 sess->logout_on_delete = 0;
1161 sess->logo_ack_needed = 0;
1162 sess->fw_login_state = DSC_LS_PORT_UNAVAIL;
1163 sess->scan_state = 0;
1167 /* ha->tgt.sess_lock supposed to be held on entry */
1168 void qlt_schedule_sess_for_deletion(struct fc_port *sess,
1169 bool immediate)
1171 struct qla_tgt *tgt = sess->tgt;
1173 if (sess->disc_state == DSC_DELETE_PEND)
1174 return;
1176 if (sess->disc_state == DSC_DELETED) {
1177 if (tgt && tgt->tgt_stop && (tgt->sess_count == 0))
1178 wake_up_all(&tgt->waitQ);
1179 if (sess->vha->fcport_count == 0)
1180 wake_up_all(&sess->vha->fcport_waitQ);
1182 if (!sess->plogi_link[QLT_PLOGI_LINK_SAME_WWN] &&
1183 !sess->plogi_link[QLT_PLOGI_LINK_CONFLICT])
1184 return;
1187 sess->disc_state = DSC_DELETE_PEND;
1189 if (sess->deleted == QLA_SESS_DELETED)
1190 sess->logout_on_delete = 0;
1192 sess->deleted = QLA_SESS_DELETION_IN_PROGRESS;
1193 qla24xx_chk_fcp_state(sess);
1195 ql_dbg(ql_dbg_tgt, sess->vha, 0xe001,
1196 "Scheduling sess %p for deletion\n", sess);
1198 schedule_work(&sess->del_work);
1201 void qlt_schedule_sess_for_deletion_lock(struct fc_port *sess)
1203 unsigned long flags;
1204 struct qla_hw_data *ha = sess->vha->hw;
1205 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
1206 qlt_schedule_sess_for_deletion(sess, 1);
1207 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
1210 /* ha->tgt.sess_lock supposed to be held on entry */
1211 static void qlt_clear_tgt_db(struct qla_tgt *tgt)
1213 struct fc_port *sess;
1214 scsi_qla_host_t *vha = tgt->vha;
1216 list_for_each_entry(sess, &vha->vp_fcports, list) {
1217 if (sess->se_sess)
1218 qlt_schedule_sess_for_deletion(sess, 1);
1221 /* At this point tgt could be already dead */
1224 static int qla24xx_get_loop_id(struct scsi_qla_host *vha, const uint8_t *s_id,
1225 uint16_t *loop_id)
1227 struct qla_hw_data *ha = vha->hw;
1228 dma_addr_t gid_list_dma;
1229 struct gid_list_info *gid_list;
1230 char *id_iter;
1231 int res, rc, i;
1232 uint16_t entries;
1234 gid_list = dma_alloc_coherent(&ha->pdev->dev, qla2x00_gid_list_size(ha),
1235 &gid_list_dma, GFP_KERNEL);
1236 if (!gid_list) {
1237 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf044,
1238 "qla_target(%d): DMA Alloc failed of %u\n",
1239 vha->vp_idx, qla2x00_gid_list_size(ha));
1240 return -ENOMEM;
1243 /* Get list of logged in devices */
1244 rc = qla24xx_gidlist_wait(vha, gid_list, gid_list_dma, &entries);
1245 if (rc != QLA_SUCCESS) {
1246 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf045,
1247 "qla_target(%d): get_id_list() failed: %x\n",
1248 vha->vp_idx, rc);
1249 res = -EBUSY;
1250 goto out_free_id_list;
1253 id_iter = (char *)gid_list;
1254 res = -ENOENT;
1255 for (i = 0; i < entries; i++) {
1256 struct gid_list_info *gid = (struct gid_list_info *)id_iter;
1257 if ((gid->al_pa == s_id[2]) &&
1258 (gid->area == s_id[1]) &&
1259 (gid->domain == s_id[0])) {
1260 *loop_id = le16_to_cpu(gid->loop_id);
1261 res = 0;
1262 break;
1264 id_iter += ha->gid_list_info_size;
1267 out_free_id_list:
1268 dma_free_coherent(&ha->pdev->dev, qla2x00_gid_list_size(ha),
1269 gid_list, gid_list_dma);
1270 return res;
1274 * Adds an extra ref to allow to drop hw lock after adding sess to the list.
1275 * Caller must put it.
1277 static struct fc_port *qlt_create_sess(
1278 struct scsi_qla_host *vha,
1279 fc_port_t *fcport,
1280 bool local)
1282 struct qla_hw_data *ha = vha->hw;
1283 struct fc_port *sess = fcport;
1284 unsigned long flags;
1286 if (vha->vha_tgt.qla_tgt->tgt_stop)
1287 return NULL;
1289 if (fcport->se_sess) {
1290 if (!kref_get_unless_zero(&sess->sess_kref)) {
1291 ql_dbg(ql_dbg_disc, vha, 0xffff,
1292 "%s: kref_get_unless_zero failed for %8phC\n",
1293 __func__, sess->port_name);
1294 return NULL;
1296 return fcport;
1298 sess->tgt = vha->vha_tgt.qla_tgt;
1299 sess->local = local;
1302 * Under normal circumstances we want to logout from firmware when
1303 * session eventually ends and release corresponding nport handle.
1304 * In the exception cases (e.g. when new PLOGI is waiting) corresponding
1305 * code will adjust these flags as necessary.
1307 sess->logout_on_delete = 1;
1308 sess->keep_nport_handle = 0;
1309 sess->logout_completed = 0;
1311 if (ha->tgt.tgt_ops->check_initiator_node_acl(vha,
1312 &fcport->port_name[0], sess) < 0) {
1313 ql_dbg(ql_dbg_tgt_mgt, vha, 0xffff,
1314 "(%d) %8phC check_initiator_node_acl failed\n",
1315 vha->vp_idx, fcport->port_name);
1316 return NULL;
1317 } else {
1318 kref_init(&fcport->sess_kref);
1320 * Take an extra reference to ->sess_kref here to handle
1321 * fc_port access across ->tgt.sess_lock reaquire.
1323 if (!kref_get_unless_zero(&sess->sess_kref)) {
1324 ql_dbg(ql_dbg_disc, vha, 0xffff,
1325 "%s: kref_get_unless_zero failed for %8phC\n",
1326 __func__, sess->port_name);
1327 return NULL;
1330 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
1331 if (!IS_SW_RESV_ADDR(sess->d_id))
1332 vha->vha_tgt.qla_tgt->sess_count++;
1334 qlt_do_generation_tick(vha, &sess->generation);
1335 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
1338 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf006,
1339 "Adding sess %p se_sess %p to tgt %p sess_count %d\n",
1340 sess, sess->se_sess, vha->vha_tgt.qla_tgt,
1341 vha->vha_tgt.qla_tgt->sess_count);
1343 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04b,
1344 "qla_target(%d): %ssession for wwn %8phC (loop_id %d, "
1345 "s_id %x:%x:%x, confirmed completion %ssupported) added\n",
1346 vha->vp_idx, local ? "local " : "", fcport->port_name,
1347 fcport->loop_id, sess->d_id.b.domain, sess->d_id.b.area,
1348 sess->d_id.b.al_pa, sess->conf_compl_supported ? "" : "not ");
1350 return sess;
1354 * max_gen - specifies maximum session generation
1355 * at which this deletion requestion is still valid
1357 void
1358 qlt_fc_port_deleted(struct scsi_qla_host *vha, fc_port_t *fcport, int max_gen)
1360 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
1361 struct fc_port *sess = fcport;
1362 unsigned long flags;
1364 if (!vha->hw->tgt.tgt_ops)
1365 return;
1367 if (!tgt)
1368 return;
1370 spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
1371 if (tgt->tgt_stop) {
1372 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
1373 return;
1375 if (!sess->se_sess) {
1376 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
1377 return;
1380 if (max_gen - sess->generation < 0) {
1381 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
1382 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf092,
1383 "Ignoring stale deletion request for se_sess %p / sess %p"
1384 " for port %8phC, req_gen %d, sess_gen %d\n",
1385 sess->se_sess, sess, sess->port_name, max_gen,
1386 sess->generation);
1387 return;
1390 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf008, "qla_tgt_fc_port_deleted %p", sess);
1392 sess->local = 1;
1393 qlt_schedule_sess_for_deletion(sess, false);
1394 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
1397 static inline int test_tgt_sess_count(struct qla_tgt *tgt)
1399 struct qla_hw_data *ha = tgt->ha;
1400 unsigned long flags;
1401 int res;
1403 * We need to protect against race, when tgt is freed before or
1404 * inside wake_up()
1406 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
1407 ql_dbg(ql_dbg_tgt, tgt->vha, 0xe002,
1408 "tgt %p, sess_count=%d\n",
1409 tgt, tgt->sess_count);
1410 res = (tgt->sess_count == 0);
1411 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
1413 return res;
1416 /* Called by tcm_qla2xxx configfs code */
1417 int qlt_stop_phase1(struct qla_tgt *tgt)
1419 struct scsi_qla_host *vha = tgt->vha;
1420 struct qla_hw_data *ha = tgt->ha;
1421 unsigned long flags;
1423 mutex_lock(&qla_tgt_mutex);
1424 if (!vha->fc_vport) {
1425 struct Scsi_Host *sh = vha->host;
1426 struct fc_host_attrs *fc_host = shost_to_fc_host(sh);
1427 bool npiv_vports;
1429 spin_lock_irqsave(sh->host_lock, flags);
1430 npiv_vports = (fc_host->npiv_vports_inuse);
1431 spin_unlock_irqrestore(sh->host_lock, flags);
1433 if (npiv_vports) {
1434 mutex_unlock(&qla_tgt_mutex);
1435 return -EPERM;
1438 if (tgt->tgt_stop || tgt->tgt_stopped) {
1439 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04e,
1440 "Already in tgt->tgt_stop or tgt_stopped state\n");
1441 mutex_unlock(&qla_tgt_mutex);
1442 return -EPERM;
1445 ql_dbg(ql_dbg_tgt, vha, 0xe003, "Stopping target for host %ld(%p)\n",
1446 vha->host_no, vha);
1448 * Mutex needed to sync with qla_tgt_fc_port_[added,deleted].
1449 * Lock is needed, because we still can get an incoming packet.
1451 mutex_lock(&vha->vha_tgt.tgt_mutex);
1452 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
1453 tgt->tgt_stop = 1;
1454 qlt_clear_tgt_db(tgt);
1455 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
1456 mutex_unlock(&vha->vha_tgt.tgt_mutex);
1457 mutex_unlock(&qla_tgt_mutex);
1459 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf009,
1460 "Waiting for sess works (tgt %p)", tgt);
1461 spin_lock_irqsave(&tgt->sess_work_lock, flags);
1462 while (!list_empty(&tgt->sess_works_list)) {
1463 spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
1464 flush_scheduled_work();
1465 spin_lock_irqsave(&tgt->sess_work_lock, flags);
1467 spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
1469 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00a,
1470 "Waiting for tgt %p: sess_count=%d\n", tgt, tgt->sess_count);
1472 wait_event(tgt->waitQ, test_tgt_sess_count(tgt));
1474 /* Big hammer */
1475 if (!ha->flags.host_shutting_down &&
1476 (qla_tgt_mode_enabled(vha) || qla_dual_mode_enabled(vha)))
1477 qlt_disable_vha(vha);
1479 /* Wait for sessions to clear out (just in case) */
1480 wait_event(tgt->waitQ, test_tgt_sess_count(tgt));
1481 return 0;
1483 EXPORT_SYMBOL(qlt_stop_phase1);
1485 /* Called by tcm_qla2xxx configfs code */
1486 void qlt_stop_phase2(struct qla_tgt *tgt)
1488 struct qla_hw_data *ha = tgt->ha;
1489 scsi_qla_host_t *vha = pci_get_drvdata(ha->pdev);
1490 unsigned long flags;
1492 if (tgt->tgt_stopped) {
1493 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04f,
1494 "Already in tgt->tgt_stopped state\n");
1495 dump_stack();
1496 return;
1499 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00b,
1500 "Waiting for %d IRQ commands to complete (tgt %p)",
1501 tgt->irq_cmd_count, tgt);
1503 mutex_lock(&vha->vha_tgt.tgt_mutex);
1504 spin_lock_irqsave(&ha->hardware_lock, flags);
1505 while ((tgt->irq_cmd_count != 0) || (tgt->atio_irq_cmd_count != 0)) {
1506 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1507 udelay(2);
1508 spin_lock_irqsave(&ha->hardware_lock, flags);
1510 tgt->tgt_stop = 0;
1511 tgt->tgt_stopped = 1;
1512 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1513 mutex_unlock(&vha->vha_tgt.tgt_mutex);
1515 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00c, "Stop of tgt %p finished",
1516 tgt);
1518 EXPORT_SYMBOL(qlt_stop_phase2);
1520 /* Called from qlt_remove_target() -> qla2x00_remove_one() */
1521 static void qlt_release(struct qla_tgt *tgt)
1523 scsi_qla_host_t *vha = tgt->vha;
1525 if ((vha->vha_tgt.qla_tgt != NULL) && !tgt->tgt_stopped)
1526 qlt_stop_phase2(tgt);
1528 vha->vha_tgt.qla_tgt = NULL;
1530 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00d,
1531 "Release of tgt %p finished\n", tgt);
1533 kfree(tgt);
1536 /* ha->hardware_lock supposed to be held on entry */
1537 static int qlt_sched_sess_work(struct qla_tgt *tgt, int type,
1538 const void *param, unsigned int param_size)
1540 struct qla_tgt_sess_work_param *prm;
1541 unsigned long flags;
1543 prm = kzalloc(sizeof(*prm), GFP_ATOMIC);
1544 if (!prm) {
1545 ql_dbg(ql_dbg_tgt_mgt, tgt->vha, 0xf050,
1546 "qla_target(%d): Unable to create session "
1547 "work, command will be refused", 0);
1548 return -ENOMEM;
1551 ql_dbg(ql_dbg_tgt_mgt, tgt->vha, 0xf00e,
1552 "Scheduling work (type %d, prm %p)"
1553 " to find session for param %p (size %d, tgt %p)\n",
1554 type, prm, param, param_size, tgt);
1556 prm->type = type;
1557 memcpy(&prm->tm_iocb, param, param_size);
1559 spin_lock_irqsave(&tgt->sess_work_lock, flags);
1560 list_add_tail(&prm->sess_works_list_entry, &tgt->sess_works_list);
1561 spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
1563 schedule_work(&tgt->sess_work);
1565 return 0;
1569 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
1571 static void qlt_send_notify_ack(struct scsi_qla_host *vha,
1572 struct imm_ntfy_from_isp *ntfy,
1573 uint32_t add_flags, uint16_t resp_code, int resp_code_valid,
1574 uint16_t srr_flags, uint16_t srr_reject_code, uint8_t srr_explan)
1576 struct qla_hw_data *ha = vha->hw;
1577 request_t *pkt;
1578 struct nack_to_isp *nack;
1580 if (!ha->flags.fw_started)
1581 return;
1583 ql_dbg(ql_dbg_tgt, vha, 0xe004, "Sending NOTIFY_ACK (ha=%p)\n", ha);
1585 /* Send marker if required */
1586 if (qlt_issue_marker(vha, 1) != QLA_SUCCESS)
1587 return;
1589 pkt = (request_t *)qla2x00_alloc_iocbs(vha, NULL);
1590 if (!pkt) {
1591 ql_dbg(ql_dbg_tgt, vha, 0xe049,
1592 "qla_target(%d): %s failed: unable to allocate "
1593 "request packet\n", vha->vp_idx, __func__);
1594 return;
1597 if (vha->vha_tgt.qla_tgt != NULL)
1598 vha->vha_tgt.qla_tgt->notify_ack_expected++;
1600 pkt->entry_type = NOTIFY_ACK_TYPE;
1601 pkt->entry_count = 1;
1603 nack = (struct nack_to_isp *)pkt;
1604 nack->ox_id = ntfy->ox_id;
1606 nack->u.isp24.handle = QLA_TGT_SKIP_HANDLE;
1607 nack->u.isp24.nport_handle = ntfy->u.isp24.nport_handle;
1608 if (le16_to_cpu(ntfy->u.isp24.status) == IMM_NTFY_ELS) {
1609 nack->u.isp24.flags = ntfy->u.isp24.flags &
1610 cpu_to_le32(NOTIFY24XX_FLAGS_PUREX_IOCB);
1612 nack->u.isp24.srr_rx_id = ntfy->u.isp24.srr_rx_id;
1613 nack->u.isp24.status = ntfy->u.isp24.status;
1614 nack->u.isp24.status_subcode = ntfy->u.isp24.status_subcode;
1615 nack->u.isp24.fw_handle = ntfy->u.isp24.fw_handle;
1616 nack->u.isp24.exchange_address = ntfy->u.isp24.exchange_address;
1617 nack->u.isp24.srr_rel_offs = ntfy->u.isp24.srr_rel_offs;
1618 nack->u.isp24.srr_ui = ntfy->u.isp24.srr_ui;
1619 nack->u.isp24.srr_flags = cpu_to_le16(srr_flags);
1620 nack->u.isp24.srr_reject_code = srr_reject_code;
1621 nack->u.isp24.srr_reject_code_expl = srr_explan;
1622 nack->u.isp24.vp_index = ntfy->u.isp24.vp_index;
1624 ql_dbg(ql_dbg_tgt, vha, 0xe005,
1625 "qla_target(%d): Sending 24xx Notify Ack %d\n",
1626 vha->vp_idx, nack->u.isp24.status);
1628 /* Memory Barrier */
1629 wmb();
1630 qla2x00_start_iocbs(vha, vha->req);
1634 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
1636 static void qlt_24xx_send_abts_resp(struct scsi_qla_host *vha,
1637 struct abts_recv_from_24xx *abts, uint32_t status,
1638 bool ids_reversed)
1640 struct qla_hw_data *ha = vha->hw;
1641 struct abts_resp_to_24xx *resp;
1642 uint32_t f_ctl;
1643 uint8_t *p;
1645 ql_dbg(ql_dbg_tgt, vha, 0xe006,
1646 "Sending task mgmt ABTS response (ha=%p, atio=%p, status=%x\n",
1647 ha, abts, status);
1649 /* Send marker if required */
1650 if (qlt_issue_marker(vha, 1) != QLA_SUCCESS)
1651 return;
1653 resp = (struct abts_resp_to_24xx *)qla2x00_alloc_iocbs_ready(vha, NULL);
1654 if (!resp) {
1655 ql_dbg(ql_dbg_tgt, vha, 0xe04a,
1656 "qla_target(%d): %s failed: unable to allocate "
1657 "request packet", vha->vp_idx, __func__);
1658 return;
1661 resp->entry_type = ABTS_RESP_24XX;
1662 resp->entry_count = 1;
1663 resp->nport_handle = abts->nport_handle;
1664 resp->vp_index = vha->vp_idx;
1665 resp->sof_type = abts->sof_type;
1666 resp->exchange_address = abts->exchange_address;
1667 resp->fcp_hdr_le = abts->fcp_hdr_le;
1668 f_ctl = cpu_to_le32(F_CTL_EXCH_CONTEXT_RESP |
1669 F_CTL_LAST_SEQ | F_CTL_END_SEQ |
1670 F_CTL_SEQ_INITIATIVE);
1671 p = (uint8_t *)&f_ctl;
1672 resp->fcp_hdr_le.f_ctl[0] = *p++;
1673 resp->fcp_hdr_le.f_ctl[1] = *p++;
1674 resp->fcp_hdr_le.f_ctl[2] = *p;
1675 if (ids_reversed) {
1676 resp->fcp_hdr_le.d_id[0] = abts->fcp_hdr_le.d_id[0];
1677 resp->fcp_hdr_le.d_id[1] = abts->fcp_hdr_le.d_id[1];
1678 resp->fcp_hdr_le.d_id[2] = abts->fcp_hdr_le.d_id[2];
1679 resp->fcp_hdr_le.s_id[0] = abts->fcp_hdr_le.s_id[0];
1680 resp->fcp_hdr_le.s_id[1] = abts->fcp_hdr_le.s_id[1];
1681 resp->fcp_hdr_le.s_id[2] = abts->fcp_hdr_le.s_id[2];
1682 } else {
1683 resp->fcp_hdr_le.d_id[0] = abts->fcp_hdr_le.s_id[0];
1684 resp->fcp_hdr_le.d_id[1] = abts->fcp_hdr_le.s_id[1];
1685 resp->fcp_hdr_le.d_id[2] = abts->fcp_hdr_le.s_id[2];
1686 resp->fcp_hdr_le.s_id[0] = abts->fcp_hdr_le.d_id[0];
1687 resp->fcp_hdr_le.s_id[1] = abts->fcp_hdr_le.d_id[1];
1688 resp->fcp_hdr_le.s_id[2] = abts->fcp_hdr_le.d_id[2];
1690 resp->exchange_addr_to_abort = abts->exchange_addr_to_abort;
1691 if (status == FCP_TMF_CMPL) {
1692 resp->fcp_hdr_le.r_ctl = R_CTL_BASIC_LINK_SERV | R_CTL_B_ACC;
1693 resp->payload.ba_acct.seq_id_valid = SEQ_ID_INVALID;
1694 resp->payload.ba_acct.low_seq_cnt = 0x0000;
1695 resp->payload.ba_acct.high_seq_cnt = 0xFFFF;
1696 resp->payload.ba_acct.ox_id = abts->fcp_hdr_le.ox_id;
1697 resp->payload.ba_acct.rx_id = abts->fcp_hdr_le.rx_id;
1698 } else {
1699 resp->fcp_hdr_le.r_ctl = R_CTL_BASIC_LINK_SERV | R_CTL_B_RJT;
1700 resp->payload.ba_rjt.reason_code =
1701 BA_RJT_REASON_CODE_UNABLE_TO_PERFORM;
1702 /* Other bytes are zero */
1705 vha->vha_tgt.qla_tgt->abts_resp_expected++;
1707 /* Memory Barrier */
1708 wmb();
1709 qla2x00_start_iocbs(vha, vha->req);
1713 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
1715 static void qlt_24xx_retry_term_exchange(struct scsi_qla_host *vha,
1716 struct abts_resp_from_24xx_fw *entry)
1718 struct ctio7_to_24xx *ctio;
1720 ql_dbg(ql_dbg_tgt, vha, 0xe007,
1721 "Sending retry TERM EXCH CTIO7 (ha=%p)\n", vha->hw);
1722 /* Send marker if required */
1723 if (qlt_issue_marker(vha, 1) != QLA_SUCCESS)
1724 return;
1726 ctio = (struct ctio7_to_24xx *)qla2x00_alloc_iocbs_ready(vha, NULL);
1727 if (ctio == NULL) {
1728 ql_dbg(ql_dbg_tgt, vha, 0xe04b,
1729 "qla_target(%d): %s failed: unable to allocate "
1730 "request packet\n", vha->vp_idx, __func__);
1731 return;
1735 * We've got on entrance firmware's response on by us generated
1736 * ABTS response. So, in it ID fields are reversed.
1739 ctio->entry_type = CTIO_TYPE7;
1740 ctio->entry_count = 1;
1741 ctio->nport_handle = entry->nport_handle;
1742 ctio->handle = QLA_TGT_SKIP_HANDLE | CTIO_COMPLETION_HANDLE_MARK;
1743 ctio->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
1744 ctio->vp_index = vha->vp_idx;
1745 ctio->initiator_id[0] = entry->fcp_hdr_le.d_id[0];
1746 ctio->initiator_id[1] = entry->fcp_hdr_le.d_id[1];
1747 ctio->initiator_id[2] = entry->fcp_hdr_le.d_id[2];
1748 ctio->exchange_addr = entry->exchange_addr_to_abort;
1749 ctio->u.status1.flags = cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_1 |
1750 CTIO7_FLAGS_TERMINATE);
1751 ctio->u.status1.ox_id = cpu_to_le16(entry->fcp_hdr_le.ox_id);
1753 /* Memory Barrier */
1754 wmb();
1755 qla2x00_start_iocbs(vha, vha->req);
1757 qlt_24xx_send_abts_resp(vha, (struct abts_recv_from_24xx *)entry,
1758 FCP_TMF_CMPL, true);
1761 static int abort_cmd_for_tag(struct scsi_qla_host *vha, uint32_t tag)
1763 struct qla_tgt_sess_op *op;
1764 struct qla_tgt_cmd *cmd;
1766 spin_lock(&vha->cmd_list_lock);
1768 list_for_each_entry(op, &vha->qla_sess_op_cmd_list, cmd_list) {
1769 if (tag == op->atio.u.isp24.exchange_addr) {
1770 op->aborted = true;
1771 spin_unlock(&vha->cmd_list_lock);
1772 return 1;
1776 list_for_each_entry(op, &vha->unknown_atio_list, cmd_list) {
1777 if (tag == op->atio.u.isp24.exchange_addr) {
1778 op->aborted = true;
1779 spin_unlock(&vha->cmd_list_lock);
1780 return 1;
1784 list_for_each_entry(cmd, &vha->qla_cmd_list, cmd_list) {
1785 if (tag == cmd->atio.u.isp24.exchange_addr) {
1786 cmd->aborted = 1;
1787 spin_unlock(&vha->cmd_list_lock);
1788 return 1;
1792 spin_unlock(&vha->cmd_list_lock);
1793 return 0;
1796 /* drop cmds for the given lun
1797 * XXX only looks for cmds on the port through which lun reset was recieved
1798 * XXX does not go through the list of other port (which may have cmds
1799 * for the same lun)
1801 static void abort_cmds_for_lun(struct scsi_qla_host *vha,
1802 uint32_t lun, uint8_t *s_id)
1804 struct qla_tgt_sess_op *op;
1805 struct qla_tgt_cmd *cmd;
1806 uint32_t key;
1808 key = sid_to_key(s_id);
1809 spin_lock(&vha->cmd_list_lock);
1810 list_for_each_entry(op, &vha->qla_sess_op_cmd_list, cmd_list) {
1811 uint32_t op_key;
1812 uint32_t op_lun;
1814 op_key = sid_to_key(op->atio.u.isp24.fcp_hdr.s_id);
1815 op_lun = scsilun_to_int(
1816 (struct scsi_lun *)&op->atio.u.isp24.fcp_cmnd.lun);
1817 if (op_key == key && op_lun == lun)
1818 op->aborted = true;
1821 list_for_each_entry(op, &vha->unknown_atio_list, cmd_list) {
1822 uint32_t op_key;
1823 u64 op_lun;
1825 op_key = sid_to_key(op->atio.u.isp24.fcp_hdr.s_id);
1826 op_lun = scsilun_to_int(
1827 (struct scsi_lun *)&op->atio.u.isp24.fcp_cmnd.lun);
1828 if (op_key == key && op_lun == lun)
1829 op->aborted = true;
1832 list_for_each_entry(cmd, &vha->qla_cmd_list, cmd_list) {
1833 uint32_t cmd_key;
1834 uint32_t cmd_lun;
1836 cmd_key = sid_to_key(cmd->atio.u.isp24.fcp_hdr.s_id);
1837 cmd_lun = scsilun_to_int(
1838 (struct scsi_lun *)&cmd->atio.u.isp24.fcp_cmnd.lun);
1839 if (cmd_key == key && cmd_lun == lun)
1840 cmd->aborted = 1;
1842 spin_unlock(&vha->cmd_list_lock);
1845 /* ha->hardware_lock supposed to be held on entry */
1846 static int __qlt_24xx_handle_abts(struct scsi_qla_host *vha,
1847 struct abts_recv_from_24xx *abts, struct fc_port *sess)
1849 struct qla_hw_data *ha = vha->hw;
1850 struct se_session *se_sess = sess->se_sess;
1851 struct qla_tgt_mgmt_cmd *mcmd;
1852 struct se_cmd *se_cmd;
1853 u32 lun = 0;
1854 int rc;
1855 bool found_lun = false;
1856 unsigned long flags;
1858 spin_lock_irqsave(&se_sess->sess_cmd_lock, flags);
1859 list_for_each_entry(se_cmd, &se_sess->sess_cmd_list, se_cmd_list) {
1860 struct qla_tgt_cmd *cmd =
1861 container_of(se_cmd, struct qla_tgt_cmd, se_cmd);
1862 if (se_cmd->tag == abts->exchange_addr_to_abort) {
1863 lun = cmd->unpacked_lun;
1864 found_lun = true;
1865 break;
1868 spin_unlock_irqrestore(&se_sess->sess_cmd_lock, flags);
1870 /* cmd not in LIO lists, look in qla list */
1871 if (!found_lun) {
1872 if (abort_cmd_for_tag(vha, abts->exchange_addr_to_abort)) {
1873 /* send TASK_ABORT response immediately */
1874 qlt_24xx_send_abts_resp(vha, abts, FCP_TMF_CMPL, false);
1875 return 0;
1876 } else {
1877 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf081,
1878 "unable to find cmd in driver or LIO for tag 0x%x\n",
1879 abts->exchange_addr_to_abort);
1880 return -ENOENT;
1884 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00f,
1885 "qla_target(%d): task abort (tag=%d)\n",
1886 vha->vp_idx, abts->exchange_addr_to_abort);
1888 mcmd = mempool_alloc(qla_tgt_mgmt_cmd_mempool, GFP_ATOMIC);
1889 if (mcmd == NULL) {
1890 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf051,
1891 "qla_target(%d): %s: Allocation of ABORT cmd failed",
1892 vha->vp_idx, __func__);
1893 return -ENOMEM;
1895 memset(mcmd, 0, sizeof(*mcmd));
1897 mcmd->sess = sess;
1898 memcpy(&mcmd->orig_iocb.abts, abts, sizeof(mcmd->orig_iocb.abts));
1899 mcmd->reset_count = vha->hw->chip_reset;
1900 mcmd->tmr_func = QLA_TGT_ABTS;
1902 rc = ha->tgt.tgt_ops->handle_tmr(mcmd, lun, mcmd->tmr_func,
1903 abts->exchange_addr_to_abort);
1904 if (rc != 0) {
1905 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf052,
1906 "qla_target(%d): tgt_ops->handle_tmr()"
1907 " failed: %d", vha->vp_idx, rc);
1908 mempool_free(mcmd, qla_tgt_mgmt_cmd_mempool);
1909 return -EFAULT;
1912 return 0;
1916 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
1918 static void qlt_24xx_handle_abts(struct scsi_qla_host *vha,
1919 struct abts_recv_from_24xx *abts)
1921 struct qla_hw_data *ha = vha->hw;
1922 struct fc_port *sess;
1923 uint32_t tag = abts->exchange_addr_to_abort;
1924 uint8_t s_id[3];
1925 int rc;
1926 unsigned long flags;
1928 if (le32_to_cpu(abts->fcp_hdr_le.parameter) & ABTS_PARAM_ABORT_SEQ) {
1929 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf053,
1930 "qla_target(%d): ABTS: Abort Sequence not "
1931 "supported\n", vha->vp_idx);
1932 qlt_24xx_send_abts_resp(vha, abts, FCP_TMF_REJECTED, false);
1933 return;
1936 if (tag == ATIO_EXCHANGE_ADDRESS_UNKNOWN) {
1937 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf010,
1938 "qla_target(%d): ABTS: Unknown Exchange "
1939 "Address received\n", vha->vp_idx);
1940 qlt_24xx_send_abts_resp(vha, abts, FCP_TMF_REJECTED, false);
1941 return;
1944 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf011,
1945 "qla_target(%d): task abort (s_id=%x:%x:%x, "
1946 "tag=%d, param=%x)\n", vha->vp_idx, abts->fcp_hdr_le.s_id[2],
1947 abts->fcp_hdr_le.s_id[1], abts->fcp_hdr_le.s_id[0], tag,
1948 le32_to_cpu(abts->fcp_hdr_le.parameter));
1950 s_id[0] = abts->fcp_hdr_le.s_id[2];
1951 s_id[1] = abts->fcp_hdr_le.s_id[1];
1952 s_id[2] = abts->fcp_hdr_le.s_id[0];
1954 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
1955 sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha, s_id);
1956 if (!sess) {
1957 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf012,
1958 "qla_target(%d): task abort for non-existant session\n",
1959 vha->vp_idx);
1960 rc = qlt_sched_sess_work(vha->vha_tgt.qla_tgt,
1961 QLA_TGT_SESS_WORK_ABORT, abts, sizeof(*abts));
1963 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
1965 if (rc != 0) {
1966 qlt_24xx_send_abts_resp(vha, abts, FCP_TMF_REJECTED,
1967 false);
1969 return;
1971 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
1974 if (sess->deleted) {
1975 qlt_24xx_send_abts_resp(vha, abts, FCP_TMF_REJECTED, false);
1976 return;
1979 rc = __qlt_24xx_handle_abts(vha, abts, sess);
1980 if (rc != 0) {
1981 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf054,
1982 "qla_target(%d): __qlt_24xx_handle_abts() failed: %d\n",
1983 vha->vp_idx, rc);
1984 qlt_24xx_send_abts_resp(vha, abts, FCP_TMF_REJECTED, false);
1985 return;
1990 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
1992 static void qlt_24xx_send_task_mgmt_ctio(struct scsi_qla_host *ha,
1993 struct qla_tgt_mgmt_cmd *mcmd, uint32_t resp_code)
1995 struct atio_from_isp *atio = &mcmd->orig_iocb.atio;
1996 struct ctio7_to_24xx *ctio;
1997 uint16_t temp;
1999 ql_dbg(ql_dbg_tgt, ha, 0xe008,
2000 "Sending task mgmt CTIO7 (ha=%p, atio=%p, resp_code=%x\n",
2001 ha, atio, resp_code);
2003 /* Send marker if required */
2004 if (qlt_issue_marker(ha, 1) != QLA_SUCCESS)
2005 return;
2007 ctio = (struct ctio7_to_24xx *)qla2x00_alloc_iocbs(ha, NULL);
2008 if (ctio == NULL) {
2009 ql_dbg(ql_dbg_tgt, ha, 0xe04c,
2010 "qla_target(%d): %s failed: unable to allocate "
2011 "request packet\n", ha->vp_idx, __func__);
2012 return;
2015 ctio->entry_type = CTIO_TYPE7;
2016 ctio->entry_count = 1;
2017 ctio->handle = QLA_TGT_SKIP_HANDLE | CTIO_COMPLETION_HANDLE_MARK;
2018 ctio->nport_handle = mcmd->sess->loop_id;
2019 ctio->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
2020 ctio->vp_index = ha->vp_idx;
2021 ctio->initiator_id[0] = atio->u.isp24.fcp_hdr.s_id[2];
2022 ctio->initiator_id[1] = atio->u.isp24.fcp_hdr.s_id[1];
2023 ctio->initiator_id[2] = atio->u.isp24.fcp_hdr.s_id[0];
2024 ctio->exchange_addr = atio->u.isp24.exchange_addr;
2025 ctio->u.status1.flags = (atio->u.isp24.attr << 9) |
2026 cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_1 | CTIO7_FLAGS_SEND_STATUS);
2027 temp = be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id);
2028 ctio->u.status1.ox_id = cpu_to_le16(temp);
2029 ctio->u.status1.scsi_status =
2030 cpu_to_le16(SS_RESPONSE_INFO_LEN_VALID);
2031 ctio->u.status1.response_len = cpu_to_le16(8);
2032 ctio->u.status1.sense_data[0] = resp_code;
2034 /* Memory Barrier */
2035 wmb();
2036 qla2x00_start_iocbs(ha, ha->req);
2039 void qlt_free_mcmd(struct qla_tgt_mgmt_cmd *mcmd)
2041 mempool_free(mcmd, qla_tgt_mgmt_cmd_mempool);
2043 EXPORT_SYMBOL(qlt_free_mcmd);
2046 * ha->hardware_lock supposed to be held on entry. Might drop it, then
2047 * reacquire
2049 void qlt_send_resp_ctio(scsi_qla_host_t *vha, struct qla_tgt_cmd *cmd,
2050 uint8_t scsi_status, uint8_t sense_key, uint8_t asc, uint8_t ascq)
2052 struct atio_from_isp *atio = &cmd->atio;
2053 struct ctio7_to_24xx *ctio;
2054 uint16_t temp;
2056 ql_dbg(ql_dbg_tgt_dif, vha, 0x3066,
2057 "Sending response CTIO7 (vha=%p, atio=%p, scsi_status=%02x, "
2058 "sense_key=%02x, asc=%02x, ascq=%02x",
2059 vha, atio, scsi_status, sense_key, asc, ascq);
2061 ctio = (struct ctio7_to_24xx *)qla2x00_alloc_iocbs(vha, NULL);
2062 if (!ctio) {
2063 ql_dbg(ql_dbg_async, vha, 0x3067,
2064 "qla2x00t(%ld): %s failed: unable to allocate request packet",
2065 vha->host_no, __func__);
2066 goto out;
2069 ctio->entry_type = CTIO_TYPE7;
2070 ctio->entry_count = 1;
2071 ctio->handle = QLA_TGT_SKIP_HANDLE;
2072 ctio->nport_handle = cmd->sess->loop_id;
2073 ctio->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
2074 ctio->vp_index = vha->vp_idx;
2075 ctio->initiator_id[0] = atio->u.isp24.fcp_hdr.s_id[2];
2076 ctio->initiator_id[1] = atio->u.isp24.fcp_hdr.s_id[1];
2077 ctio->initiator_id[2] = atio->u.isp24.fcp_hdr.s_id[0];
2078 ctio->exchange_addr = atio->u.isp24.exchange_addr;
2079 ctio->u.status1.flags = (atio->u.isp24.attr << 9) |
2080 cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_1 | CTIO7_FLAGS_SEND_STATUS);
2081 temp = be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id);
2082 ctio->u.status1.ox_id = cpu_to_le16(temp);
2083 ctio->u.status1.scsi_status =
2084 cpu_to_le16(SS_RESPONSE_INFO_LEN_VALID | scsi_status);
2085 ctio->u.status1.response_len = cpu_to_le16(18);
2086 ctio->u.status1.residual = cpu_to_le32(get_datalen_for_atio(atio));
2088 if (ctio->u.status1.residual != 0)
2089 ctio->u.status1.scsi_status |=
2090 cpu_to_le16(SS_RESIDUAL_UNDER);
2092 /* Response code and sense key */
2093 put_unaligned_le32(((0x70 << 24) | (sense_key << 8)),
2094 (&ctio->u.status1.sense_data)[0]);
2095 /* Additional sense length */
2096 put_unaligned_le32(0x0a, (&ctio->u.status1.sense_data)[1]);
2097 /* ASC and ASCQ */
2098 put_unaligned_le32(((asc << 24) | (ascq << 16)),
2099 (&ctio->u.status1.sense_data)[3]);
2101 /* Memory Barrier */
2102 wmb();
2104 qla2x00_start_iocbs(vha, vha->req);
2105 out:
2106 return;
2109 /* callback from target fabric module code */
2110 void qlt_xmit_tm_rsp(struct qla_tgt_mgmt_cmd *mcmd)
2112 struct scsi_qla_host *vha = mcmd->sess->vha;
2113 struct qla_hw_data *ha = vha->hw;
2114 unsigned long flags;
2116 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf013,
2117 "TM response mcmd (%p) status %#x state %#x",
2118 mcmd, mcmd->fc_tm_rsp, mcmd->flags);
2120 spin_lock_irqsave(&ha->hardware_lock, flags);
2122 if (!vha->flags.online || mcmd->reset_count != ha->chip_reset) {
2124 * Either the port is not online or this request was from
2125 * previous life, just abort the processing.
2127 ql_dbg(ql_dbg_async, vha, 0xe100,
2128 "RESET-TMR online/active/old-count/new-count = %d/%d/%d/%d.\n",
2129 vha->flags.online, qla2x00_reset_active(vha),
2130 mcmd->reset_count, ha->chip_reset);
2131 ha->tgt.tgt_ops->free_mcmd(mcmd);
2132 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2133 return;
2136 if (mcmd->flags == QLA24XX_MGMT_SEND_NACK) {
2137 if (mcmd->orig_iocb.imm_ntfy.u.isp24.status_subcode ==
2138 ELS_LOGO ||
2139 mcmd->orig_iocb.imm_ntfy.u.isp24.status_subcode ==
2140 ELS_PRLO ||
2141 mcmd->orig_iocb.imm_ntfy.u.isp24.status_subcode ==
2142 ELS_TPRLO) {
2143 ql_dbg(ql_dbg_disc, vha, 0xffff,
2144 "TM response logo %phC status %#x state %#x",
2145 mcmd->sess->port_name, mcmd->fc_tm_rsp,
2146 mcmd->flags);
2147 qlt_schedule_sess_for_deletion_lock(mcmd->sess);
2148 } else {
2149 qlt_send_notify_ack(vha, &mcmd->orig_iocb.imm_ntfy,
2150 0, 0, 0, 0, 0, 0);
2152 } else {
2153 if (mcmd->orig_iocb.atio.u.raw.entry_type == ABTS_RECV_24XX)
2154 qlt_24xx_send_abts_resp(vha, &mcmd->orig_iocb.abts,
2155 mcmd->fc_tm_rsp, false);
2156 else
2157 qlt_24xx_send_task_mgmt_ctio(vha, mcmd,
2158 mcmd->fc_tm_rsp);
2161 * Make the callback for ->free_mcmd() to queue_work() and invoke
2162 * target_put_sess_cmd() to drop cmd_kref to 1. The final
2163 * target_put_sess_cmd() call will be made from TFO->check_stop_free()
2164 * -> tcm_qla2xxx_check_stop_free() to release the TMR associated se_cmd
2165 * descriptor after TFO->queue_tm_rsp() -> tcm_qla2xxx_queue_tm_rsp() ->
2166 * qlt_xmit_tm_rsp() returns here..
2168 ha->tgt.tgt_ops->free_mcmd(mcmd);
2169 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2171 EXPORT_SYMBOL(qlt_xmit_tm_rsp);
2173 /* No locks */
2174 static int qlt_pci_map_calc_cnt(struct qla_tgt_prm *prm)
2176 struct qla_tgt_cmd *cmd = prm->cmd;
2178 BUG_ON(cmd->sg_cnt == 0);
2180 prm->sg = (struct scatterlist *)cmd->sg;
2181 prm->seg_cnt = pci_map_sg(prm->tgt->ha->pdev, cmd->sg,
2182 cmd->sg_cnt, cmd->dma_data_direction);
2183 if (unlikely(prm->seg_cnt == 0))
2184 goto out_err;
2186 prm->cmd->sg_mapped = 1;
2188 if (cmd->se_cmd.prot_op == TARGET_PROT_NORMAL) {
2190 * If greater than four sg entries then we need to allocate
2191 * the continuation entries
2193 if (prm->seg_cnt > prm->tgt->datasegs_per_cmd)
2194 prm->req_cnt += DIV_ROUND_UP(prm->seg_cnt -
2195 prm->tgt->datasegs_per_cmd,
2196 prm->tgt->datasegs_per_cont);
2197 } else {
2198 /* DIF */
2199 if ((cmd->se_cmd.prot_op == TARGET_PROT_DIN_INSERT) ||
2200 (cmd->se_cmd.prot_op == TARGET_PROT_DOUT_STRIP)) {
2201 prm->seg_cnt = DIV_ROUND_UP(cmd->bufflen, cmd->blk_sz);
2202 prm->tot_dsds = prm->seg_cnt;
2203 } else
2204 prm->tot_dsds = prm->seg_cnt;
2206 if (cmd->prot_sg_cnt) {
2207 prm->prot_sg = cmd->prot_sg;
2208 prm->prot_seg_cnt = pci_map_sg(prm->tgt->ha->pdev,
2209 cmd->prot_sg, cmd->prot_sg_cnt,
2210 cmd->dma_data_direction);
2211 if (unlikely(prm->prot_seg_cnt == 0))
2212 goto out_err;
2214 if ((cmd->se_cmd.prot_op == TARGET_PROT_DIN_INSERT) ||
2215 (cmd->se_cmd.prot_op == TARGET_PROT_DOUT_STRIP)) {
2216 /* Dif Bundling not support here */
2217 prm->prot_seg_cnt = DIV_ROUND_UP(cmd->bufflen,
2218 cmd->blk_sz);
2219 prm->tot_dsds += prm->prot_seg_cnt;
2220 } else
2221 prm->tot_dsds += prm->prot_seg_cnt;
2225 return 0;
2227 out_err:
2228 ql_dbg(ql_dbg_tgt, prm->cmd->vha, 0xe04d,
2229 "qla_target(%d): PCI mapping failed: sg_cnt=%d",
2230 0, prm->cmd->sg_cnt);
2231 return -1;
2234 static void qlt_unmap_sg(struct scsi_qla_host *vha, struct qla_tgt_cmd *cmd)
2236 struct qla_hw_data *ha = vha->hw;
2238 if (!cmd->sg_mapped)
2239 return;
2241 pci_unmap_sg(ha->pdev, cmd->sg, cmd->sg_cnt, cmd->dma_data_direction);
2242 cmd->sg_mapped = 0;
2244 if (cmd->prot_sg_cnt)
2245 pci_unmap_sg(ha->pdev, cmd->prot_sg, cmd->prot_sg_cnt,
2246 cmd->dma_data_direction);
2248 if (cmd->ctx_dsd_alloced)
2249 qla2x00_clean_dsd_pool(ha, NULL, cmd);
2251 if (cmd->ctx)
2252 dma_pool_free(ha->dl_dma_pool, cmd->ctx, cmd->ctx->crc_ctx_dma);
2255 static int qlt_check_reserve_free_req(struct scsi_qla_host *vha,
2256 uint32_t req_cnt)
2258 uint32_t cnt, cnt_in;
2260 if (vha->req->cnt < (req_cnt + 2)) {
2261 cnt = (uint16_t)RD_REG_DWORD(vha->req->req_q_out);
2262 cnt_in = (uint16_t)RD_REG_DWORD(vha->req->req_q_in);
2264 if (vha->req->ring_index < cnt)
2265 vha->req->cnt = cnt - vha->req->ring_index;
2266 else
2267 vha->req->cnt = vha->req->length -
2268 (vha->req->ring_index - cnt);
2270 if (unlikely(vha->req->cnt < (req_cnt + 2))) {
2271 ql_dbg(ql_dbg_io, vha, 0x305a,
2272 "qla_target(%d): There is no room in the request ring: vha->req->ring_index=%d, vha->req->cnt=%d, req_cnt=%d Req-out=%d Req-in=%d Req-Length=%d\n",
2273 vha->vp_idx, vha->req->ring_index,
2274 vha->req->cnt, req_cnt, cnt, cnt_in,
2275 vha->req->length);
2276 return -EAGAIN;
2280 vha->req->cnt -= req_cnt;
2282 return 0;
2286 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
2288 static inline void *qlt_get_req_pkt(struct scsi_qla_host *vha)
2290 /* Adjust ring index. */
2291 vha->req->ring_index++;
2292 if (vha->req->ring_index == vha->req->length) {
2293 vha->req->ring_index = 0;
2294 vha->req->ring_ptr = vha->req->ring;
2295 } else {
2296 vha->req->ring_ptr++;
2298 return (cont_entry_t *)vha->req->ring_ptr;
2301 /* ha->hardware_lock supposed to be held on entry */
2302 static inline uint32_t qlt_make_handle(struct scsi_qla_host *vha)
2304 struct qla_hw_data *ha = vha->hw;
2305 uint32_t h;
2307 h = ha->tgt.current_handle;
2308 /* always increment cmd handle */
2309 do {
2310 ++h;
2311 if (h > DEFAULT_OUTSTANDING_COMMANDS)
2312 h = 1; /* 0 is QLA_TGT_NULL_HANDLE */
2313 if (h == ha->tgt.current_handle) {
2314 ql_dbg(ql_dbg_io, vha, 0x305b,
2315 "qla_target(%d): Ran out of "
2316 "empty cmd slots in ha %p\n", vha->vp_idx, ha);
2317 h = QLA_TGT_NULL_HANDLE;
2318 break;
2320 } while ((h == QLA_TGT_NULL_HANDLE) ||
2321 (h == QLA_TGT_SKIP_HANDLE) ||
2322 (ha->tgt.cmds[h-1] != NULL));
2324 if (h != QLA_TGT_NULL_HANDLE)
2325 ha->tgt.current_handle = h;
2327 return h;
2330 /* ha->hardware_lock supposed to be held on entry */
2331 static int qlt_24xx_build_ctio_pkt(struct qla_tgt_prm *prm,
2332 struct scsi_qla_host *vha)
2334 uint32_t h;
2335 struct ctio7_to_24xx *pkt;
2336 struct qla_hw_data *ha = vha->hw;
2337 struct atio_from_isp *atio = &prm->cmd->atio;
2338 uint16_t temp;
2340 pkt = (struct ctio7_to_24xx *)vha->req->ring_ptr;
2341 prm->pkt = pkt;
2342 memset(pkt, 0, sizeof(*pkt));
2344 pkt->entry_type = CTIO_TYPE7;
2345 pkt->entry_count = (uint8_t)prm->req_cnt;
2346 pkt->vp_index = vha->vp_idx;
2348 h = qlt_make_handle(vha);
2349 if (unlikely(h == QLA_TGT_NULL_HANDLE)) {
2351 * CTIO type 7 from the firmware doesn't provide a way to
2352 * know the initiator's LOOP ID, hence we can't find
2353 * the session and, so, the command.
2355 return -EAGAIN;
2356 } else
2357 ha->tgt.cmds[h - 1] = prm->cmd;
2359 pkt->handle = h | CTIO_COMPLETION_HANDLE_MARK;
2360 pkt->nport_handle = prm->cmd->loop_id;
2361 pkt->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
2362 pkt->initiator_id[0] = atio->u.isp24.fcp_hdr.s_id[2];
2363 pkt->initiator_id[1] = atio->u.isp24.fcp_hdr.s_id[1];
2364 pkt->initiator_id[2] = atio->u.isp24.fcp_hdr.s_id[0];
2365 pkt->exchange_addr = atio->u.isp24.exchange_addr;
2366 pkt->u.status0.flags |= (atio->u.isp24.attr << 9);
2367 temp = be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id);
2368 pkt->u.status0.ox_id = cpu_to_le16(temp);
2369 pkt->u.status0.relative_offset = cpu_to_le32(prm->cmd->offset);
2371 return 0;
2375 * ha->hardware_lock supposed to be held on entry. We have already made sure
2376 * that there is sufficient amount of request entries to not drop it.
2378 static void qlt_load_cont_data_segments(struct qla_tgt_prm *prm,
2379 struct scsi_qla_host *vha)
2381 int cnt;
2382 uint32_t *dword_ptr;
2383 int enable_64bit_addressing = prm->tgt->tgt_enable_64bit_addr;
2385 /* Build continuation packets */
2386 while (prm->seg_cnt > 0) {
2387 cont_a64_entry_t *cont_pkt64 =
2388 (cont_a64_entry_t *)qlt_get_req_pkt(vha);
2391 * Make sure that from cont_pkt64 none of
2392 * 64-bit specific fields used for 32-bit
2393 * addressing. Cast to (cont_entry_t *) for
2394 * that.
2397 memset(cont_pkt64, 0, sizeof(*cont_pkt64));
2399 cont_pkt64->entry_count = 1;
2400 cont_pkt64->sys_define = 0;
2402 if (enable_64bit_addressing) {
2403 cont_pkt64->entry_type = CONTINUE_A64_TYPE;
2404 dword_ptr =
2405 (uint32_t *)&cont_pkt64->dseg_0_address;
2406 } else {
2407 cont_pkt64->entry_type = CONTINUE_TYPE;
2408 dword_ptr =
2409 (uint32_t *)&((cont_entry_t *)
2410 cont_pkt64)->dseg_0_address;
2413 /* Load continuation entry data segments */
2414 for (cnt = 0;
2415 cnt < prm->tgt->datasegs_per_cont && prm->seg_cnt;
2416 cnt++, prm->seg_cnt--) {
2417 *dword_ptr++ =
2418 cpu_to_le32(pci_dma_lo32
2419 (sg_dma_address(prm->sg)));
2420 if (enable_64bit_addressing) {
2421 *dword_ptr++ =
2422 cpu_to_le32(pci_dma_hi32
2423 (sg_dma_address
2424 (prm->sg)));
2426 *dword_ptr++ = cpu_to_le32(sg_dma_len(prm->sg));
2428 prm->sg = sg_next(prm->sg);
2434 * ha->hardware_lock supposed to be held on entry. We have already made sure
2435 * that there is sufficient amount of request entries to not drop it.
2437 static void qlt_load_data_segments(struct qla_tgt_prm *prm,
2438 struct scsi_qla_host *vha)
2440 int cnt;
2441 uint32_t *dword_ptr;
2442 int enable_64bit_addressing = prm->tgt->tgt_enable_64bit_addr;
2443 struct ctio7_to_24xx *pkt24 = (struct ctio7_to_24xx *)prm->pkt;
2445 pkt24->u.status0.transfer_length = cpu_to_le32(prm->cmd->bufflen);
2447 /* Setup packet address segment pointer */
2448 dword_ptr = pkt24->u.status0.dseg_0_address;
2450 /* Set total data segment count */
2451 if (prm->seg_cnt)
2452 pkt24->dseg_count = cpu_to_le16(prm->seg_cnt);
2454 if (prm->seg_cnt == 0) {
2455 /* No data transfer */
2456 *dword_ptr++ = 0;
2457 *dword_ptr = 0;
2458 return;
2461 /* If scatter gather */
2463 /* Load command entry data segments */
2464 for (cnt = 0;
2465 (cnt < prm->tgt->datasegs_per_cmd) && prm->seg_cnt;
2466 cnt++, prm->seg_cnt--) {
2467 *dword_ptr++ =
2468 cpu_to_le32(pci_dma_lo32(sg_dma_address(prm->sg)));
2469 if (enable_64bit_addressing) {
2470 *dword_ptr++ =
2471 cpu_to_le32(pci_dma_hi32(
2472 sg_dma_address(prm->sg)));
2474 *dword_ptr++ = cpu_to_le32(sg_dma_len(prm->sg));
2476 prm->sg = sg_next(prm->sg);
2479 qlt_load_cont_data_segments(prm, vha);
2482 static inline int qlt_has_data(struct qla_tgt_cmd *cmd)
2484 return cmd->bufflen > 0;
2487 static void qlt_print_dif_err(struct qla_tgt_prm *prm)
2489 struct qla_tgt_cmd *cmd;
2490 struct scsi_qla_host *vha;
2492 /* asc 0x10=dif error */
2493 if (prm->sense_buffer && (prm->sense_buffer[12] == 0x10)) {
2494 cmd = prm->cmd;
2495 vha = cmd->vha;
2496 /* ASCQ */
2497 switch (prm->sense_buffer[13]) {
2498 case 1:
2499 ql_dbg(ql_dbg_tgt_dif, vha, 0xffff,
2500 "BE detected Guard TAG ERR: lba[0x%llx|%lld] len[0x%x] "
2501 "se_cmd=%p tag[%x]",
2502 cmd->lba, cmd->lba, cmd->num_blks, &cmd->se_cmd,
2503 cmd->atio.u.isp24.exchange_addr);
2504 break;
2505 case 2:
2506 ql_dbg(ql_dbg_tgt_dif, vha, 0xffff,
2507 "BE detected APP TAG ERR: lba[0x%llx|%lld] len[0x%x] "
2508 "se_cmd=%p tag[%x]",
2509 cmd->lba, cmd->lba, cmd->num_blks, &cmd->se_cmd,
2510 cmd->atio.u.isp24.exchange_addr);
2511 break;
2512 case 3:
2513 ql_dbg(ql_dbg_tgt_dif, vha, 0xffff,
2514 "BE detected REF TAG ERR: lba[0x%llx|%lld] len[0x%x] "
2515 "se_cmd=%p tag[%x]",
2516 cmd->lba, cmd->lba, cmd->num_blks, &cmd->se_cmd,
2517 cmd->atio.u.isp24.exchange_addr);
2518 break;
2519 default:
2520 ql_dbg(ql_dbg_tgt_dif, vha, 0xffff,
2521 "BE detected Dif ERR: lba[%llx|%lld] len[%x] "
2522 "se_cmd=%p tag[%x]",
2523 cmd->lba, cmd->lba, cmd->num_blks, &cmd->se_cmd,
2524 cmd->atio.u.isp24.exchange_addr);
2525 break;
2527 ql_dump_buffer(ql_dbg_tgt_dif, vha, 0xffff, cmd->cdb, 16);
2532 * Called without ha->hardware_lock held
2534 static int qlt_pre_xmit_response(struct qla_tgt_cmd *cmd,
2535 struct qla_tgt_prm *prm, int xmit_type, uint8_t scsi_status,
2536 uint32_t *full_req_cnt)
2538 struct qla_tgt *tgt = cmd->tgt;
2539 struct scsi_qla_host *vha = tgt->vha;
2540 struct qla_hw_data *ha = vha->hw;
2541 struct se_cmd *se_cmd = &cmd->se_cmd;
2543 prm->cmd = cmd;
2544 prm->tgt = tgt;
2545 prm->rq_result = scsi_status;
2546 prm->sense_buffer = &cmd->sense_buffer[0];
2547 prm->sense_buffer_len = TRANSPORT_SENSE_BUFFER;
2548 prm->sg = NULL;
2549 prm->seg_cnt = -1;
2550 prm->req_cnt = 1;
2551 prm->add_status_pkt = 0;
2553 /* Send marker if required */
2554 if (qlt_issue_marker(vha, 0) != QLA_SUCCESS)
2555 return -EFAULT;
2557 if ((xmit_type & QLA_TGT_XMIT_DATA) && qlt_has_data(cmd)) {
2558 if (qlt_pci_map_calc_cnt(prm) != 0)
2559 return -EAGAIN;
2562 *full_req_cnt = prm->req_cnt;
2564 if (se_cmd->se_cmd_flags & SCF_UNDERFLOW_BIT) {
2565 prm->residual = se_cmd->residual_count;
2566 ql_dbg(ql_dbg_io + ql_dbg_verbose, vha, 0x305c,
2567 "Residual underflow: %d (tag %lld, op %x, bufflen %d, rq_result %x)\n",
2568 prm->residual, se_cmd->tag,
2569 se_cmd->t_task_cdb ? se_cmd->t_task_cdb[0] : 0,
2570 cmd->bufflen, prm->rq_result);
2571 prm->rq_result |= SS_RESIDUAL_UNDER;
2572 } else if (se_cmd->se_cmd_flags & SCF_OVERFLOW_BIT) {
2573 prm->residual = se_cmd->residual_count;
2574 ql_dbg(ql_dbg_io, vha, 0x305d,
2575 "Residual overflow: %d (tag %lld, op %x, bufflen %d, rq_result %x)\n",
2576 prm->residual, se_cmd->tag, se_cmd->t_task_cdb ?
2577 se_cmd->t_task_cdb[0] : 0, cmd->bufflen, prm->rq_result);
2578 prm->rq_result |= SS_RESIDUAL_OVER;
2581 if (xmit_type & QLA_TGT_XMIT_STATUS) {
2583 * If QLA_TGT_XMIT_DATA is not set, add_status_pkt will be
2584 * ignored in *xmit_response() below
2586 if (qlt_has_data(cmd)) {
2587 if (QLA_TGT_SENSE_VALID(prm->sense_buffer) ||
2588 (IS_FWI2_CAPABLE(ha) &&
2589 (prm->rq_result != 0))) {
2590 prm->add_status_pkt = 1;
2591 (*full_req_cnt)++;
2596 return 0;
2599 static inline int qlt_need_explicit_conf(struct qla_hw_data *ha,
2600 struct qla_tgt_cmd *cmd, int sending_sense)
2602 if (ha->tgt.enable_class_2)
2603 return 0;
2605 if (sending_sense)
2606 return cmd->conf_compl_supported;
2607 else
2608 return ha->tgt.enable_explicit_conf &&
2609 cmd->conf_compl_supported;
2612 static void qlt_24xx_init_ctio_to_isp(struct ctio7_to_24xx *ctio,
2613 struct qla_tgt_prm *prm)
2615 prm->sense_buffer_len = min_t(uint32_t, prm->sense_buffer_len,
2616 (uint32_t)sizeof(ctio->u.status1.sense_data));
2617 ctio->u.status0.flags |= cpu_to_le16(CTIO7_FLAGS_SEND_STATUS);
2618 if (qlt_need_explicit_conf(prm->tgt->ha, prm->cmd, 0)) {
2619 ctio->u.status0.flags |= cpu_to_le16(
2620 CTIO7_FLAGS_EXPLICIT_CONFORM |
2621 CTIO7_FLAGS_CONFORM_REQ);
2623 ctio->u.status0.residual = cpu_to_le32(prm->residual);
2624 ctio->u.status0.scsi_status = cpu_to_le16(prm->rq_result);
2625 if (QLA_TGT_SENSE_VALID(prm->sense_buffer)) {
2626 int i;
2628 if (qlt_need_explicit_conf(prm->tgt->ha, prm->cmd, 1)) {
2629 if ((prm->rq_result & SS_SCSI_STATUS_BYTE) != 0) {
2630 ql_dbg(ql_dbg_tgt, prm->cmd->vha, 0xe017,
2631 "Skipping EXPLICIT_CONFORM and "
2632 "CTIO7_FLAGS_CONFORM_REQ for FCP READ w/ "
2633 "non GOOD status\n");
2634 goto skip_explict_conf;
2636 ctio->u.status1.flags |= cpu_to_le16(
2637 CTIO7_FLAGS_EXPLICIT_CONFORM |
2638 CTIO7_FLAGS_CONFORM_REQ);
2640 skip_explict_conf:
2641 ctio->u.status1.flags &=
2642 ~cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_0);
2643 ctio->u.status1.flags |=
2644 cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_1);
2645 ctio->u.status1.scsi_status |=
2646 cpu_to_le16(SS_SENSE_LEN_VALID);
2647 ctio->u.status1.sense_length =
2648 cpu_to_le16(prm->sense_buffer_len);
2649 for (i = 0; i < prm->sense_buffer_len/4; i++)
2650 ((uint32_t *)ctio->u.status1.sense_data)[i] =
2651 cpu_to_be32(((uint32_t *)prm->sense_buffer)[i]);
2653 qlt_print_dif_err(prm);
2655 } else {
2656 ctio->u.status1.flags &=
2657 ~cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_0);
2658 ctio->u.status1.flags |=
2659 cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_1);
2660 ctio->u.status1.sense_length = 0;
2661 memset(ctio->u.status1.sense_data, 0,
2662 sizeof(ctio->u.status1.sense_data));
2665 /* Sense with len > 24, is it possible ??? */
2668 static inline int
2669 qlt_hba_err_chk_enabled(struct se_cmd *se_cmd)
2671 switch (se_cmd->prot_op) {
2672 case TARGET_PROT_DOUT_INSERT:
2673 case TARGET_PROT_DIN_STRIP:
2674 if (ql2xenablehba_err_chk >= 1)
2675 return 1;
2676 break;
2677 case TARGET_PROT_DOUT_PASS:
2678 case TARGET_PROT_DIN_PASS:
2679 if (ql2xenablehba_err_chk >= 2)
2680 return 1;
2681 break;
2682 case TARGET_PROT_DIN_INSERT:
2683 case TARGET_PROT_DOUT_STRIP:
2684 return 1;
2685 default:
2686 break;
2688 return 0;
2691 static inline int
2692 qla_tgt_ref_mask_check(struct se_cmd *se_cmd)
2694 switch (se_cmd->prot_op) {
2695 case TARGET_PROT_DIN_INSERT:
2696 case TARGET_PROT_DOUT_INSERT:
2697 case TARGET_PROT_DIN_STRIP:
2698 case TARGET_PROT_DOUT_STRIP:
2699 case TARGET_PROT_DIN_PASS:
2700 case TARGET_PROT_DOUT_PASS:
2701 return 1;
2702 default:
2703 return 0;
2705 return 0;
2709 * qla_tgt_set_dif_tags - Extract Ref and App tags from SCSI command
2711 static void
2712 qla_tgt_set_dif_tags(struct qla_tgt_cmd *cmd, struct crc_context *ctx,
2713 uint16_t *pfw_prot_opts)
2715 struct se_cmd *se_cmd = &cmd->se_cmd;
2716 uint32_t lba = 0xffffffff & se_cmd->t_task_lba;
2717 scsi_qla_host_t *vha = cmd->tgt->vha;
2718 struct qla_hw_data *ha = vha->hw;
2719 uint32_t t32 = 0;
2722 * wait till Mode Sense/Select cmd, modepage Ah, subpage 2
2723 * have been immplemented by TCM, before AppTag is avail.
2724 * Look for modesense_handlers[]
2726 ctx->app_tag = 0;
2727 ctx->app_tag_mask[0] = 0x0;
2728 ctx->app_tag_mask[1] = 0x0;
2730 if (IS_PI_UNINIT_CAPABLE(ha)) {
2731 if ((se_cmd->prot_type == TARGET_DIF_TYPE1_PROT) ||
2732 (se_cmd->prot_type == TARGET_DIF_TYPE2_PROT))
2733 *pfw_prot_opts |= PO_DIS_VALD_APP_ESC;
2734 else if (se_cmd->prot_type == TARGET_DIF_TYPE3_PROT)
2735 *pfw_prot_opts |= PO_DIS_VALD_APP_REF_ESC;
2738 t32 = ha->tgt.tgt_ops->get_dif_tags(cmd, pfw_prot_opts);
2740 switch (se_cmd->prot_type) {
2741 case TARGET_DIF_TYPE0_PROT:
2743 * No check for ql2xenablehba_err_chk, as it
2744 * would be an I/O error if hba tag generation
2745 * is not done.
2747 ctx->ref_tag = cpu_to_le32(lba);
2748 /* enable ALL bytes of the ref tag */
2749 ctx->ref_tag_mask[0] = 0xff;
2750 ctx->ref_tag_mask[1] = 0xff;
2751 ctx->ref_tag_mask[2] = 0xff;
2752 ctx->ref_tag_mask[3] = 0xff;
2753 break;
2754 case TARGET_DIF_TYPE1_PROT:
2756 * For TYPE 1 protection: 16 bit GUARD tag, 32 bit
2757 * REF tag, and 16 bit app tag.
2759 ctx->ref_tag = cpu_to_le32(lba);
2760 if (!qla_tgt_ref_mask_check(se_cmd) ||
2761 !(ha->tgt.tgt_ops->chk_dif_tags(t32))) {
2762 *pfw_prot_opts |= PO_DIS_REF_TAG_VALD;
2763 break;
2765 /* enable ALL bytes of the ref tag */
2766 ctx->ref_tag_mask[0] = 0xff;
2767 ctx->ref_tag_mask[1] = 0xff;
2768 ctx->ref_tag_mask[2] = 0xff;
2769 ctx->ref_tag_mask[3] = 0xff;
2770 break;
2771 case TARGET_DIF_TYPE2_PROT:
2773 * For TYPE 2 protection: 16 bit GUARD + 32 bit REF
2774 * tag has to match LBA in CDB + N
2776 ctx->ref_tag = cpu_to_le32(lba);
2777 if (!qla_tgt_ref_mask_check(se_cmd) ||
2778 !(ha->tgt.tgt_ops->chk_dif_tags(t32))) {
2779 *pfw_prot_opts |= PO_DIS_REF_TAG_VALD;
2780 break;
2782 /* enable ALL bytes of the ref tag */
2783 ctx->ref_tag_mask[0] = 0xff;
2784 ctx->ref_tag_mask[1] = 0xff;
2785 ctx->ref_tag_mask[2] = 0xff;
2786 ctx->ref_tag_mask[3] = 0xff;
2787 break;
2788 case TARGET_DIF_TYPE3_PROT:
2789 /* For TYPE 3 protection: 16 bit GUARD only */
2790 *pfw_prot_opts |= PO_DIS_REF_TAG_VALD;
2791 ctx->ref_tag_mask[0] = ctx->ref_tag_mask[1] =
2792 ctx->ref_tag_mask[2] = ctx->ref_tag_mask[3] = 0x00;
2793 break;
2797 static inline int
2798 qlt_build_ctio_crc2_pkt(struct qla_tgt_prm *prm, scsi_qla_host_t *vha)
2800 uint32_t *cur_dsd;
2801 uint32_t transfer_length = 0;
2802 uint32_t data_bytes;
2803 uint32_t dif_bytes;
2804 uint8_t bundling = 1;
2805 uint8_t *clr_ptr;
2806 struct crc_context *crc_ctx_pkt = NULL;
2807 struct qla_hw_data *ha;
2808 struct ctio_crc2_to_fw *pkt;
2809 dma_addr_t crc_ctx_dma;
2810 uint16_t fw_prot_opts = 0;
2811 struct qla_tgt_cmd *cmd = prm->cmd;
2812 struct se_cmd *se_cmd = &cmd->se_cmd;
2813 uint32_t h;
2814 struct atio_from_isp *atio = &prm->cmd->atio;
2815 struct qla_tc_param tc;
2816 uint16_t t16;
2818 ha = vha->hw;
2820 pkt = (struct ctio_crc2_to_fw *)vha->req->ring_ptr;
2821 prm->pkt = pkt;
2822 memset(pkt, 0, sizeof(*pkt));
2824 ql_dbg(ql_dbg_tgt, vha, 0xe071,
2825 "qla_target(%d):%s: se_cmd[%p] CRC2 prot_op[0x%x] cmd prot sg:cnt[%p:%x] lba[%llu]\n",
2826 vha->vp_idx, __func__, se_cmd, se_cmd->prot_op,
2827 prm->prot_sg, prm->prot_seg_cnt, se_cmd->t_task_lba);
2829 if ((se_cmd->prot_op == TARGET_PROT_DIN_INSERT) ||
2830 (se_cmd->prot_op == TARGET_PROT_DOUT_STRIP))
2831 bundling = 0;
2833 /* Compute dif len and adjust data len to incude protection */
2834 data_bytes = cmd->bufflen;
2835 dif_bytes = (data_bytes / cmd->blk_sz) * 8;
2837 switch (se_cmd->prot_op) {
2838 case TARGET_PROT_DIN_INSERT:
2839 case TARGET_PROT_DOUT_STRIP:
2840 transfer_length = data_bytes;
2841 if (cmd->prot_sg_cnt)
2842 data_bytes += dif_bytes;
2843 break;
2844 case TARGET_PROT_DIN_STRIP:
2845 case TARGET_PROT_DOUT_INSERT:
2846 case TARGET_PROT_DIN_PASS:
2847 case TARGET_PROT_DOUT_PASS:
2848 transfer_length = data_bytes + dif_bytes;
2849 break;
2850 default:
2851 BUG();
2852 break;
2855 if (!qlt_hba_err_chk_enabled(se_cmd))
2856 fw_prot_opts |= 0x10; /* Disable Guard tag checking */
2857 /* HBA error checking enabled */
2858 else if (IS_PI_UNINIT_CAPABLE(ha)) {
2859 if ((se_cmd->prot_type == TARGET_DIF_TYPE1_PROT) ||
2860 (se_cmd->prot_type == TARGET_DIF_TYPE2_PROT))
2861 fw_prot_opts |= PO_DIS_VALD_APP_ESC;
2862 else if (se_cmd->prot_type == TARGET_DIF_TYPE3_PROT)
2863 fw_prot_opts |= PO_DIS_VALD_APP_REF_ESC;
2866 switch (se_cmd->prot_op) {
2867 case TARGET_PROT_DIN_INSERT:
2868 case TARGET_PROT_DOUT_INSERT:
2869 fw_prot_opts |= PO_MODE_DIF_INSERT;
2870 break;
2871 case TARGET_PROT_DIN_STRIP:
2872 case TARGET_PROT_DOUT_STRIP:
2873 fw_prot_opts |= PO_MODE_DIF_REMOVE;
2874 break;
2875 case TARGET_PROT_DIN_PASS:
2876 case TARGET_PROT_DOUT_PASS:
2877 fw_prot_opts |= PO_MODE_DIF_PASS;
2878 /* FUTURE: does tcm require T10CRC<->IPCKSUM conversion? */
2879 break;
2880 default:/* Normal Request */
2881 fw_prot_opts |= PO_MODE_DIF_PASS;
2882 break;
2885 /* ---- PKT ---- */
2886 /* Update entry type to indicate Command Type CRC_2 IOCB */
2887 pkt->entry_type = CTIO_CRC2;
2888 pkt->entry_count = 1;
2889 pkt->vp_index = vha->vp_idx;
2891 h = qlt_make_handle(vha);
2892 if (unlikely(h == QLA_TGT_NULL_HANDLE)) {
2894 * CTIO type 7 from the firmware doesn't provide a way to
2895 * know the initiator's LOOP ID, hence we can't find
2896 * the session and, so, the command.
2898 return -EAGAIN;
2899 } else
2900 ha->tgt.cmds[h-1] = prm->cmd;
2902 pkt->handle = h | CTIO_COMPLETION_HANDLE_MARK;
2903 pkt->nport_handle = cpu_to_le16(prm->cmd->loop_id);
2904 pkt->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
2905 pkt->initiator_id[0] = atio->u.isp24.fcp_hdr.s_id[2];
2906 pkt->initiator_id[1] = atio->u.isp24.fcp_hdr.s_id[1];
2907 pkt->initiator_id[2] = atio->u.isp24.fcp_hdr.s_id[0];
2908 pkt->exchange_addr = atio->u.isp24.exchange_addr;
2910 /* silence compile warning */
2911 t16 = be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id);
2912 pkt->ox_id = cpu_to_le16(t16);
2914 t16 = (atio->u.isp24.attr << 9);
2915 pkt->flags |= cpu_to_le16(t16);
2916 pkt->relative_offset = cpu_to_le32(prm->cmd->offset);
2918 /* Set transfer direction */
2919 if (cmd->dma_data_direction == DMA_TO_DEVICE)
2920 pkt->flags = cpu_to_le16(CTIO7_FLAGS_DATA_IN);
2921 else if (cmd->dma_data_direction == DMA_FROM_DEVICE)
2922 pkt->flags = cpu_to_le16(CTIO7_FLAGS_DATA_OUT);
2924 pkt->dseg_count = prm->tot_dsds;
2925 /* Fibre channel byte count */
2926 pkt->transfer_length = cpu_to_le32(transfer_length);
2928 /* ----- CRC context -------- */
2930 /* Allocate CRC context from global pool */
2931 crc_ctx_pkt = cmd->ctx =
2932 dma_pool_alloc(ha->dl_dma_pool, GFP_ATOMIC, &crc_ctx_dma);
2934 if (!crc_ctx_pkt)
2935 goto crc_queuing_error;
2937 /* Zero out CTX area. */
2938 clr_ptr = (uint8_t *)crc_ctx_pkt;
2939 memset(clr_ptr, 0, sizeof(*crc_ctx_pkt));
2941 crc_ctx_pkt->crc_ctx_dma = crc_ctx_dma;
2942 INIT_LIST_HEAD(&crc_ctx_pkt->dsd_list);
2944 /* Set handle */
2945 crc_ctx_pkt->handle = pkt->handle;
2947 qla_tgt_set_dif_tags(cmd, crc_ctx_pkt, &fw_prot_opts);
2949 pkt->crc_context_address[0] = cpu_to_le32(LSD(crc_ctx_dma));
2950 pkt->crc_context_address[1] = cpu_to_le32(MSD(crc_ctx_dma));
2951 pkt->crc_context_len = CRC_CONTEXT_LEN_FW;
2953 if (!bundling) {
2954 cur_dsd = (uint32_t *) &crc_ctx_pkt->u.nobundling.data_address;
2955 } else {
2957 * Configure Bundling if we need to fetch interlaving
2958 * protection PCI accesses
2960 fw_prot_opts |= PO_ENABLE_DIF_BUNDLING;
2961 crc_ctx_pkt->u.bundling.dif_byte_count = cpu_to_le32(dif_bytes);
2962 crc_ctx_pkt->u.bundling.dseg_count =
2963 cpu_to_le16(prm->tot_dsds - prm->prot_seg_cnt);
2964 cur_dsd = (uint32_t *) &crc_ctx_pkt->u.bundling.data_address;
2967 /* Finish the common fields of CRC pkt */
2968 crc_ctx_pkt->blk_size = cpu_to_le16(cmd->blk_sz);
2969 crc_ctx_pkt->prot_opts = cpu_to_le16(fw_prot_opts);
2970 crc_ctx_pkt->byte_count = cpu_to_le32(data_bytes);
2971 crc_ctx_pkt->guard_seed = cpu_to_le16(0);
2973 memset((uint8_t *)&tc, 0 , sizeof(tc));
2974 tc.vha = vha;
2975 tc.blk_sz = cmd->blk_sz;
2976 tc.bufflen = cmd->bufflen;
2977 tc.sg = cmd->sg;
2978 tc.prot_sg = cmd->prot_sg;
2979 tc.ctx = crc_ctx_pkt;
2980 tc.ctx_dsd_alloced = &cmd->ctx_dsd_alloced;
2982 /* Walks data segments */
2983 pkt->flags |= cpu_to_le16(CTIO7_FLAGS_DSD_PTR);
2985 if (!bundling && prm->prot_seg_cnt) {
2986 if (qla24xx_walk_and_build_sglist_no_difb(ha, NULL, cur_dsd,
2987 prm->tot_dsds, &tc))
2988 goto crc_queuing_error;
2989 } else if (qla24xx_walk_and_build_sglist(ha, NULL, cur_dsd,
2990 (prm->tot_dsds - prm->prot_seg_cnt), &tc))
2991 goto crc_queuing_error;
2993 if (bundling && prm->prot_seg_cnt) {
2994 /* Walks dif segments */
2995 pkt->add_flags |= CTIO_CRC2_AF_DIF_DSD_ENA;
2997 cur_dsd = (uint32_t *) &crc_ctx_pkt->u.bundling.dif_address;
2998 if (qla24xx_walk_and_build_prot_sglist(ha, NULL, cur_dsd,
2999 prm->prot_seg_cnt, &tc))
3000 goto crc_queuing_error;
3002 return QLA_SUCCESS;
3004 crc_queuing_error:
3005 /* Cleanup will be performed by the caller */
3006 vha->hw->tgt.cmds[h - 1] = NULL;
3008 return QLA_FUNCTION_FAILED;
3012 * Callback to setup response of xmit_type of QLA_TGT_XMIT_DATA and *
3013 * QLA_TGT_XMIT_STATUS for >= 24xx silicon
3015 int qlt_xmit_response(struct qla_tgt_cmd *cmd, int xmit_type,
3016 uint8_t scsi_status)
3018 struct scsi_qla_host *vha = cmd->vha;
3019 struct qla_hw_data *ha = vha->hw;
3020 struct ctio7_to_24xx *pkt;
3021 struct qla_tgt_prm prm;
3022 uint32_t full_req_cnt = 0;
3023 unsigned long flags = 0;
3024 int res;
3026 spin_lock_irqsave(&ha->hardware_lock, flags);
3027 if (cmd->sess && cmd->sess->deleted) {
3028 cmd->state = QLA_TGT_STATE_PROCESSED;
3029 if (cmd->sess->logout_completed)
3030 /* no need to terminate. FW already freed exchange. */
3031 qlt_abort_cmd_on_host_reset(cmd->vha, cmd);
3032 else
3033 qlt_send_term_exchange(vha, cmd, &cmd->atio, 1, 0);
3034 spin_unlock_irqrestore(&ha->hardware_lock, flags);
3035 return 0;
3037 spin_unlock_irqrestore(&ha->hardware_lock, flags);
3039 memset(&prm, 0, sizeof(prm));
3041 ql_dbg(ql_dbg_tgt, cmd->vha, 0xe018,
3042 "is_send_status=%d, cmd->bufflen=%d, cmd->sg_cnt=%d, cmd->dma_data_direction=%d se_cmd[%p]\n",
3043 (xmit_type & QLA_TGT_XMIT_STATUS) ?
3044 1 : 0, cmd->bufflen, cmd->sg_cnt, cmd->dma_data_direction,
3045 &cmd->se_cmd);
3047 res = qlt_pre_xmit_response(cmd, &prm, xmit_type, scsi_status,
3048 &full_req_cnt);
3049 if (unlikely(res != 0)) {
3050 return res;
3053 spin_lock_irqsave(&ha->hardware_lock, flags);
3055 if (xmit_type == QLA_TGT_XMIT_STATUS)
3056 vha->tgt_counters.core_qla_snd_status++;
3057 else
3058 vha->tgt_counters.core_qla_que_buf++;
3060 if (!ha->flags.fw_started || cmd->reset_count != ha->chip_reset) {
3062 * Either the port is not online or this request was from
3063 * previous life, just abort the processing.
3065 cmd->state = QLA_TGT_STATE_PROCESSED;
3066 qlt_abort_cmd_on_host_reset(cmd->vha, cmd);
3067 ql_dbg(ql_dbg_async, vha, 0xe101,
3068 "RESET-RSP online/active/old-count/new-count = %d/%d/%d/%d.\n",
3069 vha->flags.online, qla2x00_reset_active(vha),
3070 cmd->reset_count, ha->chip_reset);
3071 spin_unlock_irqrestore(&ha->hardware_lock, flags);
3072 return 0;
3075 /* Does F/W have an IOCBs for this request */
3076 res = qlt_check_reserve_free_req(vha, full_req_cnt);
3077 if (unlikely(res))
3078 goto out_unmap_unlock;
3080 if (cmd->se_cmd.prot_op && (xmit_type & QLA_TGT_XMIT_DATA))
3081 res = qlt_build_ctio_crc2_pkt(&prm, vha);
3082 else
3083 res = qlt_24xx_build_ctio_pkt(&prm, vha);
3084 if (unlikely(res != 0)) {
3085 vha->req->cnt += full_req_cnt;
3086 goto out_unmap_unlock;
3089 pkt = (struct ctio7_to_24xx *)prm.pkt;
3091 if (qlt_has_data(cmd) && (xmit_type & QLA_TGT_XMIT_DATA)) {
3092 pkt->u.status0.flags |=
3093 cpu_to_le16(CTIO7_FLAGS_DATA_IN |
3094 CTIO7_FLAGS_STATUS_MODE_0);
3096 if (cmd->se_cmd.prot_op == TARGET_PROT_NORMAL)
3097 qlt_load_data_segments(&prm, vha);
3099 if (prm.add_status_pkt == 0) {
3100 if (xmit_type & QLA_TGT_XMIT_STATUS) {
3101 pkt->u.status0.scsi_status =
3102 cpu_to_le16(prm.rq_result);
3103 pkt->u.status0.residual =
3104 cpu_to_le32(prm.residual);
3105 pkt->u.status0.flags |= cpu_to_le16(
3106 CTIO7_FLAGS_SEND_STATUS);
3107 if (qlt_need_explicit_conf(ha, cmd, 0)) {
3108 pkt->u.status0.flags |=
3109 cpu_to_le16(
3110 CTIO7_FLAGS_EXPLICIT_CONFORM |
3111 CTIO7_FLAGS_CONFORM_REQ);
3115 } else {
3117 * We have already made sure that there is sufficient
3118 * amount of request entries to not drop HW lock in
3119 * req_pkt().
3121 struct ctio7_to_24xx *ctio =
3122 (struct ctio7_to_24xx *)qlt_get_req_pkt(vha);
3124 ql_dbg(ql_dbg_io, vha, 0x305e,
3125 "Building additional status packet 0x%p.\n",
3126 ctio);
3129 * T10Dif: ctio_crc2_to_fw overlay ontop of
3130 * ctio7_to_24xx
3132 memcpy(ctio, pkt, sizeof(*ctio));
3133 /* reset back to CTIO7 */
3134 ctio->entry_count = 1;
3135 ctio->entry_type = CTIO_TYPE7;
3136 ctio->dseg_count = 0;
3137 ctio->u.status1.flags &= ~cpu_to_le16(
3138 CTIO7_FLAGS_DATA_IN);
3140 /* Real finish is ctio_m1's finish */
3141 pkt->handle |= CTIO_INTERMEDIATE_HANDLE_MARK;
3142 pkt->u.status0.flags |= cpu_to_le16(
3143 CTIO7_FLAGS_DONT_RET_CTIO);
3145 /* qlt_24xx_init_ctio_to_isp will correct
3146 * all neccessary fields that's part of CTIO7.
3147 * There should be no residual of CTIO-CRC2 data.
3149 qlt_24xx_init_ctio_to_isp((struct ctio7_to_24xx *)ctio,
3150 &prm);
3151 pr_debug("Status CTIO7: %p\n", ctio);
3153 } else
3154 qlt_24xx_init_ctio_to_isp(pkt, &prm);
3157 cmd->state = QLA_TGT_STATE_PROCESSED; /* Mid-level is done processing */
3158 cmd->cmd_sent_to_fw = 1;
3160 /* Memory Barrier */
3161 wmb();
3162 qla2x00_start_iocbs(vha, vha->req);
3163 spin_unlock_irqrestore(&ha->hardware_lock, flags);
3165 return 0;
3167 out_unmap_unlock:
3168 qlt_unmap_sg(vha, cmd);
3169 spin_unlock_irqrestore(&ha->hardware_lock, flags);
3171 return res;
3173 EXPORT_SYMBOL(qlt_xmit_response);
3175 int qlt_rdy_to_xfer(struct qla_tgt_cmd *cmd)
3177 struct ctio7_to_24xx *pkt;
3178 struct scsi_qla_host *vha = cmd->vha;
3179 struct qla_hw_data *ha = vha->hw;
3180 struct qla_tgt *tgt = cmd->tgt;
3181 struct qla_tgt_prm prm;
3182 unsigned long flags;
3183 int res = 0;
3185 memset(&prm, 0, sizeof(prm));
3186 prm.cmd = cmd;
3187 prm.tgt = tgt;
3188 prm.sg = NULL;
3189 prm.req_cnt = 1;
3191 /* Send marker if required */
3192 if (qlt_issue_marker(vha, 0) != QLA_SUCCESS)
3193 return -EIO;
3195 /* Calculate number of entries and segments required */
3196 if (qlt_pci_map_calc_cnt(&prm) != 0)
3197 return -EAGAIN;
3199 spin_lock_irqsave(&ha->hardware_lock, flags);
3201 if (!ha->flags.fw_started || (cmd->reset_count != ha->chip_reset) ||
3202 (cmd->sess && cmd->sess->deleted)) {
3204 * Either the port is not online or this request was from
3205 * previous life, just abort the processing.
3207 cmd->state = QLA_TGT_STATE_NEED_DATA;
3208 qlt_abort_cmd_on_host_reset(cmd->vha, cmd);
3209 ql_dbg(ql_dbg_async, vha, 0xe102,
3210 "RESET-XFR online/active/old-count/new-count = %d/%d/%d/%d.\n",
3211 vha->flags.online, qla2x00_reset_active(vha),
3212 cmd->reset_count, ha->chip_reset);
3213 spin_unlock_irqrestore(&ha->hardware_lock, flags);
3214 return 0;
3217 /* Does F/W have an IOCBs for this request */
3218 res = qlt_check_reserve_free_req(vha, prm.req_cnt);
3219 if (res != 0)
3220 goto out_unlock_free_unmap;
3221 if (cmd->se_cmd.prot_op)
3222 res = qlt_build_ctio_crc2_pkt(&prm, vha);
3223 else
3224 res = qlt_24xx_build_ctio_pkt(&prm, vha);
3226 if (unlikely(res != 0)) {
3227 vha->req->cnt += prm.req_cnt;
3228 goto out_unlock_free_unmap;
3231 pkt = (struct ctio7_to_24xx *)prm.pkt;
3232 pkt->u.status0.flags |= cpu_to_le16(CTIO7_FLAGS_DATA_OUT |
3233 CTIO7_FLAGS_STATUS_MODE_0);
3235 if (cmd->se_cmd.prot_op == TARGET_PROT_NORMAL)
3236 qlt_load_data_segments(&prm, vha);
3238 cmd->state = QLA_TGT_STATE_NEED_DATA;
3239 cmd->cmd_sent_to_fw = 1;
3241 /* Memory Barrier */
3242 wmb();
3243 qla2x00_start_iocbs(vha, vha->req);
3244 spin_unlock_irqrestore(&ha->hardware_lock, flags);
3246 return res;
3248 out_unlock_free_unmap:
3249 qlt_unmap_sg(vha, cmd);
3250 spin_unlock_irqrestore(&ha->hardware_lock, flags);
3252 return res;
3254 EXPORT_SYMBOL(qlt_rdy_to_xfer);
3258 * it is assumed either hardware_lock or qpair lock is held.
3260 static void
3261 qlt_handle_dif_error(struct scsi_qla_host *vha, struct qla_tgt_cmd *cmd,
3262 struct ctio_crc_from_fw *sts)
3264 uint8_t *ap = &sts->actual_dif[0];
3265 uint8_t *ep = &sts->expected_dif[0];
3266 uint64_t lba = cmd->se_cmd.t_task_lba;
3267 uint8_t scsi_status, sense_key, asc, ascq;
3268 unsigned long flags;
3270 cmd->trc_flags |= TRC_DIF_ERR;
3272 cmd->a_guard = be16_to_cpu(*(uint16_t *)(ap + 0));
3273 cmd->a_app_tag = be16_to_cpu(*(uint16_t *)(ap + 2));
3274 cmd->a_ref_tag = be32_to_cpu(*(uint32_t *)(ap + 4));
3276 cmd->e_guard = be16_to_cpu(*(uint16_t *)(ep + 0));
3277 cmd->e_app_tag = be16_to_cpu(*(uint16_t *)(ep + 2));
3278 cmd->e_ref_tag = be32_to_cpu(*(uint32_t *)(ep + 4));
3280 ql_dbg(ql_dbg_tgt_dif, vha, 0xf075,
3281 "%s: aborted %d state %d\n", __func__, cmd->aborted, cmd->state);
3283 scsi_status = sense_key = asc = ascq = 0;
3285 /* check appl tag */
3286 if (cmd->e_app_tag != cmd->a_app_tag) {
3287 ql_dbg(ql_dbg_tgt_dif, vha, 0xffff,
3288 "App Tag ERR: cdb[%x] lba[%llx %llx] blks[%x] [Actual|Expected] "
3289 "Ref[%x|%x], App[%x|%x], "
3290 "Guard [%x|%x] cmd=%p ox_id[%04x]",
3291 cmd->cdb[0], lba, (lba+cmd->num_blks), cmd->num_blks,
3292 cmd->a_ref_tag, cmd->e_ref_tag,
3293 cmd->a_app_tag, cmd->e_app_tag,
3294 cmd->a_guard, cmd->e_guard,
3295 cmd, cmd->atio.u.isp24.fcp_hdr.ox_id);
3297 cmd->dif_err_code = DIF_ERR_APP;
3298 scsi_status = SAM_STAT_CHECK_CONDITION;
3299 sense_key = ABORTED_COMMAND;
3300 asc = 0x10;
3301 ascq = 0x2;
3304 /* check ref tag */
3305 if (cmd->e_ref_tag != cmd->a_ref_tag) {
3306 ql_dbg(ql_dbg_tgt_dif, vha, 0xffff,
3307 "Ref Tag ERR: cdb[%x] lba[%llx %llx] blks[%x] [Actual|Expected] "
3308 "Ref[%x|%x], App[%x|%x], "
3309 "Guard[%x|%x] cmd=%p ox_id[%04x] ",
3310 cmd->cdb[0], lba, (lba+cmd->num_blks), cmd->num_blks,
3311 cmd->a_ref_tag, cmd->e_ref_tag,
3312 cmd->a_app_tag, cmd->e_app_tag,
3313 cmd->a_guard, cmd->e_guard,
3314 cmd, cmd->atio.u.isp24.fcp_hdr.ox_id);
3316 cmd->dif_err_code = DIF_ERR_REF;
3317 scsi_status = SAM_STAT_CHECK_CONDITION;
3318 sense_key = ABORTED_COMMAND;
3319 asc = 0x10;
3320 ascq = 0x3;
3321 goto out;
3324 /* check guard */
3325 if (cmd->e_guard != cmd->a_guard) {
3326 ql_dbg(ql_dbg_tgt_dif, vha, 0xffff,
3327 "Guard ERR: cdb[%x] lba[%llx %llx] blks[%x] [Actual|Expected] "
3328 "Ref[%x|%x], App[%x|%x], "
3329 "Guard [%x|%x] cmd=%p ox_id[%04x]",
3330 cmd->cdb[0], lba, (lba+cmd->num_blks), cmd->num_blks,
3331 cmd->a_ref_tag, cmd->e_ref_tag,
3332 cmd->a_app_tag, cmd->e_app_tag,
3333 cmd->a_guard, cmd->e_guard,
3334 cmd, cmd->atio.u.isp24.fcp_hdr.ox_id);
3335 cmd->dif_err_code = DIF_ERR_GRD;
3336 scsi_status = SAM_STAT_CHECK_CONDITION;
3337 sense_key = ABORTED_COMMAND;
3338 asc = 0x10;
3339 ascq = 0x1;
3341 out:
3342 switch (cmd->state) {
3343 case QLA_TGT_STATE_NEED_DATA:
3344 /* handle_data will load DIF error code */
3345 cmd->state = QLA_TGT_STATE_DATA_IN;
3346 vha->hw->tgt.tgt_ops->handle_data(cmd);
3347 break;
3348 default:
3349 spin_lock_irqsave(&cmd->cmd_lock, flags);
3350 if (cmd->aborted) {
3351 spin_unlock_irqrestore(&cmd->cmd_lock, flags);
3352 vha->hw->tgt.tgt_ops->free_cmd(cmd);
3353 break;
3355 spin_unlock_irqrestore(&cmd->cmd_lock, flags);
3357 qlt_send_resp_ctio(vha, cmd, scsi_status, sense_key, asc, ascq);
3358 /* assume scsi status gets out on the wire.
3359 * Will not wait for completion.
3361 vha->hw->tgt.tgt_ops->free_cmd(cmd);
3362 break;
3366 /* If hardware_lock held on entry, might drop it, then reaquire */
3367 /* This function sends the appropriate CTIO to ISP 2xxx or 24xx */
3368 static int __qlt_send_term_imm_notif(struct scsi_qla_host *vha,
3369 struct imm_ntfy_from_isp *ntfy)
3371 struct nack_to_isp *nack;
3372 struct qla_hw_data *ha = vha->hw;
3373 request_t *pkt;
3374 int ret = 0;
3376 ql_dbg(ql_dbg_tgt_tmr, vha, 0xe01c,
3377 "Sending TERM ELS CTIO (ha=%p)\n", ha);
3379 pkt = (request_t *)qla2x00_alloc_iocbs(vha, NULL);
3380 if (pkt == NULL) {
3381 ql_dbg(ql_dbg_tgt, vha, 0xe080,
3382 "qla_target(%d): %s failed: unable to allocate "
3383 "request packet\n", vha->vp_idx, __func__);
3384 return -ENOMEM;
3387 pkt->entry_type = NOTIFY_ACK_TYPE;
3388 pkt->entry_count = 1;
3389 pkt->handle = QLA_TGT_SKIP_HANDLE;
3391 nack = (struct nack_to_isp *)pkt;
3392 nack->ox_id = ntfy->ox_id;
3394 nack->u.isp24.nport_handle = ntfy->u.isp24.nport_handle;
3395 if (le16_to_cpu(ntfy->u.isp24.status) == IMM_NTFY_ELS) {
3396 nack->u.isp24.flags = ntfy->u.isp24.flags &
3397 __constant_cpu_to_le32(NOTIFY24XX_FLAGS_PUREX_IOCB);
3400 /* terminate */
3401 nack->u.isp24.flags |=
3402 __constant_cpu_to_le16(NOTIFY_ACK_FLAGS_TERMINATE);
3404 nack->u.isp24.srr_rx_id = ntfy->u.isp24.srr_rx_id;
3405 nack->u.isp24.status = ntfy->u.isp24.status;
3406 nack->u.isp24.status_subcode = ntfy->u.isp24.status_subcode;
3407 nack->u.isp24.fw_handle = ntfy->u.isp24.fw_handle;
3408 nack->u.isp24.exchange_address = ntfy->u.isp24.exchange_address;
3409 nack->u.isp24.srr_rel_offs = ntfy->u.isp24.srr_rel_offs;
3410 nack->u.isp24.srr_ui = ntfy->u.isp24.srr_ui;
3411 nack->u.isp24.vp_index = ntfy->u.isp24.vp_index;
3413 qla2x00_start_iocbs(vha, vha->req);
3414 return ret;
3417 static void qlt_send_term_imm_notif(struct scsi_qla_host *vha,
3418 struct imm_ntfy_from_isp *imm, int ha_locked)
3420 unsigned long flags = 0;
3421 int rc;
3423 if (qlt_issue_marker(vha, ha_locked) < 0)
3424 return;
3426 if (ha_locked) {
3427 rc = __qlt_send_term_imm_notif(vha, imm);
3429 #if 0 /* Todo */
3430 if (rc == -ENOMEM)
3431 qlt_alloc_qfull_cmd(vha, imm, 0, 0);
3432 #else
3433 if (rc) {
3435 #endif
3436 goto done;
3439 spin_lock_irqsave(&vha->hw->hardware_lock, flags);
3440 rc = __qlt_send_term_imm_notif(vha, imm);
3442 #if 0 /* Todo */
3443 if (rc == -ENOMEM)
3444 qlt_alloc_qfull_cmd(vha, imm, 0, 0);
3445 #endif
3447 done:
3448 if (!ha_locked)
3449 spin_unlock_irqrestore(&vha->hw->hardware_lock, flags);
3452 /* If hardware_lock held on entry, might drop it, then reaquire */
3453 /* This function sends the appropriate CTIO to ISP 2xxx or 24xx */
3454 static int __qlt_send_term_exchange(struct scsi_qla_host *vha,
3455 struct qla_tgt_cmd *cmd,
3456 struct atio_from_isp *atio)
3458 struct ctio7_to_24xx *ctio24;
3459 struct qla_hw_data *ha = vha->hw;
3460 request_t *pkt;
3461 int ret = 0;
3462 uint16_t temp;
3464 ql_dbg(ql_dbg_tgt, vha, 0xe01c, "Sending TERM EXCH CTIO (ha=%p)\n", ha);
3466 pkt = (request_t *)qla2x00_alloc_iocbs_ready(vha, NULL);
3467 if (pkt == NULL) {
3468 ql_dbg(ql_dbg_tgt, vha, 0xe050,
3469 "qla_target(%d): %s failed: unable to allocate "
3470 "request packet\n", vha->vp_idx, __func__);
3471 return -ENOMEM;
3474 if (cmd != NULL) {
3475 if (cmd->state < QLA_TGT_STATE_PROCESSED) {
3476 ql_dbg(ql_dbg_tgt, vha, 0xe051,
3477 "qla_target(%d): Terminating cmd %p with "
3478 "incorrect state %d\n", vha->vp_idx, cmd,
3479 cmd->state);
3480 } else
3481 ret = 1;
3484 vha->tgt_counters.num_term_xchg_sent++;
3485 pkt->entry_count = 1;
3486 pkt->handle = QLA_TGT_SKIP_HANDLE | CTIO_COMPLETION_HANDLE_MARK;
3488 ctio24 = (struct ctio7_to_24xx *)pkt;
3489 ctio24->entry_type = CTIO_TYPE7;
3490 ctio24->nport_handle = CTIO7_NHANDLE_UNRECOGNIZED;
3491 ctio24->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
3492 ctio24->vp_index = vha->vp_idx;
3493 ctio24->initiator_id[0] = atio->u.isp24.fcp_hdr.s_id[2];
3494 ctio24->initiator_id[1] = atio->u.isp24.fcp_hdr.s_id[1];
3495 ctio24->initiator_id[2] = atio->u.isp24.fcp_hdr.s_id[0];
3496 ctio24->exchange_addr = atio->u.isp24.exchange_addr;
3497 ctio24->u.status1.flags = (atio->u.isp24.attr << 9) |
3498 cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_1 |
3499 CTIO7_FLAGS_TERMINATE);
3500 temp = be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id);
3501 ctio24->u.status1.ox_id = cpu_to_le16(temp);
3503 /* Most likely, it isn't needed */
3504 ctio24->u.status1.residual = get_unaligned((uint32_t *)
3505 &atio->u.isp24.fcp_cmnd.add_cdb[
3506 atio->u.isp24.fcp_cmnd.add_cdb_len]);
3507 if (ctio24->u.status1.residual != 0)
3508 ctio24->u.status1.scsi_status |= SS_RESIDUAL_UNDER;
3510 /* Memory Barrier */
3511 wmb();
3512 qla2x00_start_iocbs(vha, vha->req);
3513 return ret;
3516 static void qlt_send_term_exchange(struct scsi_qla_host *vha,
3517 struct qla_tgt_cmd *cmd, struct atio_from_isp *atio, int ha_locked,
3518 int ul_abort)
3520 unsigned long flags = 0;
3521 int rc;
3523 if (qlt_issue_marker(vha, ha_locked) < 0)
3524 return;
3526 if (ha_locked) {
3527 rc = __qlt_send_term_exchange(vha, cmd, atio);
3528 if (rc == -ENOMEM)
3529 qlt_alloc_qfull_cmd(vha, atio, 0, 0);
3530 goto done;
3532 spin_lock_irqsave(&vha->hw->hardware_lock, flags);
3533 rc = __qlt_send_term_exchange(vha, cmd, atio);
3534 if (rc == -ENOMEM)
3535 qlt_alloc_qfull_cmd(vha, atio, 0, 0);
3537 done:
3538 if (cmd && !ul_abort && !cmd->aborted) {
3539 if (cmd->sg_mapped)
3540 qlt_unmap_sg(vha, cmd);
3541 vha->hw->tgt.tgt_ops->free_cmd(cmd);
3544 if (!ha_locked)
3545 spin_unlock_irqrestore(&vha->hw->hardware_lock, flags);
3547 return;
3550 static void qlt_init_term_exchange(struct scsi_qla_host *vha)
3552 struct list_head free_list;
3553 struct qla_tgt_cmd *cmd, *tcmd;
3555 vha->hw->tgt.leak_exchg_thresh_hold =
3556 (vha->hw->cur_fw_xcb_count/100) * LEAK_EXCHG_THRESH_HOLD_PERCENT;
3558 cmd = tcmd = NULL;
3559 if (!list_empty(&vha->hw->tgt.q_full_list)) {
3560 INIT_LIST_HEAD(&free_list);
3561 list_splice_init(&vha->hw->tgt.q_full_list, &free_list);
3563 list_for_each_entry_safe(cmd, tcmd, &free_list, cmd_list) {
3564 list_del(&cmd->cmd_list);
3565 /* This cmd was never sent to TCM. There is no need
3566 * to schedule free or call free_cmd
3568 qlt_free_cmd(cmd);
3569 vha->hw->tgt.num_qfull_cmds_alloc--;
3572 vha->hw->tgt.num_qfull_cmds_dropped = 0;
3575 static void qlt_chk_exch_leak_thresh_hold(struct scsi_qla_host *vha)
3577 uint32_t total_leaked;
3579 total_leaked = vha->hw->tgt.num_qfull_cmds_dropped;
3581 if (vha->hw->tgt.leak_exchg_thresh_hold &&
3582 (total_leaked > vha->hw->tgt.leak_exchg_thresh_hold)) {
3584 ql_dbg(ql_dbg_tgt, vha, 0xe079,
3585 "Chip reset due to exchange starvation: %d/%d.\n",
3586 total_leaked, vha->hw->cur_fw_xcb_count);
3588 if (IS_P3P_TYPE(vha->hw))
3589 set_bit(FCOE_CTX_RESET_NEEDED, &vha->dpc_flags);
3590 else
3591 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
3592 qla2xxx_wake_dpc(vha);
3597 int qlt_abort_cmd(struct qla_tgt_cmd *cmd)
3599 struct qla_tgt *tgt = cmd->tgt;
3600 struct scsi_qla_host *vha = tgt->vha;
3601 struct se_cmd *se_cmd = &cmd->se_cmd;
3602 unsigned long flags;
3604 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf014,
3605 "qla_target(%d): terminating exchange for aborted cmd=%p "
3606 "(se_cmd=%p, tag=%llu)", vha->vp_idx, cmd, &cmd->se_cmd,
3607 se_cmd->tag);
3609 spin_lock_irqsave(&cmd->cmd_lock, flags);
3610 if (cmd->aborted) {
3611 spin_unlock_irqrestore(&cmd->cmd_lock, flags);
3613 * It's normal to see 2 calls in this path:
3614 * 1) XFER Rdy completion + CMD_T_ABORT
3615 * 2) TCM TMR - drain_state_list
3617 ql_dbg(ql_dbg_tgt_mgt, vha, 0xffff,
3618 "multiple abort. %p transport_state %x, t_state %x,"
3619 " se_cmd_flags %x \n", cmd, cmd->se_cmd.transport_state,
3620 cmd->se_cmd.t_state,cmd->se_cmd.se_cmd_flags);
3621 return EIO;
3623 cmd->aborted = 1;
3624 cmd->trc_flags |= TRC_ABORT;
3625 spin_unlock_irqrestore(&cmd->cmd_lock, flags);
3627 qlt_send_term_exchange(vha, cmd, &cmd->atio, 0, 1);
3628 return 0;
3630 EXPORT_SYMBOL(qlt_abort_cmd);
3632 void qlt_free_cmd(struct qla_tgt_cmd *cmd)
3634 struct fc_port *sess = cmd->sess;
3636 ql_dbg(ql_dbg_tgt, cmd->vha, 0xe074,
3637 "%s: se_cmd[%p] ox_id %04x\n",
3638 __func__, &cmd->se_cmd,
3639 be16_to_cpu(cmd->atio.u.isp24.fcp_hdr.ox_id));
3641 BUG_ON(cmd->cmd_in_wq);
3643 if (cmd->sg_mapped)
3644 qlt_unmap_sg(cmd->vha, cmd);
3646 if (!cmd->q_full)
3647 qlt_decr_num_pend_cmds(cmd->vha);
3649 BUG_ON(cmd->sg_mapped);
3650 cmd->jiffies_at_free = get_jiffies_64();
3651 if (unlikely(cmd->free_sg))
3652 kfree(cmd->sg);
3654 if (!sess || !sess->se_sess) {
3655 WARN_ON(1);
3656 return;
3658 cmd->jiffies_at_free = get_jiffies_64();
3659 percpu_ida_free(&sess->se_sess->sess_tag_pool, cmd->se_cmd.map_tag);
3661 EXPORT_SYMBOL(qlt_free_cmd);
3664 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
3666 static int qlt_term_ctio_exchange(struct scsi_qla_host *vha, void *ctio,
3667 struct qla_tgt_cmd *cmd, uint32_t status)
3669 int term = 0;
3671 if (cmd->se_cmd.prot_op)
3672 ql_dbg(ql_dbg_tgt_dif, vha, 0xffff,
3673 "Term DIF cmd: lba[0x%llx|%lld] len[0x%x] "
3674 "se_cmd=%p tag[%x] op %#x/%s",
3675 cmd->lba, cmd->lba,
3676 cmd->num_blks, &cmd->se_cmd,
3677 cmd->atio.u.isp24.exchange_addr,
3678 cmd->se_cmd.prot_op,
3679 prot_op_str(cmd->se_cmd.prot_op));
3681 if (ctio != NULL) {
3682 struct ctio7_from_24xx *c = (struct ctio7_from_24xx *)ctio;
3683 term = !(c->flags &
3684 cpu_to_le16(OF_TERM_EXCH));
3685 } else
3686 term = 1;
3688 if (term)
3689 qlt_send_term_exchange(vha, cmd, &cmd->atio, 1, 0);
3691 return term;
3694 /* ha->hardware_lock supposed to be held on entry */
3695 static inline struct qla_tgt_cmd *qlt_get_cmd(struct scsi_qla_host *vha,
3696 uint32_t handle)
3698 struct qla_hw_data *ha = vha->hw;
3700 handle--;
3701 if (ha->tgt.cmds[handle] != NULL) {
3702 struct qla_tgt_cmd *cmd = ha->tgt.cmds[handle];
3703 ha->tgt.cmds[handle] = NULL;
3704 return cmd;
3705 } else
3706 return NULL;
3709 /* ha->hardware_lock supposed to be held on entry */
3710 static struct qla_tgt_cmd *qlt_ctio_to_cmd(struct scsi_qla_host *vha,
3711 uint32_t handle, void *ctio)
3713 struct qla_tgt_cmd *cmd = NULL;
3715 /* Clear out internal marks */
3716 handle &= ~(CTIO_COMPLETION_HANDLE_MARK |
3717 CTIO_INTERMEDIATE_HANDLE_MARK);
3719 if (handle != QLA_TGT_NULL_HANDLE) {
3720 if (unlikely(handle == QLA_TGT_SKIP_HANDLE))
3721 return NULL;
3723 /* handle-1 is actually used */
3724 if (unlikely(handle > DEFAULT_OUTSTANDING_COMMANDS)) {
3725 ql_dbg(ql_dbg_tgt, vha, 0xe052,
3726 "qla_target(%d): Wrong handle %x received\n",
3727 vha->vp_idx, handle);
3728 return NULL;
3730 cmd = qlt_get_cmd(vha, handle);
3731 if (unlikely(cmd == NULL)) {
3732 ql_dbg(ql_dbg_tgt, vha, 0xe053,
3733 "qla_target(%d): Suspicious: unable to "
3734 "find the command with handle %x\n", vha->vp_idx,
3735 handle);
3736 return NULL;
3738 } else if (ctio != NULL) {
3739 /* We can't get loop ID from CTIO7 */
3740 ql_dbg(ql_dbg_tgt, vha, 0xe054,
3741 "qla_target(%d): Wrong CTIO received: QLA24xx doesn't "
3742 "support NULL handles\n", vha->vp_idx);
3743 return NULL;
3746 return cmd;
3749 /* hardware_lock should be held by caller. */
3750 static void
3751 qlt_abort_cmd_on_host_reset(struct scsi_qla_host *vha, struct qla_tgt_cmd *cmd)
3753 struct qla_hw_data *ha = vha->hw;
3754 uint32_t handle;
3756 if (cmd->sg_mapped)
3757 qlt_unmap_sg(vha, cmd);
3759 handle = qlt_make_handle(vha);
3761 /* TODO: fix debug message type and ids. */
3762 if (cmd->state == QLA_TGT_STATE_PROCESSED) {
3763 ql_dbg(ql_dbg_io, vha, 0xff00,
3764 "HOST-ABORT: handle=%d, state=PROCESSED.\n", handle);
3765 } else if (cmd->state == QLA_TGT_STATE_NEED_DATA) {
3766 cmd->write_data_transferred = 0;
3767 cmd->state = QLA_TGT_STATE_DATA_IN;
3769 ql_dbg(ql_dbg_io, vha, 0xff01,
3770 "HOST-ABORT: handle=%d, state=DATA_IN.\n", handle);
3772 ha->tgt.tgt_ops->handle_data(cmd);
3773 return;
3774 } else {
3775 ql_dbg(ql_dbg_io, vha, 0xff03,
3776 "HOST-ABORT: handle=%d, state=BAD(%d).\n", handle,
3777 cmd->state);
3778 dump_stack();
3781 cmd->trc_flags |= TRC_FLUSH;
3782 ha->tgt.tgt_ops->free_cmd(cmd);
3785 void
3786 qlt_host_reset_handler(struct qla_hw_data *ha)
3788 struct qla_tgt_cmd *cmd;
3789 unsigned long flags;
3790 scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
3791 scsi_qla_host_t *vha = NULL;
3792 struct qla_tgt *tgt = base_vha->vha_tgt.qla_tgt;
3793 uint32_t i;
3795 if (!base_vha->hw->tgt.tgt_ops)
3796 return;
3798 if (!tgt || qla_ini_mode_enabled(base_vha)) {
3799 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf003,
3800 "Target mode disabled\n");
3801 return;
3804 ql_dbg(ql_dbg_tgt_mgt, vha, 0xff10,
3805 "HOST-ABORT-HNDLR: base_vha->dpc_flags=%lx.\n",
3806 base_vha->dpc_flags);
3808 spin_lock_irqsave(&ha->hardware_lock, flags);
3809 for (i = 1; i < DEFAULT_OUTSTANDING_COMMANDS + 1; i++) {
3810 cmd = qlt_get_cmd(base_vha, i);
3811 if (!cmd)
3812 continue;
3813 /* ha->tgt.cmds entry is cleared by qlt_get_cmd. */
3814 vha = cmd->vha;
3815 qlt_abort_cmd_on_host_reset(vha, cmd);
3817 spin_unlock_irqrestore(&ha->hardware_lock, flags);
3822 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
3824 static void qlt_do_ctio_completion(struct scsi_qla_host *vha, uint32_t handle,
3825 uint32_t status, void *ctio)
3827 struct qla_hw_data *ha = vha->hw;
3828 struct se_cmd *se_cmd;
3829 struct qla_tgt_cmd *cmd;
3831 if (handle & CTIO_INTERMEDIATE_HANDLE_MARK) {
3832 /* That could happen only in case of an error/reset/abort */
3833 if (status != CTIO_SUCCESS) {
3834 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01d,
3835 "Intermediate CTIO received"
3836 " (status %x)\n", status);
3838 return;
3841 cmd = qlt_ctio_to_cmd(vha, handle, ctio);
3842 if (cmd == NULL)
3843 return;
3845 se_cmd = &cmd->se_cmd;
3846 cmd->cmd_sent_to_fw = 0;
3848 qlt_unmap_sg(vha, cmd);
3850 if (unlikely(status != CTIO_SUCCESS)) {
3851 switch (status & 0xFFFF) {
3852 case CTIO_LIP_RESET:
3853 case CTIO_TARGET_RESET:
3854 case CTIO_ABORTED:
3855 /* driver request abort via Terminate exchange */
3856 case CTIO_TIMEOUT:
3857 case CTIO_INVALID_RX_ID:
3858 /* They are OK */
3859 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf058,
3860 "qla_target(%d): CTIO with "
3861 "status %#x received, state %x, se_cmd %p, "
3862 "(LIP_RESET=e, ABORTED=2, TARGET_RESET=17, "
3863 "TIMEOUT=b, INVALID_RX_ID=8)\n", vha->vp_idx,
3864 status, cmd->state, se_cmd);
3865 break;
3867 case CTIO_PORT_LOGGED_OUT:
3868 case CTIO_PORT_UNAVAILABLE:
3870 int logged_out =
3871 (status & 0xFFFF) == CTIO_PORT_LOGGED_OUT;
3873 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf059,
3874 "qla_target(%d): CTIO with %s status %x "
3875 "received (state %x, se_cmd %p)\n", vha->vp_idx,
3876 logged_out ? "PORT LOGGED OUT" : "PORT UNAVAILABLE",
3877 status, cmd->state, se_cmd);
3879 if (logged_out && cmd->sess) {
3881 * Session is already logged out, but we need
3882 * to notify initiator, who's not aware of this
3884 cmd->sess->logout_on_delete = 0;
3885 cmd->sess->send_els_logo = 1;
3886 ql_dbg(ql_dbg_disc, vha, 0xffff,
3887 "%s %d %8phC post del sess\n",
3888 __func__, __LINE__, cmd->sess->port_name);
3890 qlt_schedule_sess_for_deletion_lock(cmd->sess);
3892 break;
3894 case CTIO_DIF_ERROR: {
3895 struct ctio_crc_from_fw *crc =
3896 (struct ctio_crc_from_fw *)ctio;
3897 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf073,
3898 "qla_target(%d): CTIO with DIF_ERROR status %x "
3899 "received (state %x, ulp_cmd %p) actual_dif[0x%llx] "
3900 "expect_dif[0x%llx]\n",
3901 vha->vp_idx, status, cmd->state, se_cmd,
3902 *((u64 *)&crc->actual_dif[0]),
3903 *((u64 *)&crc->expected_dif[0]));
3905 qlt_handle_dif_error(vha, cmd, ctio);
3906 return;
3908 default:
3909 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05b,
3910 "qla_target(%d): CTIO with error status 0x%x received (state %x, se_cmd %p\n",
3911 vha->vp_idx, status, cmd->state, se_cmd);
3912 break;
3916 /* "cmd->aborted" means
3917 * cmd is already aborted/terminated, we don't
3918 * need to terminate again. The exchange is already
3919 * cleaned up/freed at FW level. Just cleanup at driver
3920 * level.
3922 if ((cmd->state != QLA_TGT_STATE_NEED_DATA) &&
3923 (!cmd->aborted)) {
3924 cmd->trc_flags |= TRC_CTIO_ERR;
3925 if (qlt_term_ctio_exchange(vha, ctio, cmd, status))
3926 return;
3930 if (cmd->state == QLA_TGT_STATE_PROCESSED) {
3931 cmd->trc_flags |= TRC_CTIO_DONE;
3932 } else if (cmd->state == QLA_TGT_STATE_NEED_DATA) {
3933 cmd->state = QLA_TGT_STATE_DATA_IN;
3935 if (status == CTIO_SUCCESS)
3936 cmd->write_data_transferred = 1;
3938 ha->tgt.tgt_ops->handle_data(cmd);
3939 return;
3940 } else if (cmd->aborted) {
3941 cmd->trc_flags |= TRC_CTIO_ABORTED;
3942 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01e,
3943 "Aborted command %p (tag %lld) finished\n", cmd, se_cmd->tag);
3944 } else {
3945 cmd->trc_flags |= TRC_CTIO_STRANGE;
3946 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05c,
3947 "qla_target(%d): A command in state (%d) should "
3948 "not return a CTIO complete\n", vha->vp_idx, cmd->state);
3951 if (unlikely(status != CTIO_SUCCESS) &&
3952 !cmd->aborted) {
3953 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01f, "Finishing failed CTIO\n");
3954 dump_stack();
3957 ha->tgt.tgt_ops->free_cmd(cmd);
3960 static inline int qlt_get_fcp_task_attr(struct scsi_qla_host *vha,
3961 uint8_t task_codes)
3963 int fcp_task_attr;
3965 switch (task_codes) {
3966 case ATIO_SIMPLE_QUEUE:
3967 fcp_task_attr = TCM_SIMPLE_TAG;
3968 break;
3969 case ATIO_HEAD_OF_QUEUE:
3970 fcp_task_attr = TCM_HEAD_TAG;
3971 break;
3972 case ATIO_ORDERED_QUEUE:
3973 fcp_task_attr = TCM_ORDERED_TAG;
3974 break;
3975 case ATIO_ACA_QUEUE:
3976 fcp_task_attr = TCM_ACA_TAG;
3977 break;
3978 case ATIO_UNTAGGED:
3979 fcp_task_attr = TCM_SIMPLE_TAG;
3980 break;
3981 default:
3982 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05d,
3983 "qla_target: unknown task code %x, use ORDERED instead\n",
3984 task_codes);
3985 fcp_task_attr = TCM_ORDERED_TAG;
3986 break;
3989 return fcp_task_attr;
3992 static struct fc_port *qlt_make_local_sess(struct scsi_qla_host *,
3993 uint8_t *);
3995 * Process context for I/O path into tcm_qla2xxx code
3997 static void __qlt_do_work(struct qla_tgt_cmd *cmd)
3999 scsi_qla_host_t *vha = cmd->vha;
4000 struct qla_hw_data *ha = vha->hw;
4001 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
4002 struct fc_port *sess = cmd->sess;
4003 struct atio_from_isp *atio = &cmd->atio;
4004 unsigned char *cdb;
4005 unsigned long flags;
4006 uint32_t data_length;
4007 int ret, fcp_task_attr, data_dir, bidi = 0;
4009 cmd->cmd_in_wq = 0;
4010 cmd->trc_flags |= TRC_DO_WORK;
4011 if (tgt->tgt_stop)
4012 goto out_term;
4014 if (cmd->aborted) {
4015 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf082,
4016 "cmd with tag %u is aborted\n",
4017 cmd->atio.u.isp24.exchange_addr);
4018 goto out_term;
4021 spin_lock_init(&cmd->cmd_lock);
4022 cdb = &atio->u.isp24.fcp_cmnd.cdb[0];
4023 cmd->se_cmd.tag = atio->u.isp24.exchange_addr;
4024 cmd->unpacked_lun = scsilun_to_int(
4025 (struct scsi_lun *)&atio->u.isp24.fcp_cmnd.lun);
4027 if (atio->u.isp24.fcp_cmnd.rddata &&
4028 atio->u.isp24.fcp_cmnd.wrdata) {
4029 bidi = 1;
4030 data_dir = DMA_TO_DEVICE;
4031 } else if (atio->u.isp24.fcp_cmnd.rddata)
4032 data_dir = DMA_FROM_DEVICE;
4033 else if (atio->u.isp24.fcp_cmnd.wrdata)
4034 data_dir = DMA_TO_DEVICE;
4035 else
4036 data_dir = DMA_NONE;
4038 fcp_task_attr = qlt_get_fcp_task_attr(vha,
4039 atio->u.isp24.fcp_cmnd.task_attr);
4040 data_length = be32_to_cpu(get_unaligned((uint32_t *)
4041 &atio->u.isp24.fcp_cmnd.add_cdb[
4042 atio->u.isp24.fcp_cmnd.add_cdb_len]));
4044 ret = ha->tgt.tgt_ops->handle_cmd(vha, cmd, cdb, data_length,
4045 fcp_task_attr, data_dir, bidi);
4046 if (ret != 0)
4047 goto out_term;
4049 * Drop extra session reference from qla_tgt_handle_cmd_for_atio*(
4051 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
4052 ha->tgt.tgt_ops->put_sess(sess);
4053 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
4054 return;
4056 out_term:
4057 ql_dbg(ql_dbg_io, vha, 0x3060, "Terminating work cmd %p", cmd);
4059 * cmd has not sent to target yet, so pass NULL as the second
4060 * argument to qlt_send_term_exchange() and free the memory here.
4062 cmd->trc_flags |= TRC_DO_WORK_ERR;
4063 spin_lock_irqsave(&ha->hardware_lock, flags);
4064 qlt_send_term_exchange(vha, NULL, &cmd->atio, 1, 0);
4066 qlt_decr_num_pend_cmds(vha);
4067 percpu_ida_free(&sess->se_sess->sess_tag_pool, cmd->se_cmd.map_tag);
4068 spin_unlock_irqrestore(&ha->hardware_lock, flags);
4070 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
4071 ha->tgt.tgt_ops->put_sess(sess);
4072 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
4075 static void qlt_do_work(struct work_struct *work)
4077 struct qla_tgt_cmd *cmd = container_of(work, struct qla_tgt_cmd, work);
4078 scsi_qla_host_t *vha = cmd->vha;
4079 unsigned long flags;
4081 spin_lock_irqsave(&vha->cmd_list_lock, flags);
4082 list_del(&cmd->cmd_list);
4083 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
4085 __qlt_do_work(cmd);
4088 static struct qla_tgt_cmd *qlt_get_tag(scsi_qla_host_t *vha,
4089 struct fc_port *sess,
4090 struct atio_from_isp *atio)
4092 struct se_session *se_sess = sess->se_sess;
4093 struct qla_tgt_cmd *cmd;
4094 int tag;
4096 tag = percpu_ida_alloc(&se_sess->sess_tag_pool, TASK_RUNNING);
4097 if (tag < 0)
4098 return NULL;
4100 cmd = &((struct qla_tgt_cmd *)se_sess->sess_cmd_map)[tag];
4101 memset(cmd, 0, sizeof(struct qla_tgt_cmd));
4103 memcpy(&cmd->atio, atio, sizeof(*atio));
4104 cmd->state = QLA_TGT_STATE_NEW;
4105 cmd->tgt = vha->vha_tgt.qla_tgt;
4106 qlt_incr_num_pend_cmds(vha);
4107 cmd->vha = vha;
4108 cmd->se_cmd.map_tag = tag;
4109 cmd->sess = sess;
4110 cmd->loop_id = sess->loop_id;
4111 cmd->conf_compl_supported = sess->conf_compl_supported;
4113 cmd->trc_flags = 0;
4114 cmd->jiffies_at_alloc = get_jiffies_64();
4116 cmd->reset_count = vha->hw->chip_reset;
4118 return cmd;
4121 static void qlt_send_busy(struct scsi_qla_host *, struct atio_from_isp *,
4122 uint16_t);
4124 static void qlt_create_sess_from_atio(struct work_struct *work)
4126 struct qla_tgt_sess_op *op = container_of(work,
4127 struct qla_tgt_sess_op, work);
4128 scsi_qla_host_t *vha = op->vha;
4129 struct qla_hw_data *ha = vha->hw;
4130 struct fc_port *sess;
4131 struct qla_tgt_cmd *cmd;
4132 unsigned long flags;
4133 uint8_t *s_id = op->atio.u.isp24.fcp_hdr.s_id;
4135 spin_lock_irqsave(&vha->cmd_list_lock, flags);
4136 list_del(&op->cmd_list);
4137 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
4139 if (op->aborted) {
4140 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf083,
4141 "sess_op with tag %u is aborted\n",
4142 op->atio.u.isp24.exchange_addr);
4143 goto out_term;
4146 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf022,
4147 "qla_target(%d): Unable to find wwn login"
4148 " (s_id %x:%x:%x), trying to create it manually\n",
4149 vha->vp_idx, s_id[0], s_id[1], s_id[2]);
4151 if (op->atio.u.raw.entry_count > 1) {
4152 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf023,
4153 "Dropping multy entry atio %p\n", &op->atio);
4154 goto out_term;
4157 sess = qlt_make_local_sess(vha, s_id);
4158 /* sess has an extra creation ref. */
4160 if (!sess)
4161 goto out_term;
4163 * Now obtain a pre-allocated session tag using the original op->atio
4164 * packet header, and dispatch into __qlt_do_work() using the existing
4165 * process context.
4167 cmd = qlt_get_tag(vha, sess, &op->atio);
4168 if (!cmd) {
4169 spin_lock_irqsave(&ha->hardware_lock, flags);
4170 qlt_send_busy(vha, &op->atio, SAM_STAT_BUSY);
4171 ha->tgt.tgt_ops->put_sess(sess);
4172 spin_unlock_irqrestore(&ha->hardware_lock, flags);
4173 kfree(op);
4174 return;
4178 * __qlt_do_work() will call qlt_put_sess() to release
4179 * the extra reference taken above by qlt_make_local_sess()
4181 __qlt_do_work(cmd);
4182 kfree(op);
4183 return;
4184 out_term:
4185 spin_lock_irqsave(&ha->hardware_lock, flags);
4186 qlt_send_term_exchange(vha, NULL, &op->atio, 1, 0);
4187 spin_unlock_irqrestore(&ha->hardware_lock, flags);
4188 kfree(op);
4191 /* ha->hardware_lock supposed to be held on entry */
4192 static int qlt_handle_cmd_for_atio(struct scsi_qla_host *vha,
4193 struct atio_from_isp *atio)
4195 struct qla_hw_data *ha = vha->hw;
4196 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
4197 struct fc_port *sess;
4198 struct qla_tgt_cmd *cmd;
4199 unsigned long flags;
4201 if (unlikely(tgt->tgt_stop)) {
4202 ql_dbg(ql_dbg_io, vha, 0x3061,
4203 "New command while device %p is shutting down\n", tgt);
4204 return -EFAULT;
4207 sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha, atio->u.isp24.fcp_hdr.s_id);
4208 if (unlikely(!sess)) {
4209 struct qla_tgt_sess_op *op = kzalloc(sizeof(struct qla_tgt_sess_op),
4210 GFP_ATOMIC);
4211 if (!op)
4212 return -ENOMEM;
4214 memcpy(&op->atio, atio, sizeof(*atio));
4215 op->vha = vha;
4217 spin_lock(&vha->cmd_list_lock);
4218 list_add_tail(&op->cmd_list, &vha->qla_sess_op_cmd_list);
4219 spin_unlock(&vha->cmd_list_lock);
4221 INIT_WORK(&op->work, qlt_create_sess_from_atio);
4222 queue_work(qla_tgt_wq, &op->work);
4223 return 0;
4226 /* Another WWN used to have our s_id. Our PLOGI scheduled its
4227 * session deletion, but it's still in sess_del_work wq */
4228 if (sess->deleted) {
4229 ql_dbg(ql_dbg_io, vha, 0x3061,
4230 "New command while old session %p is being deleted\n",
4231 sess);
4232 return -EFAULT;
4236 * Do kref_get() before returning + dropping qla_hw_data->hardware_lock.
4238 if (!kref_get_unless_zero(&sess->sess_kref)) {
4239 ql_dbg(ql_dbg_tgt, vha, 0xffff,
4240 "%s: kref_get fail, %8phC oxid %x \n",
4241 __func__, sess->port_name,
4242 be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id));
4243 return -EFAULT;
4246 cmd = qlt_get_tag(vha, sess, atio);
4247 if (!cmd) {
4248 ql_dbg(ql_dbg_io, vha, 0x3062,
4249 "qla_target(%d): Allocation of cmd failed\n", vha->vp_idx);
4250 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
4251 ha->tgt.tgt_ops->put_sess(sess);
4252 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
4253 return -ENOMEM;
4256 cmd->cmd_in_wq = 1;
4257 cmd->trc_flags |= TRC_NEW_CMD;
4258 cmd->se_cmd.cpuid = ha->msix_count ?
4259 ha->tgt.rspq_vector_cpuid : WORK_CPU_UNBOUND;
4261 spin_lock_irqsave(&vha->cmd_list_lock, flags);
4262 list_add_tail(&cmd->cmd_list, &vha->qla_cmd_list);
4263 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
4265 INIT_WORK(&cmd->work, qlt_do_work);
4266 if (ha->msix_count) {
4267 if (cmd->atio.u.isp24.fcp_cmnd.rddata)
4268 queue_work_on(smp_processor_id(), qla_tgt_wq,
4269 &cmd->work);
4270 else
4271 queue_work_on(cmd->se_cmd.cpuid, qla_tgt_wq,
4272 &cmd->work);
4273 } else {
4274 queue_work(qla_tgt_wq, &cmd->work);
4276 return 0;
4280 /* ha->hardware_lock supposed to be held on entry */
4281 static int qlt_issue_task_mgmt(struct fc_port *sess, u64 lun,
4282 int fn, void *iocb, int flags)
4284 struct scsi_qla_host *vha = sess->vha;
4285 struct qla_hw_data *ha = vha->hw;
4286 struct qla_tgt_mgmt_cmd *mcmd;
4287 struct atio_from_isp *a = (struct atio_from_isp *)iocb;
4288 int res;
4290 mcmd = mempool_alloc(qla_tgt_mgmt_cmd_mempool, GFP_ATOMIC);
4291 if (!mcmd) {
4292 ql_dbg(ql_dbg_tgt_tmr, vha, 0x10009,
4293 "qla_target(%d): Allocation of management "
4294 "command failed, some commands and their data could "
4295 "leak\n", vha->vp_idx);
4296 return -ENOMEM;
4298 memset(mcmd, 0, sizeof(*mcmd));
4299 mcmd->sess = sess;
4301 if (iocb) {
4302 memcpy(&mcmd->orig_iocb.imm_ntfy, iocb,
4303 sizeof(mcmd->orig_iocb.imm_ntfy));
4305 mcmd->tmr_func = fn;
4306 mcmd->flags = flags;
4307 mcmd->reset_count = vha->hw->chip_reset;
4309 switch (fn) {
4310 case QLA_TGT_LUN_RESET:
4311 abort_cmds_for_lun(vha, lun, a->u.isp24.fcp_hdr.s_id);
4312 break;
4315 res = ha->tgt.tgt_ops->handle_tmr(mcmd, lun, mcmd->tmr_func, 0);
4316 if (res != 0) {
4317 ql_dbg(ql_dbg_tgt_tmr, vha, 0x1000b,
4318 "qla_target(%d): tgt.tgt_ops->handle_tmr() failed: %d\n",
4319 sess->vha->vp_idx, res);
4320 mempool_free(mcmd, qla_tgt_mgmt_cmd_mempool);
4321 return -EFAULT;
4324 return 0;
4327 /* ha->hardware_lock supposed to be held on entry */
4328 static int qlt_handle_task_mgmt(struct scsi_qla_host *vha, void *iocb)
4330 struct atio_from_isp *a = (struct atio_from_isp *)iocb;
4331 struct qla_hw_data *ha = vha->hw;
4332 struct qla_tgt *tgt;
4333 struct fc_port *sess;
4334 uint32_t lun, unpacked_lun;
4335 int fn;
4336 unsigned long flags;
4338 tgt = vha->vha_tgt.qla_tgt;
4340 lun = a->u.isp24.fcp_cmnd.lun;
4341 fn = a->u.isp24.fcp_cmnd.task_mgmt_flags;
4343 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
4344 sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha,
4345 a->u.isp24.fcp_hdr.s_id);
4346 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
4348 unpacked_lun = scsilun_to_int((struct scsi_lun *)&lun);
4350 if (!sess) {
4351 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf024,
4352 "qla_target(%d): task mgmt fn 0x%x for "
4353 "non-existant session\n", vha->vp_idx, fn);
4354 return qlt_sched_sess_work(tgt, QLA_TGT_SESS_WORK_TM, iocb,
4355 sizeof(struct atio_from_isp));
4358 if (sess->deleted)
4359 return -EFAULT;
4361 return qlt_issue_task_mgmt(sess, unpacked_lun, fn, iocb, 0);
4364 /* ha->hardware_lock supposed to be held on entry */
4365 static int __qlt_abort_task(struct scsi_qla_host *vha,
4366 struct imm_ntfy_from_isp *iocb, struct fc_port *sess)
4368 struct atio_from_isp *a = (struct atio_from_isp *)iocb;
4369 struct qla_hw_data *ha = vha->hw;
4370 struct qla_tgt_mgmt_cmd *mcmd;
4371 uint32_t lun, unpacked_lun;
4372 int rc;
4374 mcmd = mempool_alloc(qla_tgt_mgmt_cmd_mempool, GFP_ATOMIC);
4375 if (mcmd == NULL) {
4376 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05f,
4377 "qla_target(%d): %s: Allocation of ABORT cmd failed\n",
4378 vha->vp_idx, __func__);
4379 return -ENOMEM;
4381 memset(mcmd, 0, sizeof(*mcmd));
4383 mcmd->sess = sess;
4384 memcpy(&mcmd->orig_iocb.imm_ntfy, iocb,
4385 sizeof(mcmd->orig_iocb.imm_ntfy));
4387 lun = a->u.isp24.fcp_cmnd.lun;
4388 unpacked_lun = scsilun_to_int((struct scsi_lun *)&lun);
4389 mcmd->reset_count = vha->hw->chip_reset;
4390 mcmd->tmr_func = QLA_TGT_2G_ABORT_TASK;
4392 rc = ha->tgt.tgt_ops->handle_tmr(mcmd, unpacked_lun, mcmd->tmr_func,
4393 le16_to_cpu(iocb->u.isp2x.seq_id));
4394 if (rc != 0) {
4395 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf060,
4396 "qla_target(%d): tgt_ops->handle_tmr() failed: %d\n",
4397 vha->vp_idx, rc);
4398 mempool_free(mcmd, qla_tgt_mgmt_cmd_mempool);
4399 return -EFAULT;
4402 return 0;
4405 /* ha->hardware_lock supposed to be held on entry */
4406 static int qlt_abort_task(struct scsi_qla_host *vha,
4407 struct imm_ntfy_from_isp *iocb)
4409 struct qla_hw_data *ha = vha->hw;
4410 struct fc_port *sess;
4411 int loop_id;
4412 unsigned long flags;
4414 loop_id = GET_TARGET_ID(ha, (struct atio_from_isp *)iocb);
4416 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
4417 sess = ha->tgt.tgt_ops->find_sess_by_loop_id(vha, loop_id);
4418 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
4420 if (sess == NULL) {
4421 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf025,
4422 "qla_target(%d): task abort for unexisting "
4423 "session\n", vha->vp_idx);
4424 return qlt_sched_sess_work(vha->vha_tgt.qla_tgt,
4425 QLA_TGT_SESS_WORK_ABORT, iocb, sizeof(*iocb));
4428 return __qlt_abort_task(vha, iocb, sess);
4431 void qlt_logo_completion_handler(fc_port_t *fcport, int rc)
4433 if (rc != MBS_COMMAND_COMPLETE) {
4434 ql_dbg(ql_dbg_tgt_mgt, fcport->vha, 0xf093,
4435 "%s: se_sess %p / sess %p from"
4436 " port %8phC loop_id %#04x s_id %02x:%02x:%02x"
4437 " LOGO failed: %#x\n",
4438 __func__,
4439 fcport->se_sess,
4440 fcport,
4441 fcport->port_name, fcport->loop_id,
4442 fcport->d_id.b.domain, fcport->d_id.b.area,
4443 fcport->d_id.b.al_pa, rc);
4446 fcport->logout_completed = 1;
4450 * ha->hardware_lock supposed to be held on entry (to protect tgt->sess_list)
4452 * Schedules sessions with matching port_id/loop_id but different wwn for
4453 * deletion. Returns existing session with matching wwn if present.
4454 * Null otherwise.
4456 struct fc_port *
4457 qlt_find_sess_invalidate_other(scsi_qla_host_t *vha, uint64_t wwn,
4458 port_id_t port_id, uint16_t loop_id, struct fc_port **conflict_sess)
4460 struct fc_port *sess = NULL, *other_sess;
4461 uint64_t other_wwn;
4463 *conflict_sess = NULL;
4465 list_for_each_entry(other_sess, &vha->vp_fcports, list) {
4467 other_wwn = wwn_to_u64(other_sess->port_name);
4469 if (wwn == other_wwn) {
4470 WARN_ON(sess);
4471 sess = other_sess;
4472 continue;
4475 /* find other sess with nport_id collision */
4476 if (port_id.b24 == other_sess->d_id.b24) {
4477 if (loop_id != other_sess->loop_id) {
4478 ql_dbg(ql_dbg_tgt_tmr, vha, 0x1000c,
4479 "Invalidating sess %p loop_id %d wwn %llx.\n",
4480 other_sess, other_sess->loop_id, other_wwn);
4483 * logout_on_delete is set by default, but another
4484 * session that has the same s_id/loop_id combo
4485 * might have cleared it when requested this session
4486 * deletion, so don't touch it
4488 qlt_schedule_sess_for_deletion(other_sess, true);
4489 } else {
4491 * Another wwn used to have our s_id/loop_id
4492 * kill the session, but don't free the loop_id
4494 ql_dbg(ql_dbg_tgt_tmr, vha, 0xffff,
4495 "Invalidating sess %p loop_id %d wwn %llx.\n",
4496 other_sess, other_sess->loop_id, other_wwn);
4499 other_sess->keep_nport_handle = 1;
4500 *conflict_sess = other_sess;
4501 qlt_schedule_sess_for_deletion(other_sess,
4502 true);
4504 continue;
4507 /* find other sess with nport handle collision */
4508 if ((loop_id == other_sess->loop_id) &&
4509 (loop_id != FC_NO_LOOP_ID)) {
4510 ql_dbg(ql_dbg_tgt_tmr, vha, 0x1000d,
4511 "Invalidating sess %p loop_id %d wwn %llx.\n",
4512 other_sess, other_sess->loop_id, other_wwn);
4514 /* Same loop_id but different s_id
4515 * Ok to kill and logout */
4516 qlt_schedule_sess_for_deletion(other_sess, true);
4520 return sess;
4523 /* Abort any commands for this s_id waiting on qla_tgt_wq workqueue */
4524 static int abort_cmds_for_s_id(struct scsi_qla_host *vha, port_id_t *s_id)
4526 struct qla_tgt_sess_op *op;
4527 struct qla_tgt_cmd *cmd;
4528 uint32_t key;
4529 int count = 0;
4531 key = (((u32)s_id->b.domain << 16) |
4532 ((u32)s_id->b.area << 8) |
4533 ((u32)s_id->b.al_pa));
4535 spin_lock(&vha->cmd_list_lock);
4536 list_for_each_entry(op, &vha->qla_sess_op_cmd_list, cmd_list) {
4537 uint32_t op_key = sid_to_key(op->atio.u.isp24.fcp_hdr.s_id);
4539 if (op_key == key) {
4540 op->aborted = true;
4541 count++;
4545 list_for_each_entry(op, &vha->unknown_atio_list, cmd_list) {
4546 uint32_t op_key = sid_to_key(op->atio.u.isp24.fcp_hdr.s_id);
4547 if (op_key == key) {
4548 op->aborted = true;
4549 count++;
4553 list_for_each_entry(cmd, &vha->qla_cmd_list, cmd_list) {
4554 uint32_t cmd_key = sid_to_key(cmd->atio.u.isp24.fcp_hdr.s_id);
4555 if (cmd_key == key) {
4556 cmd->aborted = 1;
4557 count++;
4560 spin_unlock(&vha->cmd_list_lock);
4562 return count;
4566 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
4568 static int qlt_24xx_handle_els(struct scsi_qla_host *vha,
4569 struct imm_ntfy_from_isp *iocb)
4571 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
4572 struct qla_hw_data *ha = vha->hw;
4573 struct fc_port *sess = NULL, *conflict_sess = NULL;
4574 uint64_t wwn;
4575 port_id_t port_id;
4576 uint16_t loop_id;
4577 uint16_t wd3_lo;
4578 int res = 0;
4579 struct qlt_plogi_ack_t *pla;
4580 unsigned long flags;
4582 wwn = wwn_to_u64(iocb->u.isp24.port_name);
4584 port_id.b.domain = iocb->u.isp24.port_id[2];
4585 port_id.b.area = iocb->u.isp24.port_id[1];
4586 port_id.b.al_pa = iocb->u.isp24.port_id[0];
4587 port_id.b.rsvd_1 = 0;
4589 loop_id = le16_to_cpu(iocb->u.isp24.nport_handle);
4591 ql_dbg(ql_dbg_disc, vha, 0xf026,
4592 "qla_target(%d): Port ID: %02x:%02x:%02x ELS opcode: 0x%02x lid %d %8phC\n",
4593 vha->vp_idx, iocb->u.isp24.port_id[2],
4594 iocb->u.isp24.port_id[1], iocb->u.isp24.port_id[0],
4595 iocb->u.isp24.status_subcode, loop_id,
4596 iocb->u.isp24.port_name);
4598 /* res = 1 means ack at the end of thread
4599 * res = 0 means ack async/later.
4601 switch (iocb->u.isp24.status_subcode) {
4602 case ELS_PLOGI:
4604 /* Mark all stale commands in qla_tgt_wq for deletion */
4605 abort_cmds_for_s_id(vha, &port_id);
4607 if (wwn) {
4608 spin_lock_irqsave(&tgt->ha->tgt.sess_lock, flags);
4609 sess = qlt_find_sess_invalidate_other(vha, wwn,
4610 port_id, loop_id, &conflict_sess);
4611 spin_unlock_irqrestore(&tgt->ha->tgt.sess_lock, flags);
4614 if (IS_SW_RESV_ADDR(port_id)) {
4615 res = 1;
4616 break;
4619 pla = qlt_plogi_ack_find_add(vha, &port_id, iocb);
4620 if (!pla) {
4621 qlt_send_term_imm_notif(vha, iocb, 1);
4622 break;
4625 res = 0;
4627 if (conflict_sess) {
4628 conflict_sess->login_gen++;
4629 qlt_plogi_ack_link(vha, pla, conflict_sess,
4630 QLT_PLOGI_LINK_CONFLICT);
4633 if (!sess) {
4634 pla->ref_count++;
4635 qla24xx_post_newsess_work(vha, &port_id,
4636 iocb->u.isp24.port_name, pla);
4637 res = 0;
4638 break;
4641 qlt_plogi_ack_link(vha, pla, sess, QLT_PLOGI_LINK_SAME_WWN);
4642 sess->fw_login_state = DSC_LS_PLOGI_PEND;
4643 sess->d_id = port_id;
4644 sess->login_gen++;
4646 switch (sess->disc_state) {
4647 case DSC_DELETED:
4648 qlt_plogi_ack_unref(vha, pla);
4649 break;
4651 default:
4653 * Under normal circumstances we want to release nport handle
4654 * during LOGO process to avoid nport handle leaks inside FW.
4655 * The exception is when LOGO is done while another PLOGI with
4656 * the same nport handle is waiting as might be the case here.
4657 * Note: there is always a possibily of a race where session
4658 * deletion has already started for other reasons (e.g. ACL
4659 * removal) and now PLOGI arrives:
4660 * 1. if PLOGI arrived in FW after nport handle has been freed,
4661 * FW must have assigned this PLOGI a new/same handle and we
4662 * can proceed ACK'ing it as usual when session deletion
4663 * completes.
4664 * 2. if PLOGI arrived in FW before LOGO with LCF_FREE_NPORT
4665 * bit reached it, the handle has now been released. We'll
4666 * get an error when we ACK this PLOGI. Nothing will be sent
4667 * back to initiator. Initiator should eventually retry
4668 * PLOGI and situation will correct itself.
4670 sess->keep_nport_handle = ((sess->loop_id == loop_id) &&
4671 (sess->d_id.b24 == port_id.b24));
4673 ql_dbg(ql_dbg_disc, vha, 0xffff,
4674 "%s %d %8phC post del sess\n",
4675 __func__, __LINE__, sess->port_name);
4678 qlt_schedule_sess_for_deletion_lock(sess);
4679 break;
4682 break;
4684 case ELS_PRLI:
4685 wd3_lo = le16_to_cpu(iocb->u.isp24.u.prli.wd3_lo);
4687 if (wwn) {
4688 spin_lock_irqsave(&tgt->ha->tgt.sess_lock, flags);
4689 sess = qlt_find_sess_invalidate_other(vha, wwn, port_id,
4690 loop_id, &conflict_sess);
4691 spin_unlock_irqrestore(&tgt->ha->tgt.sess_lock, flags);
4694 if (conflict_sess) {
4695 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf09b,
4696 "PRLI with conflicting sess %p port %8phC\n",
4697 conflict_sess, conflict_sess->port_name);
4698 qlt_send_term_imm_notif(vha, iocb, 1);
4699 res = 0;
4700 break;
4703 if (sess != NULL) {
4704 if (sess->fw_login_state != DSC_LS_PLOGI_PEND &&
4705 sess->fw_login_state != DSC_LS_PLOGI_COMP) {
4707 * Impatient initiator sent PRLI before last
4708 * PLOGI could finish. Will force him to re-try,
4709 * while last one finishes.
4711 ql_log(ql_log_warn, sess->vha, 0xf095,
4712 "sess %p PRLI received, before plogi ack.\n",
4713 sess);
4714 qlt_send_term_imm_notif(vha, iocb, 1);
4715 res = 0;
4716 break;
4720 * This shouldn't happen under normal circumstances,
4721 * since we have deleted the old session during PLOGI
4723 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf096,
4724 "PRLI (loop_id %#04x) for existing sess %p (loop_id %#04x)\n",
4725 sess->loop_id, sess, iocb->u.isp24.nport_handle);
4727 sess->local = 0;
4728 sess->loop_id = loop_id;
4729 sess->d_id = port_id;
4730 sess->fw_login_state = DSC_LS_PRLI_PEND;
4732 if (wd3_lo & BIT_7)
4733 sess->conf_compl_supported = 1;
4735 if ((wd3_lo & BIT_4) == 0)
4736 sess->port_type = FCT_INITIATOR;
4737 else
4738 sess->port_type = FCT_TARGET;
4740 res = 1; /* send notify ack */
4742 /* Make session global (not used in fabric mode) */
4743 if (ha->current_topology != ISP_CFG_F) {
4744 if (sess) {
4745 ql_dbg(ql_dbg_disc, vha, 0xffff,
4746 "%s %d %8phC post nack\n",
4747 __func__, __LINE__, sess->port_name);
4748 qla24xx_post_nack_work(vha, sess, iocb,
4749 SRB_NACK_PRLI);
4750 res = 0;
4751 } else {
4752 set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
4753 set_bit(LOCAL_LOOP_UPDATE, &vha->dpc_flags);
4754 qla2xxx_wake_dpc(vha);
4756 } else {
4757 if (sess) {
4758 ql_dbg(ql_dbg_disc, vha, 0xffff,
4759 "%s %d %8phC post nack\n",
4760 __func__, __LINE__, sess->port_name);
4761 qla24xx_post_nack_work(vha, sess, iocb,
4762 SRB_NACK_PRLI);
4763 res = 0;
4766 break;
4768 case ELS_TPRLO:
4769 if (le16_to_cpu(iocb->u.isp24.flags) &
4770 NOTIFY24XX_FLAGS_GLOBAL_TPRLO) {
4771 loop_id = 0xFFFF;
4772 qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS);
4773 res = 1;
4774 break;
4776 /* drop through */
4777 case ELS_LOGO:
4778 case ELS_PRLO:
4779 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
4780 sess = qla2x00_find_fcport_by_loopid(vha, loop_id);
4781 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
4783 if (sess) {
4784 sess->login_gen++;
4785 sess->fw_login_state = DSC_LS_LOGO_PEND;
4786 sess->logo_ack_needed = 1;
4787 memcpy(sess->iocb, iocb, IOCB_SIZE);
4790 res = qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS_SESS);
4792 ql_dbg(ql_dbg_disc, vha, 0xffff,
4793 "%s: logo %llx res %d sess %p ",
4794 __func__, wwn, res, sess);
4795 if (res == 0) {
4797 * cmd went upper layer, look for qlt_xmit_tm_rsp()
4798 * for LOGO_ACK & sess delete
4800 BUG_ON(!sess);
4801 res = 0;
4802 } else {
4803 /* cmd did not go to upper layer. */
4804 if (sess) {
4805 qlt_schedule_sess_for_deletion_lock(sess);
4806 res = 0;
4808 /* else logo will be ack */
4810 break;
4811 case ELS_PDISC:
4812 case ELS_ADISC:
4814 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
4815 if (tgt->link_reinit_iocb_pending) {
4816 qlt_send_notify_ack(vha, &tgt->link_reinit_iocb,
4817 0, 0, 0, 0, 0, 0);
4818 tgt->link_reinit_iocb_pending = 0;
4821 sess = qla2x00_find_fcport_by_wwpn(vha,
4822 iocb->u.isp24.port_name, 1);
4823 if (sess) {
4824 ql_dbg(ql_dbg_disc, vha, 0xffff,
4825 "sess %p lid %d|%d DS %d LS %d\n",
4826 sess, sess->loop_id, loop_id,
4827 sess->disc_state, sess->fw_login_state);
4830 res = 1; /* send notify ack */
4831 break;
4834 case ELS_FLOGI: /* should never happen */
4835 default:
4836 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf061,
4837 "qla_target(%d): Unsupported ELS command %x "
4838 "received\n", vha->vp_idx, iocb->u.isp24.status_subcode);
4839 res = qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS_SESS);
4840 break;
4843 return res;
4847 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
4849 static void qlt_handle_imm_notify(struct scsi_qla_host *vha,
4850 struct imm_ntfy_from_isp *iocb)
4852 struct qla_hw_data *ha = vha->hw;
4853 uint32_t add_flags = 0;
4854 int send_notify_ack = 1;
4855 uint16_t status;
4857 status = le16_to_cpu(iocb->u.isp2x.status);
4858 switch (status) {
4859 case IMM_NTFY_LIP_RESET:
4861 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf032,
4862 "qla_target(%d): LIP reset (loop %#x), subcode %x\n",
4863 vha->vp_idx, le16_to_cpu(iocb->u.isp24.nport_handle),
4864 iocb->u.isp24.status_subcode);
4866 if (qlt_reset(vha, iocb, QLA_TGT_ABORT_ALL) == 0)
4867 send_notify_ack = 0;
4868 break;
4871 case IMM_NTFY_LIP_LINK_REINIT:
4873 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
4874 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf033,
4875 "qla_target(%d): LINK REINIT (loop %#x, "
4876 "subcode %x)\n", vha->vp_idx,
4877 le16_to_cpu(iocb->u.isp24.nport_handle),
4878 iocb->u.isp24.status_subcode);
4879 if (tgt->link_reinit_iocb_pending) {
4880 qlt_send_notify_ack(vha, &tgt->link_reinit_iocb,
4881 0, 0, 0, 0, 0, 0);
4883 memcpy(&tgt->link_reinit_iocb, iocb, sizeof(*iocb));
4884 tgt->link_reinit_iocb_pending = 1;
4886 * QLogic requires to wait after LINK REINIT for possible
4887 * PDISC or ADISC ELS commands
4889 send_notify_ack = 0;
4890 break;
4893 case IMM_NTFY_PORT_LOGOUT:
4894 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf034,
4895 "qla_target(%d): Port logout (loop "
4896 "%#x, subcode %x)\n", vha->vp_idx,
4897 le16_to_cpu(iocb->u.isp24.nport_handle),
4898 iocb->u.isp24.status_subcode);
4900 if (qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS_SESS) == 0)
4901 send_notify_ack = 0;
4902 /* The sessions will be cleared in the callback, if needed */
4903 break;
4905 case IMM_NTFY_GLBL_TPRLO:
4906 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf035,
4907 "qla_target(%d): Global TPRLO (%x)\n", vha->vp_idx, status);
4908 if (qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS) == 0)
4909 send_notify_ack = 0;
4910 /* The sessions will be cleared in the callback, if needed */
4911 break;
4913 case IMM_NTFY_PORT_CONFIG:
4914 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf036,
4915 "qla_target(%d): Port config changed (%x)\n", vha->vp_idx,
4916 status);
4917 if (qlt_reset(vha, iocb, QLA_TGT_ABORT_ALL) == 0)
4918 send_notify_ack = 0;
4919 /* The sessions will be cleared in the callback, if needed */
4920 break;
4922 case IMM_NTFY_GLBL_LOGO:
4923 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06a,
4924 "qla_target(%d): Link failure detected\n",
4925 vha->vp_idx);
4926 /* I_T nexus loss */
4927 if (qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS) == 0)
4928 send_notify_ack = 0;
4929 break;
4931 case IMM_NTFY_IOCB_OVERFLOW:
4932 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06b,
4933 "qla_target(%d): Cannot provide requested "
4934 "capability (IOCB overflowed the immediate notify "
4935 "resource count)\n", vha->vp_idx);
4936 break;
4938 case IMM_NTFY_ABORT_TASK:
4939 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf037,
4940 "qla_target(%d): Abort Task (S %08x I %#x -> "
4941 "L %#x)\n", vha->vp_idx,
4942 le16_to_cpu(iocb->u.isp2x.seq_id),
4943 GET_TARGET_ID(ha, (struct atio_from_isp *)iocb),
4944 le16_to_cpu(iocb->u.isp2x.lun));
4945 if (qlt_abort_task(vha, iocb) == 0)
4946 send_notify_ack = 0;
4947 break;
4949 case IMM_NTFY_RESOURCE:
4950 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06c,
4951 "qla_target(%d): Out of resources, host %ld\n",
4952 vha->vp_idx, vha->host_no);
4953 break;
4955 case IMM_NTFY_MSG_RX:
4956 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf038,
4957 "qla_target(%d): Immediate notify task %x\n",
4958 vha->vp_idx, iocb->u.isp2x.task_flags);
4959 if (qlt_handle_task_mgmt(vha, iocb) == 0)
4960 send_notify_ack = 0;
4961 break;
4963 case IMM_NTFY_ELS:
4964 if (qlt_24xx_handle_els(vha, iocb) == 0)
4965 send_notify_ack = 0;
4966 break;
4967 default:
4968 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06d,
4969 "qla_target(%d): Received unknown immediate "
4970 "notify status %x\n", vha->vp_idx, status);
4971 break;
4974 if (send_notify_ack)
4975 qlt_send_notify_ack(vha, iocb, add_flags, 0, 0, 0, 0, 0);
4979 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
4980 * This function sends busy to ISP 2xxx or 24xx.
4982 static int __qlt_send_busy(struct scsi_qla_host *vha,
4983 struct atio_from_isp *atio, uint16_t status)
4985 struct ctio7_to_24xx *ctio24;
4986 struct qla_hw_data *ha = vha->hw;
4987 request_t *pkt;
4988 struct fc_port *sess = NULL;
4989 unsigned long flags;
4991 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
4992 sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha,
4993 atio->u.isp24.fcp_hdr.s_id);
4994 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
4995 if (!sess) {
4996 qlt_send_term_exchange(vha, NULL, atio, 1, 0);
4997 return 0;
4999 /* Sending marker isn't necessary, since we called from ISR */
5001 pkt = (request_t *)qla2x00_alloc_iocbs(vha, NULL);
5002 if (!pkt) {
5003 ql_dbg(ql_dbg_io, vha, 0x3063,
5004 "qla_target(%d): %s failed: unable to allocate "
5005 "request packet", vha->vp_idx, __func__);
5006 return -ENOMEM;
5009 vha->tgt_counters.num_q_full_sent++;
5010 pkt->entry_count = 1;
5011 pkt->handle = QLA_TGT_SKIP_HANDLE | CTIO_COMPLETION_HANDLE_MARK;
5013 ctio24 = (struct ctio7_to_24xx *)pkt;
5014 ctio24->entry_type = CTIO_TYPE7;
5015 ctio24->nport_handle = sess->loop_id;
5016 ctio24->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
5017 ctio24->vp_index = vha->vp_idx;
5018 ctio24->initiator_id[0] = atio->u.isp24.fcp_hdr.s_id[2];
5019 ctio24->initiator_id[1] = atio->u.isp24.fcp_hdr.s_id[1];
5020 ctio24->initiator_id[2] = atio->u.isp24.fcp_hdr.s_id[0];
5021 ctio24->exchange_addr = atio->u.isp24.exchange_addr;
5022 ctio24->u.status1.flags = (atio->u.isp24.attr << 9) |
5023 cpu_to_le16(
5024 CTIO7_FLAGS_STATUS_MODE_1 | CTIO7_FLAGS_SEND_STATUS |
5025 CTIO7_FLAGS_DONT_RET_CTIO);
5027 * CTIO from fw w/o se_cmd doesn't provide enough info to retry it,
5028 * if the explicit conformation is used.
5030 ctio24->u.status1.ox_id = swab16(atio->u.isp24.fcp_hdr.ox_id);
5031 ctio24->u.status1.scsi_status = cpu_to_le16(status);
5032 /* Memory Barrier */
5033 wmb();
5034 qla2x00_start_iocbs(vha, vha->req);
5035 return 0;
5039 * This routine is used to allocate a command for either a QFull condition
5040 * (ie reply SAM_STAT_BUSY) or to terminate an exchange that did not go
5041 * out previously.
5043 static void
5044 qlt_alloc_qfull_cmd(struct scsi_qla_host *vha,
5045 struct atio_from_isp *atio, uint16_t status, int qfull)
5047 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
5048 struct qla_hw_data *ha = vha->hw;
5049 struct fc_port *sess;
5050 struct se_session *se_sess;
5051 struct qla_tgt_cmd *cmd;
5052 int tag;
5054 if (unlikely(tgt->tgt_stop)) {
5055 ql_dbg(ql_dbg_io, vha, 0x300a,
5056 "New command while device %p is shutting down\n", tgt);
5057 return;
5060 if ((vha->hw->tgt.num_qfull_cmds_alloc + 1) > MAX_QFULL_CMDS_ALLOC) {
5061 vha->hw->tgt.num_qfull_cmds_dropped++;
5062 if (vha->hw->tgt.num_qfull_cmds_dropped >
5063 vha->qla_stats.stat_max_qfull_cmds_dropped)
5064 vha->qla_stats.stat_max_qfull_cmds_dropped =
5065 vha->hw->tgt.num_qfull_cmds_dropped;
5067 ql_dbg(ql_dbg_io, vha, 0x3068,
5068 "qla_target(%d): %s: QFull CMD dropped[%d]\n",
5069 vha->vp_idx, __func__,
5070 vha->hw->tgt.num_qfull_cmds_dropped);
5072 qlt_chk_exch_leak_thresh_hold(vha);
5073 return;
5076 sess = ha->tgt.tgt_ops->find_sess_by_s_id
5077 (vha, atio->u.isp24.fcp_hdr.s_id);
5078 if (!sess)
5079 return;
5081 se_sess = sess->se_sess;
5083 tag = percpu_ida_alloc(&se_sess->sess_tag_pool, TASK_RUNNING);
5084 if (tag < 0)
5085 return;
5087 cmd = &((struct qla_tgt_cmd *)se_sess->sess_cmd_map)[tag];
5088 if (!cmd) {
5089 ql_dbg(ql_dbg_io, vha, 0x3009,
5090 "qla_target(%d): %s: Allocation of cmd failed\n",
5091 vha->vp_idx, __func__);
5093 vha->hw->tgt.num_qfull_cmds_dropped++;
5094 if (vha->hw->tgt.num_qfull_cmds_dropped >
5095 vha->qla_stats.stat_max_qfull_cmds_dropped)
5096 vha->qla_stats.stat_max_qfull_cmds_dropped =
5097 vha->hw->tgt.num_qfull_cmds_dropped;
5099 qlt_chk_exch_leak_thresh_hold(vha);
5100 return;
5103 memset(cmd, 0, sizeof(struct qla_tgt_cmd));
5105 qlt_incr_num_pend_cmds(vha);
5106 INIT_LIST_HEAD(&cmd->cmd_list);
5107 memcpy(&cmd->atio, atio, sizeof(*atio));
5109 cmd->tgt = vha->vha_tgt.qla_tgt;
5110 cmd->vha = vha;
5111 cmd->reset_count = vha->hw->chip_reset;
5112 cmd->q_full = 1;
5114 if (qfull) {
5115 cmd->q_full = 1;
5116 /* NOTE: borrowing the state field to carry the status */
5117 cmd->state = status;
5118 } else
5119 cmd->term_exchg = 1;
5121 list_add_tail(&cmd->cmd_list, &vha->hw->tgt.q_full_list);
5123 vha->hw->tgt.num_qfull_cmds_alloc++;
5124 if (vha->hw->tgt.num_qfull_cmds_alloc >
5125 vha->qla_stats.stat_max_qfull_cmds_alloc)
5126 vha->qla_stats.stat_max_qfull_cmds_alloc =
5127 vha->hw->tgt.num_qfull_cmds_alloc;
5131 qlt_free_qfull_cmds(struct scsi_qla_host *vha)
5133 struct qla_hw_data *ha = vha->hw;
5134 unsigned long flags;
5135 struct qla_tgt_cmd *cmd, *tcmd;
5136 struct list_head free_list;
5137 int rc = 0;
5139 if (list_empty(&ha->tgt.q_full_list))
5140 return 0;
5142 INIT_LIST_HEAD(&free_list);
5144 spin_lock_irqsave(&vha->hw->hardware_lock, flags);
5146 if (list_empty(&ha->tgt.q_full_list)) {
5147 spin_unlock_irqrestore(&vha->hw->hardware_lock, flags);
5148 return 0;
5151 list_for_each_entry_safe(cmd, tcmd, &ha->tgt.q_full_list, cmd_list) {
5152 if (cmd->q_full)
5153 /* cmd->state is a borrowed field to hold status */
5154 rc = __qlt_send_busy(vha, &cmd->atio, cmd->state);
5155 else if (cmd->term_exchg)
5156 rc = __qlt_send_term_exchange(vha, NULL, &cmd->atio);
5158 if (rc == -ENOMEM)
5159 break;
5161 if (cmd->q_full)
5162 ql_dbg(ql_dbg_io, vha, 0x3006,
5163 "%s: busy sent for ox_id[%04x]\n", __func__,
5164 be16_to_cpu(cmd->atio.u.isp24.fcp_hdr.ox_id));
5165 else if (cmd->term_exchg)
5166 ql_dbg(ql_dbg_io, vha, 0x3007,
5167 "%s: Term exchg sent for ox_id[%04x]\n", __func__,
5168 be16_to_cpu(cmd->atio.u.isp24.fcp_hdr.ox_id));
5169 else
5170 ql_dbg(ql_dbg_io, vha, 0x3008,
5171 "%s: Unexpected cmd in QFull list %p\n", __func__,
5172 cmd);
5174 list_del(&cmd->cmd_list);
5175 list_add_tail(&cmd->cmd_list, &free_list);
5177 /* piggy back on hardware_lock for protection */
5178 vha->hw->tgt.num_qfull_cmds_alloc--;
5180 spin_unlock_irqrestore(&vha->hw->hardware_lock, flags);
5182 cmd = NULL;
5184 list_for_each_entry_safe(cmd, tcmd, &free_list, cmd_list) {
5185 list_del(&cmd->cmd_list);
5186 /* This cmd was never sent to TCM. There is no need
5187 * to schedule free or call free_cmd
5189 qlt_free_cmd(cmd);
5191 return rc;
5194 static void
5195 qlt_send_busy(struct scsi_qla_host *vha,
5196 struct atio_from_isp *atio, uint16_t status)
5198 int rc = 0;
5200 rc = __qlt_send_busy(vha, atio, status);
5201 if (rc == -ENOMEM)
5202 qlt_alloc_qfull_cmd(vha, atio, status, 1);
5205 static int
5206 qlt_chk_qfull_thresh_hold(struct scsi_qla_host *vha,
5207 struct atio_from_isp *atio, bool ha_locked)
5209 struct qla_hw_data *ha = vha->hw;
5210 uint16_t status;
5211 unsigned long flags;
5213 if (ha->tgt.num_pend_cmds < Q_FULL_THRESH_HOLD(ha))
5214 return 0;
5216 if (!ha_locked)
5217 spin_lock_irqsave(&ha->hardware_lock, flags);
5218 status = temp_sam_status;
5219 qlt_send_busy(vha, atio, status);
5220 if (!ha_locked)
5221 spin_unlock_irqrestore(&ha->hardware_lock, flags);
5223 return 1;
5226 /* ha->hardware_lock supposed to be held on entry */
5227 /* called via callback from qla2xxx */
5228 static void qlt_24xx_atio_pkt(struct scsi_qla_host *vha,
5229 struct atio_from_isp *atio, uint8_t ha_locked)
5231 struct qla_hw_data *ha = vha->hw;
5232 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
5233 int rc;
5234 unsigned long flags;
5236 if (unlikely(tgt == NULL)) {
5237 ql_dbg(ql_dbg_tgt, vha, 0x3064,
5238 "ATIO pkt, but no tgt (ha %p)", ha);
5239 return;
5242 * In tgt_stop mode we also should allow all requests to pass.
5243 * Otherwise, some commands can stuck.
5246 tgt->atio_irq_cmd_count++;
5248 switch (atio->u.raw.entry_type) {
5249 case ATIO_TYPE7:
5250 if (unlikely(atio->u.isp24.exchange_addr ==
5251 ATIO_EXCHANGE_ADDRESS_UNKNOWN)) {
5252 ql_dbg(ql_dbg_io, vha, 0x3065,
5253 "qla_target(%d): ATIO_TYPE7 "
5254 "received with UNKNOWN exchange address, "
5255 "sending QUEUE_FULL\n", vha->vp_idx);
5256 if (!ha_locked)
5257 spin_lock_irqsave(&ha->hardware_lock, flags);
5258 qlt_send_busy(vha, atio, SAM_STAT_TASK_SET_FULL);
5259 if (!ha_locked)
5260 spin_unlock_irqrestore(&ha->hardware_lock, flags);
5261 break;
5266 if (likely(atio->u.isp24.fcp_cmnd.task_mgmt_flags == 0)) {
5267 rc = qlt_chk_qfull_thresh_hold(vha, atio, ha_locked);
5268 if (rc != 0) {
5269 tgt->atio_irq_cmd_count--;
5270 return;
5272 rc = qlt_handle_cmd_for_atio(vha, atio);
5273 } else {
5274 rc = qlt_handle_task_mgmt(vha, atio);
5276 if (unlikely(rc != 0)) {
5277 if (rc == -ESRCH) {
5278 if (!ha_locked)
5279 spin_lock_irqsave
5280 (&ha->hardware_lock, flags);
5282 #if 1 /* With TERM EXCHANGE some FC cards refuse to boot */
5283 qlt_send_busy(vha, atio, SAM_STAT_BUSY);
5284 #else
5285 qlt_send_term_exchange(vha, NULL, atio, 1, 0);
5286 #endif
5288 if (!ha_locked)
5289 spin_unlock_irqrestore
5290 (&ha->hardware_lock, flags);
5292 } else {
5293 if (tgt->tgt_stop) {
5294 ql_dbg(ql_dbg_tgt, vha, 0xe059,
5295 "qla_target: Unable to send "
5296 "command to target for req, "
5297 "ignoring.\n");
5298 } else {
5299 ql_dbg(ql_dbg_tgt, vha, 0xe05a,
5300 "qla_target(%d): Unable to send "
5301 "command to target, sending BUSY "
5302 "status.\n", vha->vp_idx);
5303 if (!ha_locked)
5304 spin_lock_irqsave(
5305 &ha->hardware_lock, flags);
5306 qlt_send_busy(vha, atio, SAM_STAT_BUSY);
5307 if (!ha_locked)
5308 spin_unlock_irqrestore(
5309 &ha->hardware_lock, flags);
5313 break;
5315 case IMMED_NOTIFY_TYPE:
5317 if (unlikely(atio->u.isp2x.entry_status != 0)) {
5318 ql_dbg(ql_dbg_tgt, vha, 0xe05b,
5319 "qla_target(%d): Received ATIO packet %x "
5320 "with error status %x\n", vha->vp_idx,
5321 atio->u.raw.entry_type,
5322 atio->u.isp2x.entry_status);
5323 break;
5325 ql_dbg(ql_dbg_tgt, vha, 0xe02e, "%s", "IMMED_NOTIFY ATIO");
5327 if (!ha_locked)
5328 spin_lock_irqsave(&ha->hardware_lock, flags);
5329 qlt_handle_imm_notify(vha, (struct imm_ntfy_from_isp *)atio);
5330 if (!ha_locked)
5331 spin_unlock_irqrestore(&ha->hardware_lock, flags);
5332 break;
5335 default:
5336 ql_dbg(ql_dbg_tgt, vha, 0xe05c,
5337 "qla_target(%d): Received unknown ATIO atio "
5338 "type %x\n", vha->vp_idx, atio->u.raw.entry_type);
5339 break;
5342 tgt->atio_irq_cmd_count--;
5345 /* ha->hardware_lock supposed to be held on entry */
5346 /* called via callback from qla2xxx */
5347 static void qlt_response_pkt(struct scsi_qla_host *vha, response_t *pkt)
5349 struct qla_hw_data *ha = vha->hw;
5350 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
5352 if (unlikely(tgt == NULL)) {
5353 ql_dbg(ql_dbg_tgt, vha, 0xe05d,
5354 "qla_target(%d): Response pkt %x received, but no "
5355 "tgt (ha %p)\n", vha->vp_idx, pkt->entry_type, ha);
5356 return;
5360 * In tgt_stop mode we also should allow all requests to pass.
5361 * Otherwise, some commands can stuck.
5364 tgt->irq_cmd_count++;
5366 switch (pkt->entry_type) {
5367 case CTIO_CRC2:
5368 case CTIO_TYPE7:
5370 struct ctio7_from_24xx *entry = (struct ctio7_from_24xx *)pkt;
5371 qlt_do_ctio_completion(vha, entry->handle,
5372 le16_to_cpu(entry->status)|(pkt->entry_status << 16),
5373 entry);
5374 break;
5377 case ACCEPT_TGT_IO_TYPE:
5379 struct atio_from_isp *atio = (struct atio_from_isp *)pkt;
5380 int rc;
5381 if (atio->u.isp2x.status !=
5382 cpu_to_le16(ATIO_CDB_VALID)) {
5383 ql_dbg(ql_dbg_tgt, vha, 0xe05e,
5384 "qla_target(%d): ATIO with error "
5385 "status %x received\n", vha->vp_idx,
5386 le16_to_cpu(atio->u.isp2x.status));
5387 break;
5390 rc = qlt_chk_qfull_thresh_hold(vha, atio, true);
5391 if (rc != 0) {
5392 tgt->irq_cmd_count--;
5393 return;
5396 rc = qlt_handle_cmd_for_atio(vha, atio);
5397 if (unlikely(rc != 0)) {
5398 if (rc == -ESRCH) {
5399 #if 1 /* With TERM EXCHANGE some FC cards refuse to boot */
5400 qlt_send_busy(vha, atio, 0);
5401 #else
5402 qlt_send_term_exchange(vha, NULL, atio, 1, 0);
5403 #endif
5404 } else {
5405 if (tgt->tgt_stop) {
5406 ql_dbg(ql_dbg_tgt, vha, 0xe05f,
5407 "qla_target: Unable to send "
5408 "command to target, sending TERM "
5409 "EXCHANGE for rsp\n");
5410 qlt_send_term_exchange(vha, NULL,
5411 atio, 1, 0);
5412 } else {
5413 ql_dbg(ql_dbg_tgt, vha, 0xe060,
5414 "qla_target(%d): Unable to send "
5415 "command to target, sending BUSY "
5416 "status\n", vha->vp_idx);
5417 qlt_send_busy(vha, atio, 0);
5422 break;
5424 case CONTINUE_TGT_IO_TYPE:
5426 struct ctio_to_2xxx *entry = (struct ctio_to_2xxx *)pkt;
5427 qlt_do_ctio_completion(vha, entry->handle,
5428 le16_to_cpu(entry->status)|(pkt->entry_status << 16),
5429 entry);
5430 break;
5433 case CTIO_A64_TYPE:
5435 struct ctio_to_2xxx *entry = (struct ctio_to_2xxx *)pkt;
5436 qlt_do_ctio_completion(vha, entry->handle,
5437 le16_to_cpu(entry->status)|(pkt->entry_status << 16),
5438 entry);
5439 break;
5442 case IMMED_NOTIFY_TYPE:
5443 ql_dbg(ql_dbg_tgt, vha, 0xe035, "%s", "IMMED_NOTIFY\n");
5444 qlt_handle_imm_notify(vha, (struct imm_ntfy_from_isp *)pkt);
5445 break;
5447 case NOTIFY_ACK_TYPE:
5448 if (tgt->notify_ack_expected > 0) {
5449 struct nack_to_isp *entry = (struct nack_to_isp *)pkt;
5450 ql_dbg(ql_dbg_tgt, vha, 0xe036,
5451 "NOTIFY_ACK seq %08x status %x\n",
5452 le16_to_cpu(entry->u.isp2x.seq_id),
5453 le16_to_cpu(entry->u.isp2x.status));
5454 tgt->notify_ack_expected--;
5455 if (entry->u.isp2x.status !=
5456 cpu_to_le16(NOTIFY_ACK_SUCCESS)) {
5457 ql_dbg(ql_dbg_tgt, vha, 0xe061,
5458 "qla_target(%d): NOTIFY_ACK "
5459 "failed %x\n", vha->vp_idx,
5460 le16_to_cpu(entry->u.isp2x.status));
5462 } else {
5463 ql_dbg(ql_dbg_tgt, vha, 0xe062,
5464 "qla_target(%d): Unexpected NOTIFY_ACK received\n",
5465 vha->vp_idx);
5467 break;
5469 case ABTS_RECV_24XX:
5470 ql_dbg(ql_dbg_tgt, vha, 0xe037,
5471 "ABTS_RECV_24XX: instance %d\n", vha->vp_idx);
5472 qlt_24xx_handle_abts(vha, (struct abts_recv_from_24xx *)pkt);
5473 break;
5475 case ABTS_RESP_24XX:
5476 if (tgt->abts_resp_expected > 0) {
5477 struct abts_resp_from_24xx_fw *entry =
5478 (struct abts_resp_from_24xx_fw *)pkt;
5479 ql_dbg(ql_dbg_tgt, vha, 0xe038,
5480 "ABTS_RESP_24XX: compl_status %x\n",
5481 entry->compl_status);
5482 tgt->abts_resp_expected--;
5483 if (le16_to_cpu(entry->compl_status) !=
5484 ABTS_RESP_COMPL_SUCCESS) {
5485 if ((entry->error_subcode1 == 0x1E) &&
5486 (entry->error_subcode2 == 0)) {
5488 * We've got a race here: aborted
5489 * exchange not terminated, i.e.
5490 * response for the aborted command was
5491 * sent between the abort request was
5492 * received and processed.
5493 * Unfortunately, the firmware has a
5494 * silly requirement that all aborted
5495 * exchanges must be explicitely
5496 * terminated, otherwise it refuses to
5497 * send responses for the abort
5498 * requests. So, we have to
5499 * (re)terminate the exchange and retry
5500 * the abort response.
5502 qlt_24xx_retry_term_exchange(vha,
5503 entry);
5504 } else
5505 ql_dbg(ql_dbg_tgt, vha, 0xe063,
5506 "qla_target(%d): ABTS_RESP_24XX "
5507 "failed %x (subcode %x:%x)",
5508 vha->vp_idx, entry->compl_status,
5509 entry->error_subcode1,
5510 entry->error_subcode2);
5512 } else {
5513 ql_dbg(ql_dbg_tgt, vha, 0xe064,
5514 "qla_target(%d): Unexpected ABTS_RESP_24XX "
5515 "received\n", vha->vp_idx);
5517 break;
5519 default:
5520 ql_dbg(ql_dbg_tgt, vha, 0xe065,
5521 "qla_target(%d): Received unknown response pkt "
5522 "type %x\n", vha->vp_idx, pkt->entry_type);
5523 break;
5526 tgt->irq_cmd_count--;
5530 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
5532 void qlt_async_event(uint16_t code, struct scsi_qla_host *vha,
5533 uint16_t *mailbox)
5535 struct qla_hw_data *ha = vha->hw;
5536 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
5537 int login_code;
5539 if (!ha->tgt.tgt_ops)
5540 return;
5542 if (unlikely(tgt == NULL)) {
5543 ql_dbg(ql_dbg_tgt, vha, 0xe03a,
5544 "ASYNC EVENT %#x, but no tgt (ha %p)\n", code, ha);
5545 return;
5548 if (((code == MBA_POINT_TO_POINT) || (code == MBA_CHG_IN_CONNECTION)) &&
5549 IS_QLA2100(ha))
5550 return;
5552 * In tgt_stop mode we also should allow all requests to pass.
5553 * Otherwise, some commands can stuck.
5556 tgt->irq_cmd_count++;
5558 switch (code) {
5559 case MBA_RESET: /* Reset */
5560 case MBA_SYSTEM_ERR: /* System Error */
5561 case MBA_REQ_TRANSFER_ERR: /* Request Transfer Error */
5562 case MBA_RSP_TRANSFER_ERR: /* Response Transfer Error */
5563 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03a,
5564 "qla_target(%d): System error async event %#x "
5565 "occurred", vha->vp_idx, code);
5566 break;
5567 case MBA_WAKEUP_THRES: /* Request Queue Wake-up. */
5568 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
5569 break;
5571 case MBA_LOOP_UP:
5573 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03b,
5574 "qla_target(%d): Async LOOP_UP occurred "
5575 "(m[0]=%x, m[1]=%x, m[2]=%x, m[3]=%x)", vha->vp_idx,
5576 le16_to_cpu(mailbox[0]), le16_to_cpu(mailbox[1]),
5577 le16_to_cpu(mailbox[2]), le16_to_cpu(mailbox[3]));
5578 if (tgt->link_reinit_iocb_pending) {
5579 qlt_send_notify_ack(vha, (void *)&tgt->link_reinit_iocb,
5580 0, 0, 0, 0, 0, 0);
5581 tgt->link_reinit_iocb_pending = 0;
5583 break;
5586 case MBA_LIP_OCCURRED:
5587 case MBA_LOOP_DOWN:
5588 case MBA_LIP_RESET:
5589 case MBA_RSCN_UPDATE:
5590 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03c,
5591 "qla_target(%d): Async event %#x occurred "
5592 "(m[0]=%x, m[1]=%x, m[2]=%x, m[3]=%x)", vha->vp_idx, code,
5593 le16_to_cpu(mailbox[0]), le16_to_cpu(mailbox[1]),
5594 le16_to_cpu(mailbox[2]), le16_to_cpu(mailbox[3]));
5595 break;
5597 case MBA_REJECTED_FCP_CMD:
5598 ql_dbg(ql_dbg_tgt_mgt, vha, 0xffff,
5599 "qla_target(%d): Async event LS_REJECT occurred "
5600 "(m[0]=%x, m[1]=%x, m[2]=%x, m[3]=%x)", vha->vp_idx,
5601 le16_to_cpu(mailbox[0]), le16_to_cpu(mailbox[1]),
5602 le16_to_cpu(mailbox[2]), le16_to_cpu(mailbox[3]));
5604 if (le16_to_cpu(mailbox[3]) == 1) {
5605 /* exchange starvation. */
5606 vha->hw->exch_starvation++;
5607 if (vha->hw->exch_starvation > 5) {
5608 ql_log(ql_log_warn, vha, 0xffff,
5609 "Exchange starvation-. Resetting RISC\n");
5611 vha->hw->exch_starvation = 0;
5612 if (IS_P3P_TYPE(vha->hw))
5613 set_bit(FCOE_CTX_RESET_NEEDED,
5614 &vha->dpc_flags);
5615 else
5616 set_bit(ISP_ABORT_NEEDED,
5617 &vha->dpc_flags);
5618 qla2xxx_wake_dpc(vha);
5621 break;
5623 case MBA_PORT_UPDATE:
5624 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03d,
5625 "qla_target(%d): Port update async event %#x "
5626 "occurred: updating the ports database (m[0]=%x, m[1]=%x, "
5627 "m[2]=%x, m[3]=%x)", vha->vp_idx, code,
5628 le16_to_cpu(mailbox[0]), le16_to_cpu(mailbox[1]),
5629 le16_to_cpu(mailbox[2]), le16_to_cpu(mailbox[3]));
5631 login_code = le16_to_cpu(mailbox[2]);
5632 if (login_code == 0x4) {
5633 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03e,
5634 "Async MB 2: Got PLOGI Complete\n");
5635 vha->hw->exch_starvation = 0;
5636 } else if (login_code == 0x7)
5637 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03f,
5638 "Async MB 2: Port Logged Out\n");
5639 break;
5640 default:
5641 break;
5644 tgt->irq_cmd_count--;
5647 static fc_port_t *qlt_get_port_database(struct scsi_qla_host *vha,
5648 uint16_t loop_id)
5650 fc_port_t *fcport, *tfcp, *del;
5651 int rc;
5652 unsigned long flags;
5653 u8 newfcport = 0;
5655 fcport = kzalloc(sizeof(*fcport), GFP_KERNEL);
5656 if (!fcport) {
5657 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06f,
5658 "qla_target(%d): Allocation of tmp FC port failed",
5659 vha->vp_idx);
5660 return NULL;
5663 fcport->loop_id = loop_id;
5665 rc = qla24xx_gpdb_wait(vha, fcport, 0);
5666 if (rc != QLA_SUCCESS) {
5667 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf070,
5668 "qla_target(%d): Failed to retrieve fcport "
5669 "information -- get_port_database() returned %x "
5670 "(loop_id=0x%04x)", vha->vp_idx, rc, loop_id);
5671 kfree(fcport);
5672 return NULL;
5675 del = NULL;
5676 spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
5677 tfcp = qla2x00_find_fcport_by_wwpn(vha, fcport->port_name, 1);
5679 if (tfcp) {
5680 tfcp->d_id = fcport->d_id;
5681 tfcp->port_type = fcport->port_type;
5682 tfcp->supported_classes = fcport->supported_classes;
5683 tfcp->flags |= fcport->flags;
5685 del = fcport;
5686 fcport = tfcp;
5687 } else {
5688 if (vha->hw->current_topology == ISP_CFG_F)
5689 fcport->flags |= FCF_FABRIC_DEVICE;
5691 list_add_tail(&fcport->list, &vha->vp_fcports);
5692 if (!IS_SW_RESV_ADDR(fcport->d_id))
5693 vha->fcport_count++;
5694 fcport->login_gen++;
5695 fcport->disc_state = DSC_LOGIN_COMPLETE;
5696 fcport->login_succ = 1;
5697 newfcport = 1;
5700 fcport->deleted = 0;
5701 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
5703 switch (vha->host->active_mode) {
5704 case MODE_INITIATOR:
5705 case MODE_DUAL:
5706 if (newfcport) {
5707 if (!IS_IIDMA_CAPABLE(vha->hw) || !vha->hw->flags.gpsc_supported) {
5708 ql_dbg(ql_dbg_disc, vha, 0xffff,
5709 "%s %d %8phC post upd_fcport fcp_cnt %d\n",
5710 __func__, __LINE__, fcport->port_name, vha->fcport_count);
5711 qla24xx_post_upd_fcport_work(vha, fcport);
5712 } else {
5713 ql_dbg(ql_dbg_disc, vha, 0xffff,
5714 "%s %d %8phC post gpsc fcp_cnt %d\n",
5715 __func__, __LINE__, fcport->port_name, vha->fcport_count);
5716 qla24xx_post_gpsc_work(vha, fcport);
5719 break;
5721 case MODE_TARGET:
5722 default:
5723 break;
5725 if (del)
5726 qla2x00_free_fcport(del);
5728 return fcport;
5731 /* Must be called under tgt_mutex */
5732 static struct fc_port *qlt_make_local_sess(struct scsi_qla_host *vha,
5733 uint8_t *s_id)
5735 struct fc_port *sess = NULL;
5736 fc_port_t *fcport = NULL;
5737 int rc, global_resets;
5738 uint16_t loop_id = 0;
5740 if ((s_id[0] == 0xFF) && (s_id[1] == 0xFC)) {
5742 * This is Domain Controller, so it should be
5743 * OK to drop SCSI commands from it.
5745 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf042,
5746 "Unable to find initiator with S_ID %x:%x:%x",
5747 s_id[0], s_id[1], s_id[2]);
5748 return NULL;
5751 mutex_lock(&vha->vha_tgt.tgt_mutex);
5753 retry:
5754 global_resets =
5755 atomic_read(&vha->vha_tgt.qla_tgt->tgt_global_resets_count);
5757 rc = qla24xx_get_loop_id(vha, s_id, &loop_id);
5758 if (rc != 0) {
5759 mutex_unlock(&vha->vha_tgt.tgt_mutex);
5761 ql_log(ql_log_info, vha, 0xf071,
5762 "qla_target(%d): Unable to find "
5763 "initiator with S_ID %x:%x:%x",
5764 vha->vp_idx, s_id[0], s_id[1],
5765 s_id[2]);
5767 if (rc == -ENOENT) {
5768 qlt_port_logo_t logo;
5769 sid_to_portid(s_id, &logo.id);
5770 logo.cmd_count = 1;
5771 qlt_send_first_logo(vha, &logo);
5774 return NULL;
5777 fcport = qlt_get_port_database(vha, loop_id);
5778 if (!fcport) {
5779 mutex_unlock(&vha->vha_tgt.tgt_mutex);
5780 return NULL;
5783 if (global_resets !=
5784 atomic_read(&vha->vha_tgt.qla_tgt->tgt_global_resets_count)) {
5785 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf043,
5786 "qla_target(%d): global reset during session discovery "
5787 "(counter was %d, new %d), retrying", vha->vp_idx,
5788 global_resets,
5789 atomic_read(&vha->vha_tgt.
5790 qla_tgt->tgt_global_resets_count));
5791 goto retry;
5794 sess = qlt_create_sess(vha, fcport, true);
5796 mutex_unlock(&vha->vha_tgt.tgt_mutex);
5798 return sess;
5801 static void qlt_abort_work(struct qla_tgt *tgt,
5802 struct qla_tgt_sess_work_param *prm)
5804 struct scsi_qla_host *vha = tgt->vha;
5805 struct qla_hw_data *ha = vha->hw;
5806 struct fc_port *sess = NULL;
5807 unsigned long flags = 0, flags2 = 0;
5808 uint32_t be_s_id;
5809 uint8_t s_id[3];
5810 int rc;
5812 spin_lock_irqsave(&ha->tgt.sess_lock, flags2);
5814 if (tgt->tgt_stop)
5815 goto out_term2;
5817 s_id[0] = prm->abts.fcp_hdr_le.s_id[2];
5818 s_id[1] = prm->abts.fcp_hdr_le.s_id[1];
5819 s_id[2] = prm->abts.fcp_hdr_le.s_id[0];
5821 sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha,
5822 (unsigned char *)&be_s_id);
5823 if (!sess) {
5824 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags2);
5826 sess = qlt_make_local_sess(vha, s_id);
5827 /* sess has got an extra creation ref */
5829 spin_lock_irqsave(&ha->tgt.sess_lock, flags2);
5830 if (!sess)
5831 goto out_term2;
5832 } else {
5833 if (sess->deleted) {
5834 sess = NULL;
5835 goto out_term2;
5838 if (!kref_get_unless_zero(&sess->sess_kref)) {
5839 ql_dbg(ql_dbg_tgt_tmr, vha, 0xffff,
5840 "%s: kref_get fail %8phC \n",
5841 __func__, sess->port_name);
5842 sess = NULL;
5843 goto out_term2;
5847 rc = __qlt_24xx_handle_abts(vha, &prm->abts, sess);
5848 ha->tgt.tgt_ops->put_sess(sess);
5849 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags2);
5851 if (rc != 0)
5852 goto out_term;
5853 return;
5855 out_term2:
5856 if (sess)
5857 ha->tgt.tgt_ops->put_sess(sess);
5858 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags2);
5860 out_term:
5861 spin_lock_irqsave(&ha->hardware_lock, flags);
5862 qlt_24xx_send_abts_resp(vha, &prm->abts, FCP_TMF_REJECTED, false);
5863 spin_unlock_irqrestore(&ha->hardware_lock, flags);
5866 static void qlt_tmr_work(struct qla_tgt *tgt,
5867 struct qla_tgt_sess_work_param *prm)
5869 struct atio_from_isp *a = &prm->tm_iocb2;
5870 struct scsi_qla_host *vha = tgt->vha;
5871 struct qla_hw_data *ha = vha->hw;
5872 struct fc_port *sess = NULL;
5873 unsigned long flags;
5874 uint8_t *s_id = NULL; /* to hide compiler warnings */
5875 int rc;
5876 uint32_t lun, unpacked_lun;
5877 int fn;
5878 void *iocb;
5880 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
5882 if (tgt->tgt_stop)
5883 goto out_term2;
5885 s_id = prm->tm_iocb2.u.isp24.fcp_hdr.s_id;
5886 sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha, s_id);
5887 if (!sess) {
5888 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
5890 sess = qlt_make_local_sess(vha, s_id);
5891 /* sess has got an extra creation ref */
5893 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
5894 if (!sess)
5895 goto out_term2;
5896 } else {
5897 if (sess->deleted) {
5898 sess = NULL;
5899 goto out_term2;
5902 if (!kref_get_unless_zero(&sess->sess_kref)) {
5903 ql_dbg(ql_dbg_tgt_tmr, vha, 0xffff,
5904 "%s: kref_get fail %8phC\n",
5905 __func__, sess->port_name);
5906 sess = NULL;
5907 goto out_term2;
5911 iocb = a;
5912 lun = a->u.isp24.fcp_cmnd.lun;
5913 fn = a->u.isp24.fcp_cmnd.task_mgmt_flags;
5914 unpacked_lun = scsilun_to_int((struct scsi_lun *)&lun);
5916 rc = qlt_issue_task_mgmt(sess, unpacked_lun, fn, iocb, 0);
5917 ha->tgt.tgt_ops->put_sess(sess);
5918 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
5920 if (rc != 0)
5921 goto out_term;
5922 return;
5924 out_term2:
5925 if (sess)
5926 ha->tgt.tgt_ops->put_sess(sess);
5927 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
5928 out_term:
5929 qlt_send_term_exchange(vha, NULL, &prm->tm_iocb2, 1, 0);
5932 static void qlt_sess_work_fn(struct work_struct *work)
5934 struct qla_tgt *tgt = container_of(work, struct qla_tgt, sess_work);
5935 struct scsi_qla_host *vha = tgt->vha;
5936 unsigned long flags;
5938 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf000, "Sess work (tgt %p)", tgt);
5940 spin_lock_irqsave(&tgt->sess_work_lock, flags);
5941 while (!list_empty(&tgt->sess_works_list)) {
5942 struct qla_tgt_sess_work_param *prm = list_entry(
5943 tgt->sess_works_list.next, typeof(*prm),
5944 sess_works_list_entry);
5947 * This work can be scheduled on several CPUs at time, so we
5948 * must delete the entry to eliminate double processing
5950 list_del(&prm->sess_works_list_entry);
5952 spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
5954 switch (prm->type) {
5955 case QLA_TGT_SESS_WORK_ABORT:
5956 qlt_abort_work(tgt, prm);
5957 break;
5958 case QLA_TGT_SESS_WORK_TM:
5959 qlt_tmr_work(tgt, prm);
5960 break;
5961 default:
5962 BUG_ON(1);
5963 break;
5966 spin_lock_irqsave(&tgt->sess_work_lock, flags);
5968 kfree(prm);
5970 spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
5973 /* Must be called under tgt_host_action_mutex */
5974 int qlt_add_target(struct qla_hw_data *ha, struct scsi_qla_host *base_vha)
5976 struct qla_tgt *tgt;
5978 if (!QLA_TGT_MODE_ENABLED())
5979 return 0;
5981 if (!IS_TGT_MODE_CAPABLE(ha)) {
5982 ql_log(ql_log_warn, base_vha, 0xe070,
5983 "This adapter does not support target mode.\n");
5984 return 0;
5987 ql_dbg(ql_dbg_tgt, base_vha, 0xe03b,
5988 "Registering target for host %ld(%p).\n", base_vha->host_no, ha);
5990 BUG_ON(base_vha->vha_tgt.qla_tgt != NULL);
5992 tgt = kzalloc(sizeof(struct qla_tgt), GFP_KERNEL);
5993 if (!tgt) {
5994 ql_dbg(ql_dbg_tgt, base_vha, 0xe066,
5995 "Unable to allocate struct qla_tgt\n");
5996 return -ENOMEM;
5999 if (!(base_vha->host->hostt->supported_mode & MODE_TARGET))
6000 base_vha->host->hostt->supported_mode |= MODE_TARGET;
6002 tgt->ha = ha;
6003 tgt->vha = base_vha;
6004 init_waitqueue_head(&tgt->waitQ);
6005 INIT_LIST_HEAD(&tgt->del_sess_list);
6006 spin_lock_init(&tgt->sess_work_lock);
6007 INIT_WORK(&tgt->sess_work, qlt_sess_work_fn);
6008 INIT_LIST_HEAD(&tgt->sess_works_list);
6009 atomic_set(&tgt->tgt_global_resets_count, 0);
6011 base_vha->vha_tgt.qla_tgt = tgt;
6013 ql_dbg(ql_dbg_tgt, base_vha, 0xe067,
6014 "qla_target(%d): using 64 Bit PCI addressing",
6015 base_vha->vp_idx);
6016 tgt->tgt_enable_64bit_addr = 1;
6017 /* 3 is reserved */
6018 tgt->sg_tablesize = QLA_TGT_MAX_SG_24XX(base_vha->req->length - 3);
6019 tgt->datasegs_per_cmd = QLA_TGT_DATASEGS_PER_CMD_24XX;
6020 tgt->datasegs_per_cont = QLA_TGT_DATASEGS_PER_CONT_24XX;
6022 mutex_lock(&qla_tgt_mutex);
6023 list_add_tail(&tgt->tgt_list_entry, &qla_tgt_glist);
6024 mutex_unlock(&qla_tgt_mutex);
6026 if (ha->tgt.tgt_ops && ha->tgt.tgt_ops->add_target)
6027 ha->tgt.tgt_ops->add_target(base_vha);
6029 return 0;
6032 /* Must be called under tgt_host_action_mutex */
6033 int qlt_remove_target(struct qla_hw_data *ha, struct scsi_qla_host *vha)
6035 if (!vha->vha_tgt.qla_tgt)
6036 return 0;
6038 if (vha->fc_vport) {
6039 qlt_release(vha->vha_tgt.qla_tgt);
6040 return 0;
6043 /* free left over qfull cmds */
6044 qlt_init_term_exchange(vha);
6046 mutex_lock(&qla_tgt_mutex);
6047 list_del(&vha->vha_tgt.qla_tgt->tgt_list_entry);
6048 mutex_unlock(&qla_tgt_mutex);
6050 ql_dbg(ql_dbg_tgt, vha, 0xe03c, "Unregistering target for host %ld(%p)",
6051 vha->host_no, ha);
6052 qlt_release(vha->vha_tgt.qla_tgt);
6054 return 0;
6057 void qlt_remove_target_resources(struct qla_hw_data *ha)
6059 struct scsi_qla_host *node;
6060 u32 key = 0;
6062 btree_for_each_safe32(&ha->tgt.host_map, key, node)
6063 btree_remove32(&ha->tgt.host_map, key);
6065 btree_destroy32(&ha->tgt.host_map);
6068 static void qlt_lport_dump(struct scsi_qla_host *vha, u64 wwpn,
6069 unsigned char *b)
6071 int i;
6073 pr_debug("qla2xxx HW vha->node_name: ");
6074 for (i = 0; i < WWN_SIZE; i++)
6075 pr_debug("%02x ", vha->node_name[i]);
6076 pr_debug("\n");
6077 pr_debug("qla2xxx HW vha->port_name: ");
6078 for (i = 0; i < WWN_SIZE; i++)
6079 pr_debug("%02x ", vha->port_name[i]);
6080 pr_debug("\n");
6082 pr_debug("qla2xxx passed configfs WWPN: ");
6083 put_unaligned_be64(wwpn, b);
6084 for (i = 0; i < WWN_SIZE; i++)
6085 pr_debug("%02x ", b[i]);
6086 pr_debug("\n");
6090 * qla_tgt_lport_register - register lport with external module
6092 * @qla_tgt_ops: Pointer for tcm_qla2xxx qla_tgt_ops
6093 * @wwpn: Passwd FC target WWPN
6094 * @callback: lport initialization callback for tcm_qla2xxx code
6095 * @target_lport_ptr: pointer for tcm_qla2xxx specific lport data
6097 int qlt_lport_register(void *target_lport_ptr, u64 phys_wwpn,
6098 u64 npiv_wwpn, u64 npiv_wwnn,
6099 int (*callback)(struct scsi_qla_host *, void *, u64, u64))
6101 struct qla_tgt *tgt;
6102 struct scsi_qla_host *vha;
6103 struct qla_hw_data *ha;
6104 struct Scsi_Host *host;
6105 unsigned long flags;
6106 int rc;
6107 u8 b[WWN_SIZE];
6109 mutex_lock(&qla_tgt_mutex);
6110 list_for_each_entry(tgt, &qla_tgt_glist, tgt_list_entry) {
6111 vha = tgt->vha;
6112 ha = vha->hw;
6114 host = vha->host;
6115 if (!host)
6116 continue;
6118 if (!(host->hostt->supported_mode & MODE_TARGET))
6119 continue;
6121 spin_lock_irqsave(&ha->hardware_lock, flags);
6122 if ((!npiv_wwpn || !npiv_wwnn) && host->active_mode & MODE_TARGET) {
6123 pr_debug("MODE_TARGET already active on qla2xxx(%d)\n",
6124 host->host_no);
6125 spin_unlock_irqrestore(&ha->hardware_lock, flags);
6126 continue;
6128 if (tgt->tgt_stop) {
6129 pr_debug("MODE_TARGET in shutdown on qla2xxx(%d)\n",
6130 host->host_no);
6131 spin_unlock_irqrestore(&ha->hardware_lock, flags);
6132 continue;
6134 spin_unlock_irqrestore(&ha->hardware_lock, flags);
6136 if (!scsi_host_get(host)) {
6137 ql_dbg(ql_dbg_tgt, vha, 0xe068,
6138 "Unable to scsi_host_get() for"
6139 " qla2xxx scsi_host\n");
6140 continue;
6142 qlt_lport_dump(vha, phys_wwpn, b);
6144 if (memcmp(vha->port_name, b, WWN_SIZE)) {
6145 scsi_host_put(host);
6146 continue;
6148 rc = (*callback)(vha, target_lport_ptr, npiv_wwpn, npiv_wwnn);
6149 if (rc != 0)
6150 scsi_host_put(host);
6152 mutex_unlock(&qla_tgt_mutex);
6153 return rc;
6155 mutex_unlock(&qla_tgt_mutex);
6157 return -ENODEV;
6159 EXPORT_SYMBOL(qlt_lport_register);
6162 * qla_tgt_lport_deregister - Degister lport
6164 * @vha: Registered scsi_qla_host pointer
6166 void qlt_lport_deregister(struct scsi_qla_host *vha)
6168 struct qla_hw_data *ha = vha->hw;
6169 struct Scsi_Host *sh = vha->host;
6171 * Clear the target_lport_ptr qla_target_template pointer in qla_hw_data
6173 vha->vha_tgt.target_lport_ptr = NULL;
6174 ha->tgt.tgt_ops = NULL;
6176 * Release the Scsi_Host reference for the underlying qla2xxx host
6178 scsi_host_put(sh);
6180 EXPORT_SYMBOL(qlt_lport_deregister);
6182 /* Must be called under HW lock */
6183 static void qlt_set_mode(struct scsi_qla_host *vha)
6185 switch (ql2x_ini_mode) {
6186 case QLA2XXX_INI_MODE_DISABLED:
6187 case QLA2XXX_INI_MODE_EXCLUSIVE:
6188 vha->host->active_mode = MODE_TARGET;
6189 break;
6190 case QLA2XXX_INI_MODE_ENABLED:
6191 vha->host->active_mode = MODE_UNKNOWN;
6192 break;
6193 case QLA2XXX_INI_MODE_DUAL:
6194 vha->host->active_mode = MODE_DUAL;
6195 break;
6196 default:
6197 break;
6201 /* Must be called under HW lock */
6202 static void qlt_clear_mode(struct scsi_qla_host *vha)
6204 switch (ql2x_ini_mode) {
6205 case QLA2XXX_INI_MODE_DISABLED:
6206 vha->host->active_mode = MODE_UNKNOWN;
6207 break;
6208 case QLA2XXX_INI_MODE_EXCLUSIVE:
6209 vha->host->active_mode = MODE_INITIATOR;
6210 break;
6211 case QLA2XXX_INI_MODE_ENABLED:
6212 case QLA2XXX_INI_MODE_DUAL:
6213 vha->host->active_mode = MODE_INITIATOR;
6214 break;
6215 default:
6216 break;
6221 * qla_tgt_enable_vha - NO LOCK HELD
6223 * host_reset, bring up w/ Target Mode Enabled
6225 void
6226 qlt_enable_vha(struct scsi_qla_host *vha)
6228 struct qla_hw_data *ha = vha->hw;
6229 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
6230 unsigned long flags;
6231 scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
6232 int rspq_ent = QLA83XX_RSPQ_MSIX_ENTRY_NUMBER;
6234 if (!tgt) {
6235 ql_dbg(ql_dbg_tgt, vha, 0xe069,
6236 "Unable to locate qla_tgt pointer from"
6237 " struct qla_hw_data\n");
6238 dump_stack();
6239 return;
6242 spin_lock_irqsave(&ha->hardware_lock, flags);
6243 tgt->tgt_stopped = 0;
6244 qlt_set_mode(vha);
6245 spin_unlock_irqrestore(&ha->hardware_lock, flags);
6247 if (vha->vp_idx) {
6248 qla24xx_disable_vp(vha);
6249 qla24xx_enable_vp(vha);
6250 } else {
6251 if (ha->msix_entries) {
6252 ql_dbg(ql_dbg_tgt, vha, 0xffff,
6253 "%s: host%ld : vector %d cpu %d\n",
6254 __func__, vha->host_no,
6255 ha->msix_entries[rspq_ent].vector,
6256 ha->msix_entries[rspq_ent].cpuid);
6258 ha->tgt.rspq_vector_cpuid =
6259 ha->msix_entries[rspq_ent].cpuid;
6262 set_bit(ISP_ABORT_NEEDED, &base_vha->dpc_flags);
6263 qla2xxx_wake_dpc(base_vha);
6264 qla2x00_wait_for_hba_online(base_vha);
6267 EXPORT_SYMBOL(qlt_enable_vha);
6270 * qla_tgt_disable_vha - NO LOCK HELD
6272 * Disable Target Mode and reset the adapter
6274 static void qlt_disable_vha(struct scsi_qla_host *vha)
6276 struct qla_hw_data *ha = vha->hw;
6277 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
6278 unsigned long flags;
6280 if (!tgt) {
6281 ql_dbg(ql_dbg_tgt, vha, 0xe06a,
6282 "Unable to locate qla_tgt pointer from"
6283 " struct qla_hw_data\n");
6284 dump_stack();
6285 return;
6288 spin_lock_irqsave(&ha->hardware_lock, flags);
6289 qlt_clear_mode(vha);
6290 spin_unlock_irqrestore(&ha->hardware_lock, flags);
6292 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
6293 qla2xxx_wake_dpc(vha);
6294 qla2x00_wait_for_hba_online(vha);
6298 * Called from qla_init.c:qla24xx_vport_create() contex to setup
6299 * the target mode specific struct scsi_qla_host and struct qla_hw_data
6300 * members.
6302 void
6303 qlt_vport_create(struct scsi_qla_host *vha, struct qla_hw_data *ha)
6305 vha->vha_tgt.qla_tgt = NULL;
6307 mutex_init(&vha->vha_tgt.tgt_mutex);
6308 mutex_init(&vha->vha_tgt.tgt_host_action_mutex);
6310 qlt_clear_mode(vha);
6313 * NOTE: Currently the value is kept the same for <24xx and
6314 * >=24xx ISPs. If it is necessary to change it,
6315 * the check should be added for specific ISPs,
6316 * assigning the value appropriately.
6318 ha->tgt.atio_q_length = ATIO_ENTRY_CNT_24XX;
6320 qlt_add_target(ha, vha);
6323 void
6324 qlt_rff_id(struct scsi_qla_host *vha, struct ct_sns_req *ct_req)
6327 * FC-4 Feature bit 0 indicates target functionality to the name server.
6329 if (qla_tgt_mode_enabled(vha)) {
6330 ct_req->req.rff_id.fc4_feature = BIT_0;
6331 } else if (qla_ini_mode_enabled(vha)) {
6332 ct_req->req.rff_id.fc4_feature = BIT_1;
6333 } else if (qla_dual_mode_enabled(vha))
6334 ct_req->req.rff_id.fc4_feature = BIT_0 | BIT_1;
6338 * qlt_init_atio_q_entries() - Initializes ATIO queue entries.
6339 * @ha: HA context
6341 * Beginning of ATIO ring has initialization control block already built
6342 * by nvram config routine.
6344 * Returns 0 on success.
6346 void
6347 qlt_init_atio_q_entries(struct scsi_qla_host *vha)
6349 struct qla_hw_data *ha = vha->hw;
6350 uint16_t cnt;
6351 struct atio_from_isp *pkt = (struct atio_from_isp *)ha->tgt.atio_ring;
6353 if (qla_ini_mode_enabled(vha))
6354 return;
6356 for (cnt = 0; cnt < ha->tgt.atio_q_length; cnt++) {
6357 pkt->u.raw.signature = ATIO_PROCESSED;
6358 pkt++;
6364 * qlt_24xx_process_atio_queue() - Process ATIO queue entries.
6365 * @ha: SCSI driver HA context
6367 void
6368 qlt_24xx_process_atio_queue(struct scsi_qla_host *vha, uint8_t ha_locked)
6370 struct qla_hw_data *ha = vha->hw;
6371 struct atio_from_isp *pkt;
6372 int cnt, i;
6374 if (!ha->flags.fw_started)
6375 return;
6377 while ((ha->tgt.atio_ring_ptr->signature != ATIO_PROCESSED) ||
6378 fcpcmd_is_corrupted(ha->tgt.atio_ring_ptr)) {
6379 pkt = (struct atio_from_isp *)ha->tgt.atio_ring_ptr;
6380 cnt = pkt->u.raw.entry_count;
6382 if (unlikely(fcpcmd_is_corrupted(ha->tgt.atio_ring_ptr))) {
6384 * This packet is corrupted. The header + payload
6385 * can not be trusted. There is no point in passing
6386 * it further up.
6388 ql_log(ql_log_warn, vha, 0xffff,
6389 "corrupted fcp frame SID[%3phN] OXID[%04x] EXCG[%x] %64phN\n",
6390 pkt->u.isp24.fcp_hdr.s_id,
6391 be16_to_cpu(pkt->u.isp24.fcp_hdr.ox_id),
6392 le32_to_cpu(pkt->u.isp24.exchange_addr), pkt);
6394 adjust_corrupted_atio(pkt);
6395 qlt_send_term_exchange(vha, NULL, pkt, ha_locked, 0);
6396 } else {
6397 qlt_24xx_atio_pkt_all_vps(vha,
6398 (struct atio_from_isp *)pkt, ha_locked);
6401 for (i = 0; i < cnt; i++) {
6402 ha->tgt.atio_ring_index++;
6403 if (ha->tgt.atio_ring_index == ha->tgt.atio_q_length) {
6404 ha->tgt.atio_ring_index = 0;
6405 ha->tgt.atio_ring_ptr = ha->tgt.atio_ring;
6406 } else
6407 ha->tgt.atio_ring_ptr++;
6409 pkt->u.raw.signature = ATIO_PROCESSED;
6410 pkt = (struct atio_from_isp *)ha->tgt.atio_ring_ptr;
6412 wmb();
6415 /* Adjust ring index */
6416 WRT_REG_DWORD(ISP_ATIO_Q_OUT(vha), ha->tgt.atio_ring_index);
6417 RD_REG_DWORD_RELAXED(ISP_ATIO_Q_OUT(vha));
6420 void
6421 qlt_24xx_config_rings(struct scsi_qla_host *vha)
6423 struct qla_hw_data *ha = vha->hw;
6424 if (!QLA_TGT_MODE_ENABLED())
6425 return;
6427 WRT_REG_DWORD(ISP_ATIO_Q_IN(vha), 0);
6428 WRT_REG_DWORD(ISP_ATIO_Q_OUT(vha), 0);
6429 RD_REG_DWORD(ISP_ATIO_Q_OUT(vha));
6431 if (IS_ATIO_MSIX_CAPABLE(ha)) {
6432 struct qla_msix_entry *msix = &ha->msix_entries[2];
6433 struct init_cb_24xx *icb = (struct init_cb_24xx *)ha->init_cb;
6435 icb->msix_atio = cpu_to_le16(msix->entry);
6436 ql_dbg(ql_dbg_init, vha, 0xf072,
6437 "Registering ICB vector 0x%x for atio que.\n",
6438 msix->entry);
6442 void
6443 qlt_24xx_config_nvram_stage1(struct scsi_qla_host *vha, struct nvram_24xx *nv)
6445 struct qla_hw_data *ha = vha->hw;
6446 u32 tmp;
6447 u16 t;
6449 if (qla_tgt_mode_enabled(vha) || qla_dual_mode_enabled(vha)) {
6450 if (!ha->tgt.saved_set) {
6451 /* We save only once */
6452 ha->tgt.saved_exchange_count = nv->exchange_count;
6453 ha->tgt.saved_firmware_options_1 =
6454 nv->firmware_options_1;
6455 ha->tgt.saved_firmware_options_2 =
6456 nv->firmware_options_2;
6457 ha->tgt.saved_firmware_options_3 =
6458 nv->firmware_options_3;
6459 ha->tgt.saved_set = 1;
6462 if (qla_tgt_mode_enabled(vha)) {
6463 nv->exchange_count = cpu_to_le16(0xFFFF);
6464 } else { /* dual */
6465 if (ql_dm_tgt_ex_pct > 100) {
6466 ql_dm_tgt_ex_pct = 50;
6467 } else if (ql_dm_tgt_ex_pct == 100) {
6468 /* leave some for FW */
6469 ql_dm_tgt_ex_pct = 95;
6472 tmp = ha->orig_fw_xcb_count * ql_dm_tgt_ex_pct;
6473 tmp = tmp/100;
6474 if (tmp > 0xffff)
6475 tmp = 0xffff;
6477 t = tmp & 0xffff;
6478 nv->exchange_count = cpu_to_le16(t);
6481 /* Enable target mode */
6482 nv->firmware_options_1 |= cpu_to_le32(BIT_4);
6484 /* Disable ini mode, if requested */
6485 if (qla_tgt_mode_enabled(vha))
6486 nv->firmware_options_1 |= cpu_to_le32(BIT_5);
6488 /* Disable Full Login after LIP */
6489 nv->firmware_options_1 &= cpu_to_le32(~BIT_13);
6490 /* Enable initial LIP */
6491 nv->firmware_options_1 &= cpu_to_le32(~BIT_9);
6492 if (ql2xtgt_tape_enable)
6493 /* Enable FC Tape support */
6494 nv->firmware_options_2 |= cpu_to_le32(BIT_12);
6495 else
6496 /* Disable FC Tape support */
6497 nv->firmware_options_2 &= cpu_to_le32(~BIT_12);
6499 /* Disable Full Login after LIP */
6500 nv->host_p &= cpu_to_le32(~BIT_10);
6503 * clear BIT 15 explicitly as we have seen at least
6504 * a couple of instances where this was set and this
6505 * was causing the firmware to not be initialized.
6507 nv->firmware_options_1 &= cpu_to_le32(~BIT_15);
6508 /* Enable target PRLI control */
6509 nv->firmware_options_2 |= cpu_to_le32(BIT_14);
6510 } else {
6511 if (ha->tgt.saved_set) {
6512 nv->exchange_count = ha->tgt.saved_exchange_count;
6513 nv->firmware_options_1 =
6514 ha->tgt.saved_firmware_options_1;
6515 nv->firmware_options_2 =
6516 ha->tgt.saved_firmware_options_2;
6517 nv->firmware_options_3 =
6518 ha->tgt.saved_firmware_options_3;
6520 return;
6523 if (ha->tgt.enable_class_2) {
6524 if (vha->flags.init_done)
6525 fc_host_supported_classes(vha->host) =
6526 FC_COS_CLASS2 | FC_COS_CLASS3;
6528 nv->firmware_options_2 |= cpu_to_le32(BIT_8);
6529 } else {
6530 if (vha->flags.init_done)
6531 fc_host_supported_classes(vha->host) = FC_COS_CLASS3;
6533 nv->firmware_options_2 &= ~cpu_to_le32(BIT_8);
6537 void
6538 qlt_24xx_config_nvram_stage2(struct scsi_qla_host *vha,
6539 struct init_cb_24xx *icb)
6541 struct qla_hw_data *ha = vha->hw;
6543 if (!QLA_TGT_MODE_ENABLED())
6544 return;
6546 if (ha->tgt.node_name_set) {
6547 memcpy(icb->node_name, ha->tgt.tgt_node_name, WWN_SIZE);
6548 icb->firmware_options_1 |= cpu_to_le32(BIT_14);
6551 /* disable ZIO at start time. */
6552 if (!vha->flags.init_done) {
6553 uint32_t tmp;
6554 tmp = le32_to_cpu(icb->firmware_options_2);
6555 tmp &= ~(BIT_3 | BIT_2 | BIT_1 | BIT_0);
6556 icb->firmware_options_2 = cpu_to_le32(tmp);
6560 void
6561 qlt_81xx_config_nvram_stage1(struct scsi_qla_host *vha, struct nvram_81xx *nv)
6563 struct qla_hw_data *ha = vha->hw;
6564 u32 tmp;
6565 u16 t;
6567 if (!QLA_TGT_MODE_ENABLED())
6568 return;
6570 if (qla_tgt_mode_enabled(vha) || qla_dual_mode_enabled(vha)) {
6571 if (!ha->tgt.saved_set) {
6572 /* We save only once */
6573 ha->tgt.saved_exchange_count = nv->exchange_count;
6574 ha->tgt.saved_firmware_options_1 =
6575 nv->firmware_options_1;
6576 ha->tgt.saved_firmware_options_2 =
6577 nv->firmware_options_2;
6578 ha->tgt.saved_firmware_options_3 =
6579 nv->firmware_options_3;
6580 ha->tgt.saved_set = 1;
6583 if (qla_tgt_mode_enabled(vha)) {
6584 nv->exchange_count = cpu_to_le16(0xFFFF);
6585 } else { /* dual */
6586 if (ql_dm_tgt_ex_pct > 100) {
6587 ql_dm_tgt_ex_pct = 50;
6588 } else if (ql_dm_tgt_ex_pct == 100) {
6589 /* leave some for FW */
6590 ql_dm_tgt_ex_pct = 95;
6593 tmp = ha->orig_fw_xcb_count * ql_dm_tgt_ex_pct;
6594 tmp = tmp/100;
6595 if (tmp > 0xffff)
6596 tmp = 0xffff;
6597 t = tmp & 0xffff;
6598 nv->exchange_count = cpu_to_le16(t);
6601 /* Enable target mode */
6602 nv->firmware_options_1 |= cpu_to_le32(BIT_4);
6604 /* Disable ini mode, if requested */
6605 if (qla_tgt_mode_enabled(vha))
6606 nv->firmware_options_1 |= cpu_to_le32(BIT_5);
6607 /* Disable Full Login after LIP */
6608 nv->firmware_options_1 &= cpu_to_le32(~BIT_13);
6609 /* Enable initial LIP */
6610 nv->firmware_options_1 &= cpu_to_le32(~BIT_9);
6612 * clear BIT 15 explicitly as we have seen at
6613 * least a couple of instances where this was set
6614 * and this was causing the firmware to not be
6615 * initialized.
6617 nv->firmware_options_1 &= cpu_to_le32(~BIT_15);
6618 if (ql2xtgt_tape_enable)
6619 /* Enable FC tape support */
6620 nv->firmware_options_2 |= cpu_to_le32(BIT_12);
6621 else
6622 /* Disable FC tape support */
6623 nv->firmware_options_2 &= cpu_to_le32(~BIT_12);
6625 /* Disable Full Login after LIP */
6626 nv->host_p &= cpu_to_le32(~BIT_10);
6627 /* Enable target PRLI control */
6628 nv->firmware_options_2 |= cpu_to_le32(BIT_14);
6629 } else {
6630 if (ha->tgt.saved_set) {
6631 nv->exchange_count = ha->tgt.saved_exchange_count;
6632 nv->firmware_options_1 =
6633 ha->tgt.saved_firmware_options_1;
6634 nv->firmware_options_2 =
6635 ha->tgt.saved_firmware_options_2;
6636 nv->firmware_options_3 =
6637 ha->tgt.saved_firmware_options_3;
6639 return;
6642 if (ha->tgt.enable_class_2) {
6643 if (vha->flags.init_done)
6644 fc_host_supported_classes(vha->host) =
6645 FC_COS_CLASS2 | FC_COS_CLASS3;
6647 nv->firmware_options_2 |= cpu_to_le32(BIT_8);
6648 } else {
6649 if (vha->flags.init_done)
6650 fc_host_supported_classes(vha->host) = FC_COS_CLASS3;
6652 nv->firmware_options_2 &= ~cpu_to_le32(BIT_8);
6656 void
6657 qlt_81xx_config_nvram_stage2(struct scsi_qla_host *vha,
6658 struct init_cb_81xx *icb)
6660 struct qla_hw_data *ha = vha->hw;
6662 if (!QLA_TGT_MODE_ENABLED())
6663 return;
6665 if (ha->tgt.node_name_set) {
6666 memcpy(icb->node_name, ha->tgt.tgt_node_name, WWN_SIZE);
6667 icb->firmware_options_1 |= cpu_to_le32(BIT_14);
6670 /* disable ZIO at start time. */
6671 if (!vha->flags.init_done) {
6672 uint32_t tmp;
6673 tmp = le32_to_cpu(icb->firmware_options_2);
6674 tmp &= ~(BIT_3 | BIT_2 | BIT_1 | BIT_0);
6675 icb->firmware_options_2 = cpu_to_le32(tmp);
6680 void
6681 qlt_83xx_iospace_config(struct qla_hw_data *ha)
6683 if (!QLA_TGT_MODE_ENABLED())
6684 return;
6686 ha->msix_count += 1; /* For ATIO Q */
6690 qlt_24xx_process_response_error(struct scsi_qla_host *vha,
6691 struct sts_entry_24xx *pkt)
6693 switch (pkt->entry_type) {
6694 case ABTS_RECV_24XX:
6695 case ABTS_RESP_24XX:
6696 case CTIO_TYPE7:
6697 case NOTIFY_ACK_TYPE:
6698 case CTIO_CRC2:
6699 return 1;
6700 default:
6701 return 0;
6705 void
6706 qlt_modify_vp_config(struct scsi_qla_host *vha,
6707 struct vp_config_entry_24xx *vpmod)
6709 /* enable target mode. Bit5 = 1 => disable */
6710 if (qla_tgt_mode_enabled(vha) || qla_dual_mode_enabled(vha))
6711 vpmod->options_idx1 &= ~BIT_5;
6713 /* Disable ini mode, if requested. bit4 = 1 => disable */
6714 if (qla_tgt_mode_enabled(vha))
6715 vpmod->options_idx1 &= ~BIT_4;
6718 void
6719 qlt_probe_one_stage1(struct scsi_qla_host *base_vha, struct qla_hw_data *ha)
6721 int rc;
6723 if (!QLA_TGT_MODE_ENABLED())
6724 return;
6726 if (ha->mqenable || IS_QLA83XX(ha) || IS_QLA27XX(ha)) {
6727 ISP_ATIO_Q_IN(base_vha) = &ha->mqiobase->isp25mq.atio_q_in;
6728 ISP_ATIO_Q_OUT(base_vha) = &ha->mqiobase->isp25mq.atio_q_out;
6729 } else {
6730 ISP_ATIO_Q_IN(base_vha) = &ha->iobase->isp24.atio_q_in;
6731 ISP_ATIO_Q_OUT(base_vha) = &ha->iobase->isp24.atio_q_out;
6734 mutex_init(&base_vha->vha_tgt.tgt_mutex);
6735 mutex_init(&base_vha->vha_tgt.tgt_host_action_mutex);
6737 INIT_LIST_HEAD(&base_vha->unknown_atio_list);
6738 INIT_DELAYED_WORK(&base_vha->unknown_atio_work,
6739 qlt_unknown_atio_work_fn);
6741 qlt_clear_mode(base_vha);
6743 rc = btree_init32(&ha->tgt.host_map);
6744 if (rc)
6745 ql_log(ql_log_info, base_vha, 0xffff,
6746 "Unable to initialize ha->host_map btree\n");
6748 qlt_update_vp_map(base_vha, SET_VP_IDX);
6751 irqreturn_t
6752 qla83xx_msix_atio_q(int irq, void *dev_id)
6754 struct rsp_que *rsp;
6755 scsi_qla_host_t *vha;
6756 struct qla_hw_data *ha;
6757 unsigned long flags;
6759 rsp = (struct rsp_que *) dev_id;
6760 ha = rsp->hw;
6761 vha = pci_get_drvdata(ha->pdev);
6763 spin_lock_irqsave(&ha->tgt.atio_lock, flags);
6765 qlt_24xx_process_atio_queue(vha, 0);
6767 spin_unlock_irqrestore(&ha->tgt.atio_lock, flags);
6769 return IRQ_HANDLED;
6772 static void
6773 qlt_handle_abts_recv_work(struct work_struct *work)
6775 struct qla_tgt_sess_op *op = container_of(work,
6776 struct qla_tgt_sess_op, work);
6777 scsi_qla_host_t *vha = op->vha;
6778 struct qla_hw_data *ha = vha->hw;
6779 unsigned long flags;
6781 if (qla2x00_reset_active(vha) || (op->chip_reset != ha->chip_reset))
6782 return;
6784 spin_lock_irqsave(&ha->tgt.atio_lock, flags);
6785 qlt_24xx_process_atio_queue(vha, 0);
6786 spin_unlock_irqrestore(&ha->tgt.atio_lock, flags);
6788 spin_lock_irqsave(&ha->hardware_lock, flags);
6789 qlt_response_pkt_all_vps(vha, (response_t *)&op->atio);
6790 spin_unlock_irqrestore(&ha->hardware_lock, flags);
6792 kfree(op);
6795 void
6796 qlt_handle_abts_recv(struct scsi_qla_host *vha, response_t *pkt)
6798 struct qla_tgt_sess_op *op;
6800 op = kzalloc(sizeof(*op), GFP_ATOMIC);
6802 if (!op) {
6803 /* do not reach for ATIO queue here. This is best effort err
6804 * recovery at this point.
6806 qlt_response_pkt_all_vps(vha, pkt);
6807 return;
6810 memcpy(&op->atio, pkt, sizeof(*pkt));
6811 op->vha = vha;
6812 op->chip_reset = vha->hw->chip_reset;
6813 INIT_WORK(&op->work, qlt_handle_abts_recv_work);
6814 queue_work(qla_tgt_wq, &op->work);
6815 return;
6819 qlt_mem_alloc(struct qla_hw_data *ha)
6821 if (!QLA_TGT_MODE_ENABLED())
6822 return 0;
6824 ha->tgt.tgt_vp_map = kzalloc(sizeof(struct qla_tgt_vp_map) *
6825 MAX_MULTI_ID_FABRIC, GFP_KERNEL);
6826 if (!ha->tgt.tgt_vp_map)
6827 return -ENOMEM;
6829 ha->tgt.atio_ring = dma_alloc_coherent(&ha->pdev->dev,
6830 (ha->tgt.atio_q_length + 1) * sizeof(struct atio_from_isp),
6831 &ha->tgt.atio_dma, GFP_KERNEL);
6832 if (!ha->tgt.atio_ring) {
6833 kfree(ha->tgt.tgt_vp_map);
6834 return -ENOMEM;
6836 return 0;
6839 void
6840 qlt_mem_free(struct qla_hw_data *ha)
6842 if (!QLA_TGT_MODE_ENABLED())
6843 return;
6845 if (ha->tgt.atio_ring) {
6846 dma_free_coherent(&ha->pdev->dev, (ha->tgt.atio_q_length + 1) *
6847 sizeof(struct atio_from_isp), ha->tgt.atio_ring,
6848 ha->tgt.atio_dma);
6850 kfree(ha->tgt.tgt_vp_map);
6853 /* vport_slock to be held by the caller */
6854 void
6855 qlt_update_vp_map(struct scsi_qla_host *vha, int cmd)
6857 void *slot;
6858 u32 key;
6859 int rc;
6861 if (!QLA_TGT_MODE_ENABLED())
6862 return;
6864 key = vha->d_id.b24;
6866 switch (cmd) {
6867 case SET_VP_IDX:
6868 vha->hw->tgt.tgt_vp_map[vha->vp_idx].vha = vha;
6869 break;
6870 case SET_AL_PA:
6871 slot = btree_lookup32(&vha->hw->tgt.host_map, key);
6872 if (!slot) {
6873 ql_dbg(ql_dbg_tgt_mgt, vha, 0xffff,
6874 "Save vha in host_map %p %06x\n", vha, key);
6875 rc = btree_insert32(&vha->hw->tgt.host_map,
6876 key, vha, GFP_ATOMIC);
6877 if (rc)
6878 ql_log(ql_log_info, vha, 0xffff,
6879 "Unable to insert s_id into host_map: %06x\n",
6880 key);
6881 return;
6883 ql_dbg(ql_dbg_tgt_mgt, vha, 0xffff,
6884 "replace existing vha in host_map %p %06x\n", vha, key);
6885 btree_update32(&vha->hw->tgt.host_map, key, vha);
6886 break;
6887 case RESET_VP_IDX:
6888 vha->hw->tgt.tgt_vp_map[vha->vp_idx].vha = NULL;
6889 break;
6890 case RESET_AL_PA:
6891 ql_dbg(ql_dbg_tgt_mgt, vha, 0xffff,
6892 "clear vha in host_map %p %06x\n", vha, key);
6893 slot = btree_lookup32(&vha->hw->tgt.host_map, key);
6894 if (slot)
6895 btree_remove32(&vha->hw->tgt.host_map, key);
6896 vha->d_id.b24 = 0;
6897 break;
6901 void qlt_update_host_map(struct scsi_qla_host *vha, port_id_t id)
6903 unsigned long flags;
6904 struct qla_hw_data *ha = vha->hw;
6906 if (!vha->d_id.b24) {
6907 spin_lock_irqsave(&ha->vport_slock, flags);
6908 vha->d_id = id;
6909 qlt_update_vp_map(vha, SET_AL_PA);
6910 spin_unlock_irqrestore(&ha->vport_slock, flags);
6911 } else if (vha->d_id.b24 != id.b24) {
6912 spin_lock_irqsave(&ha->vport_slock, flags);
6913 qlt_update_vp_map(vha, RESET_AL_PA);
6914 vha->d_id = id;
6915 qlt_update_vp_map(vha, SET_AL_PA);
6916 spin_unlock_irqrestore(&ha->vport_slock, flags);
6920 static int __init qlt_parse_ini_mode(void)
6922 if (strcasecmp(qlini_mode, QLA2XXX_INI_MODE_STR_EXCLUSIVE) == 0)
6923 ql2x_ini_mode = QLA2XXX_INI_MODE_EXCLUSIVE;
6924 else if (strcasecmp(qlini_mode, QLA2XXX_INI_MODE_STR_DISABLED) == 0)
6925 ql2x_ini_mode = QLA2XXX_INI_MODE_DISABLED;
6926 else if (strcasecmp(qlini_mode, QLA2XXX_INI_MODE_STR_ENABLED) == 0)
6927 ql2x_ini_mode = QLA2XXX_INI_MODE_ENABLED;
6928 else if (strcasecmp(qlini_mode, QLA2XXX_INI_MODE_STR_DUAL) == 0)
6929 ql2x_ini_mode = QLA2XXX_INI_MODE_DUAL;
6930 else
6931 return false;
6933 return true;
6936 int __init qlt_init(void)
6938 int ret;
6940 if (!qlt_parse_ini_mode()) {
6941 ql_log(ql_log_fatal, NULL, 0xe06b,
6942 "qlt_parse_ini_mode() failed\n");
6943 return -EINVAL;
6946 if (!QLA_TGT_MODE_ENABLED())
6947 return 0;
6949 qla_tgt_mgmt_cmd_cachep = kmem_cache_create("qla_tgt_mgmt_cmd_cachep",
6950 sizeof(struct qla_tgt_mgmt_cmd), __alignof__(struct
6951 qla_tgt_mgmt_cmd), 0, NULL);
6952 if (!qla_tgt_mgmt_cmd_cachep) {
6953 ql_log(ql_log_fatal, NULL, 0xe06d,
6954 "kmem_cache_create for qla_tgt_mgmt_cmd_cachep failed\n");
6955 return -ENOMEM;
6958 qla_tgt_plogi_cachep = kmem_cache_create("qla_tgt_plogi_cachep",
6959 sizeof(struct qlt_plogi_ack_t), __alignof__(struct qlt_plogi_ack_t),
6960 0, NULL);
6962 if (!qla_tgt_plogi_cachep) {
6963 ql_log(ql_log_fatal, NULL, 0xe06d,
6964 "kmem_cache_create for qla_tgt_plogi_cachep failed\n");
6965 ret = -ENOMEM;
6966 goto out_mgmt_cmd_cachep;
6969 qla_tgt_mgmt_cmd_mempool = mempool_create(25, mempool_alloc_slab,
6970 mempool_free_slab, qla_tgt_mgmt_cmd_cachep);
6971 if (!qla_tgt_mgmt_cmd_mempool) {
6972 ql_log(ql_log_fatal, NULL, 0xe06e,
6973 "mempool_create for qla_tgt_mgmt_cmd_mempool failed\n");
6974 ret = -ENOMEM;
6975 goto out_plogi_cachep;
6978 qla_tgt_wq = alloc_workqueue("qla_tgt_wq", 0, 0);
6979 if (!qla_tgt_wq) {
6980 ql_log(ql_log_fatal, NULL, 0xe06f,
6981 "alloc_workqueue for qla_tgt_wq failed\n");
6982 ret = -ENOMEM;
6983 goto out_cmd_mempool;
6986 * Return 1 to signal that initiator-mode is being disabled
6988 return (ql2x_ini_mode == QLA2XXX_INI_MODE_DISABLED) ? 1 : 0;
6990 out_cmd_mempool:
6991 mempool_destroy(qla_tgt_mgmt_cmd_mempool);
6992 out_plogi_cachep:
6993 kmem_cache_destroy(qla_tgt_plogi_cachep);
6994 out_mgmt_cmd_cachep:
6995 kmem_cache_destroy(qla_tgt_mgmt_cmd_cachep);
6996 return ret;
6999 void qlt_exit(void)
7001 if (!QLA_TGT_MODE_ENABLED())
7002 return;
7004 destroy_workqueue(qla_tgt_wq);
7005 mempool_destroy(qla_tgt_mgmt_cmd_mempool);
7006 kmem_cache_destroy(qla_tgt_plogi_cachep);
7007 kmem_cache_destroy(qla_tgt_mgmt_cmd_cachep);