Merge from vendor branch GROFF:
[dragonfly.git] / sys / bus / cam / scsi / scsi_targ_bh.c
1 /*
2  * Implementation of the Target Mode 'Black Hole device' for CAM.
3  *
4  * Copyright (c) 1999 Justin T. Gibbs.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions, and the following disclaimer,
12  *    without modification, immediately at the beginning of the file.
13  * 2. The name of the author may not be used to endorse or promote products
14  *    derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
20  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  *
28  * $FreeBSD: src/sys/cam/scsi/scsi_targ_bh.c,v 1.4.2.6 2003/11/14 11:31:25 simokawa Exp $
29  * $DragonFly: src/sys/bus/cam/scsi/scsi_targ_bh.c,v 1.9 2005/06/02 20:40:31 dillon Exp $
30  */
31 #include <sys/param.h>
32 #include <sys/queue.h>
33 #include <sys/systm.h>
34 #include <sys/kernel.h>
35 #include <sys/types.h>
36 #include <sys/buf.h>
37 #include <sys/conf.h>
38 #include <sys/devicestat.h>
39 #include <sys/malloc.h>
40 #include <sys/uio.h>
41
42 #include "../cam.h"
43 #include "../cam_ccb.h"
44 #include "../cam_extend.h"
45 #include "../cam_periph.h"
46 #include "../cam_queue.h"
47 #include "../cam_xpt_periph.h"
48 #include "../cam_debug.h"
49
50 #include "scsi_all.h"
51 #include "scsi_message.h"
52
53 typedef enum {
54         TARGBH_STATE_NORMAL,
55         TARGBH_STATE_EXCEPTION,
56         TARGBH_STATE_TEARDOWN
57 } targbh_state;
58
59 typedef enum {
60         TARGBH_FLAG_NONE         = 0x00,
61         TARGBH_FLAG_LUN_ENABLED  = 0x01
62 } targbh_flags;
63
64 typedef enum {
65         TARGBH_CCB_WORKQ,
66         TARGBH_CCB_WAITING
67 } targbh_ccb_types;
68
69 #define MAX_ACCEPT      8
70 #define MAX_IMMEDIATE   16
71 #define MAX_BUF_SIZE    256     /* Max inquiry/sense/mode page transfer */
72
73 /* Offsets into our private CCB area for storing accept information */
74 #define ccb_type        ppriv_field0
75 #define ccb_descr       ppriv_ptr1
76
77 /* We stick a pointer to the originating accept TIO in each continue I/O CCB */
78 #define ccb_atio        ppriv_ptr1
79
80 TAILQ_HEAD(ccb_queue, ccb_hdr);
81
82 struct targbh_softc {
83         struct          ccb_queue pending_queue;
84         struct          ccb_queue work_queue;
85         struct          ccb_queue unknown_atio_queue;
86         struct          devstat device_stats;
87         targbh_state    state;
88         targbh_flags    flags;  
89         u_int           init_level;
90         u_int           inq_data_len;
91         struct          ccb_accept_tio *accept_tio_list;
92         struct          ccb_hdr_slist immed_notify_slist;
93 };
94
95 struct targbh_cmd_desc {
96         struct    ccb_accept_tio* atio_link;
97         u_int     data_resid;   /* How much left to transfer */
98         u_int     data_increment;/* Amount to send before next disconnect */
99         void*     data;         /* The data. Can be from backing_store or not */
100         void*     backing_store;/* Backing store allocated for this descriptor*/
101         u_int     max_size;     /* Size of backing_store */
102         u_int32_t timeout;      
103         u_int8_t  status;       /* Status to return to initiator */
104 };
105
106 static struct scsi_inquiry_data no_lun_inq_data =
107 {
108         T_NODEVICE | (SID_QUAL_BAD_LU << 5), 0,
109         /* version */2, /* format version */2
110 };
111
112 static struct scsi_sense_data no_lun_sense_data =
113 {
114         SSD_CURRENT_ERROR|SSD_ERRCODE_VALID,
115         0,
116         SSD_KEY_NOT_READY, 
117         { 0, 0, 0, 0 },
118         /*extra_len*/offsetof(struct scsi_sense_data, fru)
119                    - offsetof(struct scsi_sense_data, extra_len),
120         { 0, 0, 0, 0 },
121         /* Logical Unit Not Supported */
122         /*ASC*/0x25, /*ASCQ*/0
123 };
124
125 static const int request_sense_size = offsetof(struct scsi_sense_data, fru);
126
127 static periph_init_t    targbhinit;
128 static void             targbhasync(void *callback_arg, u_int32_t code,
129                                     struct cam_path *path, void *arg);
130 static cam_status       targbhenlun(struct cam_periph *periph);
131 static cam_status       targbhdislun(struct cam_periph *periph);
132 static periph_ctor_t    targbhctor;
133 static periph_dtor_t    targbhdtor;
134 static periph_start_t   targbhstart;
135 static void             targbhdone(struct cam_periph *periph,
136                                    union ccb *done_ccb);
137 #ifdef NOTYET
138 static  int             targbherror(union ccb *ccb, u_int32_t cam_flags,
139                                     u_int32_t sense_flags);
140 #endif
141 static struct targbh_cmd_desc*  targbhallocdescr(void);
142 static void             targbhfreedescr(struct targbh_cmd_desc *buf);
143                                         
144 static struct periph_driver targbhdriver =
145 {
146         targbhinit, "targbh",
147         TAILQ_HEAD_INITIALIZER(targbhdriver.units), /* generation */ 0
148 };
149
150 DATA_SET(periphdriver_set, targbhdriver);
151
152 static void
153 targbhinit(void)
154 {
155         cam_status status;
156         struct cam_path *path;
157
158         /*
159          * Install a global async callback.  This callback will
160          * receive async callbacks like "new path registered".
161          */
162         status = xpt_create_path(&path, /*periph*/NULL, CAM_XPT_PATH_ID,
163                                  CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
164
165         if (status == CAM_REQ_CMP) {
166                 struct ccb_setasync csa;
167
168                 xpt_setup_ccb(&csa.ccb_h, path, /*priority*/5);
169                 csa.ccb_h.func_code = XPT_SASYNC_CB;
170                 csa.event_enable = AC_PATH_REGISTERED | AC_PATH_DEREGISTERED;
171                 csa.callback = targbhasync;
172                 csa.callback_arg = NULL;
173                 xpt_action((union ccb *)&csa);
174                 status = csa.ccb_h.status;
175                 xpt_free_path(path);
176         }
177
178         if (status != CAM_REQ_CMP) {
179                 printf("targbh: Failed to attach master async callback "
180                        "due to status 0x%x!\n", status);
181         }
182 }
183
184 static void
185 targbhasync(void *callback_arg, u_int32_t code,
186             struct cam_path *path, void *arg)
187 {
188         struct cam_path *new_path;
189         struct ccb_pathinq *cpi;
190         path_id_t bus_path_id;
191         cam_status status;
192
193         cpi = (struct ccb_pathinq *)arg;
194         if (code == AC_PATH_REGISTERED)
195                 bus_path_id = cpi->ccb_h.path_id;
196         else
197                 bus_path_id = xpt_path_path_id(path);
198         /*
199          * Allocate a peripheral instance for
200          * this target instance.
201          */
202         status = xpt_create_path(&new_path, NULL,
203                                  bus_path_id,
204                                  CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
205         if (status != CAM_REQ_CMP) {
206                 printf("targbhasync: Unable to create path "
207                         "due to status 0x%x\n", status);
208                 return;
209         }
210
211         switch (code) {
212         case AC_PATH_REGISTERED:
213         {
214                 /* Only attach to controllers that support target mode */
215                 if ((cpi->target_sprt & PIT_PROCESSOR) == 0)
216                         break;
217
218                 status = cam_periph_alloc(targbhctor, NULL, targbhdtor,
219                                           targbhstart,
220                                           "targbh", CAM_PERIPH_BIO,
221                                           new_path, targbhasync,
222                                           AC_PATH_REGISTERED,
223                                           cpi);
224                 break;
225         }
226         case AC_PATH_DEREGISTERED:
227         {
228                 struct cam_periph *periph;
229
230                 if ((periph = cam_periph_find(new_path, "targbh")) != NULL)
231                         cam_periph_invalidate(periph);
232                 break;
233         }
234         default:
235                 break;
236         }
237         xpt_free_path(new_path);
238 }
239
240 /* Attempt to enable our lun */
241 static cam_status
242 targbhenlun(struct cam_periph *periph)
243 {
244         union ccb immed_ccb;
245         struct targbh_softc *softc;
246         cam_status status;
247         int i;
248
249         softc = (struct targbh_softc *)periph->softc;
250
251         if ((softc->flags & TARGBH_FLAG_LUN_ENABLED) != 0)
252                 return (CAM_REQ_CMP);
253
254         xpt_setup_ccb(&immed_ccb.ccb_h, periph->path, /*priority*/1);
255         immed_ccb.ccb_h.func_code = XPT_EN_LUN;
256
257         /* Don't need support for any vendor specific commands */
258         immed_ccb.cel.grp6_len = 0;
259         immed_ccb.cel.grp7_len = 0;
260         immed_ccb.cel.enable = 1;
261         xpt_action(&immed_ccb);
262         status = immed_ccb.ccb_h.status;
263         if (status != CAM_REQ_CMP) {
264                 xpt_print_path(periph->path);
265                 printf("targbhenlun - Enable Lun Rejected with status 0x%x\n",
266                        status);
267                 return (status);
268         }
269         
270         softc->flags |= TARGBH_FLAG_LUN_ENABLED;
271
272         /*
273          * Build up a buffer of accept target I/O
274          * operations for incoming selections.
275          */
276         for (i = 0; i < MAX_ACCEPT; i++) {
277                 struct ccb_accept_tio *atio;
278
279                 atio = malloc(sizeof(*atio), M_DEVBUF, M_INTWAIT);
280
281                 atio->ccb_h.ccb_descr = targbhallocdescr();
282
283                 xpt_setup_ccb(&atio->ccb_h, periph->path, /*priority*/1);
284                 atio->ccb_h.func_code = XPT_ACCEPT_TARGET_IO;
285                 atio->ccb_h.cbfcnp = targbhdone;
286                 xpt_action((union ccb *)atio);
287                 status = atio->ccb_h.status;
288                 if (status != CAM_REQ_INPROG) {
289                         targbhfreedescr(atio->ccb_h.ccb_descr);
290                         free(atio, M_DEVBUF);
291                         break;
292                 }
293                 ((struct targbh_cmd_desc*)atio->ccb_h.ccb_descr)->atio_link =
294                     softc->accept_tio_list;
295                 softc->accept_tio_list = atio;
296         }
297
298         if (i == 0) {
299                 xpt_print_path(periph->path);
300                 printf("targbhenlun - Could not allocate accept tio CCBs: "
301                        "status = 0x%x\n", status);
302                 targbhdislun(periph);
303                 return (CAM_REQ_CMP_ERR);
304         }
305
306         /*
307          * Build up a buffer of immediate notify CCBs
308          * so the SIM can tell us of asynchronous target mode events.
309          */
310         for (i = 0; i < MAX_ACCEPT; i++) {
311                 struct ccb_immed_notify *inot;
312
313                 inot = malloc(sizeof(*inot), M_DEVBUF, M_INTWAIT);
314
315                 xpt_setup_ccb(&inot->ccb_h, periph->path, /*priority*/1);
316                 inot->ccb_h.func_code = XPT_IMMED_NOTIFY;
317                 inot->ccb_h.cbfcnp = targbhdone;
318                 xpt_action((union ccb *)inot);
319                 status = inot->ccb_h.status;
320                 if (status != CAM_REQ_INPROG) {
321                         free(inot, M_DEVBUF);
322                         break;
323                 }
324                 SLIST_INSERT_HEAD(&softc->immed_notify_slist, &inot->ccb_h,
325                                   periph_links.sle);
326         }
327
328         if (i == 0) {
329                 xpt_print_path(periph->path);
330                 printf("targbhenlun - Could not allocate immediate notify "
331                        "CCBs: status = 0x%x\n", status);
332                 targbhdislun(periph);
333                 return (CAM_REQ_CMP_ERR);
334         }
335
336         return (CAM_REQ_CMP);
337 }
338
339 static cam_status
340 targbhdislun(struct cam_periph *periph)
341 {
342         union ccb ccb;
343         struct targbh_softc *softc;
344         struct ccb_accept_tio* atio;
345         struct ccb_hdr *ccb_h;
346
347         softc = (struct targbh_softc *)periph->softc;
348         if ((softc->flags & TARGBH_FLAG_LUN_ENABLED) == 0)
349                 return CAM_REQ_CMP;
350
351         /* XXX Block for Continue I/O completion */
352
353         /* Kill off all ACCECPT and IMMEDIATE CCBs */
354         while ((atio = softc->accept_tio_list) != NULL) {
355                 
356                 softc->accept_tio_list =
357                     ((struct targbh_cmd_desc*)atio->ccb_h.ccb_descr)->atio_link;
358                 xpt_setup_ccb(&ccb.cab.ccb_h, periph->path, /*priority*/1);
359                 ccb.cab.ccb_h.func_code = XPT_ABORT;
360                 ccb.cab.abort_ccb = (union ccb *)atio;
361                 xpt_action(&ccb);
362         }
363
364         while ((ccb_h = SLIST_FIRST(&softc->immed_notify_slist)) != NULL) {
365                 SLIST_REMOVE_HEAD(&softc->immed_notify_slist, periph_links.sle);
366                 xpt_setup_ccb(&ccb.cab.ccb_h, periph->path, /*priority*/1);
367                 ccb.cab.ccb_h.func_code = XPT_ABORT;
368                 ccb.cab.abort_ccb = (union ccb *)ccb_h;
369                 xpt_action(&ccb);
370         }
371
372         /*
373          * Dissable this lun.
374          */
375         xpt_setup_ccb(&ccb.cel.ccb_h, periph->path, /*priority*/1);
376         ccb.cel.ccb_h.func_code = XPT_EN_LUN;
377         ccb.cel.enable = 0;
378         xpt_action(&ccb);
379
380         if (ccb.cel.ccb_h.status != CAM_REQ_CMP)
381                 printf("targbhdislun - Disabling lun on controller failed "
382                        "with status 0x%x\n", ccb.cel.ccb_h.status);
383         else 
384                 softc->flags &= ~TARGBH_FLAG_LUN_ENABLED;
385         return (ccb.cel.ccb_h.status);
386 }
387
388 static cam_status
389 targbhctor(struct cam_periph *periph, void *arg)
390 {
391         struct ccb_pathinq *cpi;
392         struct targbh_softc *softc;
393
394         cpi = (struct ccb_pathinq *)arg;
395
396         /* Allocate our per-instance private storage */
397         softc = malloc(sizeof(*softc), M_DEVBUF, M_INTWAIT | M_ZERO);
398         TAILQ_INIT(&softc->pending_queue);
399         TAILQ_INIT(&softc->work_queue);
400         softc->accept_tio_list = NULL;
401         SLIST_INIT(&softc->immed_notify_slist);
402         softc->state = TARGBH_STATE_NORMAL;
403         periph->softc = softc;
404         softc->init_level++;
405
406         return (targbhenlun(periph));
407 }
408
409 static void
410 targbhdtor(struct cam_periph *periph)
411 {
412         struct targbh_softc *softc;
413
414         softc = (struct targbh_softc *)periph->softc;
415
416         softc->state = TARGBH_STATE_TEARDOWN;
417
418         targbhdislun(periph);
419
420         switch (softc->init_level) {
421         case 0:
422                 panic("targdtor - impossible init level");;
423         case 1:
424                 /* FALLTHROUGH */
425         default:
426                 /* XXX Wait for callback of targbhdislun() */
427                 tsleep(softc, 0, "targbh", hz/2);
428                 free(softc, M_DEVBUF);
429                 break;
430         }
431 }
432
433 static void
434 targbhstart(struct cam_periph *periph, union ccb *start_ccb)
435 {
436         struct targbh_softc *softc;
437         struct ccb_hdr *ccbh;
438         struct ccb_accept_tio *atio;
439         struct targbh_cmd_desc *desc;
440         struct ccb_scsiio *csio;
441         ccb_flags flags;
442
443         softc = (struct targbh_softc *)periph->softc;
444         
445         crit_enter();
446         ccbh = TAILQ_FIRST(&softc->work_queue);
447         if (periph->immediate_priority <= periph->pinfo.priority) {
448                 start_ccb->ccb_h.ccb_type = TARGBH_CCB_WAITING;                 
449                 SLIST_INSERT_HEAD(&periph->ccb_list, &start_ccb->ccb_h,
450                                   periph_links.sle);
451                 periph->immediate_priority = CAM_PRIORITY_NONE;
452                 crit_exit();
453                 wakeup(&periph->ccb_list);
454         } else if (ccbh == NULL) {
455                 crit_exit();
456                 xpt_release_ccb(start_ccb);     
457         } else {
458                 TAILQ_REMOVE(&softc->work_queue, ccbh, periph_links.tqe);
459                 TAILQ_INSERT_HEAD(&softc->pending_queue, ccbh,
460                                   periph_links.tqe);
461                 crit_exit();
462                 atio = (struct ccb_accept_tio*)ccbh;
463                 desc = (struct targbh_cmd_desc *)atio->ccb_h.ccb_descr;
464
465                 /* Is this a tagged request? */
466                 flags = atio->ccb_h.flags & (CAM_TAG_ACTION_VALID|CAM_DIR_MASK);
467
468                 csio = &start_ccb->csio;
469                 /*
470                  * If we are done with the transaction, tell the
471                  * controller to send status and perform a CMD_CMPLT.
472                  * If we have associated sense data, see if we can
473                  * send that too.
474                  */
475                 if (desc->data_resid == desc->data_increment) {
476                         flags |= CAM_SEND_STATUS;
477                         if (atio->sense_len) {
478                                 csio->sense_len = atio->sense_len;
479                                 csio->sense_data = atio->sense_data;
480                                 flags |= CAM_SEND_SENSE;
481                         }
482
483                 }
484
485                 cam_fill_ctio(csio,
486                               /*retries*/2,
487                               targbhdone,
488                               flags,
489                               (flags & CAM_TAG_ACTION_VALID)?
490                                 MSG_SIMPLE_Q_TAG : 0,
491                               atio->tag_id,
492                               atio->init_id,
493                               desc->status,
494                               /*data_ptr*/desc->data_increment == 0
495                                           ? NULL : desc->data,
496                               /*dxfer_len*/desc->data_increment,
497                               /*timeout*/desc->timeout);
498
499                 /* Override our wildcard attachment */
500                 start_ccb->ccb_h.target_id = atio->ccb_h.target_id;
501                 start_ccb->ccb_h.target_lun = atio->ccb_h.target_lun;
502
503                 start_ccb->ccb_h.ccb_type = TARGBH_CCB_WORKQ;
504                 start_ccb->ccb_h.ccb_atio = atio;
505                 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
506                           ("Sending a CTIO\n"));
507                 xpt_action(start_ccb);
508                 crit_enter();
509                 ccbh = TAILQ_FIRST(&softc->work_queue);
510                 crit_exit();
511         }
512         if (ccbh != NULL)
513                 xpt_schedule(periph, /*priority*/1);
514 }
515
516 static void
517 targbhdone(struct cam_periph *periph, union ccb *done_ccb)
518 {
519         struct targbh_softc *softc;
520
521         softc = (struct targbh_softc *)periph->softc;
522
523         if (done_ccb->ccb_h.ccb_type == TARGBH_CCB_WAITING) {
524                 /* Caller will release the CCB */
525                 wakeup(&done_ccb->ccb_h.cbfcnp);
526                 return;
527         }
528
529         switch (done_ccb->ccb_h.func_code) {
530         case XPT_ACCEPT_TARGET_IO:
531         {
532                 struct ccb_accept_tio *atio;
533                 struct targbh_cmd_desc *descr;
534                 u_int8_t *cdb;
535
536                 atio = &done_ccb->atio;
537                 descr = (struct targbh_cmd_desc*)atio->ccb_h.ccb_descr;
538                 cdb = atio->cdb_io.cdb_bytes;
539                 if (softc->state == TARGBH_STATE_TEARDOWN
540                  || atio->ccb_h.status == CAM_REQ_ABORTED) {
541                         targbhfreedescr(descr);
542                         free(done_ccb, M_DEVBUF);
543                         return;
544                 }
545
546                 /*
547                  * Determine the type of incoming command and
548                  * setup our buffer for a response.
549                  */
550                 switch (cdb[0]) {
551                 case INQUIRY:
552                 {
553                         struct scsi_inquiry *inq;
554
555                         inq = (struct scsi_inquiry *)cdb;
556                         CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
557                                   ("Saw an inquiry!\n"));
558                         /*
559                          * Validate the command.  We don't
560                          * support any VPD pages, so complain
561                          * if EVPD is set.
562                          */
563                         if ((inq->byte2 & SI_EVPD) != 0
564                          || inq->page_code != 0) {
565                                 atio->ccb_h.flags &= ~CAM_DIR_MASK;
566                                 atio->ccb_h.flags |= CAM_DIR_NONE;
567                                 /*
568                                  * This needs to have other than a
569                                  * no_lun_sense_data response.
570                                  */
571                                 atio->sense_data = no_lun_sense_data;
572                                 atio->sense_len = sizeof(no_lun_sense_data);
573                                 descr->data_resid = 0;
574                                 descr->data_increment = 0;
575                                 descr->status = SCSI_STATUS_CHECK_COND;
576                                 break;
577                         }
578                         /*
579                          * Direction is always relative
580                          * to the initator.
581                          */
582                         atio->ccb_h.flags &= ~CAM_DIR_MASK;
583                         atio->ccb_h.flags |= CAM_DIR_IN;
584                         descr->data = &no_lun_inq_data;
585                         descr->data_resid = MIN(sizeof(no_lun_inq_data),
586                                                 SCSI_CDB6_LEN(inq->length));
587                         descr->data_increment = descr->data_resid;
588                         descr->timeout = 5 * 1000;
589                         descr->status = SCSI_STATUS_OK;
590                         break;
591                 }
592                 case REQUEST_SENSE:
593                 {
594                         struct scsi_request_sense *rsense;
595
596                         rsense = (struct scsi_request_sense *)cdb;
597                         /* Refer to static sense data */
598                         atio->ccb_h.flags &= ~CAM_DIR_MASK;
599                         atio->ccb_h.flags |= CAM_DIR_IN;
600                         descr->data = &no_lun_sense_data;
601                         descr->data_resid = request_sense_size;
602                         descr->data_resid = MIN(descr->data_resid,
603                                                 SCSI_CDB6_LEN(rsense->length));
604                         descr->data_increment = descr->data_resid;
605                         descr->timeout = 5 * 1000;
606                         descr->status = SCSI_STATUS_OK;
607                         break;
608                 }
609                 default:
610                         /* Constant CA, tell initiator */
611                         /* Direction is always relative to the initator */
612                         atio->ccb_h.flags &= ~CAM_DIR_MASK;
613                         atio->ccb_h.flags |= CAM_DIR_NONE;
614                         atio->sense_data = no_lun_sense_data;
615                         atio->sense_len = sizeof (no_lun_sense_data);
616                         descr->data_resid = 0;
617                         descr->data_increment = 0;
618                         descr->timeout = 5 * 1000;
619                         descr->status = SCSI_STATUS_CHECK_COND;
620                         break;
621                 }
622
623                 /* Queue us up to receive a Continue Target I/O ccb. */
624                 TAILQ_INSERT_TAIL(&softc->work_queue, &atio->ccb_h,
625                                   periph_links.tqe);
626                 xpt_schedule(periph, /*priority*/1);
627                 break;
628         }
629         case XPT_CONT_TARGET_IO:
630         {
631                 struct ccb_accept_tio *atio;
632                 struct targbh_cmd_desc *desc;
633
634                 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
635                           ("Received completed CTIO\n"));
636                 atio = (struct ccb_accept_tio*)done_ccb->ccb_h.ccb_atio;
637                 desc = (struct targbh_cmd_desc *)atio->ccb_h.ccb_descr;
638
639                 TAILQ_REMOVE(&softc->pending_queue, &atio->ccb_h,
640                              periph_links.tqe);
641
642                 /*
643                  * We could check for CAM_SENT_SENSE bein set here,
644                  * but since we're not maintaining any CA/UA state,
645                  * there's no point.
646                  */
647                 atio->sense_len = 0;
648                 done_ccb->ccb_h.flags &= ~CAM_SEND_SENSE;
649                 done_ccb->ccb_h.status &= ~CAM_SENT_SENSE;
650
651                 /* XXX Check for errors */
652                 desc->data_resid -= desc->data_increment;
653                 xpt_release_ccb(done_ccb);
654                 if (softc->state != TARGBH_STATE_TEARDOWN) {
655
656                         /*
657                          * Send the original accept TIO back to the
658                          * controller to handle more work.
659                          */
660                         CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
661                                   ("Returning ATIO to target\n"));
662                         /* Restore wildcards */
663                         atio->ccb_h.target_id = CAM_TARGET_WILDCARD;
664                         atio->ccb_h.target_lun = CAM_LUN_WILDCARD;
665                         xpt_action((union ccb *)atio);
666                         break;
667                 } else {
668                         targbhfreedescr(desc);
669                         free(atio, M_DEVBUF);
670                 }
671                 break;
672         }
673         case XPT_IMMED_NOTIFY:
674         {
675                 if (softc->state == TARGBH_STATE_TEARDOWN
676                  || done_ccb->ccb_h.status == CAM_REQ_ABORTED) {
677                         printf("Freed an immediate notify\n");
678                         free(done_ccb, M_DEVBUF);
679                 }
680                 break;
681         }
682         default:
683                 panic("targbhdone: Unexpected ccb opcode");
684                 break;
685         }
686 }
687
688 #ifdef NOTYET
689 static int
690 targbherror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags)
691 {
692         return 0;
693 }
694 #endif
695
696 static struct targbh_cmd_desc*
697 targbhallocdescr()
698 {
699         struct targbh_cmd_desc* descr;
700
701         /* Allocate the targbh_descr structure */
702         descr = malloc(sizeof(*descr), M_DEVBUF, M_INTWAIT | M_ZERO);
703
704         /* Allocate buffer backing store */
705         descr->backing_store = malloc(MAX_BUF_SIZE, M_DEVBUF, M_INTWAIT);
706         descr->max_size = MAX_BUF_SIZE;
707         return (descr);
708 }
709
710 static void
711 targbhfreedescr(struct targbh_cmd_desc *descr)
712 {
713         free(descr->backing_store, M_DEVBUF);
714         free(descr, M_DEVBUF);
715 }