xref: /linux/drivers/scsi/qla2xxx/qla_target.c (revision f66bc387efbee59978e076ce9bf123ac353b389c)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  *  qla_target.c SCSI LLD infrastructure for QLogic 22xx/23xx/24xx/25xx
4  *
5  *  based on qla2x00t.c code:
6  *
7  *  Copyright (C) 2004 - 2010 Vladislav Bolkhovitin <vst@vlnb.net>
8  *  Copyright (C) 2004 - 2005 Leonid Stoljar
9  *  Copyright (C) 2006 Nathaniel Clark <nate@misrule.us>
10  *  Copyright (C) 2006 - 2010 ID7 Ltd.
11  *
12  *  Forward port and refactoring to modern qla2xxx and target/configfs
13  *
14  *  Copyright (C) 2010-2013 Nicholas A. Bellinger <nab@kernel.org>
15  */
16 
17 #include <linux/module.h>
18 #include <linux/init.h>
19 #include <linux/types.h>
20 #include <linux/blkdev.h>
21 #include <linux/interrupt.h>
22 #include <linux/pci.h>
23 #include <linux/delay.h>
24 #include <linux/list.h>
25 #include <linux/workqueue.h>
26 #include <linux/unaligned.h>
27 #include <scsi/scsi.h>
28 #include <scsi/scsi_host.h>
29 #include <scsi/scsi_tcq.h>
30 
31 #include "qla_def.h"
32 #include "qla_target.h"
33 
34 static int ql2xtgt_tape_enable;
35 module_param(ql2xtgt_tape_enable, int, S_IRUGO|S_IWUSR);
36 MODULE_PARM_DESC(ql2xtgt_tape_enable,
37 		"Enables Sequence level error recovery (aka FC Tape). Default is 0 - no SLER. 1 - Enable SLER.");
38 
39 static char *qlini_mode = QLA2XXX_INI_MODE_STR_ENABLED;
40 module_param(qlini_mode, charp, S_IRUGO);
41 MODULE_PARM_DESC(qlini_mode,
42 	"Determines when initiator mode will be enabled. Possible values: "
43 	"\"exclusive\" - initiator mode will be enabled on load, "
44 	"disabled on enabling target mode and then on disabling target mode "
45 	"enabled back; "
46 	"\"disabled\" - initiator mode will never be enabled; "
47 	"\"dual\" - Initiator Modes will be enabled. Target Mode can be activated "
48 	"when ready "
49 	"\"enabled\" (default) - initiator mode will always stay enabled.");
50 
51 int ql2xuctrlirq = 1;
52 module_param(ql2xuctrlirq, int, 0644);
53 MODULE_PARM_DESC(ql2xuctrlirq,
54     "User to control IRQ placement via smp_affinity."
55     "Valid with qlini_mode=disabled."
56     "1(default): enable");
57 
58 int ql2x_ini_mode = QLA2XXX_INI_MODE_EXCLUSIVE;
59 
60 static int qla_sam_status = SAM_STAT_BUSY;
61 static int tc_sam_status = SAM_STAT_TASK_SET_FULL; /* target core */
62 
63 /*
64  * From scsi/fc/fc_fcp.h
65  */
66 enum fcp_resp_rsp_codes {
67 	FCP_TMF_CMPL = 0,
68 	FCP_DATA_LEN_INVALID = 1,
69 	FCP_CMND_FIELDS_INVALID = 2,
70 	FCP_DATA_PARAM_MISMATCH = 3,
71 	FCP_TMF_REJECTED = 4,
72 	FCP_TMF_FAILED = 5,
73 	FCP_TMF_INVALID_LUN = 9,
74 };
75 
76 /*
77  * fc_pri_ta from scsi/fc/fc_fcp.h
78  */
79 #define FCP_PTA_SIMPLE      0   /* simple task attribute */
80 #define FCP_PTA_HEADQ       1   /* head of queue task attribute */
81 #define FCP_PTA_ORDERED     2   /* ordered task attribute */
82 #define FCP_PTA_ACA         4   /* auto. contingent allegiance */
83 #define FCP_PTA_MASK        7   /* mask for task attribute field */
84 #define FCP_PRI_SHIFT       3   /* priority field starts in bit 3 */
85 #define FCP_PRI_RESVD_MASK  0x80        /* reserved bits in priority field */
86 
87 /*
88  * This driver calls qla2x00_alloc_iocbs() and qla2x00_issue_marker(), which
89  * must be called under HW lock and could unlock/lock it inside.
90  * It isn't an issue, since in the current implementation on the time when
91  * those functions are called:
92  *
93  *   - Either context is IRQ and only IRQ handler can modify HW data,
94  *     including rings related fields,
95  *
96  *   - Or access to target mode variables from struct qla_tgt doesn't
97  *     cross those functions boundaries, except tgt_stop, which
98  *     additionally protected by irq_cmd_count.
99  */
100 /* Predefs for callbacks handed to qla2xxx LLD */
101 static void qlt_24xx_atio_pkt(struct scsi_qla_host *ha,
102 	struct atio_from_isp *pkt, uint8_t);
103 static void qlt_response_pkt(struct scsi_qla_host *ha, struct rsp_que *rsp,
104 	response_t *pkt);
105 static int qlt_issue_task_mgmt(struct fc_port *sess, u64 lun,
106 	int fn, void *iocb, int flags);
107 static void qlt_send_term_exchange(struct qla_qpair *, struct qla_tgt_cmd
108 	*cmd, struct atio_from_isp *atio, int ha_locked, int ul_abort);
109 static void qlt_alloc_qfull_cmd(struct scsi_qla_host *vha,
110 	struct atio_from_isp *atio, uint16_t status, int qfull);
111 static void qlt_disable_vha(struct scsi_qla_host *vha);
112 static void qlt_clear_tgt_db(struct qla_tgt *tgt);
113 static void qlt_send_notify_ack(struct qla_qpair *qpair,
114 	struct imm_ntfy_from_isp *ntfy,
115 	uint32_t add_flags, uint16_t resp_code, int resp_code_valid,
116 	uint16_t srr_flags, uint16_t srr_reject_code, uint8_t srr_explan);
117 static void qlt_send_term_imm_notif(struct scsi_qla_host *vha,
118 	struct imm_ntfy_from_isp *imm, int ha_locked);
119 static struct fc_port *qlt_create_sess(struct scsi_qla_host *vha,
120 	fc_port_t *fcport, bool local);
121 void qlt_unreg_sess(struct fc_port *sess);
122 static void qlt_24xx_handle_abts(struct scsi_qla_host *,
123 	struct abts_recv_from_24xx *);
124 static void qlt_send_busy(struct qla_qpair *, struct atio_from_isp *,
125     uint16_t);
126 static int qlt_check_reserve_free_req(struct qla_qpair *qpair, uint32_t);
127 static inline uint32_t qlt_make_handle(struct qla_qpair *);
128 
129 /*
130  * Global Variables
131  */
132 static struct kmem_cache *qla_tgt_mgmt_cmd_cachep;
133 struct kmem_cache *qla_tgt_plogi_cachep;
134 static mempool_t *qla_tgt_mgmt_cmd_mempool;
135 static struct workqueue_struct *qla_tgt_wq;
136 static DEFINE_MUTEX(qla_tgt_mutex);
137 static LIST_HEAD(qla_tgt_glist);
138 
prot_op_str(u32 prot_op)139 static const char *prot_op_str(u32 prot_op)
140 {
141 	switch (prot_op) {
142 	case TARGET_PROT_NORMAL:	return "NORMAL";
143 	case TARGET_PROT_DIN_INSERT:	return "DIN_INSERT";
144 	case TARGET_PROT_DOUT_INSERT:	return "DOUT_INSERT";
145 	case TARGET_PROT_DIN_STRIP:	return "DIN_STRIP";
146 	case TARGET_PROT_DOUT_STRIP:	return "DOUT_STRIP";
147 	case TARGET_PROT_DIN_PASS:	return "DIN_PASS";
148 	case TARGET_PROT_DOUT_PASS:	return "DOUT_PASS";
149 	default:			return "UNKNOWN";
150 	}
151 }
152 
153 /* This API intentionally takes dest as a parameter, rather than returning
154  * int value to avoid caller forgetting to issue wmb() after the store */
qlt_do_generation_tick(struct scsi_qla_host * vha,int * dest)155 void qlt_do_generation_tick(struct scsi_qla_host *vha, int *dest)
156 {
157 	scsi_qla_host_t *base_vha = pci_get_drvdata(vha->hw->pdev);
158 	*dest = atomic_inc_return(&base_vha->generation_tick);
159 	/* memory barrier */
160 	wmb();
161 }
162 
163 /* Might release hw lock, then reaquire!! */
qlt_issue_marker(struct scsi_qla_host * vha,int vha_locked)164 static inline int qlt_issue_marker(struct scsi_qla_host *vha, int vha_locked)
165 {
166 	/* Send marker if required */
167 	if (unlikely(vha->marker_needed != 0)) {
168 		int rc = qla2x00_issue_marker(vha, vha_locked);
169 
170 		if (rc != QLA_SUCCESS) {
171 			ql_dbg(ql_dbg_tgt, vha, 0xe03d,
172 			    "qla_target(%d): issue_marker() failed\n",
173 			    vha->vp_idx);
174 		}
175 		return rc;
176 	}
177 	return QLA_SUCCESS;
178 }
179 
qla_find_host_by_d_id(struct scsi_qla_host * vha,be_id_t d_id)180 struct scsi_qla_host *qla_find_host_by_d_id(struct scsi_qla_host *vha,
181 					    be_id_t d_id)
182 {
183 	struct scsi_qla_host *host;
184 	uint32_t key;
185 
186 	if (vha->d_id.b.area == d_id.area &&
187 	    vha->d_id.b.domain == d_id.domain &&
188 	    vha->d_id.b.al_pa == d_id.al_pa)
189 		return vha;
190 
191 	key = be_to_port_id(d_id).b24;
192 
193 	host = btree_lookup32(&vha->hw->host_map, key);
194 	if (!host)
195 		ql_dbg(ql_dbg_tgt_mgt + ql_dbg_verbose, vha, 0xf005,
196 		    "Unable to find host %06x\n", key);
197 
198 	return host;
199 }
200 
qlt_incr_num_pend_cmds(struct scsi_qla_host * vha)201 static inline void qlt_incr_num_pend_cmds(struct scsi_qla_host *vha)
202 {
203 	unsigned long flags;
204 
205 	spin_lock_irqsave(&vha->hw->tgt.q_full_lock, flags);
206 
207 	vha->hw->tgt.num_pend_cmds++;
208 	if (vha->hw->tgt.num_pend_cmds > vha->qla_stats.stat_max_pend_cmds)
209 		vha->qla_stats.stat_max_pend_cmds =
210 			vha->hw->tgt.num_pend_cmds;
211 	spin_unlock_irqrestore(&vha->hw->tgt.q_full_lock, flags);
212 }
qlt_decr_num_pend_cmds(struct scsi_qla_host * vha)213 static inline void qlt_decr_num_pend_cmds(struct scsi_qla_host *vha)
214 {
215 	unsigned long flags;
216 
217 	spin_lock_irqsave(&vha->hw->tgt.q_full_lock, flags);
218 	vha->hw->tgt.num_pend_cmds--;
219 	spin_unlock_irqrestore(&vha->hw->tgt.q_full_lock, flags);
220 }
221 
222 
qlt_queue_unknown_atio(scsi_qla_host_t * vha,struct atio_from_isp * atio,uint8_t ha_locked)223 static void qlt_queue_unknown_atio(scsi_qla_host_t *vha,
224 	struct atio_from_isp *atio, uint8_t ha_locked)
225 {
226 	struct qla_tgt_sess_op *u;
227 	struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
228 	unsigned long flags;
229 
230 	if (tgt->tgt_stop) {
231 		ql_dbg(ql_dbg_async, vha, 0x502c,
232 		    "qla_target(%d): dropping unknown ATIO_TYPE7, because tgt is being stopped",
233 		    vha->vp_idx);
234 		goto out_term;
235 	}
236 
237 	u = kzalloc(sizeof(*u), GFP_ATOMIC);
238 	if (u == NULL)
239 		goto out_term;
240 
241 	u->vha = vha;
242 	memcpy(&u->atio, atio, sizeof(*atio));
243 	INIT_LIST_HEAD(&u->cmd_list);
244 
245 	spin_lock_irqsave(&vha->cmd_list_lock, flags);
246 	list_add_tail(&u->cmd_list, &vha->unknown_atio_list);
247 	spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
248 
249 	schedule_delayed_work(&vha->unknown_atio_work, 1);
250 
251 out:
252 	return;
253 
254 out_term:
255 	qlt_send_term_exchange(vha->hw->base_qpair, NULL, atio, ha_locked, 0);
256 	goto out;
257 }
258 
qlt_try_to_dequeue_unknown_atios(struct scsi_qla_host * vha,uint8_t ha_locked)259 static void qlt_try_to_dequeue_unknown_atios(struct scsi_qla_host *vha,
260 	uint8_t ha_locked)
261 {
262 	struct qla_tgt_sess_op *u, *t;
263 	scsi_qla_host_t *host;
264 	struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
265 	unsigned long flags;
266 	uint8_t queued = 0;
267 
268 	list_for_each_entry_safe(u, t, &vha->unknown_atio_list, cmd_list) {
269 		if (u->aborted) {
270 			ql_dbg(ql_dbg_async, vha, 0x502e,
271 			    "Freeing unknown %s %p, because of Abort\n",
272 			    "ATIO_TYPE7", u);
273 			qlt_send_term_exchange(vha->hw->base_qpair, NULL,
274 			    &u->atio, ha_locked, 0);
275 			goto abort;
276 		}
277 
278 		host = qla_find_host_by_d_id(vha, u->atio.u.isp24.fcp_hdr.d_id);
279 		if (host != NULL) {
280 			ql_dbg(ql_dbg_async + ql_dbg_verbose, vha, 0x502f,
281 			    "Requeuing unknown ATIO_TYPE7 %p\n", u);
282 			qlt_24xx_atio_pkt(host, &u->atio, ha_locked);
283 		} else if (tgt->tgt_stop) {
284 			ql_dbg(ql_dbg_async + ql_dbg_verbose, vha, 0x503a,
285 			    "Freeing unknown %s %p, because tgt is being stopped\n",
286 			    "ATIO_TYPE7", u);
287 			qlt_send_term_exchange(vha->hw->base_qpair, NULL,
288 			    &u->atio, ha_locked, 0);
289 		} else {
290 			ql_dbg(ql_dbg_async + ql_dbg_verbose, vha, 0x503d,
291 			    "Reschedule u %p, vha %p, host %p\n", u, vha, host);
292 			if (!queued) {
293 				queued = 1;
294 				schedule_delayed_work(&vha->unknown_atio_work,
295 				    1);
296 			}
297 			continue;
298 		}
299 
300 abort:
301 		spin_lock_irqsave(&vha->cmd_list_lock, flags);
302 		list_del(&u->cmd_list);
303 		spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
304 		kfree(u);
305 	}
306 }
307 
qlt_unknown_atio_work_fn(struct work_struct * work)308 void qlt_unknown_atio_work_fn(struct work_struct *work)
309 {
310 	struct scsi_qla_host *vha = container_of(to_delayed_work(work),
311 	    struct scsi_qla_host, unknown_atio_work);
312 
313 	qlt_try_to_dequeue_unknown_atios(vha, 0);
314 }
315 
qlt_24xx_atio_pkt_all_vps(struct scsi_qla_host * vha,struct atio_from_isp * atio,uint8_t ha_locked)316 static bool qlt_24xx_atio_pkt_all_vps(struct scsi_qla_host *vha,
317 	struct atio_from_isp *atio, uint8_t ha_locked)
318 {
319 	ql_dbg(ql_dbg_tgt, vha, 0xe072,
320 		"%s: qla_target(%d): type %x ox_id %04x\n",
321 		__func__, vha->vp_idx, atio->u.raw.entry_type,
322 		be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id));
323 
324 	switch (atio->u.raw.entry_type) {
325 	case ATIO_TYPE7:
326 	{
327 		struct scsi_qla_host *host = qla_find_host_by_d_id(vha,
328 		    atio->u.isp24.fcp_hdr.d_id);
329 		if (unlikely(NULL == host)) {
330 			ql_dbg(ql_dbg_tgt, vha, 0xe03e,
331 			    "qla_target(%d): Received ATIO_TYPE7 "
332 			    "with unknown d_id %x:%x:%x\n", vha->vp_idx,
333 			    atio->u.isp24.fcp_hdr.d_id.domain,
334 			    atio->u.isp24.fcp_hdr.d_id.area,
335 			    atio->u.isp24.fcp_hdr.d_id.al_pa);
336 
337 
338 			qlt_queue_unknown_atio(vha, atio, ha_locked);
339 			break;
340 		}
341 		if (unlikely(!list_empty(&vha->unknown_atio_list)))
342 			qlt_try_to_dequeue_unknown_atios(vha, ha_locked);
343 
344 		qlt_24xx_atio_pkt(host, atio, ha_locked);
345 		break;
346 	}
347 
348 	case IMMED_NOTIFY_TYPE:
349 	{
350 		struct scsi_qla_host *host = vha;
351 		struct imm_ntfy_from_isp *entry =
352 		    (struct imm_ntfy_from_isp *)atio;
353 
354 		qlt_issue_marker(vha, ha_locked);
355 
356 		if ((entry->u.isp24.vp_index != 0xFF) &&
357 		    (entry->u.isp24.nport_handle != cpu_to_le16(0xFFFF))) {
358 			host = qla_find_host_by_vp_idx(vha,
359 			    entry->u.isp24.vp_index);
360 			if (unlikely(!host)) {
361 				ql_dbg(ql_dbg_tgt, vha, 0xe03f,
362 				    "qla_target(%d): Received "
363 				    "ATIO (IMMED_NOTIFY_TYPE) "
364 				    "with unknown vp_index %d\n",
365 				    vha->vp_idx, entry->u.isp24.vp_index);
366 				break;
367 			}
368 		}
369 		qlt_24xx_atio_pkt(host, atio, ha_locked);
370 		break;
371 	}
372 
373 	case VP_RPT_ID_IOCB_TYPE:
374 		qla24xx_report_id_acquisition(vha,
375 			(struct vp_rpt_id_entry_24xx *)atio);
376 		break;
377 
378 	case ABTS_RECV_24XX:
379 	{
380 		struct abts_recv_from_24xx *entry =
381 			(struct abts_recv_from_24xx *)atio;
382 		struct scsi_qla_host *host = qla_find_host_by_vp_idx(vha,
383 			entry->vp_index);
384 		unsigned long flags;
385 
386 		if (unlikely(!host)) {
387 			ql_dbg(ql_dbg_tgt, vha, 0xe00a,
388 			    "qla_target(%d): Response pkt (ABTS_RECV_24XX) "
389 			    "received, with unknown vp_index %d\n",
390 			    vha->vp_idx, entry->vp_index);
391 			break;
392 		}
393 		if (!ha_locked)
394 			spin_lock_irqsave(&host->hw->hardware_lock, flags);
395 		qlt_24xx_handle_abts(host, (struct abts_recv_from_24xx *)atio);
396 		if (!ha_locked)
397 			spin_unlock_irqrestore(&host->hw->hardware_lock, flags);
398 		break;
399 	}
400 
401 	/* case PUREX_IOCB_TYPE: ql2xmvasynctoatio */
402 
403 	default:
404 		ql_dbg(ql_dbg_tgt, vha, 0xe040,
405 		    "qla_target(%d): Received unknown ATIO atio "
406 		    "type %x\n", vha->vp_idx, atio->u.raw.entry_type);
407 		break;
408 	}
409 
410 	return false;
411 }
412 
qlt_response_pkt_all_vps(struct scsi_qla_host * vha,struct rsp_que * rsp,response_t * pkt)413 void qlt_response_pkt_all_vps(struct scsi_qla_host *vha,
414 	struct rsp_que *rsp, response_t *pkt)
415 {
416 	switch (pkt->entry_type) {
417 	case CTIO_CRC2:
418 		ql_dbg(ql_dbg_tgt, vha, 0xe073,
419 			"qla_target(%d):%s: CRC2 Response pkt\n",
420 			vha->vp_idx, __func__);
421 		fallthrough;
422 	case CTIO_TYPE7:
423 	{
424 		struct ctio7_from_24xx *entry = (struct ctio7_from_24xx *)pkt;
425 		struct scsi_qla_host *host = qla_find_host_by_vp_idx(vha,
426 		    entry->vp_index);
427 		if (unlikely(!host)) {
428 			ql_dbg(ql_dbg_tgt, vha, 0xe041,
429 			    "qla_target(%d): Response pkt (CTIO_TYPE7) "
430 			    "received, with unknown vp_index %d\n",
431 			    vha->vp_idx, entry->vp_index);
432 			break;
433 		}
434 		qlt_response_pkt(host, rsp, pkt);
435 		break;
436 	}
437 
438 	case IMMED_NOTIFY_TYPE:
439 	{
440 		struct scsi_qla_host *host;
441 		struct imm_ntfy_from_isp *entry =
442 		    (struct imm_ntfy_from_isp *)pkt;
443 
444 		host = qla_find_host_by_vp_idx(vha, entry->u.isp24.vp_index);
445 		if (unlikely(!host)) {
446 			ql_dbg(ql_dbg_tgt, vha, 0xe042,
447 			    "qla_target(%d): Response pkt (IMMED_NOTIFY_TYPE) "
448 			    "received, with unknown vp_index %d\n",
449 			    vha->vp_idx, entry->u.isp24.vp_index);
450 			break;
451 		}
452 		qlt_response_pkt(host, rsp, pkt);
453 		break;
454 	}
455 
456 	case NOTIFY_ACK_TYPE:
457 	{
458 		struct scsi_qla_host *host = vha;
459 		struct nack_to_isp *entry = (struct nack_to_isp *)pkt;
460 
461 		if (0xFF != entry->u.isp24.vp_index) {
462 			host = qla_find_host_by_vp_idx(vha,
463 			    entry->u.isp24.vp_index);
464 			if (unlikely(!host)) {
465 				ql_dbg(ql_dbg_tgt, vha, 0xe043,
466 				    "qla_target(%d): Response "
467 				    "pkt (NOTIFY_ACK_TYPE) "
468 				    "received, with unknown "
469 				    "vp_index %d\n", vha->vp_idx,
470 				    entry->u.isp24.vp_index);
471 				break;
472 			}
473 		}
474 		qlt_response_pkt(host, rsp, pkt);
475 		break;
476 	}
477 
478 	case ABTS_RECV_24XX:
479 	{
480 		struct abts_recv_from_24xx *entry =
481 		    (struct abts_recv_from_24xx *)pkt;
482 		struct scsi_qla_host *host = qla_find_host_by_vp_idx(vha,
483 		    entry->vp_index);
484 		if (unlikely(!host)) {
485 			ql_dbg(ql_dbg_tgt, vha, 0xe044,
486 			    "qla_target(%d): Response pkt "
487 			    "(ABTS_RECV_24XX) received, with unknown "
488 			    "vp_index %d\n", vha->vp_idx, entry->vp_index);
489 			break;
490 		}
491 		qlt_response_pkt(host, rsp, pkt);
492 		break;
493 	}
494 
495 	case ABTS_RESP_24XX:
496 	{
497 		struct abts_resp_to_24xx *entry =
498 		    (struct abts_resp_to_24xx *)pkt;
499 		struct scsi_qla_host *host = qla_find_host_by_vp_idx(vha,
500 		    entry->vp_index);
501 		if (unlikely(!host)) {
502 			ql_dbg(ql_dbg_tgt, vha, 0xe045,
503 			    "qla_target(%d): Response pkt "
504 			    "(ABTS_RECV_24XX) received, with unknown "
505 			    "vp_index %d\n", vha->vp_idx, entry->vp_index);
506 			break;
507 		}
508 		qlt_response_pkt(host, rsp, pkt);
509 		break;
510 	}
511 	default:
512 		qlt_response_pkt(vha, rsp, pkt);
513 		break;
514 	}
515 
516 }
517 
518 /*
519  * All qlt_plogi_ack_t operations are protected by hardware_lock
520  */
qla24xx_post_nack_work(struct scsi_qla_host * vha,fc_port_t * fcport,struct imm_ntfy_from_isp * ntfy,int type)521 static int qla24xx_post_nack_work(struct scsi_qla_host *vha, fc_port_t *fcport,
522 	struct imm_ntfy_from_isp *ntfy, int type)
523 {
524 	struct qla_work_evt *e;
525 
526 	e = qla2x00_alloc_work(vha, QLA_EVT_NACK);
527 	if (!e)
528 		return QLA_FUNCTION_FAILED;
529 
530 	e->u.nack.fcport = fcport;
531 	e->u.nack.type = type;
532 	memcpy(e->u.nack.iocb, ntfy, sizeof(struct imm_ntfy_from_isp));
533 	return qla2x00_post_work(vha, e);
534 }
535 
qla2x00_async_nack_sp_done(srb_t * sp,int res)536 static void qla2x00_async_nack_sp_done(srb_t *sp, int res)
537 {
538 	struct scsi_qla_host *vha = sp->vha;
539 	unsigned long flags;
540 
541 	ql_dbg(ql_dbg_disc, vha, 0x20f2,
542 	    "Async done-%s res %x %8phC  type %d\n",
543 	    sp->name, res, sp->fcport->port_name, sp->type);
544 
545 	spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
546 	sp->fcport->flags &= ~FCF_ASYNC_SENT;
547 	sp->fcport->chip_reset = vha->hw->base_qpair->chip_reset;
548 
549 	switch (sp->type) {
550 	case SRB_NACK_PLOGI:
551 		sp->fcport->login_gen++;
552 		sp->fcport->fw_login_state = DSC_LS_PLOGI_COMP;
553 		sp->fcport->logout_on_delete = 1;
554 		sp->fcport->plogi_nack_done_deadline = jiffies + HZ;
555 		sp->fcport->send_els_logo = 0;
556 
557 		if (sp->fcport->flags & FCF_FCSP_DEVICE) {
558 			ql_dbg(ql_dbg_edif, vha, 0x20ef,
559 			    "%s %8phC edif: PLOGI- AUTH WAIT\n", __func__,
560 			    sp->fcport->port_name);
561 			qla2x00_set_fcport_disc_state(sp->fcport,
562 			    DSC_LOGIN_AUTH_PEND);
563 			qla2x00_post_aen_work(vha, FCH_EVT_PORT_ONLINE,
564 			    sp->fcport->d_id.b24);
565 			qla_edb_eventcreate(vha, VND_CMD_AUTH_STATE_NEEDED, sp->fcport->d_id.b24,
566 			    0, sp->fcport);
567 		}
568 		break;
569 
570 	case SRB_NACK_PRLI:
571 		sp->fcport->fw_login_state = DSC_LS_PRLI_COMP;
572 		sp->fcport->deleted = 0;
573 		sp->fcport->send_els_logo = 0;
574 
575 		if (!sp->fcport->login_succ &&
576 		    !IS_SW_RESV_ADDR(sp->fcport->d_id)) {
577 			sp->fcport->login_succ = 1;
578 
579 			vha->fcport_count++;
580 			spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
581 			qla24xx_sched_upd_fcport(sp->fcport);
582 			spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
583 		} else {
584 			sp->fcport->login_retry = 0;
585 			qla2x00_set_fcport_disc_state(sp->fcport,
586 			    DSC_LOGIN_COMPLETE);
587 			sp->fcport->deleted = 0;
588 			sp->fcport->logout_on_delete = 1;
589 		}
590 		break;
591 
592 	case SRB_NACK_LOGO:
593 		sp->fcport->login_gen++;
594 		sp->fcport->fw_login_state = DSC_LS_PORT_UNAVAIL;
595 		qlt_logo_completion_handler(sp->fcport, MBS_COMMAND_COMPLETE);
596 		break;
597 	}
598 	spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
599 
600 	kref_put(&sp->cmd_kref, qla2x00_sp_release);
601 }
602 
qla24xx_async_notify_ack(scsi_qla_host_t * vha,fc_port_t * fcport,struct imm_ntfy_from_isp * ntfy,int type)603 int qla24xx_async_notify_ack(scsi_qla_host_t *vha, fc_port_t *fcport,
604 	struct imm_ntfy_from_isp *ntfy, int type)
605 {
606 	int rval = QLA_FUNCTION_FAILED;
607 	srb_t *sp;
608 	char *c = NULL;
609 
610 	fcport->flags |= FCF_ASYNC_SENT;
611 	switch (type) {
612 	case SRB_NACK_PLOGI:
613 		fcport->fw_login_state = DSC_LS_PLOGI_PEND;
614 		c = "PLOGI";
615 		if (vha->hw->flags.edif_enabled &&
616 		    (le16_to_cpu(ntfy->u.isp24.flags) & NOTIFY24XX_FLAGS_FCSP))
617 			fcport->flags |= FCF_FCSP_DEVICE;
618 		break;
619 	case SRB_NACK_PRLI:
620 		fcport->fw_login_state = DSC_LS_PRLI_PEND;
621 		fcport->deleted = 0;
622 		c = "PRLI";
623 		break;
624 	case SRB_NACK_LOGO:
625 		fcport->fw_login_state = DSC_LS_LOGO_PEND;
626 		c = "LOGO";
627 		break;
628 	}
629 
630 	sp = qla2x00_get_sp(vha, fcport, GFP_ATOMIC);
631 	if (!sp)
632 		goto done;
633 
634 	sp->type = type;
635 	sp->name = "nack";
636 	qla2x00_init_async_sp(sp, qla2x00_get_async_timeout(vha) + 2,
637 			      qla2x00_async_nack_sp_done);
638 
639 	sp->u.iocb_cmd.u.nack.ntfy = ntfy;
640 
641 	ql_dbg(ql_dbg_disc, vha, 0x20f4,
642 	    "Async-%s %8phC hndl %x %s\n",
643 	    sp->name, fcport->port_name, sp->handle, c);
644 
645 	rval = qla2x00_start_sp(sp);
646 	if (rval != QLA_SUCCESS)
647 		goto done_free_sp;
648 
649 	return rval;
650 
651 done_free_sp:
652 	kref_put(&sp->cmd_kref, qla2x00_sp_release);
653 done:
654 	fcport->flags &= ~FCF_ASYNC_SENT;
655 	return rval;
656 }
657 
qla24xx_do_nack_work(struct scsi_qla_host * vha,struct qla_work_evt * e)658 void qla24xx_do_nack_work(struct scsi_qla_host *vha, struct qla_work_evt *e)
659 {
660 	fc_port_t *t;
661 
662 	switch (e->u.nack.type) {
663 	case SRB_NACK_PRLI:
664 		t = e->u.nack.fcport;
665 		flush_work(&t->del_work);
666 		flush_work(&t->free_work);
667 		mutex_lock(&vha->vha_tgt.tgt_mutex);
668 		t = qlt_create_sess(vha, e->u.nack.fcport, 0);
669 		mutex_unlock(&vha->vha_tgt.tgt_mutex);
670 		if (t) {
671 			ql_log(ql_log_info, vha, 0xd034,
672 			    "%s create sess success %p", __func__, t);
673 			/* create sess has an extra kref */
674 			vha->hw->tgt.tgt_ops->put_sess(e->u.nack.fcport);
675 		}
676 		break;
677 	}
678 	qla24xx_async_notify_ack(vha, e->u.nack.fcport,
679 	    (struct imm_ntfy_from_isp *)e->u.nack.iocb, e->u.nack.type);
680 }
681 
qla24xx_delete_sess_fn(struct work_struct * work)682 void qla24xx_delete_sess_fn(struct work_struct *work)
683 {
684 	fc_port_t *fcport = container_of(work, struct fc_port, del_work);
685 	struct qla_hw_data *ha = NULL;
686 
687 	if (!fcport || !fcport->vha || !fcport->vha->hw)
688 		return;
689 
690 	ha = fcport->vha->hw;
691 
692 	if (fcport->se_sess) {
693 		ha->tgt.tgt_ops->shutdown_sess(fcport);
694 		ha->tgt.tgt_ops->put_sess(fcport);
695 	} else {
696 		qlt_unreg_sess(fcport);
697 	}
698 }
699 
700 /*
701  * Called from qla2x00_reg_remote_port()
702  */
qlt_fc_port_added(struct scsi_qla_host * vha,fc_port_t * fcport)703 void qlt_fc_port_added(struct scsi_qla_host *vha, fc_port_t *fcport)
704 {
705 	struct qla_hw_data *ha = vha->hw;
706 	struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
707 	struct fc_port *sess = fcport;
708 	unsigned long flags;
709 
710 	if (!vha->hw->tgt.tgt_ops)
711 		return;
712 
713 	spin_lock_irqsave(&ha->tgt.sess_lock, flags);
714 	if (tgt->tgt_stop) {
715 		spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
716 		return;
717 	}
718 
719 	if (fcport->disc_state == DSC_DELETE_PEND) {
720 		spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
721 		return;
722 	}
723 
724 	if (!sess->se_sess) {
725 		spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
726 
727 		mutex_lock(&vha->vha_tgt.tgt_mutex);
728 		sess = qlt_create_sess(vha, fcport, false);
729 		mutex_unlock(&vha->vha_tgt.tgt_mutex);
730 
731 		spin_lock_irqsave(&ha->tgt.sess_lock, flags);
732 	} else {
733 		if (fcport->fw_login_state == DSC_LS_PRLI_COMP) {
734 			spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
735 			return;
736 		}
737 
738 		if (!kref_get_unless_zero(&sess->sess_kref)) {
739 			ql_dbg(ql_dbg_disc, vha, 0x2107,
740 			    "%s: kref_get fail sess %8phC \n",
741 			    __func__, sess->port_name);
742 			spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
743 			return;
744 		}
745 
746 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04c,
747 		    "qla_target(%u): %ssession for port %8phC "
748 		    "(loop ID %d) reappeared\n", vha->vp_idx,
749 		    sess->local ? "local " : "", sess->port_name, sess->loop_id);
750 
751 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf007,
752 		    "Reappeared sess %p\n", sess);
753 
754 		ha->tgt.tgt_ops->update_sess(sess, fcport->d_id,
755 		    fcport->loop_id,
756 		    (fcport->flags & FCF_CONF_COMP_SUPPORTED));
757 	}
758 
759 	if (sess && sess->local) {
760 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04d,
761 		    "qla_target(%u): local session for "
762 		    "port %8phC (loop ID %d) became global\n", vha->vp_idx,
763 		    fcport->port_name, sess->loop_id);
764 		sess->local = 0;
765 	}
766 	spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
767 
768 	ha->tgt.tgt_ops->put_sess(sess);
769 }
770 
771 /*
772  * This is a zero-base ref-counting solution, since hardware_lock
773  * guarantees that ref_count is not modified concurrently.
774  * Upon successful return content of iocb is undefined
775  */
776 static struct qlt_plogi_ack_t *
qlt_plogi_ack_find_add(struct scsi_qla_host * vha,port_id_t * id,struct imm_ntfy_from_isp * iocb)777 qlt_plogi_ack_find_add(struct scsi_qla_host *vha, port_id_t *id,
778 		       struct imm_ntfy_from_isp *iocb)
779 {
780 	struct qlt_plogi_ack_t *pla;
781 
782 	lockdep_assert_held(&vha->hw->hardware_lock);
783 
784 	list_for_each_entry(pla, &vha->plogi_ack_list, list) {
785 		if (pla->id.b24 == id->b24) {
786 			ql_dbg(ql_dbg_disc + ql_dbg_verbose, vha, 0x210d,
787 			    "%s %d %8phC Term INOT due to new INOT",
788 			    __func__, __LINE__,
789 			    pla->iocb.u.isp24.port_name);
790 			qlt_send_term_imm_notif(vha, &pla->iocb, 1);
791 			memcpy(&pla->iocb, iocb, sizeof(pla->iocb));
792 			return pla;
793 		}
794 	}
795 
796 	pla = kmem_cache_zalloc(qla_tgt_plogi_cachep, GFP_ATOMIC);
797 	if (!pla) {
798 		ql_dbg(ql_dbg_async, vha, 0x5088,
799 		       "qla_target(%d): Allocation of plogi_ack failed\n",
800 		       vha->vp_idx);
801 		return NULL;
802 	}
803 
804 	memcpy(&pla->iocb, iocb, sizeof(pla->iocb));
805 	pla->id = *id;
806 	list_add_tail(&pla->list, &vha->plogi_ack_list);
807 
808 	return pla;
809 }
810 
qlt_plogi_ack_unref(struct scsi_qla_host * vha,struct qlt_plogi_ack_t * pla)811 void qlt_plogi_ack_unref(struct scsi_qla_host *vha,
812     struct qlt_plogi_ack_t *pla)
813 {
814 	struct imm_ntfy_from_isp *iocb = &pla->iocb;
815 	port_id_t port_id;
816 	uint16_t loop_id;
817 	fc_port_t *fcport = pla->fcport;
818 
819 	BUG_ON(!pla->ref_count);
820 	pla->ref_count--;
821 
822 	if (pla->ref_count)
823 		return;
824 
825 	ql_dbg(ql_dbg_disc, vha, 0x5089,
826 	    "Sending PLOGI ACK to wwn %8phC s_id %02x:%02x:%02x loop_id %#04x"
827 	    " exch %#x ox_id %#x\n", iocb->u.isp24.port_name,
828 	    iocb->u.isp24.port_id[2], iocb->u.isp24.port_id[1],
829 	    iocb->u.isp24.port_id[0],
830 	    le16_to_cpu(iocb->u.isp24.nport_handle),
831 	    iocb->u.isp24.exchange_address, iocb->ox_id);
832 
833 	port_id.b.domain = iocb->u.isp24.port_id[2];
834 	port_id.b.area   = iocb->u.isp24.port_id[1];
835 	port_id.b.al_pa  = iocb->u.isp24.port_id[0];
836 	port_id.b.rsvd_1 = 0;
837 
838 	loop_id = le16_to_cpu(iocb->u.isp24.nport_handle);
839 
840 	fcport->loop_id = loop_id;
841 	fcport->d_id = port_id;
842 	if (iocb->u.isp24.status_subcode == ELS_PLOGI)
843 		qla24xx_post_nack_work(vha, fcport, iocb, SRB_NACK_PLOGI);
844 	else
845 		qla24xx_post_nack_work(vha, fcport, iocb, SRB_NACK_PRLI);
846 
847 	list_for_each_entry(fcport, &vha->vp_fcports, list) {
848 		if (fcport->plogi_link[QLT_PLOGI_LINK_SAME_WWN] == pla)
849 			fcport->plogi_link[QLT_PLOGI_LINK_SAME_WWN] = NULL;
850 		if (fcport->plogi_link[QLT_PLOGI_LINK_CONFLICT] == pla)
851 			fcport->plogi_link[QLT_PLOGI_LINK_CONFLICT] = NULL;
852 	}
853 
854 	list_del(&pla->list);
855 	kmem_cache_free(qla_tgt_plogi_cachep, pla);
856 }
857 
858 void
qlt_plogi_ack_link(struct scsi_qla_host * vha,struct qlt_plogi_ack_t * pla,struct fc_port * sess,enum qlt_plogi_link_t link)859 qlt_plogi_ack_link(struct scsi_qla_host *vha, struct qlt_plogi_ack_t *pla,
860     struct fc_port *sess, enum qlt_plogi_link_t link)
861 {
862 	struct imm_ntfy_from_isp *iocb = &pla->iocb;
863 	/* Inc ref_count first because link might already be pointing at pla */
864 	pla->ref_count++;
865 
866 	ql_dbg(ql_dbg_tgt_mgt, vha, 0xf097,
867 		"Linking sess %p [%d] wwn %8phC with PLOGI ACK to wwn %8phC"
868 		" s_id %02x:%02x:%02x, ref=%d pla %p link %d\n",
869 		sess, link, sess->port_name,
870 		iocb->u.isp24.port_name, iocb->u.isp24.port_id[2],
871 		iocb->u.isp24.port_id[1], iocb->u.isp24.port_id[0],
872 		pla->ref_count, pla, link);
873 
874 	if (link == QLT_PLOGI_LINK_CONFLICT) {
875 		switch (sess->disc_state) {
876 		case DSC_DELETED:
877 		case DSC_DELETE_PEND:
878 			pla->ref_count--;
879 			return;
880 		default:
881 			break;
882 		}
883 	}
884 
885 	if (sess->plogi_link[link])
886 		qlt_plogi_ack_unref(vha, sess->plogi_link[link]);
887 
888 	if (link == QLT_PLOGI_LINK_SAME_WWN)
889 		pla->fcport = sess;
890 
891 	sess->plogi_link[link] = pla;
892 }
893 
894 typedef struct {
895 	/* These fields must be initialized by the caller */
896 	port_id_t id;
897 	/*
898 	 * number of cmds dropped while we were waiting for
899 	 * initiator to ack LOGO initialize to 1 if LOGO is
900 	 * triggered by a command, otherwise, to 0
901 	 */
902 	int cmd_count;
903 
904 	/* These fields are used by callee */
905 	struct list_head list;
906 } qlt_port_logo_t;
907 
908 static void
qlt_send_first_logo(struct scsi_qla_host * vha,qlt_port_logo_t * logo)909 qlt_send_first_logo(struct scsi_qla_host *vha, qlt_port_logo_t *logo)
910 {
911 	qlt_port_logo_t *tmp;
912 	int res;
913 
914 	if (test_bit(PFLG_DRIVER_REMOVING, &vha->pci_flags)) {
915 		res = 0;
916 		goto out;
917 	}
918 
919 	mutex_lock(&vha->vha_tgt.tgt_mutex);
920 
921 	list_for_each_entry(tmp, &vha->logo_list, list) {
922 		if (tmp->id.b24 == logo->id.b24) {
923 			tmp->cmd_count += logo->cmd_count;
924 			mutex_unlock(&vha->vha_tgt.tgt_mutex);
925 			return;
926 		}
927 	}
928 
929 	list_add_tail(&logo->list, &vha->logo_list);
930 
931 	mutex_unlock(&vha->vha_tgt.tgt_mutex);
932 
933 	res = qla24xx_els_dcmd_iocb(vha, ELS_DCMD_LOGO, logo->id);
934 
935 	mutex_lock(&vha->vha_tgt.tgt_mutex);
936 	list_del(&logo->list);
937 	mutex_unlock(&vha->vha_tgt.tgt_mutex);
938 
939 out:
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);
944 }
945 
qlt_free_session_done(struct work_struct * work)946 void qlt_free_session_done(struct work_struct *work)
947 {
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 	scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
956 	struct qlt_plogi_ack_t *own =
957 		sess->plogi_link[QLT_PLOGI_LINK_SAME_WWN];
958 
959 	ql_dbg(ql_dbg_disc, vha, 0xf084,
960 		"%s: se_sess %p / sess %p from port %8phC loop_id %#04x"
961 		" s_id %02x:%02x:%02x logout %d keep %d els_logo %d\n",
962 		__func__, sess->se_sess, sess, sess->port_name, sess->loop_id,
963 		sess->d_id.b.domain, sess->d_id.b.area, sess->d_id.b.al_pa,
964 		sess->logout_on_delete, sess->keep_nport_handle,
965 		sess->send_els_logo);
966 
967 	if (!IS_SW_RESV_ADDR(sess->d_id)) {
968 		qla2x00_mark_device_lost(vha, sess, 0);
969 
970 		if (sess->send_els_logo) {
971 			qlt_port_logo_t logo;
972 
973 			logo.id = sess->d_id;
974 			logo.cmd_count = 0;
975 			INIT_LIST_HEAD(&logo.list);
976 			if (!own)
977 				qlt_send_first_logo(vha, &logo);
978 			sess->send_els_logo = 0;
979 		}
980 
981 		if (sess->logout_on_delete && sess->loop_id != FC_NO_LOOP_ID) {
982 			int rc;
983 
984 			if (!own ||
985 			     (own->iocb.u.isp24.status_subcode == ELS_PLOGI)) {
986 				sess->logout_completed = 0;
987 				rc = qla2x00_post_async_logout_work(vha, sess,
988 				    NULL);
989 				if (rc != QLA_SUCCESS)
990 					ql_log(ql_log_warn, vha, 0xf085,
991 					    "Schedule logo failed sess %p rc %d\n",
992 					    sess, rc);
993 				else
994 					logout_started = true;
995 			} else if (own && (own->iocb.u.isp24.status_subcode ==
996 				ELS_PRLI) && ha->flags.rida_fmt2) {
997 				rc = qla2x00_post_async_prlo_work(vha, sess,
998 				    NULL);
999 				if (rc != QLA_SUCCESS)
1000 					ql_log(ql_log_warn, vha, 0xf085,
1001 					    "Schedule PRLO failed sess %p rc %d\n",
1002 					    sess, rc);
1003 				else
1004 					logout_started = true;
1005 			}
1006 		} /* if sess->logout_on_delete */
1007 
1008 		if (sess->nvme_flag & NVME_FLAG_REGISTERED &&
1009 		    !(sess->nvme_flag & NVME_FLAG_DELETING)) {
1010 			sess->nvme_flag |= NVME_FLAG_DELETING;
1011 			qla_nvme_unregister_remote_port(sess);
1012 		}
1013 
1014 		if (ha->flags.edif_enabled &&
1015 				(!own || own->iocb.u.isp24.status_subcode == ELS_PLOGI)) {
1016 			sess->edif.authok = 0;
1017 			if (!ha->flags.host_shutting_down) {
1018 				ql_dbg(ql_dbg_edif, vha, 0x911e,
1019 				       "%s wwpn %8phC calling qla2x00_release_all_sadb\n",
1020 				       __func__, sess->port_name);
1021 				qla2x00_release_all_sadb(vha, sess);
1022 			} else {
1023 				ql_dbg(ql_dbg_edif, vha, 0x911e,
1024 				       "%s bypassing release_all_sadb\n",
1025 				       __func__);
1026 			}
1027 
1028 			qla_edif_clear_appdata(vha, sess);
1029 			qla_edif_sess_down(vha, sess);
1030 		}
1031 	}
1032 
1033 	/*
1034 	 * Release the target session for FC Nexus from fabric module code.
1035 	 */
1036 	if (sess->se_sess != NULL)
1037 		ha->tgt.tgt_ops->free_session(sess);
1038 
1039 	if (logout_started) {
1040 		bool traced = false;
1041 		u16 cnt = 0;
1042 
1043 		while (!READ_ONCE(sess->logout_completed)) {
1044 			if (!traced) {
1045 				ql_dbg(ql_dbg_disc, vha, 0xf086,
1046 					"%s: waiting for sess %p logout\n",
1047 					__func__, sess);
1048 				traced = true;
1049 			}
1050 			msleep(100);
1051 			cnt++;
1052 			/*
1053 			 * Driver timeout is set to 22 Sec, update count value to loop
1054 			 * long enough for log-out to complete before advancing. Otherwise,
1055 			 * straddling logout can interfere with re-login attempt.
1056 			 */
1057 			if (cnt > 230)
1058 				break;
1059 		}
1060 
1061 		ql_dbg(ql_dbg_disc, vha, 0xf087,
1062 		    "%s: sess %p logout completed\n", __func__, sess);
1063 	}
1064 
1065 	/* check for any straggling io left behind */
1066 	if (!(sess->flags & FCF_FCP2_DEVICE) &&
1067 	    qla2x00_eh_wait_for_pending_commands(sess->vha, sess->d_id.b24, 0, WAIT_TARGET)) {
1068 		ql_log(ql_log_warn, vha, 0x3027,
1069 		    "IO not return. Resetting.\n");
1070 		set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
1071 		qla2xxx_wake_dpc(vha);
1072 		qla2x00_wait_for_chip_reset(vha);
1073 	}
1074 
1075 	if (sess->logo_ack_needed) {
1076 		sess->logo_ack_needed = 0;
1077 		qla24xx_async_notify_ack(vha, sess,
1078 			(struct imm_ntfy_from_isp *)sess->iocb, SRB_NACK_LOGO);
1079 	}
1080 
1081 	spin_lock_irqsave(&ha->tgt.sess_lock, flags);
1082 	if (sess->se_sess) {
1083 		sess->se_sess = NULL;
1084 		if (tgt && !IS_SW_RESV_ADDR(sess->d_id))
1085 			tgt->sess_count--;
1086 	}
1087 
1088 	qla2x00_set_fcport_disc_state(sess, DSC_DELETED);
1089 	sess->fw_login_state = DSC_LS_PORT_UNAVAIL;
1090 
1091 	if (sess->login_succ && !IS_SW_RESV_ADDR(sess->d_id)) {
1092 		vha->fcport_count--;
1093 		sess->login_succ = 0;
1094 	}
1095 
1096 	qla2x00_clear_loop_id(sess);
1097 
1098 	if (sess->conflict) {
1099 		sess->conflict->login_pause = 0;
1100 		sess->conflict = NULL;
1101 		if (!test_bit(UNLOADING, &vha->dpc_flags))
1102 			set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
1103 	}
1104 
1105 	{
1106 		struct qlt_plogi_ack_t *con =
1107 		    sess->plogi_link[QLT_PLOGI_LINK_CONFLICT];
1108 		struct imm_ntfy_from_isp *iocb;
1109 
1110 		own = sess->plogi_link[QLT_PLOGI_LINK_SAME_WWN];
1111 
1112 		if (con) {
1113 			iocb = &con->iocb;
1114 			ql_dbg(ql_dbg_tgt_mgt, vha, 0xf099,
1115 				 "se_sess %p / sess %p port %8phC is gone,"
1116 				 " %s (ref=%d), releasing PLOGI for %8phC (ref=%d)\n",
1117 				 sess->se_sess, sess, sess->port_name,
1118 				 own ? "releasing own PLOGI" : "no own PLOGI pending",
1119 				 own ? own->ref_count : -1,
1120 				 iocb->u.isp24.port_name, con->ref_count);
1121 			qlt_plogi_ack_unref(vha, con);
1122 			sess->plogi_link[QLT_PLOGI_LINK_CONFLICT] = NULL;
1123 		} else {
1124 			ql_dbg(ql_dbg_tgt_mgt, vha, 0xf09a,
1125 			    "se_sess %p / sess %p port %8phC is gone, %s (ref=%d)\n",
1126 			    sess->se_sess, sess, sess->port_name,
1127 			    own ? "releasing own PLOGI" :
1128 			    "no own PLOGI pending",
1129 			    own ? own->ref_count : -1);
1130 		}
1131 
1132 		if (own) {
1133 			sess->fw_login_state = DSC_LS_PLOGI_PEND;
1134 			qlt_plogi_ack_unref(vha, own);
1135 			sess->plogi_link[QLT_PLOGI_LINK_SAME_WWN] = NULL;
1136 		}
1137 	}
1138 
1139 	sess->explicit_logout = 0;
1140 	spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
1141 
1142 	qla2x00_dfs_remove_rport(vha, sess);
1143 
1144 	spin_lock_irqsave(&vha->work_lock, flags);
1145 	sess->flags &= ~FCF_ASYNC_SENT;
1146 	sess->deleted = QLA_SESS_DELETED;
1147 	sess->free_pending = 0;
1148 	spin_unlock_irqrestore(&vha->work_lock, flags);
1149 
1150 	ql_dbg(ql_dbg_disc, vha, 0xf001,
1151 	    "Unregistration of sess %p %8phC finished fcp_cnt %d\n",
1152 		sess, sess->port_name, vha->fcport_count);
1153 
1154 	if (tgt && (tgt->sess_count == 0))
1155 		wake_up_all(&tgt->waitQ);
1156 
1157 	if (!test_bit(PFLG_DRIVER_REMOVING, &base_vha->pci_flags) &&
1158 	    !(vha->vp_idx && test_bit(VPORT_DELETE, &vha->dpc_flags)) &&
1159 	    (!tgt || !tgt->tgt_stop) && !LOOP_TRANSITION(vha)) {
1160 		switch (vha->host->active_mode) {
1161 		case MODE_INITIATOR:
1162 		case MODE_DUAL:
1163 			set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
1164 			qla2xxx_wake_dpc(vha);
1165 			break;
1166 		case MODE_TARGET:
1167 		default:
1168 			/* no-op */
1169 			break;
1170 		}
1171 	}
1172 
1173 	if (vha->fcport_count == 0)
1174 		wake_up_all(&vha->fcport_waitQ);
1175 }
1176 
1177 /* ha->tgt.sess_lock supposed to be held on entry */
qlt_unreg_sess(struct fc_port * sess)1178 void qlt_unreg_sess(struct fc_port *sess)
1179 {
1180 	struct scsi_qla_host *vha = sess->vha;
1181 	unsigned long flags;
1182 
1183 	ql_dbg(ql_dbg_disc, sess->vha, 0x210a,
1184 	    "%s sess %p for deletion %8phC\n",
1185 	    __func__, sess, sess->port_name);
1186 
1187 	spin_lock_irqsave(&sess->vha->work_lock, flags);
1188 	if (sess->free_pending) {
1189 		spin_unlock_irqrestore(&sess->vha->work_lock, flags);
1190 		return;
1191 	}
1192 	sess->free_pending = 1;
1193 	/*
1194 	 * Use FCF_ASYNC_SENT flag to block other cmds used in sess
1195 	 * management from being sent.
1196 	 */
1197 	sess->flags |= FCF_ASYNC_SENT;
1198 	sess->deleted = QLA_SESS_DELETION_IN_PROGRESS;
1199 	spin_unlock_irqrestore(&sess->vha->work_lock, flags);
1200 
1201 	if (sess->se_sess)
1202 		vha->hw->tgt.tgt_ops->clear_nacl_from_fcport_map(sess);
1203 
1204 	qla2x00_set_fcport_disc_state(sess, DSC_DELETE_PEND);
1205 	sess->last_rscn_gen = sess->rscn_gen;
1206 	sess->last_login_gen = sess->login_gen;
1207 
1208 	queue_work(sess->vha->hw->wq, &sess->free_work);
1209 }
1210 EXPORT_SYMBOL(qlt_unreg_sess);
1211 
qlt_reset(struct scsi_qla_host * vha,void * iocb,int mcmd)1212 static int qlt_reset(struct scsi_qla_host *vha, void *iocb, int mcmd)
1213 {
1214 	struct qla_hw_data *ha = vha->hw;
1215 	struct fc_port *sess = NULL;
1216 	uint16_t loop_id;
1217 	int res = 0;
1218 	struct imm_ntfy_from_isp *n = (struct imm_ntfy_from_isp *)iocb;
1219 	unsigned long flags;
1220 
1221 	loop_id = le16_to_cpu(n->u.isp24.nport_handle);
1222 	if (loop_id == 0xFFFF) {
1223 		/* Global event */
1224 		atomic_inc(&vha->vha_tgt.qla_tgt->tgt_global_resets_count);
1225 		spin_lock_irqsave(&ha->tgt.sess_lock, flags);
1226 		qlt_clear_tgt_db(vha->vha_tgt.qla_tgt);
1227 		spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
1228 	} else {
1229 		spin_lock_irqsave(&ha->tgt.sess_lock, flags);
1230 		sess = ha->tgt.tgt_ops->find_sess_by_loop_id(vha, loop_id);
1231 		spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
1232 	}
1233 
1234 	ql_dbg(ql_dbg_tgt, vha, 0xe000,
1235 	    "Using sess for qla_tgt_reset: %p\n", sess);
1236 	if (!sess) {
1237 		res = -ESRCH;
1238 		return res;
1239 	}
1240 
1241 	ql_dbg(ql_dbg_tgt, vha, 0xe047,
1242 	    "scsi(%ld): resetting (session %p from port %8phC mcmd %x, "
1243 	    "loop_id %d)\n", vha->host_no, sess, sess->port_name,
1244 	    mcmd, loop_id);
1245 
1246 	return qlt_issue_task_mgmt(sess, 0, mcmd, iocb, QLA24XX_MGMT_SEND_NACK);
1247 }
1248 
qla24xx_chk_fcp_state(struct fc_port * sess)1249 static void qla24xx_chk_fcp_state(struct fc_port *sess)
1250 {
1251 	if (sess->chip_reset != sess->vha->hw->base_qpair->chip_reset) {
1252 		sess->logout_on_delete = 0;
1253 		sess->logo_ack_needed = 0;
1254 		sess->fw_login_state = DSC_LS_PORT_UNAVAIL;
1255 	}
1256 }
1257 
qlt_schedule_sess_for_deletion(struct fc_port * sess)1258 void qlt_schedule_sess_for_deletion(struct fc_port *sess)
1259 {
1260 	struct qla_tgt *tgt = sess->tgt;
1261 	unsigned long flags;
1262 	u16 sec;
1263 
1264 	switch (sess->disc_state) {
1265 	case DSC_DELETE_PEND:
1266 		return;
1267 	case DSC_DELETED:
1268 		if (!sess->plogi_link[QLT_PLOGI_LINK_SAME_WWN] &&
1269 			!sess->plogi_link[QLT_PLOGI_LINK_CONFLICT]) {
1270 			if (tgt && tgt->tgt_stop && tgt->sess_count == 0)
1271 				wake_up_all(&tgt->waitQ);
1272 
1273 			if (sess->vha->fcport_count == 0)
1274 				wake_up_all(&sess->vha->fcport_waitQ);
1275 			return;
1276 		}
1277 		break;
1278 	case DSC_UPD_FCPORT:
1279 		/*
1280 		 * This port is not done reporting to upper layer.
1281 		 * let it finish
1282 		 */
1283 		sess->next_disc_state = DSC_DELETE_PEND;
1284 		sec = jiffies_to_msecs(jiffies -
1285 		    sess->jiffies_at_registration)/1000;
1286 		if (sess->sec_since_registration < sec && sec && !(sec % 5)) {
1287 			sess->sec_since_registration = sec;
1288 			ql_dbg(ql_dbg_disc, sess->vha, 0xffff,
1289 			    "%s %8phC : Slow Rport registration(%d Sec)\n",
1290 			    __func__, sess->port_name, sec);
1291 		}
1292 		return;
1293 	default:
1294 		break;
1295 	}
1296 
1297 	spin_lock_irqsave(&sess->vha->work_lock, flags);
1298 	if (sess->deleted == QLA_SESS_DELETION_IN_PROGRESS) {
1299 		spin_unlock_irqrestore(&sess->vha->work_lock, flags);
1300 		return;
1301 	}
1302 	sess->deleted = QLA_SESS_DELETION_IN_PROGRESS;
1303 	spin_unlock_irqrestore(&sess->vha->work_lock, flags);
1304 
1305 	sess->prli_pend_timer = 0;
1306 	qla2x00_set_fcport_disc_state(sess, DSC_DELETE_PEND);
1307 
1308 	qla24xx_chk_fcp_state(sess);
1309 
1310 	ql_dbg(ql_log_warn, sess->vha, 0xe001,
1311 	    "Scheduling sess %p for deletion %8phC fc4_type %x\n",
1312 	    sess, sess->port_name, sess->fc4_type);
1313 
1314 	WARN_ON(!queue_work(sess->vha->hw->wq, &sess->del_work));
1315 }
1316 
qlt_clear_tgt_db(struct qla_tgt * tgt)1317 static void qlt_clear_tgt_db(struct qla_tgt *tgt)
1318 {
1319 	struct fc_port *sess;
1320 	scsi_qla_host_t *vha = tgt->vha;
1321 
1322 	list_for_each_entry(sess, &vha->vp_fcports, list) {
1323 		if (sess->se_sess)
1324 			qlt_schedule_sess_for_deletion(sess);
1325 	}
1326 
1327 	/* At this point tgt could be already dead */
1328 }
1329 
qla24xx_get_loop_id(struct scsi_qla_host * vha,be_id_t s_id,uint16_t * loop_id)1330 static int qla24xx_get_loop_id(struct scsi_qla_host *vha, be_id_t s_id,
1331 	uint16_t *loop_id)
1332 {
1333 	struct qla_hw_data *ha = vha->hw;
1334 	dma_addr_t gid_list_dma;
1335 	struct gid_list_info *gid_list, *gid;
1336 	int res, rc, i;
1337 	uint16_t entries;
1338 
1339 	gid_list = dma_alloc_coherent(&ha->pdev->dev, qla2x00_gid_list_size(ha),
1340 	    &gid_list_dma, GFP_KERNEL);
1341 	if (!gid_list) {
1342 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf044,
1343 		    "qla_target(%d): DMA Alloc failed of %u\n",
1344 		    vha->vp_idx, qla2x00_gid_list_size(ha));
1345 		return -ENOMEM;
1346 	}
1347 
1348 	/* Get list of logged in devices */
1349 	rc = qla24xx_gidlist_wait(vha, gid_list, gid_list_dma, &entries);
1350 	if (rc != QLA_SUCCESS) {
1351 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf045,
1352 		    "qla_target(%d): get_id_list() failed: %x\n",
1353 		    vha->vp_idx, rc);
1354 		res = -EBUSY;
1355 		goto out_free_id_list;
1356 	}
1357 
1358 	gid = gid_list;
1359 	res = -ENOENT;
1360 	for (i = 0; i < entries; i++) {
1361 		if (gid->al_pa == s_id.al_pa &&
1362 		    gid->area == s_id.area &&
1363 		    gid->domain == s_id.domain) {
1364 			*loop_id = le16_to_cpu(gid->loop_id);
1365 			res = 0;
1366 			break;
1367 		}
1368 		gid = (void *)gid + ha->gid_list_info_size;
1369 	}
1370 
1371 out_free_id_list:
1372 	dma_free_coherent(&ha->pdev->dev, qla2x00_gid_list_size(ha),
1373 	    gid_list, gid_list_dma);
1374 	return res;
1375 }
1376 
1377 /*
1378  * Adds an extra ref to allow to drop hw lock after adding sess to the list.
1379  * Caller must put it.
1380  */
qlt_create_sess(struct scsi_qla_host * vha,fc_port_t * fcport,bool local)1381 static struct fc_port *qlt_create_sess(
1382 	struct scsi_qla_host *vha,
1383 	fc_port_t *fcport,
1384 	bool local)
1385 {
1386 	struct qla_hw_data *ha = vha->hw;
1387 	struct fc_port *sess = fcport;
1388 	unsigned long flags;
1389 
1390 	if (vha->vha_tgt.qla_tgt->tgt_stop)
1391 		return NULL;
1392 
1393 	if (fcport->se_sess) {
1394 		if (!kref_get_unless_zero(&sess->sess_kref)) {
1395 			ql_dbg(ql_dbg_disc, vha, 0x20f6,
1396 			    "%s: kref_get_unless_zero failed for %8phC\n",
1397 			    __func__, sess->port_name);
1398 			return NULL;
1399 		}
1400 		return fcport;
1401 	}
1402 	sess->tgt = vha->vha_tgt.qla_tgt;
1403 	sess->local = local;
1404 
1405 	/*
1406 	 * Under normal circumstances we want to logout from firmware when
1407 	 * session eventually ends and release corresponding nport handle.
1408 	 * In the exception cases (e.g. when new PLOGI is waiting) corresponding
1409 	 * code will adjust these flags as necessary.
1410 	 */
1411 	sess->logout_on_delete = 1;
1412 	sess->keep_nport_handle = 0;
1413 	sess->logout_completed = 0;
1414 
1415 	if (ha->tgt.tgt_ops->check_initiator_node_acl(vha,
1416 	    &fcport->port_name[0], sess) < 0) {
1417 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf015,
1418 		    "(%d) %8phC check_initiator_node_acl failed\n",
1419 		    vha->vp_idx, fcport->port_name);
1420 		return NULL;
1421 	} else {
1422 		kref_init(&fcport->sess_kref);
1423 		/*
1424 		 * Take an extra reference to ->sess_kref here to handle
1425 		 * fc_port access across ->tgt.sess_lock reaquire.
1426 		 */
1427 		if (!kref_get_unless_zero(&sess->sess_kref)) {
1428 			ql_dbg(ql_dbg_disc, vha, 0x20f7,
1429 			    "%s: kref_get_unless_zero failed for %8phC\n",
1430 			    __func__, sess->port_name);
1431 			return NULL;
1432 		}
1433 
1434 		spin_lock_irqsave(&ha->tgt.sess_lock, flags);
1435 		if (!IS_SW_RESV_ADDR(sess->d_id))
1436 			vha->vha_tgt.qla_tgt->sess_count++;
1437 
1438 		qlt_do_generation_tick(vha, &sess->generation);
1439 		spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
1440 	}
1441 
1442 	ql_dbg(ql_dbg_tgt_mgt, vha, 0xf006,
1443 	    "Adding sess %p se_sess %p  to tgt %p sess_count %d\n",
1444 	    sess, sess->se_sess, vha->vha_tgt.qla_tgt,
1445 	    vha->vha_tgt.qla_tgt->sess_count);
1446 
1447 	ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04b,
1448 	    "qla_target(%d): %ssession for wwn %8phC (loop_id %d, "
1449 	    "s_id %x:%x:%x, confirmed completion %ssupported) added\n",
1450 	    vha->vp_idx, local ?  "local " : "", fcport->port_name,
1451 	    fcport->loop_id, sess->d_id.b.domain, sess->d_id.b.area,
1452 	    sess->d_id.b.al_pa, sess->conf_compl_supported ?  "" : "not ");
1453 
1454 	return sess;
1455 }
1456 
test_tgt_sess_count(struct qla_tgt * tgt)1457 static inline int test_tgt_sess_count(struct qla_tgt *tgt)
1458 {
1459 	struct qla_hw_data *ha = tgt->ha;
1460 	unsigned long flags;
1461 	int res;
1462 	/*
1463 	 * We need to protect against race, when tgt is freed before or
1464 	 * inside wake_up()
1465 	 */
1466 	spin_lock_irqsave(&ha->tgt.sess_lock, flags);
1467 	ql_dbg(ql_dbg_tgt, tgt->vha, 0xe002,
1468 	    "tgt %p, sess_count=%d\n",
1469 	    tgt, tgt->sess_count);
1470 	res = (tgt->sess_count == 0);
1471 	spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
1472 
1473 	return res;
1474 }
1475 
1476 /* Called by tcm_qla2xxx configfs code */
qlt_stop_phase1(struct qla_tgt * tgt)1477 int qlt_stop_phase1(struct qla_tgt *tgt)
1478 {
1479 	struct scsi_qla_host *vha = tgt->vha;
1480 	struct qla_hw_data *ha = tgt->ha;
1481 	unsigned long flags;
1482 
1483 	mutex_lock(&ha->optrom_mutex);
1484 	mutex_lock(&qla_tgt_mutex);
1485 
1486 	if (tgt->tgt_stop || tgt->tgt_stopped) {
1487 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04e,
1488 		    "Already in tgt->tgt_stop or tgt_stopped state\n");
1489 		mutex_unlock(&qla_tgt_mutex);
1490 		mutex_unlock(&ha->optrom_mutex);
1491 		return -EPERM;
1492 	}
1493 
1494 	ql_dbg(ql_dbg_tgt_mgt, vha, 0xe003, "Stopping target for host %ld(%p)\n",
1495 	    vha->host_no, vha);
1496 	/*
1497 	 * Mutex needed to sync with qla_tgt_fc_port_[added,deleted].
1498 	 * Lock is needed, because we still can get an incoming packet.
1499 	 */
1500 	mutex_lock(&vha->vha_tgt.tgt_mutex);
1501 	tgt->tgt_stop = 1;
1502 	qlt_clear_tgt_db(tgt);
1503 	mutex_unlock(&vha->vha_tgt.tgt_mutex);
1504 	mutex_unlock(&qla_tgt_mutex);
1505 
1506 	ql_dbg(ql_dbg_tgt_mgt, vha, 0xf009,
1507 	    "Waiting for sess works (tgt %p)", tgt);
1508 	spin_lock_irqsave(&tgt->sess_work_lock, flags);
1509 	do {
1510 		spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
1511 		flush_work(&tgt->sess_work);
1512 		spin_lock_irqsave(&tgt->sess_work_lock, flags);
1513 	} while (!list_empty(&tgt->sess_works_list));
1514 	spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
1515 
1516 	ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00a,
1517 	    "Waiting for tgt %p: sess_count=%d\n", tgt, tgt->sess_count);
1518 
1519 	wait_event_timeout(tgt->waitQ, test_tgt_sess_count(tgt), 10*HZ);
1520 
1521 	/* Big hammer */
1522 	if (!ha->flags.host_shutting_down &&
1523 	    (qla_tgt_mode_enabled(vha) || qla_dual_mode_enabled(vha)))
1524 		qlt_disable_vha(vha);
1525 
1526 	/* Wait for sessions to clear out (just in case) */
1527 	wait_event_timeout(tgt->waitQ, test_tgt_sess_count(tgt), 10*HZ);
1528 	mutex_unlock(&ha->optrom_mutex);
1529 
1530 	return 0;
1531 }
1532 EXPORT_SYMBOL(qlt_stop_phase1);
1533 
1534 /* Called by tcm_qla2xxx configfs code */
qlt_stop_phase2(struct qla_tgt * tgt)1535 void qlt_stop_phase2(struct qla_tgt *tgt)
1536 {
1537 	scsi_qla_host_t *vha = tgt->vha;
1538 
1539 	if (tgt->tgt_stopped) {
1540 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04f,
1541 		    "Already in tgt->tgt_stopped state\n");
1542 		dump_stack();
1543 		return;
1544 	}
1545 	if (!tgt->tgt_stop) {
1546 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00b,
1547 		    "%s: phase1 stop is not completed\n", __func__);
1548 		dump_stack();
1549 		return;
1550 	}
1551 
1552 	mutex_lock(&tgt->ha->optrom_mutex);
1553 	mutex_lock(&vha->vha_tgt.tgt_mutex);
1554 	tgt->tgt_stop = 0;
1555 	tgt->tgt_stopped = 1;
1556 	mutex_unlock(&vha->vha_tgt.tgt_mutex);
1557 	mutex_unlock(&tgt->ha->optrom_mutex);
1558 
1559 	ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00c, "Stop of tgt %p finished\n",
1560 	    tgt);
1561 
1562 	switch (vha->qlini_mode) {
1563 	case QLA2XXX_INI_MODE_EXCLUSIVE:
1564 		vha->flags.online = 1;
1565 		set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
1566 		break;
1567 	default:
1568 		break;
1569 	}
1570 }
1571 EXPORT_SYMBOL(qlt_stop_phase2);
1572 
1573 /* Called from qlt_remove_target() -> qla2x00_remove_one() */
qlt_release(struct qla_tgt * tgt)1574 static void qlt_release(struct qla_tgt *tgt)
1575 {
1576 	scsi_qla_host_t *vha = tgt->vha;
1577 	void *node;
1578 	u64 key = 0;
1579 	u16 i;
1580 	struct qla_qpair_hint *h;
1581 	struct qla_hw_data *ha = vha->hw;
1582 
1583 	if (!tgt->tgt_stop && !tgt->tgt_stopped)
1584 		qlt_stop_phase1(tgt);
1585 
1586 	if (!tgt->tgt_stopped)
1587 		qlt_stop_phase2(tgt);
1588 
1589 	for (i = 0; i < vha->hw->max_qpairs + 1; i++) {
1590 		unsigned long flags;
1591 
1592 		h = &tgt->qphints[i];
1593 		if (h->qpair) {
1594 			spin_lock_irqsave(h->qpair->qp_lock_ptr, flags);
1595 			list_del(&h->hint_elem);
1596 			spin_unlock_irqrestore(h->qpair->qp_lock_ptr, flags);
1597 			h->qpair = NULL;
1598 		}
1599 	}
1600 	kfree(tgt->qphints);
1601 	mutex_lock(&qla_tgt_mutex);
1602 	list_del(&vha->vha_tgt.qla_tgt->tgt_list_entry);
1603 	mutex_unlock(&qla_tgt_mutex);
1604 
1605 	btree_for_each_safe64(&tgt->lun_qpair_map, key, node)
1606 		btree_remove64(&tgt->lun_qpair_map, key);
1607 
1608 	btree_destroy64(&tgt->lun_qpair_map);
1609 
1610 	if (vha->vp_idx)
1611 		if (ha->tgt.tgt_ops &&
1612 		    ha->tgt.tgt_ops->remove_target &&
1613 		    vha->vha_tgt.target_lport_ptr)
1614 			ha->tgt.tgt_ops->remove_target(vha);
1615 
1616 	vha->vha_tgt.qla_tgt = NULL;
1617 
1618 	ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00d,
1619 	    "Release of tgt %p finished\n", tgt);
1620 
1621 	kfree(tgt);
1622 }
1623 
1624 /* ha->hardware_lock supposed to be held on entry */
qlt_sched_sess_work(struct qla_tgt * tgt,int type,const void * param,unsigned int param_size)1625 static int qlt_sched_sess_work(struct qla_tgt *tgt, int type,
1626 	const void *param, unsigned int param_size)
1627 {
1628 	struct qla_tgt_sess_work_param *prm;
1629 	unsigned long flags;
1630 
1631 	prm = kzalloc(sizeof(*prm), GFP_ATOMIC);
1632 	if (!prm) {
1633 		ql_dbg(ql_dbg_tgt_mgt, tgt->vha, 0xf050,
1634 		    "qla_target(%d): Unable to create session "
1635 		    "work, command will be refused", 0);
1636 		return -ENOMEM;
1637 	}
1638 
1639 	ql_dbg(ql_dbg_tgt_mgt, tgt->vha, 0xf00e,
1640 	    "Scheduling work (type %d, prm %p)"
1641 	    " to find session for param %p (size %d, tgt %p)\n",
1642 	    type, prm, param, param_size, tgt);
1643 
1644 	prm->type = type;
1645 	memcpy(&prm->tm_iocb, param, param_size);
1646 
1647 	spin_lock_irqsave(&tgt->sess_work_lock, flags);
1648 	list_add_tail(&prm->sess_works_list_entry, &tgt->sess_works_list);
1649 	spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
1650 
1651 	schedule_work(&tgt->sess_work);
1652 
1653 	return 0;
1654 }
1655 
1656 /*
1657  * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
1658  */
qlt_send_notify_ack(struct qla_qpair * qpair,struct imm_ntfy_from_isp * ntfy,uint32_t add_flags,uint16_t resp_code,int resp_code_valid,uint16_t srr_flags,uint16_t srr_reject_code,uint8_t srr_explan)1659 static void qlt_send_notify_ack(struct qla_qpair *qpair,
1660 	struct imm_ntfy_from_isp *ntfy,
1661 	uint32_t add_flags, uint16_t resp_code, int resp_code_valid,
1662 	uint16_t srr_flags, uint16_t srr_reject_code, uint8_t srr_explan)
1663 {
1664 	struct scsi_qla_host *vha = qpair->vha;
1665 	struct qla_hw_data *ha = vha->hw;
1666 	request_t *pkt;
1667 	struct nack_to_isp *nack;
1668 
1669 	if (!ha->flags.fw_started)
1670 		return;
1671 
1672 	ql_dbg(ql_dbg_tgt, vha, 0xe004, "Sending NOTIFY_ACK (ha=%p)\n", ha);
1673 
1674 	pkt = (request_t *)__qla2x00_alloc_iocbs(qpair, NULL);
1675 	if (!pkt) {
1676 		ql_dbg(ql_dbg_tgt, vha, 0xe049,
1677 		    "qla_target(%d): %s failed: unable to allocate "
1678 		    "request packet\n", vha->vp_idx, __func__);
1679 		return;
1680 	}
1681 
1682 	if (vha->vha_tgt.qla_tgt != NULL)
1683 		vha->vha_tgt.qla_tgt->notify_ack_expected++;
1684 
1685 	pkt->entry_type = NOTIFY_ACK_TYPE;
1686 	pkt->entry_count = 1;
1687 
1688 	nack = (struct nack_to_isp *)pkt;
1689 	nack->ox_id = ntfy->ox_id;
1690 
1691 	nack->u.isp24.handle = QLA_TGT_SKIP_HANDLE;
1692 	nack->u.isp24.nport_handle = ntfy->u.isp24.nport_handle;
1693 	if (le16_to_cpu(ntfy->u.isp24.status) == IMM_NTFY_ELS) {
1694 		nack->u.isp24.flags = ntfy->u.isp24.flags &
1695 			cpu_to_le16(NOTIFY24XX_FLAGS_PUREX_IOCB);
1696 	}
1697 	nack->u.isp24.srr_rx_id = ntfy->u.isp24.srr_rx_id;
1698 	nack->u.isp24.status = ntfy->u.isp24.status;
1699 	nack->u.isp24.status_subcode = ntfy->u.isp24.status_subcode;
1700 	nack->u.isp24.fw_handle = ntfy->u.isp24.fw_handle;
1701 	nack->u.isp24.exchange_address = ntfy->u.isp24.exchange_address;
1702 	nack->u.isp24.srr_rel_offs = ntfy->u.isp24.srr_rel_offs;
1703 	nack->u.isp24.srr_ui = ntfy->u.isp24.srr_ui;
1704 	nack->u.isp24.srr_flags = cpu_to_le16(srr_flags);
1705 	nack->u.isp24.srr_reject_code = srr_reject_code;
1706 	nack->u.isp24.srr_reject_code_expl = srr_explan;
1707 	nack->u.isp24.vp_index = ntfy->u.isp24.vp_index;
1708 
1709 	/* TODO qualify this with EDIF enable */
1710 	if (ntfy->u.isp24.status_subcode == ELS_PLOGI &&
1711 	    (le16_to_cpu(ntfy->u.isp24.flags) & NOTIFY24XX_FLAGS_FCSP)) {
1712 		nack->u.isp24.flags |= cpu_to_le16(NOTIFY_ACK_FLAGS_FCSP);
1713 	}
1714 
1715 	ql_dbg(ql_dbg_tgt, vha, 0xe005,
1716 	    "qla_target(%d): Sending 24xx Notify Ack %d\n",
1717 	    vha->vp_idx, nack->u.isp24.status);
1718 
1719 	/* Memory Barrier */
1720 	wmb();
1721 	qla2x00_start_iocbs(vha, qpair->req);
1722 }
1723 
qlt_build_abts_resp_iocb(struct qla_tgt_mgmt_cmd * mcmd)1724 static int qlt_build_abts_resp_iocb(struct qla_tgt_mgmt_cmd *mcmd)
1725 {
1726 	struct scsi_qla_host *vha = mcmd->vha;
1727 	struct qla_hw_data *ha = vha->hw;
1728 	struct abts_resp_to_24xx *resp;
1729 	__le32 f_ctl;
1730 	uint32_t h;
1731 	uint8_t *p;
1732 	int rc;
1733 	struct abts_recv_from_24xx *abts = &mcmd->orig_iocb.abts;
1734 	struct qla_qpair *qpair = mcmd->qpair;
1735 
1736 	ql_dbg(ql_dbg_tgt, vha, 0xe006,
1737 	    "Sending task mgmt ABTS response (ha=%p, status=%x)\n",
1738 	    ha, mcmd->fc_tm_rsp);
1739 
1740 	rc = qlt_check_reserve_free_req(qpair, 1);
1741 	if (rc) {
1742 		ql_dbg(ql_dbg_tgt, vha, 0xe04a,
1743 		    "qla_target(%d): %s failed: unable to allocate request packet\n",
1744 		    vha->vp_idx, __func__);
1745 		return -EAGAIN;
1746 	}
1747 
1748 	resp = (struct abts_resp_to_24xx *)qpair->req->ring_ptr;
1749 	memset(resp, 0, sizeof(*resp));
1750 
1751 	h = qlt_make_handle(qpair);
1752 	if (unlikely(h == QLA_TGT_NULL_HANDLE)) {
1753 		/*
1754 		 * CTIO type 7 from the firmware doesn't provide a way to
1755 		 * know the initiator's LOOP ID, hence we can't find
1756 		 * the session and, so, the command.
1757 		 */
1758 		return -EAGAIN;
1759 	} else {
1760 		qpair->req->outstanding_cmds[h] = (srb_t *)mcmd;
1761 	}
1762 
1763 	resp->handle = make_handle(qpair->req->id, h);
1764 	resp->entry_type = ABTS_RESP_24XX;
1765 	resp->entry_count = 1;
1766 	resp->nport_handle = abts->nport_handle;
1767 	resp->vp_index = vha->vp_idx;
1768 	resp->sof_type = abts->sof_type;
1769 	resp->exchange_address = abts->exchange_address;
1770 	resp->fcp_hdr_le = abts->fcp_hdr_le;
1771 	f_ctl = cpu_to_le32(F_CTL_EXCH_CONTEXT_RESP |
1772 	    F_CTL_LAST_SEQ | F_CTL_END_SEQ |
1773 	    F_CTL_SEQ_INITIATIVE);
1774 	p = (uint8_t *)&f_ctl;
1775 	resp->fcp_hdr_le.f_ctl[0] = *p++;
1776 	resp->fcp_hdr_le.f_ctl[1] = *p++;
1777 	resp->fcp_hdr_le.f_ctl[2] = *p;
1778 
1779 	resp->fcp_hdr_le.d_id = abts->fcp_hdr_le.s_id;
1780 	resp->fcp_hdr_le.s_id = abts->fcp_hdr_le.d_id;
1781 
1782 	resp->exchange_addr_to_abort = abts->exchange_addr_to_abort;
1783 	if (mcmd->fc_tm_rsp == FCP_TMF_CMPL) {
1784 		resp->fcp_hdr_le.r_ctl = R_CTL_BASIC_LINK_SERV | R_CTL_B_ACC;
1785 		resp->payload.ba_acct.seq_id_valid = SEQ_ID_INVALID;
1786 		resp->payload.ba_acct.low_seq_cnt = 0x0000;
1787 		resp->payload.ba_acct.high_seq_cnt = cpu_to_le16(0xFFFF);
1788 		resp->payload.ba_acct.ox_id = abts->fcp_hdr_le.ox_id;
1789 		resp->payload.ba_acct.rx_id = abts->fcp_hdr_le.rx_id;
1790 	} else {
1791 		resp->fcp_hdr_le.r_ctl = R_CTL_BASIC_LINK_SERV | R_CTL_B_RJT;
1792 		resp->payload.ba_rjt.reason_code =
1793 			BA_RJT_REASON_CODE_UNABLE_TO_PERFORM;
1794 		/* Other bytes are zero */
1795 	}
1796 
1797 	vha->vha_tgt.qla_tgt->abts_resp_expected++;
1798 
1799 	/* Memory Barrier */
1800 	wmb();
1801 	if (qpair->reqq_start_iocbs)
1802 		qpair->reqq_start_iocbs(qpair);
1803 	else
1804 		qla2x00_start_iocbs(vha, qpair->req);
1805 
1806 	return rc;
1807 }
1808 
1809 /*
1810  * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
1811  */
qlt_24xx_send_abts_resp(struct qla_qpair * qpair,struct abts_recv_from_24xx * abts,uint32_t status,bool ids_reversed)1812 static void qlt_24xx_send_abts_resp(struct qla_qpair *qpair,
1813 	struct abts_recv_from_24xx *abts, uint32_t status,
1814 	bool ids_reversed)
1815 {
1816 	struct scsi_qla_host *vha = qpair->vha;
1817 	struct qla_hw_data *ha = vha->hw;
1818 	struct abts_resp_to_24xx *resp;
1819 	__le32 f_ctl;
1820 	uint8_t *p;
1821 
1822 	ql_dbg(ql_dbg_tgt, vha, 0xe006,
1823 	    "Sending task mgmt ABTS response (ha=%p, atio=%p, status=%x\n",
1824 	    ha, abts, status);
1825 
1826 	resp = (struct abts_resp_to_24xx *)qla2x00_alloc_iocbs_ready(qpair,
1827 	    NULL);
1828 	if (!resp) {
1829 		ql_dbg(ql_dbg_tgt, vha, 0xe04a,
1830 		    "qla_target(%d): %s failed: unable to allocate "
1831 		    "request packet", vha->vp_idx, __func__);
1832 		return;
1833 	}
1834 
1835 	resp->entry_type = ABTS_RESP_24XX;
1836 	resp->handle = QLA_TGT_SKIP_HANDLE;
1837 	resp->entry_count = 1;
1838 	resp->nport_handle = abts->nport_handle;
1839 	resp->vp_index = vha->vp_idx;
1840 	resp->sof_type = abts->sof_type;
1841 	resp->exchange_address = abts->exchange_address;
1842 	resp->fcp_hdr_le = abts->fcp_hdr_le;
1843 	f_ctl = cpu_to_le32(F_CTL_EXCH_CONTEXT_RESP |
1844 	    F_CTL_LAST_SEQ | F_CTL_END_SEQ |
1845 	    F_CTL_SEQ_INITIATIVE);
1846 	p = (uint8_t *)&f_ctl;
1847 	resp->fcp_hdr_le.f_ctl[0] = *p++;
1848 	resp->fcp_hdr_le.f_ctl[1] = *p++;
1849 	resp->fcp_hdr_le.f_ctl[2] = *p;
1850 	if (ids_reversed) {
1851 		resp->fcp_hdr_le.d_id = abts->fcp_hdr_le.d_id;
1852 		resp->fcp_hdr_le.s_id = abts->fcp_hdr_le.s_id;
1853 	} else {
1854 		resp->fcp_hdr_le.d_id = abts->fcp_hdr_le.s_id;
1855 		resp->fcp_hdr_le.s_id = abts->fcp_hdr_le.d_id;
1856 	}
1857 	resp->exchange_addr_to_abort = abts->exchange_addr_to_abort;
1858 	if (status == FCP_TMF_CMPL) {
1859 		resp->fcp_hdr_le.r_ctl = R_CTL_BASIC_LINK_SERV | R_CTL_B_ACC;
1860 		resp->payload.ba_acct.seq_id_valid = SEQ_ID_INVALID;
1861 		resp->payload.ba_acct.low_seq_cnt = 0x0000;
1862 		resp->payload.ba_acct.high_seq_cnt = cpu_to_le16(0xFFFF);
1863 		resp->payload.ba_acct.ox_id = abts->fcp_hdr_le.ox_id;
1864 		resp->payload.ba_acct.rx_id = abts->fcp_hdr_le.rx_id;
1865 	} else {
1866 		resp->fcp_hdr_le.r_ctl = R_CTL_BASIC_LINK_SERV | R_CTL_B_RJT;
1867 		resp->payload.ba_rjt.reason_code =
1868 			BA_RJT_REASON_CODE_UNABLE_TO_PERFORM;
1869 		/* Other bytes are zero */
1870 	}
1871 
1872 	vha->vha_tgt.qla_tgt->abts_resp_expected++;
1873 
1874 	/* Memory Barrier */
1875 	wmb();
1876 	if (qpair->reqq_start_iocbs)
1877 		qpair->reqq_start_iocbs(qpair);
1878 	else
1879 		qla2x00_start_iocbs(vha, qpair->req);
1880 }
1881 
1882 /*
1883  * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
1884  */
qlt_24xx_retry_term_exchange(struct scsi_qla_host * vha,struct qla_qpair * qpair,response_t * pkt,struct qla_tgt_mgmt_cmd * mcmd)1885 static void qlt_24xx_retry_term_exchange(struct scsi_qla_host *vha,
1886     struct qla_qpair *qpair, response_t *pkt, struct qla_tgt_mgmt_cmd *mcmd)
1887 {
1888 	struct ctio7_to_24xx *ctio;
1889 	u16 tmp;
1890 	struct abts_recv_from_24xx *entry;
1891 
1892 	ctio = (struct ctio7_to_24xx *)qla2x00_alloc_iocbs_ready(qpair, NULL);
1893 	if (ctio == NULL) {
1894 		ql_dbg(ql_dbg_tgt, vha, 0xe04b,
1895 		    "qla_target(%d): %s failed: unable to allocate "
1896 		    "request packet\n", vha->vp_idx, __func__);
1897 		return;
1898 	}
1899 
1900 	if (mcmd)
1901 		/* abts from remote port */
1902 		entry = &mcmd->orig_iocb.abts;
1903 	else
1904 		/* abts from this driver.  */
1905 		entry = (struct abts_recv_from_24xx *)pkt;
1906 
1907 	/*
1908 	 * We've got on entrance firmware's response on by us generated
1909 	 * ABTS response. So, in it ID fields are reversed.
1910 	 */
1911 
1912 	ctio->entry_type = CTIO_TYPE7;
1913 	ctio->entry_count = 1;
1914 	ctio->nport_handle = entry->nport_handle;
1915 	ctio->handle = QLA_TGT_SKIP_HANDLE |	CTIO_COMPLETION_HANDLE_MARK;
1916 	ctio->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
1917 	ctio->vp_index = vha->vp_idx;
1918 	ctio->exchange_addr = entry->exchange_addr_to_abort;
1919 	tmp = (CTIO7_FLAGS_STATUS_MODE_1 | CTIO7_FLAGS_TERMINATE);
1920 
1921 	if (mcmd) {
1922 		ctio->initiator_id = entry->fcp_hdr_le.s_id;
1923 
1924 		if (mcmd->flags & QLA24XX_MGMT_ABORT_IO_ATTR_VALID)
1925 			tmp |= (mcmd->abort_io_attr << 9);
1926 		else if (qpair->retry_term_cnt & 1)
1927 			tmp |= (0x4 << 9);
1928 	} else {
1929 		ctio->initiator_id = entry->fcp_hdr_le.d_id;
1930 
1931 		if (qpair->retry_term_cnt & 1)
1932 			tmp |= (0x4 << 9);
1933 	}
1934 	ctio->u.status1.flags = cpu_to_le16(tmp);
1935 	ctio->u.status1.ox_id = entry->fcp_hdr_le.ox_id;
1936 
1937 	ql_dbg(ql_dbg_tgt, vha, 0xe007,
1938 	    "Sending retry TERM EXCH CTIO7 flags %04xh oxid %04xh attr valid %x\n",
1939 	    le16_to_cpu(ctio->u.status1.flags),
1940 	    le16_to_cpu(ctio->u.status1.ox_id),
1941 	    (mcmd && mcmd->flags & QLA24XX_MGMT_ABORT_IO_ATTR_VALID) ? 1 : 0);
1942 
1943 	/* Memory Barrier */
1944 	wmb();
1945 	if (qpair->reqq_start_iocbs)
1946 		qpair->reqq_start_iocbs(qpair);
1947 	else
1948 		qla2x00_start_iocbs(vha, qpair->req);
1949 
1950 	if (mcmd)
1951 		qlt_build_abts_resp_iocb(mcmd);
1952 	else
1953 		qlt_24xx_send_abts_resp(qpair,
1954 		    (struct abts_recv_from_24xx *)entry, FCP_TMF_CMPL, true);
1955 
1956 }
1957 
1958 /* drop cmds for the given lun
1959  * XXX only looks for cmds on the port through which lun reset was recieved
1960  * XXX does not go through the list of other port (which may have cmds
1961  *     for the same lun)
1962  */
abort_cmds_for_lun(struct scsi_qla_host * vha,u64 lun,be_id_t s_id)1963 static void abort_cmds_for_lun(struct scsi_qla_host *vha, u64 lun, be_id_t s_id)
1964 {
1965 	struct qla_tgt_sess_op *op;
1966 	struct qla_tgt_cmd *cmd;
1967 	uint32_t key;
1968 	unsigned long flags;
1969 
1970 	key = sid_to_key(s_id);
1971 	spin_lock_irqsave(&vha->cmd_list_lock, flags);
1972 	list_for_each_entry(op, &vha->unknown_atio_list, cmd_list) {
1973 		uint32_t op_key;
1974 		u64 op_lun;
1975 
1976 		op_key = sid_to_key(op->atio.u.isp24.fcp_hdr.s_id);
1977 		op_lun = scsilun_to_int(
1978 			(struct scsi_lun *)&op->atio.u.isp24.fcp_cmnd.lun);
1979 		if (op_key == key && op_lun == lun)
1980 			op->aborted = true;
1981 	}
1982 
1983 	list_for_each_entry(cmd, &vha->qla_cmd_list, cmd_list) {
1984 		uint32_t cmd_key;
1985 		u64 cmd_lun;
1986 
1987 		cmd_key = sid_to_key(cmd->atio.u.isp24.fcp_hdr.s_id);
1988 		cmd_lun = scsilun_to_int(
1989 			(struct scsi_lun *)&cmd->atio.u.isp24.fcp_cmnd.lun);
1990 		if (cmd_key == key && cmd_lun == lun)
1991 			cmd->aborted = 1;
1992 	}
1993 	spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
1994 }
1995 
qlt_find_qphint(struct scsi_qla_host * vha,uint64_t unpacked_lun)1996 static struct qla_qpair_hint *qlt_find_qphint(struct scsi_qla_host *vha,
1997     uint64_t unpacked_lun)
1998 {
1999 	struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
2000 	struct qla_qpair_hint *h = NULL;
2001 
2002 	if (vha->flags.qpairs_available) {
2003 		h = btree_lookup64(&tgt->lun_qpair_map, unpacked_lun);
2004 		if (!h)
2005 			h = &tgt->qphints[0];
2006 	} else {
2007 		h = &tgt->qphints[0];
2008 	}
2009 
2010 	return h;
2011 }
2012 
qlt_do_tmr_work(struct work_struct * work)2013 static void qlt_do_tmr_work(struct work_struct *work)
2014 {
2015 	struct qla_tgt_mgmt_cmd *mcmd =
2016 		container_of(work, struct qla_tgt_mgmt_cmd, work);
2017 	struct qla_hw_data *ha = mcmd->vha->hw;
2018 	int rc;
2019 	uint32_t tag;
2020 	unsigned long flags;
2021 
2022 	switch (mcmd->tmr_func) {
2023 	case QLA_TGT_ABTS:
2024 		tag = le32_to_cpu(mcmd->orig_iocb.abts.exchange_addr_to_abort);
2025 		break;
2026 	default:
2027 		tag = 0;
2028 		break;
2029 	}
2030 
2031 	rc = ha->tgt.tgt_ops->handle_tmr(mcmd, mcmd->unpacked_lun,
2032 	    mcmd->tmr_func, tag);
2033 
2034 	if (rc != 0) {
2035 		spin_lock_irqsave(mcmd->qpair->qp_lock_ptr, flags);
2036 		switch (mcmd->tmr_func) {
2037 		case QLA_TGT_ABTS:
2038 			mcmd->fc_tm_rsp = FCP_TMF_REJECTED;
2039 			qlt_build_abts_resp_iocb(mcmd);
2040 			break;
2041 		case QLA_TGT_LUN_RESET:
2042 		case QLA_TGT_CLEAR_TS:
2043 		case QLA_TGT_ABORT_TS:
2044 		case QLA_TGT_CLEAR_ACA:
2045 		case QLA_TGT_TARGET_RESET:
2046 			qlt_send_busy(mcmd->qpair, &mcmd->orig_iocb.atio,
2047 			    qla_sam_status);
2048 			break;
2049 
2050 		case QLA_TGT_ABORT_ALL:
2051 		case QLA_TGT_NEXUS_LOSS_SESS:
2052 		case QLA_TGT_NEXUS_LOSS:
2053 			qlt_send_notify_ack(mcmd->qpair,
2054 			    &mcmd->orig_iocb.imm_ntfy, 0, 0, 0, 0, 0, 0);
2055 			break;
2056 		}
2057 		spin_unlock_irqrestore(mcmd->qpair->qp_lock_ptr, flags);
2058 
2059 		ql_dbg(ql_dbg_tgt_mgt, mcmd->vha, 0xf052,
2060 		    "qla_target(%d):  tgt_ops->handle_tmr() failed: %d\n",
2061 		    mcmd->vha->vp_idx, rc);
2062 		mempool_free(mcmd, qla_tgt_mgmt_cmd_mempool);
2063 	}
2064 }
2065 
2066 /* ha->hardware_lock supposed to be held on entry */
__qlt_24xx_handle_abts(struct scsi_qla_host * vha,struct abts_recv_from_24xx * abts,struct fc_port * sess)2067 static int __qlt_24xx_handle_abts(struct scsi_qla_host *vha,
2068 	struct abts_recv_from_24xx *abts, struct fc_port *sess)
2069 {
2070 	struct qla_hw_data *ha = vha->hw;
2071 	struct qla_tgt_mgmt_cmd *mcmd;
2072 	struct qla_qpair_hint *h = &vha->vha_tgt.qla_tgt->qphints[0];
2073 	struct qla_tgt_cmd *abort_cmd;
2074 
2075 	ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00f,
2076 	    "qla_target(%d): task abort (tag=%d)\n",
2077 	    vha->vp_idx, abts->exchange_addr_to_abort);
2078 
2079 	mcmd = mempool_alloc(qla_tgt_mgmt_cmd_mempool, GFP_ATOMIC);
2080 	if (mcmd == NULL) {
2081 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf051,
2082 		    "qla_target(%d): %s: Allocation of ABORT cmd failed",
2083 		    vha->vp_idx, __func__);
2084 		return -ENOMEM;
2085 	}
2086 	memset(mcmd, 0, sizeof(*mcmd));
2087 	mcmd->cmd_type = TYPE_TGT_TMCMD;
2088 	mcmd->sess = sess;
2089 	memcpy(&mcmd->orig_iocb.abts, abts, sizeof(mcmd->orig_iocb.abts));
2090 	mcmd->reset_count = ha->base_qpair->chip_reset;
2091 	mcmd->tmr_func = QLA_TGT_ABTS;
2092 	mcmd->qpair = h->qpair;
2093 	mcmd->vha = vha;
2094 
2095 	/*
2096 	 * LUN is looked up by target-core internally based on the passed
2097 	 * abts->exchange_addr_to_abort tag.
2098 	 */
2099 	mcmd->se_cmd.cpuid = h->cpuid;
2100 
2101 	abort_cmd = ha->tgt.tgt_ops->find_cmd_by_tag(sess,
2102 				le32_to_cpu(abts->exchange_addr_to_abort));
2103 	if (!abort_cmd) {
2104 		mempool_free(mcmd, qla_tgt_mgmt_cmd_mempool);
2105 		return -EIO;
2106 	}
2107 	mcmd->unpacked_lun = abort_cmd->se_cmd.orig_fe_lun;
2108 
2109 	if (abort_cmd->qpair) {
2110 		mcmd->qpair = abort_cmd->qpair;
2111 		mcmd->se_cmd.cpuid = abort_cmd->se_cmd.cpuid;
2112 		mcmd->abort_io_attr = abort_cmd->atio.u.isp24.attr;
2113 		mcmd->flags = QLA24XX_MGMT_ABORT_IO_ATTR_VALID;
2114 	}
2115 
2116 	INIT_WORK(&mcmd->work, qlt_do_tmr_work);
2117 	queue_work_on(mcmd->se_cmd.cpuid, qla_tgt_wq, &mcmd->work);
2118 
2119 	return 0;
2120 }
2121 
2122 /*
2123  * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
2124  */
qlt_24xx_handle_abts(struct scsi_qla_host * vha,struct abts_recv_from_24xx * abts)2125 static void qlt_24xx_handle_abts(struct scsi_qla_host *vha,
2126 	struct abts_recv_from_24xx *abts)
2127 {
2128 	struct qla_hw_data *ha = vha->hw;
2129 	struct fc_port *sess;
2130 	uint32_t tag = le32_to_cpu(abts->exchange_addr_to_abort);
2131 	be_id_t s_id;
2132 	int rc;
2133 	unsigned long flags;
2134 
2135 	if (le32_to_cpu(abts->fcp_hdr_le.parameter) & ABTS_PARAM_ABORT_SEQ) {
2136 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf053,
2137 		    "qla_target(%d): ABTS: Abort Sequence not "
2138 		    "supported\n", vha->vp_idx);
2139 		qlt_24xx_send_abts_resp(ha->base_qpair, abts, FCP_TMF_REJECTED,
2140 		    false);
2141 		return;
2142 	}
2143 
2144 	if (tag == ATIO_EXCHANGE_ADDRESS_UNKNOWN) {
2145 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf010,
2146 		    "qla_target(%d): ABTS: Unknown Exchange "
2147 		    "Address received\n", vha->vp_idx);
2148 		qlt_24xx_send_abts_resp(ha->base_qpair, abts, FCP_TMF_REJECTED,
2149 		    false);
2150 		return;
2151 	}
2152 
2153 	ql_dbg(ql_dbg_tgt_mgt, vha, 0xf011,
2154 	    "qla_target(%d): task abort (s_id=%x:%x:%x, "
2155 	    "tag=%d, param=%x)\n", vha->vp_idx, abts->fcp_hdr_le.s_id.domain,
2156 	    abts->fcp_hdr_le.s_id.area, abts->fcp_hdr_le.s_id.al_pa, tag,
2157 	    le32_to_cpu(abts->fcp_hdr_le.parameter));
2158 
2159 	s_id = le_id_to_be(abts->fcp_hdr_le.s_id);
2160 
2161 	spin_lock_irqsave(&ha->tgt.sess_lock, flags);
2162 	sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha, s_id);
2163 	if (!sess) {
2164 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf012,
2165 		    "qla_target(%d): task abort for non-existent session\n",
2166 		    vha->vp_idx);
2167 		spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
2168 
2169 		qlt_24xx_send_abts_resp(ha->base_qpair, abts, FCP_TMF_REJECTED,
2170 			    false);
2171 		return;
2172 	}
2173 	spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
2174 
2175 
2176 	if (sess->deleted) {
2177 		qlt_24xx_send_abts_resp(ha->base_qpair, abts, FCP_TMF_REJECTED,
2178 		    false);
2179 		return;
2180 	}
2181 
2182 	rc = __qlt_24xx_handle_abts(vha, abts, sess);
2183 	if (rc != 0) {
2184 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf054,
2185 		    "qla_target(%d): __qlt_24xx_handle_abts() failed: %d\n",
2186 		    vha->vp_idx, rc);
2187 		qlt_24xx_send_abts_resp(ha->base_qpair, abts, FCP_TMF_REJECTED,
2188 		    false);
2189 		return;
2190 	}
2191 }
2192 
2193 /*
2194  * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
2195  */
qlt_24xx_send_task_mgmt_ctio(struct qla_qpair * qpair,struct qla_tgt_mgmt_cmd * mcmd,uint32_t resp_code)2196 static void qlt_24xx_send_task_mgmt_ctio(struct qla_qpair *qpair,
2197 	struct qla_tgt_mgmt_cmd *mcmd, uint32_t resp_code)
2198 {
2199 	struct scsi_qla_host *ha = mcmd->vha;
2200 	struct atio_from_isp *atio = &mcmd->orig_iocb.atio;
2201 	struct ctio7_to_24xx *ctio;
2202 	uint16_t temp;
2203 
2204 	ql_dbg(ql_dbg_tgt, ha, 0xe008,
2205 	    "Sending task mgmt CTIO7 (ha=%p, atio=%p, resp_code=%x\n",
2206 	    ha, atio, resp_code);
2207 
2208 
2209 	ctio = (struct ctio7_to_24xx *)__qla2x00_alloc_iocbs(qpair, NULL);
2210 	if (ctio == NULL) {
2211 		ql_dbg(ql_dbg_tgt, ha, 0xe04c,
2212 		    "qla_target(%d): %s failed: unable to allocate "
2213 		    "request packet\n", ha->vp_idx, __func__);
2214 		return;
2215 	}
2216 
2217 	ctio->entry_type = CTIO_TYPE7;
2218 	ctio->entry_count = 1;
2219 	ctio->handle = QLA_TGT_SKIP_HANDLE | CTIO_COMPLETION_HANDLE_MARK;
2220 	ctio->nport_handle = cpu_to_le16(mcmd->sess->loop_id);
2221 	ctio->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
2222 	ctio->vp_index = ha->vp_idx;
2223 	ctio->initiator_id = be_id_to_le(atio->u.isp24.fcp_hdr.s_id);
2224 	ctio->exchange_addr = atio->u.isp24.exchange_addr;
2225 	temp = (atio->u.isp24.attr << 9)|
2226 		CTIO7_FLAGS_STATUS_MODE_1 | CTIO7_FLAGS_SEND_STATUS;
2227 	ctio->u.status1.flags = cpu_to_le16(temp);
2228 	temp = be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id);
2229 	ctio->u.status1.ox_id = cpu_to_le16(temp);
2230 	ctio->u.status1.scsi_status =
2231 	    cpu_to_le16(SS_RESPONSE_INFO_LEN_VALID);
2232 	ctio->u.status1.response_len = cpu_to_le16(8);
2233 	ctio->u.status1.sense_data[0] = resp_code;
2234 
2235 	/* Memory Barrier */
2236 	wmb();
2237 	if (qpair->reqq_start_iocbs)
2238 		qpair->reqq_start_iocbs(qpair);
2239 	else
2240 		qla2x00_start_iocbs(ha, qpair->req);
2241 }
2242 
qlt_free_mcmd(struct qla_tgt_mgmt_cmd * mcmd)2243 void qlt_free_mcmd(struct qla_tgt_mgmt_cmd *mcmd)
2244 {
2245 	mempool_free(mcmd, qla_tgt_mgmt_cmd_mempool);
2246 }
2247 EXPORT_SYMBOL(qlt_free_mcmd);
2248 
2249 /*
2250  * ha->hardware_lock supposed to be held on entry. Might drop it, then
2251  * reacquire
2252  */
qlt_send_resp_ctio(struct qla_qpair * qpair,struct qla_tgt_cmd * cmd,uint8_t scsi_status,uint8_t sense_key,uint8_t asc,uint8_t ascq)2253 void qlt_send_resp_ctio(struct qla_qpair *qpair, struct qla_tgt_cmd *cmd,
2254     uint8_t scsi_status, uint8_t sense_key, uint8_t asc, uint8_t ascq)
2255 {
2256 	struct atio_from_isp *atio = &cmd->atio;
2257 	struct ctio7_to_24xx *ctio;
2258 	uint16_t temp;
2259 	struct scsi_qla_host *vha = cmd->vha;
2260 
2261 	ql_dbg(ql_dbg_tgt_dif, vha, 0x3066,
2262 	    "Sending response CTIO7 (vha=%p, atio=%p, scsi_status=%02x, "
2263 	    "sense_key=%02x, asc=%02x, ascq=%02x",
2264 	    vha, atio, scsi_status, sense_key, asc, ascq);
2265 
2266 	ctio = (struct ctio7_to_24xx *)qla2x00_alloc_iocbs(vha, NULL);
2267 	if (!ctio) {
2268 		ql_dbg(ql_dbg_async, vha, 0x3067,
2269 		    "qla2x00t(%ld): %s failed: unable to allocate request packet",
2270 		    vha->host_no, __func__);
2271 		goto out;
2272 	}
2273 
2274 	ctio->entry_type = CTIO_TYPE7;
2275 	ctio->entry_count = 1;
2276 	ctio->handle = QLA_TGT_SKIP_HANDLE;
2277 	ctio->nport_handle = cpu_to_le16(cmd->sess->loop_id);
2278 	ctio->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
2279 	ctio->vp_index = vha->vp_idx;
2280 	ctio->initiator_id = be_id_to_le(atio->u.isp24.fcp_hdr.s_id);
2281 	ctio->exchange_addr = atio->u.isp24.exchange_addr;
2282 	temp = (atio->u.isp24.attr << 9) |
2283 	    CTIO7_FLAGS_STATUS_MODE_1 | CTIO7_FLAGS_SEND_STATUS;
2284 	ctio->u.status1.flags = cpu_to_le16(temp);
2285 	temp = be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id);
2286 	ctio->u.status1.ox_id = cpu_to_le16(temp);
2287 	ctio->u.status1.scsi_status =
2288 	    cpu_to_le16(SS_RESPONSE_INFO_LEN_VALID | scsi_status);
2289 	ctio->u.status1.response_len = cpu_to_le16(18);
2290 	ctio->u.status1.residual = cpu_to_le32(get_datalen_for_atio(atio));
2291 
2292 	if (ctio->u.status1.residual != 0)
2293 		ctio->u.status1.scsi_status |=
2294 		    cpu_to_le16(SS_RESIDUAL_UNDER);
2295 
2296 	/* Fixed format sense data. */
2297 	ctio->u.status1.sense_data[0] = 0x70;
2298 	ctio->u.status1.sense_data[2] = sense_key;
2299 	/* Additional sense length */
2300 	ctio->u.status1.sense_data[7] = 0xa;
2301 	/* ASC and ASCQ */
2302 	ctio->u.status1.sense_data[12] = asc;
2303 	ctio->u.status1.sense_data[13] = ascq;
2304 
2305 	/* Memory Barrier */
2306 	wmb();
2307 
2308 	if (qpair->reqq_start_iocbs)
2309 		qpair->reqq_start_iocbs(qpair);
2310 	else
2311 		qla2x00_start_iocbs(vha, qpair->req);
2312 
2313 out:
2314 	return;
2315 }
2316 
2317 /* callback from target fabric module code */
qlt_xmit_tm_rsp(struct qla_tgt_mgmt_cmd * mcmd)2318 void qlt_xmit_tm_rsp(struct qla_tgt_mgmt_cmd *mcmd)
2319 {
2320 	struct scsi_qla_host *vha = mcmd->sess->vha;
2321 	struct qla_hw_data *ha = vha->hw;
2322 	unsigned long flags;
2323 	struct qla_qpair *qpair = mcmd->qpair;
2324 	bool free_mcmd = true;
2325 
2326 	ql_dbg(ql_dbg_tgt_mgt, vha, 0xf013,
2327 	    "TM response mcmd (%p) status %#x state %#x",
2328 	    mcmd, mcmd->fc_tm_rsp, mcmd->flags);
2329 
2330 	spin_lock_irqsave(qpair->qp_lock_ptr, flags);
2331 
2332 	if (!vha->flags.online || mcmd->reset_count != qpair->chip_reset) {
2333 		/*
2334 		 * Either the port is not online or this request was from
2335 		 * previous life, just abort the processing.
2336 		 */
2337 		ql_dbg(ql_dbg_async, vha, 0xe100,
2338 			"RESET-TMR online/active/old-count/new-count = %d/%d/%d/%d.\n",
2339 			vha->flags.online, qla2x00_reset_active(vha),
2340 			mcmd->reset_count, qpair->chip_reset);
2341 		ha->tgt.tgt_ops->free_mcmd(mcmd);
2342 		spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
2343 		return;
2344 	}
2345 
2346 	if (mcmd->flags == QLA24XX_MGMT_SEND_NACK) {
2347 		switch (mcmd->orig_iocb.imm_ntfy.u.isp24.status_subcode) {
2348 		case ELS_LOGO:
2349 		case ELS_PRLO:
2350 		case ELS_TPRLO:
2351 			ql_dbg(ql_dbg_disc, vha, 0x2106,
2352 			    "TM response logo %8phC status %#x state %#x",
2353 			    mcmd->sess->port_name, mcmd->fc_tm_rsp,
2354 			    mcmd->flags);
2355 			qlt_schedule_sess_for_deletion(mcmd->sess);
2356 			break;
2357 		default:
2358 			qlt_send_notify_ack(vha->hw->base_qpair,
2359 			    &mcmd->orig_iocb.imm_ntfy, 0, 0, 0, 0, 0, 0);
2360 			break;
2361 		}
2362 	} else {
2363 		if (mcmd->orig_iocb.atio.u.raw.entry_type == ABTS_RECV_24XX) {
2364 			qlt_build_abts_resp_iocb(mcmd);
2365 			free_mcmd = false;
2366 		} else
2367 			qlt_24xx_send_task_mgmt_ctio(qpair, mcmd,
2368 			    mcmd->fc_tm_rsp);
2369 	}
2370 	/*
2371 	 * Make the callback for ->free_mcmd() to queue_work() and invoke
2372 	 * target_put_sess_cmd() to drop cmd_kref to 1.  The final
2373 	 * target_put_sess_cmd() call will be made from TFO->check_stop_free()
2374 	 * -> tcm_qla2xxx_check_stop_free() to release the TMR associated se_cmd
2375 	 * descriptor after TFO->queue_tm_rsp() -> tcm_qla2xxx_queue_tm_rsp() ->
2376 	 * qlt_xmit_tm_rsp() returns here..
2377 	 */
2378 	if (free_mcmd)
2379 		ha->tgt.tgt_ops->free_mcmd(mcmd);
2380 
2381 	spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
2382 }
2383 EXPORT_SYMBOL(qlt_xmit_tm_rsp);
2384 
2385 /* No locks */
qlt_pci_map_calc_cnt(struct qla_tgt_prm * prm)2386 static int qlt_pci_map_calc_cnt(struct qla_tgt_prm *prm)
2387 {
2388 	struct qla_tgt_cmd *cmd = prm->cmd;
2389 
2390 	BUG_ON(cmd->sg_cnt == 0);
2391 
2392 	prm->sg = (struct scatterlist *)cmd->sg;
2393 	prm->seg_cnt = dma_map_sg(&cmd->qpair->pdev->dev, cmd->sg,
2394 	    cmd->sg_cnt, cmd->dma_data_direction);
2395 	if (unlikely(prm->seg_cnt == 0))
2396 		goto out_err;
2397 
2398 	prm->cmd->sg_mapped = 1;
2399 
2400 	if (cmd->se_cmd.prot_op == TARGET_PROT_NORMAL) {
2401 		/*
2402 		 * If greater than four sg entries then we need to allocate
2403 		 * the continuation entries
2404 		 */
2405 		if (prm->seg_cnt > QLA_TGT_DATASEGS_PER_CMD_24XX)
2406 			prm->req_cnt += DIV_ROUND_UP(prm->seg_cnt -
2407 			QLA_TGT_DATASEGS_PER_CMD_24XX,
2408 			QLA_TGT_DATASEGS_PER_CONT_24XX);
2409 	} else {
2410 		/* DIF */
2411 		if ((cmd->se_cmd.prot_op == TARGET_PROT_DIN_INSERT) ||
2412 		    (cmd->se_cmd.prot_op == TARGET_PROT_DOUT_STRIP)) {
2413 			prm->seg_cnt = DIV_ROUND_UP(cmd->bufflen, cmd->blk_sz);
2414 			prm->tot_dsds = prm->seg_cnt;
2415 		} else
2416 			prm->tot_dsds = prm->seg_cnt;
2417 
2418 		if (cmd->prot_sg_cnt) {
2419 			prm->prot_sg      = cmd->prot_sg;
2420 			prm->prot_seg_cnt = dma_map_sg(&cmd->qpair->pdev->dev,
2421 				cmd->prot_sg, cmd->prot_sg_cnt,
2422 				cmd->dma_data_direction);
2423 			if (unlikely(prm->prot_seg_cnt == 0))
2424 				goto out_err;
2425 
2426 			if ((cmd->se_cmd.prot_op == TARGET_PROT_DIN_INSERT) ||
2427 			    (cmd->se_cmd.prot_op == TARGET_PROT_DOUT_STRIP)) {
2428 				/* Dif Bundling not support here */
2429 				prm->prot_seg_cnt = DIV_ROUND_UP(cmd->bufflen,
2430 								cmd->blk_sz);
2431 				prm->tot_dsds += prm->prot_seg_cnt;
2432 			} else
2433 				prm->tot_dsds += prm->prot_seg_cnt;
2434 		}
2435 	}
2436 
2437 	return 0;
2438 
2439 out_err:
2440 	ql_dbg_qp(ql_dbg_tgt, prm->cmd->qpair, 0xe04d,
2441 	    "qla_target(%d): PCI mapping failed: sg_cnt=%d",
2442 	    0, prm->cmd->sg_cnt);
2443 	return -1;
2444 }
2445 
qlt_unmap_sg(struct scsi_qla_host * vha,struct qla_tgt_cmd * cmd)2446 static void qlt_unmap_sg(struct scsi_qla_host *vha, struct qla_tgt_cmd *cmd)
2447 {
2448 	struct qla_hw_data *ha;
2449 	struct qla_qpair *qpair;
2450 
2451 	if (!cmd->sg_mapped)
2452 		return;
2453 
2454 	qpair = cmd->qpair;
2455 
2456 	dma_unmap_sg(&qpair->pdev->dev, cmd->sg, cmd->sg_cnt,
2457 	    cmd->dma_data_direction);
2458 	cmd->sg_mapped = 0;
2459 
2460 	if (cmd->prot_sg_cnt)
2461 		dma_unmap_sg(&qpair->pdev->dev, cmd->prot_sg, cmd->prot_sg_cnt,
2462 			cmd->dma_data_direction);
2463 
2464 	if (!cmd->ctx)
2465 		return;
2466 	ha = vha->hw;
2467 	if (cmd->ctx_dsd_alloced)
2468 		qla2x00_clean_dsd_pool(ha, cmd->ctx);
2469 
2470 	dma_pool_free(ha->dl_dma_pool, cmd->ctx, cmd->ctx->crc_ctx_dma);
2471 }
2472 
qlt_check_reserve_free_req(struct qla_qpair * qpair,uint32_t req_cnt)2473 static int qlt_check_reserve_free_req(struct qla_qpair *qpair,
2474 	uint32_t req_cnt)
2475 {
2476 	uint32_t cnt;
2477 	struct req_que *req = qpair->req;
2478 
2479 	if (req->cnt < (req_cnt + 2)) {
2480 		cnt = (uint16_t)(qpair->use_shadow_reg ? *req->out_ptr :
2481 		    rd_reg_dword_relaxed(req->req_q_out));
2482 
2483 		if  (req->ring_index < cnt)
2484 			req->cnt = cnt - req->ring_index;
2485 		else
2486 			req->cnt = req->length - (req->ring_index - cnt);
2487 
2488 		if (unlikely(req->cnt < (req_cnt + 2)))
2489 			return -EAGAIN;
2490 	}
2491 
2492 	req->cnt -= req_cnt;
2493 
2494 	return 0;
2495 }
2496 
2497 /*
2498  * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
2499  */
qlt_get_req_pkt(struct req_que * req)2500 static inline void *qlt_get_req_pkt(struct req_que *req)
2501 {
2502 	/* Adjust ring index. */
2503 	req->ring_index++;
2504 	if (req->ring_index == req->length) {
2505 		req->ring_index = 0;
2506 		req->ring_ptr = req->ring;
2507 	} else {
2508 		req->ring_ptr++;
2509 	}
2510 	return (cont_entry_t *)req->ring_ptr;
2511 }
2512 
2513 /* ha->hardware_lock supposed to be held on entry */
qlt_make_handle(struct qla_qpair * qpair)2514 static inline uint32_t qlt_make_handle(struct qla_qpair *qpair)
2515 {
2516 	uint32_t h;
2517 	int index;
2518 	uint8_t found = 0;
2519 	struct req_que *req = qpair->req;
2520 
2521 	h = req->current_outstanding_cmd;
2522 
2523 	for (index = 1; index < req->num_outstanding_cmds; index++) {
2524 		h++;
2525 		if (h == req->num_outstanding_cmds)
2526 			h = 1;
2527 
2528 		if (h == QLA_TGT_SKIP_HANDLE)
2529 			continue;
2530 
2531 		if (!req->outstanding_cmds[h]) {
2532 			found = 1;
2533 			break;
2534 		}
2535 	}
2536 
2537 	if (found) {
2538 		req->current_outstanding_cmd = h;
2539 	} else {
2540 		ql_dbg(ql_dbg_io, qpair->vha, 0x305b,
2541 		    "qla_target(%d): Ran out of empty cmd slots\n",
2542 		    qpair->vha->vp_idx);
2543 		h = QLA_TGT_NULL_HANDLE;
2544 	}
2545 
2546 	return h;
2547 }
2548 
2549 /* ha->hardware_lock supposed to be held on entry */
qlt_24xx_build_ctio_pkt(struct qla_qpair * qpair,struct qla_tgt_prm * prm)2550 static int qlt_24xx_build_ctio_pkt(struct qla_qpair *qpair,
2551 	struct qla_tgt_prm *prm)
2552 {
2553 	uint32_t h;
2554 	struct ctio7_to_24xx *pkt;
2555 	struct atio_from_isp *atio = &prm->cmd->atio;
2556 	uint16_t temp;
2557 	struct qla_tgt_cmd      *cmd = prm->cmd;
2558 
2559 	pkt = (struct ctio7_to_24xx *)qpair->req->ring_ptr;
2560 	prm->pkt = pkt;
2561 	memset(pkt, 0, sizeof(*pkt));
2562 
2563 	pkt->entry_type = CTIO_TYPE7;
2564 	pkt->entry_count = (uint8_t)prm->req_cnt;
2565 	pkt->vp_index = prm->cmd->vp_idx;
2566 
2567 	h = qlt_make_handle(qpair);
2568 	if (unlikely(h == QLA_TGT_NULL_HANDLE)) {
2569 		/*
2570 		 * CTIO type 7 from the firmware doesn't provide a way to
2571 		 * know the initiator's LOOP ID, hence we can't find
2572 		 * the session and, so, the command.
2573 		 */
2574 		return -EAGAIN;
2575 	} else
2576 		qpair->req->outstanding_cmds[h] = (srb_t *)prm->cmd;
2577 
2578 	pkt->handle = make_handle(qpair->req->id, h);
2579 	pkt->handle |= CTIO_COMPLETION_HANDLE_MARK;
2580 	pkt->nport_handle = cpu_to_le16(prm->cmd->loop_id);
2581 	pkt->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
2582 	pkt->initiator_id = be_id_to_le(atio->u.isp24.fcp_hdr.s_id);
2583 	pkt->exchange_addr = atio->u.isp24.exchange_addr;
2584 	temp = atio->u.isp24.attr << 9;
2585 	pkt->u.status0.flags |= cpu_to_le16(temp);
2586 	temp = be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id);
2587 	pkt->u.status0.ox_id = cpu_to_le16(temp);
2588 	pkt->u.status0.relative_offset = cpu_to_le32(prm->cmd->offset);
2589 
2590 	if (cmd->edif) {
2591 		if (cmd->dma_data_direction == DMA_TO_DEVICE)
2592 			prm->cmd->sess->edif.rx_bytes += cmd->bufflen;
2593 		if (cmd->dma_data_direction == DMA_FROM_DEVICE)
2594 			prm->cmd->sess->edif.tx_bytes += cmd->bufflen;
2595 
2596 		pkt->u.status0.edif_flags |= EF_EN_EDIF;
2597 	}
2598 
2599 	return 0;
2600 }
2601 
2602 /*
2603  * ha->hardware_lock supposed to be held on entry. We have already made sure
2604  * that there is sufficient amount of request entries to not drop it.
2605  */
qlt_load_cont_data_segments(struct qla_tgt_prm * prm)2606 static void qlt_load_cont_data_segments(struct qla_tgt_prm *prm)
2607 {
2608 	int cnt;
2609 	struct dsd64 *cur_dsd;
2610 
2611 	/* Build continuation packets */
2612 	while (prm->seg_cnt > 0) {
2613 		cont_a64_entry_t *cont_pkt64 =
2614 			(cont_a64_entry_t *)qlt_get_req_pkt(
2615 			   prm->cmd->qpair->req);
2616 
2617 		/*
2618 		 * Make sure that from cont_pkt64 none of
2619 		 * 64-bit specific fields used for 32-bit
2620 		 * addressing. Cast to (cont_entry_t *) for
2621 		 * that.
2622 		 */
2623 
2624 		memset(cont_pkt64, 0, sizeof(*cont_pkt64));
2625 
2626 		cont_pkt64->entry_count = 1;
2627 		cont_pkt64->sys_define = 0;
2628 
2629 		cont_pkt64->entry_type = CONTINUE_A64_TYPE;
2630 		cur_dsd = cont_pkt64->dsd;
2631 
2632 		/* Load continuation entry data segments */
2633 		for (cnt = 0;
2634 		    cnt < QLA_TGT_DATASEGS_PER_CONT_24XX && prm->seg_cnt;
2635 		    cnt++, prm->seg_cnt--) {
2636 			append_dsd64(&cur_dsd, prm->sg);
2637 			prm->sg = sg_next(prm->sg);
2638 		}
2639 	}
2640 }
2641 
2642 /*
2643  * ha->hardware_lock supposed to be held on entry. We have already made sure
2644  * that there is sufficient amount of request entries to not drop it.
2645  */
qlt_load_data_segments(struct qla_tgt_prm * prm)2646 static void qlt_load_data_segments(struct qla_tgt_prm *prm)
2647 {
2648 	int cnt;
2649 	struct dsd64 *cur_dsd;
2650 	struct ctio7_to_24xx *pkt24 = (struct ctio7_to_24xx *)prm->pkt;
2651 
2652 	pkt24->u.status0.transfer_length = cpu_to_le32(prm->cmd->bufflen);
2653 
2654 	/* Setup packet address segment pointer */
2655 	cur_dsd = &pkt24->u.status0.dsd;
2656 
2657 	/* Set total data segment count */
2658 	if (prm->seg_cnt)
2659 		pkt24->dseg_count = cpu_to_le16(prm->seg_cnt);
2660 
2661 	if (prm->seg_cnt == 0) {
2662 		/* No data transfer */
2663 		cur_dsd->address = 0;
2664 		cur_dsd->length = 0;
2665 		return;
2666 	}
2667 
2668 	/* If scatter gather */
2669 
2670 	/* Load command entry data segments */
2671 	for (cnt = 0;
2672 	    (cnt < QLA_TGT_DATASEGS_PER_CMD_24XX) && prm->seg_cnt;
2673 	    cnt++, prm->seg_cnt--) {
2674 		append_dsd64(&cur_dsd, prm->sg);
2675 		prm->sg = sg_next(prm->sg);
2676 	}
2677 
2678 	qlt_load_cont_data_segments(prm);
2679 }
2680 
qlt_has_data(struct qla_tgt_cmd * cmd)2681 static inline int qlt_has_data(struct qla_tgt_cmd *cmd)
2682 {
2683 	return cmd->bufflen > 0;
2684 }
2685 
qlt_print_dif_err(struct qla_tgt_prm * prm)2686 static void qlt_print_dif_err(struct qla_tgt_prm *prm)
2687 {
2688 	struct qla_tgt_cmd *cmd;
2689 	struct scsi_qla_host *vha;
2690 
2691 	/* asc 0x10=dif error */
2692 	if (prm->sense_buffer && (prm->sense_buffer[12] == 0x10)) {
2693 		cmd = prm->cmd;
2694 		vha = cmd->vha;
2695 		/* ASCQ */
2696 		switch (prm->sense_buffer[13]) {
2697 		case 1:
2698 			ql_dbg(ql_dbg_tgt_dif, vha, 0xe00b,
2699 			    "BE detected Guard TAG ERR: lba[0x%llx|%lld] len[0x%x] "
2700 			    "se_cmd=%p tag[%x]",
2701 			    cmd->lba, cmd->lba, cmd->num_blks, &cmd->se_cmd,
2702 			    cmd->atio.u.isp24.exchange_addr);
2703 			break;
2704 		case 2:
2705 			ql_dbg(ql_dbg_tgt_dif, vha, 0xe00c,
2706 			    "BE detected APP TAG ERR: lba[0x%llx|%lld] len[0x%x] "
2707 			    "se_cmd=%p tag[%x]",
2708 			    cmd->lba, cmd->lba, cmd->num_blks, &cmd->se_cmd,
2709 			    cmd->atio.u.isp24.exchange_addr);
2710 			break;
2711 		case 3:
2712 			ql_dbg(ql_dbg_tgt_dif, vha, 0xe00f,
2713 			    "BE detected REF TAG ERR: lba[0x%llx|%lld] len[0x%x] "
2714 			    "se_cmd=%p tag[%x]",
2715 			    cmd->lba, cmd->lba, cmd->num_blks, &cmd->se_cmd,
2716 			    cmd->atio.u.isp24.exchange_addr);
2717 			break;
2718 		default:
2719 			ql_dbg(ql_dbg_tgt_dif, vha, 0xe010,
2720 			    "BE detected Dif ERR: lba[%llx|%lld] len[%x] "
2721 			    "se_cmd=%p tag[%x]",
2722 			    cmd->lba, cmd->lba, cmd->num_blks, &cmd->se_cmd,
2723 			    cmd->atio.u.isp24.exchange_addr);
2724 			break;
2725 		}
2726 		ql_dump_buffer(ql_dbg_tgt_dif, vha, 0xe011, cmd->cdb, 16);
2727 	}
2728 }
2729 
2730 /*
2731  * Called without ha->hardware_lock held
2732  */
qlt_pre_xmit_response(struct qla_tgt_cmd * cmd,struct qla_tgt_prm * prm,int xmit_type,uint8_t scsi_status,uint32_t * full_req_cnt)2733 static int qlt_pre_xmit_response(struct qla_tgt_cmd *cmd,
2734 	struct qla_tgt_prm *prm, int xmit_type, uint8_t scsi_status,
2735 	uint32_t *full_req_cnt)
2736 {
2737 	struct se_cmd *se_cmd = &cmd->se_cmd;
2738 	struct qla_qpair *qpair = cmd->qpair;
2739 
2740 	prm->cmd = cmd;
2741 	prm->tgt = cmd->tgt;
2742 	prm->pkt = NULL;
2743 	prm->rq_result = scsi_status;
2744 	prm->sense_buffer = &cmd->sense_buffer[0];
2745 	prm->sense_buffer_len = TRANSPORT_SENSE_BUFFER;
2746 	prm->sg = NULL;
2747 	prm->seg_cnt = -1;
2748 	prm->req_cnt = 1;
2749 	prm->residual = 0;
2750 	prm->add_status_pkt = 0;
2751 	prm->prot_sg = NULL;
2752 	prm->prot_seg_cnt = 0;
2753 	prm->tot_dsds = 0;
2754 
2755 	if ((xmit_type & QLA_TGT_XMIT_DATA) && qlt_has_data(cmd)) {
2756 		if  (qlt_pci_map_calc_cnt(prm) != 0)
2757 			return -EAGAIN;
2758 	}
2759 
2760 	*full_req_cnt = prm->req_cnt;
2761 
2762 	if (se_cmd->se_cmd_flags & SCF_UNDERFLOW_BIT) {
2763 		prm->residual = se_cmd->residual_count;
2764 		ql_dbg_qp(ql_dbg_io + ql_dbg_verbose, qpair, 0x305c,
2765 		    "Residual underflow: %d (tag %lld, op %x, bufflen %d, rq_result %x)\n",
2766 		       prm->residual, se_cmd->tag,
2767 		       se_cmd->t_task_cdb ? se_cmd->t_task_cdb[0] : 0,
2768 		       cmd->bufflen, prm->rq_result);
2769 		prm->rq_result |= SS_RESIDUAL_UNDER;
2770 	} else if (se_cmd->se_cmd_flags & SCF_OVERFLOW_BIT) {
2771 		prm->residual = se_cmd->residual_count;
2772 		ql_dbg_qp(ql_dbg_io, qpair, 0x305d,
2773 		    "Residual overflow: %d (tag %lld, op %x, bufflen %d, rq_result %x)\n",
2774 		       prm->residual, se_cmd->tag, se_cmd->t_task_cdb ?
2775 		       se_cmd->t_task_cdb[0] : 0, cmd->bufflen, prm->rq_result);
2776 		prm->rq_result |= SS_RESIDUAL_OVER;
2777 	}
2778 
2779 	if (xmit_type & QLA_TGT_XMIT_STATUS) {
2780 		/*
2781 		 * If QLA_TGT_XMIT_DATA is not set, add_status_pkt will be
2782 		 * ignored in *xmit_response() below
2783 		 */
2784 		if (qlt_has_data(cmd)) {
2785 			if (QLA_TGT_SENSE_VALID(prm->sense_buffer) ||
2786 			    (IS_FWI2_CAPABLE(cmd->vha->hw) &&
2787 			    (prm->rq_result != 0))) {
2788 				prm->add_status_pkt = 1;
2789 				(*full_req_cnt)++;
2790 			}
2791 		}
2792 	}
2793 
2794 	return 0;
2795 }
2796 
qlt_need_explicit_conf(struct qla_tgt_cmd * cmd,int sending_sense)2797 static inline int qlt_need_explicit_conf(struct qla_tgt_cmd *cmd,
2798     int sending_sense)
2799 {
2800 	if (cmd->qpair->enable_class_2)
2801 		return 0;
2802 
2803 	if (sending_sense)
2804 		return cmd->conf_compl_supported;
2805 	else
2806 		return cmd->qpair->enable_explicit_conf &&
2807                     cmd->conf_compl_supported;
2808 }
2809 
qlt_24xx_init_ctio_to_isp(struct ctio7_to_24xx * ctio,struct qla_tgt_prm * prm)2810 static void qlt_24xx_init_ctio_to_isp(struct ctio7_to_24xx *ctio,
2811 	struct qla_tgt_prm *prm)
2812 {
2813 	prm->sense_buffer_len = min_t(uint32_t, prm->sense_buffer_len,
2814 	    (uint32_t)sizeof(ctio->u.status1.sense_data));
2815 	ctio->u.status0.flags |= cpu_to_le16(CTIO7_FLAGS_SEND_STATUS);
2816 	if (qlt_need_explicit_conf(prm->cmd, 0)) {
2817 		ctio->u.status0.flags |= cpu_to_le16(
2818 		    CTIO7_FLAGS_EXPLICIT_CONFORM |
2819 		    CTIO7_FLAGS_CONFORM_REQ);
2820 	}
2821 	ctio->u.status0.residual = cpu_to_le32(prm->residual);
2822 	ctio->u.status0.scsi_status = cpu_to_le16(prm->rq_result);
2823 	if (QLA_TGT_SENSE_VALID(prm->sense_buffer)) {
2824 		int i;
2825 
2826 		if (qlt_need_explicit_conf(prm->cmd, 1)) {
2827 			if ((prm->rq_result & SS_SCSI_STATUS_BYTE) != 0) {
2828 				ql_dbg_qp(ql_dbg_tgt, prm->cmd->qpair, 0xe017,
2829 				    "Skipping EXPLICIT_CONFORM and "
2830 				    "CTIO7_FLAGS_CONFORM_REQ for FCP READ w/ "
2831 				    "non GOOD status\n");
2832 				goto skip_explict_conf;
2833 			}
2834 			ctio->u.status1.flags |= cpu_to_le16(
2835 			    CTIO7_FLAGS_EXPLICIT_CONFORM |
2836 			    CTIO7_FLAGS_CONFORM_REQ);
2837 		}
2838 skip_explict_conf:
2839 		ctio->u.status1.flags &=
2840 		    ~cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_0);
2841 		ctio->u.status1.flags |=
2842 		    cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_1);
2843 		ctio->u.status1.scsi_status |=
2844 		    cpu_to_le16(SS_SENSE_LEN_VALID);
2845 		ctio->u.status1.sense_length =
2846 		    cpu_to_le16(prm->sense_buffer_len);
2847 		for (i = 0; i < prm->sense_buffer_len/4; i++) {
2848 			uint32_t v;
2849 
2850 			v = get_unaligned_be32(
2851 					&((uint32_t *)prm->sense_buffer)[i]);
2852 			put_unaligned_le32(v,
2853 				&((uint32_t *)ctio->u.status1.sense_data)[i]);
2854 		}
2855 		qlt_print_dif_err(prm);
2856 
2857 	} else {
2858 		ctio->u.status1.flags &=
2859 		    ~cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_0);
2860 		ctio->u.status1.flags |=
2861 		    cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_1);
2862 		ctio->u.status1.sense_length = 0;
2863 		memset(ctio->u.status1.sense_data, 0,
2864 		    sizeof(ctio->u.status1.sense_data));
2865 	}
2866 
2867 	/* Sense with len > 24, is it possible ??? */
2868 }
2869 
2870 static inline int
qlt_hba_err_chk_enabled(struct se_cmd * se_cmd)2871 qlt_hba_err_chk_enabled(struct se_cmd *se_cmd)
2872 {
2873 	switch (se_cmd->prot_op) {
2874 	case TARGET_PROT_DOUT_INSERT:
2875 	case TARGET_PROT_DIN_STRIP:
2876 		if (ql2xenablehba_err_chk >= 1)
2877 			return 1;
2878 		break;
2879 	case TARGET_PROT_DOUT_PASS:
2880 	case TARGET_PROT_DIN_PASS:
2881 		if (ql2xenablehba_err_chk >= 2)
2882 			return 1;
2883 		break;
2884 	case TARGET_PROT_DIN_INSERT:
2885 	case TARGET_PROT_DOUT_STRIP:
2886 		return 1;
2887 	default:
2888 		break;
2889 	}
2890 	return 0;
2891 }
2892 
2893 static inline int
qla_tgt_ref_mask_check(struct se_cmd * se_cmd)2894 qla_tgt_ref_mask_check(struct se_cmd *se_cmd)
2895 {
2896 	switch (se_cmd->prot_op) {
2897 	case TARGET_PROT_DIN_INSERT:
2898 	case TARGET_PROT_DOUT_INSERT:
2899 	case TARGET_PROT_DIN_STRIP:
2900 	case TARGET_PROT_DOUT_STRIP:
2901 	case TARGET_PROT_DIN_PASS:
2902 	case TARGET_PROT_DOUT_PASS:
2903 	    return 1;
2904 	default:
2905 	    return 0;
2906 	}
2907 	return 0;
2908 }
2909 
2910 /*
2911  * qla_tgt_set_dif_tags - Extract Ref and App tags from SCSI command
2912  */
2913 static void
qla_tgt_set_dif_tags(struct qla_tgt_cmd * cmd,struct crc_context * ctx,uint16_t * pfw_prot_opts)2914 qla_tgt_set_dif_tags(struct qla_tgt_cmd *cmd, struct crc_context *ctx,
2915     uint16_t *pfw_prot_opts)
2916 {
2917 	struct se_cmd *se_cmd = &cmd->se_cmd;
2918 	uint32_t lba = 0xffffffff & se_cmd->t_task_lba;
2919 	scsi_qla_host_t *vha = cmd->tgt->vha;
2920 	struct qla_hw_data *ha = vha->hw;
2921 	uint32_t t32 = 0;
2922 
2923 	/*
2924 	 * wait till Mode Sense/Select cmd, modepage Ah, subpage 2
2925 	 * have been immplemented by TCM, before AppTag is avail.
2926 	 * Look for modesense_handlers[]
2927 	 */
2928 	ctx->app_tag = 0;
2929 	ctx->app_tag_mask[0] = 0x0;
2930 	ctx->app_tag_mask[1] = 0x0;
2931 
2932 	if (IS_PI_UNINIT_CAPABLE(ha)) {
2933 		if ((se_cmd->prot_type == TARGET_DIF_TYPE1_PROT) ||
2934 		    (se_cmd->prot_type == TARGET_DIF_TYPE2_PROT))
2935 			*pfw_prot_opts |= PO_DIS_VALD_APP_ESC;
2936 		else if (se_cmd->prot_type == TARGET_DIF_TYPE3_PROT)
2937 			*pfw_prot_opts |= PO_DIS_VALD_APP_REF_ESC;
2938 	}
2939 
2940 	t32 = ha->tgt.tgt_ops->get_dif_tags(cmd, pfw_prot_opts);
2941 
2942 	switch (se_cmd->prot_type) {
2943 	case TARGET_DIF_TYPE0_PROT:
2944 		/*
2945 		 * No check for ql2xenablehba_err_chk, as it
2946 		 * would be an I/O error if hba tag generation
2947 		 * is not done.
2948 		 */
2949 		ctx->ref_tag = cpu_to_le32(lba);
2950 		/* enable ALL bytes of the ref tag */
2951 		ctx->ref_tag_mask[0] = 0xff;
2952 		ctx->ref_tag_mask[1] = 0xff;
2953 		ctx->ref_tag_mask[2] = 0xff;
2954 		ctx->ref_tag_mask[3] = 0xff;
2955 		break;
2956 	case TARGET_DIF_TYPE1_PROT:
2957 	    /*
2958 	     * For TYPE 1 protection: 16 bit GUARD tag, 32 bit
2959 	     * REF tag, and 16 bit app tag.
2960 	     */
2961 	    ctx->ref_tag = cpu_to_le32(lba);
2962 	    if (!qla_tgt_ref_mask_check(se_cmd) ||
2963 		!(ha->tgt.tgt_ops->chk_dif_tags(t32))) {
2964 		    *pfw_prot_opts |= PO_DIS_REF_TAG_VALD;
2965 		    break;
2966 	    }
2967 	    /* enable ALL bytes of the ref tag */
2968 	    ctx->ref_tag_mask[0] = 0xff;
2969 	    ctx->ref_tag_mask[1] = 0xff;
2970 	    ctx->ref_tag_mask[2] = 0xff;
2971 	    ctx->ref_tag_mask[3] = 0xff;
2972 	    break;
2973 	case TARGET_DIF_TYPE2_PROT:
2974 	    /*
2975 	     * For TYPE 2 protection: 16 bit GUARD + 32 bit REF
2976 	     * tag has to match LBA in CDB + N
2977 	     */
2978 	    ctx->ref_tag = cpu_to_le32(lba);
2979 	    if (!qla_tgt_ref_mask_check(se_cmd) ||
2980 		!(ha->tgt.tgt_ops->chk_dif_tags(t32))) {
2981 		    *pfw_prot_opts |= PO_DIS_REF_TAG_VALD;
2982 		    break;
2983 	    }
2984 	    /* enable ALL bytes of the ref tag */
2985 	    ctx->ref_tag_mask[0] = 0xff;
2986 	    ctx->ref_tag_mask[1] = 0xff;
2987 	    ctx->ref_tag_mask[2] = 0xff;
2988 	    ctx->ref_tag_mask[3] = 0xff;
2989 	    break;
2990 	case TARGET_DIF_TYPE3_PROT:
2991 	    /* For TYPE 3 protection: 16 bit GUARD only */
2992 	    *pfw_prot_opts |= PO_DIS_REF_TAG_VALD;
2993 	    ctx->ref_tag_mask[0] = ctx->ref_tag_mask[1] =
2994 		ctx->ref_tag_mask[2] = ctx->ref_tag_mask[3] = 0x00;
2995 	    break;
2996 	}
2997 }
2998 
2999 static inline int
qlt_build_ctio_crc2_pkt(struct qla_qpair * qpair,struct qla_tgt_prm * prm)3000 qlt_build_ctio_crc2_pkt(struct qla_qpair *qpair, struct qla_tgt_prm *prm)
3001 {
3002 	struct dsd64		*cur_dsd;
3003 	uint32_t		transfer_length = 0;
3004 	uint32_t		data_bytes;
3005 	uint32_t		dif_bytes;
3006 	uint8_t			bundling = 1;
3007 	struct crc_context	*crc_ctx_pkt = NULL;
3008 	struct qla_hw_data	*ha;
3009 	struct ctio_crc2_to_fw	*pkt;
3010 	dma_addr_t		crc_ctx_dma;
3011 	uint16_t		fw_prot_opts = 0;
3012 	struct qla_tgt_cmd	*cmd = prm->cmd;
3013 	struct se_cmd		*se_cmd = &cmd->se_cmd;
3014 	uint32_t h;
3015 	struct atio_from_isp *atio = &prm->cmd->atio;
3016 	struct qla_tc_param	tc;
3017 	uint16_t t16;
3018 	scsi_qla_host_t *vha = cmd->vha;
3019 
3020 	ha = vha->hw;
3021 
3022 	pkt = (struct ctio_crc2_to_fw *)qpair->req->ring_ptr;
3023 	prm->pkt = pkt;
3024 	memset(pkt, 0, sizeof(*pkt));
3025 
3026 	ql_dbg_qp(ql_dbg_tgt, cmd->qpair, 0xe071,
3027 		"qla_target(%d):%s: se_cmd[%p] CRC2 prot_op[0x%x] cmd prot sg:cnt[%p:%x] lba[%llu]\n",
3028 		cmd->vp_idx, __func__, se_cmd, se_cmd->prot_op,
3029 		prm->prot_sg, prm->prot_seg_cnt, se_cmd->t_task_lba);
3030 
3031 	if ((se_cmd->prot_op == TARGET_PROT_DIN_INSERT) ||
3032 	    (se_cmd->prot_op == TARGET_PROT_DOUT_STRIP))
3033 		bundling = 0;
3034 
3035 	/* Compute dif len and adjust data len to incude protection */
3036 	data_bytes = cmd->bufflen;
3037 	dif_bytes  = (data_bytes / cmd->blk_sz) * 8;
3038 
3039 	switch (se_cmd->prot_op) {
3040 	case TARGET_PROT_DIN_INSERT:
3041 	case TARGET_PROT_DOUT_STRIP:
3042 		transfer_length = data_bytes;
3043 		if (cmd->prot_sg_cnt)
3044 			data_bytes += dif_bytes;
3045 		break;
3046 	case TARGET_PROT_DIN_STRIP:
3047 	case TARGET_PROT_DOUT_INSERT:
3048 	case TARGET_PROT_DIN_PASS:
3049 	case TARGET_PROT_DOUT_PASS:
3050 		transfer_length = data_bytes + dif_bytes;
3051 		break;
3052 	default:
3053 		BUG();
3054 		break;
3055 	}
3056 
3057 	if (!qlt_hba_err_chk_enabled(se_cmd))
3058 		fw_prot_opts |= 0x10; /* Disable Guard tag checking */
3059 	/* HBA error checking enabled */
3060 	else if (IS_PI_UNINIT_CAPABLE(ha)) {
3061 		if ((se_cmd->prot_type == TARGET_DIF_TYPE1_PROT) ||
3062 		    (se_cmd->prot_type == TARGET_DIF_TYPE2_PROT))
3063 			fw_prot_opts |= PO_DIS_VALD_APP_ESC;
3064 		else if (se_cmd->prot_type == TARGET_DIF_TYPE3_PROT)
3065 			fw_prot_opts |= PO_DIS_VALD_APP_REF_ESC;
3066 	}
3067 
3068 	switch (se_cmd->prot_op) {
3069 	case TARGET_PROT_DIN_INSERT:
3070 	case TARGET_PROT_DOUT_INSERT:
3071 		fw_prot_opts |= PO_MODE_DIF_INSERT;
3072 		break;
3073 	case TARGET_PROT_DIN_STRIP:
3074 	case TARGET_PROT_DOUT_STRIP:
3075 		fw_prot_opts |= PO_MODE_DIF_REMOVE;
3076 		break;
3077 	case TARGET_PROT_DIN_PASS:
3078 	case TARGET_PROT_DOUT_PASS:
3079 		fw_prot_opts |= PO_MODE_DIF_PASS;
3080 		/* FUTURE: does tcm require T10CRC<->IPCKSUM conversion? */
3081 		break;
3082 	default:/* Normal Request */
3083 		fw_prot_opts |= PO_MODE_DIF_PASS;
3084 		break;
3085 	}
3086 
3087 	/* ---- PKT ---- */
3088 	/* Update entry type to indicate Command Type CRC_2 IOCB */
3089 	pkt->entry_type  = CTIO_CRC2;
3090 	pkt->entry_count = 1;
3091 	pkt->vp_index = cmd->vp_idx;
3092 
3093 	h = qlt_make_handle(qpair);
3094 	if (unlikely(h == QLA_TGT_NULL_HANDLE)) {
3095 		/*
3096 		 * CTIO type 7 from the firmware doesn't provide a way to
3097 		 * know the initiator's LOOP ID, hence we can't find
3098 		 * the session and, so, the command.
3099 		 */
3100 		return -EAGAIN;
3101 	} else
3102 		qpair->req->outstanding_cmds[h] = (srb_t *)prm->cmd;
3103 
3104 	pkt->handle  = make_handle(qpair->req->id, h);
3105 	pkt->handle |= CTIO_COMPLETION_HANDLE_MARK;
3106 	pkt->nport_handle = cpu_to_le16(prm->cmd->loop_id);
3107 	pkt->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
3108 	pkt->initiator_id = be_id_to_le(atio->u.isp24.fcp_hdr.s_id);
3109 	pkt->exchange_addr   = atio->u.isp24.exchange_addr;
3110 
3111 	/* silence compile warning */
3112 	t16 = be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id);
3113 	pkt->ox_id  = cpu_to_le16(t16);
3114 
3115 	t16 = (atio->u.isp24.attr << 9);
3116 	pkt->flags |= cpu_to_le16(t16);
3117 	pkt->relative_offset = cpu_to_le32(prm->cmd->offset);
3118 
3119 	/* Set transfer direction */
3120 	if (cmd->dma_data_direction == DMA_TO_DEVICE)
3121 		pkt->flags = cpu_to_le16(CTIO7_FLAGS_DATA_IN);
3122 	else if (cmd->dma_data_direction == DMA_FROM_DEVICE)
3123 		pkt->flags = cpu_to_le16(CTIO7_FLAGS_DATA_OUT);
3124 
3125 	pkt->dseg_count = cpu_to_le16(prm->tot_dsds);
3126 	/* Fibre channel byte count */
3127 	pkt->transfer_length = cpu_to_le32(transfer_length);
3128 
3129 	/* ----- CRC context -------- */
3130 
3131 	/* Allocate CRC context from global pool */
3132 	crc_ctx_pkt = cmd->ctx =
3133 	    dma_pool_zalloc(ha->dl_dma_pool, GFP_ATOMIC, &crc_ctx_dma);
3134 
3135 	if (!crc_ctx_pkt)
3136 		goto crc_queuing_error;
3137 
3138 	crc_ctx_pkt->crc_ctx_dma = crc_ctx_dma;
3139 	INIT_LIST_HEAD(&crc_ctx_pkt->dsd_list);
3140 
3141 	/* Set handle */
3142 	crc_ctx_pkt->handle = pkt->handle;
3143 
3144 	qla_tgt_set_dif_tags(cmd, crc_ctx_pkt, &fw_prot_opts);
3145 
3146 	put_unaligned_le64(crc_ctx_dma, &pkt->crc_context_address);
3147 	pkt->crc_context_len = cpu_to_le16(CRC_CONTEXT_LEN_FW);
3148 
3149 	if (!bundling) {
3150 		cur_dsd = &crc_ctx_pkt->u.nobundling.data_dsd[0];
3151 	} else {
3152 		/*
3153 		 * Configure Bundling if we need to fetch interlaving
3154 		 * protection PCI accesses
3155 		 */
3156 		fw_prot_opts |= PO_ENABLE_DIF_BUNDLING;
3157 		crc_ctx_pkt->u.bundling.dif_byte_count = cpu_to_le32(dif_bytes);
3158 		crc_ctx_pkt->u.bundling.dseg_count =
3159 			cpu_to_le16(prm->tot_dsds - prm->prot_seg_cnt);
3160 		cur_dsd = &crc_ctx_pkt->u.bundling.data_dsd[0];
3161 	}
3162 
3163 	/* Finish the common fields of CRC pkt */
3164 	crc_ctx_pkt->blk_size   = cpu_to_le16(cmd->blk_sz);
3165 	crc_ctx_pkt->prot_opts  = cpu_to_le16(fw_prot_opts);
3166 	crc_ctx_pkt->byte_count = cpu_to_le32(data_bytes);
3167 	crc_ctx_pkt->guard_seed = cpu_to_le16(0);
3168 
3169 	memset((uint8_t *)&tc, 0 , sizeof(tc));
3170 	tc.vha = vha;
3171 	tc.blk_sz = cmd->blk_sz;
3172 	tc.bufflen = cmd->bufflen;
3173 	tc.sg = cmd->sg;
3174 	tc.prot_sg = cmd->prot_sg;
3175 	tc.ctx = crc_ctx_pkt;
3176 	tc.ctx_dsd_alloced = &cmd->ctx_dsd_alloced;
3177 
3178 	/* Walks data segments */
3179 	pkt->flags |= cpu_to_le16(CTIO7_FLAGS_DSD_PTR);
3180 
3181 	if (!bundling && prm->prot_seg_cnt) {
3182 		if (qla24xx_walk_and_build_sglist_no_difb(ha, NULL, cur_dsd,
3183 			prm->tot_dsds, &tc))
3184 			goto crc_queuing_error;
3185 	} else if (qla24xx_walk_and_build_sglist(ha, NULL, cur_dsd,
3186 		(prm->tot_dsds - prm->prot_seg_cnt), &tc))
3187 		goto crc_queuing_error;
3188 
3189 	if (bundling && prm->prot_seg_cnt) {
3190 		/* Walks dif segments */
3191 		pkt->add_flags |= CTIO_CRC2_AF_DIF_DSD_ENA;
3192 
3193 		cur_dsd = &crc_ctx_pkt->u.bundling.dif_dsd;
3194 		if (qla24xx_walk_and_build_prot_sglist(ha, NULL, cur_dsd,
3195 			prm->prot_seg_cnt, cmd))
3196 			goto crc_queuing_error;
3197 	}
3198 	return QLA_SUCCESS;
3199 
3200 crc_queuing_error:
3201 	/* Cleanup will be performed by the caller */
3202 	qpair->req->outstanding_cmds[h] = NULL;
3203 
3204 	return QLA_FUNCTION_FAILED;
3205 }
3206 
3207 /*
3208  * Callback to setup response of xmit_type of QLA_TGT_XMIT_DATA and *
3209  * QLA_TGT_XMIT_STATUS for >= 24xx silicon
3210  */
qlt_xmit_response(struct qla_tgt_cmd * cmd,int xmit_type,uint8_t scsi_status)3211 int qlt_xmit_response(struct qla_tgt_cmd *cmd, int xmit_type,
3212 	uint8_t scsi_status)
3213 {
3214 	struct scsi_qla_host *vha = cmd->vha;
3215 	struct qla_qpair *qpair = cmd->qpair;
3216 	struct ctio7_to_24xx *pkt;
3217 	struct qla_tgt_prm prm;
3218 	uint32_t full_req_cnt = 0;
3219 	unsigned long flags = 0;
3220 	int res;
3221 
3222 	if (!qpair->fw_started || (cmd->reset_count != qpair->chip_reset) ||
3223 	    (cmd->sess && cmd->sess->deleted)) {
3224 		cmd->state = QLA_TGT_STATE_PROCESSED;
3225 		return 0;
3226 	}
3227 
3228 	ql_dbg_qp(ql_dbg_tgt, qpair, 0xe018,
3229 	    "is_send_status=%d, cmd->bufflen=%d, cmd->sg_cnt=%d, cmd->dma_data_direction=%d se_cmd[%p] qp %d\n",
3230 	    (xmit_type & QLA_TGT_XMIT_STATUS) ?
3231 	    1 : 0, cmd->bufflen, cmd->sg_cnt, cmd->dma_data_direction,
3232 	    &cmd->se_cmd, qpair->id);
3233 
3234 	res = qlt_pre_xmit_response(cmd, &prm, xmit_type, scsi_status,
3235 	    &full_req_cnt);
3236 	if (unlikely(res != 0)) {
3237 		return res;
3238 	}
3239 
3240 	spin_lock_irqsave(qpair->qp_lock_ptr, flags);
3241 
3242 	if (xmit_type == QLA_TGT_XMIT_STATUS)
3243 		qpair->tgt_counters.core_qla_snd_status++;
3244 	else
3245 		qpair->tgt_counters.core_qla_que_buf++;
3246 
3247 	if (!qpair->fw_started || cmd->reset_count != qpair->chip_reset) {
3248 		/*
3249 		 * Either the port is not online or this request was from
3250 		 * previous life, just abort the processing.
3251 		 */
3252 		cmd->state = QLA_TGT_STATE_PROCESSED;
3253 		ql_dbg_qp(ql_dbg_async, qpair, 0xe101,
3254 			"RESET-RSP online/active/old-count/new-count = %d/%d/%d/%d.\n",
3255 			vha->flags.online, qla2x00_reset_active(vha),
3256 			cmd->reset_count, qpair->chip_reset);
3257 		res = 0;
3258 		goto out_unmap_unlock;
3259 	}
3260 
3261 	/* Does F/W have an IOCBs for this request */
3262 	res = qlt_check_reserve_free_req(qpair, full_req_cnt);
3263 	if (unlikely(res))
3264 		goto out_unmap_unlock;
3265 
3266 	if (cmd->se_cmd.prot_op && (xmit_type & QLA_TGT_XMIT_DATA))
3267 		res = qlt_build_ctio_crc2_pkt(qpair, &prm);
3268 	else
3269 		res = qlt_24xx_build_ctio_pkt(qpair, &prm);
3270 	if (unlikely(res != 0)) {
3271 		qpair->req->cnt += full_req_cnt;
3272 		goto out_unmap_unlock;
3273 	}
3274 
3275 	pkt = (struct ctio7_to_24xx *)prm.pkt;
3276 
3277 	if (qlt_has_data(cmd) && (xmit_type & QLA_TGT_XMIT_DATA)) {
3278 		pkt->u.status0.flags |=
3279 		    cpu_to_le16(CTIO7_FLAGS_DATA_IN |
3280 			CTIO7_FLAGS_STATUS_MODE_0);
3281 
3282 		if (cmd->se_cmd.prot_op == TARGET_PROT_NORMAL)
3283 			qlt_load_data_segments(&prm);
3284 
3285 		if (prm.add_status_pkt == 0) {
3286 			if (xmit_type & QLA_TGT_XMIT_STATUS) {
3287 				pkt->u.status0.scsi_status =
3288 				    cpu_to_le16(prm.rq_result);
3289 				if (!cmd->edif)
3290 					pkt->u.status0.residual =
3291 						cpu_to_le32(prm.residual);
3292 
3293 				pkt->u.status0.flags |= cpu_to_le16(
3294 				    CTIO7_FLAGS_SEND_STATUS);
3295 				if (qlt_need_explicit_conf(cmd, 0)) {
3296 					pkt->u.status0.flags |=
3297 					    cpu_to_le16(
3298 						CTIO7_FLAGS_EXPLICIT_CONFORM |
3299 						CTIO7_FLAGS_CONFORM_REQ);
3300 				}
3301 			}
3302 
3303 		} else {
3304 			/*
3305 			 * We have already made sure that there is sufficient
3306 			 * amount of request entries to not drop HW lock in
3307 			 * req_pkt().
3308 			 */
3309 			struct ctio7_to_24xx *ctio =
3310 				(struct ctio7_to_24xx *)qlt_get_req_pkt(
3311 				    qpair->req);
3312 
3313 			ql_dbg_qp(ql_dbg_tgt, qpair, 0x305e,
3314 			    "Building additional status packet 0x%p.\n",
3315 			    ctio);
3316 
3317 			/*
3318 			 * T10Dif: ctio_crc2_to_fw overlay ontop of
3319 			 * ctio7_to_24xx
3320 			 */
3321 			memcpy(ctio, pkt, sizeof(*ctio));
3322 			/* reset back to CTIO7 */
3323 			ctio->entry_count = 1;
3324 			ctio->entry_type = CTIO_TYPE7;
3325 			ctio->dseg_count = 0;
3326 			ctio->u.status1.flags &= ~cpu_to_le16(
3327 			    CTIO7_FLAGS_DATA_IN);
3328 
3329 			/* Real finish is ctio_m1's finish */
3330 			pkt->handle |= CTIO_INTERMEDIATE_HANDLE_MARK;
3331 			pkt->u.status0.flags |= cpu_to_le16(
3332 			    CTIO7_FLAGS_DONT_RET_CTIO);
3333 
3334 			/* qlt_24xx_init_ctio_to_isp will correct
3335 			 * all neccessary fields that's part of CTIO7.
3336 			 * There should be no residual of CTIO-CRC2 data.
3337 			 */
3338 			qlt_24xx_init_ctio_to_isp((struct ctio7_to_24xx *)ctio,
3339 			    &prm);
3340 		}
3341 	} else
3342 		qlt_24xx_init_ctio_to_isp(pkt, &prm);
3343 
3344 
3345 	cmd->state = QLA_TGT_STATE_PROCESSED; /* Mid-level is done processing */
3346 	cmd->cmd_sent_to_fw = 1;
3347 	cmd->ctio_flags = le16_to_cpu(pkt->u.status0.flags);
3348 
3349 	/* Memory Barrier */
3350 	wmb();
3351 	if (qpair->reqq_start_iocbs)
3352 		qpair->reqq_start_iocbs(qpair);
3353 	else
3354 		qla2x00_start_iocbs(vha, qpair->req);
3355 	spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
3356 
3357 	return 0;
3358 
3359 out_unmap_unlock:
3360 	qlt_unmap_sg(vha, cmd);
3361 	spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
3362 
3363 	return res;
3364 }
3365 EXPORT_SYMBOL(qlt_xmit_response);
3366 
qlt_rdy_to_xfer(struct qla_tgt_cmd * cmd)3367 int qlt_rdy_to_xfer(struct qla_tgt_cmd *cmd)
3368 {
3369 	struct ctio7_to_24xx *pkt;
3370 	struct scsi_qla_host *vha = cmd->vha;
3371 	struct qla_tgt *tgt = cmd->tgt;
3372 	struct qla_tgt_prm prm;
3373 	unsigned long flags = 0;
3374 	int res = 0;
3375 	struct qla_qpair *qpair = cmd->qpair;
3376 
3377 	memset(&prm, 0, sizeof(prm));
3378 	prm.cmd = cmd;
3379 	prm.tgt = tgt;
3380 	prm.sg = NULL;
3381 	prm.req_cnt = 1;
3382 
3383 	if (!qpair->fw_started || (cmd->reset_count != qpair->chip_reset) ||
3384 	    (cmd->sess && cmd->sess->deleted)) {
3385 		/*
3386 		 * Either the port is not online or this request was from
3387 		 * previous life, just abort the processing.
3388 		 */
3389 		cmd->aborted = 1;
3390 		cmd->write_data_transferred = 0;
3391 		cmd->state = QLA_TGT_STATE_DATA_IN;
3392 		vha->hw->tgt.tgt_ops->handle_data(cmd);
3393 		ql_dbg_qp(ql_dbg_async, qpair, 0xe102,
3394 			"RESET-XFR online/active/old-count/new-count = %d/%d/%d/%d.\n",
3395 			vha->flags.online, qla2x00_reset_active(vha),
3396 			cmd->reset_count, qpair->chip_reset);
3397 		return 0;
3398 	}
3399 
3400 	/* Calculate number of entries and segments required */
3401 	if (qlt_pci_map_calc_cnt(&prm) != 0)
3402 		return -EAGAIN;
3403 
3404 	spin_lock_irqsave(qpair->qp_lock_ptr, flags);
3405 	/* Does F/W have an IOCBs for this request */
3406 	res = qlt_check_reserve_free_req(qpair, prm.req_cnt);
3407 	if (res != 0)
3408 		goto out_unlock_free_unmap;
3409 	if (cmd->se_cmd.prot_op)
3410 		res = qlt_build_ctio_crc2_pkt(qpair, &prm);
3411 	else
3412 		res = qlt_24xx_build_ctio_pkt(qpair, &prm);
3413 
3414 	if (unlikely(res != 0)) {
3415 		qpair->req->cnt += prm.req_cnt;
3416 		goto out_unlock_free_unmap;
3417 	}
3418 
3419 	pkt = (struct ctio7_to_24xx *)prm.pkt;
3420 	pkt->u.status0.flags |= cpu_to_le16(CTIO7_FLAGS_DATA_OUT |
3421 	    CTIO7_FLAGS_STATUS_MODE_0);
3422 
3423 	if (cmd->se_cmd.prot_op == TARGET_PROT_NORMAL)
3424 		qlt_load_data_segments(&prm);
3425 
3426 	cmd->state = QLA_TGT_STATE_NEED_DATA;
3427 	cmd->cmd_sent_to_fw = 1;
3428 	cmd->ctio_flags = le16_to_cpu(pkt->u.status0.flags);
3429 
3430 	/* Memory Barrier */
3431 	wmb();
3432 	if (qpair->reqq_start_iocbs)
3433 		qpair->reqq_start_iocbs(qpair);
3434 	else
3435 		qla2x00_start_iocbs(vha, qpair->req);
3436 	spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
3437 
3438 	return res;
3439 
3440 out_unlock_free_unmap:
3441 	qlt_unmap_sg(vha, cmd);
3442 	spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
3443 
3444 	return res;
3445 }
3446 EXPORT_SYMBOL(qlt_rdy_to_xfer);
3447 
3448 
3449 /*
3450  * it is assumed either hardware_lock or qpair lock is held.
3451  */
3452 static void
qlt_handle_dif_error(struct qla_qpair * qpair,struct qla_tgt_cmd * cmd,struct ctio_crc_from_fw * sts)3453 qlt_handle_dif_error(struct qla_qpair *qpair, struct qla_tgt_cmd *cmd,
3454 	struct ctio_crc_from_fw *sts)
3455 {
3456 	uint8_t		*ap = &sts->actual_dif[0];
3457 	uint8_t		*ep = &sts->expected_dif[0];
3458 	uint64_t	lba = cmd->se_cmd.t_task_lba;
3459 	uint8_t scsi_status, sense_key, asc, ascq;
3460 	unsigned long flags;
3461 	struct scsi_qla_host *vha = cmd->vha;
3462 
3463 	cmd->trc_flags |= TRC_DIF_ERR;
3464 
3465 	cmd->a_guard   = get_unaligned_be16(ap + 0);
3466 	cmd->a_app_tag = get_unaligned_be16(ap + 2);
3467 	cmd->a_ref_tag = get_unaligned_be32(ap + 4);
3468 
3469 	cmd->e_guard   = get_unaligned_be16(ep + 0);
3470 	cmd->e_app_tag = get_unaligned_be16(ep + 2);
3471 	cmd->e_ref_tag = get_unaligned_be32(ep + 4);
3472 
3473 	ql_dbg(ql_dbg_tgt_dif, vha, 0xf075,
3474 	    "%s: aborted %d state %d\n", __func__, cmd->aborted, cmd->state);
3475 
3476 	scsi_status = sense_key = asc = ascq = 0;
3477 
3478 	/* check appl tag */
3479 	if (cmd->e_app_tag != cmd->a_app_tag) {
3480 		ql_dbg(ql_dbg_tgt_dif, vha, 0xe00d,
3481 		    "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]",
3482 		    cmd->cdb[0], lba, (lba+cmd->num_blks), cmd->num_blks,
3483 		    cmd->a_ref_tag, cmd->e_ref_tag, cmd->a_app_tag,
3484 		    cmd->e_app_tag, cmd->a_guard, cmd->e_guard, cmd,
3485 		    cmd->atio.u.isp24.fcp_hdr.ox_id);
3486 
3487 		cmd->dif_err_code = DIF_ERR_APP;
3488 		scsi_status = SAM_STAT_CHECK_CONDITION;
3489 		sense_key = ABORTED_COMMAND;
3490 		asc = 0x10;
3491 		ascq = 0x2;
3492 	}
3493 
3494 	/* check ref tag */
3495 	if (cmd->e_ref_tag != cmd->a_ref_tag) {
3496 		ql_dbg(ql_dbg_tgt_dif, vha, 0xe00e,
3497 		    "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] ",
3498 		    cmd->cdb[0], lba, (lba+cmd->num_blks), cmd->num_blks,
3499 		    cmd->a_ref_tag, cmd->e_ref_tag, cmd->a_app_tag,
3500 		    cmd->e_app_tag, cmd->a_guard, cmd->e_guard, cmd,
3501 		    cmd->atio.u.isp24.fcp_hdr.ox_id);
3502 
3503 		cmd->dif_err_code = DIF_ERR_REF;
3504 		scsi_status = SAM_STAT_CHECK_CONDITION;
3505 		sense_key = ABORTED_COMMAND;
3506 		asc = 0x10;
3507 		ascq = 0x3;
3508 		goto out;
3509 	}
3510 
3511 	/* check guard */
3512 	if (cmd->e_guard != cmd->a_guard) {
3513 		ql_dbg(ql_dbg_tgt_dif, vha, 0xe012,
3514 		    "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]",
3515 		    cmd->cdb[0], lba, (lba+cmd->num_blks), cmd->num_blks,
3516 		    cmd->a_ref_tag, cmd->e_ref_tag, cmd->a_app_tag,
3517 		    cmd->e_app_tag, cmd->a_guard, cmd->e_guard, cmd,
3518 		    cmd->atio.u.isp24.fcp_hdr.ox_id);
3519 
3520 		cmd->dif_err_code = DIF_ERR_GRD;
3521 		scsi_status = SAM_STAT_CHECK_CONDITION;
3522 		sense_key = ABORTED_COMMAND;
3523 		asc = 0x10;
3524 		ascq = 0x1;
3525 	}
3526 out:
3527 	switch (cmd->state) {
3528 	case QLA_TGT_STATE_NEED_DATA:
3529 		/* handle_data will load DIF error code  */
3530 		cmd->state = QLA_TGT_STATE_DATA_IN;
3531 		vha->hw->tgt.tgt_ops->handle_data(cmd);
3532 		break;
3533 	default:
3534 		spin_lock_irqsave(&cmd->cmd_lock, flags);
3535 		if (cmd->aborted) {
3536 			spin_unlock_irqrestore(&cmd->cmd_lock, flags);
3537 			vha->hw->tgt.tgt_ops->free_cmd(cmd);
3538 			break;
3539 		}
3540 		spin_unlock_irqrestore(&cmd->cmd_lock, flags);
3541 
3542 		qlt_send_resp_ctio(qpair, cmd, scsi_status, sense_key, asc,
3543 		    ascq);
3544 		/* assume scsi status gets out on the wire.
3545 		 * Will not wait for completion.
3546 		 */
3547 		vha->hw->tgt.tgt_ops->free_cmd(cmd);
3548 		break;
3549 	}
3550 }
3551 
3552 /* If hardware_lock held on entry, might drop it, then reaquire */
3553 /* This function sends the appropriate CTIO to ISP 2xxx or 24xx */
__qlt_send_term_imm_notif(struct scsi_qla_host * vha,struct imm_ntfy_from_isp * ntfy)3554 static int __qlt_send_term_imm_notif(struct scsi_qla_host *vha,
3555 	struct imm_ntfy_from_isp *ntfy)
3556 {
3557 	struct nack_to_isp *nack;
3558 	struct qla_hw_data *ha = vha->hw;
3559 	request_t *pkt;
3560 	int ret = 0;
3561 
3562 	ql_dbg(ql_dbg_tgt_tmr, vha, 0xe01c,
3563 	    "Sending TERM ELS CTIO (ha=%p)\n", ha);
3564 
3565 	pkt = (request_t *)qla2x00_alloc_iocbs(vha, NULL);
3566 	if (pkt == NULL) {
3567 		ql_dbg(ql_dbg_tgt, vha, 0xe080,
3568 		    "qla_target(%d): %s failed: unable to allocate "
3569 		    "request packet\n", vha->vp_idx, __func__);
3570 		return -ENOMEM;
3571 	}
3572 
3573 	pkt->entry_type = NOTIFY_ACK_TYPE;
3574 	pkt->entry_count = 1;
3575 	pkt->handle = QLA_TGT_SKIP_HANDLE;
3576 
3577 	nack = (struct nack_to_isp *)pkt;
3578 	nack->ox_id = ntfy->ox_id;
3579 
3580 	nack->u.isp24.nport_handle = ntfy->u.isp24.nport_handle;
3581 	if (le16_to_cpu(ntfy->u.isp24.status) == IMM_NTFY_ELS) {
3582 		nack->u.isp24.flags = ntfy->u.isp24.flags &
3583 			cpu_to_le16(NOTIFY24XX_FLAGS_PUREX_IOCB);
3584 	}
3585 
3586 	/* terminate */
3587 	nack->u.isp24.flags |=
3588 		__constant_cpu_to_le16(NOTIFY_ACK_FLAGS_TERMINATE);
3589 
3590 	nack->u.isp24.srr_rx_id = ntfy->u.isp24.srr_rx_id;
3591 	nack->u.isp24.status = ntfy->u.isp24.status;
3592 	nack->u.isp24.status_subcode = ntfy->u.isp24.status_subcode;
3593 	nack->u.isp24.fw_handle = ntfy->u.isp24.fw_handle;
3594 	nack->u.isp24.exchange_address = ntfy->u.isp24.exchange_address;
3595 	nack->u.isp24.srr_rel_offs = ntfy->u.isp24.srr_rel_offs;
3596 	nack->u.isp24.srr_ui = ntfy->u.isp24.srr_ui;
3597 	nack->u.isp24.vp_index = ntfy->u.isp24.vp_index;
3598 
3599 	qla2x00_start_iocbs(vha, vha->req);
3600 	return ret;
3601 }
3602 
qlt_send_term_imm_notif(struct scsi_qla_host * vha,struct imm_ntfy_from_isp * imm,int ha_locked)3603 static void qlt_send_term_imm_notif(struct scsi_qla_host *vha,
3604 	struct imm_ntfy_from_isp *imm, int ha_locked)
3605 {
3606 	int rc;
3607 
3608 	WARN_ON_ONCE(!ha_locked);
3609 	rc = __qlt_send_term_imm_notif(vha, imm);
3610 	pr_debug("rc = %d\n", rc);
3611 }
3612 
3613 /*
3614  * If hardware_lock held on entry, might drop it, then reaquire
3615  * This function sends the appropriate CTIO to ISP 2xxx or 24xx
3616  */
__qlt_send_term_exchange(struct qla_qpair * qpair,struct qla_tgt_cmd * cmd,struct atio_from_isp * atio)3617 static int __qlt_send_term_exchange(struct qla_qpair *qpair,
3618 	struct qla_tgt_cmd *cmd,
3619 	struct atio_from_isp *atio)
3620 {
3621 	struct scsi_qla_host *vha = qpair->vha;
3622 	struct ctio7_to_24xx *ctio24;
3623 	struct qla_hw_data *ha = vha->hw;
3624 	request_t *pkt;
3625 	int ret = 0;
3626 	uint16_t temp;
3627 
3628 	ql_dbg(ql_dbg_tgt, vha, 0xe009, "Sending TERM EXCH CTIO (ha=%p)\n", ha);
3629 
3630 	if (cmd)
3631 		vha = cmd->vha;
3632 
3633 	pkt = (request_t *)qla2x00_alloc_iocbs_ready(qpair, NULL);
3634 	if (pkt == NULL) {
3635 		ql_dbg(ql_dbg_tgt, vha, 0xe050,
3636 		    "qla_target(%d): %s failed: unable to allocate "
3637 		    "request packet\n", vha->vp_idx, __func__);
3638 		return -ENOMEM;
3639 	}
3640 
3641 	if (cmd != NULL) {
3642 		if (cmd->state < QLA_TGT_STATE_PROCESSED) {
3643 			ql_dbg(ql_dbg_tgt, vha, 0xe051,
3644 			    "qla_target(%d): Terminating cmd %p with "
3645 			    "incorrect state %d\n", vha->vp_idx, cmd,
3646 			    cmd->state);
3647 		} else
3648 			ret = 1;
3649 	}
3650 
3651 	qpair->tgt_counters.num_term_xchg_sent++;
3652 	pkt->entry_count = 1;
3653 	pkt->handle = QLA_TGT_SKIP_HANDLE | CTIO_COMPLETION_HANDLE_MARK;
3654 
3655 	ctio24 = (struct ctio7_to_24xx *)pkt;
3656 	ctio24->entry_type = CTIO_TYPE7;
3657 	ctio24->nport_handle = cpu_to_le16(CTIO7_NHANDLE_UNRECOGNIZED);
3658 	ctio24->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
3659 	ctio24->vp_index = vha->vp_idx;
3660 	ctio24->initiator_id = be_id_to_le(atio->u.isp24.fcp_hdr.s_id);
3661 	ctio24->exchange_addr = atio->u.isp24.exchange_addr;
3662 	temp = (atio->u.isp24.attr << 9) | CTIO7_FLAGS_STATUS_MODE_1 |
3663 		CTIO7_FLAGS_TERMINATE;
3664 	ctio24->u.status1.flags = cpu_to_le16(temp);
3665 	temp = be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id);
3666 	ctio24->u.status1.ox_id = cpu_to_le16(temp);
3667 
3668 	/* Memory Barrier */
3669 	wmb();
3670 	if (qpair->reqq_start_iocbs)
3671 		qpair->reqq_start_iocbs(qpair);
3672 	else
3673 		qla2x00_start_iocbs(vha, qpair->req);
3674 	return ret;
3675 }
3676 
qlt_send_term_exchange(struct qla_qpair * qpair,struct qla_tgt_cmd * cmd,struct atio_from_isp * atio,int ha_locked,int ul_abort)3677 static void qlt_send_term_exchange(struct qla_qpair *qpair,
3678 	struct qla_tgt_cmd *cmd, struct atio_from_isp *atio, int ha_locked,
3679 	int ul_abort)
3680 {
3681 	struct scsi_qla_host *vha;
3682 	unsigned long flags = 0;
3683 	int rc;
3684 
3685 	/* why use different vha? NPIV */
3686 	if (cmd)
3687 		vha = cmd->vha;
3688 	else
3689 		vha = qpair->vha;
3690 
3691 	if (ha_locked) {
3692 		rc = __qlt_send_term_exchange(qpair, cmd, atio);
3693 		if (rc == -ENOMEM)
3694 			qlt_alloc_qfull_cmd(vha, atio, 0, 0);
3695 		goto done;
3696 	}
3697 	spin_lock_irqsave(qpair->qp_lock_ptr, flags);
3698 	rc = __qlt_send_term_exchange(qpair, cmd, atio);
3699 	if (rc == -ENOMEM)
3700 		qlt_alloc_qfull_cmd(vha, atio, 0, 0);
3701 
3702 done:
3703 	if (cmd && !ul_abort && !cmd->aborted) {
3704 		if (cmd->sg_mapped)
3705 			qlt_unmap_sg(vha, cmd);
3706 		vha->hw->tgt.tgt_ops->free_cmd(cmd);
3707 	}
3708 
3709 	if (!ha_locked)
3710 		spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
3711 
3712 	return;
3713 }
3714 
qlt_init_term_exchange(struct scsi_qla_host * vha)3715 static void qlt_init_term_exchange(struct scsi_qla_host *vha)
3716 {
3717 	struct list_head free_list;
3718 	struct qla_tgt_cmd *cmd, *tcmd;
3719 
3720 	vha->hw->tgt.leak_exchg_thresh_hold =
3721 	    (vha->hw->cur_fw_xcb_count/100) * LEAK_EXCHG_THRESH_HOLD_PERCENT;
3722 
3723 	cmd = tcmd = NULL;
3724 	if (!list_empty(&vha->hw->tgt.q_full_list)) {
3725 		INIT_LIST_HEAD(&free_list);
3726 		list_splice_init(&vha->hw->tgt.q_full_list, &free_list);
3727 
3728 		list_for_each_entry_safe(cmd, tcmd, &free_list, cmd_list) {
3729 			list_del(&cmd->cmd_list);
3730 			/* This cmd was never sent to TCM.  There is no need
3731 			 * to schedule free or call free_cmd
3732 			 */
3733 			qlt_free_cmd(cmd);
3734 			vha->hw->tgt.num_qfull_cmds_alloc--;
3735 		}
3736 	}
3737 	vha->hw->tgt.num_qfull_cmds_dropped = 0;
3738 }
3739 
qlt_chk_exch_leak_thresh_hold(struct scsi_qla_host * vha)3740 static void qlt_chk_exch_leak_thresh_hold(struct scsi_qla_host *vha)
3741 {
3742 	uint32_t total_leaked;
3743 
3744 	total_leaked = vha->hw->tgt.num_qfull_cmds_dropped;
3745 
3746 	if (vha->hw->tgt.leak_exchg_thresh_hold &&
3747 	    (total_leaked > vha->hw->tgt.leak_exchg_thresh_hold)) {
3748 
3749 		ql_dbg(ql_dbg_tgt, vha, 0xe079,
3750 		    "Chip reset due to exchange starvation: %d/%d.\n",
3751 		    total_leaked, vha->hw->cur_fw_xcb_count);
3752 
3753 		if (IS_P3P_TYPE(vha->hw))
3754 			set_bit(FCOE_CTX_RESET_NEEDED, &vha->dpc_flags);
3755 		else
3756 			set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
3757 		qla2xxx_wake_dpc(vha);
3758 	}
3759 
3760 }
3761 
qlt_abort_cmd(struct qla_tgt_cmd * cmd)3762 int qlt_abort_cmd(struct qla_tgt_cmd *cmd)
3763 {
3764 	struct qla_tgt *tgt = cmd->tgt;
3765 	struct scsi_qla_host *vha = tgt->vha;
3766 	struct se_cmd *se_cmd = &cmd->se_cmd;
3767 	unsigned long flags;
3768 
3769 	ql_dbg(ql_dbg_tgt_mgt, vha, 0xf014,
3770 	    "qla_target(%d): terminating exchange for aborted cmd=%p "
3771 	    "(se_cmd=%p, tag=%llu)", vha->vp_idx, cmd, &cmd->se_cmd,
3772 	    se_cmd->tag);
3773 
3774 	spin_lock_irqsave(&cmd->cmd_lock, flags);
3775 	if (cmd->aborted) {
3776 		if (cmd->sg_mapped)
3777 			qlt_unmap_sg(vha, cmd);
3778 
3779 		spin_unlock_irqrestore(&cmd->cmd_lock, flags);
3780 		/*
3781 		 * It's normal to see 2 calls in this path:
3782 		 *  1) XFER Rdy completion + CMD_T_ABORT
3783 		 *  2) TCM TMR - drain_state_list
3784 		 */
3785 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf016,
3786 		    "multiple abort. %p transport_state %x, t_state %x, "
3787 		    "se_cmd_flags %x\n", cmd, cmd->se_cmd.transport_state,
3788 		    cmd->se_cmd.t_state, cmd->se_cmd.se_cmd_flags);
3789 		return -EIO;
3790 	}
3791 	cmd->aborted = 1;
3792 	cmd->trc_flags |= TRC_ABORT;
3793 	spin_unlock_irqrestore(&cmd->cmd_lock, flags);
3794 
3795 	qlt_send_term_exchange(cmd->qpair, cmd, &cmd->atio, 0, 1);
3796 	return 0;
3797 }
3798 EXPORT_SYMBOL(qlt_abort_cmd);
3799 
qlt_free_cmd(struct qla_tgt_cmd * cmd)3800 void qlt_free_cmd(struct qla_tgt_cmd *cmd)
3801 {
3802 	struct fc_port *sess = cmd->sess;
3803 
3804 	ql_dbg(ql_dbg_tgt, cmd->vha, 0xe074,
3805 	    "%s: se_cmd[%p] ox_id %04x\n",
3806 	    __func__, &cmd->se_cmd,
3807 	    be16_to_cpu(cmd->atio.u.isp24.fcp_hdr.ox_id));
3808 
3809 	BUG_ON(cmd->cmd_in_wq);
3810 
3811 	if (!cmd->q_full)
3812 		qlt_decr_num_pend_cmds(cmd->vha);
3813 
3814 	BUG_ON(cmd->sg_mapped);
3815 	cmd->jiffies_at_free = get_jiffies_64();
3816 
3817 	if (!sess || !sess->se_sess) {
3818 		WARN_ON(1);
3819 		return;
3820 	}
3821 	cmd->jiffies_at_free = get_jiffies_64();
3822 	cmd->vha->hw->tgt.tgt_ops->rel_cmd(cmd);
3823 }
3824 EXPORT_SYMBOL(qlt_free_cmd);
3825 
3826 /*
3827  * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
3828  */
qlt_term_ctio_exchange(struct qla_qpair * qpair,void * ctio,struct qla_tgt_cmd * cmd,uint32_t status)3829 static int qlt_term_ctio_exchange(struct qla_qpair *qpair, void *ctio,
3830 	struct qla_tgt_cmd *cmd, uint32_t status)
3831 {
3832 	int term = 0;
3833 	struct scsi_qla_host *vha = qpair->vha;
3834 
3835 	if (cmd->se_cmd.prot_op)
3836 		ql_dbg(ql_dbg_tgt_dif, vha, 0xe013,
3837 		    "Term DIF cmd: lba[0x%llx|%lld] len[0x%x] "
3838 		    "se_cmd=%p tag[%x] op %#x/%s",
3839 		     cmd->lba, cmd->lba,
3840 		     cmd->num_blks, &cmd->se_cmd,
3841 		     cmd->atio.u.isp24.exchange_addr,
3842 		     cmd->se_cmd.prot_op,
3843 		     prot_op_str(cmd->se_cmd.prot_op));
3844 
3845 	if (ctio != NULL) {
3846 		struct ctio7_from_24xx *c = (struct ctio7_from_24xx *)ctio;
3847 
3848 		term = !(c->flags &
3849 		    cpu_to_le16(OF_TERM_EXCH));
3850 	} else
3851 		term = 1;
3852 
3853 	if (term)
3854 		qlt_send_term_exchange(qpair, cmd, &cmd->atio, 1, 0);
3855 
3856 	return term;
3857 }
3858 
3859 
3860 /* ha->hardware_lock supposed to be held on entry */
qlt_ctio_to_cmd(struct scsi_qla_host * vha,struct rsp_que * rsp,uint32_t handle,void * ctio)3861 static void *qlt_ctio_to_cmd(struct scsi_qla_host *vha,
3862 	struct rsp_que *rsp, uint32_t handle, void *ctio)
3863 {
3864 	void *cmd = NULL;
3865 	struct req_que *req;
3866 	int qid = GET_QID(handle);
3867 	uint32_t h = handle & ~QLA_TGT_HANDLE_MASK;
3868 
3869 	if (unlikely(h == QLA_TGT_SKIP_HANDLE))
3870 		return NULL;
3871 
3872 	if (qid == rsp->req->id) {
3873 		req = rsp->req;
3874 	} else if (vha->hw->req_q_map[qid]) {
3875 		ql_dbg(ql_dbg_tgt_mgt, vha, 0x1000a,
3876 		    "qla_target(%d): CTIO completion with different QID %d handle %x\n",
3877 		    vha->vp_idx, rsp->id, handle);
3878 		req = vha->hw->req_q_map[qid];
3879 	} else {
3880 		return NULL;
3881 	}
3882 
3883 	h &= QLA_CMD_HANDLE_MASK;
3884 
3885 	if (h != QLA_TGT_NULL_HANDLE) {
3886 		if (unlikely(h >= req->num_outstanding_cmds)) {
3887 			ql_dbg(ql_dbg_tgt, vha, 0xe052,
3888 			    "qla_target(%d): Wrong handle %x received\n",
3889 			    vha->vp_idx, handle);
3890 			return NULL;
3891 		}
3892 
3893 		cmd = req->outstanding_cmds[h];
3894 		if (unlikely(cmd == NULL)) {
3895 			ql_dbg(ql_dbg_async, vha, 0xe053,
3896 			    "qla_target(%d): Suspicious: unable to find the command with handle %x req->id %d rsp->id %d\n",
3897 				vha->vp_idx, handle, req->id, rsp->id);
3898 			return NULL;
3899 		}
3900 		req->outstanding_cmds[h] = NULL;
3901 	} else if (ctio != NULL) {
3902 		/* We can't get loop ID from CTIO7 */
3903 		ql_dbg(ql_dbg_tgt, vha, 0xe054,
3904 		    "qla_target(%d): Wrong CTIO received: QLA24xx doesn't "
3905 		    "support NULL handles\n", vha->vp_idx);
3906 		return NULL;
3907 	}
3908 
3909 	return cmd;
3910 }
3911 
3912 /*
3913  * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
3914  */
qlt_do_ctio_completion(struct scsi_qla_host * vha,struct rsp_que * rsp,uint32_t handle,uint32_t status,void * ctio)3915 static void qlt_do_ctio_completion(struct scsi_qla_host *vha,
3916     struct rsp_que *rsp, uint32_t handle, uint32_t status, void *ctio)
3917 {
3918 	struct qla_hw_data *ha = vha->hw;
3919 	struct se_cmd *se_cmd;
3920 	struct qla_tgt_cmd *cmd;
3921 	struct qla_qpair *qpair = rsp->qpair;
3922 
3923 	if (handle & CTIO_INTERMEDIATE_HANDLE_MARK) {
3924 		/* That could happen only in case of an error/reset/abort */
3925 		if (status != CTIO_SUCCESS) {
3926 			ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01d,
3927 			    "Intermediate CTIO received"
3928 			    " (status %x)\n", status);
3929 		}
3930 		return;
3931 	}
3932 
3933 	cmd = qlt_ctio_to_cmd(vha, rsp, handle, ctio);
3934 	if (cmd == NULL)
3935 		return;
3936 
3937 	if ((le16_to_cpu(((struct ctio7_from_24xx *)ctio)->flags) & CTIO7_FLAGS_DATA_OUT) &&
3938 	    cmd->sess) {
3939 		qlt_chk_edif_rx_sa_delete_pending(vha, cmd->sess,
3940 		    (struct ctio7_from_24xx *)ctio);
3941 	}
3942 
3943 	se_cmd = &cmd->se_cmd;
3944 	cmd->cmd_sent_to_fw = 0;
3945 
3946 	qlt_unmap_sg(vha, cmd);
3947 
3948 	if (unlikely(status != CTIO_SUCCESS)) {
3949 		switch (status & 0xFFFF) {
3950 		case CTIO_INVALID_RX_ID:
3951 			if (printk_ratelimit())
3952 				dev_info(&vha->hw->pdev->dev,
3953 				    "qla_target(%d): CTIO with INVALID_RX_ID ATIO attr %x CTIO Flags %x|%x\n",
3954 				    vha->vp_idx, cmd->atio.u.isp24.attr,
3955 				    ((cmd->ctio_flags >> 9) & 0xf),
3956 				    cmd->ctio_flags);
3957 
3958 			break;
3959 		case CTIO_LIP_RESET:
3960 		case CTIO_TARGET_RESET:
3961 		case CTIO_ABORTED:
3962 			/* driver request abort via Terminate exchange */
3963 		case CTIO_TIMEOUT:
3964 			/* They are OK */
3965 			ql_dbg(ql_dbg_tgt_mgt, vha, 0xf058,
3966 			    "qla_target(%d): CTIO with "
3967 			    "status %#x received, state %x, se_cmd %p, "
3968 			    "(LIP_RESET=e, ABORTED=2, TARGET_RESET=17, "
3969 			    "TIMEOUT=b, INVALID_RX_ID=8)\n", vha->vp_idx,
3970 			    status, cmd->state, se_cmd);
3971 			break;
3972 
3973 		case CTIO_PORT_LOGGED_OUT:
3974 		case CTIO_PORT_UNAVAILABLE:
3975 		{
3976 			int logged_out =
3977 				(status & 0xFFFF) == CTIO_PORT_LOGGED_OUT;
3978 
3979 			ql_dbg(ql_dbg_tgt_mgt, vha, 0xf059,
3980 			    "qla_target(%d): CTIO with %s status %x "
3981 			    "received (state %x, se_cmd %p)\n", vha->vp_idx,
3982 			    logged_out ? "PORT LOGGED OUT" : "PORT UNAVAILABLE",
3983 			    status, cmd->state, se_cmd);
3984 
3985 			if (logged_out && cmd->sess) {
3986 				/*
3987 				 * Session is already logged out, but we need
3988 				 * to notify initiator, who's not aware of this
3989 				 */
3990 				cmd->sess->send_els_logo = 1;
3991 				ql_dbg(ql_dbg_disc, vha, 0x20f8,
3992 				    "%s %d %8phC post del sess\n",
3993 				    __func__, __LINE__, cmd->sess->port_name);
3994 
3995 				qlt_schedule_sess_for_deletion(cmd->sess);
3996 			}
3997 			break;
3998 		}
3999 		case CTIO_DIF_ERROR: {
4000 			struct ctio_crc_from_fw *crc =
4001 				(struct ctio_crc_from_fw *)ctio;
4002 			ql_dbg(ql_dbg_tgt_mgt, vha, 0xf073,
4003 			    "qla_target(%d): CTIO with DIF_ERROR status %x "
4004 			    "received (state %x, ulp_cmd %p) actual_dif[0x%llx] "
4005 			    "expect_dif[0x%llx]\n",
4006 			    vha->vp_idx, status, cmd->state, se_cmd,
4007 			    *((u64 *)&crc->actual_dif[0]),
4008 			    *((u64 *)&crc->expected_dif[0]));
4009 
4010 			qlt_handle_dif_error(qpair, cmd, ctio);
4011 			return;
4012 		}
4013 
4014 		case CTIO_FAST_AUTH_ERR:
4015 		case CTIO_FAST_INCOMP_PAD_LEN:
4016 		case CTIO_FAST_INVALID_REQ:
4017 		case CTIO_FAST_SPI_ERR:
4018 			ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05b,
4019 			    "qla_target(%d): CTIO with EDIF error status 0x%x received (state %x, se_cmd %p\n",
4020 			    vha->vp_idx, status, cmd->state, se_cmd);
4021 			break;
4022 
4023 		default:
4024 			ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05b,
4025 			    "qla_target(%d): CTIO with error status 0x%x received (state %x, se_cmd %p\n",
4026 			    vha->vp_idx, status, cmd->state, se_cmd);
4027 			break;
4028 		}
4029 
4030 
4031 		/* "cmd->aborted" means
4032 		 * cmd is already aborted/terminated, we don't
4033 		 * need to terminate again.  The exchange is already
4034 		 * cleaned up/freed at FW level.  Just cleanup at driver
4035 		 * level.
4036 		 */
4037 		if ((cmd->state != QLA_TGT_STATE_NEED_DATA) &&
4038 		    (!cmd->aborted)) {
4039 			cmd->trc_flags |= TRC_CTIO_ERR;
4040 			if (qlt_term_ctio_exchange(qpair, ctio, cmd, status))
4041 				return;
4042 		}
4043 	}
4044 
4045 	if (cmd->state == QLA_TGT_STATE_PROCESSED) {
4046 		cmd->trc_flags |= TRC_CTIO_DONE;
4047 	} else if (cmd->state == QLA_TGT_STATE_NEED_DATA) {
4048 		cmd->state = QLA_TGT_STATE_DATA_IN;
4049 
4050 		if (status == CTIO_SUCCESS)
4051 			cmd->write_data_transferred = 1;
4052 
4053 		ha->tgt.tgt_ops->handle_data(cmd);
4054 		return;
4055 	} else if (cmd->aborted) {
4056 		cmd->trc_flags |= TRC_CTIO_ABORTED;
4057 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01e,
4058 		  "Aborted command %p (tag %lld) finished\n", cmd, se_cmd->tag);
4059 	} else {
4060 		cmd->trc_flags |= TRC_CTIO_STRANGE;
4061 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05c,
4062 		    "qla_target(%d): A command in state (%d) should "
4063 		    "not return a CTIO complete\n", vha->vp_idx, cmd->state);
4064 	}
4065 
4066 	if (unlikely(status != CTIO_SUCCESS) &&
4067 		!cmd->aborted) {
4068 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01f, "Finishing failed CTIO\n");
4069 		dump_stack();
4070 	}
4071 
4072 	ha->tgt.tgt_ops->free_cmd(cmd);
4073 }
4074 
qlt_get_fcp_task_attr(struct scsi_qla_host * vha,uint8_t task_codes)4075 static inline int qlt_get_fcp_task_attr(struct scsi_qla_host *vha,
4076 	uint8_t task_codes)
4077 {
4078 	int fcp_task_attr;
4079 
4080 	switch (task_codes) {
4081 	case ATIO_SIMPLE_QUEUE:
4082 		fcp_task_attr = TCM_SIMPLE_TAG;
4083 		break;
4084 	case ATIO_HEAD_OF_QUEUE:
4085 		fcp_task_attr = TCM_HEAD_TAG;
4086 		break;
4087 	case ATIO_ORDERED_QUEUE:
4088 		fcp_task_attr = TCM_ORDERED_TAG;
4089 		break;
4090 	case ATIO_ACA_QUEUE:
4091 		fcp_task_attr = TCM_ACA_TAG;
4092 		break;
4093 	case ATIO_UNTAGGED:
4094 		fcp_task_attr = TCM_SIMPLE_TAG;
4095 		break;
4096 	default:
4097 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05d,
4098 		    "qla_target: unknown task code %x, use ORDERED instead\n",
4099 		    task_codes);
4100 		fcp_task_attr = TCM_ORDERED_TAG;
4101 		break;
4102 	}
4103 
4104 	return fcp_task_attr;
4105 }
4106 
4107 /*
4108  * Process context for I/O path into tcm_qla2xxx code
4109  */
__qlt_do_work(struct qla_tgt_cmd * cmd)4110 static void __qlt_do_work(struct qla_tgt_cmd *cmd)
4111 {
4112 	scsi_qla_host_t *vha = cmd->vha;
4113 	struct qla_hw_data *ha = vha->hw;
4114 	struct fc_port *sess = cmd->sess;
4115 	struct atio_from_isp *atio = &cmd->atio;
4116 	unsigned char *cdb;
4117 	unsigned long flags;
4118 	uint32_t data_length;
4119 	int ret, fcp_task_attr, data_dir, bidi = 0;
4120 	struct qla_qpair *qpair = cmd->qpair;
4121 
4122 	cmd->cmd_in_wq = 0;
4123 	cmd->trc_flags |= TRC_DO_WORK;
4124 
4125 	if (cmd->aborted) {
4126 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf082,
4127 		    "cmd with tag %u is aborted\n",
4128 		    cmd->atio.u.isp24.exchange_addr);
4129 		goto out_term;
4130 	}
4131 
4132 	spin_lock_init(&cmd->cmd_lock);
4133 	cdb = &atio->u.isp24.fcp_cmnd.cdb[0];
4134 	cmd->se_cmd.tag = le32_to_cpu(atio->u.isp24.exchange_addr);
4135 
4136 	if (atio->u.isp24.fcp_cmnd.rddata &&
4137 	    atio->u.isp24.fcp_cmnd.wrdata) {
4138 		bidi = 1;
4139 		data_dir = DMA_TO_DEVICE;
4140 	} else if (atio->u.isp24.fcp_cmnd.rddata)
4141 		data_dir = DMA_FROM_DEVICE;
4142 	else if (atio->u.isp24.fcp_cmnd.wrdata)
4143 		data_dir = DMA_TO_DEVICE;
4144 	else
4145 		data_dir = DMA_NONE;
4146 
4147 	fcp_task_attr = qlt_get_fcp_task_attr(vha,
4148 	    atio->u.isp24.fcp_cmnd.task_attr);
4149 	data_length = get_datalen_for_atio(atio);
4150 
4151 	ret = ha->tgt.tgt_ops->handle_cmd(vha, cmd, cdb, data_length,
4152 				          fcp_task_attr, data_dir, bidi);
4153 	if (ret != 0)
4154 		goto out_term;
4155 	/*
4156 	 * Drop extra session reference from qlt_handle_cmd_for_atio().
4157 	 */
4158 	ha->tgt.tgt_ops->put_sess(sess);
4159 	return;
4160 
4161 out_term:
4162 	ql_dbg(ql_dbg_io, vha, 0x3060, "Terminating work cmd %p", cmd);
4163 	/*
4164 	 * cmd has not sent to target yet, so pass NULL as the second
4165 	 * argument to qlt_send_term_exchange() and free the memory here.
4166 	 */
4167 	cmd->trc_flags |= TRC_DO_WORK_ERR;
4168 	spin_lock_irqsave(qpair->qp_lock_ptr, flags);
4169 	qlt_send_term_exchange(qpair, NULL, &cmd->atio, 1, 0);
4170 
4171 	qlt_decr_num_pend_cmds(vha);
4172 	cmd->vha->hw->tgt.tgt_ops->rel_cmd(cmd);
4173 	spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
4174 
4175 	ha->tgt.tgt_ops->put_sess(sess);
4176 }
4177 
qlt_do_work(struct work_struct * work)4178 static void qlt_do_work(struct work_struct *work)
4179 {
4180 	struct qla_tgt_cmd *cmd = container_of(work, struct qla_tgt_cmd, work);
4181 	scsi_qla_host_t *vha = cmd->vha;
4182 	unsigned long flags;
4183 
4184 	spin_lock_irqsave(&vha->cmd_list_lock, flags);
4185 	list_del(&cmd->cmd_list);
4186 	spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
4187 
4188 	__qlt_do_work(cmd);
4189 }
4190 
qlt_clr_qp_table(struct scsi_qla_host * vha)4191 void qlt_clr_qp_table(struct scsi_qla_host *vha)
4192 {
4193 	unsigned long flags;
4194 	struct qla_hw_data *ha = vha->hw;
4195 	struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
4196 	void *node;
4197 	u64 key = 0;
4198 
4199 	ql_log(ql_log_info, vha, 0x706c,
4200 	    "User update Number of Active Qpairs %d\n",
4201 	    ha->tgt.num_act_qpairs);
4202 
4203 	spin_lock_irqsave(&ha->tgt.atio_lock, flags);
4204 
4205 	btree_for_each_safe64(&tgt->lun_qpair_map, key, node)
4206 		btree_remove64(&tgt->lun_qpair_map, key);
4207 
4208 	ha->base_qpair->lun_cnt = 0;
4209 	for (key = 0; key < ha->max_qpairs; key++)
4210 		if (ha->queue_pair_map[key])
4211 			ha->queue_pair_map[key]->lun_cnt = 0;
4212 
4213 	spin_unlock_irqrestore(&ha->tgt.atio_lock, flags);
4214 }
4215 
qlt_assign_qpair(struct scsi_qla_host * vha,struct qla_tgt_cmd * cmd)4216 static void qlt_assign_qpair(struct scsi_qla_host *vha,
4217 	struct qla_tgt_cmd *cmd)
4218 {
4219 	struct qla_qpair *qpair, *qp;
4220 	struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
4221 	struct qla_qpair_hint *h;
4222 
4223 	if (vha->flags.qpairs_available) {
4224 		h = btree_lookup64(&tgt->lun_qpair_map, cmd->unpacked_lun);
4225 		if (unlikely(!h)) {
4226 			/* spread lun to qpair ratio evently */
4227 			int lcnt = 0, rc;
4228 			struct scsi_qla_host *base_vha =
4229 				pci_get_drvdata(vha->hw->pdev);
4230 
4231 			qpair = vha->hw->base_qpair;
4232 			if (qpair->lun_cnt == 0) {
4233 				qpair->lun_cnt++;
4234 				h = qla_qpair_to_hint(tgt, qpair);
4235 				BUG_ON(!h);
4236 				rc = btree_insert64(&tgt->lun_qpair_map,
4237 					cmd->unpacked_lun, h, GFP_ATOMIC);
4238 				if (rc) {
4239 					qpair->lun_cnt--;
4240 					ql_log(ql_log_info, vha, 0xd037,
4241 					    "Unable to insert lun %llx into lun_qpair_map\n",
4242 					    cmd->unpacked_lun);
4243 				}
4244 				goto out;
4245 			} else {
4246 				lcnt = qpair->lun_cnt;
4247 			}
4248 
4249 			h = NULL;
4250 			list_for_each_entry(qp, &base_vha->qp_list,
4251 			    qp_list_elem) {
4252 				if (qp->lun_cnt == 0) {
4253 					qp->lun_cnt++;
4254 					h = qla_qpair_to_hint(tgt, qp);
4255 					BUG_ON(!h);
4256 					rc = btree_insert64(&tgt->lun_qpair_map,
4257 					    cmd->unpacked_lun, h, GFP_ATOMIC);
4258 					if (rc) {
4259 						qp->lun_cnt--;
4260 						ql_log(ql_log_info, vha, 0xd038,
4261 							"Unable to insert lun %llx into lun_qpair_map\n",
4262 							cmd->unpacked_lun);
4263 					}
4264 					qpair = qp;
4265 					goto out;
4266 				} else {
4267 					if (qp->lun_cnt < lcnt) {
4268 						lcnt = qp->lun_cnt;
4269 						qpair = qp;
4270 						continue;
4271 					}
4272 				}
4273 			}
4274 			BUG_ON(!qpair);
4275 			qpair->lun_cnt++;
4276 			h = qla_qpair_to_hint(tgt, qpair);
4277 			BUG_ON(!h);
4278 			rc = btree_insert64(&tgt->lun_qpair_map,
4279 				cmd->unpacked_lun, h, GFP_ATOMIC);
4280 			if (rc) {
4281 				qpair->lun_cnt--;
4282 				ql_log(ql_log_info, vha, 0xd039,
4283 				   "Unable to insert lun %llx into lun_qpair_map\n",
4284 				   cmd->unpacked_lun);
4285 			}
4286 		}
4287 	} else {
4288 		h = &tgt->qphints[0];
4289 	}
4290 out:
4291 	cmd->qpair = h->qpair;
4292 	cmd->se_cmd.cpuid = h->cpuid;
4293 }
4294 
qlt_get_tag(scsi_qla_host_t * vha,struct fc_port * sess,struct atio_from_isp * atio)4295 static struct qla_tgt_cmd *qlt_get_tag(scsi_qla_host_t *vha,
4296 				       struct fc_port *sess,
4297 				       struct atio_from_isp *atio)
4298 {
4299 	struct qla_tgt_cmd *cmd;
4300 
4301 	cmd = vha->hw->tgt.tgt_ops->get_cmd(sess);
4302 	if (!cmd)
4303 		return NULL;
4304 
4305 	cmd->cmd_type = TYPE_TGT_CMD;
4306 	memcpy(&cmd->atio, atio, sizeof(*atio));
4307 	INIT_LIST_HEAD(&cmd->sess_cmd_list);
4308 	cmd->state = QLA_TGT_STATE_NEW;
4309 	cmd->tgt = vha->vha_tgt.qla_tgt;
4310 	qlt_incr_num_pend_cmds(vha);
4311 	cmd->vha = vha;
4312 	cmd->sess = sess;
4313 	cmd->loop_id = sess->loop_id;
4314 	cmd->conf_compl_supported = sess->conf_compl_supported;
4315 
4316 	cmd->trc_flags = 0;
4317 	cmd->jiffies_at_alloc = get_jiffies_64();
4318 
4319 	cmd->unpacked_lun = scsilun_to_int(
4320 	    (struct scsi_lun *)&atio->u.isp24.fcp_cmnd.lun);
4321 	qlt_assign_qpair(vha, cmd);
4322 	cmd->reset_count = vha->hw->base_qpair->chip_reset;
4323 	cmd->vp_idx = vha->vp_idx;
4324 	cmd->edif = sess->edif.enable;
4325 
4326 	return cmd;
4327 }
4328 
4329 /* ha->hardware_lock supposed to be held on entry */
qlt_handle_cmd_for_atio(struct scsi_qla_host * vha,struct atio_from_isp * atio)4330 static int qlt_handle_cmd_for_atio(struct scsi_qla_host *vha,
4331 	struct atio_from_isp *atio)
4332 {
4333 	struct qla_hw_data *ha = vha->hw;
4334 	struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
4335 	struct fc_port *sess;
4336 	struct qla_tgt_cmd *cmd;
4337 	unsigned long flags;
4338 	port_id_t id;
4339 
4340 	if (unlikely(tgt->tgt_stop)) {
4341 		ql_dbg(ql_dbg_io, vha, 0x3061,
4342 		    "New command while device %p is shutting down\n", tgt);
4343 		return -ENODEV;
4344 	}
4345 
4346 	id = be_to_port_id(atio->u.isp24.fcp_hdr.s_id);
4347 	if (IS_SW_RESV_ADDR(id))
4348 		return -EBUSY;
4349 
4350 	sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha, atio->u.isp24.fcp_hdr.s_id);
4351 	if (unlikely(!sess))
4352 		return -EFAULT;
4353 
4354 	/* Another WWN used to have our s_id. Our PLOGI scheduled its
4355 	 * session deletion, but it's still in sess_del_work wq */
4356 	if (sess->deleted) {
4357 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf002,
4358 		    "New command while old session %p is being deleted\n",
4359 		    sess);
4360 		return -EFAULT;
4361 	}
4362 
4363 	/*
4364 	 * Do kref_get() before returning + dropping qla_hw_data->hardware_lock.
4365 	 */
4366 	if (!kref_get_unless_zero(&sess->sess_kref)) {
4367 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf004,
4368 		    "%s: kref_get fail, %8phC oxid %x \n",
4369 		    __func__, sess->port_name,
4370 		     be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id));
4371 		return -EFAULT;
4372 	}
4373 
4374 	cmd = qlt_get_tag(vha, sess, atio);
4375 	if (!cmd) {
4376 		ql_dbg(ql_dbg_io, vha, 0x3062,
4377 		    "qla_target(%d): Allocation of cmd failed\n", vha->vp_idx);
4378 		ha->tgt.tgt_ops->put_sess(sess);
4379 		return -EBUSY;
4380 	}
4381 
4382 	cmd->cmd_in_wq = 1;
4383 	cmd->trc_flags |= TRC_NEW_CMD;
4384 
4385 	spin_lock_irqsave(&vha->cmd_list_lock, flags);
4386 	list_add_tail(&cmd->cmd_list, &vha->qla_cmd_list);
4387 	spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
4388 
4389 	INIT_WORK(&cmd->work, qlt_do_work);
4390 	if (vha->flags.qpairs_available) {
4391 		queue_work_on(cmd->se_cmd.cpuid, qla_tgt_wq, &cmd->work);
4392 	} else if (ha->msix_count) {
4393 		if (cmd->atio.u.isp24.fcp_cmnd.rddata)
4394 			queue_work(qla_tgt_wq, &cmd->work);
4395 		else
4396 			queue_work_on(cmd->se_cmd.cpuid, qla_tgt_wq,
4397 			    &cmd->work);
4398 	} else {
4399 		queue_work(qla_tgt_wq, &cmd->work);
4400 	}
4401 
4402 	return 0;
4403 }
4404 
4405 /* ha->hardware_lock supposed to be held on entry */
qlt_issue_task_mgmt(struct fc_port * sess,u64 lun,int fn,void * iocb,int flags)4406 static int qlt_issue_task_mgmt(struct fc_port *sess, u64 lun,
4407 	int fn, void *iocb, int flags)
4408 {
4409 	struct scsi_qla_host *vha = sess->vha;
4410 	struct qla_hw_data *ha = vha->hw;
4411 	struct qla_tgt_mgmt_cmd *mcmd;
4412 	struct atio_from_isp *a = (struct atio_from_isp *)iocb;
4413 	struct qla_qpair_hint *h = &vha->vha_tgt.qla_tgt->qphints[0];
4414 
4415 	mcmd = mempool_alloc(qla_tgt_mgmt_cmd_mempool, GFP_ATOMIC);
4416 	if (!mcmd) {
4417 		ql_dbg(ql_dbg_tgt_tmr, vha, 0x10009,
4418 		    "qla_target(%d): Allocation of management "
4419 		    "command failed, some commands and their data could "
4420 		    "leak\n", vha->vp_idx);
4421 		return -ENOMEM;
4422 	}
4423 	memset(mcmd, 0, sizeof(*mcmd));
4424 	mcmd->sess = sess;
4425 
4426 	if (iocb) {
4427 		memcpy(&mcmd->orig_iocb.imm_ntfy, iocb,
4428 		    sizeof(mcmd->orig_iocb.imm_ntfy));
4429 	}
4430 	mcmd->tmr_func = fn;
4431 	mcmd->flags = flags;
4432 	mcmd->reset_count = ha->base_qpair->chip_reset;
4433 	mcmd->qpair = h->qpair;
4434 	mcmd->vha = vha;
4435 	mcmd->se_cmd.cpuid = h->cpuid;
4436 	mcmd->unpacked_lun = lun;
4437 
4438 	switch (fn) {
4439 	case QLA_TGT_LUN_RESET:
4440 	case QLA_TGT_CLEAR_TS:
4441 	case QLA_TGT_ABORT_TS:
4442 		abort_cmds_for_lun(vha, lun, a->u.isp24.fcp_hdr.s_id);
4443 		fallthrough;
4444 	case QLA_TGT_CLEAR_ACA:
4445 		h = qlt_find_qphint(vha, mcmd->unpacked_lun);
4446 		mcmd->qpair = h->qpair;
4447 		mcmd->se_cmd.cpuid = h->cpuid;
4448 		break;
4449 
4450 	case QLA_TGT_TARGET_RESET:
4451 	case QLA_TGT_NEXUS_LOSS_SESS:
4452 	case QLA_TGT_NEXUS_LOSS:
4453 	case QLA_TGT_ABORT_ALL:
4454 	default:
4455 		/* no-op */
4456 		break;
4457 	}
4458 
4459 	INIT_WORK(&mcmd->work, qlt_do_tmr_work);
4460 	queue_work_on(mcmd->se_cmd.cpuid, qla_tgt_wq,
4461 	    &mcmd->work);
4462 
4463 	return 0;
4464 }
4465 
4466 /* ha->hardware_lock supposed to be held on entry */
qlt_handle_task_mgmt(struct scsi_qla_host * vha,void * iocb)4467 static int qlt_handle_task_mgmt(struct scsi_qla_host *vha, void *iocb)
4468 {
4469 	struct atio_from_isp *a = (struct atio_from_isp *)iocb;
4470 	struct qla_hw_data *ha = vha->hw;
4471 	struct fc_port *sess;
4472 	u64 unpacked_lun;
4473 	int fn;
4474 	unsigned long flags;
4475 
4476 	fn = a->u.isp24.fcp_cmnd.task_mgmt_flags;
4477 
4478 	spin_lock_irqsave(&ha->tgt.sess_lock, flags);
4479 	sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha,
4480 	    a->u.isp24.fcp_hdr.s_id);
4481 	spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
4482 
4483 	unpacked_lun =
4484 	    scsilun_to_int((struct scsi_lun *)&a->u.isp24.fcp_cmnd.lun);
4485 
4486 	if (sess == NULL || sess->deleted)
4487 		return -EFAULT;
4488 
4489 	return qlt_issue_task_mgmt(sess, unpacked_lun, fn, iocb, 0);
4490 }
4491 
4492 /* ha->hardware_lock supposed to be held on entry */
__qlt_abort_task(struct scsi_qla_host * vha,struct imm_ntfy_from_isp * iocb,struct fc_port * sess)4493 static int __qlt_abort_task(struct scsi_qla_host *vha,
4494 	struct imm_ntfy_from_isp *iocb, struct fc_port *sess)
4495 {
4496 	struct atio_from_isp *a = (struct atio_from_isp *)iocb;
4497 	struct qla_hw_data *ha = vha->hw;
4498 	struct qla_tgt_mgmt_cmd *mcmd;
4499 	u64 unpacked_lun;
4500 	int rc;
4501 
4502 	mcmd = mempool_alloc(qla_tgt_mgmt_cmd_mempool, GFP_ATOMIC);
4503 	if (mcmd == NULL) {
4504 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05f,
4505 		    "qla_target(%d): %s: Allocation of ABORT cmd failed\n",
4506 		    vha->vp_idx, __func__);
4507 		return -ENOMEM;
4508 	}
4509 	memset(mcmd, 0, sizeof(*mcmd));
4510 
4511 	mcmd->sess = sess;
4512 	memcpy(&mcmd->orig_iocb.imm_ntfy, iocb,
4513 	    sizeof(mcmd->orig_iocb.imm_ntfy));
4514 
4515 	unpacked_lun =
4516 	    scsilun_to_int((struct scsi_lun *)&a->u.isp24.fcp_cmnd.lun);
4517 	mcmd->reset_count = ha->base_qpair->chip_reset;
4518 	mcmd->tmr_func = QLA_TGT_2G_ABORT_TASK;
4519 	mcmd->qpair = ha->base_qpair;
4520 
4521 	rc = ha->tgt.tgt_ops->handle_tmr(mcmd, unpacked_lun, mcmd->tmr_func,
4522 	    le16_to_cpu(iocb->u.isp2x.seq_id));
4523 	if (rc != 0) {
4524 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf060,
4525 		    "qla_target(%d): tgt_ops->handle_tmr() failed: %d\n",
4526 		    vha->vp_idx, rc);
4527 		mempool_free(mcmd, qla_tgt_mgmt_cmd_mempool);
4528 		return -EFAULT;
4529 	}
4530 
4531 	return 0;
4532 }
4533 
4534 /* ha->hardware_lock supposed to be held on entry */
qlt_abort_task(struct scsi_qla_host * vha,struct imm_ntfy_from_isp * iocb)4535 static int qlt_abort_task(struct scsi_qla_host *vha,
4536 	struct imm_ntfy_from_isp *iocb)
4537 {
4538 	struct qla_hw_data *ha = vha->hw;
4539 	struct fc_port *sess;
4540 	int loop_id;
4541 	unsigned long flags;
4542 
4543 	loop_id = GET_TARGET_ID(ha, (struct atio_from_isp *)iocb);
4544 
4545 	spin_lock_irqsave(&ha->tgt.sess_lock, flags);
4546 	sess = ha->tgt.tgt_ops->find_sess_by_loop_id(vha, loop_id);
4547 	spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
4548 
4549 	if (sess == NULL) {
4550 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf025,
4551 		    "qla_target(%d): task abort for unexisting "
4552 		    "session\n", vha->vp_idx);
4553 		return qlt_sched_sess_work(vha->vha_tgt.qla_tgt,
4554 		    QLA_TGT_SESS_WORK_ABORT, iocb, sizeof(*iocb));
4555 	}
4556 
4557 	return __qlt_abort_task(vha, iocb, sess);
4558 }
4559 
qlt_logo_completion_handler(fc_port_t * fcport,int rc)4560 void qlt_logo_completion_handler(fc_port_t *fcport, int rc)
4561 {
4562 	if (rc != MBS_COMMAND_COMPLETE) {
4563 		ql_dbg(ql_dbg_tgt_mgt, fcport->vha, 0xf093,
4564 			"%s: se_sess %p / sess %p from"
4565 			" port %8phC loop_id %#04x s_id %02x:%02x:%02x"
4566 			" LOGO failed: %#x\n",
4567 			__func__,
4568 			fcport->se_sess,
4569 			fcport,
4570 			fcport->port_name, fcport->loop_id,
4571 			fcport->d_id.b.domain, fcport->d_id.b.area,
4572 			fcport->d_id.b.al_pa, rc);
4573 	}
4574 
4575 	fcport->logout_completed = 1;
4576 }
4577 
4578 /*
4579 * ha->hardware_lock supposed to be held on entry (to protect tgt->sess_list)
4580 *
4581 * Schedules sessions with matching port_id/loop_id but different wwn for
4582 * deletion. Returns existing session with matching wwn if present.
4583 * Null otherwise.
4584 */
4585 struct fc_port *
qlt_find_sess_invalidate_other(scsi_qla_host_t * vha,uint64_t wwn,port_id_t port_id,uint16_t loop_id,struct fc_port ** conflict_sess)4586 qlt_find_sess_invalidate_other(scsi_qla_host_t *vha, uint64_t wwn,
4587     port_id_t port_id, uint16_t loop_id, struct fc_port **conflict_sess)
4588 {
4589 	struct fc_port *sess = NULL, *other_sess;
4590 	uint64_t other_wwn;
4591 
4592 	*conflict_sess = NULL;
4593 
4594 	list_for_each_entry(other_sess, &vha->vp_fcports, list) {
4595 
4596 		other_wwn = wwn_to_u64(other_sess->port_name);
4597 
4598 		if (wwn == other_wwn) {
4599 			WARN_ON(sess);
4600 			sess = other_sess;
4601 			continue;
4602 		}
4603 
4604 		/* find other sess with nport_id collision */
4605 		if (port_id.b24 == other_sess->d_id.b24) {
4606 			if (loop_id != other_sess->loop_id) {
4607 				ql_dbg(ql_dbg_disc, vha, 0x1000c,
4608 				    "Invalidating sess %p loop_id %d wwn %llx.\n",
4609 				    other_sess, other_sess->loop_id, other_wwn);
4610 
4611 				/*
4612 				 * logout_on_delete is set by default, but another
4613 				 * session that has the same s_id/loop_id combo
4614 				 * might have cleared it when requested this session
4615 				 * deletion, so don't touch it
4616 				 */
4617 				qlt_schedule_sess_for_deletion(other_sess);
4618 			} else {
4619 				/*
4620 				 * Another wwn used to have our s_id/loop_id
4621 				 * kill the session, but don't free the loop_id
4622 				 */
4623 				ql_dbg(ql_dbg_disc, vha, 0xf01b,
4624 				    "Invalidating sess %p loop_id %d wwn %llx.\n",
4625 				    other_sess, other_sess->loop_id, other_wwn);
4626 
4627 				other_sess->keep_nport_handle = 1;
4628 				if (other_sess->disc_state != DSC_DELETED)
4629 					*conflict_sess = other_sess;
4630 				qlt_schedule_sess_for_deletion(other_sess);
4631 			}
4632 			continue;
4633 		}
4634 
4635 		/* find other sess with nport handle collision */
4636 		if ((loop_id == other_sess->loop_id) &&
4637 			(loop_id != FC_NO_LOOP_ID)) {
4638 			ql_dbg(ql_dbg_disc, vha, 0x1000d,
4639 			       "Invalidating sess %p loop_id %d wwn %llx.\n",
4640 			       other_sess, other_sess->loop_id, other_wwn);
4641 
4642 			/* Same loop_id but different s_id
4643 			 * Ok to kill and logout */
4644 			qlt_schedule_sess_for_deletion(other_sess);
4645 		}
4646 	}
4647 
4648 	return sess;
4649 }
4650 
4651 /* Abort any commands for this s_id waiting on qla_tgt_wq workqueue */
abort_cmds_for_s_id(struct scsi_qla_host * vha,port_id_t * s_id)4652 static int abort_cmds_for_s_id(struct scsi_qla_host *vha, port_id_t *s_id)
4653 {
4654 	struct qla_tgt_sess_op *op;
4655 	struct qla_tgt_cmd *cmd;
4656 	uint32_t key;
4657 	int count = 0;
4658 	unsigned long flags;
4659 
4660 	key = (((u32)s_id->b.domain << 16) |
4661 	       ((u32)s_id->b.area   <<  8) |
4662 	       ((u32)s_id->b.al_pa));
4663 
4664 	spin_lock_irqsave(&vha->cmd_list_lock, flags);
4665 	list_for_each_entry(op, &vha->unknown_atio_list, cmd_list) {
4666 		uint32_t op_key = sid_to_key(op->atio.u.isp24.fcp_hdr.s_id);
4667 
4668 		if (op_key == key) {
4669 			op->aborted = true;
4670 			count++;
4671 		}
4672 	}
4673 
4674 	list_for_each_entry(cmd, &vha->qla_cmd_list, cmd_list) {
4675 		uint32_t cmd_key = sid_to_key(cmd->atio.u.isp24.fcp_hdr.s_id);
4676 
4677 		if (cmd_key == key) {
4678 			cmd->aborted = 1;
4679 			count++;
4680 		}
4681 	}
4682 	spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
4683 
4684 	return count;
4685 }
4686 
qlt_handle_login(struct scsi_qla_host * vha,struct imm_ntfy_from_isp * iocb)4687 static int qlt_handle_login(struct scsi_qla_host *vha,
4688     struct imm_ntfy_from_isp *iocb)
4689 {
4690 	struct fc_port *sess = NULL, *conflict_sess = NULL;
4691 	uint64_t wwn;
4692 	port_id_t port_id;
4693 	uint16_t loop_id, wd3_lo;
4694 	int res = 0;
4695 	struct qlt_plogi_ack_t *pla;
4696 	unsigned long flags;
4697 
4698 	lockdep_assert_held(&vha->hw->hardware_lock);
4699 
4700 	wwn = wwn_to_u64(iocb->u.isp24.port_name);
4701 
4702 	port_id.b.domain = iocb->u.isp24.port_id[2];
4703 	port_id.b.area   = iocb->u.isp24.port_id[1];
4704 	port_id.b.al_pa  = iocb->u.isp24.port_id[0];
4705 	port_id.b.rsvd_1 = 0;
4706 
4707 	loop_id = le16_to_cpu(iocb->u.isp24.nport_handle);
4708 
4709 	/* Mark all stale commands sitting in qla_tgt_wq for deletion */
4710 	abort_cmds_for_s_id(vha, &port_id);
4711 
4712 	if (wwn) {
4713 		spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
4714 		sess = qlt_find_sess_invalidate_other(vha, wwn,
4715 		    port_id, loop_id, &conflict_sess);
4716 		spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
4717 	} else {
4718 		ql_dbg(ql_dbg_disc, vha, 0xffff,
4719 		    "%s %d Term INOT due to WWN=0 lid=%d, NportID %06X ",
4720 		    __func__, __LINE__, loop_id, port_id.b24);
4721 		qlt_send_term_imm_notif(vha, iocb, 1);
4722 		goto out;
4723 	}
4724 
4725 	if (IS_SW_RESV_ADDR(port_id)) {
4726 		res = 1;
4727 		goto out;
4728 	}
4729 
4730 	if (vha->hw->flags.edif_enabled &&
4731 	    !(vha->e_dbell.db_flags & EDB_ACTIVE) &&
4732 	    iocb->u.isp24.status_subcode == ELS_PLOGI &&
4733 	    !(le16_to_cpu(iocb->u.isp24.flags) & NOTIFY24XX_FLAGS_FCSP)) {
4734 		ql_dbg(ql_dbg_disc, vha, 0xffff,
4735 			"%s %d Term INOT due to app not available lid=%d, NportID %06X ",
4736 			__func__, __LINE__, loop_id, port_id.b24);
4737 		qlt_send_term_imm_notif(vha, iocb, 1);
4738 		goto out;
4739 	}
4740 
4741 	if (vha->hw->flags.edif_enabled) {
4742 		if (DBELL_INACTIVE(vha)) {
4743 			ql_dbg(ql_dbg_disc, vha, 0xffff,
4744 			       "%s %d Term INOT due to app not started lid=%d, NportID %06X ",
4745 			       __func__, __LINE__, loop_id, port_id.b24);
4746 			qlt_send_term_imm_notif(vha, iocb, 1);
4747 			goto out;
4748 		} else if (iocb->u.isp24.status_subcode == ELS_PLOGI &&
4749 			   !(le16_to_cpu(iocb->u.isp24.flags) & NOTIFY24XX_FLAGS_FCSP)) {
4750 			ql_dbg(ql_dbg_disc, vha, 0xffff,
4751 			       "%s %d Term INOT due to unsecure lid=%d, NportID %06X ",
4752 			       __func__, __LINE__, loop_id, port_id.b24);
4753 			qlt_send_term_imm_notif(vha, iocb, 1);
4754 			goto out;
4755 		}
4756 	}
4757 
4758 	pla = qlt_plogi_ack_find_add(vha, &port_id, iocb);
4759 	if (!pla) {
4760 		ql_dbg(ql_dbg_disc + ql_dbg_verbose, vha, 0xffff,
4761 		    "%s %d %8phC Term INOT due to mem alloc fail",
4762 		    __func__, __LINE__,
4763 		    iocb->u.isp24.port_name);
4764 		qlt_send_term_imm_notif(vha, iocb, 1);
4765 		goto out;
4766 	}
4767 
4768 	if (conflict_sess) {
4769 		conflict_sess->login_gen++;
4770 		qlt_plogi_ack_link(vha, pla, conflict_sess,
4771 		    QLT_PLOGI_LINK_CONFLICT);
4772 	}
4773 
4774 	if (!sess) {
4775 		pla->ref_count++;
4776 		ql_dbg(ql_dbg_disc, vha, 0xffff,
4777 		    "%s %d %8phC post new sess\n",
4778 		    __func__, __LINE__, iocb->u.isp24.port_name);
4779 		if (iocb->u.isp24.status_subcode == ELS_PLOGI)
4780 			qla24xx_post_newsess_work(vha, &port_id,
4781 			    iocb->u.isp24.port_name,
4782 			    iocb->u.isp24.u.plogi.node_name,
4783 			    pla, 0);
4784 		else
4785 			qla24xx_post_newsess_work(vha, &port_id,
4786 			    iocb->u.isp24.port_name, NULL,
4787 			    pla, 0);
4788 
4789 		goto out;
4790 	}
4791 
4792 	if (sess->disc_state == DSC_UPD_FCPORT) {
4793 		u16 sec;
4794 
4795 		/*
4796 		 * Remote port registration is still going on from
4797 		 * previous login. Allow it to finish before we
4798 		 * accept the new login.
4799 		 */
4800 		sess->next_disc_state = DSC_DELETE_PEND;
4801 		sec = jiffies_to_msecs(jiffies -
4802 		    sess->jiffies_at_registration) / 1000;
4803 		if (sess->sec_since_registration < sec && sec &&
4804 		    !(sec % 5)) {
4805 			sess->sec_since_registration = sec;
4806 			ql_dbg(ql_dbg_disc, vha, 0xffff,
4807 			    "%s %8phC - Slow Rport registration (%d Sec)\n",
4808 			    __func__, sess->port_name, sec);
4809 		}
4810 
4811 		if (!conflict_sess) {
4812 			list_del(&pla->list);
4813 			kmem_cache_free(qla_tgt_plogi_cachep, pla);
4814 		}
4815 
4816 		qlt_send_term_imm_notif(vha, iocb, 1);
4817 		goto out;
4818 	}
4819 
4820 	qlt_plogi_ack_link(vha, pla, sess, QLT_PLOGI_LINK_SAME_WWN);
4821 	sess->d_id = port_id;
4822 	sess->login_gen++;
4823 	sess->loop_id = loop_id;
4824 
4825 	if (iocb->u.isp24.status_subcode == ELS_PLOGI) {
4826 		/* remote port has assigned Port ID */
4827 		if (N2N_TOPO(vha->hw) && fcport_is_bigger(sess))
4828 			vha->d_id = sess->d_id;
4829 
4830 		ql_dbg(ql_dbg_disc, vha, 0xffff,
4831 		    "%s %8phC - send port online\n",
4832 		    __func__, sess->port_name);
4833 
4834 		qla2x00_post_aen_work(vha, FCH_EVT_PORT_ONLINE,
4835 		    sess->d_id.b24);
4836 	}
4837 
4838 	if (iocb->u.isp24.status_subcode == ELS_PRLI) {
4839 		sess->fw_login_state = DSC_LS_PRLI_PEND;
4840 		sess->local = 0;
4841 		sess->loop_id = loop_id;
4842 		sess->d_id = port_id;
4843 		sess->fw_login_state = DSC_LS_PRLI_PEND;
4844 		wd3_lo = le16_to_cpu(iocb->u.isp24.u.prli.wd3_lo);
4845 
4846 		if (wd3_lo & BIT_7)
4847 			sess->conf_compl_supported = 1;
4848 
4849 		if ((wd3_lo & BIT_4) == 0)
4850 			sess->port_type = FCT_INITIATOR;
4851 		else
4852 			sess->port_type = FCT_TARGET;
4853 
4854 	} else
4855 		sess->fw_login_state = DSC_LS_PLOGI_PEND;
4856 
4857 
4858 	ql_dbg(ql_dbg_disc, vha, 0x20f9,
4859 	    "%s %d %8phC  DS %d\n",
4860 	    __func__, __LINE__, sess->port_name, sess->disc_state);
4861 
4862 	switch (sess->disc_state) {
4863 	case DSC_DELETED:
4864 	case DSC_LOGIN_PEND:
4865 		qlt_plogi_ack_unref(vha, pla);
4866 		break;
4867 
4868 	default:
4869 		/*
4870 		 * Under normal circumstances we want to release nport handle
4871 		 * during LOGO process to avoid nport handle leaks inside FW.
4872 		 * The exception is when LOGO is done while another PLOGI with
4873 		 * the same nport handle is waiting as might be the case here.
4874 		 * Note: there is always a possibily of a race where session
4875 		 * deletion has already started for other reasons (e.g. ACL
4876 		 * removal) and now PLOGI arrives:
4877 		 * 1. if PLOGI arrived in FW after nport handle has been freed,
4878 		 *    FW must have assigned this PLOGI a new/same handle and we
4879 		 *    can proceed ACK'ing it as usual when session deletion
4880 		 *    completes.
4881 		 * 2. if PLOGI arrived in FW before LOGO with LCF_FREE_NPORT
4882 		 *    bit reached it, the handle has now been released. We'll
4883 		 *    get an error when we ACK this PLOGI. Nothing will be sent
4884 		 *    back to initiator. Initiator should eventually retry
4885 		 *    PLOGI and situation will correct itself.
4886 		 */
4887 		sess->keep_nport_handle = ((sess->loop_id == loop_id) &&
4888 		    (sess->d_id.b24 == port_id.b24));
4889 
4890 		ql_dbg(ql_dbg_disc, vha, 0x20f9,
4891 		    "%s %d %8phC post del sess\n",
4892 		    __func__, __LINE__, sess->port_name);
4893 
4894 
4895 		qlt_schedule_sess_for_deletion(sess);
4896 		break;
4897 	}
4898 out:
4899 	return res;
4900 }
4901 
4902 /*
4903  * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
4904  */
qlt_24xx_handle_els(struct scsi_qla_host * vha,struct imm_ntfy_from_isp * iocb)4905 static int qlt_24xx_handle_els(struct scsi_qla_host *vha,
4906 	struct imm_ntfy_from_isp *iocb)
4907 {
4908 	struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
4909 	struct qla_hw_data *ha = vha->hw;
4910 	struct fc_port *sess = NULL, *conflict_sess = NULL;
4911 	uint64_t wwn;
4912 	port_id_t port_id;
4913 	uint16_t loop_id;
4914 	uint16_t wd3_lo;
4915 	int res = 0;
4916 	unsigned long flags;
4917 
4918 	lockdep_assert_held(&ha->hardware_lock);
4919 
4920 	wwn = wwn_to_u64(iocb->u.isp24.port_name);
4921 
4922 	port_id.b.domain = iocb->u.isp24.port_id[2];
4923 	port_id.b.area   = iocb->u.isp24.port_id[1];
4924 	port_id.b.al_pa  = iocb->u.isp24.port_id[0];
4925 	port_id.b.rsvd_1 = 0;
4926 
4927 	loop_id = le16_to_cpu(iocb->u.isp24.nport_handle);
4928 
4929 	ql_dbg(ql_dbg_disc, vha, 0xf026,
4930 	    "qla_target(%d): Port ID: %02x:%02x:%02x ELS opcode: 0x%02x lid %d %8phC\n",
4931 	    vha->vp_idx, iocb->u.isp24.port_id[2],
4932 		iocb->u.isp24.port_id[1], iocb->u.isp24.port_id[0],
4933 		   iocb->u.isp24.status_subcode, loop_id,
4934 		iocb->u.isp24.port_name);
4935 
4936 	/* res = 1 means ack at the end of thread
4937 	 * res = 0 means ack async/later.
4938 	 */
4939 	switch (iocb->u.isp24.status_subcode) {
4940 	case ELS_PLOGI:
4941 		res = qlt_handle_login(vha, iocb);
4942 		break;
4943 
4944 	case ELS_PRLI:
4945 		if (N2N_TOPO(ha)) {
4946 			sess = qla2x00_find_fcport_by_wwpn(vha,
4947 			    iocb->u.isp24.port_name, 1);
4948 
4949 			if (vha->hw->flags.edif_enabled && sess &&
4950 			    (!(sess->flags & FCF_FCSP_DEVICE) ||
4951 			     !sess->edif.authok)) {
4952 				ql_dbg(ql_dbg_disc, vha, 0xffff,
4953 				       "%s %d %8phC Term PRLI due to unauthorize PRLI\n",
4954 				       __func__, __LINE__, iocb->u.isp24.port_name);
4955 				qlt_send_term_imm_notif(vha, iocb, 1);
4956 				break;
4957 			}
4958 
4959 			if (sess && sess->plogi_link[QLT_PLOGI_LINK_SAME_WWN]) {
4960 				ql_dbg(ql_dbg_disc, vha, 0xffff,
4961 				    "%s %d %8phC Term PRLI due to PLOGI ACK not completed\n",
4962 				    __func__, __LINE__,
4963 				    iocb->u.isp24.port_name);
4964 				qlt_send_term_imm_notif(vha, iocb, 1);
4965 				break;
4966 			}
4967 
4968 			res = qlt_handle_login(vha, iocb);
4969 			break;
4970 		}
4971 
4972 		if (IS_SW_RESV_ADDR(port_id)) {
4973 			res = 1;
4974 			break;
4975 		}
4976 
4977 		wd3_lo = le16_to_cpu(iocb->u.isp24.u.prli.wd3_lo);
4978 
4979 		if (wwn) {
4980 			spin_lock_irqsave(&tgt->ha->tgt.sess_lock, flags);
4981 			sess = qlt_find_sess_invalidate_other(vha, wwn, port_id,
4982 				loop_id, &conflict_sess);
4983 			spin_unlock_irqrestore(&tgt->ha->tgt.sess_lock, flags);
4984 		}
4985 
4986 		if (conflict_sess) {
4987 			switch (conflict_sess->disc_state) {
4988 			case DSC_DELETED:
4989 			case DSC_DELETE_PEND:
4990 				break;
4991 			default:
4992 				ql_dbg(ql_dbg_tgt_mgt, vha, 0xf09b,
4993 				    "PRLI with conflicting sess %p port %8phC\n",
4994 				    conflict_sess, conflict_sess->port_name);
4995 				conflict_sess->fw_login_state =
4996 				    DSC_LS_PORT_UNAVAIL;
4997 				qlt_send_term_imm_notif(vha, iocb, 1);
4998 				res = 0;
4999 				break;
5000 			}
5001 		}
5002 
5003 		if (sess != NULL) {
5004 			bool delete = false;
5005 			int sec;
5006 
5007 			if (vha->hw->flags.edif_enabled && sess &&
5008 			    (!(sess->flags & FCF_FCSP_DEVICE) ||
5009 			     !sess->edif.authok)) {
5010 				ql_dbg(ql_dbg_disc, vha, 0xffff,
5011 				       "%s %d %8phC Term PRLI due to unauthorize prli\n",
5012 				       __func__, __LINE__, iocb->u.isp24.port_name);
5013 				qlt_send_term_imm_notif(vha, iocb, 1);
5014 				break;
5015 			}
5016 
5017 			spin_lock_irqsave(&tgt->ha->tgt.sess_lock, flags);
5018 			switch (sess->fw_login_state) {
5019 			case DSC_LS_PLOGI_PEND:
5020 			case DSC_LS_PLOGI_COMP:
5021 			case DSC_LS_PRLI_COMP:
5022 				break;
5023 			default:
5024 				delete = true;
5025 				break;
5026 			}
5027 
5028 			switch (sess->disc_state) {
5029 			case DSC_UPD_FCPORT:
5030 				spin_unlock_irqrestore(&tgt->ha->tgt.sess_lock,
5031 				    flags);
5032 
5033 				sec = jiffies_to_msecs(jiffies -
5034 				    sess->jiffies_at_registration)/1000;
5035 				if (sess->sec_since_registration < sec && sec &&
5036 				    !(sec % 5)) {
5037 					sess->sec_since_registration = sec;
5038 					ql_dbg(ql_dbg_disc, sess->vha, 0xffff,
5039 					    "%s %8phC : Slow Rport registration(%d Sec)\n",
5040 					    __func__, sess->port_name, sec);
5041 				}
5042 				qlt_send_term_imm_notif(vha, iocb, 1);
5043 				return 0;
5044 
5045 			case DSC_LOGIN_PEND:
5046 			case DSC_GPDB:
5047 			case DSC_LOGIN_COMPLETE:
5048 			case DSC_ADISC:
5049 				delete = false;
5050 				break;
5051 			default:
5052 				break;
5053 			}
5054 
5055 			if (delete) {
5056 				spin_unlock_irqrestore(&tgt->ha->tgt.sess_lock,
5057 				    flags);
5058 				/*
5059 				 * Impatient initiator sent PRLI before last
5060 				 * PLOGI could finish. Will force him to re-try,
5061 				 * while last one finishes.
5062 				 */
5063 				ql_log(ql_log_warn, sess->vha, 0xf095,
5064 				    "sess %p PRLI received, before plogi ack.\n",
5065 				    sess);
5066 				qlt_send_term_imm_notif(vha, iocb, 1);
5067 				res = 0;
5068 				break;
5069 			}
5070 
5071 			/*
5072 			 * This shouldn't happen under normal circumstances,
5073 			 * since we have deleted the old session during PLOGI
5074 			 */
5075 			ql_dbg(ql_dbg_tgt_mgt, vha, 0xf096,
5076 			    "PRLI (loop_id %#04x) for existing sess %p (loop_id %#04x)\n",
5077 			    sess->loop_id, sess, iocb->u.isp24.nport_handle);
5078 
5079 			sess->local = 0;
5080 			sess->loop_id = loop_id;
5081 			sess->d_id = port_id;
5082 			sess->fw_login_state = DSC_LS_PRLI_PEND;
5083 
5084 			if (wd3_lo & BIT_7)
5085 				sess->conf_compl_supported = 1;
5086 
5087 			if ((wd3_lo & BIT_4) == 0)
5088 				sess->port_type = FCT_INITIATOR;
5089 			else
5090 				sess->port_type = FCT_TARGET;
5091 
5092 			spin_unlock_irqrestore(&tgt->ha->tgt.sess_lock, flags);
5093 		}
5094 		res = 1; /* send notify ack */
5095 
5096 		/* Make session global (not used in fabric mode) */
5097 		if (ha->current_topology != ISP_CFG_F) {
5098 			if (sess) {
5099 				ql_dbg(ql_dbg_disc, vha, 0x20fa,
5100 				    "%s %d %8phC post nack\n",
5101 				    __func__, __LINE__, sess->port_name);
5102 				qla24xx_post_nack_work(vha, sess, iocb,
5103 					SRB_NACK_PRLI);
5104 				res = 0;
5105 			} else {
5106 				set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
5107 				set_bit(LOCAL_LOOP_UPDATE, &vha->dpc_flags);
5108 				qla2xxx_wake_dpc(vha);
5109 			}
5110 		} else {
5111 			if (sess) {
5112 				ql_dbg(ql_dbg_disc, vha, 0x20fb,
5113 				    "%s %d %8phC post nack\n",
5114 				    __func__, __LINE__, sess->port_name);
5115 				qla24xx_post_nack_work(vha, sess, iocb,
5116 					SRB_NACK_PRLI);
5117 				res = 0;
5118 			}
5119 		}
5120 		break;
5121 
5122 	case ELS_TPRLO:
5123 		if (le16_to_cpu(iocb->u.isp24.flags) &
5124 			NOTIFY24XX_FLAGS_GLOBAL_TPRLO) {
5125 			loop_id = 0xFFFF;
5126 			qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS);
5127 			res = 1;
5128 			break;
5129 		}
5130 		fallthrough;
5131 	case ELS_LOGO:
5132 	case ELS_PRLO:
5133 		spin_lock_irqsave(&ha->tgt.sess_lock, flags);
5134 		sess = qla2x00_find_fcport_by_loopid(vha, loop_id);
5135 		spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
5136 
5137 		if (sess) {
5138 			sess->login_gen++;
5139 			sess->fw_login_state = DSC_LS_LOGO_PEND;
5140 			sess->logo_ack_needed = 1;
5141 			memcpy(sess->iocb, iocb, IOCB_SIZE);
5142 		}
5143 
5144 		res = qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS_SESS);
5145 
5146 		ql_dbg(ql_dbg_disc, vha, 0x20fc,
5147 		    "%s: logo %llx res %d sess %p ",
5148 		    __func__, wwn, res, sess);
5149 		if (res == 0) {
5150 			/*
5151 			 * cmd went upper layer, look for qlt_xmit_tm_rsp()
5152 			 * for LOGO_ACK & sess delete
5153 			 */
5154 			BUG_ON(!sess);
5155 			res = 0;
5156 		} else {
5157 			/* cmd did not go to upper layer. */
5158 			if (sess) {
5159 				qlt_schedule_sess_for_deletion(sess);
5160 				res = 0;
5161 			}
5162 			/* else logo will be ack */
5163 		}
5164 		break;
5165 	case ELS_PDISC:
5166 	case ELS_ADISC:
5167 	{
5168 		struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
5169 
5170 		if (tgt->link_reinit_iocb_pending) {
5171 			qlt_send_notify_ack(ha->base_qpair,
5172 			    &tgt->link_reinit_iocb, 0, 0, 0, 0, 0, 0);
5173 			tgt->link_reinit_iocb_pending = 0;
5174 		}
5175 
5176 		sess = qla2x00_find_fcport_by_wwpn(vha,
5177 		    iocb->u.isp24.port_name, 1);
5178 		if (sess) {
5179 			ql_dbg(ql_dbg_disc, vha, 0x20fd,
5180 				"sess %p lid %d|%d DS %d LS %d\n",
5181 				sess, sess->loop_id, loop_id,
5182 				sess->disc_state, sess->fw_login_state);
5183 		}
5184 
5185 		res = 1; /* send notify ack */
5186 		break;
5187 	}
5188 
5189 	case ELS_FLOGI:	/* should never happen */
5190 	default:
5191 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf061,
5192 		    "qla_target(%d): Unsupported ELS command %x "
5193 		    "received\n", vha->vp_idx, iocb->u.isp24.status_subcode);
5194 		res = qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS_SESS);
5195 		break;
5196 	}
5197 
5198 	ql_dbg(ql_dbg_disc, vha, 0xf026,
5199 	    "qla_target(%d): Exit ELS opcode: 0x%02x res %d\n",
5200 	    vha->vp_idx, iocb->u.isp24.status_subcode, res);
5201 
5202 	return res;
5203 }
5204 
5205 /*
5206  * ha->hardware_lock supposed to be held on entry.
5207  * Might drop it, then reacquire.
5208  */
qlt_handle_imm_notify(struct scsi_qla_host * vha,struct imm_ntfy_from_isp * iocb)5209 static void qlt_handle_imm_notify(struct scsi_qla_host *vha,
5210 	struct imm_ntfy_from_isp *iocb)
5211 {
5212 	struct qla_hw_data *ha = vha->hw;
5213 	uint32_t add_flags = 0;
5214 	int send_notify_ack = 1;
5215 	uint16_t status;
5216 
5217 	lockdep_assert_held(&ha->hardware_lock);
5218 
5219 	status = le16_to_cpu(iocb->u.isp2x.status);
5220 	switch (status) {
5221 	case IMM_NTFY_LIP_RESET:
5222 	{
5223 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf032,
5224 		    "qla_target(%d): LIP reset (loop %#x), subcode %x\n",
5225 		    vha->vp_idx, le16_to_cpu(iocb->u.isp24.nport_handle),
5226 		    iocb->u.isp24.status_subcode);
5227 
5228 		if (qlt_reset(vha, iocb, QLA_TGT_ABORT_ALL) == 0)
5229 			send_notify_ack = 0;
5230 		break;
5231 	}
5232 
5233 	case IMM_NTFY_LIP_LINK_REINIT:
5234 	{
5235 		struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
5236 
5237 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf033,
5238 		    "qla_target(%d): LINK REINIT (loop %#x, "
5239 		    "subcode %x)\n", vha->vp_idx,
5240 		    le16_to_cpu(iocb->u.isp24.nport_handle),
5241 		    iocb->u.isp24.status_subcode);
5242 		if (tgt->link_reinit_iocb_pending) {
5243 			qlt_send_notify_ack(ha->base_qpair,
5244 			    &tgt->link_reinit_iocb, 0, 0, 0, 0, 0, 0);
5245 		}
5246 		memcpy(&tgt->link_reinit_iocb, iocb, sizeof(*iocb));
5247 		tgt->link_reinit_iocb_pending = 1;
5248 		/*
5249 		 * QLogic requires to wait after LINK REINIT for possible
5250 		 * PDISC or ADISC ELS commands
5251 		 */
5252 		send_notify_ack = 0;
5253 		break;
5254 	}
5255 
5256 	case IMM_NTFY_PORT_LOGOUT:
5257 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf034,
5258 		    "qla_target(%d): Port logout (loop "
5259 		    "%#x, subcode %x)\n", vha->vp_idx,
5260 		    le16_to_cpu(iocb->u.isp24.nport_handle),
5261 		    iocb->u.isp24.status_subcode);
5262 
5263 		if (qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS_SESS) == 0)
5264 			send_notify_ack = 0;
5265 		/* The sessions will be cleared in the callback, if needed */
5266 		break;
5267 
5268 	case IMM_NTFY_GLBL_TPRLO:
5269 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf035,
5270 		    "qla_target(%d): Global TPRLO (%x)\n", vha->vp_idx, status);
5271 		if (qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS) == 0)
5272 			send_notify_ack = 0;
5273 		/* The sessions will be cleared in the callback, if needed */
5274 		break;
5275 
5276 	case IMM_NTFY_PORT_CONFIG:
5277 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf036,
5278 		    "qla_target(%d): Port config changed (%x)\n", vha->vp_idx,
5279 		    status);
5280 		if (qlt_reset(vha, iocb, QLA_TGT_ABORT_ALL) == 0)
5281 			send_notify_ack = 0;
5282 		/* The sessions will be cleared in the callback, if needed */
5283 		break;
5284 
5285 	case IMM_NTFY_GLBL_LOGO:
5286 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06a,
5287 		    "qla_target(%d): Link failure detected\n",
5288 		    vha->vp_idx);
5289 		/* I_T nexus loss */
5290 		if (qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS) == 0)
5291 			send_notify_ack = 0;
5292 		break;
5293 
5294 	case IMM_NTFY_IOCB_OVERFLOW:
5295 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06b,
5296 		    "qla_target(%d): Cannot provide requested "
5297 		    "capability (IOCB overflowed the immediate notify "
5298 		    "resource count)\n", vha->vp_idx);
5299 		break;
5300 
5301 	case IMM_NTFY_ABORT_TASK:
5302 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf037,
5303 		    "qla_target(%d): Abort Task (S %08x I %#x -> "
5304 		    "L %#x)\n", vha->vp_idx,
5305 		    le16_to_cpu(iocb->u.isp2x.seq_id),
5306 		    GET_TARGET_ID(ha, (struct atio_from_isp *)iocb),
5307 		    le16_to_cpu(iocb->u.isp2x.lun));
5308 		if (qlt_abort_task(vha, iocb) == 0)
5309 			send_notify_ack = 0;
5310 		break;
5311 
5312 	case IMM_NTFY_RESOURCE:
5313 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06c,
5314 		    "qla_target(%d): Out of resources, host %ld\n",
5315 		    vha->vp_idx, vha->host_no);
5316 		break;
5317 
5318 	case IMM_NTFY_MSG_RX:
5319 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf038,
5320 		    "qla_target(%d): Immediate notify task %x\n",
5321 		    vha->vp_idx, iocb->u.isp2x.task_flags);
5322 		break;
5323 
5324 	case IMM_NTFY_ELS:
5325 		if (qlt_24xx_handle_els(vha, iocb) == 0)
5326 			send_notify_ack = 0;
5327 		break;
5328 	default:
5329 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06d,
5330 		    "qla_target(%d): Received unknown immediate "
5331 		    "notify status %x\n", vha->vp_idx, status);
5332 		break;
5333 	}
5334 
5335 	if (send_notify_ack)
5336 		qlt_send_notify_ack(ha->base_qpair, iocb, add_flags, 0, 0, 0,
5337 		    0, 0);
5338 }
5339 
5340 /*
5341  * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
5342  * This function sends busy to ISP 2xxx or 24xx.
5343  */
__qlt_send_busy(struct qla_qpair * qpair,struct atio_from_isp * atio,uint16_t status)5344 static int __qlt_send_busy(struct qla_qpair *qpair,
5345 	struct atio_from_isp *atio, uint16_t status)
5346 {
5347 	struct scsi_qla_host *vha = qpair->vha;
5348 	struct ctio7_to_24xx *ctio24;
5349 	struct qla_hw_data *ha = vha->hw;
5350 	request_t *pkt;
5351 	struct fc_port *sess = NULL;
5352 	unsigned long flags;
5353 	u16 temp;
5354 	port_id_t id;
5355 
5356 	id = be_to_port_id(atio->u.isp24.fcp_hdr.s_id);
5357 
5358 	spin_lock_irqsave(&ha->tgt.sess_lock, flags);
5359 	sess = qla2x00_find_fcport_by_nportid(vha, &id, 1);
5360 	spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
5361 	if (!sess) {
5362 		qlt_send_term_exchange(qpair, NULL, atio, 1, 0);
5363 		return 0;
5364 	}
5365 	/* Sending marker isn't necessary, since we called from ISR */
5366 
5367 	pkt = (request_t *)__qla2x00_alloc_iocbs(qpair, NULL);
5368 	if (!pkt) {
5369 		ql_dbg(ql_dbg_io, vha, 0x3063,
5370 		    "qla_target(%d): %s failed: unable to allocate "
5371 		    "request packet", vha->vp_idx, __func__);
5372 		return -ENOMEM;
5373 	}
5374 
5375 	qpair->tgt_counters.num_q_full_sent++;
5376 	pkt->entry_count = 1;
5377 	pkt->handle = QLA_TGT_SKIP_HANDLE | CTIO_COMPLETION_HANDLE_MARK;
5378 
5379 	ctio24 = (struct ctio7_to_24xx *)pkt;
5380 	ctio24->entry_type = CTIO_TYPE7;
5381 	ctio24->nport_handle = cpu_to_le16(sess->loop_id);
5382 	ctio24->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
5383 	ctio24->vp_index = vha->vp_idx;
5384 	ctio24->initiator_id = be_id_to_le(atio->u.isp24.fcp_hdr.s_id);
5385 	ctio24->exchange_addr = atio->u.isp24.exchange_addr;
5386 	temp = (atio->u.isp24.attr << 9) |
5387 		CTIO7_FLAGS_STATUS_MODE_1 | CTIO7_FLAGS_SEND_STATUS |
5388 		CTIO7_FLAGS_DONT_RET_CTIO;
5389 	ctio24->u.status1.flags = cpu_to_le16(temp);
5390 	/*
5391 	 * CTIO from fw w/o se_cmd doesn't provide enough info to retry it,
5392 	 * if the explicit conformation is used.
5393 	 */
5394 	ctio24->u.status1.ox_id =
5395 		cpu_to_le16(be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id));
5396 	ctio24->u.status1.scsi_status = cpu_to_le16(status);
5397 
5398 	ctio24->u.status1.residual = cpu_to_le32(get_datalen_for_atio(atio));
5399 
5400 	if (ctio24->u.status1.residual != 0)
5401 		ctio24->u.status1.scsi_status |= cpu_to_le16(SS_RESIDUAL_UNDER);
5402 
5403 	/* Memory Barrier */
5404 	wmb();
5405 	if (qpair->reqq_start_iocbs)
5406 		qpair->reqq_start_iocbs(qpair);
5407 	else
5408 		qla2x00_start_iocbs(vha, qpair->req);
5409 	return 0;
5410 }
5411 
5412 /*
5413  * This routine is used to allocate a command for either a QFull condition
5414  * (ie reply SAM_STAT_BUSY) or to terminate an exchange that did not go
5415  * out previously.
5416  */
5417 static void
qlt_alloc_qfull_cmd(struct scsi_qla_host * vha,struct atio_from_isp * atio,uint16_t status,int qfull)5418 qlt_alloc_qfull_cmd(struct scsi_qla_host *vha,
5419 	struct atio_from_isp *atio, uint16_t status, int qfull)
5420 {
5421 	struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
5422 	struct qla_hw_data *ha = vha->hw;
5423 	struct fc_port *sess;
5424 	struct qla_tgt_cmd *cmd;
5425 	unsigned long flags;
5426 
5427 	if (unlikely(tgt->tgt_stop)) {
5428 		ql_dbg(ql_dbg_io, vha, 0x300a,
5429 			"New command while device %p is shutting down\n", tgt);
5430 		return;
5431 	}
5432 
5433 	if ((vha->hw->tgt.num_qfull_cmds_alloc + 1) > MAX_QFULL_CMDS_ALLOC) {
5434 		vha->hw->tgt.num_qfull_cmds_dropped++;
5435 		if (vha->hw->tgt.num_qfull_cmds_dropped >
5436 			vha->qla_stats.stat_max_qfull_cmds_dropped)
5437 			vha->qla_stats.stat_max_qfull_cmds_dropped =
5438 				vha->hw->tgt.num_qfull_cmds_dropped;
5439 
5440 		ql_dbg(ql_dbg_io, vha, 0x3068,
5441 			"qla_target(%d): %s: QFull CMD dropped[%d]\n",
5442 			vha->vp_idx, __func__,
5443 			vha->hw->tgt.num_qfull_cmds_dropped);
5444 
5445 		qlt_chk_exch_leak_thresh_hold(vha);
5446 		return;
5447 	}
5448 
5449 	sess = ha->tgt.tgt_ops->find_sess_by_s_id
5450 		(vha, atio->u.isp24.fcp_hdr.s_id);
5451 	if (!sess)
5452 		return;
5453 
5454 	cmd = ha->tgt.tgt_ops->get_cmd(sess);
5455 	if (!cmd) {
5456 		ql_dbg(ql_dbg_io, vha, 0x3009,
5457 			"qla_target(%d): %s: Allocation of cmd failed\n",
5458 			vha->vp_idx, __func__);
5459 
5460 		vha->hw->tgt.num_qfull_cmds_dropped++;
5461 		if (vha->hw->tgt.num_qfull_cmds_dropped >
5462 			vha->qla_stats.stat_max_qfull_cmds_dropped)
5463 			vha->qla_stats.stat_max_qfull_cmds_dropped =
5464 				vha->hw->tgt.num_qfull_cmds_dropped;
5465 
5466 		qlt_chk_exch_leak_thresh_hold(vha);
5467 		return;
5468 	}
5469 
5470 	qlt_incr_num_pend_cmds(vha);
5471 	INIT_LIST_HEAD(&cmd->cmd_list);
5472 	memcpy(&cmd->atio, atio, sizeof(*atio));
5473 
5474 	cmd->tgt = vha->vha_tgt.qla_tgt;
5475 	cmd->vha = vha;
5476 	cmd->reset_count = ha->base_qpair->chip_reset;
5477 	cmd->q_full = 1;
5478 	cmd->qpair = ha->base_qpair;
5479 
5480 	if (qfull) {
5481 		cmd->q_full = 1;
5482 		/* NOTE: borrowing the state field to carry the status */
5483 		cmd->state = status;
5484 	} else
5485 		cmd->term_exchg = 1;
5486 
5487 	spin_lock_irqsave(&vha->hw->tgt.q_full_lock, flags);
5488 	list_add_tail(&cmd->cmd_list, &vha->hw->tgt.q_full_list);
5489 
5490 	vha->hw->tgt.num_qfull_cmds_alloc++;
5491 	if (vha->hw->tgt.num_qfull_cmds_alloc >
5492 		vha->qla_stats.stat_max_qfull_cmds_alloc)
5493 		vha->qla_stats.stat_max_qfull_cmds_alloc =
5494 			vha->hw->tgt.num_qfull_cmds_alloc;
5495 	spin_unlock_irqrestore(&vha->hw->tgt.q_full_lock, flags);
5496 }
5497 
5498 static void
qlt_send_busy(struct qla_qpair * qpair,struct atio_from_isp * atio,uint16_t status)5499 qlt_send_busy(struct qla_qpair *qpair, struct atio_from_isp *atio,
5500     uint16_t status)
5501 {
5502 	int rc = 0;
5503 	struct scsi_qla_host *vha = qpair->vha;
5504 
5505 	rc = __qlt_send_busy(qpair, atio, status);
5506 	if (rc == -ENOMEM)
5507 		qlt_alloc_qfull_cmd(vha, atio, status, 1);
5508 }
5509 
5510 static int
qlt_chk_qfull_thresh_hold(struct scsi_qla_host * vha,struct qla_qpair * qpair,struct atio_from_isp * atio,uint8_t ha_locked)5511 qlt_chk_qfull_thresh_hold(struct scsi_qla_host *vha, struct qla_qpair *qpair,
5512 	struct atio_from_isp *atio, uint8_t ha_locked)
5513 {
5514 	struct qla_hw_data *ha = vha->hw;
5515 	unsigned long flags;
5516 
5517 	if (ha->tgt.num_pend_cmds < Q_FULL_THRESH_HOLD(ha))
5518 		return 0;
5519 
5520 	if (!ha_locked)
5521 		spin_lock_irqsave(&ha->hardware_lock, flags);
5522 	qlt_send_busy(qpair, atio, qla_sam_status);
5523 	if (!ha_locked)
5524 		spin_unlock_irqrestore(&ha->hardware_lock, flags);
5525 
5526 	return 1;
5527 }
5528 
5529 /* ha->hardware_lock supposed to be held on entry */
5530 /* called via callback from qla2xxx */
qlt_24xx_atio_pkt(struct scsi_qla_host * vha,struct atio_from_isp * atio,uint8_t ha_locked)5531 static void qlt_24xx_atio_pkt(struct scsi_qla_host *vha,
5532 	struct atio_from_isp *atio, uint8_t ha_locked)
5533 {
5534 	struct qla_hw_data *ha = vha->hw;
5535 	struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
5536 	int rc;
5537 	unsigned long flags = 0;
5538 
5539 	if (unlikely(tgt == NULL)) {
5540 		ql_dbg(ql_dbg_tgt, vha, 0x3064,
5541 		    "ATIO pkt, but no tgt (ha %p)", ha);
5542 		return;
5543 	}
5544 	/*
5545 	 * In tgt_stop mode we also should allow all requests to pass.
5546 	 * Otherwise, some commands can stuck.
5547 	 */
5548 
5549 	tgt->atio_irq_cmd_count++;
5550 
5551 	switch (atio->u.raw.entry_type) {
5552 	case ATIO_TYPE7:
5553 		if (unlikely(atio->u.isp24.exchange_addr ==
5554 			     cpu_to_le32(ATIO_EXCHANGE_ADDRESS_UNKNOWN))) {
5555 			ql_dbg(ql_dbg_io, vha, 0x3065,
5556 			    "qla_target(%d): ATIO_TYPE7 "
5557 			    "received with UNKNOWN exchange address, "
5558 			    "sending QUEUE_FULL\n", vha->vp_idx);
5559 			if (!ha_locked)
5560 				spin_lock_irqsave(&ha->hardware_lock, flags);
5561 			qlt_send_busy(ha->base_qpair, atio, qla_sam_status);
5562 			if (!ha_locked)
5563 				spin_unlock_irqrestore(&ha->hardware_lock,
5564 				    flags);
5565 			break;
5566 		}
5567 
5568 		if (likely(atio->u.isp24.fcp_cmnd.task_mgmt_flags == 0)) {
5569 			rc = qlt_chk_qfull_thresh_hold(vha, ha->base_qpair,
5570 			    atio, ha_locked);
5571 			if (rc != 0) {
5572 				tgt->atio_irq_cmd_count--;
5573 				return;
5574 			}
5575 			rc = qlt_handle_cmd_for_atio(vha, atio);
5576 		} else {
5577 			rc = qlt_handle_task_mgmt(vha, atio);
5578 		}
5579 		if (unlikely(rc != 0)) {
5580 			if (!ha_locked)
5581 				spin_lock_irqsave(&ha->hardware_lock, flags);
5582 			switch (rc) {
5583 			case -ENODEV:
5584 				ql_dbg(ql_dbg_tgt, vha, 0xe05f,
5585 				    "qla_target: Unable to send command to target\n");
5586 				break;
5587 			case -EBADF:
5588 				ql_dbg(ql_dbg_tgt, vha, 0xe05f,
5589 				    "qla_target: Unable to send command to target, sending TERM EXCHANGE for rsp\n");
5590 				qlt_send_term_exchange(ha->base_qpair, NULL,
5591 				    atio, 1, 0);
5592 				break;
5593 			case -EBUSY:
5594 				ql_dbg(ql_dbg_tgt, vha, 0xe060,
5595 				    "qla_target(%d): Unable to send command to target, sending BUSY status\n",
5596 				    vha->vp_idx);
5597 				qlt_send_busy(ha->base_qpair, atio,
5598 				    tc_sam_status);
5599 				break;
5600 			default:
5601 				ql_dbg(ql_dbg_tgt, vha, 0xe060,
5602 				    "qla_target(%d): Unable to send command to target, sending BUSY status\n",
5603 				    vha->vp_idx);
5604 				qlt_send_busy(ha->base_qpair, atio,
5605 				    qla_sam_status);
5606 				break;
5607 			}
5608 			if (!ha_locked)
5609 				spin_unlock_irqrestore(&ha->hardware_lock,
5610 				    flags);
5611 		}
5612 		break;
5613 
5614 	case IMMED_NOTIFY_TYPE:
5615 	{
5616 		if (unlikely(atio->u.isp2x.entry_status != 0)) {
5617 			ql_dbg(ql_dbg_tgt, vha, 0xe05b,
5618 			    "qla_target(%d): Received ATIO packet %x "
5619 			    "with error status %x\n", vha->vp_idx,
5620 			    atio->u.raw.entry_type,
5621 			    atio->u.isp2x.entry_status);
5622 			break;
5623 		}
5624 		ql_dbg(ql_dbg_tgt, vha, 0xe02e, "%s", "IMMED_NOTIFY ATIO");
5625 
5626 		if (!ha_locked)
5627 			spin_lock_irqsave(&ha->hardware_lock, flags);
5628 		qlt_handle_imm_notify(vha, (struct imm_ntfy_from_isp *)atio);
5629 		if (!ha_locked)
5630 			spin_unlock_irqrestore(&ha->hardware_lock, flags);
5631 		break;
5632 	}
5633 
5634 	default:
5635 		ql_dbg(ql_dbg_tgt, vha, 0xe05c,
5636 		    "qla_target(%d): Received unknown ATIO atio "
5637 		    "type %x\n", vha->vp_idx, atio->u.raw.entry_type);
5638 		break;
5639 	}
5640 
5641 	tgt->atio_irq_cmd_count--;
5642 }
5643 
5644 /*
5645  * qpair lock is assume to be held
5646  * rc = 0 : send terminate & abts respond
5647  * rc != 0: do not send term & abts respond
5648  */
qlt_chk_unresolv_exchg(struct scsi_qla_host * vha,struct qla_qpair * qpair,struct abts_resp_from_24xx_fw * entry)5649 static int qlt_chk_unresolv_exchg(struct scsi_qla_host *vha,
5650     struct qla_qpair *qpair, struct abts_resp_from_24xx_fw *entry)
5651 {
5652 	struct qla_hw_data *ha = vha->hw;
5653 	int rc = 0;
5654 
5655 	/*
5656 	 * Detect unresolved exchange. If the same ABTS is unable
5657 	 * to terminate an existing command and the same ABTS loops
5658 	 * between FW & Driver, then force FW dump. Under 1 jiff,
5659 	 * we should see multiple loops.
5660 	 */
5661 	if (qpair->retry_term_exchg_addr == entry->exchange_addr_to_abort &&
5662 	    qpair->retry_term_jiff == jiffies) {
5663 		/* found existing exchange */
5664 		qpair->retry_term_cnt++;
5665 		if (qpair->retry_term_cnt >= 5) {
5666 			rc = -EIO;
5667 			qpair->retry_term_cnt = 0;
5668 			ql_log(ql_log_warn, vha, 0xffff,
5669 			    "Unable to send ABTS Respond. Dumping firmware.\n");
5670 			ql_dump_buffer(ql_dbg_tgt_mgt + ql_dbg_buffer,
5671 			    vha, 0xffff, (uint8_t *)entry, sizeof(*entry));
5672 
5673 			if (qpair == ha->base_qpair)
5674 				ha->isp_ops->fw_dump(vha);
5675 			else
5676 				qla2xxx_dump_fw(vha);
5677 
5678 			set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
5679 			qla2xxx_wake_dpc(vha);
5680 		}
5681 	} else if (qpair->retry_term_jiff != jiffies) {
5682 		qpair->retry_term_exchg_addr = entry->exchange_addr_to_abort;
5683 		qpair->retry_term_cnt = 0;
5684 		qpair->retry_term_jiff = jiffies;
5685 	}
5686 
5687 	return rc;
5688 }
5689 
5690 
qlt_handle_abts_completion(struct scsi_qla_host * vha,struct rsp_que * rsp,response_t * pkt)5691 static void qlt_handle_abts_completion(struct scsi_qla_host *vha,
5692 	struct rsp_que *rsp, response_t *pkt)
5693 {
5694 	struct abts_resp_from_24xx_fw *entry =
5695 		(struct abts_resp_from_24xx_fw *)pkt;
5696 	u32 h = pkt->handle & ~QLA_TGT_HANDLE_MASK;
5697 	struct qla_tgt_mgmt_cmd *mcmd;
5698 	struct qla_hw_data *ha = vha->hw;
5699 
5700 	mcmd = qlt_ctio_to_cmd(vha, rsp, pkt->handle, pkt);
5701 	if (mcmd == NULL && h != QLA_TGT_SKIP_HANDLE) {
5702 		ql_dbg(ql_dbg_async, vha, 0xe064,
5703 		    "qla_target(%d): ABTS Comp without mcmd\n",
5704 		    vha->vp_idx);
5705 		return;
5706 	}
5707 
5708 	if (mcmd)
5709 		vha  = mcmd->vha;
5710 	vha->vha_tgt.qla_tgt->abts_resp_expected--;
5711 
5712 	ql_dbg(ql_dbg_tgt, vha, 0xe038,
5713 	    "ABTS_RESP_24XX: compl_status %x\n",
5714 	    entry->compl_status);
5715 
5716 	if (le16_to_cpu(entry->compl_status) != ABTS_RESP_COMPL_SUCCESS) {
5717 		if (le32_to_cpu(entry->error_subcode1) == 0x1E &&
5718 		    le32_to_cpu(entry->error_subcode2) == 0) {
5719 			if (qlt_chk_unresolv_exchg(vha, rsp->qpair, entry)) {
5720 				ha->tgt.tgt_ops->free_mcmd(mcmd);
5721 				return;
5722 			}
5723 			qlt_24xx_retry_term_exchange(vha, rsp->qpair,
5724 			    pkt, mcmd);
5725 		} else {
5726 			ql_dbg(ql_dbg_tgt, vha, 0xe063,
5727 			    "qla_target(%d): ABTS_RESP_24XX failed %x (subcode %x:%x)",
5728 			    vha->vp_idx, entry->compl_status,
5729 			    entry->error_subcode1,
5730 			    entry->error_subcode2);
5731 			ha->tgt.tgt_ops->free_mcmd(mcmd);
5732 		}
5733 	} else if (mcmd) {
5734 		ha->tgt.tgt_ops->free_mcmd(mcmd);
5735 	}
5736 }
5737 
5738 /* ha->hardware_lock supposed to be held on entry */
5739 /* called via callback from qla2xxx */
qlt_response_pkt(struct scsi_qla_host * vha,struct rsp_que * rsp,response_t * pkt)5740 static void qlt_response_pkt(struct scsi_qla_host *vha,
5741 	struct rsp_que *rsp, response_t *pkt)
5742 {
5743 	struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
5744 
5745 	if (unlikely(tgt == NULL)) {
5746 		ql_dbg(ql_dbg_tgt, vha, 0xe05d,
5747 		    "qla_target(%d): Response pkt %x received, but no tgt (ha %p)\n",
5748 		    vha->vp_idx, pkt->entry_type, vha->hw);
5749 		return;
5750 	}
5751 
5752 	/*
5753 	 * In tgt_stop mode we also should allow all requests to pass.
5754 	 * Otherwise, some commands can stuck.
5755 	 */
5756 
5757 	switch (pkt->entry_type) {
5758 	case CTIO_CRC2:
5759 	case CTIO_TYPE7:
5760 	{
5761 		struct ctio7_from_24xx *entry = (struct ctio7_from_24xx *)pkt;
5762 
5763 		qlt_do_ctio_completion(vha, rsp, entry->handle,
5764 		    le16_to_cpu(entry->status)|(pkt->entry_status << 16),
5765 		    entry);
5766 		break;
5767 	}
5768 
5769 	case ACCEPT_TGT_IO_TYPE:
5770 	{
5771 		struct atio_from_isp *atio = (struct atio_from_isp *)pkt;
5772 		int rc;
5773 
5774 		if (atio->u.isp2x.status !=
5775 		    cpu_to_le16(ATIO_CDB_VALID)) {
5776 			ql_dbg(ql_dbg_tgt, vha, 0xe05e,
5777 			    "qla_target(%d): ATIO with error "
5778 			    "status %x received\n", vha->vp_idx,
5779 			    le16_to_cpu(atio->u.isp2x.status));
5780 			break;
5781 		}
5782 
5783 		rc = qlt_chk_qfull_thresh_hold(vha, rsp->qpair, atio, 1);
5784 		if (rc != 0)
5785 			return;
5786 
5787 		rc = qlt_handle_cmd_for_atio(vha, atio);
5788 		if (unlikely(rc != 0)) {
5789 			switch (rc) {
5790 			case -ENODEV:
5791 				ql_dbg(ql_dbg_tgt, vha, 0xe05f,
5792 				    "qla_target: Unable to send command to target\n");
5793 				break;
5794 			case -EBADF:
5795 				ql_dbg(ql_dbg_tgt, vha, 0xe05f,
5796 				    "qla_target: Unable to send command to target, sending TERM EXCHANGE for rsp\n");
5797 				qlt_send_term_exchange(rsp->qpair, NULL,
5798 				    atio, 1, 0);
5799 				break;
5800 			case -EBUSY:
5801 				ql_dbg(ql_dbg_tgt, vha, 0xe060,
5802 				    "qla_target(%d): Unable to send command to target, sending BUSY status\n",
5803 				    vha->vp_idx);
5804 				qlt_send_busy(rsp->qpair, atio,
5805 				    tc_sam_status);
5806 				break;
5807 			default:
5808 				ql_dbg(ql_dbg_tgt, vha, 0xe060,
5809 				    "qla_target(%d): Unable to send command to target, sending BUSY status\n",
5810 				    vha->vp_idx);
5811 				qlt_send_busy(rsp->qpair, atio,
5812 				    qla_sam_status);
5813 				break;
5814 			}
5815 		}
5816 	}
5817 	break;
5818 
5819 	case CONTINUE_TGT_IO_TYPE:
5820 	{
5821 		struct ctio_to_2xxx *entry = (struct ctio_to_2xxx *)pkt;
5822 
5823 		qlt_do_ctio_completion(vha, rsp, entry->handle,
5824 		    le16_to_cpu(entry->status)|(pkt->entry_status << 16),
5825 		    entry);
5826 		break;
5827 	}
5828 
5829 	case CTIO_A64_TYPE:
5830 	{
5831 		struct ctio_to_2xxx *entry = (struct ctio_to_2xxx *)pkt;
5832 
5833 		qlt_do_ctio_completion(vha, rsp, entry->handle,
5834 		    le16_to_cpu(entry->status)|(pkt->entry_status << 16),
5835 		    entry);
5836 		break;
5837 	}
5838 
5839 	case IMMED_NOTIFY_TYPE:
5840 		ql_dbg(ql_dbg_tgt, vha, 0xe035, "%s", "IMMED_NOTIFY\n");
5841 		qlt_handle_imm_notify(vha, (struct imm_ntfy_from_isp *)pkt);
5842 		break;
5843 
5844 	case NOTIFY_ACK_TYPE:
5845 		if (tgt->notify_ack_expected > 0) {
5846 			struct nack_to_isp *entry = (struct nack_to_isp *)pkt;
5847 
5848 			ql_dbg(ql_dbg_tgt, vha, 0xe036,
5849 			    "NOTIFY_ACK seq %08x status %x\n",
5850 			    le16_to_cpu(entry->u.isp2x.seq_id),
5851 			    le16_to_cpu(entry->u.isp2x.status));
5852 			tgt->notify_ack_expected--;
5853 			if (entry->u.isp2x.status !=
5854 			    cpu_to_le16(NOTIFY_ACK_SUCCESS)) {
5855 				ql_dbg(ql_dbg_tgt, vha, 0xe061,
5856 				    "qla_target(%d): NOTIFY_ACK "
5857 				    "failed %x\n", vha->vp_idx,
5858 				    le16_to_cpu(entry->u.isp2x.status));
5859 			}
5860 		} else {
5861 			ql_dbg(ql_dbg_tgt, vha, 0xe062,
5862 			    "qla_target(%d): Unexpected NOTIFY_ACK received\n",
5863 			    vha->vp_idx);
5864 		}
5865 		break;
5866 
5867 	case ABTS_RECV_24XX:
5868 		ql_dbg(ql_dbg_tgt, vha, 0xe037,
5869 		    "ABTS_RECV_24XX: instance %d\n", vha->vp_idx);
5870 		qlt_24xx_handle_abts(vha, (struct abts_recv_from_24xx *)pkt);
5871 		break;
5872 
5873 	case ABTS_RESP_24XX:
5874 		if (tgt->abts_resp_expected > 0) {
5875 			qlt_handle_abts_completion(vha, rsp, pkt);
5876 		} else {
5877 			ql_dbg(ql_dbg_tgt, vha, 0xe064,
5878 			    "qla_target(%d): Unexpected ABTS_RESP_24XX "
5879 			    "received\n", vha->vp_idx);
5880 		}
5881 		break;
5882 
5883 	default:
5884 		ql_dbg(ql_dbg_tgt, vha, 0xe065,
5885 		    "qla_target(%d): Received unknown response pkt "
5886 		    "type %x\n", vha->vp_idx, pkt->entry_type);
5887 		break;
5888 	}
5889 
5890 }
5891 
5892 /*
5893  * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
5894  */
qlt_async_event(uint16_t code,struct scsi_qla_host * vha,uint16_t * mailbox)5895 void qlt_async_event(uint16_t code, struct scsi_qla_host *vha,
5896 	uint16_t *mailbox)
5897 {
5898 	struct qla_hw_data *ha = vha->hw;
5899 	struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
5900 	int login_code;
5901 
5902 	if (!tgt || tgt->tgt_stop || tgt->tgt_stopped)
5903 		return;
5904 
5905 	if (((code == MBA_POINT_TO_POINT) || (code == MBA_CHG_IN_CONNECTION)) &&
5906 	    IS_QLA2100(ha))
5907 		return;
5908 	/*
5909 	 * In tgt_stop mode we also should allow all requests to pass.
5910 	 * Otherwise, some commands can stuck.
5911 	 */
5912 
5913 
5914 	switch (code) {
5915 	case MBA_RESET:			/* Reset */
5916 	case MBA_SYSTEM_ERR:		/* System Error */
5917 	case MBA_REQ_TRANSFER_ERR:	/* Request Transfer Error */
5918 	case MBA_RSP_TRANSFER_ERR:	/* Response Transfer Error */
5919 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03a,
5920 		    "qla_target(%d): System error async event %#x "
5921 		    "occurred", vha->vp_idx, code);
5922 		break;
5923 	case MBA_WAKEUP_THRES:		/* Request Queue Wake-up. */
5924 		set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
5925 		break;
5926 
5927 	case MBA_LOOP_UP:
5928 	{
5929 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03b,
5930 		    "qla_target(%d): Async LOOP_UP occurred "
5931 		    "(m[0]=%x, m[1]=%x, m[2]=%x, m[3]=%x)", vha->vp_idx,
5932 		    mailbox[0], mailbox[1], mailbox[2], mailbox[3]);
5933 		if (tgt->link_reinit_iocb_pending) {
5934 			qlt_send_notify_ack(ha->base_qpair,
5935 			    &tgt->link_reinit_iocb,
5936 			    0, 0, 0, 0, 0, 0);
5937 			tgt->link_reinit_iocb_pending = 0;
5938 		}
5939 		break;
5940 	}
5941 
5942 	case MBA_LIP_OCCURRED:
5943 	case MBA_LOOP_DOWN:
5944 	case MBA_LIP_RESET:
5945 	case MBA_RSCN_UPDATE:
5946 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03c,
5947 		    "qla_target(%d): Async event %#x occurred "
5948 		    "(m[0]=%x, m[1]=%x, m[2]=%x, m[3]=%x)", vha->vp_idx, code,
5949 		    mailbox[0], mailbox[1], mailbox[2], mailbox[3]);
5950 		break;
5951 
5952 	case MBA_REJECTED_FCP_CMD:
5953 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf017,
5954 		    "qla_target(%d): Async event LS_REJECT occurred (m[0]=%x, m[1]=%x, m[2]=%x, m[3]=%x)",
5955 		    vha->vp_idx,
5956 		    mailbox[0], mailbox[1], mailbox[2], mailbox[3]);
5957 
5958 		if (mailbox[3] == 1) {
5959 			/* exchange starvation. */
5960 			vha->hw->exch_starvation++;
5961 			if (vha->hw->exch_starvation > 5) {
5962 				ql_log(ql_log_warn, vha, 0xd03a,
5963 				    "Exchange starvation-. Resetting RISC\n");
5964 
5965 				vha->hw->exch_starvation = 0;
5966 				if (IS_P3P_TYPE(vha->hw))
5967 					set_bit(FCOE_CTX_RESET_NEEDED,
5968 					    &vha->dpc_flags);
5969 				else
5970 					set_bit(ISP_ABORT_NEEDED,
5971 					    &vha->dpc_flags);
5972 				qla2xxx_wake_dpc(vha);
5973 			}
5974 		}
5975 		break;
5976 
5977 	case MBA_PORT_UPDATE:
5978 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03d,
5979 		    "qla_target(%d): Port update async event %#x "
5980 		    "occurred: updating the ports database (m[0]=%x, m[1]=%x, "
5981 		    "m[2]=%x, m[3]=%x)", vha->vp_idx, code,
5982 		    mailbox[0], mailbox[1], mailbox[2], mailbox[3]);
5983 
5984 		login_code = mailbox[2];
5985 		if (login_code == 0x4) {
5986 			ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03e,
5987 			    "Async MB 2: Got PLOGI Complete\n");
5988 			vha->hw->exch_starvation = 0;
5989 		} else if (login_code == 0x7)
5990 			ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03f,
5991 			    "Async MB 2: Port Logged Out\n");
5992 		break;
5993 	default:
5994 		break;
5995 	}
5996 
5997 }
5998 
qlt_get_port_database(struct scsi_qla_host * vha,uint16_t loop_id)5999 static fc_port_t *qlt_get_port_database(struct scsi_qla_host *vha,
6000 	uint16_t loop_id)
6001 {
6002 	fc_port_t *fcport, *tfcp, *del;
6003 	int rc;
6004 	unsigned long flags;
6005 	u8 newfcport = 0;
6006 
6007 	fcport = qla2x00_alloc_fcport(vha, GFP_KERNEL);
6008 	if (!fcport) {
6009 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06f,
6010 		    "qla_target(%d): Allocation of tmp FC port failed",
6011 		    vha->vp_idx);
6012 		return NULL;
6013 	}
6014 
6015 	fcport->loop_id = loop_id;
6016 
6017 	rc = qla24xx_gpdb_wait(vha, fcport, 0);
6018 	if (rc != QLA_SUCCESS) {
6019 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf070,
6020 		    "qla_target(%d): Failed to retrieve fcport "
6021 		    "information -- get_port_database() returned %x "
6022 		    "(loop_id=0x%04x)", vha->vp_idx, rc, loop_id);
6023 		kfree(fcport);
6024 		return NULL;
6025 	}
6026 
6027 	del = NULL;
6028 	spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
6029 	tfcp = qla2x00_find_fcport_by_wwpn(vha, fcport->port_name, 1);
6030 
6031 	if (tfcp) {
6032 		tfcp->d_id = fcport->d_id;
6033 		tfcp->port_type = fcport->port_type;
6034 		tfcp->supported_classes = fcport->supported_classes;
6035 		tfcp->flags |= fcport->flags;
6036 		tfcp->scan_state = QLA_FCPORT_FOUND;
6037 
6038 		del = fcport;
6039 		fcport = tfcp;
6040 	} else {
6041 		if (vha->hw->current_topology == ISP_CFG_F)
6042 			fcport->flags |= FCF_FABRIC_DEVICE;
6043 
6044 		list_add_tail(&fcport->list, &vha->vp_fcports);
6045 		if (!IS_SW_RESV_ADDR(fcport->d_id))
6046 		   vha->fcport_count++;
6047 		fcport->login_gen++;
6048 		qla2x00_set_fcport_disc_state(fcport, DSC_LOGIN_COMPLETE);
6049 		fcport->login_succ = 1;
6050 		newfcport = 1;
6051 	}
6052 
6053 	fcport->deleted = 0;
6054 	spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
6055 
6056 	switch (vha->host->active_mode) {
6057 	case MODE_INITIATOR:
6058 	case MODE_DUAL:
6059 		if (newfcport) {
6060 			if (!IS_IIDMA_CAPABLE(vha->hw) || !vha->hw->flags.gpsc_supported) {
6061 				qla24xx_sched_upd_fcport(fcport);
6062 			} else {
6063 				ql_dbg(ql_dbg_disc, vha, 0x20ff,
6064 				   "%s %d %8phC post gpsc fcp_cnt %d\n",
6065 				   __func__, __LINE__, fcport->port_name, vha->fcport_count);
6066 				qla24xx_post_gpsc_work(vha, fcport);
6067 			}
6068 		}
6069 		break;
6070 
6071 	case MODE_TARGET:
6072 	default:
6073 		break;
6074 	}
6075 	if (del)
6076 		qla2x00_free_fcport(del);
6077 
6078 	return fcport;
6079 }
6080 
6081 /* Must be called under tgt_mutex */
qlt_make_local_sess(struct scsi_qla_host * vha,be_id_t s_id)6082 static struct fc_port *qlt_make_local_sess(struct scsi_qla_host *vha,
6083 					   be_id_t s_id)
6084 {
6085 	struct fc_port *sess = NULL;
6086 	fc_port_t *fcport = NULL;
6087 	int rc, global_resets;
6088 	uint16_t loop_id = 0;
6089 
6090 	if (s_id.domain == 0xFF && s_id.area == 0xFC) {
6091 		/*
6092 		 * This is Domain Controller, so it should be
6093 		 * OK to drop SCSI commands from it.
6094 		 */
6095 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf042,
6096 		    "Unable to find initiator with S_ID %x:%x:%x",
6097 		    s_id.domain, s_id.area, s_id.al_pa);
6098 		return NULL;
6099 	}
6100 
6101 	mutex_lock(&vha->vha_tgt.tgt_mutex);
6102 
6103 retry:
6104 	global_resets =
6105 	    atomic_read(&vha->vha_tgt.qla_tgt->tgt_global_resets_count);
6106 
6107 	rc = qla24xx_get_loop_id(vha, s_id, &loop_id);
6108 	if (rc != 0) {
6109 		mutex_unlock(&vha->vha_tgt.tgt_mutex);
6110 
6111 		ql_log(ql_log_info, vha, 0xf071,
6112 		    "qla_target(%d): Unable to find "
6113 		    "initiator with S_ID %x:%x:%x",
6114 		    vha->vp_idx, s_id.domain, s_id.area, s_id.al_pa);
6115 
6116 		if (rc == -ENOENT) {
6117 			qlt_port_logo_t logo;
6118 
6119 			logo.id = be_to_port_id(s_id);
6120 			logo.cmd_count = 1;
6121 			qlt_send_first_logo(vha, &logo);
6122 		}
6123 
6124 		return NULL;
6125 	}
6126 
6127 	fcport = qlt_get_port_database(vha, loop_id);
6128 	if (!fcport) {
6129 		mutex_unlock(&vha->vha_tgt.tgt_mutex);
6130 		return NULL;
6131 	}
6132 
6133 	if (global_resets !=
6134 	    atomic_read(&vha->vha_tgt.qla_tgt->tgt_global_resets_count)) {
6135 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf043,
6136 		    "qla_target(%d): global reset during session discovery "
6137 		    "(counter was %d, new %d), retrying", vha->vp_idx,
6138 		    global_resets,
6139 		    atomic_read(&vha->vha_tgt.
6140 			qla_tgt->tgt_global_resets_count));
6141 		goto retry;
6142 	}
6143 
6144 	sess = qlt_create_sess(vha, fcport, true);
6145 
6146 	mutex_unlock(&vha->vha_tgt.tgt_mutex);
6147 
6148 	return sess;
6149 }
6150 
qlt_abort_work(struct qla_tgt * tgt,struct qla_tgt_sess_work_param * prm)6151 static void qlt_abort_work(struct qla_tgt *tgt,
6152 	struct qla_tgt_sess_work_param *prm)
6153 {
6154 	struct scsi_qla_host *vha = tgt->vha;
6155 	struct qla_hw_data *ha = vha->hw;
6156 	struct fc_port *sess = NULL;
6157 	unsigned long flags = 0, flags2 = 0;
6158 	be_id_t s_id;
6159 	int rc;
6160 
6161 	spin_lock_irqsave(&ha->tgt.sess_lock, flags2);
6162 
6163 	if (tgt->tgt_stop)
6164 		goto out_term2;
6165 
6166 	s_id = le_id_to_be(prm->abts.fcp_hdr_le.s_id);
6167 
6168 	sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha, s_id);
6169 	if (!sess) {
6170 		spin_unlock_irqrestore(&ha->tgt.sess_lock, flags2);
6171 
6172 		sess = qlt_make_local_sess(vha, s_id);
6173 		/* sess has got an extra creation ref */
6174 
6175 		spin_lock_irqsave(&ha->tgt.sess_lock, flags2);
6176 		if (!sess)
6177 			goto out_term2;
6178 	} else {
6179 		if (sess->deleted) {
6180 			sess = NULL;
6181 			goto out_term2;
6182 		}
6183 
6184 		if (!kref_get_unless_zero(&sess->sess_kref)) {
6185 			ql_dbg(ql_dbg_tgt_tmr, vha, 0xf01c,
6186 			    "%s: kref_get fail %8phC \n",
6187 			     __func__, sess->port_name);
6188 			sess = NULL;
6189 			goto out_term2;
6190 		}
6191 	}
6192 
6193 	rc = __qlt_24xx_handle_abts(vha, &prm->abts, sess);
6194 	spin_unlock_irqrestore(&ha->tgt.sess_lock, flags2);
6195 
6196 	ha->tgt.tgt_ops->put_sess(sess);
6197 
6198 	if (rc != 0)
6199 		goto out_term;
6200 	return;
6201 
6202 out_term2:
6203 	spin_unlock_irqrestore(&ha->tgt.sess_lock, flags2);
6204 
6205 out_term:
6206 	spin_lock_irqsave(&ha->hardware_lock, flags);
6207 	qlt_24xx_send_abts_resp(ha->base_qpair, &prm->abts,
6208 	    FCP_TMF_REJECTED, false);
6209 	spin_unlock_irqrestore(&ha->hardware_lock, flags);
6210 }
6211 
qlt_sess_work_fn(struct work_struct * work)6212 static void qlt_sess_work_fn(struct work_struct *work)
6213 {
6214 	struct qla_tgt *tgt = container_of(work, struct qla_tgt, sess_work);
6215 	struct scsi_qla_host *vha = tgt->vha;
6216 	unsigned long flags;
6217 
6218 	ql_dbg(ql_dbg_tgt_mgt, vha, 0xf000, "Sess work (tgt %p)", tgt);
6219 
6220 	spin_lock_irqsave(&tgt->sess_work_lock, flags);
6221 	while (!list_empty(&tgt->sess_works_list)) {
6222 		struct qla_tgt_sess_work_param *prm = list_entry(
6223 		    tgt->sess_works_list.next, typeof(*prm),
6224 		    sess_works_list_entry);
6225 
6226 		/*
6227 		 * This work can be scheduled on several CPUs at time, so we
6228 		 * must delete the entry to eliminate double processing
6229 		 */
6230 		list_del(&prm->sess_works_list_entry);
6231 
6232 		spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
6233 
6234 		switch (prm->type) {
6235 		case QLA_TGT_SESS_WORK_ABORT:
6236 			qlt_abort_work(tgt, prm);
6237 			break;
6238 		default:
6239 			BUG_ON(1);
6240 			break;
6241 		}
6242 
6243 		spin_lock_irqsave(&tgt->sess_work_lock, flags);
6244 
6245 		kfree(prm);
6246 	}
6247 	spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
6248 }
6249 
6250 /* Must be called under tgt_host_action_mutex */
qlt_add_target(struct qla_hw_data * ha,struct scsi_qla_host * base_vha)6251 int qlt_add_target(struct qla_hw_data *ha, struct scsi_qla_host *base_vha)
6252 {
6253 	struct qla_tgt *tgt;
6254 	int rc, i;
6255 	struct qla_qpair_hint *h;
6256 
6257 	if (!QLA_TGT_MODE_ENABLED())
6258 		return 0;
6259 
6260 	if (!IS_TGT_MODE_CAPABLE(ha)) {
6261 		ql_log(ql_log_warn, base_vha, 0xe070,
6262 		    "This adapter does not support target mode.\n");
6263 		return 0;
6264 	}
6265 
6266 	ql_dbg(ql_dbg_tgt, base_vha, 0xe03b,
6267 	    "Registering target for host %ld(%p).\n", base_vha->host_no, ha);
6268 
6269 	BUG_ON(base_vha->vha_tgt.qla_tgt != NULL);
6270 
6271 	tgt = kzalloc(sizeof(struct qla_tgt), GFP_KERNEL);
6272 	if (!tgt) {
6273 		ql_dbg(ql_dbg_tgt, base_vha, 0xe066,
6274 		    "Unable to allocate struct qla_tgt\n");
6275 		return -ENOMEM;
6276 	}
6277 
6278 	tgt->qphints = kcalloc(ha->max_qpairs + 1,
6279 			       sizeof(struct qla_qpair_hint),
6280 			       GFP_KERNEL);
6281 	if (!tgt->qphints) {
6282 		kfree(tgt);
6283 		ql_log(ql_log_warn, base_vha, 0x0197,
6284 		    "Unable to allocate qpair hints.\n");
6285 		return -ENOMEM;
6286 	}
6287 
6288 	qla2xxx_driver_template.supported_mode |= MODE_TARGET;
6289 
6290 	rc = btree_init64(&tgt->lun_qpair_map);
6291 	if (rc) {
6292 		kfree(tgt->qphints);
6293 		kfree(tgt);
6294 		ql_log(ql_log_info, base_vha, 0x0198,
6295 			"Unable to initialize lun_qpair_map btree\n");
6296 		return -EIO;
6297 	}
6298 	h = &tgt->qphints[0];
6299 	h->qpair = ha->base_qpair;
6300 	INIT_LIST_HEAD(&h->hint_elem);
6301 	h->cpuid = ha->base_qpair->cpuid;
6302 	list_add_tail(&h->hint_elem, &ha->base_qpair->hints_list);
6303 
6304 	for (i = 0; i < ha->max_qpairs; i++) {
6305 		unsigned long flags;
6306 
6307 		struct qla_qpair *qpair = ha->queue_pair_map[i];
6308 
6309 		h = &tgt->qphints[i + 1];
6310 		INIT_LIST_HEAD(&h->hint_elem);
6311 		if (qpair) {
6312 			h->qpair = qpair;
6313 			spin_lock_irqsave(qpair->qp_lock_ptr, flags);
6314 			list_add_tail(&h->hint_elem, &qpair->hints_list);
6315 			spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
6316 			h->cpuid = qpair->cpuid;
6317 		}
6318 	}
6319 
6320 	tgt->ha = ha;
6321 	tgt->vha = base_vha;
6322 	init_waitqueue_head(&tgt->waitQ);
6323 	spin_lock_init(&tgt->sess_work_lock);
6324 	INIT_WORK(&tgt->sess_work, qlt_sess_work_fn);
6325 	INIT_LIST_HEAD(&tgt->sess_works_list);
6326 	atomic_set(&tgt->tgt_global_resets_count, 0);
6327 
6328 	base_vha->vha_tgt.qla_tgt = tgt;
6329 
6330 	ql_dbg(ql_dbg_tgt, base_vha, 0xe067,
6331 		"qla_target(%d): using 64 Bit PCI addressing",
6332 		base_vha->vp_idx);
6333 	/* 3 is reserved */
6334 	tgt->sg_tablesize = QLA_TGT_MAX_SG_24XX(base_vha->req->length - 3);
6335 
6336 	mutex_lock(&qla_tgt_mutex);
6337 	list_add_tail(&tgt->tgt_list_entry, &qla_tgt_glist);
6338 	mutex_unlock(&qla_tgt_mutex);
6339 
6340 	if (ha->tgt.tgt_ops && ha->tgt.tgt_ops->add_target)
6341 		ha->tgt.tgt_ops->add_target(base_vha);
6342 
6343 	return 0;
6344 }
6345 
6346 /* Must be called under tgt_host_action_mutex */
qlt_remove_target(struct qla_hw_data * ha,struct scsi_qla_host * vha)6347 int qlt_remove_target(struct qla_hw_data *ha, struct scsi_qla_host *vha)
6348 {
6349 	if (!vha->vha_tgt.qla_tgt)
6350 		return 0;
6351 
6352 	if (vha->fc_vport) {
6353 		qlt_release(vha->vha_tgt.qla_tgt);
6354 		return 0;
6355 	}
6356 
6357 	/* free left over qfull cmds */
6358 	qlt_init_term_exchange(vha);
6359 
6360 	ql_dbg(ql_dbg_tgt, vha, 0xe03c, "Unregistering target for host %ld(%p)",
6361 	    vha->host_no, ha);
6362 	qlt_release(vha->vha_tgt.qla_tgt);
6363 
6364 	return 0;
6365 }
6366 
qla_remove_hostmap(struct qla_hw_data * ha)6367 void qla_remove_hostmap(struct qla_hw_data *ha)
6368 {
6369 	struct scsi_qla_host *node;
6370 	u32 key = 0;
6371 
6372 	btree_for_each_safe32(&ha->host_map, key, node)
6373 		btree_remove32(&ha->host_map, key);
6374 
6375 	btree_destroy32(&ha->host_map);
6376 }
6377 
qlt_lport_dump(struct scsi_qla_host * vha,u64 wwpn,unsigned char * b)6378 static void qlt_lport_dump(struct scsi_qla_host *vha, u64 wwpn,
6379 	unsigned char *b)
6380 {
6381 	pr_debug("qla2xxx HW vha->node_name: %8phC\n", vha->node_name);
6382 	pr_debug("qla2xxx HW vha->port_name: %8phC\n", vha->port_name);
6383 	put_unaligned_be64(wwpn, b);
6384 	pr_debug("qla2xxx passed configfs WWPN: %8phC\n", b);
6385 }
6386 
6387 /**
6388  * qlt_lport_register - register lport with external module
6389  *
6390  * @target_lport_ptr: pointer for tcm_qla2xxx specific lport data
6391  * @phys_wwpn: physical port WWPN
6392  * @npiv_wwpn: NPIV WWPN
6393  * @npiv_wwnn: NPIV WWNN
6394  * @callback:  lport initialization callback for tcm_qla2xxx code
6395  */
qlt_lport_register(void * target_lport_ptr,u64 phys_wwpn,u64 npiv_wwpn,u64 npiv_wwnn,int (* callback)(struct scsi_qla_host *,void *,u64,u64))6396 int qlt_lport_register(void *target_lport_ptr, u64 phys_wwpn,
6397 		       u64 npiv_wwpn, u64 npiv_wwnn,
6398 		       int (*callback)(struct scsi_qla_host *, void *, u64, u64))
6399 {
6400 	struct qla_tgt *tgt;
6401 	struct scsi_qla_host *vha;
6402 	struct qla_hw_data *ha;
6403 	struct Scsi_Host *host;
6404 	unsigned long flags;
6405 	int rc;
6406 	u8 b[WWN_SIZE];
6407 
6408 	mutex_lock(&qla_tgt_mutex);
6409 	list_for_each_entry(tgt, &qla_tgt_glist, tgt_list_entry) {
6410 		vha = tgt->vha;
6411 		ha = vha->hw;
6412 
6413 		host = vha->host;
6414 		if (!host)
6415 			continue;
6416 
6417 		if (!(host->hostt->supported_mode & MODE_TARGET))
6418 			continue;
6419 
6420 		if (vha->qlini_mode == QLA2XXX_INI_MODE_ENABLED)
6421 			continue;
6422 
6423 		spin_lock_irqsave(&ha->hardware_lock, flags);
6424 		if ((!npiv_wwpn || !npiv_wwnn) && host->active_mode & MODE_TARGET) {
6425 			pr_debug("MODE_TARGET already active on qla2xxx(%d)\n",
6426 			    host->host_no);
6427 			spin_unlock_irqrestore(&ha->hardware_lock, flags);
6428 			continue;
6429 		}
6430 		if (tgt->tgt_stop) {
6431 			pr_debug("MODE_TARGET in shutdown on qla2xxx(%d)\n",
6432 				 host->host_no);
6433 			spin_unlock_irqrestore(&ha->hardware_lock, flags);
6434 			continue;
6435 		}
6436 		spin_unlock_irqrestore(&ha->hardware_lock, flags);
6437 
6438 		if (!scsi_host_get(host)) {
6439 			ql_dbg(ql_dbg_tgt, vha, 0xe068,
6440 			    "Unable to scsi_host_get() for"
6441 			    " qla2xxx scsi_host\n");
6442 			continue;
6443 		}
6444 		qlt_lport_dump(vha, phys_wwpn, b);
6445 
6446 		if (memcmp(vha->port_name, b, WWN_SIZE)) {
6447 			scsi_host_put(host);
6448 			continue;
6449 		}
6450 		rc = (*callback)(vha, target_lport_ptr, npiv_wwpn, npiv_wwnn);
6451 		if (rc != 0)
6452 			scsi_host_put(host);
6453 
6454 		mutex_unlock(&qla_tgt_mutex);
6455 		return rc;
6456 	}
6457 	mutex_unlock(&qla_tgt_mutex);
6458 
6459 	return -ENODEV;
6460 }
6461 EXPORT_SYMBOL(qlt_lport_register);
6462 
6463 /**
6464  * qlt_lport_deregister - Degister lport
6465  *
6466  * @vha:  Registered scsi_qla_host pointer
6467  */
qlt_lport_deregister(struct scsi_qla_host * vha)6468 void qlt_lport_deregister(struct scsi_qla_host *vha)
6469 {
6470 	struct qla_hw_data *ha = vha->hw;
6471 	struct Scsi_Host *sh = vha->host;
6472 	/*
6473 	 * Clear the target_lport_ptr qla_target_template pointer in qla_hw_data
6474 	 */
6475 	vha->vha_tgt.target_lport_ptr = NULL;
6476 	ha->tgt.tgt_ops = NULL;
6477 	/*
6478 	 * Release the Scsi_Host reference for the underlying qla2xxx host
6479 	 */
6480 	scsi_host_put(sh);
6481 }
6482 EXPORT_SYMBOL(qlt_lport_deregister);
6483 
6484 /* Must be called under HW lock */
qlt_set_mode(struct scsi_qla_host * vha)6485 void qlt_set_mode(struct scsi_qla_host *vha)
6486 {
6487 	switch (vha->qlini_mode) {
6488 	case QLA2XXX_INI_MODE_DISABLED:
6489 	case QLA2XXX_INI_MODE_EXCLUSIVE:
6490 		vha->host->active_mode = MODE_TARGET;
6491 		break;
6492 	case QLA2XXX_INI_MODE_ENABLED:
6493 		vha->host->active_mode = MODE_INITIATOR;
6494 		break;
6495 	case QLA2XXX_INI_MODE_DUAL:
6496 		vha->host->active_mode = MODE_DUAL;
6497 		break;
6498 	default:
6499 		break;
6500 	}
6501 }
6502 
6503 /* Must be called under HW lock */
qlt_clear_mode(struct scsi_qla_host * vha)6504 static void qlt_clear_mode(struct scsi_qla_host *vha)
6505 {
6506 	switch (vha->qlini_mode) {
6507 	case QLA2XXX_INI_MODE_DISABLED:
6508 		vha->host->active_mode = MODE_UNKNOWN;
6509 		break;
6510 	case QLA2XXX_INI_MODE_EXCLUSIVE:
6511 		vha->host->active_mode = MODE_INITIATOR;
6512 		break;
6513 	case QLA2XXX_INI_MODE_ENABLED:
6514 	case QLA2XXX_INI_MODE_DUAL:
6515 		vha->host->active_mode = MODE_INITIATOR;
6516 		break;
6517 	default:
6518 		break;
6519 	}
6520 }
6521 
6522 /*
6523  * qla_tgt_enable_vha - NO LOCK HELD
6524  *
6525  * host_reset, bring up w/ Target Mode Enabled
6526  */
6527 void
qlt_enable_vha(struct scsi_qla_host * vha)6528 qlt_enable_vha(struct scsi_qla_host *vha)
6529 {
6530 	struct qla_hw_data *ha = vha->hw;
6531 	struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
6532 	unsigned long flags;
6533 	scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
6534 
6535 	if (!tgt) {
6536 		ql_dbg(ql_dbg_tgt, vha, 0xe069,
6537 		    "Unable to locate qla_tgt pointer from"
6538 		    " struct qla_hw_data\n");
6539 		dump_stack();
6540 		return;
6541 	}
6542 	if (vha->qlini_mode == QLA2XXX_INI_MODE_ENABLED)
6543 		return;
6544 
6545 	if (ha->tgt.num_act_qpairs > ha->max_qpairs)
6546 		ha->tgt.num_act_qpairs = ha->max_qpairs;
6547 	spin_lock_irqsave(&ha->hardware_lock, flags);
6548 	tgt->tgt_stopped = 0;
6549 	qlt_set_mode(vha);
6550 	spin_unlock_irqrestore(&ha->hardware_lock, flags);
6551 
6552 	mutex_lock(&ha->optrom_mutex);
6553 	ql_dbg(ql_dbg_tgt_mgt, vha, 0xf021,
6554 	    "%s.\n", __func__);
6555 	if (vha->vp_idx) {
6556 		qla24xx_disable_vp(vha);
6557 		qla24xx_enable_vp(vha);
6558 	} else {
6559 		set_bit(ISP_ABORT_NEEDED, &base_vha->dpc_flags);
6560 		qla2xxx_wake_dpc(base_vha);
6561 		WARN_ON_ONCE(qla2x00_wait_for_hba_online(base_vha) !=
6562 			     QLA_SUCCESS);
6563 	}
6564 	mutex_unlock(&ha->optrom_mutex);
6565 }
6566 EXPORT_SYMBOL(qlt_enable_vha);
6567 
6568 /*
6569  * qla_tgt_disable_vha - NO LOCK HELD
6570  *
6571  * Disable Target Mode and reset the adapter
6572  */
qlt_disable_vha(struct scsi_qla_host * vha)6573 static void qlt_disable_vha(struct scsi_qla_host *vha)
6574 {
6575 	struct qla_hw_data *ha = vha->hw;
6576 	struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
6577 	unsigned long flags;
6578 
6579 	if (!tgt) {
6580 		ql_dbg(ql_dbg_tgt, vha, 0xe06a,
6581 		    "Unable to locate qla_tgt pointer from"
6582 		    " struct qla_hw_data\n");
6583 		dump_stack();
6584 		return;
6585 	}
6586 
6587 	spin_lock_irqsave(&ha->hardware_lock, flags);
6588 	qlt_clear_mode(vha);
6589 	spin_unlock_irqrestore(&ha->hardware_lock, flags);
6590 
6591 	set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
6592 	qla2xxx_wake_dpc(vha);
6593 
6594 	/*
6595 	 * We are expecting the offline state.
6596 	 * QLA_FUNCTION_FAILED means that adapter is offline.
6597 	 */
6598 	if (qla2x00_wait_for_hba_online(vha) != QLA_SUCCESS)
6599 		ql_dbg(ql_dbg_tgt, vha, 0xe081,
6600 		       "adapter is offline\n");
6601 }
6602 
6603 /*
6604  * Called from qla_init.c:qla24xx_vport_create() contex to setup
6605  * the target mode specific struct scsi_qla_host and struct qla_hw_data
6606  * members.
6607  */
6608 void
qlt_vport_create(struct scsi_qla_host * vha,struct qla_hw_data * ha)6609 qlt_vport_create(struct scsi_qla_host *vha, struct qla_hw_data *ha)
6610 {
6611 	vha->vha_tgt.qla_tgt = NULL;
6612 
6613 	mutex_init(&vha->vha_tgt.tgt_mutex);
6614 	mutex_init(&vha->vha_tgt.tgt_host_action_mutex);
6615 
6616 	INIT_LIST_HEAD(&vha->unknown_atio_list);
6617 	INIT_DELAYED_WORK(&vha->unknown_atio_work, qlt_unknown_atio_work_fn);
6618 
6619 	qlt_clear_mode(vha);
6620 
6621 	/*
6622 	 * NOTE: Currently the value is kept the same for <24xx and
6623 	 * >=24xx ISPs. If it is necessary to change it,
6624 	 * the check should be added for specific ISPs,
6625 	 * assigning the value appropriately.
6626 	 */
6627 	ha->tgt.atio_q_length = ATIO_ENTRY_CNT_24XX;
6628 
6629 	qlt_add_target(ha, vha);
6630 }
6631 
6632 u8
qlt_rff_id(struct scsi_qla_host * vha)6633 qlt_rff_id(struct scsi_qla_host *vha)
6634 {
6635 	u8 fc4_feature = 0;
6636 	/*
6637 	 * FC-4 Feature bit 0 indicates target functionality to the name server.
6638 	 */
6639 	if (qla_tgt_mode_enabled(vha)) {
6640 		fc4_feature = BIT_0;
6641 	} else if (qla_ini_mode_enabled(vha)) {
6642 		fc4_feature = BIT_1;
6643 	} else if (qla_dual_mode_enabled(vha))
6644 		fc4_feature = BIT_0 | BIT_1;
6645 
6646 	return fc4_feature;
6647 }
6648 
6649 /*
6650  * qlt_init_atio_q_entries() - Initializes ATIO queue entries.
6651  * @ha: HA context
6652  *
6653  * Beginning of ATIO ring has initialization control block already built
6654  * by nvram config routine.
6655  *
6656  * Returns 0 on success.
6657  */
6658 void
qlt_init_atio_q_entries(struct scsi_qla_host * vha)6659 qlt_init_atio_q_entries(struct scsi_qla_host *vha)
6660 {
6661 	struct qla_hw_data *ha = vha->hw;
6662 	uint16_t cnt;
6663 	struct atio_from_isp *pkt = (struct atio_from_isp *)ha->tgt.atio_ring;
6664 
6665 	if (qla_ini_mode_enabled(vha))
6666 		return;
6667 
6668 	for (cnt = 0; cnt < ha->tgt.atio_q_length; cnt++) {
6669 		pkt->u.raw.signature = cpu_to_le32(ATIO_PROCESSED);
6670 		pkt++;
6671 	}
6672 
6673 }
6674 
6675 /*
6676  * qlt_24xx_process_atio_queue() - Process ATIO queue entries.
6677  * @ha: SCSI driver HA context
6678  */
6679 void
qlt_24xx_process_atio_queue(struct scsi_qla_host * vha,uint8_t ha_locked)6680 qlt_24xx_process_atio_queue(struct scsi_qla_host *vha, uint8_t ha_locked)
6681 {
6682 	struct qla_hw_data *ha = vha->hw;
6683 	struct atio_from_isp *pkt;
6684 	int cnt, i;
6685 
6686 	if (!ha->flags.fw_started)
6687 		return;
6688 
6689 	while ((ha->tgt.atio_ring_ptr->signature != ATIO_PROCESSED) ||
6690 	    fcpcmd_is_corrupted(ha->tgt.atio_ring_ptr)) {
6691 		pkt = (struct atio_from_isp *)ha->tgt.atio_ring_ptr;
6692 		cnt = pkt->u.raw.entry_count;
6693 
6694 		if (unlikely(fcpcmd_is_corrupted(ha->tgt.atio_ring_ptr))) {
6695 			/*
6696 			 * This packet is corrupted. The header + payload
6697 			 * can not be trusted. There is no point in passing
6698 			 * it further up.
6699 			 */
6700 			ql_log(ql_log_warn, vha, 0xd03c,
6701 			    "corrupted fcp frame SID[%3phN] OXID[%04x] EXCG[%x] %64phN\n",
6702 			    &pkt->u.isp24.fcp_hdr.s_id,
6703 			    be16_to_cpu(pkt->u.isp24.fcp_hdr.ox_id),
6704 			    pkt->u.isp24.exchange_addr, pkt);
6705 
6706 			adjust_corrupted_atio(pkt);
6707 			qlt_send_term_exchange(ha->base_qpair, NULL, pkt,
6708 			    ha_locked, 0);
6709 		} else {
6710 			qlt_24xx_atio_pkt_all_vps(vha,
6711 			    (struct atio_from_isp *)pkt, ha_locked);
6712 		}
6713 
6714 		for (i = 0; i < cnt; i++) {
6715 			ha->tgt.atio_ring_index++;
6716 			if (ha->tgt.atio_ring_index == ha->tgt.atio_q_length) {
6717 				ha->tgt.atio_ring_index = 0;
6718 				ha->tgt.atio_ring_ptr = ha->tgt.atio_ring;
6719 			} else
6720 				ha->tgt.atio_ring_ptr++;
6721 
6722 			pkt->u.raw.signature = cpu_to_le32(ATIO_PROCESSED);
6723 			pkt = (struct atio_from_isp *)ha->tgt.atio_ring_ptr;
6724 		}
6725 		wmb();
6726 	}
6727 
6728 	/* Adjust ring index */
6729 	wrt_reg_dword(ISP_ATIO_Q_OUT(vha), ha->tgt.atio_ring_index);
6730 }
6731 
6732 void
qlt_24xx_config_rings(struct scsi_qla_host * vha)6733 qlt_24xx_config_rings(struct scsi_qla_host *vha)
6734 {
6735 	struct qla_hw_data *ha = vha->hw;
6736 	struct qla_msix_entry *msix = &ha->msix_entries[2];
6737 	struct init_cb_24xx *icb = (struct init_cb_24xx *)ha->init_cb;
6738 
6739 	if (!QLA_TGT_MODE_ENABLED())
6740 		return;
6741 
6742 	wrt_reg_dword(ISP_ATIO_Q_IN(vha), 0);
6743 	wrt_reg_dword(ISP_ATIO_Q_OUT(vha), 0);
6744 	rd_reg_dword(ISP_ATIO_Q_OUT(vha));
6745 
6746 	if (ha->flags.msix_enabled) {
6747 		if (IS_QLA83XX(ha) || IS_QLA27XX(ha) || IS_QLA28XX(ha)) {
6748 			icb->msix_atio = cpu_to_le16(msix->entry);
6749 			icb->firmware_options_2 &= cpu_to_le32(~BIT_26);
6750 			ql_dbg(ql_dbg_init, vha, 0xf072,
6751 			    "Registering ICB vector 0x%x for atio que.\n",
6752 			    msix->entry);
6753 		}
6754 	} else {
6755 		/* INTx|MSI */
6756 		if (IS_QLA83XX(ha) || IS_QLA27XX(ha) || IS_QLA28XX(ha)) {
6757 			icb->msix_atio = 0;
6758 			icb->firmware_options_2 |= cpu_to_le32(BIT_26);
6759 			ql_dbg(ql_dbg_init, vha, 0xf072,
6760 			    "%s: Use INTx for ATIOQ.\n", __func__);
6761 		}
6762 	}
6763 }
6764 
6765 void
qlt_24xx_config_nvram_stage1(struct scsi_qla_host * vha,struct nvram_24xx * nv)6766 qlt_24xx_config_nvram_stage1(struct scsi_qla_host *vha, struct nvram_24xx *nv)
6767 {
6768 	struct qla_hw_data *ha = vha->hw;
6769 	u32 tmp;
6770 
6771 	if (!QLA_TGT_MODE_ENABLED())
6772 		return;
6773 
6774 	if (qla_tgt_mode_enabled(vha) || qla_dual_mode_enabled(vha)) {
6775 		if (!ha->tgt.saved_set) {
6776 			/* We save only once */
6777 			ha->tgt.saved_exchange_count = nv->exchange_count;
6778 			ha->tgt.saved_firmware_options_1 =
6779 			    nv->firmware_options_1;
6780 			ha->tgt.saved_firmware_options_2 =
6781 			    nv->firmware_options_2;
6782 			ha->tgt.saved_firmware_options_3 =
6783 			    nv->firmware_options_3;
6784 			ha->tgt.saved_set = 1;
6785 		}
6786 
6787 		if (qla_tgt_mode_enabled(vha))
6788 			nv->exchange_count = cpu_to_le16(0xFFFF);
6789 		else			/* dual */
6790 			nv->exchange_count = cpu_to_le16(vha->ql2xexchoffld);
6791 
6792 		/* Enable target mode */
6793 		nv->firmware_options_1 |= cpu_to_le32(BIT_4);
6794 
6795 		/* Disable ini mode, if requested */
6796 		if (qla_tgt_mode_enabled(vha))
6797 			nv->firmware_options_1 |= cpu_to_le32(BIT_5);
6798 
6799 		/* Disable Full Login after LIP */
6800 		nv->firmware_options_1 &= cpu_to_le32(~BIT_13);
6801 		/* Enable initial LIP */
6802 		nv->firmware_options_1 &= cpu_to_le32(~BIT_9);
6803 		if (ql2xtgt_tape_enable)
6804 			/* Enable FC Tape support */
6805 			nv->firmware_options_2 |= cpu_to_le32(BIT_12);
6806 		else
6807 			/* Disable FC Tape support */
6808 			nv->firmware_options_2 &= cpu_to_le32(~BIT_12);
6809 
6810 		/* Disable Full Login after LIP */
6811 		nv->host_p &= cpu_to_le32(~BIT_10);
6812 
6813 		/*
6814 		 * clear BIT 15 explicitly as we have seen at least
6815 		 * a couple of instances where this was set and this
6816 		 * was causing the firmware to not be initialized.
6817 		 */
6818 		nv->firmware_options_1 &= cpu_to_le32(~BIT_15);
6819 		/* Enable target PRLI control */
6820 		nv->firmware_options_2 |= cpu_to_le32(BIT_14);
6821 
6822 		if (IS_QLA25XX(ha)) {
6823 			/* Change Loop-prefer to Pt-Pt */
6824 			tmp = ~(BIT_4|BIT_5|BIT_6);
6825 			nv->firmware_options_2 &= cpu_to_le32(tmp);
6826 			tmp = P2P << 4;
6827 			nv->firmware_options_2 |= cpu_to_le32(tmp);
6828 		}
6829 	} else {
6830 		if (ha->tgt.saved_set) {
6831 			nv->exchange_count = ha->tgt.saved_exchange_count;
6832 			nv->firmware_options_1 =
6833 			    ha->tgt.saved_firmware_options_1;
6834 			nv->firmware_options_2 =
6835 			    ha->tgt.saved_firmware_options_2;
6836 			nv->firmware_options_3 =
6837 			    ha->tgt.saved_firmware_options_3;
6838 		}
6839 		return;
6840 	}
6841 
6842 	if (ha->base_qpair->enable_class_2) {
6843 		if (vha->flags.init_done)
6844 			fc_host_supported_classes(vha->host) =
6845 				FC_COS_CLASS2 | FC_COS_CLASS3;
6846 
6847 		nv->firmware_options_2 |= cpu_to_le32(BIT_8);
6848 	} else {
6849 		if (vha->flags.init_done)
6850 			fc_host_supported_classes(vha->host) = FC_COS_CLASS3;
6851 
6852 		nv->firmware_options_2 &= ~cpu_to_le32(BIT_8);
6853 	}
6854 }
6855 
6856 void
qlt_24xx_config_nvram_stage2(struct scsi_qla_host * vha,struct init_cb_24xx * icb)6857 qlt_24xx_config_nvram_stage2(struct scsi_qla_host *vha,
6858 	struct init_cb_24xx *icb)
6859 {
6860 	struct qla_hw_data *ha = vha->hw;
6861 
6862 	if (!QLA_TGT_MODE_ENABLED())
6863 		return;
6864 
6865 	if (ha->tgt.node_name_set) {
6866 		memcpy(icb->node_name, ha->tgt.tgt_node_name, WWN_SIZE);
6867 		icb->firmware_options_1 |= cpu_to_le32(BIT_14);
6868 	}
6869 }
6870 
6871 void
qlt_81xx_config_nvram_stage1(struct scsi_qla_host * vha,struct nvram_81xx * nv)6872 qlt_81xx_config_nvram_stage1(struct scsi_qla_host *vha, struct nvram_81xx *nv)
6873 {
6874 	struct qla_hw_data *ha = vha->hw;
6875 	u32 tmp;
6876 
6877 	if (!QLA_TGT_MODE_ENABLED())
6878 		return;
6879 
6880 	if (qla_tgt_mode_enabled(vha) || qla_dual_mode_enabled(vha)) {
6881 		if (!ha->tgt.saved_set) {
6882 			/* We save only once */
6883 			ha->tgt.saved_exchange_count = nv->exchange_count;
6884 			ha->tgt.saved_firmware_options_1 =
6885 			    nv->firmware_options_1;
6886 			ha->tgt.saved_firmware_options_2 =
6887 			    nv->firmware_options_2;
6888 			ha->tgt.saved_firmware_options_3 =
6889 			    nv->firmware_options_3;
6890 			ha->tgt.saved_set = 1;
6891 		}
6892 
6893 		if (qla_tgt_mode_enabled(vha))
6894 			nv->exchange_count = cpu_to_le16(0xFFFF);
6895 		else			/* dual */
6896 			nv->exchange_count = cpu_to_le16(vha->ql2xexchoffld);
6897 
6898 		/* Enable target mode */
6899 		nv->firmware_options_1 |= cpu_to_le32(BIT_4);
6900 
6901 		/* Disable ini mode, if requested */
6902 		if (qla_tgt_mode_enabled(vha))
6903 			nv->firmware_options_1 |= cpu_to_le32(BIT_5);
6904 		/* Disable Full Login after LIP */
6905 		nv->firmware_options_1 &= cpu_to_le32(~BIT_13);
6906 		/* Enable initial LIP */
6907 		nv->firmware_options_1 &= cpu_to_le32(~BIT_9);
6908 		/*
6909 		 * clear BIT 15 explicitly as we have seen at
6910 		 * least a couple of instances where this was set
6911 		 * and this was causing the firmware to not be
6912 		 * initialized.
6913 		 */
6914 		nv->firmware_options_1 &= cpu_to_le32(~BIT_15);
6915 		if (ql2xtgt_tape_enable)
6916 			/* Enable FC tape support */
6917 			nv->firmware_options_2 |= cpu_to_le32(BIT_12);
6918 		else
6919 			/* Disable FC tape support */
6920 			nv->firmware_options_2 &= cpu_to_le32(~BIT_12);
6921 
6922 		/* Disable Full Login after LIP */
6923 		nv->host_p &= cpu_to_le32(~BIT_10);
6924 		/* Enable target PRLI control */
6925 		nv->firmware_options_2 |= cpu_to_le32(BIT_14);
6926 
6927 		/* Change Loop-prefer to Pt-Pt */
6928 		tmp = ~(BIT_4|BIT_5|BIT_6);
6929 		nv->firmware_options_2 &= cpu_to_le32(tmp);
6930 		tmp = P2P << 4;
6931 		nv->firmware_options_2 |= cpu_to_le32(tmp);
6932 	} else {
6933 		if (ha->tgt.saved_set) {
6934 			nv->exchange_count = ha->tgt.saved_exchange_count;
6935 			nv->firmware_options_1 =
6936 			    ha->tgt.saved_firmware_options_1;
6937 			nv->firmware_options_2 =
6938 			    ha->tgt.saved_firmware_options_2;
6939 			nv->firmware_options_3 =
6940 			    ha->tgt.saved_firmware_options_3;
6941 		}
6942 		return;
6943 	}
6944 
6945 	if (ha->base_qpair->enable_class_2) {
6946 		if (vha->flags.init_done)
6947 			fc_host_supported_classes(vha->host) =
6948 				FC_COS_CLASS2 | FC_COS_CLASS3;
6949 
6950 		nv->firmware_options_2 |= cpu_to_le32(BIT_8);
6951 	} else {
6952 		if (vha->flags.init_done)
6953 			fc_host_supported_classes(vha->host) = FC_COS_CLASS3;
6954 
6955 		nv->firmware_options_2 &= ~cpu_to_le32(BIT_8);
6956 	}
6957 }
6958 
6959 void
qlt_81xx_config_nvram_stage2(struct scsi_qla_host * vha,struct init_cb_81xx * icb)6960 qlt_81xx_config_nvram_stage2(struct scsi_qla_host *vha,
6961 	struct init_cb_81xx *icb)
6962 {
6963 	struct qla_hw_data *ha = vha->hw;
6964 
6965 	if (!QLA_TGT_MODE_ENABLED())
6966 		return;
6967 
6968 	if (ha->tgt.node_name_set) {
6969 		memcpy(icb->node_name, ha->tgt.tgt_node_name, WWN_SIZE);
6970 		icb->firmware_options_1 |= cpu_to_le32(BIT_14);
6971 	}
6972 }
6973 
6974 void
qlt_modify_vp_config(struct scsi_qla_host * vha,struct vp_config_entry_24xx * vpmod)6975 qlt_modify_vp_config(struct scsi_qla_host *vha,
6976 	struct vp_config_entry_24xx *vpmod)
6977 {
6978 	/* enable target mode.  Bit5 = 1 => disable */
6979 	if (qla_tgt_mode_enabled(vha) || qla_dual_mode_enabled(vha))
6980 		vpmod->options_idx1 &= ~BIT_5;
6981 
6982 	/* Disable ini mode, if requested.  bit4 = 1 => disable */
6983 	if (qla_tgt_mode_enabled(vha))
6984 		vpmod->options_idx1 &= ~BIT_4;
6985 }
6986 
6987 void
qlt_probe_one_stage1(struct scsi_qla_host * base_vha,struct qla_hw_data * ha)6988 qlt_probe_one_stage1(struct scsi_qla_host *base_vha, struct qla_hw_data *ha)
6989 {
6990 	mutex_init(&base_vha->vha_tgt.tgt_mutex);
6991 	if (!QLA_TGT_MODE_ENABLED())
6992 		return;
6993 
6994 	if  (ha->mqenable || IS_QLA83XX(ha) || IS_QLA27XX(ha) || IS_QLA28XX(ha)) {
6995 		ISP_ATIO_Q_IN(base_vha) = &ha->mqiobase->isp25mq.atio_q_in;
6996 		ISP_ATIO_Q_OUT(base_vha) = &ha->mqiobase->isp25mq.atio_q_out;
6997 	} else {
6998 		ISP_ATIO_Q_IN(base_vha) = &ha->iobase->isp24.atio_q_in;
6999 		ISP_ATIO_Q_OUT(base_vha) = &ha->iobase->isp24.atio_q_out;
7000 	}
7001 
7002 	mutex_init(&base_vha->vha_tgt.tgt_host_action_mutex);
7003 
7004 	INIT_LIST_HEAD(&base_vha->unknown_atio_list);
7005 	INIT_DELAYED_WORK(&base_vha->unknown_atio_work,
7006 	    qlt_unknown_atio_work_fn);
7007 
7008 	qlt_clear_mode(base_vha);
7009 
7010 	qla_update_vp_map(base_vha, SET_VP_IDX);
7011 }
7012 
7013 irqreturn_t
qla83xx_msix_atio_q(int irq,void * dev_id)7014 qla83xx_msix_atio_q(int irq, void *dev_id)
7015 {
7016 	struct rsp_que *rsp;
7017 	scsi_qla_host_t	*vha;
7018 	struct qla_hw_data *ha;
7019 	unsigned long flags;
7020 
7021 	rsp = (struct rsp_que *) dev_id;
7022 	ha = rsp->hw;
7023 	vha = pci_get_drvdata(ha->pdev);
7024 
7025 	spin_lock_irqsave(&ha->tgt.atio_lock, flags);
7026 
7027 	qlt_24xx_process_atio_queue(vha, 0);
7028 
7029 	spin_unlock_irqrestore(&ha->tgt.atio_lock, flags);
7030 
7031 	return IRQ_HANDLED;
7032 }
7033 
7034 static void
qlt_handle_abts_recv_work(struct work_struct * work)7035 qlt_handle_abts_recv_work(struct work_struct *work)
7036 {
7037 	struct qla_tgt_sess_op *op = container_of(work,
7038 		struct qla_tgt_sess_op, work);
7039 	scsi_qla_host_t *vha = op->vha;
7040 	struct qla_hw_data *ha = vha->hw;
7041 	unsigned long flags;
7042 
7043 	if (qla2x00_reset_active(vha) ||
7044 	    (op->chip_reset != ha->base_qpair->chip_reset))
7045 		return;
7046 
7047 	spin_lock_irqsave(&ha->tgt.atio_lock, flags);
7048 	qlt_24xx_process_atio_queue(vha, 0);
7049 	spin_unlock_irqrestore(&ha->tgt.atio_lock, flags);
7050 
7051 	spin_lock_irqsave(&ha->hardware_lock, flags);
7052 	qlt_response_pkt_all_vps(vha, op->rsp, (response_t *)&op->atio);
7053 	spin_unlock_irqrestore(&ha->hardware_lock, flags);
7054 
7055 	kfree(op);
7056 }
7057 
7058 void
qlt_handle_abts_recv(struct scsi_qla_host * vha,struct rsp_que * rsp,response_t * pkt)7059 qlt_handle_abts_recv(struct scsi_qla_host *vha, struct rsp_que *rsp,
7060     response_t *pkt)
7061 {
7062 	struct qla_tgt_sess_op *op;
7063 
7064 	op = kzalloc(sizeof(*op), GFP_ATOMIC);
7065 
7066 	if (!op) {
7067 		/* do not reach for ATIO queue here.  This is best effort err
7068 		 * recovery at this point.
7069 		 */
7070 		qlt_response_pkt_all_vps(vha, rsp, pkt);
7071 		return;
7072 	}
7073 
7074 	memcpy(&op->atio, pkt, sizeof(*pkt));
7075 	op->vha = vha;
7076 	op->chip_reset = vha->hw->base_qpair->chip_reset;
7077 	op->rsp = rsp;
7078 	INIT_WORK(&op->work, qlt_handle_abts_recv_work);
7079 	queue_work(qla_tgt_wq, &op->work);
7080 	return;
7081 }
7082 
7083 int
qlt_mem_alloc(struct qla_hw_data * ha)7084 qlt_mem_alloc(struct qla_hw_data *ha)
7085 {
7086 	if (!QLA_TGT_MODE_ENABLED())
7087 		return 0;
7088 
7089 	ha->tgt.atio_ring = dma_alloc_coherent(&ha->pdev->dev,
7090 	    (ha->tgt.atio_q_length + 1) * sizeof(struct atio_from_isp),
7091 	    &ha->tgt.atio_dma, GFP_KERNEL);
7092 	if (!ha->tgt.atio_ring) {
7093 		return -ENOMEM;
7094 	}
7095 	return 0;
7096 }
7097 
7098 void
qlt_mem_free(struct qla_hw_data * ha)7099 qlt_mem_free(struct qla_hw_data *ha)
7100 {
7101 	if (!QLA_TGT_MODE_ENABLED())
7102 		return;
7103 
7104 	if (ha->tgt.atio_ring) {
7105 		dma_free_coherent(&ha->pdev->dev, (ha->tgt.atio_q_length + 1) *
7106 		    sizeof(struct atio_from_isp), ha->tgt.atio_ring,
7107 		    ha->tgt.atio_dma);
7108 	}
7109 	ha->tgt.atio_ring = NULL;
7110 	ha->tgt.atio_dma = 0;
7111 }
7112 
qlt_parse_ini_mode(void)7113 static int __init qlt_parse_ini_mode(void)
7114 {
7115 	if (strcasecmp(qlini_mode, QLA2XXX_INI_MODE_STR_EXCLUSIVE) == 0)
7116 		ql2x_ini_mode = QLA2XXX_INI_MODE_EXCLUSIVE;
7117 	else if (strcasecmp(qlini_mode, QLA2XXX_INI_MODE_STR_DISABLED) == 0)
7118 		ql2x_ini_mode = QLA2XXX_INI_MODE_DISABLED;
7119 	else if (strcasecmp(qlini_mode, QLA2XXX_INI_MODE_STR_ENABLED) == 0)
7120 		ql2x_ini_mode = QLA2XXX_INI_MODE_ENABLED;
7121 	else if (strcasecmp(qlini_mode, QLA2XXX_INI_MODE_STR_DUAL) == 0)
7122 		ql2x_ini_mode = QLA2XXX_INI_MODE_DUAL;
7123 	else
7124 		return false;
7125 
7126 	return true;
7127 }
7128 
qlt_init(void)7129 int __init qlt_init(void)
7130 {
7131 	int ret;
7132 
7133 	BUILD_BUG_ON(sizeof(struct ctio7_to_24xx) != 64);
7134 	BUILD_BUG_ON(sizeof(struct ctio_to_2xxx) != 64);
7135 
7136 	if (!qlt_parse_ini_mode()) {
7137 		ql_log(ql_log_fatal, NULL, 0xe06b,
7138 		    "qlt_parse_ini_mode() failed\n");
7139 		return -EINVAL;
7140 	}
7141 
7142 	if (!QLA_TGT_MODE_ENABLED())
7143 		return 0;
7144 
7145 	qla_tgt_mgmt_cmd_cachep = kmem_cache_create("qla_tgt_mgmt_cmd_cachep",
7146 	    sizeof(struct qla_tgt_mgmt_cmd), __alignof__(struct
7147 	    qla_tgt_mgmt_cmd), 0, NULL);
7148 	if (!qla_tgt_mgmt_cmd_cachep) {
7149 		ql_log(ql_log_fatal, NULL, 0xd04b,
7150 		    "kmem_cache_create for qla_tgt_mgmt_cmd_cachep failed\n");
7151 		return -ENOMEM;
7152 	}
7153 
7154 	qla_tgt_plogi_cachep = kmem_cache_create("qla_tgt_plogi_cachep",
7155 	    sizeof(struct qlt_plogi_ack_t), __alignof__(struct qlt_plogi_ack_t),
7156 	    0, NULL);
7157 
7158 	if (!qla_tgt_plogi_cachep) {
7159 		ql_log(ql_log_fatal, NULL, 0xe06d,
7160 		    "kmem_cache_create for qla_tgt_plogi_cachep failed\n");
7161 		ret = -ENOMEM;
7162 		goto out_mgmt_cmd_cachep;
7163 	}
7164 
7165 	qla_tgt_mgmt_cmd_mempool = mempool_create(25, mempool_alloc_slab,
7166 	    mempool_free_slab, qla_tgt_mgmt_cmd_cachep);
7167 	if (!qla_tgt_mgmt_cmd_mempool) {
7168 		ql_log(ql_log_fatal, NULL, 0xe06e,
7169 		    "mempool_create for qla_tgt_mgmt_cmd_mempool failed\n");
7170 		ret = -ENOMEM;
7171 		goto out_plogi_cachep;
7172 	}
7173 
7174 	qla_tgt_wq = alloc_workqueue("qla_tgt_wq", 0, 0);
7175 	if (!qla_tgt_wq) {
7176 		ql_log(ql_log_fatal, NULL, 0xe06f,
7177 		    "alloc_workqueue for qla_tgt_wq failed\n");
7178 		ret = -ENOMEM;
7179 		goto out_cmd_mempool;
7180 	}
7181 	/*
7182 	 * Return 1 to signal that initiator-mode is being disabled
7183 	 */
7184 	return (ql2x_ini_mode == QLA2XXX_INI_MODE_DISABLED) ? 1 : 0;
7185 
7186 out_cmd_mempool:
7187 	mempool_destroy(qla_tgt_mgmt_cmd_mempool);
7188 out_plogi_cachep:
7189 	kmem_cache_destroy(qla_tgt_plogi_cachep);
7190 out_mgmt_cmd_cachep:
7191 	kmem_cache_destroy(qla_tgt_mgmt_cmd_cachep);
7192 	return ret;
7193 }
7194 
qlt_exit(void)7195 void qlt_exit(void)
7196 {
7197 	if (!QLA_TGT_MODE_ENABLED())
7198 		return;
7199 
7200 	destroy_workqueue(qla_tgt_wq);
7201 	mempool_destroy(qla_tgt_mgmt_cmd_mempool);
7202 	kmem_cache_destroy(qla_tgt_plogi_cachep);
7203 	kmem_cache_destroy(qla_tgt_mgmt_cmd_cachep);
7204 }
7205