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