]> git.openfabrics.org - ~shefty/rdma-dev.git/blob - drivers/scsi/qla2xxx/qla_target.c
iscsi-target: Bump defaults for nopin_timeout + nopin_response_timeout values
[~shefty/rdma-dev.git] / drivers / scsi / qla2xxx / qla_target.c
1 /*
2  *  qla_target.c SCSI LLD infrastructure for QLogic 22xx/23xx/24xx/25xx
3  *
4  *  based on qla2x00t.c code:
5  *
6  *  Copyright (C) 2004 - 2010 Vladislav Bolkhovitin <vst@vlnb.net>
7  *  Copyright (C) 2004 - 2005 Leonid Stoljar
8  *  Copyright (C) 2006 Nathaniel Clark <nate@misrule.us>
9  *  Copyright (C) 2006 - 2010 ID7 Ltd.
10  *
11  *  Forward port and refactoring to modern qla2xxx and target/configfs
12  *
13  *  Copyright (C) 2010-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>
29 #include <linux/blkdev.h>
30 #include <linux/interrupt.h>
31 #include <linux/pci.h>
32 #include <linux/delay.h>
33 #include <linux/list.h>
34 #include <linux/workqueue.h>
35 #include <asm/unaligned.h>
36 #include <scsi/scsi.h>
37 #include <scsi/scsi_host.h>
38 #include <scsi/scsi_tcq.h>
39 #include <target/target_core_base.h>
40 #include <target/target_core_fabric.h>
41
42 #include "qla_def.h"
43 #include "qla_target.h"
44
45 static char *qlini_mode = QLA2XXX_INI_MODE_STR_ENABLED;
46 module_param(qlini_mode, charp, S_IRUGO);
47 MODULE_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
55 static int ql2x_ini_mode = QLA2XXX_INI_MODE_EXCLUSIVE;
56
57 /*
58  * From scsi/fc/fc_fcp.h
59  */
60 enum 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 */
95 static void qlt_24xx_atio_pkt(struct scsi_qla_host *ha,
96         struct atio_from_isp *pkt);
97 static void qlt_response_pkt(struct scsi_qla_host *ha, response_t *pkt);
98 static int qlt_issue_task_mgmt(struct qla_tgt_sess *sess, uint32_t lun,
99         int fn, void *iocb, int flags);
100 static void qlt_send_term_exchange(struct scsi_qla_host *ha, struct qla_tgt_cmd
101         *cmd, struct atio_from_isp *atio, int ha_locked);
102 static 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  */
107 static struct kmem_cache *qla_tgt_cmd_cachep;
108 static struct kmem_cache *qla_tgt_mgmt_cmd_cachep;
109 static mempool_t *qla_tgt_mgmt_cmd_mempool;
110 static struct workqueue_struct *qla_tgt_wq;
111 static DEFINE_MUTEX(qla_tgt_mutex);
112 static LIST_HEAD(qla_tgt_glist);
113
114 /* ha->hardware_lock supposed to be held on entry (to protect tgt->sess_list) */
115 static 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!! */
130 static 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
145 static inline
146 struct 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
166 static inline
167 struct 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
182 void 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
236 void 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
336 static 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 */
365 void 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 }
378 EXPORT_SYMBOL(qlt_unreg_sess);
379
380 /* ha->hardware_lock supposed to be held on entry */
381 static 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 */
450 static 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 */
488 static 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
498 static 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
541 out_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
547 static 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
561 retry:
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
635 out_free_fcport:
636         kfree(fcport);
637
638 out:
639         return res;
640 }
641
642 /* ha->hardware_lock supposed to be held on entry */
643 static 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
651 static 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  */
715 static 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  */
826 void 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
895 void 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
926 static 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 */
946 void 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 }
998 EXPORT_SYMBOL(qlt_stop_phase1);
999
1000 /* Called by tcm_qla2xxx configfs code */
1001 void 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 }
1032 EXPORT_SYMBOL(qlt_stop_phase2);
1033
1034 /* Called from qlt_remove_target() -> qla2x00_remove_one() */
1035 void 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 */
1051 static 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  */
1085 static 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  */
1143 static 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  */
1220 static 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 */
1266 static 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  */
1305 static 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  */
1367 static 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         ctio->u.status1.sense_data[0] = resp_code;
1407
1408         qla2x00_start_iocbs(ha, ha->req);
1409 }
1410
1411 void qlt_free_mcmd(struct qla_tgt_mgmt_cmd *mcmd)
1412 {
1413         mempool_free(mcmd, qla_tgt_mgmt_cmd_mempool);
1414 }
1415 EXPORT_SYMBOL(qlt_free_mcmd);
1416
1417 /* callback from target fabric module code */
1418 void 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 }
1451 EXPORT_SYMBOL(qlt_xmit_tm_rsp);
1452
1453 /* No locks */
1454 static 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
1480 out_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
1487 static 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
1497 static 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  */
1534 static 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 */
1548 static 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 */
1577 static 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  */
1626 static 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  */
1693 static 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
1752 static 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  */
1760 static 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
1852 static 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  */
1869 static 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
1896 static 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
1950 static inline void qlt_check_srr_debug(struct qla_tgt_cmd *cmd, int *xmit_type)
1951 {}
1952 #endif
1953
1954 static 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                 }
1983 skip_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  */
2024 int 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
2130 out_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 }
2137 EXPORT_SYMBOL(qlt_xmit_response);
2138
2139 int 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
2188 out_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 }
2195 EXPORT_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 */
2199 static 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
2256 static 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);
2272 done:
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
2281 void 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 }
2289 EXPORT_SYMBOL(qlt_free_cmd);
2290
2291 /* ha->hardware_lock supposed to be held on entry */
2292 static 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  */
2378 static 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 */
2397 static 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 */
2412 static 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  */
2456 static 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);
2479         if (cmd == NULL)
2480                 return;
2481
2482         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 */
2573 void 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
2590 static 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
2622 static 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  */
2627 static 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);
2646         /* Do kref_get() before dropping qla_hw_data->hardware_lock. */
2647         if (sess)
2648                 kref_get(&sess->se_sess->sess_kref);
2649         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
2714 out_term:
2715         ql_dbg(ql_dbg_tgt_mgt, vha, 0xf020, "Terminating work cmd %p", cmd);
2716         /*
2717          * 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.
2719          */
2720         spin_lock_irqsave(&ha->hardware_lock, flags);
2721         qlt_send_term_exchange(vha, NULL, &cmd->atio, 1);
2722         kmem_cache_free(qla_tgt_cmd_cachep, cmd);
2723         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 */
2729 static 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 */
2763 static 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 */
2870 static 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 */
2900 static 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 */
2939 static 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  */
2963 static 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
3007 static 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
3109 static 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 */
3134 static 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 ||&nbs