- Implemented abort on timeout of stuck in the firmware commands
[mirror/scst/.git] / qla2x00t / qla2x00-target / qla2x00t.c
1 /*
2  *  qla2x00t.c
3  *
4  *  Copyright (C) 2004 - 2009 Vladislav Bolkhovitin <vst@vlnb.net>
5  *  Copyright (C) 2004 - 2005 Leonid Stoljar
6  *  Copyright (C) 2006 Nathaniel Clark <nate@misrule.us>
7  *
8  *  QLogic 2x00 SCSI target driver.
9  *
10  *  This program is free software; you can redistribute it and/or
11  *  modify it under the terms of the GNU General Public License
12  *  as published by the Free Software Foundation, version 2
13  *  of the License.
14  *
15  *  This program is distributed in the hope that it will be useful,
16  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  *  GNU General Public License for more details.
19  */
20
21 #include <linux/module.h>
22 #include <linux/init.h>
23 #include <linux/types.h>
24 #include <linux/version.h>
25 #include <linux/blkdev.h>
26 #include <linux/interrupt.h>
27 #include <scsi/scsi.h>
28 #include <scsi/scsi_host.h>
29 #include <linux/pci.h>
30 #include <linux/delay.h>
31 #include <linux/seq_file.h>
32 #include <linux/list.h>
33
34 #include <scst.h>
35
36 #include "qla2x00t.h"
37
38 #if !defined(CONFIG_SCSI_QLA2XXX_TARGET)
39 #error "CONFIG_SCSI_QLA2XXX_TARGET is NOT DEFINED"
40 #endif
41
42 #ifdef CONFIG_SCST_DEBUG
43 #define Q2T_DEFAULT_LOG_FLAGS (TRACE_FUNCTION | TRACE_PID | \
44         TRACE_OUT_OF_MEM | TRACE_MGMT | TRACE_MGMT_MINOR | \
45         TRACE_MGMT_DEBUG | TRACE_MINOR | TRACE_SPECIAL)
46 #else
47 # ifdef CONFIG_SCST_TRACING
48 #define Q2T_DEFAULT_LOG_FLAGS (TRACE_OUT_OF_MEM | TRACE_MGMT | TRACE_MINOR | \
49         TRACE_SPECIAL)
50 # endif
51 #endif
52
53 static int q2t_target_detect(struct scst_tgt_template *templ);
54 static int q2t_target_release(struct scst_tgt *scst_tgt);
55 static int q2t_xmit_response(struct scst_cmd *scst_cmd);
56 static int q2t_rdy_to_xfer(struct scst_cmd *scst_cmd);
57 static void q2t_on_free_cmd(struct scst_cmd *scst_cmd);
58 static void q2t_task_mgmt_fn_done(struct scst_mgmt_cmd *mcmd);
59
60 /* Predefs for callbacks handed to qla2xxx(target) */
61 static void q2t_response_pkt(scsi_qla_host_t *ha, sts_entry_t *pkt);
62 static void q2t_async_event(uint16_t code, scsi_qla_host_t *ha,
63         uint16_t *mailbox);
64 static void q2t_ctio_completion(scsi_qla_host_t *ha, uint32_t handle);
65 static void q2t_host_action(scsi_qla_host_t *ha,
66         enum qla2x_tgt_host_action action);
67 static void q2t_send_term_exchange(scsi_qla_host_t *ha, struct q2t_cmd *cmd,
68         struct atio_entry *atio, int ha_locked);
69 static void q2t_on_hw_pending_cmd_timeout(struct scst_cmd *scst_cmd);
70
71 /*
72  * Global Variables
73  */
74
75 #if defined(CONFIG_SCST_DEBUG) || defined(CONFIG_SCST_TRACING)
76 #define trace_flag q2t_trace_flag
77 static unsigned long q2t_trace_flag = Q2T_DEFAULT_LOG_FLAGS;
78 #endif
79
80 static struct scst_tgt_template tgt_template = {
81         .name                   = "qla2x00tgt",
82         .sg_tablesize           = 0,
83         .use_clustering         = 1,
84 #ifdef CONFIG_QLA_TGT_DEBUG_WORK_IN_THREAD
85         .xmit_response_atomic   = 0,
86         .rdy_to_xfer_atomic     = 0,
87 #else
88         .xmit_response_atomic   = 1,
89         .rdy_to_xfer_atomic     = 1,
90 #endif
91         .max_hw_pending_time    = Q2T_MAX_HW_PENDING_TIME,
92         .detect                 = q2t_target_detect,
93         .release                = q2t_target_release,
94         .xmit_response          = q2t_xmit_response,
95         .rdy_to_xfer            = q2t_rdy_to_xfer,
96         .on_free_cmd            = q2t_on_free_cmd,
97         .task_mgmt_fn_done      = q2t_task_mgmt_fn_done,
98         .on_hw_pending_cmd_timeout = q2t_on_hw_pending_cmd_timeout,
99 };
100
101 static struct kmem_cache *q2t_cmd_cachep;
102 static struct qla2x_tgt_target tgt_data;
103 static DEFINE_MUTEX(qla_mgmt_mutex);
104
105 /*
106  * Functions
107  */
108
109 static inline int test_tgt_sess_count(struct q2t_tgt *tgt, scsi_qla_host_t *ha)
110 {
111         unsigned long flags;
112         int res;
113
114         /*
115          * We need to protect against race, when tgt is freed before or
116          * inside wake_up()
117          */
118         spin_lock_irqsave(&tgt->ha->hardware_lock, flags);
119         TRACE_DBG("tgt %p, empty(sess_list)=%d sess_count=%d",
120               tgt, list_empty(&tgt->sess_list), tgt->sess_count);
121         res = (tgt->sess_count == 0);
122         spin_unlock_irqrestore(&tgt->ha->hardware_lock, flags);
123
124         return res;
125 }
126
127 /* ha->hardware_lock supposed to be held on entry */
128 static inline void q2t_exec_queue(scsi_qla_host_t *ha)
129 {
130         tgt_data.isp_cmd(ha);
131 }
132
133 /* ha->hardware_lock supposed to be held on entry */
134 static void q2t_modify_command_count(scsi_qla_host_t *ha, int cmd_count,
135         int imm_count)
136 {
137         struct modify_lun_entry *pkt;
138
139         TRACE_ENTRY();
140
141         TRACE_DBG("Sending MODIFY_LUN ha %p, cmd %d, imm %d",
142                   ha, cmd_count, imm_count);
143
144         pkt = (struct modify_lun_entry *)tgt_data.req_pkt(ha);
145         ha->tgt->modify_lun_expected++;
146
147         pkt->entry_type = MODIFY_LUN_TYPE;
148         pkt->entry_count = 1;
149         if (cmd_count < 0) {
150                 /* Subtract from command count */
151                 pkt->operators = MODIFY_LUN_CMD_SUB;
152                 pkt->command_count = -cmd_count;
153         } else if (cmd_count > 0) {
154                 /* Add to command count */
155                 pkt->operators = MODIFY_LUN_CMD_ADD;
156                 pkt->command_count = cmd_count;
157         }
158
159         if (imm_count < 0) {
160                 pkt->operators |= MODIFY_LUN_IMM_SUB;
161                 pkt->immed_notify_count = -imm_count;
162         } else if (imm_count > 0) {
163                 pkt->operators |= MODIFY_LUN_IMM_ADD;
164                 pkt->immed_notify_count = imm_count;
165         }
166
167         pkt->timeout = 0;       /* Use default */
168         q2t_exec_queue(ha);
169
170         TRACE_EXIT();
171         return;
172 }
173
174 /* ha->hardware_lock supposed to be held on entry */
175 static void __q2t_send_notify_ack(scsi_qla_host_t *ha,
176          uint16_t target_id, uint16_t status, uint16_t task_flags,
177          uint16_t seq_id, uint32_t add_flags, uint16_t resp_code,
178          int resp_code_valid, uint16_t ox_id)
179 {
180         struct nack_entry *ntfy;
181
182         TRACE_ENTRY();
183
184         /* Send marker if required */
185         if (tgt_data.issue_marker(ha) != QLA_SUCCESS) {
186                 PRINT_ERROR("qla2x00tgt(%ld): __QLA2X00_MARKER() "
187                             "failed", ha->host_no);
188                 goto out;
189         }
190
191         ntfy = (struct nack_entry *)tgt_data.req_pkt(ha);
192
193         if (ha->tgt != NULL)
194                 ha->tgt->notify_ack_expected++;
195
196         memset(ntfy, 0, sizeof(*ntfy));
197         ntfy->entry_type = NOTIFY_ACK_TYPE;
198         ntfy->entry_count = 1;
199         SET_TARGET_ID(ha, ntfy->target, target_id);
200         ntfy->status = status;
201         ntfy->task_flags = task_flags;
202         ntfy->seq_id = seq_id;
203         /* Do not increment here, the chip isn't decrementing */
204         /* ntfy->flags = __constant_cpu_to_le16(NOTIFY_ACK_RES_COUNT); */
205         ntfy->flags |= cpu_to_le16(add_flags);
206         ntfy->ox_id = ox_id;
207
208         if (resp_code_valid) {
209                 ntfy->resp_code = cpu_to_le16(resp_code);
210                 ntfy->flags |=
211                         __constant_cpu_to_le16(NOTIFY_ACK_TM_RESP_CODE_VALID);
212         }
213
214         TRACE(TRACE_SCSI, "Sending Notify Ack Seq %#x -> I %#x St %#x RC %#x",
215               le16_to_cpu(seq_id), target_id, le16_to_cpu(status),
216               le16_to_cpu(ntfy->resp_code));
217
218         q2t_exec_queue(ha);
219
220 out:
221         TRACE_EXIT();
222         return;
223 }
224 /* ha->hardware_lock supposed to be held on entry */
225 static inline void q2t_send_notify_ack(scsi_qla_host_t *ha,
226         struct notify_entry *iocb, uint32_t add_flags, uint16_t resp_code,
227         int resp_code_valid)
228 {
229         __q2t_send_notify_ack(ha,  GET_TARGET_ID(ha, iocb), iocb->status,
230               iocb->task_flags, iocb->seq_id, add_flags, resp_code,
231               resp_code_valid, iocb->ox_id);
232 }
233
234 /*
235  * register with initiator driver (but target mode isn't enabled till
236  * it's turned on via sysfs)
237  */
238 static int q2t_target_detect(struct scst_tgt_template *templ)
239 {
240         int res;
241         struct qla2x_tgt_initiator itd = {
242                 .magic                  = QLA2X_TARGET_MAGIC,
243                 .tgt_response_pkt       = q2t_response_pkt,
244                 .tgt_ctio_completion    = q2t_ctio_completion,
245                 .tgt_async_event        = q2t_async_event,
246                 .tgt_host_action        = q2t_host_action,
247         };
248
249         TRACE_ENTRY();
250
251         res = qla2xxx_tgt_register_driver(&itd, &tgt_data);
252         if (res != 0) {
253                 PRINT_ERROR("Unable to register driver: %d", res);
254                 goto out;
255         }
256
257         if (tgt_data.magic != QLA2X_INITIATOR_MAGIC) {
258                 PRINT_ERROR("Wrong version of the initiator part: %d",
259                         tgt_data.magic);
260                 res = -EINVAL;
261         }
262
263 out:
264         TRACE_EXIT();
265         return res;
266 }
267
268 /* no lock held */
269 static void q2t_free_session_done(struct scst_session *scst_sess)
270 {
271         struct q2t_sess *sess;
272         struct q2t_tgt *tgt;
273         scsi_qla_host_t *ha;
274         unsigned long flags;
275
276         TRACE_ENTRY();
277
278         sBUG_ON(scst_sess == NULL);
279         sess = (struct q2t_sess *)scst_sess_get_tgt_priv(scst_sess);
280         sBUG_ON(sess == NULL);
281         tgt = sess->tgt;
282
283         kfree(sess);
284
285         if (tgt == NULL)
286                 goto out;
287
288         TRACE_MGMT_DBG("tgt %p, empty(sess_list) %d, sess_count %d",
289               tgt, list_empty(&tgt->sess_list), tgt->sess_count);
290
291         ha = tgt->ha;
292
293         /*
294          * We need to protect against race, when tgt is freed before or
295          * inside wake_up()
296          */
297         spin_lock_irqsave(&ha->hardware_lock, flags);
298         tgt->sess_count--;
299         if (tgt->sess_count == 0)
300                 wake_up_all(&tgt->waitQ);
301         spin_unlock_irqrestore(&ha->hardware_lock, flags);
302
303 out:
304         TRACE_EXIT();
305         return;
306 }
307
308 /* ha->hardware_lock supposed to be held on entry */
309 static void q2t_unreg_sess(struct q2t_sess *sess)
310 {
311         TRACE_ENTRY();
312
313         if (sess == NULL)
314                 goto out;
315
316         list_del(&sess->list);
317
318         PRINT_INFO("qla2x00tgt(%ld): session for loop_id %d deleted",
319                 sess->tgt->ha->host_no, sess->loop_id);
320
321         /*
322          * Any commands for this session will be finished regularly,
323          * because we must not drop SCSI commands on transport level,
324          * at least without any response to the initiator.
325          */
326
327         scst_unregister_session(sess->scst_sess, 0, q2t_free_session_done);
328
329 out:
330         TRACE_EXIT();
331         return;
332 }
333
334 /* ha->hardware_lock supposed to be held on entry */
335 static void q2t_port_logout(scsi_qla_host_t *ha, int loop_id)
336 {
337         struct q2t_sess *sess = q2t_find_sess_by_lid(ha->tgt, loop_id);
338
339         TRACE_MGMT_DBG("scsi(%ld) Unregistering session %p loop_id=%d",
340               ha->host_no, sess, loop_id);
341
342         q2t_unreg_sess(sess);
343 }
344
345 /* ha->hardware_lock supposed to be held on entry */
346 static void q2t_clear_tgt_db(struct q2t_tgt *tgt)
347 {
348         struct q2t_sess *sess, *sess_tmp;
349
350         TRACE_ENTRY();
351
352         TRACE_MGMT_DBG("Clearing targets DB %p", tgt);
353
354         list_for_each_entry_safe(sess, sess_tmp, &tgt->sess_list, list) {
355                 q2t_unreg_sess(sess);
356         }
357
358         /* At this point tgt could be already dead */
359
360         TRACE_MGMT_DBG("Finished clearing Target DB %p", tgt);
361
362         TRACE_EXIT();
363         return;
364 }
365
366 /* should be called w/out hardware_lock, but tgt should be
367  * unfindable at this point */
368 static int q2t_target_release(struct scst_tgt *scst_tgt)
369 {
370         int res = 0;
371         struct q2t_tgt *tgt = (struct q2t_tgt *)scst_tgt_get_tgt_priv(scst_tgt);
372         scsi_qla_host_t *ha = tgt->ha;
373         unsigned long flags = 0;
374
375         TRACE_ENTRY();
376
377         spin_lock_irqsave(&ha->hardware_lock, flags);
378         tgt->tgt_shutdown = 1;
379         q2t_clear_tgt_db(tgt);
380         spin_unlock_irqrestore(&ha->hardware_lock, flags);
381
382         wait_event(tgt->waitQ, test_tgt_sess_count(tgt, ha));
383
384         mutex_lock(&qla_mgmt_mutex);
385
386         /* big hammer */
387         if (!ha->flags.host_shutting_down)
388                 tgt_data.disable_lun(ha);
389
390         /* wait for sessions to clear out (just in case) */
391         wait_event(tgt->waitQ, test_tgt_sess_count(tgt, ha));
392
393         TRACE_MGMT_DBG("Finished waiting for tgt %p: empty(sess_list)=%d "
394                 "sess_count=%d", tgt, list_empty(&tgt->sess_list),
395                 tgt->sess_count);
396
397         /* The lock is needed, because we still can get an incoming packet */
398         spin_lock_irqsave(&ha->hardware_lock, flags);
399         scst_tgt_set_tgt_priv(scst_tgt, NULL);
400         ha->tgt = NULL;
401         spin_unlock_irqrestore(&ha->hardware_lock, flags);
402
403         mutex_unlock(&qla_mgmt_mutex);
404
405         kfree(tgt);
406
407         TRACE_EXIT_RES(res);
408         return res;
409 }
410
411 static int q2t_pci_map_calc_cnt(struct q2t_prm *prm)
412 {
413         int res = 0;
414
415         sBUG_ON(prm->sg_cnt == 0);
416
417         /* 32 bit S/G Data Transfer */
418         prm->seg_cnt = pci_map_sg(prm->tgt->ha->pdev, prm->sg, prm->sg_cnt,
419                                scst_to_tgt_dma_dir(prm->data_direction));
420         if (unlikely(prm->seg_cnt == 0))
421                 goto out_err;
422         /*
423          * If greater than four sg entries then we need to allocate
424          * the continuation entries
425          */
426         if (prm->seg_cnt > prm->tgt->datasegs_per_cmd) {
427                 prm->req_cnt += (uint16_t)(prm->seg_cnt -
428                                 prm->tgt->datasegs_per_cmd) /
429                                 prm->tgt->datasegs_per_cont;
430                 if (((uint16_t)(prm->seg_cnt - prm->tgt->datasegs_per_cmd)) %
431                                 prm->tgt->datasegs_per_cont) {
432                         prm->req_cnt++;
433                 }
434         }
435
436 out:
437         TRACE_DBG("seg_cnt=%d, req_cnt=%d, res=%d", prm->seg_cnt,
438                 prm->req_cnt, res);
439         return res;
440
441 out_err:
442         PRINT_ERROR("qla2x00tgt(%ld): PCI mapping failed: sg_cnt=%d",
443                 prm->tgt->ha->host_no, prm->sg_cnt);
444         res = -1;
445         goto out;
446 }
447
448 /* ha->hardware_lock supposed to be held on entry */
449 static inline uint32_t q2t_make_handle(scsi_qla_host_t *ha)
450 {
451         uint32_t h;
452
453         h = ha->current_cmd;
454         /* always increment cmd handle */
455         do {
456                 ++h;
457                 if (h > MAX_OUTSTANDING_COMMANDS)
458                         h = 0;
459
460                 if (h == ha->current_cmd) {
461                         TRACE(TRACE_OUT_OF_MEM, "Ran out of empty cmd slots "
462                                 "in ha %p", ha);
463                         h = Q2T_NULL_HANDLE;
464                         break;
465                 }
466         } while ((h == Q2T_NULL_HANDLE) ||
467                  (h == Q2T_BUSY_HANDLE) ||
468                  (h == Q2T_SKIP_HANDLE) ||
469                  (ha->cmds[h] != NULL));
470
471         if (h != Q2T_NULL_HANDLE)
472                 ha->current_cmd = h;
473
474         return h;
475 }
476
477 /* ha->hardware_lock supposed to be held on entry */
478 /*
479  * NOTE: About CTIO_COMPLETION_HANDLE
480  *  This is checked for in qla2x00_process_response_queue() to see
481  *  if a handle coming back in a multi-complete should come to the tgt driver
482  *  or be handled there by qla2xxx
483  */
484 static void q2t_build_ctio_pkt(struct q2t_prm *prm)
485 {
486         uint16_t timeout;
487         uint32_t h;
488
489         prm->pkt = (struct ctio_common_entry *)tgt_data.req_pkt(prm->tgt->ha);
490
491         if (prm->tgt->tgt_enable_64bit_addr)
492                 prm->pkt->entry_type = CTIO_A64_TYPE;
493         else
494                 prm->pkt->entry_type = CONTINUE_TGT_IO_TYPE;
495
496         prm->pkt->entry_count = (uint8_t) prm->req_cnt;
497
498         h = q2t_make_handle(prm->tgt->ha);
499         if (h != Q2T_NULL_HANDLE)
500                 prm->tgt->ha->cmds[h] = prm->cmd;
501         prm->pkt->handle = h | CTIO_COMPLETION_HANDLE_MARK;
502
503         timeout = Q2T_TIMEOUT;
504         prm->pkt->timeout = cpu_to_le16(timeout);
505
506         /* Set initiator ID */
507         h = GET_TARGET_ID(prm->tgt->ha, &prm->cmd->atio);
508         SET_TARGET_ID(prm->tgt->ha, prm->pkt->target, h);
509
510         prm->pkt->exchange_id = prm->cmd->atio.exchange_id;
511
512         TRACE(TRACE_DEBUG|TRACE_SCSI,
513               "handle(scst_cmd) -> %08x, timeout %d L %#x -> I %#x E %#x",
514               prm->pkt->handle, timeout, le16_to_cpu(prm->cmd->atio.lun),
515               GET_TARGET_ID(prm->tgt->ha, prm->pkt),
516               le16_to_cpu(prm->pkt->exchange_id));
517
518 }
519
520 static void q2t_load_data_segments(struct q2t_prm *prm)
521 {
522         uint32_t cnt;
523         uint32_t *dword_ptr;
524         int enable_64bit_addressing = prm->tgt->tgt_enable_64bit_addr;
525
526         TRACE_DBG("iocb->scsi_status=%x, iocb->flags=%x",
527               le16_to_cpu(prm->pkt->scsi_status), le16_to_cpu(prm->pkt->flags));
528
529         prm->pkt->transfer_length = cpu_to_le32(prm->bufflen);
530
531         /* Setup packet address segment pointer */
532         dword_ptr = prm->pkt->dseg_0_address;
533
534         if (prm->seg_cnt == 0) {
535                 /* No data transfer */
536                 *dword_ptr++ = 0;
537                 *dword_ptr = 0;
538
539                 TRACE_BUFFER("No data, CTIO packet data",
540                              prm->pkt, REQUEST_ENTRY_SIZE);
541                 goto out;
542         }
543
544         /* Set total data segment count */
545         prm->pkt->dseg_count = cpu_to_le16(prm->seg_cnt);
546
547         /* If scatter gather */
548         TRACE_SG("%s", "Building S/G data segments...");
549         /* Load command entry data segments */
550         for (cnt = 0;
551              (cnt < prm->tgt->datasegs_per_cmd) && prm->seg_cnt;
552              cnt++, prm->seg_cnt--) {
553                 *dword_ptr++ =
554                     cpu_to_le32(pci_dma_lo32(sg_dma_address(prm->sg)));
555                 if (enable_64bit_addressing) {
556                         *dword_ptr++ =
557                             cpu_to_le32(pci_dma_hi32
558                                         (sg_dma_address(prm->sg)));
559                 }
560                 *dword_ptr++ = cpu_to_le32(sg_dma_len(prm->sg));
561
562                 TRACE_SG("S/G Segment phys_addr=%llx:%llx, len=%d",
563                         (long long unsigned int)pci_dma_hi32(
564                                 sg_dma_address(prm->sg)),
565                         (long long unsigned int)pci_dma_lo32(
566                                 sg_dma_address(prm->sg)),
567                         (int)sg_dma_len(prm->sg));
568
569                 prm->sg++;
570         }
571
572         TRACE_BUFFER("Scatter/gather, CTIO packet data",
573                      prm->pkt, REQUEST_ENTRY_SIZE);
574
575         /* Build continuation packets */
576         while (prm->seg_cnt > 0) {
577                 cont_a64_entry_t *cont_pkt64 =
578                         (cont_a64_entry_t *)tgt_data.req_cont_pkt(prm->tgt->ha);
579
580                 /*
581                  * Make sure that from cont_pkt64 none of
582                  * 64-bit specific fields used for 32-bit
583                  * addressing. Cast to (cont_entry_t *) for
584                  * that.
585                  */
586
587                 memset(cont_pkt64, 0, sizeof(*cont_pkt64));
588
589                 cont_pkt64->entry_count = 1;
590                 cont_pkt64->sys_define = 0;
591
592                 if (enable_64bit_addressing) {
593                         cont_pkt64->entry_type = CONTINUE_A64_TYPE;
594                         dword_ptr =
595                             (uint32_t *)&cont_pkt64->dseg_0_address;
596                 } else {
597                         cont_pkt64->entry_type = CONTINUE_TYPE;
598                         dword_ptr =
599                             (uint32_t *)&((cont_entry_t *)
600                                             cont_pkt64)->dseg_0_address;
601                 }
602
603                 /* Load continuation entry data segments */
604                 for (cnt = 0;
605                      cnt < prm->tgt->datasegs_per_cont && prm->seg_cnt;
606                      cnt++, prm->seg_cnt--) {
607                         *dword_ptr++ =
608                             cpu_to_le32(pci_dma_lo32
609                                         (sg_dma_address(prm->sg)));
610                         if (enable_64bit_addressing) {
611                                 *dword_ptr++ =
612                                     cpu_to_le32(pci_dma_hi32
613                                                 (sg_dma_address
614                                                  (prm->sg)));
615                         }
616                         *dword_ptr++ = cpu_to_le32(sg_dma_len(prm->sg));
617
618                         TRACE_SG("S/G Cont. phys_addr=%llx:%llx, len=%d",
619                                 (long long unsigned int)pci_dma_hi32(
620                                         sg_dma_address(prm->sg)),
621                                 (long long unsigned int)pci_dma_lo32(
622                                         sg_dma_address(prm->sg)),
623                                 (int)sg_dma_len(prm->sg));
624
625                         prm->sg++;
626                 }
627
628                 TRACE_BUFFER("Continuation packet data",
629                              cont_pkt64, REQUEST_ENTRY_SIZE);
630         }
631
632 out:
633         return;
634 }
635
636 static void q2t_init_ctio_ret_entry(struct ctio_ret_entry *ctio_m1,
637         struct q2t_prm *prm)
638 {
639         TRACE_ENTRY();
640
641         prm->sense_buffer_len = min((uint32_t)prm->sense_buffer_len,
642                                     (uint32_t)sizeof(ctio_m1->sense_data));
643
644         ctio_m1->flags = __constant_cpu_to_le16(OF_SSTS | OF_FAST_POST |
645                                                 OF_NO_DATA | OF_SS_MODE_1);
646         ctio_m1->flags |= __constant_cpu_to_le16(OF_INC_RC);
647         ctio_m1->scsi_status = cpu_to_le16(prm->rq_result);
648         ctio_m1->residual = cpu_to_le32(prm->residual);
649         if (SCST_SENSE_VALID(prm->sense_buffer)) {
650                 ctio_m1->scsi_status |=
651                                 __constant_cpu_to_le16(SS_SENSE_LEN_VALID);
652                 ctio_m1->sense_length = cpu_to_le16(prm->sense_buffer_len);
653                 memcpy(ctio_m1->sense_data, prm->sense_buffer,
654                        prm->sense_buffer_len);
655         } else {
656                 memset(ctio_m1->sense_data, 0, sizeof(ctio_m1->sense_data));
657                 ctio_m1->sense_length = 0;
658         }
659
660         TRACE_BUFFER("CTIO returned packet data", ctio_m1, REQUEST_ENTRY_SIZE);
661
662         /* Sense with len > 26, is it possible ??? */
663
664         TRACE_EXIT();
665         return;
666 }
667
668 static inline int q2t_has_data(struct scst_cmd *scst_cmd)
669 {
670         return scst_cmd_get_resp_data_len(scst_cmd) > 0;
671 }
672
673 static int q2t_xmit_response(struct scst_cmd *scst_cmd)
674 {
675         int res = SCST_TGT_RES_SUCCESS;
676         struct q2t_sess *sess;
677         int is_send_status;
678         unsigned long flags = 0;
679         struct q2t_prm prm;
680         int data_sense_flag = 0;
681         uint16_t full_req_cnt;
682
683         TRACE_ENTRY();
684         TRACE(TRACE_SCSI, "tag=%lld", scst_cmd_get_tag(scst_cmd));
685
686 #ifdef CONFIG_QLA_TGT_DEBUG_WORK_IN_THREAD
687         if (scst_cmd_atomic(scst_cmd))
688                 return SCST_TGT_RES_NEED_THREAD_CTX;
689 #endif
690
691         memset(&prm, 0, sizeof(prm));
692
693         prm.cmd = (struct q2t_cmd *)scst_cmd_get_tgt_priv(scst_cmd);
694         sess = (struct q2t_sess *)
695                 scst_sess_get_tgt_priv(scst_cmd_get_session(scst_cmd));
696
697         if (unlikely(scst_cmd_aborted(scst_cmd))) {
698                 scsi_qla_host_t *ha = sess->tgt->ha;
699
700                 TRACE(TRACE_MGMT_MINOR, "qla2x00tgt(%ld): terminating exchange "
701                         "for aborted scst_cmd=%p (tag=%lld)",
702                         ha->host_no, scst_cmd, scst_cmd_get_tag(scst_cmd));
703
704                 scst_set_delivery_status(scst_cmd, SCST_CMD_DELIVERY_ABORTED);
705
706                 prm.cmd->state = Q2T_STATE_ABORTED;
707
708                 q2t_send_term_exchange(ha, prm.cmd, &prm.cmd->atio, 0);
709                 /* !! At this point cmd could be already freed !! */
710                 goto out;
711         }
712
713         prm.sg = scst_cmd_get_sg(scst_cmd);
714         prm.bufflen = scst_cmd_get_resp_data_len(scst_cmd);
715         prm.sg_cnt = scst_cmd_get_sg_cnt(scst_cmd);
716         prm.data_direction = scst_cmd_get_data_direction(scst_cmd);
717         prm.rq_result = scst_cmd_get_status(scst_cmd);
718         prm.sense_buffer = scst_cmd_get_sense_buffer(scst_cmd);
719         prm.sense_buffer_len = scst_cmd_get_sense_buffer_len(scst_cmd);
720         prm.tgt = sess->tgt;
721         prm.seg_cnt = 0;
722         prm.req_cnt = 1;
723         is_send_status = scst_cmd_get_is_send_status(scst_cmd);
724
725         TRACE_DBG("rq_result=%x, is_send_status=%x", prm.rq_result,
726                 is_send_status);
727
728         if (prm.rq_result != 0)
729                 TRACE_BUFFER("Sense", prm.sense_buffer, prm.sense_buffer_len);
730
731         if (!is_send_status) {
732                 /* ToDo, after it's done in SCST */
733                 PRINT_ERROR("qla2x00tgt(%ld): is_send_status not set: "
734                      "feature not implemented", prm.tgt->ha->host_no);
735                 res = SCST_TGT_RES_FATAL_ERROR;
736                 goto out;
737         }
738
739         /* Acquire ring specific lock */
740         spin_lock_irqsave(&prm.tgt->ha->hardware_lock, flags);
741
742         /* Send marker if required */
743         if (tgt_data.issue_marker(prm.tgt->ha) != QLA_SUCCESS) {
744                 PRINT_ERROR("qla2x00tgt(%ld): __QLA2X00_MARKER() "
745                             "failed", prm.tgt->ha->host_no);
746                 res = SCST_TGT_RES_FATAL_ERROR;
747                 goto out_unlock;
748         }
749
750         TRACE_DBG("CTIO start: ha(%d)", (int) prm.tgt->ha->host_no);
751
752         if (q2t_has_data(scst_cmd)) {
753                 if (q2t_pci_map_calc_cnt(&prm) != 0) {
754                         res = SCST_TGT_RES_QUEUE_FULL;
755                         goto out_unlock;
756                 }
757                 full_req_cnt = prm.req_cnt;
758                 if (SCST_SENSE_VALID(prm.sense_buffer)) {
759                         data_sense_flag = 1;
760                         full_req_cnt++;
761                 }
762         } else
763                 full_req_cnt = prm.req_cnt;
764
765         q2t_build_ctio_pkt(&prm);
766
767         if (prm.data_direction != SCST_DATA_WRITE) {
768                 prm.residual =
769                     le32_to_cpu(prm.cmd->atio.data_length) - prm.bufflen;
770                 if (prm.residual > 0) {
771                         TRACE_DBG("Residual underflow: %d", prm.residual);
772                         prm.rq_result |= SS_RESIDUAL_UNDER;
773                 } else if (prm.residual < 0) {
774                         TRACE_DBG("Residual overflow: %d", prm.residual);
775                         prm.rq_result |= SS_RESIDUAL_OVER;
776                         prm.residual = -prm.residual;
777                 }
778
779                 if (q2t_has_data(scst_cmd)) {
780                         prm.pkt->flags |= __constant_cpu_to_le16(
781                                 OF_FAST_POST | OF_INC_RC | OF_DATA_IN);
782
783                         q2t_load_data_segments(&prm);
784
785                         if (data_sense_flag == 0) {
786                                 prm.pkt->scsi_status = cpu_to_le16(
787                                         prm.rq_result);
788                                 prm.pkt->residual = cpu_to_le32(prm.residual);
789                                 prm.pkt->flags |=
790                                         __constant_cpu_to_le16(OF_SSTS);
791                         } else {
792                                 struct ctio_ret_entry *ctio_m1 =
793                                         (struct ctio_ret_entry *)
794                                         tgt_data.req_cont_pkt(prm.tgt->ha);
795
796                                 TRACE_DBG("%s", "Building additional status "
797                                         "packet");
798
799                                 memcpy(ctio_m1, prm.pkt, sizeof(*ctio_m1));
800                                 ctio_m1->entry_count = 1;
801
802                                 /* Real finish is ctio_m1's finish */
803                                 prm.pkt->handle = Q2T_SKIP_HANDLE |
804                                                 CTIO_COMPLETION_HANDLE_MARK;
805
806                                 prm.pkt->flags &= ~__constant_cpu_to_le16(OF_INC_RC);
807
808                                 q2t_init_ctio_ret_entry(ctio_m1, &prm);
809                                 TRACE_BUFFER("Status CTIO packet data", ctio_m1,
810                                         REQUEST_ENTRY_SIZE);
811                         }
812                 } else
813                         q2t_init_ctio_ret_entry(
814                                 (struct ctio_ret_entry *)prm.pkt, &prm);
815         } else
816                 q2t_init_ctio_ret_entry((struct ctio_ret_entry *)prm.pkt,
817                                         &prm);
818
819         /* Mid-level is done processing */
820         prm.cmd->state = Q2T_STATE_PROCESSED;
821
822         TRACE_BUFFER("Xmitting", prm.pkt, REQUEST_ENTRY_SIZE);
823
824         q2t_exec_queue(prm.tgt->ha);
825
826 out_unlock:
827         /* Release ring specific lock */
828         spin_unlock_irqrestore(&prm.tgt->ha->hardware_lock, flags);
829
830 out:
831         TRACE_EXIT_RES(res);
832         return res;
833 }
834
835 static int q2t_rdy_to_xfer(struct scst_cmd *scst_cmd)
836 {
837         int res = SCST_TGT_RES_SUCCESS;
838         struct q2t_sess *sess;
839         unsigned long flags = 0;
840         struct q2t_prm prm;
841
842         TRACE_ENTRY();
843         TRACE(TRACE_SCSI, "tag=%lld", scst_cmd_get_tag(scst_cmd));
844
845 #ifdef CONFIG_QLA_TGT_DEBUG_WORK_IN_THREAD
846         if (scst_cmd_atomic(scst_cmd))
847                 return SCST_TGT_RES_NEED_THREAD_CTX;
848 #endif
849
850         memset(&prm, 0, sizeof(prm));
851
852         prm.cmd = (struct q2t_cmd *)scst_cmd_get_tgt_priv(scst_cmd);
853         sess = (struct q2t_sess *)
854                 scst_sess_get_tgt_priv(scst_cmd_get_session(scst_cmd));
855
856         prm.sg = scst_cmd_get_sg(scst_cmd);
857         prm.bufflen = scst_cmd_get_bufflen(scst_cmd);
858         prm.sg_cnt = scst_cmd_get_sg_cnt(scst_cmd);
859         prm.data_direction = scst_cmd_get_data_direction(scst_cmd);
860         prm.tgt = sess->tgt;
861         prm.req_cnt = 1;
862
863         /* Acquire ring specific lock */
864         spin_lock_irqsave(&prm.tgt->ha->hardware_lock, flags);
865
866         /* Send marker if required */
867         if (tgt_data.issue_marker(prm.tgt->ha) != QLA_SUCCESS) {
868                 PRINT_ERROR("qla2x00tgt(%ld): __QLA2X00_MARKER() "
869                             "failed", prm.tgt->ha->host_no);
870                 res = SCST_TGT_RES_FATAL_ERROR;
871                 goto out_unlock;
872         }
873
874         TRACE_DBG("CTIO_start: ha(%d)", (int) prm.tgt->ha->host_no);
875
876         /* Calculate number of entries and segments required */
877         if (q2t_pci_map_calc_cnt(&prm) != 0) {
878                 res = SCST_TGT_RES_QUEUE_FULL;
879                 goto out_unlock;
880         }
881
882         prm.cmd->iocb_cnt = prm.req_cnt;
883
884         q2t_build_ctio_pkt(&prm);
885
886         prm.pkt->flags = __constant_cpu_to_le16(OF_FAST_POST | OF_DATA_OUT);
887
888         q2t_load_data_segments(&prm);
889
890         prm.cmd->state = Q2T_STATE_NEED_DATA;
891
892         TRACE_BUFFER("Xfering", prm.pkt, REQUEST_ENTRY_SIZE);
893
894         q2t_exec_queue(prm.tgt->ha);
895
896 out_unlock:
897         /* Release ring specific lock */
898         spin_unlock_irqrestore(&prm.tgt->ha->hardware_lock, flags);
899
900         TRACE_EXIT_RES(res);
901         return res;
902 }
903
904 static void q2t_send_term_exchange(scsi_qla_host_t *ha, struct q2t_cmd *cmd,
905         struct atio_entry *atio, int ha_locked)
906 {
907         struct ctio_ret_entry *ctio;
908         unsigned long flags = 0;
909         int do_tgt_cmd_done = 0;
910
911         TRACE_ENTRY();
912
913         TRACE_DBG("Sending TERM EXCH CTIO (ha=%p)", ha);
914
915         if (!ha_locked)
916                 spin_lock_irqsave(&ha->hardware_lock, flags);
917
918         /* Send marker if required */
919         if (tgt_data.issue_marker(ha) != QLA_SUCCESS) {
920                 PRINT_ERROR("qla2x00tgt(%ld): __QLA2X00_MARKER() "
921                             "failed", ha->host_no);
922                 goto out_unlock;
923         }
924
925         ctio = (struct ctio_ret_entry *)tgt_data.req_pkt(ha);
926         if (ctio == NULL) {
927                 PRINT_ERROR("qla2x00tgt(%ld): %s failed: unable to allocate "
928                         "request packet", ha->host_no, __func__);
929                 goto out_unlock;
930         }
931
932         ctio->entry_type = CTIO_RET_TYPE;
933         ctio->entry_count = 1;
934
935         if (cmd != NULL) {
936                 ctio->handle = q2t_make_handle(ha);
937                 if (ctio->handle != Q2T_NULL_HANDLE) {
938                         ha->cmds[ctio->handle] = cmd;
939                 } else {
940                         ctio->handle = Q2T_SKIP_HANDLE;
941                         do_tgt_cmd_done = 1;
942                 }
943         } else
944                 ctio->handle = Q2T_SKIP_HANDLE;
945
946         ctio->handle |= CTIO_COMPLETION_HANDLE_MARK;
947
948         SET_TARGET_ID(ha, ctio->target, GET_TARGET_ID(ha, atio));
949         ctio->exchange_id = atio->exchange_id;
950
951         /* Most likely, it isn't needed */
952         ctio->residual = atio->data_length;
953         if (ctio->residual != 0)
954                 ctio->scsi_status |= SS_RESIDUAL_UNDER;
955
956         ctio->flags = __constant_cpu_to_le16(OF_FAST_POST | OF_TERM_EXCH |
957                         OF_NO_DATA | OF_SS_MODE_1);
958         ctio->flags |= __constant_cpu_to_le16(OF_INC_RC);
959
960         TRACE_BUFFER("CTIO TERM EXCH packet data", ctio, REQUEST_ENTRY_SIZE);
961
962         q2t_exec_queue(ha);
963
964 out_unlock:
965         if (!ha_locked)
966                 spin_unlock_irqrestore(&ha->hardware_lock, flags);
967
968         if (do_tgt_cmd_done) {
969                 if (!in_interrupt()) {
970                         msleep(250);
971                         scst_tgt_cmd_done(cmd->scst_cmd, SCST_CONTEXT_DIRECT);
972                 } else
973                         scst_tgt_cmd_done(cmd->scst_cmd, SCST_CONTEXT_TASKLET);
974                 /* !! At this point cmd could be already freed !! */
975         }
976
977         TRACE_EXIT();
978         return;
979 }
980
981 static inline void q2t_free_cmd(struct q2t_cmd *cmd)
982 {
983         kmem_cache_free(q2t_cmd_cachep, cmd);
984 }
985
986 static void q2t_on_free_cmd(struct scst_cmd *scst_cmd)
987 {
988         struct q2t_cmd *cmd = (struct q2t_cmd *)scst_cmd_get_tgt_priv(scst_cmd);
989
990         TRACE_ENTRY();
991         TRACE(TRACE_SCSI, "END Command tag %lld", scst_cmd_get_tag(scst_cmd));
992
993         scst_cmd_set_tgt_priv(scst_cmd, NULL);
994
995         q2t_free_cmd(cmd);
996
997         TRACE_EXIT();
998         return;
999 }
1000
1001 /* ha->hardware_lock supposed to be held on entry */
1002 static inline struct scst_cmd *q2t_get_cmd(scsi_qla_host_t *ha, uint32_t handle)
1003 {
1004         if (ha->cmds[handle] != NULL) {
1005                 struct scst_cmd *cmd = ha->cmds[handle]->scst_cmd;
1006                 ha->cmds[handle] = NULL;
1007                 return cmd;
1008         } else
1009                 return NULL;
1010 }
1011
1012 /* ha->hardware_lock supposed to be held on entry */
1013 static void q2t_do_ctio_completion(scsi_qla_host_t *ha,
1014                                    uint32_t handle,
1015                                    uint16_t status,
1016                                    struct ctio_common_entry *ctio)
1017 {
1018         struct scst_cmd *scst_cmd;
1019         struct q2t_cmd *cmd;
1020         uint16_t loop_id = -1;
1021         enum scst_exec_context context;
1022         int err = 0;
1023
1024         TRACE_ENTRY();
1025
1026 #ifdef CONFIG_QLA_TGT_DEBUG_WORK_IN_THREAD
1027         context = SCST_CONTEXT_THREAD;
1028 #else
1029         context = SCST_CONTEXT_TASKLET;
1030 #endif
1031
1032         if (ctio != NULL)
1033                 loop_id = GET_TARGET_ID(ha, ctio);
1034
1035         TRACE(TRACE_DEBUG|TRACE_SCSI, "handle(ctio %p status %#x) <- %08x I %x",
1036               ctio, status, handle, loop_id);
1037
1038         /* Clear out CTIO_COMPLETION_HANDLE_MARK */
1039         handle &= ~CTIO_COMPLETION_HANDLE_MARK;
1040
1041         if (status != CTIO_SUCCESS) {
1042                 err = 1;
1043                 switch (status) {
1044                 case CTIO_LIP_RESET:
1045                 case CTIO_TARGET_RESET:
1046                 case CTIO_ABORTED:
1047                 case CTIO_TIMEOUT:
1048                 case CTIO_INVALID_RX_ID:
1049                         /* they are OK */
1050                         TRACE(TRACE_MGMT_MINOR, "qla2x00tgt(%ld): CTIO with "
1051                                 "status %#x received (LIP_RESET=e, ABORTED=2, "
1052                                 "TARGET_RESET=17, TIMEOUT=b, "
1053                                 "INVALID_RX_ID=8)", ha->host_no, status);
1054                         break;
1055
1056                 case CTIO_PORT_LOGGED_OUT:
1057                 case CTIO_PORT_UNAVAILABLE:
1058                         PRINT_INFO("qla2x00tgt(%ld): CTIO with PORT LOGGED "
1059                                 "OUT (29) or PORT UNAVAILABLE (28) status %x "
1060                                 "received", ha->host_no, status);
1061                         break;
1062
1063                 default:
1064                         PRINT_ERROR("qla2x00tgt(%ld): CTIO with error status "
1065                                     "0x%x received", ha->host_no, status);
1066                         break;
1067                 }
1068                 q2t_modify_command_count(ha, 1, 0);
1069         }
1070
1071         if (handle != Q2T_NULL_HANDLE) {
1072                 if (unlikely(handle == Q2T_SKIP_HANDLE))
1073                         goto out;
1074                 if (unlikely(handle == Q2T_BUSY_HANDLE))
1075                         goto out;
1076                 scst_cmd = q2t_get_cmd(ha, handle);
1077                 if (unlikely(scst_cmd == NULL)) {
1078                         PRINT_INFO("qla2x00tgt(%ld): Suspicious: unable to "
1079                                    "find the command with handle %x",
1080                                    ha->host_no, handle);
1081                         goto out;
1082                 }
1083                 if (unlikely(err))
1084                         TRACE_MGMT_DBG("Found by handle failed CTIO scst_cmd "
1085                                 "%p (op %x)", scst_cmd, scst_cmd->cdb[0]);
1086         } else if (ctio != NULL) {
1087                 uint32_t tag = le16_to_cpu(ctio->exchange_id);
1088                 struct q2t_sess *sess = q2t_find_sess_by_lid(ha->tgt, loop_id);
1089
1090                 if (sess == NULL) {
1091                         PRINT_INFO("qla2x00tgt(%ld): Suspicious: "
1092                                    "ctio_completion for non-existing session "
1093                                    "(loop_id %d, tag %d)",
1094                                    ha->host_no, loop_id, tag);
1095                         goto out;
1096                 }
1097
1098                 scst_cmd = scst_find_cmd_by_tag(sess->scst_sess, tag);
1099                 if (scst_cmd == NULL) {
1100                         PRINT_INFO("qla2x00tgt(%ld): Suspicious: unable to "
1101                              "find the command with tag %d (loop_id %d)",
1102                              ha->host_no, tag, loop_id);
1103                         goto out;
1104                 }
1105                 if (unlikely(err))
1106                         TRACE_MGMT_DBG("Found by ctio failed CTIO scst_cmd %p "
1107                                 "(op %x)", scst_cmd, scst_cmd->cdb[0]);
1108
1109                 TRACE_DBG("Found scst_cmd %p", scst_cmd);
1110         } else
1111                 goto out;
1112
1113         cmd = (struct q2t_cmd *)scst_cmd_get_tgt_priv(scst_cmd);
1114         if (unlikely(err))
1115                 TRACE(TRACE_MGMT_MINOR, "Failed CTIO state %d (err %x)",
1116                         cmd->state, status);
1117
1118         if (cmd->state == Q2T_STATE_PROCESSED) {
1119                 TRACE_DBG("Command %p finished", cmd);
1120                 if (q2t_has_data(scst_cmd)) {
1121                         pci_unmap_sg(ha->pdev, scst_cmd_get_sg(scst_cmd),
1122                                 scst_cmd_get_sg_cnt(scst_cmd),
1123                                 scst_to_tgt_dma_dir(
1124                                         scst_cmd_get_data_direction(scst_cmd)));
1125                 }
1126                 goto out_free;
1127         } else if (cmd->state == Q2T_STATE_NEED_DATA) {
1128                 int rx_status = SCST_RX_STATUS_SUCCESS;
1129
1130                 cmd->state = Q2T_STATE_DATA_IN;
1131
1132                 if (status != CTIO_SUCCESS)
1133                         rx_status = SCST_RX_STATUS_ERROR;
1134
1135                 TRACE_DBG("Data received, context %x, rx_status %d",
1136                       context, rx_status);
1137
1138                 pci_unmap_sg(ha->pdev, scst_cmd_get_sg(scst_cmd),
1139                         scst_cmd_get_sg_cnt(scst_cmd),
1140                         scst_to_tgt_dma_dir(
1141                                 scst_cmd_get_data_direction(scst_cmd)));
1142
1143                 scst_rx_data(scst_cmd, rx_status, context);
1144         } else if (cmd->state == Q2T_STATE_ABORTED) {
1145                 TRACE_MGMT_DBG("Aborted command %p finished", cmd);
1146                 goto out_free;
1147         } else {
1148                 PRINT_ERROR("qla2x00tgt(%ld): A command in state (%d) should "
1149                         "not return a CTIO complete", ha->host_no, cmd->state);
1150                 goto out_free;
1151         }
1152
1153 out:
1154         TRACE_EXIT();
1155         return;
1156
1157 out_free:
1158         if (unlikely(err)) {
1159                 TRACE_MGMT_DBG("%s", "Finishing failed CTIO");
1160                 scst_set_delivery_status(scst_cmd, SCST_CMD_DELIVERY_FAILED);
1161         }
1162         scst_tgt_cmd_done(scst_cmd, context);
1163         goto out;
1164 }
1165
1166 /* ha->hardware_lock supposed to be held on entry */
1167 /* called via callback from qla2xxx */
1168 static void q2t_ctio_completion(scsi_qla_host_t *ha, uint32_t handle)
1169 {
1170         TRACE_ENTRY();
1171         sBUG_ON(ha == NULL);
1172
1173         if (ha->tgt != NULL) {
1174                 q2t_do_ctio_completion(ha, handle,
1175                                        CTIO_SUCCESS, NULL);
1176         } else {
1177                 TRACE_DBG("CTIO, but target mode not enabled. ha %p handle %#x",
1178                           ha, handle);
1179         }
1180         TRACE_EXIT();
1181         return;
1182 }
1183
1184 /* ha->hardware_lock supposed to be held on entry */
1185 static void q2t_send_busy(scsi_qla_host_t *ha, struct atio_entry *atio)
1186 {
1187         struct ctio_ret_entry *ctio;
1188
1189         TRACE_ENTRY();
1190
1191         ctio = (struct ctio_ret_entry *)tgt_data.req_pkt(ha);
1192         ctio->entry_type = CTIO_RET_TYPE;
1193         ctio->entry_count = 1;
1194         ctio->handle = Q2T_BUSY_HANDLE | CTIO_COMPLETION_HANDLE_MARK;
1195         ctio->scsi_status = __constant_cpu_to_le16(BUSY << 1);
1196         ctio->residual = atio->data_length;
1197         if (ctio->residual != 0)
1198                 ctio->scsi_status |= SS_RESIDUAL_UNDER;
1199
1200         /* Set IDs */
1201         SET_TARGET_ID(ha, ctio->target, GET_TARGET_ID(ha, atio));
1202         ctio->exchange_id = atio->exchange_id;
1203
1204         ctio->flags = __constant_cpu_to_le16(OF_SSTS | OF_FAST_POST |
1205                                              OF_NO_DATA | OF_SS_MODE_1);
1206         ctio->flags |= __constant_cpu_to_le16(OF_INC_RC);
1207
1208         TRACE_BUFFER("CTIO BUSY packet data", ctio, REQUEST_ENTRY_SIZE);
1209
1210         q2t_exec_queue(ha);
1211
1212         TRACE_EXIT();
1213         return;
1214 }
1215
1216 /* ha->hardware_lock is supposed to be held on entry */
1217 static int q2t_do_send_cmd_to_scst(scsi_qla_host_t *ha, struct q2t_cmd *cmd)
1218 {
1219         int res = 0;
1220         struct q2t_sess *sess = cmd->sess;
1221         uint16_t lun;
1222         scst_data_direction dir = SCST_DATA_NONE;
1223         enum scst_exec_context context;
1224
1225         TRACE_ENTRY();
1226
1227         /* make it be in network byte order */
1228         lun = swab16(cmd->atio.lun);
1229         cmd->scst_cmd = scst_rx_cmd(sess->scst_sess, (uint8_t *)&lun,
1230                                     sizeof(lun), cmd->atio.cdb, Q2T_MAX_CDB_LEN,
1231                                     SCST_ATOMIC);
1232
1233         if (cmd->scst_cmd == NULL) {
1234                 PRINT_ERROR("qla2x00tgt(%ld): scst_rx_cmd() failed for "
1235                      "host %ld(%p)", ha->host_no, ha->host_no, ha);
1236                 res = -EFAULT;
1237                 goto out;
1238         }
1239
1240         scst_cmd_set_tag(cmd->scst_cmd, le16_to_cpu(cmd->atio.exchange_id));
1241         scst_cmd_set_tgt_priv(cmd->scst_cmd, cmd);
1242
1243         if (cmd->atio.execution_codes & ATIO_EXEC_READ)
1244                 dir = SCST_DATA_READ;
1245         else if (cmd->atio.execution_codes & ATIO_EXEC_WRITE)
1246                 dir = SCST_DATA_WRITE;
1247         scst_cmd_set_expected(cmd->scst_cmd, dir,
1248                 le32_to_cpu(cmd->atio.data_length));
1249
1250         switch (cmd->atio.task_codes) {
1251         case ATIO_SIMPLE_QUEUE:
1252                 cmd->scst_cmd->queue_type = SCST_CMD_QUEUE_SIMPLE;
1253                 break;
1254         case ATIO_HEAD_OF_QUEUE:
1255                 cmd->scst_cmd->queue_type = SCST_CMD_QUEUE_HEAD_OF_QUEUE;
1256                 break;
1257         case ATIO_ORDERED_QUEUE:
1258                 cmd->scst_cmd->queue_type = SCST_CMD_QUEUE_ORDERED;
1259                 break;
1260         case ATIO_ACA_QUEUE:
1261                 cmd->scst_cmd->queue_type = SCST_CMD_QUEUE_ACA;
1262                 break;
1263         case ATIO_UNTAGGED:
1264                 cmd->scst_cmd->queue_type = SCST_CMD_QUEUE_UNTAGGED;
1265                 break;
1266         default:
1267                 PRINT_ERROR("qla2x00tgt(%ld): Unknown task code %x, use "
1268                         "ORDERED instead", ha->host_no, cmd->atio.task_codes);
1269                 cmd->scst_cmd->queue_type = SCST_CMD_QUEUE_ORDERED;
1270                 break;
1271         }
1272
1273 #ifdef CONFIG_QLA_TGT_DEBUG_WORK_IN_THREAD
1274         context = SCST_CONTEXT_THREAD;
1275 #else
1276         context = SCST_CONTEXT_TASKLET;
1277 #endif
1278
1279         TRACE_DBG("Context %x", context);
1280         TRACE(TRACE_SCSI, "START Command (tag %lld)",
1281                 scst_cmd_get_tag(cmd->scst_cmd));
1282         scst_cmd_init_done(cmd->scst_cmd, context);
1283
1284 out:
1285         TRACE_EXIT_RES(res);
1286         return res;
1287 }
1288
1289 /* Called in SCST's thread context */
1290 static void q2t_alloc_session_done(struct scst_session *scst_sess,
1291                                    void *data, int result)
1292 {
1293         TRACE_ENTRY();
1294
1295         if (result != 0) {
1296                 struct q2t_sess *sess = (struct q2t_sess *)data;
1297                 struct q2t_tgt *tgt = sess->tgt;
1298                 scsi_qla_host_t *ha = tgt->ha;
1299                 unsigned long flags;
1300
1301                 PRINT_INFO("qla2x00tgt(%ld): Session initialization failed",
1302                            ha->host_no);
1303
1304                 spin_lock_irqsave(&ha->hardware_lock, flags);
1305                 q2t_unreg_sess(sess);
1306                 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1307         }
1308
1309         TRACE_EXIT();
1310         return;
1311 }
1312
1313 static char *q2t_find_name(scsi_qla_host_t *ha, int loop_id)
1314 {
1315         int wwn_found = 0;
1316         char *wwn_str;
1317         fc_port_t *fcl;
1318
1319         wwn_str = kmalloc(3*WWN_SIZE, GFP_ATOMIC);
1320         if (wwn_str == NULL) {
1321                 TRACE(TRACE_OUT_OF_MEM, "%s", "Allocation of wwn_str failed");
1322                 goto out;
1323         }
1324
1325         /* Find the WWN in the port db given the loop_id */
1326         list_for_each_entry_rcu(fcl, &ha->fcports, list) {
1327             if (loop_id == (fcl->loop_id & 0xFF)) {
1328                 sprintf(wwn_str, "%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x",
1329                         fcl->port_name[0], fcl->port_name[1],
1330                         fcl->port_name[2], fcl->port_name[3],
1331                         fcl->port_name[4], fcl->port_name[5],
1332                         fcl->port_name[6], fcl->port_name[7]);
1333                 TRACE_DBG("found wwn: %s for loop_id: %d", wwn_str, loop_id);
1334                 wwn_found = 1;
1335                 break;
1336             }
1337         }
1338
1339         if (wwn_found == 0) {
1340                 TRACE_MGMT_DBG("qla2x00tgt(%ld): Unable to find wwn login for "
1341                         "loop id %d", ha->host_no, loop_id);
1342                 kfree(wwn_str);
1343                 wwn_str = NULL;
1344         }
1345
1346 out:
1347         return wwn_str;
1348 }
1349
1350 static char *q2t_make_name(scsi_qla_host_t *ha, const uint8_t *name)
1351 {
1352         char *wwn_str;
1353
1354         wwn_str = kmalloc(3*WWN_SIZE, GFP_ATOMIC);
1355         if (wwn_str == NULL) {
1356                 TRACE(TRACE_OUT_OF_MEM, "%s", "Allocation of wwn_str failed");
1357                 goto out;
1358         }
1359         sprintf(wwn_str, "%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x",
1360                 name[1], name[0], name[3], name[2], name[5], name[4],
1361                 name[7], name[6]);
1362
1363 out:
1364         return wwn_str;
1365 }
1366
1367 /* ha->hardware_lock supposed to be held on entry */
1368 static int q2t_send_cmd_to_scst(scsi_qla_host_t *ha, struct atio_entry *atio)
1369 {
1370         int res = 0;
1371         struct q2t_tgt *tgt;
1372         struct q2t_sess *sess;
1373         struct q2t_cmd *cmd;
1374         uint16_t *pn;
1375         int loop_id;
1376
1377         TRACE_ENTRY();
1378
1379         tgt = ha->tgt;
1380         loop_id = GET_TARGET_ID(ha, atio);
1381
1382         pn = (uint16_t *)(((char *)atio)+0x2a);
1383         TRACE_DBG("To SCST host_no=%ld l_id=%d tag=%d wwpn=%04x%04x%04x%04x",
1384                   ha->host_no, loop_id, le16_to_cpu(atio->exchange_id),
1385                   le16_to_cpu(pn[0]),
1386                   le16_to_cpu(pn[1]),
1387                   le16_to_cpu(pn[2]),
1388                   le16_to_cpu(pn[3]));
1389         /*        le64_to_cpu(*(uint64_t *)(((char *)atio)+0x2c))); */
1390         /*le32_to_cpu(*(uint32_t *)atio->initiator_port_name)); */
1391
1392         if (tgt->tgt_shutdown) {
1393                 TRACE_MGMT_DBG("New command while device %p is shutting "
1394                         "down", tgt);
1395                 res = -EFAULT;
1396                 goto out;
1397         }
1398
1399 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 17)
1400         cmd =  kmem_cache_alloc(q2t_cmd_cachep, GFP_ATOMIC);
1401 #else
1402         cmd =  kmem_cache_zalloc(q2t_cmd_cachep, GFP_ATOMIC);
1403 #endif
1404         if (cmd == NULL) {
1405                 TRACE(TRACE_OUT_OF_MEM, "%s", "Allocation of cmd failed");
1406                 res = -ENOMEM;
1407                 goto out;
1408         }
1409 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 17)
1410         memset(cmd, 0, sizeof(*cmd));
1411 #endif
1412
1413         TRACE_BUFFER("ATIO Coming Up", atio, sizeof(*atio));
1414         memcpy(&cmd->atio, atio, sizeof(*atio));
1415         cmd->state = Q2T_STATE_NEW;
1416
1417         sess = q2t_find_sess_by_lid(tgt, loop_id);
1418         if (unlikely(sess == NULL)) {
1419                 sess = kzalloc(sizeof(*sess), GFP_ATOMIC);
1420                 if (sess == NULL) {
1421                         TRACE(TRACE_OUT_OF_MEM, "%s",
1422                               "Allocation of sess failed");
1423                         res = -ENOMEM;
1424                         goto out_free_cmd;
1425                 }
1426
1427                 sess->tgt = tgt;
1428                 sess->loop_id = loop_id;
1429                 INIT_LIST_HEAD(&sess->list);
1430
1431                 /* register session (remote initiator) */
1432                 {
1433                         char *name;
1434                         if (IS_QLA2200(ha))
1435                                 name = q2t_find_name(ha, loop_id);
1436                         else {
1437                                 name = q2t_make_name(ha,
1438                                         atio->initiator_port_name);
1439                         }
1440                         if (name == NULL) {
1441                                 res = -ESRCH;
1442                                 goto out_free_sess;
1443                         }
1444
1445                         sess->scst_sess = scst_register_session(
1446                                 tgt->scst_tgt, 1, name, sess,
1447                                 q2t_alloc_session_done);
1448                         kfree(name);
1449                 }
1450
1451                 if (sess->scst_sess == NULL) {
1452                         PRINT_ERROR("qla2x00tgt(%ld): scst_register_session() "
1453                                 "failed for host %ld(%p)", ha->host_no,
1454                                 ha->host_no, ha);
1455                         res = -EFAULT;
1456                         goto out_free_sess;
1457                 }
1458                 scst_sess_set_tgt_priv(sess->scst_sess, sess);
1459
1460                 /* add session data to host data structure */
1461                 list_add(&sess->list, &tgt->sess_list);
1462                 tgt->sess_count++;
1463         }
1464
1465         cmd->sess = sess;
1466         res = q2t_do_send_cmd_to_scst(ha, cmd);
1467         if (res != 0)
1468                 goto out_free_cmd;
1469
1470 out:
1471         TRACE_EXIT_RES(res);
1472         return res;
1473
1474 out_free_sess:
1475         kfree(sess);
1476         /* go through */
1477
1478 out_free_cmd:
1479         q2t_free_cmd(cmd);
1480         goto out;
1481 }
1482
1483 /* ha->hardware_lock supposed to be held on entry */
1484 static int q2t_handle_task_mgmt(scsi_qla_host_t *ha, struct notify_entry *iocb)
1485 {
1486         int res = 0, rc = -1;
1487         struct q2t_mgmt_cmd *mcmd;
1488         struct q2t_tgt *tgt;
1489         struct q2t_sess *sess;
1490         int loop_id;
1491         uint16_t lun;
1492
1493         TRACE_ENTRY();
1494
1495         tgt = ha->tgt;
1496         loop_id = GET_TARGET_ID(ha, iocb);
1497
1498         /* Make it be in network byte order */
1499         lun = swab16(iocb->lun);
1500
1501         sess = q2t_find_sess_by_lid(tgt, loop_id);
1502         if (sess == NULL) {
1503                 TRACE(TRACE_MGMT, "qla2x00tgt(%ld): task mgmt fn 0x%x for "
1504                       "non-existant session", ha->host_no, iocb->task_flags);
1505                 res = -EFAULT;
1506                 goto out;
1507         }
1508
1509         mcmd = kzalloc(sizeof(*mcmd), GFP_ATOMIC);
1510         if (mcmd == NULL) {
1511                 TRACE(TRACE_OUT_OF_MEM, "%s", "Allocation of mgmt cmd failed");
1512                 res = -ENOMEM;
1513                 goto out;
1514         }
1515
1516         mcmd->sess = sess;
1517         mcmd->notify_entry = *iocb;
1518
1519         switch (iocb->task_flags) {
1520         case IMM_NTFY_CLEAR_ACA:
1521                 TRACE(TRACE_MGMT, "%s", "IMM_NTFY_CLEAR_ACA received");
1522                 rc = scst_rx_mgmt_fn_lun(sess->scst_sess, SCST_CLEAR_ACA,
1523                                          (uint8_t *)&lun, sizeof(lun),
1524                                          SCST_ATOMIC, mcmd);
1525                 break;
1526
1527         case IMM_NTFY_TARGET_RESET:
1528                 TRACE(TRACE_MGMT, "%s", "IMM_NTFY_TARGET_RESET received");
1529                 rc = scst_rx_mgmt_fn_lun(sess->scst_sess, SCST_TARGET_RESET,
1530                                          (uint8_t *)&lun, sizeof(lun),
1531                                          SCST_ATOMIC, mcmd);
1532                 break;
1533
1534         case IMM_NTFY_LUN_RESET:
1535                 TRACE(TRACE_MGMT, "%s", "IMM_NTFY_LUN_RESET received");
1536                 rc = scst_rx_mgmt_fn_lun(sess->scst_sess, SCST_LUN_RESET,
1537                                          (uint8_t *)&lun, sizeof(lun),
1538                                          SCST_ATOMIC, mcmd);
1539                 break;
1540
1541         case IMM_NTFY_CLEAR_TS:
1542                 TRACE(TRACE_MGMT, "%s", "IMM_NTFY_CLEAR_TS received");
1543                 rc = scst_rx_mgmt_fn_lun(sess->scst_sess, SCST_CLEAR_TASK_SET,
1544                                          (uint8_t *)&lun, sizeof(lun),
1545                                          SCST_ATOMIC, mcmd);
1546                 break;
1547
1548         case IMM_NTFY_ABORT_TS:
1549                 TRACE(TRACE_MGMT, "%s", "IMM_NTFY_ABORT_TS received");
1550                 rc = scst_rx_mgmt_fn_lun(sess->scst_sess, SCST_ABORT_TASK_SET,
1551                                          (uint8_t *)&lun, sizeof(lun),
1552                                          SCST_ATOMIC, mcmd);
1553                 break;
1554
1555         default:
1556                 PRINT_ERROR("qla2x00tgt(%ld): Unknown task mgmt fn 0x%x",
1557                             ha->host_no, iocb->task_flags);
1558                 break;
1559         }
1560
1561         if (rc != 0) {
1562                 PRINT_ERROR("qla2x00tgt(%ld): scst_rx_mgmt_fn_lun() failed: %d",
1563                             ha->host_no, rc);
1564                 res = -EFAULT;
1565                 goto out_free;
1566         }
1567
1568 out:
1569         TRACE_EXIT_RES(res);
1570         return res;
1571
1572 out_free:
1573         kfree(mcmd);
1574         goto out;
1575 }
1576
1577 /* ha->hardware_lock supposed to be held on entry */
1578 static int q2t_abort_task(scsi_qla_host_t *ha, struct notify_entry *iocb)
1579 {
1580         int res = 0, rc;
1581         struct q2t_mgmt_cmd *mcmd;
1582         struct q2t_sess *sess;
1583         int loop_id;
1584         uint32_t tag;
1585
1586         TRACE_ENTRY();
1587
1588         loop_id = GET_TARGET_ID(ha, iocb);
1589         tag = le16_to_cpu(iocb->seq_id);
1590
1591         sess = q2t_find_sess_by_lid(ha->tgt, loop_id);
1592         if (sess == NULL) {
1593                 TRACE(TRACE_MGMT, "qla2x00tgt(%ld): task abort for unexisting "
1594                         "session", ha->host_no);
1595                 res = -EFAULT;
1596                 goto out;
1597         }
1598
1599         mcmd = kzalloc(sizeof(*mcmd), GFP_ATOMIC);
1600         if (mcmd == NULL) {
1601                 TRACE(TRACE_OUT_OF_MEM, "%s", "Allocation of mgmt cmd failed");
1602                 res = -ENOMEM;
1603                 goto out;
1604         }
1605
1606         mcmd->sess = sess;
1607         mcmd->notify_entry = *iocb;
1608
1609         rc = scst_rx_mgmt_fn_tag(sess->scst_sess, SCST_ABORT_TASK, tag,
1610                 SCST_ATOMIC, mcmd);
1611         if (rc != 0) {
1612                 PRINT_ERROR("qla2x00tgt(%ld): scst_rx_mgmt_fn_tag() failed: %d",
1613                             ha->host_no, rc);
1614                 res = -EFAULT;
1615                 goto out_free;
1616         }
1617
1618 out:
1619         TRACE_EXIT_RES(res);
1620         return res;
1621
1622 out_free:
1623         kfree(mcmd);
1624         goto out;
1625 }
1626
1627 /* SCST Callback */
1628 static void q2t_task_mgmt_fn_done(struct scst_mgmt_cmd *scst_mcmd)
1629 {
1630         struct q2t_mgmt_cmd *mcmd;
1631         unsigned long flags;
1632
1633         TRACE_ENTRY();
1634
1635         TRACE_MGMT_DBG("scst_mcmd (%p) status %#x state %#x", scst_mcmd,
1636                 scst_mcmd->status, scst_mcmd->state);
1637
1638         mcmd = scst_mgmt_cmd_get_tgt_priv(scst_mcmd);
1639         if (unlikely(mcmd == NULL)) {
1640                 PRINT_ERROR("scst_mcmd %p tgt_spec is NULL", mcmd);
1641                 goto out;
1642         }
1643
1644         spin_lock_irqsave(&mcmd->sess->tgt->ha->hardware_lock, flags);
1645         q2t_send_notify_ack(mcmd->sess->tgt->ha, &mcmd->notify_entry, 0,
1646                 (scst_mgmt_cmd_get_status(scst_mcmd) == SCST_MGMT_STATUS_SUCCESS)
1647                          ? 0 : FC_TM_FAILED, 1);
1648         spin_unlock_irqrestore(&mcmd->sess->tgt->ha->hardware_lock, flags);
1649
1650         /* scst_mgmt_cmd_set_tgt_priv(scst_mcmd, NULL); */
1651         scst_mcmd->tgt_priv = NULL;
1652         kfree(mcmd);
1653
1654 out:
1655         TRACE_EXIT();
1656         return;
1657 }
1658
1659 /* ha->hardware_lock supposed to be held on entry */
1660 static void q2t_handle_imm_notify(scsi_qla_host_t *ha,
1661         struct notify_entry *iocb)
1662 {
1663         uint16_t status;
1664         int loop_id;
1665         uint32_t add_flags = 0;
1666         int send_notify_ack = 1;
1667
1668         TRACE_ENTRY();
1669
1670         status = le16_to_cpu(iocb->status);
1671         loop_id = GET_TARGET_ID(ha, iocb);
1672
1673         if (!ha->flags.enable_target_mode || ha->tgt == NULL) {
1674                 TRACE(TRACE_MGMT_DEBUG|TRACE_SCSI|TRACE_DEBUG,
1675                       "Acking %04x S %04x I %#x -> L %#x", status,
1676                       le16_to_cpu(iocb->seq_id), loop_id,
1677                       le16_to_cpu(iocb->lun));
1678                 goto out;
1679         }
1680
1681         TRACE_BUFFER("IMMED Notify Coming Up", iocb, sizeof(*iocb));
1682
1683         switch (status) {
1684         case IMM_NTFY_LIP_RESET:
1685                 TRACE(TRACE_MGMT, "LIP reset (I %#x)", loop_id);
1686                 /*
1687                  * ToDo: doing so we reset all holding RESERVE'ations,
1688                  * which could be unexpected, so be more carefull here
1689                  */
1690                 q2t_clear_tgt_db(ha->tgt);
1691                 /* set the Clear LIP reset event flag */
1692                 add_flags |= NOTIFY_ACK_CLEAR_LIP_RESET;
1693                 break;
1694
1695         case IMM_NTFY_IOCB_OVERFLOW:
1696                 PRINT_ERROR("qla2x00tgt(%ld): Cannot provide requested "
1697                         "capability (IOCB overflow)", ha->host_no);
1698                 break;
1699
1700         case IMM_NTFY_ABORT_TASK:
1701                 TRACE(TRACE_MGMT_MINOR, "Abort Task (S %04x I %#x -> L %#x)",
1702                       le16_to_cpu(iocb->seq_id), loop_id,
1703                       le16_to_cpu(iocb->lun));
1704                 if (q2t_abort_task(ha, iocb) == 0)
1705                         send_notify_ack = 0;
1706                 break;
1707
1708         case IMM_NTFY_PORT_LOGOUT:
1709                 TRACE(TRACE_MGMT, "Port logout (S %04x I %#x -> L %#x)",
1710                       le16_to_cpu(iocb->seq_id), loop_id,
1711                       le16_to_cpu(iocb->lun));
1712                 /*
1713                  * ToDo: doing so we reset all holding RESERVE'ations,
1714                  * which could be unexpected, so be more carefull here
1715                  */
1716                 q2t_port_logout(ha, loop_id);
1717                 break;
1718
1719         case IMM_NTFY_PORT_CONFIG:
1720         case IMM_NTFY_GLBL_TPRLO:
1721         case IMM_NTFY_GLBL_LOGO:
1722                 /* ToDo: ports DB changes handling ?? */
1723                 TRACE(TRACE_MGMT, "Port config changed, Global TPRLO or "
1724                       "Global LOGO (%d)", status);
1725                 /*
1726                  * ToDo: doing so we reset all holding RESERVE'ations,
1727                  * which could be unexpected, so be more carefull here
1728                  */
1729                 q2t_clear_tgt_db(ha->tgt);
1730                 break;
1731
1732         case IMM_NTFY_RESOURCE:
1733                 PRINT_ERROR("qla2x00tgt(%ld): Out of resources, host %ld",
1734                             ha->host_no, ha->host_no);
1735                 break;
1736
1737         case IMM_NTFY_MSG_RX:
1738                 TRACE(TRACE_MGMT, "Immediate notify task %x", iocb->task_flags);
1739                 if (q2t_handle_task_mgmt(ha, iocb) == 0)
1740                         send_notify_ack = 0;
1741                 break;
1742
1743         default:
1744                 PRINT_ERROR("qla2x00tgt(%ld): Received unknown immediate "
1745                         "notify status %x", ha->host_no, status);
1746                 break;
1747         }
1748
1749
1750 out:
1751         if (send_notify_ack)
1752                 q2t_send_notify_ack(ha, iocb, add_flags, 0, 0);
1753
1754         TRACE_EXIT();
1755         return;
1756 }
1757
1758 /* ha->hardware_lock supposed to be held on entry */
1759 /* called via callback from qla2xxx */
1760 static void q2t_response_pkt(scsi_qla_host_t *ha, sts_entry_t *pkt)
1761 {
1762         struct atio_entry *atio;
1763
1764         TRACE_ENTRY();
1765
1766         TRACE(TRACE_SCSI, "pkt %p: T %02x C %02x S %02x handle %#x",
1767               pkt, pkt->entry_type, pkt->entry_count, pkt->entry_status,
1768               pkt->handle);
1769
1770         if (unlikely(ha->tgt == NULL)) {
1771                 TRACE_DBG("response pkt, but no tgt. ha %p tgt_flag %d",
1772                         ha, ha->flags.enable_target_mode);
1773                 goto out;
1774         }
1775
1776         if (pkt->entry_status != 0) {
1777                 PRINT_ERROR("qla2x00tgt(%ld): Received response packet %x "
1778                      "with error status %x", ha->host_no, pkt->entry_type,
1779                      pkt->entry_status);
1780                 goto out;
1781         }
1782
1783         switch (pkt->entry_type) {
1784         case ACCEPT_TGT_IO_TYPE:
1785                 if (ha->flags.enable_target_mode && ha->tgt != NULL) {
1786                         int rc;
1787                         atio = (struct atio_entry *)pkt;
1788                         TRACE_DBG("ACCEPT_TGT_IO host_no %ld status %04x "
1789                                   "lun %04x read/write %d data_length %08x "
1790                                   "target_id %02x exchange_id %04x ",
1791                                   ha->host_no, le16_to_cpu(atio->status),
1792                                   le16_to_cpu(atio->lun),
1793                                   atio->execution_codes,
1794                                   le32_to_cpu(atio->data_length),
1795                                   GET_TARGET_ID(ha, atio),
1796                                   le16_to_cpu(atio->exchange_id));
1797                         if (atio->status !=
1798                                 __constant_cpu_to_le16(ATIO_CDB_VALID)) {
1799                                 PRINT_ERROR("qla2x00tgt(%ld): ATIO with error "
1800                                             "status %x received", ha->host_no,
1801                                             le16_to_cpu(atio->status));
1802                                 break;
1803                         }
1804                         PRINT_BUFF_FLAG(TRACE_SCSI, "CDB", atio->cdb,
1805                                         sizeof(atio->cdb));
1806                         rc = q2t_send_cmd_to_scst(ha, atio);
1807                         if (unlikely(rc != 0)) {
1808                                 if (rc == -ESRCH) {
1809 #if 1 /* With TERM EXCHANGE some FC cards refuse to boot */
1810                                         q2t_send_busy(ha, atio);
1811 #else
1812                                         q2t_send_term_exchange(ha, NULL,
1813                                                 atio, 1);
1814 #endif
1815                                 } else {
1816                                         if (!ha->tgt->tgt_shutdown) {
1817                                                 PRINT_INFO("qla2x00tgt(%ld): "
1818                                                     "Unable to send the "
1819                                                     "command to SCSI target "
1820                                                     "mid-level, sending BUSY "
1821                                                     "status", ha->host_no);
1822                                         }
1823                                         q2t_send_busy(ha, atio);
1824                                 }
1825                         }
1826                 } else if (!ha->tgt->tgt_shutdown) {
1827                         PRINT_ERROR("qla2x00tgt(%ld): ATIO, but target mode "
1828                                 "disabled", ha->host_no);
1829                 }
1830                 break;
1831
1832         case CONTINUE_TGT_IO_TYPE:
1833                 if (ha->flags.enable_target_mode && ha->tgt != NULL) {
1834                         struct ctio_common_entry *entry =
1835                                 (struct ctio_common_entry *)pkt;
1836                         TRACE_DBG("CONTINUE_TGT_IO: host_no %ld",
1837                                   ha->host_no);
1838                         q2t_do_ctio_completion(ha, entry->handle,
1839                                                le16_to_cpu(entry->status),
1840                                                entry);
1841                 } else if (!ha->tgt->tgt_shutdown) {
1842                         PRINT_ERROR("qla2x00tgt(%ld): CTIO, but target mode "
1843                                 "disabled", ha->host_no);
1844                 }
1845                 break;
1846
1847         case CTIO_A64_TYPE:
1848                 if (ha->flags.enable_target_mode && ha->tgt != NULL) {
1849                         struct ctio_common_entry *entry =
1850                                 (struct ctio_common_entry *)pkt;
1851                         TRACE_DBG("CTIO_A64: host_no %ld", ha->host_no);
1852                         q2t_do_ctio_completion(ha, entry->handle,
1853                                                le16_to_cpu(entry->status),
1854                                                entry);
1855                 } else if (!ha->tgt->tgt_shutdown) {
1856                         PRINT_ERROR("qla2x00tgt(%ld): CTIO_A64, but target "
1857                                 "mode disabled", ha->host_no);
1858                 }
1859                 break;
1860
1861         case IMMED_NOTIFY_TYPE:
1862                 TRACE_DBG("%s", "IMMED_NOTIFY");
1863                 q2t_handle_imm_notify(ha, (struct notify_entry *)pkt);
1864                 break;
1865
1866         case NOTIFY_ACK_TYPE:
1867                 if (ha->tgt == NULL) {
1868                         PRINT_ERROR("qla2x00tgt(%ld): NOTIFY_ACK recieved "
1869                                 "with NULL tgt", ha->host_no);
1870                 } else if (ha->tgt->notify_ack_expected > 0) {
1871                         struct nack_entry *entry = (struct nack_entry *)pkt;
1872                         TRACE_DBG("NOTIFY_ACK seq %04x status %x",
1873                                   le16_to_cpu(entry->seq_id),
1874                                   le16_to_cpu(entry->status));
1875                         ha->tgt->notify_ack_expected--;
1876                         if (entry->status !=
1877                                 __constant_cpu_to_le16(NOTIFY_ACK_SUCCESS)) {
1878                                 PRINT_ERROR("qla2x00tgt(%ld): NOTIFY_ACK "
1879                                             "failed %x", ha->host_no,
1880                                             le16_to_cpu(entry->status));
1881                         }
1882                 } else {
1883                         PRINT_ERROR("qla2x00tgt(%ld): Unexpected NOTIFY_ACK "
1884                                     "received", ha->host_no);
1885                 }
1886                 break;
1887
1888         case MODIFY_LUN_TYPE:
1889                 if ((ha->tgt != NULL) && (ha->tgt->modify_lun_expected > 0)) {
1890                         struct q2t_tgt *tgt = ha->tgt;
1891                         struct modify_lun_entry *entry =
1892                                 (struct modify_lun_entry *)pkt;
1893                         TRACE_DBG("MODIFY_LUN %x, imm %c%d, cmd %c%d",
1894                                 entry->status,
1895                                 (entry->operators & MODIFY_LUN_IMM_ADD) ? '+'
1896                                   : (entry->operators & MODIFY_LUN_IMM_SUB) ?
1897                                         '-' : ' ',
1898                                 entry->immed_notify_count,
1899                                 (entry->operators & MODIFY_LUN_CMD_ADD) ? '+'
1900                                   : (entry->operators & MODIFY_LUN_CMD_SUB) ?
1901                                         '-'  : ' ',
1902                                 entry->command_count);
1903                         tgt->modify_lun_expected--;
1904                         if (entry->status != MODIFY_LUN_SUCCESS) {
1905                                 PRINT_ERROR("qla2x00tgt(%ld): MODIFY_LUN "
1906                                             "failed %x", ha->host_no,
1907                                             entry->status);
1908                         }
1909                 } else {
1910                         PRINT_ERROR("qla2x00tgt(%ld): Unexpected MODIFY_LUN "
1911                                 "received", (ha != NULL) ? ha->host_no : -1);
1912                 }
1913                 break;
1914
1915         case ENABLE_LUN_TYPE:
1916                 if (ha->tgt != NULL) {
1917                         struct elun_entry *entry = (struct elun_entry *)pkt;
1918                         TRACE_DBG("ENABLE_LUN %x imm %u cmd %u ",
1919                                   entry->status, entry->immed_notify_count,
1920                                   entry->command_count);
1921                         if ((ha->flags.enable_target_mode) &&
1922                             (entry->status == ENABLE_LUN_ALREADY_ENABLED)) {
1923                                 TRACE_DBG("LUN is already enabled: %#x",
1924                                           entry->status);
1925                                 entry->status = ENABLE_LUN_SUCCESS;
1926                         } else if (entry->status == ENABLE_LUN_RC_NONZERO) {
1927                                 TRACE_DBG("ENABLE_LUN succeeded, but with "
1928                                         "error: %#x", entry->status);
1929                                 entry->status = ENABLE_LUN_SUCCESS;
1930                         } else if (entry->status != ENABLE_LUN_SUCCESS) {
1931                                 PRINT_ERROR("qla2x00tgt(%ld): ENABLE_LUN "
1932                                             "failed %x",
1933                                             ha->host_no, entry->status);
1934                                 ha->flags.enable_target_mode =
1935                                         ~ha->flags.enable_target_mode;
1936                         } /* else success */
1937                 }
1938                 break;
1939
1940         default:
1941                 PRINT_INFO("qla2x00tgt(%ld): Received unknown response pkt "
1942                      "type %x", ha->host_no, pkt->entry_type);
1943                 break;
1944         }
1945
1946 out:
1947         TRACE_EXIT();
1948         return;
1949 }
1950
1951 /* ha->hardware_lock supposed to be held on entry */
1952 /* called via callback from qla2xxx */
1953 static void q2t_async_event(uint16_t code, scsi_qla_host_t *ha,
1954         uint16_t *mailbox)
1955 {
1956         TRACE_ENTRY();
1957
1958         sBUG_ON(ha == NULL);
1959
1960         if (unlikely(ha->tgt == NULL)) {
1961                 TRACE(TRACE_DEBUG|TRACE_MGMT,
1962                       "ASYNC EVENT %#x, but no tgt. ha %p tgt_flag %d",
1963                       code, ha, ha->flags.enable_target_mode);
1964                 goto out;
1965         }
1966
1967         switch (code) {
1968         case MBA_RESET:                 /* Reset */
1969         case MBA_SYSTEM_ERR:            /* System Error */
1970         case MBA_REQ_TRANSFER_ERR:      /* Request Transfer Error */
1971         case MBA_RSP_TRANSFER_ERR:      /* Response Transfer Error */
1972         case MBA_LOOP_DOWN:
1973         case MBA_LIP_OCCURRED:          /* Loop Initialization Procedure */
1974         case MBA_LIP_RESET:             /* LIP reset occurred */
1975         case MBA_POINT_TO_POINT:        /* Point to point mode. */
1976         case MBA_CHG_IN_CONNECTION:     /* Change in connection mode. */
1977                 TRACE_MGMT_DBG("Async event %#x occured: clear tgt_db", code);
1978 #if 0
1979                 /*
1980                  * ToDo: doing so we reset all holding RESERVE'ations,
1981                  * which could be unexpected, so be more carefull here
1982                  */
1983                 q2t_clear_tgt_db(ha->tgt);
1984 #endif
1985                 break;
1986         case MBA_RSCN_UPDATE:
1987                 TRACE_MGMT_DBG("RSCN Update (%x) N_Port %#06x (fmt %x)", code,
1988                         ((mailbox[1] & 0xFF) << 16) | le16_to_cpu(mailbox[2]),
1989                         (mailbox[1] & 0xFF00) >> 8);
1990                 break;
1991
1992         case MBA_PORT_UPDATE:           /* Port database update occurred */
1993                 TRACE_MGMT_DBG("Port DB Chng: L_ID %#4x did %d: ignore",
1994                       le16_to_cpu(mailbox[1]), le16_to_cpu(mailbox[2]));
1995                 break;
1996
1997         case MBA_LOOP_UP:
1998         default:
1999                 TRACE_MGMT_DBG("Async event %#x occured: ignore", code);
2000                 /* just don't DO anything */
2001                 break;
2002         }
2003
2004 out:
2005         TRACE_EXIT();
2006         return;
2007 }
2008
2009 /* ha->hardware_lock supposed to be held and IRQs off */
2010 static void q2t_cleanup_hw_pending_cmd(scsi_qla_host_t *ha, struct q2t_cmd *cmd)
2011 {
2012         uint32_t h;
2013
2014         for (h = 0; h < MAX_OUTSTANDING_COMMANDS; h++) {
2015                 if (ha->cmds[h] == cmd) {
2016                         TRACE_DBG("Clearing handle %d for cmd %p", h, cmd);
2017                         ha->cmds[h] = NULL;
2018                         break;
2019                 }
2020         }
2021         return;
2022 }
2023
2024 static void q2t_on_hw_pending_cmd_timeout(struct scst_cmd *scst_cmd)
2025 {
2026         struct q2t_cmd *cmd = (struct q2t_cmd *)scst_cmd_get_tgt_priv(scst_cmd);
2027         struct q2t_tgt *tgt = cmd->sess->tgt;
2028         scsi_qla_host_t *ha = tgt->ha;
2029         unsigned long flags;
2030
2031         TRACE_ENTRY();
2032
2033         TRACE_MGMT_DBG("Cmd %p HW pending for too long (state %x)", cmd,
2034                 cmd->state);
2035
2036         spin_lock_irqsave(&ha->hardware_lock, flags);
2037
2038         if (cmd->state == Q2T_STATE_PROCESSED) {
2039                 TRACE_MGMT_DBG("Force finishing cmd %p", cmd);
2040                 if (q2t_has_data(scst_cmd)) {
2041                         pci_unmap_sg(ha->pdev, scst_cmd_get_sg(scst_cmd),
2042                                 scst_cmd_get_sg_cnt(scst_cmd),
2043                                 scst_to_tgt_dma_dir(
2044                                     scst_cmd_get_data_direction(scst_cmd)));
2045                 }
2046         } else if (cmd->state == Q2T_STATE_NEED_DATA) {
2047                 TRACE_MGMT_DBG("Force rx_data cmd %p", cmd);
2048
2049                 q2t_cleanup_hw_pending_cmd(ha, cmd);
2050
2051                 pci_unmap_sg(ha->pdev, scst_cmd_get_sg(scst_cmd),
2052                         scst_cmd_get_sg_cnt(scst_cmd),
2053                         scst_to_tgt_dma_dir(
2054                                 scst_cmd_get_data_direction(scst_cmd)));
2055
2056                 scst_rx_data(scst_cmd, SCST_RX_STATUS_ERROR_FATAL,
2057                                 SCST_CONTEXT_THREAD);
2058                 goto out_unlock;
2059         } else if (cmd->state == Q2T_STATE_ABORTED) {
2060                 TRACE_MGMT_DBG("Force finishing aborted cmd %p (tag %ld)",
2061                         cmd, (unsigned long)scst_cmd->tag);
2062         } else {
2063                 PRINT_ERROR("qla2x00tgt(%ld): A command in state (%d) should "
2064                         "not be HW pending", ha->host_no, cmd->state);
2065                 goto out_unlock;
2066         }
2067
2068         q2t_cleanup_hw_pending_cmd(ha, cmd);
2069
2070         scst_set_delivery_status(scst_cmd, SCST_CMD_DELIVERY_FAILED);
2071         scst_tgt_cmd_done(scst_cmd, SCST_CONTEXT_THREAD);
2072
2073 out_unlock:
2074         spin_unlock_irqrestore(&ha->hardware_lock, flags);
2075         TRACE_EXIT();
2076         return;
2077 }
2078
2079 static int q2t_get_target_name(scsi_qla_host_t *ha, char **wwn)
2080 {
2081         const int wwn_len = 3*WWN_SIZE+2;
2082         int res = 0;
2083         char *name;
2084
2085         name = kmalloc(wwn_len, GFP_KERNEL);
2086         if (name == NULL) {
2087                 TRACE(TRACE_OUT_OF_MEM, "%s", "Allocation of tgt name failed");
2088                 res = -ENOMEM;
2089                 goto out;
2090         }
2091
2092         sprintf(name, "%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x",
2093                 ha->port_name[0], ha->port_name[1],
2094                 ha->port_name[2], ha->port_name[3],
2095                 ha->port_name[4], ha->port_name[5],
2096                 ha->port_name[6], ha->port_name[7]);
2097
2098         *wwn = name;
2099
2100 out:
2101         return res;
2102 }
2103
2104 /* no lock held on entry */
2105 /* called via callback from qla2xxx */
2106 static void q2t_host_action(scsi_qla_host_t *ha,
2107                             enum qla2x_tgt_host_action action)
2108 {
2109         struct q2t_tgt *tgt = NULL;
2110         unsigned long flags = 0;
2111
2112         TRACE_ENTRY();
2113
2114         sBUG_ON(ha == NULL);
2115
2116         switch (action) {
2117         case ENABLE_TARGET_MODE:
2118         {
2119                 char *wwn;
2120                 int sg_tablesize;
2121
2122                 tgt = kzalloc(sizeof(*tgt), GFP_KERNEL);
2123                 if (tgt == NULL) {
2124                         TRACE(TRACE_OUT_OF_MEM, "%s",
2125                               "Allocation of tgt failed");
2126                         goto out;
2127                 }
2128
2129                 tgt->ha = ha;
2130                 INIT_LIST_HEAD(&tgt->sess_list);
2131                 init_waitqueue_head(&tgt->waitQ);
2132
2133                 if (ha->flags.enable_64bit_addressing) {
2134                         PRINT_INFO("qla2x00tgt(%ld): 64 Bit PCI "
2135                                    "Addressing Enabled", ha->host_no);
2136                         tgt->tgt_enable_64bit_addr = 1;
2137                         /* 3 is reserved */
2138                         sg_tablesize =
2139                                 QLA_MAX_SG64(ha->request_q_length - 3);
2140                         tgt->datasegs_per_cmd = DATASEGS_PER_COMMAND64;
2141                         tgt->datasegs_per_cont = DATASEGS_PER_CONT64;
2142                 } else {
2143                         PRINT_INFO("qla2x00tgt(%ld): Using 32 Bit "
2144                                    "PCI Addressing", ha->host_no);
2145                         sg_tablesize =
2146                                 QLA_MAX_SG32(ha->request_q_length - 3);
2147                         tgt->datasegs_per_cmd = DATASEGS_PER_COMMAND32;
2148                         tgt->datasegs_per_cont = DATASEGS_PER_CONT32;
2149                 }
2150
2151                 if (q2t_get_target_name(ha, &wwn) != 0) {
2152                         kfree(tgt);
2153                         goto out;
2154                 }
2155
2156                 mutex_lock(&qla_mgmt_mutex);
2157
2158                 tgt->scst_tgt = scst_register(&tgt_template, wwn);
2159                 kfree(wwn);
2160                 if (!tgt->scst_tgt) {
2161                         PRINT_ERROR("qla2x00tgt(%ld): scst_register() "
2162                                     "failed for host %ld(%p)", ha->host_no,
2163                                     ha->host_no, ha);
2164                         kfree(tgt);
2165                         goto out_unlock;
2166                 }
2167
2168                 scst_tgt_set_sg_tablesize(tgt->scst_tgt, sg_tablesize);
2169                 scst_tgt_set_tgt_priv(tgt->scst_tgt, tgt);
2170
2171                 spin_lock_irqsave(&ha->hardware_lock, flags);
2172                 ha->tgt = tgt;
2173                 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2174
2175                 TRACE_DBG("Enable lun for host %ld(%ld,%p)",
2176                           ha->host_no, ha->host_no, ha);
2177                 tgt_data.enable_lun(ha);
2178
2179                 mutex_unlock(&qla_mgmt_mutex);
2180                 break;
2181         }
2182         case DISABLE_TARGET_MODE:
2183                 mutex_lock(&qla_mgmt_mutex);
2184
2185                 spin_lock_irqsave(&ha->hardware_lock, flags);
2186                 if (ha->tgt == NULL) {
2187                         /* ensure target mode is marked as off */
2188                         ha->flags.enable_target_mode = 0;
2189                         spin_unlock_irqrestore(&ha->hardware_lock, flags);
2190
2191                         if (!ha->flags.host_shutting_down)
2192                                 tgt_data.disable_lun(ha);
2193
2194                         goto out_unlock;
2195                 }
2196
2197                 tgt = ha->tgt;
2198                 ha->tgt = NULL; /* ensure no one gets in behind us */
2199                 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2200
2201                 mutex_unlock(&qla_mgmt_mutex);
2202
2203                 TRACE_DBG("Shutting down host %ld(%ld,%p)",
2204                           ha->host_no, ha->host_no, ha);
2205                 scst_unregister(tgt->scst_tgt);
2206                 /*
2207                  * Free of tgt happens via callback q2t_target_release
2208                  * called from scst_unregister, so we shouldn't touch it again
2209                  */
2210                 tgt = NULL;
2211                 break;
2212
2213         default:
2214                 PRINT_ERROR("Unknown action %d", action);
2215                 break;
2216         }
2217
2218 out:
2219         TRACE_EXIT();
2220         return;
2221
2222 out_unlock:
2223         mutex_unlock(&qla_mgmt_mutex);
2224         goto out;
2225 }
2226
2227 #if defined(CONFIG_SCST_DEBUG) || defined(CONFIG_SCST_TRACING)
2228
2229 #define Q2T_PROC_LOG_ENTRY_NAME     "trace_level"
2230
2231 #include <linux/proc_fs.h>
2232
2233 static int q2t_log_info_show(struct seq_file *seq, void *v)
2234 {
2235         int res = 0;
2236
2237         TRACE_ENTRY();
2238
2239         res = scst_proc_log_entry_read(seq, trace_flag, NULL);
2240
2241         TRACE_EXIT_RES(res);
2242         return res;
2243 }
2244
2245 static ssize_t q2t_proc_log_entry_write(struct file *file,
2246         const char __user *buf, size_t length, loff_t *off)
2247 {
2248         int res = 0;
2249
2250         TRACE_ENTRY();
2251
2252         res = scst_proc_log_entry_write(file, buf, length, &trace_flag,
2253                 Q2T_DEFAULT_LOG_FLAGS, NULL);
2254
2255         TRACE_EXIT_RES(res);
2256         return res;
2257 }
2258
2259 static struct scst_proc_data q2t_log_proc_data = {
2260         SCST_DEF_RW_SEQ_OP(q2t_proc_log_entry_write)
2261         .show = q2t_log_info_show,
2262 };
2263 #endif
2264
2265 static int q2t_proc_log_entry_build(struct scst_tgt_template *templ)
2266 {
2267         int res = 0;
2268 #if defined(CONFIG_SCST_DEBUG) || defined(CONFIG_SCST_TRACING)
2269         struct proc_dir_entry *p, *root;
2270
2271         TRACE_ENTRY();
2272
2273         root = scst_proc_get_tgt_root(templ);
2274         if (root) {
2275                 /* create the proc file entry for the device */
2276                 q2t_log_proc_data.data = (void *)templ->name;
2277                 p = scst_create_proc_entry(root, Q2T_PROC_LOG_ENTRY_NAME,
2278                                         &q2t_log_proc_data);
2279                 if (p == NULL) {
2280                         PRINT_ERROR("Not enough memory to register "
2281                              "target driver %s entry %s in /proc",
2282                               templ->name, Q2T_PROC_LOG_ENTRY_NAME);
2283                         res = -ENOMEM;
2284                         goto out;
2285                 }
2286         }
2287
2288 out:
2289
2290         TRACE_EXIT_RES(res);
2291 #endif
2292         return res;
2293 }
2294
2295 static void q2t_proc_log_entry_clean(struct scst_tgt_template *templ)
2296 {
2297 #if defined(CONFIG_SCST_DEBUG) || defined(CONFIG_SCST_TRACING)
2298         struct proc_dir_entry *root;
2299
2300         TRACE_ENTRY();
2301
2302         root = scst_proc_get_tgt_root(templ);
2303         if (root)
2304                 remove_proc_entry(Q2T_PROC_LOG_ENTRY_NAME, root);
2305
2306         TRACE_EXIT();
2307 #endif
2308         return;
2309 }
2310
2311 static int __init q2t_init(void)
2312 {
2313         int res = 0;
2314
2315         TRACE_ENTRY();
2316
2317         PRINT_INFO("Initializing QLogic Fibre Channel HBA Driver target mode "
2318                 "addon version %s", Q2T_VERSION_STRING);
2319
2320         q2t_cmd_cachep = KMEM_CACHE(q2t_cmd, SCST_SLAB_FLAGS);
2321         if (q2t_cmd_cachep == NULL) {
2322                 res = -ENOMEM;
2323                 goto out;
2324         }
2325
2326         res = scst_register_target_template(&tgt_template);
2327         if (res < 0)
2328                 goto out_free_kmem;
2329
2330         /*
2331          * qla2xxx_tgt_register_driver() happens in q2t_target_detect
2332          * called via scst_register_target_template()
2333          */
2334
2335         res = q2t_proc_log_entry_build(&tgt_template);
2336         if (res < 0)
2337                 goto out_unreg_target;
2338
2339 out:
2340         TRACE_EXIT();
2341         return res;
2342
2343 out_unreg_target:
2344         scst_unregister_target_template(&tgt_template);
2345
2346 out_free_kmem:
2347         kmem_cache_destroy(q2t_cmd_cachep);
2348
2349         qla2xxx_tgt_unregister_driver();
2350         goto out;
2351 }
2352
2353 static void __exit q2t_exit(void)
2354 {
2355         TRACE_ENTRY();
2356
2357         q2t_proc_log_entry_clean(&tgt_template);
2358
2359         scst_unregister_target_template(&tgt_template);
2360
2361         qla2xxx_tgt_unregister_driver();
2362
2363         kmem_cache_destroy(q2t_cmd_cachep);
2364
2365         TRACE_EXIT();
2366         return;
2367 }
2368
2369 module_init(q2t_init);
2370 module_exit(q2t_exit);
2371
2372 MODULE_AUTHOR("Vladislav Bolkhovitin & Leonid Stoljar & Nathaniel Clark");
2373 MODULE_DESCRIPTION("Target mode logic for qla2xxx");
2374 MODULE_LICENSE("GPL");
2375 MODULE_VERSION(Q2T_VERSION_STRING);