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