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