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