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