]> CyberLeo.Net >> Repos - FreeBSD/stable/10.git/blob - sys/cam/ctl/scsi_ctl.c
MFC r275942: Reduce number of places where global control_softc is used.
[FreeBSD/stable/10.git] / sys / cam / ctl / scsi_ctl.c
1 /*-
2  * Copyright (c) 2008, 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/scsi_ctl.c#4 $
31  */
32 /*
33  * Peripheral driver interface between CAM and CTL (CAM Target Layer).
34  *
35  * Author: Ken Merry <ken@FreeBSD.org>
36  */
37
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD$");
40
41 #include <sys/param.h>
42 #include <sys/queue.h>
43 #include <sys/systm.h>
44 #include <sys/kernel.h>
45 #include <sys/lock.h>
46 #include <sys/mutex.h>
47 #include <sys/condvar.h>
48 #include <sys/malloc.h>
49 #include <sys/bus.h>
50 #include <sys/endian.h>
51 #include <sys/sbuf.h>
52 #include <sys/sysctl.h>
53 #include <sys/types.h>
54 #include <sys/systm.h>
55 #include <machine/bus.h>
56
57 #include <cam/cam.h>
58 #include <cam/cam_ccb.h>
59 #include <cam/cam_periph.h>
60 #include <cam/cam_queue.h>
61 #include <cam/cam_xpt_periph.h>
62 #include <cam/cam_debug.h>
63 #include <cam/cam_sim.h>
64 #include <cam/cam_xpt.h>
65
66 #include <cam/scsi/scsi_all.h>
67 #include <cam/scsi/scsi_message.h>
68
69 #include <cam/ctl/ctl_io.h>
70 #include <cam/ctl/ctl.h>
71 #include <cam/ctl/ctl_frontend.h>
72 #include <cam/ctl/ctl_util.h>
73 #include <cam/ctl/ctl_error.h>
74
75 struct ctlfe_softc {
76         struct ctl_port port;
77         path_id_t path_id;
78         u_int   maxio;
79         struct cam_sim *sim;
80         char port_name[DEV_IDLEN];
81         struct mtx lun_softc_mtx;
82         STAILQ_HEAD(, ctlfe_lun_softc) lun_softc_list;
83         STAILQ_ENTRY(ctlfe_softc) links;
84 };
85
86 STAILQ_HEAD(, ctlfe_softc) ctlfe_softc_list;
87 struct mtx ctlfe_list_mtx;
88 static char ctlfe_mtx_desc[] = "ctlfelist";
89 #ifdef CTLFE_INIT_ENABLE
90 static int ctlfe_max_targets = 1;
91 static int ctlfe_num_targets = 0;
92 #endif
93
94 typedef enum {
95         CTLFE_LUN_NONE          = 0x00,
96         CTLFE_LUN_WILDCARD      = 0x01
97 } ctlfe_lun_flags;
98
99 struct ctlfe_lun_softc {
100         struct ctlfe_softc *parent_softc;
101         struct cam_periph *periph;
102         ctlfe_lun_flags flags;
103         uint64_t ccbs_alloced;
104         uint64_t ccbs_freed;
105         uint64_t ctios_sent;
106         uint64_t ctios_returned;
107         uint64_t atios_sent;
108         uint64_t atios_returned;
109         uint64_t inots_sent;
110         uint64_t inots_returned;
111         /* bus_dma_tag_t dma_tag; */
112         TAILQ_HEAD(, ccb_hdr) work_queue;
113         STAILQ_ENTRY(ctlfe_lun_softc) links;
114 };
115
116 typedef enum {
117         CTLFE_CMD_NONE          = 0x00,
118         CTLFE_CMD_PIECEWISE     = 0x01
119 } ctlfe_cmd_flags;
120
121 /*
122  * The size limit of this structure is CTL_PORT_PRIV_SIZE, from ctl_io.h.
123  * Currently that is 600 bytes.
124  */
125 struct ctlfe_lun_cmd_info {
126         int cur_transfer_index;
127         size_t cur_transfer_off;
128         ctlfe_cmd_flags flags;
129         /*
130          * XXX KDM struct bus_dma_segment is 8 bytes on i386, and 16
131          * bytes on amd64.  So with 32 elements, this is 256 bytes on
132          * i386 and 512 bytes on amd64.
133          */
134 #define CTLFE_MAX_SEGS  32
135         bus_dma_segment_t cam_sglist[CTLFE_MAX_SEGS];
136 };
137 CTASSERT(sizeof(struct ctlfe_lun_cmd_info) <= CTL_PORT_PRIV_SIZE);
138
139 /*
140  * When we register the adapter/bus, request that this many ctl_ios be
141  * allocated.  This should be the maximum supported by the adapter, but we
142  * currently don't have a way to get that back from the path inquiry.
143  * XXX KDM add that to the path inquiry.
144  */
145 #define CTLFE_REQ_CTL_IO        4096
146 /*
147  * Number of Accept Target I/O CCBs to allocate and queue down to the
148  * adapter per LUN.
149  * XXX KDM should this be controlled by CTL?
150  */
151 #define CTLFE_ATIO_PER_LUN      1024
152 /*
153  * Number of Immediate Notify CCBs (used for aborts, resets, etc.) to
154  * allocate and queue down to the adapter per LUN.
155  * XXX KDM should this be controlled by CTL?
156  */
157 #define CTLFE_IN_PER_LUN        1024
158
159 /*
160  * Timeout (in seconds) on CTIO CCB allocation for doing a DMA or sending
161  * status to the initiator.  The SIM is expected to have its own timeouts,
162  * so we're not putting this timeout around the CCB execution time.  The
163  * SIM should timeout and let us know if it has an issue.
164  */
165 #define CTLFE_DMA_TIMEOUT       60
166
167 /*
168  * Turn this on to enable extra debugging prints.
169  */
170 #if 0
171 #define CTLFE_DEBUG
172 #endif
173
174 /*
175  * Use randomly assigned WWNN/WWPN values.  This is to work around an issue
176  * in the FreeBSD initiator that makes it unable to rescan the target if
177  * the target gets rebooted and the WWNN/WWPN stay the same.
178  */
179 #if 0
180 #define RANDOM_WWNN
181 #endif
182
183 MALLOC_DEFINE(M_CTLFE, "CAM CTL FE", "CAM CTL FE interface");
184
185 #define io_ptr          ppriv_ptr0
186
187 /* This is only used in the CTIO */
188 #define ccb_atio        ppriv_ptr1
189
190 int                     ctlfeinitialize(void);
191 void                    ctlfeshutdown(void);
192 static periph_init_t    ctlfeperiphinit;
193 static void             ctlfeasync(void *callback_arg, uint32_t code,
194                                    struct cam_path *path, void *arg);
195 static periph_ctor_t    ctlferegister;
196 static periph_oninv_t   ctlfeoninvalidate;
197 static periph_dtor_t    ctlfecleanup;
198 static periph_start_t   ctlfestart;
199 static void             ctlfedone(struct cam_periph *periph,
200                                   union ccb *done_ccb);
201
202 static void             ctlfe_onoffline(void *arg, int online);
203 static void             ctlfe_online(void *arg);
204 static void             ctlfe_offline(void *arg);
205 static int              ctlfe_lun_enable(void *arg, struct ctl_id targ_id,
206                                          int lun_id);
207 static int              ctlfe_lun_disable(void *arg, struct ctl_id targ_id,
208                                           int lun_id);
209 static void             ctlfe_dump_sim(struct cam_sim *sim);
210 static void             ctlfe_dump_queue(struct ctlfe_lun_softc *softc);
211 static void             ctlfe_datamove(union ctl_io *io);
212 static void             ctlfe_done(union ctl_io *io);
213 static void             ctlfe_dump(void);
214
215 static struct periph_driver ctlfe_driver =
216 {
217         ctlfeperiphinit, "ctl",
218         TAILQ_HEAD_INITIALIZER(ctlfe_driver.units), /*generation*/ 0,
219         CAM_PERIPH_DRV_EARLY
220 };
221
222 static struct ctl_frontend ctlfe_frontend =
223 {
224         .name = "camtgt",
225         .init = ctlfeinitialize,
226         .fe_dump = ctlfe_dump,
227         .shutdown = ctlfeshutdown,
228 };
229 CTL_FRONTEND_DECLARE(ctlfe, ctlfe_frontend);
230
231 void
232 ctlfeshutdown(void)
233 {
234         return;
235 }
236
237 int
238 ctlfeinitialize(void)
239 {
240
241         STAILQ_INIT(&ctlfe_softc_list);
242         mtx_init(&ctlfe_list_mtx, ctlfe_mtx_desc, NULL, MTX_DEF);
243         periphdriver_register(&ctlfe_driver);
244         return (0);
245 }
246
247 void
248 ctlfeperiphinit(void)
249 {
250         cam_status status;
251
252         status = xpt_register_async(AC_PATH_REGISTERED | AC_PATH_DEREGISTERED |
253                                     AC_CONTRACT, ctlfeasync, NULL, NULL);
254         if (status != CAM_REQ_CMP) {
255                 printf("ctl: Failed to attach async callback due to CAM "
256                        "status 0x%x!\n", status);
257         }
258 }
259
260 static void
261 ctlfeasync(void *callback_arg, uint32_t code, struct cam_path *path, void *arg)
262 {
263         struct ctlfe_softc *softc;
264
265 #ifdef CTLFEDEBUG
266         printf("%s: entered\n", __func__);
267 #endif
268
269         mtx_lock(&ctlfe_list_mtx);
270         STAILQ_FOREACH(softc, &ctlfe_softc_list, links) {
271                 if (softc->path_id == xpt_path_path_id(path))
272                         break;
273         }
274         mtx_unlock(&ctlfe_list_mtx);
275
276         /*
277          * When a new path gets registered, and it is capable of target
278          * mode, go ahead and attach.  Later on, we may need to be more
279          * selective, but for now this will be sufficient.
280          */
281         switch (code) {
282         case AC_PATH_REGISTERED: {
283                 struct ctl_port *port;
284                 struct ccb_pathinq *cpi;
285                 int retval;
286
287                 cpi = (struct ccb_pathinq *)arg;
288
289                 /* Don't attach if it doesn't support target mode */
290                 if ((cpi->target_sprt & PIT_PROCESSOR) == 0) {
291 #ifdef CTLFEDEBUG
292                         printf("%s: SIM %s%d doesn't support target mode\n",
293                                __func__, cpi->dev_name, cpi->unit_number);
294 #endif
295                         break;
296                 }
297
298                 if (softc != NULL) {
299 #ifdef CTLFEDEBUG
300                         printf("%s: CTL port for CAM path %u already exists\n",
301                                __func__, xpt_path_path_id(path));
302 #endif
303                         break;
304                 }
305
306 #ifdef CTLFE_INIT_ENABLE
307                 if (ctlfe_num_targets >= ctlfe_max_targets) {
308                         union ccb *ccb;
309
310                         ccb = (union ccb *)malloc(sizeof(*ccb), M_TEMP,
311                                                   M_NOWAIT | M_ZERO);
312                         if (ccb == NULL) {
313                                 printf("%s: unable to malloc CCB!\n", __func__);
314                                 return;
315                         }
316                         xpt_setup_ccb(&ccb->ccb_h, path, CAM_PRIORITY_NONE);
317
318                         ccb->ccb_h.func_code = XPT_SET_SIM_KNOB;
319                         ccb->knob.xport_specific.valid = KNOB_VALID_ROLE;
320                         ccb->knob.xport_specific.fc.role = KNOB_ROLE_INITIATOR;
321
322                         xpt_action(ccb);
323
324                         if ((ccb->ccb_h.status & CAM_STATUS_MASK) !=
325                              CAM_REQ_CMP) {
326                                 printf("%s: SIM %s%d (path id %d) initiator "
327                                        "enable failed with status %#x\n",
328                                        __func__, cpi->dev_name,
329                                        cpi->unit_number, cpi->ccb_h.path_id,
330                                        ccb->ccb_h.status);
331                         } else {
332                                 printf("%s: SIM %s%d (path id %d) initiator "
333                                        "enable succeeded\n",
334                                        __func__, cpi->dev_name,
335                                        cpi->unit_number, cpi->ccb_h.path_id);
336                         }
337
338                         free(ccb, M_TEMP);
339
340                         break;
341                 } else {
342                         ctlfe_num_targets++;
343                 }
344
345                 printf("%s: ctlfe_num_targets = %d\n", __func__,
346                        ctlfe_num_targets);
347 #endif /* CTLFE_INIT_ENABLE */
348
349                 /*
350                  * We're in an interrupt context here, so we have to
351                  * use M_NOWAIT.  Of course this means trouble if we
352                  * can't allocate memory.
353                  */
354                 softc = malloc(sizeof(*softc), M_CTLFE, M_NOWAIT | M_ZERO);
355                 if (softc == NULL) {
356                         printf("%s: unable to malloc %zd bytes for softc\n",
357                                __func__, sizeof(*softc));
358                         return;
359                 }
360
361                 softc->path_id = cpi->ccb_h.path_id;
362                 softc->sim = xpt_path_sim(path);
363                 if (cpi->maxio != 0)
364                         softc->maxio = cpi->maxio;
365                 else
366                         softc->maxio = DFLTPHYS;
367                 mtx_init(&softc->lun_softc_mtx, "LUN softc mtx", NULL, MTX_DEF);
368                 STAILQ_INIT(&softc->lun_softc_list);
369
370                 port = &softc->port;
371                 port->frontend = &ctlfe_frontend;
372
373                 /*
374                  * XXX KDM should we be more accurate here ?
375                  */
376                 if (cpi->transport == XPORT_FC)
377                         port->port_type = CTL_PORT_FC;
378                 else if (cpi->transport == XPORT_SAS)
379                         port->port_type = CTL_PORT_SAS;
380                 else
381                         port->port_type = CTL_PORT_SCSI;
382
383                 /* XXX KDM what should the real number be here? */
384                 port->num_requested_ctl_io = 4096;
385                 snprintf(softc->port_name, sizeof(softc->port_name),
386                          "%s%d", cpi->dev_name, cpi->unit_number);
387                 /*
388                  * XXX KDM it would be nice to allocate storage in the
389                  * frontend structure itself.
390                  */
391                 port->port_name = softc->port_name;
392                 port->physical_port = cpi->bus_id;
393                 port->virtual_port = 0;
394                 port->port_online = ctlfe_online;
395                 port->port_offline = ctlfe_offline;
396                 port->onoff_arg = softc;
397                 port->lun_enable = ctlfe_lun_enable;
398                 port->lun_disable = ctlfe_lun_disable;
399                 port->targ_lun_arg = softc;
400                 port->fe_datamove = ctlfe_datamove;
401                 port->fe_done = ctlfe_done;
402                 /*
403                  * XXX KDM the path inquiry doesn't give us the maximum
404                  * number of targets supported.
405                  */
406                 port->max_targets = cpi->max_target;
407                 port->max_target_id = cpi->max_target;
408                 
409                 /*
410                  * XXX KDM need to figure out whether we're the master or
411                  * slave.
412                  */
413 #ifdef CTLFEDEBUG
414                 printf("%s: calling ctl_port_register() for %s%d\n",
415                        __func__, cpi->dev_name, cpi->unit_number);
416 #endif
417                 retval = ctl_port_register(port);
418                 if (retval != 0) {
419                         printf("%s: ctl_port_register() failed with "
420                                "error %d!\n", __func__, retval);
421                         mtx_destroy(&softc->lun_softc_mtx);
422                         free(softc, M_CTLFE);
423                         break;
424                 } else {
425                         mtx_lock(&ctlfe_list_mtx);
426                         STAILQ_INSERT_TAIL(&ctlfe_softc_list, softc, links);
427                         mtx_unlock(&ctlfe_list_mtx);
428                 }
429
430                 break;
431         }
432         case AC_PATH_DEREGISTERED: {
433
434                 if (softc != NULL) {
435                         /*
436                          * XXX KDM are we certain at this point that there
437                          * are no outstanding commands for this frontend?
438                          */
439                         mtx_lock(&ctlfe_list_mtx);
440                         STAILQ_REMOVE(&ctlfe_softc_list, softc, ctlfe_softc,
441                             links);
442                         mtx_unlock(&ctlfe_list_mtx);
443                         ctl_port_deregister(&softc->port);
444                         mtx_destroy(&softc->lun_softc_mtx);
445                         free(softc, M_CTLFE);
446                 }
447                 break;
448         }
449         case AC_CONTRACT: {
450                 struct ac_contract *ac;
451
452                 ac = (struct ac_contract *)arg;
453
454                 switch (ac->contract_number) {
455                 case AC_CONTRACT_DEV_CHG: {
456                         struct ac_device_changed *dev_chg;
457                         int retval;
458
459                         dev_chg = (struct ac_device_changed *)ac->contract_data;
460
461                         printf("%s: WWPN %#jx port 0x%06x path %u target %u %s\n",
462                                __func__, dev_chg->wwpn, dev_chg->port,
463                                xpt_path_path_id(path), dev_chg->target,
464                                (dev_chg->arrived == 0) ?  "left" : "arrived");
465
466                         if (softc == NULL) {
467                                 printf("%s: CTL port for CAM path %u not "
468                                        "found!\n", __func__,
469                                        xpt_path_path_id(path));
470                                 break;
471                         }
472                         if (dev_chg->arrived != 0) {
473                                 retval = ctl_add_initiator(&softc->port,
474                                     dev_chg->target, dev_chg->wwpn, NULL);
475                         } else {
476                                 retval = ctl_remove_initiator(&softc->port,
477                                     dev_chg->target);
478                         }
479
480                         if (retval < 0) {
481                                 printf("%s: could not %s port %d iid %u "
482                                        "WWPN %#jx!\n", __func__,
483                                        (dev_chg->arrived != 0) ? "add" :
484                                        "remove", softc->port.targ_port,
485                                        dev_chg->target,
486                                        (uintmax_t)dev_chg->wwpn);
487                         }
488                         break;
489                 }
490                 default:
491                         printf("%s: unsupported contract number %ju\n",
492                                __func__, (uintmax_t)ac->contract_number);
493                         break;
494                 }
495                 break;
496         }
497         default:
498                 break;
499         }
500 }
501
502 static cam_status
503 ctlferegister(struct cam_periph *periph, void *arg)
504 {
505         struct ctlfe_softc *bus_softc;
506         struct ctlfe_lun_softc *softc;
507         union ccb en_lun_ccb;
508         cam_status status;
509         int i;
510
511         softc = (struct ctlfe_lun_softc *)arg;
512         bus_softc = softc->parent_softc;
513         
514         TAILQ_INIT(&softc->work_queue);
515         softc->periph = periph;
516         periph->softc = softc;
517
518         xpt_setup_ccb(&en_lun_ccb.ccb_h, periph->path, CAM_PRIORITY_NONE);
519         en_lun_ccb.ccb_h.func_code = XPT_EN_LUN;
520         en_lun_ccb.cel.grp6_len = 0;
521         en_lun_ccb.cel.grp7_len = 0;
522         en_lun_ccb.cel.enable = 1;
523         xpt_action(&en_lun_ccb);
524         status = (en_lun_ccb.ccb_h.status & CAM_STATUS_MASK);
525         if (status != CAM_REQ_CMP) {
526                 xpt_print(periph->path, "%s: Enable LUN failed, status 0x%x\n", 
527                           __func__, en_lun_ccb.ccb_h.status);
528                 return (status);
529         }
530
531         status = CAM_REQ_CMP;
532
533         for (i = 0; i < CTLFE_ATIO_PER_LUN; i++) {
534                 union ccb *new_ccb;
535                 union ctl_io *new_io;
536
537                 new_ccb = (union ccb *)malloc(sizeof(*new_ccb), M_CTLFE,
538                                               M_ZERO|M_NOWAIT);
539                 if (new_ccb == NULL) {
540                         status = CAM_RESRC_UNAVAIL;
541                         break;
542                 }
543                 new_io = ctl_alloc_io_nowait(bus_softc->port.ctl_pool_ref);
544                 if (new_io == NULL) {
545                         free(new_ccb, M_CTLFE);
546                         status = CAM_RESRC_UNAVAIL;
547                         break;
548                 }
549                 new_ccb->ccb_h.io_ptr = new_io;
550
551                 xpt_setup_ccb(&new_ccb->ccb_h, periph->path, /*priority*/ 1);
552                 new_ccb->ccb_h.func_code = XPT_ACCEPT_TARGET_IO;
553                 new_ccb->ccb_h.cbfcnp = ctlfedone;
554                 new_ccb->ccb_h.flags |= CAM_UNLOCKED;
555                 xpt_action(new_ccb);
556                 softc->atios_sent++;
557                 status = new_ccb->ccb_h.status;
558                 if ((status & CAM_STATUS_MASK) != CAM_REQ_INPROG) {
559                         ctl_free_io(new_io);
560                         free(new_ccb, M_CTLFE);
561                         break;
562                 }
563         }
564
565         status = cam_periph_acquire(periph);
566         if ((status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
567                 xpt_print(periph->path, "%s: could not acquire reference "
568                           "count, status = %#x\n", __func__, status);
569                 return (status);
570         }
571
572         if (i == 0) {
573                 xpt_print(periph->path, "%s: could not allocate ATIO CCBs, "
574                           "status 0x%x\n", __func__, status);
575                 return (CAM_REQ_CMP_ERR);
576         }
577
578         for (i = 0; i < CTLFE_IN_PER_LUN; i++) {
579                 union ccb *new_ccb;
580                 union ctl_io *new_io;
581
582                 new_ccb = (union ccb *)malloc(sizeof(*new_ccb), M_CTLFE,
583                                               M_ZERO|M_NOWAIT);
584                 if (new_ccb == NULL) {
585                         status = CAM_RESRC_UNAVAIL;
586                         break;
587                 }
588                 new_io = ctl_alloc_io_nowait(bus_softc->port.ctl_pool_ref);
589                 if (new_io == NULL) {
590                         free(new_ccb, M_CTLFE);
591                         status = CAM_RESRC_UNAVAIL;
592                         break;
593                 }
594                 new_ccb->ccb_h.io_ptr = new_io;
595
596                 xpt_setup_ccb(&new_ccb->ccb_h, periph->path, /*priority*/ 1);
597                 new_ccb->ccb_h.func_code = XPT_IMMEDIATE_NOTIFY;
598                 new_ccb->ccb_h.cbfcnp = ctlfedone;
599                 new_ccb->ccb_h.flags |= CAM_UNLOCKED;
600                 xpt_action(new_ccb);
601                 softc->inots_sent++;
602                 status = new_ccb->ccb_h.status;
603                 if ((status & CAM_STATUS_MASK) != CAM_REQ_INPROG) {
604                         /*
605                          * Note that we don't free the CCB here.  If the
606                          * status is not CAM_REQ_INPROG, then we're
607                          * probably talking to a SIM that says it is
608                          * target-capable but doesn't support the 
609                          * XPT_IMMEDIATE_NOTIFY CCB.  i.e. it supports the
610                          * older API.  In that case, it'll call xpt_done()
611                          * on the CCB, and we need to free it in our done
612                          * routine as a result.
613                          */
614                         break;
615                 }
616         }
617         if ((i == 0)
618          || (status != CAM_REQ_INPROG)) {
619                 xpt_print(periph->path, "%s: could not allocate immediate "
620                           "notify CCBs, status 0x%x\n", __func__, status);
621                 return (CAM_REQ_CMP_ERR);
622         }
623         mtx_lock(&bus_softc->lun_softc_mtx);
624         STAILQ_INSERT_TAIL(&bus_softc->lun_softc_list, softc, links);
625         mtx_unlock(&bus_softc->lun_softc_mtx);
626         return (CAM_REQ_CMP);
627 }
628
629 static void
630 ctlfeoninvalidate(struct cam_periph *periph)
631 {
632         union ccb en_lun_ccb;
633         cam_status status;
634         struct ctlfe_softc *bus_softc;
635         struct ctlfe_lun_softc *softc;
636
637         softc = (struct ctlfe_lun_softc *)periph->softc;
638
639         xpt_setup_ccb(&en_lun_ccb.ccb_h, periph->path, CAM_PRIORITY_NONE);
640         en_lun_ccb.ccb_h.func_code = XPT_EN_LUN;
641         en_lun_ccb.cel.grp6_len = 0;
642         en_lun_ccb.cel.grp7_len = 0;
643         en_lun_ccb.cel.enable = 0;
644         xpt_action(&en_lun_ccb);
645         status = (en_lun_ccb.ccb_h.status & CAM_STATUS_MASK);
646         if (status != CAM_REQ_CMP) {
647                 xpt_print(periph->path, "%s: Disable LUN failed, status 0x%x\n",
648                           __func__, en_lun_ccb.ccb_h.status);
649                 /*
650                  * XXX KDM what do we do now?
651                  */
652         }
653         xpt_print(periph->path, "LUN removed, %ju ATIOs outstanding, %ju "
654                   "INOTs outstanding, %d refs\n", softc->atios_sent -
655                   softc->atios_returned, softc->inots_sent -
656                   softc->inots_returned, periph->refcount);
657
658         bus_softc = softc->parent_softc;
659         mtx_lock(&bus_softc->lun_softc_mtx);
660         STAILQ_REMOVE(&bus_softc->lun_softc_list, softc, ctlfe_lun_softc, links);
661         mtx_unlock(&bus_softc->lun_softc_mtx);
662 }
663
664 static void
665 ctlfecleanup(struct cam_periph *periph)
666 {
667         struct ctlfe_lun_softc *softc;
668
669         xpt_print(periph->path, "%s: Called\n", __func__);
670
671         softc = (struct ctlfe_lun_softc *)periph->softc;
672
673         /*
674          * XXX KDM is there anything else that needs to be done here?
675          */
676
677         free(softc, M_CTLFE);
678 }
679
680 static void
681 ctlfedata(struct ctlfe_lun_softc *softc, union ctl_io *io,
682     ccb_flags *flags, uint8_t **data_ptr, uint32_t *dxfer_len,
683     u_int16_t *sglist_cnt)
684 {
685         struct ctlfe_softc *bus_softc;
686         struct ctlfe_lun_cmd_info *cmd_info;
687         struct ctl_sg_entry *ctl_sglist;
688         bus_dma_segment_t *cam_sglist;
689         size_t off;
690         int i, idx;
691
692         cmd_info = (struct ctlfe_lun_cmd_info *)io->io_hdr.port_priv;
693         bus_softc = softc->parent_softc;
694
695         /*
696          * Set the direction, relative to the initiator.
697          */
698         *flags &= ~CAM_DIR_MASK;
699         if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN)
700                 *flags |= CAM_DIR_IN;
701         else
702                 *flags |= CAM_DIR_OUT;
703
704         *flags &= ~CAM_DATA_MASK;
705         idx = cmd_info->cur_transfer_index;
706         off = cmd_info->cur_transfer_off;
707         cmd_info->flags &= ~CTLFE_CMD_PIECEWISE;
708         if (io->scsiio.kern_sg_entries == 0) {
709                 /* No S/G list. */
710                 *data_ptr = io->scsiio.kern_data_ptr + off;
711                 if (io->scsiio.kern_data_len - off <= bus_softc->maxio) {
712                         *dxfer_len = io->scsiio.kern_data_len - off;
713                 } else {
714                         *dxfer_len = bus_softc->maxio;
715                         cmd_info->cur_transfer_index = -1;
716                         cmd_info->cur_transfer_off = bus_softc->maxio;
717                         cmd_info->flags |= CTLFE_CMD_PIECEWISE;
718                 }
719                 *sglist_cnt = 0;
720
721                 if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR)
722                         *flags |= CAM_DATA_PADDR;
723                 else
724                         *flags |= CAM_DATA_VADDR;
725         } else {
726                 /* S/G list with physical or virtual pointers. */
727                 ctl_sglist = (struct ctl_sg_entry *)io->scsiio.kern_data_ptr;
728                 cam_sglist = cmd_info->cam_sglist;
729                 *dxfer_len = 0;
730                 for (i = 0; i < io->scsiio.kern_sg_entries - idx; i++) {
731                         cam_sglist[i].ds_addr = (bus_addr_t)ctl_sglist[i + idx].addr + off;
732                         if (ctl_sglist[i + idx].len - off <= bus_softc->maxio - *dxfer_len) {
733                                 cam_sglist[i].ds_len = ctl_sglist[idx + i].len - off;
734                                 *dxfer_len += cam_sglist[i].ds_len;
735                         } else {
736                                 cam_sglist[i].ds_len = bus_softc->maxio - *dxfer_len;
737                                 cmd_info->cur_transfer_index = idx + i;
738                                 cmd_info->cur_transfer_off = cam_sglist[i].ds_len + off;
739                                 cmd_info->flags |= CTLFE_CMD_PIECEWISE;
740                                 *dxfer_len += cam_sglist[i].ds_len;
741                                 if (ctl_sglist[i].len != 0)
742                                         i++;
743                                 break;
744                         }
745                         if (i == (CTLFE_MAX_SEGS - 1) &&
746                             idx + i < (io->scsiio.kern_sg_entries - 1)) {
747                                 cmd_info->cur_transfer_index = idx + i + 1;
748                                 cmd_info->cur_transfer_off = 0;
749                                 cmd_info->flags |= CTLFE_CMD_PIECEWISE;
750                                 i++;
751                                 break;
752                         }
753                         off = 0;
754                 }
755                 *sglist_cnt = i;
756                 if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR)
757                         *flags |= CAM_DATA_SG_PADDR;
758                 else
759                         *flags |= CAM_DATA_SG;
760                 *data_ptr = (uint8_t *)cam_sglist;
761         }
762 }
763
764 static void
765 ctlfestart(struct cam_periph *periph, union ccb *start_ccb)
766 {
767         struct ctlfe_lun_softc *softc;
768         struct ctlfe_lun_cmd_info *cmd_info;
769         struct ccb_hdr *ccb_h;
770         struct ccb_accept_tio *atio;
771         struct ccb_scsiio *csio;
772         uint8_t *data_ptr;
773         uint32_t dxfer_len;
774         ccb_flags flags;
775         union ctl_io *io;
776         uint8_t scsi_status;
777
778         softc = (struct ctlfe_lun_softc *)periph->softc;
779         softc->ccbs_alloced++;
780
781         ccb_h = TAILQ_FIRST(&softc->work_queue);
782         if (ccb_h == NULL) {
783                 softc->ccbs_freed++;
784                 xpt_release_ccb(start_ccb);
785                 return;
786         }
787
788         /* Take the ATIO off the work queue */
789         TAILQ_REMOVE(&softc->work_queue, ccb_h, periph_links.tqe);
790         atio = (struct ccb_accept_tio *)ccb_h;
791         io = (union ctl_io *)ccb_h->io_ptr;
792         csio = &start_ccb->csio;
793
794         flags = atio->ccb_h.flags &
795                 (CAM_DIS_DISCONNECT|CAM_TAG_ACTION_VALID|CAM_DIR_MASK);
796         cmd_info = (struct ctlfe_lun_cmd_info *)io->io_hdr.port_priv;
797         cmd_info->cur_transfer_index = 0;
798         cmd_info->cur_transfer_off = 0;
799         cmd_info->flags = 0;
800
801         if (io->io_hdr.flags & CTL_FLAG_DMA_QUEUED) {
802                 /*
803                  * Datamove call, we need to setup the S/G list.
804                  */
805                 scsi_status = 0;
806                 csio->cdb_len = atio->cdb_len;
807                 ctlfedata(softc, io, &flags, &data_ptr, &dxfer_len,
808                     &csio->sglist_cnt);
809                 io->scsiio.ext_data_filled += dxfer_len;
810                 if (io->scsiio.ext_data_filled > io->scsiio.kern_total_len) {
811                         xpt_print(periph->path, "%s: tag 0x%04x "
812                                   "fill len %u > total %u\n",
813                                   __func__, io->scsiio.tag_num,
814                                   io->scsiio.ext_data_filled,
815                                   io->scsiio.kern_total_len);
816                 }
817         } else {
818                 /*
819                  * We're done, send status back.
820                  */
821                 if ((io->io_hdr.flags & CTL_FLAG_ABORT) &&
822                     (io->io_hdr.flags & CTL_FLAG_ABORT_STATUS) == 0) {
823                         io->io_hdr.flags &= ~CTL_FLAG_STATUS_QUEUED;
824
825                         /*
826                          * If this command was aborted, we don't
827                          * need to send status back to the SIM.
828                          * Just free the CTIO and ctl_io, and
829                          * recycle the ATIO back to the SIM.
830                          */
831                         xpt_print(periph->path, "%s: aborted "
832                                   "command 0x%04x discarded\n",
833                                   __func__, io->scsiio.tag_num);
834                         /*
835                          * For a wildcard attachment, commands can
836                          * come in with a specific target/lun.  Reset
837                          * the target and LUN fields back to the
838                          * wildcard values before we send them back
839                          * down to the SIM.  The SIM has a wildcard
840                          * LUN enabled, not whatever target/lun
841                          * these happened to be.
842                          */
843                         if (softc->flags & CTLFE_LUN_WILDCARD) {
844                                 atio->ccb_h.target_id = CAM_TARGET_WILDCARD;
845                                 atio->ccb_h.target_lun = CAM_LUN_WILDCARD;
846                         }
847
848                         if ((atio->ccb_h.status & CAM_DEV_QFRZN) != 0) {
849                                 cam_release_devq(periph->path,
850                                                  /*relsim_flags*/0,
851                                                  /*reduction*/0,
852                                                  /*timeout*/0,
853                                                  /*getcount_only*/0);
854                                 atio->ccb_h.status &= ~CAM_DEV_QFRZN;
855                         }
856
857                         if (atio->ccb_h.func_code != XPT_ACCEPT_TARGET_IO) {
858                                 xpt_print(periph->path, "%s: func_code "
859                                           "is %#x\n", __func__,
860                                           atio->ccb_h.func_code);
861                         }
862                         start_ccb->ccb_h.func_code = XPT_ABORT;
863                         start_ccb->cab.abort_ccb = (union ccb *)atio;
864
865                         /* Tell the SIM that we've aborted this ATIO */
866                         xpt_action(start_ccb);
867                         softc->ccbs_freed++;
868                         xpt_release_ccb(start_ccb);
869
870                         /*
871                          * Send the ATIO back down to the SIM.
872                          */
873                         xpt_action((union ccb *)atio);
874                         softc->atios_sent++;
875
876                         /*
877                          * If we still have work to do, ask for
878                          * another CCB.  Otherwise, deactivate our
879                          * callout.
880                          */
881                         if (!TAILQ_EMPTY(&softc->work_queue))
882                                 xpt_schedule(periph, /*priority*/ 1);
883                         return;
884                 }
885                 data_ptr = NULL;
886                 dxfer_len = 0;
887                 csio->sglist_cnt = 0;
888                 scsi_status = 0;
889         }
890         if ((io->io_hdr.flags & CTL_FLAG_STATUS_QUEUED) &&
891             (cmd_info->flags & CTLFE_CMD_PIECEWISE) == 0 &&
892             ((io->io_hdr.flags & CTL_FLAG_DMA_QUEUED) == 0 ||
893              io->io_hdr.status == CTL_SUCCESS)) {
894                 io->io_hdr.flags |= CTL_FLAG_STATUS_SENT;
895                 flags |= CAM_SEND_STATUS;
896                 scsi_status = io->scsiio.scsi_status;
897                 csio->sense_len = io->scsiio.sense_len;
898 #ifdef CTLFEDEBUG
899                 printf("%s: tag %04x status %x\n", __func__,
900                        atio->tag_id, io->io_hdr.status);
901 #endif
902                 if (csio->sense_len != 0) {
903                         csio->sense_data = io->scsiio.sense_data;
904                         flags |= CAM_SEND_SENSE;
905                 } else if (scsi_status == SCSI_STATUS_CHECK_COND) {
906                         xpt_print(periph->path, "%s: check condition "
907                                   "with no sense\n", __func__);
908                 }
909         }
910
911 #ifdef CTLFEDEBUG
912         printf("%s: %s: tag %04x flags %x ptr %p len %u\n", __func__,
913                (flags & CAM_SEND_STATUS) ? "done" : "datamove",
914                atio->tag_id, flags, data_ptr, dxfer_len);
915 #endif
916
917         /*
918          * Valid combinations:
919          *  - CAM_SEND_STATUS, CAM_DATA_SG = 0, dxfer_len = 0,
920          *    sglist_cnt = 0
921          *  - CAM_SEND_STATUS = 0, CAM_DATA_SG = 0, dxfer_len != 0,
922          *    sglist_cnt = 0
923          *  - CAM_SEND_STATUS = 0, CAM_DATA_SG, dxfer_len != 0,
924          *    sglist_cnt != 0
925          */
926 #ifdef CTLFEDEBUG
927         if (((flags & CAM_SEND_STATUS)
928           && (((flags & CAM_DATA_SG) != 0)
929            || (dxfer_len != 0)
930            || (csio->sglist_cnt != 0)))
931          || (((flags & CAM_SEND_STATUS) == 0)
932           && (dxfer_len == 0))
933          || ((flags & CAM_DATA_SG)
934           && (csio->sglist_cnt == 0))
935          || (((flags & CAM_DATA_SG) == 0)
936           && (csio->sglist_cnt != 0))) {
937                 printf("%s: tag %04x cdb %02x flags %#x dxfer_len "
938                        "%d sg %u\n", __func__, atio->tag_id,
939                        atio->cdb_io.cdb_bytes[0], flags, dxfer_len,
940                        csio->sglist_cnt);
941                 printf("%s: tag %04x io status %#x\n", __func__,
942                        atio->tag_id, io->io_hdr.status);
943         }
944 #endif
945         cam_fill_ctio(csio,
946                       /*retries*/ 2,
947                       ctlfedone,
948                       flags,
949                       (flags & CAM_TAG_ACTION_VALID) ? MSG_SIMPLE_Q_TAG : 0,
950                       atio->tag_id,
951                       atio->init_id,
952                       scsi_status,
953                       /*data_ptr*/ data_ptr,
954                       /*dxfer_len*/ dxfer_len,
955                       /*timeout*/ 5 * 1000);
956         start_ccb->ccb_h.flags |= CAM_UNLOCKED;
957         start_ccb->ccb_h.ccb_atio = atio;
958         if (io->io_hdr.flags & CTL_FLAG_DMA_QUEUED)
959                 io->io_hdr.flags |= CTL_FLAG_DMA_INPROG;
960         io->io_hdr.flags &= ~(CTL_FLAG_DMA_QUEUED | CTL_FLAG_STATUS_QUEUED);
961
962         softc->ctios_sent++;
963
964         cam_periph_unlock(periph);
965         xpt_action(start_ccb);
966         cam_periph_lock(periph);
967
968         if ((atio->ccb_h.status & CAM_DEV_QFRZN) != 0) {
969                 cam_release_devq(periph->path,
970                                  /*relsim_flags*/0,
971                                  /*reduction*/0,
972                                  /*timeout*/0,
973                                  /*getcount_only*/0);
974                 atio->ccb_h.status &= ~CAM_DEV_QFRZN;
975         }
976
977         /*
978          * If we still have work to do, ask for another CCB.
979          */
980         if (!TAILQ_EMPTY(&softc->work_queue))
981                 xpt_schedule(periph, /*priority*/ 1);
982 }
983
984 static void
985 ctlfe_free_ccb(struct cam_periph *periph, union ccb *ccb)
986 {
987         struct ctlfe_lun_softc *softc;
988
989         softc = (struct ctlfe_lun_softc *)periph->softc;
990
991         switch (ccb->ccb_h.func_code) {
992         case XPT_ACCEPT_TARGET_IO:
993                 softc->atios_returned++;
994                 break;
995         case XPT_IMMEDIATE_NOTIFY:
996         case XPT_NOTIFY_ACKNOWLEDGE:
997                 softc->inots_returned++;
998                 break;
999         default:
1000                 break;
1001         }
1002
1003         ctl_free_io(ccb->ccb_h.io_ptr);
1004         free(ccb, M_CTLFE);
1005
1006         KASSERT(softc->atios_returned <= softc->atios_sent, ("%s: "
1007                 "atios_returned %ju > atios_sent %ju", __func__,
1008                 softc->atios_returned, softc->atios_sent));
1009         KASSERT(softc->inots_returned <= softc->inots_sent, ("%s: "
1010                 "inots_returned %ju > inots_sent %ju", __func__,
1011                 softc->inots_returned, softc->inots_sent));
1012
1013         /*
1014          * If we have received all of our CCBs, we can release our
1015          * reference on the peripheral driver.  It will probably go away
1016          * now.
1017          */
1018         if ((softc->atios_returned == softc->atios_sent)
1019          && (softc->inots_returned == softc->inots_sent)) {
1020                 cam_periph_release_locked(periph);
1021         }
1022 }
1023
1024 static int
1025 ctlfe_adjust_cdb(struct ccb_accept_tio *atio, uint32_t offset)
1026 {
1027         uint64_t lba;
1028         uint32_t num_blocks, nbc;
1029         uint8_t *cmdbyt = (atio->ccb_h.flags & CAM_CDB_POINTER)?
1030             atio->cdb_io.cdb_ptr : atio->cdb_io.cdb_bytes;
1031
1032         nbc = offset >> 9;      /* ASSUMING 512 BYTE BLOCKS */
1033
1034         switch (cmdbyt[0]) {
1035         case READ_6:
1036         case WRITE_6:
1037         {
1038                 struct scsi_rw_6 *cdb = (struct scsi_rw_6 *)cmdbyt;
1039                 lba = scsi_3btoul(cdb->addr);
1040                 lba &= 0x1fffff;
1041                 num_blocks = cdb->length;
1042                 if (num_blocks == 0)
1043                         num_blocks = 256;
1044                 lba += nbc;
1045                 num_blocks -= nbc;
1046                 scsi_ulto3b(lba, cdb->addr);
1047                 cdb->length = num_blocks;
1048                 break;
1049         }
1050         case READ_10:
1051         case WRITE_10:
1052         {
1053                 struct scsi_rw_10 *cdb = (struct scsi_rw_10 *)cmdbyt;
1054                 lba = scsi_4btoul(cdb->addr);
1055                 num_blocks = scsi_2btoul(cdb->length);
1056                 lba += nbc;
1057                 num_blocks -= nbc;
1058                 scsi_ulto4b(lba, cdb->addr);
1059                 scsi_ulto2b(num_blocks, cdb->length);
1060                 break;
1061         }
1062         case READ_12:
1063         case WRITE_12:
1064         {
1065                 struct scsi_rw_12 *cdb = (struct scsi_rw_12 *)cmdbyt;
1066                 lba = scsi_4btoul(cdb->addr);
1067                 num_blocks = scsi_4btoul(cdb->length);
1068                 lba += nbc;
1069                 num_blocks -= nbc;
1070                 scsi_ulto4b(lba, cdb->addr);
1071                 scsi_ulto4b(num_blocks, cdb->length);
1072                 break;
1073         }
1074         case READ_16:
1075         case WRITE_16:
1076         case WRITE_ATOMIC_16:
1077         {
1078                 struct scsi_rw_16 *cdb = (struct scsi_rw_16 *)cmdbyt;
1079                 lba = scsi_8btou64(cdb->addr);
1080                 num_blocks = scsi_4btoul(cdb->length);
1081                 lba += nbc;
1082                 num_blocks -= nbc;
1083                 scsi_u64to8b(lba, cdb->addr);
1084                 scsi_ulto4b(num_blocks, cdb->length);
1085                 break;
1086         }
1087         default:
1088                 return -1;
1089         }
1090         return (0);
1091 }
1092
1093 static void
1094 ctlfedone(struct cam_periph *periph, union ccb *done_ccb)
1095 {
1096         struct ctlfe_lun_softc *softc;
1097         struct ctlfe_softc *bus_softc;
1098         struct ccb_accept_tio *atio = NULL;
1099         union ctl_io *io = NULL;
1100         struct mtx *mtx;
1101
1102         KASSERT((done_ccb->ccb_h.flags & CAM_UNLOCKED) != 0,
1103             ("CCB in ctlfedone() without CAM_UNLOCKED flag"));
1104 #ifdef CTLFE_DEBUG
1105         printf("%s: entered, func_code = %#x\n", __func__,
1106                done_ccb->ccb_h.func_code);
1107 #endif
1108
1109         softc = (struct ctlfe_lun_softc *)periph->softc;
1110         bus_softc = softc->parent_softc;
1111         mtx = cam_periph_mtx(periph);
1112         mtx_lock(mtx);
1113
1114         /*
1115          * If the peripheral is invalid, ATIOs and immediate notify CCBs
1116          * need to be freed.  Most of the ATIOs and INOTs that come back
1117          * will be CCBs that are being returned from the SIM as a result of
1118          * our disabling the LUN.
1119          *
1120          * Other CCB types are handled in their respective cases below.
1121          */
1122         if (periph->flags & CAM_PERIPH_INVALID) {
1123                 switch (done_ccb->ccb_h.func_code) {
1124                 case XPT_ACCEPT_TARGET_IO:
1125                 case XPT_IMMEDIATE_NOTIFY:
1126                 case XPT_NOTIFY_ACKNOWLEDGE:
1127                         ctlfe_free_ccb(periph, done_ccb);
1128                         goto out;
1129                 default:
1130                         break;
1131                 }
1132
1133         }
1134         switch (done_ccb->ccb_h.func_code) {
1135         case XPT_ACCEPT_TARGET_IO: {
1136
1137                 atio = &done_ccb->atio;
1138
1139                 softc->atios_returned++;
1140
1141  resubmit:
1142                 /*
1143                  * Allocate a ctl_io, pass it to CTL, and wait for the
1144                  * datamove or done.
1145                  */
1146                 mtx_unlock(mtx);
1147                 io = done_ccb->ccb_h.io_ptr;
1148                 ctl_zero_io(io);
1149
1150                 /* Save pointers on both sides */
1151                 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr = done_ccb;
1152                 done_ccb->ccb_h.io_ptr = io;
1153
1154                 /*
1155                  * Only SCSI I/O comes down this path, resets, etc. come
1156                  * down the immediate notify path below.
1157                  */
1158                 io->io_hdr.io_type = CTL_IO_SCSI;
1159                 io->io_hdr.nexus.initid.id = atio->init_id;
1160                 io->io_hdr.nexus.targ_port = bus_softc->port.targ_port;
1161                 io->io_hdr.nexus.targ_target.id = atio->ccb_h.target_id;
1162                 io->io_hdr.nexus.targ_lun = atio->ccb_h.target_lun;
1163                 io->scsiio.tag_num = atio->tag_id;
1164                 switch (atio->tag_action) {
1165                 case CAM_TAG_ACTION_NONE:
1166                         io->scsiio.tag_type = CTL_TAG_UNTAGGED;
1167                         break;
1168                 case MSG_SIMPLE_TASK:
1169                         io->scsiio.tag_type = CTL_TAG_SIMPLE;
1170                         break;
1171                 case MSG_HEAD_OF_QUEUE_TASK:
1172                         io->scsiio.tag_type = CTL_TAG_HEAD_OF_QUEUE;
1173                         break;
1174                 case MSG_ORDERED_TASK:
1175                         io->scsiio.tag_type = CTL_TAG_ORDERED;
1176                         break;
1177                 case MSG_ACA_TASK:
1178                         io->scsiio.tag_type = CTL_TAG_ACA;
1179                         break;
1180                 default:
1181                         io->scsiio.tag_type = CTL_TAG_UNTAGGED;
1182                         printf("%s: unhandled tag type %#x!!\n", __func__,
1183                                atio->tag_action);
1184                         break;
1185                 }
1186                 if (atio->cdb_len > sizeof(io->scsiio.cdb)) {
1187                         printf("%s: WARNING: CDB len %d > ctl_io space %zd\n",
1188                                __func__, atio->cdb_len, sizeof(io->scsiio.cdb));
1189                 }
1190                 io->scsiio.cdb_len = min(atio->cdb_len, sizeof(io->scsiio.cdb));
1191                 bcopy(atio->cdb_io.cdb_bytes, io->scsiio.cdb,
1192                       io->scsiio.cdb_len);
1193
1194 #ifdef CTLFEDEBUG
1195                 printf("%s: %ju:%d:%ju:%d: tag %04x CDB %02x\n", __func__,
1196                         (uintmax_t)io->io_hdr.nexus.initid.id,
1197                         io->io_hdr.nexus.targ_port,
1198                         (uintmax_t)io->io_hdr.nexus.targ_target.id,
1199                         io->io_hdr.nexus.targ_lun,
1200                         io->scsiio.tag_num, io->scsiio.cdb[0]);
1201 #endif
1202
1203                 ctl_queue(io);
1204                 return;
1205         }
1206         case XPT_CONT_TARGET_IO: {
1207                 int srr = 0;
1208                 uint32_t srr_off = 0;
1209
1210                 atio = (struct ccb_accept_tio *)done_ccb->ccb_h.ccb_atio;
1211                 io = (union ctl_io *)atio->ccb_h.io_ptr;
1212
1213                 softc->ctios_returned++;
1214 #ifdef CTLFEDEBUG
1215                 printf("%s: got XPT_CONT_TARGET_IO tag %#x flags %#x\n",
1216                        __func__, atio->tag_id, done_ccb->ccb_h.flags);
1217 #endif
1218                 /*
1219                  * Handle SRR case were the data pointer is pushed back hack
1220                  */
1221                 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_MESSAGE_RECV
1222                     && done_ccb->csio.msg_ptr != NULL
1223                     && done_ccb->csio.msg_ptr[0] == MSG_EXTENDED
1224                     && done_ccb->csio.msg_ptr[1] == 5
1225                     && done_ccb->csio.msg_ptr[2] == 0) {
1226                         srr = 1;
1227                         srr_off =
1228                             (done_ccb->csio.msg_ptr[3] << 24)
1229                             | (done_ccb->csio.msg_ptr[4] << 16)
1230                             | (done_ccb->csio.msg_ptr[5] << 8)
1231                             | (done_ccb->csio.msg_ptr[6]);
1232                 }
1233
1234                 if (srr && (io->io_hdr.flags & CTL_FLAG_DMA_INPROG) == 0) {
1235                         /*
1236                          * If status was being sent, the back end data is now
1237                          * history. Hack it up and resubmit a new command with
1238                          * the CDB adjusted. If the SIM does the right thing,
1239                          * all of the resid math should work.
1240                          */
1241                         softc->ccbs_freed++;
1242                         xpt_release_ccb(done_ccb);
1243                         if (ctlfe_adjust_cdb(atio, srr_off) == 0) {
1244                                 done_ccb = (union ccb *)atio;
1245                                 goto resubmit;
1246                         }
1247                         /*
1248                          * Fall through to doom....
1249                          */
1250                 } else if (srr) {
1251                         /*
1252                          * If we have an srr and we're still sending data, we
1253                          * should be able to adjust offsets and cycle again.
1254                          */
1255                         io->scsiio.kern_rel_offset =
1256                             io->scsiio.ext_data_filled = srr_off;
1257                         io->scsiio.ext_data_len = io->scsiio.kern_total_len -
1258                             io->scsiio.kern_rel_offset;
1259                         softc->ccbs_freed++;
1260                         io->scsiio.io_hdr.status = CTL_STATUS_NONE;
1261                         xpt_release_ccb(done_ccb);
1262                         TAILQ_INSERT_HEAD(&softc->work_queue, &atio->ccb_h,
1263                                           periph_links.tqe);
1264                         xpt_schedule(periph, /*priority*/ 1);
1265                         break;
1266                 }
1267
1268                 /*
1269                  * If we were sending status back to the initiator, free up
1270                  * resources.  If we were doing a datamove, call the
1271                  * datamove done routine.
1272                  */
1273                 if ((io->io_hdr.flags & CTL_FLAG_DMA_INPROG) == 0) {
1274                         softc->ccbs_freed++;
1275                         xpt_release_ccb(done_ccb);
1276                         /*
1277                          * For a wildcard attachment, commands can come in
1278                          * with a specific target/lun.  Reset the target
1279                          * and LUN fields back to the wildcard values before
1280                          * we send them back down to the SIM.  The SIM has
1281                          * a wildcard LUN enabled, not whatever target/lun
1282                          * these happened to be.
1283                          */
1284                         if (softc->flags & CTLFE_LUN_WILDCARD) {
1285                                 atio->ccb_h.target_id = CAM_TARGET_WILDCARD;
1286                                 atio->ccb_h.target_lun = CAM_LUN_WILDCARD;
1287                         }
1288                         if (periph->flags & CAM_PERIPH_INVALID) {
1289                                 ctlfe_free_ccb(periph, (union ccb *)atio);
1290                         } else {
1291                                 softc->atios_sent++;
1292                                 mtx_unlock(mtx);
1293                                 xpt_action((union ccb *)atio);
1294                                 return;
1295                         }
1296                 } else {
1297                         struct ctlfe_lun_cmd_info *cmd_info;
1298                         struct ccb_scsiio *csio;
1299
1300                         csio = &done_ccb->csio;
1301                         cmd_info = (struct ctlfe_lun_cmd_info *)
1302                                 io->io_hdr.port_priv;
1303
1304                         io->io_hdr.flags &= ~CTL_FLAG_DMA_INPROG;
1305
1306                         io->scsiio.ext_data_len += csio->dxfer_len;
1307                         if (io->scsiio.ext_data_len >
1308                             io->scsiio.kern_total_len) {
1309                                 xpt_print(periph->path, "%s: tag 0x%04x "
1310                                           "done len %u > total %u sent %u\n",
1311                                           __func__, io->scsiio.tag_num,
1312                                           io->scsiio.ext_data_len,
1313                                           io->scsiio.kern_total_len,
1314                                           io->scsiio.ext_data_filled);
1315                         }
1316                         /*
1317                          * Translate CAM status to CTL status.  Success
1318                          * does not change the overall, ctl_io status.  In
1319                          * that case we just set port_status to 0.  If we
1320                          * have a failure, though, set a data phase error
1321                          * for the overall ctl_io.
1322                          */
1323                         switch (done_ccb->ccb_h.status & CAM_STATUS_MASK) {
1324                         case CAM_REQ_CMP:
1325                                 io->io_hdr.port_status = 0;
1326                                 break;
1327                         default:
1328                                 /*
1329                                  * XXX KDM we probably need to figure out a
1330                                  * standard set of errors that the SIM
1331                                  * drivers should return in the event of a
1332                                  * data transfer failure.  A data phase
1333                                  * error will at least point the user to a
1334                                  * data transfer error of some sort.
1335                                  * Hopefully the SIM printed out some
1336                                  * additional information to give the user
1337                                  * a clue what happened.
1338                                  */
1339                                 io->io_hdr.port_status = 0xbad1;
1340                                 ctl_set_data_phase_error(&io->scsiio);
1341                                 /*
1342                                  * XXX KDM figure out residual.
1343                                  */
1344                                 break;
1345                         }
1346                         /*
1347                          * If we had to break this S/G list into multiple
1348                          * pieces, figure out where we are in the list, and
1349                          * continue sending pieces if necessary.
1350                          */
1351                         if ((cmd_info->flags & CTLFE_CMD_PIECEWISE)
1352                          && (io->io_hdr.port_status == 0)) {
1353                                 ccb_flags flags;
1354                                 uint8_t scsi_status;
1355                                 uint8_t *data_ptr;
1356                                 uint32_t dxfer_len;
1357
1358                                 flags = atio->ccb_h.flags &
1359                                         (CAM_DIS_DISCONNECT|
1360                                          CAM_TAG_ACTION_VALID);
1361
1362                                 ctlfedata(softc, io, &flags, &data_ptr,
1363                                     &dxfer_len, &csio->sglist_cnt);
1364
1365                                 scsi_status = 0;
1366
1367                                 if (((flags & CAM_SEND_STATUS) == 0)
1368                                  && (dxfer_len == 0)) {
1369                                         printf("%s: tag %04x no status or "
1370                                                "len cdb = %02x\n", __func__,
1371                                                atio->tag_id,
1372                                         atio->cdb_io.cdb_bytes[0]);
1373                                         printf("%s: tag %04x io status %#x\n",
1374                                                __func__, atio->tag_id,
1375                                                io->io_hdr.status);
1376                                 }
1377
1378                                 cam_fill_ctio(csio,
1379                                               /*retries*/ 2,
1380                                               ctlfedone,
1381                                               flags,
1382                                               (flags & CAM_TAG_ACTION_VALID) ?
1383                                                MSG_SIMPLE_Q_TAG : 0,
1384                                               atio->tag_id,
1385                                               atio->init_id,
1386                                               scsi_status,
1387                                               /*data_ptr*/ data_ptr,
1388                                               /*dxfer_len*/ dxfer_len,
1389                                               /*timeout*/ 5 * 1000);
1390
1391                                 csio->ccb_h.flags |= CAM_UNLOCKED;
1392                                 csio->resid = 0;
1393                                 csio->ccb_h.ccb_atio = atio;
1394                                 io->io_hdr.flags |= CTL_FLAG_DMA_INPROG;
1395                                 softc->ctios_sent++;
1396                                 mtx_unlock(mtx);
1397                                 xpt_action((union ccb *)csio);
1398                         } else {
1399                                 /*
1400                                  * Release the CTIO.  The ATIO will be sent back
1401                                  * down to the SIM once we send status.
1402                                  */
1403                                 softc->ccbs_freed++;
1404                                 xpt_release_ccb(done_ccb);
1405                                 mtx_unlock(mtx);
1406
1407                                 /* Call the backend move done callback */
1408                                 io->scsiio.be_move_done(io);
1409                         }
1410                         return;
1411                 }
1412                 break;
1413         }
1414         case XPT_IMMEDIATE_NOTIFY: {
1415                 union ctl_io *io;
1416                 struct ccb_immediate_notify *inot;
1417                 cam_status status;
1418                 int frozen, send_ctl_io;
1419
1420                 inot = &done_ccb->cin1;
1421
1422                 softc->inots_returned++;
1423
1424                 frozen = (done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0;
1425
1426                 printf("%s: got XPT_IMMEDIATE_NOTIFY status %#x tag %#x "
1427                        "seq %#x\n", __func__, inot->ccb_h.status,
1428                        inot->tag_id, inot->seq_id);
1429
1430                 io = done_ccb->ccb_h.io_ptr;
1431                 ctl_zero_io(io);
1432
1433                 send_ctl_io = 1;
1434
1435                 io->io_hdr.io_type = CTL_IO_TASK;
1436                 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr =done_ccb;
1437                 inot->ccb_h.io_ptr = io;
1438                 io->io_hdr.nexus.initid.id = inot->initiator_id;
1439                 io->io_hdr.nexus.targ_port = bus_softc->port.targ_port;
1440                 io->io_hdr.nexus.targ_target.id = inot->ccb_h.target_id;
1441                 io->io_hdr.nexus.targ_lun = inot->ccb_h.target_lun;
1442                 /* XXX KDM should this be the tag_id? */
1443                 io->taskio.tag_num = inot->seq_id;
1444
1445                 status = inot->ccb_h.status & CAM_STATUS_MASK;
1446                 switch (status) {
1447                 case CAM_SCSI_BUS_RESET:
1448                         io->taskio.task_action = CTL_TASK_BUS_RESET;
1449                         break;
1450                 case CAM_BDR_SENT:
1451                         io->taskio.task_action = CTL_TASK_TARGET_RESET;
1452                         break;
1453                 case CAM_MESSAGE_RECV:
1454                         switch (inot->arg) {
1455                         case MSG_ABORT_TASK_SET:
1456                                 io->taskio.task_action =
1457                                     CTL_TASK_ABORT_TASK_SET;
1458                                 break;
1459                         case MSG_TARGET_RESET:
1460                                 io->taskio.task_action =
1461                                         CTL_TASK_TARGET_RESET;
1462                                 break;
1463                         case MSG_ABORT_TASK:
1464                                 io->taskio.task_action =
1465                                         CTL_TASK_ABORT_TASK;
1466                                 break;
1467                         case MSG_LOGICAL_UNIT_RESET:
1468                                 io->taskio.task_action =
1469                                         CTL_TASK_LUN_RESET;
1470                                 break;
1471                         case MSG_CLEAR_TASK_SET:
1472                                 io->taskio.task_action =
1473                                         CTL_TASK_CLEAR_TASK_SET;
1474                                 break;
1475                         case MSG_CLEAR_ACA:
1476                                 io->taskio.task_action =
1477                                         CTL_TASK_CLEAR_ACA;
1478                                 break;
1479                         case MSG_NOOP:
1480                                 send_ctl_io = 0;
1481                                 break;
1482                         default:
1483                                 xpt_print(periph->path,
1484                                           "%s: unsupported message 0x%x\n",
1485                                           __func__, inot->arg);
1486                                 send_ctl_io = 0;
1487                                 break;
1488                         }
1489                         break;
1490                 case CAM_REQ_ABORTED:
1491                         /*
1492                          * This request was sent back by the driver.
1493                          * XXX KDM what do we do here?
1494                          */
1495                         send_ctl_io = 0;
1496                         break;
1497                 case CAM_REQ_INVALID:
1498                 case CAM_PROVIDE_FAIL:
1499                 default:
1500                         /*
1501                          * We should only get here if we're talking
1502                          * to a talking to a SIM that is target
1503                          * capable but supports the old API.  In
1504                          * that case, we need to just free the CCB.
1505                          * If we actually send a notify acknowledge,
1506                          * it will send that back with an error as
1507                          * well.
1508                          */
1509
1510                         if ((status != CAM_REQ_INVALID)
1511                          && (status != CAM_PROVIDE_FAIL))
1512                                 xpt_print(periph->path,
1513                                           "%s: unsupported CAM status 0x%x\n",
1514                                           __func__, status);
1515
1516                         ctlfe_free_ccb(periph, done_ccb);
1517
1518                         goto out;
1519                 }
1520                 if (send_ctl_io != 0) {
1521                         ctl_queue(io);
1522                 } else {
1523                         done_ccb->ccb_h.status = CAM_REQ_INPROG;
1524                         done_ccb->ccb_h.func_code = XPT_NOTIFY_ACKNOWLEDGE;
1525                         xpt_action(done_ccb);
1526                 }
1527
1528                 if (frozen != 0) {
1529                         cam_release_devq(periph->path,
1530                                          /*relsim_flags*/ 0,
1531                                          /*opening reduction*/ 0,
1532                                          /*timeout*/ 0,
1533                                          /*getcount_only*/ 0);
1534                 }
1535                 break;
1536         }
1537         case XPT_NOTIFY_ACKNOWLEDGE:
1538                 /*
1539                  * Queue this back down to the SIM as an immediate notify.
1540                  */
1541                 done_ccb->ccb_h.func_code = XPT_IMMEDIATE_NOTIFY;
1542                 xpt_action(done_ccb);
1543                 softc->inots_sent++;
1544                 break;
1545         case XPT_SET_SIM_KNOB:
1546         case XPT_GET_SIM_KNOB:
1547                 break;
1548         default:
1549                 panic("%s: unexpected CCB type %#x", __func__,
1550                       done_ccb->ccb_h.func_code);
1551                 break;
1552         }
1553
1554 out:
1555         mtx_unlock(mtx);
1556 }
1557
1558 static void
1559 ctlfe_onoffline(void *arg, int online)
1560 {
1561         struct ctlfe_softc *bus_softc;
1562         union ccb *ccb;
1563         cam_status status;
1564         struct cam_path *path;
1565         int set_wwnn;
1566
1567         bus_softc = (struct ctlfe_softc *)arg;
1568
1569         set_wwnn = 0;
1570
1571         status = xpt_create_path(&path, /*periph*/ NULL, bus_softc->path_id,
1572                 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
1573         if (status != CAM_REQ_CMP) {
1574                 printf("%s: unable to create path!\n", __func__);
1575                 return;
1576         }
1577         ccb = xpt_alloc_ccb();
1578         xpt_setup_ccb(&ccb->ccb_h, path, CAM_PRIORITY_NONE);
1579
1580         /*
1581          * Copan WWN format:
1582          *
1583          * Bits 63-60:  0x5             NAA, IEEE registered name
1584          * Bits 59-36:  0x000ED5        IEEE Company name assigned to Copan
1585          * Bits 35-12:                  Copan SSN (Sequential Serial Number)
1586          * Bits 11-8:                   Type of port:
1587          *                                      1 == N-Port
1588          *                                      2 == F-Port
1589          *                                      3 == NL-Port
1590          * Bits 7-0:                    0 == Node Name, >0 == Port Number
1591          */
1592
1593         if (online != 0) {
1594
1595                 ccb->ccb_h.func_code = XPT_GET_SIM_KNOB;
1596
1597
1598                 xpt_action(ccb);
1599
1600
1601                 if ((ccb->knob.xport_specific.valid & KNOB_VALID_ADDRESS) != 0){
1602 #ifdef RANDOM_WWNN
1603                         uint64_t random_bits;
1604 #endif
1605
1606                         printf("%s: %s current WWNN %#jx\n", __func__,
1607                                bus_softc->port_name,
1608                                ccb->knob.xport_specific.fc.wwnn);
1609                         printf("%s: %s current WWPN %#jx\n", __func__,
1610                                bus_softc->port_name,
1611                                ccb->knob.xport_specific.fc.wwpn);
1612
1613 #ifdef RANDOM_WWNN
1614                         arc4rand(&random_bits, sizeof(random_bits), 0);
1615 #endif
1616
1617                         /*
1618                          * XXX KDM this is a bit of a kludge for now.  We
1619                          * take the current WWNN/WWPN from the card, and
1620                          * replace the company identifier and the NL-Port
1621                          * indicator and the port number (for the WWPN).
1622                          * This should be replaced later with ddb_GetWWNN,
1623                          * or possibly a more centralized scheme.  (It
1624                          * would be nice to have the WWNN/WWPN for each
1625                          * port stored in the ctl_port structure.)
1626                          */
1627 #ifdef RANDOM_WWNN
1628                         ccb->knob.xport_specific.fc.wwnn = 
1629                                 (random_bits &
1630                                 0x0000000fffffff00ULL) |
1631                                 /* Company ID */ 0x5000ED5000000000ULL |
1632                                 /* NL-Port */    0x0300;
1633                         ccb->knob.xport_specific.fc.wwpn = 
1634                                 (random_bits &
1635                                 0x0000000fffffff00ULL) |
1636                                 /* Company ID */ 0x5000ED5000000000ULL |
1637                                 /* NL-Port */    0x3000 |
1638                                 /* Port Num */ (bus_softc->port.targ_port & 0xff);
1639
1640                         /*
1641                          * This is a bit of an API break/reversal, but if
1642                          * we're doing the random WWNN that's a little
1643                          * different anyway.  So record what we're actually
1644                          * using with the frontend code so it's reported
1645                          * accurately.
1646                          */
1647                         ctl_port_set_wwns(&bus_softc->port,
1648                             true, ccb->knob.xport_specific.fc.wwnn,
1649                             true, ccb->knob.xport_specific.fc.wwpn);
1650                         set_wwnn = 1;
1651 #else /* RANDOM_WWNN */
1652                         /*
1653                          * If the user has specified a WWNN/WWPN, send them
1654                          * down to the SIM.  Otherwise, record what the SIM
1655                          * has reported.
1656                          */
1657                         if ((bus_softc->port.wwnn != 0)
1658                          && (bus_softc->port.wwpn != 0)) {
1659                                 ccb->knob.xport_specific.fc.wwnn =
1660                                         bus_softc->port.wwnn;
1661                                 ccb->knob.xport_specific.fc.wwpn =
1662                                         bus_softc->port.wwpn;
1663                                 set_wwnn = 1;
1664                         } else {
1665                                 ctl_port_set_wwns(&bus_softc->port,
1666                                     true, ccb->knob.xport_specific.fc.wwnn,
1667                                     true, ccb->knob.xport_specific.fc.wwpn);
1668                         }
1669 #endif /* RANDOM_WWNN */
1670
1671
1672                         if (set_wwnn != 0) {
1673                                 printf("%s: %s new WWNN %#jx\n", __func__,
1674                                        bus_softc->port_name,
1675                                 ccb->knob.xport_specific.fc.wwnn);
1676                                 printf("%s: %s new WWPN %#jx\n", __func__,
1677                                        bus_softc->port_name,
1678                                        ccb->knob.xport_specific.fc.wwpn);
1679                         }
1680                 } else {
1681                         printf("%s: %s has no valid WWNN/WWPN\n", __func__,
1682                                bus_softc->port_name);
1683                 }
1684         }
1685         ccb->ccb_h.func_code = XPT_SET_SIM_KNOB;
1686         ccb->knob.xport_specific.valid = KNOB_VALID_ROLE;
1687         if (set_wwnn != 0)
1688                 ccb->knob.xport_specific.valid |= KNOB_VALID_ADDRESS;
1689
1690         if (online != 0)
1691                 ccb->knob.xport_specific.fc.role = KNOB_ROLE_TARGET;
1692         else
1693                 ccb->knob.xport_specific.fc.role = KNOB_ROLE_NONE;
1694
1695         xpt_action(ccb);
1696
1697         if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1698                 printf("%s: SIM %s (path id %d) target %s failed with "
1699                        "status %#x\n",
1700                        __func__, bus_softc->port_name, bus_softc->path_id,
1701                        (online != 0) ? "enable" : "disable",
1702                        ccb->ccb_h.status);
1703         } else {
1704                 printf("%s: SIM %s (path id %d) target %s succeeded\n",
1705                        __func__, bus_softc->port_name, bus_softc->path_id,
1706                        (online != 0) ? "enable" : "disable");
1707         }
1708
1709         xpt_free_path(path);
1710         xpt_free_ccb(ccb);
1711 }
1712
1713 static void
1714 ctlfe_online(void *arg)
1715 {
1716         struct ctlfe_softc *bus_softc;
1717         struct cam_path *path;
1718         cam_status status;
1719         struct ctlfe_lun_softc *lun_softc;
1720         struct cam_periph *periph;
1721
1722         bus_softc = (struct ctlfe_softc *)arg;
1723
1724         /*
1725          * Create the wildcard LUN before bringing the port online.
1726          */
1727         status = xpt_create_path(&path, /*periph*/ NULL,
1728                                  bus_softc->path_id, CAM_TARGET_WILDCARD,
1729                                  CAM_LUN_WILDCARD);
1730         if (status != CAM_REQ_CMP) {
1731                 printf("%s: unable to create path for wildcard periph\n",
1732                                 __func__);
1733                 return;
1734         }
1735
1736         lun_softc = malloc(sizeof(*lun_softc), M_CTLFE, M_WAITOK | M_ZERO);
1737
1738         xpt_path_lock(path);
1739         periph = cam_periph_find(path, "ctl");
1740         if (periph != NULL) {
1741                 /* We've already got a periph, no need to alloc a new one. */
1742                 xpt_path_unlock(path);
1743                 xpt_free_path(path);
1744                 free(lun_softc, M_CTLFE);
1745                 return;
1746         }
1747         lun_softc->parent_softc = bus_softc;
1748         lun_softc->flags |= CTLFE_LUN_WILDCARD;
1749
1750         status = cam_periph_alloc(ctlferegister,
1751                                   ctlfeoninvalidate,
1752                                   ctlfecleanup,
1753                                   ctlfestart,
1754                                   "ctl",
1755                                   CAM_PERIPH_BIO,
1756                                   path,
1757                                   ctlfeasync,
1758                                   0,
1759                                   lun_softc);
1760
1761         if ((status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1762                 const struct cam_status_entry *entry;
1763
1764                 entry = cam_fetch_status_entry(status);
1765                 printf("%s: CAM error %s (%#x) returned from "
1766                        "cam_periph_alloc()\n", __func__, (entry != NULL) ?
1767                        entry->status_text : "Unknown", status);
1768                 free(lun_softc, M_CTLFE);
1769         }
1770
1771         xpt_path_unlock(path);
1772         ctlfe_onoffline(arg, /*online*/ 1);
1773         xpt_free_path(path);
1774 }
1775
1776 static void
1777 ctlfe_offline(void *arg)
1778 {
1779         struct ctlfe_softc *bus_softc;
1780         struct cam_path *path;
1781         cam_status status;
1782         struct cam_periph *periph;
1783
1784         bus_softc = (struct ctlfe_softc *)arg;
1785
1786         ctlfe_onoffline(arg, /*online*/ 0);
1787
1788         /*
1789          * Disable the wildcard LUN for this port now that we have taken
1790          * the port offline.
1791          */
1792         status = xpt_create_path(&path, /*periph*/ NULL,
1793                                  bus_softc->path_id, CAM_TARGET_WILDCARD,
1794                                  CAM_LUN_WILDCARD);
1795         if (status != CAM_REQ_CMP) {
1796                 printf("%s: unable to create path for wildcard periph\n",
1797                        __func__);
1798                 return;
1799         }
1800         xpt_path_lock(path);
1801         if ((periph = cam_periph_find(path, "ctl")) != NULL)
1802                 cam_periph_invalidate(periph);
1803         xpt_path_unlock(path);
1804         xpt_free_path(path);
1805 }
1806
1807 /*
1808  * This will get called to enable a LUN on every bus that is attached to
1809  * CTL.  So we only need to create a path/periph for this particular bus.
1810  */
1811 static int
1812 ctlfe_lun_enable(void *arg, struct ctl_id targ_id, int lun_id)
1813 {
1814         struct ctlfe_softc *bus_softc;
1815         struct ctlfe_lun_softc *softc;
1816         struct cam_path *path;
1817         struct cam_periph *periph;
1818         cam_status status;
1819
1820         bus_softc = (struct ctlfe_softc *)arg;
1821
1822         status = xpt_create_path(&path, /*periph*/ NULL,
1823                                   bus_softc->path_id,
1824                                   targ_id.id, lun_id);
1825         /* XXX KDM need some way to return status to CTL here? */
1826         if (status != CAM_REQ_CMP) {
1827                 printf("%s: could not create path, status %#x\n", __func__,
1828                        status);
1829                 return (1);
1830         }
1831
1832         softc = malloc(sizeof(*softc), M_CTLFE, M_WAITOK | M_ZERO);
1833         xpt_path_lock(path);
1834         periph = cam_periph_find(path, "ctl");
1835         if (periph != NULL) {
1836                 /* We've already got a periph, no need to alloc a new one. */
1837                 xpt_path_unlock(path);
1838                 xpt_free_path(path);
1839                 free(softc, M_CTLFE);
1840                 return (0);
1841         }
1842         softc->parent_softc = bus_softc;
1843
1844         status = cam_periph_alloc(ctlferegister,
1845                                   ctlfeoninvalidate,
1846                                   ctlfecleanup,
1847                                   ctlfestart,
1848                                   "ctl",
1849                                   CAM_PERIPH_BIO,
1850                                   path,
1851                                   ctlfeasync,
1852                                   0,
1853                                   softc);
1854
1855         if ((status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1856                 const struct cam_status_entry *entry;
1857
1858                 entry = cam_fetch_status_entry(status);
1859                 printf("%s: CAM error %s (%#x) returned from "
1860                        "cam_periph_alloc()\n", __func__, (entry != NULL) ?
1861                        entry->status_text : "Unknown", status);
1862                 free(softc, M_CTLFE);
1863         }
1864
1865         xpt_path_unlock(path);
1866         xpt_free_path(path);
1867         return (0);
1868 }
1869
1870 /*
1871  * This will get called when the user removes a LUN to disable that LUN
1872  * on every bus that is attached to CTL.  
1873  */
1874 static int
1875 ctlfe_lun_disable(void *arg, struct ctl_id targ_id, int lun_id)
1876 {
1877         struct ctlfe_softc *softc;
1878         struct ctlfe_lun_softc *lun_softc;
1879
1880         softc = (struct ctlfe_softc *)arg;
1881
1882         mtx_lock(&softc->lun_softc_mtx);
1883         STAILQ_FOREACH(lun_softc, &softc->lun_softc_list, links) {
1884                 struct cam_path *path;
1885
1886                 path = lun_softc->periph->path;
1887
1888                 if ((xpt_path_target_id(path) == targ_id.id)
1889                  && (xpt_path_lun_id(path) == lun_id)) {
1890                         break;
1891                 }
1892         }
1893         if (lun_softc == NULL) {
1894                 mtx_unlock(&softc->lun_softc_mtx);
1895                 printf("%s: can't find target %d lun %d\n", __func__,
1896                        targ_id.id, lun_id);
1897                 return (1);
1898         }
1899         cam_periph_acquire(lun_softc->periph);
1900         mtx_unlock(&softc->lun_softc_mtx);
1901
1902         cam_periph_lock(lun_softc->periph);
1903         cam_periph_invalidate(lun_softc->periph);
1904         cam_periph_unlock(lun_softc->periph);
1905         cam_periph_release(lun_softc->periph);
1906         return (0);
1907 }
1908
1909 static void
1910 ctlfe_dump_sim(struct cam_sim *sim)
1911 {
1912
1913         printf("%s%d: max tagged openings: %d, max dev openings: %d\n",
1914                sim->sim_name, sim->unit_number,
1915                sim->max_tagged_dev_openings, sim->max_dev_openings);
1916 }
1917
1918 /*
1919  * Assumes that the SIM lock is held.
1920  */
1921 static void
1922 ctlfe_dump_queue(struct ctlfe_lun_softc *softc)
1923 {
1924         struct ccb_hdr *hdr;
1925         struct cam_periph *periph;
1926         int num_items;
1927
1928         periph = softc->periph;
1929         num_items = 0;
1930
1931         TAILQ_FOREACH(hdr, &softc->work_queue, periph_links.tqe) {
1932                 union ctl_io *io = hdr->io_ptr;
1933
1934                 num_items++;
1935
1936                 /*
1937                  * Only regular SCSI I/O is put on the work
1938                  * queue, so we can print sense here.  There may be no
1939                  * sense if it's no the queue for a DMA, but this serves to
1940                  * print out the CCB as well.
1941                  *
1942                  * XXX KDM switch this over to scsi_sense_print() when
1943                  * CTL is merged in with CAM.
1944                  */
1945                 ctl_io_error_print(io, NULL);
1946
1947                 /*
1948                  * Print DMA status if we are DMA_QUEUED.
1949                  */
1950                 if (io->io_hdr.flags & CTL_FLAG_DMA_QUEUED) {
1951                         xpt_print(periph->path,
1952                             "Total %u, Current %u, Resid %u\n",
1953                             io->scsiio.kern_total_len,
1954                             io->scsiio.kern_data_len,
1955                             io->scsiio.kern_data_resid);
1956                 }
1957         }
1958
1959         xpt_print(periph->path, "%d requests total waiting for CCBs\n",
1960                   num_items);
1961         xpt_print(periph->path, "%ju CCBs outstanding (%ju allocated, %ju "
1962                   "freed)\n", (uintmax_t)(softc->ccbs_alloced -
1963                   softc->ccbs_freed), (uintmax_t)softc->ccbs_alloced,
1964                   (uintmax_t)softc->ccbs_freed);
1965         xpt_print(periph->path, "%ju CTIOs outstanding (%ju sent, %ju "
1966                   "returned\n", (uintmax_t)(softc->ctios_sent -
1967                   softc->ctios_returned), softc->ctios_sent,
1968                   softc->ctios_returned);
1969 }
1970
1971 /*
1972  * Datamove/done routine called by CTL.  Put ourselves on the queue to
1973  * receive a CCB from CAM so we can queue the continue I/O request down
1974  * to the adapter.
1975  */
1976 static void
1977 ctlfe_datamove(union ctl_io *io)
1978 {
1979         union ccb *ccb;
1980         struct cam_periph *periph;
1981         struct ctlfe_lun_softc *softc;
1982
1983         KASSERT(io->io_hdr.io_type == CTL_IO_SCSI,
1984             ("Unexpected io_type (%d) in ctlfe_datamove", io->io_hdr.io_type));
1985
1986         ccb = io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr;
1987         periph = xpt_path_periph(ccb->ccb_h.path);
1988         cam_periph_lock(periph);
1989         softc = (struct ctlfe_lun_softc *)periph->softc;
1990         io->io_hdr.flags |= CTL_FLAG_DMA_QUEUED;
1991         if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE)
1992                 io->io_hdr.flags |= CTL_FLAG_STATUS_QUEUED;
1993         TAILQ_INSERT_TAIL(&softc->work_queue, &ccb->ccb_h,
1994                           periph_links.tqe);
1995         xpt_schedule(periph, /*priority*/ 1);
1996         cam_periph_unlock(periph);
1997 }
1998
1999 static void
2000 ctlfe_done(union ctl_io *io)
2001 {
2002         union ccb *ccb;
2003         struct cam_periph *periph;
2004         struct ctlfe_lun_softc *softc;
2005
2006         ccb = io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr;
2007         periph = xpt_path_periph(ccb->ccb_h.path);
2008         cam_periph_lock(periph);
2009         softc = (struct ctlfe_lun_softc *)periph->softc;
2010
2011         if (io->io_hdr.io_type == CTL_IO_TASK) {
2012                 /*
2013                  * Task management commands don't require any further
2014                  * communication back to the adapter.  Requeue the CCB
2015                  * to the adapter, and free the CTL I/O.
2016                  */
2017                 xpt_print(ccb->ccb_h.path, "%s: returning task I/O "
2018                           "tag %#x seq %#x\n", __func__,
2019                           ccb->cin1.tag_id, ccb->cin1.seq_id);
2020                 /*
2021                  * Send the notify acknowledge down to the SIM, to let it
2022                  * know we processed the task management command.
2023                  */
2024                 ccb->ccb_h.status = CAM_REQ_INPROG;
2025                 ccb->ccb_h.func_code = XPT_NOTIFY_ACKNOWLEDGE;
2026                 xpt_action(ccb);
2027         } else if (io->io_hdr.flags & CTL_FLAG_STATUS_SENT) {
2028                 if (softc->flags & CTLFE_LUN_WILDCARD) {
2029                         ccb->ccb_h.target_id = CAM_TARGET_WILDCARD;
2030                         ccb->ccb_h.target_lun = CAM_LUN_WILDCARD;
2031                 }
2032                 if (periph->flags & CAM_PERIPH_INVALID) {
2033                         ctlfe_free_ccb(periph, ccb);
2034                 } else {
2035                         softc->atios_sent++;
2036                         cam_periph_unlock(periph);
2037                         xpt_action(ccb);
2038                         return;
2039                 }
2040         } else {
2041                 io->io_hdr.flags |= CTL_FLAG_STATUS_QUEUED;
2042                 TAILQ_INSERT_TAIL(&softc->work_queue, &ccb->ccb_h,
2043                                   periph_links.tqe);
2044                 xpt_schedule(periph, /*priority*/ 1);
2045         }
2046
2047         cam_periph_unlock(periph);
2048 }
2049
2050 static void
2051 ctlfe_dump(void)
2052 {
2053         struct ctlfe_softc *bus_softc;
2054         struct ctlfe_lun_softc *lun_softc;
2055
2056         STAILQ_FOREACH(bus_softc, &ctlfe_softc_list, links) {
2057                 ctlfe_dump_sim(bus_softc->sim);
2058                 STAILQ_FOREACH(lun_softc, &bus_softc->lun_softc_list, links)
2059                         ctlfe_dump_queue(lun_softc);
2060         }
2061 }