Merge tag 'ceph-for-4.13-rc8' of git://github.com/ceph/ceph-client
[linux/fpc-iii.git] / drivers / scsi / qla2xxx / qla_target.c
blobe101cd3043b94539a2893e77aa9949c8fd9c7acb
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 = 0;
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 ql2xuctrlirq = 1;
70 module_param(ql2xuctrlirq, int, 0644);
71 MODULE_PARM_DESC(ql2xuctrlirq,
72 "User to control IRQ placement via smp_affinity."
73 "Valid with qlini_mode=disabled."
74 "1(default): enable");
76 int ql2x_ini_mode = QLA2XXX_INI_MODE_EXCLUSIVE;
78 static int temp_sam_status = SAM_STAT_BUSY;
81 * From scsi/fc/fc_fcp.h
83 enum fcp_resp_rsp_codes {
84 FCP_TMF_CMPL = 0,
85 FCP_DATA_LEN_INVALID = 1,
86 FCP_CMND_FIELDS_INVALID = 2,
87 FCP_DATA_PARAM_MISMATCH = 3,
88 FCP_TMF_REJECTED = 4,
89 FCP_TMF_FAILED = 5,
90 FCP_TMF_INVALID_LUN = 9,
94 * fc_pri_ta from scsi/fc/fc_fcp.h
96 #define FCP_PTA_SIMPLE 0 /* simple task attribute */
97 #define FCP_PTA_HEADQ 1 /* head of queue task attribute */
98 #define FCP_PTA_ORDERED 2 /* ordered task attribute */
99 #define FCP_PTA_ACA 4 /* auto. contingent allegiance */
100 #define FCP_PTA_MASK 7 /* mask for task attribute field */
101 #define FCP_PRI_SHIFT 3 /* priority field starts in bit 3 */
102 #define FCP_PRI_RESVD_MASK 0x80 /* reserved bits in priority field */
105 * This driver calls qla2x00_alloc_iocbs() and qla2x00_issue_marker(), which
106 * must be called under HW lock and could unlock/lock it inside.
107 * It isn't an issue, since in the current implementation on the time when
108 * those functions are called:
110 * - Either context is IRQ and only IRQ handler can modify HW data,
111 * including rings related fields,
113 * - Or access to target mode variables from struct qla_tgt doesn't
114 * cross those functions boundaries, except tgt_stop, which
115 * additionally protected by irq_cmd_count.
117 /* Predefs for callbacks handed to qla2xxx LLD */
118 static void qlt_24xx_atio_pkt(struct scsi_qla_host *ha,
119 struct atio_from_isp *pkt, uint8_t);
120 static void qlt_response_pkt(struct scsi_qla_host *ha, struct rsp_que *rsp,
121 response_t *pkt);
122 static int qlt_issue_task_mgmt(struct fc_port *sess, u64 lun,
123 int fn, void *iocb, int flags);
124 static void qlt_send_term_exchange(struct qla_qpair *, struct qla_tgt_cmd
125 *cmd, struct atio_from_isp *atio, int ha_locked, int ul_abort);
126 static void qlt_alloc_qfull_cmd(struct scsi_qla_host *vha,
127 struct atio_from_isp *atio, uint16_t status, int qfull);
128 static void qlt_disable_vha(struct scsi_qla_host *vha);
129 static void qlt_clear_tgt_db(struct qla_tgt *tgt);
130 static void qlt_send_notify_ack(struct qla_qpair *qpair,
131 struct imm_ntfy_from_isp *ntfy,
132 uint32_t add_flags, uint16_t resp_code, int resp_code_valid,
133 uint16_t srr_flags, uint16_t srr_reject_code, uint8_t srr_explan);
134 static void qlt_send_term_imm_notif(struct scsi_qla_host *vha,
135 struct imm_ntfy_from_isp *imm, int ha_locked);
136 static struct fc_port *qlt_create_sess(struct scsi_qla_host *vha,
137 fc_port_t *fcport, bool local);
138 void qlt_unreg_sess(struct fc_port *sess);
139 static void qlt_24xx_handle_abts(struct scsi_qla_host *,
140 struct abts_recv_from_24xx *);
141 static void qlt_send_busy(struct qla_qpair *, struct atio_from_isp *,
142 uint16_t);
145 * Global Variables
147 static struct kmem_cache *qla_tgt_mgmt_cmd_cachep;
148 static struct kmem_cache *qla_tgt_plogi_cachep;
149 static mempool_t *qla_tgt_mgmt_cmd_mempool;
150 static struct workqueue_struct *qla_tgt_wq;
151 static DEFINE_MUTEX(qla_tgt_mutex);
152 static LIST_HEAD(qla_tgt_glist);
154 static const char *prot_op_str(u32 prot_op)
156 switch (prot_op) {
157 case TARGET_PROT_NORMAL: return "NORMAL";
158 case TARGET_PROT_DIN_INSERT: return "DIN_INSERT";
159 case TARGET_PROT_DOUT_INSERT: return "DOUT_INSERT";
160 case TARGET_PROT_DIN_STRIP: return "DIN_STRIP";
161 case TARGET_PROT_DOUT_STRIP: return "DOUT_STRIP";
162 case TARGET_PROT_DIN_PASS: return "DIN_PASS";
163 case TARGET_PROT_DOUT_PASS: return "DOUT_PASS";
164 default: return "UNKNOWN";
168 /* This API intentionally takes dest as a parameter, rather than returning
169 * int value to avoid caller forgetting to issue wmb() after the store */
170 void qlt_do_generation_tick(struct scsi_qla_host *vha, int *dest)
172 scsi_qla_host_t *base_vha = pci_get_drvdata(vha->hw->pdev);
173 *dest = atomic_inc_return(&base_vha->generation_tick);
174 /* memory barrier */
175 wmb();
178 /* Might release hw lock, then reaquire!! */
179 static inline int qlt_issue_marker(struct scsi_qla_host *vha, int vha_locked)
181 /* Send marker if required */
182 if (unlikely(vha->marker_needed != 0)) {
183 int rc = qla2x00_issue_marker(vha, vha_locked);
184 if (rc != QLA_SUCCESS) {
185 ql_dbg(ql_dbg_tgt, vha, 0xe03d,
186 "qla_target(%d): issue_marker() failed\n",
187 vha->vp_idx);
189 return rc;
191 return QLA_SUCCESS;
194 static inline
195 struct scsi_qla_host *qlt_find_host_by_d_id(struct scsi_qla_host *vha,
196 uint8_t *d_id)
198 struct scsi_qla_host *host;
199 uint32_t key = 0;
201 if ((vha->d_id.b.area == d_id[1]) && (vha->d_id.b.domain == d_id[0]) &&
202 (vha->d_id.b.al_pa == d_id[2]))
203 return vha;
205 key = (uint32_t)d_id[0] << 16;
206 key |= (uint32_t)d_id[1] << 8;
207 key |= (uint32_t)d_id[2];
209 host = btree_lookup32(&vha->hw->tgt.host_map, key);
210 if (!host)
211 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf005,
212 "Unable to find host %06x\n", key);
214 return host;
217 static inline
218 struct scsi_qla_host *qlt_find_host_by_vp_idx(struct scsi_qla_host *vha,
219 uint16_t vp_idx)
221 struct qla_hw_data *ha = vha->hw;
223 if (vha->vp_idx == vp_idx)
224 return vha;
226 BUG_ON(ha->tgt.tgt_vp_map == NULL);
227 if (likely(test_bit(vp_idx, ha->vp_idx_map)))
228 return ha->tgt.tgt_vp_map[vp_idx].vha;
230 return NULL;
233 static inline void qlt_incr_num_pend_cmds(struct scsi_qla_host *vha)
235 unsigned long flags;
237 spin_lock_irqsave(&vha->hw->tgt.q_full_lock, flags);
239 vha->hw->tgt.num_pend_cmds++;
240 if (vha->hw->tgt.num_pend_cmds > vha->qla_stats.stat_max_pend_cmds)
241 vha->qla_stats.stat_max_pend_cmds =
242 vha->hw->tgt.num_pend_cmds;
243 spin_unlock_irqrestore(&vha->hw->tgt.q_full_lock, flags);
245 static inline void qlt_decr_num_pend_cmds(struct scsi_qla_host *vha)
247 unsigned long flags;
249 spin_lock_irqsave(&vha->hw->tgt.q_full_lock, flags);
250 vha->hw->tgt.num_pend_cmds--;
251 spin_unlock_irqrestore(&vha->hw->tgt.q_full_lock, flags);
255 static void qlt_queue_unknown_atio(scsi_qla_host_t *vha,
256 struct atio_from_isp *atio, uint8_t ha_locked)
258 struct qla_tgt_sess_op *u;
259 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
260 unsigned long flags;
262 if (tgt->tgt_stop) {
263 ql_dbg(ql_dbg_async, vha, 0x502c,
264 "qla_target(%d): dropping unknown ATIO_TYPE7, because tgt is being stopped",
265 vha->vp_idx);
266 goto out_term;
269 u = kzalloc(sizeof(*u), GFP_ATOMIC);
270 if (u == NULL)
271 goto out_term;
273 u->vha = vha;
274 memcpy(&u->atio, atio, sizeof(*atio));
275 INIT_LIST_HEAD(&u->cmd_list);
277 spin_lock_irqsave(&vha->cmd_list_lock, flags);
278 list_add_tail(&u->cmd_list, &vha->unknown_atio_list);
279 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
281 schedule_delayed_work(&vha->unknown_atio_work, 1);
283 out:
284 return;
286 out_term:
287 qlt_send_term_exchange(vha->hw->base_qpair, NULL, atio, ha_locked, 0);
288 goto out;
291 static void qlt_try_to_dequeue_unknown_atios(struct scsi_qla_host *vha,
292 uint8_t ha_locked)
294 struct qla_tgt_sess_op *u, *t;
295 scsi_qla_host_t *host;
296 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
297 unsigned long flags;
298 uint8_t queued = 0;
300 list_for_each_entry_safe(u, t, &vha->unknown_atio_list, cmd_list) {
301 if (u->aborted) {
302 ql_dbg(ql_dbg_async, vha, 0x502e,
303 "Freeing unknown %s %p, because of Abort\n",
304 "ATIO_TYPE7", u);
305 qlt_send_term_exchange(vha->hw->base_qpair, NULL,
306 &u->atio, ha_locked, 0);
307 goto abort;
310 host = qlt_find_host_by_d_id(vha, u->atio.u.isp24.fcp_hdr.d_id);
311 if (host != NULL) {
312 ql_dbg(ql_dbg_async, vha, 0x502f,
313 "Requeuing unknown ATIO_TYPE7 %p\n", u);
314 qlt_24xx_atio_pkt(host, &u->atio, ha_locked);
315 } else if (tgt->tgt_stop) {
316 ql_dbg(ql_dbg_async, vha, 0x503a,
317 "Freeing unknown %s %p, because tgt is being stopped\n",
318 "ATIO_TYPE7", u);
319 qlt_send_term_exchange(vha->hw->base_qpair, NULL,
320 &u->atio, ha_locked, 0);
321 } else {
322 ql_dbg(ql_dbg_async, vha, 0x503d,
323 "Reschedule u %p, vha %p, host %p\n", u, vha, host);
324 if (!queued) {
325 queued = 1;
326 schedule_delayed_work(&vha->unknown_atio_work,
329 continue;
332 abort:
333 spin_lock_irqsave(&vha->cmd_list_lock, flags);
334 list_del(&u->cmd_list);
335 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
336 kfree(u);
340 void qlt_unknown_atio_work_fn(struct work_struct *work)
342 struct scsi_qla_host *vha = container_of(to_delayed_work(work),
343 struct scsi_qla_host, unknown_atio_work);
345 qlt_try_to_dequeue_unknown_atios(vha, 0);
348 static bool qlt_24xx_atio_pkt_all_vps(struct scsi_qla_host *vha,
349 struct atio_from_isp *atio, uint8_t ha_locked)
351 ql_dbg(ql_dbg_tgt, vha, 0xe072,
352 "%s: qla_target(%d): type %x ox_id %04x\n",
353 __func__, vha->vp_idx, atio->u.raw.entry_type,
354 be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id));
356 switch (atio->u.raw.entry_type) {
357 case ATIO_TYPE7:
359 struct scsi_qla_host *host = qlt_find_host_by_d_id(vha,
360 atio->u.isp24.fcp_hdr.d_id);
361 if (unlikely(NULL == host)) {
362 ql_dbg(ql_dbg_tgt, vha, 0xe03e,
363 "qla_target(%d): Received ATIO_TYPE7 "
364 "with unknown d_id %x:%x:%x\n", vha->vp_idx,
365 atio->u.isp24.fcp_hdr.d_id[0],
366 atio->u.isp24.fcp_hdr.d_id[1],
367 atio->u.isp24.fcp_hdr.d_id[2]);
370 qlt_queue_unknown_atio(vha, atio, ha_locked);
371 break;
373 if (unlikely(!list_empty(&vha->unknown_atio_list)))
374 qlt_try_to_dequeue_unknown_atios(vha, ha_locked);
376 qlt_24xx_atio_pkt(host, atio, ha_locked);
377 break;
380 case IMMED_NOTIFY_TYPE:
382 struct scsi_qla_host *host = vha;
383 struct imm_ntfy_from_isp *entry =
384 (struct imm_ntfy_from_isp *)atio;
386 qlt_issue_marker(vha, ha_locked);
388 if ((entry->u.isp24.vp_index != 0xFF) &&
389 (entry->u.isp24.nport_handle != 0xFFFF)) {
390 host = qlt_find_host_by_vp_idx(vha,
391 entry->u.isp24.vp_index);
392 if (unlikely(!host)) {
393 ql_dbg(ql_dbg_tgt, vha, 0xe03f,
394 "qla_target(%d): Received "
395 "ATIO (IMMED_NOTIFY_TYPE) "
396 "with unknown vp_index %d\n",
397 vha->vp_idx, entry->u.isp24.vp_index);
398 break;
401 qlt_24xx_atio_pkt(host, atio, ha_locked);
402 break;
405 case VP_RPT_ID_IOCB_TYPE:
406 qla24xx_report_id_acquisition(vha,
407 (struct vp_rpt_id_entry_24xx *)atio);
408 break;
410 case ABTS_RECV_24XX:
412 struct abts_recv_from_24xx *entry =
413 (struct abts_recv_from_24xx *)atio;
414 struct scsi_qla_host *host = qlt_find_host_by_vp_idx(vha,
415 entry->vp_index);
416 unsigned long flags;
418 if (unlikely(!host)) {
419 ql_dbg(ql_dbg_tgt, vha, 0xe00a,
420 "qla_target(%d): Response pkt (ABTS_RECV_24XX) "
421 "received, with unknown vp_index %d\n",
422 vha->vp_idx, entry->vp_index);
423 break;
425 if (!ha_locked)
426 spin_lock_irqsave(&host->hw->hardware_lock, flags);
427 qlt_24xx_handle_abts(host, (struct abts_recv_from_24xx *)atio);
428 if (!ha_locked)
429 spin_unlock_irqrestore(&host->hw->hardware_lock, flags);
430 break;
433 /* case PUREX_IOCB_TYPE: ql2xmvasynctoatio */
435 default:
436 ql_dbg(ql_dbg_tgt, vha, 0xe040,
437 "qla_target(%d): Received unknown ATIO atio "
438 "type %x\n", vha->vp_idx, atio->u.raw.entry_type);
439 break;
442 return false;
445 void qlt_response_pkt_all_vps(struct scsi_qla_host *vha,
446 struct rsp_que *rsp, response_t *pkt)
448 switch (pkt->entry_type) {
449 case CTIO_CRC2:
450 ql_dbg(ql_dbg_tgt, vha, 0xe073,
451 "qla_target(%d):%s: CRC2 Response pkt\n",
452 vha->vp_idx, __func__);
453 case CTIO_TYPE7:
455 struct ctio7_from_24xx *entry = (struct ctio7_from_24xx *)pkt;
456 struct scsi_qla_host *host = qlt_find_host_by_vp_idx(vha,
457 entry->vp_index);
458 if (unlikely(!host)) {
459 ql_dbg(ql_dbg_tgt, vha, 0xe041,
460 "qla_target(%d): Response pkt (CTIO_TYPE7) "
461 "received, with unknown vp_index %d\n",
462 vha->vp_idx, entry->vp_index);
463 break;
465 qlt_response_pkt(host, rsp, pkt);
466 break;
469 case IMMED_NOTIFY_TYPE:
471 struct scsi_qla_host *host = vha;
472 struct imm_ntfy_from_isp *entry =
473 (struct imm_ntfy_from_isp *)pkt;
475 host = qlt_find_host_by_vp_idx(vha, entry->u.isp24.vp_index);
476 if (unlikely(!host)) {
477 ql_dbg(ql_dbg_tgt, vha, 0xe042,
478 "qla_target(%d): Response pkt (IMMED_NOTIFY_TYPE) "
479 "received, with unknown vp_index %d\n",
480 vha->vp_idx, entry->u.isp24.vp_index);
481 break;
483 qlt_response_pkt(host, rsp, pkt);
484 break;
487 case NOTIFY_ACK_TYPE:
489 struct scsi_qla_host *host = vha;
490 struct nack_to_isp *entry = (struct nack_to_isp *)pkt;
492 if (0xFF != entry->u.isp24.vp_index) {
493 host = qlt_find_host_by_vp_idx(vha,
494 entry->u.isp24.vp_index);
495 if (unlikely(!host)) {
496 ql_dbg(ql_dbg_tgt, vha, 0xe043,
497 "qla_target(%d): Response "
498 "pkt (NOTIFY_ACK_TYPE) "
499 "received, with unknown "
500 "vp_index %d\n", vha->vp_idx,
501 entry->u.isp24.vp_index);
502 break;
505 qlt_response_pkt(host, rsp, pkt);
506 break;
509 case ABTS_RECV_24XX:
511 struct abts_recv_from_24xx *entry =
512 (struct abts_recv_from_24xx *)pkt;
513 struct scsi_qla_host *host = qlt_find_host_by_vp_idx(vha,
514 entry->vp_index);
515 if (unlikely(!host)) {
516 ql_dbg(ql_dbg_tgt, vha, 0xe044,
517 "qla_target(%d): Response pkt "
518 "(ABTS_RECV_24XX) received, with unknown "
519 "vp_index %d\n", vha->vp_idx, entry->vp_index);
520 break;
522 qlt_response_pkt(host, rsp, pkt);
523 break;
526 case ABTS_RESP_24XX:
528 struct abts_resp_to_24xx *entry =
529 (struct abts_resp_to_24xx *)pkt;
530 struct scsi_qla_host *host = qlt_find_host_by_vp_idx(vha,
531 entry->vp_index);
532 if (unlikely(!host)) {
533 ql_dbg(ql_dbg_tgt, vha, 0xe045,
534 "qla_target(%d): Response pkt "
535 "(ABTS_RECV_24XX) received, with unknown "
536 "vp_index %d\n", vha->vp_idx, entry->vp_index);
537 break;
539 qlt_response_pkt(host, rsp, pkt);
540 break;
543 default:
544 qlt_response_pkt(vha, rsp, pkt);
545 break;
551 * All qlt_plogi_ack_t operations are protected by hardware_lock
553 static int qla24xx_post_nack_work(struct scsi_qla_host *vha, fc_port_t *fcport,
554 struct imm_ntfy_from_isp *ntfy, int type)
556 struct qla_work_evt *e;
557 e = qla2x00_alloc_work(vha, QLA_EVT_NACK);
558 if (!e)
559 return QLA_FUNCTION_FAILED;
561 e->u.nack.fcport = fcport;
562 e->u.nack.type = type;
563 memcpy(e->u.nack.iocb, ntfy, sizeof(struct imm_ntfy_from_isp));
564 return qla2x00_post_work(vha, e);
567 static
568 void qla2x00_async_nack_sp_done(void *s, int res)
570 struct srb *sp = (struct srb *)s;
571 struct scsi_qla_host *vha = sp->vha;
572 unsigned long flags;
574 ql_dbg(ql_dbg_disc, vha, 0x20f2,
575 "Async done-%s res %x %8phC type %d\n",
576 sp->name, res, sp->fcport->port_name, sp->type);
578 spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
579 sp->fcport->flags &= ~FCF_ASYNC_SENT;
580 sp->fcport->chip_reset = vha->hw->base_qpair->chip_reset;
582 switch (sp->type) {
583 case SRB_NACK_PLOGI:
584 sp->fcport->login_gen++;
585 sp->fcport->fw_login_state = DSC_LS_PLOGI_COMP;
586 sp->fcport->logout_on_delete = 1;
587 sp->fcport->plogi_nack_done_deadline = jiffies + HZ;
588 break;
590 case SRB_NACK_PRLI:
591 sp->fcport->fw_login_state = DSC_LS_PRLI_COMP;
592 sp->fcport->deleted = 0;
594 if (!sp->fcport->login_succ &&
595 !IS_SW_RESV_ADDR(sp->fcport->d_id)) {
596 sp->fcport->login_succ = 1;
598 vha->fcport_count++;
600 if (!IS_IIDMA_CAPABLE(vha->hw) ||
601 !vha->hw->flags.gpsc_supported) {
602 ql_dbg(ql_dbg_disc, vha, 0x20f3,
603 "%s %d %8phC post upd_fcport fcp_cnt %d\n",
604 __func__, __LINE__,
605 sp->fcport->port_name,
606 vha->fcport_count);
608 qla24xx_post_upd_fcport_work(vha, sp->fcport);
609 } else {
610 ql_dbg(ql_dbg_disc, vha, 0x20f5,
611 "%s %d %8phC post gpsc fcp_cnt %d\n",
612 __func__, __LINE__,
613 sp->fcport->port_name,
614 vha->fcport_count);
616 qla24xx_post_gpsc_work(vha, sp->fcport);
619 break;
621 case SRB_NACK_LOGO:
622 sp->fcport->login_gen++;
623 sp->fcport->fw_login_state = DSC_LS_PORT_UNAVAIL;
624 qlt_logo_completion_handler(sp->fcport, MBS_COMMAND_COMPLETE);
625 break;
627 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
629 sp->free(sp);
632 int qla24xx_async_notify_ack(scsi_qla_host_t *vha, fc_port_t *fcport,
633 struct imm_ntfy_from_isp *ntfy, int type)
635 int rval = QLA_FUNCTION_FAILED;
636 srb_t *sp;
637 char *c = NULL;
639 fcport->flags |= FCF_ASYNC_SENT;
640 switch (type) {
641 case SRB_NACK_PLOGI:
642 fcport->fw_login_state = DSC_LS_PLOGI_PEND;
643 c = "PLOGI";
644 break;
645 case SRB_NACK_PRLI:
646 fcport->fw_login_state = DSC_LS_PRLI_PEND;
647 fcport->deleted = 0;
648 c = "PRLI";
649 break;
650 case SRB_NACK_LOGO:
651 fcport->fw_login_state = DSC_LS_LOGO_PEND;
652 c = "LOGO";
653 break;
656 sp = qla2x00_get_sp(vha, fcport, GFP_ATOMIC);
657 if (!sp)
658 goto done;
660 sp->type = type;
661 sp->name = "nack";
663 qla2x00_init_timer(sp, qla2x00_get_async_timeout(vha)+2);
665 sp->u.iocb_cmd.u.nack.ntfy = ntfy;
667 sp->done = qla2x00_async_nack_sp_done;
669 rval = qla2x00_start_sp(sp);
670 if (rval != QLA_SUCCESS)
671 goto done_free_sp;
673 ql_dbg(ql_dbg_disc, vha, 0x20f4,
674 "Async-%s %8phC hndl %x %s\n",
675 sp->name, fcport->port_name, sp->handle, c);
677 return rval;
679 done_free_sp:
680 sp->free(sp);
681 done:
682 fcport->flags &= ~FCF_ASYNC_SENT;
683 return rval;
686 void qla24xx_do_nack_work(struct scsi_qla_host *vha, struct qla_work_evt *e)
688 fc_port_t *t;
689 unsigned long flags;
691 switch (e->u.nack.type) {
692 case SRB_NACK_PRLI:
693 mutex_lock(&vha->vha_tgt.tgt_mutex);
694 t = qlt_create_sess(vha, e->u.nack.fcport, 0);
695 mutex_unlock(&vha->vha_tgt.tgt_mutex);
696 if (t) {
697 ql_log(ql_log_info, vha, 0xd034,
698 "%s create sess success %p", __func__, t);
699 spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
700 /* create sess has an extra kref */
701 vha->hw->tgt.tgt_ops->put_sess(e->u.nack.fcport);
702 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
704 break;
706 qla24xx_async_notify_ack(vha, e->u.nack.fcport,
707 (struct imm_ntfy_from_isp*)e->u.nack.iocb, e->u.nack.type);
710 void qla24xx_delete_sess_fn(struct work_struct *work)
712 fc_port_t *fcport = container_of(work, struct fc_port, del_work);
713 struct qla_hw_data *ha = fcport->vha->hw;
714 unsigned long flags;
716 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
718 if (fcport->se_sess) {
719 ha->tgt.tgt_ops->shutdown_sess(fcport);
720 ha->tgt.tgt_ops->put_sess(fcport);
721 } else {
722 qlt_unreg_sess(fcport);
724 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
728 * Called from qla2x00_reg_remote_port()
730 void qlt_fc_port_added(struct scsi_qla_host *vha, fc_port_t *fcport)
732 struct qla_hw_data *ha = vha->hw;
733 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
734 struct fc_port *sess = fcport;
735 unsigned long flags;
737 if (!vha->hw->tgt.tgt_ops)
738 return;
740 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
741 if (tgt->tgt_stop) {
742 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
743 return;
746 if (fcport->disc_state == DSC_DELETE_PEND) {
747 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
748 return;
751 if (!sess->se_sess) {
752 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
754 mutex_lock(&vha->vha_tgt.tgt_mutex);
755 sess = qlt_create_sess(vha, fcport, false);
756 mutex_unlock(&vha->vha_tgt.tgt_mutex);
758 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
759 } else {
760 if (fcport->fw_login_state == DSC_LS_PRLI_COMP) {
761 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
762 return;
765 if (!kref_get_unless_zero(&sess->sess_kref)) {
766 ql_dbg(ql_dbg_disc, vha, 0x2107,
767 "%s: kref_get fail sess %8phC \n",
768 __func__, sess->port_name);
769 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
770 return;
773 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04c,
774 "qla_target(%u): %ssession for port %8phC "
775 "(loop ID %d) reappeared\n", vha->vp_idx,
776 sess->local ? "local " : "", sess->port_name, sess->loop_id);
778 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf007,
779 "Reappeared sess %p\n", sess);
781 ha->tgt.tgt_ops->update_sess(sess, fcport->d_id,
782 fcport->loop_id,
783 (fcport->flags & FCF_CONF_COMP_SUPPORTED));
786 if (sess && sess->local) {
787 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04d,
788 "qla_target(%u): local session for "
789 "port %8phC (loop ID %d) became global\n", vha->vp_idx,
790 fcport->port_name, sess->loop_id);
791 sess->local = 0;
793 ha->tgt.tgt_ops->put_sess(sess);
794 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
798 * This is a zero-base ref-counting solution, since hardware_lock
799 * guarantees that ref_count is not modified concurrently.
800 * Upon successful return content of iocb is undefined
802 static struct qlt_plogi_ack_t *
803 qlt_plogi_ack_find_add(struct scsi_qla_host *vha, port_id_t *id,
804 struct imm_ntfy_from_isp *iocb)
806 struct qlt_plogi_ack_t *pla;
808 list_for_each_entry(pla, &vha->plogi_ack_list, list) {
809 if (pla->id.b24 == id->b24) {
810 qlt_send_term_imm_notif(vha, &pla->iocb, 1);
811 memcpy(&pla->iocb, iocb, sizeof(pla->iocb));
812 return pla;
816 pla = kmem_cache_zalloc(qla_tgt_plogi_cachep, GFP_ATOMIC);
817 if (!pla) {
818 ql_dbg(ql_dbg_async, vha, 0x5088,
819 "qla_target(%d): Allocation of plogi_ack failed\n",
820 vha->vp_idx);
821 return NULL;
824 memcpy(&pla->iocb, iocb, sizeof(pla->iocb));
825 pla->id = *id;
826 list_add_tail(&pla->list, &vha->plogi_ack_list);
828 return pla;
831 void qlt_plogi_ack_unref(struct scsi_qla_host *vha,
832 struct qlt_plogi_ack_t *pla)
834 struct imm_ntfy_from_isp *iocb = &pla->iocb;
835 port_id_t port_id;
836 uint16_t loop_id;
837 fc_port_t *fcport = pla->fcport;
839 BUG_ON(!pla->ref_count);
840 pla->ref_count--;
842 if (pla->ref_count)
843 return;
845 ql_dbg(ql_dbg_disc, vha, 0x5089,
846 "Sending PLOGI ACK to wwn %8phC s_id %02x:%02x:%02x loop_id %#04x"
847 " exch %#x ox_id %#x\n", iocb->u.isp24.port_name,
848 iocb->u.isp24.port_id[2], iocb->u.isp24.port_id[1],
849 iocb->u.isp24.port_id[0],
850 le16_to_cpu(iocb->u.isp24.nport_handle),
851 iocb->u.isp24.exchange_address, iocb->ox_id);
853 port_id.b.domain = iocb->u.isp24.port_id[2];
854 port_id.b.area = iocb->u.isp24.port_id[1];
855 port_id.b.al_pa = iocb->u.isp24.port_id[0];
856 port_id.b.rsvd_1 = 0;
858 loop_id = le16_to_cpu(iocb->u.isp24.nport_handle);
860 fcport->loop_id = loop_id;
861 fcport->d_id = port_id;
862 qla24xx_post_nack_work(vha, fcport, iocb, SRB_NACK_PLOGI);
864 list_for_each_entry(fcport, &vha->vp_fcports, list) {
865 if (fcport->plogi_link[QLT_PLOGI_LINK_SAME_WWN] == pla)
866 fcport->plogi_link[QLT_PLOGI_LINK_SAME_WWN] = NULL;
867 if (fcport->plogi_link[QLT_PLOGI_LINK_CONFLICT] == pla)
868 fcport->plogi_link[QLT_PLOGI_LINK_CONFLICT] = NULL;
871 list_del(&pla->list);
872 kmem_cache_free(qla_tgt_plogi_cachep, pla);
875 void
876 qlt_plogi_ack_link(struct scsi_qla_host *vha, struct qlt_plogi_ack_t *pla,
877 struct fc_port *sess, enum qlt_plogi_link_t link)
879 struct imm_ntfy_from_isp *iocb = &pla->iocb;
880 /* Inc ref_count first because link might already be pointing at pla */
881 pla->ref_count++;
883 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf097,
884 "Linking sess %p [%d] wwn %8phC with PLOGI ACK to wwn %8phC"
885 " s_id %02x:%02x:%02x, ref=%d pla %p link %d\n",
886 sess, link, sess->port_name,
887 iocb->u.isp24.port_name, iocb->u.isp24.port_id[2],
888 iocb->u.isp24.port_id[1], iocb->u.isp24.port_id[0],
889 pla->ref_count, pla, link);
891 if (sess->plogi_link[link])
892 qlt_plogi_ack_unref(vha, sess->plogi_link[link]);
894 if (link == QLT_PLOGI_LINK_SAME_WWN)
895 pla->fcport = sess;
897 sess->plogi_link[link] = pla;
900 typedef struct {
901 /* These fields must be initialized by the caller */
902 port_id_t id;
904 * number of cmds dropped while we were waiting for
905 * initiator to ack LOGO initialize to 1 if LOGO is
906 * triggered by a command, otherwise, to 0
908 int cmd_count;
910 /* These fields are used by callee */
911 struct list_head list;
912 } qlt_port_logo_t;
914 static void
915 qlt_send_first_logo(struct scsi_qla_host *vha, qlt_port_logo_t *logo)
917 qlt_port_logo_t *tmp;
918 int res;
920 mutex_lock(&vha->vha_tgt.tgt_mutex);
922 list_for_each_entry(tmp, &vha->logo_list, list) {
923 if (tmp->id.b24 == logo->id.b24) {
924 tmp->cmd_count += logo->cmd_count;
925 mutex_unlock(&vha->vha_tgt.tgt_mutex);
926 return;
930 list_add_tail(&logo->list, &vha->logo_list);
932 mutex_unlock(&vha->vha_tgt.tgt_mutex);
934 res = qla24xx_els_dcmd_iocb(vha, ELS_DCMD_LOGO, logo->id);
936 mutex_lock(&vha->vha_tgt.tgt_mutex);
937 list_del(&logo->list);
938 mutex_unlock(&vha->vha_tgt.tgt_mutex);
940 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf098,
941 "Finished LOGO to %02x:%02x:%02x, dropped %d cmds, res = %#x\n",
942 logo->id.b.domain, logo->id.b.area, logo->id.b.al_pa,
943 logo->cmd_count, res);
946 static void qlt_free_session_done(struct work_struct *work)
948 struct fc_port *sess = container_of(work, struct fc_port,
949 free_work);
950 struct qla_tgt *tgt = sess->tgt;
951 struct scsi_qla_host *vha = sess->vha;
952 struct qla_hw_data *ha = vha->hw;
953 unsigned long flags;
954 bool logout_started = false;
955 struct event_arg ea;
956 scsi_qla_host_t *base_vha;
958 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf084,
959 "%s: se_sess %p / sess %p from port %8phC loop_id %#04x"
960 " s_id %02x:%02x:%02x logout %d keep %d els_logo %d\n",
961 __func__, sess->se_sess, sess, sess->port_name, sess->loop_id,
962 sess->d_id.b.domain, sess->d_id.b.area, sess->d_id.b.al_pa,
963 sess->logout_on_delete, sess->keep_nport_handle,
964 sess->send_els_logo);
966 if (!IS_SW_RESV_ADDR(sess->d_id)) {
967 if (sess->send_els_logo) {
968 qlt_port_logo_t logo;
970 logo.id = sess->d_id;
971 logo.cmd_count = 0;
972 qlt_send_first_logo(vha, &logo);
975 if (sess->logout_on_delete) {
976 int rc;
978 rc = qla2x00_post_async_logout_work(vha, sess, NULL);
979 if (rc != QLA_SUCCESS)
980 ql_log(ql_log_warn, vha, 0xf085,
981 "Schedule logo failed sess %p rc %d\n",
982 sess, rc);
983 else
984 logout_started = true;
989 * Release the target session for FC Nexus from fabric module code.
991 if (sess->se_sess != NULL)
992 ha->tgt.tgt_ops->free_session(sess);
994 if (logout_started) {
995 bool traced = false;
997 while (!ACCESS_ONCE(sess->logout_completed)) {
998 if (!traced) {
999 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf086,
1000 "%s: waiting for sess %p logout\n",
1001 __func__, sess);
1002 traced = true;
1004 msleep(100);
1007 ql_dbg(ql_dbg_disc, vha, 0xf087,
1008 "%s: sess %p logout completed\n",__func__, sess);
1011 if (sess->logo_ack_needed) {
1012 sess->logo_ack_needed = 0;
1013 qla24xx_async_notify_ack(vha, sess,
1014 (struct imm_ntfy_from_isp *)sess->iocb, SRB_NACK_LOGO);
1017 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
1018 if (sess->se_sess) {
1019 sess->se_sess = NULL;
1020 if (tgt && !IS_SW_RESV_ADDR(sess->d_id))
1021 tgt->sess_count--;
1024 sess->disc_state = DSC_DELETED;
1025 sess->fw_login_state = DSC_LS_PORT_UNAVAIL;
1026 sess->deleted = QLA_SESS_DELETED;
1027 sess->login_retry = vha->hw->login_retry_count;
1029 if (sess->login_succ && !IS_SW_RESV_ADDR(sess->d_id)) {
1030 vha->fcport_count--;
1031 sess->login_succ = 0;
1034 if (sess->chip_reset != ha->base_qpair->chip_reset)
1035 qla2x00_clear_loop_id(sess);
1037 if (sess->conflict) {
1038 sess->conflict->login_pause = 0;
1039 sess->conflict = NULL;
1040 if (!test_bit(UNLOADING, &vha->dpc_flags))
1041 set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
1045 struct qlt_plogi_ack_t *own =
1046 sess->plogi_link[QLT_PLOGI_LINK_SAME_WWN];
1047 struct qlt_plogi_ack_t *con =
1048 sess->plogi_link[QLT_PLOGI_LINK_CONFLICT];
1049 struct imm_ntfy_from_isp *iocb;
1051 if (con) {
1052 iocb = &con->iocb;
1053 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf099,
1054 "se_sess %p / sess %p port %8phC is gone,"
1055 " %s (ref=%d), releasing PLOGI for %8phC (ref=%d)\n",
1056 sess->se_sess, sess, sess->port_name,
1057 own ? "releasing own PLOGI" : "no own PLOGI pending",
1058 own ? own->ref_count : -1,
1059 iocb->u.isp24.port_name, con->ref_count);
1060 qlt_plogi_ack_unref(vha, con);
1061 sess->plogi_link[QLT_PLOGI_LINK_CONFLICT] = NULL;
1062 } else {
1063 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf09a,
1064 "se_sess %p / sess %p port %8phC is gone, %s (ref=%d)\n",
1065 sess->se_sess, sess, sess->port_name,
1066 own ? "releasing own PLOGI" :
1067 "no own PLOGI pending",
1068 own ? own->ref_count : -1);
1071 if (own) {
1072 sess->fw_login_state = DSC_LS_PLOGI_PEND;
1073 qlt_plogi_ack_unref(vha, own);
1074 sess->plogi_link[QLT_PLOGI_LINK_SAME_WWN] = NULL;
1077 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
1079 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf001,
1080 "Unregistration of sess %p %8phC finished fcp_cnt %d\n",
1081 sess, sess->port_name, vha->fcport_count);
1083 if (tgt && (tgt->sess_count == 0))
1084 wake_up_all(&tgt->waitQ);
1086 if (vha->fcport_count == 0)
1087 wake_up_all(&vha->fcport_waitQ);
1089 base_vha = pci_get_drvdata(ha->pdev);
1090 if (test_bit(PFLG_DRIVER_REMOVING, &base_vha->pci_flags))
1091 return;
1093 if (!tgt || !tgt->tgt_stop) {
1094 memset(&ea, 0, sizeof(ea));
1095 ea.event = FCME_DELETE_DONE;
1096 ea.fcport = sess;
1097 qla2x00_fcport_event_handler(vha, &ea);
1101 /* ha->tgt.sess_lock supposed to be held on entry */
1102 void qlt_unreg_sess(struct fc_port *sess)
1104 struct scsi_qla_host *vha = sess->vha;
1106 ql_dbg(ql_dbg_disc, sess->vha, 0x210a,
1107 "%s sess %p for deletion %8phC\n",
1108 __func__, sess, sess->port_name);
1110 if (sess->se_sess)
1111 vha->hw->tgt.tgt_ops->clear_nacl_from_fcport_map(sess);
1113 qla2x00_mark_device_lost(vha, sess, 1, 1);
1115 sess->deleted = QLA_SESS_DELETION_IN_PROGRESS;
1116 sess->disc_state = DSC_DELETE_PEND;
1117 sess->last_rscn_gen = sess->rscn_gen;
1118 sess->last_login_gen = sess->login_gen;
1120 if (sess->nvme_flag & NVME_FLAG_REGISTERED)
1121 schedule_work(&sess->nvme_del_work);
1123 INIT_WORK(&sess->free_work, qlt_free_session_done);
1124 schedule_work(&sess->free_work);
1126 EXPORT_SYMBOL(qlt_unreg_sess);
1128 static int qlt_reset(struct scsi_qla_host *vha, void *iocb, int mcmd)
1130 struct qla_hw_data *ha = vha->hw;
1131 struct fc_port *sess = NULL;
1132 uint16_t loop_id;
1133 int res = 0;
1134 struct imm_ntfy_from_isp *n = (struct imm_ntfy_from_isp *)iocb;
1135 unsigned long flags;
1137 loop_id = le16_to_cpu(n->u.isp24.nport_handle);
1138 if (loop_id == 0xFFFF) {
1139 /* Global event */
1140 atomic_inc(&vha->vha_tgt.qla_tgt->tgt_global_resets_count);
1141 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
1142 qlt_clear_tgt_db(vha->vha_tgt.qla_tgt);
1143 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
1144 } else {
1145 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
1146 sess = ha->tgt.tgt_ops->find_sess_by_loop_id(vha, loop_id);
1147 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
1150 ql_dbg(ql_dbg_tgt, vha, 0xe000,
1151 "Using sess for qla_tgt_reset: %p\n", sess);
1152 if (!sess) {
1153 res = -ESRCH;
1154 return res;
1157 ql_dbg(ql_dbg_tgt, vha, 0xe047,
1158 "scsi(%ld): resetting (session %p from port %8phC mcmd %x, "
1159 "loop_id %d)\n", vha->host_no, sess, sess->port_name,
1160 mcmd, loop_id);
1162 return qlt_issue_task_mgmt(sess, 0, mcmd, iocb, QLA24XX_MGMT_SEND_NACK);
1165 static void qla24xx_chk_fcp_state(struct fc_port *sess)
1167 if (sess->chip_reset != sess->vha->hw->base_qpair->chip_reset) {
1168 sess->logout_on_delete = 0;
1169 sess->logo_ack_needed = 0;
1170 sess->fw_login_state = DSC_LS_PORT_UNAVAIL;
1171 sess->scan_state = 0;
1175 /* ha->tgt.sess_lock supposed to be held on entry */
1176 void qlt_schedule_sess_for_deletion(struct fc_port *sess,
1177 bool immediate)
1179 struct qla_tgt *tgt = sess->tgt;
1181 if (sess->disc_state == DSC_DELETE_PEND)
1182 return;
1184 if (sess->disc_state == DSC_DELETED) {
1185 if (tgt && tgt->tgt_stop && (tgt->sess_count == 0))
1186 wake_up_all(&tgt->waitQ);
1187 if (sess->vha->fcport_count == 0)
1188 wake_up_all(&sess->vha->fcport_waitQ);
1190 if (!sess->plogi_link[QLT_PLOGI_LINK_SAME_WWN] &&
1191 !sess->plogi_link[QLT_PLOGI_LINK_CONFLICT])
1192 return;
1195 sess->disc_state = DSC_DELETE_PEND;
1197 if (sess->deleted == QLA_SESS_DELETED)
1198 sess->logout_on_delete = 0;
1200 sess->deleted = QLA_SESS_DELETION_IN_PROGRESS;
1201 qla24xx_chk_fcp_state(sess);
1203 ql_dbg(ql_dbg_tgt, sess->vha, 0xe001,
1204 "Scheduling sess %p for deletion\n", sess);
1206 schedule_work(&sess->del_work);
1209 void qlt_schedule_sess_for_deletion_lock(struct fc_port *sess)
1211 unsigned long flags;
1212 struct qla_hw_data *ha = sess->vha->hw;
1213 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
1214 qlt_schedule_sess_for_deletion(sess, 1);
1215 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
1218 /* ha->tgt.sess_lock supposed to be held on entry */
1219 static void qlt_clear_tgt_db(struct qla_tgt *tgt)
1221 struct fc_port *sess;
1222 scsi_qla_host_t *vha = tgt->vha;
1224 list_for_each_entry(sess, &vha->vp_fcports, list) {
1225 if (sess->se_sess)
1226 qlt_schedule_sess_for_deletion(sess, 1);
1229 /* At this point tgt could be already dead */
1232 static int qla24xx_get_loop_id(struct scsi_qla_host *vha, const uint8_t *s_id,
1233 uint16_t *loop_id)
1235 struct qla_hw_data *ha = vha->hw;
1236 dma_addr_t gid_list_dma;
1237 struct gid_list_info *gid_list;
1238 char *id_iter;
1239 int res, rc, i;
1240 uint16_t entries;
1242 gid_list = dma_alloc_coherent(&ha->pdev->dev, qla2x00_gid_list_size(ha),
1243 &gid_list_dma, GFP_KERNEL);
1244 if (!gid_list) {
1245 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf044,
1246 "qla_target(%d): DMA Alloc failed of %u\n",
1247 vha->vp_idx, qla2x00_gid_list_size(ha));
1248 return -ENOMEM;
1251 /* Get list of logged in devices */
1252 rc = qla24xx_gidlist_wait(vha, gid_list, gid_list_dma, &entries);
1253 if (rc != QLA_SUCCESS) {
1254 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf045,
1255 "qla_target(%d): get_id_list() failed: %x\n",
1256 vha->vp_idx, rc);
1257 res = -EBUSY;
1258 goto out_free_id_list;
1261 id_iter = (char *)gid_list;
1262 res = -ENOENT;
1263 for (i = 0; i < entries; i++) {
1264 struct gid_list_info *gid = (struct gid_list_info *)id_iter;
1265 if ((gid->al_pa == s_id[2]) &&
1266 (gid->area == s_id[1]) &&
1267 (gid->domain == s_id[0])) {
1268 *loop_id = le16_to_cpu(gid->loop_id);
1269 res = 0;
1270 break;
1272 id_iter += ha->gid_list_info_size;
1275 out_free_id_list:
1276 dma_free_coherent(&ha->pdev->dev, qla2x00_gid_list_size(ha),
1277 gid_list, gid_list_dma);
1278 return res;
1282 * Adds an extra ref to allow to drop hw lock after adding sess to the list.
1283 * Caller must put it.
1285 static struct fc_port *qlt_create_sess(
1286 struct scsi_qla_host *vha,
1287 fc_port_t *fcport,
1288 bool local)
1290 struct qla_hw_data *ha = vha->hw;
1291 struct fc_port *sess = fcport;
1292 unsigned long flags;
1294 if (vha->vha_tgt.qla_tgt->tgt_stop)
1295 return NULL;
1297 if (fcport->se_sess) {
1298 if (!kref_get_unless_zero(&sess->sess_kref)) {
1299 ql_dbg(ql_dbg_disc, vha, 0x20f6,
1300 "%s: kref_get_unless_zero failed for %8phC\n",
1301 __func__, sess->port_name);
1302 return NULL;
1304 return fcport;
1306 sess->tgt = vha->vha_tgt.qla_tgt;
1307 sess->local = local;
1310 * Under normal circumstances we want to logout from firmware when
1311 * session eventually ends and release corresponding nport handle.
1312 * In the exception cases (e.g. when new PLOGI is waiting) corresponding
1313 * code will adjust these flags as necessary.
1315 sess->logout_on_delete = 1;
1316 sess->keep_nport_handle = 0;
1317 sess->logout_completed = 0;
1319 if (ha->tgt.tgt_ops->check_initiator_node_acl(vha,
1320 &fcport->port_name[0], sess) < 0) {
1321 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf015,
1322 "(%d) %8phC check_initiator_node_acl failed\n",
1323 vha->vp_idx, fcport->port_name);
1324 return NULL;
1325 } else {
1326 kref_init(&fcport->sess_kref);
1328 * Take an extra reference to ->sess_kref here to handle
1329 * fc_port access across ->tgt.sess_lock reaquire.
1331 if (!kref_get_unless_zero(&sess->sess_kref)) {
1332 ql_dbg(ql_dbg_disc, vha, 0x20f7,
1333 "%s: kref_get_unless_zero failed for %8phC\n",
1334 __func__, sess->port_name);
1335 return NULL;
1338 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
1339 if (!IS_SW_RESV_ADDR(sess->d_id))
1340 vha->vha_tgt.qla_tgt->sess_count++;
1342 qlt_do_generation_tick(vha, &sess->generation);
1343 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
1346 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf006,
1347 "Adding sess %p se_sess %p to tgt %p sess_count %d\n",
1348 sess, sess->se_sess, vha->vha_tgt.qla_tgt,
1349 vha->vha_tgt.qla_tgt->sess_count);
1351 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04b,
1352 "qla_target(%d): %ssession for wwn %8phC (loop_id %d, "
1353 "s_id %x:%x:%x, confirmed completion %ssupported) added\n",
1354 vha->vp_idx, local ? "local " : "", fcport->port_name,
1355 fcport->loop_id, sess->d_id.b.domain, sess->d_id.b.area,
1356 sess->d_id.b.al_pa, sess->conf_compl_supported ? "" : "not ");
1358 return sess;
1362 * max_gen - specifies maximum session generation
1363 * at which this deletion requestion is still valid
1365 void
1366 qlt_fc_port_deleted(struct scsi_qla_host *vha, fc_port_t *fcport, int max_gen)
1368 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
1369 struct fc_port *sess = fcport;
1370 unsigned long flags;
1372 if (!vha->hw->tgt.tgt_ops)
1373 return;
1375 if (!tgt)
1376 return;
1378 spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
1379 if (tgt->tgt_stop) {
1380 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
1381 return;
1383 if (!sess->se_sess) {
1384 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
1385 return;
1388 if (max_gen - sess->generation < 0) {
1389 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
1390 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf092,
1391 "Ignoring stale deletion request for se_sess %p / sess %p"
1392 " for port %8phC, req_gen %d, sess_gen %d\n",
1393 sess->se_sess, sess, sess->port_name, max_gen,
1394 sess->generation);
1395 return;
1398 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf008, "qla_tgt_fc_port_deleted %p", sess);
1400 sess->local = 1;
1401 qlt_schedule_sess_for_deletion(sess, false);
1402 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
1405 static inline int test_tgt_sess_count(struct qla_tgt *tgt)
1407 struct qla_hw_data *ha = tgt->ha;
1408 unsigned long flags;
1409 int res;
1411 * We need to protect against race, when tgt is freed before or
1412 * inside wake_up()
1414 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
1415 ql_dbg(ql_dbg_tgt, tgt->vha, 0xe002,
1416 "tgt %p, sess_count=%d\n",
1417 tgt, tgt->sess_count);
1418 res = (tgt->sess_count == 0);
1419 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
1421 return res;
1424 /* Called by tcm_qla2xxx configfs code */
1425 int qlt_stop_phase1(struct qla_tgt *tgt)
1427 struct scsi_qla_host *vha = tgt->vha;
1428 struct qla_hw_data *ha = tgt->ha;
1429 unsigned long flags;
1431 mutex_lock(&qla_tgt_mutex);
1432 if (!vha->fc_vport) {
1433 struct Scsi_Host *sh = vha->host;
1434 struct fc_host_attrs *fc_host = shost_to_fc_host(sh);
1435 bool npiv_vports;
1437 spin_lock_irqsave(sh->host_lock, flags);
1438 npiv_vports = (fc_host->npiv_vports_inuse);
1439 spin_unlock_irqrestore(sh->host_lock, flags);
1441 if (npiv_vports) {
1442 mutex_unlock(&qla_tgt_mutex);
1443 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf021,
1444 "NPIV is in use. Can not stop target\n");
1445 return -EPERM;
1448 if (tgt->tgt_stop || tgt->tgt_stopped) {
1449 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04e,
1450 "Already in tgt->tgt_stop or tgt_stopped state\n");
1451 mutex_unlock(&qla_tgt_mutex);
1452 return -EPERM;
1455 ql_dbg(ql_dbg_tgt_mgt, vha, 0xe003, "Stopping target for host %ld(%p)\n",
1456 vha->host_no, vha);
1458 * Mutex needed to sync with qla_tgt_fc_port_[added,deleted].
1459 * Lock is needed, because we still can get an incoming packet.
1461 mutex_lock(&vha->vha_tgt.tgt_mutex);
1462 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
1463 tgt->tgt_stop = 1;
1464 qlt_clear_tgt_db(tgt);
1465 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
1466 mutex_unlock(&vha->vha_tgt.tgt_mutex);
1467 mutex_unlock(&qla_tgt_mutex);
1469 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf009,
1470 "Waiting for sess works (tgt %p)", tgt);
1471 spin_lock_irqsave(&tgt->sess_work_lock, flags);
1472 while (!list_empty(&tgt->sess_works_list)) {
1473 spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
1474 flush_scheduled_work();
1475 spin_lock_irqsave(&tgt->sess_work_lock, flags);
1477 spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
1479 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00a,
1480 "Waiting for tgt %p: sess_count=%d\n", tgt, tgt->sess_count);
1482 wait_event(tgt->waitQ, test_tgt_sess_count(tgt));
1484 /* Big hammer */
1485 if (!ha->flags.host_shutting_down &&
1486 (qla_tgt_mode_enabled(vha) || qla_dual_mode_enabled(vha)))
1487 qlt_disable_vha(vha);
1489 /* Wait for sessions to clear out (just in case) */
1490 wait_event(tgt->waitQ, test_tgt_sess_count(tgt));
1491 return 0;
1493 EXPORT_SYMBOL(qlt_stop_phase1);
1495 /* Called by tcm_qla2xxx configfs code */
1496 void qlt_stop_phase2(struct qla_tgt *tgt)
1498 scsi_qla_host_t *vha = tgt->vha;
1500 if (tgt->tgt_stopped) {
1501 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04f,
1502 "Already in tgt->tgt_stopped state\n");
1503 dump_stack();
1504 return;
1506 if (!tgt->tgt_stop) {
1507 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00b,
1508 "%s: phase1 stop is not completed\n", __func__);
1509 dump_stack();
1510 return;
1513 mutex_lock(&vha->vha_tgt.tgt_mutex);
1514 tgt->tgt_stop = 0;
1515 tgt->tgt_stopped = 1;
1516 mutex_unlock(&vha->vha_tgt.tgt_mutex);
1518 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00c, "Stop of tgt %p finished\n",
1519 tgt);
1521 EXPORT_SYMBOL(qlt_stop_phase2);
1523 /* Called from qlt_remove_target() -> qla2x00_remove_one() */
1524 static void qlt_release(struct qla_tgt *tgt)
1526 scsi_qla_host_t *vha = tgt->vha;
1527 void *node;
1528 u64 key = 0;
1529 u16 i;
1530 struct qla_qpair_hint *h;
1532 if ((vha->vha_tgt.qla_tgt != NULL) && !tgt->tgt_stop &&
1533 !tgt->tgt_stopped)
1534 qlt_stop_phase1(tgt);
1536 if ((vha->vha_tgt.qla_tgt != NULL) && !tgt->tgt_stopped)
1537 qlt_stop_phase2(tgt);
1539 for (i = 0; i < vha->hw->max_qpairs + 1; i++) {
1540 unsigned long flags;
1542 h = &tgt->qphints[i];
1543 if (h->qpair) {
1544 spin_lock_irqsave(h->qpair->qp_lock_ptr, flags);
1545 list_del(&h->hint_elem);
1546 spin_unlock_irqrestore(h->qpair->qp_lock_ptr, flags);
1547 h->qpair = NULL;
1550 kfree(tgt->qphints);
1552 btree_for_each_safe64(&tgt->lun_qpair_map, key, node)
1553 btree_remove64(&tgt->lun_qpair_map, key);
1555 btree_destroy64(&tgt->lun_qpair_map);
1557 vha->vha_tgt.qla_tgt = NULL;
1559 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00d,
1560 "Release of tgt %p finished\n", tgt);
1562 kfree(tgt);
1565 /* ha->hardware_lock supposed to be held on entry */
1566 static int qlt_sched_sess_work(struct qla_tgt *tgt, int type,
1567 const void *param, unsigned int param_size)
1569 struct qla_tgt_sess_work_param *prm;
1570 unsigned long flags;
1572 prm = kzalloc(sizeof(*prm), GFP_ATOMIC);
1573 if (!prm) {
1574 ql_dbg(ql_dbg_tgt_mgt, tgt->vha, 0xf050,
1575 "qla_target(%d): Unable to create session "
1576 "work, command will be refused", 0);
1577 return -ENOMEM;
1580 ql_dbg(ql_dbg_tgt_mgt, tgt->vha, 0xf00e,
1581 "Scheduling work (type %d, prm %p)"
1582 " to find session for param %p (size %d, tgt %p)\n",
1583 type, prm, param, param_size, tgt);
1585 prm->type = type;
1586 memcpy(&prm->tm_iocb, param, param_size);
1588 spin_lock_irqsave(&tgt->sess_work_lock, flags);
1589 list_add_tail(&prm->sess_works_list_entry, &tgt->sess_works_list);
1590 spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
1592 schedule_work(&tgt->sess_work);
1594 return 0;
1598 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
1600 static void qlt_send_notify_ack(struct qla_qpair *qpair,
1601 struct imm_ntfy_from_isp *ntfy,
1602 uint32_t add_flags, uint16_t resp_code, int resp_code_valid,
1603 uint16_t srr_flags, uint16_t srr_reject_code, uint8_t srr_explan)
1605 struct scsi_qla_host *vha = qpair->vha;
1606 struct qla_hw_data *ha = vha->hw;
1607 request_t *pkt;
1608 struct nack_to_isp *nack;
1610 if (!ha->flags.fw_started)
1611 return;
1613 ql_dbg(ql_dbg_tgt, vha, 0xe004, "Sending NOTIFY_ACK (ha=%p)\n", ha);
1615 pkt = (request_t *)__qla2x00_alloc_iocbs(qpair, NULL);
1616 if (!pkt) {
1617 ql_dbg(ql_dbg_tgt, vha, 0xe049,
1618 "qla_target(%d): %s failed: unable to allocate "
1619 "request packet\n", vha->vp_idx, __func__);
1620 return;
1623 if (vha->vha_tgt.qla_tgt != NULL)
1624 vha->vha_tgt.qla_tgt->notify_ack_expected++;
1626 pkt->entry_type = NOTIFY_ACK_TYPE;
1627 pkt->entry_count = 1;
1629 nack = (struct nack_to_isp *)pkt;
1630 nack->ox_id = ntfy->ox_id;
1632 nack->u.isp24.handle = QLA_TGT_SKIP_HANDLE;
1633 nack->u.isp24.nport_handle = ntfy->u.isp24.nport_handle;
1634 if (le16_to_cpu(ntfy->u.isp24.status) == IMM_NTFY_ELS) {
1635 nack->u.isp24.flags = ntfy->u.isp24.flags &
1636 cpu_to_le32(NOTIFY24XX_FLAGS_PUREX_IOCB);
1638 nack->u.isp24.srr_rx_id = ntfy->u.isp24.srr_rx_id;
1639 nack->u.isp24.status = ntfy->u.isp24.status;
1640 nack->u.isp24.status_subcode = ntfy->u.isp24.status_subcode;
1641 nack->u.isp24.fw_handle = ntfy->u.isp24.fw_handle;
1642 nack->u.isp24.exchange_address = ntfy->u.isp24.exchange_address;
1643 nack->u.isp24.srr_rel_offs = ntfy->u.isp24.srr_rel_offs;
1644 nack->u.isp24.srr_ui = ntfy->u.isp24.srr_ui;
1645 nack->u.isp24.srr_flags = cpu_to_le16(srr_flags);
1646 nack->u.isp24.srr_reject_code = srr_reject_code;
1647 nack->u.isp24.srr_reject_code_expl = srr_explan;
1648 nack->u.isp24.vp_index = ntfy->u.isp24.vp_index;
1650 ql_dbg(ql_dbg_tgt, vha, 0xe005,
1651 "qla_target(%d): Sending 24xx Notify Ack %d\n",
1652 vha->vp_idx, nack->u.isp24.status);
1654 /* Memory Barrier */
1655 wmb();
1656 qla2x00_start_iocbs(vha, qpair->req);
1660 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
1662 static void qlt_24xx_send_abts_resp(struct qla_qpair *qpair,
1663 struct abts_recv_from_24xx *abts, uint32_t status,
1664 bool ids_reversed)
1666 struct scsi_qla_host *vha = qpair->vha;
1667 struct qla_hw_data *ha = vha->hw;
1668 struct abts_resp_to_24xx *resp;
1669 uint32_t f_ctl;
1670 uint8_t *p;
1672 ql_dbg(ql_dbg_tgt, vha, 0xe006,
1673 "Sending task mgmt ABTS response (ha=%p, atio=%p, status=%x\n",
1674 ha, abts, status);
1676 resp = (struct abts_resp_to_24xx *)qla2x00_alloc_iocbs_ready(qpair,
1677 NULL);
1678 if (!resp) {
1679 ql_dbg(ql_dbg_tgt, vha, 0xe04a,
1680 "qla_target(%d): %s failed: unable to allocate "
1681 "request packet", vha->vp_idx, __func__);
1682 return;
1685 resp->entry_type = ABTS_RESP_24XX;
1686 resp->entry_count = 1;
1687 resp->nport_handle = abts->nport_handle;
1688 resp->vp_index = vha->vp_idx;
1689 resp->sof_type = abts->sof_type;
1690 resp->exchange_address = abts->exchange_address;
1691 resp->fcp_hdr_le = abts->fcp_hdr_le;
1692 f_ctl = cpu_to_le32(F_CTL_EXCH_CONTEXT_RESP |
1693 F_CTL_LAST_SEQ | F_CTL_END_SEQ |
1694 F_CTL_SEQ_INITIATIVE);
1695 p = (uint8_t *)&f_ctl;
1696 resp->fcp_hdr_le.f_ctl[0] = *p++;
1697 resp->fcp_hdr_le.f_ctl[1] = *p++;
1698 resp->fcp_hdr_le.f_ctl[2] = *p;
1699 if (ids_reversed) {
1700 resp->fcp_hdr_le.d_id[0] = abts->fcp_hdr_le.d_id[0];
1701 resp->fcp_hdr_le.d_id[1] = abts->fcp_hdr_le.d_id[1];
1702 resp->fcp_hdr_le.d_id[2] = abts->fcp_hdr_le.d_id[2];
1703 resp->fcp_hdr_le.s_id[0] = abts->fcp_hdr_le.s_id[0];
1704 resp->fcp_hdr_le.s_id[1] = abts->fcp_hdr_le.s_id[1];
1705 resp->fcp_hdr_le.s_id[2] = abts->fcp_hdr_le.s_id[2];
1706 } else {
1707 resp->fcp_hdr_le.d_id[0] = abts->fcp_hdr_le.s_id[0];
1708 resp->fcp_hdr_le.d_id[1] = abts->fcp_hdr_le.s_id[1];
1709 resp->fcp_hdr_le.d_id[2] = abts->fcp_hdr_le.s_id[2];
1710 resp->fcp_hdr_le.s_id[0] = abts->fcp_hdr_le.d_id[0];
1711 resp->fcp_hdr_le.s_id[1] = abts->fcp_hdr_le.d_id[1];
1712 resp->fcp_hdr_le.s_id[2] = abts->fcp_hdr_le.d_id[2];
1714 resp->exchange_addr_to_abort = abts->exchange_addr_to_abort;
1715 if (status == FCP_TMF_CMPL) {
1716 resp->fcp_hdr_le.r_ctl = R_CTL_BASIC_LINK_SERV | R_CTL_B_ACC;
1717 resp->payload.ba_acct.seq_id_valid = SEQ_ID_INVALID;
1718 resp->payload.ba_acct.low_seq_cnt = 0x0000;
1719 resp->payload.ba_acct.high_seq_cnt = 0xFFFF;
1720 resp->payload.ba_acct.ox_id = abts->fcp_hdr_le.ox_id;
1721 resp->payload.ba_acct.rx_id = abts->fcp_hdr_le.rx_id;
1722 } else {
1723 resp->fcp_hdr_le.r_ctl = R_CTL_BASIC_LINK_SERV | R_CTL_B_RJT;
1724 resp->payload.ba_rjt.reason_code =
1725 BA_RJT_REASON_CODE_UNABLE_TO_PERFORM;
1726 /* Other bytes are zero */
1729 vha->vha_tgt.qla_tgt->abts_resp_expected++;
1731 /* Memory Barrier */
1732 wmb();
1733 if (qpair->reqq_start_iocbs)
1734 qpair->reqq_start_iocbs(qpair);
1735 else
1736 qla2x00_start_iocbs(vha, qpair->req);
1740 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
1742 static void qlt_24xx_retry_term_exchange(struct scsi_qla_host *vha,
1743 struct abts_resp_from_24xx_fw *entry)
1745 struct ctio7_to_24xx *ctio;
1747 ql_dbg(ql_dbg_tgt, vha, 0xe007,
1748 "Sending retry TERM EXCH CTIO7 (ha=%p)\n", vha->hw);
1750 ctio = (struct ctio7_to_24xx *)qla2x00_alloc_iocbs_ready(
1751 vha->hw->base_qpair, NULL);
1752 if (ctio == NULL) {
1753 ql_dbg(ql_dbg_tgt, vha, 0xe04b,
1754 "qla_target(%d): %s failed: unable to allocate "
1755 "request packet\n", vha->vp_idx, __func__);
1756 return;
1760 * We've got on entrance firmware's response on by us generated
1761 * ABTS response. So, in it ID fields are reversed.
1764 ctio->entry_type = CTIO_TYPE7;
1765 ctio->entry_count = 1;
1766 ctio->nport_handle = entry->nport_handle;
1767 ctio->handle = QLA_TGT_SKIP_HANDLE | CTIO_COMPLETION_HANDLE_MARK;
1768 ctio->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
1769 ctio->vp_index = vha->vp_idx;
1770 ctio->initiator_id[0] = entry->fcp_hdr_le.d_id[0];
1771 ctio->initiator_id[1] = entry->fcp_hdr_le.d_id[1];
1772 ctio->initiator_id[2] = entry->fcp_hdr_le.d_id[2];
1773 ctio->exchange_addr = entry->exchange_addr_to_abort;
1774 ctio->u.status1.flags = cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_1 |
1775 CTIO7_FLAGS_TERMINATE);
1776 ctio->u.status1.ox_id = cpu_to_le16(entry->fcp_hdr_le.ox_id);
1778 /* Memory Barrier */
1779 wmb();
1780 qla2x00_start_iocbs(vha, vha->req);
1782 qlt_24xx_send_abts_resp(vha->hw->base_qpair,
1783 (struct abts_recv_from_24xx *)entry,
1784 FCP_TMF_CMPL, true);
1787 static int abort_cmd_for_tag(struct scsi_qla_host *vha, uint32_t tag)
1789 struct qla_tgt_sess_op *op;
1790 struct qla_tgt_cmd *cmd;
1791 unsigned long flags;
1793 spin_lock_irqsave(&vha->cmd_list_lock, flags);
1794 list_for_each_entry(op, &vha->qla_sess_op_cmd_list, cmd_list) {
1795 if (tag == op->atio.u.isp24.exchange_addr) {
1796 op->aborted = true;
1797 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
1798 return 1;
1802 list_for_each_entry(op, &vha->unknown_atio_list, cmd_list) {
1803 if (tag == op->atio.u.isp24.exchange_addr) {
1804 op->aborted = true;
1805 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
1806 return 1;
1810 list_for_each_entry(cmd, &vha->qla_cmd_list, cmd_list) {
1811 if (tag == cmd->atio.u.isp24.exchange_addr) {
1812 cmd->aborted = 1;
1813 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
1814 return 1;
1817 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
1819 return 0;
1822 /* drop cmds for the given lun
1823 * XXX only looks for cmds on the port through which lun reset was recieved
1824 * XXX does not go through the list of other port (which may have cmds
1825 * for the same lun)
1827 static void abort_cmds_for_lun(struct scsi_qla_host *vha,
1828 u64 lun, uint8_t *s_id)
1830 struct qla_tgt_sess_op *op;
1831 struct qla_tgt_cmd *cmd;
1832 uint32_t key;
1833 unsigned long flags;
1835 key = sid_to_key(s_id);
1836 spin_lock_irqsave(&vha->cmd_list_lock, flags);
1837 list_for_each_entry(op, &vha->qla_sess_op_cmd_list, cmd_list) {
1838 uint32_t op_key;
1839 u64 op_lun;
1841 op_key = sid_to_key(op->atio.u.isp24.fcp_hdr.s_id);
1842 op_lun = scsilun_to_int(
1843 (struct scsi_lun *)&op->atio.u.isp24.fcp_cmnd.lun);
1844 if (op_key == key && op_lun == lun)
1845 op->aborted = true;
1848 list_for_each_entry(op, &vha->unknown_atio_list, cmd_list) {
1849 uint32_t op_key;
1850 u64 op_lun;
1852 op_key = sid_to_key(op->atio.u.isp24.fcp_hdr.s_id);
1853 op_lun = scsilun_to_int(
1854 (struct scsi_lun *)&op->atio.u.isp24.fcp_cmnd.lun);
1855 if (op_key == key && op_lun == lun)
1856 op->aborted = true;
1859 list_for_each_entry(cmd, &vha->qla_cmd_list, cmd_list) {
1860 uint32_t cmd_key;
1861 u64 cmd_lun;
1863 cmd_key = sid_to_key(cmd->atio.u.isp24.fcp_hdr.s_id);
1864 cmd_lun = scsilun_to_int(
1865 (struct scsi_lun *)&cmd->atio.u.isp24.fcp_cmnd.lun);
1866 if (cmd_key == key && cmd_lun == lun)
1867 cmd->aborted = 1;
1869 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
1872 /* ha->hardware_lock supposed to be held on entry */
1873 static int __qlt_24xx_handle_abts(struct scsi_qla_host *vha,
1874 struct abts_recv_from_24xx *abts, struct fc_port *sess)
1876 struct qla_hw_data *ha = vha->hw;
1877 struct qla_tgt_mgmt_cmd *mcmd;
1878 int rc;
1880 if (abort_cmd_for_tag(vha, abts->exchange_addr_to_abort)) {
1881 /* send TASK_ABORT response immediately */
1882 qlt_24xx_send_abts_resp(ha->base_qpair, abts, FCP_TMF_CMPL, false);
1883 return 0;
1886 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00f,
1887 "qla_target(%d): task abort (tag=%d)\n",
1888 vha->vp_idx, abts->exchange_addr_to_abort);
1890 mcmd = mempool_alloc(qla_tgt_mgmt_cmd_mempool, GFP_ATOMIC);
1891 if (mcmd == NULL) {
1892 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf051,
1893 "qla_target(%d): %s: Allocation of ABORT cmd failed",
1894 vha->vp_idx, __func__);
1895 return -ENOMEM;
1897 memset(mcmd, 0, sizeof(*mcmd));
1899 mcmd->sess = sess;
1900 memcpy(&mcmd->orig_iocb.abts, abts, sizeof(mcmd->orig_iocb.abts));
1901 mcmd->reset_count = ha->base_qpair->chip_reset;
1902 mcmd->tmr_func = QLA_TGT_ABTS;
1903 mcmd->qpair = ha->base_qpair;
1906 * LUN is looked up by target-core internally based on the passed
1907 * abts->exchange_addr_to_abort tag.
1909 rc = ha->tgt.tgt_ops->handle_tmr(mcmd, 0, mcmd->tmr_func,
1910 abts->exchange_addr_to_abort);
1911 if (rc != 0) {
1912 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf052,
1913 "qla_target(%d): tgt_ops->handle_tmr()"
1914 " failed: %d", vha->vp_idx, rc);
1915 mempool_free(mcmd, qla_tgt_mgmt_cmd_mempool);
1916 return -EFAULT;
1919 return 0;
1923 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
1925 static void qlt_24xx_handle_abts(struct scsi_qla_host *vha,
1926 struct abts_recv_from_24xx *abts)
1928 struct qla_hw_data *ha = vha->hw;
1929 struct fc_port *sess;
1930 uint32_t tag = abts->exchange_addr_to_abort;
1931 uint8_t s_id[3];
1932 int rc;
1933 unsigned long flags;
1935 if (le32_to_cpu(abts->fcp_hdr_le.parameter) & ABTS_PARAM_ABORT_SEQ) {
1936 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf053,
1937 "qla_target(%d): ABTS: Abort Sequence not "
1938 "supported\n", vha->vp_idx);
1939 qlt_24xx_send_abts_resp(ha->base_qpair, abts, FCP_TMF_REJECTED,
1940 false);
1941 return;
1944 if (tag == ATIO_EXCHANGE_ADDRESS_UNKNOWN) {
1945 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf010,
1946 "qla_target(%d): ABTS: Unknown Exchange "
1947 "Address received\n", vha->vp_idx);
1948 qlt_24xx_send_abts_resp(ha->base_qpair, abts, FCP_TMF_REJECTED,
1949 false);
1950 return;
1953 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf011,
1954 "qla_target(%d): task abort (s_id=%x:%x:%x, "
1955 "tag=%d, param=%x)\n", vha->vp_idx, abts->fcp_hdr_le.s_id[2],
1956 abts->fcp_hdr_le.s_id[1], abts->fcp_hdr_le.s_id[0], tag,
1957 le32_to_cpu(abts->fcp_hdr_le.parameter));
1959 s_id[0] = abts->fcp_hdr_le.s_id[2];
1960 s_id[1] = abts->fcp_hdr_le.s_id[1];
1961 s_id[2] = abts->fcp_hdr_le.s_id[0];
1963 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
1964 sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha, s_id);
1965 if (!sess) {
1966 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf012,
1967 "qla_target(%d): task abort for non-existant session\n",
1968 vha->vp_idx);
1969 rc = qlt_sched_sess_work(vha->vha_tgt.qla_tgt,
1970 QLA_TGT_SESS_WORK_ABORT, abts, sizeof(*abts));
1972 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
1974 if (rc != 0) {
1975 qlt_24xx_send_abts_resp(ha->base_qpair, abts,
1976 FCP_TMF_REJECTED, false);
1978 return;
1980 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
1983 if (sess->deleted) {
1984 qlt_24xx_send_abts_resp(ha->base_qpair, abts, FCP_TMF_REJECTED,
1985 false);
1986 return;
1989 rc = __qlt_24xx_handle_abts(vha, abts, sess);
1990 if (rc != 0) {
1991 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf054,
1992 "qla_target(%d): __qlt_24xx_handle_abts() failed: %d\n",
1993 vha->vp_idx, rc);
1994 qlt_24xx_send_abts_resp(ha->base_qpair, abts, FCP_TMF_REJECTED,
1995 false);
1996 return;
2001 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
2003 static void qlt_24xx_send_task_mgmt_ctio(struct qla_qpair *qpair,
2004 struct qla_tgt_mgmt_cmd *mcmd, uint32_t resp_code)
2006 struct scsi_qla_host *ha = qpair->vha;
2007 struct atio_from_isp *atio = &mcmd->orig_iocb.atio;
2008 struct ctio7_to_24xx *ctio;
2009 uint16_t temp;
2011 ql_dbg(ql_dbg_tgt, ha, 0xe008,
2012 "Sending task mgmt CTIO7 (ha=%p, atio=%p, resp_code=%x\n",
2013 ha, atio, resp_code);
2016 ctio = (struct ctio7_to_24xx *)__qla2x00_alloc_iocbs(qpair, NULL);
2017 if (ctio == NULL) {
2018 ql_dbg(ql_dbg_tgt, ha, 0xe04c,
2019 "qla_target(%d): %s failed: unable to allocate "
2020 "request packet\n", ha->vp_idx, __func__);
2021 return;
2024 ctio->entry_type = CTIO_TYPE7;
2025 ctio->entry_count = 1;
2026 ctio->handle = QLA_TGT_SKIP_HANDLE | CTIO_COMPLETION_HANDLE_MARK;
2027 ctio->nport_handle = mcmd->sess->loop_id;
2028 ctio->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
2029 ctio->vp_index = ha->vp_idx;
2030 ctio->initiator_id[0] = atio->u.isp24.fcp_hdr.s_id[2];
2031 ctio->initiator_id[1] = atio->u.isp24.fcp_hdr.s_id[1];
2032 ctio->initiator_id[2] = atio->u.isp24.fcp_hdr.s_id[0];
2033 ctio->exchange_addr = atio->u.isp24.exchange_addr;
2034 temp = (atio->u.isp24.attr << 9)|
2035 CTIO7_FLAGS_STATUS_MODE_1 | CTIO7_FLAGS_SEND_STATUS;
2036 ctio->u.status1.flags = cpu_to_le16(temp);
2037 temp = be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id);
2038 ctio->u.status1.ox_id = cpu_to_le16(temp);
2039 ctio->u.status1.scsi_status =
2040 cpu_to_le16(SS_RESPONSE_INFO_LEN_VALID);
2041 ctio->u.status1.response_len = cpu_to_le16(8);
2042 ctio->u.status1.sense_data[0] = resp_code;
2044 /* Memory Barrier */
2045 wmb();
2046 if (qpair->reqq_start_iocbs)
2047 qpair->reqq_start_iocbs(qpair);
2048 else
2049 qla2x00_start_iocbs(ha, qpair->req);
2052 void qlt_free_mcmd(struct qla_tgt_mgmt_cmd *mcmd)
2054 mempool_free(mcmd, qla_tgt_mgmt_cmd_mempool);
2056 EXPORT_SYMBOL(qlt_free_mcmd);
2059 * ha->hardware_lock supposed to be held on entry. Might drop it, then
2060 * reacquire
2062 void qlt_send_resp_ctio(struct qla_qpair *qpair, struct qla_tgt_cmd *cmd,
2063 uint8_t scsi_status, uint8_t sense_key, uint8_t asc, uint8_t ascq)
2065 struct atio_from_isp *atio = &cmd->atio;
2066 struct ctio7_to_24xx *ctio;
2067 uint16_t temp;
2068 struct scsi_qla_host *vha = cmd->vha;
2070 ql_dbg(ql_dbg_tgt_dif, vha, 0x3066,
2071 "Sending response CTIO7 (vha=%p, atio=%p, scsi_status=%02x, "
2072 "sense_key=%02x, asc=%02x, ascq=%02x",
2073 vha, atio, scsi_status, sense_key, asc, ascq);
2075 ctio = (struct ctio7_to_24xx *)qla2x00_alloc_iocbs(vha, NULL);
2076 if (!ctio) {
2077 ql_dbg(ql_dbg_async, vha, 0x3067,
2078 "qla2x00t(%ld): %s failed: unable to allocate request packet",
2079 vha->host_no, __func__);
2080 goto out;
2083 ctio->entry_type = CTIO_TYPE7;
2084 ctio->entry_count = 1;
2085 ctio->handle = QLA_TGT_SKIP_HANDLE;
2086 ctio->nport_handle = cmd->sess->loop_id;
2087 ctio->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
2088 ctio->vp_index = vha->vp_idx;
2089 ctio->initiator_id[0] = atio->u.isp24.fcp_hdr.s_id[2];
2090 ctio->initiator_id[1] = atio->u.isp24.fcp_hdr.s_id[1];
2091 ctio->initiator_id[2] = atio->u.isp24.fcp_hdr.s_id[0];
2092 ctio->exchange_addr = atio->u.isp24.exchange_addr;
2093 temp = (atio->u.isp24.attr << 9) |
2094 CTIO7_FLAGS_STATUS_MODE_1 | CTIO7_FLAGS_SEND_STATUS;
2095 ctio->u.status1.flags = cpu_to_le16(temp);
2096 temp = be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id);
2097 ctio->u.status1.ox_id = cpu_to_le16(temp);
2098 ctio->u.status1.scsi_status =
2099 cpu_to_le16(SS_RESPONSE_INFO_LEN_VALID | scsi_status);
2100 ctio->u.status1.response_len = cpu_to_le16(18);
2101 ctio->u.status1.residual = cpu_to_le32(get_datalen_for_atio(atio));
2103 if (ctio->u.status1.residual != 0)
2104 ctio->u.status1.scsi_status |=
2105 cpu_to_le16(SS_RESIDUAL_UNDER);
2107 /* Response code and sense key */
2108 put_unaligned_le32(((0x70 << 24) | (sense_key << 8)),
2109 (&ctio->u.status1.sense_data)[0]);
2110 /* Additional sense length */
2111 put_unaligned_le32(0x0a, (&ctio->u.status1.sense_data)[1]);
2112 /* ASC and ASCQ */
2113 put_unaligned_le32(((asc << 24) | (ascq << 16)),
2114 (&ctio->u.status1.sense_data)[3]);
2116 /* Memory Barrier */
2117 wmb();
2119 if (qpair->reqq_start_iocbs)
2120 qpair->reqq_start_iocbs(qpair);
2121 else
2122 qla2x00_start_iocbs(vha, qpair->req);
2124 out:
2125 return;
2128 /* callback from target fabric module code */
2129 void qlt_xmit_tm_rsp(struct qla_tgt_mgmt_cmd *mcmd)
2131 struct scsi_qla_host *vha = mcmd->sess->vha;
2132 struct qla_hw_data *ha = vha->hw;
2133 unsigned long flags;
2134 struct qla_qpair *qpair = mcmd->qpair;
2136 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf013,
2137 "TM response mcmd (%p) status %#x state %#x",
2138 mcmd, mcmd->fc_tm_rsp, mcmd->flags);
2140 spin_lock_irqsave(qpair->qp_lock_ptr, flags);
2142 if (!vha->flags.online || mcmd->reset_count != qpair->chip_reset) {
2144 * Either the port is not online or this request was from
2145 * previous life, just abort the processing.
2147 ql_dbg(ql_dbg_async, vha, 0xe100,
2148 "RESET-TMR online/active/old-count/new-count = %d/%d/%d/%d.\n",
2149 vha->flags.online, qla2x00_reset_active(vha),
2150 mcmd->reset_count, qpair->chip_reset);
2151 ha->tgt.tgt_ops->free_mcmd(mcmd);
2152 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
2153 return;
2156 if (mcmd->flags == QLA24XX_MGMT_SEND_NACK) {
2157 if (mcmd->orig_iocb.imm_ntfy.u.isp24.status_subcode ==
2158 ELS_LOGO ||
2159 mcmd->orig_iocb.imm_ntfy.u.isp24.status_subcode ==
2160 ELS_PRLO ||
2161 mcmd->orig_iocb.imm_ntfy.u.isp24.status_subcode ==
2162 ELS_TPRLO) {
2163 ql_dbg(ql_dbg_disc, vha, 0x2106,
2164 "TM response logo %phC status %#x state %#x",
2165 mcmd->sess->port_name, mcmd->fc_tm_rsp,
2166 mcmd->flags);
2167 qlt_schedule_sess_for_deletion_lock(mcmd->sess);
2168 } else {
2169 qlt_send_notify_ack(vha->hw->base_qpair,
2170 &mcmd->orig_iocb.imm_ntfy, 0, 0, 0, 0, 0, 0);
2172 } else {
2173 if (mcmd->orig_iocb.atio.u.raw.entry_type == ABTS_RECV_24XX)
2174 qlt_24xx_send_abts_resp(qpair, &mcmd->orig_iocb.abts,
2175 mcmd->fc_tm_rsp, false);
2176 else
2177 qlt_24xx_send_task_mgmt_ctio(qpair, mcmd,
2178 mcmd->fc_tm_rsp);
2181 * Make the callback for ->free_mcmd() to queue_work() and invoke
2182 * target_put_sess_cmd() to drop cmd_kref to 1. The final
2183 * target_put_sess_cmd() call will be made from TFO->check_stop_free()
2184 * -> tcm_qla2xxx_check_stop_free() to release the TMR associated se_cmd
2185 * descriptor after TFO->queue_tm_rsp() -> tcm_qla2xxx_queue_tm_rsp() ->
2186 * qlt_xmit_tm_rsp() returns here..
2188 ha->tgt.tgt_ops->free_mcmd(mcmd);
2189 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
2191 EXPORT_SYMBOL(qlt_xmit_tm_rsp);
2193 /* No locks */
2194 static int qlt_pci_map_calc_cnt(struct qla_tgt_prm *prm)
2196 struct qla_tgt_cmd *cmd = prm->cmd;
2198 BUG_ON(cmd->sg_cnt == 0);
2200 prm->sg = (struct scatterlist *)cmd->sg;
2201 prm->seg_cnt = pci_map_sg(cmd->qpair->pdev, cmd->sg,
2202 cmd->sg_cnt, cmd->dma_data_direction);
2203 if (unlikely(prm->seg_cnt == 0))
2204 goto out_err;
2206 prm->cmd->sg_mapped = 1;
2208 if (cmd->se_cmd.prot_op == TARGET_PROT_NORMAL) {
2210 * If greater than four sg entries then we need to allocate
2211 * the continuation entries
2213 if (prm->seg_cnt > QLA_TGT_DATASEGS_PER_CMD_24XX)
2214 prm->req_cnt += DIV_ROUND_UP(prm->seg_cnt -
2215 QLA_TGT_DATASEGS_PER_CMD_24XX,
2216 QLA_TGT_DATASEGS_PER_CONT_24XX);
2217 } else {
2218 /* DIF */
2219 if ((cmd->se_cmd.prot_op == TARGET_PROT_DIN_INSERT) ||
2220 (cmd->se_cmd.prot_op == TARGET_PROT_DOUT_STRIP)) {
2221 prm->seg_cnt = DIV_ROUND_UP(cmd->bufflen, cmd->blk_sz);
2222 prm->tot_dsds = prm->seg_cnt;
2223 } else
2224 prm->tot_dsds = prm->seg_cnt;
2226 if (cmd->prot_sg_cnt) {
2227 prm->prot_sg = cmd->prot_sg;
2228 prm->prot_seg_cnt = pci_map_sg(cmd->qpair->pdev,
2229 cmd->prot_sg, cmd->prot_sg_cnt,
2230 cmd->dma_data_direction);
2231 if (unlikely(prm->prot_seg_cnt == 0))
2232 goto out_err;
2234 if ((cmd->se_cmd.prot_op == TARGET_PROT_DIN_INSERT) ||
2235 (cmd->se_cmd.prot_op == TARGET_PROT_DOUT_STRIP)) {
2236 /* Dif Bundling not support here */
2237 prm->prot_seg_cnt = DIV_ROUND_UP(cmd->bufflen,
2238 cmd->blk_sz);
2239 prm->tot_dsds += prm->prot_seg_cnt;
2240 } else
2241 prm->tot_dsds += prm->prot_seg_cnt;
2245 return 0;
2247 out_err:
2248 ql_dbg_qp(ql_dbg_tgt, prm->cmd->qpair, 0xe04d,
2249 "qla_target(%d): PCI mapping failed: sg_cnt=%d",
2250 0, prm->cmd->sg_cnt);
2251 return -1;
2254 static void qlt_unmap_sg(struct scsi_qla_host *vha, struct qla_tgt_cmd *cmd)
2256 struct qla_hw_data *ha;
2257 struct qla_qpair *qpair;
2258 if (!cmd->sg_mapped)
2259 return;
2261 qpair = cmd->qpair;
2263 pci_unmap_sg(qpair->pdev, cmd->sg, cmd->sg_cnt,
2264 cmd->dma_data_direction);
2265 cmd->sg_mapped = 0;
2267 if (cmd->prot_sg_cnt)
2268 pci_unmap_sg(qpair->pdev, cmd->prot_sg, cmd->prot_sg_cnt,
2269 cmd->dma_data_direction);
2271 if (!cmd->ctx)
2272 return;
2273 ha = vha->hw;
2274 if (cmd->ctx_dsd_alloced)
2275 qla2x00_clean_dsd_pool(ha, cmd->ctx);
2277 dma_pool_free(ha->dl_dma_pool, cmd->ctx, cmd->ctx->crc_ctx_dma);
2280 static int qlt_check_reserve_free_req(struct qla_qpair *qpair,
2281 uint32_t req_cnt)
2283 uint32_t cnt;
2284 struct req_que *req = qpair->req;
2286 if (req->cnt < (req_cnt + 2)) {
2287 cnt = (uint16_t)(qpair->use_shadow_reg ? *req->out_ptr :
2288 RD_REG_DWORD_RELAXED(req->req_q_out));
2290 if (req->ring_index < cnt)
2291 req->cnt = cnt - req->ring_index;
2292 else
2293 req->cnt = req->length - (req->ring_index - cnt);
2295 if (unlikely(req->cnt < (req_cnt + 2)))
2296 return -EAGAIN;
2299 req->cnt -= req_cnt;
2301 return 0;
2305 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
2307 static inline void *qlt_get_req_pkt(struct req_que *req)
2309 /* Adjust ring index. */
2310 req->ring_index++;
2311 if (req->ring_index == req->length) {
2312 req->ring_index = 0;
2313 req->ring_ptr = req->ring;
2314 } else {
2315 req->ring_ptr++;
2317 return (cont_entry_t *)req->ring_ptr;
2320 /* ha->hardware_lock supposed to be held on entry */
2321 static inline uint32_t qlt_make_handle(struct qla_qpair *qpair)
2323 uint32_t h;
2324 int index;
2325 uint8_t found = 0;
2326 struct req_que *req = qpair->req;
2328 h = req->current_outstanding_cmd;
2330 for (index = 1; index < req->num_outstanding_cmds; index++) {
2331 h++;
2332 if (h == req->num_outstanding_cmds)
2333 h = 1;
2335 if (h == QLA_TGT_SKIP_HANDLE)
2336 continue;
2338 if (!req->outstanding_cmds[h]) {
2339 found = 1;
2340 break;
2344 if (found) {
2345 req->current_outstanding_cmd = h;
2346 } else {
2347 ql_dbg(ql_dbg_io, qpair->vha, 0x305b,
2348 "qla_target(%d): Ran out of empty cmd slots\n",
2349 qpair->vha->vp_idx);
2350 h = QLA_TGT_NULL_HANDLE;
2353 return h;
2356 /* ha->hardware_lock supposed to be held on entry */
2357 static int qlt_24xx_build_ctio_pkt(struct qla_qpair *qpair,
2358 struct qla_tgt_prm *prm)
2360 uint32_t h;
2361 struct ctio7_to_24xx *pkt;
2362 struct atio_from_isp *atio = &prm->cmd->atio;
2363 uint16_t temp;
2365 pkt = (struct ctio7_to_24xx *)qpair->req->ring_ptr;
2366 prm->pkt = pkt;
2367 memset(pkt, 0, sizeof(*pkt));
2369 pkt->entry_type = CTIO_TYPE7;
2370 pkt->entry_count = (uint8_t)prm->req_cnt;
2371 pkt->vp_index = prm->cmd->vp_idx;
2373 h = qlt_make_handle(qpair);
2374 if (unlikely(h == QLA_TGT_NULL_HANDLE)) {
2376 * CTIO type 7 from the firmware doesn't provide a way to
2377 * know the initiator's LOOP ID, hence we can't find
2378 * the session and, so, the command.
2380 return -EAGAIN;
2381 } else
2382 qpair->req->outstanding_cmds[h] = (srb_t *)prm->cmd;
2384 pkt->handle = MAKE_HANDLE(qpair->req->id, h);
2385 pkt->handle |= CTIO_COMPLETION_HANDLE_MARK;
2386 pkt->nport_handle = cpu_to_le16(prm->cmd->loop_id);
2387 pkt->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
2388 pkt->initiator_id[0] = atio->u.isp24.fcp_hdr.s_id[2];
2389 pkt->initiator_id[1] = atio->u.isp24.fcp_hdr.s_id[1];
2390 pkt->initiator_id[2] = atio->u.isp24.fcp_hdr.s_id[0];
2391 pkt->exchange_addr = atio->u.isp24.exchange_addr;
2392 temp = atio->u.isp24.attr << 9;
2393 pkt->u.status0.flags |= cpu_to_le16(temp);
2394 temp = be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id);
2395 pkt->u.status0.ox_id = cpu_to_le16(temp);
2396 pkt->u.status0.relative_offset = cpu_to_le32(prm->cmd->offset);
2398 return 0;
2402 * ha->hardware_lock supposed to be held on entry. We have already made sure
2403 * that there is sufficient amount of request entries to not drop it.
2405 static void qlt_load_cont_data_segments(struct qla_tgt_prm *prm)
2407 int cnt;
2408 uint32_t *dword_ptr;
2410 /* Build continuation packets */
2411 while (prm->seg_cnt > 0) {
2412 cont_a64_entry_t *cont_pkt64 =
2413 (cont_a64_entry_t *)qlt_get_req_pkt(
2414 prm->cmd->qpair->req);
2417 * Make sure that from cont_pkt64 none of
2418 * 64-bit specific fields used for 32-bit
2419 * addressing. Cast to (cont_entry_t *) for
2420 * that.
2423 memset(cont_pkt64, 0, sizeof(*cont_pkt64));
2425 cont_pkt64->entry_count = 1;
2426 cont_pkt64->sys_define = 0;
2428 cont_pkt64->entry_type = CONTINUE_A64_TYPE;
2429 dword_ptr = (uint32_t *)&cont_pkt64->dseg_0_address;
2431 /* Load continuation entry data segments */
2432 for (cnt = 0;
2433 cnt < QLA_TGT_DATASEGS_PER_CONT_24XX && prm->seg_cnt;
2434 cnt++, prm->seg_cnt--) {
2435 *dword_ptr++ =
2436 cpu_to_le32(pci_dma_lo32
2437 (sg_dma_address(prm->sg)));
2438 *dword_ptr++ = cpu_to_le32(pci_dma_hi32
2439 (sg_dma_address(prm->sg)));
2440 *dword_ptr++ = cpu_to_le32(sg_dma_len(prm->sg));
2442 prm->sg = sg_next(prm->sg);
2448 * ha->hardware_lock supposed to be held on entry. We have already made sure
2449 * that there is sufficient amount of request entries to not drop it.
2451 static void qlt_load_data_segments(struct qla_tgt_prm *prm)
2453 int cnt;
2454 uint32_t *dword_ptr;
2455 struct ctio7_to_24xx *pkt24 = (struct ctio7_to_24xx *)prm->pkt;
2457 pkt24->u.status0.transfer_length = cpu_to_le32(prm->cmd->bufflen);
2459 /* Setup packet address segment pointer */
2460 dword_ptr = pkt24->u.status0.dseg_0_address;
2462 /* Set total data segment count */
2463 if (prm->seg_cnt)
2464 pkt24->dseg_count = cpu_to_le16(prm->seg_cnt);
2466 if (prm->seg_cnt == 0) {
2467 /* No data transfer */
2468 *dword_ptr++ = 0;
2469 *dword_ptr = 0;
2470 return;
2473 /* If scatter gather */
2475 /* Load command entry data segments */
2476 for (cnt = 0;
2477 (cnt < QLA_TGT_DATASEGS_PER_CMD_24XX) && prm->seg_cnt;
2478 cnt++, prm->seg_cnt--) {
2479 *dword_ptr++ =
2480 cpu_to_le32(pci_dma_lo32(sg_dma_address(prm->sg)));
2482 *dword_ptr++ = cpu_to_le32(pci_dma_hi32(
2483 sg_dma_address(prm->sg)));
2485 *dword_ptr++ = cpu_to_le32(sg_dma_len(prm->sg));
2487 prm->sg = sg_next(prm->sg);
2490 qlt_load_cont_data_segments(prm);
2493 static inline int qlt_has_data(struct qla_tgt_cmd *cmd)
2495 return cmd->bufflen > 0;
2498 static void qlt_print_dif_err(struct qla_tgt_prm *prm)
2500 struct qla_tgt_cmd *cmd;
2501 struct scsi_qla_host *vha;
2503 /* asc 0x10=dif error */
2504 if (prm->sense_buffer && (prm->sense_buffer[12] == 0x10)) {
2505 cmd = prm->cmd;
2506 vha = cmd->vha;
2507 /* ASCQ */
2508 switch (prm->sense_buffer[13]) {
2509 case 1:
2510 ql_dbg(ql_dbg_tgt_dif, vha, 0xe00b,
2511 "BE detected Guard TAG ERR: lba[0x%llx|%lld] len[0x%x] "
2512 "se_cmd=%p tag[%x]",
2513 cmd->lba, cmd->lba, cmd->num_blks, &cmd->se_cmd,
2514 cmd->atio.u.isp24.exchange_addr);
2515 break;
2516 case 2:
2517 ql_dbg(ql_dbg_tgt_dif, vha, 0xe00c,
2518 "BE detected APP TAG ERR: lba[0x%llx|%lld] len[0x%x] "
2519 "se_cmd=%p tag[%x]",
2520 cmd->lba, cmd->lba, cmd->num_blks, &cmd->se_cmd,
2521 cmd->atio.u.isp24.exchange_addr);
2522 break;
2523 case 3:
2524 ql_dbg(ql_dbg_tgt_dif, vha, 0xe00f,
2525 "BE detected REF TAG ERR: lba[0x%llx|%lld] len[0x%x] "
2526 "se_cmd=%p tag[%x]",
2527 cmd->lba, cmd->lba, cmd->num_blks, &cmd->se_cmd,
2528 cmd->atio.u.isp24.exchange_addr);
2529 break;
2530 default:
2531 ql_dbg(ql_dbg_tgt_dif, vha, 0xe010,
2532 "BE detected Dif ERR: lba[%llx|%lld] len[%x] "
2533 "se_cmd=%p tag[%x]",
2534 cmd->lba, cmd->lba, cmd->num_blks, &cmd->se_cmd,
2535 cmd->atio.u.isp24.exchange_addr);
2536 break;
2538 ql_dump_buffer(ql_dbg_tgt_dif, vha, 0xe011, cmd->cdb, 16);
2543 * Called without ha->hardware_lock held
2545 static int qlt_pre_xmit_response(struct qla_tgt_cmd *cmd,
2546 struct qla_tgt_prm *prm, int xmit_type, uint8_t scsi_status,
2547 uint32_t *full_req_cnt)
2549 struct se_cmd *se_cmd = &cmd->se_cmd;
2550 struct qla_qpair *qpair = cmd->qpair;
2552 prm->cmd = cmd;
2553 prm->tgt = cmd->tgt;
2554 prm->pkt = NULL;
2555 prm->rq_result = scsi_status;
2556 prm->sense_buffer = &cmd->sense_buffer[0];
2557 prm->sense_buffer_len = TRANSPORT_SENSE_BUFFER;
2558 prm->sg = NULL;
2559 prm->seg_cnt = -1;
2560 prm->req_cnt = 1;
2561 prm->residual = 0;
2562 prm->add_status_pkt = 0;
2563 prm->prot_sg = NULL;
2564 prm->prot_seg_cnt = 0;
2565 prm->tot_dsds = 0;
2567 if ((xmit_type & QLA_TGT_XMIT_DATA) && qlt_has_data(cmd)) {
2568 if (qlt_pci_map_calc_cnt(prm) != 0)
2569 return -EAGAIN;
2572 *full_req_cnt = prm->req_cnt;
2574 if (se_cmd->se_cmd_flags & SCF_UNDERFLOW_BIT) {
2575 prm->residual = se_cmd->residual_count;
2576 ql_dbg_qp(ql_dbg_io + ql_dbg_verbose, qpair, 0x305c,
2577 "Residual underflow: %d (tag %lld, op %x, bufflen %d, rq_result %x)\n",
2578 prm->residual, se_cmd->tag,
2579 se_cmd->t_task_cdb ? se_cmd->t_task_cdb[0] : 0,
2580 cmd->bufflen, prm->rq_result);
2581 prm->rq_result |= SS_RESIDUAL_UNDER;
2582 } else if (se_cmd->se_cmd_flags & SCF_OVERFLOW_BIT) {
2583 prm->residual = se_cmd->residual_count;
2584 ql_dbg_qp(ql_dbg_io, qpair, 0x305d,
2585 "Residual overflow: %d (tag %lld, op %x, bufflen %d, rq_result %x)\n",
2586 prm->residual, se_cmd->tag, se_cmd->t_task_cdb ?
2587 se_cmd->t_task_cdb[0] : 0, cmd->bufflen, prm->rq_result);
2588 prm->rq_result |= SS_RESIDUAL_OVER;
2591 if (xmit_type & QLA_TGT_XMIT_STATUS) {
2593 * If QLA_TGT_XMIT_DATA is not set, add_status_pkt will be
2594 * ignored in *xmit_response() below
2596 if (qlt_has_data(cmd)) {
2597 if (QLA_TGT_SENSE_VALID(prm->sense_buffer) ||
2598 (IS_FWI2_CAPABLE(cmd->vha->hw) &&
2599 (prm->rq_result != 0))) {
2600 prm->add_status_pkt = 1;
2601 (*full_req_cnt)++;
2606 return 0;
2609 static inline int qlt_need_explicit_conf(struct qla_tgt_cmd *cmd,
2610 int sending_sense)
2612 if (cmd->qpair->enable_class_2)
2613 return 0;
2615 if (sending_sense)
2616 return cmd->conf_compl_supported;
2617 else
2618 return cmd->qpair->enable_explicit_conf &&
2619 cmd->conf_compl_supported;
2622 static void qlt_24xx_init_ctio_to_isp(struct ctio7_to_24xx *ctio,
2623 struct qla_tgt_prm *prm)
2625 prm->sense_buffer_len = min_t(uint32_t, prm->sense_buffer_len,
2626 (uint32_t)sizeof(ctio->u.status1.sense_data));
2627 ctio->u.status0.flags |= cpu_to_le16(CTIO7_FLAGS_SEND_STATUS);
2628 if (qlt_need_explicit_conf(prm->cmd, 0)) {
2629 ctio->u.status0.flags |= cpu_to_le16(
2630 CTIO7_FLAGS_EXPLICIT_CONFORM |
2631 CTIO7_FLAGS_CONFORM_REQ);
2633 ctio->u.status0.residual = cpu_to_le32(prm->residual);
2634 ctio->u.status0.scsi_status = cpu_to_le16(prm->rq_result);
2635 if (QLA_TGT_SENSE_VALID(prm->sense_buffer)) {
2636 int i;
2638 if (qlt_need_explicit_conf(prm->cmd, 1)) {
2639 if ((prm->rq_result & SS_SCSI_STATUS_BYTE) != 0) {
2640 ql_dbg_qp(ql_dbg_tgt, prm->cmd->qpair, 0xe017,
2641 "Skipping EXPLICIT_CONFORM and "
2642 "CTIO7_FLAGS_CONFORM_REQ for FCP READ w/ "
2643 "non GOOD status\n");
2644 goto skip_explict_conf;
2646 ctio->u.status1.flags |= cpu_to_le16(
2647 CTIO7_FLAGS_EXPLICIT_CONFORM |
2648 CTIO7_FLAGS_CONFORM_REQ);
2650 skip_explict_conf:
2651 ctio->u.status1.flags &=
2652 ~cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_0);
2653 ctio->u.status1.flags |=
2654 cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_1);
2655 ctio->u.status1.scsi_status |=
2656 cpu_to_le16(SS_SENSE_LEN_VALID);
2657 ctio->u.status1.sense_length =
2658 cpu_to_le16(prm->sense_buffer_len);
2659 for (i = 0; i < prm->sense_buffer_len/4; i++)
2660 ((uint32_t *)ctio->u.status1.sense_data)[i] =
2661 cpu_to_be32(((uint32_t *)prm->sense_buffer)[i]);
2663 qlt_print_dif_err(prm);
2665 } else {
2666 ctio->u.status1.flags &=
2667 ~cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_0);
2668 ctio->u.status1.flags |=
2669 cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_1);
2670 ctio->u.status1.sense_length = 0;
2671 memset(ctio->u.status1.sense_data, 0,
2672 sizeof(ctio->u.status1.sense_data));
2675 /* Sense with len > 24, is it possible ??? */
2678 static inline int
2679 qlt_hba_err_chk_enabled(struct se_cmd *se_cmd)
2681 switch (se_cmd->prot_op) {
2682 case TARGET_PROT_DOUT_INSERT:
2683 case TARGET_PROT_DIN_STRIP:
2684 if (ql2xenablehba_err_chk >= 1)
2685 return 1;
2686 break;
2687 case TARGET_PROT_DOUT_PASS:
2688 case TARGET_PROT_DIN_PASS:
2689 if (ql2xenablehba_err_chk >= 2)
2690 return 1;
2691 break;
2692 case TARGET_PROT_DIN_INSERT:
2693 case TARGET_PROT_DOUT_STRIP:
2694 return 1;
2695 default:
2696 break;
2698 return 0;
2701 static inline int
2702 qla_tgt_ref_mask_check(struct se_cmd *se_cmd)
2704 switch (se_cmd->prot_op) {
2705 case TARGET_PROT_DIN_INSERT:
2706 case TARGET_PROT_DOUT_INSERT:
2707 case TARGET_PROT_DIN_STRIP:
2708 case TARGET_PROT_DOUT_STRIP:
2709 case TARGET_PROT_DIN_PASS:
2710 case TARGET_PROT_DOUT_PASS:
2711 return 1;
2712 default:
2713 return 0;
2715 return 0;
2719 * qla_tgt_set_dif_tags - Extract Ref and App tags from SCSI command
2721 static void
2722 qla_tgt_set_dif_tags(struct qla_tgt_cmd *cmd, struct crc_context *ctx,
2723 uint16_t *pfw_prot_opts)
2725 struct se_cmd *se_cmd = &cmd->se_cmd;
2726 uint32_t lba = 0xffffffff & se_cmd->t_task_lba;
2727 scsi_qla_host_t *vha = cmd->tgt->vha;
2728 struct qla_hw_data *ha = vha->hw;
2729 uint32_t t32 = 0;
2732 * wait till Mode Sense/Select cmd, modepage Ah, subpage 2
2733 * have been immplemented by TCM, before AppTag is avail.
2734 * Look for modesense_handlers[]
2736 ctx->app_tag = 0;
2737 ctx->app_tag_mask[0] = 0x0;
2738 ctx->app_tag_mask[1] = 0x0;
2740 if (IS_PI_UNINIT_CAPABLE(ha)) {
2741 if ((se_cmd->prot_type == TARGET_DIF_TYPE1_PROT) ||
2742 (se_cmd->prot_type == TARGET_DIF_TYPE2_PROT))
2743 *pfw_prot_opts |= PO_DIS_VALD_APP_ESC;
2744 else if (se_cmd->prot_type == TARGET_DIF_TYPE3_PROT)
2745 *pfw_prot_opts |= PO_DIS_VALD_APP_REF_ESC;
2748 t32 = ha->tgt.tgt_ops->get_dif_tags(cmd, pfw_prot_opts);
2750 switch (se_cmd->prot_type) {
2751 case TARGET_DIF_TYPE0_PROT:
2753 * No check for ql2xenablehba_err_chk, as it
2754 * would be an I/O error if hba tag generation
2755 * is not done.
2757 ctx->ref_tag = cpu_to_le32(lba);
2758 /* enable ALL bytes of the ref tag */
2759 ctx->ref_tag_mask[0] = 0xff;
2760 ctx->ref_tag_mask[1] = 0xff;
2761 ctx->ref_tag_mask[2] = 0xff;
2762 ctx->ref_tag_mask[3] = 0xff;
2763 break;
2764 case TARGET_DIF_TYPE1_PROT:
2766 * For TYPE 1 protection: 16 bit GUARD tag, 32 bit
2767 * REF tag, and 16 bit app tag.
2769 ctx->ref_tag = cpu_to_le32(lba);
2770 if (!qla_tgt_ref_mask_check(se_cmd) ||
2771 !(ha->tgt.tgt_ops->chk_dif_tags(t32))) {
2772 *pfw_prot_opts |= PO_DIS_REF_TAG_VALD;
2773 break;
2775 /* enable ALL bytes of the ref tag */
2776 ctx->ref_tag_mask[0] = 0xff;
2777 ctx->ref_tag_mask[1] = 0xff;
2778 ctx->ref_tag_mask[2] = 0xff;
2779 ctx->ref_tag_mask[3] = 0xff;
2780 break;
2781 case TARGET_DIF_TYPE2_PROT:
2783 * For TYPE 2 protection: 16 bit GUARD + 32 bit REF
2784 * tag has to match LBA in CDB + N
2786 ctx->ref_tag = cpu_to_le32(lba);
2787 if (!qla_tgt_ref_mask_check(se_cmd) ||
2788 !(ha->tgt.tgt_ops->chk_dif_tags(t32))) {
2789 *pfw_prot_opts |= PO_DIS_REF_TAG_VALD;
2790 break;
2792 /* enable ALL bytes of the ref tag */
2793 ctx->ref_tag_mask[0] = 0xff;
2794 ctx->ref_tag_mask[1] = 0xff;
2795 ctx->ref_tag_mask[2] = 0xff;
2796 ctx->ref_tag_mask[3] = 0xff;
2797 break;
2798 case TARGET_DIF_TYPE3_PROT:
2799 /* For TYPE 3 protection: 16 bit GUARD only */
2800 *pfw_prot_opts |= PO_DIS_REF_TAG_VALD;
2801 ctx->ref_tag_mask[0] = ctx->ref_tag_mask[1] =
2802 ctx->ref_tag_mask[2] = ctx->ref_tag_mask[3] = 0x00;
2803 break;
2807 static inline int
2808 qlt_build_ctio_crc2_pkt(struct qla_qpair *qpair, struct qla_tgt_prm *prm)
2810 uint32_t *cur_dsd;
2811 uint32_t transfer_length = 0;
2812 uint32_t data_bytes;
2813 uint32_t dif_bytes;
2814 uint8_t bundling = 1;
2815 uint8_t *clr_ptr;
2816 struct crc_context *crc_ctx_pkt = NULL;
2817 struct qla_hw_data *ha;
2818 struct ctio_crc2_to_fw *pkt;
2819 dma_addr_t crc_ctx_dma;
2820 uint16_t fw_prot_opts = 0;
2821 struct qla_tgt_cmd *cmd = prm->cmd;
2822 struct se_cmd *se_cmd = &cmd->se_cmd;
2823 uint32_t h;
2824 struct atio_from_isp *atio = &prm->cmd->atio;
2825 struct qla_tc_param tc;
2826 uint16_t t16;
2827 scsi_qla_host_t *vha = cmd->vha;
2829 ha = vha->hw;
2831 pkt = (struct ctio_crc2_to_fw *)qpair->req->ring_ptr;
2832 prm->pkt = pkt;
2833 memset(pkt, 0, sizeof(*pkt));
2835 ql_dbg_qp(ql_dbg_tgt, cmd->qpair, 0xe071,
2836 "qla_target(%d):%s: se_cmd[%p] CRC2 prot_op[0x%x] cmd prot sg:cnt[%p:%x] lba[%llu]\n",
2837 cmd->vp_idx, __func__, se_cmd, se_cmd->prot_op,
2838 prm->prot_sg, prm->prot_seg_cnt, se_cmd->t_task_lba);
2840 if ((se_cmd->prot_op == TARGET_PROT_DIN_INSERT) ||
2841 (se_cmd->prot_op == TARGET_PROT_DOUT_STRIP))
2842 bundling = 0;
2844 /* Compute dif len and adjust data len to incude protection */
2845 data_bytes = cmd->bufflen;
2846 dif_bytes = (data_bytes / cmd->blk_sz) * 8;
2848 switch (se_cmd->prot_op) {
2849 case TARGET_PROT_DIN_INSERT:
2850 case TARGET_PROT_DOUT_STRIP:
2851 transfer_length = data_bytes;
2852 if (cmd->prot_sg_cnt)
2853 data_bytes += dif_bytes;
2854 break;
2855 case TARGET_PROT_DIN_STRIP:
2856 case TARGET_PROT_DOUT_INSERT:
2857 case TARGET_PROT_DIN_PASS:
2858 case TARGET_PROT_DOUT_PASS:
2859 transfer_length = data_bytes + dif_bytes;
2860 break;
2861 default:
2862 BUG();
2863 break;
2866 if (!qlt_hba_err_chk_enabled(se_cmd))
2867 fw_prot_opts |= 0x10; /* Disable Guard tag checking */
2868 /* HBA error checking enabled */
2869 else if (IS_PI_UNINIT_CAPABLE(ha)) {
2870 if ((se_cmd->prot_type == TARGET_DIF_TYPE1_PROT) ||
2871 (se_cmd->prot_type == TARGET_DIF_TYPE2_PROT))
2872 fw_prot_opts |= PO_DIS_VALD_APP_ESC;
2873 else if (se_cmd->prot_type == TARGET_DIF_TYPE3_PROT)
2874 fw_prot_opts |= PO_DIS_VALD_APP_REF_ESC;
2877 switch (se_cmd->prot_op) {
2878 case TARGET_PROT_DIN_INSERT:
2879 case TARGET_PROT_DOUT_INSERT:
2880 fw_prot_opts |= PO_MODE_DIF_INSERT;
2881 break;
2882 case TARGET_PROT_DIN_STRIP:
2883 case TARGET_PROT_DOUT_STRIP:
2884 fw_prot_opts |= PO_MODE_DIF_REMOVE;
2885 break;
2886 case TARGET_PROT_DIN_PASS:
2887 case TARGET_PROT_DOUT_PASS:
2888 fw_prot_opts |= PO_MODE_DIF_PASS;
2889 /* FUTURE: does tcm require T10CRC<->IPCKSUM conversion? */
2890 break;
2891 default:/* Normal Request */
2892 fw_prot_opts |= PO_MODE_DIF_PASS;
2893 break;
2896 /* ---- PKT ---- */
2897 /* Update entry type to indicate Command Type CRC_2 IOCB */
2898 pkt->entry_type = CTIO_CRC2;
2899 pkt->entry_count = 1;
2900 pkt->vp_index = cmd->vp_idx;
2902 h = qlt_make_handle(qpair);
2903 if (unlikely(h == QLA_TGT_NULL_HANDLE)) {
2905 * CTIO type 7 from the firmware doesn't provide a way to
2906 * know the initiator's LOOP ID, hence we can't find
2907 * the session and, so, the command.
2909 return -EAGAIN;
2910 } else
2911 qpair->req->outstanding_cmds[h] = (srb_t *)prm->cmd;
2913 pkt->handle = MAKE_HANDLE(qpair->req->id, h);
2914 pkt->handle |= CTIO_COMPLETION_HANDLE_MARK;
2915 pkt->nport_handle = cpu_to_le16(prm->cmd->loop_id);
2916 pkt->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
2917 pkt->initiator_id[0] = atio->u.isp24.fcp_hdr.s_id[2];
2918 pkt->initiator_id[1] = atio->u.isp24.fcp_hdr.s_id[1];
2919 pkt->initiator_id[2] = atio->u.isp24.fcp_hdr.s_id[0];
2920 pkt->exchange_addr = atio->u.isp24.exchange_addr;
2922 /* silence compile warning */
2923 t16 = be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id);
2924 pkt->ox_id = cpu_to_le16(t16);
2926 t16 = (atio->u.isp24.attr << 9);
2927 pkt->flags |= cpu_to_le16(t16);
2928 pkt->relative_offset = cpu_to_le32(prm->cmd->offset);
2930 /* Set transfer direction */
2931 if (cmd->dma_data_direction == DMA_TO_DEVICE)
2932 pkt->flags = cpu_to_le16(CTIO7_FLAGS_DATA_IN);
2933 else if (cmd->dma_data_direction == DMA_FROM_DEVICE)
2934 pkt->flags = cpu_to_le16(CTIO7_FLAGS_DATA_OUT);
2936 pkt->dseg_count = prm->tot_dsds;
2937 /* Fibre channel byte count */
2938 pkt->transfer_length = cpu_to_le32(transfer_length);
2940 /* ----- CRC context -------- */
2942 /* Allocate CRC context from global pool */
2943 crc_ctx_pkt = cmd->ctx =
2944 dma_pool_alloc(ha->dl_dma_pool, GFP_ATOMIC, &crc_ctx_dma);
2946 if (!crc_ctx_pkt)
2947 goto crc_queuing_error;
2949 /* Zero out CTX area. */
2950 clr_ptr = (uint8_t *)crc_ctx_pkt;
2951 memset(clr_ptr, 0, sizeof(*crc_ctx_pkt));
2953 crc_ctx_pkt->crc_ctx_dma = crc_ctx_dma;
2954 INIT_LIST_HEAD(&crc_ctx_pkt->dsd_list);
2956 /* Set handle */
2957 crc_ctx_pkt->handle = pkt->handle;
2959 qla_tgt_set_dif_tags(cmd, crc_ctx_pkt, &fw_prot_opts);
2961 pkt->crc_context_address[0] = cpu_to_le32(LSD(crc_ctx_dma));
2962 pkt->crc_context_address[1] = cpu_to_le32(MSD(crc_ctx_dma));
2963 pkt->crc_context_len = CRC_CONTEXT_LEN_FW;
2965 if (!bundling) {
2966 cur_dsd = (uint32_t *) &crc_ctx_pkt->u.nobundling.data_address;
2967 } else {
2969 * Configure Bundling if we need to fetch interlaving
2970 * protection PCI accesses
2972 fw_prot_opts |= PO_ENABLE_DIF_BUNDLING;
2973 crc_ctx_pkt->u.bundling.dif_byte_count = cpu_to_le32(dif_bytes);
2974 crc_ctx_pkt->u.bundling.dseg_count =
2975 cpu_to_le16(prm->tot_dsds - prm->prot_seg_cnt);
2976 cur_dsd = (uint32_t *) &crc_ctx_pkt->u.bundling.data_address;
2979 /* Finish the common fields of CRC pkt */
2980 crc_ctx_pkt->blk_size = cpu_to_le16(cmd->blk_sz);
2981 crc_ctx_pkt->prot_opts = cpu_to_le16(fw_prot_opts);
2982 crc_ctx_pkt->byte_count = cpu_to_le32(data_bytes);
2983 crc_ctx_pkt->guard_seed = cpu_to_le16(0);
2985 memset((uint8_t *)&tc, 0 , sizeof(tc));
2986 tc.vha = vha;
2987 tc.blk_sz = cmd->blk_sz;
2988 tc.bufflen = cmd->bufflen;
2989 tc.sg = cmd->sg;
2990 tc.prot_sg = cmd->prot_sg;
2991 tc.ctx = crc_ctx_pkt;
2992 tc.ctx_dsd_alloced = &cmd->ctx_dsd_alloced;
2994 /* Walks data segments */
2995 pkt->flags |= cpu_to_le16(CTIO7_FLAGS_DSD_PTR);
2997 if (!bundling && prm->prot_seg_cnt) {
2998 if (qla24xx_walk_and_build_sglist_no_difb(ha, NULL, cur_dsd,
2999 prm->tot_dsds, &tc))
3000 goto crc_queuing_error;
3001 } else if (qla24xx_walk_and_build_sglist(ha, NULL, cur_dsd,
3002 (prm->tot_dsds - prm->prot_seg_cnt), &tc))
3003 goto crc_queuing_error;
3005 if (bundling && prm->prot_seg_cnt) {
3006 /* Walks dif segments */
3007 pkt->add_flags |= CTIO_CRC2_AF_DIF_DSD_ENA;
3009 cur_dsd = (uint32_t *) &crc_ctx_pkt->u.bundling.dif_address;
3010 if (qla24xx_walk_and_build_prot_sglist(ha, NULL, cur_dsd,
3011 prm->prot_seg_cnt, &tc))
3012 goto crc_queuing_error;
3014 return QLA_SUCCESS;
3016 crc_queuing_error:
3017 /* Cleanup will be performed by the caller */
3018 qpair->req->outstanding_cmds[h] = NULL;
3020 return QLA_FUNCTION_FAILED;
3024 * Callback to setup response of xmit_type of QLA_TGT_XMIT_DATA and *
3025 * QLA_TGT_XMIT_STATUS for >= 24xx silicon
3027 int qlt_xmit_response(struct qla_tgt_cmd *cmd, int xmit_type,
3028 uint8_t scsi_status)
3030 struct scsi_qla_host *vha = cmd->vha;
3031 struct qla_qpair *qpair = cmd->qpair;
3032 struct ctio7_to_24xx *pkt;
3033 struct qla_tgt_prm prm;
3034 uint32_t full_req_cnt = 0;
3035 unsigned long flags = 0;
3036 int res;
3038 if (cmd->sess && cmd->sess->deleted) {
3039 cmd->state = QLA_TGT_STATE_PROCESSED;
3040 if (cmd->sess->logout_completed)
3041 /* no need to terminate. FW already freed exchange. */
3042 qlt_abort_cmd_on_host_reset(cmd->vha, cmd);
3043 else
3044 qlt_send_term_exchange(qpair, cmd, &cmd->atio, 0, 0);
3045 return 0;
3048 ql_dbg_qp(ql_dbg_tgt, qpair, 0xe018,
3049 "is_send_status=%d, cmd->bufflen=%d, cmd->sg_cnt=%d, cmd->dma_data_direction=%d se_cmd[%p] qp %d\n",
3050 (xmit_type & QLA_TGT_XMIT_STATUS) ?
3051 1 : 0, cmd->bufflen, cmd->sg_cnt, cmd->dma_data_direction,
3052 &cmd->se_cmd, qpair->id);
3054 res = qlt_pre_xmit_response(cmd, &prm, xmit_type, scsi_status,
3055 &full_req_cnt);
3056 if (unlikely(res != 0)) {
3057 return res;
3060 spin_lock_irqsave(qpair->qp_lock_ptr, flags);
3062 if (xmit_type == QLA_TGT_XMIT_STATUS)
3063 qpair->tgt_counters.core_qla_snd_status++;
3064 else
3065 qpair->tgt_counters.core_qla_que_buf++;
3067 if (!qpair->fw_started || cmd->reset_count != qpair->chip_reset) {
3069 * Either the port is not online or this request was from
3070 * previous life, just abort the processing.
3072 cmd->state = QLA_TGT_STATE_PROCESSED;
3073 qlt_abort_cmd_on_host_reset(cmd->vha, cmd);
3074 ql_dbg_qp(ql_dbg_async, qpair, 0xe101,
3075 "RESET-RSP online/active/old-count/new-count = %d/%d/%d/%d.\n",
3076 vha->flags.online, qla2x00_reset_active(vha),
3077 cmd->reset_count, qpair->chip_reset);
3078 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
3079 return 0;
3082 /* Does F/W have an IOCBs for this request */
3083 res = qlt_check_reserve_free_req(qpair, full_req_cnt);
3084 if (unlikely(res))
3085 goto out_unmap_unlock;
3087 if (cmd->se_cmd.prot_op && (xmit_type & QLA_TGT_XMIT_DATA))
3088 res = qlt_build_ctio_crc2_pkt(qpair, &prm);
3089 else
3090 res = qlt_24xx_build_ctio_pkt(qpair, &prm);
3091 if (unlikely(res != 0)) {
3092 qpair->req->cnt += full_req_cnt;
3093 goto out_unmap_unlock;
3096 pkt = (struct ctio7_to_24xx *)prm.pkt;
3098 if (qlt_has_data(cmd) && (xmit_type & QLA_TGT_XMIT_DATA)) {
3099 pkt->u.status0.flags |=
3100 cpu_to_le16(CTIO7_FLAGS_DATA_IN |
3101 CTIO7_FLAGS_STATUS_MODE_0);
3103 if (cmd->se_cmd.prot_op == TARGET_PROT_NORMAL)
3104 qlt_load_data_segments(&prm);
3106 if (prm.add_status_pkt == 0) {
3107 if (xmit_type & QLA_TGT_XMIT_STATUS) {
3108 pkt->u.status0.scsi_status =
3109 cpu_to_le16(prm.rq_result);
3110 pkt->u.status0.residual =
3111 cpu_to_le32(prm.residual);
3112 pkt->u.status0.flags |= cpu_to_le16(
3113 CTIO7_FLAGS_SEND_STATUS);
3114 if (qlt_need_explicit_conf(cmd, 0)) {
3115 pkt->u.status0.flags |=
3116 cpu_to_le16(
3117 CTIO7_FLAGS_EXPLICIT_CONFORM |
3118 CTIO7_FLAGS_CONFORM_REQ);
3122 } else {
3124 * We have already made sure that there is sufficient
3125 * amount of request entries to not drop HW lock in
3126 * req_pkt().
3128 struct ctio7_to_24xx *ctio =
3129 (struct ctio7_to_24xx *)qlt_get_req_pkt(
3130 qpair->req);
3132 ql_dbg_qp(ql_dbg_tgt, qpair, 0x305e,
3133 "Building additional status packet 0x%p.\n",
3134 ctio);
3137 * T10Dif: ctio_crc2_to_fw overlay ontop of
3138 * ctio7_to_24xx
3140 memcpy(ctio, pkt, sizeof(*ctio));
3141 /* reset back to CTIO7 */
3142 ctio->entry_count = 1;
3143 ctio->entry_type = CTIO_TYPE7;
3144 ctio->dseg_count = 0;
3145 ctio->u.status1.flags &= ~cpu_to_le16(
3146 CTIO7_FLAGS_DATA_IN);
3148 /* Real finish is ctio_m1's finish */
3149 pkt->handle |= CTIO_INTERMEDIATE_HANDLE_MARK;
3150 pkt->u.status0.flags |= cpu_to_le16(
3151 CTIO7_FLAGS_DONT_RET_CTIO);
3153 /* qlt_24xx_init_ctio_to_isp will correct
3154 * all neccessary fields that's part of CTIO7.
3155 * There should be no residual of CTIO-CRC2 data.
3157 qlt_24xx_init_ctio_to_isp((struct ctio7_to_24xx *)ctio,
3158 &prm);
3160 } else
3161 qlt_24xx_init_ctio_to_isp(pkt, &prm);
3164 cmd->state = QLA_TGT_STATE_PROCESSED; /* Mid-level is done processing */
3165 cmd->cmd_sent_to_fw = 1;
3167 /* Memory Barrier */
3168 wmb();
3169 if (qpair->reqq_start_iocbs)
3170 qpair->reqq_start_iocbs(qpair);
3171 else
3172 qla2x00_start_iocbs(vha, qpair->req);
3173 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
3175 return 0;
3177 out_unmap_unlock:
3178 qlt_unmap_sg(vha, cmd);
3179 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
3181 return res;
3183 EXPORT_SYMBOL(qlt_xmit_response);
3185 int qlt_rdy_to_xfer(struct qla_tgt_cmd *cmd)
3187 struct ctio7_to_24xx *pkt;
3188 struct scsi_qla_host *vha = cmd->vha;
3189 struct qla_tgt *tgt = cmd->tgt;
3190 struct qla_tgt_prm prm;
3191 unsigned long flags = 0;
3192 int res = 0;
3193 struct qla_qpair *qpair = cmd->qpair;
3195 memset(&prm, 0, sizeof(prm));
3196 prm.cmd = cmd;
3197 prm.tgt = tgt;
3198 prm.sg = NULL;
3199 prm.req_cnt = 1;
3201 /* Calculate number of entries and segments required */
3202 if (qlt_pci_map_calc_cnt(&prm) != 0)
3203 return -EAGAIN;
3205 if (!qpair->fw_started || (cmd->reset_count != qpair->chip_reset) ||
3206 (cmd->sess && cmd->sess->deleted)) {
3208 * Either the port is not online or this request was from
3209 * previous life, just abort the processing.
3211 cmd->state = QLA_TGT_STATE_NEED_DATA;
3212 qlt_abort_cmd_on_host_reset(cmd->vha, cmd);
3213 ql_dbg_qp(ql_dbg_async, qpair, 0xe102,
3214 "RESET-XFR online/active/old-count/new-count = %d/%d/%d/%d.\n",
3215 vha->flags.online, qla2x00_reset_active(vha),
3216 cmd->reset_count, qpair->chip_reset);
3217 return 0;
3220 spin_lock_irqsave(qpair->qp_lock_ptr, flags);
3221 /* Does F/W have an IOCBs for this request */
3222 res = qlt_check_reserve_free_req(qpair, prm.req_cnt);
3223 if (res != 0)
3224 goto out_unlock_free_unmap;
3225 if (cmd->se_cmd.prot_op)
3226 res = qlt_build_ctio_crc2_pkt(qpair, &prm);
3227 else
3228 res = qlt_24xx_build_ctio_pkt(qpair, &prm);
3230 if (unlikely(res != 0)) {
3231 qpair->req->cnt += prm.req_cnt;
3232 goto out_unlock_free_unmap;
3235 pkt = (struct ctio7_to_24xx *)prm.pkt;
3236 pkt->u.status0.flags |= cpu_to_le16(CTIO7_FLAGS_DATA_OUT |
3237 CTIO7_FLAGS_STATUS_MODE_0);
3239 if (cmd->se_cmd.prot_op == TARGET_PROT_NORMAL)
3240 qlt_load_data_segments(&prm);
3242 cmd->state = QLA_TGT_STATE_NEED_DATA;
3243 cmd->cmd_sent_to_fw = 1;
3245 /* Memory Barrier */
3246 wmb();
3247 if (qpair->reqq_start_iocbs)
3248 qpair->reqq_start_iocbs(qpair);
3249 else
3250 qla2x00_start_iocbs(vha, qpair->req);
3251 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
3253 return res;
3255 out_unlock_free_unmap:
3256 qlt_unmap_sg(vha, cmd);
3257 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
3259 return res;
3261 EXPORT_SYMBOL(qlt_rdy_to_xfer);
3265 * it is assumed either hardware_lock or qpair lock is held.
3267 static void
3268 qlt_handle_dif_error(struct qla_qpair *qpair, struct qla_tgt_cmd *cmd,
3269 struct ctio_crc_from_fw *sts)
3271 uint8_t *ap = &sts->actual_dif[0];
3272 uint8_t *ep = &sts->expected_dif[0];
3273 uint64_t lba = cmd->se_cmd.t_task_lba;
3274 uint8_t scsi_status, sense_key, asc, ascq;
3275 unsigned long flags;
3276 struct scsi_qla_host *vha = cmd->vha;
3278 cmd->trc_flags |= TRC_DIF_ERR;
3280 cmd->a_guard = be16_to_cpu(*(uint16_t *)(ap + 0));
3281 cmd->a_app_tag = be16_to_cpu(*(uint16_t *)(ap + 2));
3282 cmd->a_ref_tag = be32_to_cpu(*(uint32_t *)(ap + 4));
3284 cmd->e_guard = be16_to_cpu(*(uint16_t *)(ep + 0));
3285 cmd->e_app_tag = be16_to_cpu(*(uint16_t *)(ep + 2));
3286 cmd->e_ref_tag = be32_to_cpu(*(uint32_t *)(ep + 4));
3288 ql_dbg(ql_dbg_tgt_dif, vha, 0xf075,
3289 "%s: aborted %d state %d\n", __func__, cmd->aborted, cmd->state);
3291 scsi_status = sense_key = asc = ascq = 0;
3293 /* check appl tag */
3294 if (cmd->e_app_tag != cmd->a_app_tag) {
3295 ql_dbg(ql_dbg_tgt_dif, vha, 0xe00d,
3296 "App Tag ERR: cdb[%x] lba[%llx %llx] blks[%x] [Actual|Expected] Ref[%x|%x], App[%x|%x], Guard [%x|%x] cmd=%p ox_id[%04x]",
3297 cmd->cdb[0], lba, (lba+cmd->num_blks), cmd->num_blks,
3298 cmd->a_ref_tag, cmd->e_ref_tag, cmd->a_app_tag,
3299 cmd->e_app_tag, cmd->a_guard, cmd->e_guard, cmd,
3300 cmd->atio.u.isp24.fcp_hdr.ox_id);
3302 cmd->dif_err_code = DIF_ERR_APP;
3303 scsi_status = SAM_STAT_CHECK_CONDITION;
3304 sense_key = ABORTED_COMMAND;
3305 asc = 0x10;
3306 ascq = 0x2;
3309 /* check ref tag */
3310 if (cmd->e_ref_tag != cmd->a_ref_tag) {
3311 ql_dbg(ql_dbg_tgt_dif, vha, 0xe00e,
3312 "Ref Tag ERR: cdb[%x] lba[%llx %llx] blks[%x] [Actual|Expected] Ref[%x|%x], App[%x|%x], Guard[%x|%x] cmd=%p ox_id[%04x] ",
3313 cmd->cdb[0], lba, (lba+cmd->num_blks), cmd->num_blks,
3314 cmd->a_ref_tag, cmd->e_ref_tag, cmd->a_app_tag,
3315 cmd->e_app_tag, cmd->a_guard, cmd->e_guard, cmd,
3316 cmd->atio.u.isp24.fcp_hdr.ox_id);
3318 cmd->dif_err_code = DIF_ERR_REF;
3319 scsi_status = SAM_STAT_CHECK_CONDITION;
3320 sense_key = ABORTED_COMMAND;
3321 asc = 0x10;
3322 ascq = 0x3;
3323 goto out;
3326 /* check guard */
3327 if (cmd->e_guard != cmd->a_guard) {
3328 ql_dbg(ql_dbg_tgt_dif, vha, 0xe012,
3329 "Guard ERR: cdb[%x] lba[%llx %llx] blks[%x] [Actual|Expected] Ref[%x|%x], App[%x|%x], Guard [%x|%x] cmd=%p ox_id[%04x]",
3330 cmd->cdb[0], lba, (lba+cmd->num_blks), cmd->num_blks,
3331 cmd->a_ref_tag, cmd->e_ref_tag, cmd->a_app_tag,
3332 cmd->e_app_tag, cmd->a_guard, cmd->e_guard, cmd,
3333 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(qpair, cmd, scsi_status, sense_key, asc,
3358 ascq);
3359 /* assume scsi status gets out on the wire.
3360 * Will not wait for completion.
3362 vha->hw->tgt.tgt_ops->free_cmd(cmd);
3363 break;
3367 /* If hardware_lock held on entry, might drop it, then reaquire */
3368 /* This function sends the appropriate CTIO to ISP 2xxx or 24xx */
3369 static int __qlt_send_term_imm_notif(struct scsi_qla_host *vha,
3370 struct imm_ntfy_from_isp *ntfy)
3372 struct nack_to_isp *nack;
3373 struct qla_hw_data *ha = vha->hw;
3374 request_t *pkt;
3375 int ret = 0;
3377 ql_dbg(ql_dbg_tgt_tmr, vha, 0xe01c,
3378 "Sending TERM ELS CTIO (ha=%p)\n", ha);
3380 pkt = (request_t *)qla2x00_alloc_iocbs(vha, NULL);
3381 if (pkt == NULL) {
3382 ql_dbg(ql_dbg_tgt, vha, 0xe080,
3383 "qla_target(%d): %s failed: unable to allocate "
3384 "request packet\n", vha->vp_idx, __func__);
3385 return -ENOMEM;
3388 pkt->entry_type = NOTIFY_ACK_TYPE;
3389 pkt->entry_count = 1;
3390 pkt->handle = QLA_TGT_SKIP_HANDLE;
3392 nack = (struct nack_to_isp *)pkt;
3393 nack->ox_id = ntfy->ox_id;
3395 nack->u.isp24.nport_handle = ntfy->u.isp24.nport_handle;
3396 if (le16_to_cpu(ntfy->u.isp24.status) == IMM_NTFY_ELS) {
3397 nack->u.isp24.flags = ntfy->u.isp24.flags &
3398 __constant_cpu_to_le32(NOTIFY24XX_FLAGS_PUREX_IOCB);
3401 /* terminate */
3402 nack->u.isp24.flags |=
3403 __constant_cpu_to_le16(NOTIFY_ACK_FLAGS_TERMINATE);
3405 nack->u.isp24.srr_rx_id = ntfy->u.isp24.srr_rx_id;
3406 nack->u.isp24.status = ntfy->u.isp24.status;
3407 nack->u.isp24.status_subcode = ntfy->u.isp24.status_subcode;
3408 nack->u.isp24.fw_handle = ntfy->u.isp24.fw_handle;
3409 nack->u.isp24.exchange_address = ntfy->u.isp24.exchange_address;
3410 nack->u.isp24.srr_rel_offs = ntfy->u.isp24.srr_rel_offs;
3411 nack->u.isp24.srr_ui = ntfy->u.isp24.srr_ui;
3412 nack->u.isp24.vp_index = ntfy->u.isp24.vp_index;
3414 qla2x00_start_iocbs(vha, vha->req);
3415 return ret;
3418 static void qlt_send_term_imm_notif(struct scsi_qla_host *vha,
3419 struct imm_ntfy_from_isp *imm, int ha_locked)
3421 unsigned long flags = 0;
3422 int rc;
3424 if (ha_locked) {
3425 rc = __qlt_send_term_imm_notif(vha, imm);
3427 #if 0 /* Todo */
3428 if (rc == -ENOMEM)
3429 qlt_alloc_qfull_cmd(vha, imm, 0, 0);
3430 #else
3431 if (rc) {
3433 #endif
3434 goto done;
3437 spin_lock_irqsave(&vha->hw->hardware_lock, flags);
3438 rc = __qlt_send_term_imm_notif(vha, imm);
3440 #if 0 /* Todo */
3441 if (rc == -ENOMEM)
3442 qlt_alloc_qfull_cmd(vha, imm, 0, 0);
3443 #endif
3445 done:
3446 if (!ha_locked)
3447 spin_unlock_irqrestore(&vha->hw->hardware_lock, flags);
3451 * If hardware_lock held on entry, might drop it, then reaquire
3452 * This function sends the appropriate CTIO to ISP 2xxx or 24xx
3454 static int __qlt_send_term_exchange(struct qla_qpair *qpair,
3455 struct qla_tgt_cmd *cmd,
3456 struct atio_from_isp *atio)
3458 struct scsi_qla_host *vha = qpair->vha;
3459 struct ctio7_to_24xx *ctio24;
3460 struct qla_hw_data *ha = vha->hw;
3461 request_t *pkt;
3462 int ret = 0;
3463 uint16_t temp;
3465 ql_dbg(ql_dbg_tgt, vha, 0xe009, "Sending TERM EXCH CTIO (ha=%p)\n", ha);
3467 pkt = (request_t *)qla2x00_alloc_iocbs_ready(qpair, NULL);
3468 if (pkt == NULL) {
3469 ql_dbg(ql_dbg_tgt, vha, 0xe050,
3470 "qla_target(%d): %s failed: unable to allocate "
3471 "request packet\n", vha->vp_idx, __func__);
3472 return -ENOMEM;
3475 if (cmd != NULL) {
3476 if (cmd->state < QLA_TGT_STATE_PROCESSED) {
3477 ql_dbg(ql_dbg_tgt, vha, 0xe051,
3478 "qla_target(%d): Terminating cmd %p with "
3479 "incorrect state %d\n", vha->vp_idx, cmd,
3480 cmd->state);
3481 } else
3482 ret = 1;
3485 qpair->tgt_counters.num_term_xchg_sent++;
3486 pkt->entry_count = 1;
3487 pkt->handle = QLA_TGT_SKIP_HANDLE | CTIO_COMPLETION_HANDLE_MARK;
3489 ctio24 = (struct ctio7_to_24xx *)pkt;
3490 ctio24->entry_type = CTIO_TYPE7;
3491 ctio24->nport_handle = CTIO7_NHANDLE_UNRECOGNIZED;
3492 ctio24->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
3493 ctio24->vp_index = vha->vp_idx;
3494 ctio24->initiator_id[0] = atio->u.isp24.fcp_hdr.s_id[2];
3495 ctio24->initiator_id[1] = atio->u.isp24.fcp_hdr.s_id[1];
3496 ctio24->initiator_id[2] = atio->u.isp24.fcp_hdr.s_id[0];
3497 ctio24->exchange_addr = atio->u.isp24.exchange_addr;
3498 temp = (atio->u.isp24.attr << 9) | CTIO7_FLAGS_STATUS_MODE_1 |
3499 CTIO7_FLAGS_TERMINATE;
3500 ctio24->u.status1.flags = cpu_to_le16(temp);
3501 temp = be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id);
3502 ctio24->u.status1.ox_id = cpu_to_le16(temp);
3504 /* Most likely, it isn't needed */
3505 ctio24->u.status1.residual = get_unaligned((uint32_t *)
3506 &atio->u.isp24.fcp_cmnd.add_cdb[
3507 atio->u.isp24.fcp_cmnd.add_cdb_len]);
3508 if (ctio24->u.status1.residual != 0)
3509 ctio24->u.status1.scsi_status |= SS_RESIDUAL_UNDER;
3511 /* Memory Barrier */
3512 wmb();
3513 if (qpair->reqq_start_iocbs)
3514 qpair->reqq_start_iocbs(qpair);
3515 else
3516 qla2x00_start_iocbs(vha, qpair->req);
3517 return ret;
3520 static void qlt_send_term_exchange(struct qla_qpair *qpair,
3521 struct qla_tgt_cmd *cmd, struct atio_from_isp *atio, int ha_locked,
3522 int ul_abort)
3524 struct scsi_qla_host *vha;
3525 unsigned long flags = 0;
3526 int rc;
3528 /* why use different vha? NPIV */
3529 if (cmd)
3530 vha = cmd->vha;
3531 else
3532 vha = qpair->vha;
3534 if (ha_locked) {
3535 rc = __qlt_send_term_exchange(qpair, cmd, atio);
3536 if (rc == -ENOMEM)
3537 qlt_alloc_qfull_cmd(vha, atio, 0, 0);
3538 goto done;
3540 spin_lock_irqsave(qpair->qp_lock_ptr, flags);
3541 rc = __qlt_send_term_exchange(qpair, cmd, atio);
3542 if (rc == -ENOMEM)
3543 qlt_alloc_qfull_cmd(vha, atio, 0, 0);
3545 done:
3546 if (cmd && !ul_abort && !cmd->aborted) {
3547 if (cmd->sg_mapped)
3548 qlt_unmap_sg(vha, cmd);
3549 vha->hw->tgt.tgt_ops->free_cmd(cmd);
3552 if (!ha_locked)
3553 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
3555 return;
3558 static void qlt_init_term_exchange(struct scsi_qla_host *vha)
3560 struct list_head free_list;
3561 struct qla_tgt_cmd *cmd, *tcmd;
3563 vha->hw->tgt.leak_exchg_thresh_hold =
3564 (vha->hw->cur_fw_xcb_count/100) * LEAK_EXCHG_THRESH_HOLD_PERCENT;
3566 cmd = tcmd = NULL;
3567 if (!list_empty(&vha->hw->tgt.q_full_list)) {
3568 INIT_LIST_HEAD(&free_list);
3569 list_splice_init(&vha->hw->tgt.q_full_list, &free_list);
3571 list_for_each_entry_safe(cmd, tcmd, &free_list, cmd_list) {
3572 list_del(&cmd->cmd_list);
3573 /* This cmd was never sent to TCM. There is no need
3574 * to schedule free or call free_cmd
3576 qlt_free_cmd(cmd);
3577 vha->hw->tgt.num_qfull_cmds_alloc--;
3580 vha->hw->tgt.num_qfull_cmds_dropped = 0;
3583 static void qlt_chk_exch_leak_thresh_hold(struct scsi_qla_host *vha)
3585 uint32_t total_leaked;
3587 total_leaked = vha->hw->tgt.num_qfull_cmds_dropped;
3589 if (vha->hw->tgt.leak_exchg_thresh_hold &&
3590 (total_leaked > vha->hw->tgt.leak_exchg_thresh_hold)) {
3592 ql_dbg(ql_dbg_tgt, vha, 0xe079,
3593 "Chip reset due to exchange starvation: %d/%d.\n",
3594 total_leaked, vha->hw->cur_fw_xcb_count);
3596 if (IS_P3P_TYPE(vha->hw))
3597 set_bit(FCOE_CTX_RESET_NEEDED, &vha->dpc_flags);
3598 else
3599 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
3600 qla2xxx_wake_dpc(vha);
3605 int qlt_abort_cmd(struct qla_tgt_cmd *cmd)
3607 struct qla_tgt *tgt = cmd->tgt;
3608 struct scsi_qla_host *vha = tgt->vha;
3609 struct se_cmd *se_cmd = &cmd->se_cmd;
3610 unsigned long flags;
3612 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf014,
3613 "qla_target(%d): terminating exchange for aborted cmd=%p "
3614 "(se_cmd=%p, tag=%llu)", vha->vp_idx, cmd, &cmd->se_cmd,
3615 se_cmd->tag);
3617 spin_lock_irqsave(&cmd->cmd_lock, flags);
3618 if (cmd->aborted) {
3619 spin_unlock_irqrestore(&cmd->cmd_lock, flags);
3621 * It's normal to see 2 calls in this path:
3622 * 1) XFER Rdy completion + CMD_T_ABORT
3623 * 2) TCM TMR - drain_state_list
3625 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf016,
3626 "multiple abort. %p transport_state %x, t_state %x, "
3627 "se_cmd_flags %x\n", cmd, cmd->se_cmd.transport_state,
3628 cmd->se_cmd.t_state, cmd->se_cmd.se_cmd_flags);
3629 return EIO;
3631 cmd->aborted = 1;
3632 cmd->trc_flags |= TRC_ABORT;
3633 spin_unlock_irqrestore(&cmd->cmd_lock, flags);
3635 qlt_send_term_exchange(cmd->qpair, cmd, &cmd->atio, 0, 1);
3636 return 0;
3638 EXPORT_SYMBOL(qlt_abort_cmd);
3640 void qlt_free_cmd(struct qla_tgt_cmd *cmd)
3642 struct fc_port *sess = cmd->sess;
3644 ql_dbg(ql_dbg_tgt, cmd->vha, 0xe074,
3645 "%s: se_cmd[%p] ox_id %04x\n",
3646 __func__, &cmd->se_cmd,
3647 be16_to_cpu(cmd->atio.u.isp24.fcp_hdr.ox_id));
3649 BUG_ON(cmd->cmd_in_wq);
3651 if (cmd->sg_mapped)
3652 qlt_unmap_sg(cmd->vha, cmd);
3654 if (!cmd->q_full)
3655 qlt_decr_num_pend_cmds(cmd->vha);
3657 BUG_ON(cmd->sg_mapped);
3658 cmd->jiffies_at_free = get_jiffies_64();
3659 if (unlikely(cmd->free_sg))
3660 kfree(cmd->sg);
3662 if (!sess || !sess->se_sess) {
3663 WARN_ON(1);
3664 return;
3666 cmd->jiffies_at_free = get_jiffies_64();
3667 percpu_ida_free(&sess->se_sess->sess_tag_pool, cmd->se_cmd.map_tag);
3669 EXPORT_SYMBOL(qlt_free_cmd);
3672 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
3674 static int qlt_term_ctio_exchange(struct qla_qpair *qpair, void *ctio,
3675 struct qla_tgt_cmd *cmd, uint32_t status)
3677 int term = 0;
3678 struct scsi_qla_host *vha = qpair->vha;
3680 if (cmd->se_cmd.prot_op)
3681 ql_dbg(ql_dbg_tgt_dif, vha, 0xe013,
3682 "Term DIF cmd: lba[0x%llx|%lld] len[0x%x] "
3683 "se_cmd=%p tag[%x] op %#x/%s",
3684 cmd->lba, cmd->lba,
3685 cmd->num_blks, &cmd->se_cmd,
3686 cmd->atio.u.isp24.exchange_addr,
3687 cmd->se_cmd.prot_op,
3688 prot_op_str(cmd->se_cmd.prot_op));
3690 if (ctio != NULL) {
3691 struct ctio7_from_24xx *c = (struct ctio7_from_24xx *)ctio;
3692 term = !(c->flags &
3693 cpu_to_le16(OF_TERM_EXCH));
3694 } else
3695 term = 1;
3697 if (term)
3698 qlt_term_ctio_exchange(qpair, ctio, cmd, status);
3700 return term;
3704 /* ha->hardware_lock supposed to be held on entry */
3705 static struct qla_tgt_cmd *qlt_ctio_to_cmd(struct scsi_qla_host *vha,
3706 struct rsp_que *rsp, uint32_t handle, void *ctio)
3708 struct qla_tgt_cmd *cmd = NULL;
3709 struct req_que *req;
3710 int qid = GET_QID(handle);
3711 uint32_t h = handle & ~QLA_TGT_HANDLE_MASK;
3713 if (unlikely(h == QLA_TGT_SKIP_HANDLE))
3714 return NULL;
3716 if (qid == rsp->req->id) {
3717 req = rsp->req;
3718 } else if (vha->hw->req_q_map[qid]) {
3719 ql_dbg(ql_dbg_tgt_mgt, vha, 0x1000a,
3720 "qla_target(%d): CTIO completion with different QID %d handle %x\n",
3721 vha->vp_idx, rsp->id, handle);
3722 req = vha->hw->req_q_map[qid];
3723 } else {
3724 return NULL;
3727 h &= QLA_CMD_HANDLE_MASK;
3729 if (h != QLA_TGT_NULL_HANDLE) {
3730 if (unlikely(h >= req->num_outstanding_cmds)) {
3731 ql_dbg(ql_dbg_tgt, vha, 0xe052,
3732 "qla_target(%d): Wrong handle %x received\n",
3733 vha->vp_idx, handle);
3734 return NULL;
3737 cmd = (struct qla_tgt_cmd *)req->outstanding_cmds[h];
3738 if (unlikely(cmd == NULL)) {
3739 ql_dbg(ql_dbg_async, vha, 0xe053,
3740 "qla_target(%d): Suspicious: unable to find the command with handle %x req->id %d rsp->id %d\n",
3741 vha->vp_idx, handle, req->id, rsp->id);
3742 return NULL;
3744 req->outstanding_cmds[h] = NULL;
3745 } else if (ctio != NULL) {
3746 /* We can't get loop ID from CTIO7 */
3747 ql_dbg(ql_dbg_tgt, vha, 0xe054,
3748 "qla_target(%d): Wrong CTIO received: QLA24xx doesn't "
3749 "support NULL handles\n", vha->vp_idx);
3750 return NULL;
3753 return cmd;
3756 /* hardware_lock should be held by caller. */
3757 void
3758 qlt_abort_cmd_on_host_reset(struct scsi_qla_host *vha, struct qla_tgt_cmd *cmd)
3760 struct qla_hw_data *ha = vha->hw;
3762 if (cmd->sg_mapped)
3763 qlt_unmap_sg(vha, cmd);
3765 /* TODO: fix debug message type and ids. */
3766 if (cmd->state == QLA_TGT_STATE_PROCESSED) {
3767 ql_dbg(ql_dbg_io, vha, 0xff00,
3768 "HOST-ABORT: state=PROCESSED.\n");
3769 } else if (cmd->state == QLA_TGT_STATE_NEED_DATA) {
3770 cmd->write_data_transferred = 0;
3771 cmd->state = QLA_TGT_STATE_DATA_IN;
3773 ql_dbg(ql_dbg_io, vha, 0xff01,
3774 "HOST-ABORT: state=DATA_IN.\n");
3776 ha->tgt.tgt_ops->handle_data(cmd);
3777 return;
3778 } else {
3779 ql_dbg(ql_dbg_io, vha, 0xff03,
3780 "HOST-ABORT: state=BAD(%d).\n",
3781 cmd->state);
3782 dump_stack();
3785 cmd->trc_flags |= TRC_FLUSH;
3786 ha->tgt.tgt_ops->free_cmd(cmd);
3790 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
3792 static void qlt_do_ctio_completion(struct scsi_qla_host *vha,
3793 struct rsp_que *rsp, uint32_t handle, uint32_t status, void *ctio)
3795 struct qla_hw_data *ha = vha->hw;
3796 struct se_cmd *se_cmd;
3797 struct qla_tgt_cmd *cmd;
3798 struct qla_qpair *qpair = rsp->qpair;
3800 if (handle & CTIO_INTERMEDIATE_HANDLE_MARK) {
3801 /* That could happen only in case of an error/reset/abort */
3802 if (status != CTIO_SUCCESS) {
3803 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01d,
3804 "Intermediate CTIO received"
3805 " (status %x)\n", status);
3807 return;
3810 cmd = qlt_ctio_to_cmd(vha, rsp, handle, ctio);
3811 if (cmd == NULL)
3812 return;
3814 se_cmd = &cmd->se_cmd;
3815 cmd->cmd_sent_to_fw = 0;
3817 qlt_unmap_sg(vha, cmd);
3819 if (unlikely(status != CTIO_SUCCESS)) {
3820 switch (status & 0xFFFF) {
3821 case CTIO_LIP_RESET:
3822 case CTIO_TARGET_RESET:
3823 case CTIO_ABORTED:
3824 /* driver request abort via Terminate exchange */
3825 case CTIO_TIMEOUT:
3826 case CTIO_INVALID_RX_ID:
3827 /* They are OK */
3828 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf058,
3829 "qla_target(%d): CTIO with "
3830 "status %#x received, state %x, se_cmd %p, "
3831 "(LIP_RESET=e, ABORTED=2, TARGET_RESET=17, "
3832 "TIMEOUT=b, INVALID_RX_ID=8)\n", vha->vp_idx,
3833 status, cmd->state, se_cmd);
3834 break;
3836 case CTIO_PORT_LOGGED_OUT:
3837 case CTIO_PORT_UNAVAILABLE:
3839 int logged_out =
3840 (status & 0xFFFF) == CTIO_PORT_LOGGED_OUT;
3842 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf059,
3843 "qla_target(%d): CTIO with %s status %x "
3844 "received (state %x, se_cmd %p)\n", vha->vp_idx,
3845 logged_out ? "PORT LOGGED OUT" : "PORT UNAVAILABLE",
3846 status, cmd->state, se_cmd);
3848 if (logged_out && cmd->sess) {
3850 * Session is already logged out, but we need
3851 * to notify initiator, who's not aware of this
3853 cmd->sess->logout_on_delete = 0;
3854 cmd->sess->send_els_logo = 1;
3855 ql_dbg(ql_dbg_disc, vha, 0x20f8,
3856 "%s %d %8phC post del sess\n",
3857 __func__, __LINE__, cmd->sess->port_name);
3859 qlt_schedule_sess_for_deletion_lock(cmd->sess);
3861 break;
3863 case CTIO_DIF_ERROR: {
3864 struct ctio_crc_from_fw *crc =
3865 (struct ctio_crc_from_fw *)ctio;
3866 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf073,
3867 "qla_target(%d): CTIO with DIF_ERROR status %x "
3868 "received (state %x, ulp_cmd %p) actual_dif[0x%llx] "
3869 "expect_dif[0x%llx]\n",
3870 vha->vp_idx, status, cmd->state, se_cmd,
3871 *((u64 *)&crc->actual_dif[0]),
3872 *((u64 *)&crc->expected_dif[0]));
3874 qlt_handle_dif_error(qpair, cmd, ctio);
3875 return;
3877 default:
3878 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05b,
3879 "qla_target(%d): CTIO with error status 0x%x received (state %x, se_cmd %p\n",
3880 vha->vp_idx, status, cmd->state, se_cmd);
3881 break;
3885 /* "cmd->aborted" means
3886 * cmd is already aborted/terminated, we don't
3887 * need to terminate again. The exchange is already
3888 * cleaned up/freed at FW level. Just cleanup at driver
3889 * level.
3891 if ((cmd->state != QLA_TGT_STATE_NEED_DATA) &&
3892 (!cmd->aborted)) {
3893 cmd->trc_flags |= TRC_CTIO_ERR;
3894 if (qlt_term_ctio_exchange(qpair, ctio, cmd, status))
3895 return;
3899 if (cmd->state == QLA_TGT_STATE_PROCESSED) {
3900 cmd->trc_flags |= TRC_CTIO_DONE;
3901 } else if (cmd->state == QLA_TGT_STATE_NEED_DATA) {
3902 cmd->state = QLA_TGT_STATE_DATA_IN;
3904 if (status == CTIO_SUCCESS)
3905 cmd->write_data_transferred = 1;
3907 ha->tgt.tgt_ops->handle_data(cmd);
3908 return;
3909 } else if (cmd->aborted) {
3910 cmd->trc_flags |= TRC_CTIO_ABORTED;
3911 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01e,
3912 "Aborted command %p (tag %lld) finished\n", cmd, se_cmd->tag);
3913 } else {
3914 cmd->trc_flags |= TRC_CTIO_STRANGE;
3915 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05c,
3916 "qla_target(%d): A command in state (%d) should "
3917 "not return a CTIO complete\n", vha->vp_idx, cmd->state);
3920 if (unlikely(status != CTIO_SUCCESS) &&
3921 !cmd->aborted) {
3922 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01f, "Finishing failed CTIO\n");
3923 dump_stack();
3926 ha->tgt.tgt_ops->free_cmd(cmd);
3929 static inline int qlt_get_fcp_task_attr(struct scsi_qla_host *vha,
3930 uint8_t task_codes)
3932 int fcp_task_attr;
3934 switch (task_codes) {
3935 case ATIO_SIMPLE_QUEUE:
3936 fcp_task_attr = TCM_SIMPLE_TAG;
3937 break;
3938 case ATIO_HEAD_OF_QUEUE:
3939 fcp_task_attr = TCM_HEAD_TAG;
3940 break;
3941 case ATIO_ORDERED_QUEUE:
3942 fcp_task_attr = TCM_ORDERED_TAG;
3943 break;
3944 case ATIO_ACA_QUEUE:
3945 fcp_task_attr = TCM_ACA_TAG;
3946 break;
3947 case ATIO_UNTAGGED:
3948 fcp_task_attr = TCM_SIMPLE_TAG;
3949 break;
3950 default:
3951 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05d,
3952 "qla_target: unknown task code %x, use ORDERED instead\n",
3953 task_codes);
3954 fcp_task_attr = TCM_ORDERED_TAG;
3955 break;
3958 return fcp_task_attr;
3961 static struct fc_port *qlt_make_local_sess(struct scsi_qla_host *,
3962 uint8_t *);
3964 * Process context for I/O path into tcm_qla2xxx code
3966 static void __qlt_do_work(struct qla_tgt_cmd *cmd)
3968 scsi_qla_host_t *vha = cmd->vha;
3969 struct qla_hw_data *ha = vha->hw;
3970 struct fc_port *sess = cmd->sess;
3971 struct atio_from_isp *atio = &cmd->atio;
3972 unsigned char *cdb;
3973 unsigned long flags;
3974 uint32_t data_length;
3975 int ret, fcp_task_attr, data_dir, bidi = 0;
3976 struct qla_qpair *qpair = cmd->qpair;
3978 cmd->cmd_in_wq = 0;
3979 cmd->trc_flags |= TRC_DO_WORK;
3981 if (cmd->aborted) {
3982 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf082,
3983 "cmd with tag %u is aborted\n",
3984 cmd->atio.u.isp24.exchange_addr);
3985 goto out_term;
3988 spin_lock_init(&cmd->cmd_lock);
3989 cdb = &atio->u.isp24.fcp_cmnd.cdb[0];
3990 cmd->se_cmd.tag = atio->u.isp24.exchange_addr;
3992 if (atio->u.isp24.fcp_cmnd.rddata &&
3993 atio->u.isp24.fcp_cmnd.wrdata) {
3994 bidi = 1;
3995 data_dir = DMA_TO_DEVICE;
3996 } else if (atio->u.isp24.fcp_cmnd.rddata)
3997 data_dir = DMA_FROM_DEVICE;
3998 else if (atio->u.isp24.fcp_cmnd.wrdata)
3999 data_dir = DMA_TO_DEVICE;
4000 else
4001 data_dir = DMA_NONE;
4003 fcp_task_attr = qlt_get_fcp_task_attr(vha,
4004 atio->u.isp24.fcp_cmnd.task_attr);
4005 data_length = be32_to_cpu(get_unaligned((uint32_t *)
4006 &atio->u.isp24.fcp_cmnd.add_cdb[
4007 atio->u.isp24.fcp_cmnd.add_cdb_len]));
4009 ret = ha->tgt.tgt_ops->handle_cmd(vha, cmd, cdb, data_length,
4010 fcp_task_attr, data_dir, bidi);
4011 if (ret != 0)
4012 goto out_term;
4014 * Drop extra session reference from qla_tgt_handle_cmd_for_atio*(
4016 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
4017 ha->tgt.tgt_ops->put_sess(sess);
4018 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
4019 return;
4021 out_term:
4022 ql_dbg(ql_dbg_io, vha, 0x3060, "Terminating work cmd %p", cmd);
4024 * cmd has not sent to target yet, so pass NULL as the second
4025 * argument to qlt_send_term_exchange() and free the memory here.
4027 cmd->trc_flags |= TRC_DO_WORK_ERR;
4028 spin_lock_irqsave(qpair->qp_lock_ptr, flags);
4029 qlt_send_term_exchange(qpair, NULL, &cmd->atio, 1, 0);
4031 qlt_decr_num_pend_cmds(vha);
4032 percpu_ida_free(&sess->se_sess->sess_tag_pool, cmd->se_cmd.map_tag);
4033 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
4035 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
4036 ha->tgt.tgt_ops->put_sess(sess);
4037 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
4040 static void qlt_do_work(struct work_struct *work)
4042 struct qla_tgt_cmd *cmd = container_of(work, struct qla_tgt_cmd, work);
4043 scsi_qla_host_t *vha = cmd->vha;
4044 unsigned long flags;
4046 spin_lock_irqsave(&vha->cmd_list_lock, flags);
4047 list_del(&cmd->cmd_list);
4048 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
4050 __qlt_do_work(cmd);
4053 void qlt_clr_qp_table(struct scsi_qla_host *vha)
4055 unsigned long flags;
4056 struct qla_hw_data *ha = vha->hw;
4057 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
4058 void *node;
4059 u64 key = 0;
4061 ql_log(ql_log_info, vha, 0x706c,
4062 "User update Number of Active Qpairs %d\n",
4063 ha->tgt.num_act_qpairs);
4065 spin_lock_irqsave(&ha->tgt.atio_lock, flags);
4067 btree_for_each_safe64(&tgt->lun_qpair_map, key, node)
4068 btree_remove64(&tgt->lun_qpair_map, key);
4070 ha->base_qpair->lun_cnt = 0;
4071 for (key = 0; key < ha->max_qpairs; key++)
4072 if (ha->queue_pair_map[key])
4073 ha->queue_pair_map[key]->lun_cnt = 0;
4075 spin_unlock_irqrestore(&ha->tgt.atio_lock, flags);
4078 static void qlt_assign_qpair(struct scsi_qla_host *vha,
4079 struct qla_tgt_cmd *cmd)
4081 struct qla_qpair *qpair, *qp;
4082 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
4083 struct qla_qpair_hint *h;
4085 if (vha->flags.qpairs_available) {
4086 h = btree_lookup64(&tgt->lun_qpair_map, cmd->unpacked_lun);
4087 if (unlikely(!h)) {
4088 /* spread lun to qpair ratio evently */
4089 int lcnt = 0, rc;
4090 struct scsi_qla_host *base_vha =
4091 pci_get_drvdata(vha->hw->pdev);
4093 qpair = vha->hw->base_qpair;
4094 if (qpair->lun_cnt == 0) {
4095 qpair->lun_cnt++;
4096 h = qla_qpair_to_hint(tgt, qpair);
4097 BUG_ON(!h);
4098 rc = btree_insert64(&tgt->lun_qpair_map,
4099 cmd->unpacked_lun, h, GFP_ATOMIC);
4100 if (rc) {
4101 qpair->lun_cnt--;
4102 ql_log(ql_log_info, vha, 0xd037,
4103 "Unable to insert lun %llx into lun_qpair_map\n",
4104 cmd->unpacked_lun);
4106 goto out;
4107 } else {
4108 lcnt = qpair->lun_cnt;
4111 h = NULL;
4112 list_for_each_entry(qp, &base_vha->qp_list,
4113 qp_list_elem) {
4114 if (qp->lun_cnt == 0) {
4115 qp->lun_cnt++;
4116 h = qla_qpair_to_hint(tgt, qp);
4117 BUG_ON(!h);
4118 rc = btree_insert64(&tgt->lun_qpair_map,
4119 cmd->unpacked_lun, h, GFP_ATOMIC);
4120 if (rc) {
4121 qp->lun_cnt--;
4122 ql_log(ql_log_info, vha, 0xd038,
4123 "Unable to insert lun %llx into lun_qpair_map\n",
4124 cmd->unpacked_lun);
4126 qpair = qp;
4127 goto out;
4128 } else {
4129 if (qp->lun_cnt < lcnt) {
4130 lcnt = qp->lun_cnt;
4131 qpair = qp;
4132 continue;
4136 BUG_ON(!qpair);
4137 qpair->lun_cnt++;
4138 h = qla_qpair_to_hint(tgt, qpair);
4139 BUG_ON(!h);
4140 rc = btree_insert64(&tgt->lun_qpair_map,
4141 cmd->unpacked_lun, h, GFP_ATOMIC);
4142 if (rc) {
4143 qpair->lun_cnt--;
4144 ql_log(ql_log_info, vha, 0xd039,
4145 "Unable to insert lun %llx into lun_qpair_map\n",
4146 cmd->unpacked_lun);
4149 } else {
4150 h = &tgt->qphints[0];
4152 out:
4153 cmd->qpair = h->qpair;
4154 cmd->se_cmd.cpuid = h->cpuid;
4157 static struct qla_tgt_cmd *qlt_get_tag(scsi_qla_host_t *vha,
4158 struct fc_port *sess,
4159 struct atio_from_isp *atio)
4161 struct se_session *se_sess = sess->se_sess;
4162 struct qla_tgt_cmd *cmd;
4163 int tag;
4165 tag = percpu_ida_alloc(&se_sess->sess_tag_pool, TASK_RUNNING);
4166 if (tag < 0)
4167 return NULL;
4169 cmd = &((struct qla_tgt_cmd *)se_sess->sess_cmd_map)[tag];
4170 memset(cmd, 0, sizeof(struct qla_tgt_cmd));
4171 cmd->cmd_type = TYPE_TGT_CMD;
4172 memcpy(&cmd->atio, atio, sizeof(*atio));
4173 cmd->state = QLA_TGT_STATE_NEW;
4174 cmd->tgt = vha->vha_tgt.qla_tgt;
4175 qlt_incr_num_pend_cmds(vha);
4176 cmd->vha = vha;
4177 cmd->se_cmd.map_tag = tag;
4178 cmd->sess = sess;
4179 cmd->loop_id = sess->loop_id;
4180 cmd->conf_compl_supported = sess->conf_compl_supported;
4182 cmd->trc_flags = 0;
4183 cmd->jiffies_at_alloc = get_jiffies_64();
4185 cmd->unpacked_lun = scsilun_to_int(
4186 (struct scsi_lun *)&atio->u.isp24.fcp_cmnd.lun);
4187 qlt_assign_qpair(vha, cmd);
4188 cmd->reset_count = vha->hw->base_qpair->chip_reset;
4189 cmd->vp_idx = vha->vp_idx;
4191 return cmd;
4194 static void qlt_create_sess_from_atio(struct work_struct *work)
4196 struct qla_tgt_sess_op *op = container_of(work,
4197 struct qla_tgt_sess_op, work);
4198 scsi_qla_host_t *vha = op->vha;
4199 struct qla_hw_data *ha = vha->hw;
4200 struct fc_port *sess;
4201 struct qla_tgt_cmd *cmd;
4202 unsigned long flags;
4203 uint8_t *s_id = op->atio.u.isp24.fcp_hdr.s_id;
4205 spin_lock_irqsave(&vha->cmd_list_lock, flags);
4206 list_del(&op->cmd_list);
4207 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
4209 if (op->aborted) {
4210 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf083,
4211 "sess_op with tag %u is aborted\n",
4212 op->atio.u.isp24.exchange_addr);
4213 goto out_term;
4216 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf022,
4217 "qla_target(%d): Unable to find wwn login"
4218 " (s_id %x:%x:%x), trying to create it manually\n",
4219 vha->vp_idx, s_id[0], s_id[1], s_id[2]);
4221 if (op->atio.u.raw.entry_count > 1) {
4222 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf023,
4223 "Dropping multy entry atio %p\n", &op->atio);
4224 goto out_term;
4227 sess = qlt_make_local_sess(vha, s_id);
4228 /* sess has an extra creation ref. */
4230 if (!sess)
4231 goto out_term;
4233 * Now obtain a pre-allocated session tag using the original op->atio
4234 * packet header, and dispatch into __qlt_do_work() using the existing
4235 * process context.
4237 cmd = qlt_get_tag(vha, sess, &op->atio);
4238 if (!cmd) {
4239 struct qla_qpair *qpair = ha->base_qpair;
4241 spin_lock_irqsave(qpair->qp_lock_ptr, flags);
4242 qlt_send_busy(qpair, &op->atio, SAM_STAT_BUSY);
4243 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
4245 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
4246 ha->tgt.tgt_ops->put_sess(sess);
4247 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
4248 kfree(op);
4249 return;
4253 * __qlt_do_work() will call qlt_put_sess() to release
4254 * the extra reference taken above by qlt_make_local_sess()
4256 __qlt_do_work(cmd);
4257 kfree(op);
4258 return;
4259 out_term:
4260 qlt_send_term_exchange(vha->hw->base_qpair, NULL, &op->atio, 0, 0);
4261 kfree(op);
4264 /* ha->hardware_lock supposed to be held on entry */
4265 static int qlt_handle_cmd_for_atio(struct scsi_qla_host *vha,
4266 struct atio_from_isp *atio)
4268 struct qla_hw_data *ha = vha->hw;
4269 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
4270 struct fc_port *sess;
4271 struct qla_tgt_cmd *cmd;
4272 unsigned long flags;
4274 if (unlikely(tgt->tgt_stop)) {
4275 ql_dbg(ql_dbg_io, vha, 0x3061,
4276 "New command while device %p is shutting down\n", tgt);
4277 return -EFAULT;
4280 sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha, atio->u.isp24.fcp_hdr.s_id);
4281 if (unlikely(!sess)) {
4282 struct qla_tgt_sess_op *op = kzalloc(sizeof(struct qla_tgt_sess_op),
4283 GFP_ATOMIC);
4284 if (!op)
4285 return -ENOMEM;
4287 memcpy(&op->atio, atio, sizeof(*atio));
4288 op->vha = vha;
4290 spin_lock_irqsave(&vha->cmd_list_lock, flags);
4291 list_add_tail(&op->cmd_list, &vha->qla_sess_op_cmd_list);
4292 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
4294 INIT_WORK(&op->work, qlt_create_sess_from_atio);
4295 queue_work(qla_tgt_wq, &op->work);
4296 return 0;
4299 /* Another WWN used to have our s_id. Our PLOGI scheduled its
4300 * session deletion, but it's still in sess_del_work wq */
4301 if (sess->deleted) {
4302 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf002,
4303 "New command while old session %p is being deleted\n",
4304 sess);
4305 return -EFAULT;
4309 * Do kref_get() before returning + dropping qla_hw_data->hardware_lock.
4311 if (!kref_get_unless_zero(&sess->sess_kref)) {
4312 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf004,
4313 "%s: kref_get fail, %8phC oxid %x \n",
4314 __func__, sess->port_name,
4315 be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id));
4316 return -EFAULT;
4319 cmd = qlt_get_tag(vha, sess, atio);
4320 if (!cmd) {
4321 ql_dbg(ql_dbg_io, vha, 0x3062,
4322 "qla_target(%d): Allocation of cmd failed\n", vha->vp_idx);
4323 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
4324 ha->tgt.tgt_ops->put_sess(sess);
4325 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
4326 return -ENOMEM;
4329 cmd->cmd_in_wq = 1;
4330 cmd->trc_flags |= TRC_NEW_CMD;
4332 spin_lock_irqsave(&vha->cmd_list_lock, flags);
4333 list_add_tail(&cmd->cmd_list, &vha->qla_cmd_list);
4334 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
4336 INIT_WORK(&cmd->work, qlt_do_work);
4337 if (vha->flags.qpairs_available) {
4338 queue_work_on(cmd->se_cmd.cpuid, qla_tgt_wq, &cmd->work);
4339 } else if (ha->msix_count) {
4340 if (cmd->atio.u.isp24.fcp_cmnd.rddata)
4341 queue_work_on(smp_processor_id(), qla_tgt_wq,
4342 &cmd->work);
4343 else
4344 queue_work_on(cmd->se_cmd.cpuid, qla_tgt_wq,
4345 &cmd->work);
4346 } else {
4347 queue_work(qla_tgt_wq, &cmd->work);
4350 return 0;
4353 /* ha->hardware_lock supposed to be held on entry */
4354 static int qlt_issue_task_mgmt(struct fc_port *sess, u64 lun,
4355 int fn, void *iocb, int flags)
4357 struct scsi_qla_host *vha = sess->vha;
4358 struct qla_hw_data *ha = vha->hw;
4359 struct qla_tgt_mgmt_cmd *mcmd;
4360 struct atio_from_isp *a = (struct atio_from_isp *)iocb;
4361 int res;
4363 mcmd = mempool_alloc(qla_tgt_mgmt_cmd_mempool, GFP_ATOMIC);
4364 if (!mcmd) {
4365 ql_dbg(ql_dbg_tgt_tmr, vha, 0x10009,
4366 "qla_target(%d): Allocation of management "
4367 "command failed, some commands and their data could "
4368 "leak\n", vha->vp_idx);
4369 return -ENOMEM;
4371 memset(mcmd, 0, sizeof(*mcmd));
4372 mcmd->sess = sess;
4374 if (iocb) {
4375 memcpy(&mcmd->orig_iocb.imm_ntfy, iocb,
4376 sizeof(mcmd->orig_iocb.imm_ntfy));
4378 mcmd->tmr_func = fn;
4379 mcmd->flags = flags;
4380 mcmd->reset_count = ha->base_qpair->chip_reset;
4381 mcmd->qpair = ha->base_qpair;
4383 switch (fn) {
4384 case QLA_TGT_LUN_RESET:
4385 abort_cmds_for_lun(vha, lun, a->u.isp24.fcp_hdr.s_id);
4386 break;
4389 res = ha->tgt.tgt_ops->handle_tmr(mcmd, lun, mcmd->tmr_func, 0);
4390 if (res != 0) {
4391 ql_dbg(ql_dbg_tgt_tmr, vha, 0x1000b,
4392 "qla_target(%d): tgt.tgt_ops->handle_tmr() failed: %d\n",
4393 sess->vha->vp_idx, res);
4394 mempool_free(mcmd, qla_tgt_mgmt_cmd_mempool);
4395 return -EFAULT;
4398 return 0;
4401 /* ha->hardware_lock supposed to be held on entry */
4402 static int qlt_handle_task_mgmt(struct scsi_qla_host *vha, void *iocb)
4404 struct atio_from_isp *a = (struct atio_from_isp *)iocb;
4405 struct qla_hw_data *ha = vha->hw;
4406 struct qla_tgt *tgt;
4407 struct fc_port *sess;
4408 u64 unpacked_lun;
4409 int fn;
4410 unsigned long flags;
4412 tgt = vha->vha_tgt.qla_tgt;
4414 fn = a->u.isp24.fcp_cmnd.task_mgmt_flags;
4416 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
4417 sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha,
4418 a->u.isp24.fcp_hdr.s_id);
4419 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
4421 unpacked_lun =
4422 scsilun_to_int((struct scsi_lun *)&a->u.isp24.fcp_cmnd.lun);
4424 if (!sess) {
4425 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf024,
4426 "qla_target(%d): task mgmt fn 0x%x for "
4427 "non-existant session\n", vha->vp_idx, fn);
4428 return qlt_sched_sess_work(tgt, QLA_TGT_SESS_WORK_TM, iocb,
4429 sizeof(struct atio_from_isp));
4432 if (sess->deleted)
4433 return -EFAULT;
4435 return qlt_issue_task_mgmt(sess, unpacked_lun, fn, iocb, 0);
4438 /* ha->hardware_lock supposed to be held on entry */
4439 static int __qlt_abort_task(struct scsi_qla_host *vha,
4440 struct imm_ntfy_from_isp *iocb, struct fc_port *sess)
4442 struct atio_from_isp *a = (struct atio_from_isp *)iocb;
4443 struct qla_hw_data *ha = vha->hw;
4444 struct qla_tgt_mgmt_cmd *mcmd;
4445 u64 unpacked_lun;
4446 int rc;
4448 mcmd = mempool_alloc(qla_tgt_mgmt_cmd_mempool, GFP_ATOMIC);
4449 if (mcmd == NULL) {
4450 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05f,
4451 "qla_target(%d): %s: Allocation of ABORT cmd failed\n",
4452 vha->vp_idx, __func__);
4453 return -ENOMEM;
4455 memset(mcmd, 0, sizeof(*mcmd));
4457 mcmd->sess = sess;
4458 memcpy(&mcmd->orig_iocb.imm_ntfy, iocb,
4459 sizeof(mcmd->orig_iocb.imm_ntfy));
4461 unpacked_lun =
4462 scsilun_to_int((struct scsi_lun *)&a->u.isp24.fcp_cmnd.lun);
4463 mcmd->reset_count = ha->base_qpair->chip_reset;
4464 mcmd->tmr_func = QLA_TGT_2G_ABORT_TASK;
4465 mcmd->qpair = ha->base_qpair;
4467 rc = ha->tgt.tgt_ops->handle_tmr(mcmd, unpacked_lun, mcmd->tmr_func,
4468 le16_to_cpu(iocb->u.isp2x.seq_id));
4469 if (rc != 0) {
4470 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf060,
4471 "qla_target(%d): tgt_ops->handle_tmr() failed: %d\n",
4472 vha->vp_idx, rc);
4473 mempool_free(mcmd, qla_tgt_mgmt_cmd_mempool);
4474 return -EFAULT;
4477 return 0;
4480 /* ha->hardware_lock supposed to be held on entry */
4481 static int qlt_abort_task(struct scsi_qla_host *vha,
4482 struct imm_ntfy_from_isp *iocb)
4484 struct qla_hw_data *ha = vha->hw;
4485 struct fc_port *sess;
4486 int loop_id;
4487 unsigned long flags;
4489 loop_id = GET_TARGET_ID(ha, (struct atio_from_isp *)iocb);
4491 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
4492 sess = ha->tgt.tgt_ops->find_sess_by_loop_id(vha, loop_id);
4493 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
4495 if (sess == NULL) {
4496 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf025,
4497 "qla_target(%d): task abort for unexisting "
4498 "session\n", vha->vp_idx);
4499 return qlt_sched_sess_work(vha->vha_tgt.qla_tgt,
4500 QLA_TGT_SESS_WORK_ABORT, iocb, sizeof(*iocb));
4503 return __qlt_abort_task(vha, iocb, sess);
4506 void qlt_logo_completion_handler(fc_port_t *fcport, int rc)
4508 if (rc != MBS_COMMAND_COMPLETE) {
4509 ql_dbg(ql_dbg_tgt_mgt, fcport->vha, 0xf093,
4510 "%s: se_sess %p / sess %p from"
4511 " port %8phC loop_id %#04x s_id %02x:%02x:%02x"
4512 " LOGO failed: %#x\n",
4513 __func__,
4514 fcport->se_sess,
4515 fcport,
4516 fcport->port_name, fcport->loop_id,
4517 fcport->d_id.b.domain, fcport->d_id.b.area,
4518 fcport->d_id.b.al_pa, rc);
4521 fcport->logout_completed = 1;
4525 * ha->hardware_lock supposed to be held on entry (to protect tgt->sess_list)
4527 * Schedules sessions with matching port_id/loop_id but different wwn for
4528 * deletion. Returns existing session with matching wwn if present.
4529 * Null otherwise.
4531 struct fc_port *
4532 qlt_find_sess_invalidate_other(scsi_qla_host_t *vha, uint64_t wwn,
4533 port_id_t port_id, uint16_t loop_id, struct fc_port **conflict_sess)
4535 struct fc_port *sess = NULL, *other_sess;
4536 uint64_t other_wwn;
4538 *conflict_sess = NULL;
4540 list_for_each_entry(other_sess, &vha->vp_fcports, list) {
4542 other_wwn = wwn_to_u64(other_sess->port_name);
4544 if (wwn == other_wwn) {
4545 WARN_ON(sess);
4546 sess = other_sess;
4547 continue;
4550 /* find other sess with nport_id collision */
4551 if (port_id.b24 == other_sess->d_id.b24) {
4552 if (loop_id != other_sess->loop_id) {
4553 ql_dbg(ql_dbg_tgt_tmr, vha, 0x1000c,
4554 "Invalidating sess %p loop_id %d wwn %llx.\n",
4555 other_sess, other_sess->loop_id, other_wwn);
4558 * logout_on_delete is set by default, but another
4559 * session that has the same s_id/loop_id combo
4560 * might have cleared it when requested this session
4561 * deletion, so don't touch it
4563 qlt_schedule_sess_for_deletion(other_sess, true);
4564 } else {
4566 * Another wwn used to have our s_id/loop_id
4567 * kill the session, but don't free the loop_id
4569 ql_dbg(ql_dbg_tgt_tmr, vha, 0xf01b,
4570 "Invalidating sess %p loop_id %d wwn %llx.\n",
4571 other_sess, other_sess->loop_id, other_wwn);
4574 other_sess->keep_nport_handle = 1;
4575 *conflict_sess = other_sess;
4576 qlt_schedule_sess_for_deletion(other_sess,
4577 true);
4579 continue;
4582 /* find other sess with nport handle collision */
4583 if ((loop_id == other_sess->loop_id) &&
4584 (loop_id != FC_NO_LOOP_ID)) {
4585 ql_dbg(ql_dbg_tgt_tmr, vha, 0x1000d,
4586 "Invalidating sess %p loop_id %d wwn %llx.\n",
4587 other_sess, other_sess->loop_id, other_wwn);
4589 /* Same loop_id but different s_id
4590 * Ok to kill and logout */
4591 qlt_schedule_sess_for_deletion(other_sess, true);
4595 return sess;
4598 /* Abort any commands for this s_id waiting on qla_tgt_wq workqueue */
4599 static int abort_cmds_for_s_id(struct scsi_qla_host *vha, port_id_t *s_id)
4601 struct qla_tgt_sess_op *op;
4602 struct qla_tgt_cmd *cmd;
4603 uint32_t key;
4604 int count = 0;
4605 unsigned long flags;
4607 key = (((u32)s_id->b.domain << 16) |
4608 ((u32)s_id->b.area << 8) |
4609 ((u32)s_id->b.al_pa));
4611 spin_lock_irqsave(&vha->cmd_list_lock, flags);
4612 list_for_each_entry(op, &vha->qla_sess_op_cmd_list, cmd_list) {
4613 uint32_t op_key = sid_to_key(op->atio.u.isp24.fcp_hdr.s_id);
4615 if (op_key == key) {
4616 op->aborted = true;
4617 count++;
4621 list_for_each_entry(op, &vha->unknown_atio_list, cmd_list) {
4622 uint32_t op_key = sid_to_key(op->atio.u.isp24.fcp_hdr.s_id);
4623 if (op_key == key) {
4624 op->aborted = true;
4625 count++;
4629 list_for_each_entry(cmd, &vha->qla_cmd_list, cmd_list) {
4630 uint32_t cmd_key = sid_to_key(cmd->atio.u.isp24.fcp_hdr.s_id);
4631 if (cmd_key == key) {
4632 cmd->aborted = 1;
4633 count++;
4636 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
4638 return count;
4642 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
4644 static int qlt_24xx_handle_els(struct scsi_qla_host *vha,
4645 struct imm_ntfy_from_isp *iocb)
4647 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
4648 struct qla_hw_data *ha = vha->hw;
4649 struct fc_port *sess = NULL, *conflict_sess = NULL;
4650 uint64_t wwn;
4651 port_id_t port_id;
4652 uint16_t loop_id;
4653 uint16_t wd3_lo;
4654 int res = 0;
4655 struct qlt_plogi_ack_t *pla;
4656 unsigned long flags;
4658 wwn = wwn_to_u64(iocb->u.isp24.port_name);
4660 port_id.b.domain = iocb->u.isp24.port_id[2];
4661 port_id.b.area = iocb->u.isp24.port_id[1];
4662 port_id.b.al_pa = iocb->u.isp24.port_id[0];
4663 port_id.b.rsvd_1 = 0;
4665 loop_id = le16_to_cpu(iocb->u.isp24.nport_handle);
4667 ql_dbg(ql_dbg_disc, vha, 0xf026,
4668 "qla_target(%d): Port ID: %02x:%02x:%02x ELS opcode: 0x%02x lid %d %8phC\n",
4669 vha->vp_idx, iocb->u.isp24.port_id[2],
4670 iocb->u.isp24.port_id[1], iocb->u.isp24.port_id[0],
4671 iocb->u.isp24.status_subcode, loop_id,
4672 iocb->u.isp24.port_name);
4674 /* res = 1 means ack at the end of thread
4675 * res = 0 means ack async/later.
4677 switch (iocb->u.isp24.status_subcode) {
4678 case ELS_PLOGI:
4680 /* Mark all stale commands in qla_tgt_wq for deletion */
4681 abort_cmds_for_s_id(vha, &port_id);
4683 if (wwn) {
4684 spin_lock_irqsave(&tgt->ha->tgt.sess_lock, flags);
4685 sess = qlt_find_sess_invalidate_other(vha, wwn,
4686 port_id, loop_id, &conflict_sess);
4687 spin_unlock_irqrestore(&tgt->ha->tgt.sess_lock, flags);
4690 if (IS_SW_RESV_ADDR(port_id)) {
4691 res = 1;
4692 break;
4695 pla = qlt_plogi_ack_find_add(vha, &port_id, iocb);
4696 if (!pla) {
4697 qlt_send_term_imm_notif(vha, iocb, 1);
4698 break;
4701 res = 0;
4703 if (conflict_sess) {
4704 conflict_sess->login_gen++;
4705 qlt_plogi_ack_link(vha, pla, conflict_sess,
4706 QLT_PLOGI_LINK_CONFLICT);
4709 if (!sess) {
4710 pla->ref_count++;
4711 qla24xx_post_newsess_work(vha, &port_id,
4712 iocb->u.isp24.port_name, pla);
4713 res = 0;
4714 break;
4717 qlt_plogi_ack_link(vha, pla, sess, QLT_PLOGI_LINK_SAME_WWN);
4718 sess->fw_login_state = DSC_LS_PLOGI_PEND;
4719 sess->d_id = port_id;
4720 sess->login_gen++;
4722 switch (sess->disc_state) {
4723 case DSC_DELETED:
4724 qlt_plogi_ack_unref(vha, pla);
4725 break;
4727 default:
4729 * Under normal circumstances we want to release nport handle
4730 * during LOGO process to avoid nport handle leaks inside FW.
4731 * The exception is when LOGO is done while another PLOGI with
4732 * the same nport handle is waiting as might be the case here.
4733 * Note: there is always a possibily of a race where session
4734 * deletion has already started for other reasons (e.g. ACL
4735 * removal) and now PLOGI arrives:
4736 * 1. if PLOGI arrived in FW after nport handle has been freed,
4737 * FW must have assigned this PLOGI a new/same handle and we
4738 * can proceed ACK'ing it as usual when session deletion
4739 * completes.
4740 * 2. if PLOGI arrived in FW before LOGO with LCF_FREE_NPORT
4741 * bit reached it, the handle has now been released. We'll
4742 * get an error when we ACK this PLOGI. Nothing will be sent
4743 * back to initiator. Initiator should eventually retry
4744 * PLOGI and situation will correct itself.
4746 sess->keep_nport_handle = ((sess->loop_id == loop_id) &&
4747 (sess->d_id.b24 == port_id.b24));
4749 ql_dbg(ql_dbg_disc, vha, 0x20f9,
4750 "%s %d %8phC post del sess\n",
4751 __func__, __LINE__, sess->port_name);
4754 qlt_schedule_sess_for_deletion_lock(sess);
4755 break;
4758 break;
4760 case ELS_PRLI:
4761 wd3_lo = le16_to_cpu(iocb->u.isp24.u.prli.wd3_lo);
4763 if (wwn) {
4764 spin_lock_irqsave(&tgt->ha->tgt.sess_lock, flags);
4765 sess = qlt_find_sess_invalidate_other(vha, wwn, port_id,
4766 loop_id, &conflict_sess);
4767 spin_unlock_irqrestore(&tgt->ha->tgt.sess_lock, flags);
4770 if (conflict_sess) {
4771 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf09b,
4772 "PRLI with conflicting sess %p port %8phC\n",
4773 conflict_sess, conflict_sess->port_name);
4774 qlt_send_term_imm_notif(vha, iocb, 1);
4775 res = 0;
4776 break;
4779 if (sess != NULL) {
4780 if (sess->fw_login_state != DSC_LS_PLOGI_PEND &&
4781 sess->fw_login_state != DSC_LS_PLOGI_COMP) {
4783 * Impatient initiator sent PRLI before last
4784 * PLOGI could finish. Will force him to re-try,
4785 * while last one finishes.
4787 ql_log(ql_log_warn, sess->vha, 0xf095,
4788 "sess %p PRLI received, before plogi ack.\n",
4789 sess);
4790 qlt_send_term_imm_notif(vha, iocb, 1);
4791 res = 0;
4792 break;
4796 * This shouldn't happen under normal circumstances,
4797 * since we have deleted the old session during PLOGI
4799 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf096,
4800 "PRLI (loop_id %#04x) for existing sess %p (loop_id %#04x)\n",
4801 sess->loop_id, sess, iocb->u.isp24.nport_handle);
4803 sess->local = 0;
4804 sess->loop_id = loop_id;
4805 sess->d_id = port_id;
4806 sess->fw_login_state = DSC_LS_PRLI_PEND;
4808 if (wd3_lo & BIT_7)
4809 sess->conf_compl_supported = 1;
4811 if ((wd3_lo & BIT_4) == 0)
4812 sess->port_type = FCT_INITIATOR;
4813 else
4814 sess->port_type = FCT_TARGET;
4816 res = 1; /* send notify ack */
4818 /* Make session global (not used in fabric mode) */
4819 if (ha->current_topology != ISP_CFG_F) {
4820 if (sess) {
4821 ql_dbg(ql_dbg_disc, vha, 0x20fa,
4822 "%s %d %8phC post nack\n",
4823 __func__, __LINE__, sess->port_name);
4824 qla24xx_post_nack_work(vha, sess, iocb,
4825 SRB_NACK_PRLI);
4826 res = 0;
4827 } else {
4828 set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
4829 set_bit(LOCAL_LOOP_UPDATE, &vha->dpc_flags);
4830 qla2xxx_wake_dpc(vha);
4832 } else {
4833 if (sess) {
4834 ql_dbg(ql_dbg_disc, vha, 0x20fb,
4835 "%s %d %8phC post nack\n",
4836 __func__, __LINE__, sess->port_name);
4837 qla24xx_post_nack_work(vha, sess, iocb,
4838 SRB_NACK_PRLI);
4839 res = 0;
4842 break;
4844 case ELS_TPRLO:
4845 if (le16_to_cpu(iocb->u.isp24.flags) &
4846 NOTIFY24XX_FLAGS_GLOBAL_TPRLO) {
4847 loop_id = 0xFFFF;
4848 qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS);
4849 res = 1;
4850 break;
4852 /* drop through */
4853 case ELS_LOGO:
4854 case ELS_PRLO:
4855 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
4856 sess = qla2x00_find_fcport_by_loopid(vha, loop_id);
4857 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
4859 if (sess) {
4860 sess->login_gen++;
4861 sess->fw_login_state = DSC_LS_LOGO_PEND;
4862 sess->logo_ack_needed = 1;
4863 memcpy(sess->iocb, iocb, IOCB_SIZE);
4866 res = qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS_SESS);
4868 ql_dbg(ql_dbg_disc, vha, 0x20fc,
4869 "%s: logo %llx res %d sess %p ",
4870 __func__, wwn, res, sess);
4871 if (res == 0) {
4873 * cmd went upper layer, look for qlt_xmit_tm_rsp()
4874 * for LOGO_ACK & sess delete
4876 BUG_ON(!sess);
4877 res = 0;
4878 } else {
4879 /* cmd did not go to upper layer. */
4880 if (sess) {
4881 qlt_schedule_sess_for_deletion_lock(sess);
4882 res = 0;
4884 /* else logo will be ack */
4886 break;
4887 case ELS_PDISC:
4888 case ELS_ADISC:
4890 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
4891 if (tgt->link_reinit_iocb_pending) {
4892 qlt_send_notify_ack(ha->base_qpair,
4893 &tgt->link_reinit_iocb, 0, 0, 0, 0, 0, 0);
4894 tgt->link_reinit_iocb_pending = 0;
4897 sess = qla2x00_find_fcport_by_wwpn(vha,
4898 iocb->u.isp24.port_name, 1);
4899 if (sess) {
4900 ql_dbg(ql_dbg_disc, vha, 0x20fd,
4901 "sess %p lid %d|%d DS %d LS %d\n",
4902 sess, sess->loop_id, loop_id,
4903 sess->disc_state, sess->fw_login_state);
4906 res = 1; /* send notify ack */
4907 break;
4910 case ELS_FLOGI: /* should never happen */
4911 default:
4912 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf061,
4913 "qla_target(%d): Unsupported ELS command %x "
4914 "received\n", vha->vp_idx, iocb->u.isp24.status_subcode);
4915 res = qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS_SESS);
4916 break;
4919 return res;
4923 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
4925 static void qlt_handle_imm_notify(struct scsi_qla_host *vha,
4926 struct imm_ntfy_from_isp *iocb)
4928 struct qla_hw_data *ha = vha->hw;
4929 uint32_t add_flags = 0;
4930 int send_notify_ack = 1;
4931 uint16_t status;
4933 status = le16_to_cpu(iocb->u.isp2x.status);
4934 switch (status) {
4935 case IMM_NTFY_LIP_RESET:
4937 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf032,
4938 "qla_target(%d): LIP reset (loop %#x), subcode %x\n",
4939 vha->vp_idx, le16_to_cpu(iocb->u.isp24.nport_handle),
4940 iocb->u.isp24.status_subcode);
4942 if (qlt_reset(vha, iocb, QLA_TGT_ABORT_ALL) == 0)
4943 send_notify_ack = 0;
4944 break;
4947 case IMM_NTFY_LIP_LINK_REINIT:
4949 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
4950 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf033,
4951 "qla_target(%d): LINK REINIT (loop %#x, "
4952 "subcode %x)\n", vha->vp_idx,
4953 le16_to_cpu(iocb->u.isp24.nport_handle),
4954 iocb->u.isp24.status_subcode);
4955 if (tgt->link_reinit_iocb_pending) {
4956 qlt_send_notify_ack(ha->base_qpair,
4957 &tgt->link_reinit_iocb, 0, 0, 0, 0, 0, 0);
4959 memcpy(&tgt->link_reinit_iocb, iocb, sizeof(*iocb));
4960 tgt->link_reinit_iocb_pending = 1;
4962 * QLogic requires to wait after LINK REINIT for possible
4963 * PDISC or ADISC ELS commands
4965 send_notify_ack = 0;
4966 break;
4969 case IMM_NTFY_PORT_LOGOUT:
4970 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf034,
4971 "qla_target(%d): Port logout (loop "
4972 "%#x, subcode %x)\n", vha->vp_idx,
4973 le16_to_cpu(iocb->u.isp24.nport_handle),
4974 iocb->u.isp24.status_subcode);
4976 if (qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS_SESS) == 0)
4977 send_notify_ack = 0;
4978 /* The sessions will be cleared in the callback, if needed */
4979 break;
4981 case IMM_NTFY_GLBL_TPRLO:
4982 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf035,
4983 "qla_target(%d): Global TPRLO (%x)\n", vha->vp_idx, status);
4984 if (qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS) == 0)
4985 send_notify_ack = 0;
4986 /* The sessions will be cleared in the callback, if needed */
4987 break;
4989 case IMM_NTFY_PORT_CONFIG:
4990 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf036,
4991 "qla_target(%d): Port config changed (%x)\n", vha->vp_idx,
4992 status);
4993 if (qlt_reset(vha, iocb, QLA_TGT_ABORT_ALL) == 0)
4994 send_notify_ack = 0;
4995 /* The sessions will be cleared in the callback, if needed */
4996 break;
4998 case IMM_NTFY_GLBL_LOGO:
4999 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06a,
5000 "qla_target(%d): Link failure detected\n",
5001 vha->vp_idx);
5002 /* I_T nexus loss */
5003 if (qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS) == 0)
5004 send_notify_ack = 0;
5005 break;
5007 case IMM_NTFY_IOCB_OVERFLOW:
5008 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06b,
5009 "qla_target(%d): Cannot provide requested "
5010 "capability (IOCB overflowed the immediate notify "
5011 "resource count)\n", vha->vp_idx);
5012 break;
5014 case IMM_NTFY_ABORT_TASK:
5015 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf037,
5016 "qla_target(%d): Abort Task (S %08x I %#x -> "
5017 "L %#x)\n", vha->vp_idx,
5018 le16_to_cpu(iocb->u.isp2x.seq_id),
5019 GET_TARGET_ID(ha, (struct atio_from_isp *)iocb),
5020 le16_to_cpu(iocb->u.isp2x.lun));
5021 if (qlt_abort_task(vha, iocb) == 0)
5022 send_notify_ack = 0;
5023 break;
5025 case IMM_NTFY_RESOURCE:
5026 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06c,
5027 "qla_target(%d): Out of resources, host %ld\n",
5028 vha->vp_idx, vha->host_no);
5029 break;
5031 case IMM_NTFY_MSG_RX:
5032 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf038,
5033 "qla_target(%d): Immediate notify task %x\n",
5034 vha->vp_idx, iocb->u.isp2x.task_flags);
5035 if (qlt_handle_task_mgmt(vha, iocb) == 0)
5036 send_notify_ack = 0;
5037 break;
5039 case IMM_NTFY_ELS:
5040 if (qlt_24xx_handle_els(vha, iocb) == 0)
5041 send_notify_ack = 0;
5042 break;
5043 default:
5044 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06d,
5045 "qla_target(%d): Received unknown immediate "
5046 "notify status %x\n", vha->vp_idx, status);
5047 break;
5050 if (send_notify_ack)
5051 qlt_send_notify_ack(ha->base_qpair, iocb, add_flags, 0, 0, 0,
5052 0, 0);
5056 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
5057 * This function sends busy to ISP 2xxx or 24xx.
5059 static int __qlt_send_busy(struct qla_qpair *qpair,
5060 struct atio_from_isp *atio, uint16_t status)
5062 struct scsi_qla_host *vha = qpair->vha;
5063 struct ctio7_to_24xx *ctio24;
5064 struct qla_hw_data *ha = vha->hw;
5065 request_t *pkt;
5066 struct fc_port *sess = NULL;
5067 unsigned long flags;
5068 u16 temp;
5070 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
5071 sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha,
5072 atio->u.isp24.fcp_hdr.s_id);
5073 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
5074 if (!sess) {
5075 qlt_send_term_exchange(qpair, NULL, atio, 1, 0);
5076 return 0;
5078 /* Sending marker isn't necessary, since we called from ISR */
5080 pkt = (request_t *)__qla2x00_alloc_iocbs(qpair, NULL);
5081 if (!pkt) {
5082 ql_dbg(ql_dbg_io, vha, 0x3063,
5083 "qla_target(%d): %s failed: unable to allocate "
5084 "request packet", vha->vp_idx, __func__);
5085 return -ENOMEM;
5088 qpair->tgt_counters.num_q_full_sent++;
5089 pkt->entry_count = 1;
5090 pkt->handle = QLA_TGT_SKIP_HANDLE | CTIO_COMPLETION_HANDLE_MARK;
5092 ctio24 = (struct ctio7_to_24xx *)pkt;
5093 ctio24->entry_type = CTIO_TYPE7;
5094 ctio24->nport_handle = sess->loop_id;
5095 ctio24->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
5096 ctio24->vp_index = vha->vp_idx;
5097 ctio24->initiator_id[0] = atio->u.isp24.fcp_hdr.s_id[2];
5098 ctio24->initiator_id[1] = atio->u.isp24.fcp_hdr.s_id[1];
5099 ctio24->initiator_id[2] = atio->u.isp24.fcp_hdr.s_id[0];
5100 ctio24->exchange_addr = atio->u.isp24.exchange_addr;
5101 temp = (atio->u.isp24.attr << 9) |
5102 CTIO7_FLAGS_STATUS_MODE_1 | CTIO7_FLAGS_SEND_STATUS |
5103 CTIO7_FLAGS_DONT_RET_CTIO;
5104 ctio24->u.status1.flags = cpu_to_le16(temp);
5106 * CTIO from fw w/o se_cmd doesn't provide enough info to retry it,
5107 * if the explicit conformation is used.
5109 ctio24->u.status1.ox_id = swab16(atio->u.isp24.fcp_hdr.ox_id);
5110 ctio24->u.status1.scsi_status = cpu_to_le16(status);
5111 /* Memory Barrier */
5112 wmb();
5113 if (qpair->reqq_start_iocbs)
5114 qpair->reqq_start_iocbs(qpair);
5115 else
5116 qla2x00_start_iocbs(vha, qpair->req);
5117 return 0;
5121 * This routine is used to allocate a command for either a QFull condition
5122 * (ie reply SAM_STAT_BUSY) or to terminate an exchange that did not go
5123 * out previously.
5125 static void
5126 qlt_alloc_qfull_cmd(struct scsi_qla_host *vha,
5127 struct atio_from_isp *atio, uint16_t status, int qfull)
5129 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
5130 struct qla_hw_data *ha = vha->hw;
5131 struct fc_port *sess;
5132 struct se_session *se_sess;
5133 struct qla_tgt_cmd *cmd;
5134 int tag;
5135 unsigned long flags;
5137 if (unlikely(tgt->tgt_stop)) {
5138 ql_dbg(ql_dbg_io, vha, 0x300a,
5139 "New command while device %p is shutting down\n", tgt);
5140 return;
5143 if ((vha->hw->tgt.num_qfull_cmds_alloc + 1) > MAX_QFULL_CMDS_ALLOC) {
5144 vha->hw->tgt.num_qfull_cmds_dropped++;
5145 if (vha->hw->tgt.num_qfull_cmds_dropped >
5146 vha->qla_stats.stat_max_qfull_cmds_dropped)
5147 vha->qla_stats.stat_max_qfull_cmds_dropped =
5148 vha->hw->tgt.num_qfull_cmds_dropped;
5150 ql_dbg(ql_dbg_io, vha, 0x3068,
5151 "qla_target(%d): %s: QFull CMD dropped[%d]\n",
5152 vha->vp_idx, __func__,
5153 vha->hw->tgt.num_qfull_cmds_dropped);
5155 qlt_chk_exch_leak_thresh_hold(vha);
5156 return;
5159 sess = ha->tgt.tgt_ops->find_sess_by_s_id
5160 (vha, atio->u.isp24.fcp_hdr.s_id);
5161 if (!sess)
5162 return;
5164 se_sess = sess->se_sess;
5166 tag = percpu_ida_alloc(&se_sess->sess_tag_pool, TASK_RUNNING);
5167 if (tag < 0)
5168 return;
5170 cmd = &((struct qla_tgt_cmd *)se_sess->sess_cmd_map)[tag];
5171 if (!cmd) {
5172 ql_dbg(ql_dbg_io, vha, 0x3009,
5173 "qla_target(%d): %s: Allocation of cmd failed\n",
5174 vha->vp_idx, __func__);
5176 vha->hw->tgt.num_qfull_cmds_dropped++;
5177 if (vha->hw->tgt.num_qfull_cmds_dropped >
5178 vha->qla_stats.stat_max_qfull_cmds_dropped)
5179 vha->qla_stats.stat_max_qfull_cmds_dropped =
5180 vha->hw->tgt.num_qfull_cmds_dropped;
5182 qlt_chk_exch_leak_thresh_hold(vha);
5183 return;
5186 memset(cmd, 0, sizeof(struct qla_tgt_cmd));
5188 qlt_incr_num_pend_cmds(vha);
5189 INIT_LIST_HEAD(&cmd->cmd_list);
5190 memcpy(&cmd->atio, atio, sizeof(*atio));
5192 cmd->tgt = vha->vha_tgt.qla_tgt;
5193 cmd->vha = vha;
5194 cmd->reset_count = ha->base_qpair->chip_reset;
5195 cmd->q_full = 1;
5196 cmd->qpair = ha->base_qpair;
5198 if (qfull) {
5199 cmd->q_full = 1;
5200 /* NOTE: borrowing the state field to carry the status */
5201 cmd->state = status;
5202 } else
5203 cmd->term_exchg = 1;
5205 spin_lock_irqsave(&vha->hw->tgt.q_full_lock, flags);
5206 list_add_tail(&cmd->cmd_list, &vha->hw->tgt.q_full_list);
5208 vha->hw->tgt.num_qfull_cmds_alloc++;
5209 if (vha->hw->tgt.num_qfull_cmds_alloc >
5210 vha->qla_stats.stat_max_qfull_cmds_alloc)
5211 vha->qla_stats.stat_max_qfull_cmds_alloc =
5212 vha->hw->tgt.num_qfull_cmds_alloc;
5213 spin_unlock_irqrestore(&vha->hw->tgt.q_full_lock, flags);
5217 qlt_free_qfull_cmds(struct qla_qpair *qpair)
5219 struct scsi_qla_host *vha = qpair->vha;
5220 struct qla_hw_data *ha = vha->hw;
5221 unsigned long flags;
5222 struct qla_tgt_cmd *cmd, *tcmd;
5223 struct list_head free_list, q_full_list;
5224 int rc = 0;
5226 if (list_empty(&ha->tgt.q_full_list))
5227 return 0;
5229 INIT_LIST_HEAD(&free_list);
5230 INIT_LIST_HEAD(&q_full_list);
5232 spin_lock_irqsave(&vha->hw->tgt.q_full_lock, flags);
5233 if (list_empty(&ha->tgt.q_full_list)) {
5234 spin_unlock_irqrestore(&vha->hw->tgt.q_full_lock, flags);
5235 return 0;
5238 list_splice_init(&vha->hw->tgt.q_full_list, &q_full_list);
5239 spin_unlock_irqrestore(&vha->hw->tgt.q_full_lock, flags);
5241 spin_lock_irqsave(qpair->qp_lock_ptr, flags);
5242 list_for_each_entry_safe(cmd, tcmd, &q_full_list, cmd_list) {
5243 if (cmd->q_full)
5244 /* cmd->state is a borrowed field to hold status */
5245 rc = __qlt_send_busy(qpair, &cmd->atio, cmd->state);
5246 else if (cmd->term_exchg)
5247 rc = __qlt_send_term_exchange(qpair, NULL, &cmd->atio);
5249 if (rc == -ENOMEM)
5250 break;
5252 if (cmd->q_full)
5253 ql_dbg(ql_dbg_io, vha, 0x3006,
5254 "%s: busy sent for ox_id[%04x]\n", __func__,
5255 be16_to_cpu(cmd->atio.u.isp24.fcp_hdr.ox_id));
5256 else if (cmd->term_exchg)
5257 ql_dbg(ql_dbg_io, vha, 0x3007,
5258 "%s: Term exchg sent for ox_id[%04x]\n", __func__,
5259 be16_to_cpu(cmd->atio.u.isp24.fcp_hdr.ox_id));
5260 else
5261 ql_dbg(ql_dbg_io, vha, 0x3008,
5262 "%s: Unexpected cmd in QFull list %p\n", __func__,
5263 cmd);
5265 list_del(&cmd->cmd_list);
5266 list_add_tail(&cmd->cmd_list, &free_list);
5268 /* piggy back on hardware_lock for protection */
5269 vha->hw->tgt.num_qfull_cmds_alloc--;
5271 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
5273 cmd = NULL;
5275 list_for_each_entry_safe(cmd, tcmd, &free_list, cmd_list) {
5276 list_del(&cmd->cmd_list);
5277 /* This cmd was never sent to TCM. There is no need
5278 * to schedule free or call free_cmd
5280 qlt_free_cmd(cmd);
5283 if (!list_empty(&q_full_list)) {
5284 spin_lock_irqsave(&vha->hw->tgt.q_full_lock, flags);
5285 list_splice(&q_full_list, &vha->hw->tgt.q_full_list);
5286 spin_unlock_irqrestore(&vha->hw->tgt.q_full_lock, flags);
5289 return rc;
5292 static void
5293 qlt_send_busy(struct qla_qpair *qpair, struct atio_from_isp *atio,
5294 uint16_t status)
5296 int rc = 0;
5297 struct scsi_qla_host *vha = qpair->vha;
5299 rc = __qlt_send_busy(qpair, atio, status);
5300 if (rc == -ENOMEM)
5301 qlt_alloc_qfull_cmd(vha, atio, status, 1);
5304 static int
5305 qlt_chk_qfull_thresh_hold(struct scsi_qla_host *vha, struct qla_qpair *qpair,
5306 struct atio_from_isp *atio, uint8_t ha_locked)
5308 struct qla_hw_data *ha = vha->hw;
5309 uint16_t status;
5310 unsigned long flags;
5312 if (ha->tgt.num_pend_cmds < Q_FULL_THRESH_HOLD(ha))
5313 return 0;
5315 if (!ha_locked)
5316 spin_lock_irqsave(&ha->hardware_lock, flags);
5317 status = temp_sam_status;
5318 qlt_send_busy(qpair, atio, status);
5319 if (!ha_locked)
5320 spin_unlock_irqrestore(&ha->hardware_lock, flags);
5322 return 1;
5325 /* ha->hardware_lock supposed to be held on entry */
5326 /* called via callback from qla2xxx */
5327 static void qlt_24xx_atio_pkt(struct scsi_qla_host *vha,
5328 struct atio_from_isp *atio, uint8_t ha_locked)
5330 struct qla_hw_data *ha = vha->hw;
5331 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
5332 int rc;
5333 unsigned long flags;
5335 if (unlikely(tgt == NULL)) {
5336 ql_dbg(ql_dbg_tgt, vha, 0x3064,
5337 "ATIO pkt, but no tgt (ha %p)", ha);
5338 return;
5341 * In tgt_stop mode we also should allow all requests to pass.
5342 * Otherwise, some commands can stuck.
5345 tgt->atio_irq_cmd_count++;
5347 switch (atio->u.raw.entry_type) {
5348 case ATIO_TYPE7:
5349 if (unlikely(atio->u.isp24.exchange_addr ==
5350 ATIO_EXCHANGE_ADDRESS_UNKNOWN)) {
5351 ql_dbg(ql_dbg_io, vha, 0x3065,
5352 "qla_target(%d): ATIO_TYPE7 "
5353 "received with UNKNOWN exchange address, "
5354 "sending QUEUE_FULL\n", vha->vp_idx);
5355 if (!ha_locked)
5356 spin_lock_irqsave(&ha->hardware_lock, flags);
5357 qlt_send_busy(ha->base_qpair, atio,
5358 SAM_STAT_TASK_SET_FULL);
5359 if (!ha_locked)
5360 spin_unlock_irqrestore(&ha->hardware_lock,
5361 flags);
5362 break;
5365 if (likely(atio->u.isp24.fcp_cmnd.task_mgmt_flags == 0)) {
5366 rc = qlt_chk_qfull_thresh_hold(vha, ha->base_qpair,
5367 atio, ha_locked);
5368 if (rc != 0) {
5369 tgt->atio_irq_cmd_count--;
5370 return;
5372 rc = qlt_handle_cmd_for_atio(vha, atio);
5373 } else {
5374 rc = qlt_handle_task_mgmt(vha, atio);
5376 if (unlikely(rc != 0)) {
5377 if (rc == -ESRCH) {
5378 if (!ha_locked)
5379 spin_lock_irqsave(&ha->hardware_lock,
5380 flags);
5382 #if 1 /* With TERM EXCHANGE some FC cards refuse to boot */
5383 qlt_send_busy(ha->base_qpair, atio,
5384 SAM_STAT_BUSY);
5385 #else
5386 qlt_send_term_exchange(ha->base_qpair, NULL,
5387 atio, 1, 0);
5388 #endif
5389 if (!ha_locked)
5390 spin_unlock_irqrestore(
5391 &ha->hardware_lock, flags);
5392 } else {
5393 if (tgt->tgt_stop) {
5394 ql_dbg(ql_dbg_tgt, vha, 0xe059,
5395 "qla_target: Unable to send "
5396 "command to target for req, "
5397 "ignoring.\n");
5398 } else {
5399 ql_dbg(ql_dbg_tgt, vha, 0xe05a,
5400 "qla_target(%d): Unable to send "
5401 "command to target, sending BUSY "
5402 "status.\n", vha->vp_idx);
5403 if (!ha_locked)
5404 spin_lock_irqsave(
5405 &ha->hardware_lock, flags);
5406 qlt_send_busy(ha->base_qpair,
5407 atio, SAM_STAT_BUSY);
5408 if (!ha_locked)
5409 spin_unlock_irqrestore(
5410 &ha->hardware_lock, flags);
5414 break;
5416 case IMMED_NOTIFY_TYPE:
5418 if (unlikely(atio->u.isp2x.entry_status != 0)) {
5419 ql_dbg(ql_dbg_tgt, vha, 0xe05b,
5420 "qla_target(%d): Received ATIO packet %x "
5421 "with error status %x\n", vha->vp_idx,
5422 atio->u.raw.entry_type,
5423 atio->u.isp2x.entry_status);
5424 break;
5426 ql_dbg(ql_dbg_tgt, vha, 0xe02e, "%s", "IMMED_NOTIFY ATIO");
5428 if (!ha_locked)
5429 spin_lock_irqsave(&ha->hardware_lock, flags);
5430 qlt_handle_imm_notify(vha, (struct imm_ntfy_from_isp *)atio);
5431 if (!ha_locked)
5432 spin_unlock_irqrestore(&ha->hardware_lock, flags);
5433 break;
5436 default:
5437 ql_dbg(ql_dbg_tgt, vha, 0xe05c,
5438 "qla_target(%d): Received unknown ATIO atio "
5439 "type %x\n", vha->vp_idx, atio->u.raw.entry_type);
5440 break;
5443 tgt->atio_irq_cmd_count--;
5446 /* ha->hardware_lock supposed to be held on entry */
5447 /* called via callback from qla2xxx */
5448 static void qlt_response_pkt(struct scsi_qla_host *vha,
5449 struct rsp_que *rsp, response_t *pkt)
5451 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
5453 if (unlikely(tgt == NULL)) {
5454 ql_dbg(ql_dbg_tgt, vha, 0xe05d,
5455 "qla_target(%d): Response pkt %x received, but no tgt (ha %p)\n",
5456 vha->vp_idx, pkt->entry_type, vha->hw);
5457 return;
5461 * In tgt_stop mode we also should allow all requests to pass.
5462 * Otherwise, some commands can stuck.
5465 switch (pkt->entry_type) {
5466 case CTIO_CRC2:
5467 case CTIO_TYPE7:
5469 struct ctio7_from_24xx *entry = (struct ctio7_from_24xx *)pkt;
5470 qlt_do_ctio_completion(vha, rsp, entry->handle,
5471 le16_to_cpu(entry->status)|(pkt->entry_status << 16),
5472 entry);
5473 break;
5476 case ACCEPT_TGT_IO_TYPE:
5478 struct atio_from_isp *atio = (struct atio_from_isp *)pkt;
5479 int rc;
5480 if (atio->u.isp2x.status !=
5481 cpu_to_le16(ATIO_CDB_VALID)) {
5482 ql_dbg(ql_dbg_tgt, vha, 0xe05e,
5483 "qla_target(%d): ATIO with error "
5484 "status %x received\n", vha->vp_idx,
5485 le16_to_cpu(atio->u.isp2x.status));
5486 break;
5489 rc = qlt_chk_qfull_thresh_hold(vha, rsp->qpair, atio, 1);
5490 if (rc != 0)
5491 return;
5493 rc = qlt_handle_cmd_for_atio(vha, atio);
5494 if (unlikely(rc != 0)) {
5495 if (rc == -ESRCH) {
5496 #if 1 /* With TERM EXCHANGE some FC cards refuse to boot */
5497 qlt_send_busy(rsp->qpair, atio, 0);
5498 #else
5499 qlt_send_term_exchange(rsp->qpair, NULL, atio, 1, 0);
5500 #endif
5501 } else {
5502 if (tgt->tgt_stop) {
5503 ql_dbg(ql_dbg_tgt, vha, 0xe05f,
5504 "qla_target: Unable to send "
5505 "command to target, sending TERM "
5506 "EXCHANGE for rsp\n");
5507 qlt_send_term_exchange(rsp->qpair, NULL,
5508 atio, 1, 0);
5509 } else {
5510 ql_dbg(ql_dbg_tgt, vha, 0xe060,
5511 "qla_target(%d): Unable to send "
5512 "command to target, sending BUSY "
5513 "status\n", vha->vp_idx);
5514 qlt_send_busy(rsp->qpair, atio, 0);
5519 break;
5521 case CONTINUE_TGT_IO_TYPE:
5523 struct ctio_to_2xxx *entry = (struct ctio_to_2xxx *)pkt;
5524 qlt_do_ctio_completion(vha, rsp, entry->handle,
5525 le16_to_cpu(entry->status)|(pkt->entry_status << 16),
5526 entry);
5527 break;
5530 case CTIO_A64_TYPE:
5532 struct ctio_to_2xxx *entry = (struct ctio_to_2xxx *)pkt;
5533 qlt_do_ctio_completion(vha, rsp, entry->handle,
5534 le16_to_cpu(entry->status)|(pkt->entry_status << 16),
5535 entry);
5536 break;
5539 case IMMED_NOTIFY_TYPE:
5540 ql_dbg(ql_dbg_tgt, vha, 0xe035, "%s", "IMMED_NOTIFY\n");
5541 qlt_handle_imm_notify(vha, (struct imm_ntfy_from_isp *)pkt);
5542 break;
5544 case NOTIFY_ACK_TYPE:
5545 if (tgt->notify_ack_expected > 0) {
5546 struct nack_to_isp *entry = (struct nack_to_isp *)pkt;
5547 ql_dbg(ql_dbg_tgt, vha, 0xe036,
5548 "NOTIFY_ACK seq %08x status %x\n",
5549 le16_to_cpu(entry->u.isp2x.seq_id),
5550 le16_to_cpu(entry->u.isp2x.status));
5551 tgt->notify_ack_expected--;
5552 if (entry->u.isp2x.status !=
5553 cpu_to_le16(NOTIFY_ACK_SUCCESS)) {
5554 ql_dbg(ql_dbg_tgt, vha, 0xe061,
5555 "qla_target(%d): NOTIFY_ACK "
5556 "failed %x\n", vha->vp_idx,
5557 le16_to_cpu(entry->u.isp2x.status));
5559 } else {
5560 ql_dbg(ql_dbg_tgt, vha, 0xe062,
5561 "qla_target(%d): Unexpected NOTIFY_ACK received\n",
5562 vha->vp_idx);
5564 break;
5566 case ABTS_RECV_24XX:
5567 ql_dbg(ql_dbg_tgt, vha, 0xe037,
5568 "ABTS_RECV_24XX: instance %d\n", vha->vp_idx);
5569 qlt_24xx_handle_abts(vha, (struct abts_recv_from_24xx *)pkt);
5570 break;
5572 case ABTS_RESP_24XX:
5573 if (tgt->abts_resp_expected > 0) {
5574 struct abts_resp_from_24xx_fw *entry =
5575 (struct abts_resp_from_24xx_fw *)pkt;
5576 ql_dbg(ql_dbg_tgt, vha, 0xe038,
5577 "ABTS_RESP_24XX: compl_status %x\n",
5578 entry->compl_status);
5579 tgt->abts_resp_expected--;
5580 if (le16_to_cpu(entry->compl_status) !=
5581 ABTS_RESP_COMPL_SUCCESS) {
5582 if ((entry->error_subcode1 == 0x1E) &&
5583 (entry->error_subcode2 == 0)) {
5585 * We've got a race here: aborted
5586 * exchange not terminated, i.e.
5587 * response for the aborted command was
5588 * sent between the abort request was
5589 * received and processed.
5590 * Unfortunately, the firmware has a
5591 * silly requirement that all aborted
5592 * exchanges must be explicitely
5593 * terminated, otherwise it refuses to
5594 * send responses for the abort
5595 * requests. So, we have to
5596 * (re)terminate the exchange and retry
5597 * the abort response.
5599 qlt_24xx_retry_term_exchange(vha,
5600 entry);
5601 } else
5602 ql_dbg(ql_dbg_tgt, vha, 0xe063,
5603 "qla_target(%d): ABTS_RESP_24XX "
5604 "failed %x (subcode %x:%x)",
5605 vha->vp_idx, entry->compl_status,
5606 entry->error_subcode1,
5607 entry->error_subcode2);
5609 } else {
5610 ql_dbg(ql_dbg_tgt, vha, 0xe064,
5611 "qla_target(%d): Unexpected ABTS_RESP_24XX "
5612 "received\n", vha->vp_idx);
5614 break;
5616 default:
5617 ql_dbg(ql_dbg_tgt, vha, 0xe065,
5618 "qla_target(%d): Received unknown response pkt "
5619 "type %x\n", vha->vp_idx, pkt->entry_type);
5620 break;
5626 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
5628 void qlt_async_event(uint16_t code, struct scsi_qla_host *vha,
5629 uint16_t *mailbox)
5631 struct qla_hw_data *ha = vha->hw;
5632 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
5633 int login_code;
5635 if (!tgt || tgt->tgt_stop || tgt->tgt_stopped)
5636 return;
5638 if (((code == MBA_POINT_TO_POINT) || (code == MBA_CHG_IN_CONNECTION)) &&
5639 IS_QLA2100(ha))
5640 return;
5642 * In tgt_stop mode we also should allow all requests to pass.
5643 * Otherwise, some commands can stuck.
5647 switch (code) {
5648 case MBA_RESET: /* Reset */
5649 case MBA_SYSTEM_ERR: /* System Error */
5650 case MBA_REQ_TRANSFER_ERR: /* Request Transfer Error */
5651 case MBA_RSP_TRANSFER_ERR: /* Response Transfer Error */
5652 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03a,
5653 "qla_target(%d): System error async event %#x "
5654 "occurred", vha->vp_idx, code);
5655 break;
5656 case MBA_WAKEUP_THRES: /* Request Queue Wake-up. */
5657 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
5658 break;
5660 case MBA_LOOP_UP:
5662 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03b,
5663 "qla_target(%d): Async LOOP_UP occurred "
5664 "(m[0]=%x, m[1]=%x, m[2]=%x, m[3]=%x)", vha->vp_idx,
5665 le16_to_cpu(mailbox[0]), le16_to_cpu(mailbox[1]),
5666 le16_to_cpu(mailbox[2]), le16_to_cpu(mailbox[3]));
5667 if (tgt->link_reinit_iocb_pending) {
5668 qlt_send_notify_ack(ha->base_qpair,
5669 (void *)&tgt->link_reinit_iocb,
5670 0, 0, 0, 0, 0, 0);
5671 tgt->link_reinit_iocb_pending = 0;
5673 break;
5676 case MBA_LIP_OCCURRED:
5677 case MBA_LOOP_DOWN:
5678 case MBA_LIP_RESET:
5679 case MBA_RSCN_UPDATE:
5680 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03c,
5681 "qla_target(%d): Async event %#x occurred "
5682 "(m[0]=%x, m[1]=%x, m[2]=%x, m[3]=%x)", vha->vp_idx, code,
5683 le16_to_cpu(mailbox[0]), le16_to_cpu(mailbox[1]),
5684 le16_to_cpu(mailbox[2]), le16_to_cpu(mailbox[3]));
5685 break;
5687 case MBA_REJECTED_FCP_CMD:
5688 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf017,
5689 "qla_target(%d): Async event LS_REJECT occurred (m[0]=%x, m[1]=%x, m[2]=%x, m[3]=%x)",
5690 vha->vp_idx,
5691 le16_to_cpu(mailbox[0]), le16_to_cpu(mailbox[1]),
5692 le16_to_cpu(mailbox[2]), le16_to_cpu(mailbox[3]));
5694 if (le16_to_cpu(mailbox[3]) == 1) {
5695 /* exchange starvation. */
5696 vha->hw->exch_starvation++;
5697 if (vha->hw->exch_starvation > 5) {
5698 ql_log(ql_log_warn, vha, 0xd03a,
5699 "Exchange starvation-. Resetting RISC\n");
5701 vha->hw->exch_starvation = 0;
5702 if (IS_P3P_TYPE(vha->hw))
5703 set_bit(FCOE_CTX_RESET_NEEDED,
5704 &vha->dpc_flags);
5705 else
5706 set_bit(ISP_ABORT_NEEDED,
5707 &vha->dpc_flags);
5708 qla2xxx_wake_dpc(vha);
5711 break;
5713 case MBA_PORT_UPDATE:
5714 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03d,
5715 "qla_target(%d): Port update async event %#x "
5716 "occurred: updating the ports database (m[0]=%x, m[1]=%x, "
5717 "m[2]=%x, m[3]=%x)", vha->vp_idx, code,
5718 le16_to_cpu(mailbox[0]), le16_to_cpu(mailbox[1]),
5719 le16_to_cpu(mailbox[2]), le16_to_cpu(mailbox[3]));
5721 login_code = le16_to_cpu(mailbox[2]);
5722 if (login_code == 0x4) {
5723 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03e,
5724 "Async MB 2: Got PLOGI Complete\n");
5725 vha->hw->exch_starvation = 0;
5726 } else if (login_code == 0x7)
5727 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03f,
5728 "Async MB 2: Port Logged Out\n");
5729 break;
5730 default:
5731 break;
5736 static fc_port_t *qlt_get_port_database(struct scsi_qla_host *vha,
5737 uint16_t loop_id)
5739 fc_port_t *fcport, *tfcp, *del;
5740 int rc;
5741 unsigned long flags;
5742 u8 newfcport = 0;
5744 fcport = kzalloc(sizeof(*fcport), GFP_KERNEL);
5745 if (!fcport) {
5746 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06f,
5747 "qla_target(%d): Allocation of tmp FC port failed",
5748 vha->vp_idx);
5749 return NULL;
5752 fcport->loop_id = loop_id;
5754 rc = qla24xx_gpdb_wait(vha, fcport, 0);
5755 if (rc != QLA_SUCCESS) {
5756 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf070,
5757 "qla_target(%d): Failed to retrieve fcport "
5758 "information -- get_port_database() returned %x "
5759 "(loop_id=0x%04x)", vha->vp_idx, rc, loop_id);
5760 kfree(fcport);
5761 return NULL;
5764 del = NULL;
5765 spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
5766 tfcp = qla2x00_find_fcport_by_wwpn(vha, fcport->port_name, 1);
5768 if (tfcp) {
5769 tfcp->d_id = fcport->d_id;
5770 tfcp->port_type = fcport->port_type;
5771 tfcp->supported_classes = fcport->supported_classes;
5772 tfcp->flags |= fcport->flags;
5774 del = fcport;
5775 fcport = tfcp;
5776 } else {
5777 if (vha->hw->current_topology == ISP_CFG_F)
5778 fcport->flags |= FCF_FABRIC_DEVICE;
5780 list_add_tail(&fcport->list, &vha->vp_fcports);
5781 if (!IS_SW_RESV_ADDR(fcport->d_id))
5782 vha->fcport_count++;
5783 fcport->login_gen++;
5784 fcport->disc_state = DSC_LOGIN_COMPLETE;
5785 fcport->login_succ = 1;
5786 newfcport = 1;
5789 fcport->deleted = 0;
5790 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
5792 switch (vha->host->active_mode) {
5793 case MODE_INITIATOR:
5794 case MODE_DUAL:
5795 if (newfcport) {
5796 if (!IS_IIDMA_CAPABLE(vha->hw) || !vha->hw->flags.gpsc_supported) {
5797 ql_dbg(ql_dbg_disc, vha, 0x20fe,
5798 "%s %d %8phC post upd_fcport fcp_cnt %d\n",
5799 __func__, __LINE__, fcport->port_name, vha->fcport_count);
5800 qla24xx_post_upd_fcport_work(vha, fcport);
5801 } else {
5802 ql_dbg(ql_dbg_disc, vha, 0x20ff,
5803 "%s %d %8phC post gpsc fcp_cnt %d\n",
5804 __func__, __LINE__, fcport->port_name, vha->fcport_count);
5805 qla24xx_post_gpsc_work(vha, fcport);
5808 break;
5810 case MODE_TARGET:
5811 default:
5812 break;
5814 if (del)
5815 qla2x00_free_fcport(del);
5817 return fcport;
5820 /* Must be called under tgt_mutex */
5821 static struct fc_port *qlt_make_local_sess(struct scsi_qla_host *vha,
5822 uint8_t *s_id)
5824 struct fc_port *sess = NULL;
5825 fc_port_t *fcport = NULL;
5826 int rc, global_resets;
5827 uint16_t loop_id = 0;
5829 if ((s_id[0] == 0xFF) && (s_id[1] == 0xFC)) {
5831 * This is Domain Controller, so it should be
5832 * OK to drop SCSI commands from it.
5834 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf042,
5835 "Unable to find initiator with S_ID %x:%x:%x",
5836 s_id[0], s_id[1], s_id[2]);
5837 return NULL;
5840 mutex_lock(&vha->vha_tgt.tgt_mutex);
5842 retry:
5843 global_resets =
5844 atomic_read(&vha->vha_tgt.qla_tgt->tgt_global_resets_count);
5846 rc = qla24xx_get_loop_id(vha, s_id, &loop_id);
5847 if (rc != 0) {
5848 mutex_unlock(&vha->vha_tgt.tgt_mutex);
5850 ql_log(ql_log_info, vha, 0xf071,
5851 "qla_target(%d): Unable to find "
5852 "initiator with S_ID %x:%x:%x",
5853 vha->vp_idx, s_id[0], s_id[1],
5854 s_id[2]);
5856 if (rc == -ENOENT) {
5857 qlt_port_logo_t logo;
5858 sid_to_portid(s_id, &logo.id);
5859 logo.cmd_count = 1;
5860 qlt_send_first_logo(vha, &logo);
5863 return NULL;
5866 fcport = qlt_get_port_database(vha, loop_id);
5867 if (!fcport) {
5868 mutex_unlock(&vha->vha_tgt.tgt_mutex);
5869 return NULL;
5872 if (global_resets !=
5873 atomic_read(&vha->vha_tgt.qla_tgt->tgt_global_resets_count)) {
5874 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf043,
5875 "qla_target(%d): global reset during session discovery "
5876 "(counter was %d, new %d), retrying", vha->vp_idx,
5877 global_resets,
5878 atomic_read(&vha->vha_tgt.
5879 qla_tgt->tgt_global_resets_count));
5880 goto retry;
5883 sess = qlt_create_sess(vha, fcport, true);
5885 mutex_unlock(&vha->vha_tgt.tgt_mutex);
5887 return sess;
5890 static void qlt_abort_work(struct qla_tgt *tgt,
5891 struct qla_tgt_sess_work_param *prm)
5893 struct scsi_qla_host *vha = tgt->vha;
5894 struct qla_hw_data *ha = vha->hw;
5895 struct fc_port *sess = NULL;
5896 unsigned long flags = 0, flags2 = 0;
5897 uint32_t be_s_id;
5898 uint8_t s_id[3];
5899 int rc;
5901 spin_lock_irqsave(&ha->tgt.sess_lock, flags2);
5903 if (tgt->tgt_stop)
5904 goto out_term2;
5906 s_id[0] = prm->abts.fcp_hdr_le.s_id[2];
5907 s_id[1] = prm->abts.fcp_hdr_le.s_id[1];
5908 s_id[2] = prm->abts.fcp_hdr_le.s_id[0];
5910 sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha,
5911 (unsigned char *)&be_s_id);
5912 if (!sess) {
5913 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags2);
5915 sess = qlt_make_local_sess(vha, s_id);
5916 /* sess has got an extra creation ref */
5918 spin_lock_irqsave(&ha->tgt.sess_lock, flags2);
5919 if (!sess)
5920 goto out_term2;
5921 } else {
5922 if (sess->deleted) {
5923 sess = NULL;
5924 goto out_term2;
5927 if (!kref_get_unless_zero(&sess->sess_kref)) {
5928 ql_dbg(ql_dbg_tgt_tmr, vha, 0xf01c,
5929 "%s: kref_get fail %8phC \n",
5930 __func__, sess->port_name);
5931 sess = NULL;
5932 goto out_term2;
5936 rc = __qlt_24xx_handle_abts(vha, &prm->abts, sess);
5937 ha->tgt.tgt_ops->put_sess(sess);
5938 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags2);
5940 if (rc != 0)
5941 goto out_term;
5942 return;
5944 out_term2:
5945 if (sess)
5946 ha->tgt.tgt_ops->put_sess(sess);
5947 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags2);
5949 out_term:
5950 spin_lock_irqsave(&ha->hardware_lock, flags);
5951 qlt_24xx_send_abts_resp(ha->base_qpair, &prm->abts,
5952 FCP_TMF_REJECTED, false);
5953 spin_unlock_irqrestore(&ha->hardware_lock, flags);
5956 static void qlt_tmr_work(struct qla_tgt *tgt,
5957 struct qla_tgt_sess_work_param *prm)
5959 struct atio_from_isp *a = &prm->tm_iocb2;
5960 struct scsi_qla_host *vha = tgt->vha;
5961 struct qla_hw_data *ha = vha->hw;
5962 struct fc_port *sess = NULL;
5963 unsigned long flags;
5964 uint8_t *s_id = NULL; /* to hide compiler warnings */
5965 int rc;
5966 u64 unpacked_lun;
5967 int fn;
5968 void *iocb;
5970 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
5972 if (tgt->tgt_stop)
5973 goto out_term2;
5975 s_id = prm->tm_iocb2.u.isp24.fcp_hdr.s_id;
5976 sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha, s_id);
5977 if (!sess) {
5978 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
5980 sess = qlt_make_local_sess(vha, s_id);
5981 /* sess has got an extra creation ref */
5983 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
5984 if (!sess)
5985 goto out_term2;
5986 } else {
5987 if (sess->deleted) {
5988 sess = NULL;
5989 goto out_term2;
5992 if (!kref_get_unless_zero(&sess->sess_kref)) {
5993 ql_dbg(ql_dbg_tgt_tmr, vha, 0xf020,
5994 "%s: kref_get fail %8phC\n",
5995 __func__, sess->port_name);
5996 sess = NULL;
5997 goto out_term2;
6001 iocb = a;
6002 fn = a->u.isp24.fcp_cmnd.task_mgmt_flags;
6003 unpacked_lun =
6004 scsilun_to_int((struct scsi_lun *)&a->u.isp24.fcp_cmnd.lun);
6006 rc = qlt_issue_task_mgmt(sess, unpacked_lun, fn, iocb, 0);
6007 ha->tgt.tgt_ops->put_sess(sess);
6008 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
6010 if (rc != 0)
6011 goto out_term;
6012 return;
6014 out_term2:
6015 if (sess)
6016 ha->tgt.tgt_ops->put_sess(sess);
6017 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
6018 out_term:
6019 qlt_send_term_exchange(ha->base_qpair, NULL, &prm->tm_iocb2, 1, 0);
6022 static void qlt_sess_work_fn(struct work_struct *work)
6024 struct qla_tgt *tgt = container_of(work, struct qla_tgt, sess_work);
6025 struct scsi_qla_host *vha = tgt->vha;
6026 unsigned long flags;
6028 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf000, "Sess work (tgt %p)", tgt);
6030 spin_lock_irqsave(&tgt->sess_work_lock, flags);
6031 while (!list_empty(&tgt->sess_works_list)) {
6032 struct qla_tgt_sess_work_param *prm = list_entry(
6033 tgt->sess_works_list.next, typeof(*prm),
6034 sess_works_list_entry);
6037 * This work can be scheduled on several CPUs at time, so we
6038 * must delete the entry to eliminate double processing
6040 list_del(&prm->sess_works_list_entry);
6042 spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
6044 switch (prm->type) {
6045 case QLA_TGT_SESS_WORK_ABORT:
6046 qlt_abort_work(tgt, prm);
6047 break;
6048 case QLA_TGT_SESS_WORK_TM:
6049 qlt_tmr_work(tgt, prm);
6050 break;
6051 default:
6052 BUG_ON(1);
6053 break;
6056 spin_lock_irqsave(&tgt->sess_work_lock, flags);
6058 kfree(prm);
6060 spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
6063 /* Must be called under tgt_host_action_mutex */
6064 int qlt_add_target(struct qla_hw_data *ha, struct scsi_qla_host *base_vha)
6066 struct qla_tgt *tgt;
6067 int rc, i;
6068 struct qla_qpair_hint *h;
6070 if (!QLA_TGT_MODE_ENABLED())
6071 return 0;
6073 if (!IS_TGT_MODE_CAPABLE(ha)) {
6074 ql_log(ql_log_warn, base_vha, 0xe070,
6075 "This adapter does not support target mode.\n");
6076 return 0;
6079 ql_dbg(ql_dbg_tgt, base_vha, 0xe03b,
6080 "Registering target for host %ld(%p).\n", base_vha->host_no, ha);
6082 BUG_ON(base_vha->vha_tgt.qla_tgt != NULL);
6084 tgt = kzalloc(sizeof(struct qla_tgt), GFP_KERNEL);
6085 if (!tgt) {
6086 ql_dbg(ql_dbg_tgt, base_vha, 0xe066,
6087 "Unable to allocate struct qla_tgt\n");
6088 return -ENOMEM;
6091 tgt->qphints = kzalloc((ha->max_qpairs + 1) *
6092 sizeof(struct qla_qpair_hint), GFP_KERNEL);
6093 if (!tgt->qphints) {
6094 kfree(tgt);
6095 ql_log(ql_log_warn, base_vha, 0x0197,
6096 "Unable to allocate qpair hints.\n");
6097 return -ENOMEM;
6100 if (!(base_vha->host->hostt->supported_mode & MODE_TARGET))
6101 base_vha->host->hostt->supported_mode |= MODE_TARGET;
6103 rc = btree_init64(&tgt->lun_qpair_map);
6104 if (rc) {
6105 kfree(tgt->qphints);
6106 kfree(tgt);
6107 ql_log(ql_log_info, base_vha, 0x0198,
6108 "Unable to initialize lun_qpair_map btree\n");
6109 return -EIO;
6111 h = &tgt->qphints[0];
6112 h->qpair = ha->base_qpair;
6113 INIT_LIST_HEAD(&h->hint_elem);
6114 h->cpuid = ha->base_qpair->cpuid;
6115 list_add_tail(&h->hint_elem, &ha->base_qpair->hints_list);
6117 for (i = 0; i < ha->max_qpairs; i++) {
6118 unsigned long flags;
6120 struct qla_qpair *qpair = ha->queue_pair_map[i];
6121 h = &tgt->qphints[i + 1];
6122 INIT_LIST_HEAD(&h->hint_elem);
6123 if (qpair) {
6124 h->qpair = qpair;
6125 spin_lock_irqsave(qpair->qp_lock_ptr, flags);
6126 list_add_tail(&h->hint_elem, &qpair->hints_list);
6127 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
6128 h->cpuid = qpair->cpuid;
6132 tgt->ha = ha;
6133 tgt->vha = base_vha;
6134 init_waitqueue_head(&tgt->waitQ);
6135 INIT_LIST_HEAD(&tgt->del_sess_list);
6136 spin_lock_init(&tgt->sess_work_lock);
6137 INIT_WORK(&tgt->sess_work, qlt_sess_work_fn);
6138 INIT_LIST_HEAD(&tgt->sess_works_list);
6139 atomic_set(&tgt->tgt_global_resets_count, 0);
6141 base_vha->vha_tgt.qla_tgt = tgt;
6143 ql_dbg(ql_dbg_tgt, base_vha, 0xe067,
6144 "qla_target(%d): using 64 Bit PCI addressing",
6145 base_vha->vp_idx);
6146 /* 3 is reserved */
6147 tgt->sg_tablesize = QLA_TGT_MAX_SG_24XX(base_vha->req->length - 3);
6149 mutex_lock(&qla_tgt_mutex);
6150 list_add_tail(&tgt->tgt_list_entry, &qla_tgt_glist);
6151 mutex_unlock(&qla_tgt_mutex);
6153 if (ha->tgt.tgt_ops && ha->tgt.tgt_ops->add_target)
6154 ha->tgt.tgt_ops->add_target(base_vha);
6156 return 0;
6159 /* Must be called under tgt_host_action_mutex */
6160 int qlt_remove_target(struct qla_hw_data *ha, struct scsi_qla_host *vha)
6162 if (!vha->vha_tgt.qla_tgt)
6163 return 0;
6165 if (vha->fc_vport) {
6166 qlt_release(vha->vha_tgt.qla_tgt);
6167 return 0;
6170 /* free left over qfull cmds */
6171 qlt_init_term_exchange(vha);
6173 mutex_lock(&qla_tgt_mutex);
6174 list_del(&vha->vha_tgt.qla_tgt->tgt_list_entry);
6175 mutex_unlock(&qla_tgt_mutex);
6177 ql_dbg(ql_dbg_tgt, vha, 0xe03c, "Unregistering target for host %ld(%p)",
6178 vha->host_no, ha);
6179 qlt_release(vha->vha_tgt.qla_tgt);
6181 return 0;
6184 void qlt_remove_target_resources(struct qla_hw_data *ha)
6186 struct scsi_qla_host *node;
6187 u32 key = 0;
6189 btree_for_each_safe32(&ha->tgt.host_map, key, node)
6190 btree_remove32(&ha->tgt.host_map, key);
6192 btree_destroy32(&ha->tgt.host_map);
6195 static void qlt_lport_dump(struct scsi_qla_host *vha, u64 wwpn,
6196 unsigned char *b)
6198 int i;
6200 pr_debug("qla2xxx HW vha->node_name: ");
6201 for (i = 0; i < WWN_SIZE; i++)
6202 pr_debug("%02x ", vha->node_name[i]);
6203 pr_debug("\n");
6204 pr_debug("qla2xxx HW vha->port_name: ");
6205 for (i = 0; i < WWN_SIZE; i++)
6206 pr_debug("%02x ", vha->port_name[i]);
6207 pr_debug("\n");
6209 pr_debug("qla2xxx passed configfs WWPN: ");
6210 put_unaligned_be64(wwpn, b);
6211 for (i = 0; i < WWN_SIZE; i++)
6212 pr_debug("%02x ", b[i]);
6213 pr_debug("\n");
6217 * qla_tgt_lport_register - register lport with external module
6219 * @qla_tgt_ops: Pointer for tcm_qla2xxx qla_tgt_ops
6220 * @wwpn: Passwd FC target WWPN
6221 * @callback: lport initialization callback for tcm_qla2xxx code
6222 * @target_lport_ptr: pointer for tcm_qla2xxx specific lport data
6224 int qlt_lport_register(void *target_lport_ptr, u64 phys_wwpn,
6225 u64 npiv_wwpn, u64 npiv_wwnn,
6226 int (*callback)(struct scsi_qla_host *, void *, u64, u64))
6228 struct qla_tgt *tgt;
6229 struct scsi_qla_host *vha;
6230 struct qla_hw_data *ha;
6231 struct Scsi_Host *host;
6232 unsigned long flags;
6233 int rc;
6234 u8 b[WWN_SIZE];
6236 mutex_lock(&qla_tgt_mutex);
6237 list_for_each_entry(tgt, &qla_tgt_glist, tgt_list_entry) {
6238 vha = tgt->vha;
6239 ha = vha->hw;
6241 host = vha->host;
6242 if (!host)
6243 continue;
6245 if (!(host->hostt->supported_mode & MODE_TARGET))
6246 continue;
6248 spin_lock_irqsave(&ha->hardware_lock, flags);
6249 if ((!npiv_wwpn || !npiv_wwnn) && host->active_mode & MODE_TARGET) {
6250 pr_debug("MODE_TARGET already active on qla2xxx(%d)\n",
6251 host->host_no);
6252 spin_unlock_irqrestore(&ha->hardware_lock, flags);
6253 continue;
6255 if (tgt->tgt_stop) {
6256 pr_debug("MODE_TARGET in shutdown on qla2xxx(%d)\n",
6257 host->host_no);
6258 spin_unlock_irqrestore(&ha->hardware_lock, flags);
6259 continue;
6261 spin_unlock_irqrestore(&ha->hardware_lock, flags);
6263 if (!scsi_host_get(host)) {
6264 ql_dbg(ql_dbg_tgt, vha, 0xe068,
6265 "Unable to scsi_host_get() for"
6266 " qla2xxx scsi_host\n");
6267 continue;
6269 qlt_lport_dump(vha, phys_wwpn, b);
6271 if (memcmp(vha->port_name, b, WWN_SIZE)) {
6272 scsi_host_put(host);
6273 continue;
6275 rc = (*callback)(vha, target_lport_ptr, npiv_wwpn, npiv_wwnn);
6276 if (rc != 0)
6277 scsi_host_put(host);
6279 mutex_unlock(&qla_tgt_mutex);
6280 return rc;
6282 mutex_unlock(&qla_tgt_mutex);
6284 return -ENODEV;
6286 EXPORT_SYMBOL(qlt_lport_register);
6289 * qla_tgt_lport_deregister - Degister lport
6291 * @vha: Registered scsi_qla_host pointer
6293 void qlt_lport_deregister(struct scsi_qla_host *vha)
6295 struct qla_hw_data *ha = vha->hw;
6296 struct Scsi_Host *sh = vha->host;
6298 * Clear the target_lport_ptr qla_target_template pointer in qla_hw_data
6300 vha->vha_tgt.target_lport_ptr = NULL;
6301 ha->tgt.tgt_ops = NULL;
6303 * Release the Scsi_Host reference for the underlying qla2xxx host
6305 scsi_host_put(sh);
6307 EXPORT_SYMBOL(qlt_lport_deregister);
6309 /* Must be called under HW lock */
6310 static void qlt_set_mode(struct scsi_qla_host *vha)
6312 switch (ql2x_ini_mode) {
6313 case QLA2XXX_INI_MODE_DISABLED:
6314 case QLA2XXX_INI_MODE_EXCLUSIVE:
6315 vha->host->active_mode = MODE_TARGET;
6316 break;
6317 case QLA2XXX_INI_MODE_ENABLED:
6318 vha->host->active_mode = MODE_UNKNOWN;
6319 break;
6320 case QLA2XXX_INI_MODE_DUAL:
6321 vha->host->active_mode = MODE_DUAL;
6322 break;
6323 default:
6324 break;
6328 /* Must be called under HW lock */
6329 static void qlt_clear_mode(struct scsi_qla_host *vha)
6331 switch (ql2x_ini_mode) {
6332 case QLA2XXX_INI_MODE_DISABLED:
6333 vha->host->active_mode = MODE_UNKNOWN;
6334 break;
6335 case QLA2XXX_INI_MODE_EXCLUSIVE:
6336 vha->host->active_mode = MODE_INITIATOR;
6337 break;
6338 case QLA2XXX_INI_MODE_ENABLED:
6339 case QLA2XXX_INI_MODE_DUAL:
6340 vha->host->active_mode = MODE_INITIATOR;
6341 break;
6342 default:
6343 break;
6348 * qla_tgt_enable_vha - NO LOCK HELD
6350 * host_reset, bring up w/ Target Mode Enabled
6352 void
6353 qlt_enable_vha(struct scsi_qla_host *vha)
6355 struct qla_hw_data *ha = vha->hw;
6356 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
6357 unsigned long flags;
6358 scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
6360 if (!tgt) {
6361 ql_dbg(ql_dbg_tgt, vha, 0xe069,
6362 "Unable to locate qla_tgt pointer from"
6363 " struct qla_hw_data\n");
6364 dump_stack();
6365 return;
6368 spin_lock_irqsave(&ha->hardware_lock, flags);
6369 tgt->tgt_stopped = 0;
6370 qlt_set_mode(vha);
6371 spin_unlock_irqrestore(&ha->hardware_lock, flags);
6373 if (vha->vp_idx) {
6374 qla24xx_disable_vp(vha);
6375 qla24xx_enable_vp(vha);
6376 } else {
6377 set_bit(ISP_ABORT_NEEDED, &base_vha->dpc_flags);
6378 qla2xxx_wake_dpc(base_vha);
6379 qla2x00_wait_for_hba_online(base_vha);
6382 EXPORT_SYMBOL(qlt_enable_vha);
6385 * qla_tgt_disable_vha - NO LOCK HELD
6387 * Disable Target Mode and reset the adapter
6389 static void qlt_disable_vha(struct scsi_qla_host *vha)
6391 struct qla_hw_data *ha = vha->hw;
6392 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
6393 unsigned long flags;
6395 if (!tgt) {
6396 ql_dbg(ql_dbg_tgt, vha, 0xe06a,
6397 "Unable to locate qla_tgt pointer from"
6398 " struct qla_hw_data\n");
6399 dump_stack();
6400 return;
6403 spin_lock_irqsave(&ha->hardware_lock, flags);
6404 qlt_clear_mode(vha);
6405 spin_unlock_irqrestore(&ha->hardware_lock, flags);
6407 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
6408 qla2xxx_wake_dpc(vha);
6409 qla2x00_wait_for_hba_online(vha);
6413 * Called from qla_init.c:qla24xx_vport_create() contex to setup
6414 * the target mode specific struct scsi_qla_host and struct qla_hw_data
6415 * members.
6417 void
6418 qlt_vport_create(struct scsi_qla_host *vha, struct qla_hw_data *ha)
6420 vha->vha_tgt.qla_tgt = NULL;
6422 mutex_init(&vha->vha_tgt.tgt_mutex);
6423 mutex_init(&vha->vha_tgt.tgt_host_action_mutex);
6425 qlt_clear_mode(vha);
6428 * NOTE: Currently the value is kept the same for <24xx and
6429 * >=24xx ISPs. If it is necessary to change it,
6430 * the check should be added for specific ISPs,
6431 * assigning the value appropriately.
6433 ha->tgt.atio_q_length = ATIO_ENTRY_CNT_24XX;
6435 qlt_add_target(ha, vha);
6438 void
6439 qlt_rff_id(struct scsi_qla_host *vha, struct ct_sns_req *ct_req)
6442 * FC-4 Feature bit 0 indicates target functionality to the name server.
6444 if (qla_tgt_mode_enabled(vha)) {
6445 ct_req->req.rff_id.fc4_feature = BIT_0;
6446 } else if (qla_ini_mode_enabled(vha)) {
6447 ct_req->req.rff_id.fc4_feature = BIT_1;
6448 } else if (qla_dual_mode_enabled(vha))
6449 ct_req->req.rff_id.fc4_feature = BIT_0 | BIT_1;
6453 * qlt_init_atio_q_entries() - Initializes ATIO queue entries.
6454 * @ha: HA context
6456 * Beginning of ATIO ring has initialization control block already built
6457 * by nvram config routine.
6459 * Returns 0 on success.
6461 void
6462 qlt_init_atio_q_entries(struct scsi_qla_host *vha)
6464 struct qla_hw_data *ha = vha->hw;
6465 uint16_t cnt;
6466 struct atio_from_isp *pkt = (struct atio_from_isp *)ha->tgt.atio_ring;
6468 if (qla_ini_mode_enabled(vha))
6469 return;
6471 for (cnt = 0; cnt < ha->tgt.atio_q_length; cnt++) {
6472 pkt->u.raw.signature = ATIO_PROCESSED;
6473 pkt++;
6479 * qlt_24xx_process_atio_queue() - Process ATIO queue entries.
6480 * @ha: SCSI driver HA context
6482 void
6483 qlt_24xx_process_atio_queue(struct scsi_qla_host *vha, uint8_t ha_locked)
6485 struct qla_hw_data *ha = vha->hw;
6486 struct atio_from_isp *pkt;
6487 int cnt, i;
6489 if (!ha->flags.fw_started)
6490 return;
6492 while ((ha->tgt.atio_ring_ptr->signature != ATIO_PROCESSED) ||
6493 fcpcmd_is_corrupted(ha->tgt.atio_ring_ptr)) {
6494 pkt = (struct atio_from_isp *)ha->tgt.atio_ring_ptr;
6495 cnt = pkt->u.raw.entry_count;
6497 if (unlikely(fcpcmd_is_corrupted(ha->tgt.atio_ring_ptr))) {
6499 * This packet is corrupted. The header + payload
6500 * can not be trusted. There is no point in passing
6501 * it further up.
6503 ql_log(ql_log_warn, vha, 0xd03c,
6504 "corrupted fcp frame SID[%3phN] OXID[%04x] EXCG[%x] %64phN\n",
6505 pkt->u.isp24.fcp_hdr.s_id,
6506 be16_to_cpu(pkt->u.isp24.fcp_hdr.ox_id),
6507 le32_to_cpu(pkt->u.isp24.exchange_addr), pkt);
6509 adjust_corrupted_atio(pkt);
6510 qlt_send_term_exchange(ha->base_qpair, NULL, pkt,
6511 ha_locked, 0);
6512 } else {
6513 qlt_24xx_atio_pkt_all_vps(vha,
6514 (struct atio_from_isp *)pkt, ha_locked);
6517 for (i = 0; i < cnt; i++) {
6518 ha->tgt.atio_ring_index++;
6519 if (ha->tgt.atio_ring_index == ha->tgt.atio_q_length) {
6520 ha->tgt.atio_ring_index = 0;
6521 ha->tgt.atio_ring_ptr = ha->tgt.atio_ring;
6522 } else
6523 ha->tgt.atio_ring_ptr++;
6525 pkt->u.raw.signature = ATIO_PROCESSED;
6526 pkt = (struct atio_from_isp *)ha->tgt.atio_ring_ptr;
6528 wmb();
6531 /* Adjust ring index */
6532 WRT_REG_DWORD(ISP_ATIO_Q_OUT(vha), ha->tgt.atio_ring_index);
6533 RD_REG_DWORD_RELAXED(ISP_ATIO_Q_OUT(vha));
6536 void
6537 qlt_24xx_config_rings(struct scsi_qla_host *vha)
6539 struct qla_hw_data *ha = vha->hw;
6540 if (!QLA_TGT_MODE_ENABLED())
6541 return;
6543 WRT_REG_DWORD(ISP_ATIO_Q_IN(vha), 0);
6544 WRT_REG_DWORD(ISP_ATIO_Q_OUT(vha), 0);
6545 RD_REG_DWORD(ISP_ATIO_Q_OUT(vha));
6547 if (IS_ATIO_MSIX_CAPABLE(ha)) {
6548 struct qla_msix_entry *msix = &ha->msix_entries[2];
6549 struct init_cb_24xx *icb = (struct init_cb_24xx *)ha->init_cb;
6551 icb->msix_atio = cpu_to_le16(msix->entry);
6552 ql_dbg(ql_dbg_init, vha, 0xf072,
6553 "Registering ICB vector 0x%x for atio que.\n",
6554 msix->entry);
6558 void
6559 qlt_24xx_config_nvram_stage1(struct scsi_qla_host *vha, struct nvram_24xx *nv)
6561 struct qla_hw_data *ha = vha->hw;
6563 if (!QLA_TGT_MODE_ENABLED())
6564 return;
6566 if (qla_tgt_mode_enabled(vha) || qla_dual_mode_enabled(vha)) {
6567 if (!ha->tgt.saved_set) {
6568 /* We save only once */
6569 ha->tgt.saved_exchange_count = nv->exchange_count;
6570 ha->tgt.saved_firmware_options_1 =
6571 nv->firmware_options_1;
6572 ha->tgt.saved_firmware_options_2 =
6573 nv->firmware_options_2;
6574 ha->tgt.saved_firmware_options_3 =
6575 nv->firmware_options_3;
6576 ha->tgt.saved_set = 1;
6579 if (qla_tgt_mode_enabled(vha))
6580 nv->exchange_count = cpu_to_le16(0xFFFF);
6581 else /* dual */
6582 nv->exchange_count = cpu_to_le16(ql2xexchoffld);
6584 /* Enable target mode */
6585 nv->firmware_options_1 |= cpu_to_le32(BIT_4);
6587 /* Disable ini mode, if requested */
6588 if (qla_tgt_mode_enabled(vha))
6589 nv->firmware_options_1 |= cpu_to_le32(BIT_5);
6591 /* Disable Full Login after LIP */
6592 nv->firmware_options_1 &= cpu_to_le32(~BIT_13);
6593 /* Enable initial LIP */
6594 nv->firmware_options_1 &= cpu_to_le32(~BIT_9);
6595 if (ql2xtgt_tape_enable)
6596 /* Enable FC Tape support */
6597 nv->firmware_options_2 |= cpu_to_le32(BIT_12);
6598 else
6599 /* Disable FC Tape support */
6600 nv->firmware_options_2 &= cpu_to_le32(~BIT_12);
6602 /* Disable Full Login after LIP */
6603 nv->host_p &= cpu_to_le32(~BIT_10);
6606 * clear BIT 15 explicitly as we have seen at least
6607 * a couple of instances where this was set and this
6608 * was causing the firmware to not be initialized.
6610 nv->firmware_options_1 &= cpu_to_le32(~BIT_15);
6611 /* Enable target PRLI control */
6612 nv->firmware_options_2 |= cpu_to_le32(BIT_14);
6613 } else {
6614 if (ha->tgt.saved_set) {
6615 nv->exchange_count = ha->tgt.saved_exchange_count;
6616 nv->firmware_options_1 =
6617 ha->tgt.saved_firmware_options_1;
6618 nv->firmware_options_2 =
6619 ha->tgt.saved_firmware_options_2;
6620 nv->firmware_options_3 =
6621 ha->tgt.saved_firmware_options_3;
6623 return;
6626 if (ha->base_qpair->enable_class_2) {
6627 if (vha->flags.init_done)
6628 fc_host_supported_classes(vha->host) =
6629 FC_COS_CLASS2 | FC_COS_CLASS3;
6631 nv->firmware_options_2 |= cpu_to_le32(BIT_8);
6632 } else {
6633 if (vha->flags.init_done)
6634 fc_host_supported_classes(vha->host) = FC_COS_CLASS3;
6636 nv->firmware_options_2 &= ~cpu_to_le32(BIT_8);
6640 void
6641 qlt_24xx_config_nvram_stage2(struct scsi_qla_host *vha,
6642 struct init_cb_24xx *icb)
6644 struct qla_hw_data *ha = vha->hw;
6646 if (!QLA_TGT_MODE_ENABLED())
6647 return;
6649 if (ha->tgt.node_name_set) {
6650 memcpy(icb->node_name, ha->tgt.tgt_node_name, WWN_SIZE);
6651 icb->firmware_options_1 |= cpu_to_le32(BIT_14);
6654 /* disable ZIO at start time. */
6655 if (!vha->flags.init_done) {
6656 uint32_t tmp;
6657 tmp = le32_to_cpu(icb->firmware_options_2);
6658 tmp &= ~(BIT_3 | BIT_2 | BIT_1 | BIT_0);
6659 icb->firmware_options_2 = cpu_to_le32(tmp);
6663 void
6664 qlt_81xx_config_nvram_stage1(struct scsi_qla_host *vha, struct nvram_81xx *nv)
6666 struct qla_hw_data *ha = vha->hw;
6668 if (!QLA_TGT_MODE_ENABLED())
6669 return;
6671 if (qla_tgt_mode_enabled(vha) || qla_dual_mode_enabled(vha)) {
6672 if (!ha->tgt.saved_set) {
6673 /* We save only once */
6674 ha->tgt.saved_exchange_count = nv->exchange_count;
6675 ha->tgt.saved_firmware_options_1 =
6676 nv->firmware_options_1;
6677 ha->tgt.saved_firmware_options_2 =
6678 nv->firmware_options_2;
6679 ha->tgt.saved_firmware_options_3 =
6680 nv->firmware_options_3;
6681 ha->tgt.saved_set = 1;
6684 if (qla_tgt_mode_enabled(vha))
6685 nv->exchange_count = cpu_to_le16(0xFFFF);
6686 else /* dual */
6687 nv->exchange_count = cpu_to_le16(ql2xexchoffld);
6689 /* Enable target mode */
6690 nv->firmware_options_1 |= cpu_to_le32(BIT_4);
6692 /* Disable ini mode, if requested */
6693 if (qla_tgt_mode_enabled(vha))
6694 nv->firmware_options_1 |= cpu_to_le32(BIT_5);
6695 /* Disable Full Login after LIP */
6696 nv->firmware_options_1 &= cpu_to_le32(~BIT_13);
6697 /* Enable initial LIP */
6698 nv->firmware_options_1 &= cpu_to_le32(~BIT_9);
6700 * clear BIT 15 explicitly as we have seen at
6701 * least a couple of instances where this was set
6702 * and this was causing the firmware to not be
6703 * initialized.
6705 nv->firmware_options_1 &= cpu_to_le32(~BIT_15);
6706 if (ql2xtgt_tape_enable)
6707 /* Enable FC tape support */
6708 nv->firmware_options_2 |= cpu_to_le32(BIT_12);
6709 else
6710 /* Disable FC tape support */
6711 nv->firmware_options_2 &= cpu_to_le32(~BIT_12);
6713 /* Disable Full Login after LIP */
6714 nv->host_p &= cpu_to_le32(~BIT_10);
6715 /* Enable target PRLI control */
6716 nv->firmware_options_2 |= cpu_to_le32(BIT_14);
6717 } else {
6718 if (ha->tgt.saved_set) {
6719 nv->exchange_count = ha->tgt.saved_exchange_count;
6720 nv->firmware_options_1 =
6721 ha->tgt.saved_firmware_options_1;
6722 nv->firmware_options_2 =
6723 ha->tgt.saved_firmware_options_2;
6724 nv->firmware_options_3 =
6725 ha->tgt.saved_firmware_options_3;
6727 return;
6730 if (ha->base_qpair->enable_class_2) {
6731 if (vha->flags.init_done)
6732 fc_host_supported_classes(vha->host) =
6733 FC_COS_CLASS2 | FC_COS_CLASS3;
6735 nv->firmware_options_2 |= cpu_to_le32(BIT_8);
6736 } else {
6737 if (vha->flags.init_done)
6738 fc_host_supported_classes(vha->host) = FC_COS_CLASS3;
6740 nv->firmware_options_2 &= ~cpu_to_le32(BIT_8);
6744 void
6745 qlt_81xx_config_nvram_stage2(struct scsi_qla_host *vha,
6746 struct init_cb_81xx *icb)
6748 struct qla_hw_data *ha = vha->hw;
6750 if (!QLA_TGT_MODE_ENABLED())
6751 return;
6753 if (ha->tgt.node_name_set) {
6754 memcpy(icb->node_name, ha->tgt.tgt_node_name, WWN_SIZE);
6755 icb->firmware_options_1 |= cpu_to_le32(BIT_14);
6758 /* disable ZIO at start time. */
6759 if (!vha->flags.init_done) {
6760 uint32_t tmp;
6761 tmp = le32_to_cpu(icb->firmware_options_2);
6762 tmp &= ~(BIT_3 | BIT_2 | BIT_1 | BIT_0);
6763 icb->firmware_options_2 = cpu_to_le32(tmp);
6768 void
6769 qlt_83xx_iospace_config(struct qla_hw_data *ha)
6771 if (!QLA_TGT_MODE_ENABLED())
6772 return;
6774 ha->msix_count += 1; /* For ATIO Q */
6778 void
6779 qlt_modify_vp_config(struct scsi_qla_host *vha,
6780 struct vp_config_entry_24xx *vpmod)
6782 /* enable target mode. Bit5 = 1 => disable */
6783 if (qla_tgt_mode_enabled(vha) || qla_dual_mode_enabled(vha))
6784 vpmod->options_idx1 &= ~BIT_5;
6786 /* Disable ini mode, if requested. bit4 = 1 => disable */
6787 if (qla_tgt_mode_enabled(vha))
6788 vpmod->options_idx1 &= ~BIT_4;
6791 void
6792 qlt_probe_one_stage1(struct scsi_qla_host *base_vha, struct qla_hw_data *ha)
6794 int rc;
6796 if (!QLA_TGT_MODE_ENABLED())
6797 return;
6799 if (ha->mqenable || IS_QLA83XX(ha) || IS_QLA27XX(ha)) {
6800 ISP_ATIO_Q_IN(base_vha) = &ha->mqiobase->isp25mq.atio_q_in;
6801 ISP_ATIO_Q_OUT(base_vha) = &ha->mqiobase->isp25mq.atio_q_out;
6802 } else {
6803 ISP_ATIO_Q_IN(base_vha) = &ha->iobase->isp24.atio_q_in;
6804 ISP_ATIO_Q_OUT(base_vha) = &ha->iobase->isp24.atio_q_out;
6807 mutex_init(&base_vha->vha_tgt.tgt_mutex);
6808 mutex_init(&base_vha->vha_tgt.tgt_host_action_mutex);
6810 INIT_LIST_HEAD(&base_vha->unknown_atio_list);
6811 INIT_DELAYED_WORK(&base_vha->unknown_atio_work,
6812 qlt_unknown_atio_work_fn);
6814 qlt_clear_mode(base_vha);
6816 rc = btree_init32(&ha->tgt.host_map);
6817 if (rc)
6818 ql_log(ql_log_info, base_vha, 0xd03d,
6819 "Unable to initialize ha->host_map btree\n");
6821 qlt_update_vp_map(base_vha, SET_VP_IDX);
6824 irqreturn_t
6825 qla83xx_msix_atio_q(int irq, void *dev_id)
6827 struct rsp_que *rsp;
6828 scsi_qla_host_t *vha;
6829 struct qla_hw_data *ha;
6830 unsigned long flags;
6832 rsp = (struct rsp_que *) dev_id;
6833 ha = rsp->hw;
6834 vha = pci_get_drvdata(ha->pdev);
6836 spin_lock_irqsave(&ha->tgt.atio_lock, flags);
6838 qlt_24xx_process_atio_queue(vha, 0);
6840 spin_unlock_irqrestore(&ha->tgt.atio_lock, flags);
6842 return IRQ_HANDLED;
6845 static void
6846 qlt_handle_abts_recv_work(struct work_struct *work)
6848 struct qla_tgt_sess_op *op = container_of(work,
6849 struct qla_tgt_sess_op, work);
6850 scsi_qla_host_t *vha = op->vha;
6851 struct qla_hw_data *ha = vha->hw;
6852 unsigned long flags;
6854 if (qla2x00_reset_active(vha) ||
6855 (op->chip_reset != ha->base_qpair->chip_reset))
6856 return;
6858 spin_lock_irqsave(&ha->tgt.atio_lock, flags);
6859 qlt_24xx_process_atio_queue(vha, 0);
6860 spin_unlock_irqrestore(&ha->tgt.atio_lock, flags);
6862 spin_lock_irqsave(&ha->hardware_lock, flags);
6863 qlt_response_pkt_all_vps(vha, op->rsp, (response_t *)&op->atio);
6864 spin_unlock_irqrestore(&ha->hardware_lock, flags);
6866 kfree(op);
6869 void
6870 qlt_handle_abts_recv(struct scsi_qla_host *vha, struct rsp_que *rsp,
6871 response_t *pkt)
6873 struct qla_tgt_sess_op *op;
6875 op = kzalloc(sizeof(*op), GFP_ATOMIC);
6877 if (!op) {
6878 /* do not reach for ATIO queue here. This is best effort err
6879 * recovery at this point.
6881 qlt_response_pkt_all_vps(vha, rsp, pkt);
6882 return;
6885 memcpy(&op->atio, pkt, sizeof(*pkt));
6886 op->vha = vha;
6887 op->chip_reset = vha->hw->base_qpair->chip_reset;
6888 op->rsp = rsp;
6889 INIT_WORK(&op->work, qlt_handle_abts_recv_work);
6890 queue_work(qla_tgt_wq, &op->work);
6891 return;
6895 qlt_mem_alloc(struct qla_hw_data *ha)
6897 if (!QLA_TGT_MODE_ENABLED())
6898 return 0;
6900 ha->tgt.tgt_vp_map = kzalloc(sizeof(struct qla_tgt_vp_map) *
6901 MAX_MULTI_ID_FABRIC, GFP_KERNEL);
6902 if (!ha->tgt.tgt_vp_map)
6903 return -ENOMEM;
6905 ha->tgt.atio_ring = dma_alloc_coherent(&ha->pdev->dev,
6906 (ha->tgt.atio_q_length + 1) * sizeof(struct atio_from_isp),
6907 &ha->tgt.atio_dma, GFP_KERNEL);
6908 if (!ha->tgt.atio_ring) {
6909 kfree(ha->tgt.tgt_vp_map);
6910 return -ENOMEM;
6912 return 0;
6915 void
6916 qlt_mem_free(struct qla_hw_data *ha)
6918 if (!QLA_TGT_MODE_ENABLED())
6919 return;
6921 if (ha->tgt.atio_ring) {
6922 dma_free_coherent(&ha->pdev->dev, (ha->tgt.atio_q_length + 1) *
6923 sizeof(struct atio_from_isp), ha->tgt.atio_ring,
6924 ha->tgt.atio_dma);
6926 kfree(ha->tgt.tgt_vp_map);
6929 /* vport_slock to be held by the caller */
6930 void
6931 qlt_update_vp_map(struct scsi_qla_host *vha, int cmd)
6933 void *slot;
6934 u32 key;
6935 int rc;
6937 if (!QLA_TGT_MODE_ENABLED())
6938 return;
6940 key = vha->d_id.b24;
6942 switch (cmd) {
6943 case SET_VP_IDX:
6944 vha->hw->tgt.tgt_vp_map[vha->vp_idx].vha = vha;
6945 break;
6946 case SET_AL_PA:
6947 slot = btree_lookup32(&vha->hw->tgt.host_map, key);
6948 if (!slot) {
6949 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf018,
6950 "Save vha in host_map %p %06x\n", vha, key);
6951 rc = btree_insert32(&vha->hw->tgt.host_map,
6952 key, vha, GFP_ATOMIC);
6953 if (rc)
6954 ql_log(ql_log_info, vha, 0xd03e,
6955 "Unable to insert s_id into host_map: %06x\n",
6956 key);
6957 return;
6959 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf019,
6960 "replace existing vha in host_map %p %06x\n", vha, key);
6961 btree_update32(&vha->hw->tgt.host_map, key, vha);
6962 break;
6963 case RESET_VP_IDX:
6964 vha->hw->tgt.tgt_vp_map[vha->vp_idx].vha = NULL;
6965 break;
6966 case RESET_AL_PA:
6967 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01a,
6968 "clear vha in host_map %p %06x\n", vha, key);
6969 slot = btree_lookup32(&vha->hw->tgt.host_map, key);
6970 if (slot)
6971 btree_remove32(&vha->hw->tgt.host_map, key);
6972 vha->d_id.b24 = 0;
6973 break;
6977 void qlt_update_host_map(struct scsi_qla_host *vha, port_id_t id)
6979 unsigned long flags;
6980 struct qla_hw_data *ha = vha->hw;
6982 if (!vha->d_id.b24) {
6983 spin_lock_irqsave(&ha->vport_slock, flags);
6984 vha->d_id = id;
6985 qlt_update_vp_map(vha, SET_AL_PA);
6986 spin_unlock_irqrestore(&ha->vport_slock, flags);
6987 } else if (vha->d_id.b24 != id.b24) {
6988 spin_lock_irqsave(&ha->vport_slock, flags);
6989 qlt_update_vp_map(vha, RESET_AL_PA);
6990 vha->d_id = id;
6991 qlt_update_vp_map(vha, SET_AL_PA);
6992 spin_unlock_irqrestore(&ha->vport_slock, flags);
6996 static int __init qlt_parse_ini_mode(void)
6998 if (strcasecmp(qlini_mode, QLA2XXX_INI_MODE_STR_EXCLUSIVE) == 0)
6999 ql2x_ini_mode = QLA2XXX_INI_MODE_EXCLUSIVE;
7000 else if (strcasecmp(qlini_mode, QLA2XXX_INI_MODE_STR_DISABLED) == 0)
7001 ql2x_ini_mode = QLA2XXX_INI_MODE_DISABLED;
7002 else if (strcasecmp(qlini_mode, QLA2XXX_INI_MODE_STR_ENABLED) == 0)
7003 ql2x_ini_mode = QLA2XXX_INI_MODE_ENABLED;
7004 else if (strcasecmp(qlini_mode, QLA2XXX_INI_MODE_STR_DUAL) == 0)
7005 ql2x_ini_mode = QLA2XXX_INI_MODE_DUAL;
7006 else
7007 return false;
7009 return true;
7012 int __init qlt_init(void)
7014 int ret;
7016 if (!qlt_parse_ini_mode()) {
7017 ql_log(ql_log_fatal, NULL, 0xe06b,
7018 "qlt_parse_ini_mode() failed\n");
7019 return -EINVAL;
7022 if (!QLA_TGT_MODE_ENABLED())
7023 return 0;
7025 qla_tgt_mgmt_cmd_cachep = kmem_cache_create("qla_tgt_mgmt_cmd_cachep",
7026 sizeof(struct qla_tgt_mgmt_cmd), __alignof__(struct
7027 qla_tgt_mgmt_cmd), 0, NULL);
7028 if (!qla_tgt_mgmt_cmd_cachep) {
7029 ql_log(ql_log_fatal, NULL, 0xd04b,
7030 "kmem_cache_create for qla_tgt_mgmt_cmd_cachep failed\n");
7031 return -ENOMEM;
7034 qla_tgt_plogi_cachep = kmem_cache_create("qla_tgt_plogi_cachep",
7035 sizeof(struct qlt_plogi_ack_t), __alignof__(struct qlt_plogi_ack_t),
7036 0, NULL);
7038 if (!qla_tgt_plogi_cachep) {
7039 ql_log(ql_log_fatal, NULL, 0xe06d,
7040 "kmem_cache_create for qla_tgt_plogi_cachep failed\n");
7041 ret = -ENOMEM;
7042 goto out_mgmt_cmd_cachep;
7045 qla_tgt_mgmt_cmd_mempool = mempool_create(25, mempool_alloc_slab,
7046 mempool_free_slab, qla_tgt_mgmt_cmd_cachep);
7047 if (!qla_tgt_mgmt_cmd_mempool) {
7048 ql_log(ql_log_fatal, NULL, 0xe06e,
7049 "mempool_create for qla_tgt_mgmt_cmd_mempool failed\n");
7050 ret = -ENOMEM;
7051 goto out_plogi_cachep;
7054 qla_tgt_wq = alloc_workqueue("qla_tgt_wq", 0, 0);
7055 if (!qla_tgt_wq) {
7056 ql_log(ql_log_fatal, NULL, 0xe06f,
7057 "alloc_workqueue for qla_tgt_wq failed\n");
7058 ret = -ENOMEM;
7059 goto out_cmd_mempool;
7062 * Return 1 to signal that initiator-mode is being disabled
7064 return (ql2x_ini_mode == QLA2XXX_INI_MODE_DISABLED) ? 1 : 0;
7066 out_cmd_mempool:
7067 mempool_destroy(qla_tgt_mgmt_cmd_mempool);
7068 out_plogi_cachep:
7069 kmem_cache_destroy(qla_tgt_plogi_cachep);
7070 out_mgmt_cmd_cachep:
7071 kmem_cache_destroy(qla_tgt_mgmt_cmd_cachep);
7072 return ret;
7075 void qlt_exit(void)
7077 if (!QLA_TGT_MODE_ENABLED())
7078 return;
7080 destroy_workqueue(qla_tgt_wq);
7081 mempool_destroy(qla_tgt_mgmt_cmd_mempool);
7082 kmem_cache_destroy(qla_tgt_plogi_cachep);
7083 kmem_cache_destroy(qla_tgt_mgmt_cmd_cachep);