]> CyberLeo.Net >> Repos - FreeBSD/stable/10.git/blob - sys/cam/ctl/ctl_frontend_cam_sim.c
MFC r352554:
[FreeBSD/stable/10.git] / sys / cam / ctl / ctl_frontend_cam_sim.c
1 /*-
2  * Copyright (c) 2009 Silicon Graphics International Corp.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions, and the following disclaimer,
10  *    without modification.
11  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
12  *    substantially similar to the "NO WARRANTY" disclaimer below
13  *    ("Disclaimer") and any redistribution must be conditioned upon
14  *    including a substantially similar Disclaimer requirement for further
15  *    binary redistribution.
16  *
17  * NO WARRANTY
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
27  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGES.
29  *
30  * $Id: //depot/users/kenm/FreeBSD-test2/sys/cam/ctl/ctl_frontend_cam_sim.c#4 $
31  */
32 /*
33  * CTL frontend to CAM SIM interface.  This allows access to CTL LUNs via
34  * the da(4) and pass(4) drivers from inside the system.
35  *
36  * Author: Ken Merry <ken@FreeBSD.org>
37  */
38
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD$");
41
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/kernel.h>
45 #include <sys/types.h>
46 #include <sys/malloc.h>
47 #include <sys/lock.h>
48 #include <sys/mutex.h>
49 #include <sys/condvar.h>
50 #include <sys/queue.h>
51 #include <sys/bus.h>
52 #include <sys/sysctl.h>
53 #include <machine/bus.h>
54 #include <sys/sbuf.h>
55
56 #include <cam/cam.h>
57 #include <cam/cam_ccb.h>
58 #include <cam/cam_sim.h>
59 #include <cam/cam_xpt_sim.h>
60 #include <cam/cam_xpt.h>
61 #include <cam/cam_periph.h>
62 #include <cam/scsi/scsi_all.h>
63 #include <cam/scsi/scsi_message.h>
64 #include <cam/ctl/ctl_io.h>
65 #include <cam/ctl/ctl.h>
66 #include <cam/ctl/ctl_frontend.h>
67 #include <cam/ctl/ctl_debug.h>
68
69 #define io_ptr          spriv_ptr1
70
71 struct cfcs_io {
72         union ccb *ccb;
73 };
74
75 struct cfcs_softc {
76         struct ctl_port port;
77         char port_name[32];
78         struct cam_sim *sim;
79         struct cam_devq *devq;
80         struct cam_path *path;
81         struct mtx lock;
82         uint64_t wwnn;
83         uint64_t wwpn;
84         uint32_t cur_tag_num;
85         int online;
86 };
87
88 /*
89  * We can't handle CCBs with these flags.  For the most part, we just don't
90  * handle physical addresses yet.  That would require mapping things in
91  * order to do the copy.
92  */
93 #define CFCS_BAD_CCB_FLAGS (CAM_DATA_ISPHYS | CAM_MSG_BUF_PHYS |        \
94         CAM_SNS_BUF_PHYS | CAM_CDB_PHYS | CAM_SENSE_PTR |               \
95         CAM_SENSE_PHYS)
96
97 static int cfcs_init(void);
98 static int cfcs_shutdown(void);
99 static void cfcs_poll(struct cam_sim *sim);
100 static void cfcs_online(void *arg);
101 static void cfcs_offline(void *arg);
102 static void cfcs_datamove(union ctl_io *io);
103 static void cfcs_done(union ctl_io *io);
104 void cfcs_action(struct cam_sim *sim, union ccb *ccb);
105
106 struct cfcs_softc cfcs_softc;
107 /*
108  * This is primarily intended to allow for error injection to test the CAM
109  * sense data and sense residual handling code.  This sets the maximum
110  * amount of SCSI sense data that we will report to CAM.
111  */
112 static int cfcs_max_sense = sizeof(struct scsi_sense_data);
113
114 SYSCTL_NODE(_kern_cam, OID_AUTO, ctl2cam, CTLFLAG_RD, 0,
115             "CAM Target Layer SIM frontend");
116 SYSCTL_INT(_kern_cam_ctl2cam, OID_AUTO, max_sense, CTLFLAG_RW,
117            &cfcs_max_sense, 0, "Maximum sense data size");
118
119 static struct ctl_frontend cfcs_frontend =
120 {
121         .name = "camsim",
122         .init = cfcs_init,
123         .shutdown = cfcs_shutdown,
124 };
125 CTL_FRONTEND_DECLARE(ctlcfcs, cfcs_frontend);
126
127 static int
128 cfcs_init(void)
129 {
130         struct cfcs_softc *softc;
131         struct ctl_port *port;
132         int retval;
133
134         softc = &cfcs_softc;
135         bzero(softc, sizeof(*softc));
136         mtx_init(&softc->lock, "ctl2cam", NULL, MTX_DEF);
137         port = &softc->port;
138
139         port->frontend = &cfcs_frontend;
140         port->port_type = CTL_PORT_INTERNAL;
141         /* XXX KDM what should the real number be here? */
142         port->num_requested_ctl_io = 4096;
143         snprintf(softc->port_name, sizeof(softc->port_name), "camsim");
144         port->port_name = softc->port_name;
145         port->port_online = cfcs_online;
146         port->port_offline = cfcs_offline;
147         port->onoff_arg = softc;
148         port->fe_datamove = cfcs_datamove;
149         port->fe_done = cfcs_done;
150
151         /* XXX KDM what should we report here? */
152         /* XXX These should probably be fetched from CTL. */
153         port->max_targets = 1;
154         port->max_target_id = 15;
155         port->targ_port = -1;
156
157         retval = ctl_port_register(port);
158         if (retval != 0) {
159                 printf("%s: ctl_port_register() failed with error %d!\n",
160                        __func__, retval);
161                 mtx_destroy(&softc->lock);
162                 return (retval);
163         }
164
165         /*
166          * If the CTL frontend didn't tell us what our WWNN/WWPN is, go
167          * ahead and set something random.
168          */
169         if (port->wwnn == 0) {
170                 uint64_t random_bits;
171
172                 arc4rand(&random_bits, sizeof(random_bits), 0);
173                 softc->wwnn = (random_bits & 0x0000000fffffff00ULL) |
174                         /* Company ID */ 0x5000000000000000ULL |
175                         /* NL-Port */    0x0300;
176                 softc->wwpn = softc->wwnn + port->targ_port + 1;
177                 ctl_port_set_wwns(port, true, softc->wwnn, true, softc->wwpn);
178         } else {
179                 softc->wwnn = port->wwnn;
180                 softc->wwpn = port->wwpn;
181         }
182
183         mtx_lock(&softc->lock);
184         softc->devq = cam_simq_alloc(port->num_requested_ctl_io);
185         if (softc->devq == NULL) {
186                 printf("%s: error allocating devq\n", __func__);
187                 retval = ENOMEM;
188                 goto bailout;
189         }
190
191         softc->sim = cam_sim_alloc(cfcs_action, cfcs_poll, softc->port_name,
192                                    softc, /*unit*/ 0, &softc->lock, 1,
193                                    port->num_requested_ctl_io, softc->devq);
194         if (softc->sim == NULL) {
195                 printf("%s: error allocating SIM\n", __func__);
196                 retval = ENOMEM;
197                 goto bailout;
198         }
199
200         if (xpt_bus_register(softc->sim, NULL, 0) != CAM_SUCCESS) {
201                 printf("%s: error registering SIM\n", __func__);
202                 retval = ENOMEM;
203                 goto bailout;
204         }
205
206         if (xpt_create_path(&softc->path, /*periph*/NULL,
207                             cam_sim_path(softc->sim),
208                             CAM_TARGET_WILDCARD,
209                             CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
210                 printf("%s: error creating path\n", __func__);
211                 xpt_bus_deregister(cam_sim_path(softc->sim));
212                 retval = EINVAL;
213                 goto bailout;
214         }
215
216         mtx_unlock(&softc->lock);
217
218         return (retval);
219
220 bailout:
221         if (softc->sim)
222                 cam_sim_free(softc->sim, /*free_devq*/ TRUE);
223         else if (softc->devq)
224                 cam_simq_free(softc->devq);
225         mtx_unlock(&softc->lock);
226         mtx_destroy(&softc->lock);
227
228         return (retval);
229 }
230
231 static int
232 cfcs_shutdown(void)
233 {
234         struct cfcs_softc *softc = &cfcs_softc;
235         struct ctl_port *port = &softc->port;
236         int error;
237
238         ctl_port_offline(port);
239
240         mtx_lock(&softc->lock);
241         xpt_free_path(softc->path);
242         xpt_bus_deregister(cam_sim_path(softc->sim));
243         cam_sim_free(softc->sim, /*free_devq*/ TRUE);
244         mtx_unlock(&softc->lock);
245         mtx_destroy(&softc->lock);
246
247         if ((error = ctl_port_deregister(port)) != 0)
248                 printf("%s: cam_sim port deregistration failed\n", __func__);
249         return (error);
250 }
251
252 static void
253 cfcs_poll(struct cam_sim *sim)
254 {
255
256 }
257
258 static void
259 cfcs_onoffline(void *arg, int online)
260 {
261         struct cfcs_softc *softc;
262         union ccb *ccb;
263
264         softc = (struct cfcs_softc *)arg;
265
266         mtx_lock(&softc->lock);
267         softc->online = online;
268
269         ccb = xpt_alloc_ccb_nowait();
270         if (ccb == NULL) {
271                 printf("%s: unable to allocate CCB for rescan\n", __func__);
272                 goto bailout;
273         }
274
275         if (xpt_create_path(&ccb->ccb_h.path, NULL,
276                             cam_sim_path(softc->sim), CAM_TARGET_WILDCARD,
277                             CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
278                 printf("%s: can't allocate path for rescan\n", __func__);
279                 xpt_free_ccb(ccb);
280                 goto bailout;
281         }
282         xpt_rescan(ccb);
283
284 bailout:
285         mtx_unlock(&softc->lock);
286 }
287
288 static void
289 cfcs_online(void *arg)
290 {
291         cfcs_onoffline(arg, /*online*/ 1);
292 }
293
294 static void
295 cfcs_offline(void *arg)
296 {
297         cfcs_onoffline(arg, /*online*/ 0);
298 }
299
300 /*
301  * This function is very similar to ctl_ioctl_do_datamove().  Is there a
302  * way to combine the functionality?
303  *
304  * XXX KDM may need to move this into a thread.  We're doing a bcopy in the
305  * caller's context, which will usually be the backend.  That may not be a
306  * good thing.
307  */
308 static void
309 cfcs_datamove(union ctl_io *io)
310 {
311         union ccb *ccb;
312         bus_dma_segment_t cam_sg_entry, *cam_sglist;
313         struct ctl_sg_entry ctl_sg_entry, *ctl_sglist;
314         int cam_sg_count, ctl_sg_count, cam_sg_start;
315         int cam_sg_offset;
316         int len_to_copy;
317         int ctl_watermark, cam_watermark;
318         int i, j;
319
320         ccb = io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr;
321
322         /*
323          * Note that we have a check in cfcs_action() to make sure that any
324          * CCBs with "bad" flags are returned with CAM_REQ_INVALID.  This
325          * is just to make sure no one removes that check without updating
326          * this code to provide the additional functionality necessary to
327          * support those modes of operation.
328          */
329         KASSERT(((ccb->ccb_h.flags & CFCS_BAD_CCB_FLAGS) == 0), ("invalid "
330                   "CAM flags %#x", (ccb->ccb_h.flags & CFCS_BAD_CCB_FLAGS)));
331
332         /*
333          * Simplify things on both sides by putting single buffers into a
334          * single entry S/G list.
335          */
336         switch ((ccb->ccb_h.flags & CAM_DATA_MASK)) {
337         case CAM_DATA_SG: {
338                 int len_seen;
339
340                 cam_sglist = (bus_dma_segment_t *)ccb->csio.data_ptr;
341                 cam_sg_count = ccb->csio.sglist_cnt;
342                 cam_sg_start = cam_sg_count;
343                 cam_sg_offset = 0;
344
345                 for (i = 0, len_seen = 0; i < cam_sg_count; i++) {
346                         if ((len_seen + cam_sglist[i].ds_len) >=
347                              io->scsiio.kern_rel_offset) {
348                                 cam_sg_start = i;
349                                 cam_sg_offset = io->scsiio.kern_rel_offset -
350                                         len_seen;
351                                 break;
352                         }
353                         len_seen += cam_sglist[i].ds_len;
354                 }
355                 break;
356         }
357         case CAM_DATA_VADDR:
358                 cam_sglist = &cam_sg_entry;
359                 cam_sglist[0].ds_len = ccb->csio.dxfer_len;
360                 cam_sglist[0].ds_addr = (bus_addr_t)ccb->csio.data_ptr;
361                 cam_sg_count = 1;
362                 cam_sg_start = 0;
363                 cam_sg_offset = io->scsiio.kern_rel_offset;
364                 break;
365         default:
366                 panic("Invalid CAM flags %#x", ccb->ccb_h.flags);
367         }
368
369         if (io->scsiio.kern_sg_entries > 0) {
370                 ctl_sglist = (struct ctl_sg_entry *)io->scsiio.kern_data_ptr;
371                 ctl_sg_count = io->scsiio.kern_sg_entries;
372         } else {
373                 ctl_sglist = &ctl_sg_entry;
374                 ctl_sglist->addr = io->scsiio.kern_data_ptr;
375                 ctl_sglist->len = io->scsiio.kern_data_len;
376                 ctl_sg_count = 1;
377         }
378
379         ctl_watermark = 0;
380         cam_watermark = cam_sg_offset;
381         for (i = cam_sg_start, j = 0;
382              i < cam_sg_count && j < ctl_sg_count;) {
383                 uint8_t *cam_ptr, *ctl_ptr;
384
385                 len_to_copy = MIN(cam_sglist[i].ds_len - cam_watermark,
386                                   ctl_sglist[j].len - ctl_watermark);
387
388                 cam_ptr = (uint8_t *)cam_sglist[i].ds_addr;
389                 cam_ptr = cam_ptr + cam_watermark;
390                 if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR) {
391                         /*
392                          * XXX KDM fix this!
393                          */
394                         panic("need to implement bus address support");
395 #if 0
396                         kern_ptr = bus_to_virt(kern_sglist[j].addr);
397 #endif
398                 } else
399                         ctl_ptr = (uint8_t *)ctl_sglist[j].addr;
400                 ctl_ptr = ctl_ptr + ctl_watermark;
401
402                 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) ==
403                      CTL_FLAG_DATA_IN) {
404                         CTL_DEBUG_PRINT(("%s: copying %d bytes to CAM\n",
405                                          __func__, len_to_copy));
406                         CTL_DEBUG_PRINT(("%s: from %p to %p\n", ctl_ptr,
407                                          __func__, cam_ptr));
408                         bcopy(ctl_ptr, cam_ptr, len_to_copy);
409                 } else {
410                         CTL_DEBUG_PRINT(("%s: copying %d bytes from CAM\n",
411                                          __func__, len_to_copy));
412                         CTL_DEBUG_PRINT(("%s: from %p to %p\n", cam_ptr,
413                                          __func__, ctl_ptr));
414                         bcopy(cam_ptr, ctl_ptr, len_to_copy);
415                 }
416
417                 io->scsiio.ext_data_filled += len_to_copy;
418                 io->scsiio.kern_data_resid -= len_to_copy;
419
420                 cam_watermark += len_to_copy;
421                 if (cam_sglist[i].ds_len == cam_watermark) {
422                         i++;
423                         cam_watermark = 0;
424                 }
425
426                 ctl_watermark += len_to_copy;
427                 if (ctl_sglist[j].len == ctl_watermark) {
428                         j++;
429                         ctl_watermark = 0;
430                 }
431         }
432
433         if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) {
434                 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr = NULL;
435                 io->io_hdr.flags |= CTL_FLAG_STATUS_SENT;
436                 ccb->csio.resid = ccb->csio.dxfer_len -
437                     io->scsiio.ext_data_filled;
438                 ccb->ccb_h.status &= ~CAM_STATUS_MASK;
439                 ccb->ccb_h.status |= CAM_REQ_CMP;
440                 xpt_done(ccb);
441         }
442
443         io->scsiio.be_move_done(io);
444 }
445
446 static void
447 cfcs_done(union ctl_io *io)
448 {
449         union ccb *ccb;
450
451         ccb = io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr;
452         if (ccb == NULL) {
453                 ctl_free_io(io);
454                 return;
455         }
456
457         /*
458          * At this point we should have status.  If we don't, that's a bug.
459          */
460         KASSERT(((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE),
461                 ("invalid CTL status %#x", io->io_hdr.status));
462
463         /*
464          * Translate CTL status to CAM status.
465          */
466         if (ccb->ccb_h.func_code == XPT_SCSI_IO) {
467                 ccb->csio.resid = ccb->csio.dxfer_len -
468                     io->scsiio.ext_data_filled;
469         }
470         ccb->ccb_h.status &= ~CAM_STATUS_MASK;
471         switch (io->io_hdr.status & CTL_STATUS_MASK) {
472         case CTL_SUCCESS:
473                 ccb->ccb_h.status |= CAM_REQ_CMP;
474                 break;
475         case CTL_SCSI_ERROR:
476                 ccb->ccb_h.status |= CAM_SCSI_STATUS_ERROR | CAM_AUTOSNS_VALID;
477                 ccb->csio.scsi_status = io->scsiio.scsi_status;
478                 bcopy(&io->scsiio.sense_data, &ccb->csio.sense_data,
479                       min(io->scsiio.sense_len, ccb->csio.sense_len));
480                 if (ccb->csio.sense_len > io->scsiio.sense_len)
481                         ccb->csio.sense_resid = ccb->csio.sense_len -
482                                                 io->scsiio.sense_len;
483                 else
484                         ccb->csio.sense_resid = 0;
485                 if ((ccb->csio.sense_len - ccb->csio.sense_resid) >
486                      cfcs_max_sense) {
487                         ccb->csio.sense_resid = ccb->csio.sense_len -
488                                                 cfcs_max_sense;
489                 }
490                 break;
491         case CTL_CMD_ABORTED:
492                 ccb->ccb_h.status |= CAM_REQ_ABORTED;
493                 break;
494         case CTL_ERROR:
495         default:
496                 ccb->ccb_h.status |= CAM_REQ_CMP_ERR;
497                 break;
498         }
499         if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP &&
500             (ccb->ccb_h.status & CAM_DEV_QFRZN) == 0) {
501                 xpt_freeze_devq(ccb->ccb_h.path, 1);
502                 ccb->ccb_h.status |= CAM_DEV_QFRZN;
503         }
504         xpt_done(ccb);
505         ctl_free_io(io);
506 }
507
508 void
509 cfcs_action(struct cam_sim *sim, union ccb *ccb)
510 {
511         struct cfcs_softc *softc;
512         int err;
513
514         softc = (struct cfcs_softc *)cam_sim_softc(sim);
515         mtx_assert(&softc->lock, MA_OWNED);
516
517         switch (ccb->ccb_h.func_code) {
518         case XPT_SCSI_IO: {
519                 union ctl_io *io;
520                 struct ccb_scsiio *csio;
521
522                 csio = &ccb->csio;
523
524                 /*
525                  * Catch CCB flags, like physical address flags, that
526                  * indicate situations we currently can't handle.
527                  */
528                 if (ccb->ccb_h.flags & CFCS_BAD_CCB_FLAGS) {
529                         ccb->ccb_h.status = CAM_REQ_INVALID;
530                         printf("%s: bad CCB flags %#x (all flags %#x)\n",
531                                __func__, ccb->ccb_h.flags & CFCS_BAD_CCB_FLAGS,
532                                ccb->ccb_h.flags);
533                         xpt_done(ccb);
534                         return;
535                 }
536
537                 /*
538                  * If we aren't online, there are no devices to see.
539                  */
540                 if (softc->online == 0) {
541                         ccb->ccb_h.status = CAM_DEV_NOT_THERE;
542                         xpt_done(ccb);
543                         return;
544                 }
545
546                 io = ctl_alloc_io_nowait(softc->port.ctl_pool_ref);
547                 if (io == NULL) {
548                         printf("%s: can't allocate ctl_io\n", __func__);
549                         ccb->ccb_h.status = CAM_BUSY | CAM_DEV_QFRZN;
550                         xpt_freeze_devq(ccb->ccb_h.path, 1);
551                         xpt_done(ccb);
552                         return;
553                 }
554                 ctl_zero_io(io);
555                 /* Save pointers on both sides */
556                 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr = ccb;
557                 ccb->ccb_h.io_ptr = io;
558
559                 /*
560                  * Only SCSI I/O comes down this path, resets, etc. come
561                  * down via the XPT_RESET_BUS/LUN CCBs below.
562                  */
563                 io->io_hdr.io_type = CTL_IO_SCSI;
564                 io->io_hdr.nexus.initid = 1;
565                 io->io_hdr.nexus.targ_port = softc->port.targ_port;
566                 io->io_hdr.nexus.targ_lun = ctl_decode_lun(
567                     CAM_EXTLUN_BYTE_SWIZZLE(ccb->ccb_h.target_lun));
568                 /*
569                  * This tag scheme isn't the best, since we could in theory
570                  * have a very long-lived I/O and tag collision, especially
571                  * in a high I/O environment.  But it should work well
572                  * enough for now.  Since we're using unsigned ints,
573                  * they'll just wrap around.
574                  */
575                 io->scsiio.tag_num = softc->cur_tag_num++;
576                 csio->tag_id = io->scsiio.tag_num;
577                 switch (csio->tag_action) {
578                 case CAM_TAG_ACTION_NONE:
579                         io->scsiio.tag_type = CTL_TAG_UNTAGGED;
580                         break;
581                 case MSG_SIMPLE_TASK:
582                         io->scsiio.tag_type = CTL_TAG_SIMPLE;
583                         break;
584                 case MSG_HEAD_OF_QUEUE_TASK:
585                         io->scsiio.tag_type = CTL_TAG_HEAD_OF_QUEUE;
586                         break;
587                 case MSG_ORDERED_TASK:
588                         io->scsiio.tag_type = CTL_TAG_ORDERED;
589                         break;
590                 case MSG_ACA_TASK:
591                         io->scsiio.tag_type = CTL_TAG_ACA;
592                         break;
593                 default:
594                         io->scsiio.tag_type = CTL_TAG_UNTAGGED;
595                         printf("%s: unhandled tag type %#x!!\n", __func__,
596                                csio->tag_action);
597                         break;
598                 }
599                 if (csio->cdb_len > sizeof(io->scsiio.cdb)) {
600                         printf("%s: WARNING: CDB len %d > ctl_io space %zd\n",
601                                __func__, csio->cdb_len, sizeof(io->scsiio.cdb));
602                 }
603                 io->scsiio.cdb_len = min(csio->cdb_len, sizeof(io->scsiio.cdb));
604                 bcopy(scsiio_cdb_ptr(csio), io->scsiio.cdb, io->scsiio.cdb_len);
605
606                 ccb->ccb_h.status |= CAM_SIM_QUEUED;
607                 err = ctl_queue(io);
608                 if (err != CTL_RETVAL_COMPLETE) {
609                         printf("%s: func %d: error %d returned by "
610                                "ctl_queue()!\n", __func__,
611                                ccb->ccb_h.func_code, err);
612                         ctl_free_io(io);
613                         ccb->ccb_h.status = CAM_REQ_INVALID;
614                         xpt_done(ccb);
615                         return;
616                 }
617                 break;
618         }
619         case XPT_ABORT: {
620                 union ctl_io *io;
621                 union ccb *abort_ccb;
622
623                 abort_ccb = ccb->cab.abort_ccb;
624
625                 if (abort_ccb->ccb_h.func_code != XPT_SCSI_IO) {
626                         ccb->ccb_h.status = CAM_REQ_INVALID;
627                         xpt_done(ccb);
628                 }
629
630                 /*
631                  * If we aren't online, there are no devices to talk to.
632                  */
633                 if (softc->online == 0) {
634                         ccb->ccb_h.status = CAM_DEV_NOT_THERE;
635                         xpt_done(ccb);
636                         return;
637                 }
638
639                 io = ctl_alloc_io_nowait(softc->port.ctl_pool_ref);
640                 if (io == NULL) {
641                         ccb->ccb_h.status = CAM_BUSY | CAM_DEV_QFRZN;
642                         xpt_freeze_devq(ccb->ccb_h.path, 1);
643                         xpt_done(ccb);
644                         return;
645                 }
646
647                 ctl_zero_io(io);
648                 /* Save pointers on both sides */
649                 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr = ccb;
650                 ccb->ccb_h.io_ptr = io;
651
652                 io->io_hdr.io_type = CTL_IO_TASK;
653                 io->io_hdr.nexus.initid = 1;
654                 io->io_hdr.nexus.targ_port = softc->port.targ_port;
655                 io->io_hdr.nexus.targ_lun = ctl_decode_lun(
656                     CAM_EXTLUN_BYTE_SWIZZLE(ccb->ccb_h.target_lun));
657                 io->taskio.task_action = CTL_TASK_ABORT_TASK;
658                 io->taskio.tag_num = abort_ccb->csio.tag_id;
659                 switch (abort_ccb->csio.tag_action) {
660                 case CAM_TAG_ACTION_NONE:
661                         io->taskio.tag_type = CTL_TAG_UNTAGGED;
662                         break;
663                 case MSG_SIMPLE_TASK:
664                         io->taskio.tag_type = CTL_TAG_SIMPLE;
665                         break;
666                 case MSG_HEAD_OF_QUEUE_TASK:
667                         io->taskio.tag_type = CTL_TAG_HEAD_OF_QUEUE;
668                         break;
669                 case MSG_ORDERED_TASK:
670                         io->taskio.tag_type = CTL_TAG_ORDERED;
671                         break;
672                 case MSG_ACA_TASK:
673                         io->taskio.tag_type = CTL_TAG_ACA;
674                         break;
675                 default:
676                         io->taskio.tag_type = CTL_TAG_UNTAGGED;
677                         printf("%s: unhandled tag type %#x!!\n", __func__,
678                                abort_ccb->csio.tag_action);
679                         break;
680                 }
681                 err = ctl_queue(io);
682                 if (err != CTL_RETVAL_COMPLETE) {
683                         printf("%s func %d: error %d returned by "
684                                "ctl_queue()!\n", __func__,
685                                ccb->ccb_h.func_code, err);
686                         ctl_free_io(io);
687                 }
688                 break;
689         }
690         case XPT_GET_TRAN_SETTINGS: {
691                 struct ccb_trans_settings *cts;
692                 struct ccb_trans_settings_scsi *scsi;
693                 struct ccb_trans_settings_fc *fc;
694
695                 cts = &ccb->cts;
696                 scsi = &cts->proto_specific.scsi;
697                 fc = &cts->xport_specific.fc;
698
699                 
700                 cts->protocol = PROTO_SCSI;
701                 cts->protocol_version = SCSI_REV_SPC2;
702                 cts->transport = XPORT_FC;
703                 cts->transport_version = 0;
704
705                 scsi->valid = CTS_SCSI_VALID_TQ;
706                 scsi->flags = CTS_SCSI_FLAGS_TAG_ENB;
707                 fc->valid = CTS_FC_VALID_SPEED;
708                 fc->bitrate = 800000;
709                 fc->wwnn = softc->wwnn;
710                 fc->wwpn = softc->wwpn;
711                 fc->port = softc->port.targ_port;
712                 fc->valid |= CTS_FC_VALID_WWNN | CTS_FC_VALID_WWPN |
713                         CTS_FC_VALID_PORT; 
714                 ccb->ccb_h.status = CAM_REQ_CMP;
715                 break;
716         }
717         case XPT_SET_TRAN_SETTINGS:
718                 /* XXX KDM should we actually do something here? */
719                 ccb->ccb_h.status = CAM_REQ_CMP;
720                 break;
721         case XPT_RESET_BUS:
722         case XPT_RESET_DEV: {
723                 union ctl_io *io;
724
725                 /*
726                  * If we aren't online, there are no devices to talk to.
727                  */
728                 if (softc->online == 0) {
729                         ccb->ccb_h.status = CAM_DEV_NOT_THERE;
730                         xpt_done(ccb);
731                         return;
732                 }
733
734                 io = ctl_alloc_io_nowait(softc->port.ctl_pool_ref);
735                 if (io == NULL) {
736                         ccb->ccb_h.status = CAM_BUSY | CAM_DEV_QFRZN;
737                         xpt_freeze_devq(ccb->ccb_h.path, 1);
738                         xpt_done(ccb);
739                         return;
740                 }
741
742                 ctl_zero_io(io);
743                 /* Save pointers on both sides */
744                 if (ccb->ccb_h.func_code == XPT_RESET_DEV)
745                         io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr = ccb;
746                 ccb->ccb_h.io_ptr = io;
747
748                 io->io_hdr.io_type = CTL_IO_TASK;
749                 io->io_hdr.nexus.initid = 1;
750                 io->io_hdr.nexus.targ_port = softc->port.targ_port;
751                 io->io_hdr.nexus.targ_lun = ctl_decode_lun(
752                     CAM_EXTLUN_BYTE_SWIZZLE(ccb->ccb_h.target_lun));
753                 if (ccb->ccb_h.func_code == XPT_RESET_BUS)
754                         io->taskio.task_action = CTL_TASK_BUS_RESET;
755                 else
756                         io->taskio.task_action = CTL_TASK_LUN_RESET;
757
758                 err = ctl_queue(io);
759                 if (err != CTL_RETVAL_COMPLETE) {
760                         printf("%s func %d: error %d returned by "
761                               "ctl_queue()!\n", __func__,
762                               ccb->ccb_h.func_code, err);
763                         ctl_free_io(io);
764                 }
765                 break;
766         }
767         case XPT_CALC_GEOMETRY:
768                 cam_calc_geometry(&ccb->ccg, 1);
769                 xpt_done(ccb);
770                 break;
771         case XPT_PATH_INQ: {
772                 struct ccb_pathinq *cpi;
773
774                 cpi = &ccb->cpi;
775
776                 cpi->version_num = 0;
777                 cpi->hba_inquiry = PI_TAG_ABLE;
778                 cpi->target_sprt = 0;
779                 cpi->hba_misc = PIM_EXTLUNS;
780                 cpi->hba_eng_cnt = 0;
781                 cpi->max_target = 1;
782                 cpi->max_lun = 1024;
783                 /* Do we really have a limit? */
784                 cpi->maxio = 1024 * 1024;
785                 cpi->async_flags = 0;
786                 cpi->hpath_id = 0;
787                 cpi->initiator_id = 0;
788
789                 strlcpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
790                 strlcpy(cpi->hba_vid, "FreeBSD", HBA_IDLEN);
791                 strlcpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
792                 cpi->unit_number = 0;
793                 cpi->bus_id = 0;
794                 cpi->base_transfer_speed = 800000;
795                 cpi->protocol = PROTO_SCSI;
796                 cpi->protocol_version = SCSI_REV_SPC2;
797                 /*
798                  * Pretend to be Fibre Channel.
799                  */
800                 cpi->transport = XPORT_FC;
801                 cpi->transport_version = 0;
802                 cpi->xport_specific.fc.wwnn = softc->wwnn;
803                 cpi->xport_specific.fc.wwpn = softc->wwpn;
804                 cpi->xport_specific.fc.port = softc->port.targ_port;
805                 cpi->xport_specific.fc.bitrate = 8 * 1000 * 1000;
806                 cpi->ccb_h.status = CAM_REQ_CMP;
807                 break;
808         }
809         default:
810                 ccb->ccb_h.status = CAM_PROVIDE_FAIL;
811                 printf("%s: unsupported CCB type %#x\n", __func__,
812                        ccb->ccb_h.func_code);
813                 xpt_done(ccb);
814                 break;
815         }
816 }