blob: bd9c725c08e1c98a244eca58389937dc87f458da [file] [log] [blame]
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001/*
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 *
Nicholas Bellinger4c762512013-09-05 15:29:12 -070013 * Copyright (C) 2010-2013 Nicholas A. Bellinger <nab@kernel.org>
Nicholas Bellinger2d70c102012-05-15 14:34:28 -040014 *
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>
Nicholas Bellinger2d70c102012-05-15 14:34:28 -040029#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
45static char *qlini_mode = QLA2XXX_INI_MODE_STR_ENABLED;
46module_param(qlini_mode, charp, S_IRUGO);
47MODULE_PARM_DESC(qlini_mode,
48 "Determines when initiator mode will be enabled. Possible values: "
49 "\"exclusive\" - initiator mode will be enabled on load, "
50 "disabled on enabling target mode and then on disabling target mode "
51 "enabled back; "
52 "\"disabled\" - initiator mode will never be enabled; "
53 "\"enabled\" (default) - initiator mode will always stay enabled.");
54
Arun Easiaa230bc2013-01-30 03:34:39 -050055int ql2x_ini_mode = QLA2XXX_INI_MODE_EXCLUSIVE;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -040056
57/*
58 * From scsi/fc/fc_fcp.h
59 */
60enum fcp_resp_rsp_codes {
61 FCP_TMF_CMPL = 0,
62 FCP_DATA_LEN_INVALID = 1,
63 FCP_CMND_FIELDS_INVALID = 2,
64 FCP_DATA_PARAM_MISMATCH = 3,
65 FCP_TMF_REJECTED = 4,
66 FCP_TMF_FAILED = 5,
67 FCP_TMF_INVALID_LUN = 9,
68};
69
70/*
71 * fc_pri_ta from scsi/fc/fc_fcp.h
72 */
73#define FCP_PTA_SIMPLE 0 /* simple task attribute */
74#define FCP_PTA_HEADQ 1 /* head of queue task attribute */
75#define FCP_PTA_ORDERED 2 /* ordered task attribute */
Masanari Iida6efb3c02012-10-26 22:10:54 +090076#define FCP_PTA_ACA 4 /* auto. contingent allegiance */
Nicholas Bellinger2d70c102012-05-15 14:34:28 -040077#define FCP_PTA_MASK 7 /* mask for task attribute field */
78#define FCP_PRI_SHIFT 3 /* priority field starts in bit 3 */
79#define FCP_PRI_RESVD_MASK 0x80 /* reserved bits in priority field */
80
81/*
82 * This driver calls qla2x00_alloc_iocbs() and qla2x00_issue_marker(), which
83 * must be called under HW lock and could unlock/lock it inside.
84 * It isn't an issue, since in the current implementation on the time when
85 * those functions are called:
86 *
87 * - Either context is IRQ and only IRQ handler can modify HW data,
88 * including rings related fields,
89 *
90 * - Or access to target mode variables from struct qla_tgt doesn't
91 * cross those functions boundaries, except tgt_stop, which
92 * additionally protected by irq_cmd_count.
93 */
94/* Predefs for callbacks handed to qla2xxx LLD */
95static void qlt_24xx_atio_pkt(struct scsi_qla_host *ha,
96 struct atio_from_isp *pkt);
97static void qlt_response_pkt(struct scsi_qla_host *ha, response_t *pkt);
98static int qlt_issue_task_mgmt(struct qla_tgt_sess *sess, uint32_t lun,
99 int fn, void *iocb, int flags);
100static void qlt_send_term_exchange(struct scsi_qla_host *ha, struct qla_tgt_cmd
101 *cmd, struct atio_from_isp *atio, int ha_locked);
102static void qlt_reject_free_srr_imm(struct scsi_qla_host *ha,
103 struct qla_tgt_srr_imm *imm, int ha_lock);
104/*
105 * Global Variables
106 */
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400107static struct kmem_cache *qla_tgt_mgmt_cmd_cachep;
108static mempool_t *qla_tgt_mgmt_cmd_mempool;
109static struct workqueue_struct *qla_tgt_wq;
110static DEFINE_MUTEX(qla_tgt_mutex);
111static LIST_HEAD(qla_tgt_glist);
112
113/* ha->hardware_lock supposed to be held on entry (to protect tgt->sess_list) */
114static struct qla_tgt_sess *qlt_find_sess_by_port_name(
115 struct qla_tgt *tgt,
116 const uint8_t *port_name)
117{
118 struct qla_tgt_sess *sess;
119
120 list_for_each_entry(sess, &tgt->sess_list, sess_list_entry) {
121 if (!memcmp(sess->port_name, port_name, WWN_SIZE))
122 return sess;
123 }
124
125 return NULL;
126}
127
128/* Might release hw lock, then reaquire!! */
129static inline int qlt_issue_marker(struct scsi_qla_host *vha, int vha_locked)
130{
131 /* Send marker if required */
132 if (unlikely(vha->marker_needed != 0)) {
133 int rc = qla2x00_issue_marker(vha, vha_locked);
134 if (rc != QLA_SUCCESS) {
135 ql_dbg(ql_dbg_tgt, vha, 0xe03d,
136 "qla_target(%d): issue_marker() failed\n",
137 vha->vp_idx);
138 }
139 return rc;
140 }
141 return QLA_SUCCESS;
142}
143
144static inline
145struct scsi_qla_host *qlt_find_host_by_d_id(struct scsi_qla_host *vha,
146 uint8_t *d_id)
147{
148 struct qla_hw_data *ha = vha->hw;
149 uint8_t vp_idx;
150
151 if ((vha->d_id.b.area != d_id[1]) || (vha->d_id.b.domain != d_id[0]))
152 return NULL;
153
154 if (vha->d_id.b.al_pa == d_id[2])
155 return vha;
156
157 BUG_ON(ha->tgt.tgt_vp_map == NULL);
158 vp_idx = ha->tgt.tgt_vp_map[d_id[2]].idx;
159 if (likely(test_bit(vp_idx, ha->vp_idx_map)))
160 return ha->tgt.tgt_vp_map[vp_idx].vha;
161
162 return NULL;
163}
164
165static inline
166struct scsi_qla_host *qlt_find_host_by_vp_idx(struct scsi_qla_host *vha,
167 uint16_t vp_idx)
168{
169 struct qla_hw_data *ha = vha->hw;
170
171 if (vha->vp_idx == vp_idx)
172 return vha;
173
174 BUG_ON(ha->tgt.tgt_vp_map == NULL);
175 if (likely(test_bit(vp_idx, ha->vp_idx_map)))
176 return ha->tgt.tgt_vp_map[vp_idx].vha;
177
178 return NULL;
179}
180
181void qlt_24xx_atio_pkt_all_vps(struct scsi_qla_host *vha,
182 struct atio_from_isp *atio)
183{
184 switch (atio->u.raw.entry_type) {
185 case ATIO_TYPE7:
186 {
187 struct scsi_qla_host *host = qlt_find_host_by_d_id(vha,
188 atio->u.isp24.fcp_hdr.d_id);
189 if (unlikely(NULL == host)) {
190 ql_dbg(ql_dbg_tgt, vha, 0xe03e,
191 "qla_target(%d): Received ATIO_TYPE7 "
192 "with unknown d_id %x:%x:%x\n", vha->vp_idx,
193 atio->u.isp24.fcp_hdr.d_id[0],
194 atio->u.isp24.fcp_hdr.d_id[1],
195 atio->u.isp24.fcp_hdr.d_id[2]);
196 break;
197 }
198 qlt_24xx_atio_pkt(host, atio);
199 break;
200 }
201
202 case IMMED_NOTIFY_TYPE:
203 {
204 struct scsi_qla_host *host = vha;
205 struct imm_ntfy_from_isp *entry =
206 (struct imm_ntfy_from_isp *)atio;
207
208 if ((entry->u.isp24.vp_index != 0xFF) &&
209 (entry->u.isp24.nport_handle != 0xFFFF)) {
210 host = qlt_find_host_by_vp_idx(vha,
211 entry->u.isp24.vp_index);
212 if (unlikely(!host)) {
213 ql_dbg(ql_dbg_tgt, vha, 0xe03f,
214 "qla_target(%d): Received "
215 "ATIO (IMMED_NOTIFY_TYPE) "
216 "with unknown vp_index %d\n",
217 vha->vp_idx, entry->u.isp24.vp_index);
218 break;
219 }
220 }
221 qlt_24xx_atio_pkt(host, atio);
222 break;
223 }
224
225 default:
226 ql_dbg(ql_dbg_tgt, vha, 0xe040,
227 "qla_target(%d): Received unknown ATIO atio "
228 "type %x\n", vha->vp_idx, atio->u.raw.entry_type);
229 break;
230 }
231
232 return;
233}
234
235void qlt_response_pkt_all_vps(struct scsi_qla_host *vha, response_t *pkt)
236{
237 switch (pkt->entry_type) {
238 case CTIO_TYPE7:
239 {
240 struct ctio7_from_24xx *entry = (struct ctio7_from_24xx *)pkt;
241 struct scsi_qla_host *host = qlt_find_host_by_vp_idx(vha,
242 entry->vp_index);
243 if (unlikely(!host)) {
244 ql_dbg(ql_dbg_tgt, vha, 0xe041,
245 "qla_target(%d): Response pkt (CTIO_TYPE7) "
246 "received, with unknown vp_index %d\n",
247 vha->vp_idx, entry->vp_index);
248 break;
249 }
250 qlt_response_pkt(host, pkt);
251 break;
252 }
253
254 case IMMED_NOTIFY_TYPE:
255 {
256 struct scsi_qla_host *host = vha;
257 struct imm_ntfy_from_isp *entry =
258 (struct imm_ntfy_from_isp *)pkt;
259
260 host = qlt_find_host_by_vp_idx(vha, entry->u.isp24.vp_index);
261 if (unlikely(!host)) {
262 ql_dbg(ql_dbg_tgt, vha, 0xe042,
263 "qla_target(%d): Response pkt (IMMED_NOTIFY_TYPE) "
264 "received, with unknown vp_index %d\n",
265 vha->vp_idx, entry->u.isp24.vp_index);
266 break;
267 }
268 qlt_response_pkt(host, pkt);
269 break;
270 }
271
272 case NOTIFY_ACK_TYPE:
273 {
274 struct scsi_qla_host *host = vha;
275 struct nack_to_isp *entry = (struct nack_to_isp *)pkt;
276
277 if (0xFF != entry->u.isp24.vp_index) {
278 host = qlt_find_host_by_vp_idx(vha,
279 entry->u.isp24.vp_index);
280 if (unlikely(!host)) {
281 ql_dbg(ql_dbg_tgt, vha, 0xe043,
282 "qla_target(%d): Response "
283 "pkt (NOTIFY_ACK_TYPE) "
284 "received, with unknown "
285 "vp_index %d\n", vha->vp_idx,
286 entry->u.isp24.vp_index);
287 break;
288 }
289 }
290 qlt_response_pkt(host, pkt);
291 break;
292 }
293
294 case ABTS_RECV_24XX:
295 {
296 struct abts_recv_from_24xx *entry =
297 (struct abts_recv_from_24xx *)pkt;
298 struct scsi_qla_host *host = qlt_find_host_by_vp_idx(vha,
299 entry->vp_index);
300 if (unlikely(!host)) {
301 ql_dbg(ql_dbg_tgt, vha, 0xe044,
302 "qla_target(%d): Response pkt "
303 "(ABTS_RECV_24XX) received, with unknown "
304 "vp_index %d\n", vha->vp_idx, entry->vp_index);
305 break;
306 }
307 qlt_response_pkt(host, pkt);
308 break;
309 }
310
311 case ABTS_RESP_24XX:
312 {
313 struct abts_resp_to_24xx *entry =
314 (struct abts_resp_to_24xx *)pkt;
315 struct scsi_qla_host *host = qlt_find_host_by_vp_idx(vha,
316 entry->vp_index);
317 if (unlikely(!host)) {
318 ql_dbg(ql_dbg_tgt, vha, 0xe045,
319 "qla_target(%d): Response pkt "
320 "(ABTS_RECV_24XX) received, with unknown "
321 "vp_index %d\n", vha->vp_idx, entry->vp_index);
322 break;
323 }
324 qlt_response_pkt(host, pkt);
325 break;
326 }
327
328 default:
329 qlt_response_pkt(vha, pkt);
330 break;
331 }
332
333}
334
335static void qlt_free_session_done(struct work_struct *work)
336{
337 struct qla_tgt_sess *sess = container_of(work, struct qla_tgt_sess,
338 free_work);
339 struct qla_tgt *tgt = sess->tgt;
340 struct scsi_qla_host *vha = sess->vha;
341 struct qla_hw_data *ha = vha->hw;
342
343 BUG_ON(!tgt);
344 /*
345 * Release the target session for FC Nexus from fabric module code.
346 */
347 if (sess->se_sess != NULL)
348 ha->tgt.tgt_ops->free_session(sess);
349
350 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf001,
351 "Unregistration of sess %p finished\n", sess);
352
353 kfree(sess);
354 /*
355 * We need to protect against race, when tgt is freed before or
356 * inside wake_up()
357 */
358 tgt->sess_count--;
359 if (tgt->sess_count == 0)
360 wake_up_all(&tgt->waitQ);
361}
362
363/* ha->hardware_lock supposed to be held on entry */
364void qlt_unreg_sess(struct qla_tgt_sess *sess)
365{
366 struct scsi_qla_host *vha = sess->vha;
367
368 vha->hw->tgt.tgt_ops->clear_nacl_from_fcport_map(sess);
369
370 list_del(&sess->sess_list_entry);
371 if (sess->deleted)
372 list_del(&sess->del_list_entry);
373
374 INIT_WORK(&sess->free_work, qlt_free_session_done);
375 schedule_work(&sess->free_work);
376}
377EXPORT_SYMBOL(qlt_unreg_sess);
378
379/* ha->hardware_lock supposed to be held on entry */
380static int qlt_reset(struct scsi_qla_host *vha, void *iocb, int mcmd)
381{
382 struct qla_hw_data *ha = vha->hw;
383 struct qla_tgt_sess *sess = NULL;
384 uint32_t unpacked_lun, lun = 0;
385 uint16_t loop_id;
386 int res = 0;
387 struct imm_ntfy_from_isp *n = (struct imm_ntfy_from_isp *)iocb;
388 struct atio_from_isp *a = (struct atio_from_isp *)iocb;
389
390 loop_id = le16_to_cpu(n->u.isp24.nport_handle);
391 if (loop_id == 0xFFFF) {
392#if 0 /* FIXME: Re-enable Global event handling.. */
393 /* Global event */
394 atomic_inc(&ha->tgt.qla_tgt->tgt_global_resets_count);
395 qlt_clear_tgt_db(ha->tgt.qla_tgt, 1);
396 if (!list_empty(&ha->tgt.qla_tgt->sess_list)) {
397 sess = list_entry(ha->tgt.qla_tgt->sess_list.next,
398 typeof(*sess), sess_list_entry);
399 switch (mcmd) {
400 case QLA_TGT_NEXUS_LOSS_SESS:
401 mcmd = QLA_TGT_NEXUS_LOSS;
402 break;
403 case QLA_TGT_ABORT_ALL_SESS:
404 mcmd = QLA_TGT_ABORT_ALL;
405 break;
406 case QLA_TGT_NEXUS_LOSS:
407 case QLA_TGT_ABORT_ALL:
408 break;
409 default:
410 ql_dbg(ql_dbg_tgt, vha, 0xe046,
411 "qla_target(%d): Not allowed "
412 "command %x in %s", vha->vp_idx,
413 mcmd, __func__);
414 sess = NULL;
415 break;
416 }
417 } else
418 sess = NULL;
419#endif
420 } else {
421 sess = ha->tgt.tgt_ops->find_sess_by_loop_id(vha, loop_id);
422 }
423
424 ql_dbg(ql_dbg_tgt, vha, 0xe000,
425 "Using sess for qla_tgt_reset: %p\n", sess);
426 if (!sess) {
427 res = -ESRCH;
428 return res;
429 }
430
431 ql_dbg(ql_dbg_tgt, vha, 0xe047,
Oleksandr Khoshaba7b833552013-08-27 01:37:27 -0400432 "scsi(%ld): resetting (session %p from port %8phC mcmd %x, "
433 "loop_id %d)\n", vha->host_no, sess, sess->port_name,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400434 mcmd, loop_id);
435
436 lun = a->u.isp24.fcp_cmnd.lun;
437 unpacked_lun = scsilun_to_int((struct scsi_lun *)&lun);
438
439 return qlt_issue_task_mgmt(sess, unpacked_lun, mcmd,
440 iocb, QLA24XX_MGMT_SEND_NACK);
441}
442
443/* ha->hardware_lock supposed to be held on entry */
444static void qlt_schedule_sess_for_deletion(struct qla_tgt_sess *sess,
445 bool immediate)
446{
447 struct qla_tgt *tgt = sess->tgt;
448 uint32_t dev_loss_tmo = tgt->ha->port_down_retry_count + 5;
449
450 if (sess->deleted)
451 return;
452
453 ql_dbg(ql_dbg_tgt, sess->vha, 0xe001,
454 "Scheduling sess %p for deletion\n", sess);
455 list_add_tail(&sess->del_list_entry, &tgt->del_sess_list);
456 sess->deleted = 1;
457
458 if (immediate)
459 dev_loss_tmo = 0;
460
461 sess->expires = jiffies + dev_loss_tmo * HZ;
462
463 ql_dbg(ql_dbg_tgt, sess->vha, 0xe048,
Oleksandr Khoshaba7b833552013-08-27 01:37:27 -0400464 "qla_target(%d): session for port %8phC (loop ID %d) scheduled for "
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400465 "deletion in %u secs (expires: %lu) immed: %d\n",
Oleksandr Khoshaba7b833552013-08-27 01:37:27 -0400466 sess->vha->vp_idx, sess->port_name, sess->loop_id, dev_loss_tmo,
467 sess->expires, immediate);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400468
469 if (immediate)
470 schedule_delayed_work(&tgt->sess_del_work, 0);
471 else
472 schedule_delayed_work(&tgt->sess_del_work,
Shivaram Upadhyayula63832aa2013-12-10 16:06:40 +0530473 sess->expires - jiffies);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400474}
475
476/* ha->hardware_lock supposed to be held on entry */
477static void qlt_clear_tgt_db(struct qla_tgt *tgt, bool local_only)
478{
479 struct qla_tgt_sess *sess;
480
481 list_for_each_entry(sess, &tgt->sess_list, sess_list_entry)
482 qlt_schedule_sess_for_deletion(sess, true);
483
484 /* At this point tgt could be already dead */
485}
486
487static int qla24xx_get_loop_id(struct scsi_qla_host *vha, const uint8_t *s_id,
488 uint16_t *loop_id)
489{
490 struct qla_hw_data *ha = vha->hw;
491 dma_addr_t gid_list_dma;
492 struct gid_list_info *gid_list;
493 char *id_iter;
494 int res, rc, i;
495 uint16_t entries;
496
497 gid_list = dma_alloc_coherent(&ha->pdev->dev, qla2x00_gid_list_size(ha),
498 &gid_list_dma, GFP_KERNEL);
499 if (!gid_list) {
500 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf044,
501 "qla_target(%d): DMA Alloc failed of %u\n",
502 vha->vp_idx, qla2x00_gid_list_size(ha));
503 return -ENOMEM;
504 }
505
506 /* Get list of logged in devices */
507 rc = qla2x00_get_id_list(vha, gid_list, gid_list_dma, &entries);
508 if (rc != QLA_SUCCESS) {
509 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf045,
510 "qla_target(%d): get_id_list() failed: %x\n",
511 vha->vp_idx, rc);
512 res = -1;
513 goto out_free_id_list;
514 }
515
516 id_iter = (char *)gid_list;
517 res = -1;
518 for (i = 0; i < entries; i++) {
519 struct gid_list_info *gid = (struct gid_list_info *)id_iter;
520 if ((gid->al_pa == s_id[2]) &&
521 (gid->area == s_id[1]) &&
522 (gid->domain == s_id[0])) {
523 *loop_id = le16_to_cpu(gid->loop_id);
524 res = 0;
525 break;
526 }
527 id_iter += ha->gid_list_info_size;
528 }
529
530out_free_id_list:
531 dma_free_coherent(&ha->pdev->dev, qla2x00_gid_list_size(ha),
532 gid_list, gid_list_dma);
533 return res;
534}
535
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400536/* ha->hardware_lock supposed to be held on entry */
537static void qlt_undelete_sess(struct qla_tgt_sess *sess)
538{
539 BUG_ON(!sess->deleted);
540
541 list_del(&sess->del_list_entry);
542 sess->deleted = 0;
543}
544
545static void qlt_del_sess_work_fn(struct delayed_work *work)
546{
547 struct qla_tgt *tgt = container_of(work, struct qla_tgt,
548 sess_del_work);
549 struct scsi_qla_host *vha = tgt->vha;
550 struct qla_hw_data *ha = vha->hw;
551 struct qla_tgt_sess *sess;
Shivaram Upadhyayula63832aa2013-12-10 16:06:40 +0530552 unsigned long flags, elapsed;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400553
554 spin_lock_irqsave(&ha->hardware_lock, flags);
555 while (!list_empty(&tgt->del_sess_list)) {
556 sess = list_entry(tgt->del_sess_list.next, typeof(*sess),
557 del_list_entry);
Shivaram Upadhyayula63832aa2013-12-10 16:06:40 +0530558 elapsed = jiffies;
559 if (time_after_eq(elapsed, sess->expires)) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400560 qlt_undelete_sess(sess);
561
Jörn Engel08234e32013-06-12 16:27:54 -0400562 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf004,
563 "Timeout: sess %p about to be deleted\n",
564 sess);
565 ha->tgt.tgt_ops->shutdown_sess(sess);
566 ha->tgt.tgt_ops->put_sess(sess);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400567 } else {
568 schedule_delayed_work(&tgt->sess_del_work,
Shivaram Upadhyayula63832aa2013-12-10 16:06:40 +0530569 sess->expires - elapsed);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400570 break;
571 }
572 }
573 spin_unlock_irqrestore(&ha->hardware_lock, flags);
574}
575
576/*
577 * Adds an extra ref to allow to drop hw lock after adding sess to the list.
578 * Caller must put it.
579 */
580static struct qla_tgt_sess *qlt_create_sess(
581 struct scsi_qla_host *vha,
582 fc_port_t *fcport,
583 bool local)
584{
585 struct qla_hw_data *ha = vha->hw;
586 struct qla_tgt_sess *sess;
587 unsigned long flags;
588 unsigned char be_sid[3];
589
590 /* Check to avoid double sessions */
591 spin_lock_irqsave(&ha->hardware_lock, flags);
Saurav Kashyap0e8cd712014-01-14 20:40:38 -0800592 list_for_each_entry(sess, &vha->vha_tgt.qla_tgt->sess_list,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400593 sess_list_entry) {
594 if (!memcmp(sess->port_name, fcport->port_name, WWN_SIZE)) {
595 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf005,
596 "Double sess %p found (s_id %x:%x:%x, "
597 "loop_id %d), updating to d_id %x:%x:%x, "
598 "loop_id %d", sess, sess->s_id.b.domain,
599 sess->s_id.b.al_pa, sess->s_id.b.area,
600 sess->loop_id, fcport->d_id.b.domain,
601 fcport->d_id.b.al_pa, fcport->d_id.b.area,
602 fcport->loop_id);
603
604 if (sess->deleted)
605 qlt_undelete_sess(sess);
606
607 kref_get(&sess->se_sess->sess_kref);
Roland Dreierc8292d12012-10-11 13:41:32 -0700608 ha->tgt.tgt_ops->update_sess(sess, fcport->d_id, fcport->loop_id,
609 (fcport->flags & FCF_CONF_COMP_SUPPORTED));
610
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400611 if (sess->local && !local)
612 sess->local = 0;
613 spin_unlock_irqrestore(&ha->hardware_lock, flags);
614
615 return sess;
616 }
617 }
618 spin_unlock_irqrestore(&ha->hardware_lock, flags);
619
620 sess = kzalloc(sizeof(*sess), GFP_KERNEL);
621 if (!sess) {
622 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04a,
Oleksandr Khoshaba7b833552013-08-27 01:37:27 -0400623 "qla_target(%u): session allocation failed, all commands "
624 "from port %8phC will be refused", vha->vp_idx,
625 fcport->port_name);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400626
627 return NULL;
628 }
Saurav Kashyap0e8cd712014-01-14 20:40:38 -0800629 sess->tgt = vha->vha_tgt.qla_tgt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400630 sess->vha = vha;
631 sess->s_id = fcport->d_id;
632 sess->loop_id = fcport->loop_id;
633 sess->local = local;
634
635 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf006,
636 "Adding sess %p to tgt %p via ->check_initiator_node_acl()\n",
Saurav Kashyap0e8cd712014-01-14 20:40:38 -0800637 sess, vha->vha_tgt.qla_tgt);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400638
639 be_sid[0] = sess->s_id.b.domain;
640 be_sid[1] = sess->s_id.b.area;
641 be_sid[2] = sess->s_id.b.al_pa;
642 /*
643 * Determine if this fc_port->port_name is allowed to access
644 * target mode using explict NodeACLs+MappedLUNs, or using
645 * TPG demo mode. If this is successful a target mode FC nexus
646 * is created.
647 */
648 if (ha->tgt.tgt_ops->check_initiator_node_acl(vha,
649 &fcport->port_name[0], sess, &be_sid[0], fcport->loop_id) < 0) {
650 kfree(sess);
651 return NULL;
652 }
653 /*
654 * Take an extra reference to ->sess_kref here to handle qla_tgt_sess
655 * access across ->hardware_lock reaquire.
656 */
657 kref_get(&sess->se_sess->sess_kref);
658
Roland Dreierc8292d12012-10-11 13:41:32 -0700659 sess->conf_compl_supported = (fcport->flags & FCF_CONF_COMP_SUPPORTED);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400660 BUILD_BUG_ON(sizeof(sess->port_name) != sizeof(fcport->port_name));
661 memcpy(sess->port_name, fcport->port_name, sizeof(sess->port_name));
662
663 spin_lock_irqsave(&ha->hardware_lock, flags);
Saurav Kashyap0e8cd712014-01-14 20:40:38 -0800664 list_add_tail(&sess->sess_list_entry, &vha->vha_tgt.qla_tgt->sess_list);
665 vha->vha_tgt.qla_tgt->sess_count++;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400666 spin_unlock_irqrestore(&ha->hardware_lock, flags);
667
668 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04b,
Oleksandr Khoshaba7b833552013-08-27 01:37:27 -0400669 "qla_target(%d): %ssession for wwn %8phC (loop_id %d, "
670 "s_id %x:%x:%x, confirmed completion %ssupported) added\n",
671 vha->vp_idx, local ? "local " : "", fcport->port_name,
672 fcport->loop_id, sess->s_id.b.domain, sess->s_id.b.area,
673 sess->s_id.b.al_pa, sess->conf_compl_supported ? "" : "not ");
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400674
675 return sess;
676}
677
678/*
679 * Called from drivers/scsi/qla2xxx/qla_init.c:qla2x00_reg_remote_port()
680 */
681void qlt_fc_port_added(struct scsi_qla_host *vha, fc_port_t *fcport)
682{
683 struct qla_hw_data *ha = vha->hw;
Saurav Kashyap0e8cd712014-01-14 20:40:38 -0800684 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400685 struct qla_tgt_sess *sess;
686 unsigned long flags;
687
688 if (!vha->hw->tgt.tgt_ops)
689 return;
690
691 if (!tgt || (fcport->port_type != FCT_INITIATOR))
692 return;
693
Saurav Kashyap0e8cd712014-01-14 20:40:38 -0800694 if (qla_ini_mode_enabled(vha))
695 return;
696
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400697 spin_lock_irqsave(&ha->hardware_lock, flags);
698 if (tgt->tgt_stop) {
699 spin_unlock_irqrestore(&ha->hardware_lock, flags);
700 return;
701 }
702 sess = qlt_find_sess_by_port_name(tgt, fcport->port_name);
703 if (!sess) {
704 spin_unlock_irqrestore(&ha->hardware_lock, flags);
705
Saurav Kashyap0e8cd712014-01-14 20:40:38 -0800706 mutex_lock(&vha->vha_tgt.tgt_mutex);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400707 sess = qlt_create_sess(vha, fcport, false);
Saurav Kashyap0e8cd712014-01-14 20:40:38 -0800708 mutex_unlock(&vha->vha_tgt.tgt_mutex);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400709
710 spin_lock_irqsave(&ha->hardware_lock, flags);
711 } else {
712 kref_get(&sess->se_sess->sess_kref);
713
714 if (sess->deleted) {
715 qlt_undelete_sess(sess);
716
717 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04c,
Oleksandr Khoshaba7b833552013-08-27 01:37:27 -0400718 "qla_target(%u): %ssession for port %8phC "
719 "(loop ID %d) reappeared\n", vha->vp_idx,
720 sess->local ? "local " : "", sess->port_name,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400721 sess->loop_id);
722
723 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf007,
724 "Reappeared sess %p\n", sess);
725 }
Roland Dreierc8292d12012-10-11 13:41:32 -0700726 ha->tgt.tgt_ops->update_sess(sess, fcport->d_id, fcport->loop_id,
727 (fcport->flags & FCF_CONF_COMP_SUPPORTED));
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400728 }
729
730 if (sess && sess->local) {
731 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04d,
732 "qla_target(%u): local session for "
Oleksandr Khoshaba7b833552013-08-27 01:37:27 -0400733 "port %8phC (loop ID %d) became global\n", vha->vp_idx,
734 fcport->port_name, sess->loop_id);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400735 sess->local = 0;
736 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400737 ha->tgt.tgt_ops->put_sess(sess);
Jörn Engel08234e32013-06-12 16:27:54 -0400738 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400739}
740
741void qlt_fc_port_deleted(struct scsi_qla_host *vha, fc_port_t *fcport)
742{
743 struct qla_hw_data *ha = vha->hw;
Saurav Kashyap0e8cd712014-01-14 20:40:38 -0800744 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400745 struct qla_tgt_sess *sess;
746 unsigned long flags;
747
748 if (!vha->hw->tgt.tgt_ops)
749 return;
750
751 if (!tgt || (fcport->port_type != FCT_INITIATOR))
752 return;
753
754 spin_lock_irqsave(&ha->hardware_lock, flags);
755 if (tgt->tgt_stop) {
756 spin_unlock_irqrestore(&ha->hardware_lock, flags);
757 return;
758 }
759 sess = qlt_find_sess_by_port_name(tgt, fcport->port_name);
760 if (!sess) {
761 spin_unlock_irqrestore(&ha->hardware_lock, flags);
762 return;
763 }
764
765 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf008, "qla_tgt_fc_port_deleted %p", sess);
766
767 sess->local = 1;
768 qlt_schedule_sess_for_deletion(sess, false);
769 spin_unlock_irqrestore(&ha->hardware_lock, flags);
770}
771
772static inline int test_tgt_sess_count(struct qla_tgt *tgt)
773{
774 struct qla_hw_data *ha = tgt->ha;
775 unsigned long flags;
776 int res;
777 /*
778 * We need to protect against race, when tgt is freed before or
779 * inside wake_up()
780 */
781 spin_lock_irqsave(&ha->hardware_lock, flags);
782 ql_dbg(ql_dbg_tgt, tgt->vha, 0xe002,
783 "tgt %p, empty(sess_list)=%d sess_count=%d\n",
784 tgt, list_empty(&tgt->sess_list), tgt->sess_count);
785 res = (tgt->sess_count == 0);
786 spin_unlock_irqrestore(&ha->hardware_lock, flags);
787
788 return res;
789}
790
791/* Called by tcm_qla2xxx configfs code */
Nicholas Bellinger3c231bd2014-02-19 17:50:22 -0800792int qlt_stop_phase1(struct qla_tgt *tgt)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400793{
794 struct scsi_qla_host *vha = tgt->vha;
795 struct qla_hw_data *ha = tgt->ha;
796 unsigned long flags;
797
Nicholas Bellinger3c231bd2014-02-19 17:50:22 -0800798 mutex_lock(&qla_tgt_mutex);
799 if (!vha->fc_vport) {
800 struct Scsi_Host *sh = vha->host;
801 struct fc_host_attrs *fc_host = shost_to_fc_host(sh);
802 bool npiv_vports;
803
804 spin_lock_irqsave(sh->host_lock, flags);
805 npiv_vports = (fc_host->npiv_vports_inuse);
806 spin_unlock_irqrestore(sh->host_lock, flags);
807
808 if (npiv_vports) {
809 mutex_unlock(&qla_tgt_mutex);
810 return -EPERM;
811 }
812 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400813 if (tgt->tgt_stop || tgt->tgt_stopped) {
814 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04e,
815 "Already in tgt->tgt_stop or tgt_stopped state\n");
Nicholas Bellinger3c231bd2014-02-19 17:50:22 -0800816 mutex_unlock(&qla_tgt_mutex);
817 return -EPERM;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400818 }
819
820 ql_dbg(ql_dbg_tgt, vha, 0xe003, "Stopping target for host %ld(%p)\n",
821 vha->host_no, vha);
822 /*
823 * Mutex needed to sync with qla_tgt_fc_port_[added,deleted].
824 * Lock is needed, because we still can get an incoming packet.
825 */
Saurav Kashyap0e8cd712014-01-14 20:40:38 -0800826 mutex_lock(&vha->vha_tgt.tgt_mutex);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400827 spin_lock_irqsave(&ha->hardware_lock, flags);
828 tgt->tgt_stop = 1;
829 qlt_clear_tgt_db(tgt, true);
830 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Saurav Kashyap0e8cd712014-01-14 20:40:38 -0800831 mutex_unlock(&vha->vha_tgt.tgt_mutex);
Nicholas Bellinger3c231bd2014-02-19 17:50:22 -0800832 mutex_unlock(&qla_tgt_mutex);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400833
Tejun Heo43829732012-08-20 14:51:24 -0700834 flush_delayed_work(&tgt->sess_del_work);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400835
836 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf009,
837 "Waiting for sess works (tgt %p)", tgt);
838 spin_lock_irqsave(&tgt->sess_work_lock, flags);
839 while (!list_empty(&tgt->sess_works_list)) {
840 spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
841 flush_scheduled_work();
842 spin_lock_irqsave(&tgt->sess_work_lock, flags);
843 }
844 spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
845
846 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00a,
847 "Waiting for tgt %p: list_empty(sess_list)=%d "
848 "sess_count=%d\n", tgt, list_empty(&tgt->sess_list),
849 tgt->sess_count);
850
851 wait_event(tgt->waitQ, test_tgt_sess_count(tgt));
852
853 /* Big hammer */
854 if (!ha->flags.host_shutting_down && qla_tgt_mode_enabled(vha))
855 qlt_disable_vha(vha);
856
857 /* Wait for sessions to clear out (just in case) */
858 wait_event(tgt->waitQ, test_tgt_sess_count(tgt));
Nicholas Bellinger3c231bd2014-02-19 17:50:22 -0800859 return 0;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400860}
861EXPORT_SYMBOL(qlt_stop_phase1);
862
863/* Called by tcm_qla2xxx configfs code */
864void qlt_stop_phase2(struct qla_tgt *tgt)
865{
866 struct qla_hw_data *ha = tgt->ha;
Saurav Kashyap0e8cd712014-01-14 20:40:38 -0800867 scsi_qla_host_t *vha = pci_get_drvdata(ha->pdev);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400868 unsigned long flags;
869
870 if (tgt->tgt_stopped) {
Saurav Kashyap0e8cd712014-01-14 20:40:38 -0800871 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04f,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400872 "Already in tgt->tgt_stopped state\n");
873 dump_stack();
874 return;
875 }
876
Saurav Kashyap0e8cd712014-01-14 20:40:38 -0800877 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00b,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400878 "Waiting for %d IRQ commands to complete (tgt %p)",
879 tgt->irq_cmd_count, tgt);
880
Saurav Kashyap0e8cd712014-01-14 20:40:38 -0800881 mutex_lock(&vha->vha_tgt.tgt_mutex);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400882 spin_lock_irqsave(&ha->hardware_lock, flags);
883 while (tgt->irq_cmd_count != 0) {
884 spin_unlock_irqrestore(&ha->hardware_lock, flags);
885 udelay(2);
886 spin_lock_irqsave(&ha->hardware_lock, flags);
887 }
888 tgt->tgt_stop = 0;
889 tgt->tgt_stopped = 1;
890 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Saurav Kashyap0e8cd712014-01-14 20:40:38 -0800891 mutex_unlock(&vha->vha_tgt.tgt_mutex);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400892
Saurav Kashyap0e8cd712014-01-14 20:40:38 -0800893 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00c, "Stop of tgt %p finished",
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400894 tgt);
895}
896EXPORT_SYMBOL(qlt_stop_phase2);
897
898/* Called from qlt_remove_target() -> qla2x00_remove_one() */
Saurav Kashyapfa492632012-11-21 02:40:29 -0500899static void qlt_release(struct qla_tgt *tgt)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400900{
Saurav Kashyap0e8cd712014-01-14 20:40:38 -0800901 scsi_qla_host_t *vha = tgt->vha;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400902
Saurav Kashyap0e8cd712014-01-14 20:40:38 -0800903 if ((vha->vha_tgt.qla_tgt != NULL) && !tgt->tgt_stopped)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400904 qlt_stop_phase2(tgt);
905
Saurav Kashyap0e8cd712014-01-14 20:40:38 -0800906 vha->vha_tgt.qla_tgt = NULL;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400907
Saurav Kashyap0e8cd712014-01-14 20:40:38 -0800908 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00d,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400909 "Release of tgt %p finished\n", tgt);
910
911 kfree(tgt);
912}
913
914/* ha->hardware_lock supposed to be held on entry */
915static int qlt_sched_sess_work(struct qla_tgt *tgt, int type,
916 const void *param, unsigned int param_size)
917{
918 struct qla_tgt_sess_work_param *prm;
919 unsigned long flags;
920
921 prm = kzalloc(sizeof(*prm), GFP_ATOMIC);
922 if (!prm) {
923 ql_dbg(ql_dbg_tgt_mgt, tgt->vha, 0xf050,
924 "qla_target(%d): Unable to create session "
925 "work, command will be refused", 0);
926 return -ENOMEM;
927 }
928
929 ql_dbg(ql_dbg_tgt_mgt, tgt->vha, 0xf00e,
930 "Scheduling work (type %d, prm %p)"
931 " to find session for param %p (size %d, tgt %p)\n",
932 type, prm, param, param_size, tgt);
933
934 prm->type = type;
935 memcpy(&prm->tm_iocb, param, param_size);
936
937 spin_lock_irqsave(&tgt->sess_work_lock, flags);
938 list_add_tail(&prm->sess_works_list_entry, &tgt->sess_works_list);
939 spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
940
941 schedule_work(&tgt->sess_work);
942
943 return 0;
944}
945
946/*
947 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
948 */
949static void qlt_send_notify_ack(struct scsi_qla_host *vha,
950 struct imm_ntfy_from_isp *ntfy,
951 uint32_t add_flags, uint16_t resp_code, int resp_code_valid,
952 uint16_t srr_flags, uint16_t srr_reject_code, uint8_t srr_explan)
953{
954 struct qla_hw_data *ha = vha->hw;
955 request_t *pkt;
956 struct nack_to_isp *nack;
957
958 ql_dbg(ql_dbg_tgt, vha, 0xe004, "Sending NOTIFY_ACK (ha=%p)\n", ha);
959
960 /* Send marker if required */
961 if (qlt_issue_marker(vha, 1) != QLA_SUCCESS)
962 return;
963
964 pkt = (request_t *)qla2x00_alloc_iocbs(vha, NULL);
965 if (!pkt) {
966 ql_dbg(ql_dbg_tgt, vha, 0xe049,
967 "qla_target(%d): %s failed: unable to allocate "
968 "request packet\n", vha->vp_idx, __func__);
969 return;
970 }
971
Saurav Kashyap0e8cd712014-01-14 20:40:38 -0800972 if (vha->vha_tgt.qla_tgt != NULL)
973 vha->vha_tgt.qla_tgt->notify_ack_expected++;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400974
975 pkt->entry_type = NOTIFY_ACK_TYPE;
976 pkt->entry_count = 1;
977
978 nack = (struct nack_to_isp *)pkt;
979 nack->ox_id = ntfy->ox_id;
980
981 nack->u.isp24.nport_handle = ntfy->u.isp24.nport_handle;
982 if (le16_to_cpu(ntfy->u.isp24.status) == IMM_NTFY_ELS) {
983 nack->u.isp24.flags = ntfy->u.isp24.flags &
984 __constant_cpu_to_le32(NOTIFY24XX_FLAGS_PUREX_IOCB);
985 }
986 nack->u.isp24.srr_rx_id = ntfy->u.isp24.srr_rx_id;
987 nack->u.isp24.status = ntfy->u.isp24.status;
988 nack->u.isp24.status_subcode = ntfy->u.isp24.status_subcode;
Arun Easiaa230bc2013-01-30 03:34:39 -0500989 nack->u.isp24.fw_handle = ntfy->u.isp24.fw_handle;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400990 nack->u.isp24.exchange_address = ntfy->u.isp24.exchange_address;
991 nack->u.isp24.srr_rel_offs = ntfy->u.isp24.srr_rel_offs;
992 nack->u.isp24.srr_ui = ntfy->u.isp24.srr_ui;
993 nack->u.isp24.srr_flags = cpu_to_le16(srr_flags);
994 nack->u.isp24.srr_reject_code = srr_reject_code;
995 nack->u.isp24.srr_reject_code_expl = srr_explan;
996 nack->u.isp24.vp_index = ntfy->u.isp24.vp_index;
997
998 ql_dbg(ql_dbg_tgt, vha, 0xe005,
999 "qla_target(%d): Sending 24xx Notify Ack %d\n",
1000 vha->vp_idx, nack->u.isp24.status);
1001
1002 qla2x00_start_iocbs(vha, vha->req);
1003}
1004
1005/*
1006 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
1007 */
1008static void qlt_24xx_send_abts_resp(struct scsi_qla_host *vha,
1009 struct abts_recv_from_24xx *abts, uint32_t status,
1010 bool ids_reversed)
1011{
1012 struct qla_hw_data *ha = vha->hw;
1013 struct abts_resp_to_24xx *resp;
1014 uint32_t f_ctl;
1015 uint8_t *p;
1016
1017 ql_dbg(ql_dbg_tgt, vha, 0xe006,
1018 "Sending task mgmt ABTS response (ha=%p, atio=%p, status=%x\n",
1019 ha, abts, status);
1020
1021 /* Send marker if required */
1022 if (qlt_issue_marker(vha, 1) != QLA_SUCCESS)
1023 return;
1024
1025 resp = (struct abts_resp_to_24xx *)qla2x00_alloc_iocbs(vha, NULL);
1026 if (!resp) {
1027 ql_dbg(ql_dbg_tgt, vha, 0xe04a,
1028 "qla_target(%d): %s failed: unable to allocate "
1029 "request packet", vha->vp_idx, __func__);
1030 return;
1031 }
1032
1033 resp->entry_type = ABTS_RESP_24XX;
1034 resp->entry_count = 1;
1035 resp->nport_handle = abts->nport_handle;
1036 resp->vp_index = vha->vp_idx;
1037 resp->sof_type = abts->sof_type;
1038 resp->exchange_address = abts->exchange_address;
1039 resp->fcp_hdr_le = abts->fcp_hdr_le;
1040 f_ctl = __constant_cpu_to_le32(F_CTL_EXCH_CONTEXT_RESP |
1041 F_CTL_LAST_SEQ | F_CTL_END_SEQ |
1042 F_CTL_SEQ_INITIATIVE);
1043 p = (uint8_t *)&f_ctl;
1044 resp->fcp_hdr_le.f_ctl[0] = *p++;
1045 resp->fcp_hdr_le.f_ctl[1] = *p++;
1046 resp->fcp_hdr_le.f_ctl[2] = *p;
1047 if (ids_reversed) {
1048 resp->fcp_hdr_le.d_id[0] = abts->fcp_hdr_le.d_id[0];
1049 resp->fcp_hdr_le.d_id[1] = abts->fcp_hdr_le.d_id[1];
1050 resp->fcp_hdr_le.d_id[2] = abts->fcp_hdr_le.d_id[2];
1051 resp->fcp_hdr_le.s_id[0] = abts->fcp_hdr_le.s_id[0];
1052 resp->fcp_hdr_le.s_id[1] = abts->fcp_hdr_le.s_id[1];
1053 resp->fcp_hdr_le.s_id[2] = abts->fcp_hdr_le.s_id[2];
1054 } else {
1055 resp->fcp_hdr_le.d_id[0] = abts->fcp_hdr_le.s_id[0];
1056 resp->fcp_hdr_le.d_id[1] = abts->fcp_hdr_le.s_id[1];
1057 resp->fcp_hdr_le.d_id[2] = abts->fcp_hdr_le.s_id[2];
1058 resp->fcp_hdr_le.s_id[0] = abts->fcp_hdr_le.d_id[0];
1059 resp->fcp_hdr_le.s_id[1] = abts->fcp_hdr_le.d_id[1];
1060 resp->fcp_hdr_le.s_id[2] = abts->fcp_hdr_le.d_id[2];
1061 }
1062 resp->exchange_addr_to_abort = abts->exchange_addr_to_abort;
1063 if (status == FCP_TMF_CMPL) {
1064 resp->fcp_hdr_le.r_ctl = R_CTL_BASIC_LINK_SERV | R_CTL_B_ACC;
1065 resp->payload.ba_acct.seq_id_valid = SEQ_ID_INVALID;
1066 resp->payload.ba_acct.low_seq_cnt = 0x0000;
1067 resp->payload.ba_acct.high_seq_cnt = 0xFFFF;
1068 resp->payload.ba_acct.ox_id = abts->fcp_hdr_le.ox_id;
1069 resp->payload.ba_acct.rx_id = abts->fcp_hdr_le.rx_id;
1070 } else {
1071 resp->fcp_hdr_le.r_ctl = R_CTL_BASIC_LINK_SERV | R_CTL_B_RJT;
1072 resp->payload.ba_rjt.reason_code =
1073 BA_RJT_REASON_CODE_UNABLE_TO_PERFORM;
1074 /* Other bytes are zero */
1075 }
1076
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001077 vha->vha_tgt.qla_tgt->abts_resp_expected++;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001078
1079 qla2x00_start_iocbs(vha, vha->req);
1080}
1081
1082/*
1083 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
1084 */
1085static void qlt_24xx_retry_term_exchange(struct scsi_qla_host *vha,
1086 struct abts_resp_from_24xx_fw *entry)
1087{
1088 struct ctio7_to_24xx *ctio;
1089
1090 ql_dbg(ql_dbg_tgt, vha, 0xe007,
1091 "Sending retry TERM EXCH CTIO7 (ha=%p)\n", vha->hw);
1092 /* Send marker if required */
1093 if (qlt_issue_marker(vha, 1) != QLA_SUCCESS)
1094 return;
1095
1096 ctio = (struct ctio7_to_24xx *)qla2x00_alloc_iocbs(vha, NULL);
1097 if (ctio == NULL) {
1098 ql_dbg(ql_dbg_tgt, vha, 0xe04b,
1099 "qla_target(%d): %s failed: unable to allocate "
1100 "request packet\n", vha->vp_idx, __func__);
1101 return;
1102 }
1103
1104 /*
1105 * We've got on entrance firmware's response on by us generated
1106 * ABTS response. So, in it ID fields are reversed.
1107 */
1108
1109 ctio->entry_type = CTIO_TYPE7;
1110 ctio->entry_count = 1;
1111 ctio->nport_handle = entry->nport_handle;
1112 ctio->handle = QLA_TGT_SKIP_HANDLE | CTIO_COMPLETION_HANDLE_MARK;
1113 ctio->timeout = __constant_cpu_to_le16(QLA_TGT_TIMEOUT);
1114 ctio->vp_index = vha->vp_idx;
1115 ctio->initiator_id[0] = entry->fcp_hdr_le.d_id[0];
1116 ctio->initiator_id[1] = entry->fcp_hdr_le.d_id[1];
1117 ctio->initiator_id[2] = entry->fcp_hdr_le.d_id[2];
1118 ctio->exchange_addr = entry->exchange_addr_to_abort;
1119 ctio->u.status1.flags =
1120 __constant_cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_1 |
1121 CTIO7_FLAGS_TERMINATE);
1122 ctio->u.status1.ox_id = entry->fcp_hdr_le.ox_id;
1123
1124 qla2x00_start_iocbs(vha, vha->req);
1125
1126 qlt_24xx_send_abts_resp(vha, (struct abts_recv_from_24xx *)entry,
1127 FCP_TMF_CMPL, true);
1128}
1129
1130/* ha->hardware_lock supposed to be held on entry */
1131static int __qlt_24xx_handle_abts(struct scsi_qla_host *vha,
1132 struct abts_recv_from_24xx *abts, struct qla_tgt_sess *sess)
1133{
1134 struct qla_hw_data *ha = vha->hw;
Steve Hodgson06e97b42012-11-16 08:06:17 -08001135 struct se_session *se_sess = sess->se_sess;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001136 struct qla_tgt_mgmt_cmd *mcmd;
Steve Hodgson06e97b42012-11-16 08:06:17 -08001137 struct se_cmd *se_cmd;
1138 u32 lun = 0;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001139 int rc;
Steve Hodgson06e97b42012-11-16 08:06:17 -08001140 bool found_lun = false;
1141
1142 spin_lock(&se_sess->sess_cmd_lock);
1143 list_for_each_entry(se_cmd, &se_sess->sess_cmd_list, se_cmd_list) {
1144 struct qla_tgt_cmd *cmd =
1145 container_of(se_cmd, struct qla_tgt_cmd, se_cmd);
1146 if (cmd->tag == abts->exchange_addr_to_abort) {
1147 lun = cmd->unpacked_lun;
1148 found_lun = true;
1149 break;
1150 }
1151 }
1152 spin_unlock(&se_sess->sess_cmd_lock);
1153
1154 if (!found_lun)
1155 return -ENOENT;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001156
1157 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00f,
1158 "qla_target(%d): task abort (tag=%d)\n",
1159 vha->vp_idx, abts->exchange_addr_to_abort);
1160
1161 mcmd = mempool_alloc(qla_tgt_mgmt_cmd_mempool, GFP_ATOMIC);
1162 if (mcmd == NULL) {
1163 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf051,
1164 "qla_target(%d): %s: Allocation of ABORT cmd failed",
1165 vha->vp_idx, __func__);
1166 return -ENOMEM;
1167 }
1168 memset(mcmd, 0, sizeof(*mcmd));
1169
1170 mcmd->sess = sess;
1171 memcpy(&mcmd->orig_iocb.abts, abts, sizeof(mcmd->orig_iocb.abts));
1172
Steve Hodgson06e97b42012-11-16 08:06:17 -08001173 rc = ha->tgt.tgt_ops->handle_tmr(mcmd, lun, TMR_ABORT_TASK,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001174 abts->exchange_addr_to_abort);
1175 if (rc != 0) {
1176 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf052,
1177 "qla_target(%d): tgt_ops->handle_tmr()"
1178 " failed: %d", vha->vp_idx, rc);
1179 mempool_free(mcmd, qla_tgt_mgmt_cmd_mempool);
1180 return -EFAULT;
1181 }
1182
1183 return 0;
1184}
1185
1186/*
1187 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
1188 */
1189static void qlt_24xx_handle_abts(struct scsi_qla_host *vha,
1190 struct abts_recv_from_24xx *abts)
1191{
1192 struct qla_hw_data *ha = vha->hw;
1193 struct qla_tgt_sess *sess;
1194 uint32_t tag = abts->exchange_addr_to_abort;
1195 uint8_t s_id[3];
1196 int rc;
1197
1198 if (le32_to_cpu(abts->fcp_hdr_le.parameter) & ABTS_PARAM_ABORT_SEQ) {
1199 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf053,
1200 "qla_target(%d): ABTS: Abort Sequence not "
1201 "supported\n", vha->vp_idx);
1202 qlt_24xx_send_abts_resp(vha, abts, FCP_TMF_REJECTED, false);
1203 return;
1204 }
1205
1206 if (tag == ATIO_EXCHANGE_ADDRESS_UNKNOWN) {
1207 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf010,
1208 "qla_target(%d): ABTS: Unknown Exchange "
1209 "Address received\n", vha->vp_idx);
1210 qlt_24xx_send_abts_resp(vha, abts, FCP_TMF_REJECTED, false);
1211 return;
1212 }
1213
1214 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf011,
1215 "qla_target(%d): task abort (s_id=%x:%x:%x, "
1216 "tag=%d, param=%x)\n", vha->vp_idx, abts->fcp_hdr_le.s_id[2],
1217 abts->fcp_hdr_le.s_id[1], abts->fcp_hdr_le.s_id[0], tag,
1218 le32_to_cpu(abts->fcp_hdr_le.parameter));
1219
1220 s_id[0] = abts->fcp_hdr_le.s_id[2];
1221 s_id[1] = abts->fcp_hdr_le.s_id[1];
1222 s_id[2] = abts->fcp_hdr_le.s_id[0];
1223
1224 sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha, s_id);
1225 if (!sess) {
1226 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf012,
1227 "qla_target(%d): task abort for non-existant session\n",
1228 vha->vp_idx);
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001229 rc = qlt_sched_sess_work(vha->vha_tgt.qla_tgt,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001230 QLA_TGT_SESS_WORK_ABORT, abts, sizeof(*abts));
1231 if (rc != 0) {
1232 qlt_24xx_send_abts_resp(vha, abts, FCP_TMF_REJECTED,
1233 false);
1234 }
1235 return;
1236 }
1237
1238 rc = __qlt_24xx_handle_abts(vha, abts, sess);
1239 if (rc != 0) {
1240 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf054,
1241 "qla_target(%d): __qlt_24xx_handle_abts() failed: %d\n",
1242 vha->vp_idx, rc);
1243 qlt_24xx_send_abts_resp(vha, abts, FCP_TMF_REJECTED, false);
1244 return;
1245 }
1246}
1247
1248/*
1249 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
1250 */
1251static void qlt_24xx_send_task_mgmt_ctio(struct scsi_qla_host *ha,
1252 struct qla_tgt_mgmt_cmd *mcmd, uint32_t resp_code)
1253{
1254 struct atio_from_isp *atio = &mcmd->orig_iocb.atio;
1255 struct ctio7_to_24xx *ctio;
1256
1257 ql_dbg(ql_dbg_tgt, ha, 0xe008,
1258 "Sending task mgmt CTIO7 (ha=%p, atio=%p, resp_code=%x\n",
1259 ha, atio, resp_code);
1260
1261 /* Send marker if required */
1262 if (qlt_issue_marker(ha, 1) != QLA_SUCCESS)
1263 return;
1264
1265 ctio = (struct ctio7_to_24xx *)qla2x00_alloc_iocbs(ha, NULL);
1266 if (ctio == NULL) {
1267 ql_dbg(ql_dbg_tgt, ha, 0xe04c,
1268 "qla_target(%d): %s failed: unable to allocate "
1269 "request packet\n", ha->vp_idx, __func__);
1270 return;
1271 }
1272
1273 ctio->entry_type = CTIO_TYPE7;
1274 ctio->entry_count = 1;
1275 ctio->handle = QLA_TGT_SKIP_HANDLE | CTIO_COMPLETION_HANDLE_MARK;
1276 ctio->nport_handle = mcmd->sess->loop_id;
1277 ctio->timeout = __constant_cpu_to_le16(QLA_TGT_TIMEOUT);
1278 ctio->vp_index = ha->vp_idx;
1279 ctio->initiator_id[0] = atio->u.isp24.fcp_hdr.s_id[2];
1280 ctio->initiator_id[1] = atio->u.isp24.fcp_hdr.s_id[1];
1281 ctio->initiator_id[2] = atio->u.isp24.fcp_hdr.s_id[0];
1282 ctio->exchange_addr = atio->u.isp24.exchange_addr;
1283 ctio->u.status1.flags = (atio->u.isp24.attr << 9) |
1284 __constant_cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_1 |
1285 CTIO7_FLAGS_SEND_STATUS);
1286 ctio->u.status1.ox_id = swab16(atio->u.isp24.fcp_hdr.ox_id);
1287 ctio->u.status1.scsi_status =
1288 __constant_cpu_to_le16(SS_RESPONSE_INFO_LEN_VALID);
1289 ctio->u.status1.response_len = __constant_cpu_to_le16(8);
Roland Dreiere4b11b82012-09-18 15:10:56 -07001290 ctio->u.status1.sense_data[0] = resp_code;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001291
1292 qla2x00_start_iocbs(ha, ha->req);
1293}
1294
1295void qlt_free_mcmd(struct qla_tgt_mgmt_cmd *mcmd)
1296{
1297 mempool_free(mcmd, qla_tgt_mgmt_cmd_mempool);
1298}
1299EXPORT_SYMBOL(qlt_free_mcmd);
1300
1301/* callback from target fabric module code */
1302void qlt_xmit_tm_rsp(struct qla_tgt_mgmt_cmd *mcmd)
1303{
1304 struct scsi_qla_host *vha = mcmd->sess->vha;
1305 struct qla_hw_data *ha = vha->hw;
1306 unsigned long flags;
1307
1308 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf013,
1309 "TM response mcmd (%p) status %#x state %#x",
1310 mcmd, mcmd->fc_tm_rsp, mcmd->flags);
1311
1312 spin_lock_irqsave(&ha->hardware_lock, flags);
1313 if (mcmd->flags == QLA24XX_MGMT_SEND_NACK)
1314 qlt_send_notify_ack(vha, &mcmd->orig_iocb.imm_ntfy,
1315 0, 0, 0, 0, 0, 0);
1316 else {
1317 if (mcmd->se_cmd.se_tmr_req->function == TMR_ABORT_TASK)
1318 qlt_24xx_send_abts_resp(vha, &mcmd->orig_iocb.abts,
1319 mcmd->fc_tm_rsp, false);
1320 else
1321 qlt_24xx_send_task_mgmt_ctio(vha, mcmd,
1322 mcmd->fc_tm_rsp);
1323 }
1324 /*
1325 * Make the callback for ->free_mcmd() to queue_work() and invoke
1326 * target_put_sess_cmd() to drop cmd_kref to 1. The final
1327 * target_put_sess_cmd() call will be made from TFO->check_stop_free()
1328 * -> tcm_qla2xxx_check_stop_free() to release the TMR associated se_cmd
1329 * descriptor after TFO->queue_tm_rsp() -> tcm_qla2xxx_queue_tm_rsp() ->
1330 * qlt_xmit_tm_rsp() returns here..
1331 */
1332 ha->tgt.tgt_ops->free_mcmd(mcmd);
1333 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1334}
1335EXPORT_SYMBOL(qlt_xmit_tm_rsp);
1336
1337/* No locks */
1338static int qlt_pci_map_calc_cnt(struct qla_tgt_prm *prm)
1339{
1340 struct qla_tgt_cmd *cmd = prm->cmd;
1341
1342 BUG_ON(cmd->sg_cnt == 0);
1343
1344 prm->sg = (struct scatterlist *)cmd->sg;
1345 prm->seg_cnt = pci_map_sg(prm->tgt->ha->pdev, cmd->sg,
1346 cmd->sg_cnt, cmd->dma_data_direction);
1347 if (unlikely(prm->seg_cnt == 0))
1348 goto out_err;
1349
1350 prm->cmd->sg_mapped = 1;
1351
1352 /*
1353 * If greater than four sg entries then we need to allocate
1354 * the continuation entries
1355 */
1356 if (prm->seg_cnt > prm->tgt->datasegs_per_cmd)
1357 prm->req_cnt += DIV_ROUND_UP(prm->seg_cnt -
1358 prm->tgt->datasegs_per_cmd, prm->tgt->datasegs_per_cont);
1359
1360 ql_dbg(ql_dbg_tgt, prm->cmd->vha, 0xe009, "seg_cnt=%d, req_cnt=%d\n",
1361 prm->seg_cnt, prm->req_cnt);
1362 return 0;
1363
1364out_err:
1365 ql_dbg(ql_dbg_tgt, prm->cmd->vha, 0xe04d,
1366 "qla_target(%d): PCI mapping failed: sg_cnt=%d",
1367 0, prm->cmd->sg_cnt);
1368 return -1;
1369}
1370
1371static inline void qlt_unmap_sg(struct scsi_qla_host *vha,
1372 struct qla_tgt_cmd *cmd)
1373{
1374 struct qla_hw_data *ha = vha->hw;
1375
1376 BUG_ON(!cmd->sg_mapped);
1377 pci_unmap_sg(ha->pdev, cmd->sg, cmd->sg_cnt, cmd->dma_data_direction);
1378 cmd->sg_mapped = 0;
1379}
1380
1381static int qlt_check_reserve_free_req(struct scsi_qla_host *vha,
1382 uint32_t req_cnt)
1383{
1384 struct qla_hw_data *ha = vha->hw;
1385 device_reg_t __iomem *reg = ha->iobase;
1386 uint32_t cnt;
1387
1388 if (vha->req->cnt < (req_cnt + 2)) {
1389 cnt = (uint16_t)RD_REG_DWORD(&reg->isp24.req_q_out);
1390
1391 ql_dbg(ql_dbg_tgt, vha, 0xe00a,
1392 "Request ring circled: cnt=%d, vha->->ring_index=%d, "
1393 "vha->req->cnt=%d, req_cnt=%d\n", cnt,
1394 vha->req->ring_index, vha->req->cnt, req_cnt);
1395 if (vha->req->ring_index < cnt)
1396 vha->req->cnt = cnt - vha->req->ring_index;
1397 else
1398 vha->req->cnt = vha->req->length -
1399 (vha->req->ring_index - cnt);
1400 }
1401
1402 if (unlikely(vha->req->cnt < (req_cnt + 2))) {
1403 ql_dbg(ql_dbg_tgt, vha, 0xe00b,
1404 "qla_target(%d): There is no room in the "
1405 "request ring: vha->req->ring_index=%d, vha->req->cnt=%d, "
1406 "req_cnt=%d\n", vha->vp_idx, vha->req->ring_index,
1407 vha->req->cnt, req_cnt);
1408 return -EAGAIN;
1409 }
1410 vha->req->cnt -= req_cnt;
1411
1412 return 0;
1413}
1414
1415/*
1416 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
1417 */
1418static inline void *qlt_get_req_pkt(struct scsi_qla_host *vha)
1419{
1420 /* Adjust ring index. */
1421 vha->req->ring_index++;
1422 if (vha->req->ring_index == vha->req->length) {
1423 vha->req->ring_index = 0;
1424 vha->req->ring_ptr = vha->req->ring;
1425 } else {
1426 vha->req->ring_ptr++;
1427 }
1428 return (cont_entry_t *)vha->req->ring_ptr;
1429}
1430
1431/* ha->hardware_lock supposed to be held on entry */
1432static inline uint32_t qlt_make_handle(struct scsi_qla_host *vha)
1433{
1434 struct qla_hw_data *ha = vha->hw;
1435 uint32_t h;
1436
1437 h = ha->tgt.current_handle;
1438 /* always increment cmd handle */
1439 do {
1440 ++h;
Chad Dupuis8d93f552013-01-30 03:34:37 -05001441 if (h > DEFAULT_OUTSTANDING_COMMANDS)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001442 h = 1; /* 0 is QLA_TGT_NULL_HANDLE */
1443 if (h == ha->tgt.current_handle) {
1444 ql_dbg(ql_dbg_tgt, vha, 0xe04e,
1445 "qla_target(%d): Ran out of "
1446 "empty cmd slots in ha %p\n", vha->vp_idx, ha);
1447 h = QLA_TGT_NULL_HANDLE;
1448 break;
1449 }
1450 } while ((h == QLA_TGT_NULL_HANDLE) ||
1451 (h == QLA_TGT_SKIP_HANDLE) ||
1452 (ha->tgt.cmds[h-1] != NULL));
1453
1454 if (h != QLA_TGT_NULL_HANDLE)
1455 ha->tgt.current_handle = h;
1456
1457 return h;
1458}
1459
1460/* ha->hardware_lock supposed to be held on entry */
1461static int qlt_24xx_build_ctio_pkt(struct qla_tgt_prm *prm,
1462 struct scsi_qla_host *vha)
1463{
1464 uint32_t h;
1465 struct ctio7_to_24xx *pkt;
1466 struct qla_hw_data *ha = vha->hw;
1467 struct atio_from_isp *atio = &prm->cmd->atio;
1468
1469 pkt = (struct ctio7_to_24xx *)vha->req->ring_ptr;
1470 prm->pkt = pkt;
1471 memset(pkt, 0, sizeof(*pkt));
1472
1473 pkt->entry_type = CTIO_TYPE7;
1474 pkt->entry_count = (uint8_t)prm->req_cnt;
1475 pkt->vp_index = vha->vp_idx;
1476
1477 h = qlt_make_handle(vha);
1478 if (unlikely(h == QLA_TGT_NULL_HANDLE)) {
1479 /*
1480 * CTIO type 7 from the firmware doesn't provide a way to
1481 * know the initiator's LOOP ID, hence we can't find
1482 * the session and, so, the command.
1483 */
1484 return -EAGAIN;
1485 } else
1486 ha->tgt.cmds[h-1] = prm->cmd;
1487
1488 pkt->handle = h | CTIO_COMPLETION_HANDLE_MARK;
1489 pkt->nport_handle = prm->cmd->loop_id;
1490 pkt->timeout = __constant_cpu_to_le16(QLA_TGT_TIMEOUT);
1491 pkt->initiator_id[0] = atio->u.isp24.fcp_hdr.s_id[2];
1492 pkt->initiator_id[1] = atio->u.isp24.fcp_hdr.s_id[1];
1493 pkt->initiator_id[2] = atio->u.isp24.fcp_hdr.s_id[0];
1494 pkt->exchange_addr = atio->u.isp24.exchange_addr;
1495 pkt->u.status0.flags |= (atio->u.isp24.attr << 9);
1496 pkt->u.status0.ox_id = swab16(atio->u.isp24.fcp_hdr.ox_id);
1497 pkt->u.status0.relative_offset = cpu_to_le32(prm->cmd->offset);
1498
1499 ql_dbg(ql_dbg_tgt, vha, 0xe00c,
1500 "qla_target(%d): handle(cmd) -> %08x, timeout %d, ox_id %#x\n",
1501 vha->vp_idx, pkt->handle, QLA_TGT_TIMEOUT,
1502 le16_to_cpu(pkt->u.status0.ox_id));
1503 return 0;
1504}
1505
1506/*
1507 * ha->hardware_lock supposed to be held on entry. We have already made sure
1508 * that there is sufficient amount of request entries to not drop it.
1509 */
1510static void qlt_load_cont_data_segments(struct qla_tgt_prm *prm,
1511 struct scsi_qla_host *vha)
1512{
1513 int cnt;
1514 uint32_t *dword_ptr;
1515 int enable_64bit_addressing = prm->tgt->tgt_enable_64bit_addr;
1516
1517 /* Build continuation packets */
1518 while (prm->seg_cnt > 0) {
1519 cont_a64_entry_t *cont_pkt64 =
1520 (cont_a64_entry_t *)qlt_get_req_pkt(vha);
1521
1522 /*
1523 * Make sure that from cont_pkt64 none of
1524 * 64-bit specific fields used for 32-bit
1525 * addressing. Cast to (cont_entry_t *) for
1526 * that.
1527 */
1528
1529 memset(cont_pkt64, 0, sizeof(*cont_pkt64));
1530
1531 cont_pkt64->entry_count = 1;
1532 cont_pkt64->sys_define = 0;
1533
1534 if (enable_64bit_addressing) {
1535 cont_pkt64->entry_type = CONTINUE_A64_TYPE;
1536 dword_ptr =
1537 (uint32_t *)&cont_pkt64->dseg_0_address;
1538 } else {
1539 cont_pkt64->entry_type = CONTINUE_TYPE;
1540 dword_ptr =
1541 (uint32_t *)&((cont_entry_t *)
1542 cont_pkt64)->dseg_0_address;
1543 }
1544
1545 /* Load continuation entry data segments */
1546 for (cnt = 0;
1547 cnt < prm->tgt->datasegs_per_cont && prm->seg_cnt;
1548 cnt++, prm->seg_cnt--) {
1549 *dword_ptr++ =
1550 cpu_to_le32(pci_dma_lo32
1551 (sg_dma_address(prm->sg)));
1552 if (enable_64bit_addressing) {
1553 *dword_ptr++ =
1554 cpu_to_le32(pci_dma_hi32
1555 (sg_dma_address
1556 (prm->sg)));
1557 }
1558 *dword_ptr++ = cpu_to_le32(sg_dma_len(prm->sg));
1559
1560 ql_dbg(ql_dbg_tgt, vha, 0xe00d,
1561 "S/G Segment Cont. phys_addr=%llx:%llx, len=%d\n",
1562 (long long unsigned int)
1563 pci_dma_hi32(sg_dma_address(prm->sg)),
1564 (long long unsigned int)
1565 pci_dma_lo32(sg_dma_address(prm->sg)),
1566 (int)sg_dma_len(prm->sg));
1567
1568 prm->sg = sg_next(prm->sg);
1569 }
1570 }
1571}
1572
1573/*
1574 * ha->hardware_lock supposed to be held on entry. We have already made sure
1575 * that there is sufficient amount of request entries to not drop it.
1576 */
1577static void qlt_load_data_segments(struct qla_tgt_prm *prm,
1578 struct scsi_qla_host *vha)
1579{
1580 int cnt;
1581 uint32_t *dword_ptr;
1582 int enable_64bit_addressing = prm->tgt->tgt_enable_64bit_addr;
1583 struct ctio7_to_24xx *pkt24 = (struct ctio7_to_24xx *)prm->pkt;
1584
1585 ql_dbg(ql_dbg_tgt, vha, 0xe00e,
1586 "iocb->scsi_status=%x, iocb->flags=%x\n",
1587 le16_to_cpu(pkt24->u.status0.scsi_status),
1588 le16_to_cpu(pkt24->u.status0.flags));
1589
1590 pkt24->u.status0.transfer_length = cpu_to_le32(prm->cmd->bufflen);
1591
1592 /* Setup packet address segment pointer */
1593 dword_ptr = pkt24->u.status0.dseg_0_address;
1594
1595 /* Set total data segment count */
1596 if (prm->seg_cnt)
1597 pkt24->dseg_count = cpu_to_le16(prm->seg_cnt);
1598
1599 if (prm->seg_cnt == 0) {
1600 /* No data transfer */
1601 *dword_ptr++ = 0;
1602 *dword_ptr = 0;
1603 return;
1604 }
1605
1606 /* If scatter gather */
1607 ql_dbg(ql_dbg_tgt, vha, 0xe00f, "%s", "Building S/G data segments...");
1608
1609 /* Load command entry data segments */
1610 for (cnt = 0;
1611 (cnt < prm->tgt->datasegs_per_cmd) && prm->seg_cnt;
1612 cnt++, prm->seg_cnt--) {
1613 *dword_ptr++ =
1614 cpu_to_le32(pci_dma_lo32(sg_dma_address(prm->sg)));
1615 if (enable_64bit_addressing) {
1616 *dword_ptr++ =
1617 cpu_to_le32(pci_dma_hi32(
1618 sg_dma_address(prm->sg)));
1619 }
1620 *dword_ptr++ = cpu_to_le32(sg_dma_len(prm->sg));
1621
1622 ql_dbg(ql_dbg_tgt, vha, 0xe010,
1623 "S/G Segment phys_addr=%llx:%llx, len=%d\n",
1624 (long long unsigned int)pci_dma_hi32(sg_dma_address(
1625 prm->sg)),
1626 (long long unsigned int)pci_dma_lo32(sg_dma_address(
1627 prm->sg)),
1628 (int)sg_dma_len(prm->sg));
1629
1630 prm->sg = sg_next(prm->sg);
1631 }
1632
1633 qlt_load_cont_data_segments(prm, vha);
1634}
1635
1636static inline int qlt_has_data(struct qla_tgt_cmd *cmd)
1637{
1638 return cmd->bufflen > 0;
1639}
1640
1641/*
1642 * Called without ha->hardware_lock held
1643 */
1644static int qlt_pre_xmit_response(struct qla_tgt_cmd *cmd,
1645 struct qla_tgt_prm *prm, int xmit_type, uint8_t scsi_status,
1646 uint32_t *full_req_cnt)
1647{
1648 struct qla_tgt *tgt = cmd->tgt;
1649 struct scsi_qla_host *vha = tgt->vha;
1650 struct qla_hw_data *ha = vha->hw;
1651 struct se_cmd *se_cmd = &cmd->se_cmd;
1652
1653 if (unlikely(cmd->aborted)) {
1654 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf014,
1655 "qla_target(%d): terminating exchange "
1656 "for aborted cmd=%p (se_cmd=%p, tag=%d)", vha->vp_idx, cmd,
1657 se_cmd, cmd->tag);
1658
1659 cmd->state = QLA_TGT_STATE_ABORTED;
1660
1661 qlt_send_term_exchange(vha, cmd, &cmd->atio, 0);
1662
1663 /* !! At this point cmd could be already freed !! */
1664 return QLA_TGT_PRE_XMIT_RESP_CMD_ABORTED;
1665 }
1666
1667 ql_dbg(ql_dbg_tgt, vha, 0xe011, "qla_target(%d): tag=%u\n",
1668 vha->vp_idx, cmd->tag);
1669
1670 prm->cmd = cmd;
1671 prm->tgt = tgt;
1672 prm->rq_result = scsi_status;
1673 prm->sense_buffer = &cmd->sense_buffer[0];
1674 prm->sense_buffer_len = TRANSPORT_SENSE_BUFFER;
1675 prm->sg = NULL;
1676 prm->seg_cnt = -1;
1677 prm->req_cnt = 1;
1678 prm->add_status_pkt = 0;
1679
1680 ql_dbg(ql_dbg_tgt, vha, 0xe012, "rq_result=%x, xmit_type=%x\n",
1681 prm->rq_result, xmit_type);
1682
1683 /* Send marker if required */
1684 if (qlt_issue_marker(vha, 0) != QLA_SUCCESS)
1685 return -EFAULT;
1686
1687 ql_dbg(ql_dbg_tgt, vha, 0xe013, "CTIO start: vha(%d)\n", vha->vp_idx);
1688
1689 if ((xmit_type & QLA_TGT_XMIT_DATA) && qlt_has_data(cmd)) {
1690 if (qlt_pci_map_calc_cnt(prm) != 0)
1691 return -EAGAIN;
1692 }
1693
1694 *full_req_cnt = prm->req_cnt;
1695
1696 if (se_cmd->se_cmd_flags & SCF_UNDERFLOW_BIT) {
1697 prm->residual = se_cmd->residual_count;
1698 ql_dbg(ql_dbg_tgt, vha, 0xe014,
1699 "Residual underflow: %d (tag %d, "
1700 "op %x, bufflen %d, rq_result %x)\n", prm->residual,
1701 cmd->tag, se_cmd->t_task_cdb ? se_cmd->t_task_cdb[0] : 0,
1702 cmd->bufflen, prm->rq_result);
1703 prm->rq_result |= SS_RESIDUAL_UNDER;
1704 } else if (se_cmd->se_cmd_flags & SCF_OVERFLOW_BIT) {
1705 prm->residual = se_cmd->residual_count;
1706 ql_dbg(ql_dbg_tgt, vha, 0xe015,
1707 "Residual overflow: %d (tag %d, "
1708 "op %x, bufflen %d, rq_result %x)\n", prm->residual,
1709 cmd->tag, se_cmd->t_task_cdb ? se_cmd->t_task_cdb[0] : 0,
1710 cmd->bufflen, prm->rq_result);
1711 prm->rq_result |= SS_RESIDUAL_OVER;
1712 }
1713
1714 if (xmit_type & QLA_TGT_XMIT_STATUS) {
1715 /*
1716 * If QLA_TGT_XMIT_DATA is not set, add_status_pkt will be
1717 * ignored in *xmit_response() below
1718 */
1719 if (qlt_has_data(cmd)) {
1720 if (QLA_TGT_SENSE_VALID(prm->sense_buffer) ||
1721 (IS_FWI2_CAPABLE(ha) &&
1722 (prm->rq_result != 0))) {
1723 prm->add_status_pkt = 1;
1724 (*full_req_cnt)++;
1725 }
1726 }
1727 }
1728
1729 ql_dbg(ql_dbg_tgt, vha, 0xe016,
1730 "req_cnt=%d, full_req_cnt=%d, add_status_pkt=%d\n",
1731 prm->req_cnt, *full_req_cnt, prm->add_status_pkt);
1732
1733 return 0;
1734}
1735
1736static inline int qlt_need_explicit_conf(struct qla_hw_data *ha,
1737 struct qla_tgt_cmd *cmd, int sending_sense)
1738{
1739 if (ha->tgt.enable_class_2)
1740 return 0;
1741
1742 if (sending_sense)
1743 return cmd->conf_compl_supported;
1744 else
1745 return ha->tgt.enable_explicit_conf &&
1746 cmd->conf_compl_supported;
1747}
1748
1749#ifdef CONFIG_QLA_TGT_DEBUG_SRR
1750/*
1751 * Original taken from the XFS code
1752 */
1753static unsigned long qlt_srr_random(void)
1754{
1755 static int Inited;
1756 static unsigned long RandomValue;
1757 static DEFINE_SPINLOCK(lock);
1758 /* cycles pseudo-randomly through all values between 1 and 2^31 - 2 */
1759 register long rv;
1760 register long lo;
1761 register long hi;
1762 unsigned long flags;
1763
1764 spin_lock_irqsave(&lock, flags);
1765 if (!Inited) {
1766 RandomValue = jiffies;
1767 Inited = 1;
1768 }
1769 rv = RandomValue;
1770 hi = rv / 127773;
1771 lo = rv % 127773;
1772 rv = 16807 * lo - 2836 * hi;
1773 if (rv <= 0)
1774 rv += 2147483647;
1775 RandomValue = rv;
1776 spin_unlock_irqrestore(&lock, flags);
1777 return rv;
1778}
1779
1780static void qlt_check_srr_debug(struct qla_tgt_cmd *cmd, int *xmit_type)
1781{
1782#if 0 /* This is not a real status packets lost, so it won't lead to SRR */
1783 if ((*xmit_type & QLA_TGT_XMIT_STATUS) && (qlt_srr_random() % 200)
1784 == 50) {
1785 *xmit_type &= ~QLA_TGT_XMIT_STATUS;
1786 ql_dbg(ql_dbg_tgt_mgt, cmd->vha, 0xf015,
1787 "Dropping cmd %p (tag %d) status", cmd, cmd->tag);
1788 }
1789#endif
1790 /*
1791 * It's currently not possible to simulate SRRs for FCP_WRITE without
1792 * a physical link layer failure, so don't even try here..
1793 */
1794 if (cmd->dma_data_direction != DMA_FROM_DEVICE)
1795 return;
1796
1797 if (qlt_has_data(cmd) && (cmd->sg_cnt > 1) &&
1798 ((qlt_srr_random() % 100) == 20)) {
1799 int i, leave = 0;
1800 unsigned int tot_len = 0;
1801
1802 while (leave == 0)
1803 leave = qlt_srr_random() % cmd->sg_cnt;
1804
1805 for (i = 0; i < leave; i++)
1806 tot_len += cmd->sg[i].length;
1807
1808 ql_dbg(ql_dbg_tgt_mgt, cmd->vha, 0xf016,
1809 "Cutting cmd %p (tag %d) buffer"
1810 " tail to len %d, sg_cnt %d (cmd->bufflen %d,"
1811 " cmd->sg_cnt %d)", cmd, cmd->tag, tot_len, leave,
1812 cmd->bufflen, cmd->sg_cnt);
1813
1814 cmd->bufflen = tot_len;
1815 cmd->sg_cnt = leave;
1816 }
1817
1818 if (qlt_has_data(cmd) && ((qlt_srr_random() % 100) == 70)) {
1819 unsigned int offset = qlt_srr_random() % cmd->bufflen;
1820
1821 ql_dbg(ql_dbg_tgt_mgt, cmd->vha, 0xf017,
1822 "Cutting cmd %p (tag %d) buffer head "
1823 "to offset %d (cmd->bufflen %d)", cmd, cmd->tag, offset,
1824 cmd->bufflen);
1825 if (offset == 0)
1826 *xmit_type &= ~QLA_TGT_XMIT_DATA;
1827 else if (qlt_set_data_offset(cmd, offset)) {
1828 ql_dbg(ql_dbg_tgt_mgt, cmd->vha, 0xf018,
1829 "qlt_set_data_offset() failed (tag %d)", cmd->tag);
1830 }
1831 }
1832}
1833#else
1834static inline void qlt_check_srr_debug(struct qla_tgt_cmd *cmd, int *xmit_type)
1835{}
1836#endif
1837
1838static void qlt_24xx_init_ctio_to_isp(struct ctio7_to_24xx *ctio,
1839 struct qla_tgt_prm *prm)
1840{
1841 prm->sense_buffer_len = min_t(uint32_t, prm->sense_buffer_len,
1842 (uint32_t)sizeof(ctio->u.status1.sense_data));
1843 ctio->u.status0.flags |=
1844 __constant_cpu_to_le16(CTIO7_FLAGS_SEND_STATUS);
1845 if (qlt_need_explicit_conf(prm->tgt->ha, prm->cmd, 0)) {
1846 ctio->u.status0.flags |= __constant_cpu_to_le16(
1847 CTIO7_FLAGS_EXPLICIT_CONFORM |
1848 CTIO7_FLAGS_CONFORM_REQ);
1849 }
1850 ctio->u.status0.residual = cpu_to_le32(prm->residual);
1851 ctio->u.status0.scsi_status = cpu_to_le16(prm->rq_result);
1852 if (QLA_TGT_SENSE_VALID(prm->sense_buffer)) {
1853 int i;
1854
1855 if (qlt_need_explicit_conf(prm->tgt->ha, prm->cmd, 1)) {
1856 if (prm->cmd->se_cmd.scsi_status != 0) {
1857 ql_dbg(ql_dbg_tgt, prm->cmd->vha, 0xe017,
1858 "Skipping EXPLICIT_CONFORM and "
1859 "CTIO7_FLAGS_CONFORM_REQ for FCP READ w/ "
1860 "non GOOD status\n");
1861 goto skip_explict_conf;
1862 }
1863 ctio->u.status1.flags |= __constant_cpu_to_le16(
1864 CTIO7_FLAGS_EXPLICIT_CONFORM |
1865 CTIO7_FLAGS_CONFORM_REQ);
1866 }
1867skip_explict_conf:
1868 ctio->u.status1.flags &=
1869 ~__constant_cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_0);
1870 ctio->u.status1.flags |=
1871 __constant_cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_1);
1872 ctio->u.status1.scsi_status |=
1873 __constant_cpu_to_le16(SS_SENSE_LEN_VALID);
1874 ctio->u.status1.sense_length =
1875 cpu_to_le16(prm->sense_buffer_len);
1876 for (i = 0; i < prm->sense_buffer_len/4; i++)
1877 ((uint32_t *)ctio->u.status1.sense_data)[i] =
1878 cpu_to_be32(((uint32_t *)prm->sense_buffer)[i]);
1879#if 0
1880 if (unlikely((prm->sense_buffer_len % 4) != 0)) {
1881 static int q;
1882 if (q < 10) {
1883 ql_dbg(ql_dbg_tgt, vha, 0xe04f,
1884 "qla_target(%d): %d bytes of sense "
1885 "lost", prm->tgt->ha->vp_idx,
1886 prm->sense_buffer_len % 4);
1887 q++;
1888 }
1889 }
1890#endif
1891 } else {
1892 ctio->u.status1.flags &=
1893 ~__constant_cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_0);
1894 ctio->u.status1.flags |=
1895 __constant_cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_1);
1896 ctio->u.status1.sense_length = 0;
1897 memset(ctio->u.status1.sense_data, 0,
1898 sizeof(ctio->u.status1.sense_data));
1899 }
1900
1901 /* Sense with len > 24, is it possible ??? */
1902}
1903
1904/*
1905 * Callback to setup response of xmit_type of QLA_TGT_XMIT_DATA and *
1906 * QLA_TGT_XMIT_STATUS for >= 24xx silicon
1907 */
1908int qlt_xmit_response(struct qla_tgt_cmd *cmd, int xmit_type,
1909 uint8_t scsi_status)
1910{
1911 struct scsi_qla_host *vha = cmd->vha;
1912 struct qla_hw_data *ha = vha->hw;
1913 struct ctio7_to_24xx *pkt;
1914 struct qla_tgt_prm prm;
1915 uint32_t full_req_cnt = 0;
1916 unsigned long flags = 0;
1917 int res;
1918
1919 memset(&prm, 0, sizeof(prm));
1920 qlt_check_srr_debug(cmd, &xmit_type);
1921
1922 ql_dbg(ql_dbg_tgt, cmd->vha, 0xe018,
1923 "is_send_status=%d, cmd->bufflen=%d, cmd->sg_cnt=%d, "
1924 "cmd->dma_data_direction=%d\n", (xmit_type & QLA_TGT_XMIT_STATUS) ?
1925 1 : 0, cmd->bufflen, cmd->sg_cnt, cmd->dma_data_direction);
1926
1927 res = qlt_pre_xmit_response(cmd, &prm, xmit_type, scsi_status,
1928 &full_req_cnt);
1929 if (unlikely(res != 0)) {
1930 if (res == QLA_TGT_PRE_XMIT_RESP_CMD_ABORTED)
1931 return 0;
1932
1933 return res;
1934 }
1935
1936 spin_lock_irqsave(&ha->hardware_lock, flags);
1937
1938 /* Does F/W have an IOCBs for this request */
1939 res = qlt_check_reserve_free_req(vha, full_req_cnt);
1940 if (unlikely(res))
1941 goto out_unmap_unlock;
1942
1943 res = qlt_24xx_build_ctio_pkt(&prm, vha);
1944 if (unlikely(res != 0))
1945 goto out_unmap_unlock;
1946
1947
1948 pkt = (struct ctio7_to_24xx *)prm.pkt;
1949
1950 if (qlt_has_data(cmd) && (xmit_type & QLA_TGT_XMIT_DATA)) {
1951 pkt->u.status0.flags |=
1952 __constant_cpu_to_le16(CTIO7_FLAGS_DATA_IN |
1953 CTIO7_FLAGS_STATUS_MODE_0);
1954
1955 qlt_load_data_segments(&prm, vha);
1956
1957 if (prm.add_status_pkt == 0) {
1958 if (xmit_type & QLA_TGT_XMIT_STATUS) {
1959 pkt->u.status0.scsi_status =
1960 cpu_to_le16(prm.rq_result);
1961 pkt->u.status0.residual =
1962 cpu_to_le32(prm.residual);
1963 pkt->u.status0.flags |= __constant_cpu_to_le16(
1964 CTIO7_FLAGS_SEND_STATUS);
1965 if (qlt_need_explicit_conf(ha, cmd, 0)) {
1966 pkt->u.status0.flags |=
1967 __constant_cpu_to_le16(
1968 CTIO7_FLAGS_EXPLICIT_CONFORM |
1969 CTIO7_FLAGS_CONFORM_REQ);
1970 }
1971 }
1972
1973 } else {
1974 /*
1975 * We have already made sure that there is sufficient
1976 * amount of request entries to not drop HW lock in
1977 * req_pkt().
1978 */
1979 struct ctio7_to_24xx *ctio =
1980 (struct ctio7_to_24xx *)qlt_get_req_pkt(vha);
1981
1982 ql_dbg(ql_dbg_tgt, vha, 0xe019,
1983 "Building additional status packet\n");
1984
1985 memcpy(ctio, pkt, sizeof(*ctio));
1986 ctio->entry_count = 1;
1987 ctio->dseg_count = 0;
1988 ctio->u.status1.flags &= ~__constant_cpu_to_le16(
1989 CTIO7_FLAGS_DATA_IN);
1990
1991 /* Real finish is ctio_m1's finish */
1992 pkt->handle |= CTIO_INTERMEDIATE_HANDLE_MARK;
1993 pkt->u.status0.flags |= __constant_cpu_to_le16(
1994 CTIO7_FLAGS_DONT_RET_CTIO);
1995 qlt_24xx_init_ctio_to_isp((struct ctio7_to_24xx *)ctio,
1996 &prm);
1997 pr_debug("Status CTIO7: %p\n", ctio);
1998 }
1999 } else
2000 qlt_24xx_init_ctio_to_isp(pkt, &prm);
2001
2002
2003 cmd->state = QLA_TGT_STATE_PROCESSED; /* Mid-level is done processing */
2004
2005 ql_dbg(ql_dbg_tgt, vha, 0xe01a,
2006 "Xmitting CTIO7 response pkt for 24xx: %p scsi_status: 0x%02x\n",
2007 pkt, scsi_status);
2008
2009 qla2x00_start_iocbs(vha, vha->req);
2010 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2011
2012 return 0;
2013
2014out_unmap_unlock:
2015 if (cmd->sg_mapped)
2016 qlt_unmap_sg(vha, cmd);
2017 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2018
2019 return res;
2020}
2021EXPORT_SYMBOL(qlt_xmit_response);
2022
2023int qlt_rdy_to_xfer(struct qla_tgt_cmd *cmd)
2024{
2025 struct ctio7_to_24xx *pkt;
2026 struct scsi_qla_host *vha = cmd->vha;
2027 struct qla_hw_data *ha = vha->hw;
2028 struct qla_tgt *tgt = cmd->tgt;
2029 struct qla_tgt_prm prm;
2030 unsigned long flags;
2031 int res = 0;
2032
2033 memset(&prm, 0, sizeof(prm));
2034 prm.cmd = cmd;
2035 prm.tgt = tgt;
2036 prm.sg = NULL;
2037 prm.req_cnt = 1;
2038
2039 /* Send marker if required */
2040 if (qlt_issue_marker(vha, 0) != QLA_SUCCESS)
2041 return -EIO;
2042
2043 ql_dbg(ql_dbg_tgt, vha, 0xe01b, "CTIO_start: vha(%d)",
2044 (int)vha->vp_idx);
2045
2046 /* Calculate number of entries and segments required */
2047 if (qlt_pci_map_calc_cnt(&prm) != 0)
2048 return -EAGAIN;
2049
2050 spin_lock_irqsave(&ha->hardware_lock, flags);
2051
2052 /* Does F/W have an IOCBs for this request */
2053 res = qlt_check_reserve_free_req(vha, prm.req_cnt);
2054 if (res != 0)
2055 goto out_unlock_free_unmap;
2056
2057 res = qlt_24xx_build_ctio_pkt(&prm, vha);
2058 if (unlikely(res != 0))
2059 goto out_unlock_free_unmap;
2060 pkt = (struct ctio7_to_24xx *)prm.pkt;
2061 pkt->u.status0.flags |= __constant_cpu_to_le16(CTIO7_FLAGS_DATA_OUT |
2062 CTIO7_FLAGS_STATUS_MODE_0);
2063 qlt_load_data_segments(&prm, vha);
2064
2065 cmd->state = QLA_TGT_STATE_NEED_DATA;
2066
2067 qla2x00_start_iocbs(vha, vha->req);
2068 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2069
2070 return res;
2071
2072out_unlock_free_unmap:
2073 if (cmd->sg_mapped)
2074 qlt_unmap_sg(vha, cmd);
2075 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2076
2077 return res;
2078}
2079EXPORT_SYMBOL(qlt_rdy_to_xfer);
2080
2081/* If hardware_lock held on entry, might drop it, then reaquire */
2082/* This function sends the appropriate CTIO to ISP 2xxx or 24xx */
2083static int __qlt_send_term_exchange(struct scsi_qla_host *vha,
2084 struct qla_tgt_cmd *cmd,
2085 struct atio_from_isp *atio)
2086{
2087 struct ctio7_to_24xx *ctio24;
2088 struct qla_hw_data *ha = vha->hw;
2089 request_t *pkt;
2090 int ret = 0;
2091
2092 ql_dbg(ql_dbg_tgt, vha, 0xe01c, "Sending TERM EXCH CTIO (ha=%p)\n", ha);
2093
2094 pkt = (request_t *)qla2x00_alloc_iocbs(vha, NULL);
2095 if (pkt == NULL) {
2096 ql_dbg(ql_dbg_tgt, vha, 0xe050,
2097 "qla_target(%d): %s failed: unable to allocate "
2098 "request packet\n", vha->vp_idx, __func__);
2099 return -ENOMEM;
2100 }
2101
2102 if (cmd != NULL) {
2103 if (cmd->state < QLA_TGT_STATE_PROCESSED) {
2104 ql_dbg(ql_dbg_tgt, vha, 0xe051,
2105 "qla_target(%d): Terminating cmd %p with "
2106 "incorrect state %d\n", vha->vp_idx, cmd,
2107 cmd->state);
2108 } else
2109 ret = 1;
2110 }
2111
2112 pkt->entry_count = 1;
2113 pkt->handle = QLA_TGT_SKIP_HANDLE | CTIO_COMPLETION_HANDLE_MARK;
2114
2115 ctio24 = (struct ctio7_to_24xx *)pkt;
2116 ctio24->entry_type = CTIO_TYPE7;
2117 ctio24->nport_handle = cmd ? cmd->loop_id : CTIO7_NHANDLE_UNRECOGNIZED;
2118 ctio24->timeout = __constant_cpu_to_le16(QLA_TGT_TIMEOUT);
2119 ctio24->vp_index = vha->vp_idx;
2120 ctio24->initiator_id[0] = atio->u.isp24.fcp_hdr.s_id[2];
2121 ctio24->initiator_id[1] = atio->u.isp24.fcp_hdr.s_id[1];
2122 ctio24->initiator_id[2] = atio->u.isp24.fcp_hdr.s_id[0];
2123 ctio24->exchange_addr = atio->u.isp24.exchange_addr;
2124 ctio24->u.status1.flags = (atio->u.isp24.attr << 9) |
2125 __constant_cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_1 |
2126 CTIO7_FLAGS_TERMINATE);
2127 ctio24->u.status1.ox_id = swab16(atio->u.isp24.fcp_hdr.ox_id);
2128
2129 /* Most likely, it isn't needed */
2130 ctio24->u.status1.residual = get_unaligned((uint32_t *)
2131 &atio->u.isp24.fcp_cmnd.add_cdb[
2132 atio->u.isp24.fcp_cmnd.add_cdb_len]);
2133 if (ctio24->u.status1.residual != 0)
2134 ctio24->u.status1.scsi_status |= SS_RESIDUAL_UNDER;
2135
2136 qla2x00_start_iocbs(vha, vha->req);
2137 return ret;
2138}
2139
2140static void qlt_send_term_exchange(struct scsi_qla_host *vha,
2141 struct qla_tgt_cmd *cmd, struct atio_from_isp *atio, int ha_locked)
2142{
2143 unsigned long flags;
2144 int rc;
2145
2146 if (qlt_issue_marker(vha, ha_locked) < 0)
2147 return;
2148
2149 if (ha_locked) {
2150 rc = __qlt_send_term_exchange(vha, cmd, atio);
2151 goto done;
2152 }
2153 spin_lock_irqsave(&vha->hw->hardware_lock, flags);
2154 rc = __qlt_send_term_exchange(vha, cmd, atio);
2155 spin_unlock_irqrestore(&vha->hw->hardware_lock, flags);
2156done:
2157 if (rc == 1) {
2158 if (!ha_locked && !in_interrupt())
2159 msleep(250); /* just in case */
2160
2161 vha->hw->tgt.tgt_ops->free_cmd(cmd);
2162 }
2163}
2164
2165void qlt_free_cmd(struct qla_tgt_cmd *cmd)
2166{
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07002167 struct qla_tgt_sess *sess = cmd->sess;
2168
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002169 BUG_ON(cmd->sg_mapped);
2170
2171 if (unlikely(cmd->free_sg))
2172 kfree(cmd->sg);
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07002173
2174 if (!sess || !sess->se_sess) {
2175 WARN_ON(1);
2176 return;
2177 }
2178 percpu_ida_free(&sess->se_sess->sess_tag_pool, cmd->se_cmd.map_tag);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002179}
2180EXPORT_SYMBOL(qlt_free_cmd);
2181
2182/* ha->hardware_lock supposed to be held on entry */
2183static int qlt_prepare_srr_ctio(struct scsi_qla_host *vha,
2184 struct qla_tgt_cmd *cmd, void *ctio)
2185{
2186 struct qla_tgt_srr_ctio *sc;
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08002187 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002188 struct qla_tgt_srr_imm *imm;
2189
2190 tgt->ctio_srr_id++;
2191
2192 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf019,
2193 "qla_target(%d): CTIO with SRR status received\n", vha->vp_idx);
2194
2195 if (!ctio) {
2196 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf055,
2197 "qla_target(%d): SRR CTIO, but ctio is NULL\n",
2198 vha->vp_idx);
2199 return -EINVAL;
2200 }
2201
2202 sc = kzalloc(sizeof(*sc), GFP_ATOMIC);
2203 if (sc != NULL) {
2204 sc->cmd = cmd;
2205 /* IRQ is already OFF */
2206 spin_lock(&tgt->srr_lock);
2207 sc->srr_id = tgt->ctio_srr_id;
2208 list_add_tail(&sc->srr_list_entry,
2209 &tgt->srr_ctio_list);
2210 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01a,
2211 "CTIO SRR %p added (id %d)\n", sc, sc->srr_id);
2212 if (tgt->imm_srr_id == tgt->ctio_srr_id) {
2213 int found = 0;
2214 list_for_each_entry(imm, &tgt->srr_imm_list,
2215 srr_list_entry) {
2216 if (imm->srr_id == sc->srr_id) {
2217 found = 1;
2218 break;
2219 }
2220 }
2221 if (found) {
2222 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01b,
2223 "Scheduling srr work\n");
2224 schedule_work(&tgt->srr_work);
2225 } else {
2226 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf056,
2227 "qla_target(%d): imm_srr_id "
2228 "== ctio_srr_id (%d), but there is no "
2229 "corresponding SRR IMM, deleting CTIO "
2230 "SRR %p\n", vha->vp_idx,
2231 tgt->ctio_srr_id, sc);
2232 list_del(&sc->srr_list_entry);
2233 spin_unlock(&tgt->srr_lock);
2234
2235 kfree(sc);
2236 return -EINVAL;
2237 }
2238 }
2239 spin_unlock(&tgt->srr_lock);
2240 } else {
2241 struct qla_tgt_srr_imm *ti;
2242
2243 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf057,
2244 "qla_target(%d): Unable to allocate SRR CTIO entry\n",
2245 vha->vp_idx);
2246 spin_lock(&tgt->srr_lock);
2247 list_for_each_entry_safe(imm, ti, &tgt->srr_imm_list,
2248 srr_list_entry) {
2249 if (imm->srr_id == tgt->ctio_srr_id) {
2250 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01c,
2251 "IMM SRR %p deleted (id %d)\n",
2252 imm, imm->srr_id);
2253 list_del(&imm->srr_list_entry);
2254 qlt_reject_free_srr_imm(vha, imm, 1);
2255 }
2256 }
2257 spin_unlock(&tgt->srr_lock);
2258
2259 return -ENOMEM;
2260 }
2261
2262 return 0;
2263}
2264
2265/*
2266 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
2267 */
2268static int qlt_term_ctio_exchange(struct scsi_qla_host *vha, void *ctio,
2269 struct qla_tgt_cmd *cmd, uint32_t status)
2270{
2271 int term = 0;
2272
2273 if (ctio != NULL) {
2274 struct ctio7_from_24xx *c = (struct ctio7_from_24xx *)ctio;
2275 term = !(c->flags &
2276 __constant_cpu_to_le16(OF_TERM_EXCH));
2277 } else
2278 term = 1;
2279
2280 if (term)
2281 qlt_send_term_exchange(vha, cmd, &cmd->atio, 1);
2282
2283 return term;
2284}
2285
2286/* ha->hardware_lock supposed to be held on entry */
2287static inline struct qla_tgt_cmd *qlt_get_cmd(struct scsi_qla_host *vha,
2288 uint32_t handle)
2289{
2290 struct qla_hw_data *ha = vha->hw;
2291
2292 handle--;
2293 if (ha->tgt.cmds[handle] != NULL) {
2294 struct qla_tgt_cmd *cmd = ha->tgt.cmds[handle];
2295 ha->tgt.cmds[handle] = NULL;
2296 return cmd;
2297 } else
2298 return NULL;
2299}
2300
2301/* ha->hardware_lock supposed to be held on entry */
2302static struct qla_tgt_cmd *qlt_ctio_to_cmd(struct scsi_qla_host *vha,
2303 uint32_t handle, void *ctio)
2304{
2305 struct qla_tgt_cmd *cmd = NULL;
2306
2307 /* Clear out internal marks */
2308 handle &= ~(CTIO_COMPLETION_HANDLE_MARK |
2309 CTIO_INTERMEDIATE_HANDLE_MARK);
2310
2311 if (handle != QLA_TGT_NULL_HANDLE) {
2312 if (unlikely(handle == QLA_TGT_SKIP_HANDLE)) {
2313 ql_dbg(ql_dbg_tgt, vha, 0xe01d, "%s",
2314 "SKIP_HANDLE CTIO\n");
2315 return NULL;
2316 }
2317 /* handle-1 is actually used */
Chad Dupuis8d93f552013-01-30 03:34:37 -05002318 if (unlikely(handle > DEFAULT_OUTSTANDING_COMMANDS)) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002319 ql_dbg(ql_dbg_tgt, vha, 0xe052,
2320 "qla_target(%d): Wrong handle %x received\n",
2321 vha->vp_idx, handle);
2322 return NULL;
2323 }
2324 cmd = qlt_get_cmd(vha, handle);
2325 if (unlikely(cmd == NULL)) {
2326 ql_dbg(ql_dbg_tgt, vha, 0xe053,
2327 "qla_target(%d): Suspicious: unable to "
2328 "find the command with handle %x\n", vha->vp_idx,
2329 handle);
2330 return NULL;
2331 }
2332 } else if (ctio != NULL) {
2333 /* We can't get loop ID from CTIO7 */
2334 ql_dbg(ql_dbg_tgt, vha, 0xe054,
2335 "qla_target(%d): Wrong CTIO received: QLA24xx doesn't "
2336 "support NULL handles\n", vha->vp_idx);
2337 return NULL;
2338 }
2339
2340 return cmd;
2341}
2342
2343/*
2344 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
2345 */
2346static void qlt_do_ctio_completion(struct scsi_qla_host *vha, uint32_t handle,
2347 uint32_t status, void *ctio)
2348{
2349 struct qla_hw_data *ha = vha->hw;
2350 struct se_cmd *se_cmd;
2351 struct target_core_fabric_ops *tfo;
2352 struct qla_tgt_cmd *cmd;
2353
2354 ql_dbg(ql_dbg_tgt, vha, 0xe01e,
2355 "qla_target(%d): handle(ctio %p status %#x) <- %08x\n",
2356 vha->vp_idx, ctio, status, handle);
2357
2358 if (handle & CTIO_INTERMEDIATE_HANDLE_MARK) {
2359 /* That could happen only in case of an error/reset/abort */
2360 if (status != CTIO_SUCCESS) {
2361 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01d,
2362 "Intermediate CTIO received"
2363 " (status %x)\n", status);
2364 }
2365 return;
2366 }
2367
2368 cmd = qlt_ctio_to_cmd(vha, handle, ctio);
Roland Dreier092e1dc2012-06-11 18:23:15 -07002369 if (cmd == NULL)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002370 return;
Roland Dreier092e1dc2012-06-11 18:23:15 -07002371
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002372 se_cmd = &cmd->se_cmd;
2373 tfo = se_cmd->se_tfo;
2374
2375 if (cmd->sg_mapped)
2376 qlt_unmap_sg(vha, cmd);
2377
2378 if (unlikely(status != CTIO_SUCCESS)) {
2379 switch (status & 0xFFFF) {
2380 case CTIO_LIP_RESET:
2381 case CTIO_TARGET_RESET:
2382 case CTIO_ABORTED:
2383 case CTIO_TIMEOUT:
2384 case CTIO_INVALID_RX_ID:
2385 /* They are OK */
2386 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf058,
2387 "qla_target(%d): CTIO with "
2388 "status %#x received, state %x, se_cmd %p, "
2389 "(LIP_RESET=e, ABORTED=2, TARGET_RESET=17, "
2390 "TIMEOUT=b, INVALID_RX_ID=8)\n", vha->vp_idx,
2391 status, cmd->state, se_cmd);
2392 break;
2393
2394 case CTIO_PORT_LOGGED_OUT:
2395 case CTIO_PORT_UNAVAILABLE:
2396 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf059,
2397 "qla_target(%d): CTIO with PORT LOGGED "
2398 "OUT (29) or PORT UNAVAILABLE (28) status %x "
2399 "received (state %x, se_cmd %p)\n", vha->vp_idx,
2400 status, cmd->state, se_cmd);
2401 break;
2402
2403 case CTIO_SRR_RECEIVED:
2404 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05a,
2405 "qla_target(%d): CTIO with SRR_RECEIVED"
2406 " status %x received (state %x, se_cmd %p)\n",
2407 vha->vp_idx, status, cmd->state, se_cmd);
2408 if (qlt_prepare_srr_ctio(vha, cmd, ctio) != 0)
2409 break;
2410 else
2411 return;
2412
2413 default:
2414 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05b,
2415 "qla_target(%d): CTIO with error status "
2416 "0x%x received (state %x, se_cmd %p\n",
2417 vha->vp_idx, status, cmd->state, se_cmd);
2418 break;
2419 }
2420
2421 if (cmd->state != QLA_TGT_STATE_NEED_DATA)
2422 if (qlt_term_ctio_exchange(vha, ctio, cmd, status))
2423 return;
2424 }
2425
2426 if (cmd->state == QLA_TGT_STATE_PROCESSED) {
2427 ql_dbg(ql_dbg_tgt, vha, 0xe01f, "Command %p finished\n", cmd);
2428 } else if (cmd->state == QLA_TGT_STATE_NEED_DATA) {
2429 int rx_status = 0;
2430
2431 cmd->state = QLA_TGT_STATE_DATA_IN;
2432
2433 if (unlikely(status != CTIO_SUCCESS))
2434 rx_status = -EIO;
2435 else
2436 cmd->write_data_transferred = 1;
2437
2438 ql_dbg(ql_dbg_tgt, vha, 0xe020,
2439 "Data received, context %x, rx_status %d\n",
2440 0x0, rx_status);
2441
2442 ha->tgt.tgt_ops->handle_data(cmd);
2443 return;
2444 } else if (cmd->state == QLA_TGT_STATE_ABORTED) {
2445 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01e,
2446 "Aborted command %p (tag %d) finished\n", cmd, cmd->tag);
2447 } else {
2448 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05c,
2449 "qla_target(%d): A command in state (%d) should "
2450 "not return a CTIO complete\n", vha->vp_idx, cmd->state);
2451 }
2452
2453 if (unlikely(status != CTIO_SUCCESS)) {
2454 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01f, "Finishing failed CTIO\n");
2455 dump_stack();
2456 }
2457
2458 ha->tgt.tgt_ops->free_cmd(cmd);
2459}
2460
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002461static inline int qlt_get_fcp_task_attr(struct scsi_qla_host *vha,
2462 uint8_t task_codes)
2463{
2464 int fcp_task_attr;
2465
2466 switch (task_codes) {
2467 case ATIO_SIMPLE_QUEUE:
2468 fcp_task_attr = MSG_SIMPLE_TAG;
2469 break;
2470 case ATIO_HEAD_OF_QUEUE:
2471 fcp_task_attr = MSG_HEAD_TAG;
2472 break;
2473 case ATIO_ORDERED_QUEUE:
2474 fcp_task_attr = MSG_ORDERED_TAG;
2475 break;
2476 case ATIO_ACA_QUEUE:
2477 fcp_task_attr = MSG_ACA_TAG;
2478 break;
2479 case ATIO_UNTAGGED:
2480 fcp_task_attr = MSG_SIMPLE_TAG;
2481 break;
2482 default:
2483 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05d,
2484 "qla_target: unknown task code %x, use ORDERED instead\n",
2485 task_codes);
2486 fcp_task_attr = MSG_ORDERED_TAG;
2487 break;
2488 }
2489
2490 return fcp_task_attr;
2491}
2492
2493static struct qla_tgt_sess *qlt_make_local_sess(struct scsi_qla_host *,
2494 uint8_t *);
2495/*
2496 * Process context for I/O path into tcm_qla2xxx code
2497 */
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07002498static void __qlt_do_work(struct qla_tgt_cmd *cmd)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002499{
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002500 scsi_qla_host_t *vha = cmd->vha;
2501 struct qla_hw_data *ha = vha->hw;
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08002502 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07002503 struct qla_tgt_sess *sess = cmd->sess;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002504 struct atio_from_isp *atio = &cmd->atio;
2505 unsigned char *cdb;
2506 unsigned long flags;
2507 uint32_t data_length;
2508 int ret, fcp_task_attr, data_dir, bidi = 0;
2509
2510 if (tgt->tgt_stop)
2511 goto out_term;
2512
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002513 cdb = &atio->u.isp24.fcp_cmnd.cdb[0];
2514 cmd->tag = atio->u.isp24.exchange_addr;
2515 cmd->unpacked_lun = scsilun_to_int(
2516 (struct scsi_lun *)&atio->u.isp24.fcp_cmnd.lun);
2517
2518 if (atio->u.isp24.fcp_cmnd.rddata &&
2519 atio->u.isp24.fcp_cmnd.wrdata) {
2520 bidi = 1;
2521 data_dir = DMA_TO_DEVICE;
2522 } else if (atio->u.isp24.fcp_cmnd.rddata)
2523 data_dir = DMA_FROM_DEVICE;
2524 else if (atio->u.isp24.fcp_cmnd.wrdata)
2525 data_dir = DMA_TO_DEVICE;
2526 else
2527 data_dir = DMA_NONE;
2528
2529 fcp_task_attr = qlt_get_fcp_task_attr(vha,
2530 atio->u.isp24.fcp_cmnd.task_attr);
2531 data_length = be32_to_cpu(get_unaligned((uint32_t *)
2532 &atio->u.isp24.fcp_cmnd.add_cdb[
2533 atio->u.isp24.fcp_cmnd.add_cdb_len]));
2534
2535 ql_dbg(ql_dbg_tgt, vha, 0xe022,
2536 "qla_target: START qla command: %p lun: 0x%04x (tag %d)\n",
2537 cmd, cmd->unpacked_lun, cmd->tag);
2538
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07002539 ret = ha->tgt.tgt_ops->handle_cmd(vha, cmd, cdb, data_length,
2540 fcp_task_attr, data_dir, bidi);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002541 if (ret != 0)
2542 goto out_term;
2543 /*
2544 * Drop extra session reference from qla_tgt_handle_cmd_for_atio*(
2545 */
Jörn Engel08234e32013-06-12 16:27:54 -04002546 spin_lock_irqsave(&ha->hardware_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002547 ha->tgt.tgt_ops->put_sess(sess);
Jörn Engel08234e32013-06-12 16:27:54 -04002548 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002549 return;
2550
2551out_term:
2552 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf020, "Terminating work cmd %p", cmd);
2553 /*
Roland Dreierfae9eaf2012-06-11 18:23:16 -07002554 * cmd has not sent to target yet, so pass NULL as the second
2555 * argument to qlt_send_term_exchange() and free the memory here.
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002556 */
2557 spin_lock_irqsave(&ha->hardware_lock, flags);
2558 qlt_send_term_exchange(vha, NULL, &cmd->atio, 1);
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07002559 percpu_ida_free(&sess->se_sess->sess_tag_pool, cmd->se_cmd.map_tag);
2560 ha->tgt.tgt_ops->put_sess(sess);
Jörn Engel08234e32013-06-12 16:27:54 -04002561 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002562}
2563
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07002564static void qlt_do_work(struct work_struct *work)
2565{
2566 struct qla_tgt_cmd *cmd = container_of(work, struct qla_tgt_cmd, work);
2567
2568 __qlt_do_work(cmd);
2569}
2570
2571static struct qla_tgt_cmd *qlt_get_tag(scsi_qla_host_t *vha,
2572 struct qla_tgt_sess *sess,
2573 struct atio_from_isp *atio)
2574{
2575 struct se_session *se_sess = sess->se_sess;
2576 struct qla_tgt_cmd *cmd;
2577 int tag;
2578
2579 tag = percpu_ida_alloc(&se_sess->sess_tag_pool, TASK_RUNNING);
2580 if (tag < 0)
2581 return NULL;
2582
2583 cmd = &((struct qla_tgt_cmd *)se_sess->sess_cmd_map)[tag];
2584 memset(cmd, 0, sizeof(struct qla_tgt_cmd));
2585
2586 memcpy(&cmd->atio, atio, sizeof(*atio));
2587 cmd->state = QLA_TGT_STATE_NEW;
2588 cmd->tgt = vha->vha_tgt.qla_tgt;
2589 cmd->vha = vha;
2590 cmd->se_cmd.map_tag = tag;
2591 cmd->sess = sess;
2592 cmd->loop_id = sess->loop_id;
2593 cmd->conf_compl_supported = sess->conf_compl_supported;
2594
2595 return cmd;
2596}
2597
2598static void qlt_send_busy(struct scsi_qla_host *, struct atio_from_isp *,
2599 uint16_t);
2600
2601static void qlt_create_sess_from_atio(struct work_struct *work)
2602{
2603 struct qla_tgt_sess_op *op = container_of(work,
2604 struct qla_tgt_sess_op, work);
2605 scsi_qla_host_t *vha = op->vha;
2606 struct qla_hw_data *ha = vha->hw;
2607 struct qla_tgt_sess *sess;
2608 struct qla_tgt_cmd *cmd;
2609 unsigned long flags;
2610 uint8_t *s_id = op->atio.u.isp24.fcp_hdr.s_id;
2611
2612 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf022,
2613 "qla_target(%d): Unable to find wwn login"
2614 " (s_id %x:%x:%x), trying to create it manually\n",
2615 vha->vp_idx, s_id[0], s_id[1], s_id[2]);
2616
2617 if (op->atio.u.raw.entry_count > 1) {
2618 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf023,
2619 "Dropping multy entry atio %p\n", &op->atio);
2620 goto out_term;
2621 }
2622
2623 mutex_lock(&vha->vha_tgt.tgt_mutex);
2624 sess = qlt_make_local_sess(vha, s_id);
2625 /* sess has an extra creation ref. */
2626 mutex_unlock(&vha->vha_tgt.tgt_mutex);
2627
2628 if (!sess)
2629 goto out_term;
2630 /*
2631 * Now obtain a pre-allocated session tag using the original op->atio
2632 * packet header, and dispatch into __qlt_do_work() using the existing
2633 * process context.
2634 */
2635 cmd = qlt_get_tag(vha, sess, &op->atio);
2636 if (!cmd) {
2637 spin_lock_irqsave(&ha->hardware_lock, flags);
2638 qlt_send_busy(vha, &op->atio, SAM_STAT_BUSY);
2639 ha->tgt.tgt_ops->put_sess(sess);
2640 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2641 kfree(op);
2642 return;
2643 }
2644 /*
2645 * __qlt_do_work() will call ha->tgt.tgt_ops->put_sess() to release
2646 * the extra reference taken above by qlt_make_local_sess()
2647 */
2648 __qlt_do_work(cmd);
2649 kfree(op);
2650 return;
2651
2652out_term:
2653 spin_lock_irqsave(&ha->hardware_lock, flags);
2654 qlt_send_term_exchange(vha, NULL, &op->atio, 1);
2655 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2656 kfree(op);
2657
2658}
2659
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002660/* ha->hardware_lock supposed to be held on entry */
2661static int qlt_handle_cmd_for_atio(struct scsi_qla_host *vha,
2662 struct atio_from_isp *atio)
2663{
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07002664 struct qla_hw_data *ha = vha->hw;
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08002665 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07002666 struct qla_tgt_sess *sess;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002667 struct qla_tgt_cmd *cmd;
2668
2669 if (unlikely(tgt->tgt_stop)) {
2670 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf021,
2671 "New command while device %p is shutting down\n", tgt);
2672 return -EFAULT;
2673 }
2674
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07002675 sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha, atio->u.isp24.fcp_hdr.s_id);
2676 if (unlikely(!sess)) {
2677 struct qla_tgt_sess_op *op = kzalloc(sizeof(struct qla_tgt_sess_op),
2678 GFP_ATOMIC);
2679 if (!op)
2680 return -ENOMEM;
2681
2682 memcpy(&op->atio, atio, sizeof(*atio));
2683 INIT_WORK(&op->work, qlt_create_sess_from_atio);
2684 queue_work(qla_tgt_wq, &op->work);
2685 return 0;
2686 }
2687 /*
2688 * Do kref_get() before returning + dropping qla_hw_data->hardware_lock.
2689 */
2690 kref_get(&sess->se_sess->sess_kref);
2691
2692 cmd = qlt_get_tag(vha, sess, atio);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002693 if (!cmd) {
2694 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05e,
2695 "qla_target(%d): Allocation of cmd failed\n", vha->vp_idx);
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07002696 ha->tgt.tgt_ops->put_sess(sess);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002697 return -ENOMEM;
2698 }
2699
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002700 INIT_WORK(&cmd->work, qlt_do_work);
2701 queue_work(qla_tgt_wq, &cmd->work);
2702 return 0;
2703
2704}
2705
2706/* ha->hardware_lock supposed to be held on entry */
2707static int qlt_issue_task_mgmt(struct qla_tgt_sess *sess, uint32_t lun,
2708 int fn, void *iocb, int flags)
2709{
2710 struct scsi_qla_host *vha = sess->vha;
2711 struct qla_hw_data *ha = vha->hw;
2712 struct qla_tgt_mgmt_cmd *mcmd;
2713 int res;
2714 uint8_t tmr_func;
2715
2716 mcmd = mempool_alloc(qla_tgt_mgmt_cmd_mempool, GFP_ATOMIC);
2717 if (!mcmd) {
2718 ql_dbg(ql_dbg_tgt_tmr, vha, 0x10009,
2719 "qla_target(%d): Allocation of management "
2720 "command failed, some commands and their data could "
2721 "leak\n", vha->vp_idx);
2722 return -ENOMEM;
2723 }
2724 memset(mcmd, 0, sizeof(*mcmd));
2725 mcmd->sess = sess;
2726
2727 if (iocb) {
2728 memcpy(&mcmd->orig_iocb.imm_ntfy, iocb,
2729 sizeof(mcmd->orig_iocb.imm_ntfy));
2730 }
2731 mcmd->tmr_func = fn;
2732 mcmd->flags = flags;
2733
2734 switch (fn) {
2735 case QLA_TGT_CLEAR_ACA:
2736 ql_dbg(ql_dbg_tgt_tmr, vha, 0x10000,
2737 "qla_target(%d): CLEAR_ACA received\n", sess->vha->vp_idx);
2738 tmr_func = TMR_CLEAR_ACA;
2739 break;
2740
2741 case QLA_TGT_TARGET_RESET:
2742 ql_dbg(ql_dbg_tgt_tmr, vha, 0x10001,
2743 "qla_target(%d): TARGET_RESET received\n",
2744 sess->vha->vp_idx);
2745 tmr_func = TMR_TARGET_WARM_RESET;
2746 break;
2747
2748 case QLA_TGT_LUN_RESET:
2749 ql_dbg(ql_dbg_tgt_tmr, vha, 0x10002,
2750 "qla_target(%d): LUN_RESET received\n", sess->vha->vp_idx);
2751 tmr_func = TMR_LUN_RESET;
2752 break;
2753
2754 case QLA_TGT_CLEAR_TS:
2755 ql_dbg(ql_dbg_tgt_tmr, vha, 0x10003,
2756 "qla_target(%d): CLEAR_TS received\n", sess->vha->vp_idx);
2757 tmr_func = TMR_CLEAR_TASK_SET;
2758 break;
2759
2760 case QLA_TGT_ABORT_TS:
2761 ql_dbg(ql_dbg_tgt_tmr, vha, 0x10004,
2762 "qla_target(%d): ABORT_TS received\n", sess->vha->vp_idx);
2763 tmr_func = TMR_ABORT_TASK_SET;
2764 break;
2765#if 0
2766 case QLA_TGT_ABORT_ALL:
2767 ql_dbg(ql_dbg_tgt_tmr, vha, 0x10005,
2768 "qla_target(%d): Doing ABORT_ALL_TASKS\n",
2769 sess->vha->vp_idx);
2770 tmr_func = 0;
2771 break;
2772
2773 case QLA_TGT_ABORT_ALL_SESS:
2774 ql_dbg(ql_dbg_tgt_tmr, vha, 0x10006,
2775 "qla_target(%d): Doing ABORT_ALL_TASKS_SESS\n",
2776 sess->vha->vp_idx);
2777 tmr_func = 0;
2778 break;
2779
2780 case QLA_TGT_NEXUS_LOSS_SESS:
2781 ql_dbg(ql_dbg_tgt_tmr, vha, 0x10007,
2782 "qla_target(%d): Doing NEXUS_LOSS_SESS\n",
2783 sess->vha->vp_idx);
2784 tmr_func = 0;
2785 break;
2786
2787 case QLA_TGT_NEXUS_LOSS:
2788 ql_dbg(ql_dbg_tgt_tmr, vha, 0x10008,
2789 "qla_target(%d): Doing NEXUS_LOSS\n", sess->vha->vp_idx);
2790 tmr_func = 0;
2791 break;
2792#endif
2793 default:
2794 ql_dbg(ql_dbg_tgt_tmr, vha, 0x1000a,
2795 "qla_target(%d): Unknown task mgmt fn 0x%x\n",
2796 sess->vha->vp_idx, fn);
2797 mempool_free(mcmd, qla_tgt_mgmt_cmd_mempool);
2798 return -ENOSYS;
2799 }
2800
2801 res = ha->tgt.tgt_ops->handle_tmr(mcmd, lun, tmr_func, 0);
2802 if (res != 0) {
2803 ql_dbg(ql_dbg_tgt_tmr, vha, 0x1000b,
2804 "qla_target(%d): tgt.tgt_ops->handle_tmr() failed: %d\n",
2805 sess->vha->vp_idx, res);
2806 mempool_free(mcmd, qla_tgt_mgmt_cmd_mempool);
2807 return -EFAULT;
2808 }
2809
2810 return 0;
2811}
2812
2813/* ha->hardware_lock supposed to be held on entry */
2814static int qlt_handle_task_mgmt(struct scsi_qla_host *vha, void *iocb)
2815{
2816 struct atio_from_isp *a = (struct atio_from_isp *)iocb;
2817 struct qla_hw_data *ha = vha->hw;
2818 struct qla_tgt *tgt;
2819 struct qla_tgt_sess *sess;
2820 uint32_t lun, unpacked_lun;
2821 int lun_size, fn;
2822
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08002823 tgt = vha->vha_tgt.qla_tgt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002824
2825 lun = a->u.isp24.fcp_cmnd.lun;
2826 lun_size = sizeof(a->u.isp24.fcp_cmnd.lun);
2827 fn = a->u.isp24.fcp_cmnd.task_mgmt_flags;
2828 sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha,
2829 a->u.isp24.fcp_hdr.s_id);
2830 unpacked_lun = scsilun_to_int((struct scsi_lun *)&lun);
2831
2832 if (!sess) {
2833 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf024,
2834 "qla_target(%d): task mgmt fn 0x%x for "
2835 "non-existant session\n", vha->vp_idx, fn);
2836 return qlt_sched_sess_work(tgt, QLA_TGT_SESS_WORK_TM, iocb,
2837 sizeof(struct atio_from_isp));
2838 }
2839
2840 return qlt_issue_task_mgmt(sess, unpacked_lun, fn, iocb, 0);
2841}
2842
2843/* ha->hardware_lock supposed to be held on entry */
2844static int __qlt_abort_task(struct scsi_qla_host *vha,
2845 struct imm_ntfy_from_isp *iocb, struct qla_tgt_sess *sess)
2846{
2847 struct atio_from_isp *a = (struct atio_from_isp *)iocb;
2848 struct qla_hw_data *ha = vha->hw;
2849 struct qla_tgt_mgmt_cmd *mcmd;
2850 uint32_t lun, unpacked_lun;
2851 int rc;
2852
2853 mcmd = mempool_alloc(qla_tgt_mgmt_cmd_mempool, GFP_ATOMIC);
2854 if (mcmd == NULL) {
2855 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05f,
2856 "qla_target(%d): %s: Allocation of ABORT cmd failed\n",
2857 vha->vp_idx, __func__);
2858 return -ENOMEM;
2859 }
2860 memset(mcmd, 0, sizeof(*mcmd));
2861
2862 mcmd->sess = sess;
2863 memcpy(&mcmd->orig_iocb.imm_ntfy, iocb,
2864 sizeof(mcmd->orig_iocb.imm_ntfy));
2865
2866 lun = a->u.isp24.fcp_cmnd.lun;
2867 unpacked_lun = scsilun_to_int((struct scsi_lun *)&lun);
2868
2869 rc = ha->tgt.tgt_ops->handle_tmr(mcmd, unpacked_lun, TMR_ABORT_TASK,
2870 le16_to_cpu(iocb->u.isp2x.seq_id));
2871 if (rc != 0) {
2872 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf060,
2873 "qla_target(%d): tgt_ops->handle_tmr() failed: %d\n",
2874 vha->vp_idx, rc);
2875 mempool_free(mcmd, qla_tgt_mgmt_cmd_mempool);
2876 return -EFAULT;
2877 }
2878
2879 return 0;
2880}
2881
2882/* ha->hardware_lock supposed to be held on entry */
2883static int qlt_abort_task(struct scsi_qla_host *vha,
2884 struct imm_ntfy_from_isp *iocb)
2885{
2886 struct qla_hw_data *ha = vha->hw;
2887 struct qla_tgt_sess *sess;
2888 int loop_id;
2889
2890 loop_id = GET_TARGET_ID(ha, (struct atio_from_isp *)iocb);
2891
2892 sess = ha->tgt.tgt_ops->find_sess_by_loop_id(vha, loop_id);
2893 if (sess == NULL) {
2894 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf025,
2895 "qla_target(%d): task abort for unexisting "
2896 "session\n", vha->vp_idx);
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08002897 return qlt_sched_sess_work(vha->vha_tgt.qla_tgt,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002898 QLA_TGT_SESS_WORK_ABORT, iocb, sizeof(*iocb));
2899 }
2900
2901 return __qlt_abort_task(vha, iocb, sess);
2902}
2903
2904/*
2905 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
2906 */
2907static int qlt_24xx_handle_els(struct scsi_qla_host *vha,
2908 struct imm_ntfy_from_isp *iocb)
2909{
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002910 int res = 0;
2911
2912 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf026,
Oleksandr Khoshaba7b833552013-08-27 01:37:27 -04002913 "qla_target(%d): Port ID: 0x%3phC ELS opcode: 0x%02x\n",
2914 vha->vp_idx, iocb->u.isp24.port_id, iocb->u.isp24.status_subcode);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002915
2916 switch (iocb->u.isp24.status_subcode) {
2917 case ELS_PLOGI:
2918 case ELS_FLOGI:
2919 case ELS_PRLI:
2920 case ELS_LOGO:
2921 case ELS_PRLO:
2922 res = qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS_SESS);
2923 break;
2924 case ELS_PDISC:
2925 case ELS_ADISC:
2926 {
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08002927 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002928 if (tgt->link_reinit_iocb_pending) {
2929 qlt_send_notify_ack(vha, &tgt->link_reinit_iocb,
2930 0, 0, 0, 0, 0, 0);
2931 tgt->link_reinit_iocb_pending = 0;
2932 }
2933 res = 1; /* send notify ack */
2934 break;
2935 }
2936
2937 default:
2938 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf061,
2939 "qla_target(%d): Unsupported ELS command %x "
2940 "received\n", vha->vp_idx, iocb->u.isp24.status_subcode);
2941 res = qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS_SESS);
2942 break;
2943 }
2944
2945 return res;
2946}
2947
2948static int qlt_set_data_offset(struct qla_tgt_cmd *cmd, uint32_t offset)
2949{
2950 struct scatterlist *sg, *sgp, *sg_srr, *sg_srr_start = NULL;
2951 size_t first_offset = 0, rem_offset = offset, tmp = 0;
2952 int i, sg_srr_cnt, bufflen = 0;
2953
2954 ql_dbg(ql_dbg_tgt, cmd->vha, 0xe023,
2955 "Entering qla_tgt_set_data_offset: cmd: %p, cmd->sg: %p, "
2956 "cmd->sg_cnt: %u, direction: %d\n",
2957 cmd, cmd->sg, cmd->sg_cnt, cmd->dma_data_direction);
2958
2959 /*
2960 * FIXME: Reject non zero SRR relative offset until we can test
2961 * this code properly.
2962 */
2963 pr_debug("Rejecting non zero SRR rel_offs: %u\n", offset);
2964 return -1;
2965
2966 if (!cmd->sg || !cmd->sg_cnt) {
2967 ql_dbg(ql_dbg_tgt, cmd->vha, 0xe055,
2968 "Missing cmd->sg or zero cmd->sg_cnt in"
2969 " qla_tgt_set_data_offset\n");
2970 return -EINVAL;
2971 }
2972 /*
2973 * Walk the current cmd->sg list until we locate the new sg_srr_start
2974 */
2975 for_each_sg(cmd->sg, sg, cmd->sg_cnt, i) {
2976 ql_dbg(ql_dbg_tgt, cmd->vha, 0xe024,
2977 "sg[%d]: %p page: %p, length: %d, offset: %d\n",
2978 i, sg, sg_page(sg), sg->length, sg->offset);
2979
2980 if ((sg->length + tmp) > offset) {
2981 first_offset = rem_offset;
2982 sg_srr_start = sg;
2983 ql_dbg(ql_dbg_tgt, cmd->vha, 0xe025,
2984 "Found matching sg[%d], using %p as sg_srr_start, "
2985 "and using first_offset: %zu\n", i, sg,
2986 first_offset);
2987 break;
2988 }
2989 tmp += sg->length;
2990 rem_offset -= sg->length;
2991 }
2992
2993 if (!sg_srr_start) {
2994 ql_dbg(ql_dbg_tgt, cmd->vha, 0xe056,
2995 "Unable to locate sg_srr_start for offset: %u\n", offset);
2996 return -EINVAL;
2997 }
2998 sg_srr_cnt = (cmd->sg_cnt - i);
2999
3000 sg_srr = kzalloc(sizeof(struct scatterlist) * sg_srr_cnt, GFP_KERNEL);
3001 if (!sg_srr) {
3002 ql_dbg(ql_dbg_tgt, cmd->vha, 0xe057,
3003 "Unable to allocate sgp\n");
3004 return -ENOMEM;
3005 }
3006 sg_init_table(sg_srr, sg_srr_cnt);
3007 sgp = &sg_srr[0];
3008 /*
3009 * Walk the remaining list for sg_srr_start, mapping to the newly
3010 * allocated sg_srr taking first_offset into account.
3011 */
3012 for_each_sg(sg_srr_start, sg, sg_srr_cnt, i) {
3013 if (first_offset) {
3014 sg_set_page(sgp, sg_page(sg),
3015 (sg->length - first_offset), first_offset);
3016 first_offset = 0;
3017 } else {
3018 sg_set_page(sgp, sg_page(sg), sg->length, 0);
3019 }
3020 bufflen += sgp->length;
3021
3022 sgp = sg_next(sgp);
3023 if (!sgp)
3024 break;
3025 }
3026
3027 cmd->sg = sg_srr;
3028 cmd->sg_cnt = sg_srr_cnt;
3029 cmd->bufflen = bufflen;
3030 cmd->offset += offset;
3031 cmd->free_sg = 1;
3032
3033 ql_dbg(ql_dbg_tgt, cmd->vha, 0xe026, "New cmd->sg: %p\n", cmd->sg);
3034 ql_dbg(ql_dbg_tgt, cmd->vha, 0xe027, "New cmd->sg_cnt: %u\n",
3035 cmd->sg_cnt);
3036 ql_dbg(ql_dbg_tgt, cmd->vha, 0xe028, "New cmd->bufflen: %u\n",
3037 cmd->bufflen);
3038 ql_dbg(ql_dbg_tgt, cmd->vha, 0xe029, "New cmd->offset: %u\n",
3039 cmd->offset);
3040
3041 if (cmd->sg_cnt < 0)
3042 BUG();
3043
3044 if (cmd->bufflen < 0)
3045 BUG();
3046
3047 return 0;
3048}
3049
3050static inline int qlt_srr_adjust_data(struct qla_tgt_cmd *cmd,
3051 uint32_t srr_rel_offs, int *xmit_type)
3052{
3053 int res = 0, rel_offs;
3054
3055 rel_offs = srr_rel_offs - cmd->offset;
3056 ql_dbg(ql_dbg_tgt_mgt, cmd->vha, 0xf027, "srr_rel_offs=%d, rel_offs=%d",
3057 srr_rel_offs, rel_offs);
3058
3059 *xmit_type = QLA_TGT_XMIT_ALL;
3060
3061 if (rel_offs < 0) {
3062 ql_dbg(ql_dbg_tgt_mgt, cmd->vha, 0xf062,
3063 "qla_target(%d): SRR rel_offs (%d) < 0",
3064 cmd->vha->vp_idx, rel_offs);
3065 res = -1;
3066 } else if (rel_offs == cmd->bufflen)
3067 *xmit_type = QLA_TGT_XMIT_STATUS;
3068 else if (rel_offs > 0)
3069 res = qlt_set_data_offset(cmd, rel_offs);
3070
3071 return res;
3072}
3073
3074/* No locks, thread context */
3075static void qlt_handle_srr(struct scsi_qla_host *vha,
3076 struct qla_tgt_srr_ctio *sctio, struct qla_tgt_srr_imm *imm)
3077{
3078 struct imm_ntfy_from_isp *ntfy =
3079 (struct imm_ntfy_from_isp *)&imm->imm_ntfy;
3080 struct qla_hw_data *ha = vha->hw;
3081 struct qla_tgt_cmd *cmd = sctio->cmd;
3082 struct se_cmd *se_cmd = &cmd->se_cmd;
3083 unsigned long flags;
3084 int xmit_type = 0, resp = 0;
3085 uint32_t offset;
3086 uint16_t srr_ui;
3087
3088 offset = le32_to_cpu(ntfy->u.isp24.srr_rel_offs);
3089 srr_ui = ntfy->u.isp24.srr_ui;
3090
3091 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf028, "SRR cmd %p, srr_ui %x\n",
3092 cmd, srr_ui);
3093
3094 switch (srr_ui) {
3095 case SRR_IU_STATUS:
3096 spin_lock_irqsave(&ha->hardware_lock, flags);
3097 qlt_send_notify_ack(vha, ntfy,
3098 0, 0, 0, NOTIFY_ACK_SRR_FLAGS_ACCEPT, 0, 0);
3099 spin_unlock_irqrestore(&ha->hardware_lock, flags);
3100 xmit_type = QLA_TGT_XMIT_STATUS;
3101 resp = 1;
3102 break;
3103 case SRR_IU_DATA_IN:
3104 if (!cmd->sg || !cmd->sg_cnt) {
3105 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf063,
3106 "Unable to process SRR_IU_DATA_IN due to"
3107 " missing cmd->sg, state: %d\n", cmd->state);
3108 dump_stack();
3109 goto out_reject;
3110 }
3111 if (se_cmd->scsi_status != 0) {
3112 ql_dbg(ql_dbg_tgt, vha, 0xe02a,
3113 "Rejecting SRR_IU_DATA_IN with non GOOD "
3114 "scsi_status\n");
3115 goto out_reject;
3116 }
3117 cmd->bufflen = se_cmd->data_length;
3118
3119 if (qlt_has_data(cmd)) {
3120 if (qlt_srr_adjust_data(cmd, offset, &xmit_type) != 0)
3121 goto out_reject;
3122 spin_lock_irqsave(&ha->hardware_lock, flags);
3123 qlt_send_notify_ack(vha, ntfy,
3124 0, 0, 0, NOTIFY_ACK_SRR_FLAGS_ACCEPT, 0, 0);
3125 spin_unlock_irqrestore(&ha->hardware_lock, flags);
3126 resp = 1;
3127 } else {
3128 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf064,
3129 "qla_target(%d): SRR for in data for cmd "
3130 "without them (tag %d, SCSI status %d), "
3131 "reject", vha->vp_idx, cmd->tag,
3132 cmd->se_cmd.scsi_status);
3133 goto out_reject;
3134 }
3135 break;
3136 case SRR_IU_DATA_OUT:
3137 if (!cmd->sg || !cmd->sg_cnt) {
3138 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf065,
3139 "Unable to process SRR_IU_DATA_OUT due to"
3140 " missing cmd->sg\n");
3141 dump_stack();
3142 goto out_reject;
3143 }
3144 if (se_cmd->scsi_status != 0) {
3145 ql_dbg(ql_dbg_tgt, vha, 0xe02b,
3146 "Rejecting SRR_IU_DATA_OUT"
3147 " with non GOOD scsi_status\n");
3148 goto out_reject;
3149 }
3150 cmd->bufflen = se_cmd->data_length;
3151
3152 if (qlt_has_data(cmd)) {
3153 if (qlt_srr_adjust_data(cmd, offset, &xmit_type) != 0)
3154 goto out_reject;
3155 spin_lock_irqsave(&ha->hardware_lock, flags);
3156 qlt_send_notify_ack(vha, ntfy,
3157 0, 0, 0, NOTIFY_ACK_SRR_FLAGS_ACCEPT, 0, 0);
3158 spin_unlock_irqrestore(&ha->hardware_lock, flags);
3159 if (xmit_type & QLA_TGT_XMIT_DATA)
3160 qlt_rdy_to_xfer(cmd);
3161 } else {
3162 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf066,
3163 "qla_target(%d): SRR for out data for cmd "
3164 "without them (tag %d, SCSI status %d), "
3165 "reject", vha->vp_idx, cmd->tag,
3166 cmd->se_cmd.scsi_status);
3167 goto out_reject;
3168 }
3169 break;
3170 default:
3171 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf067,
3172 "qla_target(%d): Unknown srr_ui value %x",
3173 vha->vp_idx, srr_ui);
3174 goto out_reject;
3175 }
3176
3177 /* Transmit response in case of status and data-in cases */
3178 if (resp)
3179 qlt_xmit_response(cmd, xmit_type, se_cmd->scsi_status);
3180
3181 return;
3182
3183out_reject:
3184 spin_lock_irqsave(&ha->hardware_lock, flags);
3185 qlt_send_notify_ack(vha, ntfy, 0, 0, 0,
3186 NOTIFY_ACK_SRR_FLAGS_REJECT,
3187 NOTIFY_ACK_SRR_REJECT_REASON_UNABLE_TO_PERFORM,
3188 NOTIFY_ACK_SRR_FLAGS_REJECT_EXPL_NO_EXPL);
3189 if (cmd->state == QLA_TGT_STATE_NEED_DATA) {
3190 cmd->state = QLA_TGT_STATE_DATA_IN;
3191 dump_stack();
3192 } else
3193 qlt_send_term_exchange(vha, cmd, &cmd->atio, 1);
3194 spin_unlock_irqrestore(&ha->hardware_lock, flags);
3195}
3196
3197static void qlt_reject_free_srr_imm(struct scsi_qla_host *vha,
3198 struct qla_tgt_srr_imm *imm, int ha_locked)
3199{
3200 struct qla_hw_data *ha = vha->hw;
3201 unsigned long flags = 0;
3202
3203 if (!ha_locked)
3204 spin_lock_irqsave(&ha->hardware_lock, flags);
3205
3206 qlt_send_notify_ack(vha, (void *)&imm->imm_ntfy, 0, 0, 0,
3207 NOTIFY_ACK_SRR_FLAGS_REJECT,
3208 NOTIFY_ACK_SRR_REJECT_REASON_UNABLE_TO_PERFORM,
3209 NOTIFY_ACK_SRR_FLAGS_REJECT_EXPL_NO_EXPL);
3210
3211 if (!ha_locked)
3212 spin_unlock_irqrestore(&ha->hardware_lock, flags);
3213
3214 kfree(imm);
3215}
3216
3217static void qlt_handle_srr_work(struct work_struct *work)
3218{
3219 struct qla_tgt *tgt = container_of(work, struct qla_tgt, srr_work);
3220 struct scsi_qla_host *vha = tgt->vha;
3221 struct qla_tgt_srr_ctio *sctio;
3222 unsigned long flags;
3223
3224 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf029, "Entering SRR work (tgt %p)\n",
3225 tgt);
3226
3227restart:
3228 spin_lock_irqsave(&tgt->srr_lock, flags);
3229 list_for_each_entry(sctio, &tgt->srr_ctio_list, srr_list_entry) {
3230 struct qla_tgt_srr_imm *imm, *i, *ti;
3231 struct qla_tgt_cmd *cmd;
3232 struct se_cmd *se_cmd;
3233
3234 imm = NULL;
3235 list_for_each_entry_safe(i, ti, &tgt->srr_imm_list,
3236 srr_list_entry) {
3237 if (i->srr_id == sctio->srr_id) {
3238 list_del(&i->srr_list_entry);
3239 if (imm) {
3240 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf068,
3241 "qla_target(%d): There must be "
3242 "only one IMM SRR per CTIO SRR "
3243 "(IMM SRR %p, id %d, CTIO %p\n",
3244 vha->vp_idx, i, i->srr_id, sctio);
3245 qlt_reject_free_srr_imm(tgt->vha, i, 0);
3246 } else
3247 imm = i;
3248 }
3249 }
3250
3251 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf02a,
3252 "IMM SRR %p, CTIO SRR %p (id %d)\n", imm, sctio,
3253 sctio->srr_id);
3254
3255 if (imm == NULL) {
3256 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf02b,
3257 "Not found matching IMM for SRR CTIO (id %d)\n",
3258 sctio->srr_id);
3259 continue;
3260 } else
3261 list_del(&sctio->srr_list_entry);
3262
3263 spin_unlock_irqrestore(&tgt->srr_lock, flags);
3264
3265 cmd = sctio->cmd;
3266 /*
3267 * Reset qla_tgt_cmd SRR values and SGL pointer+count to follow
3268 * tcm_qla2xxx_write_pending() and tcm_qla2xxx_queue_data_in()
3269 * logic..
3270 */
3271 cmd->offset = 0;
3272 if (cmd->free_sg) {
3273 kfree(cmd->sg);
3274 cmd->sg = NULL;
3275 cmd->free_sg = 0;
3276 }
3277 se_cmd = &cmd->se_cmd;
3278
3279 cmd->sg_cnt = se_cmd->t_data_nents;
3280 cmd->sg = se_cmd->t_data_sg;
3281
3282 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf02c,
3283 "SRR cmd %p (se_cmd %p, tag %d, op %x), "
3284 "sg_cnt=%d, offset=%d", cmd, &cmd->se_cmd, cmd->tag,
Dr. Greg Wettstein6f58c782014-02-24 13:59:53 -06003285 se_cmd->t_task_cdb ? se_cmd->t_task_cdb[0] : 0,
3286 cmd->sg_cnt, cmd->offset);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003287
3288 qlt_handle_srr(vha, sctio, imm);
3289
3290 kfree(imm);
3291 kfree(sctio);
3292 goto restart;
3293 }
3294 spin_unlock_irqrestore(&tgt->srr_lock, flags);
3295}
3296
3297/* ha->hardware_lock supposed to be held on entry */
3298static void qlt_prepare_srr_imm(struct scsi_qla_host *vha,
3299 struct imm_ntfy_from_isp *iocb)
3300{
3301 struct qla_tgt_srr_imm *imm;
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08003302 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003303 struct qla_tgt_srr_ctio *sctio;
3304
3305 tgt->imm_srr_id++;
3306
3307 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf02d, "qla_target(%d): SRR received\n",
3308 vha->vp_idx);
3309
3310 imm = kzalloc(sizeof(*imm), GFP_ATOMIC);
3311 if (imm != NULL) {
3312 memcpy(&imm->imm_ntfy, iocb, sizeof(imm->imm_ntfy));
3313
3314 /* IRQ is already OFF */
3315 spin_lock(&tgt->srr_lock);
3316 imm->srr_id = tgt->imm_srr_id;
3317 list_add_tail(&imm->srr_list_entry,
3318 &tgt->srr_imm_list);
3319 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf02e,
3320 "IMM NTFY SRR %p added (id %d, ui %x)\n",
3321 imm, imm->srr_id, iocb->u.isp24.srr_ui);
3322 if (tgt->imm_srr_id == tgt->ctio_srr_id) {
3323 int found = 0;
3324 list_for_each_entry(sctio, &tgt->srr_ctio_list,
3325 srr_list_entry) {
3326 if (sctio->srr_id == imm->srr_id) {
3327 found = 1;
3328 break;
3329 }
3330 }
3331 if (found) {
3332 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf02f, "%s",
3333 "Scheduling srr work\n");
3334 schedule_work(&tgt->srr_work);
3335 } else {
3336 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf030,
3337 "qla_target(%d): imm_srr_id "
3338 "== ctio_srr_id (%d), but there is no "
3339 "corresponding SRR CTIO, deleting IMM "
3340 "SRR %p\n", vha->vp_idx, tgt->ctio_srr_id,
3341 imm);
3342 list_del(&imm->srr_list_entry);
3343
3344 kfree(imm);
3345
3346 spin_unlock(&tgt->srr_lock);
3347 goto out_reject;
3348 }
3349 }
3350 spin_unlock(&tgt->srr_lock);
3351 } else {
3352 struct qla_tgt_srr_ctio *ts;
3353
3354 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf069,
3355 "qla_target(%d): Unable to allocate SRR IMM "
3356 "entry, SRR request will be rejected\n", vha->vp_idx);
3357
3358 /* IRQ is already OFF */
3359 spin_lock(&tgt->srr_lock);
3360 list_for_each_entry_safe(sctio, ts, &tgt->srr_ctio_list,
3361 srr_list_entry) {
3362 if (sctio->srr_id == tgt->imm_srr_id) {
3363 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf031,
3364 "CTIO SRR %p deleted (id %d)\n",
3365 sctio, sctio->srr_id);
3366 list_del(&sctio->srr_list_entry);
3367 qlt_send_term_exchange(vha, sctio->cmd,
3368 &sctio->cmd->atio, 1);
3369 kfree(sctio);
3370 }
3371 }
3372 spin_unlock(&tgt->srr_lock);
3373 goto out_reject;
3374 }
3375
3376 return;
3377
3378out_reject:
3379 qlt_send_notify_ack(vha, iocb, 0, 0, 0,
3380 NOTIFY_ACK_SRR_FLAGS_REJECT,
3381 NOTIFY_ACK_SRR_REJECT_REASON_UNABLE_TO_PERFORM,
3382 NOTIFY_ACK_SRR_FLAGS_REJECT_EXPL_NO_EXPL);
3383}
3384
3385/*
3386 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
3387 */
3388static void qlt_handle_imm_notify(struct scsi_qla_host *vha,
3389 struct imm_ntfy_from_isp *iocb)
3390{
3391 struct qla_hw_data *ha = vha->hw;
3392 uint32_t add_flags = 0;
3393 int send_notify_ack = 1;
3394 uint16_t status;
3395
3396 status = le16_to_cpu(iocb->u.isp2x.status);
3397 switch (status) {
3398 case IMM_NTFY_LIP_RESET:
3399 {
3400 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf032,
3401 "qla_target(%d): LIP reset (loop %#x), subcode %x\n",
3402 vha->vp_idx, le16_to_cpu(iocb->u.isp24.nport_handle),
3403 iocb->u.isp24.status_subcode);
3404
3405 if (qlt_reset(vha, iocb, QLA_TGT_ABORT_ALL) == 0)
3406 send_notify_ack = 0;
3407 break;
3408 }
3409
3410 case IMM_NTFY_LIP_LINK_REINIT:
3411 {
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08003412 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003413 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf033,
3414 "qla_target(%d): LINK REINIT (loop %#x, "
3415 "subcode %x)\n", vha->vp_idx,
3416 le16_to_cpu(iocb->u.isp24.nport_handle),
3417 iocb->u.isp24.status_subcode);
3418 if (tgt->link_reinit_iocb_pending) {
3419 qlt_send_notify_ack(vha, &tgt->link_reinit_iocb,
3420 0, 0, 0, 0, 0, 0);
3421 }
3422 memcpy(&tgt->link_reinit_iocb, iocb, sizeof(*iocb));
3423 tgt->link_reinit_iocb_pending = 1;
3424 /*
3425 * QLogic requires to wait after LINK REINIT for possible
3426 * PDISC or ADISC ELS commands
3427 */
3428 send_notify_ack = 0;
3429 break;
3430 }
3431
3432 case IMM_NTFY_PORT_LOGOUT:
3433 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf034,
3434 "qla_target(%d): Port logout (loop "
3435 "%#x, subcode %x)\n", vha->vp_idx,
3436 le16_to_cpu(iocb->u.isp24.nport_handle),
3437 iocb->u.isp24.status_subcode);
3438
3439 if (qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS_SESS) == 0)
3440 send_notify_ack = 0;
3441 /* The sessions will be cleared in the callback, if needed */
3442 break;
3443
3444 case IMM_NTFY_GLBL_TPRLO:
3445 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf035,
3446 "qla_target(%d): Global TPRLO (%x)\n", vha->vp_idx, status);
3447 if (qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS) == 0)
3448 send_notify_ack = 0;
3449 /* The sessions will be cleared in the callback, if needed */
3450 break;
3451
3452 case IMM_NTFY_PORT_CONFIG:
3453 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf036,
3454 "qla_target(%d): Port config changed (%x)\n", vha->vp_idx,
3455 status);
3456 if (qlt_reset(vha, iocb, QLA_TGT_ABORT_ALL) == 0)
3457 send_notify_ack = 0;
3458 /* The sessions will be cleared in the callback, if needed */
3459 break;
3460
3461 case IMM_NTFY_GLBL_LOGO:
3462 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06a,
3463 "qla_target(%d): Link failure detected\n",
3464 vha->vp_idx);
3465 /* I_T nexus loss */
3466 if (qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS) == 0)
3467 send_notify_ack = 0;
3468 break;
3469
3470 case IMM_NTFY_IOCB_OVERFLOW:
3471 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06b,
3472 "qla_target(%d): Cannot provide requested "
3473 "capability (IOCB overflowed the immediate notify "
3474 "resource count)\n", vha->vp_idx);
3475 break;
3476
3477 case IMM_NTFY_ABORT_TASK:
3478 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf037,
3479 "qla_target(%d): Abort Task (S %08x I %#x -> "
3480 "L %#x)\n", vha->vp_idx,
3481 le16_to_cpu(iocb->u.isp2x.seq_id),
3482 GET_TARGET_ID(ha, (struct atio_from_isp *)iocb),
3483 le16_to_cpu(iocb->u.isp2x.lun));
3484 if (qlt_abort_task(vha, iocb) == 0)
3485 send_notify_ack = 0;
3486 break;
3487
3488 case IMM_NTFY_RESOURCE:
3489 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06c,
3490 "qla_target(%d): Out of resources, host %ld\n",
3491 vha->vp_idx, vha->host_no);
3492 break;
3493
3494 case IMM_NTFY_MSG_RX:
3495 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf038,
3496 "qla_target(%d): Immediate notify task %x\n",
3497 vha->vp_idx, iocb->u.isp2x.task_flags);
3498 if (qlt_handle_task_mgmt(vha, iocb) == 0)
3499 send_notify_ack = 0;
3500 break;
3501
3502 case IMM_NTFY_ELS:
3503 if (qlt_24xx_handle_els(vha, iocb) == 0)
3504 send_notify_ack = 0;
3505 break;
3506
3507 case IMM_NTFY_SRR:
3508 qlt_prepare_srr_imm(vha, iocb);
3509 send_notify_ack = 0;
3510 break;
3511
3512 default:
3513 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06d,
3514 "qla_target(%d): Received unknown immediate "
3515 "notify status %x\n", vha->vp_idx, status);
3516 break;
3517 }
3518
3519 if (send_notify_ack)
3520 qlt_send_notify_ack(vha, iocb, add_flags, 0, 0, 0, 0, 0);
3521}
3522
3523/*
3524 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
3525 * This function sends busy to ISP 2xxx or 24xx.
3526 */
3527static void qlt_send_busy(struct scsi_qla_host *vha,
3528 struct atio_from_isp *atio, uint16_t status)
3529{
3530 struct ctio7_to_24xx *ctio24;
3531 struct qla_hw_data *ha = vha->hw;
3532 request_t *pkt;
3533 struct qla_tgt_sess *sess = NULL;
3534
3535 sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha,
3536 atio->u.isp24.fcp_hdr.s_id);
3537 if (!sess) {
3538 qlt_send_term_exchange(vha, NULL, atio, 1);
3539 return;
3540 }
3541 /* Sending marker isn't necessary, since we called from ISR */
3542
3543 pkt = (request_t *)qla2x00_alloc_iocbs(vha, NULL);
3544 if (!pkt) {
3545 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06e,
3546 "qla_target(%d): %s failed: unable to allocate "
3547 "request packet", vha->vp_idx, __func__);
3548 return;
3549 }
3550
3551 pkt->entry_count = 1;
3552 pkt->handle = QLA_TGT_SKIP_HANDLE | CTIO_COMPLETION_HANDLE_MARK;
3553
3554 ctio24 = (struct ctio7_to_24xx *)pkt;
3555 ctio24->entry_type = CTIO_TYPE7;
3556 ctio24->nport_handle = sess->loop_id;
3557 ctio24->timeout = __constant_cpu_to_le16(QLA_TGT_TIMEOUT);
3558 ctio24->vp_index = vha->vp_idx;
3559 ctio24->initiator_id[0] = atio->u.isp24.fcp_hdr.s_id[2];
3560 ctio24->initiator_id[1] = atio->u.isp24.fcp_hdr.s_id[1];
3561 ctio24->initiator_id[2] = atio->u.isp24.fcp_hdr.s_id[0];
3562 ctio24->exchange_addr = atio->u.isp24.exchange_addr;
3563 ctio24->u.status1.flags = (atio->u.isp24.attr << 9) |
3564 __constant_cpu_to_le16(
3565 CTIO7_FLAGS_STATUS_MODE_1 | CTIO7_FLAGS_SEND_STATUS |
3566 CTIO7_FLAGS_DONT_RET_CTIO);
3567 /*
3568 * CTIO from fw w/o se_cmd doesn't provide enough info to retry it,
3569 * if the explicit conformation is used.
3570 */
3571 ctio24->u.status1.ox_id = swab16(atio->u.isp24.fcp_hdr.ox_id);
3572 ctio24->u.status1.scsi_status = cpu_to_le16(status);
3573 ctio24->u.status1.residual = get_unaligned((uint32_t *)
3574 &atio->u.isp24.fcp_cmnd.add_cdb[
3575 atio->u.isp24.fcp_cmnd.add_cdb_len]);
3576 if (ctio24->u.status1.residual != 0)
3577 ctio24->u.status1.scsi_status |= SS_RESIDUAL_UNDER;
3578
3579 qla2x00_start_iocbs(vha, vha->req);
3580}
3581
3582/* ha->hardware_lock supposed to be held on entry */
3583/* called via callback from qla2xxx */
3584static void qlt_24xx_atio_pkt(struct scsi_qla_host *vha,
3585 struct atio_from_isp *atio)
3586{
3587 struct qla_hw_data *ha = vha->hw;
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08003588 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003589 int rc;
3590
3591 if (unlikely(tgt == NULL)) {
3592 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf039,
3593 "ATIO pkt, but no tgt (ha %p)", ha);
3594 return;
3595 }
3596 ql_dbg(ql_dbg_tgt, vha, 0xe02c,
3597 "qla_target(%d): ATIO pkt %p: type %02x count %02x",
3598 vha->vp_idx, atio, atio->u.raw.entry_type,
3599 atio->u.raw.entry_count);
3600 /*
3601 * In tgt_stop mode we also should allow all requests to pass.
3602 * Otherwise, some commands can stuck.
3603 */
3604
3605 tgt->irq_cmd_count++;
3606
3607 switch (atio->u.raw.entry_type) {
3608 case ATIO_TYPE7:
3609 ql_dbg(ql_dbg_tgt, vha, 0xe02d,
3610 "ATIO_TYPE7 instance %d, lun %Lx, read/write %d/%d, "
3611 "add_cdb_len %d, data_length %04x, s_id %x:%x:%x\n",
3612 vha->vp_idx, atio->u.isp24.fcp_cmnd.lun,
3613 atio->u.isp24.fcp_cmnd.rddata,
3614 atio->u.isp24.fcp_cmnd.wrdata,
3615 atio->u.isp24.fcp_cmnd.add_cdb_len,
3616 be32_to_cpu(get_unaligned((uint32_t *)
3617 &atio->u.isp24.fcp_cmnd.add_cdb[
3618 atio->u.isp24.fcp_cmnd.add_cdb_len])),
3619 atio->u.isp24.fcp_hdr.s_id[0],
3620 atio->u.isp24.fcp_hdr.s_id[1],
3621 atio->u.isp24.fcp_hdr.s_id[2]);
3622
3623 if (unlikely(atio->u.isp24.exchange_addr ==
3624 ATIO_EXCHANGE_ADDRESS_UNKNOWN)) {
3625 ql_dbg(ql_dbg_tgt, vha, 0xe058,
3626 "qla_target(%d): ATIO_TYPE7 "
3627 "received with UNKNOWN exchange address, "
3628 "sending QUEUE_FULL\n", vha->vp_idx);
3629 qlt_send_busy(vha, atio, SAM_STAT_TASK_SET_FULL);
3630 break;
3631 }
3632 if (likely(atio->u.isp24.fcp_cmnd.task_mgmt_flags == 0))
3633 rc = qlt_handle_cmd_for_atio(vha, atio);
3634 else
3635 rc = qlt_handle_task_mgmt(vha, atio);
3636 if (unlikely(rc != 0)) {
3637 if (rc == -ESRCH) {
3638#if 1 /* With TERM EXCHANGE some FC cards refuse to boot */
3639 qlt_send_busy(vha, atio, SAM_STAT_BUSY);
3640#else
3641 qlt_send_term_exchange(vha, NULL, atio, 1);
3642#endif
3643 } else {
3644 if (tgt->tgt_stop) {
3645 ql_dbg(ql_dbg_tgt, vha, 0xe059,
3646 "qla_target: Unable to send "
3647 "command to target for req, "
3648 "ignoring.\n");
3649 } else {
3650 ql_dbg(ql_dbg_tgt, vha, 0xe05a,
3651 "qla_target(%d): Unable to send "
3652 "command to target, sending BUSY "
3653 "status.\n", vha->vp_idx);
3654 qlt_send_busy(vha, atio, SAM_STAT_BUSY);
3655 }
3656 }
3657 }
3658 break;
3659
3660 case IMMED_NOTIFY_TYPE:
3661 {
3662 if (unlikely(atio->u.isp2x.entry_status != 0)) {
3663 ql_dbg(ql_dbg_tgt, vha, 0xe05b,
3664 "qla_target(%d): Received ATIO packet %x "
3665 "with error status %x\n", vha->vp_idx,
3666 atio->u.raw.entry_type,
3667 atio->u.isp2x.entry_status);
3668 break;
3669 }
3670 ql_dbg(ql_dbg_tgt, vha, 0xe02e, "%s", "IMMED_NOTIFY ATIO");
3671 qlt_handle_imm_notify(vha, (struct imm_ntfy_from_isp *)atio);
3672 break;
3673 }
3674
3675 default:
3676 ql_dbg(ql_dbg_tgt, vha, 0xe05c,
3677 "qla_target(%d): Received unknown ATIO atio "
3678 "type %x\n", vha->vp_idx, atio->u.raw.entry_type);
3679 break;
3680 }
3681
3682 tgt->irq_cmd_count--;
3683}
3684
3685/* ha->hardware_lock supposed to be held on entry */
3686/* called via callback from qla2xxx */
3687static void qlt_response_pkt(struct scsi_qla_host *vha, response_t *pkt)
3688{
3689 struct qla_hw_data *ha = vha->hw;
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08003690 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003691
3692 if (unlikely(tgt == NULL)) {
3693 ql_dbg(ql_dbg_tgt, vha, 0xe05d,
3694 "qla_target(%d): Response pkt %x received, but no "
3695 "tgt (ha %p)\n", vha->vp_idx, pkt->entry_type, ha);
3696 return;
3697 }
3698
3699 ql_dbg(ql_dbg_tgt, vha, 0xe02f,
3700 "qla_target(%d): response pkt %p: T %02x C %02x S %02x "
3701 "handle %#x\n", vha->vp_idx, pkt, pkt->entry_type,
3702 pkt->entry_count, pkt->entry_status, pkt->handle);
3703
3704 /*
3705 * In tgt_stop mode we also should allow all requests to pass.
3706 * Otherwise, some commands can stuck.
3707 */
3708
3709 tgt->irq_cmd_count++;
3710
3711 switch (pkt->entry_type) {
3712 case CTIO_TYPE7:
3713 {
3714 struct ctio7_from_24xx *entry = (struct ctio7_from_24xx *)pkt;
3715 ql_dbg(ql_dbg_tgt, vha, 0xe030, "CTIO_TYPE7: instance %d\n",
3716 vha->vp_idx);
3717 qlt_do_ctio_completion(vha, entry->handle,
3718 le16_to_cpu(entry->status)|(pkt->entry_status << 16),
3719 entry);
3720 break;
3721 }
3722
3723 case ACCEPT_TGT_IO_TYPE:
3724 {
3725 struct atio_from_isp *atio = (struct atio_from_isp *)pkt;
3726 int rc;
3727 ql_dbg(ql_dbg_tgt, vha, 0xe031,
3728 "ACCEPT_TGT_IO instance %d status %04x "
3729 "lun %04x read/write %d data_length %04x "
3730 "target_id %02x rx_id %04x\n ", vha->vp_idx,
3731 le16_to_cpu(atio->u.isp2x.status),
3732 le16_to_cpu(atio->u.isp2x.lun),
3733 atio->u.isp2x.execution_codes,
3734 le32_to_cpu(atio->u.isp2x.data_length), GET_TARGET_ID(ha,
3735 atio), atio->u.isp2x.rx_id);
3736 if (atio->u.isp2x.status !=
3737 __constant_cpu_to_le16(ATIO_CDB_VALID)) {
3738 ql_dbg(ql_dbg_tgt, vha, 0xe05e,
3739 "qla_target(%d): ATIO with error "
3740 "status %x received\n", vha->vp_idx,
3741 le16_to_cpu(atio->u.isp2x.status));
3742 break;
3743 }
3744 ql_dbg(ql_dbg_tgt, vha, 0xe032,
3745 "FCP CDB: 0x%02x, sizeof(cdb): %lu",
3746 atio->u.isp2x.cdb[0], (unsigned long
3747 int)sizeof(atio->u.isp2x.cdb));
3748
3749 rc = qlt_handle_cmd_for_atio(vha, atio);
3750 if (unlikely(rc != 0)) {
3751 if (rc == -ESRCH) {
3752#if 1 /* With TERM EXCHANGE some FC cards refuse to boot */
3753 qlt_send_busy(vha, atio, 0);
3754#else
3755 qlt_send_term_exchange(vha, NULL, atio, 1);
3756#endif
3757 } else {
3758 if (tgt->tgt_stop) {
3759 ql_dbg(ql_dbg_tgt, vha, 0xe05f,
3760 "qla_target: Unable to send "
3761 "command to target, sending TERM "
3762 "EXCHANGE for rsp\n");
3763 qlt_send_term_exchange(vha, NULL,
3764 atio, 1);
3765 } else {
3766 ql_dbg(ql_dbg_tgt, vha, 0xe060,
3767 "qla_target(%d): Unable to send "
3768 "command to target, sending BUSY "
3769 "status\n", vha->vp_idx);
3770 qlt_send_busy(vha, atio, 0);
3771 }
3772 }
3773 }
3774 }
3775 break;
3776
3777 case CONTINUE_TGT_IO_TYPE:
3778 {
3779 struct ctio_to_2xxx *entry = (struct ctio_to_2xxx *)pkt;
3780 ql_dbg(ql_dbg_tgt, vha, 0xe033,
3781 "CONTINUE_TGT_IO: instance %d\n", vha->vp_idx);
3782 qlt_do_ctio_completion(vha, entry->handle,
3783 le16_to_cpu(entry->status)|(pkt->entry_status << 16),
3784 entry);
3785 break;
3786 }
3787
3788 case CTIO_A64_TYPE:
3789 {
3790 struct ctio_to_2xxx *entry = (struct ctio_to_2xxx *)pkt;
3791 ql_dbg(ql_dbg_tgt, vha, 0xe034, "CTIO_A64: instance %d\n",
3792 vha->vp_idx);
3793 qlt_do_ctio_completion(vha, entry->handle,
3794 le16_to_cpu(entry->status)|(pkt->entry_status << 16),
3795 entry);
3796 break;
3797 }
3798
3799 case IMMED_NOTIFY_TYPE:
3800 ql_dbg(ql_dbg_tgt, vha, 0xe035, "%s", "IMMED_NOTIFY\n");
3801 qlt_handle_imm_notify(vha, (struct imm_ntfy_from_isp *)pkt);
3802 break;
3803
3804 case NOTIFY_ACK_TYPE:
3805 if (tgt->notify_ack_expected > 0) {
3806 struct nack_to_isp *entry = (struct nack_to_isp *)pkt;
3807 ql_dbg(ql_dbg_tgt, vha, 0xe036,
3808 "NOTIFY_ACK seq %08x status %x\n",
3809 le16_to_cpu(entry->u.isp2x.seq_id),
3810 le16_to_cpu(entry->u.isp2x.status));
3811 tgt->notify_ack_expected--;
3812 if (entry->u.isp2x.status !=
3813 __constant_cpu_to_le16(NOTIFY_ACK_SUCCESS)) {
3814 ql_dbg(ql_dbg_tgt, vha, 0xe061,
3815 "qla_target(%d): NOTIFY_ACK "
3816 "failed %x\n", vha->vp_idx,
3817 le16_to_cpu(entry->u.isp2x.status));
3818 }
3819 } else {
3820 ql_dbg(ql_dbg_tgt, vha, 0xe062,
3821 "qla_target(%d): Unexpected NOTIFY_ACK received\n",
3822 vha->vp_idx);
3823 }
3824 break;
3825
3826 case ABTS_RECV_24XX:
3827 ql_dbg(ql_dbg_tgt, vha, 0xe037,
3828 "ABTS_RECV_24XX: instance %d\n", vha->vp_idx);
3829 qlt_24xx_handle_abts(vha, (struct abts_recv_from_24xx *)pkt);
3830 break;
3831
3832 case ABTS_RESP_24XX:
3833 if (tgt->abts_resp_expected > 0) {
3834 struct abts_resp_from_24xx_fw *entry =
3835 (struct abts_resp_from_24xx_fw *)pkt;
3836 ql_dbg(ql_dbg_tgt, vha, 0xe038,
3837 "ABTS_RESP_24XX: compl_status %x\n",
3838 entry->compl_status);
3839 tgt->abts_resp_expected--;
3840 if (le16_to_cpu(entry->compl_status) !=
3841 ABTS_RESP_COMPL_SUCCESS) {
3842 if ((entry->error_subcode1 == 0x1E) &&
3843 (entry->error_subcode2 == 0)) {
3844 /*
3845 * We've got a race here: aborted
3846 * exchange not terminated, i.e.
3847 * response for the aborted command was
3848 * sent between the abort request was
3849 * received and processed.
3850 * Unfortunately, the firmware has a
3851 * silly requirement that all aborted
3852 * exchanges must be explicitely
3853 * terminated, otherwise it refuses to
3854 * send responses for the abort
3855 * requests. So, we have to
3856 * (re)terminate the exchange and retry
3857 * the abort response.
3858 */
3859 qlt_24xx_retry_term_exchange(vha,
3860 entry);
3861 } else
3862 ql_dbg(ql_dbg_tgt, vha, 0xe063,
3863 "qla_target(%d): ABTS_RESP_24XX "
3864 "failed %x (subcode %x:%x)",
3865 vha->vp_idx, entry->compl_status,
3866 entry->error_subcode1,
3867 entry->error_subcode2);
3868 }
3869 } else {
3870 ql_dbg(ql_dbg_tgt, vha, 0xe064,
3871 "qla_target(%d): Unexpected ABTS_RESP_24XX "
3872 "received\n", vha->vp_idx);
3873 }
3874 break;
3875
3876 default:
3877 ql_dbg(ql_dbg_tgt, vha, 0xe065,
3878 "qla_target(%d): Received unknown response pkt "
3879 "type %x\n", vha->vp_idx, pkt->entry_type);
3880 break;
3881 }
3882
3883 tgt->irq_cmd_count--;
3884}
3885
3886/*
3887 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
3888 */
3889void qlt_async_event(uint16_t code, struct scsi_qla_host *vha,
3890 uint16_t *mailbox)
3891{
3892 struct qla_hw_data *ha = vha->hw;
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08003893 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
Alan Cox4f1d0f12012-07-04 16:35:35 +01003894 int login_code;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003895
3896 ql_dbg(ql_dbg_tgt, vha, 0xe039,
3897 "scsi(%ld): ha state %d init_done %d oper_mode %d topo %d\n",
3898 vha->host_no, atomic_read(&vha->loop_state), vha->flags.init_done,
3899 ha->operating_mode, ha->current_topology);
3900
3901 if (!ha->tgt.tgt_ops)
3902 return;
3903
3904 if (unlikely(tgt == NULL)) {
3905 ql_dbg(ql_dbg_tgt, vha, 0xe03a,
3906 "ASYNC EVENT %#x, but no tgt (ha %p)\n", code, ha);
3907 return;
3908 }
3909
3910 if (((code == MBA_POINT_TO_POINT) || (code == MBA_CHG_IN_CONNECTION)) &&
3911 IS_QLA2100(ha))
3912 return;
3913 /*
3914 * In tgt_stop mode we also should allow all requests to pass.
3915 * Otherwise, some commands can stuck.
3916 */
3917
3918 tgt->irq_cmd_count++;
3919
3920 switch (code) {
3921 case MBA_RESET: /* Reset */
3922 case MBA_SYSTEM_ERR: /* System Error */
3923 case MBA_REQ_TRANSFER_ERR: /* Request Transfer Error */
3924 case MBA_RSP_TRANSFER_ERR: /* Response Transfer Error */
3925 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03a,
3926 "qla_target(%d): System error async event %#x "
Masanari Iida6efb3c02012-10-26 22:10:54 +09003927 "occurred", vha->vp_idx, code);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003928 break;
3929 case MBA_WAKEUP_THRES: /* Request Queue Wake-up. */
3930 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
3931 break;
3932
3933 case MBA_LOOP_UP:
3934 {
3935 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03b,
Masanari Iida6efb3c02012-10-26 22:10:54 +09003936 "qla_target(%d): Async LOOP_UP occurred "
Alan Cox4f1d0f12012-07-04 16:35:35 +01003937 "(m[0]=%x, m[1]=%x, m[2]=%x, m[3]=%x)", vha->vp_idx,
3938 le16_to_cpu(mailbox[0]), le16_to_cpu(mailbox[1]),
3939 le16_to_cpu(mailbox[2]), le16_to_cpu(mailbox[3]));
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003940 if (tgt->link_reinit_iocb_pending) {
3941 qlt_send_notify_ack(vha, (void *)&tgt->link_reinit_iocb,
3942 0, 0, 0, 0, 0, 0);
3943 tgt->link_reinit_iocb_pending = 0;
3944 }
3945 break;
3946 }
3947
3948 case MBA_LIP_OCCURRED:
3949 case MBA_LOOP_DOWN:
3950 case MBA_LIP_RESET:
3951 case MBA_RSCN_UPDATE:
3952 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03c,
Masanari Iida6efb3c02012-10-26 22:10:54 +09003953 "qla_target(%d): Async event %#x occurred "
Alan Cox4f1d0f12012-07-04 16:35:35 +01003954 "(m[0]=%x, m[1]=%x, m[2]=%x, m[3]=%x)", vha->vp_idx, code,
3955 le16_to_cpu(mailbox[0]), le16_to_cpu(mailbox[1]),
3956 le16_to_cpu(mailbox[2]), le16_to_cpu(mailbox[3]));
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003957 break;
3958
3959 case MBA_PORT_UPDATE:
3960 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03d,
3961 "qla_target(%d): Port update async event %#x "
Masanari Iida6efb3c02012-10-26 22:10:54 +09003962 "occurred: updating the ports database (m[0]=%x, m[1]=%x, "
Alan Cox4f1d0f12012-07-04 16:35:35 +01003963 "m[2]=%x, m[3]=%x)", vha->vp_idx, code,
3964 le16_to_cpu(mailbox[0]), le16_to_cpu(mailbox[1]),
3965 le16_to_cpu(mailbox[2]), le16_to_cpu(mailbox[3]));
3966
3967 login_code = le16_to_cpu(mailbox[2]);
3968 if (login_code == 0x4)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003969 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03e,
3970 "Async MB 2: Got PLOGI Complete\n");
Alan Cox4f1d0f12012-07-04 16:35:35 +01003971 else if (login_code == 0x7)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003972 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03f,
3973 "Async MB 2: Port Logged Out\n");
3974 break;
3975
3976 default:
3977 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf040,
Masanari Iida6efb3c02012-10-26 22:10:54 +09003978 "qla_target(%d): Async event %#x occurred: "
Alan Cox4f1d0f12012-07-04 16:35:35 +01003979 "ignore (m[0]=%x, m[1]=%x, m[2]=%x, m[3]=%x)", vha->vp_idx,
3980 code, le16_to_cpu(mailbox[0]), le16_to_cpu(mailbox[1]),
3981 le16_to_cpu(mailbox[2]), le16_to_cpu(mailbox[3]));
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003982 break;
3983 }
3984
3985 tgt->irq_cmd_count--;
3986}
3987
3988static fc_port_t *qlt_get_port_database(struct scsi_qla_host *vha,
3989 uint16_t loop_id)
3990{
3991 fc_port_t *fcport;
3992 int rc;
3993
3994 fcport = kzalloc(sizeof(*fcport), GFP_KERNEL);
3995 if (!fcport) {
3996 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06f,
3997 "qla_target(%d): Allocation of tmp FC port failed",
3998 vha->vp_idx);
3999 return NULL;
4000 }
4001
4002 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf041, "loop_id %d", loop_id);
4003
4004 fcport->loop_id = loop_id;
4005
4006 rc = qla2x00_get_port_database(vha, fcport, 0);
4007 if (rc != QLA_SUCCESS) {
4008 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf070,
4009 "qla_target(%d): Failed to retrieve fcport "
4010 "information -- get_port_database() returned %x "
4011 "(loop_id=0x%04x)", vha->vp_idx, rc, loop_id);
4012 kfree(fcport);
4013 return NULL;
4014 }
4015
4016 return fcport;
4017}
4018
4019/* Must be called under tgt_mutex */
4020static struct qla_tgt_sess *qlt_make_local_sess(struct scsi_qla_host *vha,
4021 uint8_t *s_id)
4022{
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004023 struct qla_tgt_sess *sess = NULL;
4024 fc_port_t *fcport = NULL;
4025 int rc, global_resets;
4026 uint16_t loop_id = 0;
4027
4028retry:
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08004029 global_resets =
4030 atomic_read(&vha->vha_tgt.qla_tgt->tgt_global_resets_count);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004031
4032 rc = qla24xx_get_loop_id(vha, s_id, &loop_id);
4033 if (rc != 0) {
4034 if ((s_id[0] == 0xFF) &&
4035 (s_id[1] == 0xFC)) {
4036 /*
4037 * This is Domain Controller, so it should be
4038 * OK to drop SCSI commands from it.
4039 */
4040 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf042,
4041 "Unable to find initiator with S_ID %x:%x:%x",
4042 s_id[0], s_id[1], s_id[2]);
4043 } else
4044 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf071,
4045 "qla_target(%d): Unable to find "
4046 "initiator with S_ID %x:%x:%x",
4047 vha->vp_idx, s_id[0], s_id[1],
4048 s_id[2]);
4049 return NULL;
4050 }
4051
4052 fcport = qlt_get_port_database(vha, loop_id);
4053 if (!fcport)
4054 return NULL;
4055
4056 if (global_resets !=
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08004057 atomic_read(&vha->vha_tgt.qla_tgt->tgt_global_resets_count)) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004058 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf043,
4059 "qla_target(%d): global reset during session discovery "
4060 "(counter was %d, new %d), retrying", vha->vp_idx,
4061 global_resets,
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08004062 atomic_read(&vha->vha_tgt.
4063 qla_tgt->tgt_global_resets_count));
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004064 goto retry;
4065 }
4066
4067 sess = qlt_create_sess(vha, fcport, true);
4068
4069 kfree(fcport);
4070 return sess;
4071}
4072
4073static void qlt_abort_work(struct qla_tgt *tgt,
4074 struct qla_tgt_sess_work_param *prm)
4075{
4076 struct scsi_qla_host *vha = tgt->vha;
4077 struct qla_hw_data *ha = vha->hw;
4078 struct qla_tgt_sess *sess = NULL;
4079 unsigned long flags;
4080 uint32_t be_s_id;
4081 uint8_t s_id[3];
4082 int rc;
4083
4084 spin_lock_irqsave(&ha->hardware_lock, flags);
4085
4086 if (tgt->tgt_stop)
4087 goto out_term;
4088
4089 s_id[0] = prm->abts.fcp_hdr_le.s_id[2];
4090 s_id[1] = prm->abts.fcp_hdr_le.s_id[1];
4091 s_id[2] = prm->abts.fcp_hdr_le.s_id[0];
4092
4093 sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha,
4094 (unsigned char *)&be_s_id);
4095 if (!sess) {
4096 spin_unlock_irqrestore(&ha->hardware_lock, flags);
4097
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08004098 mutex_lock(&vha->vha_tgt.tgt_mutex);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004099 sess = qlt_make_local_sess(vha, s_id);
4100 /* sess has got an extra creation ref */
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08004101 mutex_unlock(&vha->vha_tgt.tgt_mutex);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004102
4103 spin_lock_irqsave(&ha->hardware_lock, flags);
4104 if (!sess)
4105 goto out_term;
4106 } else {
4107 kref_get(&sess->se_sess->sess_kref);
4108 }
4109
4110 if (tgt->tgt_stop)
4111 goto out_term;
4112
4113 rc = __qlt_24xx_handle_abts(vha, &prm->abts, sess);
4114 if (rc != 0)
4115 goto out_term;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004116
4117 ha->tgt.tgt_ops->put_sess(sess);
Jörn Engel08234e32013-06-12 16:27:54 -04004118 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004119 return;
4120
4121out_term:
4122 qlt_24xx_send_abts_resp(vha, &prm->abts, FCP_TMF_REJECTED, false);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004123 if (sess)
4124 ha->tgt.tgt_ops->put_sess(sess);
Jörn Engel08234e32013-06-12 16:27:54 -04004125 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004126}
4127
4128static void qlt_tmr_work(struct qla_tgt *tgt,
4129 struct qla_tgt_sess_work_param *prm)
4130{
4131 struct atio_from_isp *a = &prm->tm_iocb2;
4132 struct scsi_qla_host *vha = tgt->vha;
4133 struct qla_hw_data *ha = vha->hw;
4134 struct qla_tgt_sess *sess = NULL;
4135 unsigned long flags;
4136 uint8_t *s_id = NULL; /* to hide compiler warnings */
4137 int rc;
4138 uint32_t lun, unpacked_lun;
4139 int lun_size, fn;
4140 void *iocb;
4141
4142 spin_lock_irqsave(&ha->hardware_lock, flags);
4143
4144 if (tgt->tgt_stop)
4145 goto out_term;
4146
4147 s_id = prm->tm_iocb2.u.isp24.fcp_hdr.s_id;
4148 sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha, s_id);
4149 if (!sess) {
4150 spin_unlock_irqrestore(&ha->hardware_lock, flags);
4151
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08004152 mutex_lock(&vha->vha_tgt.tgt_mutex);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004153 sess = qlt_make_local_sess(vha, s_id);
4154 /* sess has got an extra creation ref */
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08004155 mutex_unlock(&vha->vha_tgt.tgt_mutex);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004156
4157 spin_lock_irqsave(&ha->hardware_lock, flags);
4158 if (!sess)
4159 goto out_term;
4160 } else {
4161 kref_get(&sess->se_sess->sess_kref);
4162 }
4163
4164 iocb = a;
4165 lun = a->u.isp24.fcp_cmnd.lun;
4166 lun_size = sizeof(lun);
4167 fn = a->u.isp24.fcp_cmnd.task_mgmt_flags;
4168 unpacked_lun = scsilun_to_int((struct scsi_lun *)&lun);
4169
4170 rc = qlt_issue_task_mgmt(sess, unpacked_lun, fn, iocb, 0);
4171 if (rc != 0)
4172 goto out_term;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004173
4174 ha->tgt.tgt_ops->put_sess(sess);
Jörn Engel08234e32013-06-12 16:27:54 -04004175 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004176 return;
4177
4178out_term:
4179 qlt_send_term_exchange(vha, NULL, &prm->tm_iocb2, 1);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004180 if (sess)
4181 ha->tgt.tgt_ops->put_sess(sess);
Jörn Engel08234e32013-06-12 16:27:54 -04004182 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004183}
4184
4185static void qlt_sess_work_fn(struct work_struct *work)
4186{
4187 struct qla_tgt *tgt = container_of(work, struct qla_tgt, sess_work);
4188 struct scsi_qla_host *vha = tgt->vha;
4189 unsigned long flags;
4190
4191 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf000, "Sess work (tgt %p)", tgt);
4192
4193 spin_lock_irqsave(&tgt->sess_work_lock, flags);
4194 while (!list_empty(&tgt->sess_works_list)) {
4195 struct qla_tgt_sess_work_param *prm = list_entry(
4196 tgt->sess_works_list.next, typeof(*prm),
4197 sess_works_list_entry);
4198
4199 /*
4200 * This work can be scheduled on several CPUs at time, so we
4201 * must delete the entry to eliminate double processing
4202 */
4203 list_del(&prm->sess_works_list_entry);
4204
4205 spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
4206
4207 switch (prm->type) {
4208 case QLA_TGT_SESS_WORK_ABORT:
4209 qlt_abort_work(tgt, prm);
4210 break;
4211 case QLA_TGT_SESS_WORK_TM:
4212 qlt_tmr_work(tgt, prm);
4213 break;
4214 default:
4215 BUG_ON(1);
4216 break;
4217 }
4218
4219 spin_lock_irqsave(&tgt->sess_work_lock, flags);
4220
4221 kfree(prm);
4222 }
4223 spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
4224}
4225
4226/* Must be called under tgt_host_action_mutex */
4227int qlt_add_target(struct qla_hw_data *ha, struct scsi_qla_host *base_vha)
4228{
4229 struct qla_tgt *tgt;
4230
4231 if (!QLA_TGT_MODE_ENABLED())
4232 return 0;
4233
Arun Easi33c36c02013-01-30 03:34:41 -05004234 if (!IS_TGT_MODE_CAPABLE(ha)) {
4235 ql_log(ql_log_warn, base_vha, 0xe070,
4236 "This adapter does not support target mode.\n");
4237 return 0;
4238 }
4239
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004240 ql_dbg(ql_dbg_tgt, base_vha, 0xe03b,
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08004241 "Registering target for host %ld(%p).\n", base_vha->host_no, ha);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004242
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08004243 BUG_ON(base_vha->vha_tgt.qla_tgt != NULL);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004244
4245 tgt = kzalloc(sizeof(struct qla_tgt), GFP_KERNEL);
4246 if (!tgt) {
4247 ql_dbg(ql_dbg_tgt, base_vha, 0xe066,
4248 "Unable to allocate struct qla_tgt\n");
4249 return -ENOMEM;
4250 }
4251
4252 if (!(base_vha->host->hostt->supported_mode & MODE_TARGET))
4253 base_vha->host->hostt->supported_mode |= MODE_TARGET;
4254
4255 tgt->ha = ha;
4256 tgt->vha = base_vha;
4257 init_waitqueue_head(&tgt->waitQ);
4258 INIT_LIST_HEAD(&tgt->sess_list);
4259 INIT_LIST_HEAD(&tgt->del_sess_list);
4260 INIT_DELAYED_WORK(&tgt->sess_del_work,
4261 (void (*)(struct work_struct *))qlt_del_sess_work_fn);
4262 spin_lock_init(&tgt->sess_work_lock);
4263 INIT_WORK(&tgt->sess_work, qlt_sess_work_fn);
4264 INIT_LIST_HEAD(&tgt->sess_works_list);
4265 spin_lock_init(&tgt->srr_lock);
4266 INIT_LIST_HEAD(&tgt->srr_ctio_list);
4267 INIT_LIST_HEAD(&tgt->srr_imm_list);
4268 INIT_WORK(&tgt->srr_work, qlt_handle_srr_work);
4269 atomic_set(&tgt->tgt_global_resets_count, 0);
4270
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08004271 base_vha->vha_tgt.qla_tgt = tgt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004272
4273 ql_dbg(ql_dbg_tgt, base_vha, 0xe067,
4274 "qla_target(%d): using 64 Bit PCI addressing",
4275 base_vha->vp_idx);
4276 tgt->tgt_enable_64bit_addr = 1;
4277 /* 3 is reserved */
4278 tgt->sg_tablesize = QLA_TGT_MAX_SG_24XX(base_vha->req->length - 3);
4279 tgt->datasegs_per_cmd = QLA_TGT_DATASEGS_PER_CMD_24XX;
4280 tgt->datasegs_per_cont = QLA_TGT_DATASEGS_PER_CONT_24XX;
4281
Nicholas Bellingerddb95142014-02-19 17:51:25 -08004282 if (base_vha->fc_vport)
4283 return 0;
4284
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004285 mutex_lock(&qla_tgt_mutex);
4286 list_add_tail(&tgt->tgt_list_entry, &qla_tgt_glist);
4287 mutex_unlock(&qla_tgt_mutex);
4288
4289 return 0;
4290}
4291
4292/* Must be called under tgt_host_action_mutex */
4293int qlt_remove_target(struct qla_hw_data *ha, struct scsi_qla_host *vha)
4294{
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08004295 if (!vha->vha_tgt.qla_tgt)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004296 return 0;
4297
Nicholas Bellingerddb95142014-02-19 17:51:25 -08004298 if (vha->fc_vport) {
4299 qlt_release(vha->vha_tgt.qla_tgt);
4300 return 0;
4301 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004302 mutex_lock(&qla_tgt_mutex);
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08004303 list_del(&vha->vha_tgt.qla_tgt->tgt_list_entry);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004304 mutex_unlock(&qla_tgt_mutex);
4305
4306 ql_dbg(ql_dbg_tgt, vha, 0xe03c, "Unregistering target for host %ld(%p)",
4307 vha->host_no, ha);
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08004308 qlt_release(vha->vha_tgt.qla_tgt);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004309
4310 return 0;
4311}
4312
4313static void qlt_lport_dump(struct scsi_qla_host *vha, u64 wwpn,
4314 unsigned char *b)
4315{
4316 int i;
4317
4318 pr_debug("qla2xxx HW vha->node_name: ");
4319 for (i = 0; i < WWN_SIZE; i++)
4320 pr_debug("%02x ", vha->node_name[i]);
4321 pr_debug("\n");
4322 pr_debug("qla2xxx HW vha->port_name: ");
4323 for (i = 0; i < WWN_SIZE; i++)
4324 pr_debug("%02x ", vha->port_name[i]);
4325 pr_debug("\n");
4326
4327 pr_debug("qla2xxx passed configfs WWPN: ");
4328 put_unaligned_be64(wwpn, b);
4329 for (i = 0; i < WWN_SIZE; i++)
4330 pr_debug("%02x ", b[i]);
4331 pr_debug("\n");
4332}
4333
4334/**
4335 * qla_tgt_lport_register - register lport with external module
4336 *
4337 * @qla_tgt_ops: Pointer for tcm_qla2xxx qla_tgt_ops
4338 * @wwpn: Passwd FC target WWPN
4339 * @callback: lport initialization callback for tcm_qla2xxx code
4340 * @target_lport_ptr: pointer for tcm_qla2xxx specific lport data
4341 */
Nicholas Bellinger49a47f22014-01-14 20:38:58 -08004342int qlt_lport_register(void *target_lport_ptr, u64 phys_wwpn,
4343 u64 npiv_wwpn, u64 npiv_wwnn,
4344 int (*callback)(struct scsi_qla_host *, void *, u64, u64))
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004345{
4346 struct qla_tgt *tgt;
4347 struct scsi_qla_host *vha;
4348 struct qla_hw_data *ha;
4349 struct Scsi_Host *host;
4350 unsigned long flags;
4351 int rc;
4352 u8 b[WWN_SIZE];
4353
4354 mutex_lock(&qla_tgt_mutex);
4355 list_for_each_entry(tgt, &qla_tgt_glist, tgt_list_entry) {
4356 vha = tgt->vha;
4357 ha = vha->hw;
4358
4359 host = vha->host;
4360 if (!host)
4361 continue;
4362
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004363 if (!(host->hostt->supported_mode & MODE_TARGET))
4364 continue;
4365
4366 spin_lock_irqsave(&ha->hardware_lock, flags);
Nicholas Bellinger49a47f22014-01-14 20:38:58 -08004367 if ((!npiv_wwpn || !npiv_wwnn) && host->active_mode & MODE_TARGET) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004368 pr_debug("MODE_TARGET already active on qla2xxx(%d)\n",
4369 host->host_no);
4370 spin_unlock_irqrestore(&ha->hardware_lock, flags);
4371 continue;
4372 }
Nicholas Bellingerddb95142014-02-19 17:51:25 -08004373 if (tgt->tgt_stop) {
4374 pr_debug("MODE_TARGET in shutdown on qla2xxx(%d)\n",
4375 host->host_no);
4376 spin_unlock_irqrestore(&ha->hardware_lock, flags);
4377 continue;
4378 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004379 spin_unlock_irqrestore(&ha->hardware_lock, flags);
4380
4381 if (!scsi_host_get(host)) {
4382 ql_dbg(ql_dbg_tgt, vha, 0xe068,
4383 "Unable to scsi_host_get() for"
4384 " qla2xxx scsi_host\n");
4385 continue;
4386 }
Nicholas Bellinger49a47f22014-01-14 20:38:58 -08004387 qlt_lport_dump(vha, phys_wwpn, b);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004388
4389 if (memcmp(vha->port_name, b, WWN_SIZE)) {
4390 scsi_host_put(host);
4391 continue;
4392 }
Nicholas Bellinger49a47f22014-01-14 20:38:58 -08004393 rc = (*callback)(vha, target_lport_ptr, npiv_wwpn, npiv_wwnn);
4394 if (rc != 0)
4395 scsi_host_put(host);
4396
Nicholas Bellingerddb95142014-02-19 17:51:25 -08004397 mutex_unlock(&qla_tgt_mutex);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004398 return rc;
4399 }
4400 mutex_unlock(&qla_tgt_mutex);
4401
4402 return -ENODEV;
4403}
4404EXPORT_SYMBOL(qlt_lport_register);
4405
4406/**
4407 * qla_tgt_lport_deregister - Degister lport
4408 *
4409 * @vha: Registered scsi_qla_host pointer
4410 */
4411void qlt_lport_deregister(struct scsi_qla_host *vha)
4412{
4413 struct qla_hw_data *ha = vha->hw;
4414 struct Scsi_Host *sh = vha->host;
4415 /*
4416 * Clear the target_lport_ptr qla_target_template pointer in qla_hw_data
4417 */
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08004418 vha->vha_tgt.target_lport_ptr = NULL;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004419 ha->tgt.tgt_ops = NULL;
4420 /*
4421 * Release the Scsi_Host reference for the underlying qla2xxx host
4422 */
4423 scsi_host_put(sh);
4424}
4425EXPORT_SYMBOL(qlt_lport_deregister);
4426
4427/* Must be called under HW lock */
4428void qlt_set_mode(struct scsi_qla_host *vha)
4429{
4430 struct qla_hw_data *ha = vha->hw;
4431
4432 switch (ql2x_ini_mode) {
4433 case QLA2XXX_INI_MODE_DISABLED:
4434 case QLA2XXX_INI_MODE_EXCLUSIVE:
4435 vha->host->active_mode = MODE_TARGET;
4436 break;
4437 case QLA2XXX_INI_MODE_ENABLED:
4438 vha->host->active_mode |= MODE_TARGET;
4439 break;
4440 default:
4441 break;
4442 }
4443
4444 if (ha->tgt.ini_mode_force_reverse)
4445 qla_reverse_ini_mode(vha);
4446}
4447
4448/* Must be called under HW lock */
4449void qlt_clear_mode(struct scsi_qla_host *vha)
4450{
4451 struct qla_hw_data *ha = vha->hw;
4452
4453 switch (ql2x_ini_mode) {
4454 case QLA2XXX_INI_MODE_DISABLED:
4455 vha->host->active_mode = MODE_UNKNOWN;
4456 break;
4457 case QLA2XXX_INI_MODE_EXCLUSIVE:
4458 vha->host->active_mode = MODE_INITIATOR;
4459 break;
4460 case QLA2XXX_INI_MODE_ENABLED:
4461 vha->host->active_mode &= ~MODE_TARGET;
4462 break;
4463 default:
4464 break;
4465 }
4466
4467 if (ha->tgt.ini_mode_force_reverse)
4468 qla_reverse_ini_mode(vha);
4469}
4470
4471/*
4472 * qla_tgt_enable_vha - NO LOCK HELD
4473 *
4474 * host_reset, bring up w/ Target Mode Enabled
4475 */
4476void
4477qlt_enable_vha(struct scsi_qla_host *vha)
4478{
4479 struct qla_hw_data *ha = vha->hw;
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08004480 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004481 unsigned long flags;
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08004482 scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004483
4484 if (!tgt) {
4485 ql_dbg(ql_dbg_tgt, vha, 0xe069,
4486 "Unable to locate qla_tgt pointer from"
4487 " struct qla_hw_data\n");
4488 dump_stack();
4489 return;
4490 }
4491
4492 spin_lock_irqsave(&ha->hardware_lock, flags);
4493 tgt->tgt_stopped = 0;
4494 qlt_set_mode(vha);
4495 spin_unlock_irqrestore(&ha->hardware_lock, flags);
4496
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08004497 if (vha->vp_idx) {
4498 qla24xx_disable_vp(vha);
4499 qla24xx_enable_vp(vha);
4500 } else {
4501 set_bit(ISP_ABORT_NEEDED, &base_vha->dpc_flags);
4502 qla2xxx_wake_dpc(base_vha);
4503 qla2x00_wait_for_hba_online(base_vha);
4504 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004505}
4506EXPORT_SYMBOL(qlt_enable_vha);
4507
4508/*
4509 * qla_tgt_disable_vha - NO LOCK HELD
4510 *
4511 * Disable Target Mode and reset the adapter
4512 */
4513void
4514qlt_disable_vha(struct scsi_qla_host *vha)
4515{
4516 struct qla_hw_data *ha = vha->hw;
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08004517 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004518 unsigned long flags;
4519
4520 if (!tgt) {
4521 ql_dbg(ql_dbg_tgt, vha, 0xe06a,
4522 "Unable to locate qla_tgt pointer from"
4523 " struct qla_hw_data\n");
4524 dump_stack();
4525 return;
4526 }
4527
4528 spin_lock_irqsave(&ha->hardware_lock, flags);
4529 qlt_clear_mode(vha);
4530 spin_unlock_irqrestore(&ha->hardware_lock, flags);
4531
4532 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
4533 qla2xxx_wake_dpc(vha);
4534 qla2x00_wait_for_hba_online(vha);
4535}
4536
4537/*
4538 * Called from qla_init.c:qla24xx_vport_create() contex to setup
4539 * the target mode specific struct scsi_qla_host and struct qla_hw_data
4540 * members.
4541 */
4542void
4543qlt_vport_create(struct scsi_qla_host *vha, struct qla_hw_data *ha)
4544{
4545 if (!qla_tgt_mode_enabled(vha))
4546 return;
4547
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08004548 vha->vha_tgt.qla_tgt = NULL;
4549
4550 mutex_init(&vha->vha_tgt.tgt_mutex);
4551 mutex_init(&vha->vha_tgt.tgt_host_action_mutex);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004552
4553 qlt_clear_mode(vha);
4554
4555 /*
4556 * NOTE: Currently the value is kept the same for <24xx and
4557 * >=24xx ISPs. If it is necessary to change it,
4558 * the check should be added for specific ISPs,
4559 * assigning the value appropriately.
4560 */
4561 ha->tgt.atio_q_length = ATIO_ENTRY_CNT_24XX;
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08004562
4563 qlt_add_target(ha, vha);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004564}
4565
4566void
4567qlt_rff_id(struct scsi_qla_host *vha, struct ct_sns_req *ct_req)
4568{
4569 /*
4570 * FC-4 Feature bit 0 indicates target functionality to the name server.
4571 */
4572 if (qla_tgt_mode_enabled(vha)) {
4573 if (qla_ini_mode_enabled(vha))
4574 ct_req->req.rff_id.fc4_feature = BIT_0 | BIT_1;
4575 else
4576 ct_req->req.rff_id.fc4_feature = BIT_0;
4577 } else if (qla_ini_mode_enabled(vha)) {
4578 ct_req->req.rff_id.fc4_feature = BIT_1;
4579 }
4580}
4581
4582/*
4583 * qlt_init_atio_q_entries() - Initializes ATIO queue entries.
4584 * @ha: HA context
4585 *
4586 * Beginning of ATIO ring has initialization control block already built
4587 * by nvram config routine.
4588 *
4589 * Returns 0 on success.
4590 */
4591void
4592qlt_init_atio_q_entries(struct scsi_qla_host *vha)
4593{
4594 struct qla_hw_data *ha = vha->hw;
4595 uint16_t cnt;
4596 struct atio_from_isp *pkt = (struct atio_from_isp *)ha->tgt.atio_ring;
4597
4598 if (!qla_tgt_mode_enabled(vha))
4599 return;
4600
4601 for (cnt = 0; cnt < ha->tgt.atio_q_length; cnt++) {
4602 pkt->u.raw.signature = ATIO_PROCESSED;
4603 pkt++;
4604 }
4605
4606}
4607
4608/*
4609 * qlt_24xx_process_atio_queue() - Process ATIO queue entries.
4610 * @ha: SCSI driver HA context
4611 */
4612void
4613qlt_24xx_process_atio_queue(struct scsi_qla_host *vha)
4614{
4615 struct qla_hw_data *ha = vha->hw;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004616 struct atio_from_isp *pkt;
4617 int cnt, i;
4618
4619 if (!vha->flags.online)
4620 return;
4621
4622 while (ha->tgt.atio_ring_ptr->signature != ATIO_PROCESSED) {
4623 pkt = (struct atio_from_isp *)ha->tgt.atio_ring_ptr;
4624 cnt = pkt->u.raw.entry_count;
4625
4626 qlt_24xx_atio_pkt_all_vps(vha, (struct atio_from_isp *)pkt);
4627
4628 for (i = 0; i < cnt; i++) {
4629 ha->tgt.atio_ring_index++;
4630 if (ha->tgt.atio_ring_index == ha->tgt.atio_q_length) {
4631 ha->tgt.atio_ring_index = 0;
4632 ha->tgt.atio_ring_ptr = ha->tgt.atio_ring;
4633 } else
4634 ha->tgt.atio_ring_ptr++;
4635
4636 pkt->u.raw.signature = ATIO_PROCESSED;
4637 pkt = (struct atio_from_isp *)ha->tgt.atio_ring_ptr;
4638 }
4639 wmb();
4640 }
4641
4642 /* Adjust ring index */
Arun Easiaa230bc2013-01-30 03:34:39 -05004643 WRT_REG_DWORD(ISP_ATIO_Q_OUT(vha), ha->tgt.atio_ring_index);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004644}
4645
4646void
Arun Easiaa230bc2013-01-30 03:34:39 -05004647qlt_24xx_config_rings(struct scsi_qla_host *vha)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004648{
4649 struct qla_hw_data *ha = vha->hw;
Arun Easiaa230bc2013-01-30 03:34:39 -05004650 if (!QLA_TGT_MODE_ENABLED())
4651 return;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004652
Arun Easiaa230bc2013-01-30 03:34:39 -05004653 WRT_REG_DWORD(ISP_ATIO_Q_IN(vha), 0);
4654 WRT_REG_DWORD(ISP_ATIO_Q_OUT(vha), 0);
4655 RD_REG_DWORD(ISP_ATIO_Q_OUT(vha));
4656
4657 if (IS_ATIO_MSIX_CAPABLE(ha)) {
4658 struct qla_msix_entry *msix = &ha->msix_entries[2];
4659 struct init_cb_24xx *icb = (struct init_cb_24xx *)ha->init_cb;
4660
4661 icb->msix_atio = cpu_to_le16(msix->entry);
4662 ql_dbg(ql_dbg_init, vha, 0xf072,
4663 "Registering ICB vector 0x%x for atio que.\n",
4664 msix->entry);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004665 }
4666}
4667
4668void
4669qlt_24xx_config_nvram_stage1(struct scsi_qla_host *vha, struct nvram_24xx *nv)
4670{
4671 struct qla_hw_data *ha = vha->hw;
4672
4673 if (qla_tgt_mode_enabled(vha)) {
4674 if (!ha->tgt.saved_set) {
4675 /* We save only once */
4676 ha->tgt.saved_exchange_count = nv->exchange_count;
4677 ha->tgt.saved_firmware_options_1 =
4678 nv->firmware_options_1;
4679 ha->tgt.saved_firmware_options_2 =
4680 nv->firmware_options_2;
4681 ha->tgt.saved_firmware_options_3 =
4682 nv->firmware_options_3;
4683 ha->tgt.saved_set = 1;
4684 }
4685
4686 nv->exchange_count = __constant_cpu_to_le16(0xFFFF);
4687
4688 /* Enable target mode */
4689 nv->firmware_options_1 |= __constant_cpu_to_le32(BIT_4);
4690
4691 /* Disable ini mode, if requested */
4692 if (!qla_ini_mode_enabled(vha))
4693 nv->firmware_options_1 |= __constant_cpu_to_le32(BIT_5);
4694
4695 /* Disable Full Login after LIP */
4696 nv->firmware_options_1 &= __constant_cpu_to_le32(~BIT_13);
4697 /* Enable initial LIP */
4698 nv->firmware_options_1 &= __constant_cpu_to_le32(~BIT_9);
4699 /* Enable FC tapes support */
4700 nv->firmware_options_2 |= __constant_cpu_to_le32(BIT_12);
4701 /* Disable Full Login after LIP */
4702 nv->host_p &= __constant_cpu_to_le32(~BIT_10);
4703 /* Enable target PRLI control */
4704 nv->firmware_options_2 |= __constant_cpu_to_le32(BIT_14);
4705 } else {
4706 if (ha->tgt.saved_set) {
4707 nv->exchange_count = ha->tgt.saved_exchange_count;
4708 nv->firmware_options_1 =
4709 ha->tgt.saved_firmware_options_1;
4710 nv->firmware_options_2 =
4711 ha->tgt.saved_firmware_options_2;
4712 nv->firmware_options_3 =
4713 ha->tgt.saved_firmware_options_3;
4714 }
4715 return;
4716 }
4717
4718 /* out-of-order frames reassembly */
4719 nv->firmware_options_3 |= BIT_6|BIT_9;
4720
4721 if (ha->tgt.enable_class_2) {
4722 if (vha->flags.init_done)
4723 fc_host_supported_classes(vha->host) =
4724 FC_COS_CLASS2 | FC_COS_CLASS3;
4725
4726 nv->firmware_options_2 |= __constant_cpu_to_le32(BIT_8);
4727 } else {
4728 if (vha->flags.init_done)
4729 fc_host_supported_classes(vha->host) = FC_COS_CLASS3;
4730
4731 nv->firmware_options_2 &= ~__constant_cpu_to_le32(BIT_8);
4732 }
4733}
4734
4735void
4736qlt_24xx_config_nvram_stage2(struct scsi_qla_host *vha,
4737 struct init_cb_24xx *icb)
4738{
4739 struct qla_hw_data *ha = vha->hw;
4740
4741 if (ha->tgt.node_name_set) {
4742 memcpy(icb->node_name, ha->tgt.tgt_node_name, WWN_SIZE);
4743 icb->firmware_options_1 |= __constant_cpu_to_le32(BIT_14);
4744 }
4745}
4746
Arun Easiaa230bc2013-01-30 03:34:39 -05004747void
4748qlt_81xx_config_nvram_stage1(struct scsi_qla_host *vha, struct nvram_81xx *nv)
4749{
4750 struct qla_hw_data *ha = vha->hw;
4751
4752 if (!QLA_TGT_MODE_ENABLED())
4753 return;
4754
4755 if (qla_tgt_mode_enabled(vha)) {
4756 if (!ha->tgt.saved_set) {
4757 /* We save only once */
4758 ha->tgt.saved_exchange_count = nv->exchange_count;
4759 ha->tgt.saved_firmware_options_1 =
4760 nv->firmware_options_1;
4761 ha->tgt.saved_firmware_options_2 =
4762 nv->firmware_options_2;
4763 ha->tgt.saved_firmware_options_3 =
4764 nv->firmware_options_3;
4765 ha->tgt.saved_set = 1;
4766 }
4767
4768 nv->exchange_count = __constant_cpu_to_le16(0xFFFF);
4769
4770 /* Enable target mode */
4771 nv->firmware_options_1 |= __constant_cpu_to_le32(BIT_4);
4772
4773 /* Disable ini mode, if requested */
4774 if (!qla_ini_mode_enabled(vha))
4775 nv->firmware_options_1 |=
4776 __constant_cpu_to_le32(BIT_5);
4777
4778 /* Disable Full Login after LIP */
4779 nv->firmware_options_1 &= __constant_cpu_to_le32(~BIT_13);
4780 /* Enable initial LIP */
4781 nv->firmware_options_1 &= __constant_cpu_to_le32(~BIT_9);
4782 /* Enable FC tapes support */
4783 nv->firmware_options_2 |= __constant_cpu_to_le32(BIT_12);
4784 /* Disable Full Login after LIP */
4785 nv->host_p &= __constant_cpu_to_le32(~BIT_10);
4786 /* Enable target PRLI control */
4787 nv->firmware_options_2 |= __constant_cpu_to_le32(BIT_14);
4788 } else {
4789 if (ha->tgt.saved_set) {
4790 nv->exchange_count = ha->tgt.saved_exchange_count;
4791 nv->firmware_options_1 =
4792 ha->tgt.saved_firmware_options_1;
4793 nv->firmware_options_2 =
4794 ha->tgt.saved_firmware_options_2;
4795 nv->firmware_options_3 =
4796 ha->tgt.saved_firmware_options_3;
4797 }
4798 return;
4799 }
4800
4801 /* out-of-order frames reassembly */
4802 nv->firmware_options_3 |= BIT_6|BIT_9;
4803
4804 if (ha->tgt.enable_class_2) {
4805 if (vha->flags.init_done)
4806 fc_host_supported_classes(vha->host) =
4807 FC_COS_CLASS2 | FC_COS_CLASS3;
4808
4809 nv->firmware_options_2 |= __constant_cpu_to_le32(BIT_8);
4810 } else {
4811 if (vha->flags.init_done)
4812 fc_host_supported_classes(vha->host) = FC_COS_CLASS3;
4813
4814 nv->firmware_options_2 &= ~__constant_cpu_to_le32(BIT_8);
4815 }
4816}
4817
4818void
4819qlt_81xx_config_nvram_stage2(struct scsi_qla_host *vha,
4820 struct init_cb_81xx *icb)
4821{
4822 struct qla_hw_data *ha = vha->hw;
4823
4824 if (!QLA_TGT_MODE_ENABLED())
4825 return;
4826
4827 if (ha->tgt.node_name_set) {
4828 memcpy(icb->node_name, ha->tgt.tgt_node_name, WWN_SIZE);
4829 icb->firmware_options_1 |= __constant_cpu_to_le32(BIT_14);
4830 }
4831}
4832
4833void
4834qlt_83xx_iospace_config(struct qla_hw_data *ha)
4835{
4836 if (!QLA_TGT_MODE_ENABLED())
4837 return;
4838
4839 ha->msix_count += 1; /* For ATIO Q */
4840}
4841
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004842int
4843qlt_24xx_process_response_error(struct scsi_qla_host *vha,
4844 struct sts_entry_24xx *pkt)
4845{
4846 switch (pkt->entry_type) {
4847 case ABTS_RECV_24XX:
4848 case ABTS_RESP_24XX:
4849 case CTIO_TYPE7:
4850 case NOTIFY_ACK_TYPE:
4851 return 1;
4852 default:
4853 return 0;
4854 }
4855}
4856
4857void
4858qlt_modify_vp_config(struct scsi_qla_host *vha,
4859 struct vp_config_entry_24xx *vpmod)
4860{
4861 if (qla_tgt_mode_enabled(vha))
4862 vpmod->options_idx1 &= ~BIT_5;
4863 /* Disable ini mode, if requested */
4864 if (!qla_ini_mode_enabled(vha))
4865 vpmod->options_idx1 &= ~BIT_4;
4866}
4867
4868void
4869qlt_probe_one_stage1(struct scsi_qla_host *base_vha, struct qla_hw_data *ha)
4870{
4871 if (!QLA_TGT_MODE_ENABLED())
4872 return;
4873
Arun Easiaa230bc2013-01-30 03:34:39 -05004874 if (ha->mqenable || IS_QLA83XX(ha)) {
4875 ISP_ATIO_Q_IN(base_vha) = &ha->mqiobase->isp25mq.atio_q_in;
4876 ISP_ATIO_Q_OUT(base_vha) = &ha->mqiobase->isp25mq.atio_q_out;
4877 } else {
4878 ISP_ATIO_Q_IN(base_vha) = &ha->iobase->isp24.atio_q_in;
4879 ISP_ATIO_Q_OUT(base_vha) = &ha->iobase->isp24.atio_q_out;
4880 }
4881
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08004882 mutex_init(&base_vha->vha_tgt.tgt_mutex);
4883 mutex_init(&base_vha->vha_tgt.tgt_host_action_mutex);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004884 qlt_clear_mode(base_vha);
4885}
4886
Arun Easiaa230bc2013-01-30 03:34:39 -05004887irqreturn_t
4888qla83xx_msix_atio_q(int irq, void *dev_id)
4889{
4890 struct rsp_que *rsp;
4891 scsi_qla_host_t *vha;
4892 struct qla_hw_data *ha;
4893 unsigned long flags;
4894
4895 rsp = (struct rsp_que *) dev_id;
4896 ha = rsp->hw;
4897 vha = pci_get_drvdata(ha->pdev);
4898
4899 spin_lock_irqsave(&ha->hardware_lock, flags);
4900
4901 qlt_24xx_process_atio_queue(vha);
4902 qla24xx_process_response_queue(vha, rsp);
4903
4904 spin_unlock_irqrestore(&ha->hardware_lock, flags);
4905
4906 return IRQ_HANDLED;
4907}
4908
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004909int
4910qlt_mem_alloc(struct qla_hw_data *ha)
4911{
4912 if (!QLA_TGT_MODE_ENABLED())
4913 return 0;
4914
4915 ha->tgt.tgt_vp_map = kzalloc(sizeof(struct qla_tgt_vp_map) *
4916 MAX_MULTI_ID_FABRIC, GFP_KERNEL);
4917 if (!ha->tgt.tgt_vp_map)
4918 return -ENOMEM;
4919
4920 ha->tgt.atio_ring = dma_alloc_coherent(&ha->pdev->dev,
4921 (ha->tgt.atio_q_length + 1) * sizeof(struct atio_from_isp),
4922 &ha->tgt.atio_dma, GFP_KERNEL);
4923 if (!ha->tgt.atio_ring) {
4924 kfree(ha->tgt.tgt_vp_map);
4925 return -ENOMEM;
4926 }
4927 return 0;
4928}
4929
4930void
4931qlt_mem_free(struct qla_hw_data *ha)
4932{
4933 if (!QLA_TGT_MODE_ENABLED())
4934 return;
4935
4936 if (ha->tgt.atio_ring) {
4937 dma_free_coherent(&ha->pdev->dev, (ha->tgt.atio_q_length + 1) *
4938 sizeof(struct atio_from_isp), ha->tgt.atio_ring,
4939 ha->tgt.atio_dma);
4940 }
4941 kfree(ha->tgt.tgt_vp_map);
4942}
4943
4944/* vport_slock to be held by the caller */
4945void
4946qlt_update_vp_map(struct scsi_qla_host *vha, int cmd)
4947{
4948 if (!QLA_TGT_MODE_ENABLED())
4949 return;
4950
4951 switch (cmd) {
4952 case SET_VP_IDX:
4953 vha->hw->tgt.tgt_vp_map[vha->vp_idx].vha = vha;
4954 break;
4955 case SET_AL_PA:
4956 vha->hw->tgt.tgt_vp_map[vha->d_id.b.al_pa].idx = vha->vp_idx;
4957 break;
4958 case RESET_VP_IDX:
4959 vha->hw->tgt.tgt_vp_map[vha->vp_idx].vha = NULL;
4960 break;
4961 case RESET_AL_PA:
4962 vha->hw->tgt.tgt_vp_map[vha->d_id.b.al_pa].idx = 0;
4963 break;
4964 }
4965}
4966
4967static int __init qlt_parse_ini_mode(void)
4968{
4969 if (strcasecmp(qlini_mode, QLA2XXX_INI_MODE_STR_EXCLUSIVE) == 0)
4970 ql2x_ini_mode = QLA2XXX_INI_MODE_EXCLUSIVE;
4971 else if (strcasecmp(qlini_mode, QLA2XXX_INI_MODE_STR_DISABLED) == 0)
4972 ql2x_ini_mode = QLA2XXX_INI_MODE_DISABLED;
4973 else if (strcasecmp(qlini_mode, QLA2XXX_INI_MODE_STR_ENABLED) == 0)
4974 ql2x_ini_mode = QLA2XXX_INI_MODE_ENABLED;
4975 else
4976 return false;
4977
4978 return true;
4979}
4980
4981int __init qlt_init(void)
4982{
4983 int ret;
4984
4985 if (!qlt_parse_ini_mode()) {
4986 ql_log(ql_log_fatal, NULL, 0xe06b,
4987 "qlt_parse_ini_mode() failed\n");
4988 return -EINVAL;
4989 }
4990
4991 if (!QLA_TGT_MODE_ENABLED())
4992 return 0;
4993
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004994 qla_tgt_mgmt_cmd_cachep = kmem_cache_create("qla_tgt_mgmt_cmd_cachep",
4995 sizeof(struct qla_tgt_mgmt_cmd), __alignof__(struct
4996 qla_tgt_mgmt_cmd), 0, NULL);
4997 if (!qla_tgt_mgmt_cmd_cachep) {
4998 ql_log(ql_log_fatal, NULL, 0xe06d,
4999 "kmem_cache_create for qla_tgt_mgmt_cmd_cachep failed\n");
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07005000 return -ENOMEM;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005001 }
5002
5003 qla_tgt_mgmt_cmd_mempool = mempool_create(25, mempool_alloc_slab,
5004 mempool_free_slab, qla_tgt_mgmt_cmd_cachep);
5005 if (!qla_tgt_mgmt_cmd_mempool) {
5006 ql_log(ql_log_fatal, NULL, 0xe06e,
5007 "mempool_create for qla_tgt_mgmt_cmd_mempool failed\n");
5008 ret = -ENOMEM;
5009 goto out_mgmt_cmd_cachep;
5010 }
5011
5012 qla_tgt_wq = alloc_workqueue("qla_tgt_wq", 0, 0);
5013 if (!qla_tgt_wq) {
5014 ql_log(ql_log_fatal, NULL, 0xe06f,
5015 "alloc_workqueue for qla_tgt_wq failed\n");
5016 ret = -ENOMEM;
5017 goto out_cmd_mempool;
5018 }
5019 /*
5020 * Return 1 to signal that initiator-mode is being disabled
5021 */
5022 return (ql2x_ini_mode == QLA2XXX_INI_MODE_DISABLED) ? 1 : 0;
5023
5024out_cmd_mempool:
5025 mempool_destroy(qla_tgt_mgmt_cmd_mempool);
5026out_mgmt_cmd_cachep:
5027 kmem_cache_destroy(qla_tgt_mgmt_cmd_cachep);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005028 return ret;
5029}
5030
5031void qlt_exit(void)
5032{
5033 if (!QLA_TGT_MODE_ENABLED())
5034 return;
5035
5036 destroy_workqueue(qla_tgt_wq);
5037 mempool_destroy(qla_tgt_mgmt_cmd_mempool);
5038 kmem_cache_destroy(qla_tgt_mgmt_cmd_cachep);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005039}