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